]>
Commit | Line | Data |
---|---|---|
ff524faf JN |
1 | package utils |
2 | ||
3 | import ( | |
4 | "io/ioutil" | |
5 | "log" | |
6 | "os" | |
7 | "os/exec" | |
8 | "sync" | |
9 | ) | |
10 | ||
06073685 JN |
11 | // Equivalent to the following shell alias |
12 | // alias git-pull-all="ls -d */ | xargs -P12 -I{} git -C {} pull" | |
13 | ||
ff524faf JN |
14 | func GitPullAll() { |
15 | currentDirectory, err := os.Getwd() | |
16 | if err != nil { | |
17 | log.Fatal("Error when trying to get current directory!") | |
18 | } else { | |
19 | log.Println("Pulling all git repositories in the directory: ", currentDirectory, "...") | |
20 | ||
21 | files, err := ioutil.ReadDir("./") | |
22 | if err != nil { | |
23 | log.Fatal(err.Error()) | |
24 | } | |
25 | var wg sync.WaitGroup | |
26 | ||
27 | for _, f := range files { | |
28 | if f.IsDir() { | |
29 | wg.Add(1) | |
30 | go doGitPull(&wg, f) | |
31 | } | |
32 | } | |
33 | wg.Wait() | |
34 | } | |
35 | } | |
36 | ||
37 | func doGitPull(wg *sync.WaitGroup, dir os.FileInfo) { | |
38 | defer wg.Done() | |
39 | output, err := exec.Command("git", "-C", dir.Name(), "pull", "--rebase").Output() | |
40 | if err != nil { | |
41 | log.Println("Error pulling", dir.Name(), err.Error()) | |
42 | } | |
43 | log.Println("Pulling", dir.Name(), "\n", string(output)) | |
44 | } |