[PATCH] purge: add --ignored-only/-i option to delete only ignored files

9 views
Skip to first unread message

Greg Ward

unread,
Mar 20, 2012, 9:50:51 PM3/20/12
to mercuri...@selenic.com
# HG changeset patch
# User Greg Ward <gr...@gerg.ca>
# Date 1332294424 14400
# Node ID bfde5bbbc3bb0d804a70cad737f0bf95086a448c
# Parent 12e3f93b1cbc7c930e941f0adfe632c8ad70b73d
purge: add --ignored-only/-i option to delete only ignored files

This is handy when you trust .hgignore to specify all of your build
products, but you do not trust yourself to always remember to "hg add"
new files. This treats unknown files as valuable, since they might
become added files soon.

diff --git a/hgext/purge.py b/hgext/purge.py
--- a/hgext/purge.py
+++ b/hgext/purge.py
@@ -34,6 +34,7 @@
@command('purge|clean',
[('a', 'abort-on-err', None, _('abort if an error occurs')),
('', 'all', None, _('purge ignored files too')),
+ ('i', 'ignored-only', None, _('purge only ignored files (not unknown)')),
('p', 'print', None, _('print filenames instead of deleting them')),
('0', 'print0', None, _('end filenames with NUL, for use with xargs'
' (implies -p/--print)')),
@@ -45,7 +46,7 @@
Delete files not known to Mercurial. This is useful to test local
and uncommitted changes in an otherwise-clean source tree.

- This means that purge will delete:
+ By default, purge will delete:

- Unknown files: files marked with "?" by :hg:`status`
- Empty directories: in fact Mercurial ignores directories unless
@@ -57,6 +58,11 @@
- Ignored files (unless --all is specified)
- New files added to the repository (with :hg:`add`)

+ With `--all`, purge will remove both unknown and ignored files.
+
+ With `--ignored-only`, purge will instead remove only ignored
+ files, leaving unknown files alone.
+
If directories are given on the command line, only files in these
directories are considered.

@@ -97,7 +103,9 @@
directories = []
match = scmutil.match(repo[None], dirs, opts)
match.dir = directories.append
- status = repo.status(match=match, ignored=opts['all'], unknown=True)
+ ignored = (opts['all'] or opts['ignored_only'])
+ unknown = not opts['ignored_only']
+ status = repo.status(match=match, ignored=ignored, unknown=unknown)

for f in sorted(status[4] + status[5]):
ui.note(_('Removing file %s\n') % f)
diff --git a/tests/test-purge.t b/tests/test-purge.t
--- a/tests/test-purge.t
+++ b/tests/test-purge.t
@@ -136,6 +136,35 @@
directory
r1

+delete only ignored files
+ $ touch ignored untracked directory/untracked
+ $ hg purge -p --ignored-only
+ ignored
+ $ hg purge -v --ignored-only
+ Removing file ignored
+ $ ls
+ directory
+ r1
+ untracked
+ $ hg status -ui
+ ? directory/untracked
+ ? untracked
+
+delete only files in an ignored directory
+ $ mkdir ignored
+ $ touch ignored/stuff1 ignored/stuff2
+ $ hg purge -p -i
+ ignored/stuff1
+ ignored/stuff2
+ $ hg purge -v -i
+ Removing file ignored/stuff1
+ Removing file ignored/stuff2
+ Removing directory ignored
+ $ hg status -ui
+ ? directory/untracked
+ ? untracked
+ $ hg purge
+
abort with missing files until we support name mangling filesystems

$ touch untracked_file
_______________________________________________
Mercurial-devel mailing list
Mercuri...@selenic.com
http://selenic.com/mailman/listinfo/mercurial-devel

Matt Mackall

unread,
Mar 21, 2012, 12:31:19 PM3/21/12
to Greg Ward, mercuri...@selenic.com
On Tue, 2012-03-20 at 21:50 -0400, Greg Ward wrote:
> # HG changeset patch
> # User Greg Ward <gr...@gerg.ca>
> # Date 1332294424 14400
> # Node ID bfde5bbbc3bb0d804a70cad737f0bf95086a448c
> # Parent 12e3f93b1cbc7c930e941f0adfe632c8ad70b73d
> purge: add --ignored-only/-i option to delete only ignored files
>
> This is handy when you trust .hgignore to specify all of your build
> products, but you do not trust yourself to always remember to "hg add"
> new files. This treats unknown files as valuable, since they might
> become added files soon.

Does this work?

hg purge "set:ignored()"

If so, can we add this as an example for "hg help purge -v" instead?

I'm trying to prevent a combinatoric explosion of command-specific
options by adding general-purpose features like revsets and filesets.


--
Mathematics is the supreme nostalgia of our time.

Pierre-Yves David

unread,
Mar 22, 2012, 4:36:42 AM3/22/12
to Greg Ward, mercuri...@selenic.com
On Tue, Mar 20, 2012 at 09:50:51PM -0400, Greg Ward wrote:
> # HG changeset patch
> # User Greg Ward <gr...@gerg.ca>
> # Date 1332294424 14400
> # Node ID bfde5bbbc3bb0d804a70cad737f0bf95086a448c
> # Parent 12e3f93b1cbc7c930e941f0adfe632c8ad70b73d
> purge: add --ignored-only/-i option to delete only ignored files
>
> This is handy when you trust .hgignore to specify all of your build
> products, but you do not trust yourself to always remember to "hg add"
> new files. This treats unknown files as valuable, since they might
> become added files soon.

-i is a terrible short name as it sound like "--interactive" an option that
some people would expect to ask confirmation before removing files (as `rm -i`
do)

I met such user yesterday.


--
Pierre-Yves David

http://www.logilab.fr/

signature.asc

Greg Ward

unread,
Mar 22, 2012, 8:55:43 AM3/22/12
to Pierre-Yves David, mercuri...@selenic.com

Good point. Kinda like "hg strip -n" is not a dry run.

I'll drop the short option when (if) I resend.

Greg
--
Greg Ward http://www.gerg.ca/
"What do you mean -- a European or an African swallow?"

Kevin Bullock

unread,
Mar 22, 2012, 2:46:18 PM3/22/12
to Greg Ward, mercuri...@selenic.com
On Mar 21, 2012, at 8:49 PM, Greg Ward wrote:

On 21 March 2012, Matt Mackall said:
On Tue, 2012-03-20 at 21:50 -0400, Greg Ward wrote:
# HG changeset patch
# User Greg Ward <gr...@gerg.ca>
# Date 1332294424 14400
# Node ID bfde5bbbc3bb0d804a70cad737f0bf95086a448c
# Parent  12e3f93b1cbc7c930e941f0adfe632c8ad70b73d
purge: add --ignored-only/-i option to delete only ignored files

This is handy when you trust .hgignore to specify all of your build
products, but you do not trust yourself to always remember to "hg add"
new files. This treats unknown files as valuable, since they might
become added files soon.

Does this work?

hg purge "set:ignored()"

No, but

 hg purge --all "set:ignored()"

does. purge only considers ignored files with --all.

Also, this does not remove empty directories.

However, this totally fails the usability test. I can just see me
trying to explain to one of my co-workers how to clean up a messy
working dir.

FWIW, my recommended usage of the purge extension is always:

hg --config extensions.purge= purge [whatever]

I consider it my --yes-ive-thought-through-what-im-purging option. :) It also does not lend itself to finger-macro'ing, a nice safety feature.

Doesn't solve the "--all 'set:ignored()'" issue though.

pacem in terris / мир / शान्ति / ‎‫سَلاَم‬ / 平和
Kevin R. Bullock

Matt Mackall

unread,
Mar 22, 2012, 3:22:12 PM3/22/12
to Greg Ward, mercuri...@selenic.com
On Wed, 2012-03-21 at 21:49 -0400, Greg Ward wrote:
> On 21 March 2012, Matt Mackall said:
> > On Tue, 2012-03-20 at 21:50 -0400, Greg Ward wrote:
> > > # HG changeset patch
> > > # User Greg Ward <gr...@gerg.ca>
> > > # Date 1332294424 14400
> > > # Node ID bfde5bbbc3bb0d804a70cad737f0bf95086a448c
> > > # Parent 12e3f93b1cbc7c930e941f0adfe632c8ad70b73d
> > > purge: add --ignored-only/-i option to delete only ignored files
> > >
> > > This is handy when you trust .hgignore to specify all of your build
> > > products, but you do not trust yourself to always remember to "hg add"
> > > new files. This treats unknown files as valuable, since they might
> > > become added files soon.

Ignored files may also be valuable. Config files are a common example of
things you never want to commit, but that you don't want Mercurial to
clobber. I think maybe half of projects have one or more of these.

> > Does this work?
> >
> > hg purge "set:ignored()"
>

> No, but
>
> hg purge --all "set:ignored()"

Hmm, perhaps we should fix that.

> Also, this does not remove empty directories.

Seems buggy.

> However, this totally fails the usability test. I can just see me
> trying to explain to one of my co-workers how to clean up a messy
> working dir.

The easy/safe way, of course, is 'hg clone'.

Reply all
Reply to author
Forward
0 new messages