-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
99 lines (92 loc) · 2.71 KB
/
Copy pathgit.go
File metadata and controls
99 lines (92 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
func git(root string, args ...string) (string, error) {
out, err := exec.Command("git", append([]string{"-C", root}, args...)...).Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
return "", fmt.Errorf("git %s: %s", strings.Join(args, " "), strings.TrimSpace(string(ee.Stderr)))
}
return "", fmt.Errorf("git %s: %w", strings.Join(args, " "), err)
}
return strings.TrimSpace(string(out)), nil
}
// repoRoot returns the git top-level directory containing dir, or dir itself
// when it is not inside a repository.
func repoRoot(dir string) string {
if top, err := git(dir, "rev-parse", "--show-toplevel"); err == nil {
return top
}
abs, err := filepath.Abs(dir)
if err != nil {
return dir
}
return abs
}
// Scope describes what the rules should look at.
type Scope struct {
Full bool
Base, Head string
Files []string // "M\tpath" lines from git diff --name-status
}
func (s Scope) String() string {
if s.Full {
return "whole repository"
}
return fmt.Sprintf("diff %s...%s (%s)", s.Base, s.Head, plural(len(s.Files), "file"))
}
// buildScope resolves the diff range. With an empty base it tries, in order:
// $AGENTCI_BASE, origin/$GITHUB_BASE_REF, origin/HEAD, origin/main,
// origin/master, main, master (first one whose merge-base is not HEAD itself),
// and finally HEAD~1.
func buildScope(root, base, head string, full bool) (Scope, error) {
if full {
return Scope{Full: true}, nil
}
if _, err := git(root, "rev-parse", "--verify", head); err != nil {
return Scope{}, fmt.Errorf("head ref: %w", err)
}
if base == "" {
base = detectBase(root, head)
if base == "" {
return Scope{}, fmt.Errorf("could not detect a base ref to diff against; pass -base <ref> or -full")
}
}
if _, err := git(root, "rev-parse", "--verify", base); err != nil {
return Scope{}, fmt.Errorf("base ref: %w", err)
}
out, err := git(root, "diff", "--name-status", base+"..."+head)
if err != nil {
return Scope{}, err
}
s := Scope{Base: base, Head: head}
if out != "" {
s.Files = strings.Split(out, "\n")
}
return s, nil
}
func detectBase(root, head string) string {
if b := os.Getenv("AGENTCI_BASE"); b != "" {
return b
}
var cands []string
if ref := os.Getenv("GITHUB_BASE_REF"); ref != "" {
cands = append(cands, "origin/"+ref)
}
cands = append(cands, "origin/HEAD", "origin/main", "origin/master", "main", "master")
headSHA, _ := git(root, "rev-parse", head)
for _, c := range cands {
if mb, err := git(root, "merge-base", c, head); err == nil && mb != headSHA {
return c
}
}
if _, err := git(root, "rev-parse", "--verify", head+"~1"); err == nil {
return head + "~1"
}
return ""
}