##// END OF EJS Templates
phabricator: add addremoved and addmodified functions...
Ian Moody -
r43460:c19b3270 default
parent child Browse files
Show More
@@ -1,1495 +1,1533 b''
1 # phabricator.py - simple Phabricator integration
1 # phabricator.py - simple Phabricator integration
2 #
2 #
3 # Copyright 2017 Facebook, Inc.
3 # Copyright 2017 Facebook, Inc.
4 #
4 #
5 # This software may be used and distributed according to the terms of the
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.
6 # GNU General Public License version 2 or any later version.
7 """simple Phabricator integration (EXPERIMENTAL)
7 """simple Phabricator integration (EXPERIMENTAL)
8
8
9 This extension provides a ``phabsend`` command which sends a stack of
9 This extension provides a ``phabsend`` command which sends a stack of
10 changesets to Phabricator, and a ``phabread`` command which prints a stack of
10 changesets to Phabricator, and a ``phabread`` command which prints a stack of
11 revisions in a format suitable for :hg:`import`, and a ``phabupdate`` command
11 revisions in a format suitable for :hg:`import`, and a ``phabupdate`` command
12 to update statuses in batch.
12 to update statuses in batch.
13
13
14 By default, Phabricator requires ``Test Plan`` which might prevent some
14 By default, Phabricator requires ``Test Plan`` which might prevent some
15 changeset from being sent. The requirement could be disabled by changing
15 changeset from being sent. The requirement could be disabled by changing
16 ``differential.require-test-plan-field`` config server side.
16 ``differential.require-test-plan-field`` config server side.
17
17
18 Config::
18 Config::
19
19
20 [phabricator]
20 [phabricator]
21 # Phabricator URL
21 # Phabricator URL
22 url = https://phab.example.com/
22 url = https://phab.example.com/
23
23
24 # Repo callsign. If a repo has a URL https://$HOST/diffusion/FOO, then its
24 # Repo callsign. If a repo has a URL https://$HOST/diffusion/FOO, then its
25 # callsign is "FOO".
25 # callsign is "FOO".
26 callsign = FOO
26 callsign = FOO
27
27
28 # curl command to use. If not set (default), use builtin HTTP library to
28 # curl command to use. If not set (default), use builtin HTTP library to
29 # communicate. If set, use the specified curl command. This could be useful
29 # communicate. If set, use the specified curl command. This could be useful
30 # if you need to specify advanced options that is not easily supported by
30 # if you need to specify advanced options that is not easily supported by
31 # the internal library.
31 # the internal library.
32 curlcmd = curl --connect-timeout 2 --retry 3 --silent
32 curlcmd = curl --connect-timeout 2 --retry 3 --silent
33
33
34 [auth]
34 [auth]
35 example.schemes = https
35 example.schemes = https
36 example.prefix = phab.example.com
36 example.prefix = phab.example.com
37
37
38 # API token. Get it from https://$HOST/conduit/login/
38 # API token. Get it from https://$HOST/conduit/login/
39 example.phabtoken = cli-xxxxxxxxxxxxxxxxxxxxxxxxxxxx
39 example.phabtoken = cli-xxxxxxxxxxxxxxxxxxxxxxxxxxxx
40 """
40 """
41
41
42 from __future__ import absolute_import
42 from __future__ import absolute_import
43
43
44 import base64
44 import base64
45 import contextlib
45 import contextlib
46 import hashlib
46 import hashlib
47 import itertools
47 import itertools
48 import json
48 import json
49 import mimetypes
49 import mimetypes
50 import operator
50 import operator
51 import re
51 import re
52
52
53 from mercurial.node import bin, nullid
53 from mercurial.node import bin, nullid
54 from mercurial.i18n import _
54 from mercurial.i18n import _
55 from mercurial.pycompat import getattr
55 from mercurial.pycompat import getattr
56 from mercurial.thirdparty import attr
56 from mercurial.thirdparty import attr
57 from mercurial import (
57 from mercurial import (
58 cmdutil,
58 cmdutil,
59 context,
59 context,
60 encoding,
60 encoding,
61 error,
61 error,
62 exthelper,
62 exthelper,
63 httpconnection as httpconnectionmod,
63 httpconnection as httpconnectionmod,
64 match,
64 match,
65 mdiff,
65 mdiff,
66 obsutil,
66 obsutil,
67 parser,
67 parser,
68 patch,
68 patch,
69 phases,
69 phases,
70 pycompat,
70 pycompat,
71 scmutil,
71 scmutil,
72 smartset,
72 smartset,
73 tags,
73 tags,
74 templatefilters,
74 templatefilters,
75 templateutil,
75 templateutil,
76 url as urlmod,
76 url as urlmod,
77 util,
77 util,
78 )
78 )
79 from mercurial.utils import (
79 from mercurial.utils import (
80 procutil,
80 procutil,
81 stringutil,
81 stringutil,
82 )
82 )
83
83
84 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
84 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
85 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
85 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
86 # be specifying the version(s) of Mercurial they are tested with, or
86 # be specifying the version(s) of Mercurial they are tested with, or
87 # leave the attribute unspecified.
87 # leave the attribute unspecified.
88 testedwith = b'ships-with-hg-core'
88 testedwith = b'ships-with-hg-core'
89
89
90 eh = exthelper.exthelper()
90 eh = exthelper.exthelper()
91
91
92 cmdtable = eh.cmdtable
92 cmdtable = eh.cmdtable
93 command = eh.command
93 command = eh.command
94 configtable = eh.configtable
94 configtable = eh.configtable
95 templatekeyword = eh.templatekeyword
95 templatekeyword = eh.templatekeyword
96
96
97 # developer config: phabricator.batchsize
97 # developer config: phabricator.batchsize
98 eh.configitem(
98 eh.configitem(
99 b'phabricator', b'batchsize', default=12,
99 b'phabricator', b'batchsize', default=12,
100 )
100 )
101 eh.configitem(
101 eh.configitem(
102 b'phabricator', b'callsign', default=None,
102 b'phabricator', b'callsign', default=None,
103 )
103 )
104 eh.configitem(
104 eh.configitem(
105 b'phabricator', b'curlcmd', default=None,
105 b'phabricator', b'curlcmd', default=None,
106 )
106 )
107 # developer config: phabricator.repophid
107 # developer config: phabricator.repophid
108 eh.configitem(
108 eh.configitem(
109 b'phabricator', b'repophid', default=None,
109 b'phabricator', b'repophid', default=None,
110 )
110 )
111 eh.configitem(
111 eh.configitem(
112 b'phabricator', b'url', default=None,
112 b'phabricator', b'url', default=None,
113 )
113 )
114 eh.configitem(
114 eh.configitem(
115 b'phabsend', b'confirm', default=False,
115 b'phabsend', b'confirm', default=False,
116 )
116 )
117
117
118 colortable = {
118 colortable = {
119 b'phabricator.action.created': b'green',
119 b'phabricator.action.created': b'green',
120 b'phabricator.action.skipped': b'magenta',
120 b'phabricator.action.skipped': b'magenta',
121 b'phabricator.action.updated': b'magenta',
121 b'phabricator.action.updated': b'magenta',
122 b'phabricator.desc': b'',
122 b'phabricator.desc': b'',
123 b'phabricator.drev': b'bold',
123 b'phabricator.drev': b'bold',
124 b'phabricator.node': b'',
124 b'phabricator.node': b'',
125 }
125 }
126
126
127 _VCR_FLAGS = [
127 _VCR_FLAGS = [
128 (
128 (
129 b'',
129 b'',
130 b'test-vcr',
130 b'test-vcr',
131 b'',
131 b'',
132 _(
132 _(
133 b'Path to a vcr file. If nonexistent, will record a new vcr transcript'
133 b'Path to a vcr file. If nonexistent, will record a new vcr transcript'
134 b', otherwise will mock all http requests using the specified vcr file.'
134 b', otherwise will mock all http requests using the specified vcr file.'
135 b' (ADVANCED)'
135 b' (ADVANCED)'
136 ),
136 ),
137 ),
137 ),
138 ]
138 ]
139
139
140
140
141 def vcrcommand(name, flags, spec, helpcategory=None, optionalrepo=False):
141 def vcrcommand(name, flags, spec, helpcategory=None, optionalrepo=False):
142 fullflags = flags + _VCR_FLAGS
142 fullflags = flags + _VCR_FLAGS
143
143
144 def hgmatcher(r1, r2):
144 def hgmatcher(r1, r2):
145 if r1.uri != r2.uri or r1.method != r2.method:
145 if r1.uri != r2.uri or r1.method != r2.method:
146 return False
146 return False
147 r1params = r1.body.split(b'&')
147 r1params = r1.body.split(b'&')
148 r2params = r2.body.split(b'&')
148 r2params = r2.body.split(b'&')
149 return set(r1params) == set(r2params)
149 return set(r1params) == set(r2params)
150
150
151 def sanitiserequest(request):
151 def sanitiserequest(request):
152 request.body = re.sub(
152 request.body = re.sub(
153 br'cli-[a-z0-9]+', br'cli-hahayouwish', request.body
153 br'cli-[a-z0-9]+', br'cli-hahayouwish', request.body
154 )
154 )
155 return request
155 return request
156
156
157 def sanitiseresponse(response):
157 def sanitiseresponse(response):
158 if r'set-cookie' in response[r'headers']:
158 if r'set-cookie' in response[r'headers']:
159 del response[r'headers'][r'set-cookie']
159 del response[r'headers'][r'set-cookie']
160 return response
160 return response
161
161
162 def decorate(fn):
162 def decorate(fn):
163 def inner(*args, **kwargs):
163 def inner(*args, **kwargs):
164 cassette = pycompat.fsdecode(kwargs.pop(r'test_vcr', None))
164 cassette = pycompat.fsdecode(kwargs.pop(r'test_vcr', None))
165 if cassette:
165 if cassette:
166 import hgdemandimport
166 import hgdemandimport
167
167
168 with hgdemandimport.deactivated():
168 with hgdemandimport.deactivated():
169 import vcr as vcrmod
169 import vcr as vcrmod
170 import vcr.stubs as stubs
170 import vcr.stubs as stubs
171
171
172 vcr = vcrmod.VCR(
172 vcr = vcrmod.VCR(
173 serializer=r'json',
173 serializer=r'json',
174 before_record_request=sanitiserequest,
174 before_record_request=sanitiserequest,
175 before_record_response=sanitiseresponse,
175 before_record_response=sanitiseresponse,
176 custom_patches=[
176 custom_patches=[
177 (
177 (
178 urlmod,
178 urlmod,
179 r'httpconnection',
179 r'httpconnection',
180 stubs.VCRHTTPConnection,
180 stubs.VCRHTTPConnection,
181 ),
181 ),
182 (
182 (
183 urlmod,
183 urlmod,
184 r'httpsconnection',
184 r'httpsconnection',
185 stubs.VCRHTTPSConnection,
185 stubs.VCRHTTPSConnection,
186 ),
186 ),
187 ],
187 ],
188 )
188 )
189 vcr.register_matcher(r'hgmatcher', hgmatcher)
189 vcr.register_matcher(r'hgmatcher', hgmatcher)
190 with vcr.use_cassette(cassette, match_on=[r'hgmatcher']):
190 with vcr.use_cassette(cassette, match_on=[r'hgmatcher']):
191 return fn(*args, **kwargs)
191 return fn(*args, **kwargs)
192 return fn(*args, **kwargs)
192 return fn(*args, **kwargs)
193
193
194 inner.__name__ = fn.__name__
194 inner.__name__ = fn.__name__
195 inner.__doc__ = fn.__doc__
195 inner.__doc__ = fn.__doc__
196 return command(
196 return command(
197 name,
197 name,
198 fullflags,
198 fullflags,
199 spec,
199 spec,
200 helpcategory=helpcategory,
200 helpcategory=helpcategory,
201 optionalrepo=optionalrepo,
201 optionalrepo=optionalrepo,
202 )(inner)
202 )(inner)
203
203
204 return decorate
204 return decorate
205
205
206
206
207 def urlencodenested(params):
207 def urlencodenested(params):
208 """like urlencode, but works with nested parameters.
208 """like urlencode, but works with nested parameters.
209
209
210 For example, if params is {'a': ['b', 'c'], 'd': {'e': 'f'}}, it will be
210 For example, if params is {'a': ['b', 'c'], 'd': {'e': 'f'}}, it will be
211 flattened to {'a[0]': 'b', 'a[1]': 'c', 'd[e]': 'f'} and then passed to
211 flattened to {'a[0]': 'b', 'a[1]': 'c', 'd[e]': 'f'} and then passed to
212 urlencode. Note: the encoding is consistent with PHP's http_build_query.
212 urlencode. Note: the encoding is consistent with PHP's http_build_query.
213 """
213 """
214 flatparams = util.sortdict()
214 flatparams = util.sortdict()
215
215
216 def process(prefix, obj):
216 def process(prefix, obj):
217 if isinstance(obj, bool):
217 if isinstance(obj, bool):
218 obj = {True: b'true', False: b'false'}[obj] # Python -> PHP form
218 obj = {True: b'true', False: b'false'}[obj] # Python -> PHP form
219 lister = lambda l: [(b'%d' % k, v) for k, v in enumerate(l)]
219 lister = lambda l: [(b'%d' % k, v) for k, v in enumerate(l)]
220 items = {list: lister, dict: lambda x: x.items()}.get(type(obj))
220 items = {list: lister, dict: lambda x: x.items()}.get(type(obj))
221 if items is None:
221 if items is None:
222 flatparams[prefix] = obj
222 flatparams[prefix] = obj
223 else:
223 else:
224 for k, v in items(obj):
224 for k, v in items(obj):
225 if prefix:
225 if prefix:
226 process(b'%s[%s]' % (prefix, k), v)
226 process(b'%s[%s]' % (prefix, k), v)
227 else:
227 else:
228 process(k, v)
228 process(k, v)
229
229
230 process(b'', params)
230 process(b'', params)
231 return util.urlreq.urlencode(flatparams)
231 return util.urlreq.urlencode(flatparams)
232
232
233
233
234 def readurltoken(ui):
234 def readurltoken(ui):
235 """return conduit url, token and make sure they exist
235 """return conduit url, token and make sure they exist
236
236
237 Currently read from [auth] config section. In the future, it might
237 Currently read from [auth] config section. In the future, it might
238 make sense to read from .arcconfig and .arcrc as well.
238 make sense to read from .arcconfig and .arcrc as well.
239 """
239 """
240 url = ui.config(b'phabricator', b'url')
240 url = ui.config(b'phabricator', b'url')
241 if not url:
241 if not url:
242 raise error.Abort(
242 raise error.Abort(
243 _(b'config %s.%s is required') % (b'phabricator', b'url')
243 _(b'config %s.%s is required') % (b'phabricator', b'url')
244 )
244 )
245
245
246 res = httpconnectionmod.readauthforuri(ui, url, util.url(url).user)
246 res = httpconnectionmod.readauthforuri(ui, url, util.url(url).user)
247 token = None
247 token = None
248
248
249 if res:
249 if res:
250 group, auth = res
250 group, auth = res
251
251
252 ui.debug(b"using auth.%s.* for authentication\n" % group)
252 ui.debug(b"using auth.%s.* for authentication\n" % group)
253
253
254 token = auth.get(b'phabtoken')
254 token = auth.get(b'phabtoken')
255
255
256 if not token:
256 if not token:
257 raise error.Abort(
257 raise error.Abort(
258 _(b'Can\'t find conduit token associated to %s') % (url,)
258 _(b'Can\'t find conduit token associated to %s') % (url,)
259 )
259 )
260
260
261 return url, token
261 return url, token
262
262
263
263
264 def callconduit(ui, name, params):
264 def callconduit(ui, name, params):
265 """call Conduit API, params is a dict. return json.loads result, or None"""
265 """call Conduit API, params is a dict. return json.loads result, or None"""
266 host, token = readurltoken(ui)
266 host, token = readurltoken(ui)
267 url, authinfo = util.url(b'/'.join([host, b'api', name])).authinfo()
267 url, authinfo = util.url(b'/'.join([host, b'api', name])).authinfo()
268 ui.debug(b'Conduit Call: %s %s\n' % (url, pycompat.byterepr(params)))
268 ui.debug(b'Conduit Call: %s %s\n' % (url, pycompat.byterepr(params)))
269 params = params.copy()
269 params = params.copy()
270 params[b'api.token'] = token
270 params[b'api.token'] = token
271 data = urlencodenested(params)
271 data = urlencodenested(params)
272 curlcmd = ui.config(b'phabricator', b'curlcmd')
272 curlcmd = ui.config(b'phabricator', b'curlcmd')
273 if curlcmd:
273 if curlcmd:
274 sin, sout = procutil.popen2(
274 sin, sout = procutil.popen2(
275 b'%s -d @- %s' % (curlcmd, procutil.shellquote(url))
275 b'%s -d @- %s' % (curlcmd, procutil.shellquote(url))
276 )
276 )
277 sin.write(data)
277 sin.write(data)
278 sin.close()
278 sin.close()
279 body = sout.read()
279 body = sout.read()
280 else:
280 else:
281 urlopener = urlmod.opener(ui, authinfo)
281 urlopener = urlmod.opener(ui, authinfo)
282 request = util.urlreq.request(pycompat.strurl(url), data=data)
282 request = util.urlreq.request(pycompat.strurl(url), data=data)
283 with contextlib.closing(urlopener.open(request)) as rsp:
283 with contextlib.closing(urlopener.open(request)) as rsp:
284 body = rsp.read()
284 body = rsp.read()
285 ui.debug(b'Conduit Response: %s\n' % body)
285 ui.debug(b'Conduit Response: %s\n' % body)
286 parsed = pycompat.rapply(
286 parsed = pycompat.rapply(
287 lambda x: encoding.unitolocal(x)
287 lambda x: encoding.unitolocal(x)
288 if isinstance(x, pycompat.unicode)
288 if isinstance(x, pycompat.unicode)
289 else x,
289 else x,
290 # json.loads only accepts bytes from py3.6+
290 # json.loads only accepts bytes from py3.6+
291 json.loads(encoding.unifromlocal(body)),
291 json.loads(encoding.unifromlocal(body)),
292 )
292 )
293 if parsed.get(b'error_code'):
293 if parsed.get(b'error_code'):
294 msg = _(b'Conduit Error (%s): %s') % (
294 msg = _(b'Conduit Error (%s): %s') % (
295 parsed[b'error_code'],
295 parsed[b'error_code'],
296 parsed[b'error_info'],
296 parsed[b'error_info'],
297 )
297 )
298 raise error.Abort(msg)
298 raise error.Abort(msg)
299 return parsed[b'result']
299 return parsed[b'result']
300
300
301
301
302 @vcrcommand(b'debugcallconduit', [], _(b'METHOD'), optionalrepo=True)
302 @vcrcommand(b'debugcallconduit', [], _(b'METHOD'), optionalrepo=True)
303 def debugcallconduit(ui, repo, name):
303 def debugcallconduit(ui, repo, name):
304 """call Conduit API
304 """call Conduit API
305
305
306 Call parameters are read from stdin as a JSON blob. Result will be written
306 Call parameters are read from stdin as a JSON blob. Result will be written
307 to stdout as a JSON blob.
307 to stdout as a JSON blob.
308 """
308 """
309 # json.loads only accepts bytes from 3.6+
309 # json.loads only accepts bytes from 3.6+
310 rawparams = encoding.unifromlocal(ui.fin.read())
310 rawparams = encoding.unifromlocal(ui.fin.read())
311 # json.loads only returns unicode strings
311 # json.loads only returns unicode strings
312 params = pycompat.rapply(
312 params = pycompat.rapply(
313 lambda x: encoding.unitolocal(x)
313 lambda x: encoding.unitolocal(x)
314 if isinstance(x, pycompat.unicode)
314 if isinstance(x, pycompat.unicode)
315 else x,
315 else x,
316 json.loads(rawparams),
316 json.loads(rawparams),
317 )
317 )
318 # json.dumps only accepts unicode strings
318 # json.dumps only accepts unicode strings
319 result = pycompat.rapply(
319 result = pycompat.rapply(
320 lambda x: encoding.unifromlocal(x) if isinstance(x, bytes) else x,
320 lambda x: encoding.unifromlocal(x) if isinstance(x, bytes) else x,
321 callconduit(ui, name, params),
321 callconduit(ui, name, params),
322 )
322 )
323 s = json.dumps(result, sort_keys=True, indent=2, separators=(u',', u': '))
323 s = json.dumps(result, sort_keys=True, indent=2, separators=(u',', u': '))
324 ui.write(b'%s\n' % encoding.unitolocal(s))
324 ui.write(b'%s\n' % encoding.unitolocal(s))
325
325
326
326
327 def getrepophid(repo):
327 def getrepophid(repo):
328 """given callsign, return repository PHID or None"""
328 """given callsign, return repository PHID or None"""
329 # developer config: phabricator.repophid
329 # developer config: phabricator.repophid
330 repophid = repo.ui.config(b'phabricator', b'repophid')
330 repophid = repo.ui.config(b'phabricator', b'repophid')
331 if repophid:
331 if repophid:
332 return repophid
332 return repophid
333 callsign = repo.ui.config(b'phabricator', b'callsign')
333 callsign = repo.ui.config(b'phabricator', b'callsign')
334 if not callsign:
334 if not callsign:
335 return None
335 return None
336 query = callconduit(
336 query = callconduit(
337 repo.ui,
337 repo.ui,
338 b'diffusion.repository.search',
338 b'diffusion.repository.search',
339 {b'constraints': {b'callsigns': [callsign]}},
339 {b'constraints': {b'callsigns': [callsign]}},
340 )
340 )
341 if len(query[b'data']) == 0:
341 if len(query[b'data']) == 0:
342 return None
342 return None
343 repophid = query[b'data'][0][b'phid']
343 repophid = query[b'data'][0][b'phid']
344 repo.ui.setconfig(b'phabricator', b'repophid', repophid)
344 repo.ui.setconfig(b'phabricator', b'repophid', repophid)
345 return repophid
345 return repophid
346
346
347
347
348 _differentialrevisiontagre = re.compile(br'\AD([1-9][0-9]*)\Z')
348 _differentialrevisiontagre = re.compile(br'\AD([1-9][0-9]*)\Z')
349 _differentialrevisiondescre = re.compile(
349 _differentialrevisiondescre = re.compile(
350 br'^Differential Revision:\s*(?P<url>(?:.*)D(?P<id>[1-9][0-9]*))$', re.M
350 br'^Differential Revision:\s*(?P<url>(?:.*)D(?P<id>[1-9][0-9]*))$', re.M
351 )
351 )
352
352
353
353
354 def getoldnodedrevmap(repo, nodelist):
354 def getoldnodedrevmap(repo, nodelist):
355 """find previous nodes that has been sent to Phabricator
355 """find previous nodes that has been sent to Phabricator
356
356
357 return {node: (oldnode, Differential diff, Differential Revision ID)}
357 return {node: (oldnode, Differential diff, Differential Revision ID)}
358 for node in nodelist with known previous sent versions, or associated
358 for node in nodelist with known previous sent versions, or associated
359 Differential Revision IDs. ``oldnode`` and ``Differential diff`` could
359 Differential Revision IDs. ``oldnode`` and ``Differential diff`` could
360 be ``None``.
360 be ``None``.
361
361
362 Examines commit messages like "Differential Revision:" to get the
362 Examines commit messages like "Differential Revision:" to get the
363 association information.
363 association information.
364
364
365 If such commit message line is not found, examines all precursors and their
365 If such commit message line is not found, examines all precursors and their
366 tags. Tags with format like "D1234" are considered a match and the node
366 tags. Tags with format like "D1234" are considered a match and the node
367 with that tag, and the number after "D" (ex. 1234) will be returned.
367 with that tag, and the number after "D" (ex. 1234) will be returned.
368
368
369 The ``old node``, if not None, is guaranteed to be the last diff of
369 The ``old node``, if not None, is guaranteed to be the last diff of
370 corresponding Differential Revision, and exist in the repo.
370 corresponding Differential Revision, and exist in the repo.
371 """
371 """
372 unfi = repo.unfiltered()
372 unfi = repo.unfiltered()
373 nodemap = unfi.changelog.nodemap
373 nodemap = unfi.changelog.nodemap
374
374
375 result = {} # {node: (oldnode?, lastdiff?, drev)}
375 result = {} # {node: (oldnode?, lastdiff?, drev)}
376 toconfirm = {} # {node: (force, {precnode}, drev)}
376 toconfirm = {} # {node: (force, {precnode}, drev)}
377 for node in nodelist:
377 for node in nodelist:
378 ctx = unfi[node]
378 ctx = unfi[node]
379 # For tags like "D123", put them into "toconfirm" to verify later
379 # For tags like "D123", put them into "toconfirm" to verify later
380 precnodes = list(obsutil.allpredecessors(unfi.obsstore, [node]))
380 precnodes = list(obsutil.allpredecessors(unfi.obsstore, [node]))
381 for n in precnodes:
381 for n in precnodes:
382 if n in nodemap:
382 if n in nodemap:
383 for tag in unfi.nodetags(n):
383 for tag in unfi.nodetags(n):
384 m = _differentialrevisiontagre.match(tag)
384 m = _differentialrevisiontagre.match(tag)
385 if m:
385 if m:
386 toconfirm[node] = (0, set(precnodes), int(m.group(1)))
386 toconfirm[node] = (0, set(precnodes), int(m.group(1)))
387 continue
387 continue
388
388
389 # Check commit message
389 # Check commit message
390 m = _differentialrevisiondescre.search(ctx.description())
390 m = _differentialrevisiondescre.search(ctx.description())
391 if m:
391 if m:
392 toconfirm[node] = (1, set(precnodes), int(m.group(r'id')))
392 toconfirm[node] = (1, set(precnodes), int(m.group(r'id')))
393
393
394 # Double check if tags are genuine by collecting all old nodes from
394 # Double check if tags are genuine by collecting all old nodes from
395 # Phabricator, and expect precursors overlap with it.
395 # Phabricator, and expect precursors overlap with it.
396 if toconfirm:
396 if toconfirm:
397 drevs = [drev for force, precs, drev in toconfirm.values()]
397 drevs = [drev for force, precs, drev in toconfirm.values()]
398 alldiffs = callconduit(
398 alldiffs = callconduit(
399 unfi.ui, b'differential.querydiffs', {b'revisionIDs': drevs}
399 unfi.ui, b'differential.querydiffs', {b'revisionIDs': drevs}
400 )
400 )
401 getnode = lambda d: bin(getdiffmeta(d).get(b'node', b'')) or None
401 getnode = lambda d: bin(getdiffmeta(d).get(b'node', b'')) or None
402 for newnode, (force, precset, drev) in toconfirm.items():
402 for newnode, (force, precset, drev) in toconfirm.items():
403 diffs = [
403 diffs = [
404 d for d in alldiffs.values() if int(d[b'revisionID']) == drev
404 d for d in alldiffs.values() if int(d[b'revisionID']) == drev
405 ]
405 ]
406
406
407 # "precursors" as known by Phabricator
407 # "precursors" as known by Phabricator
408 phprecset = set(getnode(d) for d in diffs)
408 phprecset = set(getnode(d) for d in diffs)
409
409
410 # Ignore if precursors (Phabricator and local repo) do not overlap,
410 # Ignore if precursors (Phabricator and local repo) do not overlap,
411 # and force is not set (when commit message says nothing)
411 # and force is not set (when commit message says nothing)
412 if not force and not bool(phprecset & precset):
412 if not force and not bool(phprecset & precset):
413 tagname = b'D%d' % drev
413 tagname = b'D%d' % drev
414 tags.tag(
414 tags.tag(
415 repo,
415 repo,
416 tagname,
416 tagname,
417 nullid,
417 nullid,
418 message=None,
418 message=None,
419 user=None,
419 user=None,
420 date=None,
420 date=None,
421 local=True,
421 local=True,
422 )
422 )
423 unfi.ui.warn(
423 unfi.ui.warn(
424 _(
424 _(
425 b'D%s: local tag removed - does not match '
425 b'D%s: local tag removed - does not match '
426 b'Differential history\n'
426 b'Differential history\n'
427 )
427 )
428 % drev
428 % drev
429 )
429 )
430 continue
430 continue
431
431
432 # Find the last node using Phabricator metadata, and make sure it
432 # Find the last node using Phabricator metadata, and make sure it
433 # exists in the repo
433 # exists in the repo
434 oldnode = lastdiff = None
434 oldnode = lastdiff = None
435 if diffs:
435 if diffs:
436 lastdiff = max(diffs, key=lambda d: int(d[b'id']))
436 lastdiff = max(diffs, key=lambda d: int(d[b'id']))
437 oldnode = getnode(lastdiff)
437 oldnode = getnode(lastdiff)
438 if oldnode and oldnode not in nodemap:
438 if oldnode and oldnode not in nodemap:
439 oldnode = None
439 oldnode = None
440
440
441 result[newnode] = (oldnode, lastdiff, drev)
441 result[newnode] = (oldnode, lastdiff, drev)
442
442
443 return result
443 return result
444
444
445
445
446 def getdiff(ctx, diffopts):
446 def getdiff(ctx, diffopts):
447 """plain-text diff without header (user, commit message, etc)"""
447 """plain-text diff without header (user, commit message, etc)"""
448 output = util.stringio()
448 output = util.stringio()
449 for chunk, _label in patch.diffui(
449 for chunk, _label in patch.diffui(
450 ctx.repo(), ctx.p1().node(), ctx.node(), None, opts=diffopts
450 ctx.repo(), ctx.p1().node(), ctx.node(), None, opts=diffopts
451 ):
451 ):
452 output.write(chunk)
452 output.write(chunk)
453 return output.getvalue()
453 return output.getvalue()
454
454
455
455
456 class DiffChangeType(object):
456 class DiffChangeType(object):
457 ADD = 1
457 ADD = 1
458 CHANGE = 2
458 CHANGE = 2
459 DELETE = 3
459 DELETE = 3
460 MOVE_AWAY = 4
460 MOVE_AWAY = 4
461 COPY_AWAY = 5
461 COPY_AWAY = 5
462 MOVE_HERE = 6
462 MOVE_HERE = 6
463 COPY_HERE = 7
463 COPY_HERE = 7
464 MULTICOPY = 8
464 MULTICOPY = 8
465
465
466
466
467 class DiffFileType(object):
467 class DiffFileType(object):
468 TEXT = 1
468 TEXT = 1
469 IMAGE = 2
469 IMAGE = 2
470 BINARY = 3
470 BINARY = 3
471
471
472
472
473 @attr.s
473 @attr.s
474 class phabhunk(dict):
474 class phabhunk(dict):
475 """Represents a Differential hunk, which is owned by a Differential change
475 """Represents a Differential hunk, which is owned by a Differential change
476 """
476 """
477
477
478 oldOffset = attr.ib(default=0) # camelcase-required
478 oldOffset = attr.ib(default=0) # camelcase-required
479 oldLength = attr.ib(default=0) # camelcase-required
479 oldLength = attr.ib(default=0) # camelcase-required
480 newOffset = attr.ib(default=0) # camelcase-required
480 newOffset = attr.ib(default=0) # camelcase-required
481 newLength = attr.ib(default=0) # camelcase-required
481 newLength = attr.ib(default=0) # camelcase-required
482 corpus = attr.ib(default='')
482 corpus = attr.ib(default='')
483 # These get added to the phabchange's equivalents
483 # These get added to the phabchange's equivalents
484 addLines = attr.ib(default=0) # camelcase-required
484 addLines = attr.ib(default=0) # camelcase-required
485 delLines = attr.ib(default=0) # camelcase-required
485 delLines = attr.ib(default=0) # camelcase-required
486
486
487
487
488 @attr.s
488 @attr.s
489 class phabchange(object):
489 class phabchange(object):
490 """Represents a Differential change, owns Differential hunks and owned by a
490 """Represents a Differential change, owns Differential hunks and owned by a
491 Differential diff. Each one represents one file in a diff.
491 Differential diff. Each one represents one file in a diff.
492 """
492 """
493
493
494 currentPath = attr.ib(default=None) # camelcase-required
494 currentPath = attr.ib(default=None) # camelcase-required
495 oldPath = attr.ib(default=None) # camelcase-required
495 oldPath = attr.ib(default=None) # camelcase-required
496 awayPaths = attr.ib(default=attr.Factory(list)) # camelcase-required
496 awayPaths = attr.ib(default=attr.Factory(list)) # camelcase-required
497 metadata = attr.ib(default=attr.Factory(dict))
497 metadata = attr.ib(default=attr.Factory(dict))
498 oldProperties = attr.ib(default=attr.Factory(dict)) # camelcase-required
498 oldProperties = attr.ib(default=attr.Factory(dict)) # camelcase-required
499 newProperties = attr.ib(default=attr.Factory(dict)) # camelcase-required
499 newProperties = attr.ib(default=attr.Factory(dict)) # camelcase-required
500 type = attr.ib(default=DiffChangeType.CHANGE)
500 type = attr.ib(default=DiffChangeType.CHANGE)
501 fileType = attr.ib(default=DiffFileType.TEXT) # camelcase-required
501 fileType = attr.ib(default=DiffFileType.TEXT) # camelcase-required
502 commitHash = attr.ib(default=None) # camelcase-required
502 commitHash = attr.ib(default=None) # camelcase-required
503 addLines = attr.ib(default=0) # camelcase-required
503 addLines = attr.ib(default=0) # camelcase-required
504 delLines = attr.ib(default=0) # camelcase-required
504 delLines = attr.ib(default=0) # camelcase-required
505 hunks = attr.ib(default=attr.Factory(list))
505 hunks = attr.ib(default=attr.Factory(list))
506
506
507 def copynewmetadatatoold(self):
507 def copynewmetadatatoold(self):
508 for key in list(self.metadata.keys()):
508 for key in list(self.metadata.keys()):
509 newkey = key.replace(b'new:', b'old:')
509 newkey = key.replace(b'new:', b'old:')
510 self.metadata[newkey] = self.metadata[key]
510 self.metadata[newkey] = self.metadata[key]
511
511
512 def addoldmode(self, value):
512 def addoldmode(self, value):
513 self.oldProperties[b'unix:filemode'] = value
513 self.oldProperties[b'unix:filemode'] = value
514
514
515 def addnewmode(self, value):
515 def addnewmode(self, value):
516 self.newProperties[b'unix:filemode'] = value
516 self.newProperties[b'unix:filemode'] = value
517
517
518 def addhunk(self, hunk):
518 def addhunk(self, hunk):
519 if not isinstance(hunk, phabhunk):
519 if not isinstance(hunk, phabhunk):
520 raise error.Abort(b'phabchange.addhunk only takes phabhunks')
520 raise error.Abort(b'phabchange.addhunk only takes phabhunks')
521 self.hunks.append(hunk)
521 self.hunks.append(hunk)
522 # It's useful to include these stats since the Phab web UI shows them,
522 # It's useful to include these stats since the Phab web UI shows them,
523 # and uses them to estimate how large a change a Revision is. Also used
523 # and uses them to estimate how large a change a Revision is. Also used
524 # in email subjects for the [+++--] bit.
524 # in email subjects for the [+++--] bit.
525 self.addLines += hunk.addLines
525 self.addLines += hunk.addLines
526 self.delLines += hunk.delLines
526 self.delLines += hunk.delLines
527
527
528
528
529 @attr.s
529 @attr.s
530 class phabdiff(object):
530 class phabdiff(object):
531 """Represents a Differential diff, owns Differential changes. Corresponds
531 """Represents a Differential diff, owns Differential changes. Corresponds
532 to a commit.
532 to a commit.
533 """
533 """
534
534
535 # Doesn't seem to be any reason to send this (output of uname -n)
535 # Doesn't seem to be any reason to send this (output of uname -n)
536 sourceMachine = attr.ib(default=b'') # camelcase-required
536 sourceMachine = attr.ib(default=b'') # camelcase-required
537 sourcePath = attr.ib(default=b'/') # camelcase-required
537 sourcePath = attr.ib(default=b'/') # camelcase-required
538 sourceControlBaseRevision = attr.ib(default=b'0' * 40) # camelcase-required
538 sourceControlBaseRevision = attr.ib(default=b'0' * 40) # camelcase-required
539 sourceControlPath = attr.ib(default=b'/') # camelcase-required
539 sourceControlPath = attr.ib(default=b'/') # camelcase-required
540 sourceControlSystem = attr.ib(default=b'hg') # camelcase-required
540 sourceControlSystem = attr.ib(default=b'hg') # camelcase-required
541 branch = attr.ib(default=b'default')
541 branch = attr.ib(default=b'default')
542 bookmark = attr.ib(default=None)
542 bookmark = attr.ib(default=None)
543 creationMethod = attr.ib(default=b'phabsend') # camelcase-required
543 creationMethod = attr.ib(default=b'phabsend') # camelcase-required
544 lintStatus = attr.ib(default=b'none') # camelcase-required
544 lintStatus = attr.ib(default=b'none') # camelcase-required
545 unitStatus = attr.ib(default=b'none') # camelcase-required
545 unitStatus = attr.ib(default=b'none') # camelcase-required
546 changes = attr.ib(default=attr.Factory(dict))
546 changes = attr.ib(default=attr.Factory(dict))
547 repositoryPHID = attr.ib(default=None) # camelcase-required
547 repositoryPHID = attr.ib(default=None) # camelcase-required
548
548
549 def addchange(self, change):
549 def addchange(self, change):
550 if not isinstance(change, phabchange):
550 if not isinstance(change, phabchange):
551 raise error.Abort(b'phabdiff.addchange only takes phabchanges')
551 raise error.Abort(b'phabdiff.addchange only takes phabchanges')
552 self.changes[change.currentPath] = change
552 self.changes[change.currentPath] = change
553
553
554
554
555 def maketext(pchange, ctx, fname):
555 def maketext(pchange, ctx, fname):
556 """populate the phabchange for a text file"""
556 """populate the phabchange for a text file"""
557 repo = ctx.repo()
557 repo = ctx.repo()
558 fmatcher = match.exact([fname])
558 fmatcher = match.exact([fname])
559 diffopts = mdiff.diffopts(git=True, context=32767)
559 diffopts = mdiff.diffopts(git=True, context=32767)
560 _pfctx, _fctx, header, fhunks = next(
560 _pfctx, _fctx, header, fhunks = next(
561 patch.diffhunks(repo, ctx.p1(), ctx, fmatcher, opts=diffopts)
561 patch.diffhunks(repo, ctx.p1(), ctx, fmatcher, opts=diffopts)
562 )
562 )
563
563
564 for fhunk in fhunks:
564 for fhunk in fhunks:
565 (oldOffset, oldLength, newOffset, newLength), lines = fhunk
565 (oldOffset, oldLength, newOffset, newLength), lines = fhunk
566 corpus = b''.join(lines[1:])
566 corpus = b''.join(lines[1:])
567 shunk = list(header)
567 shunk = list(header)
568 shunk.extend(lines)
568 shunk.extend(lines)
569 _mf, _mt, addLines, delLines, _hb = patch.diffstatsum(
569 _mf, _mt, addLines, delLines, _hb = patch.diffstatsum(
570 patch.diffstatdata(util.iterlines(shunk))
570 patch.diffstatdata(util.iterlines(shunk))
571 )
571 )
572 pchange.addhunk(
572 pchange.addhunk(
573 phabhunk(
573 phabhunk(
574 oldOffset,
574 oldOffset,
575 oldLength,
575 oldLength,
576 newOffset,
576 newOffset,
577 newLength,
577 newLength,
578 corpus,
578 corpus,
579 addLines,
579 addLines,
580 delLines,
580 delLines,
581 )
581 )
582 )
582 )
583
583
584
584
585 def uploadchunks(fctx, fphid):
585 def uploadchunks(fctx, fphid):
586 """upload large binary files as separate chunks.
586 """upload large binary files as separate chunks.
587 Phab requests chunking over 8MiB, and splits into 4MiB chunks
587 Phab requests chunking over 8MiB, and splits into 4MiB chunks
588 """
588 """
589 ui = fctx.repo().ui
589 ui = fctx.repo().ui
590 chunks = callconduit(ui, b'file.querychunks', {b'filePHID': fphid})
590 chunks = callconduit(ui, b'file.querychunks', {b'filePHID': fphid})
591 progress = ui.makeprogress(
591 progress = ui.makeprogress(
592 _(b'uploading file chunks'), unit=_(b'chunks'), total=len(chunks)
592 _(b'uploading file chunks'), unit=_(b'chunks'), total=len(chunks)
593 )
593 )
594 for chunk in chunks:
594 for chunk in chunks:
595 progress.increment()
595 progress.increment()
596 if chunk[b'complete']:
596 if chunk[b'complete']:
597 continue
597 continue
598 bstart = int(chunk[b'byteStart'])
598 bstart = int(chunk[b'byteStart'])
599 bend = int(chunk[b'byteEnd'])
599 bend = int(chunk[b'byteEnd'])
600 callconduit(
600 callconduit(
601 ui,
601 ui,
602 b'file.uploadchunk',
602 b'file.uploadchunk',
603 {
603 {
604 b'filePHID': fphid,
604 b'filePHID': fphid,
605 b'byteStart': bstart,
605 b'byteStart': bstart,
606 b'data': base64.b64encode(fctx.data()[bstart:bend]),
606 b'data': base64.b64encode(fctx.data()[bstart:bend]),
607 b'dataEncoding': b'base64',
607 b'dataEncoding': b'base64',
608 },
608 },
609 )
609 )
610 progress.complete()
610 progress.complete()
611
611
612
612
613 def uploadfile(fctx):
613 def uploadfile(fctx):
614 """upload binary files to Phabricator"""
614 """upload binary files to Phabricator"""
615 repo = fctx.repo()
615 repo = fctx.repo()
616 ui = repo.ui
616 ui = repo.ui
617 fname = fctx.path()
617 fname = fctx.path()
618 size = fctx.size()
618 size = fctx.size()
619 fhash = pycompat.bytestr(hashlib.sha256(fctx.data()).hexdigest())
619 fhash = pycompat.bytestr(hashlib.sha256(fctx.data()).hexdigest())
620
620
621 # an allocate call is required first to see if an upload is even required
621 # an allocate call is required first to see if an upload is even required
622 # (Phab might already have it) and to determine if chunking is needed
622 # (Phab might already have it) and to determine if chunking is needed
623 allocateparams = {
623 allocateparams = {
624 b'name': fname,
624 b'name': fname,
625 b'contentLength': size,
625 b'contentLength': size,
626 b'contentHash': fhash,
626 b'contentHash': fhash,
627 }
627 }
628 filealloc = callconduit(ui, b'file.allocate', allocateparams)
628 filealloc = callconduit(ui, b'file.allocate', allocateparams)
629 fphid = filealloc[b'filePHID']
629 fphid = filealloc[b'filePHID']
630
630
631 if filealloc[b'upload']:
631 if filealloc[b'upload']:
632 ui.write(_(b'uploading %s\n') % bytes(fctx))
632 ui.write(_(b'uploading %s\n') % bytes(fctx))
633 if not fphid:
633 if not fphid:
634 uploadparams = {
634 uploadparams = {
635 b'name': fname,
635 b'name': fname,
636 b'data_base64': base64.b64encode(fctx.data()),
636 b'data_base64': base64.b64encode(fctx.data()),
637 }
637 }
638 fphid = callconduit(ui, b'file.upload', uploadparams)
638 fphid = callconduit(ui, b'file.upload', uploadparams)
639 else:
639 else:
640 uploadchunks(fctx, fphid)
640 uploadchunks(fctx, fphid)
641 else:
641 else:
642 ui.debug(b'server already has %s\n' % bytes(fctx))
642 ui.debug(b'server already has %s\n' % bytes(fctx))
643
643
644 if not fphid:
644 if not fphid:
645 raise error.Abort(b'Upload of %s failed.' % bytes(fctx))
645 raise error.Abort(b'Upload of %s failed.' % bytes(fctx))
646
646
647 return fphid
647 return fphid
648
648
649
649
650 def addoldbinary(pchange, fctx, originalfname):
650 def addoldbinary(pchange, fctx, originalfname):
651 """add the metadata for the previous version of a binary file to the
651 """add the metadata for the previous version of a binary file to the
652 phabchange for the new version
652 phabchange for the new version
653 """
653 """
654 oldfctx = fctx.p1()[originalfname]
654 oldfctx = fctx.p1()[originalfname]
655 if fctx.cmp(oldfctx):
655 if fctx.cmp(oldfctx):
656 # Files differ, add the old one
656 # Files differ, add the old one
657 pchange.metadata[b'old:file:size'] = oldfctx.size()
657 pchange.metadata[b'old:file:size'] = oldfctx.size()
658 mimeguess, _enc = mimetypes.guess_type(
658 mimeguess, _enc = mimetypes.guess_type(
659 encoding.unifromlocal(oldfctx.path())
659 encoding.unifromlocal(oldfctx.path())
660 )
660 )
661 if mimeguess:
661 if mimeguess:
662 pchange.metadata[b'old:file:mime-type'] = pycompat.bytestr(
662 pchange.metadata[b'old:file:mime-type'] = pycompat.bytestr(
663 mimeguess
663 mimeguess
664 )
664 )
665 fphid = uploadfile(oldfctx)
665 fphid = uploadfile(oldfctx)
666 pchange.metadata[b'old:binary-phid'] = fphid
666 pchange.metadata[b'old:binary-phid'] = fphid
667 else:
667 else:
668 # If it's left as IMAGE/BINARY web UI might try to display it
668 # If it's left as IMAGE/BINARY web UI might try to display it
669 pchange.fileType = DiffFileType.TEXT
669 pchange.fileType = DiffFileType.TEXT
670 pchange.copynewmetadatatoold()
670 pchange.copynewmetadatatoold()
671
671
672
672
673 def makebinary(pchange, fctx):
673 def makebinary(pchange, fctx):
674 """populate the phabchange for a binary file"""
674 """populate the phabchange for a binary file"""
675 pchange.fileType = DiffFileType.BINARY
675 pchange.fileType = DiffFileType.BINARY
676 fphid = uploadfile(fctx)
676 fphid = uploadfile(fctx)
677 pchange.metadata[b'new:binary-phid'] = fphid
677 pchange.metadata[b'new:binary-phid'] = fphid
678 pchange.metadata[b'new:file:size'] = fctx.size()
678 pchange.metadata[b'new:file:size'] = fctx.size()
679 mimeguess, _enc = mimetypes.guess_type(encoding.unifromlocal(fctx.path()))
679 mimeguess, _enc = mimetypes.guess_type(encoding.unifromlocal(fctx.path()))
680 if mimeguess:
680 if mimeguess:
681 mimeguess = pycompat.bytestr(mimeguess)
681 mimeguess = pycompat.bytestr(mimeguess)
682 pchange.metadata[b'new:file:mime-type'] = mimeguess
682 pchange.metadata[b'new:file:mime-type'] = mimeguess
683 if mimeguess.startswith(b'image/'):
683 if mimeguess.startswith(b'image/'):
684 pchange.fileType = DiffFileType.IMAGE
684 pchange.fileType = DiffFileType.IMAGE
685
685
686
686
687 # Copied from mercurial/patch.py
688 gitmode = {b'l': b'120000', b'x': b'100755', b'': b'100644'}
689
690
691 def addremoved(pdiff, ctx, removed):
692 """add removed files to the phabdiff. Shouldn't include moves"""
693 for fname in removed:
694 pchange = phabchange(
695 currentPath=fname, oldPath=fname, type=DiffChangeType.DELETE
696 )
697 pchange.addoldmode(gitmode[ctx.p1()[fname].flags()])
698 fctx = ctx.p1()[fname]
699 if not fctx.isbinary():
700 maketext(pchange, ctx, fname)
701
702 pdiff.addchange(pchange)
703
704
705 def addmodified(pdiff, ctx, modified):
706 """add modified files to the phabdiff"""
707 for fname in modified:
708 fctx = ctx[fname]
709 pchange = phabchange(currentPath=fname, oldPath=fname)
710 filemode = gitmode[ctx[fname].flags()]
711 originalmode = gitmode[ctx.p1()[fname].flags()]
712 if filemode != originalmode:
713 pchange.addoldmode(originalmode)
714 pchange.addnewmode(filemode)
715
716 if fctx.isbinary():
717 makebinary(pchange, fctx)
718 addoldbinary(pchange, fctx, fname)
719 else:
720 maketext(pchange, ctx, fname)
721
722 pdiff.addchange(pchange)
723
724
687 def creatediff(ctx):
725 def creatediff(ctx):
688 """create a Differential Diff"""
726 """create a Differential Diff"""
689 repo = ctx.repo()
727 repo = ctx.repo()
690 repophid = getrepophid(repo)
728 repophid = getrepophid(repo)
691 # Create a "Differential Diff" via "differential.createrawdiff" API
729 # Create a "Differential Diff" via "differential.createrawdiff" API
692 params = {b'diff': getdiff(ctx, mdiff.diffopts(git=True, context=32767))}
730 params = {b'diff': getdiff(ctx, mdiff.diffopts(git=True, context=32767))}
693 if repophid:
731 if repophid:
694 params[b'repositoryPHID'] = repophid
732 params[b'repositoryPHID'] = repophid
695 diff = callconduit(repo.ui, b'differential.createrawdiff', params)
733 diff = callconduit(repo.ui, b'differential.createrawdiff', params)
696 if not diff:
734 if not diff:
697 raise error.Abort(_(b'cannot create diff for %s') % ctx)
735 raise error.Abort(_(b'cannot create diff for %s') % ctx)
698 return diff
736 return diff
699
737
700
738
701 def writediffproperties(ctx, diff):
739 def writediffproperties(ctx, diff):
702 """write metadata to diff so patches could be applied losslessly"""
740 """write metadata to diff so patches could be applied losslessly"""
703 params = {
741 params = {
704 b'diff_id': diff[b'id'],
742 b'diff_id': diff[b'id'],
705 b'name': b'hg:meta',
743 b'name': b'hg:meta',
706 b'data': templatefilters.json(
744 b'data': templatefilters.json(
707 {
745 {
708 b'user': ctx.user(),
746 b'user': ctx.user(),
709 b'date': b'%d %d' % ctx.date(),
747 b'date': b'%d %d' % ctx.date(),
710 b'branch': ctx.branch(),
748 b'branch': ctx.branch(),
711 b'node': ctx.hex(),
749 b'node': ctx.hex(),
712 b'parent': ctx.p1().hex(),
750 b'parent': ctx.p1().hex(),
713 }
751 }
714 ),
752 ),
715 }
753 }
716 callconduit(ctx.repo().ui, b'differential.setdiffproperty', params)
754 callconduit(ctx.repo().ui, b'differential.setdiffproperty', params)
717
755
718 params = {
756 params = {
719 b'diff_id': diff[b'id'],
757 b'diff_id': diff[b'id'],
720 b'name': b'local:commits',
758 b'name': b'local:commits',
721 b'data': templatefilters.json(
759 b'data': templatefilters.json(
722 {
760 {
723 ctx.hex(): {
761 ctx.hex(): {
724 b'author': stringutil.person(ctx.user()),
762 b'author': stringutil.person(ctx.user()),
725 b'authorEmail': stringutil.email(ctx.user()),
763 b'authorEmail': stringutil.email(ctx.user()),
726 b'time': int(ctx.date()[0]),
764 b'time': int(ctx.date()[0]),
727 b'commit': ctx.hex(),
765 b'commit': ctx.hex(),
728 b'parents': [ctx.p1().hex()],
766 b'parents': [ctx.p1().hex()],
729 b'branch': ctx.branch(),
767 b'branch': ctx.branch(),
730 },
768 },
731 }
769 }
732 ),
770 ),
733 }
771 }
734 callconduit(ctx.repo().ui, b'differential.setdiffproperty', params)
772 callconduit(ctx.repo().ui, b'differential.setdiffproperty', params)
735
773
736
774
737 def createdifferentialrevision(
775 def createdifferentialrevision(
738 ctx,
776 ctx,
739 revid=None,
777 revid=None,
740 parentrevphid=None,
778 parentrevphid=None,
741 oldnode=None,
779 oldnode=None,
742 olddiff=None,
780 olddiff=None,
743 actions=None,
781 actions=None,
744 comment=None,
782 comment=None,
745 ):
783 ):
746 """create or update a Differential Revision
784 """create or update a Differential Revision
747
785
748 If revid is None, create a new Differential Revision, otherwise update
786 If revid is None, create a new Differential Revision, otherwise update
749 revid. If parentrevphid is not None, set it as a dependency.
787 revid. If parentrevphid is not None, set it as a dependency.
750
788
751 If oldnode is not None, check if the patch content (without commit message
789 If oldnode is not None, check if the patch content (without commit message
752 and metadata) has changed before creating another diff.
790 and metadata) has changed before creating another diff.
753
791
754 If actions is not None, they will be appended to the transaction.
792 If actions is not None, they will be appended to the transaction.
755 """
793 """
756 repo = ctx.repo()
794 repo = ctx.repo()
757 if oldnode:
795 if oldnode:
758 diffopts = mdiff.diffopts(git=True, context=32767)
796 diffopts = mdiff.diffopts(git=True, context=32767)
759 oldctx = repo.unfiltered()[oldnode]
797 oldctx = repo.unfiltered()[oldnode]
760 neednewdiff = getdiff(ctx, diffopts) != getdiff(oldctx, diffopts)
798 neednewdiff = getdiff(ctx, diffopts) != getdiff(oldctx, diffopts)
761 else:
799 else:
762 neednewdiff = True
800 neednewdiff = True
763
801
764 transactions = []
802 transactions = []
765 if neednewdiff:
803 if neednewdiff:
766 diff = creatediff(ctx)
804 diff = creatediff(ctx)
767 transactions.append({b'type': b'update', b'value': diff[b'phid']})
805 transactions.append({b'type': b'update', b'value': diff[b'phid']})
768 if comment:
806 if comment:
769 transactions.append({b'type': b'comment', b'value': comment})
807 transactions.append({b'type': b'comment', b'value': comment})
770 else:
808 else:
771 # Even if we don't need to upload a new diff because the patch content
809 # Even if we don't need to upload a new diff because the patch content
772 # does not change. We might still need to update its metadata so
810 # does not change. We might still need to update its metadata so
773 # pushers could know the correct node metadata.
811 # pushers could know the correct node metadata.
774 assert olddiff
812 assert olddiff
775 diff = olddiff
813 diff = olddiff
776 writediffproperties(ctx, diff)
814 writediffproperties(ctx, diff)
777
815
778 # Set the parent Revision every time, so commit re-ordering is picked-up
816 # Set the parent Revision every time, so commit re-ordering is picked-up
779 if parentrevphid:
817 if parentrevphid:
780 transactions.append(
818 transactions.append(
781 {b'type': b'parents.set', b'value': [parentrevphid]}
819 {b'type': b'parents.set', b'value': [parentrevphid]}
782 )
820 )
783
821
784 if actions:
822 if actions:
785 transactions += actions
823 transactions += actions
786
824
787 # Parse commit message and update related fields.
825 # Parse commit message and update related fields.
788 desc = ctx.description()
826 desc = ctx.description()
789 info = callconduit(
827 info = callconduit(
790 repo.ui, b'differential.parsecommitmessage', {b'corpus': desc}
828 repo.ui, b'differential.parsecommitmessage', {b'corpus': desc}
791 )
829 )
792 for k, v in info[b'fields'].items():
830 for k, v in info[b'fields'].items():
793 if k in [b'title', b'summary', b'testPlan']:
831 if k in [b'title', b'summary', b'testPlan']:
794 transactions.append({b'type': k, b'value': v})
832 transactions.append({b'type': k, b'value': v})
795
833
796 params = {b'transactions': transactions}
834 params = {b'transactions': transactions}
797 if revid is not None:
835 if revid is not None:
798 # Update an existing Differential Revision
836 # Update an existing Differential Revision
799 params[b'objectIdentifier'] = revid
837 params[b'objectIdentifier'] = revid
800
838
801 revision = callconduit(repo.ui, b'differential.revision.edit', params)
839 revision = callconduit(repo.ui, b'differential.revision.edit', params)
802 if not revision:
840 if not revision:
803 raise error.Abort(_(b'cannot create revision for %s') % ctx)
841 raise error.Abort(_(b'cannot create revision for %s') % ctx)
804
842
805 return revision, diff
843 return revision, diff
806
844
807
845
808 def userphids(repo, names):
846 def userphids(repo, names):
809 """convert user names to PHIDs"""
847 """convert user names to PHIDs"""
810 names = [name.lower() for name in names]
848 names = [name.lower() for name in names]
811 query = {b'constraints': {b'usernames': names}}
849 query = {b'constraints': {b'usernames': names}}
812 result = callconduit(repo.ui, b'user.search', query)
850 result = callconduit(repo.ui, b'user.search', query)
813 # username not found is not an error of the API. So check if we have missed
851 # username not found is not an error of the API. So check if we have missed
814 # some names here.
852 # some names here.
815 data = result[b'data']
853 data = result[b'data']
816 resolved = set(entry[b'fields'][b'username'].lower() for entry in data)
854 resolved = set(entry[b'fields'][b'username'].lower() for entry in data)
817 unresolved = set(names) - resolved
855 unresolved = set(names) - resolved
818 if unresolved:
856 if unresolved:
819 raise error.Abort(
857 raise error.Abort(
820 _(b'unknown username: %s') % b' '.join(sorted(unresolved))
858 _(b'unknown username: %s') % b' '.join(sorted(unresolved))
821 )
859 )
822 return [entry[b'phid'] for entry in data]
860 return [entry[b'phid'] for entry in data]
823
861
824
862
825 @vcrcommand(
863 @vcrcommand(
826 b'phabsend',
864 b'phabsend',
827 [
865 [
828 (b'r', b'rev', [], _(b'revisions to send'), _(b'REV')),
866 (b'r', b'rev', [], _(b'revisions to send'), _(b'REV')),
829 (b'', b'amend', True, _(b'update commit messages')),
867 (b'', b'amend', True, _(b'update commit messages')),
830 (b'', b'reviewer', [], _(b'specify reviewers')),
868 (b'', b'reviewer', [], _(b'specify reviewers')),
831 (b'', b'blocker', [], _(b'specify blocking reviewers')),
869 (b'', b'blocker', [], _(b'specify blocking reviewers')),
832 (
870 (
833 b'm',
871 b'm',
834 b'comment',
872 b'comment',
835 b'',
873 b'',
836 _(b'add a comment to Revisions with new/updated Diffs'),
874 _(b'add a comment to Revisions with new/updated Diffs'),
837 ),
875 ),
838 (b'', b'confirm', None, _(b'ask for confirmation before sending')),
876 (b'', b'confirm', None, _(b'ask for confirmation before sending')),
839 ],
877 ],
840 _(b'REV [OPTIONS]'),
878 _(b'REV [OPTIONS]'),
841 helpcategory=command.CATEGORY_IMPORT_EXPORT,
879 helpcategory=command.CATEGORY_IMPORT_EXPORT,
842 )
880 )
843 def phabsend(ui, repo, *revs, **opts):
881 def phabsend(ui, repo, *revs, **opts):
844 """upload changesets to Phabricator
882 """upload changesets to Phabricator
845
883
846 If there are multiple revisions specified, they will be send as a stack
884 If there are multiple revisions specified, they will be send as a stack
847 with a linear dependencies relationship using the order specified by the
885 with a linear dependencies relationship using the order specified by the
848 revset.
886 revset.
849
887
850 For the first time uploading changesets, local tags will be created to
888 For the first time uploading changesets, local tags will be created to
851 maintain the association. After the first time, phabsend will check
889 maintain the association. After the first time, phabsend will check
852 obsstore and tags information so it can figure out whether to update an
890 obsstore and tags information so it can figure out whether to update an
853 existing Differential Revision, or create a new one.
891 existing Differential Revision, or create a new one.
854
892
855 If --amend is set, update commit messages so they have the
893 If --amend is set, update commit messages so they have the
856 ``Differential Revision`` URL, remove related tags. This is similar to what
894 ``Differential Revision`` URL, remove related tags. This is similar to what
857 arcanist will do, and is more desired in author-push workflows. Otherwise,
895 arcanist will do, and is more desired in author-push workflows. Otherwise,
858 use local tags to record the ``Differential Revision`` association.
896 use local tags to record the ``Differential Revision`` association.
859
897
860 The --confirm option lets you confirm changesets before sending them. You
898 The --confirm option lets you confirm changesets before sending them. You
861 can also add following to your configuration file to make it default
899 can also add following to your configuration file to make it default
862 behaviour::
900 behaviour::
863
901
864 [phabsend]
902 [phabsend]
865 confirm = true
903 confirm = true
866
904
867 phabsend will check obsstore and the above association to decide whether to
905 phabsend will check obsstore and the above association to decide whether to
868 update an existing Differential Revision, or create a new one.
906 update an existing Differential Revision, or create a new one.
869 """
907 """
870 opts = pycompat.byteskwargs(opts)
908 opts = pycompat.byteskwargs(opts)
871 revs = list(revs) + opts.get(b'rev', [])
909 revs = list(revs) + opts.get(b'rev', [])
872 revs = scmutil.revrange(repo, revs)
910 revs = scmutil.revrange(repo, revs)
873
911
874 if not revs:
912 if not revs:
875 raise error.Abort(_(b'phabsend requires at least one changeset'))
913 raise error.Abort(_(b'phabsend requires at least one changeset'))
876 if opts.get(b'amend'):
914 if opts.get(b'amend'):
877 cmdutil.checkunfinished(repo)
915 cmdutil.checkunfinished(repo)
878
916
879 # {newnode: (oldnode, olddiff, olddrev}
917 # {newnode: (oldnode, olddiff, olddrev}
880 oldmap = getoldnodedrevmap(repo, [repo[r].node() for r in revs])
918 oldmap = getoldnodedrevmap(repo, [repo[r].node() for r in revs])
881
919
882 confirm = ui.configbool(b'phabsend', b'confirm')
920 confirm = ui.configbool(b'phabsend', b'confirm')
883 confirm |= bool(opts.get(b'confirm'))
921 confirm |= bool(opts.get(b'confirm'))
884 if confirm:
922 if confirm:
885 confirmed = _confirmbeforesend(repo, revs, oldmap)
923 confirmed = _confirmbeforesend(repo, revs, oldmap)
886 if not confirmed:
924 if not confirmed:
887 raise error.Abort(_(b'phabsend cancelled'))
925 raise error.Abort(_(b'phabsend cancelled'))
888
926
889 actions = []
927 actions = []
890 reviewers = opts.get(b'reviewer', [])
928 reviewers = opts.get(b'reviewer', [])
891 blockers = opts.get(b'blocker', [])
929 blockers = opts.get(b'blocker', [])
892 phids = []
930 phids = []
893 if reviewers:
931 if reviewers:
894 phids.extend(userphids(repo, reviewers))
932 phids.extend(userphids(repo, reviewers))
895 if blockers:
933 if blockers:
896 phids.extend(
934 phids.extend(
897 map(lambda phid: b'blocking(%s)' % phid, userphids(repo, blockers))
935 map(lambda phid: b'blocking(%s)' % phid, userphids(repo, blockers))
898 )
936 )
899 if phids:
937 if phids:
900 actions.append({b'type': b'reviewers.add', b'value': phids})
938 actions.append({b'type': b'reviewers.add', b'value': phids})
901
939
902 drevids = [] # [int]
940 drevids = [] # [int]
903 diffmap = {} # {newnode: diff}
941 diffmap = {} # {newnode: diff}
904
942
905 # Send patches one by one so we know their Differential Revision PHIDs and
943 # Send patches one by one so we know their Differential Revision PHIDs and
906 # can provide dependency relationship
944 # can provide dependency relationship
907 lastrevphid = None
945 lastrevphid = None
908 for rev in revs:
946 for rev in revs:
909 ui.debug(b'sending rev %d\n' % rev)
947 ui.debug(b'sending rev %d\n' % rev)
910 ctx = repo[rev]
948 ctx = repo[rev]
911
949
912 # Get Differential Revision ID
950 # Get Differential Revision ID
913 oldnode, olddiff, revid = oldmap.get(ctx.node(), (None, None, None))
951 oldnode, olddiff, revid = oldmap.get(ctx.node(), (None, None, None))
914 if oldnode != ctx.node() or opts.get(b'amend'):
952 if oldnode != ctx.node() or opts.get(b'amend'):
915 # Create or update Differential Revision
953 # Create or update Differential Revision
916 revision, diff = createdifferentialrevision(
954 revision, diff = createdifferentialrevision(
917 ctx,
955 ctx,
918 revid,
956 revid,
919 lastrevphid,
957 lastrevphid,
920 oldnode,
958 oldnode,
921 olddiff,
959 olddiff,
922 actions,
960 actions,
923 opts.get(b'comment'),
961 opts.get(b'comment'),
924 )
962 )
925 diffmap[ctx.node()] = diff
963 diffmap[ctx.node()] = diff
926 newrevid = int(revision[b'object'][b'id'])
964 newrevid = int(revision[b'object'][b'id'])
927 newrevphid = revision[b'object'][b'phid']
965 newrevphid = revision[b'object'][b'phid']
928 if revid:
966 if revid:
929 action = b'updated'
967 action = b'updated'
930 else:
968 else:
931 action = b'created'
969 action = b'created'
932
970
933 # Create a local tag to note the association, if commit message
971 # Create a local tag to note the association, if commit message
934 # does not have it already
972 # does not have it already
935 m = _differentialrevisiondescre.search(ctx.description())
973 m = _differentialrevisiondescre.search(ctx.description())
936 if not m or int(m.group(r'id')) != newrevid:
974 if not m or int(m.group(r'id')) != newrevid:
937 tagname = b'D%d' % newrevid
975 tagname = b'D%d' % newrevid
938 tags.tag(
976 tags.tag(
939 repo,
977 repo,
940 tagname,
978 tagname,
941 ctx.node(),
979 ctx.node(),
942 message=None,
980 message=None,
943 user=None,
981 user=None,
944 date=None,
982 date=None,
945 local=True,
983 local=True,
946 )
984 )
947 else:
985 else:
948 # Nothing changed. But still set "newrevphid" so the next revision
986 # Nothing changed. But still set "newrevphid" so the next revision
949 # could depend on this one and "newrevid" for the summary line.
987 # could depend on this one and "newrevid" for the summary line.
950 newrevphid = querydrev(repo, b'%d' % revid)[0][b'phid']
988 newrevphid = querydrev(repo, b'%d' % revid)[0][b'phid']
951 newrevid = revid
989 newrevid = revid
952 action = b'skipped'
990 action = b'skipped'
953
991
954 actiondesc = ui.label(
992 actiondesc = ui.label(
955 {
993 {
956 b'created': _(b'created'),
994 b'created': _(b'created'),
957 b'skipped': _(b'skipped'),
995 b'skipped': _(b'skipped'),
958 b'updated': _(b'updated'),
996 b'updated': _(b'updated'),
959 }[action],
997 }[action],
960 b'phabricator.action.%s' % action,
998 b'phabricator.action.%s' % action,
961 )
999 )
962 drevdesc = ui.label(b'D%d' % newrevid, b'phabricator.drev')
1000 drevdesc = ui.label(b'D%d' % newrevid, b'phabricator.drev')
963 nodedesc = ui.label(bytes(ctx), b'phabricator.node')
1001 nodedesc = ui.label(bytes(ctx), b'phabricator.node')
964 desc = ui.label(ctx.description().split(b'\n')[0], b'phabricator.desc')
1002 desc = ui.label(ctx.description().split(b'\n')[0], b'phabricator.desc')
965 ui.write(
1003 ui.write(
966 _(b'%s - %s - %s: %s\n') % (drevdesc, actiondesc, nodedesc, desc)
1004 _(b'%s - %s - %s: %s\n') % (drevdesc, actiondesc, nodedesc, desc)
967 )
1005 )
968 drevids.append(newrevid)
1006 drevids.append(newrevid)
969 lastrevphid = newrevphid
1007 lastrevphid = newrevphid
970
1008
971 # Update commit messages and remove tags
1009 # Update commit messages and remove tags
972 if opts.get(b'amend'):
1010 if opts.get(b'amend'):
973 unfi = repo.unfiltered()
1011 unfi = repo.unfiltered()
974 drevs = callconduit(ui, b'differential.query', {b'ids': drevids})
1012 drevs = callconduit(ui, b'differential.query', {b'ids': drevids})
975 with repo.wlock(), repo.lock(), repo.transaction(b'phabsend'):
1013 with repo.wlock(), repo.lock(), repo.transaction(b'phabsend'):
976 wnode = unfi[b'.'].node()
1014 wnode = unfi[b'.'].node()
977 mapping = {} # {oldnode: [newnode]}
1015 mapping = {} # {oldnode: [newnode]}
978 for i, rev in enumerate(revs):
1016 for i, rev in enumerate(revs):
979 old = unfi[rev]
1017 old = unfi[rev]
980 drevid = drevids[i]
1018 drevid = drevids[i]
981 drev = [d for d in drevs if int(d[b'id']) == drevid][0]
1019 drev = [d for d in drevs if int(d[b'id']) == drevid][0]
982 newdesc = getdescfromdrev(drev)
1020 newdesc = getdescfromdrev(drev)
983 # Make sure commit message contain "Differential Revision"
1021 # Make sure commit message contain "Differential Revision"
984 if old.description() != newdesc:
1022 if old.description() != newdesc:
985 if old.phase() == phases.public:
1023 if old.phase() == phases.public:
986 ui.warn(
1024 ui.warn(
987 _(b"warning: not updating public commit %s\n")
1025 _(b"warning: not updating public commit %s\n")
988 % scmutil.formatchangeid(old)
1026 % scmutil.formatchangeid(old)
989 )
1027 )
990 continue
1028 continue
991 parents = [
1029 parents = [
992 mapping.get(old.p1().node(), (old.p1(),))[0],
1030 mapping.get(old.p1().node(), (old.p1(),))[0],
993 mapping.get(old.p2().node(), (old.p2(),))[0],
1031 mapping.get(old.p2().node(), (old.p2(),))[0],
994 ]
1032 ]
995 new = context.metadataonlyctx(
1033 new = context.metadataonlyctx(
996 repo,
1034 repo,
997 old,
1035 old,
998 parents=parents,
1036 parents=parents,
999 text=newdesc,
1037 text=newdesc,
1000 user=old.user(),
1038 user=old.user(),
1001 date=old.date(),
1039 date=old.date(),
1002 extra=old.extra(),
1040 extra=old.extra(),
1003 )
1041 )
1004
1042
1005 newnode = new.commit()
1043 newnode = new.commit()
1006
1044
1007 mapping[old.node()] = [newnode]
1045 mapping[old.node()] = [newnode]
1008 # Update diff property
1046 # Update diff property
1009 # If it fails just warn and keep going, otherwise the DREV
1047 # If it fails just warn and keep going, otherwise the DREV
1010 # associations will be lost
1048 # associations will be lost
1011 try:
1049 try:
1012 writediffproperties(unfi[newnode], diffmap[old.node()])
1050 writediffproperties(unfi[newnode], diffmap[old.node()])
1013 except util.urlerr.urlerror:
1051 except util.urlerr.urlerror:
1014 ui.warnnoi18n(
1052 ui.warnnoi18n(
1015 b'Failed to update metadata for D%s\n' % drevid
1053 b'Failed to update metadata for D%s\n' % drevid
1016 )
1054 )
1017 # Remove local tags since it's no longer necessary
1055 # Remove local tags since it's no longer necessary
1018 tagname = b'D%d' % drevid
1056 tagname = b'D%d' % drevid
1019 if tagname in repo.tags():
1057 if tagname in repo.tags():
1020 tags.tag(
1058 tags.tag(
1021 repo,
1059 repo,
1022 tagname,
1060 tagname,
1023 nullid,
1061 nullid,
1024 message=None,
1062 message=None,
1025 user=None,
1063 user=None,
1026 date=None,
1064 date=None,
1027 local=True,
1065 local=True,
1028 )
1066 )
1029 scmutil.cleanupnodes(repo, mapping, b'phabsend', fixphase=True)
1067 scmutil.cleanupnodes(repo, mapping, b'phabsend', fixphase=True)
1030 if wnode in mapping:
1068 if wnode in mapping:
1031 unfi.setparents(mapping[wnode][0])
1069 unfi.setparents(mapping[wnode][0])
1032
1070
1033
1071
1034 # Map from "hg:meta" keys to header understood by "hg import". The order is
1072 # Map from "hg:meta" keys to header understood by "hg import". The order is
1035 # consistent with "hg export" output.
1073 # consistent with "hg export" output.
1036 _metanamemap = util.sortdict(
1074 _metanamemap = util.sortdict(
1037 [
1075 [
1038 (b'user', b'User'),
1076 (b'user', b'User'),
1039 (b'date', b'Date'),
1077 (b'date', b'Date'),
1040 (b'branch', b'Branch'),
1078 (b'branch', b'Branch'),
1041 (b'node', b'Node ID'),
1079 (b'node', b'Node ID'),
1042 (b'parent', b'Parent '),
1080 (b'parent', b'Parent '),
1043 ]
1081 ]
1044 )
1082 )
1045
1083
1046
1084
1047 def _confirmbeforesend(repo, revs, oldmap):
1085 def _confirmbeforesend(repo, revs, oldmap):
1048 url, token = readurltoken(repo.ui)
1086 url, token = readurltoken(repo.ui)
1049 ui = repo.ui
1087 ui = repo.ui
1050 for rev in revs:
1088 for rev in revs:
1051 ctx = repo[rev]
1089 ctx = repo[rev]
1052 desc = ctx.description().splitlines()[0]
1090 desc = ctx.description().splitlines()[0]
1053 oldnode, olddiff, drevid = oldmap.get(ctx.node(), (None, None, None))
1091 oldnode, olddiff, drevid = oldmap.get(ctx.node(), (None, None, None))
1054 if drevid:
1092 if drevid:
1055 drevdesc = ui.label(b'D%s' % drevid, b'phabricator.drev')
1093 drevdesc = ui.label(b'D%s' % drevid, b'phabricator.drev')
1056 else:
1094 else:
1057 drevdesc = ui.label(_(b'NEW'), b'phabricator.drev')
1095 drevdesc = ui.label(_(b'NEW'), b'phabricator.drev')
1058
1096
1059 ui.write(
1097 ui.write(
1060 _(b'%s - %s: %s\n')
1098 _(b'%s - %s: %s\n')
1061 % (
1099 % (
1062 drevdesc,
1100 drevdesc,
1063 ui.label(bytes(ctx), b'phabricator.node'),
1101 ui.label(bytes(ctx), b'phabricator.node'),
1064 ui.label(desc, b'phabricator.desc'),
1102 ui.label(desc, b'phabricator.desc'),
1065 )
1103 )
1066 )
1104 )
1067
1105
1068 if ui.promptchoice(
1106 if ui.promptchoice(
1069 _(b'Send the above changes to %s (yn)?$$ &Yes $$ &No') % url
1107 _(b'Send the above changes to %s (yn)?$$ &Yes $$ &No') % url
1070 ):
1108 ):
1071 return False
1109 return False
1072
1110
1073 return True
1111 return True
1074
1112
1075
1113
1076 _knownstatusnames = {
1114 _knownstatusnames = {
1077 b'accepted',
1115 b'accepted',
1078 b'needsreview',
1116 b'needsreview',
1079 b'needsrevision',
1117 b'needsrevision',
1080 b'closed',
1118 b'closed',
1081 b'abandoned',
1119 b'abandoned',
1082 }
1120 }
1083
1121
1084
1122
1085 def _getstatusname(drev):
1123 def _getstatusname(drev):
1086 """get normalized status name from a Differential Revision"""
1124 """get normalized status name from a Differential Revision"""
1087 return drev[b'statusName'].replace(b' ', b'').lower()
1125 return drev[b'statusName'].replace(b' ', b'').lower()
1088
1126
1089
1127
1090 # Small language to specify differential revisions. Support symbols: (), :X,
1128 # Small language to specify differential revisions. Support symbols: (), :X,
1091 # +, and -.
1129 # +, and -.
1092
1130
1093 _elements = {
1131 _elements = {
1094 # token-type: binding-strength, primary, prefix, infix, suffix
1132 # token-type: binding-strength, primary, prefix, infix, suffix
1095 b'(': (12, None, (b'group', 1, b')'), None, None),
1133 b'(': (12, None, (b'group', 1, b')'), None, None),
1096 b':': (8, None, (b'ancestors', 8), None, None),
1134 b':': (8, None, (b'ancestors', 8), None, None),
1097 b'&': (5, None, None, (b'and_', 5), None),
1135 b'&': (5, None, None, (b'and_', 5), None),
1098 b'+': (4, None, None, (b'add', 4), None),
1136 b'+': (4, None, None, (b'add', 4), None),
1099 b'-': (4, None, None, (b'sub', 4), None),
1137 b'-': (4, None, None, (b'sub', 4), None),
1100 b')': (0, None, None, None, None),
1138 b')': (0, None, None, None, None),
1101 b'symbol': (0, b'symbol', None, None, None),
1139 b'symbol': (0, b'symbol', None, None, None),
1102 b'end': (0, None, None, None, None),
1140 b'end': (0, None, None, None, None),
1103 }
1141 }
1104
1142
1105
1143
1106 def _tokenize(text):
1144 def _tokenize(text):
1107 view = memoryview(text) # zero-copy slice
1145 view = memoryview(text) # zero-copy slice
1108 special = b'():+-& '
1146 special = b'():+-& '
1109 pos = 0
1147 pos = 0
1110 length = len(text)
1148 length = len(text)
1111 while pos < length:
1149 while pos < length:
1112 symbol = b''.join(
1150 symbol = b''.join(
1113 itertools.takewhile(
1151 itertools.takewhile(
1114 lambda ch: ch not in special, pycompat.iterbytestr(view[pos:])
1152 lambda ch: ch not in special, pycompat.iterbytestr(view[pos:])
1115 )
1153 )
1116 )
1154 )
1117 if symbol:
1155 if symbol:
1118 yield (b'symbol', symbol, pos)
1156 yield (b'symbol', symbol, pos)
1119 pos += len(symbol)
1157 pos += len(symbol)
1120 else: # special char, ignore space
1158 else: # special char, ignore space
1121 if text[pos] != b' ':
1159 if text[pos] != b' ':
1122 yield (text[pos], None, pos)
1160 yield (text[pos], None, pos)
1123 pos += 1
1161 pos += 1
1124 yield (b'end', None, pos)
1162 yield (b'end', None, pos)
1125
1163
1126
1164
1127 def _parse(text):
1165 def _parse(text):
1128 tree, pos = parser.parser(_elements).parse(_tokenize(text))
1166 tree, pos = parser.parser(_elements).parse(_tokenize(text))
1129 if pos != len(text):
1167 if pos != len(text):
1130 raise error.ParseError(b'invalid token', pos)
1168 raise error.ParseError(b'invalid token', pos)
1131 return tree
1169 return tree
1132
1170
1133
1171
1134 def _parsedrev(symbol):
1172 def _parsedrev(symbol):
1135 """str -> int or None, ex. 'D45' -> 45; '12' -> 12; 'x' -> None"""
1173 """str -> int or None, ex. 'D45' -> 45; '12' -> 12; 'x' -> None"""
1136 if symbol.startswith(b'D') and symbol[1:].isdigit():
1174 if symbol.startswith(b'D') and symbol[1:].isdigit():
1137 return int(symbol[1:])
1175 return int(symbol[1:])
1138 if symbol.isdigit():
1176 if symbol.isdigit():
1139 return int(symbol)
1177 return int(symbol)
1140
1178
1141
1179
1142 def _prefetchdrevs(tree):
1180 def _prefetchdrevs(tree):
1143 """return ({single-drev-id}, {ancestor-drev-id}) to prefetch"""
1181 """return ({single-drev-id}, {ancestor-drev-id}) to prefetch"""
1144 drevs = set()
1182 drevs = set()
1145 ancestordrevs = set()
1183 ancestordrevs = set()
1146 op = tree[0]
1184 op = tree[0]
1147 if op == b'symbol':
1185 if op == b'symbol':
1148 r = _parsedrev(tree[1])
1186 r = _parsedrev(tree[1])
1149 if r:
1187 if r:
1150 drevs.add(r)
1188 drevs.add(r)
1151 elif op == b'ancestors':
1189 elif op == b'ancestors':
1152 r, a = _prefetchdrevs(tree[1])
1190 r, a = _prefetchdrevs(tree[1])
1153 drevs.update(r)
1191 drevs.update(r)
1154 ancestordrevs.update(r)
1192 ancestordrevs.update(r)
1155 ancestordrevs.update(a)
1193 ancestordrevs.update(a)
1156 else:
1194 else:
1157 for t in tree[1:]:
1195 for t in tree[1:]:
1158 r, a = _prefetchdrevs(t)
1196 r, a = _prefetchdrevs(t)
1159 drevs.update(r)
1197 drevs.update(r)
1160 ancestordrevs.update(a)
1198 ancestordrevs.update(a)
1161 return drevs, ancestordrevs
1199 return drevs, ancestordrevs
1162
1200
1163
1201
1164 def querydrev(repo, spec):
1202 def querydrev(repo, spec):
1165 """return a list of "Differential Revision" dicts
1203 """return a list of "Differential Revision" dicts
1166
1204
1167 spec is a string using a simple query language, see docstring in phabread
1205 spec is a string using a simple query language, see docstring in phabread
1168 for details.
1206 for details.
1169
1207
1170 A "Differential Revision dict" looks like:
1208 A "Differential Revision dict" looks like:
1171
1209
1172 {
1210 {
1173 "id": "2",
1211 "id": "2",
1174 "phid": "PHID-DREV-672qvysjcczopag46qty",
1212 "phid": "PHID-DREV-672qvysjcczopag46qty",
1175 "title": "example",
1213 "title": "example",
1176 "uri": "https://phab.example.com/D2",
1214 "uri": "https://phab.example.com/D2",
1177 "dateCreated": "1499181406",
1215 "dateCreated": "1499181406",
1178 "dateModified": "1499182103",
1216 "dateModified": "1499182103",
1179 "authorPHID": "PHID-USER-tv3ohwc4v4jeu34otlye",
1217 "authorPHID": "PHID-USER-tv3ohwc4v4jeu34otlye",
1180 "status": "0",
1218 "status": "0",
1181 "statusName": "Needs Review",
1219 "statusName": "Needs Review",
1182 "properties": [],
1220 "properties": [],
1183 "branch": null,
1221 "branch": null,
1184 "summary": "",
1222 "summary": "",
1185 "testPlan": "",
1223 "testPlan": "",
1186 "lineCount": "2",
1224 "lineCount": "2",
1187 "activeDiffPHID": "PHID-DIFF-xoqnjkobbm6k4dk6hi72",
1225 "activeDiffPHID": "PHID-DIFF-xoqnjkobbm6k4dk6hi72",
1188 "diffs": [
1226 "diffs": [
1189 "3",
1227 "3",
1190 "4",
1228 "4",
1191 ],
1229 ],
1192 "commits": [],
1230 "commits": [],
1193 "reviewers": [],
1231 "reviewers": [],
1194 "ccs": [],
1232 "ccs": [],
1195 "hashes": [],
1233 "hashes": [],
1196 "auxiliary": {
1234 "auxiliary": {
1197 "phabricator:projects": [],
1235 "phabricator:projects": [],
1198 "phabricator:depends-on": [
1236 "phabricator:depends-on": [
1199 "PHID-DREV-gbapp366kutjebt7agcd"
1237 "PHID-DREV-gbapp366kutjebt7agcd"
1200 ]
1238 ]
1201 },
1239 },
1202 "repositoryPHID": "PHID-REPO-hub2hx62ieuqeheznasv",
1240 "repositoryPHID": "PHID-REPO-hub2hx62ieuqeheznasv",
1203 "sourcePath": null
1241 "sourcePath": null
1204 }
1242 }
1205 """
1243 """
1206
1244
1207 def fetch(params):
1245 def fetch(params):
1208 """params -> single drev or None"""
1246 """params -> single drev or None"""
1209 key = (params.get(b'ids') or params.get(b'phids') or [None])[0]
1247 key = (params.get(b'ids') or params.get(b'phids') or [None])[0]
1210 if key in prefetched:
1248 if key in prefetched:
1211 return prefetched[key]
1249 return prefetched[key]
1212 drevs = callconduit(repo.ui, b'differential.query', params)
1250 drevs = callconduit(repo.ui, b'differential.query', params)
1213 # Fill prefetched with the result
1251 # Fill prefetched with the result
1214 for drev in drevs:
1252 for drev in drevs:
1215 prefetched[drev[b'phid']] = drev
1253 prefetched[drev[b'phid']] = drev
1216 prefetched[int(drev[b'id'])] = drev
1254 prefetched[int(drev[b'id'])] = drev
1217 if key not in prefetched:
1255 if key not in prefetched:
1218 raise error.Abort(
1256 raise error.Abort(
1219 _(b'cannot get Differential Revision %r') % params
1257 _(b'cannot get Differential Revision %r') % params
1220 )
1258 )
1221 return prefetched[key]
1259 return prefetched[key]
1222
1260
1223 def getstack(topdrevids):
1261 def getstack(topdrevids):
1224 """given a top, get a stack from the bottom, [id] -> [id]"""
1262 """given a top, get a stack from the bottom, [id] -> [id]"""
1225 visited = set()
1263 visited = set()
1226 result = []
1264 result = []
1227 queue = [{b'ids': [i]} for i in topdrevids]
1265 queue = [{b'ids': [i]} for i in topdrevids]
1228 while queue:
1266 while queue:
1229 params = queue.pop()
1267 params = queue.pop()
1230 drev = fetch(params)
1268 drev = fetch(params)
1231 if drev[b'id'] in visited:
1269 if drev[b'id'] in visited:
1232 continue
1270 continue
1233 visited.add(drev[b'id'])
1271 visited.add(drev[b'id'])
1234 result.append(int(drev[b'id']))
1272 result.append(int(drev[b'id']))
1235 auxiliary = drev.get(b'auxiliary', {})
1273 auxiliary = drev.get(b'auxiliary', {})
1236 depends = auxiliary.get(b'phabricator:depends-on', [])
1274 depends = auxiliary.get(b'phabricator:depends-on', [])
1237 for phid in depends:
1275 for phid in depends:
1238 queue.append({b'phids': [phid]})
1276 queue.append({b'phids': [phid]})
1239 result.reverse()
1277 result.reverse()
1240 return smartset.baseset(result)
1278 return smartset.baseset(result)
1241
1279
1242 # Initialize prefetch cache
1280 # Initialize prefetch cache
1243 prefetched = {} # {id or phid: drev}
1281 prefetched = {} # {id or phid: drev}
1244
1282
1245 tree = _parse(spec)
1283 tree = _parse(spec)
1246 drevs, ancestordrevs = _prefetchdrevs(tree)
1284 drevs, ancestordrevs = _prefetchdrevs(tree)
1247
1285
1248 # developer config: phabricator.batchsize
1286 # developer config: phabricator.batchsize
1249 batchsize = repo.ui.configint(b'phabricator', b'batchsize')
1287 batchsize = repo.ui.configint(b'phabricator', b'batchsize')
1250
1288
1251 # Prefetch Differential Revisions in batch
1289 # Prefetch Differential Revisions in batch
1252 tofetch = set(drevs)
1290 tofetch = set(drevs)
1253 for r in ancestordrevs:
1291 for r in ancestordrevs:
1254 tofetch.update(range(max(1, r - batchsize), r + 1))
1292 tofetch.update(range(max(1, r - batchsize), r + 1))
1255 if drevs:
1293 if drevs:
1256 fetch({b'ids': list(tofetch)})
1294 fetch({b'ids': list(tofetch)})
1257 validids = sorted(set(getstack(list(ancestordrevs))) | set(drevs))
1295 validids = sorted(set(getstack(list(ancestordrevs))) | set(drevs))
1258
1296
1259 # Walk through the tree, return smartsets
1297 # Walk through the tree, return smartsets
1260 def walk(tree):
1298 def walk(tree):
1261 op = tree[0]
1299 op = tree[0]
1262 if op == b'symbol':
1300 if op == b'symbol':
1263 drev = _parsedrev(tree[1])
1301 drev = _parsedrev(tree[1])
1264 if drev:
1302 if drev:
1265 return smartset.baseset([drev])
1303 return smartset.baseset([drev])
1266 elif tree[1] in _knownstatusnames:
1304 elif tree[1] in _knownstatusnames:
1267 drevs = [
1305 drevs = [
1268 r
1306 r
1269 for r in validids
1307 for r in validids
1270 if _getstatusname(prefetched[r]) == tree[1]
1308 if _getstatusname(prefetched[r]) == tree[1]
1271 ]
1309 ]
1272 return smartset.baseset(drevs)
1310 return smartset.baseset(drevs)
1273 else:
1311 else:
1274 raise error.Abort(_(b'unknown symbol: %s') % tree[1])
1312 raise error.Abort(_(b'unknown symbol: %s') % tree[1])
1275 elif op in {b'and_', b'add', b'sub'}:
1313 elif op in {b'and_', b'add', b'sub'}:
1276 assert len(tree) == 3
1314 assert len(tree) == 3
1277 return getattr(operator, op)(walk(tree[1]), walk(tree[2]))
1315 return getattr(operator, op)(walk(tree[1]), walk(tree[2]))
1278 elif op == b'group':
1316 elif op == b'group':
1279 return walk(tree[1])
1317 return walk(tree[1])
1280 elif op == b'ancestors':
1318 elif op == b'ancestors':
1281 return getstack(walk(tree[1]))
1319 return getstack(walk(tree[1]))
1282 else:
1320 else:
1283 raise error.ProgrammingError(b'illegal tree: %r' % tree)
1321 raise error.ProgrammingError(b'illegal tree: %r' % tree)
1284
1322
1285 return [prefetched[r] for r in walk(tree)]
1323 return [prefetched[r] for r in walk(tree)]
1286
1324
1287
1325
1288 def getdescfromdrev(drev):
1326 def getdescfromdrev(drev):
1289 """get description (commit message) from "Differential Revision"
1327 """get description (commit message) from "Differential Revision"
1290
1328
1291 This is similar to differential.getcommitmessage API. But we only care
1329 This is similar to differential.getcommitmessage API. But we only care
1292 about limited fields: title, summary, test plan, and URL.
1330 about limited fields: title, summary, test plan, and URL.
1293 """
1331 """
1294 title = drev[b'title']
1332 title = drev[b'title']
1295 summary = drev[b'summary'].rstrip()
1333 summary = drev[b'summary'].rstrip()
1296 testplan = drev[b'testPlan'].rstrip()
1334 testplan = drev[b'testPlan'].rstrip()
1297 if testplan:
1335 if testplan:
1298 testplan = b'Test Plan:\n%s' % testplan
1336 testplan = b'Test Plan:\n%s' % testplan
1299 uri = b'Differential Revision: %s' % drev[b'uri']
1337 uri = b'Differential Revision: %s' % drev[b'uri']
1300 return b'\n\n'.join(filter(None, [title, summary, testplan, uri]))
1338 return b'\n\n'.join(filter(None, [title, summary, testplan, uri]))
1301
1339
1302
1340
1303 def getdiffmeta(diff):
1341 def getdiffmeta(diff):
1304 """get commit metadata (date, node, user, p1) from a diff object
1342 """get commit metadata (date, node, user, p1) from a diff object
1305
1343
1306 The metadata could be "hg:meta", sent by phabsend, like:
1344 The metadata could be "hg:meta", sent by phabsend, like:
1307
1345
1308 "properties": {
1346 "properties": {
1309 "hg:meta": {
1347 "hg:meta": {
1310 "date": "1499571514 25200",
1348 "date": "1499571514 25200",
1311 "node": "98c08acae292b2faf60a279b4189beb6cff1414d",
1349 "node": "98c08acae292b2faf60a279b4189beb6cff1414d",
1312 "user": "Foo Bar <foo@example.com>",
1350 "user": "Foo Bar <foo@example.com>",
1313 "parent": "6d0abad76b30e4724a37ab8721d630394070fe16"
1351 "parent": "6d0abad76b30e4724a37ab8721d630394070fe16"
1314 }
1352 }
1315 }
1353 }
1316
1354
1317 Or converted from "local:commits", sent by "arc", like:
1355 Or converted from "local:commits", sent by "arc", like:
1318
1356
1319 "properties": {
1357 "properties": {
1320 "local:commits": {
1358 "local:commits": {
1321 "98c08acae292b2faf60a279b4189beb6cff1414d": {
1359 "98c08acae292b2faf60a279b4189beb6cff1414d": {
1322 "author": "Foo Bar",
1360 "author": "Foo Bar",
1323 "time": 1499546314,
1361 "time": 1499546314,
1324 "branch": "default",
1362 "branch": "default",
1325 "tag": "",
1363 "tag": "",
1326 "commit": "98c08acae292b2faf60a279b4189beb6cff1414d",
1364 "commit": "98c08acae292b2faf60a279b4189beb6cff1414d",
1327 "rev": "98c08acae292b2faf60a279b4189beb6cff1414d",
1365 "rev": "98c08acae292b2faf60a279b4189beb6cff1414d",
1328 "local": "1000",
1366 "local": "1000",
1329 "parents": ["6d0abad76b30e4724a37ab8721d630394070fe16"],
1367 "parents": ["6d0abad76b30e4724a37ab8721d630394070fe16"],
1330 "summary": "...",
1368 "summary": "...",
1331 "message": "...",
1369 "message": "...",
1332 "authorEmail": "foo@example.com"
1370 "authorEmail": "foo@example.com"
1333 }
1371 }
1334 }
1372 }
1335 }
1373 }
1336
1374
1337 Note: metadata extracted from "local:commits" will lose time zone
1375 Note: metadata extracted from "local:commits" will lose time zone
1338 information.
1376 information.
1339 """
1377 """
1340 props = diff.get(b'properties') or {}
1378 props = diff.get(b'properties') or {}
1341 meta = props.get(b'hg:meta')
1379 meta = props.get(b'hg:meta')
1342 if not meta:
1380 if not meta:
1343 if props.get(b'local:commits'):
1381 if props.get(b'local:commits'):
1344 commit = sorted(props[b'local:commits'].values())[0]
1382 commit = sorted(props[b'local:commits'].values())[0]
1345 meta = {}
1383 meta = {}
1346 if b'author' in commit and b'authorEmail' in commit:
1384 if b'author' in commit and b'authorEmail' in commit:
1347 meta[b'user'] = b'%s <%s>' % (
1385 meta[b'user'] = b'%s <%s>' % (
1348 commit[b'author'],
1386 commit[b'author'],
1349 commit[b'authorEmail'],
1387 commit[b'authorEmail'],
1350 )
1388 )
1351 if b'time' in commit:
1389 if b'time' in commit:
1352 meta[b'date'] = b'%d 0' % int(commit[b'time'])
1390 meta[b'date'] = b'%d 0' % int(commit[b'time'])
1353 if b'branch' in commit:
1391 if b'branch' in commit:
1354 meta[b'branch'] = commit[b'branch']
1392 meta[b'branch'] = commit[b'branch']
1355 node = commit.get(b'commit', commit.get(b'rev'))
1393 node = commit.get(b'commit', commit.get(b'rev'))
1356 if node:
1394 if node:
1357 meta[b'node'] = node
1395 meta[b'node'] = node
1358 if len(commit.get(b'parents', ())) >= 1:
1396 if len(commit.get(b'parents', ())) >= 1:
1359 meta[b'parent'] = commit[b'parents'][0]
1397 meta[b'parent'] = commit[b'parents'][0]
1360 else:
1398 else:
1361 meta = {}
1399 meta = {}
1362 if b'date' not in meta and b'dateCreated' in diff:
1400 if b'date' not in meta and b'dateCreated' in diff:
1363 meta[b'date'] = b'%s 0' % diff[b'dateCreated']
1401 meta[b'date'] = b'%s 0' % diff[b'dateCreated']
1364 if b'branch' not in meta and diff.get(b'branch'):
1402 if b'branch' not in meta and diff.get(b'branch'):
1365 meta[b'branch'] = diff[b'branch']
1403 meta[b'branch'] = diff[b'branch']
1366 if b'parent' not in meta and diff.get(b'sourceControlBaseRevision'):
1404 if b'parent' not in meta and diff.get(b'sourceControlBaseRevision'):
1367 meta[b'parent'] = diff[b'sourceControlBaseRevision']
1405 meta[b'parent'] = diff[b'sourceControlBaseRevision']
1368 return meta
1406 return meta
1369
1407
1370
1408
1371 def readpatch(repo, drevs, write):
1409 def readpatch(repo, drevs, write):
1372 """generate plain-text patch readable by 'hg import'
1410 """generate plain-text patch readable by 'hg import'
1373
1411
1374 write is usually ui.write. drevs is what "querydrev" returns, results of
1412 write is usually ui.write. drevs is what "querydrev" returns, results of
1375 "differential.query".
1413 "differential.query".
1376 """
1414 """
1377 # Prefetch hg:meta property for all diffs
1415 # Prefetch hg:meta property for all diffs
1378 diffids = sorted(set(max(int(v) for v in drev[b'diffs']) for drev in drevs))
1416 diffids = sorted(set(max(int(v) for v in drev[b'diffs']) for drev in drevs))
1379 diffs = callconduit(repo.ui, b'differential.querydiffs', {b'ids': diffids})
1417 diffs = callconduit(repo.ui, b'differential.querydiffs', {b'ids': diffids})
1380
1418
1381 # Generate patch for each drev
1419 # Generate patch for each drev
1382 for drev in drevs:
1420 for drev in drevs:
1383 repo.ui.note(_(b'reading D%s\n') % drev[b'id'])
1421 repo.ui.note(_(b'reading D%s\n') % drev[b'id'])
1384
1422
1385 diffid = max(int(v) for v in drev[b'diffs'])
1423 diffid = max(int(v) for v in drev[b'diffs'])
1386 body = callconduit(
1424 body = callconduit(
1387 repo.ui, b'differential.getrawdiff', {b'diffID': diffid}
1425 repo.ui, b'differential.getrawdiff', {b'diffID': diffid}
1388 )
1426 )
1389 desc = getdescfromdrev(drev)
1427 desc = getdescfromdrev(drev)
1390 header = b'# HG changeset patch\n'
1428 header = b'# HG changeset patch\n'
1391
1429
1392 # Try to preserve metadata from hg:meta property. Write hg patch
1430 # Try to preserve metadata from hg:meta property. Write hg patch
1393 # headers that can be read by the "import" command. See patchheadermap
1431 # headers that can be read by the "import" command. See patchheadermap
1394 # and extract in mercurial/patch.py for supported headers.
1432 # and extract in mercurial/patch.py for supported headers.
1395 meta = getdiffmeta(diffs[b'%d' % diffid])
1433 meta = getdiffmeta(diffs[b'%d' % diffid])
1396 for k in _metanamemap.keys():
1434 for k in _metanamemap.keys():
1397 if k in meta:
1435 if k in meta:
1398 header += b'# %s %s\n' % (_metanamemap[k], meta[k])
1436 header += b'# %s %s\n' % (_metanamemap[k], meta[k])
1399
1437
1400 content = b'%s%s\n%s' % (header, desc, body)
1438 content = b'%s%s\n%s' % (header, desc, body)
1401 write(content)
1439 write(content)
1402
1440
1403
1441
1404 @vcrcommand(
1442 @vcrcommand(
1405 b'phabread',
1443 b'phabread',
1406 [(b'', b'stack', False, _(b'read dependencies'))],
1444 [(b'', b'stack', False, _(b'read dependencies'))],
1407 _(b'DREVSPEC [OPTIONS]'),
1445 _(b'DREVSPEC [OPTIONS]'),
1408 helpcategory=command.CATEGORY_IMPORT_EXPORT,
1446 helpcategory=command.CATEGORY_IMPORT_EXPORT,
1409 )
1447 )
1410 def phabread(ui, repo, spec, **opts):
1448 def phabread(ui, repo, spec, **opts):
1411 """print patches from Phabricator suitable for importing
1449 """print patches from Phabricator suitable for importing
1412
1450
1413 DREVSPEC could be a Differential Revision identity, like ``D123``, or just
1451 DREVSPEC could be a Differential Revision identity, like ``D123``, or just
1414 the number ``123``. It could also have common operators like ``+``, ``-``,
1452 the number ``123``. It could also have common operators like ``+``, ``-``,
1415 ``&``, ``(``, ``)`` for complex queries. Prefix ``:`` could be used to
1453 ``&``, ``(``, ``)`` for complex queries. Prefix ``:`` could be used to
1416 select a stack.
1454 select a stack.
1417
1455
1418 ``abandoned``, ``accepted``, ``closed``, ``needsreview``, ``needsrevision``
1456 ``abandoned``, ``accepted``, ``closed``, ``needsreview``, ``needsrevision``
1419 could be used to filter patches by status. For performance reason, they
1457 could be used to filter patches by status. For performance reason, they
1420 only represent a subset of non-status selections and cannot be used alone.
1458 only represent a subset of non-status selections and cannot be used alone.
1421
1459
1422 For example, ``:D6+8-(2+D4)`` selects a stack up to D6, plus D8 and exclude
1460 For example, ``:D6+8-(2+D4)`` selects a stack up to D6, plus D8 and exclude
1423 D2 and D4. ``:D9 & needsreview`` selects "Needs Review" revisions in a
1461 D2 and D4. ``:D9 & needsreview`` selects "Needs Review" revisions in a
1424 stack up to D9.
1462 stack up to D9.
1425
1463
1426 If --stack is given, follow dependencies information and read all patches.
1464 If --stack is given, follow dependencies information and read all patches.
1427 It is equivalent to the ``:`` operator.
1465 It is equivalent to the ``:`` operator.
1428 """
1466 """
1429 opts = pycompat.byteskwargs(opts)
1467 opts = pycompat.byteskwargs(opts)
1430 if opts.get(b'stack'):
1468 if opts.get(b'stack'):
1431 spec = b':(%s)' % spec
1469 spec = b':(%s)' % spec
1432 drevs = querydrev(repo, spec)
1470 drevs = querydrev(repo, spec)
1433 readpatch(repo, drevs, ui.write)
1471 readpatch(repo, drevs, ui.write)
1434
1472
1435
1473
1436 @vcrcommand(
1474 @vcrcommand(
1437 b'phabupdate',
1475 b'phabupdate',
1438 [
1476 [
1439 (b'', b'accept', False, _(b'accept revisions')),
1477 (b'', b'accept', False, _(b'accept revisions')),
1440 (b'', b'reject', False, _(b'reject revisions')),
1478 (b'', b'reject', False, _(b'reject revisions')),
1441 (b'', b'abandon', False, _(b'abandon revisions')),
1479 (b'', b'abandon', False, _(b'abandon revisions')),
1442 (b'', b'reclaim', False, _(b'reclaim revisions')),
1480 (b'', b'reclaim', False, _(b'reclaim revisions')),
1443 (b'm', b'comment', b'', _(b'comment on the last revision')),
1481 (b'm', b'comment', b'', _(b'comment on the last revision')),
1444 ],
1482 ],
1445 _(b'DREVSPEC [OPTIONS]'),
1483 _(b'DREVSPEC [OPTIONS]'),
1446 helpcategory=command.CATEGORY_IMPORT_EXPORT,
1484 helpcategory=command.CATEGORY_IMPORT_EXPORT,
1447 )
1485 )
1448 def phabupdate(ui, repo, spec, **opts):
1486 def phabupdate(ui, repo, spec, **opts):
1449 """update Differential Revision in batch
1487 """update Differential Revision in batch
1450
1488
1451 DREVSPEC selects revisions. See :hg:`help phabread` for its usage.
1489 DREVSPEC selects revisions. See :hg:`help phabread` for its usage.
1452 """
1490 """
1453 opts = pycompat.byteskwargs(opts)
1491 opts = pycompat.byteskwargs(opts)
1454 flags = [n for n in b'accept reject abandon reclaim'.split() if opts.get(n)]
1492 flags = [n for n in b'accept reject abandon reclaim'.split() if opts.get(n)]
1455 if len(flags) > 1:
1493 if len(flags) > 1:
1456 raise error.Abort(_(b'%s cannot be used together') % b', '.join(flags))
1494 raise error.Abort(_(b'%s cannot be used together') % b', '.join(flags))
1457
1495
1458 actions = []
1496 actions = []
1459 for f in flags:
1497 for f in flags:
1460 actions.append({b'type': f, b'value': b'true'})
1498 actions.append({b'type': f, b'value': b'true'})
1461
1499
1462 drevs = querydrev(repo, spec)
1500 drevs = querydrev(repo, spec)
1463 for i, drev in enumerate(drevs):
1501 for i, drev in enumerate(drevs):
1464 if i + 1 == len(drevs) and opts.get(b'comment'):
1502 if i + 1 == len(drevs) and opts.get(b'comment'):
1465 actions.append({b'type': b'comment', b'value': opts[b'comment']})
1503 actions.append({b'type': b'comment', b'value': opts[b'comment']})
1466 if actions:
1504 if actions:
1467 params = {
1505 params = {
1468 b'objectIdentifier': drev[b'phid'],
1506 b'objectIdentifier': drev[b'phid'],
1469 b'transactions': actions,
1507 b'transactions': actions,
1470 }
1508 }
1471 callconduit(ui, b'differential.revision.edit', params)
1509 callconduit(ui, b'differential.revision.edit', params)
1472
1510
1473
1511
1474 @eh.templatekeyword(b'phabreview', requires={b'ctx'})
1512 @eh.templatekeyword(b'phabreview', requires={b'ctx'})
1475 def template_review(context, mapping):
1513 def template_review(context, mapping):
1476 """:phabreview: Object describing the review for this changeset.
1514 """:phabreview: Object describing the review for this changeset.
1477 Has attributes `url` and `id`.
1515 Has attributes `url` and `id`.
1478 """
1516 """
1479 ctx = context.resource(mapping, b'ctx')
1517 ctx = context.resource(mapping, b'ctx')
1480 m = _differentialrevisiondescre.search(ctx.description())
1518 m = _differentialrevisiondescre.search(ctx.description())
1481 if m:
1519 if m:
1482 return templateutil.hybriddict(
1520 return templateutil.hybriddict(
1483 {b'url': m.group(r'url'), b'id': b"D%s" % m.group(r'id'),}
1521 {b'url': m.group(r'url'), b'id': b"D%s" % m.group(r'id'),}
1484 )
1522 )
1485 else:
1523 else:
1486 tags = ctx.repo().nodetags(ctx.node())
1524 tags = ctx.repo().nodetags(ctx.node())
1487 for t in tags:
1525 for t in tags:
1488 if _differentialrevisiontagre.match(t):
1526 if _differentialrevisiontagre.match(t):
1489 url = ctx.repo().ui.config(b'phabricator', b'url')
1527 url = ctx.repo().ui.config(b'phabricator', b'url')
1490 if not url.endswith(b'/'):
1528 if not url.endswith(b'/'):
1491 url += b'/'
1529 url += b'/'
1492 url += t
1530 url += t
1493
1531
1494 return templateutil.hybriddict({b'url': url, b'id': t,})
1532 return templateutil.hybriddict({b'url': url, b'id': t,})
1495 return None
1533 return None
General Comments 0
You need to be logged in to leave comments. Login now