-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitPullLoop.go
More file actions
79 lines (73 loc) · 1.57 KB
/
gitPullLoop.go
File metadata and controls
79 lines (73 loc) · 1.57 KB
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
package main
import (
"fmt"
"os"
"io/ioutil"
"os/exec"
"sync"
)
func main() {
wd, _ := os.Getwd()
dirs, _ := ioutil.ReadDir(wd)
didAnything := false
wg := new(sync.WaitGroup)
wg.Add(len(dirs))
for _, dir := range dirs {
if dir.IsDir() {
if isRepo(dir.Name()) {
func(dirName string, wg *sync.WaitGroup) {
gitPull(dirName)
defer func() {
didAnything = true
wg.Done()
}()
}(dir.Name(), wg)
} else {
wg.Done()
}
} else {
wg.Done()
}
}
wg.Wait()
if !didAnything {
fmt.Println("No git repositories found")
}
}
// Perform a git pull in the current working directory
// (assumes that git is on your $PATH)
func gitPull(dir string) {
cmd := exec.Command("git", "-C", dir, "pull")
fmt.Println("Executing ", cmd.Args, "...")
err := cmd.Run()
if nil != err {
fmt.Println("Failed to pull from ", dir)
return
}
fmt.Println("Finished pull from ", dir)
}
// Given a relative path as a string, determine if the directory contains
// a .git directory
func isRepo(dir string) bool {
files, _ := ioutil.ReadDir(dir)
for _, file := range files {
if file.Name() == ".git" {
return true
}
}
return false
}
// Starting from a string directory, recurse to find all directories
// containing .git directories (i.e., find all git repos)
// func traverseAndDo(dir string) []string {
// files, _ := ioutil.ReadDir(dir)
// var subdirs []string
// for _, file := range files {
// if file.IsDir && !isRepo(file) {
// append(subdirs, file.Name())
// }
// }
// for _, subdir := range subdirs {
// traverse(subdir)
// }
// }