changeset 602:65d8a43bc5ee

git_handler: skip exporting hg tags whose names are not valid as git tag name
author nsuke <nsukeg@gmail.com>
date Mon, 12 Aug 2013 23:20:41 +0900
parents 792955be68dd
children c42bf64d88a8
files hggit/git_handler.py tests/test-hg-tags-invalid.t
diffstat 2 files changed, 99 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/hggit/git_handler.py
+++ b/hggit/git_handler.py
@@ -4,7 +4,7 @@
 from dulwich.errors import HangupException, GitProtocolError, UpdateRefsError
 from dulwich.objects import Blob, Commit, Tag, Tree, parse_timezone, S_IFGITLINK
 from dulwich.pack import create_delta, apply_delta
-from dulwich.repo import Repo
+from dulwich.repo import Repo, check_ref_format
 from dulwich import client
 from dulwich import config as dul_config
 
@@ -1039,12 +1039,18 @@
                 tag = tag.replace(' ', '_')
                 target = self.map_git_get(hex(sha))
                 if target is not None:
-                    self.git.refs['refs/tags/' + tag] = target
-                    self.tags[tag] = hex(sha)
+                    tag_refname = 'refs/tags/' + tag
+                    if(check_ref_format(tag_refname)):
+                      self.git.refs[tag_refname] = target
+                      self.tags[tag] = hex(sha)
+                    else:
+                      self.repo.ui.warn(
+                        'Skipping export of tag %s because it '
+                        'has invalid name as a git refname.\n' % tag)
                 else:
                     self.repo.ui.warn(
                         'Skipping export of tag %s because it '
-                        'has no matching git revision.' % tag)
+                        'has no matching git revision.\n' % tag)
 
     def _filter_for_bookmarks(self, bms):
         if not self.branch_bookmark_suffix:
new file mode 100644
--- /dev/null
+++ b/tests/test-hg-tags-invalid.t
@@ -0,0 +1,89 @@
+Load commonly used test logic
+  $ . "$TESTDIR/testutil"
+
+  $ git init gitrepo
+  Initialized empty Git repository in $TESTTMP/gitrepo/.git/
+  $ cd gitrepo
+  $ echo alpha > alpha
+  $ git add alpha
+  $ fn_git_commit -m "add alpha"
+  $ git checkout -b not-master
+  Switched to a new branch 'not-master'
+
+  $ cd ..
+  $ hg clone gitrepo hgrepo | grep -v '^updating'
+  importing git objects into hg
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+
+  $ cd hgrepo
+  $ hg co master
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ fn_hg_tag alph#a
+  $ fn_hg_tag bet*a
+  $ hg push
+  pushing to $TESTTMP/gitrepo
+  Skipping export of tag bet*a because it has invalid name as a git refname.
+  searching for changes
+  adding objects
+  added 2 commits with 2 trees and 2 blobs
+  adding reference refs/tags/alph#a
+  updating reference refs/heads/master
+
+  $ hg log --graph | egrep -v ': *(not-master|master)'
+  @  changeset:   2:e72bdd9ef5c0
+  |  tag:         default/master
+  |  tag:         tip
+  |  user:        test
+  |  date:        Mon Jan 01 00:00:12 2007 +0000
+  |  summary:     Added tag bet*a for changeset 432ce25d86bc
+  |
+  o  changeset:   1:432ce25d86bc
+  |  tag:         bet*a
+  |  user:        test
+  |  date:        Mon Jan 01 00:00:11 2007 +0000
+  |  summary:     Added tag alph#a for changeset 3442585be8a6
+  |
+  o  changeset:   0:3442585be8a6
+     tag:         alph#a
+     tag:         default/not-master
+     user:        test <test@example.org>
+     date:        Mon Jan 01 00:00:10 2007 +0000
+     summary:     add alpha
+  
+
+  $ cd ..
+  $ cd gitrepo
+git should have only the valid tag alph#a but have full commit log including the missing invalid bet*a tag commit
+  $ git tag -l
+  alph#a
+
+  $ cd ..
+  $ hg clone gitrepo hgrepo2 | grep -v '^updating'
+  importing git objects into hg
+  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ hg -R hgrepo2 log --graph | egrep -v ': *(not-master|master)'
+  @  changeset:   2:e72bdd9ef5c0
+  |  tag:         default/master
+  |  tag:         tip
+  |  user:        test
+  |  date:        Mon Jan 01 00:00:12 2007 +0000
+  |  summary:     Added tag bet*a for changeset 432ce25d86bc
+  |
+  o  changeset:   1:432ce25d86bc
+  |  tag:         bet*a
+  |  user:        test
+  |  date:        Mon Jan 01 00:00:11 2007 +0000
+  |  summary:     Added tag alph#a for changeset 3442585be8a6
+  |
+  o  changeset:   0:3442585be8a6
+     tag:         alph#a
+     tag:         default/not-master
+     user:        test <test@example.org>
+     date:        Mon Jan 01 00:00:10 2007 +0000
+     summary:     add alpha
+  
+
+the tag should be in .hgtags
+  $ cat hgrepo2/.hgtags
+  3442585be8a60c6cd476bbc4e45755339f2a23ef alph#a
+  432ce25d86bc4281747aa42e27b473b992e2b0b9 bet*a