Re: [PATCH v2 09/11] gitweb: git_is_head_detached() function
- From
Jakub Narebski <jnareb@gmail.com>
- Date
- Nov 15, 2008, 23:43 UTC
- Message-ID
- <200811160043.46017.jnareb@gmail.com>
- In-Reply-To
- <1226616555-24503-10-git-send-email-giuseppe.bilotta@gmail.com>
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 <giuseppe.bilotta@gmail.com>
In my opinion this patch should really be squashed together with previous one. They belong together.
Show 18 quoted lines
> ---
> 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()".
Show 15 quoted lines
> +
> # 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?
Show 7 quoted lines
> my ($hash, $epoch, $title) = split("\n", qx(@x), 3);
>
> $ref_item{'class'} = 'head';
> --
> 1.5.6.5
>
> -- Jakub Narebski Poland