/
remote_url.go
80 lines (69 loc) · 1.73 KB
/
remote_url.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
package main
import (
"fmt"
"github.com/delphinus35/lycia/util"
"net/url"
"os/exec"
"regexp"
)
var remoteUrlPattern = regexp.MustCompile(`(?m)^origin\s+(.*)\s+\(fetch\)$`)
type remoteURL struct {
Ref string
*url.URL
}
func (r remoteURL) ToURL() *url.URL {
return &url.URL{
Scheme: r.Scheme,
Host: r.Host,
Path: r.Path,
}
}
func (r remoteURL) SourceURL(ref string, path string, from int, to int) (sourceURL string) {
r.Ref = ref
sourceURL = r.String()
if path != "" {
sourceURL = fmt.Sprintf("%s/blob/%s/%s", sourceURL, r.Ref, path)
if from != 0 {
sourceURL = fmt.Sprintf("%s#L%d", sourceURL, from)
if to != 0 {
sourceURL = fmt.Sprintf("%s-L%d", sourceURL, to)
}
}
} else if r.Ref != "master" {
sourceURL = fmt.Sprintf("%s/tree/%s", r.Path, r.Ref)
}
return
}
func (r remoteURL) IssueURL(num int) (issueURL string) {
if num == 0 {
issueURL = r.String() + "/issues"
} else if num > 0 {
issueURL = fmt.Sprintf("%s/issues/%d", r.String(), num)
} else {
issueURL = r.String()
}
return
}
func RemoteURL(dir string) (parsed *remoteURL, err error) {
cmd := exec.Command("git", "remote", "-v")
cmd.Dir = dir
out, cmdErr := cmd.Output()
outStr := string(out)
if cmdErr != nil {
msg := fmt.Sprintf("can not exec 'git remove -v' : %s", cmdErr)
err = util.LyciaError(msg)
} else if outStr == "" {
err = util.LyciaError("git remote is not defined")
} else if !remoteUrlPattern.MatchString(outStr) {
msg := fmt.Sprintf("unknown git remote string: %s", outStr)
err = util.LyciaError(msg)
} else {
rawUrl := remoteUrlPattern.FindStringSubmatch(outStr)[1]
gitUrl, _ := UrlMaker(rawUrl)
parsedURL, err := url.Parse(gitUrl.WebUrl)
if err == nil {
parsed = &remoteURL{"", parsedURL}
}
}
return
}