Show More
@@ -1,109 +1,109 b'' | |||||
1 | # commands.py - command processing for mercurial |
|
1 | # commands.py - command processing for mercurial | |
2 | # |
|
2 | # | |
3 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
|
3 | # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms |
|
5 | # This software may be used and distributed according to the terms | |
6 | # of the GNU General Public License, incorporated herein by reference. |
|
6 | # of the GNU General Public License, incorporated herein by reference. | |
7 |
|
7 | |||
8 | from demandload import demandload |
|
8 | from demandload import demandload | |
9 | from node import * |
|
9 | from node import * | |
10 | from i18n import gettext as _ |
|
10 | from i18n import gettext as _ | |
11 | demandload(globals(), 'util') |
|
11 | demandload(globals(), 'util') | |
12 | demandload(globals(), 'os sys') |
|
12 | demandload(globals(), 'os sys') | |
13 |
|
13 | |||
14 | def make_filename(repo, pat, node, |
|
14 | def make_filename(repo, pat, node, | |
15 | total=None, seqno=None, revwidth=None, pathname=None): |
|
15 | total=None, seqno=None, revwidth=None, pathname=None): | |
16 | node_expander = { |
|
16 | node_expander = { | |
17 | 'H': lambda: hex(node), |
|
17 | 'H': lambda: hex(node), | |
18 | 'R': lambda: str(repo.changelog.rev(node)), |
|
18 | 'R': lambda: str(repo.changelog.rev(node)), | |
19 | 'h': lambda: short(node), |
|
19 | 'h': lambda: short(node), | |
20 | } |
|
20 | } | |
21 | expander = { |
|
21 | expander = { | |
22 | '%': lambda: '%', |
|
22 | '%': lambda: '%', | |
23 | 'b': lambda: os.path.basename(repo.root), |
|
23 | 'b': lambda: os.path.basename(repo.root), | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 | try: |
|
26 | try: | |
27 | if node: |
|
27 | if node: | |
28 | expander.update(node_expander) |
|
28 | expander.update(node_expander) | |
29 | if node and revwidth is not None: |
|
29 | if node and revwidth is not None: | |
30 | expander['r'] = (lambda: |
|
30 | expander['r'] = (lambda: | |
31 | str(repo.changelog.rev(node)).zfill(revwidth)) |
|
31 | str(repo.changelog.rev(node)).zfill(revwidth)) | |
32 | if total is not None: |
|
32 | if total is not None: | |
33 | expander['N'] = lambda: str(total) |
|
33 | expander['N'] = lambda: str(total) | |
34 | if seqno is not None: |
|
34 | if seqno is not None: | |
35 | expander['n'] = lambda: str(seqno) |
|
35 | expander['n'] = lambda: str(seqno) | |
36 | if total is not None and seqno is not None: |
|
36 | if total is not None and seqno is not None: | |
37 | expander['n'] = lambda:str(seqno).zfill(len(str(total))) |
|
37 | expander['n'] = lambda:str(seqno).zfill(len(str(total))) | |
38 | if pathname is not None: |
|
38 | if pathname is not None: | |
39 | expander['s'] = lambda: os.path.basename(pathname) |
|
39 | expander['s'] = lambda: os.path.basename(pathname) | |
40 | expander['d'] = lambda: os.path.dirname(pathname) or '.' |
|
40 | expander['d'] = lambda: os.path.dirname(pathname) or '.' | |
41 | expander['p'] = lambda: pathname |
|
41 | expander['p'] = lambda: pathname | |
42 |
|
42 | |||
43 | newname = [] |
|
43 | newname = [] | |
44 | patlen = len(pat) |
|
44 | patlen = len(pat) | |
45 | i = 0 |
|
45 | i = 0 | |
46 | while i < patlen: |
|
46 | while i < patlen: | |
47 | c = pat[i] |
|
47 | c = pat[i] | |
48 | if c == '%': |
|
48 | if c == '%': | |
49 | i += 1 |
|
49 | i += 1 | |
50 | c = pat[i] |
|
50 | c = pat[i] | |
51 | c = expander[c]() |
|
51 | c = expander[c]() | |
52 | newname.append(c) |
|
52 | newname.append(c) | |
53 | i += 1 |
|
53 | i += 1 | |
54 | return ''.join(newname) |
|
54 | return ''.join(newname) | |
55 | except KeyError, inst: |
|
55 | except KeyError, inst: | |
56 | raise util.Abort(_("invalid format spec '%%%s' in output file name"), |
|
56 | raise util.Abort(_("invalid format spec '%%%s' in output file name"), | |
57 | inst.args[0]) |
|
57 | inst.args[0]) | |
58 |
|
58 | |||
59 | def make_file(repo, pat, node=None, |
|
59 | def make_file(repo, pat, node=None, | |
60 | total=None, seqno=None, revwidth=None, mode='wb', pathname=None): |
|
60 | total=None, seqno=None, revwidth=None, mode='wb', pathname=None): | |
61 | if not pat or pat == '-': |
|
61 | if not pat or pat == '-': | |
62 | return 'w' in mode and sys.stdout or sys.stdin |
|
62 | return 'w' in mode and sys.stdout or sys.stdin | |
63 | if hasattr(pat, 'write') and 'w' in mode: |
|
63 | if hasattr(pat, 'write') and 'w' in mode: | |
64 | return pat |
|
64 | return pat | |
65 | if hasattr(pat, 'read') and 'r' in mode: |
|
65 | if hasattr(pat, 'read') and 'r' in mode: | |
66 | return pat |
|
66 | return pat | |
67 | return open(make_filename(repo, pat, node, total, seqno, revwidth, |
|
67 | return open(make_filename(repo, pat, node, total, seqno, revwidth, | |
68 | pathname), |
|
68 | pathname), | |
69 | mode) |
|
69 | mode) | |
70 |
|
70 | |||
71 | def matchpats(repo, pats=[], opts={}, head=''): |
|
71 | def matchpats(repo, pats=[], opts={}, head=''): | |
72 | cwd = repo.getcwd() |
|
72 | cwd = repo.getcwd() | |
73 | if not pats and cwd: |
|
73 | if not pats and cwd: | |
74 | opts['include'] = [os.path.join(cwd, i) for i in opts['include']] |
|
74 | opts['include'] = [os.path.join(cwd, i) for i in opts['include']] | |
75 | opts['exclude'] = [os.path.join(cwd, x) for x in opts['exclude']] |
|
75 | opts['exclude'] = [os.path.join(cwd, x) for x in opts['exclude']] | |
76 | cwd = '' |
|
76 | cwd = '' | |
77 | return util.cmdmatcher(repo.root, cwd, pats or ['.'], opts.get('include'), |
|
77 | return util.cmdmatcher(repo.root, cwd, pats or ['.'], opts.get('include'), | |
78 | opts.get('exclude'), head) |
|
78 | opts.get('exclude'), head) | |
79 |
|
79 | |||
80 | def makewalk(repo, pats, opts, node=None, head='', badmatch=None): |
|
80 | def makewalk(repo, pats=[], opts={}, node=None, head='', badmatch=None): | |
81 | files, matchfn, anypats = matchpats(repo, pats, opts, head) |
|
81 | files, matchfn, anypats = matchpats(repo, pats, opts, head) | |
82 | exact = dict(zip(files, files)) |
|
82 | exact = dict(zip(files, files)) | |
83 | def walk(): |
|
83 | def walk(): | |
84 | for src, fn in repo.walk(node=node, files=files, match=matchfn, |
|
84 | for src, fn in repo.walk(node=node, files=files, match=matchfn, | |
85 | badmatch=badmatch): |
|
85 | badmatch=badmatch): | |
86 | yield src, fn, util.pathto(repo.getcwd(), fn), fn in exact |
|
86 | yield src, fn, util.pathto(repo.getcwd(), fn), fn in exact | |
87 | return files, matchfn, walk() |
|
87 | return files, matchfn, walk() | |
88 |
|
88 | |||
89 | def walk(repo, pats, opts, node=None, head='', badmatch=None): |
|
89 | def walk(repo, pats=[], opts={}, node=None, head='', badmatch=None): | |
90 | files, matchfn, results = makewalk(repo, pats, opts, node, head, badmatch) |
|
90 | files, matchfn, results = makewalk(repo, pats, opts, node, head, badmatch) | |
91 | for r in results: |
|
91 | for r in results: | |
92 | yield r |
|
92 | yield r | |
93 |
|
93 | |||
94 | def addremove(repo, pats, opts={}, wlock=None, dry_run=None): |
|
94 | def addremove(repo, pats=[], opts={}, wlock=None, dry_run=None): | |
95 | if dry_run is None: |
|
95 | if dry_run is None: | |
96 | dry_run = opts.get('dry_run') |
|
96 | dry_run = opts.get('dry_run') | |
97 | add, remove = [], [] |
|
97 | add, remove = [], [] | |
98 | for src, abs, rel, exact in walk(repo, pats, opts): |
|
98 | for src, abs, rel, exact in walk(repo, pats, opts): | |
99 | if src == 'f' and repo.dirstate.state(abs) == '?': |
|
99 | if src == 'f' and repo.dirstate.state(abs) == '?': | |
100 | add.append(abs) |
|
100 | add.append(abs) | |
101 | if repo.ui.verbose or not exact: |
|
101 | if repo.ui.verbose or not exact: | |
102 | repo.ui.status(_('adding %s\n') % ((pats and rel) or abs)) |
|
102 | repo.ui.status(_('adding %s\n') % ((pats and rel) or abs)) | |
103 | if repo.dirstate.state(abs) != 'r' and not os.path.exists(rel): |
|
103 | if repo.dirstate.state(abs) != 'r' and not os.path.exists(rel): | |
104 | remove.append(abs) |
|
104 | remove.append(abs) | |
105 | if repo.ui.verbose or not exact: |
|
105 | if repo.ui.verbose or not exact: | |
106 | repo.ui.status(_('removing %s\n') % ((pats and rel) or abs)) |
|
106 | repo.ui.status(_('removing %s\n') % ((pats and rel) or abs)) | |
107 | if not dry_run: |
|
107 | if not dry_run: | |
108 | repo.add(add, wlock=wlock) |
|
108 | repo.add(add, wlock=wlock) | |
109 | repo.remove(remove, wlock=wlock) |
|
109 | repo.remove(remove, wlock=wlock) |
General Comments 0
You need to be logged in to leave comments.
Login now