summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJannis Pohlmann <jannis.pohlmann@codethink.co.uk>2012-08-29 11:07:11 +0000
committerJannis Pohlmann <jannis.pohlmann@codethink.co.uk>2012-08-29 18:47:16 +0100
commit4018afbdb583517277a0f1f7d990fa4c2a04923a (patch)
tree625e63034b107c07a205d7077e0e0faec807d8b7
parentb78ac0f72aa2fc4815131bb2bbd883a373c62e48 (diff)
downloadmorph-4018afbdb583517277a0f1f7d990fa4c2a04923a.tar.gz
Make "morph show-system-branch" anywhere in a workspace, if possible
There are mainly three situations to deal with: 1. We are outside a workspace and cannot deduce the system branch. 2. We are inside a workspace and inside a branch. We can detect this by walking up from the working directory towards the workspace. If we find a .morph-system-branch in one of the parent directories, we know the branch name. If we don't find one, something is wrong. 3. We are inside a workspace but outside a branch (or partially into a branch, e.g. in foo/ where the branch is foo/bar). We can deduce the branch if we recurse into subdirectories to find a .morph-system-branch directory. Care needs to be taken to not recurse infinitely. We may also not recurse if there are multiple subdirectories as these could belong to two different branches. This commit makes "morph show-system-branch" work in all of the above scenarios. It also adds tests for all of them.
-rw-r--r--morphlib/plugins/branch_and_merge_plugin.py31
-rwxr-xr-xtests.branching/branch-creates-new-system-branch-not-from-master.script8
-rwxr-xr-xtests.branching/branch-creates-new-system-branch.script8
-rwxr-xr-xtests.branching/branch-when-branchdir-exists-locally.script6
-rwxr-xr-xtests.branching/checkout-existing-branch.script8
-rwxr-xr-xtests.branching/edit-checkouts-existing-chunk.script6
-rwxr-xr-xtests.branching/edit-clones-chunk.script6
-rwxr-xr-xtests.branching/edit-updates-stratum.script6
-rwxr-xr-xtests.branching/edit-uses-ref-from-stratum.script6
-rwxr-xr-xtests.branching/init-cwd.script6
-rwxr-xr-xtests.branching/init-default.script6
-rwxr-xr-xtests.branching/init-existing.script6
-rwxr-xr-xtests.branching/init-newdir.script6
-rwxr-xr-xtests.branching/init-nonempty.script6
-rwxr-xr-xtests.branching/merge-explicitly-named-repos.script6
-rwxr-xr-xtests.branching/minedir-not-found.script6
-rwxr-xr-xtests.branching/minedir.script6
-rwxr-xr-xtests.branching/petrify.script6
-rw-r--r--tests.branching/show-system-branch-fails-outside-workspace.exit1
-rwxr-xr-xtests.branching/show-system-branch-fails-outside-workspace.script (renamed from tests.branching/show-system-branch-shows-name-correctly.script)16
-rw-r--r--tests.branching/show-system-branch-fails-outside-workspace.stderr1
-rw-r--r--tests.branching/show-system-branch-fails-when-branch-is-ambiguous.exit1
-rwxr-xr-xtests.branching/show-system-branch-fails-when-branch-is-ambiguous.script32
-rw-r--r--tests.branching/show-system-branch-fails-when-branch-is-ambiguous.stderr1
-rw-r--r--tests.branching/show-system-branch-shows-name-correctly.stdout1
-rwxr-xr-xtests.branching/show-system-branch-works-anywhere-with-a-single-branch.script31
-rw-r--r--tests.branching/show-system-branch-works-anywhere-with-a-single-branch.stdout1
-rwxr-xr-xtests.branching/show-system-branch-works-in-different-directories-in-a-branch.script57
-rw-r--r--tests.branching/show-system-branch-works-in-different-directories-in-a-branch.stdout6
-rwxr-xr-xtests.branching/workflow.script6
30 files changed, 225 insertions, 68 deletions
diff --git a/morphlib/plugins/branch_and_merge_plugin.py b/morphlib/plugins/branch_and_merge_plugin.py
index 3145b774..cb975482 100644
--- a/morphlib/plugins/branch_and_merge_plugin.py
+++ b/morphlib/plugins/branch_and_merge_plugin.py
@@ -58,17 +58,42 @@ class BranchAndMergePlugin(cliapp.Plugin):
dirname = os.path.dirname(dirname)
raise cliapp.AppException("Can't find the workspace directory")
+ @staticmethod
+ def is_system_branch_directory(dirname):
+ return os.path.isdir(os.path.join(dirname, '.morph-system-branch'))
+
@classmethod
def deduce_system_branch(cls):
+ # 1. Deduce the workspace. If this fails, we're not inside a workspace.
workspace = cls.deduce_workspace()
+ # 2. We're in a workspace. Check if we're inside a system branch.
+ # If we are, return its name.
dirname = os.getcwd()
while dirname != workspace and dirname != '/':
- system_branch_dir = os.path.join(dirname, '.morph-system-branch')
- if os.path.isdir(system_branch_dir):
- return os.path.dirname(system_branch_dir[len(workspace)+1:])
+ if cls.is_system_branch_directory(dirname):
+ return os.path.relpath(dirname, workspace)
dirname = os.path.dirname(dirname)
+ # 3. We're in a workspace but not inside a branch. Try to find a
+ # branch directory in the directories below the current working
+ # directory. Avoid ambiguousity by only recursing deeper if there
+ # is only one subdirectory.
+ visited = set()
+ for dirname, subdirs, files in os.walk(os.getcwd(), followlinks=True):
+ # Avoid infinite recursion.
+ if dirname in visited:
+ break
+ visited.add(dirname)
+
+ if cls.is_system_branch_directory(dirname):
+ return os.path.relpath(dirname, workspace)
+
+ # Do not recurse deeper if we have more than one
+ # non-hidden directory.
+ if len([x for x in subdirs if not x.startswith('.')]) > 1:
+ break
+
raise cliapp.AppException("Can't find the system branch directory")
@staticmethod
diff --git a/tests.branching/branch-creates-new-system-branch-not-from-master.script b/tests.branching/branch-creates-new-system-branch-not-from-master.script
index 1116ae76..9356a70f 100755
--- a/tests.branching/branch-creates-new-system-branch-not-from-master.script
+++ b/tests.branching/branch-creates-new-system-branch-not-from-master.script
@@ -1,15 +1,15 @@
#!/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.
@@ -24,7 +24,7 @@ cd "$DATADIR/workspace"
"$SRCDIR/scripts/test-morph" branch newbranch origin/alfred
echo "File tree:"
-"$SRCDIR/scripts/list-tree" . | grep -v '/\.git/' |
+"$SRCDIR/scripts/list-tree" . | grep -v '/\.git/' |
sed 's,/cache/gits/file_[^/]*_,/cache/gits/file_,'
echo "Current branches:"
diff --git a/tests.branching/branch-creates-new-system-branch.script b/tests.branching/branch-creates-new-system-branch.script
index 9f3b3b4f..fa58796e 100755
--- a/tests.branching/branch-creates-new-system-branch.script
+++ b/tests.branching/branch-creates-new-system-branch.script
@@ -1,15 +1,15 @@
#!/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.
@@ -24,7 +24,7 @@ cd "$DATADIR/workspace"
"$SRCDIR/scripts/test-morph" branch newbranch
echo "File tree:"
-"$SRCDIR/scripts/list-tree" . | grep -v '/\.git/' |
+"$SRCDIR/scripts/list-tree" . | grep -v '/\.git/' |
sed 's,/cache/gits/file_[^/]*_,/cache/gits/file_,'
echo "Current branches:"
diff --git a/tests.branching/branch-when-branchdir-exists-locally.script b/tests.branching/branch-when-branchdir-exists-locally.script
index 27d32062..7278e7e9 100755
--- a/tests.branching/branch-when-branchdir-exists-locally.script
+++ b/tests.branching/branch-when-branchdir-exists-locally.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/checkout-existing-branch.script b/tests.branching/checkout-existing-branch.script
index 54697a1d..643a3a5e 100755
--- a/tests.branching/checkout-existing-branch.script
+++ b/tests.branching/checkout-existing-branch.script
@@ -1,15 +1,15 @@
#!/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.
@@ -26,7 +26,7 @@ cd "$DATADIR/workspace"
"$SRCDIR/scripts/test-morph" checkout master
echo "File tree:"
-"$SRCDIR/scripts/list-tree" . | grep -v '/\.git/' |
+"$SRCDIR/scripts/list-tree" . | grep -v '/\.git/' |
sed 's,/cache/gits/file_[^/]*_,/cache/gits/file_,'
echo "Current branches:"
diff --git a/tests.branching/edit-checkouts-existing-chunk.script b/tests.branching/edit-checkouts-existing-chunk.script
index a3a8d01e..99276853 100755
--- a/tests.branching/edit-checkouts-existing-chunk.script
+++ b/tests.branching/edit-checkouts-existing-chunk.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/edit-clones-chunk.script b/tests.branching/edit-clones-chunk.script
index 38f2cd75..20f62a27 100755
--- a/tests.branching/edit-clones-chunk.script
+++ b/tests.branching/edit-clones-chunk.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/edit-updates-stratum.script b/tests.branching/edit-updates-stratum.script
index 4a2f6a10..7b81f59a 100755
--- a/tests.branching/edit-updates-stratum.script
+++ b/tests.branching/edit-updates-stratum.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/edit-uses-ref-from-stratum.script b/tests.branching/edit-uses-ref-from-stratum.script
index 8b952b02..ce3e0ccc 100755
--- a/tests.branching/edit-uses-ref-from-stratum.script
+++ b/tests.branching/edit-uses-ref-from-stratum.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/init-cwd.script b/tests.branching/init-cwd.script
index 61a62b1d..2d14586c 100755
--- a/tests.branching/init-cwd.script
+++ b/tests.branching/init-cwd.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/init-default.script b/tests.branching/init-default.script
index f6abf7d0..de4627e7 100755
--- a/tests.branching/init-default.script
+++ b/tests.branching/init-default.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/init-existing.script b/tests.branching/init-existing.script
index b4031082..e713b9df 100755
--- a/tests.branching/init-existing.script
+++ b/tests.branching/init-existing.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/init-newdir.script b/tests.branching/init-newdir.script
index 70c8bb87..5e79ce87 100755
--- a/tests.branching/init-newdir.script
+++ b/tests.branching/init-newdir.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/init-nonempty.script b/tests.branching/init-nonempty.script
index 3bcb8e88..959da028 100755
--- a/tests.branching/init-nonempty.script
+++ b/tests.branching/init-nonempty.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/merge-explicitly-named-repos.script b/tests.branching/merge-explicitly-named-repos.script
index a8509045..9052cd31 100755
--- a/tests.branching/merge-explicitly-named-repos.script
+++ b/tests.branching/merge-explicitly-named-repos.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/minedir-not-found.script b/tests.branching/minedir-not-found.script
index 66e3336e..690f45d3 100755
--- a/tests.branching/minedir-not-found.script
+++ b/tests.branching/minedir-not-found.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/minedir.script b/tests.branching/minedir.script
index 88a7b5ef..f872a37d 100755
--- a/tests.branching/minedir.script
+++ b/tests.branching/minedir.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/petrify.script b/tests.branching/petrify.script
index 02d5f1d8..921eb048 100755
--- a/tests.branching/petrify.script
+++ b/tests.branching/petrify.script
@@ -1,15 +1,15 @@
#!/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.
diff --git a/tests.branching/show-system-branch-fails-outside-workspace.exit b/tests.branching/show-system-branch-fails-outside-workspace.exit
new file mode 100644
index 00000000..d00491fd
--- /dev/null
+++ b/tests.branching/show-system-branch-fails-outside-workspace.exit
@@ -0,0 +1 @@
+1
diff --git a/tests.branching/show-system-branch-shows-name-correctly.script b/tests.branching/show-system-branch-fails-outside-workspace.script
index 9bbbc27c..fc3f3db3 100755
--- a/tests.branching/show-system-branch-shows-name-correctly.script
+++ b/tests.branching/show-system-branch-fails-outside-workspace.script
@@ -1,15 +1,15 @@
#!/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.
@@ -21,11 +21,13 @@
set -eu
-# Create system branch.
+# Create a workspace and branch.
cd "$DATADIR/workspace"
"$SRCDIR/scripts/test-morph" init
-"$SRCDIR/scripts/test-morph" branch newbranch
+"$SRCDIR/scripts/test-morph" branch testbranch
-cd newbranch/morphs
-"$SRCDIR/scripts/test-morph" show-system-branch
+# Leave the workspace.
+cd ..
+# Try to show the current branch.
+"$SRCDIR/scripts/test-morph" show-system-branch
diff --git a/tests.branching/show-system-branch-fails-outside-workspace.stderr b/tests.branching/show-system-branch-fails-outside-workspace.stderr
new file mode 100644
index 00000000..ea9fb165
--- /dev/null
+++ b/tests.branching/show-system-branch-fails-outside-workspace.stderr
@@ -0,0 +1 @@
+ERROR: Can't find the workspace directory
diff --git a/tests.branching/show-system-branch-fails-when-branch-is-ambiguous.exit b/tests.branching/show-system-branch-fails-when-branch-is-ambiguous.exit
new file mode 100644
index 00000000..d00491fd
--- /dev/null
+++ b/tests.branching/show-system-branch-fails-when-branch-is-ambiguous.exit
@@ -0,0 +1 @@
+1
diff --git a/tests.branching/show-system-branch-fails-when-branch-is-ambiguous.script b/tests.branching/show-system-branch-fails-when-branch-is-ambiguous.script
new file mode 100755
index 00000000..f6fc4646
--- /dev/null
+++ b/tests.branching/show-system-branch-fails-when-branch-is-ambiguous.script
@@ -0,0 +1,32 @@
+#!/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.
+
+
+# Check that 'morph show-system-branch' fails when the system branch
+# is not obvious.
+
+
+set -eu
+
+# Create a workspace and two system branches
+cd "$DATADIR/workspace"
+"$SRCDIR/scripts/test-morph" init
+"$SRCDIR/scripts/test-morph" branch first/branch
+"$SRCDIR/scripts/test-morph" branch second/branch
+
+# Try to find out the branch from the workspace directory.
+cd "$DATADIR/workspace"
+"$SRCDIR/scripts/test-morph" show-system-branch
diff --git a/tests.branching/show-system-branch-fails-when-branch-is-ambiguous.stderr b/tests.branching/show-system-branch-fails-when-branch-is-ambiguous.stderr
new file mode 100644
index 00000000..7c784733
--- /dev/null
+++ b/tests.branching/show-system-branch-fails-when-branch-is-ambiguous.stderr
@@ -0,0 +1 @@
+ERROR: Can't find the system branch directory
diff --git a/tests.branching/show-system-branch-shows-name-correctly.stdout b/tests.branching/show-system-branch-shows-name-correctly.stdout
deleted file mode 100644
index 467e4889..00000000
--- a/tests.branching/show-system-branch-shows-name-correctly.stdout
+++ /dev/null
@@ -1 +0,0 @@
-newbranch
diff --git a/tests.branching/show-system-branch-works-anywhere-with-a-single-branch.script b/tests.branching/show-system-branch-works-anywhere-with-a-single-branch.script
new file mode 100755
index 00000000..66b2a01f
--- /dev/null
+++ b/tests.branching/show-system-branch-works-anywhere-with-a-single-branch.script
@@ -0,0 +1,31 @@
+#!/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.
+
+
+# Check that 'morph show-system-branch' works even outside a branch
+# if there only is one in the workspcae.
+
+
+set -eu
+
+# Create a workspace and a system branch.
+cd "$DATADIR/workspace"
+"$SRCDIR/scripts/test-morph" init
+"$SRCDIR/scripts/test-morph" branch first/branch
+
+# Show the branch even when outside the branch.
+cd "$DATADIR/workspace"
+"$SRCDIR/scripts/test-morph" show-system-branch
diff --git a/tests.branching/show-system-branch-works-anywhere-with-a-single-branch.stdout b/tests.branching/show-system-branch-works-anywhere-with-a-single-branch.stdout
new file mode 100644
index 00000000..b934ad8e
--- /dev/null
+++ b/tests.branching/show-system-branch-works-anywhere-with-a-single-branch.stdout
@@ -0,0 +1 @@
+first/branch
diff --git a/tests.branching/show-system-branch-works-in-different-directories-in-a-branch.script b/tests.branching/show-system-branch-works-in-different-directories-in-a-branch.script
new file mode 100755
index 00000000..1391f0ec
--- /dev/null
+++ b/tests.branching/show-system-branch-works-in-different-directories-in-a-branch.script
@@ -0,0 +1,57 @@
+#!/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.
+
+
+# Check that 'morph show-system-branch' shows the name of the
+# current system branch correctly from various working directories.
+
+
+set -eu
+
+# Create a workspace and two system branches.
+cd "$DATADIR/workspace"
+"$SRCDIR/scripts/test-morph" init
+"$SRCDIR/scripts/test-morph" branch first/branch
+"$SRCDIR/scripts/test-morph" branch second/branch
+
+# Create a few subdirectories in the first branch.
+mkdir -p "$DATADIR/workspace/first/branch/foo"
+mkdir -p "$DATADIR/workspace/first/branch/bar"
+mkdir -p "$DATADIR/workspace/first/branch/foo/bar/baz"
+
+# Show the first branch when partially inside the branch.
+cd "$DATADIR/workspace/first"
+"$SRCDIR/scripts/test-morph" show-system-branch
+
+# Show the first branch when inside the main branch directory.
+cd "$DATADIR/workspace/first/branch"
+"$SRCDIR/scripts/test-morph" show-system-branch
+
+# Show the first branch when somewhere inside the branch.
+cd "$DATADIR/workspace/first/branch/foo"
+"$SRCDIR/scripts/test-morph" show-system-branch
+
+# Show the first branch when somewhere else inside the branch.
+cd "$DATADIR/workspace/first/branch/foo/bar/baz"
+"$SRCDIR/scripts/test-morph" show-system-branch
+
+# Show the second branch when partially inside the branch.
+cd "$DATADIR/workspace/second"
+"$SRCDIR/scripts/test-morph" show-system-branch
+
+# Show the second branch when inside the main branch directory.
+cd "$DATADIR/workspace/second/branch"
+"$SRCDIR/scripts/test-morph" show-system-branch
diff --git a/tests.branching/show-system-branch-works-in-different-directories-in-a-branch.stdout b/tests.branching/show-system-branch-works-in-different-directories-in-a-branch.stdout
new file mode 100644
index 00000000..f9cc3aec
--- /dev/null
+++ b/tests.branching/show-system-branch-works-in-different-directories-in-a-branch.stdout
@@ -0,0 +1,6 @@
+first/branch
+first/branch
+first/branch
+first/branch
+second/branch
+second/branch
diff --git a/tests.branching/workflow.script b/tests.branching/workflow.script
index 0c4f8578..ca626cd8 100755
--- a/tests.branching/workflow.script
+++ b/tests.branching/workflow.script
@@ -1,15 +1,15 @@
#!/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.