annotate mercurial/commands.py @ 212:48398a5353e3

commands: better argument processing, per-command help -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 commands: better argument processing, per-command help This passes command line arguments as positional arguments rather than as a list and attempt to catch exceptions if the arguments don't match up. It also adds 'hg help [cmd]' which prints the syntax and docstring for the given command. manifest hash: cef3ac7076f99fce4265a5dc7acb57fb03d93270 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCnghXywK+sNU5EO8RAv+5AJ9MfCCRxE+pNu3i44v5I4IyQoRaiQCeNqEK wBzAiP+Q6F/9PL2xOrrZGHw= =EYdk -----END PGP SIGNATURE-----
author mpm@selenic.com
date Wed, 01 Jun 2005 11:11:19 -0800
parents 426d3c3ae363
children d2172916ef6c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
1 import os, re, traceback, sys
209
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
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
12 def help(ui, cmd=None):
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
13 '''show help'''
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
14 if cmd:
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
15 try:
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
16 i = find(cmd)
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
17 ui.write("%s\n\n" % i[2])
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
18 ui.write(i[0].__doc__, "\n")
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
19 except UnknownCommand:
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
20 ui.warn("unknown command %s", cmd)
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
21 sys.exit(0)
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
22
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
23 ui.status("""\
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
24 hg commands:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
25
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
26 add [files...] add the given files in the next commit
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
27 addremove add all new files, delete all missing files
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
28 annotate [files...] show changeset number per file line
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
29 branch <path> create a branch of <path> in this directory
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
30 checkout [changeset] checkout the latest or given changeset
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
31 commit commit all changes to the repository
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
32 diff [files...] diff working directory (or selected files)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
33 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
34 dumpmanifest [rev] dump the latest or given revision of the manifest
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
35 export <rev> dump the changeset header and diffs for a revision
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
36 history show changeset history
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
37 init create a new repository in this directory
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
38 log <file> show revision history of a single file
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
39 merge <path> merge changes from <path> into local repository
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
40 recover rollback an interrupted transaction
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
41 remove [files...] remove the given files in the next commit
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
42 serve export the repository via HTTP
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
43 status show new, missing, and changed files in working dir
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
44 tags show current changeset tags
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
45 undo undo the last transaction
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
46 """)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
47
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
48 def init(ui):
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
49 """create a repository"""
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
50 hg.repository(ui, ".", create=1)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
51
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
52 def checkout(u, repo, changeset=None):
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
53 '''checkout a given changeset or the current tip'''
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
54 node = repo.changelog.tip()
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
55 if changeset:
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
56 node = repo.lookup(changeset)
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
57 repo.checkout(node)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
58
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
59 def annotate(u, repo, *args, **ops):
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
60 def getnode(rev):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
61 return hg.short(repo.changelog.node(rev))
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
62
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
63 def getname(rev):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
64 try:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
65 return bcache[rev]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
66 except KeyError:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
67 cl = repo.changelog.read(repo.changelog.node(rev))
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
68 name = cl[1]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
69 f = name.find('@')
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
70 if f >= 0:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
71 name = name[:f]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
72 bcache[rev] = name
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
73 return name
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
74
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
75 bcache = {}
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
76 opmap = [['user', getname], ['number', str], ['changeset', getnode]]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
77 if not ops['user'] and not ops['changeset']:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
78 ops['number'] = 1
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
79
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
80 args = relpath(repo, args)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
81 node = repo.current
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
82 if ops['revision']:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
83 node = repo.changelog.lookup(ops['revision'])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
84 change = repo.changelog.read(node)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
85 mmap = repo.manifest.read(change[0])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
86 maxuserlen = 0
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
87 maxchangelen = 0
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
88 for f in args:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
89 lines = repo.file(f).annotate(mmap[f])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
90 pieces = []
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
91
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
92 for o, f in opmap:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
93 if ops[o]:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
94 l = [ f(n) for n,t in lines ]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
95 m = max(map(len, l))
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
96 pieces.append([ "%*s" % (m, x) for x in l])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
97
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
98 for p,l in zip(zip(*pieces), lines):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
99 u.write(" ".join(p) + ": " + l[1])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
100
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
101 def undo(ui, repo):
210
d2badbd7d1ad hg undo: fixup working dir state
mpm@selenic.com
parents: 209
diff changeset
102 repo.undo()
d2badbd7d1ad hg undo: fixup working dir state
mpm@selenic.com
parents: 209
diff changeset
103
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
104 table = {
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
105 "init": (init, [], 'hg init'),
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
106 "help": (help, [], 'hg help [command]'),
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
107 "checkout|co": (checkout, [], 'hg checkout [changeset]'),
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
108 "ann|annotate": (annotate,
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
109 [('r', 'revision', '', 'revision'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
110 ('u', 'user', None, 'show user'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
111 ('n', 'number', None, 'show revision number'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
112 ('c', 'changeset', None, 'show changeset')],
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
113 'hg annotate [-u] [-c] [-n] [-r id] [files]'),
210
d2badbd7d1ad hg undo: fixup working dir state
mpm@selenic.com
parents: 209
diff changeset
114 "undo": (undo, [], 'hg undo'),
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
115 }
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
116
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
117 norepo = "init branch help"
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
118
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
119 def find(cmd):
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
120 i = None
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
121 for e in table.keys():
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
122 if re.match(e + "$", cmd):
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
123 return table[e]
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
124
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
125 raise UnknownCommand(cmd)
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
126
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
127 def dispatch(args):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
128 options = {}
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
129 opts = [('v', 'verbose', None, 'verbose'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
130 ('d', 'debug', None, 'debug'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
131 ('q', 'quiet', None, 'quiet'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
132 ('y', 'noninteractive', None, 'run non-interactively'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
133 ]
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 args = fancyopts.fancyopts(args, opts, options,
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
136 'hg [options] <command> [options] [files]')
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 if not args:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
139 cmd = "help"
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
140 else:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
141 cmd, args = args[0], args[1:]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
142
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
143 u = ui.ui(options["verbose"], options["debug"], options["quiet"],
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
144 not options["noninteractive"])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
145
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
146 # deal with unfound commands later
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
147 i = find(cmd)
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
148
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
149 cmdoptions = {}
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
150 args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
151
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
152 if cmd not in norepo.split():
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
153 repo = hg.repository(ui = u)
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
154 d = lambda: i[0](u, repo, *args, **cmdoptions)
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
155 else:
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
156 d = lambda: i[0](u, *args, **cmdoptions)
209
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
157
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
158 try:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
159 d()
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
160 except KeyboardInterrupt:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
161 u.warn("interrupted!\n")
212
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
162 except TypeError, inst:
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
163 # was this an argument error?
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
164 tb = traceback.extract_tb(sys.exc_info()[2])
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
165 if len(tb) > 2: # no
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
166 raise
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
167 u.warn("%s: invalid arguments\n" % i[0].__name__)
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
168 u.warn("syntax: %s\n" % i[2])
48398a5353e3 commands: better argument processing, per-command help
mpm@selenic.com
parents: 211
diff changeset
169 sys.exit(-1)