Show More
@@ -0,0 +1,146 | |||||
|
1 | import os, re | |||
|
2 | from mercurial import fancyopts, ui, hg | |||
|
3 | ||||
|
4 | class UnknownCommand(Exception): pass | |||
|
5 | ||||
|
6 | def relpath(repo, args): | |||
|
7 | if os.getcwd() != repo.root: | |||
|
8 | p = os.getcwd()[len(repo.root) + 1: ] | |||
|
9 | return [ os.path.join(p, x) for x in args ] | |||
|
10 | return args | |||
|
11 | ||||
|
12 | def help(ui, args): | |||
|
13 | ui.status("""\ | |||
|
14 | hg commands: | |||
|
15 | ||||
|
16 | add [files...] add the given files in the next commit | |||
|
17 | addremove add all new files, delete all missing files | |||
|
18 | annotate [files...] show changeset number per file line | |||
|
19 | branch <path> create a branch of <path> in this directory | |||
|
20 | checkout [changeset] checkout the latest or given changeset | |||
|
21 | commit commit all changes to the repository | |||
|
22 | diff [files...] diff working directory (or selected files) | |||
|
23 | dump <file> [rev] dump the latest or given revision of a file | |||
|
24 | dumpmanifest [rev] dump the latest or given revision of the manifest | |||
|
25 | export <rev> dump the changeset header and diffs for a revision | |||
|
26 | history show changeset history | |||
|
27 | init create a new repository in this directory | |||
|
28 | log <file> show revision history of a single file | |||
|
29 | merge <path> merge changes from <path> into local repository | |||
|
30 | recover rollback an interrupted transaction | |||
|
31 | remove [files...] remove the given files in the next commit | |||
|
32 | serve export the repository via HTTP | |||
|
33 | status show new, missing, and changed files in working dir | |||
|
34 | tags show current changeset tags | |||
|
35 | undo undo the last transaction | |||
|
36 | """) | |||
|
37 | ||||
|
38 | def init(ui, args): | |||
|
39 | """create a repository""" | |||
|
40 | hg.repository(ui, ".", create=1) | |||
|
41 | ||||
|
42 | def checkout(u, repo, args): | |||
|
43 | node = repo.changelog.tip() | |||
|
44 | if args: | |||
|
45 | node = repo.lookup(args[0]) | |||
|
46 | repo.checkout(node) | |||
|
47 | ||||
|
48 | def annotate(u, repo, args, **ops): | |||
|
49 | if not args: | |||
|
50 | return | |||
|
51 | ||||
|
52 | def getnode(rev): | |||
|
53 | return hg.short(repo.changelog.node(rev)) | |||
|
54 | ||||
|
55 | def getname(rev): | |||
|
56 | try: | |||
|
57 | return bcache[rev] | |||
|
58 | except KeyError: | |||
|
59 | cl = repo.changelog.read(repo.changelog.node(rev)) | |||
|
60 | name = cl[1] | |||
|
61 | f = name.find('@') | |||
|
62 | if f >= 0: | |||
|
63 | name = name[:f] | |||
|
64 | bcache[rev] = name | |||
|
65 | return name | |||
|
66 | ||||
|
67 | bcache = {} | |||
|
68 | opmap = [['user', getname], ['number', str], ['changeset', getnode]] | |||
|
69 | if not ops['user'] and not ops['changeset']: | |||
|
70 | ops['number'] = 1 | |||
|
71 | ||||
|
72 | args = relpath(repo, args) | |||
|
73 | node = repo.current | |||
|
74 | if ops['revision']: | |||
|
75 | node = repo.changelog.lookup(ops['revision']) | |||
|
76 | change = repo.changelog.read(node) | |||
|
77 | mmap = repo.manifest.read(change[0]) | |||
|
78 | maxuserlen = 0 | |||
|
79 | maxchangelen = 0 | |||
|
80 | for f in args: | |||
|
81 | lines = repo.file(f).annotate(mmap[f]) | |||
|
82 | pieces = [] | |||
|
83 | ||||
|
84 | for o, f in opmap: | |||
|
85 | if ops[o]: | |||
|
86 | l = [ f(n) for n,t in lines ] | |||
|
87 | m = max(map(len, l)) | |||
|
88 | pieces.append([ "%*s" % (m, x) for x in l]) | |||
|
89 | ||||
|
90 | for p,l in zip(zip(*pieces), lines): | |||
|
91 | u.write(" ".join(p) + ": " + l[1]) | |||
|
92 | ||||
|
93 | table = { | |||
|
94 | "init": (init, [], 'hg init'), | |||
|
95 | "help": (help, [], 'hg init'), | |||
|
96 | "checkout|co": (checkout, [], 'hg init'), | |||
|
97 | "ann|annotate": (annotate, | |||
|
98 | [('r', 'revision', '', 'revision'), | |||
|
99 | ('u', 'user', None, 'show user'), | |||
|
100 | ('n', 'number', None, 'show revision number'), | |||
|
101 | ('c', 'changeset', None, 'show changeset')], | |||
|
102 | 'hg annotate [-u] [-c] [-n] [-r id] [files]'), | |||
|
103 | } | |||
|
104 | ||||
|
105 | norepo = "init branch help" | |||
|
106 | ||||
|
107 | def dispatch(args): | |||
|
108 | options = {} | |||
|
109 | opts = [('v', 'verbose', None, 'verbose'), | |||
|
110 | ('d', 'debug', None, 'debug'), | |||
|
111 | ('q', 'quiet', None, 'quiet'), | |||
|
112 | ('y', 'noninteractive', None, 'run non-interactively'), | |||
|
113 | ] | |||
|
114 | ||||
|
115 | args = fancyopts.fancyopts(args, opts, options, | |||
|
116 | 'hg [options] <command> [options] [files]') | |||
|
117 | ||||
|
118 | if not args: | |||
|
119 | cmd = "help" | |||
|
120 | else: | |||
|
121 | cmd, args = args[0], args[1:] | |||
|
122 | ||||
|
123 | u = ui.ui(options["verbose"], options["debug"], options["quiet"], | |||
|
124 | not options["noninteractive"]) | |||
|
125 | ||||
|
126 | i = None | |||
|
127 | for e in table.keys(): | |||
|
128 | if re.match(e + "$", cmd): | |||
|
129 | i = table[e] | |||
|
130 | ||||
|
131 | # deal with this internally later | |||
|
132 | if not i: raise UnknownCommand(cmd) | |||
|
133 | ||||
|
134 | cmdoptions = {} | |||
|
135 | args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2]) | |||
|
136 | ||||
|
137 | if cmd not in norepo.split(): | |||
|
138 | repo = hg.repository(ui = u) | |||
|
139 | d = lambda: i[0](u, repo, args, **cmdoptions) | |||
|
140 | else: | |||
|
141 | d = lambda: i[0](u, args, **cmdoptions) | |||
|
142 | ||||
|
143 | try: | |||
|
144 | d() | |||
|
145 | except KeyboardInterrupt: | |||
|
146 | u.warn("interrupted!\n") |
@@ -17,7 +17,7 | |||||
17 | # pass |
|
17 | # pass | |
18 |
|
18 | |||
19 | import sys, os, time |
|
19 | import sys, os, time | |
20 | from mercurial import hg, mdiff, fancyopts, ui |
|
20 | from mercurial import hg, mdiff, fancyopts, ui, commands | |
21 |
|
21 | |||
22 | def help(): |
|
22 | def help(): | |
23 | ui.status("""\ |
|
23 | ui.status("""\ | |
@@ -93,6 +93,13 def diff(files = None, node1 = None, nod | |||||
93 | tn = "" |
|
93 | tn = "" | |
94 | sys.stdout.write(mdiff.unidiff(to, date1, tn, date2, f)) |
|
94 | sys.stdout.write(mdiff.unidiff(to, date1, tn, date2, f)) | |
95 |
|
95 | |||
|
96 | ||||
|
97 | try: | |||
|
98 | sys.exit(commands.dispatch(sys.argv[1:])) | |||
|
99 | except commands.UnknownCommand: | |||
|
100 | # fall through | |||
|
101 | pass | |||
|
102 | ||||
96 | options = {} |
|
103 | options = {} | |
97 | opts = [('v', 'verbose', None, 'verbose'), |
|
104 | opts = [('v', 'verbose', None, 'verbose'), | |
98 | ('d', 'debug', None, 'debug'), |
|
105 | ('d', 'debug', None, 'debug'), | |
@@ -243,59 +250,6 elif cmd == "diff": | |||||
243 |
|
250 | |||
244 | diff(args, *revs) |
|
251 | diff(args, *revs) | |
245 |
|
252 | |||
246 | elif cmd == "annotate": |
|
|||
247 | bcache = {} |
|
|||
248 |
|
||||
249 | def getnode(rev): |
|
|||
250 | return hg.short(repo.changelog.node(rev)) |
|
|||
251 |
|
||||
252 | def getname(rev): |
|
|||
253 | try: |
|
|||
254 | return bcache[rev] |
|
|||
255 | except KeyError: |
|
|||
256 | cl = repo.changelog.read(repo.changelog.node(rev)) |
|
|||
257 | name = cl[1] |
|
|||
258 | f = name.find('@') |
|
|||
259 | if f >= 0: |
|
|||
260 | name = name[:f] |
|
|||
261 | bcache[rev] = name |
|
|||
262 | return name |
|
|||
263 |
|
||||
264 | aoptions = {} |
|
|||
265 | opts = [('r', 'revision', '', 'revision'), |
|
|||
266 | ('u', 'user', None, 'show user'), |
|
|||
267 | ('n', 'number', None, 'show revision number'), |
|
|||
268 | ('c', 'changeset', None, 'show changeset')] |
|
|||
269 |
|
||||
270 | args = fancyopts.fancyopts(args, opts, aoptions, |
|
|||
271 | 'hg annotate [-u] [-c] [-n] [-r id] [files]') |
|
|||
272 |
|
||||
273 | opmap = [['user', getname], ['number', str], ['changeset', getnode]] |
|
|||
274 | if not aoptions['user'] and not aoptions['changeset']: |
|
|||
275 | aoptions['number'] = 1 |
|
|||
276 |
|
||||
277 | if args: |
|
|||
278 | if relpath: args = [ os.path.join(relpath, x) for x in args ] |
|
|||
279 | node = repo.current |
|
|||
280 | if aoptions['revision']: |
|
|||
281 | node = repo.changelog.lookup(aoptions['revision']) |
|
|||
282 | change = repo.changelog.read(node) |
|
|||
283 | mmap = repo.manifest.read(change[0]) |
|
|||
284 | maxuserlen = 0 |
|
|||
285 | maxchangelen = 0 |
|
|||
286 | for f in args: |
|
|||
287 | lines = repo.file(f).annotate(mmap[f]) |
|
|||
288 | pieces = [] |
|
|||
289 |
|
||||
290 | for o, f in opmap: |
|
|||
291 | if aoptions[o]: |
|
|||
292 | l = [ f(n) for n,t in lines ] |
|
|||
293 | m = max(map(len, l)) |
|
|||
294 | pieces.append([ "%*s" % (m, x) for x in l]) |
|
|||
295 |
|
||||
296 | for p,l in zip(zip(*pieces), lines): |
|
|||
297 | sys.stdout.write(" ".join(p) + ": " + l[1]) |
|
|||
298 |
|
||||
299 | elif cmd == "export": |
|
253 | elif cmd == "export": | |
300 | node = repo.lookup(args[0]) |
|
254 | node = repo.lookup(args[0]) | |
301 | prev, other = repo.changelog.parents(node) |
|
255 | prev, other = repo.changelog.parents(node) |
@@ -46,6 +46,8 def fancyopts(args, options, state, synt | |||||
46 | elif dt[map[opt]] is type(''): state[map[opt]] = arg |
|
46 | elif dt[map[opt]] is type(''): state[map[opt]] = arg | |
47 | elif dt[map[opt]] is type([]): state[map[opt]].append(arg) |
|
47 | elif dt[map[opt]] is type([]): state[map[opt]].append(arg) | |
48 | elif dt[map[opt]] is type(None): state[map[opt]] = 1 |
|
48 | elif dt[map[opt]] is type(None): state[map[opt]] = 1 | |
49 |
|
49 | |||
|
50 | del state["help"] | |||
|
51 | ||||
50 | return args |
|
52 | return args | |
51 |
|
53 |
General Comments 0
You need to be logged in to leave comments.
Login now