Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a directory structure in Mercurial

I have a single-person single-folder mercurial repository. The directory structure is simple:

P104
  lecture_notes
    files under version control live here  

After a while I realize I want to have two directories within the repository, like this

P104
  lecture_notes
    files under version control live here  (.hg is here)
  homework
    more files under version control

Now, if I'm just trying to add files to the repository, it fails:

br@ymir:~/P104/lecture_notes$ ll ..
total 16
drwxr-xr-x 4 br br 4096 2012-02-02 18:05 ./
drwxr-xr-x 4 br br 4096 2012-02-01 20:46 ../
drwxr-xr-x 2 br br 4096 2012-02-02 17:44 homework/
drwxr-xr-x 4 br br 4096 2012-02-02 18:06 lecture_notes/
br@ymir:~/P104/lecture_notes$ hg add ../homework/hw1_P104.tex 
abort: ../homework/hw1_P104.tex not under root

My first idea was to clone the repo one level up in the directory structure, add files to the clone, and delete the original repo. But even cloning fails:

br@ymir:~/P104/2011/lecture_notes$ hg clone . ..
abort: destination '..' is not empty

So the question is whether there's a Mercurial-ish way of doing this other than creating a clean repository somewhere else and copying files manually?

like image 238
ev-br Avatar asked Feb 02 '12 18:02

ev-br


2 Answers

I like VonC's solution where you don't move the .hg folder. It's a straight forward solution. Here is an alternative where you do move it and so have to rename fewer folders:

  1. Move the .hg folder up to ~/P104

    $ mv ~/P104/lecture_notes/.hg ~/P104
    

    Seen from the perspective of Mercurial, you will have moved all the top-level files into a lecture_notes directory. There will also suddenly be new untracked files in the homework folder.

  2. Let Mercurial figure out the renames:

    $ hg addremove
    

    This will correctly detect that files that were top-level files before now live in the lecture_notes directory. The untracked (and un-ignored) files in homework are just added.

  3. Save the changes:

    $ hg commit
    

The overall "trick" is that files in the working copy are seen relative to the .hg directory. So by moving the .hg directory up in the file system hierarchy, we effectively move the working copy files down in the hierarchy inside the working copy.

like image 111
Martin Geisler Avatar answered Sep 21 '22 03:09

Martin Geisler


If the root directory of your Mercurial repo is under ~/P104/lecture_notes, I would rather:

  1. rename lecture_notes into P104
  2. make a subdirectory lecture_notes
  3. move all the existing files in that new sub-directory
  4. add homework and its files in the renamed P104 directory
  5. hg add everything

The idea is to keep the .hg repo where it is (~/P104/lecture_notes renamed into ~/P104/P104) and reorganize the files within that renamed directory.
No need to clone.

like image 27
VonC Avatar answered Sep 17 '22 03:09

VonC