Skip to content

Commit df2c37a

Browse files
Junio C HamanoKay Sievers
authored andcommitted
allow working in repositories with textual symref HEAD
1 parent d3a6fd9 commit df2c37a

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

gitweb.cgi

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,23 @@ sub git_get_type {
396396
return $type;
397397
}
398398

399+
sub git_read_head {
400+
my $project = shift;
401+
my $oENV = $ENV{'GIT_DIR'};
402+
my $retval = undef;
403+
$ENV{'GIT_DIR'} = "$projectroot/$project";
404+
if (open my $fd, "-|", "$gitbin/git-rev-parse", "--verify", "HEAD") {
405+
my $head = <$fd>;
406+
close $fd;
407+
chomp $head;
408+
if ($head =~ m/^[0-9a-fA-F]{40}$/) {
409+
$retval = $head;
410+
}
411+
}
412+
$ENV{'GIT_DIR'} = $oENV;
413+
return $retval;
414+
}
415+
399416
sub git_read_hash {
400417
my $path = shift;
401418

@@ -823,7 +840,7 @@ sub git_project_list {
823840
die_error(undef, "No project found.");
824841
}
825842
foreach my $pr (@list) {
826-
my $head = git_read_hash("$pr->{'path'}/HEAD");
843+
my $head = git_read_head($pr->{'path'});
827844
if (!defined $head) {
828845
next;
829846
}
@@ -994,7 +1011,7 @@ sub git_read_refs {
9941011

9951012
sub git_summary {
9961013
my $descr = git_read_description($project) || "none";
997-
my $head = git_read_hash("$project/HEAD");
1014+
my $head = git_read_head($project);
9981015
my %co = git_read_commit($head);
9991016
my %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});
10001017

@@ -1034,7 +1051,7 @@ sub git_summary {
10341051
"<tr><td>owner</td><td>$owner</td></tr>\n" .
10351052
"<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n" .
10361053
"</table>\n";
1037-
open my $fd, "-|", "$gitbin/git-rev-list --max-count=17 " . git_read_hash("$project/HEAD") or die_error(undef, "Open failed.");
1054+
open my $fd, "-|", "$gitbin/git-rev-list --max-count=17 " . git_read_head($project) or die_error(undef, "Open failed.");
10381055
my (@revlist) = map { chomp; $_ } <$fd>;
10391056
close $fd;
10401057
print "<div>\n" .
@@ -1172,7 +1189,7 @@ sub git_summary {
11721189
}
11731190

11741191
sub git_tag {
1175-
my $head = git_read_hash("$project/HEAD");
1192+
my $head = git_read_head($project);
11761193
git_header_html();
11771194
print "<div class=\"page_nav\">\n" .
11781195
$cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
@@ -1211,7 +1228,7 @@ sub git_tag {
12111228
}
12121229

12131230
sub git_tags {
1214-
my $head = git_read_hash("$project/HEAD");
1231+
my $head = git_read_head($project);
12151232
git_header_html();
12161233
print "<div class=\"page_nav\">\n" .
12171234
$cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
@@ -1270,7 +1287,7 @@ sub git_tags {
12701287
}
12711288

12721289
sub git_heads {
1273-
my $head = git_read_hash("$project/HEAD");
1290+
my $head = git_read_head($project);
12741291
git_header_html();
12751292
print "<div class=\"page_nav\">\n" .
12761293
$cgi->a({-href => "$my_uri?" . esc_param("p=$project;a=summary")}, "summary") .
@@ -1343,7 +1360,7 @@ sub git_get_hash_by_path {
13431360

13441361
sub git_blob {
13451362
if (!defined $hash && defined $file_name) {
1346-
my $base = $hash_base || git_read_hash("$project/HEAD");
1363+
my $base = $hash_base || git_read_head($project);
13471364
$hash = git_get_hash_by_path($base, $file_name, "blob") || die_error(undef, "Error lookup file.");
13481365
}
13491366
open my $fd, "-|", "$gitbin/git-cat-file blob $hash" or die_error(undef, "Open failed.");
@@ -1407,13 +1424,13 @@ sub git_blob_plain {
14071424

14081425
sub git_tree {
14091426
if (!defined $hash) {
1410-
$hash = git_read_hash("$project/HEAD");
1427+
$hash = git_read_head($project);
14111428
if (defined $file_name) {
1412-
my $base = $hash_base || git_read_hash("$project/HEAD");
1429+
my $base = $hash_base || $hash;
14131430
$hash = git_get_hash_by_path($base, $file_name, "tree");
14141431
}
14151432
if (!defined $hash_base) {
1416-
$hash_base = git_read_hash("$project/HEAD");
1433+
$hash_base = $hash;
14171434
}
14181435
}
14191436
$/ = "\0";
@@ -1497,7 +1514,7 @@ sub git_tree {
14971514

14981515
sub git_rss {
14991516
# http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
1500-
open my $fd, "-|", "$gitbin/git-rev-list --max-count=150 " . git_read_hash("$project/HEAD") or die_error(undef, "Open failed.");
1517+
open my $fd, "-|", "$gitbin/git-rev-list --max-count=150 " . git_read_head($project) or die_error(undef, "Open failed.");
15011518
my (@revlist) = map { chomp; $_ } <$fd>;
15021519
close $fd or die_error(undef, "Reading rev-list failed.");
15031520
print $cgi->header(-type => 'text/xml', -charset => 'utf-8');
@@ -1566,7 +1583,7 @@ sub git_opml {
15661583

15671584
foreach my $pr (@list) {
15681585
my %proj = %$pr;
1569-
my $head = git_read_hash("$proj{'path'}/HEAD");
1586+
my $head = git_read_head($proj{'path'});
15701587
if (!defined $head) {
15711588
next;
15721589
}
@@ -1587,7 +1604,7 @@ sub git_opml {
15871604
}
15881605

15891606
sub git_log {
1590-
my $head = git_read_hash("$project/HEAD");
1607+
my $head = git_read_head($project);
15911608
if (!defined $hash) {
15921609
$hash = $head;
15931610
}
@@ -2083,7 +2100,7 @@ sub git_commitdiff_plain {
20832100

20842101
sub git_history {
20852102
if (!defined $hash) {
2086-
$hash = git_read_hash("$project/HEAD");
2103+
$hash = git_read_head($project);
20872104
}
20882105
my %co = git_read_commit($hash);
20892106
if (!%co) {
@@ -2159,7 +2176,7 @@ sub git_search {
21592176
die_error("", "Text field empty.");
21602177
}
21612178
if (!defined $hash) {
2162-
$hash = git_read_hash("$project/HEAD");
2179+
$hash = git_read_head($project);
21632180
}
21642181
my %co = git_read_commit($hash);
21652182
if (!%co) {
@@ -2300,7 +2317,7 @@ sub git_search {
23002317
}
23012318

23022319
sub git_shortlog {
2303-
my $head = git_read_hash("$project/HEAD");
2320+
my $head = git_read_head($project);
23042321
if (!defined $hash) {
23052322
$hash = $head;
23062323
}

0 commit comments

Comments
 (0)