hgext/gpg.py
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Wed, 02 Dec 2015 03:12:08 +0900
changeset 27196 7b4a61570d61
parent 27117 ad2627f4af27
child 27814 a72735028336
permissions -rw-r--r--
gpg: make sign acquire wlock before processing Before this patch, "hg sign" of gpg extension executes/evaluates below without acquisition of wlock. - repo.dirstate.parents() - '.hgsigs' not in repo.dirstate It may cause unintentional result, if another command runs parallelly (see also issue4368). To avoid this issue, this patch makes "hg sign" of gpg extension acquire wlock before processing.
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
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
     8
import os, tempfile, binascii
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25791
diff changeset
     9
from mercurial import util, commands, match, cmdutil, error
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    10
from mercurial import node as hgnode
3891
6b4127c7d52a Simplify i18n imports
Matt Mackall <mpm@selenic.com>
parents: 2875
diff changeset
    11
from mercurial.i18n import _
27196
7b4a61570d61 gpg: make sign acquire wlock before processing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27117
diff changeset
    12
from mercurial import lock as lockmod
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    13
14299
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
    14
cmdtable = {}
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
    15
command = cmdutil.command(cmdtable)
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 25149
diff changeset
    16
# Note for extension authors: ONLY specify testedwith = 'internal' for
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 25149
diff changeset
    17
# 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
    18
# 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
    19
# leave the attribute unspecified.
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16688
diff changeset
    20
testedwith = 'internal'
14299
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
    21
8778
c5f36402daad use new style classes
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 8706
diff changeset
    22
