[PATCH 1 of 3] Add a forget command for easily untracking files

Steve Losh steve at stevelosh.com
Sun May 31 13:38:49 CDT 2009


# HG changeset patch
# User Steve Losh <steve at stevelosh.com>
# Date 1243753740 14400
# Node ID 67117fbecae7ce616bcc56fe95355b68ed925c06
# Parent  472ecf2c55d9eae8fa4246a3a8043aa2c098251e
Add a forget command for easily untracking files.

This command does exactly what 'hg remove -Af [FILES]' does.

The reason for creating a new command is that the options for 'hg remove'
are confusing (-A removes only deleted files, -f forces deletion, and using
both means *the exact opposite of both*).

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1125,6 +1125,44 @@
                  switch_parent=opts.get('switch_parent'),
                  opts=patch.diffopts(ui, opts))
 
+def forget(ui, repo, *pats, **opts):
+    """forget the specified files on the next commit
+
+    Schedule the indicated files for removal from the repository.
+
+    This only removes files from the current branch, not from the
+    entire project history, and it does not delete them from the
+    working directory.
+
+    This command schedules the files to be removed at the next commit.
+    To undo a remove before that, see hg revert.
+    """
+
+    if not pats:
+        raise util.Abort(_('no files specified'))
+
+    m = cmdutil.match(repo, pats, opts)
+    s = repo.status(match=m, clean=True)
+    modified, added, deleted, clean = s[0], s[1], s[3], s[6]
+    forget = modified + added + deleted + clean
+
+    for f in m.files():
+        if f not in repo.dirstate and not os.path.isdir(m.rel(f)):
+            ui.warn(_('not removing %s: file is already untracked\n')
+                    % m.rel(f))
+
+    def warn(files, reason):
+        for f in files:
+            ui.warn(_('not removing %s: file %s (use -f to force removal)\n')
+                    % (m.rel(f), reason))
+
+    for f in forget:
+        if ui.verbose or not m.exact(f):
+            ui.status(_('removing %s\n') % m.rel(f))
+
+    repo.forget(added)
+    repo.remove(modified + deleted + clean, unlink=False)
+
 def grep(ui, repo, pattern, *pats, **opts):
     """search for a pattern in specified files and revisions
 
@@ -3220,6 +3258,10 @@
           ('', 'switch-parent', None, _('diff against the second parent'))
           ] + diffopts,
          _('[OPTION]... [-o OUTFILESPEC] REV...')),
+    "^forget|untrack":
+        (forget,
+         [] + walkopts,
+         _('[OPTION]... FILE...')),
     "grep":
         (grep,
          [('0', 'print0', None, _('end fields with NUL')),


More information about the Mercurial-devel mailing list