hgext/phabricator.py
author Gregory Szorc <gregory.szorc@gmail.com>
Sun, 06 Oct 2019 16:55:18 -0400
changeset 43089 c59eb1560c44
parent 43080 86e4daa2d54c
child 43094 e8cf9ad52a78
permissions -rw-r--r--
py3: manually import getattr where it is needed The march continues. Differential Revision: https://phab.mercurial-scm.org/D7009
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
     1
# phabricator.py - simple Phabricator integration
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
     2
#
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
     3
# Copyright 2017 Facebook, Inc.
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
     4
#
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
39671
87539f615b87 phabricator: mark extension as experimental for now
Augie Fackler <raf@durin42.com>
parents: 39670
diff changeset
     7
"""simple Phabricator integration (EXPERIMENTAL)
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
     8
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
     9
This extension provides a ``phabsend`` command which sends a stack of
33993
07ffff841863 phabsend: make --amend the default
Jun Wu <quark@fb.com>
parents: 33992
diff changeset
    10
changesets to Phabricator, and a ``phabread`` command which prints a stack of
07ffff841863 phabsend: make --amend the default
Jun Wu <quark@fb.com>
parents: 33992
diff changeset
    11
revisions in a format suitable for :hg:`import`, and a ``phabupdate`` command
07ffff841863 phabsend: make --amend the default
Jun Wu <quark@fb.com>
parents: 33992
diff changeset
    12
to update statuses in batch.
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    13
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    14
By default, Phabricator requires ``Test Plan`` which might prevent some
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    15
changeset from being sent. The requirement could be disabled by changing
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    16
``differential.require-test-plan-field`` config server side.
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    17
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    18
Config::
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    19
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    20
    [phabricator]
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    21
    # Phabricator URL
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    22
    url = https://phab.example.com/
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    23
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    24
    # Repo callsign. If a repo has a URL https://$HOST/diffusion/FOO, then its
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    25
    # callsign is "FOO".
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    26
    callsign = FOO
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
    27
34081
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
    28
    # curl command to use. If not set (default), use builtin HTTP library to
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
    29
    # communicate. If set, use the specified curl command. This could be useful
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
    30
    # if you need to specify advanced options that is not easily supported by
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
    31
    # the internal library.
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
    32
    curlcmd = curl --connect-timeout 2 --retry 3 --silent
36795
4397909f82d3 phabricator: specify API tokens per host, rather than per repo
Tom Prince <mozilla@hocat.ca>
parents: 36543
diff changeset
    33
38039
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
    34
    [auth]
38040
71cf20d47f25 phabricator: split auth.url into the standard auth.schemes and auth.prefix
Matt Harbison <matt_harbison@yahoo.com>
parents: 38039
diff changeset
    35
    example.schemes = https
71cf20d47f25 phabricator: split auth.url into the standard auth.schemes and auth.prefix
Matt Harbison <matt_harbison@yahoo.com>
parents: 38039
diff changeset
    36
    example.prefix = phab.example.com
71cf20d47f25 phabricator: split auth.url into the standard auth.schemes and auth.prefix
Matt Harbison <matt_harbison@yahoo.com>
parents: 38039
diff changeset
    37
36795
4397909f82d3 phabricator: specify API tokens per host, rather than per repo
Tom Prince <mozilla@hocat.ca>
parents: 36543
diff changeset
    38
    # API token. Get it from https://$HOST/conduit/login/
38039
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
    39
    example.phabtoken = cli-xxxxxxxxxxxxxxxxxxxxxxxxxxxx
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    40
"""
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    41
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    42
from __future__ import absolute_import
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    43
41083
9d35ae3d9999 phabricator: ensure that the return of urlopener.open() is closed
Matt Harbison <matt_harbison@yahoo.com>
parents: 41081
diff changeset
    44
import contextlib
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
    45
import itertools
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    46
import json
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
    47
import operator
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    48
import re
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    49
33443
e48082e0a8d5 phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents: 33442
diff changeset
    50
from mercurial.node import bin, nullid
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    51
from mercurial.i18n import _
43089
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43080
diff changeset
    52
from mercurial.pycompat import getattr
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    53
from mercurial import (
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
    54
    cmdutil,
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
    55
    context,
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    56
    encoding,
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    57
    error,
42975
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
    58
    exthelper,
38040
71cf20d47f25 phabricator: split auth.url into the standard auth.schemes and auth.prefix
Matt Harbison <matt_harbison@yahoo.com>
parents: 38039
diff changeset
    59
    httpconnection as httpconnectionmod,
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    60
    mdiff,
33761
e6d8ee3c9ec3 obsutil: rename allprecursors into allpredecessors
Boris Feld <boris.feld@octobus.net>
parents: 33718
diff changeset
    61
    obsutil,
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
    62
    parser,
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    63
    patch,
41163
0101a35deae2 phabricator: warn if unable to amend, instead of aborting after posting
Matt Harbison <matt_harbison@yahoo.com>
parents: 41083
diff changeset
    64
    phases,
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
    65
    pycompat,
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    66
    scmutil,
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
    67
    smartset,
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    68
    tags,
42226
289d82a070e9 phabricator: use templatefilters.json in writediffproperties
Ian Moody <moz-ian@perix.co.uk>
parents: 41976
diff changeset
    69
    templatefilters,
39670
4057e38bba76 phabricator: fix templating bug by using hybriddict
Augie Fackler <raf@durin42.com>
parents: 39668
diff changeset
    70
    templateutil,
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    71
    url as urlmod,
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    72
    util,
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    73
)
37123
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36826
diff changeset
    74
from mercurial.utils import (
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36826
diff changeset
    75
    procutil,
37800
6cf5f5b4eb57 phabricator: specify some metadata compatibly with arc
Tom Prince <mozilla@hocat.ca>
parents: 37123
diff changeset
    76
    stringutil,
37123
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36826
diff changeset
    77
)
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    78
39751
c0c703861b60 phabricator: add testedwith boilerplate
Yuya Nishihara <yuya@tcha.org>
parents: 39671
diff changeset
    79
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
c0c703861b60 phabricator: add testedwith boilerplate
Yuya Nishihara <yuya@tcha.org>
parents: 39671
diff changeset
    80
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
c0c703861b60 phabricator: add testedwith boilerplate
Yuya Nishihara <yuya@tcha.org>
parents: 39671
diff changeset
    81
# be specifying the version(s) of Mercurial they are tested with, or
c0c703861b60 phabricator: add testedwith boilerplate
Yuya Nishihara <yuya@tcha.org>
parents: 39671
diff changeset
    82
# leave the attribute unspecified.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    83
testedwith = b'ships-with-hg-core'
39751
c0c703861b60 phabricator: add testedwith boilerplate
Yuya Nishihara <yuya@tcha.org>
parents: 39671
diff changeset
    84
42975
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
    85
eh = exthelper.exthelper()
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    86
42975
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
    87
cmdtable = eh.cmdtable
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
    88
command = eh.command
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
    89
configtable = eh.configtable
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
    90
templatekeyword = eh.templatekeyword
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
    91
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
    92
# developer config: phabricator.batchsize
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
    93
eh.configitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
    94
    b'phabricator', b'batchsize', default=12,
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
    95
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
    96
eh.configitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
    97
    b'phabricator', b'callsign', default=None,
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
    98
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
    99
eh.configitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   100
    b'phabricator', b'curlcmd', default=None,
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   101
)
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   102
# developer config: phabricator.repophid
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   103
eh.configitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   104
    b'phabricator', b'repophid', default=None,
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   105
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   106
eh.configitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   107
    b'phabricator', b'url', default=None,
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   108
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   109
eh.configitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   110
    b'phabsend', b'confirm', default=False,
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   111
)
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   112
34080
941c33cfde81 phabricator: standardize colors
Jun Wu <quark@fb.com>
parents: 33996
diff changeset
   113
colortable = {
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   114
    b'phabricator.action.created': b'green',
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   115
    b'phabricator.action.skipped': b'magenta',
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   116
    b'phabricator.action.updated': b'magenta',
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   117
    b'phabricator.desc': b'',
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   118
    b'phabricator.drev': b'bold',
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   119
    b'phabricator.node': b'',
34080
941c33cfde81 phabricator: standardize colors
Jun Wu <quark@fb.com>
parents: 33996
diff changeset
   120
}
941c33cfde81 phabricator: standardize colors
Jun Wu <quark@fb.com>
parents: 33996
diff changeset
   121
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   122
_VCR_FLAGS = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   123
    (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   124
        b'',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   125
        b'test-vcr',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   126
        b'',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   127
        _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   128
            b'Path to a vcr file. If nonexistent, will record a new vcr transcript'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   129
            b', otherwise will mock all http requests using the specified vcr file.'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   130
            b' (ADVANCED)'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   131
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   132
    ),
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   133
]
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   134
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   135
42451
16312ea45a8b phabricator: make `hg debugcallconduit` work outside a hg repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42450
diff changeset
   136
def vcrcommand(name, flags, spec, helpcategory=None, optionalrepo=False):
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   137
    fullflags = flags + _VCR_FLAGS
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   138
42295
af13e2088f77 phabricator: add custom vcr matcher to match request bodies
Ian Moody <moz-ian@perix.co.uk>
parents: 42286
diff changeset
   139
    def hgmatcher(r1, r2):
