Mercurial > hg
annotate contrib/phabricator.py @ 33601:850d2ec2cf6a stable
phabricator: convert unicode to binary when writing patches
This is a quick fix to make `hg phabread D189` work.
It seems we might want to replace all `r''` to `u''`, and add more
`encoding.*to*` to be more explicit when interacting with `json` module.
Differential Revision: https://phab.mercurial-scm.org/D192
author | Jun Wu <quark@fb.com> |
---|---|
date | Thu, 27 Jul 2017 12:03:01 -0700 |
parents | 91e3dcefc9b7 |
children | 40cfe3197bc1 |
rev | line source |
---|---|
33198 | 1 # phabricator.py - simple Phabricator integration |
2 # | |
3 # Copyright 2017 Facebook, Inc. | |
4 # | |
5 # This software may be used and distributed according to the terms of the | |
6 # GNU General Public License version 2 or any later version. | |
7 """simple Phabricator integration | |
8 | |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
9 This extension provides a ``phabsend`` command which sends a stack of |
33200
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
10 changesets to Phabricator without amending commit messages, and a ``phabread`` |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
11 command which prints a stack of revisions in a format suitable |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
12 for :hg:`import`. |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
13 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
14 By default, Phabricator requires ``Test Plan`` which might prevent some |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
15 changeset from being sent. The requirement could be disabled by changing |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
16 ``differential.require-test-plan-field`` config server side. |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
17 |
33198 | 18 Config:: |
19 | |
20 [phabricator] | |
21 # Phabricator URL | |
22 url = https://phab.example.com/ | |
23 | |
24 # API token. Get it from https://$HOST/conduit/login/ | |
25 token = cli-xxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
26 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
27 # Repo callsign. If a repo has a URL https://$HOST/diffusion/FOO, then its |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
28 # callsign is "FOO". |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
29 callsign = FOO |
33200
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
30 |
33198 | 31 """ |
32 | |
33 from __future__ import absolute_import | |
34 | |
35 import json | |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
36 import re |
33198 | 37 |
33443
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
38 from mercurial.node import bin, nullid |
33198 | 39 from mercurial.i18n import _ |
40 from mercurial import ( | |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
41 encoding, |
33198 | 42 error, |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
43 mdiff, |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
44 obsolete, |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
45 patch, |
33198 | 46 registrar, |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
47 scmutil, |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
48 tags, |
33198 | 49 url as urlmod, |
50 util, | |
51 ) | |
52 | |
53 cmdtable = {} | |
54 command = registrar.command(cmdtable) | |
55 | |
56 def urlencodenested(params): | |
57 """like urlencode, but works with nested parameters. | |
58 | |
59 For example, if params is {'a': ['b', 'c'], 'd': {'e': 'f'}}, it will be | |
60 flattened to {'a[0]': 'b', 'a[1]': 'c', 'd[e]': 'f'} and then passed to | |
61 urlencode. Note: the encoding is consistent with PHP's http_build_query. | |
62 """ | |
63 flatparams = util.sortdict() | |
64 def process(prefix, obj): | |
65 items = {list: enumerate, dict: lambda x: x.items()}.get(type(obj)) | |
66 if items is None: | |
67 flatparams[prefix] = obj | |
68 else: | |
69 for k, v in items(obj): | |
70 if prefix: | |
71 process('%s[%s]' % (prefix, k), v) | |
72 else: | |
73 process(k, v) | |
74 process('', params) | |
75 return util.urlreq.urlencode(flatparams) | |
76 | |
77 def readurltoken(repo): | |
78 """return conduit url, token and make sure they exist | |
79 | |
80 Currently read from [phabricator] config section. In the future, it might | |
81 make sense to read from .arcconfig and .arcrc as well. | |
82 """ | |
83 values = [] | |
84 section = 'phabricator' | |
85 for name in ['url', 'token']: | |
86 value = repo.ui.config(section, name) | |
87 if not value: | |
88 raise error.Abort(_('config %s.%s is required') % (section, name)) | |
89 values.append(value) | |
90 return values | |
91 | |
92 def callconduit(repo, name, params): | |
93 """call Conduit API, params is a dict. return json.loads result, or None""" | |
94 host, token = readurltoken(repo) | |
95 url, authinfo = util.url('/'.join([host, 'api', name])).authinfo() | |
96 urlopener = urlmod.opener(repo.ui, authinfo) | |
97 repo.ui.debug('Conduit Call: %s %s\n' % (url, params)) | |
98 params = params.copy() | |
99 params['api.token'] = token | |
100 request = util.urlreq.request(url, data=urlencodenested(params)) | |
101 body = urlopener.open(request).read() | |
102 repo.ui.debug('Conduit Response: %s\n' % body) | |
103 parsed = json.loads(body) | |
104 if parsed.get(r'error_code'): | |
105 msg = (_('Conduit Error (%s): %s') | |
106 % (parsed[r'error_code'], parsed[r'error_info'])) | |
107 raise error.Abort(msg) | |
108 return parsed[r'result'] | |
109 | |
110 @command('debugcallconduit', [], _('METHOD')) | |
111 def debugcallconduit(ui, repo, name): | |
112 """call Conduit API | |
113 | |
114 Call parameters are read from stdin as a JSON blob. Result will be written | |
115 to stdout as a JSON blob. | |
116 """ | |
117 params = json.loads(ui.fin.read()) | |
118 result = callconduit(repo, name, params) | |
119 s = json.dumps(result, sort_keys=True, indent=2, separators=(',', ': ')) | |
120 ui.write('%s\n' % s) | |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
121 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
122 def getrepophid(repo): |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
123 """given callsign, return repository PHID or None""" |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
124 # developer config: phabricator.repophid |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
125 repophid = repo.ui.config('phabricator', 'repophid') |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
126 if repophid: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
127 return repophid |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
128 callsign = repo.ui.config('phabricator', 'callsign') |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
129 if not callsign: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
130 return None |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
131 query = callconduit(repo, 'diffusion.repository.search', |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
132 {'constraints': {'callsigns': [callsign]}}) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
133 if len(query[r'data']) == 0: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
134 return None |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
135 repophid = encoding.strtolocal(query[r'data'][0][r'phid']) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
136 repo.ui.setconfig('phabricator', 'repophid', repophid) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
137 return repophid |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
138 |
33263
ed61189763ef
phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33200
diff
changeset
|
139 _differentialrevisiontagre = re.compile('\AD([1-9][0-9]*)\Z') |
ed61189763ef
phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33200
diff
changeset
|
140 _differentialrevisiondescre = re.compile( |
33564
91e3dcefc9b7
phabricator: sanity check Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33498
diff
changeset
|
141 '^Differential Revision:\s*(.*)D([1-9][0-9]*)$', re.M) |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
142 |
33442
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
143 def getoldnodedrevmap(repo, nodelist): |
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
144 """find previous nodes that has been sent to Phabricator |
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
145 |
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
146 return {node: (oldnode or None, Differential Revision ID)} |
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
147 for node in nodelist with known previous sent versions, or associated |
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
148 Differential Revision IDs. |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
149 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
150 Examines all precursors and their tags. Tags with format like "D1234" are |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
151 considered a match and the node with that tag, and the number after "D" |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
152 (ex. 1234) will be returned. |
33263
ed61189763ef
phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33200
diff
changeset
|
153 |
ed61189763ef
phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33200
diff
changeset
|
154 If tags are not found, examine commit message. The "Differential Revision:" |
ed61189763ef
phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33200
diff
changeset
|
155 line could associate this changeset to a Differential Revision. |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
156 """ |
33442
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
157 url, token = readurltoken(repo) |
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
158 unfi = repo.unfiltered() |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
159 nodemap = unfi.changelog.nodemap |
33263
ed61189763ef
phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33200
diff
changeset
|
160 |
33442
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
161 result = {} # {node: (oldnode or None, drev)} |
33443
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
162 toconfirm = {} # {node: (oldnode, {precnode}, drev)} |
33442
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
163 for node in nodelist: |
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
164 ctx = unfi[node] |
33443
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
165 # For tags like "D123", put them into "toconfirm" to verify later |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
166 precnodes = list(obsolete.allprecursors(unfi.obsstore, [node])) |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
167 for n in precnodes: |
33442
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
168 if n in nodemap: |
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
169 for tag in unfi.nodetags(n): |
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
170 m = _differentialrevisiontagre.match(tag) |
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
171 if m: |
33443
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
172 toconfirm[node] = (n, set(precnodes), int(m.group(1))) |
33442
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
173 continue |
33263
ed61189763ef
phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33200
diff
changeset
|
174 |
33564
91e3dcefc9b7
phabricator: sanity check Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33498
diff
changeset
|
175 # Check commit message (make sure URL matches) |
33442
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
176 m = _differentialrevisiondescre.search(ctx.description()) |
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
177 if m: |
33564
91e3dcefc9b7
phabricator: sanity check Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33498
diff
changeset
|
178 if m.group(1).rstrip('/') == url.rstrip('/'): |
91e3dcefc9b7
phabricator: sanity check Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33498
diff
changeset
|
179 result[node] = (None, int(m.group(2))) |
91e3dcefc9b7
phabricator: sanity check Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33498
diff
changeset
|
180 else: |
91e3dcefc9b7
phabricator: sanity check Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33498
diff
changeset
|
181 unfi.ui.warn(_('%s: Differential Revision URL ignored - host ' |
91e3dcefc9b7
phabricator: sanity check Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33498
diff
changeset
|
182 'does not match config\n') % ctx) |
33263
ed61189763ef
phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents:
33200
diff
changeset
|
183 |
33443
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
184 # Double check if tags are genuine by collecting all old nodes from |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
185 # Phabricator, and expect precursors overlap with it. |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
186 if toconfirm: |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
187 confirmed = {} # {drev: {oldnode}} |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
188 drevs = [drev for n, precs, drev in toconfirm.values()] |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
189 diffs = callconduit(unfi, 'differential.querydiffs', |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
190 {'revisionIDs': drevs}) |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
191 for diff in diffs.values(): |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
192 drev = int(diff[r'revisionID']) |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
193 oldnode = bin(encoding.unitolocal(getdiffmeta(diff).get(r'node'))) |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
194 if node: |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
195 confirmed.setdefault(drev, set()).add(oldnode) |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
196 for newnode, (oldnode, precset, drev) in toconfirm.items(): |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
197 if bool(precset & confirmed.get(drev, set())): |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
198 result[newnode] = (oldnode, drev) |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
199 else: |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
200 tagname = 'D%d' % drev |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
201 tags.tag(repo, tagname, nullid, message=None, user=None, |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
202 date=None, local=True) |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
203 unfi.ui.warn(_('D%s: local tag removed - does not match ' |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
204 'Differential history\n') % drev) |
e48082e0a8d5
phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents:
33442
diff
changeset
|
205 |
33442
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
206 return result |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
207 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
208 def getdiff(ctx, diffopts): |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
209 """plain-text diff without header (user, commit message, etc)""" |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
210 output = util.stringio() |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
211 for chunk, _label in patch.diffui(ctx.repo(), ctx.p1().node(), ctx.node(), |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
212 None, opts=diffopts): |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
213 output.write(chunk) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
214 return output.getvalue() |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
215 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
216 def creatediff(ctx): |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
217 """create a Differential Diff""" |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
218 repo = ctx.repo() |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
219 repophid = getrepophid(repo) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
220 # Create a "Differential Diff" via "differential.createrawdiff" API |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
221 params = {'diff': getdiff(ctx, mdiff.diffopts(git=True, context=32767))} |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
222 if repophid: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
223 params['repositoryPHID'] = repophid |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
224 diff = callconduit(repo, 'differential.createrawdiff', params) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
225 if not diff: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
226 raise error.Abort(_('cannot create diff for %s') % ctx) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
227 return diff |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
228 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
229 def writediffproperties(ctx, diff): |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
230 """write metadata to diff so patches could be applied losslessly""" |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
231 params = { |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
232 'diff_id': diff[r'id'], |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
233 'name': 'hg:meta', |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
234 'data': json.dumps({ |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
235 'user': ctx.user(), |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
236 'date': '%d %d' % ctx.date(), |
33264
266321579c68
phabricator: add node and p1 to hg:meta property
Jun Wu <quark@fb.com>
parents:
33263
diff
changeset
|
237 'node': ctx.hex(), |
266321579c68
phabricator: add node and p1 to hg:meta property
Jun Wu <quark@fb.com>
parents:
33263
diff
changeset
|
238 'parent': ctx.p1().hex(), |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
239 }), |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
240 } |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
241 callconduit(ctx.repo(), 'differential.setdiffproperty', params) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
242 |
33498
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
243 def createdifferentialrevision(ctx, revid=None, parentrevid=None, oldnode=None, |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
244 actions=None): |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
245 """create or update a Differential Revision |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
246 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
247 If revid is None, create a new Differential Revision, otherwise update |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
248 revid. If parentrevid is not None, set it as a dependency. |
33265
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
249 |
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
250 If oldnode is not None, check if the patch content (without commit message |
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
251 and metadata) has changed before creating another diff. |
33498
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
252 |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
253 If actions is not None, they will be appended to the transaction. |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
254 """ |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
255 repo = ctx.repo() |
33265
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
256 if oldnode: |
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
257 diffopts = mdiff.diffopts(git=True, context=1) |
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
258 oldctx = repo.unfiltered()[oldnode] |
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
259 neednewdiff = (getdiff(ctx, diffopts) != getdiff(oldctx, diffopts)) |
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
260 else: |
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
261 neednewdiff = True |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
262 |
33265
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
263 transactions = [] |
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
264 if neednewdiff: |
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
265 diff = creatediff(ctx) |
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
266 writediffproperties(ctx, diff) |
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
267 transactions.append({'type': 'update', 'value': diff[r'phid']}) |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
268 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
269 # Use a temporary summary to set dependency. There might be better ways but |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
270 # I cannot find them for now. But do not do that if we are updating an |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
271 # existing revision (revid is not None) since that introduces visible |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
272 # churns (someone edited "Summary" twice) on the web page. |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
273 if parentrevid and revid is None: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
274 summary = 'Depends on D%s' % parentrevid |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
275 transactions += [{'type': 'summary', 'value': summary}, |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
276 {'type': 'summary', 'value': ' '}] |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
277 |
33498
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
278 if actions: |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
279 transactions += actions |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
280 |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
281 # Parse commit message and update related fields. |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
282 desc = ctx.description() |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
283 info = callconduit(repo, 'differential.parsecommitmessage', |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
284 {'corpus': desc}) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
285 for k, v in info[r'fields'].items(): |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
286 if k in ['title', 'summary', 'testPlan']: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
287 transactions.append({'type': k, 'value': v}) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
288 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
289 params = {'transactions': transactions} |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
290 if revid is not None: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
291 # Update an existing Differential Revision |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
292 params['objectIdentifier'] = revid |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
293 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
294 revision = callconduit(repo, 'differential.revision.edit', params) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
295 if not revision: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
296 raise error.Abort(_('cannot create revision for %s') % ctx) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
297 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
298 return revision |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
299 |
33498
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
300 def userphids(repo, names): |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
301 """convert user names to PHIDs""" |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
302 query = {'constraints': {'usernames': names}} |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
303 result = callconduit(repo, 'user.search', query) |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
304 # username not found is not an error of the API. So check if we have missed |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
305 # some names here. |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
306 data = result[r'data'] |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
307 resolved = set(entry[r'fields'][r'username'] for entry in data) |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
308 unresolved = set(names) - resolved |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
309 if unresolved: |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
310 raise error.Abort(_('unknown username: %s') |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
311 % ' '.join(sorted(unresolved))) |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
312 return [entry[r'phid'] for entry in data] |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
313 |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
314 @command('phabsend', |
33498
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
315 [('r', 'rev', [], _('revisions to send'), _('REV')), |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
316 ('', 'reviewer', [], _('specify reviewers'))], |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
317 _('REV [OPTIONS]')) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
318 def phabsend(ui, repo, *revs, **opts): |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
319 """upload changesets to Phabricator |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
320 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
321 If there are multiple revisions specified, they will be send as a stack |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
322 with a linear dependencies relationship using the order specified by the |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
323 revset. |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
324 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
325 For the first time uploading changesets, local tags will be created to |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
326 maintain the association. After the first time, phabsend will check |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
327 obsstore and tags information so it can figure out whether to update an |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
328 existing Differential Revision, or create a new one. |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
329 """ |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
330 revs = list(revs) + opts.get('rev', []) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
331 revs = scmutil.revrange(repo, revs) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
332 |
33266
5b2391b46906
phabricator: abort if phabsend gets empty revs
Jun Wu <quark@fb.com>
parents:
33265
diff
changeset
|
333 if not revs: |
5b2391b46906
phabricator: abort if phabsend gets empty revs
Jun Wu <quark@fb.com>
parents:
33265
diff
changeset
|
334 raise error.Abort(_('phabsend requires at least one changeset')) |
5b2391b46906
phabricator: abort if phabsend gets empty revs
Jun Wu <quark@fb.com>
parents:
33265
diff
changeset
|
335 |
33498
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
336 actions = [] |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
337 reviewers = opts.get('reviewer', []) |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
338 if reviewers: |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
339 phids = userphids(repo, reviewers) |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
340 actions.append({'type': 'reviewers.add', 'value': phids}) |
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
341 |
33442
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
342 oldnodedrev = getoldnodedrevmap(repo, [repo[r].node() for r in revs]) |
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
343 |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
344 # Send patches one by one so we know their Differential Revision IDs and |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
345 # can provide dependency relationship |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
346 lastrevid = None |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
347 for rev in revs: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
348 ui.debug('sending rev %d\n' % rev) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
349 ctx = repo[rev] |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
350 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
351 # Get Differential Revision ID |
33442
3ab0d5767b54
phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents:
33441
diff
changeset
|
352 oldnode, revid = oldnodedrev.get(ctx.node(), (None, None)) |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
353 if oldnode != ctx.node(): |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
354 # Create or update Differential Revision |
33265
95f658b558a3
phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents:
33264
diff
changeset
|
355 revision = createdifferentialrevision(ctx, revid, lastrevid, |
33498
b7a75b9a3386
phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents:
33443
diff
changeset
|
356 oldnode, actions) |
33199
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
357 newrevid = int(revision[r'object'][r'id']) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
358 if revid: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
359 action = _('updated') |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
360 else: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
361 action = _('created') |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
362 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
363 # Create a local tag to note the association |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
364 tagname = 'D%d' % newrevid |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
365 tags.tag(repo, tagname, ctx.node(), message=None, user=None, |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
366 date=None, local=True) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
367 else: |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
368 # Nothing changed. But still set "newrevid" so the next revision |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
369 # could depend on this one. |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
370 newrevid = revid |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
371 action = _('skipped') |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
372 |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
373 ui.write(_('D%s: %s - %s: %s\n') % (newrevid, action, ctx, |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
374 ctx.description().split('\n')[0])) |
228ad1e58a85
phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents:
33198
diff
changeset
|
375 lastrevid = newrevid |
33200
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
376 |
33264
266321579c68
phabricator: add node and p1 to hg:meta property
Jun Wu <quark@fb.com>
parents:
33263
diff
changeset
|
377 # Map from "hg:meta" keys to header understood by "hg import". The order is |
266321579c68
phabricator: add node and p1 to hg:meta property
Jun Wu <quark@fb.com>
parents:
33263
diff
changeset
|
378 # consistent with "hg export" output. |
266321579c68
phabricator: add node and p1 to hg:meta property
Jun Wu <quark@fb.com>
parents:
33263
diff
changeset
|
379 _metanamemap = util.sortdict([(r'user', 'User'), (r'date', 'Date'), |
266321579c68
phabricator: add node and p1 to hg:meta property
Jun Wu <quark@fb.com>
parents:
33263
diff
changeset
|
380 (r'node', 'Node ID'), (r'parent', 'Parent ')]) |
266321579c68
phabricator: add node and p1 to hg:meta property
Jun Wu <quark@fb.com>
parents:
33263
diff
changeset
|
381 |
33267
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
382 def querydrev(repo, params, stack=False): |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
383 """return a list of "Differential Revision" dicts |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
384 |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
385 params is the input of "differential.query" API, and is expected to match |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
386 just a single Differential Revision. |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
387 |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
388 A "Differential Revision dict" looks like: |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
389 |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
390 { |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
391 "id": "2", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
392 "phid": "PHID-DREV-672qvysjcczopag46qty", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
393 "title": "example", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
394 "uri": "https://phab.example.com/D2", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
395 "dateCreated": "1499181406", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
396 "dateModified": "1499182103", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
397 "authorPHID": "PHID-USER-tv3ohwc4v4jeu34otlye", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
398 "status": "0", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
399 "statusName": "Needs Review", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
400 "properties": [], |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
401 "branch": null, |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
402 "summary": "", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
403 "testPlan": "", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
404 "lineCount": "2", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
405 "activeDiffPHID": "PHID-DIFF-xoqnjkobbm6k4dk6hi72", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
406 "diffs": [ |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
407 "3", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
408 "4", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
409 ], |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
410 "commits": [], |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
411 "reviewers": [], |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
412 "ccs": [], |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
413 "hashes": [], |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
414 "auxiliary": { |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
415 "phabricator:projects": [], |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
416 "phabricator:depends-on": [ |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
417 "PHID-DREV-gbapp366kutjebt7agcd" |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
418 ] |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
419 }, |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
420 "repositoryPHID": "PHID-REPO-hub2hx62ieuqeheznasv", |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
421 "sourcePath": null |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
422 } |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
423 |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
424 If stack is True, return a list of "Differential Revision dict"s in an |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
425 order that the latter ones depend on the former ones. Otherwise, return a |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
426 list of a unique "Differential Revision dict". |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
427 """ |
33269
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
428 prefetched = {} # {id or phid: drev} |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
429 def fetch(params): |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
430 """params -> single drev or None""" |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
431 key = (params.get(r'ids') or params.get(r'phids') or [None])[0] |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
432 if key in prefetched: |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
433 return prefetched[key] |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
434 # Otherwise, send the request. If we're fetching a stack, be smarter |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
435 # and fetch more ids in one batch, even if it could be unnecessary. |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
436 batchparams = params |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
437 if stack and len(params.get(r'ids', [])) == 1: |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
438 i = int(params[r'ids'][0]) |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
439 # developer config: phabricator.batchsize |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
440 batchsize = repo.ui.configint('phabricator', 'batchsize', 12) |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
441 batchparams = {'ids': range(max(1, i - batchsize), i + 1)} |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
442 drevs = callconduit(repo, 'differential.query', batchparams) |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
443 # Fill prefetched with the result |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
444 for drev in drevs: |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
445 prefetched[drev[r'phid']] = drev |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
446 prefetched[int(drev[r'id'])] = drev |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
447 if key not in prefetched: |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
448 raise error.Abort(_('cannot get Differential Revision %r') % params) |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
449 return prefetched[key] |
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
450 |
33271
02299a28ba34
phabricator: do not read a same revision twice
Jun Wu <quark@fb.com>
parents:
33269
diff
changeset
|
451 visited = set() |
33267
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
452 result = [] |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
453 queue = [params] |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
454 while queue: |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
455 params = queue.pop() |
33269
ead6749354e1
phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents:
33268
diff
changeset
|
456 drev = fetch(params) |
33271
02299a28ba34
phabricator: do not read a same revision twice
Jun Wu <quark@fb.com>
parents:
33269
diff
changeset
|
457 if drev[r'id'] in visited: |
02299a28ba34
phabricator: do not read a same revision twice
Jun Wu <quark@fb.com>
parents:
33269
diff
changeset
|
458 continue |
02299a28ba34
phabricator: do not read a same revision twice
Jun Wu <quark@fb.com>
parents:
33269
diff
changeset
|
459 visited.add(drev[r'id']) |
33267
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
460 result.append(drev) |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
461 if stack: |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
462 auxiliary = drev.get(r'auxiliary', {}) |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
463 depends = auxiliary.get(r'phabricator:depends-on', []) |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
464 for phid in depends: |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
465 queue.append({'phids': [phid]}) |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
466 result.reverse() |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
467 return result |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
468 |
33268
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
469 def getdescfromdrev(drev): |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
470 """get description (commit message) from "Differential Revision" |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
471 |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
472 This is similar to differential.getcommitmessage API. But we only care |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
473 about limited fields: title, summary, test plan, and URL. |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
474 """ |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
475 title = drev[r'title'] |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
476 summary = drev[r'summary'].rstrip() |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
477 testplan = drev[r'testPlan'].rstrip() |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
478 if testplan: |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
479 testplan = 'Test Plan:\n%s' % testplan |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
480 uri = 'Differential Revision: %s' % drev[r'uri'] |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
481 return '\n\n'.join(filter(None, [title, summary, testplan, uri])) |
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
482 |
33441
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
483 def getdiffmeta(diff): |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
484 """get commit metadata (date, node, user, p1) from a diff object |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
485 |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
486 The metadata could be "hg:meta", sent by phabsend, like: |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
487 |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
488 "properties": { |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
489 "hg:meta": { |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
490 "date": "1499571514 25200", |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
491 "node": "98c08acae292b2faf60a279b4189beb6cff1414d", |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
492 "user": "Foo Bar <foo@example.com>", |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
493 "parent": "6d0abad76b30e4724a37ab8721d630394070fe16" |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
494 } |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
495 } |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
496 |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
497 Or converted from "local:commits", sent by "arc", like: |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
498 |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
499 "properties": { |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
500 "local:commits": { |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
501 "98c08acae292b2faf60a279b4189beb6cff1414d": { |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
502 "author": "Foo Bar", |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
503 "time": 1499546314, |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
504 "branch": "default", |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
505 "tag": "", |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
506 "commit": "98c08acae292b2faf60a279b4189beb6cff1414d", |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
507 "rev": "98c08acae292b2faf60a279b4189beb6cff1414d", |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
508 "local": "1000", |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
509 "parents": ["6d0abad76b30e4724a37ab8721d630394070fe16"], |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
510 "summary": "...", |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
511 "message": "...", |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
512 "authorEmail": "foo@example.com" |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
513 } |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
514 } |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
515 } |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
516 |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
517 Note: metadata extracted from "local:commits" will lose time zone |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
518 information. |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
519 """ |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
520 props = diff.get(r'properties') or {} |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
521 meta = props.get(r'hg:meta') |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
522 if not meta and props.get(r'local:commits'): |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
523 commit = sorted(props[r'local:commits'].values())[0] |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
524 meta = { |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
525 r'date': r'%d 0' % commit[r'time'], |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
526 r'node': commit[r'rev'], |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
527 r'user': r'%s <%s>' % (commit[r'author'], commit[r'authorEmail']), |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
528 } |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
529 if len(commit.get(r'parents', ())) >= 1: |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
530 meta[r'parent'] = commit[r'parents'][0] |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
531 return meta or {} |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
532 |
33267
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
533 def readpatch(repo, params, write, stack=False): |
33200
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
534 """generate plain-text patch readable by 'hg import' |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
535 |
33267
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
536 write is usually ui.write. params is passed to "differential.query". If |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
537 stack is True, also write dependent patches. |
33200
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
538 """ |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
539 # Differential Revisions |
33267
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
540 drevs = querydrev(repo, params, stack) |
33200
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
541 |
33267
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
542 # Prefetch hg:meta property for all diffs |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
543 diffids = sorted(set(max(int(v) for v in drev[r'diffs']) for drev in drevs)) |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
544 diffs = callconduit(repo, 'differential.querydiffs', {'ids': diffids}) |
33200
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
545 |
33267
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
546 # Generate patch for each drev |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
547 for drev in drevs: |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
548 repo.ui.note(_('reading D%s\n') % drev[r'id']) |
33200
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
549 |
33267
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
550 diffid = max(int(v) for v in drev[r'diffs']) |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
551 body = callconduit(repo, 'differential.getrawdiff', {'diffID': diffid}) |
33268
85391b95961d
phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents:
33267
diff
changeset
|
552 desc = getdescfromdrev(drev) |
33267
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
553 header = '# HG changeset patch\n' |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
554 |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
555 # Try to preserve metadata from hg:meta property. Write hg patch |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
556 # headers that can be read by the "import" command. See patchheadermap |
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
557 # and extract in mercurial/patch.py for supported headers. |
33441
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
558 meta = getdiffmeta(diffs[str(diffid)]) |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
559 for k in _metanamemap.keys(): |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
560 if k in meta: |
de7c6ec27d99
phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents:
33271
diff
changeset
|
561 header += '# %s %s\n' % (_metanamemap[k], meta[k]) |
33200
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
562 |
33601
850d2ec2cf6a
phabricator: convert unicode to binary when writing patches
Jun Wu <quark@fb.com>
parents:
33564
diff
changeset
|
563 content = '%s%s\n%s' % (header, desc, body) |
850d2ec2cf6a
phabricator: convert unicode to binary when writing patches
Jun Wu <quark@fb.com>
parents:
33564
diff
changeset
|
564 write(encoding.unitolocal(content)) |
33200
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
565 |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
566 @command('phabread', |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
567 [('', 'stack', False, _('read dependencies'))], |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
568 _('REVID [OPTIONS]')) |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
569 def phabread(ui, repo, revid, **opts): |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
570 """print patches from Phabricator suitable for importing |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
571 |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
572 REVID could be a Differential Revision identity, like ``D123``, or just the |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
573 number ``123``, or a full URL like ``https://phab.example.com/D123``. |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
574 |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
575 If --stack is given, follow dependencies information and read all patches. |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
576 """ |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
577 try: |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
578 revid = int(revid.split('/')[-1].replace('D', '')) |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
579 except ValueError: |
04cf9927f350
phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents:
33199
diff
changeset
|
580 raise error.Abort(_('invalid Revision ID: %s') % revid) |
33267
dba9f88659a3
phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents:
33266
diff
changeset
|
581 readpatch(repo, {'ids': [revid]}, ui.write, opts.get('stack')) |