Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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