19 |
19 |
20 from mercurial import hg, util |
20 from mercurial import hg, util |
21 from mercurial.i18n import _ |
21 from mercurial.i18n import _ |
22 import os |
22 import os |
23 |
23 |
24 class Purge(object): |
24 def dopurge(ui, repo, dirs=None, act=True, abort_on_err=False, eol='\n'): |
25 def __init__(self): |
25 def error(msg): |
26 pass |
26 if abort_on_err: |
|
27 raise util.Abort(msg) |
|
28 else: |
|
29 ui.warn(_('warning: %s\n') % msg) |
27 |
30 |
28 def purge(self, ui, repo, dirs=None, act=True, abort_on_err=False, eol='\n'): |
31 def remove(remove_func, name): |
29 def error(msg): |
32 if act: |
30 if abort_on_err: |
33 try: |
31 raise util.Abort(msg) |
34 remove_func(os.path.join(repo.root, name)) |
32 else: |
35 except OSError, e: |
33 ui.warn(_('warning: %s\n') % msg) |
36 error(_('%s cannot be removed') % name) |
|
37 else: |
|
38 ui.write('%s%s' % (name, eol)) |
34 |
39 |
35 def remove(remove_func, name): |
40 directories = [] |
36 if act: |
41 files = [] |
37 try: |
42 for src, f, st in repo.dirstate.statwalk(files=dirs, ignored=True, |
38 remove_func(os.path.join(repo.root, name)) |
43 directories=True): |
39 except OSError, e: |
44 if src == 'd': |
40 error(_('%s cannot be removed') % name) |
45 directories.append(f) |
41 else: |
46 elif src == 'f' and f not in repo.dirstate: |
42 ui.write('%s%s' % (name, eol)) |
47 files.append(f) |
43 |
48 |
44 directories = [] |
49 directories.sort() |
45 files = [] |
|
46 for src, f, st in repo.dirstate.statwalk(files=dirs, ignored=True, |
|
47 directories=True): |
|
48 if src == 'd': |
|
49 directories.append(f) |
|
50 elif src == 'f' and f not in repo.dirstate: |
|
51 files.append(f) |
|
52 |
50 |
53 directories.sort() |
51 for f in files: |
|
52 if f not in repo.dirstate: |
|
53 ui.note(_('Removing file %s\n') % f) |
|
54 remove(os.remove, f) |
54 |
55 |
55 for f in files: |
56 for f in directories[::-1]: |
56 if f not in repo.dirstate: |
57 if not os.listdir(repo.wjoin(f)): |
57 ui.note(_('Removing file %s\n') % f) |
58 ui.note(_('Removing directory %s\n') % f) |
58 remove(os.remove, f) |
59 remove(os.rmdir, f) |
59 |
|
60 for f in directories[::-1]: |
|
61 if not os.listdir(repo.wjoin(f)): |
|
62 ui.note(_('Removing directory %s\n') % f) |
|
63 remove(os.rmdir, f) |
|
64 |
60 |
65 |
61 |
66 def purge(ui, repo, *dirs, **opts): |
62 def purge(ui, repo, *dirs, **opts): |
67 '''removes files not tracked by mercurial |
63 '''removes files not tracked by mercurial |
68 |
64 |
91 abort_on_err = bool(opts['abort_on_err']) |
87 abort_on_err = bool(opts['abort_on_err']) |
92 eol = opts['print0'] and '\0' or '\n' |
88 eol = opts['print0'] and '\0' or '\n' |
93 if eol == '\0': |
89 if eol == '\0': |
94 # --print0 implies --print |
90 # --print0 implies --print |
95 act = False |
91 act = False |
96 p = Purge() |
92 dopurge(ui, repo, dirs, act, abort_on_err, eol) |
97 p.purge(ui, repo, dirs, act, abort_on_err, eol) |
|
98 |
93 |
99 |
94 |
100 cmdtable = { |
95 cmdtable = { |
101 'purge': |
96 'purge': |
102 (purge, |
97 (purge, |