<commit_before><commit_msg>add version command<commit_after>package main

import (
	"bytes"
	"flag"
	"fmt"
	"go/format"
	"io"
	"log"
	"os"
	"os/exec"
	"strings"
	"text/template"
)

func version() (string, error) {
	cmd := exec.Command("git", "log", "-n", "1", "--format=format:+%h %cd", "HEAD")
	data, err := cmd.Output()
	if err != nil {
		return "", err
	}
	return string(data), nil
}

type Version struct {
	Generated string
	Pkgname   string
	Version   string
}

func main() {
	var (
		output  = flag.String("o", "", "write version information to file")
		pkgname = flag.String("pkg", "main", "generated package name")
		prefix  = flag.String("prefix", "", "version information prefix")
	)

	flag.Parse()

	var w io.Writer
	if *output != "" {
		f, err := os.Create(*output)
		if err != nil {
			log.Fatal(err)
		}
		defer f.Close()
		w = f
	} else {
		w = os.Stdout
	}

	tmpl, err := template.New("version").Parse(`{{.Generated}}; DO NOT EDIT!

package {{.Pkgname}}

func Version() string { return "{{.Version}}" }`)
	if err != nil {
		log.Fatal(err)
	}

	version, err := version()
	if err != nil {
		log.Fatal(err)
	}

	v := Version{
		Generated: fmt.Sprintf(`// Code generated by %q`, strings.Join(os.Args, " ")),
		Pkgname:   *pkgname,
	}
	if *prefix != "" {
		v.Version = *prefix + " " + version
	} else {
		v.Version = version
	}

	buf := bytes.Buffer{}
	if err = tmpl.Execute(&buf, v); err != nil {
		log.Fatal(err)
	}

	data, err := format.Source(buf.Bytes())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Fprintf(w, "%s\n", data)
}
{'fraction_non_alphanumeric': 0.1968810916179337, 'fraction_numerical': 0.000649772579597141, 'mean_word_length': 3.2777777777777777, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2706725', 'n_tokens_mistral': 626, 'n_tokens_neox': 593, 'n_words': 150}