-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-remote.go
51 lines (41 loc) · 1.32 KB
/
git-remote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package git
import (
"github.com/d3code/pkg/shell"
"github.com/d3code/pkg/xerr"
"os/exec"
"strings"
)
func Remote(path string) (string, error) {
command := exec.Command("git", "-C", shell.FullPath(path), "remote", "get-url", "origin")
output, err := command.Output()
if exitErr, ok := err.(*exec.ExitError); ok &&
strings.Contains(string(exitErr.Stderr), "No such remote") {
return "", nil
} else if err != nil {
return "", err
}
remote := string(output)
return strings.TrimSuffix(remote, "\n"), nil
}
func SetRemote(path string, repo string) {
url := FormatRepositoryUrl(repo)
shell.RunCmd(path, false, "git", "remote", "set-url", "origin", url)
}
func RemoteBehind(path string) bool {
command := exec.Command("git", "-C", shell.FullPath(path), "status")
output, err := command.Output()
xerr.ExitIfError(err)
if strings.Contains(string(output), "branch is ahead") {
return true
}
return false
}
func RemoteAhead(path string) bool {
command := exec.Command("git", "-C", shell.FullPath(path), "status")
output, err := command.Output()
xerr.ExitIfError(err)
if strings.Contains(string(output), "branch is behind") || strings.Contains(string(output), "use \"git pull\"") {
return true
}
return false
}