Skip to content

Instantly share code, notes, and snippets.

@mattip
Last active December 28, 2023 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattip/b6752c164a075c2aa53f4069e9c30573 to your computer and use it in GitHub Desktop.
Save mattip/b6752c164a075c2aa53f4069e9c30573 to your computer and use it in GitHub Desktop.
Converting hg repo to git, with notes for each node which branch it came from

Cloning the hg repo as a git repo

the tools

Get this repo https://github.com/mnauw/git-remote-hg which is a fork of the original and supports modern git/hg. Copy git-hg-helper and git-remote-hg to ~/bin and make them executable with chmod +x ~/bin/git*

clone the repo as both hg and git

git clone hg::https://foss.heptapod.net/pypy/cffi cffi-git hg clone https://foss.heptapod.net/pypy/cffi cffi-hg

prepare a file with all the hg hashes and the branches they are on

Find out how many commits there are (assume the last commit was to default) export top=$(cd cffi-hg; hg id -r default -n) Then prepare the file. The idea is:

  • get the hg hash for the commit $(cd ../cffi-hg; hg log -r $i -T"{node}\n")
  • convert it to a git hash $(cd cffi-git; git-hg-helper git-rev ...
  • echo that without a newline echo -n ...
  • next, echo a space and then the hg branch for the commit
       for (( i=0; i<=$top; i++)); 
           do echo -n $(cd cffi-git; git-hg-helper git-rev $(cd ../cffi-hg; hg log -r $i -T"{node}\n"));
              echo " $(cd cffi-hg; hg log -r $i -T'{branch}\n')";
       done >> hash-branch.txt
    

add a note with the branch to the git repo

Iterate through the file, calling git checkout -q <hash> && git notes --ref refs/notes/branch add -m branch:<branch>

(cd cffi-git; $(awk -F" " '{print "git checkout -q " $1 " git notes --ref refs/notes/branch add -m branch:"$2}' hash-branch.txt))

Pushing to github

I couldn't get git push --all to work, it only pushed the local head. So I needed to push each branch separately.

  • create a file of all the branches: $(cd cffi-hg; hg branches | cut -f1 -d" " > branches.txt)
  • for each branch, check it out and then push to the remote
    while read branch; do git checkout branches/$branch && git push origin branches/$branch; done < branches.txt
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment