author | mpm@selenic.com |
Fri, 03 Jun 2005 12:58:47 -0800 | |
changeset 232 | fc4a6e5b5812 |
parent 230 | 00ea3613f82c |
child 237 | 4f802588cdfb |
child 237 | 4f802588cdfb |
child 241 | afe895fcc0d0 |
permissions | -rw-r--r-- |
221 | 1 |
import os, re, traceback, sys, signal, time |
209 | 2 |
from mercurial import fancyopts, ui, hg |
3 |
||
4 |
class UnknownCommand(Exception): pass |
|
5 |
||
213 | 6 |
def filterfiles(list, files): |
7 |
l = [ x for x in list if x in files ] |
|
8 |
||
9 |
for f in files: |
|
10 |
if f[-1] != os.sep: f += os.sep |
|
11 |
l += [ x for x in list if x.startswith(f) ] |
|
12 |
return l |
|
13 |
||
14 |
def relfilter(repo, args): |
|
15 |
if os.getcwd() != repo.root: |
|
16 |
p = os.getcwd()[len(repo.root) + 1: ] |
|
17 |
return filterfiles(p, args) |
|
18 |
return args |
|
19 |
||
209 | 20 |
def relpath(repo, args): |
21 |
if os.getcwd() != repo.root: |
|
22 |
p = os.getcwd()[len(repo.root) + 1: ] |
|
23 |
return [ os.path.join(p, x) for x in args ] |
|
24 |
return args |
|
25 |
||
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
26 |
def help(ui, cmd=None): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
27 |
'''show help''' |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
28 |
if cmd: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
29 |
try: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
30 |
i = find(cmd) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
31 |
ui.write("%s\n\n" % i[2]) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
32 |
ui.write(i[0].__doc__, "\n") |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
33 |
except UnknownCommand: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
34 |
ui.warn("unknown command %s", cmd) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
35 |
sys.exit(0) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
36 |
|
209 | 37 |
ui.status("""\ |
38 |
hg commands: |
|
39 |
||
40 |
add [files...] add the given files in the next commit |
|
41 |
addremove add all new files, delete all missing files |
|
42 |
annotate [files...] show changeset number per file line |
|
43 |
branch <path> create a branch of <path> in this directory |
|
44 |
checkout [changeset] checkout the latest or given changeset |
|
45 |
commit commit all changes to the repository |
|
46 |
diff [files...] diff working directory (or selected files) |
|
47 |
dump <file> [rev] dump the latest or given revision of a file |
|
48 |
dumpmanifest [rev] dump the latest or given revision of the manifest |
|
49 |
export <rev> dump the changeset header and diffs for a revision |
|
50 |
history show changeset history |
|
51 |
init create a new repository in this directory |
|
52 |
log <file> show revision history of a single file |
|
53 |
merge <path> merge changes from <path> into local repository |
|
54 |
recover rollback an interrupted transaction |
|
55 |
remove [files...] remove the given files in the next commit |
|
56 |
serve export the repository via HTTP |
|
57 |
status show new, missing, and changed files in working dir |
|
58 |
tags show current changeset tags |
|
59 |
undo undo the last transaction |
|
60 |
""") |
|
61 |
||
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
62 |
def init(ui): |
209 | 63 |
"""create a repository""" |
64 |
hg.repository(ui, ".", create=1) |
|
65 |
||
213 | 66 |
def branch(ui, path): |
67 |
'''branch from a local repository''' |
|
68 |
# this should eventually support remote repos |
|
69 |
os.system("cp -al %s/.hg .hg" % path) |
|
70 |
||
219
8ff4532376a4
hg checkout: refuse to checkout if there are outstanding changes
mpm@selenic.com
parents:
214
diff
changeset
|
71 |
def checkout(ui, repo, changeset=None): |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
72 |
'''checkout a given changeset or the current tip''' |
230 | 73 |
(c, a, d, u) = repo.diffdir(repo.root) |
220 | 74 |
if c or a or d: |
219
8ff4532376a4
hg checkout: refuse to checkout if there are outstanding changes
mpm@selenic.com
parents:
214
diff
changeset
|
75 |
ui.warn("aborting (outstanding changes in working directory)\n") |
8ff4532376a4
hg checkout: refuse to checkout if there are outstanding changes
mpm@selenic.com
parents:
214
diff
changeset
|
76 |
sys.exit(1) |
8ff4532376a4
hg checkout: refuse to checkout if there are outstanding changes
mpm@selenic.com
parents:
214
diff
changeset
|
77 |
|
209 | 78 |
node = repo.changelog.tip() |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
79 |
if changeset: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
80 |
node = repo.lookup(changeset) |
209 | 81 |
repo.checkout(node) |
82 |
||
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
83 |
def annotate(u, repo, *args, **ops): |
209 | 84 |
def getnode(rev): |
85 |
return hg.short(repo.changelog.node(rev)) |
|
86 |
||
87 |
def getname(rev): |
|
88 |
try: |
|
89 |
return bcache[rev] |
|
90 |
except KeyError: |
|
91 |
cl = repo.changelog.read(repo.changelog.node(rev)) |
|
92 |
name = cl[1] |
|
93 |
f = name.find('@') |
|
94 |
if f >= 0: |
|
95 |
name = name[:f] |
|
96 |
bcache[rev] = name |
|
97 |
return name |
|
98 |
||
99 |
bcache = {} |
|
100 |
opmap = [['user', getname], ['number', str], ['changeset', getnode]] |
|
101 |
if not ops['user'] and not ops['changeset']: |
|
102 |
ops['number'] = 1 |
|
103 |
||
104 |
args = relpath(repo, args) |
|
227 | 105 |
node = repo.dirstate.parents()[0] |
209 | 106 |
if ops['revision']: |
107 |
node = repo.changelog.lookup(ops['revision']) |
|
108 |
change = repo.changelog.read(node) |
|
109 |
mmap = repo.manifest.read(change[0]) |
|
110 |
maxuserlen = 0 |
|
111 |
maxchangelen = 0 |
|
112 |
for f in args: |
|
113 |
lines = repo.file(f).annotate(mmap[f]) |
|
114 |
pieces = [] |
|
115 |
||
116 |
for o, f in opmap: |
|
117 |
if ops[o]: |
|
118 |
l = [ f(n) for n,t in lines ] |
|
119 |
m = max(map(len, l)) |
|
120 |
pieces.append([ "%*s" % (m, x) for x in l]) |
|
121 |
||
122 |
for p,l in zip(zip(*pieces), lines): |
|
123 |
u.write(" ".join(p) + ": " + l[1]) |
|
124 |
||
221 | 125 |
def heads(ui, repo): |
126 |
'''show current repository heads''' |
|
127 |
for n in repo.changelog.heads(): |
|
128 |
i = repo.changelog.rev(n) |
|
129 |
changes = repo.changelog.read(n) |
|
130 |
(p1, p2) = repo.changelog.parents(n) |
|
131 |
(h, h1, h2) = map(hg.hex, (n, p1, p2)) |
|
132 |
(i1, i2) = map(repo.changelog.rev, (p1, p2)) |
|
133 |
print "rev: %4d:%s" % (i, h) |
|
134 |
print "parents: %4d:%s" % (i1, h1) |
|
135 |
if i2: print " %4d:%s" % (i2, h2) |
|
136 |
print "manifest: %4d:%s" % (repo.manifest.rev(changes[0]), |
|
137 |
hg.hex(changes[0])) |
|
138 |
print "user:", changes[1] |
|
139 |
print "date:", time.asctime( |
|
140 |
time.localtime(float(changes[2].split(' ')[0]))) |
|
141 |
if ui.verbose: print "files:", " ".join(changes[3]) |
|
142 |
print "description:" |
|
143 |
print changes[4] |
|
144 |
||
227 | 145 |
def parents(ui, repo, node = None): |
146 |
'''show the parents of the current working dir''' |
|
147 |
if node: |
|
148 |
p = repo.changelog.parents(repo.lookup(hg.bin(node))) |
|
149 |
else: |
|
150 |
p = repo.dirstate.parents() |
|
151 |
||
152 |
for n in p: |
|
153 |
if n != hg.nullid: |
|
154 |
ui.write("%d:%s\n" % (repo.changelog.rev(n), hg.hex(n))) |
|
155 |
||
232
fc4a6e5b5812
hg resolve: merge a given node into the working directory
mpm@selenic.com
parents:
230
diff
changeset
|
156 |
def resolve(ui, repo, node = None): |
fc4a6e5b5812
hg resolve: merge a given node into the working directory
mpm@selenic.com
parents:
230
diff
changeset
|
157 |
'''merge a given node or the current tip into the working dir''' |
fc4a6e5b5812
hg resolve: merge a given node into the working directory
mpm@selenic.com
parents:
230
diff
changeset
|
158 |
if not node: |
fc4a6e5b5812
hg resolve: merge a given node into the working directory
mpm@selenic.com
parents:
230
diff
changeset
|
159 |
node = repo.changelog.tip() |
fc4a6e5b5812
hg resolve: merge a given node into the working directory
mpm@selenic.com
parents:
230
diff
changeset
|
160 |
else: |
fc4a6e5b5812
hg resolve: merge a given node into the working directory
mpm@selenic.com
parents:
230
diff
changeset
|
161 |
node = repo.lookup(node) |
fc4a6e5b5812
hg resolve: merge a given node into the working directory
mpm@selenic.com
parents:
230
diff
changeset
|
162 |
repo.resolve(node) |
fc4a6e5b5812
hg resolve: merge a given node into the working directory
mpm@selenic.com
parents:
230
diff
changeset
|
163 |
|
213 | 164 |
def status(ui, repo): |
165 |
'''show changed files in the working directory |
|
166 |
||
167 |
C = changed |
|
168 |
A = added |
|
169 |
R = removed |
|
170 |
? = not tracked''' |
|
230 | 171 |
(c, a, d, u) = repo.diffdir(repo.root) |
220 | 172 |
(c, a, d, u) = map(lambda x: relfilter(repo, x), (c, a, d, u)) |
213 | 173 |
|
174 |
for f in c: print "C", f |
|
220 | 175 |
for f in a: print "A", f |
213 | 176 |
for f in d: print "R", f |
220 | 177 |
for f in u: print "?", f |
213 | 178 |
|
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
179 |
def undo(ui, repo): |
210 | 180 |
repo.undo() |
181 |
||
209 | 182 |
table = { |
183 |
"init": (init, [], 'hg init'), |
|
213 | 184 |
"branch|clone": (branch, [], 'hg branch [path]'), |
221 | 185 |
"heads": (heads, [], 'hg heads'), |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
186 |
"help": (help, [], 'hg help [command]'), |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
187 |
"checkout|co": (checkout, [], 'hg checkout [changeset]'), |
209 | 188 |
"ann|annotate": (annotate, |
189 |
[('r', 'revision', '', 'revision'), |
|
190 |
('u', 'user', None, 'show user'), |
|
191 |
('n', 'number', None, 'show revision number'), |
|
192 |
('c', 'changeset', None, 'show changeset')], |
|
193 |
'hg annotate [-u] [-c] [-n] [-r id] [files]'), |
|
227 | 194 |
"parents": (parents, [], 'hg parents [node]'), |
232
fc4a6e5b5812
hg resolve: merge a given node into the working directory
mpm@selenic.com
parents:
230
diff
changeset
|
195 |
"resolve": (resolve, [], 'hg resolve [node]'), |
213 | 196 |
"status": (status, [], 'hg status'), |
210 | 197 |
"undo": (undo, [], 'hg undo'), |
209 | 198 |
} |
199 |
||
200 |
norepo = "init branch help" |
|
201 |
||
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
202 |
def find(cmd): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
203 |
i = None |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
204 |
for e in table.keys(): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
205 |
if re.match(e + "$", cmd): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
206 |
return table[e] |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
207 |
|
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
208 |
raise UnknownCommand(cmd) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
209 |
|
214 | 210 |
class SignalInterrupt(Exception): pass |
211 |
||
212 |
def catchterm(*args): |
|
213 |
raise SignalInterrupt |
|
214 |
||
209 | 215 |
def dispatch(args): |
216 |
options = {} |
|
217 |
opts = [('v', 'verbose', None, 'verbose'), |
|
218 |
('d', 'debug', None, 'debug'), |
|
219 |
('q', 'quiet', None, 'quiet'), |
|
220 |
('y', 'noninteractive', None, 'run non-interactively'), |
|
221 |
] |
|
222 |
||
223 |
args = fancyopts.fancyopts(args, opts, options, |
|
224 |
'hg [options] <command> [options] [files]') |
|
225 |
||
226 |
if not args: |
|
227 |
cmd = "help" |
|
228 |
else: |
|
229 |
cmd, args = args[0], args[1:] |
|
230 |
||
231 |
u = ui.ui(options["verbose"], options["debug"], options["quiet"], |
|
232 |
not options["noninteractive"]) |
|
233 |
||
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
234 |
# deal with unfound commands later |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
235 |
i = find(cmd) |
209 | 236 |
|
214 | 237 |
signal.signal(signal.SIGTERM, catchterm) |
238 |
||
209 | 239 |
cmdoptions = {} |
240 |
args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2]) |
|
241 |
||
242 |
if cmd not in norepo.split(): |
|
243 |
repo = hg.repository(ui = u) |
|
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
244 |
d = lambda: i[0](u, repo, *args, **cmdoptions) |
209 | 245 |
else: |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
246 |
d = lambda: i[0](u, *args, **cmdoptions) |
209 | 247 |
|
248 |
try: |
|
249 |
d() |
|
214 | 250 |
except SignalInterrupt: |
251 |
u.warn("killed!\n") |
|
209 | 252 |
except KeyboardInterrupt: |
253 |
u.warn("interrupted!\n") |
|
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
254 |
except TypeError, inst: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
255 |
# was this an argument error? |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
256 |
tb = traceback.extract_tb(sys.exc_info()[2]) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
257 |
if len(tb) > 2: # no |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
258 |
raise |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
259 |
u.warn("%s: invalid arguments\n" % i[0].__name__) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
260 |
u.warn("syntax: %s\n" % i[2]) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
261 |
sys.exit(-1) |