From 88dd24c2885af5f3aa2205a9c30d32d1bb2c65d9 Mon Sep 17 00:00:00 2001 From: Yasuaki Uechi Date: Fri, 25 Dec 2015 16:54:17 +0900 Subject: [PATCH] Add command 'update' --- README.md | 24 +++++++++++++++++------- command_doctor.go | 13 +++---------- command_list.go | 10 ++-------- command_update.go | 34 ++++++++++++++++++++++++++++++++++ ghq.go | 9 +++++++++ git.go | 16 ++++++++++++++++ main.go | 3 ++- 7 files changed, 83 insertions(+), 26 deletions(-) create mode 100644 command_update.go diff --git a/README.md b/README.md index 286cf04..9b77787 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ List all of repositories changed git status ```console ❯ gst -/Users/uetchy/repos/src/github.com/uetchy/cabret +/Users/uetchy/Repos/src/github.com/uetchy/cabret -- 4 hours ago A .eslintrc M .gitignore @@ -33,7 +33,7 @@ M index.html M index.js M package.json -/Users/uetchy/repos/src/github.com/uetchy/gst +/Users/uetchy/Repos/src/github.com/uetchy/gst -- 3 minutes ago A changelog.md R gst.go -> main.go @@ -43,8 +43,8 @@ with __--short__ option: ```console ❯ gst --short -/Users/uetchy/repos/src/github.com/uetchy/ferret -/Users/uetchy/repos/src/github.com/uetchy/gst +/Users/uetchy/Repos/src/github.com/uetchy/ferret +/Users/uetchy/Repos/src/github.com/uetchy/gst ``` You can also use `peco` for pipeline processing as: @@ -61,9 +61,9 @@ Before start using 'new' and 'rm' command, You __must__ set 'github.user' to .gi ```console ❯ gst new awesome-project -/Users/uetchy/repos/src/github.com/uetchy/awesome-project +/Users/uetchy/Repos/src/github.com/uetchy/awesome-project ❯ gst new epic-team/awesome-project -/Users/uetchy/repos/src/github.com/epic-team/awesome-project +/Users/uetchy/Repos/src/github.com/epic-team/awesome-project ``` with `cd`, You can jump to created project: @@ -86,7 +86,7 @@ Remove git repository. ```console ❯ gst rm horrible-project -Remove? /Users/uetchy/repos/src/github.com/uetchy/horrible-project +Remove? /Users/uetchy/Repos/src/github.com/uetchy/horrible-project ``` ### doctor @@ -99,3 +99,13 @@ Health-check for repositories. Expected: github.com/uetchy/google-cloud-vision-raspi-sample Actual: bitbucket.org/uetchy/scent ``` + +### update + +`git pull` for all repositories. + +```console +❯ gst update +/Users/uetchy/Repos/src/github.com/uetchy/gst +Already up-to-date. +``` diff --git a/command_doctor.go b/command_doctor.go index 46f8ac4..c60c727 100644 --- a/command_doctor.go +++ b/command_doctor.go @@ -5,7 +5,6 @@ import ( "github.com/codegangsta/cli" // "github.com/daviddengcn/go-colortext" // "github.com/dustin/go-humanize" - "os" "strings" ) @@ -22,13 +21,7 @@ var commandDoctor = cli.Command{ func doDoctor(c *cli.Context) { // fixupIssues := c.Bool("fixup") - - ghqPath, err := getGhqPath() - if err != nil { - fmt.Println("You must setup ghq first") - os.Exit(1) - } - + ghqPath := verifyGhqPath() reposChannel := searchForRepos(ghqPath) // Listing repos @@ -38,12 +31,12 @@ func doDoctor(c *cli.Context) { source := strings.TrimPrefix(repo.Path, ghqPath+"/") if remoteOriginURL == "" { - fmt.Println("["+source+"] 'remote.origin' doesn't exist:") + fmt.Println("[" + source + "] 'remote.origin' doesn't exist:") fmt.Println(" Expected:\t", source) fmt.Println(" Actual:\t (no remote)") fmt.Println() } else if target != source && !strings.Contains(source, "golang.org/x/") { - fmt.Println("["+source+"] 'remote.origin' has changed:") + fmt.Println("[" + source + "] 'remote.origin' has changed:") fmt.Println(" Expected:\t", target) fmt.Println(" Actual:\t", source) // if fixupIssues { diff --git a/command_list.go b/command_list.go index 438b4ad..c723684 100644 --- a/command_list.go +++ b/command_list.go @@ -5,7 +5,6 @@ import ( "github.com/codegangsta/cli" "github.com/daviddengcn/go-colortext" "github.com/dustin/go-humanize" - "os" "sort" ) @@ -23,16 +22,11 @@ var commandList = cli.Command{ } func doList(c *cli.Context) { - ghqPath, err := getGhqPath() - if err != nil { - fmt.Println("You must setup ghq first") - os.Exit(1) - } + ghqPath := verifyGhqPath() + reposChannel := searchForRepos(ghqPath) shortExpression := c.Bool("short") - reposChannel := searchForRepos(ghqPath) - // Sort by time repos := []Repository{} for repo := range reposChannel { diff --git a/command_update.go b/command_update.go new file mode 100644 index 0000000..2a7c0c2 --- /dev/null +++ b/command_update.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "github.com/codegangsta/cli" + "github.com/daviddengcn/go-colortext" +) + +var flagsOfUpdate = []cli.Flag{ + cli.BoolFlag{ + Name: "short, s", + Usage: "shorten result for pipeline processing", + }, +} + +var commandUpdate = cli.Command{ + Name: "update", + Action: doUpdate, + Flags: flagsOfUpdate, +} + +func doUpdate(c *cli.Context) { + ghqPath := verifyGhqPath() + repos := searchForRepos(ghqPath) + + // Listing repos + for repo := range repos { + printlnWithColor(repo.Path, ct.Cyan) + err := GitPull(repo.Path) + if err != nil { + fmt.Println(err) + } + } +} diff --git a/ghq.go b/ghq.go index 9e15885..d5bce7e 100644 --- a/ghq.go +++ b/ghq.go @@ -41,6 +41,15 @@ func (bmt RepositoriesByModTime) Less(i, j int) bool { return bmt.Repositories[i].ModTime.Before(bmt.Repositories[j].ModTime) } +func verifyGhqPath() string { + ghqPath, err := getGhqPath() + if err != nil { + fmt.Println("You must setup ghq first") + os.Exit(1) + } + return ghqPath +} + func getGhqPath() (string, error) { out, err := exec.Command("ghq", "root").Output() if err != nil { diff --git a/git.go b/git.go index a19de60..bcc8bda 100644 --- a/git.go +++ b/git.go @@ -2,6 +2,7 @@ package main import ( "errors" + "fmt" "github.com/motemen/go-gitconfig" "os" "os/exec" @@ -59,3 +60,18 @@ func GitStatus(targetPath string) ([]string, error) { return statuses, nil } + +// GitPull pulls remote branch +func GitPull(targetPath string) error { + if err := os.Chdir(targetPath); err != nil { + return err + } + + out, err := exec.Command("git", "pull").Output() + if err != nil { + return err + } + + fmt.Println(string(out)) + return nil +} diff --git a/main.go b/main.go index d25b45f..b75ae93 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,7 @@ import ( ) // Version of this program -var Version string = "HEAD" +var Version = "HEAD" // Commands are list of available commands var Commands = []cli.Command{ @@ -14,6 +14,7 @@ var Commands = []cli.Command{ commandNew, commandRemove, commandDoctor, + commandUpdate, } func main() {