Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Bourget committed Mar 12, 2012
0 parents commit efe27a1
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
# Ingore Mercurial, until you need to inter-operate with it
44 changes: 44 additions & 0 deletions README.rst
@@ -0,0 +1,44 @@
git-hg bi-directional interface
-------------------------------

This does the job "hg-git" does for Mercurial, but the reverse. It actually uses
the same tool (the gexport / gimport feature of Hg-Git), written by GitHub.

This is based on the Blog post by Travis Cline

http://traviscline.com/blog/2010/04/27/using-hg-git-to-work-in-git-and-push-to-hg/

To install:

sudo ln -s `pwd`/githg.py /usr/lib/git-core/git-hg

Make sure you have hg-git installed. See http://hg-git.github.com/

git hg clone https://bitbucket.org/user/project

Work on your Git repo, do some commits.

git hg push

which pushes to the local .hg repo, and offers to push upstream.

When new changes arrive from upstream, run:

git hg fetch

or

git hg pull

which map to the same Git semantics: ``fetch`` updates your branch with new stuff, and ``pull`` adds a merge to the mix.

This code is not yet tested with complex topologies, additions, comments and pull requests are welcome.



TODO
----

git-hg should check for a same bookmarks Hg configuration, etc.

The first release was written at PyCon 2012, by Alexandre Bourget
85 changes: 85 additions & 0 deletions githg.py
@@ -0,0 +1,85 @@
#!/usr/bin/python
# -=- encoding: utf-8 -=-

"""Git-hg is a bi-directional interface to Mercurial, like git-svn, but for Hg.
git hg clone hg://blah/repository
git hg push
git hg fetch
git hg pull
Refs:
Isn't bi-directional: https://github.com/offbytwo/git-hg
Hard to remember: http://traviscline.com/blog/2010/04/27/using-hg-git-to-work-in-git-and-push-to-hg/
"""

import os
import sys

# TODO: when running a command, ensure the Hg bookmark plugin is active and
# installed

def clone(url):
"""This makes an hg clone and makes that appear as a Git repository."""
subdir = os.path.basename(url)
os.system("hg clone -U %s" % (url,))
os.system("""
cd %s
hg bookmark hg/default -r default
# Do the import
hg gexport
# Configure the git repo and checkout
ln -s .hg/git .git
git branch master hg/default
git config core.bare false
git reset --hard
# Ignore the .hg stuff
echo '.hg' >> .git/info/exclude
echo "[ui]
ignore = `pwd`/.hg/hgignore" >> .hg/hgrc
echo ".git" >> .hg/hgignore
""" % subdir)

def push():
"""Pushes back to the Hg repository.
Like ``git push``, but up to the remote Mercurial repo.
"""
os.system("hg gimport")
res = raw_input("Import Git commits into Hg local repo. Push back to the Hg remote ?")
if res.lower() in ('y', 'yes', '1', 'true'):
os.system("hg push")
os.system("hg bookmark -f hg/default -r default")

def fetch():
"""Update the local branches with what is up on the remote Mercurial repo.
Equivalent to ``git fetch`` in Git.
This updates the Git branches to point to the Mercurial ones.
"""
os.system("hg pull")
os.system("hg bookmark -f hg/default -r default")
os.system("hg gexport")

def pull():
"""Fetch and merge the remote changes to the Hg repo.
Equivalent to ``git pull`` in Git.
"""
fetch()
os.system("git merge hg/default")


if __name__ == '__main__':
map = {'clone': clone,
'fetch': fetch,
'pull': pull,
'push': push}
if sys.argv[1] in map:
map[sys.argv[1]](*(sys.argv[2:]))

0 comments on commit efe27a1

Please sign in to comment.