mercurial/commands.py
author mpm@selenic.com
Wed, 01 Jun 2005 10:43:11 -0800
changeset 211 426d3c3ae363
parent 210 d2badbd7d1ad
child 212 48398a5353e3
permissions -rw-r--r--
commands: fix up some help strings -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 commands: fix up some help strings manifest hash: e9e6061cd37d77e8061cab6a0cd3ca701e6900d7 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCngG/ywK+sNU5EO8RAnhCAJ422e7LecJ5D/15I5PcQZxGvsgvvQCeOWIb 7LTyyAYDeht6yOQCdLWmXIE= =bjQG -----END PGP SIGNATURE-----
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
     1
import os, re
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
     2
from mercurial import fancyopts, ui, hg
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
     3
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
     4
class UnknownCommand(Exception): pass
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
     5
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
     6
def relpath(repo, args):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
     7
    if os.getcwd() != repo.root:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
     8
        p = os.getcwd()[len(repo.root) + 1: ]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
     9
        return [ os.path.join(p, x) for x in args ]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    10
    return args
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    11
    
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    12
def help(ui, args):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    13
    ui.status("""\
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    14
 hg commands:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    15
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    16
 add [files...]        add the given files in the next commit
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    17
 addremove             add all new files, delete all missing files
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    18
 annotate [files...]   show changeset number per file line
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    19
 branch <path>         create a branch of <path> in this directory
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    20
 checkout [changeset]  checkout the latest or given changeset
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    21
 commit                commit all changes to the repository
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    22
 diff [files...]       diff working directory (or selected files)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    23
 dump <file> [rev]     dump the latest or given revision of a file
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    24
 dumpmanifest [rev]    dump the latest or given revision of the manifest
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    25
 export <rev>          dump the changeset header and diffs for a revision
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    26
 history               show changeset history
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    27
 init                  create a new repository in this directory
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    28
 log <file>            show revision history of a single file
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    29
 merge <path>          merge changes from <path> into local repository
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    30
 recover               rollback an interrupted transaction
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    31
 remove [files...]     remove the given files in the next commit
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    32
 serve                 export the repository via HTTP
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    33
 status                show new, missing, and changed files in working dir
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    34
 tags                  show current changeset tags
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    35
 undo                  undo the last transaction
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    36
""")
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    37
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    38
def init(ui, args):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    39
    """create a repository"""
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    40
    hg.repository(ui, ".", create=1)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    41
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    42
def checkout(u, repo, args):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    43
    node = repo.changelog.tip()
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    44
    if args:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    45
        node = repo.lookup(args[0])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    46
    repo.checkout(node)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    47
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    48
def annotate(u, repo, args, **ops):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    49
    if not args:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    50
        return
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    51
        
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    52
    def getnode(rev):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    53
        return hg.short(repo.changelog.node(rev))
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    54
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    55
    def getname(rev):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    56
        try:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    57
            return bcache[rev]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    58
        except KeyError:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    59
            cl = repo.changelog.read(repo.changelog.node(rev))
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    60
            name = cl[1]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    61
            f = name.find('@')
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    62
            if f >= 0:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    63
                name = name[:f]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    64
            bcache[rev] = name
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    65
            return name
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    66
    
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    67
    bcache = {}
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    68
    opmap = [['user', getname], ['number', str], ['changeset', getnode]]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    69
    if not ops['user'] and not ops['changeset']:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    70
        ops['number'] = 1
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    71
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    72
    args = relpath(repo, args)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    73
    node = repo.current
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    74
    if ops['revision']:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    75
        node = repo.changelog.lookup(ops['revision'])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    76
    change = repo.changelog.read(node)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    77
    mmap = repo.manifest.read(change[0])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    78
    maxuserlen = 0
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    79
    maxchangelen = 0
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    80
    for f in args:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    81
        lines = repo.file(f).annotate(mmap[f])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    82
        pieces = []
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    83
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    84
        for o, f in opmap:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    85
            if ops[o]:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    86
                l = [ f(n) for n,t in lines ]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    87
                m = max(map(len, l))
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    88
                pieces.append([ "%*s" % (m, x) for x in l])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    89
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    90
        for p,l in zip(zip(*pieces), lines):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    91
            u.write(" ".join(p) + ": " + l[1])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    92
210
d2badbd7d1ad hg undo: fixup working dir state
mpm@selenic.com
parents: 209
diff changeset
    93
def undo(ui, repo, args):
d2badbd7d1ad hg undo: fixup working dir state
mpm@selenic.com
parents: 209
diff changeset
    94
    repo.undo()
d2badbd7d1ad hg undo: fixup working dir state
mpm@selenic.com
parents: 209
diff changeset
    95
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    96
table = {
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
    97
    "init": (init, [], 'hg init'),
211
426d3c3ae363 commands: fix up some help strings
mpm@selenic.com
parents: 210
diff changeset
    98
    "help": (help, [], 'hg help'),
426d3c3ae363 commands: fix up some help strings
mpm@selenic.com
parents: 210
diff changeset
    99
    "checkout|co": (checkout, [], 'hg checkout'),
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   100
    "ann|annotate": (annotate,
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   101
                     [('r', 'revision', '', 'revision'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   102
                      ('u', 'user', None, 'show user'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   103
                      ('n', 'number', None, 'show revision number'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   104
                      ('c', 'changeset', None, 'show changeset')],
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   105
                     'hg annotate [-u] [-c] [-n] [-r id] [files]'),
210
d2badbd7d1ad hg undo: fixup working dir state
mpm@selenic.com
parents: 209
diff changeset
   106
    "undo": (undo, [], 'hg undo'),
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   107
    }
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   108
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   109
norepo = "init branch help"
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   110
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   111
def dispatch(args):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   112
    options = {}
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   113
    opts = [('v', 'verbose', None, 'verbose'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   114
            ('d', 'debug', None, 'debug'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   115
            ('q', 'quiet', None, 'quiet'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   116
            ('y', 'noninteractive', None, 'run non-interactively'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   117
            ]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   118
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   119
    args = fancyopts.fancyopts(args, opts, options,
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   120
                               'hg [options] <command> [options] [files]')
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   121
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   122
    if not args:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   123
        cmd = "help"
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   124
    else:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   125
        cmd, args = args[0], args[1:]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   126
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   127
    u = ui.ui(options["verbose"], options["debug"], options["quiet"],
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   128
           not options["noninteractive"])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   129
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   130
    i = None
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   131
    for e in table.keys():
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   132
        if re.match(e + "$", cmd):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   133
            i = table[e]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   134
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   135
    # deal with this internally later
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   136
    if not i: raise UnknownCommand(cmd)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   137
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   138
    cmdoptions = {}
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   139
    args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   140
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   141
    if cmd not in norepo.split():
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   142
        repo = hg.repository(ui = u)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   143
        d = lambda: i[0](u, repo, args, **cmdoptions)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   144
    else:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   145
        d = lambda: i[0](u, args, **cmdoptions)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   146
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   147
    try:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   148
        d()
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   149
    except KeyboardInterrupt:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
   150
        u.warn("interrupted!\n")