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 reset_timer {1031our$t0= [Time::HiRes::gettimeofday()]1032ifdefined$t0;1033our$number_of_git_cmds=0;1034}10351036sub run_request {1037 reset_timer();10381039 evaluate_uri();1040 check_loadavg();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();1089 evaluate_gitweb_config();1090 evaluate_git_version();10911092# $projectroot and $projects_list might be set in gitweb config file1093$projects_list||=$projectroot;10941095$pre_listen_hook->()1096if$pre_listen_hook;10971098 REQUEST:1099while($cgi=$CGI->new()) {1100$pre_dispatch_hook->()1101if$pre_dispatch_hook;11021103 run_request();11041105$pre_dispatch_hook->()1106if$post_dispatch_hook;11071108last REQUEST if($is_last_request->());1109}11101111 DONE_GITWEB:11121;1113}11141115run();11161117if(defined caller) {1118# wrapped in a subroutine processing requests,1119# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1120return;1121}else{1122# pure CGI script, serving single request1123exit;1124}11251126## ======================================================================1127## action links11281129# possible values of extra options1130# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1131# -replay => 1 - start from a current view (replay with modifications)1132# -path_info => 0|1 - don't use/use path_info URL (if possible)1133sub href {1134my%params=@_;1135# default is to use -absolute url() i.e. $my_uri1136my$href=$params{-full} ?$my_url:$my_uri;11371138$params{'project'} =$projectunlessexists$params{'project'};11391140if($params{-replay}) {1141while(my($name,$symbol) =each%cgi_param_mapping) {1142if(!exists$params{$name}) {1143$params{$name} =$input_params{$name};1144}1145}1146}11471148my$use_pathinfo= gitweb_check_feature('pathinfo');1149if(defined$params{'project'} &&1150(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1151# try to put as many parameters as possible in PATH_INFO:1152# - project name1153# - action1154# - hash_parent or hash_parent_base:/file_parent1155# - hash or hash_base:/filename1156# - the snapshot_format as an appropriate suffix11571158# When the script is the root DirectoryIndex for the domain,1159# $href here would be something like http://gitweb.example.com/1160# Thus, we strip any trailing / from $href, to spare us double1161# slashes in the final URL1162$href=~ s,/$,,;11631164# Then add the project name, if present1165$href.="/".esc_url($params{'project'});1166delete$params{'project'};11671168# since we destructively absorb parameters, we keep this1169# boolean that remembers if we're handling a snapshot1170my$is_snapshot=$params{'action'}eq'snapshot';11711172# Summary just uses the project path URL, any other action is1173# added to the URL1174if(defined$params{'action'}) {1175$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1176delete$params{'action'};1177}11781179# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1180# stripping nonexistent or useless pieces1181$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1182||$params{'hash_parent'} ||$params{'hash'});1183if(defined$params{'hash_base'}) {1184if(defined$params{'hash_parent_base'}) {1185$href.= esc_url($params{'hash_parent_base'});1186# skip the file_parent if it's the same as the file_name1187if(defined$params{'file_parent'}) {1188if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1189delete$params{'file_parent'};1190}elsif($params{'file_parent'} !~/\.\./) {1191$href.=":/".esc_url($params{'file_parent'});1192delete$params{'file_parent'};1193}1194}1195$href.="..";1196delete$params{'hash_parent'};1197delete$params{'hash_parent_base'};1198}elsif(defined$params{'hash_parent'}) {1199$href.= esc_url($params{'hash_parent'})."..";1200delete$params{'hash_parent'};1201}12021203$href.= esc_url($params{'hash_base'});1204if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1205$href.=":/".esc_url($params{'file_name'});1206delete$params{'file_name'};1207}1208delete$params{'hash'};1209delete$params{'hash_base'};1210}elsif(defined$params{'hash'}) {1211$href.= esc_url($params{'hash'});1212delete$params{'hash'};1213}12141215# If the action was a snapshot, we can absorb the1216# snapshot_format parameter too1217if($is_snapshot) {1218my$fmt=$params{'snapshot_format'};1219# snapshot_format should always be defined when href()1220# is called, but just in case some code forgets, we1221# fall back to the default1222$fmt||=$snapshot_fmts[0];1223$href.=$known_snapshot_formats{$fmt}{'suffix'};1224delete$params{'snapshot_format'};1225}1226}12271228# now encode the parameters explicitly1229my@result= ();1230for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1231my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1232if(defined$params{$name}) {1233if(ref($params{$name})eq"ARRAY") {1234foreachmy$par(@{$params{$name}}) {1235push@result,$symbol."=". esc_param($par);1236}1237}else{1238push@result,$symbol."=". esc_param($params{$name});1239}1240}1241}1242$href.="?".join(';',@result)ifscalar@result;12431244return$href;1245}124612471248## ======================================================================1249## validation, quoting/unquoting and escaping12501251sub validate_action {1252my$input=shift||returnundef;1253returnundefunlessexists$actions{$input};1254return$input;1255}12561257sub validate_project {1258my$input=shift||returnundef;1259if(!validate_pathname($input) ||1260!(-d "$projectroot/$input") ||1261!check_export_ok("$projectroot/$input") ||1262($strict_export&& !project_in_list($input))) {1263returnundef;1264}else{1265return$input;1266}1267}12681269sub validate_pathname {1270my$input=shift||returnundef;12711272# no '.' or '..' as elements of path, i.e. no '.' nor '..'1273# at the beginning, at the end, and between slashes.1274# also this catches doubled slashes1275if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1276returnundef;1277}1278# no null characters1279if($input=~m!\0!) {1280returnundef;1281}1282return$input;1283}12841285sub validate_refname {1286my$input=shift||returnundef;12871288# textual hashes are O.K.1289if($input=~m/^[0-9a-fA-F]{40}$/) {1290return$input;1291}1292# it must be correct pathname1293$input= validate_pathname($input)1294orreturnundef;1295# restrictions on ref name according to git-check-ref-format1296if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1297returnundef;1298}1299return$input;1300}13011302# decode sequences of octets in utf8 into Perl's internal form,1303# which is utf-8 with utf8 flag set if needed. gitweb writes out1304# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1305sub to_utf8 {1306my$str=shift;1307returnundefunlessdefined$str;1308if(utf8::valid($str)) {1309 utf8::decode($str);1310return$str;1311}else{1312return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1313}1314}13151316# quote unsafe chars, but keep the slash, even when it's not1317# correct, but quoted slashes look too horrible in bookmarks1318sub esc_param {1319my$str=shift;1320returnundefunlessdefined$str;1321$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1322$str=~s/ /\+/g;1323return$str;1324}13251326# quote unsafe chars in whole URL, so some charactrs cannot be quoted1327sub esc_url {1328my$str=shift;1329returnundefunlessdefined$str;1330$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1331$str=~s/\+/%2B/g;1332$str=~s/ /\+/g;1333return$str;1334}13351336# replace invalid utf8 character with SUBSTITUTION sequence1337sub esc_html {1338my$str=shift;1339my%opts=@_;13401341returnundefunlessdefined$str;13421343$str= to_utf8($str);1344$str=$cgi->escapeHTML($str);1345if($opts{'-nbsp'}) {1346$str=~s/ / /g;1347}1348$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1349return$str;1350}13511352# quote control characters and escape filename to HTML1353sub esc_path {1354my$str=shift;1355my%opts=@_;13561357returnundefunlessdefined$str;13581359$str= to_utf8($str);1360$str=$cgi->escapeHTML($str);1361if($opts{'-nbsp'}) {1362$str=~s/ / /g;1363}1364$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1365return$str;1366}13671368# Make control characters "printable", using character escape codes (CEC)1369sub quot_cec {1370my$cntrl=shift;1371my%opts=@_;1372my%es= (# character escape codes, aka escape sequences1373"\t"=>'\t',# tab (HT)1374"\n"=>'\n',# line feed (LF)1375"\r"=>'\r',# carrige return (CR)1376"\f"=>'\f',# form feed (FF)1377"\b"=>'\b',# backspace (BS)1378"\a"=>'\a',# alarm (bell) (BEL)1379"\e"=>'\e',# escape (ESC)1380"\013"=>'\v',# vertical tab (VT)1381"\000"=>'\0',# nul character (NUL)1382);1383my$chr= ( (exists$es{$cntrl})1384?$es{$cntrl}1385:sprintf('\%2x',ord($cntrl)) );1386if($opts{-nohtml}) {1387return$chr;1388}else{1389return"<span class=\"cntrl\">$chr</span>";1390}1391}13921393# Alternatively use unicode control pictures codepoints,1394# Unicode "printable representation" (PR)1395sub quot_upr {1396my$cntrl=shift;1397my%opts=@_;13981399my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1400if($opts{-nohtml}) {1401return$chr;1402}else{1403return"<span class=\"cntrl\">$chr</span>";1404}1405}14061407# git may return quoted and escaped filenames1408sub unquote {1409my$str=shift;14101411sub unq {1412my$seq=shift;1413my%es= (# character escape codes, aka escape sequences1414't'=>"\t",# tab (HT, TAB)1415'n'=>"\n",# newline (NL)1416'r'=>"\r",# return (CR)1417'f'=>"\f",# form feed (FF)1418'b'=>"\b",# backspace (BS)1419'a'=>"\a",# alarm (bell) (BEL)1420'e'=>"\e",# escape (ESC)1421'v'=>"\013",# vertical tab (VT)1422);14231424if($seq=~m/^[0-7]{1,3}$/) {1425# octal char sequence1426returnchr(oct($seq));1427}elsif(exists$es{$seq}) {1428# C escape sequence, aka character escape code1429return$es{$seq};1430}1431# quoted ordinary character1432return$seq;1433}14341435if($str=~m/^"(.*)"$/) {1436# needs unquoting1437$str=$1;1438$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1439}1440return$str;1441}14421443# escape tabs (convert tabs to spaces)1444sub untabify {1445my$line=shift;14461447while((my$pos=index($line,"\t")) != -1) {1448if(my$count= (8- ($pos%8))) {1449my$spaces=' ' x $count;1450$line=~s/\t/$spaces/;1451}1452}14531454return$line;1455}14561457sub project_in_list {1458my$project=shift;1459my@list= git_get_projects_list();1460return@list&&scalar(grep{$_->{'path'}eq$project}@list);1461}14621463## ----------------------------------------------------------------------1464## HTML aware string manipulation14651466# Try to chop given string on a word boundary between position1467# $len and $len+$add_len. If there is no word boundary there,1468# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1469# (marking chopped part) would be longer than given string.1470sub chop_str {1471my$str=shift;1472my$len=shift;1473my$add_len=shift||10;1474my$where=shift||'right';# 'left' | 'center' | 'right'14751476# Make sure perl knows it is utf8 encoded so we don't1477# cut in the middle of a utf8 multibyte char.1478$str= to_utf8($str);14791480# allow only $len chars, but don't cut a word if it would fit in $add_len1481# if it doesn't fit, cut it if it's still longer than the dots we would add1482# remove chopped character entities entirely14831484# when chopping in the middle, distribute $len into left and right part1485# return early if chopping wouldn't make string shorter1486if($whereeq'center') {1487return$strif($len+5>=length($str));# filler is length 51488$len=int($len/2);1489}else{1490return$strif($len+4>=length($str));# filler is length 41491}14921493# regexps: ending and beginning with word part up to $add_len1494my$endre=qr/.{$len}\w{0,$add_len}/;1495my$begre=qr/\w{0,$add_len}.{$len}/;14961497if($whereeq'left') {1498$str=~m/^(.*?)($begre)$/;1499my($lead,$body) = ($1,$2);1500if(length($lead) >4) {1501$lead=" ...";1502}1503return"$lead$body";15041505}elsif($whereeq'center') {1506$str=~m/^($endre)(.*)$/;1507my($left,$str) = ($1,$2);1508$str=~m/^(.*?)($begre)$/;1509my($mid,$right) = ($1,$2);1510if(length($mid) >5) {1511$mid=" ... ";1512}1513return"$left$mid$right";15141515}else{1516$str=~m/^($endre)(.*)$/;1517my$body=$1;1518my$tail=$2;1519if(length($tail) >4) {1520$tail="... ";1521}1522return"$body$tail";1523}1524}15251526# takes the same arguments as chop_str, but also wraps a <span> around the1527# result with a title attribute if it does get chopped. Additionally, the1528# string is HTML-escaped.1529sub chop_and_escape_str {1530my($str) =@_;15311532my$chopped= chop_str(@_);1533if($choppedeq$str) {1534return esc_html($chopped);1535}else{1536$str=~s/[[:cntrl:]]/?/g;1537return$cgi->span({-title=>$str}, esc_html($chopped));1538}1539}15401541## ----------------------------------------------------------------------1542## functions returning short strings15431544# CSS class for given age value (in seconds)1545sub age_class {1546my$age=shift;15471548if(!defined$age) {1549return"noage";1550}elsif($age<60*60*2) {1551return"age0";1552}elsif($age<60*60*24*2) {1553return"age1";1554}else{1555return"age2";1556}1557}15581559# convert age in seconds to "nn units ago" string1560sub age_string {1561my$age=shift;1562my$age_str;15631564if($age>60*60*24*365*2) {1565$age_str= (int$age/60/60/24/365);1566$age_str.=" years ago";1567}elsif($age>60*60*24*(365/12)*2) {1568$age_str=int$age/60/60/24/(365/12);1569$age_str.=" months ago";1570}elsif($age>60*60*24*7*2) {1571$age_str=int$age/60/60/24/7;1572$age_str.=" weeks ago";1573}elsif($age>60*60*24*2) {1574$age_str=int$age/60/60/24;1575$age_str.=" days ago";1576}elsif($age>60*60*2) {1577$age_str=int$age/60/60;1578$age_str.=" hours ago";1579}elsif($age>60*2) {1580$age_str=int$age/60;1581$age_str.=" min ago";1582}elsif($age>2) {1583$age_str=int$age;1584$age_str.=" sec ago";1585}else{1586$age_str.=" right now";1587}1588return$age_str;1589}15901591useconstant{1592 S_IFINVALID =>0030000,1593 S_IFGITLINK =>0160000,1594};15951596# submodule/subproject, a commit object reference1597sub S_ISGITLINK {1598my$mode=shift;15991600return(($mode& S_IFMT) == S_IFGITLINK)1601}16021603# convert file mode in octal to symbolic file mode string1604sub mode_str {1605my$mode=oct shift;16061607if(S_ISGITLINK($mode)) {1608return'm---------';1609}elsif(S_ISDIR($mode& S_IFMT)) {1610return'drwxr-xr-x';1611}elsif(S_ISLNK($mode)) {1612return'lrwxrwxrwx';1613}elsif(S_ISREG($mode)) {1614# git cares only about the executable bit1615if($mode& S_IXUSR) {1616return'-rwxr-xr-x';1617}else{1618return'-rw-r--r--';1619};1620}else{1621return'----------';1622}1623}16241625# convert file mode in octal to file type string1626sub file_type {1627my$mode=shift;16281629if($mode!~m/^[0-7]+$/) {1630return$mode;1631}else{1632$mode=oct$mode;1633}16341635if(S_ISGITLINK($mode)) {1636return"submodule";1637}elsif(S_ISDIR($mode& S_IFMT)) {1638return"directory";1639}elsif(S_ISLNK($mode)) {1640return"symlink";1641}elsif(S_ISREG($mode)) {1642return"file";1643}else{1644return"unknown";1645}1646}16471648# convert file mode in octal to file type description string1649sub file_type_long {1650my$mode=shift;16511652if($mode!~m/^[0-7]+$/) {1653return$mode;1654}else{1655$mode=oct$mode;1656}16571658if(S_ISGITLINK($mode)) {1659return"submodule";1660}elsif(S_ISDIR($mode& S_IFMT)) {1661return"directory";1662}elsif(S_ISLNK($mode)) {1663return"symlink";1664}elsif(S_ISREG($mode)) {1665if($mode& S_IXUSR) {1666return"executable";1667}else{1668return"file";1669};1670}else{1671return"unknown";1672}1673}167416751676## ----------------------------------------------------------------------1677## functions returning short HTML fragments, or transforming HTML fragments1678## which don't belong to other sections16791680# format line of commit message.1681sub format_log_line_html {1682my$line=shift;16831684$line= esc_html($line, -nbsp=>1);1685$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1686$cgi->a({-href => href(action=>"object", hash=>$1),1687-class=>"text"},$1);1688}eg;16891690return$line;1691}16921693# format marker of refs pointing to given object16941695# the destination action is chosen based on object type and current context:1696# - for annotated tags, we choose the tag view unless it's the current view1697# already, in which case we go to shortlog view1698# - for other refs, we keep the current view if we're in history, shortlog or1699# log view, and select shortlog otherwise1700sub format_ref_marker {1701my($refs,$id) =@_;1702my$markers='';17031704if(defined$refs->{$id}) {1705foreachmy$ref(@{$refs->{$id}}) {1706# this code exploits the fact that non-lightweight tags are the1707# only indirect objects, and that they are the only objects for which1708# we want to use tag instead of shortlog as action1709my($type,$name) =qw();1710my$indirect= ($ref=~s/\^\{\}$//);1711# e.g. tags/v2.6.11 or heads/next1712if($ref=~m!^(.*?)s?/(.*)$!) {1713$type=$1;1714$name=$2;1715}else{1716$type="ref";1717$name=$ref;1718}17191720my$class=$type;1721$class.=" indirect"if$indirect;17221723my$dest_action="shortlog";17241725if($indirect) {1726$dest_action="tag"unless$actioneq"tag";1727}elsif($action=~/^(history|(short)?log)$/) {1728$dest_action=$action;1729}17301731my$dest="";1732$dest.="refs/"unless$ref=~ m!^refs/!;1733$dest.=$ref;17341735my$link=$cgi->a({1736-href => href(1737 action=>$dest_action,1738 hash=>$dest1739)},$name);17401741$markers.=" <span class=\"$class\"title=\"$ref\">".1742$link."</span>";1743}1744}17451746if($markers) {1747return' <span class="refs">'.$markers.'</span>';1748}else{1749return"";1750}1751}17521753# format, perhaps shortened and with markers, title line1754sub format_subject_html {1755my($long,$short,$href,$extra) =@_;1756$extra=''unlessdefined($extra);17571758if(length($short) <length($long)) {1759$long=~s/[[:cntrl:]]/?/g;1760return$cgi->a({-href =>$href, -class=>"list subject",1761-title => to_utf8($long)},1762 esc_html($short)) .$extra;1763}else{1764return$cgi->a({-href =>$href, -class=>"list subject"},1765 esc_html($long)) .$extra;1766}1767}17681769# Rather than recomputing the url for an email multiple times, we cache it1770# after the first hit. This gives a visible benefit in views where the avatar1771# for the same email is used repeatedly (e.g. shortlog).1772# The cache is shared by all avatar engines (currently gravatar only), which1773# are free to use it as preferred. Since only one avatar engine is used for any1774# given page, there's no risk for cache conflicts.1775our%avatar_cache= ();17761777# Compute the picon url for a given email, by using the picon search service over at1778# http://www.cs.indiana.edu/picons/search.html1779sub picon_url {1780my$email=lc shift;1781if(!$avatar_cache{$email}) {1782my($user,$domain) =split('@',$email);1783$avatar_cache{$email} =1784"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1785"$domain/$user/".1786"users+domains+unknown/up/single";1787}1788return$avatar_cache{$email};1789}17901791# Compute the gravatar url for a given email, if it's not in the cache already.1792# Gravatar stores only the part of the URL before the size, since that's the1793# one computationally more expensive. This also allows reuse of the cache for1794# different sizes (for this particular engine).1795sub gravatar_url {1796my$email=lc shift;1797my$size=shift;1798$avatar_cache{$email} ||=1799"http://www.gravatar.com/avatar/".1800 Digest::MD5::md5_hex($email) ."?s=";1801return$avatar_cache{$email} .$size;1802}18031804# Insert an avatar for the given $email at the given $size if the feature1805# is enabled.1806sub git_get_avatar {1807my($email,%opts) =@_;1808my$pre_white= ($opts{-pad_before} ?" ":"");1809my$post_white= ($opts{-pad_after} ?" ":"");1810$opts{-size} ||='default';1811my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1812my$url="";1813if($git_avatareq'gravatar') {1814$url= gravatar_url($email,$size);1815}elsif($git_avatareq'picon') {1816$url= picon_url($email);1817}1818# Other providers can be added by extending the if chain, defining $url1819# as needed. If no variant puts something in $url, we assume avatars1820# are completely disabled/unavailable.1821if($url) {1822return$pre_white.1823"<img width=\"$size\"".1824"class=\"avatar\"".1825"src=\"$url\"".1826"alt=\"\"".1827"/>".$post_white;1828}else{1829return"";1830}1831}18321833sub format_search_author {1834my($author,$searchtype,$displaytext) =@_;1835my$have_search= gitweb_check_feature('search');18361837if($have_search) {1838my$performed="";1839if($searchtypeeq'author') {1840$performed="authored";1841}elsif($searchtypeeq'committer') {1842$performed="committed";1843}18441845return$cgi->a({-href => href(action=>"search", hash=>$hash,1846 searchtext=>$author,1847 searchtype=>$searchtype),class=>"list",1848 title=>"Search for commits$performedby$author"},1849$displaytext);18501851}else{1852return$displaytext;1853}1854}18551856# format the author name of the given commit with the given tag1857# the author name is chopped and escaped according to the other1858# optional parameters (see chop_str).1859sub format_author_html {1860my$tag=shift;1861my$co=shift;1862my$author= chop_and_escape_str($co->{'author_name'},@_);1863return"<$tagclass=\"author\">".1864 format_search_author($co->{'author_name'},"author",1865 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1866$author) .1867"</$tag>";1868}18691870# format git diff header line, i.e. "diff --(git|combined|cc) ..."1871sub format_git_diff_header_line {1872my$line=shift;1873my$diffinfo=shift;1874my($from,$to) =@_;18751876if($diffinfo->{'nparents'}) {1877# combined diff1878$line=~s!^(diff (.*?) )"?.*$!$1!;1879if($to->{'href'}) {1880$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1881 esc_path($to->{'file'}));1882}else{# file was deleted (no href)1883$line.= esc_path($to->{'file'});1884}1885}else{1886# "ordinary" diff1887$line=~s!^(diff (.*?) )"?a/.*$!$1!;1888if($from->{'href'}) {1889$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1890'a/'. esc_path($from->{'file'}));1891}else{# file was added (no href)1892$line.='a/'. esc_path($from->{'file'});1893}1894$line.=' ';1895if($to->{'href'}) {1896$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1897'b/'. esc_path($to->{'file'}));1898}else{# file was deleted1899$line.='b/'. esc_path($to->{'file'});1900}1901}19021903return"<div class=\"diff header\">$line</div>\n";1904}19051906# format extended diff header line, before patch itself1907sub format_extended_diff_header_line {1908my$line=shift;1909my$diffinfo=shift;1910my($from,$to) =@_;19111912# match <path>1913if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1914$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1915 esc_path($from->{'file'}));1916}1917if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1918$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1919 esc_path($to->{'file'}));1920}1921# match single <mode>1922if($line=~m/\s(\d{6})$/) {1923$line.='<span class="info"> ('.1924 file_type_long($1) .1925')</span>';1926}1927# match <hash>1928if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1929# can match only for combined diff1930$line='index ';1931for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1932if($from->{'href'}[$i]) {1933$line.=$cgi->a({-href=>$from->{'href'}[$i],1934-class=>"hash"},1935substr($diffinfo->{'from_id'}[$i],0,7));1936}else{1937$line.='0' x 7;1938}1939# separator1940$line.=','if($i<$diffinfo->{'nparents'} -1);1941}1942$line.='..';1943if($to->{'href'}) {1944$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1945substr($diffinfo->{'to_id'},0,7));1946}else{1947$line.='0' x 7;1948}19491950}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1951# can match only for ordinary diff1952my($from_link,$to_link);1953if($from->{'href'}) {1954$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1955substr($diffinfo->{'from_id'},0,7));1956}else{1957$from_link='0' x 7;1958}1959if($to->{'href'}) {1960$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1961substr($diffinfo->{'to_id'},0,7));1962}else{1963$to_link='0' x 7;1964}1965my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1966$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1967}19681969return$line."<br/>\n";1970}19711972# format from-file/to-file diff header1973sub format_diff_from_to_header {1974my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1975my$line;1976my$result='';19771978$line=$from_line;1979#assert($line =~ m/^---/) if DEBUG;1980# no extra formatting for "^--- /dev/null"1981if(!$diffinfo->{'nparents'}) {1982# ordinary (single parent) diff1983if($line=~m!^--- "?a/!) {1984if($from->{'href'}) {1985$line='--- a/'.1986$cgi->a({-href=>$from->{'href'}, -class=>"path"},1987 esc_path($from->{'file'}));1988}else{1989$line='--- a/'.1990 esc_path($from->{'file'});1991}1992}1993$result.= qq!<div class="diff from_file">$line</div>\n!;19941995}else{1996# combined diff (merge commit)1997for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1998if($from->{'href'}[$i]) {1999$line='--- '.2000$cgi->a({-href=>href(action=>"blobdiff",2001 hash_parent=>$diffinfo->{'from_id'}[$i],2002 hash_parent_base=>$parents[$i],2003 file_parent=>$from->{'file'}[$i],2004 hash=>$diffinfo->{'to_id'},2005 hash_base=>$hash,2006 file_name=>$to->{'file'}),2007-class=>"path",2008-title=>"diff". ($i+1)},2009$i+1) .2010'/'.2011$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2012 esc_path($from->{'file'}[$i]));2013}else{2014$line='--- /dev/null';2015}2016$result.= qq!<div class="diff from_file">$line</div>\n!;2017}2018}20192020$line=$to_line;2021#assert($line =~ m/^\+\+\+/) if DEBUG;2022# no extra formatting for "^+++ /dev/null"2023if($line=~m!^\+\+\+ "?b/!) {2024if($to->{'href'}) {2025$line='+++ b/'.2026$cgi->a({-href=>$to->{'href'}, -class=>"path"},2027 esc_path($to->{'file'}));2028}else{2029$line='+++ b/'.2030 esc_path($to->{'file'});2031}2032}2033$result.= qq!<div class="diff to_file">$line</div>\n!;20342035return$result;2036}20372038# create note for patch simplified by combined diff2039sub format_diff_cc_simplified {2040my($diffinfo,@parents) =@_;2041my$result='';20422043$result.="<div class=\"diff header\">".2044"diff --cc ";2045if(!is_deleted($diffinfo)) {2046$result.=$cgi->a({-href => href(action=>"blob",2047 hash_base=>$hash,2048 hash=>$diffinfo->{'to_id'},2049 file_name=>$diffinfo->{'to_file'}),2050-class=>"path"},2051 esc_path($diffinfo->{'to_file'}));2052}else{2053$result.= esc_path($diffinfo->{'to_file'});2054}2055$result.="</div>\n".# class="diff header"2056"<div class=\"diff nodifferences\">".2057"Simple merge".2058"</div>\n";# class="diff nodifferences"20592060return$result;2061}20622063# format patch (diff) line (not to be used for diff headers)2064sub format_diff_line {2065my$line=shift;2066my($from,$to) =@_;2067my$diff_class="";20682069chomp$line;20702071if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2072# combined diff2073my$prefix=substr($line,0,scalar@{$from->{'href'}});2074if($line=~m/^\@{3}/) {2075$diff_class=" chunk_header";2076}elsif($line=~m/^\\/) {2077$diff_class=" incomplete";2078}elsif($prefix=~tr/+/+/) {2079$diff_class=" add";2080}elsif($prefix=~tr/-/-/) {2081$diff_class=" rem";2082}2083}else{2084# assume ordinary diff2085my$char=substr($line,0,1);2086if($chareq'+') {2087$diff_class=" add";2088}elsif($chareq'-') {2089$diff_class=" rem";2090}elsif($chareq'@') {2091$diff_class=" chunk_header";2092}elsif($chareq"\\") {2093$diff_class=" incomplete";2094}2095}2096$line= untabify($line);2097if($from&&$to&&$line=~m/^\@{2} /) {2098my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2099$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;21002101$from_lines=0unlessdefined$from_lines;2102$to_lines=0unlessdefined$to_lines;21032104if($from->{'href'}) {2105$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2106-class=>"list"},$from_text);2107}2108if($to->{'href'}) {2109$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2110-class=>"list"},$to_text);2111}2112$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2113"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2114return"<div class=\"diff$diff_class\">$line</div>\n";2115}elsif($from&&$to&&$line=~m/^\@{3}/) {2116my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2117my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);21182119@from_text=split(' ',$ranges);2120for(my$i=0;$i<@from_text; ++$i) {2121($from_start[$i],$from_nlines[$i]) =2122(split(',',substr($from_text[$i],1)),0);2123}21242125$to_text=pop@from_text;2126$to_start=pop@from_start;2127$to_nlines=pop@from_nlines;21282129$line="<span class=\"chunk_info\">$prefix";2130for(my$i=0;$i<@from_text; ++$i) {2131if($from->{'href'}[$i]) {2132$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2133-class=>"list"},$from_text[$i]);2134}else{2135$line.=$from_text[$i];2136}2137$line.=" ";2138}2139if($to->{'href'}) {2140$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2141-class=>"list"},$to_text);2142}else{2143$line.=$to_text;2144}2145$line.="$prefix</span>".2146"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2147return"<div class=\"diff$diff_class\">$line</div>\n";2148}2149return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2150}21512152# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2153# linked. Pass the hash of the tree/commit to snapshot.2154sub format_snapshot_links {2155my($hash) =@_;2156my$num_fmts=@snapshot_fmts;2157if($num_fmts>1) {2158# A parenthesized list of links bearing format names.2159# e.g. "snapshot (_tar.gz_ _zip_)"2160return"snapshot (".join(' ',map2161$cgi->a({2162-href => href(2163 action=>"snapshot",2164 hash=>$hash,2165 snapshot_format=>$_2166)2167},$known_snapshot_formats{$_}{'display'})2168,@snapshot_fmts) .")";2169}elsif($num_fmts==1) {2170# A single "snapshot" link whose tooltip bears the format name.2171# i.e. "_snapshot_"2172my($fmt) =@snapshot_fmts;2173return2174$cgi->a({2175-href => href(2176 action=>"snapshot",2177 hash=>$hash,2178 snapshot_format=>$fmt2179),2180-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2181},"snapshot");2182}else{# $num_fmts == 02183returnundef;2184}2185}21862187## ......................................................................2188## functions returning values to be passed, perhaps after some2189## transformation, to other functions; e.g. returning arguments to href()21902191# returns hash to be passed to href to generate gitweb URL2192# in -title key it returns description of link2193sub get_feed_info {2194my$format=shift||'Atom';2195my%res= (action =>lc($format));21962197# feed links are possible only for project views2198return unless(defined$project);2199# some views should link to OPML, or to generic project feed,2200# or don't have specific feed yet (so they should use generic)2201return if($action=~/^(?:tags|heads|forks|tag|search)$/x);22022203my$branch;2204# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2205# from tag links; this also makes possible to detect branch links2206if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2207(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2208$branch=$1;2209}2210# find log type for feed description (title)2211my$type='log';2212if(defined$file_name) {2213$type="history of$file_name";2214$type.="/"if($actioneq'tree');2215$type.=" on '$branch'"if(defined$branch);2216}else{2217$type="log of$branch"if(defined$branch);2218}22192220$res{-title} =$type;2221$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2222$res{'file_name'} =$file_name;22232224return%res;2225}22262227## ----------------------------------------------------------------------2228## git utility subroutines, invoking git commands22292230# returns path to the core git executable and the --git-dir parameter as list2231sub git_cmd {2232$number_of_git_cmds++;2233return$GIT,'--git-dir='.$git_dir;2234}22352236# quote the given arguments for passing them to the shell2237# quote_command("command", "arg 1", "arg with ' and ! characters")2238# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2239# Try to avoid using this function wherever possible.2240sub quote_command {2241returnjoin(' ',2242map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2243}22442245# get HEAD ref of given project as hash2246sub git_get_head_hash {2247return git_get_full_hash(shift,'HEAD');2248}22492250sub git_get_full_hash {2251return git_get_hash(@_);2252}22532254sub git_get_short_hash {2255return git_get_hash(@_,'--short=7');2256}22572258sub git_get_hash {2259my($project,$hash,@options) =@_;2260my$o_git_dir=$git_dir;2261my$retval=undef;2262$git_dir="$projectroot/$project";2263if(open my$fd,'-|', git_cmd(),'rev-parse',2264'--verify','-q',@options,$hash) {2265$retval= <$fd>;2266chomp$retvalifdefined$retval;2267close$fd;2268}2269if(defined$o_git_dir) {2270$git_dir=$o_git_dir;2271}2272return$retval;2273}22742275# get type of given object2276sub git_get_type {2277my$hash=shift;22782279open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2280my$type= <$fd>;2281close$fdorreturn;2282chomp$type;2283return$type;2284}22852286# repository configuration2287our$config_file='';2288our%config;22892290# store multiple values for single key as anonymous array reference2291# single values stored directly in the hash, not as [ <value> ]2292sub hash_set_multi {2293my($hash,$key,$value) =@_;22942295if(!exists$hash->{$key}) {2296$hash->{$key} =$value;2297}elsif(!ref$hash->{$key}) {2298$hash->{$key} = [$hash->{$key},$value];2299}else{2300push@{$hash->{$key}},$value;2301}2302}23032304# return hash of git project configuration2305# optionally limited to some section, e.g. 'gitweb'2306sub git_parse_project_config {2307my$section_regexp=shift;2308my%config;23092310local$/="\0";23112312open my$fh,"-|", git_cmd(),"config",'-z','-l',2313orreturn;23142315while(my$keyval= <$fh>) {2316chomp$keyval;2317my($key,$value) =split(/\n/,$keyval,2);23182319 hash_set_multi(\%config,$key,$value)2320if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2321}2322close$fh;23232324return%config;2325}23262327# convert config value to boolean: 'true' or 'false'2328# no value, number > 0, 'true' and 'yes' values are true2329# rest of values are treated as false (never as error)2330sub config_to_bool {2331my$val=shift;23322333return1if!defined$val;# section.key23342335# strip leading and trailing whitespace2336$val=~s/^\s+//;2337$val=~s/\s+$//;23382339return(($val=~/^\d+$/&&$val) ||# section.key = 12340($val=~/^(?:true|yes)$/i));# section.key = true2341}23422343# convert config value to simple decimal number2344# an optional value suffix of 'k', 'm', or 'g' will cause the value2345# to be multiplied by 1024, 1048576, or 10737418242346sub config_to_int {2347my$val=shift;23482349# strip leading and trailing whitespace2350$val=~s/^\s+//;2351$val=~s/\s+$//;23522353if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2354$unit=lc($unit);2355# unknown unit is treated as 12356return$num* ($uniteq'g'?1073741824:2357$uniteq'm'?1048576:2358$uniteq'k'?1024:1);2359}2360return$val;2361}23622363# convert config value to array reference, if needed2364sub config_to_multi {2365my$val=shift;23662367returnref($val) ?$val: (defined($val) ? [$val] : []);2368}23692370sub git_get_project_config {2371my($key,$type) =@_;23722373return unlessdefined$git_dir;23742375# key sanity check2376return unless($key);2377$key=~s/^gitweb\.//;2378return if($key=~m/\W/);23792380# type sanity check2381if(defined$type) {2382$type=~s/^--//;2383$type=undef2384unless($typeeq'bool'||$typeeq'int');2385}23862387# get config2388if(!defined$config_file||2389$config_filene"$git_dir/config") {2390%config= git_parse_project_config('gitweb');2391$config_file="$git_dir/config";2392}23932394# check if config variable (key) exists2395return unlessexists$config{"gitweb.$key"};23962397# ensure given type2398if(!defined$type) {2399return$config{"gitweb.$key"};2400}elsif($typeeq'bool') {2401# backward compatibility: 'git config --bool' returns true/false2402return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2403}elsif($typeeq'int') {2404return config_to_int($config{"gitweb.$key"});2405}2406return$config{"gitweb.$key"};2407}24082409# get hash of given path at given ref2410sub git_get_hash_by_path {2411my$base=shift;2412my$path=shift||returnundef;2413my$type=shift;24142415$path=~ s,/+$,,;24162417open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2418or die_error(500,"Open git-ls-tree failed");2419my$line= <$fd>;2420close$fdorreturnundef;24212422if(!defined$line) {2423# there is no tree or hash given by $path at $base2424returnundef;2425}24262427#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2428$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2429if(defined$type&&$typene$2) {2430# type doesn't match2431returnundef;2432}2433return$3;2434}24352436# get path of entry with given hash at given tree-ish (ref)2437# used to get 'from' filename for combined diff (merge commit) for renames2438sub git_get_path_by_hash {2439my$base=shift||return;2440my$hash=shift||return;24412442local$/="\0";24432444open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2445orreturnundef;2446while(my$line= <$fd>) {2447chomp$line;24482449#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2450#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2451if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2452close$fd;2453return$1;2454}2455}2456close$fd;2457returnundef;2458}24592460## ......................................................................2461## git utility functions, directly accessing git repository24622463sub git_get_project_description {2464my$path=shift;24652466$git_dir="$projectroot/$path";2467open my$fd,'<',"$git_dir/description"2468orreturn git_get_project_config('description');2469my$descr= <$fd>;2470close$fd;2471if(defined$descr) {2472chomp$descr;2473}2474return$descr;2475}24762477sub git_get_project_ctags {2478my$path=shift;2479my$ctags= {};24802481$git_dir="$projectroot/$path";2482opendir my$dh,"$git_dir/ctags"2483orreturn$ctags;2484foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2485open my$ct,'<',$_ornext;2486my$val= <$ct>;2487chomp$val;2488close$ct;2489my$ctag=$_;$ctag=~ s#.*/##;2490$ctags->{$ctag} =$val;2491}2492closedir$dh;2493$ctags;2494}24952496sub git_populate_project_tagcloud {2497my$ctags=shift;24982499# First, merge different-cased tags; tags vote on casing2500my%ctags_lc;2501foreach(keys%$ctags) {2502$ctags_lc{lc$_}->{count} +=$ctags->{$_};2503if(not$ctags_lc{lc$_}->{topcount}2504or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2505$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2506$ctags_lc{lc$_}->{topname} =$_;2507}2508}25092510my$cloud;2511if(eval{require HTML::TagCloud;1; }) {2512$cloud= HTML::TagCloud->new;2513foreach(sort keys%ctags_lc) {2514# Pad the title with spaces so that the cloud looks2515# less crammed.2516my$title=$ctags_lc{$_}->{topname};2517$title=~s/ / /g;2518$title=~s/^/ /g;2519$title=~s/$/ /g;2520$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2521}2522}else{2523$cloud= \%ctags_lc;2524}2525$cloud;2526}25272528sub git_show_project_tagcloud {2529my($cloud,$count) =@_;2530print STDERR ref($cloud)."..\n";2531if(ref$cloudeq'HTML::TagCloud') {2532return$cloud->html_and_css($count);2533}else{2534my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2535return'<p align="center">'.join(', ',map{2536"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2537}splice(@tags,0,$count)) .'</p>';2538}2539}25402541sub git_get_project_url_list {2542my$path=shift;25432544$git_dir="$projectroot/$path";2545open my$fd,'<',"$git_dir/cloneurl"2546orreturnwantarray?2547@{ config_to_multi(git_get_project_config('url')) } :2548 config_to_multi(git_get_project_config('url'));2549my@git_project_url_list=map{chomp;$_} <$fd>;2550close$fd;25512552returnwantarray?@git_project_url_list: \@git_project_url_list;2553}25542555sub git_get_projects_list {2556my($filter) =@_;2557my@list;25582559$filter||='';2560$filter=~s/\.git$//;25612562my$check_forks= gitweb_check_feature('forks');25632564if(-d $projects_list) {2565# search in directory2566my$dir=$projects_list. ($filter?"/$filter":'');2567# remove the trailing "/"2568$dir=~s!/+$!!;2569my$pfxlen=length("$dir");2570my$pfxdepth= ($dir=~tr!/!!);25712572 File::Find::find({2573 follow_fast =>1,# follow symbolic links2574 follow_skip =>2,# ignore duplicates2575 dangling_symlinks =>0,# ignore dangling symlinks, silently2576 wanted =>sub{2577# global variables2578our$project_maxdepth;2579our$projectroot;2580# skip project-list toplevel, if we get it.2581return if(m!^[/.]$!);2582# only directories can be git repositories2583return unless(-d $_);2584# don't traverse too deep (Find is super slow on os x)2585if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2586$File::Find::prune =1;2587return;2588}25892590my$subdir=substr($File::Find::name,$pfxlen+1);2591# we check related file in $projectroot2592my$path= ($filter?"$filter/":'') .$subdir;2593if(check_export_ok("$projectroot/$path")) {2594push@list, { path =>$path};2595$File::Find::prune =1;2596}2597},2598},"$dir");25992600}elsif(-f $projects_list) {2601# read from file(url-encoded):2602# 'git%2Fgit.git Linus+Torvalds'2603# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2604# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2605my%paths;2606open my$fd,'<',$projects_listorreturn;2607 PROJECT:2608while(my$line= <$fd>) {2609chomp$line;2610my($path,$owner) =split' ',$line;2611$path= unescape($path);2612$owner= unescape($owner);2613if(!defined$path) {2614next;2615}2616if($filterne'') {2617# looking for forks;2618my$pfx=substr($path,0,length($filter));2619if($pfxne$filter) {2620next PROJECT;2621}2622my$sfx=substr($path,length($filter));2623if($sfx!~/^\/.*\.git$/) {2624next PROJECT;2625}2626}elsif($check_forks) {2627 PATH:2628foreachmy$filter(keys%paths) {2629# looking for forks;2630my$pfx=substr($path,0,length($filter));2631if($pfxne$filter) {2632next PATH;2633}2634my$sfx=substr($path,length($filter));2635if($sfx!~/^\/.*\.git$/) {2636next PATH;2637}2638# is a fork, don't include it in2639# the list2640next PROJECT;2641}2642}2643if(check_export_ok("$projectroot/$path")) {2644my$pr= {2645 path =>$path,2646 owner => to_utf8($owner),2647};2648push@list,$pr;2649(my$forks_path=$path) =~s/\.git$//;2650$paths{$forks_path}++;2651}2652}2653close$fd;2654}2655return@list;2656}26572658our$gitweb_project_owner=undef;2659sub git_get_project_list_from_file {26602661return if(defined$gitweb_project_owner);26622663$gitweb_project_owner= {};2664# read from file (url-encoded):2665# 'git%2Fgit.git Linus+Torvalds'2666# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2667# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2668if(-f $projects_list) {2669open(my$fd,'<',$projects_list);2670while(my$line= <$fd>) {2671chomp$line;2672my($pr,$ow) =split' ',$line;2673$pr= unescape($pr);2674$ow= unescape($ow);2675$gitweb_project_owner->{$pr} = to_utf8($ow);2676}2677close$fd;2678}2679}26802681sub git_get_project_owner {2682my$project=shift;2683my$owner;26842685returnundefunless$project;2686$git_dir="$projectroot/$project";26872688if(!defined$gitweb_project_owner) {2689 git_get_project_list_from_file();2690}26912692if(exists$gitweb_project_owner->{$project}) {2693$owner=$gitweb_project_owner->{$project};2694}2695if(!defined$owner){2696$owner= git_get_project_config('owner');2697}2698if(!defined$owner) {2699$owner= get_file_owner("$git_dir");2700}27012702return$owner;2703}27042705sub git_get_last_activity {2706my($path) =@_;2707my$fd;27082709$git_dir="$projectroot/$path";2710open($fd,"-|", git_cmd(),'for-each-ref',2711'--format=%(committer)',2712'--sort=-committerdate',2713'--count=1',2714'refs/heads')orreturn;2715my$most_recent= <$fd>;2716close$fdorreturn;2717if(defined$most_recent&&2718$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2719my$timestamp=$1;2720my$age=time-$timestamp;2721return($age, age_string($age));2722}2723return(undef,undef);2724}27252726sub git_get_references {2727my$type=shift||"";2728my%refs;2729# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112730# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2731open my$fd,"-|", git_cmd(),"show-ref","--dereference",2732($type? ("--","refs/$type") : ())# use -- <pattern> if $type2733orreturn;27342735while(my$line= <$fd>) {2736chomp$line;2737if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2738if(defined$refs{$1}) {2739push@{$refs{$1}},$2;2740}else{2741$refs{$1} = [$2];2742}2743}2744}2745close$fdorreturn;2746return \%refs;2747}27482749sub git_get_rev_name_tags {2750my$hash=shift||returnundef;27512752open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2753orreturn;2754my$name_rev= <$fd>;2755close$fd;27562757if($name_rev=~ m|^$hash tags/(.*)$|) {2758return$1;2759}else{2760# catches also '$hash undefined' output2761returnundef;2762}2763}27642765## ----------------------------------------------------------------------2766## parse to hash functions27672768sub parse_date {2769my$epoch=shift;2770my$tz=shift||"-0000";27712772my%date;2773my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2774my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2775my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2776$date{'hour'} =$hour;2777$date{'minute'} =$min;2778$date{'mday'} =$mday;2779$date{'day'} =$days[$wday];2780$date{'month'} =$months[$mon];2781$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2782$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2783$date{'mday-time'} =sprintf"%d%s%02d:%02d",2784$mday,$months[$mon],$hour,$min;2785$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",27861900+$year,1+$mon,$mday,$hour,$min,$sec;27872788$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2789my$local=$epoch+ ((int$1+ ($2/60)) *3600);2790($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2791$date{'hour_local'} =$hour;2792$date{'minute_local'} =$min;2793$date{'tz_local'} =$tz;2794$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",27951900+$year,$mon+1,$mday,2796$hour,$min,$sec,$tz);2797return%date;2798}27992800sub parse_tag {2801my$tag_id=shift;2802my%tag;2803my@comment;28042805open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2806$tag{'id'} =$tag_id;2807while(my$line= <$fd>) {2808chomp$line;2809if($line=~m/^object ([0-9a-fA-F]{40})$/) {2810$tag{'object'} =$1;2811}elsif($line=~m/^type (.+)$/) {2812$tag{'type'} =$1;2813}elsif($line=~m/^tag (.+)$/) {2814$tag{'name'} =$1;2815}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2816$tag{'author'} =$1;2817$tag{'author_epoch'} =$2;2818$tag{'author_tz'} =$3;2819if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2820$tag{'author_name'} =$1;2821$tag{'author_email'} =$2;2822}else{2823$tag{'author_name'} =$tag{'author'};2824}2825}elsif($line=~m/--BEGIN/) {2826push@comment,$line;2827last;2828}elsif($lineeq"") {2829last;2830}2831}2832push@comment, <$fd>;2833$tag{'comment'} = \@comment;2834close$fdorreturn;2835if(!defined$tag{'name'}) {2836return2837};2838return%tag2839}28402841sub parse_commit_text {2842my($commit_text,$withparents) =@_;2843my@commit_lines=split'\n',$commit_text;2844my%co;28452846pop@commit_lines;# Remove '\0'28472848if(!@commit_lines) {2849return;2850}28512852my$header=shift@commit_lines;2853if($header!~m/^[0-9a-fA-F]{40}/) {2854return;2855}2856($co{'id'},my@parents) =split' ',$header;2857while(my$line=shift@commit_lines) {2858last if$lineeq"\n";2859if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2860$co{'tree'} =$1;2861}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2862push@parents,$1;2863}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2864$co{'author'} = to_utf8($1);2865$co{'author_epoch'} =$2;2866$co{'author_tz'} =$3;2867if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2868$co{'author_name'} =$1;2869$co{'author_email'} =$2;2870}else{2871$co{'author_name'} =$co{'author'};2872}2873}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2874$co{'committer'} = to_utf8($1);2875$co{'committer_epoch'} =$2;2876$co{'committer_tz'} =$3;2877if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2878$co{'committer_name'} =$1;2879$co{'committer_email'} =$2;2880}else{2881$co{'committer_name'} =$co{'committer'};2882}2883}2884}2885if(!defined$co{'tree'}) {2886return;2887};2888$co{'parents'} = \@parents;2889$co{'parent'} =$parents[0];28902891foreachmy$title(@commit_lines) {2892$title=~s/^ //;2893if($titlene"") {2894$co{'title'} = chop_str($title,80,5);2895# remove leading stuff of merges to make the interesting part visible2896if(length($title) >50) {2897$title=~s/^Automatic //;2898$title=~s/^merge (of|with) /Merge ... /i;2899if(length($title) >50) {2900$title=~s/(http|rsync):\/\///;2901}2902if(length($title) >50) {2903$title=~s/(master|www|rsync)\.//;2904}2905if(length($title) >50) {2906$title=~s/kernel.org:?//;2907}2908if(length($title) >50) {2909$title=~s/\/pub\/scm//;2910}2911}2912$co{'title_short'} = chop_str($title,50,5);2913last;2914}2915}2916if(!defined$co{'title'} ||$co{'title'}eq"") {2917$co{'title'} =$co{'title_short'} ='(no commit message)';2918}2919# remove added spaces2920foreachmy$line(@commit_lines) {2921$line=~s/^ //;2922}2923$co{'comment'} = \@commit_lines;29242925my$age=time-$co{'committer_epoch'};2926$co{'age'} =$age;2927$co{'age_string'} = age_string($age);2928my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2929if($age>60*60*24*7*2) {2930$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2931$co{'age_string_age'} =$co{'age_string'};2932}else{2933$co{'age_string_date'} =$co{'age_string'};2934$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2935}2936return%co;2937}29382939sub parse_commit {2940my($commit_id) =@_;2941my%co;29422943local$/="\0";29442945open my$fd,"-|", git_cmd(),"rev-list",2946"--parents",2947"--header",2948"--max-count=1",2949$commit_id,2950"--",2951or die_error(500,"Open git-rev-list failed");2952%co= parse_commit_text(<$fd>,1);2953close$fd;29542955return%co;2956}29572958sub parse_commits {2959my($commit_id,$maxcount,$skip,$filename,@args) =@_;2960my@cos;29612962$maxcount||=1;2963$skip||=0;29642965local$/="\0";29662967open my$fd,"-|", git_cmd(),"rev-list",2968"--header",2969@args,2970("--max-count=".$maxcount),2971("--skip=".$skip),2972@extra_options,2973$commit_id,2974"--",2975($filename? ($filename) : ())2976or die_error(500,"Open git-rev-list failed");2977while(my$line= <$fd>) {2978my%co= parse_commit_text($line);2979push@cos, \%co;2980}2981close$fd;29822983returnwantarray?@cos: \@cos;2984}29852986# parse line of git-diff-tree "raw" output2987sub parse_difftree_raw_line {2988my$line=shift;2989my%res;29902991# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2992# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2993if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2994$res{'from_mode'} =$1;2995$res{'to_mode'} =$2;2996$res{'from_id'} =$3;2997$res{'to_id'} =$4;2998$res{'status'} =$5;2999$res{'similarity'} =$6;3000if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3001($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3002}else{3003$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3004}3005}3006# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3007# combined diff (for merge commit)3008elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3009$res{'nparents'} =length($1);3010$res{'from_mode'} = [split(' ',$2) ];3011$res{'to_mode'} =pop@{$res{'from_mode'}};3012$res{'from_id'} = [split(' ',$3) ];3013$res{'to_id'} =pop@{$res{'from_id'}};3014$res{'status'} = [split('',$4) ];3015$res{'to_file'} = unquote($5);3016}3017# 'c512b523472485aef4fff9e57b229d9d243c967f'3018elsif($line=~m/^([0-9a-fA-F]{40})$/) {3019$res{'commit'} =$1;3020}30213022returnwantarray?%res: \%res;3023}30243025# wrapper: return parsed line of git-diff-tree "raw" output3026# (the argument might be raw line, or parsed info)3027sub parsed_difftree_line {3028my$line_or_ref=shift;30293030if(ref($line_or_ref)eq"HASH") {3031# pre-parsed (or generated by hand)3032return$line_or_ref;3033}else{3034return parse_difftree_raw_line($line_or_ref);3035}3036}30373038# parse line of git-ls-tree output3039sub parse_ls_tree_line {3040my$line=shift;3041my%opts=@_;3042my%res;30433044if($opts{'-l'}) {3045#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3046$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;30473048$res{'mode'} =$1;3049$res{'type'} =$2;3050$res{'hash'} =$3;3051$res{'size'} =$4;3052if($opts{'-z'}) {3053$res{'name'} =$5;3054}else{3055$res{'name'} = unquote($5);3056}3057}else{3058#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3059$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;30603061$res{'mode'} =$1;3062$res{'type'} =$2;3063$res{'hash'} =$3;3064if($opts{'-z'}) {3065$res{'name'} =$4;3066}else{3067$res{'name'} = unquote($4);3068}3069}30703071returnwantarray?%res: \%res;3072}30733074# generates _two_ hashes, references to which are passed as 2 and 3 argument3075sub parse_from_to_diffinfo {3076my($diffinfo,$from,$to,@parents) =@_;30773078if($diffinfo->{'nparents'}) {3079# combined diff3080$from->{'file'} = [];3081$from->{'href'} = [];3082 fill_from_file_info($diffinfo,@parents)3083unlessexists$diffinfo->{'from_file'};3084for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3085$from->{'file'}[$i] =3086defined$diffinfo->{'from_file'}[$i] ?3087$diffinfo->{'from_file'}[$i] :3088$diffinfo->{'to_file'};3089if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3090$from->{'href'}[$i] = href(action=>"blob",3091 hash_base=>$parents[$i],3092 hash=>$diffinfo->{'from_id'}[$i],3093 file_name=>$from->{'file'}[$i]);3094}else{3095$from->{'href'}[$i] =undef;3096}3097}3098}else{3099# ordinary (not combined) diff3100$from->{'file'} =$diffinfo->{'from_file'};3101if($diffinfo->{'status'}ne"A") {# not new (added) file3102$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3103 hash=>$diffinfo->{'from_id'},3104 file_name=>$from->{'file'});3105}else{3106delete$from->{'href'};3107}3108}31093110$to->{'file'} =$diffinfo->{'to_file'};3111if(!is_deleted($diffinfo)) {# file exists in result3112$to->{'href'} = href(action=>"blob", hash_base=>$hash,3113 hash=>$diffinfo->{'to_id'},3114 file_name=>$to->{'file'});3115}else{3116delete$to->{'href'};3117}3118}31193120## ......................................................................3121## parse to array of hashes functions31223123sub git_get_heads_list {3124my$limit=shift;3125my@headslist;31263127open my$fd,'-|', git_cmd(),'for-each-ref',3128($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3129'--format=%(objectname) %(refname) %(subject)%00%(committer)',3130'refs/heads'3131orreturn;3132while(my$line= <$fd>) {3133my%ref_item;31343135chomp$line;3136my($refinfo,$committerinfo) =split(/\0/,$line);3137my($hash,$name,$title) =split(' ',$refinfo,3);3138my($committer,$epoch,$tz) =3139($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3140$ref_item{'fullname'} =$name;3141$name=~s!^refs/heads/!!;31423143$ref_item{'name'} =$name;3144$ref_item{'id'} =$hash;3145$ref_item{'title'} =$title||'(no commit message)';3146$ref_item{'epoch'} =$epoch;3147if($epoch) {3148$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3149}else{3150$ref_item{'age'} ="unknown";3151}31523153push@headslist, \%ref_item;3154}3155close$fd;31563157returnwantarray?@headslist: \@headslist;3158}31593160sub git_get_tags_list {3161my$limit=shift;3162my@tagslist;31633164open my$fd,'-|', git_cmd(),'for-each-ref',3165($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3166'--format=%(objectname) %(objecttype) %(refname) '.3167'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3168'refs/tags'3169orreturn;3170while(my$line= <$fd>) {3171my%ref_item;31723173chomp$line;3174my($refinfo,$creatorinfo) =split(/\0/,$line);3175my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3176my($creator,$epoch,$tz) =3177($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3178$ref_item{'fullname'} =$name;3179$name=~s!^refs/tags/!!;31803181$ref_item{'type'} =$type;3182$ref_item{'id'} =$id;3183$ref_item{'name'} =$name;3184if($typeeq"tag") {3185$ref_item{'subject'} =$title;3186$ref_item{'reftype'} =$reftype;3187$ref_item{'refid'} =$refid;3188}else{3189$ref_item{'reftype'} =$type;3190$ref_item{'refid'} =$id;3191}31923193if($typeeq"tag"||$typeeq"commit") {3194$ref_item{'epoch'} =$epoch;3195if($epoch) {3196$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3197}else{3198$ref_item{'age'} ="unknown";3199}3200}32013202push@tagslist, \%ref_item;3203}3204close$fd;32053206returnwantarray?@tagslist: \@tagslist;3207}32083209## ----------------------------------------------------------------------3210## filesystem-related functions32113212sub get_file_owner {3213my$path=shift;32143215my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3216my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3217if(!defined$gcos) {3218returnundef;3219}3220my$owner=$gcos;3221$owner=~s/[,;].*$//;3222return to_utf8($owner);3223}32243225# assume that file exists3226sub insert_file {3227my$filename=shift;32283229open my$fd,'<',$filename;3230print map{ to_utf8($_) } <$fd>;3231close$fd;3232}32333234## ......................................................................3235## mimetype related functions32363237sub mimetype_guess_file {3238my$filename=shift;3239my$mimemap=shift;3240-r $mimemaporreturnundef;32413242my%mimemap;3243open(my$mh,'<',$mimemap)orreturnundef;3244while(<$mh>) {3245next ifm/^#/;# skip comments3246my($mimetype,$exts) =split(/\t+/);3247if(defined$exts) {3248my@exts=split(/\s+/,$exts);3249foreachmy$ext(@exts) {3250$mimemap{$ext} =$mimetype;3251}3252}3253}3254close($mh);32553256$filename=~/\.([^.]*)$/;3257return$mimemap{$1};3258}32593260sub mimetype_guess {3261my$filename=shift;3262my$mime;3263$filename=~/\./orreturnundef;32643265if($mimetypes_file) {3266my$file=$mimetypes_file;3267if($file!~m!^/!) {# if it is relative path3268# it is relative to project3269$file="$projectroot/$project/$file";3270}3271$mime= mimetype_guess_file($filename,$file);3272}3273$mime||= mimetype_guess_file($filename,'/etc/mime.types');3274return$mime;3275}32763277sub blob_mimetype {3278my$fd=shift;3279my$filename=shift;32803281if($filename) {3282my$mime= mimetype_guess($filename);3283$mimeandreturn$mime;3284}32853286# just in case3287return$default_blob_plain_mimetypeunless$fd;32883289if(-T $fd) {3290return'text/plain';3291}elsif(!$filename) {3292return'application/octet-stream';3293}elsif($filename=~m/\.png$/i) {3294return'image/png';3295}elsif($filename=~m/\.gif$/i) {3296return'image/gif';3297}elsif($filename=~m/\.jpe?g$/i) {3298return'image/jpeg';3299}else{3300return'application/octet-stream';3301}3302}33033304sub blob_contenttype {3305my($fd,$file_name,$type) =@_;33063307$type||= blob_mimetype($fd,$file_name);3308if($typeeq'text/plain'&&defined$default_text_plain_charset) {3309$type.="; charset=$default_text_plain_charset";3310}33113312return$type;3313}33143315# guess file syntax for syntax highlighting; return undef if no highlighting3316# the name of syntax can (in the future) depend on syntax highlighter used3317sub guess_file_syntax {3318my($highlight,$mimetype,$file_name) =@_;3319returnundefunless($highlight&&defined$file_name);33203321# configuration for 'highlight' (http://www.andre-simon.de/)3322# match by basename3323my%highlight_basename= (3324#'Program' => 'py',3325#'Library' => 'py',3326'SConstruct'=>'py',# SCons equivalent of Makefile3327'Makefile'=>'make',3328);3329# match by extension3330my%highlight_ext= (3331# main extensions, defining name of syntax;3332# see files in /usr/share/highlight/langDefs/ directory3333map{$_=>$_}3334qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),3335# alternate extensions, see /etc/highlight/filetypes.conf3336'h'=>'c',3337map{$_=>'cpp'}qw(cxx c++ cc),3338map{$_=>'php'}qw(php3 php4),3339map{$_=>'pl'}qw(perl pm),# perhaps also 'cgi'3340'mak'=>'make',3341map{$_=>'xml'}qw(xhtml html htm),3342);33433344my$basename= basename($file_name,'.in');3345return$highlight_basename{$basename}3346ifexists$highlight_basename{$basename};33473348$basename=~/\.([^.]*)$/;3349my$ext=$1orreturnundef;3350return$highlight_ext{$ext}3351ifexists$highlight_ext{$ext};33523353returnundef;3354}33553356# run highlighter and return FD of its output,3357# or return original FD if no highlighting3358sub run_highlighter {3359my($fd,$highlight,$syntax) =@_;3360return$fdunless($highlight&&defined$syntax);33613362close$fd3363or die_error(404,"Reading blob failed");3364open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3365"highlight --xhtml --fragment --syntax$syntax|"3366or die_error(500,"Couldn't open file or run syntax highlighter");3367return$fd;3368}33693370## ======================================================================3371## functions printing HTML: header, footer, error page33723373sub get_page_title {3374my$title= to_utf8($site_name);33753376return$titleunless(defined$project);3377$title.=" - ". to_utf8($project);33783379return$titleunless(defined$action);3380$title.="/$action";# $action is US-ASCII (7bit ASCII)33813382return$titleunless(defined$file_name);3383$title.=" - ". esc_path($file_name);3384if($actioneq"tree"&&$file_name!~ m|/$|) {3385$title.="/";3386}33873388return$title;3389}33903391sub git_header_html {3392my$status=shift||"200 OK";3393my$expires=shift;3394my%opts=@_;33953396my$title= get_page_title();3397my$content_type;3398# require explicit support from the UA if we are to send the page as3399# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3400# we have to do this because MSIE sometimes globs '*/*', pretending to3401# support xhtml+xml but choking when it gets what it asked for.3402if(defined$cgi->http('HTTP_ACCEPT') &&3403$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3404$cgi->Accept('application/xhtml+xml') !=0) {3405$content_type='application/xhtml+xml';3406}else{3407$content_type='text/html';3408}3409print$cgi->header(-type=>$content_type, -charset =>'utf-8',3410-status=>$status, -expires =>$expires)3411unless($opts{'-no_http_header'});3412my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3413print<<EOF;3414<?xml version="1.0" encoding="utf-8"?>3415<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3416<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3417<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3418<!-- git core binaries version$git_version-->3419<head>3420<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3421<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3422<meta name="robots" content="index, nofollow"/>3423<title>$title</title>3424EOF3425# the stylesheet, favicon etc urls won't work correctly with path_info3426# unless we set the appropriate base URL3427if($ENV{'PATH_INFO'}) {3428print"<base href=\"".esc_url($base_url)."\"/>\n";3429}3430# print out each stylesheet that exist, providing backwards capability3431# for those people who defined $stylesheet in a config file3432if(defined$stylesheet) {3433print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3434}else{3435foreachmy$stylesheet(@stylesheets) {3436next unless$stylesheet;3437print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3438}3439}3440if(defined$project) {3441my%href_params= get_feed_info();3442if(!exists$href_params{'-title'}) {3443$href_params{'-title'} ='log';3444}34453446foreachmy$formatqw(RSS Atom){3447my$type=lc($format);3448my%link_attr= (3449'-rel'=>'alternate',3450'-title'=>"$project-$href_params{'-title'} -$formatfeed",3451'-type'=>"application/$type+xml"3452);34533454$href_params{'action'} =$type;3455$link_attr{'-href'} = href(%href_params);3456print"<link ".3457"rel=\"$link_attr{'-rel'}\"".3458"title=\"$link_attr{'-title'}\"".3459"href=\"$link_attr{'-href'}\"".3460"type=\"$link_attr{'-type'}\"".3461"/>\n";34623463$href_params{'extra_options'} ='--no-merges';3464$link_attr{'-href'} = href(%href_params);3465$link_attr{'-title'} .=' (no merges)';3466print"<link ".3467"rel=\"$link_attr{'-rel'}\"".3468"title=\"$link_attr{'-title'}\"".3469"href=\"$link_attr{'-href'}\"".3470"type=\"$link_attr{'-type'}\"".3471"/>\n";3472}34733474}else{3475printf('<link rel="alternate" title="%sprojects list" '.3476'href="%s" type="text/plain; charset=utf-8" />'."\n",3477$site_name, href(project=>undef, action=>"project_index"));3478printf('<link rel="alternate" title="%sprojects feeds" '.3479'href="%s" type="text/x-opml" />'."\n",3480$site_name, href(project=>undef, action=>"opml"));3481}3482if(defined$favicon) {3483printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3484}34853486print"</head>\n".3487"<body>\n";34883489if(defined$site_header&& -f $site_header) {3490 insert_file($site_header);3491}34923493print"<div class=\"page_header\">\n".3494$cgi->a({-href => esc_url($logo_url),3495-title =>$logo_label},3496qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3497print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3498if(defined$project) {3499print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3500if(defined$action) {3501print" /$action";3502}3503print"\n";3504}3505print"</div>\n";35063507my$have_search= gitweb_check_feature('search');3508if(defined$project&&$have_search) {3509if(!defined$searchtext) {3510$searchtext="";3511}3512my$search_hash;3513if(defined$hash_base) {3514$search_hash=$hash_base;3515}elsif(defined$hash) {3516$search_hash=$hash;3517}else{3518$search_hash="HEAD";3519}3520my$action=$my_uri;3521my$use_pathinfo= gitweb_check_feature('pathinfo');3522if($use_pathinfo) {3523$action.="/".esc_url($project);3524}3525print$cgi->startform(-method=>"get", -action =>$action) .3526"<div class=\"search\">\n".3527(!$use_pathinfo&&3528$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3529$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3530$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3531$cgi->popup_menu(-name =>'st', -default=>'commit',3532-values=> ['commit','grep','author','committer','pickaxe']) .3533$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3534" search:\n",3535$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3536"<span title=\"Extended regular expression\">".3537$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3538-checked =>$search_use_regexp) .3539"</span>".3540"</div>".3541$cgi->end_form() ."\n";3542}3543}35443545sub git_footer_html {3546my$feed_class='rss_logo';35473548print"<div class=\"page_footer\">\n";3549if(defined$project) {3550my$descr= git_get_project_description($project);3551if(defined$descr) {3552print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3553}35543555my%href_params= get_feed_info();3556if(!%href_params) {3557$feed_class.=' generic';3558}3559$href_params{'-title'} ||='log';35603561foreachmy$formatqw(RSS Atom){3562$href_params{'action'} =lc($format);3563print$cgi->a({-href => href(%href_params),3564-title =>"$href_params{'-title'}$formatfeed",3565-class=>$feed_class},$format)."\n";3566}35673568}else{3569print$cgi->a({-href => href(project=>undef, action=>"opml"),3570-class=>$feed_class},"OPML") ." ";3571print$cgi->a({-href => href(project=>undef, action=>"project_index"),3572-class=>$feed_class},"TXT") ."\n";3573}3574print"</div>\n";# class="page_footer"35753576if(defined$t0&& gitweb_check_feature('timed')) {3577print"<div id=\"generating_info\">\n";3578print'This page took '.3579'<span id="generating_time" class="time_span">'.3580 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3581' seconds </span>'.3582' and '.3583'<span id="generating_cmd">'.3584$number_of_git_cmds.3585'</span> git commands '.3586" to generate.\n";3587print"</div>\n";# class="page_footer"3588}35893590if(defined$site_footer&& -f $site_footer) {3591 insert_file($site_footer);3592}35933594print qq!<script type="text/javascript" src="$javascript"></script>\n!;3595if(defined$action&&3596$actioneq'blame_incremental') {3597print qq!<script type="text/javascript">\n!.3598 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3599 qq!"!. href() .qq!");\n!.3600 qq!</script>\n!;3601}elsif(gitweb_check_feature('javascript-actions')) {3602print qq!<script type="text/javascript">\n!.3603 qq!window.onload = fixLinks;\n!.3604 qq!</script>\n!;3605}36063607print"</body>\n".3608"</html>";3609}36103611# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3612# Example: die_error(404, 'Hash not found')3613# By convention, use the following status codes (as defined in RFC 2616):3614# 400: Invalid or missing CGI parameters, or3615# requested object exists but has wrong type.3616# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3617# this server or project.3618# 404: Requested object/revision/project doesn't exist.3619# 500: The server isn't configured properly, or3620# an internal error occurred (e.g. failed assertions caused by bugs), or3621# an unknown error occurred (e.g. the git binary died unexpectedly).3622# 503: The server is currently unavailable (because it is overloaded,3623# or down for maintenance). Generally, this is a temporary state.3624sub die_error {3625my$status=shift||500;3626my$error= esc_html(shift) ||"Internal Server Error";3627my$extra=shift;3628my%opts=@_;36293630my%http_responses= (3631400=>'400 Bad Request',3632403=>'403 Forbidden',3633404=>'404 Not Found',3634500=>'500 Internal Server Error',3635503=>'503 Service Unavailable',3636);3637 git_header_html($http_responses{$status},undef,%opts);3638print<<EOF;3639<div class="page_body">3640<br /><br />3641$status-$error3642<br />3643EOF3644if(defined$extra) {3645print"<hr />\n".3646"$extra\n";3647}3648print"</div>\n";36493650 git_footer_html();3651goto DONE_GITWEB3652unless($opts{'-error_handler'});3653}36543655## ----------------------------------------------------------------------3656## functions printing or outputting HTML: navigation36573658sub git_print_page_nav {3659my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3660$extra=''if!defined$extra;# pager or formats36613662my@navs=qw(summary shortlog log commit commitdiff tree);3663if($suppress) {3664@navs=grep{$_ne$suppress}@navs;3665}36663667my%arg=map{$_=> {action=>$_} }@navs;3668if(defined$head) {3669for(qw(commit commitdiff)) {3670$arg{$_}{'hash'} =$head;3671}3672if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3673for(qw(shortlog log)) {3674$arg{$_}{'hash'} =$head;3675}3676}3677}36783679$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3680$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;36813682my@actions= gitweb_get_feature('actions');3683my%repl= (3684'%'=>'%',3685'n'=>$project,# project name3686'f'=>$git_dir,# project path within filesystem3687'h'=>$treehead||'',# current hash ('h' parameter)3688'b'=>$treebase||'',# hash base ('hb' parameter)3689);3690while(@actions) {3691my($label,$link,$pos) =splice(@actions,0,3);3692# insert3693@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3694# munch munch3695$link=~s/%([%nfhb])/$repl{$1}/g;3696$arg{$label}{'_href'} =$link;3697}36983699print"<div class=\"page_nav\">\n".3700(join" | ",3701map{$_eq$current?3702$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3703}@navs);3704print"<br/>\n$extra<br/>\n".3705"</div>\n";3706}37073708sub format_paging_nav {3709my($action,$page,$has_next_link) =@_;3710my$paging_nav;371137123713if($page>0) {3714$paging_nav.=3715$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3716" ⋅ ".3717$cgi->a({-href => href(-replay=>1, page=>$page-1),3718-accesskey =>"p", -title =>"Alt-p"},"prev");3719}else{3720$paging_nav.="first ⋅ prev";3721}37223723if($has_next_link) {3724$paging_nav.=" ⋅ ".3725$cgi->a({-href => href(-replay=>1, page=>$page+1),3726-accesskey =>"n", -title =>"Alt-n"},"next");3727}else{3728$paging_nav.=" ⋅ next";3729}37303731return$paging_nav;3732}37333734## ......................................................................3735## functions printing or outputting HTML: div37363737sub git_print_header_div {3738my($action,$title,$hash,$hash_base) =@_;3739my%args= ();37403741$args{'action'} =$action;3742$args{'hash'} =$hashif$hash;3743$args{'hash_base'} =$hash_baseif$hash_base;37443745print"<div class=\"header\">\n".3746$cgi->a({-href => href(%args), -class=>"title"},3747$title?$title:$action) .3748"\n</div>\n";3749}37503751sub print_local_time {3752print format_local_time(@_);3753}37543755sub format_local_time {3756my$localtime='';3757my%date=@_;3758if($date{'hour_local'} <6) {3759$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3760$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3761}else{3762$localtime.=sprintf(" (%02d:%02d%s)",3763$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3764}37653766return$localtime;3767}37683769# Outputs the author name and date in long form3770sub git_print_authorship {3771my$co=shift;3772my%opts=@_;3773my$tag=$opts{-tag} ||'div';3774my$author=$co->{'author_name'};37753776my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3777print"<$tagclass=\"author_date\">".3778 format_search_author($author,"author", esc_html($author)) .3779" [$ad{'rfc2822'}";3780 print_local_time(%ad)if($opts{-localtime});3781print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3782."</$tag>\n";3783}37843785# Outputs table rows containing the full author or committer information,3786# in the format expected for 'commit' view (& similia).3787# Parameters are a commit hash reference, followed by the list of people3788# to output information for. If the list is empty it defalts to both3789# author and committer.3790sub git_print_authorship_rows {3791my$co=shift;3792# too bad we can't use @people = @_ || ('author', 'committer')3793my@people=@_;3794@people= ('author','committer')unless@people;3795foreachmy$who(@people) {3796my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3797print"<tr><td>$who</td><td>".3798 format_search_author($co->{"${who}_name"},$who,3799 esc_html($co->{"${who}_name"})) ." ".3800 format_search_author($co->{"${who}_email"},$who,3801 esc_html("<".$co->{"${who}_email"} .">")) .3802"</td><td rowspan=\"2\">".3803 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3804"</td></tr>\n".3805"<tr>".3806"<td></td><td>$wd{'rfc2822'}";3807 print_local_time(%wd);3808print"</td>".3809"</tr>\n";3810}3811}38123813sub git_print_page_path {3814my$name=shift;3815my$type=shift;3816my$hb=shift;381738183819print"<div class=\"page_path\">";3820print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3821-title =>'tree root'}, to_utf8("[$project]"));3822print" / ";3823if(defined$name) {3824my@dirname=split'/',$name;3825my$basename=pop@dirname;3826my$fullname='';38273828foreachmy$dir(@dirname) {3829$fullname.= ($fullname?'/':'') .$dir;3830print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3831 hash_base=>$hb),3832-title =>$fullname}, esc_path($dir));3833print" / ";3834}3835if(defined$type&&$typeeq'blob') {3836print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3837 hash_base=>$hb),3838-title =>$name}, esc_path($basename));3839}elsif(defined$type&&$typeeq'tree') {3840print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3841 hash_base=>$hb),3842-title =>$name}, esc_path($basename));3843print" / ";3844}else{3845print esc_path($basename);3846}3847}3848print"<br/></div>\n";3849}38503851sub git_print_log {3852my$log=shift;3853my%opts=@_;38543855if($opts{'-remove_title'}) {3856# remove title, i.e. first line of log3857shift@$log;3858}3859# remove leading empty lines3860while(defined$log->[0] &&$log->[0]eq"") {3861shift@$log;3862}38633864# print log3865my$signoff=0;3866my$empty=0;3867foreachmy$line(@$log) {3868if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3869$signoff=1;3870$empty=0;3871if(!$opts{'-remove_signoff'}) {3872print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3873next;3874}else{3875# remove signoff lines3876next;3877}3878}else{3879$signoff=0;3880}38813882# print only one empty line3883# do not print empty line after signoff3884if($lineeq"") {3885next if($empty||$signoff);3886$empty=1;3887}else{3888$empty=0;3889}38903891print format_log_line_html($line) ."<br/>\n";3892}38933894if($opts{'-final_empty_line'}) {3895# end with single empty line3896print"<br/>\n"unless$empty;3897}3898}38993900# return link target (what link points to)3901sub git_get_link_target {3902my$hash=shift;3903my$link_target;39043905# read link3906open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3907orreturn;3908{3909local$/=undef;3910$link_target= <$fd>;3911}3912close$fd3913orreturn;39143915return$link_target;3916}39173918# given link target, and the directory (basedir) the link is in,3919# return target of link relative to top directory (top tree);3920# return undef if it is not possible (including absolute links).3921sub normalize_link_target {3922my($link_target,$basedir) =@_;39233924# absolute symlinks (beginning with '/') cannot be normalized3925return if(substr($link_target,0,1)eq'/');39263927# normalize link target to path from top (root) tree (dir)3928my$path;3929if($basedir) {3930$path=$basedir.'/'.$link_target;3931}else{3932# we are in top (root) tree (dir)3933$path=$link_target;3934}39353936# remove //, /./, and /../3937my@path_parts;3938foreachmy$part(split('/',$path)) {3939# discard '.' and ''3940next if(!$part||$parteq'.');3941# handle '..'3942if($parteq'..') {3943if(@path_parts) {3944pop@path_parts;3945}else{3946# link leads outside repository (outside top dir)3947return;3948}3949}else{3950push@path_parts,$part;3951}3952}3953$path=join('/',@path_parts);39543955return$path;3956}39573958# print tree entry (row of git_tree), but without encompassing <tr> element3959sub git_print_tree_entry {3960my($t,$basedir,$hash_base,$have_blame) =@_;39613962my%base_key= ();3963$base_key{'hash_base'} =$hash_baseifdefined$hash_base;39643965# The format of a table row is: mode list link. Where mode is3966# the mode of the entry, list is the name of the entry, an href,3967# and link is the action links of the entry.39683969print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3970if(exists$t->{'size'}) {3971print"<td class=\"size\">$t->{'size'}</td>\n";3972}3973if($t->{'type'}eq"blob") {3974print"<td class=\"list\">".3975$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3976 file_name=>"$basedir$t->{'name'}",%base_key),3977-class=>"list"}, esc_path($t->{'name'}));3978if(S_ISLNK(oct$t->{'mode'})) {3979my$link_target= git_get_link_target($t->{'hash'});3980if($link_target) {3981my$norm_target= normalize_link_target($link_target,$basedir);3982if(defined$norm_target) {3983print" -> ".3984$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3985 file_name=>$norm_target),3986-title =>$norm_target}, esc_path($link_target));3987}else{3988print" -> ". esc_path($link_target);3989}3990}3991}3992print"</td>\n";3993print"<td class=\"link\">";3994print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3995 file_name=>"$basedir$t->{'name'}",%base_key)},3996"blob");3997if($have_blame) {3998print" | ".3999$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4000 file_name=>"$basedir$t->{'name'}",%base_key)},4001"blame");4002}4003if(defined$hash_base) {4004print" | ".4005$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4006 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4007"history");4008}4009print" | ".4010$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4011 file_name=>"$basedir$t->{'name'}")},4012"raw");4013print"</td>\n";40144015}elsif($t->{'type'}eq"tree") {4016print"<td class=\"list\">";4017print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4018 file_name=>"$basedir$t->{'name'}",4019%base_key)},4020 esc_path($t->{'name'}));4021print"</td>\n";4022print"<td class=\"link\">";4023print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4024 file_name=>"$basedir$t->{'name'}",4025%base_key)},4026"tree");4027if(defined$hash_base) {4028print" | ".4029$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4030 file_name=>"$basedir$t->{'name'}")},4031"history");4032}4033print"</td>\n";4034}else{4035# unknown object: we can only present history for it4036# (this includes 'commit' object, i.e. submodule support)4037print"<td class=\"list\">".4038 esc_path($t->{'name'}) .4039"</td>\n";4040print"<td class=\"link\">";4041if(defined$hash_base) {4042print$cgi->a({-href => href(action=>"history",4043 hash_base=>$hash_base,4044 file_name=>"$basedir$t->{'name'}")},4045"history");4046}4047print"</td>\n";4048}4049}40504051## ......................................................................4052## functions printing large fragments of HTML40534054# get pre-image filenames for merge (combined) diff4055sub fill_from_file_info {4056my($diff,@parents) =@_;40574058$diff->{'from_file'} = [ ];4059$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4060for(my$i=0;$i<$diff->{'nparents'};$i++) {4061if($diff->{'status'}[$i]eq'R'||4062$diff->{'status'}[$i]eq'C') {4063$diff->{'from_file'}[$i] =4064 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4065}4066}40674068return$diff;4069}40704071# is current raw difftree line of file deletion4072sub is_deleted {4073my$diffinfo=shift;40744075return$diffinfo->{'to_id'}eq('0' x 40);4076}40774078# does patch correspond to [previous] difftree raw line4079# $diffinfo - hashref of parsed raw diff format4080# $patchinfo - hashref of parsed patch diff format4081# (the same keys as in $diffinfo)4082sub is_patch_split {4083my($diffinfo,$patchinfo) =@_;40844085returndefined$diffinfo&&defined$patchinfo4086&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4087}408840894090sub git_difftree_body {4091my($difftree,$hash,@parents) =@_;4092my($parent) =$parents[0];4093my$have_blame= gitweb_check_feature('blame');4094print"<div class=\"list_head\">\n";4095if($#{$difftree} >10) {4096print(($#{$difftree} +1) ." files changed:\n");4097}4098print"</div>\n";40994100print"<table class=\"".4101(@parents>1?"combined ":"") .4102"diff_tree\">\n";41034104# header only for combined diff in 'commitdiff' view4105my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4106if($has_header) {4107# table header4108print"<thead><tr>\n".4109"<th></th><th></th>\n";# filename, patchN link4110for(my$i=0;$i<@parents;$i++) {4111my$par=$parents[$i];4112print"<th>".4113$cgi->a({-href => href(action=>"commitdiff",4114 hash=>$hash, hash_parent=>$par),4115-title =>'commitdiff to parent number '.4116($i+1) .': '.substr($par,0,7)},4117$i+1) .4118" </th>\n";4119}4120print"</tr></thead>\n<tbody>\n";4121}41224123my$alternate=1;4124my$patchno=0;4125foreachmy$line(@{$difftree}) {4126my$diff= parsed_difftree_line($line);41274128if($alternate) {4129print"<tr class=\"dark\">\n";4130}else{4131print"<tr class=\"light\">\n";4132}4133$alternate^=1;41344135if(exists$diff->{'nparents'}) {# combined diff41364137 fill_from_file_info($diff,@parents)4138unlessexists$diff->{'from_file'};41394140if(!is_deleted($diff)) {4141# file exists in the result (child) commit4142print"<td>".4143$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4144 file_name=>$diff->{'to_file'},4145 hash_base=>$hash),4146-class=>"list"}, esc_path($diff->{'to_file'})) .4147"</td>\n";4148}else{4149print"<td>".4150 esc_path($diff->{'to_file'}) .4151"</td>\n";4152}41534154if($actioneq'commitdiff') {4155# link to patch4156$patchno++;4157print"<td class=\"link\">".4158$cgi->a({-href =>"#patch$patchno"},"patch") .4159" | ".4160"</td>\n";4161}41624163my$has_history=0;4164my$not_deleted=0;4165for(my$i=0;$i<$diff->{'nparents'};$i++) {4166my$hash_parent=$parents[$i];4167my$from_hash=$diff->{'from_id'}[$i];4168my$from_path=$diff->{'from_file'}[$i];4169my$status=$diff->{'status'}[$i];41704171$has_history||= ($statusne'A');4172$not_deleted||= ($statusne'D');41734174if($statuseq'A') {4175print"<td class=\"link\"align=\"right\"> | </td>\n";4176}elsif($statuseq'D') {4177print"<td class=\"link\">".4178$cgi->a({-href => href(action=>"blob",4179 hash_base=>$hash,4180 hash=>$from_hash,4181 file_name=>$from_path)},4182"blob". ($i+1)) .4183" | </td>\n";4184}else{4185if($diff->{'to_id'}eq$from_hash) {4186print"<td class=\"link nochange\">";4187}else{4188print"<td class=\"link\">";4189}4190print$cgi->a({-href => href(action=>"blobdiff",4191 hash=>$diff->{'to_id'},4192 hash_parent=>$from_hash,4193 hash_base=>$hash,4194 hash_parent_base=>$hash_parent,4195 file_name=>$diff->{'to_file'},4196 file_parent=>$from_path)},4197"diff". ($i+1)) .4198" | </td>\n";4199}4200}42014202print"<td class=\"link\">";4203if($not_deleted) {4204print$cgi->a({-href => href(action=>"blob",4205 hash=>$diff->{'to_id'},4206 file_name=>$diff->{'to_file'},4207 hash_base=>$hash)},4208"blob");4209print" | "if($has_history);4210}4211if($has_history) {4212print$cgi->a({-href => href(action=>"history",4213 file_name=>$diff->{'to_file'},4214 hash_base=>$hash)},4215"history");4216}4217print"</td>\n";42184219print"</tr>\n";4220next;# instead of 'else' clause, to avoid extra indent4221}4222# else ordinary diff42234224my($to_mode_oct,$to_mode_str,$to_file_type);4225my($from_mode_oct,$from_mode_str,$from_file_type);4226if($diff->{'to_mode'}ne('0' x 6)) {4227$to_mode_oct=oct$diff->{'to_mode'};4228if(S_ISREG($to_mode_oct)) {# only for regular file4229$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4230}4231$to_file_type= file_type($diff->{'to_mode'});4232}4233if($diff->{'from_mode'}ne('0' x 6)) {4234$from_mode_oct=oct$diff->{'from_mode'};4235if(S_ISREG($to_mode_oct)) {# only for regular file4236$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4237}4238$from_file_type= file_type($diff->{'from_mode'});4239}42404241if($diff->{'status'}eq"A") {# created4242my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4243$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4244$mode_chng.="]</span>";4245print"<td>";4246print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4247 hash_base=>$hash, file_name=>$diff->{'file'}),4248-class=>"list"}, esc_path($diff->{'file'}));4249print"</td>\n";4250print"<td>$mode_chng</td>\n";4251print"<td class=\"link\">";4252if($actioneq'commitdiff') {4253# link to patch4254$patchno++;4255print$cgi->a({-href =>"#patch$patchno"},"patch");4256print" | ";4257}4258print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4259 hash_base=>$hash, file_name=>$diff->{'file'})},4260"blob");4261print"</td>\n";42624263}elsif($diff->{'status'}eq"D") {# deleted4264my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4265print"<td>";4266print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4267 hash_base=>$parent, file_name=>$diff->{'file'}),4268-class=>"list"}, esc_path($diff->{'file'}));4269print"</td>\n";4270print"<td>$mode_chng</td>\n";4271print"<td class=\"link\">";4272if($actioneq'commitdiff') {4273# link to patch4274$patchno++;4275print$cgi->a({-href =>"#patch$patchno"},"patch");4276print" | ";4277}4278print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4279 hash_base=>$parent, file_name=>$diff->{'file'})},4280"blob") ." | ";4281if($have_blame) {4282print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4283 file_name=>$diff->{'file'})},4284"blame") ." | ";4285}4286print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4287 file_name=>$diff->{'file'})},4288"history");4289print"</td>\n";42904291}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4292my$mode_chnge="";4293if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4294$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4295if($from_file_typene$to_file_type) {4296$mode_chnge.=" from$from_file_typeto$to_file_type";4297}4298if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4299if($from_mode_str&&$to_mode_str) {4300$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4301}elsif($to_mode_str) {4302$mode_chnge.=" mode:$to_mode_str";4303}4304}4305$mode_chnge.="]</span>\n";4306}4307print"<td>";4308print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4309 hash_base=>$hash, file_name=>$diff->{'file'}),4310-class=>"list"}, esc_path($diff->{'file'}));4311print"</td>\n";4312print"<td>$mode_chnge</td>\n";4313print"<td class=\"link\">";4314if($actioneq'commitdiff') {4315# link to patch4316$patchno++;4317print$cgi->a({-href =>"#patch$patchno"},"patch") .4318" | ";4319}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4320# "commit" view and modified file (not onlu mode changed)4321print$cgi->a({-href => href(action=>"blobdiff",4322 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4323 hash_base=>$hash, hash_parent_base=>$parent,4324 file_name=>$diff->{'file'})},4325"diff") .4326" | ";4327}4328print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4329 hash_base=>$hash, file_name=>$diff->{'file'})},4330"blob") ." | ";4331if($have_blame) {4332print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4333 file_name=>$diff->{'file'})},4334"blame") ." | ";4335}4336print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4337 file_name=>$diff->{'file'})},4338"history");4339print"</td>\n";43404341}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4342my%status_name= ('R'=>'moved','C'=>'copied');4343my$nstatus=$status_name{$diff->{'status'}};4344my$mode_chng="";4345if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4346# mode also for directories, so we cannot use $to_mode_str4347$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4348}4349print"<td>".4350$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4351 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4352-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4353"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4354$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4355 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4356-class=>"list"}, esc_path($diff->{'from_file'})) .4357" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4358"<td class=\"link\">";4359if($actioneq'commitdiff') {4360# link to patch4361$patchno++;4362print$cgi->a({-href =>"#patch$patchno"},"patch") .4363" | ";4364}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4365# "commit" view and modified file (not only pure rename or copy)4366print$cgi->a({-href => href(action=>"blobdiff",4367 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4368 hash_base=>$hash, hash_parent_base=>$parent,4369 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4370"diff") .4371" | ";4372}4373print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4374 hash_base=>$parent, file_name=>$diff->{'to_file'})},4375"blob") ." | ";4376if($have_blame) {4377print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4378 file_name=>$diff->{'to_file'})},4379"blame") ." | ";4380}4381print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4382 file_name=>$diff->{'to_file'})},4383"history");4384print"</td>\n";43854386}# we should not encounter Unmerged (U) or Unknown (X) status4387print"</tr>\n";4388}4389print"</tbody>"if$has_header;4390print"</table>\n";4391}43924393sub git_patchset_body {4394my($fd,$difftree,$hash,@hash_parents) =@_;4395my($hash_parent) =$hash_parents[0];43964397my$is_combined= (@hash_parents>1);4398my$patch_idx=0;4399my$patch_number=0;4400my$patch_line;4401my$diffinfo;4402my$to_name;4403my(%from,%to);44044405print"<div class=\"patchset\">\n";44064407# skip to first patch4408while($patch_line= <$fd>) {4409chomp$patch_line;44104411last if($patch_line=~m/^diff /);4412}44134414 PATCH:4415while($patch_line) {44164417# parse "git diff" header line4418if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4419# $1 is from_name, which we do not use4420$to_name= unquote($2);4421$to_name=~s!^b/!!;4422}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4423# $1 is 'cc' or 'combined', which we do not use4424$to_name= unquote($2);4425}else{4426$to_name=undef;4427}44284429# check if current patch belong to current raw line4430# and parse raw git-diff line if needed4431if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4432# this is continuation of a split patch4433print"<div class=\"patch cont\">\n";4434}else{4435# advance raw git-diff output if needed4436$patch_idx++ifdefined$diffinfo;44374438# read and prepare patch information4439$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);44404441# compact combined diff output can have some patches skipped4442# find which patch (using pathname of result) we are at now;4443if($is_combined) {4444while($to_namene$diffinfo->{'to_file'}) {4445print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4446 format_diff_cc_simplified($diffinfo,@hash_parents) .4447"</div>\n";# class="patch"44484449$patch_idx++;4450$patch_number++;44514452last if$patch_idx>$#$difftree;4453$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4454}4455}44564457# modifies %from, %to hashes4458 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);44594460# this is first patch for raw difftree line with $patch_idx index4461# we index @$difftree array from 0, but number patches from 14462print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4463}44644465# git diff header4466#assert($patch_line =~ m/^diff /) if DEBUG;4467#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4468$patch_number++;4469# print "git diff" header4470print format_git_diff_header_line($patch_line,$diffinfo,4471 \%from, \%to);44724473# print extended diff header4474print"<div class=\"diff extended_header\">\n";4475 EXTENDED_HEADER:4476while($patch_line= <$fd>) {4477chomp$patch_line;44784479last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);44804481print format_extended_diff_header_line($patch_line,$diffinfo,4482 \%from, \%to);4483}4484print"</div>\n";# class="diff extended_header"44854486# from-file/to-file diff header4487if(!$patch_line) {4488print"</div>\n";# class="patch"4489last PATCH;4490}4491next PATCH if($patch_line=~m/^diff /);4492#assert($patch_line =~ m/^---/) if DEBUG;44934494my$last_patch_line=$patch_line;4495$patch_line= <$fd>;4496chomp$patch_line;4497#assert($patch_line =~ m/^\+\+\+/) if DEBUG;44984499print format_diff_from_to_header($last_patch_line,$patch_line,4500$diffinfo, \%from, \%to,4501@hash_parents);45024503# the patch itself4504 LINE:4505while($patch_line= <$fd>) {4506chomp$patch_line;45074508next PATCH if($patch_line=~m/^diff /);45094510print format_diff_line($patch_line, \%from, \%to);4511}45124513}continue{4514print"</div>\n";# class="patch"4515}45164517# for compact combined (--cc) format, with chunk and patch simpliciaction4518# patchset might be empty, but there might be unprocessed raw lines4519for(++$patch_idxif$patch_number>0;4520$patch_idx<@$difftree;4521++$patch_idx) {4522# read and prepare patch information4523$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);45244525# generate anchor for "patch" links in difftree / whatchanged part4526print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4527 format_diff_cc_simplified($diffinfo,@hash_parents) .4528"</div>\n";# class="patch"45294530$patch_number++;4531}45324533if($patch_number==0) {4534if(@hash_parents>1) {4535print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4536}else{4537print"<div class=\"diff nodifferences\">No differences found</div>\n";4538}4539}45404541print"</div>\n";# class="patchset"4542}45434544# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45454546# fills project list info (age, description, owner, forks) for each4547# project in the list, removing invalid projects from returned list4548# NOTE: modifies $projlist, but does not remove entries from it4549sub fill_project_list_info {4550my($projlist,$check_forks) =@_;4551my@projects;45524553my$show_ctags= gitweb_check_feature('ctags');4554 PROJECT:4555foreachmy$pr(@$projlist) {4556my(@activity) = git_get_last_activity($pr->{'path'});4557unless(@activity) {4558next PROJECT;4559}4560($pr->{'age'},$pr->{'age_string'}) =@activity;4561if(!defined$pr->{'descr'}) {4562my$descr= git_get_project_description($pr->{'path'}) ||"";4563$descr= to_utf8($descr);4564$pr->{'descr_long'} =$descr;4565$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4566}4567if(!defined$pr->{'owner'}) {4568$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4569}4570if($check_forks) {4571my$pname=$pr->{'path'};4572if(($pname=~s/\.git$//) &&4573($pname!~/\/$/) &&4574(-d "$projectroot/$pname")) {4575$pr->{'forks'} ="-d$projectroot/$pname";4576}else{4577$pr->{'forks'} =0;4578}4579}4580$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4581push@projects,$pr;4582}45834584return@projects;4585}45864587# print 'sort by' <th> element, generating 'sort by $name' replay link4588# if that order is not selected4589sub print_sort_th {4590print format_sort_th(@_);4591}45924593sub format_sort_th {4594my($name,$order,$header) =@_;4595my$sort_th="";4596$header||=ucfirst($name);45974598if($ordereq$name) {4599$sort_th.="<th>$header</th>\n";4600}else{4601$sort_th.="<th>".4602$cgi->a({-href => href(-replay=>1, order=>$name),4603-class=>"header"},$header) .4604"</th>\n";4605}46064607return$sort_th;4608}46094610sub git_project_list_body {4611# actually uses global variable $project4612my($projlist,$order,$from,$to,$extra,$no_header) =@_;46134614my$check_forks= gitweb_check_feature('forks');4615my@projects= fill_project_list_info($projlist,$check_forks);46164617$order||=$default_projects_order;4618$from=0unlessdefined$from;4619$to=$#projectsif(!defined$to||$#projects<$to);46204621my%order_info= (4622 project => { key =>'path', type =>'str'},4623 descr => { key =>'descr_long', type =>'str'},4624 owner => { key =>'owner', type =>'str'},4625 age => { key =>'age', type =>'num'}4626);4627my$oi=$order_info{$order};4628if($oi->{'type'}eq'str') {4629@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4630}else{4631@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4632}46334634my$show_ctags= gitweb_check_feature('ctags');4635if($show_ctags) {4636my%ctags;4637foreachmy$p(@projects) {4638foreachmy$ct(keys%{$p->{'ctags'}}) {4639$ctags{$ct} +=$p->{'ctags'}->{$ct};4640}4641}4642my$cloud= git_populate_project_tagcloud(\%ctags);4643print git_show_project_tagcloud($cloud,64);4644}46454646print"<table class=\"project_list\">\n";4647unless($no_header) {4648print"<tr>\n";4649if($check_forks) {4650print"<th></th>\n";4651}4652 print_sort_th('project',$order,'Project');4653 print_sort_th('descr',$order,'Description');4654 print_sort_th('owner',$order,'Owner');4655 print_sort_th('age',$order,'Last Change');4656print"<th></th>\n".# for links4657"</tr>\n";4658}4659my$alternate=1;4660my$tagfilter=$cgi->param('by_tag');4661for(my$i=$from;$i<=$to;$i++) {4662my$pr=$projects[$i];46634664next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4665next if$searchtextand not$pr->{'path'} =~/$searchtext/4666and not$pr->{'descr_long'} =~/$searchtext/;4667# Weed out forks or non-matching entries of search4668if($check_forks) {4669my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4670$forkbase="^$forkbase"if$forkbase;4671next ifnot$searchtextand not$tagfilterand$show_ctags4672and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4673}46744675if($alternate) {4676print"<tr class=\"dark\">\n";4677}else{4678print"<tr class=\"light\">\n";4679}4680$alternate^=1;4681if($check_forks) {4682print"<td>";4683if($pr->{'forks'}) {4684print"<!--$pr->{'forks'} -->\n";4685print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4686}4687print"</td>\n";4688}4689print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4690-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4691"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4692-class=>"list", -title =>$pr->{'descr_long'}},4693 esc_html($pr->{'descr'})) ."</td>\n".4694"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4695print"<td class=\"". age_class($pr->{'age'}) ."\">".4696(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4697"<td class=\"link\">".4698$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4699$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4700$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4701$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4702($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4703"</td>\n".4704"</tr>\n";4705}4706if(defined$extra) {4707print"<tr>\n";4708if($check_forks) {4709print"<td></td>\n";4710}4711print"<td colspan=\"5\">$extra</td>\n".4712"</tr>\n";4713}4714print"</table>\n";4715}47164717sub git_log_body {4718# uses global variable $project4719my($commitlist,$from,$to,$refs,$extra) =@_;47204721$from=0unlessdefined$from;4722$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47234724for(my$i=0;$i<=$to;$i++) {4725my%co= %{$commitlist->[$i]};4726next if!%co;4727my$commit=$co{'id'};4728my$ref= format_ref_marker($refs,$commit);4729my%ad= parse_date($co{'author_epoch'});4730 git_print_header_div('commit',4731"<span class=\"age\">$co{'age_string'}</span>".4732 esc_html($co{'title'}) .$ref,4733$commit);4734print"<div class=\"title_text\">\n".4735"<div class=\"log_link\">\n".4736$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4737" | ".4738$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4739" | ".4740$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4741"<br/>\n".4742"</div>\n";4743 git_print_authorship(\%co, -tag =>'span');4744print"<br/>\n</div>\n";47454746print"<div class=\"log_body\">\n";4747 git_print_log($co{'comment'}, -final_empty_line=>1);4748print"</div>\n";4749}4750if($extra) {4751print"<div class=\"page_nav\">\n";4752print"$extra\n";4753print"</div>\n";4754}4755}47564757sub git_shortlog_body {4758# uses global variable $project4759my($commitlist,$from,$to,$refs,$extra) =@_;47604761$from=0unlessdefined$from;4762$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47634764print"<table class=\"shortlog\">\n";4765my$alternate=1;4766for(my$i=$from;$i<=$to;$i++) {4767my%co= %{$commitlist->[$i]};4768my$commit=$co{'id'};4769my$ref= format_ref_marker($refs,$commit);4770if($alternate) {4771print"<tr class=\"dark\">\n";4772}else{4773print"<tr class=\"light\">\n";4774}4775$alternate^=1;4776# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4777print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4778 format_author_html('td', \%co,10) ."<td>";4779print format_subject_html($co{'title'},$co{'title_short'},4780 href(action=>"commit", hash=>$commit),$ref);4781print"</td>\n".4782"<td class=\"link\">".4783$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4784$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4785$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4786my$snapshot_links= format_snapshot_links($commit);4787if(defined$snapshot_links) {4788print" | ".$snapshot_links;4789}4790print"</td>\n".4791"</tr>\n";4792}4793if(defined$extra) {4794print"<tr>\n".4795"<td colspan=\"4\">$extra</td>\n".4796"</tr>\n";4797}4798print"</table>\n";4799}48004801sub git_history_body {4802# Warning: assumes constant type (blob or tree) during history4803my($commitlist,$from,$to,$refs,$extra,4804$file_name,$file_hash,$ftype) =@_;48054806$from=0unlessdefined$from;4807$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});48084809print"<table class=\"history\">\n";4810my$alternate=1;4811for(my$i=$from;$i<=$to;$i++) {4812my%co= %{$commitlist->[$i]};4813if(!%co) {4814next;4815}4816my$commit=$co{'id'};48174818my$ref= format_ref_marker($refs,$commit);48194820if($alternate) {4821print"<tr class=\"dark\">\n";4822}else{4823print"<tr class=\"light\">\n";4824}4825$alternate^=1;4826print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4827# shortlog: format_author_html('td', \%co, 10)4828 format_author_html('td', \%co,15,3) ."<td>";4829# originally git_history used chop_str($co{'title'}, 50)4830print format_subject_html($co{'title'},$co{'title_short'},4831 href(action=>"commit", hash=>$commit),$ref);4832print"</td>\n".4833"<td class=\"link\">".4834$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4835$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");48364837if($ftypeeq'blob') {4838my$blob_current=$file_hash;4839my$blob_parent= git_get_hash_by_path($commit,$file_name);4840if(defined$blob_current&&defined$blob_parent&&4841$blob_currentne$blob_parent) {4842print" | ".4843$cgi->a({-href => href(action=>"blobdiff",4844 hash=>$blob_current, hash_parent=>$blob_parent,4845 hash_base=>$hash_base, hash_parent_base=>$commit,4846 file_name=>$file_name)},4847"diff to current");4848}4849}4850print"</td>\n".4851"</tr>\n";4852}4853if(defined$extra) {4854print"<tr>\n".4855"<td colspan=\"4\">$extra</td>\n".4856"</tr>\n";4857}4858print"</table>\n";4859}48604861sub git_tags_body {4862# uses global variable $project4863my($taglist,$from,$to,$extra) =@_;4864$from=0unlessdefined$from;4865$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);48664867print"<table class=\"tags\">\n";4868my$alternate=1;4869for(my$i=$from;$i<=$to;$i++) {4870my$entry=$taglist->[$i];4871my%tag=%$entry;4872my$comment=$tag{'subject'};4873my$comment_short;4874if(defined$comment) {4875$comment_short= chop_str($comment,30,5);4876}4877if($alternate) {4878print"<tr class=\"dark\">\n";4879}else{4880print"<tr class=\"light\">\n";4881}4882$alternate^=1;4883if(defined$tag{'age'}) {4884print"<td><i>$tag{'age'}</i></td>\n";4885}else{4886print"<td></td>\n";4887}4888print"<td>".4889$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4890-class=>"list name"}, esc_html($tag{'name'})) .4891"</td>\n".4892"<td>";4893if(defined$comment) {4894print format_subject_html($comment,$comment_short,4895 href(action=>"tag", hash=>$tag{'id'}));4896}4897print"</td>\n".4898"<td class=\"selflink\">";4899if($tag{'type'}eq"tag") {4900print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4901}else{4902print" ";4903}4904print"</td>\n".4905"<td class=\"link\">"." | ".4906$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4907if($tag{'reftype'}eq"commit") {4908print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4909" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4910}elsif($tag{'reftype'}eq"blob") {4911print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4912}4913print"</td>\n".4914"</tr>";4915}4916if(defined$extra) {4917print"<tr>\n".4918"<td colspan=\"5\">$extra</td>\n".4919"</tr>\n";4920}4921print"</table>\n";4922}49234924sub git_heads_body {4925# uses global variable $project4926my($headlist,$head,$from,$to,$extra) =@_;4927$from=0unlessdefined$from;4928$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);49294930print"<table class=\"heads\">\n";4931my$alternate=1;4932for(my$i=$from;$i<=$to;$i++) {4933my$entry=$headlist->[$i];4934my%ref=%$entry;4935my$curr=$ref{'id'}eq$head;4936if($alternate) {4937print"<tr class=\"dark\">\n";4938}else{4939print"<tr class=\"light\">\n";4940}4941$alternate^=1;4942print"<td><i>$ref{'age'}</i></td>\n".4943($curr?"<td class=\"current_head\">":"<td>") .4944$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4945-class=>"list name"},esc_html($ref{'name'})) .4946"</td>\n".4947"<td class=\"link\">".4948$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4949$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4950$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4951"</td>\n".4952"</tr>";4953}4954if(defined$extra) {4955print"<tr>\n".4956"<td colspan=\"3\">$extra</td>\n".4957"</tr>\n";4958}4959print"</table>\n";4960}49614962sub git_search_grep_body {4963my($commitlist,$from,$to,$extra) =@_;4964$from=0unlessdefined$from;4965$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);49664967print"<table class=\"commit_search\">\n";4968my$alternate=1;4969for(my$i=$from;$i<=$to;$i++) {4970my%co= %{$commitlist->[$i]};4971if(!%co) {4972next;4973}4974my$commit=$co{'id'};4975if($alternate) {4976print"<tr class=\"dark\">\n";4977}else{4978print"<tr class=\"light\">\n";4979}4980$alternate^=1;4981print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4982 format_author_html('td', \%co,15,5) .4983"<td>".4984$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4985-class=>"list subject"},4986 chop_and_escape_str($co{'title'},50) ."<br/>");4987my$comment=$co{'comment'};4988foreachmy$line(@$comment) {4989if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4990my($lead,$match,$trail) = ($1,$2,$3);4991$match= chop_str($match,70,5,'center');4992my$contextlen=int((80-length($match))/2);4993$contextlen=30if($contextlen>30);4994$lead= chop_str($lead,$contextlen,10,'left');4995$trail= chop_str($trail,$contextlen,10,'right');49964997$lead= esc_html($lead);4998$match= esc_html($match);4999$trail= esc_html($trail);50005001print"$lead<span class=\"match\">$match</span>$trail<br />";5002}5003}5004print"</td>\n".5005"<td class=\"link\">".5006$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5007" | ".5008$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .5009" | ".5010$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5011print"</td>\n".5012"</tr>\n";5013}5014if(defined$extra) {5015print"<tr>\n".5016"<td colspan=\"3\">$extra</td>\n".5017"</tr>\n";5018}5019print"</table>\n";5020}50215022## ======================================================================5023## ======================================================================5024## actions50255026sub git_project_list {5027my$order=$input_params{'order'};5028if(defined$order&&$order!~m/none|project|descr|owner|age/) {5029 die_error(400,"Unknown order parameter");5030}50315032my@list= git_get_projects_list();5033if(!@list) {5034 die_error(404,"No projects found");5035}50365037 git_header_html();5038if(defined$home_text&& -f $home_text) {5039print"<div class=\"index_include\">\n";5040 insert_file($home_text);5041print"</div>\n";5042}5043print$cgi->startform(-method=>"get") .5044"<p class=\"projsearch\">Search:\n".5045$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".5046"</p>".5047$cgi->end_form() ."\n";5048 git_project_list_body(\@list,$order);5049 git_footer_html();5050}50515052sub git_forks {5053my$order=$input_params{'order'};5054if(defined$order&&$order!~m/none|project|descr|owner|age/) {5055 die_error(400,"Unknown order parameter");5056}50575058my@list= git_get_projects_list($project);5059if(!@list) {5060 die_error(404,"No forks found");5061}50625063 git_header_html();5064 git_print_page_nav('','');5065 git_print_header_div('summary',"$projectforks");5066 git_project_list_body(\@list,$order);5067 git_footer_html();5068}50695070sub git_project_index {5071my@projects= git_get_projects_list($project);50725073print$cgi->header(5074-type =>'text/plain',5075-charset =>'utf-8',5076-content_disposition =>'inline; filename="index.aux"');50775078foreachmy$pr(@projects) {5079if(!exists$pr->{'owner'}) {5080$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");5081}50825083my($path,$owner) = ($pr->{'path'},$pr->{'owner'});5084# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '5085$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5086$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5087$path=~s/ /\+/g;5088$owner=~s/ /\+/g;50895090print"$path$owner\n";5091}5092}50935094sub git_summary {5095my$descr= git_get_project_description($project) ||"none";5096my%co= parse_commit("HEAD");5097my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();5098my$head=$co{'id'};50995100my$owner= git_get_project_owner($project);51015102my$refs= git_get_references();5103# These get_*_list functions return one more to allow us to see if5104# there are more ...5105my@taglist= git_get_tags_list(16);5106my@headlist= git_get_heads_list(16);5107my@forklist;5108my$check_forks= gitweb_check_feature('forks');51095110if($check_forks) {5111@forklist= git_get_projects_list($project);5112}51135114 git_header_html();5115 git_print_page_nav('summary','',$head);51165117print"<div class=\"title\"> </div>\n";5118print"<table class=\"projects_list\">\n".5119"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".5120"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";5121if(defined$cd{'rfc2822'}) {5122print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";5123}51245125# use per project git URL list in $projectroot/$project/cloneurl5126# or make project git URL from git base URL and project name5127my$url_tag="URL";5128my@url_list= git_get_project_url_list($project);5129@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;5130foreachmy$git_url(@url_list) {5131next unless$git_url;5132print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";5133$url_tag="";5134}51355136# Tag cloud5137my$show_ctags= gitweb_check_feature('ctags');5138if($show_ctags) {5139my$ctags= git_get_project_ctags($project);5140my$cloud= git_populate_project_tagcloud($ctags);5141print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";5142print"</td>\n<td>"unless%$ctags;5143print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";5144print"</td>\n<td>"if%$ctags;5145print git_show_project_tagcloud($cloud,48);5146print"</td></tr>";5147}51485149print"</table>\n";51505151# If XSS prevention is on, we don't include README.html.5152# TODO: Allow a readme in some safe format.5153if(!$prevent_xss&& -s "$projectroot/$project/README.html") {5154print"<div class=\"title\">readme</div>\n".5155"<div class=\"readme\">\n";5156 insert_file("$projectroot/$project/README.html");5157print"\n</div>\n";# class="readme"5158}51595160# we need to request one more than 16 (0..15) to check if5161# those 16 are all5162my@commitlist=$head? parse_commits($head,17) : ();5163if(@commitlist) {5164 git_print_header_div('shortlog');5165 git_shortlog_body(\@commitlist,0,15,$refs,5166$#commitlist<=15?undef:5167$cgi->a({-href => href(action=>"shortlog")},"..."));5168}51695170if(@taglist) {5171 git_print_header_div('tags');5172 git_tags_body(\@taglist,0,15,5173$#taglist<=15?undef:5174$cgi->a({-href => href(action=>"tags")},"..."));5175}51765177if(@headlist) {5178 git_print_header_div('heads');5179 git_heads_body(\@headlist,$head,0,15,5180$#headlist<=15?undef:5181$cgi->a({-href => href(action=>"heads")},"..."));5182}51835184if(@forklist) {5185 git_print_header_div('forks');5186 git_project_list_body(\@forklist,'age',0,15,5187$#forklist<=15?undef:5188$cgi->a({-href => href(action=>"forks")},"..."),5189'no_header');5190}51915192 git_footer_html();5193}51945195sub git_tag {5196my$head= git_get_head_hash($project);5197 git_header_html();5198 git_print_page_nav('','',$head,undef,$head);5199my%tag= parse_tag($hash);52005201if(!%tag) {5202 die_error(404,"Unknown tag object");5203}52045205 git_print_header_div('commit', esc_html($tag{'name'}),$hash);5206print"<div class=\"title_text\">\n".5207"<table class=\"object_header\">\n".5208"<tr>\n".5209"<td>object</td>\n".5210"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5211$tag{'object'}) ."</td>\n".5212"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5213$tag{'type'}) ."</td>\n".5214"</tr>\n";5215if(defined($tag{'author'})) {5216 git_print_authorship_rows(\%tag,'author');5217}5218print"</table>\n\n".5219"</div>\n";5220print"<div class=\"page_body\">";5221my$comment=$tag{'comment'};5222foreachmy$line(@$comment) {5223chomp$line;5224print esc_html($line, -nbsp=>1) ."<br/>\n";5225}5226print"</div>\n";5227 git_footer_html();5228}52295230sub git_blame_common {5231my$format=shift||'porcelain';5232if($formateq'porcelain'&&$cgi->param('js')) {5233$format='incremental';5234$action='blame_incremental';# for page title etc5235}52365237# permissions5238 gitweb_check_feature('blame')5239or die_error(403,"Blame view not allowed");52405241# error checking5242 die_error(400,"No file name given")unless$file_name;5243$hash_base||= git_get_head_hash($project);5244 die_error(404,"Couldn't find base commit")unless$hash_base;5245my%co= parse_commit($hash_base)5246or die_error(404,"Commit not found");5247my$ftype="blob";5248if(!defined$hash) {5249$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5250or die_error(404,"Error looking up file");5251}else{5252$ftype= git_get_type($hash);5253if($ftype!~"blob") {5254 die_error(400,"Object is not a blob");5255}5256}52575258my$fd;5259if($formateq'incremental') {5260# get file contents (as base)5261open$fd,"-|", git_cmd(),'cat-file','blob',$hash5262or die_error(500,"Open git-cat-file failed");5263}elsif($formateq'data') {5264# run git-blame --incremental5265open$fd,"-|", git_cmd(),"blame","--incremental",5266$hash_base,"--",$file_name5267or die_error(500,"Open git-blame --incremental failed");5268}else{5269# run git-blame --porcelain5270open$fd,"-|", git_cmd(),"blame",'-p',5271$hash_base,'--',$file_name5272or die_error(500,"Open git-blame --porcelain failed");5273}52745275# incremental blame data returns early5276if($formateq'data') {5277print$cgi->header(5278-type=>"text/plain", -charset =>"utf-8",5279-status=>"200 OK");5280local$| =1;# output autoflush5281printwhile<$fd>;5282close$fd5283or print"ERROR$!\n";52845285print'END';5286if(defined$t0&& gitweb_check_feature('timed')) {5287print' '.5288 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5289' '.$number_of_git_cmds;5290}5291print"\n";52925293return;5294}52955296# page header5297 git_header_html();5298my$formats_nav=5299$cgi->a({-href => href(action=>"blob", -replay=>1)},5300"blob") .5301" | ";5302if($formateq'incremental') {5303$formats_nav.=5304$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5305"blame") ." (non-incremental)";5306}else{5307$formats_nav.=5308$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5309"blame") ." (incremental)";5310}5311$formats_nav.=5312" | ".5313$cgi->a({-href => href(action=>"history", -replay=>1)},5314"history") .5315" | ".5316$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5317"HEAD");5318 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5319 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5320 git_print_page_path($file_name,$ftype,$hash_base);53215322# page body5323if($formateq'incremental') {5324print"<noscript>\n<div class=\"error\"><center><b>\n".5325"This page requires JavaScript to run.\nUse ".5326$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5327'this page').5328" instead.\n".5329"</b></center></div>\n</noscript>\n";53305331print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5332}53335334print qq!<div class="page_body">\n!;5335print qq!<div id="progress_info">.../ ...</div>\n!5336if($formateq'incremental');5337print qq!<table id="blame_table"class="blame" width="100%">\n!.5338#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5339 qq!<thead>\n!.5340 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5341 qq!</thead>\n!.5342 qq!<tbody>\n!;53435344my@rev_color=qw(light dark);5345my$num_colors=scalar(@rev_color);5346my$current_color=0;53475348if($formateq'incremental') {5349my$color_class=$rev_color[$current_color];53505351#contents of a file5352my$linenr=0;5353 LINE:5354while(my$line= <$fd>) {5355chomp$line;5356$linenr++;53575358print qq!<tr id="l$linenr"class="$color_class">!.5359 qq!<td class="sha1"><a href=""> </a></td>!.5360 qq!<td class="linenr">!.5361 qq!<a class="linenr" href="">$linenr</a></td>!;5362print qq!<td class="pre">! . esc_html($line) ."</td>\n";5363print qq!</tr>\n!;5364}53655366}else{# porcelain, i.e. ordinary blame5367my%metainfo= ();# saves information about commits53685369# blame data5370 LINE:5371while(my$line= <$fd>) {5372chomp$line;5373# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5374# no <lines in group> for subsequent lines in group of lines5375my($full_rev,$orig_lineno,$lineno,$group_size) =5376($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5377if(!exists$metainfo{$full_rev}) {5378$metainfo{$full_rev} = {'nprevious'=>0};5379}5380my$meta=$metainfo{$full_rev};5381my$data;5382while($data= <$fd>) {5383chomp$data;5384last if($data=~s/^\t//);# contents of line5385if($data=~/^(\S+)(?: (.*))?$/) {5386$meta->{$1} =$2unlessexists$meta->{$1};5387}5388if($data=~/^previous /) {5389$meta->{'nprevious'}++;5390}5391}5392my$short_rev=substr($full_rev,0,8);5393my$author=$meta->{'author'};5394my%date=5395 parse_date($meta->{'author-time'},$meta->{'author-tz'});5396my$date=$date{'iso-tz'};5397if($group_size) {5398$current_color= ($current_color+1) %$num_colors;5399}5400my$tr_class=$rev_color[$current_color];5401$tr_class.=' boundary'if(exists$meta->{'boundary'});5402$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5403$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5404print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5405if($group_size) {5406print"<td class=\"sha1\"";5407print" title=\"". esc_html($author) .",$date\"";5408print" rowspan=\"$group_size\""if($group_size>1);5409print">";5410print$cgi->a({-href => href(action=>"commit",5411 hash=>$full_rev,5412 file_name=>$file_name)},5413 esc_html($short_rev));5414if($group_size>=2) {5415my@author_initials= ($author=~/\b([[:upper:]])\B/g);5416if(@author_initials) {5417print"<br />".5418 esc_html(join('',@author_initials));5419# or join('.', ...)5420}5421}5422print"</td>\n";5423}5424# 'previous' <sha1 of parent commit> <filename at commit>5425if(exists$meta->{'previous'} &&5426$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5427$meta->{'parent'} =$1;5428$meta->{'file_parent'} = unquote($2);5429}5430my$linenr_commit=5431exists($meta->{'parent'}) ?5432$meta->{'parent'} :$full_rev;5433my$linenr_filename=5434exists($meta->{'file_parent'}) ?5435$meta->{'file_parent'} : unquote($meta->{'filename'});5436my$blamed= href(action =>'blame',5437 file_name =>$linenr_filename,5438 hash_base =>$linenr_commit);5439print"<td class=\"linenr\">";5440print$cgi->a({ -href =>"$blamed#l$orig_lineno",5441-class=>"linenr"},5442 esc_html($lineno));5443print"</td>";5444print"<td class=\"pre\">". esc_html($data) ."</td>\n";5445print"</tr>\n";5446}# end while54475448}54495450# footer5451print"</tbody>\n".5452"</table>\n";# class="blame"5453print"</div>\n";# class="blame_body"5454close$fd5455or print"Reading blob failed\n";54565457 git_footer_html();5458}54595460sub git_blame {5461 git_blame_common();5462}54635464sub git_blame_incremental {5465 git_blame_common('incremental');5466}54675468sub git_blame_data {5469 git_blame_common('data');5470}54715472sub git_tags {5473my$head= git_get_head_hash($project);5474 git_header_html();5475 git_print_page_nav('','',$head,undef,$head);5476 git_print_header_div('summary',$project);54775478my@tagslist= git_get_tags_list();5479if(@tagslist) {5480 git_tags_body(\@tagslist);5481}5482 git_footer_html();5483}54845485sub git_heads {5486my$head= git_get_head_hash($project);5487 git_header_html();5488 git_print_page_nav('','',$head,undef,$head);5489 git_print_header_div('summary',$project);54905491my@headslist= git_get_heads_list();5492if(@headslist) {5493 git_heads_body(\@headslist,$head);5494}5495 git_footer_html();5496}54975498sub git_blob_plain {5499my$type=shift;5500my$expires;55015502if(!defined$hash) {5503if(defined$file_name) {5504my$base=$hash_base|| git_get_head_hash($project);5505$hash= git_get_hash_by_path($base,$file_name,"blob")5506or die_error(404,"Cannot find file");5507}else{5508 die_error(400,"No file name defined");5509}5510}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5511# blobs defined by non-textual hash id's can be cached5512$expires="+1d";5513}55145515open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5516or die_error(500,"Open git-cat-file blob '$hash' failed");55175518# content-type (can include charset)5519$type= blob_contenttype($fd,$file_name,$type);55205521# "save as" filename, even when no $file_name is given5522my$save_as="$hash";5523if(defined$file_name) {5524$save_as=$file_name;5525}elsif($type=~m/^text\//) {5526$save_as.='.txt';5527}55285529# With XSS prevention on, blobs of all types except a few known safe5530# ones are served with "Content-Disposition: attachment" to make sure5531# they don't run in our security domain. For certain image types,5532# blob view writes an <img> tag referring to blob_plain view, and we5533# want to be sure not to break that by serving the image as an5534# attachment (though Firefox 3 doesn't seem to care).5535my$sandbox=$prevent_xss&&5536$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;55375538print$cgi->header(5539-type =>$type,5540-expires =>$expires,5541-content_disposition =>5542($sandbox?'attachment':'inline')5543.'; filename="'.$save_as.'"');5544local$/=undef;5545binmode STDOUT,':raw';5546print<$fd>;5547binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5548close$fd;5549}55505551sub git_blob {5552my$expires;55535554if(!defined$hash) {5555if(defined$file_name) {5556my$base=$hash_base|| git_get_head_hash($project);5557$hash= git_get_hash_by_path($base,$file_name,"blob")5558or die_error(404,"Cannot find file");5559}else{5560 die_error(400,"No file name defined");5561}5562}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5563# blobs defined by non-textual hash id's can be cached5564$expires="+1d";5565}55665567my$have_blame= gitweb_check_feature('blame');5568open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5569or die_error(500,"Couldn't cat$file_name,$hash");5570my$mimetype= blob_mimetype($fd,$file_name);5571# use 'blob_plain' (aka 'raw') view for files that cannot be displayed5572if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5573close$fd;5574return git_blob_plain($mimetype);5575}5576# we can have blame only for text/* mimetype5577$have_blame&&= ($mimetype=~m!^text/!);55785579my$highlight= gitweb_check_feature('highlight');5580my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);5581$fd= run_highlighter($fd,$highlight,$syntax)5582if$syntax;55835584 git_header_html(undef,$expires);5585my$formats_nav='';5586if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5587if(defined$file_name) {5588if($have_blame) {5589$formats_nav.=5590$cgi->a({-href => href(action=>"blame", -replay=>1)},5591"blame") .5592" | ";5593}5594$formats_nav.=5595$cgi->a({-href => href(action=>"history", -replay=>1)},5596"history") .5597" | ".5598$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5599"raw") .5600" | ".5601$cgi->a({-href => href(action=>"blob",5602 hash_base=>"HEAD", file_name=>$file_name)},5603"HEAD");5604}else{5605$formats_nav.=5606$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5607"raw");5608}5609 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5610 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5611}else{5612print"<div class=\"page_nav\">\n".5613"<br/><br/></div>\n".5614"<div class=\"title\">$hash</div>\n";5615}5616 git_print_page_path($file_name,"blob",$hash_base);5617print"<div class=\"page_body\">\n";5618if($mimetype=~m!^image/!) {5619print qq!<img type="$mimetype"!;5620if($file_name) {5621print qq! alt="$file_name" title="$file_name"!;5622}5623print qq! src="! .5624 href(action=>"blob_plain", hash=>$hash,5625 hash_base=>$hash_base, file_name=>$file_name) .5626 qq!"/>\n!;5627}else{5628my$nr;5629while(my$line= <$fd>) {5630chomp$line;5631$nr++;5632$line= untabify($line);5633printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,5634$nr, href(-replay =>1),$nr,$nr,$syntax?$line: esc_html($line, -nbsp=>1);5635}5636}5637close$fd5638or print"Reading blob failed.\n";5639print"</div>";5640 git_footer_html();5641}56425643sub git_tree {5644if(!defined$hash_base) {5645$hash_base="HEAD";5646}5647if(!defined$hash) {5648if(defined$file_name) {5649$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5650}else{5651$hash=$hash_base;5652}5653}5654 die_error(404,"No such tree")unlessdefined($hash);56555656my$show_sizes= gitweb_check_feature('show-sizes');5657my$have_blame= gitweb_check_feature('blame');56585659my@entries= ();5660{5661local$/="\0";5662open my$fd,"-|", git_cmd(),"ls-tree",'-z',5663($show_sizes?'-l': ()),@extra_options,$hash5664or die_error(500,"Open git-ls-tree failed");5665@entries=map{chomp;$_} <$fd>;5666close$fd5667or die_error(404,"Reading tree failed");5668}56695670my$refs= git_get_references();5671my$ref= format_ref_marker($refs,$hash_base);5672 git_header_html();5673my$basedir='';5674if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5675my@views_nav= ();5676if(defined$file_name) {5677push@views_nav,5678$cgi->a({-href => href(action=>"history", -replay=>1)},5679"history"),5680$cgi->a({-href => href(action=>"tree",5681 hash_base=>"HEAD", file_name=>$file_name)},5682"HEAD"),5683}5684my$snapshot_links= format_snapshot_links($hash);5685if(defined$snapshot_links) {5686# FIXME: Should be available when we have no hash base as well.5687push@views_nav,$snapshot_links;5688}5689 git_print_page_nav('tree','',$hash_base,undef,undef,5690join(' | ',@views_nav));5691 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5692}else{5693undef$hash_base;5694print"<div class=\"page_nav\">\n";5695print"<br/><br/></div>\n";5696print"<div class=\"title\">$hash</div>\n";5697}5698if(defined$file_name) {5699$basedir=$file_name;5700if($basedirne''&&substr($basedir, -1)ne'/') {5701$basedir.='/';5702}5703 git_print_page_path($file_name,'tree',$hash_base);5704}5705print"<div class=\"page_body\">\n";5706print"<table class=\"tree\">\n";5707my$alternate=1;5708# '..' (top directory) link if possible5709if(defined$hash_base&&5710defined$file_name&&$file_name=~m![^/]+$!) {5711if($alternate) {5712print"<tr class=\"dark\">\n";5713}else{5714print"<tr class=\"light\">\n";5715}5716$alternate^=1;57175718my$up=$file_name;5719$up=~s!/?[^/]+$!!;5720undef$upunless$up;5721# based on git_print_tree_entry5722print'<td class="mode">'. mode_str('040000') ."</td>\n";5723print'<td class="size"> </td>'."\n"if$show_sizes;5724print'<td class="list">';5725print$cgi->a({-href => href(action=>"tree",5726 hash_base=>$hash_base,5727 file_name=>$up)},5728"..");5729print"</td>\n";5730print"<td class=\"link\"></td>\n";57315732print"</tr>\n";5733}5734foreachmy$line(@entries) {5735my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);57365737if($alternate) {5738print"<tr class=\"dark\">\n";5739}else{5740print"<tr class=\"light\">\n";5741}5742$alternate^=1;57435744 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);57455746print"</tr>\n";5747}5748print"</table>\n".5749"</div>";5750 git_footer_html();5751}57525753sub snapshot_name {5754my($project,$hash) =@_;57555756# path/to/project.git -> project5757# path/to/project/.git -> project5758my$name= to_utf8($project);5759$name=~ s,([^/])/*\.git$,$1,;5760$name= basename($name);5761# sanitize name5762$name=~s/[[:cntrl:]]/?/g;57635764my$ver=$hash;5765if($hash=~/^[0-9a-fA-F]+$/) {5766# shorten SHA-1 hash5767my$full_hash= git_get_full_hash($project,$hash);5768if($full_hash=~/^$hash/&&length($hash) >7) {5769$ver= git_get_short_hash($project,$hash);5770}5771}elsif($hash=~m!^refs/tags/(.*)$!) {5772# tags don't need shortened SHA-1 hash5773$ver=$1;5774}else{5775# branches and other need shortened SHA-1 hash5776if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5777$ver=$1;5778}5779$ver.='-'. git_get_short_hash($project,$hash);5780}5781# in case of hierarchical branch names5782$ver=~s!/!.!g;57835784# name = project-version_string5785$name="$name-$ver";57865787returnwantarray? ($name,$name) :$name;5788}57895790sub git_snapshot {5791my$format=$input_params{'snapshot_format'};5792if(!@snapshot_fmts) {5793 die_error(403,"Snapshots not allowed");5794}5795# default to first supported snapshot format5796$format||=$snapshot_fmts[0];5797if($format!~m/^[a-z0-9]+$/) {5798 die_error(400,"Invalid snapshot format parameter");5799}elsif(!exists($known_snapshot_formats{$format})) {5800 die_error(400,"Unknown snapshot format");5801}elsif($known_snapshot_formats{$format}{'disabled'}) {5802 die_error(403,"Snapshot format not allowed");5803}elsif(!grep($_eq$format,@snapshot_fmts)) {5804 die_error(403,"Unsupported snapshot format");5805}58065807my$type= git_get_type("$hash^{}");5808if(!$type) {5809 die_error(404,'Object does not exist');5810}elsif($typeeq'blob') {5811 die_error(400,'Object is not a tree-ish');5812}58135814my($name,$prefix) = snapshot_name($project,$hash);5815my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5816my$cmd= quote_command(5817 git_cmd(),'archive',5818"--format=$known_snapshot_formats{$format}{'format'}",5819"--prefix=$prefix/",$hash);5820if(exists$known_snapshot_formats{$format}{'compressor'}) {5821$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5822}58235824$filename=~s/(["\\])/\\$1/g;5825print$cgi->header(5826-type =>$known_snapshot_formats{$format}{'type'},5827-content_disposition =>'inline; filename="'.$filename.'"',5828-status =>'200 OK');58295830open my$fd,"-|",$cmd5831or die_error(500,"Execute git-archive failed");5832binmode STDOUT,':raw';5833print<$fd>;5834binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5835close$fd;5836}58375838sub git_log_generic {5839my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;58405841my$head= git_get_head_hash($project);5842if(!defined$base) {5843$base=$head;5844}5845if(!defined$page) {5846$page=0;5847}5848my$refs= git_get_references();58495850my$commit_hash=$base;5851if(defined$parent) {5852$commit_hash="$parent..$base";5853}5854my@commitlist=5855 parse_commits($commit_hash,101, (100*$page),5856defined$file_name? ($file_name,"--full-history") : ());58575858my$ftype;5859if(!defined$file_hash&&defined$file_name) {5860# some commits could have deleted file in question,5861# and not have it in tree, but one of them has to have it5862for(my$i=0;$i<@commitlist;$i++) {5863$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5864last ifdefined$file_hash;5865}5866}5867if(defined$file_hash) {5868$ftype= git_get_type($file_hash);5869}5870if(defined$file_name&& !defined$ftype) {5871 die_error(500,"Unknown type of object");5872}5873my%co;5874if(defined$file_name) {5875%co= parse_commit($base)5876or die_error(404,"Unknown commit object");5877}587858795880my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5881my$next_link='';5882if($#commitlist>=100) {5883$next_link=5884$cgi->a({-href => href(-replay=>1, page=>$page+1),5885-accesskey =>"n", -title =>"Alt-n"},"next");5886}5887my$patch_max= gitweb_get_feature('patches');5888if($patch_max&& !defined$file_name) {5889if($patch_max<0||@commitlist<=$patch_max) {5890$paging_nav.=" ⋅ ".5891$cgi->a({-href => href(action=>"patches", -replay=>1)},5892"patches");5893}5894}58955896 git_header_html();5897 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5898if(defined$file_name) {5899 git_print_header_div('commit', esc_html($co{'title'}),$base);5900}else{5901 git_print_header_div('summary',$project)5902}5903 git_print_page_path($file_name,$ftype,$hash_base)5904if(defined$file_name);59055906$body_subr->(\@commitlist,0,99,$refs,$next_link,5907$file_name,$file_hash,$ftype);59085909 git_footer_html();5910}59115912sub git_log {5913 git_log_generic('log', \&git_log_body,5914$hash,$hash_parent);5915}59165917sub git_commit {5918$hash||=$hash_base||"HEAD";5919my%co= parse_commit($hash)5920or die_error(404,"Unknown commit object");59215922my$parent=$co{'parent'};5923my$parents=$co{'parents'};# listref59245925# we need to prepare $formats_nav before any parameter munging5926my$formats_nav;5927if(!defined$parent) {5928# --root commitdiff5929$formats_nav.='(initial)';5930}elsif(@$parents==1) {5931# single parent commit5932$formats_nav.=5933'(parent: '.5934$cgi->a({-href => href(action=>"commit",5935 hash=>$parent)},5936 esc_html(substr($parent,0,7))) .5937')';5938}else{5939# merge commit5940$formats_nav.=5941'(merge: '.5942join(' ',map{5943$cgi->a({-href => href(action=>"commit",5944 hash=>$_)},5945 esc_html(substr($_,0,7)));5946}@$parents) .5947')';5948}5949if(gitweb_check_feature('patches') &&@$parents<=1) {5950$formats_nav.=" | ".5951$cgi->a({-href => href(action=>"patch", -replay=>1)},5952"patch");5953}59545955if(!defined$parent) {5956$parent="--root";5957}5958my@difftree;5959open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5960@diff_opts,5961(@$parents<=1?$parent:'-c'),5962$hash,"--"5963or die_error(500,"Open git-diff-tree failed");5964@difftree=map{chomp;$_} <$fd>;5965close$fdor die_error(404,"Reading git-diff-tree failed");59665967# non-textual hash id's can be cached5968my$expires;5969if($hash=~m/^[0-9a-fA-F]{40}$/) {5970$expires="+1d";5971}5972my$refs= git_get_references();5973my$ref= format_ref_marker($refs,$co{'id'});59745975 git_header_html(undef,$expires);5976 git_print_page_nav('commit','',5977$hash,$co{'tree'},$hash,5978$formats_nav);59795980if(defined$co{'parent'}) {5981 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5982}else{5983 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5984}5985print"<div class=\"title_text\">\n".5986"<table class=\"object_header\">\n";5987 git_print_authorship_rows(\%co);5988print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5989print"<tr>".5990"<td>tree</td>".5991"<td class=\"sha1\">".5992$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5993class=>"list"},$co{'tree'}) .5994"</td>".5995"<td class=\"link\">".5996$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5997"tree");5998my$snapshot_links= format_snapshot_links($hash);5999if(defined$snapshot_links) {6000print" | ".$snapshot_links;6001}6002print"</td>".6003"</tr>\n";60046005foreachmy$par(@$parents) {6006print"<tr>".6007"<td>parent</td>".6008"<td class=\"sha1\">".6009$cgi->a({-href => href(action=>"commit", hash=>$par),6010class=>"list"},$par) .6011"</td>".6012"<td class=\"link\">".6013$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .6014" | ".6015$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .6016"</td>".6017"</tr>\n";6018}6019print"</table>".6020"</div>\n";60216022print"<div class=\"page_body\">\n";6023 git_print_log($co{'comment'});6024print"</div>\n";60256026 git_difftree_body(\@difftree,$hash,@$parents);60276028 git_footer_html();6029}60306031sub git_object {6032# object is defined by:6033# - hash or hash_base alone6034# - hash_base and file_name6035my$type;60366037# - hash or hash_base alone6038if($hash|| ($hash_base&& !defined$file_name)) {6039my$object_id=$hash||$hash_base;60406041open my$fd,"-|", quote_command(6042 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'6043or die_error(404,"Object does not exist");6044$type= <$fd>;6045chomp$type;6046close$fd6047or die_error(404,"Object does not exist");60486049# - hash_base and file_name6050}elsif($hash_base&&defined$file_name) {6051$file_name=~ s,/+$,,;60526053system(git_cmd(),"cat-file",'-e',$hash_base) ==06054or die_error(404,"Base object does not exist");60556056# here errors should not hapen6057open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name6058or die_error(500,"Open git-ls-tree failed");6059my$line= <$fd>;6060close$fd;60616062#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'6063unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {6064 die_error(404,"File or directory for given base does not exist");6065}6066$type=$2;6067$hash=$3;6068}else{6069 die_error(400,"Not enough information to find object");6070}60716072print$cgi->redirect(-uri => href(action=>$type, -full=>1,6073 hash=>$hash, hash_base=>$hash_base,6074 file_name=>$file_name),6075-status =>'302 Found');6076}60776078sub git_blobdiff {6079my$format=shift||'html';60806081my$fd;6082my@difftree;6083my%diffinfo;6084my$expires;60856086# preparing $fd and %diffinfo for git_patchset_body6087# new style URI6088if(defined$hash_base&&defined$hash_parent_base) {6089if(defined$file_name) {6090# read raw output6091open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6092$hash_parent_base,$hash_base,6093"--", (defined$file_parent?$file_parent: ()),$file_name6094or die_error(500,"Open git-diff-tree failed");6095@difftree=map{chomp;$_} <$fd>;6096close$fd6097or die_error(404,"Reading git-diff-tree failed");6098@difftree6099or die_error(404,"Blob diff not found");61006101}elsif(defined$hash&&6102$hash=~/[0-9a-fA-F]{40}/) {6103# try to find filename from $hash61046105# read filtered raw output6106open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6107$hash_parent_base,$hash_base,"--"6108or die_error(500,"Open git-diff-tree failed");6109@difftree=6110# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'6111# $hash == to_id6112grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}6113map{chomp;$_} <$fd>;6114close$fd6115or die_error(404,"Reading git-diff-tree failed");6116@difftree6117or die_error(404,"Blob diff not found");61186119}else{6120 die_error(400,"Missing one of the blob diff parameters");6121}61226123if(@difftree>1) {6124 die_error(400,"Ambiguous blob diff specification");6125}61266127%diffinfo= parse_difftree_raw_line($difftree[0]);6128$file_parent||=$diffinfo{'from_file'} ||$file_name;6129$file_name||=$diffinfo{'to_file'};61306131$hash_parent||=$diffinfo{'from_id'};6132$hash||=$diffinfo{'to_id'};61336134# non-textual hash id's can be cached6135if($hash_base=~m/^[0-9a-fA-F]{40}$/&&6136$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {6137$expires='+1d';6138}61396140# open patch output6141open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6142'-p', ($formateq'html'?"--full-index": ()),6143$hash_parent_base,$hash_base,6144"--", (defined$file_parent?$file_parent: ()),$file_name6145or die_error(500,"Open git-diff-tree failed");6146}61476148# old/legacy style URI -- not generated anymore since 1.4.3.6149if(!%diffinfo) {6150 die_error('404 Not Found',"Missing one of the blob diff parameters")6151}61526153# header6154if($formateq'html') {6155my$formats_nav=6156$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},6157"raw");6158 git_header_html(undef,$expires);6159if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6160 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6161 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6162}else{6163print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";6164print"<div class=\"title\">$hashvs$hash_parent</div>\n";6165}6166if(defined$file_name) {6167 git_print_page_path($file_name,"blob",$hash_base);6168}else{6169print"<div class=\"page_path\"></div>\n";6170}61716172}elsif($formateq'plain') {6173print$cgi->header(6174-type =>'text/plain',6175-charset =>'utf-8',6176-expires =>$expires,6177-content_disposition =>'inline; filename="'."$file_name".'.patch"');61786179print"X-Git-Url: ".$cgi->self_url() ."\n\n";61806181}else{6182 die_error(400,"Unknown blobdiff format");6183}61846185# patch6186if($formateq'html') {6187print"<div class=\"page_body\">\n";61886189 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);6190close$fd;61916192print"</div>\n";# class="page_body"6193 git_footer_html();61946195}else{6196while(my$line= <$fd>) {6197$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;6198$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;61996200print$line;62016202last if$line=~m!^\+\+\+!;6203}6204local$/=undef;6205print<$fd>;6206close$fd;6207}6208}62096210sub git_blobdiff_plain {6211 git_blobdiff('plain');6212}62136214sub git_commitdiff {6215my%params=@_;6216my$format=$params{-format} ||'html';62176218my($patch_max) = gitweb_get_feature('patches');6219if($formateq'patch') {6220 die_error(403,"Patch view not allowed")unless$patch_max;6221}62226223$hash||=$hash_base||"HEAD";6224my%co= parse_commit($hash)6225or die_error(404,"Unknown commit object");62266227# choose format for commitdiff for merge6228if(!defined$hash_parent&& @{$co{'parents'}} >1) {6229$hash_parent='--cc';6230}6231# we need to prepare $formats_nav before almost any parameter munging6232my$formats_nav;6233if($formateq'html') {6234$formats_nav=6235$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6236"raw");6237if($patch_max&& @{$co{'parents'}} <=1) {6238$formats_nav.=" | ".6239$cgi->a({-href => href(action=>"patch", -replay=>1)},6240"patch");6241}62426243if(defined$hash_parent&&6244$hash_parentne'-c'&&$hash_parentne'--cc') {6245# commitdiff with two commits given6246my$hash_parent_short=$hash_parent;6247if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6248$hash_parent_short=substr($hash_parent,0,7);6249}6250$formats_nav.=6251' (from';6252for(my$i=0;$i< @{$co{'parents'}};$i++) {6253if($co{'parents'}[$i]eq$hash_parent) {6254$formats_nav.=' parent '. ($i+1);6255last;6256}6257}6258$formats_nav.=': '.6259$cgi->a({-href => href(action=>"commitdiff",6260 hash=>$hash_parent)},6261 esc_html($hash_parent_short)) .6262')';6263}elsif(!$co{'parent'}) {6264# --root commitdiff6265$formats_nav.=' (initial)';6266}elsif(scalar@{$co{'parents'}} ==1) {6267# single parent commit6268$formats_nav.=6269' (parent: '.6270$cgi->a({-href => href(action=>"commitdiff",6271 hash=>$co{'parent'})},6272 esc_html(substr($co{'parent'},0,7))) .6273')';6274}else{6275# merge commit6276if($hash_parenteq'--cc') {6277$formats_nav.=' | '.6278$cgi->a({-href => href(action=>"commitdiff",6279 hash=>$hash, hash_parent=>'-c')},6280'combined');6281}else{# $hash_parent eq '-c'6282$formats_nav.=' | '.6283$cgi->a({-href => href(action=>"commitdiff",6284 hash=>$hash, hash_parent=>'--cc')},6285'compact');6286}6287$formats_nav.=6288' (merge: '.6289join(' ',map{6290$cgi->a({-href => href(action=>"commitdiff",6291 hash=>$_)},6292 esc_html(substr($_,0,7)));6293} @{$co{'parents'}} ) .6294')';6295}6296}62976298my$hash_parent_param=$hash_parent;6299if(!defined$hash_parent_param) {6300# --cc for multiple parents, --root for parentless6301$hash_parent_param=6302@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6303}63046305# read commitdiff6306my$fd;6307my@difftree;6308if($formateq'html') {6309open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6310"--no-commit-id","--patch-with-raw","--full-index",6311$hash_parent_param,$hash,"--"6312or die_error(500,"Open git-diff-tree failed");63136314while(my$line= <$fd>) {6315chomp$line;6316# empty line ends raw part of diff-tree output6317last unless$line;6318push@difftree,scalar parse_difftree_raw_line($line);6319}63206321}elsif($formateq'plain') {6322open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6323'-p',$hash_parent_param,$hash,"--"6324or die_error(500,"Open git-diff-tree failed");6325}elsif($formateq'patch') {6326# For commit ranges, we limit the output to the number of6327# patches specified in the 'patches' feature.6328# For single commits, we limit the output to a single patch,6329# diverging from the git-format-patch default.6330my@commit_spec= ();6331if($hash_parent) {6332if($patch_max>0) {6333push@commit_spec,"-$patch_max";6334}6335push@commit_spec,'-n',"$hash_parent..$hash";6336}else{6337if($params{-single}) {6338push@commit_spec,'-1';6339}else{6340if($patch_max>0) {6341push@commit_spec,"-$patch_max";6342}6343push@commit_spec,"-n";6344}6345push@commit_spec,'--root',$hash;6346}6347open$fd,"-|", git_cmd(),"format-patch",@diff_opts,6348'--encoding=utf8','--stdout',@commit_spec6349or die_error(500,"Open git-format-patch failed");6350}else{6351 die_error(400,"Unknown commitdiff format");6352}63536354# non-textual hash id's can be cached6355my$expires;6356if($hash=~m/^[0-9a-fA-F]{40}$/) {6357$expires="+1d";6358}63596360# write commit message6361if($formateq'html') {6362my$refs= git_get_references();6363my$ref= format_ref_marker($refs,$co{'id'});63646365 git_header_html(undef,$expires);6366 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6367 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6368print"<div class=\"title_text\">\n".6369"<table class=\"object_header\">\n";6370 git_print_authorship_rows(\%co);6371print"</table>".6372"</div>\n";6373print"<div class=\"page_body\">\n";6374if(@{$co{'comment'}} >1) {6375print"<div class=\"log\">\n";6376 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6377print"</div>\n";# class="log"6378}63796380}elsif($formateq'plain') {6381my$refs= git_get_references("tags");6382my$tagname= git_get_rev_name_tags($hash);6383my$filename= basename($project) ."-$hash.patch";63846385print$cgi->header(6386-type =>'text/plain',6387-charset =>'utf-8',6388-expires =>$expires,6389-content_disposition =>'inline; filename="'."$filename".'"');6390my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6391print"From: ". to_utf8($co{'author'}) ."\n";6392print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6393print"Subject: ". to_utf8($co{'title'}) ."\n";63946395print"X-Git-Tag:$tagname\n"if$tagname;6396print"X-Git-Url: ".$cgi->self_url() ."\n\n";63976398foreachmy$line(@{$co{'comment'}}) {6399print to_utf8($line) ."\n";6400}6401print"---\n\n";6402}elsif($formateq'patch') {6403my$filename= basename($project) ."-$hash.patch";64046405print$cgi->header(6406-type =>'text/plain',6407-charset =>'utf-8',6408-expires =>$expires,6409-content_disposition =>'inline; filename="'."$filename".'"');6410}64116412# write patch6413if($formateq'html') {6414my$use_parents= !defined$hash_parent||6415$hash_parenteq'-c'||$hash_parenteq'--cc';6416 git_difftree_body(\@difftree,$hash,6417$use_parents? @{$co{'parents'}} :$hash_parent);6418print"<br/>\n";64196420 git_patchset_body($fd, \@difftree,$hash,6421$use_parents? @{$co{'parents'}} :$hash_parent);6422close$fd;6423print"</div>\n";# class="page_body"6424 git_footer_html();64256426}elsif($formateq'plain') {6427local$/=undef;6428print<$fd>;6429close$fd6430or print"Reading git-diff-tree failed\n";6431}elsif($formateq'patch') {6432local$/=undef;6433print<$fd>;6434close$fd6435or print"Reading git-format-patch failed\n";6436}6437}64386439sub git_commitdiff_plain {6440 git_commitdiff(-format =>'plain');6441}64426443# format-patch-style patches6444sub git_patch {6445 git_commitdiff(-format =>'patch', -single =>1);6446}64476448sub git_patches {6449 git_commitdiff(-format =>'patch');6450}64516452sub git_history {6453 git_log_generic('history', \&git_history_body,6454$hash_base,$hash_parent_base,6455$file_name,$hash);6456}64576458sub git_search {6459 gitweb_check_feature('search')or die_error(403,"Search is disabled");6460if(!defined$searchtext) {6461 die_error(400,"Text field is empty");6462}6463if(!defined$hash) {6464$hash= git_get_head_hash($project);6465}6466my%co= parse_commit($hash);6467if(!%co) {6468 die_error(404,"Unknown commit object");6469}6470if(!defined$page) {6471$page=0;6472}64736474$searchtype||='commit';6475if($searchtypeeq'pickaxe') {6476# pickaxe may take all resources of your box and run for several minutes6477# with every query - so decide by yourself how public you make this feature6478 gitweb_check_feature('pickaxe')6479or die_error(403,"Pickaxe is disabled");6480}6481if($searchtypeeq'grep') {6482 gitweb_check_feature('grep')6483or die_error(403,"Grep is disabled");6484}64856486 git_header_html();64876488if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6489my$greptype;6490if($searchtypeeq'commit') {6491$greptype="--grep=";6492}elsif($searchtypeeq'author') {6493$greptype="--author=";6494}elsif($searchtypeeq'committer') {6495$greptype="--committer=";6496}6497$greptype.=$searchtext;6498my@commitlist= parse_commits($hash,101, (100*$page),undef,6499$greptype,'--regexp-ignore-case',6500$search_use_regexp?'--extended-regexp':'--fixed-strings');65016502my$paging_nav='';6503if($page>0) {6504$paging_nav.=6505$cgi->a({-href => href(action=>"search", hash=>$hash,6506 searchtext=>$searchtext,6507 searchtype=>$searchtype)},6508"first");6509$paging_nav.=" ⋅ ".6510$cgi->a({-href => href(-replay=>1, page=>$page-1),6511-accesskey =>"p", -title =>"Alt-p"},"prev");6512}else{6513$paging_nav.="first";6514$paging_nav.=" ⋅ prev";6515}6516my$next_link='';6517if($#commitlist>=100) {6518$next_link=6519$cgi->a({-href => href(-replay=>1, page=>$page+1),6520-accesskey =>"n", -title =>"Alt-n"},"next");6521$paging_nav.=" ⋅$next_link";6522}else{6523$paging_nav.=" ⋅ next";6524}65256526if($#commitlist>=100) {6527}65286529 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6530 git_print_header_div('commit', esc_html($co{'title'}),$hash);6531 git_search_grep_body(\@commitlist,0,99,$next_link);6532}65336534if($searchtypeeq'pickaxe') {6535 git_print_page_nav('','',$hash,$co{'tree'},$hash);6536 git_print_header_div('commit', esc_html($co{'title'}),$hash);65376538print"<table class=\"pickaxe search\">\n";6539my$alternate=1;6540local$/="\n";6541open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6542'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6543($search_use_regexp?'--pickaxe-regex': ());6544undef%co;6545my@files;6546while(my$line= <$fd>) {6547chomp$line;6548next unless$line;65496550my%set= parse_difftree_raw_line($line);6551if(defined$set{'commit'}) {6552# finish previous commit6553if(%co) {6554print"</td>\n".6555"<td class=\"link\">".6556$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6557" | ".6558$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6559print"</td>\n".6560"</tr>\n";6561}65626563if($alternate) {6564print"<tr class=\"dark\">\n";6565}else{6566print"<tr class=\"light\">\n";6567}6568$alternate^=1;6569%co= parse_commit($set{'commit'});6570my$author= chop_and_escape_str($co{'author_name'},15,5);6571print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6572"<td><i>$author</i></td>\n".6573"<td>".6574$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6575-class=>"list subject"},6576 chop_and_escape_str($co{'title'},50) ."<br/>");6577}elsif(defined$set{'to_id'}) {6578next if($set{'to_id'} =~m/^0{40}$/);65796580print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6581 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6582-class=>"list"},6583"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6584"<br/>\n";6585}6586}6587close$fd;65886589# finish last commit (warning: repetition!)6590if(%co) {6591print"</td>\n".6592"<td class=\"link\">".6593$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6594" | ".6595$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6596print"</td>\n".6597"</tr>\n";6598}65996600print"</table>\n";6601}66026603if($searchtypeeq'grep') {6604 git_print_page_nav('','',$hash,$co{'tree'},$hash);6605 git_print_header_div('commit', esc_html($co{'title'}),$hash);66066607print"<table class=\"grep_search\">\n";6608my$alternate=1;6609my$matches=0;6610local$/="\n";6611open my$fd,"-|", git_cmd(),'grep','-n',6612$search_use_regexp? ('-E','-i') :'-F',6613$searchtext,$co{'tree'};6614my$lastfile='';6615while(my$line= <$fd>) {6616chomp$line;6617my($file,$lno,$ltext,$binary);6618last if($matches++>1000);6619if($line=~/^Binary file (.+) matches$/) {6620$file=$1;6621$binary=1;6622}else{6623(undef,$file,$lno,$ltext) =split(/:/,$line,4);6624}6625if($filene$lastfile) {6626$lastfileand print"</td></tr>\n";6627if($alternate++) {6628print"<tr class=\"dark\">\n";6629}else{6630print"<tr class=\"light\">\n";6631}6632print"<td class=\"list\">".6633$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6634 file_name=>"$file"),6635-class=>"list"}, esc_path($file));6636print"</td><td>\n";6637$lastfile=$file;6638}6639if($binary) {6640print"<div class=\"binary\">Binary file</div>\n";6641}else{6642$ltext= untabify($ltext);6643if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6644$ltext= esc_html($1, -nbsp=>1);6645$ltext.='<span class="match">';6646$ltext.= esc_html($2, -nbsp=>1);6647$ltext.='</span>';6648$ltext.= esc_html($3, -nbsp=>1);6649}else{6650$ltext= esc_html($ltext, -nbsp=>1);6651}6652print"<div class=\"pre\">".6653$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6654 file_name=>"$file").'#l'.$lno,6655-class=>"linenr"},sprintf('%4i',$lno))6656.' '.$ltext."</div>\n";6657}6658}6659if($lastfile) {6660print"</td></tr>\n";6661if($matches>1000) {6662print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6663}6664}else{6665print"<div class=\"diff nodifferences\">No matches found</div>\n";6666}6667close$fd;66686669print"</table>\n";6670}6671 git_footer_html();6672}66736674sub git_search_help {6675 git_header_html();6676 git_print_page_nav('','',$hash,$hash,$hash);6677print<<EOT;6678<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6679regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6680the pattern entered is recognized as the POSIX extended6681<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6682insensitive).</p>6683<dl>6684<dt><b>commit</b></dt>6685<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6686EOT6687my$have_grep= gitweb_check_feature('grep');6688if($have_grep) {6689print<<EOT;6690<dt><b>grep</b></dt>6691<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6692 a different one) are searched for the given pattern. On large trees, this search can take6693a while and put some strain on the server, so please use it with some consideration. Note that6694due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6695case-sensitive.</dd>6696EOT6697}6698print<<EOT;6699<dt><b>author</b></dt>6700<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6701<dt><b>committer</b></dt>6702<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6703EOT6704my$have_pickaxe= gitweb_check_feature('pickaxe');6705if($have_pickaxe) {6706print<<EOT;6707<dt><b>pickaxe</b></dt>6708<dd>All commits that caused the string to appear or disappear from any file (changes that6709added, removed or "modified" the string) will be listed. This search can take a while and6710takes a lot of strain on the server, so please use it wisely. Note that since you may be6711interested even in changes just changing the case as well, this search is case sensitive.</dd>6712EOT6713}6714print"</dl>\n";6715 git_footer_html();6716}67176718sub git_shortlog {6719 git_log_generic('shortlog', \&git_shortlog_body,6720$hash,$hash_parent);6721}67226723## ......................................................................6724## feeds (RSS, Atom; OPML)67256726sub git_feed {6727my$format=shift||'atom';6728my$have_blame= gitweb_check_feature('blame');67296730# Atom: http://www.atomenabled.org/developers/syndication/6731# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6732if($formatne'rss'&&$formatne'atom') {6733 die_error(400,"Unknown web feed format");6734}67356736# log/feed of current (HEAD) branch, log of given branch, history of file/directory6737my$head=$hash||'HEAD';6738my@commitlist= parse_commits($head,150,0,$file_name);67396740my%latest_commit;6741my%latest_date;6742my$content_type="application/$format+xml";6743if(defined$cgi->http('HTTP_ACCEPT') &&6744$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6745# browser (feed reader) prefers text/xml6746$content_type='text/xml';6747}6748if(defined($commitlist[0])) {6749%latest_commit= %{$commitlist[0]};6750my$latest_epoch=$latest_commit{'committer_epoch'};6751%latest_date= parse_date($latest_epoch);6752my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6753if(defined$if_modified) {6754my$since;6755if(eval{require HTTP::Date;1; }) {6756$since= HTTP::Date::str2time($if_modified);6757}elsif(eval{require Time::ParseDate;1; }) {6758$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6759}6760if(defined$since&&$latest_epoch<=$since) {6761print$cgi->header(6762-type =>$content_type,6763-charset =>'utf-8',6764-last_modified =>$latest_date{'rfc2822'},6765-status =>'304 Not Modified');6766return;6767}6768}6769print$cgi->header(6770-type =>$content_type,6771-charset =>'utf-8',6772-last_modified =>$latest_date{'rfc2822'});6773}else{6774print$cgi->header(6775-type =>$content_type,6776-charset =>'utf-8');6777}67786779# Optimization: skip generating the body if client asks only6780# for Last-Modified date.6781return if($cgi->request_method()eq'HEAD');67826783# header variables6784my$title="$site_name-$project/$action";6785my$feed_type='log';6786if(defined$hash) {6787$title.=" - '$hash'";6788$feed_type='branch log';6789if(defined$file_name) {6790$title.=" ::$file_name";6791$feed_type='history';6792}6793}elsif(defined$file_name) {6794$title.=" -$file_name";6795$feed_type='history';6796}6797$title.="$feed_type";6798my$descr= git_get_project_description($project);6799if(defined$descr) {6800$descr= esc_html($descr);6801}else{6802$descr="$project".6803($formateq'rss'?'RSS':'Atom') .6804" feed";6805}6806my$owner= git_get_project_owner($project);6807$owner= esc_html($owner);68086809#header6810my$alt_url;6811if(defined$file_name) {6812$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6813}elsif(defined$hash) {6814$alt_url= href(-full=>1, action=>"log", hash=>$hash);6815}else{6816$alt_url= href(-full=>1, action=>"summary");6817}6818print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6819if($formateq'rss') {6820print<<XML;6821<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6822<channel>6823XML6824print"<title>$title</title>\n".6825"<link>$alt_url</link>\n".6826"<description>$descr</description>\n".6827"<language>en</language>\n".6828# project owner is responsible for 'editorial' content6829"<managingEditor>$owner</managingEditor>\n";6830if(defined$logo||defined$favicon) {6831# prefer the logo to the favicon, since RSS6832# doesn't allow both6833my$img= esc_url($logo||$favicon);6834print"<image>\n".6835"<url>$img</url>\n".6836"<title>$title</title>\n".6837"<link>$alt_url</link>\n".6838"</image>\n";6839}6840if(%latest_date) {6841print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6842print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6843}6844print"<generator>gitweb v.$version/$git_version</generator>\n";6845}elsif($formateq'atom') {6846print<<XML;6847<feed xmlns="http://www.w3.org/2005/Atom">6848XML6849print"<title>$title</title>\n".6850"<subtitle>$descr</subtitle>\n".6851'<link rel="alternate" type="text/html" href="'.6852$alt_url.'" />'."\n".6853'<link rel="self" type="'.$content_type.'" href="'.6854$cgi->self_url() .'" />'."\n".6855"<id>". href(-full=>1) ."</id>\n".6856# use project owner for feed author6857"<author><name>$owner</name></author>\n";6858if(defined$favicon) {6859print"<icon>". esc_url($favicon) ."</icon>\n";6860}6861if(defined$logo_url) {6862# not twice as wide as tall: 72 x 27 pixels6863print"<logo>". esc_url($logo) ."</logo>\n";6864}6865if(!%latest_date) {6866# dummy date to keep the feed valid until commits trickle in:6867print"<updated>1970-01-01T00:00:00Z</updated>\n";6868}else{6869print"<updated>$latest_date{'iso-8601'}</updated>\n";6870}6871print"<generator version='$version/$git_version'>gitweb</generator>\n";6872}68736874# contents6875for(my$i=0;$i<=$#commitlist;$i++) {6876my%co= %{$commitlist[$i]};6877my$commit=$co{'id'};6878# we read 150, we always show 30 and the ones more recent than 48 hours6879if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6880last;6881}6882my%cd= parse_date($co{'author_epoch'});68836884# get list of changed files6885open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6886$co{'parent'} ||"--root",6887$co{'id'},"--", (defined$file_name?$file_name: ())6888ornext;6889my@difftree=map{chomp;$_} <$fd>;6890close$fd6891ornext;68926893# print element (entry, item)6894my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6895if($formateq'rss') {6896print"<item>\n".6897"<title>". esc_html($co{'title'}) ."</title>\n".6898"<author>". esc_html($co{'author'}) ."</author>\n".6899"<pubDate>$cd{'rfc2822'}</pubDate>\n".6900"<guid isPermaLink=\"true\">$co_url</guid>\n".6901"<link>$co_url</link>\n".6902"<description>". esc_html($co{'title'}) ."</description>\n".6903"<content:encoded>".6904"<![CDATA[\n";6905}elsif($formateq'atom') {6906print"<entry>\n".6907"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6908"<updated>$cd{'iso-8601'}</updated>\n".6909"<author>\n".6910" <name>". esc_html($co{'author_name'}) ."</name>\n";6911if($co{'author_email'}) {6912print" <email>". esc_html($co{'author_email'}) ."</email>\n";6913}6914print"</author>\n".6915# use committer for contributor6916"<contributor>\n".6917" <name>". esc_html($co{'committer_name'}) ."</name>\n";6918if($co{'committer_email'}) {6919print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6920}6921print"</contributor>\n".6922"<published>$cd{'iso-8601'}</published>\n".6923"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6924"<id>$co_url</id>\n".6925"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6926"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6927}6928my$comment=$co{'comment'};6929print"<pre>\n";6930foreachmy$line(@$comment) {6931$line= esc_html($line);6932print"$line\n";6933}6934print"</pre><ul>\n";6935foreachmy$difftree_line(@difftree) {6936my%difftree= parse_difftree_raw_line($difftree_line);6937next if!$difftree{'from_id'};69386939my$file=$difftree{'file'} ||$difftree{'to_file'};69406941print"<li>".6942"[".6943$cgi->a({-href => href(-full=>1, action=>"blobdiff",6944 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6945 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6946 file_name=>$file, file_parent=>$difftree{'from_file'}),6947-title =>"diff"},'D');6948if($have_blame) {6949print$cgi->a({-href => href(-full=>1, action=>"blame",6950 file_name=>$file, hash_base=>$commit),6951-title =>"blame"},'B');6952}6953# if this is not a feed of a file history6954if(!defined$file_name||$file_namene$file) {6955print$cgi->a({-href => href(-full=>1, action=>"history",6956 file_name=>$file, hash=>$commit),6957-title =>"history"},'H');6958}6959$file= esc_path($file);6960print"] ".6961"$file</li>\n";6962}6963if($formateq'rss') {6964print"</ul>]]>\n".6965"</content:encoded>\n".6966"</item>\n";6967}elsif($formateq'atom') {6968print"</ul>\n</div>\n".6969"</content>\n".6970"</entry>\n";6971}6972}69736974# end of feed6975if($formateq'rss') {6976print"</channel>\n</rss>\n";6977}elsif($formateq'atom') {6978print"</feed>\n";6979}6980}69816982sub git_rss {6983 git_feed('rss');6984}69856986sub git_atom {6987 git_feed('atom');6988}69896990sub git_opml {6991my@list= git_get_projects_list();69926993print$cgi->header(6994-type =>'text/xml',6995-charset =>'utf-8',6996-content_disposition =>'inline; filename="opml.xml"');69976998print<<XML;6999<?xml version="1.0" encoding="utf-8"?>7000<opml version="1.0">7001<head>7002 <title>$site_nameOPML Export</title>7003</head>7004<body>7005<outline text="git RSS feeds">7006XML70077008foreachmy$pr(@list) {7009my%proj=%$pr;7010my$head= git_get_head_hash($proj{'path'});7011if(!defined$head) {7012next;7013}7014$git_dir="$projectroot/$proj{'path'}";7015my%co= parse_commit($head);7016if(!%co) {7017next;7018}70197020my$path= esc_html(chop_str($proj{'path'},25,5));7021my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);7022my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);7023print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";7024}7025print<<XML;7026</outline>7027</body>7028</opml>7029XML7030}