How do I correctly install dulwich to get hg-git working on Windows?

Question:

I’m trying to use the hg-git Mercurial extension on Windows (Windows 7 64-bit, to be specific). I have Mercurial and Git installed. I have Python 2.5 (32-bit) installed.

I followed the instructions on http://hg-git.github.com/ to install the extension. The initial easy_install failed because it was unable to compile dulwich without Visual Studio 2003.

I installed dulwich manually by:

  • git clone git://git.samba.org/jelmer/dulwich.git
  • cd dulwich
  • c:Python25python setup.py –pure install

Now when I run easy_install hg-git, it succeeds (since the dulwich dependency is satisfied).

In my C:UsersusernameMercurial.ini, I have:

[extensions]
hgext.bookmarks =
hggit =

When I type ‘hg’ at a command prompt, I see:
“*** failed to import extension hggit: No module named hggit”

Looking under my c:Python25 folder, the only reference to hggit I see is Libsite-packageshg_git-0.2.1-py2.5.egg. Is this supposed to be extracted somewhere, or should it work as-is?

Since that failed, I attempted the “more involved” instructions from the hg-git page that suggested cloning git://github.com/schacon/hg-git.git and referencing the path in my Mercurial configuration. I cloned the repo, and changed my extensions file to look like:

[extensions]
hgext.bookmarks =
hggit = c:codehg-githggit

Now when I run hg, I see: *** failed to import extension hggit from c:codehg-githggit: No module named dulwich.errors.

Ok, so that tells me that it is finding hggit now, because I can see in hg-githggitgit_handler.py that it calls

from dulwich.errors import HangupException

That makes me think dulwich is not installed correctly, or not in the path.

Update:

From Python command line:

import dulwich

yields Import Error: No module named dulwich

However, under C:Python25Libsite-packages, I do have a dulwich-0.5.0-py2.5.egg folder which appears to be populated. This was created by the steps mentioned above. Is there an additional step I need to take to make it part of the Python “path”?

From Python command line (as suggested in one of the answers):

import pkg_resources
pkg_resources.require('dulwich')

yields [dulwich 0.5.0 (c:python25libsite-packagesdulwich-0.5.0-py2.5.egg)]

So what does that tell me? Importing dulwich fails, but apparently pkg_resources can find it. What can I do with that information?

Asked By: Joshua Flanagan

||

Answers:

Until you get import dulwich to work, hggit won’t work. Check that the dulwich egg file is in your easy-install.pth file under site-packages.

For further debugging you can try ask pkg_resources about it:

import pkg_resources
pkg_resources.require("dulwich")
Answered By: David Fraser

Try following configuration (change to your path), which works for me:

[extensions]
; hg-git extention
hgext.bookmarks =
hggit = C:Python26Libsite-packageshg_git-0.2.1-py2.6.egghggit

In my case when I have empty value for hggit =, I get the same error as you do in this case. But I can import dulwich without problem in python shell, so you should check your easy-install.pth (as pointed out by David) if it contains dulwich-0.5.0-py2.5.egg.
I did install pure version of dulwich as well.

Answered By: van

If you can install TortoiseHg, it includes dulwich and other requirements.

Answered By: kuy

That makes me think dulwich is not
installed correctly, or not in the
path.

You’re absolutely right. Mercurial binary distributions for Windows are ‘frozen’ – they use the Python code and interpreter bundled with them and therefore independent of packages installed in system PYTHONPATH. When you specify path to hggit extension in Mercurial.ini, hg tries to import it using direct path, but dulwich library is not imported explicitly by hg and doesn’t bundled with its library, so the import fails.

It is possible to add both Dulwich and HgGit into library.zip that is installed along with hg.exe, but for me the best way is to install everything from source including Mercurial and execute commands using .bat files installed into PythonScripts. In this case you will need to:

  1. Install Mercurial from source. This builds “pure” version, because Windows users usually don’t have Visual Studio or alternative compiler for compiling C speedups.
  2. Install Dulwich – I’d use latest trunk snapshot for both Git and Dulwich.

    python setup.py –pure install

  3. Install latest HgGit snapshot

    python setup.py install

  4. Edit Mercurial.ini to enable hggit =

  5. Launch Mercurial using your PythonScriptshg.bat
Answered By: anatoly techtonik

I ran into this problem too with dulwich.errors. Instead of installing everything from scratch. I just copied dulwich from my default site-packages to the mercurial site-packages. worked with no problems.

Answered By: Marco

I found a simpler solution at http://candidcode.com/2010/01/12/a-guide-to-converting-from-mercurial-hg-to-git-on-a-windows-client/

And then I found a yet simpler solution myself:

To use the hg-git Mercurial extension on Windows:

  1. install the official Mercurial binaries
  2. put dulwich folder
    from dulwich sources and hggit folder from hg-git sources to the
    root of library.zip in Mercurial installation folder
  3. add the following to %USERPROFILE%Mercurial.ini:

