migrate Hg to Git via "fast-export" in Windows

6.3k Views Asked by At

I have a project having mercurial HG code source management,and try to migrate it on a VSTS/Git platform.

I read that there is a tool for doing that, "fast-export"... So I tried but not did't achieve to do it working in Windows...

Say I have D:\MyProject containing the .hg directory and the project code files, I

  • downloaded the fast-export files in the "D:\fast-export-master" folder,
  • created a new directory D:\MyProject-git
  • launched in that D:\MyProject-git the git init command (a new .git directory appeared then);
  • now from the same D:\MyProject-git i did
    ..\fast-export-master\hg-fast-export.sh -r "D:\MyProject"

now strange things starts happening, a command line window appeared and disappeared so quickly I didn't understood what happened, so I relaunched the previous command in a hope to see the message... I should try tens of times until able to capture in the printscreen the command-line message, that says strange things:

which: no D:\fast-export-master\hg-fast-export.sh in (/c/Users/xxx/bin:mingw64/bin:/user/local/bin:/usr/bin:/bin/mingw64/bin:/usr/bin:/c/Users/xxx/bin:/c/ProgramData/Oracle/Java/javapath:/c/WINDOWS/system32 [...etc...etc...])

I wonder what is the problem and how to solve it...

EDIT:

Following the @max630 suggestion bellow, I reinstalled Python and update the PATH, so it's recognized in the command

> python
Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

opened the "GIT Bash" from the start menu then

cd "/D/MyProject-git"
../fast-export-master/hg-fast-export.sh -r "D:\MyProject" --force

that gave:

../fast-export-master/hg-fast-export.sh: line 175: python: command not found
C:\Program Files\Git\mingw64\libexec\git-core\git-fast-import.exe statistics:
---------------------------------------------------------------------
Objects allocated:       5000
...

enter image description here

6

There are 6 best solutions below

1
On BEST ANSWER

I think your concern is that you imported the "hg-fast-export" into a directory that was not added to your environment variables, according to the error message "D:\fast-export-master\hg-fast-export.sh" was not found in the list of your environment variables, so try to add th following "D:\fast-export-master\"in your environment variables and then try again.

If it does not work out yet, look at this article and follow step by step what was said:

https://www.appveyor.com/blog/2014/02/23/converting-mercurial-repository-to-git-on-windows/

The author has copied the directory "hg-fast-export" into the new directory git (without the .git of "hg-fast-export") and has also added hg, git and python to his environment variables.

good luck

0
On

Following the @max630 suggestion bellow, I reinstalled Python and update the PATH

Starting with Git 2.24 (Q4 2019), you will have to install Python 3, since the hg-to-git script (in contrib/) has been updated to work with Python 3.

See commit d17ae00 (18 Sep 2019) by Hervé Beraud (4383).
(Merged by Junio C Hamano -- gitster -- in commit 8f53fe1, 07 Oct 2019)

hg-to-git: make it compatible with both python3 and python2

Python 2 is EOL at the end of 2019, many distros and systems now come with python 3 as their default version.

Rewrite features used in hg-to-git that are no longer supported in Python 3, in such a way that an updated code can still be usable with Python 2:

  • print is not a statement; use print() function instead.
  • dict.has_key(key) is no more; use "key in dict" instead.
0
On

In case it helps anyone, it took me a while to get hg-fast-export to work, but I eventually got there. Here are some issues I ran into:

  • I needed to install Python and also had to add the Python binary directory to the system PATH.
  • The instructions say to run hg-fast-export from a git bash window. On my first attempt I could not even cd to the correct directory, because my bash shell had me "jailed" to my home directory hierarchy. I changed the git bash shortcut to Run as Administrator, removed the --cd-to-home argument, and set it start in C:. Note that once you start the shell, paths will start with /c/.
  • After issuing command git init to create an empty repo, I also had to issue git config core.ignoreCase false before the export would go further.
    • Next I got the error "ImportError: No module named mercurial". I had done a binary install of TortoiseHg but it needed sources. I downloaded Mercurial sources and added the root source directory to the system environment variable PYTHONPATH. Then I re-launched git bash.
    • Now the git-fast-export worked!
    • As documented, git status will show all files as "deleted". Issue the command git checkout HEAD, repeat git status, and it should show "nothing to commit, working tree clean".
5
On

There seems to be a bug in the bash bundled with git. It does not initialize "$@" properly if started from directly running .sh file from cmd.

Open "GIT Bash" from start menu, then issue commands:

cd "/D/MyProject-git"

to navigate to D:\MyProject-git, and

../fast-export-master/hg-fast-export.sh -r "D:\MyProject"

to run your script

3
On

Since you already installed TortoiseHg, there has an easier way to migrate hg to git.

First, enable hggit in TortoiseHg:

TortoiseHg -> global setting -> Extensions Tab -> select hggit -> OK.

enter image description here

Then use below commands to migarte and push to VSTS git repo:

# In an empty folder
mkdir git
cd git
git init
cd ..
hg clone <URL for Hg repo>
cd hgrepo
hg bookmarks ../git
cd ..
cd git
git checkout hg
git remote add origin <VSTS git repo URL>
git push origin --all
0
On

I spent hours and hours trying many of the above solutions, and nothing worked well for me. I tried the fast-export and the hggit in tortoisehg in numerous variations.

In the end I used the GitHub Importer to import all of my mercurial repository into GitHub.

I just needed to provide the URL of the repository (mine was in BitBucket), and it was a very smooth and easy process. Much smoother than trying to get python working in git bash, path variables, etc!

I also had some significant local changes that I could not push to bitbucket because they're not allowing pushes. So I had to copy those files manually into the git repository.

I also could not get teamcity to detect the changes when switching from VCS roots from bitbucket to github. So I created a new project in team city.

I hope this helps anybody who is facing these challenges.