Kiln » gitkiln Read More
Clone URL:  
main.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package main import ( "fmt" "net/url" "os" "os/exec" "strings" "developers.kilnhg.com/Code/Kiln/Group/gitkiln/kiln" ) const documentation = ` USAGE: git-kiln [<command> [args]] git-kiln provides quick access to the Kiln repository associated with this repository. To work, git-kiln must be run from within a Git repository that either has a Kiln HTTP repository as the upstream remote, or has the config value "kiln.url" set to an HTTP-accessible Kiln repository. The latter is helpful if you access Kiln via SSH, or have an alternative repository as your upstream remote. Commands: add-remote [--ssh] <repository> [<remote-alias>] add the provided repository as a remote; if no local alias is provided, then the name of the repository will be used as the remote annotate <file> [<file2> [...]] annotate one or more files in Kiln create-branch <branch-name> create a branch repository in Kiln, which can be added to your remotes via "gitkiln add-remote" filehistory <file> [<file2> [...]] show the history for one or more files in Kiln hg-sha <sha> [<sha> [...]] show the equivalent Mercurial SHAs corresponding to the given Git SHAs history go to the history of this repository in Kiln logout log out of Kiln, disposing your authentication token related show related repositories in Kiln, including the entire graph of which commits have and have not been merged into branch repositories settings go to the settings tab for this repository in Kiln show <commit> [<commit2> [...]] go to the provided commits in Kiln. Commit IDs are expanded locally, so "git-kiln show HEAD^" and similar commands will work fine. show-file <file> [<file2> [...]] show the contents of one or more files in Kiln version show gitkiln's version number If you provide no command, git-kiln will take you to the repository in Kiln. ` const kilnUrlSuffix = "kiln/" func extractUrlsAndPaths(s string) (base *url.URL, path string, err error) { out, err := exec.Command("git", "config", "kiln.url").Output() if err == nil && len(out) > 0 { base, err = url.Parse(strings.TrimSpace(string(out))) if err != nil { err = fmt.Errorf("unable to parse kiln.url \"%s\"", string(out)) return } } else { base, err = url.Parse(s) if err != nil { err = fmt.Errorf("unable to parse remote URL \"%s\"", s) return } if base.Scheme != "http" && base.Scheme != "https" && base.Scheme != "ssh" { err = fmt.Errorf("unsupport Kiln URL scheme: \"%s\"", s) return } if base.Scheme == "ssh" { if strings.HasPrefix(base.Host, "our.fogbugz.com") { base.Host = "our.fogbugz.com" base.Path = "/kiln/Code" + base.Path + ".git" base.Scheme = "http" } else if !strings.HasSuffix(base.Host, ".kilnhg.com") { err = fmt.Errorf("cannot infer HTTP path to Kiln repository; please specify using kiln.url") return } else { base.Scheme = "https" base.Path = "/Code" + base.Path + ".git" } } } base.User = nil components := strings.Split(base.Path, "/") l := len(components) components[l-1] = strings.TrimSuffix(components[l-1], ".git") path = strings.Join(components[l-3:l], "/") if strings.HasSuffix(base.Host, ".kilnhg.com") { base.Path = "" } else { idx := strings.Index(base.Path, kilnUrlSuffix) if idx < 0 { err = fmt.Errorf("not sure where Kiln is based on %v", base) return } base.Path = base.Path[0 : idx+len(kilnUrlSuffix)] } return } func isGitAvailable() bool { if err := exec.Command("git", "version").Run(); err != nil { return false } else { return true } } func showHelp() { fmt.Println(documentation) } func showVersion() { fmt.Println(VERSION) } func ensureArgs(n int, explanation string) bool { if len(os.Args) < n { fmt.Fprintln(os.Stderr, explanation) showHelp() return false } else { return true } } func requireAuth(k *kiln.Client) { if err := k.EnsureCredentials(); err != nil { fmt.Fprintf(os.Stderr, "unable to logon: %v\n", err) os.Exit(1) } } func splitRepoPath(repoPath string) (project, group, repo string) { parts := strings.Split(repoPath, "/") repo = parts[len(parts)-1] if len(parts) > 1 { group = parts[len(parts)-2] } if len(parts) > 2 { project = parts[len(parts)-3] } return } func findTargets(related []kiln.Repo, project, group, repo string) []kiln.Repo { targets := make([]kiln.Repo, 0) for _, r := range related { if r.Slug == repo && (len(group) == 0 || r.GroupSlug == group) && (len(project) == 0 || r.ProjectSlug == project) { targets = append(targets, r) } } return targets } func dispatch(k *kiln.Client, repoPath string) { var command string if len(os.Args) == 1 { command = "history" } else { command = os.Args[1] } switch command { case "add-remote": if len(os.Args) < 3 || len(os.Args) > 5 { fmt.Fprintf(os.Stderr, "you must provide a repo to add as the remote") showHelp() return } requireAuth(k) args := os.Args[2:len(os.Args)] ssh := false if args[0] == "--ssh" { ssh = true args = args[1:len(args)] } related, err := k.RelatedRepos(repoPath) if err != nil { fmt.Fprintf(os.Stderr, "unable to query related repos: %v\n", err) return } project, group, repo := splitRepoPath(args[0]) targets := findTargets(related, project, group, repo) if len(targets) == 0 { fmt.Fprintf(os.Stderr, "nothing found; you must use exact matches\n") } else if len(targets) > 1 { fmt.Fprintf(os.Stderr, "too many matches; please provide more components\n") fmt.Fprintf(os.Stderr, "Matches:\n") for _, r := range targets { fmt.Fprintf(os.Stderr, "\t%v/%v/%v\n", r.ProjectSlug, r.GroupSlug, r.Slug) } fmt.Fprintln(os.Stderr, "") } else { var remoteName string if len(args) == 2 { remoteName = args[1] } else { remoteName = repo } var remoteURL string if ssh { remoteURL = targets[0].GitSshUrl } else { remoteURL = targets[0].GitUrl } out, err := exec.Command("git", "remote", "add", remoteName, remoteURL).CombinedOutput() if err != nil || strings.HasPrefix(string(out), "fatal: ") { fmt.Fprintf(os.Stderr, "could not add remote: %s", out) } } case "annotate": if ensureArgs(3, "you must provide at least one file to annotate") { for _, file := range os.Args[2:len(os.Args)] { k.BrowseAnnotatedFile(repoPath, file) } } case "create-branch": if ensureArgs(3, "you must provide a branch name to create") { requireAuth(k) branchName := os.Args[2] r, err := k.CreateBranch(repoPath, branchName) if err != nil { fmt.Fprintf(os.Stderr, "could not create branch: %v\n", err) return } fmt.Printf("created branch repository \"%v\"\n", r.GitUrl) out, err := exec.Command("git", "remote", "add", branchName, r.GitUrl).CombinedOutput() if err != nil || strings.HasPrefix(string(out), "fatal: ") { fmt.Fprintf(os.Stderr, "could not add remote: %s", out) return } fmt.Printf("added \"%v\" as remote for \"%v\"\n", branchName, r.GitUrl) } case "filehistory": if ensureArgs(3, "you must provide at least one file to view") { for _, file := range os.Args[2:len(os.Args)] { k.BrowseFileHistory(repoPath, file) } } case "help": showHelp() case "hg-sha": if ensureArgs(3, "you must provide at least one commit SHA to lookup") { requireAuth(k) commits := make([]string, 0, len(os.Args)-2) for _, commit := range os.Args[2:len(os.Args)] { if fullSHA, err := k.ResolveSHA(commit); err != nil { fmt.Fprintf(os.Stderr, "WARNING: Unable to resolve %v\n", commit) } else { commits = append(commits, fullSHA) } } equivalents, err := k.MercurialEquivalents(repoPath, commits) if err != nil { fmt.Printf("Unable to look up Mercurial SHAs: %v\n", err) } else { for gitSHA, hgSHAs := range equivalents { fmt.Printf("Git[%v]: Hg%v\n", gitSHA, hgSHAs) } } } case "history": k.BrowseHistory(repoPath) case "logout": if k.LoadCredentials() { k.DeleteCredentials() } case "related": k.BrowseRelated(repoPath) case "settings": k.BrowseSettings(repoPath) case "show": if ensureArgs(3, "you must supply at least one commit SHA to show") { for _, commit := range os.Args[2:len(os.Args)] { if err := k.BrowseCommit(repoPath, commit); err != nil { fmt.Fprintf(os.Stderr, "couldn't load commit: %v\n", err) } } } case "show-file": if ensureArgs(3, "you must provide at least one file to view") { for _, file := range os.Args[2:len(os.Args)] { k.BrowseFile(repoPath, file) } } case "version": showVersion() default: showHelp() } } func main() { if !isGitAvailable() { fmt.Fprintln(os.Stderr, "git not found") os.Exit(1) } if _, err := kiln.GitRoot(); err != nil { if len(os.Args) == 2 && os.Args[1] == "help" { showHelp() return } else if len(os.Args) == 2 && os.Args[1] == "version" { showVersion() return } else { fmt.Fprintln(os.Stderr, "git-kiln must be run from within a Git repository; see \"gitkiln help\"") os.Exit(1) } } bytesOut, err := exec.Command("git", "ls-remote", "--get-url").Output() repoUrl := strings.TrimSpace(string(bytesOut)) if err != nil || strings.HasPrefix(repoUrl, "fatal:") { fmt.Fprintln(os.Stderr, "no remote found; set one with \"git remote add\"") os.Exit(1) } kilnUrl, repoPath, err := extractUrlsAndPaths(repoUrl) if err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } k := kiln.NewClient(kilnUrl) dispatch(k, repoPath) }