Subject: [PATCH 11/14] gitweb: git_is_head_detached() function

From: Giuseppe Bilotta <[email-masked]>
Date: Fri, 31 Aug 2007 13:19:11 +0200

The function checks if the HEAD for the current project is detached by checking if 'git branch' returns "* (no branch)"

Signed-off-by: Giuseppe Bilotta <[email-masked]>
---
 gitweb/gitweb.perl |   13 +++++++++----
 1 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index e743c3d..a745434 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -1416,6 +1416,13 @@ sub git_get_head_hash {
 	return $retval;
 }
 
+# check if current HEAD is detached
+sub git_is_head_detached {
+	my @x = (git_cmd(), 'branch');
+	my @ret = split("\n", qx(@x));
+	return 0 + grep { /^* \(no branch\)$/ } @ret;
+}
+
 # get type of given object
 sub git_get_type {
 	my $hash = shift;
@@ -2091,11 +2098,9 @@ sub git_get_heads_list {
 	my @headslist;
 
 	if (grep { $_ eq 'heads' } @class) {
-		my @x = (git_cmd(), 'branch');
-		my @ret = split("\n", qx(@x));
-		if (grep { /^* \(no branch\)$/ } @ret) { ;
+		if (git_is_head_detached()) { ;
 			my %ref_item;
-			@x = (git_cmd(), 'log', '-1', '--pretty=format:%H%n%ct%n%s');
+			my @x = (git_cmd(), 'log', '-1', '--pretty=format:%H%n%ct%n%s');
 			my ($hash, $epoch, $title) = split("\n", qx(@x), 3);
 
 			$ref_item{'class'} = 'head';
-- 
1.5.2.5