BlueGit

timfa bluegit

Files

bluegit / internal / gitrepo / branch.go

branch.go

fix: toon mergeblokkades en markeer een al gepushte merge
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
81edde89 History
branch.go 3.1 KiB Download Raw
1package gitrepo
2
3import (
4 "context"
5 "strconv"
6 "strings"
7)
8
9// BranchTip is de punt van een lokale branch.
10type BranchTip struct {
11 Name, SHA, Subject, Date string
12}
13
14// BranchTips geeft elke lokale branch met zijn laatste commit.
15func BranchTips(ctx context.Context, gitBin, dir string) ([]BranchTip, error) {
16 const format = "%(refname:short)%00%(objectname)%00%(subject)%00%(creatordate:unix)"
17 out, err := run(ctx, gitBin, nil, "-C", dir, "for-each-ref", "--format="+format, "refs/heads")
18 if err != nil {
19 return nil, err
20 }
21 var tips []BranchTip
22 for _, line := range strings.Split(strings.TrimRight(out, "\n"), "\n") {
23 if line == "" {
24 continue
25 }
26 parts := strings.Split(line, "\x00")
27 if len(parts) < 4 || parts[0] == "" {
28 continue
29 }
30 tips = append(tips, BranchTip{Name: parts[0], SHA: parts[1], Subject: parts[2], Date: parts[3]})
31 }
32 return tips, nil
33}
34
35// AheadBehind telt commits die branch voor en achter base heeft.
36// Links in de git-telling is achter, rechts is voor.
37func AheadBehind(ctx context.Context, gitBin, dir, base, branch string) (ahead, behind int, err error) {
38 out, err := run(ctx, gitBin, nil, "-C", dir, "rev-list", "--left-right", "--count", base+"..."+branch)
39 if err != nil {
40 return 0, 0, err
41 }
42 fields := strings.Fields(out)
43 if len(fields) != 2 {
44 return 0, 0, nil
45 }
46 behind, _ = strconv.Atoi(fields[0])
47 ahead, _ = strconv.Atoi(fields[1])
48 return ahead, behind, nil
49}
50
51// CreateBranch maakt name op rev. rev is een branch, tag of commit.
52func CreateBranch(ctx context.Context, gitBin, dir, name, rev string) error {
53 sha, err := run(ctx, gitBin, nil, "-C", dir, "rev-parse", "--verify", "--end-of-options", rev+"^{commit}")
54 if err != nil {
55 return err
56 }
57 _, err = run(ctx, gitBin, nil, "-C", dir, "branch", "--", name, strings.TrimSpace(sha))
58 return err
59}
60
61// DeleteBranch verwijdert een lokale branch.
62func DeleteBranch(ctx context.Context, gitBin, dir, name string) error {
63 _, err := run(ctx, gitBin, nil, "-C", dir, "branch", "-D", "--", name)
64 return err
65}
66
67// RenameBranch hernoemt een lokale branch.
68func RenameBranch(ctx context.Context, gitBin, dir, oldName, newName string) error {
69 _, err := run(ctx, gitBin, nil, "-C", dir, "branch", "-m", "--", oldName, newName)
70 return err
71}
72
73// ComparePatch is de three-dot diff van from...to, dezelfde vorm als een commit.
74func ComparePatch(ctx context.Context, gitBin, dir, from, to string) ([]PatchFile, error) {
75 out, err := runAllow(ctx, gitBin, nil, 1, "-c", "core.quotepath=false", "-C", dir, "diff", "--no-color", "--find-renames", "--unified=3", from+"..."+to)
76 if err != nil {
77 return nil, err
78 }
79 return ParsePatch(out), nil
80}
81
82// Subjects geeft de onderwerpen van rev, nieuwste eerst.
83func Subjects(ctx context.Context, gitBin, dir, rev string, limit int) ([]string, error) {
84 if limit < 1 {
85 limit = 1
86 }
87 out, err := run(ctx, gitBin, nil, "-C", dir, "log", "--format=%s", "-n", strconv.Itoa(limit), rev)
88 if err != nil {
89 return nil, err
90 }
91 var subjects []string
92 for _, line := range strings.Split(strings.TrimRight(out, "\n"), "\n") {
93 if line != "" {
94 subjects = append(subjects, line)
95 }
96 }
97 return subjects, nil
98}