|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/schollz/progressbar/v2" |
| 11 | +) |
| 12 | + |
| 13 | +type entry struct { |
| 14 | + commit string |
| 15 | + timestamp int |
| 16 | + data string |
| 17 | +} |
| 18 | + |
| 19 | +var directory string |
| 20 | +var command string |
| 21 | +var branch string |
| 22 | +var outputFile string |
| 23 | +var limit int |
| 24 | +var after string |
| 25 | +var before string |
| 26 | + |
| 27 | +func main() { |
| 28 | + flag.StringVar(&directory, "directory", "", "git workspace (by default current directory)") |
| 29 | + flag.StringVar(&command, "command", "", "command to execute") |
| 30 | + flag.StringVar(&branch, "branch", "master", "git branch to check") |
| 31 | + flag.StringVar(&outputFile, "output", "out.csv", "output csv file") |
| 32 | + flag.StringVar(&after, "after", "", "optional begin date of history search") |
| 33 | + flag.StringVar(&before, "before", "", "optional end date of history search") |
| 34 | + flag.IntVar(&limit, "limit", 500, "max number of commits to check") |
| 35 | + flag.Parse() |
| 36 | + |
| 37 | + // if no parameter given, use history of current history |
| 38 | + var err error |
| 39 | + if directory == "" { |
| 40 | + directory, err = os.Getwd() |
| 41 | + checkError(err) |
| 42 | + } |
| 43 | + |
| 44 | + if stat, err := os.Stat(directory); err != nil || !stat.IsDir() { |
| 45 | + fmt.Printf("Invalid directory: %s\n", directory) |
| 46 | + flag.PrintDefaults() |
| 47 | + os.Exit(1) |
| 48 | + } |
| 49 | + |
| 50 | + commits, err := getCommits(branch, limit, after, before) |
| 51 | + checkError(err) |
| 52 | + |
| 53 | + bar := progressbar.New(len(commits)) |
| 54 | + result := make([]entry, 0, len(commits)) |
| 55 | + |
| 56 | + for _, commit := range commits { |
| 57 | + bar.Add(1) |
| 58 | + result = append(result, evaluate(commit)) |
| 59 | + } |
| 60 | + |
| 61 | + // checkout to initial version |
| 62 | + checkout(branch) |
| 63 | + |
| 64 | + out, err := os.Create(outputFile) |
| 65 | + checkError(err) |
| 66 | + defer out.Close() |
| 67 | + |
| 68 | + err = writeCsv(result, out) |
| 69 | + checkError(err) |
| 70 | + fmt.Printf("\nWrote file %s\n", outputFile) |
| 71 | +} |
| 72 | + |
| 73 | +func checkError(err error) { |
| 74 | + if err != nil { |
| 75 | + fmt.Println(err) |
| 76 | + os.Exit(1) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func evaluate(commit entry) entry { |
| 81 | + checkout(commit.commit) |
| 82 | + |
| 83 | + stdout, _ := execute("sh", "-c", command) |
| 84 | + |
| 85 | + commit.data = strings.TrimSpace(stdout) |
| 86 | + |
| 87 | + return commit |
| 88 | +} |
| 89 | + |
| 90 | +func execute(name string, args ...string) (string, error) { |
| 91 | + cmd := exec.Command(name, args...) |
| 92 | + cmd.Dir = directory |
| 93 | + out, err := cmd.CombinedOutput() |
| 94 | + |
| 95 | + if err != nil { |
| 96 | + fmt.Fprintf(os.Stderr, "Command '%s' failed: %s: %s", cmd.String(), err, string(out)) |
| 97 | + os.Exit(1) |
| 98 | + } |
| 99 | + |
| 100 | + return string(out), err |
| 101 | +} |
0 commit comments