-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathghq.go
158 lines (130 loc) · 3.19 KB
/
ghq.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
package main
import (
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
)
// Repository represents git repository
type Repository struct {
Type string
Path string
ModTime time.Time
}
// Repositories contains array of Repository
type Repositories []Repository
// Len return number of repositories
func (r Repositories) Len() int {
return len(r)
}
// Swap repository
func (r Repositories) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
// RepositoriesByModTime is wrapper of sort algorithm for order by mod time
type RepositoriesByModTime struct {
Repositories
}
// Less sort array by mod time
func (bmt RepositoriesByModTime) Less(i, j int) bool {
return bmt.Repositories[i].ModTime.Before(bmt.Repositories[j].ModTime)
}
func verifyGhqPath() string {
ghqPath, err := getGhqPath()
if err != nil {
fmt.Println("You must setup ghq first")
os.Exit(1)
}
return ghqPath
}
func getGhqPath() (string, error) {
out, err := exec.Command("ghq", "root").Output()
if err != nil {
return "", err
}
return string(out)[:len(out)-1], nil
}
func searchForRepos(rootPath string) <-chan Repository {
repos := make(chan Repository)
go func() {
filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
// skip file
if !info.IsDir() {
return nil
}
// skip directories which is not a repository
if _, err := os.Stat(filepath.Join(path, ".git")); err != nil {
return nil
}
repository := Repository{
Type: "git",
Path: path,
ModTime: info.ModTime(),
}
repos <- repository
return filepath.SkipDir
})
close(repos)
}()
return repos
}
var hasSchemePattern = regexp.MustCompile("^[^:]+://")
var scpLikeURLPattern = regexp.MustCompile("^([^@]+@)?([^:]+):/?(.+)$")
func formatURL(ref string) (*url.URL, error) {
if !hasSchemePattern.MatchString(ref) && scpLikeURLPattern.MatchString(ref) {
matched := scpLikeURLPattern.FindStringSubmatch(ref)
user := matched[1]
host := matched[2]
path := matched[3]
ref = fmt.Sprintf("ssh://%s%s/%s", user, host, path)
}
url, err := url.Parse(ref)
if err != nil {
return url, err
}
if !url.IsAbs() {
if !strings.Contains(url.Path, "/") {
url.Path = url.Path + "/" + url.Path
}
url.Scheme = "https"
url.Host = "github.com"
if url.Path[0] != '/' {
url.Path = "/" + url.Path
}
}
return url, nil
}
func compileTargetPathFromURL(query string) string {
source, _ := formatURL(query)
encodedPath := strings.TrimSuffix(source.Path, ".git")
ghqPath := filepath.Join(source.Host, encodedPath)
return ghqPath
}
func compileTargetPath(query string) string {
ghqPath, err := getGhqPath()
if err != nil {
fmt.Println("You must setup 'ghq' command")
os.Exit(1)
}
re, _ := regexp.Compile("^(?:(?:(.+?)/)?(.+?)/)?(.+)$")
res := re.FindStringSubmatch(query)
targetHost := res[1]
targetUser := res[2]
targetPath := res[3]
if res[1] == "" {
targetHost = "github.com"
}
if res[2] == "" {
targetUser, err = GitConfigGet("global", "github.user")
if err != nil {
fmt.Println("You must set github.user first")
fmt.Println("> git config --global github.user <name>")
os.Exit(1)
}
}
return filepath.Join(ghqPath, targetHost, targetUser, targetPath)
}