af13e2088f77 phabricator: add custom vcr matcher to match request bodies
Ian Moody <moz-ian@perix.co.uk>
parents: 42286
diff changeset
   140
        if r1.uri != r2.uri or r1.method != r2.method:
af13e2088f77 phabricator: add custom vcr matcher to match request bodies
Ian Moody <moz-ian@perix.co.uk>
parents: 42286
diff changeset
   141
            return False
af13e2088f77 phabricator: add custom vcr matcher to match request bodies
Ian Moody <moz-ian@perix.co.uk>
parents: 42286
diff changeset
   142
        r1params = r1.body.split(b'&')
af13e2088f77 phabricator: add custom vcr matcher to match request bodies
Ian Moody <moz-ian@perix.co.uk>
parents: 42286
diff changeset
   143
        r2params = r2.body.split(b'&')
af13e2088f77 phabricator: add custom vcr matcher to match request bodies
Ian Moody <moz-ian@perix.co.uk>
parents: 42286
diff changeset
   144
        return set(r1params) == set(r2params)
af13e2088f77 phabricator: add custom vcr matcher to match request bodies
Ian Moody <moz-ian@perix.co.uk>
parents: 42286
diff changeset
   145
42459
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   146
    def sanitiserequest(request):
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   147
        request.body = re.sub(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   148
            br'cli-[a-z0-9]+', br'cli-hahayouwish', request.body
42459
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   149
        )
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   150
        return request
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   151
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   152
    def sanitiseresponse(response):
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   153
        if r'set-cookie' in response[r'headers']:
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   154
            del response[r'headers'][r'set-cookie']
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   155
        return response
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   156
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   157
    def decorate(fn):
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   158
        def inner(*args, **kwargs):