[extensions]
hgext.bookmarks=
hggit=

To have SSH support you need plink.exe from PuTTY family. After that you should add the following to Mercurial.ini:

[ui]
username = John Doe <[email protected]>
ssh=d:/home/lib/dll/plink.exe -i "d:/home2/ssh-private-key.ppk"

When connecting to a SSH server for the first time, you should start putty.exe and try to connect using it. It will add the server key fingerprint to the registry. Otherwise plink will ask you to accept the fingerprint but it doesn’t accept any input.

You can use puttygen.exe to generate private keys. Either use keys without a passphrase or use Pageant.exe ssh authentication agent.

Answered By: nponeccop

sudo apt-get install python-dev # Dam you dependency!

sudo easy_install dulwich

Success!

Answered By: Thierry

Based on techtonik’s explanation of the reason for the failing import of dulwich.errors, I found a solution which appears simpler to me than the already proposed ones:

On the Mercurial download page there is offered

Mercurial <x.y.z> for Python 2.7 on Windows x86 (source install)

and

Mercurial <x.y.z> for Python 2.7 on Windows x64 (source install)

as .EXE files which install Mercurial as a Python module into an existing Python 2.7 (x86 or x64) installation.

If hg-git and dulwich have been installed to this Python installation as well, the import errors should vanish.

If you are not sure what to do now, I hope this step-by-step explanation helps:

Prerequisites

  • Python 2.7 is installed
  • <python 2.7 install dir> and <python 2.7 install dir>Scripts are in the PATH environment variable
  • hg-git (and dulwich) have been installed into this Python installation via the instructions from here

Steps

  1. Deinstall any existing Mercurial installation
  2. Download and install one of the above-quoted EXE files, depending on whether your Python 2.7 installation is 32bit or 64bit (If you get a “Python 2.7 cannot be found in registry” error, you probably need the other one.)

Now hg clone <some git repo> should work on the console.

Answered By: Daniel K

I got this error as well even after downloading the latest Tortoisehg and making sure the hggit plugin was installed as well as my .ini & hgrc files had the right entry to enable hggit.

Turns out my problem was that I had both mercurial and tortoisehg in my path. So when I ran any hg commands, it was using the hg.exe in mercurial folder as opposed to the hg.exe in the torsoisehg directory.

This makes sense but my mercurial installation did not have the plug ins. My fix was to remove mercurial from my path so hg commands go through the tortoisehg directory since it has hg completely bundled. Note however, the recommended option might be to upgrade mercurual to a version that has the plugins that one needs but this is what worked for me. I tried replacing the library.zip in mercurial with the one in tortoisehg and this worked but it led to other errors as one would imagine.

@techtonik’s answer led me down this road for which I am grateful.

Recap: verify which hg exe is running your hg commands by checking your path because that hg.exe does not find the plugins for whatever reason.

Answered By: delloPiro

Had this problem today when installing the latest TortoiseHg.

Get the latest python 2.7 (I used 2.7.16) setup on your system, you probably have this already.
Get a command window and go to the c:Python27 folder
To run pip use Scriptspip or easy_install use Scriptseasy_install
Try pip -V to make sure you get the 2.7 version and not some 3.X version

Wrong:

c:Python27>pip -V
pip 20.2.4 from c:python38libsite-packagespip (python 3.8)

Right:

c:Python27>Scriptspip -V
pip 20.2.4 from c:python27libsite-packagespip (python 2.7)

If dulwich or hg-git are installed already

Scriptspip uninstall dulwich
Scriptspip uninstall hg-git

Install hg-git

Scriptseasy_install install hg-git

You should now have two folders

C:Python27Libsite-packagesdulwich-0.19.16-py2.7-win-amd64.eggdulwich
C:Python27Libsite-packageshg_git-0.9.0-py2.7.egghggit

It will only work if Dulwich is version 0.19.16 (less than 0.20.x)

Copy these folders (dulwich and hggit) into the zip-file
C:Program FilesTortoiseHgliblibrary.zip

folder in zip file

Answered By: xpress

