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 10use5.008; 11use strict; 12use warnings; 13use CGI qw(:standard :escapeHTML -nosticky); 14use CGI::Util qw(unescape); 15use CGI::Carp qw(fatalsToBrowser set_message); 16use Encode; 17use Fcntl ':mode'; 18use File::Find qw(); 19use File::Basename qw(basename); 20binmode STDOUT,':utf8'; 21 22our$t0; 23if(eval{require Time::HiRes;1; }) { 24$t0= [Time::HiRes::gettimeofday()]; 25} 26our$number_of_git_cmds=0; 27 28BEGIN{ 29 CGI->compile()if$ENV{'MOD_PERL'}; 30} 31 32our$version="++GIT_VERSION++"; 33 34our($my_url,$my_uri,$base_url,$path_info,$home_link); 35sub evaluate_uri { 36our$cgi; 37 38our$my_url=$cgi->url(); 39our$my_uri=$cgi->url(-absolute =>1); 40 41# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 42# needed and used only for URLs with nonempty PATH_INFO 43our$base_url=$my_url; 44 45# When the script is used as DirectoryIndex, the URL does not contain the name 46# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 47# have to do it ourselves. We make $path_info global because it's also used 48# later on. 49# 50# Another issue with the script being the DirectoryIndex is that the resulting 51# $my_url data is not the full script URL: this is good, because we want 52# generated links to keep implying the script name if it wasn't explicitly 53# indicated in the URL we're handling, but it means that $my_url cannot be used 54# as base URL. 55# Therefore, if we needed to strip PATH_INFO, then we know that we have 56# to build the base URL ourselves: 57our$path_info=$ENV{"PATH_INFO"}; 58if($path_info) { 59if($my_url=~ s,\Q$path_info\E$,, && 60$my_uri=~ s,\Q$path_info\E$,, && 61defined$ENV{'SCRIPT_NAME'}) { 62$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 63} 64} 65 66# target of the home link on top of all pages 67our$home_link=$my_uri||"/"; 68} 69 70# core git executable to use 71# this can just be "git" if your webserver has a sensible PATH 72our$GIT="++GIT_BINDIR++/git"; 73 74# absolute fs-path which will be prepended to the project path 75#our $projectroot = "/pub/scm"; 76our$projectroot="++GITWEB_PROJECTROOT++"; 77 78# fs traversing limit for getting project list 79# the number is relative to the projectroot 80our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 81 82# string of the home link on top of all pages 83our$home_link_str="++GITWEB_HOME_LINK_STR++"; 84 85# name of your site or organization to appear in page titles 86# replace this with something more descriptive for clearer bookmarks 87our$site_name="++GITWEB_SITENAME++" 88|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 89 90# filename of html text to include at top of each page 91our$site_header="++GITWEB_SITE_HEADER++"; 92# html text to include at home page 93our$home_text="++GITWEB_HOMETEXT++"; 94# filename of html text to include at bottom of each page 95our$site_footer="++GITWEB_SITE_FOOTER++"; 96 97# URI of stylesheets 98our@stylesheets= ("++GITWEB_CSS++"); 99# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 100our$stylesheet=undef; 101# URI of GIT logo (72x27 size) 102our$logo="++GITWEB_LOGO++"; 103# URI of GIT favicon, assumed to be image/png type 104our$favicon="++GITWEB_FAVICON++"; 105# URI of gitweb.js (JavaScript code for gitweb) 106our$javascript="++GITWEB_JS++"; 107 108# URI and label (title) of GIT logo link 109#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 110#our $logo_label = "git documentation"; 111our$logo_url="http://git-scm.com/"; 112our$logo_label="git homepage"; 113 114# source of projects list 115our$projects_list="++GITWEB_LIST++"; 116 117# the width (in characters) of the projects list "Description" column 118our$projects_list_description_width=25; 119 120# default order of projects list 121# valid values are none, project, descr, owner, and age 122our$default_projects_order="project"; 123 124# show repository only if this file exists 125# (only effective if this variable evaluates to true) 126our$export_ok="++GITWEB_EXPORT_OK++"; 127 128# show repository only if this subroutine returns true 129# when given the path to the project, for example: 130# sub { return -e "$_[0]/git-daemon-export-ok"; } 131our$export_auth_hook=undef; 132 133# only allow viewing of repositories also shown on the overview page 134our$strict_export="++GITWEB_STRICT_EXPORT++"; 135 136# list of git base URLs used for URL to where fetch project from, 137# i.e. full URL is "$git_base_url/$project" 138our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 139 140# default blob_plain mimetype and default charset for text/plain blob 141our$default_blob_plain_mimetype='text/plain'; 142our$default_text_plain_charset=undef; 143 144# file to use for guessing MIME types before trying /etc/mime.types 145# (relative to the current git repository) 146our$mimetypes_file=undef; 147 148# assume this charset if line contains non-UTF-8 characters; 149# it should be valid encoding (see Encoding::Supported(3pm) for list), 150# for which encoding all byte sequences are valid, for example 151# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 152# could be even 'utf-8' for the old behavior) 153our$fallback_encoding='latin1'; 154 155# rename detection options for git-diff and git-diff-tree 156# - default is '-M', with the cost proportional to 157# (number of removed files) * (number of new files). 158# - more costly is '-C' (which implies '-M'), with the cost proportional to 159# (number of changed files + number of removed files) * (number of new files) 160# - even more costly is '-C', '--find-copies-harder' with cost 161# (number of files in the original tree) * (number of new files) 162# - one might want to include '-B' option, e.g. '-B', '-M' 163our@diff_opts= ('-M');# taken from git_commit 164 165# Disables features that would allow repository owners to inject script into 166# the gitweb domain. 167our$prevent_xss=0; 168 169# Path to the highlight executable to use (must be the one from 170# http://www.andre-simon.de due to assumptions about parameters and output). 171# Useful if highlight is not installed on your webserver's PATH. 172# [Default: highlight] 173our$highlight_bin="++HIGHLIGHT_BIN++"; 174 175# information about snapshot formats that gitweb is capable of serving 176our%known_snapshot_formats= ( 177# name => { 178# 'display' => display name, 179# 'type' => mime type, 180# 'suffix' => filename suffix, 181# 'format' => --format for git-archive, 182# 'compressor' => [compressor command and arguments] 183# (array reference, optional) 184# 'disabled' => boolean (optional)} 185# 186'tgz'=> { 187'display'=>'tar.gz', 188'type'=>'application/x-gzip', 189'suffix'=>'.tar.gz', 190'format'=>'tar', 191'compressor'=> ['gzip']}, 192 193'tbz2'=> { 194'display'=>'tar.bz2', 195'type'=>'application/x-bzip2', 196'suffix'=>'.tar.bz2', 197'format'=>'tar', 198'compressor'=> ['bzip2']}, 199 200'txz'=> { 201'display'=>'tar.xz', 202'type'=>'application/x-xz', 203'suffix'=>'.tar.xz', 204'format'=>'tar', 205'compressor'=> ['xz'], 206'disabled'=>1}, 207 208'zip'=> { 209'display'=>'zip', 210'type'=>'application/x-zip', 211'suffix'=>'.zip', 212'format'=>'zip'}, 213); 214 215# Aliases so we understand old gitweb.snapshot values in repository 216# configuration. 217our%known_snapshot_format_aliases= ( 218'gzip'=>'tgz', 219'bzip2'=>'tbz2', 220'xz'=>'txz', 221 222# backward compatibility: legacy gitweb config support 223'x-gzip'=>undef,'gz'=>undef, 224'x-bzip2'=>undef,'bz2'=>undef, 225'x-zip'=>undef,''=>undef, 226); 227 228# Pixel sizes for icons and avatars. If the default font sizes or lineheights 229# are changed, it may be appropriate to change these values too via 230# $GITWEB_CONFIG. 231our%avatar_size= ( 232'default'=>16, 233'double'=>32 234); 235 236# Used to set the maximum load that we will still respond to gitweb queries. 237# If server load exceed this value then return "503 server busy" error. 238# If gitweb cannot determined server load, it is taken to be 0. 239# Leave it undefined (or set to 'undef') to turn off load checking. 240our$maxload=300; 241 242# configuration for 'highlight' (http://www.andre-simon.de/) 243# match by basename 244our%highlight_basename= ( 245#'Program' => 'py', 246#'Library' => 'py', 247'SConstruct'=>'py',# SCons equivalent of Makefile 248'Makefile'=>'make', 249); 250# match by extension 251our%highlight_ext= ( 252# main extensions, defining name of syntax; 253# see files in /usr/share/highlight/langDefs/ directory 254map{$_=>$_} 255qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl), 256# alternate extensions, see /etc/highlight/filetypes.conf 257'h'=>'c', 258map{$_=>'cpp'}qw(cxx c++ cc), 259map{$_=>'php'}qw(php3 php4), 260map{$_=>'pl'}qw(perl pm),# perhaps also 'cgi' 261'mak'=>'make', 262map{$_=>'xml'}qw(xhtml html htm), 263); 264 265# You define site-wide feature defaults here; override them with 266# $GITWEB_CONFIG as necessary. 267our%feature= ( 268# feature => { 269# 'sub' => feature-sub (subroutine), 270# 'override' => allow-override (boolean), 271# 'default' => [ default options...] (array reference)} 272# 273# if feature is overridable (it means that allow-override has true value), 274# then feature-sub will be called with default options as parameters; 275# return value of feature-sub indicates if to enable specified feature 276# 277# if there is no 'sub' key (no feature-sub), then feature cannot be 278# overridden 279# 280# use gitweb_get_feature(<feature>) to retrieve the <feature> value 281# (an array) or gitweb_check_feature(<feature>) to check if <feature> 282# is enabled 283 284# Enable the 'blame' blob view, showing the last commit that modified 285# each line in the file. This can be very CPU-intensive. 286 287# To enable system wide have in $GITWEB_CONFIG 288# $feature{'blame'}{'default'} = [1]; 289# To have project specific config enable override in $GITWEB_CONFIG 290# $feature{'blame'}{'override'} = 1; 291# and in project config gitweb.blame = 0|1; 292'blame'=> { 293'sub'=>sub{ feature_bool('blame',@_) }, 294'override'=>0, 295'default'=> [0]}, 296 297# Enable the 'snapshot' link, providing a compressed archive of any 298# tree. This can potentially generate high traffic if you have large 299# project. 300 301# Value is a list of formats defined in %known_snapshot_formats that 302# you wish to offer. 303# To disable system wide have in $GITWEB_CONFIG 304# $feature{'snapshot'}{'default'} = []; 305# To have project specific config enable override in $GITWEB_CONFIG 306# $feature{'snapshot'}{'override'} = 1; 307# and in project config, a comma-separated list of formats or "none" 308# to disable. Example: gitweb.snapshot = tbz2,zip; 309'snapshot'=> { 310'sub'=> \&feature_snapshot, 311'override'=>0, 312'default'=> ['tgz']}, 313 314# Enable text search, which will list the commits which match author, 315# committer or commit text to a given string. Enabled by default. 316# Project specific override is not supported. 317'search'=> { 318'override'=>0, 319'default'=> [1]}, 320 321# Enable grep search, which will list the files in currently selected 322# tree containing the given string. Enabled by default. This can be 323# potentially CPU-intensive, of course. 324 325# To enable system wide have in $GITWEB_CONFIG 326# $feature{'grep'}{'default'} = [1]; 327# To have project specific config enable override in $GITWEB_CONFIG 328# $feature{'grep'}{'override'} = 1; 329# and in project config gitweb.grep = 0|1; 330'grep'=> { 331'sub'=>sub{ feature_bool('grep',@_) }, 332'override'=>0, 333'default'=> [1]}, 334 335# Enable the pickaxe search, which will list the commits that modified 336# a given string in a file. This can be practical and quite faster 337# alternative to 'blame', but still potentially CPU-intensive. 338 339# To enable system wide have in $GITWEB_CONFIG 340# $feature{'pickaxe'}{'default'} = [1]; 341# To have project specific config enable override in $GITWEB_CONFIG 342# $feature{'pickaxe'}{'override'} = 1; 343# and in project config gitweb.pickaxe = 0|1; 344'pickaxe'=> { 345'sub'=>sub{ feature_bool('pickaxe',@_) }, 346'override'=>0, 347'default'=> [1]}, 348 349# Enable showing size of blobs in a 'tree' view, in a separate 350# column, similar to what 'ls -l' does. This cost a bit of IO. 351 352# To disable system wide have in $GITWEB_CONFIG 353# $feature{'show-sizes'}{'default'} = [0]; 354# To have project specific config enable override in $GITWEB_CONFIG 355# $feature{'show-sizes'}{'override'} = 1; 356# and in project config gitweb.showsizes = 0|1; 357'show-sizes'=> { 358'sub'=>sub{ feature_bool('showsizes',@_) }, 359'override'=>0, 360'default'=> [1]}, 361 362# Make gitweb use an alternative format of the URLs which can be 363# more readable and natural-looking: project name is embedded 364# directly in the path and the query string contains other 365# auxiliary information. All gitweb installations recognize 366# URL in either format; this configures in which formats gitweb 367# generates links. 368 369# To enable system wide have in $GITWEB_CONFIG 370# $feature{'pathinfo'}{'default'} = [1]; 371# Project specific override is not supported. 372 373# Note that you will need to change the default location of CSS, 374# favicon, logo and possibly other files to an absolute URL. Also, 375# if gitweb.cgi serves as your indexfile, you will need to force 376# $my_uri to contain the script name in your $GITWEB_CONFIG. 377'pathinfo'=> { 378'override'=>0, 379'default'=> [0]}, 380 381# Make gitweb consider projects in project root subdirectories 382# to be forks of existing projects. Given project $projname.git, 383# projects matching $projname/*.git will not be shown in the main 384# projects list, instead a '+' mark will be added to $projname 385# there and a 'forks' view will be enabled for the project, listing 386# all the forks. If project list is taken from a file, forks have 387# to be listed after the main project. 388 389# To enable system wide have in $GITWEB_CONFIG 390# $feature{'forks'}{'default'} = [1]; 391# Project specific override is not supported. 392'forks'=> { 393'override'=>0, 394'default'=> [0]}, 395 396# Insert custom links to the action bar of all project pages. 397# This enables you mainly to link to third-party scripts integrating 398# into gitweb; e.g. git-browser for graphical history representation 399# or custom web-based repository administration interface. 400 401# The 'default' value consists of a list of triplets in the form 402# (label, link, position) where position is the label after which 403# to insert the link and link is a format string where %n expands 404# to the project name, %f to the project path within the filesystem, 405# %h to the current hash (h gitweb parameter) and %b to the current 406# hash base (hb gitweb parameter); %% expands to %. 407 408# To enable system wide have in $GITWEB_CONFIG e.g. 409# $feature{'actions'}{'default'} = [('graphiclog', 410# '/git-browser/by-commit.html?r=%n', 'summary')]; 411# Project specific override is not supported. 412'actions'=> { 413'override'=>0, 414'default'=> []}, 415 416# Allow gitweb scan project content tags described in ctags/ 417# of project repository, and display the popular Web 2.0-ish 418# "tag cloud" near the project list. Note that this is something 419# COMPLETELY different from the normal Git tags. 420 421# gitweb by itself can show existing tags, but it does not handle 422# tagging itself; you need an external application for that. 423# For an example script, check Girocco's cgi/tagproj.cgi. 424# You may want to install the HTML::TagCloud Perl module to get 425# a pretty tag cloud instead of just a list of tags. 426 427# To enable system wide have in $GITWEB_CONFIG 428# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 429# Project specific override is not supported. 430'ctags'=> { 431'override'=>0, 432'default'=> [0]}, 433 434# The maximum number of patches in a patchset generated in patch 435# view. Set this to 0 or undef to disable patch view, or to a 436# negative number to remove any limit. 437 438# To disable system wide have in $GITWEB_CONFIG 439# $feature{'patches'}{'default'} = [0]; 440# To have project specific config enable override in $GITWEB_CONFIG 441# $feature{'patches'}{'override'} = 1; 442# and in project config gitweb.patches = 0|n; 443# where n is the maximum number of patches allowed in a patchset. 444'patches'=> { 445'sub'=> \&feature_patches, 446'override'=>0, 447'default'=> [16]}, 448 449# Avatar support. When this feature is enabled, views such as 450# shortlog or commit will display an avatar associated with 451# the email of the committer(s) and/or author(s). 452 453# Currently available providers are gravatar and picon. 454# If an unknown provider is specified, the feature is disabled. 455 456# Gravatar depends on Digest::MD5. 457# Picon currently relies on the indiana.edu database. 458 459# To enable system wide have in $GITWEB_CONFIG 460# $feature{'avatar'}{'default'} = ['<provider>']; 461# where <provider> is either gravatar or picon. 462# To have project specific config enable override in $GITWEB_CONFIG 463# $feature{'avatar'}{'override'} = 1; 464# and in project config gitweb.avatar = <provider>; 465'avatar'=> { 466'sub'=> \&feature_avatar, 467'override'=>0, 468'default'=> ['']}, 469 470# Enable displaying how much time and how many git commands 471# it took to generate and display page. Disabled by default. 472# Project specific override is not supported. 473'timed'=> { 474'override'=>0, 475'default'=> [0]}, 476 477# Enable turning some links into links to actions which require 478# JavaScript to run (like 'blame_incremental'). Not enabled by 479# default. Project specific override is currently not supported. 480'javascript-actions'=> { 481'override'=>0, 482'default'=> [0]}, 483 484# Syntax highlighting support. This is based on Daniel Svensson's 485# and Sham Chukoury's work in gitweb-xmms2.git. 486# It requires the 'highlight' program present in $PATH, 487# and therefore is disabled by default. 488 489# To enable system wide have in $GITWEB_CONFIG 490# $feature{'highlight'}{'default'} = [1]; 491 492'highlight'=> { 493'sub'=>sub{ feature_bool('highlight',@_) }, 494'override'=>0, 495'default'=> [0]}, 496); 497 498sub gitweb_get_feature { 499my($name) =@_; 500return unlessexists$feature{$name}; 501my($sub,$override,@defaults) = ( 502$feature{$name}{'sub'}, 503$feature{$name}{'override'}, 504@{$feature{$name}{'default'}}); 505# project specific override is possible only if we have project 506our$git_dir;# global variable, declared later 507if(!$override|| !defined$git_dir) { 508return@defaults; 509} 510if(!defined$sub) { 511warn"feature$nameis not overridable"; 512return@defaults; 513} 514return$sub->(@defaults); 515} 516 517# A wrapper to check if a given feature is enabled. 518# With this, you can say 519# 520# my $bool_feat = gitweb_check_feature('bool_feat'); 521# gitweb_check_feature('bool_feat') or somecode; 522# 523# instead of 524# 525# my ($bool_feat) = gitweb_get_feature('bool_feat'); 526# (gitweb_get_feature('bool_feat'))[0] or somecode; 527# 528sub gitweb_check_feature { 529return(gitweb_get_feature(@_))[0]; 530} 531 532 533sub feature_bool { 534my$key=shift; 535my($val) = git_get_project_config($key,'--bool'); 536 537if(!defined$val) { 538return($_[0]); 539}elsif($valeq'true') { 540return(1); 541}elsif($valeq'false') { 542return(0); 543} 544} 545 546sub feature_snapshot { 547my(@fmts) =@_; 548 549my($val) = git_get_project_config('snapshot'); 550 551if($val) { 552@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 553} 554 555return@fmts; 556} 557 558sub feature_patches { 559my@val= (git_get_project_config('patches','--int')); 560 561if(@val) { 562return@val; 563} 564 565return($_[0]); 566} 567 568sub feature_avatar { 569my@val= (git_get_project_config('avatar')); 570 571return@val?@val:@_; 572} 573 574# checking HEAD file with -e is fragile if the repository was 575# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 576# and then pruned. 577sub check_head_link { 578my($dir) =@_; 579my$headfile="$dir/HEAD"; 580return((-e $headfile) || 581(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 582} 583 584sub check_export_ok { 585my($dir) =@_; 586return(check_head_link($dir) && 587(!$export_ok|| -e "$dir/$export_ok") && 588(!$export_auth_hook||$export_auth_hook->($dir))); 589} 590 591# process alternate names for backward compatibility 592# filter out unsupported (unknown) snapshot formats 593sub filter_snapshot_fmts { 594my@fmts=@_; 595 596@fmts=map{ 597exists$known_snapshot_format_aliases{$_} ? 598$known_snapshot_format_aliases{$_} :$_}@fmts; 599@fmts=grep{ 600exists$known_snapshot_formats{$_} && 601!$known_snapshot_formats{$_}{'disabled'}}@fmts; 602} 603 604our($GITWEB_CONFIG,$GITWEB_CONFIG_SYSTEM); 605sub evaluate_gitweb_config { 606our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 607our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 608# die if there are errors parsing config file 609if(-e $GITWEB_CONFIG) { 610do$GITWEB_CONFIG; 611die$@if$@; 612}elsif(-e $GITWEB_CONFIG_SYSTEM) { 613do$GITWEB_CONFIG_SYSTEM; 614die$@if$@; 615} 616} 617 618# Get loadavg of system, to compare against $maxload. 619# Currently it requires '/proc/loadavg' present to get loadavg; 620# if it is not present it returns 0, which means no load checking. 621sub get_loadavg { 622if( -e '/proc/loadavg'){ 623open my$fd,'<','/proc/loadavg' 624orreturn0; 625my@load=split(/\s+/,scalar<$fd>); 626close$fd; 627 628# The first three columns measure CPU and IO utilization of the last one, 629# five, and 10 minute periods. The fourth column shows the number of 630# currently running processes and the total number of processes in the m/n 631# format. The last column displays the last process ID used. 632return$load[0] ||0; 633} 634# additional checks for load average should go here for things that don't export 635# /proc/loadavg 636 637return0; 638} 639 640# version of the core git binary 641our$git_version; 642sub evaluate_git_version { 643our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 644$number_of_git_cmds++; 645} 646 647sub check_loadavg { 648if(defined$maxload&& get_loadavg() >$maxload) { 649 die_error(503,"The load average on the server is too high"); 650} 651} 652 653# ====================================================================== 654# input validation and dispatch 655 656# input parameters can be collected from a variety of sources (presently, CGI 657# and PATH_INFO), so we define an %input_params hash that collects them all 658# together during validation: this allows subsequent uses (e.g. href()) to be 659# agnostic of the parameter origin 660 661our%input_params= (); 662 663# input parameters are stored with the long parameter name as key. This will 664# also be used in the href subroutine to convert parameters to their CGI 665# equivalent, and since the href() usage is the most frequent one, we store 666# the name -> CGI key mapping here, instead of the reverse. 667# 668# XXX: Warning: If you touch this, check the search form for updating, 669# too. 670 671our@cgi_param_mapping= ( 672 project =>"p", 673 action =>"a", 674 file_name =>"f", 675 file_parent =>"fp", 676 hash =>"h", 677 hash_parent =>"hp", 678 hash_base =>"hb", 679 hash_parent_base =>"hpb", 680 page =>"pg", 681 order =>"o", 682 searchtext =>"s", 683 searchtype =>"st", 684 snapshot_format =>"sf", 685 extra_options =>"opt", 686 search_use_regexp =>"sr", 687# this must be last entry (for manipulation from JavaScript) 688 javascript =>"js" 689); 690our%cgi_param_mapping=@cgi_param_mapping; 691 692# we will also need to know the possible actions, for validation 693our%actions= ( 694"blame"=> \&git_blame, 695"blame_incremental"=> \&git_blame_incremental, 696"blame_data"=> \&git_blame_data, 697"blobdiff"=> \&git_blobdiff, 698"blobdiff_plain"=> \&git_blobdiff_plain, 699"blob"=> \&git_blob, 700"blob_plain"=> \&git_blob_plain, 701"commitdiff"=> \&git_commitdiff, 702"commitdiff_plain"=> \&git_commitdiff_plain, 703"commit"=> \&git_commit, 704"forks"=> \&git_forks, 705"heads"=> \&git_heads, 706"history"=> \&git_history, 707"log"=> \&git_log, 708"patch"=> \&git_patch, 709"patches"=> \&git_patches, 710"rss"=> \&git_rss, 711"atom"=> \&git_atom, 712"search"=> \&git_search, 713"search_help"=> \&git_search_help, 714"shortlog"=> \&git_shortlog, 715"summary"=> \&git_summary, 716"tag"=> \&git_tag, 717"tags"=> \&git_tags, 718"tree"=> \&git_tree, 719"snapshot"=> \&git_snapshot, 720"object"=> \&git_object, 721# those below don't need $project 722"opml"=> \&git_opml, 723"project_list"=> \&git_project_list, 724"project_index"=> \&git_project_index, 725); 726 727# finally, we have the hash of allowed extra_options for the commands that 728# allow them 729our%allowed_options= ( 730"--no-merges"=> [qw(rss atom log shortlog history)], 731); 732 733# fill %input_params with the CGI parameters. All values except for 'opt' 734# should be single values, but opt can be an array. We should probably 735# build an array of parameters that can be multi-valued, but since for the time 736# being it's only this one, we just single it out 737sub evaluate_query_params { 738our$cgi; 739 740while(my($name,$symbol) =each%cgi_param_mapping) { 741if($symboleq'opt') { 742$input_params{$name} = [$cgi->param($symbol) ]; 743}else{ 744$input_params{$name} =$cgi->param($symbol); 745} 746} 747} 748 749# now read PATH_INFO and update the parameter list for missing parameters 750sub evaluate_path_info { 751return ifdefined$input_params{'project'}; 752return if!$path_info; 753$path_info=~ s,^/+,,; 754return if!$path_info; 755 756# find which part of PATH_INFO is project 757my$project=$path_info; 758$project=~ s,/+$,,; 759while($project&& !check_head_link("$projectroot/$project")) { 760$project=~ s,/*[^/]*$,,; 761} 762return unless$project; 763$input_params{'project'} =$project; 764 765# do not change any parameters if an action is given using the query string 766return if$input_params{'action'}; 767$path_info=~ s,^\Q$project\E/*,,; 768 769# next, check if we have an action 770my$action=$path_info; 771$action=~ s,/.*$,,; 772if(exists$actions{$action}) { 773$path_info=~ s,^$action/*,,; 774$input_params{'action'} =$action; 775} 776 777# list of actions that want hash_base instead of hash, but can have no 778# pathname (f) parameter 779my@wants_base= ( 780'tree', 781'history', 782); 783 784# we want to catch, among others 785# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 786my($parentrefname,$parentpathname,$refname,$pathname) = 787($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/); 788 789# first, analyze the 'current' part 790if(defined$pathname) { 791# we got "branch:filename" or "branch:dir/" 792# we could use git_get_type(branch:pathname), but: 793# - it needs $git_dir 794# - it does a git() call 795# - the convention of terminating directories with a slash 796# makes it superfluous 797# - embedding the action in the PATH_INFO would make it even 798# more superfluous 799$pathname=~ s,^/+,,; 800if(!$pathname||substr($pathname, -1)eq"/") { 801$input_params{'action'} ||="tree"; 802$pathname=~ s,/$,,; 803}else{ 804# the default action depends on whether we had parent info 805# or not 806if($parentrefname) { 807$input_params{'action'} ||="blobdiff_plain"; 808}else{ 809$input_params{'action'} ||="blob_plain"; 810} 811} 812$input_params{'hash_base'} ||=$refname; 813$input_params{'file_name'} ||=$pathname; 814}elsif(defined$refname) { 815# we got "branch". In this case we have to choose if we have to 816# set hash or hash_base. 817# 818# Most of the actions without a pathname only want hash to be 819# set, except for the ones specified in @wants_base that want 820# hash_base instead. It should also be noted that hand-crafted 821# links having 'history' as an action and no pathname or hash 822# set will fail, but that happens regardless of PATH_INFO. 823if(defined$parentrefname) { 824# if there is parent let the default be 'shortlog' action 825# (for http://git.example.com/repo.git/A..B links); if there 826# is no parent, dispatch will detect type of object and set 827# action appropriately if required (if action is not set) 828$input_params{'action'} ||="shortlog"; 829} 830if($input_params{'action'} && 831grep{$_eq$input_params{'action'} }@wants_base) { 832$input_params{'hash_base'} ||=$refname; 833}else{ 834$input_params{'hash'} ||=$refname; 835} 836} 837 838# next, handle the 'parent' part, if present 839if(defined$parentrefname) { 840# a missing pathspec defaults to the 'current' filename, allowing e.g. 841# someproject/blobdiff/oldrev..newrev:/filename 842if($parentpathname) { 843$parentpathname=~ s,^/+,,; 844$parentpathname=~ s,/$,,; 845$input_params{'file_parent'} ||=$parentpathname; 846}else{ 847$input_params{'file_parent'} ||=$input_params{'file_name'}; 848} 849# we assume that hash_parent_base is wanted if a path was specified, 850# or if the action wants hash_base instead of hash 851if(defined$input_params{'file_parent'} || 852grep{$_eq$input_params{'action'} }@wants_base) { 853$input_params{'hash_parent_base'} ||=$parentrefname; 854}else{ 855$input_params{'hash_parent'} ||=$parentrefname; 856} 857} 858 859# for the snapshot action, we allow URLs in the form 860# $project/snapshot/$hash.ext 861# where .ext determines the snapshot and gets removed from the 862# passed $refname to provide the $hash. 863# 864# To be able to tell that $refname includes the format extension, we 865# require the following two conditions to be satisfied: 866# - the hash input parameter MUST have been set from the $refname part 867# of the URL (i.e. they must be equal) 868# - the snapshot format MUST NOT have been defined already (e.g. from 869# CGI parameter sf) 870# It's also useless to try any matching unless $refname has a dot, 871# so we check for that too 872if(defined$input_params{'action'} && 873$input_params{'action'}eq'snapshot'&& 874defined$refname&&index($refname,'.') != -1&& 875$refnameeq$input_params{'hash'} && 876!defined$input_params{'snapshot_format'}) { 877# We loop over the known snapshot formats, checking for 878# extensions. Allowed extensions are both the defined suffix 879# (which includes the initial dot already) and the snapshot 880# format key itself, with a prepended dot 881while(my($fmt,$opt) =each%known_snapshot_formats) { 882my$hash=$refname; 883unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 884next; 885} 886my$sfx=$1; 887# a valid suffix was found, so set the snapshot format 888# and reset the hash parameter 889$input_params{'snapshot_format'} =$fmt; 890$input_params{'hash'} =$hash; 891# we also set the format suffix to the one requested 892# in the URL: this way a request for e.g. .tgz returns 893# a .tgz instead of a .tar.gz 894$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 895last; 896} 897} 898} 899 900our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 901$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 902$searchtext,$search_regexp); 903sub evaluate_and_validate_params { 904our$action=$input_params{'action'}; 905if(defined$action) { 906if(!validate_action($action)) { 907 die_error(400,"Invalid action parameter"); 908} 909} 910 911# parameters which are pathnames 912our$project=$input_params{'project'}; 913if(defined$project) { 914if(!validate_project($project)) { 915undef$project; 916 die_error(404,"No such project"); 917} 918} 919 920our$file_name=$input_params{'file_name'}; 921if(defined$file_name) { 922if(!validate_pathname($file_name)) { 923 die_error(400,"Invalid file parameter"); 924} 925} 926 927our$file_parent=$input_params{'file_parent'}; 928if(defined$file_parent) { 929if(!validate_pathname($file_parent)) { 930 die_error(400,"Invalid file parent parameter"); 931} 932} 933 934# parameters which are refnames 935our$hash=$input_params{'hash'}; 936if(defined$hash) { 937if(!validate_refname($hash)) { 938 die_error(400,"Invalid hash parameter"); 939} 940} 941 942our$hash_parent=$input_params{'hash_parent'}; 943if(defined$hash_parent) { 944if(!validate_refname($hash_parent)) { 945 die_error(400,"Invalid hash parent parameter"); 946} 947} 948 949our$hash_base=$input_params{'hash_base'}; 950if(defined$hash_base) { 951if(!validate_refname($hash_base)) { 952 die_error(400,"Invalid hash base parameter"); 953} 954} 955 956our@extra_options= @{$input_params{'extra_options'}}; 957# @extra_options is always defined, since it can only be (currently) set from 958# CGI, and $cgi->param() returns the empty array in array context if the param 959# is not set 960foreachmy$opt(@extra_options) { 961if(not exists$allowed_options{$opt}) { 962 die_error(400,"Invalid option parameter"); 963} 964if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 965 die_error(400,"Invalid option parameter for this action"); 966} 967} 968 969our$hash_parent_base=$input_params{'hash_parent_base'}; 970if(defined$hash_parent_base) { 971if(!validate_refname($hash_parent_base)) { 972 die_error(400,"Invalid hash parent base parameter"); 973} 974} 975 976# other parameters 977our$page=$input_params{'page'}; 978if(defined$page) { 979if($page=~m/[^0-9]/) { 980 die_error(400,"Invalid page parameter"); 981} 982} 983 984our$searchtype=$input_params{'searchtype'}; 985if(defined$searchtype) { 986if($searchtype=~m/[^a-z]/) { 987 die_error(400,"Invalid searchtype parameter"); 988} 989} 990 991our$search_use_regexp=$input_params{'search_use_regexp'}; 992 993our$searchtext=$input_params{'searchtext'}; 994our$search_regexp; 995if(defined$searchtext) { 996if(length($searchtext) <2) { 997 die_error(403,"At least two characters are required for search parameter"); 998} 999$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext;1000}1001}10021003# path to the current git repository1004our$git_dir;1005sub evaluate_git_dir {1006our$git_dir="$projectroot/$project"if$project;1007}10081009our(@snapshot_fmts,$git_avatar);1010sub configure_gitweb_features {1011# list of supported snapshot formats1012our@snapshot_fmts= gitweb_get_feature('snapshot');1013@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);10141015# check that the avatar feature is set to a known provider name,1016# and for each provider check if the dependencies are satisfied.1017# if the provider name is invalid or the dependencies are not met,1018# reset $git_avatar to the empty string.1019our($git_avatar) = gitweb_get_feature('avatar');1020if($git_avatareq'gravatar') {1021$git_avatar=''unless(eval{require Digest::MD5;1; });1022}elsif($git_avatareq'picon') {1023# no dependencies1024}else{1025$git_avatar='';1026}1027}10281029# custom error handler: 'die <message>' is Internal Server Error1030sub handle_errors_html {1031my$msg=shift;# it is already HTML escaped10321033# to avoid infinite loop where error occurs in die_error,1034# change handler to default handler, disabling handle_errors_html1035 set_message("Error occured when inside die_error:\n$msg");10361037# you cannot jump out of die_error when called as error handler;1038# the subroutine set via CGI::Carp::set_message is called _after_1039# HTTP headers are already written, so it cannot write them itself1040 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1);1041}1042set_message(\&handle_errors_html);10431044# dispatch1045sub dispatch {1046if(!defined$action) {1047if(defined$hash) {1048$action= git_get_type($hash);1049}elsif(defined$hash_base&&defined$file_name) {1050$action= git_get_type("$hash_base:$file_name");1051}elsif(defined$project) {1052$action='summary';1053}else{1054$action='project_list';1055}1056}1057if(!defined($actions{$action})) {1058 die_error(400,"Unknown action");1059}1060if($action!~m/^(?:opml|project_list|project_index)$/&&1061!$project) {1062 die_error(400,"Project needed");1063}1064$actions{$action}->();1065}10661067sub reset_timer {1068our$t0= [Time::HiRes::gettimeofday()]1069ifdefined$t0;1070our$number_of_git_cmds=0;1071}10721073sub run_request {1074 reset_timer();10751076 evaluate_uri();1077 evaluate_gitweb_config();1078 evaluate_git_version();1079 check_loadavg();10801081# $projectroot and $projects_list might be set in gitweb config file1082$projects_list||=$projectroot;10831084 evaluate_query_params();1085 evaluate_path_info();1086 evaluate_and_validate_params();1087 evaluate_git_dir();10881089 configure_gitweb_features();10901091 dispatch();1092}10931094our$is_last_request=sub{1};1095our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1096our$CGI='CGI';1097our$cgi;1098sub configure_as_fcgi {1099require CGI::Fast;1100our$CGI='CGI::Fast';11011102my$request_number=0;1103# let each child service 100 requests1104our$is_last_request=sub{ ++$request_number>100};1105}1106sub evaluate_argv {1107my$script_name=$ENV{'SCRIPT_NAME'} ||$ENV{'SCRIPT_FILENAME'} || __FILE__;1108 configure_as_fcgi()1109if$script_name=~/\.fcgi$/;11101111return unless(@ARGV);11121113require Getopt::Long;1114 Getopt::Long::GetOptions(1115'fastcgi|fcgi|f'=> \&configure_as_fcgi,1116'nproc|n=i'=>sub{1117my($arg,$val) =@_;1118return unlesseval{require FCGI::ProcManager;1; };1119my$proc_manager= FCGI::ProcManager->new({1120 n_processes =>$val,1121});1122our$pre_listen_hook=sub{$proc_manager->pm_manage() };1123our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1124our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1125},1126);1127}11281129sub run {1130 evaluate_argv();11311132$pre_listen_hook->()1133if$pre_listen_hook;11341135 REQUEST:1136while($cgi=$CGI->new()) {1137$pre_dispatch_hook->()1138if$pre_dispatch_hook;11391140 run_request();11411142$post_dispatch_hook->()1143if$post_dispatch_hook;11441145last REQUEST if($is_last_request->());1146}11471148 DONE_GITWEB:11491;1150}11511152run();11531154if(defined caller) {1155# wrapped in a subroutine processing requests,1156# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1157return;1158}else{1159# pure CGI script, serving single request1160exit;1161}11621163## ======================================================================1164## action links11651166# possible values of extra options1167# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1168# -replay => 1 - start from a current view (replay with modifications)1169# -path_info => 0|1 - don't use/use path_info URL (if possible)1170sub href {1171my%params=@_;1172# default is to use -absolute url() i.e. $my_uri1173my$href=$params{-full} ?$my_url:$my_uri;11741175$params{'project'} =$projectunlessexists$params{'project'};11761177if($params{-replay}) {1178while(my($name,$symbol) =each%cgi_param_mapping) {1179if(!exists$params{$name}) {1180$params{$name} =$input_params{$name};1181}1182}1183}11841185my$use_pathinfo= gitweb_check_feature('pathinfo');1186if(defined$params{'project'} &&1187(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1188# try to put as many parameters as possible in PATH_INFO:1189# - project name1190# - action1191# - hash_parent or hash_parent_base:/file_parent1192# - hash or hash_base:/filename1193# - the snapshot_format as an appropriate suffix11941195# When the script is the root DirectoryIndex for the domain,1196# $href here would be something like http://gitweb.example.com/1197# Thus, we strip any trailing / from $href, to spare us double1198# slashes in the final URL1199$href=~ s,/$,,;12001201# Then add the project name, if present1202$href.="/".esc_url($params{'project'});1203delete$params{'project'};12041205# since we destructively absorb parameters, we keep this1206# boolean that remembers if we're handling a snapshot1207my$is_snapshot=$params{'action'}eq'snapshot';12081209# Summary just uses the project path URL, any other action is1210# added to the URL1211if(defined$params{'action'}) {1212$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1213delete$params{'action'};1214}12151216# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1217# stripping nonexistent or useless pieces1218$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1219||$params{'hash_parent'} ||$params{'hash'});1220if(defined$params{'hash_base'}) {1221if(defined$params{'hash_parent_base'}) {1222$href.= esc_url($params{'hash_parent_base'});1223# skip the file_parent if it's the same as the file_name1224if(defined$params{'file_parent'}) {1225if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1226delete$params{'file_parent'};1227}elsif($params{'file_parent'} !~/\.\./) {1228$href.=":/".esc_url($params{'file_parent'});1229delete$params{'file_parent'};1230}1231}1232$href.="..";1233delete$params{'hash_parent'};1234delete$params{'hash_parent_base'};1235}elsif(defined$params{'hash_parent'}) {1236$href.= esc_url($params{'hash_parent'})."..";1237delete$params{'hash_parent'};1238}12391240$href.= esc_url($params{'hash_base'});1241if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1242$href.=":/".esc_url($params{'file_name'});1243delete$params{'file_name'};1244}1245delete$params{'hash'};1246delete$params{'hash_base'};1247}elsif(defined$params{'hash'}) {1248$href.= esc_url($params{'hash'});1249delete$params{'hash'};1250}12511252# If the action was a snapshot, we can absorb the1253# snapshot_format parameter too1254if($is_snapshot) {1255my$fmt=$params{'snapshot_format'};1256# snapshot_format should always be defined when href()1257# is called, but just in case some code forgets, we1258# fall back to the default1259$fmt||=$snapshot_fmts[0];1260$href.=$known_snapshot_formats{$fmt}{'suffix'};1261delete$params{'snapshot_format'};1262}1263}12641265# now encode the parameters explicitly1266my@result= ();1267for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1268my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1269if(defined$params{$name}) {1270if(ref($params{$name})eq"ARRAY") {1271foreachmy$par(@{$params{$name}}) {1272push@result,$symbol."=". esc_param($par);1273}1274}else{1275push@result,$symbol."=". esc_param($params{$name});1276}1277}1278}1279$href.="?".join(';',@result)ifscalar@result;12801281return$href;1282}128312841285## ======================================================================1286## validation, quoting/unquoting and escaping12871288sub validate_action {1289my$input=shift||returnundef;1290returnundefunlessexists$actions{$input};1291return$input;1292}12931294sub validate_project {1295my$input=shift||returnundef;1296if(!validate_pathname($input) ||1297!(-d "$projectroot/$input") ||1298!check_export_ok("$projectroot/$input") ||1299($strict_export&& !project_in_list($input))) {1300returnundef;1301}else{1302return$input;1303}1304}13051306sub validate_pathname {1307my$input=shift||returnundef;13081309# no '.' or '..' as elements of path, i.e. no '.' nor '..'1310# at the beginning, at the end, and between slashes.1311# also this catches doubled slashes1312if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1313returnundef;1314}1315# no null characters1316if($input=~m!\0!) {1317returnundef;1318}1319return$input;1320}13211322sub validate_refname {1323my$input=shift||returnundef;13241325# textual hashes are O.K.1326if($input=~m/^[0-9a-fA-F]{40}$/) {1327return$input;1328}1329# it must be correct pathname1330$input= validate_pathname($input)1331orreturnundef;1332# restrictions on ref name according to git-check-ref-format1333if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1334returnundef;1335}1336return$input;1337}13381339# decode sequences of octets in utf8 into Perl's internal form,1340# which is utf-8 with utf8 flag set if needed. gitweb writes out1341# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1342sub to_utf8 {1343my$str=shift;1344returnundefunlessdefined$str;1345if(utf8::valid($str)) {1346 utf8::decode($str);1347return$str;1348}else{1349return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1350}1351}13521353# quote unsafe chars, but keep the slash, even when it's not1354# correct, but quoted slashes look too horrible in bookmarks1355sub esc_param {1356my$str=shift;1357returnundefunlessdefined$str;1358$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1359$str=~s/ /\+/g;1360return$str;1361}13621363# quote unsafe chars in whole URL, so some characters cannot be quoted1364sub esc_url {1365my$str=shift;1366returnundefunlessdefined$str;1367$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;1368$str=~s/ /\+/g;1369return$str;1370}13711372# replace invalid utf8 character with SUBSTITUTION sequence1373sub esc_html {1374my$str=shift;1375my%opts=@_;13761377returnundefunlessdefined$str;13781379$str= to_utf8($str);1380$str=$cgi->escapeHTML($str);1381if($opts{'-nbsp'}) {1382$str=~s/ / /g;1383}1384$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1385return$str;1386}13871388# quote control characters and escape filename to HTML1389sub esc_path {1390my$str=shift;1391my%opts=@_;13921393returnundefunlessdefined$str;13941395$str= to_utf8($str);1396$str=$cgi->escapeHTML($str);1397if($opts{'-nbsp'}) {1398$str=~s/ / /g;1399}1400$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1401return$str;1402}14031404# Make control characters "printable", using character escape codes (CEC)1405sub quot_cec {1406my$cntrl=shift;1407my%opts=@_;1408my%es= (# character escape codes, aka escape sequences1409"\t"=>'\t',# tab (HT)1410"\n"=>'\n',# line feed (LF)1411"\r"=>'\r',# carrige return (CR)1412"\f"=>'\f',# form feed (FF)1413"\b"=>'\b',# backspace (BS)1414"\a"=>'\a',# alarm (bell) (BEL)1415"\e"=>'\e',# escape (ESC)1416"\013"=>'\v',# vertical tab (VT)1417"\000"=>'\0',# nul character (NUL)1418);1419my$chr= ( (exists$es{$cntrl})1420?$es{$cntrl}1421:sprintf('\%2x',ord($cntrl)) );1422if($opts{-nohtml}) {1423return$chr;1424}else{1425return"<span class=\"cntrl\">$chr</span>";1426}1427}14281429# Alternatively use unicode control pictures codepoints,1430# Unicode "printable representation" (PR)1431sub quot_upr {1432my$cntrl=shift;1433my%opts=@_;14341435my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1436if($opts{-nohtml}) {1437return$chr;1438}else{1439return"<span class=\"cntrl\">$chr</span>";1440}1441}14421443# git may return quoted and escaped filenames1444sub unquote {1445my$str=shift;14461447sub unq {1448my$seq=shift;1449my%es= (# character escape codes, aka escape sequences1450't'=>"\t",# tab (HT, TAB)1451'n'=>"\n",# newline (NL)1452'r'=>"\r",# return (CR)1453'f'=>"\f",# form feed (FF)1454'b'=>"\b",# backspace (BS)1455'a'=>"\a",# alarm (bell) (BEL)1456'e'=>"\e",# escape (ESC)1457'v'=>"\013",# vertical tab (VT)1458);14591460if($seq=~m/^[0-7]{1,3}$/) {1461# octal char sequence1462returnchr(oct($seq));1463}elsif(exists$es{$seq}) {1464# C escape sequence, aka character escape code1465return$es{$seq};1466}1467# quoted ordinary character1468return$seq;1469}14701471if($str=~m/^"(.*)"$/) {1472# needs unquoting1473$str=$1;1474$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1475}1476return$str;1477}14781479# escape tabs (convert tabs to spaces)1480sub untabify {1481my$line=shift;14821483while((my$pos=index($line,"\t")) != -1) {1484if(my$count= (8- ($pos%8))) {1485my$spaces=' ' x $count;1486$line=~s/\t/$spaces/;1487}1488}14891490return$line;1491}14921493sub project_in_list {1494my$project=shift;1495my@list= git_get_projects_list();1496return@list&&scalar(grep{$_->{'path'}eq$project}@list);1497}14981499## ----------------------------------------------------------------------1500## HTML aware string manipulation15011502# Try to chop given string on a word boundary between position1503# $len and $len+$add_len. If there is no word boundary there,1504# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1505# (marking chopped part) would be longer than given string.1506sub chop_str {1507my$str=shift;1508my$len=shift;1509my$add_len=shift||10;1510my$where=shift||'right';# 'left' | 'center' | 'right'15111512# Make sure perl knows it is utf8 encoded so we don't1513# cut in the middle of a utf8 multibyte char.1514$str= to_utf8($str);15151516# allow only $len chars, but don't cut a word if it would fit in $add_len1517# if it doesn't fit, cut it if it's still longer than the dots we would add1518# remove chopped character entities entirely15191520# when chopping in the middle, distribute $len into left and right part1521# return early if chopping wouldn't make string shorter1522if($whereeq'center') {1523return$strif($len+5>=length($str));# filler is length 51524$len=int($len/2);1525}else{1526return$strif($len+4>=length($str));# filler is length 41527}15281529# regexps: ending and beginning with word part up to $add_len1530my$endre=qr/.{$len}\w{0,$add_len}/;1531my$begre=qr/\w{0,$add_len}.{$len}/;15321533if($whereeq'left') {1534$str=~m/^(.*?)($begre)$/;1535my($lead,$body) = ($1,$2);1536if(length($lead) >4) {1537$lead=" ...";1538}1539return"$lead$body";15401541}elsif($whereeq'center') {1542$str=~m/^($endre)(.*)$/;1543my($left,$str) = ($1,$2);1544$str=~m/^(.*?)($begre)$/;1545my($mid,$right) = ($1,$2);1546if(length($mid) >5) {1547$mid=" ... ";1548}1549return"$left$mid$right";15501551}else{1552$str=~m/^($endre)(.*)$/;1553my$body=$1;1554my$tail=$2;1555if(length($tail) >4) {1556$tail="... ";1557}1558return"$body$tail";1559}1560}15611562# takes the same arguments as chop_str, but also wraps a <span> around the1563# result with a title attribute if it does get chopped. Additionally, the1564# string is HTML-escaped.1565sub chop_and_escape_str {1566my($str) =@_;15671568my$chopped= chop_str(@_);1569if($choppedeq$str) {1570return esc_html($chopped);1571}else{1572$str=~s/[[:cntrl:]]/?/g;1573return$cgi->span({-title=>$str}, esc_html($chopped));1574}1575}15761577## ----------------------------------------------------------------------1578## functions returning short strings15791580# CSS class for given age value (in seconds)1581sub age_class {1582my$age=shift;15831584if(!defined$age) {1585return"noage";1586}elsif($age<60*60*2) {1587return"age0";1588}elsif($age<60*60*24*2) {1589return"age1";1590}else{1591return"age2";1592}1593}15941595# convert age in seconds to "nn units ago" string1596sub age_string {1597my$age=shift;1598my$age_str;15991600if($age>60*60*24*365*2) {1601$age_str= (int$age/60/60/24/365);1602$age_str.=" years ago";1603}elsif($age>60*60*24*(365/12)*2) {1604$age_str=int$age/60/60/24/(365/12);1605$age_str.=" months ago";1606}elsif($age>60*60*24*7*2) {1607$age_str=int$age/60/60/24/7;1608$age_str.=" weeks ago";1609}elsif($age>60*60*24*2) {1610$age_str=int$age/60/60/24;1611$age_str.=" days ago";1612}elsif($age>60*60*2) {1613$age_str=int$age/60/60;1614$age_str.=" hours ago";1615}elsif($age>60*2) {1616$age_str=int$age/60;1617$age_str.=" min ago";1618}elsif($age>2) {1619$age_str=int$age;1620$age_str.=" sec ago";1621}else{1622$age_str.=" right now";1623}1624return$age_str;1625}16261627useconstant{1628 S_IFINVALID =>0030000,1629 S_IFGITLINK =>0160000,1630};16311632# submodule/subproject, a commit object reference1633sub S_ISGITLINK {1634my$mode=shift;16351636return(($mode& S_IFMT) == S_IFGITLINK)1637}16381639# convert file mode in octal to symbolic file mode string1640sub mode_str {1641my$mode=oct shift;16421643if(S_ISGITLINK($mode)) {1644return'm---------';1645}elsif(S_ISDIR($mode& S_IFMT)) {1646return'drwxr-xr-x';1647}elsif(S_ISLNK($mode)) {1648return'lrwxrwxrwx';1649}elsif(S_ISREG($mode)) {1650# git cares only about the executable bit1651if($mode& S_IXUSR) {1652return'-rwxr-xr-x';1653}else{1654return'-rw-r--r--';1655};1656}else{1657return'----------';1658}1659}16601661# convert file mode in octal to file type string1662sub file_type {1663my$mode=shift;16641665if($mode!~m/^[0-7]+$/) {1666return$mode;1667}else{1668$mode=oct$mode;1669}16701671if(S_ISGITLINK($mode)) {1672return"submodule";1673}elsif(S_ISDIR($mode& S_IFMT)) {1674return"directory";1675}elsif(S_ISLNK($mode)) {1676return"symlink";1677}elsif(S_ISREG($mode)) {1678return"file";1679}else{1680return"unknown";1681}1682}16831684# convert file mode in octal to file type description string1685sub file_type_long {1686my$mode=shift;16871688if($mode!~m/^[0-7]+$/) {1689return$mode;1690}else{1691$mode=oct$mode;1692}16931694if(S_ISGITLINK($mode)) {1695return"submodule";1696}elsif(S_ISDIR($mode& S_IFMT)) {1697return"directory";1698}elsif(S_ISLNK($mode)) {1699return"symlink";1700}elsif(S_ISREG($mode)) {1701if($mode& S_IXUSR) {1702return"executable";1703}else{1704return"file";1705};1706}else{1707return"unknown";1708}1709}171017111712## ----------------------------------------------------------------------1713## functions returning short HTML fragments, or transforming HTML fragments1714## which don't belong to other sections17151716# format line of commit message.1717sub format_log_line_html {1718my$line=shift;17191720$line= esc_html($line, -nbsp=>1);1721$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1722$cgi->a({-href => href(action=>"object", hash=>$1),1723-class=>"text"},$1);1724}eg;17251726return$line;1727}17281729# format marker of refs pointing to given object17301731# the destination action is chosen based on object type and current context:1732# - for annotated tags, we choose the tag view unless it's the current view1733# already, in which case we go to shortlog view1734# - for other refs, we keep the current view if we're in history, shortlog or1735# log view, and select shortlog otherwise1736sub format_ref_marker {1737my($refs,$id) =@_;1738my$markers='';17391740if(defined$refs->{$id}) {1741foreachmy$ref(@{$refs->{$id}}) {1742# this code exploits the fact that non-lightweight tags are the1743# only indirect objects, and that they are the only objects for which1744# we want to use tag instead of shortlog as action1745my($type,$name) =qw();1746my$indirect= ($ref=~s/\^\{\}$//);1747# e.g. tags/v2.6.11 or heads/next1748if($ref=~m!^(.*?)s?/(.*)$!) {1749$type=$1;1750$name=$2;1751}else{1752$type="ref";1753$name=$ref;1754}17551756my$class=$type;1757$class.=" indirect"if$indirect;17581759my$dest_action="shortlog";17601761if($indirect) {1762$dest_action="tag"unless$actioneq"tag";1763}elsif($action=~/^(history|(short)?log)$/) {1764$dest_action=$action;1765}17661767my$dest="";1768$dest.="refs/"unless$ref=~ m!^refs/!;1769$dest.=$ref;17701771my$link=$cgi->a({1772-href => href(1773 action=>$dest_action,1774 hash=>$dest1775)},$name);17761777$markers.=" <span class=\"$class\"title=\"$ref\">".1778$link."</span>";1779}1780}17811782if($markers) {1783return' <span class="refs">'.$markers.'</span>';1784}else{1785return"";1786}1787}17881789# format, perhaps shortened and with markers, title line1790sub format_subject_html {1791my($long,$short,$href,$extra) =@_;1792$extra=''unlessdefined($extra);17931794if(length($short) <length($long)) {1795$long=~s/[[:cntrl:]]/?/g;1796return$cgi->a({-href =>$href, -class=>"list subject",1797-title => to_utf8($long)},1798 esc_html($short)) .$extra;1799}else{1800return$cgi->a({-href =>$href, -class=>"list subject"},1801 esc_html($long)) .$extra;1802}1803}18041805# Rather than recomputing the url for an email multiple times, we cache it1806# after the first hit. This gives a visible benefit in views where the avatar1807# for the same email is used repeatedly (e.g. shortlog).1808# The cache is shared by all avatar engines (currently gravatar only), which1809# are free to use it as preferred. Since only one avatar engine is used for any1810# given page, there's no risk for cache conflicts.1811our%avatar_cache= ();18121813# Compute the picon url for a given email, by using the picon search service over at1814# http://www.cs.indiana.edu/picons/search.html1815sub picon_url {1816my$email=lc shift;1817if(!$avatar_cache{$email}) {1818my($user,$domain) =split('@',$email);1819$avatar_cache{$email} =1820"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1821"$domain/$user/".1822"users+domains+unknown/up/single";1823}1824return$avatar_cache{$email};1825}18261827# Compute the gravatar url for a given email, if it's not in the cache already.1828# Gravatar stores only the part of the URL before the size, since that's the1829# one computationally more expensive. This also allows reuse of the cache for1830# different sizes (for this particular engine).1831sub gravatar_url {1832my$email=lc shift;1833my$size=shift;1834$avatar_cache{$email} ||=1835"http://www.gravatar.com/avatar/".1836 Digest::MD5::md5_hex($email) ."?s=";1837return$avatar_cache{$email} .$size;1838}18391840# Insert an avatar for the given $email at the given $size if the feature1841# is enabled.1842sub git_get_avatar {1843my($email,%opts) =@_;1844my$pre_white= ($opts{-pad_before} ?" ":"");1845my$post_white= ($opts{-pad_after} ?" ":"");1846$opts{-size} ||='default';1847my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1848my$url="";1849if($git_avatareq'gravatar') {1850$url= gravatar_url($email,$size);1851}elsif($git_avatareq'picon') {1852$url= picon_url($email);1853}1854# Other providers can be added by extending the if chain, defining $url1855# as needed. If no variant puts something in $url, we assume avatars1856# are completely disabled/unavailable.1857if($url) {1858return$pre_white.1859"<img width=\"$size\"".1860"class=\"avatar\"".1861"src=\"$url\"".1862"alt=\"\"".1863"/>".$post_white;1864}else{1865return"";1866}1867}18681869sub format_search_author {1870my($author,$searchtype,$displaytext) =@_;1871my$have_search= gitweb_check_feature('search');18721873if($have_search) {1874my$performed="";1875if($searchtypeeq'author') {1876$performed="authored";1877}elsif($searchtypeeq'committer') {1878$performed="committed";1879}18801881return$cgi->a({-href => href(action=>"search", hash=>$hash,1882 searchtext=>$author,1883 searchtype=>$searchtype),class=>"list",1884 title=>"Search for commits$performedby$author"},1885$displaytext);18861887}else{1888return$displaytext;1889}1890}18911892# format the author name of the given commit with the given tag1893# the author name is chopped and escaped according to the other1894# optional parameters (see chop_str).1895sub format_author_html {1896my$tag=shift;1897my$co=shift;1898my$author= chop_and_escape_str($co->{'author_name'},@_);1899return"<$tagclass=\"author\">".1900 format_search_author($co->{'author_name'},"author",1901 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1902$author) .1903"</$tag>";1904}19051906# format git diff header line, i.e. "diff --(git|combined|cc) ..."1907sub format_git_diff_header_line {1908my$line=shift;1909my$diffinfo=shift;1910my($from,$to) =@_;19111912if($diffinfo->{'nparents'}) {1913# combined diff1914$line=~s!^(diff (.*?) )"?.*$!$1!;1915if($to->{'href'}) {1916$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1917 esc_path($to->{'file'}));1918}else{# file was deleted (no href)1919$line.= esc_path($to->{'file'});1920}1921}else{1922# "ordinary" diff1923$line=~s!^(diff (.*?) )"?a/.*$!$1!;1924if($from->{'href'}) {1925$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1926'a/'. esc_path($from->{'file'}));1927}else{# file was added (no href)1928$line.='a/'. esc_path($from->{'file'});1929}1930$line.=' ';1931if($to->{'href'}) {1932$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1933'b/'. esc_path($to->{'file'}));1934}else{# file was deleted1935$line.='b/'. esc_path($to->{'file'});1936}1937}19381939return"<div class=\"diff header\">$line</div>\n";1940}19411942# format extended diff header line, before patch itself1943sub format_extended_diff_header_line {1944my$line=shift;1945my$diffinfo=shift;1946my($from,$to) =@_;19471948# match <path>1949if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1950$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1951 esc_path($from->{'file'}));1952}1953if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1954$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1955 esc_path($to->{'file'}));1956}1957# match single <mode>1958if($line=~m/\s(\d{6})$/) {1959$line.='<span class="info"> ('.1960 file_type_long($1) .1961')</span>';1962}1963# match <hash>1964if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1965# can match only for combined diff1966$line='index ';1967for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1968if($from->{'href'}[$i]) {1969$line.=$cgi->a({-href=>$from->{'href'}[$i],1970-class=>"hash"},1971substr($diffinfo->{'from_id'}[$i],0,7));1972}else{1973$line.='0' x 7;1974}1975# separator1976$line.=','if($i<$diffinfo->{'nparents'} -1);1977}1978$line.='..';1979if($to->{'href'}) {1980$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1981substr($diffinfo->{'to_id'},0,7));1982}else{1983$line.='0' x 7;1984}19851986}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1987# can match only for ordinary diff1988my($from_link,$to_link);1989if($from->{'href'}) {1990$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1991substr($diffinfo->{'from_id'},0,7));1992}else{1993$from_link='0' x 7;1994}1995if($to->{'href'}) {1996$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1997substr($diffinfo->{'to_id'},0,7));1998}else{1999$to_link='0' x 7;2000}2001my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});2002$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;2003}20042005return$line."<br/>\n";2006}20072008# format from-file/to-file diff header2009sub format_diff_from_to_header {2010my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;2011my$line;2012my$result='';20132014$line=$from_line;2015#assert($line =~ m/^---/) if DEBUG;2016# no extra formatting for "^--- /dev/null"2017if(!$diffinfo->{'nparents'}) {2018# ordinary (single parent) diff2019if($line=~m!^--- "?a/!) {2020if($from->{'href'}) {2021$line='--- a/'.2022$cgi->a({-href=>$from->{'href'}, -class=>"path"},2023 esc_path($from->{'file'}));2024}else{2025$line='--- a/'.2026 esc_path($from->{'file'});2027}2028}2029$result.= qq!<div class="diff from_file">$line</div>\n!;20302031}else{2032# combined diff (merge commit)2033for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2034if($from->{'href'}[$i]) {2035$line='--- '.2036$cgi->a({-href=>href(action=>"blobdiff",2037 hash_parent=>$diffinfo->{'from_id'}[$i],2038 hash_parent_base=>$parents[$i],2039 file_parent=>$from->{'file'}[$i],2040 hash=>$diffinfo->{'to_id'},2041 hash_base=>$hash,2042 file_name=>$to->{'file'}),2043-class=>"path",2044-title=>"diff". ($i+1)},2045$i+1) .2046'/'.2047$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2048 esc_path($from->{'file'}[$i]));2049}else{2050$line='--- /dev/null';2051}2052$result.= qq!<div class="diff from_file">$line</div>\n!;2053}2054}20552056$line=$to_line;2057#assert($line =~ m/^\+\+\+/) if DEBUG;2058# no extra formatting for "^+++ /dev/null"2059if($line=~m!^\+\+\+ "?b/!) {2060if($to->{'href'}) {2061$line='+++ b/'.2062$cgi->a({-href=>$to->{'href'}, -class=>"path"},2063 esc_path($to->{'file'}));2064}else{2065$line='+++ b/'.2066 esc_path($to->{'file'});2067}2068}2069$result.= qq!<div class="diff to_file">$line</div>\n!;20702071return$result;2072}20732074# create note for patch simplified by combined diff2075sub format_diff_cc_simplified {2076my($diffinfo,@parents) =@_;2077my$result='';20782079$result.="<div class=\"diff header\">".2080"diff --cc ";2081if(!is_deleted($diffinfo)) {2082$result.=$cgi->a({-href => href(action=>"blob",2083 hash_base=>$hash,2084 hash=>$diffinfo->{'to_id'},2085 file_name=>$diffinfo->{'to_file'}),2086-class=>"path"},2087 esc_path($diffinfo->{'to_file'}));2088}else{2089$result.= esc_path($diffinfo->{'to_file'});2090}2091$result.="</div>\n".# class="diff header"2092"<div class=\"diff nodifferences\">".2093"Simple merge".2094"</div>\n";# class="diff nodifferences"20952096return$result;2097}20982099# format patch (diff) line (not to be used for diff headers)2100sub format_diff_line {2101my$line=shift;2102my($from,$to) =@_;2103my$diff_class="";21042105chomp$line;21062107if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2108# combined diff2109my$prefix=substr($line,0,scalar@{$from->{'href'}});2110if($line=~m/^\@{3}/) {2111$diff_class=" chunk_header";2112}elsif($line=~m/^\\/) {2113$diff_class=" incomplete";2114}elsif($prefix=~tr/+/+/) {2115$diff_class=" add";2116}elsif($prefix=~tr/-/-/) {2117$diff_class=" rem";2118}2119}else{2120# assume ordinary diff2121my$char=substr($line,0,1);2122if($chareq'+') {2123$diff_class=" add";2124}elsif($chareq'-') {2125$diff_class=" rem";2126}elsif($chareq'@') {2127$diff_class=" chunk_header";2128}elsif($chareq"\\") {2129$diff_class=" incomplete";2130}2131}2132$line= untabify($line);2133if($from&&$to&&$line=~m/^\@{2} /) {2134my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2135$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;21362137$from_lines=0unlessdefined$from_lines;2138$to_lines=0unlessdefined$to_lines;21392140if($from->{'href'}) {2141$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2142-class=>"list"},$from_text);2143}2144if($to->{'href'}) {2145$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2146-class=>"list"},$to_text);2147}2148$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2149"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2150return"<div class=\"diff$diff_class\">$line</div>\n";2151}elsif($from&&$to&&$line=~m/^\@{3}/) {2152my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2153my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);21542155@from_text=split(' ',$ranges);2156for(my$i=0;$i<@from_text; ++$i) {2157($from_start[$i],$from_nlines[$i]) =2158(split(',',substr($from_text[$i],1)),0);2159}21602161$to_text=pop@from_text;2162$to_start=pop@from_start;2163$to_nlines=pop@from_nlines;21642165$line="<span class=\"chunk_info\">$prefix";2166for(my$i=0;$i<@from_text; ++$i) {2167if($from->{'href'}[$i]) {2168$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2169-class=>"list"},$from_text[$i]);2170}else{2171$line.=$from_text[$i];2172}2173$line.=" ";2174}2175if($to->{'href'}) {2176$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2177-class=>"list"},$to_text);2178}else{2179$line.=$to_text;2180}2181$line.="$prefix</span>".2182"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2183return"<div class=\"diff$diff_class\">$line</div>\n";2184}2185return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2186}21872188# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2189# linked. Pass the hash of the tree/commit to snapshot.2190sub format_snapshot_links {2191my($hash) =@_;2192my$num_fmts=@snapshot_fmts;2193if($num_fmts>1) {2194# A parenthesized list of links bearing format names.2195# e.g. "snapshot (_tar.gz_ _zip_)"2196return"snapshot (".join(' ',map2197$cgi->a({2198-href => href(2199 action=>"snapshot",2200 hash=>$hash,2201 snapshot_format=>$_2202)2203},$known_snapshot_formats{$_}{'display'})2204,@snapshot_fmts) .")";2205}elsif($num_fmts==1) {2206# A single "snapshot" link whose tooltip bears the format name.2207# i.e. "_snapshot_"2208my($fmt) =@snapshot_fmts;2209return2210$cgi->a({2211-href => href(2212 action=>"snapshot",2213 hash=>$hash,2214 snapshot_format=>$fmt2215),2216-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2217},"snapshot");2218}else{# $num_fmts == 02219returnundef;2220}2221}22222223## ......................................................................2224## functions returning values to be passed, perhaps after some2225## transformation, to other functions; e.g. returning arguments to href()22262227# returns hash to be passed to href to generate gitweb URL2228# in -title key it returns description of link2229sub get_feed_info {2230my$format=shift||'Atom';2231my%res= (action =>lc($format));22322233# feed links are possible only for project views2234return unless(defined$project);2235# some views should link to OPML, or to generic project feed,2236# or don't have specific feed yet (so they should use generic)2237return if($action=~/^(?:tags|heads|forks|tag|search)$/x);22382239my$branch;2240# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2241# from tag links; this also makes possible to detect branch links2242if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2243(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2244$branch=$1;2245}2246# find log type for feed description (title)2247my$type='log';2248if(defined$file_name) {2249$type="history of$file_name";2250$type.="/"if($actioneq'tree');2251$type.=" on '$branch'"if(defined$branch);2252}else{2253$type="log of$branch"if(defined$branch);2254}22552256$res{-title} =$type;2257$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2258$res{'file_name'} =$file_name;22592260return%res;2261}22622263## ----------------------------------------------------------------------2264## git utility subroutines, invoking git commands22652266# returns path to the core git executable and the --git-dir parameter as list2267sub git_cmd {2268$number_of_git_cmds++;2269return$GIT,'--git-dir='.$git_dir;2270}22712272# quote the given arguments for passing them to the shell2273# quote_command("command", "arg 1", "arg with ' and ! characters")2274# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2275# Try to avoid using this function wherever possible.2276sub quote_command {2277returnjoin(' ',2278map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2279}22802281# get HEAD ref of given project as hash2282sub git_get_head_hash {2283return git_get_full_hash(shift,'HEAD');2284}22852286sub git_get_full_hash {2287return git_get_hash(@_);2288}22892290sub git_get_short_hash {2291return git_get_hash(@_,'--short=7');2292}22932294sub git_get_hash {2295my($project,$hash,@options) =@_;2296my$o_git_dir=$git_dir;2297my$retval=undef;2298$git_dir="$projectroot/$project";2299if(open my$fd,'-|', git_cmd(),'rev-parse',2300'--verify','-q',@options,$hash) {2301$retval= <$fd>;2302chomp$retvalifdefined$retval;2303close$fd;2304}2305if(defined$o_git_dir) {2306$git_dir=$o_git_dir;2307}2308return$retval;2309}23102311# get type of given object2312sub git_get_type {2313my$hash=shift;23142315open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2316my$type= <$fd>;2317close$fdorreturn;2318chomp$type;2319return$type;2320}23212322# repository configuration2323our$config_file='';2324our%config;23252326# store multiple values for single key as anonymous array reference2327# single values stored directly in the hash, not as [ <value> ]2328sub hash_set_multi {2329my($hash,$key,$value) =@_;23302331if(!exists$hash->{$key}) {2332$hash->{$key} =$value;2333}elsif(!ref$hash->{$key}) {2334$hash->{$key} = [$hash->{$key},$value];2335}else{2336push@{$hash->{$key}},$value;2337}2338}23392340# return hash of git project configuration2341# optionally limited to some section, e.g. 'gitweb'2342sub git_parse_project_config {2343my$section_regexp=shift;2344my%config;23452346local$/="\0";23472348open my$fh,"-|", git_cmd(),"config",'-z','-l',2349orreturn;23502351while(my$keyval= <$fh>) {2352chomp$keyval;2353my($key,$value) =split(/\n/,$keyval,2);23542355 hash_set_multi(\%config,$key,$value)2356if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2357}2358close$fh;23592360return%config;2361}23622363# convert config value to boolean: 'true' or 'false'2364# no value, number > 0, 'true' and 'yes' values are true2365# rest of values are treated as false (never as error)2366sub config_to_bool {2367my$val=shift;23682369return1if!defined$val;# section.key23702371# strip leading and trailing whitespace2372$val=~s/^\s+//;2373$val=~s/\s+$//;23742375return(($val=~/^\d+$/&&$val) ||# section.key = 12376($val=~/^(?:true|yes)$/i));# section.key = true2377}23782379# convert config value to simple decimal number2380# an optional value suffix of 'k', 'm', or 'g' will cause the value2381# to be multiplied by 1024, 1048576, or 10737418242382sub config_to_int {2383my$val=shift;23842385# strip leading and trailing whitespace2386$val=~s/^\s+//;2387$val=~s/\s+$//;23882389if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2390$unit=lc($unit);2391# unknown unit is treated as 12392return$num* ($uniteq'g'?1073741824:2393$uniteq'm'?1048576:2394$uniteq'k'?1024:1);2395}2396return$val;2397}23982399# convert config value to array reference, if needed2400sub config_to_multi {2401my$val=shift;24022403returnref($val) ?$val: (defined($val) ? [$val] : []);2404}24052406sub git_get_project_config {2407my($key,$type) =@_;24082409return unlessdefined$git_dir;24102411# key sanity check2412return unless($key);2413$key=~s/^gitweb\.//;2414return if($key=~m/\W/);24152416# type sanity check2417if(defined$type) {2418$type=~s/^--//;2419$type=undef2420unless($typeeq'bool'||$typeeq'int');2421}24222423# get config2424if(!defined$config_file||2425$config_filene"$git_dir/config") {2426%config= git_parse_project_config('gitweb');2427$config_file="$git_dir/config";2428}24292430# check if config variable (key) exists2431return unlessexists$config{"gitweb.$key"};24322433# ensure given type2434if(!defined$type) {2435return$config{"gitweb.$key"};2436}elsif($typeeq'bool') {2437# backward compatibility: 'git config --bool' returns true/false2438return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2439}elsif($typeeq'int') {2440return config_to_int($config{"gitweb.$key"});2441}2442return$config{"gitweb.$key"};2443}24442445# get hash of given path at given ref2446sub git_get_hash_by_path {2447my$base=shift;2448my$path=shift||returnundef;2449my$type=shift;24502451$path=~ s,/+$,,;24522453open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2454or die_error(500,"Open git-ls-tree failed");2455my$line= <$fd>;2456close$fdorreturnundef;24572458if(!defined$line) {2459# there is no tree or hash given by $path at $base2460returnundef;2461}24622463#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2464$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2465if(defined$type&&$typene$2) {2466# type doesn't match2467returnundef;2468}2469return$3;2470}24712472# get path of entry with given hash at given tree-ish (ref)2473# used to get 'from' filename for combined diff (merge commit) for renames2474sub git_get_path_by_hash {2475my$base=shift||return;2476my$hash=shift||return;24772478local$/="\0";24792480open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2481orreturnundef;2482while(my$line= <$fd>) {2483chomp$line;24842485#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2486#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2487if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2488close$fd;2489return$1;2490}2491}2492close$fd;2493returnundef;2494}24952496## ......................................................................2497## git utility functions, directly accessing git repository24982499sub git_get_project_description {2500my$path=shift;25012502$git_dir="$projectroot/$path";2503open my$fd,'<',"$git_dir/description"2504orreturn git_get_project_config('description');2505my$descr= <$fd>;2506close$fd;2507if(defined$descr) {2508chomp$descr;2509}2510return$descr;2511}25122513sub git_get_project_ctags {2514my$path=shift;2515my$ctags= {};25162517$git_dir="$projectroot/$path";2518opendir my$dh,"$git_dir/ctags"2519orreturn$ctags;2520foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2521open my$ct,'<',$_ornext;2522my$val= <$ct>;2523chomp$val;2524close$ct;2525my$ctag=$_;$ctag=~ s#.*/##;2526$ctags->{$ctag} =$val;2527}2528closedir$dh;2529$ctags;2530}25312532sub git_populate_project_tagcloud {2533my$ctags=shift;25342535# First, merge different-cased tags; tags vote on casing2536my%ctags_lc;2537foreach(keys%$ctags) {2538$ctags_lc{lc$_}->{count} +=$ctags->{$_};2539if(not$ctags_lc{lc$_}->{topcount}2540or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2541$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2542$ctags_lc{lc$_}->{topname} =$_;2543}2544}25452546my$cloud;2547if(eval{require HTML::TagCloud;1; }) {2548$cloud= HTML::TagCloud->new;2549foreach(sort keys%ctags_lc) {2550# Pad the title with spaces so that the cloud looks2551# less crammed.2552my$title=$ctags_lc{$_}->{topname};2553$title=~s/ / /g;2554$title=~s/^/ /g;2555$title=~s/$/ /g;2556$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2557}2558}else{2559$cloud= \%ctags_lc;2560}2561$cloud;2562}25632564sub git_show_project_tagcloud {2565my($cloud,$count) =@_;2566print STDERR ref($cloud)."..\n";2567if(ref$cloudeq'HTML::TagCloud') {2568return$cloud->html_and_css($count);2569}else{2570my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2571return'<p align="center">'.join(', ',map{2572"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2573}splice(@tags,0,$count)) .'</p>';2574}2575}25762577sub git_get_project_url_list {2578my$path=shift;25792580$git_dir="$projectroot/$path";2581open my$fd,'<',"$git_dir/cloneurl"2582orreturnwantarray?2583@{ config_to_multi(git_get_project_config('url')) } :2584 config_to_multi(git_get_project_config('url'));2585my@git_project_url_list=map{chomp;$_} <$fd>;2586close$fd;25872588returnwantarray?@git_project_url_list: \@git_project_url_list;2589}25902591sub git_get_projects_list {2592my($filter) =@_;2593my@list;25942595$filter||='';2596$filter=~s/\.git$//;25972598my$check_forks= gitweb_check_feature('forks');25992600if(-d $projects_list) {2601# search in directory2602my$dir=$projects_list. ($filter?"/$filter":'');2603# remove the trailing "/"2604$dir=~s!/+$!!;2605my$pfxlen=length("$dir");2606my$pfxdepth= ($dir=~tr!/!!);26072608 File::Find::find({2609 follow_fast =>1,# follow symbolic links2610 follow_skip =>2,# ignore duplicates2611 dangling_symlinks =>0,# ignore dangling symlinks, silently2612 wanted =>sub{2613# global variables2614our$project_maxdepth;2615our$projectroot;2616# skip project-list toplevel, if we get it.2617return if(m!^[/.]$!);2618# only directories can be git repositories2619return unless(-d $_);2620# don't traverse too deep (Find is super slow on os x)2621if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2622$File::Find::prune =1;2623return;2624}26252626my$subdir=substr($File::Find::name,$pfxlen+1);2627# we check related file in $projectroot2628my$path= ($filter?"$filter/":'') .$subdir;2629if(check_export_ok("$projectroot/$path")) {2630push@list, { path =>$path};2631$File::Find::prune =1;2632}2633},2634},"$dir");26352636}elsif(-f $projects_list) {2637# read from file(url-encoded):2638# 'git%2Fgit.git Linus+Torvalds'2639# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2640# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2641my%paths;2642open my$fd,'<',$projects_listorreturn;2643 PROJECT:2644while(my$line= <$fd>) {2645chomp$line;2646my($path,$owner) =split' ',$line;2647$path= unescape($path);2648$owner= unescape($owner);2649if(!defined$path) {2650next;2651}2652if($filterne'') {2653# looking for forks;2654my$pfx=substr($path,0,length($filter));2655if($pfxne$filter) {2656next PROJECT;2657}2658my$sfx=substr($path,length($filter));2659if($sfx!~/^\/.*\.git$/) {2660next PROJECT;2661}2662}elsif($check_forks) {2663 PATH:2664foreachmy$filter(keys%paths) {2665# looking for forks;2666my$pfx=substr($path,0,length($filter));2667if($pfxne$filter) {2668next PATH;2669}2670my$sfx=substr($path,length($filter));2671if($sfx!~/^\/.*\.git$/) {2672next PATH;2673}2674# is a fork, don't include it in2675# the list2676next PROJECT;2677}2678}2679if(check_export_ok("$projectroot/$path")) {2680my$pr= {2681 path =>$path,2682 owner => to_utf8($owner),2683};2684push@list,$pr;2685(my$forks_path=$path) =~s/\.git$//;2686$paths{$forks_path}++;2687}2688}2689close$fd;2690}2691return@list;2692}26932694our$gitweb_project_owner=undef;2695sub git_get_project_list_from_file {26962697return if(defined$gitweb_project_owner);26982699$gitweb_project_owner= {};2700# read from file (url-encoded):2701# 'git%2Fgit.git Linus+Torvalds'2702# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2703# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2704if(-f $projects_list) {2705open(my$fd,'<',$projects_list);2706while(my$line= <$fd>) {2707chomp$line;2708my($pr,$ow) =split' ',$line;2709$pr= unescape($pr);2710$ow= unescape($ow);2711$gitweb_project_owner->{$pr} = to_utf8($ow);2712}2713close$fd;2714}2715}27162717sub git_get_project_owner {2718my$project=shift;2719my$owner;27202721returnundefunless$project;2722$git_dir="$projectroot/$project";27232724if(!defined$gitweb_project_owner) {2725 git_get_project_list_from_file();2726}27272728if(exists$gitweb_project_owner->{$project}) {2729$owner=$gitweb_project_owner->{$project};2730}2731if(!defined$owner){2732$owner= git_get_project_config('owner');2733}2734if(!defined$owner) {2735$owner= get_file_owner("$git_dir");2736}27372738return$owner;2739}27402741sub git_get_last_activity {2742my($path) =@_;2743my$fd;27442745$git_dir="$projectroot/$path";2746open($fd,"-|", git_cmd(),'for-each-ref',2747'--format=%(committer)',2748'--sort=-committerdate',2749'--count=1',2750'refs/heads')orreturn;2751my$most_recent= <$fd>;2752close$fdorreturn;2753if(defined$most_recent&&2754$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2755my$timestamp=$1;2756my$age=time-$timestamp;2757return($age, age_string($age));2758}2759return(undef,undef);2760}27612762sub git_get_references {2763my$type=shift||"";2764my%refs;2765# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112766# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2767open my$fd,"-|", git_cmd(),"show-ref","--dereference",2768($type? ("--","refs/$type") : ())# use -- <pattern> if $type2769orreturn;27702771while(my$line= <$fd>) {2772chomp$line;2773if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2774if(defined$refs{$1}) {2775push@{$refs{$1}},$2;2776}else{2777$refs{$1} = [$2];2778}2779}2780}2781close$fdorreturn;2782return \%refs;2783}27842785sub git_get_rev_name_tags {2786my$hash=shift||returnundef;27872788open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2789orreturn;2790my$name_rev= <$fd>;2791close$fd;27922793if($name_rev=~ m|^$hash tags/(.*)$|) {2794return$1;2795}else{2796# catches also '$hash undefined' output2797returnundef;2798}2799}28002801## ----------------------------------------------------------------------2802## parse to hash functions28032804sub parse_date {2805my$epoch=shift;2806my$tz=shift||"-0000";28072808my%date;2809my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2810my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2811my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2812$date{'hour'} =$hour;2813$date{'minute'} =$min;2814$date{'mday'} =$mday;2815$date{'day'} =$days[$wday];2816$date{'month'} =$months[$mon];2817$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2818$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2819$date{'mday-time'} =sprintf"%d%s%02d:%02d",2820$mday,$months[$mon],$hour,$min;2821$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",28221900+$year,1+$mon,$mday,$hour,$min,$sec;28232824$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2825my$local=$epoch+ ((int$1+ ($2/60)) *3600);2826($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2827$date{'hour_local'} =$hour;2828$date{'minute_local'} =$min;2829$date{'tz_local'} =$tz;2830$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",28311900+$year,$mon+1,$mday,2832$hour,$min,$sec,$tz);2833return%date;2834}28352836sub parse_tag {2837my$tag_id=shift;2838my%tag;2839my@comment;28402841open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2842$tag{'id'} =$tag_id;2843while(my$line= <$fd>) {2844chomp$line;2845if($line=~m/^object ([0-9a-fA-F]{40})$/) {2846$tag{'object'} =$1;2847}elsif($line=~m/^type (.+)$/) {2848$tag{'type'} =$1;2849}elsif($line=~m/^tag (.+)$/) {2850$tag{'name'} =$1;2851}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2852$tag{'author'} =$1;2853$tag{'author_epoch'} =$2;2854$tag{'author_tz'} =$3;2855if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2856$tag{'author_name'} =$1;2857$tag{'author_email'} =$2;2858}else{2859$tag{'author_name'} =$tag{'author'};2860}2861}elsif($line=~m/--BEGIN/) {2862push@comment,$line;2863last;2864}elsif($lineeq"") {2865last;2866}2867}2868push@comment, <$fd>;2869$tag{'comment'} = \@comment;2870close$fdorreturn;2871if(!defined$tag{'name'}) {2872return2873};2874return%tag2875}28762877sub parse_commit_text {2878my($commit_text,$withparents) =@_;2879my@commit_lines=split'\n',$commit_text;2880my%co;28812882pop@commit_lines;# Remove '\0'28832884if(!@commit_lines) {2885return;2886}28872888my$header=shift@commit_lines;2889if($header!~m/^[0-9a-fA-F]{40}/) {2890return;2891}2892($co{'id'},my@parents) =split' ',$header;2893while(my$line=shift@commit_lines) {2894last if$lineeq"\n";2895if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2896$co{'tree'} =$1;2897}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2898push@parents,$1;2899}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2900$co{'author'} = to_utf8($1);2901$co{'author_epoch'} =$2;2902$co{'author_tz'} =$3;2903if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2904$co{'author_name'} =$1;2905$co{'author_email'} =$2;2906}else{2907$co{'author_name'} =$co{'author'};2908}2909}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2910$co{'committer'} = to_utf8($1);2911$co{'committer_epoch'} =$2;2912$co{'committer_tz'} =$3;2913if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2914$co{'committer_name'} =$1;2915$co{'committer_email'} =$2;2916}else{2917$co{'committer_name'} =$co{'committer'};2918}2919}2920}2921if(!defined$co{'tree'}) {2922return;2923};2924$co{'parents'} = \@parents;2925$co{'parent'} =$parents[0];29262927foreachmy$title(@commit_lines) {2928$title=~s/^ //;2929if($titlene"") {2930$co{'title'} = chop_str($title,80,5);2931# remove leading stuff of merges to make the interesting part visible2932if(length($title) >50) {2933$title=~s/^Automatic //;2934$title=~s/^merge (of|with) /Merge ... /i;2935if(length($title) >50) {2936$title=~s/(http|rsync):\/\///;2937}2938if(length($title) >50) {2939$title=~s/(master|www|rsync)\.//;2940}2941if(length($title) >50) {2942$title=~s/kernel.org:?//;2943}2944if(length($title) >50) {2945$title=~s/\/pub\/scm//;2946}2947}2948$co{'title_short'} = chop_str($title,50,5);2949last;2950}2951}2952if(!defined$co{'title'} ||$co{'title'}eq"") {2953$co{'title'} =$co{'title_short'} ='(no commit message)';2954}2955# remove added spaces2956foreachmy$line(@commit_lines) {2957$line=~s/^ //;2958}2959$co{'comment'} = \@commit_lines;29602961my$age=time-$co{'committer_epoch'};2962$co{'age'} =$age;2963$co{'age_string'} = age_string($age);2964my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2965if($age>60*60*24*7*2) {2966$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2967$co{'age_string_age'} =$co{'age_string'};2968}else{2969$co{'age_string_date'} =$co{'age_string'};2970$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2971}2972return%co;2973}29742975sub parse_commit {2976my($commit_id) =@_;2977my%co;29782979local$/="\0";29802981open my$fd,"-|", git_cmd(),"rev-list",2982"--parents",2983"--header",2984"--max-count=1",2985$commit_id,2986"--",2987or die_error(500,"Open git-rev-list failed");2988%co= parse_commit_text(<$fd>,1);2989close$fd;29902991return%co;2992}29932994sub parse_commits {2995my($commit_id,$maxcount,$skip,$filename,@args) =@_;2996my@cos;29972998$maxcount||=1;2999$skip||=0;30003001local$/="\0";30023003open my$fd,"-|", git_cmd(),"rev-list",3004"--header",3005@args,3006("--max-count=".$maxcount),3007("--skip=".$skip),3008@extra_options,3009$commit_id,3010"--",3011($filename? ($filename) : ())3012or die_error(500,"Open git-rev-list failed");3013while(my$line= <$fd>) {3014my%co= parse_commit_text($line);3015push@cos, \%co;3016}3017close$fd;30183019returnwantarray?@cos: \@cos;3020}30213022# parse line of git-diff-tree "raw" output3023sub parse_difftree_raw_line {3024my$line=shift;3025my%res;30263027# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'3028# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'3029if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {3030$res{'from_mode'} =$1;3031$res{'to_mode'} =$2;3032$res{'from_id'} =$3;3033$res{'to_id'} =$4;3034$res{'status'} =$5;3035$res{'similarity'} =$6;3036if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3037($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3038}else{3039$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3040}3041}3042# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3043# combined diff (for merge commit)3044elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3045$res{'nparents'} =length($1);3046$res{'from_mode'} = [split(' ',$2) ];3047$res{'to_mode'} =pop@{$res{'from_mode'}};3048$res{'from_id'} = [split(' ',$3) ];3049$res{'to_id'} =pop@{$res{'from_id'}};3050$res{'status'} = [split('',$4) ];3051$res{'to_file'} = unquote($5);3052}3053# 'c512b523472485aef4fff9e57b229d9d243c967f'3054elsif($line=~m/^([0-9a-fA-F]{40})$/) {3055$res{'commit'} =$1;3056}30573058returnwantarray?%res: \%res;3059}30603061# wrapper: return parsed line of git-diff-tree "raw" output3062# (the argument might be raw line, or parsed info)3063sub parsed_difftree_line {3064my$line_or_ref=shift;30653066if(ref($line_or_ref)eq"HASH") {3067# pre-parsed (or generated by hand)3068return$line_or_ref;3069}else{3070return parse_difftree_raw_line($line_or_ref);3071}3072}30733074# parse line of git-ls-tree output3075sub parse_ls_tree_line {3076my$line=shift;3077my%opts=@_;3078my%res;30793080if($opts{'-l'}) {3081#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3082$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;30833084$res{'mode'} =$1;3085$res{'type'} =$2;3086$res{'hash'} =$3;3087$res{'size'} =$4;3088if($opts{'-z'}) {3089$res{'name'} =$5;3090}else{3091$res{'name'} = unquote($5);3092}3093}else{3094#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3095$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;30963097$res{'mode'} =$1;3098$res{'type'} =$2;3099$res{'hash'} =$3;3100if($opts{'-z'}) {3101$res{'name'} =$4;3102}else{3103$res{'name'} = unquote($4);3104}3105}31063107returnwantarray?%res: \%res;3108}31093110# generates _two_ hashes, references to which are passed as 2 and 3 argument3111sub parse_from_to_diffinfo {3112my($diffinfo,$from,$to,@parents) =@_;31133114if($diffinfo->{'nparents'}) {3115# combined diff3116$from->{'file'} = [];3117$from->{'href'} = [];3118 fill_from_file_info($diffinfo,@parents)3119unlessexists$diffinfo->{'from_file'};3120for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3121$from->{'file'}[$i] =3122defined$diffinfo->{'from_file'}[$i] ?3123$diffinfo->{'from_file'}[$i] :3124$diffinfo->{'to_file'};3125if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3126$from->{'href'}[$i] = href(action=>"blob",3127 hash_base=>$parents[$i],3128 hash=>$diffinfo->{'from_id'}[$i],3129 file_name=>$from->{'file'}[$i]);3130}else{3131$from->{'href'}[$i] =undef;3132}3133}3134}else{3135# ordinary (not combined) diff3136$from->{'file'} =$diffinfo->{'from_file'};3137if($diffinfo->{'status'}ne"A") {# not new (added) file3138$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3139 hash=>$diffinfo->{'from_id'},3140 file_name=>$from->{'file'});3141}else{3142delete$from->{'href'};3143}3144}31453146$to->{'file'} =$diffinfo->{'to_file'};3147if(!is_deleted($diffinfo)) {# file exists in result3148$to->{'href'} = href(action=>"blob", hash_base=>$hash,3149 hash=>$diffinfo->{'to_id'},3150 file_name=>$to->{'file'});3151}else{3152delete$to->{'href'};3153}3154}31553156## ......................................................................3157## parse to array of hashes functions31583159sub git_get_heads_list {3160my$limit=shift;3161my@headslist;31623163open my$fd,'-|', git_cmd(),'for-each-ref',3164($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3165'--format=%(objectname) %(refname) %(subject)%00%(committer)',3166'refs/heads'3167orreturn;3168while(my$line= <$fd>) {3169my%ref_item;31703171chomp$line;3172my($refinfo,$committerinfo) =split(/\0/,$line);3173my($hash,$name,$title) =split(' ',$refinfo,3);3174my($committer,$epoch,$tz) =3175($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3176$ref_item{'fullname'} =$name;3177$name=~s!^refs/heads/!!;31783179$ref_item{'name'} =$name;3180$ref_item{'id'} =$hash;3181$ref_item{'title'} =$title||'(no commit message)';3182$ref_item{'epoch'} =$epoch;3183if($epoch) {3184$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3185}else{3186$ref_item{'age'} ="unknown";3187}31883189push@headslist, \%ref_item;3190}3191close$fd;31923193returnwantarray?@headslist: \@headslist;3194}31953196sub git_get_tags_list {3197my$limit=shift;3198my@tagslist;31993200open my$fd,'-|', git_cmd(),'for-each-ref',3201($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3202'--format=%(objectname) %(objecttype) %(refname) '.3203'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3204'refs/tags'3205orreturn;3206while(my$line= <$fd>) {3207my%ref_item;32083209chomp$line;3210my($refinfo,$creatorinfo) =split(/\0/,$line);3211my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3212my($creator,$epoch,$tz) =3213($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3214$ref_item{'fullname'} =$name;3215$name=~s!^refs/tags/!!;32163217$ref_item{'type'} =$type;3218$ref_item{'id'} =$id;3219$ref_item{'name'} =$name;3220if($typeeq"tag") {3221$ref_item{'subject'} =$title;3222$ref_item{'reftype'} =$reftype;3223$ref_item{'refid'} =$refid;3224}else{3225$ref_item{'reftype'} =$type;3226$ref_item{'refid'} =$id;3227}32283229if($typeeq"tag"||$typeeq"commit") {3230$ref_item{'epoch'} =$epoch;3231if($epoch) {3232$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3233}else{3234$ref_item{'age'} ="unknown";3235}3236}32373238push@tagslist, \%ref_item;3239}3240close$fd;32413242returnwantarray?@tagslist: \@tagslist;3243}32443245## ----------------------------------------------------------------------3246## filesystem-related functions32473248sub get_file_owner {3249my$path=shift;32503251my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3252my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3253if(!defined$gcos) {3254returnundef;3255}3256my$owner=$gcos;3257$owner=~s/[,;].*$//;3258return to_utf8($owner);3259}32603261# assume that file exists3262sub insert_file {3263my$filename=shift;32643265open my$fd,'<',$filename;3266print map{ to_utf8($_) } <$fd>;3267close$fd;3268}32693270## ......................................................................3271## mimetype related functions32723273sub mimetype_guess_file {3274my$filename=shift;3275my$mimemap=shift;3276-r $mimemaporreturnundef;32773278my%mimemap;3279open(my$mh,'<',$mimemap)orreturnundef;3280while(<$mh>) {3281next ifm/^#/;# skip comments3282my($mimetype,$exts) =split(/\t+/);3283if(defined$exts) {3284my@exts=split(/\s+/,$exts);3285foreachmy$ext(@exts) {3286$mimemap{$ext} =$mimetype;3287}3288}3289}3290close($mh);32913292$filename=~/\.([^.]*)$/;3293return$mimemap{$1};3294}32953296sub mimetype_guess {3297my$filename=shift;3298my$mime;3299$filename=~/\./orreturnundef;33003301if($mimetypes_file) {3302my$file=$mimetypes_file;3303if($file!~m!^/!) {# if it is relative path3304# it is relative to project3305$file="$projectroot/$project/$file";3306}3307$mime= mimetype_guess_file($filename,$file);3308}3309$mime||= mimetype_guess_file($filename,'/etc/mime.types');3310return$mime;3311}33123313sub blob_mimetype {3314my$fd=shift;3315my$filename=shift;33163317if($filename) {3318my$mime= mimetype_guess($filename);3319$mimeandreturn$mime;3320}33213322# just in case3323return$default_blob_plain_mimetypeunless$fd;33243325if(-T $fd) {3326return'text/plain';3327}elsif(!$filename) {3328return'application/octet-stream';3329}elsif($filename=~m/\.png$/i) {3330return'image/png';3331}elsif($filename=~m/\.gif$/i) {3332return'image/gif';3333}elsif($filename=~m/\.jpe?g$/i) {3334return'image/jpeg';3335}else{3336return'application/octet-stream';3337}3338}33393340sub blob_contenttype {3341my($fd,$file_name,$type) =@_;33423343$type||= blob_mimetype($fd,$file_name);3344if($typeeq'text/plain'&&defined$default_text_plain_charset) {3345$type.="; charset=$default_text_plain_charset";3346}33473348return$type;3349}33503351# guess file syntax for syntax highlighting; return undef if no highlighting3352# the name of syntax can (in the future) depend on syntax highlighter used3353sub guess_file_syntax {3354my($highlight,$mimetype,$file_name) =@_;3355returnundefunless($highlight&&defined$file_name);3356my$basename= basename($file_name,'.in');3357return$highlight_basename{$basename}3358ifexists$highlight_basename{$basename};33593360$basename=~/\.([^.]*)$/;3361my$ext=$1orreturnundef;3362return$highlight_ext{$ext}3363ifexists$highlight_ext{$ext};33643365returnundef;3366}33673368# run highlighter and return FD of its output,3369# or return original FD if no highlighting3370sub run_highlighter {3371my($fd,$highlight,$syntax) =@_;3372return$fdunless($highlight&&defined$syntax);33733374close$fd3375or die_error(404,"Reading blob failed");3376open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3377 quote_command($highlight_bin).3378" --xhtml --fragment --syntax$syntax|"3379or die_error(500,"Couldn't open file or run syntax highlighter");3380return$fd;3381}33823383## ======================================================================3384## functions printing HTML: header, footer, error page33853386sub get_page_title {3387my$title= to_utf8($site_name);33883389return$titleunless(defined$project);3390$title.=" - ". to_utf8($project);33913392return$titleunless(defined$action);3393$title.="/$action";# $action is US-ASCII (7bit ASCII)33943395return$titleunless(defined$file_name);3396$title.=" - ". esc_path($file_name);3397if($actioneq"tree"&&$file_name!~ m|/$|) {3398$title.="/";3399}34003401return$title;3402}34033404sub git_header_html {3405my$status=shift||"200 OK";3406my$expires=shift;3407my%opts=@_;34083409my$title= get_page_title();3410my$content_type;3411# require explicit support from the UA if we are to send the page as3412# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3413# we have to do this because MSIE sometimes globs '*/*', pretending to3414# support xhtml+xml but choking when it gets what it asked for.3415if(defined$cgi->http('HTTP_ACCEPT') &&3416$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3417$cgi->Accept('application/xhtml+xml') !=0) {3418$content_type='application/xhtml+xml';3419}else{3420$content_type='text/html';3421}3422print$cgi->header(-type=>$content_type, -charset =>'utf-8',3423-status=>$status, -expires =>$expires)3424unless($opts{'-no_http_header'});3425my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3426print<<EOF;3427<?xml version="1.0" encoding="utf-8"?>3428<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3429<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3430<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3431<!-- git core binaries version$git_version-->3432<head>3433<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3434<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3435<meta name="robots" content="index, nofollow"/>3436<title>$title</title>3437EOF3438# the stylesheet, favicon etc urls won't work correctly with path_info3439# unless we set the appropriate base URL3440if($ENV{'PATH_INFO'}) {3441print"<base href=\"".esc_url($base_url)."\"/>\n";3442}3443# print out each stylesheet that exist, providing backwards capability3444# for those people who defined $stylesheet in a config file3445if(defined$stylesheet) {3446print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3447}else{3448foreachmy$stylesheet(@stylesheets) {3449next unless$stylesheet;3450print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3451}3452}3453if(defined$project) {3454my%href_params= get_feed_info();3455if(!exists$href_params{'-title'}) {3456$href_params{'-title'} ='log';3457}34583459foreachmy$formatqw(RSS Atom){3460my$type=lc($format);3461my%link_attr= (3462'-rel'=>'alternate',3463'-title'=>"$project-$href_params{'-title'} -$formatfeed",3464'-type'=>"application/$type+xml"3465);34663467$href_params{'action'} =$type;3468$link_attr{'-href'} = href(%href_params);3469print"<link ".3470"rel=\"$link_attr{'-rel'}\"".3471"title=\"$link_attr{'-title'}\"".3472"href=\"$link_attr{'-href'}\"".3473"type=\"$link_attr{'-type'}\"".3474"/>\n";34753476$href_params{'extra_options'} ='--no-merges';3477$link_attr{'-href'} = href(%href_params);3478$link_attr{'-title'} .=' (no merges)';3479print"<link ".3480"rel=\"$link_attr{'-rel'}\"".3481"title=\"$link_attr{'-title'}\"".3482"href=\"$link_attr{'-href'}\"".3483"type=\"$link_attr{'-type'}\"".3484"/>\n";3485}34863487}else{3488printf('<link rel="alternate" title="%sprojects list" '.3489'href="%s" type="text/plain; charset=utf-8" />'."\n",3490$site_name, href(project=>undef, action=>"project_index"));3491printf('<link rel="alternate" title="%sprojects feeds" '.3492'href="%s" type="text/x-opml" />'."\n",3493$site_name, href(project=>undef, action=>"opml"));3494}3495if(defined$favicon) {3496printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3497}34983499print"</head>\n".3500"<body>\n";35013502if(defined$site_header&& -f $site_header) {3503 insert_file($site_header);3504}35053506print"<div class=\"page_header\">\n".3507$cgi->a({-href => esc_url($logo_url),3508-title =>$logo_label},3509qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3510print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3511if(defined$project) {3512print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3513if(defined$action) {3514print" /$action";3515}3516print"\n";3517}3518print"</div>\n";35193520my$have_search= gitweb_check_feature('search');3521if(defined$project&&$have_search) {3522if(!defined$searchtext) {3523$searchtext="";3524}3525my$search_hash;3526if(defined$hash_base) {3527$search_hash=$hash_base;3528}elsif(defined$hash) {3529$search_hash=$hash;3530}else{3531$search_hash="HEAD";3532}3533my$action=$my_uri;3534my$use_pathinfo= gitweb_check_feature('pathinfo');3535if($use_pathinfo) {3536$action.="/".esc_url($project);3537}3538print$cgi->startform(-method=>"get", -action =>$action) .3539"<div class=\"search\">\n".3540(!$use_pathinfo&&3541$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3542$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3543$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3544$cgi->popup_menu(-name =>'st', -default=>'commit',3545-values=> ['commit','grep','author','committer','pickaxe']) .3546$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3547" search:\n",3548$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3549"<span title=\"Extended regular expression\">".3550$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3551-checked =>$search_use_regexp) .3552"</span>".3553"</div>".3554$cgi->end_form() ."\n";3555}3556}35573558sub git_footer_html {3559my$feed_class='rss_logo';35603561print"<div class=\"page_footer\">\n";3562if(defined$project) {3563my$descr= git_get_project_description($project);3564if(defined$descr) {3565print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3566}35673568my%href_params= get_feed_info();3569if(!%href_params) {3570$feed_class.=' generic';3571}3572$href_params{'-title'} ||='log';35733574foreachmy$formatqw(RSS Atom){3575$href_params{'action'} =lc($format);3576print$cgi->a({-href => href(%href_params),3577-title =>"$href_params{'-title'}$formatfeed",3578-class=>$feed_class},$format)."\n";3579}35803581}else{3582print$cgi->a({-href => href(project=>undef, action=>"opml"),3583-class=>$feed_class},"OPML") ." ";3584print$cgi->a({-href => href(project=>undef, action=>"project_index"),3585-class=>$feed_class},"TXT") ."\n";3586}3587print"</div>\n";# class="page_footer"35883589if(defined$t0&& gitweb_check_feature('timed')) {3590print"<div id=\"generating_info\">\n";3591print'This page took '.3592'<span id="generating_time" class="time_span">'.3593 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3594' seconds </span>'.3595' and '.3596'<span id="generating_cmd">'.3597$number_of_git_cmds.3598'</span> git commands '.3599" to generate.\n";3600print"</div>\n";# class="page_footer"3601}36023603if(defined$site_footer&& -f $site_footer) {3604 insert_file($site_footer);3605}36063607print qq!<script type="text/javascript" src="$javascript"></script>\n!;3608if(defined$action&&3609$actioneq'blame_incremental') {3610print qq!<script type="text/javascript">\n!.3611 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3612 qq!"!. href() .qq!");\n!.3613 qq!</script>\n!;3614}elsif(gitweb_check_feature('javascript-actions')) {3615print qq!<script type="text/javascript">\n!.3616 qq!window.onload = fixLinks;\n!.3617 qq!</script>\n!;3618}36193620print"</body>\n".3621"</html>";3622}36233624# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3625# Example: die_error(404, 'Hash not found')3626# By convention, use the following status codes (as defined in RFC 2616):3627# 400: Invalid or missing CGI parameters, or3628# requested object exists but has wrong type.3629# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3630# this server or project.3631# 404: Requested object/revision/project doesn't exist.3632# 500: The server isn't configured properly, or3633# an internal error occurred (e.g. failed assertions caused by bugs), or3634# an unknown error occurred (e.g. the git binary died unexpectedly).3635# 503: The server is currently unavailable (because it is overloaded,3636# or down for maintenance). Generally, this is a temporary state.3637sub die_error {3638my$status=shift||500;3639my$error= esc_html(shift) ||"Internal Server Error";3640my$extra=shift;3641my%opts=@_;36423643my%http_responses= (3644400=>'400 Bad Request',3645403=>'403 Forbidden',3646404=>'404 Not Found',3647500=>'500 Internal Server Error',3648503=>'503 Service Unavailable',3649);3650 git_header_html($http_responses{$status},undef,%opts);3651print<<EOF;3652<div class="page_body">3653<br /><br />3654$status-$error3655<br />3656EOF3657if(defined$extra) {3658print"<hr />\n".3659"$extra\n";3660}3661print"</div>\n";36623663 git_footer_html();3664goto DONE_GITWEB3665unless($opts{'-error_handler'});3666}36673668## ----------------------------------------------------------------------3669## functions printing or outputting HTML: navigation36703671sub git_print_page_nav {3672my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3673$extra=''if!defined$extra;# pager or formats36743675my@navs=qw(summary shortlog log commit commitdiff tree);3676if($suppress) {3677@navs=grep{$_ne$suppress}@navs;3678}36793680my%arg=map{$_=> {action=>$_} }@navs;3681if(defined$head) {3682for(qw(commit commitdiff)) {3683$arg{$_}{'hash'} =$head;3684}3685if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3686for(qw(shortlog log)) {3687$arg{$_}{'hash'} =$head;3688}3689}3690}36913692$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3693$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;36943695my@actions= gitweb_get_feature('actions');3696my%repl= (3697'%'=>'%',3698'n'=>$project,# project name3699'f'=>$git_dir,# project path within filesystem3700'h'=>$treehead||'',# current hash ('h' parameter)3701'b'=>$treebase||'',# hash base ('hb' parameter)3702);3703while(@actions) {3704my($label,$link,$pos) =splice(@actions,0,3);3705# insert3706@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3707# munch munch3708$link=~s/%([%nfhb])/$repl{$1}/g;3709$arg{$label}{'_href'} =$link;3710}37113712print"<div class=\"page_nav\">\n".3713(join" | ",3714map{$_eq$current?3715$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3716}@navs);3717print"<br/>\n$extra<br/>\n".3718"</div>\n";3719}37203721sub format_paging_nav {3722my($action,$page,$has_next_link) =@_;3723my$paging_nav;372437253726if($page>0) {3727$paging_nav.=3728$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3729" ⋅ ".3730$cgi->a({-href => href(-replay=>1, page=>$page-1),3731-accesskey =>"p", -title =>"Alt-p"},"prev");3732}else{3733$paging_nav.="first ⋅ prev";3734}37353736if($has_next_link) {3737$paging_nav.=" ⋅ ".3738$cgi->a({-href => href(-replay=>1, page=>$page+1),3739-accesskey =>"n", -title =>"Alt-n"},"next");3740}else{3741$paging_nav.=" ⋅ next";3742}37433744return$paging_nav;3745}37463747## ......................................................................3748## functions printing or outputting HTML: div37493750sub git_print_header_div {3751my($action,$title,$hash,$hash_base) =@_;3752my%args= ();37533754$args{'action'} =$action;3755$args{'hash'} =$hashif$hash;3756$args{'hash_base'} =$hash_baseif$hash_base;37573758print"<div class=\"header\">\n".3759$cgi->a({-href => href(%args), -class=>"title"},3760$title?$title:$action) .3761"\n</div>\n";3762}37633764sub print_local_time {3765print format_local_time(@_);3766}37673768sub format_local_time {3769my$localtime='';3770my%date=@_;3771if($date{'hour_local'} <6) {3772$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3773$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3774}else{3775$localtime.=sprintf(" (%02d:%02d%s)",3776$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3777}37783779return$localtime;3780}37813782# Outputs the author name and date in long form3783sub git_print_authorship {3784my$co=shift;3785my%opts=@_;3786my$tag=$opts{-tag} ||'div';3787my$author=$co->{'author_name'};37883789my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3790print"<$tagclass=\"author_date\">".3791 format_search_author($author,"author", esc_html($author)) .3792" [$ad{'rfc2822'}";3793 print_local_time(%ad)if($opts{-localtime});3794print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3795."</$tag>\n";3796}37973798# Outputs table rows containing the full author or committer information,3799# in the format expected for 'commit' view (& similar).3800# Parameters are a commit hash reference, followed by the list of people3801# to output information for. If the list is empty it defaults to both3802# author and committer.3803sub git_print_authorship_rows {3804my$co=shift;3805# too bad we can't use @people = @_ || ('author', 'committer')3806my@people=@_;3807@people= ('author','committer')unless@people;3808foreachmy$who(@people) {3809my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3810print"<tr><td>$who</td><td>".3811 format_search_author($co->{"${who}_name"},$who,3812 esc_html($co->{"${who}_name"})) ." ".3813 format_search_author($co->{"${who}_email"},$who,3814 esc_html("<".$co->{"${who}_email"} .">")) .3815"</td><td rowspan=\"2\">".3816 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3817"</td></tr>\n".3818"<tr>".3819"<td></td><td>$wd{'rfc2822'}";3820 print_local_time(%wd);3821print"</td>".3822"</tr>\n";3823}3824}38253826sub git_print_page_path {3827my$name=shift;3828my$type=shift;3829my$hb=shift;383038313832print"<div class=\"page_path\">";3833print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3834-title =>'tree root'}, to_utf8("[$project]"));3835print" / ";3836if(defined$name) {3837my@dirname=split'/',$name;3838my$basename=pop@dirname;3839my$fullname='';38403841foreachmy$dir(@dirname) {3842$fullname.= ($fullname?'/':'') .$dir;3843print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3844 hash_base=>$hb),3845-title =>$fullname}, esc_path($dir));3846print" / ";3847}3848if(defined$type&&$typeeq'blob') {3849print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3850 hash_base=>$hb),3851-title =>$name}, esc_path($basename));3852}elsif(defined$type&&$typeeq'tree') {3853print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3854 hash_base=>$hb),3855-title =>$name}, esc_path($basename));3856print" / ";3857}else{3858print esc_path($basename);3859}3860}3861print"<br/></div>\n";3862}38633864sub git_print_log {3865my$log=shift;3866my%opts=@_;38673868if($opts{'-remove_title'}) {3869# remove title, i.e. first line of log3870shift@$log;3871}3872# remove leading empty lines3873while(defined$log->[0] &&$log->[0]eq"") {3874shift@$log;3875}38763877# print log3878my$signoff=0;3879my$empty=0;3880foreachmy$line(@$log) {3881if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3882$signoff=1;3883$empty=0;3884if(!$opts{'-remove_signoff'}) {3885print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3886next;3887}else{3888# remove signoff lines3889next;3890}3891}else{3892$signoff=0;3893}38943895# print only one empty line3896# do not print empty line after signoff3897if($lineeq"") {3898next if($empty||$signoff);3899$empty=1;3900}else{3901$empty=0;3902}39033904print format_log_line_html($line) ."<br/>\n";3905}39063907if($opts{'-final_empty_line'}) {3908# end with single empty line3909print"<br/>\n"unless$empty;3910}3911}39123913# return link target (what link points to)3914sub git_get_link_target {3915my$hash=shift;3916my$link_target;39173918# read link3919open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3920orreturn;3921{3922local$/=undef;3923$link_target= <$fd>;3924}3925close$fd3926orreturn;39273928return$link_target;3929}39303931# given link target, and the directory (basedir) the link is in,3932# return target of link relative to top directory (top tree);3933# return undef if it is not possible (including absolute links).3934sub normalize_link_target {3935my($link_target,$basedir) =@_;39363937# absolute symlinks (beginning with '/') cannot be normalized3938return if(substr($link_target,0,1)eq'/');39393940# normalize link target to path from top (root) tree (dir)3941my$path;3942if($basedir) {3943$path=$basedir.'/'.$link_target;3944}else{3945# we are in top (root) tree (dir)3946$path=$link_target;3947}39483949# remove //, /./, and /../3950my@path_parts;3951foreachmy$part(split('/',$path)) {3952# discard '.' and ''3953next if(!$part||$parteq'.');3954# handle '..'3955if($parteq'..') {3956if(@path_parts) {3957pop@path_parts;3958}else{3959# link leads outside repository (outside top dir)3960return;3961}3962}else{3963push@path_parts,$part;3964}3965}3966$path=join('/',@path_parts);39673968return$path;3969}39703971# print tree entry (row of git_tree), but without encompassing <tr> element3972sub git_print_tree_entry {3973my($t,$basedir,$hash_base,$have_blame) =@_;39743975my%base_key= ();3976$base_key{'hash_base'} =$hash_baseifdefined$hash_base;39773978# The format of a table row is: mode list link. Where mode is3979# the mode of the entry, list is the name of the entry, an href,3980# and link is the action links of the entry.39813982print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3983if(exists$t->{'size'}) {3984print"<td class=\"size\">$t->{'size'}</td>\n";3985}3986if($t->{'type'}eq"blob") {3987print"<td class=\"list\">".3988$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3989 file_name=>"$basedir$t->{'name'}",%base_key),3990-class=>"list"}, esc_path($t->{'name'}));3991if(S_ISLNK(oct$t->{'mode'})) {3992my$link_target= git_get_link_target($t->{'hash'});3993if($link_target) {3994my$norm_target= normalize_link_target($link_target,$basedir);3995if(defined$norm_target) {3996print" -> ".3997$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3998 file_name=>$norm_target),3999-title =>$norm_target}, esc_path($link_target));4000}else{4001print" -> ". esc_path($link_target);4002}4003}4004}4005print"</td>\n";4006print"<td class=\"link\">";4007print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4008 file_name=>"$basedir$t->{'name'}",%base_key)},4009"blob");4010if($have_blame) {4011print" | ".4012$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4013 file_name=>"$basedir$t->{'name'}",%base_key)},4014"blame");4015}4016if(defined$hash_base) {4017print" | ".4018$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4019 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4020"history");4021}4022print" | ".4023$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4024 file_name=>"$basedir$t->{'name'}")},4025"raw");4026print"</td>\n";40274028}elsif($t->{'type'}eq"tree") {4029print"<td class=\"list\">";4030print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4031 file_name=>"$basedir$t->{'name'}",4032%base_key)},4033 esc_path($t->{'name'}));4034print"</td>\n";4035print"<td class=\"link\">";4036print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4037 file_name=>"$basedir$t->{'name'}",4038%base_key)},4039"tree");4040if(defined$hash_base) {4041print" | ".4042$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4043 file_name=>"$basedir$t->{'name'}")},4044"history");4045}4046print"</td>\n";4047}else{4048# unknown object: we can only present history for it4049# (this includes 'commit' object, i.e. submodule support)4050print"<td class=\"list\">".4051 esc_path($t->{'name'}) .4052"</td>\n";4053print"<td class=\"link\">";4054if(defined$hash_base) {4055print$cgi->a({-href => href(action=>"history",4056 hash_base=>$hash_base,4057 file_name=>"$basedir$t->{'name'}")},4058"history");4059}4060print"</td>\n";4061}4062}40634064## ......................................................................4065## functions printing large fragments of HTML40664067# get pre-image filenames for merge (combined) diff4068sub fill_from_file_info {4069my($diff,@parents) =@_;40704071$diff->{'from_file'} = [ ];4072$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4073for(my$i=0;$i<$diff->{'nparents'};$i++) {4074if($diff->{'status'}[$i]eq'R'||4075$diff->{'status'}[$i]eq'C') {4076$diff->{'from_file'}[$i] =4077 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4078}4079}40804081return$diff;4082}40834084# is current raw difftree line of file deletion4085sub is_deleted {4086my$diffinfo=shift;40874088return$diffinfo->{'to_id'}eq('0' x 40);4089}40904091# does patch correspond to [previous] difftree raw line4092# $diffinfo - hashref of parsed raw diff format4093# $patchinfo - hashref of parsed patch diff format4094# (the same keys as in $diffinfo)4095sub is_patch_split {4096my($diffinfo,$patchinfo) =@_;40974098returndefined$diffinfo&&defined$patchinfo4099&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4100}410141024103sub git_difftree_body {4104my($difftree,$hash,@parents) =@_;4105my($parent) =$parents[0];4106my$have_blame= gitweb_check_feature('blame');4107print"<div class=\"list_head\">\n";4108if($#{$difftree} >10) {4109print(($#{$difftree} +1) ." files changed:\n");4110}4111print"</div>\n";41124113print"<table class=\"".4114(@parents>1?"combined ":"") .4115"diff_tree\">\n";41164117# header only for combined diff in 'commitdiff' view4118my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4119if($has_header) {4120# table header4121print"<thead><tr>\n".4122"<th></th><th></th>\n";# filename, patchN link4123for(my$i=0;$i<@parents;$i++) {4124my$par=$parents[$i];4125print"<th>".4126$cgi->a({-href => href(action=>"commitdiff",4127 hash=>$hash, hash_parent=>$par),4128-title =>'commitdiff to parent number '.4129($i+1) .': '.substr($par,0,7)},4130$i+1) .4131" </th>\n";4132}4133print"</tr></thead>\n<tbody>\n";4134}41354136my$alternate=1;4137my$patchno=0;4138foreachmy$line(@{$difftree}) {4139my$diff= parsed_difftree_line($line);41404141if($alternate) {4142print"<tr class=\"dark\">\n";4143}else{4144print"<tr class=\"light\">\n";4145}4146$alternate^=1;41474148if(exists$diff->{'nparents'}) {# combined diff41494150 fill_from_file_info($diff,@parents)4151unlessexists$diff->{'from_file'};41524153if(!is_deleted($diff)) {4154# file exists in the result (child) commit4155print"<td>".4156$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4157 file_name=>$diff->{'to_file'},4158 hash_base=>$hash),4159-class=>"list"}, esc_path($diff->{'to_file'})) .4160"</td>\n";4161}else{4162print"<td>".4163 esc_path($diff->{'to_file'}) .4164"</td>\n";4165}41664167if($actioneq'commitdiff') {4168# link to patch4169$patchno++;4170print"<td class=\"link\">".4171$cgi->a({-href =>"#patch$patchno"},"patch") .4172" | ".4173"</td>\n";4174}41754176my$has_history=0;4177my$not_deleted=0;4178for(my$i=0;$i<$diff->{'nparents'};$i++) {4179my$hash_parent=$parents[$i];4180my$from_hash=$diff->{'from_id'}[$i];4181my$from_path=$diff->{'from_file'}[$i];4182my$status=$diff->{'status'}[$i];41834184$has_history||= ($statusne'A');4185$not_deleted||= ($statusne'D');41864187if($statuseq'A') {4188print"<td class=\"link\"align=\"right\"> | </td>\n";4189}elsif($statuseq'D') {4190print"<td class=\"link\">".4191$cgi->a({-href => href(action=>"blob",4192 hash_base=>$hash,4193 hash=>$from_hash,4194 file_name=>$from_path)},4195"blob". ($i+1)) .4196" | </td>\n";4197}else{4198if($diff->{'to_id'}eq$from_hash) {4199print"<td class=\"link nochange\">";4200}else{4201print"<td class=\"link\">";4202}4203print$cgi->a({-href => href(action=>"blobdiff",4204 hash=>$diff->{'to_id'},4205 hash_parent=>$from_hash,4206 hash_base=>$hash,4207 hash_parent_base=>$hash_parent,4208 file_name=>$diff->{'to_file'},4209 file_parent=>$from_path)},4210"diff". ($i+1)) .4211" | </td>\n";4212}4213}42144215print"<td class=\"link\">";4216if($not_deleted) {4217print$cgi->a({-href => href(action=>"blob",4218 hash=>$diff->{'to_id'},4219 file_name=>$diff->{'to_file'},4220 hash_base=>$hash)},4221"blob");4222print" | "if($has_history);4223}4224if($has_history) {4225print$cgi->a({-href => href(action=>"history",4226 file_name=>$diff->{'to_file'},4227 hash_base=>$hash)},4228"history");4229}4230print"</td>\n";42314232print"</tr>\n";4233next;# instead of 'else' clause, to avoid extra indent4234}4235# else ordinary diff42364237my($to_mode_oct,$to_mode_str,$to_file_type);4238my($from_mode_oct,$from_mode_str,$from_file_type);4239if($diff->{'to_mode'}ne('0' x 6)) {4240$to_mode_oct=oct$diff->{'to_mode'};4241if(S_ISREG($to_mode_oct)) {# only for regular file4242$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4243}4244$to_file_type= file_type($diff->{'to_mode'});4245}4246if($diff->{'from_mode'}ne('0' x 6)) {4247$from_mode_oct=oct$diff->{'from_mode'};4248if(S_ISREG($to_mode_oct)) {# only for regular file4249$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4250}4251$from_file_type= file_type($diff->{'from_mode'});4252}42534254if($diff->{'status'}eq"A") {# created4255my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4256$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4257$mode_chng.="]</span>";4258print"<td>";4259print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4260 hash_base=>$hash, file_name=>$diff->{'file'}),4261-class=>"list"}, esc_path($diff->{'file'}));4262print"</td>\n";4263print"<td>$mode_chng</td>\n";4264print"<td class=\"link\">";4265if($actioneq'commitdiff') {4266# link to patch4267$patchno++;4268print$cgi->a({-href =>"#patch$patchno"},"patch");4269print" | ";4270}4271print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4272 hash_base=>$hash, file_name=>$diff->{'file'})},4273"blob");4274print"</td>\n";42754276}elsif($diff->{'status'}eq"D") {# deleted4277my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4278print"<td>";4279print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4280 hash_base=>$parent, file_name=>$diff->{'file'}),4281-class=>"list"}, esc_path($diff->{'file'}));4282print"</td>\n";4283print"<td>$mode_chng</td>\n";4284print"<td class=\"link\">";4285if($actioneq'commitdiff') {4286# link to patch4287$patchno++;4288print$cgi->a({-href =>"#patch$patchno"},"patch");4289print" | ";4290}4291print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4292 hash_base=>$parent, file_name=>$diff->{'file'})},4293"blob") ." | ";4294if($have_blame) {4295print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4296 file_name=>$diff->{'file'})},4297"blame") ." | ";4298}4299print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4300 file_name=>$diff->{'file'})},4301"history");4302print"</td>\n";43034304}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4305my$mode_chnge="";4306if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4307$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4308if($from_file_typene$to_file_type) {4309$mode_chnge.=" from$from_file_typeto$to_file_type";4310}4311if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4312if($from_mode_str&&$to_mode_str) {4313$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4314}elsif($to_mode_str) {4315$mode_chnge.=" mode:$to_mode_str";4316}4317}4318$mode_chnge.="]</span>\n";4319}4320print"<td>";4321print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4322 hash_base=>$hash, file_name=>$diff->{'file'}),4323-class=>"list"}, esc_path($diff->{'file'}));4324print"</td>\n";4325print"<td>$mode_chnge</td>\n";4326print"<td class=\"link\">";4327if($actioneq'commitdiff') {4328# link to patch4329$patchno++;4330print$cgi->a({-href =>"#patch$patchno"},"patch") .4331" | ";4332}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4333# "commit" view and modified file (not onlu mode changed)4334print$cgi->a({-href => href(action=>"blobdiff",4335 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4336 hash_base=>$hash, hash_parent_base=>$parent,4337 file_name=>$diff->{'file'})},4338"diff") .4339" | ";4340}4341print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4342 hash_base=>$hash, file_name=>$diff->{'file'})},4343"blob") ." | ";4344if($have_blame) {4345print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4346 file_name=>$diff->{'file'})},4347"blame") ." | ";4348}4349print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4350 file_name=>$diff->{'file'})},4351"history");4352print"</td>\n";43534354}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4355my%status_name= ('R'=>'moved','C'=>'copied');4356my$nstatus=$status_name{$diff->{'status'}};4357my$mode_chng="";4358if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4359# mode also for directories, so we cannot use $to_mode_str4360$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4361}4362print"<td>".4363$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4364 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4365-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4366"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4367$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4368 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4369-class=>"list"}, esc_path($diff->{'from_file'})) .4370" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4371"<td class=\"link\">";4372if($actioneq'commitdiff') {4373# link to patch4374$patchno++;4375print$cgi->a({-href =>"#patch$patchno"},"patch") .4376" | ";4377}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4378# "commit" view and modified file (not only pure rename or copy)4379print$cgi->a({-href => href(action=>"blobdiff",4380 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4381 hash_base=>$hash, hash_parent_base=>$parent,4382 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4383"diff") .4384" | ";4385}4386print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4387 hash_base=>$parent, file_name=>$diff->{'to_file'})},4388"blob") ." | ";4389if($have_blame) {4390print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4391 file_name=>$diff->{'to_file'})},4392"blame") ." | ";4393}4394print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4395 file_name=>$diff->{'to_file'})},4396"history");4397print"</td>\n";43984399}# we should not encounter Unmerged (U) or Unknown (X) status4400print"</tr>\n";4401}4402print"</tbody>"if$has_header;4403print"</table>\n";4404}44054406sub git_patchset_body {4407my($fd,$difftree,$hash,@hash_parents) =@_;4408my($hash_parent) =$hash_parents[0];44094410my$is_combined= (@hash_parents>1);4411my$patch_idx=0;4412my$patch_number=0;4413my$patch_line;4414my$diffinfo;4415my$to_name;4416my(%from,%to);44174418print"<div class=\"patchset\">\n";44194420# skip to first patch4421while($patch_line= <$fd>) {4422chomp$patch_line;44234424last if($patch_line=~m/^diff /);4425}44264427 PATCH:4428while($patch_line) {44294430# parse "git diff" header line4431if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4432# $1 is from_name, which we do not use4433$to_name= unquote($2);4434$to_name=~s!^b/!!;4435}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4436# $1 is 'cc' or 'combined', which we do not use4437$to_name= unquote($2);4438}else{4439$to_name=undef;4440}44414442# check if current patch belong to current raw line4443# and parse raw git-diff line if needed4444if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4445# this is continuation of a split patch4446print"<div class=\"patch cont\">\n";4447}else{4448# advance raw git-diff output if needed4449$patch_idx++ifdefined$diffinfo;44504451# read and prepare patch information4452$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);44534454# compact combined diff output can have some patches skipped4455# find which patch (using pathname of result) we are at now;4456if($is_combined) {4457while($to_namene$diffinfo->{'to_file'}) {4458print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4459 format_diff_cc_simplified($diffinfo,@hash_parents) .4460"</div>\n";# class="patch"44614462$patch_idx++;4463$patch_number++;44644465last if$patch_idx>$#$difftree;4466$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4467}4468}44694470# modifies %from, %to hashes4471 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);44724473# this is first patch for raw difftree line with $patch_idx index4474# we index @$difftree array from 0, but number patches from 14475print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4476}44774478# git diff header4479#assert($patch_line =~ m/^diff /) if DEBUG;4480#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4481$patch_number++;4482# print "git diff" header4483print format_git_diff_header_line($patch_line,$diffinfo,4484 \%from, \%to);44854486# print extended diff header4487print"<div class=\"diff extended_header\">\n";4488 EXTENDED_HEADER:4489while($patch_line= <$fd>) {4490chomp$patch_line;44914492last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);44934494print format_extended_diff_header_line($patch_line,$diffinfo,4495 \%from, \%to);4496}4497print"</div>\n";# class="diff extended_header"44984499# from-file/to-file diff header4500if(!$patch_line) {4501print"</div>\n";# class="patch"4502last PATCH;4503}4504next PATCH if($patch_line=~m/^diff /);4505#assert($patch_line =~ m/^---/) if DEBUG;45064507my$last_patch_line=$patch_line;4508$patch_line= <$fd>;4509chomp$patch_line;4510#assert($patch_line =~ m/^\+\+\+/) if DEBUG;45114512print format_diff_from_to_header($last_patch_line,$patch_line,4513$diffinfo, \%from, \%to,4514@hash_parents);45154516# the patch itself4517 LINE:4518while($patch_line= <$fd>) {4519chomp$patch_line;45204521next PATCH if($patch_line=~m/^diff /);45224523print format_diff_line($patch_line, \%from, \%to);4524}45254526}continue{4527print"</div>\n";# class="patch"4528}45294530# for compact combined (--cc) format, with chunk and patch simplification4531# the patchset might be empty, but there might be unprocessed raw lines4532for(++$patch_idxif$patch_number>0;4533$patch_idx<@$difftree;4534++$patch_idx) {4535# read and prepare patch information4536$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);45374538# generate anchor for "patch" links in difftree / whatchanged part4539print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4540 format_diff_cc_simplified($diffinfo,@hash_parents) .4541"</div>\n";# class="patch"45424543$patch_number++;4544}45454546if($patch_number==0) {4547if(@hash_parents>1) {4548print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4549}else{4550print"<div class=\"diff nodifferences\">No differences found</div>\n";4551}4552}45534554print"</div>\n";# class="patchset"4555}45564557# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45584559# fills project list info (age, description, owner, forks) for each4560# project in the list, removing invalid projects from returned list4561# NOTE: modifies $projlist, but does not remove entries from it4562sub fill_project_list_info {4563my($projlist,$check_forks) =@_;4564my@projects;45654566my$show_ctags= gitweb_check_feature('ctags');4567 PROJECT:4568foreachmy$pr(@$projlist) {4569my(@activity) = git_get_last_activity($pr->{'path'});4570unless(@activity) {4571next PROJECT;4572}4573($pr->{'age'},$pr->{'age_string'}) =@activity;4574if(!defined$pr->{'descr'}) {4575my$descr= git_get_project_description($pr->{'path'}) ||"";4576$descr= to_utf8($descr);4577$pr->{'descr_long'} =$descr;4578$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4579}4580if(!defined$pr->{'owner'}) {4581$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4582}4583if($check_forks) {4584my$pname=$pr->{'path'};4585if(($pname=~s/\.git$//) &&4586($pname!~/\/$/) &&4587(-d "$projectroot/$pname")) {4588$pr->{'forks'} ="-d$projectroot/$pname";4589}else{4590$pr->{'forks'} =0;4591}4592}4593$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4594push@projects,$pr;4595}45964597return@projects;4598}45994600# print 'sort by' <th> element, generating 'sort by $name' replay link4601# if that order is not selected4602sub print_sort_th {4603print format_sort_th(@_);4604}46054606sub format_sort_th {4607my($name,$order,$header) =@_;4608my$sort_th="";4609$header||=ucfirst($name);46104611if($ordereq$name) {4612$sort_th.="<th>$header</th>\n";4613}else{4614$sort_th.="<th>".4615$cgi->a({-href => href(-replay=>1, order=>$name),4616-class=>"header"},$header) .4617"</th>\n";4618}46194620return$sort_th;4621}46224623sub git_project_list_body {4624# actually uses global variable $project4625my($projlist,$order,$from,$to,$extra,$no_header) =@_;46264627my$check_forks= gitweb_check_feature('forks');4628my@projects= fill_project_list_info($projlist,$check_forks);46294630$order||=$default_projects_order;4631$from=0unlessdefined$from;4632$to=$#projectsif(!defined$to||$#projects<$to);46334634my%order_info= (4635 project => { key =>'path', type =>'str'},4636 descr => { key =>'descr_long', type =>'str'},4637 owner => { key =>'owner', type =>'str'},4638 age => { key =>'age', type =>'num'}4639);4640my$oi=$order_info{$order};4641if($oi->{'type'}eq'str') {4642@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4643}else{4644@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4645}46464647my$show_ctags= gitweb_check_feature('ctags');4648if($show_ctags) {4649my%ctags;4650foreachmy$p(@projects) {4651foreachmy$ct(keys%{$p->{'ctags'}}) {4652$ctags{$ct} +=$p->{'ctags'}->{$ct};4653}4654}4655my$cloud= git_populate_project_tagcloud(\%ctags);4656print git_show_project_tagcloud($cloud,64);4657}46584659print"<table class=\"project_list\">\n";4660unless($no_header) {4661print"<tr>\n";4662if($check_forks) {4663print"<th></th>\n";4664}4665 print_sort_th('project',$order,'Project');4666 print_sort_th('descr',$order,'Description');4667 print_sort_th('owner',$order,'Owner');4668 print_sort_th('age',$order,'Last Change');4669print"<th></th>\n".# for links4670"</tr>\n";4671}4672my$alternate=1;4673my$tagfilter=$cgi->param('by_tag');4674for(my$i=$from;$i<=$to;$i++) {4675my$pr=$projects[$i];46764677next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4678next if$searchtextand not$pr->{'path'} =~/$searchtext/4679and not$pr->{'descr_long'} =~/$searchtext/;4680# Weed out forks or non-matching entries of search4681if($check_forks) {4682my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4683$forkbase="^$forkbase"if$forkbase;4684next ifnot$searchtextand not$tagfilterand$show_ctags4685and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4686}46874688if($alternate) {4689print"<tr class=\"dark\">\n";4690}else{4691print"<tr class=\"light\">\n";4692}4693$alternate^=1;4694if($check_forks) {4695print"<td>";4696if($pr->{'forks'}) {4697print"<!--$pr->{'forks'} -->\n";4698print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4699}4700print"</td>\n";4701}4702print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4703-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4704"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4705-class=>"list", -title =>$pr->{'descr_long'}},4706 esc_html($pr->{'descr'})) ."</td>\n".4707"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4708print"<td class=\"". age_class($pr->{'age'}) ."\">".4709(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4710"<td class=\"link\">".4711$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4712$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4713$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4714$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4715($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4716"</td>\n".4717"</tr>\n";4718}4719if(defined$extra) {4720print"<tr>\n";4721if($check_forks) {4722print"<td></td>\n";4723}4724print"<td colspan=\"5\">$extra</td>\n".4725"</tr>\n";4726}4727print"</table>\n";4728}47294730sub git_log_body {4731# uses global variable $project4732my($commitlist,$from,$to,$refs,$extra) =@_;47334734$from=0unlessdefined$from;4735$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47364737for(my$i=0;$i<=$to;$i++) {4738my%co= %{$commitlist->[$i]};4739next if!%co;4740my$commit=$co{'id'};4741my$ref= format_ref_marker($refs,$commit);4742my%ad= parse_date($co{'author_epoch'});4743 git_print_header_div('commit',4744"<span class=\"age\">$co{'age_string'}</span>".4745 esc_html($co{'title'}) .$ref,4746$commit);4747print"<div class=\"title_text\">\n".4748"<div class=\"log_link\">\n".4749$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4750" | ".4751$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4752" | ".4753$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4754"<br/>\n".4755"</div>\n";4756 git_print_authorship(\%co, -tag =>'span');4757print"<br/>\n</div>\n";47584759print"<div class=\"log_body\">\n";4760 git_print_log($co{'comment'}, -final_empty_line=>1);4761print"</div>\n";4762}4763if($extra) {4764print"<div class=\"page_nav\">\n";4765print"$extra\n";4766print"</div>\n";4767}4768}47694770sub git_shortlog_body {4771# uses global variable $project4772my($commitlist,$from,$to,$refs,$extra) =@_;47734774$from=0unlessdefined$from;4775$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47764777print"<table class=\"shortlog\">\n";4778my$alternate=1;4779for(my$i=$from;$i<=$to;$i++) {4780my%co= %{$commitlist->[$i]};4781my$commit=$co{'id'};4782my$ref= format_ref_marker($refs,$commit);4783if($alternate) {4784print"<tr class=\"dark\">\n";4785}else{4786print"<tr class=\"light\">\n";4787}4788$alternate^=1;4789# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4790print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4791 format_author_html('td', \%co,10) ."<td>";4792print format_subject_html($co{'title'},$co{'title_short'},4793 href(action=>"commit", hash=>$commit),$ref);4794print"</td>\n".4795"<td class=\"link\">".4796$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4797$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4798$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4799my$snapshot_links= format_snapshot_links($commit);4800if(defined$snapshot_links) {4801print" | ".$snapshot_links;4802}4803print"</td>\n".4804"</tr>\n";4805}4806if(defined$extra) {4807print"<tr>\n".4808"<td colspan=\"4\">$extra</td>\n".4809"</tr>\n";4810}4811print"</table>\n";4812}48134814sub git_history_body {4815# Warning: assumes constant type (blob or tree) during history4816my($commitlist,$from,$to,$refs,$extra,4817$file_name,$file_hash,$ftype) =@_;48184819$from=0unlessdefined$from;4820$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});48214822print"<table class=\"history\">\n";4823my$alternate=1;4824for(my$i=$from;$i<=$to;$i++) {4825my%co= %{$commitlist->[$i]};4826if(!%co) {4827next;4828}4829my$commit=$co{'id'};48304831my$ref= format_ref_marker($refs,$commit);48324833if($alternate) {4834print"<tr class=\"dark\">\n";4835}else{4836print"<tr class=\"light\">\n";4837}4838$alternate^=1;4839print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4840# shortlog: format_author_html('td', \%co, 10)4841 format_author_html('td', \%co,15,3) ."<td>";4842# originally git_history used chop_str($co{'title'}, 50)4843print format_subject_html($co{'title'},$co{'title_short'},4844 href(action=>"commit", hash=>$commit),$ref);4845print"</td>\n".4846"<td class=\"link\">".4847$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4848$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");48494850if($ftypeeq'blob') {4851my$blob_current=$file_hash;4852my$blob_parent= git_get_hash_by_path($commit,$file_name);4853if(defined$blob_current&&defined$blob_parent&&4854$blob_currentne$blob_parent) {4855print" | ".4856$cgi->a({-href => href(action=>"blobdiff",4857 hash=>$blob_current, hash_parent=>$blob_parent,4858 hash_base=>$hash_base, hash_parent_base=>$commit,4859 file_name=>$file_name)},4860"diff to current");4861}4862}4863print"</td>\n".4864"</tr>\n";4865}4866if(defined$extra) {4867print"<tr>\n".4868"<td colspan=\"4\">$extra</td>\n".4869"</tr>\n";4870}4871print"</table>\n";4872}48734874sub git_tags_body {4875# uses global variable $project4876my($taglist,$from,$to,$extra) =@_;4877$from=0unlessdefined$from;4878$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);48794880print"<table class=\"tags\">\n";4881my$alternate=1;4882for(my$i=$from;$i<=$to;$i++) {4883my$entry=$taglist->[$i];4884my%tag=%$entry;4885my$comment=$tag{'subject'};4886my$comment_short;4887if(defined$comment) {4888$comment_short= chop_str($comment,30,5);4889}4890if($alternate) {4891print"<tr class=\"dark\">\n";4892}else{4893print"<tr class=\"light\">\n";4894}4895$alternate^=1;4896if(defined$tag{'age'}) {4897print"<td><i>$tag{'age'}</i></td>\n";4898}else{4899print"<td></td>\n";4900}4901print"<td>".4902$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4903-class=>"list name"}, esc_html($tag{'name'})) .4904"</td>\n".4905"<td>";4906if(defined$comment) {4907print format_subject_html($comment,$comment_short,4908 href(action=>"tag", hash=>$tag{'id'}));4909}4910print"</td>\n".4911"<td class=\"selflink\">";4912if($tag{'type'}eq"tag") {4913print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4914}else{4915print" ";4916}4917print"</td>\n".4918"<td class=\"link\">"." | ".4919$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4920if($tag{'reftype'}eq"commit") {4921print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4922" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4923}elsif($tag{'reftype'}eq"blob") {4924print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4925}4926print"</td>\n".4927"</tr>";4928}4929if(defined$extra) {4930print"<tr>\n".4931"<td colspan=\"5\">$extra</td>\n".4932"</tr>\n";4933}4934print"</table>\n";4935}49364937sub git_heads_body {4938# uses global variable $project4939my($headlist,$head,$from,$to,$extra) =@_;4940$from=0unlessdefined$from;4941$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);49424943print"<table class=\"heads\">\n";4944my$alternate=1;4945for(my$i=$from;$i<=$to;$i++) {4946my$entry=$headlist->[$i];4947my%ref=%$entry;4948my$curr=$ref{'id'}eq$head;4949if($alternate) {4950print"<tr class=\"dark\">\n";4951}else{4952print"<tr class=\"light\">\n";4953}4954$alternate^=1;4955print"<td><i>$ref{'age'}</i></td>\n".4956($curr?"<td class=\"current_head\">":"<td>") .4957$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4958-class=>"list name"},esc_html($ref{'name'})) .4959"</td>\n".4960"<td class=\"link\">".4961$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4962$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4963$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4964"</td>\n".4965"</tr>";4966}4967if(defined$extra) {4968print"<tr>\n".4969"<td colspan=\"3\">$extra</td>\n".4970"</tr>\n";4971}4972print"</table>\n";4973}49744975sub git_search_grep_body {4976my($commitlist,$from,$to,$extra) =@_;4977$from=0unlessdefined$from;4978$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);49794980print"<table class=\"commit_search\">\n";4981my$alternate=1;4982for(my$i=$from;$i<=$to;$i++) {4983my%co= %{$commitlist->[$i]};4984if(!%co) {4985next;4986}4987my$commit=$co{'id'};4988if($alternate) {4989print"<tr class=\"dark\">\n";4990}else{4991print"<tr class=\"light\">\n";4992}4993$alternate^=1;4994print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4995 format_author_html('td', \%co,15,5) .4996"<td>".4997$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4998-class=>"list subject"},4999 chop_and_escape_str($co{'title'},50) ."<br/>");5000my$comment=$co{'comment'};5001foreachmy$line(@$comment) {5002if($line=~m/^(.*?)($search_regexp)(.*)$/i) {5003my($lead,$match,$trail) = ($1,$2,$3);5004$match= chop_str($match,70,5,'center');5005my$contextlen=int((80-length($match))/2);5006$contextlen=30if($contextlen>30);5007$lead= chop_str($lead,$contextlen,10,'left');5008$trail= chop_str($trail,$contextlen,10,'right');50095010$lead= esc_html($lead);5011$match= esc_html($match);5012$trail= esc_html($trail);50135014print"$lead<span class=\"match\">$match</span>$trail<br />";5015}5016}5017print"</td>\n".5018"<td class=\"link\">".5019$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5020" | ".5021$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .5022" | ".5023$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5024print"</td>\n".5025"</tr>\n";5026}5027if(defined$extra) {5028print"<tr>\n".5029"<td colspan=\"3\">$extra</td>\n".5030"</tr>\n";5031}5032print"</table>\n";5033}50345035## ======================================================================5036## ======================================================================5037## actions50385039sub git_project_list {5040my$order=$input_params{'order'};5041if(defined$order&&$order!~m/none|project|descr|owner|age/) {5042 die_error(400,"Unknown order parameter");5043}50445045my@list= git_get_projects_list();5046if(!@list) {5047 die_error(404,"No projects found");5048}50495050 git_header_html();5051if(defined$home_text&& -f $home_text) {5052print"<div class=\"index_include\">\n";5053 insert_file($home_text);5054print"</div>\n";5055}5056print$cgi->startform(-method=>"get") .5057"<p class=\"projsearch\">Search:\n".5058$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".5059"</p>".5060$cgi->end_form() ."\n";5061 git_project_list_body(\@list,$order);5062 git_footer_html();5063}50645065sub git_forks {5066my$order=$input_params{'order'};5067if(defined$order&&$order!~m/none|project|descr|owner|age/) {5068 die_error(400,"Unknown order parameter");5069}50705071my@list= git_get_projects_list($project);5072if(!@list) {5073 die_error(404,"No forks found");5074}50755076 git_header_html();5077 git_print_page_nav('','');5078 git_print_header_div('summary',"$projectforks");5079 git_project_list_body(\@list,$order);5080 git_footer_html();5081}50825083sub git_project_index {5084my@projects= git_get_projects_list($project);50855086print$cgi->header(5087-type =>'text/plain',5088-charset =>'utf-8',5089-content_disposition =>'inline; filename="index.aux"');50905091foreachmy$pr(@projects) {5092if(!exists$pr->{'owner'}) {5093$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");5094}50955096my($path,$owner) = ($pr->{'path'},$pr->{'owner'});5097# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '5098$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5099$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5100$path=~s/ /\+/g;5101$owner=~s/ /\+/g;51025103print"$path$owner\n";5104}5105}51065107sub git_summary {5108my$descr= git_get_project_description($project) ||"none";5109my%co= parse_commit("HEAD");5110my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();5111my$head=$co{'id'};51125113my$owner= git_get_project_owner($project);51145115my$refs= git_get_references();5116# These get_*_list functions return one more to allow us to see if5117# there are more ...5118my@taglist= git_get_tags_list(16);5119my@headlist= git_get_heads_list(16);5120my@forklist;5121my$check_forks= gitweb_check_feature('forks');51225123if($check_forks) {5124@forklist= git_get_projects_list($project);5125}51265127 git_header_html();5128 git_print_page_nav('summary','',$head);51295130print"<div class=\"title\"> </div>\n";5131print"<table class=\"projects_list\">\n".5132"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".5133"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";5134if(defined$cd{'rfc2822'}) {5135print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";5136}51375138# use per project git URL list in $projectroot/$project/cloneurl5139# or make project git URL from git base URL and project name5140my$url_tag="URL";5141my@url_list= git_get_project_url_list($project);5142@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;5143foreachmy$git_url(@url_list) {5144next unless$git_url;5145print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";5146$url_tag="";5147}51485149# Tag cloud5150my$show_ctags= gitweb_check_feature('ctags');5151if($show_ctags) {5152my$ctags= git_get_project_ctags($project);5153my$cloud= git_populate_project_tagcloud($ctags);5154print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";5155print"</td>\n<td>"unless%$ctags;5156print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";5157print"</td>\n<td>"if%$ctags;5158print git_show_project_tagcloud($cloud,48);5159print"</td></tr>";5160}51615162print"</table>\n";51635164# If XSS prevention is on, we don't include README.html.5165# TODO: Allow a readme in some safe format.5166if(!$prevent_xss&& -s "$projectroot/$project/README.html") {5167print"<div class=\"title\">readme</div>\n".5168"<div class=\"readme\">\n";5169 insert_file("$projectroot/$project/README.html");5170print"\n</div>\n";# class="readme"5171}51725173# we need to request one more than 16 (0..15) to check if5174# those 16 are all5175my@commitlist=$head? parse_commits($head,17) : ();5176if(@commitlist) {5177 git_print_header_div('shortlog');5178 git_shortlog_body(\@commitlist,0,15,$refs,5179$#commitlist<=15?undef:5180$cgi->a({-href => href(action=>"shortlog")},"..."));5181}51825183if(@taglist) {5184 git_print_header_div('tags');5185 git_tags_body(\@taglist,0,15,5186$#taglist<=15?undef:5187$cgi->a({-href => href(action=>"tags")},"..."));5188}51895190if(@headlist) {5191 git_print_header_div('heads');5192 git_heads_body(\@headlist,$head,0,15,5193$#headlist<=15?undef:5194$cgi->a({-href => href(action=>"heads")},"..."));5195}51965197if(@forklist) {5198 git_print_header_div('forks');5199 git_project_list_body(\@forklist,'age',0,15,5200$#forklist<=15?undef:5201$cgi->a({-href => href(action=>"forks")},"..."),5202'no_header');5203}52045205 git_footer_html();5206}52075208sub git_tag {5209my%tag= parse_tag($hash);52105211if(!%tag) {5212 die_error(404,"Unknown tag object");5213}52145215my$head= git_get_head_hash($project);5216 git_header_html();5217 git_print_page_nav('','',$head,undef,$head);5218 git_print_header_div('commit', esc_html($tag{'name'}),$hash);5219print"<div class=\"title_text\">\n".5220"<table class=\"object_header\">\n".5221"<tr>\n".5222"<td>object</td>\n".5223"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5224$tag{'object'}) ."</td>\n".5225"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5226$tag{'type'}) ."</td>\n".5227"</tr>\n";5228if(defined($tag{'author'})) {5229 git_print_authorship_rows(\%tag,'author');5230}5231print"</table>\n\n".5232"</div>\n";5233print"<div class=\"page_body\">";5234my$comment=$tag{'comment'};5235foreachmy$line(@$comment) {5236chomp$line;5237print esc_html($line, -nbsp=>1) ."<br/>\n";5238}5239print"</div>\n";5240 git_footer_html();5241}52425243sub git_blame_common {5244my$format=shift||'porcelain';5245if($formateq'porcelain'&&$cgi->param('js')) {5246$format='incremental';5247$action='blame_incremental';# for page title etc5248}52495250# permissions5251 gitweb_check_feature('blame')5252or die_error(403,"Blame view not allowed");52535254# error checking5255 die_error(400,"No file name given")unless$file_name;5256$hash_base||= git_get_head_hash($project);5257 die_error(404,"Couldn't find base commit")unless$hash_base;5258my%co= parse_commit($hash_base)5259or die_error(404,"Commit not found");5260my$ftype="blob";5261if(!defined$hash) {5262$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5263or die_error(404,"Error looking up file");5264}else{5265$ftype= git_get_type($hash);5266if($ftype!~"blob") {5267 die_error(400,"Object is not a blob");5268}5269}52705271my$fd;5272if($formateq'incremental') {5273# get file contents (as base)5274open$fd,"-|", git_cmd(),'cat-file','blob',$hash5275or die_error(500,"Open git-cat-file failed");5276}elsif($formateq'data') {5277# run git-blame --incremental5278open$fd,"-|", git_cmd(),"blame","--incremental",5279$hash_base,"--",$file_name5280or die_error(500,"Open git-blame --incremental failed");5281}else{5282# run git-blame --porcelain5283open$fd,"-|", git_cmd(),"blame",'-p',5284$hash_base,'--',$file_name5285or die_error(500,"Open git-blame --porcelain failed");5286}52875288# incremental blame data returns early5289if($formateq'data') {5290print$cgi->header(5291-type=>"text/plain", -charset =>"utf-8",5292-status=>"200 OK");5293local$| =1;# output autoflush5294printwhile<$fd>;5295close$fd5296or print"ERROR$!\n";52975298print'END';5299if(defined$t0&& gitweb_check_feature('timed')) {5300print' '.5301 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5302' '.$number_of_git_cmds;5303}5304print"\n";53055306return;5307}53085309# page header5310 git_header_html();5311my$formats_nav=5312$cgi->a({-href => href(action=>"blob", -replay=>1)},5313"blob") .5314" | ";5315if($formateq'incremental') {5316$formats_nav.=5317$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5318"blame") ." (non-incremental)";5319}else{5320$formats_nav.=5321$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5322"blame") ." (incremental)";5323}5324$formats_nav.=5325" | ".5326$cgi->a({-href => href(action=>"history", -replay=>1)},5327"history") .5328" | ".5329$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5330"HEAD");5331 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5332 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5333 git_print_page_path($file_name,$ftype,$hash_base);53345335# page body5336if($formateq'incremental') {5337print"<noscript>\n<div class=\"error\"><center><b>\n".5338"This page requires JavaScript to run.\nUse ".5339$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5340'this page').5341" instead.\n".5342"</b></center></div>\n</noscript>\n";53435344print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5345}53465347print qq!<div class="page_body">\n!;5348print qq!<div id="progress_info">.../ ...</div>\n!5349if($formateq'incremental');5350print qq!<table id="blame_table"class="blame" width="100%">\n!.5351#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5352 qq!<thead>\n!.5353 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5354 qq!</thead>\n!.5355 qq!<tbody>\n!;53565357my@rev_color=qw(light dark);5358my$num_colors=scalar(@rev_color);5359my$current_color=0;53605361if($formateq'incremental') {5362my$color_class=$rev_color[$current_color];53635364#contents of a file5365my$linenr=0;5366 LINE:5367while(my$line= <$fd>) {5368chomp$line;5369$linenr++;53705371print qq!<tr id="l$linenr"class="$color_class">!.5372 qq!<td class="sha1"><a href=""> </a></td>!.5373 qq!<td class="linenr">!.5374 qq!<a class="linenr" href="">$linenr</a></td>!;5375print qq!<td class="pre">! . esc_html($line) ."</td>\n";5376print qq!</tr>\n!;5377}53785379}else{# porcelain, i.e. ordinary blame5380my%metainfo= ();# saves information about commits53815382# blame data5383 LINE:5384while(my$line= <$fd>) {5385chomp$line;5386# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5387# no <lines in group> for subsequent lines in group of lines5388my($full_rev,$orig_lineno,$lineno,$group_size) =5389($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5390if(!exists$metainfo{$full_rev}) {5391$metainfo{$full_rev} = {'nprevious'=>0};5392}5393my$meta=$metainfo{$full_rev};5394my$data;5395while($data= <$fd>) {5396chomp$data;5397last if($data=~s/^\t//);# contents of line5398if($data=~/^(\S+)(?: (.*))?$/) {5399$meta->{$1} =$2unlessexists$meta->{$1};5400}5401if($data=~/^previous /) {5402$meta->{'nprevious'}++;5403}5404}5405my$short_rev=substr($full_rev,0,8);5406my$author=$meta->{'author'};5407my%date=5408 parse_date($meta->{'author-time'},$meta->{'author-tz'});5409my$date=$date{'iso-tz'};5410if($group_size) {5411$current_color= ($current_color+1) %$num_colors;5412}5413my$tr_class=$rev_color[$current_color];5414$tr_class.=' boundary'if(exists$meta->{'boundary'});5415$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5416$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5417print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5418if($group_size) {5419print"<td class=\"sha1\"";5420print" title=\"". esc_html($author) .",$date\"";5421print" rowspan=\"$group_size\""if($group_size>1);5422print">";5423print$cgi->a({-href => href(action=>"commit",5424 hash=>$full_rev,5425 file_name=>$file_name)},5426 esc_html($short_rev));5427if($group_size>=2) {5428my@author_initials= ($author=~/\b([[:upper:]])\B/g);5429if(@author_initials) {5430print"<br />".5431 esc_html(join('',@author_initials));5432# or join('.', ...)5433}5434}5435print"</td>\n";5436}5437# 'previous' <sha1 of parent commit> <filename at commit>5438if(exists$meta->{'previous'} &&5439$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5440$meta->{'parent'} =$1;5441$meta->{'file_parent'} = unquote($2);5442}5443my$linenr_commit=5444exists($meta->{'parent'}) ?5445$meta->{'parent'} :$full_rev;5446my$linenr_filename=5447exists($meta->{'file_parent'}) ?5448$meta->{'file_parent'} : unquote($meta->{'filename'});5449my$blamed= href(action =>'blame',5450 file_name =>$linenr_filename,5451 hash_base =>$linenr_commit);5452print"<td class=\"linenr\">";5453print$cgi->a({ -href =>"$blamed#l$orig_lineno",5454-class=>"linenr"},5455 esc_html($lineno));5456print"</td>";5457print"<td class=\"pre\">". esc_html($data) ."</td>\n";5458print"</tr>\n";5459}# end while54605461}54625463# footer5464print"</tbody>\n".5465"</table>\n";# class="blame"5466print"</div>\n";# class="blame_body"5467close$fd5468or print"Reading blob failed\n";54695470 git_footer_html();5471}54725473sub git_blame {5474 git_blame_common();5475}54765477sub git_blame_incremental {5478 git_blame_common('incremental');5479}54805481sub git_blame_data {5482 git_blame_common('data');5483}54845485sub git_tags {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@tagslist= git_get_tags_list();5492if(@tagslist) {5493 git_tags_body(\@tagslist);5494}5495 git_footer_html();5496}54975498sub git_heads {5499my$head= git_get_head_hash($project);5500 git_header_html();5501 git_print_page_nav('','',$head,undef,$head);5502 git_print_header_div('summary',$project);55035504my@headslist= git_get_heads_list();5505if(@headslist) {5506 git_heads_body(\@headslist,$head);5507}5508 git_footer_html();5509}55105511sub git_blob_plain {5512my$type=shift;5513my$expires;55145515if(!defined$hash) {5516if(defined$file_name) {5517my$base=$hash_base|| git_get_head_hash($project);5518$hash= git_get_hash_by_path($base,$file_name,"blob")5519or die_error(404,"Cannot find file");5520}else{5521 die_error(400,"No file name defined");5522}5523}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5524# blobs defined by non-textual hash id's can be cached5525$expires="+1d";5526}55275528open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5529or die_error(500,"Open git-cat-file blob '$hash' failed");55305531# content-type (can include charset)5532$type= blob_contenttype($fd,$file_name,$type);55335534# "save as" filename, even when no $file_name is given5535my$save_as="$hash";5536if(defined$file_name) {5537$save_as=$file_name;5538}elsif($type=~m/^text\//) {5539$save_as.='.txt';5540}55415542# With XSS prevention on, blobs of all types except a few known safe5543# ones are served with "Content-Disposition: attachment" to make sure5544# they don't run in our security domain. For certain image types,5545# blob view writes an <img> tag referring to blob_plain view, and we5546# want to be sure not to break that by serving the image as an5547# attachment (though Firefox 3 doesn't seem to care).5548my$sandbox=$prevent_xss&&5549$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;55505551print$cgi->header(5552-type =>$type,5553-expires =>$expires,5554-content_disposition =>5555($sandbox?'attachment':'inline')5556.'; filename="'.$save_as.'"');5557local$/=undef;5558binmode STDOUT,':raw';5559print<$fd>;5560binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5561close$fd;5562}55635564sub git_blob {5565my$expires;55665567if(!defined$hash) {5568if(defined$file_name) {5569my$base=$hash_base|| git_get_head_hash($project);5570$hash= git_get_hash_by_path($base,$file_name,"blob")5571or die_error(404,"Cannot find file");5572}else{5573 die_error(400,"No file name defined");5574}5575}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5576# blobs defined by non-textual hash id's can be cached5577$expires="+1d";5578}55795580my$have_blame= gitweb_check_feature('blame');5581open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5582or die_error(500,"Couldn't cat$file_name,$hash");5583my$mimetype= blob_mimetype($fd,$file_name);5584# use 'blob_plain' (aka 'raw') view for files that cannot be displayed5585if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5586close$fd;5587return git_blob_plain($mimetype);5588}5589# we can have blame only for text/* mimetype5590$have_blame&&= ($mimetype=~m!^text/!);55915592my$highlight= gitweb_check_feature('highlight');5593my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);5594$fd= run_highlighter($fd,$highlight,$syntax)5595if$syntax;55965597 git_header_html(undef,$expires);5598my$formats_nav='';5599if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5600if(defined$file_name) {5601if($have_blame) {5602$formats_nav.=5603$cgi->a({-href => href(action=>"blame", -replay=>1)},5604"blame") .5605" | ";5606}5607$formats_nav.=5608$cgi->a({-href => href(action=>"history", -replay=>1)},5609"history") .5610" | ".5611$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5612"raw") .5613" | ".5614$cgi->a({-href => href(action=>"blob",5615 hash_base=>"HEAD", file_name=>$file_name)},5616"HEAD");5617}else{5618$formats_nav.=5619$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5620"raw");5621}5622 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5623 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5624}else{5625print"<div class=\"page_nav\">\n".5626"<br/><br/></div>\n".5627"<div class=\"title\">$hash</div>\n";5628}5629 git_print_page_path($file_name,"blob",$hash_base);5630print"<div class=\"page_body\">\n";5631if($mimetype=~m!^image/!) {5632print qq!<img type="$mimetype"!;5633if($file_name) {5634print qq! alt="$file_name" title="$file_name"!;5635}5636print qq! src="! .5637 href(action=>"blob_plain", hash=>$hash,5638 hash_base=>$hash_base, file_name=>$file_name) .5639 qq!"/>\n!;5640}else{5641my$nr;5642while(my$line= <$fd>) {5643chomp$line;5644$nr++;5645$line= untabify($line);5646printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,5647$nr, href(-replay =>1),$nr,$nr,$syntax?$line: esc_html($line, -nbsp=>1);5648}5649}5650close$fd5651or print"Reading blob failed.\n";5652print"</div>";5653 git_footer_html();5654}56555656sub git_tree {5657if(!defined$hash_base) {5658$hash_base="HEAD";5659}5660if(!defined$hash) {5661if(defined$file_name) {5662$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5663}else{5664$hash=$hash_base;5665}5666}5667 die_error(404,"No such tree")unlessdefined($hash);56685669my$show_sizes= gitweb_check_feature('show-sizes');5670my$have_blame= gitweb_check_feature('blame');56715672my@entries= ();5673{5674local$/="\0";5675open my$fd,"-|", git_cmd(),"ls-tree",'-z',5676($show_sizes?'-l': ()),@extra_options,$hash5677or die_error(500,"Open git-ls-tree failed");5678@entries=map{chomp;$_} <$fd>;5679close$fd5680or die_error(404,"Reading tree failed");5681}56825683my$refs= git_get_references();5684my$ref= format_ref_marker($refs,$hash_base);5685 git_header_html();5686my$basedir='';5687if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5688my@views_nav= ();5689if(defined$file_name) {5690push@views_nav,5691$cgi->a({-href => href(action=>"history", -replay=>1)},5692"history"),5693$cgi->a({-href => href(action=>"tree",5694 hash_base=>"HEAD", file_name=>$file_name)},5695"HEAD"),5696}5697my$snapshot_links= format_snapshot_links($hash);5698if(defined$snapshot_links) {5699# FIXME: Should be available when we have no hash base as well.5700push@views_nav,$snapshot_links;5701}5702 git_print_page_nav('tree','',$hash_base,undef,undef,5703join(' | ',@views_nav));5704 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5705}else{5706undef$hash_base;5707print"<div class=\"page_nav\">\n";5708print"<br/><br/></div>\n";5709print"<div class=\"title\">$hash</div>\n";5710}5711if(defined$file_name) {5712$basedir=$file_name;5713if($basedirne''&&substr($basedir, -1)ne'/') {5714$basedir.='/';5715}5716 git_print_page_path($file_name,'tree',$hash_base);5717}5718print"<div class=\"page_body\">\n";5719print"<table class=\"tree\">\n";5720my$alternate=1;5721# '..' (top directory) link if possible5722if(defined$hash_base&&5723defined$file_name&&$file_name=~m![^/]+$!) {5724if($alternate) {5725print"<tr class=\"dark\">\n";5726}else{5727print"<tr class=\"light\">\n";5728}5729$alternate^=1;57305731my$up=$file_name;5732$up=~s!/?[^/]+$!!;5733undef$upunless$up;5734# based on git_print_tree_entry5735print'<td class="mode">'. mode_str('040000') ."</td>\n";5736print'<td class="size"> </td>'."\n"if$show_sizes;5737print'<td class="list">';5738print$cgi->a({-href => href(action=>"tree",5739 hash_base=>$hash_base,5740 file_name=>$up)},5741"..");5742print"</td>\n";5743print"<td class=\"link\"></td>\n";57445745print"</tr>\n";5746}5747foreachmy$line(@entries) {5748my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);57495750if($alternate) {5751print"<tr class=\"dark\">\n";5752}else{5753print"<tr class=\"light\">\n";5754}5755$alternate^=1;57565757 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);57585759print"</tr>\n";5760}5761print"</table>\n".5762"</div>";5763 git_footer_html();5764}57655766sub snapshot_name {5767my($project,$hash) =@_;57685769# path/to/project.git -> project5770# path/to/project/.git -> project5771my$name= to_utf8($project);5772$name=~ s,([^/])/*\.git$,$1,;5773$name= basename($name);5774# sanitize name5775$name=~s/[[:cntrl:]]/?/g;57765777my$ver=$hash;5778if($hash=~/^[0-9a-fA-F]+$/) {5779# shorten SHA-1 hash5780my$full_hash= git_get_full_hash($project,$hash);5781if($full_hash=~/^$hash/&&length($hash) >7) {5782$ver= git_get_short_hash($project,$hash);5783}5784}elsif($hash=~m!^refs/tags/(.*)$!) {5785# tags don't need shortened SHA-1 hash5786$ver=$1;5787}else{5788# branches and other need shortened SHA-1 hash5789if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5790$ver=$1;5791}5792$ver.='-'. git_get_short_hash($project,$hash);5793}5794# in case of hierarchical branch names5795$ver=~s!/!.!g;57965797# name = project-version_string5798$name="$name-$ver";57995800returnwantarray? ($name,$name) :$name;5801}58025803sub git_snapshot {5804my$format=$input_params{'snapshot_format'};5805if(!@snapshot_fmts) {5806 die_error(403,"Snapshots not allowed");5807}5808# default to first supported snapshot format5809$format||=$snapshot_fmts[0];5810if($format!~m/^[a-z0-9]+$/) {5811 die_error(400,"Invalid snapshot format parameter");5812}elsif(!exists($known_snapshot_formats{$format})) {5813 die_error(400,"Unknown snapshot format");5814}elsif($known_snapshot_formats{$format}{'disabled'}) {5815 die_error(403,"Snapshot format not allowed");5816}elsif(!grep($_eq$format,@snapshot_fmts)) {5817 die_error(403,"Unsupported snapshot format");5818}58195820my$type= git_get_type("$hash^{}");5821if(!$type) {5822 die_error(404,'Object does not exist');5823}elsif($typeeq'blob') {5824 die_error(400,'Object is not a tree-ish');5825}58265827my($name,$prefix) = snapshot_name($project,$hash);5828my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5829my$cmd= quote_command(5830 git_cmd(),'archive',5831"--format=$known_snapshot_formats{$format}{'format'}",5832"--prefix=$prefix/",$hash);5833if(exists$known_snapshot_formats{$format}{'compressor'}) {5834$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5835}58365837$filename=~s/(["\\])/\\$1/g;5838print$cgi->header(5839-type =>$known_snapshot_formats{$format}{'type'},5840-content_disposition =>'inline; filename="'.$filename.'"',5841-status =>'200 OK');58425843open my$fd,"-|",$cmd5844or die_error(500,"Execute git-archive failed");5845binmode STDOUT,':raw';5846print<$fd>;5847binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5848close$fd;5849}58505851sub git_log_generic {5852my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;58535854my$head= git_get_head_hash($project);5855if(!defined$base) {5856$base=$head;5857}5858if(!defined$page) {5859$page=0;5860}5861my$refs= git_get_references();58625863my$commit_hash=$base;5864if(defined$parent) {5865$commit_hash="$parent..$base";5866}5867my@commitlist=5868 parse_commits($commit_hash,101, (100*$page),5869defined$file_name? ($file_name,"--full-history") : ());58705871my$ftype;5872if(!defined$file_hash&&defined$file_name) {5873# some commits could have deleted file in question,5874# and not have it in tree, but one of them has to have it5875for(my$i=0;$i<@commitlist;$i++) {5876$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5877last ifdefined$file_hash;5878}5879}5880if(defined$file_hash) {5881$ftype= git_get_type($file_hash);5882}5883if(defined$file_name&& !defined$ftype) {5884 die_error(500,"Unknown type of object");5885}5886my%co;5887if(defined$file_name) {5888%co= parse_commit($base)5889or die_error(404,"Unknown commit object");5890}589158925893my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5894my$next_link='';5895if($#commitlist>=100) {5896$next_link=5897$cgi->a({-href => href(-replay=>1, page=>$page+1),5898-accesskey =>"n", -title =>"Alt-n"},"next");5899}5900my$patch_max= gitweb_get_feature('patches');5901if($patch_max&& !defined$file_name) {5902if($patch_max<0||@commitlist<=$patch_max) {5903$paging_nav.=" ⋅ ".5904$cgi->a({-href => href(action=>"patches", -replay=>1)},5905"patches");5906}5907}59085909 git_header_html();5910 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5911if(defined$file_name) {5912 git_print_header_div('commit', esc_html($co{'title'}),$base);5913}else{5914 git_print_header_div('summary',$project)5915}5916 git_print_page_path($file_name,$ftype,$hash_base)5917if(defined$file_name);59185919$body_subr->(\@commitlist,0,99,$refs,$next_link,5920$file_name,$file_hash,$ftype);59215922 git_footer_html();5923}59245925sub git_log {5926 git_log_generic('log', \&git_log_body,5927$hash,$hash_parent);5928}59295930sub git_commit {5931$hash||=$hash_base||"HEAD";5932my%co= parse_commit($hash)5933or die_error(404,"Unknown commit object");59345935my$parent=$co{'parent'};5936my$parents=$co{'parents'};# listref59375938# we need to prepare $formats_nav before any parameter munging5939my$formats_nav;5940if(!defined$parent) {5941# --root commitdiff5942$formats_nav.='(initial)';5943}elsif(@$parents==1) {5944# single parent commit5945$formats_nav.=5946'(parent: '.5947$cgi->a({-href => href(action=>"commit",5948 hash=>$parent)},5949 esc_html(substr($parent,0,7))) .5950')';5951}else{5952# merge commit5953$formats_nav.=5954'(merge: '.5955join(' ',map{5956$cgi->a({-href => href(action=>"commit",5957 hash=>$_)},5958 esc_html(substr($_,0,7)));5959}@$parents) .5960')';5961}5962if(gitweb_check_feature('patches') &&@$parents<=1) {5963$formats_nav.=" | ".5964$cgi->a({-href => href(action=>"patch", -replay=>1)},5965"patch");5966}59675968if(!defined$parent) {5969$parent="--root";5970}5971my@difftree;5972open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5973@diff_opts,5974(@$parents<=1?$parent:'-c'),5975$hash,"--"5976or die_error(500,"Open git-diff-tree failed");5977@difftree=map{chomp;$_} <$fd>;5978close$fdor die_error(404,"Reading git-diff-tree failed");59795980# non-textual hash id's can be cached5981my$expires;5982if($hash=~m/^[0-9a-fA-F]{40}$/) {5983$expires="+1d";5984}5985my$refs= git_get_references();5986my$ref= format_ref_marker($refs,$co{'id'});59875988 git_header_html(undef,$expires);5989 git_print_page_nav('commit','',5990$hash,$co{'tree'},$hash,5991$formats_nav);59925993if(defined$co{'parent'}) {5994 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5995}else{5996 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5997}5998print"<div class=\"title_text\">\n".5999"<table class=\"object_header\">\n";6000 git_print_authorship_rows(\%co);6001print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";6002print"<tr>".6003"<td>tree</td>".6004"<td class=\"sha1\">".6005$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),6006class=>"list"},$co{'tree'}) .6007"</td>".6008"<td class=\"link\">".6009$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},6010"tree");6011my$snapshot_links= format_snapshot_links($hash);6012if(defined$snapshot_links) {6013print" | ".$snapshot_links;6014}6015print"</td>".6016"</tr>\n";60176018foreachmy$par(@$parents) {6019print"<tr>".6020"<td>parent</td>".6021"<td class=\"sha1\">".6022$cgi->a({-href => href(action=>"commit", hash=>$par),6023class=>"list"},$par) .6024"</td>".6025"<td class=\"link\">".6026$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .6027" | ".6028$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .6029"</td>".6030"</tr>\n";6031}6032print"</table>".6033"</div>\n";60346035print"<div class=\"page_body\">\n";6036 git_print_log($co{'comment'});6037print"</div>\n";60386039 git_difftree_body(\@difftree,$hash,@$parents);60406041 git_footer_html();6042}60436044sub git_object {6045# object is defined by:6046# - hash or hash_base alone6047# - hash_base and file_name6048my$type;60496050# - hash or hash_base alone6051if($hash|| ($hash_base&& !defined$file_name)) {6052my$object_id=$hash||$hash_base;60536054open my$fd,"-|", quote_command(6055 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'6056or die_error(404,"Object does not exist");6057$type= <$fd>;6058chomp$type;6059close$fd6060or die_error(404,"Object does not exist");60616062# - hash_base and file_name6063}elsif($hash_base&&defined$file_name) {6064$file_name=~ s,/+$,,;60656066system(git_cmd(),"cat-file",'-e',$hash_base) ==06067or die_error(404,"Base object does not exist");60686069# here errors should not hapen6070open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name6071or die_error(500,"Open git-ls-tree failed");6072my$line= <$fd>;6073close$fd;60746075#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'6076unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {6077 die_error(404,"File or directory for given base does not exist");6078}6079$type=$2;6080$hash=$3;6081}else{6082 die_error(400,"Not enough information to find object");6083}60846085print$cgi->redirect(-uri => href(action=>$type, -full=>1,6086 hash=>$hash, hash_base=>$hash_base,6087 file_name=>$file_name),6088-status =>'302 Found');6089}60906091sub git_blobdiff {6092my$format=shift||'html';60936094my$fd;6095my@difftree;6096my%diffinfo;6097my$expires;60986099# preparing $fd and %diffinfo for git_patchset_body6100# new style URI6101if(defined$hash_base&&defined$hash_parent_base) {6102if(defined$file_name) {6103# read raw output6104open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6105$hash_parent_base,$hash_base,6106"--", (defined$file_parent?$file_parent: ()),$file_name6107or die_error(500,"Open git-diff-tree failed");6108@difftree=map{chomp;$_} <$fd>;6109close$fd6110or die_error(404,"Reading git-diff-tree failed");6111@difftree6112or die_error(404,"Blob diff not found");61136114}elsif(defined$hash&&6115$hash=~/[0-9a-fA-F]{40}/) {6116# try to find filename from $hash61176118# read filtered raw output6119open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6120$hash_parent_base,$hash_base,"--"6121or die_error(500,"Open git-diff-tree failed");6122@difftree=6123# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'6124# $hash == to_id6125grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}6126map{chomp;$_} <$fd>;6127close$fd6128or die_error(404,"Reading git-diff-tree failed");6129@difftree6130or die_error(404,"Blob diff not found");61316132}else{6133 die_error(400,"Missing one of the blob diff parameters");6134}61356136if(@difftree>1) {6137 die_error(400,"Ambiguous blob diff specification");6138}61396140%diffinfo= parse_difftree_raw_line($difftree[0]);6141$file_parent||=$diffinfo{'from_file'} ||$file_name;6142$file_name||=$diffinfo{'to_file'};61436144$hash_parent||=$diffinfo{'from_id'};6145$hash||=$diffinfo{'to_id'};61466147# non-textual hash id's can be cached6148if($hash_base=~m/^[0-9a-fA-F]{40}$/&&6149$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {6150$expires='+1d';6151}61526153# open patch output6154open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6155'-p', ($formateq'html'?"--full-index": ()),6156$hash_parent_base,$hash_base,6157"--", (defined$file_parent?$file_parent: ()),$file_name6158or die_error(500,"Open git-diff-tree failed");6159}61606161# old/legacy style URI -- not generated anymore since 1.4.3.6162if(!%diffinfo) {6163 die_error('404 Not Found',"Missing one of the blob diff parameters")6164}61656166# header6167if($formateq'html') {6168my$formats_nav=6169$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},6170"raw");6171 git_header_html(undef,$expires);6172if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6173 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6174 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6175}else{6176print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";6177print"<div class=\"title\">$hashvs$hash_parent</div>\n";6178}6179if(defined$file_name) {6180 git_print_page_path($file_name,"blob",$hash_base);6181}else{6182print"<div class=\"page_path\"></div>\n";6183}61846185}elsif($formateq'plain') {6186print$cgi->header(6187-type =>'text/plain',6188-charset =>'utf-8',6189-expires =>$expires,6190-content_disposition =>'inline; filename="'."$file_name".'.patch"');61916192print"X-Git-Url: ".$cgi->self_url() ."\n\n";61936194}else{6195 die_error(400,"Unknown blobdiff format");6196}61976198# patch6199if($formateq'html') {6200print"<div class=\"page_body\">\n";62016202 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);6203close$fd;62046205print"</div>\n";# class="page_body"6206 git_footer_html();62076208}else{6209while(my$line= <$fd>) {6210$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;6211$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;62126213print$line;62146215last if$line=~m!^\+\+\+!;6216}6217local$/=undef;6218print<$fd>;6219close$fd;6220}6221}62226223sub git_blobdiff_plain {6224 git_blobdiff('plain');6225}62266227sub git_commitdiff {6228my%params=@_;6229my$format=$params{-format} ||'html';62306231my($patch_max) = gitweb_get_feature('patches');6232if($formateq'patch') {6233 die_error(403,"Patch view not allowed")unless$patch_max;6234}62356236$hash||=$hash_base||"HEAD";6237my%co= parse_commit($hash)6238or die_error(404,"Unknown commit object");62396240# choose format for commitdiff for merge6241if(!defined$hash_parent&& @{$co{'parents'}} >1) {6242$hash_parent='--cc';6243}6244# we need to prepare $formats_nav before almost any parameter munging6245my$formats_nav;6246if($formateq'html') {6247$formats_nav=6248$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6249"raw");6250if($patch_max&& @{$co{'parents'}} <=1) {6251$formats_nav.=" | ".6252$cgi->a({-href => href(action=>"patch", -replay=>1)},6253"patch");6254}62556256if(defined$hash_parent&&6257$hash_parentne'-c'&&$hash_parentne'--cc') {6258# commitdiff with two commits given6259my$hash_parent_short=$hash_parent;6260if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6261$hash_parent_short=substr($hash_parent,0,7);6262}6263$formats_nav.=6264' (from';6265for(my$i=0;$i< @{$co{'parents'}};$i++) {6266if($co{'parents'}[$i]eq$hash_parent) {6267$formats_nav.=' parent '. ($i+1);6268last;6269}6270}6271$formats_nav.=': '.6272$cgi->a({-href => href(action=>"commitdiff",6273 hash=>$hash_parent)},6274 esc_html($hash_parent_short)) .6275')';6276}elsif(!$co{'parent'}) {6277# --root commitdiff6278$formats_nav.=' (initial)';6279}elsif(scalar@{$co{'parents'}} ==1) {6280# single parent commit6281$formats_nav.=6282' (parent: '.6283$cgi->a({-href => href(action=>"commitdiff",6284 hash=>$co{'parent'})},6285 esc_html(substr($co{'parent'},0,7))) .6286')';6287}else{6288# merge commit6289if($hash_parenteq'--cc') {6290$formats_nav.=' | '.6291$cgi->a({-href => href(action=>"commitdiff",6292 hash=>$hash, hash_parent=>'-c')},6293'combined');6294}else{# $hash_parent eq '-c'6295$formats_nav.=' | '.6296$cgi->a({-href => href(action=>"commitdiff",6297 hash=>$hash, hash_parent=>'--cc')},6298'compact');6299}6300$formats_nav.=6301' (merge: '.6302join(' ',map{6303$cgi->a({-href => href(action=>"commitdiff",6304 hash=>$_)},6305 esc_html(substr($_,0,7)));6306} @{$co{'parents'}} ) .6307')';6308}6309}63106311my$hash_parent_param=$hash_parent;6312if(!defined$hash_parent_param) {6313# --cc for multiple parents, --root for parentless6314$hash_parent_param=6315@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6316}63176318# read commitdiff6319my$fd;6320my@difftree;6321if($formateq'html') {6322open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6323"--no-commit-id","--patch-with-raw","--full-index",6324$hash_parent_param,$hash,"--"6325or die_error(500,"Open git-diff-tree failed");63266327while(my$line= <$fd>) {6328chomp$line;6329# empty line ends raw part of diff-tree output6330last unless$line;6331push@difftree,scalar parse_difftree_raw_line($line);6332}63336334}elsif($formateq'plain') {6335open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6336'-p',$hash_parent_param,$hash,"--"6337or die_error(500,"Open git-diff-tree failed");6338}elsif($formateq'patch') {6339# For commit ranges, we limit the output to the number of6340# patches specified in the 'patches' feature.6341# For single commits, we limit the output to a single patch,6342# diverging from the git-format-patch default.6343my@commit_spec= ();6344if($hash_parent) {6345if($patch_max>0) {6346push@commit_spec,"-$patch_max";6347}6348push@commit_spec,'-n',"$hash_parent..$hash";6349}else{6350if($params{-single}) {6351push@commit_spec,'-1';6352}else{6353if($patch_max>0) {6354push@commit_spec,"-$patch_max";6355}6356push@commit_spec,"-n";6357}6358push@commit_spec,'--root',$hash;6359}6360open$fd,"-|", git_cmd(),"format-patch",@diff_opts,6361'--encoding=utf8','--stdout',@commit_spec6362or die_error(500,"Open git-format-patch failed");6363}else{6364 die_error(400,"Unknown commitdiff format");6365}63666367# non-textual hash id's can be cached6368my$expires;6369if($hash=~m/^[0-9a-fA-F]{40}$/) {6370$expires="+1d";6371}63726373# write commit message6374if($formateq'html') {6375my$refs= git_get_references();6376my$ref= format_ref_marker($refs,$co{'id'});63776378 git_header_html(undef,$expires);6379 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6380 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6381print"<div class=\"title_text\">\n".6382"<table class=\"object_header\">\n";6383 git_print_authorship_rows(\%co);6384print"</table>".6385"</div>\n";6386print"<div class=\"page_body\">\n";6387if(@{$co{'comment'}} >1) {6388print"<div class=\"log\">\n";6389 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6390print"</div>\n";# class="log"6391}63926393}elsif($formateq'plain') {6394my$refs= git_get_references("tags");6395my$tagname= git_get_rev_name_tags($hash);6396my$filename= basename($project) ."-$hash.patch";63976398print$cgi->header(6399-type =>'text/plain',6400-charset =>'utf-8',6401-expires =>$expires,6402-content_disposition =>'inline; filename="'."$filename".'"');6403my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6404print"From: ". to_utf8($co{'author'}) ."\n";6405print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6406print"Subject: ". to_utf8($co{'title'}) ."\n";64076408print"X-Git-Tag:$tagname\n"if$tagname;6409print"X-Git-Url: ".$cgi->self_url() ."\n\n";64106411foreachmy$line(@{$co{'comment'}}) {6412print to_utf8($line) ."\n";6413}6414print"---\n\n";6415}elsif($formateq'patch') {6416my$filename= basename($project) ."-$hash.patch";64176418print$cgi->header(6419-type =>'text/plain',6420-charset =>'utf-8',6421-expires =>$expires,6422-content_disposition =>'inline; filename="'."$filename".'"');6423}64246425# write patch6426if($formateq'html') {6427my$use_parents= !defined$hash_parent||6428$hash_parenteq'-c'||$hash_parenteq'--cc';6429 git_difftree_body(\@difftree,$hash,6430$use_parents? @{$co{'parents'}} :$hash_parent);6431print"<br/>\n";64326433 git_patchset_body($fd, \@difftree,$hash,6434$use_parents? @{$co{'parents'}} :$hash_parent);6435close$fd;6436print"</div>\n";# class="page_body"6437 git_footer_html();64386439}elsif($formateq'plain') {6440local$/=undef;6441print<$fd>;6442close$fd6443or print"Reading git-diff-tree failed\n";6444}elsif($formateq'patch') {6445local$/=undef;6446print<$fd>;6447close$fd6448or print"Reading git-format-patch failed\n";6449}6450}64516452sub git_commitdiff_plain {6453 git_commitdiff(-format =>'plain');6454}64556456# format-patch-style patches6457sub git_patch {6458 git_commitdiff(-format =>'patch', -single =>1);6459}64606461sub git_patches {6462 git_commitdiff(-format =>'patch');6463}64646465sub git_history {6466 git_log_generic('history', \&git_history_body,6467$hash_base,$hash_parent_base,6468$file_name,$hash);6469}64706471sub git_search {6472 gitweb_check_feature('search')or die_error(403,"Search is disabled");6473if(!defined$searchtext) {6474 die_error(400,"Text field is empty");6475}6476if(!defined$hash) {6477$hash= git_get_head_hash($project);6478}6479my%co= parse_commit($hash);6480if(!%co) {6481 die_error(404,"Unknown commit object");6482}6483if(!defined$page) {6484$page=0;6485}64866487$searchtype||='commit';6488if($searchtypeeq'pickaxe') {6489# pickaxe may take all resources of your box and run for several minutes6490# with every query - so decide by yourself how public you make this feature6491 gitweb_check_feature('pickaxe')6492or die_error(403,"Pickaxe is disabled");6493}6494if($searchtypeeq'grep') {6495 gitweb_check_feature('grep')6496or die_error(403,"Grep is disabled");6497}64986499 git_header_html();65006501if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6502my$greptype;6503if($searchtypeeq'commit') {6504$greptype="--grep=";6505}elsif($searchtypeeq'author') {6506$greptype="--author=";6507}elsif($searchtypeeq'committer') {6508$greptype="--committer=";6509}6510$greptype.=$searchtext;6511my@commitlist= parse_commits($hash,101, (100*$page),undef,6512$greptype,'--regexp-ignore-case',6513$search_use_regexp?'--extended-regexp':'--fixed-strings');65146515my$paging_nav='';6516if($page>0) {6517$paging_nav.=6518$cgi->a({-href => href(action=>"search", hash=>$hash,6519 searchtext=>$searchtext,6520 searchtype=>$searchtype)},6521"first");6522$paging_nav.=" ⋅ ".6523$cgi->a({-href => href(-replay=>1, page=>$page-1),6524-accesskey =>"p", -title =>"Alt-p"},"prev");6525}else{6526$paging_nav.="first";6527$paging_nav.=" ⋅ prev";6528}6529my$next_link='';6530if($#commitlist>=100) {6531$next_link=6532$cgi->a({-href => href(-replay=>1, page=>$page+1),6533-accesskey =>"n", -title =>"Alt-n"},"next");6534$paging_nav.=" ⋅$next_link";6535}else{6536$paging_nav.=" ⋅ next";6537}65386539 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6540 git_print_header_div('commit', esc_html($co{'title'}),$hash);6541if($page==0&& !@commitlist) {6542print"<p>No match.</p>\n";6543}else{6544 git_search_grep_body(\@commitlist,0,99,$next_link);6545}6546}65476548if($searchtypeeq'pickaxe') {6549 git_print_page_nav('','',$hash,$co{'tree'},$hash);6550 git_print_header_div('commit', esc_html($co{'title'}),$hash);65516552print"<table class=\"pickaxe search\">\n";6553my$alternate=1;6554local$/="\n";6555open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6556'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6557($search_use_regexp?'--pickaxe-regex': ());6558undef%co;6559my@files;6560while(my$line= <$fd>) {6561chomp$line;6562next unless$line;65636564my%set= parse_difftree_raw_line($line);6565if(defined$set{'commit'}) {6566# finish previous commit6567if(%co) {6568print"</td>\n".6569"<td class=\"link\">".6570$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6571" | ".6572$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6573print"</td>\n".6574"</tr>\n";6575}65766577if($alternate) {6578print"<tr class=\"dark\">\n";6579}else{6580print"<tr class=\"light\">\n";6581}6582$alternate^=1;6583%co= parse_commit($set{'commit'});6584my$author= chop_and_escape_str($co{'author_name'},15,5);6585print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6586"<td><i>$author</i></td>\n".6587"<td>".6588$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6589-class=>"list subject"},6590 chop_and_escape_str($co{'title'},50) ."<br/>");6591}elsif(defined$set{'to_id'}) {6592next if($set{'to_id'} =~m/^0{40}$/);65936594print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6595 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6596-class=>"list"},6597"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6598"<br/>\n";6599}6600}6601close$fd;66026603# finish last commit (warning: repetition!)6604if(%co) {6605print"</td>\n".6606"<td class=\"link\">".6607$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6608" | ".6609$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6610print"</td>\n".6611"</tr>\n";6612}66136614print"</table>\n";6615}66166617if($searchtypeeq'grep') {6618 git_print_page_nav('','',$hash,$co{'tree'},$hash);6619 git_print_header_div('commit', esc_html($co{'title'}),$hash);66206621print"<table class=\"grep_search\">\n";6622my$alternate=1;6623my$matches=0;6624local$/="\n";6625open my$fd,"-|", git_cmd(),'grep','-n',6626$search_use_regexp? ('-E','-i') :'-F',6627$searchtext,$co{'tree'};6628my$lastfile='';6629while(my$line= <$fd>) {6630chomp$line;6631my($file,$lno,$ltext,$binary);6632last if($matches++>1000);6633if($line=~/^Binary file (.+) matches$/) {6634$file=$1;6635$binary=1;6636}else{6637(undef,$file,$lno,$ltext) =split(/:/,$line,4);6638}6639if($filene$lastfile) {6640$lastfileand print"</td></tr>\n";6641if($alternate++) {6642print"<tr class=\"dark\">\n";6643}else{6644print"<tr class=\"light\">\n";6645}6646print"<td class=\"list\">".6647$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6648 file_name=>"$file"),6649-class=>"list"}, esc_path($file));6650print"</td><td>\n";6651$lastfile=$file;6652}6653if($binary) {6654print"<div class=\"binary\">Binary file</div>\n";6655}else{6656$ltext= untabify($ltext);6657if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6658$ltext= esc_html($1, -nbsp=>1);6659$ltext.='<span class="match">';6660$ltext.= esc_html($2, -nbsp=>1);6661$ltext.='</span>';6662$ltext.= esc_html($3, -nbsp=>1);6663}else{6664$ltext= esc_html($ltext, -nbsp=>1);6665}6666print"<div class=\"pre\">".6667$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6668 file_name=>"$file").'#l'.$lno,6669-class=>"linenr"},sprintf('%4i',$lno))6670.' '.$ltext."</div>\n";6671}6672}6673if($lastfile) {6674print"</td></tr>\n";6675if($matches>1000) {6676print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6677}6678}else{6679print"<div class=\"diff nodifferences\">No matches found</div>\n";6680}6681close$fd;66826683print"</table>\n";6684}6685 git_footer_html();6686}66876688sub git_search_help {6689 git_header_html();6690 git_print_page_nav('','',$hash,$hash,$hash);6691print<<EOT;6692<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6693regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6694the pattern entered is recognized as the POSIX extended6695<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6696insensitive).</p>6697<dl>6698<dt><b>commit</b></dt>6699<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6700EOT6701my$have_grep= gitweb_check_feature('grep');6702if($have_grep) {6703print<<EOT;6704<dt><b>grep</b></dt>6705<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6706 a different one) are searched for the given pattern. On large trees, this search can take6707a while and put some strain on the server, so please use it with some consideration. Note that6708due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6709case-sensitive.</dd>6710EOT6711}6712print<<EOT;6713<dt><b>author</b></dt>6714<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6715<dt><b>committer</b></dt>6716<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6717EOT6718my$have_pickaxe= gitweb_check_feature('pickaxe');6719if($have_pickaxe) {6720print<<EOT;6721<dt><b>pickaxe</b></dt>6722<dd>All commits that caused the string to appear or disappear from any file (changes that6723added, removed or "modified" the string) will be listed. This search can take a while and6724takes a lot of strain on the server, so please use it wisely. Note that since you may be6725interested even in changes just changing the case as well, this search is case sensitive.</dd>6726EOT6727}6728print"</dl>\n";6729 git_footer_html();6730}67316732sub git_shortlog {6733 git_log_generic('shortlog', \&git_shortlog_body,6734$hash,$hash_parent);6735}67366737## ......................................................................6738## feeds (RSS, Atom; OPML)67396740sub git_feed {6741my$format=shift||'atom';6742my$have_blame= gitweb_check_feature('blame');67436744# Atom: http://www.atomenabled.org/developers/syndication/6745# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6746if($formatne'rss'&&$formatne'atom') {6747 die_error(400,"Unknown web feed format");6748}67496750# log/feed of current (HEAD) branch, log of given branch, history of file/directory6751my$head=$hash||'HEAD';6752my@commitlist= parse_commits($head,150,0,$file_name);67536754my%latest_commit;6755my%latest_date;6756my$content_type="application/$format+xml";6757if(defined$cgi->http('HTTP_ACCEPT') &&6758$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6759# browser (feed reader) prefers text/xml6760$content_type='text/xml';6761}6762if(defined($commitlist[0])) {6763%latest_commit= %{$commitlist[0]};6764my$latest_epoch=$latest_commit{'committer_epoch'};6765%latest_date= parse_date($latest_epoch);6766my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6767if(defined$if_modified) {6768my$since;6769if(eval{require HTTP::Date;1; }) {6770$since= HTTP::Date::str2time($if_modified);6771}elsif(eval{require Time::ParseDate;1; }) {6772$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6773}6774if(defined$since&&$latest_epoch<=$since) {6775print$cgi->header(6776-type =>$content_type,6777-charset =>'utf-8',6778-last_modified =>$latest_date{'rfc2822'},6779-status =>'304 Not Modified');6780return;6781}6782}6783print$cgi->header(6784-type =>$content_type,6785-charset =>'utf-8',6786-last_modified =>$latest_date{'rfc2822'});6787}else{6788print$cgi->header(6789-type =>$content_type,6790-charset =>'utf-8');6791}67926793# Optimization: skip generating the body if client asks only6794# for Last-Modified date.6795return if($cgi->request_method()eq'HEAD');67966797# header variables6798my$title="$site_name-$project/$action";6799my$feed_type='log';6800if(defined$hash) {6801$title.=" - '$hash'";6802$feed_type='branch log';6803if(defined$file_name) {6804$title.=" ::$file_name";6805$feed_type='history';6806}6807}elsif(defined$file_name) {6808$title.=" -$file_name";6809$feed_type='history';6810}6811$title.="$feed_type";6812my$descr= git_get_project_description($project);6813if(defined$descr) {6814$descr= esc_html($descr);6815}else{6816$descr="$project".6817($formateq'rss'?'RSS':'Atom') .6818" feed";6819}6820my$owner= git_get_project_owner($project);6821$owner= esc_html($owner);68226823#header6824my$alt_url;6825if(defined$file_name) {6826$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6827}elsif(defined$hash) {6828$alt_url= href(-full=>1, action=>"log", hash=>$hash);6829}else{6830$alt_url= href(-full=>1, action=>"summary");6831}6832print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6833if($formateq'rss') {6834print<<XML;6835<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6836<channel>6837XML6838print"<title>$title</title>\n".6839"<link>$alt_url</link>\n".6840"<description>$descr</description>\n".6841"<language>en</language>\n".6842# project owner is responsible for 'editorial' content6843"<managingEditor>$owner</managingEditor>\n";6844if(defined$logo||defined$favicon) {6845# prefer the logo to the favicon, since RSS6846# doesn't allow both6847my$img= esc_url($logo||$favicon);6848print"<image>\n".6849"<url>$img</url>\n".6850"<title>$title</title>\n".6851"<link>$alt_url</link>\n".6852"</image>\n";6853}6854if(%latest_date) {6855print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6856print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6857}6858print"<generator>gitweb v.$version/$git_version</generator>\n";6859}elsif($formateq'atom') {6860print<<XML;6861<feed xmlns="http://www.w3.org/2005/Atom">6862XML6863print"<title>$title</title>\n".6864"<subtitle>$descr</subtitle>\n".6865'<link rel="alternate" type="text/html" href="'.6866$alt_url.'" />'."\n".6867'<link rel="self" type="'.$content_type.'" href="'.6868$cgi->self_url() .'" />'."\n".6869"<id>". href(-full=>1) ."</id>\n".6870# use project owner for feed author6871"<author><name>$owner</name></author>\n";6872if(defined$favicon) {6873print"<icon>". esc_url($favicon) ."</icon>\n";6874}6875if(defined$logo_url) {6876# not twice as wide as tall: 72 x 27 pixels6877print"<logo>". esc_url($logo) ."</logo>\n";6878}6879if(!%latest_date) {6880# dummy date to keep the feed valid until commits trickle in:6881print"<updated>1970-01-01T00:00:00Z</updated>\n";6882}else{6883print"<updated>$latest_date{'iso-8601'}</updated>\n";6884}6885print"<generator version='$version/$git_version'>gitweb</generator>\n";6886}68876888# contents6889for(my$i=0;$i<=$#commitlist;$i++) {6890my%co= %{$commitlist[$i]};6891my$commit=$co{'id'};6892# we read 150, we always show 30 and the ones more recent than 48 hours6893if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6894last;6895}6896my%cd= parse_date($co{'author_epoch'});68976898# get list of changed files6899open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6900$co{'parent'} ||"--root",6901$co{'id'},"--", (defined$file_name?$file_name: ())6902ornext;6903my@difftree=map{chomp;$_} <$fd>;6904close$fd6905ornext;69066907# print element (entry, item)6908my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6909if($formateq'rss') {6910print"<item>\n".6911"<title>". esc_html($co{'title'}) ."</title>\n".6912"<author>". esc_html($co{'author'}) ."</author>\n".6913"<pubDate>$cd{'rfc2822'}</pubDate>\n".6914"<guid isPermaLink=\"true\">$co_url</guid>\n".6915"<link>$co_url</link>\n".6916"<description>". esc_html($co{'title'}) ."</description>\n".6917"<content:encoded>".6918"<![CDATA[\n";6919}elsif($formateq'atom') {6920print"<entry>\n".6921"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6922"<updated>$cd{'iso-8601'}</updated>\n".6923"<author>\n".6924" <name>". esc_html($co{'author_name'}) ."</name>\n";6925if($co{'author_email'}) {6926print" <email>". esc_html($co{'author_email'}) ."</email>\n";6927}6928print"</author>\n".6929# use committer for contributor6930"<contributor>\n".6931" <name>". esc_html($co{'committer_name'}) ."</name>\n";6932if($co{'committer_email'}) {6933print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6934}6935print"</contributor>\n".6936"<published>$cd{'iso-8601'}</published>\n".6937"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6938"<id>$co_url</id>\n".6939"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6940"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6941}6942my$comment=$co{'comment'};6943print"<pre>\n";6944foreachmy$line(@$comment) {6945$line= esc_html($line);6946print"$line\n";6947}6948print"</pre><ul>\n";6949foreachmy$difftree_line(@difftree) {6950my%difftree= parse_difftree_raw_line($difftree_line);6951next if!$difftree{'from_id'};69526953my$file=$difftree{'file'} ||$difftree{'to_file'};69546955print"<li>".6956"[".6957$cgi->a({-href => href(-full=>1, action=>"blobdiff",6958 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6959 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6960 file_name=>$file, file_parent=>$difftree{'from_file'}),6961-title =>"diff"},'D');6962if($have_blame) {6963print$cgi->a({-href => href(-full=>1, action=>"blame",6964 file_name=>$file, hash_base=>$commit),6965-title =>"blame"},'B');6966}6967# if this is not a feed of a file history6968if(!defined$file_name||$file_namene$file) {6969print$cgi->a({-href => href(-full=>1, action=>"history",6970 file_name=>$file, hash=>$commit),6971-title =>"history"},'H');6972}6973$file= esc_path($file);6974print"] ".6975"$file</li>\n";6976}6977if($formateq'rss') {6978print"</ul>]]>\n".6979"</content:encoded>\n".6980"</item>\n";6981}elsif($formateq'atom') {6982print"</ul>\n</div>\n".6983"</content>\n".6984"</entry>\n";6985}6986}69876988# end of feed6989if($formateq'rss') {6990print"</channel>\n</rss>\n";6991}elsif($formateq'atom') {6992print"</feed>\n";6993}6994}69956996sub git_rss {6997 git_feed('rss');6998}69997000sub git_atom {7001 git_feed('atom');7002}70037004sub git_opml {7005my@list= git_get_projects_list();70067007print$cgi->header(7008-type =>'text/xml',7009-charset =>'utf-8',7010-content_disposition =>'inline; filename="opml.xml"');70117012print<<XML;7013<?xml version="1.0" encoding="utf-8"?>7014<opml version="1.0">7015<head>7016 <title>$site_nameOPML Export</title>7017</head>7018<body>7019<outline text="git RSS feeds">7020XML70217022foreachmy$pr(@list) {7023my%proj=%$pr;7024my$head= git_get_head_hash($proj{'path'});7025if(!defined$head) {7026next;7027}7028$git_dir="$projectroot/$proj{'path'}";7029my%co= parse_commit($head);7030if(!%co) {7031next;7032}70337034my$path= esc_html(chop_str($proj{'path'},25,5));7035my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);7036my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);7037print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";7038}7039print<<XML;7040</outline>7041</body>7042</opml>7043XML7044}