Skip to content

Commit 712774a

Browse files
authored
Merge pull request #7 from sinshutu/master
Add command fetch
2 parents 8b37ca3 + 959e238 commit 712774a

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ $ gst update
9797
Already up-to-date.
9898
```
9999

100+
### fetch
101+
102+
`git fetch --tags --prune` to all repositories.
103+
104+
```
105+
$ gst fetch
106+
/Users/uetchy/Repos/src/github.com/uetchy/gst
107+
* [new branch] dev -> origin/dev
108+
- [deleted] (none) -> origin/test
109+
* [new tag] v1.0.0 -> v1.0.0
110+
```
111+
100112
## Quick Install
101113

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

136148
```
137149
go get github.com/uetchy/gst
138-
```
150+
```

command_fetch.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/codegangsta/cli"
6+
"github.com/daviddengcn/go-colortext"
7+
)
8+
9+
var flagsOfFetch = []cli.Flag{
10+
cli.BoolFlag{
11+
Name: "short, s",
12+
Usage: "shorten result for pipeline processing",
13+
},
14+
}
15+
16+
var commandFetch = cli.Command{
17+
Name: "fetch",
18+
Action: doFetch,
19+
Flags: flagsOfFetch,
20+
}
21+
22+
func doFetch(c *cli.Context) error {
23+
ghqPath := verifyGhqPath()
24+
repos := searchForRepos(ghqPath)
25+
26+
// Listing repos
27+
for repo := range repos {
28+
printlnWithColor(repo.Path, ct.Cyan)
29+
out, err := GitFetch(repo.Path)
30+
if err != nil {
31+
fmt.Println(err)
32+
continue
33+
}
34+
if out != "" {
35+
fmt.Println(err)
36+
}
37+
}
38+
return nil
39+
}

git.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,26 @@ func GitPull(targetPath string) (string, error) {
157157

158158
return string(out), nil
159159
}
160+
161+
// GitFetch update tags and remove old branches
162+
func GitFetch(targetPath string) (string, error) {
163+
if err := os.Chdir(targetPath); err != nil {
164+
return "", err
165+
}
166+
167+
out, err := exec.Command("git", "fetch", "--tags", "--prune").CombinedOutput()
168+
if err != nil {
169+
eout := string(out)
170+
if strings.HasPrefix(eout, "conq: repository does not exist.") {
171+
return "", &RepositoryNotFoundError{targetPath}
172+
} else if strings.HasPrefix(eout, "ERROR: Repository not found.") {
173+
return "", &RepositoryNotFoundError{targetPath}
174+
} else if strings.HasPrefix(eout, "fatal: No remote repository specified.") {
175+
return "", &NoRemoteSpecifiedError{targetPath}
176+
} else {
177+
return "", err
178+
}
179+
}
180+
181+
return string(out), nil
182+
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var Commands = []cli.Command{
1515
commandRemove,
1616
commandDoctor,
1717
commandUpdate,
18+
commandFetch,
1819
}
1920

2021
func main() {

0 commit comments

Comments
 (0)