After a long research I finally managed to setup hggit properly.
What I have done:

  1. I have installed the newest version of TortoiseHg (in my case Version 5.9.1)
    This official version (automatically) contains Python-2.7.18 (According to Help/About)
  2. Download dulwich: cd C:dulwich, git clone https://github.com/dulwich/dulwich.git .
  3. TortoiseHg uses Python 2.7, dulwich doesn’t support this (old) Python version anymore. We have to go back to an older version of dulwich (<0.20)
    git reset --hard c6993451a745d313f61e5d080b9f9d8611a8d7f4
  4. The subfolder C:dulwichdulwich has to be copied inside the zip file
    C:Program FilesTortoiseHgliblibrary.zip so that there is a path
    C:Program FilesTortoiseHgliblibrary.zipdulwich
    This can be done by the following commands:
    a) Extract the currenty content of TortoiseHg’s library
    md "C:Program FilesTortoiseHglibExtractedLibrary"
    tar -xf "C:Program FilesTortoiseHgliblibrary.zip" -C "C:Program FilesTortoiseHglibExtractedLibrary"
    b) Copy dulwich files into directory
    xcopy C:dulwichdulwich "C:Program FilesTortoiseHglibExtractedLibrarydulwich"
    c) Save old library.zip and create new library.zip
    ren "C:Program FilesTortoiseHgliblibrary.zip" library_old.zip
    cd "C:Program FilesTortoiseHglibExtractedLibrary"
    tar -acf "C:Program FilesTortoiseHgliblibrary.zip" *.*
    d) Cleanup
    cd ..
    rd /q /s "C:Program FilesTortoiseHglibExtractedLibrary"
    rd /q /s C:dulwich
  5. Download hg-git: cd C:hg-git, hg clone https://foss.heptapod.net/mercurial/hg-git .
  6. The current version of hg-git also does not support Python 2.7 anymore. We have to go back to the last working version 0.9.0 (=revision 1320 in TortoiseHg):
    hg update 6d7d6f174df8
  7. Create a new empty repository which will be "connected" to a remote git repository
    cd C:MyLocalHgRepoWhichIsConnectedToGit, hg init
  8. Create a textfile C:MyLocalHgRepoWhichIsConnectedToGit.hghgrc with the following content:
[paths]
default = C:ConnectedGitRepoForPushAndPull

[extensions]
hgext.bookmarks = 
hggit = C:hg-githggit

Remark: Alternatively you can also copy the folder C:hg-githggit inside library.zip and leave the path in the hgrc file empty. Indeed this is my favourite way. Do it the same way as I have explained under step 4 with dulwich.

That’s it. Now the Mercurial Repo is linked to the Git Repo
and you can execute hg pull or hg push inside the Hg Repo.



If you don’t want to read all my explanations you can simply use this batch file and execute it:

hggit.bat

@echo off
rem Installation von dulwich+hggit für TortoiseHg
rem Michael Hutter, 22.09.2021
rem https://stackoverflow.com/a/69279657/9134997

if "%1"=="install" goto install
if "%1"=="clonegitrepo" goto clonegitrepo
echo Syntax:
echo %0 install (installiert dulwich und hggit in TortoiseHg)
echo %0 clonegitrepo C:HgRepoToCreate C:GitRepoToClone (Klonen eines Git-Repos in ein Hg-Repo)
pause
goto ende


:install
set LibraryPath=C:Program FilesTortoiseHglib
set TempPathDulwich=C:Tempdulwich
set TempPathHggit=C:Temphg-git

if not exist "%LibraryPath%library.zip" (
echo Die Datei %LibraryPath%library.zip existiert nicht!
pause
goto Ende
)

rem Installation von dulwich
md %TempPathDulwich%
cd %TempPathDulwich%
git clone https://github.com/dulwich/dulwich.git .
git reset --hard c6993451a745d313f61e5d080b9f9d8611a8d7f4
pause

rem Installation von hg-git
md %TempPathHggit%
cd %TempPathHggit%
hg clone https://foss.heptapod.net/mercurial/hg-git .
hg update 6d7d6f174df8
pause

rem dulwich und hggit in library.zip aufnehmen
md "%LibraryPath%ExtractedLibrary"
tar -xf "%LibraryPath%library.zip" -C "%LibraryPath%ExtractedLibrary"
xcopy %TempPathDulwich%dulwich "%LibraryPath%ExtractedLibrarydulwich"
xcopy %TempPathHggit%hggit "%LibraryPath%ExtractedLibraryhggit"
ren "%LibraryPath%library.zip" library_old.zip
if exist "%LibraryPath%library.zip" del "%LibraryPath%library.zip"
cd "%LibraryPath%ExtractedLibrary"
tar -acf "%LibraryPath%library.zip" *.*

rem Aufräumen
cd ..
rd /q /s "%LibraryPath%ExtractedLibrary"
rd /q /s %TempPathDulwich%
rd /q /s %TempPathHggit%
pause
goto ende


:clonegitrepo
rem Klonen eines Git-Repos in ein lokales Hg-Repo
echo Erstelle neues HgRepo %2
md %2
cd %2
if not exist %2 (
echo "Fehler: %2 existiert nicht!"
pause
goto ende
)
hg init
echo [paths] > .hghgrc
echo default = %3 >> .hghgrc
echo. >> .hghgrc
echo [extensions] >> .hghgrc
echo hgext.bookmarks = >> .hghgrc
echo hggit = >> .hghgrc
hg pull
hg update
pause

:ende
Answered By: Michael Hutter