Debian Codegraph @

lib/pama/revctrl/git.go

raw
1 package revctrl
2
3 import (
4 "bytes"
5 "fmt"
6 "os"
7 "os/exec"
8 "path/filepath"
9 "strings"
10
11 "git.sr.ht/~rjarry/aerc/lib/log"
12 "git.sr.ht/~rjarry/aerc/lib/pama/models"
13 )
14
15 func init() {
16 register("git", newGit)
17 }
18
19 func newGit(s string) models.RevisionController {
20 return &git{path: strings.TrimSpace(s)}
21 }
22
23 type git struct {
24 path string
25 }
26
27 func (g git) Support() bool {
28 _, exitcode, err := g.do("rev-parse")
29 return exitcode == 0 && err == nil
30 }
31
32 func (g git) Root() (string, error) {
33 s, _, err := g.do("rev-parse", "--show-toplevel")
34 return s, err
35 }
36
37 func (g git) Head() (string, error) {
38 s, _, err := g.do("rev-list", "-n 1", "HEAD")
39 return s, err
40 }
41
42 func (g git) History(commit string) ([]string, error) {
43 s, _, err := g.do("rev-list", "--reverse", fmt.Sprintf("%s..HEAD", commit))
44 return strings.Fields(s), err
45 }
46
47 func (g git) Subject(commit string) string {
48 s, exitcode, err := g.do("log", "-1", "--pretty=%s", commit)
49 if exitcode > 0 || err != nil {
50 return ""
51 }
52 return s
53 }
54
55 func (g git) Author(commit string) string {
56 s, exitcode, err := g.do("log", "-1", "--pretty=%an", commit)
57 if exitcode > 0 || err != nil {
58 return ""
59 }
60 return s
61 }
62
63 func (g git) Date(commit string) string {
64 s, exitcode, err := g.do("log", "-1", "--pretty=%as", commit)
65 if exitcode > 0 || err != nil {
66 return ""
67 }
68 return s
69 }
70
71 func (g git) Drop(commit string) error {
72 _, exitcode, err := g.do("rebase", "--onto", commit+"^", commit)
73 if exitcode > 0 {
74 return fmt.Errorf("failed to drop commit %s", commit)
75 }
76 return err
77 }
78
79 func (g git) Exists(commit string) bool {
80 _, exitcode, err := g.do("merge-base", "--is-ancestor", commit, "HEAD")
81 return exitcode == 0 && err == nil
82 }
83
84 func (g git) Clean() bool {
85 // is a rebase in progress?
86 dirs := []string{"rebase-merge", "rebase-apply"}
87 for _, dir := range dirs {
88 relPath, _, err := g.do("rev-parse", "--git-path", dir)
89 if err == nil {
90 if _, err := os.Stat(filepath.Join(g.path, relPath)); !os.IsNotExist(err) {
91 log.Errorf("%s exists: another rebase in progress..", dir)
92 return false
93 }
94 }
95 }
96 // are there unstaged changes?
97 s, exitcode, err := g.do("diff-index", "HEAD")
98 return len(s) == 0 && exitcode == 0 && err == nil
99 }
100
101 func (g git) CreateWorktree(target, commit string) error {
102 _, exitcode, err := g.do("worktree", "add", target, commit)
103 if exitcode > 0 {
104 return fmt.Errorf("failed to create worktree in %s: %w", target, err)
105 }
106 return err
107 }
108
109 func (g git) DeleteWorktree(target string) error {
110 _, exitcode, err := g.do("worktree", "remove", target)
111 if exitcode > 0 {
112 return fmt.Errorf("failed to delete worktree in %s: %w", target, err)
113 }
114 return err
115 }
116
117 func (g git) ApplyCmd() string {
118 // TODO: should we return a *exec.Cmd instead of a string?
119 return fmt.Sprintf("git -C %s am -3 --empty drop", g.path)
120 }
121
122 func (g git) do(args ...string) (string, int, error) {
123 proc := exec.Command("git", "-C", g.path)
124 proc.Args = append(proc.Args, args...)
125 proc.Env = os.Environ()
126 result, err := proc.Output()
127 return string(bytes.TrimSpace(result)), proc.ProcessState.ExitCode(), err
128 }