-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
196 lines (164 loc) · 4.43 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Package main provides a command line tool to check for git repositories in a directory tree
package main
import (
"flag"
"fmt"
"log"
"net"
"net/url"
"os"
"path/filepath"
"strings"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
gogitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
type repoStatus struct {
Path string
LocalChanges int
RemoteChanges int
}
var maxDepth int
var repos []repoStatus
var rootPath string // global rootPath variable
func getSignersFromAgent() ([]ssh.Signer, error) {
socket := os.Getenv("SSH_AUTH_SOCK")
conn, err := net.Dial("unix", socket)
if err != nil {
return nil, err
}
defer conn.Close()
agentClient := agent.NewClient(conn)
signers, err := agentClient.Signers()
if err != nil {
return nil, err
}
return signers, nil
}
func visitAndCheckGit(path string, depth int) {
if depth > maxDepth {
return
}
log.Printf("Visiting path: %s at depth: %d\n", path, depth)
// checking if current directory is a git repository
r, err := git.PlainOpen(path)
if err == nil {
log.Printf("Checking path: %s\n", path)
w, err := r.Worktree()
if err != nil {
log.Printf("Error getting worktree for path %s: %s\n", path, err)
return
}
status, err := w.Status()
if err != nil {
log.Printf("Error getting status for path %s: %s\n", path, err)
return
}
localChanges := countChanges(status)
signers, err := getSignersFromAgent()
if err != nil {
log.Printf("Error getting signers from agent: %v\n", err)
return
}
if len(signers) == 0 {
log.Printf("No SSH keys loaded in ssh-agent")
return
}
// get the remote origin URL to extract the username
remotes, err := r.Remotes()
if err != nil {
log.Printf("Error getting remotes for path %s: %s\n", path, err)
return
}
var username string
for _, remote := range remotes {
if remote.Config().Name == "origin" {
remoteURL := remote.Config().URLs[0]
if strings.HasPrefix(remoteURL, "git@") {
parts := strings.Split(remoteURL, "@")
if len(parts) > 1 {
username = parts[0]
}
} else {
u, err := url.Parse(remoteURL)
if err != nil {
log.Printf("Error parsing URL for path %s: %s\n", path, err)
return
}
username = u.User.Username()
}
}
}
// if no username found, fall back to "git"
if username == "" {
username = "git"
}
auth, err := gogitssh.NewSSHAgentAuth(username)
if err != nil {
log.Printf("Error creating auth object: %v\n", err)
return
}
auth.HostKeyCallback = ssh.InsecureIgnoreHostKey()
// fetch latest commits
err = r.Fetch(&git.FetchOptions{
RemoteName: "origin",
Progress: nil,
Auth: auth,
})
if err != nil && err != git.NoErrAlreadyUpToDate {
log.Printf("Error fetching from origin for path %s: %s\n", path, err)
return
}
// get HEAD reference
headRef, err := r.Head()
if err != nil {
log.Printf("Error getting HEAD for path %s: %s\n", path, err)
return
}
// get remote reference
remoteRef, err := r.Reference(plumbing.NewRemoteReferenceName("origin", headRef.Name().Short()), true)
if err != nil {
log.Printf("Error getting remote reference for path %s: %s\n", path, err)
return
}
remoteChanges, err := countRemoteChanges(r, headRef, remoteRef)
if err != nil {
log.Printf("Error counting remote changes for path %s: %s\n", path, err)
return
}
repos = append(repos, repoStatus{Path: path, LocalChanges: localChanges, RemoteChanges: remoteChanges})
// return here because we don't need to check subdirectories if this is a git repo
return
}
// getting subdirectories
files, err := os.ReadDir(path)
if err != nil {
log.Printf("Error reading directory %s: %s\n", path, err)
return
}
for _, f := range files {
if f.Type().IsDir() && f.Type()&os.ModeSymlink != os.ModeSymlink {
visitAndCheckGit(filepath.Join(path, f.Name()), depth+1)
}
}
}
func main() {
maxDepthPtr := flag.Int("depth", 4, "the maximum depth")
flag.Parse()
maxDepth = *maxDepthPtr
args := flag.Args()
if len(args) > 0 {
rootPath = args[0]
} else {
rootPath = "." // default to the current directory
}
visitAndCheckGit(rootPath, 0)
// Print each repository only once
for _, repo := range repos {
fmt.Printf("In Folder [%s]: git present\n", repo.Path)
fmt.Println("Local Changes:", repo.LocalChanges)
fmt.Println("Remote Changes:", repo.RemoteChanges)
}
}