Skip to content

Commit

Permalink
Merge pull request #7 from sinshutu/master
Browse files Browse the repository at this point in the history
Add command fetch
  • Loading branch information
uetchy committed Aug 27, 2017
2 parents 8b37ca3 + 959e238 commit 712774a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ $ gst update
Already up-to-date.
```

### fetch

`git fetch --tags --prune` to all repositories.

```
$ gst fetch
/Users/uetchy/Repos/src/github.com/uetchy/gst
* [new branch] dev -> origin/dev
- [deleted] (none) -> origin/test
* [new tag] v1.0.0 -> v1.0.0
```

## Quick Install

See [releases](https://github.com/uetchy/gst/releases/latest).
Expand Down Expand Up @@ -135,4 +147,4 @@ chmod +x /usr/local/bin/gst

```
go get github.com/uetchy/gst
```
```
39 changes: 39 additions & 0 deletions command_fetch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"github.com/codegangsta/cli"
"github.com/daviddengcn/go-colortext"
)

var flagsOfFetch = []cli.Flag{
cli.BoolFlag{
Name: "short, s",
Usage: "shorten result for pipeline processing",
},
}

var commandFetch = cli.Command{
Name: "fetch",
Action: doFetch,
Flags: flagsOfFetch,
}

func doFetch(c *cli.Context) error {
ghqPath := verifyGhqPath()
repos := searchForRepos(ghqPath)

// Listing repos
for repo := range repos {
printlnWithColor(repo.Path, ct.Cyan)
out, err := GitFetch(repo.Path)
if err != nil {
fmt.Println(err)
continue
}
if out != "" {
fmt.Println(err)
}
}
return nil
}
23 changes: 23 additions & 0 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,26 @@ func GitPull(targetPath string) (string, error) {

return string(out), nil
}

// GitFetch update tags and remove old branches
func GitFetch(targetPath string) (string, error) {
if err := os.Chdir(targetPath); err != nil {
return "", err
}

out, err := exec.Command("git", "fetch", "--tags", "--prune").CombinedOutput()
if err != nil {
eout := string(out)
if strings.HasPrefix(eout, "conq: repository does not exist.") {
return "", &RepositoryNotFoundError{targetPath}
} else if strings.HasPrefix(eout, "ERROR: Repository not found.") {
return "", &RepositoryNotFoundError{targetPath}
} else if strings.HasPrefix(eout, "fatal: No remote repository specified.") {
return "", &NoRemoteSpecifiedError{targetPath}
} else {
return "", err
}
}

return string(out), nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var Commands = []cli.Command{
commandRemove,
commandDoctor,
commandUpdate,
commandFetch,
}

func main() {
Expand Down

0 comments on commit 712774a

Please sign in to comment.