changeset 298:6ad6945b6629

pull: make it possible to un-wedge the repo after stripping git revs This adds a new command, which is inelegant, but it lets you safely pick up pulling fairly quickly.
author Augie Fackler <durin42@gmail.com>
date Thu, 25 Mar 2010 20:24:00 -0500
parents a90fe3e8a8c3
children 6bde2f727de9
files hggit/__init__.py hggit/git_handler.py tests/test-pull-after-strip tests/test-pull-after-strip.out
diffstat 4 files changed, 179 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/hggit/__init__.py
+++ b/hggit/__init__.py
@@ -81,6 +81,16 @@
     git = GitHandler(repo, ui)
     git.clear()
 
+def git_cleanup(ui, repo):
+    new_map = []
+    for line in repo.opener(GitHandler.mapfile):
+        gitsha, hgsha = line.strip().split(' ', 1)
+        if hgsha in repo:
+            new_map.append('%s %s\n' % (gitsha, hgsha))
+    f = repo.opener(GitHandler.mapfile, 'wb')
+    map(f.write, new_map)
+    ui.status(_('git commit map cleaned\n'))
+
 cmdtable = {
   "gimport":
         (gimport, [], _('hg gimport')),
@@ -88,4 +98,6 @@
         (gexport, [], _('hg gexport')),
   "gclear":
       (gclear, [], _('Clears out the Git cached data')),
+  "git-cleanup": (git_cleanup, [], _(
+        "Cleans up git repository after history editing"))
 }
--- a/hggit/git_handler.py
+++ b/hggit/git_handler.py
@@ -14,12 +14,12 @@
 
 
 class GitHandler(object):
+    mapfile = 'git-mapfile'
+    tagsfile = 'git-tags'
 
     def __init__(self, dest_repo, ui):
         self.repo = dest_repo
         self.ui = ui
-        self.mapfile = 'git-mapfile'
-        self.tagsfile = 'git-tags'
 
         if ui.config('git', 'intree'):
             self.gitdir = self.repo.wjoin('.git')
@@ -509,6 +509,8 @@
         if octopus:
             extra['hg-git'] ='octopus-done'
 
+        if p1 not in self.repo or p2 not in self.repo:
+            raise hgutil.Abort(_('you appear to have run strip - please run hg git-cleanup'))
         ctx = context.memctx(self.repo, (p1, p2), text, list(files), getfilectx,
                              author, date, extra)
 
new file mode 100755
--- /dev/null
+++ b/tests/test-pull-after-strip
@@ -0,0 +1,85 @@
+#!/bin/sh
+
+# Fails for some reason, need to investigate
+# "$TESTDIR/hghave" git || exit 80
+
+# bail if the user does not have dulwich
+python -c 'import dulwich, dulwich.repo' || exit 80
+
+# bail early if the user is already running git-daemon
+echo hi | nc localhost 9418 2>/dev/null && exit 80
+
+
+cat >> $HGRCPATH <<EOF
+[extensions]
+graphlog=
+bookmarks=
+mq=
+EOF
+echo "hggit=$(echo $(dirname $(dirname $0)))/hggit" >> $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'
+
+git tag alpha
+
+git checkout -b beta 2>&1 | sed s/\'/\"/g
+echo beta > beta
+git add beta
+commit -m 'add beta'
+
+
+cd ..
+git daemon --base-path="$(pwd)"\
+ --listen=localhost\
+ --export-all\
+ --pid-file=gitdaemon.pid \
+ --detach --reuseaddr
+
+echo % clone a tag
+hg clone -r alpha git://localhost/gitrepo hgrepo-a | grep -v '^updating'
+cd hgrepo-a
+hg log --graph
+
+cd ..
+echo % clone a branch
+hg clone -r beta git://localhost/gitrepo hgrepo-b | grep -v '^updating'
+cd hgrepo-b
+hg log --graph
+cd ..
+
+cd gitrepo
+echo beta line 2 >> beta
+git add beta
+commit -m 'add to beta'
+
+cd ..
+cd hgrepo-b
+hg strip tip 2>&1 | grep -v saving
+hg pull -r beta
+hg git-cleanup
+hg pull -r beta
+hg log --graph
+
+cd ..
+kill `cat gitdaemon.pid`
new file mode 100644
--- /dev/null
+++ b/tests/test-pull-after-strip.out
@@ -0,0 +1,78 @@
+Initialized empty Git repository in gitrepo/.git/
+
+Switched to a new branch "beta"
+% clone a tag
+importing Hg objects into Git
+Counting objects: 3, done.
+Total 3 (delta 0), reused 0 (delta 0)
+importing Git objects into Hg
+1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+@  changeset:   0:3442585be8a6
+   tag:         master
+   tag:         default/master
+   tag:         tip
+   tag:         alpha
+   user:        test <test@example.org>
+   date:        Mon Jan 01 00:00:10 2007 +0000
+   summary:     add alpha
+
+% clone a branch
+importing Hg objects into Git
+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
+2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+@  changeset:   1:7bcd915dc873
+|  tag:         default/beta
+|  tag:         tip
+|  tag:         beta
+|  user:        test <test@example.org>
+|  date:        Mon Jan 01 00:00:11 2007 +0000
+|  summary:     add beta
+|
+o  changeset:   0:3442585be8a6
+   tag:         master
+   tag:         alpha
+   tag:         default/master
+   user:        test <test@example.org>
+   date:        Mon Jan 01 00:00:10 2007 +0000
+   summary:     add alpha
+
+0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+pulling from git://localhost/gitrepo
+importing Hg objects into Git
+Counting objects: 6, done.
+Compressing objects:  25% (1/4)   
Compressing objects:  50% (2/4)   
Compressing objects:  75% (3/4)   
Compressing objects: 100% (4/4)   
Compressing objects: 100% (4/4), done.
+Total 6 (delta 0), reused 0 (delta 0)
+importing Git objects into Hg
+abort: you appear to have run strip - please run hg git-cleanup
+git commit map cleaned
+pulling from git://localhost/gitrepo
+importing Hg objects into Git
+Counting objects: 6, done.
+Compressing objects:  25% (1/4)   
Compressing objects:  50% (2/4)   
Compressing objects:  75% (3/4)   
Compressing objects: 100% (4/4)   
Compressing objects: 100% (4/4), done.
+Total 6 (delta 0), reused 0 (delta 0)
+importing Git objects into Hg
+(run 'hg update' to get a working copy)
+o  changeset:   2:611948b1ec6a
+|  tag:         default/beta
+|  tag:         tip
+|  tag:         beta
+|  tag:         master
+|  user:        test <test@example.org>
+|  date:        Mon Jan 01 00:00:12 2007 +0000
+|  summary:     add to beta
+|
+o  changeset:   1:7bcd915dc873
+|  user:        test <test@example.org>
+|  date:        Mon Jan 01 00:00:11 2007 +0000
+|  summary:     add beta
+|
+@  changeset:   0:3442585be8a6
+   tag:         alpha
+   tag:         default/master
+   user:        test <test@example.org>
+   date:        Mon Jan 01 00:00:10 2007 +0000
+   summary:     add alpha
+