From 48a267d5080dc27277cfaaea03875fe9af2305bf Mon Sep 17 00:00:00 2001 From: Peter Droogmans Date: Fri, 13 Jul 2012 16:49:15 +0200 Subject: [PATCH] better Head detection if refs/heads is pointing to non-existing master --- lib/Git/Repository.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/Git/Repository.php b/lib/Git/Repository.php index b9fc3d4..4b297c0 100644 --- a/lib/Git/Repository.php +++ b/lib/Git/Repository.php @@ -422,14 +422,21 @@ class Repository else { return 'master'; } + // Find first existing branch foreach (explode("\n", $file) as $line) { $m = array(); if (preg_match('#ref:\srefs/heads/(.+)#', $line, $m)) { - return $m[1]; + if ($this->hasBranch($m[1])) { + return $m[1]; + } } } // Default to something sane if in a detached HEAD state. + $branches = $this->getBranches(); + if (!empty($branches)) { + return current($branches); + } return 'master'; }