Skip to content

Instantly share code, notes, and snippets.

@alex3305
Created November 20, 2015 08:45
Show Gist options
  • Save alex3305/2f7f9b882611e56d1dd4 to your computer and use it in GitHub Desktop.
Save alex3305/2f7f9b882611e56d1dd4 to your computer and use it in GitHub Desktop.
Git recursive pull
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