-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (113 loc) · 3.06 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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"text/tabwriter"
"github.com/charmbracelet/log"
)
var skipFolders = []string{
"node_modules",
}
var gitReposChan = make(chan string, 10)
type results struct {
repo string
branches map[string]string
changes int
}
var allResults = []results{}
func walkDirectory(dir string) error {
defer close(gitReposChan)
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
for _, skip := range skipFolders {
if info.Name() == skip {
return filepath.SkipDir
}
}
if info.Name() == ".git" {
repoFolder := filepath.Dir(path)
log.Debug("Found git repository:", repoFolder)
gitReposChan <- repoFolder
return filepath.SkipDir
}
}
return nil
})
}
func handleGitRepos(wg *sync.WaitGroup) {
defer wg.Done()
for repoFolder := range gitReposChan {
log.Debug("Processing git repository:", repoFolder)
repo := &GitRepo{folder: repoFolder}
results := results{
repo: repoFolder,
branches: map[string]string{},
changes: 0,
}
branches, err := repo.getUnpushedBranches()
if err != nil {
log.Error("Error checking git status:", err)
continue
} else {
results.branches = branches
}
changes, err := repo.getUnpushedChanges()
if err != nil {
log.Error("Error checking git status:", err)
continue
} else {
results.changes = changes
}
allResults = append(allResults, results)
}
}
func printLookingForContributors() {
log.Info("--------------------------------------------")
log.Info("This project was developed in a short time, before I returned my laptop to the IT department. I'm looking for contributors to help me improve it.")
log.Info("Any help is welcome, be it a suggestion, a bug report, a pull request...")
log.Info("https://github.com/baruchiro/gh-local-changes")
log.Info("--------------------------------------------")
}
func main() {
defer printLookingForContributors()
dir := "."
if len(os.Args) > 1 {
dir = os.Args[1]
if dir == "-h" || dir == "--help" {
log.Info("Usage: go-gh [dir]")
os.Exit(0)
}
// Fail if the directory does not exist
if _, err := os.Stat(dir); os.IsNotExist(err) {
log.Fatal("Directory does not exist:", dir)
}
}
wg := &sync.WaitGroup{}
wg.Add(1)
go handleGitRepos(wg)
if err := walkDirectory(dir); err != nil {
log.Fatal("Error walking directory:", err)
os.Exit(1)
}
wg.Wait()
// Print results in a table
log.Info("Results:")
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.Debug)
fmt.Fprintln(w, "Repo\tBranches\tChanges")
for _, r := range allResults {
if len(r.branches) > 0 || r.changes > 0 {
fmt.Fprintf(w, "%s\t%v\t%d\n", r.repo, len(r.branches), r.changes)
}
}
w.Flush()
log.Info("To see the unpushed branches, run: git " + strings.Join(unpushedBranchesCommand, " "))
log.Info("To see the unpushed changes, run: git " + strings.Join(unpushedChangesCommand, " "))
}
// For more examples of using go-gh, see:
// https://github.com/cli/go-gh/blob/trunk/example_gh_test.go