-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (70 loc) · 1.7 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
func main() {
path, _ := os.Getwd()
if len(os.Args) > 1 {
path = os.Args[1]
}
absPath, _ := filepath.Abs(path)
repos := findRepos(absPath)
if len(repos) == 0 {
fmt.Println("no git repositories found on", absPath)
return
}
fmt.Println("updating projects on", absPath)
for i, repo := range repos {
fmt.Printf("%d of %d %s\n", i+1, len(repos), repo)
out, err := exec.Command("git", "-C", repo, "fetch", "-p", "-a").CombinedOutput()
fmt.Printf("%v\n", string(out))
if err != nil {
fmt.Printf("failed to fetch: %v\n\n", err)
continue
}
out, err = exec.Command("git", "-C", repo, "pull").CombinedOutput()
fmt.Printf("%v\n", string(out))
if err != nil {
fmt.Printf("failed to pull: %v\n\n", err)
continue
}
out, err = exec.Command("git", "-C", repo, "submodule", "update", "--recursive").CombinedOutput()
fmt.Printf("%v\n", string(out))
if err != nil {
fmt.Printf("failed to update submodule: %v\n\n", err)
continue
}
}
}
func findRepos(path string) []string {
if isRepo(path) {
return []string{path}
}
repos := []string{}
return findReposRecursive(path, repos)
}
func findReposRecursive(path string, repos []string) []string {
dirs, _ := os.ReadDir(path)
for _, dir := range dirs {
if dir.IsDir() {
fullPath := filepath.Join(path, dir.Name())
if isRepo(fullPath) {
repos = append(repos, fullPath)
} else {
repos = findReposRecursive(fullPath, repos)
}
}
}
return repos
}
func isRepo(path string) bool {
return folderExists(filepath.Join(path, ".git"))
}
func folderExists(path string) bool {
_, err := os.Stat(path)
hasFolder := !os.IsNotExist(err)
return hasFolder
}