Subject: Re: [PATCH v2 09/11] gitweb: git_is_head_detached() function

From: Jakub Narebski <[email-masked]>
Date: Sun, 16 Nov 2008 00:43:45 +0100

On Thu, 13 Nov 2008, Giuseppe Bilotta wrote:

> 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]>

In my opinion this patch should really be squashed together with
previous one. They belong together.

> ---
>  gitweb/gitweb.perl |   13 +++++++++----
>  1 files changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
> index a168f6f..ceb0271 100755
> --- a/gitweb/gitweb.perl
> +++ b/gitweb/gitweb.perl
> @@ -1844,6 +1844,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;
> +}

First, not git-branch. Second, you can use Perl-only solution:

+# check if current HEAD is detached
+sub git_is_head_detached {
+	my $head_file = "$project/HEAD";
+	return if -l $head_file; # symlink
+	open my $fd, '<', $head_file
+		or return;
+	my $head_hash = <$fd>;
+	close $fd;
+	return if $head_hash =~ /^ref: /;
+	return $head_hash;
+}

Alternate solution would be to create git_get_symbolic_ref, and use
"!defined $current_branch" in place of "git_is_head_detached()".

> +
>  # get type of given object
>  sub git_get_type {
>  	my $hash = shift;
> @@ -2673,11 +2680,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');

Hmmm... git-log, git-show, or perhaps parse_commit?

>  			my ($hash, $epoch, $title) = split("\n", qx(@x), 3);
>  
>  			$ref_item{'class'} = 'head';
> -- 
> 1.5.6.5
> 
> 

-- 
Jakub Narebski
Poland