First run at a remove option allowing file deletion
Chad Netzer
cnetzer at comcast.net
Mon Jul 11 00:15:36 UTC 2005
I cooked up this patch, mainly for discussion. If you give a 'force'
option to remove, it will delete the file before completing the remove,
but only if the file is under HG control, and unmodified.
I'm not particularly fond of the 'force' designation (-f), but 'delete'
would become (-d), and that is used as the date option elsewhere and I
wouldn't want them to be confused.
Here is a capture of a session using the option:
% mkdir temp
% cd temp
% hg init
% echo "add and do not modify" >> foo1
% echo "add and modify later" >> foo2
% echo "add and manually remove later" >> foo3
% echo "add and force remove later" >> foo4
% hg addremove
% hg commit
% rm foo3
% echo "add and do not commit" >> foo5
% hg add foo5
% echo "do not add" >> foo6
% echo "modified" >> foo2
% hg status
C foo2
A foo5
R foo3
? foo6
%
% hg remove -f foo2 foo6 # Each of these an error
foo2 has changed since last commit
foo6 is not tracked by hg
abort: not able to force file removal due to errors
%
% hg remove -f foo3 # WON'T attempt deletion, just remove
% hg remove -f foo1 # should delete and remove
Deleted file: foo1
%
% ls foo1
ls: foo1: No such file or directory
%
% hg status # foo1 and foo3 removed, foo2 changed
C foo2
A foo5
R foo1
R foo3
? foo6
And the patch itself:
# HG changeset patch
# User chad dot netzer at gmail dot com
# Node ID f9d1c64834eb4778c67ea5295a60a4566e15498b
# Parent 3662e3d6b6906bd28a1127386c0c96b44ed50b13
Support deletion of files as option to remove command.
diff -r 3662e3d6b690 -r f9d1c64834eb mercurial/commands.py
--- a/mercurial/commands.py Sun Jul 10 22:06:30 2005
+++ b/mercurial/commands.py Sun Jul 10 23:57:05 2005
@@ -787,8 +787,37 @@
"""roll back an interrupted transaction"""
repo.recover()
-def remove(ui, repo, file, *files):
+def remove(ui, repo, file, *files, **opts):
"""remove the specified files on the next commit"""
+
+ if opts['force']:
+ (c, a, d, u) = repo.changes(None, None)
+ (c, a, d, u) = map(lambda x: relfilter(repo, x), (c, a, d, u))
+
+ force_err = False
+ force_remove_files = []
+ for f in (file,) + files:
+ if f in c:
+ ui.warn("%s has changed since last commit\n" % f)
+ force_err = True
+ elif f in u:
+ ui.warn("%s is not tracked by hg\n" % f)
+ force_err = True
+ elif f in d:
+ pass # Do not attempt to delete already removed files
+ else:
+ # All others are candidates for os.remove()
+ force_remove_files.append(f)
+
+ if force_err:
+ ui.warn('abort: not able to force file removal due to
errors\n')
+ return -1 # Is this respected? Or should we sys.exit()?
+ else:
+ # Must we use relpath here, or the unmodified filenames?
+ for f in relpath(repo, force_remove_files):
+ os.remove(f)
+ ui.warn("Deleted file: %s\n" % f)
+
repo.remove(relpath(repo, (file,) + files))
def revert(ui, repo, *names, **opts):
@@ -1099,7 +1128,9 @@
('l', 'logfile', "", 'commit text file')],
'hg rawcommit [options] [files]'),
"recover": (recover, [], "hg recover"),
- "^remove|rm": (remove, [], "hg remove [files]"),
+ "^remove|rm": (remove,
+ [('f', 'force', None, 'delete immediately if
unmodified')],
+ "hg remove [options] [files]"),
"^revert": (revert,
[("n", "nonrecursive", None, "don't recurse into
subdirs"),
("r", "rev", "", "revision")],
More information about the Mercurial
mailing list