hgext/phabricator.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Thu, 21 Nov 2019 18:10:12 +0100
changeset 43847 16b607e9f714
parent 43597 4cb3f5bb29ec
child 43857 70060915c3f2
permissions -rw-r--r--
phabricator: fix processing of tags/desc in getoldnodedrevmap() It seems that the previous logic was wrong (it essentially comes from changeset 3ab0d5767b54 where the result got accumulated instead of early returned). First of all, the "continue" in first "if m:" is useless because we're at the end of the loop. Then, the algorithm seems weird because we will process all predecessors of a node and possibly override `toconfirm[node]` for each of these having a tag (maybe this doesn't happen, but still). Finally, we would also override `toconfirm[node]` when the "Differential Revision: " is found in changeset description. Maybe this is not a big deal when there is no mix of local tag and changeset description update? The logic is changed so that the loop on predecessors stops upon first match of a tag and so that the changeset description is only checked if no tag was found. Therefore, `toconfirm[node]` is only set once. Differential Revision: https://phab.mercurial-scm.org/D7513
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
43187
453079605242 phabricator: add the uploadchunks function
Ian Moody <moz-ian@perix.co.uk>
parents: 43186
diff changeset
    44
import base64
41083
9d35ae3d9999 phabricator: ensure that the return of urlopener.open() is closed
Matt Harbison <matt_harbison@yahoo.com>
parents: 41081
diff changeset
    45
import contextlib
43188
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
    46
import hashlib
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
    47
import itertools
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    48
import json
43189
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
    49
import mimetypes
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
    50
import operator
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    51
import re
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    52
33443
e48082e0a8d5 phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents: 33442
diff changeset
    53
from mercurial.node import bin, nullid
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    54
from mercurial.i18n import _
43089
c59eb1560c44 py3: manually import getattr where it is needed
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43080
diff changeset
    55
from mercurial.pycompat import getattr
43183
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
    56
from mercurial.thirdparty import attr
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    57
from mercurial import (
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
    58
    cmdutil,
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
    59
    context,
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    60
    encoding,
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    61
    error,
42975
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
    62
    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
    63
    httpconnection as httpconnectionmod,
43186
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
    64
    match,
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    65
    mdiff,
33761
e6d8ee3c9ec3 obsutil: rename allprecursors into allpredecessors
Boris Feld <boris.feld@octobus.net>
parents: 33718
diff changeset
    66
    obsutil,
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
    67
    parser,
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    68
    patch,
41163
0101a35deae2 phabricator: warn if unable to amend, instead of aborting after posting
Matt Harbison <matt_harbison@yahoo.com>
parents: 41083
diff changeset
    69
    phases,
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
    70
    pycompat,
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    71
    scmutil,
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
    72
    smartset,
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
    73
    tags,
42226
289d82a070e9 phabricator: use templatefilters.json in writediffproperties
Ian Moody <moz-ian@perix.co.uk>
parents: 41976
diff changeset
    74
    templatefilters,
39670
4057e38bba76 phabricator: fix templating bug by using hybriddict
Augie Fackler <raf@durin42.com>
parents: 39668
diff changeset
    75
    templateutil,
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    76
    url as urlmod,
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    77
    util,
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    78
)
37123
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36826
diff changeset
    79
from mercurial.utils import (
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36826
diff changeset
    80
    procutil,
37800
6cf5f5b4eb57 phabricator: specify some metadata compatibly with arc
Tom Prince <mozilla@hocat.ca>
parents: 37123
diff changeset
    81
    stringutil,
37123
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36826
diff changeset
    82
)
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    83
39751
c0c703861b60 phabricator: add testedwith boilerplate
Yuya Nishihara <yuya@tcha.org>
parents: 39671
diff changeset
    84
# 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
    85
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
c0c703861b60 phabricator: add testedwith boilerplate
Yuya Nishihara <yuya@tcha.org>
parents: 39671
diff changeset
    86
# 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
    87
# leave the attribute unspecified.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    88
testedwith = b'ships-with-hg-core'
39751
c0c703861b60 phabricator: add testedwith boilerplate
Yuya Nishihara <yuya@tcha.org>
parents: 39671
diff changeset
    89
42975
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
    90
eh = exthelper.exthelper()
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
    91
42975
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
    92
cmdtable = eh.cmdtable
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
    93
command = eh.command
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
    94
configtable = eh.configtable
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
    95
templatekeyword = eh.templatekeyword
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
    96
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
    97
# developer config: phabricator.batchsize
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
    98
eh.configitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
    99
    b'phabricator', b'batchsize', default=12,
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   100
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   101
eh.configitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   102
    b'phabricator', b'callsign', default=None,
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   103
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   104
eh.configitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   105
    b'phabricator', b'curlcmd', default=None,
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   106
)
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   107
# developer config: phabricator.repophid
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   108
eh.configitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   109
    b'phabricator', b'repophid', default=None,
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   110
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   111
eh.configitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   112
    b'phabricator', b'url', default=None,
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   113
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   114
eh.configitem(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   115
    b'phabsend', b'confirm', default=False,
38075
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   116
)
5a7cf42ba6ef phabricator: register config settings
Matt Harbison <matt_harbison@yahoo.com>
parents: 38040
diff changeset
   117
34080
941c33cfde81 phabricator: standardize colors
Jun Wu <quark@fb.com>
parents: 33996
diff changeset
   118
