This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
/
main.go
180 lines (153 loc) · 3.89 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"github.com/jedib0t/go-pretty/table"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
func main() {
dirPath := os.Args[1]
files, err := ioutil.ReadDir(dirPath)
if err != nil {
log.Fatal(err)
}
var rows []table.Row
for _, f := range files {
if f.IsDir() {
absPath := path.Join(dirPath, f.Name())
branch, tag, err1 := GetCurrentBranchAndTagFromPath(absPath)
remoteNames, err2 := GetRemotes(absPath)
head, err3 := GetCurrentCommitFromPath(absPath)
if err1 == nil && err2 == nil && err3 == nil {
if len(remoteNames) == 0 {
rows = append(rows, table.Row{f.Name(), head[:7], branch, tag})
continue
} else {
rows = append(rows, table.Row{f.Name(), head[:7], branch, tag, remoteNames[0]})
}
for i, remoteName := range remoteNames {
if i == 0 {
continue
}
rows = append(rows, table.Row{"", "", "", remoteName})
}
}
}
}
// Print the branch names, tags and remotes in a table
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Repository", "Head", "Branch", "Tag", "Remotes"})
t.AppendRows(rows)
t.Render()
}
func Info(format string, args ...interface{}) {
fmt.Printf("\x1b[34;1m%s\x1b[0m\n", fmt.Sprintf(format, args...))
}
func CheckIfError(err error) {
if err == nil {
return
}
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
os.Exit(1)
}
func GetRemotes(path string) ([]string, error) {
r, err := git.PlainOpen(path)
if err != nil {
return nil, err
}
remotes, err := r.Remotes()
remoteNames := make([]string, len(remotes))
for i, remote := range remotes {
remoteNames[i] = remote.String()
// Info(remote.String())
}
return remoteNames, nil
}
func GetCurrentBranchAndTagFromPath(path string) (string, string, error) {
r, err := git.PlainOpen(path)
if err != nil {
return "", "", err
}
return GetCurrentBranchAndTag(r)
}
func GetCurrentBranchAndTag(repository *git.Repository) (string, string, error) {
branchRefs, err := repository.Branches()
if err != nil {
return "", "", err
}
headRef, err := repository.Head()
if err != nil {
return "", "", err
}
var currentBranchName string
branchRefs.ForEach(func(branchRef *plumbing.Reference) error {
if branchRef.Hash() == headRef.Hash() {
currentBranchName = branchRef.Name().Short()
return nil
}
return nil
})
var currentTagName string
tagRefs, err := repository.Tags()
tagRefs.ForEach(func(tagRef *plumbing.Reference) error {
if tagRef.Hash() == headRef.Hash() {
currentTagName = tagRef.Name().Short()
return nil
}
return nil
})
return currentBranchName, currentTagName, nil
}
func GetCurrentCommitFromPath(path string) (string, error) {
r, err := git.PlainOpen(path)
if err != nil {
return "", err
}
return GetCurrentCommit(r)
}
func GetCurrentCommit(repository *git.Repository) (string, error) {
headRef, err := repository.Head()
if err != nil {
return "", err
}
headSha := headRef.Hash().String()
return headSha, nil
}
func GetLatestTagFromRepository(repository *git.Repository) (string, error) {
tagRefs, err := repository.Tags()
if err != nil {
return "", err
}
var latestTagCommit *object.Commit
var latestTagName string
err = tagRefs.ForEach(func(tagRef *plumbing.Reference) error {
revision := plumbing.Revision(tagRef.Name().String())
tagCommitHash, err := repository.ResolveRevision(revision)
if err != nil {
return err
}
commit, err := repository.CommitObject(*tagCommitHash)
if err != nil {
return err
}
if latestTagCommit == nil {
latestTagCommit = commit
latestTagName = tagRef.Name().String()
}
if commit.Committer.When.After(latestTagCommit.Committer.When) {
latestTagCommit = commit
latestTagName = tagRef.Name().String()
}
return nil
})
if err != nil {
return "", err
}
return latestTagName, nil
}