Kiln » gitkiln Read More
Clone URL:  
Pushed to one repository · View In Graph Contained in master

add "add-remote" command

Changeset b750101b8e1a

Parent c69c2ff00cbf

committed by Profile picture of User 12Benjamin Pollack <benjamin@fogcreek.com>

Profile picture of User 12 authored by Benjamin Pollack <benjamin@fogcreek.com>

Changes to one file · Browse files at b750101b8e1a Showing diff from parent c69c2ff00cbf Diff from another changeset...

Change 1 of 2 Show Entire File main.go Stacked
 
103
104
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
107
108
 
116
117
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
120
121
 
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
 
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
@@ -103,6 +103,37 @@
  }  }   +func requireAuth(k *kiln.KilnClient) { + 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.KilnRepo, project, group, repo string) []kiln.KilnRepo { + targets := make([]kiln.KilnRepo, 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.KilnClient, repoPath string) {   var command string   if len(os.Args) == 1 { @@ -116,6 +147,59 @@
  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 "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 "filehistory":