generated from fallion/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommits_between.go
More file actions
68 lines (60 loc) · 1.49 KB
/
commits_between.go
File metadata and controls
68 lines (60 loc) · 1.49 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
package git
import (
"strings"
)
// CommitsBetween returns a slice of commit hashes between two commits
func (g *Git) CommitsBetween(from Hash, to Hash) ([]Hash, error) {
// If from and to are equal, return empty slice
if from == to {
return []Hash{}, nil
}
fromStr := from.String()
toStr := to.String()
// Check if 'to' is an empty hash (all zeros)
var emptyHash Hash
if to == emptyHash {
// If 'to' is empty, return all commits from 'from'
output, err := g.runGitCommand("log", "--format=%H", fromStr)
if err != nil {
return nil, err
}
lines := strings.Split(output, "\n")
var commits []Hash
for _, line := range lines {
if line == "" {
continue
}
hash, err := NewHash(line)
if err != nil {
continue
}
commits = append(commits, hash)
}
return commits, nil
}
// Get commits from 'from' to 'to' (excluding 'to')
// Use ^to to exclude the 'to' commit
output, err := g.runGitCommand("log", "--format=%H", fromStr, "^"+toStr)
if err != nil {
// If the command fails, it might be because there are no commits between
// Try to check if 'to' is reachable from 'from'
_, err2 := g.runGitCommand("merge-base", "--is-ancestor", toStr, fromStr)
if err2 != nil {
return []Hash{}, nil
}
return nil, err
}
lines := strings.Split(output, "\n")
var commits []Hash
for _, line := range lines {
if line == "" {
continue
}
hash, err := NewHash(line)
if err != nil {
continue
}
commits = append(commits, hash)
}
return commits, nil
}