Created
November 20, 2015 08:45
-
-
Save alex3305/2f7f9b882611e56d1dd4 to your computer and use it in GitHub Desktop.
Git recursive pull
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io/ioutil" | |
"os" | |
"log" | |
"os/exec" | |
"bytes" | |
) | |
func main() { | |
files, _ := ioutil.ReadDir("./") | |
for _, f := range files { | |
if f.IsDir() { | |
err := os.Chdir(f.Name()) | |
if err != nil { | |
log.Fatal("Could not change to directory ", f.Name()) | |
} | |
log.Println("Switching to ", f.Name()) | |
cmd := exec.Command("git", "pull", "origin", getCurrentBranch()) | |
output, err := cmd.CombinedOutput() | |
cmd.Run() | |
if output != nil { | |
log.Println(string(output)) | |
} | |
if err != nil { | |
log.Println("Could not pull into directory ", f.Name(), ": ", err) | |
} | |
os.Chdir("..") | |
} | |
} | |
} | |
func getCurrentBranch() string { | |
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") | |
output, err := cmd.CombinedOutput() | |
cmd.Run() | |
if err != nil { | |
log.Println("Could not retrieve current branch ", err) | |
} | |
return string(bytes.TrimSpace(output)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment