-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.go
184 lines (165 loc) · 4.79 KB
/
git.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package git
import (
"errors"
"fmt"
"log"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/github/hub/commands"
)
type GitFormat int
const (
SSHFormat GitFormat = iota
HTTPSFormat = iota
)
// Short ssh style doesn't allow a custom port
// http://stackoverflow.com/a/5738592/329700
var sshExp = regexp.MustCompile("^(?P<sshUser>[^@]+)@(?P<domain>[^:]+):(?P<pathRepo>.*)\\.git/?$")
// https://github.com/Shyp/shyp_api.git
var httpsExp = regexp.MustCompile("^https://(?P<domain>[^/:]+)(:(?P<port>[[0-9]+))?/(?P<pathRepo>.+?)(\\.git/?)?$")
// A remote URL. Easiest to describe with an example:
//
// [email protected]:Shyp/shyp_api.git
//
// Would be parsed as follows:
//
// Path = Shyp
// Host = github.com
// RepoName = shyp_api
// SSHUser = git
// URL = [email protected]:Shyp/shyp_api.git
// Format = SSHFormat
//
// Similarly:
//
// https://github.com/Shyp/shyp_api.git
//
// User = Shyp
// Host = github.com
// RepoName = shyp_api
// SSHUser = ""
// Format = HTTPSFormat
type RemoteURL struct {
Host string
Port int
Path string
RepoName string
Format GitFormat
// The full URL
URL string
// If the remote uses the SSH format, this is the name of the SSH user for
// the remote. Usually "git@"
SSHUser string
}
func getPathAndRepoName(pathAndRepo string) (string, string) {
paths := strings.Split(pathAndRepo, "/")
repoName := paths[len(paths)-1]
path := strings.Join(paths[:len(paths)-1], "/")
return path, repoName
}
// ParseRemoteURL takes a git remote URL and returns an object with its
// component parts, or an error if the remote cannot be parsed
func ParseRemoteURL(remoteURL string) (*RemoteURL, error) {
match := sshExp.FindStringSubmatch(remoteURL)
if len(match) > 0 {
path, repoName := getPathAndRepoName(match[3])
return &RemoteURL{
Path: path,
Host: match[2],
RepoName: repoName,
URL: match[0],
Port: 22,
Format: SSHFormat,
SSHUser: match[1],
}, nil
}
match = httpsExp.FindStringSubmatch(remoteURL)
if len(match) > 0 {
var port int
var err error
if len(match[3]) > 0 {
port, err = strconv.Atoi(match[3])
if err != nil {
log.Panicf("git: invalid port: %s", match[3])
}
} else {
port = 443
}
path, repoName := getPathAndRepoName(match[4])
return &RemoteURL{
Path: path,
Host: match[1],
RepoName: repoName,
URL: match[0],
Port: port,
Format: HTTPSFormat,
}, nil
}
return nil, fmt.Errorf("Could not parse %s as a git remote", remoteURL)
}
// RemoteURL returns a Remote object with information about the given Git
// remote.
func GetRemoteURL(remoteName string) (*RemoteURL, error) {
rawRemote, err := exec.Command("git", "config", "--get", fmt.Sprintf("remote.%s.url", remoteName)).Output()
if err != nil {
return nil, err
}
// git response includes a newline
remote := strings.TrimSpace(string(rawRemote))
return ParseRemoteURL(remote)
}
// CurrentBranch returns the name of the current Git branch. Returns an error
// if you are not on a branch, or if you are not in a git repository.
func CurrentBranch() (string, error) {
result, err := exec.Command("git", "symbolic-ref", "--short", "HEAD").Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(result)), nil
}
func getLastCommitMessage() (string, error) {
result, err := exec.Command("git", "log", "-1", "--pretty=%B").Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(result)), nil
}
// CreateAndOpenPullRequest creates a PR on Github and opens it up in your browser
// Returns an error if you are not on a branch, or if you are not in a git repository.
func CreateAndOpenPullRequest() error {
message, err := getLastCommitMessage()
if err != nil {
return err
}
cmd := commands.CmdRunner.Lookup("pull-request")
args := commands.NewArgs([]string{"pull-request", "-m", message, "-o"})
execError := commands.CmdRunner.Call(cmd, args)
return execError.Err
}
// Tip returns the SHA of the given Git branch. If the empty string is
// provided, defaults to HEAD on the current branch.
func Tip(branch string) (string, error) {
if branch == "" {
branch = "HEAD"
}
result, err := exec.Command("git", "rev-parse", "--short", branch).CombinedOutput()
if err != nil {
if strings.Contains(string(result), "Needed a single revision") {
return "", fmt.Errorf("git: Branch %s is unknown, can't get tip", branch)
}
return "", err
}
return strings.TrimSpace(string(result)), nil
}
// Root returns the root directory of the current Git repository, or an error
// if you are not in a git repository.
func Root() (string, error) {
result, err := exec.Command("git", "rev-parse", "--show-toplevel").CombinedOutput()
trimmed := strings.TrimSpace(string(result))
if err != nil {
return "", errors.New(trimmed)
}
return strings.TrimSpace(trimmed), nil
}