diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2012-10-01 19:30:23 +0100 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2012-10-03 14:40:37 +0100 |
commit | 8bcb622e182d49d143af417bd3d0a64d4188356b (patch) | |
tree | 917404b4d2108af6e175fb8e84eaea69548a7d6f | |
parent | 28f8e387b8e5bf6ffcc0ca340bfc8717d5ffc9d5 (diff) | |
download | morph-8bcb622e182d49d143af417bd3d0a64d4188356b.tar.gz |
Use origin/ref for all refs other than the system branch ref itself
The rationale here is that inside a checkout of a system branch, the
user should only be committing to the refs for that system branch,
because those are the only ones that 'morph merge' will look at.
This removes a problem where we could be confused by a ref with a
name that would be sorted before 'master' in the result of 'git
show-ref'.
-rw-r--r-- | morphlib/git.py | 9 | ||||
-rw-r--r-- | morphlib/plugins/branch_and_merge_plugin.py | 16 | ||||
-rwxr-xr-x | tests.branching/ambiguous-refs.script | 49 |
3 files changed, 68 insertions, 6 deletions
diff --git a/morphlib/git.py b/morphlib/git.py index 27f22351..b3dd2c45 100644 --- a/morphlib/git.py +++ b/morphlib/git.py @@ -221,13 +221,12 @@ def clone_into(runcmd, srcpath, targetpath, ref=None): else: runcmd(['git', 'clone', '-b', ref, srcpath, targetpath]) -def find_first_ref(runcmd, gitdir, ref): - '''Find the *first* ref match and returns its sha1.''' - return runcmd(['git', 'show-ref', ref], - cwd=gitdir).split("\n")[0].split(" ")[0] - def is_valid_sha1(ref): '''Checks whether a string is a valid SHA1.''' valid_chars = 'abcdefABCDEF0123456789' return len(ref) == 40 and all([x in valid_chars for x in ref]) + +def rev_parse(runcmd, gitdir, ref): + '''Find the sha1 for the given ref''' + return runcmd(['git', 'rev-parse', ref], cwd=gitdir)[0:40] diff --git a/morphlib/plugins/branch_and_merge_plugin.py b/morphlib/plugins/branch_and_merge_plugin.py index 279a9948..6f2e38fc 100644 --- a/morphlib/plugins/branch_and_merge_plugin.py +++ b/morphlib/plugins/branch_and_merge_plugin.py @@ -221,12 +221,26 @@ class BranchAndMergePlugin(cliapp.Plugin): self.app.runcmd(['git', 'remote', 'update'], cwd=dirname) def load_morphology(self, repo_dir, name, ref=None): + '''Returns a morphology, optionally retrieved from a specific ref + + The repositories that make up a system branch checkout should only + have modifications to the ref matching that system branch. Unless 'ref' + is a sha1, origin/ref will be used instead to ensure that no local + modifications are considered and no local tracking branch needs to be + created. Where the ref is that of the system branch, 'None' should be + passed because we should load directly from the working tree in this + case, honouring any uncommitted changes. + + ''' + if ref is None: filename = os.path.join(repo_dir, '%s.morph' % name) with open(filename) as f: text = f.read() else: - ref = morphlib.git.find_first_ref(self.app.runcmd, repo_dir, ref) + if not morphlib.git.is_valid_sha1(ref): + ref = morphlib.git.rev_parse(self.app.runcmd, repo_dir, + 'origin/' + ref) try: text = self.app.runcmd(['git', 'cat-file', 'blob', '%s:%s.morph' % (ref, name)], diff --git a/tests.branching/ambiguous-refs.script b/tests.branching/ambiguous-refs.script new file mode 100755 index 00000000..3321a310 --- /dev/null +++ b/tests.branching/ambiguous-refs.script @@ -0,0 +1,49 @@ +#!/bin/sh +# Copyright (C) 2012 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +# 'git show-ref master' sorts its output alphebetically, so we can't rely on +# it to resolve 'master': we might get 'alpha/master' instead. + +set -eu + +. "$SRCDIR/scripts/fix-committer-info" + +# Disable test on versions of Python before 2.7. +if ! python --version 2>&1 | grep '^Python 2\.[78]' > /dev/null +then + exit 0 +fi + +cd "$DATADIR/morphs" +git mv hello-stratum.morph goodbye-stratum.morph +sed -e 's/"morph": "hello-stratum"/"morph": "goodbye-stratum"/' \ + -i hello-system.morph + +git commit --quiet -am "Rename hello-system" + +cd "$DATADIR/workspace" +"$SRCDIR/scripts/test-morph" init +"$SRCDIR/scripts/test-morph" branch baserock:morphs release + +# Create an extra ref to confuse any users of git show-ref +cd release/baserock:morphs +git checkout --quiet -b alpha/master HEAD~1 +git checkout --quiet release + +# The petrify will fail if we resolved 'master' as 'alpha/master' by mistake. +cd "$DATADIR/workspace/release/baserock:morphs" +"$SRCDIR/scripts/test-morph" petrify |