generated from fallion/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch_diff_commits.go
More file actions
39 lines (32 loc) · 1.13 KB
/
branch_diff_commits.go
File metadata and controls
39 lines (32 loc) · 1.13 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
package git
import (
"fmt"
"strings"
)
// BranchDiffCommits compares commits from 2 branches and returns of a diff of them.
// Uses git log with exclusion syntax for efficient comparison - finds commits in branchA that are not in branchB.
// This is more efficient than fetching all commits from both branches and comparing them.
func (g *Git) BranchDiffCommits(branchA string, branchB string) ([]Hash, error) {
// git log branchA ^branchB shows all commits reachable from branchA but not from branchB
// This is equivalent to: commits in branchA that are not in branchB
// The ^branchB syntax excludes all commits reachable from branchB
output, err := g.runGitCommand("log", "--format=%H", branchA, "^"+branchB)
if err != nil {
return nil, fmt.Errorf("failed comparing branches %v and %v: %v", branchA, branchB, err)
}
var diffCommits []Hash
lines := strings.Split(output, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
hash, err := NewHash(line)
if err != nil {
// Skip invalid hashes
continue
}
diffCommits = append(diffCommits, hash)
}
return diffCommits, nil
}