Use tag for import branch naming when given

When importing using a tag for the upstream point to use, use the given
tag provided instead of running describe against it again as this might
result in the wrong tag being returned.

The documentation for git describe states that when provided a tag,
`git describe <tag>` will simply return the given tag, however
when it is not an annotated tag it is possible for there to be multiple
tags on the same commit and `git describe` may return any of them
instead of the originally given tag. Explicitly add a check to see if
the commit(ish) reference to import from is a tag and skip calling `git
describe` in such a case.

Change-Id: I7c6c3c2eb138089ccbc13d773da3d8a4a8efe673
Related-Bug: #1625878
This commit is contained in:
Darragh Bailey
2016-10-04 14:43:29 +01:00
parent bb152bb522
commit f243bd6355
2 changed files with 63 additions and 8 deletions

View File

@@ -154,17 +154,27 @@ class ImportUpstream(LogDedentMixin, GitMixin):
if not import_branch:
import_branch = self.import_branch
# use describe in order to be certain about unique identifying 'commit'
# determine if given a tag to import from and use as given, otherwise
# convert the given branch/commit into a described commit based on
# a recent tag.
#
# Create a describe string with the following format:
# <describe upstream>[-<extra branch abbref hash>]*
#
# Simply appends the 7 character ref abbreviation for each extra branch
# prefixed with '-', for each extra branch in the order they are given.
describe_commit = self.git.describe(commit, tags=True,
with_exceptions=False)
if not describe_commit:
self.log.warning("No tag describes the upstream branch")
describe_commit = self.git.describe(commit, always=True, tags=True)
# Extra branch abbref hash is the 7 character ref abbreviation for each
# extra branch joined with '-', in the order they are passed in to
# uniquely describe the full import.
if self.git.show_ref(commit, tags=True, with_exceptions=False):
# if given upstream ref is a tag, no need to describe
describe_commit = commit
else:
describe_commit = self.git.describe(commit, tags=True,
with_exceptions=False)
if not describe_commit:
self.log.warning("No tag describes the upstream branch")
describe_commit = self.git.describe(commit, always=True,
tags=True)
self.log.info("""
Using '%s' to describe:

View File

@@ -102,3 +102,48 @@ class TestImport(base.BaseTestCase):
self.assertEqual("?? dummy-file", self.git.status(porcelain=True),
"ImportUpstream.finish() failed to leave user "
"files not managed untouched.")
def test_import_create_import_branch_from_tag(self):
"""Test that using a tag to import from uses the correct tag
Repository layout being checked
B---C local/master
/
A---D---E upstream/master (tags: tag-um-1, tag-um-2)
"""
tree = [
('A', []),
('B', ['A']),
('C', ['B']),
('D', ['A']),
('E', ['D']),
]
branches = {
'head': ('master', 'C'),
'upstream': ('upstream/master', 'E'),
}
self.gittree = base.BuildTree(self.testrepo, tree, branches.values())
self.git.tag("tag-um-1", "upstream/master")
self.git.tag("tag-um-2", "upstream/master")
iu = ImportUpstream("master", "tag-um-1", "import/{describe}")
# create import
iu.create_import()
self.assertEqual(iu.import_branch, "import/tag-um-1",
"ImportUpstream.create_import() failed to use the "
"tag 'tag-um-1' for the import branch name")
# to confirm the tag being used is coming from the user, must
# test with the other tag to ensure it will use what is given
# to create the import branch, as 'git describe' can sometimes
# simply return one of the two tags applied, while what is
# desired is that only the tag given is used.
iu = ImportUpstream("master", "tag-um-2", "import/{describe}")
# create new import
iu.create_import()
self.assertEqual(iu.import_branch, "import/tag-um-2",
"ImportUpstream.create_import() failed to use the "
"tag 'tag-um-2' for the import branch name")