1#!/usr/bin/perl 2 3# gitweb - simple web interface to track changes in git repositories 4# 5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org> 6# (C) 2005, Christian Gierke 7# 8# This program is licensed under the GPLv2 9 10use strict; 11use warnings; 12use CGI qw(:standard :escapeHTML -nosticky); 13use CGI::Util qw(unescape); 14use CGI::Carp qw(fatalsToBrowser); 15use Encode; 16use Fcntl ':mode'; 17use File::Find qw(); 18use File::Basename qw(basename); 19binmode STDOUT,':utf8'; 20 21BEGIN{ 22 CGI->compile()if$ENV{'MOD_PERL'}; 23} 24 25our$cgi= new CGI; 26our$version="++GIT_VERSION++"; 27our$my_url=$cgi->url(); 28our$my_uri=$cgi->url(-absolute =>1); 29 30# core git executable to use 31# this can just be "git" if your webserver has a sensible PATH 32our$GIT="++GIT_BINDIR++/git"; 33 34# absolute fs-path which will be prepended to the project path 35#our $projectroot = "/pub/scm"; 36our$projectroot="++GITWEB_PROJECTROOT++"; 37 38# fs traversing limit for getting project list 39# the number is relative to the projectroot 40our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 41 42# target of the home link on top of all pages 43our$home_link=$my_uri||"/"; 44 45# string of the home link on top of all pages 46our$home_link_str="++GITWEB_HOME_LINK_STR++"; 47 48# name of your site or organization to appear in page titles 49# replace this with something more descriptive for clearer bookmarks 50our$site_name="++GITWEB_SITENAME++" 51|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 52 53# filename of html text to include at top of each page 54our$site_header="++GITWEB_SITE_HEADER++"; 55# html text to include at home page 56our$home_text="++GITWEB_HOMETEXT++"; 57# filename of html text to include at bottom of each page 58our$site_footer="++GITWEB_SITE_FOOTER++"; 59 60# URI of stylesheets 61our@stylesheets= ("++GITWEB_CSS++"); 62# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 63our$stylesheet=undef; 64# URI of GIT logo (72x27 size) 65our$logo="++GITWEB_LOGO++"; 66# URI of GIT favicon, assumed to be image/png type 67our$favicon="++GITWEB_FAVICON++"; 68 69# URI and label (title) of GIT logo link 70#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 71#our $logo_label = "git documentation"; 72our$logo_url="http://git.or.cz/"; 73our$logo_label="git homepage"; 74 75# source of projects list 76our$projects_list="++GITWEB_LIST++"; 77 78# the width (in characters) of the projects list "Description" column 79our$projects_list_description_width=25; 80 81# default order of projects list 82# valid values are none, project, descr, owner, and age 83our$default_projects_order="project"; 84 85# show repository only if this file exists 86# (only effective if this variable evaluates to true) 87our$export_ok="++GITWEB_EXPORT_OK++"; 88 89# only allow viewing of repositories also shown on the overview page 90our$strict_export="++GITWEB_STRICT_EXPORT++"; 91 92# list of git base URLs used for URL to where fetch project from, 93# i.e. full URL is "$git_base_url/$project" 94our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 95 96# default blob_plain mimetype and default charset for text/plain blob 97our$default_blob_plain_mimetype='text/plain'; 98our$default_text_plain_charset=undef; 99 100# file to use for guessing MIME types before trying /etc/mime.types 101# (relative to the current git repository) 102our$mimetypes_file=undef; 103 104# assume this charset if line contains non-UTF-8 characters; 105# it should be valid encoding (see Encoding::Supported(3pm) for list), 106# for which encoding all byte sequences are valid, for example 107# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 108# could be even 'utf-8' for the old behavior) 109our$fallback_encoding='latin1'; 110 111# rename detection options for git-diff and git-diff-tree 112# - default is '-M', with the cost proportional to 113# (number of removed files) * (number of new files). 114# - more costly is '-C' (which implies '-M'), with the cost proportional to 115# (number of changed files + number of removed files) * (number of new files) 116# - even more costly is '-C', '--find-copies-harder' with cost 117# (number of files in the original tree) * (number of new files) 118# - one might want to include '-B' option, e.g. '-B', '-M' 119our@diff_opts= ('-M');# taken from git_commit 120 121# information about snapshot formats that gitweb is capable of serving 122our%known_snapshot_formats= ( 123# name => { 124# 'display' => display name, 125# 'type' => mime type, 126# 'suffix' => filename suffix, 127# 'format' => --format for git-archive, 128# 'compressor' => [compressor command and arguments] 129# (array reference, optional)} 130# 131'tgz'=> { 132'display'=>'tar.gz', 133'type'=>'application/x-gzip', 134'suffix'=>'.tar.gz', 135'format'=>'tar', 136'compressor'=> ['gzip']}, 137 138'tbz2'=> { 139'display'=>'tar.bz2', 140'type'=>'application/x-bzip2', 141'suffix'=>'.tar.bz2', 142'format'=>'tar', 143'compressor'=> ['bzip2']}, 144 145'zip'=> { 146'display'=>'zip', 147'type'=>'application/x-zip', 148'suffix'=>'.zip', 149'format'=>'zip'}, 150); 151 152# Aliases so we understand old gitweb.snapshot values in repository 153# configuration. 154our%known_snapshot_format_aliases= ( 155'gzip'=>'tgz', 156'bzip2'=>'tbz2', 157 158# backward compatibility: legacy gitweb config support 159'x-gzip'=>undef,'gz'=>undef, 160'x-bzip2'=>undef,'bz2'=>undef, 161'x-zip'=>undef,''=>undef, 162); 163 164# You define site-wide feature defaults here; override them with 165# $GITWEB_CONFIG as necessary. 166our%feature= ( 167# feature => { 168# 'sub' => feature-sub (subroutine), 169# 'override' => allow-override (boolean), 170# 'default' => [ default options...] (array reference)} 171# 172# if feature is overridable (it means that allow-override has true value), 173# then feature-sub will be called with default options as parameters; 174# return value of feature-sub indicates if to enable specified feature 175# 176# if there is no 'sub' key (no feature-sub), then feature cannot be 177# overriden 178# 179# use gitweb_check_feature(<feature>) to check if <feature> is enabled 180 181# Enable the 'blame' blob view, showing the last commit that modified 182# each line in the file. This can be very CPU-intensive. 183 184# To enable system wide have in $GITWEB_CONFIG 185# $feature{'blame'}{'default'} = [1]; 186# To have project specific config enable override in $GITWEB_CONFIG 187# $feature{'blame'}{'override'} = 1; 188# and in project config gitweb.blame = 0|1; 189'blame'=> { 190'sub'=> \&feature_blame, 191'override'=>0, 192'default'=> [0]}, 193 194# Enable the 'snapshot' link, providing a compressed archive of any 195# tree. This can potentially generate high traffic if you have large 196# project. 197 198# Value is a list of formats defined in %known_snapshot_formats that 199# you wish to offer. 200# To disable system wide have in $GITWEB_CONFIG 201# $feature{'snapshot'}{'default'} = []; 202# To have project specific config enable override in $GITWEB_CONFIG 203# $feature{'snapshot'}{'override'} = 1; 204# and in project config, a comma-separated list of formats or "none" 205# to disable. Example: gitweb.snapshot = tbz2,zip; 206'snapshot'=> { 207'sub'=> \&feature_snapshot, 208'override'=>0, 209'default'=> ['tgz']}, 210 211# Enable text search, which will list the commits which match author, 212# committer or commit text to a given string. Enabled by default. 213# Project specific override is not supported. 214'search'=> { 215'override'=>0, 216'default'=> [1]}, 217 218# Enable grep search, which will list the files in currently selected 219# tree containing the given string. Enabled by default. This can be 220# potentially CPU-intensive, of course. 221 222# To enable system wide have in $GITWEB_CONFIG 223# $feature{'grep'}{'default'} = [1]; 224# To have project specific config enable override in $GITWEB_CONFIG 225# $feature{'grep'}{'override'} = 1; 226# and in project config gitweb.grep = 0|1; 227'grep'=> { 228'override'=>0, 229'default'=> [1]}, 230 231# Enable the pickaxe search, which will list the commits that modified 232# a given string in a file. This can be practical and quite faster 233# alternative to 'blame', but still potentially CPU-intensive. 234 235# To enable system wide have in $GITWEB_CONFIG 236# $feature{'pickaxe'}{'default'} = [1]; 237# To have project specific config enable override in $GITWEB_CONFIG 238# $feature{'pickaxe'}{'override'} = 1; 239# and in project config gitweb.pickaxe = 0|1; 240'pickaxe'=> { 241'sub'=> \&feature_pickaxe, 242'override'=>0, 243'default'=> [1]}, 244 245# Make gitweb use an alternative format of the URLs which can be 246# more readable and natural-looking: project name is embedded 247# directly in the path and the query string contains other 248# auxiliary information. All gitweb installations recognize 249# URL in either format; this configures in which formats gitweb 250# generates links. 251 252# To enable system wide have in $GITWEB_CONFIG 253# $feature{'pathinfo'}{'default'} = [1]; 254# Project specific override is not supported. 255 256# Note that you will need to change the default location of CSS, 257# favicon, logo and possibly other files to an absolute URL. Also, 258# if gitweb.cgi serves as your indexfile, you will need to force 259# $my_uri to contain the script name in your $GITWEB_CONFIG. 260'pathinfo'=> { 261'override'=>0, 262'default'=> [0]}, 263 264# Make gitweb consider projects in project root subdirectories 265# to be forks of existing projects. Given project $projname.git, 266# projects matching $projname/*.git will not be shown in the main 267# projects list, instead a '+' mark will be added to $projname 268# there and a 'forks' view will be enabled for the project, listing 269# all the forks. If project list is taken from a file, forks have 270# to be listed after the main project. 271 272# To enable system wide have in $GITWEB_CONFIG 273# $feature{'forks'}{'default'} = [1]; 274# Project specific override is not supported. 275'forks'=> { 276'override'=>0, 277'default'=> [0]}, 278 279# Insert custom links to the action bar of all project pages. 280# This enables you mainly to link to third-party scripts integrating 281# into gitweb; e.g. git-browser for graphical history representation 282# or custom web-based repository administration interface. 283 284# The 'default' value consists of a list of triplets in the form 285# (label, link, position) where position is the label after which 286# to inster the link and link is a format string where %n expands 287# to the project name, %f to the project path within the filesystem, 288# %h to the current hash (h gitweb parameter) and %b to the current 289# hash base (hb gitweb parameter). 290 291# To enable system wide have in $GITWEB_CONFIG e.g. 292# $feature{'actions'}{'default'} = [('graphiclog', 293# '/git-browser/by-commit.html?r=%n', 'summary')]; 294# Project specific override is not supported. 295'actions'=> { 296'override'=>0, 297'default'=> []}, 298); 299 300sub gitweb_check_feature { 301my($name) =@_; 302return unlessexists$feature{$name}; 303my($sub,$override,@defaults) = ( 304$feature{$name}{'sub'}, 305$feature{$name}{'override'}, 306@{$feature{$name}{'default'}}); 307if(!$override) {return@defaults; } 308if(!defined$sub) { 309warn"feature$nameis not overrideable"; 310return@defaults; 311} 312return$sub->(@defaults); 313} 314 315sub feature_blame { 316my($val) = git_get_project_config('blame','--bool'); 317 318if($valeq'true') { 319return1; 320}elsif($valeq'false') { 321return0; 322} 323 324return$_[0]; 325} 326 327sub feature_snapshot { 328my(@fmts) =@_; 329 330my($val) = git_get_project_config('snapshot'); 331 332if($val) { 333@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 334} 335 336return@fmts; 337} 338 339sub feature_grep { 340my($val) = git_get_project_config('grep','--bool'); 341 342if($valeq'true') { 343return(1); 344}elsif($valeq'false') { 345return(0); 346} 347 348return($_[0]); 349} 350 351sub feature_pickaxe { 352my($val) = git_get_project_config('pickaxe','--bool'); 353 354if($valeq'true') { 355return(1); 356}elsif($valeq'false') { 357return(0); 358} 359 360return($_[0]); 361} 362 363# checking HEAD file with -e is fragile if the repository was 364# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 365# and then pruned. 366sub check_head_link { 367my($dir) =@_; 368my$headfile="$dir/HEAD"; 369return((-e $headfile) || 370(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 371} 372 373sub check_export_ok { 374my($dir) =@_; 375return(check_head_link($dir) && 376(!$export_ok|| -e "$dir/$export_ok")); 377} 378 379# process alternate names for backward compatibility 380# filter out unsupported (unknown) snapshot formats 381sub filter_snapshot_fmts { 382my@fmts=@_; 383 384@fmts=map{ 385exists$known_snapshot_format_aliases{$_} ? 386$known_snapshot_format_aliases{$_} :$_}@fmts; 387@fmts=grep(exists$known_snapshot_formats{$_},@fmts); 388 389} 390 391our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 392if(-e $GITWEB_CONFIG) { 393do$GITWEB_CONFIG; 394}else{ 395our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 396do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 397} 398 399# version of the core git binary 400our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 401 402$projects_list||=$projectroot; 403 404# ====================================================================== 405# input validation and dispatch 406our$action=$cgi->param('a'); 407if(defined$action) { 408if($action=~m/[^0-9a-zA-Z\.\-_]/) { 409 die_error(400,"Invalid action parameter"); 410} 411} 412 413# parameters which are pathnames 414our$project=$cgi->param('p'); 415if(defined$project) { 416if(!validate_pathname($project) || 417!(-d "$projectroot/$project") || 418!check_head_link("$projectroot/$project") || 419($export_ok&& !(-e "$projectroot/$project/$export_ok")) || 420($strict_export&& !project_in_list($project))) { 421undef$project; 422 die_error(404,"No such project"); 423} 424} 425 426our$file_name=$cgi->param('f'); 427if(defined$file_name) { 428if(!validate_pathname($file_name)) { 429 die_error(400,"Invalid file parameter"); 430} 431} 432 433our$file_parent=$cgi->param('fp'); 434if(defined$file_parent) { 435if(!validate_pathname($file_parent)) { 436 die_error(400,"Invalid file parent parameter"); 437} 438} 439 440# parameters which are refnames 441our$hash=$cgi->param('h'); 442if(defined$hash) { 443if(!validate_refname($hash)) { 444 die_error(400,"Invalid hash parameter"); 445} 446} 447 448our$hash_parent=$cgi->param('hp'); 449if(defined$hash_parent) { 450if(!validate_refname($hash_parent)) { 451 die_error(400,"Invalid hash parent parameter"); 452} 453} 454 455our$hash_base=$cgi->param('hb'); 456if(defined$hash_base) { 457if(!validate_refname($hash_base)) { 458 die_error(400,"Invalid hash base parameter"); 459} 460} 461 462my%allowed_options= ( 463"--no-merges"=> [qw(rss atom log shortlog history)], 464); 465 466our@extra_options=$cgi->param('opt'); 467if(defined@extra_options) { 468foreachmy$opt(@extra_options) { 469if(not exists$allowed_options{$opt}) { 470 die_error(400,"Invalid option parameter"); 471} 472if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 473 die_error(400,"Invalid option parameter for this action"); 474} 475} 476} 477 478our$hash_parent_base=$cgi->param('hpb'); 479if(defined$hash_parent_base) { 480if(!validate_refname($hash_parent_base)) { 481 die_error(400,"Invalid hash parent base parameter"); 482} 483} 484 485# other parameters 486our$page=$cgi->param('pg'); 487if(defined$page) { 488if($page=~m/[^0-9]/) { 489 die_error(400,"Invalid page parameter"); 490} 491} 492 493our$searchtype=$cgi->param('st'); 494if(defined$searchtype) { 495if($searchtype=~m/[^a-z]/) { 496 die_error(400,"Invalid searchtype parameter"); 497} 498} 499 500our$search_use_regexp=$cgi->param('sr'); 501 502our$searchtext=$cgi->param('s'); 503our$search_regexp; 504if(defined$searchtext) { 505if(length($searchtext) <2) { 506 die_error(403,"At least two characters are required for search parameter"); 507} 508$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 509} 510 511# now read PATH_INFO and use it as alternative to parameters 512sub evaluate_path_info { 513return ifdefined$project; 514my$path_info=$ENV{"PATH_INFO"}; 515return if!$path_info; 516$path_info=~ s,^/+,,; 517return if!$path_info; 518# find which part of PATH_INFO is project 519$project=$path_info; 520$project=~ s,/+$,,; 521while($project&& !check_head_link("$projectroot/$project")) { 522$project=~ s,/*[^/]*$,,; 523} 524# validate project 525$project= validate_pathname($project); 526if(!$project|| 527($export_ok&& !-e "$projectroot/$project/$export_ok") || 528($strict_export&& !project_in_list($project))) { 529undef$project; 530return; 531} 532# do not change any parameters if an action is given using the query string 533return if$action; 534$path_info=~ s,^\Q$project\E/*,,; 535my($refname,$pathname) =split(/:/,$path_info,2); 536if(defined$pathname) { 537# we got "project.git/branch:filename" or "project.git/branch:dir/" 538# we could use git_get_type(branch:pathname), but it needs $git_dir 539$pathname=~ s,^/+,,; 540if(!$pathname||substr($pathname, -1)eq"/") { 541$action||="tree"; 542$pathname=~ s,/$,,; 543}else{ 544$action||="blob_plain"; 545} 546$hash_base||= validate_refname($refname); 547$file_name||= validate_pathname($pathname); 548}elsif(defined$refname) { 549# we got "project.git/branch" 550$action||="shortlog"; 551$hash||= validate_refname($refname); 552} 553} 554evaluate_path_info(); 555 556# path to the current git repository 557our$git_dir; 558$git_dir="$projectroot/$project"if$project; 559 560# dispatch 561my%actions= ( 562"blame"=> \&git_blame, 563"blobdiff"=> \&git_blobdiff, 564"blobdiff_plain"=> \&git_blobdiff_plain, 565"blob"=> \&git_blob, 566"blob_plain"=> \&git_blob_plain, 567"commitdiff"=> \&git_commitdiff, 568"commitdiff_plain"=> \&git_commitdiff_plain, 569"commit"=> \&git_commit, 570"forks"=> \&git_forks, 571"heads"=> \&git_heads, 572"history"=> \&git_history, 573"log"=> \&git_log, 574"rss"=> \&git_rss, 575"atom"=> \&git_atom, 576"search"=> \&git_search, 577"search_help"=> \&git_search_help, 578"shortlog"=> \&git_shortlog, 579"summary"=> \&git_summary, 580"tag"=> \&git_tag, 581"tags"=> \&git_tags, 582"tree"=> \&git_tree, 583"snapshot"=> \&git_snapshot, 584"object"=> \&git_object, 585# those below don't need $project 586"opml"=> \&git_opml, 587"project_list"=> \&git_project_list, 588"project_index"=> \&git_project_index, 589); 590 591if(!defined$action) { 592if(defined$hash) { 593$action= git_get_type($hash); 594}elsif(defined$hash_base&&defined$file_name) { 595$action= git_get_type("$hash_base:$file_name"); 596}elsif(defined$project) { 597$action='summary'; 598}else{ 599$action='project_list'; 600} 601} 602if(!defined($actions{$action})) { 603 die_error(400,"Unknown action"); 604} 605if($action!~m/^(opml|project_list|project_index)$/&& 606!$project) { 607 die_error(400,"Project needed"); 608} 609$actions{$action}->(); 610exit; 611 612## ====================================================================== 613## action links 614 615sub href (%) { 616my%params=@_; 617# default is to use -absolute url() i.e. $my_uri 618my$href=$params{-full} ?$my_url:$my_uri; 619 620# XXX: Warning: If you touch this, check the search form for updating, 621# too. 622 623my@mapping= ( 624 project =>"p", 625 action =>"a", 626 file_name =>"f", 627 file_parent =>"fp", 628 hash =>"h", 629 hash_parent =>"hp", 630 hash_base =>"hb", 631 hash_parent_base =>"hpb", 632 page =>"pg", 633 order =>"o", 634 searchtext =>"s", 635 searchtype =>"st", 636 snapshot_format =>"sf", 637 extra_options =>"opt", 638 search_use_regexp =>"sr", 639); 640my%mapping=@mapping; 641 642$params{'project'} =$projectunlessexists$params{'project'}; 643 644if($params{-replay}) { 645while(my($name,$symbol) =each%mapping) { 646if(!exists$params{$name}) { 647# to allow for multivalued params we use arrayref form 648$params{$name} = [$cgi->param($symbol) ]; 649} 650} 651} 652 653my($use_pathinfo) = gitweb_check_feature('pathinfo'); 654if($use_pathinfo) { 655# use PATH_INFO for project name 656$href.="/".esc_url($params{'project'})ifdefined$params{'project'}; 657delete$params{'project'}; 658 659# Summary just uses the project path URL 660if(defined$params{'action'} &&$params{'action'}eq'summary') { 661delete$params{'action'}; 662} 663} 664 665# now encode the parameters explicitly 666my@result= (); 667for(my$i=0;$i<@mapping;$i+=2) { 668my($name,$symbol) = ($mapping[$i],$mapping[$i+1]); 669if(defined$params{$name}) { 670if(ref($params{$name})eq"ARRAY") { 671foreachmy$par(@{$params{$name}}) { 672push@result,$symbol."=". esc_param($par); 673} 674}else{ 675push@result,$symbol."=". esc_param($params{$name}); 676} 677} 678} 679$href.="?".join(';',@result)ifscalar@result; 680 681return$href; 682} 683 684 685## ====================================================================== 686## validation, quoting/unquoting and escaping 687 688sub validate_pathname { 689my$input=shift||returnundef; 690 691# no '.' or '..' as elements of path, i.e. no '.' nor '..' 692# at the beginning, at the end, and between slashes. 693# also this catches doubled slashes 694if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 695returnundef; 696} 697# no null characters 698if($input=~m!\0!) { 699returnundef; 700} 701return$input; 702} 703 704sub validate_refname { 705my$input=shift||returnundef; 706 707# textual hashes are O.K. 708if($input=~m/^[0-9a-fA-F]{40}$/) { 709return$input; 710} 711# it must be correct pathname 712$input= validate_pathname($input) 713orreturnundef; 714# restrictions on ref name according to git-check-ref-format 715if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { 716returnundef; 717} 718return$input; 719} 720 721# decode sequences of octets in utf8 into Perl's internal form, 722# which is utf-8 with utf8 flag set if needed. gitweb writes out 723# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning 724sub to_utf8 { 725my$str=shift; 726if(utf8::valid($str)) { 727 utf8::decode($str); 728return$str; 729}else{ 730return decode($fallback_encoding,$str, Encode::FB_DEFAULT); 731} 732} 733 734# quote unsafe chars, but keep the slash, even when it's not 735# correct, but quoted slashes look too horrible in bookmarks 736sub esc_param { 737my$str=shift; 738$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg; 739$str=~s/\+/%2B/g; 740$str=~s/ /\+/g; 741return$str; 742} 743 744# quote unsafe chars in whole URL, so some charactrs cannot be quoted 745sub esc_url { 746my$str=shift; 747$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg; 748$str=~s/\+/%2B/g; 749$str=~s/ /\+/g; 750return$str; 751} 752 753# replace invalid utf8 character with SUBSTITUTION sequence 754sub esc_html ($;%) { 755my$str=shift; 756my%opts=@_; 757 758$str= to_utf8($str); 759$str=$cgi->escapeHTML($str); 760if($opts{'-nbsp'}) { 761$str=~s/ / /g; 762} 763$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg; 764return$str; 765} 766 767# quote control characters and escape filename to HTML 768sub esc_path { 769my$str=shift; 770my%opts=@_; 771 772$str= to_utf8($str); 773$str=$cgi->escapeHTML($str); 774if($opts{'-nbsp'}) { 775$str=~s/ / /g; 776} 777$str=~ s|([[:cntrl:]])|quot_cec($1)|eg; 778return$str; 779} 780 781# Make control characters "printable", using character escape codes (CEC) 782sub quot_cec { 783my$cntrl=shift; 784my%opts=@_; 785my%es= (# character escape codes, aka escape sequences 786"\t"=>'\t',# tab (HT) 787"\n"=>'\n',# line feed (LF) 788"\r"=>'\r',# carrige return (CR) 789"\f"=>'\f',# form feed (FF) 790"\b"=>'\b',# backspace (BS) 791"\a"=>'\a',# alarm (bell) (BEL) 792"\e"=>'\e',# escape (ESC) 793"\013"=>'\v',# vertical tab (VT) 794"\000"=>'\0',# nul character (NUL) 795); 796my$chr= ( (exists$es{$cntrl}) 797?$es{$cntrl} 798:sprintf('\%03o',ord($cntrl)) ); 799if($opts{-nohtml}) { 800return$chr; 801}else{ 802return"<span class=\"cntrl\">$chr</span>"; 803} 804} 805 806# Alternatively use unicode control pictures codepoints, 807# Unicode "printable representation" (PR) 808sub quot_upr { 809my$cntrl=shift; 810my%opts=@_; 811 812my$chr=sprintf('&#%04d;',0x2400+ord($cntrl)); 813if($opts{-nohtml}) { 814return$chr; 815}else{ 816return"<span class=\"cntrl\">$chr</span>"; 817} 818} 819 820# git may return quoted and escaped filenames 821sub unquote { 822my$str=shift; 823 824sub unq { 825my$seq=shift; 826my%es= (# character escape codes, aka escape sequences 827't'=>"\t",# tab (HT, TAB) 828'n'=>"\n",# newline (NL) 829'r'=>"\r",# return (CR) 830'f'=>"\f",# form feed (FF) 831'b'=>"\b",# backspace (BS) 832'a'=>"\a",# alarm (bell) (BEL) 833'e'=>"\e",# escape (ESC) 834'v'=>"\013",# vertical tab (VT) 835); 836 837if($seq=~m/^[0-7]{1,3}$/) { 838# octal char sequence 839returnchr(oct($seq)); 840}elsif(exists$es{$seq}) { 841# C escape sequence, aka character escape code 842return$es{$seq}; 843} 844# quoted ordinary character 845return$seq; 846} 847 848if($str=~m/^"(.*)"$/) { 849# needs unquoting 850$str=$1; 851$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg; 852} 853return$str; 854} 855 856# escape tabs (convert tabs to spaces) 857sub untabify { 858my$line=shift; 859 860while((my$pos=index($line,"\t")) != -1) { 861if(my$count= (8- ($pos%8))) { 862my$spaces=' ' x $count; 863$line=~s/\t/$spaces/; 864} 865} 866 867return$line; 868} 869 870sub project_in_list { 871my$project=shift; 872my@list= git_get_projects_list(); 873return@list&&scalar(grep{$_->{'path'}eq$project}@list); 874} 875 876## ---------------------------------------------------------------------- 877## HTML aware string manipulation 878 879# Try to chop given string on a word boundary between position 880# $len and $len+$add_len. If there is no word boundary there, 881# chop at $len+$add_len. Do not chop if chopped part plus ellipsis 882# (marking chopped part) would be longer than given string. 883sub chop_str { 884my$str=shift; 885my$len=shift; 886my$add_len=shift||10; 887my$where=shift||'right';# 'left' | 'center' | 'right' 888 889# Make sure perl knows it is utf8 encoded so we don't 890# cut in the middle of a utf8 multibyte char. 891$str= to_utf8($str); 892 893# allow only $len chars, but don't cut a word if it would fit in $add_len 894# if it doesn't fit, cut it if it's still longer than the dots we would add 895# remove chopped character entities entirely 896 897# when chopping in the middle, distribute $len into left and right part 898# return early if chopping wouldn't make string shorter 899if($whereeq'center') { 900return$strif($len+5>=length($str));# filler is length 5 901$len=int($len/2); 902}else{ 903return$strif($len+4>=length($str));# filler is length 4 904} 905 906# regexps: ending and beginning with word part up to $add_len 907my$endre=qr/.{$len}\w{0,$add_len}/; 908my$begre=qr/\w{0,$add_len}.{$len}/; 909 910if($whereeq'left') { 911$str=~m/^(.*?)($begre)$/; 912my($lead,$body) = ($1,$2); 913if(length($lead) >4) { 914$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/); 915$lead=" ..."; 916} 917return"$lead$body"; 918 919}elsif($whereeq'center') { 920$str=~m/^($endre)(.*)$/; 921my($left,$str) = ($1,$2); 922$str=~m/^(.*?)($begre)$/; 923my($mid,$right) = ($1,$2); 924if(length($mid) >5) { 925$left=~s/&[^;]*$//; 926$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/); 927$mid=" ... "; 928} 929return"$left$mid$right"; 930 931}else{ 932$str=~m/^($endre)(.*)$/; 933my$body=$1; 934my$tail=$2; 935if(length($tail) >4) { 936$body=~s/&[^;]*$//; 937$tail="... "; 938} 939return"$body$tail"; 940} 941} 942 943# takes the same arguments as chop_str, but also wraps a <span> around the 944# result with a title attribute if it does get chopped. Additionally, the 945# string is HTML-escaped. 946sub chop_and_escape_str { 947my($str) =@_; 948 949my$chopped= chop_str(@_); 950if($choppedeq$str) { 951return esc_html($chopped); 952}else{ 953$str=~s/([[:cntrl:]])/?/g; 954return$cgi->span({-title=>$str}, esc_html($chopped)); 955} 956} 957 958## ---------------------------------------------------------------------- 959## functions returning short strings 960 961# CSS class for given age value (in seconds) 962sub age_class { 963my$age=shift; 964 965if(!defined$age) { 966return"noage"; 967}elsif($age<60*60*2) { 968return"age0"; 969}elsif($age<60*60*24*2) { 970return"age1"; 971}else{ 972return"age2"; 973} 974} 975 976# convert age in seconds to "nn units ago" string 977sub age_string { 978my$age=shift; 979my$age_str; 980 981if($age>60*60*24*365*2) { 982$age_str= (int$age/60/60/24/365); 983$age_str.=" years ago"; 984}elsif($age>60*60*24*(365/12)*2) { 985$age_str=int$age/60/60/24/(365/12); 986$age_str.=" months ago"; 987}elsif($age>60*60*24*7*2) { 988$age_str=int$age/60/60/24/7; 989$age_str.=" weeks ago"; 990}elsif($age>60*60*24*2) { 991$age_str=int$age/60/60/24; 992$age_str.=" days ago"; 993}elsif($age>60*60*2) { 994$age_str=int$age/60/60; 995$age_str.=" hours ago"; 996}elsif($age>60*2) { 997$age_str=int$age/60; 998$age_str.=" min ago"; 999}elsif($age>2) {1000$age_str=int$age;1001$age_str.=" sec ago";1002}else{1003$age_str.=" right now";1004}1005return$age_str;1006}10071008useconstant{1009 S_IFINVALID =>0030000,1010 S_IFGITLINK =>0160000,1011};10121013# submodule/subproject, a commit object reference1014sub S_ISGITLINK($) {1015my$mode=shift;10161017return(($mode& S_IFMT) == S_IFGITLINK)1018}10191020# convert file mode in octal to symbolic file mode string1021sub mode_str {1022my$mode=oct shift;10231024if(S_ISGITLINK($mode)) {1025return'm---------';1026}elsif(S_ISDIR($mode& S_IFMT)) {1027return'drwxr-xr-x';1028}elsif(S_ISLNK($mode)) {1029return'lrwxrwxrwx';1030}elsif(S_ISREG($mode)) {1031# git cares only about the executable bit1032if($mode& S_IXUSR) {1033return'-rwxr-xr-x';1034}else{1035return'-rw-r--r--';1036};1037}else{1038return'----------';1039}1040}10411042# convert file mode in octal to file type string1043sub file_type {1044my$mode=shift;10451046if($mode!~m/^[0-7]+$/) {1047return$mode;1048}else{1049$mode=oct$mode;1050}10511052if(S_ISGITLINK($mode)) {1053return"submodule";1054}elsif(S_ISDIR($mode& S_IFMT)) {1055return"directory";1056}elsif(S_ISLNK($mode)) {1057return"symlink";1058}elsif(S_ISREG($mode)) {1059return"file";1060}else{1061return"unknown";1062}1063}10641065# convert file mode in octal to file type description string1066sub file_type_long {1067my$mode=shift;10681069if($mode!~m/^[0-7]+$/) {1070return$mode;1071}else{1072$mode=oct$mode;1073}10741075if(S_ISGITLINK($mode)) {1076return"submodule";1077}elsif(S_ISDIR($mode& S_IFMT)) {1078return"directory";1079}elsif(S_ISLNK($mode)) {1080return"symlink";1081}elsif(S_ISREG($mode)) {1082if($mode& S_IXUSR) {1083return"executable";1084}else{1085return"file";1086};1087}else{1088return"unknown";1089}1090}109110921093## ----------------------------------------------------------------------1094## functions returning short HTML fragments, or transforming HTML fragments1095## which don't belong to other sections10961097# format line of commit message.1098sub format_log_line_html {1099my$line=shift;11001101$line= esc_html($line, -nbsp=>1);1102if($line=~m/([0-9a-fA-F]{8,40})/) {1103my$hash_text=$1;1104my$link=1105$cgi->a({-href => href(action=>"object", hash=>$hash_text),1106-class=>"text"},$hash_text);1107$line=~s/$hash_text/$link/;1108}1109return$line;1110}11111112# format marker of refs pointing to given object11131114# the destination action is chosen based on object type and current context:1115# - for annotated tags, we choose the tag view unless it's the current view1116# already, in which case we go to shortlog view1117# - for other refs, we keep the current view if we're in history, shortlog or1118# log view, and select shortlog otherwise1119sub format_ref_marker {1120my($refs,$id) =@_;1121my$markers='';11221123if(defined$refs->{$id}) {1124foreachmy$ref(@{$refs->{$id}}) {1125# this code exploits the fact that non-lightweight tags are the1126# only indirect objects, and that they are the only objects for which1127# we want to use tag instead of shortlog as action1128my($type,$name) =qw();1129my$indirect= ($ref=~s/\^\{\}$//);1130# e.g. tags/v2.6.11 or heads/next1131if($ref=~m!^(.*?)s?/(.*)$!) {1132$type=$1;1133$name=$2;1134}else{1135$type="ref";1136$name=$ref;1137}11381139my$class=$type;1140$class.=" indirect"if$indirect;11411142my$dest_action="shortlog";11431144if($indirect) {1145$dest_action="tag"unless$actioneq"tag";1146}elsif($action=~/^(history|(short)?log)$/) {1147$dest_action=$action;1148}11491150my$dest="";1151$dest.="refs/"unless$ref=~ m!^refs/!;1152$dest.=$ref;11531154my$link=$cgi->a({1155-href => href(1156 action=>$dest_action,1157 hash=>$dest1158)},$name);11591160$markers.=" <span class=\"$class\"title=\"$ref\">".1161$link."</span>";1162}1163}11641165if($markers) {1166return' <span class="refs">'.$markers.'</span>';1167}else{1168return"";1169}1170}11711172# format, perhaps shortened and with markers, title line1173sub format_subject_html {1174my($long,$short,$href,$extra) =@_;1175$extra=''unlessdefined($extra);11761177if(length($short) <length($long)) {1178return$cgi->a({-href =>$href, -class=>"list subject",1179-title => to_utf8($long)},1180 esc_html($short) .$extra);1181}else{1182return$cgi->a({-href =>$href, -class=>"list subject"},1183 esc_html($long) .$extra);1184}1185}11861187# format git diff header line, i.e. "diff --(git|combined|cc) ..."1188sub format_git_diff_header_line {1189my$line=shift;1190my$diffinfo=shift;1191my($from,$to) =@_;11921193if($diffinfo->{'nparents'}) {1194# combined diff1195$line=~s!^(diff (.*?) )"?.*$!$1!;1196if($to->{'href'}) {1197$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1198 esc_path($to->{'file'}));1199}else{# file was deleted (no href)1200$line.= esc_path($to->{'file'});1201}1202}else{1203# "ordinary" diff1204$line=~s!^(diff (.*?) )"?a/.*$!$1!;1205if($from->{'href'}) {1206$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1207'a/'. esc_path($from->{'file'}));1208}else{# file was added (no href)1209$line.='a/'. esc_path($from->{'file'});1210}1211$line.=' ';1212if($to->{'href'}) {1213$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1214'b/'. esc_path($to->{'file'}));1215}else{# file was deleted1216$line.='b/'. esc_path($to->{'file'});1217}1218}12191220return"<div class=\"diff header\">$line</div>\n";1221}12221223# format extended diff header line, before patch itself1224sub format_extended_diff_header_line {1225my$line=shift;1226my$diffinfo=shift;1227my($from,$to) =@_;12281229# match <path>1230if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1231$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1232 esc_path($from->{'file'}));1233}1234if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1235$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1236 esc_path($to->{'file'}));1237}1238# match single <mode>1239if($line=~m/\s(\d{6})$/) {1240$line.='<span class="info"> ('.1241 file_type_long($1) .1242')</span>';1243}1244# match <hash>1245if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1246# can match only for combined diff1247$line='index ';1248for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1249if($from->{'href'}[$i]) {1250$line.=$cgi->a({-href=>$from->{'href'}[$i],1251-class=>"hash"},1252substr($diffinfo->{'from_id'}[$i],0,7));1253}else{1254$line.='0' x 7;1255}1256# separator1257$line.=','if($i<$diffinfo->{'nparents'} -1);1258}1259$line.='..';1260if($to->{'href'}) {1261$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1262substr($diffinfo->{'to_id'},0,7));1263}else{1264$line.='0' x 7;1265}12661267}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1268# can match only for ordinary diff1269my($from_link,$to_link);1270if($from->{'href'}) {1271$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1272substr($diffinfo->{'from_id'},0,7));1273}else{1274$from_link='0' x 7;1275}1276if($to->{'href'}) {1277$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1278substr($diffinfo->{'to_id'},0,7));1279}else{1280$to_link='0' x 7;1281}1282my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1283$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1284}12851286return$line."<br/>\n";1287}12881289# format from-file/to-file diff header1290sub format_diff_from_to_header {1291my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1292my$line;1293my$result='';12941295$line=$from_line;1296#assert($line =~ m/^---/) if DEBUG;1297# no extra formatting for "^--- /dev/null"1298if(!$diffinfo->{'nparents'}) {1299# ordinary (single parent) diff1300if($line=~m!^--- "?a/!) {1301if($from->{'href'}) {1302$line='--- a/'.1303$cgi->a({-href=>$from->{'href'}, -class=>"path"},1304 esc_path($from->{'file'}));1305}else{1306$line='--- a/'.1307 esc_path($from->{'file'});1308}1309}1310$result.= qq!<div class="diff from_file">$line</div>\n!;13111312}else{1313# combined diff (merge commit)1314for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1315if($from->{'href'}[$i]) {1316$line='--- '.1317$cgi->a({-href=>href(action=>"blobdiff",1318 hash_parent=>$diffinfo->{'from_id'}[$i],1319 hash_parent_base=>$parents[$i],1320 file_parent=>$from->{'file'}[$i],1321 hash=>$diffinfo->{'to_id'},1322 hash_base=>$hash,1323 file_name=>$to->{'file'}),1324-class=>"path",1325-title=>"diff". ($i+1)},1326$i+1) .1327'/'.1328$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1329 esc_path($from->{'file'}[$i]));1330}else{1331$line='--- /dev/null';1332}1333$result.= qq!<div class="diff from_file">$line</div>\n!;1334}1335}13361337$line=$to_line;1338#assert($line =~ m/^\+\+\+/) if DEBUG;1339# no extra formatting for "^+++ /dev/null"1340if($line=~m!^\+\+\+ "?b/!) {1341if($to->{'href'}) {1342$line='+++ b/'.1343$cgi->a({-href=>$to->{'href'}, -class=>"path"},1344 esc_path($to->{'file'}));1345}else{1346$line='+++ b/'.1347 esc_path($to->{'file'});1348}1349}1350$result.= qq!<div class="diff to_file">$line</div>\n!;13511352return$result;1353}13541355# create note for patch simplified by combined diff1356sub format_diff_cc_simplified {1357my($diffinfo,@parents) =@_;1358my$result='';13591360$result.="<div class=\"diff header\">".1361"diff --cc ";1362if(!is_deleted($diffinfo)) {1363$result.=$cgi->a({-href => href(action=>"blob",1364 hash_base=>$hash,1365 hash=>$diffinfo->{'to_id'},1366 file_name=>$diffinfo->{'to_file'}),1367-class=>"path"},1368 esc_path($diffinfo->{'to_file'}));1369}else{1370$result.= esc_path($diffinfo->{'to_file'});1371}1372$result.="</div>\n".# class="diff header"1373"<div class=\"diff nodifferences\">".1374"Simple merge".1375"</div>\n";# class="diff nodifferences"13761377return$result;1378}13791380# format patch (diff) line (not to be used for diff headers)1381sub format_diff_line {1382my$line=shift;1383my($from,$to) =@_;1384my$diff_class="";13851386chomp$line;13871388if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1389# combined diff1390my$prefix=substr($line,0,scalar@{$from->{'href'}});1391if($line=~m/^\@{3}/) {1392$diff_class=" chunk_header";1393}elsif($line=~m/^\\/) {1394$diff_class=" incomplete";1395}elsif($prefix=~tr/+/+/) {1396$diff_class=" add";1397}elsif($prefix=~tr/-/-/) {1398$diff_class=" rem";1399}1400}else{1401# assume ordinary diff1402my$char=substr($line,0,1);1403if($chareq'+') {1404$diff_class=" add";1405}elsif($chareq'-') {1406$diff_class=" rem";1407}elsif($chareq'@') {1408$diff_class=" chunk_header";1409}elsif($chareq"\\") {1410$diff_class=" incomplete";1411}1412}1413$line= untabify($line);1414if($from&&$to&&$line=~m/^\@{2} /) {1415my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1416$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;14171418$from_lines=0unlessdefined$from_lines;1419$to_lines=0unlessdefined$to_lines;14201421if($from->{'href'}) {1422$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1423-class=>"list"},$from_text);1424}1425if($to->{'href'}) {1426$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1427-class=>"list"},$to_text);1428}1429$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1430"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1431return"<div class=\"diff$diff_class\">$line</div>\n";1432}elsif($from&&$to&&$line=~m/^\@{3}/) {1433my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1434my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);14351436@from_text=split(' ',$ranges);1437for(my$i=0;$i<@from_text; ++$i) {1438($from_start[$i],$from_nlines[$i]) =1439(split(',',substr($from_text[$i],1)),0);1440}14411442$to_text=pop@from_text;1443$to_start=pop@from_start;1444$to_nlines=pop@from_nlines;14451446$line="<span class=\"chunk_info\">$prefix";1447for(my$i=0;$i<@from_text; ++$i) {1448if($from->{'href'}[$i]) {1449$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1450-class=>"list"},$from_text[$i]);1451}else{1452$line.=$from_text[$i];1453}1454$line.=" ";1455}1456if($to->{'href'}) {1457$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1458-class=>"list"},$to_text);1459}else{1460$line.=$to_text;1461}1462$line.="$prefix</span>".1463"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1464return"<div class=\"diff$diff_class\">$line</div>\n";1465}1466return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1467}14681469# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1470# linked. Pass the hash of the tree/commit to snapshot.1471sub format_snapshot_links {1472my($hash) =@_;1473my@snapshot_fmts= gitweb_check_feature('snapshot');1474@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);1475my$num_fmts=@snapshot_fmts;1476if($num_fmts>1) {1477# A parenthesized list of links bearing format names.1478# e.g. "snapshot (_tar.gz_ _zip_)"1479return"snapshot (".join(' ',map1480$cgi->a({1481-href => href(1482 action=>"snapshot",1483 hash=>$hash,1484 snapshot_format=>$_1485)1486},$known_snapshot_formats{$_}{'display'})1487,@snapshot_fmts) .")";1488}elsif($num_fmts==1) {1489# A single "snapshot" link whose tooltip bears the format name.1490# i.e. "_snapshot_"1491my($fmt) =@snapshot_fmts;1492return1493$cgi->a({1494-href => href(1495 action=>"snapshot",1496 hash=>$hash,1497 snapshot_format=>$fmt1498),1499-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1500},"snapshot");1501}else{# $num_fmts == 01502returnundef;1503}1504}15051506## ......................................................................1507## functions returning values to be passed, perhaps after some1508## transformation, to other functions; e.g. returning arguments to href()15091510# returns hash to be passed to href to generate gitweb URL1511# in -title key it returns description of link1512sub get_feed_info {1513my$format=shift||'Atom';1514my%res= (action =>lc($format));15151516# feed links are possible only for project views1517return unless(defined$project);1518# some views should link to OPML, or to generic project feed,1519# or don't have specific feed yet (so they should use generic)1520return if($action=~/^(?:tags|heads|forks|tag|search)$/x);15211522my$branch;1523# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1524# from tag links; this also makes possible to detect branch links1525if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1526(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1527$branch=$1;1528}1529# find log type for feed description (title)1530my$type='log';1531if(defined$file_name) {1532$type="history of$file_name";1533$type.="/"if($actioneq'tree');1534$type.=" on '$branch'"if(defined$branch);1535}else{1536$type="log of$branch"if(defined$branch);1537}15381539$res{-title} =$type;1540$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1541$res{'file_name'} =$file_name;15421543return%res;1544}15451546## ----------------------------------------------------------------------1547## git utility subroutines, invoking git commands15481549# returns path to the core git executable and the --git-dir parameter as list1550sub git_cmd {1551return$GIT,'--git-dir='.$git_dir;1552}15531554# quote the given arguments for passing them to the shell1555# quote_command("command", "arg 1", "arg with ' and ! characters")1556# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1557# Try to avoid using this function wherever possible.1558sub quote_command {1559returnjoin(' ',1560map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1561}15621563# get HEAD ref of given project as hash1564sub git_get_head_hash {1565my$project=shift;1566my$o_git_dir=$git_dir;1567my$retval=undef;1568$git_dir="$projectroot/$project";1569if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1570my$head= <$fd>;1571close$fd;1572if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1573$retval=$1;1574}1575}1576if(defined$o_git_dir) {1577$git_dir=$o_git_dir;1578}1579return$retval;1580}15811582# get type of given object1583sub git_get_type {1584my$hash=shift;15851586open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1587my$type= <$fd>;1588close$fdorreturn;1589chomp$type;1590return$type;1591}15921593# repository configuration1594our$config_file='';1595our%config;15961597# store multiple values for single key as anonymous array reference1598# single values stored directly in the hash, not as [ <value> ]1599sub hash_set_multi {1600my($hash,$key,$value) =@_;16011602if(!exists$hash->{$key}) {1603$hash->{$key} =$value;1604}elsif(!ref$hash->{$key}) {1605$hash->{$key} = [$hash->{$key},$value];1606}else{1607push@{$hash->{$key}},$value;1608}1609}16101611# return hash of git project configuration1612# optionally limited to some section, e.g. 'gitweb'1613sub git_parse_project_config {1614my$section_regexp=shift;1615my%config;16161617local$/="\0";16181619open my$fh,"-|", git_cmd(),"config",'-z','-l',1620orreturn;16211622while(my$keyval= <$fh>) {1623chomp$keyval;1624my($key,$value) =split(/\n/,$keyval,2);16251626 hash_set_multi(\%config,$key,$value)1627if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1628}1629close$fh;16301631return%config;1632}16331634# convert config value to boolean, 'true' or 'false'1635# no value, number > 0, 'true' and 'yes' values are true1636# rest of values are treated as false (never as error)1637sub config_to_bool {1638my$val=shift;16391640# strip leading and trailing whitespace1641$val=~s/^\s+//;1642$val=~s/\s+$//;16431644return(!defined$val||# section.key1645($val=~/^\d+$/&&$val) ||# section.key = 11646($val=~/^(?:true|yes)$/i));# section.key = true1647}16481649# convert config value to simple decimal number1650# an optional value suffix of 'k', 'm', or 'g' will cause the value1651# to be multiplied by 1024, 1048576, or 10737418241652sub config_to_int {1653my$val=shift;16541655# strip leading and trailing whitespace1656$val=~s/^\s+//;1657$val=~s/\s+$//;16581659if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1660$unit=lc($unit);1661# unknown unit is treated as 11662return$num* ($uniteq'g'?1073741824:1663$uniteq'm'?1048576:1664$uniteq'k'?1024:1);1665}1666return$val;1667}16681669# convert config value to array reference, if needed1670sub config_to_multi {1671my$val=shift;16721673returnref($val) ?$val: (defined($val) ? [$val] : []);1674}16751676sub git_get_project_config {1677my($key,$type) =@_;16781679# key sanity check1680return unless($key);1681$key=~s/^gitweb\.//;1682return if($key=~m/\W/);16831684# type sanity check1685if(defined$type) {1686$type=~s/^--//;1687$type=undef1688unless($typeeq'bool'||$typeeq'int');1689}16901691# get config1692if(!defined$config_file||1693$config_filene"$git_dir/config") {1694%config= git_parse_project_config('gitweb');1695$config_file="$git_dir/config";1696}16971698# ensure given type1699if(!defined$type) {1700return$config{"gitweb.$key"};1701}elsif($typeeq'bool') {1702# backward compatibility: 'git config --bool' returns true/false1703return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1704}elsif($typeeq'int') {1705return config_to_int($config{"gitweb.$key"});1706}1707return$config{"gitweb.$key"};1708}17091710# get hash of given path at given ref1711sub git_get_hash_by_path {1712my$base=shift;1713my$path=shift||returnundef;1714my$type=shift;17151716$path=~ s,/+$,,;17171718open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path1719or die_error(500,"Open git-ls-tree failed");1720my$line= <$fd>;1721close$fdorreturnundef;17221723if(!defined$line) {1724# there is no tree or hash given by $path at $base1725returnundef;1726}17271728#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'1729$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;1730if(defined$type&&$typene$2) {1731# type doesn't match1732returnundef;1733}1734return$3;1735}17361737# get path of entry with given hash at given tree-ish (ref)1738# used to get 'from' filename for combined diff (merge commit) for renames1739sub git_get_path_by_hash {1740my$base=shift||return;1741my$hash=shift||return;17421743local$/="\0";17441745open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base1746orreturnundef;1747while(my$line= <$fd>) {1748chomp$line;17491750#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'1751#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'1752if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {1753close$fd;1754return$1;1755}1756}1757close$fd;1758returnundef;1759}17601761## ......................................................................1762## git utility functions, directly accessing git repository17631764sub git_get_project_description {1765my$path=shift;17661767$git_dir="$projectroot/$path";1768open my$fd,"$git_dir/description"1769orreturn git_get_project_config('description');1770my$descr= <$fd>;1771close$fd;1772if(defined$descr) {1773chomp$descr;1774}1775return$descr;1776}17771778sub git_get_project_url_list {1779my$path=shift;17801781$git_dir="$projectroot/$path";1782open my$fd,"$git_dir/cloneurl"1783orreturnwantarray?1784@{ config_to_multi(git_get_project_config('url')) } :1785 config_to_multi(git_get_project_config('url'));1786my@git_project_url_list=map{chomp;$_} <$fd>;1787close$fd;17881789returnwantarray?@git_project_url_list: \@git_project_url_list;1790}17911792sub git_get_projects_list {1793my($filter) =@_;1794my@list;17951796$filter||='';1797$filter=~s/\.git$//;17981799my($check_forks) = gitweb_check_feature('forks');18001801if(-d $projects_list) {1802# search in directory1803my$dir=$projects_list. ($filter?"/$filter":'');1804# remove the trailing "/"1805$dir=~s!/+$!!;1806my$pfxlen=length("$dir");1807my$pfxdepth= ($dir=~tr!/!!);18081809 File::Find::find({1810 follow_fast =>1,# follow symbolic links1811 follow_skip =>2,# ignore duplicates1812 dangling_symlinks =>0,# ignore dangling symlinks, silently1813 wanted =>sub{1814# skip project-list toplevel, if we get it.1815return if(m!^[/.]$!);1816# only directories can be git repositories1817return unless(-d $_);1818# don't traverse too deep (Find is super slow on os x)1819if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {1820$File::Find::prune =1;1821return;1822}18231824my$subdir=substr($File::Find::name,$pfxlen+1);1825# we check related file in $projectroot1826if($check_forksand$subdir=~ m#/.#) {1827$File::Find::prune =1;1828}elsif(check_export_ok("$projectroot/$filter/$subdir")) {1829push@list, { path => ($filter?"$filter/":'') .$subdir};1830$File::Find::prune =1;1831}1832},1833},"$dir");18341835}elsif(-f $projects_list) {1836# read from file(url-encoded):1837# 'git%2Fgit.git Linus+Torvalds'1838# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'1839# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'1840my%paths;1841open my($fd),$projects_listorreturn;1842 PROJECT:1843while(my$line= <$fd>) {1844chomp$line;1845my($path,$owner) =split' ',$line;1846$path= unescape($path);1847$owner= unescape($owner);1848if(!defined$path) {1849next;1850}1851if($filterne'') {1852# looking for forks;1853my$pfx=substr($path,0,length($filter));1854if($pfxne$filter) {1855next PROJECT;1856}1857my$sfx=substr($path,length($filter));1858if($sfx!~/^\/.*\.git$/) {1859next PROJECT;1860}1861}elsif($check_forks) {1862 PATH:1863foreachmy$filter(keys%paths) {1864# looking for forks;1865my$pfx=substr($path,0,length($filter));1866if($pfxne$filter) {1867next PATH;1868}1869my$sfx=substr($path,length($filter));1870if($sfx!~/^\/.*\.git$/) {1871next PATH;1872}1873# is a fork, don't include it in1874# the list1875next PROJECT;1876}1877}1878if(check_export_ok("$projectroot/$path")) {1879my$pr= {1880 path =>$path,1881 owner => to_utf8($owner),1882};1883push@list,$pr;1884(my$forks_path=$path) =~s/\.git$//;1885$paths{$forks_path}++;1886}1887}1888close$fd;1889}1890return@list;1891}18921893our$gitweb_project_owner=undef;1894sub git_get_project_list_from_file {18951896return if(defined$gitweb_project_owner);18971898$gitweb_project_owner= {};1899# read from file (url-encoded):1900# 'git%2Fgit.git Linus+Torvalds'1901# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'1902# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'1903if(-f $projects_list) {1904open(my$fd,$projects_list);1905while(my$line= <$fd>) {1906chomp$line;1907my($pr,$ow) =split' ',$line;1908$pr= unescape($pr);1909$ow= unescape($ow);1910$gitweb_project_owner->{$pr} = to_utf8($ow);1911}1912close$fd;1913}1914}19151916sub git_get_project_owner {1917my$project=shift;1918my$owner;19191920returnundefunless$project;1921$git_dir="$projectroot/$project";19221923if(!defined$gitweb_project_owner) {1924 git_get_project_list_from_file();1925}19261927if(exists$gitweb_project_owner->{$project}) {1928$owner=$gitweb_project_owner->{$project};1929}1930if(!defined$owner){1931$owner= git_get_project_config('owner');1932}1933if(!defined$owner) {1934$owner= get_file_owner("$git_dir");1935}19361937return$owner;1938}19391940sub git_get_last_activity {1941my($path) =@_;1942my$fd;19431944$git_dir="$projectroot/$path";1945open($fd,"-|", git_cmd(),'for-each-ref',1946'--format=%(committer)',1947'--sort=-committerdate',1948'--count=1',1949'refs/heads')orreturn;1950my$most_recent= <$fd>;1951close$fdorreturn;1952if(defined$most_recent&&1953$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {1954my$timestamp=$1;1955my$age=time-$timestamp;1956return($age, age_string($age));1957}1958return(undef,undef);1959}19601961sub git_get_references {1962my$type=shift||"";1963my%refs;1964# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.111965# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}1966open my$fd,"-|", git_cmd(),"show-ref","--dereference",1967($type? ("--","refs/$type") : ())# use -- <pattern> if $type1968orreturn;19691970while(my$line= <$fd>) {1971chomp$line;1972if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {1973if(defined$refs{$1}) {1974push@{$refs{$1}},$2;1975}else{1976$refs{$1} = [$2];1977}1978}1979}1980close$fdorreturn;1981return \%refs;1982}19831984sub git_get_rev_name_tags {1985my$hash=shift||returnundef;19861987open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash1988orreturn;1989my$name_rev= <$fd>;1990close$fd;19911992if($name_rev=~ m|^$hash tags/(.*)$|) {1993return$1;1994}else{1995# catches also '$hash undefined' output1996returnundef;1997}1998}19992000## ----------------------------------------------------------------------2001## parse to hash functions20022003sub parse_date {2004my$epoch=shift;2005my$tz=shift||"-0000";20062007my%date;2008my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2009my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2010my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2011$date{'hour'} =$hour;2012$date{'minute'} =$min;2013$date{'mday'} =$mday;2014$date{'day'} =$days[$wday];2015$date{'month'} =$months[$mon];2016$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2017$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2018$date{'mday-time'} =sprintf"%d%s%02d:%02d",2019$mday,$months[$mon],$hour,$min;2020$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",20211900+$year,1+$mon,$mday,$hour,$min,$sec;20222023$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2024my$local=$epoch+ ((int$1+ ($2/60)) *3600);2025($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2026$date{'hour_local'} =$hour;2027$date{'minute_local'} =$min;2028$date{'tz_local'} =$tz;2029$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",20301900+$year,$mon+1,$mday,2031$hour,$min,$sec,$tz);2032return%date;2033}20342035sub parse_tag {2036my$tag_id=shift;2037my%tag;2038my@comment;20392040open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2041$tag{'id'} =$tag_id;2042while(my$line= <$fd>) {2043chomp$line;2044if($line=~m/^object ([0-9a-fA-F]{40})$/) {2045$tag{'object'} =$1;2046}elsif($line=~m/^type (.+)$/) {2047$tag{'type'} =$1;2048}elsif($line=~m/^tag (.+)$/) {2049$tag{'name'} =$1;2050}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2051$tag{'author'} =$1;2052$tag{'epoch'} =$2;2053$tag{'tz'} =$3;2054}elsif($line=~m/--BEGIN/) {2055push@comment,$line;2056last;2057}elsif($lineeq"") {2058last;2059}2060}2061push@comment, <$fd>;2062$tag{'comment'} = \@comment;2063close$fdorreturn;2064if(!defined$tag{'name'}) {2065return2066};2067return%tag2068}20692070sub parse_commit_text {2071my($commit_text,$withparents) =@_;2072my@commit_lines=split'\n',$commit_text;2073my%co;20742075pop@commit_lines;# Remove '\0'20762077if(!@commit_lines) {2078return;2079}20802081my$header=shift@commit_lines;2082if($header!~m/^[0-9a-fA-F]{40}/) {2083return;2084}2085($co{'id'},my@parents) =split' ',$header;2086while(my$line=shift@commit_lines) {2087last if$lineeq"\n";2088if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2089$co{'tree'} =$1;2090}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2091push@parents,$1;2092}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2093$co{'author'} =$1;2094$co{'author_epoch'} =$2;2095$co{'author_tz'} =$3;2096if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2097$co{'author_name'} =$1;2098$co{'author_email'} =$2;2099}else{2100$co{'author_name'} =$co{'author'};2101}2102}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2103$co{'committer'} =$1;2104$co{'committer_epoch'} =$2;2105$co{'committer_tz'} =$3;2106$co{'committer_name'} =$co{'committer'};2107if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2108$co{'committer_name'} =$1;2109$co{'committer_email'} =$2;2110}else{2111$co{'committer_name'} =$co{'committer'};2112}2113}2114}2115if(!defined$co{'tree'}) {2116return;2117};2118$co{'parents'} = \@parents;2119$co{'parent'} =$parents[0];21202121foreachmy$title(@commit_lines) {2122$title=~s/^ //;2123if($titlene"") {2124$co{'title'} = chop_str($title,80,5);2125# remove leading stuff of merges to make the interesting part visible2126if(length($title) >50) {2127$title=~s/^Automatic //;2128$title=~s/^merge (of|with) /Merge ... /i;2129if(length($title) >50) {2130$title=~s/(http|rsync):\/\///;2131}2132if(length($title) >50) {2133$title=~s/(master|www|rsync)\.//;2134}2135if(length($title) >50) {2136$title=~s/kernel.org:?//;2137}2138if(length($title) >50) {2139$title=~s/\/pub\/scm//;2140}2141}2142$co{'title_short'} = chop_str($title,50,5);2143last;2144}2145}2146if(!defined$co{'title'} ||$co{'title'}eq"") {2147$co{'title'} =$co{'title_short'} ='(no commit message)';2148}2149# remove added spaces2150foreachmy$line(@commit_lines) {2151$line=~s/^ //;2152}2153$co{'comment'} = \@commit_lines;21542155my$age=time-$co{'committer_epoch'};2156$co{'age'} =$age;2157$co{'age_string'} = age_string($age);2158my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2159if($age>60*60*24*7*2) {2160$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2161$co{'age_string_age'} =$co{'age_string'};2162}else{2163$co{'age_string_date'} =$co{'age_string'};2164$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2165}2166return%co;2167}21682169sub parse_commit {2170my($commit_id) =@_;2171my%co;21722173local$/="\0";21742175open my$fd,"-|", git_cmd(),"rev-list",2176"--parents",2177"--header",2178"--max-count=1",2179$commit_id,2180"--",2181or die_error(500,"Open git-rev-list failed");2182%co= parse_commit_text(<$fd>,1);2183close$fd;21842185return%co;2186}21872188sub parse_commits {2189my($commit_id,$maxcount,$skip,$filename,@args) =@_;2190my@cos;21912192$maxcount||=1;2193$skip||=0;21942195local$/="\0";21962197open my$fd,"-|", git_cmd(),"rev-list",2198"--header",2199@args,2200("--max-count=".$maxcount),2201("--skip=".$skip),2202@extra_options,2203$commit_id,2204"--",2205($filename? ($filename) : ())2206or die_error(500,"Open git-rev-list failed");2207while(my$line= <$fd>) {2208my%co= parse_commit_text($line);2209push@cos, \%co;2210}2211close$fd;22122213returnwantarray?@cos: \@cos;2214}22152216# parse line of git-diff-tree "raw" output2217sub parse_difftree_raw_line {2218my$line=shift;2219my%res;22202221# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2222# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2223if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2224$res{'from_mode'} =$1;2225$res{'to_mode'} =$2;2226$res{'from_id'} =$3;2227$res{'to_id'} =$4;2228$res{'status'} =$5;2229$res{'similarity'} =$6;2230if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2231($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2232}else{2233$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2234}2235}2236# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2237# combined diff (for merge commit)2238elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2239$res{'nparents'} =length($1);2240$res{'from_mode'} = [split(' ',$2) ];2241$res{'to_mode'} =pop@{$res{'from_mode'}};2242$res{'from_id'} = [split(' ',$3) ];2243$res{'to_id'} =pop@{$res{'from_id'}};2244$res{'status'} = [split('',$4) ];2245$res{'to_file'} = unquote($5);2246}2247# 'c512b523472485aef4fff9e57b229d9d243c967f'2248elsif($line=~m/^([0-9a-fA-F]{40})$/) {2249$res{'commit'} =$1;2250}22512252returnwantarray?%res: \%res;2253}22542255# wrapper: return parsed line of git-diff-tree "raw" output2256# (the argument might be raw line, or parsed info)2257sub parsed_difftree_line {2258my$line_or_ref=shift;22592260if(ref($line_or_ref)eq"HASH") {2261# pre-parsed (or generated by hand)2262return$line_or_ref;2263}else{2264return parse_difftree_raw_line($line_or_ref);2265}2266}22672268# parse line of git-ls-tree output2269sub parse_ls_tree_line ($;%) {2270my$line=shift;2271my%opts=@_;2272my%res;22732274#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2275$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;22762277$res{'mode'} =$1;2278$res{'type'} =$2;2279$res{'hash'} =$3;2280if($opts{'-z'}) {2281$res{'name'} =$4;2282}else{2283$res{'name'} = unquote($4);2284}22852286returnwantarray?%res: \%res;2287}22882289# generates _two_ hashes, references to which are passed as 2 and 3 argument2290sub parse_from_to_diffinfo {2291my($diffinfo,$from,$to,@parents) =@_;22922293if($diffinfo->{'nparents'}) {2294# combined diff2295$from->{'file'} = [];2296$from->{'href'} = [];2297 fill_from_file_info($diffinfo,@parents)2298unlessexists$diffinfo->{'from_file'};2299for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2300$from->{'file'}[$i] =2301defined$diffinfo->{'from_file'}[$i] ?2302$diffinfo->{'from_file'}[$i] :2303$diffinfo->{'to_file'};2304if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2305$from->{'href'}[$i] = href(action=>"blob",2306 hash_base=>$parents[$i],2307 hash=>$diffinfo->{'from_id'}[$i],2308 file_name=>$from->{'file'}[$i]);2309}else{2310$from->{'href'}[$i] =undef;2311}2312}2313}else{2314# ordinary (not combined) diff2315$from->{'file'} =$diffinfo->{'from_file'};2316if($diffinfo->{'status'}ne"A") {# not new (added) file2317$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2318 hash=>$diffinfo->{'from_id'},2319 file_name=>$from->{'file'});2320}else{2321delete$from->{'href'};2322}2323}23242325$to->{'file'} =$diffinfo->{'to_file'};2326if(!is_deleted($diffinfo)) {# file exists in result2327$to->{'href'} = href(action=>"blob", hash_base=>$hash,2328 hash=>$diffinfo->{'to_id'},2329 file_name=>$to->{'file'});2330}else{2331delete$to->{'href'};2332}2333}23342335## ......................................................................2336## parse to array of hashes functions23372338sub git_get_heads_list {2339my$limit=shift;2340my@headslist;23412342open my$fd,'-|', git_cmd(),'for-each-ref',2343($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2344'--format=%(objectname) %(refname) %(subject)%00%(committer)',2345'refs/heads'2346orreturn;2347while(my$line= <$fd>) {2348my%ref_item;23492350chomp$line;2351my($refinfo,$committerinfo) =split(/\0/,$line);2352my($hash,$name,$title) =split(' ',$refinfo,3);2353my($committer,$epoch,$tz) =2354($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2355$ref_item{'fullname'} =$name;2356$name=~s!^refs/heads/!!;23572358$ref_item{'name'} =$name;2359$ref_item{'id'} =$hash;2360$ref_item{'title'} =$title||'(no commit message)';2361$ref_item{'epoch'} =$epoch;2362if($epoch) {2363$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2364}else{2365$ref_item{'age'} ="unknown";2366}23672368push@headslist, \%ref_item;2369}2370close$fd;23712372returnwantarray?@headslist: \@headslist;2373}23742375sub git_get_tags_list {2376my$limit=shift;2377my@tagslist;23782379open my$fd,'-|', git_cmd(),'for-each-ref',2380($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2381'--format=%(objectname) %(objecttype) %(refname) '.2382'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2383'refs/tags'2384orreturn;2385while(my$line= <$fd>) {2386my%ref_item;23872388chomp$line;2389my($refinfo,$creatorinfo) =split(/\0/,$line);2390my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2391my($creator,$epoch,$tz) =2392($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2393$ref_item{'fullname'} =$name;2394$name=~s!^refs/tags/!!;23952396$ref_item{'type'} =$type;2397$ref_item{'id'} =$id;2398$ref_item{'name'} =$name;2399if($typeeq"tag") {2400$ref_item{'subject'} =$title;2401$ref_item{'reftype'} =$reftype;2402$ref_item{'refid'} =$refid;2403}else{2404$ref_item{'reftype'} =$type;2405$ref_item{'refid'} =$id;2406}24072408if($typeeq"tag"||$typeeq"commit") {2409$ref_item{'epoch'} =$epoch;2410if($epoch) {2411$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2412}else{2413$ref_item{'age'} ="unknown";2414}2415}24162417push@tagslist, \%ref_item;2418}2419close$fd;24202421returnwantarray?@tagslist: \@tagslist;2422}24232424## ----------------------------------------------------------------------2425## filesystem-related functions24262427sub get_file_owner {2428my$path=shift;24292430my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2431my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2432if(!defined$gcos) {2433returnundef;2434}2435my$owner=$gcos;2436$owner=~s/[,;].*$//;2437return to_utf8($owner);2438}24392440## ......................................................................2441## mimetype related functions24422443sub mimetype_guess_file {2444my$filename=shift;2445my$mimemap=shift;2446-r $mimemaporreturnundef;24472448my%mimemap;2449open(MIME,$mimemap)orreturnundef;2450while(<MIME>) {2451next ifm/^#/;# skip comments2452my($mime,$exts) =split(/\t+/);2453if(defined$exts) {2454my@exts=split(/\s+/,$exts);2455foreachmy$ext(@exts) {2456$mimemap{$ext} =$mime;2457}2458}2459}2460close(MIME);24612462$filename=~/\.([^.]*)$/;2463return$mimemap{$1};2464}24652466sub mimetype_guess {2467my$filename=shift;2468my$mime;2469$filename=~/\./orreturnundef;24702471if($mimetypes_file) {2472my$file=$mimetypes_file;2473if($file!~m!^/!) {# if it is relative path2474# it is relative to project2475$file="$projectroot/$project/$file";2476}2477$mime= mimetype_guess_file($filename,$file);2478}2479$mime||= mimetype_guess_file($filename,'/etc/mime.types');2480return$mime;2481}24822483sub blob_mimetype {2484my$fd=shift;2485my$filename=shift;24862487if($filename) {2488my$mime= mimetype_guess($filename);2489$mimeandreturn$mime;2490}24912492# just in case2493return$default_blob_plain_mimetypeunless$fd;24942495if(-T $fd) {2496return'text/plain';2497}elsif(!$filename) {2498return'application/octet-stream';2499}elsif($filename=~m/\.png$/i) {2500return'image/png';2501}elsif($filename=~m/\.gif$/i) {2502return'image/gif';2503}elsif($filename=~m/\.jpe?g$/i) {2504return'image/jpeg';2505}else{2506return'application/octet-stream';2507}2508}25092510sub blob_contenttype {2511my($fd,$file_name,$type) =@_;25122513$type||= blob_mimetype($fd,$file_name);2514if($typeeq'text/plain'&&defined$default_text_plain_charset) {2515$type.="; charset=$default_text_plain_charset";2516}25172518return$type;2519}25202521## ======================================================================2522## functions printing HTML: header, footer, error page25232524sub git_header_html {2525my$status=shift||"200 OK";2526my$expires=shift;25272528my$title="$site_name";2529if(defined$project) {2530$title.=" - ". to_utf8($project);2531if(defined$action) {2532$title.="/$action";2533if(defined$file_name) {2534$title.=" - ". esc_path($file_name);2535if($actioneq"tree"&&$file_name!~ m|/$|) {2536$title.="/";2537}2538}2539}2540}2541my$content_type;2542# require explicit support from the UA if we are to send the page as2543# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2544# we have to do this because MSIE sometimes globs '*/*', pretending to2545# support xhtml+xml but choking when it gets what it asked for.2546if(defined$cgi->http('HTTP_ACCEPT') &&2547$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2548$cgi->Accept('application/xhtml+xml') !=0) {2549$content_type='application/xhtml+xml';2550}else{2551$content_type='text/html';2552}2553print$cgi->header(-type=>$content_type, -charset =>'utf-8',2554-status=>$status, -expires =>$expires);2555my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2556print<<EOF;2557<?xml version="1.0" encoding="utf-8"?>2558<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2559<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2560<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2561<!-- git core binaries version$git_version-->2562<head>2563<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2564<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2565<meta name="robots" content="index, nofollow"/>2566<title>$title</title>2567EOF2568# print out each stylesheet that exist2569if(defined$stylesheet) {2570#provides backwards capability for those people who define style sheet in a config file2571print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2572}else{2573foreachmy$stylesheet(@stylesheets) {2574next unless$stylesheet;2575print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2576}2577}2578if(defined$project) {2579my%href_params= get_feed_info();2580if(!exists$href_params{'-title'}) {2581$href_params{'-title'} ='log';2582}25832584foreachmy$formatqw(RSS Atom){2585my$type=lc($format);2586my%link_attr= (2587'-rel'=>'alternate',2588'-title'=>"$project-$href_params{'-title'} -$formatfeed",2589'-type'=>"application/$type+xml"2590);25912592$href_params{'action'} =$type;2593$link_attr{'-href'} = href(%href_params);2594print"<link ".2595"rel=\"$link_attr{'-rel'}\"".2596"title=\"$link_attr{'-title'}\"".2597"href=\"$link_attr{'-href'}\"".2598"type=\"$link_attr{'-type'}\"".2599"/>\n";26002601$href_params{'extra_options'} ='--no-merges';2602$link_attr{'-href'} = href(%href_params);2603$link_attr{'-title'} .=' (no merges)';2604print"<link ".2605"rel=\"$link_attr{'-rel'}\"".2606"title=\"$link_attr{'-title'}\"".2607"href=\"$link_attr{'-href'}\"".2608"type=\"$link_attr{'-type'}\"".2609"/>\n";2610}26112612}else{2613printf('<link rel="alternate" title="%sprojects list" '.2614'href="%s" type="text/plain; charset=utf-8" />'."\n",2615$site_name, href(project=>undef, action=>"project_index"));2616printf('<link rel="alternate" title="%sprojects feeds" '.2617'href="%s" type="text/x-opml" />'."\n",2618$site_name, href(project=>undef, action=>"opml"));2619}2620if(defined$favicon) {2621printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2622}26232624print"</head>\n".2625"<body>\n";26262627if(-f $site_header) {2628open(my$fd,$site_header);2629print<$fd>;2630close$fd;2631}26322633print"<div class=\"page_header\">\n".2634$cgi->a({-href => esc_url($logo_url),2635-title =>$logo_label},2636qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2637print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2638if(defined$project) {2639print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2640if(defined$action) {2641print" /$action";2642}2643print"\n";2644}2645print"</div>\n";26462647my($have_search) = gitweb_check_feature('search');2648if(defined$project&&$have_search) {2649if(!defined$searchtext) {2650$searchtext="";2651}2652my$search_hash;2653if(defined$hash_base) {2654$search_hash=$hash_base;2655}elsif(defined$hash) {2656$search_hash=$hash;2657}else{2658$search_hash="HEAD";2659}2660my$action=$my_uri;2661my($use_pathinfo) = gitweb_check_feature('pathinfo');2662if($use_pathinfo) {2663$action.="/".esc_url($project);2664}2665print$cgi->startform(-method=>"get", -action =>$action) .2666"<div class=\"search\">\n".2667(!$use_pathinfo&&2668$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .2669$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".2670$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".2671$cgi->popup_menu(-name =>'st', -default=>'commit',2672-values=> ['commit','grep','author','committer','pickaxe']) .2673$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .2674" search:\n",2675$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".2676"<span title=\"Extended regular expression\">".2677$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',2678-checked =>$search_use_regexp) .2679"</span>".2680"</div>".2681$cgi->end_form() ."\n";2682}2683}26842685sub git_footer_html {2686my$feed_class='rss_logo';26872688print"<div class=\"page_footer\">\n";2689if(defined$project) {2690my$descr= git_get_project_description($project);2691if(defined$descr) {2692print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";2693}26942695my%href_params= get_feed_info();2696if(!%href_params) {2697$feed_class.=' generic';2698}2699$href_params{'-title'} ||='log';27002701foreachmy$formatqw(RSS Atom){2702$href_params{'action'} =lc($format);2703print$cgi->a({-href => href(%href_params),2704-title =>"$href_params{'-title'}$formatfeed",2705-class=>$feed_class},$format)."\n";2706}27072708}else{2709print$cgi->a({-href => href(project=>undef, action=>"opml"),2710-class=>$feed_class},"OPML") ." ";2711print$cgi->a({-href => href(project=>undef, action=>"project_index"),2712-class=>$feed_class},"TXT") ."\n";2713}2714print"</div>\n";# class="page_footer"27152716if(-f $site_footer) {2717open(my$fd,$site_footer);2718print<$fd>;2719close$fd;2720}27212722print"</body>\n".2723"</html>";2724}27252726# die_error(<http_status_code>, <error_message>)2727# Example: die_error(404, 'Hash not found')2728# By convention, use the following status codes (as defined in RFC 2616):2729# 400: Invalid or missing CGI parameters, or2730# requested object exists but has wrong type.2731# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on2732# this server or project.2733# 404: Requested object/revision/project doesn't exist.2734# 500: The server isn't configured properly, or2735# an internal error occurred (e.g. failed assertions caused by bugs), or2736# an unknown error occurred (e.g. the git binary died unexpectedly).2737sub die_error {2738my$status=shift||500;2739my$error=shift||"Internal server error";27402741my%http_responses= (400=>'400 Bad Request',2742403=>'403 Forbidden',2743404=>'404 Not Found',2744500=>'500 Internal Server Error');2745 git_header_html($http_responses{$status});2746print<<EOF;2747<div class="page_body">2748<br /><br />2749$status-$error2750<br />2751</div>2752EOF2753 git_footer_html();2754exit;2755}27562757## ----------------------------------------------------------------------2758## functions printing or outputting HTML: navigation27592760sub git_print_page_nav {2761my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;2762$extra=''if!defined$extra;# pager or formats27632764my@navs=qw(summary shortlog log commit commitdiff tree);2765if($suppress) {2766@navs=grep{$_ne$suppress}@navs;2767}27682769my%arg=map{$_=> {action=>$_} }@navs;2770if(defined$head) {2771for(qw(commit commitdiff)) {2772$arg{$_}{'hash'} =$head;2773}2774if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {2775for(qw(shortlog log)) {2776$arg{$_}{'hash'} =$head;2777}2778}2779}27802781$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;2782$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;27832784my@actions= gitweb_check_feature('actions');2785while(@actions) {2786my($label,$link,$pos) = (shift(@actions),shift(@actions),shift(@actions));2787@navs=map{$_eq$pos? ($_,$label) :$_}@navs;2788# munch munch2789$link=~ s#%n#$project#g;2790$link=~ s#%f#$git_dir#g;2791$treehead?$link=~ s#%h#$treehead#g : $link =~ s#%h##g;2792$treebase?$link=~ s#%b#$treebase#g : $link =~ s#%b##g;2793$arg{$label}{'_href'} =$link;2794}27952796print"<div class=\"page_nav\">\n".2797(join" | ",2798map{$_eq$current?2799$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")2800}@navs);2801print"<br/>\n$extra<br/>\n".2802"</div>\n";2803}28042805sub format_paging_nav {2806my($action,$hash,$head,$page,$has_next_link) =@_;2807my$paging_nav;280828092810if($hashne$head||$page) {2811$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");2812}else{2813$paging_nav.="HEAD";2814}28152816if($page>0) {2817$paging_nav.=" ⋅ ".2818$cgi->a({-href => href(-replay=>1, page=>$page-1),2819-accesskey =>"p", -title =>"Alt-p"},"prev");2820}else{2821$paging_nav.=" ⋅ prev";2822}28232824if($has_next_link) {2825$paging_nav.=" ⋅ ".2826$cgi->a({-href => href(-replay=>1, page=>$page+1),2827-accesskey =>"n", -title =>"Alt-n"},"next");2828}else{2829$paging_nav.=" ⋅ next";2830}28312832return$paging_nav;2833}28342835## ......................................................................2836## functions printing or outputting HTML: div28372838sub git_print_header_div {2839my($action,$title,$hash,$hash_base) =@_;2840my%args= ();28412842$args{'action'} =$action;2843$args{'hash'} =$hashif$hash;2844$args{'hash_base'} =$hash_baseif$hash_base;28452846print"<div class=\"header\">\n".2847$cgi->a({-href => href(%args), -class=>"title"},2848$title?$title:$action) .2849"\n</div>\n";2850}28512852#sub git_print_authorship (\%) {2853sub git_print_authorship {2854my$co=shift;28552856my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});2857print"<div class=\"author_date\">".2858 esc_html($co->{'author_name'}) .2859" [$ad{'rfc2822'}";2860if($ad{'hour_local'} <6) {2861printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",2862$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});2863}else{2864printf(" (%02d:%02d%s)",2865$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});2866}2867print"]</div>\n";2868}28692870sub git_print_page_path {2871my$name=shift;2872my$type=shift;2873my$hb=shift;287428752876print"<div class=\"page_path\">";2877print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),2878-title =>'tree root'}, to_utf8("[$project]"));2879print" / ";2880if(defined$name) {2881my@dirname=split'/',$name;2882my$basename=pop@dirname;2883my$fullname='';28842885foreachmy$dir(@dirname) {2886$fullname.= ($fullname?'/':'') .$dir;2887print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,2888 hash_base=>$hb),2889-title =>$fullname}, esc_path($dir));2890print" / ";2891}2892if(defined$type&&$typeeq'blob') {2893print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,2894 hash_base=>$hb),2895-title =>$name}, esc_path($basename));2896}elsif(defined$type&&$typeeq'tree') {2897print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,2898 hash_base=>$hb),2899-title =>$name}, esc_path($basename));2900print" / ";2901}else{2902print esc_path($basename);2903}2904}2905print"<br/></div>\n";2906}29072908# sub git_print_log (\@;%) {2909sub git_print_log ($;%) {2910my$log=shift;2911my%opts=@_;29122913if($opts{'-remove_title'}) {2914# remove title, i.e. first line of log2915shift@$log;2916}2917# remove leading empty lines2918while(defined$log->[0] &&$log->[0]eq"") {2919shift@$log;2920}29212922# print log2923my$signoff=0;2924my$empty=0;2925foreachmy$line(@$log) {2926if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {2927$signoff=1;2928$empty=0;2929if(!$opts{'-remove_signoff'}) {2930print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";2931next;2932}else{2933# remove signoff lines2934next;2935}2936}else{2937$signoff=0;2938}29392940# print only one empty line2941# do not print empty line after signoff2942if($lineeq"") {2943next if($empty||$signoff);2944$empty=1;2945}else{2946$empty=0;2947}29482949print format_log_line_html($line) ."<br/>\n";2950}29512952if($opts{'-final_empty_line'}) {2953# end with single empty line2954print"<br/>\n"unless$empty;2955}2956}29572958# return link target (what link points to)2959sub git_get_link_target {2960my$hash=shift;2961my$link_target;29622963# read link2964open my$fd,"-|", git_cmd(),"cat-file","blob",$hash2965orreturn;2966{2967local$/;2968$link_target= <$fd>;2969}2970close$fd2971orreturn;29722973return$link_target;2974}29752976# given link target, and the directory (basedir) the link is in,2977# return target of link relative to top directory (top tree);2978# return undef if it is not possible (including absolute links).2979sub normalize_link_target {2980my($link_target,$basedir,$hash_base) =@_;29812982# we can normalize symlink target only if $hash_base is provided2983return unless$hash_base;29842985# absolute symlinks (beginning with '/') cannot be normalized2986return if(substr($link_target,0,1)eq'/');29872988# normalize link target to path from top (root) tree (dir)2989my$path;2990if($basedir) {2991$path=$basedir.'/'.$link_target;2992}else{2993# we are in top (root) tree (dir)2994$path=$link_target;2995}29962997# remove //, /./, and /../2998my@path_parts;2999foreachmy$part(split('/',$path)) {3000# discard '.' and ''3001next if(!$part||$parteq'.');3002# handle '..'3003if($parteq'..') {3004if(@path_parts) {3005pop@path_parts;3006}else{3007# link leads outside repository (outside top dir)3008return;3009}3010}else{3011push@path_parts,$part;3012}3013}3014$path=join('/',@path_parts);30153016return$path;3017}30183019# print tree entry (row of git_tree), but without encompassing <tr> element3020sub git_print_tree_entry {3021my($t,$basedir,$hash_base,$have_blame) =@_;30223023my%base_key= ();3024$base_key{'hash_base'} =$hash_baseifdefined$hash_base;30253026# The format of a table row is: mode list link. Where mode is3027# the mode of the entry, list is the name of the entry, an href,3028# and link is the action links of the entry.30293030print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3031if($t->{'type'}eq"blob") {3032print"<td class=\"list\">".3033$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3034 file_name=>"$basedir$t->{'name'}",%base_key),3035-class=>"list"}, esc_path($t->{'name'}));3036if(S_ISLNK(oct$t->{'mode'})) {3037my$link_target= git_get_link_target($t->{'hash'});3038if($link_target) {3039my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3040if(defined$norm_target) {3041print" -> ".3042$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3043 file_name=>$norm_target),3044-title =>$norm_target}, esc_path($link_target));3045}else{3046print" -> ". esc_path($link_target);3047}3048}3049}3050print"</td>\n";3051print"<td class=\"link\">";3052print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3053 file_name=>"$basedir$t->{'name'}",%base_key)},3054"blob");3055if($have_blame) {3056print" | ".3057$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3058 file_name=>"$basedir$t->{'name'}",%base_key)},3059"blame");3060}3061if(defined$hash_base) {3062print" | ".3063$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3064 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3065"history");3066}3067print" | ".3068$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3069 file_name=>"$basedir$t->{'name'}")},3070"raw");3071print"</td>\n";30723073}elsif($t->{'type'}eq"tree") {3074print"<td class=\"list\">";3075print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3076 file_name=>"$basedir$t->{'name'}",%base_key)},3077 esc_path($t->{'name'}));3078print"</td>\n";3079print"<td class=\"link\">";3080print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3081 file_name=>"$basedir$t->{'name'}",%base_key)},3082"tree");3083if(defined$hash_base) {3084print" | ".3085$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3086 file_name=>"$basedir$t->{'name'}")},3087"history");3088}3089print"</td>\n";3090}else{3091# unknown object: we can only present history for it3092# (this includes 'commit' object, i.e. submodule support)3093print"<td class=\"list\">".3094 esc_path($t->{'name'}) .3095"</td>\n";3096print"<td class=\"link\">";3097if(defined$hash_base) {3098print$cgi->a({-href => href(action=>"history",3099 hash_base=>$hash_base,3100 file_name=>"$basedir$t->{'name'}")},3101"history");3102}3103print"</td>\n";3104}3105}31063107## ......................................................................3108## functions printing large fragments of HTML31093110# get pre-image filenames for merge (combined) diff3111sub fill_from_file_info {3112my($diff,@parents) =@_;31133114$diff->{'from_file'} = [ ];3115$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3116for(my$i=0;$i<$diff->{'nparents'};$i++) {3117if($diff->{'status'}[$i]eq'R'||3118$diff->{'status'}[$i]eq'C') {3119$diff->{'from_file'}[$i] =3120 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3121}3122}31233124return$diff;3125}31263127# is current raw difftree line of file deletion3128sub is_deleted {3129my$diffinfo=shift;31303131return$diffinfo->{'to_id'}eq('0' x 40);3132}31333134# does patch correspond to [previous] difftree raw line3135# $diffinfo - hashref of parsed raw diff format3136# $patchinfo - hashref of parsed patch diff format3137# (the same keys as in $diffinfo)3138sub is_patch_split {3139my($diffinfo,$patchinfo) =@_;31403141returndefined$diffinfo&&defined$patchinfo3142&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3143}314431453146sub git_difftree_body {3147my($difftree,$hash,@parents) =@_;3148my($parent) =$parents[0];3149my($have_blame) = gitweb_check_feature('blame');3150print"<div class=\"list_head\">\n";3151if($#{$difftree} >10) {3152print(($#{$difftree} +1) ." files changed:\n");3153}3154print"</div>\n";31553156print"<table class=\"".3157(@parents>1?"combined ":"") .3158"diff_tree\">\n";31593160# header only for combined diff in 'commitdiff' view3161my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3162if($has_header) {3163# table header3164print"<thead><tr>\n".3165"<th></th><th></th>\n";# filename, patchN link3166for(my$i=0;$i<@parents;$i++) {3167my$par=$parents[$i];3168print"<th>".3169$cgi->a({-href => href(action=>"commitdiff",3170 hash=>$hash, hash_parent=>$par),3171-title =>'commitdiff to parent number '.3172($i+1) .': '.substr($par,0,7)},3173$i+1) .3174" </th>\n";3175}3176print"</tr></thead>\n<tbody>\n";3177}31783179my$alternate=1;3180my$patchno=0;3181foreachmy$line(@{$difftree}) {3182my$diff= parsed_difftree_line($line);31833184if($alternate) {3185print"<tr class=\"dark\">\n";3186}else{3187print"<tr class=\"light\">\n";3188}3189$alternate^=1;31903191if(exists$diff->{'nparents'}) {# combined diff31923193 fill_from_file_info($diff,@parents)3194unlessexists$diff->{'from_file'};31953196if(!is_deleted($diff)) {3197# file exists in the result (child) commit3198print"<td>".3199$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3200 file_name=>$diff->{'to_file'},3201 hash_base=>$hash),3202-class=>"list"}, esc_path($diff->{'to_file'})) .3203"</td>\n";3204}else{3205print"<td>".3206 esc_path($diff->{'to_file'}) .3207"</td>\n";3208}32093210if($actioneq'commitdiff') {3211# link to patch3212$patchno++;3213print"<td class=\"link\">".3214$cgi->a({-href =>"#patch$patchno"},"patch") .3215" | ".3216"</td>\n";3217}32183219my$has_history=0;3220my$not_deleted=0;3221for(my$i=0;$i<$diff->{'nparents'};$i++) {3222my$hash_parent=$parents[$i];3223my$from_hash=$diff->{'from_id'}[$i];3224my$from_path=$diff->{'from_file'}[$i];3225my$status=$diff->{'status'}[$i];32263227$has_history||= ($statusne'A');3228$not_deleted||= ($statusne'D');32293230if($statuseq'A') {3231print"<td class=\"link\"align=\"right\"> | </td>\n";3232}elsif($statuseq'D') {3233print"<td class=\"link\">".3234$cgi->a({-href => href(action=>"blob",3235 hash_base=>$hash,3236 hash=>$from_hash,3237 file_name=>$from_path)},3238"blob". ($i+1)) .3239" | </td>\n";3240}else{3241if($diff->{'to_id'}eq$from_hash) {3242print"<td class=\"link nochange\">";3243}else{3244print"<td class=\"link\">";3245}3246print$cgi->a({-href => href(action=>"blobdiff",3247 hash=>$diff->{'to_id'},3248 hash_parent=>$from_hash,3249 hash_base=>$hash,3250 hash_parent_base=>$hash_parent,3251 file_name=>$diff->{'to_file'},3252 file_parent=>$from_path)},3253"diff". ($i+1)) .3254" | </td>\n";3255}3256}32573258print"<td class=\"link\">";3259if($not_deleted) {3260print$cgi->a({-href => href(action=>"blob",3261 hash=>$diff->{'to_id'},3262 file_name=>$diff->{'to_file'},3263 hash_base=>$hash)},3264"blob");3265print" | "if($has_history);3266}3267if($has_history) {3268print$cgi->a({-href => href(action=>"history",3269 file_name=>$diff->{'to_file'},3270 hash_base=>$hash)},3271"history");3272}3273print"</td>\n";32743275print"</tr>\n";3276next;# instead of 'else' clause, to avoid extra indent3277}3278# else ordinary diff32793280my($to_mode_oct,$to_mode_str,$to_file_type);3281my($from_mode_oct,$from_mode_str,$from_file_type);3282if($diff->{'to_mode'}ne('0' x 6)) {3283$to_mode_oct=oct$diff->{'to_mode'};3284if(S_ISREG($to_mode_oct)) {# only for regular file3285$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3286}3287$to_file_type= file_type($diff->{'to_mode'});3288}3289if($diff->{'from_mode'}ne('0' x 6)) {3290$from_mode_oct=oct$diff->{'from_mode'};3291if(S_ISREG($to_mode_oct)) {# only for regular file3292$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3293}3294$from_file_type= file_type($diff->{'from_mode'});3295}32963297if($diff->{'status'}eq"A") {# created3298my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3299$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3300$mode_chng.="]</span>";3301print"<td>";3302print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3303 hash_base=>$hash, file_name=>$diff->{'file'}),3304-class=>"list"}, esc_path($diff->{'file'}));3305print"</td>\n";3306print"<td>$mode_chng</td>\n";3307print"<td class=\"link\">";3308if($actioneq'commitdiff') {3309# link to patch3310$patchno++;3311print$cgi->a({-href =>"#patch$patchno"},"patch");3312print" | ";3313}3314print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3315 hash_base=>$hash, file_name=>$diff->{'file'})},3316"blob");3317print"</td>\n";33183319}elsif($diff->{'status'}eq"D") {# deleted3320my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3321print"<td>";3322print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3323 hash_base=>$parent, file_name=>$diff->{'file'}),3324-class=>"list"}, esc_path($diff->{'file'}));3325print"</td>\n";3326print"<td>$mode_chng</td>\n";3327print"<td class=\"link\">";3328if($actioneq'commitdiff') {3329# link to patch3330$patchno++;3331print$cgi->a({-href =>"#patch$patchno"},"patch");3332print" | ";3333}3334print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3335 hash_base=>$parent, file_name=>$diff->{'file'})},3336"blob") ." | ";3337if($have_blame) {3338print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3339 file_name=>$diff->{'file'})},3340"blame") ." | ";3341}3342print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3343 file_name=>$diff->{'file'})},3344"history");3345print"</td>\n";33463347}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3348my$mode_chnge="";3349if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3350$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3351if($from_file_typene$to_file_type) {3352$mode_chnge.=" from$from_file_typeto$to_file_type";3353}3354if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3355if($from_mode_str&&$to_mode_str) {3356$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3357}elsif($to_mode_str) {3358$mode_chnge.=" mode:$to_mode_str";3359}3360}3361$mode_chnge.="]</span>\n";3362}3363print"<td>";3364print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3365 hash_base=>$hash, file_name=>$diff->{'file'}),3366-class=>"list"}, esc_path($diff->{'file'}));3367print"</td>\n";3368print"<td>$mode_chnge</td>\n";3369print"<td class=\"link\">";3370if($actioneq'commitdiff') {3371# link to patch3372$patchno++;3373print$cgi->a({-href =>"#patch$patchno"},"patch") .3374" | ";3375}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3376# "commit" view and modified file (not onlu mode changed)3377print$cgi->a({-href => href(action=>"blobdiff",3378 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3379 hash_base=>$hash, hash_parent_base=>$parent,3380 file_name=>$diff->{'file'})},3381"diff") .3382" | ";3383}3384print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3385 hash_base=>$hash, file_name=>$diff->{'file'})},3386"blob") ." | ";3387if($have_blame) {3388print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3389 file_name=>$diff->{'file'})},3390"blame") ." | ";3391}3392print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3393 file_name=>$diff->{'file'})},3394"history");3395print"</td>\n";33963397}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3398my%status_name= ('R'=>'moved','C'=>'copied');3399my$nstatus=$status_name{$diff->{'status'}};3400my$mode_chng="";3401if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3402# mode also for directories, so we cannot use $to_mode_str3403$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3404}3405print"<td>".3406$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3407 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3408-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3409"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3410$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3411 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3412-class=>"list"}, esc_path($diff->{'from_file'})) .3413" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3414"<td class=\"link\">";3415if($actioneq'commitdiff') {3416# link to patch3417$patchno++;3418print$cgi->a({-href =>"#patch$patchno"},"patch") .3419" | ";3420}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3421# "commit" view and modified file (not only pure rename or copy)3422print$cgi->a({-href => href(action=>"blobdiff",3423 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3424 hash_base=>$hash, hash_parent_base=>$parent,3425 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3426"diff") .3427" | ";3428}3429print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3430 hash_base=>$parent, file_name=>$diff->{'to_file'})},3431"blob") ." | ";3432if($have_blame) {3433print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3434 file_name=>$diff->{'to_file'})},3435"blame") ." | ";3436}3437print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3438 file_name=>$diff->{'to_file'})},3439"history");3440print"</td>\n";34413442}# we should not encounter Unmerged (U) or Unknown (X) status3443print"</tr>\n";3444}3445print"</tbody>"if$has_header;3446print"</table>\n";3447}34483449sub git_patchset_body {3450my($fd,$difftree,$hash,@hash_parents) =@_;3451my($hash_parent) =$hash_parents[0];34523453my$is_combined= (@hash_parents>1);3454my$patch_idx=0;3455my$patch_number=0;3456my$patch_line;3457my$diffinfo;3458my$to_name;3459my(%from,%to);34603461print"<div class=\"patchset\">\n";34623463# skip to first patch3464while($patch_line= <$fd>) {3465chomp$patch_line;34663467last if($patch_line=~m/^diff /);3468}34693470 PATCH:3471while($patch_line) {34723473# parse "git diff" header line3474if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3475# $1 is from_name, which we do not use3476$to_name= unquote($2);3477$to_name=~s!^b/!!;3478}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3479# $1 is 'cc' or 'combined', which we do not use3480$to_name= unquote($2);3481}else{3482$to_name=undef;3483}34843485# check if current patch belong to current raw line3486# and parse raw git-diff line if needed3487if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3488# this is continuation of a split patch3489print"<div class=\"patch cont\">\n";3490}else{3491# advance raw git-diff output if needed3492$patch_idx++ifdefined$diffinfo;34933494# read and prepare patch information3495$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);34963497# compact combined diff output can have some patches skipped3498# find which patch (using pathname of result) we are at now;3499if($is_combined) {3500while($to_namene$diffinfo->{'to_file'}) {3501print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3502 format_diff_cc_simplified($diffinfo,@hash_parents) .3503"</div>\n";# class="patch"35043505$patch_idx++;3506$patch_number++;35073508last if$patch_idx>$#$difftree;3509$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3510}3511}35123513# modifies %from, %to hashes3514 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);35153516# this is first patch for raw difftree line with $patch_idx index3517# we index @$difftree array from 0, but number patches from 13518print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3519}35203521# git diff header3522#assert($patch_line =~ m/^diff /) if DEBUG;3523#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3524$patch_number++;3525# print "git diff" header3526print format_git_diff_header_line($patch_line,$diffinfo,3527 \%from, \%to);35283529# print extended diff header3530print"<div class=\"diff extended_header\">\n";3531 EXTENDED_HEADER:3532while($patch_line= <$fd>) {3533chomp$patch_line;35343535last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);35363537print format_extended_diff_header_line($patch_line,$diffinfo,3538 \%from, \%to);3539}3540print"</div>\n";# class="diff extended_header"35413542# from-file/to-file diff header3543if(!$patch_line) {3544print"</div>\n";# class="patch"3545last PATCH;3546}3547next PATCH if($patch_line=~m/^diff /);3548#assert($patch_line =~ m/^---/) if DEBUG;35493550my$last_patch_line=$patch_line;3551$patch_line= <$fd>;3552chomp$patch_line;3553#assert($patch_line =~ m/^\+\+\+/) if DEBUG;35543555print format_diff_from_to_header($last_patch_line,$patch_line,3556$diffinfo, \%from, \%to,3557@hash_parents);35583559# the patch itself3560 LINE:3561while($patch_line= <$fd>) {3562chomp$patch_line;35633564next PATCH if($patch_line=~m/^diff /);35653566print format_diff_line($patch_line, \%from, \%to);3567}35683569}continue{3570print"</div>\n";# class="patch"3571}35723573# for compact combined (--cc) format, with chunk and patch simpliciaction3574# patchset might be empty, but there might be unprocessed raw lines3575for(++$patch_idxif$patch_number>0;3576$patch_idx<@$difftree;3577++$patch_idx) {3578# read and prepare patch information3579$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);35803581# generate anchor for "patch" links in difftree / whatchanged part3582print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3583 format_diff_cc_simplified($diffinfo,@hash_parents) .3584"</div>\n";# class="patch"35853586$patch_number++;3587}35883589if($patch_number==0) {3590if(@hash_parents>1) {3591print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3592}else{3593print"<div class=\"diff nodifferences\">No differences found</div>\n";3594}3595}35963597print"</div>\n";# class="patchset"3598}35993600# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36013602# fills project list info (age, description, owner, forks) for each3603# project in the list, removing invalid projects from returned list3604# NOTE: modifies $projlist, but does not remove entries from it3605sub fill_project_list_info {3606my($projlist,$check_forks) =@_;3607my@projects;36083609 PROJECT:3610foreachmy$pr(@$projlist) {3611my(@activity) = git_get_last_activity($pr->{'path'});3612unless(@activity) {3613next PROJECT;3614}3615($pr->{'age'},$pr->{'age_string'}) =@activity;3616if(!defined$pr->{'descr'}) {3617my$descr= git_get_project_description($pr->{'path'}) ||"";3618$descr= to_utf8($descr);3619$pr->{'descr_long'} =$descr;3620$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3621}3622if(!defined$pr->{'owner'}) {3623$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3624}3625if($check_forks) {3626my$pname=$pr->{'path'};3627if(($pname=~s/\.git$//) &&3628($pname!~/\/$/) &&3629(-d "$projectroot/$pname")) {3630$pr->{'forks'} ="-d$projectroot/$pname";3631}else{3632$pr->{'forks'} =0;3633}3634}3635push@projects,$pr;3636}36373638return@projects;3639}36403641# print 'sort by' <th> element, generating 'sort by $name' replay link3642# if that order is not selected3643sub print_sort_th {3644my($name,$order,$header) =@_;3645$header||=ucfirst($name);36463647if($ordereq$name) {3648print"<th>$header</th>\n";3649}else{3650print"<th>".3651$cgi->a({-href => href(-replay=>1, order=>$name),3652-class=>"header"},$header) .3653"</th>\n";3654}3655}36563657sub git_project_list_body {3658my($projlist,$order,$from,$to,$extra,$no_header) =@_;36593660my($check_forks) = gitweb_check_feature('forks');3661my@projects= fill_project_list_info($projlist,$check_forks);36623663$order||=$default_projects_order;3664$from=0unlessdefined$from;3665$to=$#projectsif(!defined$to||$#projects<$to);36663667my%order_info= (3668 project => { key =>'path', type =>'str'},3669 descr => { key =>'descr_long', type =>'str'},3670 owner => { key =>'owner', type =>'str'},3671 age => { key =>'age', type =>'num'}3672);3673my$oi=$order_info{$order};3674if($oi->{'type'}eq'str') {3675@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;3676}else{3677@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;3678}36793680print"<table class=\"project_list\">\n";3681unless($no_header) {3682print"<tr>\n";3683if($check_forks) {3684print"<th></th>\n";3685}3686 print_sort_th('project',$order,'Project');3687 print_sort_th('descr',$order,'Description');3688 print_sort_th('owner',$order,'Owner');3689 print_sort_th('age',$order,'Last Change');3690print"<th></th>\n".# for links3691"</tr>\n";3692}3693my$alternate=1;3694for(my$i=$from;$i<=$to;$i++) {3695my$pr=$projects[$i];3696if($alternate) {3697print"<tr class=\"dark\">\n";3698}else{3699print"<tr class=\"light\">\n";3700}3701$alternate^=1;3702if($check_forks) {3703print"<td>";3704if($pr->{'forks'}) {3705print"<!--$pr->{'forks'} -->\n";3706print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");3707}3708print"</td>\n";3709}3710print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3711-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".3712"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3713-class=>"list", -title =>$pr->{'descr_long'}},3714 esc_html($pr->{'descr'})) ."</td>\n".3715"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";3716print"<td class=\"". age_class($pr->{'age'}) ."\">".3717(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".3718"<td class=\"link\">".3719$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".3720$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".3721$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".3722$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .3723($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .3724"</td>\n".3725"</tr>\n";3726}3727if(defined$extra) {3728print"<tr>\n";3729if($check_forks) {3730print"<td></td>\n";3731}3732print"<td colspan=\"5\">$extra</td>\n".3733"</tr>\n";3734}3735print"</table>\n";3736}37373738sub git_shortlog_body {3739# uses global variable $project3740my($commitlist,$from,$to,$refs,$extra) =@_;37413742$from=0unlessdefined$from;3743$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);37443745print"<table class=\"shortlog\">\n";3746my$alternate=1;3747for(my$i=$from;$i<=$to;$i++) {3748my%co= %{$commitlist->[$i]};3749my$commit=$co{'id'};3750my$ref= format_ref_marker($refs,$commit);3751if($alternate) {3752print"<tr class=\"dark\">\n";3753}else{3754print"<tr class=\"light\">\n";3755}3756$alternate^=1;3757my$author= chop_and_escape_str($co{'author_name'},10);3758# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .3759print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".3760"<td><i>".$author."</i></td>\n".3761"<td>";3762print format_subject_html($co{'title'},$co{'title_short'},3763 href(action=>"commit", hash=>$commit),$ref);3764print"</td>\n".3765"<td class=\"link\">".3766$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".3767$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".3768$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");3769my$snapshot_links= format_snapshot_links($commit);3770if(defined$snapshot_links) {3771print" | ".$snapshot_links;3772}3773print"</td>\n".3774"</tr>\n";3775}3776if(defined$extra) {3777print"<tr>\n".3778"<td colspan=\"4\">$extra</td>\n".3779"</tr>\n";3780}3781print"</table>\n";3782}37833784sub git_history_body {3785# Warning: assumes constant type (blob or tree) during history3786my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;37873788$from=0unlessdefined$from;3789$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});37903791print"<table class=\"history\">\n";3792my$alternate=1;3793for(my$i=$from;$i<=$to;$i++) {3794my%co= %{$commitlist->[$i]};3795if(!%co) {3796next;3797}3798my$commit=$co{'id'};37993800my$ref= format_ref_marker($refs,$commit);38013802if($alternate) {3803print"<tr class=\"dark\">\n";3804}else{3805print"<tr class=\"light\">\n";3806}3807$alternate^=1;3808# shortlog uses chop_str($co{'author_name'}, 10)3809my$author= chop_and_escape_str($co{'author_name'},15,3);3810print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".3811"<td><i>".$author."</i></td>\n".3812"<td>";3813# originally git_history used chop_str($co{'title'}, 50)3814print format_subject_html($co{'title'},$co{'title_short'},3815 href(action=>"commit", hash=>$commit),$ref);3816print"</td>\n".3817"<td class=\"link\">".3818$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".3819$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");38203821if($ftypeeq'blob') {3822my$blob_current= git_get_hash_by_path($hash_base,$file_name);3823my$blob_parent= git_get_hash_by_path($commit,$file_name);3824if(defined$blob_current&&defined$blob_parent&&3825$blob_currentne$blob_parent) {3826print" | ".3827$cgi->a({-href => href(action=>"blobdiff",3828 hash=>$blob_current, hash_parent=>$blob_parent,3829 hash_base=>$hash_base, hash_parent_base=>$commit,3830 file_name=>$file_name)},3831"diff to current");3832}3833}3834print"</td>\n".3835"</tr>\n";3836}3837if(defined$extra) {3838print"<tr>\n".3839"<td colspan=\"4\">$extra</td>\n".3840"</tr>\n";3841}3842print"</table>\n";3843}38443845sub git_tags_body {3846# uses global variable $project3847my($taglist,$from,$to,$extra) =@_;3848$from=0unlessdefined$from;3849$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);38503851print"<table class=\"tags\">\n";3852my$alternate=1;3853for(my$i=$from;$i<=$to;$i++) {3854my$entry=$taglist->[$i];3855my%tag=%$entry;3856my$comment=$tag{'subject'};3857my$comment_short;3858if(defined$comment) {3859$comment_short= chop_str($comment,30,5);3860}3861if($alternate) {3862print"<tr class=\"dark\">\n";3863}else{3864print"<tr class=\"light\">\n";3865}3866$alternate^=1;3867if(defined$tag{'age'}) {3868print"<td><i>$tag{'age'}</i></td>\n";3869}else{3870print"<td></td>\n";3871}3872print"<td>".3873$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),3874-class=>"list name"}, esc_html($tag{'name'})) .3875"</td>\n".3876"<td>";3877if(defined$comment) {3878print format_subject_html($comment,$comment_short,3879 href(action=>"tag", hash=>$tag{'id'}));3880}3881print"</td>\n".3882"<td class=\"selflink\">";3883if($tag{'type'}eq"tag") {3884print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");3885}else{3886print" ";3887}3888print"</td>\n".3889"<td class=\"link\">"." | ".3890$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});3891if($tag{'reftype'}eq"commit") {3892print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .3893" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");3894}elsif($tag{'reftype'}eq"blob") {3895print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");3896}3897print"</td>\n".3898"</tr>";3899}3900if(defined$extra) {3901print"<tr>\n".3902"<td colspan=\"5\">$extra</td>\n".3903"</tr>\n";3904}3905print"</table>\n";3906}39073908sub git_heads_body {3909# uses global variable $project3910my($headlist,$head,$from,$to,$extra) =@_;3911$from=0unlessdefined$from;3912$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);39133914print"<table class=\"heads\">\n";3915my$alternate=1;3916for(my$i=$from;$i<=$to;$i++) {3917my$entry=$headlist->[$i];3918my%ref=%$entry;3919my$curr=$ref{'id'}eq$head;3920if($alternate) {3921print"<tr class=\"dark\">\n";3922}else{3923print"<tr class=\"light\">\n";3924}3925$alternate^=1;3926print"<td><i>$ref{'age'}</i></td>\n".3927($curr?"<td class=\"current_head\">":"<td>") .3928$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),3929-class=>"list name"},esc_html($ref{'name'})) .3930"</td>\n".3931"<td class=\"link\">".3932$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".3933$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".3934$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .3935"</td>\n".3936"</tr>";3937}3938if(defined$extra) {3939print"<tr>\n".3940"<td colspan=\"3\">$extra</td>\n".3941"</tr>\n";3942}3943print"</table>\n";3944}39453946sub git_search_grep_body {3947my($commitlist,$from,$to,$extra) =@_;3948$from=0unlessdefined$from;3949$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);39503951print"<table class=\"commit_search\">\n";3952my$alternate=1;3953for(my$i=$from;$i<=$to;$i++) {3954my%co= %{$commitlist->[$i]};3955if(!%co) {3956next;3957}3958my$commit=$co{'id'};3959if($alternate) {3960print"<tr class=\"dark\">\n";3961}else{3962print"<tr class=\"light\">\n";3963}3964$alternate^=1;3965my$author= chop_and_escape_str($co{'author_name'},15,5);3966print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".3967"<td><i>".$author."</i></td>\n".3968"<td>".3969$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),3970-class=>"list subject"},3971 chop_and_escape_str($co{'title'},50) ."<br/>");3972my$comment=$co{'comment'};3973foreachmy$line(@$comment) {3974if($line=~m/^(.*?)($search_regexp)(.*)$/i) {3975my($lead,$match,$trail) = ($1,$2,$3);3976$match= chop_str($match,70,5,'center');3977my$contextlen=int((80-length($match))/2);3978$contextlen=30if($contextlen>30);3979$lead= chop_str($lead,$contextlen,10,'left');3980$trail= chop_str($trail,$contextlen,10,'right');39813982$lead= esc_html($lead);3983$match= esc_html($match);3984$trail= esc_html($trail);39853986print"$lead<span class=\"match\">$match</span>$trail<br />";3987}3988}3989print"</td>\n".3990"<td class=\"link\">".3991$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .3992" | ".3993$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .3994" | ".3995$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");3996print"</td>\n".3997"</tr>\n";3998}3999if(defined$extra) {4000print"<tr>\n".4001"<td colspan=\"3\">$extra</td>\n".4002"</tr>\n";4003}4004print"</table>\n";4005}40064007## ======================================================================4008## ======================================================================4009## actions40104011sub git_project_list {4012my$order=$cgi->param('o');4013if(defined$order&&$order!~m/none|project|descr|owner|age/) {4014 die_error(400,"Unknown order parameter");4015}40164017my@list= git_get_projects_list();4018if(!@list) {4019 die_error(404,"No projects found");4020}40214022 git_header_html();4023if(-f $home_text) {4024print"<div class=\"index_include\">\n";4025open(my$fd,$home_text);4026print<$fd>;4027close$fd;4028print"</div>\n";4029}4030 git_project_list_body(\@list,$order);4031 git_footer_html();4032}40334034sub git_forks {4035my$order=$cgi->param('o');4036if(defined$order&&$order!~m/none|project|descr|owner|age/) {4037 die_error(400,"Unknown order parameter");4038}40394040my@list= git_get_projects_list($project);4041if(!@list) {4042 die_error(404,"No forks found");4043}40444045 git_header_html();4046 git_print_page_nav('','');4047 git_print_header_div('summary',"$projectforks");4048 git_project_list_body(\@list,$order);4049 git_footer_html();4050}40514052sub git_project_index {4053my@projects= git_get_projects_list($project);40544055print$cgi->header(4056-type =>'text/plain',4057-charset =>'utf-8',4058-content_disposition =>'inline; filename="index.aux"');40594060foreachmy$pr(@projects) {4061if(!exists$pr->{'owner'}) {4062$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4063}40644065my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4066# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4067$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4068$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4069$path=~s/ /\+/g;4070$owner=~s/ /\+/g;40714072print"$path$owner\n";4073}4074}40754076sub git_summary {4077my$descr= git_get_project_description($project) ||"none";4078my%co= parse_commit("HEAD");4079my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4080my$head=$co{'id'};40814082my$owner= git_get_project_owner($project);40834084my$refs= git_get_references();4085# These get_*_list functions return one more to allow us to see if4086# there are more ...4087my@taglist= git_get_tags_list(16);4088my@headlist= git_get_heads_list(16);4089my@forklist;4090my($check_forks) = gitweb_check_feature('forks');40914092if($check_forks) {4093@forklist= git_get_projects_list($project);4094}40954096 git_header_html();4097 git_print_page_nav('summary','',$head);40984099print"<div class=\"title\"> </div>\n";4100print"<table class=\"projects_list\">\n".4101"<tr><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4102"<tr><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4103if(defined$cd{'rfc2822'}) {4104print"<tr><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4105}41064107# use per project git URL list in $projectroot/$project/cloneurl4108# or make project git URL from git base URL and project name4109my$url_tag="URL";4110my@url_list= git_get_project_url_list($project);4111@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4112foreachmy$git_url(@url_list) {4113next unless$git_url;4114print"<tr><td>$url_tag</td><td>$git_url</td></tr>\n";4115$url_tag="";4116}4117print"</table>\n";41184119if(-s "$projectroot/$project/README.html") {4120if(open my$fd,"$projectroot/$project/README.html") {4121print"<div class=\"title\">readme</div>\n".4122"<div class=\"readme\">\n";4123print$_while(<$fd>);4124print"\n</div>\n";# class="readme"4125close$fd;4126}4127}41284129# we need to request one more than 16 (0..15) to check if4130# those 16 are all4131my@commitlist=$head? parse_commits($head,17) : ();4132if(@commitlist) {4133 git_print_header_div('shortlog');4134 git_shortlog_body(\@commitlist,0,15,$refs,4135$#commitlist<=15?undef:4136$cgi->a({-href => href(action=>"shortlog")},"..."));4137}41384139if(@taglist) {4140 git_print_header_div('tags');4141 git_tags_body(\@taglist,0,15,4142$#taglist<=15?undef:4143$cgi->a({-href => href(action=>"tags")},"..."));4144}41454146if(@headlist) {4147 git_print_header_div('heads');4148 git_heads_body(\@headlist,$head,0,15,4149$#headlist<=15?undef:4150$cgi->a({-href => href(action=>"heads")},"..."));4151}41524153if(@forklist) {4154 git_print_header_div('forks');4155 git_project_list_body(\@forklist,'age',0,15,4156$#forklist<=15?undef:4157$cgi->a({-href => href(action=>"forks")},"..."),4158'no_header');4159}41604161 git_footer_html();4162}41634164sub git_tag {4165my$head= git_get_head_hash($project);4166 git_header_html();4167 git_print_page_nav('','',$head,undef,$head);4168my%tag= parse_tag($hash);41694170if(!%tag) {4171 die_error(404,"Unknown tag object");4172}41734174 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4175print"<div class=\"title_text\">\n".4176"<table class=\"object_header\">\n".4177"<tr>\n".4178"<td>object</td>\n".4179"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4180$tag{'object'}) ."</td>\n".4181"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4182$tag{'type'}) ."</td>\n".4183"</tr>\n";4184if(defined($tag{'author'})) {4185my%ad= parse_date($tag{'epoch'},$tag{'tz'});4186print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4187print"<tr><td></td><td>".$ad{'rfc2822'} .4188sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4189"</td></tr>\n";4190}4191print"</table>\n\n".4192"</div>\n";4193print"<div class=\"page_body\">";4194my$comment=$tag{'comment'};4195foreachmy$line(@$comment) {4196chomp$line;4197print esc_html($line, -nbsp=>1) ."<br/>\n";4198}4199print"</div>\n";4200 git_footer_html();4201}42024203sub git_blame {4204my$fd;4205my$ftype;42064207 gitweb_check_feature('blame')4208or die_error(403,"Blame view not allowed");42094210 die_error(400,"No file name given")unless$file_name;4211$hash_base||= git_get_head_hash($project);4212 die_error(404,"Couldn't find base commit")unless($hash_base);4213my%co= parse_commit($hash_base)4214or die_error(404,"Commit not found");4215if(!defined$hash) {4216$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4217or die_error(404,"Error looking up file");4218}4219$ftype= git_get_type($hash);4220if($ftype!~"blob") {4221 die_error(400,"Object is not a blob");4222}4223open($fd,"-|", git_cmd(),"blame",'-p','--',4224$file_name,$hash_base)4225or die_error(500,"Open git-blame failed");4226 git_header_html();4227my$formats_nav=4228$cgi->a({-href => href(action=>"blob", -replay=>1)},4229"blob") .4230" | ".4231$cgi->a({-href => href(action=>"history", -replay=>1)},4232"history") .4233" | ".4234$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4235"HEAD");4236 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4237 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4238 git_print_page_path($file_name,$ftype,$hash_base);4239my@rev_color= (qw(light2 dark2));4240my$num_colors=scalar(@rev_color);4241my$current_color=0;4242my$last_rev;4243print<<HTML;4244<div class="page_body">4245<table class="blame">4246<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4247HTML4248my%metainfo= ();4249while(1) {4250$_= <$fd>;4251last unlessdefined$_;4252my($full_rev,$orig_lineno,$lineno,$group_size) =4253/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;4254if(!exists$metainfo{$full_rev}) {4255$metainfo{$full_rev} = {};4256}4257my$meta=$metainfo{$full_rev};4258while(<$fd>) {4259last if(s/^\t//);4260if(/^(\S+) (.*)$/) {4261$meta->{$1} =$2;4262}4263}4264my$data=$_;4265chomp$data;4266my$rev=substr($full_rev,0,8);4267my$author=$meta->{'author'};4268my%date= parse_date($meta->{'author-time'},4269$meta->{'author-tz'});4270my$date=$date{'iso-tz'};4271if($group_size) {4272$current_color= ++$current_color%$num_colors;4273}4274print"<tr class=\"$rev_color[$current_color]\">\n";4275if($group_size) {4276print"<td class=\"sha1\"";4277print" title=\"". esc_html($author) .",$date\"";4278print" rowspan=\"$group_size\""if($group_size>1);4279print">";4280print$cgi->a({-href => href(action=>"commit",4281 hash=>$full_rev,4282 file_name=>$file_name)},4283 esc_html($rev));4284print"</td>\n";4285}4286open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4287or die_error(500,"Open git-rev-parse failed");4288my$parent_commit= <$dd>;4289close$dd;4290chomp($parent_commit);4291my$blamed= href(action =>'blame',4292 file_name =>$meta->{'filename'},4293 hash_base =>$parent_commit);4294print"<td class=\"linenr\">";4295print$cgi->a({ -href =>"$blamed#l$orig_lineno",4296-id =>"l$lineno",4297-class=>"linenr"},4298 esc_html($lineno));4299print"</td>";4300print"<td class=\"pre\">". esc_html($data) ."</td>\n";4301print"</tr>\n";4302}4303print"</table>\n";4304print"</div>";4305close$fd4306or print"Reading blob failed\n";4307 git_footer_html();4308}43094310sub git_tags {4311my$head= git_get_head_hash($project);4312 git_header_html();4313 git_print_page_nav('','',$head,undef,$head);4314 git_print_header_div('summary',$project);43154316my@tagslist= git_get_tags_list();4317if(@tagslist) {4318 git_tags_body(\@tagslist);4319}4320 git_footer_html();4321}43224323sub git_heads {4324my$head= git_get_head_hash($project);4325 git_header_html();4326 git_print_page_nav('','',$head,undef,$head);4327 git_print_header_div('summary',$project);43284329my@headslist= git_get_heads_list();4330if(@headslist) {4331 git_heads_body(\@headslist,$head);4332}4333 git_footer_html();4334}43354336sub git_blob_plain {4337my$type=shift;4338my$expires;43394340if(!defined$hash) {4341if(defined$file_name) {4342my$base=$hash_base|| git_get_head_hash($project);4343$hash= git_get_hash_by_path($base,$file_name,"blob")4344or die_error(404,"Cannot find file");4345}else{4346 die_error(400,"No file name defined");4347}4348}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4349# blobs defined by non-textual hash id's can be cached4350$expires="+1d";4351}43524353open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4354or die_error(500,"Open git-cat-file blob '$hash' failed");43554356# content-type (can include charset)4357$type= blob_contenttype($fd,$file_name,$type);43584359# "save as" filename, even when no $file_name is given4360my$save_as="$hash";4361if(defined$file_name) {4362$save_as=$file_name;4363}elsif($type=~m/^text\//) {4364$save_as.='.txt';4365}43664367print$cgi->header(4368-type =>$type,4369-expires =>$expires,4370-content_disposition =>'inline; filename="'.$save_as.'"');4371undef$/;4372binmode STDOUT,':raw';4373print<$fd>;4374binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4375$/="\n";4376close$fd;4377}43784379sub git_blob {4380my$expires;43814382if(!defined$hash) {4383if(defined$file_name) {4384my$base=$hash_base|| git_get_head_hash($project);4385$hash= git_get_hash_by_path($base,$file_name,"blob")4386or die_error(404,"Cannot find file");4387}else{4388 die_error(400,"No file name defined");4389}4390}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4391# blobs defined by non-textual hash id's can be cached4392$expires="+1d";4393}43944395my($have_blame) = gitweb_check_feature('blame');4396open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4397or die_error(500,"Couldn't cat$file_name,$hash");4398my$mimetype= blob_mimetype($fd,$file_name);4399if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4400close$fd;4401return git_blob_plain($mimetype);4402}4403# we can have blame only for text/* mimetype4404$have_blame&&= ($mimetype=~m!^text/!);44054406 git_header_html(undef,$expires);4407my$formats_nav='';4408if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4409if(defined$file_name) {4410if($have_blame) {4411$formats_nav.=4412$cgi->a({-href => href(action=>"blame", -replay=>1)},4413"blame") .4414" | ";4415}4416$formats_nav.=4417$cgi->a({-href => href(action=>"history", -replay=>1)},4418"history") .4419" | ".4420$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4421"raw") .4422" | ".4423$cgi->a({-href => href(action=>"blob",4424 hash_base=>"HEAD", file_name=>$file_name)},4425"HEAD");4426}else{4427$formats_nav.=4428$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4429"raw");4430}4431 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4432 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4433}else{4434print"<div class=\"page_nav\">\n".4435"<br/><br/></div>\n".4436"<div class=\"title\">$hash</div>\n";4437}4438 git_print_page_path($file_name,"blob",$hash_base);4439print"<div class=\"page_body\">\n";4440if($mimetype=~m!^image/!) {4441print qq!<img type="$mimetype"!;4442if($file_name) {4443print qq! alt="$file_name" title="$file_name"!;4444}4445print qq! src="! .4446 href(action=>"blob_plain", hash=>$hash,4447 hash_base=>$hash_base, file_name=>$file_name) .4448 qq!"/>\n!;4449}else{4450my$nr;4451while(my$line= <$fd>) {4452chomp$line;4453$nr++;4454$line= untabify($line);4455printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4456$nr,$nr,$nr, esc_html($line, -nbsp=>1);4457}4458}4459close$fd4460or print"Reading blob failed.\n";4461print"</div>";4462 git_footer_html();4463}44644465sub git_tree {4466if(!defined$hash_base) {4467$hash_base="HEAD";4468}4469if(!defined$hash) {4470if(defined$file_name) {4471$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4472}else{4473$hash=$hash_base;4474}4475}4476$/="\0";4477open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4478or die_error(500,"Open git-ls-tree failed");4479my@entries=map{chomp;$_} <$fd>;4480close$fdor die_error(404,"Reading tree failed");4481$/="\n";44824483my$refs= git_get_references();4484my$ref= format_ref_marker($refs,$hash_base);4485 git_header_html();4486my$basedir='';4487my($have_blame) = gitweb_check_feature('blame');4488if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4489my@views_nav= ();4490if(defined$file_name) {4491push@views_nav,4492$cgi->a({-href => href(action=>"history", -replay=>1)},4493"history"),4494$cgi->a({-href => href(action=>"tree",4495 hash_base=>"HEAD", file_name=>$file_name)},4496"HEAD"),4497}4498my$snapshot_links= format_snapshot_links($hash);4499if(defined$snapshot_links) {4500# FIXME: Should be available when we have no hash base as well.4501push@views_nav,$snapshot_links;4502}4503 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4504 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4505}else{4506undef$hash_base;4507print"<div class=\"page_nav\">\n";4508print"<br/><br/></div>\n";4509print"<div class=\"title\">$hash</div>\n";4510}4511if(defined$file_name) {4512$basedir=$file_name;4513if($basedirne''&&substr($basedir, -1)ne'/') {4514$basedir.='/';4515}4516}4517 git_print_page_path($file_name,'tree',$hash_base);4518print"<div class=\"page_body\">\n";4519print"<table class=\"tree\">\n";4520my$alternate=1;4521# '..' (top directory) link if possible4522if(defined$hash_base&&4523defined$file_name&&$file_name=~m![^/]+$!) {4524if($alternate) {4525print"<tr class=\"dark\">\n";4526}else{4527print"<tr class=\"light\">\n";4528}4529$alternate^=1;45304531my$up=$file_name;4532$up=~s!/?[^/]+$!!;4533undef$upunless$up;4534# based on git_print_tree_entry4535print'<td class="mode">'. mode_str('040000') ."</td>\n";4536print'<td class="list">';4537print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4538 file_name=>$up)},4539"..");4540print"</td>\n";4541print"<td class=\"link\"></td>\n";45424543print"</tr>\n";4544}4545foreachmy$line(@entries) {4546my%t= parse_ls_tree_line($line, -z =>1);45474548if($alternate) {4549print"<tr class=\"dark\">\n";4550}else{4551print"<tr class=\"light\">\n";4552}4553$alternate^=1;45544555 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);45564557print"</tr>\n";4558}4559print"</table>\n".4560"</div>";4561 git_footer_html();4562}45634564sub git_snapshot {4565my@supported_fmts= gitweb_check_feature('snapshot');4566@supported_fmts= filter_snapshot_fmts(@supported_fmts);45674568my$format=$cgi->param('sf');4569if(!@supported_fmts) {4570 die_error(403,"Snapshots not allowed");4571}4572# default to first supported snapshot format4573$format||=$supported_fmts[0];4574if($format!~m/^[a-z0-9]+$/) {4575 die_error(400,"Invalid snapshot format parameter");4576}elsif(!exists($known_snapshot_formats{$format})) {4577 die_error(400,"Unknown snapshot format");4578}elsif(!grep($_eq$format,@supported_fmts)) {4579 die_error(403,"Unsupported snapshot format");4580}45814582if(!defined$hash) {4583$hash= git_get_head_hash($project);4584}45854586my$name=$project;4587$name=~ s,([^/])/*\.git$,$1,;4588$name= basename($name);4589my$filename= to_utf8($name);4590$name=~s/\047/\047\\\047\047/g;4591my$cmd;4592$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";4593$cmd= quote_command(4594 git_cmd(),'archive',4595"--format=$known_snapshot_formats{$format}{'format'}",4596"--prefix=$name/",$hash);4597if(exists$known_snapshot_formats{$format}{'compressor'}) {4598$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});4599}46004601print$cgi->header(4602-type =>$known_snapshot_formats{$format}{'type'},4603-content_disposition =>'inline; filename="'."$filename".'"',4604-status =>'200 OK');46054606open my$fd,"-|",$cmd4607or die_error(500,"Execute git-archive failed");4608binmode STDOUT,':raw';4609print<$fd>;4610binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4611close$fd;4612}46134614sub git_log {4615my$head= git_get_head_hash($project);4616if(!defined$hash) {4617$hash=$head;4618}4619if(!defined$page) {4620$page=0;4621}4622my$refs= git_get_references();46234624my@commitlist= parse_commits($hash,101, (100*$page));46254626my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);46274628 git_header_html();4629 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);46304631if(!@commitlist) {4632my%co= parse_commit($hash);46334634 git_print_header_div('summary',$project);4635print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";4636}4637my$to= ($#commitlist>=99) ? (99) : ($#commitlist);4638for(my$i=0;$i<=$to;$i++) {4639my%co= %{$commitlist[$i]};4640next if!%co;4641my$commit=$co{'id'};4642my$ref= format_ref_marker($refs,$commit);4643my%ad= parse_date($co{'author_epoch'});4644 git_print_header_div('commit',4645"<span class=\"age\">$co{'age_string'}</span>".4646 esc_html($co{'title'}) .$ref,4647$commit);4648print"<div class=\"title_text\">\n".4649"<div class=\"log_link\">\n".4650$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4651" | ".4652$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4653" | ".4654$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4655"<br/>\n".4656"</div>\n".4657"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".4658"</div>\n";46594660print"<div class=\"log_body\">\n";4661 git_print_log($co{'comment'}, -final_empty_line=>1);4662print"</div>\n";4663}4664if($#commitlist>=100) {4665print"<div class=\"page_nav\">\n";4666print$cgi->a({-href => href(-replay=>1, page=>$page+1),4667-accesskey =>"n", -title =>"Alt-n"},"next");4668print"</div>\n";4669}4670 git_footer_html();4671}46724673sub git_commit {4674$hash||=$hash_base||"HEAD";4675my%co= parse_commit($hash)4676or die_error(404,"Unknown commit object");4677my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});4678my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});46794680my$parent=$co{'parent'};4681my$parents=$co{'parents'};# listref46824683# we need to prepare $formats_nav before any parameter munging4684my$formats_nav;4685if(!defined$parent) {4686# --root commitdiff4687$formats_nav.='(initial)';4688}elsif(@$parents==1) {4689# single parent commit4690$formats_nav.=4691'(parent: '.4692$cgi->a({-href => href(action=>"commit",4693 hash=>$parent)},4694 esc_html(substr($parent,0,7))) .4695')';4696}else{4697# merge commit4698$formats_nav.=4699'(merge: '.4700join(' ',map{4701$cgi->a({-href => href(action=>"commit",4702 hash=>$_)},4703 esc_html(substr($_,0,7)));4704}@$parents) .4705')';4706}47074708if(!defined$parent) {4709$parent="--root";4710}4711my@difftree;4712open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",4713@diff_opts,4714(@$parents<=1?$parent:'-c'),4715$hash,"--"4716or die_error(500,"Open git-diff-tree failed");4717@difftree=map{chomp;$_} <$fd>;4718close$fdor die_error(404,"Reading git-diff-tree failed");47194720# non-textual hash id's can be cached4721my$expires;4722if($hash=~m/^[0-9a-fA-F]{40}$/) {4723$expires="+1d";4724}4725my$refs= git_get_references();4726my$ref= format_ref_marker($refs,$co{'id'});47274728 git_header_html(undef,$expires);4729 git_print_page_nav('commit','',4730$hash,$co{'tree'},$hash,4731$formats_nav);47324733if(defined$co{'parent'}) {4734 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);4735}else{4736 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);4737}4738print"<div class=\"title_text\">\n".4739"<table class=\"object_header\">\n";4740print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".4741"<tr>".4742"<td></td><td>$ad{'rfc2822'}";4743if($ad{'hour_local'} <6) {4744printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",4745$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});4746}else{4747printf(" (%02d:%02d%s)",4748$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});4749}4750print"</td>".4751"</tr>\n";4752print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";4753print"<tr><td></td><td>$cd{'rfc2822'}".4754sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .4755"</td></tr>\n";4756print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";4757print"<tr>".4758"<td>tree</td>".4759"<td class=\"sha1\">".4760$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),4761class=>"list"},$co{'tree'}) .4762"</td>".4763"<td class=\"link\">".4764$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},4765"tree");4766my$snapshot_links= format_snapshot_links($hash);4767if(defined$snapshot_links) {4768print" | ".$snapshot_links;4769}4770print"</td>".4771"</tr>\n";47724773foreachmy$par(@$parents) {4774print"<tr>".4775"<td>parent</td>".4776"<td class=\"sha1\">".4777$cgi->a({-href => href(action=>"commit", hash=>$par),4778class=>"list"},$par) .4779"</td>".4780"<td class=\"link\">".4781$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .4782" | ".4783$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .4784"</td>".4785"</tr>\n";4786}4787print"</table>".4788"</div>\n";47894790print"<div class=\"page_body\">\n";4791 git_print_log($co{'comment'});4792print"</div>\n";47934794 git_difftree_body(\@difftree,$hash,@$parents);47954796 git_footer_html();4797}47984799sub git_object {4800# object is defined by:4801# - hash or hash_base alone4802# - hash_base and file_name4803my$type;48044805# - hash or hash_base alone4806if($hash|| ($hash_base&& !defined$file_name)) {4807my$object_id=$hash||$hash_base;48084809open my$fd,"-|", quote_command(4810 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'4811or die_error(404,"Object does not exist");4812$type= <$fd>;4813chomp$type;4814close$fd4815or die_error(404,"Object does not exist");48164817# - hash_base and file_name4818}elsif($hash_base&&defined$file_name) {4819$file_name=~ s,/+$,,;48204821system(git_cmd(),"cat-file",'-e',$hash_base) ==04822or die_error(404,"Base object does not exist");48234824# here errors should not hapen4825open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name4826or die_error(500,"Open git-ls-tree failed");4827my$line= <$fd>;4828close$fd;48294830#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'4831unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {4832 die_error(404,"File or directory for given base does not exist");4833}4834$type=$2;4835$hash=$3;4836}else{4837 die_error(400,"Not enough information to find object");4838}48394840print$cgi->redirect(-uri => href(action=>$type, -full=>1,4841 hash=>$hash, hash_base=>$hash_base,4842 file_name=>$file_name),4843-status =>'302 Found');4844}48454846sub git_blobdiff {4847my$format=shift||'html';48484849my$fd;4850my@difftree;4851my%diffinfo;4852my$expires;48534854# preparing $fd and %diffinfo for git_patchset_body4855# new style URI4856if(defined$hash_base&&defined$hash_parent_base) {4857if(defined$file_name) {4858# read raw output4859open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,4860$hash_parent_base,$hash_base,4861"--", (defined$file_parent?$file_parent: ()),$file_name4862or die_error(500,"Open git-diff-tree failed");4863@difftree=map{chomp;$_} <$fd>;4864close$fd4865or die_error(404,"Reading git-diff-tree failed");4866@difftree4867or die_error(404,"Blob diff not found");48684869}elsif(defined$hash&&4870$hash=~/[0-9a-fA-F]{40}/) {4871# try to find filename from $hash48724873# read filtered raw output4874open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,4875$hash_parent_base,$hash_base,"--"4876or die_error(500,"Open git-diff-tree failed");4877@difftree=4878# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'4879# $hash == to_id4880grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}4881map{chomp;$_} <$fd>;4882close$fd4883or die_error(404,"Reading git-diff-tree failed");4884@difftree4885or die_error(404,"Blob diff not found");48864887}else{4888 die_error(400,"Missing one of the blob diff parameters");4889}48904891if(@difftree>1) {4892 die_error(400,"Ambiguous blob diff specification");4893}48944895%diffinfo= parse_difftree_raw_line($difftree[0]);4896$file_parent||=$diffinfo{'from_file'} ||$file_name;4897$file_name||=$diffinfo{'to_file'};48984899$hash_parent||=$diffinfo{'from_id'};4900$hash||=$diffinfo{'to_id'};49014902# non-textual hash id's can be cached4903if($hash_base=~m/^[0-9a-fA-F]{40}$/&&4904$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {4905$expires='+1d';4906}49074908# open patch output4909open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,4910'-p', ($formateq'html'?"--full-index": ()),4911$hash_parent_base,$hash_base,4912"--", (defined$file_parent?$file_parent: ()),$file_name4913or die_error(500,"Open git-diff-tree failed");4914}49154916# old/legacy style URI4917if(!%diffinfo&&# if new style URI failed4918defined$hash&&defined$hash_parent) {4919# fake git-diff-tree raw output4920$diffinfo{'from_mode'} =$diffinfo{'to_mode'} ="blob";4921$diffinfo{'from_id'} =$hash_parent;4922$diffinfo{'to_id'} =$hash;4923if(defined$file_name) {4924if(defined$file_parent) {4925$diffinfo{'status'} ='2';4926$diffinfo{'from_file'} =$file_parent;4927$diffinfo{'to_file'} =$file_name;4928}else{# assume not renamed4929$diffinfo{'status'} ='1';4930$diffinfo{'from_file'} =$file_name;4931$diffinfo{'to_file'} =$file_name;4932}4933}else{# no filename given4934$diffinfo{'status'} ='2';4935$diffinfo{'from_file'} =$hash_parent;4936$diffinfo{'to_file'} =$hash;4937}49384939# non-textual hash id's can be cached4940if($hash=~m/^[0-9a-fA-F]{40}$/&&4941$hash_parent=~m/^[0-9a-fA-F]{40}$/) {4942$expires='+1d';4943}49444945# open patch output4946open$fd,"-|", git_cmd(),"diff",@diff_opts,4947'-p', ($formateq'html'?"--full-index": ()),4948$hash_parent,$hash,"--"4949or die_error(500,"Open git-diff failed");4950}else{4951 die_error(400,"Missing one of the blob diff parameters")4952unless%diffinfo;4953}49544955# header4956if($formateq'html') {4957my$formats_nav=4958$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},4959"raw");4960 git_header_html(undef,$expires);4961if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4962 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4963 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4964}else{4965print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";4966print"<div class=\"title\">$hashvs$hash_parent</div>\n";4967}4968if(defined$file_name) {4969 git_print_page_path($file_name,"blob",$hash_base);4970}else{4971print"<div class=\"page_path\"></div>\n";4972}49734974}elsif($formateq'plain') {4975print$cgi->header(4976-type =>'text/plain',4977-charset =>'utf-8',4978-expires =>$expires,4979-content_disposition =>'inline; filename="'."$file_name".'.patch"');49804981print"X-Git-Url: ".$cgi->self_url() ."\n\n";49824983}else{4984 die_error(400,"Unknown blobdiff format");4985}49864987# patch4988if($formateq'html') {4989print"<div class=\"page_body\">\n";49904991 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);4992close$fd;49934994print"</div>\n";# class="page_body"4995 git_footer_html();49964997}else{4998while(my$line= <$fd>) {4999$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5000$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;50015002print$line;50035004last if$line=~m!^\+\+\+!;5005}5006local$/=undef;5007print<$fd>;5008close$fd;5009}5010}50115012sub git_blobdiff_plain {5013 git_blobdiff('plain');5014}50155016sub git_commitdiff {5017my$format=shift||'html';5018$hash||=$hash_base||"HEAD";5019my%co= parse_commit($hash)5020or die_error(404,"Unknown commit object");50215022# choose format for commitdiff for merge5023if(!defined$hash_parent&& @{$co{'parents'}} >1) {5024$hash_parent='--cc';5025}5026# we need to prepare $formats_nav before almost any parameter munging5027my$formats_nav;5028if($formateq'html') {5029$formats_nav=5030$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5031"raw");50325033if(defined$hash_parent&&5034$hash_parentne'-c'&&$hash_parentne'--cc') {5035# commitdiff with two commits given5036my$hash_parent_short=$hash_parent;5037if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5038$hash_parent_short=substr($hash_parent,0,7);5039}5040$formats_nav.=5041' (from';5042for(my$i=0;$i< @{$co{'parents'}};$i++) {5043if($co{'parents'}[$i]eq$hash_parent) {5044$formats_nav.=' parent '. ($i+1);5045last;5046}5047}5048$formats_nav.=': '.5049$cgi->a({-href => href(action=>"commitdiff",5050 hash=>$hash_parent)},5051 esc_html($hash_parent_short)) .5052')';5053}elsif(!$co{'parent'}) {5054# --root commitdiff5055$formats_nav.=' (initial)';5056}elsif(scalar@{$co{'parents'}} ==1) {5057# single parent commit5058$formats_nav.=5059' (parent: '.5060$cgi->a({-href => href(action=>"commitdiff",5061 hash=>$co{'parent'})},5062 esc_html(substr($co{'parent'},0,7))) .5063')';5064}else{5065# merge commit5066if($hash_parenteq'--cc') {5067$formats_nav.=' | '.5068$cgi->a({-href => href(action=>"commitdiff",5069 hash=>$hash, hash_parent=>'-c')},5070'combined');5071}else{# $hash_parent eq '-c'5072$formats_nav.=' | '.5073$cgi->a({-href => href(action=>"commitdiff",5074 hash=>$hash, hash_parent=>'--cc')},5075'compact');5076}5077$formats_nav.=5078' (merge: '.5079join(' ',map{5080$cgi->a({-href => href(action=>"commitdiff",5081 hash=>$_)},5082 esc_html(substr($_,0,7)));5083} @{$co{'parents'}} ) .5084')';5085}5086}50875088my$hash_parent_param=$hash_parent;5089if(!defined$hash_parent_param) {5090# --cc for multiple parents, --root for parentless5091$hash_parent_param=5092@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5093}50945095# read commitdiff5096my$fd;5097my@difftree;5098if($formateq'html') {5099open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5100"--no-commit-id","--patch-with-raw","--full-index",5101$hash_parent_param,$hash,"--"5102or die_error(500,"Open git-diff-tree failed");51035104while(my$line= <$fd>) {5105chomp$line;5106# empty line ends raw part of diff-tree output5107last unless$line;5108push@difftree,scalar parse_difftree_raw_line($line);5109}51105111}elsif($formateq'plain') {5112open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5113'-p',$hash_parent_param,$hash,"--"5114or die_error(500,"Open git-diff-tree failed");51155116}else{5117 die_error(400,"Unknown commitdiff format");5118}51195120# non-textual hash id's can be cached5121my$expires;5122if($hash=~m/^[0-9a-fA-F]{40}$/) {5123$expires="+1d";5124}51255126# write commit message5127if($formateq'html') {5128my$refs= git_get_references();5129my$ref= format_ref_marker($refs,$co{'id'});51305131 git_header_html(undef,$expires);5132 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5133 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5134 git_print_authorship(\%co);5135print"<div class=\"page_body\">\n";5136if(@{$co{'comment'}} >1) {5137print"<div class=\"log\">\n";5138 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5139print"</div>\n";# class="log"5140}51415142}elsif($formateq'plain') {5143my$refs= git_get_references("tags");5144my$tagname= git_get_rev_name_tags($hash);5145my$filename= basename($project) ."-$hash.patch";51465147print$cgi->header(5148-type =>'text/plain',5149-charset =>'utf-8',5150-expires =>$expires,5151-content_disposition =>'inline; filename="'."$filename".'"');5152my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5153print"From: ". to_utf8($co{'author'}) ."\n";5154print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5155print"Subject: ". to_utf8($co{'title'}) ."\n";51565157print"X-Git-Tag:$tagname\n"if$tagname;5158print"X-Git-Url: ".$cgi->self_url() ."\n\n";51595160foreachmy$line(@{$co{'comment'}}) {5161print to_utf8($line) ."\n";5162}5163print"---\n\n";5164}51655166# write patch5167if($formateq'html') {5168my$use_parents= !defined$hash_parent||5169$hash_parenteq'-c'||$hash_parenteq'--cc';5170 git_difftree_body(\@difftree,$hash,5171$use_parents? @{$co{'parents'}} :$hash_parent);5172print"<br/>\n";51735174 git_patchset_body($fd, \@difftree,$hash,5175$use_parents? @{$co{'parents'}} :$hash_parent);5176close$fd;5177print"</div>\n";# class="page_body"5178 git_footer_html();51795180}elsif($formateq'plain') {5181local$/=undef;5182print<$fd>;5183close$fd5184or print"Reading git-diff-tree failed\n";5185}5186}51875188sub git_commitdiff_plain {5189 git_commitdiff('plain');5190}51915192sub git_history {5193if(!defined$hash_base) {5194$hash_base= git_get_head_hash($project);5195}5196if(!defined$page) {5197$page=0;5198}5199my$ftype;5200my%co= parse_commit($hash_base)5201or die_error(404,"Unknown commit object");52025203my$refs= git_get_references();5204my$limit=sprintf("--max-count=%i", (100* ($page+1)));52055206my@commitlist= parse_commits($hash_base,101, (100*$page),5207$file_name,"--full-history")5208or die_error(404,"No such file or directory on given branch");52095210if(!defined$hash&&defined$file_name) {5211# some commits could have deleted file in question,5212# and not have it in tree, but one of them has to have it5213for(my$i=0;$i<=@commitlist;$i++) {5214$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5215last ifdefined$hash;5216}5217}5218if(defined$hash) {5219$ftype= git_get_type($hash);5220}5221if(!defined$ftype) {5222 die_error(500,"Unknown type of object");5223}52245225my$paging_nav='';5226if($page>0) {5227$paging_nav.=5228$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5229 file_name=>$file_name)},5230"first");5231$paging_nav.=" ⋅ ".5232$cgi->a({-href => href(-replay=>1, page=>$page-1),5233-accesskey =>"p", -title =>"Alt-p"},"prev");5234}else{5235$paging_nav.="first";5236$paging_nav.=" ⋅ prev";5237}5238my$next_link='';5239if($#commitlist>=100) {5240$next_link=5241$cgi->a({-href => href(-replay=>1, page=>$page+1),5242-accesskey =>"n", -title =>"Alt-n"},"next");5243$paging_nav.=" ⋅$next_link";5244}else{5245$paging_nav.=" ⋅ next";5246}52475248 git_header_html();5249 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5250 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5251 git_print_page_path($file_name,$ftype,$hash_base);52525253 git_history_body(\@commitlist,0,99,5254$refs,$hash_base,$ftype,$next_link);52555256 git_footer_html();5257}52585259sub git_search {5260 gitweb_check_feature('search')or die_error(403,"Search is disabled");5261if(!defined$searchtext) {5262 die_error(400,"Text field is empty");5263}5264if(!defined$hash) {5265$hash= git_get_head_hash($project);5266}5267my%co= parse_commit($hash);5268if(!%co) {5269 die_error(404,"Unknown commit object");5270}5271if(!defined$page) {5272$page=0;5273}52745275$searchtype||='commit';5276if($searchtypeeq'pickaxe') {5277# pickaxe may take all resources of your box and run for several minutes5278# with every query - so decide by yourself how public you make this feature5279 gitweb_check_feature('pickaxe')5280or die_error(403,"Pickaxe is disabled");5281}5282if($searchtypeeq'grep') {5283 gitweb_check_feature('grep')5284or die_error(403,"Grep is disabled");5285}52865287 git_header_html();52885289if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5290my$greptype;5291if($searchtypeeq'commit') {5292$greptype="--grep=";5293}elsif($searchtypeeq'author') {5294$greptype="--author=";5295}elsif($searchtypeeq'committer') {5296$greptype="--committer=";5297}5298$greptype.=$searchtext;5299my@commitlist= parse_commits($hash,101, (100*$page),undef,5300$greptype,'--regexp-ignore-case',5301$search_use_regexp?'--extended-regexp':'--fixed-strings');53025303my$paging_nav='';5304if($page>0) {5305$paging_nav.=5306$cgi->a({-href => href(action=>"search", hash=>$hash,5307 searchtext=>$searchtext,5308 searchtype=>$searchtype)},5309"first");5310$paging_nav.=" ⋅ ".5311$cgi->a({-href => href(-replay=>1, page=>$page-1),5312-accesskey =>"p", -title =>"Alt-p"},"prev");5313}else{5314$paging_nav.="first";5315$paging_nav.=" ⋅ prev";5316}5317my$next_link='';5318if($#commitlist>=100) {5319$next_link=5320$cgi->a({-href => href(-replay=>1, page=>$page+1),5321-accesskey =>"n", -title =>"Alt-n"},"next");5322$paging_nav.=" ⋅$next_link";5323}else{5324$paging_nav.=" ⋅ next";5325}53265327if($#commitlist>=100) {5328}53295330 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5331 git_print_header_div('commit', esc_html($co{'title'}),$hash);5332 git_search_grep_body(\@commitlist,0,99,$next_link);5333}53345335if($searchtypeeq'pickaxe') {5336 git_print_page_nav('','',$hash,$co{'tree'},$hash);5337 git_print_header_div('commit', esc_html($co{'title'}),$hash);53385339print"<table class=\"pickaxe search\">\n";5340my$alternate=1;5341$/="\n";5342open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5343'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5344($search_use_regexp?'--pickaxe-regex': ());5345undef%co;5346my@files;5347while(my$line= <$fd>) {5348chomp$line;5349next unless$line;53505351my%set= parse_difftree_raw_line($line);5352if(defined$set{'commit'}) {5353# finish previous commit5354if(%co) {5355print"</td>\n".5356"<td class=\"link\">".5357$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5358" | ".5359$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5360print"</td>\n".5361"</tr>\n";5362}53635364if($alternate) {5365print"<tr class=\"dark\">\n";5366}else{5367print"<tr class=\"light\">\n";5368}5369$alternate^=1;5370%co= parse_commit($set{'commit'});5371my$author= chop_and_escape_str($co{'author_name'},15,5);5372print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5373"<td><i>$author</i></td>\n".5374"<td>".5375$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5376-class=>"list subject"},5377 chop_and_escape_str($co{'title'},50) ."<br/>");5378}elsif(defined$set{'to_id'}) {5379next if($set{'to_id'} =~m/^0{40}$/);53805381print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5382 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5383-class=>"list"},5384"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5385"<br/>\n";5386}5387}5388close$fd;53895390# finish last commit (warning: repetition!)5391if(%co) {5392print"</td>\n".5393"<td class=\"link\">".5394$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5395" | ".5396$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5397print"</td>\n".5398"</tr>\n";5399}54005401print"</table>\n";5402}54035404if($searchtypeeq'grep') {5405 git_print_page_nav('','',$hash,$co{'tree'},$hash);5406 git_print_header_div('commit', esc_html($co{'title'}),$hash);54075408print"<table class=\"grep_search\">\n";5409my$alternate=1;5410my$matches=0;5411$/="\n";5412open my$fd,"-|", git_cmd(),'grep','-n',5413$search_use_regexp? ('-E','-i') :'-F',5414$searchtext,$co{'tree'};5415my$lastfile='';5416while(my$line= <$fd>) {5417chomp$line;5418my($file,$lno,$ltext,$binary);5419last if($matches++>1000);5420if($line=~/^Binary file (.+) matches$/) {5421$file=$1;5422$binary=1;5423}else{5424(undef,$file,$lno,$ltext) =split(/:/,$line,4);5425}5426if($filene$lastfile) {5427$lastfileand print"</td></tr>\n";5428if($alternate++) {5429print"<tr class=\"dark\">\n";5430}else{5431print"<tr class=\"light\">\n";5432}5433print"<td class=\"list\">".5434$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5435 file_name=>"$file"),5436-class=>"list"}, esc_path($file));5437print"</td><td>\n";5438$lastfile=$file;5439}5440if($binary) {5441print"<div class=\"binary\">Binary file</div>\n";5442}else{5443$ltext= untabify($ltext);5444if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5445$ltext= esc_html($1, -nbsp=>1);5446$ltext.='<span class="match">';5447$ltext.= esc_html($2, -nbsp=>1);5448$ltext.='</span>';5449$ltext.= esc_html($3, -nbsp=>1);5450}else{5451$ltext= esc_html($ltext, -nbsp=>1);5452}5453print"<div class=\"pre\">".5454$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5455 file_name=>"$file").'#l'.$lno,5456-class=>"linenr"},sprintf('%4i',$lno))5457.' '.$ltext."</div>\n";5458}5459}5460if($lastfile) {5461print"</td></tr>\n";5462if($matches>1000) {5463print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5464}5465}else{5466print"<div class=\"diff nodifferences\">No matches found</div>\n";5467}5468close$fd;54695470print"</table>\n";5471}5472 git_footer_html();5473}54745475sub git_search_help {5476 git_header_html();5477 git_print_page_nav('','',$hash,$hash,$hash);5478print<<EOT;5479<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5480regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5481the pattern entered is recognized as the POSIX extended5482<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5483insensitive).</p>5484<dl>5485<dt><b>commit</b></dt>5486<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5487EOT5488my($have_grep) = gitweb_check_feature('grep');5489if($have_grep) {5490print<<EOT;5491<dt><b>grep</b></dt>5492<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5493 a different one) are searched for the given pattern. On large trees, this search can take5494a while and put some strain on the server, so please use it with some consideration. Note that5495due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5496case-sensitive.</dd>5497EOT5498}5499print<<EOT;5500<dt><b>author</b></dt>5501<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5502<dt><b>committer</b></dt>5503<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5504EOT5505my($have_pickaxe) = gitweb_check_feature('pickaxe');5506if($have_pickaxe) {5507print<<EOT;5508<dt><b>pickaxe</b></dt>5509<dd>All commits that caused the string to appear or disappear from any file (changes that5510added, removed or "modified" the string) will be listed. This search can take a while and5511takes a lot of strain on the server, so please use it wisely. Note that since you may be5512interested even in changes just changing the case as well, this search is case sensitive.</dd>5513EOT5514}5515print"</dl>\n";5516 git_footer_html();5517}55185519sub git_shortlog {5520my$head= git_get_head_hash($project);5521if(!defined$hash) {5522$hash=$head;5523}5524if(!defined$page) {5525$page=0;5526}5527my$refs= git_get_references();55285529my$commit_hash=$hash;5530if(defined$hash_parent) {5531$commit_hash="$hash_parent..$hash";5532}5533my@commitlist= parse_commits($commit_hash,101, (100*$page));55345535my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5536my$next_link='';5537if($#commitlist>=100) {5538$next_link=5539$cgi->a({-href => href(-replay=>1, page=>$page+1),5540-accesskey =>"n", -title =>"Alt-n"},"next");5541}55425543 git_header_html();5544 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);5545 git_print_header_div('summary',$project);55465547 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);55485549 git_footer_html();5550}55515552## ......................................................................5553## feeds (RSS, Atom; OPML)55545555sub git_feed {5556my$format=shift||'atom';5557my($have_blame) = gitweb_check_feature('blame');55585559# Atom: http://www.atomenabled.org/developers/syndication/5560# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ5561if($formatne'rss'&&$formatne'atom') {5562 die_error(400,"Unknown web feed format");5563}55645565# log/feed of current (HEAD) branch, log of given branch, history of file/directory5566my$head=$hash||'HEAD';5567my@commitlist= parse_commits($head,150,0,$file_name);55685569my%latest_commit;5570my%latest_date;5571my$content_type="application/$format+xml";5572if(defined$cgi->http('HTTP_ACCEPT') &&5573$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {5574# browser (feed reader) prefers text/xml5575$content_type='text/xml';5576}5577if(defined($commitlist[0])) {5578%latest_commit= %{$commitlist[0]};5579%latest_date= parse_date($latest_commit{'author_epoch'});5580print$cgi->header(5581-type =>$content_type,5582-charset =>'utf-8',5583-last_modified =>$latest_date{'rfc2822'});5584}else{5585print$cgi->header(5586-type =>$content_type,5587-charset =>'utf-8');5588}55895590# Optimization: skip generating the body if client asks only5591# for Last-Modified date.5592return if($cgi->request_method()eq'HEAD');55935594# header variables5595my$title="$site_name-$project/$action";5596my$feed_type='log';5597if(defined$hash) {5598$title.=" - '$hash'";5599$feed_type='branch log';5600if(defined$file_name) {5601$title.=" ::$file_name";5602$feed_type='history';5603}5604}elsif(defined$file_name) {5605$title.=" -$file_name";5606$feed_type='history';5607}5608$title.="$feed_type";5609my$descr= git_get_project_description($project);5610if(defined$descr) {5611$descr= esc_html($descr);5612}else{5613$descr="$project".5614($formateq'rss'?'RSS':'Atom') .5615" feed";5616}5617my$owner= git_get_project_owner($project);5618$owner= esc_html($owner);56195620#header5621my$alt_url;5622if(defined$file_name) {5623$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);5624}elsif(defined$hash) {5625$alt_url= href(-full=>1, action=>"log", hash=>$hash);5626}else{5627$alt_url= href(-full=>1, action=>"summary");5628}5629print qq!<?xml version="1.0" encoding="utf-8"?>\n!;5630if($formateq'rss') {5631print<<XML;5632<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">5633<channel>5634XML5635print"<title>$title</title>\n".5636"<link>$alt_url</link>\n".5637"<description>$descr</description>\n".5638"<language>en</language>\n";5639}elsif($formateq'atom') {5640print<<XML;5641<feed xmlns="http://www.w3.org/2005/Atom">5642XML5643print"<title>$title</title>\n".5644"<subtitle>$descr</subtitle>\n".5645'<link rel="alternate" type="text/html" href="'.5646$alt_url.'" />'."\n".5647'<link rel="self" type="'.$content_type.'" href="'.5648$cgi->self_url() .'" />'."\n".5649"<id>". href(-full=>1) ."</id>\n".5650# use project owner for feed author5651"<author><name>$owner</name></author>\n";5652if(defined$favicon) {5653print"<icon>". esc_url($favicon) ."</icon>\n";5654}5655if(defined$logo_url) {5656# not twice as wide as tall: 72 x 27 pixels5657print"<logo>". esc_url($logo) ."</logo>\n";5658}5659if(!%latest_date) {5660# dummy date to keep the feed valid until commits trickle in:5661print"<updated>1970-01-01T00:00:00Z</updated>\n";5662}else{5663print"<updated>$latest_date{'iso-8601'}</updated>\n";5664}5665}56665667# contents5668for(my$i=0;$i<=$#commitlist;$i++) {5669my%co= %{$commitlist[$i]};5670my$commit=$co{'id'};5671# we read 150, we always show 30 and the ones more recent than 48 hours5672if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {5673last;5674}5675my%cd= parse_date($co{'author_epoch'});56765677# get list of changed files5678open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5679$co{'parent'} ||"--root",5680$co{'id'},"--", (defined$file_name?$file_name: ())5681ornext;5682my@difftree=map{chomp;$_} <$fd>;5683close$fd5684ornext;56855686# print element (entry, item)5687my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);5688if($formateq'rss') {5689print"<item>\n".5690"<title>". esc_html($co{'title'}) ."</title>\n".5691"<author>". esc_html($co{'author'}) ."</author>\n".5692"<pubDate>$cd{'rfc2822'}</pubDate>\n".5693"<guid isPermaLink=\"true\">$co_url</guid>\n".5694"<link>$co_url</link>\n".5695"<description>". esc_html($co{'title'}) ."</description>\n".5696"<content:encoded>".5697"<![CDATA[\n";5698}elsif($formateq'atom') {5699print"<entry>\n".5700"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".5701"<updated>$cd{'iso-8601'}</updated>\n".5702"<author>\n".5703" <name>". esc_html($co{'author_name'}) ."</name>\n";5704if($co{'author_email'}) {5705print" <email>". esc_html($co{'author_email'}) ."</email>\n";5706}5707print"</author>\n".5708# use committer for contributor5709"<contributor>\n".5710" <name>". esc_html($co{'committer_name'}) ."</name>\n";5711if($co{'committer_email'}) {5712print" <email>". esc_html($co{'committer_email'}) ."</email>\n";5713}5714print"</contributor>\n".5715"<published>$cd{'iso-8601'}</published>\n".5716"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".5717"<id>$co_url</id>\n".5718"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".5719"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";5720}5721my$comment=$co{'comment'};5722print"<pre>\n";5723foreachmy$line(@$comment) {5724$line= esc_html($line);5725print"$line\n";5726}5727print"</pre><ul>\n";5728foreachmy$difftree_line(@difftree) {5729my%difftree= parse_difftree_raw_line($difftree_line);5730next if!$difftree{'from_id'};57315732my$file=$difftree{'file'} ||$difftree{'to_file'};57335734print"<li>".5735"[".5736$cgi->a({-href => href(-full=>1, action=>"blobdiff",5737 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},5738 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},5739 file_name=>$file, file_parent=>$difftree{'from_file'}),5740-title =>"diff"},'D');5741if($have_blame) {5742print$cgi->a({-href => href(-full=>1, action=>"blame",5743 file_name=>$file, hash_base=>$commit),5744-title =>"blame"},'B');5745}5746# if this is not a feed of a file history5747if(!defined$file_name||$file_namene$file) {5748print$cgi->a({-href => href(-full=>1, action=>"history",5749 file_name=>$file, hash=>$commit),5750-title =>"history"},'H');5751}5752$file= esc_path($file);5753print"] ".5754"$file</li>\n";5755}5756if($formateq'rss') {5757print"</ul>]]>\n".5758"</content:encoded>\n".5759"</item>\n";5760}elsif($formateq'atom') {5761print"</ul>\n</div>\n".5762"</content>\n".5763"</entry>\n";5764}5765}57665767# end of feed5768if($formateq'rss') {5769print"</channel>\n</rss>\n";5770}elsif($formateq'atom') {5771print"</feed>\n";5772}5773}57745775sub git_rss {5776 git_feed('rss');5777}57785779sub git_atom {5780 git_feed('atom');5781}57825783sub git_opml {5784my@list= git_get_projects_list();57855786print$cgi->header(-type =>'text/xml', -charset =>'utf-8');5787print<<XML;5788<?xml version="1.0" encoding="utf-8"?>5789<opml version="1.0">5790<head>5791 <title>$site_nameOPML Export</title>5792</head>5793<body>5794<outline text="git RSS feeds">5795XML57965797foreachmy$pr(@list) {5798my%proj=%$pr;5799my$head= git_get_head_hash($proj{'path'});5800if(!defined$head) {5801next;5802}5803$git_dir="$projectroot/$proj{'path'}";5804my%co= parse_commit($head);5805if(!%co) {5806next;5807}58085809my$path= esc_html(chop_str($proj{'path'},25,5));5810my$rss="$my_url?p=$proj{'path'};a=rss";5811my$html="$my_url?p=$proj{'path'};a=summary";5812print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";5813}5814print<<XML;5815</outline>5816</body>5817</opml>5818XML5819}