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.
---
morphlib/plugins/branch_and_merge_plugin.py | 31 ++++++++++--
...reates-new-system-branch-not-from-master.script | 8 +--
.../branch-creates-new-system-branch.script | 8 +--
.../branch-when-branchdir-exists-locally.script | 6 +--
tests.branching/checkout-existing-branch.script | 8 +--
.../edit-checkouts-existing-chunk.script | 6 +--
tests.branching/edit-clones-chunk.script | 6 +--
tests.branching/edit-updates-stratum.script | 6 +--
tests.branching/edit-uses-ref-from-stratum.script | 6 +--
tests.branching/init-cwd.script | 6 +--
tests.branching/init-default.script | 6 +--
tests.branching/init-existing.script | 6 +--
tests.branching/init-newdir.script | 6 +--
tests.branching/init-nonempty.script | 6 +--
.../merge-explicitly-named-repos.script | 6 +--
tests.branching/minedir-not-found.script | 6 +--
tests.branching/minedir.script | 6 +--
tests.branching/petrify.script | 6 +--
...show-system-branch-fails-outside-workspace.exit | 1 +
...ow-system-branch-fails-outside-workspace.script | 33 +++++++++++++
...ow-system-branch-fails-outside-workspace.stderr | 1 +
...stem-branch-fails-when-branch-is-ambiguous.exit | 1 +
...em-branch-fails-when-branch-is-ambiguous.script | 32 ++++++++++++
...em-branch-fails-when-branch-is-ambiguous.stderr | 1 +
.../show-system-branch-shows-name-correctly.script | 31 ------------
.../show-system-branch-shows-name-correctly.stdout | 1 -
...anch-works-anywhere-with-a-single-branch.script | 31 ++++++++++++
...anch-works-anywhere-with-a-single-branch.stdout | 1 +
...rks-in-different-directories-in-a-branch.script | 57 ++++++++++++++++++++++
...rks-in-different-directories-in-a-branch.stdout | 6 +++
tests.branching/workflow.script | 6 +--
31 files changed, 249 insertions(+), 92 deletions(-)
create mode 100644 tests.branching/show-system-branch-fails-outside-workspace.exit
create mode 100755 tests.branching/show-system-branch-fails-outside-workspace.script
create mode 100644 tests.branching/show-system-branch-fails-outside-workspace.stderr
create mode 100644
tests.branching/show-system-branch-fails-when-branch-is-ambiguous.exit
create mode 100755
tests.branching/show-system-branch-fails-when-branch-is-ambiguous.script
create mode 100644
tests.branching/show-system-branch-fails-when-branch-is-ambiguous.stderr
delete mode 100755 tests.branching/show-system-branch-shows-name-correctly.script
delete mode 100644 tests.branching/show-system-branch-shows-name-correctly.stdout
create mode 100755
tests.branching/show-system-branch-works-anywhere-with-a-single-branch.script
create mode 100644
tests.branching/show-system-branch-works-anywhere-with-a-single-branch.stdout
create mode 100755
tests.branching/show-system-branch-works-in-different-directories-in-a-branch.script
create mode 100644
tests.branching/show-system-branch-works-in-different-directories-in-a-branch.stdout
diff --git a/morphlib/plugins/branch_and_merge_plugin.py
b/morphlib/plugins/branch_and_merge_plugin.py
index 3145b77..cb97548 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 1116ae7..9356a70 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 9f3b3b4..fa58796 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 27d3206..7278e7e 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 54697a1..643a3a5 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 a3a8d01..9927685 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 38f2cd7..20f62a2 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 4a2f6a1..7b81f59 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 8b952b0..ce3e0cc 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 61a62b1..2d14586 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 f6abf7d..de4627e 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 b403108..e713b9d 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 70c8bb8..5e79ce8 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 3bcb8e8..959da02 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 a850904..9052cd3 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 66e3336..690f45d 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 88a7b5e..f872a37 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 02d5f1d..921eb04 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 0000000..d00491f
--- /dev/null
+++ b/tests.branching/show-system-branch-fails-outside-workspace.exit
@@ -0,0 +1 @@
+1
diff --git a/tests.branching/show-system-branch-fails-outside-workspace.script
b/tests.branching/show-system-branch-fails-outside-workspace.script
new file mode 100755
index 0000000..fc3f3db
--- /dev/null
+++ b/tests.branching/show-system-branch-fails-outside-workspace.script
@@ -0,0 +1,33 @@
+#!/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.
+
+
+set -eu
+
+# Create a workspace and branch.
+cd "$DATADIR/workspace"
+"$SRCDIR/scripts/test-morph" init
+"$SRCDIR/scripts/test-morph" branch testbranch
+
+# 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 0000000..ea9fb16
--- /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 0000000..d00491f
--- /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 0000000..f6fc464
--- /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 0000000..7c78473
--- /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.script
b/tests.branching/show-system-branch-shows-name-correctly.script
deleted file mode 100755
index 9bbbc27..0000000
--- a/tests.branching/show-system-branch-shows-name-correctly.script
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/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.
-
-
-set -eu
-
-# Create system branch.
-cd "$DATADIR/workspace"
-"$SRCDIR/scripts/test-morph" init
-"$SRCDIR/scripts/test-morph" branch newbranch
-
-cd newbranch/morphs
-"$SRCDIR/scripts/test-morph" show-system-branch
-
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 467e488..0000000
--- 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 0000000..66b2a01
--- /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 0000000..b934ad8
--- /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 0000000..1391f0e
--- /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 0000000..f9cc3ae
--- /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 0c4f857..ca626cd 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.
--
1.7.11.4