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= decode_utf8($ENV{"PATH_INFO"}); 56if($path_info) { 57# $path_info has already been URL-decoded by the web server, but 58# $my_url and $my_uri have not. URL-decode them so we can properly 59# strip $path_info. 60$my_url= unescape($my_url); 61$my_uri= unescape($my_uri); 62if($my_url=~ s,\Q$path_info\E$,, && 63$my_uri=~ s,\Q$path_info\E$,, && 64defined$ENV{'SCRIPT_NAME'}) { 65$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 66} 67} 68 69# target of the home link on top of all pages 70our$home_link=$my_uri||"/"; 71} 72 73# core git executable to use 74# this can just be "git" if your webserver has a sensible PATH 75our$GIT="++GIT_BINDIR++/git"; 76 77# absolute fs-path which will be prepended to the project path 78#our $projectroot = "/pub/scm"; 79our$projectroot="++GITWEB_PROJECTROOT++"; 80 81# fs traversing limit for getting project list 82# the number is relative to the projectroot 83our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 84 85# string of the home link on top of all pages 86our$home_link_str="++GITWEB_HOME_LINK_STR++"; 87 88# extra breadcrumbs preceding the home link 89our@extra_breadcrumbs= (); 90 91# name of your site or organization to appear in page titles 92# replace this with something more descriptive for clearer bookmarks 93our$site_name="++GITWEB_SITENAME++" 94|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 95 96# html snippet to include in the <head> section of each page 97our$site_html_head_string="++GITWEB_SITE_HTML_HEAD_STRING++"; 98# filename of html text to include at top of each page 99our$site_header="++GITWEB_SITE_HEADER++"; 100# html text to include at home page 101our$home_text="++GITWEB_HOMETEXT++"; 102# filename of html text to include at bottom of each page 103our$site_footer="++GITWEB_SITE_FOOTER++"; 104 105# URI of stylesheets 106our@stylesheets= ("++GITWEB_CSS++"); 107# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 108our$stylesheet=undef; 109# URI of GIT logo (72x27 size) 110our$logo="++GITWEB_LOGO++"; 111# URI of GIT favicon, assumed to be image/png type 112our$favicon="++GITWEB_FAVICON++"; 113# URI of gitweb.js (JavaScript code for gitweb) 114our$javascript="++GITWEB_JS++"; 115 116# URI and label (title) of GIT logo link 117#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 118#our $logo_label = "git documentation"; 119our$logo_url="http://git-scm.com/"; 120our$logo_label="git homepage"; 121 122# source of projects list 123our$projects_list="++GITWEB_LIST++"; 124 125# the width (in characters) of the projects list "Description" column 126our$projects_list_description_width=25; 127 128# group projects by category on the projects list 129# (enabled if this variable evaluates to true) 130our$projects_list_group_categories=0; 131 132# default category if none specified 133# (leave the empty string for no category) 134our$project_list_default_category=""; 135 136# default order of projects list 137# valid values are none, project, descr, owner, and age 138our$default_projects_order="project"; 139 140# show repository only if this file exists 141# (only effective if this variable evaluates to true) 142our$export_ok="++GITWEB_EXPORT_OK++"; 143 144# don't generate age column on the projects list page 145our$omit_age_column=0; 146 147# don't generate information about owners of repositories 148our$omit_owner=0; 149 150# show repository only if this subroutine returns true 151# when given the path to the project, for example: 152# sub { return -e "$_[0]/git-daemon-export-ok"; } 153our$export_auth_hook=undef; 154 155# only allow viewing of repositories also shown on the overview page 156our$strict_export="++GITWEB_STRICT_EXPORT++"; 157 158# list of git base URLs used for URL to where fetch project from, 159# i.e. full URL is "$git_base_url/$project" 160our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 161 162# default blob_plain mimetype and default charset for text/plain blob 163our$default_blob_plain_mimetype='text/plain'; 164our$default_text_plain_charset=undef; 165 166# file to use for guessing MIME types before trying /etc/mime.types 167# (relative to the current git repository) 168our$mimetypes_file=undef; 169 170# assume this charset if line contains non-UTF-8 characters; 171# it should be valid encoding (see Encoding::Supported(3pm) for list), 172# for which encoding all byte sequences are valid, for example 173# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 174# could be even 'utf-8' for the old behavior) 175our$fallback_encoding='latin1'; 176 177# rename detection options for git-diff and git-diff-tree 178# - default is '-M', with the cost proportional to 179# (number of removed files) * (number of new files). 180# - more costly is '-C' (which implies '-M'), with the cost proportional to 181# (number of changed files + number of removed files) * (number of new files) 182# - even more costly is '-C', '--find-copies-harder' with cost 183# (number of files in the original tree) * (number of new files) 184# - one might want to include '-B' option, e.g. '-B', '-M' 185our@diff_opts= ('-M');# taken from git_commit 186 187# Disables features that would allow repository owners to inject script into 188# the gitweb domain. 189our$prevent_xss=0; 190 191# Path to the highlight executable to use (must be the one from 192# http://www.andre-simon.de due to assumptions about parameters and output). 193# Useful if highlight is not installed on your webserver's PATH. 194# [Default: highlight] 195our$highlight_bin="++HIGHLIGHT_BIN++"; 196 197# information about snapshot formats that gitweb is capable of serving 198our%known_snapshot_formats= ( 199# name => { 200# 'display' => display name, 201# 'type' => mime type, 202# 'suffix' => filename suffix, 203# 'format' => --format for git-archive, 204# 'compressor' => [compressor command and arguments] 205# (array reference, optional) 206# 'disabled' => boolean (optional)} 207# 208'tgz'=> { 209'display'=>'tar.gz', 210'type'=>'application/x-gzip', 211'suffix'=>'.tar.gz', 212'format'=>'tar', 213'compressor'=> ['gzip','-n']}, 214 215'tbz2'=> { 216'display'=>'tar.bz2', 217'type'=>'application/x-bzip2', 218'suffix'=>'.tar.bz2', 219'format'=>'tar', 220'compressor'=> ['bzip2']}, 221 222'txz'=> { 223'display'=>'tar.xz', 224'type'=>'application/x-xz', 225'suffix'=>'.tar.xz', 226'format'=>'tar', 227'compressor'=> ['xz'], 228'disabled'=>1}, 229 230'zip'=> { 231'display'=>'zip', 232'type'=>'application/x-zip', 233'suffix'=>'.zip', 234'format'=>'zip'}, 235); 236 237# Aliases so we understand old gitweb.snapshot values in repository 238# configuration. 239our%known_snapshot_format_aliases= ( 240'gzip'=>'tgz', 241'bzip2'=>'tbz2', 242'xz'=>'txz', 243 244# backward compatibility: legacy gitweb config support 245'x-gzip'=>undef,'gz'=>undef, 246'x-bzip2'=>undef,'bz2'=>undef, 247'x-zip'=>undef,''=>undef, 248); 249 250# Pixel sizes for icons and avatars. If the default font sizes or lineheights 251# are changed, it may be appropriate to change these values too via 252# $GITWEB_CONFIG. 253our%avatar_size= ( 254'default'=>16, 255'double'=>32 256); 257 258# Used to set the maximum load that we will still respond to gitweb queries. 259# If server load exceed this value then return "503 server busy" error. 260# If gitweb cannot determined server load, it is taken to be 0. 261# Leave it undefined (or set to 'undef') to turn off load checking. 262our$maxload=300; 263 264# configuration for 'highlight' (http://www.andre-simon.de/) 265# match by basename 266our%highlight_basename= ( 267#'Program' => 'py', 268#'Library' => 'py', 269'SConstruct'=>'py',# SCons equivalent of Makefile 270'Makefile'=>'make', 271); 272# match by extension 273our%highlight_ext= ( 274# main extensions, defining name of syntax; 275# see files in /usr/share/highlight/langDefs/ directory 276(map{$_=>$_}qw(py rb java css js tex bib xml awk bat ini spec tcl sql)), 277# alternate extensions, see /etc/highlight/filetypes.conf 278(map{$_=>'c'}qw(c h)), 279(map{$_=>'sh'}qw(sh bash zsh ksh)), 280(map{$_=>'cpp'}qw(cpp cxx c++ cc)), 281(map{$_=>'php'}qw(php php3 php4 php5 phps)), 282(map{$_=>'pl'}qw(pl perl pm)),# perhaps also 'cgi' 283(map{$_=>'make'}qw(make mak mk)), 284(map{$_=>'xml'}qw(xml xhtml html htm)), 285); 286 287# You define site-wide feature defaults here; override them with 288# $GITWEB_CONFIG as necessary. 289our%feature= ( 290# feature => { 291# 'sub' => feature-sub (subroutine), 292# 'override' => allow-override (boolean), 293# 'default' => [ default options...] (array reference)} 294# 295# if feature is overridable (it means that allow-override has true value), 296# then feature-sub will be called with default options as parameters; 297# return value of feature-sub indicates if to enable specified feature 298# 299# if there is no 'sub' key (no feature-sub), then feature cannot be 300# overridden 301# 302# use gitweb_get_feature(<feature>) to retrieve the <feature> value 303# (an array) or gitweb_check_feature(<feature>) to check if <feature> 304# is enabled 305 306# Enable the 'blame' blob view, showing the last commit that modified 307# each line in the file. This can be very CPU-intensive. 308 309# To enable system wide have in $GITWEB_CONFIG 310# $feature{'blame'}{'default'} = [1]; 311# To have project specific config enable override in $GITWEB_CONFIG 312# $feature{'blame'}{'override'} = 1; 313# and in project config gitweb.blame = 0|1; 314'blame'=> { 315'sub'=>sub{ feature_bool('blame',@_) }, 316'override'=>0, 317'default'=> [0]}, 318 319# Enable the 'snapshot' link, providing a compressed archive of any 320# tree. This can potentially generate high traffic if you have large 321# project. 322 323# Value is a list of formats defined in %known_snapshot_formats that 324# you wish to offer. 325# To disable system wide have in $GITWEB_CONFIG 326# $feature{'snapshot'}{'default'} = []; 327# To have project specific config enable override in $GITWEB_CONFIG 328# $feature{'snapshot'}{'override'} = 1; 329# and in project config, a comma-separated list of formats or "none" 330# to disable. Example: gitweb.snapshot = tbz2,zip; 331'snapshot'=> { 332'sub'=> \&feature_snapshot, 333'override'=>0, 334'default'=> ['tgz']}, 335 336# Enable text search, which will list the commits which match author, 337# committer or commit text to a given string. Enabled by default. 338# Project specific override is not supported. 339# 340# Note that this controls all search features, which means that if 341# it is disabled, then 'grep' and 'pickaxe' search would also be 342# disabled. 343'search'=> { 344'override'=>0, 345'default'=> [1]}, 346 347# Enable grep search, which will list the files in currently selected 348# tree containing the given string. Enabled by default. This can be 349# potentially CPU-intensive, of course. 350# Note that you need to have 'search' feature enabled too. 351 352# To enable system wide have in $GITWEB_CONFIG 353# $feature{'grep'}{'default'} = [1]; 354# To have project specific config enable override in $GITWEB_CONFIG 355# $feature{'grep'}{'override'} = 1; 356# and in project config gitweb.grep = 0|1; 357'grep'=> { 358'sub'=>sub{ feature_bool('grep',@_) }, 359'override'=>0, 360'default'=> [1]}, 361 362# Enable the pickaxe search, which will list the commits that modified 363# a given string in a file. This can be practical and quite faster 364# alternative to 'blame', but still potentially CPU-intensive. 365# Note that you need to have 'search' feature enabled too. 366 367# To enable system wide have in $GITWEB_CONFIG 368# $feature{'pickaxe'}{'default'} = [1]; 369# To have project specific config enable override in $GITWEB_CONFIG 370# $feature{'pickaxe'}{'override'} = 1; 371# and in project config gitweb.pickaxe = 0|1; 372'pickaxe'=> { 373'sub'=>sub{ feature_bool('pickaxe',@_) }, 374'override'=>0, 375'default'=> [1]}, 376 377# Enable showing size of blobs in a 'tree' view, in a separate 378# column, similar to what 'ls -l' does. This cost a bit of IO. 379 380# To disable system wide have in $GITWEB_CONFIG 381# $feature{'show-sizes'}{'default'} = [0]; 382# To have project specific config enable override in $GITWEB_CONFIG 383# $feature{'show-sizes'}{'override'} = 1; 384# and in project config gitweb.showsizes = 0|1; 385'show-sizes'=> { 386'sub'=>sub{ feature_bool('showsizes',@_) }, 387'override'=>0, 388'default'=> [1]}, 389 390# Make gitweb use an alternative format of the URLs which can be 391# more readable and natural-looking: project name is embedded 392# directly in the path and the query string contains other 393# auxiliary information. All gitweb installations recognize 394# URL in either format; this configures in which formats gitweb 395# generates links. 396 397# To enable system wide have in $GITWEB_CONFIG 398# $feature{'pathinfo'}{'default'} = [1]; 399# Project specific override is not supported. 400 401# Note that you will need to change the default location of CSS, 402# favicon, logo and possibly other files to an absolute URL. Also, 403# if gitweb.cgi serves as your indexfile, you will need to force 404# $my_uri to contain the script name in your $GITWEB_CONFIG. 405'pathinfo'=> { 406'override'=>0, 407'default'=> [0]}, 408 409# Make gitweb consider projects in project root subdirectories 410# to be forks of existing projects. Given project $projname.git, 411# projects matching $projname/*.git will not be shown in the main 412# projects list, instead a '+' mark will be added to $projname 413# there and a 'forks' view will be enabled for the project, listing 414# all the forks. If project list is taken from a file, forks have 415# to be listed after the main project. 416 417# To enable system wide have in $GITWEB_CONFIG 418# $feature{'forks'}{'default'} = [1]; 419# Project specific override is not supported. 420'forks'=> { 421'override'=>0, 422'default'=> [0]}, 423 424# Insert custom links to the action bar of all project pages. 425# This enables you mainly to link to third-party scripts integrating 426# into gitweb; e.g. git-browser for graphical history representation 427# or custom web-based repository administration interface. 428 429# The 'default' value consists of a list of triplets in the form 430# (label, link, position) where position is the label after which 431# to insert the link and link is a format string where %n expands 432# to the project name, %f to the project path within the filesystem, 433# %h to the current hash (h gitweb parameter) and %b to the current 434# hash base (hb gitweb parameter); %% expands to %. 435 436# To enable system wide have in $GITWEB_CONFIG e.g. 437# $feature{'actions'}{'default'} = [('graphiclog', 438# '/git-browser/by-commit.html?r=%n', 'summary')]; 439# Project specific override is not supported. 440'actions'=> { 441'override'=>0, 442'default'=> []}, 443 444# Allow gitweb scan project content tags of project repository, 445# and display the popular Web 2.0-ish "tag cloud" near the projects 446# list. Note that this is something COMPLETELY different from the 447# normal Git tags. 448 449# gitweb by itself can show existing tags, but it does not handle 450# tagging itself; you need to do it externally, outside gitweb. 451# The format is described in git_get_project_ctags() subroutine. 452# You may want to install the HTML::TagCloud Perl module to get 453# a pretty tag cloud instead of just a list of tags. 454 455# To enable system wide have in $GITWEB_CONFIG 456# $feature{'ctags'}{'default'} = [1]; 457# Project specific override is not supported. 458 459# In the future whether ctags editing is enabled might depend 460# on the value, but using 1 should always mean no editing of ctags. 461'ctags'=> { 462'override'=>0, 463'default'=> [0]}, 464 465# The maximum number of patches in a patchset generated in patch 466# view. Set this to 0 or undef to disable patch view, or to a 467# negative number to remove any limit. 468 469# To disable system wide have in $GITWEB_CONFIG 470# $feature{'patches'}{'default'} = [0]; 471# To have project specific config enable override in $GITWEB_CONFIG 472# $feature{'patches'}{'override'} = 1; 473# and in project config gitweb.patches = 0|n; 474# where n is the maximum number of patches allowed in a patchset. 475'patches'=> { 476'sub'=> \&feature_patches, 477'override'=>0, 478'default'=> [16]}, 479 480# Avatar support. When this feature is enabled, views such as 481# shortlog or commit will display an avatar associated with 482# the email of the committer(s) and/or author(s). 483 484# Currently available providers are gravatar and picon. 485# If an unknown provider is specified, the feature is disabled. 486 487# Gravatar depends on Digest::MD5. 488# Picon currently relies on the indiana.edu database. 489 490# To enable system wide have in $GITWEB_CONFIG 491# $feature{'avatar'}{'default'} = ['<provider>']; 492# where <provider> is either gravatar or picon. 493# To have project specific config enable override in $GITWEB_CONFIG 494# $feature{'avatar'}{'override'} = 1; 495# and in project config gitweb.avatar = <provider>; 496'avatar'=> { 497'sub'=> \&feature_avatar, 498'override'=>0, 499'default'=> ['']}, 500 501# Enable displaying how much time and how many git commands 502# it took to generate and display page. Disabled by default. 503# Project specific override is not supported. 504'timed'=> { 505'override'=>0, 506'default'=> [0]}, 507 508# Enable turning some links into links to actions which require 509# JavaScript to run (like 'blame_incremental'). Not enabled by 510# default. Project specific override is currently not supported. 511'javascript-actions'=> { 512'override'=>0, 513'default'=> [0]}, 514 515# Enable and configure ability to change common timezone for dates 516# in gitweb output via JavaScript. Enabled by default. 517# Project specific override is not supported. 518'javascript-timezone'=> { 519'override'=>0, 520'default'=> [ 521'local',# default timezone: 'utc', 'local', or '(-|+)HHMM' format, 522# or undef to turn off this feature 523'gitweb_tz',# name of cookie where to store selected timezone 524'datetime',# CSS class used to mark up dates for manipulation 525]}, 526 527# Syntax highlighting support. This is based on Daniel Svensson's 528# and Sham Chukoury's work in gitweb-xmms2.git. 529# It requires the 'highlight' program present in $PATH, 530# and therefore is disabled by default. 531 532# To enable system wide have in $GITWEB_CONFIG 533# $feature{'highlight'}{'default'} = [1]; 534 535'highlight'=> { 536'sub'=>sub{ feature_bool('highlight',@_) }, 537'override'=>0, 538'default'=> [0]}, 539 540# Enable displaying of remote heads in the heads list 541 542# To enable system wide have in $GITWEB_CONFIG 543# $feature{'remote_heads'}{'default'} = [1]; 544# To have project specific config enable override in $GITWEB_CONFIG 545# $feature{'remote_heads'}{'override'} = 1; 546# and in project config gitweb.remoteheads = 0|1; 547'remote_heads'=> { 548'sub'=>sub{ feature_bool('remote_heads',@_) }, 549'override'=>0, 550'default'=> [0]}, 551); 552 553sub gitweb_get_feature { 554my($name) =@_; 555return unlessexists$feature{$name}; 556my($sub,$override,@defaults) = ( 557$feature{$name}{'sub'}, 558$feature{$name}{'override'}, 559@{$feature{$name}{'default'}}); 560# project specific override is possible only if we have project 561our$git_dir;# global variable, declared later 562if(!$override|| !defined$git_dir) { 563return@defaults; 564} 565if(!defined$sub) { 566warn"feature$nameis not overridable"; 567return@defaults; 568} 569return$sub->(@defaults); 570} 571 572# A wrapper to check if a given feature is enabled. 573# With this, you can say 574# 575# my $bool_feat = gitweb_check_feature('bool_feat'); 576# gitweb_check_feature('bool_feat') or somecode; 577# 578# instead of 579# 580# my ($bool_feat) = gitweb_get_feature('bool_feat'); 581# (gitweb_get_feature('bool_feat'))[0] or somecode; 582# 583sub gitweb_check_feature { 584return(gitweb_get_feature(@_))[0]; 585} 586 587 588sub feature_bool { 589my$key=shift; 590my($val) = git_get_project_config($key,'--bool'); 591 592if(!defined$val) { 593return($_[0]); 594}elsif($valeq'true') { 595return(1); 596}elsif($valeq'false') { 597return(0); 598} 599} 600 601sub feature_snapshot { 602my(@fmts) =@_; 603 604my($val) = git_get_project_config('snapshot'); 605 606if($val) { 607@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 608} 609 610return@fmts; 611} 612 613sub feature_patches { 614my@val= (git_get_project_config('patches','--int')); 615 616if(@val) { 617return@val; 618} 619 620return($_[0]); 621} 622 623sub feature_avatar { 624my@val= (git_get_project_config('avatar')); 625 626return@val?@val:@_; 627} 628 629# checking HEAD file with -e is fragile if the repository was 630# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 631# and then pruned. 632sub check_head_link { 633my($dir) =@_; 634my$headfile="$dir/HEAD"; 635return((-e $headfile) || 636(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 637} 638 639sub check_export_ok { 640my($dir) =@_; 641return(check_head_link($dir) && 642(!$export_ok|| -e "$dir/$export_ok") && 643(!$export_auth_hook||$export_auth_hook->($dir))); 644} 645 646# process alternate names for backward compatibility 647# filter out unsupported (unknown) snapshot formats 648sub filter_snapshot_fmts { 649my@fmts=@_; 650 651@fmts=map{ 652exists$known_snapshot_format_aliases{$_} ? 653$known_snapshot_format_aliases{$_} :$_}@fmts; 654@fmts=grep{ 655exists$known_snapshot_formats{$_} && 656!$known_snapshot_formats{$_}{'disabled'}}@fmts; 657} 658 659# If it is set to code reference, it is code that it is to be run once per 660# request, allowing updating configurations that change with each request, 661# while running other code in config file only once. 662# 663# Otherwise, if it is false then gitweb would process config file only once; 664# if it is true then gitweb config would be run for each request. 665our$per_request_config=1; 666 667# read and parse gitweb config file given by its parameter. 668# returns true on success, false on recoverable error, allowing 669# to chain this subroutine, using first file that exists. 670# dies on errors during parsing config file, as it is unrecoverable. 671sub read_config_file { 672my$filename=shift; 673return unlessdefined$filename; 674# die if there are errors parsing config file 675if(-e $filename) { 676do$filename; 677die$@if$@; 678return1; 679} 680return; 681} 682 683our($GITWEB_CONFIG,$GITWEB_CONFIG_SYSTEM,$GITWEB_CONFIG_COMMON); 684sub evaluate_gitweb_config { 685our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 686our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 687our$GITWEB_CONFIG_COMMON=$ENV{'GITWEB_CONFIG_COMMON'} ||"++GITWEB_CONFIG_COMMON++"; 688 689# Protect against duplications of file names, to not read config twice. 690# Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so 691# there possibility of duplication of filename there doesn't matter. 692$GITWEB_CONFIG=""if($GITWEB_CONFIGeq$GITWEB_CONFIG_COMMON); 693$GITWEB_CONFIG_SYSTEM=""if($GITWEB_CONFIG_SYSTEMeq$GITWEB_CONFIG_COMMON); 694 695# Common system-wide settings for convenience. 696# Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM. 697 read_config_file($GITWEB_CONFIG_COMMON); 698 699# Use first config file that exists. This means use the per-instance 700# GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG. 701 read_config_file($GITWEB_CONFIG)andreturn; 702 read_config_file($GITWEB_CONFIG_SYSTEM); 703} 704 705# Get loadavg of system, to compare against $maxload. 706# Currently it requires '/proc/loadavg' present to get loadavg; 707# if it is not present it returns 0, which means no load checking. 708sub get_loadavg { 709if( -e '/proc/loadavg'){ 710open my$fd,'<','/proc/loadavg' 711orreturn0; 712my@load=split(/\s+/,scalar<$fd>); 713close$fd; 714 715# The first three columns measure CPU and IO utilization of the last one, 716# five, and 10 minute periods. The fourth column shows the number of 717# currently running processes and the total number of processes in the m/n 718# format. The last column displays the last process ID used. 719return$load[0] ||0; 720} 721# additional checks for load average should go here for things that don't export 722# /proc/loadavg 723 724return0; 725} 726 727# version of the core git binary 728our$git_version; 729sub evaluate_git_version { 730our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 731$number_of_git_cmds++; 732} 733 734sub check_loadavg { 735if(defined$maxload&& get_loadavg() >$maxload) { 736 die_error(503,"The load average on the server is too high"); 737} 738} 739 740# ====================================================================== 741# input validation and dispatch 742 743# input parameters can be collected from a variety of sources (presently, CGI 744# and PATH_INFO), so we define an %input_params hash that collects them all 745# together during validation: this allows subsequent uses (e.g. href()) to be 746# agnostic of the parameter origin 747 748our%input_params= (); 749 750# input parameters are stored with the long parameter name as key. This will 751# also be used in the href subroutine to convert parameters to their CGI 752# equivalent, and since the href() usage is the most frequent one, we store 753# the name -> CGI key mapping here, instead of the reverse. 754# 755# XXX: Warning: If you touch this, check the search form for updating, 756# too. 757 758our@cgi_param_mapping= ( 759 project =>"p", 760 action =>"a", 761 file_name =>"f", 762 file_parent =>"fp", 763 hash =>"h", 764 hash_parent =>"hp", 765 hash_base =>"hb", 766 hash_parent_base =>"hpb", 767 page =>"pg", 768 order =>"o", 769 searchtext =>"s", 770 searchtype =>"st", 771 snapshot_format =>"sf", 772 extra_options =>"opt", 773 search_use_regexp =>"sr", 774 ctag =>"by_tag", 775 diff_style =>"ds", 776 project_filter =>"pf", 777# this must be last entry (for manipulation from JavaScript) 778 javascript =>"js" 779); 780our%cgi_param_mapping=@cgi_param_mapping; 781 782# we will also need to know the possible actions, for validation 783our%actions= ( 784"blame"=> \&git_blame, 785"blame_incremental"=> \&git_blame_incremental, 786"blame_data"=> \&git_blame_data, 787"blobdiff"=> \&git_blobdiff, 788"blobdiff_plain"=> \&git_blobdiff_plain, 789"blob"=> \&git_blob, 790"blob_plain"=> \&git_blob_plain, 791"commitdiff"=> \&git_commitdiff, 792"commitdiff_plain"=> \&git_commitdiff_plain, 793"commit"=> \&git_commit, 794"forks"=> \&git_forks, 795"heads"=> \&git_heads, 796"history"=> \&git_history, 797"log"=> \&git_log, 798"patch"=> \&git_patch, 799"patches"=> \&git_patches, 800"remotes"=> \&git_remotes, 801"rss"=> \&git_rss, 802"atom"=> \&git_atom, 803"search"=> \&git_search, 804"search_help"=> \&git_search_help, 805"shortlog"=> \&git_shortlog, 806"summary"=> \&git_summary, 807"tag"=> \&git_tag, 808"tags"=> \&git_tags, 809"tree"=> \&git_tree, 810"snapshot"=> \&git_snapshot, 811"object"=> \&git_object, 812# those below don't need $project 813"opml"=> \&git_opml, 814"project_list"=> \&git_project_list, 815"project_index"=> \&git_project_index, 816); 817 818# finally, we have the hash of allowed extra_options for the commands that 819# allow them 820our%allowed_options= ( 821"--no-merges"=> [qw(rss atom log shortlog history)], 822); 823 824# fill %input_params with the CGI parameters. All values except for 'opt' 825# should be single values, but opt can be an array. We should probably 826# build an array of parameters that can be multi-valued, but since for the time 827# being it's only this one, we just single it out 828sub evaluate_query_params { 829our$cgi; 830 831while(my($name,$symbol) =each%cgi_param_mapping) { 832if($symboleq'opt') { 833$input_params{$name} = [map{ decode_utf8($_) }$cgi->param($symbol) ]; 834}else{ 835$input_params{$name} = decode_utf8($cgi->param($symbol)); 836} 837} 838} 839 840# now read PATH_INFO and update the parameter list for missing parameters 841sub evaluate_path_info { 842return ifdefined$input_params{'project'}; 843return if!$path_info; 844$path_info=~ s,^/+,,; 845return if!$path_info; 846 847# find which part of PATH_INFO is project 848my$project=$path_info; 849$project=~ s,/+$,,; 850while($project&& !check_head_link("$projectroot/$project")) { 851$project=~ s,/*[^/]*$,,; 852} 853return unless$project; 854$input_params{'project'} =$project; 855 856# do not change any parameters if an action is given using the query string 857return if$input_params{'action'}; 858$path_info=~ s,^\Q$project\E/*,,; 859 860# next, check if we have an action 861my$action=$path_info; 862$action=~ s,/.*$,,; 863if(exists$actions{$action}) { 864$path_info=~ s,^$action/*,,; 865$input_params{'action'} =$action; 866} 867 868# list of actions that want hash_base instead of hash, but can have no 869# pathname (f) parameter 870my@wants_base= ( 871'tree', 872'history', 873); 874 875# we want to catch, among others 876# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 877my($parentrefname,$parentpathname,$refname,$pathname) = 878($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/); 879 880# first, analyze the 'current' part 881if(defined$pathname) { 882# we got "branch:filename" or "branch:dir/" 883# we could use git_get_type(branch:pathname), but: 884# - it needs $git_dir 885# - it does a git() call 886# - the convention of terminating directories with a slash 887# makes it superfluous 888# - embedding the action in the PATH_INFO would make it even 889# more superfluous 890$pathname=~ s,^/+,,; 891if(!$pathname||substr($pathname, -1)eq"/") { 892$input_params{'action'} ||="tree"; 893$pathname=~ s,/$,,; 894}else{ 895# the default action depends on whether we had parent info 896# or not 897if($parentrefname) { 898$input_params{'action'} ||="blobdiff_plain"; 899}else{ 900$input_params{'action'} ||="blob_plain"; 901} 902} 903$input_params{'hash_base'} ||=$refname; 904$input_params{'file_name'} ||=$pathname; 905}elsif(defined$refname) { 906# we got "branch". In this case we have to choose if we have to 907# set hash or hash_base. 908# 909# Most of the actions without a pathname only want hash to be 910# set, except for the ones specified in @wants_base that want 911# hash_base instead. It should also be noted that hand-crafted 912# links having 'history' as an action and no pathname or hash 913# set will fail, but that happens regardless of PATH_INFO. 914if(defined$parentrefname) { 915# if there is parent let the default be 'shortlog' action 916# (for http://git.example.com/repo.git/A..B links); if there 917# is no parent, dispatch will detect type of object and set 918# action appropriately if required (if action is not set) 919$input_params{'action'} ||="shortlog"; 920} 921if($input_params{'action'} && 922grep{$_eq$input_params{'action'} }@wants_base) { 923$input_params{'hash_base'} ||=$refname; 924}else{ 925$input_params{'hash'} ||=$refname; 926} 927} 928 929# next, handle the 'parent' part, if present 930if(defined$parentrefname) { 931# a missing pathspec defaults to the 'current' filename, allowing e.g. 932# someproject/blobdiff/oldrev..newrev:/filename 933if($parentpathname) { 934$parentpathname=~ s,^/+,,; 935$parentpathname=~ s,/$,,; 936$input_params{'file_parent'} ||=$parentpathname; 937}else{ 938$input_params{'file_parent'} ||=$input_params{'file_name'}; 939} 940# we assume that hash_parent_base is wanted if a path was specified, 941# or if the action wants hash_base instead of hash 942if(defined$input_params{'file_parent'} || 943grep{$_eq$input_params{'action'} }@wants_base) { 944$input_params{'hash_parent_base'} ||=$parentrefname; 945}else{ 946$input_params{'hash_parent'} ||=$parentrefname; 947} 948} 949 950# for the snapshot action, we allow URLs in the form 951# $project/snapshot/$hash.ext 952# where .ext determines the snapshot and gets removed from the 953# passed $refname to provide the $hash. 954# 955# To be able to tell that $refname includes the format extension, we 956# require the following two conditions to be satisfied: 957# - the hash input parameter MUST have been set from the $refname part 958# of the URL (i.e. they must be equal) 959# - the snapshot format MUST NOT have been defined already (e.g. from 960# CGI parameter sf) 961# It's also useless to try any matching unless $refname has a dot, 962# so we check for that too 963if(defined$input_params{'action'} && 964$input_params{'action'}eq'snapshot'&& 965defined$refname&&index($refname,'.') != -1&& 966$refnameeq$input_params{'hash'} && 967!defined$input_params{'snapshot_format'}) { 968# We loop over the known snapshot formats, checking for 969# extensions. Allowed extensions are both the defined suffix 970# (which includes the initial dot already) and the snapshot 971# format key itself, with a prepended dot 972while(my($fmt,$opt) =each%known_snapshot_formats) { 973my$hash=$refname; 974unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 975next; 976} 977my$sfx=$1; 978# a valid suffix was found, so set the snapshot format 979# and reset the hash parameter 980$input_params{'snapshot_format'} =$fmt; 981$input_params{'hash'} =$hash; 982# we also set the format suffix to the one requested 983# in the URL: this way a request for e.g. .tgz returns 984# a .tgz instead of a .tar.gz 985$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 986last; 987} 988} 989} 990 991our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 992$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 993$searchtext,$search_regexp,$project_filter); 994sub evaluate_and_validate_params { 995our$action=$input_params{'action'}; 996if(defined$action) { 997if(!validate_action($action)) { 998 die_error(400,"Invalid action parameter"); 999}1000}10011002# parameters which are pathnames1003our$project=$input_params{'project'};1004if(defined$project) {1005if(!validate_project($project)) {1006undef$project;1007 die_error(404,"No such project");1008}1009}10101011our$project_filter=$input_params{'project_filter'};1012if(defined$project_filter) {1013if(!validate_pathname($project_filter)) {1014 die_error(404,"Invalid project_filter parameter");1015}1016}10171018our$file_name=$input_params{'file_name'};1019if(defined$file_name) {1020if(!validate_pathname($file_name)) {1021 die_error(400,"Invalid file parameter");1022}1023}10241025our$file_parent=$input_params{'file_parent'};1026if(defined$file_parent) {1027if(!validate_pathname($file_parent)) {1028 die_error(400,"Invalid file parent parameter");1029}1030}10311032# parameters which are refnames1033our$hash=$input_params{'hash'};1034if(defined$hash) {1035if(!validate_refname($hash)) {1036 die_error(400,"Invalid hash parameter");1037}1038}10391040our$hash_parent=$input_params{'hash_parent'};1041if(defined$hash_parent) {1042if(!validate_refname($hash_parent)) {1043 die_error(400,"Invalid hash parent parameter");1044}1045}10461047our$hash_base=$input_params{'hash_base'};1048if(defined$hash_base) {1049if(!validate_refname($hash_base)) {1050 die_error(400,"Invalid hash base parameter");1051}1052}10531054our@extra_options= @{$input_params{'extra_options'}};1055# @extra_options is always defined, since it can only be (currently) set from1056# CGI, and $cgi->param() returns the empty array in array context if the param1057# is not set1058foreachmy$opt(@extra_options) {1059if(not exists$allowed_options{$opt}) {1060 die_error(400,"Invalid option parameter");1061}1062if(not grep(/^$action$/, @{$allowed_options{$opt}})) {1063 die_error(400,"Invalid option parameter for this action");1064}1065}10661067our$hash_parent_base=$input_params{'hash_parent_base'};1068if(defined$hash_parent_base) {1069if(!validate_refname($hash_parent_base)) {1070 die_error(400,"Invalid hash parent base parameter");1071}1072}10731074# other parameters1075our$page=$input_params{'page'};1076if(defined$page) {1077if($page=~m/[^0-9]/) {1078 die_error(400,"Invalid page parameter");1079}1080}10811082our$searchtype=$input_params{'searchtype'};1083if(defined$searchtype) {1084if($searchtype=~m/[^a-z]/) {1085 die_error(400,"Invalid searchtype parameter");1086}1087}10881089our$search_use_regexp=$input_params{'search_use_regexp'};10901091our$searchtext=$input_params{'searchtext'};1092our$search_regexp=undef;1093if(defined$searchtext) {1094if(length($searchtext) <2) {1095 die_error(403,"At least two characters are required for search parameter");1096}1097if($search_use_regexp) {1098$search_regexp=$searchtext;1099if(!eval{qr/$search_regexp/;1; }) {1100(my$error=$@) =~s/ at \S+ line \d+.*\n?//;1101 die_error(400,"Invalid search regexp '$search_regexp'",1102 esc_html($error));1103}1104}else{1105$search_regexp=quotemeta$searchtext;1106}1107}1108}11091110# path to the current git repository1111our$git_dir;1112sub evaluate_git_dir {1113our$git_dir="$projectroot/$project"if$project;1114}11151116our(@snapshot_fmts,$git_avatar);1117sub configure_gitweb_features {1118# list of supported snapshot formats1119our@snapshot_fmts= gitweb_get_feature('snapshot');1120@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);11211122# check that the avatar feature is set to a known provider name,1123# and for each provider check if the dependencies are satisfied.1124# if the provider name is invalid or the dependencies are not met,1125# reset $git_avatar to the empty string.1126our($git_avatar) = gitweb_get_feature('avatar');1127if($git_avatareq'gravatar') {1128$git_avatar=''unless(eval{require Digest::MD5;1; });1129}elsif($git_avatareq'picon') {1130# no dependencies1131}else{1132$git_avatar='';1133}1134}11351136# custom error handler: 'die <message>' is Internal Server Error1137sub handle_errors_html {1138my$msg=shift;# it is already HTML escaped11391140# to avoid infinite loop where error occurs in die_error,1141# change handler to default handler, disabling handle_errors_html1142 set_message("Error occurred when inside die_error:\n$msg");11431144# you cannot jump out of die_error when called as error handler;1145# the subroutine set via CGI::Carp::set_message is called _after_1146# HTTP headers are already written, so it cannot write them itself1147 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1);1148}1149set_message(\&handle_errors_html);11501151# dispatch1152sub dispatch {1153if(!defined$action) {1154if(defined$hash) {1155$action= git_get_type($hash);1156$actionor die_error(404,"Object does not exist");1157}elsif(defined$hash_base&&defined$file_name) {1158$action= git_get_type("$hash_base:$file_name");1159$actionor die_error(404,"File or directory does not exist");1160}elsif(defined$project) {1161$action='summary';1162}else{1163$action='project_list';1164}1165}1166if(!defined($actions{$action})) {1167 die_error(400,"Unknown action");1168}1169if($action!~m/^(?:opml|project_list|project_index)$/&&1170!$project) {1171 die_error(400,"Project needed");1172}1173$actions{$action}->();1174}11751176sub reset_timer {1177our$t0= [ gettimeofday() ]1178ifdefined$t0;1179our$number_of_git_cmds=0;1180}11811182our$first_request=1;1183sub run_request {1184 reset_timer();11851186 evaluate_uri();1187if($first_request) {1188 evaluate_gitweb_config();1189 evaluate_git_version();1190}1191if($per_request_config) {1192if(ref($per_request_config)eq'CODE') {1193$per_request_config->();1194}elsif(!$first_request) {1195 evaluate_gitweb_config();1196}1197}1198 check_loadavg();11991200# $projectroot and $projects_list might be set in gitweb config file1201$projects_list||=$projectroot;12021203 evaluate_query_params();1204 evaluate_path_info();1205 evaluate_and_validate_params();1206 evaluate_git_dir();12071208 configure_gitweb_features();12091210 dispatch();1211}12121213our$is_last_request=sub{1};1214our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1215our$CGI='CGI';1216our$cgi;1217sub configure_as_fcgi {1218require CGI::Fast;1219our$CGI='CGI::Fast';12201221my$request_number=0;1222# let each child service 100 requests1223our$is_last_request=sub{ ++$request_number>100};1224}1225sub evaluate_argv {1226my$script_name=$ENV{'SCRIPT_NAME'} ||$ENV{'SCRIPT_FILENAME'} || __FILE__;1227 configure_as_fcgi()1228if$script_name=~/\.fcgi$/;12291230return unless(@ARGV);12311232require Getopt::Long;1233 Getopt::Long::GetOptions(1234'fastcgi|fcgi|f'=> \&configure_as_fcgi,1235'nproc|n=i'=>sub{1236my($arg,$val) =@_;1237return unlesseval{require FCGI::ProcManager;1; };1238my$proc_manager= FCGI::ProcManager->new({1239 n_processes =>$val,1240});1241our$pre_listen_hook=sub{$proc_manager->pm_manage() };1242our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1243our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1244},1245);1246}12471248sub run {1249 evaluate_argv();12501251$first_request=1;1252$pre_listen_hook->()1253if$pre_listen_hook;12541255 REQUEST:1256while($cgi=$CGI->new()) {1257$pre_dispatch_hook->()1258if$pre_dispatch_hook;12591260 run_request();12611262$post_dispatch_hook->()1263if$post_dispatch_hook;1264$first_request=0;12651266last REQUEST if($is_last_request->());1267}12681269 DONE_GITWEB:12701;1271}12721273run();12741275if(defined caller) {1276# wrapped in a subroutine processing requests,1277# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1278return;1279}else{1280# pure CGI script, serving single request1281exit;1282}12831284## ======================================================================1285## action links12861287# possible values of extra options1288# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1289# -replay => 1 - start from a current view (replay with modifications)1290# -path_info => 0|1 - don't use/use path_info URL (if possible)1291# -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone1292sub href {1293my%params=@_;1294# default is to use -absolute url() i.e. $my_uri1295my$href=$params{-full} ?$my_url:$my_uri;12961297# implicit -replay, must be first of implicit params1298$params{-replay} =1if(keys%params==1&&$params{-anchor});12991300$params{'project'} =$projectunlessexists$params{'project'};13011302if($params{-replay}) {1303while(my($name,$symbol) =each%cgi_param_mapping) {1304if(!exists$params{$name}) {1305$params{$name} =$input_params{$name};1306}1307}1308}13091310my$use_pathinfo= gitweb_check_feature('pathinfo');1311if(defined$params{'project'} &&1312(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1313# try to put as many parameters as possible in PATH_INFO:1314# - project name1315# - action1316# - hash_parent or hash_parent_base:/file_parent1317# - hash or hash_base:/filename1318# - the snapshot_format as an appropriate suffix13191320# When the script is the root DirectoryIndex for the domain,1321# $href here would be something like http://gitweb.example.com/1322# Thus, we strip any trailing / from $href, to spare us double1323# slashes in the final URL1324$href=~ s,/$,,;13251326# Then add the project name, if present1327$href.="/".esc_path_info($params{'project'});1328delete$params{'project'};13291330# since we destructively absorb parameters, we keep this1331# boolean that remembers if we're handling a snapshot1332my$is_snapshot=$params{'action'}eq'snapshot';13331334# Summary just uses the project path URL, any other action is1335# added to the URL1336if(defined$params{'action'}) {1337$href.="/".esc_path_info($params{'action'})1338unless$params{'action'}eq'summary';1339delete$params{'action'};1340}13411342# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1343# stripping nonexistent or useless pieces1344$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1345||$params{'hash_parent'} ||$params{'hash'});1346if(defined$params{'hash_base'}) {1347if(defined$params{'hash_parent_base'}) {1348$href.= esc_path_info($params{'hash_parent_base'});1349# skip the file_parent if it's the same as the file_name1350if(defined$params{'file_parent'}) {1351if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1352delete$params{'file_parent'};1353}elsif($params{'file_parent'} !~/\.\./) {1354$href.=":/".esc_path_info($params{'file_parent'});1355delete$params{'file_parent'};1356}1357}1358$href.="..";1359delete$params{'hash_parent'};1360delete$params{'hash_parent_base'};1361}elsif(defined$params{'hash_parent'}) {1362$href.= esc_path_info($params{'hash_parent'})."..";1363delete$params{'hash_parent'};1364}13651366$href.= esc_path_info($params{'hash_base'});1367if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1368$href.=":/".esc_path_info($params{'file_name'});1369delete$params{'file_name'};1370}1371delete$params{'hash'};1372delete$params{'hash_base'};1373}elsif(defined$params{'hash'}) {1374$href.= esc_path_info($params{'hash'});1375delete$params{'hash'};1376}13771378# If the action was a snapshot, we can absorb the1379# snapshot_format parameter too1380if($is_snapshot) {1381my$fmt=$params{'snapshot_format'};1382# snapshot_format should always be defined when href()1383# is called, but just in case some code forgets, we1384# fall back to the default1385$fmt||=$snapshot_fmts[0];1386$href.=$known_snapshot_formats{$fmt}{'suffix'};1387delete$params{'snapshot_format'};1388}1389}13901391# now encode the parameters explicitly1392my@result= ();1393for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1394my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1395if(defined$params{$name}) {1396if(ref($params{$name})eq"ARRAY") {1397foreachmy$par(@{$params{$name}}) {1398push@result,$symbol."=". esc_param($par);1399}1400}else{1401push@result,$symbol."=". esc_param($params{$name});1402}1403}1404}1405$href.="?".join(';',@result)ifscalar@result;14061407# final transformation: trailing spaces must be escaped (URI-encoded)1408$href=~s/(\s+)$/CGI::escape($1)/e;14091410if($params{-anchor}) {1411$href.="#".esc_param($params{-anchor});1412}14131414return$href;1415}141614171418## ======================================================================1419## validation, quoting/unquoting and escaping14201421sub validate_action {1422my$input=shift||returnundef;1423returnundefunlessexists$actions{$input};1424return$input;1425}14261427sub validate_project {1428my$input=shift||returnundef;1429if(!validate_pathname($input) ||1430!(-d "$projectroot/$input") ||1431!check_export_ok("$projectroot/$input") ||1432($strict_export&& !project_in_list($input))) {1433returnundef;1434}else{1435return$input;1436}1437}14381439sub validate_pathname {1440my$input=shift||returnundef;14411442# no '.' or '..' as elements of path, i.e. no '.' nor '..'1443# at the beginning, at the end, and between slashes.1444# also this catches doubled slashes1445if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1446returnundef;1447}1448# no null characters1449if($input=~m!\0!) {1450returnundef;1451}1452return$input;1453}14541455sub is_valid_ref_format {1456my$input=shift||returnundef;14571458# restrictions on ref name according to git-check-ref-format1459if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1460returnundef;1461}1462return$input;1463}14641465sub validate_refname {1466my$input=shift||returnundef;14671468# textual hashes are O.K.1469if($input=~m/^[0-9a-fA-F]{40}$/) {1470return$input;1471}1472# it must be correct pathname1473$input= validate_pathname($input)1474orreturnundef;1475# check git-check-ref-format restrictions1476 is_valid_ref_format($input)1477orreturnundef;1478return$input;1479}14801481# decode sequences of octets in utf8 into Perl's internal form,1482# which is utf-8 with utf8 flag set if needed. gitweb writes out1483# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1484sub to_utf8 {1485my$str=shift;1486returnundefunlessdefined$str;14871488if(utf8::is_utf8($str) || utf8::decode($str)) {1489return$str;1490}else{1491return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1492}1493}14941495# quote unsafe chars, but keep the slash, even when it's not1496# correct, but quoted slashes look too horrible in bookmarks1497sub esc_param {1498my$str=shift;1499returnundefunlessdefined$str;1500$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1501$str=~s/ /\+/g;1502return$str;1503}15041505# the quoting rules for path_info fragment are slightly different1506sub esc_path_info {1507my$str=shift;1508returnundefunlessdefined$str;15091510# path_info doesn't treat '+' as space (specially), but '?' must be escaped1511$str=~s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;15121513return$str;1514}15151516# quote unsafe chars in whole URL, so some characters cannot be quoted1517sub esc_url {1518my$str=shift;1519returnundefunlessdefined$str;1520$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;1521$str=~s/ /\+/g;1522return$str;1523}15241525# quote unsafe characters in HTML attributes1526sub esc_attr {15271528# for XHTML conformance escaping '"' to '"' is not enough1529return esc_html(@_);1530}15311532# replace invalid utf8 character with SUBSTITUTION sequence1533sub esc_html {1534my$str=shift;1535my%opts=@_;15361537returnundefunlessdefined$str;15381539$str= to_utf8($str);1540$str=$cgi->escapeHTML($str);1541if($opts{'-nbsp'}) {1542$str=~s/ / /g;1543}1544$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1545return$str;1546}15471548# quote control characters and escape filename to HTML1549sub esc_path {1550my$str=shift;1551my%opts=@_;15521553returnundefunlessdefined$str;15541555$str= to_utf8($str);1556$str=$cgi->escapeHTML($str);1557if($opts{'-nbsp'}) {1558$str=~s/ / /g;1559}1560$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1561return$str;1562}15631564# Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)1565sub sanitize {1566my$str=shift;15671568returnundefunlessdefined$str;15691570$str= to_utf8($str);1571$str=~ s|([[:cntrl:]])|(index("\t\n\r",$1) != -1?$1: quot_cec($1))|eg;1572return$str;1573}15741575# Make control characters "printable", using character escape codes (CEC)1576sub quot_cec {1577my$cntrl=shift;1578my%opts=@_;1579my%es= (# character escape codes, aka escape sequences1580"\t"=>'\t',# tab (HT)1581"\n"=>'\n',# line feed (LF)1582"\r"=>'\r',# carrige return (CR)1583"\f"=>'\f',# form feed (FF)1584"\b"=>'\b',# backspace (BS)1585"\a"=>'\a',# alarm (bell) (BEL)1586"\e"=>'\e',# escape (ESC)1587"\013"=>'\v',# vertical tab (VT)1588"\000"=>'\0',# nul character (NUL)1589);1590my$chr= ( (exists$es{$cntrl})1591?$es{$cntrl}1592:sprintf('\%2x',ord($cntrl)) );1593if($opts{-nohtml}) {1594return$chr;1595}else{1596return"<span class=\"cntrl\">$chr</span>";1597}1598}15991600# Alternatively use unicode control pictures codepoints,1601# Unicode "printable representation" (PR)1602sub quot_upr {1603my$cntrl=shift;1604my%opts=@_;16051606my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1607if($opts{-nohtml}) {1608return$chr;1609}else{1610return"<span class=\"cntrl\">$chr</span>";1611}1612}16131614# git may return quoted and escaped filenames1615sub unquote {1616my$str=shift;16171618sub unq {1619my$seq=shift;1620my%es= (# character escape codes, aka escape sequences1621't'=>"\t",# tab (HT, TAB)1622'n'=>"\n",# newline (NL)1623'r'=>"\r",# return (CR)1624'f'=>"\f",# form feed (FF)1625'b'=>"\b",# backspace (BS)1626'a'=>"\a",# alarm (bell) (BEL)1627'e'=>"\e",# escape (ESC)1628'v'=>"\013",# vertical tab (VT)1629);16301631if($seq=~m/^[0-7]{1,3}$/) {1632# octal char sequence1633returnchr(oct($seq));1634}elsif(exists$es{$seq}) {1635# C escape sequence, aka character escape code1636return$es{$seq};1637}1638# quoted ordinary character1639return$seq;1640}16411642if($str=~m/^"(.*)"$/) {1643# needs unquoting1644$str=$1;1645$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1646}1647return$str;1648}16491650# escape tabs (convert tabs to spaces)1651sub untabify {1652my$line=shift;16531654while((my$pos=index($line,"\t")) != -1) {1655if(my$count= (8- ($pos%8))) {1656my$spaces=' ' x $count;1657$line=~s/\t/$spaces/;1658}1659}16601661return$line;1662}16631664sub project_in_list {1665my$project=shift;1666my@list= git_get_projects_list();1667return@list&&scalar(grep{$_->{'path'}eq$project}@list);1668}16691670## ----------------------------------------------------------------------1671## HTML aware string manipulation16721673# Try to chop given string on a word boundary between position1674# $len and $len+$add_len. If there is no word boundary there,1675# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1676# (marking chopped part) would be longer than given string.1677sub chop_str {1678my$str=shift;1679my$len=shift;1680my$add_len=shift||10;1681my$where=shift||'right';# 'left' | 'center' | 'right'16821683# Make sure perl knows it is utf8 encoded so we don't1684# cut in the middle of a utf8 multibyte char.1685$str= to_utf8($str);16861687# allow only $len chars, but don't cut a word if it would fit in $add_len1688# if it doesn't fit, cut it if it's still longer than the dots we would add1689# remove chopped character entities entirely16901691# when chopping in the middle, distribute $len into left and right part1692# return early if chopping wouldn't make string shorter1693if($whereeq'center') {1694return$strif($len+5>=length($str));# filler is length 51695$len=int($len/2);1696}else{1697return$strif($len+4>=length($str));# filler is length 41698}16991700# regexps: ending and beginning with word part up to $add_len1701my$endre=qr/.{$len}\w{0,$add_len}/;1702my$begre=qr/\w{0,$add_len}.{$len}/;17031704if($whereeq'left') {1705$str=~m/^(.*?)($begre)$/;1706my($lead,$body) = ($1,$2);1707if(length($lead) >4) {1708$lead=" ...";1709}1710return"$lead$body";17111712}elsif($whereeq'center') {1713$str=~m/^($endre)(.*)$/;1714my($left,$str) = ($1,$2);1715$str=~m/^(.*?)($begre)$/;1716my($mid,$right) = ($1,$2);1717if(length($mid) >5) {1718$mid=" ... ";1719}1720return"$left$mid$right";17211722}else{1723$str=~m/^($endre)(.*)$/;1724my$body=$1;1725my$tail=$2;1726if(length($tail) >4) {1727$tail="... ";1728}1729return"$body$tail";1730}1731}17321733# takes the same arguments as chop_str, but also wraps a <span> around the1734# result with a title attribute if it does get chopped. Additionally, the1735# string is HTML-escaped.1736sub chop_and_escape_str {1737my($str) =@_;17381739my$chopped= chop_str(@_);1740$str= to_utf8($str);1741if($choppedeq$str) {1742return esc_html($chopped);1743}else{1744$str=~s/[[:cntrl:]]/?/g;1745return$cgi->span({-title=>$str}, esc_html($chopped));1746}1747}17481749# Highlight selected fragments of string, using given CSS class,1750# and escape HTML. It is assumed that fragments do not overlap.1751# Regions are passed as list of pairs (array references).1752#1753# Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns1754# '<span class="mark">foo</span>bar'1755sub esc_html_hl_regions {1756my($str,$css_class,@sel) =@_;1757my%opts=grep{ref($_)ne'ARRAY'}@sel;1758@sel=grep{ref($_)eq'ARRAY'}@sel;1759return esc_html($str,%opts)unless@sel;17601761my$out='';1762my$pos=0;17631764formy$s(@sel) {1765my($begin,$end) =@$s;17661767# Don't create empty <span> elements.1768next if$end<=$begin;17691770my$escaped= esc_html(substr($str,$begin,$end-$begin),1771%opts);17721773$out.= esc_html(substr($str,$pos,$begin-$pos),%opts)1774if($begin-$pos>0);1775$out.=$cgi->span({-class=>$css_class},$escaped);17761777$pos=$end;1778}1779$out.= esc_html(substr($str,$pos),%opts)1780if($pos<length($str));17811782return$out;1783}17841785# return positions of beginning and end of each match1786sub matchpos_list {1787my($str,$regexp) =@_;1788return unless(defined$str&&defined$regexp);17891790my@matches;1791while($str=~/$regexp/g) {1792push@matches, [$-[0],$+[0]];1793}1794return@matches;1795}17961797# highlight match (if any), and escape HTML1798sub esc_html_match_hl {1799my($str,$regexp) =@_;1800return esc_html($str)unlessdefined$regexp;18011802my@matches= matchpos_list($str,$regexp);1803return esc_html($str)unless@matches;18041805return esc_html_hl_regions($str,'match',@matches);1806}180718081809# highlight match (if any) of shortened string, and escape HTML1810sub esc_html_match_hl_chopped {1811my($str,$chopped,$regexp) =@_;1812return esc_html_match_hl($str,$regexp)unlessdefined$chopped;18131814my@matches= matchpos_list($str,$regexp);1815return esc_html($chopped)unless@matches;18161817# filter matches so that we mark chopped string1818my$tail="... ";# see chop_str1819unless($chopped=~s/\Q$tail\E$//) {1820$tail='';1821}1822my$chop_len=length($chopped);1823my$tail_len=length($tail);1824my@filtered;18251826formy$m(@matches) {1827if($m->[0] >$chop_len) {1828push@filtered, [$chop_len,$chop_len+$tail_len]if($tail_len>0);1829last;1830}elsif($m->[1] >$chop_len) {1831push@filtered, [$m->[0],$chop_len+$tail_len];1832last;1833}1834push@filtered,$m;1835}18361837return esc_html_hl_regions($chopped.$tail,'match',@filtered);1838}18391840## ----------------------------------------------------------------------1841## functions returning short strings18421843# CSS class for given age value (in seconds)1844sub age_class {1845my$age=shift;18461847if(!defined$age) {1848return"noage";1849}elsif($age<60*60*2) {1850return"age0";1851}elsif($age<60*60*24*2) {1852return"age1";1853}else{1854return"age2";1855}1856}18571858# convert age in seconds to "nn units ago" string1859sub age_string {1860my$age=shift;1861my$age_str;18621863if($age>60*60*24*365*2) {1864$age_str= (int$age/60/60/24/365);1865$age_str.=" years ago";1866}elsif($age>60*60*24*(365/12)*2) {1867$age_str=int$age/60/60/24/(365/12);1868$age_str.=" months ago";1869}elsif($age>60*60*24*7*2) {1870$age_str=int$age/60/60/24/7;1871$age_str.=" weeks ago";1872}elsif($age>60*60*24*2) {1873$age_str=int$age/60/60/24;1874$age_str.=" days ago";1875}elsif($age>60*60*2) {1876$age_str=int$age/60/60;1877$age_str.=" hours ago";1878}elsif($age>60*2) {1879$age_str=int$age/60;1880$age_str.=" min ago";1881}elsif($age>2) {1882$age_str=int$age;1883$age_str.=" sec ago";1884}else{1885$age_str.=" right now";1886}1887return$age_str;1888}18891890useconstant{1891 S_IFINVALID =>0030000,1892 S_IFGITLINK =>0160000,1893};18941895# submodule/subproject, a commit object reference1896sub S_ISGITLINK {1897my$mode=shift;18981899return(($mode& S_IFMT) == S_IFGITLINK)1900}19011902# convert file mode in octal to symbolic file mode string1903sub mode_str {1904my$mode=oct shift;19051906if(S_ISGITLINK($mode)) {1907return'm---------';1908}elsif(S_ISDIR($mode& S_IFMT)) {1909return'drwxr-xr-x';1910}elsif(S_ISLNK($mode)) {1911return'lrwxrwxrwx';1912}elsif(S_ISREG($mode)) {1913# git cares only about the executable bit1914if($mode& S_IXUSR) {1915return'-rwxr-xr-x';1916}else{1917return'-rw-r--r--';1918};1919}else{1920return'----------';1921}1922}19231924# convert file mode in octal to file type string1925sub file_type {1926my$mode=shift;19271928if($mode!~m/^[0-7]+$/) {1929return$mode;1930}else{1931$mode=oct$mode;1932}19331934if(S_ISGITLINK($mode)) {1935return"submodule";1936}elsif(S_ISDIR($mode& S_IFMT)) {1937return"directory";1938}elsif(S_ISLNK($mode)) {1939return"symlink";1940}elsif(S_ISREG($mode)) {1941return"file";1942}else{1943return"unknown";1944}1945}19461947# convert file mode in octal to file type description string1948sub file_type_long {1949my$mode=shift;19501951if($mode!~m/^[0-7]+$/) {1952return$mode;1953}else{1954$mode=oct$mode;1955}19561957if(S_ISGITLINK($mode)) {1958return"submodule";1959}elsif(S_ISDIR($mode& S_IFMT)) {1960return"directory";1961}elsif(S_ISLNK($mode)) {1962return"symlink";1963}elsif(S_ISREG($mode)) {1964if($mode& S_IXUSR) {1965return"executable";1966}else{1967return"file";1968};1969}else{1970return"unknown";1971}1972}197319741975## ----------------------------------------------------------------------1976## functions returning short HTML fragments, or transforming HTML fragments1977## which don't belong to other sections19781979# format line of commit message.1980sub format_log_line_html {1981my$line=shift;19821983$line= esc_html($line, -nbsp=>1);1984$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1985$cgi->a({-href => href(action=>"object", hash=>$1),1986-class=>"text"},$1);1987}eg;19881989return$line;1990}19911992# format marker of refs pointing to given object19931994# the destination action is chosen based on object type and current context:1995# - for annotated tags, we choose the tag view unless it's the current view1996# already, in which case we go to shortlog view1997# - for other refs, we keep the current view if we're in history, shortlog or1998# log view, and select shortlog otherwise1999sub format_ref_marker {2000my($refs,$id) =@_;2001my$markers='';20022003if(defined$refs->{$id}) {2004foreachmy$ref(@{$refs->{$id}}) {2005# this code exploits the fact that non-lightweight tags are the2006# only indirect objects, and that they are the only objects for which2007# we want to use tag instead of shortlog as action2008my($type,$name) =qw();2009my$indirect= ($ref=~s/\^\{\}$//);2010# e.g. tags/v2.6.11 or heads/next2011if($ref=~m!^(.*?)s?/(.*)$!) {2012$type=$1;2013$name=$2;2014}else{2015$type="ref";2016$name=$ref;2017}20182019my$class=$type;2020$class.=" indirect"if$indirect;20212022my$dest_action="shortlog";20232024if($indirect) {2025$dest_action="tag"unless$actioneq"tag";2026}elsif($action=~/^(history|(short)?log)$/) {2027$dest_action=$action;2028}20292030my$dest="";2031$dest.="refs/"unless$ref=~ m!^refs/!;2032$dest.=$ref;20332034my$link=$cgi->a({2035-href => href(2036 action=>$dest_action,2037 hash=>$dest2038)},$name);20392040$markers.=" <span class=\"".esc_attr($class)."\"title=\"".esc_attr($ref)."\">".2041$link."</span>";2042}2043}20442045if($markers) {2046return' <span class="refs">'.$markers.'</span>';2047}else{2048return"";2049}2050}20512052# format, perhaps shortened and with markers, title line2053sub format_subject_html {2054my($long,$short,$href,$extra) =@_;2055$extra=''unlessdefined($extra);20562057if(length($short) <length($long)) {2058$long=~s/[[:cntrl:]]/?/g;2059return$cgi->a({-href =>$href, -class=>"list subject",2060-title => to_utf8($long)},2061 esc_html($short)) .$extra;2062}else{2063return$cgi->a({-href =>$href, -class=>"list subject"},2064 esc_html($long)) .$extra;2065}2066}20672068# Rather than recomputing the url for an email multiple times, we cache it2069# after the first hit. This gives a visible benefit in views where the avatar2070# for the same email is used repeatedly (e.g. shortlog).2071# The cache is shared by all avatar engines (currently gravatar only), which2072# are free to use it as preferred. Since only one avatar engine is used for any2073# given page, there's no risk for cache conflicts.2074our%avatar_cache= ();20752076# Compute the picon url for a given email, by using the picon search service over at2077# http://www.cs.indiana.edu/picons/search.html2078sub picon_url {2079my$email=lc shift;2080if(!$avatar_cache{$email}) {2081my($user,$domain) =split('@',$email);2082$avatar_cache{$email} =2083"//www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".2084"$domain/$user/".2085"users+domains+unknown/up/single";2086}2087return$avatar_cache{$email};2088}20892090# Compute the gravatar url for a given email, if it's not in the cache already.2091# Gravatar stores only the part of the URL before the size, since that's the2092# one computationally more expensive. This also allows reuse of the cache for2093# different sizes (for this particular engine).2094sub gravatar_url {2095my$email=lc shift;2096my$size=shift;2097$avatar_cache{$email} ||=2098"//www.gravatar.com/avatar/".2099 Digest::MD5::md5_hex($email) ."?s=";2100return$avatar_cache{$email} .$size;2101}21022103# Insert an avatar for the given $email at the given $size if the feature2104# is enabled.2105sub git_get_avatar {2106my($email,%opts) =@_;2107my$pre_white= ($opts{-pad_before} ?" ":"");2108my$post_white= ($opts{-pad_after} ?" ":"");2109$opts{-size} ||='default';2110my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};2111my$url="";2112if($git_avatareq'gravatar') {2113$url= gravatar_url($email,$size);2114}elsif($git_avatareq'picon') {2115$url= picon_url($email);2116}2117# Other providers can be added by extending the if chain, defining $url2118# as needed. If no variant puts something in $url, we assume avatars2119# are completely disabled/unavailable.2120if($url) {2121return$pre_white.2122"<img width=\"$size\"".2123"class=\"avatar\"".2124"src=\"".esc_url($url)."\"".2125"alt=\"\"".2126"/>".$post_white;2127}else{2128return"";2129}2130}21312132sub format_search_author {2133my($author,$searchtype,$displaytext) =@_;2134my$have_search= gitweb_check_feature('search');21352136if($have_search) {2137my$performed="";2138if($searchtypeeq'author') {2139$performed="authored";2140}elsif($searchtypeeq'committer') {2141$performed="committed";2142}21432144return$cgi->a({-href => href(action=>"search", hash=>$hash,2145 searchtext=>$author,2146 searchtype=>$searchtype),class=>"list",2147 title=>"Search for commits$performedby$author"},2148$displaytext);21492150}else{2151return$displaytext;2152}2153}21542155# format the author name of the given commit with the given tag2156# the author name is chopped and escaped according to the other2157# optional parameters (see chop_str).2158sub format_author_html {2159my$tag=shift;2160my$co=shift;2161my$author= chop_and_escape_str($co->{'author_name'},@_);2162return"<$tagclass=\"author\">".2163 format_search_author($co->{'author_name'},"author",2164 git_get_avatar($co->{'author_email'}, -pad_after =>1) .2165$author) .2166"</$tag>";2167}21682169# format git diff header line, i.e. "diff --(git|combined|cc) ..."2170sub format_git_diff_header_line {2171my$line=shift;2172my$diffinfo=shift;2173my($from,$to) =@_;21742175if($diffinfo->{'nparents'}) {2176# combined diff2177$line=~s!^(diff (.*?) )"?.*$!$1!;2178if($to->{'href'}) {2179$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2180 esc_path($to->{'file'}));2181}else{# file was deleted (no href)2182$line.= esc_path($to->{'file'});2183}2184}else{2185# "ordinary" diff2186$line=~s!^(diff (.*?) )"?a/.*$!$1!;2187if($from->{'href'}) {2188$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},2189'a/'. esc_path($from->{'file'}));2190}else{# file was added (no href)2191$line.='a/'. esc_path($from->{'file'});2192}2193$line.=' ';2194if($to->{'href'}) {2195$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2196'b/'. esc_path($to->{'file'}));2197}else{# file was deleted2198$line.='b/'. esc_path($to->{'file'});2199}2200}22012202return"<div class=\"diff header\">$line</div>\n";2203}22042205# format extended diff header line, before patch itself2206sub format_extended_diff_header_line {2207my$line=shift;2208my$diffinfo=shift;2209my($from,$to) =@_;22102211# match <path>2212if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {2213$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},2214 esc_path($from->{'file'}));2215}2216if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {2217$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},2218 esc_path($to->{'file'}));2219}2220# match single <mode>2221if($line=~m/\s(\d{6})$/) {2222$line.='<span class="info"> ('.2223 file_type_long($1) .2224')</span>';2225}2226# match <hash>2227if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {2228# can match only for combined diff2229$line='index ';2230for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2231if($from->{'href'}[$i]) {2232$line.=$cgi->a({-href=>$from->{'href'}[$i],2233-class=>"hash"},2234substr($diffinfo->{'from_id'}[$i],0,7));2235}else{2236$line.='0' x 7;2237}2238# separator2239$line.=','if($i<$diffinfo->{'nparents'} -1);2240}2241$line.='..';2242if($to->{'href'}) {2243$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2244substr($diffinfo->{'to_id'},0,7));2245}else{2246$line.='0' x 7;2247}22482249}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {2250# can match only for ordinary diff2251my($from_link,$to_link);2252if($from->{'href'}) {2253$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},2254substr($diffinfo->{'from_id'},0,7));2255}else{2256$from_link='0' x 7;2257}2258if($to->{'href'}) {2259$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2260substr($diffinfo->{'to_id'},0,7));2261}else{2262$to_link='0' x 7;2263}2264my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});2265$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;2266}22672268return$line."<br/>\n";2269}22702271# format from-file/to-file diff header2272sub format_diff_from_to_header {2273my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;2274my$line;2275my$result='';22762277$line=$from_line;2278#assert($line =~ m/^---/) if DEBUG;2279# no extra formatting for "^--- /dev/null"2280if(!$diffinfo->{'nparents'}) {2281# ordinary (single parent) diff2282if($line=~m!^--- "?a/!) {2283if($from->{'href'}) {2284$line='--- a/'.2285$cgi->a({-href=>$from->{'href'}, -class=>"path"},2286 esc_path($from->{'file'}));2287}else{2288$line='--- a/'.2289 esc_path($from->{'file'});2290}2291}2292$result.= qq!<div class="diff from_file">$line</div>\n!;22932294}else{2295# combined diff (merge commit)2296for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2297if($from->{'href'}[$i]) {2298$line='--- '.2299$cgi->a({-href=>href(action=>"blobdiff",2300 hash_parent=>$diffinfo->{'from_id'}[$i],2301 hash_parent_base=>$parents[$i],2302 file_parent=>$from->{'file'}[$i],2303 hash=>$diffinfo->{'to_id'},2304 hash_base=>$hash,2305 file_name=>$to->{'file'}),2306-class=>"path",2307-title=>"diff". ($i+1)},2308$i+1) .2309'/'.2310$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2311 esc_path($from->{'file'}[$i]));2312}else{2313$line='--- /dev/null';2314}2315$result.= qq!<div class="diff from_file">$line</div>\n!;2316}2317}23182319$line=$to_line;2320#assert($line =~ m/^\+\+\+/) if DEBUG;2321# no extra formatting for "^+++ /dev/null"2322if($line=~m!^\+\+\+ "?b/!) {2323if($to->{'href'}) {2324$line='+++ b/'.2325$cgi->a({-href=>$to->{'href'}, -class=>"path"},2326 esc_path($to->{'file'}));2327}else{2328$line='+++ b/'.2329 esc_path($to->{'file'});2330}2331}2332$result.= qq!<div class="diff to_file">$line</div>\n!;23332334return$result;2335}23362337# create note for patch simplified by combined diff2338sub format_diff_cc_simplified {2339my($diffinfo,@parents) =@_;2340my$result='';23412342$result.="<div class=\"diff header\">".2343"diff --cc ";2344if(!is_deleted($diffinfo)) {2345$result.=$cgi->a({-href => href(action=>"blob",2346 hash_base=>$hash,2347 hash=>$diffinfo->{'to_id'},2348 file_name=>$diffinfo->{'to_file'}),2349-class=>"path"},2350 esc_path($diffinfo->{'to_file'}));2351}else{2352$result.= esc_path($diffinfo->{'to_file'});2353}2354$result.="</div>\n".# class="diff header"2355"<div class=\"diff nodifferences\">".2356"Simple merge".2357"</div>\n";# class="diff nodifferences"23582359return$result;2360}23612362sub diff_line_class {2363my($line,$from,$to) =@_;23642365# ordinary diff2366my$num_sign=1;2367# combined diff2368if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2369$num_sign=scalar@{$from->{'href'}};2370}23712372my@diff_line_classifier= (2373{ regexp =>qr/^\@\@{$num_sign} /,class=>"chunk_header"},2374{ regexp =>qr/^\\/,class=>"incomplete"},2375{ regexp =>qr/^ {$num_sign}/,class=>"ctx"},2376# classifier for context must come before classifier add/rem,2377# or we would have to use more complicated regexp, for example2378# qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;2379{ regexp =>qr/^[+ ]{$num_sign}/,class=>"add"},2380{ regexp =>qr/^[- ]{$num_sign}/,class=>"rem"},2381);2382formy$clsfy(@diff_line_classifier) {2383return$clsfy->{'class'}2384if($line=~$clsfy->{'regexp'});2385}23862387# fallback2388return"";2389}23902391# assumes that $from and $to are defined and correctly filled,2392# and that $line holds a line of chunk header for unified diff2393sub format_unidiff_chunk_header {2394my($line,$from,$to) =@_;23952396my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2397$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;23982399$from_lines=0unlessdefined$from_lines;2400$to_lines=0unlessdefined$to_lines;24012402if($from->{'href'}) {2403$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2404-class=>"list"},$from_text);2405}2406if($to->{'href'}) {2407$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2408-class=>"list"},$to_text);2409}2410$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2411"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2412return$line;2413}24142415# assumes that $from and $to are defined and correctly filled,2416# and that $line holds a line of chunk header for combined diff2417sub format_cc_diff_chunk_header {2418my($line,$from,$to) =@_;24192420my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2421my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);24222423@from_text=split(' ',$ranges);2424for(my$i=0;$i<@from_text; ++$i) {2425($from_start[$i],$from_nlines[$i]) =2426(split(',',substr($from_text[$i],1)),0);2427}24282429$to_text=pop@from_text;2430$to_start=pop@from_start;2431$to_nlines=pop@from_nlines;24322433$line="<span class=\"chunk_info\">$prefix";2434for(my$i=0;$i<@from_text; ++$i) {2435if($from->{'href'}[$i]) {2436$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2437-class=>"list"},$from_text[$i]);2438}else{2439$line.=$from_text[$i];2440}2441$line.=" ";2442}2443if($to->{'href'}) {2444$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2445-class=>"list"},$to_text);2446}else{2447$line.=$to_text;2448}2449$line.="$prefix</span>".2450"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2451return$line;2452}24532454# process patch (diff) line (not to be used for diff headers),2455# returning HTML-formatted (but not wrapped) line.2456# If the line is passed as a reference, it is treated as HTML and not2457# esc_html()'ed.2458sub format_diff_line {2459my($line,$diff_class,$from,$to) =@_;24602461if(ref($line)) {2462$line=$$line;2463}else{2464chomp$line;2465$line= untabify($line);24662467if($from&&$to&&$line=~m/^\@{2} /) {2468$line= format_unidiff_chunk_header($line,$from,$to);2469}elsif($from&&$to&&$line=~m/^\@{3}/) {2470$line= format_cc_diff_chunk_header($line,$from,$to);2471}else{2472$line= esc_html($line, -nbsp=>1);2473}2474}24752476my$diff_classes="diff";2477$diff_classes.="$diff_class"if($diff_class);2478$line="<div class=\"$diff_classes\">$line</div>\n";24792480return$line;2481}24822483# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2484# linked. Pass the hash of the tree/commit to snapshot.2485sub format_snapshot_links {2486my($hash) =@_;2487my$num_fmts=@snapshot_fmts;2488if($num_fmts>1) {2489# A parenthesized list of links bearing format names.2490# e.g. "snapshot (_tar.gz_ _zip_)"2491return"snapshot (".join(' ',map2492$cgi->a({2493-href => href(2494 action=>"snapshot",2495 hash=>$hash,2496 snapshot_format=>$_2497)2498},$known_snapshot_formats{$_}{'display'})2499,@snapshot_fmts) .")";2500}elsif($num_fmts==1) {2501# A single "snapshot" link whose tooltip bears the format name.2502# i.e. "_snapshot_"2503my($fmt) =@snapshot_fmts;2504return2505$cgi->a({2506-href => href(2507 action=>"snapshot",2508 hash=>$hash,2509 snapshot_format=>$fmt2510),2511-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2512},"snapshot");2513}else{# $num_fmts == 02514returnundef;2515}2516}25172518## ......................................................................2519## functions returning values to be passed, perhaps after some2520## transformation, to other functions; e.g. returning arguments to href()25212522# returns hash to be passed to href to generate gitweb URL2523# in -title key it returns description of link2524sub get_feed_info {2525my$format=shift||'Atom';2526my%res= (action =>lc($format));25272528# feed links are possible only for project views2529return unless(defined$project);2530# some views should link to OPML, or to generic project feed,2531# or don't have specific feed yet (so they should use generic)2532return if(!$action||$action=~/^(?:tags|heads|forks|tag|search)$/x);25332534my$branch;2535# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2536# from tag links; this also makes possible to detect branch links2537if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2538(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2539$branch=$1;2540}2541# find log type for feed description (title)2542my$type='log';2543if(defined$file_name) {2544$type="history of$file_name";2545$type.="/"if($actioneq'tree');2546$type.=" on '$branch'"if(defined$branch);2547}else{2548$type="log of$branch"if(defined$branch);2549}25502551$res{-title} =$type;2552$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2553$res{'file_name'} =$file_name;25542555return%res;2556}25572558## ----------------------------------------------------------------------2559## git utility subroutines, invoking git commands25602561# returns path to the core git executable and the --git-dir parameter as list2562sub git_cmd {2563$number_of_git_cmds++;2564return$GIT,'--git-dir='.$git_dir;2565}25662567# quote the given arguments for passing them to the shell2568# quote_command("command", "arg 1", "arg with ' and ! characters")2569# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2570# Try to avoid using this function wherever possible.2571sub quote_command {2572returnjoin(' ',2573map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2574}25752576# get HEAD ref of given project as hash2577sub git_get_head_hash {2578return git_get_full_hash(shift,'HEAD');2579}25802581sub git_get_full_hash {2582return git_get_hash(@_);2583}25842585sub git_get_short_hash {2586return git_get_hash(@_,'--short=7');2587}25882589sub git_get_hash {2590my($project,$hash,@options) =@_;2591my$o_git_dir=$git_dir;2592my$retval=undef;2593$git_dir="$projectroot/$project";2594if(open my$fd,'-|', git_cmd(),'rev-parse',2595'--verify','-q',@options,$hash) {2596$retval= <$fd>;2597chomp$retvalifdefined$retval;2598close$fd;2599}2600if(defined$o_git_dir) {2601$git_dir=$o_git_dir;2602}2603return$retval;2604}26052606# get type of given object2607sub git_get_type {2608my$hash=shift;26092610open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2611my$type= <$fd>;2612close$fdorreturn;2613chomp$type;2614return$type;2615}26162617# repository configuration2618our$config_file='';2619our%config;26202621# store multiple values for single key as anonymous array reference2622# single values stored directly in the hash, not as [ <value> ]2623sub hash_set_multi {2624my($hash,$key,$value) =@_;26252626if(!exists$hash->{$key}) {2627$hash->{$key} =$value;2628}elsif(!ref$hash->{$key}) {2629$hash->{$key} = [$hash->{$key},$value];2630}else{2631push@{$hash->{$key}},$value;2632}2633}26342635# return hash of git project configuration2636# optionally limited to some section, e.g. 'gitweb'2637sub git_parse_project_config {2638my$section_regexp=shift;2639my%config;26402641local$/="\0";26422643open my$fh,"-|", git_cmd(),"config",'-z','-l',2644orreturn;26452646while(my$keyval= <$fh>) {2647chomp$keyval;2648my($key,$value) =split(/\n/,$keyval,2);26492650 hash_set_multi(\%config,$key,$value)2651if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2652}2653close$fh;26542655return%config;2656}26572658# convert config value to boolean: 'true' or 'false'2659# no value, number > 0, 'true' and 'yes' values are true2660# rest of values are treated as false (never as error)2661sub config_to_bool {2662my$val=shift;26632664return1if!defined$val;# section.key26652666# strip leading and trailing whitespace2667$val=~s/^\s+//;2668$val=~s/\s+$//;26692670return(($val=~/^\d+$/&&$val) ||# section.key = 12671($val=~/^(?:true|yes)$/i));# section.key = true2672}26732674# convert config value to simple decimal number2675# an optional value suffix of 'k', 'm', or 'g' will cause the value2676# to be multiplied by 1024, 1048576, or 10737418242677sub config_to_int {2678my$val=shift;26792680# strip leading and trailing whitespace2681$val=~s/^\s+//;2682$val=~s/\s+$//;26832684if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2685$unit=lc($unit);2686# unknown unit is treated as 12687return$num* ($uniteq'g'?1073741824:2688$uniteq'm'?1048576:2689$uniteq'k'?1024:1);2690}2691return$val;2692}26932694# convert config value to array reference, if needed2695sub config_to_multi {2696my$val=shift;26972698returnref($val) ?$val: (defined($val) ? [$val] : []);2699}27002701sub git_get_project_config {2702my($key,$type) =@_;27032704return unlessdefined$git_dir;27052706# key sanity check2707return unless($key);2708# only subsection, if exists, is case sensitive,2709# and not lowercased by 'git config -z -l'2710if(my($hi,$mi,$lo) = ($key=~/^([^.]*)\.(.*)\.([^.]*)$/)) {2711$lo=~s/_//g;2712$key=join(".",lc($hi),$mi,lc($lo));2713return if($lo=~/\W/||$hi=~/\W/);2714}else{2715$key=lc($key);2716$key=~s/_//g;2717return if($key=~/\W/);2718}2719$key=~s/^gitweb\.//;27202721# type sanity check2722if(defined$type) {2723$type=~s/^--//;2724$type=undef2725unless($typeeq'bool'||$typeeq'int');2726}27272728# get config2729if(!defined$config_file||2730$config_filene"$git_dir/config") {2731%config= git_parse_project_config('gitweb');2732$config_file="$git_dir/config";2733}27342735# check if config variable (key) exists2736return unlessexists$config{"gitweb.$key"};27372738# ensure given type2739if(!defined$type) {2740return$config{"gitweb.$key"};2741}elsif($typeeq'bool') {2742# backward compatibility: 'git config --bool' returns true/false2743return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2744}elsif($typeeq'int') {2745return config_to_int($config{"gitweb.$key"});2746}2747return$config{"gitweb.$key"};2748}27492750# get hash of given path at given ref2751sub git_get_hash_by_path {2752my$base=shift;2753my$path=shift||returnundef;2754my$type=shift;27552756$path=~ s,/+$,,;27572758open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2759or die_error(500,"Open git-ls-tree failed");2760my$line= <$fd>;2761close$fdorreturnundef;27622763if(!defined$line) {2764# there is no tree or hash given by $path at $base2765returnundef;2766}27672768#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2769$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2770if(defined$type&&$typene$2) {2771# type doesn't match2772returnundef;2773}2774return$3;2775}27762777# get path of entry with given hash at given tree-ish (ref)2778# used to get 'from' filename for combined diff (merge commit) for renames2779sub git_get_path_by_hash {2780my$base=shift||return;2781my$hash=shift||return;27822783local$/="\0";27842785open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2786orreturnundef;2787while(my$line= <$fd>) {2788chomp$line;27892790#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2791#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2792if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2793close$fd;2794return$1;2795}2796}2797close$fd;2798returnundef;2799}28002801## ......................................................................2802## git utility functions, directly accessing git repository28032804# get the value of config variable either from file named as the variable2805# itself in the repository ($GIT_DIR/$name file), or from gitweb.$name2806# configuration variable in the repository config file.2807sub git_get_file_or_project_config {2808my($path,$name) =@_;28092810$git_dir="$projectroot/$path";2811open my$fd,'<',"$git_dir/$name"2812orreturn git_get_project_config($name);2813my$conf= <$fd>;2814close$fd;2815if(defined$conf) {2816chomp$conf;2817}2818return$conf;2819}28202821sub git_get_project_description {2822my$path=shift;2823return git_get_file_or_project_config($path,'description');2824}28252826sub git_get_project_category {2827my$path=shift;2828return git_get_file_or_project_config($path,'category');2829}283028312832# supported formats:2833# * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)2834# - if its contents is a number, use it as tag weight,2835# - otherwise add a tag with weight 12836# * $GIT_DIR/ctags file, each line is a tag (with weight 1)2837# the same value multiple times increases tag weight2838# * `gitweb.ctag' multi-valued repo config variable2839sub git_get_project_ctags {2840my$project=shift;2841my$ctags= {};28422843$git_dir="$projectroot/$project";2844if(opendir my$dh,"$git_dir/ctags") {2845my@files=grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh);2846foreachmy$tagfile(@files) {2847open my$ct,'<',$tagfile2848ornext;2849my$val= <$ct>;2850chomp$valif$val;2851close$ct;28522853(my$ctag=$tagfile) =~ s#.*/##;2854if($val=~/^\d+$/) {2855$ctags->{$ctag} =$val;2856}else{2857$ctags->{$ctag} =1;2858}2859}2860closedir$dh;28612862}elsif(open my$fh,'<',"$git_dir/ctags") {2863while(my$line= <$fh>) {2864chomp$line;2865$ctags->{$line}++if$line;2866}2867close$fh;28682869}else{2870my$taglist= config_to_multi(git_get_project_config('ctag'));2871foreachmy$tag(@$taglist) {2872$ctags->{$tag}++;2873}2874}28752876return$ctags;2877}28782879# return hash, where keys are content tags ('ctags'),2880# and values are sum of weights of given tag in every project2881sub git_gather_all_ctags {2882my$projects=shift;2883my$ctags= {};28842885foreachmy$p(@$projects) {2886foreachmy$ct(keys%{$p->{'ctags'}}) {2887$ctags->{$ct} +=$p->{'ctags'}->{$ct};2888}2889}28902891return$ctags;2892}28932894sub git_populate_project_tagcloud {2895my$ctags=shift;28962897# First, merge different-cased tags; tags vote on casing2898my%ctags_lc;2899foreach(keys%$ctags) {2900$ctags_lc{lc$_}->{count} +=$ctags->{$_};2901if(not$ctags_lc{lc$_}->{topcount}2902or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2903$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2904$ctags_lc{lc$_}->{topname} =$_;2905}2906}29072908my$cloud;2909my$matched=$input_params{'ctag'};2910if(eval{require HTML::TagCloud;1; }) {2911$cloud= HTML::TagCloud->new;2912foreachmy$ctag(sort keys%ctags_lc) {2913# Pad the title with spaces so that the cloud looks2914# less crammed.2915my$title= esc_html($ctags_lc{$ctag}->{topname});2916$title=~s/ / /g;2917$title=~s/^/ /g;2918$title=~s/$/ /g;2919if(defined$matched&&$matchedeq$ctag) {2920$title=qq(<span class="match">$title</span>);2921}2922$cloud->add($title, href(project=>undef, ctag=>$ctag),2923$ctags_lc{$ctag}->{count});2924}2925}else{2926$cloud= {};2927foreachmy$ctag(keys%ctags_lc) {2928my$title= esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);2929if(defined$matched&&$matchedeq$ctag) {2930$title=qq(<span class="match">$title</span>);2931}2932$cloud->{$ctag}{count} =$ctags_lc{$ctag}->{count};2933$cloud->{$ctag}{ctag} =2934$cgi->a({-href=>href(project=>undef, ctag=>$ctag)},$title);2935}2936}2937return$cloud;2938}29392940sub git_show_project_tagcloud {2941my($cloud,$count) =@_;2942if(ref$cloudeq'HTML::TagCloud') {2943return$cloud->html_and_css($count);2944}else{2945my@tags=sort{$cloud->{$a}->{'count'} <=>$cloud->{$b}->{'count'} }keys%$cloud;2946return2947'<div id="htmltagcloud"'.($project?'':' align="center"').'>'.2948join(', ',map{2949$cloud->{$_}->{'ctag'}2950}splice(@tags,0,$count)) .2951'</div>';2952}2953}29542955sub git_get_project_url_list {2956my$path=shift;29572958$git_dir="$projectroot/$path";2959open my$fd,'<',"$git_dir/cloneurl"2960orreturnwantarray?2961@{ config_to_multi(git_get_project_config('url')) } :2962 config_to_multi(git_get_project_config('url'));2963my@git_project_url_list=map{chomp;$_} <$fd>;2964close$fd;29652966returnwantarray?@git_project_url_list: \@git_project_url_list;2967}29682969sub git_get_projects_list {2970my$filter=shift||'';2971my$paranoid=shift;2972my@list;29732974if(-d $projects_list) {2975# search in directory2976my$dir=$projects_list;2977# remove the trailing "/"2978$dir=~s!/+$!!;2979my$pfxlen=length("$dir");2980my$pfxdepth= ($dir=~tr!/!!);2981# when filtering, search only given subdirectory2982if($filter&& !$paranoid) {2983$dir.="/$filter";2984$dir=~s!/+$!!;2985}29862987 File::Find::find({2988 follow_fast =>1,# follow symbolic links2989 follow_skip =>2,# ignore duplicates2990 dangling_symlinks =>0,# ignore dangling symlinks, silently2991 wanted =>sub{2992# global variables2993our$project_maxdepth;2994our$projectroot;2995# skip project-list toplevel, if we get it.2996return if(m!^[/.]$!);2997# only directories can be git repositories2998return unless(-d $_);2999# don't traverse too deep (Find is super slow on os x)3000# $project_maxdepth excludes depth of $projectroot3001if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {3002$File::Find::prune =1;3003return;3004}30053006my$path=substr($File::Find::name,$pfxlen+1);3007# paranoidly only filter here3008if($paranoid&&$filter&&$path!~m!^\Q$filter\E/!) {3009next;3010}3011# we check related file in $projectroot3012if(check_export_ok("$projectroot/$path")) {3013push@list, { path =>$path};3014$File::Find::prune =1;3015}3016},3017},"$dir");30183019}elsif(-f $projects_list) {3020# read from file(url-encoded):3021# 'git%2Fgit.git Linus+Torvalds'3022# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'3023# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'3024open my$fd,'<',$projects_listorreturn;3025 PROJECT:3026while(my$line= <$fd>) {3027chomp$line;3028my($path,$owner) =split' ',$line;3029$path= unescape($path);3030$owner= unescape($owner);3031if(!defined$path) {3032next;3033}3034# if $filter is rpovided, check if $path begins with $filter3035if($filter&&$path!~m!^\Q$filter\E/!) {3036next;3037}3038if(check_export_ok("$projectroot/$path")) {3039my$pr= {3040 path =>$path3041};3042if($owner) {3043$pr->{'owner'} = to_utf8($owner);3044}3045push@list,$pr;3046}3047}3048close$fd;3049}3050return@list;3051}30523053# written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)3054# as side effects it sets 'forks' field to list of forks for forked projects3055sub filter_forks_from_projects_list {3056my$projects=shift;30573058my%trie;# prefix tree of directories (path components)3059# generate trie out of those directories that might contain forks3060foreachmy$pr(@$projects) {3061my$path=$pr->{'path'};3062$path=~s/\.git$//;# forks of 'repo.git' are in 'repo/' directory3063next if($path=~m!/$!);# skip non-bare repositories, e.g. 'repo/.git'3064next unless($path);# skip '.git' repository: tests, git-instaweb3065next unless(-d "$projectroot/$path");# containing directory exists3066$pr->{'forks'} = [];# there can be 0 or more forks of project30673068# add to trie3069my@dirs=split('/',$path);3070# walk the trie, until either runs out of components or out of trie3071my$ref= \%trie;3072while(scalar@dirs&&3073exists($ref->{$dirs[0]})) {3074$ref=$ref->{shift@dirs};3075}3076# create rest of trie structure from rest of components3077foreachmy$dir(@dirs) {3078$ref=$ref->{$dir} = {};3079}3080# create end marker, store $pr as a data3081$ref->{''} =$prif(!exists$ref->{''});3082}30833084# filter out forks, by finding shortest prefix match for paths3085my@filtered;3086 PROJECT:3087foreachmy$pr(@$projects) {3088# trie lookup3089my$ref= \%trie;3090 DIR:3091foreachmy$dir(split('/',$pr->{'path'})) {3092if(exists$ref->{''}) {3093# found [shortest] prefix, is a fork - skip it3094push@{$ref->{''}{'forks'}},$pr;3095next PROJECT;3096}3097if(!exists$ref->{$dir}) {3098# not in trie, cannot have prefix, not a fork3099push@filtered,$pr;3100next PROJECT;3101}3102# If the dir is there, we just walk one step down the trie.3103$ref=$ref->{$dir};3104}3105# we ran out of trie3106# (shouldn't happen: it's either no match, or end marker)3107push@filtered,$pr;3108}31093110return@filtered;3111}31123113# note: fill_project_list_info must be run first,3114# for 'descr_long' and 'ctags' to be filled3115sub search_projects_list {3116my($projlist,%opts) =@_;3117my$tagfilter=$opts{'tagfilter'};3118my$search_re=$opts{'search_regexp'};31193120return@$projlist3121unless($tagfilter||$search_re);31223123# searching projects require filling to be run before it;3124 fill_project_list_info($projlist,3125$tagfilter?'ctags': (),3126$search_re? ('path','descr') : ());3127my@projects;3128 PROJECT:3129foreachmy$pr(@$projlist) {31303131if($tagfilter) {3132next unlessref($pr->{'ctags'})eq'HASH';3133next unless3134grep{lc($_)eq lc($tagfilter) }keys%{$pr->{'ctags'}};3135}31363137if($search_re) {3138next unless3139$pr->{'path'} =~/$search_re/||3140$pr->{'descr_long'} =~/$search_re/;3141}31423143push@projects,$pr;3144}31453146return@projects;3147}31483149our$gitweb_project_owner=undef;3150sub git_get_project_list_from_file {31513152return if(defined$gitweb_project_owner);31533154$gitweb_project_owner= {};3155# read from file (url-encoded):3156# 'git%2Fgit.git Linus+Torvalds'3157# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'3158# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'3159if(-f $projects_list) {3160open(my$fd,'<',$projects_list);3161while(my$line= <$fd>) {3162chomp$line;3163my($pr,$ow) =split' ',$line;3164$pr= unescape($pr);3165$ow= unescape($ow);3166$gitweb_project_owner->{$pr} = to_utf8($ow);3167}3168close$fd;3169}3170}31713172sub git_get_project_owner {3173my$project=shift;3174my$owner;31753176returnundefunless$project;3177$git_dir="$projectroot/$project";31783179if(!defined$gitweb_project_owner) {3180 git_get_project_list_from_file();3181}31823183if(exists$gitweb_project_owner->{$project}) {3184$owner=$gitweb_project_owner->{$project};3185}3186if(!defined$owner){3187$owner= git_get_project_config('owner');3188}3189if(!defined$owner) {3190$owner= get_file_owner("$git_dir");3191}31923193return$owner;3194}31953196sub git_get_last_activity {3197my($path) =@_;3198my$fd;31993200$git_dir="$projectroot/$path";3201open($fd,"-|", git_cmd(),'for-each-ref',3202'--format=%(committer)',3203'--sort=-committerdate',3204'--count=1',3205'refs/heads')orreturn;3206my$most_recent= <$fd>;3207close$fdorreturn;3208if(defined$most_recent&&3209$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {3210my$timestamp=$1;3211my$age=time-$timestamp;3212return($age, age_string($age));3213}3214return(undef,undef);3215}32163217# Implementation note: when a single remote is wanted, we cannot use 'git3218# remote show -n' because that command always work (assuming it's a remote URL3219# if it's not defined), and we cannot use 'git remote show' because that would3220# try to make a network roundtrip. So the only way to find if that particular3221# remote is defined is to walk the list provided by 'git remote -v' and stop if3222# and when we find what we want.3223sub git_get_remotes_list {3224my$wanted=shift;3225my%remotes= ();32263227open my$fd,'-|', git_cmd(),'remote','-v';3228return unless$fd;3229while(my$remote= <$fd>) {3230chomp$remote;3231$remote=~s!\t(.*?)\s+\((\w+)\)$!!;3232next if$wantedand not$remoteeq$wanted;3233my($url,$key) = ($1,$2);32343235$remotes{$remote} ||= {'heads'=> () };3236$remotes{$remote}{$key} =$url;3237}3238close$fdorreturn;3239returnwantarray?%remotes: \%remotes;3240}32413242# Takes a hash of remotes as first parameter and fills it by adding the3243# available remote heads for each of the indicated remotes.3244sub fill_remote_heads {3245my$remotes=shift;3246my@heads=map{"remotes/$_"}keys%$remotes;3247my@remoteheads= git_get_heads_list(undef,@heads);3248foreachmy$remote(keys%$remotes) {3249$remotes->{$remote}{'heads'} = [grep{3250$_->{'name'} =~s!^$remote/!!3251}@remoteheads];3252}3253}32543255sub git_get_references {3256my$type=shift||"";3257my%refs;3258# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.113259# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}3260open my$fd,"-|", git_cmd(),"show-ref","--dereference",3261($type? ("--","refs/$type") : ())# use -- <pattern> if $type3262orreturn;32633264while(my$line= <$fd>) {3265chomp$line;3266if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {3267if(defined$refs{$1}) {3268push@{$refs{$1}},$2;3269}else{3270$refs{$1} = [$2];3271}3272}3273}3274close$fdorreturn;3275return \%refs;3276}32773278sub git_get_rev_name_tags {3279my$hash=shift||returnundef;32803281open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash3282orreturn;3283my$name_rev= <$fd>;3284close$fd;32853286if($name_rev=~ m|^$hash tags/(.*)$|) {3287return$1;3288}else{3289# catches also '$hash undefined' output3290returnundef;3291}3292}32933294## ----------------------------------------------------------------------3295## parse to hash functions32963297sub parse_date {3298my$epoch=shift;3299my$tz=shift||"-0000";33003301my%date;3302my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");3303my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");3304my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);3305$date{'hour'} =$hour;3306$date{'minute'} =$min;3307$date{'mday'} =$mday;3308$date{'day'} =$days[$wday];3309$date{'month'} =$months[$mon];3310$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",3311$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;3312$date{'mday-time'} =sprintf"%d%s%02d:%02d",3313$mday,$months[$mon],$hour,$min;3314$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",33151900+$year,1+$mon,$mday,$hour,$min,$sec;33163317my($tz_sign,$tz_hour,$tz_min) =3318($tz=~m/^([-+])(\d\d)(\d\d)$/);3319$tz_sign= ($tz_signeq'-'? -1: +1);3320my$local=$epoch+$tz_sign*((($tz_hour*60) +$tz_min)*60);3321($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);3322$date{'hour_local'} =$hour;3323$date{'minute_local'} =$min;3324$date{'tz_local'} =$tz;3325$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",33261900+$year,$mon+1,$mday,3327$hour,$min,$sec,$tz);3328return%date;3329}33303331sub parse_tag {3332my$tag_id=shift;3333my%tag;3334my@comment;33353336open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;3337$tag{'id'} =$tag_id;3338while(my$line= <$fd>) {3339chomp$line;3340if($line=~m/^object ([0-9a-fA-F]{40})$/) {3341$tag{'object'} =$1;3342}elsif($line=~m/^type (.+)$/) {3343$tag{'type'} =$1;3344}elsif($line=~m/^tag (.+)$/) {3345$tag{'name'} =$1;3346}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {3347$tag{'author'} =$1;3348$tag{'author_epoch'} =$2;3349$tag{'author_tz'} =$3;3350if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {3351$tag{'author_name'} =$1;3352$tag{'author_email'} =$2;3353}else{3354$tag{'author_name'} =$tag{'author'};3355}3356}elsif($line=~m/--BEGIN/) {3357push@comment,$line;3358last;3359}elsif($lineeq"") {3360last;3361}3362}3363push@comment, <$fd>;3364$tag{'comment'} = \@comment;3365close$fdorreturn;3366if(!defined$tag{'name'}) {3367return3368};3369return%tag3370}33713372sub parse_commit_text {3373my($commit_text,$withparents) =@_;3374my@commit_lines=split'\n',$commit_text;3375my%co;33763377pop@commit_lines;# Remove '\0'33783379if(!@commit_lines) {3380return;3381}33823383my$header=shift@commit_lines;3384if($header!~m/^[0-9a-fA-F]{40}/) {3385return;3386}3387($co{'id'},my@parents) =split' ',$header;3388while(my$line=shift@commit_lines) {3389last if$lineeq"\n";3390if($line=~m/^tree ([0-9a-fA-F]{40})$/) {3391$co{'tree'} =$1;3392}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {3393push@parents,$1;3394}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {3395$co{'author'} = to_utf8($1);3396$co{'author_epoch'} =$2;3397$co{'author_tz'} =$3;3398if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {3399$co{'author_name'} =$1;3400$co{'author_email'} =$2;3401}else{3402$co{'author_name'} =$co{'author'};3403}3404}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {3405$co{'committer'} = to_utf8($1);3406$co{'committer_epoch'} =$2;3407$co{'committer_tz'} =$3;3408if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {3409$co{'committer_name'} =$1;3410$co{'committer_email'} =$2;3411}else{3412$co{'committer_name'} =$co{'committer'};3413}3414}3415}3416if(!defined$co{'tree'}) {3417return;3418};3419$co{'parents'} = \@parents;3420$co{'parent'} =$parents[0];34213422foreachmy$title(@commit_lines) {3423$title=~s/^ //;3424if($titlene"") {3425$co{'title'} = chop_str($title,80,5);3426# remove leading stuff of merges to make the interesting part visible3427if(length($title) >50) {3428$title=~s/^Automatic //;3429$title=~s/^merge (of|with) /Merge ... /i;3430if(length($title) >50) {3431$title=~s/(http|rsync):\/\///;3432}3433if(length($title) >50) {3434$title=~s/(master|www|rsync)\.//;3435}3436if(length($title) >50) {3437$title=~s/kernel.org:?//;3438}3439if(length($title) >50) {3440$title=~s/\/pub\/scm//;3441}3442}3443$co{'title_short'} = chop_str($title,50,5);3444last;3445}3446}3447if(!defined$co{'title'} ||$co{'title'}eq"") {3448$co{'title'} =$co{'title_short'} ='(no commit message)';3449}3450# remove added spaces3451foreachmy$line(@commit_lines) {3452$line=~s/^ //;3453}3454$co{'comment'} = \@commit_lines;34553456my$age=time-$co{'committer_epoch'};3457$co{'age'} =$age;3458$co{'age_string'} = age_string($age);3459my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});3460if($age>60*60*24*7*2) {3461$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3462$co{'age_string_age'} =$co{'age_string'};3463}else{3464$co{'age_string_date'} =$co{'age_string'};3465$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3466}3467return%co;3468}34693470sub parse_commit {3471my($commit_id) =@_;3472my%co;34733474local$/="\0";34753476open my$fd,"-|", git_cmd(),"rev-list",3477"--parents",3478"--header",3479"--max-count=1",3480$commit_id,3481"--",3482or die_error(500,"Open git-rev-list failed");3483%co= parse_commit_text(<$fd>,1);3484close$fd;34853486return%co;3487}34883489sub parse_commits {3490my($commit_id,$maxcount,$skip,$filename,@args) =@_;3491my@cos;34923493$maxcount||=1;3494$skip||=0;34953496local$/="\0";34973498open my$fd,"-|", git_cmd(),"rev-list",3499"--header",3500@args,3501("--max-count=".$maxcount),3502("--skip=".$skip),3503@extra_options,3504$commit_id,3505"--",3506($filename? ($filename) : ())3507or die_error(500,"Open git-rev-list failed");3508while(my$line= <$fd>) {3509my%co= parse_commit_text($line);3510push@cos, \%co;3511}3512close$fd;35133514returnwantarray?@cos: \@cos;3515}35163517# parse line of git-diff-tree "raw" output3518sub parse_difftree_raw_line {3519my$line=shift;3520my%res;35213522# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'3523# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'3524if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {3525$res{'from_mode'} =$1;3526$res{'to_mode'} =$2;3527$res{'from_id'} =$3;3528$res{'to_id'} =$4;3529$res{'status'} =$5;3530$res{'similarity'} =$6;3531if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3532($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3533}else{3534$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3535}3536}3537# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3538# combined diff (for merge commit)3539elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3540$res{'nparents'} =length($1);3541$res{'from_mode'} = [split(' ',$2) ];3542$res{'to_mode'} =pop@{$res{'from_mode'}};3543$res{'from_id'} = [split(' ',$3) ];3544$res{'to_id'} =pop@{$res{'from_id'}};3545$res{'status'} = [split('',$4) ];3546$res{'to_file'} = unquote($5);3547}3548# 'c512b523472485aef4fff9e57b229d9d243c967f'3549elsif($line=~m/^([0-9a-fA-F]{40})$/) {3550$res{'commit'} =$1;3551}35523553returnwantarray?%res: \%res;3554}35553556# wrapper: return parsed line of git-diff-tree "raw" output3557# (the argument might be raw line, or parsed info)3558sub parsed_difftree_line {3559my$line_or_ref=shift;35603561if(ref($line_or_ref)eq"HASH") {3562# pre-parsed (or generated by hand)3563return$line_or_ref;3564}else{3565return parse_difftree_raw_line($line_or_ref);3566}3567}35683569# parse line of git-ls-tree output3570sub parse_ls_tree_line {3571my$line=shift;3572my%opts=@_;3573my%res;35743575if($opts{'-l'}) {3576#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3577$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;35783579$res{'mode'} =$1;3580$res{'type'} =$2;3581$res{'hash'} =$3;3582$res{'size'} =$4;3583if($opts{'-z'}) {3584$res{'name'} =$5;3585}else{3586$res{'name'} = unquote($5);3587}3588}else{3589#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3590$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;35913592$res{'mode'} =$1;3593$res{'type'} =$2;3594$res{'hash'} =$3;3595if($opts{'-z'}) {3596$res{'name'} =$4;3597}else{3598$res{'name'} = unquote($4);3599}3600}36013602returnwantarray?%res: \%res;3603}36043605# generates _two_ hashes, references to which are passed as 2 and 3 argument3606sub parse_from_to_diffinfo {3607my($diffinfo,$from,$to,@parents) =@_;36083609if($diffinfo->{'nparents'}) {3610# combined diff3611$from->{'file'} = [];3612$from->{'href'} = [];3613 fill_from_file_info($diffinfo,@parents)3614unlessexists$diffinfo->{'from_file'};3615for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3616$from->{'file'}[$i] =3617defined$diffinfo->{'from_file'}[$i] ?3618$diffinfo->{'from_file'}[$i] :3619$diffinfo->{'to_file'};3620if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3621$from->{'href'}[$i] = href(action=>"blob",3622 hash_base=>$parents[$i],3623 hash=>$diffinfo->{'from_id'}[$i],3624 file_name=>$from->{'file'}[$i]);3625}else{3626$from->{'href'}[$i] =undef;3627}3628}3629}else{3630# ordinary (not combined) diff3631$from->{'file'} =$diffinfo->{'from_file'};3632if($diffinfo->{'status'}ne"A") {# not new (added) file3633$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3634 hash=>$diffinfo->{'from_id'},3635 file_name=>$from->{'file'});3636}else{3637delete$from->{'href'};3638}3639}36403641$to->{'file'} =$diffinfo->{'to_file'};3642if(!is_deleted($diffinfo)) {# file exists in result3643$to->{'href'} = href(action=>"blob", hash_base=>$hash,3644 hash=>$diffinfo->{'to_id'},3645 file_name=>$to->{'file'});3646}else{3647delete$to->{'href'};3648}3649}36503651## ......................................................................3652## parse to array of hashes functions36533654sub git_get_heads_list {3655my($limit,@classes) =@_;3656@classes= ('heads')unless@classes;3657my@patterns=map{"refs/$_"}@classes;3658my@headslist;36593660open my$fd,'-|', git_cmd(),'for-each-ref',3661($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3662'--format=%(objectname) %(refname) %(subject)%00%(committer)',3663@patterns3664orreturn;3665while(my$line= <$fd>) {3666my%ref_item;36673668chomp$line;3669my($refinfo,$committerinfo) =split(/\0/,$line);3670my($hash,$name,$title) =split(' ',$refinfo,3);3671my($committer,$epoch,$tz) =3672($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3673$ref_item{'fullname'} =$name;3674$name=~s!^refs/(?:head|remote)s/!!;36753676$ref_item{'name'} =$name;3677$ref_item{'id'} =$hash;3678$ref_item{'title'} =$title||'(no commit message)';3679$ref_item{'epoch'} =$epoch;3680if($epoch) {3681$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3682}else{3683$ref_item{'age'} ="unknown";3684}36853686push@headslist, \%ref_item;3687}3688close$fd;36893690returnwantarray?@headslist: \@headslist;3691}36923693sub git_get_tags_list {3694my$limit=shift;3695my@tagslist;36963697open my$fd,'-|', git_cmd(),'for-each-ref',3698($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3699'--format=%(objectname) %(objecttype) %(refname) '.3700'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3701'refs/tags'3702orreturn;3703while(my$line= <$fd>) {3704my%ref_item;37053706chomp$line;3707my($refinfo,$creatorinfo) =split(/\0/,$line);3708my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3709my($creator,$epoch,$tz) =3710($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3711$ref_item{'fullname'} =$name;3712$name=~s!^refs/tags/!!;37133714$ref_item{'type'} =$type;3715$ref_item{'id'} =$id;3716$ref_item{'name'} =$name;3717if($typeeq"tag") {3718$ref_item{'subject'} =$title;3719$ref_item{'reftype'} =$reftype;3720$ref_item{'refid'} =$refid;3721}else{3722$ref_item{'reftype'} =$type;3723$ref_item{'refid'} =$id;3724}37253726if($typeeq"tag"||$typeeq"commit") {3727$ref_item{'epoch'} =$epoch;3728if($epoch) {3729$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3730}else{3731$ref_item{'age'} ="unknown";3732}3733}37343735push@tagslist, \%ref_item;3736}3737close$fd;37383739returnwantarray?@tagslist: \@tagslist;3740}37413742## ----------------------------------------------------------------------3743## filesystem-related functions37443745sub get_file_owner {3746my$path=shift;37473748my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3749my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3750if(!defined$gcos) {3751returnundef;3752}3753my$owner=$gcos;3754$owner=~s/[,;].*$//;3755return to_utf8($owner);3756}37573758# assume that file exists3759sub insert_file {3760my$filename=shift;37613762open my$fd,'<',$filename;3763print map{ to_utf8($_) } <$fd>;3764close$fd;3765}37663767## ......................................................................3768## mimetype related functions37693770sub mimetype_guess_file {3771my$filename=shift;3772my$mimemap=shift;3773-r $mimemaporreturnundef;37743775my%mimemap;3776open(my$mh,'<',$mimemap)orreturnundef;3777while(<$mh>) {3778next ifm/^#/;# skip comments3779my($mimetype,@exts) =split(/\s+/);3780foreachmy$ext(@exts) {3781$mimemap{$ext} =$mimetype;3782}3783}3784close($mh);37853786$filename=~/\.([^.]*)$/;3787return$mimemap{$1};3788}37893790sub mimetype_guess {3791my$filename=shift;3792my$mime;3793$filename=~/\./orreturnundef;37943795if($mimetypes_file) {3796my$file=$mimetypes_file;3797if($file!~m!^/!) {# if it is relative path3798# it is relative to project3799$file="$projectroot/$project/$file";3800}3801$mime= mimetype_guess_file($filename,$file);3802}3803$mime||= mimetype_guess_file($filename,'/etc/mime.types');3804return$mime;3805}38063807sub blob_mimetype {3808my$fd=shift;3809my$filename=shift;38103811if($filename) {3812my$mime= mimetype_guess($filename);3813$mimeandreturn$mime;3814}38153816# just in case3817return$default_blob_plain_mimetypeunless$fd;38183819if(-T $fd) {3820return'text/plain';3821}elsif(!$filename) {3822return'application/octet-stream';3823}elsif($filename=~m/\.png$/i) {3824return'image/png';3825}elsif($filename=~m/\.gif$/i) {3826return'image/gif';3827}elsif($filename=~m/\.jpe?g$/i) {3828return'image/jpeg';3829}else{3830return'application/octet-stream';3831}3832}38333834sub blob_contenttype {3835my($fd,$file_name,$type) =@_;38363837$type||= blob_mimetype($fd,$file_name);3838if($typeeq'text/plain'&&defined$default_text_plain_charset) {3839$type.="; charset=$default_text_plain_charset";3840}38413842return$type;3843}38443845# guess file syntax for syntax highlighting; return undef if no highlighting3846# the name of syntax can (in the future) depend on syntax highlighter used3847sub guess_file_syntax {3848my($highlight,$mimetype,$file_name) =@_;3849returnundefunless($highlight&&defined$file_name);3850my$basename= basename($file_name,'.in');3851return$highlight_basename{$basename}3852ifexists$highlight_basename{$basename};38533854$basename=~/\.([^.]*)$/;3855my$ext=$1orreturnundef;3856return$highlight_ext{$ext}3857ifexists$highlight_ext{$ext};38583859returnundef;3860}38613862# run highlighter and return FD of its output,3863# or return original FD if no highlighting3864sub run_highlighter {3865my($fd,$highlight,$syntax) =@_;3866return$fdunless($highlight&&defined$syntax);38673868close$fd;3869open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3870 quote_command($highlight_bin).3871" --replace-tabs=8 --fragment --syntax$syntax|"3872or die_error(500,"Couldn't open file or run syntax highlighter");3873return$fd;3874}38753876## ======================================================================3877## functions printing HTML: header, footer, error page38783879sub get_page_title {3880my$title= to_utf8($site_name);38813882unless(defined$project) {3883if(defined$project_filter) {3884$title.=" - projects in '". esc_path($project_filter) ."'";3885}3886return$title;3887}3888$title.=" - ". to_utf8($project);38893890return$titleunless(defined$action);3891$title.="/$action";# $action is US-ASCII (7bit ASCII)38923893return$titleunless(defined$file_name);3894$title.=" - ". esc_path($file_name);3895if($actioneq"tree"&&$file_name!~ m|/$|) {3896$title.="/";3897}38983899return$title;3900}39013902sub get_content_type_html {3903# require explicit support from the UA if we are to send the page as3904# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3905# we have to do this because MSIE sometimes globs '*/*', pretending to3906# support xhtml+xml but choking when it gets what it asked for.3907if(defined$cgi->http('HTTP_ACCEPT') &&3908$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3909$cgi->Accept('application/xhtml+xml') !=0) {3910return'application/xhtml+xml';3911}else{3912return'text/html';3913}3914}39153916sub print_feed_meta {3917if(defined$project) {3918my%href_params= get_feed_info();3919if(!exists$href_params{'-title'}) {3920$href_params{'-title'} ='log';3921}39223923foreachmy$format(qw(RSS Atom)) {3924my$type=lc($format);3925my%link_attr= (3926'-rel'=>'alternate',3927'-title'=> esc_attr("$project-$href_params{'-title'} -$formatfeed"),3928'-type'=>"application/$type+xml"3929);39303931$href_params{'extra_options'} =undef;3932$href_params{'action'} =$type;3933$link_attr{'-href'} = href(%href_params);3934print"<link ".3935"rel=\"$link_attr{'-rel'}\"".3936"title=\"$link_attr{'-title'}\"".3937"href=\"$link_attr{'-href'}\"".3938"type=\"$link_attr{'-type'}\"".3939"/>\n";39403941$href_params{'extra_options'} ='--no-merges';3942$link_attr{'-href'} = href(%href_params);3943$link_attr{'-title'} .=' (no merges)';3944print"<link ".3945"rel=\"$link_attr{'-rel'}\"".3946"title=\"$link_attr{'-title'}\"".3947"href=\"$link_attr{'-href'}\"".3948"type=\"$link_attr{'-type'}\"".3949"/>\n";3950}39513952}else{3953printf('<link rel="alternate" title="%sprojects list" '.3954'href="%s" type="text/plain; charset=utf-8" />'."\n",3955 esc_attr($site_name), href(project=>undef, action=>"project_index"));3956printf('<link rel="alternate" title="%sprojects feeds" '.3957'href="%s" type="text/x-opml" />'."\n",3958 esc_attr($site_name), href(project=>undef, action=>"opml"));3959}3960}39613962sub print_header_links {3963my$status=shift;39643965# print out each stylesheet that exist, providing backwards capability3966# for those people who defined $stylesheet in a config file3967if(defined$stylesheet) {3968print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3969}else{3970foreachmy$stylesheet(@stylesheets) {3971next unless$stylesheet;3972print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3973}3974}3975 print_feed_meta()3976if($statuseq'200 OK');3977if(defined$favicon) {3978printqq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);3979}3980}39813982sub print_nav_breadcrumbs_path {3983my$dirprefix=undef;3984while(my$part=shift) {3985$dirprefix.="/"ifdefined$dirprefix;3986$dirprefix.=$part;3987print$cgi->a({-href => href(project =>undef,3988 project_filter =>$dirprefix,3989 action =>"project_list")},3990 esc_html($part)) ." / ";3991}3992}39933994sub print_nav_breadcrumbs {3995my%opts=@_;39963997formy$crumb(@extra_breadcrumbs, [$home_link_str=>$home_link]) {3998print$cgi->a({-href => esc_url($crumb->[1])},$crumb->[0]) ." / ";3999}4000if(defined$project) {4001my@dirname=split'/',$project;4002my$projectbasename=pop@dirname;4003 print_nav_breadcrumbs_path(@dirname);4004print$cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));4005if(defined$action) {4006my$action_print=$action;4007if(defined$opts{-action_extra}) {4008$action_print=$cgi->a({-href => href(action=>$action)},4009$action);4010}4011print" /$action_print";4012}4013if(defined$opts{-action_extra}) {4014print" /$opts{-action_extra}";4015}4016print"\n";4017}elsif(defined$project_filter) {4018 print_nav_breadcrumbs_path(split'/',$project_filter);4019}4020}40214022sub print_search_form {4023if(!defined$searchtext) {4024$searchtext="";4025}4026my$search_hash;4027if(defined$hash_base) {4028$search_hash=$hash_base;4029}elsif(defined$hash) {4030$search_hash=$hash;4031}else{4032$search_hash="HEAD";4033}4034my$action=$my_uri;4035my$use_pathinfo= gitweb_check_feature('pathinfo');4036if($use_pathinfo) {4037$action.="/".esc_url($project);4038}4039print$cgi->startform(-method=>"get", -action =>$action) .4040"<div class=\"search\">\n".4041(!$use_pathinfo&&4042$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .4043$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".4044$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".4045$cgi->popup_menu(-name =>'st', -default=>'commit',4046-values=> ['commit','grep','author','committer','pickaxe']) .4047" ".$cgi->a({-href => href(action=>"search_help"),4048-title =>"search help"},"?") ." search:\n",4049$cgi->textfield(-name =>"s", -value =>$searchtext, -override =>1) ."\n".4050"<span title=\"Extended regular expression\">".4051$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',4052-checked =>$search_use_regexp) .4053"</span>".4054"</div>".4055$cgi->end_form() ."\n";4056}40574058sub git_header_html {4059my$status=shift||"200 OK";4060my$expires=shift;4061my%opts=@_;40624063my$title= get_page_title();4064my$content_type= get_content_type_html();4065print$cgi->header(-type=>$content_type, -charset =>'utf-8',4066-status=>$status, -expires =>$expires)4067unless($opts{'-no_http_header'});4068my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';4069print<<EOF;4070<?xml version="1.0" encoding="utf-8"?>4071<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4072<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">4073<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->4074<!-- git core binaries version$git_version-->4075<head>4076<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>4077<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>4078<meta name="robots" content="index, nofollow"/>4079<title>$title</title>4080EOF4081# the stylesheet, favicon etc urls won't work correctly with path_info4082# unless we set the appropriate base URL4083if($ENV{'PATH_INFO'}) {4084print"<base href=\"".esc_url($base_url)."\"/>\n";4085}4086 print_header_links($status);40874088if(defined$site_html_head_string) {4089print to_utf8($site_html_head_string);4090}40914092print"</head>\n".4093"<body>\n";40944095if(defined$site_header&& -f $site_header) {4096 insert_file($site_header);4097}40984099print"<div class=\"page_header\">\n";4100if(defined$logo) {4101print$cgi->a({-href => esc_url($logo_url),4102-title =>$logo_label},4103$cgi->img({-src => esc_url($logo),4104-width =>72, -height =>27,4105-alt =>"git",4106-class=>"logo"}));4107}4108 print_nav_breadcrumbs(%opts);4109print"</div>\n";41104111my$have_search= gitweb_check_feature('search');4112if(defined$project&&$have_search) {4113 print_search_form();4114}4115}41164117sub git_footer_html {4118my$feed_class='rss_logo';41194120print"<div class=\"page_footer\">\n";4121if(defined$project) {4122my$descr= git_get_project_description($project);4123if(defined$descr) {4124print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";4125}41264127my%href_params= get_feed_info();4128if(!%href_params) {4129$feed_class.=' generic';4130}4131$href_params{'-title'} ||='log';41324133foreachmy$format(qw(RSS Atom)) {4134$href_params{'action'} =lc($format);4135print$cgi->a({-href => href(%href_params),4136-title =>"$href_params{'-title'}$formatfeed",4137-class=>$feed_class},$format)."\n";4138}41394140}else{4141print$cgi->a({-href => href(project=>undef, action=>"opml",4142 project_filter =>$project_filter),4143-class=>$feed_class},"OPML") ." ";4144print$cgi->a({-href => href(project=>undef, action=>"project_index",4145 project_filter =>$project_filter),4146-class=>$feed_class},"TXT") ."\n";4147}4148print"</div>\n";# class="page_footer"41494150if(defined$t0&& gitweb_check_feature('timed')) {4151print"<div id=\"generating_info\">\n";4152print'This page took '.4153'<span id="generating_time" class="time_span">'.4154 tv_interval($t0, [ gettimeofday() ]).4155' seconds </span>'.4156' and '.4157'<span id="generating_cmd">'.4158$number_of_git_cmds.4159'</span> git commands '.4160" to generate.\n";4161print"</div>\n";# class="page_footer"4162}41634164if(defined$site_footer&& -f $site_footer) {4165 insert_file($site_footer);4166}41674168print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;4169if(defined$action&&4170$actioneq'blame_incremental') {4171print qq!<script type="text/javascript">\n!.4172 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.4173 qq!"!. href() .qq!");\n!.4174 qq!</script>\n!;4175}else{4176my($jstimezone,$tz_cookie,$datetime_class) =4177 gitweb_get_feature('javascript-timezone');41784179print qq!<script type="text/javascript">\n!.4180 qq!window.onload = function () {\n!;4181if(gitweb_check_feature('javascript-actions')) {4182print qq! fixLinks();\n!;4183}4184if($jstimezone&&$tz_cookie&&$datetime_class) {4185print qq! var tz_cookie = { name:'$tz_cookie', expires:14, path:'/'};\n!.# in days4186 qq! onloadTZSetup('$jstimezone', tz_cookie,'$datetime_class');\n!;4187}4188print qq!};\n!.4189 qq!</script>\n!;4190}41914192print"</body>\n".4193"</html>";4194}41954196# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])4197# Example: die_error(404, 'Hash not found')4198# By convention, use the following status codes (as defined in RFC 2616):4199# 400: Invalid or missing CGI parameters, or4200# requested object exists but has wrong type.4201# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on4202# this server or project.4203# 404: Requested object/revision/project doesn't exist.4204# 500: The server isn't configured properly, or4205# an internal error occurred (e.g. failed assertions caused by bugs), or4206# an unknown error occurred (e.g. the git binary died unexpectedly).4207# 503: The server is currently unavailable (because it is overloaded,4208# or down for maintenance). Generally, this is a temporary state.4209sub die_error {4210my$status=shift||500;4211my$error= esc_html(shift) ||"Internal Server Error";4212my$extra=shift;4213my%opts=@_;42144215my%http_responses= (4216400=>'400 Bad Request',4217403=>'403 Forbidden',4218404=>'404 Not Found',4219500=>'500 Internal Server Error',4220503=>'503 Service Unavailable',4221);4222 git_header_html($http_responses{$status},undef,%opts);4223print<<EOF;4224<div class="page_body">4225<br /><br />4226$status-$error4227<br />4228EOF4229if(defined$extra) {4230print"<hr />\n".4231"$extra\n";4232}4233print"</div>\n";42344235 git_footer_html();4236goto DONE_GITWEB4237unless($opts{'-error_handler'});4238}42394240## ----------------------------------------------------------------------4241## functions printing or outputting HTML: navigation42424243sub git_print_page_nav {4244my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;4245$extra=''if!defined$extra;# pager or formats42464247my@navs=qw(summary shortlog log commit commitdiff tree);4248if($suppress) {4249@navs=grep{$_ne$suppress}@navs;4250}42514252my%arg=map{$_=> {action=>$_} }@navs;4253if(defined$head) {4254for(qw(commit commitdiff)) {4255$arg{$_}{'hash'} =$head;4256}4257if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {4258for(qw(shortlog log)) {4259$arg{$_}{'hash'} =$head;4260}4261}4262}42634264$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;4265$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;42664267my@actions= gitweb_get_feature('actions');4268my%repl= (4269'%'=>'%',4270'n'=>$project,# project name4271'f'=>$git_dir,# project path within filesystem4272'h'=>$treehead||'',# current hash ('h' parameter)4273'b'=>$treebase||'',# hash base ('hb' parameter)4274);4275while(@actions) {4276my($label,$link,$pos) =splice(@actions,0,3);4277# insert4278@navs=map{$_eq$pos? ($_,$label) :$_}@navs;4279# munch munch4280$link=~s/%([%nfhb])/$repl{$1}/g;4281$arg{$label}{'_href'} =$link;4282}42834284print"<div class=\"page_nav\">\n".4285(join" | ",4286map{$_eq$current?4287$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")4288}@navs);4289print"<br/>\n$extra<br/>\n".4290"</div>\n";4291}42924293# returns a submenu for the nagivation of the refs views (tags, heads,4294# remotes) with the current view disabled and the remotes view only4295# available if the feature is enabled4296sub format_ref_views {4297my($current) =@_;4298my@ref_views=qw{tags heads};4299push@ref_views,'remotes'if gitweb_check_feature('remote_heads');4300returnjoin" | ",map{4301$_eq$current?$_:4302$cgi->a({-href => href(action=>$_)},$_)4303}@ref_views4304}43054306sub format_paging_nav {4307my($action,$page,$has_next_link) =@_;4308my$paging_nav;430943104311if($page>0) {4312$paging_nav.=4313$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .4314" ⋅ ".4315$cgi->a({-href => href(-replay=>1, page=>$page-1),4316-accesskey =>"p", -title =>"Alt-p"},"prev");4317}else{4318$paging_nav.="first ⋅ prev";4319}43204321if($has_next_link) {4322$paging_nav.=" ⋅ ".4323$cgi->a({-href => href(-replay=>1, page=>$page+1),4324-accesskey =>"n", -title =>"Alt-n"},"next");4325}else{4326$paging_nav.=" ⋅ next";4327}43284329return$paging_nav;4330}43314332## ......................................................................4333## functions printing or outputting HTML: div43344335sub git_print_header_div {4336my($action,$title,$hash,$hash_base) =@_;4337my%args= ();43384339$args{'action'} =$action;4340$args{'hash'} =$hashif$hash;4341$args{'hash_base'} =$hash_baseif$hash_base;43424343print"<div class=\"header\">\n".4344$cgi->a({-href => href(%args), -class=>"title"},4345$title?$title:$action) .4346"\n</div>\n";4347}43484349sub format_repo_url {4350my($name,$url) =@_;4351return"<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";4352}43534354# Group output by placing it in a DIV element and adding a header.4355# Options for start_div() can be provided by passing a hash reference as the4356# first parameter to the function.4357# Options to git_print_header_div() can be provided by passing an array4358# reference. This must follow the options to start_div if they are present.4359# The content can be a scalar, which is output as-is, a scalar reference, which4360# is output after html escaping, an IO handle passed either as *handle or4361# *handle{IO}, or a function reference. In the latter case all following4362# parameters will be taken as argument to the content function call.4363sub git_print_section {4364my($div_args,$header_args,$content);4365my$arg=shift;4366if(ref($arg)eq'HASH') {4367$div_args=$arg;4368$arg=shift;4369}4370if(ref($arg)eq'ARRAY') {4371$header_args=$arg;4372$arg=shift;4373}4374$content=$arg;43754376print$cgi->start_div($div_args);4377 git_print_header_div(@$header_args);43784379if(ref($content)eq'CODE') {4380$content->(@_);4381}elsif(ref($content)eq'SCALAR') {4382print esc_html($$content);4383}elsif(ref($content)eq'GLOB'or ref($content)eq'IO::Handle') {4384print<$content>;4385}elsif(!ref($content) &&defined($content)) {4386print$content;4387}43884389print$cgi->end_div;4390}43914392sub format_timestamp_html {4393my$date=shift;4394my$strtime=$date->{'rfc2822'};43954396my(undef,undef,$datetime_class) =4397 gitweb_get_feature('javascript-timezone');4398if($datetime_class) {4399$strtime= qq!<span class="$datetime_class">$strtime</span>!;4400}44014402my$localtime_format='(%02d:%02d%s)';4403if($date->{'hour_local'} <6) {4404$localtime_format='(<span class="atnight">%02d:%02d</span>%s)';4405}4406$strtime.=' '.4407sprintf($localtime_format,4408$date->{'hour_local'},$date->{'minute_local'},$date->{'tz_local'});44094410return$strtime;4411}44124413# Outputs the author name and date in long form4414sub git_print_authorship {4415my$co=shift;4416my%opts=@_;4417my$tag=$opts{-tag} ||'div';4418my$author=$co->{'author_name'};44194420my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});4421print"<$tagclass=\"author_date\">".4422 format_search_author($author,"author", esc_html($author)) .4423" [".format_timestamp_html(\%ad)."]".4424 git_get_avatar($co->{'author_email'}, -pad_before =>1) .4425"</$tag>\n";4426}44274428# Outputs table rows containing the full author or committer information,4429# in the format expected for 'commit' view (& similar).4430# Parameters are a commit hash reference, followed by the list of people4431# to output information for. If the list is empty it defaults to both4432# author and committer.4433sub git_print_authorship_rows {4434my$co=shift;4435# too bad we can't use @people = @_ || ('author', 'committer')4436my@people=@_;4437@people= ('author','committer')unless@people;4438foreachmy$who(@people) {4439my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});4440print"<tr><td>$who</td><td>".4441 format_search_author($co->{"${who}_name"},$who,4442 esc_html($co->{"${who}_name"})) ." ".4443 format_search_author($co->{"${who}_email"},$who,4444 esc_html("<".$co->{"${who}_email"} .">")) .4445"</td><td rowspan=\"2\">".4446 git_get_avatar($co->{"${who}_email"}, -size =>'double') .4447"</td></tr>\n".4448"<tr>".4449"<td></td><td>".4450 format_timestamp_html(\%wd) .4451"</td>".4452"</tr>\n";4453}4454}44554456sub git_print_page_path {4457my$name=shift;4458my$type=shift;4459my$hb=shift;446044614462print"<div class=\"page_path\">";4463print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),4464-title =>'tree root'}, to_utf8("[$project]"));4465print" / ";4466if(defined$name) {4467my@dirname=split'/',$name;4468my$basename=pop@dirname;4469my$fullname='';44704471foreachmy$dir(@dirname) {4472$fullname.= ($fullname?'/':'') .$dir;4473print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,4474 hash_base=>$hb),4475-title =>$fullname}, esc_path($dir));4476print" / ";4477}4478if(defined$type&&$typeeq'blob') {4479print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,4480 hash_base=>$hb),4481-title =>$name}, esc_path($basename));4482}elsif(defined$type&&$typeeq'tree') {4483print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,4484 hash_base=>$hb),4485-title =>$name}, esc_path($basename));4486print" / ";4487}else{4488print esc_path($basename);4489}4490}4491print"<br/></div>\n";4492}44934494sub git_print_log {4495my$log=shift;4496my%opts=@_;44974498if($opts{'-remove_title'}) {4499# remove title, i.e. first line of log4500shift@$log;4501}4502# remove leading empty lines4503while(defined$log->[0] &&$log->[0]eq"") {4504shift@$log;4505}45064507# print log4508my$skip_blank_line=0;4509foreachmy$line(@$log) {4510if($line=~m/^\s*([A-Z][-A-Za-z]*-[Bb]y|C[Cc]): /) {4511if(!$opts{'-remove_signoff'}) {4512print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";4513$skip_blank_line=1;4514}4515next;4516}45174518if($line=~ m,\s*([a-z]*link): (https?://\S+),i) {4519if(!$opts{'-remove_signoff'}) {4520print"<span class=\"signoff\">". esc_html($1) .": ".4521"<a href=\"". esc_html($2) ."\">". esc_html($2) ."</a>".4522"</span><br/>\n";4523$skip_blank_line=1;4524}4525next;4526}45274528# print only one empty line4529# do not print empty line after signoff4530if($lineeq"") {4531next if($skip_blank_line);4532$skip_blank_line=1;4533}else{4534$skip_blank_line=0;4535}45364537print format_log_line_html($line) ."<br/>\n";4538}45394540if($opts{'-final_empty_line'}) {4541# end with single empty line4542print"<br/>\n"unless$skip_blank_line;4543}4544}45454546# return link target (what link points to)4547sub git_get_link_target {4548my$hash=shift;4549my$link_target;45504551# read link4552open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4553orreturn;4554{4555local$/=undef;4556$link_target= <$fd>;4557}4558close$fd4559orreturn;45604561return$link_target;4562}45634564# given link target, and the directory (basedir) the link is in,4565# return target of link relative to top directory (top tree);4566# return undef if it is not possible (including absolute links).4567sub normalize_link_target {4568my($link_target,$basedir) =@_;45694570# absolute symlinks (beginning with '/') cannot be normalized4571return if(substr($link_target,0,1)eq'/');45724573# normalize link target to path from top (root) tree (dir)4574my$path;4575if($basedir) {4576$path=$basedir.'/'.$link_target;4577}else{4578# we are in top (root) tree (dir)4579$path=$link_target;4580}45814582# remove //, /./, and /../4583my@path_parts;4584foreachmy$part(split('/',$path)) {4585# discard '.' and ''4586next if(!$part||$parteq'.');4587# handle '..'4588if($parteq'..') {4589if(@path_parts) {4590pop@path_parts;4591}else{4592# link leads outside repository (outside top dir)4593return;4594}4595}else{4596push@path_parts,$part;4597}4598}4599$path=join('/',@path_parts);46004601return$path;4602}46034604# print tree entry (row of git_tree), but without encompassing <tr> element4605sub git_print_tree_entry {4606my($t,$basedir,$hash_base,$have_blame) =@_;46074608my%base_key= ();4609$base_key{'hash_base'} =$hash_baseifdefined$hash_base;46104611# The format of a table row is: mode list link. Where mode is4612# the mode of the entry, list is the name of the entry, an href,4613# and link is the action links of the entry.46144615print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";4616if(exists$t->{'size'}) {4617print"<td class=\"size\">$t->{'size'}</td>\n";4618}4619if($t->{'type'}eq"blob") {4620print"<td class=\"list\">".4621$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4622 file_name=>"$basedir$t->{'name'}",%base_key),4623-class=>"list"}, esc_path($t->{'name'}));4624if(S_ISLNK(oct$t->{'mode'})) {4625my$link_target= git_get_link_target($t->{'hash'});4626if($link_target) {4627my$norm_target= normalize_link_target($link_target,$basedir);4628if(defined$norm_target) {4629print" -> ".4630$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,4631 file_name=>$norm_target),4632-title =>$norm_target}, esc_path($link_target));4633}else{4634print" -> ". esc_path($link_target);4635}4636}4637}4638print"</td>\n";4639print"<td class=\"link\">";4640print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4641 file_name=>"$basedir$t->{'name'}",%base_key)},4642"blob");4643if($have_blame) {4644print" | ".4645$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4646 file_name=>"$basedir$t->{'name'}",%base_key)},4647"blame");4648}4649if(defined$hash_base) {4650print" | ".4651$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4652 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4653"history");4654}4655print" | ".4656$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4657 file_name=>"$basedir$t->{'name'}")},4658"raw");4659print"</td>\n";46604661}elsif($t->{'type'}eq"tree") {4662print"<td class=\"list\">";4663print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4664 file_name=>"$basedir$t->{'name'}",4665%base_key)},4666 esc_path($t->{'name'}));4667print"</td>\n";4668print"<td class=\"link\">";4669print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4670 file_name=>"$basedir$t->{'name'}",4671%base_key)},4672"tree");4673if(defined$hash_base) {4674print" | ".4675$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4676 file_name=>"$basedir$t->{'name'}")},4677"history");4678}4679print"</td>\n";4680}else{4681# unknown object: we can only present history for it4682# (this includes 'commit' object, i.e. submodule support)4683print"<td class=\"list\">".4684 esc_path($t->{'name'}) .4685"</td>\n";4686print"<td class=\"link\">";4687if(defined$hash_base) {4688print$cgi->a({-href => href(action=>"history",4689 hash_base=>$hash_base,4690 file_name=>"$basedir$t->{'name'}")},4691"history");4692}4693print"</td>\n";4694}4695}46964697## ......................................................................4698## functions printing large fragments of HTML46994700# get pre-image filenames for merge (combined) diff4701sub fill_from_file_info {4702my($diff,@parents) =@_;47034704$diff->{'from_file'} = [ ];4705$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4706for(my$i=0;$i<$diff->{'nparents'};$i++) {4707if($diff->{'status'}[$i]eq'R'||4708$diff->{'status'}[$i]eq'C') {4709$diff->{'from_file'}[$i] =4710 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4711}4712}47134714return$diff;4715}47164717# is current raw difftree line of file deletion4718sub is_deleted {4719my$diffinfo=shift;47204721return$diffinfo->{'to_id'}eq('0' x 40);4722}47234724# does patch correspond to [previous] difftree raw line4725# $diffinfo - hashref of parsed raw diff format4726# $patchinfo - hashref of parsed patch diff format4727# (the same keys as in $diffinfo)4728sub is_patch_split {4729my($diffinfo,$patchinfo) =@_;47304731returndefined$diffinfo&&defined$patchinfo4732&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4733}473447354736sub git_difftree_body {4737my($difftree,$hash,@parents) =@_;4738my($parent) =$parents[0];4739my$have_blame= gitweb_check_feature('blame');4740print"<div class=\"list_head\">\n";4741if($#{$difftree} >10) {4742print(($#{$difftree} +1) ." files changed:\n");4743}4744print"</div>\n";47454746print"<table class=\"".4747(@parents>1?"combined ":"") .4748"diff_tree\">\n";47494750# header only for combined diff in 'commitdiff' view4751my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4752if($has_header) {4753# table header4754print"<thead><tr>\n".4755"<th></th><th></th>\n";# filename, patchN link4756for(my$i=0;$i<@parents;$i++) {4757my$par=$parents[$i];4758print"<th>".4759$cgi->a({-href => href(action=>"commitdiff",4760 hash=>$hash, hash_parent=>$par),4761-title =>'commitdiff to parent number '.4762($i+1) .': '.substr($par,0,7)},4763$i+1) .4764" </th>\n";4765}4766print"</tr></thead>\n<tbody>\n";4767}47684769my$alternate=1;4770my$patchno=0;4771foreachmy$line(@{$difftree}) {4772my$diff= parsed_difftree_line($line);47734774if($alternate) {4775print"<tr class=\"dark\">\n";4776}else{4777print"<tr class=\"light\">\n";4778}4779$alternate^=1;47804781if(exists$diff->{'nparents'}) {# combined diff47824783 fill_from_file_info($diff,@parents)4784unlessexists$diff->{'from_file'};47854786if(!is_deleted($diff)) {4787# file exists in the result (child) commit4788print"<td>".4789$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4790 file_name=>$diff->{'to_file'},4791 hash_base=>$hash),4792-class=>"list"}, esc_path($diff->{'to_file'})) .4793"</td>\n";4794}else{4795print"<td>".4796 esc_path($diff->{'to_file'}) .4797"</td>\n";4798}47994800if($actioneq'commitdiff') {4801# link to patch4802$patchno++;4803print"<td class=\"link\">".4804$cgi->a({-href => href(-anchor=>"patch$patchno")},4805"patch") .4806" | ".4807"</td>\n";4808}48094810my$has_history=0;4811my$not_deleted=0;4812for(my$i=0;$i<$diff->{'nparents'};$i++) {4813my$hash_parent=$parents[$i];4814my$from_hash=$diff->{'from_id'}[$i];4815my$from_path=$diff->{'from_file'}[$i];4816my$status=$diff->{'status'}[$i];48174818$has_history||= ($statusne'A');4819$not_deleted||= ($statusne'D');48204821if($statuseq'A') {4822print"<td class=\"link\"align=\"right\"> | </td>\n";4823}elsif($statuseq'D') {4824print"<td class=\"link\">".4825$cgi->a({-href => href(action=>"blob",4826 hash_base=>$hash,4827 hash=>$from_hash,4828 file_name=>$from_path)},4829"blob". ($i+1)) .4830" | </td>\n";4831}else{4832if($diff->{'to_id'}eq$from_hash) {4833print"<td class=\"link nochange\">";4834}else{4835print"<td class=\"link\">";4836}4837print$cgi->a({-href => href(action=>"blobdiff",4838 hash=>$diff->{'to_id'},4839 hash_parent=>$from_hash,4840 hash_base=>$hash,4841 hash_parent_base=>$hash_parent,4842 file_name=>$diff->{'to_file'},4843 file_parent=>$from_path)},4844"diff". ($i+1)) .4845" | </td>\n";4846}4847}48484849print"<td class=\"link\">";4850if($not_deleted) {4851print$cgi->a({-href => href(action=>"blob",4852 hash=>$diff->{'to_id'},4853 file_name=>$diff->{'to_file'},4854 hash_base=>$hash)},4855"blob");4856print" | "if($has_history);4857}4858if($has_history) {4859print$cgi->a({-href => href(action=>"history",4860 file_name=>$diff->{'to_file'},4861 hash_base=>$hash)},4862"history");4863}4864print"</td>\n";48654866print"</tr>\n";4867next;# instead of 'else' clause, to avoid extra indent4868}4869# else ordinary diff48704871my($to_mode_oct,$to_mode_str,$to_file_type);4872my($from_mode_oct,$from_mode_str,$from_file_type);4873if($diff->{'to_mode'}ne('0' x 6)) {4874$to_mode_oct=oct$diff->{'to_mode'};4875if(S_ISREG($to_mode_oct)) {# only for regular file4876$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4877}4878$to_file_type= file_type($diff->{'to_mode'});4879}4880if($diff->{'from_mode'}ne('0' x 6)) {4881$from_mode_oct=oct$diff->{'from_mode'};4882if(S_ISREG($from_mode_oct)) {# only for regular file4883$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4884}4885$from_file_type= file_type($diff->{'from_mode'});4886}48874888if($diff->{'status'}eq"A") {# created4889my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4890$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4891$mode_chng.="]</span>";4892print"<td>";4893print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4894 hash_base=>$hash, file_name=>$diff->{'file'}),4895-class=>"list"}, esc_path($diff->{'file'}));4896print"</td>\n";4897print"<td>$mode_chng</td>\n";4898print"<td class=\"link\">";4899if($actioneq'commitdiff') {4900# link to patch4901$patchno++;4902print$cgi->a({-href => href(-anchor=>"patch$patchno")},4903"patch") .4904" | ";4905}4906print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4907 hash_base=>$hash, file_name=>$diff->{'file'})},4908"blob");4909print"</td>\n";49104911}elsif($diff->{'status'}eq"D") {# deleted4912my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4913print"<td>";4914print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4915 hash_base=>$parent, file_name=>$diff->{'file'}),4916-class=>"list"}, esc_path($diff->{'file'}));4917print"</td>\n";4918print"<td>$mode_chng</td>\n";4919print"<td class=\"link\">";4920if($actioneq'commitdiff') {4921# link to patch4922$patchno++;4923print$cgi->a({-href => href(-anchor=>"patch$patchno")},4924"patch") .4925" | ";4926}4927print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4928 hash_base=>$parent, file_name=>$diff->{'file'})},4929"blob") ." | ";4930if($have_blame) {4931print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4932 file_name=>$diff->{'file'})},4933"blame") ." | ";4934}4935print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4936 file_name=>$diff->{'file'})},4937"history");4938print"</td>\n";49394940}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4941my$mode_chnge="";4942if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4943$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4944if($from_file_typene$to_file_type) {4945$mode_chnge.=" from$from_file_typeto$to_file_type";4946}4947if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4948if($from_mode_str&&$to_mode_str) {4949$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4950}elsif($to_mode_str) {4951$mode_chnge.=" mode:$to_mode_str";4952}4953}4954$mode_chnge.="]</span>\n";4955}4956print"<td>";4957print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4958 hash_base=>$hash, file_name=>$diff->{'file'}),4959-class=>"list"}, esc_path($diff->{'file'}));4960print"</td>\n";4961print"<td>$mode_chnge</td>\n";4962print"<td class=\"link\">";4963if($actioneq'commitdiff') {4964# link to patch4965$patchno++;4966print$cgi->a({-href => href(-anchor=>"patch$patchno")},4967"patch") .4968" | ";4969}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4970# "commit" view and modified file (not onlu mode changed)4971print$cgi->a({-href => href(action=>"blobdiff",4972 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4973 hash_base=>$hash, hash_parent_base=>$parent,4974 file_name=>$diff->{'file'})},4975"diff") .4976" | ";4977}4978print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4979 hash_base=>$hash, file_name=>$diff->{'file'})},4980"blob") ." | ";4981if($have_blame) {4982print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4983 file_name=>$diff->{'file'})},4984"blame") ." | ";4985}4986print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4987 file_name=>$diff->{'file'})},4988"history");4989print"</td>\n";49904991}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4992my%status_name= ('R'=>'moved','C'=>'copied');4993my$nstatus=$status_name{$diff->{'status'}};4994my$mode_chng="";4995if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4996# mode also for directories, so we cannot use $to_mode_str4997$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4998}4999print"<td>".5000$cgi->a({-href => href(action=>"blob", hash_base=>$hash,5001 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),5002-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".5003"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".5004$cgi->a({-href => href(action=>"blob", hash_base=>$parent,5005 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),5006-class=>"list"}, esc_path($diff->{'from_file'})) .5007" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".5008"<td class=\"link\">";5009if($actioneq'commitdiff') {5010# link to patch5011$patchno++;5012print$cgi->a({-href => href(-anchor=>"patch$patchno")},5013"patch") .5014" | ";5015}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {5016# "commit" view and modified file (not only pure rename or copy)5017print$cgi->a({-href => href(action=>"blobdiff",5018 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},5019 hash_base=>$hash, hash_parent_base=>$parent,5020 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},5021"diff") .5022" | ";5023}5024print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},5025 hash_base=>$parent, file_name=>$diff->{'to_file'})},5026"blob") ." | ";5027if($have_blame) {5028print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,5029 file_name=>$diff->{'to_file'})},5030"blame") ." | ";5031}5032print$cgi->a({-href => href(action=>"history", hash_base=>$hash,5033 file_name=>$diff->{'to_file'})},5034"history");5035print"</td>\n";50365037}# we should not encounter Unmerged (U) or Unknown (X) status5038print"</tr>\n";5039}5040print"</tbody>"if$has_header;5041print"</table>\n";5042}50435044# Print context lines and then rem/add lines in a side-by-side manner.5045sub print_sidebyside_diff_lines {5046my($ctx,$rem,$add) =@_;50475048# print context block before add/rem block5049if(@$ctx) {5050print join'',5051'<div class="chunk_block ctx">',5052'<div class="old">',5053@$ctx,5054'</div>',5055'<div class="new">',5056@$ctx,5057'</div>',5058'</div>';5059}50605061if(!@$add) {5062# pure removal5063print join'',5064'<div class="chunk_block rem">',5065'<div class="old">',5066@$rem,5067'</div>',5068'</div>';5069}elsif(!@$rem) {5070# pure addition5071print join'',5072'<div class="chunk_block add">',5073'<div class="new">',5074@$add,5075'</div>',5076'</div>';5077}else{5078print join'',5079'<div class="chunk_block chg">',5080'<div class="old">',5081@$rem,5082'</div>',5083'<div class="new">',5084@$add,5085'</div>',5086'</div>';5087}5088}50895090# Print context lines and then rem/add lines in inline manner.5091sub print_inline_diff_lines {5092my($ctx,$rem,$add) =@_;50935094print@$ctx,@$rem,@$add;5095}50965097# Format removed and added line, mark changed part and HTML-format them.5098# Implementation is based on contrib/diff-highlight5099sub format_rem_add_lines_pair {5100my($rem,$add,$num_parents) =@_;51015102# We need to untabify lines before split()'ing them;5103# otherwise offsets would be invalid.5104chomp$rem;5105chomp$add;5106$rem= untabify($rem);5107$add= untabify($add);51085109my@rem=split(//,$rem);5110my@add=split(//,$add);5111my($esc_rem,$esc_add);5112# Ignore leading +/- characters for each parent.5113my($prefix_len,$suffix_len) = ($num_parents,0);5114my($prefix_has_nonspace,$suffix_has_nonspace);51155116my$shorter= (@rem<@add) ?@rem:@add;5117while($prefix_len<$shorter) {5118last if($rem[$prefix_len]ne$add[$prefix_len]);51195120$prefix_has_nonspace=1if($rem[$prefix_len] !~/\s/);5121$prefix_len++;5122}51235124while($prefix_len+$suffix_len<$shorter) {5125last if($rem[-1-$suffix_len]ne$add[-1-$suffix_len]);51265127$suffix_has_nonspace=1if($rem[-1-$suffix_len] !~/\s/);5128$suffix_len++;5129}51305131# Mark lines that are different from each other, but have some common5132# part that isn't whitespace. If lines are completely different, don't5133# mark them because that would make output unreadable, especially if5134# diff consists of multiple lines.5135if($prefix_has_nonspace||$suffix_has_nonspace) {5136$esc_rem= esc_html_hl_regions($rem,'marked',5137[$prefix_len,@rem-$suffix_len], -nbsp=>1);5138$esc_add= esc_html_hl_regions($add,'marked',5139[$prefix_len,@add-$suffix_len], -nbsp=>1);5140}else{5141$esc_rem= esc_html($rem, -nbsp=>1);5142$esc_add= esc_html($add, -nbsp=>1);5143}51445145return format_diff_line(\$esc_rem,'rem'),5146 format_diff_line(\$esc_add,'add');5147}51485149# HTML-format diff context, removed and added lines.5150sub format_ctx_rem_add_lines {5151my($ctx,$rem,$add,$num_parents) =@_;5152my(@new_ctx,@new_rem,@new_add);5153my$can_highlight=0;5154my$is_combined= ($num_parents>1);51555156# Highlight if every removed line has a corresponding added line.5157if(@$add>0&&@$add==@$rem) {5158$can_highlight=1;51595160# Highlight lines in combined diff only if the chunk contains5161# diff between the same version, e.g.5162#5163# - a5164# - b5165# + c5166# + d5167#5168# Otherwise the highlightling would be confusing.5169if($is_combined) {5170for(my$i=0;$i<@$add;$i++) {5171my$prefix_rem=substr($rem->[$i],0,$num_parents);5172my$prefix_add=substr($add->[$i],0,$num_parents);51735174$prefix_rem=~s/-/+/g;51755176if($prefix_remne$prefix_add) {5177$can_highlight=0;5178last;5179}5180}5181}5182}51835184if($can_highlight) {5185for(my$i=0;$i<@$add;$i++) {5186my($line_rem,$line_add) = format_rem_add_lines_pair(5187$rem->[$i],$add->[$i],$num_parents);5188push@new_rem,$line_rem;5189push@new_add,$line_add;5190}5191}else{5192@new_rem=map{ format_diff_line($_,'rem') }@$rem;5193@new_add=map{ format_diff_line($_,'add') }@$add;5194}51955196@new_ctx=map{ format_diff_line($_,'ctx') }@$ctx;51975198return(\@new_ctx, \@new_rem, \@new_add);5199}52005201# Print context lines and then rem/add lines.5202sub print_diff_lines {5203my($ctx,$rem,$add,$diff_style,$num_parents) =@_;5204my$is_combined=$num_parents>1;52055206($ctx,$rem,$add) = format_ctx_rem_add_lines($ctx,$rem,$add,5207$num_parents);52085209if($diff_styleeq'sidebyside'&& !$is_combined) {5210 print_sidebyside_diff_lines($ctx,$rem,$add);5211}else{5212# default 'inline' style and unknown styles5213 print_inline_diff_lines($ctx,$rem,$add);5214}5215}52165217sub print_diff_chunk {5218my($diff_style,$num_parents,$from,$to,@chunk) =@_;5219my(@ctx,@rem,@add);52205221# The class of the previous line.5222my$prev_class='';52235224return unless@chunk;52255226# incomplete last line might be among removed or added lines,5227# or both, or among context lines: find which5228for(my$i=1;$i<@chunk;$i++) {5229if($chunk[$i][0]eq'incomplete') {5230$chunk[$i][0] =$chunk[$i-1][0];5231}5232}52335234# guardian5235push@chunk, ["",""];52365237foreachmy$line_info(@chunk) {5238my($class,$line) =@$line_info;52395240# print chunk headers5241if($class&&$classeq'chunk_header') {5242print format_diff_line($line,$class,$from,$to);5243next;5244}52455246## print from accumulator when have some add/rem lines or end5247# of chunk (flush context lines), or when have add and rem5248# lines and new block is reached (otherwise add/rem lines could5249# be reordered)5250if(!$class|| ((@rem||@add) &&$classeq'ctx') ||5251(@rem&&@add&&$classne$prev_class)) {5252 print_diff_lines(\@ctx, \@rem, \@add,5253$diff_style,$num_parents);5254@ctx=@rem=@add= ();5255}52565257## adding lines to accumulator5258# guardian value5259last unless$line;5260# rem, add or change5261if($classeq'rem') {5262push@rem,$line;5263}elsif($classeq'add') {5264push@add,$line;5265}5266# context line5267if($classeq'ctx') {5268push@ctx,$line;5269}52705271$prev_class=$class;5272}5273}52745275sub git_patchset_body {5276my($fd,$diff_style,$difftree,$hash,@hash_parents) =@_;5277my($hash_parent) =$hash_parents[0];52785279my$is_combined= (@hash_parents>1);5280my$patch_idx=0;5281my$patch_number=0;5282my$patch_line;5283my$diffinfo;5284my$to_name;5285my(%from,%to);5286my@chunk;# for side-by-side diff52875288print"<div class=\"patchset\">\n";52895290# skip to first patch5291while($patch_line= <$fd>) {5292chomp$patch_line;52935294last if($patch_line=~m/^diff /);5295}52965297 PATCH:5298while($patch_line) {52995300# parse "git diff" header line5301if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {5302# $1 is from_name, which we do not use5303$to_name= unquote($2);5304$to_name=~s!^b/!!;5305}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {5306# $1 is 'cc' or 'combined', which we do not use5307$to_name= unquote($2);5308}else{5309$to_name=undef;5310}53115312# check if current patch belong to current raw line5313# and parse raw git-diff line if needed5314if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {5315# this is continuation of a split patch5316print"<div class=\"patch cont\">\n";5317}else{5318# advance raw git-diff output if needed5319$patch_idx++ifdefined$diffinfo;53205321# read and prepare patch information5322$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);53235324# compact combined diff output can have some patches skipped5325# find which patch (using pathname of result) we are at now;5326if($is_combined) {5327while($to_namene$diffinfo->{'to_file'}) {5328print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".5329 format_diff_cc_simplified($diffinfo,@hash_parents) .5330"</div>\n";# class="patch"53315332$patch_idx++;5333$patch_number++;53345335last if$patch_idx>$#$difftree;5336$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);5337}5338}53395340# modifies %from, %to hashes5341 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);53425343# this is first patch for raw difftree line with $patch_idx index5344# we index @$difftree array from 0, but number patches from 15345print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";5346}53475348# git diff header5349#assert($patch_line =~ m/^diff /) if DEBUG;5350#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed5351$patch_number++;5352# print "git diff" header5353print format_git_diff_header_line($patch_line,$diffinfo,5354 \%from, \%to);53555356# print extended diff header5357print"<div class=\"diff extended_header\">\n";5358 EXTENDED_HEADER:5359while($patch_line= <$fd>) {5360chomp$patch_line;53615362last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);53635364print format_extended_diff_header_line($patch_line,$diffinfo,5365 \%from, \%to);5366}5367print"</div>\n";# class="diff extended_header"53685369# from-file/to-file diff header5370if(!$patch_line) {5371print"</div>\n";# class="patch"5372last PATCH;5373}5374next PATCH if($patch_line=~m/^diff /);5375#assert($patch_line =~ m/^---/) if DEBUG;53765377my$last_patch_line=$patch_line;5378$patch_line= <$fd>;5379chomp$patch_line;5380#assert($patch_line =~ m/^\+\+\+/) if DEBUG;53815382print format_diff_from_to_header($last_patch_line,$patch_line,5383$diffinfo, \%from, \%to,5384@hash_parents);53855386# the patch itself5387 LINE:5388while($patch_line= <$fd>) {5389chomp$patch_line;53905391next PATCH if($patch_line=~m/^diff /);53925393my$class= diff_line_class($patch_line, \%from, \%to);53945395if($classeq'chunk_header') {5396 print_diff_chunk($diff_style,scalar@hash_parents, \%from, \%to,@chunk);5397@chunk= ();5398}53995400push@chunk, [$class,$patch_line];5401}54025403}continue{5404if(@chunk) {5405 print_diff_chunk($diff_style,scalar@hash_parents, \%from, \%to,@chunk);5406@chunk= ();5407}5408print"</div>\n";# class="patch"5409}54105411# for compact combined (--cc) format, with chunk and patch simplification5412# the patchset might be empty, but there might be unprocessed raw lines5413for(++$patch_idxif$patch_number>0;5414$patch_idx<@$difftree;5415++$patch_idx) {5416# read and prepare patch information5417$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);54185419# generate anchor for "patch" links in difftree / whatchanged part5420print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".5421 format_diff_cc_simplified($diffinfo,@hash_parents) .5422"</div>\n";# class="patch"54235424$patch_number++;5425}54265427if($patch_number==0) {5428if(@hash_parents>1) {5429print"<div class=\"diff nodifferences\">Trivial merge</div>\n";5430}else{5431print"<div class=\"diff nodifferences\">No differences found</div>\n";5432}5433}54345435print"</div>\n";# class="patchset"5436}54375438# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54395440sub git_project_search_form {5441my($searchtext,$search_use_regexp) =@_;54425443my$limit='';5444if($project_filter) {5445$limit=" in '$project_filter/'";5446}54475448print"<div class=\"projsearch\">\n";5449print$cgi->startform(-method=>'get', -action =>$my_uri) .5450$cgi->hidden(-name =>'a', -value =>'project_list') ."\n";5451print$cgi->hidden(-name =>'pf', -value =>$project_filter)."\n"5452if(defined$project_filter);5453print$cgi->textfield(-name =>'s', -value =>$searchtext,5454-title =>"Search project by name and description$limit",5455-size =>60) ."\n".5456"<span title=\"Extended regular expression\">".5457$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',5458-checked =>$search_use_regexp) .5459"</span>\n".5460$cgi->submit(-name =>'btnS', -value =>'Search') .5461$cgi->end_form() ."\n".5462$cgi->a({-href => href(project =>undef, searchtext =>undef,5463 project_filter =>$project_filter)},5464 esc_html("List all projects$limit")) ."<br />\n";5465print"</div>\n";5466}54675468# entry for given @keys needs filling if at least one of keys in list5469# is not present in %$project_info5470sub project_info_needs_filling {5471my($project_info,@keys) =@_;54725473# return List::MoreUtils::any { !exists $project_info->{$_} } @keys;5474foreachmy$key(@keys) {5475if(!exists$project_info->{$key}) {5476return1;5477}5478}5479return;5480}54815482# fills project list info (age, description, owner, category, forks, etc.)5483# for each project in the list, removing invalid projects from5484# returned list, or fill only specified info.5485#5486# Invalid projects are removed from the returned list if and only if you5487# ask 'age' or 'age_string' to be filled, because they are the only fields5488# that run unconditionally git command that requires repository, and5489# therefore do always check if project repository is invalid.5490#5491# USAGE:5492# * fill_project_list_info(\@project_list, 'descr_long', 'ctags')5493# ensures that 'descr_long' and 'ctags' fields are filled5494# * @project_list = fill_project_list_info(\@project_list)5495# ensures that all fields are filled (and invalid projects removed)5496#5497# NOTE: modifies $projlist, but does not remove entries from it5498sub fill_project_list_info {5499my($projlist,@wanted_keys) =@_;5500my@projects;5501my$filter_set=sub{return@_; };5502if(@wanted_keys) {5503my%wanted_keys=map{$_=>1}@wanted_keys;5504$filter_set=sub{returngrep{$wanted_keys{$_} }@_; };5505}55065507my$show_ctags= gitweb_check_feature('ctags');5508 PROJECT:5509foreachmy$pr(@$projlist) {5510if(project_info_needs_filling($pr,$filter_set->('age','age_string'))) {5511my(@activity) = git_get_last_activity($pr->{'path'});5512unless(@activity) {5513next PROJECT;5514}5515($pr->{'age'},$pr->{'age_string'}) =@activity;5516}5517if(project_info_needs_filling($pr,$filter_set->('descr','descr_long'))) {5518my$descr= git_get_project_description($pr->{'path'}) ||"";5519$descr= to_utf8($descr);5520$pr->{'descr_long'} =$descr;5521$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);5522}5523if(project_info_needs_filling($pr,$filter_set->('owner'))) {5524$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";5525}5526if($show_ctags&&5527 project_info_needs_filling($pr,$filter_set->('ctags'))) {5528$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});5529}5530if($projects_list_group_categories&&5531 project_info_needs_filling($pr,$filter_set->('category'))) {5532my$cat= git_get_project_category($pr->{'path'}) ||5533$project_list_default_category;5534$pr->{'category'} = to_utf8($cat);5535}55365537push@projects,$pr;5538}55395540return@projects;5541}55425543sub sort_projects_list {5544my($projlist,$order) =@_;55455546sub order_str {5547my$key=shift;5548return sub{$a->{$key}cmp$b->{$key} };5549}55505551sub order_num_then_undef {5552my$key=shift;5553return sub{5554defined$a->{$key} ?5555(defined$b->{$key} ?$a->{$key} <=>$b->{$key} : -1) :5556(defined$b->{$key} ?1:0)5557};5558}55595560my%orderings= (5561 project => order_str('path'),5562 descr => order_str('descr_long'),5563 owner => order_str('owner'),5564 age => order_num_then_undef('age'),5565);55665567my$ordering=$orderings{$order};5568returndefined$ordering?sort$ordering @$projlist:@$projlist;5569}55705571# returns a hash of categories, containing the list of project5572# belonging to each category5573sub build_projlist_by_category {5574my($projlist,$from,$to) =@_;5575my%categories;55765577$from=0unlessdefined$from;5578$to=$#$projlistif(!defined$to||$#$projlist<$to);55795580for(my$i=$from;$i<=$to;$i++) {5581my$pr=$projlist->[$i];5582push@{$categories{$pr->{'category'} }},$pr;5583}55845585returnwantarray?%categories: \%categories;5586}55875588# print 'sort by' <th> element, generating 'sort by $name' replay link5589# if that order is not selected5590sub print_sort_th {5591print format_sort_th(@_);5592}55935594sub format_sort_th {5595my($name,$order,$header) =@_;5596my$sort_th="";5597$header||=ucfirst($name);55985599if($ordereq$name) {5600$sort_th.="<th>$header</th>\n";5601}else{5602$sort_th.="<th>".5603$cgi->a({-href => href(-replay=>1, order=>$name),5604-class=>"header"},$header) .5605"</th>\n";5606}56075608return$sort_th;5609}56105611sub git_project_list_rows {5612my($projlist,$from,$to,$check_forks) =@_;56135614$from=0unlessdefined$from;5615$to=$#$projlistif(!defined$to||$#$projlist<$to);56165617my$alternate=1;5618for(my$i=$from;$i<=$to;$i++) {5619my$pr=$projlist->[$i];56205621if($alternate) {5622print"<tr class=\"dark\">\n";5623}else{5624print"<tr class=\"light\">\n";5625}5626$alternate^=1;56275628if($check_forks) {5629print"<td>";5630if($pr->{'forks'}) {5631my$nforks=scalar@{$pr->{'forks'}};5632if($nforks>0) {5633print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),5634-title =>"$nforksforks"},"+");5635}else{5636print$cgi->span({-title =>"$nforksforks"},"+");5637}5638}5639print"</td>\n";5640}5641print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5642-class=>"list"},5643 esc_html_match_hl($pr->{'path'},$search_regexp)) .5644"</td>\n".5645"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5646-class=>"list",5647-title =>$pr->{'descr_long'}},5648$search_regexp5649? esc_html_match_hl_chopped($pr->{'descr_long'},5650$pr->{'descr'},$search_regexp)5651: esc_html($pr->{'descr'})) .5652"</td>\n";5653unless($omit_owner) {5654print"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";5655}5656unless($omit_age_column) {5657print"<td class=\"". age_class($pr->{'age'}) ."\">".5658(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n";5659}5660print"<td class=\"link\">".5661$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".5662$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".5663$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".5664$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .5665($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .5666"</td>\n".5667"</tr>\n";5668}5669}56705671sub git_project_list_body {5672# actually uses global variable $project5673my($projlist,$order,$from,$to,$extra,$no_header) =@_;5674my@projects=@$projlist;56755676my$check_forks= gitweb_check_feature('forks');5677my$show_ctags= gitweb_check_feature('ctags');5678my$tagfilter=$show_ctags?$input_params{'ctag'} :undef;5679$check_forks=undef5680if($tagfilter||$search_regexp);56815682# filtering out forks before filling info allows to do less work5683@projects= filter_forks_from_projects_list(\@projects)5684if($check_forks);5685# search_projects_list pre-fills required info5686@projects= search_projects_list(\@projects,5687'search_regexp'=>$search_regexp,5688'tagfilter'=>$tagfilter)5689if($tagfilter||$search_regexp);5690# fill the rest5691my@all_fields= ('descr','descr_long','ctags','category');5692push@all_fields, ('age','age_string')unless($omit_age_column);5693push@all_fields,'owner'unless($omit_owner);5694@projects= fill_project_list_info(\@projects,@all_fields);56955696$order||=$default_projects_order;5697$from=0unlessdefined$from;5698$to=$#projectsif(!defined$to||$#projects<$to);56995700# short circuit5701if($from>$to) {5702print"<center>\n".5703"<b>No such projects found</b><br />\n".5704"Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".5705"</center>\n<br />\n";5706return;5707}57085709@projects= sort_projects_list(\@projects,$order);57105711if($show_ctags) {5712my$ctags= git_gather_all_ctags(\@projects);5713my$cloud= git_populate_project_tagcloud($ctags);5714print git_show_project_tagcloud($cloud,64);5715}57165717print"<table class=\"project_list\">\n";5718unless($no_header) {5719print"<tr>\n";5720if($check_forks) {5721print"<th></th>\n";5722}5723 print_sort_th('project',$order,'Project');5724 print_sort_th('descr',$order,'Description');5725 print_sort_th('owner',$order,'Owner')unless$omit_owner;5726 print_sort_th('age',$order,'Last Change')unless$omit_age_column;5727print"<th></th>\n".# for links5728"</tr>\n";5729}57305731if($projects_list_group_categories) {5732# only display categories with projects in the $from-$to window5733@projects=sort{$a->{'category'}cmp$b->{'category'}}@projects[$from..$to];5734my%categories= build_projlist_by_category(\@projects,$from,$to);5735foreachmy$cat(sort keys%categories) {5736unless($cateq"") {5737print"<tr>\n";5738if($check_forks) {5739print"<td></td>\n";5740}5741print"<td class=\"category\"colspan=\"5\">".esc_html($cat)."</td>\n";5742print"</tr>\n";5743}57445745 git_project_list_rows($categories{$cat},undef,undef,$check_forks);5746}5747}else{5748 git_project_list_rows(\@projects,$from,$to,$check_forks);5749}57505751if(defined$extra) {5752print"<tr>\n";5753if($check_forks) {5754print"<td></td>\n";5755}5756print"<td colspan=\"5\">$extra</td>\n".5757"</tr>\n";5758}5759print"</table>\n";5760}57615762sub git_log_body {5763# uses global variable $project5764my($commitlist,$from,$to,$refs,$extra) =@_;57655766$from=0unlessdefined$from;5767$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);57685769for(my$i=0;$i<=$to;$i++) {5770my%co= %{$commitlist->[$i]};5771next if!%co;5772my$commit=$co{'id'};5773my$ref= format_ref_marker($refs,$commit);5774 git_print_header_div('commit',5775"<span class=\"age\">$co{'age_string'}</span>".5776 esc_html($co{'title'}) .$ref,5777$commit);5778print"<div class=\"title_text\">\n".5779"<div class=\"log_link\">\n".5780$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5781" | ".5782$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5783" | ".5784$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5785"<br/>\n".5786"</div>\n";5787 git_print_authorship(\%co, -tag =>'span');5788print"<br/>\n</div>\n";57895790print"<div class=\"log_body\">\n";5791 git_print_log($co{'comment'}, -final_empty_line=>1);5792print"</div>\n";5793}5794if($extra) {5795print"<div class=\"page_nav\">\n";5796print"$extra\n";5797print"</div>\n";5798}5799}58005801sub git_shortlog_body {5802# uses global variable $project5803my($commitlist,$from,$to,$refs,$extra) =@_;58045805$from=0unlessdefined$from;5806$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);58075808print"<table class=\"shortlog\">\n";5809my$alternate=1;5810for(my$i=$from;$i<=$to;$i++) {5811my%co= %{$commitlist->[$i]};5812my$commit=$co{'id'};5813my$ref= format_ref_marker($refs,$commit);5814if($alternate) {5815print"<tr class=\"dark\">\n";5816}else{5817print"<tr class=\"light\">\n";5818}5819$alternate^=1;5820# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .5821print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5822 format_author_html('td', \%co,10) ."<td>";5823print format_subject_html($co{'title'},$co{'title_short'},5824 href(action=>"commit", hash=>$commit),$ref);5825print"</td>\n".5826"<td class=\"link\">".5827$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".5828$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".5829$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");5830my$snapshot_links= format_snapshot_links($commit);5831if(defined$snapshot_links) {5832print" | ".$snapshot_links;5833}5834print"</td>\n".5835"</tr>\n";5836}5837if(defined$extra) {5838print"<tr>\n".5839"<td colspan=\"4\">$extra</td>\n".5840"</tr>\n";5841}5842print"</table>\n";5843}58445845sub git_history_body {5846# Warning: assumes constant type (blob or tree) during history5847my($commitlist,$from,$to,$refs,$extra,5848$file_name,$file_hash,$ftype) =@_;58495850$from=0unlessdefined$from;5851$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});58525853print"<table class=\"history\">\n";5854my$alternate=1;5855for(my$i=$from;$i<=$to;$i++) {5856my%co= %{$commitlist->[$i]};5857if(!%co) {5858next;5859}5860my$commit=$co{'id'};58615862my$ref= format_ref_marker($refs,$commit);58635864if($alternate) {5865print"<tr class=\"dark\">\n";5866}else{5867print"<tr class=\"light\">\n";5868}5869$alternate^=1;5870print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5871# shortlog: format_author_html('td', \%co, 10)5872 format_author_html('td', \%co,15,3) ."<td>";5873# originally git_history used chop_str($co{'title'}, 50)5874print format_subject_html($co{'title'},$co{'title_short'},5875 href(action=>"commit", hash=>$commit),$ref);5876print"</td>\n".5877"<td class=\"link\">".5878$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".5879$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");58805881if($ftypeeq'blob') {5882my$blob_current=$file_hash;5883my$blob_parent= git_get_hash_by_path($commit,$file_name);5884if(defined$blob_current&&defined$blob_parent&&5885$blob_currentne$blob_parent) {5886print" | ".5887$cgi->a({-href => href(action=>"blobdiff",5888 hash=>$blob_current, hash_parent=>$blob_parent,5889 hash_base=>$hash_base, hash_parent_base=>$commit,5890 file_name=>$file_name)},5891"diff to current");5892}5893}5894print"</td>\n".5895"</tr>\n";5896}5897if(defined$extra) {5898print"<tr>\n".5899"<td colspan=\"4\">$extra</td>\n".5900"</tr>\n";5901}5902print"</table>\n";5903}59045905sub git_tags_body {5906# uses global variable $project5907my($taglist,$from,$to,$extra) =@_;5908$from=0unlessdefined$from;5909$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);59105911print"<table class=\"tags\">\n";5912my$alternate=1;5913for(my$i=$from;$i<=$to;$i++) {5914my$entry=$taglist->[$i];5915my%tag=%$entry;5916my$comment=$tag{'subject'};5917my$comment_short;5918if(defined$comment) {5919$comment_short= chop_str($comment,30,5);5920}5921if($alternate) {5922print"<tr class=\"dark\">\n";5923}else{5924print"<tr class=\"light\">\n";5925}5926$alternate^=1;5927if(defined$tag{'age'}) {5928print"<td><i>$tag{'age'}</i></td>\n";5929}else{5930print"<td></td>\n";5931}5932print"<td>".5933$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),5934-class=>"list name"}, esc_html($tag{'name'})) .5935"</td>\n".5936"<td>";5937if(defined$comment) {5938print format_subject_html($comment,$comment_short,5939 href(action=>"tag", hash=>$tag{'id'}));5940}5941print"</td>\n".5942"<td class=\"selflink\">";5943if($tag{'type'}eq"tag") {5944print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");5945}else{5946print" ";5947}5948print"</td>\n".5949"<td class=\"link\">"." | ".5950$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});5951if($tag{'reftype'}eq"commit") {5952print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .5953" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");5954}elsif($tag{'reftype'}eq"blob") {5955print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");5956}5957print"</td>\n".5958"</tr>";5959}5960if(defined$extra) {5961print"<tr>\n".5962"<td colspan=\"5\">$extra</td>\n".5963"</tr>\n";5964}5965print"</table>\n";5966}59675968sub git_heads_body {5969# uses global variable $project5970my($headlist,$head_at,$from,$to,$extra) =@_;5971$from=0unlessdefined$from;5972$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);59735974print"<table class=\"heads\">\n";5975my$alternate=1;5976for(my$i=$from;$i<=$to;$i++) {5977my$entry=$headlist->[$i];5978my%ref=%$entry;5979my$curr=defined$head_at&&$ref{'id'}eq$head_at;5980if($alternate) {5981print"<tr class=\"dark\">\n";5982}else{5983print"<tr class=\"light\">\n";5984}5985$alternate^=1;5986print"<td><i>$ref{'age'}</i></td>\n".5987($curr?"<td class=\"current_head\">":"<td>") .5988$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),5989-class=>"list name"},esc_html($ref{'name'})) .5990"</td>\n".5991"<td class=\"link\">".5992$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".5993$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".5994$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})},"tree") .5995"</td>\n".5996"</tr>";5997}5998if(defined$extra) {5999print"<tr>\n".6000"<td colspan=\"3\">$extra</td>\n".6001"</tr>\n";6002}6003print"</table>\n";6004}60056006# Display a single remote block6007sub git_remote_block {6008my($remote,$rdata,$limit,$head) =@_;60096010my$heads=$rdata->{'heads'};6011my$fetch=$rdata->{'fetch'};6012my$push=$rdata->{'push'};60136014my$urls_table="<table class=\"projects_list\">\n";60156016if(defined$fetch) {6017if($fetcheq$push) {6018$urls_table.= format_repo_url("URL",$fetch);6019}else{6020$urls_table.= format_repo_url("Fetch URL",$fetch);6021$urls_table.= format_repo_url("Push URL",$push)ifdefined$push;6022}6023}elsif(defined$push) {6024$urls_table.= format_repo_url("Push URL",$push);6025}else{6026$urls_table.= format_repo_url("","No remote URL");6027}60286029$urls_table.="</table>\n";60306031my$dots;6032if(defined$limit&&$limit<@$heads) {6033$dots=$cgi->a({-href => href(action=>"remotes", hash=>$remote)},"...");6034}60356036print$urls_table;6037 git_heads_body($heads,$head,0,$limit,$dots);6038}60396040# Display a list of remote names with the respective fetch and push URLs6041sub git_remotes_list {6042my($remotedata,$limit) =@_;6043print"<table class=\"heads\">\n";6044my$alternate=1;6045my@remotes=sort keys%$remotedata;60466047my$limited=$limit&&$limit<@remotes;60486049$#remotes=$limit-1if$limited;60506051while(my$remote=shift@remotes) {6052my$rdata=$remotedata->{$remote};6053my$fetch=$rdata->{'fetch'};6054my$push=$rdata->{'push'};6055if($alternate) {6056print"<tr class=\"dark\">\n";6057}else{6058print"<tr class=\"light\">\n";6059}6060$alternate^=1;6061print"<td>".6062$cgi->a({-href=> href(action=>'remotes', hash=>$remote),6063-class=>"list name"},esc_html($remote)) .6064"</td>";6065print"<td class=\"link\">".6066(defined$fetch?$cgi->a({-href=>$fetch},"fetch") :"fetch") .6067" | ".6068(defined$push?$cgi->a({-href=>$push},"push") :"push") .6069"</td>";60706071print"</tr>\n";6072}60736074if($limited) {6075print"<tr>\n".6076"<td colspan=\"3\">".6077$cgi->a({-href => href(action=>"remotes")},"...") .6078"</td>\n"."</tr>\n";6079}60806081print"</table>";6082}60836084# Display remote heads grouped by remote, unless there are too many6085# remotes, in which case we only display the remote names6086sub git_remotes_body {6087my($remotedata,$limit,$head) =@_;6088if($limitand$limit<keys%$remotedata) {6089 git_remotes_list($remotedata,$limit);6090}else{6091 fill_remote_heads($remotedata);6092while(my($remote,$rdata) =each%$remotedata) {6093 git_print_section({-class=>"remote", -id=>$remote},6094["remotes",$remote,$remote],sub{6095 git_remote_block($remote,$rdata,$limit,$head);6096});6097}6098}6099}61006101sub git_search_message {6102my%co=@_;61036104my$greptype;6105if($searchtypeeq'commit') {6106$greptype="--grep=";6107}elsif($searchtypeeq'author') {6108$greptype="--author=";6109}elsif($searchtypeeq'committer') {6110$greptype="--committer=";6111}6112$greptype.=$searchtext;6113my@commitlist= parse_commits($hash,101, (100*$page),undef,6114$greptype,'--regexp-ignore-case',6115$search_use_regexp?'--extended-regexp':'--fixed-strings');61166117my$paging_nav='';6118if($page>0) {6119$paging_nav.=6120$cgi->a({-href => href(-replay=>1, page=>undef)},6121"first") .6122" ⋅ ".6123$cgi->a({-href => href(-replay=>1, page=>$page-1),6124-accesskey =>"p", -title =>"Alt-p"},"prev");6125}else{6126$paging_nav.="first ⋅ prev";6127}6128my$next_link='';6129if($#commitlist>=100) {6130$next_link=6131$cgi->a({-href => href(-replay=>1, page=>$page+1),6132-accesskey =>"n", -title =>"Alt-n"},"next");6133$paging_nav.=" ⋅$next_link";6134}else{6135$paging_nav.=" ⋅ next";6136}61376138 git_header_html();61396140 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6141 git_print_header_div('commit', esc_html($co{'title'}),$hash);6142if($page==0&& !@commitlist) {6143print"<p>No match.</p>\n";6144}else{6145 git_search_grep_body(\@commitlist,0,99,$next_link);6146}61476148 git_footer_html();6149}61506151sub git_search_changes {6152my%co=@_;61536154local$/="\n";6155open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6156'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6157($search_use_regexp?'--pickaxe-regex': ())6158or die_error(500,"Open git-log failed");61596160 git_header_html();61616162 git_print_page_nav('','',$hash,$co{'tree'},$hash);6163 git_print_header_div('commit', esc_html($co{'title'}),$hash);61646165print"<table class=\"pickaxe search\">\n";6166my$alternate=1;6167undef%co;6168my@files;6169while(my$line= <$fd>) {6170chomp$line;6171next unless$line;61726173my%set= parse_difftree_raw_line($line);6174if(defined$set{'commit'}) {6175# finish previous commit6176if(%co) {6177print"</td>\n".6178"<td class=\"link\">".6179$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},6180"commit") .6181" | ".6182$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},6183 hash_base=>$co{'id'})},6184"tree") .6185"</td>\n".6186"</tr>\n";6187}61886189if($alternate) {6190print"<tr class=\"dark\">\n";6191}else{6192print"<tr class=\"light\">\n";6193}6194$alternate^=1;6195%co= parse_commit($set{'commit'});6196my$author= chop_and_escape_str($co{'author_name'},15,5);6197print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6198"<td><i>$author</i></td>\n".6199"<td>".6200$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6201-class=>"list subject"},6202 chop_and_escape_str($co{'title'},50) ."<br/>");6203}elsif(defined$set{'to_id'}) {6204next if($set{'to_id'} =~m/^0{40}$/);62056206print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6207 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6208-class=>"list"},6209"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6210"<br/>\n";6211}6212}6213close$fd;62146215# finish last commit (warning: repetition!)6216if(%co) {6217print"</td>\n".6218"<td class=\"link\">".6219$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},6220"commit") .6221" | ".6222$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},6223 hash_base=>$co{'id'})},6224"tree") .6225"</td>\n".6226"</tr>\n";6227}62286229print"</table>\n";62306231 git_footer_html();6232}62336234sub git_search_files {6235my%co=@_;62366237local$/="\n";6238open my$fd,"-|", git_cmd(),'grep','-n','-z',6239$search_use_regexp? ('-E','-i') :'-F',6240$searchtext,$co{'tree'}6241or die_error(500,"Open git-grep failed");62426243 git_header_html();62446245 git_print_page_nav('','',$hash,$co{'tree'},$hash);6246 git_print_header_div('commit', esc_html($co{'title'}),$hash);62476248print"<table class=\"grep_search\">\n";6249my$alternate=1;6250my$matches=0;6251my$lastfile='';6252my$file_href;6253while(my$line= <$fd>) {6254chomp$line;6255my($file,$lno,$ltext,$binary);6256last if($matches++>1000);6257if($line=~/^Binary file (.+) matches$/) {6258$file=$1;6259$binary=1;6260}else{6261($file,$lno,$ltext) =split(/\0/,$line,3);6262$file=~s/^$co{'tree'}://;6263}6264if($filene$lastfile) {6265$lastfileand print"</td></tr>\n";6266if($alternate++) {6267print"<tr class=\"dark\">\n";6268}else{6269print"<tr class=\"light\">\n";6270}6271$file_href= href(action=>"blob", hash_base=>$co{'id'},6272 file_name=>$file);6273print"<td class=\"list\">".6274$cgi->a({-href =>$file_href, -class=>"list"}, esc_path($file));6275print"</td><td>\n";6276$lastfile=$file;6277}6278if($binary) {6279print"<div class=\"binary\">Binary file</div>\n";6280}else{6281$ltext= untabify($ltext);6282if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6283$ltext= esc_html($1, -nbsp=>1);6284$ltext.='<span class="match">';6285$ltext.= esc_html($2, -nbsp=>1);6286$ltext.='</span>';6287$ltext.= esc_html($3, -nbsp=>1);6288}else{6289$ltext= esc_html($ltext, -nbsp=>1);6290}6291print"<div class=\"pre\">".6292$cgi->a({-href =>$file_href.'#l'.$lno,6293-class=>"linenr"},sprintf('%4i',$lno)) .6294' '.$ltext."</div>\n";6295}6296}6297if($lastfile) {6298print"</td></tr>\n";6299if($matches>1000) {6300print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6301}6302}else{6303print"<div class=\"diff nodifferences\">No matches found</div>\n";6304}6305close$fd;63066307print"</table>\n";63086309 git_footer_html();6310}63116312sub git_search_grep_body {6313my($commitlist,$from,$to,$extra) =@_;6314$from=0unlessdefined$from;6315$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);63166317print"<table class=\"commit_search\">\n";6318my$alternate=1;6319for(my$i=$from;$i<=$to;$i++) {6320my%co= %{$commitlist->[$i]};6321if(!%co) {6322next;6323}6324my$commit=$co{'id'};6325if($alternate) {6326print"<tr class=\"dark\">\n";6327}else{6328print"<tr class=\"light\">\n";6329}6330$alternate^=1;6331print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6332 format_author_html('td', \%co,15,5) .6333"<td>".6334$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6335-class=>"list subject"},6336 chop_and_escape_str($co{'title'},50) ."<br/>");6337my$comment=$co{'comment'};6338foreachmy$line(@$comment) {6339if($line=~m/^(.*?)($search_regexp)(.*)$/i) {6340my($lead,$match,$trail) = ($1,$2,$3);6341$match= chop_str($match,70,5,'center');6342my$contextlen=int((80-length($match))/2);6343$contextlen=30if($contextlen>30);6344$lead= chop_str($lead,$contextlen,10,'left');6345$trail= chop_str($trail,$contextlen,10,'right');63466347$lead= esc_html($lead);6348$match= esc_html($match);6349$trail= esc_html($trail);63506351print"$lead<span class=\"match\">$match</span>$trail<br />";6352}6353}6354print"</td>\n".6355"<td class=\"link\">".6356$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6357" | ".6358$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .6359" | ".6360$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6361print"</td>\n".6362"</tr>\n";6363}6364if(defined$extra) {6365print"<tr>\n".6366"<td colspan=\"3\">$extra</td>\n".6367"</tr>\n";6368}6369print"</table>\n";6370}63716372## ======================================================================6373## ======================================================================6374## actions63756376sub git_project_list {6377my$order=$input_params{'order'};6378if(defined$order&&$order!~m/none|project|descr|owner|age/) {6379 die_error(400,"Unknown order parameter");6380}63816382my@list= git_get_projects_list($project_filter,$strict_export);6383if(!@list) {6384 die_error(404,"No projects found");6385}63866387 git_header_html();6388if(defined$home_text&& -f $home_text) {6389print"<div class=\"index_include\">\n";6390 insert_file($home_text);6391print"</div>\n";6392}63936394 git_project_search_form($searchtext,$search_use_regexp);6395 git_project_list_body(\@list,$order);6396 git_footer_html();6397}63986399sub git_forks {6400my$order=$input_params{'order'};6401if(defined$order&&$order!~m/none|project|descr|owner|age/) {6402 die_error(400,"Unknown order parameter");6403}64046405my$filter=$project;6406$filter=~s/\.git$//;6407my@list= git_get_projects_list($filter);6408if(!@list) {6409 die_error(404,"No forks found");6410}64116412 git_header_html();6413 git_print_page_nav('','');6414 git_print_header_div('summary',"$projectforks");6415 git_project_list_body(\@list,$order);6416 git_footer_html();6417}64186419sub git_project_index {6420my@projects= git_get_projects_list($project_filter,$strict_export);6421if(!@projects) {6422 die_error(404,"No projects found");6423}64246425print$cgi->header(6426-type =>'text/plain',6427-charset =>'utf-8',6428-content_disposition =>'inline; filename="index.aux"');64296430foreachmy$pr(@projects) {6431if(!exists$pr->{'owner'}) {6432$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");6433}64346435my($path,$owner) = ($pr->{'path'},$pr->{'owner'});6436# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '6437$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;6438$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;6439$path=~s/ /\+/g;6440$owner=~s/ /\+/g;64416442print"$path$owner\n";6443}6444}64456446sub git_summary {6447my$descr= git_get_project_description($project) ||"none";6448my%co= parse_commit("HEAD");6449my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();6450my$head=$co{'id'};6451my$remote_heads= gitweb_check_feature('remote_heads');64526453my$owner= git_get_project_owner($project);64546455my$refs= git_get_references();6456# These get_*_list functions return one more to allow us to see if6457# there are more ...6458my@taglist= git_get_tags_list(16);6459my@headlist= git_get_heads_list(16);6460my%remotedata=$remote_heads? git_get_remotes_list() : ();6461my@forklist;6462my$check_forks= gitweb_check_feature('forks');64636464if($check_forks) {6465# find forks of a project6466my$filter=$project;6467$filter=~s/\.git$//;6468@forklist= git_get_projects_list($filter);6469# filter out forks of forks6470@forklist= filter_forks_from_projects_list(\@forklist)6471if(@forklist);6472}64736474 git_header_html();6475 git_print_page_nav('summary','',$head);64766477print"<div class=\"title\"> </div>\n";6478print"<table class=\"projects_list\">\n".6479"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n";6480if($ownerand not$omit_owner) {6481print"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";6482}6483if(defined$cd{'rfc2822'}) {6484print"<tr id=\"metadata_lchange\"><td>last change</td>".6485"<td>".format_timestamp_html(\%cd)."</td></tr>\n";6486}64876488# use per project git URL list in $projectroot/$project/cloneurl6489# or make project git URL from git base URL and project name6490my$url_tag="URL";6491my@url_list= git_get_project_url_list($project);6492@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;6493foreachmy$git_url(@url_list) {6494next unless$git_url;6495print format_repo_url($url_tag,$git_url);6496$url_tag="";6497}64986499# Tag cloud6500my$show_ctags= gitweb_check_feature('ctags');6501if($show_ctags) {6502my$ctags= git_get_project_ctags($project);6503if(%$ctags) {6504# without ability to add tags, don't show if there are none6505my$cloud= git_populate_project_tagcloud($ctags);6506print"<tr id=\"metadata_ctags\">".6507"<td>content tags</td>".6508"<td>".git_show_project_tagcloud($cloud,48)."</td>".6509"</tr>\n";6510}6511}65126513print"</table>\n";65146515# If XSS prevention is on, we don't include README.html.6516# TODO: Allow a readme in some safe format.6517if(!$prevent_xss&& -s "$projectroot/$project/README.html") {6518print"<div class=\"title\">readme</div>\n".6519"<div class=\"readme\">\n";6520 insert_file("$projectroot/$project/README.html");6521print"\n</div>\n";# class="readme"6522}65236524# we need to request one more than 16 (0..15) to check if6525# those 16 are all6526my@commitlist=$head? parse_commits($head,17) : ();6527if(@commitlist) {6528 git_print_header_div('shortlog');6529 git_shortlog_body(\@commitlist,0,15,$refs,6530$#commitlist<=15?undef:6531$cgi->a({-href => href(action=>"shortlog")},"..."));6532}65336534if(@taglist) {6535 git_print_header_div('tags');6536 git_tags_body(\@taglist,0,15,6537$#taglist<=15?undef:6538$cgi->a({-href => href(action=>"tags")},"..."));6539}65406541if(@headlist) {6542 git_print_header_div('heads');6543 git_heads_body(\@headlist,$head,0,15,6544$#headlist<=15?undef:6545$cgi->a({-href => href(action=>"heads")},"..."));6546}65476548if(%remotedata) {6549 git_print_header_div('remotes');6550 git_remotes_body(\%remotedata,15,$head);6551}65526553if(@forklist) {6554 git_print_header_div('forks');6555 git_project_list_body(\@forklist,'age',0,15,6556$#forklist<=15?undef:6557$cgi->a({-href => href(action=>"forks")},"..."),6558'no_header');6559}65606561 git_footer_html();6562}65636564sub git_tag {6565my%tag= parse_tag($hash);65666567if(!%tag) {6568 die_error(404,"Unknown tag object");6569}65706571my$head= git_get_head_hash($project);6572 git_header_html();6573 git_print_page_nav('','',$head,undef,$head);6574 git_print_header_div('commit', esc_html($tag{'name'}),$hash);6575print"<div class=\"title_text\">\n".6576"<table class=\"object_header\">\n".6577"<tr>\n".6578"<td>object</td>\n".6579"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},6580$tag{'object'}) ."</td>\n".6581"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},6582$tag{'type'}) ."</td>\n".6583"</tr>\n";6584if(defined($tag{'author'})) {6585 git_print_authorship_rows(\%tag,'author');6586}6587print"</table>\n\n".6588"</div>\n";6589print"<div class=\"page_body\">";6590my$comment=$tag{'comment'};6591foreachmy$line(@$comment) {6592chomp$line;6593print esc_html($line, -nbsp=>1) ."<br/>\n";6594}6595print"</div>\n";6596 git_footer_html();6597}65986599sub git_blame_common {6600my$format=shift||'porcelain';6601if($formateq'porcelain'&&$input_params{'javascript'}) {6602$format='incremental';6603$action='blame_incremental';# for page title etc6604}66056606# permissions6607 gitweb_check_feature('blame')6608or die_error(403,"Blame view not allowed");66096610# error checking6611 die_error(400,"No file name given")unless$file_name;6612$hash_base||= git_get_head_hash($project);6613 die_error(404,"Couldn't find base commit")unless$hash_base;6614my%co= parse_commit($hash_base)6615or die_error(404,"Commit not found");6616my$ftype="blob";6617if(!defined$hash) {6618$hash= git_get_hash_by_path($hash_base,$file_name,"blob")6619or die_error(404,"Error looking up file");6620}else{6621$ftype= git_get_type($hash);6622if($ftype!~"blob") {6623 die_error(400,"Object is not a blob");6624}6625}66266627my$fd;6628if($formateq'incremental') {6629# get file contents (as base)6630open$fd,"-|", git_cmd(),'cat-file','blob',$hash6631or die_error(500,"Open git-cat-file failed");6632}elsif($formateq'data') {6633# run git-blame --incremental6634open$fd,"-|", git_cmd(),"blame","--incremental",6635$hash_base,"--",$file_name6636or die_error(500,"Open git-blame --incremental failed");6637}else{6638# run git-blame --porcelain6639open$fd,"-|", git_cmd(),"blame",'-p',6640$hash_base,'--',$file_name6641or die_error(500,"Open git-blame --porcelain failed");6642}6643binmode$fd,':utf8';66446645# incremental blame data returns early6646if($formateq'data') {6647print$cgi->header(6648-type=>"text/plain", -charset =>"utf-8",6649-status=>"200 OK");6650local$| =1;# output autoflush6651while(my$line= <$fd>) {6652print to_utf8($line);6653}6654close$fd6655or print"ERROR$!\n";66566657print'END';6658if(defined$t0&& gitweb_check_feature('timed')) {6659print' '.6660 tv_interval($t0, [ gettimeofday() ]).6661' '.$number_of_git_cmds;6662}6663print"\n";66646665return;6666}66676668# page header6669 git_header_html();6670my$formats_nav=6671$cgi->a({-href => href(action=>"blob", -replay=>1)},6672"blob") .6673" | ";6674if($formateq'incremental') {6675$formats_nav.=6676$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},6677"blame") ." (non-incremental)";6678}else{6679$formats_nav.=6680$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},6681"blame") ." (incremental)";6682}6683$formats_nav.=6684" | ".6685$cgi->a({-href => href(action=>"history", -replay=>1)},6686"history") .6687" | ".6688$cgi->a({-href => href(action=>$action, file_name=>$file_name)},6689"HEAD");6690 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6691 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6692 git_print_page_path($file_name,$ftype,$hash_base);66936694# page body6695if($formateq'incremental') {6696print"<noscript>\n<div class=\"error\"><center><b>\n".6697"This page requires JavaScript to run.\nUse ".6698$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},6699'this page').6700" instead.\n".6701"</b></center></div>\n</noscript>\n";67026703print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;6704}67056706print qq!<div class="page_body">\n!;6707print qq!<div id="progress_info">.../ ...</div>\n!6708if($formateq'incremental');6709print qq!<table id="blame_table"class="blame" width="100%">\n!.6710#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.6711 qq!<thead>\n!.6712 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.6713 qq!</thead>\n!.6714 qq!<tbody>\n!;67156716my@rev_color=qw(light dark);6717my$num_colors=scalar(@rev_color);6718my$current_color=0;67196720if($formateq'incremental') {6721my$color_class=$rev_color[$current_color];67226723#contents of a file6724my$linenr=0;6725 LINE:6726while(my$line= <$fd>) {6727chomp$line;6728$linenr++;67296730print qq!<tr id="l$linenr"class="$color_class">!.6731 qq!<td class="sha1"><a href=""> </a></td>!.6732 qq!<td class="linenr">!.6733 qq!<a class="linenr" href="">$linenr</a></td>!;6734print qq!<td class="pre">! . esc_html($line) ."</td>\n";6735print qq!</tr>\n!;6736}67376738}else{# porcelain, i.e. ordinary blame6739my%metainfo= ();# saves information about commits67406741# blame data6742 LINE:6743while(my$line= <$fd>) {6744chomp$line;6745# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]6746# no <lines in group> for subsequent lines in group of lines6747my($full_rev,$orig_lineno,$lineno,$group_size) =6748($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);6749if(!exists$metainfo{$full_rev}) {6750$metainfo{$full_rev} = {'nprevious'=>0};6751}6752my$meta=$metainfo{$full_rev};6753my$data;6754while($data= <$fd>) {6755chomp$data;6756last if($data=~s/^\t//);# contents of line6757if($data=~/^(\S+)(?: (.*))?$/) {6758$meta->{$1} =$2unlessexists$meta->{$1};6759}6760if($data=~/^previous /) {6761$meta->{'nprevious'}++;6762}6763}6764my$short_rev=substr($full_rev,0,8);6765my$author=$meta->{'author'};6766my%date=6767 parse_date($meta->{'author-time'},$meta->{'author-tz'});6768my$date=$date{'iso-tz'};6769if($group_size) {6770$current_color= ($current_color+1) %$num_colors;6771}6772my$tr_class=$rev_color[$current_color];6773$tr_class.=' boundary'if(exists$meta->{'boundary'});6774$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);6775$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);6776print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";6777if($group_size) {6778print"<td class=\"sha1\"";6779print" title=\"". esc_html($author) .",$date\"";6780print" rowspan=\"$group_size\""if($group_size>1);6781print">";6782print$cgi->a({-href => href(action=>"commit",6783 hash=>$full_rev,6784 file_name=>$file_name)},6785 esc_html($short_rev));6786if($group_size>=2) {6787my@author_initials= ($author=~/\b([[:upper:]])\B/g);6788if(@author_initials) {6789print"<br />".6790 esc_html(join('',@author_initials));6791# or join('.', ...)6792}6793}6794print"</td>\n";6795}6796# 'previous' <sha1 of parent commit> <filename at commit>6797if(exists$meta->{'previous'} &&6798$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {6799$meta->{'parent'} =$1;6800$meta->{'file_parent'} = unquote($2);6801}6802my$linenr_commit=6803exists($meta->{'parent'}) ?6804$meta->{'parent'} :$full_rev;6805my$linenr_filename=6806exists($meta->{'file_parent'}) ?6807$meta->{'file_parent'} : unquote($meta->{'filename'});6808my$blamed= href(action =>'blame',6809 file_name =>$linenr_filename,6810 hash_base =>$linenr_commit);6811print"<td class=\"linenr\">";6812print$cgi->a({ -href =>"$blamed#l$orig_lineno",6813-class=>"linenr"},6814 esc_html($lineno));6815print"</td>";6816print"<td class=\"pre\">". esc_html($data) ."</td>\n";6817print"</tr>\n";6818}# end while68196820}68216822# footer6823print"</tbody>\n".6824"</table>\n";# class="blame"6825print"</div>\n";# class="blame_body"6826close$fd6827or print"Reading blob failed\n";68286829 git_footer_html();6830}68316832sub git_blame {6833 git_blame_common();6834}68356836sub git_blame_incremental {6837 git_blame_common('incremental');6838}68396840sub git_blame_data {6841 git_blame_common('data');6842}68436844sub git_tags {6845my$head= git_get_head_hash($project);6846 git_header_html();6847 git_print_page_nav('','',$head,undef,$head,format_ref_views('tags'));6848 git_print_header_div('summary',$project);68496850my@tagslist= git_get_tags_list();6851if(@tagslist) {6852 git_tags_body(\@tagslist);6853}6854 git_footer_html();6855}68566857sub git_heads {6858my$head= git_get_head_hash($project);6859 git_header_html();6860 git_print_page_nav('','',$head,undef,$head,format_ref_views('heads'));6861 git_print_header_div('summary',$project);68626863my@headslist= git_get_heads_list();6864if(@headslist) {6865 git_heads_body(\@headslist,$head);6866}6867 git_footer_html();6868}68696870# used both for single remote view and for list of all the remotes6871sub git_remotes {6872 gitweb_check_feature('remote_heads')6873or die_error(403,"Remote heads view is disabled");68746875my$head= git_get_head_hash($project);6876my$remote=$input_params{'hash'};68776878my$remotedata= git_get_remotes_list($remote);6879 die_error(500,"Unable to get remote information")unlessdefined$remotedata;68806881unless(%$remotedata) {6882 die_error(404,defined$remote?6883"Remote$remotenot found":6884"No remotes found");6885}68866887 git_header_html(undef,undef, -action_extra =>$remote);6888 git_print_page_nav('','',$head,undef,$head,6889 format_ref_views($remote?'':'remotes'));68906891 fill_remote_heads($remotedata);6892if(defined$remote) {6893 git_print_header_div('remotes',"$remoteremote for$project");6894 git_remote_block($remote,$remotedata->{$remote},undef,$head);6895}else{6896 git_print_header_div('summary',"$projectremotes");6897 git_remotes_body($remotedata,undef,$head);6898}68996900 git_footer_html();6901}69026903sub git_blob_plain {6904my$type=shift;6905my$expires;69066907if(!defined$hash) {6908if(defined$file_name) {6909my$base=$hash_base|| git_get_head_hash($project);6910$hash= git_get_hash_by_path($base,$file_name,"blob")6911or die_error(404,"Cannot find file");6912}else{6913 die_error(400,"No file name defined");6914}6915}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6916# blobs defined by non-textual hash id's can be cached6917$expires="+1d";6918}69196920open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6921or die_error(500,"Open git-cat-file blob '$hash' failed");69226923# content-type (can include charset)6924$type= blob_contenttype($fd,$file_name,$type);69256926# "save as" filename, even when no $file_name is given6927my$save_as="$hash";6928if(defined$file_name) {6929$save_as=$file_name;6930}elsif($type=~m/^text\//) {6931$save_as.='.txt';6932}69336934# With XSS prevention on, blobs of all types except a few known safe6935# ones are served with "Content-Disposition: attachment" to make sure6936# they don't run in our security domain. For certain image types,6937# blob view writes an <img> tag referring to blob_plain view, and we6938# want to be sure not to break that by serving the image as an6939# attachment (though Firefox 3 doesn't seem to care).6940my$sandbox=$prevent_xss&&6941$type!~m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;69426943# serve text/* as text/plain6944if($prevent_xss&&6945($type=~m!^text/[a-z]+\b(.*)$!||6946($type=~m!^[a-z]+/[a-z]\+xml\b(.*)$!&& -T $fd))) {6947my$rest=$1;6948$rest=defined$rest?$rest:'';6949$type="text/plain$rest";6950}69516952print$cgi->header(6953-type =>$type,6954-expires =>$expires,6955-content_disposition =>6956($sandbox?'attachment':'inline')6957.'; filename="'.$save_as.'"');6958local$/=undef;6959binmode STDOUT,':raw';6960print<$fd>;6961binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi6962close$fd;6963}69646965sub git_blob {6966my$expires;69676968if(!defined$hash) {6969if(defined$file_name) {6970my$base=$hash_base|| git_get_head_hash($project);6971$hash= git_get_hash_by_path($base,$file_name,"blob")6972or die_error(404,"Cannot find file");6973}else{6974 die_error(400,"No file name defined");6975}6976}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6977# blobs defined by non-textual hash id's can be cached6978$expires="+1d";6979}69806981my$have_blame= gitweb_check_feature('blame');6982open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6983or die_error(500,"Couldn't cat$file_name,$hash");6984my$mimetype= blob_mimetype($fd,$file_name);6985# use 'blob_plain' (aka 'raw') view for files that cannot be displayed6986if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {6987close$fd;6988return git_blob_plain($mimetype);6989}6990# we can have blame only for text/* mimetype6991$have_blame&&= ($mimetype=~m!^text/!);69926993my$highlight= gitweb_check_feature('highlight');6994my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);6995$fd= run_highlighter($fd,$highlight,$syntax)6996if$syntax;69976998 git_header_html(undef,$expires);6999my$formats_nav='';7000if(defined$hash_base&& (my%co= parse_commit($hash_base))) {7001if(defined$file_name) {7002if($have_blame) {7003$formats_nav.=7004$cgi->a({-href => href(action=>"blame", -replay=>1)},7005"blame") .7006" | ";7007}7008$formats_nav.=7009$cgi->a({-href => href(action=>"history", -replay=>1)},7010"history") .7011" | ".7012$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},7013"raw") .7014" | ".7015$cgi->a({-href => href(action=>"blob",7016 hash_base=>"HEAD", file_name=>$file_name)},7017"HEAD");7018}else{7019$formats_nav.=7020$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},7021"raw");7022}7023 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);7024 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);7025}else{7026print"<div class=\"page_nav\">\n".7027"<br/><br/></div>\n".7028"<div class=\"title\">".esc_html($hash)."</div>\n";7029}7030 git_print_page_path($file_name,"blob",$hash_base);7031print"<div class=\"page_body\">\n";7032if($mimetype=~m!^image/!) {7033print qq!<img type="!.esc_attr($mimetype).qq!"!;7034if($file_name) {7035print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;7036}7037print qq! src="! .7038 href(action=>"blob_plain", hash=>$hash,7039 hash_base=>$hash_base, file_name=>$file_name) .7040 qq!"/>\n!;7041}else{7042my$nr;7043while(my$line= <$fd>) {7044chomp$line;7045$nr++;7046$line= untabify($line);7047printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,7048$nr, esc_attr(href(-replay =>1)),$nr,$nr,7049$syntax? sanitize($line) : esc_html($line, -nbsp=>1);7050}7051}7052close$fd7053or print"Reading blob failed.\n";7054print"</div>";7055 git_footer_html();7056}70577058sub git_tree {7059if(!defined$hash_base) {7060$hash_base="HEAD";7061}7062if(!defined$hash) {7063if(defined$file_name) {7064$hash= git_get_hash_by_path($hash_base,$file_name,"tree");7065}else{7066$hash=$hash_base;7067}7068}7069 die_error(404,"No such tree")unlessdefined($hash);70707071my$show_sizes= gitweb_check_feature('show-sizes');7072my$have_blame= gitweb_check_feature('blame');70737074my@entries= ();7075{7076local$/="\0";7077open my$fd,"-|", git_cmd(),"ls-tree",'-z',7078($show_sizes?'-l': ()),@extra_options,$hash7079or die_error(500,"Open git-ls-tree failed");7080@entries=map{chomp;$_} <$fd>;7081close$fd7082or die_error(404,"Reading tree failed");7083}70847085my$refs= git_get_references();7086my$ref= format_ref_marker($refs,$hash_base);7087 git_header_html();7088my$basedir='';7089if(defined$hash_base&& (my%co= parse_commit($hash_base))) {7090my@views_nav= ();7091if(defined$file_name) {7092push@views_nav,7093$cgi->a({-href => href(action=>"history", -replay=>1)},7094"history"),7095$cgi->a({-href => href(action=>"tree",7096 hash_base=>"HEAD", file_name=>$file_name)},7097"HEAD"),7098}7099my$snapshot_links= format_snapshot_links($hash);7100if(defined$snapshot_links) {7101# FIXME: Should be available when we have no hash base as well.7102push@views_nav,$snapshot_links;7103}7104 git_print_page_nav('tree','',$hash_base,undef,undef,7105join(' | ',@views_nav));7106 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);7107}else{7108undef$hash_base;7109print"<div class=\"page_nav\">\n";7110print"<br/><br/></div>\n";7111print"<div class=\"title\">".esc_html($hash)."</div>\n";7112}7113if(defined$file_name) {7114$basedir=$file_name;7115if($basedirne''&&substr($basedir, -1)ne'/') {7116$basedir.='/';7117}7118 git_print_page_path($file_name,'tree',$hash_base);7119}7120print"<div class=\"page_body\">\n";7121print"<table class=\"tree\">\n";7122my$alternate=1;7123# '..' (top directory) link if possible7124if(defined$hash_base&&7125defined$file_name&&$file_name=~m![^/]+$!) {7126if($alternate) {7127print"<tr class=\"dark\">\n";7128}else{7129print"<tr class=\"light\">\n";7130}7131$alternate^=1;71327133my$up=$file_name;7134$up=~s!/?[^/]+$!!;7135undef$upunless$up;7136# based on git_print_tree_entry7137print'<td class="mode">'. mode_str('040000') ."</td>\n";7138print'<td class="size"> </td>'."\n"if$show_sizes;7139print'<td class="list">';7140print$cgi->a({-href => href(action=>"tree",7141 hash_base=>$hash_base,7142 file_name=>$up)},7143"..");7144print"</td>\n";7145print"<td class=\"link\"></td>\n";71467147print"</tr>\n";7148}7149foreachmy$line(@entries) {7150my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);71517152if($alternate) {7153print"<tr class=\"dark\">\n";7154}else{7155print"<tr class=\"light\">\n";7156}7157$alternate^=1;71587159 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);71607161print"</tr>\n";7162}7163print"</table>\n".7164"</div>";7165 git_footer_html();7166}71677168sub snapshot_name {7169my($project,$hash) =@_;71707171# path/to/project.git -> project7172# path/to/project/.git -> project7173my$name= to_utf8($project);7174$name=~ s,([^/])/*\.git$,$1,;7175$name= basename($name);7176# sanitize name7177$name=~s/[[:cntrl:]]/?/g;71787179my$ver=$hash;7180if($hash=~/^[0-9a-fA-F]+$/) {7181# shorten SHA-1 hash7182my$full_hash= git_get_full_hash($project,$hash);7183if($full_hash=~/^$hash/&&length($hash) >7) {7184$ver= git_get_short_hash($project,$hash);7185}7186}elsif($hash=~m!^refs/tags/(.*)$!) {7187# tags don't need shortened SHA-1 hash7188$ver=$1;7189}else{7190# branches and other need shortened SHA-1 hash7191if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {7192$ver=$1;7193}7194$ver.='-'. git_get_short_hash($project,$hash);7195}7196# in case of hierarchical branch names7197$ver=~s!/!.!g;71987199# name = project-version_string7200$name="$name-$ver";72017202returnwantarray? ($name,$name) :$name;7203}72047205sub exit_if_unmodified_since {7206my($latest_epoch) =@_;7207our$cgi;72087209my$if_modified=$cgi->http('IF_MODIFIED_SINCE');7210if(defined$if_modified) {7211my$since;7212if(eval{require HTTP::Date;1; }) {7213$since= HTTP::Date::str2time($if_modified);7214}elsif(eval{require Time::ParseDate;1; }) {7215$since= Time::ParseDate::parsedate($if_modified, GMT =>1);7216}7217if(defined$since&&$latest_epoch<=$since) {7218my%latest_date= parse_date($latest_epoch);7219print$cgi->header(7220-last_modified =>$latest_date{'rfc2822'},7221-status =>'304 Not Modified');7222goto DONE_GITWEB;7223}7224}7225}72267227sub git_snapshot {7228my$format=$input_params{'snapshot_format'};7229if(!@snapshot_fmts) {7230 die_error(403,"Snapshots not allowed");7231}7232# default to first supported snapshot format7233$format||=$snapshot_fmts[0];7234if($format!~m/^[a-z0-9]+$/) {7235 die_error(400,"Invalid snapshot format parameter");7236}elsif(!exists($known_snapshot_formats{$format})) {7237 die_error(400,"Unknown snapshot format");7238}elsif($known_snapshot_formats{$format}{'disabled'}) {7239 die_error(403,"Snapshot format not allowed");7240}elsif(!grep($_eq$format,@snapshot_fmts)) {7241 die_error(403,"Unsupported snapshot format");7242}72437244my$type= git_get_type("$hash^{}");7245if(!$type) {7246 die_error(404,'Object does not exist');7247}elsif($typeeq'blob') {7248 die_error(400,'Object is not a tree-ish');7249}72507251my($name,$prefix) = snapshot_name($project,$hash);7252my$filename="$name$known_snapshot_formats{$format}{'suffix'}";72537254my%co= parse_commit($hash);7255 exit_if_unmodified_since($co{'committer_epoch'})if%co;72567257my$cmd= quote_command(7258 git_cmd(),'archive',7259"--format=$known_snapshot_formats{$format}{'format'}",7260"--prefix=$prefix/",$hash);7261if(exists$known_snapshot_formats{$format}{'compressor'}) {7262$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});7263}72647265$filename=~s/(["\\])/\\$1/g;7266my%latest_date;7267if(%co) {7268%latest_date= parse_date($co{'committer_epoch'},$co{'committer_tz'});7269}72707271print$cgi->header(7272-type =>$known_snapshot_formats{$format}{'type'},7273-content_disposition =>'inline; filename="'.$filename.'"',7274%co? (-last_modified =>$latest_date{'rfc2822'}) : (),7275-status =>'200 OK');72767277open my$fd,"-|",$cmd7278or die_error(500,"Execute git-archive failed");7279binmode STDOUT,':raw';7280print<$fd>;7281binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi7282close$fd;7283}72847285sub git_log_generic {7286my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;72877288my$head= git_get_head_hash($project);7289if(!defined$base) {7290$base=$head;7291}7292if(!defined$page) {7293$page=0;7294}7295my$refs= git_get_references();72967297my$commit_hash=$base;7298if(defined$parent) {7299$commit_hash="$parent..$base";7300}7301my@commitlist=7302 parse_commits($commit_hash,101, (100*$page),7303defined$file_name? ($file_name,"--full-history") : ());73047305my$ftype;7306if(!defined$file_hash&&defined$file_name) {7307# some commits could have deleted file in question,7308# and not have it in tree, but one of them has to have it7309for(my$i=0;$i<@commitlist;$i++) {7310$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);7311last ifdefined$file_hash;7312}7313}7314if(defined$file_hash) {7315$ftype= git_get_type($file_hash);7316}7317if(defined$file_name&& !defined$ftype) {7318 die_error(500,"Unknown type of object");7319}7320my%co;7321if(defined$file_name) {7322%co= parse_commit($base)7323or die_error(404,"Unknown commit object");7324}732573267327my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);7328my$next_link='';7329if($#commitlist>=100) {7330$next_link=7331$cgi->a({-href => href(-replay=>1, page=>$page+1),7332-accesskey =>"n", -title =>"Alt-n"},"next");7333}7334my$patch_max= gitweb_get_feature('patches');7335if($patch_max&& !defined$file_name) {7336if($patch_max<0||@commitlist<=$patch_max) {7337$paging_nav.=" ⋅ ".7338$cgi->a({-href => href(action=>"patches", -replay=>1)},7339"patches");7340}7341}73427343 git_header_html();7344 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);7345if(defined$file_name) {7346 git_print_header_div('commit', esc_html($co{'title'}),$base);7347}else{7348 git_print_header_div('summary',$project)7349}7350 git_print_page_path($file_name,$ftype,$hash_base)7351if(defined$file_name);73527353$body_subr->(\@commitlist,0,99,$refs,$next_link,7354$file_name,$file_hash,$ftype);73557356 git_footer_html();7357}73587359sub git_log {7360 git_log_generic('log', \&git_log_body,7361$hash,$hash_parent);7362}73637364sub git_commit {7365$hash||=$hash_base||"HEAD";7366my%co= parse_commit($hash)7367or die_error(404,"Unknown commit object");73687369my$parent=$co{'parent'};7370my$parents=$co{'parents'};# listref73717372# we need to prepare $formats_nav before any parameter munging7373my$formats_nav;7374if(!defined$parent) {7375# --root commitdiff7376$formats_nav.='(initial)';7377}elsif(@$parents==1) {7378# single parent commit7379$formats_nav.=7380'(parent: '.7381$cgi->a({-href => href(action=>"commit",7382 hash=>$parent)},7383 esc_html(substr($parent,0,7))) .7384')';7385}else{7386# merge commit7387$formats_nav.=7388'(merge: '.7389join(' ',map{7390$cgi->a({-href => href(action=>"commit",7391 hash=>$_)},7392 esc_html(substr($_,0,7)));7393}@$parents) .7394')';7395}7396if(gitweb_check_feature('patches') &&@$parents<=1) {7397$formats_nav.=" | ".7398$cgi->a({-href => href(action=>"patch", -replay=>1)},7399"patch");7400}74017402if(!defined$parent) {7403$parent="--root";7404}7405my@difftree;7406open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",7407@diff_opts,7408(@$parents<=1?$parent:'-c'),7409$hash,"--"7410or die_error(500,"Open git-diff-tree failed");7411@difftree=map{chomp;$_} <$fd>;7412close$fdor die_error(404,"Reading git-diff-tree failed");74137414# non-textual hash id's can be cached7415my$expires;7416if($hash=~m/^[0-9a-fA-F]{40}$/) {7417$expires="+1d";7418}7419my$refs= git_get_references();7420my$ref= format_ref_marker($refs,$co{'id'});74217422 git_header_html(undef,$expires);7423 git_print_page_nav('commit','',7424$hash,$co{'tree'},$hash,7425$formats_nav);74267427if(defined$co{'parent'}) {7428 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);7429}else{7430 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);7431}7432print"<div class=\"title_text\">\n".7433"<table class=\"object_header\">\n";7434 git_print_authorship_rows(\%co);7435print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";7436print"<tr>".7437"<td>tree</td>".7438"<td class=\"sha1\">".7439$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),7440class=>"list"},$co{'tree'}) .7441"</td>".7442"<td class=\"link\">".7443$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},7444"tree");7445my$snapshot_links= format_snapshot_links($hash);7446if(defined$snapshot_links) {7447print" | ".$snapshot_links;7448}7449print"</td>".7450"</tr>\n";74517452foreachmy$par(@$parents) {7453print"<tr>".7454"<td>parent</td>".7455"<td class=\"sha1\">".7456$cgi->a({-href => href(action=>"commit", hash=>$par),7457class=>"list"},$par) .7458"</td>".7459"<td class=\"link\">".7460$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .7461" | ".7462$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .7463"</td>".7464"</tr>\n";7465}7466print"</table>".7467"</div>\n";74687469print"<div class=\"page_body\">\n";7470 git_print_log($co{'comment'});7471print"</div>\n";74727473 git_difftree_body(\@difftree,$hash,@$parents);74747475 git_footer_html();7476}74777478sub git_object {7479# object is defined by:7480# - hash or hash_base alone7481# - hash_base and file_name7482my$type;74837484# - hash or hash_base alone7485if($hash|| ($hash_base&& !defined$file_name)) {7486my$object_id=$hash||$hash_base;74877488open my$fd,"-|", quote_command(7489 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'7490or die_error(404,"Object does not exist");7491$type= <$fd>;7492chomp$type;7493close$fd7494or die_error(404,"Object does not exist");74957496# - hash_base and file_name7497}elsif($hash_base&&defined$file_name) {7498$file_name=~ s,/+$,,;74997500system(git_cmd(),"cat-file",'-e',$hash_base) ==07501or die_error(404,"Base object does not exist");75027503# here errors should not happen7504open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name7505or die_error(500,"Open git-ls-tree failed");7506my$line= <$fd>;7507close$fd;75087509#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'7510unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {7511 die_error(404,"File or directory for given base does not exist");7512}7513$type=$2;7514$hash=$3;7515}else{7516 die_error(400,"Not enough information to find object");7517}75187519print$cgi->redirect(-uri => href(action=>$type, -full=>1,7520 hash=>$hash, hash_base=>$hash_base,7521 file_name=>$file_name),7522-status =>'302 Found');7523}75247525sub git_blobdiff {7526my$format=shift||'html';7527my$diff_style=$input_params{'diff_style'} ||'inline';75287529my$fd;7530my@difftree;7531my%diffinfo;7532my$expires;75337534# preparing $fd and %diffinfo for git_patchset_body7535# new style URI7536if(defined$hash_base&&defined$hash_parent_base) {7537if(defined$file_name) {7538# read raw output7539open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7540$hash_parent_base,$hash_base,7541"--", (defined$file_parent?$file_parent: ()),$file_name7542or die_error(500,"Open git-diff-tree failed");7543@difftree=map{chomp;$_} <$fd>;7544close$fd7545or die_error(404,"Reading git-diff-tree failed");7546@difftree7547or die_error(404,"Blob diff not found");75487549}elsif(defined$hash&&7550$hash=~/[0-9a-fA-F]{40}/) {7551# try to find filename from $hash75527553# read filtered raw output7554open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7555$hash_parent_base,$hash_base,"--"7556or die_error(500,"Open git-diff-tree failed");7557@difftree=7558# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'7559# $hash == to_id7560grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}7561map{chomp;$_} <$fd>;7562close$fd7563or die_error(404,"Reading git-diff-tree failed");7564@difftree7565or die_error(404,"Blob diff not found");75667567}else{7568 die_error(400,"Missing one of the blob diff parameters");7569}75707571if(@difftree>1) {7572 die_error(400,"Ambiguous blob diff specification");7573}75747575%diffinfo= parse_difftree_raw_line($difftree[0]);7576$file_parent||=$diffinfo{'from_file'} ||$file_name;7577$file_name||=$diffinfo{'to_file'};75787579$hash_parent||=$diffinfo{'from_id'};7580$hash||=$diffinfo{'to_id'};75817582# non-textual hash id's can be cached7583if($hash_base=~m/^[0-9a-fA-F]{40}$/&&7584$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {7585$expires='+1d';7586}75877588# open patch output7589open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7590'-p', ($formateq'html'?"--full-index": ()),7591$hash_parent_base,$hash_base,7592"--", (defined$file_parent?$file_parent: ()),$file_name7593or die_error(500,"Open git-diff-tree failed");7594}75957596# old/legacy style URI -- not generated anymore since 1.4.3.7597if(!%diffinfo) {7598 die_error('404 Not Found',"Missing one of the blob diff parameters")7599}76007601# header7602if($formateq'html') {7603my$formats_nav=7604$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},7605"raw");7606$formats_nav.= diff_style_nav($diff_style);7607 git_header_html(undef,$expires);7608if(defined$hash_base&& (my%co= parse_commit($hash_base))) {7609 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);7610 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);7611}else{7612print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";7613print"<div class=\"title\">".esc_html("$hashvs$hash_parent")."</div>\n";7614}7615if(defined$file_name) {7616 git_print_page_path($file_name,"blob",$hash_base);7617}else{7618print"<div class=\"page_path\"></div>\n";7619}76207621}elsif($formateq'plain') {7622print$cgi->header(7623-type =>'text/plain',7624-charset =>'utf-8',7625-expires =>$expires,7626-content_disposition =>'inline; filename="'."$file_name".'.patch"');76277628print"X-Git-Url: ".$cgi->self_url() ."\n\n";76297630}else{7631 die_error(400,"Unknown blobdiff format");7632}76337634# patch7635if($formateq'html') {7636print"<div class=\"page_body\">\n";76377638 git_patchset_body($fd,$diff_style,7639[ \%diffinfo],$hash_base,$hash_parent_base);7640close$fd;76417642print"</div>\n";# class="page_body"7643 git_footer_html();76447645}else{7646while(my$line= <$fd>) {7647$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;7648$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;76497650print$line;76517652last if$line=~m!^\+\+\+!;7653}7654local$/=undef;7655print<$fd>;7656close$fd;7657}7658}76597660sub git_blobdiff_plain {7661 git_blobdiff('plain');7662}76637664# assumes that it is added as later part of already existing navigation,7665# so it returns "| foo | bar" rather than just "foo | bar"7666sub diff_style_nav {7667my($diff_style,$is_combined) =@_;7668$diff_style||='inline';76697670return""if($is_combined);76717672my@styles= (inline =>'inline','sidebyside'=>'side by side');7673my%styles=@styles;7674@styles=7675@styles[map{$_*2}0..$#styles/2];76767677returnjoin'',7678map{" | ".$_}7679map{7680$_eq$diff_style?$styles{$_} :7681$cgi->a({-href => href(-replay=>1, diff_style =>$_)},$styles{$_})7682}@styles;7683}76847685sub git_commitdiff {7686my%params=@_;7687my$format=$params{-format} ||'html';7688my$diff_style=$input_params{'diff_style'} ||'inline';76897690my($patch_max) = gitweb_get_feature('patches');7691if($formateq'patch') {7692 die_error(403,"Patch view not allowed")unless$patch_max;7693}76947695$hash||=$hash_base||"HEAD";7696my%co= parse_commit($hash)7697or die_error(404,"Unknown commit object");76987699# choose format for commitdiff for merge7700if(!defined$hash_parent&& @{$co{'parents'}} >1) {7701$hash_parent='--cc';7702}7703# we need to prepare $formats_nav before almost any parameter munging7704my$formats_nav;7705if($formateq'html') {7706$formats_nav=7707$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},7708"raw");7709if($patch_max&& @{$co{'parents'}} <=1) {7710$formats_nav.=" | ".7711$cgi->a({-href => href(action=>"patch", -replay=>1)},7712"patch");7713}7714$formats_nav.= diff_style_nav($diff_style, @{$co{'parents'}} >1);77157716if(defined$hash_parent&&7717$hash_parentne'-c'&&$hash_parentne'--cc') {7718# commitdiff with two commits given7719my$hash_parent_short=$hash_parent;7720if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {7721$hash_parent_short=substr($hash_parent,0,7);7722}7723$formats_nav.=7724' (from';7725for(my$i=0;$i< @{$co{'parents'}};$i++) {7726if($co{'parents'}[$i]eq$hash_parent) {7727$formats_nav.=' parent '. ($i+1);7728last;7729}7730}7731$formats_nav.=': '.7732$cgi->a({-href => href(-replay=>1,7733 hash=>$hash_parent, hash_base=>undef)},7734 esc_html($hash_parent_short)) .7735')';7736}elsif(!$co{'parent'}) {7737# --root commitdiff7738$formats_nav.=' (initial)';7739}elsif(scalar@{$co{'parents'}} ==1) {7740# single parent commit7741$formats_nav.=7742' (parent: '.7743$cgi->a({-href => href(-replay=>1,7744 hash=>$co{'parent'}, hash_base=>undef)},7745 esc_html(substr($co{'parent'},0,7))) .7746')';7747}else{7748# merge commit7749if($hash_parenteq'--cc') {7750$formats_nav.=' | '.7751$cgi->a({-href => href(-replay=>1,7752 hash=>$hash, hash_parent=>'-c')},7753'combined');7754}else{# $hash_parent eq '-c'7755$formats_nav.=' | '.7756$cgi->a({-href => href(-replay=>1,7757 hash=>$hash, hash_parent=>'--cc')},7758'compact');7759}7760$formats_nav.=7761' (merge: '.7762join(' ',map{7763$cgi->a({-href => href(-replay=>1,7764 hash=>$_, hash_base=>undef)},7765 esc_html(substr($_,0,7)));7766} @{$co{'parents'}} ) .7767')';7768}7769}77707771my$hash_parent_param=$hash_parent;7772if(!defined$hash_parent_param) {7773# --cc for multiple parents, --root for parentless7774$hash_parent_param=7775@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';7776}77777778# read commitdiff7779my$fd;7780my@difftree;7781if($formateq'html') {7782open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7783"--no-commit-id","--patch-with-raw","--full-index",7784$hash_parent_param,$hash,"--"7785or die_error(500,"Open git-diff-tree failed");77867787while(my$line= <$fd>) {7788chomp$line;7789# empty line ends raw part of diff-tree output7790last unless$line;7791push@difftree,scalar parse_difftree_raw_line($line);7792}77937794}elsif($formateq'plain') {7795open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7796'-p',$hash_parent_param,$hash,"--"7797or die_error(500,"Open git-diff-tree failed");7798}elsif($formateq'patch') {7799# For commit ranges, we limit the output to the number of7800# patches specified in the 'patches' feature.7801# For single commits, we limit the output to a single patch,7802# diverging from the git-format-patch default.7803my@commit_spec= ();7804if($hash_parent) {7805if($patch_max>0) {7806push@commit_spec,"-$patch_max";7807}7808push@commit_spec,'-n',"$hash_parent..$hash";7809}else{7810if($params{-single}) {7811push@commit_spec,'-1';7812}else{7813if($patch_max>0) {7814push@commit_spec,"-$patch_max";7815}7816push@commit_spec,"-n";7817}7818push@commit_spec,'--root',$hash;7819}7820open$fd,"-|", git_cmd(),"format-patch",@diff_opts,7821'--encoding=utf8','--stdout',@commit_spec7822or die_error(500,"Open git-format-patch failed");7823}else{7824 die_error(400,"Unknown commitdiff format");7825}78267827# non-textual hash id's can be cached7828my$expires;7829if($hash=~m/^[0-9a-fA-F]{40}$/) {7830$expires="+1d";7831}78327833# write commit message7834if($formateq'html') {7835my$refs= git_get_references();7836my$ref= format_ref_marker($refs,$co{'id'});78377838 git_header_html(undef,$expires);7839 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);7840 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);7841print"<div class=\"title_text\">\n".7842"<table class=\"object_header\">\n";7843 git_print_authorship_rows(\%co);7844print"</table>".7845"</div>\n";7846print"<div class=\"page_body\">\n";7847if(@{$co{'comment'}} >1) {7848print"<div class=\"log\">\n";7849 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);7850print"</div>\n";# class="log"7851}78527853}elsif($formateq'plain') {7854my$refs= git_get_references("tags");7855my$tagname= git_get_rev_name_tags($hash);7856my$filename= basename($project) ."-$hash.patch";78577858print$cgi->header(7859-type =>'text/plain',7860-charset =>'utf-8',7861-expires =>$expires,7862-content_disposition =>'inline; filename="'."$filename".'"');7863my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});7864print"From: ". to_utf8($co{'author'}) ."\n";7865print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";7866print"Subject: ". to_utf8($co{'title'}) ."\n";78677868print"X-Git-Tag:$tagname\n"if$tagname;7869print"X-Git-Url: ".$cgi->self_url() ."\n\n";78707871foreachmy$line(@{$co{'comment'}}) {7872print to_utf8($line) ."\n";7873}7874print"---\n\n";7875}elsif($formateq'patch') {7876my$filename= basename($project) ."-$hash.patch";78777878print$cgi->header(7879-type =>'text/plain',7880-charset =>'utf-8',7881-expires =>$expires,7882-content_disposition =>'inline; filename="'."$filename".'"');7883}78847885# write patch7886if($formateq'html') {7887my$use_parents= !defined$hash_parent||7888$hash_parenteq'-c'||$hash_parenteq'--cc';7889 git_difftree_body(\@difftree,$hash,7890$use_parents? @{$co{'parents'}} :$hash_parent);7891print"<br/>\n";78927893 git_patchset_body($fd,$diff_style,7894 \@difftree,$hash,7895$use_parents? @{$co{'parents'}} :$hash_parent);7896close$fd;7897print"</div>\n";# class="page_body"7898 git_footer_html();78997900}elsif($formateq'plain') {7901local$/=undef;7902print<$fd>;7903close$fd7904or print"Reading git-diff-tree failed\n";7905}elsif($formateq'patch') {7906local$/=undef;7907print<$fd>;7908close$fd7909or print"Reading git-format-patch failed\n";7910}7911}79127913sub git_commitdiff_plain {7914 git_commitdiff(-format =>'plain');7915}79167917# format-patch-style patches7918sub git_patch {7919 git_commitdiff(-format =>'patch', -single =>1);7920}79217922sub git_patches {7923 git_commitdiff(-format =>'patch');7924}79257926sub git_history {7927 git_log_generic('history', \&git_history_body,7928$hash_base,$hash_parent_base,7929$file_name,$hash);7930}79317932sub git_search {7933$searchtype||='commit';79347935# check if appropriate features are enabled7936 gitweb_check_feature('search')7937or die_error(403,"Search is disabled");7938if($searchtypeeq'pickaxe') {7939# pickaxe may take all resources of your box and run for several minutes7940# with every query - so decide by yourself how public you make this feature7941 gitweb_check_feature('pickaxe')7942or die_error(403,"Pickaxe search is disabled");7943}7944if($searchtypeeq'grep') {7945# grep search might be potentially CPU-intensive, too7946 gitweb_check_feature('grep')7947or die_error(403,"Grep search is disabled");7948}79497950if(!defined$searchtext) {7951 die_error(400,"Text field is empty");7952}7953if(!defined$hash) {7954$hash= git_get_head_hash($project);7955}7956my%co= parse_commit($hash);7957if(!%co) {7958 die_error(404,"Unknown commit object");7959}7960if(!defined$page) {7961$page=0;7962}79637964if($searchtypeeq'commit'||7965$searchtypeeq'author'||7966$searchtypeeq'committer') {7967 git_search_message(%co);7968}elsif($searchtypeeq'pickaxe') {7969 git_search_changes(%co);7970}elsif($searchtypeeq'grep') {7971 git_search_files(%co);7972}else{7973 die_error(400,"Unknown search type");7974}7975}79767977sub git_search_help {7978 git_header_html();7979 git_print_page_nav('','',$hash,$hash,$hash);7980print<<EOT;7981<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without7982regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,7983the pattern entered is recognized as the POSIX extended7984<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case7985insensitive).</p>7986<dl>7987<dt><b>commit</b></dt>7988<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>7989EOT7990my$have_grep= gitweb_check_feature('grep');7991if($have_grep) {7992print<<EOT;7993<dt><b>grep</b></dt>7994<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing7995 a different one) are searched for the given pattern. On large trees, this search can take7996a while and put some strain on the server, so please use it with some consideration. Note that7997due to git-grep peculiarity, currently if regexp mode is turned off, the matches are7998case-sensitive.</dd>7999EOT8000}8001print<<EOT;8002<dt><b>author</b></dt>8003<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>8004<dt><b>committer</b></dt>8005<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>8006EOT8007my$have_pickaxe= gitweb_check_feature('pickaxe');8008if($have_pickaxe) {8009print<<EOT;8010<dt><b>pickaxe</b></dt>8011<dd>All commits that caused the string to appear or disappear from any file (changes that8012added, removed or "modified" the string) will be listed. This search can take a while and8013takes a lot of strain on the server, so please use it wisely. Note that since you may be8014interested even in changes just changing the case as well, this search is case sensitive.</dd>8015EOT8016}8017print"</dl>\n";8018 git_footer_html();8019}80208021sub git_shortlog {8022 git_log_generic('shortlog', \&git_shortlog_body,8023$hash,$hash_parent);8024}80258026## ......................................................................8027## feeds (RSS, Atom; OPML)80288029sub git_feed {8030my$format=shift||'atom';8031my$have_blame= gitweb_check_feature('blame');80328033# Atom: http://www.atomenabled.org/developers/syndication/8034# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ8035if($formatne'rss'&&$formatne'atom') {8036 die_error(400,"Unknown web feed format");8037}80388039# log/feed of current (HEAD) branch, log of given branch, history of file/directory8040my$head=$hash||'HEAD';8041my@commitlist= parse_commits($head,150,0,$file_name);80428043my%latest_commit;8044my%latest_date;8045my$content_type="application/$format+xml";8046if(defined$cgi->http('HTTP_ACCEPT') &&8047$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {8048# browser (feed reader) prefers text/xml8049$content_type='text/xml';8050}8051if(defined($commitlist[0])) {8052%latest_commit= %{$commitlist[0]};8053my$latest_epoch=$latest_commit{'committer_epoch'};8054 exit_if_unmodified_since($latest_epoch);8055%latest_date= parse_date($latest_epoch,$latest_commit{'committer_tz'});8056}8057print$cgi->header(8058-type =>$content_type,8059-charset =>'utf-8',8060%latest_date? (-last_modified =>$latest_date{'rfc2822'}) : (),8061-status =>'200 OK');80628063# Optimization: skip generating the body if client asks only8064# for Last-Modified date.8065return if($cgi->request_method()eq'HEAD');80668067# header variables8068my$title="$site_name-$project/$action";8069my$feed_type='log';8070if(defined$hash) {8071$title.=" - '$hash'";8072$feed_type='branch log';8073if(defined$file_name) {8074$title.=" ::$file_name";8075$feed_type='history';8076}8077}elsif(defined$file_name) {8078$title.=" -$file_name";8079$feed_type='history';8080}8081$title.="$feed_type";8082$title= esc_html($title);8083my$descr= git_get_project_description($project);8084if(defined$descr) {8085$descr= esc_html($descr);8086}else{8087$descr="$project".8088($formateq'rss'?'RSS':'Atom') .8089" feed";8090}8091my$owner= git_get_project_owner($project);8092$owner= esc_html($owner);80938094#header8095my$alt_url;8096if(defined$file_name) {8097$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);8098}elsif(defined$hash) {8099$alt_url= href(-full=>1, action=>"log", hash=>$hash);8100}else{8101$alt_url= href(-full=>1, action=>"summary");8102}8103print qq!<?xml version="1.0" encoding="utf-8"?>\n!;8104if($formateq'rss') {8105print<<XML;8106<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">8107<channel>8108XML8109print"<title>$title</title>\n".8110"<link>$alt_url</link>\n".8111"<description>$descr</description>\n".8112"<language>en</language>\n".8113# project owner is responsible for 'editorial' content8114"<managingEditor>$owner</managingEditor>\n";8115if(defined$logo||defined$favicon) {8116# prefer the logo to the favicon, since RSS8117# doesn't allow both8118my$img= esc_url($logo||$favicon);8119print"<image>\n".8120"<url>$img</url>\n".8121"<title>$title</title>\n".8122"<link>$alt_url</link>\n".8123"</image>\n";8124}8125if(%latest_date) {8126print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";8127print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";8128}8129print"<generator>gitweb v.$version/$git_version</generator>\n";8130}elsif($formateq'atom') {8131print<<XML;8132<feed xmlns="http://www.w3.org/2005/Atom">8133XML8134print"<title>$title</title>\n".8135"<subtitle>$descr</subtitle>\n".8136'<link rel="alternate" type="text/html" href="'.8137$alt_url.'" />'."\n".8138'<link rel="self" type="'.$content_type.'" href="'.8139$cgi->self_url() .'" />'."\n".8140"<id>". href(-full=>1) ."</id>\n".8141# use project owner for feed author8142"<author><name>$owner</name></author>\n";8143if(defined$favicon) {8144print"<icon>". esc_url($favicon) ."</icon>\n";8145}8146if(defined$logo) {8147# not twice as wide as tall: 72 x 27 pixels8148print"<logo>". esc_url($logo) ."</logo>\n";8149}8150if(!%latest_date) {8151# dummy date to keep the feed valid until commits trickle in:8152print"<updated>1970-01-01T00:00:00Z</updated>\n";8153}else{8154print"<updated>$latest_date{'iso-8601'}</updated>\n";8155}8156print"<generator version='$version/$git_version'>gitweb</generator>\n";8157}81588159# contents8160for(my$i=0;$i<=$#commitlist;$i++) {8161my%co= %{$commitlist[$i]};8162my$commit=$co{'id'};8163# we read 150, we always show 30 and the ones more recent than 48 hours8164if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {8165last;8166}8167my%cd= parse_date($co{'author_epoch'},$co{'author_tz'});81688169# get list of changed files8170open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,8171$co{'parent'} ||"--root",8172$co{'id'},"--", (defined$file_name?$file_name: ())8173ornext;8174my@difftree=map{chomp;$_} <$fd>;8175close$fd8176ornext;81778178# print element (entry, item)8179my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);8180if($formateq'rss') {8181print"<item>\n".8182"<title>". esc_html($co{'title'}) ."</title>\n".8183"<author>". esc_html($co{'author'}) ."</author>\n".8184"<pubDate>$cd{'rfc2822'}</pubDate>\n".8185"<guid isPermaLink=\"true\">$co_url</guid>\n".8186"<link>$co_url</link>\n".8187"<description>". esc_html($co{'title'}) ."</description>\n".8188"<content:encoded>".8189"<![CDATA[\n";8190}elsif($formateq'atom') {8191print"<entry>\n".8192"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".8193"<updated>$cd{'iso-8601'}</updated>\n".8194"<author>\n".8195" <name>". esc_html($co{'author_name'}) ."</name>\n";8196if($co{'author_email'}) {8197print" <email>". esc_html($co{'author_email'}) ."</email>\n";8198}8199print"</author>\n".8200# use committer for contributor8201"<contributor>\n".8202" <name>". esc_html($co{'committer_name'}) ."</name>\n";8203if($co{'committer_email'}) {8204print" <email>". esc_html($co{'committer_email'}) ."</email>\n";8205}8206print"</contributor>\n".8207"<published>$cd{'iso-8601'}</published>\n".8208"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".8209"<id>$co_url</id>\n".8210"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".8211"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";8212}8213my$comment=$co{'comment'};8214print"<pre>\n";8215foreachmy$line(@$comment) {8216$line= esc_html($line);8217print"$line\n";8218}8219print"</pre><ul>\n";8220foreachmy$difftree_line(@difftree) {8221my%difftree= parse_difftree_raw_line($difftree_line);8222next if!$difftree{'from_id'};82238224my$file=$difftree{'file'} ||$difftree{'to_file'};82258226print"<li>".8227"[".8228$cgi->a({-href => href(-full=>1, action=>"blobdiff",8229 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},8230 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},8231 file_name=>$file, file_parent=>$difftree{'from_file'}),8232-title =>"diff"},'D');8233if($have_blame) {8234print$cgi->a({-href => href(-full=>1, action=>"blame",8235 file_name=>$file, hash_base=>$commit),8236-title =>"blame"},'B');8237}8238# if this is not a feed of a file history8239if(!defined$file_name||$file_namene$file) {8240print$cgi->a({-href => href(-full=>1, action=>"history",8241 file_name=>$file, hash=>$commit),8242-title =>"history"},'H');8243}8244$file= esc_path($file);8245print"] ".8246"$file</li>\n";8247}8248if($formateq'rss') {8249print"</ul>]]>\n".8250"</content:encoded>\n".8251"</item>\n";8252}elsif($formateq'atom') {8253print"</ul>\n</div>\n".8254"</content>\n".8255"</entry>\n";8256}8257}82588259# end of feed8260if($formateq'rss') {8261print"</channel>\n</rss>\n";8262}elsif($formateq'atom') {8263print"</feed>\n";8264}8265}82668267sub git_rss {8268 git_feed('rss');8269}82708271sub git_atom {8272 git_feed('atom');8273}82748275sub git_opml {8276my@list= git_get_projects_list($project_filter,$strict_export);8277if(!@list) {8278 die_error(404,"No projects found");8279}82808281print$cgi->header(8282-type =>'text/xml',8283-charset =>'utf-8',8284-content_disposition =>'inline; filename="opml.xml"');82858286my$title= esc_html($site_name);8287my$filter=" within subdirectory ";8288if(defined$project_filter) {8289$filter.= esc_html($project_filter);8290}else{8291$filter="";8292}8293print<<XML;8294<?xml version="1.0" encoding="utf-8"?>8295<opml version="1.0">8296<head>8297 <title>$titleOPML Export$filter</title>8298</head>8299<body>8300<outline text="git RSS feeds">8301XML83028303foreachmy$pr(@list) {8304my%proj=%$pr;8305my$head= git_get_head_hash($proj{'path'});8306if(!defined$head) {8307next;8308}8309$git_dir="$projectroot/$proj{'path'}";8310my%co= parse_commit($head);8311if(!%co) {8312next;8313}83148315my$path= esc_html(chop_str($proj{'path'},25,5));8316my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);8317my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);8318print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";8319}8320print<<XML;8321</outline>8322</body>8323</opml>8324XML8325}