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

add hg-sha command

Changeset f8151ea4ffc6

Parent cf5e6ffac8b7

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

Changes to 2 files · Browse files at f8151ea4ffc6 Showing diff from parent cf5e6ffac8b7 Diff from another changeset...

Change 1 of 2 Show Changes Only kiln/​api.go Stacked
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 package kiln    import (   "encoding/json"   "fmt"   "strconv"   "strings"  )    // Represents the error JSON returned by Kiln API requests  type ApiError struct {   // Guaranteed-unique code for Kiln errors   Code string `json:"codeError"`   // Human-readable description   Description string `json:"sError"`  }    // Represents a collection of JSON errors, as returned on API failure  type ApiErrors map[string][]ApiError    // Represents the project JSON returned from the Kiln API  type Project struct {   Id int64 `json:"ixProject"`   Slug string `json:"sSlug"`   Name string `json:"sName"`   Description string `json:"sDescription"`   DefaultPermission string `json:"permissionDefault"`   RepoGroups []RepoGroup  }    type RepoGroup struct {   Id int64 `json:"ixRepoGroup"`   ProjectId int64 `json:"ixProject"`   Slug string `json:"sSlug"`   Name string `json:"sName"`   Repos []Repo `json:"repos"`  }    type Repo struct {   Id int64 `json:"ixRepo"`   RepoGroupId int64 `json:"ixRepoGroup"`   ParentId int64 `json:"ixParent"`   IsCentral bool `json:"fCentral"`   Slug string `json:"sSlug"`   GroupSlug string `json:"sGroupSlug"`   ProjectSlug string `json:"sProjectSlug"`   GitUrl string `json:"sGitUrl"`   GitSshUrl string `json:"sGitSshUrl"`   Name string `json:"sName"`   Description string `json:"sDescription"`   Status string `json:"sStatus"`   Size int64 `json:"bytesSize"`   Vcs int `json:"vcs"`   Creator Person `json:"personCreator"`   DefaultPermission string `json:"permissionDefault"`   Branches []Repo `json:"repoBranches"` +} + +type Changeset struct { + Rev string `json:"rev"` + Author string `json:"sAuthor"` + Description string `json:"sDescription"` + Bugs []int `json:"ixBugs"` + Reviews []int `json:"ixReviews"` + Vcs string `json:"vcs"` + MercurialSHAs []string `json:"revsHg"` + GitSHAs []string `json:"revsGit"`  }    type Person struct {   Id int64 `json:"ixPerson"`   Name string `json:"sName"`   Email string `json:"sEmail"`  }    func (k *Client) CreateBranch(repoPath, branchName string) (newRepo *Repo, err error) {   r, err := k.RepoForPath(repoPath)   if err != nil {   return   }   params := apiParams{   "sName": branchName,   "ixRepoGroup": strconv.FormatInt(r.RepoGroupId, 10),   "ixParent": strconv.FormatInt(r.Id, 10),   "fCentral": "false",   }   var resp []byte   if resp, err = k.apiPost("Repo/Create", params); err == nil {   var errors ApiErrors   if err = json.Unmarshal(resp, &errors); err == nil {   if kilnErr, _ := errors["errors"]; len(kilnErr) > 0 {   err = fmt.Errorf("failed: %v", kilnErr[0].Description)   return   }   }   newRepo = new(Repo)   err = json.Unmarshal(resp, newRepo)   return   }   return  }    func (k *Client) Projects() (projects []Project, err error) {   if resp, err := k.apiGet("Project", apiParams{}); err == nil {   err = json.Unmarshal(resp, &projects)   }   return  }    func (k *Client) RepoForPath(repoPath string) (*Repo, error) {   parts := strings.Split(repoPath, "/")   if len(parts) != 3 {   return nil, fmt.Errorf("unknown repository target: %v", repoPath)   }   if projects, err := k.Projects(); err == nil {   for _, project := range projects {   for _, repoGroup := range project.RepoGroups {   for _, repo := range repoGroup.Repos {   if strings.EqualFold(repo.ProjectSlug, parts[0]) &&   strings.EqualFold(repo.GroupSlug, parts[1]) &&   strings.EqualFold(repo.Slug, parts[2]) {   return &repo, nil   }   }   }   }   }   return nil, fmt.Errorf("repository not found")  }    func (k *Client) IdForRepo(repoPath string) (int64, error) {   repo, err := k.RepoForPath(repoPath)   if err != nil {   return -1, err   }   return repo.Id, nil  }    func (k *Client) RelatedRepos(repoPath string) (repos []Repo, err error) {   repoId, err := k.IdForRepo(repoPath)   if err == nil {   resp, err := k.apiGet(fmt.Sprintf("Repo/%v/Related", repoId), apiParams{})   if err == nil {   err = json.Unmarshal(resp, &repos)   }   }   return  } + +func (k *Client) MercurialEquivalent(repoPath, commit string) (hgSHAs []string, err error) { + var repoId int64 + var resp []byte + var fullSHA string + + repoId, err = k.IdForRepo(repoPath) + if err != nil { + return + } + fullSHA, err = k.ResolveSHA(commit) + if err != nil { + return + } + resp, err = k.apiGet(fmt.Sprintf("Repo/%v/History", repoId), apiParams{"revs": fullSHA}) + if err != nil { + return + } + var errors ApiErrors + if err = json.Unmarshal(resp, &errors); err == nil { + if apiErrors, _ := errors["errors"]; len(apiErrors) > 0 { + err = fmt.Errorf("failed: %v", apiErrors[0].Description) + } + } + var changesets []Changeset + if err = json.Unmarshal(resp, &changesets); err != nil { + err = fmt.Errorf("unable to process response: %v", err) + } else { + for _, changeset := range changesets { + hgSHAs = append(hgSHAs, changeset.MercurialSHAs...) + } + } + return +}
Change 1 of 2 Show Entire File main.go Stacked
 
34
35
36
 
 
 
37
38
39
 
229
230
231
 
 
 
 
 
 
 
 
 
 
 
232
233
234
 
34
35
36
37
38
39
40
41
42
 
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
@@ -34,6 +34,9 @@
  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 @@ -229,6 +232,17 @@
  }   case "help":   showHelp() + case "hg-sha": + if ensureArgs(3, "you must provide at least one commit SHA to lookup") { + requireAuth(k) + for _, commit := range os.Args[2:len(os.Args)] { + if equivalent, err := k.MercurialEquivalent(repoPath, commit); err == nil { + fmt.Printf("%v: %v\n", commit, equivalent) + } else { + fmt.Printf("%v: <none found> (%v)\n", commit, err) + } + } + }   case "history":   k.BrowseHistory(repoPath)   case "logout":