changeset 19:2be9c0bd88af

Warn, but don't fail when bookmarks is not enabled.
author Augie Fackler <durin42@gmail.com>
date Sun, 26 Apr 2009 18:27:47 -0700
parents feab56c76c92
children 9a93e8b0ec64
files git_handler.py tests/test-sane-without-bookmarks tests/test-sane-without-bookmarks.out
diffstat 3 files changed, 107 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/git_handler.py
+++ b/git_handler.py
@@ -9,7 +9,7 @@
 from mercurial import hg, util, context, error
 
 class GitHandler(object):
-    
+
     def __init__(self, dest_repo, ui):
         self.repo = dest_repo
         self.ui = ui
@@ -17,14 +17,14 @@
         self.load_git()
         self.load_map()
         self.load_config()
-        
-    # make the git data directory        
+
+    # make the git data directory
     def init_if_missing(self):
         git_hg_path = os.path.join(self.repo.path, 'git')
         if not os.path.exists(git_hg_path):
             os.mkdir(git_hg_path)
             dulwich.repo.Repo.init_bare(git_hg_path)
-    
+
     def load_git(self):
         git_dir = os.path.join(self.repo.path, 'git')
         self.git = Repo(git_dir)
@@ -37,26 +37,26 @@
             for line in self.repo.opener('git-mapfile'):
                 gitsha, hgsha = line.strip().split(' ', 1)
                 self._map[gitsha] = hgsha
-    
+
     def save_map(self):
         file = self.repo.opener('git-mapfile', 'w+')
         for gitsha, hgsha in self._map.iteritems():
             file.write("%s %s\n" % (gitsha, hgsha))
         file.close()
-    
+
     def load_config(self):
         self._config = {}
         if os.path.exists(self.repo.join('git-config')):
             for line in self.repo.opener('git-config'):
                 key, value = line.strip().split(' ', 1)
                 self._config[key] = value
-    
+
     def save_config(self):
         file = self.repo.opener('git-config', 'w+')
         for key, value in self._config.iteritems():
             file.write("%s %s\n" % (key, value))
         file.close()
-    
+
 
     ## END FILE LOAD AND SAVE METHODS
 
@@ -72,24 +72,24 @@
         self.export_git_objects()
         self.upload_pack(remote_name)
         self.save_map()
-        
+
     # TODO: make these actually save and recall
     def remote_add(self, remote_name, git_url):
         self._config['remote.' + remote_name + '.url'] = git_url
         self.save_config()
-        
+
     def remote_name_to_url(self, remote_name):
         return self._config['remote.' + remote_name + '.url']
-        
+
     def remote_head(self, remote_name):
         for head, sha in self.git.remote_refs(remote_name).iteritems():
             if head == 'HEAD':
                 return self._map[sha]
         return None
-    
+
     def upload_pack(self, remote_name):
         self.ui.status(_("upload pack\n"))
-        
+
     def fetch_pack(self, remote_name):
         git_url = self.remote_name_to_url(remote_name)
         client, path = self.get_transport_and_path(git_url)
@@ -103,7 +103,7 @@
             self.git.set_remote_refs(refs, remote_name)
         except:
             f.close()
-            raise    
+            raise
 
     def import_git_objects(self, remote_name):
         self.ui.status(_("importing Git objects into Hg\n"))
@@ -111,11 +111,11 @@
         todo = []
         done = set()
         convert_list = {}
-        
+
         # get a list of all the head shas
         for head, sha in self.git.remote_refs(remote_name).iteritems():
             todo.append(sha)
-        
+
         # traverse the heads getting a list of all the unique commits
         # TODO : stop when we hit a SHA we've already imported
         while todo:
@@ -127,32 +127,37 @@
             commit = self.git.commit(sha)
             convert_list[sha] = commit
             todo.extend([p for p in commit.parents if p not in done])
-        
-        # sort the commits 
+
+        # sort the commits
         commits = TopoSort(convert_list).items()
-        
+
         # import each of the commits, oldest first
         for csha in commits:
             commit = convert_list[csha]
             self.import_git_commit(commit)
-        
+
         # update Hg bookmarks
         bms = {}
         for head, sha in self.git.remote_refs(remote_name).iteritems():
             hgsha = hex_to_sha(self._map[sha])
             if not head == 'HEAD':
-                bms[remote_name + '/' + head] = hgsha            
-        bookmarks.write(self.repo, bms)
+                bms[remote_name + '/' + head] = hgsha
+        if hasattr(self.repo, '_bookmarkcurrent'):
+            bookmarks.write(self.repo, bms)
+        else:
+            self.repo.ui.warn('bookmarks are not enabled, not writing'
+                              ' them out!')
+
 
     def import_git_commit(self, commit):
         print "importing: " + commit.id
