-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbranch.go
113 lines (91 loc) · 2.91 KB
/
branch.go
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"sort"
"strings"
"time"
)
type Branch struct {
branch string
hash string
}
type BranchAuthor struct {
author string
branch string
date string
}
func GetBranches(branchToCheck string) (branchesResult []Branch) {
//Getting remote branch list...
var output, _ = RunShellCommand("git", []string{"ls-remote"})
var branches []string = Filter(strings.Split(output, "\n"), func(s string) bool {
return len(s) > 0 &&
!strings.Contains(s, "HEAD") &&
!strings.Contains(s, "refs/tags/") &&
!strings.Contains(s, "refs/heads/"+branchToCheck) &&
!strings.Contains(s, "From ")
})
for i := range branches {
var parsed []string = strings.Split(branches[i], "\trefs/heads/")
if len(parsed) > 1 {
branchesResult = append(branchesResult, Branch{
hash: parsed[0],
branch: parsed[1],
})
}
}
return
}
func GetBranchAndAuthors(user string) (branchAuthors []BranchAuthor) {
const divider string = "--X--X--"
// Getting branch and author info...
output, _ := RunShellCommand("git", []string{
"for-each-ref",
fmt.Sprintf("--format=%%(authorname)%s%%(refname:strip=3)%s%%(committerdate)", divider, divider),
"--sort=committerdate",
"refs/remotes",
})
var branchAuthorList []string = Filter(strings.Split(output, "\n"), func(s string) bool {
return len(s) > 0
})
for _, branchAuthor := range branchAuthorList {
var row []string = strings.Split(branchAuthor, divider)
if user == "" || strings.Contains(strings.ToLower(row[0]), strings.ToLower(user)) {
t, _ := time.Parse("Mon Jan 2 15:04:05 2006 -0700", row[2])
branchAuthors = append(branchAuthors, BranchAuthor{
author: row[0],
branch: row[1],
date: t.Format("2006-01-02 15:04:05"),
})
}
}
return
}
func GetMergedBranches(user string, branchToCheck string) (branchAuthorResult []BranchAuthor) {
SpinnerUpdate("Getting branch and author info...")
var branchAuthors []BranchAuthor = GetBranchAndAuthors(user)
SpinnerUpdate("Getting remote branch list...")
var branches = GetBranches(branchToCheck)
SpinnerUpdate("Checking branches and commits...")
for _, branch := range branches {
var output, _ = RunShellCommand("git", []string{"branch", "-r", "--contains", branch.hash})
var branchExistsInMergedList []string = Filter(strings.Split(output, "\n"), func(s string) bool {
return len(s) > 0 && !strings.Contains(s, "HEAD")
})
branchExistsInMergedList = Map(branchExistsInMergedList, func(s string) string {
return strings.TrimSpace(s)
})
if len(branchExistsInMergedList) > 0 && Contains(branchExistsInMergedList, "origin/"+branchToCheck) {
for _, branchAuthor := range branchAuthors {
if branchAuthor.branch == branch.branch {
branchAuthorResult = append(branchAuthorResult, branchAuthor)
break
}
}
}
}
// sort list by date
sort.Slice(branchAuthorResult, func(i, j int) bool {
return branchAuthorResult[i].date > branchAuthorResult[j].date
})
return
}