class gpg(object):
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    23
    def __init__(self, path, key=None):
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    24
        self.path = path
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    25
        self.key = (key and " --local-user \"%s\"" % key) or ""
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    26
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    27
    def sign(self, data):
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    28
        gpgcmd = "%s --sign --detach-sign%s" % (self.path, self.key)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    29
        return util.filter(data, gpgcmd)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    30
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    31
    def verify(self, data, sig):
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    32
        """ 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
    33
        sigfile = datafile = None
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    34
        try:
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    35
            # create temporary files
2165
d821918e3bee Use better names (hg-{usage}-{random}.{suffix}) for temporary files.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1685
diff changeset
    36
            fd, sigfile = tempfile.mkstemp(prefix="hg-gpg-", suffix=".sig")
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    37
            fp = os.fdopen(fd, 'wb')
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    38
            fp.write(sig)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    39
            fp.close()
2165
d821918e3bee Use better names (hg-{usage}-{random}.{suffix}) for temporary files.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 1685
diff changeset
    40
            fd, datafile = tempfile.mkstemp(prefix="hg-gpg-", suffix=".txt")
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    41
            fp = os.fdopen(fd, 'wb')
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    42
            fp.write(data)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    43
            fp.close()
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    44
            gpgcmd = ("%s --logger-fd 1 --status-fd 1 --verify "
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    45
                      "\"%s\" \"%s\"" % (self.path, sigfile, datafile))
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    46
            ret = util.filter("", gpgcmd)
2231
9a2f4b2e7cf1 gpg extension: Always remove temporary files created by 'hg sigcheck'.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2165
diff changeset
    47
        finally:
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    48
            for f in (sigfile, datafile):
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    49
                try:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    50
                    if f:
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    51
                        os.unlink(f)
16688
cfb6682961b8 cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 14299
diff changeset
    52
                except OSError:
10282
08a0f04b56bd many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents: 10263
diff changeset
    53
                    pass
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    54
        keys = []
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    55
        key, fingerprint = None, None
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    56
        for l in ret.splitlines():
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    57
            # see DETAILS in the gnupg documentation
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    58
            # filter the logger output
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    59
            if not l.startswith("[GNUPG:]"):
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    60
                continue
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    61
            l = l[9:]
19441
8b312c080adb gpg: treat "ERRSIG" as a valid key id but no fingerprint
Wei, Elson <elson.wei@gmail.com>
parents: 16991
diff changeset
    62
            if l.startswith("VALIDSIG"):
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    63
                # fingerprint of the primary key
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    64
                fingerprint = l.split()[10]
19441
8b312c080adb gpg: treat "ERRSIG" as a valid key id but no fingerprint
Wei, Elson <elson.wei@gmail.com>
parents: 16991
diff changeset
    65
            elif l.startswith("ERRSIG"):
8b312c080adb gpg: treat "ERRSIG" as a valid key id but no fingerprint
Wei, Elson <elson.wei@gmail.com>
parents: 16991
diff changeset
    66
                key = l.split(" ", 3)[:2]
8b312c080adb gpg: treat "ERRSIG" as a valid key id but no fingerprint
Wei, Elson <elson.wei@gmail.com>
parents: 16991
diff changeset
    67
                key.append("")
8b312c080adb gpg: treat "ERRSIG" as a valid key id but no fingerprint
Wei, Elson <elson.wei@gmail.com>
parents: 16991
diff changeset
    68
                fingerprint = None
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    69
            elif (l.startswith("GOODSIG") or
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    70
                  l.startswith("EXPSIG") or
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    71
                  l.startswith("EXPKEYSIG") or
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    72
                  l.startswith("BADSIG")):
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    73
                if key is not None:
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    74
                    keys.append(key + [fingerprint])
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    75
                key = l.split(" ", 2)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    76
                fingerprint = None
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    77
        if key is not None:
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    78
            keys.append(key + [fingerprint])
19442
33c72f054e16 gpg: getkeys() removes unused returning value "err"
Wei, Elson <elson.wei@gmail.com>
parents: 19441
diff changeset
    79
        return keys
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    80
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    81
def newgpg(ui, **opts):
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    82
    """create a new gpg instance"""
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    83
    gpgpath = ui.config("gpg", "cmd", "gpg")
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    84
    gpgkey = opts.get('key')
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    85
    if not gpgkey:
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    86
        gpgkey = ui.config("gpg", "key", None)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    87
    return gpg(gpgpath, gpgkey)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
    88
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    89
def sigwalk(repo):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    90
    """
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    91
    walk over every sigs, yields a couple
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    92
    ((node, version, sig), (filename, linenumber))
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    93
    """
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    94
    def parsefile(fileiter, context):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    95
        ln = 1
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    96
        for l in fileiter:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    97
            if not l:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    98
                continue
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
    99
            yield (l.split(" ", 2), (context, ln))
10394
4612cded5176 fix coding style (reported by pylint)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10282
diff changeset
   100
            ln += 1
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   101
8210
344751cd8cb8 replace various uses of list.reverse()
Matt Mackall <mpm@selenic.com>
parents: 6760
diff changeset
   102
    # read the heads
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   103
    fl = repo.file(".hgsigs")
8210
344751cd8cb8 replace various uses of list.reverse()
Matt Mackall <mpm@selenic.com>
parents: 6760
diff changeset
   104
    for r in reversed(fl.heads()):
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   105
        fn = ".hgsigs|%s" % hgnode.short(r)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   106
        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
   107
            yield item
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   108
    try:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   109
        # read local signatures
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   110
        fn = "localsigs"
23877
7cc77030c557 localrepo: remove all external users of localrepo.opener
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 22683
diff changeset
   111
        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
   112
            yield item
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   113
    except IOError:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   114
        pass
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   115
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   116
def getkeys(ui, repo, mygpg, sigdata, context):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   117
    """get the keys who signed a data"""
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   118
    fn, ln = context
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   119
    node, version, sig = sigdata
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   120
    prefix = "%s:%d" % (fn, ln)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   121
    node = hgnode.bin(node)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   122
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   123
    data = node2txt(repo, node, version)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   124
    sig = binascii.a2b_base64(sig)
19442
33c72f054e16 gpg: getkeys() removes unused returning value "err"
Wei, Elson <elson.wei@gmail.com>
parents: 19441
diff changeset
   125
    keys = mygpg.verify(data, sig)
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   126
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   127
    validkeys = []
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   128
    # warn for expired key and/or sigs
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   129
    for key in keys:
19444
f9e04a4e28d0 gpg: show "Unknown key ID xxxxxxxx" when the status is ERRSIG
Wei, Elson <elson.wei@gmail.com>
parents: 19443
diff changeset
   130
        if key[0] == "ERRSIG":
f9e04a4e28d0 gpg: show "Unknown key ID xxxxxxxx" when the status is ERRSIG
Wei, Elson <elson.wei@gmail.com>
parents: 19443
diff changeset
   131
            ui.write(_("%s Unknown key ID \"%s\"\n")
f9e04a4e28d0 gpg: show "Unknown key ID xxxxxxxx" when the status is ERRSIG
Wei, Elson <elson.wei@gmail.com>
parents: 19443
diff changeset
   132
                     % (prefix, shortkey(ui, key[1][:15])))
f9e04a4e28d0 gpg: show "Unknown key ID xxxxxxxx" when the status is ERRSIG
Wei, Elson <elson.wei@gmail.com>
parents: 19443
diff changeset
   133
            continue
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   134
        if key[0] == "BADSIG":
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   135
            ui.write(_("%s Bad signature from \"%s\"\n") % (prefix, key[2]))
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   136
            continue
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   137
        if key[0] == "EXPSIG":
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   138
            ui.write(_("%s Note: Signature has expired"
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   139
                       " (signed by: \"%s\")\n") % (prefix, key[2]))
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   140
        elif key[0] == "EXPKEYSIG":
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   141
            ui.write(_("%s Note: This key has expired"
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   142
                       " (signed by: \"%s\")\n") % (prefix, key[2]))
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   143
        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
   144
    return validkeys
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   145
14299
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
   146
@command("sigs", [], _('hg sigs'))
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   147
def sigs(ui, repo):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   148
    """list signed changesets"""
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   149
    mygpg = newgpg(ui)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   150
    revs = {}
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   151
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   152
    for data, context in sigwalk(repo):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   153
        node, version, sig = data
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   154
        fn, ln = context
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   155
        try:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   156
            n = repo.lookup(node)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   157
        except KeyError:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   158
            ui.warn(_("%s:%d node does not exist\n") % (fn, ln))
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   159
            continue
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   160
        r = repo.changelog.rev(n)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   161
        keys = getkeys(ui, repo, mygpg, data, context)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   162
        if not keys:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   163
            continue
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   164
        revs.setdefault(r, [])
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   165
        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
   166
    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
   167
        for k in revs[rev]:
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
   168
            r = "%5d:%s" % (rev, hgnode.hex(repo.changelog.node(rev)))
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   169
            ui.write("%-30s %s\n" % (keystr(ui, k), r))
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   170
16991
1aae34f109fd consistency: use REV instead of REVISION
Thomas Arendsen Hein <thomas@intevation.de>
parents: 16927
diff changeset
   171
@command("sigcheck", [], _('hg sigcheck REV'))
27117
ad2627f4af27 gpg: rename sigcheck function
timeless <timeless@mozdev.org>
parents: 26587
diff changeset
   172
def sigcheck(ui, repo, rev):
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   173
    """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
   174
    mygpg = newgpg(ui)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   175
    rev = repo.lookup(rev)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   176
    hexrev = hgnode.hex(rev)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   177
    keys = []
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   178
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   179
    for data, context in sigwalk(repo):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   180
        node, version, sig = data
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   181
        if node == hexrev:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   182
            k = getkeys(ui, repo, mygpg, data, context)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   183
            if k:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   184
                keys.extend(k)
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   185
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   186
    if not keys:
