annotate mercurial/commands.py @ 209:63af1db35611

Beginning of new command parsing interface -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Beginning of new command parsing interface This adds commands.py, with a primary interface dispatch(args) Dispatch searches a table of known commands, handles switches, sets up a repo object if appropriate, and dispatches the command. It also handles KeyboardInterrupt and can handle similar exceptions in the future. If the command is unknown, it falls through to the current command handler. Commands currently handled by the new scheme: help, init, and annotate manifest hash: 134cd032c880985e3f92f82efb8b629dd862ba4c -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCnXEGywK+sNU5EO8RAuDAAJ9q7K4w7qGVWv1NWjCPFGO/UJc6VQCdEhMQ sBBlSRzah9QPy8K94catZyg= =wuRf -----END PGP SIGNATURE-----
author mpm@selenic.com
date Wed, 01 Jun 2005 00:25:42 -0800
parents
children d2badbd7d1ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
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
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
93 table = {
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
94 "init": (init, [], 'hg init'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
95 "help": (help, [], 'hg init'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
96 "checkout|co": (checkout, [], 'hg init'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
97 "ann|annotate": (annotate,
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
98 [('r', 'revision', '', 'revision'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
99 ('u', 'user', None, 'show user'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
100 ('n', 'number', None, 'show revision number'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
101 ('c', 'changeset', None, 'show changeset')],
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
102 'hg annotate [-u] [-c] [-n] [-r id] [files]'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
103 }
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
104
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
105 norepo = "init branch help"
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
106
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
107 def dispatch(args):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
108 options = {}
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
109 opts = [('v', 'verbose', None, 'verbose'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
110 ('d', 'debug', None, 'debug'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
111 ('q', 'quiet', None, 'quiet'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
112 ('y', 'noninteractive', None, 'run non-interactively'),
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
113 ]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
114
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
115 args = fancyopts.fancyopts(args, opts, options,
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
116 'hg [options] <command> [options] [files]')
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 if not args:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
119 cmd = "help"
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
120 else:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
121 cmd, args = args[0], args[1:]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
122
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
123 u = ui.ui(options["verbose"], options["debug"], options["quiet"],
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
124 not options["noninteractive"])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
125
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
126 i = None
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
127 for e in table.keys():
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
128 if re.match(e + "$", cmd):
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
129 i = table[e]
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
130
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
131 # deal with this internally later
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
132 if not i: raise UnknownCommand(cmd)
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 cmdoptions = {}
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
135 args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2])
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
136
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
137 if cmd not in norepo.split():
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
138 repo = hg.repository(ui = u)
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
139 d = lambda: i[0](u, repo, args, **cmdoptions)
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 d = lambda: i[0](u, args, **cmdoptions)
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 try:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
144 d()
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
145 except KeyboardInterrupt:
63af1db35611 Beginning of new command parsing interface
mpm@selenic.com
parents:
diff changeset
146 u.warn("interrupted!\n")