Debian Codegraph @

internal/cmd/benchcheck/git.go

raw
1 package main
2
3 import (
4 "bytes"
5 "context"
6 "fmt"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "strings"
11 )
12
13 // defaultBaseRefs are tried in order when the base ref is not specified.
14 // The remote-tracking branch is for environments without the local branch, such as CI.
15 var defaultBaseRefs = []string{"master", "origin/master"}
16
17 type repository struct {
18 root string
19 }
20
21 func (r *repository) git(ctx context.Context, args ...string) (string, error) {
22 var stdout, stderr bytes.Buffer
23 cmd := exec.CommandContext(ctx, "git", args...)
24 cmd.Dir = r.root
25 cmd.Stdout = &stdout
26 cmd.Stderr = &stderr
27 if err := cmd.Run(); err != nil {
28 return "", fmt.Errorf("failed to run git %s: %w: %s", strings.Join(args, " "), err, stderr.String())
29 }
30 return strings.TrimSpace(stdout.String()), nil
31 }
32
33 func openRepository(ctx context.Context) (*repository, error) {
34 wd, err := os.Getwd()
35 if err != nil {
36 return nil, err
37 }
38 root, err := (&repository{root: wd}).git(ctx, "rev-parse", "--show-toplevel")
39 if err != nil {
40 return nil, err
41 }
42 return &repository{root: root}, nil
43 }
44
45 func (r *repository) commit(ctx context.Context, ref string) (string, error) {
46 return r.git(ctx, "rev-parse", "--verify", ref+"^{commit}")
47 }
48
49 // baseCommit resolves the commit to compare with.
50 func (r *repository) baseCommit(ctx context.Context, ref string) (string, error) {
51 if ref != "" {
52 return r.commit(ctx, ref)
53 }
54 for _, candidate := range defaultBaseRefs {
55 if commit, err := r.commit(ctx, candidate); err == nil {
56 return commit, nil
57 }
58 }
59 return "", fmt.Errorf("failed to find the base branch: tried %s", strings.Join(defaultBaseRefs, ", "))
60 }
61
62 // dirty reports whether the working tree has changes which are not committed yet.
63 func (r *repository) dirty(ctx context.Context) (bool, error) {
64 out, err := r.git(ctx, "status", "--porcelain")
65 if err != nil {
66 return false, err
67 }
68 return out != "", nil
69 }
70
71 // defaultCacheDir is located under the common git directory
72 // so that it is shared by all worktrees and never gets committed.
73 func (r *repository) defaultCacheDir(ctx context.Context) (string, error) {
74 commonDir, err := r.git(ctx, "rev-parse", "--git-common-dir")
75 if err != nil {
76 return "", err
77 }
78 if !filepath.IsAbs(commonDir) {
79 commonDir = filepath.Join(r.root, commonDir)
80 }
81 return filepath.Join(commonDir, "benchcheck"), nil
82 }
83
84 // checkout creates a temporary worktree of the commit at dir and returns the function to remove it.
85 func (r *repository) checkout(ctx context.Context, dir, commit string) (func(), error) {
86 if _, err := r.git(ctx, "worktree", "add", "--detach", dir, commit); err != nil {
87 return nil, err
88 }
89 cleanup := func() {
90 // ctx may already be canceled by a signal, but the worktree must be removed anyway.
91 if _, err := r.git(context.WithoutCancel(ctx), "worktree", "remove", "--force", dir); err != nil {
92 fmt.Fprintln(os.Stderr, err)
93 }
94 }
95 return cleanup, nil
96 }