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 21our$t0; 22if(eval{require Time::HiRes;1; }) { 23$t0= [Time::HiRes::gettimeofday()]; 24} 25our$number_of_git_cmds=0; 26 27BEGIN{ 28 CGI->compile()if$ENV{'MOD_PERL'}; 29} 30 31our$version="++GIT_VERSION++"; 32 33our($my_url,$my_uri,$base_url,$path_info,$home_link); 34sub evaluate_uri { 35our$cgi; 36 37our$my_url=$cgi->url(); 38our$my_uri=$cgi->url(-absolute =>1); 39 40# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 41# needed and used only for URLs with nonempty PATH_INFO 42our$base_url=$my_url; 43 44# When the script is used as DirectoryIndex, the URL does not contain the name 45# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 46# have to do it ourselves. We make $path_info global because it's also used 47# later on. 48# 49# Another issue with the script being the DirectoryIndex is that the resulting 50# $my_url data is not the full script URL: this is good, because we want 51# generated links to keep implying the script name if it wasn't explicitly 52# indicated in the URL we're handling, but it means that $my_url cannot be used 53# as base URL. 54# Therefore, if we needed to strip PATH_INFO, then we know that we have 55# to build the base URL ourselves: 56our$path_info=$ENV{"PATH_INFO"}; 57if($path_info) { 58if($my_url=~ s,\Q$path_info\E$,, && 59$my_uri=~ s,\Q$path_info\E$,, && 60defined$ENV{'SCRIPT_NAME'}) { 61$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 62} 63} 64 65# target of the home link on top of all pages 66our$home_link=$my_uri||"/"; 67} 68 69# core git executable to use 70# this can just be "git" if your webserver has a sensible PATH 71our$GIT="++GIT_BINDIR++/git"; 72 73# absolute fs-path which will be prepended to the project path 74#our $projectroot = "/pub/scm"; 75our$projectroot="++GITWEB_PROJECTROOT++"; 76 77# fs traversing limit for getting project list 78# the number is relative to the projectroot 79our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 80 81# string of the home link on top of all pages 82our$home_link_str="++GITWEB_HOME_LINK_STR++"; 83 84# name of your site or organization to appear in page titles 85# replace this with something more descriptive for clearer bookmarks 86our$site_name="++GITWEB_SITENAME++" 87|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 88 89# filename of html text to include at top of each page 90our$site_header="++GITWEB_SITE_HEADER++"; 91# html text to include at home page 92our$home_text="++GITWEB_HOMETEXT++"; 93# filename of html text to include at bottom of each page 94our$site_footer="++GITWEB_SITE_FOOTER++"; 95 96# URI of stylesheets 97our@stylesheets= ("++GITWEB_CSS++"); 98# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 99our$stylesheet=undef; 100# URI of GIT logo (72x27 size) 101our$logo="++GITWEB_LOGO++"; 102# URI of GIT favicon, assumed to be image/png type 103our$favicon="++GITWEB_FAVICON++"; 104# URI of gitweb.js (JavaScript code for gitweb) 105our$javascript="++GITWEB_JS++"; 106 107# URI and label (title) of GIT logo link 108#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 109#our $logo_label = "git documentation"; 110our$logo_url="http://git-scm.com/"; 111our$logo_label="git homepage"; 112 113# source of projects list 114our$projects_list="++GITWEB_LIST++"; 115 116# the width (in characters) of the projects list "Description" column 117our$projects_list_description_width=25; 118 119# default order of projects list 120# valid values are none, project, descr, owner, and age 121our$default_projects_order="project"; 122 123# show repository only if this file exists 124# (only effective if this variable evaluates to true) 125our$export_ok="++GITWEB_EXPORT_OK++"; 126 127# show repository only if this subroutine returns true 128# when given the path to the project, for example: 129# sub { return -e "$_[0]/git-daemon-export-ok"; } 130our$export_auth_hook=undef; 131 132# only allow viewing of repositories also shown on the overview page 133our$strict_export="++GITWEB_STRICT_EXPORT++"; 134 135# list of git base URLs used for URL to where fetch project from, 136# i.e. full URL is "$git_base_url/$project" 137our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 138 139# default blob_plain mimetype and default charset for text/plain blob 140our$default_blob_plain_mimetype='text/plain'; 141our$default_text_plain_charset=undef; 142 143# file to use for guessing MIME types before trying /etc/mime.types 144# (relative to the current git repository) 145our$mimetypes_file=undef; 146 147# assume this charset if line contains non-UTF-8 characters; 148# it should be valid encoding (see Encoding::Supported(3pm) for list), 149# for which encoding all byte sequences are valid, for example 150# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 151# could be even 'utf-8' for the old behavior) 152our$fallback_encoding='latin1'; 153 154# rename detection options for git-diff and git-diff-tree 155# - default is '-M', with the cost proportional to 156# (number of removed files) * (number of new files). 157# - more costly is '-C' (which implies '-M'), with the cost proportional to 158# (number of changed files + number of removed files) * (number of new files) 159# - even more costly is '-C', '--find-copies-harder' with cost 160# (number of files in the original tree) * (number of new files) 161# - one might want to include '-B' option, e.g. '-B', '-M' 162our@diff_opts= ('-M');# taken from git_commit 163 164# Disables features that would allow repository owners to inject script into 165# the gitweb domain. 166our$prevent_xss=0; 167 168# information about snapshot formats that gitweb is capable of serving 169our%known_snapshot_formats= ( 170# name => { 171# 'display' => display name, 172# 'type' => mime type, 173# 'suffix' => filename suffix, 174# 'format' => --format for git-archive, 175# 'compressor' => [compressor command and arguments] 176# (array reference, optional) 177# 'disabled' => boolean (optional)} 178# 179'tgz'=> { 180'display'=>'tar.gz', 181'type'=>'application/x-gzip', 182'suffix'=>'.tar.gz', 183'format'=>'tar', 184'compressor'=> ['gzip']}, 185 186'tbz2'=> { 187'display'=>'tar.bz2', 188'type'=>'application/x-bzip2', 189'suffix'=>'.tar.bz2', 190'format'=>'tar', 191'compressor'=> ['bzip2']}, 192 193'txz'=> { 194'display'=>'tar.xz', 195'type'=>'application/x-xz', 196'suffix'=>'.tar.xz', 197'format'=>'tar', 198'compressor'=> ['xz'], 199'disabled'=>1}, 200 201'zip'=> { 202'display'=>'zip', 203'type'=>'application/x-zip', 204'suffix'=>'.zip', 205'format'=>'zip'}, 206); 207 208# Aliases so we understand old gitweb.snapshot values in repository 209# configuration. 210our%known_snapshot_format_aliases= ( 211'gzip'=>'tgz', 212'bzip2'=>'tbz2', 213'xz'=>'txz', 214 215# backward compatibility: legacy gitweb config support 216'x-gzip'=>undef,'gz'=>undef, 217'x-bzip2'=>undef,'bz2'=>undef, 218'x-zip'=>undef,''=>undef, 219); 220 221# Pixel sizes for icons and avatars. If the default font sizes or lineheights 222# are changed, it may be appropriate to change these values too via 223# $GITWEB_CONFIG. 224our%avatar_size= ( 225'default'=>16, 226'double'=>32 227); 228 229# Used to set the maximum load that we will still respond to gitweb queries. 230# If server load exceed this value then return "503 server busy" error. 231# If gitweb cannot determined server load, it is taken to be 0. 232# Leave it undefined (or set to 'undef') to turn off load checking. 233our$maxload=300; 234 235# You define site-wide feature defaults here; override them with 236# $GITWEB_CONFIG as necessary. 237our%feature= ( 238# feature => { 239# 'sub' => feature-sub (subroutine), 240# 'override' => allow-override (boolean), 241# 'default' => [ default options...] (array reference)} 242# 243# if feature is overridable (it means that allow-override has true value), 244# then feature-sub will be called with default options as parameters; 245# return value of feature-sub indicates if to enable specified feature 246# 247# if there is no 'sub' key (no feature-sub), then feature cannot be 248# overriden 249# 250# use gitweb_get_feature(<feature>) to retrieve the <feature> value 251# (an array) or gitweb_check_feature(<feature>) to check if <feature> 252# is enabled 253 254# Enable the 'blame' blob view, showing the last commit that modified 255# each line in the file. This can be very CPU-intensive. 256 257# To enable system wide have in $GITWEB_CONFIG 258# $feature{'blame'}{'default'} = [1]; 259# To have project specific config enable override in $GITWEB_CONFIG 260# $feature{'blame'}{'override'} = 1; 261# and in project config gitweb.blame = 0|1; 262'blame'=> { 263'sub'=>sub{ feature_bool('blame',@_) }, 264'override'=>0, 265'default'=> [0]}, 266 267# Enable the 'snapshot' link, providing a compressed archive of any 268# tree. This can potentially generate high traffic if you have large 269# project. 270 271# Value is a list of formats defined in %known_snapshot_formats that 272# you wish to offer. 273# To disable system wide have in $GITWEB_CONFIG 274# $feature{'snapshot'}{'default'} = []; 275# To have project specific config enable override in $GITWEB_CONFIG 276# $feature{'snapshot'}{'override'} = 1; 277# and in project config, a comma-separated list of formats or "none" 278# to disable. Example: gitweb.snapshot = tbz2,zip; 279'snapshot'=> { 280'sub'=> \&feature_snapshot, 281'override'=>0, 282'default'=> ['tgz']}, 283 284# Enable text search, which will list the commits which match author, 285# committer or commit text to a given string. Enabled by default. 286# Project specific override is not supported. 287'search'=> { 288'override'=>0, 289'default'=> [1]}, 290 291# Enable grep search, which will list the files in currently selected 292# tree containing the given string. Enabled by default. This can be 293# potentially CPU-intensive, of course. 294 295# To enable system wide have in $GITWEB_CONFIG 296# $feature{'grep'}{'default'} = [1]; 297# To have project specific config enable override in $GITWEB_CONFIG 298# $feature{'grep'}{'override'} = 1; 299# and in project config gitweb.grep = 0|1; 300'grep'=> { 301'sub'=>sub{ feature_bool('grep',@_) }, 302'override'=>0, 303'default'=> [1]}, 304 305# Enable the pickaxe search, which will list the commits that modified 306# a given string in a file. This can be practical and quite faster 307# alternative to 'blame', but still potentially CPU-intensive. 308 309# To enable system wide have in $GITWEB_CONFIG 310# $feature{'pickaxe'}{'default'} = [1]; 311# To have project specific config enable override in $GITWEB_CONFIG 312# $feature{'pickaxe'}{'override'} = 1; 313# and in project config gitweb.pickaxe = 0|1; 314'pickaxe'=> { 315'sub'=>sub{ feature_bool('pickaxe',@_) }, 316'override'=>0, 317'default'=> [1]}, 318 319# Enable showing size of blobs in a 'tree' view, in a separate 320# column, similar to what 'ls -l' does. This cost a bit of IO. 321 322# To disable system wide have in $GITWEB_CONFIG 323# $feature{'show-sizes'}{'default'} = [0]; 324# To have project specific config enable override in $GITWEB_CONFIG 325# $feature{'show-sizes'}{'override'} = 1; 326# and in project config gitweb.showsizes = 0|1; 327'show-sizes'=> { 328'sub'=>sub{ feature_bool('showsizes',@_) }, 329'override'=>0, 330'default'=> [1]}, 331 332# Make gitweb use an alternative format of the URLs which can be 333# more readable and natural-looking: project name is embedded 334# directly in the path and the query string contains other 335# auxiliary information. All gitweb installations recognize 336# URL in either format; this configures in which formats gitweb 337# generates links. 338 339# To enable system wide have in $GITWEB_CONFIG 340# $feature{'pathinfo'}{'default'} = [1]; 341# Project specific override is not supported. 342 343# Note that you will need to change the default location of CSS, 344# favicon, logo and possibly other files to an absolute URL. Also, 345# if gitweb.cgi serves as your indexfile, you will need to force 346# $my_uri to contain the script name in your $GITWEB_CONFIG. 347'pathinfo'=> { 348'override'=>0, 349'default'=> [0]}, 350 351# Make gitweb consider projects in project root subdirectories 352# to be forks of existing projects. Given project $projname.git, 353# projects matching $projname/*.git will not be shown in the main 354# projects list, instead a '+' mark will be added to $projname 355# there and a 'forks' view will be enabled for the project, listing 356# all the forks. If project list is taken from a file, forks have 357# to be listed after the main project. 358 359# To enable system wide have in $GITWEB_CONFIG 360# $feature{'forks'}{'default'} = [1]; 361# Project specific override is not supported. 362'forks'=> { 363'override'=>0, 364'default'=> [0]}, 365 366# Insert custom links to the action bar of all project pages. 367# This enables you mainly to link to third-party scripts integrating 368# into gitweb; e.g. git-browser for graphical history representation 369# or custom web-based repository administration interface. 370 371# The 'default' value consists of a list of triplets in the form 372# (label, link, position) where position is the label after which 373# to insert the link and link is a format string where %n expands 374# to the project name, %f to the project path within the filesystem, 375# %h to the current hash (h gitweb parameter) and %b to the current 376# hash base (hb gitweb parameter); %% expands to %. 377 378# To enable system wide have in $GITWEB_CONFIG e.g. 379# $feature{'actions'}{'default'} = [('graphiclog', 380# '/git-browser/by-commit.html?r=%n', 'summary')]; 381# Project specific override is not supported. 382'actions'=> { 383'override'=>0, 384'default'=> []}, 385 386# Allow gitweb scan project content tags described in ctags/ 387# of project repository, and display the popular Web 2.0-ish 388# "tag cloud" near the project list. Note that this is something 389# COMPLETELY different from the normal Git tags. 390 391# gitweb by itself can show existing tags, but it does not handle 392# tagging itself; you need an external application for that. 393# For an example script, check Girocco's cgi/tagproj.cgi. 394# You may want to install the HTML::TagCloud Perl module to get 395# a pretty tag cloud instead of just a list of tags. 396 397# To enable system wide have in $GITWEB_CONFIG 398# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 399# Project specific override is not supported. 400'ctags'=> { 401'override'=>0, 402'default'=> [0]}, 403 404# The maximum number of patches in a patchset generated in patch 405# view. Set this to 0 or undef to disable patch view, or to a 406# negative number to remove any limit. 407 408# To disable system wide have in $GITWEB_CONFIG 409# $feature{'patches'}{'default'} = [0]; 410# To have project specific config enable override in $GITWEB_CONFIG 411# $feature{'patches'}{'override'} = 1; 412# and in project config gitweb.patches = 0|n; 413# where n is the maximum number of patches allowed in a patchset. 414'patches'=> { 415'sub'=> \&feature_patches, 416'override'=>0, 417'default'=> [16]}, 418 419# Avatar support. When this feature is enabled, views such as 420# shortlog or commit will display an avatar associated with 421# the email of the committer(s) and/or author(s). 422 423# Currently available providers are gravatar and picon. 424# If an unknown provider is specified, the feature is disabled. 425 426# Gravatar depends on Digest::MD5. 427# Picon currently relies on the indiana.edu database. 428 429# To enable system wide have in $GITWEB_CONFIG 430# $feature{'avatar'}{'default'} = ['<provider>']; 431# where <provider> is either gravatar or picon. 432# To have project specific config enable override in $GITWEB_CONFIG 433# $feature{'avatar'}{'override'} = 1; 434# and in project config gitweb.avatar = <provider>; 435'avatar'=> { 436'sub'=> \&feature_avatar, 437'override'=>0, 438'default'=> ['']}, 439 440# Enable displaying how much time and how many git commands 441# it took to generate and display page. Disabled by default. 442# Project specific override is not supported. 443'timed'=> { 444'override'=>0, 445'default'=> [0]}, 446 447# Enable turning some links into links to actions which require 448# JavaScript to run (like 'blame_incremental'). Not enabled by 449# default. Project specific override is currently not supported. 450'javascript-actions'=> { 451'override'=>0, 452'default'=> [0]}, 453); 454 455sub gitweb_get_feature { 456my($name) =@_; 457return unlessexists$feature{$name}; 458my($sub,$override,@defaults) = ( 459$feature{$name}{'sub'}, 460$feature{$name}{'override'}, 461@{$feature{$name}{'default'}}); 462# project specific override is possible only if we have project 463our$git_dir;# global variable, declared later 464if(!$override|| !defined$git_dir) { 465return@defaults; 466} 467if(!defined$sub) { 468warn"feature$nameis not overridable"; 469return@defaults; 470} 471return$sub->(@defaults); 472} 473 474# A wrapper to check if a given feature is enabled. 475# With this, you can say 476# 477# my $bool_feat = gitweb_check_feature('bool_feat'); 478# gitweb_check_feature('bool_feat') or somecode; 479# 480# instead of 481# 482# my ($bool_feat) = gitweb_get_feature('bool_feat'); 483# (gitweb_get_feature('bool_feat'))[0] or somecode; 484# 485sub gitweb_check_feature { 486return(gitweb_get_feature(@_))[0]; 487} 488 489 490sub feature_bool { 491my$key=shift; 492my($val) = git_get_project_config($key,'--bool'); 493 494if(!defined$val) { 495return($_[0]); 496}elsif($valeq'true') { 497return(1); 498}elsif($valeq'false') { 499return(0); 500} 501} 502 503sub feature_snapshot { 504my(@fmts) =@_; 505 506my($val) = git_get_project_config('snapshot'); 507 508if($val) { 509@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 510} 511 512return@fmts; 513} 514 515sub feature_patches { 516my@val= (git_get_project_config('patches','--int')); 517 518if(@val) { 519return@val; 520} 521 522return($_[0]); 523} 524 525sub feature_avatar { 526my@val= (git_get_project_config('avatar')); 527 528return@val?@val:@_; 529} 530 531# checking HEAD file with -e is fragile if the repository was 532# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 533# and then pruned. 534sub check_head_link { 535my($dir) =@_; 536my$headfile="$dir/HEAD"; 537return((-e $headfile) || 538(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 539} 540 541sub check_export_ok { 542my($dir) =@_; 543return(check_head_link($dir) && 544(!$export_ok|| -e "$dir/$export_ok") && 545(!$export_auth_hook||$export_auth_hook->($dir))); 546} 547 548# process alternate names for backward compatibility 549# filter out unsupported (unknown) snapshot formats 550sub filter_snapshot_fmts { 551my@fmts=@_; 552 553@fmts=map{ 554exists$known_snapshot_format_aliases{$_} ? 555$known_snapshot_format_aliases{$_} :$_}@fmts; 556@fmts=grep{ 557exists$known_snapshot_formats{$_} && 558!$known_snapshot_formats{$_}{'disabled'}}@fmts; 559} 560 561our($GITWEB_CONFIG,$GITWEB_CONFIG_SYSTEM); 562sub evaluate_gitweb_config { 563our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 564our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 565# die if there are errors parsing config file 566if(-e $GITWEB_CONFIG) { 567do$GITWEB_CONFIG; 568die$@if$@; 569}elsif(-e $GITWEB_CONFIG_SYSTEM) { 570do$GITWEB_CONFIG_SYSTEM; 571die$@if$@; 572} 573} 574 575# Get loadavg of system, to compare against $maxload. 576# Currently it requires '/proc/loadavg' present to get loadavg; 577# if it is not present it returns 0, which means no load checking. 578sub get_loadavg { 579if( -e '/proc/loadavg'){ 580open my$fd,'<','/proc/loadavg' 581orreturn0; 582my@load=split(/\s+/,scalar<$fd>); 583close$fd; 584 585# The first three columns measure CPU and IO utilization of the last one, 586# five, and 10 minute periods. The fourth column shows the number of 587# currently running processes and the total number of processes in the m/n 588# format. The last column displays the last process ID used. 589return$load[0] ||0; 590} 591# additional checks for load average should go here for things that don't export 592# /proc/loadavg 593 594return0; 595} 596 597# version of the core git binary 598our$git_version; 599sub evaluate_git_version { 600our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 601$number_of_git_cmds++; 602} 603 604sub check_loadavg { 605if(defined$maxload&& get_loadavg() >$maxload) { 606 die_error(503,"The load average on the server is too high"); 607} 608} 609 610# ====================================================================== 611# input validation and dispatch 612 613# input parameters can be collected from a variety of sources (presently, CGI 614# and PATH_INFO), so we define an %input_params hash that collects them all 615# together during validation: this allows subsequent uses (e.g. href()) to be 616# agnostic of the parameter origin 617 618our%input_params= (); 619 620# input parameters are stored with the long parameter name as key. This will 621# also be used in the href subroutine to convert parameters to their CGI 622# equivalent, and since the href() usage is the most frequent one, we store 623# the name -> CGI key mapping here, instead of the reverse. 624# 625# XXX: Warning: If you touch this, check the search form for updating, 626# too. 627 628our@cgi_param_mapping= ( 629 project =>"p", 630 action =>"a", 631 file_name =>"f", 632 file_parent =>"fp", 633 hash =>"h", 634 hash_parent =>"hp", 635 hash_base =>"hb", 636 hash_parent_base =>"hpb", 637 page =>"pg", 638 order =>"o", 639 searchtext =>"s", 640 searchtype =>"st", 641 snapshot_format =>"sf", 642 extra_options =>"opt", 643 search_use_regexp =>"sr", 644# this must be last entry (for manipulation from JavaScript) 645 javascript =>"js" 646); 647our%cgi_param_mapping=@cgi_param_mapping; 648 649# we will also need to know the possible actions, for validation 650our%actions= ( 651"blame"=> \&git_blame, 652"blame_incremental"=> \&git_blame_incremental, 653"blame_data"=> \&git_blame_data, 654"blobdiff"=> \&git_blobdiff, 655"blobdiff_plain"=> \&git_blobdiff_plain, 656"blob"=> \&git_blob, 657"blob_plain"=> \&git_blob_plain, 658"commitdiff"=> \&git_commitdiff, 659"commitdiff_plain"=> \&git_commitdiff_plain, 660"commit"=> \&git_commit, 661"forks"=> \&git_forks, 662"heads"=> \&git_heads, 663"history"=> \&git_history, 664"log"=> \&git_log, 665"patch"=> \&git_patch, 666"patches"=> \&git_patches, 667"rss"=> \&git_rss, 668"atom"=> \&git_atom, 669"search"=> \&git_search, 670"search_help"=> \&git_search_help, 671"shortlog"=> \&git_shortlog, 672"summary"=> \&git_summary, 673"tag"=> \&git_tag, 674"tags"=> \&git_tags, 675"tree"=> \&git_tree, 676"snapshot"=> \&git_snapshot, 677"object"=> \&git_object, 678# those below don't need $project 679"opml"=> \&git_opml, 680"project_list"=> \&git_project_list, 681"project_index"=> \&git_project_index, 682); 683 684# finally, we have the hash of allowed extra_options for the commands that 685# allow them 686our%allowed_options= ( 687"--no-merges"=> [qw(rss atom log shortlog history)], 688); 689 690# fill %input_params with the CGI parameters. All values except for 'opt' 691# should be single values, but opt can be an array. We should probably 692# build an array of parameters that can be multi-valued, but since for the time 693# being it's only this one, we just single it out 694sub evaluate_query_params { 695our$cgi; 696 697while(my($name,$symbol) =each%cgi_param_mapping) { 698if($symboleq'opt') { 699$input_params{$name} = [$cgi->param($symbol) ]; 700}else{ 701$input_params{$name} =$cgi->param($symbol); 702} 703} 704} 705 706# now read PATH_INFO and update the parameter list for missing parameters 707sub evaluate_path_info { 708return ifdefined$input_params{'project'}; 709return if!$path_info; 710$path_info=~ s,^/+,,; 711return if!$path_info; 712 713# find which part of PATH_INFO is project 714my$project=$path_info; 715$project=~ s,/+$,,; 716while($project&& !check_head_link("$projectroot/$project")) { 717$project=~ s,/*[^/]*$,,; 718} 719return unless$project; 720$input_params{'project'} =$project; 721 722# do not change any parameters if an action is given using the query string 723return if$input_params{'action'}; 724$path_info=~ s,^\Q$project\E/*,,; 725 726# next, check if we have an action 727my$action=$path_info; 728$action=~ s,/.*$,,; 729if(exists$actions{$action}) { 730$path_info=~ s,^$action/*,,; 731$input_params{'action'} =$action; 732} 733 734# list of actions that want hash_base instead of hash, but can have no 735# pathname (f) parameter 736my@wants_base= ( 737'tree', 738'history', 739); 740 741# we want to catch 742# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 743my($parentrefname,$parentpathname,$refname,$pathname) = 744($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 745 746# first, analyze the 'current' part 747if(defined$pathname) { 748# we got "branch:filename" or "branch:dir/" 749# we could use git_get_type(branch:pathname), but: 750# - it needs $git_dir 751# - it does a git() call 752# - the convention of terminating directories with a slash 753# makes it superfluous 754# - embedding the action in the PATH_INFO would make it even 755# more superfluous 756$pathname=~ s,^/+,,; 757if(!$pathname||substr($pathname, -1)eq"/") { 758$input_params{'action'} ||="tree"; 759$pathname=~ s,/$,,; 760}else{ 761# the default action depends on whether we had parent info 762# or not 763if($parentrefname) { 764$input_params{'action'} ||="blobdiff_plain"; 765}else{ 766$input_params{'action'} ||="blob_plain"; 767} 768} 769$input_params{'hash_base'} ||=$refname; 770$input_params{'file_name'} ||=$pathname; 771}elsif(defined$refname) { 772# we got "branch". In this case we have to choose if we have to 773# set hash or hash_base. 774# 775# Most of the actions without a pathname only want hash to be 776# set, except for the ones specified in @wants_base that want 777# hash_base instead. It should also be noted that hand-crafted 778# links having 'history' as an action and no pathname or hash 779# set will fail, but that happens regardless of PATH_INFO. 780$input_params{'action'} ||="shortlog"; 781if(grep{$_eq$input_params{'action'} }@wants_base) { 782$input_params{'hash_base'} ||=$refname; 783}else{ 784$input_params{'hash'} ||=$refname; 785} 786} 787 788# next, handle the 'parent' part, if present 789if(defined$parentrefname) { 790# a missing pathspec defaults to the 'current' filename, allowing e.g. 791# someproject/blobdiff/oldrev..newrev:/filename 792if($parentpathname) { 793$parentpathname=~ s,^/+,,; 794$parentpathname=~ s,/$,,; 795$input_params{'file_parent'} ||=$parentpathname; 796}else{ 797$input_params{'file_parent'} ||=$input_params{'file_name'}; 798} 799# we assume that hash_parent_base is wanted if a path was specified, 800# or if the action wants hash_base instead of hash 801if(defined$input_params{'file_parent'} || 802grep{$_eq$input_params{'action'} }@wants_base) { 803$input_params{'hash_parent_base'} ||=$parentrefname; 804}else{ 805$input_params{'hash_parent'} ||=$parentrefname; 806} 807} 808 809# for the snapshot action, we allow URLs in the form 810# $project/snapshot/$hash.ext 811# where .ext determines the snapshot and gets removed from the 812# passed $refname to provide the $hash. 813# 814# To be able to tell that $refname includes the format extension, we 815# require the following two conditions to be satisfied: 816# - the hash input parameter MUST have been set from the $refname part 817# of the URL (i.e. they must be equal) 818# - the snapshot format MUST NOT have been defined already (e.g. from 819# CGI parameter sf) 820# It's also useless to try any matching unless $refname has a dot, 821# so we check for that too 822if(defined$input_params{'action'} && 823$input_params{'action'}eq'snapshot'&& 824defined$refname&&index($refname,'.') != -1&& 825$refnameeq$input_params{'hash'} && 826!defined$input_params{'snapshot_format'}) { 827# We loop over the known snapshot formats, checking for 828# extensions. Allowed extensions are both the defined suffix 829# (which includes the initial dot already) and the snapshot 830# format key itself, with a prepended dot 831while(my($fmt,$opt) =each%known_snapshot_formats) { 832my$hash=$refname; 833unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 834next; 835} 836my$sfx=$1; 837# a valid suffix was found, so set the snapshot format 838# and reset the hash parameter 839$input_params{'snapshot_format'} =$fmt; 840$input_params{'hash'} =$hash; 841# we also set the format suffix to the one requested 842# in the URL: this way a request for e.g. .tgz returns 843# a .tgz instead of a .tar.gz 844$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 845last; 846} 847} 848} 849 850our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 851$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 852$searchtext,$search_regexp); 853sub evaluate_and_validate_params { 854our$action=$input_params{'action'}; 855if(defined$action) { 856if(!validate_action($action)) { 857 die_error(400,"Invalid action parameter"); 858} 859} 860 861# parameters which are pathnames 862our$project=$input_params{'project'}; 863if(defined$project) { 864if(!validate_project($project)) { 865undef$project; 866 die_error(404,"No such project"); 867} 868} 869 870our$file_name=$input_params{'file_name'}; 871if(defined$file_name) { 872if(!validate_pathname($file_name)) { 873 die_error(400,"Invalid file parameter"); 874} 875} 876 877our$file_parent=$input_params{'file_parent'}; 878if(defined$file_parent) { 879if(!validate_pathname($file_parent)) { 880 die_error(400,"Invalid file parent parameter"); 881} 882} 883 884# parameters which are refnames 885our$hash=$input_params{'hash'}; 886if(defined$hash) { 887if(!validate_refname($hash)) { 888 die_error(400,"Invalid hash parameter"); 889} 890} 891 892our$hash_parent=$input_params{'hash_parent'}; 893if(defined$hash_parent) { 894if(!validate_refname($hash_parent)) { 895 die_error(400,"Invalid hash parent parameter"); 896} 897} 898 899our$hash_base=$input_params{'hash_base'}; 900if(defined$hash_base) { 901if(!validate_refname($hash_base)) { 902 die_error(400,"Invalid hash base parameter"); 903} 904} 905 906our@extra_options= @{$input_params{'extra_options'}}; 907# @extra_options is always defined, since it can only be (currently) set from 908# CGI, and $cgi->param() returns the empty array in array context if the param 909# is not set 910foreachmy$opt(@extra_options) { 911if(not exists$allowed_options{$opt}) { 912 die_error(400,"Invalid option parameter"); 913} 914if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 915 die_error(400,"Invalid option parameter for this action"); 916} 917} 918 919our$hash_parent_base=$input_params{'hash_parent_base'}; 920if(defined$hash_parent_base) { 921if(!validate_refname($hash_parent_base)) { 922 die_error(400,"Invalid hash parent base parameter"); 923} 924} 925 926# other parameters 927our$page=$input_params{'page'}; 928if(defined$page) { 929if($page=~m/[^0-9]/) { 930 die_error(400,"Invalid page parameter"); 931} 932} 933 934our$searchtype=$input_params{'searchtype'}; 935if(defined$searchtype) { 936if($searchtype=~m/[^a-z]/) { 937 die_error(400,"Invalid searchtype parameter"); 938} 939} 940 941our$search_use_regexp=$input_params{'search_use_regexp'}; 942 943our$searchtext=$input_params{'searchtext'}; 944our$search_regexp; 945if(defined$searchtext) { 946if(length($searchtext) <2) { 947 die_error(403,"At least two characters are required for search parameter"); 948} 949$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 950} 951} 952 953# path to the current git repository 954our$git_dir; 955sub evaluate_git_dir { 956our$git_dir="$projectroot/$project"if$project; 957} 958 959our(@snapshot_fmts,$git_avatar); 960sub configure_gitweb_features { 961# list of supported snapshot formats 962our@snapshot_fmts= gitweb_get_feature('snapshot'); 963@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 964 965# check that the avatar feature is set to a known provider name, 966# and for each provider check if the dependencies are satisfied. 967# if the provider name is invalid or the dependencies are not met, 968# reset $git_avatar to the empty string. 969our($git_avatar) = gitweb_get_feature('avatar'); 970if($git_avatareq'gravatar') { 971$git_avatar=''unless(eval{require Digest::MD5;1; }); 972}elsif($git_avatareq'picon') { 973# no dependencies 974}else{ 975$git_avatar=''; 976} 977} 978 979# dispatch 980sub dispatch { 981if(!defined$action) { 982if(defined$hash) { 983$action= git_get_type($hash); 984}elsif(defined$hash_base&&defined$file_name) { 985$action= git_get_type("$hash_base:$file_name"); 986}elsif(defined$project) { 987$action='summary'; 988}else{ 989$action='project_list'; 990} 991} 992if(!defined($actions{$action})) { 993 die_error(400,"Unknown action"); 994} 995if($action!~m/^(?:opml|project_list|project_index)$/&& 996!$project) { 997 die_error(400,"Project needed"); 998} 999$actions{$action}->();1000}10011002sub run_request {1003our$t0= [Time::HiRes::gettimeofday()]1004ifdefined$t0;10051006 evaluate_uri();1007 evaluate_gitweb_config();1008 evaluate_git_version();1009 check_loadavg();10101011# $projectroot and $projects_list might be set in gitweb config file1012$projects_list||=$projectroot;10131014 evaluate_query_params();1015 evaluate_path_info();1016 evaluate_and_validate_params();1017 evaluate_git_dir();10181019 configure_gitweb_features();10201021 dispatch();1022}10231024our$is_last_request=sub{1};1025our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1026our$CGI='CGI';1027our$cgi;1028sub evaluate_argv {1029return unless(@ARGV);10301031require Getopt::Long;1032 Getopt::Long::GetOptions(1033'fastcgi|fcgi|f'=>sub{1034require CGI::Fast;1035our$CGI='CGI::Fast';10361037my$request_number=0;1038# let each child service 100 requests1039our$is_last_request=sub{ ++$request_number>100};1040},1041'nproc|n=i'=>sub{1042my($arg,$val) =@_;1043return unlesseval{require FCGI::ProcManager;1; };1044my$proc_manager= FCGI::ProcManager->new({1045 n_processes =>$val,1046});1047our$pre_listen_hook=sub{$proc_manager->pm_manage() };1048our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1049our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1050},1051);1052}10531054sub run {1055 evaluate_argv();10561057$pre_listen_hook->()1058if$pre_listen_hook;10591060 REQUEST:1061while($cgi=$CGI->new()) {1062$pre_dispatch_hook->()1063if$pre_dispatch_hook;10641065 run_request();10661067$pre_dispatch_hook->()1068if$post_dispatch_hook;10691070last REQUEST if($is_last_request->());1071}10721073 DONE_GITWEB:10741;1075}10761077run();10781079## ======================================================================1080## action links10811082# possible values of extra options1083# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1084# -replay => 1 - start from a current view (replay with modifications)1085# -path_info => 0|1 - don't use/use path_info URL (if possible)1086sub href {1087my%params=@_;1088# default is to use -absolute url() i.e. $my_uri1089my$href=$params{-full} ?$my_url:$my_uri;10901091$params{'project'} =$projectunlessexists$params{'project'};10921093if($params{-replay}) {1094while(my($name,$symbol) =each%cgi_param_mapping) {1095if(!exists$params{$name}) {1096$params{$name} =$input_params{$name};1097}1098}1099}11001101my$use_pathinfo= gitweb_check_feature('pathinfo');1102if(defined$params{'project'} &&1103(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1104# try to put as many parameters as possible in PATH_INFO:1105# - project name1106# - action1107# - hash_parent or hash_parent_base:/file_parent1108# - hash or hash_base:/filename1109# - the snapshot_format as an appropriate suffix11101111# When the script is the root DirectoryIndex for the domain,1112# $href here would be something like http://gitweb.example.com/1113# Thus, we strip any trailing / from $href, to spare us double1114# slashes in the final URL1115$href=~ s,/$,,;11161117# Then add the project name, if present1118$href.="/".esc_url($params{'project'});1119delete$params{'project'};11201121# since we destructively absorb parameters, we keep this1122# boolean that remembers if we're handling a snapshot1123my$is_snapshot=$params{'action'}eq'snapshot';11241125# Summary just uses the project path URL, any other action is1126# added to the URL1127if(defined$params{'action'}) {1128$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1129delete$params{'action'};1130}11311132# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1133# stripping nonexistent or useless pieces1134$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1135||$params{'hash_parent'} ||$params{'hash'});1136if(defined$params{'hash_base'}) {1137if(defined$params{'hash_parent_base'}) {1138$href.= esc_url($params{'hash_parent_base'});1139# skip the file_parent if it's the same as the file_name1140if(defined$params{'file_parent'}) {1141if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1142delete$params{'file_parent'};1143}elsif($params{'file_parent'} !~/\.\./) {1144$href.=":/".esc_url($params{'file_parent'});1145delete$params{'file_parent'};1146}1147}1148$href.="..";1149delete$params{'hash_parent'};1150delete$params{'hash_parent_base'};1151}elsif(defined$params{'hash_parent'}) {1152$href.= esc_url($params{'hash_parent'})."..";1153delete$params{'hash_parent'};1154}11551156$href.= esc_url($params{'hash_base'});1157if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1158$href.=":/".esc_url($params{'file_name'});1159delete$params{'file_name'};1160}1161delete$params{'hash'};1162delete$params{'hash_base'};1163}elsif(defined$params{'hash'}) {1164$href.= esc_url($params{'hash'});1165delete$params{'hash'};1166}11671168# If the action was a snapshot, we can absorb the1169# snapshot_format parameter too1170if($is_snapshot) {1171my$fmt=$params{'snapshot_format'};1172# snapshot_format should always be defined when href()1173# is called, but just in case some code forgets, we1174# fall back to the default1175$fmt||=$snapshot_fmts[0];1176$href.=$known_snapshot_formats{$fmt}{'suffix'};1177delete$params{'snapshot_format'};1178}1179}11801181# now encode the parameters explicitly1182my@result= ();1183for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1184my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1185if(defined$params{$name}) {1186if(ref($params{$name})eq"ARRAY") {1187foreachmy$par(@{$params{$name}}) {1188push@result,$symbol."=". esc_param($par);1189}1190}else{1191push@result,$symbol."=". esc_param($params{$name});1192}1193}1194}1195$href.="?".join(';',@result)ifscalar@result;11961197return$href;1198}119912001201## ======================================================================1202## validation, quoting/unquoting and escaping12031204sub validate_action {1205my$input=shift||returnundef;1206returnundefunlessexists$actions{$input};1207return$input;1208}12091210sub validate_project {1211my$input=shift||returnundef;1212if(!validate_pathname($input) ||1213!(-d "$projectroot/$input") ||1214!check_export_ok("$projectroot/$input") ||1215($strict_export&& !project_in_list($input))) {1216returnundef;1217}else{1218return$input;1219}1220}12211222sub validate_pathname {1223my$input=shift||returnundef;12241225# no '.' or '..' as elements of path, i.e. no '.' nor '..'1226# at the beginning, at the end, and between slashes.1227# also this catches doubled slashes1228if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1229returnundef;1230}1231# no null characters1232if($input=~m!\0!) {1233returnundef;1234}1235return$input;1236}12371238sub validate_refname {1239my$input=shift||returnundef;12401241# textual hashes are O.K.1242if($input=~m/^[0-9a-fA-F]{40}$/) {1243return$input;1244}1245# it must be correct pathname1246$input= validate_pathname($input)1247orreturnundef;1248# restrictions on ref name according to git-check-ref-format1249if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1250returnundef;1251}1252return$input;1253}12541255# decode sequences of octets in utf8 into Perl's internal form,1256# which is utf-8 with utf8 flag set if needed. gitweb writes out1257# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1258sub to_utf8 {1259my$str=shift;1260returnundefunlessdefined$str;1261if(utf8::valid($str)) {1262 utf8::decode($str);1263return$str;1264}else{1265return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1266}1267}12681269# quote unsafe chars, but keep the slash, even when it's not1270# correct, but quoted slashes look too horrible in bookmarks1271sub esc_param {1272my$str=shift;1273returnundefunlessdefined$str;1274$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1275$str=~s/ /\+/g;1276return$str;1277}12781279# quote unsafe chars in whole URL, so some charactrs cannot be quoted1280sub esc_url {1281my$str=shift;1282returnundefunlessdefined$str;1283$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1284$str=~s/\+/%2B/g;1285$str=~s/ /\+/g;1286return$str;1287}12881289# replace invalid utf8 character with SUBSTITUTION sequence1290sub esc_html {1291my$str=shift;1292my%opts=@_;12931294returnundefunlessdefined$str;12951296$str= to_utf8($str);1297$str=$cgi->escapeHTML($str);1298if($opts{'-nbsp'}) {1299$str=~s/ / /g;1300}1301$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1302return$str;1303}13041305# quote control characters and escape filename to HTML1306sub esc_path {1307my$str=shift;1308my%opts=@_;13091310returnundefunlessdefined$str;13111312$str= to_utf8($str);1313$str=$cgi->escapeHTML($str);1314if($opts{'-nbsp'}) {1315$str=~s/ / /g;1316}1317$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1318return$str;1319}13201321# Make control characters "printable", using character escape codes (CEC)1322sub quot_cec {1323my$cntrl=shift;1324my%opts=@_;1325my%es= (# character escape codes, aka escape sequences1326"\t"=>'\t',# tab (HT)1327"\n"=>'\n',# line feed (LF)1328"\r"=>'\r',# carrige return (CR)1329"\f"=>'\f',# form feed (FF)1330"\b"=>'\b',# backspace (BS)1331"\a"=>'\a',# alarm (bell) (BEL)1332"\e"=>'\e',# escape (ESC)1333"\013"=>'\v',# vertical tab (VT)1334"\000"=>'\0',# nul character (NUL)1335);1336my$chr= ( (exists$es{$cntrl})1337?$es{$cntrl}1338:sprintf('\%2x',ord($cntrl)) );1339if($opts{-nohtml}) {1340return$chr;1341}else{1342return"<span class=\"cntrl\">$chr</span>";1343}1344}13451346# Alternatively use unicode control pictures codepoints,1347# Unicode "printable representation" (PR)1348sub quot_upr {1349my$cntrl=shift;1350my%opts=@_;13511352my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1353if($opts{-nohtml}) {1354return$chr;1355}else{1356return"<span class=\"cntrl\">$chr</span>";1357}1358}13591360# git may return quoted and escaped filenames1361sub unquote {1362my$str=shift;13631364sub unq {1365my$seq=shift;1366my%es= (# character escape codes, aka escape sequences1367't'=>"\t",# tab (HT, TAB)1368'n'=>"\n",# newline (NL)1369'r'=>"\r",# return (CR)1370'f'=>"\f",# form feed (FF)1371'b'=>"\b",# backspace (BS)1372'a'=>"\a",# alarm (bell) (BEL)1373'e'=>"\e",# escape (ESC)1374'v'=>"\013",# vertical tab (VT)1375);13761377if($seq=~m/^[0-7]{1,3}$/) {1378# octal char sequence1379returnchr(oct($seq));1380}elsif(exists$es{$seq}) {1381# C escape sequence, aka character escape code1382return$es{$seq};1383}1384# quoted ordinary character1385return$seq;1386}13871388if($str=~m/^"(.*)"$/) {1389# needs unquoting1390$str=$1;1391$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1392}1393return$str;1394}13951396# escape tabs (convert tabs to spaces)1397sub untabify {1398my$line=shift;13991400while((my$pos=index($line,"\t")) != -1) {1401if(my$count= (8- ($pos%8))) {1402my$spaces=' ' x $count;1403$line=~s/\t/$spaces/;1404}1405}14061407return$line;1408}14091410sub project_in_list {1411my$project=shift;1412my@list= git_get_projects_list();1413return@list&&scalar(grep{$_->{'path'}eq$project}@list);1414}14151416## ----------------------------------------------------------------------1417## HTML aware string manipulation14181419# Try to chop given string on a word boundary between position1420# $len and $len+$add_len. If there is no word boundary there,1421# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1422# (marking chopped part) would be longer than given string.1423sub chop_str {1424my$str=shift;1425my$len=shift;1426my$add_len=shift||10;1427my$where=shift||'right';# 'left' | 'center' | 'right'14281429# Make sure perl knows it is utf8 encoded so we don't1430# cut in the middle of a utf8 multibyte char.1431$str= to_utf8($str);14321433# allow only $len chars, but don't cut a word if it would fit in $add_len1434# if it doesn't fit, cut it if it's still longer than the dots we would add1435# remove chopped character entities entirely14361437# when chopping in the middle, distribute $len into left and right part1438# return early if chopping wouldn't make string shorter1439if($whereeq'center') {1440return$strif($len+5>=length($str));# filler is length 51441$len=int($len/2);1442}else{1443return$strif($len+4>=length($str));# filler is length 41444}14451446# regexps: ending and beginning with word part up to $add_len1447my$endre=qr/.{$len}\w{0,$add_len}/;1448my$begre=qr/\w{0,$add_len}.{$len}/;14491450if($whereeq'left') {1451$str=~m/^(.*?)($begre)$/;1452my($lead,$body) = ($1,$2);1453if(length($lead) >4) {1454$lead=" ...";1455}1456return"$lead$body";14571458}elsif($whereeq'center') {1459$str=~m/^($endre)(.*)$/;1460my($left,$str) = ($1,$2);1461$str=~m/^(.*?)($begre)$/;1462my($mid,$right) = ($1,$2);1463if(length($mid) >5) {1464$mid=" ... ";1465}1466return"$left$mid$right";14671468}else{1469$str=~m/^($endre)(.*)$/;1470my$body=$1;1471my$tail=$2;1472if(length($tail) >4) {1473$tail="... ";1474}1475return"$body$tail";1476}1477}14781479# takes the same arguments as chop_str, but also wraps a <span> around the1480# result with a title attribute if it does get chopped. Additionally, the1481# string is HTML-escaped.1482sub chop_and_escape_str {1483my($str) =@_;14841485my$chopped= chop_str(@_);1486if($choppedeq$str) {1487return esc_html($chopped);1488}else{1489$str=~s/[[:cntrl:]]/?/g;1490return$cgi->span({-title=>$str}, esc_html($chopped));1491}1492}14931494## ----------------------------------------------------------------------1495## functions returning short strings14961497# CSS class for given age value (in seconds)1498sub age_class {1499my$age=shift;15001501if(!defined$age) {1502return"noage";1503}elsif($age<60*60*2) {1504return"age0";1505}elsif($age<60*60*24*2) {1506return"age1";1507}else{1508return"age2";1509}1510}15111512# convert age in seconds to "nn units ago" string1513sub age_string {1514my$age=shift;1515my$age_str;15161517if($age>60*60*24*365*2) {1518$age_str= (int$age/60/60/24/365);1519$age_str.=" years ago";1520}elsif($age>60*60*24*(365/12)*2) {1521$age_str=int$age/60/60/24/(365/12);1522$age_str.=" months ago";1523}elsif($age>60*60*24*7*2) {1524$age_str=int$age/60/60/24/7;1525$age_str.=" weeks ago";1526}elsif($age>60*60*24*2) {1527$age_str=int$age/60/60/24;1528$age_str.=" days ago";1529}elsif($age>60*60*2) {1530$age_str=int$age/60/60;1531$age_str.=" hours ago";1532}elsif($age>60*2) {1533$age_str=int$age/60;1534$age_str.=" min ago";1535}elsif($age>2) {1536$age_str=int$age;1537$age_str.=" sec ago";1538}else{1539$age_str.=" right now";1540}1541return$age_str;1542}15431544useconstant{1545 S_IFINVALID =>0030000,1546 S_IFGITLINK =>0160000,1547};15481549# submodule/subproject, a commit object reference1550sub S_ISGITLINK {1551my$mode=shift;15521553return(($mode& S_IFMT) == S_IFGITLINK)1554}15551556# convert file mode in octal to symbolic file mode string1557sub mode_str {1558my$mode=oct shift;15591560if(S_ISGITLINK($mode)) {1561return'm---------';1562}elsif(S_ISDIR($mode& S_IFMT)) {1563return'drwxr-xr-x';1564}elsif(S_ISLNK($mode)) {1565return'lrwxrwxrwx';1566}elsif(S_ISREG($mode)) {1567# git cares only about the executable bit1568if($mode& S_IXUSR) {1569return'-rwxr-xr-x';1570}else{1571return'-rw-r--r--';1572};1573}else{1574return'----------';1575}1576}15771578# convert file mode in octal to file type string1579sub file_type {1580my$mode=shift;15811582if($mode!~m/^[0-7]+$/) {1583return$mode;1584}else{1585$mode=oct$mode;1586}15871588if(S_ISGITLINK($mode)) {1589return"submodule";1590}elsif(S_ISDIR($mode& S_IFMT)) {1591return"directory";1592}elsif(S_ISLNK($mode)) {1593return"symlink";1594}elsif(S_ISREG($mode)) {1595return"file";1596}else{1597return"unknown";1598}1599}16001601# convert file mode in octal to file type description string1602sub file_type_long {1603my$mode=shift;16041605if($mode!~m/^[0-7]+$/) {1606return$mode;1607}else{1608$mode=oct$mode;1609}16101611if(S_ISGITLINK($mode)) {1612return"submodule";1613}elsif(S_ISDIR($mode& S_IFMT)) {1614return"directory";1615}elsif(S_ISLNK($mode)) {1616return"symlink";1617}elsif(S_ISREG($mode)) {1618if($mode& S_IXUSR) {1619return"executable";1620}else{1621return"file";1622};1623}else{1624return"unknown";1625}1626}162716281629## ----------------------------------------------------------------------1630## functions returning short HTML fragments, or transforming HTML fragments1631## which don't belong to other sections16321633# format line of commit message.1634sub format_log_line_html {1635my$line=shift;16361637$line= esc_html($line, -nbsp=>1);1638$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1639$cgi->a({-href => href(action=>"object", hash=>$1),1640-class=>"text"},$1);1641}eg;16421643return$line;1644}16451646# format marker of refs pointing to given object16471648# the destination action is chosen based on object type and current context:1649# - for annotated tags, we choose the tag view unless it's the current view1650# already, in which case we go to shortlog view1651# - for other refs, we keep the current view if we're in history, shortlog or1652# log view, and select shortlog otherwise1653sub format_ref_marker {1654my($refs,$id) =@_;1655my$markers='';16561657if(defined$refs->{$id}) {1658foreachmy$ref(@{$refs->{$id}}) {1659# this code exploits the fact that non-lightweight tags are the1660# only indirect objects, and that they are the only objects for which1661# we want to use tag instead of shortlog as action1662my($type,$name) =qw();1663my$indirect= ($ref=~s/\^\{\}$//);1664# e.g. tags/v2.6.11 or heads/next1665if($ref=~m!^(.*?)s?/(.*)$!) {1666$type=$1;1667$name=$2;1668}else{1669$type="ref";1670$name=$ref;1671}16721673my$class=$type;1674$class.=" indirect"if$indirect;16751676my$dest_action="shortlog";16771678if($indirect) {1679$dest_action="tag"unless$actioneq"tag";1680}elsif($action=~/^(history|(short)?log)$/) {1681$dest_action=$action;1682}16831684my$dest="";1685$dest.="refs/"unless$ref=~ m!^refs/!;1686$dest.=$ref;16871688my$link=$cgi->a({1689-href => href(1690 action=>$dest_action,1691 hash=>$dest1692)},$name);16931694$markers.=" <span class=\"$class\"title=\"$ref\">".1695$link."</span>";1696}1697}16981699if($markers) {1700return' <span class="refs">'.$markers.'</span>';1701}else{1702return"";1703}1704}17051706# format, perhaps shortened and with markers, title line1707sub format_subject_html {1708my($long,$short,$href,$extra) =@_;1709$extra=''unlessdefined($extra);17101711if(length($short) <length($long)) {1712$long=~s/[[:cntrl:]]/?/g;1713return$cgi->a({-href =>$href, -class=>"list subject",1714-title => to_utf8($long)},1715 esc_html($short)) .$extra;1716}else{1717return$cgi->a({-href =>$href, -class=>"list subject"},1718 esc_html($long)) .$extra;1719}1720}17211722# Rather than recomputing the url for an email multiple times, we cache it1723# after the first hit. This gives a visible benefit in views where the avatar1724# for the same email is used repeatedly (e.g. shortlog).1725# The cache is shared by all avatar engines (currently gravatar only), which1726# are free to use it as preferred. Since only one avatar engine is used for any1727# given page, there's no risk for cache conflicts.1728our%avatar_cache= ();17291730# Compute the picon url for a given email, by using the picon search service over at1731# http://www.cs.indiana.edu/picons/search.html1732sub picon_url {1733my$email=lc shift;1734if(!$avatar_cache{$email}) {1735my($user,$domain) =split('@',$email);1736$avatar_cache{$email} =1737"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1738"$domain/$user/".1739"users+domains+unknown/up/single";1740}1741return$avatar_cache{$email};1742}17431744# Compute the gravatar url for a given email, if it's not in the cache already.1745# Gravatar stores only the part of the URL before the size, since that's the1746# one computationally more expensive. This also allows reuse of the cache for1747# different sizes (for this particular engine).1748sub gravatar_url {1749my$email=lc shift;1750my$size=shift;1751$avatar_cache{$email} ||=1752"http://www.gravatar.com/avatar/".1753 Digest::MD5::md5_hex($email) ."?s=";1754return$avatar_cache{$email} .$size;1755}17561757# Insert an avatar for the given $email at the given $size if the feature1758# is enabled.1759sub git_get_avatar {1760my($email,%opts) =@_;1761my$pre_white= ($opts{-pad_before} ?" ":"");1762my$post_white= ($opts{-pad_after} ?" ":"");1763$opts{-size} ||='default';1764my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1765my$url="";1766if($git_avatareq'gravatar') {1767$url= gravatar_url($email,$size);1768}elsif($git_avatareq'picon') {1769$url= picon_url($email);1770}1771# Other providers can be added by extending the if chain, defining $url1772# as needed. If no variant puts something in $url, we assume avatars1773# are completely disabled/unavailable.1774if($url) {1775return$pre_white.1776"<img width=\"$size\"".1777"class=\"avatar\"".1778"src=\"$url\"".1779"alt=\"\"".1780"/>".$post_white;1781}else{1782return"";1783}1784}17851786sub format_search_author {1787my($author,$searchtype,$displaytext) =@_;1788my$have_search= gitweb_check_feature('search');17891790if($have_search) {1791my$performed="";1792if($searchtypeeq'author') {1793$performed="authored";1794}elsif($searchtypeeq'committer') {1795$performed="committed";1796}17971798return$cgi->a({-href => href(action=>"search", hash=>$hash,1799 searchtext=>$author,1800 searchtype=>$searchtype),class=>"list",1801 title=>"Search for commits$performedby$author"},1802$displaytext);18031804}else{1805return$displaytext;1806}1807}18081809# format the author name of the given commit with the given tag1810# the author name is chopped and escaped according to the other1811# optional parameters (see chop_str).1812sub format_author_html {1813my$tag=shift;1814my$co=shift;1815my$author= chop_and_escape_str($co->{'author_name'},@_);1816return"<$tagclass=\"author\">".1817 format_search_author($co->{'author_name'},"author",1818 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1819$author) .1820"</$tag>";1821}18221823# format git diff header line, i.e. "diff --(git|combined|cc) ..."1824sub format_git_diff_header_line {1825my$line=shift;1826my$diffinfo=shift;1827my($from,$to) =@_;18281829if($diffinfo->{'nparents'}) {1830# combined diff1831$line=~s!^(diff (.*?) )"?.*$!$1!;1832if($to->{'href'}) {1833$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1834 esc_path($to->{'file'}));1835}else{# file was deleted (no href)1836$line.= esc_path($to->{'file'});1837}1838}else{1839# "ordinary" diff1840$line=~s!^(diff (.*?) )"?a/.*$!$1!;1841if($from->{'href'}) {1842$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1843'a/'. esc_path($from->{'file'}));1844}else{# file was added (no href)1845$line.='a/'. esc_path($from->{'file'});1846}1847$line.=' ';1848if($to->{'href'}) {1849$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1850'b/'. esc_path($to->{'file'}));1851}else{# file was deleted1852$line.='b/'. esc_path($to->{'file'});1853}1854}18551856return"<div class=\"diff header\">$line</div>\n";1857}18581859# format extended diff header line, before patch itself1860sub format_extended_diff_header_line {1861my$line=shift;1862my$diffinfo=shift;1863my($from,$to) =@_;18641865# match <path>1866if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1867$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1868 esc_path($from->{'file'}));1869}1870if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1871$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1872 esc_path($to->{'file'}));1873}1874# match single <mode>1875if($line=~m/\s(\d{6})$/) {1876$line.='<span class="info"> ('.1877 file_type_long($1) .1878')</span>';1879}1880# match <hash>1881if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1882# can match only for combined diff1883$line='index ';1884for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1885if($from->{'href'}[$i]) {1886$line.=$cgi->a({-href=>$from->{'href'}[$i],1887-class=>"hash"},1888substr($diffinfo->{'from_id'}[$i],0,7));1889}else{1890$line.='0' x 7;1891}1892# separator1893$line.=','if($i<$diffinfo->{'nparents'} -1);1894}1895$line.='..';1896if($to->{'href'}) {1897$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1898substr($diffinfo->{'to_id'},0,7));1899}else{1900$line.='0' x 7;1901}19021903}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1904# can match only for ordinary diff1905my($from_link,$to_link);1906if($from->{'href'}) {1907$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1908substr($diffinfo->{'from_id'},0,7));1909}else{1910$from_link='0' x 7;1911}1912if($to->{'href'}) {1913$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1914substr($diffinfo->{'to_id'},0,7));1915}else{1916$to_link='0' x 7;1917}1918my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1919$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1920}19211922return$line."<br/>\n";1923}19241925# format from-file/to-file diff header1926sub format_diff_from_to_header {1927my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1928my$line;1929my$result='';19301931$line=$from_line;1932#assert($line =~ m/^---/) if DEBUG;1933# no extra formatting for "^--- /dev/null"1934if(!$diffinfo->{'nparents'}) {1935# ordinary (single parent) diff1936if($line=~m!^--- "?a/!) {1937if($from->{'href'}) {1938$line='--- a/'.1939$cgi->a({-href=>$from->{'href'}, -class=>"path"},1940 esc_path($from->{'file'}));1941}else{1942$line='--- a/'.1943 esc_path($from->{'file'});1944}1945}1946$result.= qq!<div class="diff from_file">$line</div>\n!;19471948}else{1949# combined diff (merge commit)1950for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1951if($from->{'href'}[$i]) {1952$line='--- '.1953$cgi->a({-href=>href(action=>"blobdiff",1954 hash_parent=>$diffinfo->{'from_id'}[$i],1955 hash_parent_base=>$parents[$i],1956 file_parent=>$from->{'file'}[$i],1957 hash=>$diffinfo->{'to_id'},1958 hash_base=>$hash,1959 file_name=>$to->{'file'}),1960-class=>"path",1961-title=>"diff". ($i+1)},1962$i+1) .1963'/'.1964$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1965 esc_path($from->{'file'}[$i]));1966}else{1967$line='--- /dev/null';1968}1969$result.= qq!<div class="diff from_file">$line</div>\n!;1970}1971}19721973$line=$to_line;1974#assert($line =~ m/^\+\+\+/) if DEBUG;1975# no extra formatting for "^+++ /dev/null"1976if($line=~m!^\+\+\+ "?b/!) {1977if($to->{'href'}) {1978$line='+++ b/'.1979$cgi->a({-href=>$to->{'href'}, -class=>"path"},1980 esc_path($to->{'file'}));1981}else{1982$line='+++ b/'.1983 esc_path($to->{'file'});1984}1985}1986$result.= qq!<div class="diff to_file">$line</div>\n!;19871988return$result;1989}19901991# create note for patch simplified by combined diff1992sub format_diff_cc_simplified {1993my($diffinfo,@parents) =@_;1994my$result='';19951996$result.="<div class=\"diff header\">".1997"diff --cc ";1998if(!is_deleted($diffinfo)) {1999$result.=$cgi->a({-href => href(action=>"blob",2000 hash_base=>$hash,2001 hash=>$diffinfo->{'to_id'},2002 file_name=>$diffinfo->{'to_file'}),2003-class=>"path"},2004 esc_path($diffinfo->{'to_file'}));2005}else{2006$result.= esc_path($diffinfo->{'to_file'});2007}2008$result.="</div>\n".# class="diff header"2009"<div class=\"diff nodifferences\">".2010"Simple merge".2011"</div>\n";# class="diff nodifferences"20122013return$result;2014}20152016# format patch (diff) line (not to be used for diff headers)2017sub format_diff_line {2018my$line=shift;2019my($from,$to) =@_;2020my$diff_class="";20212022chomp$line;20232024if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2025# combined diff2026my$prefix=substr($line,0,scalar@{$from->{'href'}});2027if($line=~m/^\@{3}/) {2028$diff_class=" chunk_header";2029}elsif($line=~m/^\\/) {2030$diff_class=" incomplete";2031}elsif($prefix=~tr/+/+/) {2032$diff_class=" add";2033}elsif($prefix=~tr/-/-/) {2034$diff_class=" rem";2035}2036}else{2037# assume ordinary diff2038my$char=substr($line,0,1);2039if($chareq'+') {2040$diff_class=" add";2041}elsif($chareq'-') {2042$diff_class=" rem";2043}elsif($chareq'@') {2044$diff_class=" chunk_header";2045}elsif($chareq"\\") {2046$diff_class=" incomplete";2047}2048}2049$line= untabify($line);2050if($from&&$to&&$line=~m/^\@{2} /) {2051my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2052$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;20532054$from_lines=0unlessdefined$from_lines;2055$to_lines=0unlessdefined$to_lines;20562057if($from->{'href'}) {2058$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2059-class=>"list"},$from_text);2060}2061if($to->{'href'}) {2062$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2063-class=>"list"},$to_text);2064}2065$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2066"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2067return"<div class=\"diff$diff_class\">$line</div>\n";2068}elsif($from&&$to&&$line=~m/^\@{3}/) {2069my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2070my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);20712072@from_text=split(' ',$ranges);2073for(my$i=0;$i<@from_text; ++$i) {2074($from_start[$i],$from_nlines[$i]) =2075(split(',',substr($from_text[$i],1)),0);2076}20772078$to_text=pop@from_text;2079$to_start=pop@from_start;2080$to_nlines=pop@from_nlines;20812082$line="<span class=\"chunk_info\">$prefix";2083for(my$i=0;$i<@from_text; ++$i) {2084if($from->{'href'}[$i]) {2085$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2086-class=>"list"},$from_text[$i]);2087}else{2088$line.=$from_text[$i];2089}2090$line.=" ";2091}2092if($to->{'href'}) {2093$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2094-class=>"list"},$to_text);2095}else{2096$line.=$to_text;2097}2098$line.="$prefix</span>".2099"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2100return"<div class=\"diff$diff_class\">$line</div>\n";2101}2102return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2103}21042105# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2106# linked. Pass the hash of the tree/commit to snapshot.2107sub format_snapshot_links {2108my($hash) =@_;2109my$num_fmts=@snapshot_fmts;2110if($num_fmts>1) {2111# A parenthesized list of links bearing format names.2112# e.g. "snapshot (_tar.gz_ _zip_)"2113return"snapshot (".join(' ',map2114$cgi->a({2115-href => href(2116 action=>"snapshot",2117 hash=>$hash,2118 snapshot_format=>$_2119)2120},$known_snapshot_formats{$_}{'display'})2121,@snapshot_fmts) .")";2122}elsif($num_fmts==1) {2123# A single "snapshot" link whose tooltip bears the format name.2124# i.e. "_snapshot_"2125my($fmt) =@snapshot_fmts;2126return2127$cgi->a({2128-href => href(2129 action=>"snapshot",2130 hash=>$hash,2131 snapshot_format=>$fmt2132),2133-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2134},"snapshot");2135}else{# $num_fmts == 02136returnundef;2137}2138}21392140## ......................................................................2141## functions returning values to be passed, perhaps after some2142## transformation, to other functions; e.g. returning arguments to href()21432144# returns hash to be passed to href to generate gitweb URL2145# in -title key it returns description of link2146sub get_feed_info {2147my$format=shift||'Atom';2148my%res= (action =>lc($format));21492150# feed links are possible only for project views2151return unless(defined$project);2152# some views should link to OPML, or to generic project feed,2153# or don't have specific feed yet (so they should use generic)2154return if($action=~/^(?:tags|heads|forks|tag|search)$/x);21552156my$branch;2157# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2158# from tag links; this also makes possible to detect branch links2159if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2160(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2161$branch=$1;2162}2163# find log type for feed description (title)2164my$type='log';2165if(defined$file_name) {2166$type="history of$file_name";2167$type.="/"if($actioneq'tree');2168$type.=" on '$branch'"if(defined$branch);2169}else{2170$type="log of$branch"if(defined$branch);2171}21722173$res{-title} =$type;2174$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2175$res{'file_name'} =$file_name;21762177return%res;2178}21792180## ----------------------------------------------------------------------2181## git utility subroutines, invoking git commands21822183# returns path to the core git executable and the --git-dir parameter as list2184sub git_cmd {2185$number_of_git_cmds++;2186return$GIT,'--git-dir='.$git_dir;2187}21882189# quote the given arguments for passing them to the shell2190# quote_command("command", "arg 1", "arg with ' and ! characters")2191# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2192# Try to avoid using this function wherever possible.2193sub quote_command {2194returnjoin(' ',2195map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2196}21972198# get HEAD ref of given project as hash2199sub git_get_head_hash {2200return git_get_full_hash(shift,'HEAD');2201}22022203sub git_get_full_hash {2204return git_get_hash(@_);2205}22062207sub git_get_short_hash {2208return git_get_hash(@_,'--short=7');2209}22102211sub git_get_hash {2212my($project,$hash,@options) =@_;2213my$o_git_dir=$git_dir;2214my$retval=undef;2215$git_dir="$projectroot/$project";2216if(open my$fd,'-|', git_cmd(),'rev-parse',2217'--verify','-q',@options,$hash) {2218$retval= <$fd>;2219chomp$retvalifdefined$retval;2220close$fd;2221}2222if(defined$o_git_dir) {2223$git_dir=$o_git_dir;2224}2225return$retval;2226}22272228# get type of given object2229sub git_get_type {2230my$hash=shift;22312232open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2233my$type= <$fd>;2234close$fdorreturn;2235chomp$type;2236return$type;2237}22382239# repository configuration2240our$config_file='';2241our%config;22422243# store multiple values for single key as anonymous array reference2244# single values stored directly in the hash, not as [ <value> ]2245sub hash_set_multi {2246my($hash,$key,$value) =@_;22472248if(!exists$hash->{$key}) {2249$hash->{$key} =$value;2250}elsif(!ref$hash->{$key}) {2251$hash->{$key} = [$hash->{$key},$value];2252}else{2253push@{$hash->{$key}},$value;2254}2255}22562257# return hash of git project configuration2258# optionally limited to some section, e.g. 'gitweb'2259sub git_parse_project_config {2260my$section_regexp=shift;2261my%config;22622263local$/="\0";22642265open my$fh,"-|", git_cmd(),"config",'-z','-l',2266orreturn;22672268while(my$keyval= <$fh>) {2269chomp$keyval;2270my($key,$value) =split(/\n/,$keyval,2);22712272 hash_set_multi(\%config,$key,$value)2273if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2274}2275close$fh;22762277return%config;2278}22792280# convert config value to boolean: 'true' or 'false'2281# no value, number > 0, 'true' and 'yes' values are true2282# rest of values are treated as false (never as error)2283sub config_to_bool {2284my$val=shift;22852286return1if!defined$val;# section.key22872288# strip leading and trailing whitespace2289$val=~s/^\s+//;2290$val=~s/\s+$//;22912292return(($val=~/^\d+$/&&$val) ||# section.key = 12293($val=~/^(?:true|yes)$/i));# section.key = true2294}22952296# convert config value to simple decimal number2297# an optional value suffix of 'k', 'm', or 'g' will cause the value2298# to be multiplied by 1024, 1048576, or 10737418242299sub config_to_int {2300my$val=shift;23012302# strip leading and trailing whitespace2303$val=~s/^\s+//;2304$val=~s/\s+$//;23052306if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2307$unit=lc($unit);2308# unknown unit is treated as 12309return$num* ($uniteq'g'?1073741824:2310$uniteq'm'?1048576:2311$uniteq'k'?1024:1);2312}2313return$val;2314}23152316# convert config value to array reference, if needed2317sub config_to_multi {2318my$val=shift;23192320returnref($val) ?$val: (defined($val) ? [$val] : []);2321}23222323sub git_get_project_config {2324my($key,$type) =@_;23252326return unlessdefined$git_dir;23272328# key sanity check2329return unless($key);2330$key=~s/^gitweb\.//;2331return if($key=~m/\W/);23322333# type sanity check2334if(defined$type) {2335$type=~s/^--//;2336$type=undef2337unless($typeeq'bool'||$typeeq'int');2338}23392340# get config2341if(!defined$config_file||2342$config_filene"$git_dir/config") {2343%config= git_parse_project_config('gitweb');2344$config_file="$git_dir/config";2345}23462347# check if config variable (key) exists2348return unlessexists$config{"gitweb.$key"};23492350# ensure given type2351if(!defined$type) {2352return$config{"gitweb.$key"};2353}elsif($typeeq'bool') {2354# backward compatibility: 'git config --bool' returns true/false2355return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2356}elsif($typeeq'int') {2357return config_to_int($config{"gitweb.$key"});2358}2359return$config{"gitweb.$key"};2360}23612362# get hash of given path at given ref2363sub git_get_hash_by_path {2364my$base=shift;2365my$path=shift||returnundef;2366my$type=shift;23672368$path=~ s,/+$,,;23692370open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2371or die_error(500,"Open git-ls-tree failed");2372my$line= <$fd>;2373close$fdorreturnundef;23742375if(!defined$line) {2376# there is no tree or hash given by $path at $base2377returnundef;2378}23792380#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2381$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2382if(defined$type&&$typene$2) {2383# type doesn't match2384returnundef;2385}2386return$3;2387}23882389# get path of entry with given hash at given tree-ish (ref)2390# used to get 'from' filename for combined diff (merge commit) for renames2391sub git_get_path_by_hash {2392my$base=shift||return;2393my$hash=shift||return;23942395local$/="\0";23962397open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2398orreturnundef;2399while(my$line= <$fd>) {2400chomp$line;24012402#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2403#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2404if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2405close$fd;2406return$1;2407}2408}2409close$fd;2410returnundef;2411}24122413## ......................................................................2414## git utility functions, directly accessing git repository24152416sub git_get_project_description {2417my$path=shift;24182419$git_dir="$projectroot/$path";2420open my$fd,'<',"$git_dir/description"2421orreturn git_get_project_config('description');2422my$descr= <$fd>;2423close$fd;2424if(defined$descr) {2425chomp$descr;2426}2427return$descr;2428}24292430sub git_get_project_ctags {2431my$path=shift;2432my$ctags= {};24332434$git_dir="$projectroot/$path";2435opendir my$dh,"$git_dir/ctags"2436orreturn$ctags;2437foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2438open my$ct,'<',$_ornext;2439my$val= <$ct>;2440chomp$val;2441close$ct;2442my$ctag=$_;$ctag=~ s#.*/##;2443$ctags->{$ctag} =$val;2444}2445closedir$dh;2446$ctags;2447}24482449sub git_populate_project_tagcloud {2450my$ctags=shift;24512452# First, merge different-cased tags; tags vote on casing2453my%ctags_lc;2454foreach(keys%$ctags) {2455$ctags_lc{lc$_}->{count} +=$ctags->{$_};2456if(not$ctags_lc{lc$_}->{topcount}2457or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2458$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2459$ctags_lc{lc$_}->{topname} =$_;2460}2461}24622463my$cloud;2464if(eval{require HTML::TagCloud;1; }) {2465$cloud= HTML::TagCloud->new;2466foreach(sort keys%ctags_lc) {2467# Pad the title with spaces so that the cloud looks2468# less crammed.2469my$title=$ctags_lc{$_}->{topname};2470$title=~s/ / /g;2471$title=~s/^/ /g;2472$title=~s/$/ /g;2473$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2474}2475}else{2476$cloud= \%ctags_lc;2477}2478$cloud;2479}24802481sub git_show_project_tagcloud {2482my($cloud,$count) =@_;2483print STDERR ref($cloud)."..\n";2484if(ref$cloudeq'HTML::TagCloud') {2485return$cloud->html_and_css($count);2486}else{2487my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2488return'<p align="center">'.join(', ',map{2489"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2490}splice(@tags,0,$count)) .'</p>';2491}2492}24932494sub git_get_project_url_list {2495my$path=shift;24962497$git_dir="$projectroot/$path";2498open my$fd,'<',"$git_dir/cloneurl"2499orreturnwantarray?2500@{ config_to_multi(git_get_project_config('url')) } :2501 config_to_multi(git_get_project_config('url'));2502my@git_project_url_list=map{chomp;$_} <$fd>;2503close$fd;25042505returnwantarray?@git_project_url_list: \@git_project_url_list;2506}25072508sub git_get_projects_list {2509my($filter) =@_;2510my@list;25112512$filter||='';2513$filter=~s/\.git$//;25142515my$check_forks= gitweb_check_feature('forks');25162517if(-d $projects_list) {2518# search in directory2519my$dir=$projects_list. ($filter?"/$filter":'');2520# remove the trailing "/"2521$dir=~s!/+$!!;2522my$pfxlen=length("$dir");2523my$pfxdepth= ($dir=~tr!/!!);25242525 File::Find::find({2526 follow_fast =>1,# follow symbolic links2527 follow_skip =>2,# ignore duplicates2528 dangling_symlinks =>0,# ignore dangling symlinks, silently2529 wanted =>sub{2530# skip project-list toplevel, if we get it.2531return if(m!^[/.]$!);2532# only directories can be git repositories2533return unless(-d $_);2534# don't traverse too deep (Find is super slow on os x)2535if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2536$File::Find::prune =1;2537return;2538}25392540my$subdir=substr($File::Find::name,$pfxlen+1);2541# we check related file in $projectroot2542my$path= ($filter?"$filter/":'') .$subdir;2543if(check_export_ok("$projectroot/$path")) {2544push@list, { path =>$path};2545$File::Find::prune =1;2546}2547},2548},"$dir");25492550}elsif(-f $projects_list) {2551# read from file(url-encoded):2552# 'git%2Fgit.git Linus+Torvalds'2553# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2554# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2555my%paths;2556open my$fd,'<',$projects_listorreturn;2557 PROJECT:2558while(my$line= <$fd>) {2559chomp$line;2560my($path,$owner) =split' ',$line;2561$path= unescape($path);2562$owner= unescape($owner);2563if(!defined$path) {2564next;2565}2566if($filterne'') {2567# looking for forks;2568my$pfx=substr($path,0,length($filter));2569if($pfxne$filter) {2570next PROJECT;2571}2572my$sfx=substr($path,length($filter));2573if($sfx!~/^\/.*\.git$/) {2574next PROJECT;2575}2576}elsif($check_forks) {2577 PATH:2578foreachmy$filter(keys%paths) {2579# looking for forks;2580my$pfx=substr($path,0,length($filter));2581if($pfxne$filter) {2582next PATH;2583}2584my$sfx=substr($path,length($filter));2585if($sfx!~/^\/.*\.git$/) {2586next PATH;2587}2588# is a fork, don't include it in2589# the list2590next PROJECT;2591}2592}2593if(check_export_ok("$projectroot/$path")) {2594my$pr= {2595 path =>$path,2596 owner => to_utf8($owner),2597};2598push@list,$pr;2599(my$forks_path=$path) =~s/\.git$//;2600$paths{$forks_path}++;2601}2602}2603close$fd;2604}2605return@list;2606}26072608our$gitweb_project_owner=undef;2609sub git_get_project_list_from_file {26102611return if(defined$gitweb_project_owner);26122613$gitweb_project_owner= {};2614# read from file (url-encoded):2615# 'git%2Fgit.git Linus+Torvalds'2616# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2617# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2618if(-f $projects_list) {2619open(my$fd,'<',$projects_list);2620while(my$line= <$fd>) {2621chomp$line;2622my($pr,$ow) =split' ',$line;2623$pr= unescape($pr);2624$ow= unescape($ow);2625$gitweb_project_owner->{$pr} = to_utf8($ow);2626}2627close$fd;2628}2629}26302631sub git_get_project_owner {2632my$project=shift;2633my$owner;26342635returnundefunless$project;2636$git_dir="$projectroot/$project";26372638if(!defined$gitweb_project_owner) {2639 git_get_project_list_from_file();2640}26412642if(exists$gitweb_project_owner->{$project}) {2643$owner=$gitweb_project_owner->{$project};2644}2645if(!defined$owner){2646$owner= git_get_project_config('owner');2647}2648if(!defined$owner) {2649$owner= get_file_owner("$git_dir");2650}26512652return$owner;2653}26542655sub git_get_last_activity {2656my($path) =@_;2657my$fd;26582659$git_dir="$projectroot/$path";2660open($fd,"-|", git_cmd(),'for-each-ref',2661'--format=%(committer)',2662'--sort=-committerdate',2663'--count=1',2664'refs/heads')orreturn;2665my$most_recent= <$fd>;2666close$fdorreturn;2667if(defined$most_recent&&2668$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2669my$timestamp=$1;2670my$age=time-$timestamp;2671return($age, age_string($age));2672}2673return(undef,undef);2674}26752676sub git_get_references {2677my$type=shift||"";2678my%refs;2679# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112680# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2681open my$fd,"-|", git_cmd(),"show-ref","--dereference",2682($type? ("--","refs/$type") : ())# use -- <pattern> if $type2683orreturn;26842685while(my$line= <$fd>) {2686chomp$line;2687if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2688if(defined$refs{$1}) {2689push@{$refs{$1}},$2;2690}else{2691$refs{$1} = [$2];2692}2693}2694}2695close$fdorreturn;2696return \%refs;2697}26982699sub git_get_rev_name_tags {2700my$hash=shift||returnundef;27012702open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2703orreturn;2704my$name_rev= <$fd>;2705close$fd;27062707if($name_rev=~ m|^$hash tags/(.*)$|) {2708return$1;2709}else{2710# catches also '$hash undefined' output2711returnundef;2712}2713}27142715## ----------------------------------------------------------------------2716## parse to hash functions27172718sub parse_date {2719my$epoch=shift;2720my$tz=shift||"-0000";27212722my%date;2723my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2724my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2725my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2726$date{'hour'} =$hour;2727$date{'minute'} =$min;2728$date{'mday'} =$mday;2729$date{'day'} =$days[$wday];2730$date{'month'} =$months[$mon];2731$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2732$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2733$date{'mday-time'} =sprintf"%d%s%02d:%02d",2734$mday,$months[$mon],$hour,$min;2735$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",27361900+$year,1+$mon,$mday,$hour,$min,$sec;27372738$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2739my$local=$epoch+ ((int$1+ ($2/60)) *3600);2740($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2741$date{'hour_local'} =$hour;2742$date{'minute_local'} =$min;2743$date{'tz_local'} =$tz;2744$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",27451900+$year,$mon+1,$mday,2746$hour,$min,$sec,$tz);2747return%date;2748}27492750sub parse_tag {2751my$tag_id=shift;2752my%tag;2753my@comment;27542755open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2756$tag{'id'} =$tag_id;2757while(my$line= <$fd>) {2758chomp$line;2759if($line=~m/^object ([0-9a-fA-F]{40})$/) {2760$tag{'object'} =$1;2761}elsif($line=~m/^type (.+)$/) {2762$tag{'type'} =$1;2763}elsif($line=~m/^tag (.+)$/) {2764$tag{'name'} =$1;2765}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2766$tag{'author'} =$1;2767$tag{'author_epoch'} =$2;2768$tag{'author_tz'} =$3;2769if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2770$tag{'author_name'} =$1;2771$tag{'author_email'} =$2;2772}else{2773$tag{'author_name'} =$tag{'author'};2774}2775}elsif($line=~m/--BEGIN/) {2776push@comment,$line;2777last;2778}elsif($lineeq"") {2779last;2780}2781}2782push@comment, <$fd>;2783$tag{'comment'} = \@comment;2784close$fdorreturn;2785if(!defined$tag{'name'}) {2786return2787};2788return%tag2789}27902791sub parse_commit_text {2792my($commit_text,$withparents) =@_;2793my@commit_lines=split'\n',$commit_text;2794my%co;27952796pop@commit_lines;# Remove '\0'27972798if(!@commit_lines) {2799return;2800}28012802my$header=shift@commit_lines;2803if($header!~m/^[0-9a-fA-F]{40}/) {2804return;2805}2806($co{'id'},my@parents) =split' ',$header;2807while(my$line=shift@commit_lines) {2808last if$lineeq"\n";2809if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2810$co{'tree'} =$1;2811}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2812push@parents,$1;2813}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2814$co{'author'} = to_utf8($1);2815$co{'author_epoch'} =$2;2816$co{'author_tz'} =$3;2817if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2818$co{'author_name'} =$1;2819$co{'author_email'} =$2;2820}else{2821$co{'author_name'} =$co{'author'};2822}2823}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2824$co{'committer'} = to_utf8($1);2825$co{'committer_epoch'} =$2;2826$co{'committer_tz'} =$3;2827if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2828$co{'committer_name'} =$1;2829$co{'committer_email'} =$2;2830}else{2831$co{'committer_name'} =$co{'committer'};2832}2833}2834}2835if(!defined$co{'tree'}) {2836return;2837};2838$co{'parents'} = \@parents;2839$co{'parent'} =$parents[0];28402841foreachmy$title(@commit_lines) {2842$title=~s/^ //;2843if($titlene"") {2844$co{'title'} = chop_str($title,80,5);2845# remove leading stuff of merges to make the interesting part visible2846if(length($title) >50) {2847$title=~s/^Automatic //;2848$title=~s/^merge (of|with) /Merge ... /i;2849if(length($title) >50) {2850$title=~s/(http|rsync):\/\///;2851}2852if(length($title) >50) {2853$title=~s/(master|www|rsync)\.//;2854}2855if(length($title) >50) {2856$title=~s/kernel.org:?//;2857}2858if(length($title) >50) {2859$title=~s/\/pub\/scm//;2860}2861}2862$co{'title_short'} = chop_str($title,50,5);2863last;2864}2865}2866if(!defined$co{'title'} ||$co{'title'}eq"") {2867$co{'title'} =$co{'title_short'} ='(no commit message)';2868}2869# remove added spaces2870foreachmy$line(@commit_lines) {2871$line=~s/^ //;2872}2873$co{'comment'} = \@commit_lines;28742875my$age=time-$co{'committer_epoch'};2876$co{'age'} =$age;2877$co{'age_string'} = age_string($age);2878my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2879if($age>60*60*24*7*2) {2880$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2881$co{'age_string_age'} =$co{'age_string'};2882}else{2883$co{'age_string_date'} =$co{'age_string'};2884$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2885}2886return%co;2887}28882889sub parse_commit {2890my($commit_id) =@_;2891my%co;28922893local$/="\0";28942895open my$fd,"-|", git_cmd(),"rev-list",2896"--parents",2897"--header",2898"--max-count=1",2899$commit_id,2900"--",2901or die_error(500,"Open git-rev-list failed");2902%co= parse_commit_text(<$fd>,1);2903close$fd;29042905return%co;2906}29072908sub parse_commits {2909my($commit_id,$maxcount,$skip,$filename,@args) =@_;2910my@cos;29112912$maxcount||=1;2913$skip||=0;29142915local$/="\0";29162917open my$fd,"-|", git_cmd(),"rev-list",2918"--header",2919@args,2920("--max-count=".$maxcount),2921("--skip=".$skip),2922@extra_options,2923$commit_id,2924"--",2925($filename? ($filename) : ())2926or die_error(500,"Open git-rev-list failed");2927while(my$line= <$fd>) {2928my%co= parse_commit_text($line);2929push@cos, \%co;2930}2931close$fd;29322933returnwantarray?@cos: \@cos;2934}29352936# parse line of git-diff-tree "raw" output2937sub parse_difftree_raw_line {2938my$line=shift;2939my%res;29402941# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2942# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2943if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2944$res{'from_mode'} =$1;2945$res{'to_mode'} =$2;2946$res{'from_id'} =$3;2947$res{'to_id'} =$4;2948$res{'status'} =$5;2949$res{'similarity'} =$6;2950if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2951($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2952}else{2953$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2954}2955}2956# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2957# combined diff (for merge commit)2958elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2959$res{'nparents'} =length($1);2960$res{'from_mode'} = [split(' ',$2) ];2961$res{'to_mode'} =pop@{$res{'from_mode'}};2962$res{'from_id'} = [split(' ',$3) ];2963$res{'to_id'} =pop@{$res{'from_id'}};2964$res{'status'} = [split('',$4) ];2965$res{'to_file'} = unquote($5);2966}2967# 'c512b523472485aef4fff9e57b229d9d243c967f'2968elsif($line=~m/^([0-9a-fA-F]{40})$/) {2969$res{'commit'} =$1;2970}29712972returnwantarray?%res: \%res;2973}29742975# wrapper: return parsed line of git-diff-tree "raw" output2976# (the argument might be raw line, or parsed info)2977sub parsed_difftree_line {2978my$line_or_ref=shift;29792980if(ref($line_or_ref)eq"HASH") {2981# pre-parsed (or generated by hand)2982return$line_or_ref;2983}else{2984return parse_difftree_raw_line($line_or_ref);2985}2986}29872988# parse line of git-ls-tree output2989sub parse_ls_tree_line {2990my$line=shift;2991my%opts=@_;2992my%res;29932994if($opts{'-l'}) {2995#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'2996$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;29972998$res{'mode'} =$1;2999$res{'type'} =$2;3000$res{'hash'} =$3;3001$res{'size'} =$4;3002if($opts{'-z'}) {3003$res{'name'} =$5;3004}else{3005$res{'name'} = unquote($5);3006}3007}else{3008#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3009$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;30103011$res{'mode'} =$1;3012$res{'type'} =$2;3013$res{'hash'} =$3;3014if($opts{'-z'}) {3015$res{'name'} =$4;3016}else{3017$res{'name'} = unquote($4);3018}3019}30203021returnwantarray?%res: \%res;3022}30233024# generates _two_ hashes, references to which are passed as 2 and 3 argument3025sub parse_from_to_diffinfo {3026my($diffinfo,$from,$to,@parents) =@_;30273028if($diffinfo->{'nparents'}) {3029# combined diff3030$from->{'file'} = [];3031$from->{'href'} = [];3032 fill_from_file_info($diffinfo,@parents)3033unlessexists$diffinfo->{'from_file'};3034for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3035$from->{'file'}[$i] =3036defined$diffinfo->{'from_file'}[$i] ?3037$diffinfo->{'from_file'}[$i] :3038$diffinfo->{'to_file'};3039if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3040$from->{'href'}[$i] = href(action=>"blob",3041 hash_base=>$parents[$i],3042 hash=>$diffinfo->{'from_id'}[$i],3043 file_name=>$from->{'file'}[$i]);3044}else{3045$from->{'href'}[$i] =undef;3046}3047}3048}else{3049# ordinary (not combined) diff3050$from->{'file'} =$diffinfo->{'from_file'};3051if($diffinfo->{'status'}ne"A") {# not new (added) file3052$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3053 hash=>$diffinfo->{'from_id'},3054 file_name=>$from->{'file'});3055}else{3056delete$from->{'href'};3057}3058}30593060$to->{'file'} =$diffinfo->{'to_file'};3061if(!is_deleted($diffinfo)) {# file exists in result3062$to->{'href'} = href(action=>"blob", hash_base=>$hash,3063 hash=>$diffinfo->{'to_id'},3064 file_name=>$to->{'file'});3065}else{3066delete$to->{'href'};3067}3068}30693070## ......................................................................3071## parse to array of hashes functions30723073sub git_get_heads_list {3074my$limit=shift;3075my@headslist;30763077open my$fd,'-|', git_cmd(),'for-each-ref',3078($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3079'--format=%(objectname) %(refname) %(subject)%00%(committer)',3080'refs/heads'3081orreturn;3082while(my$line= <$fd>) {3083my%ref_item;30843085chomp$line;3086my($refinfo,$committerinfo) =split(/\0/,$line);3087my($hash,$name,$title) =split(' ',$refinfo,3);3088my($committer,$epoch,$tz) =3089($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3090$ref_item{'fullname'} =$name;3091$name=~s!^refs/heads/!!;30923093$ref_item{'name'} =$name;3094$ref_item{'id'} =$hash;3095$ref_item{'title'} =$title||'(no commit message)';3096$ref_item{'epoch'} =$epoch;3097if($epoch) {3098$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3099}else{3100$ref_item{'age'} ="unknown";3101}31023103push@headslist, \%ref_item;3104}3105close$fd;31063107returnwantarray?@headslist: \@headslist;3108}31093110sub git_get_tags_list {3111my$limit=shift;3112my@tagslist;31133114open my$fd,'-|', git_cmd(),'for-each-ref',3115($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3116'--format=%(objectname) %(objecttype) %(refname) '.3117'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3118'refs/tags'3119orreturn;3120while(my$line= <$fd>) {3121my%ref_item;31223123chomp$line;3124my($refinfo,$creatorinfo) =split(/\0/,$line);3125my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3126my($creator,$epoch,$tz) =3127($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3128$ref_item{'fullname'} =$name;3129$name=~s!^refs/tags/!!;31303131$ref_item{'type'} =$type;3132$ref_item{'id'} =$id;3133$ref_item{'name'} =$name;3134if($typeeq"tag") {3135$ref_item{'subject'} =$title;3136$ref_item{'reftype'} =$reftype;3137$ref_item{'refid'} =$refid;3138}else{3139$ref_item{'reftype'} =$type;3140$ref_item{'refid'} =$id;3141}31423143if($typeeq"tag"||$typeeq"commit") {3144$ref_item{'epoch'} =$epoch;3145if($epoch) {3146$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3147}else{3148$ref_item{'age'} ="unknown";3149}3150}31513152push@tagslist, \%ref_item;3153}3154close$fd;31553156returnwantarray?@tagslist: \@tagslist;3157}31583159## ----------------------------------------------------------------------3160## filesystem-related functions31613162sub get_file_owner {3163my$path=shift;31643165my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3166my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3167if(!defined$gcos) {3168returnundef;3169}3170my$owner=$gcos;3171$owner=~s/[,;].*$//;3172return to_utf8($owner);3173}31743175# assume that file exists3176sub insert_file {3177my$filename=shift;31783179open my$fd,'<',$filename;3180print map{ to_utf8($_) } <$fd>;3181close$fd;3182}31833184## ......................................................................3185## mimetype related functions31863187sub mimetype_guess_file {3188my$filename=shift;3189my$mimemap=shift;3190-r $mimemaporreturnundef;31913192my%mimemap;3193open(my$mh,'<',$mimemap)orreturnundef;3194while(<$mh>) {3195next ifm/^#/;# skip comments3196my($mimetype,$exts) =split(/\t+/);3197if(defined$exts) {3198my@exts=split(/\s+/,$exts);3199foreachmy$ext(@exts) {3200$mimemap{$ext} =$mimetype;3201}3202}3203}3204close($mh);32053206$filename=~/\.([^.]*)$/;3207return$mimemap{$1};3208}32093210sub mimetype_guess {3211my$filename=shift;3212my$mime;3213$filename=~/\./orreturnundef;32143215if($mimetypes_file) {3216my$file=$mimetypes_file;3217if($file!~m!^/!) {# if it is relative path3218# it is relative to project3219$file="$projectroot/$project/$file";3220}3221$mime= mimetype_guess_file($filename,$file);3222}3223$mime||= mimetype_guess_file($filename,'/etc/mime.types');3224return$mime;3225}32263227sub blob_mimetype {3228my$fd=shift;3229my$filename=shift;32303231if($filename) {3232my$mime= mimetype_guess($filename);3233$mimeandreturn$mime;3234}32353236# just in case3237return$default_blob_plain_mimetypeunless$fd;32383239if(-T $fd) {3240return'text/plain';3241}elsif(!$filename) {3242return'application/octet-stream';3243}elsif($filename=~m/\.png$/i) {3244return'image/png';3245}elsif($filename=~m/\.gif$/i) {3246return'image/gif';3247}elsif($filename=~m/\.jpe?g$/i) {3248return'image/jpeg';3249}else{3250return'application/octet-stream';3251}3252}32533254sub blob_contenttype {3255my($fd,$file_name,$type) =@_;32563257$type||= blob_mimetype($fd,$file_name);3258if($typeeq'text/plain'&&defined$default_text_plain_charset) {3259$type.="; charset=$default_text_plain_charset";3260}32613262return$type;3263}32643265## ======================================================================3266## functions printing HTML: header, footer, error page32673268sub git_header_html {3269my$status=shift||"200 OK";3270my$expires=shift;32713272my$title="$site_name";3273if(defined$project) {3274$title.=" - ". to_utf8($project);3275if(defined$action) {3276$title.="/$action";3277if(defined$file_name) {3278$title.=" - ". esc_path($file_name);3279if($actioneq"tree"&&$file_name!~ m|/$|) {3280$title.="/";3281}3282}3283}3284}3285my$content_type;3286# require explicit support from the UA if we are to send the page as3287# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3288# we have to do this because MSIE sometimes globs '*/*', pretending to3289# support xhtml+xml but choking when it gets what it asked for.3290if(defined$cgi->http('HTTP_ACCEPT') &&3291$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3292$cgi->Accept('application/xhtml+xml') !=0) {3293$content_type='application/xhtml+xml';3294}else{3295$content_type='text/html';3296}3297print$cgi->header(-type=>$content_type, -charset =>'utf-8',3298-status=>$status, -expires =>$expires);3299my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3300print<<EOF;3301<?xml version="1.0" encoding="utf-8"?>3302<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3303<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3304<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3305<!-- git core binaries version$git_version-->3306<head>3307<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3308<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3309<meta name="robots" content="index, nofollow"/>3310<title>$title</title>3311EOF3312# the stylesheet, favicon etc urls won't work correctly with path_info3313# unless we set the appropriate base URL3314if($ENV{'PATH_INFO'}) {3315print"<base href=\"".esc_url($base_url)."\"/>\n";3316}3317# print out each stylesheet that exist, providing backwards capability3318# for those people who defined $stylesheet in a config file3319if(defined$stylesheet) {3320print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3321}else{3322foreachmy$stylesheet(@stylesheets) {3323next unless$stylesheet;3324print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3325}3326}3327if(defined$project) {3328my%href_params= get_feed_info();3329if(!exists$href_params{'-title'}) {3330$href_params{'-title'} ='log';3331}33323333foreachmy$formatqw(RSS Atom){3334my$type=lc($format);3335my%link_attr= (3336'-rel'=>'alternate',3337'-title'=>"$project-$href_params{'-title'} -$formatfeed",3338'-type'=>"application/$type+xml"3339);33403341$href_params{'action'} =$type;3342$link_attr{'-href'} = href(%href_params);3343print"<link ".3344"rel=\"$link_attr{'-rel'}\"".3345"title=\"$link_attr{'-title'}\"".3346"href=\"$link_attr{'-href'}\"".3347"type=\"$link_attr{'-type'}\"".3348"/>\n";33493350$href_params{'extra_options'} ='--no-merges';3351$link_attr{'-href'} = href(%href_params);3352$link_attr{'-title'} .=' (no merges)';3353print"<link ".3354"rel=\"$link_attr{'-rel'}\"".3355"title=\"$link_attr{'-title'}\"".3356"href=\"$link_attr{'-href'}\"".3357"type=\"$link_attr{'-type'}\"".3358"/>\n";3359}33603361}else{3362printf('<link rel="alternate" title="%sprojects list" '.3363'href="%s" type="text/plain; charset=utf-8" />'."\n",3364$site_name, href(project=>undef, action=>"project_index"));3365printf('<link rel="alternate" title="%sprojects feeds" '.3366'href="%s" type="text/x-opml" />'."\n",3367$site_name, href(project=>undef, action=>"opml"));3368}3369if(defined$favicon) {3370printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3371}33723373print"</head>\n".3374"<body>\n";33753376if(defined$site_header&& -f $site_header) {3377 insert_file($site_header);3378}33793380print"<div class=\"page_header\">\n".3381$cgi->a({-href => esc_url($logo_url),3382-title =>$logo_label},3383qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3384print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3385if(defined$project) {3386print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3387if(defined$action) {3388print" /$action";3389}3390print"\n";3391}3392print"</div>\n";33933394my$have_search= gitweb_check_feature('search');3395if(defined$project&&$have_search) {3396if(!defined$searchtext) {3397$searchtext="";3398}3399my$search_hash;3400if(defined$hash_base) {3401$search_hash=$hash_base;3402}elsif(defined$hash) {3403$search_hash=$hash;3404}else{3405$search_hash="HEAD";3406}3407my$action=$my_uri;3408my$use_pathinfo= gitweb_check_feature('pathinfo');3409if($use_pathinfo) {3410$action.="/".esc_url($project);3411}3412print$cgi->startform(-method=>"get", -action =>$action) .3413"<div class=\"search\">\n".3414(!$use_pathinfo&&3415$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3416$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3417$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3418$cgi->popup_menu(-name =>'st', -default=>'commit',3419-values=> ['commit','grep','author','committer','pickaxe']) .3420$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3421" search:\n",3422$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3423"<span title=\"Extended regular expression\">".3424$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3425-checked =>$search_use_regexp) .3426"</span>".3427"</div>".3428$cgi->end_form() ."\n";3429}3430}34313432sub git_footer_html {3433my$feed_class='rss_logo';34343435print"<div class=\"page_footer\">\n";3436if(defined$project) {3437my$descr= git_get_project_description($project);3438if(defined$descr) {3439print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3440}34413442my%href_params= get_feed_info();3443if(!%href_params) {3444$feed_class.=' generic';3445}3446$href_params{'-title'} ||='log';34473448foreachmy$formatqw(RSS Atom){3449$href_params{'action'} =lc($format);3450print$cgi->a({-href => href(%href_params),3451-title =>"$href_params{'-title'}$formatfeed",3452-class=>$feed_class},$format)."\n";3453}34543455}else{3456print$cgi->a({-href => href(project=>undef, action=>"opml"),3457-class=>$feed_class},"OPML") ." ";3458print$cgi->a({-href => href(project=>undef, action=>"project_index"),3459-class=>$feed_class},"TXT") ."\n";3460}3461print"</div>\n";# class="page_footer"34623463if(defined$t0&& gitweb_check_feature('timed')) {3464print"<div id=\"generating_info\">\n";3465print'This page took '.3466'<span id="generating_time" class="time_span">'.3467 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3468' seconds </span>'.3469' and '.3470'<span id="generating_cmd">'.3471$number_of_git_cmds.3472'</span> git commands '.3473" to generate.\n";3474print"</div>\n";# class="page_footer"3475}34763477if(defined$site_footer&& -f $site_footer) {3478 insert_file($site_footer);3479}34803481print qq!<script type="text/javascript" src="$javascript"></script>\n!;3482if(defined$action&&3483$actioneq'blame_incremental') {3484print qq!<script type="text/javascript">\n!.3485 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3486 qq!"!. href() .qq!");\n!.3487 qq!</script>\n!;3488}elsif(gitweb_check_feature('javascript-actions')) {3489print qq!<script type="text/javascript">\n!.3490 qq!window.onload = fixLinks;\n!.3491 qq!</script>\n!;3492}34933494print"</body>\n".3495"</html>";3496}34973498# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3499# Example: die_error(404, 'Hash not found')3500# By convention, use the following status codes (as defined in RFC 2616):3501# 400: Invalid or missing CGI parameters, or3502# requested object exists but has wrong type.3503# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3504# this server or project.3505# 404: Requested object/revision/project doesn't exist.3506# 500: The server isn't configured properly, or3507# an internal error occurred (e.g. failed assertions caused by bugs), or3508# an unknown error occurred (e.g. the git binary died unexpectedly).3509# 503: The server is currently unavailable (because it is overloaded,3510# or down for maintenance). Generally, this is a temporary state.3511sub die_error {3512my$status=shift||500;3513my$error= esc_html(shift) ||"Internal Server Error";3514my$extra=shift;35153516my%http_responses= (3517400=>'400 Bad Request',3518403=>'403 Forbidden',3519404=>'404 Not Found',3520500=>'500 Internal Server Error',3521503=>'503 Service Unavailable',3522);3523 git_header_html($http_responses{$status});3524print<<EOF;3525<div class="page_body">3526<br /><br />3527$status-$error3528<br />3529EOF3530if(defined$extra) {3531print"<hr />\n".3532"$extra\n";3533}3534print"</div>\n";35353536 git_footer_html();3537goto DONE_GITWEB;3538}35393540## ----------------------------------------------------------------------3541## functions printing or outputting HTML: navigation35423543sub git_print_page_nav {3544my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3545$extra=''if!defined$extra;# pager or formats35463547my@navs=qw(summary shortlog log commit commitdiff tree);3548if($suppress) {3549@navs=grep{$_ne$suppress}@navs;3550}35513552my%arg=map{$_=> {action=>$_} }@navs;3553if(defined$head) {3554for(qw(commit commitdiff)) {3555$arg{$_}{'hash'} =$head;3556}3557if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3558for(qw(shortlog log)) {3559$arg{$_}{'hash'} =$head;3560}3561}3562}35633564$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3565$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;35663567my@actions= gitweb_get_feature('actions');3568my%repl= (3569'%'=>'%',3570'n'=>$project,# project name3571'f'=>$git_dir,# project path within filesystem3572'h'=>$treehead||'',# current hash ('h' parameter)3573'b'=>$treebase||'',# hash base ('hb' parameter)3574);3575while(@actions) {3576my($label,$link,$pos) =splice(@actions,0,3);3577# insert3578@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3579# munch munch3580$link=~s/%([%nfhb])/$repl{$1}/g;3581$arg{$label}{'_href'} =$link;3582}35833584print"<div class=\"page_nav\">\n".3585(join" | ",3586map{$_eq$current?3587$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3588}@navs);3589print"<br/>\n$extra<br/>\n".3590"</div>\n";3591}35923593sub format_paging_nav {3594my($action,$page,$has_next_link) =@_;3595my$paging_nav;359635973598if($page>0) {3599$paging_nav.=3600$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3601" ⋅ ".3602$cgi->a({-href => href(-replay=>1, page=>$page-1),3603-accesskey =>"p", -title =>"Alt-p"},"prev");3604}else{3605$paging_nav.="first ⋅ prev";3606}36073608if($has_next_link) {3609$paging_nav.=" ⋅ ".3610$cgi->a({-href => href(-replay=>1, page=>$page+1),3611-accesskey =>"n", -title =>"Alt-n"},"next");3612}else{3613$paging_nav.=" ⋅ next";3614}36153616return$paging_nav;3617}36183619## ......................................................................3620## functions printing or outputting HTML: div36213622sub git_print_header_div {3623my($action,$title,$hash,$hash_base) =@_;3624my%args= ();36253626$args{'action'} =$action;3627$args{'hash'} =$hashif$hash;3628$args{'hash_base'} =$hash_baseif$hash_base;36293630print"<div class=\"header\">\n".3631$cgi->a({-href => href(%args), -class=>"title"},3632$title?$title:$action) .3633"\n</div>\n";3634}36353636sub print_local_time {3637print format_local_time(@_);3638}36393640sub format_local_time {3641my$localtime='';3642my%date=@_;3643if($date{'hour_local'} <6) {3644$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3645$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3646}else{3647$localtime.=sprintf(" (%02d:%02d%s)",3648$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3649}36503651return$localtime;3652}36533654# Outputs the author name and date in long form3655sub git_print_authorship {3656my$co=shift;3657my%opts=@_;3658my$tag=$opts{-tag} ||'div';3659my$author=$co->{'author_name'};36603661my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3662print"<$tagclass=\"author_date\">".3663 format_search_author($author,"author", esc_html($author)) .3664" [$ad{'rfc2822'}";3665 print_local_time(%ad)if($opts{-localtime});3666print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3667."</$tag>\n";3668}36693670# Outputs table rows containing the full author or committer information,3671# in the format expected for 'commit' view (& similia).3672# Parameters are a commit hash reference, followed by the list of people3673# to output information for. If the list is empty it defalts to both3674# author and committer.3675sub git_print_authorship_rows {3676my$co=shift;3677# too bad we can't use @people = @_ || ('author', 'committer')3678my@people=@_;3679@people= ('author','committer')unless@people;3680foreachmy$who(@people) {3681my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3682print"<tr><td>$who</td><td>".3683 format_search_author($co->{"${who}_name"},$who,3684 esc_html($co->{"${who}_name"})) ." ".3685 format_search_author($co->{"${who}_email"},$who,3686 esc_html("<".$co->{"${who}_email"} .">")) .3687"</td><td rowspan=\"2\">".3688 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3689"</td></tr>\n".3690"<tr>".3691"<td></td><td>$wd{'rfc2822'}";3692 print_local_time(%wd);3693print"</td>".3694"</tr>\n";3695}3696}36973698sub git_print_page_path {3699my$name=shift;3700my$type=shift;3701my$hb=shift;370237033704print"<div class=\"page_path\">";3705print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3706-title =>'tree root'}, to_utf8("[$project]"));3707print" / ";3708if(defined$name) {3709my@dirname=split'/',$name;3710my$basename=pop@dirname;3711my$fullname='';37123713foreachmy$dir(@dirname) {3714$fullname.= ($fullname?'/':'') .$dir;3715print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3716 hash_base=>$hb),3717-title =>$fullname}, esc_path($dir));3718print" / ";3719}3720if(defined$type&&$typeeq'blob') {3721print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3722 hash_base=>$hb),3723-title =>$name}, esc_path($basename));3724}elsif(defined$type&&$typeeq'tree') {3725print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3726 hash_base=>$hb),3727-title =>$name}, esc_path($basename));3728print" / ";3729}else{3730print esc_path($basename);3731}3732}3733print"<br/></div>\n";3734}37353736sub git_print_log {3737my$log=shift;3738my%opts=@_;37393740if($opts{'-remove_title'}) {3741# remove title, i.e. first line of log3742shift@$log;3743}3744# remove leading empty lines3745while(defined$log->[0] &&$log->[0]eq"") {3746shift@$log;3747}37483749# print log3750my$signoff=0;3751my$empty=0;3752foreachmy$line(@$log) {3753if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3754$signoff=1;3755$empty=0;3756if(!$opts{'-remove_signoff'}) {3757print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3758next;3759}else{3760# remove signoff lines3761next;3762}3763}else{3764$signoff=0;3765}37663767# print only one empty line3768# do not print empty line after signoff3769if($lineeq"") {3770next if($empty||$signoff);3771$empty=1;3772}else{3773$empty=0;3774}37753776print format_log_line_html($line) ."<br/>\n";3777}37783779if($opts{'-final_empty_line'}) {3780# end with single empty line3781print"<br/>\n"unless$empty;3782}3783}37843785# return link target (what link points to)3786sub git_get_link_target {3787my$hash=shift;3788my$link_target;37893790# read link3791open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3792orreturn;3793{3794local$/=undef;3795$link_target= <$fd>;3796}3797close$fd3798orreturn;37993800return$link_target;3801}38023803# given link target, and the directory (basedir) the link is in,3804# return target of link relative to top directory (top tree);3805# return undef if it is not possible (including absolute links).3806sub normalize_link_target {3807my($link_target,$basedir) =@_;38083809# absolute symlinks (beginning with '/') cannot be normalized3810return if(substr($link_target,0,1)eq'/');38113812# normalize link target to path from top (root) tree (dir)3813my$path;3814if($basedir) {3815$path=$basedir.'/'.$link_target;3816}else{3817# we are in top (root) tree (dir)3818$path=$link_target;3819}38203821# remove //, /./, and /../3822my@path_parts;3823foreachmy$part(split('/',$path)) {3824# discard '.' and ''3825next if(!$part||$parteq'.');3826# handle '..'3827if($parteq'..') {3828if(@path_parts) {3829pop@path_parts;3830}else{3831# link leads outside repository (outside top dir)3832return;3833}3834}else{3835push@path_parts,$part;3836}3837}3838$path=join('/',@path_parts);38393840return$path;3841}38423843# print tree entry (row of git_tree), but without encompassing <tr> element3844sub git_print_tree_entry {3845my($t,$basedir,$hash_base,$have_blame) =@_;38463847my%base_key= ();3848$base_key{'hash_base'} =$hash_baseifdefined$hash_base;38493850# The format of a table row is: mode list link. Where mode is3851# the mode of the entry, list is the name of the entry, an href,3852# and link is the action links of the entry.38533854print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3855if(exists$t->{'size'}) {3856print"<td class=\"size\">$t->{'size'}</td>\n";3857}3858if($t->{'type'}eq"blob") {3859print"<td class=\"list\">".3860$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3861 file_name=>"$basedir$t->{'name'}",%base_key),3862-class=>"list"}, esc_path($t->{'name'}));3863if(S_ISLNK(oct$t->{'mode'})) {3864my$link_target= git_get_link_target($t->{'hash'});3865if($link_target) {3866my$norm_target= normalize_link_target($link_target,$basedir);3867if(defined$norm_target) {3868print" -> ".3869$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3870 file_name=>$norm_target),3871-title =>$norm_target}, esc_path($link_target));3872}else{3873print" -> ". esc_path($link_target);3874}3875}3876}3877print"</td>\n";3878print"<td class=\"link\">";3879print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3880 file_name=>"$basedir$t->{'name'}",%base_key)},3881"blob");3882if($have_blame) {3883print" | ".3884$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3885 file_name=>"$basedir$t->{'name'}",%base_key)},3886"blame");3887}3888if(defined$hash_base) {3889print" | ".3890$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3891 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3892"history");3893}3894print" | ".3895$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3896 file_name=>"$basedir$t->{'name'}")},3897"raw");3898print"</td>\n";38993900}elsif($t->{'type'}eq"tree") {3901print"<td class=\"list\">";3902print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3903 file_name=>"$basedir$t->{'name'}",3904%base_key)},3905 esc_path($t->{'name'}));3906print"</td>\n";3907print"<td class=\"link\">";3908print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3909 file_name=>"$basedir$t->{'name'}",3910%base_key)},3911"tree");3912if(defined$hash_base) {3913print" | ".3914$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3915 file_name=>"$basedir$t->{'name'}")},3916"history");3917}3918print"</td>\n";3919}else{3920# unknown object: we can only present history for it3921# (this includes 'commit' object, i.e. submodule support)3922print"<td class=\"list\">".3923 esc_path($t->{'name'}) .3924"</td>\n";3925print"<td class=\"link\">";3926if(defined$hash_base) {3927print$cgi->a({-href => href(action=>"history",3928 hash_base=>$hash_base,3929 file_name=>"$basedir$t->{'name'}")},3930"history");3931}3932print"</td>\n";3933}3934}39353936## ......................................................................3937## functions printing large fragments of HTML39383939# get pre-image filenames for merge (combined) diff3940sub fill_from_file_info {3941my($diff,@parents) =@_;39423943$diff->{'from_file'} = [ ];3944$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3945for(my$i=0;$i<$diff->{'nparents'};$i++) {3946if($diff->{'status'}[$i]eq'R'||3947$diff->{'status'}[$i]eq'C') {3948$diff->{'from_file'}[$i] =3949 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3950}3951}39523953return$diff;3954}39553956# is current raw difftree line of file deletion3957sub is_deleted {3958my$diffinfo=shift;39593960return$diffinfo->{'to_id'}eq('0' x 40);3961}39623963# does patch correspond to [previous] difftree raw line3964# $diffinfo - hashref of parsed raw diff format3965# $patchinfo - hashref of parsed patch diff format3966# (the same keys as in $diffinfo)3967sub is_patch_split {3968my($diffinfo,$patchinfo) =@_;39693970returndefined$diffinfo&&defined$patchinfo3971&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3972}397339743975sub git_difftree_body {3976my($difftree,$hash,@parents) =@_;3977my($parent) =$parents[0];3978my$have_blame= gitweb_check_feature('blame');3979print"<div class=\"list_head\">\n";3980if($#{$difftree} >10) {3981print(($#{$difftree} +1) ." files changed:\n");3982}3983print"</div>\n";39843985print"<table class=\"".3986(@parents>1?"combined ":"") .3987"diff_tree\">\n";39883989# header only for combined diff in 'commitdiff' view3990my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3991if($has_header) {3992# table header3993print"<thead><tr>\n".3994"<th></th><th></th>\n";# filename, patchN link3995for(my$i=0;$i<@parents;$i++) {3996my$par=$parents[$i];3997print"<th>".3998$cgi->a({-href => href(action=>"commitdiff",3999 hash=>$hash, hash_parent=>$par),4000-title =>'commitdiff to parent number '.4001($i+1) .': '.substr($par,0,7)},4002$i+1) .4003" </th>\n";4004}4005print"</tr></thead>\n<tbody>\n";4006}40074008my$alternate=1;4009my$patchno=0;4010foreachmy$line(@{$difftree}) {4011my$diff= parsed_difftree_line($line);40124013if($alternate) {4014print"<tr class=\"dark\">\n";4015}else{4016print"<tr class=\"light\">\n";4017}4018$alternate^=1;40194020if(exists$diff->{'nparents'}) {# combined diff40214022 fill_from_file_info($diff,@parents)4023unlessexists$diff->{'from_file'};40244025if(!is_deleted($diff)) {4026# file exists in the result (child) commit4027print"<td>".4028$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4029 file_name=>$diff->{'to_file'},4030 hash_base=>$hash),4031-class=>"list"}, esc_path($diff->{'to_file'})) .4032"</td>\n";4033}else{4034print"<td>".4035 esc_path($diff->{'to_file'}) .4036"</td>\n";4037}40384039if($actioneq'commitdiff') {4040# link to patch4041$patchno++;4042print"<td class=\"link\">".4043$cgi->a({-href =>"#patch$patchno"},"patch") .4044" | ".4045"</td>\n";4046}40474048my$has_history=0;4049my$not_deleted=0;4050for(my$i=0;$i<$diff->{'nparents'};$i++) {4051my$hash_parent=$parents[$i];4052my$from_hash=$diff->{'from_id'}[$i];4053my$from_path=$diff->{'from_file'}[$i];4054my$status=$diff->{'status'}[$i];40554056$has_history||= ($statusne'A');4057$not_deleted||= ($statusne'D');40584059if($statuseq'A') {4060print"<td class=\"link\"align=\"right\"> | </td>\n";4061}elsif($statuseq'D') {4062print"<td class=\"link\">".4063$cgi->a({-href => href(action=>"blob",4064 hash_base=>$hash,4065 hash=>$from_hash,4066 file_name=>$from_path)},4067"blob". ($i+1)) .4068" | </td>\n";4069}else{4070if($diff->{'to_id'}eq$from_hash) {4071print"<td class=\"link nochange\">";4072}else{4073print"<td class=\"link\">";4074}4075print$cgi->a({-href => href(action=>"blobdiff",4076 hash=>$diff->{'to_id'},4077 hash_parent=>$from_hash,4078 hash_base=>$hash,4079 hash_parent_base=>$hash_parent,4080 file_name=>$diff->{'to_file'},4081 file_parent=>$from_path)},4082"diff". ($i+1)) .4083" | </td>\n";4084}4085}40864087print"<td class=\"link\">";4088if($not_deleted) {4089print$cgi->a({-href => href(action=>"blob",4090 hash=>$diff->{'to_id'},4091 file_name=>$diff->{'to_file'},4092 hash_base=>$hash)},4093"blob");4094print" | "if($has_history);4095}4096if($has_history) {4097print$cgi->a({-href => href(action=>"history",4098 file_name=>$diff->{'to_file'},4099 hash_base=>$hash)},4100"history");4101}4102print"</td>\n";41034104print"</tr>\n";4105next;# instead of 'else' clause, to avoid extra indent4106}4107# else ordinary diff41084109my($to_mode_oct,$to_mode_str,$to_file_type);4110my($from_mode_oct,$from_mode_str,$from_file_type);4111if($diff->{'to_mode'}ne('0' x 6)) {4112$to_mode_oct=oct$diff->{'to_mode'};4113if(S_ISREG($to_mode_oct)) {# only for regular file4114$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4115}4116$to_file_type= file_type($diff->{'to_mode'});4117}4118if($diff->{'from_mode'}ne('0' x 6)) {4119$from_mode_oct=oct$diff->{'from_mode'};4120if(S_ISREG($to_mode_oct)) {# only for regular file4121$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4122}4123$from_file_type= file_type($diff->{'from_mode'});4124}41254126if($diff->{'status'}eq"A") {# created4127my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4128$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4129$mode_chng.="]</span>";4130print"<td>";4131print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4132 hash_base=>$hash, file_name=>$diff->{'file'}),4133-class=>"list"}, esc_path($diff->{'file'}));4134print"</td>\n";4135print"<td>$mode_chng</td>\n";4136print"<td class=\"link\">";4137if($actioneq'commitdiff') {4138# link to patch4139$patchno++;4140print$cgi->a({-href =>"#patch$patchno"},"patch");4141print" | ";4142}4143print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4144 hash_base=>$hash, file_name=>$diff->{'file'})},4145"blob");4146print"</td>\n";41474148}elsif($diff->{'status'}eq"D") {# deleted4149my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4150print"<td>";4151print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4152 hash_base=>$parent, file_name=>$diff->{'file'}),4153-class=>"list"}, esc_path($diff->{'file'}));4154print"</td>\n";4155print"<td>$mode_chng</td>\n";4156print"<td class=\"link\">";4157if($actioneq'commitdiff') {4158# link to patch4159$patchno++;4160print$cgi->a({-href =>"#patch$patchno"},"patch");4161print" | ";4162}4163print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4164 hash_base=>$parent, file_name=>$diff->{'file'})},4165"blob") ." | ";4166if($have_blame) {4167print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4168 file_name=>$diff->{'file'})},4169"blame") ." | ";4170}4171print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4172 file_name=>$diff->{'file'})},4173"history");4174print"</td>\n";41754176}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4177my$mode_chnge="";4178if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4179$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4180if($from_file_typene$to_file_type) {4181$mode_chnge.=" from$from_file_typeto$to_file_type";4182}4183if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4184if($from_mode_str&&$to_mode_str) {4185$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4186}elsif($to_mode_str) {4187$mode_chnge.=" mode:$to_mode_str";4188}4189}4190$mode_chnge.="]</span>\n";4191}4192print"<td>";4193print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4194 hash_base=>$hash, file_name=>$diff->{'file'}),4195-class=>"list"}, esc_path($diff->{'file'}));4196print"</td>\n";4197print"<td>$mode_chnge</td>\n";4198print"<td class=\"link\">";4199if($actioneq'commitdiff') {4200# link to patch4201$patchno++;4202print$cgi->a({-href =>"#patch$patchno"},"patch") .4203" | ";4204}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4205# "commit" view and modified file (not onlu mode changed)4206print$cgi->a({-href => href(action=>"blobdiff",4207 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4208 hash_base=>$hash, hash_parent_base=>$parent,4209 file_name=>$diff->{'file'})},4210"diff") .4211" | ";4212}4213print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4214 hash_base=>$hash, file_name=>$diff->{'file'})},4215"blob") ." | ";4216if($have_blame) {4217print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4218 file_name=>$diff->{'file'})},4219"blame") ." | ";4220}4221print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4222 file_name=>$diff->{'file'})},4223"history");4224print"</td>\n";42254226}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4227my%status_name= ('R'=>'moved','C'=>'copied');4228my$nstatus=$status_name{$diff->{'status'}};4229my$mode_chng="";4230if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4231# mode also for directories, so we cannot use $to_mode_str4232$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4233}4234print"<td>".4235$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4236 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4237-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4238"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4239$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4240 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4241-class=>"list"}, esc_path($diff->{'from_file'})) .4242" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4243"<td class=\"link\">";4244if($actioneq'commitdiff') {4245# link to patch4246$patchno++;4247print$cgi->a({-href =>"#patch$patchno"},"patch") .4248" | ";4249}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4250# "commit" view and modified file (not only pure rename or copy)4251print$cgi->a({-href => href(action=>"blobdiff",4252 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4253 hash_base=>$hash, hash_parent_base=>$parent,4254 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4255"diff") .4256" | ";4257}4258print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4259 hash_base=>$parent, file_name=>$diff->{'to_file'})},4260"blob") ." | ";4261if($have_blame) {4262print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4263 file_name=>$diff->{'to_file'})},4264"blame") ." | ";4265}4266print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4267 file_name=>$diff->{'to_file'})},4268"history");4269print"</td>\n";42704271}# we should not encounter Unmerged (U) or Unknown (X) status4272print"</tr>\n";4273}4274print"</tbody>"if$has_header;4275print"</table>\n";4276}42774278sub git_patchset_body {4279my($fd,$difftree,$hash,@hash_parents) =@_;4280my($hash_parent) =$hash_parents[0];42814282my$is_combined= (@hash_parents>1);4283my$patch_idx=0;4284my$patch_number=0;4285my$patch_line;4286my$diffinfo;4287my$to_name;4288my(%from,%to);42894290print"<div class=\"patchset\">\n";42914292# skip to first patch4293while($patch_line= <$fd>) {4294chomp$patch_line;42954296last if($patch_line=~m/^diff /);4297}42984299 PATCH:4300while($patch_line) {43014302# parse "git diff" header line4303if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4304# $1 is from_name, which we do not use4305$to_name= unquote($2);4306$to_name=~s!^b/!!;4307}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4308# $1 is 'cc' or 'combined', which we do not use4309$to_name= unquote($2);4310}else{4311$to_name=undef;4312}43134314# check if current patch belong to current raw line4315# and parse raw git-diff line if needed4316if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4317# this is continuation of a split patch4318print"<div class=\"patch cont\">\n";4319}else{4320# advance raw git-diff output if needed4321$patch_idx++ifdefined$diffinfo;43224323# read and prepare patch information4324$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);43254326# compact combined diff output can have some patches skipped4327# find which patch (using pathname of result) we are at now;4328if($is_combined) {4329while($to_namene$diffinfo->{'to_file'}) {4330print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4331 format_diff_cc_simplified($diffinfo,@hash_parents) .4332"</div>\n";# class="patch"43334334$patch_idx++;4335$patch_number++;43364337last if$patch_idx>$#$difftree;4338$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4339}4340}43414342# modifies %from, %to hashes4343 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);43444345# this is first patch for raw difftree line with $patch_idx index4346# we index @$difftree array from 0, but number patches from 14347print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4348}43494350# git diff header4351#assert($patch_line =~ m/^diff /) if DEBUG;4352#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4353$patch_number++;4354# print "git diff" header4355print format_git_diff_header_line($patch_line,$diffinfo,4356 \%from, \%to);43574358# print extended diff header4359print"<div class=\"diff extended_header\">\n";4360 EXTENDED_HEADER:4361while($patch_line= <$fd>) {4362chomp$patch_line;43634364last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);43654366print format_extended_diff_header_line($patch_line,$diffinfo,4367 \%from, \%to);4368}4369print"</div>\n";# class="diff extended_header"43704371# from-file/to-file diff header4372if(!$patch_line) {4373print"</div>\n";# class="patch"4374last PATCH;4375}4376next PATCH if($patch_line=~m/^diff /);4377#assert($patch_line =~ m/^---/) if DEBUG;43784379my$last_patch_line=$patch_line;4380$patch_line= <$fd>;4381chomp$patch_line;4382#assert($patch_line =~ m/^\+\+\+/) if DEBUG;43834384print format_diff_from_to_header($last_patch_line,$patch_line,4385$diffinfo, \%from, \%to,4386@hash_parents);43874388# the patch itself4389 LINE:4390while($patch_line= <$fd>) {4391chomp$patch_line;43924393next PATCH if($patch_line=~m/^diff /);43944395print format_diff_line($patch_line, \%from, \%to);4396}43974398}continue{4399print"</div>\n";# class="patch"4400}44014402# for compact combined (--cc) format, with chunk and patch simpliciaction4403# patchset might be empty, but there might be unprocessed raw lines4404for(++$patch_idxif$patch_number>0;4405$patch_idx<@$difftree;4406++$patch_idx) {4407# read and prepare patch information4408$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);44094410# generate anchor for "patch" links in difftree / whatchanged part4411print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4412 format_diff_cc_simplified($diffinfo,@hash_parents) .4413"</div>\n";# class="patch"44144415$patch_number++;4416}44174418if($patch_number==0) {4419if(@hash_parents>1) {4420print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4421}else{4422print"<div class=\"diff nodifferences\">No differences found</div>\n";4423}4424}44254426print"</div>\n";# class="patchset"4427}44284429# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44304431# fills project list info (age, description, owner, forks) for each4432# project in the list, removing invalid projects from returned list4433# NOTE: modifies $projlist, but does not remove entries from it4434sub fill_project_list_info {4435my($projlist,$check_forks) =@_;4436my@projects;44374438my$show_ctags= gitweb_check_feature('ctags');4439 PROJECT:4440foreachmy$pr(@$projlist) {4441my(@activity) = git_get_last_activity($pr->{'path'});4442unless(@activity) {4443next PROJECT;4444}4445($pr->{'age'},$pr->{'age_string'}) =@activity;4446if(!defined$pr->{'descr'}) {4447my$descr= git_get_project_description($pr->{'path'}) ||"";4448$descr= to_utf8($descr);4449$pr->{'descr_long'} =$descr;4450$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4451}4452if(!defined$pr->{'owner'}) {4453$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4454}4455if($check_forks) {4456my$pname=$pr->{'path'};4457if(($pname=~s/\.git$//) &&4458($pname!~/\/$/) &&4459(-d "$projectroot/$pname")) {4460$pr->{'forks'} ="-d$projectroot/$pname";4461}else{4462$pr->{'forks'} =0;4463}4464}4465$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4466push@projects,$pr;4467}44684469return@projects;4470}44714472# print 'sort by' <th> element, generating 'sort by $name' replay link4473# if that order is not selected4474sub print_sort_th {4475print format_sort_th(@_);4476}44774478sub format_sort_th {4479my($name,$order,$header) =@_;4480my$sort_th="";4481$header||=ucfirst($name);44824483if($ordereq$name) {4484$sort_th.="<th>$header</th>\n";4485}else{4486$sort_th.="<th>".4487$cgi->a({-href => href(-replay=>1, order=>$name),4488-class=>"header"},$header) .4489"</th>\n";4490}44914492return$sort_th;4493}44944495sub git_project_list_body {4496# actually uses global variable $project4497my($projlist,$order,$from,$to,$extra,$no_header) =@_;44984499my$check_forks= gitweb_check_feature('forks');4500my@projects= fill_project_list_info($projlist,$check_forks);45014502$order||=$default_projects_order;4503$from=0unlessdefined$from;4504$to=$#projectsif(!defined$to||$#projects<$to);45054506my%order_info= (4507 project => { key =>'path', type =>'str'},4508 descr => { key =>'descr_long', type =>'str'},4509 owner => { key =>'owner', type =>'str'},4510 age => { key =>'age', type =>'num'}4511);4512my$oi=$order_info{$order};4513if($oi->{'type'}eq'str') {4514@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4515}else{4516@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4517}45184519my$show_ctags= gitweb_check_feature('ctags');4520if($show_ctags) {4521my%ctags;4522foreachmy$p(@projects) {4523foreachmy$ct(keys%{$p->{'ctags'}}) {4524$ctags{$ct} +=$p->{'ctags'}->{$ct};4525}4526}4527my$cloud= git_populate_project_tagcloud(\%ctags);4528print git_show_project_tagcloud($cloud,64);4529}45304531print"<table class=\"project_list\">\n";4532unless($no_header) {4533print"<tr>\n";4534if($check_forks) {4535print"<th></th>\n";4536}4537 print_sort_th('project',$order,'Project');4538 print_sort_th('descr',$order,'Description');4539 print_sort_th('owner',$order,'Owner');4540 print_sort_th('age',$order,'Last Change');4541print"<th></th>\n".# for links4542"</tr>\n";4543}4544my$alternate=1;4545my$tagfilter=$cgi->param('by_tag');4546for(my$i=$from;$i<=$to;$i++) {4547my$pr=$projects[$i];45484549next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4550next if$searchtextand not$pr->{'path'} =~/$searchtext/4551and not$pr->{'descr_long'} =~/$searchtext/;4552# Weed out forks or non-matching entries of search4553if($check_forks) {4554my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4555$forkbase="^$forkbase"if$forkbase;4556next ifnot$searchtextand not$tagfilterand$show_ctags4557and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4558}45594560if($alternate) {4561print"<tr class=\"dark\">\n";4562}else{4563print"<tr class=\"light\">\n";4564}4565$alternate^=1;4566if($check_forks) {4567print"<td>";4568if($pr->{'forks'}) {4569print"<!--$pr->{'forks'} -->\n";4570print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4571}4572print"</td>\n";4573}4574print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4575-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4576"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4577-class=>"list", -title =>$pr->{'descr_long'}},4578 esc_html($pr->{'descr'})) ."</td>\n".4579"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4580print"<td class=\"". age_class($pr->{'age'}) ."\">".4581(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4582"<td class=\"link\">".4583$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4584$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4585$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4586$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4587($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4588"</td>\n".4589"</tr>\n";4590}4591if(defined$extra) {4592print"<tr>\n";4593if($check_forks) {4594print"<td></td>\n";4595}4596print"<td colspan=\"5\">$extra</td>\n".4597"</tr>\n";4598}4599print"</table>\n";4600}46014602sub git_log_body {4603# uses global variable $project4604my($commitlist,$from,$to,$refs,$extra) =@_;46054606$from=0unlessdefined$from;4607$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);46084609for(my$i=0;$i<=$to;$i++) {4610my%co= %{$commitlist->[$i]};4611next if!%co;4612my$commit=$co{'id'};4613my$ref= format_ref_marker($refs,$commit);4614my%ad= parse_date($co{'author_epoch'});4615 git_print_header_div('commit',4616"<span class=\"age\">$co{'age_string'}</span>".4617 esc_html($co{'title'}) .$ref,4618$commit);4619print"<div class=\"title_text\">\n".4620"<div class=\"log_link\">\n".4621$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4622" | ".4623$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4624" | ".4625$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4626"<br/>\n".4627"</div>\n";4628 git_print_authorship(\%co, -tag =>'span');4629print"<br/>\n</div>\n";46304631print"<div class=\"log_body\">\n";4632 git_print_log($co{'comment'}, -final_empty_line=>1);4633print"</div>\n";4634}4635if($extra) {4636print"<div class=\"page_nav\">\n";4637print"$extra\n";4638print"</div>\n";4639}4640}46414642sub git_shortlog_body {4643# uses global variable $project4644my($commitlist,$from,$to,$refs,$extra) =@_;46454646$from=0unlessdefined$from;4647$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);46484649print"<table class=\"shortlog\">\n";4650my$alternate=1;4651for(my$i=$from;$i<=$to;$i++) {4652my%co= %{$commitlist->[$i]};4653my$commit=$co{'id'};4654my$ref= format_ref_marker($refs,$commit);4655if($alternate) {4656print"<tr class=\"dark\">\n";4657}else{4658print"<tr class=\"light\">\n";4659}4660$alternate^=1;4661# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4662print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4663 format_author_html('td', \%co,10) ."<td>";4664print format_subject_html($co{'title'},$co{'title_short'},4665 href(action=>"commit", hash=>$commit),$ref);4666print"</td>\n".4667"<td class=\"link\">".4668$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4669$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4670$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4671my$snapshot_links= format_snapshot_links($commit);4672if(defined$snapshot_links) {4673print" | ".$snapshot_links;4674}4675print"</td>\n".4676"</tr>\n";4677}4678if(defined$extra) {4679print"<tr>\n".4680"<td colspan=\"4\">$extra</td>\n".4681"</tr>\n";4682}4683print"</table>\n";4684}46854686sub git_history_body {4687# Warning: assumes constant type (blob or tree) during history4688my($commitlist,$from,$to,$refs,$extra,4689$file_name,$file_hash,$ftype) =@_;46904691$from=0unlessdefined$from;4692$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});46934694print"<table class=\"history\">\n";4695my$alternate=1;4696for(my$i=$from;$i<=$to;$i++) {4697my%co= %{$commitlist->[$i]};4698if(!%co) {4699next;4700}4701my$commit=$co{'id'};47024703my$ref= format_ref_marker($refs,$commit);47044705if($alternate) {4706print"<tr class=\"dark\">\n";4707}else{4708print"<tr class=\"light\">\n";4709}4710$alternate^=1;4711print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4712# shortlog: format_author_html('td', \%co, 10)4713 format_author_html('td', \%co,15,3) ."<td>";4714# originally git_history used chop_str($co{'title'}, 50)4715print format_subject_html($co{'title'},$co{'title_short'},4716 href(action=>"commit", hash=>$commit),$ref);4717print"</td>\n".4718"<td class=\"link\">".4719$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4720$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");47214722if($ftypeeq'blob') {4723my$blob_current=$file_hash;4724my$blob_parent= git_get_hash_by_path($commit,$file_name);4725if(defined$blob_current&&defined$blob_parent&&4726$blob_currentne$blob_parent) {4727print" | ".4728$cgi->a({-href => href(action=>"blobdiff",4729 hash=>$blob_current, hash_parent=>$blob_parent,4730 hash_base=>$hash_base, hash_parent_base=>$commit,4731 file_name=>$file_name)},4732"diff to current");4733}4734}4735print"</td>\n".4736"</tr>\n";4737}4738if(defined$extra) {4739print"<tr>\n".4740"<td colspan=\"4\">$extra</td>\n".4741"</tr>\n";4742}4743print"</table>\n";4744}47454746sub git_tags_body {4747# uses global variable $project4748my($taglist,$from,$to,$extra) =@_;4749$from=0unlessdefined$from;4750$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);47514752print"<table class=\"tags\">\n";4753my$alternate=1;4754for(my$i=$from;$i<=$to;$i++) {4755my$entry=$taglist->[$i];4756my%tag=%$entry;4757my$comment=$tag{'subject'};4758my$comment_short;4759if(defined$comment) {4760$comment_short= chop_str($comment,30,5);4761}4762if($alternate) {4763print"<tr class=\"dark\">\n";4764}else{4765print"<tr class=\"light\">\n";4766}4767$alternate^=1;4768if(defined$tag{'age'}) {4769print"<td><i>$tag{'age'}</i></td>\n";4770}else{4771print"<td></td>\n";4772}4773print"<td>".4774$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4775-class=>"list name"}, esc_html($tag{'name'})) .4776"</td>\n".4777"<td>";4778if(defined$comment) {4779print format_subject_html($comment,$comment_short,4780 href(action=>"tag", hash=>$tag{'id'}));4781}4782print"</td>\n".4783"<td class=\"selflink\">";4784if($tag{'type'}eq"tag") {4785print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4786}else{4787print" ";4788}4789print"</td>\n".4790"<td class=\"link\">"." | ".4791$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4792if($tag{'reftype'}eq"commit") {4793print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4794" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4795}elsif($tag{'reftype'}eq"blob") {4796print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4797}4798print"</td>\n".4799"</tr>";4800}4801if(defined$extra) {4802print"<tr>\n".4803"<td colspan=\"5\">$extra</td>\n".4804"</tr>\n";4805}4806print"</table>\n";4807}48084809sub git_heads_body {4810# uses global variable $project4811my($headlist,$head,$from,$to,$extra) =@_;4812$from=0unlessdefined$from;4813$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);48144815print"<table class=\"heads\">\n";4816my$alternate=1;4817for(my$i=$from;$i<=$to;$i++) {4818my$entry=$headlist->[$i];4819my%ref=%$entry;4820my$curr=$ref{'id'}eq$head;4821if($alternate) {4822print"<tr class=\"dark\">\n";4823}else{4824print"<tr class=\"light\">\n";4825}4826$alternate^=1;4827print"<td><i>$ref{'age'}</i></td>\n".4828($curr?"<td class=\"current_head\">":"<td>") .4829$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4830-class=>"list name"},esc_html($ref{'name'})) .4831"</td>\n".4832"<td class=\"link\">".4833$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4834$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4835$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4836"</td>\n".4837"</tr>";4838}4839if(defined$extra) {4840print"<tr>\n".4841"<td colspan=\"3\">$extra</td>\n".4842"</tr>\n";4843}4844print"</table>\n";4845}48464847sub git_search_grep_body {4848my($commitlist,$from,$to,$extra) =@_;4849$from=0unlessdefined$from;4850$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);48514852print"<table class=\"commit_search\">\n";4853my$alternate=1;4854for(my$i=$from;$i<=$to;$i++) {4855my%co= %{$commitlist->[$i]};4856if(!%co) {4857next;4858}4859my$commit=$co{'id'};4860if($alternate) {4861print"<tr class=\"dark\">\n";4862}else{4863print"<tr class=\"light\">\n";4864}4865$alternate^=1;4866print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4867 format_author_html('td', \%co,15,5) .4868"<td>".4869$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4870-class=>"list subject"},4871 chop_and_escape_str($co{'title'},50) ."<br/>");4872my$comment=$co{'comment'};4873foreachmy$line(@$comment) {4874if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4875my($lead,$match,$trail) = ($1,$2,$3);4876$match= chop_str($match,70,5,'center');4877my$contextlen=int((80-length($match))/2);4878$contextlen=30if($contextlen>30);4879$lead= chop_str($lead,$contextlen,10,'left');4880$trail= chop_str($trail,$contextlen,10,'right');48814882$lead= esc_html($lead);4883$match= esc_html($match);4884$trail= esc_html($trail);48854886print"$lead<span class=\"match\">$match</span>$trail<br />";4887}4888}4889print"</td>\n".4890"<td class=\"link\">".4891$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4892" | ".4893$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4894" | ".4895$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4896print"</td>\n".4897"</tr>\n";4898}4899if(defined$extra) {4900print"<tr>\n".4901"<td colspan=\"3\">$extra</td>\n".4902"</tr>\n";4903}4904print"</table>\n";4905}49064907## ======================================================================4908## ======================================================================4909## actions49104911sub git_project_list {4912my$order=$input_params{'order'};4913if(defined$order&&$order!~m/none|project|descr|owner|age/) {4914 die_error(400,"Unknown order parameter");4915}49164917my@list= git_get_projects_list();4918if(!@list) {4919 die_error(404,"No projects found");4920}49214922 git_header_html();4923if(defined$home_text&& -f $home_text) {4924print"<div class=\"index_include\">\n";4925 insert_file($home_text);4926print"</div>\n";4927}4928print$cgi->startform(-method=>"get") .4929"<p class=\"projsearch\">Search:\n".4930$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4931"</p>".4932$cgi->end_form() ."\n";4933 git_project_list_body(\@list,$order);4934 git_footer_html();4935}49364937sub git_forks {4938my$order=$input_params{'order'};4939if(defined$order&&$order!~m/none|project|descr|owner|age/) {4940 die_error(400,"Unknown order parameter");4941}49424943my@list= git_get_projects_list($project);4944if(!@list) {4945 die_error(404,"No forks found");4946}49474948 git_header_html();4949 git_print_page_nav('','');4950 git_print_header_div('summary',"$projectforks");4951 git_project_list_body(\@list,$order);4952 git_footer_html();4953}49544955sub git_project_index {4956my@projects= git_get_projects_list($project);49574958print$cgi->header(4959-type =>'text/plain',4960-charset =>'utf-8',4961-content_disposition =>'inline; filename="index.aux"');49624963foreachmy$pr(@projects) {4964if(!exists$pr->{'owner'}) {4965$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4966}49674968my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4969# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4970$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4971$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4972$path=~s/ /\+/g;4973$owner=~s/ /\+/g;49744975print"$path$owner\n";4976}4977}49784979sub git_summary {4980my$descr= git_get_project_description($project) ||"none";4981my%co= parse_commit("HEAD");4982my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4983my$head=$co{'id'};49844985my$owner= git_get_project_owner($project);49864987my$refs= git_get_references();4988# These get_*_list functions return one more to allow us to see if4989# there are more ...4990my@taglist= git_get_tags_list(16);4991my@headlist= git_get_heads_list(16);4992my@forklist;4993my$check_forks= gitweb_check_feature('forks');49944995if($check_forks) {4996@forklist= git_get_projects_list($project);4997}49984999 git_header_html();5000 git_print_page_nav('summary','',$head);50015002print"<div class=\"title\"> </div>\n";5003print"<table class=\"projects_list\">\n".5004"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".5005"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";5006if(defined$cd{'rfc2822'}) {5007print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";5008}50095010# use per project git URL list in $projectroot/$project/cloneurl5011# or make project git URL from git base URL and project name5012my$url_tag="URL";5013my@url_list= git_get_project_url_list($project);5014@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;5015foreachmy$git_url(@url_list) {5016next unless$git_url;5017print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";5018$url_tag="";5019}50205021# Tag cloud5022my$show_ctags= gitweb_check_feature('ctags');5023if($show_ctags) {5024my$ctags= git_get_project_ctags($project);5025my$cloud= git_populate_project_tagcloud($ctags);5026print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";5027print"</td>\n<td>"unless%$ctags;5028print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";5029print"</td>\n<td>"if%$ctags;5030print git_show_project_tagcloud($cloud,48);5031print"</td></tr>";5032}50335034print"</table>\n";50355036# If XSS prevention is on, we don't include README.html.5037# TODO: Allow a readme in some safe format.5038if(!$prevent_xss&& -s "$projectroot/$project/README.html") {5039print"<div class=\"title\">readme</div>\n".5040"<div class=\"readme\">\n";5041 insert_file("$projectroot/$project/README.html");5042print"\n</div>\n";# class="readme"5043}50445045# we need to request one more than 16 (0..15) to check if5046# those 16 are all5047my@commitlist=$head? parse_commits($head,17) : ();5048if(@commitlist) {5049 git_print_header_div('shortlog');5050 git_shortlog_body(\@commitlist,0,15,$refs,5051$#commitlist<=15?undef:5052$cgi->a({-href => href(action=>"shortlog")},"..."));5053}50545055if(@taglist) {5056 git_print_header_div('tags');5057 git_tags_body(\@taglist,0,15,5058$#taglist<=15?undef:5059$cgi->a({-href => href(action=>"tags")},"..."));5060}50615062if(@headlist) {5063 git_print_header_div('heads');5064 git_heads_body(\@headlist,$head,0,15,5065$#headlist<=15?undef:5066$cgi->a({-href => href(action=>"heads")},"..."));5067}50685069if(@forklist) {5070 git_print_header_div('forks');5071 git_project_list_body(\@forklist,'age',0,15,5072$#forklist<=15?undef:5073$cgi->a({-href => href(action=>"forks")},"..."),5074'no_header');5075}50765077 git_footer_html();5078}50795080sub git_tag {5081my$head= git_get_head_hash($project);5082 git_header_html();5083 git_print_page_nav('','',$head,undef,$head);5084my%tag= parse_tag($hash);50855086if(!%tag) {5087 die_error(404,"Unknown tag object");5088}50895090 git_print_header_div('commit', esc_html($tag{'name'}),$hash);5091print"<div class=\"title_text\">\n".5092"<table class=\"object_header\">\n".5093"<tr>\n".5094"<td>object</td>\n".5095"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5096$tag{'object'}) ."</td>\n".5097"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5098$tag{'type'}) ."</td>\n".5099"</tr>\n";5100if(defined($tag{'author'})) {5101 git_print_authorship_rows(\%tag,'author');5102}5103print"</table>\n\n".5104"</div>\n";5105print"<div class=\"page_body\">";5106my$comment=$tag{'comment'};5107foreachmy$line(@$comment) {5108chomp$line;5109print esc_html($line, -nbsp=>1) ."<br/>\n";5110}5111print"</div>\n";5112 git_footer_html();5113}51145115sub git_blame_common {5116my$format=shift||'porcelain';5117if($formateq'porcelain'&&$cgi->param('js')) {5118$format='incremental';5119$action='blame_incremental';# for page title etc5120}51215122# permissions5123 gitweb_check_feature('blame')5124or die_error(403,"Blame view not allowed");51255126# error checking5127 die_error(400,"No file name given")unless$file_name;5128$hash_base||= git_get_head_hash($project);5129 die_error(404,"Couldn't find base commit")unless$hash_base;5130my%co= parse_commit($hash_base)5131or die_error(404,"Commit not found");5132my$ftype="blob";5133if(!defined$hash) {5134$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5135or die_error(404,"Error looking up file");5136}else{5137$ftype= git_get_type($hash);5138if($ftype!~"blob") {5139 die_error(400,"Object is not a blob");5140}5141}51425143my$fd;5144if($formateq'incremental') {5145# get file contents (as base)5146open$fd,"-|", git_cmd(),'cat-file','blob',$hash5147or die_error(500,"Open git-cat-file failed");5148}elsif($formateq'data') {5149# run git-blame --incremental5150open$fd,"-|", git_cmd(),"blame","--incremental",5151$hash_base,"--",$file_name5152or die_error(500,"Open git-blame --incremental failed");5153}else{5154# run git-blame --porcelain5155open$fd,"-|", git_cmd(),"blame",'-p',5156$hash_base,'--',$file_name5157or die_error(500,"Open git-blame --porcelain failed");5158}51595160# incremental blame data returns early5161if($formateq'data') {5162print$cgi->header(5163-type=>"text/plain", -charset =>"utf-8",5164-status=>"200 OK");5165local$| =1;# output autoflush5166printwhile<$fd>;5167close$fd5168or print"ERROR$!\n";51695170print'END';5171if(defined$t0&& gitweb_check_feature('timed')) {5172print' '.5173 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5174' '.$number_of_git_cmds;5175}5176print"\n";51775178return;5179}51805181# page header5182 git_header_html();5183my$formats_nav=5184$cgi->a({-href => href(action=>"blob", -replay=>1)},5185"blob") .5186" | ";5187if($formateq'incremental') {5188$formats_nav.=5189$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5190"blame") ." (non-incremental)";5191}else{5192$formats_nav.=5193$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5194"blame") ." (incremental)";5195}5196$formats_nav.=5197" | ".5198$cgi->a({-href => href(action=>"history", -replay=>1)},5199"history") .5200" | ".5201$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5202"HEAD");5203 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5204 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5205 git_print_page_path($file_name,$ftype,$hash_base);52065207# page body5208if($formateq'incremental') {5209print"<noscript>\n<div class=\"error\"><center><b>\n".5210"This page requires JavaScript to run.\nUse ".5211$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5212'this page').5213" instead.\n".5214"</b></center></div>\n</noscript>\n";52155216print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5217}52185219print qq!<div class="page_body">\n!;5220print qq!<div id="progress_info">.../ ...</div>\n!5221if($formateq'incremental');5222print qq!<table id="blame_table"class="blame" width="100%">\n!.5223#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5224 qq!<thead>\n!.5225 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5226 qq!</thead>\n!.5227 qq!<tbody>\n!;52285229my@rev_color=qw(light dark);5230my$num_colors=scalar(@rev_color);5231my$current_color=0;52325233if($formateq'incremental') {5234my$color_class=$rev_color[$current_color];52355236#contents of a file5237my$linenr=0;5238 LINE:5239while(my$line= <$fd>) {5240chomp$line;5241$linenr++;52425243print qq!<tr id="l$linenr"class="$color_class">!.5244 qq!<td class="sha1"><a href=""> </a></td>!.5245 qq!<td class="linenr">!.5246 qq!<a class="linenr" href="">$linenr</a></td>!;5247print qq!<td class="pre">! . esc_html($line) ."</td>\n";5248print qq!</tr>\n!;5249}52505251}else{# porcelain, i.e. ordinary blame5252my%metainfo= ();# saves information about commits52535254# blame data5255 LINE:5256while(my$line= <$fd>) {5257chomp$line;5258# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5259# no <lines in group> for subsequent lines in group of lines5260my($full_rev,$orig_lineno,$lineno,$group_size) =5261($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5262if(!exists$metainfo{$full_rev}) {5263$metainfo{$full_rev} = {'nprevious'=>0};5264}5265my$meta=$metainfo{$full_rev};5266my$data;5267while($data= <$fd>) {5268chomp$data;5269last if($data=~s/^\t//);# contents of line5270if($data=~/^(\S+)(?: (.*))?$/) {5271$meta->{$1} =$2unlessexists$meta->{$1};5272}5273if($data=~/^previous /) {5274$meta->{'nprevious'}++;5275}5276}5277my$short_rev=substr($full_rev,0,8);5278my$author=$meta->{'author'};5279my%date=5280 parse_date($meta->{'author-time'},$meta->{'author-tz'});5281my$date=$date{'iso-tz'};5282if($group_size) {5283$current_color= ($current_color+1) %$num_colors;5284}5285my$tr_class=$rev_color[$current_color];5286$tr_class.=' boundary'if(exists$meta->{'boundary'});5287$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5288$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5289print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5290if($group_size) {5291print"<td class=\"sha1\"";5292print" title=\"". esc_html($author) .",$date\"";5293print" rowspan=\"$group_size\""if($group_size>1);5294print">";5295print$cgi->a({-href => href(action=>"commit",5296 hash=>$full_rev,5297 file_name=>$file_name)},5298 esc_html($short_rev));5299if($group_size>=2) {5300my@author_initials= ($author=~/\b([[:upper:]])\B/g);5301if(@author_initials) {5302print"<br />".5303 esc_html(join('',@author_initials));5304# or join('.', ...)5305}5306}5307print"</td>\n";5308}5309# 'previous' <sha1 of parent commit> <filename at commit>5310if(exists$meta->{'previous'} &&5311$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5312$meta->{'parent'} =$1;5313$meta->{'file_parent'} = unquote($2);5314}5315my$linenr_commit=5316exists($meta->{'parent'}) ?5317$meta->{'parent'} :$full_rev;5318my$linenr_filename=5319exists($meta->{'file_parent'}) ?5320$meta->{'file_parent'} : unquote($meta->{'filename'});5321my$blamed= href(action =>'blame',5322 file_name =>$linenr_filename,5323 hash_base =>$linenr_commit);5324print"<td class=\"linenr\">";5325print$cgi->a({ -href =>"$blamed#l$orig_lineno",5326-class=>"linenr"},5327 esc_html($lineno));5328print"</td>";5329print"<td class=\"pre\">". esc_html($data) ."</td>\n";5330print"</tr>\n";5331}# end while53325333}53345335# footer5336print"</tbody>\n".5337"</table>\n";# class="blame"5338print"</div>\n";# class="blame_body"5339close$fd5340or print"Reading blob failed\n";53415342 git_footer_html();5343}53445345sub git_blame {5346 git_blame_common();5347}53485349sub git_blame_incremental {5350 git_blame_common('incremental');5351}53525353sub git_blame_data {5354 git_blame_common('data');5355}53565357sub git_tags {5358my$head= git_get_head_hash($project);5359 git_header_html();5360 git_print_page_nav('','',$head,undef,$head);5361 git_print_header_div('summary',$project);53625363my@tagslist= git_get_tags_list();5364if(@tagslist) {5365 git_tags_body(\@tagslist);5366}5367 git_footer_html();5368}53695370sub git_heads {5371my$head= git_get_head_hash($project);5372 git_header_html();5373 git_print_page_nav('','',$head,undef,$head);5374 git_print_header_div('summary',$project);53755376my@headslist= git_get_heads_list();5377if(@headslist) {5378 git_heads_body(\@headslist,$head);5379}5380 git_footer_html();5381}53825383sub git_blob_plain {5384my$type=shift;5385my$expires;53865387if(!defined$hash) {5388if(defined$file_name) {5389my$base=$hash_base|| git_get_head_hash($project);5390$hash= git_get_hash_by_path($base,$file_name,"blob")5391or die_error(404,"Cannot find file");5392}else{5393 die_error(400,"No file name defined");5394}5395}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5396# blobs defined by non-textual hash id's can be cached5397$expires="+1d";5398}53995400open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5401or die_error(500,"Open git-cat-file blob '$hash' failed");54025403# content-type (can include charset)5404$type= blob_contenttype($fd,$file_name,$type);54055406# "save as" filename, even when no $file_name is given5407my$save_as="$hash";5408if(defined$file_name) {5409$save_as=$file_name;5410}elsif($type=~m/^text\//) {5411$save_as.='.txt';5412}54135414# With XSS prevention on, blobs of all types except a few known safe5415# ones are served with "Content-Disposition: attachment" to make sure5416# they don't run in our security domain. For certain image types,5417# blob view writes an <img> tag referring to blob_plain view, and we5418# want to be sure not to break that by serving the image as an5419# attachment (though Firefox 3 doesn't seem to care).5420my$sandbox=$prevent_xss&&5421$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;54225423print$cgi->header(5424-type =>$type,5425-expires =>$expires,5426-content_disposition =>5427($sandbox?'attachment':'inline')5428.'; filename="'.$save_as.'"');5429local$/=undef;5430binmode STDOUT,':raw';5431print<$fd>;5432binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5433close$fd;5434}54355436sub git_blob {5437my$expires;54385439if(!defined$hash) {5440if(defined$file_name) {5441my$base=$hash_base|| git_get_head_hash($project);5442$hash= git_get_hash_by_path($base,$file_name,"blob")5443or die_error(404,"Cannot find file");5444}else{5445 die_error(400,"No file name defined");5446}5447}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5448# blobs defined by non-textual hash id's can be cached5449$expires="+1d";5450}54515452my$have_blame= gitweb_check_feature('blame');5453open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5454or die_error(500,"Couldn't cat$file_name,$hash");5455my$mimetype= blob_mimetype($fd,$file_name);5456if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5457close$fd;5458return git_blob_plain($mimetype);5459}5460# we can have blame only for text/* mimetype5461$have_blame&&= ($mimetype=~m!^text/!);54625463 git_header_html(undef,$expires);5464my$formats_nav='';5465if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5466if(defined$file_name) {5467if($have_blame) {5468$formats_nav.=5469$cgi->a({-href => href(action=>"blame", -replay=>1)},5470"blame") .5471" | ";5472}5473$formats_nav.=5474$cgi->a({-href => href(action=>"history", -replay=>1)},5475"history") .5476" | ".5477$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5478"raw") .5479" | ".5480$cgi->a({-href => href(action=>"blob",5481 hash_base=>"HEAD", file_name=>$file_name)},5482"HEAD");5483}else{5484$formats_nav.=5485$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5486"raw");5487}5488 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5489 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5490}else{5491print"<div class=\"page_nav\">\n".5492"<br/><br/></div>\n".5493"<div class=\"title\">$hash</div>\n";5494}5495 git_print_page_path($file_name,"blob",$hash_base);5496print"<div class=\"page_body\">\n";5497if($mimetype=~m!^image/!) {5498print qq!<img type="$mimetype"!;5499if($file_name) {5500print qq! alt="$file_name" title="$file_name"!;5501}5502print qq! src="! .5503 href(action=>"blob_plain", hash=>$hash,5504 hash_base=>$hash_base, file_name=>$file_name) .5505 qq!"/>\n!;5506}else{5507my$nr;5508while(my$line= <$fd>) {5509chomp$line;5510$nr++;5511$line= untabify($line);5512printf"<div class=\"pre\"><a id=\"l%i\"href=\"". href(-replay =>1)5513."#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5514$nr,$nr,$nr, esc_html($line, -nbsp=>1);5515}5516}5517close$fd5518or print"Reading blob failed.\n";5519print"</div>";5520 git_footer_html();5521}55225523sub git_tree {5524if(!defined$hash_base) {5525$hash_base="HEAD";5526}5527if(!defined$hash) {5528if(defined$file_name) {5529$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5530}else{5531$hash=$hash_base;5532}5533}5534 die_error(404,"No such tree")unlessdefined($hash);55355536my$show_sizes= gitweb_check_feature('show-sizes');5537my$have_blame= gitweb_check_feature('blame');55385539my@entries= ();5540{5541local$/="\0";5542open my$fd,"-|", git_cmd(),"ls-tree",'-z',5543($show_sizes?'-l': ()),@extra_options,$hash5544or die_error(500,"Open git-ls-tree failed");5545@entries=map{chomp;$_} <$fd>;5546close$fd5547or die_error(404,"Reading tree failed");5548}55495550my$refs= git_get_references();5551my$ref= format_ref_marker($refs,$hash_base);5552 git_header_html();5553my$basedir='';5554if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5555my@views_nav= ();5556if(defined$file_name) {5557push@views_nav,5558$cgi->a({-href => href(action=>"history", -replay=>1)},5559"history"),5560$cgi->a({-href => href(action=>"tree",5561 hash_base=>"HEAD", file_name=>$file_name)},5562"HEAD"),5563}5564my$snapshot_links= format_snapshot_links($hash);5565if(defined$snapshot_links) {5566# FIXME: Should be available when we have no hash base as well.5567push@views_nav,$snapshot_links;5568}5569 git_print_page_nav('tree','',$hash_base,undef,undef,5570join(' | ',@views_nav));5571 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5572}else{5573undef$hash_base;5574print"<div class=\"page_nav\">\n";5575print"<br/><br/></div>\n";5576print"<div class=\"title\">$hash</div>\n";5577}5578if(defined$file_name) {5579$basedir=$file_name;5580if($basedirne''&&substr($basedir, -1)ne'/') {5581$basedir.='/';5582}5583 git_print_page_path($file_name,'tree',$hash_base);5584}5585print"<div class=\"page_body\">\n";5586print"<table class=\"tree\">\n";5587my$alternate=1;5588# '..' (top directory) link if possible5589if(defined$hash_base&&5590defined$file_name&&$file_name=~m![^/]+$!) {5591if($alternate) {5592print"<tr class=\"dark\">\n";5593}else{5594print"<tr class=\"light\">\n";5595}5596$alternate^=1;55975598my$up=$file_name;5599$up=~s!/?[^/]+$!!;5600undef$upunless$up;5601# based on git_print_tree_entry5602print'<td class="mode">'. mode_str('040000') ."</td>\n";5603print'<td class="size"> </td>'."\n"if$show_sizes;5604print'<td class="list">';5605print$cgi->a({-href => href(action=>"tree",5606 hash_base=>$hash_base,5607 file_name=>$up)},5608"..");5609print"</td>\n";5610print"<td class=\"link\"></td>\n";56115612print"</tr>\n";5613}5614foreachmy$line(@entries) {5615my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);56165617if($alternate) {5618print"<tr class=\"dark\">\n";5619}else{5620print"<tr class=\"light\">\n";5621}5622$alternate^=1;56235624 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);56255626print"</tr>\n";5627}5628print"</table>\n".5629"</div>";5630 git_footer_html();5631}56325633sub snapshot_name {5634my($project,$hash) =@_;56355636# path/to/project.git -> project5637# path/to/project/.git -> project5638my$name= to_utf8($project);5639$name=~ s,([^/])/*\.git$,$1,;5640$name= basename($name);5641# sanitize name5642$name=~s/[[:cntrl:]]/?/g;56435644my$ver=$hash;5645if($hash=~/^[0-9a-fA-F]+$/) {5646# shorten SHA-1 hash5647my$full_hash= git_get_full_hash($project,$hash);5648if($full_hash=~/^$hash/&&length($hash) >7) {5649$ver= git_get_short_hash($project,$hash);5650}5651}elsif($hash=~m!^refs/tags/(.*)$!) {5652# tags don't need shortened SHA-1 hash5653$ver=$1;5654}else{5655# branches and other need shortened SHA-1 hash5656if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5657$ver=$1;5658}5659$ver.='-'. git_get_short_hash($project,$hash);5660}5661# in case of hierarchical branch names5662$ver=~s!/!.!g;56635664# name = project-version_string5665$name="$name-$ver";56665667returnwantarray? ($name,$name) :$name;5668}56695670sub git_snapshot {5671my$format=$input_params{'snapshot_format'};5672if(!@snapshot_fmts) {5673 die_error(403,"Snapshots not allowed");5674}5675# default to first supported snapshot format5676$format||=$snapshot_fmts[0];5677if($format!~m/^[a-z0-9]+$/) {5678 die_error(400,"Invalid snapshot format parameter");5679}elsif(!exists($known_snapshot_formats{$format})) {5680 die_error(400,"Unknown snapshot format");5681}elsif($known_snapshot_formats{$format}{'disabled'}) {5682 die_error(403,"Snapshot format not allowed");5683}elsif(!grep($_eq$format,@snapshot_fmts)) {5684 die_error(403,"Unsupported snapshot format");5685}56865687my$type= git_get_type("$hash^{}");5688if(!$type) {5689 die_error(404,'Object does not exist');5690}elsif($typeeq'blob') {5691 die_error(400,'Object is not a tree-ish');5692}56935694my($name,$prefix) = snapshot_name($project,$hash);5695my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5696my$cmd= quote_command(5697 git_cmd(),'archive',5698"--format=$known_snapshot_formats{$format}{'format'}",5699"--prefix=$prefix/",$hash);5700if(exists$known_snapshot_formats{$format}{'compressor'}) {5701$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5702}57035704$filename=~s/(["\\])/\\$1/g;5705print$cgi->header(5706-type =>$known_snapshot_formats{$format}{'type'},5707-content_disposition =>'inline; filename="'.$filename.'"',5708-status =>'200 OK');57095710open my$fd,"-|",$cmd5711or die_error(500,"Execute git-archive failed");5712binmode STDOUT,':raw';5713print<$fd>;5714binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5715close$fd;5716}57175718sub git_log_generic {5719my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;57205721my$head= git_get_head_hash($project);5722if(!defined$base) {5723$base=$head;5724}5725if(!defined$page) {5726$page=0;5727}5728my$refs= git_get_references();57295730my$commit_hash=$base;5731if(defined$parent) {5732$commit_hash="$parent..$base";5733}5734my@commitlist=5735 parse_commits($commit_hash,101, (100*$page),5736defined$file_name? ($file_name,"--full-history") : ());57375738my$ftype;5739if(!defined$file_hash&&defined$file_name) {5740# some commits could have deleted file in question,5741# and not have it in tree, but one of them has to have it5742for(my$i=0;$i<@commitlist;$i++) {5743$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5744last ifdefined$file_hash;5745}5746}5747if(defined$file_hash) {5748$ftype= git_get_type($file_hash);5749}5750if(defined$file_name&& !defined$ftype) {5751 die_error(500,"Unknown type of object");5752}5753my%co;5754if(defined$file_name) {5755%co= parse_commit($base)5756or die_error(404,"Unknown commit object");5757}575857595760my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5761my$next_link='';5762if($#commitlist>=100) {5763$next_link=5764$cgi->a({-href => href(-replay=>1, page=>$page+1),5765-accesskey =>"n", -title =>"Alt-n"},"next");5766}5767my$patch_max= gitweb_get_feature('patches');5768if($patch_max&& !defined$file_name) {5769if($patch_max<0||@commitlist<=$patch_max) {5770$paging_nav.=" ⋅ ".5771$cgi->a({-href => href(action=>"patches", -replay=>1)},5772"patches");5773}5774}57755776 git_header_html();5777 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5778if(defined$file_name) {5779 git_print_header_div('commit', esc_html($co{'title'}),$base);5780}else{5781 git_print_header_div('summary',$project)5782}5783 git_print_page_path($file_name,$ftype,$hash_base)5784if(defined$file_name);57855786$body_subr->(\@commitlist,0,99,$refs,$next_link,5787$file_name,$file_hash,$ftype);57885789 git_footer_html();5790}57915792sub git_log {5793 git_log_generic('log', \&git_log_body,5794$hash,$hash_parent);5795}57965797sub git_commit {5798$hash||=$hash_base||"HEAD";5799my%co= parse_commit($hash)5800or die_error(404,"Unknown commit object");58015802my$parent=$co{'parent'};5803my$parents=$co{'parents'};# listref58045805# we need to prepare $formats_nav before any parameter munging5806my$formats_nav;5807if(!defined$parent) {5808# --root commitdiff5809$formats_nav.='(initial)';5810}elsif(@$parents==1) {5811# single parent commit5812$formats_nav.=5813'(parent: '.5814$cgi->a({-href => href(action=>"commit",5815 hash=>$parent)},5816 esc_html(substr($parent,0,7))) .5817')';5818}else{5819# merge commit5820$formats_nav.=5821'(merge: '.5822join(' ',map{5823$cgi->a({-href => href(action=>"commit",5824 hash=>$_)},5825 esc_html(substr($_,0,7)));5826}@$parents) .5827')';5828}5829if(gitweb_check_feature('patches') &&@$parents<=1) {5830$formats_nav.=" | ".5831$cgi->a({-href => href(action=>"patch", -replay=>1)},5832"patch");5833}58345835if(!defined$parent) {5836$parent="--root";5837}5838my@difftree;5839open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5840@diff_opts,5841(@$parents<=1?$parent:'-c'),5842$hash,"--"5843or die_error(500,"Open git-diff-tree failed");5844@difftree=map{chomp;$_} <$fd>;5845close$fdor die_error(404,"Reading git-diff-tree failed");58465847# non-textual hash id's can be cached5848my$expires;5849if($hash=~m/^[0-9a-fA-F]{40}$/) {5850$expires="+1d";5851}5852my$refs= git_get_references();5853my$ref= format_ref_marker($refs,$co{'id'});58545855 git_header_html(undef,$expires);5856 git_print_page_nav('commit','',5857$hash,$co{'tree'},$hash,5858$formats_nav);58595860if(defined$co{'parent'}) {5861 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5862}else{5863 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5864}5865print"<div class=\"title_text\">\n".5866"<table class=\"object_header\">\n";5867 git_print_authorship_rows(\%co);5868print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5869print"<tr>".5870"<td>tree</td>".5871"<td class=\"sha1\">".5872$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5873class=>"list"},$co{'tree'}) .5874"</td>".5875"<td class=\"link\">".5876$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5877"tree");5878my$snapshot_links= format_snapshot_links($hash);5879if(defined$snapshot_links) {5880print" | ".$snapshot_links;5881}5882print"</td>".5883"</tr>\n";58845885foreachmy$par(@$parents) {5886print"<tr>".5887"<td>parent</td>".5888"<td class=\"sha1\">".5889$cgi->a({-href => href(action=>"commit", hash=>$par),5890class=>"list"},$par) .5891"</td>".5892"<td class=\"link\">".5893$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5894" | ".5895$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5896"</td>".5897"</tr>\n";5898}5899print"</table>".5900"</div>\n";59015902print"<div class=\"page_body\">\n";5903 git_print_log($co{'comment'});5904print"</div>\n";59055906 git_difftree_body(\@difftree,$hash,@$parents);59075908 git_footer_html();5909}59105911sub git_object {5912# object is defined by:5913# - hash or hash_base alone5914# - hash_base and file_name5915my$type;59165917# - hash or hash_base alone5918if($hash|| ($hash_base&& !defined$file_name)) {5919my$object_id=$hash||$hash_base;59205921open my$fd,"-|", quote_command(5922 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5923or die_error(404,"Object does not exist");5924$type= <$fd>;5925chomp$type;5926close$fd5927or die_error(404,"Object does not exist");59285929# - hash_base and file_name5930}elsif($hash_base&&defined$file_name) {5931$file_name=~ s,/+$,,;59325933system(git_cmd(),"cat-file",'-e',$hash_base) ==05934or die_error(404,"Base object does not exist");59355936# here errors should not hapen5937open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5938or die_error(500,"Open git-ls-tree failed");5939my$line= <$fd>;5940close$fd;59415942#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5943unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5944 die_error(404,"File or directory for given base does not exist");5945}5946$type=$2;5947$hash=$3;5948}else{5949 die_error(400,"Not enough information to find object");5950}59515952print$cgi->redirect(-uri => href(action=>$type, -full=>1,5953 hash=>$hash, hash_base=>$hash_base,5954 file_name=>$file_name),5955-status =>'302 Found');5956}59575958sub git_blobdiff {5959my$format=shift||'html';59605961my$fd;5962my@difftree;5963my%diffinfo;5964my$expires;59655966# preparing $fd and %diffinfo for git_patchset_body5967# new style URI5968if(defined$hash_base&&defined$hash_parent_base) {5969if(defined$file_name) {5970# read raw output5971open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5972$hash_parent_base,$hash_base,5973"--", (defined$file_parent?$file_parent: ()),$file_name5974or die_error(500,"Open git-diff-tree failed");5975@difftree=map{chomp;$_} <$fd>;5976close$fd5977or die_error(404,"Reading git-diff-tree failed");5978@difftree5979or die_error(404,"Blob diff not found");59805981}elsif(defined$hash&&5982$hash=~/[0-9a-fA-F]{40}/) {5983# try to find filename from $hash59845985# read filtered raw output5986open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5987$hash_parent_base,$hash_base,"--"5988or die_error(500,"Open git-diff-tree failed");5989@difftree=5990# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5991# $hash == to_id5992grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5993map{chomp;$_} <$fd>;5994close$fd5995or die_error(404,"Reading git-diff-tree failed");5996@difftree5997or die_error(404,"Blob diff not found");59985999}else{6000 die_error(400,"Missing one of the blob diff parameters");6001}60026003if(@difftree>1) {6004 die_error(400,"Ambiguous blob diff specification");6005}60066007%diffinfo= parse_difftree_raw_line($difftree[0]);6008$file_parent||=$diffinfo{'from_file'} ||$file_name;6009$file_name||=$diffinfo{'to_file'};60106011$hash_parent||=$diffinfo{'from_id'};6012$hash||=$diffinfo{'to_id'};60136014# non-textual hash id's can be cached6015if($hash_base=~m/^[0-9a-fA-F]{40}$/&&6016$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {6017$expires='+1d';6018}60196020# open patch output6021open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6022'-p', ($formateq'html'?"--full-index": ()),6023$hash_parent_base,$hash_base,6024"--", (defined$file_parent?$file_parent: ()),$file_name6025or die_error(500,"Open git-diff-tree failed");6026}60276028# old/legacy style URI -- not generated anymore since 1.4.3.6029if(!%diffinfo) {6030 die_error('404 Not Found',"Missing one of the blob diff parameters")6031}60326033# header6034if($formateq'html') {6035my$formats_nav=6036$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},6037"raw");6038 git_header_html(undef,$expires);6039if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6040 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6041 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6042}else{6043print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";6044print"<div class=\"title\">$hashvs$hash_parent</div>\n";6045}6046if(defined$file_name) {6047 git_print_page_path($file_name,"blob",$hash_base);6048}else{6049print"<div class=\"page_path\"></div>\n";6050}60516052}elsif($formateq'plain') {6053print$cgi->header(6054-type =>'text/plain',6055-charset =>'utf-8',6056-expires =>$expires,6057-content_disposition =>'inline; filename="'."$file_name".'.patch"');60586059print"X-Git-Url: ".$cgi->self_url() ."\n\n";60606061}else{6062 die_error(400,"Unknown blobdiff format");6063}60646065# patch6066if($formateq'html') {6067print"<div class=\"page_body\">\n";60686069 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);6070close$fd;60716072print"</div>\n";# class="page_body"6073 git_footer_html();60746075}else{6076while(my$line= <$fd>) {6077$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;6078$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;60796080print$line;60816082last if$line=~m!^\+\+\+!;6083}6084local$/=undef;6085print<$fd>;6086close$fd;6087}6088}60896090sub git_blobdiff_plain {6091 git_blobdiff('plain');6092}60936094sub git_commitdiff {6095my%params=@_;6096my$format=$params{-format} ||'html';60976098my($patch_max) = gitweb_get_feature('patches');6099if($formateq'patch') {6100 die_error(403,"Patch view not allowed")unless$patch_max;6101}61026103$hash||=$hash_base||"HEAD";6104my%co= parse_commit($hash)6105or die_error(404,"Unknown commit object");61066107# choose format for commitdiff for merge6108if(!defined$hash_parent&& @{$co{'parents'}} >1) {6109$hash_parent='--cc';6110}6111# we need to prepare $formats_nav before almost any parameter munging6112my$formats_nav;6113if($formateq'html') {6114$formats_nav=6115$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6116"raw");6117if($patch_max&& @{$co{'parents'}} <=1) {6118$formats_nav.=" | ".6119$cgi->a({-href => href(action=>"patch", -replay=>1)},6120"patch");6121}61226123if(defined$hash_parent&&6124$hash_parentne'-c'&&$hash_parentne'--cc') {6125# commitdiff with two commits given6126my$hash_parent_short=$hash_parent;6127if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6128$hash_parent_short=substr($hash_parent,0,7);6129}6130$formats_nav.=6131' (from';6132for(my$i=0;$i< @{$co{'parents'}};$i++) {6133if($co{'parents'}[$i]eq$hash_parent) {6134$formats_nav.=' parent '. ($i+1);6135last;6136}6137}6138$formats_nav.=': '.6139$cgi->a({-href => href(action=>"commitdiff",6140 hash=>$hash_parent)},6141 esc_html($hash_parent_short)) .6142')';6143}elsif(!$co{'parent'}) {6144# --root commitdiff6145$formats_nav.=' (initial)';6146}elsif(scalar@{$co{'parents'}} ==1) {6147# single parent commit6148$formats_nav.=6149' (parent: '.6150$cgi->a({-href => href(action=>"commitdiff",6151 hash=>$co{'parent'})},6152 esc_html(substr($co{'parent'},0,7))) .6153')';6154}else{6155# merge commit6156if($hash_parenteq'--cc') {6157$formats_nav.=' | '.6158$cgi->a({-href => href(action=>"commitdiff",6159 hash=>$hash, hash_parent=>'-c')},6160'combined');6161}else{# $hash_parent eq '-c'6162$formats_nav.=' | '.6163$cgi->a({-href => href(action=>"commitdiff",6164 hash=>$hash, hash_parent=>'--cc')},6165'compact');6166}6167$formats_nav.=6168' (merge: '.6169join(' ',map{6170$cgi->a({-href => href(action=>"commitdiff",6171 hash=>$_)},6172 esc_html(substr($_,0,7)));6173} @{$co{'parents'}} ) .6174')';6175}6176}61776178my$hash_parent_param=$hash_parent;6179if(!defined$hash_parent_param) {6180# --cc for multiple parents, --root for parentless6181$hash_parent_param=6182@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6183}61846185# read commitdiff6186my$fd;6187my@difftree;6188if($formateq'html') {6189open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6190"--no-commit-id","--patch-with-raw","--full-index",6191$hash_parent_param,$hash,"--"6192or die_error(500,"Open git-diff-tree failed");61936194while(my$line= <$fd>) {6195chomp$line;6196# empty line ends raw part of diff-tree output6197last unless$line;6198push@difftree,scalar parse_difftree_raw_line($line);6199}62006201}elsif($formateq'plain') {6202open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6203'-p',$hash_parent_param,$hash,"--"6204or die_error(500,"Open git-diff-tree failed");6205}elsif($formateq'patch') {6206# For commit ranges, we limit the output to the number of6207# patches specified in the 'patches' feature.6208# For single commits, we limit the output to a single patch,6209# diverging from the git-format-patch default.6210my@commit_spec= ();6211if($hash_parent) {6212if($patch_max>0) {6213push@commit_spec,"-$patch_max";6214}6215push@commit_spec,'-n',"$hash_parent..$hash";6216}else{6217if($params{-single}) {6218push@commit_spec,'-1';6219}else{6220if($patch_max>0) {6221push@commit_spec,"-$patch_max";6222}6223push@commit_spec,"-n";6224}6225push@commit_spec,'--root',$hash;6226}6227open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',6228'--stdout',@commit_spec6229or die_error(500,"Open git-format-patch failed");6230}else{6231 die_error(400,"Unknown commitdiff format");6232}62336234# non-textual hash id's can be cached6235my$expires;6236if($hash=~m/^[0-9a-fA-F]{40}$/) {6237$expires="+1d";6238}62396240# write commit message6241if($formateq'html') {6242my$refs= git_get_references();6243my$ref= format_ref_marker($refs,$co{'id'});62446245 git_header_html(undef,$expires);6246 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6247 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6248print"<div class=\"title_text\">\n".6249"<table class=\"object_header\">\n";6250 git_print_authorship_rows(\%co);6251print"</table>".6252"</div>\n";6253print"<div class=\"page_body\">\n";6254if(@{$co{'comment'}} >1) {6255print"<div class=\"log\">\n";6256 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6257print"</div>\n";# class="log"6258}62596260}elsif($formateq'plain') {6261my$refs= git_get_references("tags");6262my$tagname= git_get_rev_name_tags($hash);6263my$filename= basename($project) ."-$hash.patch";62646265print$cgi->header(6266-type =>'text/plain',6267-charset =>'utf-8',6268-expires =>$expires,6269-content_disposition =>'inline; filename="'."$filename".'"');6270my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6271print"From: ". to_utf8($co{'author'}) ."\n";6272print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6273print"Subject: ". to_utf8($co{'title'}) ."\n";62746275print"X-Git-Tag:$tagname\n"if$tagname;6276print"X-Git-Url: ".$cgi->self_url() ."\n\n";62776278foreachmy$line(@{$co{'comment'}}) {6279print to_utf8($line) ."\n";6280}6281print"---\n\n";6282}elsif($formateq'patch') {6283my$filename= basename($project) ."-$hash.patch";62846285print$cgi->header(6286-type =>'text/plain',6287-charset =>'utf-8',6288-expires =>$expires,6289-content_disposition =>'inline; filename="'."$filename".'"');6290}62916292# write patch6293if($formateq'html') {6294my$use_parents= !defined$hash_parent||6295$hash_parenteq'-c'||$hash_parenteq'--cc';6296 git_difftree_body(\@difftree,$hash,6297$use_parents? @{$co{'parents'}} :$hash_parent);6298print"<br/>\n";62996300 git_patchset_body($fd, \@difftree,$hash,6301$use_parents? @{$co{'parents'}} :$hash_parent);6302close$fd;6303print"</div>\n";# class="page_body"6304 git_footer_html();63056306}elsif($formateq'plain') {6307local$/=undef;6308print<$fd>;6309close$fd6310or print"Reading git-diff-tree failed\n";6311}elsif($formateq'patch') {6312local$/=undef;6313print<$fd>;6314close$fd6315or print"Reading git-format-patch failed\n";6316}6317}63186319sub git_commitdiff_plain {6320 git_commitdiff(-format =>'plain');6321}63226323# format-patch-style patches6324sub git_patch {6325 git_commitdiff(-format =>'patch', -single =>1);6326}63276328sub git_patches {6329 git_commitdiff(-format =>'patch');6330}63316332sub git_history {6333 git_log_generic('history', \&git_history_body,6334$hash_base,$hash_parent_base,6335$file_name,$hash);6336}63376338sub git_search {6339 gitweb_check_feature('search')or die_error(403,"Search is disabled");6340if(!defined$searchtext) {6341 die_error(400,"Text field is empty");6342}6343if(!defined$hash) {6344$hash= git_get_head_hash($project);6345}6346my%co= parse_commit($hash);6347if(!%co) {6348 die_error(404,"Unknown commit object");6349}6350if(!defined$page) {6351$page=0;6352}63536354$searchtype||='commit';6355if($searchtypeeq'pickaxe') {6356# pickaxe may take all resources of your box and run for several minutes6357# with every query - so decide by yourself how public you make this feature6358 gitweb_check_feature('pickaxe')6359or die_error(403,"Pickaxe is disabled");6360}6361if($searchtypeeq'grep') {6362 gitweb_check_feature('grep')6363or die_error(403,"Grep is disabled");6364}63656366 git_header_html();63676368if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6369my$greptype;6370if($searchtypeeq'commit') {6371$greptype="--grep=";6372}elsif($searchtypeeq'author') {6373$greptype="--author=";6374}elsif($searchtypeeq'committer') {6375$greptype="--committer=";6376}6377$greptype.=$searchtext;6378my@commitlist= parse_commits($hash,101, (100*$page),undef,6379$greptype,'--regexp-ignore-case',6380$search_use_regexp?'--extended-regexp':'--fixed-strings');63816382my$paging_nav='';6383if($page>0) {6384$paging_nav.=6385$cgi->a({-href => href(action=>"search", hash=>$hash,6386 searchtext=>$searchtext,6387 searchtype=>$searchtype)},6388"first");6389$paging_nav.=" ⋅ ".6390$cgi->a({-href => href(-replay=>1, page=>$page-1),6391-accesskey =>"p", -title =>"Alt-p"},"prev");6392}else{6393$paging_nav.="first";6394$paging_nav.=" ⋅ prev";6395}6396my$next_link='';6397if($#commitlist>=100) {6398$next_link=6399$cgi->a({-href => href(-replay=>1, page=>$page+1),6400-accesskey =>"n", -title =>"Alt-n"},"next");6401$paging_nav.=" ⋅$next_link";6402}else{6403$paging_nav.=" ⋅ next";6404}64056406if($#commitlist>=100) {6407}64086409 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6410 git_print_header_div('commit', esc_html($co{'title'}),$hash);6411 git_search_grep_body(\@commitlist,0,99,$next_link);6412}64136414if($searchtypeeq'pickaxe') {6415 git_print_page_nav('','',$hash,$co{'tree'},$hash);6416 git_print_header_div('commit', esc_html($co{'title'}),$hash);64176418print"<table class=\"pickaxe search\">\n";6419my$alternate=1;6420local$/="\n";6421open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6422'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6423($search_use_regexp?'--pickaxe-regex': ());6424undef%co;6425my@files;6426while(my$line= <$fd>) {6427chomp$line;6428next unless$line;64296430my%set= parse_difftree_raw_line($line);6431if(defined$set{'commit'}) {6432# finish previous commit6433if(%co) {6434print"</td>\n".6435"<td class=\"link\">".6436$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6437" | ".6438$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6439print"</td>\n".6440"</tr>\n";6441}64426443if($alternate) {6444print"<tr class=\"dark\">\n";6445}else{6446print"<tr class=\"light\">\n";6447}6448$alternate^=1;6449%co= parse_commit($set{'commit'});6450my$author= chop_and_escape_str($co{'author_name'},15,5);6451print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6452"<td><i>$author</i></td>\n".6453"<td>".6454$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6455-class=>"list subject"},6456 chop_and_escape_str($co{'title'},50) ."<br/>");6457}elsif(defined$set{'to_id'}) {6458next if($set{'to_id'} =~m/^0{40}$/);64596460print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6461 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6462-class=>"list"},6463"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6464"<br/>\n";6465}6466}6467close$fd;64686469# finish last commit (warning: repetition!)6470if(%co) {6471print"</td>\n".6472"<td class=\"link\">".6473$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6474" | ".6475$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6476print"</td>\n".6477"</tr>\n";6478}64796480print"</table>\n";6481}64826483if($searchtypeeq'grep') {6484 git_print_page_nav('','',$hash,$co{'tree'},$hash);6485 git_print_header_div('commit', esc_html($co{'title'}),$hash);64866487print"<table class=\"grep_search\">\n";6488my$alternate=1;6489my$matches=0;6490local$/="\n";6491open my$fd,"-|", git_cmd(),'grep','-n',6492$search_use_regexp? ('-E','-i') :'-F',6493$searchtext,$co{'tree'};6494my$lastfile='';6495while(my$line= <$fd>) {6496chomp$line;6497my($file,$lno,$ltext,$binary);6498last if($matches++>1000);6499if($line=~/^Binary file (.+) matches$/) {6500$file=$1;6501$binary=1;6502}else{6503(undef,$file,$lno,$ltext) =split(/:/,$line,4);6504}6505if($filene$lastfile) {6506$lastfileand print"</td></tr>\n";6507if($alternate++) {6508print"<tr class=\"dark\">\n";6509}else{6510print"<tr class=\"light\">\n";6511}6512print"<td class=\"list\">".6513$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6514 file_name=>"$file"),6515-class=>"list"}, esc_path($file));6516print"</td><td>\n";6517$lastfile=$file;6518}6519if($binary) {6520print"<div class=\"binary\">Binary file</div>\n";6521}else{6522$ltext= untabify($ltext);6523if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6524$ltext= esc_html($1, -nbsp=>1);6525$ltext.='<span class="match">';6526$ltext.= esc_html($2, -nbsp=>1);6527$ltext.='</span>';6528$ltext.= esc_html($3, -nbsp=>1);6529}else{6530$ltext= esc_html($ltext, -nbsp=>1);6531}6532print"<div class=\"pre\">".6533$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6534 file_name=>"$file").'#l'.$lno,6535-class=>"linenr"},sprintf('%4i',$lno))6536.' '.$ltext."</div>\n";6537}6538}6539if($lastfile) {6540print"</td></tr>\n";6541if($matches>1000) {6542print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6543}6544}else{6545print"<div class=\"diff nodifferences\">No matches found</div>\n";6546}6547close$fd;65486549print"</table>\n";6550}6551 git_footer_html();6552}65536554sub git_search_help {6555 git_header_html();6556 git_print_page_nav('','',$hash,$hash,$hash);6557print<<EOT;6558<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6559regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6560the pattern entered is recognized as the POSIX extended6561<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6562insensitive).</p>6563<dl>6564<dt><b>commit</b></dt>6565<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6566EOT6567my$have_grep= gitweb_check_feature('grep');6568if($have_grep) {6569print<<EOT;6570<dt><b>grep</b></dt>6571<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6572 a different one) are searched for the given pattern. On large trees, this search can take6573a while and put some strain on the server, so please use it with some consideration. Note that6574due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6575case-sensitive.</dd>6576EOT6577}6578print<<EOT;6579<dt><b>author</b></dt>6580<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6581<dt><b>committer</b></dt>6582<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6583EOT6584my$have_pickaxe= gitweb_check_feature('pickaxe');6585if($have_pickaxe) {6586print<<EOT;6587<dt><b>pickaxe</b></dt>6588<dd>All commits that caused the string to appear or disappear from any file (changes that6589added, removed or "modified" the string) will be listed. This search can take a while and6590takes a lot of strain on the server, so please use it wisely. Note that since you may be6591interested even in changes just changing the case as well, this search is case sensitive.</dd>6592EOT6593}6594print"</dl>\n";6595 git_footer_html();6596}65976598sub git_shortlog {6599 git_log_generic('shortlog', \&git_shortlog_body,6600$hash,$hash_parent);6601}66026603## ......................................................................6604## feeds (RSS, Atom; OPML)66056606sub git_feed {6607my$format=shift||'atom';6608my$have_blame= gitweb_check_feature('blame');66096610# Atom: http://www.atomenabled.org/developers/syndication/6611# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6612if($formatne'rss'&&$formatne'atom') {6613 die_error(400,"Unknown web feed format");6614}66156616# log/feed of current (HEAD) branch, log of given branch, history of file/directory6617my$head=$hash||'HEAD';6618my@commitlist= parse_commits($head,150,0,$file_name);66196620my%latest_commit;6621my%latest_date;6622my$content_type="application/$format+xml";6623if(defined$cgi->http('HTTP_ACCEPT') &&6624$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6625# browser (feed reader) prefers text/xml6626$content_type='text/xml';6627}6628if(defined($commitlist[0])) {6629%latest_commit= %{$commitlist[0]};6630my$latest_epoch=$latest_commit{'committer_epoch'};6631%latest_date= parse_date($latest_epoch);6632my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6633if(defined$if_modified) {6634my$since;6635if(eval{require HTTP::Date;1; }) {6636$since= HTTP::Date::str2time($if_modified);6637}elsif(eval{require Time::ParseDate;1; }) {6638$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6639}6640if(defined$since&&$latest_epoch<=$since) {6641print$cgi->header(6642-type =>$content_type,6643-charset =>'utf-8',6644-last_modified =>$latest_date{'rfc2822'},6645-status =>'304 Not Modified');6646return;6647}6648}6649print$cgi->header(6650-type =>$content_type,6651-charset =>'utf-8',6652-last_modified =>$latest_date{'rfc2822'});6653}else{6654print$cgi->header(6655-type =>$content_type,6656-charset =>'utf-8');6657}66586659# Optimization: skip generating the body if client asks only6660# for Last-Modified date.6661return if($cgi->request_method()eq'HEAD');66626663# header variables6664my$title="$site_name-$project/$action";6665my$feed_type='log';6666if(defined$hash) {6667$title.=" - '$hash'";6668$feed_type='branch log';6669if(defined$file_name) {6670$title.=" ::$file_name";6671$feed_type='history';6672}6673}elsif(defined$file_name) {6674$title.=" -$file_name";6675$feed_type='history';6676}6677$title.="$feed_type";6678my$descr= git_get_project_description($project);6679if(defined$descr) {6680$descr= esc_html($descr);6681}else{6682$descr="$project".6683($formateq'rss'?'RSS':'Atom') .6684" feed";6685}6686my$owner= git_get_project_owner($project);6687$owner= esc_html($owner);66886689#header6690my$alt_url;6691if(defined$file_name) {6692$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6693}elsif(defined$hash) {6694$alt_url= href(-full=>1, action=>"log", hash=>$hash);6695}else{6696$alt_url= href(-full=>1, action=>"summary");6697}6698print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6699if($formateq'rss') {6700print<<XML;6701<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6702<channel>6703XML6704print"<title>$title</title>\n".6705"<link>$alt_url</link>\n".6706"<description>$descr</description>\n".6707"<language>en</language>\n".6708# project owner is responsible for 'editorial' content6709"<managingEditor>$owner</managingEditor>\n";6710if(defined$logo||defined$favicon) {6711# prefer the logo to the favicon, since RSS6712# doesn't allow both6713my$img= esc_url($logo||$favicon);6714print"<image>\n".6715"<url>$img</url>\n".6716"<title>$title</title>\n".6717"<link>$alt_url</link>\n".6718"</image>\n";6719}6720if(%latest_date) {6721print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6722print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6723}6724print"<generator>gitweb v.$version/$git_version</generator>\n";6725}elsif($formateq'atom') {6726print<<XML;6727<feed xmlns="http://www.w3.org/2005/Atom">6728XML6729print"<title>$title</title>\n".6730"<subtitle>$descr</subtitle>\n".6731'<link rel="alternate" type="text/html" href="'.6732$alt_url.'" />'."\n".6733'<link rel="self" type="'.$content_type.'" href="'.6734$cgi->self_url() .'" />'."\n".6735"<id>". href(-full=>1) ."</id>\n".6736# use project owner for feed author6737"<author><name>$owner</name></author>\n";6738if(defined$favicon) {6739print"<icon>". esc_url($favicon) ."</icon>\n";6740}6741if(defined$logo_url) {6742# not twice as wide as tall: 72 x 27 pixels6743print"<logo>". esc_url($logo) ."</logo>\n";6744}6745if(!%latest_date) {6746# dummy date to keep the feed valid until commits trickle in:6747print"<updated>1970-01-01T00:00:00Z</updated>\n";6748}else{6749print"<updated>$latest_date{'iso-8601'}</updated>\n";6750}6751print"<generator version='$version/$git_version'>gitweb</generator>\n";6752}67536754# contents6755for(my$i=0;$i<=$#commitlist;$i++) {6756my%co= %{$commitlist[$i]};6757my$commit=$co{'id'};6758# we read 150, we always show 30 and the ones more recent than 48 hours6759if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6760last;6761}6762my%cd= parse_date($co{'author_epoch'});67636764# get list of changed files6765open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6766$co{'parent'} ||"--root",6767$co{'id'},"--", (defined$file_name?$file_name: ())6768ornext;6769my@difftree=map{chomp;$_} <$fd>;6770close$fd6771ornext;67726773# print element (entry, item)6774my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6775if($formateq'rss') {6776print"<item>\n".6777"<title>". esc_html($co{'title'}) ."</title>\n".6778"<author>". esc_html($co{'author'}) ."</author>\n".6779"<pubDate>$cd{'rfc2822'}</pubDate>\n".6780"<guid isPermaLink=\"true\">$co_url</guid>\n".6781"<link>$co_url</link>\n".6782"<description>". esc_html($co{'title'}) ."</description>\n".6783"<content:encoded>".6784"<![CDATA[\n";6785}elsif($formateq'atom') {6786print"<entry>\n".6787"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6788"<updated>$cd{'iso-8601'}</updated>\n".6789"<author>\n".6790" <name>". esc_html($co{'author_name'}) ."</name>\n";6791if($co{'author_email'}) {6792print" <email>". esc_html($co{'author_email'}) ."</email>\n";6793}6794print"</author>\n".6795# use committer for contributor6796"<contributor>\n".6797" <name>". esc_html($co{'committer_name'}) ."</name>\n";6798if($co{'committer_email'}) {6799print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6800}6801print"</contributor>\n".6802"<published>$cd{'iso-8601'}</published>\n".6803"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6804"<id>$co_url</id>\n".6805"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6806"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6807}6808my$comment=$co{'comment'};6809print"<pre>\n";6810foreachmy$line(@$comment) {6811$line= esc_html($line);6812print"$line\n";6813}6814print"</pre><ul>\n";6815foreachmy$difftree_line(@difftree) {6816my%difftree= parse_difftree_raw_line($difftree_line);6817next if!$difftree{'from_id'};68186819my$file=$difftree{'file'} ||$difftree{'to_file'};68206821print"<li>".6822"[".6823$cgi->a({-href => href(-full=>1, action=>"blobdiff",6824 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6825 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6826 file_name=>$file, file_parent=>$difftree{'from_file'}),6827-title =>"diff"},'D');6828if($have_blame) {6829print$cgi->a({-href => href(-full=>1, action=>"blame",6830 file_name=>$file, hash_base=>$commit),6831-title =>"blame"},'B');6832}6833# if this is not a feed of a file history6834if(!defined$file_name||$file_namene$file) {6835print$cgi->a({-href => href(-full=>1, action=>"history",6836 file_name=>$file, hash=>$commit),6837-title =>"history"},'H');6838}6839$file= esc_path($file);6840print"] ".6841"$file</li>\n";6842}6843if($formateq'rss') {6844print"</ul>]]>\n".6845"</content:encoded>\n".6846"</item>\n";6847}elsif($formateq'atom') {6848print"</ul>\n</div>\n".6849"</content>\n".6850"</entry>\n";6851}6852}68536854# end of feed6855if($formateq'rss') {6856print"</channel>\n</rss>\n";6857}elsif($formateq'atom') {6858print"</feed>\n";6859}6860}68616862sub git_rss {6863 git_feed('rss');6864}68656866sub git_atom {6867 git_feed('atom');6868}68696870sub git_opml {6871my@list= git_get_projects_list();68726873print$cgi->header(6874-type =>'text/xml',6875-charset =>'utf-8',6876-content_disposition =>'inline; filename="opml.xml"');68776878print<<XML;6879<?xml version="1.0" encoding="utf-8"?>6880<opml version="1.0">6881<head>6882 <title>$site_nameOPML Export</title>6883</head>6884<body>6885<outline text="git RSS feeds">6886XML68876888foreachmy$pr(@list) {6889my%proj=%$pr;6890my$head= git_get_head_hash($proj{'path'});6891if(!defined$head) {6892next;6893}6894$git_dir="$projectroot/$proj{'path'}";6895my%co= parse_commit($head);6896if(!%co) {6897next;6898}68996900my$path= esc_html(chop_str($proj{'path'},25,5));6901my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6902my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6903print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6904}6905print<<XML;6906</outline>6907</body>6908</opml>6909XML6910}