Taylor
Taylor

Reputation: 6410

hg: replacing directory contents in one change

With hg, how can I replace the contents of a directory in one change?

More specifically, I have headers for a large C++ library checked into my repo and I'd like to update them (including adding and removing files) to the latest version of the library. And I'd like to do this in one change, rather than first a removal of the old version and then addition of the new, which would break tests and produce unhelpful diffs.

Upvotes: 1

Views: 210

Answers (2)

Harvey
Harvey

Reputation: 5821

Within your mercurial repo:

# Change to parent directory of your vendor directory
cd vendor_dir/..
rm -rf vendor_dir
# unpack/copy/checkout new vendor sources into a new vendor_dir
# Tell mercurial to figure out what changed
# Feel free to play with the similarity percentage and the (-n) do nothing switch
hg addremove --similarity 70 vendor_dir
hg commit vendor_dir

Updated:

  1. Changed to work on the parent directory since hg rm * within vendor_dir misses dot files, if any.
  2. Change from hg rm to rm -rf because I wrongly assumed that addremove after rm would do the right thing. Hint: it doesn't.
  3. Realized that the default similarity of 100% is not appropriate for vendor releases.

Upvotes: 4

Steve Barnes
Steve Barnes

Reputation: 28390

If they are all changed then, (assuming a flat directory structure:

hg remove \path\to\directory\to\replace\*.h
copy \path\to\new\files\*.* \path\to\directory\to\replace\
hg add \path\to\directory\to\replace\*.*
hg commit -m "Library SoAndSo headers replaced"
hg push

The first line says forget all the files on the next commit but then the new ones are added in the same commit - remember only the last but one line actually changes the local copy of the repository and it only becomes public on the last line.

If you do have sub-directories then you can just remove the *.h on the first line, use xcopy or explorer to copy the new directory structure into place and remove the *.* on the 3rd line.

Upvotes: 2

Related Questions