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 set_message); 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# Syntax highlighting support. This is based on Daniel Svensson's 455# and Sham Chukoury's work in gitweb-xmms2.git. 456# It requires the 'highlight' program present in $PATH, 457# and therefore is disabled by default. 458 459# To enable system wide have in $GITWEB_CONFIG 460# $feature{'highlight'}{'default'} = [1]; 461 462'highlight'=> { 463'sub'=>sub{ feature_bool('highlight',@_) }, 464'override'=>0, 465'default'=> [0]}, 466); 467 468sub gitweb_get_feature { 469my($name) =@_; 470return unlessexists$feature{$name}; 471my($sub,$override,@defaults) = ( 472$feature{$name}{'sub'}, 473$feature{$name}{'override'}, 474@{$feature{$name}{'default'}}); 475# project specific override is possible only if we have project 476our$git_dir;# global variable, declared later 477if(!$override|| !defined$git_dir) { 478return@defaults; 479} 480if(!defined$sub) { 481warn"feature$nameis not overridable"; 482return@defaults; 483} 484return$sub->(@defaults); 485} 486 487# A wrapper to check if a given feature is enabled. 488# With this, you can say 489# 490# my $bool_feat = gitweb_check_feature('bool_feat'); 491# gitweb_check_feature('bool_feat') or somecode; 492# 493# instead of 494# 495# my ($bool_feat) = gitweb_get_feature('bool_feat'); 496# (gitweb_get_feature('bool_feat'))[0] or somecode; 497# 498sub gitweb_check_feature { 499return(gitweb_get_feature(@_))[0]; 500} 501 502 503sub feature_bool { 504my$key=shift; 505my($val) = git_get_project_config($key,'--bool'); 506 507if(!defined$val) { 508return($_[0]); 509}elsif($valeq'true') { 510return(1); 511}elsif($valeq'false') { 512return(0); 513} 514} 515 516sub feature_snapshot { 517my(@fmts) =@_; 518 519my($val) = git_get_project_config('snapshot'); 520 521if($val) { 522@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 523} 524 525return@fmts; 526} 527 528sub feature_patches { 529my@val= (git_get_project_config('patches','--int')); 530 531if(@val) { 532return@val; 533} 534 535return($_[0]); 536} 537 538sub feature_avatar { 539my@val= (git_get_project_config('avatar')); 540 541return@val?@val:@_; 542} 543 544# checking HEAD file with -e is fragile if the repository was 545# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 546# and then pruned. 547sub check_head_link { 548my($dir) =@_; 549my$headfile="$dir/HEAD"; 550return((-e $headfile) || 551(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 552} 553 554sub check_export_ok { 555my($dir) =@_; 556return(check_head_link($dir) && 557(!$export_ok|| -e "$dir/$export_ok") && 558(!$export_auth_hook||$export_auth_hook->($dir))); 559} 560 561# process alternate names for backward compatibility 562# filter out unsupported (unknown) snapshot formats 563sub filter_snapshot_fmts { 564my@fmts=@_; 565 566@fmts=map{ 567exists$known_snapshot_format_aliases{$_} ? 568$known_snapshot_format_aliases{$_} :$_}@fmts; 569@fmts=grep{ 570exists$known_snapshot_formats{$_} && 571!$known_snapshot_formats{$_}{'disabled'}}@fmts; 572} 573 574our($GITWEB_CONFIG,$GITWEB_CONFIG_SYSTEM); 575sub evaluate_gitweb_config { 576our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 577our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 578# die if there are errors parsing config file 579if(-e $GITWEB_CONFIG) { 580do$GITWEB_CONFIG; 581die$@if$@; 582}elsif(-e $GITWEB_CONFIG_SYSTEM) { 583do$GITWEB_CONFIG_SYSTEM; 584die$@if$@; 585} 586} 587 588# Get loadavg of system, to compare against $maxload. 589# Currently it requires '/proc/loadavg' present to get loadavg; 590# if it is not present it returns 0, which means no load checking. 591sub get_loadavg { 592if( -e '/proc/loadavg'){ 593open my$fd,'<','/proc/loadavg' 594orreturn0; 595my@load=split(/\s+/,scalar<$fd>); 596close$fd; 597 598# The first three columns measure CPU and IO utilization of the last one, 599# five, and 10 minute periods. The fourth column shows the number of 600# currently running processes and the total number of processes in the m/n 601# format. The last column displays the last process ID used. 602return$load[0] ||0; 603} 604# additional checks for load average should go here for things that don't export 605# /proc/loadavg 606 607return0; 608} 609 610# version of the core git binary 611our$git_version; 612sub evaluate_git_version { 613our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 614$number_of_git_cmds++; 615} 616 617sub check_loadavg { 618if(defined$maxload&& get_loadavg() >$maxload) { 619 die_error(503,"The load average on the server is too high"); 620} 621} 622 623# ====================================================================== 624# input validation and dispatch 625 626# input parameters can be collected from a variety of sources (presently, CGI 627# and PATH_INFO), so we define an %input_params hash that collects them all 628# together during validation: this allows subsequent uses (e.g. href()) to be 629# agnostic of the parameter origin 630 631our%input_params= (); 632 633# input parameters are stored with the long parameter name as key. This will 634# also be used in the href subroutine to convert parameters to their CGI 635# equivalent, and since the href() usage is the most frequent one, we store 636# the name -> CGI key mapping here, instead of the reverse. 637# 638# XXX: Warning: If you touch this, check the search form for updating, 639# too. 640 641our@cgi_param_mapping= ( 642 project =>"p", 643 action =>"a", 644 file_name =>"f", 645 file_parent =>"fp", 646 hash =>"h", 647 hash_parent =>"hp", 648 hash_base =>"hb", 649 hash_parent_base =>"hpb", 650 page =>"pg", 651 order =>"o", 652 searchtext =>"s", 653 searchtype =>"st", 654 snapshot_format =>"sf", 655 extra_options =>"opt", 656 search_use_regexp =>"sr", 657# this must be last entry (for manipulation from JavaScript) 658 javascript =>"js" 659); 660our%cgi_param_mapping=@cgi_param_mapping; 661 662# we will also need to know the possible actions, for validation 663our%actions= ( 664"blame"=> \&git_blame, 665"blame_incremental"=> \&git_blame_incremental, 666"blame_data"=> \&git_blame_data, 667"blobdiff"=> \&git_blobdiff, 668"blobdiff_plain"=> \&git_blobdiff_plain, 669"blob"=> \&git_blob, 670"blob_plain"=> \&git_blob_plain, 671"commitdiff"=> \&git_commitdiff, 672"commitdiff_plain"=> \&git_commitdiff_plain, 673"commit"=> \&git_commit, 674"forks"=> \&git_forks, 675"heads"=> \&git_heads, 676"history"=> \&git_history, 677"log"=> \&git_log, 678"patch"=> \&git_patch, 679"patches"=> \&git_patches, 680"rss"=> \&git_rss, 681"atom"=> \&git_atom, 682"search"=> \&git_search, 683"search_help"=> \&git_search_help, 684"shortlog"=> \&git_shortlog, 685"summary"=> \&git_summary, 686"tag"=> \&git_tag, 687"tags"=> \&git_tags, 688"tree"=> \&git_tree, 689"snapshot"=> \&git_snapshot, 690"object"=> \&git_object, 691# those below don't need $project 692"opml"=> \&git_opml, 693"project_list"=> \&git_project_list, 694"project_index"=> \&git_project_index, 695); 696 697# finally, we have the hash of allowed extra_options for the commands that 698# allow them 699our%allowed_options= ( 700"--no-merges"=> [qw(rss atom log shortlog history)], 701); 702 703# fill %input_params with the CGI parameters. All values except for 'opt' 704# should be single values, but opt can be an array. We should probably 705# build an array of parameters that can be multi-valued, but since for the time 706# being it's only this one, we just single it out 707sub evaluate_query_params { 708our$cgi; 709 710while(my($name,$symbol) =each%cgi_param_mapping) { 711if($symboleq'opt') { 712$input_params{$name} = [$cgi->param($symbol) ]; 713}else{ 714$input_params{$name} =$cgi->param($symbol); 715} 716} 717} 718 719# now read PATH_INFO and update the parameter list for missing parameters 720sub evaluate_path_info { 721return ifdefined$input_params{'project'}; 722return if!$path_info; 723$path_info=~ s,^/+,,; 724return if!$path_info; 725 726# find which part of PATH_INFO is project 727my$project=$path_info; 728$project=~ s,/+$,,; 729while($project&& !check_head_link("$projectroot/$project")) { 730$project=~ s,/*[^/]*$,,; 731} 732return unless$project; 733$input_params{'project'} =$project; 734 735# do not change any parameters if an action is given using the query string 736return if$input_params{'action'}; 737$path_info=~ s,^\Q$project\E/*,,; 738 739# next, check if we have an action 740my$action=$path_info; 741$action=~ s,/.*$,,; 742if(exists$actions{$action}) { 743$path_info=~ s,^$action/*,,; 744$input_params{'action'} =$action; 745} 746 747# list of actions that want hash_base instead of hash, but can have no 748# pathname (f) parameter 749my@wants_base= ( 750'tree', 751'history', 752); 753 754# we want to catch 755# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 756my($parentrefname,$parentpathname,$refname,$pathname) = 757($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 758 759# first, analyze the 'current' part 760if(defined$pathname) { 761# we got "branch:filename" or "branch:dir/" 762# we could use git_get_type(branch:pathname), but: 763# - it needs $git_dir 764# - it does a git() call 765# - the convention of terminating directories with a slash 766# makes it superfluous 767# - embedding the action in the PATH_INFO would make it even 768# more superfluous 769$pathname=~ s,^/+,,; 770if(!$pathname||substr($pathname, -1)eq"/") { 771$input_params{'action'} ||="tree"; 772$pathname=~ s,/$,,; 773}else{ 774# the default action depends on whether we had parent info 775# or not 776if($parentrefname) { 777$input_params{'action'} ||="blobdiff_plain"; 778}else{ 779$input_params{'action'} ||="blob_plain"; 780} 781} 782$input_params{'hash_base'} ||=$refname; 783$input_params{'file_name'} ||=$pathname; 784}elsif(defined$refname) { 785# we got "branch". In this case we have to choose if we have to 786# set hash or hash_base. 787# 788# Most of the actions without a pathname only want hash to be 789# set, except for the ones specified in @wants_base that want 790# hash_base instead. It should also be noted that hand-crafted 791# links having 'history' as an action and no pathname or hash 792# set will fail, but that happens regardless of PATH_INFO. 793$input_params{'action'} ||="shortlog"; 794if(grep{$_eq$input_params{'action'} }@wants_base) { 795$input_params{'hash_base'} ||=$refname; 796}else{ 797$input_params{'hash'} ||=$refname; 798} 799} 800 801# next, handle the 'parent' part, if present 802if(defined$parentrefname) { 803# a missing pathspec defaults to the 'current' filename, allowing e.g. 804# someproject/blobdiff/oldrev..newrev:/filename 805if($parentpathname) { 806$parentpathname=~ s,^/+,,; 807$parentpathname=~ s,/$,,; 808$input_params{'file_parent'} ||=$parentpathname; 809}else{ 810$input_params{'file_parent'} ||=$input_params{'file_name'}; 811} 812# we assume that hash_parent_base is wanted if a path was specified, 813# or if the action wants hash_base instead of hash 814if(defined$input_params{'file_parent'} || 815grep{$_eq$input_params{'action'} }@wants_base) { 816$input_params{'hash_parent_base'} ||=$parentrefname; 817}else{ 818$input_params{'hash_parent'} ||=$parentrefname; 819} 820} 821 822# for the snapshot action, we allow URLs in the form 823# $project/snapshot/$hash.ext 824# where .ext determines the snapshot and gets removed from the 825# passed $refname to provide the $hash. 826# 827# To be able to tell that $refname includes the format extension, we 828# require the following two conditions to be satisfied: 829# - the hash input parameter MUST have been set from the $refname part 830# of the URL (i.e. they must be equal) 831# - the snapshot format MUST NOT have been defined already (e.g. from 832# CGI parameter sf) 833# It's also useless to try any matching unless $refname has a dot, 834# so we check for that too 835if(defined$input_params{'action'} && 836$input_params{'action'}eq'snapshot'&& 837defined$refname&&index($refname,'.') != -1&& 838$refnameeq$input_params{'hash'} && 839!defined$input_params{'snapshot_format'}) { 840# We loop over the known snapshot formats, checking for 841# extensions. Allowed extensions are both the defined suffix 842# (which includes the initial dot already) and the snapshot 843# format key itself, with a prepended dot 844while(my($fmt,$opt) =each%known_snapshot_formats) { 845my$hash=$refname; 846unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 847next; 848} 849my$sfx=$1; 850# a valid suffix was found, so set the snapshot format 851# and reset the hash parameter 852$input_params{'snapshot_format'} =$fmt; 853$input_params{'hash'} =$hash; 854# we also set the format suffix to the one requested 855# in the URL: this way a request for e.g. .tgz returns 856# a .tgz instead of a .tar.gz 857$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 858last; 859} 860} 861} 862 863our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 864$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 865$searchtext,$search_regexp); 866sub evaluate_and_validate_params { 867our$action=$input_params{'action'}; 868if(defined$action) { 869if(!validate_action($action)) { 870 die_error(400,"Invalid action parameter"); 871} 872} 873 874# parameters which are pathnames 875our$project=$input_params{'project'}; 876if(defined$project) { 877if(!validate_project($project)) { 878undef$project; 879 die_error(404,"No such project"); 880} 881} 882 883our$file_name=$input_params{'file_name'}; 884if(defined$file_name) { 885if(!validate_pathname($file_name)) { 886 die_error(400,"Invalid file parameter"); 887} 888} 889 890our$file_parent=$input_params{'file_parent'}; 891if(defined$file_parent) { 892if(!validate_pathname($file_parent)) { 893 die_error(400,"Invalid file parent parameter"); 894} 895} 896 897# parameters which are refnames 898our$hash=$input_params{'hash'}; 899if(defined$hash) { 900if(!validate_refname($hash)) { 901 die_error(400,"Invalid hash parameter"); 902} 903} 904 905our$hash_parent=$input_params{'hash_parent'}; 906if(defined$hash_parent) { 907if(!validate_refname($hash_parent)) { 908 die_error(400,"Invalid hash parent parameter"); 909} 910} 911 912our$hash_base=$input_params{'hash_base'}; 913if(defined$hash_base) { 914if(!validate_refname($hash_base)) { 915 die_error(400,"Invalid hash base parameter"); 916} 917} 918 919our@extra_options= @{$input_params{'extra_options'}}; 920# @extra_options is always defined, since it can only be (currently) set from 921# CGI, and $cgi->param() returns the empty array in array context if the param 922# is not set 923foreachmy$opt(@extra_options) { 924if(not exists$allowed_options{$opt}) { 925 die_error(400,"Invalid option parameter"); 926} 927if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 928 die_error(400,"Invalid option parameter for this action"); 929} 930} 931 932our$hash_parent_base=$input_params{'hash_parent_base'}; 933if(defined$hash_parent_base) { 934if(!validate_refname($hash_parent_base)) { 935 die_error(400,"Invalid hash parent base parameter"); 936} 937} 938 939# other parameters 940our$page=$input_params{'page'}; 941if(defined$page) { 942if($page=~m/[^0-9]/) { 943 die_error(400,"Invalid page parameter"); 944} 945} 946 947our$searchtype=$input_params{'searchtype'}; 948if(defined$searchtype) { 949if($searchtype=~m/[^a-z]/) { 950 die_error(400,"Invalid searchtype parameter"); 951} 952} 953 954our$search_use_regexp=$input_params{'search_use_regexp'}; 955 956our$searchtext=$input_params{'searchtext'}; 957our$search_regexp; 958if(defined$searchtext) { 959if(length($searchtext) <2) { 960 die_error(403,"At least two characters are required for search parameter"); 961} 962$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 963} 964} 965 966# path to the current git repository 967our$git_dir; 968sub evaluate_git_dir { 969our$git_dir="$projectroot/$project"if$project; 970} 971 972our(@snapshot_fmts,$git_avatar); 973sub configure_gitweb_features { 974# list of supported snapshot formats 975our@snapshot_fmts= gitweb_get_feature('snapshot'); 976@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 977 978# check that the avatar feature is set to a known provider name, 979# and for each provider check if the dependencies are satisfied. 980# if the provider name is invalid or the dependencies are not met, 981# reset $git_avatar to the empty string. 982our($git_avatar) = gitweb_get_feature('avatar'); 983if($git_avatareq'gravatar') { 984$git_avatar=''unless(eval{require Digest::MD5;1; }); 985}elsif($git_avatareq'picon') { 986# no dependencies 987}else{ 988$git_avatar=''; 989} 990} 991 992# custom error handler: 'die <message>' is Internal Server Error 993sub handle_errors_html { 994my$msg=shift;# it is already HTML escaped 995 996# to avoid infinite loop where error occurs in die_error, 997# change handler to default handler, disabling handle_errors_html 998 set_message("Error occured when inside die_error:\n$msg"); 9991000# you cannot jump out of die_error when called as error handler;1001# the subroutine set via CGI::Carp::set_message is called _after_1002# HTTP headers are already written, so it cannot write them itself1003 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1);1004}1005set_message(\&handle_errors_html);10061007# dispatch1008sub dispatch {1009if(!defined$action) {1010if(defined$hash) {1011$action= git_get_type($hash);1012}elsif(defined$hash_base&&defined$file_name) {1013$action= git_get_type("$hash_base:$file_name");1014}elsif(defined$project) {1015$action='summary';1016}else{1017$action='project_list';1018}1019}1020if(!defined($actions{$action})) {1021 die_error(400,"Unknown action");1022}1023if($action!~m/^(?:opml|project_list|project_index)$/&&1024!$project) {1025 die_error(400,"Project needed");1026}1027$actions{$action}->();1028}10291030sub run_request {1031our$t0= [Time::HiRes::gettimeofday()]1032ifdefined$t0;10331034 evaluate_uri();1035 evaluate_gitweb_config();1036 evaluate_git_version();1037 check_loadavg();10381039# $projectroot and $projects_list might be set in gitweb config file1040$projects_list||=$projectroot;10411042 evaluate_query_params();1043 evaluate_path_info();1044 evaluate_and_validate_params();1045 evaluate_git_dir();10461047 configure_gitweb_features();10481049 dispatch();1050}10511052our$is_last_request=sub{1};1053our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1054our$CGI='CGI';1055our$cgi;1056sub configure_as_fcgi {1057require CGI::Fast;1058our$CGI='CGI::Fast';10591060my$request_number=0;1061# let each child service 100 requests1062our$is_last_request=sub{ ++$request_number>100};1063}1064sub evaluate_argv {1065my$script_name=$ENV{'SCRIPT_NAME'} ||$ENV{'SCRIPT_FILENAME'} || __FILE__;1066 configure_as_fcgi()1067if$script_name=~/\.fcgi$/;10681069return unless(@ARGV);10701071require Getopt::Long;1072 Getopt::Long::GetOptions(1073'fastcgi|fcgi|f'=> \&configure_as_fcgi,1074'nproc|n=i'=>sub{1075my($arg,$val) =@_;1076return unlesseval{require FCGI::ProcManager;1; };1077my$proc_manager= FCGI::ProcManager->new({1078 n_processes =>$val,1079});1080our$pre_listen_hook=sub{$proc_manager->pm_manage() };1081our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1082our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1083},1084);1085}10861087sub run {1088 evaluate_argv();10891090$pre_listen_hook->()1091if$pre_listen_hook;10921093 REQUEST:1094while($cgi=$CGI->new()) {1095$pre_dispatch_hook->()1096if$pre_dispatch_hook;10971098 run_request();10991100$pre_dispatch_hook->()1101if$post_dispatch_hook;11021103last REQUEST if($is_last_request->());1104}11051106 DONE_GITWEB:11071;1108}11091110run();11111112## ======================================================================1113## action links11141115# possible values of extra options1116# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1117# -replay => 1 - start from a current view (replay with modifications)1118# -path_info => 0|1 - don't use/use path_info URL (if possible)1119sub href {1120my%params=@_;1121# default is to use -absolute url() i.e. $my_uri1122my$href=$params{-full} ?$my_url:$my_uri;11231124$params{'project'} =$projectunlessexists$params{'project'};11251126if($params{-replay}) {1127while(my($name,$symbol) =each%cgi_param_mapping) {1128if(!exists$params{$name}) {1129$params{$name} =$input_params{$name};1130}1131}1132}11331134my$use_pathinfo= gitweb_check_feature('pathinfo');1135if(defined$params{'project'} &&1136(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1137# try to put as many parameters as possible in PATH_INFO:1138# - project name1139# - action1140# - hash_parent or hash_parent_base:/file_parent1141# - hash or hash_base:/filename1142# - the snapshot_format as an appropriate suffix11431144# When the script is the root DirectoryIndex for the domain,1145# $href here would be something like http://gitweb.example.com/1146# Thus, we strip any trailing / from $href, to spare us double1147# slashes in the final URL1148$href=~ s,/$,,;11491150# Then add the project name, if present1151$href.="/".esc_url($params{'project'});1152delete$params{'project'};11531154# since we destructively absorb parameters, we keep this1155# boolean that remembers if we're handling a snapshot1156my$is_snapshot=$params{'action'}eq'snapshot';11571158# Summary just uses the project path URL, any other action is1159# added to the URL1160if(defined$params{'action'}) {1161$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1162delete$params{'action'};1163}11641165# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1166# stripping nonexistent or useless pieces1167$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1168||$params{'hash_parent'} ||$params{'hash'});1169if(defined$params{'hash_base'}) {1170if(defined$params{'hash_parent_base'}) {1171$href.= esc_url($params{'hash_parent_base'});1172# skip the file_parent if it's the same as the file_name1173if(defined$params{'file_parent'}) {1174if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1175delete$params{'file_parent'};1176}elsif($params{'file_parent'} !~/\.\./) {1177$href.=":/".esc_url($params{'file_parent'});1178delete$params{'file_parent'};1179}1180}1181$href.="..";1182delete$params{'hash_parent'};1183delete$params{'hash_parent_base'};1184}elsif(defined$params{'hash_parent'}) {1185$href.= esc_url($params{'hash_parent'})."..";1186delete$params{'hash_parent'};1187}11881189$href.= esc_url($params{'hash_base'});1190if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1191$href.=":/".esc_url($params{'file_name'});1192delete$params{'file_name'};1193}1194delete$params{'hash'};1195delete$params{'hash_base'};1196}elsif(defined$params{'hash'}) {1197$href.= esc_url($params{'hash'});1198delete$params{'hash'};1199}12001201# If the action was a snapshot, we can absorb the1202# snapshot_format parameter too1203if($is_snapshot) {1204my$fmt=$params{'snapshot_format'};1205# snapshot_format should always be defined when href()1206# is called, but just in case some code forgets, we1207# fall back to the default1208$fmt||=$snapshot_fmts[0];1209$href.=$known_snapshot_formats{$fmt}{'suffix'};1210delete$params{'snapshot_format'};1211}1212}12131214# now encode the parameters explicitly1215my@result= ();1216for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1217my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1218if(defined$params{$name}) {1219if(ref($params{$name})eq"ARRAY") {1220foreachmy$par(@{$params{$name}}) {1221push@result,$symbol."=". esc_param($par);1222}1223}else{1224push@result,$symbol."=". esc_param($params{$name});1225}1226}1227}1228$href.="?".join(';',@result)ifscalar@result;12291230return$href;1231}123212331234## ======================================================================1235## validation, quoting/unquoting and escaping12361237sub validate_action {1238my$input=shift||returnundef;1239returnundefunlessexists$actions{$input};1240return$input;1241}12421243sub validate_project {1244my$input=shift||returnundef;1245if(!validate_pathname($input) ||1246!(-d "$projectroot/$input") ||1247!check_export_ok("$projectroot/$input") ||1248($strict_export&& !project_in_list($input))) {1249returnundef;1250}else{1251return$input;1252}1253}12541255sub validate_pathname {1256my$input=shift||returnundef;12571258# no '.' or '..' as elements of path, i.e. no '.' nor '..'1259# at the beginning, at the end, and between slashes.1260# also this catches doubled slashes1261if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1262returnundef;1263}1264# no null characters1265if($input=~m!\0!) {1266returnundef;1267}1268return$input;1269}12701271sub validate_refname {1272my$input=shift||returnundef;12731274# textual hashes are O.K.1275if($input=~m/^[0-9a-fA-F]{40}$/) {1276return$input;1277}1278# it must be correct pathname1279$input= validate_pathname($input)1280orreturnundef;1281# restrictions on ref name according to git-check-ref-format1282if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1283returnundef;1284}1285return$input;1286}12871288# decode sequences of octets in utf8 into Perl's internal form,1289# which is utf-8 with utf8 flag set if needed. gitweb writes out1290# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1291sub to_utf8 {1292my$str=shift;1293returnundefunlessdefined$str;1294if(utf8::valid($str)) {1295 utf8::decode($str);1296return$str;1297}else{1298return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1299}1300}13011302# quote unsafe chars, but keep the slash, even when it's not1303# correct, but quoted slashes look too horrible in bookmarks1304sub esc_param {1305my$str=shift;1306returnundefunlessdefined$str;1307$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1308$str=~s/ /\+/g;1309return$str;1310}13111312# quote unsafe chars in whole URL, so some charactrs cannot be quoted1313sub esc_url {1314my$str=shift;1315returnundefunlessdefined$str;1316$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1317$str=~s/\+/%2B/g;1318$str=~s/ /\+/g;1319return$str;1320}13211322# replace invalid utf8 character with SUBSTITUTION sequence1323sub esc_html {1324my$str=shift;1325my%opts=@_;13261327returnundefunlessdefined$str;13281329$str= to_utf8($str);1330$str=$cgi->escapeHTML($str);1331if($opts{'-nbsp'}) {1332$str=~s/ / /g;1333}1334$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1335return$str;1336}13371338# quote control characters and escape filename to HTML1339sub esc_path {1340my$str=shift;1341my%opts=@_;13421343returnundefunlessdefined$str;13441345$str= to_utf8($str);1346$str=$cgi->escapeHTML($str);1347if($opts{'-nbsp'}) {1348$str=~s/ / /g;1349}1350$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1351return$str;1352}13531354# Make control characters "printable", using character escape codes (CEC)1355sub quot_cec {1356my$cntrl=shift;1357my%opts=@_;1358my%es= (# character escape codes, aka escape sequences1359"\t"=>'\t',# tab (HT)1360"\n"=>'\n',# line feed (LF)1361"\r"=>'\r',# carrige return (CR)1362"\f"=>'\f',# form feed (FF)1363"\b"=>'\b',# backspace (BS)1364"\a"=>'\a',# alarm (bell) (BEL)1365"\e"=>'\e',# escape (ESC)1366"\013"=>'\v',# vertical tab (VT)1367"\000"=>'\0',# nul character (NUL)1368);1369my$chr= ( (exists$es{$cntrl})1370?$es{$cntrl}1371:sprintf('\%2x',ord($cntrl)) );1372if($opts{-nohtml}) {1373return$chr;1374}else{1375return"<span class=\"cntrl\">$chr</span>";1376}1377}13781379# Alternatively use unicode control pictures codepoints,1380# Unicode "printable representation" (PR)1381sub quot_upr {1382my$cntrl=shift;1383my%opts=@_;13841385my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1386if($opts{-nohtml}) {1387return$chr;1388}else{1389return"<span class=\"cntrl\">$chr</span>";1390}1391}13921393# git may return quoted and escaped filenames1394sub unquote {1395my$str=shift;13961397sub unq {1398my$seq=shift;1399my%es= (# character escape codes, aka escape sequences1400't'=>"\t",# tab (HT, TAB)1401'n'=>"\n",# newline (NL)1402'r'=>"\r",# return (CR)1403'f'=>"\f",# form feed (FF)1404'b'=>"\b",# backspace (BS)1405'a'=>"\a",# alarm (bell) (BEL)1406'e'=>"\e",# escape (ESC)1407'v'=>"\013",# vertical tab (VT)1408);14091410if($seq=~m/^[0-7]{1,3}$/) {1411# octal char sequence1412returnchr(oct($seq));1413}elsif(exists$es{$seq}) {1414# C escape sequence, aka character escape code1415return$es{$seq};1416}1417# quoted ordinary character1418return$seq;1419}14201421if($str=~m/^"(.*)"$/) {1422# needs unquoting1423$str=$1;1424$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1425}1426return$str;1427}14281429# escape tabs (convert tabs to spaces)1430sub untabify {1431my$line=shift;14321433while((my$pos=index($line,"\t")) != -1) {1434if(my$count= (8- ($pos%8))) {1435my$spaces=' ' x $count;1436$line=~s/\t/$spaces/;1437}1438}14391440return$line;1441}14421443sub project_in_list {1444my$project=shift;1445my@list= git_get_projects_list();1446return@list&&scalar(grep{$_->{'path'}eq$project}@list);1447}14481449## ----------------------------------------------------------------------1450## HTML aware string manipulation14511452# Try to chop given string on a word boundary between position1453# $len and $len+$add_len. If there is no word boundary there,1454# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1455# (marking chopped part) would be longer than given string.1456sub chop_str {1457my$str=shift;1458my$len=shift;1459my$add_len=shift||10;1460my$where=shift||'right';# 'left' | 'center' | 'right'14611462# Make sure perl knows it is utf8 encoded so we don't1463# cut in the middle of a utf8 multibyte char.1464$str= to_utf8($str);14651466# allow only $len chars, but don't cut a word if it would fit in $add_len1467# if it doesn't fit, cut it if it's still longer than the dots we would add1468# remove chopped character entities entirely14691470# when chopping in the middle, distribute $len into left and right part1471# return early if chopping wouldn't make string shorter1472if($whereeq'center') {1473return$strif($len+5>=length($str));# filler is length 51474$len=int($len/2);1475}else{1476return$strif($len+4>=length($str));# filler is length 41477}14781479# regexps: ending and beginning with word part up to $add_len1480my$endre=qr/.{$len}\w{0,$add_len}/;1481my$begre=qr/\w{0,$add_len}.{$len}/;14821483if($whereeq'left') {1484$str=~m/^(.*?)($begre)$/;1485my($lead,$body) = ($1,$2);1486if(length($lead) >4) {1487$lead=" ...";1488}1489return"$lead$body";14901491}elsif($whereeq'center') {1492$str=~m/^($endre)(.*)$/;1493my($left,$str) = ($1,$2);1494$str=~m/^(.*?)($begre)$/;1495my($mid,$right) = ($1,$2);1496if(length($mid) >5) {1497$mid=" ... ";1498}1499return"$left$mid$right";15001501}else{1502$str=~m/^($endre)(.*)$/;1503my$body=$1;1504my$tail=$2;1505if(length($tail) >4) {1506$tail="... ";1507}1508return"$body$tail";1509}1510}15111512# takes the same arguments as chop_str, but also wraps a <span> around the1513# result with a title attribute if it does get chopped. Additionally, the1514# string is HTML-escaped.1515sub chop_and_escape_str {1516my($str) =@_;15171518my$chopped= chop_str(@_);1519if($choppedeq$str) {1520return esc_html($chopped);1521}else{1522$str=~s/[[:cntrl:]]/?/g;1523return$cgi->span({-title=>$str}, esc_html($chopped));1524}1525}15261527## ----------------------------------------------------------------------1528## functions returning short strings15291530# CSS class for given age value (in seconds)1531sub age_class {1532my$age=shift;15331534if(!defined$age) {1535return"noage";1536}elsif($age<60*60*2) {1537return"age0";1538}elsif($age<60*60*24*2) {1539return"age1";1540}else{1541return"age2";1542}1543}15441545# convert age in seconds to "nn units ago" string1546sub age_string {1547my$age=shift;1548my$age_str;15491550if($age>60*60*24*365*2) {1551$age_str= (int$age/60/60/24/365);1552$age_str.=" years ago";1553}elsif($age>60*60*24*(365/12)*2) {1554$age_str=int$age/60/60/24/(365/12);1555$age_str.=" months ago";1556}elsif($age>60*60*24*7*2) {1557$age_str=int$age/60/60/24/7;1558$age_str.=" weeks ago";1559}elsif($age>60*60*24*2) {1560$age_str=int$age/60/60/24;1561$age_str.=" days ago";1562}elsif($age>60*60*2) {1563$age_str=int$age/60/60;1564$age_str.=" hours ago";1565}elsif($age>60*2) {1566$age_str=int$age/60;1567$age_str.=" min ago";1568}elsif($age>2) {1569$age_str=int$age;1570$age_str.=" sec ago";1571}else{1572$age_str.=" right now";1573}1574return$age_str;1575}15761577useconstant{1578 S_IFINVALID =>0030000,1579 S_IFGITLINK =>0160000,1580};15811582# submodule/subproject, a commit object reference1583sub S_ISGITLINK {1584my$mode=shift;15851586return(($mode& S_IFMT) == S_IFGITLINK)1587}15881589# convert file mode in octal to symbolic file mode string1590sub mode_str {1591my$mode=oct shift;15921593if(S_ISGITLINK($mode)) {1594return'm---------';1595}elsif(S_ISDIR($mode& S_IFMT)) {1596return'drwxr-xr-x';1597}elsif(S_ISLNK($mode)) {1598return'lrwxrwxrwx';1599}elsif(S_ISREG($mode)) {1600# git cares only about the executable bit1601if($mode& S_IXUSR) {1602return'-rwxr-xr-x';1603}else{1604return'-rw-r--r--';1605};1606}else{1607return'----------';1608}1609}16101611# convert file mode in octal to file type string1612sub file_type {1613my$mode=shift;16141615if($mode!~m/^[0-7]+$/) {1616return$mode;1617}else{1618$mode=oct$mode;1619}16201621if(S_ISGITLINK($mode)) {1622return"submodule";1623}elsif(S_ISDIR($mode& S_IFMT)) {1624return"directory";1625}elsif(S_ISLNK($mode)) {1626return"symlink";1627}elsif(S_ISREG($mode)) {1628return"file";1629}else{1630return"unknown";1631}1632}16331634# convert file mode in octal to file type description string1635sub file_type_long {1636my$mode=shift;16371638if($mode!~m/^[0-7]+$/) {1639return$mode;1640}else{1641$mode=oct$mode;1642}16431644if(S_ISGITLINK($mode)) {1645return"submodule";1646}elsif(S_ISDIR($mode& S_IFMT)) {1647return"directory";1648}elsif(S_ISLNK($mode)) {1649return"symlink";1650}elsif(S_ISREG($mode)) {1651if($mode& S_IXUSR) {1652return"executable";1653}else{1654return"file";1655};1656}else{1657return"unknown";1658}1659}166016611662## ----------------------------------------------------------------------1663## functions returning short HTML fragments, or transforming HTML fragments1664## which don't belong to other sections16651666# format line of commit message.1667sub format_log_line_html {1668my$line=shift;16691670$line= esc_html($line, -nbsp=>1);1671$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1672$cgi->a({-href => href(action=>"object", hash=>$1),1673-class=>"text"},$1);1674}eg;16751676return$line;1677}16781679# format marker of refs pointing to given object16801681# the destination action is chosen based on object type and current context:1682# - for annotated tags, we choose the tag view unless it's the current view1683# already, in which case we go to shortlog view1684# - for other refs, we keep the current view if we're in history, shortlog or1685# log view, and select shortlog otherwise1686sub format_ref_marker {1687my($refs,$id) =@_;1688my$markers='';16891690if(defined$refs->{$id}) {1691foreachmy$ref(@{$refs->{$id}}) {1692# this code exploits the fact that non-lightweight tags are the1693# only indirect objects, and that they are the only objects for which1694# we want to use tag instead of shortlog as action1695my($type,$name) =qw();1696my$indirect= ($ref=~s/\^\{\}$//);1697# e.g. tags/v2.6.11 or heads/next1698if($ref=~m!^(.*?)s?/(.*)$!) {1699$type=$1;1700$name=$2;1701}else{1702$type="ref";1703$name=$ref;1704}17051706my$class=$type;1707$class.=" indirect"if$indirect;17081709my$dest_action="shortlog";17101711if($indirect) {1712$dest_action="tag"unless$actioneq"tag";1713}elsif($action=~/^(history|(short)?log)$/) {1714$dest_action=$action;1715}17161717my$dest="";1718$dest.="refs/"unless$ref=~ m!^refs/!;1719$dest.=$ref;17201721my$link=$cgi->a({1722-href => href(1723 action=>$dest_action,1724 hash=>$dest1725)},$name);17261727$markers.=" <span class=\"$class\"title=\"$ref\">".1728$link."</span>";1729}1730}17311732if($markers) {1733return' <span class="refs">'.$markers.'</span>';1734}else{1735return"";1736}1737}17381739# format, perhaps shortened and with markers, title line1740sub format_subject_html {1741my($long,$short,$href,$extra) =@_;1742$extra=''unlessdefined($extra);17431744if(length($short) <length($long)) {1745$long=~s/[[:cntrl:]]/?/g;1746return$cgi->a({-href =>$href, -class=>"list subject",1747-title => to_utf8($long)},1748 esc_html($short)) .$extra;1749}else{1750return$cgi->a({-href =>$href, -class=>"list subject"},1751 esc_html($long)) .$extra;1752}1753}17541755# Rather than recomputing the url for an email multiple times, we cache it1756# after the first hit. This gives a visible benefit in views where the avatar1757# for the same email is used repeatedly (e.g. shortlog).1758# The cache is shared by all avatar engines (currently gravatar only), which1759# are free to use it as preferred. Since only one avatar engine is used for any1760# given page, there's no risk for cache conflicts.1761our%avatar_cache= ();17621763# Compute the picon url for a given email, by using the picon search service over at1764# http://www.cs.indiana.edu/picons/search.html1765sub picon_url {1766my$email=lc shift;1767if(!$avatar_cache{$email}) {1768my($user,$domain) =split('@',$email);1769$avatar_cache{$email} =1770"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1771"$domain/$user/".1772"users+domains+unknown/up/single";1773}1774return$avatar_cache{$email};1775}17761777# Compute the gravatar url for a given email, if it's not in the cache already.1778# Gravatar stores only the part of the URL before the size, since that's the1779# one computationally more expensive. This also allows reuse of the cache for1780# different sizes (for this particular engine).1781sub gravatar_url {1782my$email=lc shift;1783my$size=shift;1784$avatar_cache{$email} ||=1785"http://www.gravatar.com/avatar/".1786 Digest::MD5::md5_hex($email) ."?s=";1787return$avatar_cache{$email} .$size;1788}17891790# Insert an avatar for the given $email at the given $size if the feature1791# is enabled.1792sub git_get_avatar {1793my($email,%opts) =@_;1794my$pre_white= ($opts{-pad_before} ?" ":"");1795my$post_white= ($opts{-pad_after} ?" ":"");1796$opts{-size} ||='default';1797my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1798my$url="";1799if($git_avatareq'gravatar') {1800$url= gravatar_url($email,$size);1801}elsif($git_avatareq'picon') {1802$url= picon_url($email);1803}1804# Other providers can be added by extending the if chain, defining $url1805# as needed. If no variant puts something in $url, we assume avatars1806# are completely disabled/unavailable.1807if($url) {1808return$pre_white.1809"<img width=\"$size\"".1810"class=\"avatar\"".1811"src=\"$url\"".1812"alt=\"\"".1813"/>".$post_white;1814}else{1815return"";1816}1817}18181819sub format_search_author {1820my($author,$searchtype,$displaytext) =@_;1821my$have_search= gitweb_check_feature('search');18221823if($have_search) {1824my$performed="";1825if($searchtypeeq'author') {1826$performed="authored";1827}elsif($searchtypeeq'committer') {1828$performed="committed";1829}18301831return$cgi->a({-href => href(action=>"search", hash=>$hash,1832 searchtext=>$author,1833 searchtype=>$searchtype),class=>"list",1834 title=>"Search for commits$performedby$author"},1835$displaytext);18361837}else{1838return$displaytext;1839}1840}18411842# format the author name of the given commit with the given tag1843# the author name is chopped and escaped according to the other1844# optional parameters (see chop_str).1845sub format_author_html {1846my$tag=shift;1847my$co=shift;1848my$author= chop_and_escape_str($co->{'author_name'},@_);1849return"<$tagclass=\"author\">".1850 format_search_author($co->{'author_name'},"author",1851 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1852$author) .1853"</$tag>";1854}18551856# format git diff header line, i.e. "diff --(git|combined|cc) ..."1857sub format_git_diff_header_line {1858my$line=shift;1859my$diffinfo=shift;1860my($from,$to) =@_;18611862if($diffinfo->{'nparents'}) {1863# combined diff1864$line=~s!^(diff (.*?) )"?.*$!$1!;1865if($to->{'href'}) {1866$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1867 esc_path($to->{'file'}));1868}else{# file was deleted (no href)1869$line.= esc_path($to->{'file'});1870}1871}else{1872# "ordinary" diff1873$line=~s!^(diff (.*?) )"?a/.*$!$1!;1874if($from->{'href'}) {1875$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1876'a/'. esc_path($from->{'file'}));1877}else{# file was added (no href)1878$line.='a/'. esc_path($from->{'file'});1879}1880$line.=' ';1881if($to->{'href'}) {1882$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1883'b/'. esc_path($to->{'file'}));1884}else{# file was deleted1885$line.='b/'. esc_path($to->{'file'});1886}1887}18881889return"<div class=\"diff header\">$line</div>\n";1890}18911892# format extended diff header line, before patch itself1893sub format_extended_diff_header_line {1894my$line=shift;1895my$diffinfo=shift;1896my($from,$to) =@_;18971898# match <path>1899if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1900$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1901 esc_path($from->{'file'}));1902}1903if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1904$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1905 esc_path($to->{'file'}));1906}1907# match single <mode>1908if($line=~m/\s(\d{6})$/) {1909$line.='<span class="info"> ('.1910 file_type_long($1) .1911')</span>';1912}1913# match <hash>1914if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1915# can match only for combined diff1916$line='index ';1917for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1918if($from->{'href'}[$i]) {1919$line.=$cgi->a({-href=>$from->{'href'}[$i],1920-class=>"hash"},1921substr($diffinfo->{'from_id'}[$i],0,7));1922}else{1923$line.='0' x 7;1924}1925# separator1926$line.=','if($i<$diffinfo->{'nparents'} -1);1927}1928$line.='..';1929if($to->{'href'}) {1930$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1931substr($diffinfo->{'to_id'},0,7));1932}else{1933$line.='0' x 7;1934}19351936}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1937# can match only for ordinary diff1938my($from_link,$to_link);1939if($from->{'href'}) {1940$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1941substr($diffinfo->{'from_id'},0,7));1942}else{1943$from_link='0' x 7;1944}1945if($to->{'href'}) {1946$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1947substr($diffinfo->{'to_id'},0,7));1948}else{1949$to_link='0' x 7;1950}1951my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1952$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1953}19541955return$line."<br/>\n";1956}19571958# format from-file/to-file diff header1959sub format_diff_from_to_header {1960my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1961my$line;1962my$result='';19631964$line=$from_line;1965#assert($line =~ m/^---/) if DEBUG;1966# no extra formatting for "^--- /dev/null"1967if(!$diffinfo->{'nparents'}) {1968# ordinary (single parent) diff1969if($line=~m!^--- "?a/!) {1970if($from->{'href'}) {1971$line='--- a/'.1972$cgi->a({-href=>$from->{'href'}, -class=>"path"},1973 esc_path($from->{'file'}));1974}else{1975$line='--- a/'.1976 esc_path($from->{'file'});1977}1978}1979$result.= qq!<div class="diff from_file">$line</div>\n!;19801981}else{1982# combined diff (merge commit)1983for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1984if($from->{'href'}[$i]) {1985$line='--- '.1986$cgi->a({-href=>href(action=>"blobdiff",1987 hash_parent=>$diffinfo->{'from_id'}[$i],1988 hash_parent_base=>$parents[$i],1989 file_parent=>$from->{'file'}[$i],1990 hash=>$diffinfo->{'to_id'},1991 hash_base=>$hash,1992 file_name=>$to->{'file'}),1993-class=>"path",1994-title=>"diff". ($i+1)},1995$i+1) .1996'/'.1997$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1998 esc_path($from->{'file'}[$i]));1999}else{2000$line='--- /dev/null';2001}2002$result.= qq!<div class="diff from_file">$line</div>\n!;2003}2004}20052006$line=$to_line;2007#assert($line =~ m/^\+\+\+/) if DEBUG;2008# no extra formatting for "^+++ /dev/null"2009if($line=~m!^\+\+\+ "?b/!) {2010if($to->{'href'}) {2011$line='+++ b/'.2012$cgi->a({-href=>$to->{'href'}, -class=>"path"},2013 esc_path($to->{'file'}));2014}else{2015$line='+++ b/'.2016 esc_path($to->{'file'});2017}2018}2019$result.= qq!<div class="diff to_file">$line</div>\n!;20202021return$result;2022}20232024# create note for patch simplified by combined diff2025sub format_diff_cc_simplified {2026my($diffinfo,@parents) =@_;2027my$result='';20282029$result.="<div class=\"diff header\">".2030"diff --cc ";2031if(!is_deleted($diffinfo)) {2032$result.=$cgi->a({-href => href(action=>"blob",2033 hash_base=>$hash,2034 hash=>$diffinfo->{'to_id'},2035 file_name=>$diffinfo->{'to_file'}),2036-class=>"path"},2037 esc_path($diffinfo->{'to_file'}));2038}else{2039$result.= esc_path($diffinfo->{'to_file'});2040}2041$result.="</div>\n".# class="diff header"2042"<div class=\"diff nodifferences\">".2043"Simple merge".2044"</div>\n";# class="diff nodifferences"20452046return$result;2047}20482049# format patch (diff) line (not to be used for diff headers)2050sub format_diff_line {2051my$line=shift;2052my($from,$to) =@_;2053my$diff_class="";20542055chomp$line;20562057if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2058# combined diff2059my$prefix=substr($line,0,scalar@{$from->{'href'}});2060if($line=~m/^\@{3}/) {2061$diff_class=" chunk_header";2062}elsif($line=~m/^\\/) {2063$diff_class=" incomplete";2064}elsif($prefix=~tr/+/+/) {2065$diff_class=" add";2066}elsif($prefix=~tr/-/-/) {2067$diff_class=" rem";2068}2069}else{2070# assume ordinary diff2071my$char=substr($line,0,1);2072if($chareq'+') {2073$diff_class=" add";2074}elsif($chareq'-') {2075$diff_class=" rem";2076}elsif($chareq'@') {2077$diff_class=" chunk_header";2078}elsif($chareq"\\") {2079$diff_class=" incomplete";2080}2081}2082$line= untabify($line);2083if($from&&$to&&$line=~m/^\@{2} /) {2084my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2085$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;20862087$from_lines=0unlessdefined$from_lines;2088$to_lines=0unlessdefined$to_lines;20892090if($from->{'href'}) {2091$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2092-class=>"list"},$from_text);2093}2094if($to->{'href'}) {2095$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2096-class=>"list"},$to_text);2097}2098$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2099"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2100return"<div class=\"diff$diff_class\">$line</div>\n";2101}elsif($from&&$to&&$line=~m/^\@{3}/) {2102my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2103my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);21042105@from_text=split(' ',$ranges);2106for(my$i=0;$i<@from_text; ++$i) {2107($from_start[$i],$from_nlines[$i]) =2108(split(',',substr($from_text[$i],1)),0);2109}21102111$to_text=pop@from_text;2112$to_start=pop@from_start;2113$to_nlines=pop@from_nlines;21142115$line="<span class=\"chunk_info\">$prefix";2116for(my$i=0;$i<@from_text; ++$i) {2117if($from->{'href'}[$i]) {2118$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2119-class=>"list"},$from_text[$i]);2120}else{2121$line.=$from_text[$i];2122}2123$line.=" ";2124}2125if($to->{'href'}) {2126$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2127-class=>"list"},$to_text);2128}else{2129$line.=$to_text;2130}2131$line.="$prefix</span>".2132"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2133return"<div class=\"diff$diff_class\">$line</div>\n";2134}2135return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2136}21372138# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2139# linked. Pass the hash of the tree/commit to snapshot.2140sub format_snapshot_links {2141my($hash) =@_;2142my$num_fmts=@snapshot_fmts;2143if($num_fmts>1) {2144# A parenthesized list of links bearing format names.2145# e.g. "snapshot (_tar.gz_ _zip_)"2146return"snapshot (".join(' ',map2147$cgi->a({2148-href => href(2149 action=>"snapshot",2150 hash=>$hash,2151 snapshot_format=>$_2152)2153},$known_snapshot_formats{$_}{'display'})2154,@snapshot_fmts) .")";2155}elsif($num_fmts==1) {2156# A single "snapshot" link whose tooltip bears the format name.2157# i.e. "_snapshot_"2158my($fmt) =@snapshot_fmts;2159return2160$cgi->a({2161-href => href(2162 action=>"snapshot",2163 hash=>$hash,2164 snapshot_format=>$fmt2165),2166-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2167},"snapshot");2168}else{# $num_fmts == 02169returnundef;2170}2171}21722173## ......................................................................2174## functions returning values to be passed, perhaps after some2175## transformation, to other functions; e.g. returning arguments to href()21762177# returns hash to be passed to href to generate gitweb URL2178# in -title key it returns description of link2179sub get_feed_info {2180my$format=shift||'Atom';2181my%res= (action =>lc($format));21822183# feed links are possible only for project views2184return unless(defined$project);2185# some views should link to OPML, or to generic project feed,2186# or don't have specific feed yet (so they should use generic)2187return if($action=~/^(?:tags|heads|forks|tag|search)$/x);21882189my$branch;2190# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2191# from tag links; this also makes possible to detect branch links2192if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2193(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2194$branch=$1;2195}2196# find log type for feed description (title)2197my$type='log';2198if(defined$file_name) {2199$type="history of$file_name";2200$type.="/"if($actioneq'tree');2201$type.=" on '$branch'"if(defined$branch);2202}else{2203$type="log of$branch"if(defined$branch);2204}22052206$res{-title} =$type;2207$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2208$res{'file_name'} =$file_name;22092210return%res;2211}22122213## ----------------------------------------------------------------------2214## git utility subroutines, invoking git commands22152216# returns path to the core git executable and the --git-dir parameter as list2217sub git_cmd {2218$number_of_git_cmds++;2219return$GIT,'--git-dir='.$git_dir;2220}22212222# quote the given arguments for passing them to the shell2223# quote_command("command", "arg 1", "arg with ' and ! characters")2224# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2225# Try to avoid using this function wherever possible.2226sub quote_command {2227returnjoin(' ',2228map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2229}22302231# get HEAD ref of given project as hash2232sub git_get_head_hash {2233return git_get_full_hash(shift,'HEAD');2234}22352236sub git_get_full_hash {2237return git_get_hash(@_);2238}22392240sub git_get_short_hash {2241return git_get_hash(@_,'--short=7');2242}22432244sub git_get_hash {2245my($project,$hash,@options) =@_;2246my$o_git_dir=$git_dir;2247my$retval=undef;2248$git_dir="$projectroot/$project";2249if(open my$fd,'-|', git_cmd(),'rev-parse',2250'--verify','-q',@options,$hash) {2251$retval= <$fd>;2252chomp$retvalifdefined$retval;2253close$fd;2254}2255if(defined$o_git_dir) {2256$git_dir=$o_git_dir;2257}2258return$retval;2259}22602261# get type of given object2262sub git_get_type {2263my$hash=shift;22642265open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2266my$type= <$fd>;2267close$fdorreturn;2268chomp$type;2269return$type;2270}22712272# repository configuration2273our$config_file='';2274our%config;22752276# store multiple values for single key as anonymous array reference2277# single values stored directly in the hash, not as [ <value> ]2278sub hash_set_multi {2279my($hash,$key,$value) =@_;22802281if(!exists$hash->{$key}) {2282$hash->{$key} =$value;2283}elsif(!ref$hash->{$key}) {2284$hash->{$key} = [$hash->{$key},$value];2285}else{2286push@{$hash->{$key}},$value;2287}2288}22892290# return hash of git project configuration2291# optionally limited to some section, e.g. 'gitweb'2292sub git_parse_project_config {2293my$section_regexp=shift;2294my%config;22952296local$/="\0";22972298open my$fh,"-|", git_cmd(),"config",'-z','-l',2299orreturn;23002301while(my$keyval= <$fh>) {2302chomp$keyval;2303my($key,$value) =split(/\n/,$keyval,2);23042305 hash_set_multi(\%config,$key,$value)2306if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2307}2308close$fh;23092310return%config;2311}23122313# convert config value to boolean: 'true' or 'false'2314# no value, number > 0, 'true' and 'yes' values are true2315# rest of values are treated as false (never as error)2316sub config_to_bool {2317my$val=shift;23182319return1if!defined$val;# section.key23202321# strip leading and trailing whitespace2322$val=~s/^\s+//;2323$val=~s/\s+$//;23242325return(($val=~/^\d+$/&&$val) ||# section.key = 12326($val=~/^(?:true|yes)$/i));# section.key = true2327}23282329# convert config value to simple decimal number2330# an optional value suffix of 'k', 'm', or 'g' will cause the value2331# to be multiplied by 1024, 1048576, or 10737418242332sub config_to_int {2333my$val=shift;23342335# strip leading and trailing whitespace2336$val=~s/^\s+//;2337$val=~s/\s+$//;23382339if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2340$unit=lc($unit);2341# unknown unit is treated as 12342return$num* ($uniteq'g'?1073741824:2343$uniteq'm'?1048576:2344$uniteq'k'?1024:1);2345}2346return$val;2347}23482349# convert config value to array reference, if needed2350sub config_to_multi {2351my$val=shift;23522353returnref($val) ?$val: (defined($val) ? [$val] : []);2354}23552356sub git_get_project_config {2357my($key,$type) =@_;23582359return unlessdefined$git_dir;23602361# key sanity check2362return unless($key);2363$key=~s/^gitweb\.//;2364return if($key=~m/\W/);23652366# type sanity check2367if(defined$type) {2368$type=~s/^--//;2369$type=undef2370unless($typeeq'bool'||$typeeq'int');2371}23722373# get config2374if(!defined$config_file||2375$config_filene"$git_dir/config") {2376%config= git_parse_project_config('gitweb');2377$config_file="$git_dir/config";2378}23792380# check if config variable (key) exists2381return unlessexists$config{"gitweb.$key"};23822383# ensure given type2384if(!defined$type) {2385return$config{"gitweb.$key"};2386}elsif($typeeq'bool') {2387# backward compatibility: 'git config --bool' returns true/false2388return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2389}elsif($typeeq'int') {2390return config_to_int($config{"gitweb.$key"});2391}2392return$config{"gitweb.$key"};2393}23942395# get hash of given path at given ref2396sub git_get_hash_by_path {2397my$base=shift;2398my$path=shift||returnundef;2399my$type=shift;24002401$path=~ s,/+$,,;24022403open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2404or die_error(500,"Open git-ls-tree failed");2405my$line= <$fd>;2406close$fdorreturnundef;24072408if(!defined$line) {2409# there is no tree or hash given by $path at $base2410returnundef;2411}24122413#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2414$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2415if(defined$type&&$typene$2) {2416# type doesn't match2417returnundef;2418}2419return$3;2420}24212422# get path of entry with given hash at given tree-ish (ref)2423# used to get 'from' filename for combined diff (merge commit) for renames2424sub git_get_path_by_hash {2425my$base=shift||return;2426my$hash=shift||return;24272428local$/="\0";24292430open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2431orreturnundef;2432while(my$line= <$fd>) {2433chomp$line;24342435#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2436#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2437if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2438close$fd;2439return$1;2440}2441}2442close$fd;2443returnundef;2444}24452446## ......................................................................2447## git utility functions, directly accessing git repository24482449sub git_get_project_description {2450my$path=shift;24512452$git_dir="$projectroot/$path";2453open my$fd,'<',"$git_dir/description"2454orreturn git_get_project_config('description');2455my$descr= <$fd>;2456close$fd;2457if(defined$descr) {2458chomp$descr;2459}2460return$descr;2461}24622463sub git_get_project_ctags {2464my$path=shift;2465my$ctags= {};24662467$git_dir="$projectroot/$path";2468opendir my$dh,"$git_dir/ctags"2469orreturn$ctags;2470foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2471open my$ct,'<',$_ornext;2472my$val= <$ct>;2473chomp$val;2474close$ct;2475my$ctag=$_;$ctag=~ s#.*/##;2476$ctags->{$ctag} =$val;2477}2478closedir$dh;2479$ctags;2480}24812482sub git_populate_project_tagcloud {2483my$ctags=shift;24842485# First, merge different-cased tags; tags vote on casing2486my%ctags_lc;2487foreach(keys%$ctags) {2488$ctags_lc{lc$_}->{count} +=$ctags->{$_};2489if(not$ctags_lc{lc$_}->{topcount}2490or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2491$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2492$ctags_lc{lc$_}->{topname} =$_;2493}2494}24952496my$cloud;2497if(eval{require HTML::TagCloud;1; }) {2498$cloud= HTML::TagCloud->new;2499foreach(sort keys%ctags_lc) {2500# Pad the title with spaces so that the cloud looks2501# less crammed.2502my$title=$ctags_lc{$_}->{topname};2503$title=~s/ / /g;2504$title=~s/^/ /g;2505$title=~s/$/ /g;2506$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2507}2508}else{2509$cloud= \%ctags_lc;2510}2511$cloud;2512}25132514sub git_show_project_tagcloud {2515my($cloud,$count) =@_;2516print STDERR ref($cloud)."..\n";2517if(ref$cloudeq'HTML::TagCloud') {2518return$cloud->html_and_css($count);2519}else{2520my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2521return'<p align="center">'.join(', ',map{2522"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2523}splice(@tags,0,$count)) .'</p>';2524}2525}25262527sub git_get_project_url_list {2528my$path=shift;25292530$git_dir="$projectroot/$path";2531open my$fd,'<',"$git_dir/cloneurl"2532orreturnwantarray?2533@{ config_to_multi(git_get_project_config('url')) } :2534 config_to_multi(git_get_project_config('url'));2535my@git_project_url_list=map{chomp;$_} <$fd>;2536close$fd;25372538returnwantarray?@git_project_url_list: \@git_project_url_list;2539}25402541sub git_get_projects_list {2542my($filter) =@_;2543my@list;25442545$filter||='';2546$filter=~s/\.git$//;25472548my$check_forks= gitweb_check_feature('forks');25492550if(-d $projects_list) {2551# search in directory2552my$dir=$projects_list. ($filter?"/$filter":'');2553# remove the trailing "/"2554$dir=~s!/+$!!;2555my$pfxlen=length("$dir");2556my$pfxdepth= ($dir=~tr!/!!);25572558 File::Find::find({2559 follow_fast =>1,# follow symbolic links2560 follow_skip =>2,# ignore duplicates2561 dangling_symlinks =>0,# ignore dangling symlinks, silently2562 wanted =>sub{2563# global variables2564our$project_maxdepth;2565our$projectroot;2566# skip project-list toplevel, if we get it.2567return if(m!^[/.]$!);2568# only directories can be git repositories2569return unless(-d $_);2570# don't traverse too deep (Find is super slow on os x)2571if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2572$File::Find::prune =1;2573return;2574}25752576my$subdir=substr($File::Find::name,$pfxlen+1);2577# we check related file in $projectroot2578my$path= ($filter?"$filter/":'') .$subdir;2579if(check_export_ok("$projectroot/$path")) {2580push@list, { path =>$path};2581$File::Find::prune =1;2582}2583},2584},"$dir");25852586}elsif(-f $projects_list) {2587# read from file(url-encoded):2588# 'git%2Fgit.git Linus+Torvalds'2589# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2590# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2591my%paths;2592open my$fd,'<',$projects_listorreturn;2593 PROJECT:2594while(my$line= <$fd>) {2595chomp$line;2596my($path,$owner) =split' ',$line;2597$path= unescape($path);2598$owner= unescape($owner);2599if(!defined$path) {2600next;2601}2602if($filterne'') {2603# looking for forks;2604my$pfx=substr($path,0,length($filter));2605if($pfxne$filter) {2606next PROJECT;2607}2608my$sfx=substr($path,length($filter));2609if($sfx!~/^\/.*\.git$/) {2610next PROJECT;2611}2612}elsif($check_forks) {2613 PATH:2614foreachmy$filter(keys%paths) {2615# looking for forks;2616my$pfx=substr($path,0,length($filter));2617if($pfxne$filter) {2618next PATH;2619}2620my$sfx=substr($path,length($filter));2621if($sfx!~/^\/.*\.git$/) {2622next PATH;2623}2624# is a fork, don't include it in2625# the list2626next PROJECT;2627}2628}2629if(check_export_ok("$projectroot/$path")) {2630my$pr= {2631 path =>$path,2632 owner => to_utf8($owner),2633};2634push@list,$pr;2635(my$forks_path=$path) =~s/\.git$//;2636$paths{$forks_path}++;2637}2638}2639close$fd;2640}2641return@list;2642}26432644our$gitweb_project_owner=undef;2645sub git_get_project_list_from_file {26462647return if(defined$gitweb_project_owner);26482649$gitweb_project_owner= {};2650# read from file (url-encoded):2651# 'git%2Fgit.git Linus+Torvalds'2652# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2653# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2654if(-f $projects_list) {2655open(my$fd,'<',$projects_list);2656while(my$line= <$fd>) {2657chomp$line;2658my($pr,$ow) =split' ',$line;2659$pr= unescape($pr);2660$ow= unescape($ow);2661$gitweb_project_owner->{$pr} = to_utf8($ow);2662}2663close$fd;2664}2665}26662667sub git_get_project_owner {2668my$project=shift;2669my$owner;26702671returnundefunless$project;2672$git_dir="$projectroot/$project";26732674if(!defined$gitweb_project_owner) {2675 git_get_project_list_from_file();2676}26772678if(exists$gitweb_project_owner->{$project}) {2679$owner=$gitweb_project_owner->{$project};2680}2681if(!defined$owner){2682$owner= git_get_project_config('owner');2683}2684if(!defined$owner) {2685$owner= get_file_owner("$git_dir");2686}26872688return$owner;2689}26902691sub git_get_last_activity {2692my($path) =@_;2693my$fd;26942695$git_dir="$projectroot/$path";2696open($fd,"-|", git_cmd(),'for-each-ref',2697'--format=%(committer)',2698'--sort=-committerdate',2699'--count=1',2700'refs/heads')orreturn;2701my$most_recent= <$fd>;2702close$fdorreturn;2703if(defined$most_recent&&2704$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2705my$timestamp=$1;2706my$age=time-$timestamp;2707return($age, age_string($age));2708}2709return(undef,undef);2710}27112712sub git_get_references {2713my$type=shift||"";2714my%refs;2715# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112716# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2717open my$fd,"-|", git_cmd(),"show-ref","--dereference",2718($type? ("--","refs/$type") : ())# use -- <pattern> if $type2719orreturn;27202721while(my$line= <$fd>) {2722chomp$line;2723if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2724if(defined$refs{$1}) {2725push@{$refs{$1}},$2;2726}else{2727$refs{$1} = [$2];2728}2729}2730}2731close$fdorreturn;2732return \%refs;2733}27342735sub git_get_rev_name_tags {2736my$hash=shift||returnundef;27372738open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2739orreturn;2740my$name_rev= <$fd>;2741close$fd;27422743if($name_rev=~ m|^$hash tags/(.*)$|) {2744return$1;2745}else{2746# catches also '$hash undefined' output2747returnundef;2748}2749}27502751## ----------------------------------------------------------------------2752## parse to hash functions27532754sub parse_date {2755my$epoch=shift;2756my$tz=shift||"-0000";27572758my%date;2759my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2760my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2761my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2762$date{'hour'} =$hour;2763$date{'minute'} =$min;2764$date{'mday'} =$mday;2765$date{'day'} =$days[$wday];2766$date{'month'} =$months[$mon];2767$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2768$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2769$date{'mday-time'} =sprintf"%d%s%02d:%02d",2770$mday,$months[$mon],$hour,$min;2771$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",27721900+$year,1+$mon,$mday,$hour,$min,$sec;27732774$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2775my$local=$epoch+ ((int$1+ ($2/60)) *3600);2776($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2777$date{'hour_local'} =$hour;2778$date{'minute_local'} =$min;2779$date{'tz_local'} =$tz;2780$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",27811900+$year,$mon+1,$mday,2782$hour,$min,$sec,$tz);2783return%date;2784}27852786sub parse_tag {2787my$tag_id=shift;2788my%tag;2789my@comment;27902791open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2792$tag{'id'} =$tag_id;2793while(my$line= <$fd>) {2794chomp$line;2795if($line=~m/^object ([0-9a-fA-F]{40})$/) {2796$tag{'object'} =$1;2797}elsif($line=~m/^type (.+)$/) {2798$tag{'type'} =$1;2799}elsif($line=~m/^tag (.+)$/) {2800$tag{'name'} =$1;2801}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2802$tag{'author'} =$1;2803$tag{'author_epoch'} =$2;2804$tag{'author_tz'} =$3;2805if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2806$tag{'author_name'} =$1;2807$tag{'author_email'} =$2;2808}else{2809$tag{'author_name'} =$tag{'author'};2810}2811}elsif($line=~m/--BEGIN/) {2812push@comment,$line;2813last;2814}elsif($lineeq"") {2815last;2816}2817}2818push@comment, <$fd>;2819$tag{'comment'} = \@comment;2820close$fdorreturn;2821if(!defined$tag{'name'}) {2822return2823};2824return%tag2825}28262827sub parse_commit_text {2828my($commit_text,$withparents) =@_;2829my@commit_lines=split'\n',$commit_text;2830my%co;28312832pop@commit_lines;# Remove '\0'28332834if(!@commit_lines) {2835return;2836}28372838my$header=shift@commit_lines;2839if($header!~m/^[0-9a-fA-F]{40}/) {2840return;2841}2842($co{'id'},my@parents) =split' ',$header;2843while(my$line=shift@commit_lines) {2844last if$lineeq"\n";2845if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2846$co{'tree'} =$1;2847}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2848push@parents,$1;2849}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2850$co{'author'} = to_utf8($1);2851$co{'author_epoch'} =$2;2852$co{'author_tz'} =$3;2853if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2854$co{'author_name'} =$1;2855$co{'author_email'} =$2;2856}else{2857$co{'author_name'} =$co{'author'};2858}2859}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2860$co{'committer'} = to_utf8($1);2861$co{'committer_epoch'} =$2;2862$co{'committer_tz'} =$3;2863if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2864$co{'committer_name'} =$1;2865$co{'committer_email'} =$2;2866}else{2867$co{'committer_name'} =$co{'committer'};2868}2869}2870}2871if(!defined$co{'tree'}) {2872return;2873};2874$co{'parents'} = \@parents;2875$co{'parent'} =$parents[0];28762877foreachmy$title(@commit_lines) {2878$title=~s/^ //;2879if($titlene"") {2880$co{'title'} = chop_str($title,80,5);2881# remove leading stuff of merges to make the interesting part visible2882if(length($title) >50) {2883$title=~s/^Automatic //;2884$title=~s/^merge (of|with) /Merge ... /i;2885if(length($title) >50) {2886$title=~s/(http|rsync):\/\///;2887}2888if(length($title) >50) {2889$title=~s/(master|www|rsync)\.//;2890}2891if(length($title) >50) {2892$title=~s/kernel.org:?//;2893}2894if(length($title) >50) {2895$title=~s/\/pub\/scm//;2896}2897}2898$co{'title_short'} = chop_str($title,50,5);2899last;2900}2901}2902if(!defined$co{'title'} ||$co{'title'}eq"") {2903$co{'title'} =$co{'title_short'} ='(no commit message)';2904}2905# remove added spaces2906foreachmy$line(@commit_lines) {2907$line=~s/^ //;2908}2909$co{'comment'} = \@commit_lines;29102911my$age=time-$co{'committer_epoch'};2912$co{'age'} =$age;2913$co{'age_string'} = age_string($age);2914my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2915if($age>60*60*24*7*2) {2916$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2917$co{'age_string_age'} =$co{'age_string'};2918}else{2919$co{'age_string_date'} =$co{'age_string'};2920$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2921}2922return%co;2923}29242925sub parse_commit {2926my($commit_id) =@_;2927my%co;29282929local$/="\0";29302931open my$fd,"-|", git_cmd(),"rev-list",2932"--parents",2933"--header",2934"--max-count=1",2935$commit_id,2936"--",2937or die_error(500,"Open git-rev-list failed");2938%co= parse_commit_text(<$fd>,1);2939close$fd;29402941return%co;2942}29432944sub parse_commits {2945my($commit_id,$maxcount,$skip,$filename,@args) =@_;2946my@cos;29472948$maxcount||=1;2949$skip||=0;29502951local$/="\0";29522953open my$fd,"-|", git_cmd(),"rev-list",2954"--header",2955@args,2956("--max-count=".$maxcount),2957("--skip=".$skip),2958@extra_options,2959$commit_id,2960"--",2961($filename? ($filename) : ())2962or die_error(500,"Open git-rev-list failed");2963while(my$line= <$fd>) {2964my%co= parse_commit_text($line);2965push@cos, \%co;2966}2967close$fd;29682969returnwantarray?@cos: \@cos;2970}29712972# parse line of git-diff-tree "raw" output2973sub parse_difftree_raw_line {2974my$line=shift;2975my%res;29762977# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2978# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2979if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2980$res{'from_mode'} =$1;2981$res{'to_mode'} =$2;2982$res{'from_id'} =$3;2983$res{'to_id'} =$4;2984$res{'status'} =$5;2985$res{'similarity'} =$6;2986if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2987($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2988}else{2989$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2990}2991}2992# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2993# combined diff (for merge commit)2994elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2995$res{'nparents'} =length($1);2996$res{'from_mode'} = [split(' ',$2) ];2997$res{'to_mode'} =pop@{$res{'from_mode'}};2998$res{'from_id'} = [split(' ',$3) ];2999$res{'to_id'} =pop@{$res{'from_id'}};3000$res{'status'} = [split('',$4) ];3001$res{'to_file'} = unquote($5);3002}3003# 'c512b523472485aef4fff9e57b229d9d243c967f'3004elsif($line=~m/^([0-9a-fA-F]{40})$/) {3005$res{'commit'} =$1;3006}30073008returnwantarray?%res: \%res;3009}30103011# wrapper: return parsed line of git-diff-tree "raw" output3012# (the argument might be raw line, or parsed info)3013sub parsed_difftree_line {3014my$line_or_ref=shift;30153016if(ref($line_or_ref)eq"HASH") {3017# pre-parsed (or generated by hand)3018return$line_or_ref;3019}else{3020return parse_difftree_raw_line($line_or_ref);3021}3022}30233024# parse line of git-ls-tree output3025sub parse_ls_tree_line {3026my$line=shift;3027my%opts=@_;3028my%res;30293030if($opts{'-l'}) {3031#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3032$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;30333034$res{'mode'} =$1;3035$res{'type'} =$2;3036$res{'hash'} =$3;3037$res{'size'} =$4;3038if($opts{'-z'}) {3039$res{'name'} =$5;3040}else{3041$res{'name'} = unquote($5);3042}3043}else{3044#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3045$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;30463047$res{'mode'} =$1;3048$res{'type'} =$2;3049$res{'hash'} =$3;3050if($opts{'-z'}) {3051$res{'name'} =$4;3052}else{3053$res{'name'} = unquote($4);3054}3055}30563057returnwantarray?%res: \%res;3058}30593060# generates _two_ hashes, references to which are passed as 2 and 3 argument3061sub parse_from_to_diffinfo {3062my($diffinfo,$from,$to,@parents) =@_;30633064if($diffinfo->{'nparents'}) {3065# combined diff3066$from->{'file'} = [];3067$from->{'href'} = [];3068 fill_from_file_info($diffinfo,@parents)3069unlessexists$diffinfo->{'from_file'};3070for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3071$from->{'file'}[$i] =3072defined$diffinfo->{'from_file'}[$i] ?3073$diffinfo->{'from_file'}[$i] :3074$diffinfo->{'to_file'};3075if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3076$from->{'href'}[$i] = href(action=>"blob",3077 hash_base=>$parents[$i],3078 hash=>$diffinfo->{'from_id'}[$i],3079 file_name=>$from->{'file'}[$i]);3080}else{3081$from->{'href'}[$i] =undef;3082}3083}3084}else{3085# ordinary (not combined) diff3086$from->{'file'} =$diffinfo->{'from_file'};3087if($diffinfo->{'status'}ne"A") {# not new (added) file3088$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3089 hash=>$diffinfo->{'from_id'},3090 file_name=>$from->{'file'});3091}else{3092delete$from->{'href'};3093}3094}30953096$to->{'file'} =$diffinfo->{'to_file'};3097if(!is_deleted($diffinfo)) {# file exists in result3098$to->{'href'} = href(action=>"blob", hash_base=>$hash,3099 hash=>$diffinfo->{'to_id'},3100 file_name=>$to->{'file'});3101}else{3102delete$to->{'href'};3103}3104}31053106## ......................................................................3107## parse to array of hashes functions31083109sub git_get_heads_list {3110my$limit=shift;3111my@headslist;31123113open my$fd,'-|', git_cmd(),'for-each-ref',3114($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3115'--format=%(objectname) %(refname) %(subject)%00%(committer)',3116'refs/heads'3117orreturn;3118while(my$line= <$fd>) {3119my%ref_item;31203121chomp$line;3122my($refinfo,$committerinfo) =split(/\0/,$line);3123my($hash,$name,$title) =split(' ',$refinfo,3);3124my($committer,$epoch,$tz) =3125($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3126$ref_item{'fullname'} =$name;3127$name=~s!^refs/heads/!!;31283129$ref_item{'name'} =$name;3130$ref_item{'id'} =$hash;3131$ref_item{'title'} =$title||'(no commit message)';3132$ref_item{'epoch'} =$epoch;3133if($epoch) {3134$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3135}else{3136$ref_item{'age'} ="unknown";3137}31383139push@headslist, \%ref_item;3140}3141close$fd;31423143returnwantarray?@headslist: \@headslist;3144}31453146sub git_get_tags_list {3147my$limit=shift;3148my@tagslist;31493150open my$fd,'-|', git_cmd(),'for-each-ref',3151($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3152'--format=%(objectname) %(objecttype) %(refname) '.3153'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3154'refs/tags'3155orreturn;3156while(my$line= <$fd>) {3157my%ref_item;31583159chomp$line;3160my($refinfo,$creatorinfo) =split(/\0/,$line);3161my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3162my($creator,$epoch,$tz) =3163($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3164$ref_item{'fullname'} =$name;3165$name=~s!^refs/tags/!!;31663167$ref_item{'type'} =$type;3168$ref_item{'id'} =$id;3169$ref_item{'name'} =$name;3170if($typeeq"tag") {3171$ref_item{'subject'} =$title;3172$ref_item{'reftype'} =$reftype;3173$ref_item{'refid'} =$refid;3174}else{3175$ref_item{'reftype'} =$type;3176$ref_item{'refid'} =$id;3177}31783179if($typeeq"tag"||$typeeq"commit") {3180$ref_item{'epoch'} =$epoch;3181if($epoch) {3182$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3183}else{3184$ref_item{'age'} ="unknown";3185}3186}31873188push@tagslist, \%ref_item;3189}3190close$fd;31913192returnwantarray?@tagslist: \@tagslist;3193}31943195## ----------------------------------------------------------------------3196## filesystem-related functions31973198sub get_file_owner {3199my$path=shift;32003201my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3202my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3203if(!defined$gcos) {3204returnundef;3205}3206my$owner=$gcos;3207$owner=~s/[,;].*$//;3208return to_utf8($owner);3209}32103211# assume that file exists3212sub insert_file {3213my$filename=shift;32143215open my$fd,'<',$filename;3216print map{ to_utf8($_) } <$fd>;3217close$fd;3218}32193220## ......................................................................3221## mimetype related functions32223223sub mimetype_guess_file {3224my$filename=shift;3225my$mimemap=shift;3226-r $mimemaporreturnundef;32273228my%mimemap;3229open(my$mh,'<',$mimemap)orreturnundef;3230while(<$mh>) {3231next ifm/^#/;# skip comments3232my($mimetype,$exts) =split(/\t+/);3233if(defined$exts) {3234my@exts=split(/\s+/,$exts);3235foreachmy$ext(@exts) {3236$mimemap{$ext} =$mimetype;3237}3238}3239}3240close($mh);32413242$filename=~/\.([^.]*)$/;3243return$mimemap{$1};3244}32453246sub mimetype_guess {3247my$filename=shift;3248my$mime;3249$filename=~/\./orreturnundef;32503251if($mimetypes_file) {3252my$file=$mimetypes_file;3253if($file!~m!^/!) {# if it is relative path3254# it is relative to project3255$file="$projectroot/$project/$file";3256}3257$mime= mimetype_guess_file($filename,$file);3258}3259$mime||= mimetype_guess_file($filename,'/etc/mime.types');3260return$mime;3261}32623263sub blob_mimetype {3264my$fd=shift;3265my$filename=shift;32663267if($filename) {3268my$mime= mimetype_guess($filename);3269$mimeandreturn$mime;3270}32713272# just in case3273return$default_blob_plain_mimetypeunless$fd;32743275if(-T $fd) {3276return'text/plain';3277}elsif(!$filename) {3278return'application/octet-stream';3279}elsif($filename=~m/\.png$/i) {3280return'image/png';3281}elsif($filename=~m/\.gif$/i) {3282return'image/gif';3283}elsif($filename=~m/\.jpe?g$/i) {3284return'image/jpeg';3285}else{3286return'application/octet-stream';3287}3288}32893290sub blob_contenttype {3291my($fd,$file_name,$type) =@_;32923293$type||= blob_mimetype($fd,$file_name);3294if($typeeq'text/plain'&&defined$default_text_plain_charset) {3295$type.="; charset=$default_text_plain_charset";3296}32973298return$type;3299}33003301# guess file syntax for syntax highlighting; return undef if no highlighting3302# the name of syntax can (in the future) depend on syntax highlighter used3303sub guess_file_syntax {3304my($highlight,$mimetype,$file_name) =@_;3305returnundefunless($highlight&&defined$file_name);33063307# configuration for 'highlight' (http://www.andre-simon.de/)3308# match by basename3309my%highlight_basename= (3310#'Program' => 'py',3311#'Library' => 'py',3312'SConstruct'=>'py',# SCons equivalent of Makefile3313'Makefile'=>'make',3314);3315# match by extension3316my%highlight_ext= (3317# main extensions, defining name of syntax;3318# see files in /usr/share/highlight/langDefs/ directory3319map{$_=>$_}3320qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),3321# alternate extensions, see /etc/highlight/filetypes.conf3322'h'=>'c',3323map{$_=>'cpp'}qw(cxx c++ cc),3324map{$_=>'php'}qw(php3 php4),3325map{$_=>'pl'}qw(perl pm),# perhaps also 'cgi'3326'mak'=>'make',3327map{$_=>'xml'}qw(xhtml html htm),3328);33293330my$basename= basename($file_name,'.in');3331return$highlight_basename{$basename}3332ifexists$highlight_basename{$basename};33333334$basename=~/\.([^.]*)$/;3335my$ext=$1orreturnundef;3336return$highlight_ext{$ext}3337ifexists$highlight_ext{$ext};33383339returnundef;3340}33413342# run highlighter and return FD of its output,3343# or return original FD if no highlighting3344sub run_highlighter {3345my($fd,$highlight,$syntax) =@_;3346return$fdunless($highlight&&defined$syntax);33473348close$fd3349or die_error(404,"Reading blob failed");3350open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3351"highlight --xhtml --fragment --syntax$syntax|"3352or die_error(500,"Couldn't open file or run syntax highlighter");3353return$fd;3354}33553356## ======================================================================3357## functions printing HTML: header, footer, error page33583359sub get_page_title {3360my$title= to_utf8($site_name);33613362return$titleunless(defined$project);3363$title.=" - ". to_utf8($project);33643365return$titleunless(defined$action);3366$title.="/$action";# $action is US-ASCII (7bit ASCII)33673368return$titleunless(defined$file_name);3369$title.=" - ". esc_path($file_name);3370if($actioneq"tree"&&$file_name!~ m|/$|) {3371$title.="/";3372}33733374return$title;3375}33763377sub git_header_html {3378my$status=shift||"200 OK";3379my$expires=shift;3380my%opts=@_;33813382my$title= get_page_title();3383my$content_type;3384# require explicit support from the UA if we are to send the page as3385# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3386# we have to do this because MSIE sometimes globs '*/*', pretending to3387# support xhtml+xml but choking when it gets what it asked for.3388if(defined$cgi->http('HTTP_ACCEPT') &&3389$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3390$cgi->Accept('application/xhtml+xml') !=0) {3391$content_type='application/xhtml+xml';3392}else{3393$content_type='text/html';3394}3395print$cgi->header(-type=>$content_type, -charset =>'utf-8',3396-status=>$status, -expires =>$expires)3397unless($opts{'-no_http_header'});3398my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3399print<<EOF;3400<?xml version="1.0" encoding="utf-8"?>3401<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3402<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3403<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3404<!-- git core binaries version$git_version-->3405<head>3406<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3407<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3408<meta name="robots" content="index, nofollow"/>3409<title>$title</title>3410EOF3411# the stylesheet, favicon etc urls won't work correctly with path_info3412# unless we set the appropriate base URL3413if($ENV{'PATH_INFO'}) {3414print"<base href=\"".esc_url($base_url)."\"/>\n";3415}3416# print out each stylesheet that exist, providing backwards capability3417# for those people who defined $stylesheet in a config file3418if(defined$stylesheet) {3419print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3420}else{3421foreachmy$stylesheet(@stylesheets) {3422next unless$stylesheet;3423print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3424}3425}3426if(defined$project) {3427my%href_params= get_feed_info();3428if(!exists$href_params{'-title'}) {3429$href_params{'-title'} ='log';3430}34313432foreachmy$formatqw(RSS Atom){3433my$type=lc($format);3434my%link_attr= (3435'-rel'=>'alternate',3436'-title'=>"$project-$href_params{'-title'} -$formatfeed",3437'-type'=>"application/$type+xml"3438);34393440$href_params{'action'} =$type;3441$link_attr{'-href'} = href(%href_params);3442print"<link ".3443"rel=\"$link_attr{'-rel'}\"".3444"title=\"$link_attr{'-title'}\"".3445"href=\"$link_attr{'-href'}\"".3446"type=\"$link_attr{'-type'}\"".3447"/>\n";34483449$href_params{'extra_options'} ='--no-merges';3450$link_attr{'-href'} = href(%href_params);3451$link_attr{'-title'} .=' (no merges)';3452print"<link ".3453"rel=\"$link_attr{'-rel'}\"".3454"title=\"$link_attr{'-title'}\"".3455"href=\"$link_attr{'-href'}\"".3456"type=\"$link_attr{'-type'}\"".3457"/>\n";3458}34593460}else{3461printf('<link rel="alternate" title="%sprojects list" '.3462'href="%s" type="text/plain; charset=utf-8" />'."\n",3463$site_name, href(project=>undef, action=>"project_index"));3464printf('<link rel="alternate" title="%sprojects feeds" '.3465'href="%s" type="text/x-opml" />'."\n",3466$site_name, href(project=>undef, action=>"opml"));3467}3468if(defined$favicon) {3469printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3470}34713472print"</head>\n".3473"<body>\n";34743475if(defined$site_header&& -f $site_header) {3476 insert_file($site_header);3477}34783479print"<div class=\"page_header\">\n".3480$cgi->a({-href => esc_url($logo_url),3481-title =>$logo_label},3482qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3483print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3484if(defined$project) {3485print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3486if(defined$action) {3487print" /$action";3488}3489print"\n";3490}3491print"</div>\n";34923493my$have_search= gitweb_check_feature('search');3494if(defined$project&&$have_search) {3495if(!defined$searchtext) {3496$searchtext="";3497}3498my$search_hash;3499if(defined$hash_base) {3500$search_hash=$hash_base;3501}elsif(defined$hash) {3502$search_hash=$hash;3503}else{3504$search_hash="HEAD";3505}3506my$action=$my_uri;3507my$use_pathinfo= gitweb_check_feature('pathinfo');3508if($use_pathinfo) {3509$action.="/".esc_url($project);3510}3511print$cgi->startform(-method=>"get", -action =>$action) .3512"<div class=\"search\">\n".3513(!$use_pathinfo&&3514$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3515$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3516$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3517$cgi->popup_menu(-name =>'st', -default=>'commit',3518-values=> ['commit','grep','author','committer','pickaxe']) .3519$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3520" search:\n",3521$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3522"<span title=\"Extended regular expression\">".3523$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3524-checked =>$search_use_regexp) .3525"</span>".3526"</div>".3527$cgi->end_form() ."\n";3528}3529}35303531sub git_footer_html {3532my$feed_class='rss_logo';35333534print"<div class=\"page_footer\">\n";3535if(defined$project) {3536my$descr= git_get_project_description($project);3537if(defined$descr) {3538print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3539}35403541my%href_params= get_feed_info();3542if(!%href_params) {3543$feed_class.=' generic';3544}3545$href_params{'-title'} ||='log';35463547foreachmy$formatqw(RSS Atom){3548$href_params{'action'} =lc($format);3549print$cgi->a({-href => href(%href_params),3550-title =>"$href_params{'-title'}$formatfeed",3551-class=>$feed_class},$format)."\n";3552}35533554}else{3555print$cgi->a({-href => href(project=>undef, action=>"opml"),3556-class=>$feed_class},"OPML") ." ";3557print$cgi->a({-href => href(project=>undef, action=>"project_index"),3558-class=>$feed_class},"TXT") ."\n";3559}3560print"</div>\n";# class="page_footer"35613562if(defined$t0&& gitweb_check_feature('timed')) {3563print"<div id=\"generating_info\">\n";3564print'This page took '.3565'<span id="generating_time" class="time_span">'.3566 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3567' seconds </span>'.3568' and '.3569'<span id="generating_cmd">'.3570$number_of_git_cmds.3571'</span> git commands '.3572" to generate.\n";3573print"</div>\n";# class="page_footer"3574}35753576if(defined$site_footer&& -f $site_footer) {3577 insert_file($site_footer);3578}35793580print qq!<script type="text/javascript" src="$javascript"></script>\n!;3581if(defined$action&&3582$actioneq'blame_incremental') {3583print qq!<script type="text/javascript">\n!.3584 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3585 qq!"!. href() .qq!");\n!.3586 qq!</script>\n!;3587}elsif(gitweb_check_feature('javascript-actions')) {3588print qq!<script type="text/javascript">\n!.3589 qq!window.onload = fixLinks;\n!.3590 qq!</script>\n!;3591}35923593print"</body>\n".3594"</html>";3595}35963597# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3598# Example: die_error(404, 'Hash not found')3599# By convention, use the following status codes (as defined in RFC 2616):3600# 400: Invalid or missing CGI parameters, or3601# requested object exists but has wrong type.3602# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3603# this server or project.3604# 404: Requested object/revision/project doesn't exist.3605# 500: The server isn't configured properly, or3606# an internal error occurred (e.g. failed assertions caused by bugs), or3607# an unknown error occurred (e.g. the git binary died unexpectedly).3608# 503: The server is currently unavailable (because it is overloaded,3609# or down for maintenance). Generally, this is a temporary state.3610sub die_error {3611my$status=shift||500;3612my$error= esc_html(shift) ||"Internal Server Error";3613my$extra=shift;3614my%opts=@_;36153616my%http_responses= (3617400=>'400 Bad Request',3618403=>'403 Forbidden',3619404=>'404 Not Found',3620500=>'500 Internal Server Error',3621503=>'503 Service Unavailable',3622);3623 git_header_html($http_responses{$status},undef,%opts);3624print<<EOF;3625<div class="page_body">3626<br /><br />3627$status-$error3628<br />3629EOF3630if(defined$extra) {3631print"<hr />\n".3632"$extra\n";3633}3634print"</div>\n";36353636 git_footer_html();3637goto DONE_GITWEB3638unless($opts{'-error_handler'});3639}36403641## ----------------------------------------------------------------------3642## functions printing or outputting HTML: navigation36433644sub git_print_page_nav {3645my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3646$extra=''if!defined$extra;# pager or formats36473648my@navs=qw(summary shortlog log commit commitdiff tree);3649if($suppress) {3650@navs=grep{$_ne$suppress}@navs;3651}36523653my%arg=map{$_=> {action=>$_} }@navs;3654if(defined$head) {3655for(qw(commit commitdiff)) {3656$arg{$_}{'hash'} =$head;3657}3658if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3659for(qw(shortlog log)) {3660$arg{$_}{'hash'} =$head;3661}3662}3663}36643665$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3666$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;36673668my@actions= gitweb_get_feature('actions');3669my%repl= (3670'%'=>'%',3671'n'=>$project,# project name3672'f'=>$git_dir,# project path within filesystem3673'h'=>$treehead||'',# current hash ('h' parameter)3674'b'=>$treebase||'',# hash base ('hb' parameter)3675);3676while(@actions) {3677my($label,$link,$pos) =splice(@actions,0,3);3678# insert3679@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3680# munch munch3681$link=~s/%([%nfhb])/$repl{$1}/g;3682$arg{$label}{'_href'} =$link;3683}36843685print"<div class=\"page_nav\">\n".3686(join" | ",3687map{$_eq$current?3688$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3689}@navs);3690print"<br/>\n$extra<br/>\n".3691"</div>\n";3692}36933694sub format_paging_nav {3695my($action,$page,$has_next_link) =@_;3696my$paging_nav;369736983699if($page>0) {3700$paging_nav.=3701$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3702" ⋅ ".3703$cgi->a({-href => href(-replay=>1, page=>$page-1),3704-accesskey =>"p", -title =>"Alt-p"},"prev");3705}else{3706$paging_nav.="first ⋅ prev";3707}37083709if($has_next_link) {3710$paging_nav.=" ⋅ ".3711$cgi->a({-href => href(-replay=>1, page=>$page+1),3712-accesskey =>"n", -title =>"Alt-n"},"next");3713}else{3714$paging_nav.=" ⋅ next";3715}37163717return$paging_nav;3718}37193720## ......................................................................3721## functions printing or outputting HTML: div37223723sub git_print_header_div {3724my($action,$title,$hash,$hash_base) =@_;3725my%args= ();37263727$args{'action'} =$action;3728$args{'hash'} =$hashif$hash;3729$args{'hash_base'} =$hash_baseif$hash_base;37303731print"<div class=\"header\">\n".3732$cgi->a({-href => href(%args), -class=>"title"},3733$title?$title:$action) .3734"\n</div>\n";3735}37363737sub print_local_time {3738print format_local_time(@_);3739}37403741sub format_local_time {3742my$localtime='';3743my%date=@_;3744if($date{'hour_local'} <6) {3745$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3746$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3747}else{3748$localtime.=sprintf(" (%02d:%02d%s)",3749$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3750}37513752return$localtime;3753}37543755# Outputs the author name and date in long form3756sub git_print_authorship {3757my$co=shift;3758my%opts=@_;3759my$tag=$opts{-tag} ||'div';3760my$author=$co->{'author_name'};37613762my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3763print"<$tagclass=\"author_date\">".3764 format_search_author($author,"author", esc_html($author)) .3765" [$ad{'rfc2822'}";3766 print_local_time(%ad)if($opts{-localtime});3767print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3768."</$tag>\n";3769}37703771# Outputs table rows containing the full author or committer information,3772# in the format expected for 'commit' view (& similia).3773# Parameters are a commit hash reference, followed by the list of people3774# to output information for. If the list is empty it defalts to both3775# author and committer.3776sub git_print_authorship_rows {3777my$co=shift;3778# too bad we can't use @people = @_ || ('author', 'committer')3779my@people=@_;3780@people= ('author','committer')unless@people;3781foreachmy$who(@people) {3782my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3783print"<tr><td>$who</td><td>".3784 format_search_author($co->{"${who}_name"},$who,3785 esc_html($co->{"${who}_name"})) ." ".3786 format_search_author($co->{"${who}_email"},$who,3787 esc_html("<".$co->{"${who}_email"} .">")) .3788"</td><td rowspan=\"2\">".3789 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3790"</td></tr>\n".3791"<tr>".3792"<td></td><td>$wd{'rfc2822'}";3793 print_local_time(%wd);3794print"</td>".3795"</tr>\n";3796}3797}37983799sub git_print_page_path {3800my$name=shift;3801my$type=shift;3802my$hb=shift;380338043805print"<div class=\"page_path\">";3806print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3807-title =>'tree root'}, to_utf8("[$project]"));3808print" / ";3809if(defined$name) {3810my@dirname=split'/',$name;3811my$basename=pop@dirname;3812my$fullname='';38133814foreachmy$dir(@dirname) {3815$fullname.= ($fullname?'/':'') .$dir;3816print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3817 hash_base=>$hb),3818-title =>$fullname}, esc_path($dir));3819print" / ";3820}3821if(defined$type&&$typeeq'blob') {3822print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3823 hash_base=>$hb),3824-title =>$name}, esc_path($basename));3825}elsif(defined$type&&$typeeq'tree') {3826print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3827 hash_base=>$hb),3828-title =>$name}, esc_path($basename));3829print" / ";3830}else{3831print esc_path($basename);3832}3833}3834print"<br/></div>\n";3835}38363837sub git_print_log {3838my$log=shift;3839my%opts=@_;38403841if($opts{'-remove_title'}) {3842# remove title, i.e. first line of log3843shift@$log;3844}3845# remove leading empty lines3846while(defined$log->[0] &&$log->[0]eq"") {3847shift@$log;3848}38493850# print log3851my$signoff=0;3852my$empty=0;3853foreachmy$line(@$log) {3854if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3855$signoff=1;3856$empty=0;3857if(!$opts{'-remove_signoff'}) {3858print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3859next;3860}else{3861# remove signoff lines3862next;3863}3864}else{3865$signoff=0;3866}38673868# print only one empty line3869# do not print empty line after signoff3870if($lineeq"") {3871next if($empty||$signoff);3872$empty=1;3873}else{3874$empty=0;3875}38763877print format_log_line_html($line) ."<br/>\n";3878}38793880if($opts{'-final_empty_line'}) {3881# end with single empty line3882print"<br/>\n"unless$empty;3883}3884}38853886# return link target (what link points to)3887sub git_get_link_target {3888my$hash=shift;3889my$link_target;38903891# read link3892open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3893orreturn;3894{3895local$/=undef;3896$link_target= <$fd>;3897}3898close$fd3899orreturn;39003901return$link_target;3902}39033904# given link target, and the directory (basedir) the link is in,3905# return target of link relative to top directory (top tree);3906# return undef if it is not possible (including absolute links).3907sub normalize_link_target {3908my($link_target,$basedir) =@_;39093910# absolute symlinks (beginning with '/') cannot be normalized3911return if(substr($link_target,0,1)eq'/');39123913# normalize link target to path from top (root) tree (dir)3914my$path;3915if($basedir) {3916$path=$basedir.'/'.$link_target;3917}else{3918# we are in top (root) tree (dir)3919$path=$link_target;3920}39213922# remove //, /./, and /../3923my@path_parts;3924foreachmy$part(split('/',$path)) {3925# discard '.' and ''3926next if(!$part||$parteq'.');3927# handle '..'3928if($parteq'..') {3929if(@path_parts) {3930pop@path_parts;3931}else{3932# link leads outside repository (outside top dir)3933return;3934}3935}else{3936push@path_parts,$part;3937}3938}3939$path=join('/',@path_parts);39403941return$path;3942}39433944# print tree entry (row of git_tree), but without encompassing <tr> element3945sub git_print_tree_entry {3946my($t,$basedir,$hash_base,$have_blame) =@_;39473948my%base_key= ();3949$base_key{'hash_base'} =$hash_baseifdefined$hash_base;39503951# The format of a table row is: mode list link. Where mode is3952# the mode of the entry, list is the name of the entry, an href,3953# and link is the action links of the entry.39543955print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3956if(exists$t->{'size'}) {3957print"<td class=\"size\">$t->{'size'}</td>\n";3958}3959if($t->{'type'}eq"blob") {3960print"<td class=\"list\">".3961$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3962 file_name=>"$basedir$t->{'name'}",%base_key),3963-class=>"list"}, esc_path($t->{'name'}));3964if(S_ISLNK(oct$t->{'mode'})) {3965my$link_target= git_get_link_target($t->{'hash'});3966if($link_target) {3967my$norm_target= normalize_link_target($link_target,$basedir);3968if(defined$norm_target) {3969print" -> ".3970$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3971 file_name=>$norm_target),3972-title =>$norm_target}, esc_path($link_target));3973}else{3974print" -> ". esc_path($link_target);3975}3976}3977}3978print"</td>\n";3979print"<td class=\"link\">";3980print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3981 file_name=>"$basedir$t->{'name'}",%base_key)},3982"blob");3983if($have_blame) {3984print" | ".3985$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3986 file_name=>"$basedir$t->{'name'}",%base_key)},3987"blame");3988}3989if(defined$hash_base) {3990print" | ".3991$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3992 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3993"history");3994}3995print" | ".3996$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3997 file_name=>"$basedir$t->{'name'}")},3998"raw");3999print"</td>\n";40004001}elsif($t->{'type'}eq"tree") {4002print"<td class=\"list\">";4003print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4004 file_name=>"$basedir$t->{'name'}",4005%base_key)},4006 esc_path($t->{'name'}));4007print"</td>\n";4008print"<td class=\"link\">";4009print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4010 file_name=>"$basedir$t->{'name'}",4011%base_key)},4012"tree");4013if(defined$hash_base) {4014print" | ".4015$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4016 file_name=>"$basedir$t->{'name'}")},4017"history");4018}4019print"</td>\n";4020}else{4021# unknown object: we can only present history for it4022# (this includes 'commit' object, i.e. submodule support)4023print"<td class=\"list\">".4024 esc_path($t->{'name'}) .4025"</td>\n";4026print"<td class=\"link\">";4027if(defined$hash_base) {4028print$cgi->a({-href => href(action=>"history",4029 hash_base=>$hash_base,4030 file_name=>"$basedir$t->{'name'}")},4031"history");4032}4033print"</td>\n";4034}4035}40364037## ......................................................................4038## functions printing large fragments of HTML40394040# get pre-image filenames for merge (combined) diff4041sub fill_from_file_info {4042my($diff,@parents) =@_;40434044$diff->{'from_file'} = [ ];4045$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4046for(my$i=0;$i<$diff->{'nparents'};$i++) {4047if($diff->{'status'}[$i]eq'R'||4048$diff->{'status'}[$i]eq'C') {4049$diff->{'from_file'}[$i] =4050 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4051}4052}40534054return$diff;4055}40564057# is current raw difftree line of file deletion4058sub is_deleted {4059my$diffinfo=shift;40604061return$diffinfo->{'to_id'}eq('0' x 40);4062}40634064# does patch correspond to [previous] difftree raw line4065# $diffinfo - hashref of parsed raw diff format4066# $patchinfo - hashref of parsed patch diff format4067# (the same keys as in $diffinfo)4068sub is_patch_split {4069my($diffinfo,$patchinfo) =@_;40704071returndefined$diffinfo&&defined$patchinfo4072&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4073}407440754076sub git_difftree_body {4077my($difftree,$hash,@parents) =@_;4078my($parent) =$parents[0];4079my$have_blame= gitweb_check_feature('blame');4080print"<div class=\"list_head\">\n";4081if($#{$difftree} >10) {4082print(($#{$difftree} +1) ." files changed:\n");4083}4084print"</div>\n";40854086print"<table class=\"".4087(@parents>1?"combined ":"") .4088"diff_tree\">\n";40894090# header only for combined diff in 'commitdiff' view4091my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4092if($has_header) {4093# table header4094print"<thead><tr>\n".4095"<th></th><th></th>\n";# filename, patchN link4096for(my$i=0;$i<@parents;$i++) {4097my$par=$parents[$i];4098print"<th>".4099$cgi->a({-href => href(action=>"commitdiff",4100 hash=>$hash, hash_parent=>$par),4101-title =>'commitdiff to parent number '.4102($i+1) .': '.substr($par,0,7)},4103$i+1) .4104" </th>\n";4105}4106print"</tr></thead>\n<tbody>\n";4107}41084109my$alternate=1;4110my$patchno=0;4111foreachmy$line(@{$difftree}) {4112my$diff= parsed_difftree_line($line);41134114if($alternate) {4115print"<tr class=\"dark\">\n";4116}else{4117print"<tr class=\"light\">\n";4118}4119$alternate^=1;41204121if(exists$diff->{'nparents'}) {# combined diff41224123 fill_from_file_info($diff,@parents)4124unlessexists$diff->{'from_file'};41254126if(!is_deleted($diff)) {4127# file exists in the result (child) commit4128print"<td>".4129$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4130 file_name=>$diff->{'to_file'},4131 hash_base=>$hash),4132-class=>"list"}, esc_path($diff->{'to_file'})) .4133"</td>\n";4134}else{4135print"<td>".4136 esc_path($diff->{'to_file'}) .4137"</td>\n";4138}41394140if($actioneq'commitdiff') {4141# link to patch4142$patchno++;4143print"<td class=\"link\">".4144$cgi->a({-href =>"#patch$patchno"},"patch") .4145" | ".4146"</td>\n";4147}41484149my$has_history=0;4150my$not_deleted=0;4151for(my$i=0;$i<$diff->{'nparents'};$i++) {4152my$hash_parent=$parents[$i];4153my$from_hash=$diff->{'from_id'}[$i];4154my$from_path=$diff->{'from_file'}[$i];4155my$status=$diff->{'status'}[$i];41564157$has_history||= ($statusne'A');4158$not_deleted||= ($statusne'D');41594160if($statuseq'A') {4161print"<td class=\"link\"align=\"right\"> | </td>\n";4162}elsif($statuseq'D') {4163print"<td class=\"link\">".4164$cgi->a({-href => href(action=>"blob",4165 hash_base=>$hash,4166 hash=>$from_hash,4167 file_name=>$from_path)},4168"blob". ($i+1)) .4169" | </td>\n";4170}else{4171if($diff->{'to_id'}eq$from_hash) {4172print"<td class=\"link nochange\">";4173}else{4174print"<td class=\"link\">";4175}4176print$cgi->a({-href => href(action=>"blobdiff",4177 hash=>$diff->{'to_id'},4178 hash_parent=>$from_hash,4179 hash_base=>$hash,4180 hash_parent_base=>$hash_parent,4181 file_name=>$diff->{'to_file'},4182 file_parent=>$from_path)},4183"diff". ($i+1)) .4184" | </td>\n";4185}4186}41874188print"<td class=\"link\">";4189if($not_deleted) {4190print$cgi->a({-href => href(action=>"blob",4191 hash=>$diff->{'to_id'},4192 file_name=>$diff->{'to_file'},4193 hash_base=>$hash)},4194"blob");4195print" | "if($has_history);4196}4197if($has_history) {4198print$cgi->a({-href => href(action=>"history",4199 file_name=>$diff->{'to_file'},4200 hash_base=>$hash)},4201"history");4202}4203print"</td>\n";42044205print"</tr>\n";4206next;# instead of 'else' clause, to avoid extra indent4207}4208# else ordinary diff42094210my($to_mode_oct,$to_mode_str,$to_file_type);4211my($from_mode_oct,$from_mode_str,$from_file_type);4212if($diff->{'to_mode'}ne('0' x 6)) {4213$to_mode_oct=oct$diff->{'to_mode'};4214if(S_ISREG($to_mode_oct)) {# only for regular file4215$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4216}4217$to_file_type= file_type($diff->{'to_mode'});4218}4219if($diff->{'from_mode'}ne('0' x 6)) {4220$from_mode_oct=oct$diff->{'from_mode'};4221if(S_ISREG($to_mode_oct)) {# only for regular file4222$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4223}4224$from_file_type= file_type($diff->{'from_mode'});4225}42264227if($diff->{'status'}eq"A") {# created4228my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4229$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4230$mode_chng.="]</span>";4231print"<td>";4232print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4233 hash_base=>$hash, file_name=>$diff->{'file'}),4234-class=>"list"}, esc_path($diff->{'file'}));4235print"</td>\n";4236print"<td>$mode_chng</td>\n";4237print"<td class=\"link\">";4238if($actioneq'commitdiff') {4239# link to patch4240$patchno++;4241print$cgi->a({-href =>"#patch$patchno"},"patch");4242print" | ";4243}4244print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4245 hash_base=>$hash, file_name=>$diff->{'file'})},4246"blob");4247print"</td>\n";42484249}elsif($diff->{'status'}eq"D") {# deleted4250my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4251print"<td>";4252print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4253 hash_base=>$parent, file_name=>$diff->{'file'}),4254-class=>"list"}, esc_path($diff->{'file'}));4255print"</td>\n";4256print"<td>$mode_chng</td>\n";4257print"<td class=\"link\">";4258if($actioneq'commitdiff') {4259# link to patch4260$patchno++;4261print$cgi->a({-href =>"#patch$patchno"},"patch");4262print" | ";4263}4264print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4265 hash_base=>$parent, file_name=>$diff->{'file'})},4266"blob") ." | ";4267if($have_blame) {4268print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4269 file_name=>$diff->{'file'})},4270"blame") ." | ";4271}4272print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4273 file_name=>$diff->{'file'})},4274"history");4275print"</td>\n";42764277}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4278my$mode_chnge="";4279if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4280$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4281if($from_file_typene$to_file_type) {4282$mode_chnge.=" from$from_file_typeto$to_file_type";4283}4284if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4285if($from_mode_str&&$to_mode_str) {4286$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4287}elsif($to_mode_str) {4288$mode_chnge.=" mode:$to_mode_str";4289}4290}4291$mode_chnge.="]</span>\n";4292}4293print"<td>";4294print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4295 hash_base=>$hash, file_name=>$diff->{'file'}),4296-class=>"list"}, esc_path($diff->{'file'}));4297print"</td>\n";4298print"<td>$mode_chnge</td>\n";4299print"<td class=\"link\">";4300if($actioneq'commitdiff') {4301# link to patch4302$patchno++;4303print$cgi->a({-href =>"#patch$patchno"},"patch") .4304" | ";4305}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4306# "commit" view and modified file (not onlu mode changed)4307print$cgi->a({-href => href(action=>"blobdiff",4308 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4309 hash_base=>$hash, hash_parent_base=>$parent,4310 file_name=>$diff->{'file'})},4311"diff") .4312" | ";4313}4314print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4315 hash_base=>$hash, file_name=>$diff->{'file'})},4316"blob") ." | ";4317if($have_blame) {4318print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4319 file_name=>$diff->{'file'})},4320"blame") ." | ";4321}4322print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4323 file_name=>$diff->{'file'})},4324"history");4325print"</td>\n";43264327}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4328my%status_name= ('R'=>'moved','C'=>'copied');4329my$nstatus=$status_name{$diff->{'status'}};4330my$mode_chng="";4331if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4332# mode also for directories, so we cannot use $to_mode_str4333$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4334}4335print"<td>".4336$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4337 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4338-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4339"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4340$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4341 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4342-class=>"list"}, esc_path($diff->{'from_file'})) .4343" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4344"<td class=\"link\">";4345if($actioneq'commitdiff') {4346# link to patch4347$patchno++;4348print$cgi->a({-href =>"#patch$patchno"},"patch") .4349" | ";4350}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4351# "commit" view and modified file (not only pure rename or copy)4352print$cgi->a({-href => href(action=>"blobdiff",4353 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4354 hash_base=>$hash, hash_parent_base=>$parent,4355 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4356"diff") .4357" | ";4358}4359print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4360 hash_base=>$parent, file_name=>$diff->{'to_file'})},4361"blob") ." | ";4362if($have_blame) {4363print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4364 file_name=>$diff->{'to_file'})},4365"blame") ." | ";4366}4367print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4368 file_name=>$diff->{'to_file'})},4369"history");4370print"</td>\n";43714372}# we should not encounter Unmerged (U) or Unknown (X) status4373print"</tr>\n";4374}4375print"</tbody>"if$has_header;4376print"</table>\n";4377}43784379sub git_patchset_body {4380my($fd,$difftree,$hash,@hash_parents) =@_;4381my($hash_parent) =$hash_parents[0];43824383my$is_combined= (@hash_parents>1);4384my$patch_idx=0;4385my$patch_number=0;4386my$patch_line;4387my$diffinfo;4388my$to_name;4389my(%from,%to);43904391print"<div class=\"patchset\">\n";43924393# skip to first patch4394while($patch_line= <$fd>) {4395chomp$patch_line;43964397last if($patch_line=~m/^diff /);4398}43994400 PATCH:4401while($patch_line) {44024403# parse "git diff" header line4404if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4405# $1 is from_name, which we do not use4406$to_name= unquote($2);4407$to_name=~s!^b/!!;4408}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4409# $1 is 'cc' or 'combined', which we do not use4410$to_name= unquote($2);4411}else{4412$to_name=undef;4413}44144415# check if current patch belong to current raw line4416# and parse raw git-diff line if needed4417if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4418# this is continuation of a split patch4419print"<div class=\"patch cont\">\n";4420}else{4421# advance raw git-diff output if needed4422$patch_idx++ifdefined$diffinfo;44234424# read and prepare patch information4425$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);44264427# compact combined diff output can have some patches skipped4428# find which patch (using pathname of result) we are at now;4429if($is_combined) {4430while($to_namene$diffinfo->{'to_file'}) {4431print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4432 format_diff_cc_simplified($diffinfo,@hash_parents) .4433"</div>\n";# class="patch"44344435$patch_idx++;4436$patch_number++;44374438last if$patch_idx>$#$difftree;4439$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4440}4441}44424443# modifies %from, %to hashes4444 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);44454446# this is first patch for raw difftree line with $patch_idx index4447# we index @$difftree array from 0, but number patches from 14448print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4449}44504451# git diff header4452#assert($patch_line =~ m/^diff /) if DEBUG;4453#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4454$patch_number++;4455# print "git diff" header4456print format_git_diff_header_line($patch_line,$diffinfo,4457 \%from, \%to);44584459# print extended diff header4460print"<div class=\"diff extended_header\">\n";4461 EXTENDED_HEADER:4462while($patch_line= <$fd>) {4463chomp$patch_line;44644465last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);44664467print format_extended_diff_header_line($patch_line,$diffinfo,4468 \%from, \%to);4469}4470print"</div>\n";# class="diff extended_header"44714472# from-file/to-file diff header4473if(!$patch_line) {4474print"</div>\n";# class="patch"4475last PATCH;4476}4477next PATCH if($patch_line=~m/^diff /);4478#assert($patch_line =~ m/^---/) if DEBUG;44794480my$last_patch_line=$patch_line;4481$patch_line= <$fd>;4482chomp$patch_line;4483#assert($patch_line =~ m/^\+\+\+/) if DEBUG;44844485print format_diff_from_to_header($last_patch_line,$patch_line,4486$diffinfo, \%from, \%to,4487@hash_parents);44884489# the patch itself4490 LINE:4491while($patch_line= <$fd>) {4492chomp$patch_line;44934494next PATCH if($patch_line=~m/^diff /);44954496print format_diff_line($patch_line, \%from, \%to);4497}44984499}continue{4500print"</div>\n";# class="patch"4501}45024503# for compact combined (--cc) format, with chunk and patch simpliciaction4504# patchset might be empty, but there might be unprocessed raw lines4505for(++$patch_idxif$patch_number>0;4506$patch_idx<@$difftree;4507++$patch_idx) {4508# read and prepare patch information4509$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);45104511# generate anchor for "patch" links in difftree / whatchanged part4512print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4513 format_diff_cc_simplified($diffinfo,@hash_parents) .4514"</div>\n";# class="patch"45154516$patch_number++;4517}45184519if($patch_number==0) {4520if(@hash_parents>1) {4521print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4522}else{4523print"<div class=\"diff nodifferences\">No differences found</div>\n";4524}4525}45264527print"</div>\n";# class="patchset"4528}45294530# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45314532# fills project list info (age, description, owner, forks) for each4533# project in the list, removing invalid projects from returned list4534# NOTE: modifies $projlist, but does not remove entries from it4535sub fill_project_list_info {4536my($projlist,$check_forks) =@_;4537my@projects;45384539my$show_ctags= gitweb_check_feature('ctags');4540 PROJECT:4541foreachmy$pr(@$projlist) {4542my(@activity) = git_get_last_activity($pr->{'path'});4543unless(@activity) {4544next PROJECT;4545}4546($pr->{'age'},$pr->{'age_string'}) =@activity;4547if(!defined$pr->{'descr'}) {4548my$descr= git_get_project_description($pr->{'path'}) ||"";4549$descr= to_utf8($descr);4550$pr->{'descr_long'} =$descr;4551$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4552}4553if(!defined$pr->{'owner'}) {4554$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4555}4556if($check_forks) {4557my$pname=$pr->{'path'};4558if(($pname=~s/\.git$//) &&4559($pname!~/\/$/) &&4560(-d "$projectroot/$pname")) {4561$pr->{'forks'} ="-d$projectroot/$pname";4562}else{4563$pr->{'forks'} =0;4564}4565}4566$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4567push@projects,$pr;4568}45694570return@projects;4571}45724573# print 'sort by' <th> element, generating 'sort by $name' replay link4574# if that order is not selected4575sub print_sort_th {4576print format_sort_th(@_);4577}45784579sub format_sort_th {4580my($name,$order,$header) =@_;4581my$sort_th="";4582$header||=ucfirst($name);45834584if($ordereq$name) {4585$sort_th.="<th>$header</th>\n";4586}else{4587$sort_th.="<th>".4588$cgi->a({-href => href(-replay=>1, order=>$name),4589-class=>"header"},$header) .4590"</th>\n";4591}45924593return$sort_th;4594}45954596sub git_project_list_body {4597# actually uses global variable $project4598my($projlist,$order,$from,$to,$extra,$no_header) =@_;45994600my$check_forks= gitweb_check_feature('forks');4601my@projects= fill_project_list_info($projlist,$check_forks);46024603$order||=$default_projects_order;4604$from=0unlessdefined$from;4605$to=$#projectsif(!defined$to||$#projects<$to);46064607my%order_info= (4608 project => { key =>'path', type =>'str'},4609 descr => { key =>'descr_long', type =>'str'},4610 owner => { key =>'owner', type =>'str'},4611 age => { key =>'age', type =>'num'}4612);4613my$oi=$order_info{$order};4614if($oi->{'type'}eq'str') {4615@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4616}else{4617@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4618}46194620my$show_ctags= gitweb_check_feature('ctags');4621if($show_ctags) {4622my%ctags;4623foreachmy$p(@projects) {4624foreachmy$ct(keys%{$p->{'ctags'}}) {4625$ctags{$ct} +=$p->{'ctags'}->{$ct};4626}4627}4628my$cloud= git_populate_project_tagcloud(\%ctags);4629print git_show_project_tagcloud($cloud,64);4630}46314632print"<table class=\"project_list\">\n";4633unless($no_header) {4634print"<tr>\n";4635if($check_forks) {4636print"<th></th>\n";4637}4638 print_sort_th('project',$order,'Project');4639 print_sort_th('descr',$order,'Description');4640 print_sort_th('owner',$order,'Owner');4641 print_sort_th('age',$order,'Last Change');4642print"<th></th>\n".# for links4643"</tr>\n";4644}4645my$alternate=1;4646my$tagfilter=$cgi->param('by_tag');4647for(my$i=$from;$i<=$to;$i++) {4648my$pr=$projects[$i];46494650next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4651next if$searchtextand not$pr->{'path'} =~/$searchtext/4652and not$pr->{'descr_long'} =~/$searchtext/;4653# Weed out forks or non-matching entries of search4654if($check_forks) {4655my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4656$forkbase="^$forkbase"if$forkbase;4657next ifnot$searchtextand not$tagfilterand$show_ctags4658and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4659}46604661if($alternate) {4662print"<tr class=\"dark\">\n";4663}else{4664print"<tr class=\"light\">\n";4665}4666$alternate^=1;4667if($check_forks) {4668print"<td>";4669if($pr->{'forks'}) {4670print"<!--$pr->{'forks'} -->\n";4671print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4672}4673print"</td>\n";4674}4675print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4676-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4677"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4678-class=>"list", -title =>$pr->{'descr_long'}},4679 esc_html($pr->{'descr'})) ."</td>\n".4680"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4681print"<td class=\"". age_class($pr->{'age'}) ."\">".4682(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4683"<td class=\"link\">".4684$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4685$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4686$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4687$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4688($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4689"</td>\n".4690"</tr>\n";4691}4692if(defined$extra) {4693print"<tr>\n";4694if($check_forks) {4695print"<td></td>\n";4696}4697print"<td colspan=\"5\">$extra</td>\n".4698"</tr>\n";4699}4700print"</table>\n";4701}47024703sub git_log_body {4704# uses global variable $project4705my($commitlist,$from,$to,$refs,$extra) =@_;47064707$from=0unlessdefined$from;4708$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47094710for(my$i=0;$i<=$to;$i++) {4711my%co= %{$commitlist->[$i]};4712next if!%co;4713my$commit=$co{'id'};4714my$ref= format_ref_marker($refs,$commit);4715my%ad= parse_date($co{'author_epoch'});4716 git_print_header_div('commit',4717"<span class=\"age\">$co{'age_string'}</span>".4718 esc_html($co{'title'}) .$ref,4719$commit);4720print"<div class=\"title_text\">\n".4721"<div class=\"log_link\">\n".4722$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4723" | ".4724$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4725" | ".4726$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4727"<br/>\n".4728"</div>\n";4729 git_print_authorship(\%co, -tag =>'span');4730print"<br/>\n</div>\n";47314732print"<div class=\"log_body\">\n";4733 git_print_log($co{'comment'}, -final_empty_line=>1);4734print"</div>\n";4735}4736if($extra) {4737print"<div class=\"page_nav\">\n";4738print"$extra\n";4739print"</div>\n";4740}4741}47424743sub git_shortlog_body {4744# uses global variable $project4745my($commitlist,$from,$to,$refs,$extra) =@_;47464747$from=0unlessdefined$from;4748$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47494750print"<table class=\"shortlog\">\n";4751my$alternate=1;4752for(my$i=$from;$i<=$to;$i++) {4753my%co= %{$commitlist->[$i]};4754my$commit=$co{'id'};4755my$ref= format_ref_marker($refs,$commit);4756if($alternate) {4757print"<tr class=\"dark\">\n";4758}else{4759print"<tr class=\"light\">\n";4760}4761$alternate^=1;4762# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4763print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4764 format_author_html('td', \%co,10) ."<td>";4765print format_subject_html($co{'title'},$co{'title_short'},4766 href(action=>"commit", hash=>$commit),$ref);4767print"</td>\n".4768"<td class=\"link\">".4769$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4770$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4771$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4772my$snapshot_links= format_snapshot_links($commit);4773if(defined$snapshot_links) {4774print" | ".$snapshot_links;4775}4776print"</td>\n".4777"</tr>\n";4778}4779if(defined$extra) {4780print"<tr>\n".4781"<td colspan=\"4\">$extra</td>\n".4782"</tr>\n";4783}4784print"</table>\n";4785}47864787sub git_history_body {4788# Warning: assumes constant type (blob or tree) during history4789my($commitlist,$from,$to,$refs,$extra,4790$file_name,$file_hash,$ftype) =@_;47914792$from=0unlessdefined$from;4793$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});47944795print"<table class=\"history\">\n";4796my$alternate=1;4797for(my$i=$from;$i<=$to;$i++) {4798my%co= %{$commitlist->[$i]};4799if(!%co) {4800next;4801}4802my$commit=$co{'id'};48034804my$ref= format_ref_marker($refs,$commit);48054806if($alternate) {4807print"<tr class=\"dark\">\n";4808}else{4809print"<tr class=\"light\">\n";4810}4811$alternate^=1;4812print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4813# shortlog: format_author_html('td', \%co, 10)4814 format_author_html('td', \%co,15,3) ."<td>";4815# originally git_history used chop_str($co{'title'}, 50)4816print format_subject_html($co{'title'},$co{'title_short'},4817 href(action=>"commit", hash=>$commit),$ref);4818print"</td>\n".4819"<td class=\"link\">".4820$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4821$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");48224823if($ftypeeq'blob') {4824my$blob_current=$file_hash;4825my$blob_parent= git_get_hash_by_path($commit,$file_name);4826if(defined$blob_current&&defined$blob_parent&&4827$blob_currentne$blob_parent) {4828print" | ".4829$cgi->a({-href => href(action=>"blobdiff",4830 hash=>$blob_current, hash_parent=>$blob_parent,4831 hash_base=>$hash_base, hash_parent_base=>$commit,4832 file_name=>$file_name)},4833"diff to current");4834}4835}4836print"</td>\n".4837"</tr>\n";4838}4839if(defined$extra) {4840print"<tr>\n".4841"<td colspan=\"4\">$extra</td>\n".4842"</tr>\n";4843}4844print"</table>\n";4845}48464847sub git_tags_body {4848# uses global variable $project4849my($taglist,$from,$to,$extra) =@_;4850$from=0unlessdefined$from;4851$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);48524853print"<table class=\"tags\">\n";4854my$alternate=1;4855for(my$i=$from;$i<=$to;$i++) {4856my$entry=$taglist->[$i];4857my%tag=%$entry;4858my$comment=$tag{'subject'};4859my$comment_short;4860if(defined$comment) {4861$comment_short= chop_str($comment,30,5);4862}4863if($alternate) {4864print"<tr class=\"dark\">\n";4865}else{4866print"<tr class=\"light\">\n";4867}4868$alternate^=1;4869if(defined$tag{'age'}) {4870print"<td><i>$tag{'age'}</i></td>\n";4871}else{4872print"<td></td>\n";4873}4874print"<td>".4875$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4876-class=>"list name"}, esc_html($tag{'name'})) .4877"</td>\n".4878"<td>";4879if(defined$comment) {4880print format_subject_html($comment,$comment_short,4881 href(action=>"tag", hash=>$tag{'id'}));4882}4883print"</td>\n".4884"<td class=\"selflink\">";4885if($tag{'type'}eq"tag") {4886print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4887}else{4888print" ";4889}4890print"</td>\n".4891"<td class=\"link\">"." | ".4892$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4893if($tag{'reftype'}eq"commit") {4894print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4895" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4896}elsif($tag{'reftype'}eq"blob") {4897print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4898}4899print"</td>\n".4900"</tr>";4901}4902if(defined$extra) {4903print"<tr>\n".4904"<td colspan=\"5\">$extra</td>\n".4905"</tr>\n";4906}4907print"</table>\n";4908}49094910sub git_heads_body {4911# uses global variable $project4912my($headlist,$head,$from,$to,$extra) =@_;4913$from=0unlessdefined$from;4914$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);49154916print"<table class=\"heads\">\n";4917my$alternate=1;4918for(my$i=$from;$i<=$to;$i++) {4919my$entry=$headlist->[$i];4920my%ref=%$entry;4921my$curr=$ref{'id'}eq$head;4922if($alternate) {4923print"<tr class=\"dark\">\n";4924}else{4925print"<tr class=\"light\">\n";4926}4927$alternate^=1;4928print"<td><i>$ref{'age'}</i></td>\n".4929($curr?"<td class=\"current_head\">":"<td>") .4930$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4931-class=>"list name"},esc_html($ref{'name'})) .4932"</td>\n".4933"<td class=\"link\">".4934$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4935$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4936$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4937"</td>\n".4938"</tr>";4939}4940if(defined$extra) {4941print"<tr>\n".4942"<td colspan=\"3\">$extra</td>\n".4943"</tr>\n";4944}4945print"</table>\n";4946}49474948sub git_search_grep_body {4949my($commitlist,$from,$to,$extra) =@_;4950$from=0unlessdefined$from;4951$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);49524953print"<table class=\"commit_search\">\n";4954my$alternate=1;4955for(my$i=$from;$i<=$to;$i++) {4956my%co= %{$commitlist->[$i]};4957if(!%co) {4958next;4959}4960my$commit=$co{'id'};4961if($alternate) {4962print"<tr class=\"dark\">\n";4963}else{4964print"<tr class=\"light\">\n";4965}4966$alternate^=1;4967print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4968 format_author_html('td', \%co,15,5) .4969"<td>".4970$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4971-class=>"list subject"},4972 chop_and_escape_str($co{'title'},50) ."<br/>");4973my$comment=$co{'comment'};4974foreachmy$line(@$comment) {4975if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4976my($lead,$match,$trail) = ($1,$2,$3);4977$match= chop_str($match,70,5,'center');4978my$contextlen=int((80-length($match))/2);4979$contextlen=30if($contextlen>30);4980$lead= chop_str($lead,$contextlen,10,'left');4981$trail= chop_str($trail,$contextlen,10,'right');49824983$lead= esc_html($lead);4984$match= esc_html($match);4985$trail= esc_html($trail);49864987print"$lead<span class=\"match\">$match</span>$trail<br />";4988}4989}4990print"</td>\n".4991"<td class=\"link\">".4992$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4993" | ".4994$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4995" | ".4996$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4997print"</td>\n".4998"</tr>\n";4999}5000if(defined$extra) {5001print"<tr>\n".5002"<td colspan=\"3\">$extra</td>\n".5003"</tr>\n";5004}5005print"</table>\n";5006}50075008## ======================================================================5009## ======================================================================5010## actions50115012sub git_project_list {5013my$order=$input_params{'order'};5014if(defined$order&&$order!~m/none|project|descr|owner|age/) {5015 die_error(400,"Unknown order parameter");5016}50175018my@list= git_get_projects_list();5019if(!@list) {5020 die_error(404,"No projects found");5021}50225023 git_header_html();5024if(defined$home_text&& -f $home_text) {5025print"<div class=\"index_include\">\n";5026 insert_file($home_text);5027print"</div>\n";5028}5029print$cgi->startform(-method=>"get") .5030"<p class=\"projsearch\">Search:\n".5031$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".5032"</p>".5033$cgi->end_form() ."\n";5034 git_project_list_body(\@list,$order);5035 git_footer_html();5036}50375038sub git_forks {5039my$order=$input_params{'order'};5040if(defined$order&&$order!~m/none|project|descr|owner|age/) {5041 die_error(400,"Unknown order parameter");5042}50435044my@list= git_get_projects_list($project);5045if(!@list) {5046 die_error(404,"No forks found");5047}50485049 git_header_html();5050 git_print_page_nav('','');5051 git_print_header_div('summary',"$projectforks");5052 git_project_list_body(\@list,$order);5053 git_footer_html();5054}50555056sub git_project_index {5057my@projects= git_get_projects_list($project);50585059print$cgi->header(5060-type =>'text/plain',5061-charset =>'utf-8',5062-content_disposition =>'inline; filename="index.aux"');50635064foreachmy$pr(@projects) {5065if(!exists$pr->{'owner'}) {5066$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");5067}50685069my($path,$owner) = ($pr->{'path'},$pr->{'owner'});5070# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '5071$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5072$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5073$path=~s/ /\+/g;5074$owner=~s/ /\+/g;50755076print"$path$owner\n";5077}5078}50795080sub git_summary {5081my$descr= git_get_project_description($project) ||"none";5082my%co= parse_commit("HEAD");5083my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();5084my$head=$co{'id'};50855086my$owner= git_get_project_owner($project);50875088my$refs= git_get_references();5089# These get_*_list functions return one more to allow us to see if5090# there are more ...5091my@taglist= git_get_tags_list(16);5092my@headlist= git_get_heads_list(16);5093my@forklist;5094my$check_forks= gitweb_check_feature('forks');50955096if($check_forks) {5097@forklist= git_get_projects_list($project);5098}50995100 git_header_html();5101 git_print_page_nav('summary','',$head);51025103print"<div class=\"title\"> </div>\n";5104print"<table class=\"projects_list\">\n".5105"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".5106"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";5107if(defined$cd{'rfc2822'}) {5108print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";5109}51105111# use per project git URL list in $projectroot/$project/cloneurl5112# or make project git URL from git base URL and project name5113my$url_tag="URL";5114my@url_list= git_get_project_url_list($project);5115@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;5116foreachmy$git_url(@url_list) {5117next unless$git_url;5118print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";5119$url_tag="";5120}51215122# Tag cloud5123my$show_ctags= gitweb_check_feature('ctags');5124if($show_ctags) {5125my$ctags= git_get_project_ctags($project);5126my$cloud= git_populate_project_tagcloud($ctags);5127print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";5128print"</td>\n<td>"unless%$ctags;5129print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";5130print"</td>\n<td>"if%$ctags;5131print git_show_project_tagcloud($cloud,48);5132print"</td></tr>";5133}51345135print"</table>\n";51365137# If XSS prevention is on, we don't include README.html.5138# TODO: Allow a readme in some safe format.5139if(!$prevent_xss&& -s "$projectroot/$project/README.html") {5140print"<div class=\"title\">readme</div>\n".5141"<div class=\"readme\">\n";5142 insert_file("$projectroot/$project/README.html");5143print"\n</div>\n";# class="readme"5144}51455146# we need to request one more than 16 (0..15) to check if5147# those 16 are all5148my@commitlist=$head? parse_commits($head,17) : ();5149if(@commitlist) {5150 git_print_header_div('shortlog');5151 git_shortlog_body(\@commitlist,0,15,$refs,5152$#commitlist<=15?undef:5153$cgi->a({-href => href(action=>"shortlog")},"..."));5154}51555156if(@taglist) {5157 git_print_header_div('tags');5158 git_tags_body(\@taglist,0,15,5159$#taglist<=15?undef:5160$cgi->a({-href => href(action=>"tags")},"..."));5161}51625163if(@headlist) {5164 git_print_header_div('heads');5165 git_heads_body(\@headlist,$head,0,15,5166$#headlist<=15?undef:5167$cgi->a({-href => href(action=>"heads")},"..."));5168}51695170if(@forklist) {5171 git_print_header_div('forks');5172 git_project_list_body(\@forklist,'age',0,15,5173$#forklist<=15?undef:5174$cgi->a({-href => href(action=>"forks")},"..."),5175'no_header');5176}51775178 git_footer_html();5179}51805181sub git_tag {5182my$head= git_get_head_hash($project);5183 git_header_html();5184 git_print_page_nav('','',$head,undef,$head);5185my%tag= parse_tag($hash);51865187if(!%tag) {5188 die_error(404,"Unknown tag object");5189}51905191 git_print_header_div('commit', esc_html($tag{'name'}),$hash);5192print"<div class=\"title_text\">\n".5193"<table class=\"object_header\">\n".5194"<tr>\n".5195"<td>object</td>\n".5196"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5197$tag{'object'}) ."</td>\n".5198"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5199$tag{'type'}) ."</td>\n".5200"</tr>\n";5201if(defined($tag{'author'})) {5202 git_print_authorship_rows(\%tag,'author');5203}5204print"</table>\n\n".5205"</div>\n";5206print"<div class=\"page_body\">";5207my$comment=$tag{'comment'};5208foreachmy$line(@$comment) {5209chomp$line;5210print esc_html($line, -nbsp=>1) ."<br/>\n";5211}5212print"</div>\n";5213 git_footer_html();5214}52155216sub git_blame_common {5217my$format=shift||'porcelain';5218if($formateq'porcelain'&&$cgi->param('js')) {5219$format='incremental';5220$action='blame_incremental';# for page title etc5221}52225223# permissions5224 gitweb_check_feature('blame')5225or die_error(403,"Blame view not allowed");52265227# error checking5228 die_error(400,"No file name given")unless$file_name;5229$hash_base||= git_get_head_hash($project);5230 die_error(404,"Couldn't find base commit")unless$hash_base;5231my%co= parse_commit($hash_base)5232or die_error(404,"Commit not found");5233my$ftype="blob";5234if(!defined$hash) {5235$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5236or die_error(404,"Error looking up file");5237}else{5238$ftype= git_get_type($hash);5239if($ftype!~"blob") {5240 die_error(400,"Object is not a blob");5241}5242}52435244my$fd;5245if($formateq'incremental') {5246# get file contents (as base)5247open$fd,"-|", git_cmd(),'cat-file','blob',$hash5248or die_error(500,"Open git-cat-file failed");5249}elsif($formateq'data') {5250# run git-blame --incremental5251open$fd,"-|", git_cmd(),"blame","--incremental",5252$hash_base,"--",$file_name5253or die_error(500,"Open git-blame --incremental failed");5254}else{5255# run git-blame --porcelain5256open$fd,"-|", git_cmd(),"blame",'-p',5257$hash_base,'--',$file_name5258or die_error(500,"Open git-blame --porcelain failed");5259}52605261# incremental blame data returns early5262if($formateq'data') {5263print$cgi->header(5264-type=>"text/plain", -charset =>"utf-8",5265-status=>"200 OK");5266local$| =1;# output autoflush5267printwhile<$fd>;5268close$fd5269or print"ERROR$!\n";52705271print'END';5272if(defined$t0&& gitweb_check_feature('timed')) {5273print' '.5274 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5275' '.$number_of_git_cmds;5276}5277print"\n";52785279return;5280}52815282# page header5283 git_header_html();5284my$formats_nav=5285$cgi->a({-href => href(action=>"blob", -replay=>1)},5286"blob") .5287" | ";5288if($formateq'incremental') {5289$formats_nav.=5290$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5291"blame") ." (non-incremental)";5292}else{5293$formats_nav.=5294$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5295"blame") ." (incremental)";5296}5297$formats_nav.=5298" | ".5299$cgi->a({-href => href(action=>"history", -replay=>1)},5300"history") .5301" | ".5302$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5303"HEAD");5304 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5305 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5306 git_print_page_path($file_name,$ftype,$hash_base);53075308# page body5309if($formateq'incremental') {5310print"<noscript>\n<div class=\"error\"><center><b>\n".5311"This page requires JavaScript to run.\nUse ".5312$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5313'this page').5314" instead.\n".5315"</b></center></div>\n</noscript>\n";53165317print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5318}53195320print qq!<div class="page_body">\n!;5321print qq!<div id="progress_info">.../ ...</div>\n!5322if($formateq'incremental');5323print qq!<table id="blame_table"class="blame" width="100%">\n!.5324#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5325 qq!<thead>\n!.5326 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5327 qq!</thead>\n!.5328 qq!<tbody>\n!;53295330my@rev_color=qw(light dark);5331my$num_colors=scalar(@rev_color);5332my$current_color=0;53335334if($formateq'incremental') {5335my$color_class=$rev_color[$current_color];53365337#contents of a file5338my$linenr=0;5339 LINE:5340while(my$line= <$fd>) {5341chomp$line;5342$linenr++;53435344print qq!<tr id="l$linenr"class="$color_class">!.5345 qq!<td class="sha1"><a href=""> </a></td>!.5346 qq!<td class="linenr">!.5347 qq!<a class="linenr" href="">$linenr</a></td>!;5348print qq!<td class="pre">! . esc_html($line) ."</td>\n";5349print qq!</tr>\n!;5350}53515352}else{# porcelain, i.e. ordinary blame5353my%metainfo= ();# saves information about commits53545355# blame data5356 LINE:5357while(my$line= <$fd>) {5358chomp$line;5359# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5360# no <lines in group> for subsequent lines in group of lines5361my($full_rev,$orig_lineno,$lineno,$group_size) =5362($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5363if(!exists$metainfo{$full_rev}) {5364$metainfo{$full_rev} = {'nprevious'=>0};5365}5366my$meta=$metainfo{$full_rev};5367my$data;5368while($data= <$fd>) {5369chomp$data;5370last if($data=~s/^\t//);# contents of line5371if($data=~/^(\S+)(?: (.*))?$/) {5372$meta->{$1} =$2unlessexists$meta->{$1};5373}5374if($data=~/^previous /) {5375$meta->{'nprevious'}++;5376}5377}5378my$short_rev=substr($full_rev,0,8);5379my$author=$meta->{'author'};5380my%date=5381 parse_date($meta->{'author-time'},$meta->{'author-tz'});5382my$date=$date{'iso-tz'};5383if($group_size) {5384$current_color= ($current_color+1) %$num_colors;5385}5386my$tr_class=$rev_color[$current_color];5387$tr_class.=' boundary'if(exists$meta->{'boundary'});5388$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5389$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5390print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5391if($group_size) {5392print"<td class=\"sha1\"";5393print" title=\"". esc_html($author) .",$date\"";5394print" rowspan=\"$group_size\""if($group_size>1);5395print">";5396print$cgi->a({-href => href(action=>"commit",5397 hash=>$full_rev,5398 file_name=>$file_name)},5399 esc_html($short_rev));5400if($group_size>=2) {5401my@author_initials= ($author=~/\b([[:upper:]])\B/g);5402if(@author_initials) {5403print"<br />".5404 esc_html(join('',@author_initials));5405# or join('.', ...)5406}5407}5408print"</td>\n";5409}5410# 'previous' <sha1 of parent commit> <filename at commit>5411if(exists$meta->{'previous'} &&5412$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5413$meta->{'parent'} =$1;5414$meta->{'file_parent'} = unquote($2);5415}5416my$linenr_commit=5417exists($meta->{'parent'}) ?5418$meta->{'parent'} :$full_rev;5419my$linenr_filename=5420exists($meta->{'file_parent'}) ?5421$meta->{'file_parent'} : unquote($meta->{'filename'});5422my$blamed= href(action =>'blame',5423 file_name =>$linenr_filename,5424 hash_base =>$linenr_commit);5425print"<td class=\"linenr\">";5426print$cgi->a({ -href =>"$blamed#l$orig_lineno",5427-class=>"linenr"},5428 esc_html($lineno));5429print"</td>";5430print"<td class=\"pre\">". esc_html($data) ."</td>\n";5431print"</tr>\n";5432}# end while54335434}54355436# footer5437print"</tbody>\n".5438"</table>\n";# class="blame"5439print"</div>\n";# class="blame_body"5440close$fd5441or print"Reading blob failed\n";54425443 git_footer_html();5444}54455446sub git_blame {5447 git_blame_common();5448}54495450sub git_blame_incremental {5451 git_blame_common('incremental');5452}54535454sub git_blame_data {5455 git_blame_common('data');5456}54575458sub git_tags {5459my$head= git_get_head_hash($project);5460 git_header_html();5461 git_print_page_nav('','',$head,undef,$head);5462 git_print_header_div('summary',$project);54635464my@tagslist= git_get_tags_list();5465if(@tagslist) {5466 git_tags_body(\@tagslist);5467}5468 git_footer_html();5469}54705471sub git_heads {5472my$head= git_get_head_hash($project);5473 git_header_html();5474 git_print_page_nav('','',$head,undef,$head);5475 git_print_header_div('summary',$project);54765477my@headslist= git_get_heads_list();5478if(@headslist) {5479 git_heads_body(\@headslist,$head);5480}5481 git_footer_html();5482}54835484sub git_blob_plain {5485my$type=shift;5486my$expires;54875488if(!defined$hash) {5489if(defined$file_name) {5490my$base=$hash_base|| git_get_head_hash($project);5491$hash= git_get_hash_by_path($base,$file_name,"blob")5492or die_error(404,"Cannot find file");5493}else{5494 die_error(400,"No file name defined");5495}5496}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5497# blobs defined by non-textual hash id's can be cached5498$expires="+1d";5499}55005501open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5502or die_error(500,"Open git-cat-file blob '$hash' failed");55035504# content-type (can include charset)5505$type= blob_contenttype($fd,$file_name,$type);55065507# "save as" filename, even when no $file_name is given5508my$save_as="$hash";5509if(defined$file_name) {5510$save_as=$file_name;5511}elsif($type=~m/^text\//) {5512$save_as.='.txt';5513}55145515# With XSS prevention on, blobs of all types except a few known safe5516# ones are served with "Content-Disposition: attachment" to make sure5517# they don't run in our security domain. For certain image types,5518# blob view writes an <img> tag referring to blob_plain view, and we5519# want to be sure not to break that by serving the image as an5520# attachment (though Firefox 3 doesn't seem to care).5521my$sandbox=$prevent_xss&&5522$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;55235524print$cgi->header(5525-type =>$type,5526-expires =>$expires,5527-content_disposition =>5528($sandbox?'attachment':'inline')5529.'; filename="'.$save_as.'"');5530local$/=undef;5531binmode STDOUT,':raw';5532print<$fd>;5533binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5534close$fd;5535}55365537sub git_blob {5538my$expires;55395540if(!defined$hash) {5541if(defined$file_name) {5542my$base=$hash_base|| git_get_head_hash($project);5543$hash= git_get_hash_by_path($base,$file_name,"blob")5544or die_error(404,"Cannot find file");5545}else{5546 die_error(400,"No file name defined");5547}5548}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5549# blobs defined by non-textual hash id's can be cached5550$expires="+1d";5551}55525553my$have_blame= gitweb_check_feature('blame');5554open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5555or die_error(500,"Couldn't cat$file_name,$hash");5556my$mimetype= blob_mimetype($fd,$file_name);5557# use 'blob_plain' (aka 'raw') view for files that cannot be displayed5558if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5559close$fd;5560return git_blob_plain($mimetype);5561}5562# we can have blame only for text/* mimetype5563$have_blame&&= ($mimetype=~m!^text/!);55645565my$highlight= gitweb_check_feature('highlight');5566my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);5567$fd= run_highlighter($fd,$highlight,$syntax)5568if$syntax;55695570 git_header_html(undef,$expires);5571my$formats_nav='';5572if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5573if(defined$file_name) {5574if($have_blame) {5575$formats_nav.=5576$cgi->a({-href => href(action=>"blame", -replay=>1)},5577"blame") .5578" | ";5579}5580$formats_nav.=5581$cgi->a({-href => href(action=>"history", -replay=>1)},5582"history") .5583" | ".5584$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5585"raw") .5586" | ".5587$cgi->a({-href => href(action=>"blob",5588 hash_base=>"HEAD", file_name=>$file_name)},5589"HEAD");5590}else{5591$formats_nav.=5592$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5593"raw");5594}5595 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5596 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5597}else{5598print"<div class=\"page_nav\">\n".5599"<br/><br/></div>\n".5600"<div class=\"title\">$hash</div>\n";5601}5602 git_print_page_path($file_name,"blob",$hash_base);5603print"<div class=\"page_body\">\n";5604if($mimetype=~m!^image/!) {5605print qq!<img type="$mimetype"!;5606if($file_name) {5607print qq! alt="$file_name" title="$file_name"!;5608}5609print qq! src="! .5610 href(action=>"blob_plain", hash=>$hash,5611 hash_base=>$hash_base, file_name=>$file_name) .5612 qq!"/>\n!;5613}else{5614my$nr;5615while(my$line= <$fd>) {5616chomp$line;5617$nr++;5618$line= untabify($line);5619printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,5620$nr, href(-replay =>1),$nr,$nr,$syntax?$line: esc_html($line, -nbsp=>1);5621}5622}5623close$fd5624or print"Reading blob failed.\n";5625print"</div>";5626 git_footer_html();5627}56285629sub git_tree {5630if(!defined$hash_base) {5631$hash_base="HEAD";5632}5633if(!defined$hash) {5634if(defined$file_name) {5635$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5636}else{5637$hash=$hash_base;5638}5639}5640 die_error(404,"No such tree")unlessdefined($hash);56415642my$show_sizes= gitweb_check_feature('show-sizes');5643my$have_blame= gitweb_check_feature('blame');56445645my@entries= ();5646{5647local$/="\0";5648open my$fd,"-|", git_cmd(),"ls-tree",'-z',5649($show_sizes?'-l': ()),@extra_options,$hash5650or die_error(500,"Open git-ls-tree failed");5651@entries=map{chomp;$_} <$fd>;5652close$fd5653or die_error(404,"Reading tree failed");5654}56555656my$refs= git_get_references();5657my$ref= format_ref_marker($refs,$hash_base);5658 git_header_html();5659my$basedir='';5660if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5661my@views_nav= ();5662if(defined$file_name) {5663push@views_nav,5664$cgi->a({-href => href(action=>"history", -replay=>1)},5665"history"),5666$cgi->a({-href => href(action=>"tree",5667 hash_base=>"HEAD", file_name=>$file_name)},5668"HEAD"),5669}5670my$snapshot_links= format_snapshot_links($hash);5671if(defined$snapshot_links) {5672# FIXME: Should be available when we have no hash base as well.5673push@views_nav,$snapshot_links;5674}5675 git_print_page_nav('tree','',$hash_base,undef,undef,5676join(' | ',@views_nav));5677 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5678}else{5679undef$hash_base;5680print"<div class=\"page_nav\">\n";5681print"<br/><br/></div>\n";5682print"<div class=\"title\">$hash</div>\n";5683}5684if(defined$file_name) {5685$basedir=$file_name;5686if($basedirne''&&substr($basedir, -1)ne'/') {5687$basedir.='/';5688}5689 git_print_page_path($file_name,'tree',$hash_base);5690}5691print"<div class=\"page_body\">\n";5692print"<table class=\"tree\">\n";5693my$alternate=1;5694# '..' (top directory) link if possible5695if(defined$hash_base&&5696defined$file_name&&$file_name=~m![^/]+$!) {5697if($alternate) {5698print"<tr class=\"dark\">\n";5699}else{5700print"<tr class=\"light\">\n";5701}5702$alternate^=1;57035704my$up=$file_name;5705$up=~s!/?[^/]+$!!;5706undef$upunless$up;5707# based on git_print_tree_entry5708print'<td class="mode">'. mode_str('040000') ."</td>\n";5709print'<td class="size"> </td>'."\n"if$show_sizes;5710print'<td class="list">';5711print$cgi->a({-href => href(action=>"tree",5712 hash_base=>$hash_base,5713 file_name=>$up)},5714"..");5715print"</td>\n";5716print"<td class=\"link\"></td>\n";57175718print"</tr>\n";5719}5720foreachmy$line(@entries) {5721my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);57225723if($alternate) {5724print"<tr class=\"dark\">\n";5725}else{5726print"<tr class=\"light\">\n";5727}5728$alternate^=1;57295730 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);57315732print"</tr>\n";5733}5734print"</table>\n".5735"</div>";5736 git_footer_html();5737}57385739sub snapshot_name {5740my($project,$hash) =@_;57415742# path/to/project.git -> project5743# path/to/project/.git -> project5744my$name= to_utf8($project);5745$name=~ s,([^/])/*\.git$,$1,;5746$name= basename($name);5747# sanitize name5748$name=~s/[[:cntrl:]]/?/g;57495750my$ver=$hash;5751if($hash=~/^[0-9a-fA-F]+$/) {5752# shorten SHA-1 hash5753my$full_hash= git_get_full_hash($project,$hash);5754if($full_hash=~/^$hash/&&length($hash) >7) {5755$ver= git_get_short_hash($project,$hash);5756}5757}elsif($hash=~m!^refs/tags/(.*)$!) {5758# tags don't need shortened SHA-1 hash5759$ver=$1;5760}else{5761# branches and other need shortened SHA-1 hash5762if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5763$ver=$1;5764}5765$ver.='-'. git_get_short_hash($project,$hash);5766}5767# in case of hierarchical branch names5768$ver=~s!/!.!g;57695770# name = project-version_string5771$name="$name-$ver";57725773returnwantarray? ($name,$name) :$name;5774}57755776sub git_snapshot {5777my$format=$input_params{'snapshot_format'};5778if(!@snapshot_fmts) {5779 die_error(403,"Snapshots not allowed");5780}5781# default to first supported snapshot format5782$format||=$snapshot_fmts[0];5783if($format!~m/^[a-z0-9]+$/) {5784 die_error(400,"Invalid snapshot format parameter");5785}elsif(!exists($known_snapshot_formats{$format})) {5786 die_error(400,"Unknown snapshot format");5787}elsif($known_snapshot_formats{$format}{'disabled'}) {5788 die_error(403,"Snapshot format not allowed");5789}elsif(!grep($_eq$format,@snapshot_fmts)) {5790 die_error(403,"Unsupported snapshot format");5791}57925793my$type= git_get_type("$hash^{}");5794if(!$type) {5795 die_error(404,'Object does not exist');5796}elsif($typeeq'blob') {5797 die_error(400,'Object is not a tree-ish');5798}57995800my($name,$prefix) = snapshot_name($project,$hash);5801my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5802my$cmd= quote_command(5803 git_cmd(),'archive',5804"--format=$known_snapshot_formats{$format}{'format'}",5805"--prefix=$prefix/",$hash);5806if(exists$known_snapshot_formats{$format}{'compressor'}) {5807$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5808}58095810$filename=~s/(["\\])/\\$1/g;5811print$cgi->header(5812-type =>$known_snapshot_formats{$format}{'type'},5813-content_disposition =>'inline; filename="'.$filename.'"',5814-status =>'200 OK');58155816open my$fd,"-|",$cmd5817or die_error(500,"Execute git-archive failed");5818binmode STDOUT,':raw';5819print<$fd>;5820binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5821close$fd;5822}58235824sub git_log_generic {5825my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;58265827my$head= git_get_head_hash($project);5828if(!defined$base) {5829$base=$head;5830}5831if(!defined$page) {5832$page=0;5833}5834my$refs= git_get_references();58355836my$commit_hash=$base;5837if(defined$parent) {5838$commit_hash="$parent..$base";5839}5840my@commitlist=5841 parse_commits($commit_hash,101, (100*$page),5842defined$file_name? ($file_name,"--full-history") : ());58435844my$ftype;5845if(!defined$file_hash&&defined$file_name) {5846# some commits could have deleted file in question,5847# and not have it in tree, but one of them has to have it5848for(my$i=0;$i<@commitlist;$i++) {5849$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5850last ifdefined$file_hash;5851}5852}5853if(defined$file_hash) {5854$ftype= git_get_type($file_hash);5855}5856if(defined$file_name&& !defined$ftype) {5857 die_error(500,"Unknown type of object");5858}5859my%co;5860if(defined$file_name) {5861%co= parse_commit($base)5862or die_error(404,"Unknown commit object");5863}586458655866my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5867my$next_link='';5868if($#commitlist>=100) {5869$next_link=5870$cgi->a({-href => href(-replay=>1, page=>$page+1),5871-accesskey =>"n", -title =>"Alt-n"},"next");5872}5873my$patch_max= gitweb_get_feature('patches');5874if($patch_max&& !defined$file_name) {5875if($patch_max<0||@commitlist<=$patch_max) {5876$paging_nav.=" ⋅ ".5877$cgi->a({-href => href(action=>"patches", -replay=>1)},5878"patches");5879}5880}58815882 git_header_html();5883 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5884if(defined$file_name) {5885 git_print_header_div('commit', esc_html($co{'title'}),$base);5886}else{5887 git_print_header_div('summary',$project)5888}5889 git_print_page_path($file_name,$ftype,$hash_base)5890if(defined$file_name);58915892$body_subr->(\@commitlist,0,99,$refs,$next_link,5893$file_name,$file_hash,$ftype);58945895 git_footer_html();5896}58975898sub git_log {5899 git_log_generic('log', \&git_log_body,5900$hash,$hash_parent);5901}59025903sub git_commit {5904$hash||=$hash_base||"HEAD";5905my%co= parse_commit($hash)5906or die_error(404,"Unknown commit object");59075908my$parent=$co{'parent'};5909my$parents=$co{'parents'};# listref59105911# we need to prepare $formats_nav before any parameter munging5912my$formats_nav;5913if(!defined$parent) {5914# --root commitdiff5915$formats_nav.='(initial)';5916}elsif(@$parents==1) {5917# single parent commit5918$formats_nav.=5919'(parent: '.5920$cgi->a({-href => href(action=>"commit",5921 hash=>$parent)},5922 esc_html(substr($parent,0,7))) .5923')';5924}else{5925# merge commit5926$formats_nav.=5927'(merge: '.5928join(' ',map{5929$cgi->a({-href => href(action=>"commit",5930 hash=>$_)},5931 esc_html(substr($_,0,7)));5932}@$parents) .5933')';5934}5935if(gitweb_check_feature('patches') &&@$parents<=1) {5936$formats_nav.=" | ".5937$cgi->a({-href => href(action=>"patch", -replay=>1)},5938"patch");5939}59405941if(!defined$parent) {5942$parent="--root";5943}5944my@difftree;5945open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5946@diff_opts,5947(@$parents<=1?$parent:'-c'),5948$hash,"--"5949or die_error(500,"Open git-diff-tree failed");5950@difftree=map{chomp;$_} <$fd>;5951close$fdor die_error(404,"Reading git-diff-tree failed");59525953# non-textual hash id's can be cached5954my$expires;5955if($hash=~m/^[0-9a-fA-F]{40}$/) {5956$expires="+1d";5957}5958my$refs= git_get_references();5959my$ref= format_ref_marker($refs,$co{'id'});59605961 git_header_html(undef,$expires);5962 git_print_page_nav('commit','',5963$hash,$co{'tree'},$hash,5964$formats_nav);59655966if(defined$co{'parent'}) {5967 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5968}else{5969 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5970}5971print"<div class=\"title_text\">\n".5972"<table class=\"object_header\">\n";5973 git_print_authorship_rows(\%co);5974print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5975print"<tr>".5976"<td>tree</td>".5977"<td class=\"sha1\">".5978$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5979class=>"list"},$co{'tree'}) .5980"</td>".5981"<td class=\"link\">".5982$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5983"tree");5984my$snapshot_links= format_snapshot_links($hash);5985if(defined$snapshot_links) {5986print" | ".$snapshot_links;5987}5988print"</td>".5989"</tr>\n";59905991foreachmy$par(@$parents) {5992print"<tr>".5993"<td>parent</td>".5994"<td class=\"sha1\">".5995$cgi->a({-href => href(action=>"commit", hash=>$par),5996class=>"list"},$par) .5997"</td>".5998"<td class=\"link\">".5999$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .6000" | ".6001$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .6002"</td>".6003"</tr>\n";6004}6005print"</table>".6006"</div>\n";60076008print"<div class=\"page_body\">\n";6009 git_print_log($co{'comment'});6010print"</div>\n";60116012 git_difftree_body(\@difftree,$hash,@$parents);60136014 git_footer_html();6015}60166017sub git_object {6018# object is defined by:6019# - hash or hash_base alone6020# - hash_base and file_name6021my$type;60226023# - hash or hash_base alone6024if($hash|| ($hash_base&& !defined$file_name)) {6025my$object_id=$hash||$hash_base;60266027open my$fd,"-|", quote_command(6028 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'6029or die_error(404,"Object does not exist");6030$type= <$fd>;6031chomp$type;6032close$fd6033or die_error(404,"Object does not exist");60346035# - hash_base and file_name6036}elsif($hash_base&&defined$file_name) {6037$file_name=~ s,/+$,,;60386039system(git_cmd(),"cat-file",'-e',$hash_base) ==06040or die_error(404,"Base object does not exist");60416042# here errors should not hapen6043open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name6044or die_error(500,"Open git-ls-tree failed");6045my$line= <$fd>;6046close$fd;60476048#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'6049unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {6050 die_error(404,"File or directory for given base does not exist");6051}6052$type=$2;6053$hash=$3;6054}else{6055 die_error(400,"Not enough information to find object");6056}60576058print$cgi->redirect(-uri => href(action=>$type, -full=>1,6059 hash=>$hash, hash_base=>$hash_base,6060 file_name=>$file_name),6061-status =>'302 Found');6062}60636064sub git_blobdiff {6065my$format=shift||'html';60666067my$fd;6068my@difftree;6069my%diffinfo;6070my$expires;60716072# preparing $fd and %diffinfo for git_patchset_body6073# new style URI6074if(defined$hash_base&&defined$hash_parent_base) {6075if(defined$file_name) {6076# read raw output6077open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6078$hash_parent_base,$hash_base,6079"--", (defined$file_parent?$file_parent: ()),$file_name6080or die_error(500,"Open git-diff-tree failed");6081@difftree=map{chomp;$_} <$fd>;6082close$fd6083or die_error(404,"Reading git-diff-tree failed");6084@difftree6085or die_error(404,"Blob diff not found");60866087}elsif(defined$hash&&6088$hash=~/[0-9a-fA-F]{40}/) {6089# try to find filename from $hash60906091# read filtered raw output6092open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6093$hash_parent_base,$hash_base,"--"6094or die_error(500,"Open git-diff-tree failed");6095@difftree=6096# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'6097# $hash == to_id6098grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}6099map{chomp;$_} <$fd>;6100close$fd6101or die_error(404,"Reading git-diff-tree failed");6102@difftree6103or die_error(404,"Blob diff not found");61046105}else{6106 die_error(400,"Missing one of the blob diff parameters");6107}61086109if(@difftree>1) {6110 die_error(400,"Ambiguous blob diff specification");6111}61126113%diffinfo= parse_difftree_raw_line($difftree[0]);6114$file_parent||=$diffinfo{'from_file'} ||$file_name;6115$file_name||=$diffinfo{'to_file'};61166117$hash_parent||=$diffinfo{'from_id'};6118$hash||=$diffinfo{'to_id'};61196120# non-textual hash id's can be cached6121if($hash_base=~m/^[0-9a-fA-F]{40}$/&&6122$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {6123$expires='+1d';6124}61256126# open patch output6127open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6128'-p', ($formateq'html'?"--full-index": ()),6129$hash_parent_base,$hash_base,6130"--", (defined$file_parent?$file_parent: ()),$file_name6131or die_error(500,"Open git-diff-tree failed");6132}61336134# old/legacy style URI -- not generated anymore since 1.4.3.6135if(!%diffinfo) {6136 die_error('404 Not Found',"Missing one of the blob diff parameters")6137}61386139# header6140if($formateq'html') {6141my$formats_nav=6142$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},6143"raw");6144 git_header_html(undef,$expires);6145if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6146 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6147 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6148}else{6149print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";6150print"<div class=\"title\">$hashvs$hash_parent</div>\n";6151}6152if(defined$file_name) {6153 git_print_page_path($file_name,"blob",$hash_base);6154}else{6155print"<div class=\"page_path\"></div>\n";6156}61576158}elsif($formateq'plain') {6159print$cgi->header(6160-type =>'text/plain',6161-charset =>'utf-8',6162-expires =>$expires,6163-content_disposition =>'inline; filename="'."$file_name".'.patch"');61646165print"X-Git-Url: ".$cgi->self_url() ."\n\n";61666167}else{6168 die_error(400,"Unknown blobdiff format");6169}61706171# patch6172if($formateq'html') {6173print"<div class=\"page_body\">\n";61746175 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);6176close$fd;61776178print"</div>\n";# class="page_body"6179 git_footer_html();61806181}else{6182while(my$line= <$fd>) {6183$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;6184$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;61856186print$line;61876188last if$line=~m!^\+\+\+!;6189}6190local$/=undef;6191print<$fd>;6192close$fd;6193}6194}61956196sub git_blobdiff_plain {6197 git_blobdiff('plain');6198}61996200sub git_commitdiff {6201my%params=@_;6202my$format=$params{-format} ||'html';62036204my($patch_max) = gitweb_get_feature('patches');6205if($formateq'patch') {6206 die_error(403,"Patch view not allowed")unless$patch_max;6207}62086209$hash||=$hash_base||"HEAD";6210my%co= parse_commit($hash)6211or die_error(404,"Unknown commit object");62126213# choose format for commitdiff for merge6214if(!defined$hash_parent&& @{$co{'parents'}} >1) {6215$hash_parent='--cc';6216}6217# we need to prepare $formats_nav before almost any parameter munging6218my$formats_nav;6219if($formateq'html') {6220$formats_nav=6221$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6222"raw");6223if($patch_max&& @{$co{'parents'}} <=1) {6224$formats_nav.=" | ".6225$cgi->a({-href => href(action=>"patch", -replay=>1)},6226"patch");6227}62286229if(defined$hash_parent&&6230$hash_parentne'-c'&&$hash_parentne'--cc') {6231# commitdiff with two commits given6232my$hash_parent_short=$hash_parent;6233if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6234$hash_parent_short=substr($hash_parent,0,7);6235}6236$formats_nav.=6237' (from';6238for(my$i=0;$i< @{$co{'parents'}};$i++) {6239if($co{'parents'}[$i]eq$hash_parent) {6240$formats_nav.=' parent '. ($i+1);6241last;6242}6243}6244$formats_nav.=': '.6245$cgi->a({-href => href(action=>"commitdiff",6246 hash=>$hash_parent)},6247 esc_html($hash_parent_short)) .6248')';6249}elsif(!$co{'parent'}) {6250# --root commitdiff6251$formats_nav.=' (initial)';6252}elsif(scalar@{$co{'parents'}} ==1) {6253# single parent commit6254$formats_nav.=6255' (parent: '.6256$cgi->a({-href => href(action=>"commitdiff",6257 hash=>$co{'parent'})},6258 esc_html(substr($co{'parent'},0,7))) .6259')';6260}else{6261# merge commit6262if($hash_parenteq'--cc') {6263$formats_nav.=' | '.6264$cgi->a({-href => href(action=>"commitdiff",6265 hash=>$hash, hash_parent=>'-c')},6266'combined');6267}else{# $hash_parent eq '-c'6268$formats_nav.=' | '.6269$cgi->a({-href => href(action=>"commitdiff",6270 hash=>$hash, hash_parent=>'--cc')},6271'compact');6272}6273$formats_nav.=6274' (merge: '.6275join(' ',map{6276$cgi->a({-href => href(action=>"commitdiff",6277 hash=>$_)},6278 esc_html(substr($_,0,7)));6279} @{$co{'parents'}} ) .6280')';6281}6282}62836284my$hash_parent_param=$hash_parent;6285if(!defined$hash_parent_param) {6286# --cc for multiple parents, --root for parentless6287$hash_parent_param=6288@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6289}62906291# read commitdiff6292my$fd;6293my@difftree;6294if($formateq'html') {6295open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6296"--no-commit-id","--patch-with-raw","--full-index",6297$hash_parent_param,$hash,"--"6298or die_error(500,"Open git-diff-tree failed");62996300while(my$line= <$fd>) {6301chomp$line;6302# empty line ends raw part of diff-tree output6303last unless$line;6304push@difftree,scalar parse_difftree_raw_line($line);6305}63066307}elsif($formateq'plain') {6308open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6309'-p',$hash_parent_param,$hash,"--"6310or die_error(500,"Open git-diff-tree failed");6311}elsif($formateq'patch') {6312# For commit ranges, we limit the output to the number of6313# patches specified in the 'patches' feature.6314# For single commits, we limit the output to a single patch,6315# diverging from the git-format-patch default.6316my@commit_spec= ();6317if($hash_parent) {6318if($patch_max>0) {6319push@commit_spec,"-$patch_max";6320}6321push@commit_spec,'-n',"$hash_parent..$hash";6322}else{6323if($params{-single}) {6324push@commit_spec,'-1';6325}else{6326if($patch_max>0) {6327push@commit_spec,"-$patch_max";6328}6329push@commit_spec,"-n";6330}6331push@commit_spec,'--root',$hash;6332}6333open$fd,"-|", git_cmd(),"format-patch",@diff_opts,6334'--encoding=utf8','--stdout',@commit_spec6335or die_error(500,"Open git-format-patch failed");6336}else{6337 die_error(400,"Unknown commitdiff format");6338}63396340# non-textual hash id's can be cached6341my$expires;6342if($hash=~m/^[0-9a-fA-F]{40}$/) {6343$expires="+1d";6344}63456346# write commit message6347if($formateq'html') {6348my$refs= git_get_references();6349my$ref= format_ref_marker($refs,$co{'id'});63506351 git_header_html(undef,$expires);6352 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6353 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6354print"<div class=\"title_text\">\n".6355"<table class=\"object_header\">\n";6356 git_print_authorship_rows(\%co);6357print"</table>".6358"</div>\n";6359print"<div class=\"page_body\">\n";6360if(@{$co{'comment'}} >1) {6361print"<div class=\"log\">\n";6362 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6363print"</div>\n";# class="log"6364}63656366}elsif($formateq'plain') {6367my$refs= git_get_references("tags");6368my$tagname= git_get_rev_name_tags($hash);6369my$filename= basename($project) ."-$hash.patch";63706371print$cgi->header(6372-type =>'text/plain',6373-charset =>'utf-8',6374-expires =>$expires,6375-content_disposition =>'inline; filename="'."$filename".'"');6376my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6377print"From: ". to_utf8($co{'author'}) ."\n";6378print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6379print"Subject: ". to_utf8($co{'title'}) ."\n";63806381print"X-Git-Tag:$tagname\n"if$tagname;6382print"X-Git-Url: ".$cgi->self_url() ."\n\n";63836384foreachmy$line(@{$co{'comment'}}) {6385print to_utf8($line) ."\n";6386}6387print"---\n\n";6388}elsif($formateq'patch') {6389my$filename= basename($project) ."-$hash.patch";63906391print$cgi->header(6392-type =>'text/plain',6393-charset =>'utf-8',6394-expires =>$expires,6395-content_disposition =>'inline; filename="'."$filename".'"');6396}63976398# write patch6399if($formateq'html') {6400my$use_parents= !defined$hash_parent||6401$hash_parenteq'-c'||$hash_parenteq'--cc';6402 git_difftree_body(\@difftree,$hash,6403$use_parents? @{$co{'parents'}} :$hash_parent);6404print"<br/>\n";64056406 git_patchset_body($fd, \@difftree,$hash,6407$use_parents? @{$co{'parents'}} :$hash_parent);6408close$fd;6409print"</div>\n";# class="page_body"6410 git_footer_html();64116412}elsif($formateq'plain') {6413local$/=undef;6414print<$fd>;6415close$fd6416or print"Reading git-diff-tree failed\n";6417}elsif($formateq'patch') {6418local$/=undef;6419print<$fd>;6420close$fd6421or print"Reading git-format-patch failed\n";6422}6423}64246425sub git_commitdiff_plain {6426 git_commitdiff(-format =>'plain');6427}64286429# format-patch-style patches6430sub git_patch {6431 git_commitdiff(-format =>'patch', -single =>1);6432}64336434sub git_patches {6435 git_commitdiff(-format =>'patch');6436}64376438sub git_history {6439 git_log_generic('history', \&git_history_body,6440$hash_base,$hash_parent_base,6441$file_name,$hash);6442}64436444sub git_search {6445 gitweb_check_feature('search')or die_error(403,"Search is disabled");6446if(!defined$searchtext) {6447 die_error(400,"Text field is empty");6448}6449if(!defined$hash) {6450$hash= git_get_head_hash($project);6451}6452my%co= parse_commit($hash);6453if(!%co) {6454 die_error(404,"Unknown commit object");6455}6456if(!defined$page) {6457$page=0;6458}64596460$searchtype||='commit';6461if($searchtypeeq'pickaxe') {6462# pickaxe may take all resources of your box and run for several minutes6463# with every query - so decide by yourself how public you make this feature6464 gitweb_check_feature('pickaxe')6465or die_error(403,"Pickaxe is disabled");6466}6467if($searchtypeeq'grep') {6468 gitweb_check_feature('grep')6469or die_error(403,"Grep is disabled");6470}64716472 git_header_html();64736474if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6475my$greptype;6476if($searchtypeeq'commit') {6477$greptype="--grep=";6478}elsif($searchtypeeq'author') {6479$greptype="--author=";6480}elsif($searchtypeeq'committer') {6481$greptype="--committer=";6482}6483$greptype.=$searchtext;6484my@commitlist= parse_commits($hash,101, (100*$page),undef,6485$greptype,'--regexp-ignore-case',6486$search_use_regexp?'--extended-regexp':'--fixed-strings');64876488my$paging_nav='';6489if($page>0) {6490$paging_nav.=6491$cgi->a({-href => href(action=>"search", hash=>$hash,6492 searchtext=>$searchtext,6493 searchtype=>$searchtype)},6494"first");6495$paging_nav.=" ⋅ ".6496$cgi->a({-href => href(-replay=>1, page=>$page-1),6497-accesskey =>"p", -title =>"Alt-p"},"prev");6498}else{6499$paging_nav.="first";6500$paging_nav.=" ⋅ prev";6501}6502my$next_link='';6503if($#commitlist>=100) {6504$next_link=6505$cgi->a({-href => href(-replay=>1, page=>$page+1),6506-accesskey =>"n", -title =>"Alt-n"},"next");6507$paging_nav.=" ⋅$next_link";6508}else{6509$paging_nav.=" ⋅ next";6510}65116512if($#commitlist>=100) {6513}65146515 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6516 git_print_header_div('commit', esc_html($co{'title'}),$hash);6517 git_search_grep_body(\@commitlist,0,99,$next_link);6518}65196520if($searchtypeeq'pickaxe') {6521 git_print_page_nav('','',$hash,$co{'tree'},$hash);6522 git_print_header_div('commit', esc_html($co{'title'}),$hash);65236524print"<table class=\"pickaxe search\">\n";6525my$alternate=1;6526local$/="\n";6527open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6528'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6529($search_use_regexp?'--pickaxe-regex': ());6530undef%co;6531my@files;6532while(my$line= <$fd>) {6533chomp$line;6534next unless$line;65356536my%set= parse_difftree_raw_line($line);6537if(defined$set{'commit'}) {6538# finish previous commit6539if(%co) {6540print"</td>\n".6541"<td class=\"link\">".6542$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6543" | ".6544$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6545print"</td>\n".6546"</tr>\n";6547}65486549if($alternate) {6550print"<tr class=\"dark\">\n";6551}else{6552print"<tr class=\"light\">\n";6553}6554$alternate^=1;6555%co= parse_commit($set{'commit'});6556my$author= chop_and_escape_str($co{'author_name'},15,5);6557print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6558"<td><i>$author</i></td>\n".6559"<td>".6560$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6561-class=>"list subject"},6562 chop_and_escape_str($co{'title'},50) ."<br/>");6563}elsif(defined$set{'to_id'}) {6564next if($set{'to_id'} =~m/^0{40}$/);65656566print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6567 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6568-class=>"list"},6569"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6570"<br/>\n";6571}6572}6573close$fd;65746575# finish last commit (warning: repetition!)6576if(%co) {6577print"</td>\n".6578"<td class=\"link\">".6579$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6580" | ".6581$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6582print"</td>\n".6583"</tr>\n";6584}65856586print"</table>\n";6587}65886589if($searchtypeeq'grep') {6590 git_print_page_nav('','',$hash,$co{'tree'},$hash);6591 git_print_header_div('commit', esc_html($co{'title'}),$hash);65926593print"<table class=\"grep_search\">\n";6594my$alternate=1;6595my$matches=0;6596local$/="\n";6597open my$fd,"-|", git_cmd(),'grep','-n',6598$search_use_regexp? ('-E','-i') :'-F',6599$searchtext,$co{'tree'};6600my$lastfile='';6601while(my$line= <$fd>) {6602chomp$line;6603my($file,$lno,$ltext,$binary);6604last if($matches++>1000);6605if($line=~/^Binary file (.+) matches$/) {6606$file=$1;6607$binary=1;6608}else{6609(undef,$file,$lno,$ltext) =split(/:/,$line,4);6610}6611if($filene$lastfile) {6612$lastfileand print"</td></tr>\n";6613if($alternate++) {6614print"<tr class=\"dark\">\n";6615}else{6616print"<tr class=\"light\">\n";6617}6618print"<td class=\"list\">".6619$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6620 file_name=>"$file"),6621-class=>"list"}, esc_path($file));6622print"</td><td>\n";6623$lastfile=$file;6624}6625if($binary) {6626print"<div class=\"binary\">Binary file</div>\n";6627}else{6628$ltext= untabify($ltext);6629if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6630$ltext= esc_html($1, -nbsp=>1);6631$ltext.='<span class="match">';6632$ltext.= esc_html($2, -nbsp=>1);6633$ltext.='</span>';6634$ltext.= esc_html($3, -nbsp=>1);6635}else{6636$ltext= esc_html($ltext, -nbsp=>1);6637}6638print"<div class=\"pre\">".6639$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6640 file_name=>"$file").'#l'.$lno,6641-class=>"linenr"},sprintf('%4i',$lno))6642.' '.$ltext."</div>\n";6643}6644}6645if($lastfile) {6646print"</td></tr>\n";6647if($matches>1000) {6648print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6649}6650}else{6651print"<div class=\"diff nodifferences\">No matches found</div>\n";6652}6653close$fd;66546655print"</table>\n";6656}6657 git_footer_html();6658}66596660sub git_search_help {6661 git_header_html();6662 git_print_page_nav('','',$hash,$hash,$hash);6663print<<EOT;6664<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6665regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6666the pattern entered is recognized as the POSIX extended6667<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6668insensitive).</p>6669<dl>6670<dt><b>commit</b></dt>6671<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6672EOT6673my$have_grep= gitweb_check_feature('grep');6674if($have_grep) {6675print<<EOT;6676<dt><b>grep</b></dt>6677<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6678 a different one) are searched for the given pattern. On large trees, this search can take6679a while and put some strain on the server, so please use it with some consideration. Note that6680due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6681case-sensitive.</dd>6682EOT6683}6684print<<EOT;6685<dt><b>author</b></dt>6686<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6687<dt><b>committer</b></dt>6688<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6689EOT6690my$have_pickaxe= gitweb_check_feature('pickaxe');6691if($have_pickaxe) {6692print<<EOT;6693<dt><b>pickaxe</b></dt>6694<dd>All commits that caused the string to appear or disappear from any file (changes that6695added, removed or "modified" the string) will be listed. This search can take a while and6696takes a lot of strain on the server, so please use it wisely. Note that since you may be6697interested even in changes just changing the case as well, this search is case sensitive.</dd>6698EOT6699}6700print"</dl>\n";6701 git_footer_html();6702}67036704sub git_shortlog {6705 git_log_generic('shortlog', \&git_shortlog_body,6706$hash,$hash_parent);6707}67086709## ......................................................................6710## feeds (RSS, Atom; OPML)67116712sub git_feed {6713my$format=shift||'atom';6714my$have_blame= gitweb_check_feature('blame');67156716# Atom: http://www.atomenabled.org/developers/syndication/6717# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6718if($formatne'rss'&&$formatne'atom') {6719 die_error(400,"Unknown web feed format");6720}67216722# log/feed of current (HEAD) branch, log of given branch, history of file/directory6723my$head=$hash||'HEAD';6724my@commitlist= parse_commits($head,150,0,$file_name);67256726my%latest_commit;6727my%latest_date;6728my$content_type="application/$format+xml";6729if(defined$cgi->http('HTTP_ACCEPT') &&6730$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6731# browser (feed reader) prefers text/xml6732$content_type='text/xml';6733}6734if(defined($commitlist[0])) {6735%latest_commit= %{$commitlist[0]};6736my$latest_epoch=$latest_commit{'committer_epoch'};6737%latest_date= parse_date($latest_epoch);6738my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6739if(defined$if_modified) {6740my$since;6741if(eval{require HTTP::Date;1; }) {6742$since= HTTP::Date::str2time($if_modified);6743}elsif(eval{require Time::ParseDate;1; }) {6744$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6745}6746if(defined$since&&$latest_epoch<=$since) {6747print$cgi->header(6748-type =>$content_type,6749-charset =>'utf-8',6750-last_modified =>$latest_date{'rfc2822'},6751-status =>'304 Not Modified');6752return;6753}6754}6755print$cgi->header(6756-type =>$content_type,6757-charset =>'utf-8',6758-last_modified =>$latest_date{'rfc2822'});6759}else{6760print$cgi->header(6761-type =>$content_type,6762-charset =>'utf-8');6763}67646765# Optimization: skip generating the body if client asks only6766# for Last-Modified date.6767return if($cgi->request_method()eq'HEAD');67686769# header variables6770my$title="$site_name-$project/$action";6771my$feed_type='log';6772if(defined$hash) {6773$title.=" - '$hash'";6774$feed_type='branch log';6775if(defined$file_name) {6776$title.=" ::$file_name";6777$feed_type='history';6778}6779}elsif(defined$file_name) {6780$title.=" -$file_name";6781$feed_type='history';6782}6783$title.="$feed_type";6784my$descr= git_get_project_description($project);6785if(defined$descr) {6786$descr= esc_html($descr);6787}else{6788$descr="$project".6789($formateq'rss'?'RSS':'Atom') .6790" feed";6791}6792my$owner= git_get_project_owner($project);6793$owner= esc_html($owner);67946795#header6796my$alt_url;6797if(defined$file_name) {6798$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6799}elsif(defined$hash) {6800$alt_url= href(-full=>1, action=>"log", hash=>$hash);6801}else{6802$alt_url= href(-full=>1, action=>"summary");6803}6804print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6805if($formateq'rss') {6806print<<XML;6807<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6808<channel>6809XML6810print"<title>$title</title>\n".6811"<link>$alt_url</link>\n".6812"<description>$descr</description>\n".6813"<language>en</language>\n".6814# project owner is responsible for 'editorial' content6815"<managingEditor>$owner</managingEditor>\n";6816if(defined$logo||defined$favicon) {6817# prefer the logo to the favicon, since RSS6818# doesn't allow both6819my$img= esc_url($logo||$favicon);6820print"<image>\n".6821"<url>$img</url>\n".6822"<title>$title</title>\n".6823"<link>$alt_url</link>\n".6824"</image>\n";6825}6826if(%latest_date) {6827print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6828print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6829}6830print"<generator>gitweb v.$version/$git_version</generator>\n";6831}elsif($formateq'atom') {6832print<<XML;6833<feed xmlns="http://www.w3.org/2005/Atom">6834XML6835print"<title>$title</title>\n".6836"<subtitle>$descr</subtitle>\n".6837'<link rel="alternate" type="text/html" href="'.6838$alt_url.'" />'."\n".6839'<link rel="self" type="'.$content_type.'" href="'.6840$cgi->self_url() .'" />'."\n".6841"<id>". href(-full=>1) ."</id>\n".6842# use project owner for feed author6843"<author><name>$owner</name></author>\n";6844if(defined$favicon) {6845print"<icon>". esc_url($favicon) ."</icon>\n";6846}6847if(defined$logo_url) {6848# not twice as wide as tall: 72 x 27 pixels6849print"<logo>". esc_url($logo) ."</logo>\n";6850}6851if(!%latest_date) {6852# dummy date to keep the feed valid until commits trickle in:6853print"<updated>1970-01-01T00:00:00Z</updated>\n";6854}else{6855print"<updated>$latest_date{'iso-8601'}</updated>\n";6856}6857print"<generator version='$version/$git_version'>gitweb</generator>\n";6858}68596860# contents6861for(my$i=0;$i<=$#commitlist;$i++) {6862my%co= %{$commitlist[$i]};6863my$commit=$co{'id'};6864# we read 150, we always show 30 and the ones more recent than 48 hours6865if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6866last;6867}6868my%cd= parse_date($co{'author_epoch'});68696870# get list of changed files6871open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6872$co{'parent'} ||"--root",6873$co{'id'},"--", (defined$file_name?$file_name: ())6874ornext;6875my@difftree=map{chomp;$_} <$fd>;6876close$fd6877ornext;68786879# print element (entry, item)6880my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6881if($formateq'rss') {6882print"<item>\n".6883"<title>". esc_html($co{'title'}) ."</title>\n".6884"<author>". esc_html($co{'author'}) ."</author>\n".6885"<pubDate>$cd{'rfc2822'}</pubDate>\n".6886"<guid isPermaLink=\"true\">$co_url</guid>\n".6887"<link>$co_url</link>\n".6888"<description>". esc_html($co{'title'}) ."</description>\n".6889"<content:encoded>".6890"<![CDATA[\n";6891}elsif($formateq'atom') {6892print"<entry>\n".6893"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6894"<updated>$cd{'iso-8601'}</updated>\n".6895"<author>\n".6896" <name>". esc_html($co{'author_name'}) ."</name>\n";6897if($co{'author_email'}) {6898print" <email>". esc_html($co{'author_email'}) ."</email>\n";6899}6900print"</author>\n".6901# use committer for contributor6902"<contributor>\n".6903" <name>". esc_html($co{'committer_name'}) ."</name>\n";6904if($co{'committer_email'}) {6905print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6906}6907print"</contributor>\n".6908"<published>$cd{'iso-8601'}</published>\n".6909"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6910"<id>$co_url</id>\n".6911"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6912"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6913}6914my$comment=$co{'comment'};6915print"<pre>\n";6916foreachmy$line(@$comment) {6917$line= esc_html($line);6918print"$line\n";6919}6920print"</pre><ul>\n";6921foreachmy$difftree_line(@difftree) {6922my%difftree= parse_difftree_raw_line($difftree_line);6923next if!$difftree{'from_id'};69246925my$file=$difftree{'file'} ||$difftree{'to_file'};69266927print"<li>".6928"[".6929$cgi->a({-href => href(-full=>1, action=>"blobdiff",6930 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6931 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6932 file_name=>$file, file_parent=>$difftree{'from_file'}),6933-title =>"diff"},'D');6934if($have_blame) {6935print$cgi->a({-href => href(-full=>1, action=>"blame",6936 file_name=>$file, hash_base=>$commit),6937-title =>"blame"},'B');6938}6939# if this is not a feed of a file history6940if(!defined$file_name||$file_namene$file) {6941print$cgi->a({-href => href(-full=>1, action=>"history",6942 file_name=>$file, hash=>$commit),6943-title =>"history"},'H');6944}6945$file= esc_path($file);6946print"] ".6947"$file</li>\n";6948}6949if($formateq'rss') {6950print"</ul>]]>\n".6951"</content:encoded>\n".6952"</item>\n";6953}elsif($formateq'atom') {6954print"</ul>\n</div>\n".6955"</content>\n".6956"</entry>\n";6957}6958}69596960# end of feed6961if($formateq'rss') {6962print"</channel>\n</rss>\n";6963}elsif($formateq'atom') {6964print"</feed>\n";6965}6966}69676968sub git_rss {6969 git_feed('rss');6970}69716972sub git_atom {6973 git_feed('atom');6974}69756976sub git_opml {6977my@list= git_get_projects_list();69786979print$cgi->header(6980-type =>'text/xml',6981-charset =>'utf-8',6982-content_disposition =>'inline; filename="opml.xml"');69836984print<<XML;6985<?xml version="1.0" encoding="utf-8"?>6986<opml version="1.0">6987<head>6988 <title>$site_nameOPML Export</title>6989</head>6990<body>6991<outline text="git RSS feeds">6992XML69936994foreachmy$pr(@list) {6995my%proj=%$pr;6996my$head= git_get_head_hash($proj{'path'});6997if(!defined$head) {6998next;6999}7000$git_dir="$projectroot/$proj{'path'}";7001my%co= parse_commit($head);7002if(!%co) {7003next;7004}70057006my$path= esc_html(chop_str($proj{'path'},25,5));7007my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);7008my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);7009print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";7010}7011print<<XML;7012</outline>7013</body>7014</opml>7015XML7016}