{"thread":{"id":"5214","subject":"How to resolve git-am conflict (possible bug)","startedAt":"2006-08-12T00:30:48Z","lastAt":"2006-08-12T19:49:38Z","messageCount":8,"participants":["Jakub Narebski","Junio C Hamano","Johannes Schindelin"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"25147","messageId":"ebj7er$64j$1@sea.gmane.org","threadId":"5214","inReplyTo":null,"subject":"How to resolve git-am conflict (possible bug)","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2006-08-12T00:30:48Z","receivedAt":"2006-08-12T00:30:48Z","isPatch":false,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"When trying to pally patch generated by qgit (in attachement) to current\nversion of my private development branch (current version of file to be\npatched in attachement), I have encountered git-am patch conflict.\n\nFirst, there is no mention of _how_ to resolve conflict in git-am, besides\nthe fact that you need to git-update-index resolved files.\n\nSecond, in my case git-am --3way didn't leave merge conflict markers in the\nfile, nor did it write .rej file, nor did it put different versions od\naffected file in stages 1,2,3. Sometimes it did remove affected file (can't\nconfirm), but it didn't leave <file>~<branch> files (as said by GyrosGeier\non #git).\n\nThird, I wonder why it printed the same error message _twice_. See below for\nthe sequence of commands and it's output\n\n---\n1078:jnareb@roke:~/git> rm -rf .dotest/\n1079:jnareb@roke:~/git> git reset --hard\n1080:jnareb@roke:~/git> git am --3way --utf8 --keep --binary \\\n  mdir.0/0001-gitweb-Refactor-git_history_body.txt\n\nApplying '[PATCH 1/1] gitweb: Refactor git_history_body'\n\nerror: patch failed: gitweb/gitweb.perl:2295\nerror: gitweb/gitweb.perl: patch does not apply\nerror: patch failed: gitweb/gitweb.perl:2295\nerror: gitweb/gitweb.perl: patch does not apply\nPatch failed at 0001.\nWhen you have resolved this problem run \"git-am -3 --resolved\".\nIf you would prefer to skip this patch, instead run \"git-am -3 --skip\".\n1081:jnareb@roke:~/git> ll gitweb/gitweb.perl*\n-rwxr-xr-x  1 jnareb users 80187 Aug 11 15:18 gitweb/gitweb.perl\n1082:jnareb@roke:~/git> git ls-files -s gitweb/gitweb.perl\n100755 d917bdf68de2859c805edc9f6fb9ebba1e9ad7d1 0       gitweb/gitweb.perl\n1084:jnareb@roke:~/git> git grep \">>>>\" gitweb/gitweb.perl || \n  echo \"Not found\"\nNot found\n\n-- \nJakub Narebski\nWarsaw, Poland\nShadeHawk on #git\n\n\n>From c03bddd0fef127b21f9253a70ca4bd1d31212562 Mon Sep 17 00:00:00 2001\nFrom: Jakub Narebski <jnareb@gmail.com>\nDate: Sun, 6 Aug 2006 01:23:17 +0200\nSubject: [PATCH 1/1] gitweb: Refactor git_history_body\n\nSeparates main part of git_history into git_history_body subroutine,\nand make output more similar to git_shortlog.\n\nSigned-off-by: Jakub Narebski <jnareb@gmail.com>\n---\n gitweb/gitweb.perl |   93 ++++++++++++++++++++++++++++++++--------------------\n 1 files changed, 57 insertions(+), 36 deletions(-)\n\ndiff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl\nindex 7ea52b1..2a39145 100755\n--- a/gitweb/gitweb.perl\n+++ b/gitweb/gitweb.perl\n@@ -1124,6 +1124,62 @@ sub git_shortlog_body {\n \tprint \"</table>\\n\";\n }\n \n+sub git_history_body {\n+\t# Warning: assumes constant type (blob or tree) during history\n+\tmy ($fd, $refs, $hash_base, $ftype, $extra) = @_;\n+\n+\tprint \"<table class=\\\"history\\\" cellspacing=\\\"0\\\">\\n\";\n+\tmy $alternate = 0;\n+\twhile (my $line = <$fd>) {\n+\t\tif ($line !~ m/^([0-9a-fA-F]{40})/) {\n+\t\t\tnext;\n+\t\t}\n+\n+\t\tmy $commit = $1;\n+\t\tmy %co = git_read_commit($commit);\n+\t\tif (!%co) {\n+\t\t\tnext;\n+\t\t}\n+\n+\t\tmy $ref = git_get_referencing($refs, $commit);\n+\n+\t\tif ($alternate) {\n+\t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n+\t\t} else {\n+\t\t\tprint \"<tr class=\\\"light\\\">\\n\";\n+\t\t}\n+\t\t$alternate ^= 1;\n+\t\tprint \"<td title=\\\"$co{'age_string_age'}\\\"><i>$co{'age_string_date'}</i></td>\\n\" .\n+\t\t      # shortlog uses      chop_str($co{'author_name'}, 10)\n+\t\t      \"<td><i>\" . esc_html(chop_str($co{'author_name'}, 15, 3)) . \"</i></td>\\n\" .\n+\t\t      \"<td>\";\n+\t\t# originally git_history used chop_str($co{'title'}, 50)\n+\t\tprint_title_html($co{'title'}, $co{'title_short'}, \"p=$project;a=commit;h=$commit\", 1, $ref);\n+\t\tprint \"</td>\\n\" .\n+\t\t      \"<td class=\\\"link\\\">\" .\n+\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$commit\")}, \"commit\") . \" | \" .\n+\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commitdiff;h=$commit\")}, \"commitdiff\") . \" | \" .\n+\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=$ftype;hb=$commit;f=$file_name\")}, $ftype);\n+\n+\t\tmy $blob_current = git_get_hash_by_path($hash_base, $file_name);\n+\t\tmy $blob_parent  = git_get_hash_by_path($commit, $file_name);\n+\t\tif (defined $blob_current && defined $blob_parent &&\n+\t\t\t\t$blob_current ne $blob_parent) {\n+\t\t\tprint \" | \" .\n+\t\t\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blobdiff;h=$blob_current;hp=$blob_parent;hb=$commit;f=$file_name\")},\n+\t\t\t\t\"diff to current\");\n+\t\t}\n+\t\tprint \"</td>\\n\" .\n+\t\t      \"</tr>\\n\";\n+\t}\n+\tif (defined $extra) {\n+\t\tprint \"<tr>\\n\" .\n+\t\t      \"<td colspan=\\\"4\\\">$extra</td>\\n\" .\n+\t\t      \"</tr>\\n\";\n+\t}\n+\tprint \"</table>\\n\";\n+}\n+\n sub git_tags_body {\n \t# uses global variable $project\n \tmy ($taglist, $from, $to, $extra) = @_;\n@@ -2295,42 +2351,7 @@ sub git_history {\n \n \topen my $fd, \"-|\",\n \t\t$GIT, \"rev-list\", \"--full-history\", $hash_base, \"--\", $file_name;\n-\tprint \"<table cellspacing=\\\"0\\\">\\n\";\n-\tmy $alternate = 0;\n-\twhile (my $line = <$fd>) {\n-\t\tif ($line =~ m/^([0-9a-fA-F]{40})/){\n-\t\t\tmy $commit = $1;\n-\t\t\tmy %co = git_read_commit($commit);\n-\t\t\tif (!%co) {\n-\t\t\t\tnext;\n-\t\t\t}\n-\t\t\tmy $ref = git_get_referencing($refs, $commit);\n-\t\t\tif ($alternate) {\n-\t\t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n-\t\t\t} else {\n-\t\t\t\tprint \"<tr class=\\\"light\\\">\\n\";\n-\t\t\t}\n-\t\t\t$alternate ^= 1;\n-\t\t\tprint \"<td title=\\\"$co{'age_string_age'}\\\"><i>$co{'age_string_date'}</i></td>\\n\" .\n-\t\t\t      \"<td><i>\" . esc_html(chop_str($co{'author_name'}, 15, 3)) . \"</i></td>\\n\" .\n-\t\t\t      \"<td>\" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$commit\"), -class => \"list\"}, \"<b>\" .\n-\t\t\t      esc_html(chop_str($co{'title'}, 50)) . \"$ref</b>\") . \"</td>\\n\" .\n-\t\t\t      \"<td class=\\\"link\\\">\" .\n-\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$commit\")}, \"commit\") .\n-\t\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commitdiff;h=$commit\")}, \"commitdiff\") .\n-\t\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=$ftype;hb=$commit;f=$file_name\")}, $ftype);\n-\t\t\tmy $blob = git_get_hash_by_path($hash_base, $file_name);\n-\t\t\tmy $blob_parent = git_get_hash_by_path($commit, $file_name);\n-\t\t\tif (defined $blob && defined $blob_parent && $blob ne $blob_parent) {\n-\t\t\t\tprint \" | \" .\n-\t\t\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name\")},\n-\t\t\t\t\"diff to current\");\n-\t\t\t}\n-\t\t\tprint \"</td>\\n\" .\n-\t\t\t      \"</tr>\\n\";\n-\t\t}\n-\t}\n-\tprint \"</table>\\n\";\n+\tgit_history_body($fd, $refs, $hash_base, $ftype);\n \tclose $fd;\n \tgit_footer_html();\n }\n-- \n1.4.1.1\n\n\n\n#!/usr/bin/perl\n\n# gitweb - simple web interface to track changes in git repositories\n#\n# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org>\n# (C) 2005, Christian Gierke\n#\n# This program is licensed under the GPLv2\n\nuse strict;\nuse warnings;\nuse CGI qw(:standard :escapeHTML -nosticky);\nuse CGI::Util qw(unescape);\nuse CGI::Carp qw(fatalsToBrowser);\nuse Encode;\nuse Fcntl ':mode';\nuse File::Find qw();\nbinmode STDOUT, ':utf8';\n\nour $cgi = new CGI;\nour $version = \"++GIT_VERSION++\";\nour $my_url = $cgi->url();\nour $my_uri = $cgi->url(-absolute => 1);\n\n# core git executable to use\n# this can just be \"git\" if your webserver has a sensible PATH\nour $GIT = \"++GIT_BINDIR++/git\";\n\n# absolute fs-path which will be prepended to the project path\n#our $projectroot = \"/pub/scm\";\nour $projectroot = \"++GITWEB_PROJECTROOT++\";\n\n# location for temporary files needed for diffs\nour $git_temp = \"/tmp/gitweb\";\n\n# target of the home link on top of all pages\nour $home_link = $my_uri;\n\n# name of your site or organization to appear in page titles\n# replace this with something more descriptive for clearer bookmarks\nour $site_name = \"++GITWEB_SITENAME++\" || $ENV{'SERVER_NAME'} || \"Untitled\";\n\n# html text to include at home page\nour $home_text = \"++GITWEB_HOMETEXT++\";\n\n# URI of default stylesheet\nour $stylesheet = \"++GITWEB_CSS++\";\n# URI of GIT logo\nour $logo = \"++GITWEB_LOGO++\";\n\n# source of projects list\nour $projects_list = \"++GITWEB_LIST++\";\n\n# default blob_plain mimetype and default charset for text/plain blob\nour $default_blob_plain_mimetype = 'text/plain';\nour $default_text_plain_charset  = undef;\n\n# file to use for guessing MIME types before trying /etc/mime.types\n# (relative to the current git repository)\nour $mimetypes_file = undef;\n\nour $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || \"++GITWEB_CONFIG++\";\nrequire $GITWEB_CONFIG if -e $GITWEB_CONFIG;\n\n# version of the core git binary\nour $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : \"unknown\";\n\n$projects_list ||= $projectroot;\nif (! -d $git_temp) {\n\tmkdir($git_temp, 0700) || die_error(undef, \"Couldn't mkdir $git_temp\");\n}\n\n# ======================================================================\n# input validation and dispatch\nour $action = $cgi->param('a');\nif (defined $action) {\n\tif ($action =~ m/[^0-9a-zA-Z\\.\\-_]/) {\n\t\tdie_error(undef, \"Invalid action parameter\");\n\t}\n\t# action which does not check rest of parameters\n\tif ($action eq \"opml\") {\n\t\tgit_opml();\n\t\texit;\n\t}\n}\n\nour $project = ($cgi->param('p') || $ENV{'PATH_INFO'});\nif (defined $project) {\n\t$project =~ s|^/||;\n\t$project =~ s|/$||;\n}\nif (defined $project && $project) {\n\tif (!validate_input($project)) {\n\t\tdie_error(undef, \"Invalid project parameter\");\n\t}\n\tif (!(-d \"$projectroot/$project\")) {\n\t\tdie_error(undef, \"No such directory\");\n\t}\n\tif (!(-e \"$projectroot/$project/HEAD\")) {\n\t\tdie_error(undef, \"No such project\");\n\t}\n\t$ENV{'GIT_DIR'} = \"$projectroot/$project\";\n} else {\n\tgit_project_list();\n\texit;\n}\n\nour $file_name = $cgi->param('f');\nif (defined $file_name) {\n\tif (!validate_input($file_name)) {\n\t\tdie_error(undef, \"Invalid file parameter\");\n\t}\n}\n\nour $hash = $cgi->param('h');\nif (defined $hash) {\n\tif (!validate_input($hash)) {\n\t\tdie_error(undef, \"Invalid hash parameter\");\n\t}\n}\n\nour $hash_parent = $cgi->param('hp');\nif (defined $hash_parent) {\n\tif (!validate_input($hash_parent)) {\n\t\tdie_error(undef, \"Invalid hash parent parameter\");\n\t}\n}\n\nour $hash_base = $cgi->param('hb');\nif (defined $hash_base) {\n\tif (!validate_input($hash_base)) {\n\t\tdie_error(undef, \"Invalid hash base parameter\");\n\t}\n}\n\nour $page = $cgi->param('pg');\nif (defined $page) {\n\tif ($page =~ m/[^0-9]$/) {\n\t\tdie_error(undef, \"Invalid page parameter\");\n\t}\n}\n\nour $searchtext = $cgi->param('s');\nif (defined $searchtext) {\n\tif ($searchtext =~ m/[^a-zA-Z0-9_\\.\\/\\-\\+\\:\\@ ]/) {\n\t\tdie_error(undef, \"Invalid search parameter\");\n\t}\n\t$searchtext = quotemeta $searchtext;\n}\n\n# dispatch\nmy %actions = (\n\t\"blame\" => \\&git_blame2,\n\t\"blobdiff\" => \\&git_blobdiff,\n\t\"blobdiff_plain\" => \\&git_blobdiff_plain,\n\t\"blob\" => \\&git_blob,\n\t\"blob_plain\" => \\&git_blob_plain,\n\t\"commitdiff\" => \\&git_commitdiff,\n\t\"commitdiff_plain\" => \\&git_commitdiff_plain,\n\t\"commit\" => \\&git_commit,\n\t\"heads\" => \\&git_heads,\n\t\"history\" => \\&git_history,\n\t\"log\" => \\&git_log,\n\t\"rss\" => \\&git_rss,\n\t\"search\" => \\&git_search,\n\t\"shortlog\" => \\&git_shortlog,\n\t\"summary\" => \\&git_summary,\n\t\"tag\" => \\&git_tag,\n\t\"tags\" => \\&git_tags,\n\t\"tree\" => \\&git_tree,\n);\n\n$action = 'summary' if (!defined($action));\nif (!defined($actions{$action})) {\n\tdie_error(undef, \"Unknown action\");\n}\n$actions{$action}->();\nexit;\n\n## ======================================================================\n## validation, quoting/unquoting and escaping\n\nsub validate_input {\n\tmy $input = shift;\n\n\tif ($input =~ m/^[0-9a-fA-F]{40}$/) {\n\t\treturn $input;\n\t}\n\tif ($input =~ m/(^|\\/)(|\\.|\\.\\.)($|\\/)/) {\n\t\treturn undef;\n\t}\n\tif ($input =~ m/[^a-zA-Z0-9_\\x80-\\xff\\ \\t\\.\\/\\-\\+\\#\\~\\%]/) {\n\t\treturn undef;\n\t}\n\treturn $input;\n}\n\n# quote unsafe chars, but keep the slash, even when it's not\n# correct, but quoted slashes look too horrible in bookmarks\nsub esc_param {\n\tmy $str = shift;\n\t$str =~ s/([^A-Za-z0-9\\-_.~();\\/;?:@&=])/sprintf(\"%%%02X\", ord($1))/eg;\n\t$str =~ s/\\+/%2B/g;\n\t$str =~ s/ /\\+/g;\n\treturn $str;\n}\n\n# replace invalid utf8 character with SUBSTITUTION sequence\nsub esc_html {\n\tmy $str = shift;\n\t$str = decode(\"utf8\", $str, Encode::FB_DEFAULT);\n\t$str = escapeHTML($str);\n\t$str =~ s/\\014/^L/g; # escape FORM FEED (FF) character (e.g. in COPYING file)\n\treturn $str;\n}\n\n# git may return quoted and escaped filenames\nsub unquote {\n\tmy $str = shift;\n\tif ($str =~ m/^\"(.*)\"$/) {\n\t\t$str = $1;\n\t\t$str =~ s/\\\\([0-7]{1,3})/chr(oct($1))/eg;\n\t}\n\treturn $str;\n}\n\n# escape tabs (convert tabs to spaces)\nsub untabify {\n\tmy $line = shift;\n\n\twhile ((my $pos = index($line, \"\\t\")) != -1) {\n\t\tif (my $count = (8 - ($pos % 8))) {\n\t\t\tmy $spaces = ' ' x $count;\n\t\t\t$line =~ s/\\t/$spaces/;\n\t\t}\n\t}\n\n\treturn $line;\n}\n\n## ----------------------------------------------------------------------\n## HTML aware string manipulation\n\nsub chop_str {\n\tmy $str = shift;\n\tmy $len = shift;\n\tmy $add_len = shift || 10;\n\n\t# allow only $len chars, but don't cut a word if it would fit in $add_len\n\t# if it doesn't fit, cut it if it's still longer than the dots we would add\n\t$str =~ m/^(.{0,$len}[^ \\/\\-_:\\.@]{0,$add_len})(.*)/;\n\tmy $body = $1;\n\tmy $tail = $2;\n\tif (length($tail) > 4) {\n\t\t$tail = \" ...\";\n\t\t$body =~ s/&[^;]*$//; # remove chopped character entities\n\t}\n\treturn \"$body$tail\";\n}\n\n## ----------------------------------------------------------------------\n## functions returning short strings\n\n# CSS class for given age value (in seconds)\nsub age_class {\n\tmy $age = shift;\n\n\tif ($age < 60*60*2) {\n\t\treturn \"age0\";\n\t} elsif ($age < 60*60*24*2) {\n\t\treturn \"age1\";\n\t} else {\n\t\treturn \"age2\";\n\t}\n}\n\n# convert age in seconds to \"nn units ago\" string\nsub age_string {\n\tmy $age = shift;\n\tmy $age_str;\n\n\tif ($age > 60*60*24*365*2) {\n\t\t$age_str = (int $age/60/60/24/365);\n\t\t$age_str .= \" years ago\";\n\t} elsif ($age > 60*60*24*(365/12)*2) {\n\t\t$age_str = int $age/60/60/24/(365/12);\n\t\t$age_str .= \" months ago\";\n\t} elsif ($age > 60*60*24*7*2) {\n\t\t$age_str = int $age/60/60/24/7;\n\t\t$age_str .= \" weeks ago\";\n\t} elsif ($age > 60*60*24*2) {\n\t\t$age_str = int $age/60/60/24;\n\t\t$age_str .= \" days ago\";\n\t} elsif ($age > 60*60*2) {\n\t\t$age_str = int $age/60/60;\n\t\t$age_str .= \" hours ago\";\n\t} elsif ($age > 60*2) {\n\t\t$age_str = int $age/60;\n\t\t$age_str .= \" min ago\";\n\t} elsif ($age > 2) {\n\t\t$age_str = int $age;\n\t\t$age_str .= \" sec ago\";\n\t} else {\n\t\t$age_str .= \" right now\";\n\t}\n\treturn $age_str;\n}\n\n# convert file mode in octal to symbolic file mode string\nsub mode_str {\n\tmy $mode = oct shift;\n\n\tif (S_ISDIR($mode & S_IFMT)) {\n\t\treturn 'drwxr-xr-x';\n\t} elsif (S_ISLNK($mode)) {\n\t\treturn 'lrwxrwxrwx';\n\t} elsif (S_ISREG($mode)) {\n\t\t# git cares only about the executable bit\n\t\tif ($mode & S_IXUSR) {\n\t\t\treturn '-rwxr-xr-x';\n\t\t} else {\n\t\t\treturn '-rw-r--r--';\n\t\t};\n\t} else {\n\t\treturn '----------';\n\t}\n}\n\n# convert file mode in octal to file type string\nsub file_type {\n\tmy $mode = oct shift;\n\n\tif (S_ISDIR($mode & S_IFMT)) {\n\t\treturn \"directory\";\n\t} elsif (S_ISLNK($mode)) {\n\t\treturn \"symlink\";\n\t} elsif (S_ISREG($mode)) {\n\t\treturn \"file\";\n\t} else {\n\t\treturn \"unknown\";\n\t}\n}\n\n## ----------------------------------------------------------------------\n## functions returning short HTML fragments, or transforming HTML fragments\n## which don't beling to other sections\n\n# format line of commit message or tag comment\nsub format_log_line_html {\n\tmy $line = shift;\n\n\t$line = esc_html($line);\n\t$line =~ s/ /&nbsp;/g;\n\tif ($line =~ m/([0-9a-fA-F]{40})/) {\n\t\tmy $hash_text = $1;\n\t\tif (git_get_type($hash_text) eq \"commit\") {\n\t\t\tmy $link = $cgi->a({-class => \"text\", -href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$hash_text\")}, $hash_text);\n\t\t\t$line =~ s/$hash_text/$link/;\n\t\t}\n\t}\n\treturn $line;\n}\n\n# format marker of refs pointing to given object\nsub git_get_referencing {\n\tmy ($refs, $id) = @_;\n\n\tif (defined $refs->{$id}) {\n\t\treturn ' <span class=\"tag\">' . esc_html($refs->{$id}) . '</span>';\n\t} else {\n\t\treturn \"\";\n\t}\n}\n\n# format, perhaps shortened and with markers, title line\nsub format_subject_html {\n\tmy ($long, $short, $query, $extra) = @_;\n\t$extra = '' unless defined($extra);\n\n\tif (length($short) < length($long)) {\n\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param($query),\n\t\t               -class => \"list\", -title => $long},\n\t\t      esc_html($short) . $extra;\n\t} else {\n\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param($query),\n\t\t               -class => \"list\"},\n\t\t      esc_html($long)  . $extra;\n\t}\n}\n\n## ----------------------------------------------------------------------\n## git utility subroutines, invoking git commands\n\n# get HEAD ref of given project as hash\nsub git_read_head {\n\tmy $project = shift;\n\tmy $oENV = $ENV{'GIT_DIR'};\n\tmy $retval = undef;\n\t$ENV{'GIT_DIR'} = \"$projectroot/$project\";\n\tif (open my $fd, \"-|\", $GIT, \"rev-parse\", \"--verify\", \"HEAD\") {\n\t\tmy $head = <$fd>;\n\t\tclose $fd;\n\t\tif (defined $head && $head =~ /^([0-9a-fA-F]{40})$/) {\n\t\t\t$retval = $1;\n\t\t}\n\t}\n\tif (defined $oENV) {\n\t\t$ENV{'GIT_DIR'} = $oENV;\n\t}\n\treturn $retval;\n}\n\n# get type of given object\nsub git_get_type {\n\tmy $hash = shift;\n\n\topen my $fd, \"-|\", $GIT, \"cat-file\", '-t', $hash or return;\n\tmy $type = <$fd>;\n\tclose $fd or return;\n\tchomp $type;\n\treturn $type;\n}\n\nsub git_get_project_config {\n\tmy $key = shift;\n\n\treturn unless ($key);\n\t$key =~ s/^gitweb\\.//;\n\treturn if ($key =~ m/\\W/);\n\n\tmy $val = qx($GIT repo-config --get gitweb.$key);\n\treturn ($val);\n}\n\nsub git_get_project_config_bool {\n\tmy $val = git_get_project_config (@_);\n\tif ($val and $val =~ m/true|yes|on/) {\n\t\treturn (1);\n\t}\n\treturn; # implicit false\n}\n\n# get hash of given path at given ref\nsub git_get_hash_by_path {\n\tmy $base = shift;\n\tmy $path = shift || return undef;\n\n\tmy $tree = $base;\n\n\topen my $fd, \"-|\", $GIT, \"ls-tree\", $base, \"--\", $path\n\t\tor die_error(undef, \"Open git-ls-tree failed\");\n\tmy $line = <$fd>;\n\tclose $fd or return undef;\n\n\t#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa\tpanic.c'\n\t$line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\\t(.+)$/;\n\treturn $3;\n}\n\n## ......................................................................\n## git utility functions, directly accessing git repository\n\n# assumes that PATH is not symref\nsub git_read_hash {\n\tmy $path = shift;\n\n\topen my $fd, \"$projectroot/$path\" or return undef;\n\tmy $head = <$fd>;\n\tclose $fd;\n\tchomp $head;\n\tif ($head =~ m/^[0-9a-fA-F]{40}$/) {\n\t\treturn $head;\n\t}\n}\n\nsub git_read_description {\n\tmy $path = shift;\n\n\topen my $fd, \"$projectroot/$path/description\" or return undef;\n\tmy $descr = <$fd>;\n\tclose $fd;\n\tchomp $descr;\n\treturn $descr;\n}\n\nsub git_read_projects {\n\tmy @list;\n\n\tif (-d $projects_list) {\n\t\t# search in directory\n\t\tmy $dir = $projects_list;\n\t\topendir my ($dh), $dir or return undef;\n\t\twhile (my $dir = readdir($dh)) {\n\t\t\tif (-e \"$projectroot/$dir/HEAD\") {\n\t\t\t\tmy $pr = {\n\t\t\t\t\tpath => $dir,\n\t\t\t\t};\n\t\t\t\tpush @list, $pr\n\t\t\t}\n\t\t}\n\t\tclosedir($dh);\n\t} elsif (-f $projects_list) {\n\t\t# read from file(url-encoded):\n\t\t# 'git%2Fgit.git Linus+Torvalds'\n\t\t# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'\n\t\t# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'\n\t\topen my ($fd), $projects_list or return undef;\n\t\twhile (my $line = <$fd>) {\n\t\t\tchomp $line;\n\t\t\tmy ($path, $owner) = split ' ', $line;\n\t\t\t$path = unescape($path);\n\t\t\t$owner = unescape($owner);\n\t\t\tif (!defined $path) {\n\t\t\t\tnext;\n\t\t\t}\n\t\t\tif (-e \"$projectroot/$path/HEAD\") {\n\t\t\t\tmy $pr = {\n\t\t\t\t\tpath => $path,\n\t\t\t\t\towner => decode(\"utf8\", $owner, Encode::FB_DEFAULT),\n\t\t\t\t};\n\t\t\t\tpush @list, $pr\n\t\t\t}\n\t\t}\n\t\tclose $fd;\n\t}\n\t@list = sort {$a->{'path'} cmp $b->{'path'}} @list;\n\treturn @list;\n}\n\nsub read_info_ref {\n\tmy $type = shift || \"\";\n\tmy %refs;\n\t# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c\trefs/tags/v2.6.11\n\t# c39ae07f393806ccf406ef966e9a15afc43cc36a\trefs/tags/v2.6.11^{}\n\topen my $fd, \"$projectroot/$project/info/refs\" or return;\n\twhile (my $line = <$fd>) {\n\t\tchomp $line;\n\t\t# attention: for $type == \"\" it saves only last path part of ref name\n\t\t# e.g. from 'refs/heads/jn/gitweb' it would leave only 'gitweb'\n\t\tif ($line =~ m/^([0-9a-fA-F]{40})\\t.*$type\\/([^\\^]+)/) {\n\t\t\tif (defined $refs{$1}) {\n\t\t\t\t$refs{$1} .= \" / $2\";\n\t\t\t} else {\n\t\t\t\t$refs{$1} = $2;\n\t\t\t}\n\t\t}\n\t}\n\tclose $fd or return;\n\treturn \\%refs;\n}\n\n## ----------------------------------------------------------------------\n## parse to hash functions\n\nsub date_str {\n\tmy $epoch = shift;\n\tmy $tz = shift || \"-0000\";\n\n\tmy %date;\n\tmy @months = (\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\");\n\tmy @days = (\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\");\n\tmy ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($epoch);\n\t$date{'hour'} = $hour;\n\t$date{'minute'} = $min;\n\t$date{'mday'} = $mday;\n\t$date{'day'} = $days[$wday];\n\t$date{'month'} = $months[$mon];\n\t$date{'rfc2822'} = sprintf \"%s, %d %s %4d %02d:%02d:%02d +0000\", $days[$wday], $mday, $months[$mon], 1900+$year, $hour ,$min, $sec;\n\t$date{'mday-time'} = sprintf \"%d %s %02d:%02d\", $mday, $months[$mon], $hour ,$min;\n\n\t$tz =~ m/^([+\\-][0-9][0-9])([0-9][0-9])$/;\n\tmy $local = $epoch + ((int $1 + ($2/60)) * 3600);\n\t($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($local);\n\t$date{'hour_local'} = $hour;\n\t$date{'minute_local'} = $min;\n\t$date{'tz_local'} = $tz;\n\treturn %date;\n}\n\nsub git_read_tag {\n\tmy $tag_id = shift;\n\tmy %tag;\n\tmy @comment;\n\n\topen my $fd, \"-|\", $GIT, \"cat-file\", \"tag\", $tag_id or return;\n\t$tag{'id'} = $tag_id;\n\twhile (my $line = <$fd>) {\n\t\tchomp $line;\n\t\tif ($line =~ m/^object ([0-9a-fA-F]{40})$/) {\n\t\t\t$tag{'object'} = $1;\n\t\t} elsif ($line =~ m/^type (.+)$/) {\n\t\t\t$tag{'type'} = $1;\n\t\t} elsif ($line =~ m/^tag (.+)$/) {\n\t\t\t$tag{'name'} = $1;\n\t\t} elsif ($line =~ m/^tagger (.*) ([0-9]+) (.*)$/) {\n\t\t\t$tag{'author'} = $1;\n\t\t\t$tag{'epoch'} = $2;\n\t\t\t$tag{'tz'} = $3;\n\t\t} elsif ($line =~ m/--BEGIN/) {\n\t\t\tpush @comment, $line;\n\t\t\tlast;\n\t\t} elsif ($line eq \"\") {\n\t\t\tlast;\n\t\t}\n\t}\n\tpush @comment, <$fd>;\n\t$tag{'comment'} = \\@comment;\n\tclose $fd or return;\n\tif (!defined $tag{'name'}) {\n\t\treturn\n\t};\n\treturn %tag\n}\n\nsub git_read_commit {\n\tmy $commit_id = shift;\n\tmy $commit_text = shift;\n\n\tmy @commit_lines;\n\tmy %co;\n\n\tif (defined $commit_text) {\n\t\t@commit_lines = @$commit_text;\n\t} else {\n\t\t$/ = \"\\0\";\n\t\topen my $fd, \"-|\", $GIT, \"rev-list\", \"--header\", \"--parents\", \"--max-count=1\", $commit_id or return;\n\t\t@commit_lines = split '\\n', <$fd>;\n\t\tclose $fd or return;\n\t\t$/ = \"\\n\";\n\t\tpop @commit_lines;\n\t}\n\tmy $header = shift @commit_lines;\n\tif (!($header =~ m/^[0-9a-fA-F]{40}/)) {\n\t\treturn;\n\t}\n\t($co{'id'}, my @parents) = split ' ', $header;\n\t$co{'parents'} = \\@parents;\n\t$co{'parent'} = $parents[0];\n\twhile (my $line = shift @commit_lines) {\n\t\tlast if $line eq \"\\n\";\n\t\tif ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {\n\t\t\t$co{'tree'} = $1;\n\t\t} elsif ($line =~ m/^author (.*) ([0-9]+) (.*)$/) {\n\t\t\t$co{'author'} = $1;\n\t\t\t$co{'author_epoch'} = $2;\n\t\t\t$co{'author_tz'} = $3;\n\t\t\tif ($co{'author'} =~ m/^([^<]+) </) {\n\t\t\t\t$co{'author_name'} = $1;\n\t\t\t} else {\n\t\t\t\t$co{'author_name'} = $co{'author'};\n\t\t\t}\n\t\t} elsif ($line =~ m/^committer (.*) ([0-9]+) (.*)$/) {\n\t\t\t$co{'committer'} = $1;\n\t\t\t$co{'committer_epoch'} = $2;\n\t\t\t$co{'committer_tz'} = $3;\n\t\t\t$co{'committer_name'} = $co{'committer'};\n\t\t\t$co{'committer_name'} =~ s/ <.*//;\n\t\t}\n\t}\n\tif (!defined $co{'tree'}) {\n\t\treturn;\n\t};\n\n\tforeach my $title (@commit_lines) {\n\t\t$title =~ s/^    //;\n\t\tif ($title ne \"\") {\n\t\t\t$co{'title'} = chop_str($title, 80, 5);\n\t\t\t# remove leading stuff of merges to make the interesting part visible\n\t\t\tif (length($title) > 50) {\n\t\t\t\t$title =~ s/^Automatic //;\n\t\t\t\t$title =~ s/^merge (of|with) /Merge ... /i;\n\t\t\t\tif (length($title) > 50) {\n\t\t\t\t\t$title =~ s/(http|rsync):\\/\\///;\n\t\t\t\t}\n\t\t\t\tif (length($title) > 50) {\n\t\t\t\t\t$title =~ s/(master|www|rsync)\\.//;\n\t\t\t\t}\n\t\t\t\tif (length($title) > 50) {\n\t\t\t\t\t$title =~ s/kernel.org:?//;\n\t\t\t\t}\n\t\t\t\tif (length($title) > 50) {\n\t\t\t\t\t$title =~ s/\\/pub\\/scm//;\n\t\t\t\t}\n\t\t\t}\n\t\t\t$co{'title_short'} = chop_str($title, 50, 5);\n\t\t\tlast;\n\t\t}\n\t}\n\t# remove added spaces\n\tforeach my $line (@commit_lines) {\n\t\t$line =~ s/^    //;\n\t}\n\t$co{'comment'} = \\@commit_lines;\n\n\tmy $age = time - $co{'committer_epoch'};\n\t$co{'age'} = $age;\n\t$co{'age_string'} = age_string($age);\n\tmy ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday) = gmtime($co{'committer_epoch'});\n\tif ($age > 60*60*24*7*2) {\n\t\t$co{'age_string_date'} = sprintf \"%4i-%02u-%02i\", 1900 + $year, $mon+1, $mday;\n\t\t$co{'age_string_age'} = $co{'age_string'};\n\t} else {\n\t\t$co{'age_string_date'} = $co{'age_string'};\n\t\t$co{'age_string_age'} = sprintf \"%4i-%02u-%02i\", 1900 + $year, $mon+1, $mday;\n\t}\n\treturn %co;\n}\n\n# parse ref from ref_file, given by ref_id, with given type\nsub parse_ref {\n\tmy $ref_file = shift;\n\tmy $ref_id = shift;\n\tmy $type = shift || git_get_type($ref_id);\n\tmy %ref_item;\n\n\t$ref_item{'type'} = $type;\n\t$ref_item{'id'} = $ref_id;\n\t$ref_item{'epoch'} = 0;\n\t$ref_item{'age'} = \"unknown\";\n\tif ($type eq \"tag\") {\n\t\tmy %tag = git_read_tag($ref_id);\n\t\t$ref_item{'comment'} = $tag{'comment'};\n\t\tif ($tag{'type'} eq \"commit\") {\n\t\t\tmy %co = git_read_commit($tag{'object'});\n\t\t\t$ref_item{'epoch'} = $co{'committer_epoch'};\n\t\t\t$ref_item{'age'} = $co{'age_string'};\n\t\t} elsif (defined($tag{'epoch'})) {\n\t\t\tmy $age = time - $tag{'epoch'};\n\t\t\t$ref_item{'epoch'} = $tag{'epoch'};\n\t\t\t$ref_item{'age'} = age_string($age);\n\t\t}\n\t\t$ref_item{'reftype'} = $tag{'type'};\n\t\t$ref_item{'name'} = $tag{'name'};\n\t\t$ref_item{'refid'} = $tag{'object'};\n\t} elsif ($type eq \"commit\"){\n\t\tmy %co = git_read_commit($ref_id);\n\t\t$ref_item{'reftype'} = \"commit\";\n\t\t$ref_item{'name'} = $ref_file;\n\t\t$ref_item{'title'} = $co{'title'};\n\t\t$ref_item{'refid'} = $ref_id;\n\t\t$ref_item{'epoch'} = $co{'committer_epoch'};\n\t\t$ref_item{'age'} = $co{'age_string'};\n\t} else {\n\t\t$ref_item{'reftype'} = $type;\n\t\t$ref_item{'name'} = $ref_file;\n\t\t$ref_item{'refid'} = $ref_id;\n\t}\n\n\treturn %ref_item;\n}\n\n## ......................................................................\n## parse to array of hashes functions\n\nsub git_read_refs {\n\tmy $ref_dir = shift;\n\tmy @reflist;\n\n\tmy @refs;\n\tmy $pfxlen = length(\"$projectroot/$project/$ref_dir\");\n\tFile::Find::find(sub {\n\t\treturn if (/^\\./);\n\t\tif (-f $_) {\n\t\t\tpush @refs, substr($File::Find::name, $pfxlen + 1);\n\t\t}\n\t}, \"$projectroot/$project/$ref_dir\");\n\n\tforeach my $ref_file (@refs) {\n\t\tmy $ref_id = git_read_hash(\"$project/$ref_dir/$ref_file\");\n\t\tmy $type = git_get_type($ref_id) || next;\n\t\tmy %ref_item = parse_ref($ref_file, $ref_id, $type);\n\n\t\tpush @reflist, \\%ref_item;\n\t}\n\t# sort refs by age\n\t@reflist = sort {$b->{'epoch'} <=> $a->{'epoch'}} @reflist;\n\treturn \\@reflist;\n}\n\n## ----------------------------------------------------------------------\n## filesystem-related functions\n\nsub get_file_owner {\n\tmy $path = shift;\n\n\tmy ($dev, $ino, $mode, $nlink, $st_uid, $st_gid, $rdev, $size) = stat($path);\n\tmy ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwuid($st_uid);\n\tif (!defined $gcos) {\n\t\treturn undef;\n\t}\n\tmy $owner = $gcos;\n\t$owner =~ s/[,;].*$//;\n\treturn decode(\"utf8\", $owner, Encode::FB_DEFAULT);\n}\n\n## ......................................................................\n## mimetype related functions\n\nsub mimetype_guess_file {\n\tmy $filename = shift;\n\tmy $mimemap = shift;\n\t-r $mimemap or return undef;\n\n\tmy %mimemap;\n\topen(MIME, $mimemap) or return undef;\n\twhile (<MIME>) {\n\t\tmy ($mime, $exts) = split(/\\t+/);\n\t\tif (defined $exts) {\n\t\t\tmy @exts = split(/\\s+/, $exts);\n\t\t\tforeach my $ext (@exts) {\n\t\t\t\t$mimemap{$ext} = $mime;\n\t\t\t}\n\t\t}\n\t}\n\tclose(MIME);\n\n\t$filename =~ /\\.(.*?)$/;\n\treturn $mimemap{$1};\n}\n\nsub mimetype_guess {\n\tmy $filename = shift;\n\tmy $mime;\n\t$filename =~ /\\./ or return undef;\n\n\tif ($mimetypes_file) {\n\t\tmy $file = $mimetypes_file;\n\t\t#$file =~ m#^/# or $file = \"$projectroot/$path/$file\";\n\t\t$mime = mimetype_guess_file($filename, $file);\n\t}\n\t$mime ||= mimetype_guess_file($filename, '/etc/mime.types');\n\treturn $mime;\n}\n\nsub git_blob_plain_mimetype {\n\tmy $fd = shift;\n\tmy $filename = shift;\n\n\tif ($filename) {\n\t\tmy $mime = mimetype_guess($filename);\n\t\t$mime and return $mime;\n\t}\n\n\t# just in case\n\treturn $default_blob_plain_mimetype unless $fd;\n\n\tif (-T $fd) {\n\t\treturn 'text/plain' .\n\t\t       ($default_text_plain_charset ? '; charset='.$default_text_plain_charset : '');\n\t} elsif (! $filename) {\n\t\treturn 'application/octet-stream';\n\t} elsif ($filename =~ m/\\.png$/i) {\n\t\treturn 'image/png';\n\t} elsif ($filename =~ m/\\.gif$/i) {\n\t\treturn 'image/gif';\n\t} elsif ($filename =~ m/\\.jpe?g$/i) {\n\t\treturn 'image/jpeg';\n\t} else {\n\t\treturn 'application/octet-stream';\n\t}\n}\n\n## ======================================================================\n## functions printing HTML: header, footer, error page\n\nsub git_header_html {\n\tmy $status = shift || \"200 OK\";\n\tmy $expires = shift;\n\n\tmy $title = \"$site_name git\";\n\tif (defined $project) {\n\t\t$title .= \" - $project\";\n\t\tif (defined $action) {\n\t\t\t$title .= \"/$action\";\n\t\t\tif (defined $file_name) {\n\t\t\t\t$title .= \" - $file_name\";\n\t\t\t\tif ($action eq \"tree\" && $file_name !~ m|/$|) {\n\t\t\t\t\t$title .= \"/\";\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tmy $content_type;\n\t# require explicit support from the UA if we are to send the page as\n\t# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.\n\t# we have to do this because MSIE sometimes globs '*/*', pretending to\n\t# support xhtml+xml but choking when it gets what it asked for.\n\tif (defined $cgi->http('HTTP_ACCEPT') && $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\\s|^)application\\/xhtml\\+xml(,|;|\\s|$)/ && $cgi->Accept('application/xhtml+xml') != 0) {\n\t\t$content_type = 'application/xhtml+xml';\n\t} else {\n\t\t$content_type = 'text/html';\n\t}\n\tprint $cgi->header(-type=>$content_type, -charset => 'utf-8', -status=> $status, -expires => $expires);\n\tprint <<EOF;\n<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\" lang=\"en-US\">\n<!-- git web interface v$version, (C) 2005-2006, Kay Sievers <kay.sievers\\@vrfy.org>, Christian Gierke -->\n<!-- git core binaries version $git_version -->\n<head>\n<meta http-equiv=\"content-type\" content=\"$content_type; charset=utf-8\"/>\n<meta name=\"robots\" content=\"index, nofollow\"/>\n<title>$title</title>\n<link rel=\"stylesheet\" type=\"text/css\" href=\"$stylesheet\"/>\nEOF\n\tif (defined $project) {\n\t\tprintf('<link rel=\"alternate\" title=\"%s log\" '.\n\t\t       'href=\"%s\" type=\"application/rss+xml\"/>'.\"\\n\",\n\t\t       esc_param($project),\n\t\t       esc_param(\"$my_uri?p=$project;a=rss\"));\n\t}\n\n\tprint \"</head>\\n\" .\n\t      \"<body>\\n\" .\n\t      \"<div class=\\\"page_header\\\">\\n\" .\n\t      \"<a href=\\\"http://www.kernel.org/pub/software/scm/git/docs/\\\" title=\\\"git documentation\\\">\" .\n\t      \"<img src=\\\"$logo\\\" width=\\\"72\\\" height=\\\"27\\\" alt=\\\"git\\\" style=\\\"float:right; border-width:0px;\\\"/>\" .\n\t      \"</a>\\n\";\n\tprint $cgi->a({-href => esc_param($home_link)}, \"projects\") . \" / \";\n\tif (defined $project) {\n\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=summary\")}, esc_html($project));\n\t\tif (defined $action) {\n\t\t\tprint \" / $action\";\n\t\t}\n\t\tprint \"\\n\";\n\t\tif (!defined $searchtext) {\n\t\t\t$searchtext = \"\";\n\t\t}\n\t\tmy $search_hash;\n\t\tif (defined $hash_base) {\n\t\t\t$search_hash = $hash_base;\n\t\t} elsif (defined $hash) {\n\t\t\t$search_hash = $hash;\n\t\t} else {\n\t\t\t$search_hash = \"HEAD\";\n\t\t}\n\t\t$cgi->param(\"a\", \"search\");\n\t\t$cgi->param(\"h\", $search_hash);\n\t\tprint $cgi->startform(-method => \"get\", -action => $my_uri) .\n\t\t      \"<div class=\\\"search\\\">\\n\" .\n\t\t      $cgi->hidden(-name => \"p\") . \"\\n\" .\n\t\t      $cgi->hidden(-name => \"a\") . \"\\n\" .\n\t\t      $cgi->hidden(-name => \"h\") . \"\\n\" .\n\t\t      $cgi->textfield(-name => \"s\", -value => $searchtext) . \"\\n\" .\n\t\t      \"</div>\" .\n\t\t      $cgi->end_form() . \"\\n\";\n\t}\n\tprint \"</div>\\n\";\n}\n\nsub git_footer_html {\n\tprint \"<div class=\\\"page_footer\\\">\\n\";\n\tif (defined $project) {\n\t\tmy $descr = git_read_description($project);\n\t\tif (defined $descr) {\n\t\t\tprint \"<div class=\\\"page_footer_text\\\">\" . esc_html($descr) . \"</div>\\n\";\n\t\t}\n\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=rss\"), -class => \"rss_logo\"}, \"RSS\") . \"\\n\";\n\t} else {\n\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"a=opml\"), -class => \"rss_logo\"}, \"OPML\") . \"\\n\";\n\t}\n\tprint \"</div>\\n\" .\n\t      \"</body>\\n\" .\n\t      \"</html>\";\n}\n\nsub die_error {\n\tmy $status = shift || \"403 Forbidden\";\n\tmy $error = shift || \"Malformed query, file missing or permission denied\";\n\n\tgit_header_html($status);\n\tprint \"<div class=\\\"page_body\\\">\\n\" .\n\t      \"<br/><br/>\\n\" .\n\t      \"$status - $error\\n\" .\n\t      \"<br/>\\n\" .\n\t      \"</div>\\n\";\n\tgit_footer_html();\n\texit;\n}\n\n## ----------------------------------------------------------------------\n## functions printing or outputting HTML: navigation\n\nsub git_page_nav {\n\tmy ($current, $suppress, $head, $treehead, $treebase, $extra) = @_;\n\t$extra = '' if !defined $extra; # pager or formats\n\n\tmy @navs = qw(summary shortlog log commit commitdiff tree);\n\tif ($suppress) {\n\t\t@navs = grep { $_ ne $suppress } @navs;\n\t}\n\n\tmy %arg = map { $_, ''} @navs;\n\tif (defined $head) {\n\t\tfor (qw(commit commitdiff)) {\n\t\t\t$arg{$_} = \";h=$head\";\n\t\t}\n\t\tif ($current =~ m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {\n\t\t\tfor (qw(shortlog log)) {\n\t\t\t\t$arg{$_} = \";h=$head\";\n\t\t\t}\n\t\t}\n\t}\n\t$arg{tree} .= \";h=$treehead\" if defined $treehead;\n\t$arg{tree} .= \";hb=$treebase\" if defined $treebase;\n\n\tprint \"<div class=\\\"page_nav\\\">\\n\" .\n\t\t(join \" | \",\n\t\t map { $_ eq $current\n\t\t\t\t\t ? $_\n\t\t\t\t\t : $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=$_$arg{$_}\")}, \"$_\")\n\t\t\t\t }\n\t\t @navs);\n\tprint \"<br/>\\n$extra<br/>\\n\" .\n\t      \"</div>\\n\";\n}\n\nsub git_get_paging_nav {\n\tmy ($action, $hash, $head, $page, $nrevs) = @_;\n\tmy $paging_nav;\n\n\n\tif ($hash ne $head || $page) {\n\t\t$paging_nav .= $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=$action\")}, \"HEAD\");\n\t} else {\n\t\t$paging_nav .= \"HEAD\";\n\t}\n\n\tif ($page > 0) {\n\t\t$paging_nav .= \" &sdot; \" .\n\t\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=$action;h=$hash;pg=\" . ($page-1)),\n\t\t\t         -accesskey => \"p\", -title => \"Alt-p\"}, \"prev\");\n\t} else {\n\t\t$paging_nav .= \" &sdot; prev\";\n\t}\n\n\tif ($nrevs >= (100 * ($page+1)-1)) {\n\t\t$paging_nav .= \" &sdot; \" .\n\t\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=$action;h=$hash;pg=\" . ($page+1)),\n\t\t\t         -accesskey => \"n\", -title => \"Alt-n\"}, \"next\");\n\t} else {\n\t\t$paging_nav .= \" &sdot; next\";\n\t}\n\n\treturn $paging_nav;\n}\n\n## ......................................................................\n## functions printing or outputting HTML: div\n\nsub git_header_div {\n\tmy ($action, $title, $hash, $hash_base) = @_;\n\tmy $rest = '';\n\n\t$rest .= \";h=$hash\" if $hash;\n\t$rest .= \";hb=$hash_base\" if $hash_base;\n\n\tprint \"<div class=\\\"header\\\">\\n\" .\n\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=$action$rest\"),\n\t               -class => \"title\"}, $title ? $title : $action) . \"\\n\" .\n\t      \"</div>\\n\";\n}\n\nsub git_print_page_path {\n\tmy $name = shift;\n\tmy $type = shift;\n\n\tif (!defined $name) {\n\t\tprint \"<div class=\\\"page_path\\\"><b>/</b></div>\\n\";\n\t} elsif (defined $type && $type eq 'blob') {\n\t\tprint \"<div class=\\\"page_path\\\"><b>\" .\n\t\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob_plain;f=$file_name\")}, esc_html($name)) . \"</b><br/></div>\\n\";\n\t} else {\n\t\tprint \"<div class=\\\"page_path\\\"><b>\" . esc_html($name) . \"</b><br/></div>\\n\";\n\t}\n}\n\n## ......................................................................\n## functions printing large fragments of HTML\n\nsub git_shortlog_body {\n\t# uses global variable $project\n\tmy ($revlist, $from, $to, $refs, $extra) = @_;\n\t$from = 0 unless defined $from;\n\t$to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);\n\n\tprint \"<table class=\\\"shortlog\\\" cellspacing=\\\"0\\\">\\n\";\n\tmy $alternate = 0;\n\tfor (my $i = $from; $i <= $to; $i++) {\n\t\tmy $commit = $revlist->[$i];\n\t\t#my $ref = defined $refs ? git_get_referencing($refs, $commit) : '';\n\t\tmy $ref = git_get_referencing($refs, $commit);\n\t\tmy %co = git_read_commit($commit);\n\t\tif ($alternate) {\n\t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n\t\t} else {\n\t\t\tprint \"<tr class=\\\"light\\\">\\n\";\n\t\t}\n\t\t$alternate ^= 1;\n\t\t# git_summary() used print \"<td><i>$co{'age_string'}</i></td>\\n\" .\n\t\tprint \"<td title=\\\"$co{'age_string_age'}\\\"><i>$co{'age_string_date'}</i></td>\\n\" .\n\t\t      \"<td><i>\" . esc_html(chop_str($co{'author_name'}, 10)) . \"</i></td>\\n\" .\n\t\t      \"<td>\";\n\t\tprint format_subject_html($co{'title'}, $co{'title_short'}, \"p=$project;a=commit;h=$commit\", $ref);\n\t\tprint \"</td>\\n\" .\n\t\t      \"<td class=\\\"link\\\">\" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$commit\")}, \"commit\") . \" | \" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commitdiff;h=$commit\")}, \"commitdiff\") .\n\t\t      \"</td>\\n\" .\n\t\t      \"</tr>\\n\";\n\t}\n\tif (defined $extra) {\n\t\tprint \"<tr>\\n\" .\n\t\t      \"<td colspan=\\\"4\\\">$extra</td>\\n\" .\n\t\t      \"</tr>\\n\";\n\t}\n\tprint \"</table>\\n\";\n}\n\nsub git_tags_body {\n\t# uses global variable $project\n\tmy ($taglist, $from, $to, $extra) = @_;\n\t$from = 0 unless defined $from;\n\t$to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);\n\n\tprint \"<table class=\\\"tags\\\" cellspacing=\\\"0\\\">\\n\";\n\tmy $alternate = 0;\n\tfor (my $i = $from; $i <= $to; $i++) {\n\t\tmy $entry = $taglist->[$i];\n\t\tmy %tag = %$entry;\n\t\tmy $comment_lines = $tag{'comment'};\n\t\tmy $comment = shift @$comment_lines;\n\t\tmy $comment_short;\n\t\tif (defined $comment) {\n\t\t\t$comment_short = chop_str($comment, 30, 5);\n\t\t}\n\t\tif ($alternate) {\n\t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n\t\t} else {\n\t\t\tprint \"<tr class=\\\"light\\\">\\n\";\n\t\t}\n\t\t$alternate ^= 1;\n\t\tprint \"<td><i>$tag{'age'}</i></td>\\n\" .\n\t\t      \"<td>\" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=$tag{'reftype'};h=$tag{'refid'}\"),\n\t\t               -class => \"list\"}, \"<b>\" . esc_html($tag{'name'}) . \"</b>\") .\n\t\t      \"</td>\\n\" .\n\t\t      \"<td>\";\n\t\tif (defined $comment) {\n\t\t\tprint format_subject_html($comment, $comment_short, \"p=$project;a=tag;h=$tag{'id'}\");\n\t\t}\n\t\tprint \"</td>\\n\" .\n\t\t      \"<td class=\\\"selflink\\\">\";\n\t\tif ($tag{'type'} eq \"tag\") {\n\t\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=tag;h=$tag{'id'}\")}, \"tag\");\n\t\t} else {\n\t\t\tprint \"&nbsp;\";\n\t\t}\n\t\tprint \"</td>\\n\" .\n\t\t      \"<td class=\\\"link\\\">\" . \" | \" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=$tag{'reftype'};h=$tag{'refid'}\")}, $tag{'reftype'});\n\t\tif ($tag{'reftype'} eq \"commit\") {\n\t\t\tprint \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=shortlog;h=$tag{'name'}\")}, \"shortlog\") .\n\t\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=log;h=$tag{'refid'}\")}, \"log\");\n\t\t} elsif ($tag{'reftype'} eq \"blob\") {\n\t\t\tprint \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob_plain;h=$tag{'refid'}\")}, \"raw\");\n\t\t}\n\t\tprint \"</td>\\n\" .\n\t\t      \"</tr>\";\n\t}\n\tif (defined $extra) {\n\t\tprint \"<tr>\\n\" .\n\t\t      \"<td colspan=\\\"5\\\">$extra</td>\\n\" .\n\t\t      \"</tr>\\n\";\n\t}\n\tprint \"</table>\\n\";\n}\n\nsub git_heads_body {\n\t# uses global variable $project\n\tmy ($taglist, $head, $from, $to, $extra) = @_;\n\t$from = 0 unless defined $from;\n\t$to = $#{$taglist} if (!defined $to || $#{$taglist} < $to);\n\n\tprint \"<table class=\\\"heads\\\" cellspacing=\\\"0\\\">\\n\";\n\tmy $alternate = 0;\n\tfor (my $i = $from; $i <= $to; $i++) {\n\t\tmy $entry = $taglist->[$i];\n\t\tmy %tag = %$entry;\n\t\tmy $curr = $tag{'id'} eq $head;\n\t\tif ($alternate) {\n\t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n\t\t} else {\n\t\t\tprint \"<tr class=\\\"light\\\">\\n\";\n\t\t}\n\t\t$alternate ^= 1;\n\t\tprint \"<td><i>$tag{'age'}</i></td>\\n\" .\n\t\t      ($tag{'id'} eq $head ? \"<td class=\\\"current_head\\\">\" : \"<td>\") .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=shortlog;h=$tag{'name'}\"),\n\t\t               -class => \"list\"}, \"<b>\" . esc_html($tag{'name'}) . \"</b>\") .\n\t\t      \"</td>\\n\" .\n\t\t      \"<td class=\\\"link\\\">\" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=shortlog;h=$tag{'name'}\")}, \"shortlog\") . \" | \" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=log;h=$tag{'name'}\")}, \"log\") .\n\t\t      \"</td>\\n\" .\n\t\t      \"</tr>\";\n\t}\n\tif (defined $extra) {\n\t\tprint \"<tr>\\n\" .\n\t\t      \"<td colspan=\\\"3\\\">$extra</td>\\n\" .\n\t\t      \"</tr>\\n\";\n\t}\n\tprint \"</table>\\n\";\n}\n\n## ----------------------------------------------------------------------\n## functions printing large fragments, format as one of arguments\n\nsub git_diff_print {\n\tmy $from = shift;\n\tmy $from_name = shift;\n\tmy $to = shift;\n\tmy $to_name = shift;\n\tmy $format = shift || \"html\";\n\n\tmy $from_tmp = \"/dev/null\";\n\tmy $to_tmp = \"/dev/null\";\n\tmy $pid = $$;\n\n\t# create tmp from-file\n\tif (defined $from) {\n\t\t$from_tmp = \"$git_temp/gitweb_\" . $$ . \"_from\";\n\t\topen my $fd2, \"> $from_tmp\";\n\t\topen my $fd, \"-|\", $GIT, \"cat-file\", \"blob\", $from;\n\t\tmy @file = <$fd>;\n\t\tprint $fd2 @file;\n\t\tclose $fd2;\n\t\tclose $fd;\n\t}\n\n\t# create tmp to-file\n\tif (defined $to) {\n\t\t$to_tmp = \"$git_temp/gitweb_\" . $$ . \"_to\";\n\t\topen my $fd2, \"> $to_tmp\";\n\t\topen my $fd, \"-|\", $GIT, \"cat-file\", \"blob\", $to;\n\t\tmy @file = <$fd>;\n\t\tprint $fd2 @file;\n\t\tclose $fd2;\n\t\tclose $fd;\n\t}\n\n\topen my $fd, \"-|\", \"/usr/bin/diff -u -p -L \\'$from_name\\' -L \\'$to_name\\' $from_tmp $to_tmp\";\n\tif ($format eq \"plain\") {\n\t\tundef $/;\n\t\tprint <$fd>;\n\t\t$/ = \"\\n\";\n\t} else {\n\t\twhile (my $line = <$fd>) {\n\t\t\tchomp $line;\n\t\t\tmy $char = substr($line, 0, 1);\n\t\t\tmy $diff_class = \"\";\n\t\t\tif ($char eq '+') {\n\t\t\t\t$diff_class = \" add\";\n\t\t\t} elsif ($char eq \"-\") {\n\t\t\t\t$diff_class = \" rem\";\n\t\t\t} elsif ($char eq \"@\") {\n\t\t\t\t$diff_class = \" chunk_header\";\n\t\t\t} elsif ($char eq \"\\\\\") {\n\t\t\t\t# skip errors\n\t\t\t\tnext;\n\t\t\t}\n\t\t\t$line = untabify($line);\n\t\t\tprint \"<div class=\\\"diff$diff_class\\\">\" . esc_html($line) . \"</div>\\n\";\n\t\t}\n\t}\n\tclose $fd;\n\n\tif (defined $from) {\n\t\tunlink($from_tmp);\n\t}\n\tif (defined $to) {\n\t\tunlink($to_tmp);\n\t}\n}\n\n\n## ======================================================================\n## ======================================================================\n## actions\n\nsub git_project_list {\n\tmy $order = $cgi->param('o');\n\tif (defined $order && $order !~ m/project|descr|owner|age/) {\n\t\tdie_error(undef, \"Unknown order parameter\");\n\t}\n\n\tmy @list = git_read_projects();\n\tmy @projects;\n\tif (!@list) {\n\t\tdie_error(undef, \"No projects found\");\n\t}\n\tforeach my $pr (@list) {\n\t\tmy $head = git_read_head($pr->{'path'});\n\t\tif (!defined $head) {\n\t\t\tnext;\n\t\t}\n\t\t$ENV{'GIT_DIR'} = \"$projectroot/$pr->{'path'}\";\n\t\tmy %co = git_read_commit($head);\n\t\tif (!%co) {\n\t\t\tnext;\n\t\t}\n\t\t$pr->{'commit'} = \\%co;\n\t\tif (!defined $pr->{'descr'}) {\n\t\t\tmy $descr = git_read_description($pr->{'path'}) || \"\";\n\t\t\t$pr->{'descr'} = chop_str($descr, 25, 5);\n\t\t}\n\t\tif (!defined $pr->{'owner'}) {\n\t\t\t$pr->{'owner'} = get_file_owner(\"$projectroot/$pr->{'path'}\") || \"\";\n\t\t}\n\t\tpush @projects, $pr;\n\t}\n\n\tgit_header_html();\n\tif (-f $home_text) {\n\t\tprint \"<div class=\\\"index_include\\\">\\n\";\n\t\topen (my $fd, $home_text);\n\t\tprint <$fd>;\n\t\tclose $fd;\n\t\tprint \"</div>\\n\";\n\t}\n\tprint \"<table class=\\\"project_list\\\">\\n\" .\n\t      \"<tr>\\n\";\n\t$order ||= \"project\";\n\tif ($order eq \"project\") {\n\t\t@projects = sort {$a->{'path'} cmp $b->{'path'}} @projects;\n\t\tprint \"<th>Project</th>\\n\";\n\t} else {\n\t\tprint \"<th>\" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"o=project\"),\n\t\t               -class => \"header\"}, \"Project\") .\n\t\t      \"</th>\\n\";\n\t}\n\tif ($order eq \"descr\") {\n\t\t@projects = sort {$a->{'descr'} cmp $b->{'descr'}} @projects;\n\t\tprint \"<th>Description</th>\\n\";\n\t} else {\n\t\tprint \"<th>\" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"o=descr\"),\n\t\t               -class => \"header\"}, \"Description\") .\n\t\t      \"</th>\\n\";\n\t}\n\tif ($order eq \"owner\") {\n\t\t@projects = sort {$a->{'owner'} cmp $b->{'owner'}} @projects;\n\t\tprint \"<th>Owner</th>\\n\";\n\t} else {\n\t\tprint \"<th>\" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"o=owner\"),\n\t\t               -class => \"header\"}, \"Owner\") .\n\t\t      \"</th>\\n\";\n\t}\n\tif ($order eq \"age\") {\n\t\t@projects = sort {$a->{'commit'}{'age'} <=> $b->{'commit'}{'age'}} @projects;\n\t\tprint \"<th>Last Change</th>\\n\";\n\t} else {\n\t\tprint \"<th>\" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"o=age\"),\n\t\t               -class => \"header\"}, \"Last Change\") .\n\t\t      \"</th>\\n\";\n\t}\n\tprint \"<th></th>\\n\" .\n\t      \"</tr>\\n\";\n\tmy $alternate = 0;\n\tforeach my $pr (@projects) {\n\t\tif ($alternate) {\n\t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n\t\t} else {\n\t\t\tprint \"<tr class=\\\"light\\\">\\n\";\n\t\t}\n\t\t$alternate ^= 1;\n\t\tprint \"<td>\" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$pr->{'path'};a=summary\"),\n\t\t                        -class => \"list\"}, esc_html($pr->{'path'})) . \"</td>\\n\" .\n\t\t      \"<td>\" . esc_html($pr->{'descr'}) . \"</td>\\n\" .\n\t\t      \"<td><i>\" . chop_str($pr->{'owner'}, 15) . \"</i></td>\\n\";\n\t\tprint \"<td class=\\\"\". age_class($pr->{'commit'}{'age'}) . \"\\\">\" .\n\t\t      $pr->{'commit'}{'age_string'} . \"</td>\\n\" .\n\t\t      \"<td class=\\\"link\\\">\" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$pr->{'path'};a=summary\")}, \"summary\")   . \" | \" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$pr->{'path'};a=shortlog\")}, \"shortlog\") . \" | \" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$pr->{'path'};a=log\")}, \"log\") .\n\t\t      \"</td>\\n\" .\n\t\t      \"</tr>\\n\";\n\t}\n\tprint \"</table>\\n\";\n\tgit_footer_html();\n}\n\nsub git_summary {\n\tmy $descr = git_read_description($project) || \"none\";\n\tmy $head = git_read_head($project);\n\tmy %co = git_read_commit($head);\n\tmy %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});\n\n\tmy $owner;\n\tif (-f $projects_list) {\n\t\topen (my $fd , $projects_list);\n\t\twhile (my $line = <$fd>) {\n\t\t\tchomp $line;\n\t\t\tmy ($pr, $ow) = split ' ', $line;\n\t\t\t$pr = unescape($pr);\n\t\t\t$ow = unescape($ow);\n\t\t\tif ($pr eq $project) {\n\t\t\t\t$owner = decode(\"utf8\", $ow, Encode::FB_DEFAULT);\n\t\t\t\tlast;\n\t\t\t}\n\t\t}\n\t\tclose $fd;\n\t}\n\tif (!defined $owner) {\n\t\t$owner = get_file_owner(\"$projectroot/$project\");\n\t}\n\n\tmy $refs = read_info_ref();\n\tgit_header_html();\n\tgit_page_nav('summary','', $head);\n\n\tprint \"<div class=\\\"title\\\">&nbsp;</div>\\n\";\n\tprint \"<table cellspacing=\\\"0\\\">\\n\" .\n\t      \"<tr><td>description</td><td>\" . esc_html($descr) . \"</td></tr>\\n\" .\n\t      \"<tr><td>owner</td><td>$owner</td></tr>\\n\" .\n\t      \"<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\\n\" .\n\t      \"</table>\\n\";\n\n\topen my $fd, \"-|\", $GIT, \"rev-list\", \"--max-count=17\", git_read_head($project)\n\t\tor die_error(undef, \"Open git-rev-list failed\");\n\tmy @revlist = map { chomp; $_ } <$fd>;\n\tclose $fd;\n\tgit_header_div('shortlog');\n\tgit_shortlog_body(\\@revlist, 0, 15, $refs,\n\t                  $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=shortlog\")}, \"...\"));\n\n\tmy $taglist = git_read_refs(\"refs/tags\");\n\tif (defined @$taglist) {\n\t\tgit_header_div('tags');\n\t\tgit_tags_body($taglist, 0, 15,\n\t\t              $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=tags\")}, \"...\"));\n\t}\n\n\tmy $headlist = git_read_refs(\"refs/heads\");\n\tif (defined @$headlist) {\n\t\tgit_header_div('heads');\n\t\tgit_heads_body($headlist, $head, 0, 15,\n\t\t               $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=heads\")}, \"...\"));\n\t}\n\n\tgit_footer_html();\n}\n\nsub git_tag {\n\tmy $head = git_read_head($project);\n\tgit_header_html();\n\tgit_page_nav('','', $head,undef,$head);\n\tmy %tag = git_read_tag($hash);\n\tgit_header_div('commit', esc_html($tag{'name'}), $hash);\n\tprint \"<div class=\\\"title_text\\\">\\n\" .\n\t      \"<table cellspacing=\\\"0\\\">\\n\" .\n\t      \"<tr>\\n\" .\n\t      \"<td>object</td>\\n\" .\n\t      \"<td>\" . $cgi->a({-class => \"list\", -href => \"$my_uri?\" . esc_param(\"p=$project;a=$tag{'type'};h=$tag{'object'}\")}, $tag{'object'}) . \"</td>\\n\" .\n\t      \"<td class=\\\"link\\\">\" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=$tag{'type'};h=$tag{'object'}\")}, $tag{'type'}) . \"</td>\\n\" .\n\t      \"</tr>\\n\";\n\tif (defined($tag{'author'})) {\n\t\tmy %ad = date_str($tag{'epoch'}, $tag{'tz'});\n\t\tprint \"<tr><td>author</td><td>\" . esc_html($tag{'author'}) . \"</td></tr>\\n\";\n\t\tprint \"<tr><td></td><td>\" . $ad{'rfc2822'} . sprintf(\" (%02d:%02d %s)\", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'}) . \"</td></tr>\\n\";\n\t}\n\tprint \"</table>\\n\\n\" .\n\t      \"</div>\\n\";\n\tprint \"<div class=\\\"page_body\\\">\";\n\tmy $comment = $tag{'comment'};\n\tforeach my $line (@$comment) {\n\t\tprint esc_html($line) . \"<br/>\\n\";\n\t}\n\tprint \"</div>\\n\";\n\tgit_footer_html();\n}\n\nsub git_blame2 {\n\tmy $fd;\n\tmy $ftype;\n\tdie_error(undef, \"Permission denied\") if (!git_get_project_config_bool ('blame'));\n\tdie_error('404 Not Found', \"File name not defined\") if (!$file_name);\n\t$hash_base ||= git_read_head($project);\n\tdie_error(undef, \"Couldn't find base commit\") unless ($hash_base);\n\tmy %co = git_read_commit($hash_base)\n\t\tor die_error(undef, \"Reading commit failed\");\n\tif (!defined $hash) {\n\t\t$hash = git_get_hash_by_path($hash_base, $file_name, \"blob\")\n\t\t\tor die_error(undef, \"Error looking up file\");\n\t}\n\t$ftype = git_get_type($hash);\n\tif ($ftype !~ \"blob\") {\n\t\tdie_error(\"400 Bad Request\", \"Object is not a blob\");\n\t}\n\topen ($fd, \"-|\", $GIT, \"blame\", '-l', $file_name, $hash_base)\n\t\tor die_error(undef, \"Open git-blame failed\");\n\tgit_header_html();\n\tmy $formats_nav =\n\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name\")}, \"blob\") .\n\t\t\" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blame;f=$file_name\")}, \"head\");\n\tgit_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);\n\tgit_header_div('commit', esc_html($co{'title'}), $hash_base);\n\tgit_print_page_path($file_name, $ftype);\n\tmy @rev_color = (qw(light2 dark2));\n\tmy $num_colors = scalar(@rev_color);\n\tmy $current_color = 0;\n\tmy $last_rev;\n\tprint \"<div class=\\\"page_body\\\">\\n\";\n\tprint \"<table class=\\\"blame\\\">\\n\";\n\tprint \"<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\\n\";\n\twhile (<$fd>) {\n\t\t/^([0-9a-fA-F]{40}).*?(\\d+)\\)\\s{1}(\\s*.*)/;\n\t\tmy $full_rev = $1;\n\t\tmy $rev = substr($full_rev, 0, 8);\n\t\tmy $lineno = $2;\n\t\tmy $data = $3;\n\n\t\tif (!defined $last_rev) {\n\t\t\t$last_rev = $full_rev;\n\t\t} elsif ($last_rev ne $full_rev) {\n\t\t\t$last_rev = $full_rev;\n\t\t\t$current_color = ++$current_color % $num_colors;\n\t\t}\n\t\tprint \"<tr class=\\\"$rev_color[$current_color]\\\">\\n\";\n\t\tprint \"<td class=\\\"sha1\\\">\" .\n\t\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$full_rev;f=$file_name\")}, esc_html($rev)) . \"</td>\\n\";\n\t\tprint \"<td class=\\\"linenr\\\"><a id=\\\"l$lineno\\\" href=\\\"#l$lineno\\\" class=\\\"linenr\\\">\" . esc_html($lineno) . \"</a></td>\\n\";\n\t\tprint \"<td class=\\\"pre\\\">\" . esc_html($data) . \"</td>\\n\";\n\t\tprint \"</tr>\\n\";\n\t}\n\tprint \"</table>\\n\";\n\tprint \"</div>\";\n\tclose $fd or print \"Reading blob failed\\n\";\n\tgit_footer_html();\n}\n\nsub git_blame {\n\tmy $fd;\n\tdie_error('403 Permission denied', \"Permission denied\") if (!git_get_project_config_bool ('blame'));\n\tdie_error('404 Not Found', \"File name not defined\") if (!$file_name);\n\t$hash_base ||= git_read_head($project);\n\tdie_error(undef, \"Couldn't find base commit\") unless ($hash_base);\n\tmy %co = git_read_commit($hash_base)\n\t\tor die_error(undef, \"Reading commit failed\");\n\tif (!defined $hash) {\n\t\t$hash = git_get_hash_by_path($hash_base, $file_name, \"blob\")\n\t\t\tor die_error(undef, \"Error lookup file\");\n\t}\n\topen ($fd, \"-|\", $GIT, \"annotate\", '-l', '-t', '-r', $file_name, $hash_base)\n\t\tor die_error(undef, \"Open git-annotate failed\");\n\tgit_header_html();\n\tmy $formats_nav =\n\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name\")}, \"blob\") .\n\t\t\" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blame;f=$file_name\")}, \"head\");\n\tgit_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);\n\tgit_header_div('commit', esc_html($co{'title'}), $hash_base);\n\tgit_print_page_path($file_name, 'blob');\n\tprint \"<div class=\\\"page_body\\\">\\n\";\n\tprint <<HTML;\n<table class=\"blame\">\n  <tr>\n    <th>Commit</th>\n    <th>Age</th>\n    <th>Author</th>\n    <th>Line</th>\n    <th>Data</th>\n  </tr>\nHTML\n\tmy @line_class = (qw(light dark));\n\tmy $line_class_len = scalar (@line_class);\n\tmy $line_class_num = $#line_class;\n\twhile (my $line = <$fd>) {\n\t\tmy $long_rev;\n\t\tmy $short_rev;\n\t\tmy $author;\n\t\tmy $time;\n\t\tmy $lineno;\n\t\tmy $data;\n\t\tmy $age;\n\t\tmy $age_str;\n\t\tmy $age_class;\n\n\t\tchomp $line;\n\t\t$line_class_num = ($line_class_num + 1) % $line_class_len;\n\n\t\tif ($line =~ m/^([0-9a-fA-F]{40})\\t\\(\\s*([^\\t]+)\\t(\\d+) \\+\\d\\d\\d\\d\\t(\\d+)\\)(.*)$/) {\n\t\t\t$long_rev = $1;\n\t\t\t$author   = $2;\n\t\t\t$time     = $3;\n\t\t\t$lineno   = $4;\n\t\t\t$data     = $5;\n\t\t} else {\n\t\t\tprint qq(  <tr><td colspan=\"5\" class=\"error\">Unable to parse: $line</td></tr>\\n);\n\t\t\tnext;\n\t\t}\n\t\t$short_rev  = substr ($long_rev, 0, 8);\n\t\t$age        = time () - $time;\n\t\t$age_str    = age_string ($age);\n\t\t$age_str    =~ s/ /&nbsp;/g;\n\t\t$age_class  = age_class($age);\n\t\t$author     = esc_html ($author);\n\t\t$author     =~ s/ /&nbsp;/g;\n\n\t\t$data = untabify($data);\n\t\t$data = esc_html ($data);\n\n\t\tprint <<HTML;\n  <tr class=\"$line_class[$line_class_num]\">\n    <td class=\"sha1\"><a href=\"$my_uri?${\\esc_param (\"p=$project;a=commit;h=$long_rev\")}\" class=\"text\">$short_rev..</a></td>\n    <td class=\"$age_class\">$age_str</td>\n    <td>$author</td>\n    <td class=\"linenr\"><a id=\"$lineno\" href=\"#$lineno\" class=\"linenr\">$lineno</a></td>\n    <td class=\"pre\">$data</td>\n  </tr>\nHTML\n\t} # while (my $line = <$fd>)\n\tprint \"</table>\\n\\n\";\n\tclose $fd or print \"Reading blob failed.\\n\";\n\tprint \"</div>\";\n\tgit_footer_html();\n}\n\nsub git_tags {\n\tmy $head = git_read_head($project);\n\tgit_header_html();\n\tgit_page_nav('','', $head,undef,$head);\n\tgit_header_div('summary', $project);\n\n\tmy $taglist = git_read_refs(\"refs/tags\");\n\tif (defined @$taglist) {\n\t\tgit_tags_body($taglist);\n\t}\n\tgit_footer_html();\n}\n\nsub git_heads {\n\tmy $head = git_read_head($project);\n\tgit_header_html();\n\tgit_page_nav('','', $head,undef,$head);\n\tgit_header_div('summary', $project);\n\n\tmy $taglist = git_read_refs(\"refs/heads\");\n\tif (defined @$taglist) {\n\t\tgit_heads_body($taglist, $head);\n\t}\n\tgit_footer_html();\n}\n\nsub git_blob_plain {\n\tif (!defined $hash) {\n\t\tif (defined $file_name) {\n\t\t\tmy $base = $hash_base || git_read_head($project);\n\t\t\t$hash = git_get_hash_by_path($base, $file_name, \"blob\")\n\t\t\t\tor die_error(undef, \"Error lookup file\");\n\t\t} else {\n\t\t\tdie_error(undef, \"No file name defined\");\n\t\t}\n\t}\n\tmy $type = shift;\n\topen my $fd, \"-|\", $GIT, \"cat-file\", \"blob\", $hash\n\t\tor die_error(undef, \"Couldn't cat $file_name, $hash\");\n\n\t$type ||= git_blob_plain_mimetype($fd, $file_name);\n\n\t# save as filename, even when no $file_name is given\n\tmy $save_as = \"$hash\";\n\tif (defined $file_name) {\n\t\t$save_as = $file_name;\n\t} elsif ($type =~ m/^text\\//) {\n\t\t$save_as .= '.txt';\n\t}\n\n\tprint $cgi->header(-type => \"$type\", '-content-disposition' => \"inline; filename=\\\"$save_as\\\"\");\n\tundef $/;\n\tbinmode STDOUT, ':raw';\n\tprint <$fd>;\n\tbinmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi\n\t$/ = \"\\n\";\n\tclose $fd;\n}\n\nsub git_blob {\n\tif (!defined $hash) {\n\t\tif (defined $file_name) {\n\t\t\tmy $base = $hash_base || git_read_head($project);\n\t\t\t$hash = git_get_hash_by_path($base, $file_name, \"blob\")\n\t\t\t\tor die_error(undef, \"Error lookup file\");\n\t\t} else {\n\t\t\tdie_error(undef, \"No file name defined\");\n\t\t}\n\t}\n\tmy $have_blame = git_get_project_config_bool ('blame');\n\topen my $fd, \"-|\", $GIT, \"cat-file\", \"blob\", $hash\n\t\tor die_error(undef, \"Couldn't cat $file_name, $hash\");\n\tmy $mimetype = git_blob_plain_mimetype($fd, $file_name);\n\tif ($mimetype !~ m/^text\\//) {\n\t\tclose $fd;\n\t\treturn git_blob_plain($mimetype);\n\t}\n\tgit_header_html();\n\tmy $formats_nav = '';\n\tif (defined $hash_base && (my %co = git_read_commit($hash_base))) {\n\t\tif (defined $file_name) {\n\t\t\tif ($have_blame) {\n\t\t\t\t$formats_nav .= $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blame;h=$hash;hb=$hash_base;f=$file_name\")}, \"blame\") . \" | \";\n\t\t\t}\n\t\t\t$formats_nav .=\n\t\t\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob_plain;h=$hash;f=$file_name\")}, \"plain\") .\n\t\t\t\t\" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;hb=HEAD;f=$file_name\")}, \"head\");\n\t\t} else {\n\t\t\t$formats_nav .= $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob_plain;h=$hash\")}, \"plain\");\n\t\t}\n\t\tgit_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);\n\t\tgit_header_div('commit', esc_html($co{'title'}), $hash_base);\n\t} else {\n\t\tprint \"<div class=\\\"page_nav\\\">\\n\" .\n\t\t      \"<br/><br/></div>\\n\" .\n\t\t      \"<div class=\\\"title\\\">$hash</div>\\n\";\n\t}\n\tgit_print_page_path($file_name, \"blob\");\n\tprint \"<div class=\\\"page_body\\\">\\n\";\n\tmy $nr;\n\twhile (my $line = <$fd>) {\n\t\tchomp $line;\n\t\t$nr++;\n\t\t$line = untabify($line);\n\t\tprintf \"<div class=\\\"pre\\\"><a id=\\\"l%i\\\" href=\\\"#l%i\\\" class=\\\"linenr\\\">%4i</a> %s</div>\\n\", $nr, $nr, $nr, esc_html($line);\n\t}\n\tclose $fd or print \"Reading blob failed.\\n\";\n\tprint \"</div>\";\n\tgit_footer_html();\n}\n\nsub git_tree {\n\tif (!defined $hash) {\n\t\t$hash = git_read_head($project);\n\t\tif (defined $file_name) {\n\t\t\tmy $base = $hash_base || $hash;\n\t\t\t$hash = git_get_hash_by_path($base, $file_name, \"tree\");\n\t\t}\n\t\tif (!defined $hash_base) {\n\t\t\t$hash_base = $hash;\n\t\t}\n\t}\n\t$/ = \"\\0\";\n\topen my $fd, \"-|\", $GIT, \"ls-tree\", '-z', $hash\n\t\tor die_error(undef, \"Open git-ls-tree failed\");\n\tmy @entries = map { chomp; $_ } <$fd>;\n\tclose $fd or die_error(undef, \"Reading tree failed\");\n\t$/ = \"\\n\";\n\n\tmy $refs = read_info_ref();\n\tmy $ref = git_get_referencing($refs, $hash_base);\n\tgit_header_html();\n\tmy $base_key = \"\";\n\tmy $base = \"\";\n\tmy $have_blame = git_get_project_config_bool ('blame');\n\tif (defined $hash_base && (my %co = git_read_commit($hash_base))) {\n\t\t$base_key = \";hb=$hash_base\";\n\t\tgit_page_nav('tree','', $hash_base);\n\t\tgit_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);\n\t} else {\n\t\tprint \"<div class=\\\"page_nav\\\">\\n\";\n\t\tprint \"<br/><br/></div>\\n\";\n\t\tprint \"<div class=\\\"title\\\">$hash</div>\\n\";\n\t}\n\tif (defined $file_name) {\n\t\t$base = esc_html(\"$file_name/\");\n\t}\n\tgit_print_page_path($file_name, 'tree');\n\tprint \"<div class=\\\"page_body\\\">\\n\";\n\tprint \"<table cellspacing=\\\"0\\\">\\n\";\n\tmy $alternate = 0;\n\tforeach my $line (@entries) {\n\t\t#'100644\tblob\t0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa\tpanic.c'\n\t\t$line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\\t(.+)$/;\n\t\tmy $t_mode = $1;\n\t\tmy $t_type = $2;\n\t\tmy $t_hash = $3;\n\t\tmy $t_name = validate_input($4);\n\t\tif ($alternate) {\n\t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n\t\t} else {\n\t\t\tprint \"<tr class=\\\"light\\\">\\n\";\n\t\t}\n\t\t$alternate ^= 1;\n\t\tprint \"<td class=\\\"mode\\\">\" . mode_str($t_mode) . \"</td>\\n\";\n\t\tif ($t_type eq \"blob\") {\n\t\t\tprint \"<td class=\\\"list\\\">\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name\"), -class => \"list\"}, esc_html($t_name)) .\n\t\t\t      \"</td>\\n\" .\n\t\t\t      \"<td class=\\\"link\\\">\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$t_hash$base_key;f=$base$t_name\")}, \"blob\");\n\t\t\tif ($have_blame) {\n\t\t\t\tprint \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blame;h=$t_hash$base_key;f=$base$t_name\")}, \"blame\");\n\t\t\t}\n\t\t\tprint \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=history;h=$t_hash;hb=$hash_base;f=$base$t_name\")}, \"history\") .\n\t\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob_plain;h=$t_hash;f=$base$t_name\")}, \"raw\") .\n\t\t\t      \"</td>\\n\";\n\t\t} elsif ($t_type eq \"tree\") {\n\t\t\tprint \"<td class=\\\"list\\\">\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name\")}, esc_html($t_name)) .\n\t\t\t      \"</td>\\n\" .\n\t\t\t      \"<td class=\\\"link\\\">\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=tree;h=$t_hash$base_key;f=$base$t_name\")}, \"tree\") .\n\t\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=history;hb=$hash_base;f=$base$t_name\")}, \"history\") .\n\t\t\t      \"</td>\\n\";\n\t\t}\n\t\tprint \"</tr>\\n\";\n\t}\n\tprint \"</table>\\n\" .\n\t      \"</div>\";\n\tgit_footer_html();\n}\n\nsub git_log {\n\tmy $head = git_read_head($project);\n\tif (!defined $hash) {\n\t\t$hash = $head;\n\t}\n\tif (!defined $page) {\n\t\t$page = 0;\n\t}\n\tmy $refs = read_info_ref();\n\n\tmy $limit = sprintf(\"--max-count=%i\", (100 * ($page+1)));\n\topen my $fd, \"-|\", $GIT, \"rev-list\", $limit, $hash\n\t\tor die_error(undef, \"Open git-rev-list failed\");\n\tmy @revlist = map { chomp; $_ } <$fd>;\n\tclose $fd;\n\n\tmy $paging_nav = git_get_paging_nav('log', $hash, $head, $page, $#revlist);\n\n\tgit_header_html();\n\tgit_page_nav('log','', $hash,undef,undef, $paging_nav);\n\n\tif (!@revlist) {\n\t\tmy %co = git_read_commit($hash);\n\n\t\tgit_header_div('summary', $project);\n\t\tprint \"<div class=\\\"page_body\\\"> Last change $co{'age_string'}.<br/><br/></div>\\n\";\n\t}\n\tfor (my $i = ($page * 100); $i <= $#revlist; $i++) {\n\t\tmy $commit = $revlist[$i];\n\t\tmy $ref = git_get_referencing($refs, $commit);\n\t\tmy %co = git_read_commit($commit);\n\t\tnext if !%co;\n\t\tmy %ad = date_str($co{'author_epoch'});\n\t\tgit_header_div('commit',\n\t\t               \"<span class=\\\"age\\\">$co{'age_string'}</span>\" .\n\t\t               esc_html($co{'title'}) . $ref,\n\t\t               $commit);\n\t\tprint \"<div class=\\\"title_text\\\">\\n\" .\n\t\t      \"<div class=\\\"log_link\\\">\\n\" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$commit\")}, \"commit\") .\n\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commitdiff;h=$commit\")}, \"commitdiff\") .\n\t\t      \"<br/>\\n\" .\n\t\t      \"</div>\\n\" .\n\t\t      \"<i>\" . esc_html($co{'author_name'}) .  \" [$ad{'rfc2822'}]</i><br/>\\n\" .\n\t\t      \"</div>\\n\" .\n\t\t      \"<div class=\\\"log_body\\\">\\n\";\n\t\tmy $comment = $co{'comment'};\n\t\tmy $empty = 0;\n\t\tforeach my $line (@$comment) {\n\t\t\tif ($line =~ m/^ *(signed[ \\-]off[ \\-]by[ :]|acked[ \\-]by[ :]|cc[ :])/i) {\n\t\t\t\tnext;\n\t\t\t}\n\t\t\tif ($line eq \"\") {\n\t\t\t\tif ($empty) {\n\t\t\t\t\tnext;\n\t\t\t\t}\n\t\t\t\t$empty = 1;\n\t\t\t} else {\n\t\t\t\t$empty = 0;\n\t\t\t}\n\t\t\tprint format_log_line_html($line) . \"<br/>\\n\";\n\t\t}\n\t\tif (!$empty) {\n\t\t\tprint \"<br/>\\n\";\n\t\t}\n\t\tprint \"</div>\\n\";\n\t}\n\tgit_footer_html();\n}\n\nsub git_commit {\n\tmy %co = git_read_commit($hash);\n\tif (!%co) {\n\t\tdie_error(undef, \"Unknown commit object\");\n\t}\n\tmy %ad = date_str($co{'author_epoch'}, $co{'author_tz'});\n\tmy %cd = date_str($co{'committer_epoch'}, $co{'committer_tz'});\n\n\tmy $parent = $co{'parent'};\n\tif (!defined $parent) {\n\t\t$parent = \"--root\";\n\t}\n\topen my $fd, \"-|\", $GIT, \"diff-tree\", '-r', '-M', $parent, $hash\n\t\tor die_error(undef, \"Open git-diff-tree failed\");\n\tmy @difftree = map { chomp; $_ } <$fd>;\n\tclose $fd or die_error(undef, \"Reading git-diff-tree failed\");\n\n\t# non-textual hash id's can be cached\n\tmy $expires;\n\tif ($hash =~ m/^[0-9a-fA-F]{40}$/) {\n\t\t$expires = \"+1d\";\n\t}\n\tmy $refs = read_info_ref();\n\tmy $ref = git_get_referencing($refs, $co{'id'});\n\tmy $formats_nav = '';\n\tif (defined $file_name && defined $co{'parent'}) {\n\t\tmy $parent = $co{'parent'};\n\t\t$formats_nav .= $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blame;hb=$parent;f=$file_name\")}, \"blame\");\n\t}\n\tgit_header_html(undef, $expires);\n\tgit_page_nav('commit', defined $co{'parent'} ? '' : 'commitdiff',\n\t             $hash, $co{'tree'}, $hash,\n\t             $formats_nav);\n\n\tif (defined $co{'parent'}) {\n\t\tgit_header_div('commitdiff', esc_html($co{'title'}) . $ref, $hash);\n\t} else {\n\t\tgit_header_div('tree', esc_html($co{'title'}) . $ref, $co{'tree'}, $hash);\n\t}\n\tprint \"<div class=\\\"title_text\\\">\\n\" .\n\t      \"<table cellspacing=\\\"0\\\">\\n\";\n\tprint \"<tr><td>author</td><td>\" . esc_html($co{'author'}) . \"</td></tr>\\n\".\n\t      \"<tr>\" .\n\t      \"<td></td><td> $ad{'rfc2822'}\";\n\tif ($ad{'hour_local'} < 6) {\n\t\tprintf(\" (<span class=\\\"atnight\\\">%02d:%02d</span> %s)\", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});\n\t} else {\n\t\tprintf(\" (%02d:%02d %s)\", $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});\n\t}\n\tprint \"</td>\" .\n\t      \"</tr>\\n\";\n\tprint \"<tr><td>committer</td><td>\" . esc_html($co{'committer'}) . \"</td></tr>\\n\";\n\tprint \"<tr><td></td><td> $cd{'rfc2822'}\" . sprintf(\" (%02d:%02d %s)\", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) . \"</td></tr>\\n\";\n\tprint \"<tr><td>commit</td><td class=\\\"sha1\\\">$co{'id'}</td></tr>\\n\";\n\tprint \"<tr>\" .\n\t      \"<td>tree</td>\" .\n\t      \"<td class=\\\"sha1\\\">\" .\n\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=tree;h=$co{'tree'};hb=$hash\"), class => \"list\"}, $co{'tree'}) .\n\t      \"</td>\" .\n\t      \"<td class=\\\"link\\\">\" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=tree;h=$co{'tree'};hb=$hash\")}, \"tree\") .\n\t      \"</td>\" .\n\t      \"</tr>\\n\";\n\tmy $parents = $co{'parents'};\n\tforeach my $par (@$parents) {\n\t\tprint \"<tr>\" .\n\t\t      \"<td>parent</td>\" .\n\t\t      \"<td class=\\\"sha1\\\">\" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$par\"), class => \"list\"}, $par) . \"</td>\" .\n\t\t      \"<td class=\\\"link\\\">\" .\n\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$par\")}, \"commit\") .\n\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commitdiff;h=$hash;hp=$par\")}, \"commitdiff\") .\n\t\t      \"</td>\" .\n\t\t      \"</tr>\\n\";\n\t}\n\tprint \"</table>\".\n\t      \"</div>\\n\";\n\tprint \"<div class=\\\"page_body\\\">\\n\";\n\tmy $comment = $co{'comment'};\n\tmy $empty = 0;\n\tmy $signed = 0;\n\tforeach my $line (@$comment) {\n\t\t# print only one empty line\n\t\tif ($line eq \"\") {\n\t\t\tif ($empty || $signed) {\n\t\t\t\tnext;\n\t\t\t}\n\t\t\t$empty = 1;\n\t\t} else {\n\t\t\t$empty = 0;\n\t\t}\n\t\tif ($line =~ m/^ *(signed[ \\-]off[ \\-]by[ :]|acked[ \\-]by[ :]|cc[ :])/i) {\n\t\t\t$signed = 1;\n\t\t\tprint \"<span class=\\\"signoff\\\">\" . esc_html($line) . \"</span><br/>\\n\";\n\t\t} else {\n\t\t\t$signed = 0;\n\t\t\tprint format_log_line_html($line) . \"<br/>\\n\";\n\t\t}\n\t}\n\tprint \"</div>\\n\";\n\tprint \"<div class=\\\"list_head\\\">\\n\";\n\tif ($#difftree > 10) {\n\t\tprint(($#difftree + 1) . \" files changed:\\n\");\n\t}\n\tprint \"</div>\\n\";\n\tprint \"<table class=\\\"diff_tree\\\">\\n\";\n\tmy $alternate = 0;\n\tforeach my $line (@difftree) {\n\t\t# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'\n\t\t# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'\n\t\tif ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\\t(.*)$/) {\n\t\t\tnext;\n\t\t}\n\t\tmy $from_mode = $1;\n\t\tmy $to_mode = $2;\n\t\tmy $from_id = $3;\n\t\tmy $to_id = $4;\n\t\tmy $status = $5;\n\t\tmy $similarity = $6;\n\t\tmy $file = validate_input(unquote($7));\n\t\tif ($alternate) {\n\t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n\t\t} else {\n\t\t\tprint \"<tr class=\\\"light\\\">\\n\";\n\t\t}\n\t\t$alternate ^= 1;\n\t\tif ($status eq \"A\") {\n\t\t\tmy $mode_chng = \"\";\n\t\t\tif (S_ISREG(oct $to_mode)) {\n\t\t\t\t$mode_chng = sprintf(\" with mode: %04o\", (oct $to_mode) & 0777);\n\t\t\t}\n\t\t\tprint \"<td>\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$to_id;hb=$hash;f=$file\"), -class => \"list\"}, esc_html($file)) . \"</td>\\n\" .\n\t\t\t      \"<td><span class=\\\"file_status new\\\">[new \" . file_type($to_mode) . \"$mode_chng]</span></td>\\n\" .\n\t\t\t      \"<td class=\\\"link\\\">\" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$to_id;hb=$hash;f=$file\")}, \"blob\") . \"</td>\\n\";\n\t\t} elsif ($status eq \"D\") {\n\t\t\tprint \"<td>\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$from_id;hb=$parent;f=$file\"), -class => \"list\"}, esc_html($file)) . \"</td>\\n\" .\n\t\t\t      \"<td><span class=\\\"file_status deleted\\\">[deleted \" . file_type($from_mode). \"]</span></td>\\n\" .\n\t\t\t      \"<td class=\\\"link\\\">\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$from_id;hb=$parent;f=$file\")}, \"blob\") .\n\t\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=history;hb=$parent;f=$file\")}, \"history\") .\n\t\t\t      \"</td>\\n\"\n\t\t} elsif ($status eq \"M\" || $status eq \"T\") {\n\t\t\tmy $mode_chnge = \"\";\n\t\t\tif ($from_mode != $to_mode) {\n\t\t\t\t$mode_chnge = \" <span class=\\\"file_status mode_chnge\\\">[changed\";\n\t\t\t\tif (((oct $from_mode) & S_IFMT) != ((oct $to_mode) & S_IFMT)) {\n\t\t\t\t\t$mode_chnge .= \" from \" . file_type($from_mode) . \" to \" . file_type($to_mode);\n\t\t\t\t}\n\t\t\t\tif (((oct $from_mode) & 0777) != ((oct $to_mode) & 0777)) {\n\t\t\t\t\tif (S_ISREG($from_mode) && S_ISREG($to_mode)) {\n\t\t\t\t\t\t$mode_chnge .= sprintf(\" mode: %04o->%04o\", (oct $from_mode) & 0777, (oct $to_mode) & 0777);\n\t\t\t\t\t} elsif (S_ISREG($to_mode)) {\n\t\t\t\t\t\t$mode_chnge .= sprintf(\" mode: %04o\", (oct $to_mode) & 0777);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t$mode_chnge .= \"]</span>\\n\";\n\t\t\t}\n\t\t\tprint \"<td>\";\n\t\t\tif ($to_id ne $from_id) {\n\t\t\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file\"), -class => \"list\"}, esc_html($file));\n\t\t\t} else {\n\t\t\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$to_id;hb=$hash;f=$file\"), -class => \"list\"}, esc_html($file));\n\t\t\t}\n\t\t\tprint \"</td>\\n\" .\n\t\t\t      \"<td>$mode_chnge</td>\\n\" .\n\t\t\t      \"<td class=\\\"link\\\">\";\n\t\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$to_id;hb=$hash;f=$file\")}, \"blob\");\n\t\t\tif ($to_id ne $from_id) {\n\t\t\t\tprint \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$file\")}, \"diff\");\n\t\t\t}\n\t\t\tprint \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=history;hb=$hash;f=$file\")}, \"history\") . \"\\n\";\n\t\t\tprint \"</td>\\n\";\n\t\t} elsif ($status eq \"R\") {\n\t\t\tmy ($from_file, $to_file) = split \"\\t\", $file;\n\t\t\tmy $mode_chng = \"\";\n\t\t\tif ($from_mode != $to_mode) {\n\t\t\t\t$mode_chng = sprintf(\", mode: %04o\", (oct $to_mode) & 0777);\n\t\t\t}\n\t\t\tprint \"<td>\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file\"), -class => \"list\"}, esc_html($to_file)) . \"</td>\\n\" .\n\t\t\t      \"<td><span class=\\\"file_status moved\\\">[moved from \" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$from_id;hb=$parent;f=$from_file\"), -class => \"list\"}, esc_html($from_file)) .\n\t\t\t      \" with \" . (int $similarity) . \"% similarity$mode_chng]</span></td>\\n\" .\n\t\t\t      \"<td class=\\\"link\\\">\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$to_id;hb=$hash;f=$to_file\")}, \"blob\");\n\t\t\tif ($to_id ne $from_id) {\n\t\t\t\tprint \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blobdiff;h=$to_id;hp=$from_id;hb=$hash;f=$to_file\")}, \"diff\");\n\t\t\t}\n\t\t\tprint \"</td>\\n\";\n\t\t}\n\t\tprint \"</tr>\\n\";\n\t}\n\tprint \"</table>\\n\";\n\tgit_footer_html();\n}\n\nsub git_blobdiff {\n\tmkdir($git_temp, 0700);\n\tgit_header_html();\n\tif (defined $hash_base && (my %co = git_read_commit($hash_base))) {\n\t\tmy $formats_nav =\n\t\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blobdiff_plain;h=$hash;hp=$hash_parent\")}, \"plain\");\n\t\tgit_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);\n\t\tgit_header_div('commit', esc_html($co{'title'}), $hash_base);\n\t} else {\n\t\tprint \"<div class=\\\"page_nav\\\">\\n\" .\n\t\t      \"<br/><br/></div>\\n\" .\n\t\t      \"<div class=\\\"title\\\">$hash vs $hash_parent</div>\\n\";\n\t}\n\tgit_print_page_path($file_name, \"blob\");\n\tprint \"<div class=\\\"page_body\\\">\\n\" .\n\t      \"<div class=\\\"diff_info\\\">blob:\" .\n\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$hash_parent;hb=$hash_base;f=$file_name\")}, $hash_parent) .\n\t      \" -> blob:\" .\n\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$hash;hb=$hash_base;f=$file_name\")}, $hash) .\n\t      \"</div>\\n\";\n\tgit_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash);\n\tprint \"</div>\";\n\tgit_footer_html();\n}\n\nsub git_blobdiff_plain {\n\tmkdir($git_temp, 0700);\n\tprint $cgi->header(-type => \"text/plain\", -charset => 'utf-8');\n\tgit_diff_print($hash_parent, $file_name || $hash_parent, $hash, $file_name || $hash, \"plain\");\n}\n\nsub git_commitdiff {\n\tmkdir($git_temp, 0700);\n\tmy %co = git_read_commit($hash);\n\tif (!%co) {\n\t\tdie_error(undef, \"Unknown commit object\");\n\t}\n\tif (!defined $hash_parent) {\n\t\t$hash_parent = $co{'parent'} || '--root';\n\t}\n\topen my $fd, \"-|\", $GIT, \"diff-tree\", '-r', $hash_parent, $hash\n\t\tor die_error(undef, \"Open git-diff-tree failed\");\n\tmy @difftree = map { chomp; $_ } <$fd>;\n\tclose $fd or die_error(undef, \"Reading git-diff-tree failed\");\n\n\t# non-textual hash id's can be cached\n\tmy $expires;\n\tif ($hash =~ m/^[0-9a-fA-F]{40}$/) {\n\t\t$expires = \"+1d\";\n\t}\n\tmy $refs = read_info_ref();\n\tmy $ref = git_get_referencing($refs, $co{'id'});\n\tmy $formats_nav =\n\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commitdiff_plain;h=$hash;hp=$hash_parent\")}, \"plain\");\n\tgit_header_html(undef, $expires);\n\tgit_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);\n\tgit_header_div('commit', esc_html($co{'title'}) . $ref, $hash);\n\tprint \"<div class=\\\"page_body\\\">\\n\";\n\tmy $comment = $co{'comment'};\n\tmy $empty = 0;\n\tmy $signed = 0;\n\tmy @log = @$comment;\n\t# remove first and empty lines after that\n\tshift @log;\n\twhile (defined $log[0] && $log[0] eq \"\") {\n\t\tshift @log;\n\t}\n\tforeach my $line (@log) {\n\t\tif ($line =~ m/^ *(signed[ \\-]off[ \\-]by[ :]|acked[ \\-]by[ :]|cc[ :])/i) {\n\t\t\tnext;\n\t\t}\n\t\tif ($line eq \"\") {\n\t\t\tif ($empty) {\n\t\t\t\tnext;\n\t\t\t}\n\t\t\t$empty = 1;\n\t\t} else {\n\t\t\t$empty = 0;\n\t\t}\n\t\tprint format_log_line_html($line) . \"<br/>\\n\";\n\t}\n\tprint \"<br/>\\n\";\n\tforeach my $line (@difftree) {\n\t\t# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M      ls-files.c'\n\t\t# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M      rev-tree.c'\n\t\tif ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\\t(.*)$/) {\n\t\t\tnext;\n\t\t}\n\t\tmy $from_mode = $1;\n\t\tmy $to_mode = $2;\n\t\tmy $from_id = $3;\n\t\tmy $to_id = $4;\n\t\tmy $status = $5;\n\t\tmy $file = validate_input(unquote($6));\n\t\tif ($status eq \"A\") {\n\t\t\tprint \"<div class=\\\"diff_info\\\">\" . file_type($to_mode) . \":\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$to_id;hb=$hash;f=$file\")}, $to_id) . \"(new)\" .\n\t\t\t      \"</div>\\n\";\n\t\t\tgit_diff_print(undef, \"/dev/null\", $to_id, \"b/$file\");\n\t\t} elsif ($status eq \"D\") {\n\t\t\tprint \"<div class=\\\"diff_info\\\">\" . file_type($from_mode) . \":\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$from_id;hb=$hash_parent;f=$file\")}, $from_id) . \"(deleted)\" .\n\t\t\t      \"</div>\\n\";\n\t\t\tgit_diff_print($from_id, \"a/$file\", undef, \"/dev/null\");\n\t\t} elsif ($status eq \"M\") {\n\t\t\tif ($from_id ne $to_id) {\n\t\t\t\tprint \"<div class=\\\"diff_info\\\">\" .\n\t\t\t\t      file_type($from_mode) . \":\" .\n\t\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$from_id;hb=$hash_parent;f=$file\")}, $from_id) .\n\t\t\t\t      \" -> \" .\n\t\t\t\t      file_type($to_mode) . \":\" .\n\t\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$to_id;hb=$hash;f=$file\")}, $to_id);\n\t\t\t\tprint \"</div>\\n\";\n\t\t\t\tgit_diff_print($from_id, \"a/$file\",  $to_id, \"b/$file\");\n\t\t\t}\n\t\t}\n\t}\n\tprint \"<br/>\\n\" .\n\t      \"</div>\";\n\tgit_footer_html();\n}\n\nsub git_commitdiff_plain {\n\tmkdir($git_temp, 0700);\n\tmy %co = git_read_commit($hash);\n\tif (!%co) {\n\t\tdie_error(undef, \"Unknown commit object\");\n\t}\n\tif (!defined $hash_parent) {\n\t\t$hash_parent = $co{'parent'} || '--root';\n\t}\n\topen my $fd, \"-|\", $GIT, \"diff-tree\", '-r', $hash_parent, $hash\n\t\tor die_error(undef, \"Open git-diff-tree failed\");\n\tmy @difftree = map { chomp; $_ } <$fd>;\n\tclose $fd or die_error(undef, \"Reading diff-tree failed\");\n\n\t# try to figure out the next tag after this commit\n\tmy $tagname;\n\tmy $refs = read_info_ref(\"tags\");\n\topen $fd, \"-|\", $GIT, \"rev-list\", \"HEAD\";\n\tmy @commits = map { chomp; $_ } <$fd>;\n\tclose $fd;\n\tforeach my $commit (@commits) {\n\t\tif (defined $refs->{$commit}) {\n\t\t\t$tagname = $refs->{$commit}\n\t\t}\n\t\tif ($commit eq $hash) {\n\t\t\tlast;\n\t\t}\n\t}\n\n\tprint $cgi->header(-type => \"text/plain\", -charset => 'utf-8', '-content-disposition' => \"inline; filename=\\\"git-$hash.patch\\\"\");\n\tmy %ad = date_str($co{'author_epoch'}, $co{'author_tz'});\n\tmy $comment = $co{'comment'};\n\tprint \"From: $co{'author'}\\n\" .\n\t      \"Date: $ad{'rfc2822'} ($ad{'tz_local'})\\n\".\n\t      \"Subject: $co{'title'}\\n\";\n\tif (defined $tagname) {\n\t\tprint \"X-Git-Tag: $tagname\\n\";\n\t}\n\tprint \"X-Git-Url: $my_url?p=$project;a=commitdiff;h=$hash\\n\" .\n\t      \"\\n\";\n\n\tforeach my $line (@$comment) {;\n\t\tprint \"$line\\n\";\n\t}\n\tprint \"---\\n\\n\";\n\n\tforeach my $line (@difftree) {\n\t\tif ($line !~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\\t(.*)$/) {\n\t\t\tnext;\n\t\t}\n\t\tmy $from_id = $3;\n\t\tmy $to_id = $4;\n\t\tmy $status = $5;\n\t\tmy $file = $6;\n\t\tif ($status eq \"A\") {\n\t\t\tgit_diff_print(undef, \"/dev/null\", $to_id, \"b/$file\", \"plain\");\n\t\t} elsif ($status eq \"D\") {\n\t\t\tgit_diff_print($from_id, \"a/$file\", undef, \"/dev/null\", \"plain\");\n\t\t} elsif ($status eq \"M\") {\n\t\t\tgit_diff_print($from_id, \"a/$file\",  $to_id, \"b/$file\", \"plain\");\n\t\t}\n\t}\n}\n\nsub git_history {\n\tif (!defined $hash_base) {\n\t\t$hash_base = git_read_head($project);\n\t}\n\tmy $ftype;\n\tmy %co = git_read_commit($hash_base);\n\tif (!%co) {\n\t\tdie_error(undef, \"Unknown commit object\");\n\t}\n\tmy $refs = read_info_ref();\n\tgit_header_html();\n\tgit_page_nav('','', $hash_base,$co{'tree'},$hash_base);\n\tgit_header_div('commit', esc_html($co{'title'}), $hash_base);\n\tif (!defined $hash && defined $file_name) {\n\t\t$hash = git_get_hash_by_path($hash_base, $file_name);\n\t}\n\tif (defined $hash) {\n\t\t$ftype = git_get_type($hash);\n\t}\n\tgit_print_page_path($file_name, $ftype);\n\n\topen my $fd, \"-|\",\n\t\t$GIT, \"rev-list\", \"--full-history\", \"--remove-empty\", $hash_base, \"--\", $file_name;\n\tprint \"<table cellspacing=\\\"0\\\">\\n\";\n\tmy $alternate = 0;\n\twhile (my $line = <$fd>) {\n\t\tif ($line =~ m/^([0-9a-fA-F]{40})/){\n\t\t\tmy $commit = $1;\n\t\t\tmy %co = git_read_commit($commit);\n\t\t\tif (!%co) {\n\t\t\t\tnext;\n\t\t\t}\n\t\t\tmy $ref = git_get_referencing($refs, $commit);\n\t\t\tif ($alternate) {\n\t\t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n\t\t\t} else {\n\t\t\t\tprint \"<tr class=\\\"light\\\">\\n\";\n\t\t\t}\n\t\t\t$alternate ^= 1;\n\t\t\tprint \"<td title=\\\"$co{'age_string_age'}\\\"><i>$co{'age_string_date'}</i></td>\\n\" .\n\t\t\t      \"<td><i>\" . esc_html(chop_str($co{'author_name'}, 15, 3)) . \"</i></td>\\n\" .\n\t\t\t      \"<td>\" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$commit\"), -class => \"list\"}, \"<b>\" .\n\t\t\t      esc_html(chop_str($co{'title'}, 50)) . \"$ref</b>\") . \"</td>\\n\" .\n\t\t\t      \"<td class=\\\"link\\\">\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$commit\")}, \"commit\") .\n\t\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commitdiff;h=$commit\")}, \"commitdiff\") .\n\t\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=$ftype;hb=$commit;f=$file_name\")}, $ftype);\n\t\t\tmy $blob = git_get_hash_by_path($hash_base, $file_name);\n\t\t\tmy $blob_parent = git_get_hash_by_path($commit, $file_name);\n\t\t\tif (defined $blob && defined $blob_parent && $blob ne $blob_parent) {\n\t\t\t\tprint \" | \" .\n\t\t\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blobdiff;h=$blob;hp=$blob_parent;hb=$commit;f=$file_name\")},\n\t\t\t\t\"diff to current\");\n\t\t\t}\n\t\t\tprint \"</td>\\n\" .\n\t\t\t      \"</tr>\\n\";\n\t\t}\n\t}\n\tprint \"</table>\\n\";\n\tclose $fd;\n\tgit_footer_html();\n}\n\nsub git_search {\n\tif (!defined $searchtext) {\n\t\tdie_error(undef, \"Text field empty\");\n\t}\n\tif (!defined $hash) {\n\t\t$hash = git_read_head($project);\n\t}\n\tmy %co = git_read_commit($hash);\n\tif (!%co) {\n\t\tdie_error(undef, \"Unknown commit object\");\n\t}\n\t# pickaxe may take all resources of your box and run for several minutes\n\t# with every query - so decide by yourself how public you make this feature :)\n\tmy $commit_search = 1;\n\tmy $author_search = 0;\n\tmy $committer_search = 0;\n\tmy $pickaxe_search = 0;\n\tif ($searchtext =~ s/^author\\\\://i) {\n\t\t$author_search = 1;\n\t} elsif ($searchtext =~ s/^committer\\\\://i) {\n\t\t$committer_search = 1;\n\t} elsif ($searchtext =~ s/^pickaxe\\\\://i) {\n\t\t$commit_search = 0;\n\t\t$pickaxe_search = 1;\n\t}\n\tgit_header_html();\n\tgit_page_nav('','', $hash,$co{'tree'},$hash);\n\tgit_header_div('commit', esc_html($co{'title'}), $hash);\n\n\tprint \"<table cellspacing=\\\"0\\\">\\n\";\n\tmy $alternate = 0;\n\tif ($commit_search) {\n\t\t$/ = \"\\0\";\n\t\topen my $fd, \"-|\", $GIT, \"rev-list\", \"--header\", \"--parents\", $hash or next;\n\t\twhile (my $commit_text = <$fd>) {\n\t\t\tif (!grep m/$searchtext/i, $commit_text) {\n\t\t\t\tnext;\n\t\t\t}\n\t\t\tif ($author_search && !grep m/\\nauthor .*$searchtext/i, $commit_text) {\n\t\t\t\tnext;\n\t\t\t}\n\t\t\tif ($committer_search && !grep m/\\ncommitter .*$searchtext/i, $commit_text) {\n\t\t\t\tnext;\n\t\t\t}\n\t\t\tmy @commit_lines = split \"\\n\", $commit_text;\n\t\t\tmy %co = git_read_commit(undef, \\@commit_lines);\n\t\t\tif (!%co) {\n\t\t\t\tnext;\n\t\t\t}\n\t\t\tif ($alternate) {\n\t\t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n\t\t\t} else {\n\t\t\t\tprint \"<tr class=\\\"light\\\">\\n\";\n\t\t\t}\n\t\t\t$alternate ^= 1;\n\t\t\tprint \"<td title=\\\"$co{'age_string_age'}\\\"><i>$co{'age_string_date'}</i></td>\\n\" .\n\t\t\t      \"<td><i>\" . esc_html(chop_str($co{'author_name'}, 15, 5)) . \"</i></td>\\n\" .\n\t\t\t      \"<td>\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$co{'id'}\"), -class => \"list\"}, \"<b>\" . esc_html(chop_str($co{'title'}, 50)) . \"</b><br/>\");\n\t\t\tmy $comment = $co{'comment'};\n\t\t\tforeach my $line (@$comment) {\n\t\t\t\tif ($line =~ m/^(.*)($searchtext)(.*)$/i) {\n\t\t\t\t\tmy $lead = esc_html($1) || \"\";\n\t\t\t\t\t$lead = chop_str($lead, 30, 10);\n\t\t\t\t\tmy $match = esc_html($2) || \"\";\n\t\t\t\t\tmy $trail = esc_html($3) || \"\";\n\t\t\t\t\t$trail = chop_str($trail, 30, 10);\n\t\t\t\t\tmy $text = \"$lead<span class=\\\"match\\\">$match</span>$trail\";\n\t\t\t\t\tprint chop_str($text, 80, 5) . \"<br/>\\n\";\n\t\t\t\t}\n\t\t\t}\n\t\t\tprint \"</td>\\n\" .\n\t\t\t      \"<td class=\\\"link\\\">\" .\n\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$co{'id'}\")}, \"commit\") .\n\t\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}\")}, \"tree\");\n\t\t\tprint \"</td>\\n\" .\n\t\t\t      \"</tr>\\n\";\n\t\t}\n\t\tclose $fd;\n\t}\n\n\tif ($pickaxe_search) {\n\t\t$/ = \"\\n\";\n\t\topen my $fd, \"-|\", \"$GIT rev-list $hash | $GIT diff-tree -r --stdin -S\\'$searchtext\\'\";\n\t\tundef %co;\n\t\tmy @files;\n\t\twhile (my $line = <$fd>) {\n\t\t\tif (%co && $line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)\\t(.*)$/) {\n\t\t\t\tmy %set;\n\t\t\t\t$set{'file'} = $6;\n\t\t\t\t$set{'from_id'} = $3;\n\t\t\t\t$set{'to_id'} = $4;\n\t\t\t\t$set{'id'} = $set{'to_id'};\n\t\t\t\tif ($set{'id'} =~ m/0{40}/) {\n\t\t\t\t\t$set{'id'} = $set{'from_id'};\n\t\t\t\t}\n\t\t\t\tif ($set{'id'} =~ m/0{40}/) {\n\t\t\t\t\tnext;\n\t\t\t\t}\n\t\t\t\tpush @files, \\%set;\n\t\t\t} elsif ($line =~ m/^([0-9a-fA-F]{40})$/){\n\t\t\t\tif (%co) {\n\t\t\t\t\tif ($alternate) {\n\t\t\t\t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n\t\t\t\t\t} else {\n\t\t\t\t\t\tprint \"<tr class=\\\"light\\\">\\n\";\n\t\t\t\t\t}\n\t\t\t\t\t$alternate ^= 1;\n\t\t\t\t\tprint \"<td title=\\\"$co{'age_string_age'}\\\"><i>$co{'age_string_date'}</i></td>\\n\" .\n\t\t\t\t\t      \"<td><i>\" . esc_html(chop_str($co{'author_name'}, 15, 5)) . \"</i></td>\\n\" .\n\t\t\t\t\t      \"<td>\" .\n\t\t\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$co{'id'}\"), -class => \"list\"}, \"<b>\" .\n\t\t\t\t\t      esc_html(chop_str($co{'title'}, 50)) . \"</b><br/>\");\n\t\t\t\t\twhile (my $setref = shift @files) {\n\t\t\t\t\t\tmy %set = %$setref;\n\t\t\t\t\t\tprint $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=blob;h=$set{'id'};hb=$co{'id'};f=$set{'file'}\"), class => \"list\"},\n\t\t\t\t\t\t      \"<span class=\\\"match\\\">\" . esc_html($set{'file'}) . \"</span>\") .\n\t\t\t\t\t\t      \"<br/>\\n\";\n\t\t\t\t\t}\n\t\t\t\t\tprint \"</td>\\n\" .\n\t\t\t\t\t      \"<td class=\\\"link\\\">\" .\n\t\t\t\t\t      $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=commit;h=$co{'id'}\")}, \"commit\") .\n\t\t\t\t\t      \" | \" . $cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=tree;h=$co{'tree'};hb=$co{'id'}\")}, \"tree\");\n\t\t\t\t\tprint \"</td>\\n\" .\n\t\t\t\t\t      \"</tr>\\n\";\n\t\t\t\t}\n\t\t\t\t%co = git_read_commit($1);\n\t\t\t}\n\t\t}\n\t\tclose $fd;\n\t}\n\tprint \"</table>\\n\";\n\tgit_footer_html();\n}\n\nsub git_shortlog {\n\tmy $head = git_read_head($project);\n\tif (!defined $hash) {\n\t\t$hash = $head;\n\t}\n\tif (!defined $page) {\n\t\t$page = 0;\n\t}\n\tmy $refs = read_info_ref();\n\n\tmy $limit = sprintf(\"--max-count=%i\", (100 * ($page+1)));\n\topen my $fd, \"-|\", $GIT, \"rev-list\", $limit, $hash\n\t\tor die_error(undef, \"Open git-rev-list failed\");\n\tmy @revlist = map { chomp; $_ } <$fd>;\n\tclose $fd;\n\n\tmy $paging_nav = git_get_paging_nav('shortlog', $hash, $head, $page, $#revlist);\n\tmy $next_link = '';\n\tif ($#revlist >= (100 * ($page+1)-1)) {\n\t\t$next_link =\n\t\t\t$cgi->a({-href => \"$my_uri?\" . esc_param(\"p=$project;a=shortlog;h=$hash;pg=\" . ($page+1)),\n\t\t\t         -title => \"Alt-n\"}, \"next\");\n\t}\n\n\n\tgit_header_html();\n\tgit_page_nav('shortlog','', $hash,$hash,$hash, $paging_nav);\n\tgit_header_div('summary', $project);\n\n\tgit_shortlog_body(\\@revlist, ($page * 100), $#revlist, $refs, $next_link);\n\n\tgit_footer_html();\n}\n\n## ......................................................................\n## feeds (RSS, OPML)\n\nsub git_rss {\n\t# http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ\n\topen my $fd, \"-|\", $GIT, \"rev-list\", \"--max-count=150\", git_read_head($project)\n\t\tor die_error(undef, \"Open git-rev-list failed\");\n\tmy @revlist = map { chomp; $_ } <$fd>;\n\tclose $fd or die_error(undef, \"Reading git-rev-list failed\");\n\tprint $cgi->header(-type => 'text/xml', -charset => 'utf-8');\n\tprint \"<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\"?>\\n\".\n\t      \"<rss version=\\\"2.0\\\" xmlns:content=\\\"http://purl.org/rss/1.0/modules/content/\\\">\\n\";\n\tprint \"<channel>\\n\";\n\tprint \"<title>$project</title>\\n\".\n\t      \"<link>\" . esc_html(\"$my_url?p=$project;a=summary\") . \"</link>\\n\".\n\t      \"<description>$project log</description>\\n\".\n\t      \"<language>en</language>\\n\";\n\n\tfor (my $i = 0; $i <= $#revlist; $i++) {\n\t\tmy $commit = $revlist[$i];\n\t\tmy %co = git_read_commit($commit);\n\t\t# we read 150, we always show 30 and the ones more recent than 48 hours\n\t\tif (($i >= 20) && ((time - $co{'committer_epoch'}) > 48*60*60)) {\n\t\t\tlast;\n\t\t}\n\t\tmy %cd = date_str($co{'committer_epoch'});\n\t\topen $fd, \"-|\", $GIT, \"diff-tree\", '-r', $co{'parent'}, $co{'id'} or next;\n\t\tmy @difftree = map { chomp; $_ } <$fd>;\n\t\tclose $fd or next;\n\t\tprint \"<item>\\n\" .\n\t\t      \"<title>\" .\n\t\t      sprintf(\"%d %s %02d:%02d\", $cd{'mday'}, $cd{'month'}, $cd{'hour'}, $cd{'minute'}) . \" - \" . esc_html($co{'title'}) .\n\t\t      \"</title>\\n\" .\n\t\t      \"<author>\" . esc_html($co{'author'}) . \"</author>\\n\" .\n\t\t      \"<pubDate>$cd{'rfc2822'}</pubDate>\\n\" .\n\t\t      \"<guid isPermaLink=\\\"true\\\">\" . esc_html(\"$my_url?p=$project;a=commit;h=$commit\") . \"</guid>\\n\" .\n\t\t      \"<link>\" . esc_html(\"$my_url?p=$project;a=commit;h=$commit\") . \"</link>\\n\" .\n\t\t      \"<description>\" . esc_html($co{'title'}) . \"</description>\\n\" .\n\t\t      \"<content:encoded>\" .\n\t\t      \"<![CDATA[\\n\";\n\t\tmy $comment = $co{'comment'};\n\t\tforeach my $line (@$comment) {\n\t\t\t$line = decode(\"utf8\", $line, Encode::FB_DEFAULT);\n\t\t\tprint \"$line<br/>\\n\";\n\t\t}\n\t\tprint \"<br/>\\n\";\n\t\tforeach my $line (@difftree) {\n\t\t\tif (!($line =~ m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\\t(.*)$/)) {\n\t\t\t\tnext;\n\t\t\t}\n\t\t\tmy $file = validate_input(unquote($7));\n\t\t\t$file = decode(\"utf8\", $file, Encode::FB_DEFAULT);\n\t\t\tprint \"$file<br/>\\n\";\n\t\t}\n\t\tprint \"]]>\\n\" .\n\t\t      \"</content:encoded>\\n\" .\n\t\t      \"</item>\\n\";\n\t}\n\tprint \"</channel></rss>\";\n}\n\nsub git_opml {\n\tmy @list = git_read_projects();\n\n\tprint $cgi->header(-type => 'text/xml', -charset => 'utf-8');\n\tprint \"<?xml version=\\\"1.0\\\" encoding=\\\"utf-8\\\"?>\\n\".\n\t      \"<opml version=\\\"1.0\\\">\\n\".\n\t      \"<head>\".\n\t      \"  <title>$site_name Git OPML Export</title>\\n\".\n\t      \"</head>\\n\".\n\t      \"<body>\\n\".\n\t      \"<outline text=\\\"git RSS feeds\\\">\\n\";\n\n\tforeach my $pr (@list) {\n\t\tmy %proj = %$pr;\n\t\tmy $head = git_read_head($proj{'path'});\n\t\tif (!defined $head) {\n\t\t\tnext;\n\t\t}\n\t\t$ENV{'GIT_DIR'} = \"$projectroot/$proj{'path'}\";\n\t\tmy %co = git_read_commit($head);\n\t\tif (!%co) {\n\t\t\tnext;\n\t\t}\n\n\t\tmy $path = esc_html(chop_str($proj{'path'}, 25, 5));\n\t\tmy $rss  = \"$my_url?p=$proj{'path'};a=rss\";\n\t\tmy $html = \"$my_url?p=$proj{'path'};a=summary\";\n\t\tprint \"<outline type=\\\"rss\\\" text=\\\"$path\\\" title=\\\"$path\\\" xmlUrl=\\\"$rss\\\" htmlUrl=\\\"$html\\\"/>\\n\";\n\t}\n\tprint \"</outline>\\n\".\n\t      \"</body>\\n\".\n\t      \"</opml>\\n\";\n}\n"},{"id":"25149","messageId":"7vzmearclv.fsf@assigned-by-dhcp.cox.net","threadId":"5214","inReplyTo":"ebj7er$64j$1@sea.gmane.org","subject":"Re: How to resolve git-am conflict (possible bug)","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-08-12T01:01:32Z","receivedAt":"2006-08-12T01:01:32Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jakub Narebski <jnareb@gmail.com> writes:\n\n> 1078:jnareb@roke:~/git> rm -rf .dotest/\n> 1079:jnareb@roke:~/git> git reset --hard\n> 1080:jnareb@roke:~/git> git am --3way --utf8 --keep --binary \\\n>   mdir.0/0001-gitweb-Refactor-git_history_body.txt\n>\n> Applying '[PATCH 1/1] gitweb: Refactor git_history_body'\n>\n> error: patch failed: gitweb/gitweb.perl:2295\n> error: gitweb/gitweb.perl: patch does not apply\n> error: patch failed: gitweb/gitweb.perl:2295\n> error: gitweb/gitweb.perl: patch does not apply\n> Patch failed at 0001.\n> When you have resolved this problem run \"git-am -3 --resolved\".\n\nThis is not falling back to three-way.  One likely reason is you\ndo not have blobs identified on the \"index \" line of the patch\nin your repostory.\n"},{"id":"25150","messageId":"7vslk2rbq8.fsf@assigned-by-dhcp.cox.net","threadId":"5214","inReplyTo":"ebj7er$64j$1@sea.gmane.org","subject":"Re: How to resolve git-am conflict (possible bug)","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-08-12T01:20:31Z","receivedAt":"2006-08-12T01:20:31Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jakub Narebski <jnareb@gmail.com> writes:\n\n> Third, I wonder why it printed the same error message _twice_.\n\nDo you have blob 7ea52b1?  Otherwise you would not see two \"does\nnot apply\" messages, so I suspect you do.  Does the patch\ncleanly apply to that blob?\n\nMore likely explanation is that you edited the patch by hand for\nsome reason, and made it inapplicable to the base blob the\n\"index\" line records.\n\nThe first \"patch does not apply\" comes from ll. 363 of git-am.\nAfter it fails because the patch does not apply to the version\nof gitweb.perl in your index, since you told it to fall back to\nthree-way merge, l. 391 calls fall_back_3way, which inspects the\npatch, finds the \"index\" line and notices that the patch claims\nto apply to blob 7ea52b1, finds the blob in your repository, and\nprepares a temporary index with \"update-index -z --index-info\"\non l. 58 successfully, tries to apply the patch again on l. 63.\n\nHowever, the patch contents and the blob object name recorded on\nthe index line are not necessarily consistent if you hand edited\nthe patch (IOW, the context lines in the patch contents may not\nmatch blob 7ea52b1).\n"},{"id":"25158","messageId":"ebk5tf$31k$1@sea.gmane.org","threadId":"5214","inReplyTo":"7vslk2rbq8.fsf@assigned-by-dhcp.cox.net","subject":"Re: How to resolve git-am conflict (possible bug)","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2006-08-12T09:10:40Z","receivedAt":"2006-08-12T09:10:40Z","isPatch":false,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Junio C Hamano wrote:\n\n> Jakub Narebski <jnareb@gmail.com> writes:\n> \n>> Third, I wonder why it printed the same error message _twice_.\n> \n> Do you have blob 7ea52b1?  Otherwise you would not see two \"does\n> not apply\" messages, so I suspect you do.  Does the patch\n> cleanly apply to that blob?\n> \n> More likely explanation is that you edited the patch by hand for\n> some reason, and made it inapplicable to the base blob the\n> \"index\" line records.\n\nYes, I have edited \"post-sub-rename\" patch by hand (by script) in attempt\nfor it to apply cleanly to the top of \"pre-sub-rename\" development branch.\nBTW patch applies cleanly to merge-base of the branch the patch is from and\nthe branch it is applied to.\n\nWhy do we not record commit id in patch? And how git-rebase deals with this? \n\n> The first \"patch does not apply\" comes from ll. 363 of git-am.\n> After it fails because the patch does not apply to the version\n> of gitweb.perl in your index, since you told it to fall back to\n> three-way merge, l. 391 calls fall_back_3way, which inspects the\n> patch, finds the \"index\" line and notices that the patch claims\n> to apply to blob 7ea52b1, finds the blob in your repository, and\n> prepares a temporary index with \"update-index -z --index-info\"\n> on l. 58 successfully, tries to apply the patch again on l. 63.\n> \n> However, the patch contents and the blob object name recorded on\n> the index line are not necessarily consistent if you hand edited\n> the patch (IOW, the context lines in the patch contents may not\n> match blob 7ea52b1).\n\nIt would be nice then if git-am was more verbose, for example\n\"Applying patch to blob 7ea52b1... gitweb/gitweb.perl\" or something\nlike that.\n\nAnd first complaint still apply: in git-am(1) there is precious few\ndocumentation (or at least references) about _how_ to resolve merge\nconflict or failed patch (does git-apply creates *.orig and *.rej \nfiles?)\n\n-- \nJakub Narebski\nWarsaw, Poland\nShadeHawk on #git\n"},{"id":"25159","messageId":"ebk6be$31k$2@sea.gmane.org","threadId":"5214","inReplyTo":"ebk5tf$31k$1@sea.gmane.org","subject":"Re: How to resolve git-am conflict (possible bug)","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2006-08-12T09:18:07Z","receivedAt":"2006-08-12T09:18:07Z","isPatch":false,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Jakub Narebski wrote:\n\n> Junio C Hamano wrote:\n[...] \n>> More likely explanation is that you edited the patch by hand for\n>> some reason, and made it inapplicable to the base blob the\n>> \"index\" line records.\n\nOriginal (not edited) version of patch generates nice merge conflict.\n\n> It would be nice then if git-am was more verbose, for example\n> \"Applying patch to blob 7ea52b1... gitweb/gitweb.perl\" or something\n> like that.\n\nOr at least some information what git-am is attempting before second \n'patch failed' error message...\n\n-- \nJakub Narebski\nWarsaw, Poland\nShadeHawk on #git\n"},{"id":"25160","messageId":"Pine.LNX.4.63.0608121146550.10541@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"5214","inReplyTo":"ebk5tf$31k$1@sea.gmane.org","subject":"Re: How to resolve git-am conflict (possible bug)","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2006-08-12T09:52:54Z","receivedAt":"2006-08-12T09:52:54Z","isPatch":false,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Sat, 12 Aug 2006, Jakub Narebski wrote:\n\n> Why do we not record commit id in patch?\n\nBecause we do not have to.\n\nThere is a commit SHA1 which guarantees the integrity of the commit, and \nthen you can generate the patch-id with\n\n\t$ git show $commit | git patch-id\n\n> And how git-rebase deals with this? \n\nAFAICT by cherry-picking the commit, and if nothing changed in the index, \njust ignoring the patch.\n\nHth,\nDscho\n"},{"id":"25165","messageId":"7v7j1dpwe6.fsf@assigned-by-dhcp.cox.net","threadId":"5214","inReplyTo":"Pine.LNX.4.63.0608121146550.10541@wbgn013.biozentrum.uni-wuerzburg.de","subject":"Re: How to resolve git-am conflict (possible bug)","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-08-12T19:49:21Z","receivedAt":"2006-08-12T19:49:21Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:\n\n> On Sat, 12 Aug 2006, Jakub Narebski wrote:\n>\n>> Why do we not record commit id in patch?\n>\n> Because we do not have to.\n\nMore relevant point is that more often than not it does not\nhelp.  The most common workflow that involves format-patch\noutput is to give your change to somebody else that does not\nhave (and does not merge with) your repository, and the commit\nthat is formatted is your own.  The other party does not have\nthe commit so telling its object name is useless.\n\nEven if you recorded the commit object name of the pre-image\nthat would not help unless the patch happens to be the first\npatch in a series forked from something the other party has.\n\n>> And how git-rebase deals with this? \n\nIt applies the format-patch output using \"git-am -3\".  In this\ncase, the preimage blobs recorded on \"index\" lines are\nguaranteed to exist in the repository the \"git-am\" runs, because\nthe patches are coming from the same repository.  And the\npatches are obviously not munged (we do not give you a chance to\nmuck with them between the time we generate and we apply) so\nthey are guaranteed to apply to the blobs recorded on \"index\"\nlines.  Running the three-way fallback procedure on a change\nalready present on the new \"onto\" branch results in no change in\nthe index and that is how it notices the patch has already been\napplied.\n"},{"id":"25166","messageId":"7vzme9oht9.fsf@assigned-by-dhcp.cox.net","threadId":"5214","inReplyTo":"ebk6be$31k$2@sea.gmane.org","subject":"Re: How to resolve git-am conflict (possible bug)","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-08-12T19:49:38Z","receivedAt":"2006-08-12T19:49:38Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jakub Narebski <jnareb@gmail.com> writes:\n\n> Jakub Narebski wrote:\n>\n>> Junio C Hamano wrote:\n> [...] \n>>> More likely explanation is that you edited the patch by hand for\n>>> some reason, and made it inapplicable to the base blob the\n>>> \"index\" line records.\n>\n> Original (not edited) version of patch generates nice merge conflict.\n\nThe moral of the story is that either (1) you hand edit the\npatch to apply cleanly to the target, or (2) if you do edit, do\nnot make it inapplicable to the preimage blob recorded in the\npatch, so that -3 still would work.\n\n>> It would be nice then if git-am was more verbose, for example\n>> \"Applying patch to blob 7ea52b1... gitweb/gitweb.perl\" or something\n>> like that.\n>\n> Or at least some information what git-am is attempting before second \n> 'patch failed' error message...\n\nHow about this?\n\ndiff --git a/git-am.sh b/git-am.sh\nindex 04f0119..aff4bb7 100755\n--- a/git-am.sh\n+++ b/git-am.sh\n@@ -45,6 +45,12 @@ go_next () {\n \tthis=$next\n }\n \n+cannot_fallback () {\n+\techo >&2 \"$1\"\n+\techo >&2 \"Cannot fall back to three-way merge.\"\n+\texit 1\n+}\n+\n fall_back_3way () {\n     O_OBJECT=`cd \"$GIT_OBJECT_DIRECTORY\" && pwd`\n \n@@ -52,19 +58,23 @@ fall_back_3way () {\n     mkdir \"$dotest/patch-merge-tmp-dir\"\n \n     # First see if the patch records the index info that we can use.\n-    if git-apply -z --index-info \"$dotest/patch\" \\\n-\t>\"$dotest/patch-merge-index-info\" 2>/dev/null &&\n-\tGIT_INDEX_FILE=\"$dotest/patch-merge-tmp-index\" \\\n-\tgit-update-index -z --index-info <\"$dotest/patch-merge-index-info\" &&\n-\tGIT_INDEX_FILE=\"$dotest/patch-merge-tmp-index\" \\\n-\tgit-write-tree >\"$dotest/patch-merge-base+\" &&\n-\t# index has the base tree now.\n-\tGIT_INDEX_FILE=\"$dotest/patch-merge-tmp-index\" \\\n+    git-apply -z --index-info \"$dotest/patch\" \\\n+    \t>\"$dotest/patch-merge-index-info\" &&\n+    GIT_INDEX_FILE=\"$dotest/patch-merge-tmp-index\" \\\n+    git-update-index -z --index-info <\"$dotest/patch-merge-index-info\" &&\n+    GIT_INDEX_FILE=\"$dotest/patch-merge-tmp-index\" \\\n+    git-write-tree >\"$dotest/patch-merge-base+\" ||\n+    cannot_fallback \"Patch does not record usable index information.\"\n+\n+    echo Using index info to reconstruct a base tree...\n+    if GIT_INDEX_FILE=\"$dotest/patch-merge-tmp-index\" \\\n \tgit-apply $binary --cached <\"$dotest/patch\"\n     then\n-\techo Using index info to reconstruct a base tree...\n \tmv \"$dotest/patch-merge-base+\" \"$dotest/patch-merge-base\"\n \tmv \"$dotest/patch-merge-tmp-index\" \"$dotest/patch-merge-index\"\n+    else\n+        cannot_fallback \"Did you hand edit your patch?\n+It does not apply to blobs recorded in its index.\"\n     fi\n \n     test -f \"$dotest/patch-merge-index\" &&\n"}]}