Allow SHA1 as reference for inputs

Support specifying upstream and additional branches to import via
SHA1s.

Check for pattern character '*' in provided patterns to determine which
to treat as patterns and which as explicit revisions.

Include additional test function to check converting all refs to SHA1s
as input, and an additional scenario to check passing in a relative ref
using '~' to pick a different commit to the head of the branch.

Change-Id: Ie6ad695a34307d96b95f81b017ba9f190bc258dc
Partial-Bug: #1547180
This commit is contained in:
Darragh Bailey 2016-09-27 16:47:28 +01:00
parent 47092a9b63
commit 5f4038e268
4 changed files with 79 additions and 8 deletions

View File

@ -63,14 +63,13 @@ class ImportUpstream(LogDedentMixin, GitMixin):
invalid_ref = False
for branch in branches:
if not any(head for head in self.repo.heads
if head.name == branch):
msg = "Specified ref does not exist: '%s'"
if not self.is_valid_commit(branch):
msg = "Specified commit(ish) does not exist: '%s'"
self.log.error(msg, branch)
invalid_ref = True
if invalid_ref:
raise ImportUpstreamError("Invalid ref")
raise ImportUpstreamError("Invalid branch/tag/sha1")
@property
def branch(self):
@ -145,7 +144,7 @@ class ImportUpstream(LogDedentMixin, GitMixin):
commit = self.upstream
try:
self.git.show_ref(commit, quiet=True, heads=True)
self.git.rev_parse("%s^{commit}" % commit, verify=True)
except GitCommandError as e:
msg = "Invalid commit '%s' specified to import from"

View File

@ -20,6 +20,7 @@ from abc import abstractmethod
import itertools
import re
from git_upstream import errors
from git_upstream import lib
from git_upstream.lib.pygitcompat import Commit
from git_upstream.lib.utils import GitMixin
@ -276,7 +277,18 @@ class UpstreamMergeBaseSearcher(LogDedentMixin, Searcher):
if not patterns:
patterns = ["upstream/*"]
self._patterns = patterns
self._patterns = set()
self._revs = set()
for rev in patterns:
if "*" in rev:
self._patterns.add(rev)
elif self.is_valid_commit(rev):
self._revs.add(rev)
else:
raise errors.GitUpstreamError(
"'%s' not recognized as valid commit(ish) or pattern" %
rev)
self._references = ["refs/heads/{0}".format(ref)
for ref in self.patterns]
@ -332,8 +344,12 @@ class UpstreamMergeBaseSearcher(LogDedentMixin, Searcher):
"Searching for most recent merge base with upstream branches")
# process pattern given to get a list of refs to check
rev_list_args = self.git.for_each_ref(
*self._references, format="%(refname:short)").splitlines()
if self._references:
rev_list_args = self.git.for_each_ref(
*self._references, format="%(refname:short)").splitlines()
else:
rev_list_args = []
rev_list_args.extend(self._revs)
self.log.info(
"""
Upstream refs:

View File

@ -0,0 +1,43 @@
#
# Copyright (c) 2016 Hewlett-Packard Enterprise Development Company, L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
---
- desc: |
Test passing of alternative ref names works
Repository layout being checked (assumed already replayed)
C---D local/master
/
A---B---E---F upstream/master
tree:
- [A, []]
- [B, [A]]
- [C, [B]]
- [D, [C]]
- [E, [B]]
- [F, [E]]
branches:
head: [master, D]
upstream: [upstream/master, F]
parser-args: [-q, import, upstream/master~2]
expected: []
check-merge: False

View File

@ -88,6 +88,19 @@ class TestImportCommand(TestWithScenarios, BaseTestCase):
if extra_test_func:
extra_test_func()
def test_command_with_sha1s(self):
# convert branches/tags to sha1s and use sha1 instead of branch
parser_args = [
arg if (arg.startswith('-') or arg in ("import",))
else self.git.rev_parse(arg)
for arg in self.parser_args
]
args = self.parser.parse_args(parser_args)
self.assertThat(args.cmd.run(args), Equals(True),
"import command failed to complete successfully")
self._check_tree_state()
def _check_tree_state(self):
expected = getattr(self, 'expect_found', None)