hgext/gpg.py
author Manuel Jacob <me@manueljacob.de>
Thu, 25 Jun 2020 03:10:13 +0200
branchstable
changeset 45003 839328c5a728
parent 43506 9f70512ae2cf
child 45942 89a2afe31e82
permissions -rw-r--r--
hgweb: deduplicate code A following patch will change the way keys and values are encoded. To reduce the diff, I’ve split off the uninteresting part.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
     1
# Copyright 2005, 2006 Benoit Boissinot <benoit.boissinot@ens-lyon.org>
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
     2
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8210
diff changeset
     3
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9183
diff changeset
     4
# GNU General Public License version 2 or any later version.
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
     5
8934
9dda4c73fc3b extensions: change descriptions for extensions providing a few commands
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8894
diff changeset
     6
'''commands to sign and verify changesets'''
8873
e872ef2e6758 help: add/fix docstrings for a bunch of extensions
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 8808
diff changeset
     7
29124
555ab813c237 py3: make hgext/gpg.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27814
diff changeset
     8
from __future__ import absolute_import
555ab813c237 py3: make hgext/gpg.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27814
diff changeset
     9
555ab813c237 py3: make hgext/gpg.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27814
diff changeset
    10
import binascii
555ab813c237 py3: make hgext/gpg.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27814
diff changeset
    11
import os
29205
a0939666b836 py3: move up symbol imports to enforce import-checker rules
Yuya Nishihara <yuya@tcha.org>
parents: 29124
diff changeset
    12
a0939666b836 py3: move up symbol imports to enforce import-checker rules
Yuya Nishihara <yuya@tcha.org>
parents: 29124
diff changeset
    13
from mercurial.i18n import _
29124
555ab813c237 py3: make hgext/gpg.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27814
diff changeset
    14