16927
7922004a46b8 gpg: lowercase messages
Martin Geisler <mg@aragost.com>
parents: 16743
diff changeset
   187
        ui.write(_("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
   188
        return
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   189
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   190
    # print summary
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   191
    ui.write("%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
   192
    for key in keys:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   193
        ui.write(" %s\n" % keystr(ui, key))
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   194
1681
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   195
def keystr(ui, key):
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   196
    """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
   197
    keyid, user, fingerprint = key
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   198
    comment = ui.config("gpg", fingerprint, None)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   199
    if comment:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   200
        return "%s (%s)" % (user, comment)
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   201
    else:
98eef041f9c7 fixes for gpg.py extension
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 1676
diff changeset
   202
        return user
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   203
14299
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
   204
@command("sign",
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
   205
         [('l', 'local', None, _('make the signature local')),
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
   206
          ('f', 'force', None, _('sign even if the sigfile is modified')),
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
   207
          ('', 'no-commit', None, _('do not commit the sigfile after signing')),
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
   208
          ('k', 'key', '',
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
   209
           _('the key id to sign with'), _('ID')),
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
   210
          ('m', 'message', '',
21951
59af0b21ec31 doc: unify help text for "--message" option
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21711
diff changeset
   211
           _('use text as commit message'), _('TEXT')),
21711
0986af9e7006 gpg: accept '--edit' like other commands creating new changeset
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19444
diff changeset
   212
          ('e', 'edit', False, _('invoke editor on commit messages')),
14299
f3ba4125d9e9 gpg: use cmdutil.command decorator
Martin Geisler <mg@aragost.com>
parents: 14168
diff changeset
   213
         ] + commands.commitopts2,
16991
1aae34f109fd consistency: use REV instead of REVISION
Thomas Arendsen Hein <thomas@intevation.de>
parents: 16927
diff changeset
   214
         _('hg sign [OPTION]... [REV]...'))
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   215
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
   216
    """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
   217
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   218
    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
   219
    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
   220
25791
917be0574d7f gpg: mention undocumented options
Matt Mackall <mpm@selenic.com>
parents: 25660
diff changeset
   221
    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
   222
    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
   223
11193
687c7d395f20 Use our custom hg reStructuredText role some more
Martin Geisler <mg@aragost.com>
parents: 10532
diff changeset
   224
    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
   225
    """
27196
7b4a61570d61 gpg: make sign acquire wlock before processing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27117
diff changeset
   226
    wlock = None
7b4a61570d61 gpg: make sign acquire wlock before processing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27117
diff changeset
   227
    try:
7b4a61570d61 gpg: make sign acquire wlock before processing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27117
diff changeset
   228
        wlock = repo.wlock()
7b4a61570d61 gpg: make sign acquire wlock before processing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27117
diff changeset
   229
        return _dosign(ui, repo, *revs, **opts)
7b4a61570d61 gpg: make sign acquire wlock before processing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27117
diff changeset
   230
    finally:
7b4a61570d61 gpg: make sign acquire wlock before processing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27117
diff changeset
   231
        lockmod.release(wlock)
3916
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   232
27196
7b4a61570d61 gpg: make sign acquire wlock before processing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 27117
diff changeset
   233
def _dosign(ui, repo, *revs, **opts):
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   234
    mygpg = newgpg(ui, **opts)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   235
    sigver = "0"
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   236
    sigmessage = ""
6139
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5475
diff changeset
   237
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5475
diff changeset
   238
    date = opts.get('date')
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5475
diff changeset
   239
    if date:
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5475
diff changeset
   240
        opts['date'] = util.parsedate(date)
989467e8e3a9 Fix bad behaviour when specifying an invalid date (issue700)
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5475
diff changeset
   241
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   242
    if revs:
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   243
        nodes = [repo.lookup(n) for n in revs]
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   244
    else:
3916
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   245
        nodes = [node for node in repo.dirstate.parents()
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   246
                 if node != hgnode.nullid]
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   247
        if len(nodes) > 1:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25791
diff changeset
   248
            raise error.Abort(_('uncommitted merge - please provide a '
3916
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   249
                               'specific revision'))
b1806b211910 Make 'hg sign' behave like other commands: Default to current parent.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 2875
diff changeset
   250
        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
   251
            nodes = [repo.changelog.tip()]
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   252
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   253
    for n in nodes:
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   254
        hexnode = hgnode.hex(n)
16927
7922004a46b8 gpg: lowercase messages
Martin Geisler <mg@aragost.com>
parents: 16743
diff changeset
   255
        ui.write(_("signing %d:%s\n") % (repo.changelog.rev(n),
10510
f77f3383c666 i18n: mark more strings for translation
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 10394
diff changeset
   256
                                         hgnode.short(n)))
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   257
        # build data
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   258
        data = node2txt(repo, n, sigver)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   259
        sig = mygpg.sign(data)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   260
        if not sig:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25791
diff changeset
   261
            raise error.Abort(_("error while signing"))
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   262
        sig = binascii.b2a_base64(sig)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   263
        sig = sig.replace("\n", "")
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   264
        sigmessage += "%s %s %s\n" % (hexnode, sigver, sig)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   265
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   266
    # write it
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   267
    if opts['local']:
23877
7cc77030c557 localrepo: remove all external users of localrepo.opener
Angel Ezquerra <angel.ezquerra@gmail.com>
parents: 22683
diff changeset
   268
        repo.vfs.append("localsigs", sigmessage)
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   269
        return
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   270
22682
9c89ac99690e gpg: move test of force before status call
Matt Mackall <mpm@selenic.com>
parents: 22681
diff changeset
   271
    if not opts["force"]:
9c89ac99690e gpg: move test of force before status call
Matt Mackall <mpm@selenic.com>
parents: 22681
diff changeset
   272
        msigs = match.exact(repo.root, '', ['.hgsigs'])
25149
3f0744eeaeaf cleanup: use __builtins__.any instead of util.any
Augie Fackler <augie@google.com>
parents: 23877
diff changeset
   273
        if any(repo.status(match=msigs, unknown=True, ignored=True)):
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25791
diff changeset
   274
            raise error.Abort(_("working copy of .hgsigs is changed "),
22683
f1872fdc1c3c gpg: use an abort hint and don't mention --force
Matt Mackall <mpm@selenic.com>
parents: 22682
diff changeset
   275
                             hint=_("please commit .hgsigs manually"))
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   276
13400
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 12965
diff changeset
   277
    sigsfile = repo.wfile(".hgsigs", "ab")
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 12965
diff changeset
   278
    sigsfile.write(sigmessage)
14f3795a5ed7 explicitly close files
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents: 12965
diff changeset
   279
    sigsfile.close()
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   280
4906
30847b8af7ca dirstate: add __contains__ and make __getitem__ more useful
Matt Mackall <mpm@selenic.com>
parents: 4730
diff changeset
   281
    if '.hgsigs' not in repo.dirstate:
11303
a1aad8333864 move working dir/dirstate methods from localrepo to workingctx
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 11193
diff changeset
   282
        repo[None].add([".hgsigs"])
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   283
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   284
    if opts["no_commit"]:
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   285
        return
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   286
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   287
    message = opts['message']
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   288
    if not message:
9183
d0225fa2f6c4 do not translate commit messages
Martin Geisler <mg@lazybytes.net>
parents: 8934
diff changeset
   289
        # we don't translate commit messages
d0225fa2f6c4 do not translate commit messages
Martin Geisler <mg@lazybytes.net>
parents: 8934
diff changeset
   290
        message = "\n".join(["Added signature for changeset %s"
5475
3aa5c45874c6 gpg: use the same log message format as hg tag
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5147
diff changeset
   291
                             % hgnode.short(n)
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   292
                             for n in nodes])
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   293
    try:
22001
135176a198d0 gpg: pass 'editform' argument to 'cmdutil.getcommiteditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21951
diff changeset
   294
        editor = cmdutil.getcommiteditor(editform='gpg.sign', **opts)
21711
0986af9e7006 gpg: accept '--edit' like other commands creating new changeset
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 19444
diff changeset
   295
        repo.commit(message, opts['user'], opts['date'], match=msigs,
22001
135176a198d0 gpg: pass 'editform' argument to 'cmdutil.getcommiteditor'
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 21951
diff changeset
   296
                    editor=editor)
25660
328739ea70c3 global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25186
diff changeset
   297
    except ValueError as inst:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25791
diff changeset
   298
        raise error.Abort(str(inst))
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   299
19443
2a7fd31ac548 gpg: add shortkey() to convert from long id to short
Wei, Elson <elson.wei@gmail.com>
parents: 19442
diff changeset
   300
def shortkey(ui, key):
2a7fd31ac548 gpg: add shortkey() to convert from long id to short
Wei, Elson <elson.wei@gmail.com>
parents: 19442
diff changeset
   301
    if len(key) != 16:
2a7fd31ac548 gpg: add shortkey() to convert from long id to short
Wei, Elson <elson.wei@gmail.com>
parents: 19442
diff changeset
   302
        ui.debug("key ID \"%s\" format error\n" % key)
2a7fd31ac548 gpg: add shortkey() to convert from long id to short
Wei, Elson <elson.wei@gmail.com>
parents: 19442
diff changeset
   303
        return key
2a7fd31ac548 gpg: add shortkey() to convert from long id to short
Wei, Elson <elson.wei@gmail.com>
parents: 19442
diff changeset
   304
2a7fd31ac548 gpg: add shortkey() to convert from long id to short
Wei, Elson <elson.wei@gmail.com>
parents: 19442
diff changeset
   305
    return key[-8:]
2a7fd31ac548 gpg: add shortkey() to convert from long id to short
Wei, Elson <elson.wei@gmail.com>
parents: 19442
diff changeset
   306
1592
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   307
def node2txt(repo, node, ver):
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   308
    """map a manifest into some text"""
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   309
    if ver == "0":
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   310
        return "%s\n" % hgnode.hex(node)
347c44611348 gpg signing extension for hg
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff changeset
   311
    else:
26587
56b2bcea2529 error: get Abort from 'error' instead of 'util'
Pierre-Yves David <pierre-yves.david@fb.com>
parents: 25791
diff changeset
   312
        raise error.Abort(_("unknown signature version"))