41903
2bad8f92cebf py3: use fsencode for vcr recording paths and strings for custom_patches args
Ian Moody <moz-ian@perix.co.uk>
parents: 41902
diff changeset
   159
            cassette = pycompat.fsdecode(kwargs.pop(r'test_vcr', None))
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   160
            if cassette:
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   161
                import hgdemandimport
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   162
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   163
                with hgdemandimport.deactivated():
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   164
                    import vcr as vcrmod
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   165
                    import vcr.stubs as stubs
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   166
40378
b015f30a91fb phabricator: do more of the VCR work in demandimport.deactivated()
Augie Fackler <augie@google.com>
parents: 40151
diff changeset
   167
                    vcr = vcrmod.VCR(
b015f30a91fb phabricator: do more of the VCR work in demandimport.deactivated()
Augie Fackler <augie@google.com>
parents: 40151
diff changeset
   168
                        serializer=r'json',
42459
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   169
                        before_record_request=sanitiserequest,
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   170
                        before_record_response=sanitiseresponse,
40378
b015f30a91fb phabricator: do more of the VCR work in demandimport.deactivated()
Augie Fackler <augie@google.com>
parents: 40151
diff changeset
   171
                        custom_patches=[
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   172
                            (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   173
                                urlmod,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   174
                                r'httpconnection',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   175
                                stubs.VCRHTTPConnection,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   176
                            ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   177
                            (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   178
                                urlmod,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   179
                                r'httpsconnection',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   180
                                stubs.VCRHTTPSConnection,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   181
                            ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   182
                        ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   183
                    )
42295
af13e2088f77 phabricator: add custom vcr matcher to match request bodies
Ian Moody <moz-ian@perix.co.uk>
parents: 42286
diff changeset
   184
                    vcr.register_matcher(r'hgmatcher', hgmatcher)
af13e2088f77 phabricator: add custom vcr matcher to match request bodies
Ian Moody <moz-ian@perix.co.uk>
parents: 42286
diff changeset
   185
                    with vcr.use_cassette(cassette, match_on=[r'hgmatcher']):
40378
b015f30a91fb phabricator: do more of the VCR work in demandimport.deactivated()
Augie Fackler <augie@google.com>
parents: 40151
diff changeset
   186
                        return fn(*args, **kwargs)
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   187
            return fn(*args, **kwargs)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   188
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   189
        inner.__name__ = fn.__name__
40435
7e2c58b08e74 phabricator: ensure the command summaries are available in extension help
Matt Harbison <matt_harbison@yahoo.com>
parents: 40378
diff changeset
   190
        inner.__doc__ = fn.__doc__
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   191
        return command(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   192
            name,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   193
            fullflags,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   194
            spec,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   195
            helpcategory=helpcategory,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   196
            optionalrepo=optionalrepo,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   197
        )(inner)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   198
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   199
    return decorate
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   200
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   201
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   202
def urlencodenested(params):
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   203
    """like urlencode, but works with nested parameters.
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   204
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   205
    For example, if params is {'a': ['b', 'c'], 'd': {'e': 'f'}}, it will be
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   206
    flattened to {'a[0]': 'b', 'a[1]': 'c', 'd[e]': 'f'} and then passed to
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   207
    urlencode. Note: the encoding is consistent with PHP's http_build_query.
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   208
    """
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   209
    flatparams = util.sortdict()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   210
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   211
    def process(prefix, obj):
40473
d7d3164e6a31 phabricator: properly encode boolean types in the request body
Matt Harbison <matt_harbison@yahoo.com>
parents: 40435
diff changeset
   212
        if isinstance(obj, bool):
d7d3164e6a31 phabricator: properly encode boolean types in the request body
Matt Harbison <matt_harbison@yahoo.com>
parents: 40435
diff changeset
   213
            obj = {True: b'true', False: b'false'}[obj]  # Python -> PHP form
41905
47125193bad0 py3: convert indexes into bytes when enumerating lists in urlencodenested
Ian Moody <moz-ian@perix.co.uk>
parents: 41904
diff changeset
   214
        lister = lambda l: [(b'%d' % k, v) for k, v in enumerate(l)]
47125193bad0 py3: convert indexes into bytes when enumerating lists in urlencodenested
Ian Moody <moz-ian@perix.co.uk>
parents: 41904
diff changeset
   215
        items = {list: lister, dict: lambda x: x.items()}.get(type(obj))
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   216
        if items is None:
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   217
            flatparams[prefix] = obj
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   218
        else:
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   219
            for k, v in items(obj):
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   220
                if prefix:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   221
                    process(b'%s[%s]' % (prefix, k), v)
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   222
                else:
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   223
                    process(k, v)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   224
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   225
    process(b'', params)
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   226
    return util.urlreq.urlencode(flatparams)
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   227
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   228
42449
500b64c5d991 phabricator: pass ui into readurltoken instead of passing repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42447
diff changeset
   229
def readurltoken(ui):
38039
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
   230
    """return conduit url, token and make sure they exist
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
   231
38040
71cf20d47f25 phabricator: split auth.url into the standard auth.schemes and auth.prefix
Matt Harbison <matt_harbison@yahoo.com>
parents: 38039
diff changeset
   232
    Currently read from [auth] config section. In the future, it might
38039
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
   233
    make sense to read from .arcconfig and .arcrc as well.
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
   234
    """
42449
500b64c5d991 phabricator: pass ui into readurltoken instead of passing repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42447
diff changeset
   235
    url = ui.config(b'phabricator', b'url')
38039
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
   236
    if not url:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   237
        raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   238
            _(b'config %s.%s is required') % (b'phabricator', b'url')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   239
        )
38039
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
   240
42449
500b64c5d991 phabricator: pass ui into readurltoken instead of passing repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42447
diff changeset
   241
    res = httpconnectionmod.readauthforuri(ui, url, util.url(url).user)
38040
71cf20d47f25 phabricator: split auth.url into the standard auth.schemes and auth.prefix
Matt Harbison <matt_harbison@yahoo.com>
parents: 38039
diff changeset
   242
    token = None
38039
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
   243
38040
71cf20d47f25 phabricator: split auth.url into the standard auth.schemes and auth.prefix
Matt Harbison <matt_harbison@yahoo.com>
parents: 38039
diff changeset
   244
    if res:
71cf20d47f25 phabricator: split auth.url into the standard auth.schemes and auth.prefix
Matt Harbison <matt_harbison@yahoo.com>
parents: 38039
diff changeset
   245
        group, auth = res
71cf20d47f25 phabricator: split auth.url into the standard auth.schemes and auth.prefix
Matt Harbison <matt_harbison@yahoo.com>
parents: 38039
diff changeset
   246
42449
500b64c5d991 phabricator: pass ui into readurltoken instead of passing repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42447
diff changeset
   247
        ui.debug(b"using auth.%s.* for authentication\n" % group)
38040
71cf20d47f25 phabricator: split auth.url into the standard auth.schemes and auth.prefix
Matt Harbison <matt_harbison@yahoo.com>
parents: 38039
diff changeset
   248
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   249
        token = auth.get(b'phabtoken')
38039
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
   250
36795
4397909f82d3 phabricator: specify API tokens per host, rather than per repo
Tom Prince <mozilla@hocat.ca>
parents: 36543
diff changeset
   251
    if not token:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   252
        raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   253
            _(b'Can\'t find conduit token associated to %s') % (url,)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   254
        )
36795
4397909f82d3 phabricator: specify API tokens per host, rather than per repo
Tom Prince <mozilla@hocat.ca>
parents: 36543
diff changeset
   255
4397909f82d3 phabricator: specify API tokens per host, rather than per repo
Tom Prince <mozilla@hocat.ca>
parents: 36543
diff changeset
   256
    return url, token
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   257
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   258
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   259
def callconduit(ui, name, params):
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   260
    """call Conduit API, params is a dict. return json.loads result, or None"""
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   261
    host, token = readurltoken(ui)
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   262
    url, authinfo = util.url(b'/'.join([host, b'api', name])).authinfo()
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   263
    ui.debug(b'Conduit Call: %s %s\n' % (url, pycompat.byterepr(params)))
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   264
    params = params.copy()
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   265
    params[b'api.token'] = token
34081
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
   266
    data = urlencodenested(params)
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   267
    curlcmd = ui.config(b'phabricator', b'curlcmd')
34081
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
   268
    if curlcmd:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   269
        sin, sout = procutil.popen2(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   270
            b'%s -d @- %s' % (curlcmd, procutil.shellquote(url))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   271
        )
34081
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
   272
        sin.write(data)
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
   273
        sin.close()
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
   274
        body = sout.read()
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
   275
    else:
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   276
        urlopener = urlmod.opener(ui, authinfo)
41906
a98dabdb5a7b py3: convert URL to str before passing it to request
Ian Moody <moz-ian@perix.co.uk>
parents: 41905
diff changeset
   277
        request = util.urlreq.request(pycompat.strurl(url), data=data)
41083
9d35ae3d9999 phabricator: ensure that the return of urlopener.open() is closed
Matt Harbison <matt_harbison@yahoo.com>
parents: 41081
diff changeset
   278
        with contextlib.closing(urlopener.open(request)) as rsp:
9d35ae3d9999 phabricator: ensure that the return of urlopener.open() is closed
Matt Harbison <matt_harbison@yahoo.com>
parents: 41081
diff changeset
   279
            body = rsp.read()
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   280
    ui.debug(b'Conduit Response: %s\n' % body)
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   281
    parsed = pycompat.rapply(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   282
        lambda x: encoding.unitolocal(x)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   283
        if isinstance(x, pycompat.unicode)
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   284
        else x,
43047
0f90c2d2d7e8 py3: fix phabricator's use of json.loads() for py3.5
Ian Moody <moz-ian@perix.co.uk>
parents: 42975
diff changeset
   285
        # json.loads only accepts bytes from py3.6+
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   286
        json.loads(encoding.unifromlocal(body)),
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   287
    )
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   288
    if parsed.get(b'error_code'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   289
        msg = _(b'Conduit Error (%s): %s') % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   290
            parsed[b'error_code'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   291
            parsed[b'error_info'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   292
        )
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   293
        raise error.Abort(msg)
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   294
    return parsed[b'result']
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   295
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   296
42451
16312ea45a8b phabricator: make `hg debugcallconduit` work outside a hg repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42450
diff changeset
   297
@vcrcommand(b'debugcallconduit', [], _(b'METHOD'), optionalrepo=True)
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   298
def debugcallconduit(ui, repo, name):
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   299
    """call Conduit API
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   300
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   301
    Call parameters are read from stdin as a JSON blob. Result will be written
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   302
    to stdout as a JSON blob.
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   303
    """
41976
99e00e5c4746 py3: convert to/from bytes/unicode for json.(dump|load)s in debugcallconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41975
diff changeset
   304
    # json.loads only accepts bytes from 3.6+
99e00e5c4746 py3: convert to/from bytes/unicode for json.(dump|load)s in debugcallconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41975
diff changeset
   305
    rawparams = encoding.unifromlocal(ui.fin.read())
99e00e5c4746 py3: convert to/from bytes/unicode for json.(dump|load)s in debugcallconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41975
diff changeset
   306
    # json.loads only returns unicode strings
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   307
    params = pycompat.rapply(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   308
        lambda x: encoding.unitolocal(x)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   309
        if isinstance(x, pycompat.unicode)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   310
        else x,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   311
        json.loads(rawparams),
41976
99e00e5c4746 py3: convert to/from bytes/unicode for json.(dump|load)s in debugcallconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41975
diff changeset
   312
    )
99e00e5c4746 py3: convert to/from bytes/unicode for json.(dump|load)s in debugcallconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41975
diff changeset
   313
    # json.dumps only accepts unicode strings
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   314
    result = pycompat.rapply(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   315
        lambda x: encoding.unifromlocal(x) if isinstance(x, bytes) else x,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   316
        callconduit(ui, name, params),
41976
99e00e5c4746 py3: convert to/from bytes/unicode for json.(dump|load)s in debugcallconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41975
diff changeset
   317
    )
99e00e5c4746 py3: convert to/from bytes/unicode for json.(dump|load)s in debugcallconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41975
diff changeset
   318
    s = json.dumps(result, sort_keys=True, indent=2, separators=(u',', u': '))
99e00e5c4746 py3: convert to/from bytes/unicode for json.(dump|load)s in debugcallconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41975
diff changeset
   319
    ui.write(b'%s\n' % encoding.unitolocal(s))
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   320
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   321
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   322
def getrepophid(repo):
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   323
    """given callsign, return repository PHID or None"""
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   324
    # developer config: phabricator.repophid
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   325
    repophid = repo.ui.config(b'phabricator', b'repophid')
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   326
    if repophid:
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   327
        return repophid
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   328
    callsign = repo.ui.config(b'phabricator', b'callsign')
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   329
    if not callsign:
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   330
        return None
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   331
    query = callconduit(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   332
        repo.ui,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   333
        b'diffusion.repository.search',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   334
        {b'constraints': {b'callsigns': [callsign]}},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   335
    )
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   336
    if len(query[b'data']) == 0:
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   337
        return None
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   338
    repophid = query[b'data'][0][b'phid']
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   339
    repo.ui.setconfig(b'phabricator', b'repophid', repophid)
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   340
    return repophid
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   341
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   342
41546
bd3f03d8cc9f global: use raw strings for regular expressions with escapes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41387
diff changeset
   343
_differentialrevisiontagre = re.compile(br'\AD([1-9][0-9]*)\Z')
33263
ed61189763ef phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents: 33202
diff changeset
   344
_differentialrevisiondescre = re.compile(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   345
    br'^Differential Revision:\s*(?P<url>(?:.*)D(?P<id>[1-9][0-9]*))$', re.M
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   346
)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   347
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   348
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   349
def getoldnodedrevmap(repo, nodelist):
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   350
    """find previous nodes that has been sent to Phabricator
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   351
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   352
    return {node: (oldnode, Differential diff, Differential Revision ID)}
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   353
    for node in nodelist with known previous sent versions, or associated
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   354
    Differential Revision IDs. ``oldnode`` and ``Differential diff`` could
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   355
    be ``None``.
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   356
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   357
    Examines commit messages like "Differential Revision:" to get the
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   358
    association information.
33263
ed61189763ef phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents: 33202
diff changeset
   359
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   360
    If such commit message line is not found, examines all precursors and their
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   361
    tags. Tags with format like "D1234" are considered a match and the node
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   362
    with that tag, and the number after "D" (ex. 1234) will be returned.
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   363
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   364
    The ``old node``, if not None, is guaranteed to be the last diff of
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   365
    corresponding Differential Revision, and exist in the repo.
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   366
    """
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   367
    unfi = repo.unfiltered()
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   368
    nodemap = unfi.changelog.nodemap
33263
ed61189763ef phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents: 33202
diff changeset
   369
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   370
    result = {}  # {node: (oldnode?, lastdiff?, drev)}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   371
    toconfirm = {}  # {node: (force, {precnode}, drev)}
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   372
    for node in nodelist:
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   373
        ctx = unfi[node]
33443
e48082e0a8d5 phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents: 33442
diff changeset
   374
        # For tags like "D123", put them into "toconfirm" to verify later
33761
e6d8ee3c9ec3 obsutil: rename allprecursors into allpredecessors
Boris Feld <boris.feld@octobus.net>
parents: 33718
diff changeset
   375
        precnodes = list(obsutil.allpredecessors(unfi.obsstore, [node]))
33443
e48082e0a8d5 phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents: 33442
diff changeset
   376
        for n in precnodes:
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   377
            if n in nodemap:
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   378
                for tag in unfi.nodetags(n):
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   379
                    m = _differentialrevisiontagre.match(tag)
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   380
                    if m:
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   381
                        toconfirm[node] = (0, set(precnodes), int(m.group(1)))
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   382
                        continue
33263
ed61189763ef phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents: 33202
diff changeset
   383
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   384
        # Check commit message
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   385
        m = _differentialrevisiondescre.search(ctx.description())
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   386
        if m:
41910
16d050678047 py3: use r'' for group name arguments to MatchObjects in phabricator.py
Ian Moody <moz-ian@perix.co.uk>
parents: 41909
diff changeset
   387
            toconfirm[node] = (1, set(precnodes), int(m.group(r'id')))
33263
ed61189763ef phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents: 33202
diff changeset
   388
33443
e48082e0a8d5 phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents: 33442
diff changeset
   389
    # Double check if tags are genuine by collecting all old nodes from
e48082e0a8d5 phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents: 33442
diff changeset
   390
    # Phabricator, and expect precursors overlap with it.
e48082e0a8d5 phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents: 33442
diff changeset
   391
    if toconfirm:
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   392
        drevs = [drev for force, precs, drev in toconfirm.values()]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   393
        alldiffs = callconduit(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   394
            unfi.ui, b'differential.querydiffs', {b'revisionIDs': drevs}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   395
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   396
        getnode = lambda d: bin(getdiffmeta(d).get(b'node', b'')) or None
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   397
        for newnode, (force, precset, drev) in toconfirm.items():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   398
            diffs = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   399
                d for d in alldiffs.values() if int(d[b'revisionID']) == drev
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   400
            ]
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   401
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   402
            # "precursors" as known by Phabricator
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   403
            phprecset = set(getnode(d) for d in diffs)
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   404
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   405
            # Ignore if precursors (Phabricator and local repo) do not overlap,
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   406
            # and force is not set (when commit message says nothing)
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   407
            if not force and not bool(phprecset & precset):
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   408
                tagname = b'D%d' % drev
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   409
                tags.tag(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   410
                    repo,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   411
                    tagname,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   412
                    nullid,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   413
                    message=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   414
                    user=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   415
                    date=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   416
                    local=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   417
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   418
                unfi.ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   419
                    _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   420
                        b'D%s: local tag removed - does not match '
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   421
                        b'Differential history\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   422
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   423
                    % drev
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   424
                )
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   425
                continue
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   426
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   427
            # Find the last node using Phabricator metadata, and make sure it
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   428
            # exists in the repo
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   429
            oldnode = lastdiff = None
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   430
            if diffs:
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   431
                lastdiff = max(diffs, key=lambda d: int(d[b'id']))
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   432
                oldnode = getnode(lastdiff)
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   433
                if oldnode and oldnode not in nodemap:
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   434
                    oldnode = None
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   435
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   436
            result[newnode] = (oldnode, lastdiff, drev)
33443
e48082e0a8d5 phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents: 33442
diff changeset
   437
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   438
    return result
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   439
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   440
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   441
def getdiff(ctx, diffopts):
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   442
    """plain-text diff without header (user, commit message, etc)"""
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   443
    output = util.stringio()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   444
    for chunk, _label in patch.diffui(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   445
        ctx.repo(), ctx.p1().node(), ctx.node(), None, opts=diffopts
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   446
    ):
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   447
        output.write(chunk)
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   448
    return output.getvalue()
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   449
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   450
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   451
def creatediff(ctx):
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   452
    """create a Differential Diff"""
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   453
    repo = ctx.repo()
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   454
    repophid = getrepophid(repo)
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   455
    # Create a "Differential Diff" via "differential.createrawdiff" API
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   456
    params = {b'diff': getdiff(ctx, mdiff.diffopts(git=True, context=32767))}
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   457
    if repophid:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   458
        params[b'repositoryPHID'] = repophid
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   459
    diff = callconduit(repo.ui, b'differential.createrawdiff', params)
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   460
    if not diff:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   461
        raise error.Abort(_(b'cannot create diff for %s') % ctx)
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   462
    return diff
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   463
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   464
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   465
def writediffproperties(ctx, diff):
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   466
    """write metadata to diff so patches could be applied losslessly"""
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   467
    params = {
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   468
        b'diff_id': diff[b'id'],
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   469
        b'name': b'hg:meta',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   470
        b'data': templatefilters.json(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   471
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   472
                b'user': ctx.user(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   473
                b'date': b'%d %d' % ctx.date(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   474
                b'branch': ctx.branch(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   475
                b'node': ctx.hex(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   476
                b'parent': ctx.p1().hex(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   477
            }
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   478
        ),
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   479
    }
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   480
    callconduit(ctx.repo().ui, b'differential.setdiffproperty', params)
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   481
37800
6cf5f5b4eb57 phabricator: specify some metadata compatibly with arc
Tom Prince <mozilla@hocat.ca>
parents: 37123
diff changeset
   482
    params = {
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   483
        b'diff_id': diff[b'id'],
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   484
        b'name': b'local:commits',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   485
        b'data': templatefilters.json(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   486
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   487
                ctx.hex(): {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   488
                    b'author': stringutil.person(ctx.user()),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   489
                    b'authorEmail': stringutil.email(ctx.user()),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   490
                    b'time': int(ctx.date()[0]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   491
                    b'commit': ctx.hex(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   492
                    b'parents': [ctx.p1().hex()],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   493
                    b'branch': ctx.branch(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   494
                },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   495
            }
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   496
        ),
37800
6cf5f5b4eb57 phabricator: specify some metadata compatibly with arc
Tom Prince <mozilla@hocat.ca>
parents: 37123
diff changeset
   497
    }
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   498
    callconduit(ctx.repo().ui, b'differential.setdiffproperty', params)
37800
6cf5f5b4eb57 phabricator: specify some metadata compatibly with arc
Tom Prince <mozilla@hocat.ca>
parents: 37123
diff changeset
   499
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   500
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   501
def createdifferentialrevision(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   502
    ctx,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   503
    revid=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   504
    parentrevphid=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   505
    oldnode=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   506
    olddiff=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   507
    actions=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   508
    comment=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   509
):
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   510
    """create or update a Differential Revision
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   511
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   512
    If revid is None, create a new Differential Revision, otherwise update
42465
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
   513
    revid. If parentrevphid is not None, set it as a dependency.
33265
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   514
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   515
    If oldnode is not None, check if the patch content (without commit message
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   516
    and metadata) has changed before creating another diff.
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   517
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   518
    If actions is not None, they will be appended to the transaction.
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   519
    """
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   520
    repo = ctx.repo()
33265
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   521
    if oldnode:
33994
27ff2a87d8c0 phabsend: detect patch change with larger context
Jun Wu <quark@fb.com>
parents: 33993
diff changeset
   522
        diffopts = mdiff.diffopts(git=True, context=32767)
33265
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   523
        oldctx = repo.unfiltered()[oldnode]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   524
        neednewdiff = getdiff(ctx, diffopts) != getdiff(oldctx, diffopts)
33265
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   525
    else:
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   526
        neednewdiff = True
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   527
33265
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   528
    transactions = []
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   529
    if neednewdiff:
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   530
        diff = creatediff(ctx)
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   531
        transactions.append({b'type': b'update', b'value': diff[b'phid']})
42447
29528c4235a1 phabricator: add commenting to phabsend for new/updated Diffs
Ian Moody <moz-ian@perix.co.uk>
parents: 42295
diff changeset
   532
        if comment:
29528c4235a1 phabricator: add commenting to phabsend for new/updated Diffs
Ian Moody <moz-ian@perix.co.uk>
parents: 42295
diff changeset
   533
            transactions.append({b'type': b'comment', b'value': comment})
33718
f100354cce52 phabricator: update diff property even if we choose not to create a new diff
Jun Wu <quark@fb.com>
parents: 33717
diff changeset
   534
    else:
f100354cce52 phabricator: update diff property even if we choose not to create a new diff
Jun Wu <quark@fb.com>
parents: 33717
diff changeset
   535
        # Even if we don't need to upload a new diff because the patch content
f100354cce52 phabricator: update diff property even if we choose not to create a new diff
Jun Wu <quark@fb.com>
parents: 33717
diff changeset
   536
        # does not change. We might still need to update its metadata so
f100354cce52 phabricator: update diff property even if we choose not to create a new diff
Jun Wu <quark@fb.com>
parents: 33717
diff changeset
   537
        # pushers could know the correct node metadata.
f100354cce52 phabricator: update diff property even if we choose not to create a new diff
Jun Wu <quark@fb.com>
parents: 33717
diff changeset
   538
        assert olddiff
f100354cce52 phabricator: update diff property even if we choose not to create a new diff
Jun Wu <quark@fb.com>
parents: 33717
diff changeset
   539
        diff = olddiff
f100354cce52 phabricator: update diff property even if we choose not to create a new diff
Jun Wu <quark@fb.com>
parents: 33717
diff changeset
   540
    writediffproperties(ctx, diff)
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   541
42465
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
   542
    # Set the parent Revision every time, so commit re-ordering is picked-up
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
   543
    if parentrevphid:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   544
        transactions.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   545
            {b'type': b'parents.set', b'value': [parentrevphid]}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   546
        )
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   547
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   548
    if actions:
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   549
        transactions += actions
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   550
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   551
    # Parse commit message and update related fields.
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   552
    desc = ctx.description()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   553
    info = callconduit(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   554
        repo.ui, b'differential.parsecommitmessage', {b'corpus': desc}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   555
    )
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   556
    for k, v in info[b'fields'].items():
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   557
        if k in [b'title', b'summary', b'testPlan']:
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   558
            transactions.append({b'type': k, b'value': v})
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   559
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   560
    params = {b'transactions': transactions}
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   561
    if revid is not None:
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   562
        # Update an existing Differential Revision
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   563
        params[b'objectIdentifier'] = revid
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   564
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   565
    revision = callconduit(repo.ui, b'differential.revision.edit', params)
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   566
    if not revision:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   567
        raise error.Abort(_(b'cannot create revision for %s') % ctx)
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   568
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   569
    return revision, diff
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   570
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   571
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   572
def userphids(repo, names):
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   573
    """convert user names to PHIDs"""
41716
570e62f1dcf2 phabricator: make user searches case-insensitive
Julien Cristau <jcristau@mozilla.com>
parents: 41546
diff changeset
   574
    names = [name.lower() for name in names]
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   575
    query = {b'constraints': {b'usernames': names}}
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   576
    result = callconduit(repo.ui, b'user.search', query)
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   577
    # username not found is not an error of the API. So check if we have missed
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   578
    # some names here.
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   579
    data = result[b'data']
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   580
    resolved = set(entry[b'fields'][b'username'].lower() for entry in data)
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   581
    unresolved = set(names) - resolved
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   582
    if unresolved:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   583
        raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   584
            _(b'unknown username: %s') % b' '.join(sorted(unresolved))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   585
        )
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   586
    return [entry[b'phid'] for entry in data]
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   587
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   588
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   589
@vcrcommand(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   590
    b'phabsend',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   591
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   592
        (b'r', b'rev', [], _(b'revisions to send'), _(b'REV')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   593
        (b'', b'amend', True, _(b'update commit messages')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   594
        (b'', b'reviewer', [], _(b'specify reviewers')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   595
        (b'', b'blocker', [], _(b'specify blocking reviewers')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   596
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   597
            b'm',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   598
            b'comment',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   599
            b'',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   600
            _(b'add a comment to Revisions with new/updated Diffs'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   601
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   602
        (b'', b'confirm', None, _(b'ask for confirmation before sending')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   603
    ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   604
    _(b'REV [OPTIONS]'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   605
    helpcategory=command.CATEGORY_IMPORT_EXPORT,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   606
)
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   607
def phabsend(ui, repo, *revs, **opts):
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   608
    """upload changesets to Phabricator
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   609
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   610
    If there are multiple revisions specified, they will be send as a stack
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   611
    with a linear dependencies relationship using the order specified by the
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   612
    revset.
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   613
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   614
    For the first time uploading changesets, local tags will be created to
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   615
    maintain the association. After the first time, phabsend will check
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   616
    obsstore and tags information so it can figure out whether to update an
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   617
    existing Differential Revision, or create a new one.
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   618
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   619
    If --amend is set, update commit messages so they have the
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   620
    ``Differential Revision`` URL, remove related tags. This is similar to what
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   621
    arcanist will do, and is more desired in author-push workflows. Otherwise,
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   622
    use local tags to record the ``Differential Revision`` association.
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   623
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   624
    The --confirm option lets you confirm changesets before sending them. You
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   625
    can also add following to your configuration file to make it default
33992
45a8cd74de4e phabsend: polish the docstring a bit
Jun Wu <quark@fb.com>
parents: 33855
diff changeset
   626
    behaviour::
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   627
33992
45a8cd74de4e phabsend: polish the docstring a bit
Jun Wu <quark@fb.com>
parents: 33855
diff changeset
   628
        [phabsend]
45a8cd74de4e phabsend: polish the docstring a bit
Jun Wu <quark@fb.com>
parents: 33855
diff changeset
   629
        confirm = true
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   630
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   631
    phabsend will check obsstore and the above association to decide whether to
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   632
    update an existing Differential Revision, or create a new one.
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   633
    """
41975
51ba9fbcca52 py3: use pycompat.byteskwargs on opts in phabricator.py
Ian Moody <moz-ian@perix.co.uk>
parents: 41955
diff changeset
   634
    opts = pycompat.byteskwargs(opts)
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   635
    revs = list(revs) + opts.get(b'rev', [])
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   636
    revs = scmutil.revrange(repo, revs)
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   637
33266
5b2391b46906 phabricator: abort if phabsend gets empty revs
Jun Wu <quark@fb.com>
parents: 33265
diff changeset
   638
    if not revs:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   639
        raise error.Abort(_(b'phabsend requires at least one changeset'))
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   640
    if opts.get(b'amend'):
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   641
        cmdutil.checkunfinished(repo)
33266
5b2391b46906 phabricator: abort if phabsend gets empty revs
Jun Wu <quark@fb.com>
parents: 33265
diff changeset
   642
33996
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
   643
    # {newnode: (oldnode, olddiff, olddrev}
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
   644
    oldmap = getoldnodedrevmap(repo, [repo[r].node() for r in revs])
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
   645
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   646
    confirm = ui.configbool(b'phabsend', b'confirm')
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   647
    confirm |= bool(opts.get(b'confirm'))
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   648
    if confirm:
33996
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
   649
        confirmed = _confirmbeforesend(repo, revs, oldmap)
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   650
        if not confirmed:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   651
            raise error.Abort(_(b'phabsend cancelled'))
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   652
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   653
    actions = []
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   654
    reviewers = opts.get(b'reviewer', [])
42460
f33d3ee110da phabricator: add --blocker argument to phabsend to specify blocking reviewers
Ian Moody <moz-ian@perix.co.uk>
parents: 42459
diff changeset
   655
    blockers = opts.get(b'blocker', [])
f33d3ee110da phabricator: add --blocker argument to phabsend to specify blocking reviewers
Ian Moody <moz-ian@perix.co.uk>
parents: 42459
diff changeset
   656
    phids = []
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   657
    if reviewers:
42460
f33d3ee110da phabricator: add --blocker argument to phabsend to specify blocking reviewers
Ian Moody <moz-ian@perix.co.uk>
parents: 42459
diff changeset
   658
        phids.extend(userphids(repo, reviewers))
f33d3ee110da phabricator: add --blocker argument to phabsend to specify blocking reviewers
Ian Moody <moz-ian@perix.co.uk>
parents: 42459
diff changeset
   659
    if blockers:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   660
        phids.extend(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   661
            map(lambda phid: b'blocking(%s)' % phid, userphids(repo, blockers))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   662
        )
42460
f33d3ee110da phabricator: add --blocker argument to phabsend to specify blocking reviewers
Ian Moody <moz-ian@perix.co.uk>
parents: 42459
diff changeset
   663
    if phids:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   664
        actions.append({b'type': b'reviewers.add', b'value': phids})
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   665
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   666
    drevids = []  # [int]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   667
    diffmap = {}  # {newnode: diff}
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   668
42465
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
   669
    # Send patches one by one so we know their Differential Revision PHIDs and
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   670
    # can provide dependency relationship
42465
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
   671
    lastrevphid = None
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   672
    for rev in revs:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   673
        ui.debug(b'sending rev %d\n' % rev)
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   674
        ctx = repo[rev]
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   675
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   676
        # Get Differential Revision ID
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   677
        oldnode, olddiff, revid = oldmap.get(ctx.node(), (None, None, None))
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   678
        if oldnode != ctx.node() or opts.get(b'amend'):
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   679
            # Create or update Differential Revision
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   680
            revision, diff = createdifferentialrevision(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   681
                ctx,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   682
                revid,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   683
                lastrevphid,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   684
                oldnode,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   685
                olddiff,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   686
                actions,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   687
                opts.get(b'comment'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   688
            )
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   689
            diffmap[ctx.node()] = diff
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   690
            newrevid = int(revision[b'object'][b'id'])
42465
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
   691
            newrevphid = revision[b'object'][b'phid']
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   692
            if revid:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   693
                action = b'updated'
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   694
            else:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   695
                action = b'created'
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   696
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   697
            # Create a local tag to note the association, if commit message
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   698
            # does not have it already
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   699
            m = _differentialrevisiondescre.search(ctx.description())
41910
16d050678047 py3: use r'' for group name arguments to MatchObjects in phabricator.py
Ian Moody <moz-ian@perix.co.uk>
parents: 41909
diff changeset
   700
            if not m or int(m.group(r'id')) != newrevid:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   701
                tagname = b'D%d' % newrevid
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   702
                tags.tag(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   703
                    repo,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   704
                    tagname,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   705
                    ctx.node(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   706
                    message=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   707
                    user=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   708
                    date=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   709
                    local=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   710
                )
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   711
        else:
42465
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
   712
            # Nothing changed. But still set "newrevphid" so the next revision
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
   713
            # could depend on this one and "newrevid" for the summary line.
42954
6fb281f39c25 py3: pass a bytestring into querydrev instead of a string that'll TypeError
Ian Moody <moz-ian@perix.co.uk>
parents: 42677
diff changeset
   714
            newrevphid = querydrev(repo, b'%d' % revid)[0][b'phid']
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   715
            newrevid = revid
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   716
            action = b'skipped'
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   717
34080
941c33cfde81 phabricator: standardize colors
Jun Wu <quark@fb.com>
parents: 33996
diff changeset
   718
        actiondesc = ui.label(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   719
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   720
                b'created': _(b'created'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   721
                b'skipped': _(b'skipped'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   722
                b'updated': _(b'updated'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   723
            }[action],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   724
            b'phabricator.action.%s' % action,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   725
        )
41909
634b56b54e7c py3: use %d instead of %s when formatting an int into a byte string
Ian Moody <moz-ian@perix.co.uk>
parents: 41908
diff changeset
   726
        drevdesc = ui.label(b'D%d' % newrevid, b'phabricator.drev')
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   727
        nodedesc = ui.label(bytes(ctx), b'phabricator.node')
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   728
        desc = ui.label(ctx.description().split(b'\n')[0], b'phabricator.desc')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   729
        ui.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   730
            _(b'%s - %s - %s: %s\n') % (drevdesc, actiondesc, nodedesc, desc)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   731
        )
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   732
        drevids.append(newrevid)
42465
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
   733
        lastrevphid = newrevphid
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
   734
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   735
    # Update commit messages and remove tags
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   736
    if opts.get(b'amend'):
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   737
        unfi = repo.unfiltered()
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   738
        drevs = callconduit(ui, b'differential.query', {b'ids': drevids})
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   739
        with repo.wlock(), repo.lock(), repo.transaction(b'phabsend'):
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   740
            wnode = unfi[b'.'].node()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   741
            mapping = {}  # {oldnode: [newnode]}
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   742
            for i, rev in enumerate(revs):
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   743
                old = unfi[rev]
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   744
                drevid = drevids[i]
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   745
                drev = [d for d in drevs if int(d[b'id']) == drevid][0]
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   746
                newdesc = getdescfromdrev(drev)
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   747
                # Make sure commit message contain "Differential Revision"
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   748
                if old.description() != newdesc:
41163
0101a35deae2 phabricator: warn if unable to amend, instead of aborting after posting
Matt Harbison <matt_harbison@yahoo.com>
parents: 41083
diff changeset
   749
                    if old.phase() == phases.public:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   750
                        ui.warn(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   751
                            _(b"warning: not updating public commit %s\n")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   752
                            % scmutil.formatchangeid(old)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   753
                        )
41163
0101a35deae2 phabricator: warn if unable to amend, instead of aborting after posting
Matt Harbison <matt_harbison@yahoo.com>
parents: 41083
diff changeset
   754
                        continue
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   755
                    parents = [
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   756
                        mapping.get(old.p1().node(), (old.p1(),))[0],
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   757
                        mapping.get(old.p2().node(), (old.p2(),))[0],
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   758
                    ]
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   759
                    new = context.metadataonlyctx(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   760
                        repo,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   761
                        old,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   762
                        parents=parents,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   763
                        text=newdesc,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   764
                        user=old.user(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   765
                        date=old.date(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   766
                        extra=old.extra(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   767
                    )
38342
bb7e3c6ef592 phabricator: preserve the phase when amending in the Differential fields
Matt Harbison <matt_harbison@yahoo.com>
parents: 38075
diff changeset
   768
38429
32fba6fe893d scmutil: make cleanupnodes optionally also fix the phase
Martin von Zweigbergk <martinvonz@google.com>
parents: 38398
diff changeset
   769
                    newnode = new.commit()
38342
bb7e3c6ef592 phabricator: preserve the phase when amending in the Differential fields
Matt Harbison <matt_harbison@yahoo.com>
parents: 38075
diff changeset
   770
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   771
                    mapping[old.node()] = [newnode]
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   772
                    # Update diff property
42677
e26c2440a030 phabricator: don't abort if property writing fails during amending
Ian Moody <moz-ian@perix.co.uk>
parents: 42618
diff changeset
   773
                    # If it fails just warn and keep going, otherwise the DREV
e26c2440a030 phabricator: don't abort if property writing fails during amending
Ian Moody <moz-ian@perix.co.uk>
parents: 42618
diff changeset
   774
                    # associations will be lost
e26c2440a030 phabricator: don't abort if property writing fails during amending
Ian Moody <moz-ian@perix.co.uk>
parents: 42618
diff changeset
   775
                    try:
e26c2440a030 phabricator: don't abort if property writing fails during amending
Ian Moody <moz-ian@perix.co.uk>
parents: 42618
diff changeset
   776
                        writediffproperties(unfi[newnode], diffmap[old.node()])
e26c2440a030 phabricator: don't abort if property writing fails during amending
Ian Moody <moz-ian@perix.co.uk>
parents: 42618
diff changeset
   777
                    except util.urlerr.urlerror:
43080
86e4daa2d54c cleanup: mark some ui.(status|note|warn|write) calls as not needing i18n
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   778
                        ui.warnnoi18n(b'Failed to update metadata for D%s\n' % drevid)
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   779
                # Remove local tags since it's no longer necessary
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   780
                tagname = b'D%d' % drevid
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   781
                if tagname in repo.tags():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   782
                    tags.tag(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   783
                        repo,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   784
                        tagname,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   785
                        nullid,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   786
                        message=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   787
                        user=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   788
                        date=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   789
                        local=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   790
                    )
38429
32fba6fe893d scmutil: make cleanupnodes optionally also fix the phase
Martin von Zweigbergk <martinvonz@google.com>
parents: 38398
diff changeset
   791
            scmutil.cleanupnodes(repo, mapping, b'phabsend', fixphase=True)
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   792
            if wnode in mapping:
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   793
                unfi.setparents(mapping[wnode][0])
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   794
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   795
33264
266321579c68 phabricator: add node and p1 to hg:meta property
Jun Wu <quark@fb.com>
parents: 33263
diff changeset
   796
# Map from "hg:meta" keys to header understood by "hg import". The order is
266321579c68 phabricator: add node and p1 to hg:meta property
Jun Wu <quark@fb.com>
parents: 33263
diff changeset
   797
# consistent with "hg export" output.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   798
_metanamemap = util.sortdict(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   799
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   800
        (b'user', b'User'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   801
        (b'date', b'Date'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   802
        (b'branch', b'Branch'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   803
        (b'node', b'Node ID'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   804
        (b'parent', b'Parent '),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   805
    ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   806
)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   807
33264
266321579c68 phabricator: add node and p1 to hg:meta property
Jun Wu <quark@fb.com>
parents: 33263
diff changeset
   808
33996
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
   809
def _confirmbeforesend(repo, revs, oldmap):
42449
500b64c5d991 phabricator: pass ui into readurltoken instead of passing repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42447
diff changeset
   810
    url, token = readurltoken(repo.ui)
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   811
    ui = repo.ui
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   812
    for rev in revs:
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   813
        ctx = repo[rev]
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   814
        desc = ctx.description().splitlines()[0]
33996
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
   815
        oldnode, olddiff, drevid = oldmap.get(ctx.node(), (None, None, None))
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
   816
        if drevid:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   817
            drevdesc = ui.label(b'D%s' % drevid, b'phabricator.drev')
33996
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
   818
        else:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   819
            drevdesc = ui.label(_(b'NEW'), b'phabricator.drev')
33996
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
   820
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   821
        ui.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   822
            _(b'%s - %s: %s\n')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   823
            % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   824
                drevdesc,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   825
                ui.label(bytes(ctx), b'phabricator.node'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   826
                ui.label(desc, b'phabricator.desc'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   827
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   828
        )
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   829
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   830
    if ui.promptchoice(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   831
        _(b'Send the above changes to %s (yn)?' b'$$ &Yes $$ &No') % url
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   832
    ):
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   833
        return False
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   834
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   835
    return True
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
   836
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   837
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   838
_knownstatusnames = {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   839
    b'accepted',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   840
    b'needsreview',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   841
    b'needsrevision',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   842
    b'closed',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   843
    b'abandoned',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   844
}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   845
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
   846
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
   847
def _getstatusname(drev):
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
   848
    """get normalized status name from a Differential Revision"""
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   849
    return drev[b'statusName'].replace(b' ', b'').lower()
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
   850
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   851
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   852
# Small language to specify differential revisions. Support symbols: (), :X,
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   853
# +, and -.
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   854
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   855
_elements = {
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   856
    # token-type: binding-strength, primary, prefix, infix, suffix
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   857
    b'(': (12, None, (b'group', 1, b')'), None, None),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   858
    b':': (8, None, (b'ancestors', 8), None, None),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   859
    b'&': (5, None, None, (b'and_', 5), None),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   860
    b'+': (4, None, None, (b'add', 4), None),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   861
    b'-': (4, None, None, (b'sub', 4), None),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   862
    b')': (0, None, None, None, None),
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   863
    b'symbol': (0, b'symbol', None, None, None),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   864
    b'end': (0, None, None, None, None),
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   865
}
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   866
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   867
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   868
def _tokenize(text):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   869
    view = memoryview(text)  # zero-copy slice
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   870
    special = b'():+-& '
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   871
    pos = 0
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   872
    length = len(text)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   873
    while pos < length:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   874
        symbol = b''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   875
            itertools.takewhile(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   876
                lambda ch: ch not in special, pycompat.iterbytestr(view[pos:])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   877
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   878
        )
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   879
        if symbol:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   880
            yield (b'symbol', symbol, pos)
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   881
            pos += len(symbol)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   882
        else:  # special char, ignore space
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   883
            if text[pos] != b' ':
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
   884
                yield (text[pos], None, pos)
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   885
            pos += 1
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   886
    yield (b'end', None, pos)
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   887
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   888
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   889
def _parse(text):
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   890
    tree, pos = parser.parser(_elements).parse(_tokenize(text))
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   891
    if pos != len(text):
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   892
        raise error.ParseError(b'invalid token', pos)
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   893
    return tree
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   894
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   895
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   896
def _parsedrev(symbol):
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   897
    """str -> int or None, ex. 'D45' -> 45; '12' -> 12; 'x' -> None"""
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   898
    if symbol.startswith(b'D') and symbol[1:].isdigit():
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   899
        return int(symbol[1:])
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   900
    if symbol.isdigit():
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   901
        return int(symbol)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   902
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   903
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   904
def _prefetchdrevs(tree):
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   905
    """return ({single-drev-id}, {ancestor-drev-id}) to prefetch"""
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   906
    drevs = set()
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   907
    ancestordrevs = set()
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   908
    op = tree[0]
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   909
    if op == b'symbol':
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   910
        r = _parsedrev(tree[1])
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   911
        if r:
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   912
            drevs.add(r)
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   913
    elif op == b'ancestors':
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   914
        r, a = _prefetchdrevs(tree[1])
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   915
        drevs.update(r)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   916
        ancestordrevs.update(r)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   917
        ancestordrevs.update(a)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   918
    else:
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   919
        for t in tree[1:]:
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   920
            r, a = _prefetchdrevs(t)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   921
            drevs.update(r)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   922
            ancestordrevs.update(a)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   923
    return drevs, ancestordrevs
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   924
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   925
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   926
def querydrev(repo, spec):
33267
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   927
    """return a list of "Differential Revision" dicts
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   928
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   929
    spec is a string using a simple query language, see docstring in phabread
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   930
    for details.
33267
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   931
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   932
    A "Differential Revision dict" looks like:
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   933
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   934
        {
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   935
            "id": "2",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   936
            "phid": "PHID-DREV-672qvysjcczopag46qty",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   937
            "title": "example",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   938
            "uri": "https://phab.example.com/D2",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   939
            "dateCreated": "1499181406",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   940
            "dateModified": "1499182103",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   941
            "authorPHID": "PHID-USER-tv3ohwc4v4jeu34otlye",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   942
            "status": "0",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   943
            "statusName": "Needs Review",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   944
            "properties": [],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   945
            "branch": null,
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   946
            "summary": "",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   947
            "testPlan": "",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   948
            "lineCount": "2",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   949
            "activeDiffPHID": "PHID-DIFF-xoqnjkobbm6k4dk6hi72",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   950
            "diffs": [
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   951
              "3",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   952
              "4",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   953
            ],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   954
            "commits": [],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   955
            "reviewers": [],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   956
            "ccs": [],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   957
            "hashes": [],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   958
            "auxiliary": {
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   959
              "phabricator:projects": [],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   960
              "phabricator:depends-on": [
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   961
                "PHID-DREV-gbapp366kutjebt7agcd"
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   962
              ]
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   963
            },
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   964
            "repositoryPHID": "PHID-REPO-hub2hx62ieuqeheznasv",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   965
            "sourcePath": null
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   966
        }
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   967
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   968
33269
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
   969
    def fetch(params):
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
   970
        """params -> single drev or None"""
41907
59bae59b7498 py3: fix a few "dict keys as str instead of bytes" issues in phabricator.py
Ian Moody <moz-ian@perix.co.uk>
parents: 41906
diff changeset
   971
        key = (params.get(b'ids') or params.get(b'phids') or [None])[0]
33269
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
   972
        if key in prefetched:
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
   973
            return prefetched[key]
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   974
        drevs = callconduit(repo.ui, b'differential.query', params)
33269
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
   975
        # Fill prefetched with the result
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
   976
        for drev in drevs:
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   977
            prefetched[drev[b'phid']] = drev
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   978
            prefetched[int(drev[b'id'])] = drev
33269
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
   979
        if key not in prefetched:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   980
            raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   981
                _(b'cannot get Differential Revision %r') % params
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   982
            )
33269
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
   983
        return prefetched[key]
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
   984
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   985
    def getstack(topdrevids):
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   986
        """given a top, get a stack from the bottom, [id] -> [id]"""
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   987
        visited = set()
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   988
        result = []
41907
59bae59b7498 py3: fix a few "dict keys as str instead of bytes" issues in phabricator.py
Ian Moody <moz-ian@perix.co.uk>
parents: 41906
diff changeset
   989
        queue = [{b'ids': [i]} for i in topdrevids]
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   990
        while queue:
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   991
            params = queue.pop()
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   992
            drev = fetch(params)
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   993
            if drev[b'id'] in visited:
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
   994
                continue
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   995
            visited.add(drev[b'id'])
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   996
            result.append(int(drev[b'id']))
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   997
            auxiliary = drev.get(b'auxiliary', {})
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   998
            depends = auxiliary.get(b'phabricator:depends-on', [])
33267
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
   999
            for phid in depends:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1000
                queue.append({b'phids': [phid]})
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1001
        result.reverse()
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1002
        return smartset.baseset(result)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1003
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1004
    # Initialize prefetch cache
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1005
    prefetched = {}  # {id or phid: drev}
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1006
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1007
    tree = _parse(spec)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1008
    drevs, ancestordrevs = _prefetchdrevs(tree)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1009
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1010
    # developer config: phabricator.batchsize
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1011
    batchsize = repo.ui.configint(b'phabricator', b'batchsize')
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1012
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1013
    # Prefetch Differential Revisions in batch
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1014
    tofetch = set(drevs)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1015
    for r in ancestordrevs:
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1016
        tofetch.update(range(max(1, r - batchsize), r + 1))
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1017
    if drevs:
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1018
        fetch({b'ids': list(tofetch)})
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1019
    validids = sorted(set(getstack(list(ancestordrevs))) | set(drevs))
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1020
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1021
    # Walk through the tree, return smartsets
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1022
    def walk(tree):
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1023
        op = tree[0]
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1024
        if op == b'symbol':
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1025
            drev = _parsedrev(tree[1])
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1026
            if drev:
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1027
                return smartset.baseset([drev])
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1028
            elif tree[1] in _knownstatusnames:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1029
                drevs = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1030
                    r
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1031
                    for r in validids
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1032
                    if _getstatusname(prefetched[r]) == tree[1]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1033
                ]
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1034
                return smartset.baseset(drevs)
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1035
            else:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1036
                raise error.Abort(_(b'unknown symbol: %s') % tree[1])
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1037
        elif op in {b'and_', b'add', b'sub'}:
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1038
            assert len(tree) == 3
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1039
            return getattr(operator, op)(walk(tree[1]), walk(tree[2]))
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1040
        elif op == b'group':
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1041
            return walk(tree[1])
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1042
        elif op == b'ancestors':
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1043
            return getstack(walk(tree[1]))
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1044
        else:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1045
            raise error.ProgrammingError(b'illegal tree: %r' % tree)
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1046
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1047
    return [prefetched[r] for r in walk(tree)]
33267
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1048
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1049
33268
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1050
def getdescfromdrev(drev):
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1051
    """get description (commit message) from "Differential Revision"
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1052
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1053
    This is similar to differential.getcommitmessage API. But we only care
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1054
    about limited fields: title, summary, test plan, and URL.
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1055
    """
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1056
    title = drev[b'title']
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1057
    summary = drev[b'summary'].rstrip()
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1058
    testplan = drev[b'testPlan'].rstrip()
33268
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1059
    if testplan:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1060
        testplan = b'Test Plan:\n%s' % testplan
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1061
    uri = b'Differential Revision: %s' % drev[b'uri']
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1062
    return b'\n\n'.join(filter(None, [title, summary, testplan, uri]))
33268
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1063
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1064
33441
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1065
def getdiffmeta(diff):
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1066
    """get commit metadata (date, node, user, p1) from a diff object
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1067
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1068
    The metadata could be "hg:meta", sent by phabsend, like:
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1069
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1070
        "properties": {
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1071
          "hg:meta": {
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1072
            "date": "1499571514 25200",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1073
            "node": "98c08acae292b2faf60a279b4189beb6cff1414d",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1074
            "user": "Foo Bar <foo@example.com>",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1075
            "parent": "6d0abad76b30e4724a37ab8721d630394070fe16"
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1076
          }
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1077
        }
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1078
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1079
    Or converted from "local:commits", sent by "arc", like:
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1080
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1081
        "properties": {
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1082
          "local:commits": {
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1083
            "98c08acae292b2faf60a279b4189beb6cff1414d": {
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1084
              "author": "Foo Bar",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1085
              "time": 1499546314,
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1086
              "branch": "default",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1087
              "tag": "",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1088
              "commit": "98c08acae292b2faf60a279b4189beb6cff1414d",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1089
              "rev": "98c08acae292b2faf60a279b4189beb6cff1414d",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1090
              "local": "1000",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1091
              "parents": ["6d0abad76b30e4724a37ab8721d630394070fe16"],
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1092
              "summary": "...",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1093
              "message": "...",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1094
              "authorEmail": "foo@example.com"
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1095
            }
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1096
          }
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1097
        }
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1098
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1099
    Note: metadata extracted from "local:commits" will lose time zone
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1100
    information.
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1101
    """
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1102
    props = diff.get(b'properties') or {}
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1103
    meta = props.get(b'hg:meta')
42285
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1104
    if not meta:
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1105
        if props.get(b'local:commits'):
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1106
            commit = sorted(props[b'local:commits'].values())[0]
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1107
            meta = {}
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1108
            if b'author' in commit and b'authorEmail' in commit:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1109
                meta[b'user'] = b'%s <%s>' % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1110
                    commit[b'author'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1111
                    commit[b'authorEmail'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1112
                )
42285
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1113
            if b'time' in commit:
42618
c17e6a3e7356 phabricator: handle local:commits time being string or int
Ian Moody <moz-ian@perix.co.uk>
parents: 42465
diff changeset
  1114
                meta[b'date'] = b'%d 0' % int(commit[b'time'])
42285
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1115
            if b'branch' in commit:
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1116
                meta[b'branch'] = commit[b'branch']
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1117
            node = commit.get(b'commit', commit.get(b'rev'))
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1118
            if node:
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1119
                meta[b'node'] = node
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1120
            if len(commit.get(b'parents', ())) >= 1:
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1121
                meta[b'parent'] = commit[b'parents'][0]
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1122
        else:
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1123
            meta = {}
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1124
    if b'date' not in meta and b'dateCreated' in diff:
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1125
        meta[b'date'] = b'%s 0' % diff[b'dateCreated']
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1126
    if b'branch' not in meta and diff.get(b'branch'):
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1127
        meta[b'branch'] = diff[b'branch']
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1128
    if b'parent' not in meta and diff.get(b'sourceControlBaseRevision'):
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1129
        meta[b'parent'] = diff[b'sourceControlBaseRevision']
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1130
    return meta
33441
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1131
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1132
33852
75fdaf851e83 phabricator: change "readpatch" to be more flexible
Jun Wu <quark@fb.com>
parents: 33808
diff changeset
  1133
def readpatch(repo, drevs, write):
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1134
    """generate plain-text patch readable by 'hg import'
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1135
33852
75fdaf851e83 phabricator: change "readpatch" to be more flexible
Jun Wu <quark@fb.com>
parents: 33808
diff changeset
  1136
    write is usually ui.write. drevs is what "querydrev" returns, results of
75fdaf851e83 phabricator: change "readpatch" to be more flexible
Jun Wu <quark@fb.com>
parents: 33808
diff changeset
  1137
    "differential.query".
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1138
    """
33267
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1139
    # Prefetch hg:meta property for all diffs
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1140
    diffids = sorted(set(max(int(v) for v in drev[b'diffs']) for drev in drevs))
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
  1141
    diffs = callconduit(repo.ui, b'differential.querydiffs', {b'ids': diffids})
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1142
33267
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1143
    # Generate patch for each drev
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1144
    for drev in drevs:
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1145
        repo.ui.note(_(b'reading D%s\n') % drev[b'id'])
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1146
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1147
        diffid = max(int(v) for v in drev[b'diffs'])
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1148
        body = callconduit(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1149
            repo.ui, b'differential.getrawdiff', {b'diffID': diffid}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1150
        )
33268
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1151
        desc = getdescfromdrev(drev)
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1152
        header = b'# HG changeset patch\n'
33267
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1153
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1154
        # Try to preserve metadata from hg:meta property. Write hg patch
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1155
        # headers that can be read by the "import" command. See patchheadermap
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1156
        # and extract in mercurial/patch.py for supported headers.
41907
59bae59b7498 py3: fix a few "dict keys as str instead of bytes" issues in phabricator.py
Ian Moody <moz-ian@perix.co.uk>
parents: 41906
diff changeset
  1157
        meta = getdiffmeta(diffs[b'%d' % diffid])
33441
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1158
        for k in _metanamemap.keys():
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1159
            if k in meta:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1160
                header += b'# %s %s\n' % (_metanamemap[k], meta[k])
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1161
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1162
        content = b'%s%s\n%s' % (header, desc, body)
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1163
        write(content)
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1164
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1165
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1166
@vcrcommand(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1167
    b'phabread',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1168
    [(b'', b'stack', False, _(b'read dependencies'))],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1169
    _(b'DREVSPEC [OPTIONS]'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1170
    helpcategory=command.CATEGORY_IMPORT_EXPORT,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1171
)
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1172
def phabread(ui, repo, spec, **opts):
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1173
    """print patches from Phabricator suitable for importing
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1174
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1175
    DREVSPEC could be a Differential Revision identity, like ``D123``, or just
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1176
    the number ``123``. It could also have common operators like ``+``, ``-``,
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1177
    ``&``, ``(``, ``)`` for complex queries. Prefix ``:`` could be used to
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1178
    select a stack.
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1179
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1180
    ``abandoned``, ``accepted``, ``closed``, ``needsreview``, ``needsrevision``
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1181
    could be used to filter patches by status. For performance reason, they
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1182
    only represent a subset of non-status selections and cannot be used alone.
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1183
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1184
    For example, ``:D6+8-(2+D4)`` selects a stack up to D6, plus D8 and exclude
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1185
    D2 and D4. ``:D9 & needsreview`` selects "Needs Review" revisions in a
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1186
    stack up to D9.
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1187
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1188
    If --stack is given, follow dependencies information and read all patches.
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1189
    It is equivalent to the ``:`` operator.
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1190
    """
41975
51ba9fbcca52 py3: use pycompat.byteskwargs on opts in phabricator.py
Ian Moody <moz-ian@perix.co.uk>
parents: 41955
diff changeset
  1191
    opts = pycompat.byteskwargs(opts)
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1192
    if opts.get(b'stack'):
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1193
        spec = b':(%s)' % spec
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1194
    drevs = querydrev(repo, spec)
33852
75fdaf851e83 phabricator: change "readpatch" to be more flexible
Jun Wu <quark@fb.com>
parents: 33808
diff changeset
  1195
    readpatch(repo, drevs, ui.write)
33855
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1196
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1197
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1198
@vcrcommand(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1199
    b'phabupdate',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1200
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1201
        (b'', b'accept', False, _(b'accept revisions')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1202
        (b'', b'reject', False, _(b'reject revisions')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1203
        (b'', b'abandon', False, _(b'abandon revisions')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1204
        (b'', b'reclaim', False, _(b'reclaim revisions')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1205
        (b'm', b'comment', b'', _(b'comment on the last revision')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1206
    ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1207
    _(b'DREVSPEC [OPTIONS]'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1208
    helpcategory=command.CATEGORY_IMPORT_EXPORT,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1209
)
33855
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1210
def phabupdate(ui, repo, spec, **opts):
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1211
    """update Differential Revision in batch
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1212
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1213
    DREVSPEC selects revisions. See :hg:`help phabread` for its usage.
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1214
    """
41975
51ba9fbcca52 py3: use pycompat.byteskwargs on opts in phabricator.py
Ian Moody <moz-ian@perix.co.uk>
parents: 41955
diff changeset
  1215
    opts = pycompat.byteskwargs(opts)
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1216
    flags = [n for n in b'accept reject abandon reclaim'.split() if opts.get(n)]
33855
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1217
    if len(flags) > 1:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1218
        raise error.Abort(_(b'%s cannot be used together') % b', '.join(flags))
33855
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1219
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1220
    actions = []
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1221
    for f in flags:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1222
        actions.append({b'type': f, b'value': b'true'})
33855
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1223
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1224
    drevs = querydrev(repo, spec)
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1225
    for i, drev in enumerate(drevs):
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1226
        if i + 1 == len(drevs) and opts.get(b'comment'):
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1227
            actions.append({b'type': b'comment', b'value': opts[b'comment']})
33855
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1228
        if actions:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1229
            params = {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1230
                b'objectIdentifier': drev[b'phid'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1231
                b'transactions': actions,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1232
            }
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
  1233
            callconduit(ui, b'differential.revision.edit', params)
35722
f18ba40d792f phabricator: add a template item for linking to a differential review
Tom Prince <mozilla@hocat.ca>
parents: 35626
diff changeset
  1234
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1235
42975
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
  1236
@eh.templatekeyword(b'phabreview', requires={b'ctx'})
36543
7b74afec6772 templatekw: switch non-showlist template keywords to new API
Yuya Nishihara <yuya@tcha.org>
parents: 35722
diff changeset
  1237
def template_review(context, mapping):
35722
f18ba40d792f phabricator: add a template item for linking to a differential review
Tom Prince <mozilla@hocat.ca>
parents: 35626
diff changeset
  1238
    """:phabreview: Object describing the review for this changeset.
f18ba40d792f phabricator: add a template item for linking to a differential review
Tom Prince <mozilla@hocat.ca>
parents: 35626
diff changeset
  1239
    Has attributes `url` and `id`.
f18ba40d792f phabricator: add a template item for linking to a differential review
Tom Prince <mozilla@hocat.ca>
parents: 35626
diff changeset
  1240
    """
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1241
    ctx = context.resource(mapping, b'ctx')
35722
f18ba40d792f phabricator: add a template item for linking to a differential review
Tom Prince <mozilla@hocat.ca>
parents: 35626
diff changeset
  1242
    m = _differentialrevisiondescre.search(ctx.description())
f18ba40d792f phabricator: add a template item for linking to a differential review
Tom Prince <mozilla@hocat.ca>
parents: 35626
diff changeset
  1243
    if m:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1244
        return templateutil.hybriddict(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1245
            {b'url': m.group(r'url'), b'id': b"D%s" % m.group(r'id'),}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1246
        )
41164
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1247
    else:
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1248
        tags = ctx.repo().nodetags(ctx.node())
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1249
        for t in tags:
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1250
            if _differentialrevisiontagre.match(t):
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1251
                url = ctx.repo().ui.config(b'phabricator', b'url')
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1252
                if not url.endswith(b'/'):
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1253
                    url += b'/'
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1254
                url += t
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1255
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1256
                return templateutil.hybriddict({b'url': url, b'id': t,})
41164
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1257
    return None