from mercurial import (
555ab813c237 py3: make hgext/gpg.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27814
diff changeset
    15
    cmdutil,
555ab813c237 py3: make hgext/gpg.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27814
diff changeset
    16
    error,
40293
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 38164
diff changeset
    17
    help,
29124
555ab813c237 py3: make hgext/gpg.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27814
diff changeset
    18
    match,
555ab813c237 py3: make hgext/gpg.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27814
diff changeset
    19
    node as hgnode,
30925
82f1ef8b4477 py3: convert the mode argument of os.fdopen to unicodes (2 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents: 29841
diff changeset
    20
    pycompat,
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 31414
diff changeset
    21
    registrar,
29124
555ab813c237 py3: make hgext/gpg.py use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27814
diff changeset
    22
)
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36835
diff changeset
    23
from mercurial.utils import (
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36835
diff changeset
    24
    dateutil,
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36835
diff changeset
    25
    procutil,
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36835
diff changeset
    26
)
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    27
14299
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
    28
cmdtable = {}
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 31414
diff changeset
    29
command = registrar.command(cmdtable)
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 29239
diff changeset
    30
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 25149
diff changeset
    31
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 25149
diff changeset
    32
# be specifying the version(s) of Mercurial they are tested with, or
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 25149
diff changeset
    33
# leave the attribute unspecified.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    34
testedwith = b'ships-with-hg-core'
14299
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
    35
34501
b0c42fec8dc2 configitems: register the 'gpg.cmd' config
Boris Feld <boris.feld@octobus.net>
parents: 32375
diff changeset
    36
configtable = {}
b0c42fec8dc2 configitems: register the 'gpg.cmd' config
Boris Feld <boris.feld@octobus.net>
parents: 32375
diff changeset
    37
configitem = registrar.configitem(configtable)
b0c42fec8dc2 configitems: register the 'gpg.cmd' config
Boris Feld <boris.feld@octobus.net>
parents: 32375
diff changeset
    38
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
    39
configitem(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    40
    b'gpg', b'cmd', default=b'gpg',
34501
b0c42fec8dc2 configitems: register the 'gpg.cmd' config
Boris Feld <boris.feld@octobus.net>
parents: 32375
diff changeset
    41
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
    42
configitem(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    43
    b'gpg', b'key', default=None,
34502
d54526c257af configitems: register the 'gpg.key' config
Boris Feld <boris.feld@octobus.net>
parents: 34501
diff changeset
    44
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
    45
configitem(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    46
    b'gpg', b'.*', default=None, generic=True,
34770
bb05720913d3 configitems: register the 'gpg' arbitraty key section
Boris Feld <boris.feld@octobus.net>
parents: 34502
diff changeset
    47
)
34501
b0c42fec8dc2 configitems: register the 'gpg.cmd' config
Boris Feld <boris.feld@octobus.net>
parents: 32375
diff changeset
    48
40293
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 38164
diff changeset
    49
# Custom help category
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    50
_HELP_CATEGORY = b'gpg'
42235
ade02721d3fa help: register the 'gpg' command category and give it a description
Sietse Brouwer <sbbrouwer@gmail.com>
parents: 41676
diff changeset
    51
help.CATEGORY_ORDER.insert(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
    52
    help.CATEGORY_ORDER.index(registrar.command.CATEGORY_HELP), _HELP_CATEGORY
42235
ade02721d3fa help: register the 'gpg' command category and give it a description
Sietse Brouwer <sbbrouwer@gmail.com>
parents: 41676
diff changeset
    53
)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    54
help.CATEGORY_NAMES[_HELP_CATEGORY] = b'Signing changes (GPG)'
40293
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 38164
diff changeset
    55
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
    56
8778
c5f36402daad use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8706
diff changeset
    57
class gpg(object):
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    58
    def __init__(self, path, key=None):
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    59
        self.path = path
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    60
        self.key = (key and b" --local-user \"%s\"" % key) or b""
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    61
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    62
    def sign(self, data):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    63
        gpgcmd = b"%s --sign --detach-sign%s" % (self.path, self.key)
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36835
diff changeset
    64
        return procutil.filter(data, gpgcmd)
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    65
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    66
    def verify(self, data, sig):
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    67
        """ returns of the good and bad signatures"""
2231
9a2f4b2e7cf1 gpg extension: Always remove temporary files created by 'hg sigcheck'.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2165
diff changeset
    68
        sigfile = datafile = None
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    69
        try:
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    70
            # create temporary files
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    71
            fd, sigfile = pycompat.mkstemp(prefix=b"hg-gpg-", suffix=b".sig")
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43346
diff changeset
    72
            fp = os.fdopen(fd, 'wb')
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    73
            fp.write(sig)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    74
            fp.close()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    75
            fd, datafile = pycompat.mkstemp(prefix=b"hg-gpg-", suffix=b".txt")
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43346
diff changeset
    76
            fp = os.fdopen(fd, 'wb')
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    77
            fp.write(data)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    78
            fp.close()
43346
6ada8a274b9c formatting: run black version 19.10b0 on the codebase
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43117
diff changeset
    79
            gpgcmd = (
6ada8a274b9c formatting: run black version 19.10b0 on the codebase
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43117
diff changeset
    80
                b"%s --logger-fd 1 --status-fd 1 --verify \"%s\" \"%s\""
6ada8a274b9c formatting: run black version 19.10b0 on the codebase
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43117
diff changeset
    81
                % (self.path, sigfile, datafile,)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
    82
            )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    83
            ret = procutil.filter(b"", gpgcmd)
2231
9a2f4b2e7cf1 gpg extension: Always remove temporary files created by 'hg sigcheck'.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2165
diff changeset
    84
        finally:
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    85
            for f in (sigfile, datafile):
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    86
                try:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    87
                    if f:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    88
                        os.unlink(f)
16688
cfb6682961b8 cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 14299
diff changeset
    89
                except OSError:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    90
                    pass
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    91
        keys = []
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    92
        key, fingerprint = None, None
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    93
        for l in ret.splitlines():
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    94
            # see DETAILS in the gnupg documentation
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    95
            # filter the logger output
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    96
            if not l.startswith(b"[GNUPG:]"):
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    97
                continue
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    98
            l = l[9:]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    99
            if l.startswith(b"VALIDSIG"):
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   100
                # fingerprint of the primary key
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   101
                fingerprint = l.split()[10]
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   102
            elif l.startswith(b"ERRSIG"):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   103
                key = l.split(b" ", 3)[:2]
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   104
                key.append(b"")
19441
8b312c080adb gpg: treat "ERRSIG" as a valid key id but no fingerprint
Wei, Elson <elson.wei@gmail.com>
parents: 16991
diff changeset
   105
                fingerprint = None
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   106
            elif (
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   107
                l.startswith(b"GOODSIG")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   108
                or l.startswith(b"EXPSIG")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   109
                or l.startswith(b"EXPKEYSIG")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   110
                or l.startswith(b"BADSIG")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   111
            ):
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   112
                if key is not None:
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   113
                    keys.append(key + [fingerprint])
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   114
                key = l.split(b" ", 2)
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   115
                fingerprint = None
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   116
        if key is not None:
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   117
            keys.append(key + [fingerprint])
19442
33c72f054e16 gpg: getkeys() removes unused returning value "err"
Wei, Elson <elson.wei@gmail.com>
parents: 19441
diff changeset
   118
        return keys
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   119
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   120
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   121
def newgpg(ui, **opts):
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   122
    """create a new gpg instance"""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   123
    gpgpath = ui.config(b"gpg", b"cmd")
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43346
diff changeset
   124
    gpgkey = opts.get('key')
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   125
    if not gpgkey:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   126
        gpgkey = ui.config(b"gpg", b"key")
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   127
    return gpg(gpgpath, gpgkey)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   128
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   129
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   130
def sigwalk(repo):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   131
    """
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   132
    walk over every sigs, yields a couple
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   133
    ((node, version, sig), (filename, linenumber))
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   134
    """
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   135
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   136
    def parsefile(fileiter, context):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   137
        ln = 1
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   138
        for l in fileiter:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   139
            if not l:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   140
                continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   141
            yield (l.split(b" ", 2), (context, ln))
10394
4612cded5176 fix coding style (reported by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   142
            ln += 1
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   143
8210
344751cd8cb8 replace various uses of list.reverse()
Matt Mackall <mpm@selenic.com>
parents: 6760
diff changeset
   144
    # read the heads
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   145
    fl = repo.file(b".hgsigs")
8210
344751cd8cb8 replace various uses of list.reverse()
Matt Mackall <mpm@selenic.com>
parents: 6760
diff changeset
   146
    for r in reversed(fl.heads()):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   147
        fn = b".hgsigs|%s" % hgnode.short(r)
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   148
        for item in parsefile(fl.read(r).splitlines(), fn):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   149
            yield item
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   150
    try:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   151
        # read local signatures
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   152
        fn = b"localsigs"
23877
7cc77030c557 localrepo: remove all external users of localrepo.opener
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 22683
diff changeset
   153
        for item in parsefile(repo.vfs(fn), fn):
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   154
            yield item
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   155
    except IOError:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   156
        pass
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   157
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   158
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   159
def getkeys(ui, repo, mygpg, sigdata, context):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   160
    """get the keys who signed a data"""
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   161
    fn, ln = context
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   162
    node, version, sig = sigdata
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   163
    prefix = b"%s:%d" % (fn, ln)
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   164
    node = hgnode.bin(node)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   165
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   166
    data = node2txt(repo, node, version)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   167
    sig = binascii.a2b_base64(sig)
19442
33c72f054e16 gpg: getkeys() removes unused returning value "err"
Wei, Elson <elson.wei@gmail.com>
parents: 19441
diff changeset
   168
    keys = mygpg.verify(data, sig)
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   169
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   170
    validkeys = []
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   171
    # warn for expired key and/or sigs
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   172
    for key in keys:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   173
        if key[0] == b"ERRSIG":
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   174
            ui.write(_(b"%s Unknown key ID \"%s\"\n") % (prefix, key[1]))
19444
f9e04a4e28d0 gpg: show "Unknown key ID xxxxxxxx" when the status is ERRSIG
Wei, Elson <elson.wei@gmail.com>
parents: 19443
diff changeset
   175
            continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   176
        if key[0] == b"BADSIG":
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   177
            ui.write(_(b"%s Bad signature from \"%s\"\n") % (prefix, key[2]))
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   178
            continue
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   179
        if key[0] == b"EXPSIG":
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   180
            ui.write(
43117
8ff1ecfadcd1 cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents: 43077
diff changeset
   181
                _(b"%s Note: Signature has expired (signed by: \"%s\")\n")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   182
                % (prefix, key[2])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   183
            )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   184
        elif key[0] == b"EXPKEYSIG":
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   185
            ui.write(
43117
8ff1ecfadcd1 cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents: 43077
diff changeset
   186
                _(b"%s Note: This key has expired (signed by: \"%s\")\n")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   187
                % (prefix, key[2])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   188
            )
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   189
        validkeys.append((key[1], key[2], key[3]))
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   190
    return validkeys
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   191
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   192
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   193
@command(b"sigs", [], _(b'hg sigs'), helpcategory=_HELP_CATEGORY)
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   194
def sigs(ui, repo):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   195
    """list signed changesets"""
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   196
    mygpg = newgpg(ui)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   197
    revs = {}
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   198
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   199
    for data, context in sigwalk(repo):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   200
        node, version, sig = data
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   201
        fn, ln = context
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   202
        try:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   203
            n = repo.lookup(node)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   204
        except KeyError:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   205
            ui.warn(_(b"%s:%d node does not exist\n") % (fn, ln))
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   206
            continue
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   207
        r = repo.changelog.rev(n)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   208
        keys = getkeys(ui, repo, mygpg, data, context)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   209
        if not keys:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   210
            continue
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   211
        revs.setdefault(r, [])
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   212
        revs[r].extend(keys)
8303
db52cc4f2f97 gpg: use reverse kwarg to sort sigs in reversed order
Martin Geisler <mg@lazybytes.net>
parents: 8301
diff changeset
   213
    for rev in sorted(revs, reverse=True):
1682
ca1cda9220d5 fix an exception in gpg.py with multiples sigs for the same cset
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1681
diff changeset
   214
        for k in revs[rev]:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   215
            r = b"%5d:%s" % (rev, hgnode.hex(repo.changelog.node(rev)))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   216
            ui.write(b"%-30s %s\n" % (keystr(ui, k), r))
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   217
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   218
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   219
@command(b"sigcheck", [], _(b'hg sigcheck REV'), helpcategory=_HELP_CATEGORY)
27117
ad2627f4af27 gpg: rename sigcheck function
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   220
def sigcheck(ui, repo, rev):
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   221
    """verify all the signatures there may be for a particular revision"""
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   222
    mygpg = newgpg(ui)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   223
    rev = repo.lookup(rev)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   224
    hexrev = hgnode.hex(rev)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   225
    keys = []
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   226
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   227
    for data, context in sigwalk(repo):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   228
        node, version, sig = data
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   229
        if node == hexrev:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   230
            k = getkeys(ui, repo, mygpg, data, context)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   231
            if k:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   232
                keys.extend(k)
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   233
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   234
    if not keys:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   235
        ui.write(_(b"no valid signature for %s\n") % hgnode.short(rev))
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   236
        return
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   237
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   238
    # print summary
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   239
    ui.write(_(b"%s is signed by:\n") % hgnode.short(rev))
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   240
    for key in keys:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   241
        ui.write(b" %s\n" % keystr(ui, key))
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   242
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   243
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   244
def keystr(ui, key):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   245
    """associate a string to a key (username, comment)"""
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   246
    keyid, user, fingerprint = key
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   247
    comment = ui.config(b"gpg", fingerprint)
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   248
    if comment:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   249
        return b"%s (%s)" % (user, comment)
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   250
    else:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   251
        return user
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   252
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   253
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   254
@command(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   255
    b"sign",
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   256
    [
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   257
        (b'l', b'local', None, _(b'make the signature local')),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   258
        (b'f', b'force', None, _(b'sign even if the sigfile is modified')),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   259
        (
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   260
            b'',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   261
            b'no-commit',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   262
            None,
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   263
            _(b'do not commit the sigfile after signing'),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   264
        ),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   265
        (b'k', b'key', b'', _(b'the key id to sign with'), _(b'ID')),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   266
        (b'm', b'message', b'', _(b'use text as commit message'), _(b'TEXT')),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   267
        (b'e', b'edit', False, _(b'invoke editor on commit messages')),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   268
    ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   269
    + cmdutil.commitopts2,
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   270
    _(b'hg sign [OPTION]... [REV]...'),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   271
    helpcategory=_HELP_CATEGORY,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   272
)
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   273
def sign(ui, repo, *revs, **opts):
3916
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   274
    """add a signature for the current or given revision
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   275
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   276
    If no revision is given, the parent of the working directory is used,
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   277
    or tip if no revision is checked out.
6163
1f733c2f0165 Document log date ranges and mention 'hg help dates' for all commands (issue998)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 6139
diff changeset
   278
25791
917be0574d7f gpg: mention undocumented options
Matt Mackall <mpm@selenic.com>
parents: 25660
diff changeset
   279
    The ``gpg.cmd`` config setting can be used to specify the command
917be0574d7f gpg: mention undocumented options
Matt Mackall <mpm@selenic.com>
parents: 25660
diff changeset
   280
    to run. A default key can be specified with ``gpg.key``.
917be0574d7f gpg: mention undocumented options
Matt Mackall <mpm@selenic.com>
parents: 25660
diff changeset
   281
11193
687c7d395f20 Use our custom hg reStructuredText role some more
Martin Geisler <mg@aragost.com>
parents: 10532
diff changeset
   282
    See :hg:`help dates` for a list of formats valid for -d/--date.
3916
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   283
    """
27814
a72735028336 with: use context manager for wlock in sign
Bryan O'Sullivan <bryano@fb.com>
parents: 27196
diff changeset
   284
    with repo.wlock():
27196
7b4a61570d61 gpg: make sign acquire wlock before processing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27117
diff changeset
   285
        return _dosign(ui, repo, *revs, **opts)
3916
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   286
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   287
27196
7b4a61570d61 gpg: make sign acquire wlock before processing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27117
diff changeset
   288
def _dosign(ui, repo, *revs, **opts):
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   289
    mygpg = newgpg(ui, **opts)
34978
de1f045781e0 py3: handle keyword arguments in hgext/gpg.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34770
diff changeset
   290
    opts = pycompat.byteskwargs(opts)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   291
    sigver = b"0"
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   292
    sigmessage = b""
6139
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5475
diff changeset
   293
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   294
    date = opts.get(b'date')
6139
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5475
diff changeset
   295
    if date:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   296
        opts[b'date'] = dateutil.parsedate(date)
6139
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5475
diff changeset
   297
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   298
    if revs:
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   299
        nodes = [repo.lookup(n) for n in revs]
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   300
    else:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   301
        nodes = [
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   302
            node for node in repo.dirstate.parents() if node != hgnode.nullid
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   303
        ]
3916
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   304
        if len(nodes) > 1:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   305
            raise error.Abort(
43117
8ff1ecfadcd1 cleanup: join string literals that are already on one line
Martin von Zweigbergk <martinvonz@google.com>
parents: 43077
diff changeset
   306
                _(b'uncommitted merge - please provide a specific revision')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   307
            )
3916
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   308
        if not nodes:
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   309
            nodes = [repo.changelog.tip()]
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   310
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   311
    for n in nodes:
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   312
        hexnode = hgnode.hex(n)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   313
        ui.write(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   314
            _(b"signing %d:%s\n") % (repo.changelog.rev(n), hgnode.short(n))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   315
        )
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   316
        # build data
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   317
        data = node2txt(repo, n, sigver)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   318
        sig = mygpg.sign(data)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   319
        if not sig:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   320
            raise error.Abort(_(b"error while signing"))
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   321
        sig = binascii.b2a_base64(sig)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   322
        sig = sig.replace(b"\n", b"")
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   323
        sigmessage += b"%s %s %s\n" % (hexnode, sigver, sig)
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   324
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   325
    # write it
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   326
    if opts[b'local']:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   327
        repo.vfs.append(b"localsigs", sigmessage)
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   328
        return
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   329
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   330
    if not opts[b"force"]:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   331
        msigs = match.exact([b'.hgsigs'])
25149
3f0744eeaeaf cleanup: use __builtins__.any instead of util.any
Augie Fackler <augie@google.com>
parents: 23877
diff changeset
   332
        if any(repo.status(match=msigs, unknown=True, ignored=True)):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   333
            raise error.Abort(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   334
                _(b"working copy of .hgsigs is changed "),
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   335
                hint=_(b"please commit .hgsigs manually"),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   336
            )
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   337
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   338
    sigsfile = repo.wvfs(b".hgsigs", b"ab")
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 12965
diff changeset
   339
    sigsfile.write(sigmessage)
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 12965
diff changeset
   340
    sigsfile.close()
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   341
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   342
    if b'.hgsigs' not in repo.dirstate:
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   343
        repo[None].add([b".hgsigs"])
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   344
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   345
    if opts[b"no_commit"]:
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   346
        return
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   347
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   348
    message = opts[b'message']
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   349
    if not message:
9183
d0225fa2f6c4 do not translate commit messages
Martin Geisler <mg@lazybytes.net>
parents: 8934
diff changeset
   350
        # we don't translate commit messages
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   351
        message = b"\n".join(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   352
            [
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   353
                b"Added signature for changeset %s" % hgnode.short(n)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   354
                for n in nodes
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   355
            ]
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   356
        )
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   357
    try:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   358
        editor = cmdutil.getcommiteditor(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   359
            editform=b'gpg.sign', **pycompat.strkwargs(opts)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   360
        )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   361
        repo.commit(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   362
            message, opts[b'user'], opts[b'date'], match=msigs, editor=editor
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   363
        )
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25186
diff changeset
   364
    except ValueError as inst:
36653
2f7a3c90c0d7 py3: use pycompat.bytestr() to convert error messages to bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36607
diff changeset
   365
        raise error.Abort(pycompat.bytestr(inst))
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   366
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   367
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   368
def node2txt(repo, node, ver):
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   369
    """map a manifest into some text"""
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   370
    if ver == b"0":
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   371
        return b"%s\n" % hgnode.hex(node)
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   372
    else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   373
        raise error.Abort(_(b"unknown signature version"))
40293
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 38164
diff changeset
   374
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   375
40293
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 38164
diff changeset
   376
def extsetup(ui):
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 38164
diff changeset
   377
    # Add our category before "Repository maintenance".
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 38164
diff changeset
   378
    help.CATEGORY_ORDER.insert(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   379
        help.CATEGORY_ORDER.index(command.CATEGORY_MAINTENANCE), _HELP_CATEGORY
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42235
diff changeset
   380
    )
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   381
    help.CATEGORY_NAMES[_HELP_CATEGORY] = b'GPG signing'