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 validate_refname {1456my$input=shift||returnundef;14571458# textual hashes are O.K.1459if($input=~m/^[0-9a-fA-F]{40}$/) {1460return$input;1461}1462# it must be correct pathname1463$input= validate_pathname($input)1464orreturnundef;1465# restrictions on ref name according to git-check-ref-format1466if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1467returnundef;1468}1469return$input;1470}14711472# decode sequences of octets in utf8 into Perl's internal form,1473# which is utf-8 with utf8 flag set if needed. gitweb writes out1474# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1475sub to_utf8 {1476my$str=shift;1477returnundefunlessdefined$str;14781479if(utf8::is_utf8($str) || utf8::decode($str)) {1480return$str;1481}else{1482return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1483}1484}14851486# quote unsafe chars, but keep the slash, even when it's not1487# correct, but quoted slashes look too horrible in bookmarks1488sub esc_param {1489my$str=shift;1490returnundefunlessdefined$str;1491$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1492$str=~s/ /\+/g;1493return$str;1494}14951496# the quoting rules for path_info fragment are slightly different1497sub esc_path_info {1498my$str=shift;1499returnundefunlessdefined$str;15001501# path_info doesn't treat '+' as space (specially), but '?' must be escaped1502$str=~s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;15031504return$str;1505}15061507# quote unsafe chars in whole URL, so some characters cannot be quoted1508sub esc_url {1509my$str=shift;1510returnundefunlessdefined$str;1511$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;1512$str=~s/ /\+/g;1513return$str;1514}15151516# quote unsafe characters in HTML attributes1517sub esc_attr {15181519# for XHTML conformance escaping '"' to '"' is not enough1520return esc_html(@_);1521}15221523# replace invalid utf8 character with SUBSTITUTION sequence1524sub esc_html {1525my$str=shift;1526my%opts=@_;15271528returnundefunlessdefined$str;15291530$str= to_utf8($str);1531$str=$cgi->escapeHTML($str);1532if($opts{'-nbsp'}) {1533$str=~s/ / /g;1534}1535$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1536return$str;1537}15381539# quote control characters and escape filename to HTML1540sub esc_path {1541my$str=shift;1542my%opts=@_;15431544returnundefunlessdefined$str;15451546$str= to_utf8($str);1547$str=$cgi->escapeHTML($str);1548if($opts{'-nbsp'}) {1549$str=~s/ / /g;1550}1551$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1552return$str;1553}15541555# Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)1556sub sanitize {1557my$str=shift;15581559returnundefunlessdefined$str;15601561$str= to_utf8($str);1562$str=~ s|([[:cntrl:]])|(index("\t\n\r",$1) != -1?$1: quot_cec($1))|eg;1563return$str;1564}15651566# Make control characters "printable", using character escape codes (CEC)1567sub quot_cec {1568my$cntrl=shift;1569my%opts=@_;1570my%es= (# character escape codes, aka escape sequences1571"\t"=>'\t',# tab (HT)1572"\n"=>'\n',# line feed (LF)1573"\r"=>'\r',# carrige return (CR)1574"\f"=>'\f',# form feed (FF)1575"\b"=>'\b',# backspace (BS)1576"\a"=>'\a',# alarm (bell) (BEL)1577"\e"=>'\e',# escape (ESC)1578"\013"=>'\v',# vertical tab (VT)1579"\000"=>'\0',# nul character (NUL)1580);1581my$chr= ( (exists$es{$cntrl})1582?$es{$cntrl}1583:sprintf('\%2x',ord($cntrl)) );1584if($opts{-nohtml}) {1585return$chr;1586}else{1587return"<span class=\"cntrl\">$chr</span>";1588}1589}15901591# Alternatively use unicode control pictures codepoints,1592# Unicode "printable representation" (PR)1593sub quot_upr {1594my$cntrl=shift;1595my%opts=@_;15961597my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1598if($opts{-nohtml}) {1599return$chr;1600}else{1601return"<span class=\"cntrl\">$chr</span>";1602}1603}16041605# git may return quoted and escaped filenames1606sub unquote {1607my$str=shift;16081609sub unq {1610my$seq=shift;1611my%es= (# character escape codes, aka escape sequences1612't'=>"\t",# tab (HT, TAB)1613'n'=>"\n",# newline (NL)1614'r'=>"\r",# return (CR)1615'f'=>"\f",# form feed (FF)1616'b'=>"\b",# backspace (BS)1617'a'=>"\a",# alarm (bell) (BEL)1618'e'=>"\e",# escape (ESC)1619'v'=>"\013",# vertical tab (VT)1620);16211622if($seq=~m/^[0-7]{1,3}$/) {1623# octal char sequence1624returnchr(oct($seq));1625}elsif(exists$es{$seq}) {1626# C escape sequence, aka character escape code1627return$es{$seq};1628}1629# quoted ordinary character1630return$seq;1631}16321633if($str=~m/^"(.*)"$/) {1634# needs unquoting1635$str=$1;1636$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1637}1638return$str;1639}16401641# escape tabs (convert tabs to spaces)1642sub untabify {1643my$line=shift;16441645while((my$pos=index($line,"\t")) != -1) {1646if(my$count= (8- ($pos%8))) {1647my$spaces=' ' x $count;1648$line=~s/\t/$spaces/;1649}1650}16511652return$line;1653}16541655sub project_in_list {1656my$project=shift;1657my@list= git_get_projects_list();1658return@list&&scalar(grep{$_->{'path'}eq$project}@list);1659}16601661## ----------------------------------------------------------------------1662## HTML aware string manipulation16631664# Try to chop given string on a word boundary between position1665# $len and $len+$add_len. If there is no word boundary there,1666# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1667# (marking chopped part) would be longer than given string.1668sub chop_str {1669my$str=shift;1670my$len=shift;1671my$add_len=shift||10;1672my$where=shift||'right';# 'left' | 'center' | 'right'16731674# Make sure perl knows it is utf8 encoded so we don't1675# cut in the middle of a utf8 multibyte char.1676$str= to_utf8($str);16771678# allow only $len chars, but don't cut a word if it would fit in $add_len1679# if it doesn't fit, cut it if it's still longer than the dots we would add1680# remove chopped character entities entirely16811682# when chopping in the middle, distribute $len into left and right part1683# return early if chopping wouldn't make string shorter1684if($whereeq'center') {1685return$strif($len+5>=length($str));# filler is length 51686$len=int($len/2);1687}else{1688return$strif($len+4>=length($str));# filler is length 41689}16901691# regexps: ending and beginning with word part up to $add_len1692my$endre=qr/.{$len}\w{0,$add_len}/;1693my$begre=qr/\w{0,$add_len}.{$len}/;16941695if($whereeq'left') {1696$str=~m/^(.*?)($begre)$/;1697my($lead,$body) = ($1,$2);1698if(length($lead) >4) {1699$lead=" ...";1700}1701return"$lead$body";17021703}elsif($whereeq'center') {1704$str=~m/^($endre)(.*)$/;1705my($left,$str) = ($1,$2);1706$str=~m/^(.*?)($begre)$/;1707my($mid,$right) = ($1,$2);1708if(length($mid) >5) {1709$mid=" ... ";1710}1711return"$left$mid$right";17121713}else{1714$str=~m/^($endre)(.*)$/;1715my$body=$1;1716my$tail=$2;1717if(length($tail) >4) {1718$tail="... ";1719}1720return"$body$tail";1721}1722}17231724# takes the same arguments as chop_str, but also wraps a <span> around the1725# result with a title attribute if it does get chopped. Additionally, the1726# string is HTML-escaped.1727sub chop_and_escape_str {1728my($str) =@_;17291730my$chopped= chop_str(@_);1731$str= to_utf8($str);1732if($choppedeq$str) {1733return esc_html($chopped);1734}else{1735$str=~s/[[:cntrl:]]/?/g;1736return$cgi->span({-title=>$str}, esc_html($chopped));1737}1738}17391740# Highlight selected fragments of string, using given CSS class,1741# and escape HTML. It is assumed that fragments do not overlap.1742# Regions are passed as list of pairs (array references).1743#1744# Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns1745# '<span class="mark">foo</span>bar'1746sub esc_html_hl_regions {1747my($str,$css_class,@sel) =@_;1748my%opts=grep{ref($_)ne'ARRAY'}@sel;1749@sel=grep{ref($_)eq'ARRAY'}@sel;1750return esc_html($str,%opts)unless@sel;17511752my$out='';1753my$pos=0;17541755formy$s(@sel) {1756my($begin,$end) =@$s;17571758# Don't create empty <span> elements.1759next if$end<=$begin;17601761my$escaped= esc_html(substr($str,$begin,$end-$begin),1762%opts);17631764$out.= esc_html(substr($str,$pos,$begin-$pos),%opts)1765if($begin-$pos>0);1766$out.=$cgi->span({-class=>$css_class},$escaped);17671768$pos=$end;1769}1770$out.= esc_html(substr($str,$pos),%opts)1771if($pos<length($str));17721773return$out;1774}17751776# return positions of beginning and end of each match1777sub matchpos_list {1778my($str,$regexp) =@_;1779return unless(defined$str&&defined$regexp);17801781my@matches;1782while($str=~/$regexp/g) {1783push@matches, [$-[0],$+[0]];1784}1785return@matches;1786}17871788# highlight match (if any), and escape HTML1789sub esc_html_match_hl {1790my($str,$regexp) =@_;1791return esc_html($str)unlessdefined$regexp;17921793my@matches= matchpos_list($str,$regexp);1794return esc_html($str)unless@matches;17951796return esc_html_hl_regions($str,'match',@matches);1797}179817991800# highlight match (if any) of shortened string, and escape HTML1801sub esc_html_match_hl_chopped {1802my($str,$chopped,$regexp) =@_;1803return esc_html_match_hl($str,$regexp)unlessdefined$chopped;18041805my@matches= matchpos_list($str,$regexp);1806return esc_html($chopped)unless@matches;18071808# filter matches so that we mark chopped string1809my$tail="... ";# see chop_str1810unless($chopped=~s/\Q$tail\E$//) {1811$tail='';1812}1813my$chop_len=length($chopped);1814my$tail_len=length($tail);1815my@filtered;18161817formy$m(@matches) {1818if($m->[0] >$chop_len) {1819push@filtered, [$chop_len,$chop_len+$tail_len]if($tail_len>0);1820last;1821}elsif($m->[1] >$chop_len) {1822push@filtered, [$m->[0],$chop_len+$tail_len];1823last;1824}1825push@filtered,$m;1826}18271828return esc_html_hl_regions($chopped.$tail,'match',@filtered);1829}18301831## ----------------------------------------------------------------------1832## functions returning short strings18331834# CSS class for given age value (in seconds)1835sub age_class {1836my$age=shift;18371838if(!defined$age) {1839return"noage";1840}elsif($age<60*60*2) {1841return"age0";1842}elsif($age<60*60*24*2) {1843return"age1";1844}else{1845return"age2";1846}1847}18481849# convert age in seconds to "nn units ago" string1850sub age_string {1851my$age=shift;1852my$age_str;18531854if($age>60*60*24*365*2) {1855$age_str= (int$age/60/60/24/365);1856$age_str.=" years ago";1857}elsif($age>60*60*24*(365/12)*2) {1858$age_str=int$age/60/60/24/(365/12);1859$age_str.=" months ago";1860}elsif($age>60*60*24*7*2) {1861$age_str=int$age/60/60/24/7;1862$age_str.=" weeks ago";1863}elsif($age>60*60*24*2) {1864$age_str=int$age/60/60/24;1865$age_str.=" days ago";1866}elsif($age>60*60*2) {1867$age_str=int$age/60/60;1868$age_str.=" hours ago";1869}elsif($age>60*2) {1870$age_str=int$age/60;1871$age_str.=" min ago";1872}elsif($age>2) {1873$age_str=int$age;1874$age_str.=" sec ago";1875}else{1876$age_str.=" right now";1877}1878return$age_str;1879}18801881useconstant{1882 S_IFINVALID =>0030000,1883 S_IFGITLINK =>0160000,1884};18851886# submodule/subproject, a commit object reference1887sub S_ISGITLINK {1888my$mode=shift;18891890return(($mode& S_IFMT) == S_IFGITLINK)1891}18921893# convert file mode in octal to symbolic file mode string1894sub mode_str {1895my$mode=oct shift;18961897if(S_ISGITLINK($mode)) {1898return'm---------';1899}elsif(S_ISDIR($mode& S_IFMT)) {1900return'drwxr-xr-x';1901}elsif(S_ISLNK($mode)) {1902return'lrwxrwxrwx';1903}elsif(S_ISREG($mode)) {1904# git cares only about the executable bit1905if($mode& S_IXUSR) {1906return'-rwxr-xr-x';1907}else{1908return'-rw-r--r--';1909};1910}else{1911return'----------';1912}1913}19141915# convert file mode in octal to file type string1916sub file_type {1917my$mode=shift;19181919if($mode!~m/^[0-7]+$/) {1920return$mode;1921}else{1922$mode=oct$mode;1923}19241925if(S_ISGITLINK($mode)) {1926return"submodule";1927}elsif(S_ISDIR($mode& S_IFMT)) {1928return"directory";1929}elsif(S_ISLNK($mode)) {1930return"symlink";1931}elsif(S_ISREG($mode)) {1932return"file";1933}else{1934return"unknown";1935}1936}19371938# convert file mode in octal to file type description string1939sub file_type_long {1940my$mode=shift;19411942if($mode!~m/^[0-7]+$/) {1943return$mode;1944}else{1945$mode=oct$mode;1946}19471948if(S_ISGITLINK($mode)) {1949return"submodule";1950}elsif(S_ISDIR($mode& S_IFMT)) {1951return"directory";1952}elsif(S_ISLNK($mode)) {1953return"symlink";1954}elsif(S_ISREG($mode)) {1955if($mode& S_IXUSR) {1956return"executable";1957}else{1958return"file";1959};1960}else{1961return"unknown";1962}1963}196419651966## ----------------------------------------------------------------------1967## functions returning short HTML fragments, or transforming HTML fragments1968## which don't belong to other sections19691970# format line of commit message.1971sub format_log_line_html {1972my$line=shift;19731974$line= esc_html($line, -nbsp=>1);1975$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1976$cgi->a({-href => href(action=>"object", hash=>$1),1977-class=>"text"},$1);1978}eg;19791980return$line;1981}19821983# format marker of refs pointing to given object19841985# the destination action is chosen based on object type and current context:1986# - for annotated tags, we choose the tag view unless it's the current view1987# already, in which case we go to shortlog view1988# - for other refs, we keep the current view if we're in history, shortlog or1989# log view, and select shortlog otherwise1990sub format_ref_marker {1991my($refs,$id) =@_;1992my$markers='';19931994if(defined$refs->{$id}) {1995foreachmy$ref(@{$refs->{$id}}) {1996# this code exploits the fact that non-lightweight tags are the1997# only indirect objects, and that they are the only objects for which1998# we want to use tag instead of shortlog as action1999my($type,$name) =qw();2000my$indirect= ($ref=~s/\^\{\}$//);2001# e.g. tags/v2.6.11 or heads/next2002if($ref=~m!^(.*?)s?/(.*)$!) {2003$type=$1;2004$name=$2;2005}else{2006$type="ref";2007$name=$ref;2008}20092010my$class=$type;2011$class.=" indirect"if$indirect;20122013my$dest_action="shortlog";20142015if($indirect) {2016$dest_action="tag"unless$actioneq"tag";2017}elsif($action=~/^(history|(short)?log)$/) {2018$dest_action=$action;2019}20202021my$dest="";2022$dest.="refs/"unless$ref=~ m!^refs/!;2023$dest.=$ref;20242025my$link=$cgi->a({2026-href => href(2027 action=>$dest_action,2028 hash=>$dest2029)},$name);20302031$markers.=" <span class=\"".esc_attr($class)."\"title=\"".esc_attr($ref)."\">".2032$link."</span>";2033}2034}20352036if($markers) {2037return' <span class="refs">'.$markers.'</span>';2038}else{2039return"";2040}2041}20422043# format, perhaps shortened and with markers, title line2044sub format_subject_html {2045my($long,$short,$href,$extra) =@_;2046$extra=''unlessdefined($extra);20472048if(length($short) <length($long)) {2049$long=~s/[[:cntrl:]]/?/g;2050return$cgi->a({-href =>$href, -class=>"list subject",2051-title => to_utf8($long)},2052 esc_html($short)) .$extra;2053}else{2054return$cgi->a({-href =>$href, -class=>"list subject"},2055 esc_html($long)) .$extra;2056}2057}20582059# Rather than recomputing the url for an email multiple times, we cache it2060# after the first hit. This gives a visible benefit in views where the avatar2061# for the same email is used repeatedly (e.g. shortlog).2062# The cache is shared by all avatar engines (currently gravatar only), which2063# are free to use it as preferred. Since only one avatar engine is used for any2064# given page, there's no risk for cache conflicts.2065our%avatar_cache= ();20662067# Compute the picon url for a given email, by using the picon search service over at2068# http://www.cs.indiana.edu/picons/search.html2069sub picon_url {2070my$email=lc shift;2071if(!$avatar_cache{$email}) {2072my($user,$domain) =split('@',$email);2073$avatar_cache{$email} =2074"//www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".2075"$domain/$user/".2076"users+domains+unknown/up/single";2077}2078return$avatar_cache{$email};2079}20802081# Compute the gravatar url for a given email, if it's not in the cache already.2082# Gravatar stores only the part of the URL before the size, since that's the2083# one computationally more expensive. This also allows reuse of the cache for2084# different sizes (for this particular engine).2085sub gravatar_url {2086my$email=lc shift;2087my$size=shift;2088$avatar_cache{$email} ||=2089"//www.gravatar.com/avatar/".2090 Digest::MD5::md5_hex($email) ."?s=";2091return$avatar_cache{$email} .$size;2092}20932094# Insert an avatar for the given $email at the given $size if the feature2095# is enabled.2096sub git_get_avatar {2097my($email,%opts) =@_;2098my$pre_white= ($opts{-pad_before} ?" ":"");2099my$post_white= ($opts{-pad_after} ?" ":"");2100$opts{-size} ||='default';2101my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};2102my$url="";2103if($git_avatareq'gravatar') {2104$url= gravatar_url($email,$size);2105}elsif($git_avatareq'picon') {2106$url= picon_url($email);2107}2108# Other providers can be added by extending the if chain, defining $url2109# as needed. If no variant puts something in $url, we assume avatars2110# are completely disabled/unavailable.2111if($url) {2112return$pre_white.2113"<img width=\"$size\"".2114"class=\"avatar\"".2115"src=\"".esc_url($url)."\"".2116"alt=\"\"".2117"/>".$post_white;2118}else{2119return"";2120}2121}21222123sub format_search_author {2124my($author,$searchtype,$displaytext) =@_;2125my$have_search= gitweb_check_feature('search');21262127if($have_search) {2128my$performed="";2129if($searchtypeeq'author') {2130$performed="authored";2131}elsif($searchtypeeq'committer') {2132$performed="committed";2133}21342135return$cgi->a({-href => href(action=>"search", hash=>$hash,2136 searchtext=>$author,2137 searchtype=>$searchtype),class=>"list",2138 title=>"Search for commits$performedby$author"},2139$displaytext);21402141}else{2142return$displaytext;2143}2144}21452146# format the author name of the given commit with the given tag2147# the author name is chopped and escaped according to the other2148# optional parameters (see chop_str).2149sub format_author_html {2150my$tag=shift;2151my$co=shift;2152my$author= chop_and_escape_str($co->{'author_name'},@_);2153return"<$tagclass=\"author\">".2154 format_search_author($co->{'author_name'},"author",2155 git_get_avatar($co->{'author_email'}, -pad_after =>1) .2156$author) .2157"</$tag>";2158}21592160# format git diff header line, i.e. "diff --(git|combined|cc) ..."2161sub format_git_diff_header_line {2162my$line=shift;2163my$diffinfo=shift;2164my($from,$to) =@_;21652166if($diffinfo->{'nparents'}) {2167# combined diff2168$line=~s!^(diff (.*?) )"?.*$!$1!;2169if($to->{'href'}) {2170$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2171 esc_path($to->{'file'}));2172}else{# file was deleted (no href)2173$line.= esc_path($to->{'file'});2174}2175}else{2176# "ordinary" diff2177$line=~s!^(diff (.*?) )"?a/.*$!$1!;2178if($from->{'href'}) {2179$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},2180'a/'. esc_path($from->{'file'}));2181}else{# file was added (no href)2182$line.='a/'. esc_path($from->{'file'});2183}2184$line.=' ';2185if($to->{'href'}) {2186$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2187'b/'. esc_path($to->{'file'}));2188}else{# file was deleted2189$line.='b/'. esc_path($to->{'file'});2190}2191}21922193return"<div class=\"diff header\">$line</div>\n";2194}21952196# format extended diff header line, before patch itself2197sub format_extended_diff_header_line {2198my$line=shift;2199my$diffinfo=shift;2200my($from,$to) =@_;22012202# match <path>2203if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {2204$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},2205 esc_path($from->{'file'}));2206}2207if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {2208$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},2209 esc_path($to->{'file'}));2210}2211# match single <mode>2212if($line=~m/\s(\d{6})$/) {2213$line.='<span class="info"> ('.2214 file_type_long($1) .2215')</span>';2216}2217# match <hash>2218if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {2219# can match only for combined diff2220$line='index ';2221for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2222if($from->{'href'}[$i]) {2223$line.=$cgi->a({-href=>$from->{'href'}[$i],2224-class=>"hash"},2225substr($diffinfo->{'from_id'}[$i],0,7));2226}else{2227$line.='0' x 7;2228}2229# separator2230$line.=','if($i<$diffinfo->{'nparents'} -1);2231}2232$line.='..';2233if($to->{'href'}) {2234$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2235substr($diffinfo->{'to_id'},0,7));2236}else{2237$line.='0' x 7;2238}22392240}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {2241# can match only for ordinary diff2242my($from_link,$to_link);2243if($from->{'href'}) {2244$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},2245substr($diffinfo->{'from_id'},0,7));2246}else{2247$from_link='0' x 7;2248}2249if($to->{'href'}) {2250$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2251substr($diffinfo->{'to_id'},0,7));2252}else{2253$to_link='0' x 7;2254}2255my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});2256$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;2257}22582259return$line."<br/>\n";2260}22612262# format from-file/to-file diff header2263sub format_diff_from_to_header {2264my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;2265my$line;2266my$result='';22672268$line=$from_line;2269#assert($line =~ m/^---/) if DEBUG;2270# no extra formatting for "^--- /dev/null"2271if(!$diffinfo->{'nparents'}) {2272# ordinary (single parent) diff2273if($line=~m!^--- "?a/!) {2274if($from->{'href'}) {2275$line='--- a/'.2276$cgi->a({-href=>$from->{'href'}, -class=>"path"},2277 esc_path($from->{'file'}));2278}else{2279$line='--- a/'.2280 esc_path($from->{'file'});2281}2282}2283$result.= qq!<div class="diff from_file">$line</div>\n!;22842285}else{2286# combined diff (merge commit)2287for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2288if($from->{'href'}[$i]) {2289$line='--- '.2290$cgi->a({-href=>href(action=>"blobdiff",2291 hash_parent=>$diffinfo->{'from_id'}[$i],2292 hash_parent_base=>$parents[$i],2293 file_parent=>$from->{'file'}[$i],2294 hash=>$diffinfo->{'to_id'},2295 hash_base=>$hash,2296 file_name=>$to->{'file'}),2297-class=>"path",2298-title=>"diff". ($i+1)},2299$i+1) .2300'/'.2301$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2302 esc_path($from->{'file'}[$i]));2303}else{2304$line='--- /dev/null';2305}2306$result.= qq!<div class="diff from_file">$line</div>\n!;2307}2308}23092310$line=$to_line;2311#assert($line =~ m/^\+\+\+/) if DEBUG;2312# no extra formatting for "^+++ /dev/null"2313if($line=~m!^\+\+\+ "?b/!) {2314if($to->{'href'}) {2315$line='+++ b/'.2316$cgi->a({-href=>$to->{'href'}, -class=>"path"},2317 esc_path($to->{'file'}));2318}else{2319$line='+++ b/'.2320 esc_path($to->{'file'});2321}2322}2323$result.= qq!<div class="diff to_file">$line</div>\n!;23242325return$result;2326}23272328# create note for patch simplified by combined diff2329sub format_diff_cc_simplified {2330my($diffinfo,@parents) =@_;2331my$result='';23322333$result.="<div class=\"diff header\">".2334"diff --cc ";2335if(!is_deleted($diffinfo)) {2336$result.=$cgi->a({-href => href(action=>"blob",2337 hash_base=>$hash,2338 hash=>$diffinfo->{'to_id'},2339 file_name=>$diffinfo->{'to_file'}),2340-class=>"path"},2341 esc_path($diffinfo->{'to_file'}));2342}else{2343$result.= esc_path($diffinfo->{'to_file'});2344}2345$result.="</div>\n".# class="diff header"2346"<div class=\"diff nodifferences\">".2347"Simple merge".2348"</div>\n";# class="diff nodifferences"23492350return$result;2351}23522353sub diff_line_class {2354my($line,$from,$to) =@_;23552356# ordinary diff2357my$num_sign=1;2358# combined diff2359if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2360$num_sign=scalar@{$from->{'href'}};2361}23622363my@diff_line_classifier= (2364{ regexp =>qr/^\@\@{$num_sign} /,class=>"chunk_header"},2365{ regexp =>qr/^\\/,class=>"incomplete"},2366{ regexp =>qr/^ {$num_sign}/,class=>"ctx"},2367# classifier for context must come before classifier add/rem,2368# or we would have to use more complicated regexp, for example2369# qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;2370{ regexp =>qr/^[+ ]{$num_sign}/,class=>"add"},2371{ regexp =>qr/^[- ]{$num_sign}/,class=>"rem"},2372);2373formy$clsfy(@diff_line_classifier) {2374return$clsfy->{'class'}2375if($line=~$clsfy->{'regexp'});2376}23772378# fallback2379return"";2380}23812382# assumes that $from and $to are defined and correctly filled,2383# and that $line holds a line of chunk header for unified diff2384sub format_unidiff_chunk_header {2385my($line,$from,$to) =@_;23862387my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2388$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;23892390$from_lines=0unlessdefined$from_lines;2391$to_lines=0unlessdefined$to_lines;23922393if($from->{'href'}) {2394$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2395-class=>"list"},$from_text);2396}2397if($to->{'href'}) {2398$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2399-class=>"list"},$to_text);2400}2401$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2402"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2403return$line;2404}24052406# assumes that $from and $to are defined and correctly filled,2407# and that $line holds a line of chunk header for combined diff2408sub format_cc_diff_chunk_header {2409my($line,$from,$to) =@_;24102411my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2412my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);24132414@from_text=split(' ',$ranges);2415for(my$i=0;$i<@from_text; ++$i) {2416($from_start[$i],$from_nlines[$i]) =2417(split(',',substr($from_text[$i],1)),0);2418}24192420$to_text=pop@from_text;2421$to_start=pop@from_start;2422$to_nlines=pop@from_nlines;24232424$line="<span class=\"chunk_info\">$prefix";2425for(my$i=0;$i<@from_text; ++$i) {2426if($from->{'href'}[$i]) {2427$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2428-class=>"list"},$from_text[$i]);2429}else{2430$line.=$from_text[$i];2431}2432$line.=" ";2433}2434if($to->{'href'}) {2435$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2436-class=>"list"},$to_text);2437}else{2438$line.=$to_text;2439}2440$line.="$prefix</span>".2441"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2442return$line;2443}24442445# process patch (diff) line (not to be used for diff headers),2446# returning HTML-formatted (but not wrapped) line.2447# If the line is passed as a reference, it is treated as HTML and not2448# esc_html()'ed.2449sub format_diff_line {2450my($line,$diff_class,$from,$to) =@_;24512452if(ref($line)) {2453$line=$$line;2454}else{2455chomp$line;2456$line= untabify($line);24572458if($from&&$to&&$line=~m/^\@{2} /) {2459$line= format_unidiff_chunk_header($line,$from,$to);2460}elsif($from&&$to&&$line=~m/^\@{3}/) {2461$line= format_cc_diff_chunk_header($line,$from,$to);2462}else{2463$line= esc_html($line, -nbsp=>1);2464}2465}24662467my$diff_classes="diff";2468$diff_classes.="$diff_class"if($diff_class);2469$line="<div class=\"$diff_classes\">$line</div>\n";24702471return$line;2472}24732474# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2475# linked. Pass the hash of the tree/commit to snapshot.2476sub format_snapshot_links {2477my($hash) =@_;2478my$num_fmts=@snapshot_fmts;2479if($num_fmts>1) {2480# A parenthesized list of links bearing format names.2481# e.g. "snapshot (_tar.gz_ _zip_)"2482return"snapshot (".join(' ',map2483$cgi->a({2484-href => href(2485 action=>"snapshot",2486 hash=>$hash,2487 snapshot_format=>$_2488)2489},$known_snapshot_formats{$_}{'display'})2490,@snapshot_fmts) .")";2491}elsif($num_fmts==1) {2492# A single "snapshot" link whose tooltip bears the format name.2493# i.e. "_snapshot_"2494my($fmt) =@snapshot_fmts;2495return2496$cgi->a({2497-href => href(2498 action=>"snapshot",2499 hash=>$hash,2500 snapshot_format=>$fmt2501),2502-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2503},"snapshot");2504}else{# $num_fmts == 02505returnundef;2506}2507}25082509## ......................................................................2510## functions returning values to be passed, perhaps after some2511## transformation, to other functions; e.g. returning arguments to href()25122513# returns hash to be passed to href to generate gitweb URL2514# in -title key it returns description of link2515sub get_feed_info {2516my$format=shift||'Atom';2517my%res= (action =>lc($format));25182519# feed links are possible only for project views2520return unless(defined$project);2521# some views should link to OPML, or to generic project feed,2522# or don't have specific feed yet (so they should use generic)2523return if(!$action||$action=~/^(?:tags|heads|forks|tag|search)$/x);25242525my$branch;2526# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2527# from tag links; this also makes possible to detect branch links2528if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2529(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2530$branch=$1;2531}2532# find log type for feed description (title)2533my$type='log';2534if(defined$file_name) {2535$type="history of$file_name";2536$type.="/"if($actioneq'tree');2537$type.=" on '$branch'"if(defined$branch);2538}else{2539$type="log of$branch"if(defined$branch);2540}25412542$res{-title} =$type;2543$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2544$res{'file_name'} =$file_name;25452546return%res;2547}25482549## ----------------------------------------------------------------------2550## git utility subroutines, invoking git commands25512552# returns path to the core git executable and the --git-dir parameter as list2553sub git_cmd {2554$number_of_git_cmds++;2555return$GIT,'--git-dir='.$git_dir;2556}25572558# quote the given arguments for passing them to the shell2559# quote_command("command", "arg 1", "arg with ' and ! characters")2560# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2561# Try to avoid using this function wherever possible.2562sub quote_command {2563returnjoin(' ',2564map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2565}25662567# get HEAD ref of given project as hash2568sub git_get_head_hash {2569return git_get_full_hash(shift,'HEAD');2570}25712572sub git_get_full_hash {2573return git_get_hash(@_);2574}25752576sub git_get_short_hash {2577return git_get_hash(@_,'--short=7');2578}25792580sub git_get_hash {2581my($project,$hash,@options) =@_;2582my$o_git_dir=$git_dir;2583my$retval=undef;2584$git_dir="$projectroot/$project";2585if(open my$fd,'-|', git_cmd(),'rev-parse',2586'--verify','-q',@options,$hash) {2587$retval= <$fd>;2588chomp$retvalifdefined$retval;2589close$fd;2590}2591if(defined$o_git_dir) {2592$git_dir=$o_git_dir;2593}2594return$retval;2595}25962597# get type of given object2598sub git_get_type {2599my$hash=shift;26002601open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2602my$type= <$fd>;2603close$fdorreturn;2604chomp$type;2605return$type;2606}26072608# repository configuration2609our$config_file='';2610our%config;26112612# store multiple values for single key as anonymous array reference2613# single values stored directly in the hash, not as [ <value> ]2614sub hash_set_multi {2615my($hash,$key,$value) =@_;26162617if(!exists$hash->{$key}) {2618$hash->{$key} =$value;2619}elsif(!ref$hash->{$key}) {2620$hash->{$key} = [$hash->{$key},$value];2621}else{2622push@{$hash->{$key}},$value;2623}2624}26252626# return hash of git project configuration2627# optionally limited to some section, e.g. 'gitweb'2628sub git_parse_project_config {2629my$section_regexp=shift;2630my%config;26312632local$/="\0";26332634open my$fh,"-|", git_cmd(),"config",'-z','-l',2635orreturn;26362637while(my$keyval= <$fh>) {2638chomp$keyval;2639my($key,$value) =split(/\n/,$keyval,2);26402641 hash_set_multi(\%config,$key,$value)2642if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2643}2644close$fh;26452646return%config;2647}26482649# convert config value to boolean: 'true' or 'false'2650# no value, number > 0, 'true' and 'yes' values are true2651# rest of values are treated as false (never as error)2652sub config_to_bool {2653my$val=shift;26542655return1if!defined$val;# section.key26562657# strip leading and trailing whitespace2658$val=~s/^\s+//;2659$val=~s/\s+$//;26602661return(($val=~/^\d+$/&&$val) ||# section.key = 12662($val=~/^(?:true|yes)$/i));# section.key = true2663}26642665# convert config value to simple decimal number2666# an optional value suffix of 'k', 'm', or 'g' will cause the value2667# to be multiplied by 1024, 1048576, or 10737418242668sub config_to_int {2669my$val=shift;26702671# strip leading and trailing whitespace2672$val=~s/^\s+//;2673$val=~s/\s+$//;26742675if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2676$unit=lc($unit);2677# unknown unit is treated as 12678return$num* ($uniteq'g'?1073741824:2679$uniteq'm'?1048576:2680$uniteq'k'?1024:1);2681}2682return$val;2683}26842685# convert config value to array reference, if needed2686sub config_to_multi {2687my$val=shift;26882689returnref($val) ?$val: (defined($val) ? [$val] : []);2690}26912692sub git_get_project_config {2693my($key,$type) =@_;26942695return unlessdefined$git_dir;26962697# key sanity check2698return unless($key);2699# only subsection, if exists, is case sensitive,2700# and not lowercased by 'git config -z -l'2701if(my($hi,$mi,$lo) = ($key=~/^([^.]*)\.(.*)\.([^.]*)$/)) {2702$lo=~s/_//g;2703$key=join(".",lc($hi),$mi,lc($lo));2704return if($lo=~/\W/||$hi=~/\W/);2705}else{2706$key=lc($key);2707$key=~s/_//g;2708return if($key=~/\W/);2709}2710$key=~s/^gitweb\.//;27112712# type sanity check2713if(defined$type) {2714$type=~s/^--//;2715$type=undef2716unless($typeeq'bool'||$typeeq'int');2717}27182719# get config2720if(!defined$config_file||2721$config_filene"$git_dir/config") {2722%config= git_parse_project_config('gitweb');2723$config_file="$git_dir/config";2724}27252726# check if config variable (key) exists2727return unlessexists$config{"gitweb.$key"};27282729# ensure given type2730if(!defined$type) {2731return$config{"gitweb.$key"};2732}elsif($typeeq'bool') {2733# backward compatibility: 'git config --bool' returns true/false2734return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2735}elsif($typeeq'int') {2736return config_to_int($config{"gitweb.$key"});2737}2738return$config{"gitweb.$key"};2739}27402741# get hash of given path at given ref2742sub git_get_hash_by_path {2743my$base=shift;2744my$path=shift||returnundef;2745my$type=shift;27462747$path=~ s,/+$,,;27482749open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2750or die_error(500,"Open git-ls-tree failed");2751my$line= <$fd>;2752close$fdorreturnundef;27532754if(!defined$line) {2755# there is no tree or hash given by $path at $base2756returnundef;2757}27582759#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2760$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2761if(defined$type&&$typene$2) {2762# type doesn't match2763returnundef;2764}2765return$3;2766}27672768# get path of entry with given hash at given tree-ish (ref)2769# used to get 'from' filename for combined diff (merge commit) for renames2770sub git_get_path_by_hash {2771my$base=shift||return;2772my$hash=shift||return;27732774local$/="\0";27752776open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2777orreturnundef;2778while(my$line= <$fd>) {2779chomp$line;27802781#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2782#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2783if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2784close$fd;2785return$1;2786}2787}2788close$fd;2789returnundef;2790}27912792## ......................................................................2793## git utility functions, directly accessing git repository27942795# get the value of config variable either from file named as the variable2796# itself in the repository ($GIT_DIR/$name file), or from gitweb.$name2797# configuration variable in the repository config file.2798sub git_get_file_or_project_config {2799my($path,$name) =@_;28002801$git_dir="$projectroot/$path";2802open my$fd,'<',"$git_dir/$name"2803orreturn git_get_project_config($name);2804my$conf= <$fd>;2805close$fd;2806if(defined$conf) {2807chomp$conf;2808}2809return$conf;2810}28112812sub git_get_project_description {2813my$path=shift;2814return git_get_file_or_project_config($path,'description');2815}28162817sub git_get_project_category {2818my$path=shift;2819return git_get_file_or_project_config($path,'category');2820}282128222823# supported formats:2824# * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)2825# - if its contents is a number, use it as tag weight,2826# - otherwise add a tag with weight 12827# * $GIT_DIR/ctags file, each line is a tag (with weight 1)2828# the same value multiple times increases tag weight2829# * `gitweb.ctag' multi-valued repo config variable2830sub git_get_project_ctags {2831my$project=shift;2832my$ctags= {};28332834$git_dir="$projectroot/$project";2835if(opendir my$dh,"$git_dir/ctags") {2836my@files=grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh);2837foreachmy$tagfile(@files) {2838open my$ct,'<',$tagfile2839ornext;2840my$val= <$ct>;2841chomp$valif$val;2842close$ct;28432844(my$ctag=$tagfile) =~ s#.*/##;2845if($val=~/^\d+$/) {2846$ctags->{$ctag} =$val;2847}else{2848$ctags->{$ctag} =1;2849}2850}2851closedir$dh;28522853}elsif(open my$fh,'<',"$git_dir/ctags") {2854while(my$line= <$fh>) {2855chomp$line;2856$ctags->{$line}++if$line;2857}2858close$fh;28592860}else{2861my$taglist= config_to_multi(git_get_project_config('ctag'));2862foreachmy$tag(@$taglist) {2863$ctags->{$tag}++;2864}2865}28662867return$ctags;2868}28692870# return hash, where keys are content tags ('ctags'),2871# and values are sum of weights of given tag in every project2872sub git_gather_all_ctags {2873my$projects=shift;2874my$ctags= {};28752876foreachmy$p(@$projects) {2877foreachmy$ct(keys%{$p->{'ctags'}}) {2878$ctags->{$ct} +=$p->{'ctags'}->{$ct};2879}2880}28812882return$ctags;2883}28842885sub git_populate_project_tagcloud {2886my$ctags=shift;28872888# First, merge different-cased tags; tags vote on casing2889my%ctags_lc;2890foreach(keys%$ctags) {2891$ctags_lc{lc$_}->{count} +=$ctags->{$_};2892if(not$ctags_lc{lc$_}->{topcount}2893or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2894$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2895$ctags_lc{lc$_}->{topname} =$_;2896}2897}28982899my$cloud;2900my$matched=$input_params{'ctag'};2901if(eval{require HTML::TagCloud;1; }) {2902$cloud= HTML::TagCloud->new;2903foreachmy$ctag(sort keys%ctags_lc) {2904# Pad the title with spaces so that the cloud looks2905# less crammed.2906my$title= esc_html($ctags_lc{$ctag}->{topname});2907$title=~s/ / /g;2908$title=~s/^/ /g;2909$title=~s/$/ /g;2910if(defined$matched&&$matchedeq$ctag) {2911$title=qq(<span class="match">$title</span>);2912}2913$cloud->add($title, href(project=>undef, ctag=>$ctag),2914$ctags_lc{$ctag}->{count});2915}2916}else{2917$cloud= {};2918foreachmy$ctag(keys%ctags_lc) {2919my$title= esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);2920if(defined$matched&&$matchedeq$ctag) {2921$title=qq(<span class="match">$title</span>);2922}2923$cloud->{$ctag}{count} =$ctags_lc{$ctag}->{count};2924$cloud->{$ctag}{ctag} =2925$cgi->a({-href=>href(project=>undef, ctag=>$ctag)},$title);2926}2927}2928return$cloud;2929}29302931sub git_show_project_tagcloud {2932my($cloud,$count) =@_;2933if(ref$cloudeq'HTML::TagCloud') {2934return$cloud->html_and_css($count);2935}else{2936my@tags=sort{$cloud->{$a}->{'count'} <=>$cloud->{$b}->{'count'} }keys%$cloud;2937return2938'<div id="htmltagcloud"'.($project?'':' align="center"').'>'.2939join(', ',map{2940$cloud->{$_}->{'ctag'}2941}splice(@tags,0,$count)) .2942'</div>';2943}2944}29452946sub git_get_project_url_list {2947my$path=shift;29482949$git_dir="$projectroot/$path";2950open my$fd,'<',"$git_dir/cloneurl"2951orreturnwantarray?2952@{ config_to_multi(git_get_project_config('url')) } :2953 config_to_multi(git_get_project_config('url'));2954my@git_project_url_list=map{chomp;$_} <$fd>;2955close$fd;29562957returnwantarray?@git_project_url_list: \@git_project_url_list;2958}29592960sub git_get_projects_list {2961my$filter=shift||'';2962my$paranoid=shift;2963my@list;29642965if(-d $projects_list) {2966# search in directory2967my$dir=$projects_list;2968# remove the trailing "/"2969$dir=~s!/+$!!;2970my$pfxlen=length("$dir");2971my$pfxdepth= ($dir=~tr!/!!);2972# when filtering, search only given subdirectory2973if($filter&& !$paranoid) {2974$dir.="/$filter";2975$dir=~s!/+$!!;2976}29772978 File::Find::find({2979 follow_fast =>1,# follow symbolic links2980 follow_skip =>2,# ignore duplicates2981 dangling_symlinks =>0,# ignore dangling symlinks, silently2982 wanted =>sub{2983# global variables2984our$project_maxdepth;2985our$projectroot;2986# skip project-list toplevel, if we get it.2987return if(m!^[/.]$!);2988# only directories can be git repositories2989return unless(-d $_);2990# don't traverse too deep (Find is super slow on os x)2991# $project_maxdepth excludes depth of $projectroot2992if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2993$File::Find::prune =1;2994return;2995}29962997my$path=substr($File::Find::name,$pfxlen+1);2998# paranoidly only filter here2999if($paranoid&&$filter&&$path!~m!^\Q$filter\E/!) {3000next;3001}3002# we check related file in $projectroot3003if(check_export_ok("$projectroot/$path")) {3004push@list, { path =>$path};3005$File::Find::prune =1;3006}3007},3008},"$dir");30093010}elsif(-f $projects_list) {3011# read from file(url-encoded):3012# 'git%2Fgit.git Linus+Torvalds'3013# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'3014# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'3015open my$fd,'<',$projects_listorreturn;3016 PROJECT:3017while(my$line= <$fd>) {3018chomp$line;3019my($path,$owner) =split' ',$line;3020$path= unescape($path);3021$owner= unescape($owner);3022if(!defined$path) {3023next;3024}3025# if $filter is rpovided, check if $path begins with $filter3026if($filter&&$path!~m!^\Q$filter\E/!) {3027next;3028}3029if(check_export_ok("$projectroot/$path")) {3030my$pr= {3031 path =>$path3032};3033if($owner) {3034$pr->{'owner'} = to_utf8($owner);3035}3036push@list,$pr;3037}3038}3039close$fd;3040}3041return@list;3042}30433044# written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)3045# as side effects it sets 'forks' field to list of forks for forked projects3046sub filter_forks_from_projects_list {3047my$projects=shift;30483049my%trie;# prefix tree of directories (path components)3050# generate trie out of those directories that might contain forks3051foreachmy$pr(@$projects) {3052my$path=$pr->{'path'};3053$path=~s/\.git$//;# forks of 'repo.git' are in 'repo/' directory3054next if($path=~m!/$!);# skip non-bare repositories, e.g. 'repo/.git'3055next unless($path);# skip '.git' repository: tests, git-instaweb3056next unless(-d "$projectroot/$path");# containing directory exists3057$pr->{'forks'} = [];# there can be 0 or more forks of project30583059# add to trie3060my@dirs=split('/',$path);3061# walk the trie, until either runs out of components or out of trie3062my$ref= \%trie;3063while(scalar@dirs&&3064exists($ref->{$dirs[0]})) {3065$ref=$ref->{shift@dirs};3066}3067# create rest of trie structure from rest of components3068foreachmy$dir(@dirs) {3069$ref=$ref->{$dir} = {};3070}3071# create end marker, store $pr as a data3072$ref->{''} =$prif(!exists$ref->{''});3073}30743075# filter out forks, by finding shortest prefix match for paths3076my@filtered;3077 PROJECT:3078foreachmy$pr(@$projects) {3079# trie lookup3080my$ref= \%trie;3081 DIR:3082foreachmy$dir(split('/',$pr->{'path'})) {3083if(exists$ref->{''}) {3084# found [shortest] prefix, is a fork - skip it3085push@{$ref->{''}{'forks'}},$pr;3086next PROJECT;3087}3088if(!exists$ref->{$dir}) {3089# not in trie, cannot have prefix, not a fork3090push@filtered,$pr;3091next PROJECT;3092}3093# If the dir is there, we just walk one step down the trie.3094$ref=$ref->{$dir};3095}3096# we ran out of trie3097# (shouldn't happen: it's either no match, or end marker)3098push@filtered,$pr;3099}31003101return@filtered;3102}31033104# note: fill_project_list_info must be run first,3105# for 'descr_long' and 'ctags' to be filled3106sub search_projects_list {3107my($projlist,%opts) =@_;3108my$tagfilter=$opts{'tagfilter'};3109my$search_re=$opts{'search_regexp'};31103111return@$projlist3112unless($tagfilter||$search_re);31133114# searching projects require filling to be run before it;3115 fill_project_list_info($projlist,3116$tagfilter?'ctags': (),3117$search_re? ('path','descr') : ());3118my@projects;3119 PROJECT:3120foreachmy$pr(@$projlist) {31213122if($tagfilter) {3123next unlessref($pr->{'ctags'})eq'HASH';3124next unless3125grep{lc($_)eq lc($tagfilter) }keys%{$pr->{'ctags'}};3126}31273128if($search_re) {3129next unless3130$pr->{'path'} =~/$search_re/||3131$pr->{'descr_long'} =~/$search_re/;3132}31333134push@projects,$pr;3135}31363137return@projects;3138}31393140our$gitweb_project_owner=undef;3141sub git_get_project_list_from_file {31423143return if(defined$gitweb_project_owner);31443145$gitweb_project_owner= {};3146# read from file (url-encoded):3147# 'git%2Fgit.git Linus+Torvalds'3148# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'3149# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'3150if(-f $projects_list) {3151open(my$fd,'<',$projects_list);3152while(my$line= <$fd>) {3153chomp$line;3154my($pr,$ow) =split' ',$line;3155$pr= unescape($pr);3156$ow= unescape($ow);3157$gitweb_project_owner->{$pr} = to_utf8($ow);3158}3159close$fd;3160}3161}31623163sub git_get_project_owner {3164my$project=shift;3165my$owner;31663167returnundefunless$project;3168$git_dir="$projectroot/$project";31693170if(!defined$gitweb_project_owner) {3171 git_get_project_list_from_file();3172}31733174if(exists$gitweb_project_owner->{$project}) {3175$owner=$gitweb_project_owner->{$project};3176}3177if(!defined$owner){3178$owner= git_get_project_config('owner');3179}3180if(!defined$owner) {3181$owner= get_file_owner("$git_dir");3182}31833184return$owner;3185}31863187sub git_get_last_activity {3188my($path) =@_;3189my$fd;31903191$git_dir="$projectroot/$path";3192open($fd,"-|", git_cmd(),'for-each-ref',3193'--format=%(committer)',3194'--sort=-committerdate',3195'--count=1',3196'refs/heads')orreturn;3197my$most_recent= <$fd>;3198close$fdorreturn;3199if(defined$most_recent&&3200$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {3201my$timestamp=$1;3202my$age=time-$timestamp;3203return($age, age_string($age));3204}3205return(undef,undef);3206}32073208# Implementation note: when a single remote is wanted, we cannot use 'git3209# remote show -n' because that command always work (assuming it's a remote URL3210# if it's not defined), and we cannot use 'git remote show' because that would3211# try to make a network roundtrip. So the only way to find if that particular3212# remote is defined is to walk the list provided by 'git remote -v' and stop if3213# and when we find what we want.3214sub git_get_remotes_list {3215my$wanted=shift;3216my%remotes= ();32173218open my$fd,'-|', git_cmd(),'remote','-v';3219return unless$fd;3220while(my$remote= <$fd>) {3221chomp$remote;3222$remote=~s!\t(.*?)\s+\((\w+)\)$!!;3223next if$wantedand not$remoteeq$wanted;3224my($url,$key) = ($1,$2);32253226$remotes{$remote} ||= {'heads'=> () };3227$remotes{$remote}{$key} =$url;3228}3229close$fdorreturn;3230returnwantarray?%remotes: \%remotes;3231}32323233# Takes a hash of remotes as first parameter and fills it by adding the3234# available remote heads for each of the indicated remotes.3235sub fill_remote_heads {3236my$remotes=shift;3237my@heads=map{"remotes/$_"}keys%$remotes;3238my@remoteheads= git_get_heads_list(undef,@heads);3239foreachmy$remote(keys%$remotes) {3240$remotes->{$remote}{'heads'} = [grep{3241$_->{'name'} =~s!^$remote/!!3242}@remoteheads];3243}3244}32453246sub git_get_references {3247my$type=shift||"";3248my%refs;3249# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.113250# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}3251open my$fd,"-|", git_cmd(),"show-ref","--dereference",3252($type? ("--","refs/$type") : ())# use -- <pattern> if $type3253orreturn;32543255while(my$line= <$fd>) {3256chomp$line;3257if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {3258if(defined$refs{$1}) {3259push@{$refs{$1}},$2;3260}else{3261$refs{$1} = [$2];3262}3263}3264}3265close$fdorreturn;3266return \%refs;3267}32683269sub git_get_rev_name_tags {3270my$hash=shift||returnundef;32713272open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash3273orreturn;3274my$name_rev= <$fd>;3275close$fd;32763277if($name_rev=~ m|^$hash tags/(.*)$|) {3278return$1;3279}else{3280# catches also '$hash undefined' output3281returnundef;3282}3283}32843285## ----------------------------------------------------------------------3286## parse to hash functions32873288sub parse_date {3289my$epoch=shift;3290my$tz=shift||"-0000";32913292my%date;3293my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");3294my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");3295my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);3296$date{'hour'} =$hour;3297$date{'minute'} =$min;3298$date{'mday'} =$mday;3299$date{'day'} =$days[$wday];3300$date{'month'} =$months[$mon];3301$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",3302$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;3303$date{'mday-time'} =sprintf"%d%s%02d:%02d",3304$mday,$months[$mon],$hour,$min;3305$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",33061900+$year,1+$mon,$mday,$hour,$min,$sec;33073308my($tz_sign,$tz_hour,$tz_min) =3309($tz=~m/^([-+])(\d\d)(\d\d)$/);3310$tz_sign= ($tz_signeq'-'? -1: +1);3311my$local=$epoch+$tz_sign*((($tz_hour*60) +$tz_min)*60);3312($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);3313$date{'hour_local'} =$hour;3314$date{'minute_local'} =$min;3315$date{'tz_local'} =$tz;3316$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",33171900+$year,$mon+1,$mday,3318$hour,$min,$sec,$tz);3319return%date;3320}33213322sub parse_tag {3323my$tag_id=shift;3324my%tag;3325my@comment;33263327open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;3328$tag{'id'} =$tag_id;3329while(my$line= <$fd>) {3330chomp$line;3331if($line=~m/^object ([0-9a-fA-F]{40})$/) {3332$tag{'object'} =$1;3333}elsif($line=~m/^type (.+)$/) {3334$tag{'type'} =$1;3335}elsif($line=~m/^tag (.+)$/) {3336$tag{'name'} =$1;3337}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {3338$tag{'author'} =$1;3339$tag{'author_epoch'} =$2;3340$tag{'author_tz'} =$3;3341if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {3342$tag{'author_name'} =$1;3343$tag{'author_email'} =$2;3344}else{3345$tag{'author_name'} =$tag{'author'};3346}3347}elsif($line=~m/--BEGIN/) {3348push@comment,$line;3349last;3350}elsif($lineeq"") {3351last;3352}3353}3354push@comment, <$fd>;3355$tag{'comment'} = \@comment;3356close$fdorreturn;3357if(!defined$tag{'name'}) {3358return3359};3360return%tag3361}33623363sub parse_commit_text {3364my($commit_text,$withparents) =@_;3365my@commit_lines=split'\n',$commit_text;3366my%co;33673368pop@commit_lines;# Remove '\0'33693370if(!@commit_lines) {3371return;3372}33733374my$header=shift@commit_lines;3375if($header!~m/^[0-9a-fA-F]{40}/) {3376return;3377}3378($co{'id'},my@parents) =split' ',$header;3379while(my$line=shift@commit_lines) {3380last if$lineeq"\n";3381if($line=~m/^tree ([0-9a-fA-F]{40})$/) {3382$co{'tree'} =$1;3383}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {3384push@parents,$1;3385}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {3386$co{'author'} = to_utf8($1);3387$co{'author_epoch'} =$2;3388$co{'author_tz'} =$3;3389if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {3390$co{'author_name'} =$1;3391$co{'author_email'} =$2;3392}else{3393$co{'author_name'} =$co{'author'};3394}3395}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {3396$co{'committer'} = to_utf8($1);3397$co{'committer_epoch'} =$2;3398$co{'committer_tz'} =$3;3399if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {3400$co{'committer_name'} =$1;3401$co{'committer_email'} =$2;3402}else{3403$co{'committer_name'} =$co{'committer'};3404}3405}3406}3407if(!defined$co{'tree'}) {3408return;3409};3410$co{'parents'} = \@parents;3411$co{'parent'} =$parents[0];34123413foreachmy$title(@commit_lines) {3414$title=~s/^ //;3415if($titlene"") {3416$co{'title'} = chop_str($title,80,5);3417# remove leading stuff of merges to make the interesting part visible3418if(length($title) >50) {3419$title=~s/^Automatic //;3420$title=~s/^merge (of|with) /Merge ... /i;3421if(length($title) >50) {3422$title=~s/(http|rsync):\/\///;3423}3424if(length($title) >50) {3425$title=~s/(master|www|rsync)\.//;3426}3427if(length($title) >50) {3428$title=~s/kernel.org:?//;3429}3430if(length($title) >50) {3431$title=~s/\/pub\/scm//;3432}3433}3434$co{'title_short'} = chop_str($title,50,5);3435last;3436}3437}3438if(!defined$co{'title'} ||$co{'title'}eq"") {3439$co{'title'} =$co{'title_short'} ='(no commit message)';3440}3441# remove added spaces3442foreachmy$line(@commit_lines) {3443$line=~s/^ //;3444}3445$co{'comment'} = \@commit_lines;34463447my$age=time-$co{'committer_epoch'};3448$co{'age'} =$age;3449$co{'age_string'} = age_string($age);3450my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});3451if($age>60*60*24*7*2) {3452$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3453$co{'age_string_age'} =$co{'age_string'};3454}else{3455$co{'age_string_date'} =$co{'age_string'};3456$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3457}3458return%co;3459}34603461sub parse_commit {3462my($commit_id) =@_;3463my%co;34643465local$/="\0";34663467open my$fd,"-|", git_cmd(),"rev-list",3468"--parents",3469"--header",3470"--max-count=1",3471$commit_id,3472"--",3473or die_error(500,"Open git-rev-list failed");3474%co= parse_commit_text(<$fd>,1);3475close$fd;34763477return%co;3478}34793480sub parse_commits {3481my($commit_id,$maxcount,$skip,$filename,@args) =@_;3482my@cos;34833484$maxcount||=1;3485$skip||=0;34863487local$/="\0";34883489open my$fd,"-|", git_cmd(),"rev-list",3490"--header",3491@args,3492("--max-count=".$maxcount),3493("--skip=".$skip),3494@extra_options,3495$commit_id,3496"--",3497($filename? ($filename) : ())3498or die_error(500,"Open git-rev-list failed");3499while(my$line= <$fd>) {3500my%co= parse_commit_text($line);3501push@cos, \%co;3502}3503close$fd;35043505returnwantarray?@cos: \@cos;3506}35073508# parse line of git-diff-tree "raw" output3509sub parse_difftree_raw_line {3510my$line=shift;3511my%res;35123513# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'3514# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'3515if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {3516$res{'from_mode'} =$1;3517$res{'to_mode'} =$2;3518$res{'from_id'} =$3;3519$res{'to_id'} =$4;3520$res{'status'} =$5;3521$res{'similarity'} =$6;3522if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3523($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3524}else{3525$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3526}3527}3528# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3529# combined diff (for merge commit)3530elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3531$res{'nparents'} =length($1);3532$res{'from_mode'} = [split(' ',$2) ];3533$res{'to_mode'} =pop@{$res{'from_mode'}};3534$res{'from_id'} = [split(' ',$3) ];3535$res{'to_id'} =pop@{$res{'from_id'}};3536$res{'status'} = [split('',$4) ];3537$res{'to_file'} = unquote($5);3538}3539# 'c512b523472485aef4fff9e57b229d9d243c967f'3540elsif($line=~m/^([0-9a-fA-F]{40})$/) {3541$res{'commit'} =$1;3542}35433544returnwantarray?%res: \%res;3545}35463547# wrapper: return parsed line of git-diff-tree "raw" output3548# (the argument might be raw line, or parsed info)3549sub parsed_difftree_line {3550my$line_or_ref=shift;35513552if(ref($line_or_ref)eq"HASH") {3553# pre-parsed (or generated by hand)3554return$line_or_ref;3555}else{3556return parse_difftree_raw_line($line_or_ref);3557}3558}35593560# parse line of git-ls-tree output3561sub parse_ls_tree_line {3562my$line=shift;3563my%opts=@_;3564my%res;35653566if($opts{'-l'}) {3567#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3568$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;35693570$res{'mode'} =$1;3571$res{'type'} =$2;3572$res{'hash'} =$3;3573$res{'size'} =$4;3574if($opts{'-z'}) {3575$res{'name'} =$5;3576}else{3577$res{'name'} = unquote($5);3578}3579}else{3580#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3581$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;35823583$res{'mode'} =$1;3584$res{'type'} =$2;3585$res{'hash'} =$3;3586if($opts{'-z'}) {3587$res{'name'} =$4;3588}else{3589$res{'name'} = unquote($4);3590}3591}35923593returnwantarray?%res: \%res;3594}35953596# generates _two_ hashes, references to which are passed as 2 and 3 argument3597sub parse_from_to_diffinfo {3598my($diffinfo,$from,$to,@parents) =@_;35993600if($diffinfo->{'nparents'}) {3601# combined diff3602$from->{'file'} = [];3603$from->{'href'} = [];3604 fill_from_file_info($diffinfo,@parents)3605unlessexists$diffinfo->{'from_file'};3606for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3607$from->{'file'}[$i] =3608defined$diffinfo->{'from_file'}[$i] ?3609$diffinfo->{'from_file'}[$i] :3610$diffinfo->{'to_file'};3611if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3612$from->{'href'}[$i] = href(action=>"blob",3613 hash_base=>$parents[$i],3614 hash=>$diffinfo->{'from_id'}[$i],3615 file_name=>$from->{'file'}[$i]);3616}else{3617$from->{'href'}[$i] =undef;3618}3619}3620}else{3621# ordinary (not combined) diff3622$from->{'file'} =$diffinfo->{'from_file'};3623if($diffinfo->{'status'}ne"A") {# not new (added) file3624$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3625 hash=>$diffinfo->{'from_id'},3626 file_name=>$from->{'file'});3627}else{3628delete$from->{'href'};3629}3630}36313632$to->{'file'} =$diffinfo->{'to_file'};3633if(!is_deleted($diffinfo)) {# file exists in result3634$to->{'href'} = href(action=>"blob", hash_base=>$hash,3635 hash=>$diffinfo->{'to_id'},3636 file_name=>$to->{'file'});3637}else{3638delete$to->{'href'};3639}3640}36413642## ......................................................................3643## parse to array of hashes functions36443645sub git_get_heads_list {3646my($limit,@classes) =@_;3647@classes= ('heads')unless@classes;3648my@patterns=map{"refs/$_"}@classes;3649my@headslist;36503651open my$fd,'-|', git_cmd(),'for-each-ref',3652($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3653'--format=%(objectname) %(refname) %(subject)%00%(committer)',3654@patterns3655orreturn;3656while(my$line= <$fd>) {3657my%ref_item;36583659chomp$line;3660my($refinfo,$committerinfo) =split(/\0/,$line);3661my($hash,$name,$title) =split(' ',$refinfo,3);3662my($committer,$epoch,$tz) =3663($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3664$ref_item{'fullname'} =$name;3665$name=~s!^refs/(?:head|remote)s/!!;36663667$ref_item{'name'} =$name;3668$ref_item{'id'} =$hash;3669$ref_item{'title'} =$title||'(no commit message)';3670$ref_item{'epoch'} =$epoch;3671if($epoch) {3672$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3673}else{3674$ref_item{'age'} ="unknown";3675}36763677push@headslist, \%ref_item;3678}3679close$fd;36803681returnwantarray?@headslist: \@headslist;3682}36833684sub git_get_tags_list {3685my$limit=shift;3686my@tagslist;36873688open my$fd,'-|', git_cmd(),'for-each-ref',3689($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3690'--format=%(objectname) %(objecttype) %(refname) '.3691'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3692'refs/tags'3693orreturn;3694while(my$line= <$fd>) {3695my%ref_item;36963697chomp$line;3698my($refinfo,$creatorinfo) =split(/\0/,$line);3699my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3700my($creator,$epoch,$tz) =3701($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3702$ref_item{'fullname'} =$name;3703$name=~s!^refs/tags/!!;37043705$ref_item{'type'} =$type;3706$ref_item{'id'} =$id;3707$ref_item{'name'} =$name;3708if($typeeq"tag") {3709$ref_item{'subject'} =$title;3710$ref_item{'reftype'} =$reftype;3711$ref_item{'refid'} =$refid;3712}else{3713$ref_item{'reftype'} =$type;3714$ref_item{'refid'} =$id;3715}37163717if($typeeq"tag"||$typeeq"commit") {3718$ref_item{'epoch'} =$epoch;3719if($epoch) {3720$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3721}else{3722$ref_item{'age'} ="unknown";3723}3724}37253726push@tagslist, \%ref_item;3727}3728close$fd;37293730returnwantarray?@tagslist: \@tagslist;3731}37323733## ----------------------------------------------------------------------3734## filesystem-related functions37353736sub get_file_owner {3737my$path=shift;37383739my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3740my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3741if(!defined$gcos) {3742returnundef;3743}3744my$owner=$gcos;3745$owner=~s/[,;].*$//;3746return to_utf8($owner);3747}37483749# assume that file exists3750sub insert_file {3751my$filename=shift;37523753open my$fd,'<',$filename;3754print map{ to_utf8($_) } <$fd>;3755close$fd;3756}37573758## ......................................................................3759## mimetype related functions37603761sub mimetype_guess_file {3762my$filename=shift;3763my$mimemap=shift;3764-r $mimemaporreturnundef;37653766my%mimemap;3767open(my$mh,'<',$mimemap)orreturnundef;3768while(<$mh>) {3769next ifm/^#/;# skip comments3770my($mimetype,@exts) =split(/\s+/);3771foreachmy$ext(@exts) {3772$mimemap{$ext} =$mimetype;3773}3774}3775close($mh);37763777$filename=~/\.([^.]*)$/;3778return$mimemap{$1};3779}37803781sub mimetype_guess {3782my$filename=shift;3783my$mime;3784$filename=~/\./orreturnundef;37853786if($mimetypes_file) {3787my$file=$mimetypes_file;3788if($file!~m!^/!) {# if it is relative path3789# it is relative to project3790$file="$projectroot/$project/$file";3791}3792$mime= mimetype_guess_file($filename,$file);3793}3794$mime||= mimetype_guess_file($filename,'/etc/mime.types');3795return$mime;3796}37973798sub blob_mimetype {3799my$fd=shift;3800my$filename=shift;38013802if($filename) {3803my$mime= mimetype_guess($filename);3804$mimeandreturn$mime;3805}38063807# just in case3808return$default_blob_plain_mimetypeunless$fd;38093810if(-T $fd) {3811return'text/plain';3812}elsif(!$filename) {3813return'application/octet-stream';3814}elsif($filename=~m/\.png$/i) {3815return'image/png';3816}elsif($filename=~m/\.gif$/i) {3817return'image/gif';3818}elsif($filename=~m/\.jpe?g$/i) {3819return'image/jpeg';3820}else{3821return'application/octet-stream';3822}3823}38243825sub blob_contenttype {3826my($fd,$file_name,$type) =@_;38273828$type||= blob_mimetype($fd,$file_name);3829if($typeeq'text/plain'&&defined$default_text_plain_charset) {3830$type.="; charset=$default_text_plain_charset";3831}38323833return$type;3834}38353836# guess file syntax for syntax highlighting; return undef if no highlighting3837# the name of syntax can (in the future) depend on syntax highlighter used3838sub guess_file_syntax {3839my($highlight,$mimetype,$file_name) =@_;3840returnundefunless($highlight&&defined$file_name);3841my$basename= basename($file_name,'.in');3842return$highlight_basename{$basename}3843ifexists$highlight_basename{$basename};38443845$basename=~/\.([^.]*)$/;3846my$ext=$1orreturnundef;3847return$highlight_ext{$ext}3848ifexists$highlight_ext{$ext};38493850returnundef;3851}38523853# run highlighter and return FD of its output,3854# or return original FD if no highlighting3855sub run_highlighter {3856my($fd,$highlight,$syntax) =@_;3857return$fdunless($highlight&&defined$syntax);38583859close$fd;3860open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3861 quote_command($highlight_bin).3862" --replace-tabs=8 --fragment --syntax$syntax|"3863or die_error(500,"Couldn't open file or run syntax highlighter");3864return$fd;3865}38663867## ======================================================================3868## functions printing HTML: header, footer, error page38693870sub get_page_title {3871my$title= to_utf8($site_name);38723873unless(defined$project) {3874if(defined$project_filter) {3875$title.=" - projects in '". esc_path($project_filter) ."'";3876}3877return$title;3878}3879$title.=" - ". to_utf8($project);38803881return$titleunless(defined$action);3882$title.="/$action";# $action is US-ASCII (7bit ASCII)38833884return$titleunless(defined$file_name);3885$title.=" - ". esc_path($file_name);3886if($actioneq"tree"&&$file_name!~ m|/$|) {3887$title.="/";3888}38893890return$title;3891}38923893sub get_content_type_html {3894# require explicit support from the UA if we are to send the page as3895# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3896# we have to do this because MSIE sometimes globs '*/*', pretending to3897# support xhtml+xml but choking when it gets what it asked for.3898if(defined$cgi->http('HTTP_ACCEPT') &&3899$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3900$cgi->Accept('application/xhtml+xml') !=0) {3901return'application/xhtml+xml';3902}else{3903return'text/html';3904}3905}39063907sub print_feed_meta {3908if(defined$project) {3909my%href_params= get_feed_info();3910if(!exists$href_params{'-title'}) {3911$href_params{'-title'} ='log';3912}39133914foreachmy$format(qw(RSS Atom)) {3915my$type=lc($format);3916my%link_attr= (3917'-rel'=>'alternate',3918'-title'=> esc_attr("$project-$href_params{'-title'} -$formatfeed"),3919'-type'=>"application/$type+xml"3920);39213922$href_params{'extra_options'} =undef;3923$href_params{'action'} =$type;3924$link_attr{'-href'} = href(%href_params);3925print"<link ".3926"rel=\"$link_attr{'-rel'}\"".3927"title=\"$link_attr{'-title'}\"".3928"href=\"$link_attr{'-href'}\"".3929"type=\"$link_attr{'-type'}\"".3930"/>\n";39313932$href_params{'extra_options'} ='--no-merges';3933$link_attr{'-href'} = href(%href_params);3934$link_attr{'-title'} .=' (no merges)';3935print"<link ".3936"rel=\"$link_attr{'-rel'}\"".3937"title=\"$link_attr{'-title'}\"".3938"href=\"$link_attr{'-href'}\"".3939"type=\"$link_attr{'-type'}\"".3940"/>\n";3941}39423943}else{3944printf('<link rel="alternate" title="%sprojects list" '.3945'href="%s" type="text/plain; charset=utf-8" />'."\n",3946 esc_attr($site_name), href(project=>undef, action=>"project_index"));3947printf('<link rel="alternate" title="%sprojects feeds" '.3948'href="%s" type="text/x-opml" />'."\n",3949 esc_attr($site_name), href(project=>undef, action=>"opml"));3950}3951}39523953sub print_header_links {3954my$status=shift;39553956# print out each stylesheet that exist, providing backwards capability3957# for those people who defined $stylesheet in a config file3958if(defined$stylesheet) {3959print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3960}else{3961foreachmy$stylesheet(@stylesheets) {3962next unless$stylesheet;3963print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3964}3965}3966 print_feed_meta()3967if($statuseq'200 OK');3968if(defined$favicon) {3969printqq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);3970}3971}39723973sub print_nav_breadcrumbs_path {3974my$dirprefix=undef;3975while(my$part=shift) {3976$dirprefix.="/"ifdefined$dirprefix;3977$dirprefix.=$part;3978print$cgi->a({-href => href(project =>undef,3979 project_filter =>$dirprefix,3980 action =>"project_list")},3981 esc_html($part)) ." / ";3982}3983}39843985sub print_nav_breadcrumbs {3986my%opts=@_;39873988formy$crumb(@extra_breadcrumbs, [$home_link_str=>$home_link]) {3989print$cgi->a({-href => esc_url($crumb->[1])},$crumb->[0]) ." / ";3990}3991if(defined$project) {3992my@dirname=split'/',$project;3993my$projectbasename=pop@dirname;3994 print_nav_breadcrumbs_path(@dirname);3995print$cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));3996if(defined$action) {3997my$action_print=$action;3998if(defined$opts{-action_extra}) {3999$action_print=$cgi->a({-href => href(action=>$action)},4000$action);4001}4002print" /$action_print";4003}4004if(defined$opts{-action_extra}) {4005print" /$opts{-action_extra}";4006}4007print"\n";4008}elsif(defined$project_filter) {4009 print_nav_breadcrumbs_path(split'/',$project_filter);4010}4011}40124013sub print_search_form {4014if(!defined$searchtext) {4015$searchtext="";4016}4017my$search_hash;4018if(defined$hash_base) {4019$search_hash=$hash_base;4020}elsif(defined$hash) {4021$search_hash=$hash;4022}else{4023$search_hash="HEAD";4024}4025my$action=$my_uri;4026my$use_pathinfo= gitweb_check_feature('pathinfo');4027if($use_pathinfo) {4028$action.="/".esc_url($project);4029}4030print$cgi->startform(-method=>"get", -action =>$action) .4031"<div class=\"search\">\n".4032(!$use_pathinfo&&4033$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .4034$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".4035$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".4036$cgi->popup_menu(-name =>'st', -default=>'commit',4037-values=> ['commit','grep','author','committer','pickaxe']) .4038$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .4039" search:\n",4040$cgi->textfield(-name =>"s", -value =>$searchtext, -override =>1) ."\n".4041"<span title=\"Extended regular expression\">".4042$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',4043-checked =>$search_use_regexp) .4044"</span>".4045"</div>".4046$cgi->end_form() ."\n";4047}40484049sub git_header_html {4050my$status=shift||"200 OK";4051my$expires=shift;4052my%opts=@_;40534054my$title= get_page_title();4055my$content_type= get_content_type_html();4056print$cgi->header(-type=>$content_type, -charset =>'utf-8',4057-status=>$status, -expires =>$expires)4058unless($opts{'-no_http_header'});4059my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';4060print<<EOF;4061<?xml version="1.0" encoding="utf-8"?>4062<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4063<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">4064<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->4065<!-- git core binaries version$git_version-->4066<head>4067<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>4068<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>4069<meta name="robots" content="index, nofollow"/>4070<title>$title</title>4071EOF4072# the stylesheet, favicon etc urls won't work correctly with path_info4073# unless we set the appropriate base URL4074if($ENV{'PATH_INFO'}) {4075print"<base href=\"".esc_url($base_url)."\"/>\n";4076}4077 print_header_links($status);40784079if(defined$site_html_head_string) {4080print to_utf8($site_html_head_string);4081}40824083print"</head>\n".4084"<body>\n";40854086if(defined$site_header&& -f $site_header) {4087 insert_file($site_header);4088}40894090print"<div class=\"page_header\">\n";4091if(defined$logo) {4092print$cgi->a({-href => esc_url($logo_url),4093-title =>$logo_label},4094$cgi->img({-src => esc_url($logo),4095-width =>72, -height =>27,4096-alt =>"git",4097-class=>"logo"}));4098}4099 print_nav_breadcrumbs(%opts);4100print"</div>\n";41014102my$have_search= gitweb_check_feature('search');4103if(defined$project&&$have_search) {4104 print_search_form();4105}4106}41074108sub git_footer_html {4109my$feed_class='rss_logo';41104111print"<div class=\"page_footer\">\n";4112if(defined$project) {4113my$descr= git_get_project_description($project);4114if(defined$descr) {4115print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";4116}41174118my%href_params= get_feed_info();4119if(!%href_params) {4120$feed_class.=' generic';4121}4122$href_params{'-title'} ||='log';41234124foreachmy$format(qw(RSS Atom)) {4125$href_params{'action'} =lc($format);4126print$cgi->a({-href => href(%href_params),4127-title =>"$href_params{'-title'}$formatfeed",4128-class=>$feed_class},$format)."\n";4129}41304131}else{4132print$cgi->a({-href => href(project=>undef, action=>"opml",4133 project_filter =>$project_filter),4134-class=>$feed_class},"OPML") ." ";4135print$cgi->a({-href => href(project=>undef, action=>"project_index",4136 project_filter =>$project_filter),4137-class=>$feed_class},"TXT") ."\n";4138}4139print"</div>\n";# class="page_footer"41404141if(defined$t0&& gitweb_check_feature('timed')) {4142print"<div id=\"generating_info\">\n";4143print'This page took '.4144'<span id="generating_time" class="time_span">'.4145 tv_interval($t0, [ gettimeofday() ]).4146' seconds </span>'.4147' and '.4148'<span id="generating_cmd">'.4149$number_of_git_cmds.4150'</span> git commands '.4151" to generate.\n";4152print"</div>\n";# class="page_footer"4153}41544155if(defined$site_footer&& -f $site_footer) {4156 insert_file($site_footer);4157}41584159print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;4160if(defined$action&&4161$actioneq'blame_incremental') {4162print qq!<script type="text/javascript">\n!.4163 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.4164 qq!"!. href() .qq!");\n!.4165 qq!</script>\n!;4166}else{4167my($jstimezone,$tz_cookie,$datetime_class) =4168 gitweb_get_feature('javascript-timezone');41694170print qq!<script type="text/javascript">\n!.4171 qq!window.onload = function () {\n!;4172if(gitweb_check_feature('javascript-actions')) {4173print qq! fixLinks();\n!;4174}4175if($jstimezone&&$tz_cookie&&$datetime_class) {4176print qq! var tz_cookie = { name:'$tz_cookie', expires:14, path:'/'};\n!.# in days4177 qq! onloadTZSetup('$jstimezone', tz_cookie,'$datetime_class');\n!;4178}4179print qq!};\n!.4180 qq!</script>\n!;4181}41824183print"</body>\n".4184"</html>";4185}41864187# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])4188# Example: die_error(404, 'Hash not found')4189# By convention, use the following status codes (as defined in RFC 2616):4190# 400: Invalid or missing CGI parameters, or4191# requested object exists but has wrong type.4192# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on4193# this server or project.4194# 404: Requested object/revision/project doesn't exist.4195# 500: The server isn't configured properly, or4196# an internal error occurred (e.g. failed assertions caused by bugs), or4197# an unknown error occurred (e.g. the git binary died unexpectedly).4198# 503: The server is currently unavailable (because it is overloaded,4199# or down for maintenance). Generally, this is a temporary state.4200sub die_error {4201my$status=shift||500;4202my$error= esc_html(shift) ||"Internal Server Error";4203my$extra=shift;4204my%opts=@_;42054206my%http_responses= (4207400=>'400 Bad Request',4208403=>'403 Forbidden',4209404=>'404 Not Found',4210500=>'500 Internal Server Error',4211503=>'503 Service Unavailable',4212);4213 git_header_html($http_responses{$status},undef,%opts);4214print<<EOF;4215<div class="page_body">4216<br /><br />4217$status-$error4218<br />4219EOF4220if(defined$extra) {4221print"<hr />\n".4222"$extra\n";4223}4224print"</div>\n";42254226 git_footer_html();4227goto DONE_GITWEB4228unless($opts{'-error_handler'});4229}42304231## ----------------------------------------------------------------------4232## functions printing or outputting HTML: navigation42334234sub git_print_page_nav {4235my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;4236$extra=''if!defined$extra;# pager or formats42374238my@navs=qw(summary shortlog log commit commitdiff tree);4239if($suppress) {4240@navs=grep{$_ne$suppress}@navs;4241}42424243my%arg=map{$_=> {action=>$_} }@navs;4244if(defined$head) {4245for(qw(commit commitdiff)) {4246$arg{$_}{'hash'} =$head;4247}4248if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {4249for(qw(shortlog log)) {4250$arg{$_}{'hash'} =$head;4251}4252}4253}42544255$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;4256$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;42574258my@actions= gitweb_get_feature('actions');4259my%repl= (4260'%'=>'%',4261'n'=>$project,# project name4262'f'=>$git_dir,# project path within filesystem4263'h'=>$treehead||'',# current hash ('h' parameter)4264'b'=>$treebase||'',# hash base ('hb' parameter)4265);4266while(@actions) {4267my($label,$link,$pos) =splice(@actions,0,3);4268# insert4269@navs=map{$_eq$pos? ($_,$label) :$_}@navs;4270# munch munch4271$link=~s/%([%nfhb])/$repl{$1}/g;4272$arg{$label}{'_href'} =$link;4273}42744275print"<div class=\"page_nav\">\n".4276(join" | ",4277map{$_eq$current?4278$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")4279}@navs);4280print"<br/>\n$extra<br/>\n".4281"</div>\n";4282}42834284# returns a submenu for the nagivation of the refs views (tags, heads,4285# remotes) with the current view disabled and the remotes view only4286# available if the feature is enabled4287sub format_ref_views {4288my($current) =@_;4289my@ref_views=qw{tags heads};4290push@ref_views,'remotes'if gitweb_check_feature('remote_heads');4291returnjoin" | ",map{4292$_eq$current?$_:4293$cgi->a({-href => href(action=>$_)},$_)4294}@ref_views4295}42964297sub format_paging_nav {4298my($action,$page,$has_next_link) =@_;4299my$paging_nav;430043014302if($page>0) {4303$paging_nav.=4304$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .4305" ⋅ ".4306$cgi->a({-href => href(-replay=>1, page=>$page-1),4307-accesskey =>"p", -title =>"Alt-p"},"prev");4308}else{4309$paging_nav.="first ⋅ prev";4310}43114312if($has_next_link) {4313$paging_nav.=" ⋅ ".4314$cgi->a({-href => href(-replay=>1, page=>$page+1),4315-accesskey =>"n", -title =>"Alt-n"},"next");4316}else{4317$paging_nav.=" ⋅ next";4318}43194320return$paging_nav;4321}43224323## ......................................................................4324## functions printing or outputting HTML: div43254326sub git_print_header_div {4327my($action,$title,$hash,$hash_base) =@_;4328my%args= ();43294330$args{'action'} =$action;4331$args{'hash'} =$hashif$hash;4332$args{'hash_base'} =$hash_baseif$hash_base;43334334print"<div class=\"header\">\n".4335$cgi->a({-href => href(%args), -class=>"title"},4336$title?$title:$action) .4337"\n</div>\n";4338}43394340sub format_repo_url {4341my($name,$url) =@_;4342return"<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";4343}43444345# Group output by placing it in a DIV element and adding a header.4346# Options for start_div() can be provided by passing a hash reference as the4347# first parameter to the function.4348# Options to git_print_header_div() can be provided by passing an array4349# reference. This must follow the options to start_div if they are present.4350# The content can be a scalar, which is output as-is, a scalar reference, which4351# is output after html escaping, an IO handle passed either as *handle or4352# *handle{IO}, or a function reference. In the latter case all following4353# parameters will be taken as argument to the content function call.4354sub git_print_section {4355my($div_args,$header_args,$content);4356my$arg=shift;4357if(ref($arg)eq'HASH') {4358$div_args=$arg;4359$arg=shift;4360}4361if(ref($arg)eq'ARRAY') {4362$header_args=$arg;4363$arg=shift;4364}4365$content=$arg;43664367print$cgi->start_div($div_args);4368 git_print_header_div(@$header_args);43694370if(ref($content)eq'CODE') {4371$content->(@_);4372}elsif(ref($content)eq'SCALAR') {4373print esc_html($$content);4374}elsif(ref($content)eq'GLOB'or ref($content)eq'IO::Handle') {4375print<$content>;4376}elsif(!ref($content) &&defined($content)) {4377print$content;4378}43794380print$cgi->end_div;4381}43824383sub format_timestamp_html {4384my$date=shift;4385my$strtime=$date->{'rfc2822'};43864387my(undef,undef,$datetime_class) =4388 gitweb_get_feature('javascript-timezone');4389if($datetime_class) {4390$strtime= qq!<span class="$datetime_class">$strtime</span>!;4391}43924393my$localtime_format='(%02d:%02d%s)';4394if($date->{'hour_local'} <6) {4395$localtime_format='(<span class="atnight">%02d:%02d</span>%s)';4396}4397$strtime.=' '.4398sprintf($localtime_format,4399$date->{'hour_local'},$date->{'minute_local'},$date->{'tz_local'});44004401return$strtime;4402}44034404# Outputs the author name and date in long form4405sub git_print_authorship {4406my$co=shift;4407my%opts=@_;4408my$tag=$opts{-tag} ||'div';4409my$author=$co->{'author_name'};44104411my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});4412print"<$tagclass=\"author_date\">".4413 format_search_author($author,"author", esc_html($author)) .4414" [".format_timestamp_html(\%ad)."]".4415 git_get_avatar($co->{'author_email'}, -pad_before =>1) .4416"</$tag>\n";4417}44184419# Outputs table rows containing the full author or committer information,4420# in the format expected for 'commit' view (& similar).4421# Parameters are a commit hash reference, followed by the list of people4422# to output information for. If the list is empty it defaults to both4423# author and committer.4424sub git_print_authorship_rows {4425my$co=shift;4426# too bad we can't use @people = @_ || ('author', 'committer')4427my@people=@_;4428@people= ('author','committer')unless@people;4429foreachmy$who(@people) {4430my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});4431print"<tr><td>$who</td><td>".4432 format_search_author($co->{"${who}_name"},$who,4433 esc_html($co->{"${who}_name"})) ." ".4434 format_search_author($co->{"${who}_email"},$who,4435 esc_html("<".$co->{"${who}_email"} .">")) .4436"</td><td rowspan=\"2\">".4437 git_get_avatar($co->{"${who}_email"}, -size =>'double') .4438"</td></tr>\n".4439"<tr>".4440"<td></td><td>".4441 format_timestamp_html(\%wd) .4442"</td>".4443"</tr>\n";4444}4445}44464447sub git_print_page_path {4448my$name=shift;4449my$type=shift;4450my$hb=shift;445144524453print"<div class=\"page_path\">";4454print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),4455-title =>'tree root'}, to_utf8("[$project]"));4456print" / ";4457if(defined$name) {4458my@dirname=split'/',$name;4459my$basename=pop@dirname;4460my$fullname='';44614462foreachmy$dir(@dirname) {4463$fullname.= ($fullname?'/':'') .$dir;4464print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,4465 hash_base=>$hb),4466-title =>$fullname}, esc_path($dir));4467print" / ";4468}4469if(defined$type&&$typeeq'blob') {4470print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,4471 hash_base=>$hb),4472-title =>$name}, esc_path($basename));4473}elsif(defined$type&&$typeeq'tree') {4474print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,4475 hash_base=>$hb),4476-title =>$name}, esc_path($basename));4477print" / ";4478}else{4479print esc_path($basename);4480}4481}4482print"<br/></div>\n";4483}44844485sub git_print_log {4486my$log=shift;4487my%opts=@_;44884489if($opts{'-remove_title'}) {4490# remove title, i.e. first line of log4491shift@$log;4492}4493# remove leading empty lines4494while(defined$log->[0] &&$log->[0]eq"") {4495shift@$log;4496}44974498# print log4499my$skip_blank_line=0;4500foreachmy$line(@$log) {4501if($line=~m/^\s*([A-Z][-A-Za-z]*-[Bb]y|C[Cc]): /) {4502if(!$opts{'-remove_signoff'}) {4503print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";4504$skip_blank_line=1;4505}4506next;4507}45084509if($line=~ m,\s*([a-z]*link): (https?://\S+),i) {4510if(!$opts{'-remove_signoff'}) {4511print"<span class=\"signoff\">". esc_html($1) .": ".4512"<a href=\"". esc_html($2) ."\">". esc_html($2) ."</a>".4513"</span><br/>\n";4514$skip_blank_line=1;4515}4516next;4517}45184519# print only one empty line4520# do not print empty line after signoff4521if($lineeq"") {4522next if($skip_blank_line);4523$skip_blank_line=1;4524}else{4525$skip_blank_line=0;4526}45274528print format_log_line_html($line) ."<br/>\n";4529}45304531if($opts{'-final_empty_line'}) {4532# end with single empty line4533print"<br/>\n"unless$skip_blank_line;4534}4535}45364537# return link target (what link points to)4538sub git_get_link_target {4539my$hash=shift;4540my$link_target;45414542# read link4543open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4544orreturn;4545{4546local$/=undef;4547$link_target= <$fd>;4548}4549close$fd4550orreturn;45514552return$link_target;4553}45544555# given link target, and the directory (basedir) the link is in,4556# return target of link relative to top directory (top tree);4557# return undef if it is not possible (including absolute links).4558sub normalize_link_target {4559my($link_target,$basedir) =@_;45604561# absolute symlinks (beginning with '/') cannot be normalized4562return if(substr($link_target,0,1)eq'/');45634564# normalize link target to path from top (root) tree (dir)4565my$path;4566if($basedir) {4567$path=$basedir.'/'.$link_target;4568}else{4569# we are in top (root) tree (dir)4570$path=$link_target;4571}45724573# remove //, /./, and /../4574my@path_parts;4575foreachmy$part(split('/',$path)) {4576# discard '.' and ''4577next if(!$part||$parteq'.');4578# handle '..'4579if($parteq'..') {4580if(@path_parts) {4581pop@path_parts;4582}else{4583# link leads outside repository (outside top dir)4584return;4585}4586}else{4587push@path_parts,$part;4588}4589}4590$path=join('/',@path_parts);45914592return$path;4593}45944595# print tree entry (row of git_tree), but without encompassing <tr> element4596sub git_print_tree_entry {4597my($t,$basedir,$hash_base,$have_blame) =@_;45984599my%base_key= ();4600$base_key{'hash_base'} =$hash_baseifdefined$hash_base;46014602# The format of a table row is: mode list link. Where mode is4603# the mode of the entry, list is the name of the entry, an href,4604# and link is the action links of the entry.46054606print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";4607if(exists$t->{'size'}) {4608print"<td class=\"size\">$t->{'size'}</td>\n";4609}4610if($t->{'type'}eq"blob") {4611print"<td class=\"list\">".4612$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4613 file_name=>"$basedir$t->{'name'}",%base_key),4614-class=>"list"}, esc_path($t->{'name'}));4615if(S_ISLNK(oct$t->{'mode'})) {4616my$link_target= git_get_link_target($t->{'hash'});4617if($link_target) {4618my$norm_target= normalize_link_target($link_target,$basedir);4619if(defined$norm_target) {4620print" -> ".4621$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,4622 file_name=>$norm_target),4623-title =>$norm_target}, esc_path($link_target));4624}else{4625print" -> ". esc_path($link_target);4626}4627}4628}4629print"</td>\n";4630print"<td class=\"link\">";4631print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4632 file_name=>"$basedir$t->{'name'}",%base_key)},4633"blob");4634if($have_blame) {4635print" | ".4636$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4637 file_name=>"$basedir$t->{'name'}",%base_key)},4638"blame");4639}4640if(defined$hash_base) {4641print" | ".4642$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4643 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4644"history");4645}4646print" | ".4647$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4648 file_name=>"$basedir$t->{'name'}")},4649"raw");4650print"</td>\n";46514652}elsif($t->{'type'}eq"tree") {4653print"<td class=\"list\">";4654print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4655 file_name=>"$basedir$t->{'name'}",4656%base_key)},4657 esc_path($t->{'name'}));4658print"</td>\n";4659print"<td class=\"link\">";4660print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4661 file_name=>"$basedir$t->{'name'}",4662%base_key)},4663"tree");4664if(defined$hash_base) {4665print" | ".4666$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4667 file_name=>"$basedir$t->{'name'}")},4668"history");4669}4670print"</td>\n";4671}else{4672# unknown object: we can only present history for it4673# (this includes 'commit' object, i.e. submodule support)4674print"<td class=\"list\">".4675 esc_path($t->{'name'}) .4676"</td>\n";4677print"<td class=\"link\">";4678if(defined$hash_base) {4679print$cgi->a({-href => href(action=>"history",4680 hash_base=>$hash_base,4681 file_name=>"$basedir$t->{'name'}")},4682"history");4683}4684print"</td>\n";4685}4686}46874688## ......................................................................4689## functions printing large fragments of HTML46904691# get pre-image filenames for merge (combined) diff4692sub fill_from_file_info {4693my($diff,@parents) =@_;46944695$diff->{'from_file'} = [ ];4696$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4697for(my$i=0;$i<$diff->{'nparents'};$i++) {4698if($diff->{'status'}[$i]eq'R'||4699$diff->{'status'}[$i]eq'C') {4700$diff->{'from_file'}[$i] =4701 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4702}4703}47044705return$diff;4706}47074708# is current raw difftree line of file deletion4709sub is_deleted {4710my$diffinfo=shift;47114712return$diffinfo->{'to_id'}eq('0' x 40);4713}47144715# does patch correspond to [previous] difftree raw line4716# $diffinfo - hashref of parsed raw diff format4717# $patchinfo - hashref of parsed patch diff format4718# (the same keys as in $diffinfo)4719sub is_patch_split {4720my($diffinfo,$patchinfo) =@_;47214722returndefined$diffinfo&&defined$patchinfo4723&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4724}472547264727sub git_difftree_body {4728my($difftree,$hash,@parents) =@_;4729my($parent) =$parents[0];4730my$have_blame= gitweb_check_feature('blame');4731print"<div class=\"list_head\">\n";4732if($#{$difftree} >10) {4733print(($#{$difftree} +1) ." files changed:\n");4734}4735print"</div>\n";47364737print"<table class=\"".4738(@parents>1?"combined ":"") .4739"diff_tree\">\n";47404741# header only for combined diff in 'commitdiff' view4742my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4743if($has_header) {4744# table header4745print"<thead><tr>\n".4746"<th></th><th></th>\n";# filename, patchN link4747for(my$i=0;$i<@parents;$i++) {4748my$par=$parents[$i];4749print"<th>".4750$cgi->a({-href => href(action=>"commitdiff",4751 hash=>$hash, hash_parent=>$par),4752-title =>'commitdiff to parent number '.4753($i+1) .': '.substr($par,0,7)},4754$i+1) .4755" </th>\n";4756}4757print"</tr></thead>\n<tbody>\n";4758}47594760my$alternate=1;4761my$patchno=0;4762foreachmy$line(@{$difftree}) {4763my$diff= parsed_difftree_line($line);47644765if($alternate) {4766print"<tr class=\"dark\">\n";4767}else{4768print"<tr class=\"light\">\n";4769}4770$alternate^=1;47714772if(exists$diff->{'nparents'}) {# combined diff47734774 fill_from_file_info($diff,@parents)4775unlessexists$diff->{'from_file'};47764777if(!is_deleted($diff)) {4778# file exists in the result (child) commit4779print"<td>".4780$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4781 file_name=>$diff->{'to_file'},4782 hash_base=>$hash),4783-class=>"list"}, esc_path($diff->{'to_file'})) .4784"</td>\n";4785}else{4786print"<td>".4787 esc_path($diff->{'to_file'}) .4788"</td>\n";4789}47904791if($actioneq'commitdiff') {4792# link to patch4793$patchno++;4794print"<td class=\"link\">".4795$cgi->a({-href => href(-anchor=>"patch$patchno")},4796"patch") .4797" | ".4798"</td>\n";4799}48004801my$has_history=0;4802my$not_deleted=0;4803for(my$i=0;$i<$diff->{'nparents'};$i++) {4804my$hash_parent=$parents[$i];4805my$from_hash=$diff->{'from_id'}[$i];4806my$from_path=$diff->{'from_file'}[$i];4807my$status=$diff->{'status'}[$i];48084809$has_history||= ($statusne'A');4810$not_deleted||= ($statusne'D');48114812if($statuseq'A') {4813print"<td class=\"link\"align=\"right\"> | </td>\n";4814}elsif($statuseq'D') {4815print"<td class=\"link\">".4816$cgi->a({-href => href(action=>"blob",4817 hash_base=>$hash,4818 hash=>$from_hash,4819 file_name=>$from_path)},4820"blob". ($i+1)) .4821" | </td>\n";4822}else{4823if($diff->{'to_id'}eq$from_hash) {4824print"<td class=\"link nochange\">";4825}else{4826print"<td class=\"link\">";4827}4828print$cgi->a({-href => href(action=>"blobdiff",4829 hash=>$diff->{'to_id'},4830 hash_parent=>$from_hash,4831 hash_base=>$hash,4832 hash_parent_base=>$hash_parent,4833 file_name=>$diff->{'to_file'},4834 file_parent=>$from_path)},4835"diff". ($i+1)) .4836" | </td>\n";4837}4838}48394840print"<td class=\"link\">";4841if($not_deleted) {4842print$cgi->a({-href => href(action=>"blob",4843 hash=>$diff->{'to_id'},4844 file_name=>$diff->{'to_file'},4845 hash_base=>$hash)},4846"blob");4847print" | "if($has_history);4848}4849if($has_history) {4850print$cgi->a({-href => href(action=>"history",4851 file_name=>$diff->{'to_file'},4852 hash_base=>$hash)},4853"history");4854}4855print"</td>\n";48564857print"</tr>\n";4858next;# instead of 'else' clause, to avoid extra indent4859}4860# else ordinary diff48614862my($to_mode_oct,$to_mode_str,$to_file_type);4863my($from_mode_oct,$from_mode_str,$from_file_type);4864if($diff->{'to_mode'}ne('0' x 6)) {4865$to_mode_oct=oct$diff->{'to_mode'};4866if(S_ISREG($to_mode_oct)) {# only for regular file4867$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4868}4869$to_file_type= file_type($diff->{'to_mode'});4870}4871if($diff->{'from_mode'}ne('0' x 6)) {4872$from_mode_oct=oct$diff->{'from_mode'};4873if(S_ISREG($from_mode_oct)) {# only for regular file4874$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4875}4876$from_file_type= file_type($diff->{'from_mode'});4877}48784879if($diff->{'status'}eq"A") {# created4880my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4881$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4882$mode_chng.="]</span>";4883print"<td>";4884print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4885 hash_base=>$hash, file_name=>$diff->{'file'}),4886-class=>"list"}, esc_path($diff->{'file'}));4887print"</td>\n";4888print"<td>$mode_chng</td>\n";4889print"<td class=\"link\">";4890if($actioneq'commitdiff') {4891# link to patch4892$patchno++;4893print$cgi->a({-href => href(-anchor=>"patch$patchno")},4894"patch") .4895" | ";4896}4897print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4898 hash_base=>$hash, file_name=>$diff->{'file'})},4899"blob");4900print"</td>\n";49014902}elsif($diff->{'status'}eq"D") {# deleted4903my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4904print"<td>";4905print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4906 hash_base=>$parent, file_name=>$diff->{'file'}),4907-class=>"list"}, esc_path($diff->{'file'}));4908print"</td>\n";4909print"<td>$mode_chng</td>\n";4910print"<td class=\"link\">";4911if($actioneq'commitdiff') {4912# link to patch4913$patchno++;4914print$cgi->a({-href => href(-anchor=>"patch$patchno")},4915"patch") .4916" | ";4917}4918print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4919 hash_base=>$parent, file_name=>$diff->{'file'})},4920"blob") ." | ";4921if($have_blame) {4922print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4923 file_name=>$diff->{'file'})},4924"blame") ." | ";4925}4926print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4927 file_name=>$diff->{'file'})},4928"history");4929print"</td>\n";49304931}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4932my$mode_chnge="";4933if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4934$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4935if($from_file_typene$to_file_type) {4936$mode_chnge.=" from$from_file_typeto$to_file_type";4937}4938if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4939if($from_mode_str&&$to_mode_str) {4940$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4941}elsif($to_mode_str) {4942$mode_chnge.=" mode:$to_mode_str";4943}4944}4945$mode_chnge.="]</span>\n";4946}4947print"<td>";4948print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4949 hash_base=>$hash, file_name=>$diff->{'file'}),4950-class=>"list"}, esc_path($diff->{'file'}));4951print"</td>\n";4952print"<td>$mode_chnge</td>\n";4953print"<td class=\"link\">";4954if($actioneq'commitdiff') {4955# link to patch4956$patchno++;4957print$cgi->a({-href => href(-anchor=>"patch$patchno")},4958"patch") .4959" | ";4960}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4961# "commit" view and modified file (not onlu mode changed)4962print$cgi->a({-href => href(action=>"blobdiff",4963 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4964 hash_base=>$hash, hash_parent_base=>$parent,4965 file_name=>$diff->{'file'})},4966"diff") .4967" | ";4968}4969print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4970 hash_base=>$hash, file_name=>$diff->{'file'})},4971"blob") ." | ";4972if($have_blame) {4973print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4974 file_name=>$diff->{'file'})},4975"blame") ." | ";4976}4977print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4978 file_name=>$diff->{'file'})},4979"history");4980print"</td>\n";49814982}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4983my%status_name= ('R'=>'moved','C'=>'copied');4984my$nstatus=$status_name{$diff->{'status'}};4985my$mode_chng="";4986if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4987# mode also for directories, so we cannot use $to_mode_str4988$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4989}4990print"<td>".4991$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4992 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4993-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4994"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4995$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4996 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4997-class=>"list"}, esc_path($diff->{'from_file'})) .4998" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4999"<td class=\"link\">";5000if($actioneq'commitdiff') {5001# link to patch5002$patchno++;5003print$cgi->a({-href => href(-anchor=>"patch$patchno")},5004"patch") .5005" | ";5006}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {5007# "commit" view and modified file (not only pure rename or copy)5008print$cgi->a({-href => href(action=>"blobdiff",5009 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},5010 hash_base=>$hash, hash_parent_base=>$parent,5011 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},5012"diff") .5013" | ";5014}5015print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},5016 hash_base=>$parent, file_name=>$diff->{'to_file'})},5017"blob") ." | ";5018if($have_blame) {5019print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,5020 file_name=>$diff->{'to_file'})},5021"blame") ." | ";5022}5023print$cgi->a({-href => href(action=>"history", hash_base=>$hash,5024 file_name=>$diff->{'to_file'})},5025"history");5026print"</td>\n";50275028}# we should not encounter Unmerged (U) or Unknown (X) status5029print"</tr>\n";5030}5031print"</tbody>"if$has_header;5032print"</table>\n";5033}50345035# Print context lines and then rem/add lines in a side-by-side manner.5036sub print_sidebyside_diff_lines {5037my($ctx,$rem,$add) =@_;50385039# print context block before add/rem block5040if(@$ctx) {5041print join'',5042'<div class="chunk_block ctx">',5043'<div class="old">',5044@$ctx,5045'</div>',5046'<div class="new">',5047@$ctx,5048'</div>',5049'</div>';5050}50515052if(!@$add) {5053# pure removal5054print join'',5055'<div class="chunk_block rem">',5056'<div class="old">',5057@$rem,5058'</div>',5059'</div>';5060}elsif(!@$rem) {5061# pure addition5062print join'',5063'<div class="chunk_block add">',5064'<div class="new">',5065@$add,5066'</div>',5067'</div>';5068}else{5069print join'',5070'<div class="chunk_block chg">',5071'<div class="old">',5072@$rem,5073'</div>',5074'<div class="new">',5075@$add,5076'</div>',5077'</div>';5078}5079}50805081# Print context lines and then rem/add lines in inline manner.5082sub print_inline_diff_lines {5083my($ctx,$rem,$add) =@_;50845085print@$ctx,@$rem,@$add;5086}50875088# Format removed and added line, mark changed part and HTML-format them.5089# Implementation is based on contrib/diff-highlight5090sub format_rem_add_lines_pair {5091my($rem,$add,$num_parents) =@_;50925093# We need to untabify lines before split()'ing them;5094# otherwise offsets would be invalid.5095chomp$rem;5096chomp$add;5097$rem= untabify($rem);5098$add= untabify($add);50995100my@rem=split(//,$rem);5101my@add=split(//,$add);5102my($esc_rem,$esc_add);5103# Ignore leading +/- characters for each parent.5104my($prefix_len,$suffix_len) = ($num_parents,0);5105my($prefix_has_nonspace,$suffix_has_nonspace);51065107my$shorter= (@rem<@add) ?@rem:@add;5108while($prefix_len<$shorter) {5109last if($rem[$prefix_len]ne$add[$prefix_len]);51105111$prefix_has_nonspace=1if($rem[$prefix_len] !~/\s/);5112$prefix_len++;5113}51145115while($prefix_len+$suffix_len<$shorter) {5116last if($rem[-1-$suffix_len]ne$add[-1-$suffix_len]);51175118$suffix_has_nonspace=1if($rem[-1-$suffix_len] !~/\s/);5119$suffix_len++;5120}51215122# Mark lines that are different from each other, but have some common5123# part that isn't whitespace. If lines are completely different, don't5124# mark them because that would make output unreadable, especially if5125# diff consists of multiple lines.5126if($prefix_has_nonspace||$suffix_has_nonspace) {5127$esc_rem= esc_html_hl_regions($rem,'marked',5128[$prefix_len,@rem-$suffix_len], -nbsp=>1);5129$esc_add= esc_html_hl_regions($add,'marked',5130[$prefix_len,@add-$suffix_len], -nbsp=>1);5131}else{5132$esc_rem= esc_html($rem, -nbsp=>1);5133$esc_add= esc_html($add, -nbsp=>1);5134}51355136return format_diff_line(\$esc_rem,'rem'),5137 format_diff_line(\$esc_add,'add');5138}51395140# HTML-format diff context, removed and added lines.5141sub format_ctx_rem_add_lines {5142my($ctx,$rem,$add,$num_parents) =@_;5143my(@new_ctx,@new_rem,@new_add);5144my$can_highlight=0;5145my$is_combined= ($num_parents>1);51465147# Highlight if every removed line has a corresponding added line.5148if(@$add>0&&@$add==@$rem) {5149$can_highlight=1;51505151# Highlight lines in combined diff only if the chunk contains5152# diff between the same version, e.g.5153#5154# - a5155# - b5156# + c5157# + d5158#5159# Otherwise the highlightling would be confusing.5160if($is_combined) {5161for(my$i=0;$i<@$add;$i++) {5162my$prefix_rem=substr($rem->[$i],0,$num_parents);5163my$prefix_add=substr($add->[$i],0,$num_parents);51645165$prefix_rem=~s/-/+/g;51665167if($prefix_remne$prefix_add) {5168$can_highlight=0;5169last;5170}5171}5172}5173}51745175if($can_highlight) {5176for(my$i=0;$i<@$add;$i++) {5177my($line_rem,$line_add) = format_rem_add_lines_pair(5178$rem->[$i],$add->[$i],$num_parents);5179push@new_rem,$line_rem;5180push@new_add,$line_add;5181}5182}else{5183@new_rem=map{ format_diff_line($_,'rem') }@$rem;5184@new_add=map{ format_diff_line($_,'add') }@$add;5185}51865187@new_ctx=map{ format_diff_line($_,'ctx') }@$ctx;51885189return(\@new_ctx, \@new_rem, \@new_add);5190}51915192# Print context lines and then rem/add lines.5193sub print_diff_lines {5194my($ctx,$rem,$add,$diff_style,$num_parents) =@_;5195my$is_combined=$num_parents>1;51965197($ctx,$rem,$add) = format_ctx_rem_add_lines($ctx,$rem,$add,5198$num_parents);51995200if($diff_styleeq'sidebyside'&& !$is_combined) {5201 print_sidebyside_diff_lines($ctx,$rem,$add);5202}else{5203# default 'inline' style and unknown styles5204 print_inline_diff_lines($ctx,$rem,$add);5205}5206}52075208sub print_diff_chunk {5209my($diff_style,$num_parents,$from,$to,@chunk) =@_;5210my(@ctx,@rem,@add);52115212# The class of the previous line.5213my$prev_class='';52145215return unless@chunk;52165217# incomplete last line might be among removed or added lines,5218# or both, or among context lines: find which5219for(my$i=1;$i<@chunk;$i++) {5220if($chunk[$i][0]eq'incomplete') {5221$chunk[$i][0] =$chunk[$i-1][0];5222}5223}52245225# guardian5226push@chunk, ["",""];52275228foreachmy$line_info(@chunk) {5229my($class,$line) =@$line_info;52305231# print chunk headers5232if($class&&$classeq'chunk_header') {5233print format_diff_line($line,$class,$from,$to);5234next;5235}52365237## print from accumulator when have some add/rem lines or end5238# of chunk (flush context lines), or when have add and rem5239# lines and new block is reached (otherwise add/rem lines could5240# be reordered)5241if(!$class|| ((@rem||@add) &&$classeq'ctx') ||5242(@rem&&@add&&$classne$prev_class)) {5243 print_diff_lines(\@ctx, \@rem, \@add,5244$diff_style,$num_parents);5245@ctx=@rem=@add= ();5246}52475248## adding lines to accumulator5249# guardian value5250last unless$line;5251# rem, add or change5252if($classeq'rem') {5253push@rem,$line;5254}elsif($classeq'add') {5255push@add,$line;5256}5257# context line5258if($classeq'ctx') {5259push@ctx,$line;5260}52615262$prev_class=$class;5263}5264}52655266sub git_patchset_body {5267my($fd,$diff_style,$difftree,$hash,@hash_parents) =@_;5268my($hash_parent) =$hash_parents[0];52695270my$is_combined= (@hash_parents>1);5271my$patch_idx=0;5272my$patch_number=0;5273my$patch_line;5274my$diffinfo;5275my$to_name;5276my(%from,%to);5277my@chunk;# for side-by-side diff52785279print"<div class=\"patchset\">\n";52805281# skip to first patch5282while($patch_line= <$fd>) {5283chomp$patch_line;52845285last if($patch_line=~m/^diff /);5286}52875288 PATCH:5289while($patch_line) {52905291# parse "git diff" header line5292if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {5293# $1 is from_name, which we do not use5294$to_name= unquote($2);5295$to_name=~s!^b/!!;5296}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {5297# $1 is 'cc' or 'combined', which we do not use5298$to_name= unquote($2);5299}else{5300$to_name=undef;5301}53025303# check if current patch belong to current raw line5304# and parse raw git-diff line if needed5305if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {5306# this is continuation of a split patch5307print"<div class=\"patch cont\">\n";5308}else{5309# advance raw git-diff output if needed5310$patch_idx++ifdefined$diffinfo;53115312# read and prepare patch information5313$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);53145315# compact combined diff output can have some patches skipped5316# find which patch (using pathname of result) we are at now;5317if($is_combined) {5318while($to_namene$diffinfo->{'to_file'}) {5319print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".5320 format_diff_cc_simplified($diffinfo,@hash_parents) .5321"</div>\n";# class="patch"53225323$patch_idx++;5324$patch_number++;53255326last if$patch_idx>$#$difftree;5327$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);5328}5329}53305331# modifies %from, %to hashes5332 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);53335334# this is first patch for raw difftree line with $patch_idx index5335# we index @$difftree array from 0, but number patches from 15336print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";5337}53385339# git diff header5340#assert($patch_line =~ m/^diff /) if DEBUG;5341#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed5342$patch_number++;5343# print "git diff" header5344print format_git_diff_header_line($patch_line,$diffinfo,5345 \%from, \%to);53465347# print extended diff header5348print"<div class=\"diff extended_header\">\n";5349 EXTENDED_HEADER:5350while($patch_line= <$fd>) {5351chomp$patch_line;53525353last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);53545355print format_extended_diff_header_line($patch_line,$diffinfo,5356 \%from, \%to);5357}5358print"</div>\n";# class="diff extended_header"53595360# from-file/to-file diff header5361if(!$patch_line) {5362print"</div>\n";# class="patch"5363last PATCH;5364}5365next PATCH if($patch_line=~m/^diff /);5366#assert($patch_line =~ m/^---/) if DEBUG;53675368my$last_patch_line=$patch_line;5369$patch_line= <$fd>;5370chomp$patch_line;5371#assert($patch_line =~ m/^\+\+\+/) if DEBUG;53725373print format_diff_from_to_header($last_patch_line,$patch_line,5374$diffinfo, \%from, \%to,5375@hash_parents);53765377# the patch itself5378 LINE:5379while($patch_line= <$fd>) {5380chomp$patch_line;53815382next PATCH if($patch_line=~m/^diff /);53835384my$class= diff_line_class($patch_line, \%from, \%to);53855386if($classeq'chunk_header') {5387 print_diff_chunk($diff_style,scalar@hash_parents, \%from, \%to,@chunk);5388@chunk= ();5389}53905391push@chunk, [$class,$patch_line];5392}53935394}continue{5395if(@chunk) {5396 print_diff_chunk($diff_style,scalar@hash_parents, \%from, \%to,@chunk);5397@chunk= ();5398}5399print"</div>\n";# class="patch"5400}54015402# for compact combined (--cc) format, with chunk and patch simplification5403# the patchset might be empty, but there might be unprocessed raw lines5404for(++$patch_idxif$patch_number>0;5405$patch_idx<@$difftree;5406++$patch_idx) {5407# read and prepare patch information5408$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);54095410# generate anchor for "patch" links in difftree / whatchanged part5411print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".5412 format_diff_cc_simplified($diffinfo,@hash_parents) .5413"</div>\n";# class="patch"54145415$patch_number++;5416}54175418if($patch_number==0) {5419if(@hash_parents>1) {5420print"<div class=\"diff nodifferences\">Trivial merge</div>\n";5421}else{5422print"<div class=\"diff nodifferences\">No differences found</div>\n";5423}5424}54255426print"</div>\n";# class="patchset"5427}54285429# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54305431sub git_project_search_form {5432my($searchtext,$search_use_regexp) =@_;54335434my$limit='';5435if($project_filter) {5436$limit=" in '$project_filter/'";5437}54385439print"<div class=\"projsearch\">\n";5440print$cgi->startform(-method=>'get', -action =>$my_uri) .5441$cgi->hidden(-name =>'a', -value =>'project_list') ."\n";5442print$cgi->hidden(-name =>'pf', -value =>$project_filter)."\n"5443if(defined$project_filter);5444print$cgi->textfield(-name =>'s', -value =>$searchtext,5445-title =>"Search project by name and description$limit",5446-size =>60) ."\n".5447"<span title=\"Extended regular expression\">".5448$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',5449-checked =>$search_use_regexp) .5450"</span>\n".5451$cgi->submit(-name =>'btnS', -value =>'Search') .5452$cgi->end_form() ."\n".5453$cgi->a({-href => href(project =>undef, searchtext =>undef,5454 project_filter =>$project_filter)},5455 esc_html("List all projects$limit")) ."<br />\n";5456print"</div>\n";5457}54585459# entry for given @keys needs filling if at least one of keys in list5460# is not present in %$project_info5461sub project_info_needs_filling {5462my($project_info,@keys) =@_;54635464# return List::MoreUtils::any { !exists $project_info->{$_} } @keys;5465foreachmy$key(@keys) {5466if(!exists$project_info->{$key}) {5467return1;5468}5469}5470return;5471}54725473# fills project list info (age, description, owner, category, forks, etc.)5474# for each project in the list, removing invalid projects from5475# returned list, or fill only specified info.5476#5477# Invalid projects are removed from the returned list if and only if you5478# ask 'age' or 'age_string' to be filled, because they are the only fields5479# that run unconditionally git command that requires repository, and5480# therefore do always check if project repository is invalid.5481#5482# USAGE:5483# * fill_project_list_info(\@project_list, 'descr_long', 'ctags')5484# ensures that 'descr_long' and 'ctags' fields are filled5485# * @project_list = fill_project_list_info(\@project_list)5486# ensures that all fields are filled (and invalid projects removed)5487#5488# NOTE: modifies $projlist, but does not remove entries from it5489sub fill_project_list_info {5490my($projlist,@wanted_keys) =@_;5491my@projects;5492my$filter_set=sub{return@_; };5493if(@wanted_keys) {5494my%wanted_keys=map{$_=>1}@wanted_keys;5495$filter_set=sub{returngrep{$wanted_keys{$_} }@_; };5496}54975498my$show_ctags= gitweb_check_feature('ctags');5499 PROJECT:5500foreachmy$pr(@$projlist) {5501if(project_info_needs_filling($pr,$filter_set->('age','age_string'))) {5502my(@activity) = git_get_last_activity($pr->{'path'});5503unless(@activity) {5504next PROJECT;5505}5506($pr->{'age'},$pr->{'age_string'}) =@activity;5507}5508if(project_info_needs_filling($pr,$filter_set->('descr','descr_long'))) {5509my$descr= git_get_project_description($pr->{'path'}) ||"";5510$descr= to_utf8($descr);5511$pr->{'descr_long'} =$descr;5512$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);5513}5514if(project_info_needs_filling($pr,$filter_set->('owner'))) {5515$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";5516}5517if($show_ctags&&5518 project_info_needs_filling($pr,$filter_set->('ctags'))) {5519$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});5520}5521if($projects_list_group_categories&&5522 project_info_needs_filling($pr,$filter_set->('category'))) {5523my$cat= git_get_project_category($pr->{'path'}) ||5524$project_list_default_category;5525$pr->{'category'} = to_utf8($cat);5526}55275528push@projects,$pr;5529}55305531return@projects;5532}55335534sub sort_projects_list {5535my($projlist,$order) =@_;55365537sub order_str {5538my$key=shift;5539return sub{$a->{$key}cmp$b->{$key} };5540}55415542sub order_num_then_undef {5543my$key=shift;5544return sub{5545defined$a->{$key} ?5546(defined$b->{$key} ?$a->{$key} <=>$b->{$key} : -1) :5547(defined$b->{$key} ?1:0)5548};5549}55505551my%orderings= (5552 project => order_str('path'),5553 descr => order_str('descr_long'),5554 owner => order_str('owner'),5555 age => order_num_then_undef('age'),5556);55575558my$ordering=$orderings{$order};5559returndefined$ordering?sort$ordering @$projlist:@$projlist;5560}55615562# returns a hash of categories, containing the list of project5563# belonging to each category5564sub build_projlist_by_category {5565my($projlist,$from,$to) =@_;5566my%categories;55675568$from=0unlessdefined$from;5569$to=$#$projlistif(!defined$to||$#$projlist<$to);55705571for(my$i=$from;$i<=$to;$i++) {5572my$pr=$projlist->[$i];5573push@{$categories{$pr->{'category'} }},$pr;5574}55755576returnwantarray?%categories: \%categories;5577}55785579# print 'sort by' <th> element, generating 'sort by $name' replay link5580# if that order is not selected5581sub print_sort_th {5582print format_sort_th(@_);5583}55845585sub format_sort_th {5586my($name,$order,$header) =@_;5587my$sort_th="";5588$header||=ucfirst($name);55895590if($ordereq$name) {5591$sort_th.="<th>$header</th>\n";5592}else{5593$sort_th.="<th>".5594$cgi->a({-href => href(-replay=>1, order=>$name),5595-class=>"header"},$header) .5596"</th>\n";5597}55985599return$sort_th;5600}56015602sub git_project_list_rows {5603my($projlist,$from,$to,$check_forks) =@_;56045605$from=0unlessdefined$from;5606$to=$#$projlistif(!defined$to||$#$projlist<$to);56075608my$alternate=1;5609for(my$i=$from;$i<=$to;$i++) {5610my$pr=$projlist->[$i];56115612if($alternate) {5613print"<tr class=\"dark\">\n";5614}else{5615print"<tr class=\"light\">\n";5616}5617$alternate^=1;56185619if($check_forks) {5620print"<td>";5621if($pr->{'forks'}) {5622my$nforks=scalar@{$pr->{'forks'}};5623if($nforks>0) {5624print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),5625-title =>"$nforksforks"},"+");5626}else{5627print$cgi->span({-title =>"$nforksforks"},"+");5628}5629}5630print"</td>\n";5631}5632print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5633-class=>"list"},5634 esc_html_match_hl($pr->{'path'},$search_regexp)) .5635"</td>\n".5636"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5637-class=>"list",5638-title =>$pr->{'descr_long'}},5639$search_regexp5640? esc_html_match_hl_chopped($pr->{'descr_long'},5641$pr->{'descr'},$search_regexp)5642: esc_html($pr->{'descr'})) .5643"</td>\n";5644unless($omit_owner) {5645print"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";5646}5647unless($omit_age_column) {5648print"<td class=\"". age_class($pr->{'age'}) ."\">".5649(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n";5650}5651print"<td class=\"link\">".5652$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".5653$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".5654$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".5655$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .5656($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .5657"</td>\n".5658"</tr>\n";5659}5660}56615662sub git_project_list_body {5663# actually uses global variable $project5664my($projlist,$order,$from,$to,$extra,$no_header) =@_;5665my@projects=@$projlist;56665667my$check_forks= gitweb_check_feature('forks');5668my$show_ctags= gitweb_check_feature('ctags');5669my$tagfilter=$show_ctags?$input_params{'ctag'} :undef;5670$check_forks=undef5671if($tagfilter||$search_regexp);56725673# filtering out forks before filling info allows to do less work5674@projects= filter_forks_from_projects_list(\@projects)5675if($check_forks);5676# search_projects_list pre-fills required info5677@projects= search_projects_list(\@projects,5678'search_regexp'=>$search_regexp,5679'tagfilter'=>$tagfilter)5680if($tagfilter||$search_regexp);5681# fill the rest5682my@all_fields= ('descr','descr_long','ctags','category');5683push@all_fields, ('age','age_string')unless($omit_age_column);5684push@all_fields,'owner'unless($omit_owner);5685@projects= fill_project_list_info(\@projects,@all_fields);56865687$order||=$default_projects_order;5688$from=0unlessdefined$from;5689$to=$#projectsif(!defined$to||$#projects<$to);56905691# short circuit5692if($from>$to) {5693print"<center>\n".5694"<b>No such projects found</b><br />\n".5695"Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".5696"</center>\n<br />\n";5697return;5698}56995700@projects= sort_projects_list(\@projects,$order);57015702if($show_ctags) {5703my$ctags= git_gather_all_ctags(\@projects);5704my$cloud= git_populate_project_tagcloud($ctags);5705print git_show_project_tagcloud($cloud,64);5706}57075708print"<table class=\"project_list\">\n";5709unless($no_header) {5710print"<tr>\n";5711if($check_forks) {5712print"<th></th>\n";5713}5714 print_sort_th('project',$order,'Project');5715 print_sort_th('descr',$order,'Description');5716 print_sort_th('owner',$order,'Owner')unless$omit_owner;5717 print_sort_th('age',$order,'Last Change')unless$omit_age_column;5718print"<th></th>\n".# for links5719"</tr>\n";5720}57215722if($projects_list_group_categories) {5723# only display categories with projects in the $from-$to window5724@projects=sort{$a->{'category'}cmp$b->{'category'}}@projects[$from..$to];5725my%categories= build_projlist_by_category(\@projects,$from,$to);5726foreachmy$cat(sort keys%categories) {5727unless($cateq"") {5728print"<tr>\n";5729if($check_forks) {5730print"<td></td>\n";5731}5732print"<td class=\"category\"colspan=\"5\">".esc_html($cat)."</td>\n";5733print"</tr>\n";5734}57355736 git_project_list_rows($categories{$cat},undef,undef,$check_forks);5737}5738}else{5739 git_project_list_rows(\@projects,$from,$to,$check_forks);5740}57415742if(defined$extra) {5743print"<tr>\n";5744if($check_forks) {5745print"<td></td>\n";5746}5747print"<td colspan=\"5\">$extra</td>\n".5748"</tr>\n";5749}5750print"</table>\n";5751}57525753sub git_log_body {5754# uses global variable $project5755my($commitlist,$from,$to,$refs,$extra) =@_;57565757$from=0unlessdefined$from;5758$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);57595760for(my$i=0;$i<=$to;$i++) {5761my%co= %{$commitlist->[$i]};5762next if!%co;5763my$commit=$co{'id'};5764my$ref= format_ref_marker($refs,$commit);5765 git_print_header_div('commit',5766"<span class=\"age\">$co{'age_string'}</span>".5767 esc_html($co{'title'}) .$ref,5768$commit);5769print"<div class=\"title_text\">\n".5770"<div class=\"log_link\">\n".5771$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5772" | ".5773$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5774" | ".5775$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5776"<br/>\n".5777"</div>\n";5778 git_print_authorship(\%co, -tag =>'span');5779print"<br/>\n</div>\n";57805781print"<div class=\"log_body\">\n";5782 git_print_log($co{'comment'}, -final_empty_line=>1);5783print"</div>\n";5784}5785if($extra) {5786print"<div class=\"page_nav\">\n";5787print"$extra\n";5788print"</div>\n";5789}5790}57915792sub git_shortlog_body {5793# uses global variable $project5794my($commitlist,$from,$to,$refs,$extra) =@_;57955796$from=0unlessdefined$from;5797$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);57985799print"<table class=\"shortlog\">\n";5800my$alternate=1;5801for(my$i=$from;$i<=$to;$i++) {5802my%co= %{$commitlist->[$i]};5803my$commit=$co{'id'};5804my$ref= format_ref_marker($refs,$commit);5805if($alternate) {5806print"<tr class=\"dark\">\n";5807}else{5808print"<tr class=\"light\">\n";5809}5810$alternate^=1;5811# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .5812print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5813 format_author_html('td', \%co,10) ."<td>";5814print format_subject_html($co{'title'},$co{'title_short'},5815 href(action=>"commit", hash=>$commit),$ref);5816print"</td>\n".5817"<td class=\"link\">".5818$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".5819$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".5820$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");5821my$snapshot_links= format_snapshot_links($commit);5822if(defined$snapshot_links) {5823print" | ".$snapshot_links;5824}5825print"</td>\n".5826"</tr>\n";5827}5828if(defined$extra) {5829print"<tr>\n".5830"<td colspan=\"4\">$extra</td>\n".5831"</tr>\n";5832}5833print"</table>\n";5834}58355836sub git_history_body {5837# Warning: assumes constant type (blob or tree) during history5838my($commitlist,$from,$to,$refs,$extra,5839$file_name,$file_hash,$ftype) =@_;58405841$from=0unlessdefined$from;5842$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});58435844print"<table class=\"history\">\n";5845my$alternate=1;5846for(my$i=$from;$i<=$to;$i++) {5847my%co= %{$commitlist->[$i]};5848if(!%co) {5849next;5850}5851my$commit=$co{'id'};58525853my$ref= format_ref_marker($refs,$commit);58545855if($alternate) {5856print"<tr class=\"dark\">\n";5857}else{5858print"<tr class=\"light\">\n";5859}5860$alternate^=1;5861print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5862# shortlog: format_author_html('td', \%co, 10)5863 format_author_html('td', \%co,15,3) ."<td>";5864# originally git_history used chop_str($co{'title'}, 50)5865print format_subject_html($co{'title'},$co{'title_short'},5866 href(action=>"commit", hash=>$commit),$ref);5867print"</td>\n".5868"<td class=\"link\">".5869$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".5870$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");58715872if($ftypeeq'blob') {5873my$blob_current=$file_hash;5874my$blob_parent= git_get_hash_by_path($commit,$file_name);5875if(defined$blob_current&&defined$blob_parent&&5876$blob_currentne$blob_parent) {5877print" | ".5878$cgi->a({-href => href(action=>"blobdiff",5879 hash=>$blob_current, hash_parent=>$blob_parent,5880 hash_base=>$hash_base, hash_parent_base=>$commit,5881 file_name=>$file_name)},5882"diff to current");5883}5884}5885print"</td>\n".5886"</tr>\n";5887}5888if(defined$extra) {5889print"<tr>\n".5890"<td colspan=\"4\">$extra</td>\n".5891"</tr>\n";5892}5893print"</table>\n";5894}58955896sub git_tags_body {5897# uses global variable $project5898my($taglist,$from,$to,$extra) =@_;5899$from=0unlessdefined$from;5900$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);59015902print"<table class=\"tags\">\n";5903my$alternate=1;5904for(my$i=$from;$i<=$to;$i++) {5905my$entry=$taglist->[$i];5906my%tag=%$entry;5907my$comment=$tag{'subject'};5908my$comment_short;5909if(defined$comment) {5910$comment_short= chop_str($comment,30,5);5911}5912if($alternate) {5913print"<tr class=\"dark\">\n";5914}else{5915print"<tr class=\"light\">\n";5916}5917$alternate^=1;5918if(defined$tag{'age'}) {5919print"<td><i>$tag{'age'}</i></td>\n";5920}else{5921print"<td></td>\n";5922}5923print"<td>".5924$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),5925-class=>"list name"}, esc_html($tag{'name'})) .5926"</td>\n".5927"<td>";5928if(defined$comment) {5929print format_subject_html($comment,$comment_short,5930 href(action=>"tag", hash=>$tag{'id'}));5931}5932print"</td>\n".5933"<td class=\"selflink\">";5934if($tag{'type'}eq"tag") {5935print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");5936}else{5937print" ";5938}5939print"</td>\n".5940"<td class=\"link\">"." | ".5941$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});5942if($tag{'reftype'}eq"commit") {5943print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .5944" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");5945}elsif($tag{'reftype'}eq"blob") {5946print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");5947}5948print"</td>\n".5949"</tr>";5950}5951if(defined$extra) {5952print"<tr>\n".5953"<td colspan=\"5\">$extra</td>\n".5954"</tr>\n";5955}5956print"</table>\n";5957}59585959sub git_heads_body {5960# uses global variable $project5961my($headlist,$head_at,$from,$to,$extra) =@_;5962$from=0unlessdefined$from;5963$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);59645965print"<table class=\"heads\">\n";5966my$alternate=1;5967for(my$i=$from;$i<=$to;$i++) {5968my$entry=$headlist->[$i];5969my%ref=%$entry;5970my$curr=defined$head_at&&$ref{'id'}eq$head_at;5971if($alternate) {5972print"<tr class=\"dark\">\n";5973}else{5974print"<tr class=\"light\">\n";5975}5976$alternate^=1;5977print"<td><i>$ref{'age'}</i></td>\n".5978($curr?"<td class=\"current_head\">":"<td>") .5979$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),5980-class=>"list name"},esc_html($ref{'name'})) .5981"</td>\n".5982"<td class=\"link\">".5983$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".5984$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".5985$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})},"tree") .5986"</td>\n".5987"</tr>";5988}5989if(defined$extra) {5990print"<tr>\n".5991"<td colspan=\"3\">$extra</td>\n".5992"</tr>\n";5993}5994print"</table>\n";5995}59965997# Display a single remote block5998sub git_remote_block {5999my($remote,$rdata,$limit,$head) =@_;60006001my$heads=$rdata->{'heads'};6002my$fetch=$rdata->{'fetch'};6003my$push=$rdata->{'push'};60046005my$urls_table="<table class=\"projects_list\">\n";60066007if(defined$fetch) {6008if($fetcheq$push) {6009$urls_table.= format_repo_url("URL",$fetch);6010}else{6011$urls_table.= format_repo_url("Fetch URL",$fetch);6012$urls_table.= format_repo_url("Push URL",$push)ifdefined$push;6013}6014}elsif(defined$push) {6015$urls_table.= format_repo_url("Push URL",$push);6016}else{6017$urls_table.= format_repo_url("","No remote URL");6018}60196020$urls_table.="</table>\n";60216022my$dots;6023if(defined$limit&&$limit<@$heads) {6024$dots=$cgi->a({-href => href(action=>"remotes", hash=>$remote)},"...");6025}60266027print$urls_table;6028 git_heads_body($heads,$head,0,$limit,$dots);6029}60306031# Display a list of remote names with the respective fetch and push URLs6032sub git_remotes_list {6033my($remotedata,$limit) =@_;6034print"<table class=\"heads\">\n";6035my$alternate=1;6036my@remotes=sort keys%$remotedata;60376038my$limited=$limit&&$limit<@remotes;60396040$#remotes=$limit-1if$limited;60416042while(my$remote=shift@remotes) {6043my$rdata=$remotedata->{$remote};6044my$fetch=$rdata->{'fetch'};6045my$push=$rdata->{'push'};6046if($alternate) {6047print"<tr class=\"dark\">\n";6048}else{6049print"<tr class=\"light\">\n";6050}6051$alternate^=1;6052print"<td>".6053$cgi->a({-href=> href(action=>'remotes', hash=>$remote),6054-class=>"list name"},esc_html($remote)) .6055"</td>";6056print"<td class=\"link\">".6057(defined$fetch?$cgi->a({-href=>$fetch},"fetch") :"fetch") .6058" | ".6059(defined$push?$cgi->a({-href=>$push},"push") :"push") .6060"</td>";60616062print"</tr>\n";6063}60646065if($limited) {6066print"<tr>\n".6067"<td colspan=\"3\">".6068$cgi->a({-href => href(action=>"remotes")},"...") .6069"</td>\n"."</tr>\n";6070}60716072print"</table>";6073}60746075# Display remote heads grouped by remote, unless there are too many6076# remotes, in which case we only display the remote names6077sub git_remotes_body {6078my($remotedata,$limit,$head) =@_;6079if($limitand$limit<keys%$remotedata) {6080 git_remotes_list($remotedata,$limit);6081}else{6082 fill_remote_heads($remotedata);6083while(my($remote,$rdata) =each%$remotedata) {6084 git_print_section({-class=>"remote", -id=>$remote},6085["remotes",$remote,$remote],sub{6086 git_remote_block($remote,$rdata,$limit,$head);6087});6088}6089}6090}60916092sub git_search_message {6093my%co=@_;60946095my$greptype;6096if($searchtypeeq'commit') {6097$greptype="--grep=";6098}elsif($searchtypeeq'author') {6099$greptype="--author=";6100}elsif($searchtypeeq'committer') {6101$greptype="--committer=";6102}6103$greptype.=$searchtext;6104my@commitlist= parse_commits($hash,101, (100*$page),undef,6105$greptype,'--regexp-ignore-case',6106$search_use_regexp?'--extended-regexp':'--fixed-strings');61076108my$paging_nav='';6109if($page>0) {6110$paging_nav.=6111$cgi->a({-href => href(-replay=>1, page=>undef)},6112"first") .6113" ⋅ ".6114$cgi->a({-href => href(-replay=>1, page=>$page-1),6115-accesskey =>"p", -title =>"Alt-p"},"prev");6116}else{6117$paging_nav.="first ⋅ prev";6118}6119my$next_link='';6120if($#commitlist>=100) {6121$next_link=6122$cgi->a({-href => href(-replay=>1, page=>$page+1),6123-accesskey =>"n", -title =>"Alt-n"},"next");6124$paging_nav.=" ⋅$next_link";6125}else{6126$paging_nav.=" ⋅ next";6127}61286129 git_header_html();61306131 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6132 git_print_header_div('commit', esc_html($co{'title'}),$hash);6133if($page==0&& !@commitlist) {6134print"<p>No match.</p>\n";6135}else{6136 git_search_grep_body(\@commitlist,0,99,$next_link);6137}61386139 git_footer_html();6140}61416142sub git_search_changes {6143my%co=@_;61446145local$/="\n";6146open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6147'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6148($search_use_regexp?'--pickaxe-regex': ())6149or die_error(500,"Open git-log failed");61506151 git_header_html();61526153 git_print_page_nav('','',$hash,$co{'tree'},$hash);6154 git_print_header_div('commit', esc_html($co{'title'}),$hash);61556156print"<table class=\"pickaxe search\">\n";6157my$alternate=1;6158undef%co;6159my@files;6160while(my$line= <$fd>) {6161chomp$line;6162next unless$line;61636164my%set= parse_difftree_raw_line($line);6165if(defined$set{'commit'}) {6166# finish previous commit6167if(%co) {6168print"</td>\n".6169"<td class=\"link\">".6170$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},6171"commit") .6172" | ".6173$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},6174 hash_base=>$co{'id'})},6175"tree") .6176"</td>\n".6177"</tr>\n";6178}61796180if($alternate) {6181print"<tr class=\"dark\">\n";6182}else{6183print"<tr class=\"light\">\n";6184}6185$alternate^=1;6186%co= parse_commit($set{'commit'});6187my$author= chop_and_escape_str($co{'author_name'},15,5);6188print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6189"<td><i>$author</i></td>\n".6190"<td>".6191$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6192-class=>"list subject"},6193 chop_and_escape_str($co{'title'},50) ."<br/>");6194}elsif(defined$set{'to_id'}) {6195next if($set{'to_id'} =~m/^0{40}$/);61966197print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6198 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6199-class=>"list"},6200"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6201"<br/>\n";6202}6203}6204close$fd;62056206# finish last commit (warning: repetition!)6207if(%co) {6208print"</td>\n".6209"<td class=\"link\">".6210$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},6211"commit") .6212" | ".6213$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},6214 hash_base=>$co{'id'})},6215"tree") .6216"</td>\n".6217"</tr>\n";6218}62196220print"</table>\n";62216222 git_footer_html();6223}62246225sub git_search_files {6226my%co=@_;62276228local$/="\n";6229open my$fd,"-|", git_cmd(),'grep','-n','-z',6230$search_use_regexp? ('-E','-i') :'-F',6231$searchtext,$co{'tree'}6232or die_error(500,"Open git-grep failed");62336234 git_header_html();62356236 git_print_page_nav('','',$hash,$co{'tree'},$hash);6237 git_print_header_div('commit', esc_html($co{'title'}),$hash);62386239print"<table class=\"grep_search\">\n";6240my$alternate=1;6241my$matches=0;6242my$lastfile='';6243my$file_href;6244while(my$line= <$fd>) {6245chomp$line;6246my($file,$lno,$ltext,$binary);6247last if($matches++>1000);6248if($line=~/^Binary file (.+) matches$/) {6249$file=$1;6250$binary=1;6251}else{6252($file,$lno,$ltext) =split(/\0/,$line,3);6253$file=~s/^$co{'tree'}://;6254}6255if($filene$lastfile) {6256$lastfileand print"</td></tr>\n";6257if($alternate++) {6258print"<tr class=\"dark\">\n";6259}else{6260print"<tr class=\"light\">\n";6261}6262$file_href= href(action=>"blob", hash_base=>$co{'id'},6263 file_name=>$file);6264print"<td class=\"list\">".6265$cgi->a({-href =>$file_href, -class=>"list"}, esc_path($file));6266print"</td><td>\n";6267$lastfile=$file;6268}6269if($binary) {6270print"<div class=\"binary\">Binary file</div>\n";6271}else{6272$ltext= untabify($ltext);6273if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6274$ltext= esc_html($1, -nbsp=>1);6275$ltext.='<span class="match">';6276$ltext.= esc_html($2, -nbsp=>1);6277$ltext.='</span>';6278$ltext.= esc_html($3, -nbsp=>1);6279}else{6280$ltext= esc_html($ltext, -nbsp=>1);6281}6282print"<div class=\"pre\">".6283$cgi->a({-href =>$file_href.'#l'.$lno,6284-class=>"linenr"},sprintf('%4i',$lno)) .6285' '.$ltext."</div>\n";6286}6287}6288if($lastfile) {6289print"</td></tr>\n";6290if($matches>1000) {6291print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6292}6293}else{6294print"<div class=\"diff nodifferences\">No matches found</div>\n";6295}6296close$fd;62976298print"</table>\n";62996300 git_footer_html();6301}63026303sub git_search_grep_body {6304my($commitlist,$from,$to,$extra) =@_;6305$from=0unlessdefined$from;6306$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);63076308print"<table class=\"commit_search\">\n";6309my$alternate=1;6310for(my$i=$from;$i<=$to;$i++) {6311my%co= %{$commitlist->[$i]};6312if(!%co) {6313next;6314}6315my$commit=$co{'id'};6316if($alternate) {6317print"<tr class=\"dark\">\n";6318}else{6319print"<tr class=\"light\">\n";6320}6321$alternate^=1;6322print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6323 format_author_html('td', \%co,15,5) .6324"<td>".6325$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6326-class=>"list subject"},6327 chop_and_escape_str($co{'title'},50) ."<br/>");6328my$comment=$co{'comment'};6329foreachmy$line(@$comment) {6330if($line=~m/^(.*?)($search_regexp)(.*)$/i) {6331my($lead,$match,$trail) = ($1,$2,$3);6332$match= chop_str($match,70,5,'center');6333my$contextlen=int((80-length($match))/2);6334$contextlen=30if($contextlen>30);6335$lead= chop_str($lead,$contextlen,10,'left');6336$trail= chop_str($trail,$contextlen,10,'right');63376338$lead= esc_html($lead);6339$match= esc_html($match);6340$trail= esc_html($trail);63416342print"$lead<span class=\"match\">$match</span>$trail<br />";6343}6344}6345print"</td>\n".6346"<td class=\"link\">".6347$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6348" | ".6349$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .6350" | ".6351$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6352print"</td>\n".6353"</tr>\n";6354}6355if(defined$extra) {6356print"<tr>\n".6357"<td colspan=\"3\">$extra</td>\n".6358"</tr>\n";6359}6360print"</table>\n";6361}63626363## ======================================================================6364## ======================================================================6365## actions63666367sub git_project_list {6368my$order=$input_params{'order'};6369if(defined$order&&$order!~m/none|project|descr|owner|age/) {6370 die_error(400,"Unknown order parameter");6371}63726373my@list= git_get_projects_list($project_filter,$strict_export);6374if(!@list) {6375 die_error(404,"No projects found");6376}63776378 git_header_html();6379if(defined$home_text&& -f $home_text) {6380print"<div class=\"index_include\">\n";6381 insert_file($home_text);6382print"</div>\n";6383}63846385 git_project_search_form($searchtext,$search_use_regexp);6386 git_project_list_body(\@list,$order);6387 git_footer_html();6388}63896390sub git_forks {6391my$order=$input_params{'order'};6392if(defined$order&&$order!~m/none|project|descr|owner|age/) {6393 die_error(400,"Unknown order parameter");6394}63956396my$filter=$project;6397$filter=~s/\.git$//;6398my@list= git_get_projects_list($filter);6399if(!@list) {6400 die_error(404,"No forks found");6401}64026403 git_header_html();6404 git_print_page_nav('','');6405 git_print_header_div('summary',"$projectforks");6406 git_project_list_body(\@list,$order);6407 git_footer_html();6408}64096410sub git_project_index {6411my@projects= git_get_projects_list($project_filter,$strict_export);6412if(!@projects) {6413 die_error(404,"No projects found");6414}64156416print$cgi->header(6417-type =>'text/plain',6418-charset =>'utf-8',6419-content_disposition =>'inline; filename="index.aux"');64206421foreachmy$pr(@projects) {6422if(!exists$pr->{'owner'}) {6423$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");6424}64256426my($path,$owner) = ($pr->{'path'},$pr->{'owner'});6427# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '6428$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;6429$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;6430$path=~s/ /\+/g;6431$owner=~s/ /\+/g;64326433print"$path$owner\n";6434}6435}64366437sub git_summary {6438my$descr= git_get_project_description($project) ||"none";6439my%co= parse_commit("HEAD");6440my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();6441my$head=$co{'id'};6442my$remote_heads= gitweb_check_feature('remote_heads');64436444my$owner= git_get_project_owner($project);64456446my$refs= git_get_references();6447# These get_*_list functions return one more to allow us to see if6448# there are more ...6449my@taglist= git_get_tags_list(16);6450my@headlist= git_get_heads_list(16);6451my%remotedata=$remote_heads? git_get_remotes_list() : ();6452my@forklist;6453my$check_forks= gitweb_check_feature('forks');64546455if($check_forks) {6456# find forks of a project6457my$filter=$project;6458$filter=~s/\.git$//;6459@forklist= git_get_projects_list($filter);6460# filter out forks of forks6461@forklist= filter_forks_from_projects_list(\@forklist)6462if(@forklist);6463}64646465 git_header_html();6466 git_print_page_nav('summary','',$head);64676468print"<div class=\"title\"> </div>\n";6469print"<table class=\"projects_list\">\n".6470"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n";6471unless($omit_owner) {6472print"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";6473}6474if(defined$cd{'rfc2822'}) {6475print"<tr id=\"metadata_lchange\"><td>last change</td>".6476"<td>".format_timestamp_html(\%cd)."</td></tr>\n";6477}64786479# use per project git URL list in $projectroot/$project/cloneurl6480# or make project git URL from git base URL and project name6481my$url_tag="URL";6482my@url_list= git_get_project_url_list($project);6483@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;6484foreachmy$git_url(@url_list) {6485next unless$git_url;6486print format_repo_url($url_tag,$git_url);6487$url_tag="";6488}64896490# Tag cloud6491my$show_ctags= gitweb_check_feature('ctags');6492if($show_ctags) {6493my$ctags= git_get_project_ctags($project);6494if(%$ctags) {6495# without ability to add tags, don't show if there are none6496my$cloud= git_populate_project_tagcloud($ctags);6497print"<tr id=\"metadata_ctags\">".6498"<td>content tags</td>".6499"<td>".git_show_project_tagcloud($cloud,48)."</td>".6500"</tr>\n";6501}6502}65036504print"</table>\n";65056506# If XSS prevention is on, we don't include README.html.6507# TODO: Allow a readme in some safe format.6508if(!$prevent_xss&& -s "$projectroot/$project/README.html") {6509print"<div class=\"title\">readme</div>\n".6510"<div class=\"readme\">\n";6511 insert_file("$projectroot/$project/README.html");6512print"\n</div>\n";# class="readme"6513}65146515# we need to request one more than 16 (0..15) to check if6516# those 16 are all6517my@commitlist=$head? parse_commits($head,17) : ();6518if(@commitlist) {6519 git_print_header_div('shortlog');6520 git_shortlog_body(\@commitlist,0,15,$refs,6521$#commitlist<=15?undef:6522$cgi->a({-href => href(action=>"shortlog")},"..."));6523}65246525if(@taglist) {6526 git_print_header_div('tags');6527 git_tags_body(\@taglist,0,15,6528$#taglist<=15?undef:6529$cgi->a({-href => href(action=>"tags")},"..."));6530}65316532if(@headlist) {6533 git_print_header_div('heads');6534 git_heads_body(\@headlist,$head,0,15,6535$#headlist<=15?undef:6536$cgi->a({-href => href(action=>"heads")},"..."));6537}65386539if(%remotedata) {6540 git_print_header_div('remotes');6541 git_remotes_body(\%remotedata,15,$head);6542}65436544if(@forklist) {6545 git_print_header_div('forks');6546 git_project_list_body(\@forklist,'age',0,15,6547$#forklist<=15?undef:6548$cgi->a({-href => href(action=>"forks")},"..."),6549'no_header');6550}65516552 git_footer_html();6553}65546555sub git_tag {6556my%tag= parse_tag($hash);65576558if(!%tag) {6559 die_error(404,"Unknown tag object");6560}65616562my$head= git_get_head_hash($project);6563 git_header_html();6564 git_print_page_nav('','',$head,undef,$head);6565 git_print_header_div('commit', esc_html($tag{'name'}),$hash);6566print"<div class=\"title_text\">\n".6567"<table class=\"object_header\">\n".6568"<tr>\n".6569"<td>object</td>\n".6570"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},6571$tag{'object'}) ."</td>\n".6572"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},6573$tag{'type'}) ."</td>\n".6574"</tr>\n";6575if(defined($tag{'author'})) {6576 git_print_authorship_rows(\%tag,'author');6577}6578print"</table>\n\n".6579"</div>\n";6580print"<div class=\"page_body\">";6581my$comment=$tag{'comment'};6582foreachmy$line(@$comment) {6583chomp$line;6584print esc_html($line, -nbsp=>1) ."<br/>\n";6585}6586print"</div>\n";6587 git_footer_html();6588}65896590sub git_blame_common {6591my$format=shift||'porcelain';6592if($formateq'porcelain'&&$input_params{'javascript'}) {6593$format='incremental';6594$action='blame_incremental';# for page title etc6595}65966597# permissions6598 gitweb_check_feature('blame')6599or die_error(403,"Blame view not allowed");66006601# error checking6602 die_error(400,"No file name given")unless$file_name;6603$hash_base||= git_get_head_hash($project);6604 die_error(404,"Couldn't find base commit")unless$hash_base;6605my%co= parse_commit($hash_base)6606or die_error(404,"Commit not found");6607my$ftype="blob";6608if(!defined$hash) {6609$hash= git_get_hash_by_path($hash_base,$file_name,"blob")6610or die_error(404,"Error looking up file");6611}else{6612$ftype= git_get_type($hash);6613if($ftype!~"blob") {6614 die_error(400,"Object is not a blob");6615}6616}66176618my$fd;6619if($formateq'incremental') {6620# get file contents (as base)6621open$fd,"-|", git_cmd(),'cat-file','blob',$hash6622or die_error(500,"Open git-cat-file failed");6623}elsif($formateq'data') {6624# run git-blame --incremental6625open$fd,"-|", git_cmd(),"blame","--incremental",6626$hash_base,"--",$file_name6627or die_error(500,"Open git-blame --incremental failed");6628}else{6629# run git-blame --porcelain6630open$fd,"-|", git_cmd(),"blame",'-p',6631$hash_base,'--',$file_name6632or die_error(500,"Open git-blame --porcelain failed");6633}66346635# incremental blame data returns early6636if($formateq'data') {6637print$cgi->header(6638-type=>"text/plain", -charset =>"utf-8",6639-status=>"200 OK");6640local$| =1;# output autoflush6641while(my$line= <$fd>) {6642print to_utf8($line);6643}6644close$fd6645or print"ERROR$!\n";66466647print'END';6648if(defined$t0&& gitweb_check_feature('timed')) {6649print' '.6650 tv_interval($t0, [ gettimeofday() ]).6651' '.$number_of_git_cmds;6652}6653print"\n";66546655return;6656}66576658# page header6659 git_header_html();6660my$formats_nav=6661$cgi->a({-href => href(action=>"blob", -replay=>1)},6662"blob") .6663" | ";6664if($formateq'incremental') {6665$formats_nav.=6666$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},6667"blame") ." (non-incremental)";6668}else{6669$formats_nav.=6670$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},6671"blame") ." (incremental)";6672}6673$formats_nav.=6674" | ".6675$cgi->a({-href => href(action=>"history", -replay=>1)},6676"history") .6677" | ".6678$cgi->a({-href => href(action=>$action, file_name=>$file_name)},6679"HEAD");6680 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6681 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6682 git_print_page_path($file_name,$ftype,$hash_base);66836684# page body6685if($formateq'incremental') {6686print"<noscript>\n<div class=\"error\"><center><b>\n".6687"This page requires JavaScript to run.\nUse ".6688$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},6689'this page').6690" instead.\n".6691"</b></center></div>\n</noscript>\n";66926693print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;6694}66956696print qq!<div class="page_body">\n!;6697print qq!<div id="progress_info">.../ ...</div>\n!6698if($formateq'incremental');6699print qq!<table id="blame_table"class="blame" width="100%">\n!.6700#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.6701 qq!<thead>\n!.6702 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.6703 qq!</thead>\n!.6704 qq!<tbody>\n!;67056706my@rev_color=qw(light dark);6707my$num_colors=scalar(@rev_color);6708my$current_color=0;67096710if($formateq'incremental') {6711my$color_class=$rev_color[$current_color];67126713#contents of a file6714my$linenr=0;6715 LINE:6716while(my$line= <$fd>) {6717chomp$line;6718$linenr++;67196720print qq!<tr id="l$linenr"class="$color_class">!.6721 qq!<td class="sha1"><a href=""> </a></td>!.6722 qq!<td class="linenr">!.6723 qq!<a class="linenr" href="">$linenr</a></td>!;6724print qq!<td class="pre">! . esc_html($line) ."</td>\n";6725print qq!</tr>\n!;6726}67276728}else{# porcelain, i.e. ordinary blame6729my%metainfo= ();# saves information about commits67306731# blame data6732 LINE:6733while(my$line= <$fd>) {6734chomp$line;6735# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]6736# no <lines in group> for subsequent lines in group of lines6737my($full_rev,$orig_lineno,$lineno,$group_size) =6738($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);6739if(!exists$metainfo{$full_rev}) {6740$metainfo{$full_rev} = {'nprevious'=>0};6741}6742my$meta=$metainfo{$full_rev};6743my$data;6744while($data= <$fd>) {6745chomp$data;6746last if($data=~s/^\t//);# contents of line6747if($data=~/^(\S+)(?: (.*))?$/) {6748$meta->{$1} =$2unlessexists$meta->{$1};6749}6750if($data=~/^previous /) {6751$meta->{'nprevious'}++;6752}6753}6754my$short_rev=substr($full_rev,0,8);6755my$author=$meta->{'author'};6756my%date=6757 parse_date($meta->{'author-time'},$meta->{'author-tz'});6758my$date=$date{'iso-tz'};6759if($group_size) {6760$current_color= ($current_color+1) %$num_colors;6761}6762my$tr_class=$rev_color[$current_color];6763$tr_class.=' boundary'if(exists$meta->{'boundary'});6764$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);6765$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);6766print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";6767if($group_size) {6768print"<td class=\"sha1\"";6769print" title=\"". esc_html($author) .",$date\"";6770print" rowspan=\"$group_size\""if($group_size>1);6771print">";6772print$cgi->a({-href => href(action=>"commit",6773 hash=>$full_rev,6774 file_name=>$file_name)},6775 esc_html($short_rev));6776if($group_size>=2) {6777my@author_initials= ($author=~/\b([[:upper:]])\B/g);6778if(@author_initials) {6779print"<br />".6780 esc_html(join('',@author_initials));6781# or join('.', ...)6782}6783}6784print"</td>\n";6785}6786# 'previous' <sha1 of parent commit> <filename at commit>6787if(exists$meta->{'previous'} &&6788$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {6789$meta->{'parent'} =$1;6790$meta->{'file_parent'} = unquote($2);6791}6792my$linenr_commit=6793exists($meta->{'parent'}) ?6794$meta->{'parent'} :$full_rev;6795my$linenr_filename=6796exists($meta->{'file_parent'}) ?6797$meta->{'file_parent'} : unquote($meta->{'filename'});6798my$blamed= href(action =>'blame',6799 file_name =>$linenr_filename,6800 hash_base =>$linenr_commit);6801print"<td class=\"linenr\">";6802print$cgi->a({ -href =>"$blamed#l$orig_lineno",6803-class=>"linenr"},6804 esc_html($lineno));6805print"</td>";6806print"<td class=\"pre\">". esc_html($data) ."</td>\n";6807print"</tr>\n";6808}# end while68096810}68116812# footer6813print"</tbody>\n".6814"</table>\n";# class="blame"6815print"</div>\n";# class="blame_body"6816close$fd6817or print"Reading blob failed\n";68186819 git_footer_html();6820}68216822sub git_blame {6823 git_blame_common();6824}68256826sub git_blame_incremental {6827 git_blame_common('incremental');6828}68296830sub git_blame_data {6831 git_blame_common('data');6832}68336834sub git_tags {6835my$head= git_get_head_hash($project);6836 git_header_html();6837 git_print_page_nav('','',$head,undef,$head,format_ref_views('tags'));6838 git_print_header_div('summary',$project);68396840my@tagslist= git_get_tags_list();6841if(@tagslist) {6842 git_tags_body(\@tagslist);6843}6844 git_footer_html();6845}68466847sub git_heads {6848my$head= git_get_head_hash($project);6849 git_header_html();6850 git_print_page_nav('','',$head,undef,$head,format_ref_views('heads'));6851 git_print_header_div('summary',$project);68526853my@headslist= git_get_heads_list();6854if(@headslist) {6855 git_heads_body(\@headslist,$head);6856}6857 git_footer_html();6858}68596860# used both for single remote view and for list of all the remotes6861sub git_remotes {6862 gitweb_check_feature('remote_heads')6863or die_error(403,"Remote heads view is disabled");68646865my$head= git_get_head_hash($project);6866my$remote=$input_params{'hash'};68676868my$remotedata= git_get_remotes_list($remote);6869 die_error(500,"Unable to get remote information")unlessdefined$remotedata;68706871unless(%$remotedata) {6872 die_error(404,defined$remote?6873"Remote$remotenot found":6874"No remotes found");6875}68766877 git_header_html(undef,undef, -action_extra =>$remote);6878 git_print_page_nav('','',$head,undef,$head,6879 format_ref_views($remote?'':'remotes'));68806881 fill_remote_heads($remotedata);6882if(defined$remote) {6883 git_print_header_div('remotes',"$remoteremote for$project");6884 git_remote_block($remote,$remotedata->{$remote},undef,$head);6885}else{6886 git_print_header_div('summary',"$projectremotes");6887 git_remotes_body($remotedata,undef,$head);6888}68896890 git_footer_html();6891}68926893sub git_blob_plain {6894my$type=shift;6895my$expires;68966897if(!defined$hash) {6898if(defined$file_name) {6899my$base=$hash_base|| git_get_head_hash($project);6900$hash= git_get_hash_by_path($base,$file_name,"blob")6901or die_error(404,"Cannot find file");6902}else{6903 die_error(400,"No file name defined");6904}6905}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6906# blobs defined by non-textual hash id's can be cached6907$expires="+1d";6908}69096910open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6911or die_error(500,"Open git-cat-file blob '$hash' failed");69126913# content-type (can include charset)6914$type= blob_contenttype($fd,$file_name,$type);69156916# "save as" filename, even when no $file_name is given6917my$save_as="$hash";6918if(defined$file_name) {6919$save_as=$file_name;6920}elsif($type=~m/^text\//) {6921$save_as.='.txt';6922}69236924# With XSS prevention on, blobs of all types except a few known safe6925# ones are served with "Content-Disposition: attachment" to make sure6926# they don't run in our security domain. For certain image types,6927# blob view writes an <img> tag referring to blob_plain view, and we6928# want to be sure not to break that by serving the image as an6929# attachment (though Firefox 3 doesn't seem to care).6930my$sandbox=$prevent_xss&&6931$type!~m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;69326933# serve text/* as text/plain6934if($prevent_xss&&6935($type=~m!^text/[a-z]+\b(.*)$!||6936($type=~m!^[a-z]+/[a-z]\+xml\b(.*)$!&& -T $fd))) {6937my$rest=$1;6938$rest=defined$rest?$rest:'';6939$type="text/plain$rest";6940}69416942print$cgi->header(6943-type =>$type,6944-expires =>$expires,6945-content_disposition =>6946($sandbox?'attachment':'inline')6947.'; filename="'.$save_as.'"');6948local$/=undef;6949binmode STDOUT,':raw';6950print<$fd>;6951binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi6952close$fd;6953}69546955sub git_blob {6956my$expires;69576958if(!defined$hash) {6959if(defined$file_name) {6960my$base=$hash_base|| git_get_head_hash($project);6961$hash= git_get_hash_by_path($base,$file_name,"blob")6962or die_error(404,"Cannot find file");6963}else{6964 die_error(400,"No file name defined");6965}6966}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6967# blobs defined by non-textual hash id's can be cached6968$expires="+1d";6969}69706971my$have_blame= gitweb_check_feature('blame');6972open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6973or die_error(500,"Couldn't cat$file_name,$hash");6974my$mimetype= blob_mimetype($fd,$file_name);6975# use 'blob_plain' (aka 'raw') view for files that cannot be displayed6976if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {6977close$fd;6978return git_blob_plain($mimetype);6979}6980# we can have blame only for text/* mimetype6981$have_blame&&= ($mimetype=~m!^text/!);69826983my$highlight= gitweb_check_feature('highlight');6984my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);6985$fd= run_highlighter($fd,$highlight,$syntax)6986if$syntax;69876988 git_header_html(undef,$expires);6989my$formats_nav='';6990if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6991if(defined$file_name) {6992if($have_blame) {6993$formats_nav.=6994$cgi->a({-href => href(action=>"blame", -replay=>1)},6995"blame") .6996" | ";6997}6998$formats_nav.=6999$cgi->a({-href => href(action=>"history", -replay=>1)},7000"history") .7001" | ".7002$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},7003"raw") .7004" | ".7005$cgi->a({-href => href(action=>"blob",7006 hash_base=>"HEAD", file_name=>$file_name)},7007"HEAD");7008}else{7009$formats_nav.=7010$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},7011"raw");7012}7013 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);7014 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);7015}else{7016print"<div class=\"page_nav\">\n".7017"<br/><br/></div>\n".7018"<div class=\"title\">".esc_html($hash)."</div>\n";7019}7020 git_print_page_path($file_name,"blob",$hash_base);7021print"<div class=\"page_body\">\n";7022if($mimetype=~m!^image/!) {7023print qq!<img type="!.esc_attr($mimetype).qq!"!;7024if($file_name) {7025print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;7026}7027print qq! src="! .7028 href(action=>"blob_plain", hash=>$hash,7029 hash_base=>$hash_base, file_name=>$file_name) .7030 qq!"/>\n!;7031}else{7032my$nr;7033while(my$line= <$fd>) {7034chomp$line;7035$nr++;7036$line= untabify($line);7037printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,7038$nr, esc_attr(href(-replay =>1)),$nr,$nr,7039$syntax? sanitize($line) : esc_html($line, -nbsp=>1);7040}7041}7042close$fd7043or print"Reading blob failed.\n";7044print"</div>";7045 git_footer_html();7046}70477048sub git_tree {7049if(!defined$hash_base) {7050$hash_base="HEAD";7051}7052if(!defined$hash) {7053if(defined$file_name) {7054$hash= git_get_hash_by_path($hash_base,$file_name,"tree");7055}else{7056$hash=$hash_base;7057}7058}7059 die_error(404,"No such tree")unlessdefined($hash);70607061my$show_sizes= gitweb_check_feature('show-sizes');7062my$have_blame= gitweb_check_feature('blame');70637064my@entries= ();7065{7066local$/="\0";7067open my$fd,"-|", git_cmd(),"ls-tree",'-z',7068($show_sizes?'-l': ()),@extra_options,$hash7069or die_error(500,"Open git-ls-tree failed");7070@entries=map{chomp;$_} <$fd>;7071close$fd7072or die_error(404,"Reading tree failed");7073}70747075my$refs= git_get_references();7076my$ref= format_ref_marker($refs,$hash_base);7077 git_header_html();7078my$basedir='';7079if(defined$hash_base&& (my%co= parse_commit($hash_base))) {7080my@views_nav= ();7081if(defined$file_name) {7082push@views_nav,7083$cgi->a({-href => href(action=>"history", -replay=>1)},7084"history"),7085$cgi->a({-href => href(action=>"tree",7086 hash_base=>"HEAD", file_name=>$file_name)},7087"HEAD"),7088}7089my$snapshot_links= format_snapshot_links($hash);7090if(defined$snapshot_links) {7091# FIXME: Should be available when we have no hash base as well.7092push@views_nav,$snapshot_links;7093}7094 git_print_page_nav('tree','',$hash_base,undef,undef,7095join(' | ',@views_nav));7096 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);7097}else{7098undef$hash_base;7099print"<div class=\"page_nav\">\n";7100print"<br/><br/></div>\n";7101print"<div class=\"title\">".esc_html($hash)."</div>\n";7102}7103if(defined$file_name) {7104$basedir=$file_name;7105if($basedirne''&&substr($basedir, -1)ne'/') {7106$basedir.='/';7107}7108 git_print_page_path($file_name,'tree',$hash_base);7109}7110print"<div class=\"page_body\">\n";7111print"<table class=\"tree\">\n";7112my$alternate=1;7113# '..' (top directory) link if possible7114if(defined$hash_base&&7115defined$file_name&&$file_name=~m![^/]+$!) {7116if($alternate) {7117print"<tr class=\"dark\">\n";7118}else{7119print"<tr class=\"light\">\n";7120}7121$alternate^=1;71227123my$up=$file_name;7124$up=~s!/?[^/]+$!!;7125undef$upunless$up;7126# based on git_print_tree_entry7127print'<td class="mode">'. mode_str('040000') ."</td>\n";7128print'<td class="size"> </td>'."\n"if$show_sizes;7129print'<td class="list">';7130print$cgi->a({-href => href(action=>"tree",7131 hash_base=>$hash_base,7132 file_name=>$up)},7133"..");7134print"</td>\n";7135print"<td class=\"link\"></td>\n";71367137print"</tr>\n";7138}7139foreachmy$line(@entries) {7140my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);71417142if($alternate) {7143print"<tr class=\"dark\">\n";7144}else{7145print"<tr class=\"light\">\n";7146}7147$alternate^=1;71487149 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);71507151print"</tr>\n";7152}7153print"</table>\n".7154"</div>";7155 git_footer_html();7156}71577158sub snapshot_name {7159my($project,$hash) =@_;71607161# path/to/project.git -> project7162# path/to/project/.git -> project7163my$name= to_utf8($project);7164$name=~ s,([^/])/*\.git$,$1,;7165$name= basename($name);7166# sanitize name7167$name=~s/[[:cntrl:]]/?/g;71687169my$ver=$hash;7170if($hash=~/^[0-9a-fA-F]+$/) {7171# shorten SHA-1 hash7172my$full_hash= git_get_full_hash($project,$hash);7173if($full_hash=~/^$hash/&&length($hash) >7) {7174$ver= git_get_short_hash($project,$hash);7175}7176}elsif($hash=~m!^refs/tags/(.*)$!) {7177# tags don't need shortened SHA-1 hash7178$ver=$1;7179}else{7180# branches and other need shortened SHA-1 hash7181if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {7182$ver=$1;7183}7184$ver.='-'. git_get_short_hash($project,$hash);7185}7186# in case of hierarchical branch names7187$ver=~s!/!.!g;71887189# name = project-version_string7190$name="$name-$ver";71917192returnwantarray? ($name,$name) :$name;7193}71947195sub exit_if_unmodified_since {7196my($latest_epoch) =@_;7197our$cgi;71987199my$if_modified=$cgi->http('IF_MODIFIED_SINCE');7200if(defined$if_modified) {7201my$since;7202if(eval{require HTTP::Date;1; }) {7203$since= HTTP::Date::str2time($if_modified);7204}elsif(eval{require Time::ParseDate;1; }) {7205$since= Time::ParseDate::parsedate($if_modified, GMT =>1);7206}7207if(defined$since&&$latest_epoch<=$since) {7208my%latest_date= parse_date($latest_epoch);7209print$cgi->header(7210-last_modified =>$latest_date{'rfc2822'},7211-status =>'304 Not Modified');7212goto DONE_GITWEB;7213}7214}7215}72167217sub git_snapshot {7218my$format=$input_params{'snapshot_format'};7219if(!@snapshot_fmts) {7220 die_error(403,"Snapshots not allowed");7221}7222# default to first supported snapshot format7223$format||=$snapshot_fmts[0];7224if($format!~m/^[a-z0-9]+$/) {7225 die_error(400,"Invalid snapshot format parameter");7226}elsif(!exists($known_snapshot_formats{$format})) {7227 die_error(400,"Unknown snapshot format");7228}elsif($known_snapshot_formats{$format}{'disabled'}) {7229 die_error(403,"Snapshot format not allowed");7230}elsif(!grep($_eq$format,@snapshot_fmts)) {7231 die_error(403,"Unsupported snapshot format");7232}72337234my$type= git_get_type("$hash^{}");7235if(!$type) {7236 die_error(404,'Object does not exist');7237}elsif($typeeq'blob') {7238 die_error(400,'Object is not a tree-ish');7239}72407241my($name,$prefix) = snapshot_name($project,$hash);7242my$filename="$name$known_snapshot_formats{$format}{'suffix'}";72437244my%co= parse_commit($hash);7245 exit_if_unmodified_since($co{'committer_epoch'})if%co;72467247my$cmd= quote_command(7248 git_cmd(),'archive',7249"--format=$known_snapshot_formats{$format}{'format'}",7250"--prefix=$prefix/",$hash);7251if(exists$known_snapshot_formats{$format}{'compressor'}) {7252$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});7253}72547255$filename=~s/(["\\])/\\$1/g;7256my%latest_date;7257if(%co) {7258%latest_date= parse_date($co{'committer_epoch'},$co{'committer_tz'});7259}72607261print$cgi->header(7262-type =>$known_snapshot_formats{$format}{'type'},7263-content_disposition =>'inline; filename="'.$filename.'"',7264%co? (-last_modified =>$latest_date{'rfc2822'}) : (),7265-status =>'200 OK');72667267open my$fd,"-|",$cmd7268or die_error(500,"Execute git-archive failed");7269binmode STDOUT,':raw';7270print<$fd>;7271binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi7272close$fd;7273}72747275sub git_log_generic {7276my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;72777278my$head= git_get_head_hash($project);7279if(!defined$base) {7280$base=$head;7281}7282if(!defined$page) {7283$page=0;7284}7285my$refs= git_get_references();72867287my$commit_hash=$base;7288if(defined$parent) {7289$commit_hash="$parent..$base";7290}7291my@commitlist=7292 parse_commits($commit_hash,101, (100*$page),7293defined$file_name? ($file_name,"--full-history") : ());72947295my$ftype;7296if(!defined$file_hash&&defined$file_name) {7297# some commits could have deleted file in question,7298# and not have it in tree, but one of them has to have it7299for(my$i=0;$i<@commitlist;$i++) {7300$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);7301last ifdefined$file_hash;7302}7303}7304if(defined$file_hash) {7305$ftype= git_get_type($file_hash);7306}7307if(defined$file_name&& !defined$ftype) {7308 die_error(500,"Unknown type of object");7309}7310my%co;7311if(defined$file_name) {7312%co= parse_commit($base)7313or die_error(404,"Unknown commit object");7314}731573167317my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);7318my$next_link='';7319if($#commitlist>=100) {7320$next_link=7321$cgi->a({-href => href(-replay=>1, page=>$page+1),7322-accesskey =>"n", -title =>"Alt-n"},"next");7323}7324my$patch_max= gitweb_get_feature('patches');7325if($patch_max&& !defined$file_name) {7326if($patch_max<0||@commitlist<=$patch_max) {7327$paging_nav.=" ⋅ ".7328$cgi->a({-href => href(action=>"patches", -replay=>1)},7329"patches");7330}7331}73327333 git_header_html();7334 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);7335if(defined$file_name) {7336 git_print_header_div('commit', esc_html($co{'title'}),$base);7337}else{7338 git_print_header_div('summary',$project)7339}7340 git_print_page_path($file_name,$ftype,$hash_base)7341if(defined$file_name);73427343$body_subr->(\@commitlist,0,99,$refs,$next_link,7344$file_name,$file_hash,$ftype);73457346 git_footer_html();7347}73487349sub git_log {7350 git_log_generic('log', \&git_log_body,7351$hash,$hash_parent);7352}73537354sub git_commit {7355$hash||=$hash_base||"HEAD";7356my%co= parse_commit($hash)7357or die_error(404,"Unknown commit object");73587359my$parent=$co{'parent'};7360my$parents=$co{'parents'};# listref73617362# we need to prepare $formats_nav before any parameter munging7363my$formats_nav;7364if(!defined$parent) {7365# --root commitdiff7366$formats_nav.='(initial)';7367}elsif(@$parents==1) {7368# single parent commit7369$formats_nav.=7370'(parent: '.7371$cgi->a({-href => href(action=>"commit",7372 hash=>$parent)},7373 esc_html(substr($parent,0,7))) .7374')';7375}else{7376# merge commit7377$formats_nav.=7378'(merge: '.7379join(' ',map{7380$cgi->a({-href => href(action=>"commit",7381 hash=>$_)},7382 esc_html(substr($_,0,7)));7383}@$parents) .7384')';7385}7386if(gitweb_check_feature('patches') &&@$parents<=1) {7387$formats_nav.=" | ".7388$cgi->a({-href => href(action=>"patch", -replay=>1)},7389"patch");7390}73917392if(!defined$parent) {7393$parent="--root";7394}7395my@difftree;7396open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",7397@diff_opts,7398(@$parents<=1?$parent:'-c'),7399$hash,"--"7400or die_error(500,"Open git-diff-tree failed");7401@difftree=map{chomp;$_} <$fd>;7402close$fdor die_error(404,"Reading git-diff-tree failed");74037404# non-textual hash id's can be cached7405my$expires;7406if($hash=~m/^[0-9a-fA-F]{40}$/) {7407$expires="+1d";7408}7409my$refs= git_get_references();7410my$ref= format_ref_marker($refs,$co{'id'});74117412 git_header_html(undef,$expires);7413 git_print_page_nav('commit','',7414$hash,$co{'tree'},$hash,7415$formats_nav);74167417if(defined$co{'parent'}) {7418 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);7419}else{7420 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);7421}7422print"<div class=\"title_text\">\n".7423"<table class=\"object_header\">\n";7424 git_print_authorship_rows(\%co);7425print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";7426print"<tr>".7427"<td>tree</td>".7428"<td class=\"sha1\">".7429$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),7430class=>"list"},$co{'tree'}) .7431"</td>".7432"<td class=\"link\">".7433$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},7434"tree");7435my$snapshot_links= format_snapshot_links($hash);7436if(defined$snapshot_links) {7437print" | ".$snapshot_links;7438}7439print"</td>".7440"</tr>\n";74417442foreachmy$par(@$parents) {7443print"<tr>".7444"<td>parent</td>".7445"<td class=\"sha1\">".7446$cgi->a({-href => href(action=>"commit", hash=>$par),7447class=>"list"},$par) .7448"</td>".7449"<td class=\"link\">".7450$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .7451" | ".7452$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .7453"</td>".7454"</tr>\n";7455}7456print"</table>".7457"</div>\n";74587459print"<div class=\"page_body\">\n";7460 git_print_log($co{'comment'});7461print"</div>\n";74627463 git_difftree_body(\@difftree,$hash,@$parents);74647465 git_footer_html();7466}74677468sub git_object {7469# object is defined by:7470# - hash or hash_base alone7471# - hash_base and file_name7472my$type;74737474# - hash or hash_base alone7475if($hash|| ($hash_base&& !defined$file_name)) {7476my$object_id=$hash||$hash_base;74777478open my$fd,"-|", quote_command(7479 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'7480or die_error(404,"Object does not exist");7481$type= <$fd>;7482chomp$type;7483close$fd7484or die_error(404,"Object does not exist");74857486# - hash_base and file_name7487}elsif($hash_base&&defined$file_name) {7488$file_name=~ s,/+$,,;74897490system(git_cmd(),"cat-file",'-e',$hash_base) ==07491or die_error(404,"Base object does not exist");74927493# here errors should not happen7494open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name7495or die_error(500,"Open git-ls-tree failed");7496my$line= <$fd>;7497close$fd;74987499#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'7500unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {7501 die_error(404,"File or directory for given base does not exist");7502}7503$type=$2;7504$hash=$3;7505}else{7506 die_error(400,"Not enough information to find object");7507}75087509print$cgi->redirect(-uri => href(action=>$type, -full=>1,7510 hash=>$hash, hash_base=>$hash_base,7511 file_name=>$file_name),7512-status =>'302 Found');7513}75147515sub git_blobdiff {7516my$format=shift||'html';7517my$diff_style=$input_params{'diff_style'} ||'inline';75187519my$fd;7520my@difftree;7521my%diffinfo;7522my$expires;75237524# preparing $fd and %diffinfo for git_patchset_body7525# new style URI7526if(defined$hash_base&&defined$hash_parent_base) {7527if(defined$file_name) {7528# read raw output7529open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7530$hash_parent_base,$hash_base,7531"--", (defined$file_parent?$file_parent: ()),$file_name7532or die_error(500,"Open git-diff-tree failed");7533@difftree=map{chomp;$_} <$fd>;7534close$fd7535or die_error(404,"Reading git-diff-tree failed");7536@difftree7537or die_error(404,"Blob diff not found");75387539}elsif(defined$hash&&7540$hash=~/[0-9a-fA-F]{40}/) {7541# try to find filename from $hash75427543# read filtered raw output7544open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7545$hash_parent_base,$hash_base,"--"7546or die_error(500,"Open git-diff-tree failed");7547@difftree=7548# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'7549# $hash == to_id7550grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}7551map{chomp;$_} <$fd>;7552close$fd7553or die_error(404,"Reading git-diff-tree failed");7554@difftree7555or die_error(404,"Blob diff not found");75567557}else{7558 die_error(400,"Missing one of the blob diff parameters");7559}75607561if(@difftree>1) {7562 die_error(400,"Ambiguous blob diff specification");7563}75647565%diffinfo= parse_difftree_raw_line($difftree[0]);7566$file_parent||=$diffinfo{'from_file'} ||$file_name;7567$file_name||=$diffinfo{'to_file'};75687569$hash_parent||=$diffinfo{'from_id'};7570$hash||=$diffinfo{'to_id'};75717572# non-textual hash id's can be cached7573if($hash_base=~m/^[0-9a-fA-F]{40}$/&&7574$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {7575$expires='+1d';7576}75777578# open patch output7579open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7580'-p', ($formateq'html'?"--full-index": ()),7581$hash_parent_base,$hash_base,7582"--", (defined$file_parent?$file_parent: ()),$file_name7583or die_error(500,"Open git-diff-tree failed");7584}75857586# old/legacy style URI -- not generated anymore since 1.4.3.7587if(!%diffinfo) {7588 die_error('404 Not Found',"Missing one of the blob diff parameters")7589}75907591# header7592if($formateq'html') {7593my$formats_nav=7594$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},7595"raw");7596$formats_nav.= diff_style_nav($diff_style);7597 git_header_html(undef,$expires);7598if(defined$hash_base&& (my%co= parse_commit($hash_base))) {7599 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);7600 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);7601}else{7602print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";7603print"<div class=\"title\">".esc_html("$hashvs$hash_parent")."</div>\n";7604}7605if(defined$file_name) {7606 git_print_page_path($file_name,"blob",$hash_base);7607}else{7608print"<div class=\"page_path\"></div>\n";7609}76107611}elsif($formateq'plain') {7612print$cgi->header(7613-type =>'text/plain',7614-charset =>'utf-8',7615-expires =>$expires,7616-content_disposition =>'inline; filename="'."$file_name".'.patch"');76177618print"X-Git-Url: ".$cgi->self_url() ."\n\n";76197620}else{7621 die_error(400,"Unknown blobdiff format");7622}76237624# patch7625if($formateq'html') {7626print"<div class=\"page_body\">\n";76277628 git_patchset_body($fd,$diff_style,7629[ \%diffinfo],$hash_base,$hash_parent_base);7630close$fd;76317632print"</div>\n";# class="page_body"7633 git_footer_html();76347635}else{7636while(my$line= <$fd>) {7637$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;7638$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;76397640print$line;76417642last if$line=~m!^\+\+\+!;7643}7644local$/=undef;7645print<$fd>;7646close$fd;7647}7648}76497650sub git_blobdiff_plain {7651 git_blobdiff('plain');7652}76537654# assumes that it is added as later part of already existing navigation,7655# so it returns "| foo | bar" rather than just "foo | bar"7656sub diff_style_nav {7657my($diff_style,$is_combined) =@_;7658$diff_style||='inline';76597660return""if($is_combined);76617662my@styles= (inline =>'inline','sidebyside'=>'side by side');7663my%styles=@styles;7664@styles=7665@styles[map{$_*2}0..$#styles/2];76667667returnjoin'',7668map{" | ".$_}7669map{7670$_eq$diff_style?$styles{$_} :7671$cgi->a({-href => href(-replay=>1, diff_style =>$_)},$styles{$_})7672}@styles;7673}76747675sub git_commitdiff {7676my%params=@_;7677my$format=$params{-format} ||'html';7678my$diff_style=$input_params{'diff_style'} ||'inline';76797680my($patch_max) = gitweb_get_feature('patches');7681if($formateq'patch') {7682 die_error(403,"Patch view not allowed")unless$patch_max;7683}76847685$hash||=$hash_base||"HEAD";7686my%co= parse_commit($hash)7687or die_error(404,"Unknown commit object");76887689# choose format for commitdiff for merge7690if(!defined$hash_parent&& @{$co{'parents'}} >1) {7691$hash_parent='--cc';7692}7693# we need to prepare $formats_nav before almost any parameter munging7694my$formats_nav;7695if($formateq'html') {7696$formats_nav=7697$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},7698"raw");7699if($patch_max&& @{$co{'parents'}} <=1) {7700$formats_nav.=" | ".7701$cgi->a({-href => href(action=>"patch", -replay=>1)},7702"patch");7703}7704$formats_nav.= diff_style_nav($diff_style, @{$co{'parents'}} >1);77057706if(defined$hash_parent&&7707$hash_parentne'-c'&&$hash_parentne'--cc') {7708# commitdiff with two commits given7709my$hash_parent_short=$hash_parent;7710if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {7711$hash_parent_short=substr($hash_parent,0,7);7712}7713$formats_nav.=7714' (from';7715for(my$i=0;$i< @{$co{'parents'}};$i++) {7716if($co{'parents'}[$i]eq$hash_parent) {7717$formats_nav.=' parent '. ($i+1);7718last;7719}7720}7721$formats_nav.=': '.7722$cgi->a({-href => href(-replay=>1,7723 hash=>$hash_parent, hash_base=>undef)},7724 esc_html($hash_parent_short)) .7725')';7726}elsif(!$co{'parent'}) {7727# --root commitdiff7728$formats_nav.=' (initial)';7729}elsif(scalar@{$co{'parents'}} ==1) {7730# single parent commit7731$formats_nav.=7732' (parent: '.7733$cgi->a({-href => href(-replay=>1,7734 hash=>$co{'parent'}, hash_base=>undef)},7735 esc_html(substr($co{'parent'},0,7))) .7736')';7737}else{7738# merge commit7739if($hash_parenteq'--cc') {7740$formats_nav.=' | '.7741$cgi->a({-href => href(-replay=>1,7742 hash=>$hash, hash_parent=>'-c')},7743'combined');7744}else{# $hash_parent eq '-c'7745$formats_nav.=' | '.7746$cgi->a({-href => href(-replay=>1,7747 hash=>$hash, hash_parent=>'--cc')},7748'compact');7749}7750$formats_nav.=7751' (merge: '.7752join(' ',map{7753$cgi->a({-href => href(-replay=>1,7754 hash=>$_, hash_base=>undef)},7755 esc_html(substr($_,0,7)));7756} @{$co{'parents'}} ) .7757')';7758}7759}77607761my$hash_parent_param=$hash_parent;7762if(!defined$hash_parent_param) {7763# --cc for multiple parents, --root for parentless7764$hash_parent_param=7765@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';7766}77677768# read commitdiff7769my$fd;7770my@difftree;7771if($formateq'html') {7772open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7773"--no-commit-id","--patch-with-raw","--full-index",7774$hash_parent_param,$hash,"--"7775or die_error(500,"Open git-diff-tree failed");77767777while(my$line= <$fd>) {7778chomp$line;7779# empty line ends raw part of diff-tree output7780last unless$line;7781push@difftree,scalar parse_difftree_raw_line($line);7782}77837784}elsif($formateq'plain') {7785open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7786'-p',$hash_parent_param,$hash,"--"7787or die_error(500,"Open git-diff-tree failed");7788}elsif($formateq'patch') {7789# For commit ranges, we limit the output to the number of7790# patches specified in the 'patches' feature.7791# For single commits, we limit the output to a single patch,7792# diverging from the git-format-patch default.7793my@commit_spec= ();7794if($hash_parent) {7795if($patch_max>0) {7796push@commit_spec,"-$patch_max";7797}7798push@commit_spec,'-n',"$hash_parent..$hash";7799}else{7800if($params{-single}) {7801push@commit_spec,'-1';7802}else{7803if($patch_max>0) {7804push@commit_spec,"-$patch_max";7805}7806push@commit_spec,"-n";7807}7808push@commit_spec,'--root',$hash;7809}7810open$fd,"-|", git_cmd(),"format-patch",@diff_opts,7811'--encoding=utf8','--stdout',@commit_spec7812or die_error(500,"Open git-format-patch failed");7813}else{7814 die_error(400,"Unknown commitdiff format");7815}78167817# non-textual hash id's can be cached7818my$expires;7819if($hash=~m/^[0-9a-fA-F]{40}$/) {7820$expires="+1d";7821}78227823# write commit message7824if($formateq'html') {7825my$refs= git_get_references();7826my$ref= format_ref_marker($refs,$co{'id'});78277828 git_header_html(undef,$expires);7829 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);7830 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);7831print"<div class=\"title_text\">\n".7832"<table class=\"object_header\">\n";7833 git_print_authorship_rows(\%co);7834print"</table>".7835"</div>\n";7836print"<div class=\"page_body\">\n";7837if(@{$co{'comment'}} >1) {7838print"<div class=\"log\">\n";7839 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);7840print"</div>\n";# class="log"7841}78427843}elsif($formateq'plain') {7844my$refs= git_get_references("tags");7845my$tagname= git_get_rev_name_tags($hash);7846my$filename= basename($project) ."-$hash.patch";78477848print$cgi->header(7849-type =>'text/plain',7850-charset =>'utf-8',7851-expires =>$expires,7852-content_disposition =>'inline; filename="'."$filename".'"');7853my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});7854print"From: ". to_utf8($co{'author'}) ."\n";7855print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";7856print"Subject: ". to_utf8($co{'title'}) ."\n";78577858print"X-Git-Tag:$tagname\n"if$tagname;7859print"X-Git-Url: ".$cgi->self_url() ."\n\n";78607861foreachmy$line(@{$co{'comment'}}) {7862print to_utf8($line) ."\n";7863}7864print"---\n\n";7865}elsif($formateq'patch') {7866my$filename= basename($project) ."-$hash.patch";78677868print$cgi->header(7869-type =>'text/plain',7870-charset =>'utf-8',7871-expires =>$expires,7872-content_disposition =>'inline; filename="'."$filename".'"');7873}78747875# write patch7876if($formateq'html') {7877my$use_parents= !defined$hash_parent||7878$hash_parenteq'-c'||$hash_parenteq'--cc';7879 git_difftree_body(\@difftree,$hash,7880$use_parents? @{$co{'parents'}} :$hash_parent);7881print"<br/>\n";78827883 git_patchset_body($fd,$diff_style,7884 \@difftree,$hash,7885$use_parents? @{$co{'parents'}} :$hash_parent);7886close$fd;7887print"</div>\n";# class="page_body"7888 git_footer_html();78897890}elsif($formateq'plain') {7891local$/=undef;7892print<$fd>;7893close$fd7894or print"Reading git-diff-tree failed\n";7895}elsif($formateq'patch') {7896local$/=undef;7897print<$fd>;7898close$fd7899or print"Reading git-format-patch failed\n";7900}7901}79027903sub git_commitdiff_plain {7904 git_commitdiff(-format =>'plain');7905}79067907# format-patch-style patches7908sub git_patch {7909 git_commitdiff(-format =>'patch', -single =>1);7910}79117912sub git_patches {7913 git_commitdiff(-format =>'patch');7914}79157916sub git_history {7917 git_log_generic('history', \&git_history_body,7918$hash_base,$hash_parent_base,7919$file_name,$hash);7920}79217922sub git_search {7923$searchtype||='commit';79247925# check if appropriate features are enabled7926 gitweb_check_feature('search')7927or die_error(403,"Search is disabled");7928if($searchtypeeq'pickaxe') {7929# pickaxe may take all resources of your box and run for several minutes7930# with every query - so decide by yourself how public you make this feature7931 gitweb_check_feature('pickaxe')7932or die_error(403,"Pickaxe search is disabled");7933}7934if($searchtypeeq'grep') {7935# grep search might be potentially CPU-intensive, too7936 gitweb_check_feature('grep')7937or die_error(403,"Grep search is disabled");7938}79397940if(!defined$searchtext) {7941 die_error(400,"Text field is empty");7942}7943if(!defined$hash) {7944$hash= git_get_head_hash($project);7945}7946my%co= parse_commit($hash);7947if(!%co) {7948 die_error(404,"Unknown commit object");7949}7950if(!defined$page) {7951$page=0;7952}79537954if($searchtypeeq'commit'||7955$searchtypeeq'author'||7956$searchtypeeq'committer') {7957 git_search_message(%co);7958}elsif($searchtypeeq'pickaxe') {7959 git_search_changes(%co);7960}elsif($searchtypeeq'grep') {7961 git_search_files(%co);7962}else{7963 die_error(400,"Unknown search type");7964}7965}79667967sub git_search_help {7968 git_header_html();7969 git_print_page_nav('','',$hash,$hash,$hash);7970print<<EOT;7971<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without7972regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,7973the pattern entered is recognized as the POSIX extended7974<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case7975insensitive).</p>7976<dl>7977<dt><b>commit</b></dt>7978<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>7979EOT7980my$have_grep= gitweb_check_feature('grep');7981if($have_grep) {7982print<<EOT;7983<dt><b>grep</b></dt>7984<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing7985 a different one) are searched for the given pattern. On large trees, this search can take7986a while and put some strain on the server, so please use it with some consideration. Note that7987due to git-grep peculiarity, currently if regexp mode is turned off, the matches are7988case-sensitive.</dd>7989EOT7990}7991print<<EOT;7992<dt><b>author</b></dt>7993<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>7994<dt><b>committer</b></dt>7995<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>7996EOT7997my$have_pickaxe= gitweb_check_feature('pickaxe');7998if($have_pickaxe) {7999print<<EOT;8000<dt><b>pickaxe</b></dt>8001<dd>All commits that caused the string to appear or disappear from any file (changes that8002added, removed or "modified" the string) will be listed. This search can take a while and8003takes a lot of strain on the server, so please use it wisely. Note that since you may be8004interested even in changes just changing the case as well, this search is case sensitive.</dd>8005EOT8006}8007print"</dl>\n";8008 git_footer_html();8009}80108011sub git_shortlog {8012 git_log_generic('shortlog', \&git_shortlog_body,8013$hash,$hash_parent);8014}80158016## ......................................................................8017## feeds (RSS, Atom; OPML)80188019sub git_feed {8020my$format=shift||'atom';8021my$have_blame= gitweb_check_feature('blame');80228023# Atom: http://www.atomenabled.org/developers/syndication/8024# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ8025if($formatne'rss'&&$formatne'atom') {8026 die_error(400,"Unknown web feed format");8027}80288029# log/feed of current (HEAD) branch, log of given branch, history of file/directory8030my$head=$hash||'HEAD';8031my@commitlist= parse_commits($head,150,0,$file_name);80328033my%latest_commit;8034my%latest_date;8035my$content_type="application/$format+xml";8036if(defined$cgi->http('HTTP_ACCEPT') &&8037$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {8038# browser (feed reader) prefers text/xml8039$content_type='text/xml';8040}8041if(defined($commitlist[0])) {8042%latest_commit= %{$commitlist[0]};8043my$latest_epoch=$latest_commit{'committer_epoch'};8044 exit_if_unmodified_since($latest_epoch);8045%latest_date= parse_date($latest_epoch,$latest_commit{'committer_tz'});8046}8047print$cgi->header(8048-type =>$content_type,8049-charset =>'utf-8',8050%latest_date? (-last_modified =>$latest_date{'rfc2822'}) : (),8051-status =>'200 OK');80528053# Optimization: skip generating the body if client asks only8054# for Last-Modified date.8055return if($cgi->request_method()eq'HEAD');80568057# header variables8058my$title="$site_name-$project/$action";8059my$feed_type='log';8060if(defined$hash) {8061$title.=" - '$hash'";8062$feed_type='branch log';8063if(defined$file_name) {8064$title.=" ::$file_name";8065$feed_type='history';8066}8067}elsif(defined$file_name) {8068$title.=" -$file_name";8069$feed_type='history';8070}8071$title.="$feed_type";8072$title= esc_html($title);8073my$descr= git_get_project_description($project);8074if(defined$descr) {8075$descr= esc_html($descr);8076}else{8077$descr="$project".8078($formateq'rss'?'RSS':'Atom') .8079" feed";8080}8081my$owner= git_get_project_owner($project);8082$owner= esc_html($owner);80838084#header8085my$alt_url;8086if(defined$file_name) {8087$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);8088}elsif(defined$hash) {8089$alt_url= href(-full=>1, action=>"log", hash=>$hash);8090}else{8091$alt_url= href(-full=>1, action=>"summary");8092}8093print qq!<?xml version="1.0" encoding="utf-8"?>\n!;8094if($formateq'rss') {8095print<<XML;8096<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">8097<channel>8098XML8099print"<title>$title</title>\n".8100"<link>$alt_url</link>\n".8101"<description>$descr</description>\n".8102"<language>en</language>\n".8103# project owner is responsible for 'editorial' content8104"<managingEditor>$owner</managingEditor>\n";8105if(defined$logo||defined$favicon) {8106# prefer the logo to the favicon, since RSS8107# doesn't allow both8108my$img= esc_url($logo||$favicon);8109print"<image>\n".8110"<url>$img</url>\n".8111"<title>$title</title>\n".8112"<link>$alt_url</link>\n".8113"</image>\n";8114}8115if(%latest_date) {8116print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";8117print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";8118}8119print"<generator>gitweb v.$version/$git_version</generator>\n";8120}elsif($formateq'atom') {8121print<<XML;8122<feed xmlns="http://www.w3.org/2005/Atom">8123XML8124print"<title>$title</title>\n".8125"<subtitle>$descr</subtitle>\n".8126'<link rel="alternate" type="text/html" href="'.8127$alt_url.'" />'."\n".8128'<link rel="self" type="'.$content_type.'" href="'.8129$cgi->self_url() .'" />'."\n".8130"<id>". href(-full=>1) ."</id>\n".8131# use project owner for feed author8132"<author><name>$owner</name></author>\n";8133if(defined$favicon) {8134print"<icon>". esc_url($favicon) ."</icon>\n";8135}8136if(defined$logo) {8137# not twice as wide as tall: 72 x 27 pixels8138print"<logo>". esc_url($logo) ."</logo>\n";8139}8140if(!%latest_date) {8141# dummy date to keep the feed valid until commits trickle in:8142print"<updated>1970-01-01T00:00:00Z</updated>\n";8143}else{8144print"<updated>$latest_date{'iso-8601'}</updated>\n";8145}8146print"<generator version='$version/$git_version'>gitweb</generator>\n";8147}81488149# contents8150for(my$i=0;$i<=$#commitlist;$i++) {8151my%co= %{$commitlist[$i]};8152my$commit=$co{'id'};8153# we read 150, we always show 30 and the ones more recent than 48 hours8154if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {8155last;8156}8157my%cd= parse_date($co{'author_epoch'},$co{'author_tz'});81588159# get list of changed files8160open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,8161$co{'parent'} ||"--root",8162$co{'id'},"--", (defined$file_name?$file_name: ())8163ornext;8164my@difftree=map{chomp;$_} <$fd>;8165close$fd8166ornext;81678168# print element (entry, item)8169my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);8170if($formateq'rss') {8171print"<item>\n".8172"<title>". esc_html($co{'title'}) ."</title>\n".8173"<author>". esc_html($co{'author'}) ."</author>\n".8174"<pubDate>$cd{'rfc2822'}</pubDate>\n".8175"<guid isPermaLink=\"true\">$co_url</guid>\n".8176"<link>$co_url</link>\n".8177"<description>". esc_html($co{'title'}) ."</description>\n".8178"<content:encoded>".8179"<![CDATA[\n";8180}elsif($formateq'atom') {8181print"<entry>\n".8182"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".8183"<updated>$cd{'iso-8601'}</updated>\n".8184"<author>\n".8185" <name>". esc_html($co{'author_name'}) ."</name>\n";8186if($co{'author_email'}) {8187print" <email>". esc_html($co{'author_email'}) ."</email>\n";8188}8189print"</author>\n".8190# use committer for contributor8191"<contributor>\n".8192" <name>". esc_html($co{'committer_name'}) ."</name>\n";8193if($co{'committer_email'}) {8194print" <email>". esc_html($co{'committer_email'}) ."</email>\n";8195}8196print"</contributor>\n".8197"<published>$cd{'iso-8601'}</published>\n".8198"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".8199"<id>$co_url</id>\n".8200"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".8201"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";8202}8203my$comment=$co{'comment'};8204print"<pre>\n";8205foreachmy$line(@$comment) {8206$line= esc_html($line);8207print"$line\n";8208}8209print"</pre><ul>\n";8210foreachmy$difftree_line(@difftree) {8211my%difftree= parse_difftree_raw_line($difftree_line);8212next if!$difftree{'from_id'};82138214my$file=$difftree{'file'} ||$difftree{'to_file'};82158216print"<li>".8217"[".8218$cgi->a({-href => href(-full=>1, action=>"blobdiff",8219 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},8220 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},8221 file_name=>$file, file_parent=>$difftree{'from_file'}),8222-title =>"diff"},'D');8223if($have_blame) {8224print$cgi->a({-href => href(-full=>1, action=>"blame",8225 file_name=>$file, hash_base=>$commit),8226-title =>"blame"},'B');8227}8228# if this is not a feed of a file history8229if(!defined$file_name||$file_namene$file) {8230print$cgi->a({-href => href(-full=>1, action=>"history",8231 file_name=>$file, hash=>$commit),8232-title =>"history"},'H');8233}8234$file= esc_path($file);8235print"] ".8236"$file</li>\n";8237}8238if($formateq'rss') {8239print"</ul>]]>\n".8240"</content:encoded>\n".8241"</item>\n";8242}elsif($formateq'atom') {8243print"</ul>\n</div>\n".8244"</content>\n".8245"</entry>\n";8246}8247}82488249# end of feed8250if($formateq'rss') {8251print"</channel>\n</rss>\n";8252}elsif($formateq'atom') {8253print"</feed>\n";8254}8255}82568257sub git_rss {8258 git_feed('rss');8259}82608261sub git_atom {8262 git_feed('atom');8263}82648265sub git_opml {8266my@list= git_get_projects_list($project_filter,$strict_export);8267if(!@list) {8268 die_error(404,"No projects found");8269}82708271print$cgi->header(8272-type =>'text/xml',8273-charset =>'utf-8',8274-content_disposition =>'inline; filename="opml.xml"');82758276my$title= esc_html($site_name);8277my$filter=" within subdirectory ";8278if(defined$project_filter) {8279$filter.= esc_html($project_filter);8280}else{8281$filter="";8282}8283print<<XML;8284<?xml version="1.0" encoding="utf-8"?>8285<opml version="1.0">8286<head>8287 <title>$titleOPML Export$filter</title>8288</head>8289<body>8290<outline text="git RSS feeds">8291XML82928293foreachmy$pr(@list) {8294my%proj=%$pr;8295my$head= git_get_head_hash($proj{'path'});8296if(!defined$head) {8297next;8298}8299$git_dir="$projectroot/$proj{'path'}";8300my%co= parse_commit($head);8301if(!%co) {8302next;8303}83048305my$path= esc_html(chop_str($proj{'path'},25,5));8306my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);8307my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);8308print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";8309}8310print<<XML;8311</outline>8312</body>8313</opml>8314XML8315}