This is more flexible than relying on the branch root repository
directory to have the original name. The user might rename the
branch root directory and we still want to be able to find it.
Add a test that this new functionality works.
---
morphlib/plugins/branch_and_merge_plugin.py | 30 ++++++++++++++++---
...edit-works-after-branch-root-was-renamed.script | 34 ++++++++++++++++++++++
...edit-works-after-branch-root-was-renamed.stdout | 18 ++++++++++++
3 files changed, 78 insertions(+), 4 deletions(-)
create mode 100755 tests.branching/edit-works-after-branch-root-was-renamed.script
create mode 100644 tests.branching/edit-works-after-branch-root-was-renamed.stdout
diff --git a/morphlib/plugins/branch_and_merge_plugin.py
b/morphlib/plugins/branch_and_merge_plugin.py
index ea9b67a..cedf248 100644
--- a/morphlib/plugins/branch_and_merge_plugin.py
+++ b/morphlib/plugins/branch_and_merge_plugin.py
@@ -311,6 +311,31 @@ class BranchAndMergePlugin(cliapp.Plugin):
path = os.path.join(path, parts.path[1:])
return path
+ def find_repository(self, branch_dir, repo):
+ visited = set()
+ for dirname, subdirs, files in os.walk(branch_dir, followlinks=True):
+ # Avoid infinite recursion.
+ if dirname in visited:
+ subdirs[:] = []
+ continue
+ visited.add(dirname)
+
+ # Check if the current directory is a git repository and, if so,
+ # whether it was cloned from the repo we are looking for.
+ if '.git' in subdirs:
+ try:
+ original_repo = self.app.runcmd(
+ ['git', 'config', 'morph.repository'],
cwd=dirname)
+ original_repo = original_repo.strip()
+
+ if repo == original_repo:
+ return dirname
+ except:
+ pass
+
+ # Do not recurse into hidden directories.
+ subdirs[:] = [x for x in subdirs if not x.startswith('.')]
+
def checkout(self, args):
'''Check out an existing system branch.'''
@@ -384,10 +409,7 @@ class BranchAndMergePlugin(cliapp.Plugin):
# Find out which repository we branched off from.
branch_dir = os.path.join(workspace, system_branch)
branch_root = self.read_branch_root(branch_dir)
-
- # Convert it to a local directory in the branch.
- branch_root_path = self.convert_uri_to_path(branch_root)
- branch_root_dir = os.path.join(branch_dir, branch_root_path)
+ branch_root_dir = self.find_repository(branch_dir, branch_root)
# Find out which repository to edit.
repo, repo_url = args[0], self.resolve_reponame(self.app, args[0])
diff --git a/tests.branching/edit-works-after-branch-root-was-renamed.script
b/tests.branching/edit-works-after-branch-root-was-renamed.script
new file mode 100755
index 0000000..da8fb0b
--- /dev/null
+++ b/tests.branching/edit-works-after-branch-root-was-renamed.script
@@ -0,0 +1,34 @@
+#!/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.
+
+# Verify that the branch root repository created by "morph branch" or
+# "morph checkout" can be renamed and "morph edit" still finds the
+# branch root repo and works.
+
+set -eu
+
+cd "$DATADIR/workspace"
+
+"$SRCDIR/scripts/test-morph" init
+"$SRCDIR/scripts/test-morph" checkout baserock:morphs master
+
+cd "$DATADIR/workspace/master"
+mv baserock:morphs my-renamed-morphs
+
+"$SRCDIR/scripts/test-morph" edit baserock:hello master
+
+"$SRCDIR/scripts/list-tree" "$DATADIR/workspace" | grep -v
'/\.git/' |
+ sed 's,/cache/gits/file_[^/]*_,/cache/gits/file_,'
diff --git a/tests.branching/edit-works-after-branch-root-was-renamed.stdout
b/tests.branching/edit-works-after-branch-root-was-renamed.stdout
new file mode 100644
index 0000000..5554e08
--- /dev/null
+++ b/tests.branching/edit-works-after-branch-root-was-renamed.stdout
@@ -0,0 +1,18 @@
+d .
+d ./.morph
+d ./.morph/cache
+d ./.morph/cache/gits
+d ./.morph/cache/gits/file_hello
+d ./.morph/cache/gits/file_hello/.git
+d ./.morph/cache/gits/file_morphs
+d ./.morph/cache/gits/file_morphs/.git
+d ./master
+d ./master/.morph-system-branch
+d ./master/baserock:hello
+d ./master/baserock:hello/.git
+d ./master/my-renamed-morphs
+d ./master/my-renamed-morphs/.git
+f ./master/.morph-system-branch/branch-root
+f ./master/baserock:hello/hello.morph
+f ./master/my-renamed-morphs/hello-stratum.morph
+f ./master/my-renamed-morphs/hello-system.morph
--
1.7.11.4