colortable = {
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   119
    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
   120
    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
   121
    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
   122
    b'phabricator.desc': b'',
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   123
    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
   124
    b'phabricator.node': b'',
34080
941c33cfde81 phabricator: standardize colors
Jun Wu <quark@fb.com>
parents: 33996
diff changeset
   125
}
941c33cfde81 phabricator: standardize colors
Jun Wu <quark@fb.com>
parents: 33996
diff changeset
   126
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   127
_VCR_FLAGS = [
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   128
    (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   129
        b'',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   130
        b'test-vcr',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   131
        b'',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   132
        _(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   133
            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
   134
            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
   135
            b' (ADVANCED)'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   136
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   137
    ),
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   138
]
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   139
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   140
42451
16312ea45a8b phabricator: make `hg debugcallconduit` work outside a hg repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42450
diff changeset
   141
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
   142
    fullflags = flags + _VCR_FLAGS
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   143
42295
af13e2088f77 phabricator: add custom vcr matcher to match request bodies
Ian Moody <moz-ian@perix.co.uk>
parents: 42286
diff changeset
   144
    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
   145
        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
   146
            return False
43264
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   147
        r1params = util.urlreq.parseqs(r1.body)
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   148
        r2params = util.urlreq.parseqs(r2.body)
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   149
        for key in r1params:
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   150
            if key not in r2params:
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   151
                return False
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   152
            value = r1params[key][0]
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   153
            # we want to compare json payloads without worrying about ordering
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   154
            if value.startswith(b'{') and value.endswith(b'}'):
43380
579672b347d2 py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43350
diff changeset
   155
                r1json = pycompat.json_loads(value)
579672b347d2 py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43350
diff changeset
   156
                r2json = pycompat.json_loads(r2params[key][0])
43264
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   157
                if r1json != r2json:
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   158
                    return False
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   159
            elif r2params[key][0] != value:
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   160
                return False
a4da1c3b82ab phabricator: update hgmatcher to cope with the new data format
Ian Moody <moz-ian@perix.co.uk>
parents: 43263
diff changeset
   161
        return True
42295
af13e2088f77 phabricator: add custom vcr matcher to match request bodies
Ian Moody <moz-ian@perix.co.uk>
parents: 42286
diff changeset
   162
42459
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   163
    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
   164
        request.body = re.sub(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   165
            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
   166
        )
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   167
        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
   168
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   169
    def sanitiseresponse(response):
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43552
diff changeset
   170
        if 'set-cookie' in response['headers']:
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43552
diff changeset
   171
            del response['headers']['set-cookie']
42459
d3c81439e2ee phabricator: auto-sanitise API tokens and HTTP cookies from VCR recordings
Ian Moody <moz-ian@perix.co.uk>
parents: 42451
diff changeset
   172
        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
   173
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   174
    def decorate(fn):
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   175
        def inner(*args, **kwargs):
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43552
diff changeset
   176
            cassette = pycompat.fsdecode(kwargs.pop('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
   177
            if cassette:
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   178
                import hgdemandimport
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   179
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   180
                with hgdemandimport.deactivated():
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   181
                    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
   182
                    import vcr.stubs as stubs
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   183
40378
b015f30a91fb phabricator: do more of the VCR work in demandimport.deactivated()
Augie Fackler <augie@google.com>
parents: 40151
diff changeset
   184
                    vcr = vcrmod.VCR(
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43552
diff changeset
   185
                        serializer='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
   186
                        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
   187
                        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
   188
                        custom_patches=[
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   189
                            (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   190
                                urlmod,
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43552
diff changeset
   191
                                'httpconnection',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   192
                                stubs.VCRHTTPConnection,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   193
                            ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   194
                            (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   195
                                urlmod,
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43552
diff changeset
   196
                                'httpsconnection',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   197
                                stubs.VCRHTTPSConnection,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   198
                            ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   199
                        ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   200
                    )
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43552
diff changeset
   201
                    vcr.register_matcher('hgmatcher', hgmatcher)
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43552
diff changeset
   202
                    with vcr.use_cassette(cassette, match_on=['hgmatcher']):
40378
b015f30a91fb phabricator: do more of the VCR work in demandimport.deactivated()
Augie Fackler <augie@google.com>
parents: 40151
diff changeset
   203
                        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
   204
            return fn(*args, **kwargs)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   205
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   206
        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
   207
        inner.__doc__ = fn.__doc__
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   208
        return command(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   209
            name,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   210
            fullflags,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   211
            spec,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   212
            helpcategory=helpcategory,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   213
            optionalrepo=optionalrepo,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   214
        )(inner)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   215
39666
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   216
    return decorate
d8f07b16abfc phabricator: add support for using the vcr library to mock interactions
Augie Fackler <raf@durin42.com>
parents: 38983
diff changeset
   217
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   218
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   219
def urlencodenested(params):
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   220
    """like urlencode, but works with nested parameters.
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   221
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   222
    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
   223
    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
   224
    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
   225
    """
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   226
    flatparams = util.sortdict()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   227
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   228
    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
   229
        if isinstance(obj, bool):
d7d3164e6a31 phabricator: properly encode boolean types in the request body
Matt Harbison <matt_harbison@yahoo.com>
parents: 40435
diff changeset
   230
            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
   231
        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
   232
        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
   233
        if items is None:
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   234
            flatparams[prefix] = obj
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   235
        else:
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   236
            for k, v in items(obj):
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   237
                if prefix:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   238
                    process(b'%s[%s]' % (prefix, k), v)
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   239
                else:
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   240
                    process(k, v)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   241
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   242
    process(b'', params)
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   243
    return util.urlreq.urlencode(flatparams)
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   244
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   245
42449
500b64c5d991 phabricator: pass ui into readurltoken instead of passing repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42447
diff changeset
   246
def readurltoken(ui):
38039
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
   247
    """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
   248
38040
71cf20d47f25 phabricator: split auth.url into the standard auth.schemes and auth.prefix
Matt Harbison <matt_harbison@yahoo.com>
parents: 38039
diff changeset
   249
    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
   250
    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
   251
    """
42449
500b64c5d991 phabricator: pass ui into readurltoken instead of passing repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42447
diff changeset
   252
    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
   253
    if not url:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   254
        raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   255
            _(b'config %s.%s is required') % (b'phabricator', b'url')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   256
        )
38039
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
   257
42449
500b64c5d991 phabricator: pass ui into readurltoken instead of passing repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42447
diff changeset
   258
    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
   259
    token = None
38039
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
   260
38040
71cf20d47f25 phabricator: split auth.url into the standard auth.schemes and auth.prefix
Matt Harbison <matt_harbison@yahoo.com>
parents: 38039
diff changeset
   261
    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
   262
        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
   263
42449
500b64c5d991 phabricator: pass ui into readurltoken instead of passing repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42447
diff changeset
   264
        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
   265
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   266
        token = auth.get(b'phabtoken')
38039
0fa050bc68cb phabricator: migrate [phabricator.auth] to [auth]
Matt Harbison <matt_harbison@yahoo.com>
parents: 38020
diff changeset
   267
36795
4397909f82d3 phabricator: specify API tokens per host, rather than per repo
Tom Prince <mozilla@hocat.ca>
parents: 36543
diff changeset
   268
    if not token:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   269
        raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   270
            _(b'Can\'t find conduit token associated to %s') % (url,)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   271
        )
36795
4397909f82d3 phabricator: specify API tokens per host, rather than per repo
Tom Prince <mozilla@hocat.ca>
parents: 36543
diff changeset
   272
4397909f82d3 phabricator: specify API tokens per host, rather than per repo
Tom Prince <mozilla@hocat.ca>
parents: 36543
diff changeset
   273
    return url, token
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   274
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   275
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   276
def callconduit(ui, name, params):
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   277
    """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
   278
    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
   279
    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
   280
    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
   281
    params = params.copy()
43261
f5aa4a53acd1 phabricator: change conduit data format to match arcanist
Ian Moody <moz-ian@perix.co.uk>
parents: 43259
diff changeset
   282
    params[b'__conduit__'] = {
f5aa4a53acd1 phabricator: change conduit data format to match arcanist
Ian Moody <moz-ian@perix.co.uk>
parents: 43259
diff changeset
   283
        b'token': token,
f5aa4a53acd1 phabricator: change conduit data format to match arcanist
Ian Moody <moz-ian@perix.co.uk>
parents: 43259
diff changeset
   284
    }
f5aa4a53acd1 phabricator: change conduit data format to match arcanist
Ian Moody <moz-ian@perix.co.uk>
parents: 43259
diff changeset
   285
    rawdata = {
f5aa4a53acd1 phabricator: change conduit data format to match arcanist
Ian Moody <moz-ian@perix.co.uk>
parents: 43259
diff changeset
   286
        b'params': templatefilters.json(params),
f5aa4a53acd1 phabricator: change conduit data format to match arcanist
Ian Moody <moz-ian@perix.co.uk>
parents: 43259
diff changeset
   287
        b'output': b'json',
f5aa4a53acd1 phabricator: change conduit data format to match arcanist
Ian Moody <moz-ian@perix.co.uk>
parents: 43259
diff changeset
   288
        b'__conduit__': 1,
f5aa4a53acd1 phabricator: change conduit data format to match arcanist
Ian Moody <moz-ian@perix.co.uk>
parents: 43259
diff changeset
   289
    }
f5aa4a53acd1 phabricator: change conduit data format to match arcanist
Ian Moody <moz-ian@perix.co.uk>
parents: 43259
diff changeset
   290
    data = urlencodenested(rawdata)
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   291
    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
   292
    if curlcmd:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   293
        sin, sout = procutil.popen2(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   294
            b'%s -d @- %s' % (curlcmd, procutil.shellquote(url))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   295
        )
34081
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
   296
        sin.write(data)
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
   297
        sin.close()
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
   298
        body = sout.read()
8b659b7388c0 phabricator: add a config to use curl for communication
Jun Wu <quark@fb.com>
parents: 34080
diff changeset
   299
    else:
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   300
        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
   301
        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
   302
        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
   303
            body = rsp.read()
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   304
    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
   305
    parsed = pycompat.rapply(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   306
        lambda x: encoding.unitolocal(x)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   307
        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
   308
        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
   309
        # json.loads only accepts bytes from py3.6+
43380
579672b347d2 py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43350
diff changeset
   310
        pycompat.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
   311
    )
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   312
    if parsed.get(b'error_code'):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   313
        msg = _(b'Conduit Error (%s): %s') % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   314
            parsed[b'error_code'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   315
            parsed[b'error_info'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   316
        )
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   317
        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
   318
    return parsed[b'result']
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   319
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   320
42451
16312ea45a8b phabricator: make `hg debugcallconduit` work outside a hg repo
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42450
diff changeset
   321
@vcrcommand(b'debugcallconduit', [], _(b'METHOD'), optionalrepo=True)
33200
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   322
def debugcallconduit(ui, repo, name):
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   323
    """call Conduit API
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   324
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   325
    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
   326
    to stdout as a JSON blob.
36b3febd739f phabricator: add a contrib script
Jun Wu <quark@fb.com>
parents:
diff changeset
   327
    """
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
   328
    # 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
   329
    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
   330
    # json.loads only returns unicode strings
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   331
    params = pycompat.rapply(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   332
        lambda x: encoding.unitolocal(x)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   333
        if isinstance(x, pycompat.unicode)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   334
        else x,
43380
579672b347d2 py3: define and use json.loads polyfill
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43350
diff changeset
   335
        pycompat.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
   336
    )
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
   337
    # json.dumps only accepts unicode strings
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   338
    result = pycompat.rapply(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   339
        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
   340
        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
   341
    )
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
   342
    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
   343
    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
   344
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   345
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   346
def getrepophid(repo):
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   347
    """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
   348
    # 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
   349
    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
   350
    if repophid:
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   351
        return repophid
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   352
    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
   353
    if not callsign:
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   354
        return None
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   355
    query = callconduit(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   356
        repo.ui,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   357
        b'diffusion.repository.search',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   358
        {b'constraints': {b'callsigns': [callsign]}},
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   359
    )
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   360
    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
   361
        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
   362
    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
   363
    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
   364
    return repophid
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   365
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   366
41546
bd3f03d8cc9f global: use raw strings for regular expressions with escapes
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41387
diff changeset
   367
_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
   368
_differentialrevisiondescre = re.compile(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   369
    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
   370
)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   371
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   372
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   373
def getoldnodedrevmap(repo, nodelist):
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   374
    """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
   375
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   376
    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
   377
    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
   378
    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
   379
    be ``None``.
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   380
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   381
    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
   382
    association information.
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
    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
   385
    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
   386
    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
   387
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   388
    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
   389
    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
   390
    """
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   391
    unfi = repo.unfiltered()
43597
4cb3f5bb29ec index: use `index.has_node` in `phabricator.getoldnodedrevmap`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43554
diff changeset
   392
    has_node = unfi.changelog.index.has_node
33263
ed61189763ef phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents: 33202
diff changeset
   393
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   394
    result = {}  # {node: (oldnode?, lastdiff?, drev)}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   395
    toconfirm = {}  # {node: (force, {precnode}, drev)}
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   396
    for node in nodelist:
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   397
        ctx = unfi[node]
33443
e48082e0a8d5 phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents: 33442
diff changeset
   398
        # 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
   399
        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
   400
        for n in precnodes:
43597
4cb3f5bb29ec index: use `index.has_node` in `phabricator.getoldnodedrevmap`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43554
diff changeset
   401
            if has_node(n):
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   402
                for tag in unfi.nodetags(n):
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   403
                    m = _differentialrevisiontagre.match(tag)
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   404
                    if m:
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   405
                        toconfirm[node] = (0, set(precnodes), int(m.group(1)))
43847
16b607e9f714 phabricator: fix processing of tags/desc in getoldnodedrevmap()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43597
diff changeset
   406
                        break
16b607e9f714 phabricator: fix processing of tags/desc in getoldnodedrevmap()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43597
diff changeset
   407
                else:
16b607e9f714 phabricator: fix processing of tags/desc in getoldnodedrevmap()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43597
diff changeset
   408
                    continue  # move to next predecessor
16b607e9f714 phabricator: fix processing of tags/desc in getoldnodedrevmap()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43597
diff changeset
   409
                break  # found a tag, stop
16b607e9f714 phabricator: fix processing of tags/desc in getoldnodedrevmap()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43597
diff changeset
   410
        else:
16b607e9f714 phabricator: fix processing of tags/desc in getoldnodedrevmap()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43597
diff changeset
   411
            # Check commit message
16b607e9f714 phabricator: fix processing of tags/desc in getoldnodedrevmap()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43597
diff changeset
   412
            m = _differentialrevisiondescre.search(ctx.description())
16b607e9f714 phabricator: fix processing of tags/desc in getoldnodedrevmap()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43597
diff changeset
   413
            if m:
16b607e9f714 phabricator: fix processing of tags/desc in getoldnodedrevmap()
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 43597
diff changeset
   414
                toconfirm[node] = (1, set(precnodes), int(m.group('id')))
33263
ed61189763ef phabricator: check associated Differential Revision from commit message
Jun Wu <quark@fb.com>
parents: 33202
diff changeset
   415
33443
e48082e0a8d5 phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents: 33442
diff changeset
   416
    # 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
   417
    # Phabricator, and expect precursors overlap with it.
e48082e0a8d5 phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents: 33442
diff changeset
   418
    if toconfirm:
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   419
        drevs = [drev for force, precs, drev in toconfirm.values()]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   420
        alldiffs = callconduit(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   421
            unfi.ui, b'differential.querydiffs', {b'revisionIDs': drevs}
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
        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
   424
        for newnode, (force, precset, drev) in toconfirm.items():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   425
            diffs = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   426
                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
   427
            ]
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   428
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   429
            # "precursors" as known by Phabricator
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   430
            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
   431
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   432
            # 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
   433
            # 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
   434
            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
   435
                tagname = b'D%d' % drev
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   436
                tags.tag(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   437
                    repo,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   438
                    tagname,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   439
                    nullid,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   440
                    message=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   441
                    user=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   442
                    date=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   443
                    local=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   444
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   445
                unfi.ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   446
                    _(
43399
742065def6ca py3: use %d to format an int
Ian Moody <moz-ian@perix.co.uk>
parents: 43380
diff changeset
   447
                        b'D%d: local tag removed - does not match '
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   448
                        b'Differential history\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   449
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   450
                    % drev
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   451
                )
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   452
                continue
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   453
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   454
            # 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
   455
            # exists in the repo
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   456
            oldnode = lastdiff = None
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   457
            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
   458
                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
   459
                oldnode = getnode(lastdiff)
43597
4cb3f5bb29ec index: use `index.has_node` in `phabricator.getoldnodedrevmap`
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43554
diff changeset
   460
                if oldnode and not has_node(oldnode):
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   461
                    oldnode = None
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   462
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
   463
            result[newnode] = (oldnode, lastdiff, drev)
33443
e48082e0a8d5 phabricator: verify local tags before trusting them
Jun Wu <quark@fb.com>
parents: 33442
diff changeset
   464
33442
3ab0d5767b54 phabricator: finding old nodes in batch
Jun Wu <quark@fb.com>
parents: 33441
diff changeset
   465
    return result
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   466
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   467
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   468
def getdiff(ctx, diffopts):
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   469
    """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
   470
    output = util.stringio()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   471
    for chunk, _label in patch.diffui(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   472
        ctx.repo(), ctx.p1().node(), ctx.node(), None, opts=diffopts
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   473
    ):
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   474
        output.write(chunk)
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   475
    return output.getvalue()
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   476
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   477
43182
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   478
class DiffChangeType(object):
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   479
    ADD = 1
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   480
    CHANGE = 2
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   481
    DELETE = 3
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   482
    MOVE_AWAY = 4
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   483
    COPY_AWAY = 5
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   484
    MOVE_HERE = 6
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   485
    COPY_HERE = 7
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   486
    MULTICOPY = 8
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   487
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   488
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   489
class DiffFileType(object):
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   490
    TEXT = 1
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   491
    IMAGE = 2
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   492
    BINARY = 3
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   493
a66e2844b0c6 phabricator: add the DiffChangeType and DiffFileType constants
Ian Moody <moz-ian@perix.co.uk>
parents: 43117
diff changeset
   494
43183
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   495
@attr.s
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   496
class phabhunk(dict):
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   497
    """Represents a Differential hunk, which is owned by a Differential change
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   498
    """
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   499
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   500
    oldOffset = attr.ib(default=0)  # camelcase-required
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   501
    oldLength = attr.ib(default=0)  # camelcase-required
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   502
    newOffset = attr.ib(default=0)  # camelcase-required
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   503
    newLength = attr.ib(default=0)  # camelcase-required
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   504
    corpus = attr.ib(default='')
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   505
    # These get added to the phabchange's equivalents
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   506
    addLines = attr.ib(default=0)  # camelcase-required
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   507
    delLines = attr.ib(default=0)  # camelcase-required
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   508
73d4bc60e389 phabricator: add the phabhunk data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43182
diff changeset
   509
43184
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   510
@attr.s
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   511
class phabchange(object):
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   512
    """Represents a Differential change, owns Differential hunks and owned by a
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   513
    Differential diff.  Each one represents one file in a diff.
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   514
    """
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   515
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   516
    currentPath = attr.ib(default=None)  # camelcase-required
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   517
    oldPath = attr.ib(default=None)  # camelcase-required
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   518
    awayPaths = attr.ib(default=attr.Factory(list))  # camelcase-required
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   519
    metadata = attr.ib(default=attr.Factory(dict))
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   520
    oldProperties = attr.ib(default=attr.Factory(dict))  # camelcase-required
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   521
    newProperties = attr.ib(default=attr.Factory(dict))  # camelcase-required
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   522
    type = attr.ib(default=DiffChangeType.CHANGE)
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   523
    fileType = attr.ib(default=DiffFileType.TEXT)  # camelcase-required
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   524
    commitHash = attr.ib(default=None)  # camelcase-required
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   525
    addLines = attr.ib(default=0)  # camelcase-required
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   526
    delLines = attr.ib(default=0)  # camelcase-required
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   527
    hunks = attr.ib(default=attr.Factory(list))
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   528
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   529
    def copynewmetadatatoold(self):
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   530
        for key in list(self.metadata.keys()):
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   531
            newkey = key.replace(b'new:', b'old:')
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   532
            self.metadata[newkey] = self.metadata[key]
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   533
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   534
    def addoldmode(self, value):
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   535
        self.oldProperties[b'unix:filemode'] = value
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   536
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   537
    def addnewmode(self, value):
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   538
        self.newProperties[b'unix:filemode'] = value
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   539
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   540
    def addhunk(self, hunk):
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   541
        if not isinstance(hunk, phabhunk):
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   542
            raise error.Abort(b'phabchange.addhunk only takes phabhunks')
43259
162b81e65e60 phabricator: convert phabhunk and phabchange keys to bytes when finalising
Ian Moody <moz-ian@perix.co.uk>
parents: 43258
diff changeset
   543
        self.hunks.append(pycompat.byteskwargs(attr.asdict(hunk)))
43184
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   544
        # It's useful to include these stats since the Phab web UI shows them,
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   545
        # and uses them to estimate how large a change a Revision is. Also used
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   546
        # in email subjects for the [+++--] bit.
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   547
        self.addLines += hunk.addLines
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   548
        self.delLines += hunk.delLines
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   549
99ee4afd352f phabricator: add the phabchange data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43183
diff changeset
   550
43185
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   551
@attr.s
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   552
class phabdiff(object):
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   553
    """Represents a Differential diff, owns Differential changes.  Corresponds
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   554
    to a commit.
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   555
    """
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   556
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   557
    # Doesn't seem to be any reason to send this (output of uname -n)
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   558
    sourceMachine = attr.ib(default=b'')  # camelcase-required
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   559
    sourcePath = attr.ib(default=b'/')  # camelcase-required
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   560
    sourceControlBaseRevision = attr.ib(default=b'0' * 40)  # camelcase-required
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   561
    sourceControlPath = attr.ib(default=b'/')  # camelcase-required
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   562
    sourceControlSystem = attr.ib(default=b'hg')  # camelcase-required
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   563
    branch = attr.ib(default=b'default')
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   564
    bookmark = attr.ib(default=None)
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   565
    creationMethod = attr.ib(default=b'phabsend')  # camelcase-required
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   566
    lintStatus = attr.ib(default=b'none')  # camelcase-required
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   567
    unitStatus = attr.ib(default=b'none')  # camelcase-required
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   568
    changes = attr.ib(default=attr.Factory(dict))
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   569
    repositoryPHID = attr.ib(default=None)  # camelcase-required
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   570
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   571
    def addchange(self, change):
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   572
        if not isinstance(change, phabchange):
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   573
            raise error.Abort(b'phabdiff.addchange only takes phabchanges')
43259
162b81e65e60 phabricator: convert phabhunk and phabchange keys to bytes when finalising
Ian Moody <moz-ian@perix.co.uk>
parents: 43258
diff changeset
   574
        self.changes[change.currentPath] = pycompat.byteskwargs(
162b81e65e60 phabricator: convert phabhunk and phabchange keys to bytes when finalising
Ian Moody <moz-ian@perix.co.uk>
parents: 43258
diff changeset
   575
            attr.asdict(change)
162b81e65e60 phabricator: convert phabhunk and phabchange keys to bytes when finalising
Ian Moody <moz-ian@perix.co.uk>
parents: 43258
diff changeset
   576
        )
43185
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   577
75e7628b488f phabricator: add the phabdiff data structure
Ian Moody <moz-ian@perix.co.uk>
parents: 43184
diff changeset
   578
43186
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   579
def maketext(pchange, ctx, fname):
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   580
    """populate the phabchange for a text file"""
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   581
    repo = ctx.repo()
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   582
    fmatcher = match.exact([fname])
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   583
    diffopts = mdiff.diffopts(git=True, context=32767)
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   584
    _pfctx, _fctx, header, fhunks = next(
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   585
        patch.diffhunks(repo, ctx.p1(), ctx, fmatcher, opts=diffopts)
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   586
    )
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   587
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   588
    for fhunk in fhunks:
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   589
        (oldOffset, oldLength, newOffset, newLength), lines = fhunk
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   590
        corpus = b''.join(lines[1:])
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   591
        shunk = list(header)
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   592
        shunk.extend(lines)
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   593
        _mf, _mt, addLines, delLines, _hb = patch.diffstatsum(
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   594
            patch.diffstatdata(util.iterlines(shunk))
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   595
        )
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   596
        pchange.addhunk(
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   597
            phabhunk(
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   598
                oldOffset,
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   599
                oldLength,
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   600
                newOffset,
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   601
                newLength,
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   602
                corpus,
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   603
                addLines,
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   604
                delLines,
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   605
            )
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   606
        )
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   607
f742fabad507 phabricator: add the maketext function
Ian Moody <moz-ian@perix.co.uk>
parents: 43185
diff changeset
   608
43187
453079605242 phabricator: add the uploadchunks function
Ian Moody <moz-ian@perix.co.uk>
parents: 43186
diff changeset
   609
def uploadchunks(fctx, fphid):
453079605242 phabricator: add the uploadchunks function
Ian Moody <moz-ian@perix.co.uk>
parents: 43186
diff changeset
   610
    """upload large binary files as separate chunks.
453079605242 phabricator: add the uploadchunks function
Ian Moody <moz-ian@perix.co.uk>
parents: 43186
diff changeset
   611
    Phab requests chunking over 8MiB, and splits into 4MiB chunks
453079605242 phabricator: add the uploadchunks function
Ian Moody <moz-ian@perix.co.uk>
parents: 43186
diff changeset
   612
    """
453079605242 phabricator: add the uploadchunks function
Ian Moody <moz-ian@perix.co.uk>
parents: 43186
diff changeset
   613
    ui = fctx.repo().ui
453079605242 phabricator: add the uploadchunks function
Ian Moody <moz-ian@perix.co.uk>
parents: 43186
diff changeset
   614
    chunks = callconduit(ui, b'file.querychunks', {b'filePHID': fphid})
43552
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   615
    with ui.makeprogress(
43187
453079605242 phabricator: add the uploadchunks function
Ian Moody <moz-ian@perix.co.uk>
parents: 43186
diff changeset
   616
        _(b'uploading file chunks'), unit=_(b'chunks'), total=len(chunks)
43552
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   617
    ) as progress:
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   618
        for chunk in chunks:
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   619
            progress.increment()
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   620
            if chunk[b'complete']:
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   621
                continue
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   622
            bstart = int(chunk[b'byteStart'])
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   623
            bend = int(chunk[b'byteEnd'])
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   624
            callconduit(
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   625
                ui,
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   626
                b'file.uploadchunk',
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   627
                {
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   628
                    b'filePHID': fphid,
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   629
                    b'byteStart': bstart,
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   630
                    b'data': base64.b64encode(fctx.data()[bstart:bend]),
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   631
                    b'dataEncoding': b'base64',
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   632
                },
a78a65c33b5a phabricator: use context manager form of progress in uploadchunks
Ian Moody <moz-ian@perix.co.uk>
parents: 43399
diff changeset
   633
            )
43187
453079605242 phabricator: add the uploadchunks function
Ian Moody <moz-ian@perix.co.uk>
parents: 43186
diff changeset
   634
453079605242 phabricator: add the uploadchunks function
Ian Moody <moz-ian@perix.co.uk>
parents: 43186
diff changeset
   635
43188
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   636
def uploadfile(fctx):
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   637
    """upload binary files to Phabricator"""
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   638
    repo = fctx.repo()
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   639
    ui = repo.ui
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   640
    fname = fctx.path()
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   641
    size = fctx.size()
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   642
    fhash = pycompat.bytestr(hashlib.sha256(fctx.data()).hexdigest())
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   643
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   644
    # an allocate call is required first to see if an upload is even required
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   645
    # (Phab might already have it) and to determine if chunking is needed
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   646
    allocateparams = {
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   647
        b'name': fname,
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   648
        b'contentLength': size,
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   649
        b'contentHash': fhash,
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   650
    }
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   651
    filealloc = callconduit(ui, b'file.allocate', allocateparams)
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   652
    fphid = filealloc[b'filePHID']
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   653
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   654
    if filealloc[b'upload']:
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   655
        ui.write(_(b'uploading %s\n') % bytes(fctx))
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   656
        if not fphid:
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   657
            uploadparams = {
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   658
                b'name': fname,
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   659
                b'data_base64': base64.b64encode(fctx.data()),
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   660
            }
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   661
            fphid = callconduit(ui, b'file.upload', uploadparams)
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   662
        else:
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   663
            uploadchunks(fctx, fphid)
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   664
    else:
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   665
        ui.debug(b'server already has %s\n' % bytes(fctx))
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   666
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   667
    if not fphid:
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   668
        raise error.Abort(b'Upload of %s failed.' % bytes(fctx))
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   669
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   670
    return fphid
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   671
24e8aac7c630 phabricator: add the uploadfile function
Ian Moody <moz-ian@perix.co.uk>
parents: 43187
diff changeset
   672
43189
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   673
def addoldbinary(pchange, fctx, originalfname):
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   674
    """add the metadata for the previous version of a binary file to the
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   675
    phabchange for the new version
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   676
    """
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   677
    oldfctx = fctx.p1()[originalfname]
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   678
    if fctx.cmp(oldfctx):
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   679
        # Files differ, add the old one
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   680
        pchange.metadata[b'old:file:size'] = oldfctx.size()
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   681
        mimeguess, _enc = mimetypes.guess_type(
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   682
            encoding.unifromlocal(oldfctx.path())
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   683
        )
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   684
        if mimeguess:
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   685
            pchange.metadata[b'old:file:mime-type'] = pycompat.bytestr(
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   686
                mimeguess
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   687
            )
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   688
        fphid = uploadfile(oldfctx)
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   689
        pchange.metadata[b'old:binary-phid'] = fphid
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   690
    else:
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   691
        # If it's left as IMAGE/BINARY web UI might try to display it
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   692
        pchange.fileType = DiffFileType.TEXT
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   693
        pchange.copynewmetadatatoold()
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   694
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   695
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   696
def makebinary(pchange, fctx):
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   697
    """populate the phabchange for a binary file"""
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   698
    pchange.fileType = DiffFileType.BINARY
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   699
    fphid = uploadfile(fctx)
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   700
    pchange.metadata[b'new:binary-phid'] = fphid
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   701
    pchange.metadata[b'new:file:size'] = fctx.size()
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   702
    mimeguess, _enc = mimetypes.guess_type(encoding.unifromlocal(fctx.path()))
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   703
    if mimeguess:
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   704
        mimeguess = pycompat.bytestr(mimeguess)
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   705
        pchange.metadata[b'new:file:mime-type'] = mimeguess
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   706
        if mimeguess.startswith(b'image/'):
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   707
            pchange.fileType = DiffFileType.IMAGE
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   708
9f802243a42e phabricator: add makebinary and addoldbinary functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43188
diff changeset
   709
43190
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   710
# Copied from mercurial/patch.py
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   711
gitmode = {b'l': b'120000', b'x': b'100755', b'': b'100644'}
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   712
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   713
43263
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   714
def notutf8(fctx):
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   715
    """detect non-UTF-8 text files since Phabricator requires them to be marked
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   716
    as binary
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   717
    """
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   718
    try:
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   719
        fctx.data().decode('utf-8')
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   720
        if fctx.parents():
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   721
            fctx.p1().data().decode('utf-8')
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   722
        return False
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   723
    except UnicodeDecodeError:
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   724
        fctx.repo().ui.write(
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   725
            _(b'file %s detected as non-UTF-8, marked as binary\n')
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   726
            % fctx.path()
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   727
        )
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   728
        return True
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   729
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   730
43190
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   731
def addremoved(pdiff, ctx, removed):
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   732
    """add removed files to the phabdiff. Shouldn't include moves"""
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   733
    for fname in removed:
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   734
        pchange = phabchange(
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   735
            currentPath=fname, oldPath=fname, type=DiffChangeType.DELETE
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   736
        )
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   737
        pchange.addoldmode(gitmode[ctx.p1()[fname].flags()])
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   738
        fctx = ctx.p1()[fname]
43263
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   739
        if not (fctx.isbinary() or notutf8(fctx)):
43190
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   740
            maketext(pchange, ctx, fname)
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   741
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   742
        pdiff.addchange(pchange)
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   743
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   744
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   745
def addmodified(pdiff, ctx, modified):
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   746
    """add modified files to the phabdiff"""
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   747
    for fname in modified:
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   748
        fctx = ctx[fname]
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   749
        pchange = phabchange(currentPath=fname, oldPath=fname)
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   750
        filemode = gitmode[ctx[fname].flags()]
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   751
        originalmode = gitmode[ctx.p1()[fname].flags()]
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   752
        if filemode != originalmode:
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   753
            pchange.addoldmode(originalmode)
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   754
            pchange.addnewmode(filemode)
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   755
43263
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   756
        if fctx.isbinary() or notutf8(fctx):
43190
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   757
            makebinary(pchange, fctx)
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   758
            addoldbinary(pchange, fctx, fname)
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   759
        else:
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   760
            maketext(pchange, ctx, fname)
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   761
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   762
        pdiff.addchange(pchange)
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   763
c19b327017b9 phabricator: add addremoved and addmodified functions
Ian Moody <moz-ian@perix.co.uk>
parents: 43189
diff changeset
   764
43258
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   765
def addadded(pdiff, ctx, added, removed):
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   766
    """add file adds to the phabdiff, both new files and copies/moves"""
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   767
    # Keep track of files that've been recorded as moved/copied, so if there are
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   768
    # additional copies we can mark them (moves get removed from removed)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   769
    copiedchanges = {}
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   770
    movedchanges = {}
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   771
    for fname in added:
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   772
        fctx = ctx[fname]
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   773
        pchange = phabchange(currentPath=fname)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   774
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   775
        filemode = gitmode[ctx[fname].flags()]
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   776
        renamed = fctx.renamed()
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   777
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   778
        if renamed:
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   779
            originalfname = renamed[0]
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   780
            originalmode = gitmode[ctx.p1()[originalfname].flags()]
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   781
            pchange.oldPath = originalfname
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   782
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   783
            if originalfname in removed:
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   784
                origpchange = phabchange(
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   785
                    currentPath=originalfname,
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   786
                    oldPath=originalfname,
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   787
                    type=DiffChangeType.MOVE_AWAY,
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   788
                    awayPaths=[fname],
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   789
                )
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   790
                movedchanges[originalfname] = origpchange
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   791
                removed.remove(originalfname)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   792
                pchange.type = DiffChangeType.MOVE_HERE
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   793
            elif originalfname in movedchanges:
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   794
                movedchanges[originalfname].type = DiffChangeType.MULTICOPY
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   795
                movedchanges[originalfname].awayPaths.append(fname)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   796
                pchange.type = DiffChangeType.COPY_HERE
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   797
            else:  # pure copy
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   798
                if originalfname not in copiedchanges:
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   799
                    origpchange = phabchange(
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   800
                        currentPath=originalfname, type=DiffChangeType.COPY_AWAY
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   801
                    )
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   802
                    copiedchanges[originalfname] = origpchange
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   803
                else:
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   804
                    origpchange = copiedchanges[originalfname]
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   805
                origpchange.awayPaths.append(fname)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   806
                pchange.type = DiffChangeType.COPY_HERE
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   807
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   808
            if filemode != originalmode:
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   809
                pchange.addoldmode(originalmode)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   810
                pchange.addnewmode(filemode)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   811
        else:  # Brand-new file
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   812
            pchange.addnewmode(gitmode[fctx.flags()])
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   813
            pchange.type = DiffChangeType.ADD
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   814
43263
06a33a501aa2 phabricator: treat non-utf-8 text files as binary as phabricator requires
Ian Moody <moz-ian@perix.co.uk>
parents: 43262
diff changeset
   815
        if fctx.isbinary() or notutf8(fctx):
43258
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   816
            makebinary(pchange, fctx)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   817
            if renamed:
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   818
                addoldbinary(pchange, fctx, originalfname)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   819
        else:
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   820
            maketext(pchange, ctx, fname)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   821
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   822
        pdiff.addchange(pchange)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   823
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   824
    for _path, copiedchange in copiedchanges.items():
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   825
        pdiff.addchange(copiedchange)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   826
    for _path, movedchange in movedchanges.items():
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   827
        pdiff.addchange(movedchange)
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   828
d5d1edf66091 phabricator: add addadded function
Ian Moody <moz-ian@perix.co.uk>
parents: 43190
diff changeset
   829
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   830
def creatediff(ctx):
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   831
    """create a Differential Diff"""
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   832
    repo = ctx.repo()
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   833
    repophid = getrepophid(repo)
43262
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   834
    # Create a "Differential Diff" via "differential.creatediff" API
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   835
    pdiff = phabdiff(
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   836
        sourceControlBaseRevision=b'%s' % ctx.p1().hex(),
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   837
        branch=b'%s' % ctx.branch(),
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   838
    )
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   839
    modified, added, removed, _d, _u, _i, _c = ctx.p1().status(ctx)
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   840
    # addadded will remove moved files from removed, so addremoved won't get
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   841
    # them
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   842
    addadded(pdiff, ctx, added, removed)
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   843
    addmodified(pdiff, ctx, modified)
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   844
    addremoved(pdiff, ctx, removed)
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   845
    if repophid:
43262
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   846
        pdiff.repositoryPHID = repophid
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   847
    diff = callconduit(
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   848
        repo.ui,
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   849
        b'differential.creatediff',
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   850
        pycompat.byteskwargs(attr.asdict(pdiff)),
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   851
    )
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   852
    if not diff:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   853
        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
   854
    return diff
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   855
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   856
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   857
def writediffproperties(ctx, diff):
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   858
    """write metadata to diff so patches could be applied losslessly"""
43262
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   859
    # creatediff returns with a diffid but query returns with an id
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   860
    diffid = diff.get(b'diffid', diff.get(b'id'))
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   861
    params = {
43262
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   862
        b'diff_id': diffid,
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   863
        b'name': b'hg:meta',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   864
        b'data': templatefilters.json(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   865
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   866
                b'user': ctx.user(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   867
                b'date': b'%d %d' % ctx.date(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   868
                b'branch': ctx.branch(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   869
                b'node': ctx.hex(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   870
                b'parent': ctx.p1().hex(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   871
            }
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   872
        ),
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   873
    }
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   874
    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
   875
37800
6cf5f5b4eb57 phabricator: specify some metadata compatibly with arc
Tom Prince <mozilla@hocat.ca>
parents: 37123
diff changeset
   876
    params = {
43262
af067d29b19e phabricator: switch to the creatediff endpoint
Ian Moody <moz-ian@perix.co.uk>
parents: 43261
diff changeset
   877
        b'diff_id': diffid,
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   878
        b'name': b'local:commits',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   879
        b'data': templatefilters.json(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   880
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   881
                ctx.hex(): {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   882
                    b'author': stringutil.person(ctx.user()),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   883
                    b'authorEmail': stringutil.email(ctx.user()),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   884
                    b'time': int(ctx.date()[0]),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   885
                    b'commit': ctx.hex(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   886
                    b'parents': [ctx.p1().hex()],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   887
                    b'branch': ctx.branch(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   888
                },
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   889
            }
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   890
        ),
37800
6cf5f5b4eb57 phabricator: specify some metadata compatibly with arc
Tom Prince <mozilla@hocat.ca>
parents: 37123
diff changeset
   891
    }
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   892
    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
   893
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   894
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   895
def createdifferentialrevision(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   896
    ctx,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   897
    revid=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   898
    parentrevphid=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   899
    oldnode=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   900
    olddiff=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   901
    actions=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   902
    comment=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   903
):
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   904
    """create or update a Differential Revision
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   905
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   906
    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
   907
    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
   908
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   909
    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
   910
    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
   911
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   912
    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
   913
    """
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   914
    repo = ctx.repo()
33265
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   915
    if oldnode:
33994
27ff2a87d8c0 phabsend: detect patch change with larger context
Jun Wu <quark@fb.com>
parents: 33993
diff changeset
   916
        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
   917
        oldctx = repo.unfiltered()[oldnode]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   918
        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
   919
    else:
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   920
        neednewdiff = True
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   921
33265
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   922
    transactions = []
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   923
    if neednewdiff:
95f658b558a3 phabricator: do not upload new diff if nothing changes
Jun Wu <quark@fb.com>
parents: 33264
diff changeset
   924
        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
   925
        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
   926
        if comment:
29528c4235a1 phabricator: add commenting to phabsend for new/updated Diffs
Ian Moody <moz-ian@perix.co.uk>
parents: 42295
diff changeset
   927
            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
   928
    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
   929
        # 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
   930
        # 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
   931
        # 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
   932
        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
   933
        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
   934
    writediffproperties(ctx, diff)
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   935
42465
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
   936
    # 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
   937
    if parentrevphid:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   938
        transactions.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   939
            {b'type': b'parents.set', b'value': [parentrevphid]}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   940
        )
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   941
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   942
    if actions:
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   943
        transactions += actions
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   944
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   945
    # 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
   946
    desc = ctx.description()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   947
    info = callconduit(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   948
        repo.ui, b'differential.parsecommitmessage', {b'corpus': desc}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   949
    )
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   950
    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
   951
        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
   952
            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
   953
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   954
    params = {b'transactions': transactions}
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   955
    if revid is not None:
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   956
        # 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
   957
        params[b'objectIdentifier'] = revid
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   958
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
   959
    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
   960
    if not revision:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
   961
        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
   962
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
   963
    return revision, diff
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
   964
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   965
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   966
def userphids(repo, names):
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   967
    """convert user names to PHIDs"""
41716
570e62f1dcf2 phabricator: make user searches case-insensitive
Julien Cristau <jcristau@mozilla.com>
parents: 41546
diff changeset
   968
    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
   969
    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
   970
    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
   971
    # 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
   972
    # 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
   973
    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
   974
    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
   975
    unresolved = set(names) - resolved
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
   976
    if unresolved:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   977
        raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   978
            _(b'unknown username: %s') % b' '.join(sorted(unresolved))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   979
        )
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
   980
    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
   981
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   982
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   983
@vcrcommand(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   984
    b'phabsend',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   985
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   986
        (b'r', b'rev', [], _(b'revisions to send'), _(b'REV')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   987
        (b'', b'amend', True, _(b'update commit messages')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   988
        (b'', b'reviewer', [], _(b'specify reviewers')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   989
        (b'', b'blocker', [], _(b'specify blocking reviewers')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   990
        (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   991
            b'm',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   992
            b'comment',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   993
            b'',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   994
            _(b'add a comment to Revisions with new/updated Diffs'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   995
        ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   996
        (b'', b'confirm', None, _(b'ask for confirmation before sending')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   997
    ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   998
    _(b'REV [OPTIONS]'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
   999
    helpcategory=command.CATEGORY_IMPORT_EXPORT,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1000
)
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1001
def phabsend(ui, repo, *revs, **opts):
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1002
    """upload changesets to Phabricator
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1003
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1004
    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
  1005
    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
  1006
    revset.
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1007
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1008
    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
  1009
    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
  1010
    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
  1011
    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
  1012
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1013
    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
  1014
    ``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
  1015
    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
  1016
    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
  1017
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1018
    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
  1019
    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
  1020
    behaviour::
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1021
33992
45a8cd74de4e phabsend: polish the docstring a bit
Jun Wu <quark@fb.com>
parents: 33855
diff changeset
  1022
        [phabsend]
45a8cd74de4e phabsend: polish the docstring a bit
Jun Wu <quark@fb.com>
parents: 33855
diff changeset
  1023
        confirm = true
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1024
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1025
    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
  1026
    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
  1027
    """
41975
51ba9fbcca52 py3: use pycompat.byteskwargs on opts in phabricator.py
Ian Moody <moz-ian@perix.co.uk>
parents: 41955
diff changeset
  1028
    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
  1029
    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
  1030
    revs = scmutil.revrange(repo, revs)
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1031
33266
5b2391b46906 phabricator: abort if phabsend gets empty revs
Jun Wu <quark@fb.com>
parents: 33265
diff changeset
  1032
    if not revs:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1033
        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
  1034
    if opts.get(b'amend'):
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1035
        cmdutil.checkunfinished(repo)
33266
5b2391b46906 phabricator: abort if phabsend gets empty revs
Jun Wu <quark@fb.com>
parents: 33265
diff changeset
  1036
33996
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
  1037
    # {newnode: (oldnode, olddiff, olddrev}
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
  1038
    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
  1039
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1040
    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
  1041
    confirm |= bool(opts.get(b'confirm'))
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1042
    if confirm:
33996
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
  1043
        confirmed = _confirmbeforesend(repo, revs, oldmap)
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1044
        if not confirmed:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1045
            raise error.Abort(_(b'phabsend cancelled'))
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1046
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
  1047
    actions = []
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1048
    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
  1049
    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
  1050
    phids = []
33498
b7a75b9a3386 phabricator: allow specifying reviewers on phabsend
Jun Wu <quark@fb.com>
parents: 33443
diff changeset
  1051
    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
  1052
        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
  1053
    if blockers:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1054
        phids.extend(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1055
            map(lambda phid: b'blocking(%s)' % phid, userphids(repo, blockers))
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1056
        )
42460
f33d3ee110da phabricator: add --blocker argument to phabsend to specify blocking reviewers
Ian Moody <moz-ian@perix.co.uk>
parents: 42459
diff changeset
  1057
    if phids:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1058
        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
  1059
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1060
    drevids = []  # [int]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1061
    diffmap = {}  # {newnode: diff}
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1062
42465
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
  1063
    # 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
  1064
    # 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
  1065
    lastrevphid = None
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1066
    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
  1067
        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
  1068
        ctx = repo[rev]
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1069
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1070
        # Get Differential Revision ID
33717
1664406a44d9 phabricator: use Phabricator's last node information
Jun Wu <quark@fb.com>
parents: 33716
diff changeset
  1071
        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
  1072
        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
  1073
            # Create or update Differential Revision
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1074
            revision, diff = createdifferentialrevision(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1075
                ctx,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1076
                revid,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1077
                lastrevphid,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1078
                oldnode,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1079
                olddiff,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1080
                actions,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1081
                opts.get(b'comment'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1082
            )
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1083
            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
  1084
            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
  1085
            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
  1086
            if revid:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1087
                action = b'updated'
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1088
            else:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1089
                action = b'created'
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1090
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1091
            # 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
  1092
            # does not have it already
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1093
            m = _differentialrevisiondescre.search(ctx.description())
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43552
diff changeset
  1094
            if not m or int(m.group('id')) != newrevid:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1095
                tagname = b'D%d' % newrevid
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1096
                tags.tag(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1097
                    repo,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1098
                    tagname,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1099
                    ctx.node(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1100
                    message=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1101
                    user=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1102
                    date=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1103
                    local=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1104
                )
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1105
        else:
42465
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
  1106
            # 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
  1107
            # 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
  1108
            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
  1109
            newrevid = revid
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1110
            action = b'skipped'
33201
228ad1e58a85 phabricator: add phabsend command to send a stack
Jun Wu <quark@fb.com>
parents: 33200
diff changeset
  1111
34080
941c33cfde81 phabricator: standardize colors
Jun Wu <quark@fb.com>
parents: 33996
diff changeset
  1112
        actiondesc = ui.label(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1113
            {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1114
                b'created': _(b'created'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1115
                b'skipped': _(b'skipped'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1116
                b'updated': _(b'updated'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1117
            }[action],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1118
            b'phabricator.action.%s' % action,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1119
        )
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
  1120
        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
  1121
        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
  1122
        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
  1123
        ui.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1124
            _(b'%s - %s - %s: %s\n') % (drevdesc, actiondesc, nodedesc, desc)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1125
        )
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1126
        drevids.append(newrevid)
42465
c19d259fd6ad phabricator: use parents.set to always set dependencies
Ian Moody <moz-ian@perix.co.uk>
parents: 42460
diff changeset
  1127
        lastrevphid = newrevphid
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1128
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1129
    # 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
  1130
    if opts.get(b'amend'):
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1131
        unfi = repo.unfiltered()
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
  1132
        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
  1133
        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
  1134
            wnode = unfi[b'.'].node()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1135
            mapping = {}  # {oldnode: [newnode]}
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1136
            for i, rev in enumerate(revs):
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1137
                old = unfi[rev]
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1138
                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
  1139
                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
  1140
                newdesc = getdescfromdrev(drev)
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1141
                # Make sure commit message contain "Differential Revision"
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1142
                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
  1143
                    if old.phase() == phases.public:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1144
                        ui.warn(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
  1145
                            _(b"warning: not updating public commit %s\n")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1146
                            % scmutil.formatchangeid(old)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1147
                        )
41163
0101a35deae2 phabricator: warn if unable to amend, instead of aborting after posting
Matt Harbison <matt_harbison@yahoo.com>
parents: 41083
diff changeset
  1148
                        continue
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1149
                    parents = [
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1150
                        mapping.get(old.p1().node(), (old.p1(),))[0],
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1151
                        mapping.get(old.p2().node(), (old.p2(),))[0],
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1152
                    ]
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1153
                    new = context.metadataonlyctx(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1154
                        repo,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1155
                        old,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1156
                        parents=parents,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1157
                        text=newdesc,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1158
                        user=old.user(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1159
                        date=old.date(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1160
                        extra=old.extra(),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1161
                    )
38342
bb7e3c6ef592 phabricator: preserve the phase when amending in the Differential fields
Matt Harbison <matt_harbison@yahoo.com>
parents: 38075
diff changeset
  1162
38429
32fba6fe893d scmutil: make cleanupnodes optionally also fix the phase
Martin von Zweigbergk <martinvonz@google.com>
parents: 38398
diff changeset
  1163
                    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
  1164
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1165
                    mapping[old.node()] = [newnode]
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1166
                    # 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
  1167
                    # 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
  1168
                    # 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
  1169
                    try:
e26c2440a030 phabricator: don't abort if property writing fails during amending
Ian Moody <moz-ian@perix.co.uk>
parents: 42618
diff changeset
  1170
                        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
  1171
                    except util.urlerr.urlerror:
43094
e8cf9ad52a78 formatting: run black on all file again
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43089
diff changeset
  1172
                        ui.warnnoi18n(
43321
a2ff3aff81d2 py3: use %d instead of %s when formatting an int into a bytestring
Ian Moody <moz-ian@perix.co.uk>
parents: 43282
diff changeset
  1173
                            b'Failed to update metadata for D%d\n' % drevid
43094
e8cf9ad52a78 formatting: run black on all file again
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43089
diff changeset
  1174
                        )
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1175
                # 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
  1176
                tagname = b'D%d' % drevid
33808
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1177
                if tagname in repo.tags():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1178
                    tags.tag(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1179
                        repo,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1180
                        tagname,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1181
                        nullid,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1182
                        message=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1183
                        user=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1184
                        date=None,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1185
                        local=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1186
                    )
38429
32fba6fe893d scmutil: make cleanupnodes optionally also fix the phase
Martin von Zweigbergk <martinvonz@google.com>
parents: 38398
diff changeset
  1187
            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
  1188
            if wnode in mapping:
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1189
                unfi.setparents(mapping[wnode][0])
fa3aa6c98bb7 phabricator: add --amend option to phabsend
Jun Wu <quark@fb.com>
parents: 33806
diff changeset
  1190
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1191
33264
266321579c68 phabricator: add node and p1 to hg:meta property
Jun Wu <quark@fb.com>
parents: 33263
diff changeset
  1192
# 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
  1193
# consistent with "hg export" output.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1194
_metanamemap = util.sortdict(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1195
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1196
        (b'user', b'User'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1197
        (b'date', b'Date'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1198
        (b'branch', b'Branch'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1199
        (b'node', b'Node ID'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1200
        (b'parent', b'Parent '),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1201
    ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1202
)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1203
33264
266321579c68 phabricator: add node and p1 to hg:meta property
Jun Wu <quark@fb.com>
parents: 33263
diff changeset
  1204
33996
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
  1205
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
  1206
    url, token = readurltoken(repo.ui)
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1207
    ui = repo.ui
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1208
    for rev in revs:
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1209
        ctx = repo[rev]
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1210
        desc = ctx.description().splitlines()[0]
33996
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
  1211
        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
  1212
        if drevid:
43321
a2ff3aff81d2 py3: use %d instead of %s when formatting an int into a bytestring
Ian Moody <moz-ian@perix.co.uk>
parents: 43282
diff changeset
  1213
            drevdesc = ui.label(b'D%d' % drevid, b'phabricator.drev')
33996
088598153aa2 phabsend: show associated Differential Revisions with --confirm
Jun Wu <quark@fb.com>
parents: 33995
diff changeset
  1214
        else:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1215
            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
  1216
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1217
        ui.write(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1218
            _(b'%s - %s: %s\n')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1219
            % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1220
                drevdesc,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1221
                ui.label(bytes(ctx), b'phabricator.node'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1222
                ui.label(desc, b'phabricator.desc'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1223
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1224
        )
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1225
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1226
    if ui.promptchoice(
43117
8ff1ecfadcd1 cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents: 43094
diff changeset
  1227
        _(b'Send the above changes to %s (yn)?$$ &Yes $$ &No') % url
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1228
    ):
33716
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1229
        return False
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1230
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1231
    return True
40cfe3197bc1 phabricator: add --confirm option to phabsend command
Pulkit Goyal <7895pulkit@gmail.com>
parents: 33601
diff changeset
  1232
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1233
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1234
_knownstatusnames = {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1235
    b'accepted',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1236
    b'needsreview',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1237
    b'needsrevision',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1238
    b'closed',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1239
    b'abandoned',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1240
}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1241
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1242
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1243
def _getstatusname(drev):
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1244
    """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
  1245
    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
  1246
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1247
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1248
# 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
  1249
# +, and -.
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1250
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1251
_elements = {
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1252
    # token-type: binding-strength, primary, prefix, infix, suffix
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1253
    b'(': (12, None, (b'group', 1, b')'), None, None),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1254
    b':': (8, None, (b'ancestors', 8), None, None),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1255
    b'&': (5, None, None, (b'and_', 5), None),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1256
    b'+': (4, None, None, (b'add', 4), None),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1257
    b'-': (4, None, None, (b'sub', 4), None),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1258
    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
  1259
    b'symbol': (0, b'symbol', None, None, None),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1260
    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
  1261
}
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1262
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1263
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1264
def _tokenize(text):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1265
    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
  1266
    special = b'():+-& '
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1267
    pos = 0
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1268
    length = len(text)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1269
    while pos < length:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1270
        symbol = b''.join(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1271
            itertools.takewhile(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1272
                lambda ch: ch not in special, pycompat.iterbytestr(view[pos:])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1273
            )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1274
        )
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1275
        if symbol:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1276
            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
  1277
            pos += len(symbol)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1278
        else:  # special char, ignore space
43282
47946f08e463 py3: don't index into bytes in phabricator's _tokenize()
Ian Moody <moz-ian@perix.co.uk>
parents: 43264
diff changeset
  1279
            if text[pos : pos + 1] != b' ':
47946f08e463 py3: don't index into bytes in phabricator's _tokenize()
Ian Moody <moz-ian@perix.co.uk>
parents: 43264
diff changeset
  1280
                yield (text[pos : pos + 1], None, pos)
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1281
            pos += 1
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1282
    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
  1283
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1284
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1285
def _parse(text):
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1286
    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
  1287
    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
  1288
        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
  1289
    return tree
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1290
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1291
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1292
def _parsedrev(symbol):
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1293
    """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
  1294
    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
  1295
        return int(symbol[1:])
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1296
    if symbol.isdigit():
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1297
        return int(symbol)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1298
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1299
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1300
def _prefetchdrevs(tree):
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1301
    """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
  1302
    drevs = set()
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1303
    ancestordrevs = set()
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1304
    op = tree[0]
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1305
    if op == b'symbol':
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1306
        r = _parsedrev(tree[1])
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1307
        if r:
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1308
            drevs.add(r)
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1309
    elif op == b'ancestors':
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1310
        r, a = _prefetchdrevs(tree[1])
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1311
        drevs.update(r)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1312
        ancestordrevs.update(r)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1313
        ancestordrevs.update(a)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1314
    else:
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1315
        for t in tree[1:]:
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1316
            r, a = _prefetchdrevs(t)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1317
            drevs.update(r)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1318
            ancestordrevs.update(a)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1319
    return drevs, ancestordrevs
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1320
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1321
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1322
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
  1323
    """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
  1324
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1325
    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
  1326
    for details.
33267
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1327
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1328
    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
  1329
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1330
        {
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1331
            "id": "2",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1332
            "phid": "PHID-DREV-672qvysjcczopag46qty",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1333
            "title": "example",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1334
            "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
  1335
            "dateCreated": "1499181406",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1336
            "dateModified": "1499182103",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1337
            "authorPHID": "PHID-USER-tv3ohwc4v4jeu34otlye",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1338
            "status": "0",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1339
            "statusName": "Needs Review",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1340
            "properties": [],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1341
            "branch": null,
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1342
            "summary": "",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1343
            "testPlan": "",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1344
            "lineCount": "2",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1345
            "activeDiffPHID": "PHID-DIFF-xoqnjkobbm6k4dk6hi72",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1346
            "diffs": [
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1347
              "3",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1348
              "4",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1349
            ],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1350
            "commits": [],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1351
            "reviewers": [],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1352
            "ccs": [],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1353
            "hashes": [],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1354
            "auxiliary": {
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1355
              "phabricator:projects": [],
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1356
              "phabricator:depends-on": [
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1357
                "PHID-DREV-gbapp366kutjebt7agcd"
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1358
              ]
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1359
            },
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1360
            "repositoryPHID": "PHID-REPO-hub2hx62ieuqeheznasv",
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1361
            "sourcePath": null
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1362
        }
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1363
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1364
33269
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
  1365
    def fetch(params):
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
  1366
        """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
  1367
        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
  1368
        if key in prefetched:
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
  1369
            return prefetched[key]
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
  1370
        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
  1371
        # Fill prefetched with the result
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
  1372
        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
  1373
            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
  1374
            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
  1375
        if key not in prefetched:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1376
            raise error.Abort(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1377
                _(b'cannot get Differential Revision %r') % params
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1378
            )
33269
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
  1379
        return prefetched[key]
ead6749354e1 phabricator: try to fetch differential revisions in batch
Jun Wu <quark@fb.com>
parents: 33268
diff changeset
  1380
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1381
    def getstack(topdrevids):
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1382
        """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
  1383
        visited = set()
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1384
        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
  1385
        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
  1386
        while queue:
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1387
            params = queue.pop()
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1388
            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
  1389
            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
  1390
                continue
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1391
            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
  1392
            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
  1393
            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
  1394
            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
  1395
            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
  1396
                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
  1397
        result.reverse()
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1398
        return smartset.baseset(result)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1399
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1400
    # Initialize prefetch cache
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1401
    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
  1402
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1403
    tree = _parse(spec)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1404
    drevs, ancestordrevs = _prefetchdrevs(tree)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1405
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1406
    # 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
  1407
    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
  1408
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1409
    # Prefetch Differential Revisions in batch
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1410
    tofetch = set(drevs)
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1411
    for r in ancestordrevs:
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1412
        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
  1413
    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
  1414
        fetch({b'ids': list(tofetch)})
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1415
    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
  1416
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1417
    # 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
  1418
    def walk(tree):
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1419
        op = tree[0]
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1420
        if op == b'symbol':
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1421
            drev = _parsedrev(tree[1])
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1422
            if drev:
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1423
                return smartset.baseset([drev])
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1424
            elif tree[1] in _knownstatusnames:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1425
                drevs = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1426
                    r
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1427
                    for r in validids
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1428
                    if _getstatusname(prefetched[r]) == tree[1]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1429
                ]
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1430
                return smartset.baseset(drevs)
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1431
            else:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1432
                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
  1433
        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
  1434
            assert len(tree) == 3
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1435
            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
  1436
        elif op == b'group':
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1437
            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
  1438
        elif op == b'ancestors':
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1439
            return getstack(walk(tree[1]))
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1440
        else:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1441
            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
  1442
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1443
    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
  1444
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1445
33268
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1446
def getdescfromdrev(drev):
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1447
    """get description (commit message) from "Differential Revision"
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1448
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1449
    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
  1450
    about limited fields: title, summary, test plan, and URL.
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1451
    """
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1452
    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
  1453
    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
  1454
    testplan = drev[b'testPlan'].rstrip()
33268
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1455
    if testplan:
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1456
        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
  1457
    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
  1458
    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
  1459
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1460
33441
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1461
def getdiffmeta(diff):
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1462
    """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
  1463
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1464
    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
  1465
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1466
        "properties": {
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1467
          "hg:meta": {
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1468
            "date": "1499571514 25200",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1469
            "node": "98c08acae292b2faf60a279b4189beb6cff1414d",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1470
            "user": "Foo Bar <foo@example.com>",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1471
            "parent": "6d0abad76b30e4724a37ab8721d630394070fe16"
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1472
          }
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1473
        }
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1474
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1475
    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
  1476
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1477
        "properties": {
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1478
          "local:commits": {
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1479
            "98c08acae292b2faf60a279b4189beb6cff1414d": {
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1480
              "author": "Foo Bar",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1481
              "time": 1499546314,
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1482
              "branch": "default",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1483
              "tag": "",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1484
              "commit": "98c08acae292b2faf60a279b4189beb6cff1414d",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1485
              "rev": "98c08acae292b2faf60a279b4189beb6cff1414d",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1486
              "local": "1000",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1487
              "parents": ["6d0abad76b30e4724a37ab8721d630394070fe16"],
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1488
              "summary": "...",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1489
              "message": "...",
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1490
              "authorEmail": "foo@example.com"
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1491
            }
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1492
          }
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1493
        }
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1494
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1495
    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
  1496
    information.
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1497
    """
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1498
    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
  1499
    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
  1500
    if not meta:
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1501
        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
  1502
            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
  1503
            meta = {}
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1504
            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
  1505
                meta[b'user'] = b'%s <%s>' % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1506
                    commit[b'author'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1507
                    commit[b'authorEmail'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1508
                )
42285
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1509
            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
  1510
                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
  1511
            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
  1512
                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
  1513
            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
  1514
            if node:
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1515
                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
  1516
            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
  1517
                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
  1518
        else:
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1519
            meta = {}
a4f7dceb07bf phabricator: fallback to reading metadata from diff for phabread
Ian Moody <moz-ian@perix.co.uk>
parents: 42284
diff changeset
  1520
    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
  1521
        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
  1522
    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
  1523
        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
  1524
    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
  1525
        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
  1526
    return meta
33441
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1527
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1528
33852
75fdaf851e83 phabricator: change "readpatch" to be more flexible
Jun Wu <quark@fb.com>
parents: 33808
diff changeset
  1529
def readpatch(repo, drevs, write):
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1530
    """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
  1531
33852
75fdaf851e83 phabricator: change "readpatch" to be more flexible
Jun Wu <quark@fb.com>
parents: 33808
diff changeset
  1532
    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
  1533
    "differential.query".
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1534
    """
33267
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1535
    # 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
  1536
    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
  1537
    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
  1538
33267
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1539
    # 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
  1540
    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
  1541
        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
  1542
41902
c340a8ac7ef3 phabricator: convert conduit response JSON unicode to bytes inside callconduit
Ian Moody <moz-ian@perix.co.uk>
parents: 41716
diff changeset
  1543
        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
  1544
        body = callconduit(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1545
            repo.ui, b'differential.getrawdiff', {b'diffID': diffid}
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1546
        )
33268
85391b95961d phabricator: avoid calling differential.getcommitmessage
Jun Wu <quark@fb.com>
parents: 33267
diff changeset
  1547
        desc = getdescfromdrev(drev)
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1548
        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
  1549
dba9f88659a3 phabricator: rework phabread to reduce memory usage and round-trips
Jun Wu <quark@fb.com>
parents: 33266
diff changeset
  1550
        # 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
  1551
        # 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
  1552
        # 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
  1553
        meta = getdiffmeta(diffs[b'%d' % diffid])
33441
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1554
        for k in _metanamemap.keys():
de7c6ec27d99 phabricator: respect metadata sent by arc
Jun Wu <quark@fb.com>
parents: 33271
diff changeset
  1555
            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
  1556
                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
  1557
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1558
        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
  1559
        write(content)
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1560
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1561
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1562
@vcrcommand(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1563
    b'phabread',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1564
    [(b'', b'stack', False, _(b'read dependencies'))],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1565
    _(b'DREVSPEC [OPTIONS]'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1566
    helpcategory=command.CATEGORY_IMPORT_EXPORT,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1567
)
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1568
def phabread(ui, repo, spec, **opts):
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1569
    """print patches from Phabricator suitable for importing
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1570
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1571
    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
  1572
    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
  1573
    ``&``, ``(``, ``)`` 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
  1574
    select a stack.
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1575
33854
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1576
    ``abandoned``, ``accepted``, ``closed``, ``needsreview``, ``needsrevision``
fb59192b4981 phabricator: add status to revision query language
Jun Wu <quark@fb.com>
parents: 33853
diff changeset
  1577
    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
  1578
    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
  1579
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1580
    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
  1581
    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
  1582
    stack up to D9.
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1583
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1584
    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
  1585
    It is equivalent to the ``:`` operator.
33202
04cf9927f350 phabricator: add phabread command to read patches
Jun Wu <quark@fb.com>
parents: 33201
diff changeset
  1586
    """
41975
51ba9fbcca52 py3: use pycompat.byteskwargs on opts in phabricator.py
Ian Moody <moz-ian@perix.co.uk>
parents: 41955
diff changeset
  1587
    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
  1588
    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
  1589
        spec = b':(%s)' % spec
33853
539541779010 phabricator: add a small language to query Differential Revisions
Jun Wu <quark@fb.com>
parents: 33852
diff changeset
  1590
    drevs = querydrev(repo, spec)
33852
75fdaf851e83 phabricator: change "readpatch" to be more flexible
Jun Wu <quark@fb.com>
parents: 33808
diff changeset
  1591
    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
  1592
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1593
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1594
@vcrcommand(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1595
    b'phabupdate',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1596
    [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1597
        (b'', b'accept', False, _(b'accept revisions')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1598
        (b'', b'reject', False, _(b'reject revisions')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1599
        (b'', b'abandon', False, _(b'abandon revisions')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1600
        (b'', b'reclaim', False, _(b'reclaim revisions')),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1601
        (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
  1602
    ],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1603
    _(b'DREVSPEC [OPTIONS]'),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1604
    helpcategory=command.CATEGORY_IMPORT_EXPORT,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1605
)
33855
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1606
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
  1607
    """update Differential Revision in batch
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1608
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1609
    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
  1610
    """
41975
51ba9fbcca52 py3: use pycompat.byteskwargs on opts in phabricator.py
Ian Moody <moz-ian@perix.co.uk>
parents: 41955
diff changeset
  1611
    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
  1612
    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
  1613
    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
  1614
        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
  1615
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1616
    actions = []
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1617
    for f in flags:
43350
132ddd1cba85 phabricator: use True primitive instead of b'true' for phabupdate actions
Ian Moody <moz-ian@perix.co.uk>
parents: 43321
diff changeset
  1618
        actions.append({b'type': f, b'value': True})
33855
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1619
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1620
    drevs = querydrev(repo, spec)
6e666cd59879 phabricator: add phabupdate command to update status in batch
Jun Wu <quark@fb.com>
parents: 33854
diff changeset
  1621
    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
  1622
        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
  1623
            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
  1624
        if actions:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1625
            params = {
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1626
                b'objectIdentifier': drev[b'phid'],
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1627
                b'transactions': actions,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1628
            }
42450
f163e2b2594c phabricator: pass ui instead of repo to callconduit
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 42449
diff changeset
  1629
            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
  1630
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1631
42975
24bf7a3d3c30 phabricator: use exthelper to register commands, config, and templates
Matt Harbison <matt_harbison@yahoo.com>
parents: 42955
diff changeset
  1632
@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
  1633
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
  1634
    """: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
  1635
    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
  1636
    """
38398
81a4be7099fa py3: byte-stringify literals in contrib/phabricator.py as example
Yuya Nishihara <yuya@tcha.org>
parents: 38342
diff changeset
  1637
    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
  1638
    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
  1639
    if m:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1640
        return templateutil.hybriddict(
43554
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43552
diff changeset
  1641
            {b'url': m.group('url'), b'id': b"D%s" % m.group('id'),}
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1642
        )
41164
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1643
    else:
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1644
        tags = ctx.repo().nodetags(ctx.node())
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1645
        for t in tags:
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1646
            if _differentialrevisiontagre.match(t):
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1647
                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
  1648
                if not url.endswith(b'/'):
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1649
                    url += b'/'
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1650
                url += t
43fd1947301d phabricator: teach {phabreview} to work without --amend
Matt Harbison <matt_harbison@yahoo.com>
parents: 41163
diff changeset
  1651
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 43047
diff changeset
  1652
                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
  1653
    return None