-        
+
         # TODO : (?) have to handle merge contexts at some point (two parent files, etc)
         def getfilectx(repo, memctx, f):
             (e, sha, data) = self.git.get_file(commit, f)
             e = '' # TODO : make this a real mode
             return context.memfilectx(f, data, 'l' in e, 'x' in e, None)
-        
+
         p1 = "0" * 40
         p2 = "0" * 40
         if len(commit.parents) > 0:
@@ -181,7 +186,7 @@
         # save changeset to mapping file
         gitsha = commit.id
         self._map[gitsha] = p2
-        
+
     def getfilectx(self, source, repo, memctx, f):
         v = files[f]
         data = source.getfile(f, v)
@@ -211,18 +216,18 @@
    Public domain, do with it as you will
 """
 class TopoSort(object):
-    
+
     def __init__(self, commitdict):
         self._sorted = self.robust_topological_sort(commitdict)
         self._shas = []
         for level in self._sorted:
             for sha in level:
                 self._shas.append(sha)
-            
+
     def items(self):
         self._shas.reverse()
         return self._shas
-        
+
     def strongly_connected_components(self, graph):
         """ Find the strongly connected components in a graph using
             Tarjan's algorithm.
@@ -303,6 +308,6 @@
             for successor in graph[node].parents:
                 successor_c = node_component[successor]
                 if node_c != successor_c:
-                    component_graph[node_c].append(successor_c) 
+                    component_graph[node_c].append(successor_c)
 
         return self.topological_sort(component_graph)
new file mode 100644
--- /dev/null
+++ b/tests/test-sane-without-bookmarks
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+# Fails for some reason, need to investigate
+# "$TESTDIR/hghave" git || exit 80
+
+# bail early if the user is already running git-daemon
+echo hi | nc localhost 9418 && exit 80
+
+echo "[extensions]" >> $HGRCPATH
+echo "hggit=$(echo $(dirname $(dirname $0)))" >> $HGRCPATH
+echo 'hgext.graphlog =' >> $HGRCPATH
+
+GIT_AUTHOR_NAME='test'; export GIT_AUTHOR_NAME
+GIT_AUTHOR_EMAIL='test@example.org'; export GIT_AUTHOR_EMAIL
+GIT_AUTHOR_DATE="2007-01-01 00:00:00 +0000"; export GIT_AUTHOR_DATE
+GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; export GIT_COMMITTER_NAME
+GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"; export GIT_COMMITTER_EMAIL
+GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"; export GIT_COMMITTER_DATE
+
+count=10
+commit()
+{
+    GIT_AUTHOR_DATE="2007-01-01 00:00:$count +0000"
+    GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
+    git commit "$@" >/dev/null 2>/dev/null || echo "git commit error"
+    count=`expr $count + 1`
+}
+
+mkdir gitrepo
+cd gitrepo
+git init | python -c "import sys; print sys.stdin.read().replace('$(dirname $(pwd))/', '')"
+echo alpha > alpha
+git add alpha
+commit -m 'add alpha'
+echo beta > beta
+git add beta
+commit -m 'add beta'
+
+# dulwich does not presently support local git repos, workaround
+cd ..
+git-daemon --base-path="$(pwd)"\
+ --listen=127.0.0.1\
+ --export-all\
+  --pid-file=gitdaemon.pid \
+ --detach --reuseaddr
+
+hg gclone git://127.0.0.1/gitrepo hgrepo
+cd hgrepo
+hg log --graph
+
+cd ..
+kill `cat gitdaemon.pid`
new file mode 100644
--- /dev/null
+++ b/tests/test-sane-without-bookmarks.out
@@ -0,0 +1,21 @@
+Initialized empty Git repository in gitrepo/.git/
+
+fetching from : origin
+Counting objects: 6, done.
+Compressing objects:  33% (1/3)   
Compressing objects:  66% (2/3)   
Compressing objects: 100% (3/3)   
Compressing objects: 100% (3/3), done.
+Total 6 (delta 0), reused 0 (delta 0)
+importing Git objects into Hg
+importing: 7eeab2ea75ec1ac0ff3d500b5b6f8a3447dd7c03
+importing: 9497a4ee62e16ee641860d7677cdb2589ea15554
+bookmarks are not enabled, not writing them out!2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+@  changeset:   1:7bcd915dc873
+|  tag:         tip
+|  user:        test <test@example.org>
+|  date:        Mon Jan 01 00:00:11 2007 +0000
+|  summary:     add beta
+|
+o  changeset:   0:3442585be8a6
+   user:        test <test@example.org>
+   date:        Mon Jan 01 00:00:10 2007 +0000
+   summary:     add alpha
+