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); 20use Time::HiRes qw(gettimeofday tv_interval); 21binmode STDOUT,':utf8'; 22 23our$t0= [ gettimeofday() ]; 24our$number_of_git_cmds=0; 25 26BEGIN{ 27 CGI->compile()if$ENV{'MOD_PERL'}; 28} 29 30our$version="++GIT_VERSION++"; 31 32our($my_url,$my_uri,$base_url,$path_info,$home_link); 33sub evaluate_uri { 34our$cgi; 35 36our$my_url=$cgi->url(); 37our$my_uri=$cgi->url(-absolute =>1); 38 39# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 40# needed and used only for URLs with nonempty PATH_INFO 41our$base_url=$my_url; 42 43# When the script is used as DirectoryIndex, the URL does not contain the name 44# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 45# have to do it ourselves. We make $path_info global because it's also used 46# later on. 47# 48# Another issue with the script being the DirectoryIndex is that the resulting 49# $my_url data is not the full script URL: this is good, because we want 50# generated links to keep implying the script name if it wasn't explicitly 51# indicated in the URL we're handling, but it means that $my_url cannot be used 52# as base URL. 53# Therefore, if we needed to strip PATH_INFO, then we know that we have 54# to build the base URL ourselves: 55our$path_info=$ENV{"PATH_INFO"}; 56if($path_info) { 57if($my_url=~ s,\Q$path_info\E$,, && 58$my_uri=~ s,\Q$path_info\E$,, && 59defined$ENV{'SCRIPT_NAME'}) { 60$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 61} 62} 63 64# target of the home link on top of all pages 65our$home_link=$my_uri||"/"; 66} 67 68# core git executable to use 69# this can just be "git" if your webserver has a sensible PATH 70our$GIT="++GIT_BINDIR++/git"; 71 72# absolute fs-path which will be prepended to the project path 73#our $projectroot = "/pub/scm"; 74our$projectroot="++GITWEB_PROJECTROOT++"; 75 76# fs traversing limit for getting project list 77# the number is relative to the projectroot 78our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 79 80# string of the home link on top of all pages 81our$home_link_str="++GITWEB_HOME_LINK_STR++"; 82 83# name of your site or organization to appear in page titles 84# replace this with something more descriptive for clearer bookmarks 85our$site_name="++GITWEB_SITENAME++" 86|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 87 88# filename of html text to include at top of each page 89our$site_header="++GITWEB_SITE_HEADER++"; 90# html text to include at home page 91our$home_text="++GITWEB_HOMETEXT++"; 92# filename of html text to include at bottom of each page 93our$site_footer="++GITWEB_SITE_FOOTER++"; 94 95# URI of stylesheets 96our@stylesheets= ("++GITWEB_CSS++"); 97# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 98our$stylesheet=undef; 99# URI of GIT logo (72x27 size) 100our$logo="++GITWEB_LOGO++"; 101# URI of GIT favicon, assumed to be image/png type 102our$favicon="++GITWEB_FAVICON++"; 103# URI of gitweb.js (JavaScript code for gitweb) 104our$javascript="++GITWEB_JS++"; 105 106# URI and label (title) of GIT logo link 107#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 108#our $logo_label = "git documentation"; 109our$logo_url="http://git-scm.com/"; 110our$logo_label="git homepage"; 111 112# source of projects list 113our$projects_list="++GITWEB_LIST++"; 114 115# the width (in characters) of the projects list "Description" column 116our$projects_list_description_width=25; 117 118# group projects by category on the projects list 119# (enabled if this variable evaluates to true) 120our$projects_list_group_categories=0; 121 122# default category if none specified 123# (leave the empty string for no category) 124our$project_list_default_category=""; 125 126# default order of projects list 127# valid values are none, project, descr, owner, and age 128our$default_projects_order="project"; 129 130# show repository only if this file exists 131# (only effective if this variable evaluates to true) 132our$export_ok="++GITWEB_EXPORT_OK++"; 133 134# show repository only if this subroutine returns true 135# when given the path to the project, for example: 136# sub { return -e "$_[0]/git-daemon-export-ok"; } 137our$export_auth_hook=undef; 138 139# only allow viewing of repositories also shown on the overview page 140our$strict_export="++GITWEB_STRICT_EXPORT++"; 141 142# list of git base URLs used for URL to where fetch project from, 143# i.e. full URL is "$git_base_url/$project" 144our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 145 146# default blob_plain mimetype and default charset for text/plain blob 147our$default_blob_plain_mimetype='text/plain'; 148our$default_text_plain_charset=undef; 149 150# file to use for guessing MIME types before trying /etc/mime.types 151# (relative to the current git repository) 152our$mimetypes_file=undef; 153 154# assume this charset if line contains non-UTF-8 characters; 155# it should be valid encoding (see Encoding::Supported(3pm) for list), 156# for which encoding all byte sequences are valid, for example 157# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 158# could be even 'utf-8' for the old behavior) 159our$fallback_encoding='latin1'; 160 161# rename detection options for git-diff and git-diff-tree 162# - default is '-M', with the cost proportional to 163# (number of removed files) * (number of new files). 164# - more costly is '-C' (which implies '-M'), with the cost proportional to 165# (number of changed files + number of removed files) * (number of new files) 166# - even more costly is '-C', '--find-copies-harder' with cost 167# (number of files in the original tree) * (number of new files) 168# - one might want to include '-B' option, e.g. '-B', '-M' 169our@diff_opts= ('-M');# taken from git_commit 170 171# Disables features that would allow repository owners to inject script into 172# the gitweb domain. 173our$prevent_xss=0; 174 175# Path to the highlight executable to use (must be the one from 176# http://www.andre-simon.de due to assumptions about parameters and output). 177# Useful if highlight is not installed on your webserver's PATH. 178# [Default: highlight] 179our$highlight_bin="++HIGHLIGHT_BIN++"; 180 181# information about snapshot formats that gitweb is capable of serving 182our%known_snapshot_formats= ( 183# name => { 184# 'display' => display name, 185# 'type' => mime type, 186# 'suffix' => filename suffix, 187# 'format' => --format for git-archive, 188# 'compressor' => [compressor command and arguments] 189# (array reference, optional) 190# 'disabled' => boolean (optional)} 191# 192'tgz'=> { 193'display'=>'tar.gz', 194'type'=>'application/x-gzip', 195'suffix'=>'.tar.gz', 196'format'=>'tar', 197'compressor'=> ['gzip','-n']}, 198 199'tbz2'=> { 200'display'=>'tar.bz2', 201'type'=>'application/x-bzip2', 202'suffix'=>'.tar.bz2', 203'format'=>'tar', 204'compressor'=> ['bzip2']}, 205 206'txz'=> { 207'display'=>'tar.xz', 208'type'=>'application/x-xz', 209'suffix'=>'.tar.xz', 210'format'=>'tar', 211'compressor'=> ['xz'], 212'disabled'=>1}, 213 214'zip'=> { 215'display'=>'zip', 216'type'=>'application/x-zip', 217'suffix'=>'.zip', 218'format'=>'zip'}, 219); 220 221# Aliases so we understand old gitweb.snapshot values in repository 222# configuration. 223our%known_snapshot_format_aliases= ( 224'gzip'=>'tgz', 225'bzip2'=>'tbz2', 226'xz'=>'txz', 227 228# backward compatibility: legacy gitweb config support 229'x-gzip'=>undef,'gz'=>undef, 230'x-bzip2'=>undef,'bz2'=>undef, 231'x-zip'=>undef,''=>undef, 232); 233 234# Pixel sizes for icons and avatars. If the default font sizes or lineheights 235# are changed, it may be appropriate to change these values too via 236# $GITWEB_CONFIG. 237our%avatar_size= ( 238'default'=>16, 239'double'=>32 240); 241 242# Used to set the maximum load that we will still respond to gitweb queries. 243# If server load exceed this value then return "503 server busy" error. 244# If gitweb cannot determined server load, it is taken to be 0. 245# Leave it undefined (or set to 'undef') to turn off load checking. 246our$maxload=300; 247 248# configuration for 'highlight' (http://www.andre-simon.de/) 249# match by basename 250our%highlight_basename= ( 251#'Program' => 'py', 252#'Library' => 'py', 253'SConstruct'=>'py',# SCons equivalent of Makefile 254'Makefile'=>'make', 255); 256# match by extension 257our%highlight_ext= ( 258# main extensions, defining name of syntax; 259# see files in /usr/share/highlight/langDefs/ directory 260map{$_=>$_} 261qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl sql make), 262# alternate extensions, see /etc/highlight/filetypes.conf 263'h'=>'c', 264map{$_=>'sh'}qw(bash zsh ksh), 265map{$_=>'cpp'}qw(cxx c++ cc), 266map{$_=>'php'}qw(php3 php4 php5 phps), 267map{$_=>'pl'}qw(perl pm),# perhaps also 'cgi' 268map{$_=>'make'}qw(mak mk), 269map{$_=>'xml'}qw(xhtml html htm), 270); 271 272# You define site-wide feature defaults here; override them with 273# $GITWEB_CONFIG as necessary. 274our%feature= ( 275# feature => { 276# 'sub' => feature-sub (subroutine), 277# 'override' => allow-override (boolean), 278# 'default' => [ default options...] (array reference)} 279# 280# if feature is overridable (it means that allow-override has true value), 281# then feature-sub will be called with default options as parameters; 282# return value of feature-sub indicates if to enable specified feature 283# 284# if there is no 'sub' key (no feature-sub), then feature cannot be 285# overridden 286# 287# use gitweb_get_feature(<feature>) to retrieve the <feature> value 288# (an array) or gitweb_check_feature(<feature>) to check if <feature> 289# is enabled 290 291# Enable the 'blame' blob view, showing the last commit that modified 292# each line in the file. This can be very CPU-intensive. 293 294# To enable system wide have in $GITWEB_CONFIG 295# $feature{'blame'}{'default'} = [1]; 296# To have project specific config enable override in $GITWEB_CONFIG 297# $feature{'blame'}{'override'} = 1; 298# and in project config gitweb.blame = 0|1; 299'blame'=> { 300'sub'=>sub{ feature_bool('blame',@_) }, 301'override'=>0, 302'default'=> [0]}, 303 304# Enable the 'snapshot' link, providing a compressed archive of any 305# tree. This can potentially generate high traffic if you have large 306# project. 307 308# Value is a list of formats defined in %known_snapshot_formats that 309# you wish to offer. 310# To disable system wide have in $GITWEB_CONFIG 311# $feature{'snapshot'}{'default'} = []; 312# To have project specific config enable override in $GITWEB_CONFIG 313# $feature{'snapshot'}{'override'} = 1; 314# and in project config, a comma-separated list of formats or "none" 315# to disable. Example: gitweb.snapshot = tbz2,zip; 316'snapshot'=> { 317'sub'=> \&feature_snapshot, 318'override'=>0, 319'default'=> ['tgz']}, 320 321# Enable text search, which will list the commits which match author, 322# committer or commit text to a given string. Enabled by default. 323# Project specific override is not supported. 324# 325# Note that this controls all search features, which means that if 326# it is disabled, then 'grep' and 'pickaxe' search would also be 327# disabled. 328'search'=> { 329'override'=>0, 330'default'=> [1]}, 331 332# Enable grep search, which will list the files in currently selected 333# tree containing the given string. Enabled by default. This can be 334# potentially CPU-intensive, of course. 335# Note that you need to have 'search' feature enabled too. 336 337# To enable system wide have in $GITWEB_CONFIG 338# $feature{'grep'}{'default'} = [1]; 339# To have project specific config enable override in $GITWEB_CONFIG 340# $feature{'grep'}{'override'} = 1; 341# and in project config gitweb.grep = 0|1; 342'grep'=> { 343'sub'=>sub{ feature_bool('grep',@_) }, 344'override'=>0, 345'default'=> [1]}, 346 347# Enable the pickaxe search, which will list the commits that modified 348# a given string in a file. This can be practical and quite faster 349# alternative to 'blame', but still potentially CPU-intensive. 350# Note that you need to have 'search' feature enabled too. 351 352# To enable system wide have in $GITWEB_CONFIG 353# $feature{'pickaxe'}{'default'} = [1]; 354# To have project specific config enable override in $GITWEB_CONFIG 355# $feature{'pickaxe'}{'override'} = 1; 356# and in project config gitweb.pickaxe = 0|1; 357'pickaxe'=> { 358'sub'=>sub{ feature_bool('pickaxe',@_) }, 359'override'=>0, 360'default'=> [1]}, 361 362# Enable showing size of blobs in a 'tree' view, in a separate 363# column, similar to what 'ls -l' does. This cost a bit of IO. 364 365# To disable system wide have in $GITWEB_CONFIG 366# $feature{'show-sizes'}{'default'} = [0]; 367# To have project specific config enable override in $GITWEB_CONFIG 368# $feature{'show-sizes'}{'override'} = 1; 369# and in project config gitweb.showsizes = 0|1; 370'show-sizes'=> { 371'sub'=>sub{ feature_bool('showsizes',@_) }, 372'override'=>0, 373'default'=> [1]}, 374 375# Make gitweb use an alternative format of the URLs which can be 376# more readable and natural-looking: project name is embedded 377# directly in the path and the query string contains other 378# auxiliary information. All gitweb installations recognize 379# URL in either format; this configures in which formats gitweb 380# generates links. 381 382# To enable system wide have in $GITWEB_CONFIG 383# $feature{'pathinfo'}{'default'} = [1]; 384# Project specific override is not supported. 385 386# Note that you will need to change the default location of CSS, 387# favicon, logo and possibly other files to an absolute URL. Also, 388# if gitweb.cgi serves as your indexfile, you will need to force 389# $my_uri to contain the script name in your $GITWEB_CONFIG. 390'pathinfo'=> { 391'override'=>0, 392'default'=> [0]}, 393 394# Make gitweb consider projects in project root subdirectories 395# to be forks of existing projects. Given project $projname.git, 396# projects matching $projname/*.git will not be shown in the main 397# projects list, instead a '+' mark will be added to $projname 398# there and a 'forks' view will be enabled for the project, listing 399# all the forks. If project list is taken from a file, forks have 400# to be listed after the main project. 401 402# To enable system wide have in $GITWEB_CONFIG 403# $feature{'forks'}{'default'} = [1]; 404# Project specific override is not supported. 405'forks'=> { 406'override'=>0, 407'default'=> [0]}, 408 409# Insert custom links to the action bar of all project pages. 410# This enables you mainly to link to third-party scripts integrating 411# into gitweb; e.g. git-browser for graphical history representation 412# or custom web-based repository administration interface. 413 414# The 'default' value consists of a list of triplets in the form 415# (label, link, position) where position is the label after which 416# to insert the link and link is a format string where %n expands 417# to the project name, %f to the project path within the filesystem, 418# %h to the current hash (h gitweb parameter) and %b to the current 419# hash base (hb gitweb parameter); %% expands to %. 420 421# To enable system wide have in $GITWEB_CONFIG e.g. 422# $feature{'actions'}{'default'} = [('graphiclog', 423# '/git-browser/by-commit.html?r=%n', 'summary')]; 424# Project specific override is not supported. 425'actions'=> { 426'override'=>0, 427'default'=> []}, 428 429# Allow gitweb scan project content tags of project repository, 430# and display the popular Web 2.0-ish "tag cloud" near the projects 431# list. Note that this is something COMPLETELY different from the 432# normal Git tags. 433 434# gitweb by itself can show existing tags, but it does not handle 435# tagging itself; you need to do it externally, outside gitweb. 436# The format is described in git_get_project_ctags() subroutine. 437# You may want to install the HTML::TagCloud Perl module to get 438# a pretty tag cloud instead of just a list of tags. 439 440# To enable system wide have in $GITWEB_CONFIG 441# $feature{'ctags'}{'default'} = [1]; 442# Project specific override is not supported. 443 444# In the future whether ctags editing is enabled might depend 445# on the value, but using 1 should always mean no editing of ctags. 446'ctags'=> { 447'override'=>0, 448'default'=> [0]}, 449 450# The maximum number of patches in a patchset generated in patch 451# view. Set this to 0 or undef to disable patch view, or to a 452# negative number to remove any limit. 453 454# To disable system wide have in $GITWEB_CONFIG 455# $feature{'patches'}{'default'} = [0]; 456# To have project specific config enable override in $GITWEB_CONFIG 457# $feature{'patches'}{'override'} = 1; 458# and in project config gitweb.patches = 0|n; 459# where n is the maximum number of patches allowed in a patchset. 460'patches'=> { 461'sub'=> \&feature_patches, 462'override'=>0, 463'default'=> [16]}, 464 465# Avatar support. When this feature is enabled, views such as 466# shortlog or commit will display an avatar associated with 467# the email of the committer(s) and/or author(s). 468 469# Currently available providers are gravatar and picon. 470# If an unknown provider is specified, the feature is disabled. 471 472# Gravatar depends on Digest::MD5. 473# Picon currently relies on the indiana.edu database. 474 475# To enable system wide have in $GITWEB_CONFIG 476# $feature{'avatar'}{'default'} = ['<provider>']; 477# where <provider> is either gravatar or picon. 478# To have project specific config enable override in $GITWEB_CONFIG 479# $feature{'avatar'}{'override'} = 1; 480# and in project config gitweb.avatar = <provider>; 481'avatar'=> { 482'sub'=> \&feature_avatar, 483'override'=>0, 484'default'=> ['']}, 485 486# Enable displaying how much time and how many git commands 487# it took to generate and display page. Disabled by default. 488# Project specific override is not supported. 489'timed'=> { 490'override'=>0, 491'default'=> [0]}, 492 493# Enable turning some links into links to actions which require 494# JavaScript to run (like 'blame_incremental'). Not enabled by 495# default. Project specific override is currently not supported. 496'javascript-actions'=> { 497'override'=>0, 498'default'=> [0]}, 499 500# Enable and configure ability to change common timezone for dates 501# in gitweb output via JavaScript. Enabled by default. 502# Project specific override is not supported. 503'javascript-timezone'=> { 504'override'=>0, 505'default'=> [ 506'local',# default timezone: 'utc', 'local', or '(-|+)HHMM' format, 507# or undef to turn off this feature 508'gitweb_tz',# name of cookie where to store selected timezone 509'datetime',# CSS class used to mark up dates for manipulation 510]}, 511 512# Syntax highlighting support. This is based on Daniel Svensson's 513# and Sham Chukoury's work in gitweb-xmms2.git. 514# It requires the 'highlight' program present in $PATH, 515# and therefore is disabled by default. 516 517# To enable system wide have in $GITWEB_CONFIG 518# $feature{'highlight'}{'default'} = [1]; 519 520'highlight'=> { 521'sub'=>sub{ feature_bool('highlight',@_) }, 522'override'=>0, 523'default'=> [0]}, 524 525# Enable displaying of remote heads in the heads list 526 527# To enable system wide have in $GITWEB_CONFIG 528# $feature{'remote_heads'}{'default'} = [1]; 529# To have project specific config enable override in $GITWEB_CONFIG 530# $feature{'remote_heads'}{'override'} = 1; 531# and in project config gitweb.remote_heads = 0|1; 532'remote_heads'=> { 533'sub'=>sub{ feature_bool('remote_heads',@_) }, 534'override'=>0, 535'default'=> [0]}, 536); 537 538sub gitweb_get_feature { 539my($name) =@_; 540return unlessexists$feature{$name}; 541my($sub,$override,@defaults) = ( 542$feature{$name}{'sub'}, 543$feature{$name}{'override'}, 544@{$feature{$name}{'default'}}); 545# project specific override is possible only if we have project 546our$git_dir;# global variable, declared later 547if(!$override|| !defined$git_dir) { 548return@defaults; 549} 550if(!defined$sub) { 551warn"feature$nameis not overridable"; 552return@defaults; 553} 554return$sub->(@defaults); 555} 556 557# A wrapper to check if a given feature is enabled. 558# With this, you can say 559# 560# my $bool_feat = gitweb_check_feature('bool_feat'); 561# gitweb_check_feature('bool_feat') or somecode; 562# 563# instead of 564# 565# my ($bool_feat) = gitweb_get_feature('bool_feat'); 566# (gitweb_get_feature('bool_feat'))[0] or somecode; 567# 568sub gitweb_check_feature { 569return(gitweb_get_feature(@_))[0]; 570} 571 572 573sub feature_bool { 574my$key=shift; 575my($val) = git_get_project_config($key,'--bool'); 576 577if(!defined$val) { 578return($_[0]); 579}elsif($valeq'true') { 580return(1); 581}elsif($valeq'false') { 582return(0); 583} 584} 585 586sub feature_snapshot { 587my(@fmts) =@_; 588 589my($val) = git_get_project_config('snapshot'); 590 591if($val) { 592@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 593} 594 595return@fmts; 596} 597 598sub feature_patches { 599my@val= (git_get_project_config('patches','--int')); 600 601if(@val) { 602return@val; 603} 604 605return($_[0]); 606} 607 608sub feature_avatar { 609my@val= (git_get_project_config('avatar')); 610 611return@val?@val:@_; 612} 613 614# checking HEAD file with -e is fragile if the repository was 615# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 616# and then pruned. 617sub check_head_link { 618my($dir) =@_; 619my$headfile="$dir/HEAD"; 620return((-e $headfile) || 621(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 622} 623 624sub check_export_ok { 625my($dir) =@_; 626return(check_head_link($dir) && 627(!$export_ok|| -e "$dir/$export_ok") && 628(!$export_auth_hook||$export_auth_hook->($dir))); 629} 630 631# process alternate names for backward compatibility 632# filter out unsupported (unknown) snapshot formats 633sub filter_snapshot_fmts { 634my@fmts=@_; 635 636@fmts=map{ 637exists$known_snapshot_format_aliases{$_} ? 638$known_snapshot_format_aliases{$_} :$_}@fmts; 639@fmts=grep{ 640exists$known_snapshot_formats{$_} && 641!$known_snapshot_formats{$_}{'disabled'}}@fmts; 642} 643 644# If it is set to code reference, it is code that it is to be run once per 645# request, allowing updating configurations that change with each request, 646# while running other code in config file only once. 647# 648# Otherwise, if it is false then gitweb would process config file only once; 649# if it is true then gitweb config would be run for each request. 650our$per_request_config=1; 651 652# read and parse gitweb config file given by its parameter. 653# returns true on success, false on recoverable error, allowing 654# to chain this subroutine, using first file that exists. 655# dies on errors during parsing config file, as it is unrecoverable. 656sub read_config_file { 657my$filename=shift; 658return unlessdefined$filename; 659# die if there are errors parsing config file 660if(-e $filename) { 661do$filename; 662die$@if$@; 663return1; 664} 665return; 666} 667 668our($GITWEB_CONFIG,$GITWEB_CONFIG_SYSTEM); 669sub evaluate_gitweb_config { 670our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 671our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 672 673# use first config file that exists 674 read_config_file($GITWEB_CONFIG)or 675 read_config_file($GITWEB_CONFIG_SYSTEM); 676} 677 678# Get loadavg of system, to compare against $maxload. 679# Currently it requires '/proc/loadavg' present to get loadavg; 680# if it is not present it returns 0, which means no load checking. 681sub get_loadavg { 682if( -e '/proc/loadavg'){ 683open my$fd,'<','/proc/loadavg' 684orreturn0; 685my@load=split(/\s+/,scalar<$fd>); 686close$fd; 687 688# The first three columns measure CPU and IO utilization of the last one, 689# five, and 10 minute periods. The fourth column shows the number of 690# currently running processes and the total number of processes in the m/n 691# format. The last column displays the last process ID used. 692return$load[0] ||0; 693} 694# additional checks for load average should go here for things that don't export 695# /proc/loadavg 696 697return0; 698} 699 700# version of the core git binary 701our$git_version; 702sub evaluate_git_version { 703our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 704$number_of_git_cmds++; 705} 706 707sub check_loadavg { 708if(defined$maxload&& get_loadavg() >$maxload) { 709 die_error(503,"The load average on the server is too high"); 710} 711} 712 713# ====================================================================== 714# input validation and dispatch 715 716# input parameters can be collected from a variety of sources (presently, CGI 717# and PATH_INFO), so we define an %input_params hash that collects them all 718# together during validation: this allows subsequent uses (e.g. href()) to be 719# agnostic of the parameter origin 720 721our%input_params= (); 722 723# input parameters are stored with the long parameter name as key. This will 724# also be used in the href subroutine to convert parameters to their CGI 725# equivalent, and since the href() usage is the most frequent one, we store 726# the name -> CGI key mapping here, instead of the reverse. 727# 728# XXX: Warning: If you touch this, check the search form for updating, 729# too. 730 731our@cgi_param_mapping= ( 732 project =>"p", 733 action =>"a", 734 file_name =>"f", 735 file_parent =>"fp", 736 hash =>"h", 737 hash_parent =>"hp", 738 hash_base =>"hb", 739 hash_parent_base =>"hpb", 740 page =>"pg", 741 order =>"o", 742 searchtext =>"s", 743 searchtype =>"st", 744 snapshot_format =>"sf", 745 extra_options =>"opt", 746 search_use_regexp =>"sr", 747 ctag =>"by_tag", 748# this must be last entry (for manipulation from JavaScript) 749 javascript =>"js" 750); 751our%cgi_param_mapping=@cgi_param_mapping; 752 753# we will also need to know the possible actions, for validation 754our%actions= ( 755"blame"=> \&git_blame, 756"blame_incremental"=> \&git_blame_incremental, 757"blame_data"=> \&git_blame_data, 758"blobdiff"=> \&git_blobdiff, 759"blobdiff_plain"=> \&git_blobdiff_plain, 760"blob"=> \&git_blob, 761"blob_plain"=> \&git_blob_plain, 762"commitdiff"=> \&git_commitdiff, 763"commitdiff_plain"=> \&git_commitdiff_plain, 764"commit"=> \&git_commit, 765"forks"=> \&git_forks, 766"heads"=> \&git_heads, 767"history"=> \&git_history, 768"log"=> \&git_log, 769"patch"=> \&git_patch, 770"patches"=> \&git_patches, 771"remotes"=> \&git_remotes, 772"rss"=> \&git_rss, 773"atom"=> \&git_atom, 774"search"=> \&git_search, 775"search_help"=> \&git_search_help, 776"shortlog"=> \&git_shortlog, 777"summary"=> \&git_summary, 778"tag"=> \&git_tag, 779"tags"=> \&git_tags, 780"tree"=> \&git_tree, 781"snapshot"=> \&git_snapshot, 782"object"=> \&git_object, 783# those below don't need $project 784"opml"=> \&git_opml, 785"project_list"=> \&git_project_list, 786"project_index"=> \&git_project_index, 787); 788 789# finally, we have the hash of allowed extra_options for the commands that 790# allow them 791our%allowed_options= ( 792"--no-merges"=> [qw(rss atom log shortlog history)], 793); 794 795# fill %input_params with the CGI parameters. All values except for 'opt' 796# should be single values, but opt can be an array. We should probably 797# build an array of parameters that can be multi-valued, but since for the time 798# being it's only this one, we just single it out 799sub evaluate_query_params { 800our$cgi; 801 802while(my($name,$symbol) =each%cgi_param_mapping) { 803if($symboleq'opt') { 804$input_params{$name} = [$cgi->param($symbol) ]; 805}else{ 806$input_params{$name} =$cgi->param($symbol); 807} 808} 809} 810 811# now read PATH_INFO and update the parameter list for missing parameters 812sub evaluate_path_info { 813return ifdefined$input_params{'project'}; 814return if!$path_info; 815$path_info=~ s,^/+,,; 816return if!$path_info; 817 818# find which part of PATH_INFO is project 819my$project=$path_info; 820$project=~ s,/+$,,; 821while($project&& !check_head_link("$projectroot/$project")) { 822$project=~ s,/*[^/]*$,,; 823} 824return unless$project; 825$input_params{'project'} =$project; 826 827# do not change any parameters if an action is given using the query string 828return if$input_params{'action'}; 829$path_info=~ s,^\Q$project\E/*,,; 830 831# next, check if we have an action 832my$action=$path_info; 833$action=~ s,/.*$,,; 834if(exists$actions{$action}) { 835$path_info=~ s,^$action/*,,; 836$input_params{'action'} =$action; 837} 838 839# list of actions that want hash_base instead of hash, but can have no 840# pathname (f) parameter 841my@wants_base= ( 842'tree', 843'history', 844); 845 846# we want to catch, among others 847# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 848my($parentrefname,$parentpathname,$refname,$pathname) = 849($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/); 850 851# first, analyze the 'current' part 852if(defined$pathname) { 853# we got "branch:filename" or "branch:dir/" 854# we could use git_get_type(branch:pathname), but: 855# - it needs $git_dir 856# - it does a git() call 857# - the convention of terminating directories with a slash 858# makes it superfluous 859# - embedding the action in the PATH_INFO would make it even 860# more superfluous 861$pathname=~ s,^/+,,; 862if(!$pathname||substr($pathname, -1)eq"/") { 863$input_params{'action'} ||="tree"; 864$pathname=~ s,/$,,; 865}else{ 866# the default action depends on whether we had parent info 867# or not 868if($parentrefname) { 869$input_params{'action'} ||="blobdiff_plain"; 870}else{ 871$input_params{'action'} ||="blob_plain"; 872} 873} 874$input_params{'hash_base'} ||=$refname; 875$input_params{'file_name'} ||=$pathname; 876}elsif(defined$refname) { 877# we got "branch". In this case we have to choose if we have to 878# set hash or hash_base. 879# 880# Most of the actions without a pathname only want hash to be 881# set, except for the ones specified in @wants_base that want 882# hash_base instead. It should also be noted that hand-crafted 883# links having 'history' as an action and no pathname or hash 884# set will fail, but that happens regardless of PATH_INFO. 885if(defined$parentrefname) { 886# if there is parent let the default be 'shortlog' action 887# (for http://git.example.com/repo.git/A..B links); if there 888# is no parent, dispatch will detect type of object and set 889# action appropriately if required (if action is not set) 890$input_params{'action'} ||="shortlog"; 891} 892if($input_params{'action'} && 893grep{$_eq$input_params{'action'} }@wants_base) { 894$input_params{'hash_base'} ||=$refname; 895}else{ 896$input_params{'hash'} ||=$refname; 897} 898} 899 900# next, handle the 'parent' part, if present 901if(defined$parentrefname) { 902# a missing pathspec defaults to the 'current' filename, allowing e.g. 903# someproject/blobdiff/oldrev..newrev:/filename 904if($parentpathname) { 905$parentpathname=~ s,^/+,,; 906$parentpathname=~ s,/$,,; 907$input_params{'file_parent'} ||=$parentpathname; 908}else{ 909$input_params{'file_parent'} ||=$input_params{'file_name'}; 910} 911# we assume that hash_parent_base is wanted if a path was specified, 912# or if the action wants hash_base instead of hash 913if(defined$input_params{'file_parent'} || 914grep{$_eq$input_params{'action'} }@wants_base) { 915$input_params{'hash_parent_base'} ||=$parentrefname; 916}else{ 917$input_params{'hash_parent'} ||=$parentrefname; 918} 919} 920 921# for the snapshot action, we allow URLs in the form 922# $project/snapshot/$hash.ext 923# where .ext determines the snapshot and gets removed from the 924# passed $refname to provide the $hash. 925# 926# To be able to tell that $refname includes the format extension, we 927# require the following two conditions to be satisfied: 928# - the hash input parameter MUST have been set from the $refname part 929# of the URL (i.e. they must be equal) 930# - the snapshot format MUST NOT have been defined already (e.g. from 931# CGI parameter sf) 932# It's also useless to try any matching unless $refname has a dot, 933# so we check for that too 934if(defined$input_params{'action'} && 935$input_params{'action'}eq'snapshot'&& 936defined$refname&&index($refname,'.') != -1&& 937$refnameeq$input_params{'hash'} && 938!defined$input_params{'snapshot_format'}) { 939# We loop over the known snapshot formats, checking for 940# extensions. Allowed extensions are both the defined suffix 941# (which includes the initial dot already) and the snapshot 942# format key itself, with a prepended dot 943while(my($fmt,$opt) =each%known_snapshot_formats) { 944my$hash=$refname; 945unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 946next; 947} 948my$sfx=$1; 949# a valid suffix was found, so set the snapshot format 950# and reset the hash parameter 951$input_params{'snapshot_format'} =$fmt; 952$input_params{'hash'} =$hash; 953# we also set the format suffix to the one requested 954# in the URL: this way a request for e.g. .tgz returns 955# a .tgz instead of a .tar.gz 956$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 957last; 958} 959} 960} 961 962our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 963$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 964$searchtext,$search_regexp); 965sub evaluate_and_validate_params { 966our$action=$input_params{'action'}; 967if(defined$action) { 968if(!validate_action($action)) { 969 die_error(400,"Invalid action parameter"); 970} 971} 972 973# parameters which are pathnames 974our$project=$input_params{'project'}; 975if(defined$project) { 976if(!validate_project($project)) { 977undef$project; 978 die_error(404,"No such project"); 979} 980} 981 982our$file_name=$input_params{'file_name'}; 983if(defined$file_name) { 984if(!validate_pathname($file_name)) { 985 die_error(400,"Invalid file parameter"); 986} 987} 988 989our$file_parent=$input_params{'file_parent'}; 990if(defined$file_parent) { 991if(!validate_pathname($file_parent)) { 992 die_error(400,"Invalid file parent parameter"); 993} 994} 995 996# parameters which are refnames 997our$hash=$input_params{'hash'}; 998if(defined$hash) { 999if(!validate_refname($hash)) {1000 die_error(400,"Invalid hash parameter");1001}1002}10031004our$hash_parent=$input_params{'hash_parent'};1005if(defined$hash_parent) {1006if(!validate_refname($hash_parent)) {1007 die_error(400,"Invalid hash parent parameter");1008}1009}10101011our$hash_base=$input_params{'hash_base'};1012if(defined$hash_base) {1013if(!validate_refname($hash_base)) {1014 die_error(400,"Invalid hash base parameter");1015}1016}10171018our@extra_options= @{$input_params{'extra_options'}};1019# @extra_options is always defined, since it can only be (currently) set from1020# CGI, and $cgi->param() returns the empty array in array context if the param1021# is not set1022foreachmy$opt(@extra_options) {1023if(not exists$allowed_options{$opt}) {1024 die_error(400,"Invalid option parameter");1025}1026if(not grep(/^$action$/, @{$allowed_options{$opt}})) {1027 die_error(400,"Invalid option parameter for this action");1028}1029}10301031our$hash_parent_base=$input_params{'hash_parent_base'};1032if(defined$hash_parent_base) {1033if(!validate_refname($hash_parent_base)) {1034 die_error(400,"Invalid hash parent base parameter");1035}1036}10371038# other parameters1039our$page=$input_params{'page'};1040if(defined$page) {1041if($page=~m/[^0-9]/) {1042 die_error(400,"Invalid page parameter");1043}1044}10451046our$searchtype=$input_params{'searchtype'};1047if(defined$searchtype) {1048if($searchtype=~m/[^a-z]/) {1049 die_error(400,"Invalid searchtype parameter");1050}1051}10521053our$search_use_regexp=$input_params{'search_use_regexp'};10541055our$searchtext=$input_params{'searchtext'};1056our$search_regexp;1057if(defined$searchtext) {1058if(length($searchtext) <2) {1059 die_error(403,"At least two characters are required for search parameter");1060}1061$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext;1062}1063}10641065# path to the current git repository1066our$git_dir;1067sub evaluate_git_dir {1068our$git_dir="$projectroot/$project"if$project;1069}10701071our(@snapshot_fmts,$git_avatar);1072sub configure_gitweb_features {1073# list of supported snapshot formats1074our@snapshot_fmts= gitweb_get_feature('snapshot');1075@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);10761077# check that the avatar feature is set to a known provider name,1078# and for each provider check if the dependencies are satisfied.1079# if the provider name is invalid or the dependencies are not met,1080# reset $git_avatar to the empty string.1081our($git_avatar) = gitweb_get_feature('avatar');1082if($git_avatareq'gravatar') {1083$git_avatar=''unless(eval{require Digest::MD5;1; });1084}elsif($git_avatareq'picon') {1085# no dependencies1086}else{1087$git_avatar='';1088}1089}10901091# custom error handler: 'die <message>' is Internal Server Error1092sub handle_errors_html {1093my$msg=shift;# it is already HTML escaped10941095# to avoid infinite loop where error occurs in die_error,1096# change handler to default handler, disabling handle_errors_html1097 set_message("Error occured when inside die_error:\n$msg");10981099# you cannot jump out of die_error when called as error handler;1100# the subroutine set via CGI::Carp::set_message is called _after_1101# HTTP headers are already written, so it cannot write them itself1102 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1);1103}1104set_message(\&handle_errors_html);11051106# dispatch1107sub dispatch {1108if(!defined$action) {1109if(defined$hash) {1110$action= git_get_type($hash);1111}elsif(defined$hash_base&&defined$file_name) {1112$action= git_get_type("$hash_base:$file_name");1113}elsif(defined$project) {1114$action='summary';1115}else{1116$action='project_list';1117}1118}1119if(!defined($actions{$action})) {1120 die_error(400,"Unknown action");1121}1122if($action!~m/^(?:opml|project_list|project_index)$/&&1123!$project) {1124 die_error(400,"Project needed");1125}1126$actions{$action}->();1127}11281129sub reset_timer {1130our$t0= [ gettimeofday() ]1131ifdefined$t0;1132our$number_of_git_cmds=0;1133}11341135our$first_request=1;1136sub run_request {1137 reset_timer();11381139 evaluate_uri();1140if($first_request) {1141 evaluate_gitweb_config();1142 evaluate_git_version();1143}1144if($per_request_config) {1145if(ref($per_request_config)eq'CODE') {1146$per_request_config->();1147}elsif(!$first_request) {1148 evaluate_gitweb_config();1149}1150}1151 check_loadavg();11521153# $projectroot and $projects_list might be set in gitweb config file1154$projects_list||=$projectroot;11551156 evaluate_query_params();1157 evaluate_path_info();1158 evaluate_and_validate_params();1159 evaluate_git_dir();11601161 configure_gitweb_features();11621163 dispatch();1164}11651166our$is_last_request=sub{1};1167our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1168our$CGI='CGI';1169our$cgi;1170sub configure_as_fcgi {1171require CGI::Fast;1172our$CGI='CGI::Fast';11731174my$request_number=0;1175# let each child service 100 requests1176our$is_last_request=sub{ ++$request_number>100};1177}1178sub evaluate_argv {1179my$script_name=$ENV{'SCRIPT_NAME'} ||$ENV{'SCRIPT_FILENAME'} || __FILE__;1180 configure_as_fcgi()1181if$script_name=~/\.fcgi$/;11821183return unless(@ARGV);11841185require Getopt::Long;1186 Getopt::Long::GetOptions(1187'fastcgi|fcgi|f'=> \&configure_as_fcgi,1188'nproc|n=i'=>sub{1189my($arg,$val) =@_;1190return unlesseval{require FCGI::ProcManager;1; };1191my$proc_manager= FCGI::ProcManager->new({1192 n_processes =>$val,1193});1194our$pre_listen_hook=sub{$proc_manager->pm_manage() };1195our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1196our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1197},1198);1199}12001201sub run {1202 evaluate_argv();12031204$first_request=1;1205$pre_listen_hook->()1206if$pre_listen_hook;12071208 REQUEST:1209while($cgi=$CGI->new()) {1210$pre_dispatch_hook->()1211if$pre_dispatch_hook;12121213 run_request();12141215$post_dispatch_hook->()1216if$post_dispatch_hook;1217$first_request=0;12181219last REQUEST if($is_last_request->());1220}12211222 DONE_GITWEB:12231;1224}12251226run();12271228if(defined caller) {1229# wrapped in a subroutine processing requests,1230# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1231return;1232}else{1233# pure CGI script, serving single request1234exit;1235}12361237## ======================================================================1238## action links12391240# possible values of extra options1241# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1242# -replay => 1 - start from a current view (replay with modifications)1243# -path_info => 0|1 - don't use/use path_info URL (if possible)1244# -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone1245sub href {1246my%params=@_;1247# default is to use -absolute url() i.e. $my_uri1248my$href=$params{-full} ?$my_url:$my_uri;12491250# implicit -replay, must be first of implicit params1251$params{-replay} =1if(keys%params==1&&$params{-anchor});12521253$params{'project'} =$projectunlessexists$params{'project'};12541255if($params{-replay}) {1256while(my($name,$symbol) =each%cgi_param_mapping) {1257if(!exists$params{$name}) {1258$params{$name} =$input_params{$name};1259}1260}1261}12621263my$use_pathinfo= gitweb_check_feature('pathinfo');1264if(defined$params{'project'} &&1265(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1266# try to put as many parameters as possible in PATH_INFO:1267# - project name1268# - action1269# - hash_parent or hash_parent_base:/file_parent1270# - hash or hash_base:/filename1271# - the snapshot_format as an appropriate suffix12721273# When the script is the root DirectoryIndex for the domain,1274# $href here would be something like http://gitweb.example.com/1275# Thus, we strip any trailing / from $href, to spare us double1276# slashes in the final URL1277$href=~ s,/$,,;12781279# Then add the project name, if present1280$href.="/".esc_path_info($params{'project'});1281delete$params{'project'};12821283# since we destructively absorb parameters, we keep this1284# boolean that remembers if we're handling a snapshot1285my$is_snapshot=$params{'action'}eq'snapshot';12861287# Summary just uses the project path URL, any other action is1288# added to the URL1289if(defined$params{'action'}) {1290$href.="/".esc_path_info($params{'action'})1291unless$params{'action'}eq'summary';1292delete$params{'action'};1293}12941295# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1296# stripping nonexistent or useless pieces1297$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1298||$params{'hash_parent'} ||$params{'hash'});1299if(defined$params{'hash_base'}) {1300if(defined$params{'hash_parent_base'}) {1301$href.= esc_path_info($params{'hash_parent_base'});1302# skip the file_parent if it's the same as the file_name1303if(defined$params{'file_parent'}) {1304if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1305delete$params{'file_parent'};1306}elsif($params{'file_parent'} !~/\.\./) {1307$href.=":/".esc_path_info($params{'file_parent'});1308delete$params{'file_parent'};1309}1310}1311$href.="..";1312delete$params{'hash_parent'};1313delete$params{'hash_parent_base'};1314}elsif(defined$params{'hash_parent'}) {1315$href.= esc_path_info($params{'hash_parent'})."..";1316delete$params{'hash_parent'};1317}13181319$href.= esc_path_info($params{'hash_base'});1320if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1321$href.=":/".esc_path_info($params{'file_name'});1322delete$params{'file_name'};1323}1324delete$params{'hash'};1325delete$params{'hash_base'};1326}elsif(defined$params{'hash'}) {1327$href.= esc_path_info($params{'hash'});1328delete$params{'hash'};1329}13301331# If the action was a snapshot, we can absorb the1332# snapshot_format parameter too1333if($is_snapshot) {1334my$fmt=$params{'snapshot_format'};1335# snapshot_format should always be defined when href()1336# is called, but just in case some code forgets, we1337# fall back to the default1338$fmt||=$snapshot_fmts[0];1339$href.=$known_snapshot_formats{$fmt}{'suffix'};1340delete$params{'snapshot_format'};1341}1342}13431344# now encode the parameters explicitly1345my@result= ();1346for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1347my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1348if(defined$params{$name}) {1349if(ref($params{$name})eq"ARRAY") {1350foreachmy$par(@{$params{$name}}) {1351push@result,$symbol."=". esc_param($par);1352}1353}else{1354push@result,$symbol."=". esc_param($params{$name});1355}1356}1357}1358$href.="?".join(';',@result)ifscalar@result;13591360# final transformation: trailing spaces must be escaped (URI-encoded)1361$href=~s/(\s+)$/CGI::escape($1)/e;13621363if($params{-anchor}) {1364$href.="#".esc_param($params{-anchor});1365}13661367return$href;1368}136913701371## ======================================================================1372## validation, quoting/unquoting and escaping13731374sub validate_action {1375my$input=shift||returnundef;1376returnundefunlessexists$actions{$input};1377return$input;1378}13791380sub validate_project {1381my$input=shift||returnundef;1382if(!validate_pathname($input) ||1383!(-d "$projectroot/$input") ||1384!check_export_ok("$projectroot/$input") ||1385($strict_export&& !project_in_list($input))) {1386returnundef;1387}else{1388return$input;1389}1390}13911392sub validate_pathname {1393my$input=shift||returnundef;13941395# no '.' or '..' as elements of path, i.e. no '.' nor '..'1396# at the beginning, at the end, and between slashes.1397# also this catches doubled slashes1398if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1399returnundef;1400}1401# no null characters1402if($input=~m!\0!) {1403returnundef;1404}1405return$input;1406}14071408sub validate_refname {1409my$input=shift||returnundef;14101411# textual hashes are O.K.1412if($input=~m/^[0-9a-fA-F]{40}$/) {1413return$input;1414}1415# it must be correct pathname1416$input= validate_pathname($input)1417orreturnundef;1418# restrictions on ref name according to git-check-ref-format1419if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1420returnundef;1421}1422return$input;1423}14241425# decode sequences of octets in utf8 into Perl's internal form,1426# which is utf-8 with utf8 flag set if needed. gitweb writes out1427# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1428sub to_utf8 {1429my$str=shift;1430returnundefunlessdefined$str;1431if(utf8::valid($str)) {1432 utf8::decode($str);1433return$str;1434}else{1435return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1436}1437}14381439# quote unsafe chars, but keep the slash, even when it's not1440# correct, but quoted slashes look too horrible in bookmarks1441sub esc_param {1442my$str=shift;1443returnundefunlessdefined$str;1444$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1445$str=~s/ /\+/g;1446return$str;1447}14481449# the quoting rules for path_info fragment are slightly different1450sub esc_path_info {1451my$str=shift;1452returnundefunlessdefined$str;14531454# path_info doesn't treat '+' as space (specially), but '?' must be escaped1455$str=~s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;14561457return$str;1458}14591460# quote unsafe chars in whole URL, so some characters cannot be quoted1461sub esc_url {1462my$str=shift;1463returnundefunlessdefined$str;1464$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;1465$str=~s/ /\+/g;1466return$str;1467}14681469# quote unsafe characters in HTML attributes1470sub esc_attr {14711472# for XHTML conformance escaping '"' to '"' is not enough1473return esc_html(@_);1474}14751476# replace invalid utf8 character with SUBSTITUTION sequence1477sub esc_html {1478my$str=shift;1479my%opts=@_;14801481returnundefunlessdefined$str;14821483$str= to_utf8($str);1484$str=$cgi->escapeHTML($str);1485if($opts{'-nbsp'}) {1486$str=~s/ / /g;1487}1488$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1489return$str;1490}14911492# quote control characters and escape filename to HTML1493sub esc_path {1494my$str=shift;1495my%opts=@_;14961497returnundefunlessdefined$str;14981499$str= to_utf8($str);1500$str=$cgi->escapeHTML($str);1501if($opts{'-nbsp'}) {1502$str=~s/ / /g;1503}1504$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1505return$str;1506}15071508# Make control characters "printable", using character escape codes (CEC)1509sub quot_cec {1510my$cntrl=shift;1511my%opts=@_;1512my%es= (# character escape codes, aka escape sequences1513"\t"=>'\t',# tab (HT)1514"\n"=>'\n',# line feed (LF)1515"\r"=>'\r',# carrige return (CR)1516"\f"=>'\f',# form feed (FF)1517"\b"=>'\b',# backspace (BS)1518"\a"=>'\a',# alarm (bell) (BEL)1519"\e"=>'\e',# escape (ESC)1520"\013"=>'\v',# vertical tab (VT)1521"\000"=>'\0',# nul character (NUL)1522);1523my$chr= ( (exists$es{$cntrl})1524?$es{$cntrl}1525:sprintf('\%2x',ord($cntrl)) );1526if($opts{-nohtml}) {1527return$chr;1528}else{1529return"<span class=\"cntrl\">$chr</span>";1530}1531}15321533# Alternatively use unicode control pictures codepoints,1534# Unicode "printable representation" (PR)1535sub quot_upr {1536my$cntrl=shift;1537my%opts=@_;15381539my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1540if($opts{-nohtml}) {1541return$chr;1542}else{1543return"<span class=\"cntrl\">$chr</span>";1544}1545}15461547# git may return quoted and escaped filenames1548sub unquote {1549my$str=shift;15501551sub unq {1552my$seq=shift;1553my%es= (# character escape codes, aka escape sequences1554't'=>"\t",# tab (HT, TAB)1555'n'=>"\n",# newline (NL)1556'r'=>"\r",# return (CR)1557'f'=>"\f",# form feed (FF)1558'b'=>"\b",# backspace (BS)1559'a'=>"\a",# alarm (bell) (BEL)1560'e'=>"\e",# escape (ESC)1561'v'=>"\013",# vertical tab (VT)1562);15631564if($seq=~m/^[0-7]{1,3}$/) {1565# octal char sequence1566returnchr(oct($seq));1567}elsif(exists$es{$seq}) {1568# C escape sequence, aka character escape code1569return$es{$seq};1570}1571# quoted ordinary character1572return$seq;1573}15741575if($str=~m/^"(.*)"$/) {1576# needs unquoting1577$str=$1;1578$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1579}1580return$str;1581}15821583# escape tabs (convert tabs to spaces)1584sub untabify {1585my$line=shift;15861587while((my$pos=index($line,"\t")) != -1) {1588if(my$count= (8- ($pos%8))) {1589my$spaces=' ' x $count;1590$line=~s/\t/$spaces/;1591}1592}15931594return$line;1595}15961597sub project_in_list {1598my$project=shift;1599my@list= git_get_projects_list();1600return@list&&scalar(grep{$_->{'path'}eq$project}@list);1601}16021603## ----------------------------------------------------------------------1604## HTML aware string manipulation16051606# Try to chop given string on a word boundary between position1607# $len and $len+$add_len. If there is no word boundary there,1608# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1609# (marking chopped part) would be longer than given string.1610sub chop_str {1611my$str=shift;1612my$len=shift;1613my$add_len=shift||10;1614my$where=shift||'right';# 'left' | 'center' | 'right'16151616# Make sure perl knows it is utf8 encoded so we don't1617# cut in the middle of a utf8 multibyte char.1618$str= to_utf8($str);16191620# allow only $len chars, but don't cut a word if it would fit in $add_len1621# if it doesn't fit, cut it if it's still longer than the dots we would add1622# remove chopped character entities entirely16231624# when chopping in the middle, distribute $len into left and right part1625# return early if chopping wouldn't make string shorter1626if($whereeq'center') {1627return$strif($len+5>=length($str));# filler is length 51628$len=int($len/2);1629}else{1630return$strif($len+4>=length($str));# filler is length 41631}16321633# regexps: ending and beginning with word part up to $add_len1634my$endre=qr/.{$len}\w{0,$add_len}/;1635my$begre=qr/\w{0,$add_len}.{$len}/;16361637if($whereeq'left') {1638$str=~m/^(.*?)($begre)$/;1639my($lead,$body) = ($1,$2);1640if(length($lead) >4) {1641$lead=" ...";1642}1643return"$lead$body";16441645}elsif($whereeq'center') {1646$str=~m/^($endre)(.*)$/;1647my($left,$str) = ($1,$2);1648$str=~m/^(.*?)($begre)$/;1649my($mid,$right) = ($1,$2);1650if(length($mid) >5) {1651$mid=" ... ";1652}1653return"$left$mid$right";16541655}else{1656$str=~m/^($endre)(.*)$/;1657my$body=$1;1658my$tail=$2;1659if(length($tail) >4) {1660$tail="... ";1661}1662return"$body$tail";1663}1664}16651666# takes the same arguments as chop_str, but also wraps a <span> around the1667# result with a title attribute if it does get chopped. Additionally, the1668# string is HTML-escaped.1669sub chop_and_escape_str {1670my($str) =@_;16711672my$chopped= chop_str(@_);1673if($choppedeq$str) {1674return esc_html($chopped);1675}else{1676$str=~s/[[:cntrl:]]/?/g;1677return$cgi->span({-title=>$str}, esc_html($chopped));1678}1679}16801681## ----------------------------------------------------------------------1682## functions returning short strings16831684# CSS class for given age value (in seconds)1685sub age_class {1686my$age=shift;16871688if(!defined$age) {1689return"noage";1690}elsif($age<60*60*2) {1691return"age0";1692}elsif($age<60*60*24*2) {1693return"age1";1694}else{1695return"age2";1696}1697}16981699# convert age in seconds to "nn units ago" string1700sub age_string {1701my$age=shift;1702my$age_str;17031704if($age>60*60*24*365*2) {1705$age_str= (int$age/60/60/24/365);1706$age_str.=" years ago";1707}elsif($age>60*60*24*(365/12)*2) {1708$age_str=int$age/60/60/24/(365/12);1709$age_str.=" months ago";1710}elsif($age>60*60*24*7*2) {1711$age_str=int$age/60/60/24/7;1712$age_str.=" weeks ago";1713}elsif($age>60*60*24*2) {1714$age_str=int$age/60/60/24;1715$age_str.=" days ago";1716}elsif($age>60*60*2) {1717$age_str=int$age/60/60;1718$age_str.=" hours ago";1719}elsif($age>60*2) {1720$age_str=int$age/60;1721$age_str.=" min ago";1722}elsif($age>2) {1723$age_str=int$age;1724$age_str.=" sec ago";1725}else{1726$age_str.=" right now";1727}1728return$age_str;1729}17301731useconstant{1732 S_IFINVALID =>0030000,1733 S_IFGITLINK =>0160000,1734};17351736# submodule/subproject, a commit object reference1737sub S_ISGITLINK {1738my$mode=shift;17391740return(($mode& S_IFMT) == S_IFGITLINK)1741}17421743# convert file mode in octal to symbolic file mode string1744sub mode_str {1745my$mode=oct shift;17461747if(S_ISGITLINK($mode)) {1748return'm---------';1749}elsif(S_ISDIR($mode& S_IFMT)) {1750return'drwxr-xr-x';1751}elsif(S_ISLNK($mode)) {1752return'lrwxrwxrwx';1753}elsif(S_ISREG($mode)) {1754# git cares only about the executable bit1755if($mode& S_IXUSR) {1756return'-rwxr-xr-x';1757}else{1758return'-rw-r--r--';1759};1760}else{1761return'----------';1762}1763}17641765# convert file mode in octal to file type string1766sub file_type {1767my$mode=shift;17681769if($mode!~m/^[0-7]+$/) {1770return$mode;1771}else{1772$mode=oct$mode;1773}17741775if(S_ISGITLINK($mode)) {1776return"submodule";1777}elsif(S_ISDIR($mode& S_IFMT)) {1778return"directory";1779}elsif(S_ISLNK($mode)) {1780return"symlink";1781}elsif(S_ISREG($mode)) {1782return"file";1783}else{1784return"unknown";1785}1786}17871788# convert file mode in octal to file type description string1789sub file_type_long {1790my$mode=shift;17911792if($mode!~m/^[0-7]+$/) {1793return$mode;1794}else{1795$mode=oct$mode;1796}17971798if(S_ISGITLINK($mode)) {1799return"submodule";1800}elsif(S_ISDIR($mode& S_IFMT)) {1801return"directory";1802}elsif(S_ISLNK($mode)) {1803return"symlink";1804}elsif(S_ISREG($mode)) {1805if($mode& S_IXUSR) {1806return"executable";1807}else{1808return"file";1809};1810}else{1811return"unknown";1812}1813}181418151816## ----------------------------------------------------------------------1817## functions returning short HTML fragments, or transforming HTML fragments1818## which don't belong to other sections18191820# format line of commit message.1821sub format_log_line_html {1822my$line=shift;18231824$line= esc_html($line, -nbsp=>1);1825$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1826$cgi->a({-href => href(action=>"object", hash=>$1),1827-class=>"text"},$1);1828}eg;18291830return$line;1831}18321833# format marker of refs pointing to given object18341835# the destination action is chosen based on object type and current context:1836# - for annotated tags, we choose the tag view unless it's the current view1837# already, in which case we go to shortlog view1838# - for other refs, we keep the current view if we're in history, shortlog or1839# log view, and select shortlog otherwise1840sub format_ref_marker {1841my($refs,$id) =@_;1842my$markers='';18431844if(defined$refs->{$id}) {1845foreachmy$ref(@{$refs->{$id}}) {1846# this code exploits the fact that non-lightweight tags are the1847# only indirect objects, and that they are the only objects for which1848# we want to use tag instead of shortlog as action1849my($type,$name) =qw();1850my$indirect= ($ref=~s/\^\{\}$//);1851# e.g. tags/v2.6.11 or heads/next1852if($ref=~m!^(.*?)s?/(.*)$!) {1853$type=$1;1854$name=$2;1855}else{1856$type="ref";1857$name=$ref;1858}18591860my$class=$type;1861$class.=" indirect"if$indirect;18621863my$dest_action="shortlog";18641865if($indirect) {1866$dest_action="tag"unless$actioneq"tag";1867}elsif($action=~/^(history|(short)?log)$/) {1868$dest_action=$action;1869}18701871my$dest="";1872$dest.="refs/"unless$ref=~ m!^refs/!;1873$dest.=$ref;18741875my$link=$cgi->a({1876-href => href(1877 action=>$dest_action,1878 hash=>$dest1879)},$name);18801881$markers.=" <span class=\"".esc_attr($class)."\"title=\"".esc_attr($ref)."\">".1882$link."</span>";1883}1884}18851886if($markers) {1887return' <span class="refs">'.$markers.'</span>';1888}else{1889return"";1890}1891}18921893# format, perhaps shortened and with markers, title line1894sub format_subject_html {1895my($long,$short,$href,$extra) =@_;1896$extra=''unlessdefined($extra);18971898if(length($short) <length($long)) {1899$long=~s/[[:cntrl:]]/?/g;1900return$cgi->a({-href =>$href, -class=>"list subject",1901-title => to_utf8($long)},1902 esc_html($short)) .$extra;1903}else{1904return$cgi->a({-href =>$href, -class=>"list subject"},1905 esc_html($long)) .$extra;1906}1907}19081909# Rather than recomputing the url for an email multiple times, we cache it1910# after the first hit. This gives a visible benefit in views where the avatar1911# for the same email is used repeatedly (e.g. shortlog).1912# The cache is shared by all avatar engines (currently gravatar only), which1913# are free to use it as preferred. Since only one avatar engine is used for any1914# given page, there's no risk for cache conflicts.1915our%avatar_cache= ();19161917# Compute the picon url for a given email, by using the picon search service over at1918# http://www.cs.indiana.edu/picons/search.html1919sub picon_url {1920my$email=lc shift;1921if(!$avatar_cache{$email}) {1922my($user,$domain) =split('@',$email);1923$avatar_cache{$email} =1924"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1925"$domain/$user/".1926"users+domains+unknown/up/single";1927}1928return$avatar_cache{$email};1929}19301931# Compute the gravatar url for a given email, if it's not in the cache already.1932# Gravatar stores only the part of the URL before the size, since that's the1933# one computationally more expensive. This also allows reuse of the cache for1934# different sizes (for this particular engine).1935sub gravatar_url {1936my$email=lc shift;1937my$size=shift;1938$avatar_cache{$email} ||=1939"http://www.gravatar.com/avatar/".1940 Digest::MD5::md5_hex($email) ."?s=";1941return$avatar_cache{$email} .$size;1942}19431944# Insert an avatar for the given $email at the given $size if the feature1945# is enabled.1946sub git_get_avatar {1947my($email,%opts) =@_;1948my$pre_white= ($opts{-pad_before} ?" ":"");1949my$post_white= ($opts{-pad_after} ?" ":"");1950$opts{-size} ||='default';1951my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1952my$url="";1953if($git_avatareq'gravatar') {1954$url= gravatar_url($email,$size);1955}elsif($git_avatareq'picon') {1956$url= picon_url($email);1957}1958# Other providers can be added by extending the if chain, defining $url1959# as needed. If no variant puts something in $url, we assume avatars1960# are completely disabled/unavailable.1961if($url) {1962return$pre_white.1963"<img width=\"$size\"".1964"class=\"avatar\"".1965"src=\"".esc_url($url)."\"".1966"alt=\"\"".1967"/>".$post_white;1968}else{1969return"";1970}1971}19721973sub format_search_author {1974my($author,$searchtype,$displaytext) =@_;1975my$have_search= gitweb_check_feature('search');19761977if($have_search) {1978my$performed="";1979if($searchtypeeq'author') {1980$performed="authored";1981}elsif($searchtypeeq'committer') {1982$performed="committed";1983}19841985return$cgi->a({-href => href(action=>"search", hash=>$hash,1986 searchtext=>$author,1987 searchtype=>$searchtype),class=>"list",1988 title=>"Search for commits$performedby$author"},1989$displaytext);19901991}else{1992return$displaytext;1993}1994}19951996# format the author name of the given commit with the given tag1997# the author name is chopped and escaped according to the other1998# optional parameters (see chop_str).1999sub format_author_html {2000my$tag=shift;2001my$co=shift;2002my$author= chop_and_escape_str($co->{'author_name'},@_);2003return"<$tagclass=\"author\">".2004 format_search_author($co->{'author_name'},"author",2005 git_get_avatar($co->{'author_email'}, -pad_after =>1) .2006$author) .2007"</$tag>";2008}20092010# format git diff header line, i.e. "diff --(git|combined|cc) ..."2011sub format_git_diff_header_line {2012my$line=shift;2013my$diffinfo=shift;2014my($from,$to) =@_;20152016if($diffinfo->{'nparents'}) {2017# combined diff2018$line=~s!^(diff (.*?) )"?.*$!$1!;2019if($to->{'href'}) {2020$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2021 esc_path($to->{'file'}));2022}else{# file was deleted (no href)2023$line.= esc_path($to->{'file'});2024}2025}else{2026# "ordinary" diff2027$line=~s!^(diff (.*?) )"?a/.*$!$1!;2028if($from->{'href'}) {2029$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},2030'a/'. esc_path($from->{'file'}));2031}else{# file was added (no href)2032$line.='a/'. esc_path($from->{'file'});2033}2034$line.=' ';2035if($to->{'href'}) {2036$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2037'b/'. esc_path($to->{'file'}));2038}else{# file was deleted2039$line.='b/'. esc_path($to->{'file'});2040}2041}20422043return"<div class=\"diff header\">$line</div>\n";2044}20452046# format extended diff header line, before patch itself2047sub format_extended_diff_header_line {2048my$line=shift;2049my$diffinfo=shift;2050my($from,$to) =@_;20512052# match <path>2053if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {2054$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},2055 esc_path($from->{'file'}));2056}2057if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {2058$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},2059 esc_path($to->{'file'}));2060}2061# match single <mode>2062if($line=~m/\s(\d{6})$/) {2063$line.='<span class="info"> ('.2064 file_type_long($1) .2065')</span>';2066}2067# match <hash>2068if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {2069# can match only for combined diff2070$line='index ';2071for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2072if($from->{'href'}[$i]) {2073$line.=$cgi->a({-href=>$from->{'href'}[$i],2074-class=>"hash"},2075substr($diffinfo->{'from_id'}[$i],0,7));2076}else{2077$line.='0' x 7;2078}2079# separator2080$line.=','if($i<$diffinfo->{'nparents'} -1);2081}2082$line.='..';2083if($to->{'href'}) {2084$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2085substr($diffinfo->{'to_id'},0,7));2086}else{2087$line.='0' x 7;2088}20892090}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {2091# can match only for ordinary diff2092my($from_link,$to_link);2093if($from->{'href'}) {2094$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},2095substr($diffinfo->{'from_id'},0,7));2096}else{2097$from_link='0' x 7;2098}2099if($to->{'href'}) {2100$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2101substr($diffinfo->{'to_id'},0,7));2102}else{2103$to_link='0' x 7;2104}2105my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});2106$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;2107}21082109return$line."<br/>\n";2110}21112112# format from-file/to-file diff header2113sub format_diff_from_to_header {2114my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;2115my$line;2116my$result='';21172118$line=$from_line;2119#assert($line =~ m/^---/) if DEBUG;2120# no extra formatting for "^--- /dev/null"2121if(!$diffinfo->{'nparents'}) {2122# ordinary (single parent) diff2123if($line=~m!^--- "?a/!) {2124if($from->{'href'}) {2125$line='--- a/'.2126$cgi->a({-href=>$from->{'href'}, -class=>"path"},2127 esc_path($from->{'file'}));2128}else{2129$line='--- a/'.2130 esc_path($from->{'file'});2131}2132}2133$result.= qq!<div class="diff from_file">$line</div>\n!;21342135}else{2136# combined diff (merge commit)2137for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2138if($from->{'href'}[$i]) {2139$line='--- '.2140$cgi->a({-href=>href(action=>"blobdiff",2141 hash_parent=>$diffinfo->{'from_id'}[$i],2142 hash_parent_base=>$parents[$i],2143 file_parent=>$from->{'file'}[$i],2144 hash=>$diffinfo->{'to_id'},2145 hash_base=>$hash,2146 file_name=>$to->{'file'}),2147-class=>"path",2148-title=>"diff". ($i+1)},2149$i+1) .2150'/'.2151$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2152 esc_path($from->{'file'}[$i]));2153}else{2154$line='--- /dev/null';2155}2156$result.= qq!<div class="diff from_file">$line</div>\n!;2157}2158}21592160$line=$to_line;2161#assert($line =~ m/^\+\+\+/) if DEBUG;2162# no extra formatting for "^+++ /dev/null"2163if($line=~m!^\+\+\+ "?b/!) {2164if($to->{'href'}) {2165$line='+++ b/'.2166$cgi->a({-href=>$to->{'href'}, -class=>"path"},2167 esc_path($to->{'file'}));2168}else{2169$line='+++ b/'.2170 esc_path($to->{'file'});2171}2172}2173$result.= qq!<div class="diff to_file">$line</div>\n!;21742175return$result;2176}21772178# create note for patch simplified by combined diff2179sub format_diff_cc_simplified {2180my($diffinfo,@parents) =@_;2181my$result='';21822183$result.="<div class=\"diff header\">".2184"diff --cc ";2185if(!is_deleted($diffinfo)) {2186$result.=$cgi->a({-href => href(action=>"blob",2187 hash_base=>$hash,2188 hash=>$diffinfo->{'to_id'},2189 file_name=>$diffinfo->{'to_file'}),2190-class=>"path"},2191 esc_path($diffinfo->{'to_file'}));2192}else{2193$result.= esc_path($diffinfo->{'to_file'});2194}2195$result.="</div>\n".# class="diff header"2196"<div class=\"diff nodifferences\">".2197"Simple merge".2198"</div>\n";# class="diff nodifferences"21992200return$result;2201}22022203# format patch (diff) line (not to be used for diff headers)2204sub format_diff_line {2205my$line=shift;2206my($from,$to) =@_;2207my$diff_class="";22082209chomp$line;22102211if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2212# combined diff2213my$prefix=substr($line,0,scalar@{$from->{'href'}});2214if($line=~m/^\@{3}/) {2215$diff_class=" chunk_header";2216}elsif($line=~m/^\\/) {2217$diff_class=" incomplete";2218}elsif($prefix=~tr/+/+/) {2219$diff_class=" add";2220}elsif($prefix=~tr/-/-/) {2221$diff_class=" rem";2222}2223}else{2224# assume ordinary diff2225my$char=substr($line,0,1);2226if($chareq'+') {2227$diff_class=" add";2228}elsif($chareq'-') {2229$diff_class=" rem";2230}elsif($chareq'@') {2231$diff_class=" chunk_header";2232}elsif($chareq"\\") {2233$diff_class=" incomplete";2234}2235}2236$line= untabify($line);2237if($from&&$to&&$line=~m/^\@{2} /) {2238my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2239$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;22402241$from_lines=0unlessdefined$from_lines;2242$to_lines=0unlessdefined$to_lines;22432244if($from->{'href'}) {2245$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2246-class=>"list"},$from_text);2247}2248if($to->{'href'}) {2249$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2250-class=>"list"},$to_text);2251}2252$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2253"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2254return"<div class=\"diff$diff_class\">$line</div>\n";2255}elsif($from&&$to&&$line=~m/^\@{3}/) {2256my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2257my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);22582259@from_text=split(' ',$ranges);2260for(my$i=0;$i<@from_text; ++$i) {2261($from_start[$i],$from_nlines[$i]) =2262(split(',',substr($from_text[$i],1)),0);2263}22642265$to_text=pop@from_text;2266$to_start=pop@from_start;2267$to_nlines=pop@from_nlines;22682269$line="<span class=\"chunk_info\">$prefix";2270for(my$i=0;$i<@from_text; ++$i) {2271if($from->{'href'}[$i]) {2272$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2273-class=>"list"},$from_text[$i]);2274}else{2275$line.=$from_text[$i];2276}2277$line.=" ";2278}2279if($to->{'href'}) {2280$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2281-class=>"list"},$to_text);2282}else{2283$line.=$to_text;2284}2285$line.="$prefix</span>".2286"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2287return"<div class=\"diff$diff_class\">$line</div>\n";2288}2289return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2290}22912292# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2293# linked. Pass the hash of the tree/commit to snapshot.2294sub format_snapshot_links {2295my($hash) =@_;2296my$num_fmts=@snapshot_fmts;2297if($num_fmts>1) {2298# A parenthesized list of links bearing format names.2299# e.g. "snapshot (_tar.gz_ _zip_)"2300return"snapshot (".join(' ',map2301$cgi->a({2302-href => href(2303 action=>"snapshot",2304 hash=>$hash,2305 snapshot_format=>$_2306)2307},$known_snapshot_formats{$_}{'display'})2308,@snapshot_fmts) .")";2309}elsif($num_fmts==1) {2310# A single "snapshot" link whose tooltip bears the format name.2311# i.e. "_snapshot_"2312my($fmt) =@snapshot_fmts;2313return2314$cgi->a({2315-href => href(2316 action=>"snapshot",2317 hash=>$hash,2318 snapshot_format=>$fmt2319),2320-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2321},"snapshot");2322}else{# $num_fmts == 02323returnundef;2324}2325}23262327## ......................................................................2328## functions returning values to be passed, perhaps after some2329## transformation, to other functions; e.g. returning arguments to href()23302331# returns hash to be passed to href to generate gitweb URL2332# in -title key it returns description of link2333sub get_feed_info {2334my$format=shift||'Atom';2335my%res= (action =>lc($format));23362337# feed links are possible only for project views2338return unless(defined$project);2339# some views should link to OPML, or to generic project feed,2340# or don't have specific feed yet (so they should use generic)2341return if($action=~/^(?:tags|heads|forks|tag|search)$/x);23422343my$branch;2344# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2345# from tag links; this also makes possible to detect branch links2346if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2347(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2348$branch=$1;2349}2350# find log type for feed description (title)2351my$type='log';2352if(defined$file_name) {2353$type="history of$file_name";2354$type.="/"if($actioneq'tree');2355$type.=" on '$branch'"if(defined$branch);2356}else{2357$type="log of$branch"if(defined$branch);2358}23592360$res{-title} =$type;2361$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2362$res{'file_name'} =$file_name;23632364return%res;2365}23662367## ----------------------------------------------------------------------2368## git utility subroutines, invoking git commands23692370# returns path to the core git executable and the --git-dir parameter as list2371sub git_cmd {2372$number_of_git_cmds++;2373return$GIT,'--git-dir='.$git_dir;2374}23752376# quote the given arguments for passing them to the shell2377# quote_command("command", "arg 1", "arg with ' and ! characters")2378# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2379# Try to avoid using this function wherever possible.2380sub quote_command {2381returnjoin(' ',2382map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2383}23842385# get HEAD ref of given project as hash2386sub git_get_head_hash {2387return git_get_full_hash(shift,'HEAD');2388}23892390sub git_get_full_hash {2391return git_get_hash(@_);2392}23932394sub git_get_short_hash {2395return git_get_hash(@_,'--short=7');2396}23972398sub git_get_hash {2399my($project,$hash,@options) =@_;2400my$o_git_dir=$git_dir;2401my$retval=undef;2402$git_dir="$projectroot/$project";2403if(open my$fd,'-|', git_cmd(),'rev-parse',2404'--verify','-q',@options,$hash) {2405$retval= <$fd>;2406chomp$retvalifdefined$retval;2407close$fd;2408}2409if(defined$o_git_dir) {2410$git_dir=$o_git_dir;2411}2412return$retval;2413}24142415# get type of given object2416sub git_get_type {2417my$hash=shift;24182419open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2420my$type= <$fd>;2421close$fdorreturn;2422chomp$type;2423return$type;2424}24252426# repository configuration2427our$config_file='';2428our%config;24292430# store multiple values for single key as anonymous array reference2431# single values stored directly in the hash, not as [ <value> ]2432sub hash_set_multi {2433my($hash,$key,$value) =@_;24342435if(!exists$hash->{$key}) {2436$hash->{$key} =$value;2437}elsif(!ref$hash->{$key}) {2438$hash->{$key} = [$hash->{$key},$value];2439}else{2440push@{$hash->{$key}},$value;2441}2442}24432444# return hash of git project configuration2445# optionally limited to some section, e.g. 'gitweb'2446sub git_parse_project_config {2447my$section_regexp=shift;2448my%config;24492450local$/="\0";24512452open my$fh,"-|", git_cmd(),"config",'-z','-l',2453orreturn;24542455while(my$keyval= <$fh>) {2456chomp$keyval;2457my($key,$value) =split(/\n/,$keyval,2);24582459 hash_set_multi(\%config,$key,$value)2460if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2461}2462close$fh;24632464return%config;2465}24662467# convert config value to boolean: 'true' or 'false'2468# no value, number > 0, 'true' and 'yes' values are true2469# rest of values are treated as false (never as error)2470sub config_to_bool {2471my$val=shift;24722473return1if!defined$val;# section.key24742475# strip leading and trailing whitespace2476$val=~s/^\s+//;2477$val=~s/\s+$//;24782479return(($val=~/^\d+$/&&$val) ||# section.key = 12480($val=~/^(?:true|yes)$/i));# section.key = true2481}24822483# convert config value to simple decimal number2484# an optional value suffix of 'k', 'm', or 'g' will cause the value2485# to be multiplied by 1024, 1048576, or 10737418242486sub config_to_int {2487my$val=shift;24882489# strip leading and trailing whitespace2490$val=~s/^\s+//;2491$val=~s/\s+$//;24922493if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2494$unit=lc($unit);2495# unknown unit is treated as 12496return$num* ($uniteq'g'?1073741824:2497$uniteq'm'?1048576:2498$uniteq'k'?1024:1);2499}2500return$val;2501}25022503# convert config value to array reference, if needed2504sub config_to_multi {2505my$val=shift;25062507returnref($val) ?$val: (defined($val) ? [$val] : []);2508}25092510sub git_get_project_config {2511my($key,$type) =@_;25122513return unlessdefined$git_dir;25142515# key sanity check2516return unless($key);2517$key=~s/^gitweb\.//;2518return if($key=~m/\W/);25192520# type sanity check2521if(defined$type) {2522$type=~s/^--//;2523$type=undef2524unless($typeeq'bool'||$typeeq'int');2525}25262527# get config2528if(!defined$config_file||2529$config_filene"$git_dir/config") {2530%config= git_parse_project_config('gitweb');2531$config_file="$git_dir/config";2532}25332534# check if config variable (key) exists2535return unlessexists$config{"gitweb.$key"};25362537# ensure given type2538if(!defined$type) {2539return$config{"gitweb.$key"};2540}elsif($typeeq'bool') {2541# backward compatibility: 'git config --bool' returns true/false2542return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2543}elsif($typeeq'int') {2544return config_to_int($config{"gitweb.$key"});2545}2546return$config{"gitweb.$key"};2547}25482549# get hash of given path at given ref2550sub git_get_hash_by_path {2551my$base=shift;2552my$path=shift||returnundef;2553my$type=shift;25542555$path=~ s,/+$,,;25562557open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2558or die_error(500,"Open git-ls-tree failed");2559my$line= <$fd>;2560close$fdorreturnundef;25612562if(!defined$line) {2563# there is no tree or hash given by $path at $base2564returnundef;2565}25662567#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2568$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2569if(defined$type&&$typene$2) {2570# type doesn't match2571returnundef;2572}2573return$3;2574}25752576# get path of entry with given hash at given tree-ish (ref)2577# used to get 'from' filename for combined diff (merge commit) for renames2578sub git_get_path_by_hash {2579my$base=shift||return;2580my$hash=shift||return;25812582local$/="\0";25832584open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2585orreturnundef;2586while(my$line= <$fd>) {2587chomp$line;25882589#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2590#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2591if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2592close$fd;2593return$1;2594}2595}2596close$fd;2597returnundef;2598}25992600## ......................................................................2601## git utility functions, directly accessing git repository26022603# get the value of config variable either from file named as the variable2604# itself in the repository ($GIT_DIR/$name file), or from gitweb.$name2605# configuration variable in the repository config file.2606sub git_get_file_or_project_config {2607my($path,$name) =@_;26082609$git_dir="$projectroot/$path";2610open my$fd,'<',"$git_dir/$name"2611orreturn git_get_project_config($name);2612my$conf= <$fd>;2613close$fd;2614if(defined$conf) {2615chomp$conf;2616}2617return$conf;2618}26192620sub git_get_project_description {2621my$path=shift;2622return git_get_file_or_project_config($path,'description');2623}26242625sub git_get_project_category {2626my$path=shift;2627return git_get_file_or_project_config($path,'category');2628}262926302631# supported formats:2632# * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)2633# - if its contents is a number, use it as tag weight,2634# - otherwise add a tag with weight 12635# * $GIT_DIR/ctags file, each line is a tag (with weight 1)2636# the same value multiple times increases tag weight2637# * `gitweb.ctag' multi-valued repo config variable2638sub git_get_project_ctags {2639my$project=shift;2640my$ctags= {};26412642$git_dir="$projectroot/$project";2643if(opendir my$dh,"$git_dir/ctags") {2644my@files=grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh);2645foreachmy$tagfile(@files) {2646open my$ct,'<',$tagfile2647ornext;2648my$val= <$ct>;2649chomp$valif$val;2650close$ct;26512652(my$ctag=$tagfile) =~ s#.*/##;2653if($val=~/^\d+$/) {2654$ctags->{$ctag} =$val;2655}else{2656$ctags->{$ctag} =1;2657}2658}2659closedir$dh;26602661}elsif(open my$fh,'<',"$git_dir/ctags") {2662while(my$line= <$fh>) {2663chomp$line;2664$ctags->{$line}++if$line;2665}2666close$fh;26672668}else{2669my$taglist= config_to_multi(git_get_project_config('ctag'));2670foreachmy$tag(@$taglist) {2671$ctags->{$tag}++;2672}2673}26742675return$ctags;2676}26772678# return hash, where keys are content tags ('ctags'),2679# and values are sum of weights of given tag in every project2680sub git_gather_all_ctags {2681my$projects=shift;2682my$ctags= {};26832684foreachmy$p(@$projects) {2685foreachmy$ct(keys%{$p->{'ctags'}}) {2686$ctags->{$ct} +=$p->{'ctags'}->{$ct};2687}2688}26892690return$ctags;2691}26922693sub git_populate_project_tagcloud {2694my$ctags=shift;26952696# First, merge different-cased tags; tags vote on casing2697my%ctags_lc;2698foreach(keys%$ctags) {2699$ctags_lc{lc$_}->{count} +=$ctags->{$_};2700if(not$ctags_lc{lc$_}->{topcount}2701or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2702$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2703$ctags_lc{lc$_}->{topname} =$_;2704}2705}27062707my$cloud;2708my$matched=$cgi->param('by_tag');2709if(eval{require HTML::TagCloud;1; }) {2710$cloud= HTML::TagCloud->new;2711foreachmy$ctag(sort keys%ctags_lc) {2712# Pad the title with spaces so that the cloud looks2713# less crammed.2714my$title= esc_html($ctags_lc{$ctag}->{topname});2715$title=~s/ / /g;2716$title=~s/^/ /g;2717$title=~s/$/ /g;2718if(defined$matched&&$matchedeq$ctag) {2719$title=qq(<span class="match">$title</span>);2720}2721$cloud->add($title, href(project=>undef, ctag=>$ctag),2722$ctags_lc{$ctag}->{count});2723}2724}else{2725$cloud= {};2726foreachmy$ctag(keys%ctags_lc) {2727my$title= esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);2728if(defined$matched&&$matchedeq$ctag) {2729$title=qq(<span class="match">$title</span>);2730}2731$cloud->{$ctag}{count} =$ctags_lc{$ctag}->{count};2732$cloud->{$ctag}{ctag} =2733$cgi->a({-href=>href(project=>undef, ctag=>$ctag)},$title);2734}2735}2736return$cloud;2737}27382739sub git_show_project_tagcloud {2740my($cloud,$count) =@_;2741if(ref$cloudeq'HTML::TagCloud') {2742return$cloud->html_and_css($count);2743}else{2744my@tags=sort{$cloud->{$a}->{'count'} <=>$cloud->{$b}->{'count'} }keys%$cloud;2745return2746'<div id="htmltagcloud"'.($project?'':' align="center"').'>'.2747join(', ',map{2748$cloud->{$_}->{'ctag'}2749}splice(@tags,0,$count)) .2750'</div>';2751}2752}27532754sub git_get_project_url_list {2755my$path=shift;27562757$git_dir="$projectroot/$path";2758open my$fd,'<',"$git_dir/cloneurl"2759orreturnwantarray?2760@{ config_to_multi(git_get_project_config('url')) } :2761 config_to_multi(git_get_project_config('url'));2762my@git_project_url_list=map{chomp;$_} <$fd>;2763close$fd;27642765returnwantarray?@git_project_url_list: \@git_project_url_list;2766}27672768sub git_get_projects_list {2769my$filter=shift||'';2770my@list;27712772$filter=~s/\.git$//;27732774if(-d $projects_list) {2775# search in directory2776my$dir=$projects_list;2777# remove the trailing "/"2778$dir=~s!/+$!!;2779my$pfxlen=length("$projects_list");2780my$pfxdepth= ($projects_list=~tr!/!!);2781# when filtering, search only given subdirectory2782if($filter) {2783$dir.="/$filter";2784$dir=~s!/+$!!;2785}27862787 File::Find::find({2788 follow_fast =>1,# follow symbolic links2789 follow_skip =>2,# ignore duplicates2790 dangling_symlinks =>0,# ignore dangling symlinks, silently2791 wanted =>sub{2792# global variables2793our$project_maxdepth;2794our$projectroot;2795# skip project-list toplevel, if we get it.2796return if(m!^[/.]$!);2797# only directories can be git repositories2798return unless(-d $_);2799# don't traverse too deep (Find is super slow on os x)2800# $project_maxdepth excludes depth of $projectroot2801if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2802$File::Find::prune =1;2803return;2804}28052806my$path=substr($File::Find::name,$pfxlen+1);2807# we check related file in $projectroot2808if(check_export_ok("$projectroot/$path")) {2809push@list, { path =>$path};2810$File::Find::prune =1;2811}2812},2813},"$dir");28142815}elsif(-f $projects_list) {2816# read from file(url-encoded):2817# 'git%2Fgit.git Linus+Torvalds'2818# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2819# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2820open my$fd,'<',$projects_listorreturn;2821 PROJECT:2822while(my$line= <$fd>) {2823chomp$line;2824my($path,$owner) =split' ',$line;2825$path= unescape($path);2826$owner= unescape($owner);2827if(!defined$path) {2828next;2829}2830# if $filter is rpovided, check if $path begins with $filter2831if($filter&&$path!~m!^\Q$filter\E/!) {2832next;2833}2834if(check_export_ok("$projectroot/$path")) {2835my$pr= {2836 path =>$path,2837 owner => to_utf8($owner),2838};2839push@list,$pr;2840}2841}2842close$fd;2843}2844return@list;2845}28462847# written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)2848# as side effects it sets 'forks' field to list of forks for forked projects2849sub filter_forks_from_projects_list {2850my$projects=shift;28512852my%trie;# prefix tree of directories (path components)2853# generate trie out of those directories that might contain forks2854foreachmy$pr(@$projects) {2855my$path=$pr->{'path'};2856$path=~s/\.git$//;# forks of 'repo.git' are in 'repo/' directory2857next if($path=~m!/$!);# skip non-bare repositories, e.g. 'repo/.git'2858next unless($path);# skip '.git' repository: tests, git-instaweb2859next unless(-d $path);# containing directory exists2860$pr->{'forks'} = [];# there can be 0 or more forks of project28612862# add to trie2863my@dirs=split('/',$path);2864# walk the trie, until either runs out of components or out of trie2865my$ref= \%trie;2866while(scalar@dirs&&2867exists($ref->{$dirs[0]})) {2868$ref=$ref->{shift@dirs};2869}2870# create rest of trie structure from rest of components2871foreachmy$dir(@dirs) {2872$ref=$ref->{$dir} = {};2873}2874# create end marker, store $pr as a data2875$ref->{''} =$prif(!exists$ref->{''});2876}28772878# filter out forks, by finding shortest prefix match for paths2879my@filtered;2880 PROJECT:2881foreachmy$pr(@$projects) {2882# trie lookup2883my$ref= \%trie;2884 DIR:2885foreachmy$dir(split('/',$pr->{'path'})) {2886if(exists$ref->{''}) {2887# found [shortest] prefix, is a fork - skip it2888push@{$ref->{''}{'forks'}},$pr;2889next PROJECT;2890}2891if(!exists$ref->{$dir}) {2892# not in trie, cannot have prefix, not a fork2893push@filtered,$pr;2894next PROJECT;2895}2896# If the dir is there, we just walk one step down the trie.2897$ref=$ref->{$dir};2898}2899# we ran out of trie2900# (shouldn't happen: it's either no match, or end marker)2901push@filtered,$pr;2902}29032904return@filtered;2905}29062907# note: fill_project_list_info must be run first,2908# for 'descr_long' and 'ctags' to be filled2909sub search_projects_list {2910my($projlist,%opts) =@_;2911my$tagfilter=$opts{'tagfilter'};2912my$searchtext=$opts{'searchtext'};29132914return@$projlist2915unless($tagfilter||$searchtext);29162917my@projects;2918 PROJECT:2919foreachmy$pr(@$projlist) {29202921if($tagfilter) {2922next unlessref($pr->{'ctags'})eq'HASH';2923next unless2924grep{lc($_)eq lc($tagfilter) }keys%{$pr->{'ctags'}};2925}29262927if($searchtext) {2928next unless2929$pr->{'path'} =~/$searchtext/||2930$pr->{'descr_long'} =~/$searchtext/;2931}29322933push@projects,$pr;2934}29352936return@projects;2937}29382939our$gitweb_project_owner=undef;2940sub git_get_project_list_from_file {29412942return if(defined$gitweb_project_owner);29432944$gitweb_project_owner= {};2945# read from file (url-encoded):2946# 'git%2Fgit.git Linus+Torvalds'2947# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2948# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2949if(-f $projects_list) {2950open(my$fd,'<',$projects_list);2951while(my$line= <$fd>) {2952chomp$line;2953my($pr,$ow) =split' ',$line;2954$pr= unescape($pr);2955$ow= unescape($ow);2956$gitweb_project_owner->{$pr} = to_utf8($ow);2957}2958close$fd;2959}2960}29612962sub git_get_project_owner {2963my$project=shift;2964my$owner;29652966returnundefunless$project;2967$git_dir="$projectroot/$project";29682969if(!defined$gitweb_project_owner) {2970 git_get_project_list_from_file();2971}29722973if(exists$gitweb_project_owner->{$project}) {2974$owner=$gitweb_project_owner->{$project};2975}2976if(!defined$owner){2977$owner= git_get_project_config('owner');2978}2979if(!defined$owner) {2980$owner= get_file_owner("$git_dir");2981}29822983return$owner;2984}29852986sub git_get_last_activity {2987my($path) =@_;2988my$fd;29892990$git_dir="$projectroot/$path";2991open($fd,"-|", git_cmd(),'for-each-ref',2992'--format=%(committer)',2993'--sort=-committerdate',2994'--count=1',2995'refs/heads')orreturn;2996my$most_recent= <$fd>;2997close$fdorreturn;2998if(defined$most_recent&&2999$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {3000my$timestamp=$1;3001my$age=time-$timestamp;3002return($age, age_string($age));3003}3004return(undef,undef);3005}30063007# Implementation note: when a single remote is wanted, we cannot use 'git3008# remote show -n' because that command always work (assuming it's a remote URL3009# if it's not defined), and we cannot use 'git remote show' because that would3010# try to make a network roundtrip. So the only way to find if that particular3011# remote is defined is to walk the list provided by 'git remote -v' and stop if3012# and when we find what we want.3013sub git_get_remotes_list {3014my$wanted=shift;3015my%remotes= ();30163017open my$fd,'-|', git_cmd(),'remote','-v';3018return unless$fd;3019while(my$remote= <$fd>) {3020chomp$remote;3021$remote=~s!\t(.*?)\s+\((\w+)\)$!!;3022next if$wantedand not$remoteeq$wanted;3023my($url,$key) = ($1,$2);30243025$remotes{$remote} ||= {'heads'=> () };3026$remotes{$remote}{$key} =$url;3027}3028close$fdorreturn;3029returnwantarray?%remotes: \%remotes;3030}30313032# Takes a hash of remotes as first parameter and fills it by adding the3033# available remote heads for each of the indicated remotes.3034sub fill_remote_heads {3035my$remotes=shift;3036my@heads=map{"remotes/$_"}keys%$remotes;3037my@remoteheads= git_get_heads_list(undef,@heads);3038foreachmy$remote(keys%$remotes) {3039$remotes->{$remote}{'heads'} = [grep{3040$_->{'name'} =~s!^$remote/!!3041}@remoteheads];3042}3043}30443045sub git_get_references {3046my$type=shift||"";3047my%refs;3048# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.113049# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}3050open my$fd,"-|", git_cmd(),"show-ref","--dereference",3051($type? ("--","refs/$type") : ())# use -- <pattern> if $type3052orreturn;30533054while(my$line= <$fd>) {3055chomp$line;3056if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {3057if(defined$refs{$1}) {3058push@{$refs{$1}},$2;3059}else{3060$refs{$1} = [$2];3061}3062}3063}3064close$fdorreturn;3065return \%refs;3066}30673068sub git_get_rev_name_tags {3069my$hash=shift||returnundef;30703071open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash3072orreturn;3073my$name_rev= <$fd>;3074close$fd;30753076if($name_rev=~ m|^$hash tags/(.*)$|) {3077return$1;3078}else{3079# catches also '$hash undefined' output3080returnundef;3081}3082}30833084## ----------------------------------------------------------------------3085## parse to hash functions30863087sub parse_date {3088my$epoch=shift;3089my$tz=shift||"-0000";30903091my%date;3092my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");3093my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");3094my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);3095$date{'hour'} =$hour;3096$date{'minute'} =$min;3097$date{'mday'} =$mday;3098$date{'day'} =$days[$wday];3099$date{'month'} =$months[$mon];3100$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",3101$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;3102$date{'mday-time'} =sprintf"%d%s%02d:%02d",3103$mday,$months[$mon],$hour,$min;3104$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",31051900+$year,1+$mon,$mday,$hour,$min,$sec;31063107my($tz_sign,$tz_hour,$tz_min) =3108($tz=~m/^([-+])(\d\d)(\d\d)$/);3109$tz_sign= ($tz_signeq'-'? -1: +1);3110my$local=$epoch+$tz_sign*((($tz_hour*60) +$tz_min)*60);3111($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);3112$date{'hour_local'} =$hour;3113$date{'minute_local'} =$min;3114$date{'tz_local'} =$tz;3115$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",31161900+$year,$mon+1,$mday,3117$hour,$min,$sec,$tz);3118return%date;3119}31203121sub parse_tag {3122my$tag_id=shift;3123my%tag;3124my@comment;31253126open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;3127$tag{'id'} =$tag_id;3128while(my$line= <$fd>) {3129chomp$line;3130if($line=~m/^object ([0-9a-fA-F]{40})$/) {3131$tag{'object'} =$1;3132}elsif($line=~m/^type (.+)$/) {3133$tag{'type'} =$1;3134}elsif($line=~m/^tag (.+)$/) {3135$tag{'name'} =$1;3136}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {3137$tag{'author'} =$1;3138$tag{'author_epoch'} =$2;3139$tag{'author_tz'} =$3;3140if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {3141$tag{'author_name'} =$1;3142$tag{'author_email'} =$2;3143}else{3144$tag{'author_name'} =$tag{'author'};3145}3146}elsif($line=~m/--BEGIN/) {3147push@comment,$line;3148last;3149}elsif($lineeq"") {3150last;3151}3152}3153push@comment, <$fd>;3154$tag{'comment'} = \@comment;3155close$fdorreturn;3156if(!defined$tag{'name'}) {3157return3158};3159return%tag3160}31613162sub parse_commit_text {3163my($commit_text,$withparents) =@_;3164my@commit_lines=split'\n',$commit_text;3165my%co;31663167pop@commit_lines;# Remove '\0'31683169if(!@commit_lines) {3170return;3171}31723173my$header=shift@commit_lines;3174if($header!~m/^[0-9a-fA-F]{40}/) {3175return;3176}3177($co{'id'},my@parents) =split' ',$header;3178while(my$line=shift@commit_lines) {3179last if$lineeq"\n";3180if($line=~m/^tree ([0-9a-fA-F]{40})$/) {3181$co{'tree'} =$1;3182}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {3183push@parents,$1;3184}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {3185$co{'author'} = to_utf8($1);3186$co{'author_epoch'} =$2;3187$co{'author_tz'} =$3;3188if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {3189$co{'author_name'} =$1;3190$co{'author_email'} =$2;3191}else{3192$co{'author_name'} =$co{'author'};3193}3194}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {3195$co{'committer'} = to_utf8($1);3196$co{'committer_epoch'} =$2;3197$co{'committer_tz'} =$3;3198if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {3199$co{'committer_name'} =$1;3200$co{'committer_email'} =$2;3201}else{3202$co{'committer_name'} =$co{'committer'};3203}3204}3205}3206if(!defined$co{'tree'}) {3207return;3208};3209$co{'parents'} = \@parents;3210$co{'parent'} =$parents[0];32113212foreachmy$title(@commit_lines) {3213$title=~s/^ //;3214if($titlene"") {3215$co{'title'} = chop_str($title,80,5);3216# remove leading stuff of merges to make the interesting part visible3217if(length($title) >50) {3218$title=~s/^Automatic //;3219$title=~s/^merge (of|with) /Merge ... /i;3220if(length($title) >50) {3221$title=~s/(http|rsync):\/\///;3222}3223if(length($title) >50) {3224$title=~s/(master|www|rsync)\.//;3225}3226if(length($title) >50) {3227$title=~s/kernel.org:?//;3228}3229if(length($title) >50) {3230$title=~s/\/pub\/scm//;3231}3232}3233$co{'title_short'} = chop_str($title,50,5);3234last;3235}3236}3237if(!defined$co{'title'} ||$co{'title'}eq"") {3238$co{'title'} =$co{'title_short'} ='(no commit message)';3239}3240# remove added spaces3241foreachmy$line(@commit_lines) {3242$line=~s/^ //;3243}3244$co{'comment'} = \@commit_lines;32453246my$age=time-$co{'committer_epoch'};3247$co{'age'} =$age;3248$co{'age_string'} = age_string($age);3249my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});3250if($age>60*60*24*7*2) {3251$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3252$co{'age_string_age'} =$co{'age_string'};3253}else{3254$co{'age_string_date'} =$co{'age_string'};3255$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3256}3257return%co;3258}32593260sub parse_commit {3261my($commit_id) =@_;3262my%co;32633264local$/="\0";32653266open my$fd,"-|", git_cmd(),"rev-list",3267"--parents",3268"--header",3269"--max-count=1",3270$commit_id,3271"--",3272or die_error(500,"Open git-rev-list failed");3273%co= parse_commit_text(<$fd>,1);3274close$fd;32753276return%co;3277}32783279sub parse_commits {3280my($commit_id,$maxcount,$skip,$filename,@args) =@_;3281my@cos;32823283$maxcount||=1;3284$skip||=0;32853286local$/="\0";32873288open my$fd,"-|", git_cmd(),"rev-list",3289"--header",3290@args,3291("--max-count=".$maxcount),3292("--skip=".$skip),3293@extra_options,3294$commit_id,3295"--",3296($filename? ($filename) : ())3297or die_error(500,"Open git-rev-list failed");3298while(my$line= <$fd>) {3299my%co= parse_commit_text($line);3300push@cos, \%co;3301}3302close$fd;33033304returnwantarray?@cos: \@cos;3305}33063307# parse line of git-diff-tree "raw" output3308sub parse_difftree_raw_line {3309my$line=shift;3310my%res;33113312# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'3313# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'3314if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {3315$res{'from_mode'} =$1;3316$res{'to_mode'} =$2;3317$res{'from_id'} =$3;3318$res{'to_id'} =$4;3319$res{'status'} =$5;3320$res{'similarity'} =$6;3321if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3322($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3323}else{3324$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3325}3326}3327# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3328# combined diff (for merge commit)3329elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3330$res{'nparents'} =length($1);3331$res{'from_mode'} = [split(' ',$2) ];3332$res{'to_mode'} =pop@{$res{'from_mode'}};3333$res{'from_id'} = [split(' ',$3) ];3334$res{'to_id'} =pop@{$res{'from_id'}};3335$res{'status'} = [split('',$4) ];3336$res{'to_file'} = unquote($5);3337}3338# 'c512b523472485aef4fff9e57b229d9d243c967f'3339elsif($line=~m/^([0-9a-fA-F]{40})$/) {3340$res{'commit'} =$1;3341}33423343returnwantarray?%res: \%res;3344}33453346# wrapper: return parsed line of git-diff-tree "raw" output3347# (the argument might be raw line, or parsed info)3348sub parsed_difftree_line {3349my$line_or_ref=shift;33503351if(ref($line_or_ref)eq"HASH") {3352# pre-parsed (or generated by hand)3353return$line_or_ref;3354}else{3355return parse_difftree_raw_line($line_or_ref);3356}3357}33583359# parse line of git-ls-tree output3360sub parse_ls_tree_line {3361my$line=shift;3362my%opts=@_;3363my%res;33643365if($opts{'-l'}) {3366#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3367$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;33683369$res{'mode'} =$1;3370$res{'type'} =$2;3371$res{'hash'} =$3;3372$res{'size'} =$4;3373if($opts{'-z'}) {3374$res{'name'} =$5;3375}else{3376$res{'name'} = unquote($5);3377}3378}else{3379#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3380$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;33813382$res{'mode'} =$1;3383$res{'type'} =$2;3384$res{'hash'} =$3;3385if($opts{'-z'}) {3386$res{'name'} =$4;3387}else{3388$res{'name'} = unquote($4);3389}3390}33913392returnwantarray?%res: \%res;3393}33943395# generates _two_ hashes, references to which are passed as 2 and 3 argument3396sub parse_from_to_diffinfo {3397my($diffinfo,$from,$to,@parents) =@_;33983399if($diffinfo->{'nparents'}) {3400# combined diff3401$from->{'file'} = [];3402$from->{'href'} = [];3403 fill_from_file_info($diffinfo,@parents)3404unlessexists$diffinfo->{'from_file'};3405for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3406$from->{'file'}[$i] =3407defined$diffinfo->{'from_file'}[$i] ?3408$diffinfo->{'from_file'}[$i] :3409$diffinfo->{'to_file'};3410if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3411$from->{'href'}[$i] = href(action=>"blob",3412 hash_base=>$parents[$i],3413 hash=>$diffinfo->{'from_id'}[$i],3414 file_name=>$from->{'file'}[$i]);3415}else{3416$from->{'href'}[$i] =undef;3417}3418}3419}else{3420# ordinary (not combined) diff3421$from->{'file'} =$diffinfo->{'from_file'};3422if($diffinfo->{'status'}ne"A") {# not new (added) file3423$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3424 hash=>$diffinfo->{'from_id'},3425 file_name=>$from->{'file'});3426}else{3427delete$from->{'href'};3428}3429}34303431$to->{'file'} =$diffinfo->{'to_file'};3432if(!is_deleted($diffinfo)) {# file exists in result3433$to->{'href'} = href(action=>"blob", hash_base=>$hash,3434 hash=>$diffinfo->{'to_id'},3435 file_name=>$to->{'file'});3436}else{3437delete$to->{'href'};3438}3439}34403441## ......................................................................3442## parse to array of hashes functions34433444sub git_get_heads_list {3445my($limit,@classes) =@_;3446@classes= ('heads')unless@classes;3447my@patterns=map{"refs/$_"}@classes;3448my@headslist;34493450open my$fd,'-|', git_cmd(),'for-each-ref',3451($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3452'--format=%(objectname) %(refname) %(subject)%00%(committer)',3453@patterns3454orreturn;3455while(my$line= <$fd>) {3456my%ref_item;34573458chomp$line;3459my($refinfo,$committerinfo) =split(/\0/,$line);3460my($hash,$name,$title) =split(' ',$refinfo,3);3461my($committer,$epoch,$tz) =3462($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3463$ref_item{'fullname'} =$name;3464$name=~s!^refs/(?:head|remote)s/!!;34653466$ref_item{'name'} =$name;3467$ref_item{'id'} =$hash;3468$ref_item{'title'} =$title||'(no commit message)';3469$ref_item{'epoch'} =$epoch;3470if($epoch) {3471$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3472}else{3473$ref_item{'age'} ="unknown";3474}34753476push@headslist, \%ref_item;3477}3478close$fd;34793480returnwantarray?@headslist: \@headslist;3481}34823483sub git_get_tags_list {3484my$limit=shift;3485my@tagslist;34863487open my$fd,'-|', git_cmd(),'for-each-ref',3488($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3489'--format=%(objectname) %(objecttype) %(refname) '.3490'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3491'refs/tags'3492orreturn;3493while(my$line= <$fd>) {3494my%ref_item;34953496chomp$line;3497my($refinfo,$creatorinfo) =split(/\0/,$line);3498my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3499my($creator,$epoch,$tz) =3500($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3501$ref_item{'fullname'} =$name;3502$name=~s!^refs/tags/!!;35033504$ref_item{'type'} =$type;3505$ref_item{'id'} =$id;3506$ref_item{'name'} =$name;3507if($typeeq"tag") {3508$ref_item{'subject'} =$title;3509$ref_item{'reftype'} =$reftype;3510$ref_item{'refid'} =$refid;3511}else{3512$ref_item{'reftype'} =$type;3513$ref_item{'refid'} =$id;3514}35153516if($typeeq"tag"||$typeeq"commit") {3517$ref_item{'epoch'} =$epoch;3518if($epoch) {3519$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3520}else{3521$ref_item{'age'} ="unknown";3522}3523}35243525push@tagslist, \%ref_item;3526}3527close$fd;35283529returnwantarray?@tagslist: \@tagslist;3530}35313532## ----------------------------------------------------------------------3533## filesystem-related functions35343535sub get_file_owner {3536my$path=shift;35373538my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3539my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3540if(!defined$gcos) {3541returnundef;3542}3543my$owner=$gcos;3544$owner=~s/[,;].*$//;3545return to_utf8($owner);3546}35473548# assume that file exists3549sub insert_file {3550my$filename=shift;35513552open my$fd,'<',$filename;3553print map{ to_utf8($_) } <$fd>;3554close$fd;3555}35563557## ......................................................................3558## mimetype related functions35593560sub mimetype_guess_file {3561my$filename=shift;3562my$mimemap=shift;3563-r $mimemaporreturnundef;35643565my%mimemap;3566open(my$mh,'<',$mimemap)orreturnundef;3567while(<$mh>) {3568next ifm/^#/;# skip comments3569my($mimetype,@exts) =split(/\s+/);3570foreachmy$ext(@exts) {3571$mimemap{$ext} =$mimetype;3572}3573}3574close($mh);35753576$filename=~/\.([^.]*)$/;3577return$mimemap{$1};3578}35793580sub mimetype_guess {3581my$filename=shift;3582my$mime;3583$filename=~/\./orreturnundef;35843585if($mimetypes_file) {3586my$file=$mimetypes_file;3587if($file!~m!^/!) {# if it is relative path3588# it is relative to project3589$file="$projectroot/$project/$file";3590}3591$mime= mimetype_guess_file($filename,$file);3592}3593$mime||= mimetype_guess_file($filename,'/etc/mime.types');3594return$mime;3595}35963597sub blob_mimetype {3598my$fd=shift;3599my$filename=shift;36003601if($filename) {3602my$mime= mimetype_guess($filename);3603$mimeandreturn$mime;3604}36053606# just in case3607return$default_blob_plain_mimetypeunless$fd;36083609if(-T $fd) {3610return'text/plain';3611}elsif(!$filename) {3612return'application/octet-stream';3613}elsif($filename=~m/\.png$/i) {3614return'image/png';3615}elsif($filename=~m/\.gif$/i) {3616return'image/gif';3617}elsif($filename=~m/\.jpe?g$/i) {3618return'image/jpeg';3619}else{3620return'application/octet-stream';3621}3622}36233624sub blob_contenttype {3625my($fd,$file_name,$type) =@_;36263627$type||= blob_mimetype($fd,$file_name);3628if($typeeq'text/plain'&&defined$default_text_plain_charset) {3629$type.="; charset=$default_text_plain_charset";3630}36313632return$type;3633}36343635# guess file syntax for syntax highlighting; return undef if no highlighting3636# the name of syntax can (in the future) depend on syntax highlighter used3637sub guess_file_syntax {3638my($highlight,$mimetype,$file_name) =@_;3639returnundefunless($highlight&&defined$file_name);3640my$basename= basename($file_name,'.in');3641return$highlight_basename{$basename}3642ifexists$highlight_basename{$basename};36433644$basename=~/\.([^.]*)$/;3645my$ext=$1orreturnundef;3646return$highlight_ext{$ext}3647ifexists$highlight_ext{$ext};36483649returnundef;3650}36513652# run highlighter and return FD of its output,3653# or return original FD if no highlighting3654sub run_highlighter {3655my($fd,$highlight,$syntax) =@_;3656return$fdunless($highlight&&defined$syntax);36573658close$fd;3659open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3660 quote_command($highlight_bin).3661" --replace-tabs=8 --fragment --syntax$syntax|"3662or die_error(500,"Couldn't open file or run syntax highlighter");3663return$fd;3664}36653666## ======================================================================3667## functions printing HTML: header, footer, error page36683669sub get_page_title {3670my$title= to_utf8($site_name);36713672return$titleunless(defined$project);3673$title.=" - ". to_utf8($project);36743675return$titleunless(defined$action);3676$title.="/$action";# $action is US-ASCII (7bit ASCII)36773678return$titleunless(defined$file_name);3679$title.=" - ". esc_path($file_name);3680if($actioneq"tree"&&$file_name!~ m|/$|) {3681$title.="/";3682}36833684return$title;3685}36863687sub get_content_type_html {3688# require explicit support from the UA if we are to send the page as3689# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3690# we have to do this because MSIE sometimes globs '*/*', pretending to3691# support xhtml+xml but choking when it gets what it asked for.3692if(defined$cgi->http('HTTP_ACCEPT') &&3693$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3694$cgi->Accept('application/xhtml+xml') !=0) {3695return'application/xhtml+xml';3696}else{3697return'text/html';3698}3699}37003701sub print_feed_meta {3702if(defined$project) {3703my%href_params= get_feed_info();3704if(!exists$href_params{'-title'}) {3705$href_params{'-title'} ='log';3706}37073708foreachmy$format(qw(RSS Atom)) {3709my$type=lc($format);3710my%link_attr= (3711'-rel'=>'alternate',3712'-title'=> esc_attr("$project-$href_params{'-title'} -$formatfeed"),3713'-type'=>"application/$type+xml"3714);37153716$href_params{'action'} =$type;3717$link_attr{'-href'} = href(%href_params);3718print"<link ".3719"rel=\"$link_attr{'-rel'}\"".3720"title=\"$link_attr{'-title'}\"".3721"href=\"$link_attr{'-href'}\"".3722"type=\"$link_attr{'-type'}\"".3723"/>\n";37243725$href_params{'extra_options'} ='--no-merges';3726$link_attr{'-href'} = href(%href_params);3727$link_attr{'-title'} .=' (no merges)';3728print"<link ".3729"rel=\"$link_attr{'-rel'}\"".3730"title=\"$link_attr{'-title'}\"".3731"href=\"$link_attr{'-href'}\"".3732"type=\"$link_attr{'-type'}\"".3733"/>\n";3734}37353736}else{3737printf('<link rel="alternate" title="%sprojects list" '.3738'href="%s" type="text/plain; charset=utf-8" />'."\n",3739 esc_attr($site_name), href(project=>undef, action=>"project_index"));3740printf('<link rel="alternate" title="%sprojects feeds" '.3741'href="%s" type="text/x-opml" />'."\n",3742 esc_attr($site_name), href(project=>undef, action=>"opml"));3743}3744}37453746sub print_header_links {3747my$status=shift;37483749# print out each stylesheet that exist, providing backwards capability3750# for those people who defined $stylesheet in a config file3751if(defined$stylesheet) {3752print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3753}else{3754foreachmy$stylesheet(@stylesheets) {3755next unless$stylesheet;3756print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3757}3758}3759 print_feed_meta()3760if($statuseq'200 OK');3761if(defined$favicon) {3762printqq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);3763}3764}37653766sub print_nav_breadcrumbs {3767my%opts=@_;37683769print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3770if(defined$project) {3771print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3772if(defined$action) {3773my$action_print=$action;3774if(defined$opts{-action_extra}) {3775$action_print=$cgi->a({-href => href(action=>$action)},3776$action);3777}3778print" /$action_print";3779}3780if(defined$opts{-action_extra}) {3781print" /$opts{-action_extra}";3782}3783print"\n";3784}3785}37863787sub print_search_form {3788if(!defined$searchtext) {3789$searchtext="";3790}3791my$search_hash;3792if(defined$hash_base) {3793$search_hash=$hash_base;3794}elsif(defined$hash) {3795$search_hash=$hash;3796}else{3797$search_hash="HEAD";3798}3799my$action=$my_uri;3800my$use_pathinfo= gitweb_check_feature('pathinfo');3801if($use_pathinfo) {3802$action.="/".esc_url($project);3803}3804print$cgi->startform(-method=>"get", -action =>$action) .3805"<div class=\"search\">\n".3806(!$use_pathinfo&&3807$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3808$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3809$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3810$cgi->popup_menu(-name =>'st', -default=>'commit',3811-values=> ['commit','grep','author','committer','pickaxe']) .3812$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3813" search:\n",3814$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3815"<span title=\"Extended regular expression\">".3816$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3817-checked =>$search_use_regexp) .3818"</span>".3819"</div>".3820$cgi->end_form() ."\n";3821}38223823sub git_header_html {3824my$status=shift||"200 OK";3825my$expires=shift;3826my%opts=@_;38273828my$title= get_page_title();3829my$content_type= get_content_type_html();3830print$cgi->header(-type=>$content_type, -charset =>'utf-8',3831-status=>$status, -expires =>$expires)3832unless($opts{'-no_http_header'});3833my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3834print<<EOF;3835<?xml version="1.0" encoding="utf-8"?>3836<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3837<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3838<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3839<!-- git core binaries version$git_version-->3840<head>3841<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3842<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3843<meta name="robots" content="index, nofollow"/>3844<title>$title</title>3845EOF3846# the stylesheet, favicon etc urls won't work correctly with path_info3847# unless we set the appropriate base URL3848if($ENV{'PATH_INFO'}) {3849print"<base href=\"".esc_url($base_url)."\"/>\n";3850}3851 print_header_links($status);3852print"</head>\n".3853"<body>\n";38543855if(defined$site_header&& -f $site_header) {3856 insert_file($site_header);3857}38583859print"<div class=\"page_header\">\n";3860if(defined$logo) {3861print$cgi->a({-href => esc_url($logo_url),3862-title =>$logo_label},3863$cgi->img({-src => esc_url($logo),3864-width =>72, -height =>27,3865-alt =>"git",3866-class=>"logo"}));3867}3868 print_nav_breadcrumbs(%opts);3869print"</div>\n";38703871my$have_search= gitweb_check_feature('search');3872if(defined$project&&$have_search) {3873 print_search_form();3874}3875}38763877sub git_footer_html {3878my$feed_class='rss_logo';38793880print"<div class=\"page_footer\">\n";3881if(defined$project) {3882my$descr= git_get_project_description($project);3883if(defined$descr) {3884print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3885}38863887my%href_params= get_feed_info();3888if(!%href_params) {3889$feed_class.=' generic';3890}3891$href_params{'-title'} ||='log';38923893foreachmy$format(qw(RSS Atom)) {3894$href_params{'action'} =lc($format);3895print$cgi->a({-href => href(%href_params),3896-title =>"$href_params{'-title'}$formatfeed",3897-class=>$feed_class},$format)."\n";3898}38993900}else{3901print$cgi->a({-href => href(project=>undef, action=>"opml"),3902-class=>$feed_class},"OPML") ." ";3903print$cgi->a({-href => href(project=>undef, action=>"project_index"),3904-class=>$feed_class},"TXT") ."\n";3905}3906print"</div>\n";# class="page_footer"39073908if(defined$t0&& gitweb_check_feature('timed')) {3909print"<div id=\"generating_info\">\n";3910print'This page took '.3911'<span id="generating_time" class="time_span">'.3912 tv_interval($t0, [ gettimeofday() ]).3913' seconds </span>'.3914' and '.3915'<span id="generating_cmd">'.3916$number_of_git_cmds.3917'</span> git commands '.3918" to generate.\n";3919print"</div>\n";# class="page_footer"3920}39213922if(defined$site_footer&& -f $site_footer) {3923 insert_file($site_footer);3924}39253926print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;3927if(defined$action&&3928$actioneq'blame_incremental') {3929print qq!<script type="text/javascript">\n!.3930 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3931 qq!"!. href() .qq!");\n!.3932 qq!</script>\n!;3933}else{3934my($jstimezone,$tz_cookie,$datetime_class) =3935 gitweb_get_feature('javascript-timezone');39363937print qq!<script type="text/javascript">\n!.3938 qq!window.onload = function () {\n!;3939if(gitweb_check_feature('javascript-actions')) {3940print qq! fixLinks();\n!;3941}3942if($jstimezone&&$tz_cookie&&$datetime_class) {3943print qq! var tz_cookie = { name:'$tz_cookie', expires:14, path:'/'};\n!.# in days3944 qq! onloadTZSetup('$jstimezone', tz_cookie,'$datetime_class');\n!;3945}3946print qq!};\n!.3947 qq!</script>\n!;3948}39493950print"</body>\n".3951"</html>";3952}39533954# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3955# Example: die_error(404, 'Hash not found')3956# By convention, use the following status codes (as defined in RFC 2616):3957# 400: Invalid or missing CGI parameters, or3958# requested object exists but has wrong type.3959# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3960# this server or project.3961# 404: Requested object/revision/project doesn't exist.3962# 500: The server isn't configured properly, or3963# an internal error occurred (e.g. failed assertions caused by bugs), or3964# an unknown error occurred (e.g. the git binary died unexpectedly).3965# 503: The server is currently unavailable (because it is overloaded,3966# or down for maintenance). Generally, this is a temporary state.3967sub die_error {3968my$status=shift||500;3969my$error= esc_html(shift) ||"Internal Server Error";3970my$extra=shift;3971my%opts=@_;39723973my%http_responses= (3974400=>'400 Bad Request',3975403=>'403 Forbidden',3976404=>'404 Not Found',3977500=>'500 Internal Server Error',3978503=>'503 Service Unavailable',3979);3980 git_header_html($http_responses{$status},undef,%opts);3981print<<EOF;3982<div class="page_body">3983<br /><br />3984$status-$error3985<br />3986EOF3987if(defined$extra) {3988print"<hr />\n".3989"$extra\n";3990}3991print"</div>\n";39923993 git_footer_html();3994goto DONE_GITWEB3995unless($opts{'-error_handler'});3996}39973998## ----------------------------------------------------------------------3999## functions printing or outputting HTML: navigation40004001sub git_print_page_nav {4002my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;4003$extra=''if!defined$extra;# pager or formats40044005my@navs=qw(summary shortlog log commit commitdiff tree);4006if($suppress) {4007@navs=grep{$_ne$suppress}@navs;4008}40094010my%arg=map{$_=> {action=>$_} }@navs;4011if(defined$head) {4012for(qw(commit commitdiff)) {4013$arg{$_}{'hash'} =$head;4014}4015if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {4016for(qw(shortlog log)) {4017$arg{$_}{'hash'} =$head;4018}4019}4020}40214022$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;4023$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;40244025my@actions= gitweb_get_feature('actions');4026my%repl= (4027'%'=>'%',4028'n'=>$project,# project name4029'f'=>$git_dir,# project path within filesystem4030'h'=>$treehead||'',# current hash ('h' parameter)4031'b'=>$treebase||'',# hash base ('hb' parameter)4032);4033while(@actions) {4034my($label,$link,$pos) =splice(@actions,0,3);4035# insert4036@navs=map{$_eq$pos? ($_,$label) :$_}@navs;4037# munch munch4038$link=~s/%([%nfhb])/$repl{$1}/g;4039$arg{$label}{'_href'} =$link;4040}40414042print"<div class=\"page_nav\">\n".4043(join" | ",4044map{$_eq$current?4045$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")4046}@navs);4047print"<br/>\n$extra<br/>\n".4048"</div>\n";4049}40504051# returns a submenu for the nagivation of the refs views (tags, heads,4052# remotes) with the current view disabled and the remotes view only4053# available if the feature is enabled4054sub format_ref_views {4055my($current) =@_;4056my@ref_views=qw{tags heads};4057push@ref_views,'remotes'if gitweb_check_feature('remote_heads');4058returnjoin" | ",map{4059$_eq$current?$_:4060$cgi->a({-href => href(action=>$_)},$_)4061}@ref_views4062}40634064sub format_paging_nav {4065my($action,$page,$has_next_link) =@_;4066my$paging_nav;406740684069if($page>0) {4070$paging_nav.=4071$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .4072" ⋅ ".4073$cgi->a({-href => href(-replay=>1, page=>$page-1),4074-accesskey =>"p", -title =>"Alt-p"},"prev");4075}else{4076$paging_nav.="first ⋅ prev";4077}40784079if($has_next_link) {4080$paging_nav.=" ⋅ ".4081$cgi->a({-href => href(-replay=>1, page=>$page+1),4082-accesskey =>"n", -title =>"Alt-n"},"next");4083}else{4084$paging_nav.=" ⋅ next";4085}40864087return$paging_nav;4088}40894090## ......................................................................4091## functions printing or outputting HTML: div40924093sub git_print_header_div {4094my($action,$title,$hash,$hash_base) =@_;4095my%args= ();40964097$args{'action'} =$action;4098$args{'hash'} =$hashif$hash;4099$args{'hash_base'} =$hash_baseif$hash_base;41004101print"<div class=\"header\">\n".4102$cgi->a({-href => href(%args), -class=>"title"},4103$title?$title:$action) .4104"\n</div>\n";4105}41064107sub format_repo_url {4108my($name,$url) =@_;4109return"<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";4110}41114112# Group output by placing it in a DIV element and adding a header.4113# Options for start_div() can be provided by passing a hash reference as the4114# first parameter to the function.4115# Options to git_print_header_div() can be provided by passing an array4116# reference. This must follow the options to start_div if they are present.4117# The content can be a scalar, which is output as-is, a scalar reference, which4118# is output after html escaping, an IO handle passed either as *handle or4119# *handle{IO}, or a function reference. In the latter case all following4120# parameters will be taken as argument to the content function call.4121sub git_print_section {4122my($div_args,$header_args,$content);4123my$arg=shift;4124if(ref($arg)eq'HASH') {4125$div_args=$arg;4126$arg=shift;4127}4128if(ref($arg)eq'ARRAY') {4129$header_args=$arg;4130$arg=shift;4131}4132$content=$arg;41334134print$cgi->start_div($div_args);4135 git_print_header_div(@$header_args);41364137if(ref($content)eq'CODE') {4138$content->(@_);4139}elsif(ref($content)eq'SCALAR') {4140print esc_html($$content);4141}elsif(ref($content)eq'GLOB'or ref($content)eq'IO::Handle') {4142print<$content>;4143}elsif(!ref($content) &&defined($content)) {4144print$content;4145}41464147print$cgi->end_div;4148}41494150sub format_timestamp_html {4151my$date=shift;4152my$strtime=$date->{'rfc2822'};41534154my(undef,undef,$datetime_class) =4155 gitweb_get_feature('javascript-timezone');4156if($datetime_class) {4157$strtime= qq!<span class="$datetime_class">$strtime</span>!;4158}41594160my$localtime_format='(%02d:%02d%s)';4161if($date->{'hour_local'} <6) {4162$localtime_format='(<span class="atnight">%02d:%02d</span>%s)';4163}4164$strtime.=' '.4165sprintf($localtime_format,4166$date->{'hour_local'},$date->{'minute_local'},$date->{'tz_local'});41674168return$strtime;4169}41704171# Outputs the author name and date in long form4172sub git_print_authorship {4173my$co=shift;4174my%opts=@_;4175my$tag=$opts{-tag} ||'div';4176my$author=$co->{'author_name'};41774178my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});4179print"<$tagclass=\"author_date\">".4180 format_search_author($author,"author", esc_html($author)) .4181" [".format_timestamp_html(\%ad)."]".4182 git_get_avatar($co->{'author_email'}, -pad_before =>1) .4183"</$tag>\n";4184}41854186# Outputs table rows containing the full author or committer information,4187# in the format expected for 'commit' view (& similar).4188# Parameters are a commit hash reference, followed by the list of people4189# to output information for. If the list is empty it defaults to both4190# author and committer.4191sub git_print_authorship_rows {4192my$co=shift;4193# too bad we can't use @people = @_ || ('author', 'committer')4194my@people=@_;4195@people= ('author','committer')unless@people;4196foreachmy$who(@people) {4197my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});4198print"<tr><td>$who</td><td>".4199 format_search_author($co->{"${who}_name"},$who,4200 esc_html($co->{"${who}_name"})) ." ".4201 format_search_author($co->{"${who}_email"},$who,4202 esc_html("<".$co->{"${who}_email"} .">")) .4203"</td><td rowspan=\"2\">".4204 git_get_avatar($co->{"${who}_email"}, -size =>'double') .4205"</td></tr>\n".4206"<tr>".4207"<td></td><td>".4208 format_timestamp_html(\%wd) .4209"</td>".4210"</tr>\n";4211}4212}42134214sub git_print_page_path {4215my$name=shift;4216my$type=shift;4217my$hb=shift;421842194220print"<div class=\"page_path\">";4221print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),4222-title =>'tree root'}, to_utf8("[$project]"));4223print" / ";4224if(defined$name) {4225my@dirname=split'/',$name;4226my$basename=pop@dirname;4227my$fullname='';42284229foreachmy$dir(@dirname) {4230$fullname.= ($fullname?'/':'') .$dir;4231print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,4232 hash_base=>$hb),4233-title =>$fullname}, esc_path($dir));4234print" / ";4235}4236if(defined$type&&$typeeq'blob') {4237print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,4238 hash_base=>$hb),4239-title =>$name}, esc_path($basename));4240}elsif(defined$type&&$typeeq'tree') {4241print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,4242 hash_base=>$hb),4243-title =>$name}, esc_path($basename));4244print" / ";4245}else{4246print esc_path($basename);4247}4248}4249print"<br/></div>\n";4250}42514252sub git_print_log {4253my$log=shift;4254my%opts=@_;42554256if($opts{'-remove_title'}) {4257# remove title, i.e. first line of log4258shift@$log;4259}4260# remove leading empty lines4261while(defined$log->[0] &&$log->[0]eq"") {4262shift@$log;4263}42644265# print log4266my$signoff=0;4267my$empty=0;4268foreachmy$line(@$log) {4269if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {4270$signoff=1;4271$empty=0;4272if(!$opts{'-remove_signoff'}) {4273print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";4274next;4275}else{4276# remove signoff lines4277next;4278}4279}else{4280$signoff=0;4281}42824283# print only one empty line4284# do not print empty line after signoff4285if($lineeq"") {4286next if($empty||$signoff);4287$empty=1;4288}else{4289$empty=0;4290}42914292print format_log_line_html($line) ."<br/>\n";4293}42944295if($opts{'-final_empty_line'}) {4296# end with single empty line4297print"<br/>\n"unless$empty;4298}4299}43004301# return link target (what link points to)4302sub git_get_link_target {4303my$hash=shift;4304my$link_target;43054306# read link4307open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4308orreturn;4309{4310local$/=undef;4311$link_target= <$fd>;4312}4313close$fd4314orreturn;43154316return$link_target;4317}43184319# given link target, and the directory (basedir) the link is in,4320# return target of link relative to top directory (top tree);4321# return undef if it is not possible (including absolute links).4322sub normalize_link_target {4323my($link_target,$basedir) =@_;43244325# absolute symlinks (beginning with '/') cannot be normalized4326return if(substr($link_target,0,1)eq'/');43274328# normalize link target to path from top (root) tree (dir)4329my$path;4330if($basedir) {4331$path=$basedir.'/'.$link_target;4332}else{4333# we are in top (root) tree (dir)4334$path=$link_target;4335}43364337# remove //, /./, and /../4338my@path_parts;4339foreachmy$part(split('/',$path)) {4340# discard '.' and ''4341next if(!$part||$parteq'.');4342# handle '..'4343if($parteq'..') {4344if(@path_parts) {4345pop@path_parts;4346}else{4347# link leads outside repository (outside top dir)4348return;4349}4350}else{4351push@path_parts,$part;4352}4353}4354$path=join('/',@path_parts);43554356return$path;4357}43584359# print tree entry (row of git_tree), but without encompassing <tr> element4360sub git_print_tree_entry {4361my($t,$basedir,$hash_base,$have_blame) =@_;43624363my%base_key= ();4364$base_key{'hash_base'} =$hash_baseifdefined$hash_base;43654366# The format of a table row is: mode list link. Where mode is4367# the mode of the entry, list is the name of the entry, an href,4368# and link is the action links of the entry.43694370print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";4371if(exists$t->{'size'}) {4372print"<td class=\"size\">$t->{'size'}</td>\n";4373}4374if($t->{'type'}eq"blob") {4375print"<td class=\"list\">".4376$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4377 file_name=>"$basedir$t->{'name'}",%base_key),4378-class=>"list"}, esc_path($t->{'name'}));4379if(S_ISLNK(oct$t->{'mode'})) {4380my$link_target= git_get_link_target($t->{'hash'});4381if($link_target) {4382my$norm_target= normalize_link_target($link_target,$basedir);4383if(defined$norm_target) {4384print" -> ".4385$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,4386 file_name=>$norm_target),4387-title =>$norm_target}, esc_path($link_target));4388}else{4389print" -> ". esc_path($link_target);4390}4391}4392}4393print"</td>\n";4394print"<td class=\"link\">";4395print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4396 file_name=>"$basedir$t->{'name'}",%base_key)},4397"blob");4398if($have_blame) {4399print" | ".4400$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4401 file_name=>"$basedir$t->{'name'}",%base_key)},4402"blame");4403}4404if(defined$hash_base) {4405print" | ".4406$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4407 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4408"history");4409}4410print" | ".4411$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4412 file_name=>"$basedir$t->{'name'}")},4413"raw");4414print"</td>\n";44154416}elsif($t->{'type'}eq"tree") {4417print"<td class=\"list\">";4418print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4419 file_name=>"$basedir$t->{'name'}",4420%base_key)},4421 esc_path($t->{'name'}));4422print"</td>\n";4423print"<td class=\"link\">";4424print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4425 file_name=>"$basedir$t->{'name'}",4426%base_key)},4427"tree");4428if(defined$hash_base) {4429print" | ".4430$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4431 file_name=>"$basedir$t->{'name'}")},4432"history");4433}4434print"</td>\n";4435}else{4436# unknown object: we can only present history for it4437# (this includes 'commit' object, i.e. submodule support)4438print"<td class=\"list\">".4439 esc_path($t->{'name'}) .4440"</td>\n";4441print"<td class=\"link\">";4442if(defined$hash_base) {4443print$cgi->a({-href => href(action=>"history",4444 hash_base=>$hash_base,4445 file_name=>"$basedir$t->{'name'}")},4446"history");4447}4448print"</td>\n";4449}4450}44514452## ......................................................................4453## functions printing large fragments of HTML44544455# get pre-image filenames for merge (combined) diff4456sub fill_from_file_info {4457my($diff,@parents) =@_;44584459$diff->{'from_file'} = [ ];4460$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4461for(my$i=0;$i<$diff->{'nparents'};$i++) {4462if($diff->{'status'}[$i]eq'R'||4463$diff->{'status'}[$i]eq'C') {4464$diff->{'from_file'}[$i] =4465 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4466}4467}44684469return$diff;4470}44714472# is current raw difftree line of file deletion4473sub is_deleted {4474my$diffinfo=shift;44754476return$diffinfo->{'to_id'}eq('0' x 40);4477}44784479# does patch correspond to [previous] difftree raw line4480# $diffinfo - hashref of parsed raw diff format4481# $patchinfo - hashref of parsed patch diff format4482# (the same keys as in $diffinfo)4483sub is_patch_split {4484my($diffinfo,$patchinfo) =@_;44854486returndefined$diffinfo&&defined$patchinfo4487&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4488}448944904491sub git_difftree_body {4492my($difftree,$hash,@parents) =@_;4493my($parent) =$parents[0];4494my$have_blame= gitweb_check_feature('blame');4495print"<div class=\"list_head\">\n";4496if($#{$difftree} >10) {4497print(($#{$difftree} +1) ." files changed:\n");4498}4499print"</div>\n";45004501print"<table class=\"".4502(@parents>1?"combined ":"") .4503"diff_tree\">\n";45044505# header only for combined diff in 'commitdiff' view4506my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4507if($has_header) {4508# table header4509print"<thead><tr>\n".4510"<th></th><th></th>\n";# filename, patchN link4511for(my$i=0;$i<@parents;$i++) {4512my$par=$parents[$i];4513print"<th>".4514$cgi->a({-href => href(action=>"commitdiff",4515 hash=>$hash, hash_parent=>$par),4516-title =>'commitdiff to parent number '.4517($i+1) .': '.substr($par,0,7)},4518$i+1) .4519" </th>\n";4520}4521print"</tr></thead>\n<tbody>\n";4522}45234524my$alternate=1;4525my$patchno=0;4526foreachmy$line(@{$difftree}) {4527my$diff= parsed_difftree_line($line);45284529if($alternate) {4530print"<tr class=\"dark\">\n";4531}else{4532print"<tr class=\"light\">\n";4533}4534$alternate^=1;45354536if(exists$diff->{'nparents'}) {# combined diff45374538 fill_from_file_info($diff,@parents)4539unlessexists$diff->{'from_file'};45404541if(!is_deleted($diff)) {4542# file exists in the result (child) commit4543print"<td>".4544$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4545 file_name=>$diff->{'to_file'},4546 hash_base=>$hash),4547-class=>"list"}, esc_path($diff->{'to_file'})) .4548"</td>\n";4549}else{4550print"<td>".4551 esc_path($diff->{'to_file'}) .4552"</td>\n";4553}45544555if($actioneq'commitdiff') {4556# link to patch4557$patchno++;4558print"<td class=\"link\">".4559$cgi->a({-href => href(-anchor=>"patch$patchno")},4560"patch") .4561" | ".4562"</td>\n";4563}45644565my$has_history=0;4566my$not_deleted=0;4567for(my$i=0;$i<$diff->{'nparents'};$i++) {4568my$hash_parent=$parents[$i];4569my$from_hash=$diff->{'from_id'}[$i];4570my$from_path=$diff->{'from_file'}[$i];4571my$status=$diff->{'status'}[$i];45724573$has_history||= ($statusne'A');4574$not_deleted||= ($statusne'D');45754576if($statuseq'A') {4577print"<td class=\"link\"align=\"right\"> | </td>\n";4578}elsif($statuseq'D') {4579print"<td class=\"link\">".4580$cgi->a({-href => href(action=>"blob",4581 hash_base=>$hash,4582 hash=>$from_hash,4583 file_name=>$from_path)},4584"blob". ($i+1)) .4585" | </td>\n";4586}else{4587if($diff->{'to_id'}eq$from_hash) {4588print"<td class=\"link nochange\">";4589}else{4590print"<td class=\"link\">";4591}4592print$cgi->a({-href => href(action=>"blobdiff",4593 hash=>$diff->{'to_id'},4594 hash_parent=>$from_hash,4595 hash_base=>$hash,4596 hash_parent_base=>$hash_parent,4597 file_name=>$diff->{'to_file'},4598 file_parent=>$from_path)},4599"diff". ($i+1)) .4600" | </td>\n";4601}4602}46034604print"<td class=\"link\">";4605if($not_deleted) {4606print$cgi->a({-href => href(action=>"blob",4607 hash=>$diff->{'to_id'},4608 file_name=>$diff->{'to_file'},4609 hash_base=>$hash)},4610"blob");4611print" | "if($has_history);4612}4613if($has_history) {4614print$cgi->a({-href => href(action=>"history",4615 file_name=>$diff->{'to_file'},4616 hash_base=>$hash)},4617"history");4618}4619print"</td>\n";46204621print"</tr>\n";4622next;# instead of 'else' clause, to avoid extra indent4623}4624# else ordinary diff46254626my($to_mode_oct,$to_mode_str,$to_file_type);4627my($from_mode_oct,$from_mode_str,$from_file_type);4628if($diff->{'to_mode'}ne('0' x 6)) {4629$to_mode_oct=oct$diff->{'to_mode'};4630if(S_ISREG($to_mode_oct)) {# only for regular file4631$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4632}4633$to_file_type= file_type($diff->{'to_mode'});4634}4635if($diff->{'from_mode'}ne('0' x 6)) {4636$from_mode_oct=oct$diff->{'from_mode'};4637if(S_ISREG($from_mode_oct)) {# only for regular file4638$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4639}4640$from_file_type= file_type($diff->{'from_mode'});4641}46424643if($diff->{'status'}eq"A") {# created4644my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4645$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4646$mode_chng.="]</span>";4647print"<td>";4648print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4649 hash_base=>$hash, file_name=>$diff->{'file'}),4650-class=>"list"}, esc_path($diff->{'file'}));4651print"</td>\n";4652print"<td>$mode_chng</td>\n";4653print"<td class=\"link\">";4654if($actioneq'commitdiff') {4655# link to patch4656$patchno++;4657print$cgi->a({-href => href(-anchor=>"patch$patchno")},4658"patch") .4659" | ";4660}4661print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4662 hash_base=>$hash, file_name=>$diff->{'file'})},4663"blob");4664print"</td>\n";46654666}elsif($diff->{'status'}eq"D") {# deleted4667my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4668print"<td>";4669print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4670 hash_base=>$parent, file_name=>$diff->{'file'}),4671-class=>"list"}, esc_path($diff->{'file'}));4672print"</td>\n";4673print"<td>$mode_chng</td>\n";4674print"<td class=\"link\">";4675if($actioneq'commitdiff') {4676# link to patch4677$patchno++;4678print$cgi->a({-href => href(-anchor=>"patch$patchno")},4679"patch") .4680" | ";4681}4682print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4683 hash_base=>$parent, file_name=>$diff->{'file'})},4684"blob") ." | ";4685if($have_blame) {4686print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4687 file_name=>$diff->{'file'})},4688"blame") ." | ";4689}4690print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4691 file_name=>$diff->{'file'})},4692"history");4693print"</td>\n";46944695}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4696my$mode_chnge="";4697if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4698$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4699if($from_file_typene$to_file_type) {4700$mode_chnge.=" from$from_file_typeto$to_file_type";4701}4702if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4703if($from_mode_str&&$to_mode_str) {4704$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4705}elsif($to_mode_str) {4706$mode_chnge.=" mode:$to_mode_str";4707}4708}4709$mode_chnge.="]</span>\n";4710}4711print"<td>";4712print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4713 hash_base=>$hash, file_name=>$diff->{'file'}),4714-class=>"list"}, esc_path($diff->{'file'}));4715print"</td>\n";4716print"<td>$mode_chnge</td>\n";4717print"<td class=\"link\">";4718if($actioneq'commitdiff') {4719# link to patch4720$patchno++;4721print$cgi->a({-href => href(-anchor=>"patch$patchno")},4722"patch") .4723" | ";4724}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4725# "commit" view and modified file (not onlu mode changed)4726print$cgi->a({-href => href(action=>"blobdiff",4727 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4728 hash_base=>$hash, hash_parent_base=>$parent,4729 file_name=>$diff->{'file'})},4730"diff") .4731" | ";4732}4733print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4734 hash_base=>$hash, file_name=>$diff->{'file'})},4735"blob") ." | ";4736if($have_blame) {4737print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4738 file_name=>$diff->{'file'})},4739"blame") ." | ";4740}4741print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4742 file_name=>$diff->{'file'})},4743"history");4744print"</td>\n";47454746}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4747my%status_name= ('R'=>'moved','C'=>'copied');4748my$nstatus=$status_name{$diff->{'status'}};4749my$mode_chng="";4750if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4751# mode also for directories, so we cannot use $to_mode_str4752$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4753}4754print"<td>".4755$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4756 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4757-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4758"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4759$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4760 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4761-class=>"list"}, esc_path($diff->{'from_file'})) .4762" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4763"<td class=\"link\">";4764if($actioneq'commitdiff') {4765# link to patch4766$patchno++;4767print$cgi->a({-href => href(-anchor=>"patch$patchno")},4768"patch") .4769" | ";4770}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4771# "commit" view and modified file (not only pure rename or copy)4772print$cgi->a({-href => href(action=>"blobdiff",4773 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4774 hash_base=>$hash, hash_parent_base=>$parent,4775 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4776"diff") .4777" | ";4778}4779print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4780 hash_base=>$parent, file_name=>$diff->{'to_file'})},4781"blob") ." | ";4782if($have_blame) {4783print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4784 file_name=>$diff->{'to_file'})},4785"blame") ." | ";4786}4787print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4788 file_name=>$diff->{'to_file'})},4789"history");4790print"</td>\n";47914792}# we should not encounter Unmerged (U) or Unknown (X) status4793print"</tr>\n";4794}4795print"</tbody>"if$has_header;4796print"</table>\n";4797}47984799sub git_patchset_body {4800my($fd,$difftree,$hash,@hash_parents) =@_;4801my($hash_parent) =$hash_parents[0];48024803my$is_combined= (@hash_parents>1);4804my$patch_idx=0;4805my$patch_number=0;4806my$patch_line;4807my$diffinfo;4808my$to_name;4809my(%from,%to);48104811print"<div class=\"patchset\">\n";48124813# skip to first patch4814while($patch_line= <$fd>) {4815chomp$patch_line;48164817last if($patch_line=~m/^diff /);4818}48194820 PATCH:4821while($patch_line) {48224823# parse "git diff" header line4824if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4825# $1 is from_name, which we do not use4826$to_name= unquote($2);4827$to_name=~s!^b/!!;4828}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4829# $1 is 'cc' or 'combined', which we do not use4830$to_name= unquote($2);4831}else{4832$to_name=undef;4833}48344835# check if current patch belong to current raw line4836# and parse raw git-diff line if needed4837if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4838# this is continuation of a split patch4839print"<div class=\"patch cont\">\n";4840}else{4841# advance raw git-diff output if needed4842$patch_idx++ifdefined$diffinfo;48434844# read and prepare patch information4845$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);48464847# compact combined diff output can have some patches skipped4848# find which patch (using pathname of result) we are at now;4849if($is_combined) {4850while($to_namene$diffinfo->{'to_file'}) {4851print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4852 format_diff_cc_simplified($diffinfo,@hash_parents) .4853"</div>\n";# class="patch"48544855$patch_idx++;4856$patch_number++;48574858last if$patch_idx>$#$difftree;4859$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4860}4861}48624863# modifies %from, %to hashes4864 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);48654866# this is first patch for raw difftree line with $patch_idx index4867# we index @$difftree array from 0, but number patches from 14868print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4869}48704871# git diff header4872#assert($patch_line =~ m/^diff /) if DEBUG;4873#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4874$patch_number++;4875# print "git diff" header4876print format_git_diff_header_line($patch_line,$diffinfo,4877 \%from, \%to);48784879# print extended diff header4880print"<div class=\"diff extended_header\">\n";4881 EXTENDED_HEADER:4882while($patch_line= <$fd>) {4883chomp$patch_line;48844885last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);48864887print format_extended_diff_header_line($patch_line,$diffinfo,4888 \%from, \%to);4889}4890print"</div>\n";# class="diff extended_header"48914892# from-file/to-file diff header4893if(!$patch_line) {4894print"</div>\n";# class="patch"4895last PATCH;4896}4897next PATCH if($patch_line=~m/^diff /);4898#assert($patch_line =~ m/^---/) if DEBUG;48994900my$last_patch_line=$patch_line;4901$patch_line= <$fd>;4902chomp$patch_line;4903#assert($patch_line =~ m/^\+\+\+/) if DEBUG;49044905print format_diff_from_to_header($last_patch_line,$patch_line,4906$diffinfo, \%from, \%to,4907@hash_parents);49084909# the patch itself4910 LINE:4911while($patch_line= <$fd>) {4912chomp$patch_line;49134914next PATCH if($patch_line=~m/^diff /);49154916print format_diff_line($patch_line, \%from, \%to);4917}49184919}continue{4920print"</div>\n";# class="patch"4921}49224923# for compact combined (--cc) format, with chunk and patch simplification4924# the patchset might be empty, but there might be unprocessed raw lines4925for(++$patch_idxif$patch_number>0;4926$patch_idx<@$difftree;4927++$patch_idx) {4928# read and prepare patch information4929$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);49304931# generate anchor for "patch" links in difftree / whatchanged part4932print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4933 format_diff_cc_simplified($diffinfo,@hash_parents) .4934"</div>\n";# class="patch"49354936$patch_number++;4937}49384939if($patch_number==0) {4940if(@hash_parents>1) {4941print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4942}else{4943print"<div class=\"diff nodifferences\">No differences found</div>\n";4944}4945}49464947print"</div>\n";# class="patchset"4948}49494950# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49514952# fills project list info (age, description, owner, category, forks)4953# for each project in the list, removing invalid projects from4954# returned list4955# NOTE: modifies $projlist, but does not remove entries from it4956sub fill_project_list_info {4957my$projlist=shift;4958my@projects;49594960my$show_ctags= gitweb_check_feature('ctags');4961 PROJECT:4962foreachmy$pr(@$projlist) {4963my(@activity) = git_get_last_activity($pr->{'path'});4964unless(@activity) {4965next PROJECT;4966}4967($pr->{'age'},$pr->{'age_string'}) =@activity;4968if(!defined$pr->{'descr'}) {4969my$descr= git_get_project_description($pr->{'path'}) ||"";4970$descr= to_utf8($descr);4971$pr->{'descr_long'} =$descr;4972$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4973}4974if(!defined$pr->{'owner'}) {4975$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4976}4977if($show_ctags) {4978$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4979}4980if($projects_list_group_categories&& !defined$pr->{'category'}) {4981my$cat= git_get_project_category($pr->{'path'}) ||4982$project_list_default_category;4983$pr->{'category'} = to_utf8($cat);4984}49854986push@projects,$pr;4987}49884989return@projects;4990}49914992sub sort_projects_list {4993my($projlist,$order) =@_;4994my@projects;49954996my%order_info= (4997 project => { key =>'path', type =>'str'},4998 descr => { key =>'descr_long', type =>'str'},4999 owner => { key =>'owner', type =>'str'},5000 age => { key =>'age', type =>'num'}5001);5002my$oi=$order_info{$order};5003return@$projlistunlessdefined$oi;5004if($oi->{'type'}eq'str') {5005@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@$projlist;5006}else{5007@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@$projlist;5008}50095010return@projects;5011}50125013# returns a hash of categories, containing the list of project5014# belonging to each category5015sub build_projlist_by_category {5016my($projlist,$from,$to) =@_;5017my%categories;50185019$from=0unlessdefined$from;5020$to=$#$projlistif(!defined$to||$#$projlist<$to);50215022for(my$i=$from;$i<=$to;$i++) {5023my$pr=$projlist->[$i];5024push@{$categories{$pr->{'category'} }},$pr;5025}50265027returnwantarray?%categories: \%categories;5028}50295030# print 'sort by' <th> element, generating 'sort by $name' replay link5031# if that order is not selected5032sub print_sort_th {5033print format_sort_th(@_);5034}50355036sub format_sort_th {5037my($name,$order,$header) =@_;5038my$sort_th="";5039$header||=ucfirst($name);50405041if($ordereq$name) {5042$sort_th.="<th>$header</th>\n";5043}else{5044$sort_th.="<th>".5045$cgi->a({-href => href(-replay=>1, order=>$name),5046-class=>"header"},$header) .5047"</th>\n";5048}50495050return$sort_th;5051}50525053sub git_project_list_rows {5054my($projlist,$from,$to,$check_forks) =@_;50555056$from=0unlessdefined$from;5057$to=$#$projlistif(!defined$to||$#$projlist<$to);50585059my$alternate=1;5060for(my$i=$from;$i<=$to;$i++) {5061my$pr=$projlist->[$i];50625063if($alternate) {5064print"<tr class=\"dark\">\n";5065}else{5066print"<tr class=\"light\">\n";5067}5068$alternate^=1;50695070if($check_forks) {5071print"<td>";5072if($pr->{'forks'}) {5073my$nforks=scalar@{$pr->{'forks'}};5074if($nforks>0) {5075print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),5076-title =>"$nforksforks"},"+");5077}else{5078print$cgi->span({-title =>"$nforksforks"},"+");5079}5080}5081print"</td>\n";5082}5083print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5084-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".5085"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5086-class=>"list", -title =>$pr->{'descr_long'}},5087 esc_html($pr->{'descr'})) ."</td>\n".5088"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";5089print"<td class=\"". age_class($pr->{'age'}) ."\">".5090(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".5091"<td class=\"link\">".5092$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".5093$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".5094$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".5095$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .5096($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .5097"</td>\n".5098"</tr>\n";5099}5100}51015102sub git_project_list_body {5103# actually uses global variable $project5104my($projlist,$order,$from,$to,$extra,$no_header) =@_;5105my@projects=@$projlist;51065107my$check_forks= gitweb_check_feature('forks');5108my$show_ctags= gitweb_check_feature('ctags');5109my$tagfilter=$show_ctags?$cgi->param('by_tag') :undef;5110$check_forks=undef5111if($tagfilter||$searchtext);51125113# filtering out forks before filling info allows to do less work5114@projects= filter_forks_from_projects_list(\@projects)5115if($check_forks);5116@projects= fill_project_list_info(\@projects);5117# searching projects require filling to be run before it5118@projects= search_projects_list(\@projects,5119'searchtext'=>$searchtext,5120'tagfilter'=>$tagfilter)5121if($tagfilter||$searchtext);51225123$order||=$default_projects_order;5124$from=0unlessdefined$from;5125$to=$#projectsif(!defined$to||$#projects<$to);51265127# short circuit5128if($from>$to) {5129print"<center>\n".5130"<b>No such projects found</b><br />\n".5131"Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".5132"</center>\n<br />\n";5133return;5134}51355136@projects= sort_projects_list(\@projects,$order);51375138if($show_ctags) {5139my$ctags= git_gather_all_ctags(\@projects);5140my$cloud= git_populate_project_tagcloud($ctags);5141print git_show_project_tagcloud($cloud,64);5142}51435144print"<table class=\"project_list\">\n";5145unless($no_header) {5146print"<tr>\n";5147if($check_forks) {5148print"<th></th>\n";5149}5150 print_sort_th('project',$order,'Project');5151 print_sort_th('descr',$order,'Description');5152 print_sort_th('owner',$order,'Owner');5153 print_sort_th('age',$order,'Last Change');5154print"<th></th>\n".# for links5155"</tr>\n";5156}51575158if($projects_list_group_categories) {5159# only display categories with projects in the $from-$to window5160@projects=sort{$a->{'category'}cmp$b->{'category'}}@projects[$from..$to];5161my%categories= build_projlist_by_category(\@projects,$from,$to);5162foreachmy$cat(sort keys%categories) {5163unless($cateq"") {5164print"<tr>\n";5165if($check_forks) {5166print"<td></td>\n";5167}5168print"<td class=\"category\"colspan=\"5\">".esc_html($cat)."</td>\n";5169print"</tr>\n";5170}51715172 git_project_list_rows($categories{$cat},undef,undef,$check_forks);5173}5174}else{5175 git_project_list_rows(\@projects,$from,$to,$check_forks);5176}51775178if(defined$extra) {5179print"<tr>\n";5180if($check_forks) {5181print"<td></td>\n";5182}5183print"<td colspan=\"5\">$extra</td>\n".5184"</tr>\n";5185}5186print"</table>\n";5187}51885189sub git_log_body {5190# uses global variable $project5191my($commitlist,$from,$to,$refs,$extra) =@_;51925193$from=0unlessdefined$from;5194$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);51955196for(my$i=0;$i<=$to;$i++) {5197my%co= %{$commitlist->[$i]};5198next if!%co;5199my$commit=$co{'id'};5200my$ref= format_ref_marker($refs,$commit);5201 git_print_header_div('commit',5202"<span class=\"age\">$co{'age_string'}</span>".5203 esc_html($co{'title'}) .$ref,5204$commit);5205print"<div class=\"title_text\">\n".5206"<div class=\"log_link\">\n".5207$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5208" | ".5209$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5210" | ".5211$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5212"<br/>\n".5213"</div>\n";5214 git_print_authorship(\%co, -tag =>'span');5215print"<br/>\n</div>\n";52165217print"<div class=\"log_body\">\n";5218 git_print_log($co{'comment'}, -final_empty_line=>1);5219print"</div>\n";5220}5221if($extra) {5222print"<div class=\"page_nav\">\n";5223print"$extra\n";5224print"</div>\n";5225}5226}52275228sub git_shortlog_body {5229# uses global variable $project5230my($commitlist,$from,$to,$refs,$extra) =@_;52315232$from=0unlessdefined$from;5233$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);52345235print"<table class=\"shortlog\">\n";5236my$alternate=1;5237for(my$i=$from;$i<=$to;$i++) {5238my%co= %{$commitlist->[$i]};5239my$commit=$co{'id'};5240my$ref= format_ref_marker($refs,$commit);5241if($alternate) {5242print"<tr class=\"dark\">\n";5243}else{5244print"<tr class=\"light\">\n";5245}5246$alternate^=1;5247# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .5248print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5249 format_author_html('td', \%co,10) ."<td>";5250print format_subject_html($co{'title'},$co{'title_short'},5251 href(action=>"commit", hash=>$commit),$ref);5252print"</td>\n".5253"<td class=\"link\">".5254$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".5255$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".5256$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");5257my$snapshot_links= format_snapshot_links($commit);5258if(defined$snapshot_links) {5259print" | ".$snapshot_links;5260}5261print"</td>\n".5262"</tr>\n";5263}5264if(defined$extra) {5265print"<tr>\n".5266"<td colspan=\"4\">$extra</td>\n".5267"</tr>\n";5268}5269print"</table>\n";5270}52715272sub git_history_body {5273# Warning: assumes constant type (blob or tree) during history5274my($commitlist,$from,$to,$refs,$extra,5275$file_name,$file_hash,$ftype) =@_;52765277$from=0unlessdefined$from;5278$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});52795280print"<table class=\"history\">\n";5281my$alternate=1;5282for(my$i=$from;$i<=$to;$i++) {5283my%co= %{$commitlist->[$i]};5284if(!%co) {5285next;5286}5287my$commit=$co{'id'};52885289my$ref= format_ref_marker($refs,$commit);52905291if($alternate) {5292print"<tr class=\"dark\">\n";5293}else{5294print"<tr class=\"light\">\n";5295}5296$alternate^=1;5297print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5298# shortlog: format_author_html('td', \%co, 10)5299 format_author_html('td', \%co,15,3) ."<td>";5300# originally git_history used chop_str($co{'title'}, 50)5301print format_subject_html($co{'title'},$co{'title_short'},5302 href(action=>"commit", hash=>$commit),$ref);5303print"</td>\n".5304"<td class=\"link\">".5305$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".5306$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");53075308if($ftypeeq'blob') {5309my$blob_current=$file_hash;5310my$blob_parent= git_get_hash_by_path($commit,$file_name);5311if(defined$blob_current&&defined$blob_parent&&5312$blob_currentne$blob_parent) {5313print" | ".5314$cgi->a({-href => href(action=>"blobdiff",5315 hash=>$blob_current, hash_parent=>$blob_parent,5316 hash_base=>$hash_base, hash_parent_base=>$commit,5317 file_name=>$file_name)},5318"diff to current");5319}5320}5321print"</td>\n".5322"</tr>\n";5323}5324if(defined$extra) {5325print"<tr>\n".5326"<td colspan=\"4\">$extra</td>\n".5327"</tr>\n";5328}5329print"</table>\n";5330}53315332sub git_tags_body {5333# uses global variable $project5334my($taglist,$from,$to,$extra) =@_;5335$from=0unlessdefined$from;5336$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);53375338print"<table class=\"tags\">\n";5339my$alternate=1;5340for(my$i=$from;$i<=$to;$i++) {5341my$entry=$taglist->[$i];5342my%tag=%$entry;5343my$comment=$tag{'subject'};5344my$comment_short;5345if(defined$comment) {5346$comment_short= chop_str($comment,30,5);5347}5348if($alternate) {5349print"<tr class=\"dark\">\n";5350}else{5351print"<tr class=\"light\">\n";5352}5353$alternate^=1;5354if(defined$tag{'age'}) {5355print"<td><i>$tag{'age'}</i></td>\n";5356}else{5357print"<td></td>\n";5358}5359print"<td>".5360$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),5361-class=>"list name"}, esc_html($tag{'name'})) .5362"</td>\n".5363"<td>";5364if(defined$comment) {5365print format_subject_html($comment,$comment_short,5366 href(action=>"tag", hash=>$tag{'id'}));5367}5368print"</td>\n".5369"<td class=\"selflink\">";5370if($tag{'type'}eq"tag") {5371print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");5372}else{5373print" ";5374}5375print"</td>\n".5376"<td class=\"link\">"." | ".5377$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});5378if($tag{'reftype'}eq"commit") {5379print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .5380" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");5381}elsif($tag{'reftype'}eq"blob") {5382print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");5383}5384print"</td>\n".5385"</tr>";5386}5387if(defined$extra) {5388print"<tr>\n".5389"<td colspan=\"5\">$extra</td>\n".5390"</tr>\n";5391}5392print"</table>\n";5393}53945395sub git_heads_body {5396# uses global variable $project5397my($headlist,$head,$from,$to,$extra) =@_;5398$from=0unlessdefined$from;5399$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);54005401print"<table class=\"heads\">\n";5402my$alternate=1;5403for(my$i=$from;$i<=$to;$i++) {5404my$entry=$headlist->[$i];5405my%ref=%$entry;5406my$curr=$ref{'id'}eq$head;5407if($alternate) {5408print"<tr class=\"dark\">\n";5409}else{5410print"<tr class=\"light\">\n";5411}5412$alternate^=1;5413print"<td><i>$ref{'age'}</i></td>\n".5414($curr?"<td class=\"current_head\">":"<td>") .5415$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),5416-class=>"list name"},esc_html($ref{'name'})) .5417"</td>\n".5418"<td class=\"link\">".5419$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".5420$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".5421$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})},"tree") .5422"</td>\n".5423"</tr>";5424}5425if(defined$extra) {5426print"<tr>\n".5427"<td colspan=\"3\">$extra</td>\n".5428"</tr>\n";5429}5430print"</table>\n";5431}54325433# Display a single remote block5434sub git_remote_block {5435my($remote,$rdata,$limit,$head) =@_;54365437my$heads=$rdata->{'heads'};5438my$fetch=$rdata->{'fetch'};5439my$push=$rdata->{'push'};54405441my$urls_table="<table class=\"projects_list\">\n";54425443if(defined$fetch) {5444if($fetcheq$push) {5445$urls_table.= format_repo_url("URL",$fetch);5446}else{5447$urls_table.= format_repo_url("Fetch URL",$fetch);5448$urls_table.= format_repo_url("Push URL",$push)ifdefined$push;5449}5450}elsif(defined$push) {5451$urls_table.= format_repo_url("Push URL",$push);5452}else{5453$urls_table.= format_repo_url("","No remote URL");5454}54555456$urls_table.="</table>\n";54575458my$dots;5459if(defined$limit&&$limit<@$heads) {5460$dots=$cgi->a({-href => href(action=>"remotes", hash=>$remote)},"...");5461}54625463print$urls_table;5464 git_heads_body($heads,$head,0,$limit,$dots);5465}54665467# Display a list of remote names with the respective fetch and push URLs5468sub git_remotes_list {5469my($remotedata,$limit) =@_;5470print"<table class=\"heads\">\n";5471my$alternate=1;5472my@remotes=sort keys%$remotedata;54735474my$limited=$limit&&$limit<@remotes;54755476$#remotes=$limit-1if$limited;54775478while(my$remote=shift@remotes) {5479my$rdata=$remotedata->{$remote};5480my$fetch=$rdata->{'fetch'};5481my$push=$rdata->{'push'};5482if($alternate) {5483print"<tr class=\"dark\">\n";5484}else{5485print"<tr class=\"light\">\n";5486}5487$alternate^=1;5488print"<td>".5489$cgi->a({-href=> href(action=>'remotes', hash=>$remote),5490-class=>"list name"},esc_html($remote)) .5491"</td>";5492print"<td class=\"link\">".5493(defined$fetch?$cgi->a({-href=>$fetch},"fetch") :"fetch") .5494" | ".5495(defined$push?$cgi->a({-href=>$push},"push") :"push") .5496"</td>";54975498print"</tr>\n";5499}55005501if($limited) {5502print"<tr>\n".5503"<td colspan=\"3\">".5504$cgi->a({-href => href(action=>"remotes")},"...") .5505"</td>\n"."</tr>\n";5506}55075508print"</table>";5509}55105511# Display remote heads grouped by remote, unless there are too many5512# remotes, in which case we only display the remote names5513sub git_remotes_body {5514my($remotedata,$limit,$head) =@_;5515if($limitand$limit<keys%$remotedata) {5516 git_remotes_list($remotedata,$limit);5517}else{5518 fill_remote_heads($remotedata);5519while(my($remote,$rdata) =each%$remotedata) {5520 git_print_section({-class=>"remote", -id=>$remote},5521["remotes",$remote,$remote],sub{5522 git_remote_block($remote,$rdata,$limit,$head);5523});5524}5525}5526}55275528sub git_search_message {5529my%co=@_;55305531my$greptype;5532if($searchtypeeq'commit') {5533$greptype="--grep=";5534}elsif($searchtypeeq'author') {5535$greptype="--author=";5536}elsif($searchtypeeq'committer') {5537$greptype="--committer=";5538}5539$greptype.=$searchtext;5540my@commitlist= parse_commits($hash,101, (100*$page),undef,5541$greptype,'--regexp-ignore-case',5542$search_use_regexp?'--extended-regexp':'--fixed-strings');55435544my$paging_nav='';5545if($page>0) {5546$paging_nav.=5547$cgi->a({-href => href(-replay=>1, page=>undef)},5548"first") .5549" ⋅ ".5550$cgi->a({-href => href(-replay=>1, page=>$page-1),5551-accesskey =>"p", -title =>"Alt-p"},"prev");5552}else{5553$paging_nav.="first ⋅ prev";5554}5555my$next_link='';5556if($#commitlist>=100) {5557$next_link=5558$cgi->a({-href => href(-replay=>1, page=>$page+1),5559-accesskey =>"n", -title =>"Alt-n"},"next");5560$paging_nav.=" ⋅$next_link";5561}else{5562$paging_nav.=" ⋅ next";5563}55645565 git_header_html();55665567 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5568 git_print_header_div('commit', esc_html($co{'title'}),$hash);5569if($page==0&& !@commitlist) {5570print"<p>No match.</p>\n";5571}else{5572 git_search_grep_body(\@commitlist,0,99,$next_link);5573}55745575 git_footer_html();5576}55775578sub git_search_changes {5579my%co=@_;55805581local$/="\n";5582open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5583'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5584($search_use_regexp?'--pickaxe-regex': ())5585or die_error(500,"Open git-log failed");55865587 git_header_html();55885589 git_print_page_nav('','',$hash,$co{'tree'},$hash);5590 git_print_header_div('commit', esc_html($co{'title'}),$hash);55915592print"<table class=\"pickaxe search\">\n";5593my$alternate=1;5594undef%co;5595my@files;5596while(my$line= <$fd>) {5597chomp$line;5598next unless$line;55995600my%set= parse_difftree_raw_line($line);5601if(defined$set{'commit'}) {5602# finish previous commit5603if(%co) {5604print"</td>\n".5605"<td class=\"link\">".5606$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},5607"commit") .5608" | ".5609$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},5610 hash_base=>$co{'id'})},5611"tree") .5612"</td>\n".5613"</tr>\n";5614}56155616if($alternate) {5617print"<tr class=\"dark\">\n";5618}else{5619print"<tr class=\"light\">\n";5620}5621$alternate^=1;5622%co= parse_commit($set{'commit'});5623my$author= chop_and_escape_str($co{'author_name'},15,5);5624print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5625"<td><i>$author</i></td>\n".5626"<td>".5627$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5628-class=>"list subject"},5629 chop_and_escape_str($co{'title'},50) ."<br/>");5630}elsif(defined$set{'to_id'}) {5631next if($set{'to_id'} =~m/^0{40}$/);56325633print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5634 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5635-class=>"list"},5636"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5637"<br/>\n";5638}5639}5640close$fd;56415642# finish last commit (warning: repetition!)5643if(%co) {5644print"</td>\n".5645"<td class=\"link\">".5646$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},5647"commit") .5648" | ".5649$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},5650 hash_base=>$co{'id'})},5651"tree") .5652"</td>\n".5653"</tr>\n";5654}56555656print"</table>\n";56575658 git_footer_html();5659}56605661sub git_search_files {5662my%co=@_;56635664local$/="\n";5665open my$fd,"-|", git_cmd(),'grep','-n',5666$search_use_regexp? ('-E','-i') :'-F',5667$searchtext,$co{'tree'}5668or die_error(500,"Open git-grep failed");56695670 git_header_html();56715672 git_print_page_nav('','',$hash,$co{'tree'},$hash);5673 git_print_header_div('commit', esc_html($co{'title'}),$hash);56745675print"<table class=\"grep_search\">\n";5676my$alternate=1;5677my$matches=0;5678my$lastfile='';5679while(my$line= <$fd>) {5680chomp$line;5681my($file,$lno,$ltext,$binary);5682last if($matches++>1000);5683if($line=~/^Binary file (.+) matches$/) {5684$file=$1;5685$binary=1;5686}else{5687(undef,$file,$lno,$ltext) =split(/:/,$line,4);5688}5689if($filene$lastfile) {5690$lastfileand print"</td></tr>\n";5691if($alternate++) {5692print"<tr class=\"dark\">\n";5693}else{5694print"<tr class=\"light\">\n";5695}5696print"<td class=\"list\">".5697$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5698 file_name=>"$file"),5699-class=>"list"}, esc_path($file));5700print"</td><td>\n";5701$lastfile=$file;5702}5703if($binary) {5704print"<div class=\"binary\">Binary file</div>\n";5705}else{5706$ltext= untabify($ltext);5707if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5708$ltext= esc_html($1, -nbsp=>1);5709$ltext.='<span class="match">';5710$ltext.= esc_html($2, -nbsp=>1);5711$ltext.='</span>';5712$ltext.= esc_html($3, -nbsp=>1);5713}else{5714$ltext= esc_html($ltext, -nbsp=>1);5715}5716print"<div class=\"pre\">".5717$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5718 file_name=>"$file").'#l'.$lno,5719-class=>"linenr"},sprintf('%4i',$lno))5720.' '.$ltext."</div>\n";5721}5722}5723if($lastfile) {5724print"</td></tr>\n";5725if($matches>1000) {5726print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5727}5728}else{5729print"<div class=\"diff nodifferences\">No matches found</div>\n";5730}5731close$fd;57325733print"</table>\n";57345735 git_footer_html();5736}57375738sub git_search_grep_body {5739my($commitlist,$from,$to,$extra) =@_;5740$from=0unlessdefined$from;5741$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);57425743print"<table class=\"commit_search\">\n";5744my$alternate=1;5745for(my$i=$from;$i<=$to;$i++) {5746my%co= %{$commitlist->[$i]};5747if(!%co) {5748next;5749}5750my$commit=$co{'id'};5751if($alternate) {5752print"<tr class=\"dark\">\n";5753}else{5754print"<tr class=\"light\">\n";5755}5756$alternate^=1;5757print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5758 format_author_html('td', \%co,15,5) .5759"<td>".5760$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5761-class=>"list subject"},5762 chop_and_escape_str($co{'title'},50) ."<br/>");5763my$comment=$co{'comment'};5764foreachmy$line(@$comment) {5765if($line=~m/^(.*?)($search_regexp)(.*)$/i) {5766my($lead,$match,$trail) = ($1,$2,$3);5767$match= chop_str($match,70,5,'center');5768my$contextlen=int((80-length($match))/2);5769$contextlen=30if($contextlen>30);5770$lead= chop_str($lead,$contextlen,10,'left');5771$trail= chop_str($trail,$contextlen,10,'right');57725773$lead= esc_html($lead);5774$match= esc_html($match);5775$trail= esc_html($trail);57765777print"$lead<span class=\"match\">$match</span>$trail<br />";5778}5779}5780print"</td>\n".5781"<td class=\"link\">".5782$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5783" | ".5784$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .5785" | ".5786$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5787print"</td>\n".5788"</tr>\n";5789}5790if(defined$extra) {5791print"<tr>\n".5792"<td colspan=\"3\">$extra</td>\n".5793"</tr>\n";5794}5795print"</table>\n";5796}57975798## ======================================================================5799## ======================================================================5800## actions58015802sub git_project_list {5803my$order=$input_params{'order'};5804if(defined$order&&$order!~m/none|project|descr|owner|age/) {5805 die_error(400,"Unknown order parameter");5806}58075808my@list= git_get_projects_list();5809if(!@list) {5810 die_error(404,"No projects found");5811}58125813 git_header_html();5814if(defined$home_text&& -f $home_text) {5815print"<div class=\"index_include\">\n";5816 insert_file($home_text);5817print"</div>\n";5818}5819print$cgi->startform(-method=>"get") .5820"<p class=\"projsearch\">Search:\n".5821$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".5822"</p>".5823$cgi->end_form() ."\n";5824 git_project_list_body(\@list,$order);5825 git_footer_html();5826}58275828sub git_forks {5829my$order=$input_params{'order'};5830if(defined$order&&$order!~m/none|project|descr|owner|age/) {5831 die_error(400,"Unknown order parameter");5832}58335834my@list= git_get_projects_list($project);5835if(!@list) {5836 die_error(404,"No forks found");5837}58385839 git_header_html();5840 git_print_page_nav('','');5841 git_print_header_div('summary',"$projectforks");5842 git_project_list_body(\@list,$order);5843 git_footer_html();5844}58455846sub git_project_index {5847my@projects= git_get_projects_list();5848if(!@projects) {5849 die_error(404,"No projects found");5850}58515852print$cgi->header(5853-type =>'text/plain',5854-charset =>'utf-8',5855-content_disposition =>'inline; filename="index.aux"');58565857foreachmy$pr(@projects) {5858if(!exists$pr->{'owner'}) {5859$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");5860}58615862my($path,$owner) = ($pr->{'path'},$pr->{'owner'});5863# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '5864$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5865$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5866$path=~s/ /\+/g;5867$owner=~s/ /\+/g;58685869print"$path$owner\n";5870}5871}58725873sub git_summary {5874my$descr= git_get_project_description($project) ||"none";5875my%co= parse_commit("HEAD");5876my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();5877my$head=$co{'id'};5878my$remote_heads= gitweb_check_feature('remote_heads');58795880my$owner= git_get_project_owner($project);58815882my$refs= git_get_references();5883# These get_*_list functions return one more to allow us to see if5884# there are more ...5885my@taglist= git_get_tags_list(16);5886my@headlist= git_get_heads_list(16);5887my%remotedata=$remote_heads? git_get_remotes_list() : ();5888my@forklist;5889my$check_forks= gitweb_check_feature('forks');58905891if($check_forks) {5892# find forks of a project5893@forklist= git_get_projects_list($project);5894# filter out forks of forks5895@forklist= filter_forks_from_projects_list(\@forklist)5896if(@forklist);5897}58985899 git_header_html();5900 git_print_page_nav('summary','',$head);59015902print"<div class=\"title\"> </div>\n";5903print"<table class=\"projects_list\">\n".5904"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".5905"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";5906if(defined$cd{'rfc2822'}) {5907print"<tr id=\"metadata_lchange\"><td>last change</td>".5908"<td>".format_timestamp_html(\%cd)."</td></tr>\n";5909}59105911# use per project git URL list in $projectroot/$project/cloneurl5912# or make project git URL from git base URL and project name5913my$url_tag="URL";5914my@url_list= git_get_project_url_list($project);5915@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;5916foreachmy$git_url(@url_list) {5917next unless$git_url;5918print format_repo_url($url_tag,$git_url);5919$url_tag="";5920}59215922# Tag cloud5923my$show_ctags= gitweb_check_feature('ctags');5924if($show_ctags) {5925my$ctags= git_get_project_ctags($project);5926if(%$ctags) {5927# without ability to add tags, don't show if there are none5928my$cloud= git_populate_project_tagcloud($ctags);5929print"<tr id=\"metadata_ctags\">".5930"<td>content tags</td>".5931"<td>".git_show_project_tagcloud($cloud,48)."</td>".5932"</tr>\n";5933}5934}59355936print"</table>\n";59375938# If XSS prevention is on, we don't include README.html.5939# TODO: Allow a readme in some safe format.5940if(!$prevent_xss&& -s "$projectroot/$project/README.html") {5941print"<div class=\"title\">readme</div>\n".5942"<div class=\"readme\">\n";5943 insert_file("$projectroot/$project/README.html");5944print"\n</div>\n";# class="readme"5945}59465947# we need to request one more than 16 (0..15) to check if5948# those 16 are all5949my@commitlist=$head? parse_commits($head,17) : ();5950if(@commitlist) {5951 git_print_header_div('shortlog');5952 git_shortlog_body(\@commitlist,0,15,$refs,5953$#commitlist<=15?undef:5954$cgi->a({-href => href(action=>"shortlog")},"..."));5955}59565957if(@taglist) {5958 git_print_header_div('tags');5959 git_tags_body(\@taglist,0,15,5960$#taglist<=15?undef:5961$cgi->a({-href => href(action=>"tags")},"..."));5962}59635964if(@headlist) {5965 git_print_header_div('heads');5966 git_heads_body(\@headlist,$head,0,15,5967$#headlist<=15?undef:5968$cgi->a({-href => href(action=>"heads")},"..."));5969}59705971if(%remotedata) {5972 git_print_header_div('remotes');5973 git_remotes_body(\%remotedata,15,$head);5974}59755976if(@forklist) {5977 git_print_header_div('forks');5978 git_project_list_body(\@forklist,'age',0,15,5979$#forklist<=15?undef:5980$cgi->a({-href => href(action=>"forks")},"..."),5981'no_header');5982}59835984 git_footer_html();5985}59865987sub git_tag {5988my%tag= parse_tag($hash);59895990if(!%tag) {5991 die_error(404,"Unknown tag object");5992}59935994my$head= git_get_head_hash($project);5995 git_header_html();5996 git_print_page_nav('','',$head,undef,$head);5997 git_print_header_div('commit', esc_html($tag{'name'}),$hash);5998print"<div class=\"title_text\">\n".5999"<table class=\"object_header\">\n".6000"<tr>\n".6001"<td>object</td>\n".6002"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},6003$tag{'object'}) ."</td>\n".6004"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},6005$tag{'type'}) ."</td>\n".6006"</tr>\n";6007if(defined($tag{'author'})) {6008 git_print_authorship_rows(\%tag,'author');6009}6010print"</table>\n\n".6011"</div>\n";6012print"<div class=\"page_body\">";6013my$comment=$tag{'comment'};6014foreachmy$line(@$comment) {6015chomp$line;6016print esc_html($line, -nbsp=>1) ."<br/>\n";6017}6018print"</div>\n";6019 git_footer_html();6020}60216022sub git_blame_common {6023my$format=shift||'porcelain';6024if($formateq'porcelain'&&$cgi->param('js')) {6025$format='incremental';6026$action='blame_incremental';# for page title etc6027}60286029# permissions6030 gitweb_check_feature('blame')6031or die_error(403,"Blame view not allowed");60326033# error checking6034 die_error(400,"No file name given")unless$file_name;6035$hash_base||= git_get_head_hash($project);6036 die_error(404,"Couldn't find base commit")unless$hash_base;6037my%co= parse_commit($hash_base)6038or die_error(404,"Commit not found");6039my$ftype="blob";6040if(!defined$hash) {6041$hash= git_get_hash_by_path($hash_base,$file_name,"blob")6042or die_error(404,"Error looking up file");6043}else{6044$ftype= git_get_type($hash);6045if($ftype!~"blob") {6046 die_error(400,"Object is not a blob");6047}6048}60496050my$fd;6051if($formateq'incremental') {6052# get file contents (as base)6053open$fd,"-|", git_cmd(),'cat-file','blob',$hash6054or die_error(500,"Open git-cat-file failed");6055}elsif($formateq'data') {6056# run git-blame --incremental6057open$fd,"-|", git_cmd(),"blame","--incremental",6058$hash_base,"--",$file_name6059or die_error(500,"Open git-blame --incremental failed");6060}else{6061# run git-blame --porcelain6062open$fd,"-|", git_cmd(),"blame",'-p',6063$hash_base,'--',$file_name6064or die_error(500,"Open git-blame --porcelain failed");6065}60666067# incremental blame data returns early6068if($formateq'data') {6069print$cgi->header(6070-type=>"text/plain", -charset =>"utf-8",6071-status=>"200 OK");6072local$| =1;# output autoflush6073printwhile<$fd>;6074close$fd6075or print"ERROR$!\n";60766077print'END';6078if(defined$t0&& gitweb_check_feature('timed')) {6079print' '.6080 tv_interval($t0, [ gettimeofday() ]).6081' '.$number_of_git_cmds;6082}6083print"\n";60846085return;6086}60876088# page header6089 git_header_html();6090my$formats_nav=6091$cgi->a({-href => href(action=>"blob", -replay=>1)},6092"blob") .6093" | ";6094if($formateq'incremental') {6095$formats_nav.=6096$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},6097"blame") ." (non-incremental)";6098}else{6099$formats_nav.=6100$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},6101"blame") ." (incremental)";6102}6103$formats_nav.=6104" | ".6105$cgi->a({-href => href(action=>"history", -replay=>1)},6106"history") .6107" | ".6108$cgi->a({-href => href(action=>$action, file_name=>$file_name)},6109"HEAD");6110 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6111 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6112 git_print_page_path($file_name,$ftype,$hash_base);61136114# page body6115if($formateq'incremental') {6116print"<noscript>\n<div class=\"error\"><center><b>\n".6117"This page requires JavaScript to run.\nUse ".6118$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},6119'this page').6120" instead.\n".6121"</b></center></div>\n</noscript>\n";61226123print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;6124}61256126print qq!<div class="page_body">\n!;6127print qq!<div id="progress_info">.../ ...</div>\n!6128if($formateq'incremental');6129print qq!<table id="blame_table"class="blame" width="100%">\n!.6130#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.6131 qq!<thead>\n!.6132 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.6133 qq!</thead>\n!.6134 qq!<tbody>\n!;61356136my@rev_color=qw(light dark);6137my$num_colors=scalar(@rev_color);6138my$current_color=0;61396140if($formateq'incremental') {6141my$color_class=$rev_color[$current_color];61426143#contents of a file6144my$linenr=0;6145 LINE:6146while(my$line= <$fd>) {6147chomp$line;6148$linenr++;61496150print qq!<tr id="l$linenr"class="$color_class">!.6151 qq!<td class="sha1"><a href=""> </a></td>!.6152 qq!<td class="linenr">!.6153 qq!<a class="linenr" href="">$linenr</a></td>!;6154print qq!<td class="pre">! . esc_html($line) ."</td>\n";6155print qq!</tr>\n!;6156}61576158}else{# porcelain, i.e. ordinary blame6159my%metainfo= ();# saves information about commits61606161# blame data6162 LINE:6163while(my$line= <$fd>) {6164chomp$line;6165# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]6166# no <lines in group> for subsequent lines in group of lines6167my($full_rev,$orig_lineno,$lineno,$group_size) =6168($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);6169if(!exists$metainfo{$full_rev}) {6170$metainfo{$full_rev} = {'nprevious'=>0};6171}6172my$meta=$metainfo{$full_rev};6173my$data;6174while($data= <$fd>) {6175chomp$data;6176last if($data=~s/^\t//);# contents of line6177if($data=~/^(\S+)(?: (.*))?$/) {6178$meta->{$1} =$2unlessexists$meta->{$1};6179}6180if($data=~/^previous /) {6181$meta->{'nprevious'}++;6182}6183}6184my$short_rev=substr($full_rev,0,8);6185my$author=$meta->{'author'};6186my%date=6187 parse_date($meta->{'author-time'},$meta->{'author-tz'});6188my$date=$date{'iso-tz'};6189if($group_size) {6190$current_color= ($current_color+1) %$num_colors;6191}6192my$tr_class=$rev_color[$current_color];6193$tr_class.=' boundary'if(exists$meta->{'boundary'});6194$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);6195$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);6196print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";6197if($group_size) {6198print"<td class=\"sha1\"";6199print" title=\"". esc_html($author) .",$date\"";6200print" rowspan=\"$group_size\""if($group_size>1);6201print">";6202print$cgi->a({-href => href(action=>"commit",6203 hash=>$full_rev,6204 file_name=>$file_name)},6205 esc_html($short_rev));6206if($group_size>=2) {6207my@author_initials= ($author=~/\b([[:upper:]])\B/g);6208if(@author_initials) {6209print"<br />".6210 esc_html(join('',@author_initials));6211# or join('.', ...)6212}6213}6214print"</td>\n";6215}6216# 'previous' <sha1 of parent commit> <filename at commit>6217if(exists$meta->{'previous'} &&6218$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {6219$meta->{'parent'} =$1;6220$meta->{'file_parent'} = unquote($2);6221}6222my$linenr_commit=6223exists($meta->{'parent'}) ?6224$meta->{'parent'} :$full_rev;6225my$linenr_filename=6226exists($meta->{'file_parent'}) ?6227$meta->{'file_parent'} : unquote($meta->{'filename'});6228my$blamed= href(action =>'blame',6229 file_name =>$linenr_filename,6230 hash_base =>$linenr_commit);6231print"<td class=\"linenr\">";6232print$cgi->a({ -href =>"$blamed#l$orig_lineno",6233-class=>"linenr"},6234 esc_html($lineno));6235print"</td>";6236print"<td class=\"pre\">". esc_html($data) ."</td>\n";6237print"</tr>\n";6238}# end while62396240}62416242# footer6243print"</tbody>\n".6244"</table>\n";# class="blame"6245print"</div>\n";# class="blame_body"6246close$fd6247or print"Reading blob failed\n";62486249 git_footer_html();6250}62516252sub git_blame {6253 git_blame_common();6254}62556256sub git_blame_incremental {6257 git_blame_common('incremental');6258}62596260sub git_blame_data {6261 git_blame_common('data');6262}62636264sub git_tags {6265my$head= git_get_head_hash($project);6266 git_header_html();6267 git_print_page_nav('','',$head,undef,$head,format_ref_views('tags'));6268 git_print_header_div('summary',$project);62696270my@tagslist= git_get_tags_list();6271if(@tagslist) {6272 git_tags_body(\@tagslist);6273}6274 git_footer_html();6275}62766277sub git_heads {6278my$head= git_get_head_hash($project);6279 git_header_html();6280 git_print_page_nav('','',$head,undef,$head,format_ref_views('heads'));6281 git_print_header_div('summary',$project);62826283my@headslist= git_get_heads_list();6284if(@headslist) {6285 git_heads_body(\@headslist,$head);6286}6287 git_footer_html();6288}62896290# used both for single remote view and for list of all the remotes6291sub git_remotes {6292 gitweb_check_feature('remote_heads')6293or die_error(403,"Remote heads view is disabled");62946295my$head= git_get_head_hash($project);6296my$remote=$input_params{'hash'};62976298my$remotedata= git_get_remotes_list($remote);6299 die_error(500,"Unable to get remote information")unlessdefined$remotedata;63006301unless(%$remotedata) {6302 die_error(404,defined$remote?6303"Remote$remotenot found":6304"No remotes found");6305}63066307 git_header_html(undef,undef, -action_extra =>$remote);6308 git_print_page_nav('','',$head,undef,$head,6309 format_ref_views($remote?'':'remotes'));63106311 fill_remote_heads($remotedata);6312if(defined$remote) {6313 git_print_header_div('remotes',"$remoteremote for$project");6314 git_remote_block($remote,$remotedata->{$remote},undef,$head);6315}else{6316 git_print_header_div('summary',"$projectremotes");6317 git_remotes_body($remotedata,undef,$head);6318}63196320 git_footer_html();6321}63226323sub git_blob_plain {6324my$type=shift;6325my$expires;63266327if(!defined$hash) {6328if(defined$file_name) {6329my$base=$hash_base|| git_get_head_hash($project);6330$hash= git_get_hash_by_path($base,$file_name,"blob")6331or die_error(404,"Cannot find file");6332}else{6333 die_error(400,"No file name defined");6334}6335}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6336# blobs defined by non-textual hash id's can be cached6337$expires="+1d";6338}63396340open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6341or die_error(500,"Open git-cat-file blob '$hash' failed");63426343# content-type (can include charset)6344$type= blob_contenttype($fd,$file_name,$type);63456346# "save as" filename, even when no $file_name is given6347my$save_as="$hash";6348if(defined$file_name) {6349$save_as=$file_name;6350}elsif($type=~m/^text\//) {6351$save_as.='.txt';6352}63536354# With XSS prevention on, blobs of all types except a few known safe6355# ones are served with "Content-Disposition: attachment" to make sure6356# they don't run in our security domain. For certain image types,6357# blob view writes an <img> tag referring to blob_plain view, and we6358# want to be sure not to break that by serving the image as an6359# attachment (though Firefox 3 doesn't seem to care).6360my$sandbox=$prevent_xss&&6361$type!~m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;63626363# serve text/* as text/plain6364if($prevent_xss&&6365($type=~m!^text/[a-z]+\b(.*)$!||6366($type=~m!^[a-z]+/[a-z]\+xml\b(.*)$!&& -T $fd))) {6367my$rest=$1;6368$rest=defined$rest?$rest:'';6369$type="text/plain$rest";6370}63716372print$cgi->header(6373-type =>$type,6374-expires =>$expires,6375-content_disposition =>6376($sandbox?'attachment':'inline')6377.'; filename="'.$save_as.'"');6378local$/=undef;6379binmode STDOUT,':raw';6380print<$fd>;6381binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi6382close$fd;6383}63846385sub git_blob {6386my$expires;63876388if(!defined$hash) {6389if(defined$file_name) {6390my$base=$hash_base|| git_get_head_hash($project);6391$hash= git_get_hash_by_path($base,$file_name,"blob")6392or die_error(404,"Cannot find file");6393}else{6394 die_error(400,"No file name defined");6395}6396}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6397# blobs defined by non-textual hash id's can be cached6398$expires="+1d";6399}64006401my$have_blame= gitweb_check_feature('blame');6402open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6403or die_error(500,"Couldn't cat$file_name,$hash");6404my$mimetype= blob_mimetype($fd,$file_name);6405# use 'blob_plain' (aka 'raw') view for files that cannot be displayed6406if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {6407close$fd;6408return git_blob_plain($mimetype);6409}6410# we can have blame only for text/* mimetype6411$have_blame&&= ($mimetype=~m!^text/!);64126413my$highlight= gitweb_check_feature('highlight');6414my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);6415$fd= run_highlighter($fd,$highlight,$syntax)6416if$syntax;64176418 git_header_html(undef,$expires);6419my$formats_nav='';6420if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6421if(defined$file_name) {6422if($have_blame) {6423$formats_nav.=6424$cgi->a({-href => href(action=>"blame", -replay=>1)},6425"blame") .6426" | ";6427}6428$formats_nav.=6429$cgi->a({-href => href(action=>"history", -replay=>1)},6430"history") .6431" | ".6432$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},6433"raw") .6434" | ".6435$cgi->a({-href => href(action=>"blob",6436 hash_base=>"HEAD", file_name=>$file_name)},6437"HEAD");6438}else{6439$formats_nav.=6440$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},6441"raw");6442}6443 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6444 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6445}else{6446print"<div class=\"page_nav\">\n".6447"<br/><br/></div>\n".6448"<div class=\"title\">".esc_html($hash)."</div>\n";6449}6450 git_print_page_path($file_name,"blob",$hash_base);6451print"<div class=\"page_body\">\n";6452if($mimetype=~m!^image/!) {6453print qq!<img type="!.esc_attr($mimetype).qq!"!;6454if($file_name) {6455print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;6456}6457print qq! src="! .6458 href(action=>"blob_plain", hash=>$hash,6459 hash_base=>$hash_base, file_name=>$file_name) .6460 qq!"/>\n!;6461}else{6462my$nr;6463while(my$line= <$fd>) {6464chomp$line;6465$nr++;6466$line= untabify($line);6467printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,6468$nr, esc_attr(href(-replay =>1)),$nr,$nr,$syntax?$line: esc_html($line, -nbsp=>1);6469}6470}6471close$fd6472or print"Reading blob failed.\n";6473print"</div>";6474 git_footer_html();6475}64766477sub git_tree {6478if(!defined$hash_base) {6479$hash_base="HEAD";6480}6481if(!defined$hash) {6482if(defined$file_name) {6483$hash= git_get_hash_by_path($hash_base,$file_name,"tree");6484}else{6485$hash=$hash_base;6486}6487}6488 die_error(404,"No such tree")unlessdefined($hash);64896490my$show_sizes= gitweb_check_feature('show-sizes');6491my$have_blame= gitweb_check_feature('blame');64926493my@entries= ();6494{6495local$/="\0";6496open my$fd,"-|", git_cmd(),"ls-tree",'-z',6497($show_sizes?'-l': ()),@extra_options,$hash6498or die_error(500,"Open git-ls-tree failed");6499@entries=map{chomp;$_} <$fd>;6500close$fd6501or die_error(404,"Reading tree failed");6502}65036504my$refs= git_get_references();6505my$ref= format_ref_marker($refs,$hash_base);6506 git_header_html();6507my$basedir='';6508if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6509my@views_nav= ();6510if(defined$file_name) {6511push@views_nav,6512$cgi->a({-href => href(action=>"history", -replay=>1)},6513"history"),6514$cgi->a({-href => href(action=>"tree",6515 hash_base=>"HEAD", file_name=>$file_name)},6516"HEAD"),6517}6518my$snapshot_links= format_snapshot_links($hash);6519if(defined$snapshot_links) {6520# FIXME: Should be available when we have no hash base as well.6521push@views_nav,$snapshot_links;6522}6523 git_print_page_nav('tree','',$hash_base,undef,undef,6524join(' | ',@views_nav));6525 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);6526}else{6527undef$hash_base;6528print"<div class=\"page_nav\">\n";6529print"<br/><br/></div>\n";6530print"<div class=\"title\">".esc_html($hash)."</div>\n";6531}6532if(defined$file_name) {6533$basedir=$file_name;6534if($basedirne''&&substr($basedir, -1)ne'/') {6535$basedir.='/';6536}6537 git_print_page_path($file_name,'tree',$hash_base);6538}6539print"<div class=\"page_body\">\n";6540print"<table class=\"tree\">\n";6541my$alternate=1;6542# '..' (top directory) link if possible6543if(defined$hash_base&&6544defined$file_name&&$file_name=~m![^/]+$!) {6545if($alternate) {6546print"<tr class=\"dark\">\n";6547}else{6548print"<tr class=\"light\">\n";6549}6550$alternate^=1;65516552my$up=$file_name;6553$up=~s!/?[^/]+$!!;6554undef$upunless$up;6555# based on git_print_tree_entry6556print'<td class="mode">'. mode_str('040000') ."</td>\n";6557print'<td class="size"> </td>'."\n"if$show_sizes;6558print'<td class="list">';6559print$cgi->a({-href => href(action=>"tree",6560 hash_base=>$hash_base,6561 file_name=>$up)},6562"..");6563print"</td>\n";6564print"<td class=\"link\"></td>\n";65656566print"</tr>\n";6567}6568foreachmy$line(@entries) {6569my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);65706571if($alternate) {6572print"<tr class=\"dark\">\n";6573}else{6574print"<tr class=\"light\">\n";6575}6576$alternate^=1;65776578 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);65796580print"</tr>\n";6581}6582print"</table>\n".6583"</div>";6584 git_footer_html();6585}65866587sub snapshot_name {6588my($project,$hash) =@_;65896590# path/to/project.git -> project6591# path/to/project/.git -> project6592my$name= to_utf8($project);6593$name=~ s,([^/])/*\.git$,$1,;6594$name= basename($name);6595# sanitize name6596$name=~s/[[:cntrl:]]/?/g;65976598my$ver=$hash;6599if($hash=~/^[0-9a-fA-F]+$/) {6600# shorten SHA-1 hash6601my$full_hash= git_get_full_hash($project,$hash);6602if($full_hash=~/^$hash/&&length($hash) >7) {6603$ver= git_get_short_hash($project,$hash);6604}6605}elsif($hash=~m!^refs/tags/(.*)$!) {6606# tags don't need shortened SHA-1 hash6607$ver=$1;6608}else{6609# branches and other need shortened SHA-1 hash6610if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {6611$ver=$1;6612}6613$ver.='-'. git_get_short_hash($project,$hash);6614}6615# in case of hierarchical branch names6616$ver=~s!/!.!g;66176618# name = project-version_string6619$name="$name-$ver";66206621returnwantarray? ($name,$name) :$name;6622}66236624sub git_snapshot {6625my$format=$input_params{'snapshot_format'};6626if(!@snapshot_fmts) {6627 die_error(403,"Snapshots not allowed");6628}6629# default to first supported snapshot format6630$format||=$snapshot_fmts[0];6631if($format!~m/^[a-z0-9]+$/) {6632 die_error(400,"Invalid snapshot format parameter");6633}elsif(!exists($known_snapshot_formats{$format})) {6634 die_error(400,"Unknown snapshot format");6635}elsif($known_snapshot_formats{$format}{'disabled'}) {6636 die_error(403,"Snapshot format not allowed");6637}elsif(!grep($_eq$format,@snapshot_fmts)) {6638 die_error(403,"Unsupported snapshot format");6639}66406641my$type= git_get_type("$hash^{}");6642if(!$type) {6643 die_error(404,'Object does not exist');6644}elsif($typeeq'blob') {6645 die_error(400,'Object is not a tree-ish');6646}66476648my($name,$prefix) = snapshot_name($project,$hash);6649my$filename="$name$known_snapshot_formats{$format}{'suffix'}";6650my$cmd= quote_command(6651 git_cmd(),'archive',6652"--format=$known_snapshot_formats{$format}{'format'}",6653"--prefix=$prefix/",$hash);6654if(exists$known_snapshot_formats{$format}{'compressor'}) {6655$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});6656}66576658$filename=~s/(["\\])/\\$1/g;6659print$cgi->header(6660-type =>$known_snapshot_formats{$format}{'type'},6661-content_disposition =>'inline; filename="'.$filename.'"',6662-status =>'200 OK');66636664open my$fd,"-|",$cmd6665or die_error(500,"Execute git-archive failed");6666binmode STDOUT,':raw';6667print<$fd>;6668binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi6669close$fd;6670}66716672sub git_log_generic {6673my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;66746675my$head= git_get_head_hash($project);6676if(!defined$base) {6677$base=$head;6678}6679if(!defined$page) {6680$page=0;6681}6682my$refs= git_get_references();66836684my$commit_hash=$base;6685if(defined$parent) {6686$commit_hash="$parent..$base";6687}6688my@commitlist=6689 parse_commits($commit_hash,101, (100*$page),6690defined$file_name? ($file_name,"--full-history") : ());66916692my$ftype;6693if(!defined$file_hash&&defined$file_name) {6694# some commits could have deleted file in question,6695# and not have it in tree, but one of them has to have it6696for(my$i=0;$i<@commitlist;$i++) {6697$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);6698last ifdefined$file_hash;6699}6700}6701if(defined$file_hash) {6702$ftype= git_get_type($file_hash);6703}6704if(defined$file_name&& !defined$ftype) {6705 die_error(500,"Unknown type of object");6706}6707my%co;6708if(defined$file_name) {6709%co= parse_commit($base)6710or die_error(404,"Unknown commit object");6711}671267136714my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);6715my$next_link='';6716if($#commitlist>=100) {6717$next_link=6718$cgi->a({-href => href(-replay=>1, page=>$page+1),6719-accesskey =>"n", -title =>"Alt-n"},"next");6720}6721my$patch_max= gitweb_get_feature('patches');6722if($patch_max&& !defined$file_name) {6723if($patch_max<0||@commitlist<=$patch_max) {6724$paging_nav.=" ⋅ ".6725$cgi->a({-href => href(action=>"patches", -replay=>1)},6726"patches");6727}6728}67296730 git_header_html();6731 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);6732if(defined$file_name) {6733 git_print_header_div('commit', esc_html($co{'title'}),$base);6734}else{6735 git_print_header_div('summary',$project)6736}6737 git_print_page_path($file_name,$ftype,$hash_base)6738if(defined$file_name);67396740$body_subr->(\@commitlist,0,99,$refs,$next_link,6741$file_name,$file_hash,$ftype);67426743 git_footer_html();6744}67456746sub git_log {6747 git_log_generic('log', \&git_log_body,6748$hash,$hash_parent);6749}67506751sub git_commit {6752$hash||=$hash_base||"HEAD";6753my%co= parse_commit($hash)6754or die_error(404,"Unknown commit object");67556756my$parent=$co{'parent'};6757my$parents=$co{'parents'};# listref67586759# we need to prepare $formats_nav before any parameter munging6760my$formats_nav;6761if(!defined$parent) {6762# --root commitdiff6763$formats_nav.='(initial)';6764}elsif(@$parents==1) {6765# single parent commit6766$formats_nav.=6767'(parent: '.6768$cgi->a({-href => href(action=>"commit",6769 hash=>$parent)},6770 esc_html(substr($parent,0,7))) .6771')';6772}else{6773# merge commit6774$formats_nav.=6775'(merge: '.6776join(' ',map{6777$cgi->a({-href => href(action=>"commit",6778 hash=>$_)},6779 esc_html(substr($_,0,7)));6780}@$parents) .6781')';6782}6783if(gitweb_check_feature('patches') &&@$parents<=1) {6784$formats_nav.=" | ".6785$cgi->a({-href => href(action=>"patch", -replay=>1)},6786"patch");6787}67886789if(!defined$parent) {6790$parent="--root";6791}6792my@difftree;6793open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",6794@diff_opts,6795(@$parents<=1?$parent:'-c'),6796$hash,"--"6797or die_error(500,"Open git-diff-tree failed");6798@difftree=map{chomp;$_} <$fd>;6799close$fdor die_error(404,"Reading git-diff-tree failed");68006801# non-textual hash id's can be cached6802my$expires;6803if($hash=~m/^[0-9a-fA-F]{40}$/) {6804$expires="+1d";6805}6806my$refs= git_get_references();6807my$ref= format_ref_marker($refs,$co{'id'});68086809 git_header_html(undef,$expires);6810 git_print_page_nav('commit','',6811$hash,$co{'tree'},$hash,6812$formats_nav);68136814if(defined$co{'parent'}) {6815 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);6816}else{6817 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);6818}6819print"<div class=\"title_text\">\n".6820"<table class=\"object_header\">\n";6821 git_print_authorship_rows(\%co);6822print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";6823print"<tr>".6824"<td>tree</td>".6825"<td class=\"sha1\">".6826$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),6827class=>"list"},$co{'tree'}) .6828"</td>".6829"<td class=\"link\">".6830$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},6831"tree");6832my$snapshot_links= format_snapshot_links($hash);6833if(defined$snapshot_links) {6834print" | ".$snapshot_links;6835}6836print"</td>".6837"</tr>\n";68386839foreachmy$par(@$parents) {6840print"<tr>".6841"<td>parent</td>".6842"<td class=\"sha1\">".6843$cgi->a({-href => href(action=>"commit", hash=>$par),6844class=>"list"},$par) .6845"</td>".6846"<td class=\"link\">".6847$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .6848" | ".6849$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .6850"</td>".6851"</tr>\n";6852}6853print"</table>".6854"</div>\n";68556856print"<div class=\"page_body\">\n";6857 git_print_log($co{'comment'});6858print"</div>\n";68596860 git_difftree_body(\@difftree,$hash,@$parents);68616862 git_footer_html();6863}68646865sub git_object {6866# object is defined by:6867# - hash or hash_base alone6868# - hash_base and file_name6869my$type;68706871# - hash or hash_base alone6872if($hash|| ($hash_base&& !defined$file_name)) {6873my$object_id=$hash||$hash_base;68746875open my$fd,"-|", quote_command(6876 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'6877or die_error(404,"Object does not exist");6878$type= <$fd>;6879chomp$type;6880close$fd6881or die_error(404,"Object does not exist");68826883# - hash_base and file_name6884}elsif($hash_base&&defined$file_name) {6885$file_name=~ s,/+$,,;68866887system(git_cmd(),"cat-file",'-e',$hash_base) ==06888or die_error(404,"Base object does not exist");68896890# here errors should not hapen6891open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name6892or die_error(500,"Open git-ls-tree failed");6893my$line= <$fd>;6894close$fd;68956896#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'6897unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {6898 die_error(404,"File or directory for given base does not exist");6899}6900$type=$2;6901$hash=$3;6902}else{6903 die_error(400,"Not enough information to find object");6904}69056906print$cgi->redirect(-uri => href(action=>$type, -full=>1,6907 hash=>$hash, hash_base=>$hash_base,6908 file_name=>$file_name),6909-status =>'302 Found');6910}69116912sub git_blobdiff {6913my$format=shift||'html';69146915my$fd;6916my@difftree;6917my%diffinfo;6918my$expires;69196920# preparing $fd and %diffinfo for git_patchset_body6921# new style URI6922if(defined$hash_base&&defined$hash_parent_base) {6923if(defined$file_name) {6924# read raw output6925open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6926$hash_parent_base,$hash_base,6927"--", (defined$file_parent?$file_parent: ()),$file_name6928or die_error(500,"Open git-diff-tree failed");6929@difftree=map{chomp;$_} <$fd>;6930close$fd6931or die_error(404,"Reading git-diff-tree failed");6932@difftree6933or die_error(404,"Blob diff not found");69346935}elsif(defined$hash&&6936$hash=~/[0-9a-fA-F]{40}/) {6937# try to find filename from $hash69386939# read filtered raw output6940open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6941$hash_parent_base,$hash_base,"--"6942or die_error(500,"Open git-diff-tree failed");6943@difftree=6944# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'6945# $hash == to_id6946grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}6947map{chomp;$_} <$fd>;6948close$fd6949or die_error(404,"Reading git-diff-tree failed");6950@difftree6951or die_error(404,"Blob diff not found");69526953}else{6954 die_error(400,"Missing one of the blob diff parameters");6955}69566957if(@difftree>1) {6958 die_error(400,"Ambiguous blob diff specification");6959}69606961%diffinfo= parse_difftree_raw_line($difftree[0]);6962$file_parent||=$diffinfo{'from_file'} ||$file_name;6963$file_name||=$diffinfo{'to_file'};69646965$hash_parent||=$diffinfo{'from_id'};6966$hash||=$diffinfo{'to_id'};69676968# non-textual hash id's can be cached6969if($hash_base=~m/^[0-9a-fA-F]{40}$/&&6970$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {6971$expires='+1d';6972}69736974# open patch output6975open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6976'-p', ($formateq'html'?"--full-index": ()),6977$hash_parent_base,$hash_base,6978"--", (defined$file_parent?$file_parent: ()),$file_name6979or die_error(500,"Open git-diff-tree failed");6980}69816982# old/legacy style URI -- not generated anymore since 1.4.3.6983if(!%diffinfo) {6984 die_error('404 Not Found',"Missing one of the blob diff parameters")6985}69866987# header6988if($formateq'html') {6989my$formats_nav=6990$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},6991"raw");6992 git_header_html(undef,$expires);6993if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6994 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6995 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6996}else{6997print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";6998print"<div class=\"title\">".esc_html("$hashvs$hash_parent")."</div>\n";6999}7000if(defined$file_name) {7001 git_print_page_path($file_name,"blob",$hash_base);7002}else{7003print"<div class=\"page_path\"></div>\n";7004}70057006}elsif($formateq'plain') {7007print$cgi->header(7008-type =>'text/plain',7009-charset =>'utf-8',7010-expires =>$expires,7011-content_disposition =>'inline; filename="'."$file_name".'.patch"');70127013print"X-Git-Url: ".$cgi->self_url() ."\n\n";70147015}else{7016 die_error(400,"Unknown blobdiff format");7017}70187019# patch7020if($formateq'html') {7021print"<div class=\"page_body\">\n";70227023 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);7024close$fd;70257026print"</div>\n";# class="page_body"7027 git_footer_html();70287029}else{7030while(my$line= <$fd>) {7031$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;7032$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;70337034print$line;70357036last if$line=~m!^\+\+\+!;7037}7038local$/=undef;7039print<$fd>;7040close$fd;7041}7042}70437044sub git_blobdiff_plain {7045 git_blobdiff('plain');7046}70477048sub git_commitdiff {7049my%params=@_;7050my$format=$params{-format} ||'html';70517052my($patch_max) = gitweb_get_feature('patches');7053if($formateq'patch') {7054 die_error(403,"Patch view not allowed")unless$patch_max;7055}70567057$hash||=$hash_base||"HEAD";7058my%co= parse_commit($hash)7059or die_error(404,"Unknown commit object");70607061# choose format for commitdiff for merge7062if(!defined$hash_parent&& @{$co{'parents'}} >1) {7063$hash_parent='--cc';7064}7065# we need to prepare $formats_nav before almost any parameter munging7066my$formats_nav;7067if($formateq'html') {7068$formats_nav=7069$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},7070"raw");7071if($patch_max&& @{$co{'parents'}} <=1) {7072$formats_nav.=" | ".7073$cgi->a({-href => href(action=>"patch", -replay=>1)},7074"patch");7075}70767077if(defined$hash_parent&&7078$hash_parentne'-c'&&$hash_parentne'--cc') {7079# commitdiff with two commits given7080my$hash_parent_short=$hash_parent;7081if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {7082$hash_parent_short=substr($hash_parent,0,7);7083}7084$formats_nav.=7085' (from';7086for(my$i=0;$i< @{$co{'parents'}};$i++) {7087if($co{'parents'}[$i]eq$hash_parent) {7088$formats_nav.=' parent '. ($i+1);7089last;7090}7091}7092$formats_nav.=': '.7093$cgi->a({-href => href(action=>"commitdiff",7094 hash=>$hash_parent)},7095 esc_html($hash_parent_short)) .7096')';7097}elsif(!$co{'parent'}) {7098# --root commitdiff7099$formats_nav.=' (initial)';7100}elsif(scalar@{$co{'parents'}} ==1) {7101# single parent commit7102$formats_nav.=7103' (parent: '.7104$cgi->a({-href => href(action=>"commitdiff",7105 hash=>$co{'parent'})},7106 esc_html(substr($co{'parent'},0,7))) .7107')';7108}else{7109# merge commit7110if($hash_parenteq'--cc') {7111$formats_nav.=' | '.7112$cgi->a({-href => href(action=>"commitdiff",7113 hash=>$hash, hash_parent=>'-c')},7114'combined');7115}else{# $hash_parent eq '-c'7116$formats_nav.=' | '.7117$cgi->a({-href => href(action=>"commitdiff",7118 hash=>$hash, hash_parent=>'--cc')},7119'compact');7120}7121$formats_nav.=7122' (merge: '.7123join(' ',map{7124$cgi->a({-href => href(action=>"commitdiff",7125 hash=>$_)},7126 esc_html(substr($_,0,7)));7127} @{$co{'parents'}} ) .7128')';7129}7130}71317132my$hash_parent_param=$hash_parent;7133if(!defined$hash_parent_param) {7134# --cc for multiple parents, --root for parentless7135$hash_parent_param=7136@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';7137}71387139# read commitdiff7140my$fd;7141my@difftree;7142if($formateq'html') {7143open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7144"--no-commit-id","--patch-with-raw","--full-index",7145$hash_parent_param,$hash,"--"7146or die_error(500,"Open git-diff-tree failed");71477148while(my$line= <$fd>) {7149chomp$line;7150# empty line ends raw part of diff-tree output7151last unless$line;7152push@difftree,scalar parse_difftree_raw_line($line);7153}71547155}elsif($formateq'plain') {7156open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7157'-p',$hash_parent_param,$hash,"--"7158or die_error(500,"Open git-diff-tree failed");7159}elsif($formateq'patch') {7160# For commit ranges, we limit the output to the number of7161# patches specified in the 'patches' feature.7162# For single commits, we limit the output to a single patch,7163# diverging from the git-format-patch default.7164my@commit_spec= ();7165if($hash_parent) {7166if($patch_max>0) {7167push@commit_spec,"-$patch_max";7168}7169push@commit_spec,'-n',"$hash_parent..$hash";7170}else{7171if($params{-single}) {7172push@commit_spec,'-1';7173}else{7174if($patch_max>0) {7175push@commit_spec,"-$patch_max";7176}7177push@commit_spec,"-n";7178}7179push@commit_spec,'--root',$hash;7180}7181open$fd,"-|", git_cmd(),"format-patch",@diff_opts,7182'--encoding=utf8','--stdout',@commit_spec7183or die_error(500,"Open git-format-patch failed");7184}else{7185 die_error(400,"Unknown commitdiff format");7186}71877188# non-textual hash id's can be cached7189my$expires;7190if($hash=~m/^[0-9a-fA-F]{40}$/) {7191$expires="+1d";7192}71937194# write commit message7195if($formateq'html') {7196my$refs= git_get_references();7197my$ref= format_ref_marker($refs,$co{'id'});71987199 git_header_html(undef,$expires);7200 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);7201 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);7202print"<div class=\"title_text\">\n".7203"<table class=\"object_header\">\n";7204 git_print_authorship_rows(\%co);7205print"</table>".7206"</div>\n";7207print"<div class=\"page_body\">\n";7208if(@{$co{'comment'}} >1) {7209print"<div class=\"log\">\n";7210 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);7211print"</div>\n";# class="log"7212}72137214}elsif($formateq'plain') {7215my$refs= git_get_references("tags");7216my$tagname= git_get_rev_name_tags($hash);7217my$filename= basename($project) ."-$hash.patch";72187219print$cgi->header(7220-type =>'text/plain',7221-charset =>'utf-8',7222-expires =>$expires,7223-content_disposition =>'inline; filename="'."$filename".'"');7224my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});7225print"From: ". to_utf8($co{'author'}) ."\n";7226print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";7227print"Subject: ". to_utf8($co{'title'}) ."\n";72287229print"X-Git-Tag:$tagname\n"if$tagname;7230print"X-Git-Url: ".$cgi->self_url() ."\n\n";72317232foreachmy$line(@{$co{'comment'}}) {7233print to_utf8($line) ."\n";7234}7235print"---\n\n";7236}elsif($formateq'patch') {7237my$filename= basename($project) ."-$hash.patch";72387239print$cgi->header(7240-type =>'text/plain',7241-charset =>'utf-8',7242-expires =>$expires,7243-content_disposition =>'inline; filename="'."$filename".'"');7244}72457246# write patch7247if($formateq'html') {7248my$use_parents= !defined$hash_parent||7249$hash_parenteq'-c'||$hash_parenteq'--cc';7250 git_difftree_body(\@difftree,$hash,7251$use_parents? @{$co{'parents'}} :$hash_parent);7252print"<br/>\n";72537254 git_patchset_body($fd, \@difftree,$hash,7255$use_parents? @{$co{'parents'}} :$hash_parent);7256close$fd;7257print"</div>\n";# class="page_body"7258 git_footer_html();72597260}elsif($formateq'plain') {7261local$/=undef;7262print<$fd>;7263close$fd7264or print"Reading git-diff-tree failed\n";7265}elsif($formateq'patch') {7266local$/=undef;7267print<$fd>;7268close$fd7269or print"Reading git-format-patch failed\n";7270}7271}72727273sub git_commitdiff_plain {7274 git_commitdiff(-format =>'plain');7275}72767277# format-patch-style patches7278sub git_patch {7279 git_commitdiff(-format =>'patch', -single =>1);7280}72817282sub git_patches {7283 git_commitdiff(-format =>'patch');7284}72857286sub git_history {7287 git_log_generic('history', \&git_history_body,7288$hash_base,$hash_parent_base,7289$file_name,$hash);7290}72917292sub git_search {7293$searchtype||='commit';72947295# check if appropriate features are enabled7296 gitweb_check_feature('search')7297or die_error(403,"Search is disabled");7298if($searchtypeeq'pickaxe') {7299# pickaxe may take all resources of your box and run for several minutes7300# with every query - so decide by yourself how public you make this feature7301 gitweb_check_feature('pickaxe')7302or die_error(403,"Pickaxe search is disabled");7303}7304if($searchtypeeq'grep') {7305# grep search might be potentially CPU-intensive, too7306 gitweb_check_feature('grep')7307or die_error(403,"Grep search is disabled");7308}73097310if(!defined$searchtext) {7311 die_error(400,"Text field is empty");7312}7313if(!defined$hash) {7314$hash= git_get_head_hash($project);7315}7316my%co= parse_commit($hash);7317if(!%co) {7318 die_error(404,"Unknown commit object");7319}7320if(!defined$page) {7321$page=0;7322}73237324if($searchtypeeq'commit'||7325$searchtypeeq'author'||7326$searchtypeeq'committer') {7327 git_search_message(%co);7328}elsif($searchtypeeq'pickaxe') {7329 git_search_changes(%co);7330}elsif($searchtypeeq'grep') {7331 git_search_files(%co);7332}else{7333 die_error(400,"Unknown search type");7334}7335}73367337sub git_search_help {7338 git_header_html();7339 git_print_page_nav('','',$hash,$hash,$hash);7340print<<EOT;7341<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without7342regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,7343the pattern entered is recognized as the POSIX extended7344<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case7345insensitive).</p>7346<dl>7347<dt><b>commit</b></dt>7348<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>7349EOT7350my$have_grep= gitweb_check_feature('grep');7351if($have_grep) {7352print<<EOT;7353<dt><b>grep</b></dt>7354<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing7355 a different one) are searched for the given pattern. On large trees, this search can take7356a while and put some strain on the server, so please use it with some consideration. Note that7357due to git-grep peculiarity, currently if regexp mode is turned off, the matches are7358case-sensitive.</dd>7359EOT7360}7361print<<EOT;7362<dt><b>author</b></dt>7363<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>7364<dt><b>committer</b></dt>7365<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>7366EOT7367my$have_pickaxe= gitweb_check_feature('pickaxe');7368if($have_pickaxe) {7369print<<EOT;7370<dt><b>pickaxe</b></dt>7371<dd>All commits that caused the string to appear or disappear from any file (changes that7372added, removed or "modified" the string) will be listed. This search can take a while and7373takes a lot of strain on the server, so please use it wisely. Note that since you may be7374interested even in changes just changing the case as well, this search is case sensitive.</dd>7375EOT7376}7377print"</dl>\n";7378 git_footer_html();7379}73807381sub git_shortlog {7382 git_log_generic('shortlog', \&git_shortlog_body,7383$hash,$hash_parent);7384}73857386## ......................................................................7387## feeds (RSS, Atom; OPML)73887389sub git_feed {7390my$format=shift||'atom';7391my$have_blame= gitweb_check_feature('blame');73927393# Atom: http://www.atomenabled.org/developers/syndication/7394# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ7395if($formatne'rss'&&$formatne'atom') {7396 die_error(400,"Unknown web feed format");7397}73987399# log/feed of current (HEAD) branch, log of given branch, history of file/directory7400my$head=$hash||'HEAD';7401my@commitlist= parse_commits($head,150,0,$file_name);74027403my%latest_commit;7404my%latest_date;7405my$content_type="application/$format+xml";7406if(defined$cgi->http('HTTP_ACCEPT') &&7407$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {7408# browser (feed reader) prefers text/xml7409$content_type='text/xml';7410}7411if(defined($commitlist[0])) {7412%latest_commit= %{$commitlist[0]};7413my$latest_epoch=$latest_commit{'committer_epoch'};7414%latest_date= parse_date($latest_epoch,$latest_commit{'comitter_tz'});7415my$if_modified=$cgi->http('IF_MODIFIED_SINCE');7416if(defined$if_modified) {7417my$since;7418if(eval{require HTTP::Date;1; }) {7419$since= HTTP::Date::str2time($if_modified);7420}elsif(eval{require Time::ParseDate;1; }) {7421$since= Time::ParseDate::parsedate($if_modified, GMT =>1);7422}7423if(defined$since&&$latest_epoch<=$since) {7424print$cgi->header(7425-type =>$content_type,7426-charset =>'utf-8',7427-last_modified =>$latest_date{'rfc2822'},7428-status =>'304 Not Modified');7429return;7430}7431}7432print$cgi->header(7433-type =>$content_type,7434-charset =>'utf-8',7435-last_modified =>$latest_date{'rfc2822'});7436}else{7437print$cgi->header(7438-type =>$content_type,7439-charset =>'utf-8');7440}74417442# Optimization: skip generating the body if client asks only7443# for Last-Modified date.7444return if($cgi->request_method()eq'HEAD');74457446# header variables7447my$title="$site_name-$project/$action";7448my$feed_type='log';7449if(defined$hash) {7450$title.=" - '$hash'";7451$feed_type='branch log';7452if(defined$file_name) {7453$title.=" ::$file_name";7454$feed_type='history';7455}7456}elsif(defined$file_name) {7457$title.=" -$file_name";7458$feed_type='history';7459}7460$title.="$feed_type";7461my$descr= git_get_project_description($project);7462if(defined$descr) {7463$descr= esc_html($descr);7464}else{7465$descr="$project".7466($formateq'rss'?'RSS':'Atom') .7467" feed";7468}7469my$owner= git_get_project_owner($project);7470$owner= esc_html($owner);74717472#header7473my$alt_url;7474if(defined$file_name) {7475$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);7476}elsif(defined$hash) {7477$alt_url= href(-full=>1, action=>"log", hash=>$hash);7478}else{7479$alt_url= href(-full=>1, action=>"summary");7480}7481print qq!<?xml version="1.0" encoding="utf-8"?>\n!;7482if($formateq'rss') {7483print<<XML;7484<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">7485<channel>7486XML7487print"<title>$title</title>\n".7488"<link>$alt_url</link>\n".7489"<description>$descr</description>\n".7490"<language>en</language>\n".7491# project owner is responsible for 'editorial' content7492"<managingEditor>$owner</managingEditor>\n";7493if(defined$logo||defined$favicon) {7494# prefer the logo to the favicon, since RSS7495# doesn't allow both7496my$img= esc_url($logo||$favicon);7497print"<image>\n".7498"<url>$img</url>\n".7499"<title>$title</title>\n".7500"<link>$alt_url</link>\n".7501"</image>\n";7502}7503if(%latest_date) {7504print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";7505print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";7506}7507print"<generator>gitweb v.$version/$git_version</generator>\n";7508}elsif($formateq'atom') {7509print<<XML;7510<feed xmlns="http://www.w3.org/2005/Atom">7511XML7512print"<title>$title</title>\n".7513"<subtitle>$descr</subtitle>\n".7514'<link rel="alternate" type="text/html" href="'.7515$alt_url.'" />'."\n".7516'<link rel="self" type="'.$content_type.'" href="'.7517$cgi->self_url() .'" />'."\n".7518"<id>". href(-full=>1) ."</id>\n".7519# use project owner for feed author7520"<author><name>$owner</name></author>\n";7521if(defined$favicon) {7522print"<icon>". esc_url($favicon) ."</icon>\n";7523}7524if(defined$logo) {7525# not twice as wide as tall: 72 x 27 pixels7526print"<logo>". esc_url($logo) ."</logo>\n";7527}7528if(!%latest_date) {7529# dummy date to keep the feed valid until commits trickle in:7530print"<updated>1970-01-01T00:00:00Z</updated>\n";7531}else{7532print"<updated>$latest_date{'iso-8601'}</updated>\n";7533}7534print"<generator version='$version/$git_version'>gitweb</generator>\n";7535}75367537# contents7538for(my$i=0;$i<=$#commitlist;$i++) {7539my%co= %{$commitlist[$i]};7540my$commit=$co{'id'};7541# we read 150, we always show 30 and the ones more recent than 48 hours7542if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {7543last;7544}7545my%cd= parse_date($co{'author_epoch'},$co{'author_tz'});75467547# get list of changed files7548open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7549$co{'parent'} ||"--root",7550$co{'id'},"--", (defined$file_name?$file_name: ())7551ornext;7552my@difftree=map{chomp;$_} <$fd>;7553close$fd7554ornext;75557556# print element (entry, item)7557my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);7558if($formateq'rss') {7559print"<item>\n".7560"<title>". esc_html($co{'title'}) ."</title>\n".7561"<author>". esc_html($co{'author'}) ."</author>\n".7562"<pubDate>$cd{'rfc2822'}</pubDate>\n".7563"<guid isPermaLink=\"true\">$co_url</guid>\n".7564"<link>$co_url</link>\n".7565"<description>". esc_html($co{'title'}) ."</description>\n".7566"<content:encoded>".7567"<![CDATA[\n";7568}elsif($formateq'atom') {7569print"<entry>\n".7570"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".7571"<updated>$cd{'iso-8601'}</updated>\n".7572"<author>\n".7573" <name>". esc_html($co{'author_name'}) ."</name>\n";7574if($co{'author_email'}) {7575print" <email>". esc_html($co{'author_email'}) ."</email>\n";7576}7577print"</author>\n".7578# use committer for contributor7579"<contributor>\n".7580" <name>". esc_html($co{'committer_name'}) ."</name>\n";7581if($co{'committer_email'}) {7582print" <email>". esc_html($co{'committer_email'}) ."</email>\n";7583}7584print"</contributor>\n".7585"<published>$cd{'iso-8601'}</published>\n".7586"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".7587"<id>$co_url</id>\n".7588"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".7589"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";7590}7591my$comment=$co{'comment'};7592print"<pre>\n";7593foreachmy$line(@$comment) {7594$line= esc_html($line);7595print"$line\n";7596}7597print"</pre><ul>\n";7598foreachmy$difftree_line(@difftree) {7599my%difftree= parse_difftree_raw_line($difftree_line);7600next if!$difftree{'from_id'};76017602my$file=$difftree{'file'} ||$difftree{'to_file'};76037604print"<li>".7605"[".7606$cgi->a({-href => href(-full=>1, action=>"blobdiff",7607 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},7608 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},7609 file_name=>$file, file_parent=>$difftree{'from_file'}),7610-title =>"diff"},'D');7611if($have_blame) {7612print$cgi->a({-href => href(-full=>1, action=>"blame",7613 file_name=>$file, hash_base=>$commit),7614-title =>"blame"},'B');7615}7616# if this is not a feed of a file history7617if(!defined$file_name||$file_namene$file) {7618print$cgi->a({-href => href(-full=>1, action=>"history",7619 file_name=>$file, hash=>$commit),7620-title =>"history"},'H');7621}7622$file= esc_path($file);7623print"] ".7624"$file</li>\n";7625}7626if($formateq'rss') {7627print"</ul>]]>\n".7628"</content:encoded>\n".7629"</item>\n";7630}elsif($formateq'atom') {7631print"</ul>\n</div>\n".7632"</content>\n".7633"</entry>\n";7634}7635}76367637# end of feed7638if($formateq'rss') {7639print"</channel>\n</rss>\n";7640}elsif($formateq'atom') {7641print"</feed>\n";7642}7643}76447645sub git_rss {7646 git_feed('rss');7647}76487649sub git_atom {7650 git_feed('atom');7651}76527653sub git_opml {7654my@list= git_get_projects_list();7655if(!@list) {7656 die_error(404,"No projects found");7657}76587659print$cgi->header(7660-type =>'text/xml',7661-charset =>'utf-8',7662-content_disposition =>'inline; filename="opml.xml"');76637664print<<XML;7665<?xml version="1.0" encoding="utf-8"?>7666<opml version="1.0">7667<head>7668 <title>$site_nameOPML Export</title>7669</head>7670<body>7671<outline text="git RSS feeds">7672XML76737674foreachmy$pr(@list) {7675my%proj=%$pr;7676my$head= git_get_head_hash($proj{'path'});7677if(!defined$head) {7678next;7679}7680$git_dir="$projectroot/$proj{'path'}";7681my%co= parse_commit($head);7682if(!%co) {7683next;7684}76857686my$path= esc_html(chop_str($proj{'path'},25,5));7687my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);7688my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);7689print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";7690}7691print<<XML;7692</outline>7693</body>7694</opml>7695XML7696}