[PATCH 1 of 3] convert: add bookmarks reading support to git backend
Edouard Gomez
ed.gomez at free.fr
Fri Mar 25 00:38:52 UTC 2011
# HG changeset patch
# User Edouard Gomez <ed.gomez at free.fr>
# Date 1301013523 -3600
# Node ID ed5861a67cb9d20fb034a0d1a78197ea1695e982
# Parent 78a0a815fd41d794550d12428362cd51b261c1c6
convert: add bookmarks reading support to git backend
diff --git a/hgext/convert/git.py b/hgext/convert/git.py
--- a/hgext/convert/git.py
+++ b/hgext/convert/git.py
@@ -17,19 +17,27 @@
# cannot remove environment variable. Just assume none have
# both issues.
if hasattr(os, 'unsetenv'):
- def gitopen(self, s):
+ def gitopen(self, s, noerr=False):
prevgitdir = os.environ.get('GIT_DIR')
os.environ['GIT_DIR'] = self.path
try:
- return util.popen(s, 'rb')
+ if noerr:
+ (stdin, stdout, stderr) = util.popen3(s)
+ return stdout
+ else:
+ return util.popen(s, 'rb')
finally:
if prevgitdir is None:
del os.environ['GIT_DIR']
else:
os.environ['GIT_DIR'] = prevgitdir
else:
- def gitopen(self, s):
- return util.popen('GIT_DIR=%s %s' % (self.path, s), 'rb')
+ def gitopen(self, s, noerr=False):
+ if noerr:
+ (sin, so, se) = util.popen3('GIT_DIR=%s %s' % (self.path, s))
+ return stdout
+ else:
+ util.popen('GIT_DIR=%s %s' % (self.path, s), 'rb')
def gitread(self, s):
fh = self.gitopen(s)
@@ -168,3 +176,30 @@
raise util.Abort(_('cannot read changes in %s') % version)
return changes
+
+ def getbookmarks(self):
+ bookmarks = {}
+
+ # Interesting references in git are prefixed
+ prefix = 'refs/heads/'
+ prefixlen = len(prefix)
+
+ # factor two commands
+ gitcmd = { 'remote/': 'git ls-remote --heads origin',
+ '': 'git show-ref'}
+
+ # Origin heads
+ for reftype in gitcmd:
+ try:
+ fh = self.gitopen(gitcmd[reftype], noerr=True)
+ for line in fh:
+ line = line.strip()
+ rev, name = line.split(None, 1)
+ if not name.startswith(prefix):
+ continue
+ name = '%s%s' % (reftype, name[prefixlen:])
+ bookmarks[name] = rev
+ except:
+ pass
+
+ return bookmarks
diff --git a/tests/test-convert-git.t b/tests/test-convert-git.t
--- a/tests/test-convert-git.t
+++ b/tests/test-convert-git.t
@@ -57,9 +57,11 @@
2 t4.1
1 t4.2
0 Merge branch other
+ updating bookmarks
$ hg up -q -R git-repo-hg
$ hg -R git-repo-hg tip -v
changeset: 5:c78094926be2
+ bookmark: master
tag: tip
parent: 3:f5f5cb45432b
parent: 4:4e174f80c67c
@@ -217,6 +219,7 @@
sorting...
converting...
0 addbinary
+ updating bookmarks
$ cd git-repo3-hg
$ hg up -C
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
@@ -248,8 +251,10 @@
converting...
1 addfoo
0 addfoo2
+ updating bookmarks
$ hg -R git-repo4-hg log -v
changeset: 1:d63e967f93da
+ bookmark: master
tag: tip
user: nottest <test at example.org>
date: Mon Jan 01 00:00:21 2007 +0000
diff --git a/tests/test-convert-tagsbranch-topology.t b/tests/test-convert-tagsbranch-topology.t
--- a/tests/test-convert-tagsbranch-topology.t
+++ b/tests/test-convert-tagsbranch-topology.t
@@ -49,6 +49,7 @@
converting...
0 rev1
updating tags
+ updating bookmarks
Simulate upstream updates after first conversion
@@ -67,6 +68,7 @@
converting...
0 rev2
updating tags
+ updating bookmarks
Print the log
More information about the Mercurial-devel
mailing list