How to automatically remove all .orig files in Mercurial working tree?
Solution 1
Personally, I use
$ rm **/*.orig
if I get tired of the .orig
files. This works in Zsh and in Bash 4 after you run shopt -s globstar
.
But if you use another shell or want a built-in solution, then maybe you'll like the purge extension (link updated 2016-08-25). That lets you remove all untracked files with
$ hg purge
You can remove all untracked and ignored files with
$ hg purge --all
An advantage of using hg purge
is that this will also cleanup directories that become empty after removing the files. The rm
command line will just leave the empty directories behind.
Solution 2
btw. find
utility has an action -delete
so you can type only:
find <path-to-files> -name '*.orig' -delete
Solution 3
If you just want to delete the .orig files, and you happen to be on a Windows computer, the following seems to work well:
D:\workspace>hg purge -I **/*.orig --all
This will delete all untracked files that end in .orig, but won't delete other untracked files, as the other answers would.
You can test this before running it by putting the --print
flag in as well.
Solution 4
The following will remove .orig files in your entire working copy hierarchy and also works for paths containing spaces:
find . -name *.orig | while read -d $'\n' file; do rm -v "$file"; done;
I use an alias in my .bash_profile:
alias clearorig='echo "Removing .orig files..."; find . -name *.orig | \
while read -d $'\''\n'\'' file; do rm -v "$file"; done;'
Solution 5
I'm working in Powershell and didn't see the answer here:
# NOTE: be in the root of your repository
# fetch all .orig files recursively
$orig = (dir *.orig -recurse) ;
# remove each .orig file
foreach ($item in $orig) { del $($item.FullName) ; }
# afterwards I make sure to remove the references to the .orig files in the repo
# then commit & push
hg addremove ;
hg commit -m "remove .orig" ;
hg push ;
rkj
Updated on May 04, 2020Comments
-
rkj about 4 years
During merges mercurial leaves .orig file for any unresolved file. But after manually resolving problems and marking a file correct it does not delete the .orig file. Can it be automatically removed by some command?
I work on a Mac so I can use something like:
find . -iname '*.orig' -exec rm '{}' ';'
and alias it or something, but I'd rather use something like hg cleanup...
UPDATE:
Since some time now, Purge extension is bundled with Mercurial and solves this problem nicely.
-
rkj almost 15 yearsrm is quite short :). But the purge removes all untracked files - and some file I use are not tracked on purpose - private project configuration, database configuration, etc...
-
rkj almost 15 yearscan you be more more specific? Are you proposing removing all files on commit hook? :)
-
dfa almost 15 yearsyou can execute find . -iname '.orig' -exec rm '{}' ';' *after a merge, see the doc for details
-
Martin Geisler over 12 yearsWhy am I getting downvotes for this? Is it because this only works in shells like Zsh that understand
**
? I said personally since this is what I use myself, and then I gave a general, cross-platform answer in the form of the purge extension. -
Martin Geisler over 12 yearsThis is not a good answer: file names with spaces will break the
rm
invocation. Please let me know if you fix the answer so that I can remove the down vote. -
Benbob about 12 years@Martin It doesn't recurse into sub-directories is my guess.
-
Martin Geisler about 12 years@Keyo: the
rm
command line does recurse, that's the purpose of the**
part. It wont remove directories that become empty after removing the.orig
files. That's a slight advantage of usinghg purge
. It's rare that directories come and go in my projects, so I hadn't thought of this before. -
Gary almost 11 yearsYou need to add double quotes "" around the variable $f. This is a common problem in a lot of scripts that operate on files and directories, including Apple documentation (which I notified them to update years ago and still has incompatible script examples) :P
-
Collin Klopfenstein over 10 years@MartinGeisler fixed! :)
-
Martin Geisler over 10 years@CollinKlopfenstein: Imagine you have files named
foo.txt
andbar baz.txt
. Thefind
call will returnfoo.txt\nbar baz.txt\n
and the for loop will split this into three files:foo.txt
,bar
, andbaz.txt
. Only the first name is correct. This is why you needfind -print0
or should letfind
executerm
. -
miguelSantirso over 10 yearsI agree this is the best option if you have purge installed
-
Mecki over 10 yearsHow is that piping through several processes any better than
find . -name '*.orig' -exec rm '{}' ';'
or even simplerfind . -name '*.orig' -delete
, where all work is performed byfind
andrm
directly or just byfind
on its own? -
Mecki over 10 yearsHow is that piping through several processes any better than
find . -name '*.orig' -exec rm '{}' ';'
or even simplerfind . -name '*.orig' -delete
, where all work is performed byfind
andrm
directly or just byfind
on its own? -
mlissner about 10 yearsCorrect me if I'm wrong here, but won't this have all the usual issues that you have when you send output to xargs without using the
-0
flags? From the man page: "-0 Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode."
-
mlissner about 10 years@Mecki, pretty sure it's not, and that it's prone to errors when files contain weird characters...like newlines, for example.
-
Gary about 10 years@mlissner, you are correct. In my case, we typically didn't put spaces or newlines in filenames, so for the general case this solution above wouldn't work.