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# name of your site or organization to appear in page titles 89# replace this with something more descriptive for clearer bookmarks 90our$site_name="++GITWEB_SITENAME++" 91|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 92 93# html snippet to include in the <head> section of each page 94our$site_html_head_string="++GITWEB_SITE_HTML_HEAD_STRING++"; 95# filename of html text to include at top of each page 96our$site_header="++GITWEB_SITE_HEADER++"; 97# html text to include at home page 98our$home_text="++GITWEB_HOMETEXT++"; 99# filename of html text to include at bottom of each page 100our$site_footer="++GITWEB_SITE_FOOTER++"; 101 102# URI of stylesheets 103our@stylesheets= ("++GITWEB_CSS++"); 104# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 105our$stylesheet=undef; 106# URI of GIT logo (72x27 size) 107our$logo="++GITWEB_LOGO++"; 108# URI of GIT favicon, assumed to be image/png type 109our$favicon="++GITWEB_FAVICON++"; 110# URI of gitweb.js (JavaScript code for gitweb) 111our$javascript="++GITWEB_JS++"; 112 113# URI and label (title) of GIT logo link 114#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 115#our $logo_label = "git documentation"; 116our$logo_url="http://git-scm.com/"; 117our$logo_label="git homepage"; 118 119# source of projects list 120our$projects_list="++GITWEB_LIST++"; 121 122# the width (in characters) of the projects list "Description" column 123our$projects_list_description_width=25; 124 125# group projects by category on the projects list 126# (enabled if this variable evaluates to true) 127our$projects_list_group_categories=0; 128 129# default category if none specified 130# (leave the empty string for no category) 131our$project_list_default_category=""; 132 133# default order of projects list 134# valid values are none, project, descr, owner, and age 135our$default_projects_order="project"; 136 137# show repository only if this file exists 138# (only effective if this variable evaluates to true) 139our$export_ok="++GITWEB_EXPORT_OK++"; 140 141# don't generate age column on the projects list page 142our$omit_age_column=0; 143 144# don't generate information about owners of repositories 145our$omit_owner=0; 146 147# show repository only if this subroutine returns true 148# when given the path to the project, for example: 149# sub { return -e "$_[0]/git-daemon-export-ok"; } 150our$export_auth_hook=undef; 151 152# only allow viewing of repositories also shown on the overview page 153our$strict_export="++GITWEB_STRICT_EXPORT++"; 154 155# list of git base URLs used for URL to where fetch project from, 156# i.e. full URL is "$git_base_url/$project" 157our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 158 159# default blob_plain mimetype and default charset for text/plain blob 160our$default_blob_plain_mimetype='text/plain'; 161our$default_text_plain_charset=undef; 162 163# file to use for guessing MIME types before trying /etc/mime.types 164# (relative to the current git repository) 165our$mimetypes_file=undef; 166 167# assume this charset if line contains non-UTF-8 characters; 168# it should be valid encoding (see Encoding::Supported(3pm) for list), 169# for which encoding all byte sequences are valid, for example 170# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 171# could be even 'utf-8' for the old behavior) 172our$fallback_encoding='latin1'; 173 174# rename detection options for git-diff and git-diff-tree 175# - default is '-M', with the cost proportional to 176# (number of removed files) * (number of new files). 177# - more costly is '-C' (which implies '-M'), with the cost proportional to 178# (number of changed files + number of removed files) * (number of new files) 179# - even more costly is '-C', '--find-copies-harder' with cost 180# (number of files in the original tree) * (number of new files) 181# - one might want to include '-B' option, e.g. '-B', '-M' 182our@diff_opts= ('-M');# taken from git_commit 183 184# Disables features that would allow repository owners to inject script into 185# the gitweb domain. 186our$prevent_xss=0; 187 188# Path to the highlight executable to use (must be the one from 189# http://www.andre-simon.de due to assumptions about parameters and output). 190# Useful if highlight is not installed on your webserver's PATH. 191# [Default: highlight] 192our$highlight_bin="++HIGHLIGHT_BIN++"; 193 194# information about snapshot formats that gitweb is capable of serving 195our%known_snapshot_formats= ( 196# name => { 197# 'display' => display name, 198# 'type' => mime type, 199# 'suffix' => filename suffix, 200# 'format' => --format for git-archive, 201# 'compressor' => [compressor command and arguments] 202# (array reference, optional) 203# 'disabled' => boolean (optional)} 204# 205'tgz'=> { 206'display'=>'tar.gz', 207'type'=>'application/x-gzip', 208'suffix'=>'.tar.gz', 209'format'=>'tar', 210'compressor'=> ['gzip','-n']}, 211 212'tbz2'=> { 213'display'=>'tar.bz2', 214'type'=>'application/x-bzip2', 215'suffix'=>'.tar.bz2', 216'format'=>'tar', 217'compressor'=> ['bzip2']}, 218 219'txz'=> { 220'display'=>'tar.xz', 221'type'=>'application/x-xz', 222'suffix'=>'.tar.xz', 223'format'=>'tar', 224'compressor'=> ['xz'], 225'disabled'=>1}, 226 227'zip'=> { 228'display'=>'zip', 229'type'=>'application/x-zip', 230'suffix'=>'.zip', 231'format'=>'zip'}, 232); 233 234# Aliases so we understand old gitweb.snapshot values in repository 235# configuration. 236our%known_snapshot_format_aliases= ( 237'gzip'=>'tgz', 238'bzip2'=>'tbz2', 239'xz'=>'txz', 240 241# backward compatibility: legacy gitweb config support 242'x-gzip'=>undef,'gz'=>undef, 243'x-bzip2'=>undef,'bz2'=>undef, 244'x-zip'=>undef,''=>undef, 245); 246 247# Pixel sizes for icons and avatars. If the default font sizes or lineheights 248# are changed, it may be appropriate to change these values too via 249# $GITWEB_CONFIG. 250our%avatar_size= ( 251'default'=>16, 252'double'=>32 253); 254 255# Used to set the maximum load that we will still respond to gitweb queries. 256# If server load exceed this value then return "503 server busy" error. 257# If gitweb cannot determined server load, it is taken to be 0. 258# Leave it undefined (or set to 'undef') to turn off load checking. 259our$maxload=300; 260 261# configuration for 'highlight' (http://www.andre-simon.de/) 262# match by basename 263our%highlight_basename= ( 264#'Program' => 'py', 265#'Library' => 'py', 266'SConstruct'=>'py',# SCons equivalent of Makefile 267'Makefile'=>'make', 268); 269# match by extension 270our%highlight_ext= ( 271# main extensions, defining name of syntax; 272# see files in /usr/share/highlight/langDefs/ directory 273map{$_=>$_} 274qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl sql make), 275# alternate extensions, see /etc/highlight/filetypes.conf 276'h'=>'c', 277map{$_=>'sh'}qw(bash zsh ksh), 278map{$_=>'cpp'}qw(cxx c++ cc), 279map{$_=>'php'}qw(php3 php4 php5 phps), 280map{$_=>'pl'}qw(perl pm),# perhaps also 'cgi' 281map{$_=>'make'}qw(mak mk), 282map{$_=>'xml'}qw(xhtml html htm), 283); 284 285# You define site-wide feature defaults here; override them with 286# $GITWEB_CONFIG as necessary. 287our%feature= ( 288# feature => { 289# 'sub' => feature-sub (subroutine), 290# 'override' => allow-override (boolean), 291# 'default' => [ default options...] (array reference)} 292# 293# if feature is overridable (it means that allow-override has true value), 294# then feature-sub will be called with default options as parameters; 295# return value of feature-sub indicates if to enable specified feature 296# 297# if there is no 'sub' key (no feature-sub), then feature cannot be 298# overridden 299# 300# use gitweb_get_feature(<feature>) to retrieve the <feature> value 301# (an array) or gitweb_check_feature(<feature>) to check if <feature> 302# is enabled 303 304# Enable the 'blame' blob view, showing the last commit that modified 305# each line in the file. This can be very CPU-intensive. 306 307# To enable system wide have in $GITWEB_CONFIG 308# $feature{'blame'}{'default'} = [1]; 309# To have project specific config enable override in $GITWEB_CONFIG 310# $feature{'blame'}{'override'} = 1; 311# and in project config gitweb.blame = 0|1; 312'blame'=> { 313'sub'=>sub{ feature_bool('blame',@_) }, 314'override'=>0, 315'default'=> [0]}, 316 317# Enable the 'snapshot' link, providing a compressed archive of any 318# tree. This can potentially generate high traffic if you have large 319# project. 320 321# Value is a list of formats defined in %known_snapshot_formats that 322# you wish to offer. 323# To disable system wide have in $GITWEB_CONFIG 324# $feature{'snapshot'}{'default'} = []; 325# To have project specific config enable override in $GITWEB_CONFIG 326# $feature{'snapshot'}{'override'} = 1; 327# and in project config, a comma-separated list of formats or "none" 328# to disable. Example: gitweb.snapshot = tbz2,zip; 329'snapshot'=> { 330'sub'=> \&feature_snapshot, 331'override'=>0, 332'default'=> ['tgz']}, 333 334# Enable text search, which will list the commits which match author, 335# committer or commit text to a given string. Enabled by default. 336# Project specific override is not supported. 337# 338# Note that this controls all search features, which means that if 339# it is disabled, then 'grep' and 'pickaxe' search would also be 340# disabled. 341'search'=> { 342'override'=>0, 343'default'=> [1]}, 344 345# Enable grep search, which will list the files in currently selected 346# tree containing the given string. Enabled by default. This can be 347# potentially CPU-intensive, of course. 348# Note that you need to have 'search' feature enabled too. 349 350# To enable system wide have in $GITWEB_CONFIG 351# $feature{'grep'}{'default'} = [1]; 352# To have project specific config enable override in $GITWEB_CONFIG 353# $feature{'grep'}{'override'} = 1; 354# and in project config gitweb.grep = 0|1; 355'grep'=> { 356'sub'=>sub{ feature_bool('grep',@_) }, 357'override'=>0, 358'default'=> [1]}, 359 360# Enable the pickaxe search, which will list the commits that modified 361# a given string in a file. This can be practical and quite faster 362# alternative to 'blame', but still potentially CPU-intensive. 363# Note that you need to have 'search' feature enabled too. 364 365# To enable system wide have in $GITWEB_CONFIG 366# $feature{'pickaxe'}{'default'} = [1]; 367# To have project specific config enable override in $GITWEB_CONFIG 368# $feature{'pickaxe'}{'override'} = 1; 369# and in project config gitweb.pickaxe = 0|1; 370'pickaxe'=> { 371'sub'=>sub{ feature_bool('pickaxe',@_) }, 372'override'=>0, 373'default'=> [1]}, 374 375# Enable showing size of blobs in a 'tree' view, in a separate 376# column, similar to what 'ls -l' does. This cost a bit of IO. 377 378# To disable system wide have in $GITWEB_CONFIG 379# $feature{'show-sizes'}{'default'} = [0]; 380# To have project specific config enable override in $GITWEB_CONFIG 381# $feature{'show-sizes'}{'override'} = 1; 382# and in project config gitweb.showsizes = 0|1; 383'show-sizes'=> { 384'sub'=>sub{ feature_bool('showsizes',@_) }, 385'override'=>0, 386'default'=> [1]}, 387 388# Make gitweb use an alternative format of the URLs which can be 389# more readable and natural-looking: project name is embedded 390# directly in the path and the query string contains other 391# auxiliary information. All gitweb installations recognize 392# URL in either format; this configures in which formats gitweb 393# generates links. 394 395# To enable system wide have in $GITWEB_CONFIG 396# $feature{'pathinfo'}{'default'} = [1]; 397# Project specific override is not supported. 398 399# Note that you will need to change the default location of CSS, 400# favicon, logo and possibly other files to an absolute URL. Also, 401# if gitweb.cgi serves as your indexfile, you will need to force 402# $my_uri to contain the script name in your $GITWEB_CONFIG. 403'pathinfo'=> { 404'override'=>0, 405'default'=> [0]}, 406 407# Make gitweb consider projects in project root subdirectories 408# to be forks of existing projects. Given project $projname.git, 409# projects matching $projname/*.git will not be shown in the main 410# projects list, instead a '+' mark will be added to $projname 411# there and a 'forks' view will be enabled for the project, listing 412# all the forks. If project list is taken from a file, forks have 413# to be listed after the main project. 414 415# To enable system wide have in $GITWEB_CONFIG 416# $feature{'forks'}{'default'} = [1]; 417# Project specific override is not supported. 418'forks'=> { 419'override'=>0, 420'default'=> [0]}, 421 422# Insert custom links to the action bar of all project pages. 423# This enables you mainly to link to third-party scripts integrating 424# into gitweb; e.g. git-browser for graphical history representation 425# or custom web-based repository administration interface. 426 427# The 'default' value consists of a list of triplets in the form 428# (label, link, position) where position is the label after which 429# to insert the link and link is a format string where %n expands 430# to the project name, %f to the project path within the filesystem, 431# %h to the current hash (h gitweb parameter) and %b to the current 432# hash base (hb gitweb parameter); %% expands to %. 433 434# To enable system wide have in $GITWEB_CONFIG e.g. 435# $feature{'actions'}{'default'} = [('graphiclog', 436# '/git-browser/by-commit.html?r=%n', 'summary')]; 437# Project specific override is not supported. 438'actions'=> { 439'override'=>0, 440'default'=> []}, 441 442# Allow gitweb scan project content tags of project repository, 443# and display the popular Web 2.0-ish "tag cloud" near the projects 444# list. Note that this is something COMPLETELY different from the 445# normal Git tags. 446 447# gitweb by itself can show existing tags, but it does not handle 448# tagging itself; you need to do it externally, outside gitweb. 449# The format is described in git_get_project_ctags() subroutine. 450# You may want to install the HTML::TagCloud Perl module to get 451# a pretty tag cloud instead of just a list of tags. 452 453# To enable system wide have in $GITWEB_CONFIG 454# $feature{'ctags'}{'default'} = [1]; 455# Project specific override is not supported. 456 457# In the future whether ctags editing is enabled might depend 458# on the value, but using 1 should always mean no editing of ctags. 459'ctags'=> { 460'override'=>0, 461'default'=> [0]}, 462 463# The maximum number of patches in a patchset generated in patch 464# view. Set this to 0 or undef to disable patch view, or to a 465# negative number to remove any limit. 466 467# To disable system wide have in $GITWEB_CONFIG 468# $feature{'patches'}{'default'} = [0]; 469# To have project specific config enable override in $GITWEB_CONFIG 470# $feature{'patches'}{'override'} = 1; 471# and in project config gitweb.patches = 0|n; 472# where n is the maximum number of patches allowed in a patchset. 473'patches'=> { 474'sub'=> \&feature_patches, 475'override'=>0, 476'default'=> [16]}, 477 478# Avatar support. When this feature is enabled, views such as 479# shortlog or commit will display an avatar associated with 480# the email of the committer(s) and/or author(s). 481 482# Currently available providers are gravatar and picon. 483# If an unknown provider is specified, the feature is disabled. 484 485# Gravatar depends on Digest::MD5. 486# Picon currently relies on the indiana.edu database. 487 488# To enable system wide have in $GITWEB_CONFIG 489# $feature{'avatar'}{'default'} = ['<provider>']; 490# where <provider> is either gravatar or picon. 491# To have project specific config enable override in $GITWEB_CONFIG 492# $feature{'avatar'}{'override'} = 1; 493# and in project config gitweb.avatar = <provider>; 494'avatar'=> { 495'sub'=> \&feature_avatar, 496'override'=>0, 497'default'=> ['']}, 498 499# Enable displaying how much time and how many git commands 500# it took to generate and display page. Disabled by default. 501# Project specific override is not supported. 502'timed'=> { 503'override'=>0, 504'default'=> [0]}, 505 506# Enable turning some links into links to actions which require 507# JavaScript to run (like 'blame_incremental'). Not enabled by 508# default. Project specific override is currently not supported. 509'javascript-actions'=> { 510'override'=>0, 511'default'=> [0]}, 512 513# Enable and configure ability to change common timezone for dates 514# in gitweb output via JavaScript. Enabled by default. 515# Project specific override is not supported. 516'javascript-timezone'=> { 517'override'=>0, 518'default'=> [ 519'local',# default timezone: 'utc', 'local', or '(-|+)HHMM' format, 520# or undef to turn off this feature 521'gitweb_tz',# name of cookie where to store selected timezone 522'datetime',# CSS class used to mark up dates for manipulation 523]}, 524 525# Syntax highlighting support. This is based on Daniel Svensson's 526# and Sham Chukoury's work in gitweb-xmms2.git. 527# It requires the 'highlight' program present in $PATH, 528# and therefore is disabled by default. 529 530# To enable system wide have in $GITWEB_CONFIG 531# $feature{'highlight'}{'default'} = [1]; 532 533'highlight'=> { 534'sub'=>sub{ feature_bool('highlight',@_) }, 535'override'=>0, 536'default'=> [0]}, 537 538# Enable displaying of remote heads in the heads list 539 540# To enable system wide have in $GITWEB_CONFIG 541# $feature{'remote_heads'}{'default'} = [1]; 542# To have project specific config enable override in $GITWEB_CONFIG 543# $feature{'remote_heads'}{'override'} = 1; 544# and in project config gitweb.remoteheads = 0|1; 545'remote_heads'=> { 546'sub'=>sub{ feature_bool('remote_heads',@_) }, 547'override'=>0, 548'default'=> [0]}, 549); 550 551sub gitweb_get_feature { 552my($name) =@_; 553return unlessexists$feature{$name}; 554my($sub,$override,@defaults) = ( 555$feature{$name}{'sub'}, 556$feature{$name}{'override'}, 557@{$feature{$name}{'default'}}); 558# project specific override is possible only if we have project 559our$git_dir;# global variable, declared later 560if(!$override|| !defined$git_dir) { 561return@defaults; 562} 563if(!defined$sub) { 564warn"feature$nameis not overridable"; 565return@defaults; 566} 567return$sub->(@defaults); 568} 569 570# A wrapper to check if a given feature is enabled. 571# With this, you can say 572# 573# my $bool_feat = gitweb_check_feature('bool_feat'); 574# gitweb_check_feature('bool_feat') or somecode; 575# 576# instead of 577# 578# my ($bool_feat) = gitweb_get_feature('bool_feat'); 579# (gitweb_get_feature('bool_feat'))[0] or somecode; 580# 581sub gitweb_check_feature { 582return(gitweb_get_feature(@_))[0]; 583} 584 585 586sub feature_bool { 587my$key=shift; 588my($val) = git_get_project_config($key,'--bool'); 589 590if(!defined$val) { 591return($_[0]); 592}elsif($valeq'true') { 593return(1); 594}elsif($valeq'false') { 595return(0); 596} 597} 598 599sub feature_snapshot { 600my(@fmts) =@_; 601 602my($val) = git_get_project_config('snapshot'); 603 604if($val) { 605@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 606} 607 608return@fmts; 609} 610 611sub feature_patches { 612my@val= (git_get_project_config('patches','--int')); 613 614if(@val) { 615return@val; 616} 617 618return($_[0]); 619} 620 621sub feature_avatar { 622my@val= (git_get_project_config('avatar')); 623 624return@val?@val:@_; 625} 626 627# checking HEAD file with -e is fragile if the repository was 628# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 629# and then pruned. 630sub check_head_link { 631my($dir) =@_; 632my$headfile="$dir/HEAD"; 633return((-e $headfile) || 634(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 635} 636 637sub check_export_ok { 638my($dir) =@_; 639return(check_head_link($dir) && 640(!$export_ok|| -e "$dir/$export_ok") && 641(!$export_auth_hook||$export_auth_hook->($dir))); 642} 643 644# process alternate names for backward compatibility 645# filter out unsupported (unknown) snapshot formats 646sub filter_snapshot_fmts { 647my@fmts=@_; 648 649@fmts=map{ 650exists$known_snapshot_format_aliases{$_} ? 651$known_snapshot_format_aliases{$_} :$_}@fmts; 652@fmts=grep{ 653exists$known_snapshot_formats{$_} && 654!$known_snapshot_formats{$_}{'disabled'}}@fmts; 655} 656 657# If it is set to code reference, it is code that it is to be run once per 658# request, allowing updating configurations that change with each request, 659# while running other code in config file only once. 660# 661# Otherwise, if it is false then gitweb would process config file only once; 662# if it is true then gitweb config would be run for each request. 663our$per_request_config=1; 664 665# read and parse gitweb config file given by its parameter. 666# returns true on success, false on recoverable error, allowing 667# to chain this subroutine, using first file that exists. 668# dies on errors during parsing config file, as it is unrecoverable. 669sub read_config_file { 670my$filename=shift; 671return unlessdefined$filename; 672# die if there are errors parsing config file 673if(-e $filename) { 674do$filename; 675die$@if$@; 676return1; 677} 678return; 679} 680 681our($GITWEB_CONFIG,$GITWEB_CONFIG_SYSTEM,$GITWEB_CONFIG_COMMON); 682sub evaluate_gitweb_config { 683our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 684our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 685our$GITWEB_CONFIG_COMMON=$ENV{'GITWEB_CONFIG_COMMON'} ||"++GITWEB_CONFIG_COMMON++"; 686 687# Protect agains duplications of file names, to not read config twice. 688# Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so 689# there possibility of duplication of filename there doesn't matter. 690$GITWEB_CONFIG=""if($GITWEB_CONFIGeq$GITWEB_CONFIG_COMMON); 691$GITWEB_CONFIG_SYSTEM=""if($GITWEB_CONFIG_SYSTEMeq$GITWEB_CONFIG_COMMON); 692 693# Common system-wide settings for convenience. 694# Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM. 695 read_config_file($GITWEB_CONFIG_COMMON); 696 697# Use first config file that exists. This means use the per-instance 698# GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG. 699 read_config_file($GITWEB_CONFIG)andreturn; 700 read_config_file($GITWEB_CONFIG_SYSTEM); 701} 702 703# Get loadavg of system, to compare against $maxload. 704# Currently it requires '/proc/loadavg' present to get loadavg; 705# if it is not present it returns 0, which means no load checking. 706sub get_loadavg { 707if( -e '/proc/loadavg'){ 708open my$fd,'<','/proc/loadavg' 709orreturn0; 710my@load=split(/\s+/,scalar<$fd>); 711close$fd; 712 713# The first three columns measure CPU and IO utilization of the last one, 714# five, and 10 minute periods. The fourth column shows the number of 715# currently running processes and the total number of processes in the m/n 716# format. The last column displays the last process ID used. 717return$load[0] ||0; 718} 719# additional checks for load average should go here for things that don't export 720# /proc/loadavg 721 722return0; 723} 724 725# version of the core git binary 726our$git_version; 727sub evaluate_git_version { 728our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 729$number_of_git_cmds++; 730} 731 732sub check_loadavg { 733if(defined$maxload&& get_loadavg() >$maxload) { 734 die_error(503,"The load average on the server is too high"); 735} 736} 737 738# ====================================================================== 739# input validation and dispatch 740 741# input parameters can be collected from a variety of sources (presently, CGI 742# and PATH_INFO), so we define an %input_params hash that collects them all 743# together during validation: this allows subsequent uses (e.g. href()) to be 744# agnostic of the parameter origin 745 746our%input_params= (); 747 748# input parameters are stored with the long parameter name as key. This will 749# also be used in the href subroutine to convert parameters to their CGI 750# equivalent, and since the href() usage is the most frequent one, we store 751# the name -> CGI key mapping here, instead of the reverse. 752# 753# XXX: Warning: If you touch this, check the search form for updating, 754# too. 755 756our@cgi_param_mapping= ( 757 project =>"p", 758 action =>"a", 759 file_name =>"f", 760 file_parent =>"fp", 761 hash =>"h", 762 hash_parent =>"hp", 763 hash_base =>"hb", 764 hash_parent_base =>"hpb", 765 page =>"pg", 766 order =>"o", 767 searchtext =>"s", 768 searchtype =>"st", 769 snapshot_format =>"sf", 770 extra_options =>"opt", 771 search_use_regexp =>"sr", 772 ctag =>"by_tag", 773 diff_style =>"ds", 774 project_filter =>"pf", 775# this must be last entry (for manipulation from JavaScript) 776 javascript =>"js" 777); 778our%cgi_param_mapping=@cgi_param_mapping; 779 780# we will also need to know the possible actions, for validation 781our%actions= ( 782"blame"=> \&git_blame, 783"blame_incremental"=> \&git_blame_incremental, 784"blame_data"=> \&git_blame_data, 785"blobdiff"=> \&git_blobdiff, 786"blobdiff_plain"=> \&git_blobdiff_plain, 787"blob"=> \&git_blob, 788"blob_plain"=> \&git_blob_plain, 789"commitdiff"=> \&git_commitdiff, 790"commitdiff_plain"=> \&git_commitdiff_plain, 791"commit"=> \&git_commit, 792"forks"=> \&git_forks, 793"heads"=> \&git_heads, 794"history"=> \&git_history, 795"log"=> \&git_log, 796"patch"=> \&git_patch, 797"patches"=> \&git_patches, 798"remotes"=> \&git_remotes, 799"rss"=> \&git_rss, 800"atom"=> \&git_atom, 801"search"=> \&git_search, 802"search_help"=> \&git_search_help, 803"shortlog"=> \&git_shortlog, 804"summary"=> \&git_summary, 805"tag"=> \&git_tag, 806"tags"=> \&git_tags, 807"tree"=> \&git_tree, 808"snapshot"=> \&git_snapshot, 809"object"=> \&git_object, 810# those below don't need $project 811"opml"=> \&git_opml, 812"project_list"=> \&git_project_list, 813"project_index"=> \&git_project_index, 814); 815 816# finally, we have the hash of allowed extra_options for the commands that 817# allow them 818our%allowed_options= ( 819"--no-merges"=> [qw(rss atom log shortlog history)], 820); 821 822# fill %input_params with the CGI parameters. All values except for 'opt' 823# should be single values, but opt can be an array. We should probably 824# build an array of parameters that can be multi-valued, but since for the time 825# being it's only this one, we just single it out 826sub evaluate_query_params { 827our$cgi; 828 829while(my($name,$symbol) =each%cgi_param_mapping) { 830if($symboleq'opt') { 831$input_params{$name} = [map{ decode_utf8($_) }$cgi->param($symbol) ]; 832}else{ 833$input_params{$name} = decode_utf8($cgi->param($symbol)); 834} 835} 836} 837 838# now read PATH_INFO and update the parameter list for missing parameters 839sub evaluate_path_info { 840return ifdefined$input_params{'project'}; 841return if!$path_info; 842$path_info=~ s,^/+,,; 843return if!$path_info; 844 845# find which part of PATH_INFO is project 846my$project=$path_info; 847$project=~ s,/+$,,; 848while($project&& !check_head_link("$projectroot/$project")) { 849$project=~ s,/*[^/]*$,,; 850} 851return unless$project; 852$input_params{'project'} =$project; 853 854# do not change any parameters if an action is given using the query string 855return if$input_params{'action'}; 856$path_info=~ s,^\Q$project\E/*,,; 857 858# next, check if we have an action 859my$action=$path_info; 860$action=~ s,/.*$,,; 861if(exists$actions{$action}) { 862$path_info=~ s,^$action/*,,; 863$input_params{'action'} =$action; 864} 865 866# list of actions that want hash_base instead of hash, but can have no 867# pathname (f) parameter 868my@wants_base= ( 869'tree', 870'history', 871); 872 873# we want to catch, among others 874# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 875my($parentrefname,$parentpathname,$refname,$pathname) = 876($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/); 877 878# first, analyze the 'current' part 879if(defined$pathname) { 880# we got "branch:filename" or "branch:dir/" 881# we could use git_get_type(branch:pathname), but: 882# - it needs $git_dir 883# - it does a git() call 884# - the convention of terminating directories with a slash 885# makes it superfluous 886# - embedding the action in the PATH_INFO would make it even 887# more superfluous 888$pathname=~ s,^/+,,; 889if(!$pathname||substr($pathname, -1)eq"/") { 890$input_params{'action'} ||="tree"; 891$pathname=~ s,/$,,; 892}else{ 893# the default action depends on whether we had parent info 894# or not 895if($parentrefname) { 896$input_params{'action'} ||="blobdiff_plain"; 897}else{ 898$input_params{'action'} ||="blob_plain"; 899} 900} 901$input_params{'hash_base'} ||=$refname; 902$input_params{'file_name'} ||=$pathname; 903}elsif(defined$refname) { 904# we got "branch". In this case we have to choose if we have to 905# set hash or hash_base. 906# 907# Most of the actions without a pathname only want hash to be 908# set, except for the ones specified in @wants_base that want 909# hash_base instead. It should also be noted that hand-crafted 910# links having 'history' as an action and no pathname or hash 911# set will fail, but that happens regardless of PATH_INFO. 912if(defined$parentrefname) { 913# if there is parent let the default be 'shortlog' action 914# (for http://git.example.com/repo.git/A..B links); if there 915# is no parent, dispatch will detect type of object and set 916# action appropriately if required (if action is not set) 917$input_params{'action'} ||="shortlog"; 918} 919if($input_params{'action'} && 920grep{$_eq$input_params{'action'} }@wants_base) { 921$input_params{'hash_base'} ||=$refname; 922}else{ 923$input_params{'hash'} ||=$refname; 924} 925} 926 927# next, handle the 'parent' part, if present 928if(defined$parentrefname) { 929# a missing pathspec defaults to the 'current' filename, allowing e.g. 930# someproject/blobdiff/oldrev..newrev:/filename 931if($parentpathname) { 932$parentpathname=~ s,^/+,,; 933$parentpathname=~ s,/$,,; 934$input_params{'file_parent'} ||=$parentpathname; 935}else{ 936$input_params{'file_parent'} ||=$input_params{'file_name'}; 937} 938# we assume that hash_parent_base is wanted if a path was specified, 939# or if the action wants hash_base instead of hash 940if(defined$input_params{'file_parent'} || 941grep{$_eq$input_params{'action'} }@wants_base) { 942$input_params{'hash_parent_base'} ||=$parentrefname; 943}else{ 944$input_params{'hash_parent'} ||=$parentrefname; 945} 946} 947 948# for the snapshot action, we allow URLs in the form 949# $project/snapshot/$hash.ext 950# where .ext determines the snapshot and gets removed from the 951# passed $refname to provide the $hash. 952# 953# To be able to tell that $refname includes the format extension, we 954# require the following two conditions to be satisfied: 955# - the hash input parameter MUST have been set from the $refname part 956# of the URL (i.e. they must be equal) 957# - the snapshot format MUST NOT have been defined already (e.g. from 958# CGI parameter sf) 959# It's also useless to try any matching unless $refname has a dot, 960# so we check for that too 961if(defined$input_params{'action'} && 962$input_params{'action'}eq'snapshot'&& 963defined$refname&&index($refname,'.') != -1&& 964$refnameeq$input_params{'hash'} && 965!defined$input_params{'snapshot_format'}) { 966# We loop over the known snapshot formats, checking for 967# extensions. Allowed extensions are both the defined suffix 968# (which includes the initial dot already) and the snapshot 969# format key itself, with a prepended dot 970while(my($fmt,$opt) =each%known_snapshot_formats) { 971my$hash=$refname; 972unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 973next; 974} 975my$sfx=$1; 976# a valid suffix was found, so set the snapshot format 977# and reset the hash parameter 978$input_params{'snapshot_format'} =$fmt; 979$input_params{'hash'} =$hash; 980# we also set the format suffix to the one requested 981# in the URL: this way a request for e.g. .tgz returns 982# a .tgz instead of a .tar.gz 983$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 984last; 985} 986} 987} 988 989our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 990$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 991$searchtext,$search_regexp,$project_filter); 992sub evaluate_and_validate_params { 993our$action=$input_params{'action'}; 994if(defined$action) { 995if(!validate_action($action)) { 996 die_error(400,"Invalid action parameter"); 997} 998} 9991000# parameters which are pathnames1001our$project=$input_params{'project'};1002if(defined$project) {1003if(!validate_project($project)) {1004undef$project;1005 die_error(404,"No such project");1006}1007}10081009our$project_filter=$input_params{'project_filter'};1010if(defined$project_filter) {1011if(!validate_pathname($project_filter)) {1012 die_error(404,"Invalid project_filter parameter");1013}1014}10151016our$file_name=$input_params{'file_name'};1017if(defined$file_name) {1018if(!validate_pathname($file_name)) {1019 die_error(400,"Invalid file parameter");1020}1021}10221023our$file_parent=$input_params{'file_parent'};1024if(defined$file_parent) {1025if(!validate_pathname($file_parent)) {1026 die_error(400,"Invalid file parent parameter");1027}1028}10291030# parameters which are refnames1031our$hash=$input_params{'hash'};1032if(defined$hash) {1033if(!validate_refname($hash)) {1034 die_error(400,"Invalid hash parameter");1035}1036}10371038our$hash_parent=$input_params{'hash_parent'};1039if(defined$hash_parent) {1040if(!validate_refname($hash_parent)) {1041 die_error(400,"Invalid hash parent parameter");1042}1043}10441045our$hash_base=$input_params{'hash_base'};1046if(defined$hash_base) {1047if(!validate_refname($hash_base)) {1048 die_error(400,"Invalid hash base parameter");1049}1050}10511052our@extra_options= @{$input_params{'extra_options'}};1053# @extra_options is always defined, since it can only be (currently) set from1054# CGI, and $cgi->param() returns the empty array in array context if the param1055# is not set1056foreachmy$opt(@extra_options) {1057if(not exists$allowed_options{$opt}) {1058 die_error(400,"Invalid option parameter");1059}1060if(not grep(/^$action$/, @{$allowed_options{$opt}})) {1061 die_error(400,"Invalid option parameter for this action");1062}1063}10641065our$hash_parent_base=$input_params{'hash_parent_base'};1066if(defined$hash_parent_base) {1067if(!validate_refname($hash_parent_base)) {1068 die_error(400,"Invalid hash parent base parameter");1069}1070}10711072# other parameters1073our$page=$input_params{'page'};1074if(defined$page) {1075if($page=~m/[^0-9]/) {1076 die_error(400,"Invalid page parameter");1077}1078}10791080our$searchtype=$input_params{'searchtype'};1081if(defined$searchtype) {1082if($searchtype=~m/[^a-z]/) {1083 die_error(400,"Invalid searchtype parameter");1084}1085}10861087our$search_use_regexp=$input_params{'search_use_regexp'};10881089our$searchtext=$input_params{'searchtext'};1090our$search_regexp;1091if(defined$searchtext) {1092if(length($searchtext) <2) {1093 die_error(403,"At least two characters are required for search parameter");1094}1095if($search_use_regexp) {1096$search_regexp=$searchtext;1097if(!eval{qr/$search_regexp/;1; }) {1098(my$error=$@) =~s/ at \S+ line \d+.*\n?//;1099 die_error(400,"Invalid search regexp '$search_regexp'",1100 esc_html($error));1101}1102}else{1103$search_regexp=quotemeta$searchtext;1104}1105}1106}11071108# path to the current git repository1109our$git_dir;1110sub evaluate_git_dir {1111our$git_dir="$projectroot/$project"if$project;1112}11131114our(@snapshot_fmts,$git_avatar);1115sub configure_gitweb_features {1116# list of supported snapshot formats1117our@snapshot_fmts= gitweb_get_feature('snapshot');1118@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);11191120# check that the avatar feature is set to a known provider name,1121# and for each provider check if the dependencies are satisfied.1122# if the provider name is invalid or the dependencies are not met,1123# reset $git_avatar to the empty string.1124our($git_avatar) = gitweb_get_feature('avatar');1125if($git_avatareq'gravatar') {1126$git_avatar=''unless(eval{require Digest::MD5;1; });1127}elsif($git_avatareq'picon') {1128# no dependencies1129}else{1130$git_avatar='';1131}1132}11331134# custom error handler: 'die <message>' is Internal Server Error1135sub handle_errors_html {1136my$msg=shift;# it is already HTML escaped11371138# to avoid infinite loop where error occurs in die_error,1139# change handler to default handler, disabling handle_errors_html1140 set_message("Error occured when inside die_error:\n$msg");11411142# you cannot jump out of die_error when called as error handler;1143# the subroutine set via CGI::Carp::set_message is called _after_1144# HTTP headers are already written, so it cannot write them itself1145 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1);1146}1147set_message(\&handle_errors_html);11481149# dispatch1150sub dispatch {1151if(!defined$action) {1152if(defined$hash) {1153$action= git_get_type($hash);1154$actionor die_error(404,"Object does not exist");1155}elsif(defined$hash_base&&defined$file_name) {1156$action= git_get_type("$hash_base:$file_name");1157$actionor die_error(404,"File or directory does not exist");1158}elsif(defined$project) {1159$action='summary';1160}else{1161$action='project_list';1162}1163}1164if(!defined($actions{$action})) {1165 die_error(400,"Unknown action");1166}1167if($action!~m/^(?:opml|project_list|project_index)$/&&1168!$project) {1169 die_error(400,"Project needed");1170}1171$actions{$action}->();1172}11731174sub reset_timer {1175our$t0= [ gettimeofday() ]1176ifdefined$t0;1177our$number_of_git_cmds=0;1178}11791180our$first_request=1;1181sub run_request {1182 reset_timer();11831184 evaluate_uri();1185if($first_request) {1186 evaluate_gitweb_config();1187 evaluate_git_version();1188}1189if($per_request_config) {1190if(ref($per_request_config)eq'CODE') {1191$per_request_config->();1192}elsif(!$first_request) {1193 evaluate_gitweb_config();1194}1195}1196 check_loadavg();11971198# $projectroot and $projects_list might be set in gitweb config file1199$projects_list||=$projectroot;12001201 evaluate_query_params();1202 evaluate_path_info();1203 evaluate_and_validate_params();1204 evaluate_git_dir();12051206 configure_gitweb_features();12071208 dispatch();1209}12101211our$is_last_request=sub{1};1212our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1213our$CGI='CGI';1214our$cgi;1215sub configure_as_fcgi {1216require CGI::Fast;1217our$CGI='CGI::Fast';12181219my$request_number=0;1220# let each child service 100 requests1221our$is_last_request=sub{ ++$request_number>100};1222}1223sub evaluate_argv {1224my$script_name=$ENV{'SCRIPT_NAME'} ||$ENV{'SCRIPT_FILENAME'} || __FILE__;1225 configure_as_fcgi()1226if$script_name=~/\.fcgi$/;12271228return unless(@ARGV);12291230require Getopt::Long;1231 Getopt::Long::GetOptions(1232'fastcgi|fcgi|f'=> \&configure_as_fcgi,1233'nproc|n=i'=>sub{1234my($arg,$val) =@_;1235return unlesseval{require FCGI::ProcManager;1; };1236my$proc_manager= FCGI::ProcManager->new({1237 n_processes =>$val,1238});1239our$pre_listen_hook=sub{$proc_manager->pm_manage() };1240our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1241our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1242},1243);1244}12451246sub run {1247 evaluate_argv();12481249$first_request=1;1250$pre_listen_hook->()1251if$pre_listen_hook;12521253 REQUEST:1254while($cgi=$CGI->new()) {1255$pre_dispatch_hook->()1256if$pre_dispatch_hook;12571258 run_request();12591260$post_dispatch_hook->()1261if$post_dispatch_hook;1262$first_request=0;12631264last REQUEST if($is_last_request->());1265}12661267 DONE_GITWEB:12681;1269}12701271run();12721273if(defined caller) {1274# wrapped in a subroutine processing requests,1275# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1276return;1277}else{1278# pure CGI script, serving single request1279exit;1280}12811282## ======================================================================1283## action links12841285# possible values of extra options1286# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1287# -replay => 1 - start from a current view (replay with modifications)1288# -path_info => 0|1 - don't use/use path_info URL (if possible)1289# -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone1290sub href {1291my%params=@_;1292# default is to use -absolute url() i.e. $my_uri1293my$href=$params{-full} ?$my_url:$my_uri;12941295# implicit -replay, must be first of implicit params1296$params{-replay} =1if(keys%params==1&&$params{-anchor});12971298$params{'project'} =$projectunlessexists$params{'project'};12991300if($params{-replay}) {1301while(my($name,$symbol) =each%cgi_param_mapping) {1302if(!exists$params{$name}) {1303$params{$name} =$input_params{$name};1304}1305}1306}13071308my$use_pathinfo= gitweb_check_feature('pathinfo');1309if(defined$params{'project'} &&1310(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1311# try to put as many parameters as possible in PATH_INFO:1312# - project name1313# - action1314# - hash_parent or hash_parent_base:/file_parent1315# - hash or hash_base:/filename1316# - the snapshot_format as an appropriate suffix13171318# When the script is the root DirectoryIndex for the domain,1319# $href here would be something like http://gitweb.example.com/1320# Thus, we strip any trailing / from $href, to spare us double1321# slashes in the final URL1322$href=~ s,/$,,;13231324# Then add the project name, if present1325$href.="/".esc_path_info($params{'project'});1326delete$params{'project'};13271328# since we destructively absorb parameters, we keep this1329# boolean that remembers if we're handling a snapshot1330my$is_snapshot=$params{'action'}eq'snapshot';13311332# Summary just uses the project path URL, any other action is1333# added to the URL1334if(defined$params{'action'}) {1335$href.="/".esc_path_info($params{'action'})1336unless$params{'action'}eq'summary';1337delete$params{'action'};1338}13391340# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1341# stripping nonexistent or useless pieces1342$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1343||$params{'hash_parent'} ||$params{'hash'});1344if(defined$params{'hash_base'}) {1345if(defined$params{'hash_parent_base'}) {1346$href.= esc_path_info($params{'hash_parent_base'});1347# skip the file_parent if it's the same as the file_name1348if(defined$params{'file_parent'}) {1349if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1350delete$params{'file_parent'};1351}elsif($params{'file_parent'} !~/\.\./) {1352$href.=":/".esc_path_info($params{'file_parent'});1353delete$params{'file_parent'};1354}1355}1356$href.="..";1357delete$params{'hash_parent'};1358delete$params{'hash_parent_base'};1359}elsif(defined$params{'hash_parent'}) {1360$href.= esc_path_info($params{'hash_parent'})."..";1361delete$params{'hash_parent'};1362}13631364$href.= esc_path_info($params{'hash_base'});1365if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1366$href.=":/".esc_path_info($params{'file_name'});1367delete$params{'file_name'};1368}1369delete$params{'hash'};1370delete$params{'hash_base'};1371}elsif(defined$params{'hash'}) {1372$href.= esc_path_info($params{'hash'});1373delete$params{'hash'};1374}13751376# If the action was a snapshot, we can absorb the1377# snapshot_format parameter too1378if($is_snapshot) {1379my$fmt=$params{'snapshot_format'};1380# snapshot_format should always be defined when href()1381# is called, but just in case some code forgets, we1382# fall back to the default1383$fmt||=$snapshot_fmts[0];1384$href.=$known_snapshot_formats{$fmt}{'suffix'};1385delete$params{'snapshot_format'};1386}1387}13881389# now encode the parameters explicitly1390my@result= ();1391for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1392my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1393if(defined$params{$name}) {1394if(ref($params{$name})eq"ARRAY") {1395foreachmy$par(@{$params{$name}}) {1396push@result,$symbol."=". esc_param($par);1397}1398}else{1399push@result,$symbol."=". esc_param($params{$name});1400}1401}1402}1403$href.="?".join(';',@result)ifscalar@result;14041405# final transformation: trailing spaces must be escaped (URI-encoded)1406$href=~s/(\s+)$/CGI::escape($1)/e;14071408if($params{-anchor}) {1409$href.="#".esc_param($params{-anchor});1410}14111412return$href;1413}141414151416## ======================================================================1417## validation, quoting/unquoting and escaping14181419sub validate_action {1420my$input=shift||returnundef;1421returnundefunlessexists$actions{$input};1422return$input;1423}14241425sub validate_project {1426my$input=shift||returnundef;1427if(!validate_pathname($input) ||1428!(-d "$projectroot/$input") ||1429!check_export_ok("$projectroot/$input") ||1430($strict_export&& !project_in_list($input))) {1431returnundef;1432}else{1433return$input;1434}1435}14361437sub validate_pathname {1438my$input=shift||returnundef;14391440# no '.' or '..' as elements of path, i.e. no '.' nor '..'1441# at the beginning, at the end, and between slashes.1442# also this catches doubled slashes1443if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1444returnundef;1445}1446# no null characters1447if($input=~m!\0!) {1448returnundef;1449}1450return$input;1451}14521453sub validate_refname {1454my$input=shift||returnundef;14551456# textual hashes are O.K.1457if($input=~m/^[0-9a-fA-F]{40}$/) {1458return$input;1459}1460# it must be correct pathname1461$input= validate_pathname($input)1462orreturnundef;1463# restrictions on ref name according to git-check-ref-format1464if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1465returnundef;1466}1467return$input;1468}14691470# decode sequences of octets in utf8 into Perl's internal form,1471# which is utf-8 with utf8 flag set if needed. gitweb writes out1472# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1473sub to_utf8 {1474my$str=shift;1475returnundefunlessdefined$str;14761477if(utf8::is_utf8($str) || utf8::decode($str)) {1478return$str;1479}else{1480return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1481}1482}14831484# quote unsafe chars, but keep the slash, even when it's not1485# correct, but quoted slashes look too horrible in bookmarks1486sub esc_param {1487my$str=shift;1488returnundefunlessdefined$str;1489$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1490$str=~s/ /\+/g;1491return$str;1492}14931494# the quoting rules for path_info fragment are slightly different1495sub esc_path_info {1496my$str=shift;1497returnundefunlessdefined$str;14981499# path_info doesn't treat '+' as space (specially), but '?' must be escaped1500$str=~s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;15011502return$str;1503}15041505# quote unsafe chars in whole URL, so some characters cannot be quoted1506sub esc_url {1507my$str=shift;1508returnundefunlessdefined$str;1509$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;1510$str=~s/ /\+/g;1511return$str;1512}15131514# quote unsafe characters in HTML attributes1515sub esc_attr {15161517# for XHTML conformance escaping '"' to '"' is not enough1518return esc_html(@_);1519}15201521# replace invalid utf8 character with SUBSTITUTION sequence1522sub esc_html {1523my$str=shift;1524my%opts=@_;15251526returnundefunlessdefined$str;15271528$str= to_utf8($str);1529$str=$cgi->escapeHTML($str);1530if($opts{'-nbsp'}) {1531$str=~s/ / /g;1532}1533$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1534return$str;1535}15361537# quote control characters and escape filename to HTML1538sub esc_path {1539my$str=shift;1540my%opts=@_;15411542returnundefunlessdefined$str;15431544$str= to_utf8($str);1545$str=$cgi->escapeHTML($str);1546if($opts{'-nbsp'}) {1547$str=~s/ / /g;1548}1549$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1550return$str;1551}15521553# Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)1554sub sanitize {1555my$str=shift;15561557returnundefunlessdefined$str;15581559$str= to_utf8($str);1560$str=~ s|([[:cntrl:]])|($1=~/[\t\n\r]/?$1: quot_cec($1))|eg;1561return$str;1562}15631564# Make control characters "printable", using character escape codes (CEC)1565sub quot_cec {1566my$cntrl=shift;1567my%opts=@_;1568my%es= (# character escape codes, aka escape sequences1569"\t"=>'\t',# tab (HT)1570"\n"=>'\n',# line feed (LF)1571"\r"=>'\r',# carrige return (CR)1572"\f"=>'\f',# form feed (FF)1573"\b"=>'\b',# backspace (BS)1574"\a"=>'\a',# alarm (bell) (BEL)1575"\e"=>'\e',# escape (ESC)1576"\013"=>'\v',# vertical tab (VT)1577"\000"=>'\0',# nul character (NUL)1578);1579my$chr= ( (exists$es{$cntrl})1580?$es{$cntrl}1581:sprintf('\%2x',ord($cntrl)) );1582if($opts{-nohtml}) {1583return$chr;1584}else{1585return"<span class=\"cntrl\">$chr</span>";1586}1587}15881589# Alternatively use unicode control pictures codepoints,1590# Unicode "printable representation" (PR)1591sub quot_upr {1592my$cntrl=shift;1593my%opts=@_;15941595my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1596if($opts{-nohtml}) {1597return$chr;1598}else{1599return"<span class=\"cntrl\">$chr</span>";1600}1601}16021603# git may return quoted and escaped filenames1604sub unquote {1605my$str=shift;16061607sub unq {1608my$seq=shift;1609my%es= (# character escape codes, aka escape sequences1610't'=>"\t",# tab (HT, TAB)1611'n'=>"\n",# newline (NL)1612'r'=>"\r",# return (CR)1613'f'=>"\f",# form feed (FF)1614'b'=>"\b",# backspace (BS)1615'a'=>"\a",# alarm (bell) (BEL)1616'e'=>"\e",# escape (ESC)1617'v'=>"\013",# vertical tab (VT)1618);16191620if($seq=~m/^[0-7]{1,3}$/) {1621# octal char sequence1622returnchr(oct($seq));1623}elsif(exists$es{$seq}) {1624# C escape sequence, aka character escape code1625return$es{$seq};1626}1627# quoted ordinary character1628return$seq;1629}16301631if($str=~m/^"(.*)"$/) {1632# needs unquoting1633$str=$1;1634$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1635}1636return$str;1637}16381639# escape tabs (convert tabs to spaces)1640sub untabify {1641my$line=shift;16421643while((my$pos=index($line,"\t")) != -1) {1644if(my$count= (8- ($pos%8))) {1645my$spaces=' ' x $count;1646$line=~s/\t/$spaces/;1647}1648}16491650return$line;1651}16521653sub project_in_list {1654my$project=shift;1655my@list= git_get_projects_list();1656return@list&&scalar(grep{$_->{'path'}eq$project}@list);1657}16581659## ----------------------------------------------------------------------1660## HTML aware string manipulation16611662# Try to chop given string on a word boundary between position1663# $len and $len+$add_len. If there is no word boundary there,1664# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1665# (marking chopped part) would be longer than given string.1666sub chop_str {1667my$str=shift;1668my$len=shift;1669my$add_len=shift||10;1670my$where=shift||'right';# 'left' | 'center' | 'right'16711672# Make sure perl knows it is utf8 encoded so we don't1673# cut in the middle of a utf8 multibyte char.1674$str= to_utf8($str);16751676# allow only $len chars, but don't cut a word if it would fit in $add_len1677# if it doesn't fit, cut it if it's still longer than the dots we would add1678# remove chopped character entities entirely16791680# when chopping in the middle, distribute $len into left and right part1681# return early if chopping wouldn't make string shorter1682if($whereeq'center') {1683return$strif($len+5>=length($str));# filler is length 51684$len=int($len/2);1685}else{1686return$strif($len+4>=length($str));# filler is length 41687}16881689# regexps: ending and beginning with word part up to $add_len1690my$endre=qr/.{$len}\w{0,$add_len}/;1691my$begre=qr/\w{0,$add_len}.{$len}/;16921693if($whereeq'left') {1694$str=~m/^(.*?)($begre)$/;1695my($lead,$body) = ($1,$2);1696if(length($lead) >4) {1697$lead=" ...";1698}1699return"$lead$body";17001701}elsif($whereeq'center') {1702$str=~m/^($endre)(.*)$/;1703my($left,$str) = ($1,$2);1704$str=~m/^(.*?)($begre)$/;1705my($mid,$right) = ($1,$2);1706if(length($mid) >5) {1707$mid=" ... ";1708}1709return"$left$mid$right";17101711}else{1712$str=~m/^($endre)(.*)$/;1713my$body=$1;1714my$tail=$2;1715if(length($tail) >4) {1716$tail="... ";1717}1718return"$body$tail";1719}1720}17211722# takes the same arguments as chop_str, but also wraps a <span> around the1723# result with a title attribute if it does get chopped. Additionally, the1724# string is HTML-escaped.1725sub chop_and_escape_str {1726my($str) =@_;17271728my$chopped= chop_str(@_);1729$str= to_utf8($str);1730if($choppedeq$str) {1731return esc_html($chopped);1732}else{1733$str=~s/[[:cntrl:]]/?/g;1734return$cgi->span({-title=>$str}, esc_html($chopped));1735}1736}17371738# Highlight selected fragments of string, using given CSS class,1739# and escape HTML. It is assumed that fragments do not overlap.1740# Regions are passed as list of pairs (array references).1741#1742# Example: esc_html_hl_regions("foobar", "mark", [ 0, 3 ]) returns1743# '<span class="mark">foo</span>bar'1744sub esc_html_hl_regions {1745my($str,$css_class,@sel) =@_;1746my%opts=grep{ref($_)ne'ARRAY'}@sel;1747@sel=grep{ref($_)eq'ARRAY'}@sel;1748return esc_html($str,%opts)unless@sel;17491750my$out='';1751my$pos=0;17521753formy$s(@sel) {1754my($begin,$end) =@$s;17551756# Don't create empty <span> elements.1757next if$end<=$begin;17581759my$escaped= esc_html(substr($str,$begin,$end-$begin),1760%opts);17611762$out.= esc_html(substr($str,$pos,$begin-$pos),%opts)1763if($begin-$pos>0);1764$out.=$cgi->span({-class=>$css_class},$escaped);17651766$pos=$end;1767}1768$out.= esc_html(substr($str,$pos),%opts)1769if($pos<length($str));17701771return$out;1772}17731774# return positions of beginning and end of each match1775sub matchpos_list {1776my($str,$regexp) =@_;1777return unless(defined$str&&defined$regexp);17781779my@matches;1780while($str=~/$regexp/g) {1781push@matches, [$-[0],$+[0]];1782}1783return@matches;1784}17851786# highlight match (if any), and escape HTML1787sub esc_html_match_hl {1788my($str,$regexp) =@_;1789return esc_html($str)unlessdefined$regexp;17901791my@matches= matchpos_list($str,$regexp);1792return esc_html($str)unless@matches;17931794return esc_html_hl_regions($str,'match',@matches);1795}179617971798# highlight match (if any) of shortened string, and escape HTML1799sub esc_html_match_hl_chopped {1800my($str,$chopped,$regexp) =@_;1801return esc_html_match_hl($str,$regexp)unlessdefined$chopped;18021803my@matches= matchpos_list($str,$regexp);1804return esc_html($chopped)unless@matches;18051806# filter matches so that we mark chopped string1807my$tail="... ";# see chop_str1808unless($chopped=~s/\Q$tail\E$//) {1809$tail='';1810}1811my$chop_len=length($chopped);1812my$tail_len=length($tail);1813my@filtered;18141815formy$m(@matches) {1816if($m->[0] >$chop_len) {1817push@filtered, [$chop_len,$chop_len+$tail_len]if($tail_len>0);1818last;1819}elsif($m->[1] >$chop_len) {1820push@filtered, [$m->[0],$chop_len+$tail_len];1821last;1822}1823push@filtered,$m;1824}18251826return esc_html_hl_regions($chopped.$tail,'match',@filtered);1827}18281829## ----------------------------------------------------------------------1830## functions returning short strings18311832# CSS class for given age value (in seconds)1833sub age_class {1834my$age=shift;18351836if(!defined$age) {1837return"noage";1838}elsif($age<60*60*2) {1839return"age0";1840}elsif($age<60*60*24*2) {1841return"age1";1842}else{1843return"age2";1844}1845}18461847# convert age in seconds to "nn units ago" string1848sub age_string {1849my$age=shift;1850my$age_str;18511852if($age>60*60*24*365*2) {1853$age_str= (int$age/60/60/24/365);1854$age_str.=" years ago";1855}elsif($age>60*60*24*(365/12)*2) {1856$age_str=int$age/60/60/24/(365/12);1857$age_str.=" months ago";1858}elsif($age>60*60*24*7*2) {1859$age_str=int$age/60/60/24/7;1860$age_str.=" weeks ago";1861}elsif($age>60*60*24*2) {1862$age_str=int$age/60/60/24;1863$age_str.=" days ago";1864}elsif($age>60*60*2) {1865$age_str=int$age/60/60;1866$age_str.=" hours ago";1867}elsif($age>60*2) {1868$age_str=int$age/60;1869$age_str.=" min ago";1870}elsif($age>2) {1871$age_str=int$age;1872$age_str.=" sec ago";1873}else{1874$age_str.=" right now";1875}1876return$age_str;1877}18781879useconstant{1880 S_IFINVALID =>0030000,1881 S_IFGITLINK =>0160000,1882};18831884# submodule/subproject, a commit object reference1885sub S_ISGITLINK {1886my$mode=shift;18871888return(($mode& S_IFMT) == S_IFGITLINK)1889}18901891# convert file mode in octal to symbolic file mode string1892sub mode_str {1893my$mode=oct shift;18941895if(S_ISGITLINK($mode)) {1896return'm---------';1897}elsif(S_ISDIR($mode& S_IFMT)) {1898return'drwxr-xr-x';1899}elsif(S_ISLNK($mode)) {1900return'lrwxrwxrwx';1901}elsif(S_ISREG($mode)) {1902# git cares only about the executable bit1903if($mode& S_IXUSR) {1904return'-rwxr-xr-x';1905}else{1906return'-rw-r--r--';1907};1908}else{1909return'----------';1910}1911}19121913# convert file mode in octal to file type string1914sub file_type {1915my$mode=shift;19161917if($mode!~m/^[0-7]+$/) {1918return$mode;1919}else{1920$mode=oct$mode;1921}19221923if(S_ISGITLINK($mode)) {1924return"submodule";1925}elsif(S_ISDIR($mode& S_IFMT)) {1926return"directory";1927}elsif(S_ISLNK($mode)) {1928return"symlink";1929}elsif(S_ISREG($mode)) {1930return"file";1931}else{1932return"unknown";1933}1934}19351936# convert file mode in octal to file type description string1937sub file_type_long {1938my$mode=shift;19391940if($mode!~m/^[0-7]+$/) {1941return$mode;1942}else{1943$mode=oct$mode;1944}19451946if(S_ISGITLINK($mode)) {1947return"submodule";1948}elsif(S_ISDIR($mode& S_IFMT)) {1949return"directory";1950}elsif(S_ISLNK($mode)) {1951return"symlink";1952}elsif(S_ISREG($mode)) {1953if($mode& S_IXUSR) {1954return"executable";1955}else{1956return"file";1957};1958}else{1959return"unknown";1960}1961}196219631964## ----------------------------------------------------------------------1965## functions returning short HTML fragments, or transforming HTML fragments1966## which don't belong to other sections19671968# format line of commit message.1969sub format_log_line_html {1970my$line=shift;19711972$line= esc_html($line, -nbsp=>1);1973$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1974$cgi->a({-href => href(action=>"object", hash=>$1),1975-class=>"text"},$1);1976}eg;19771978return$line;1979}19801981# format marker of refs pointing to given object19821983# the destination action is chosen based on object type and current context:1984# - for annotated tags, we choose the tag view unless it's the current view1985# already, in which case we go to shortlog view1986# - for other refs, we keep the current view if we're in history, shortlog or1987# log view, and select shortlog otherwise1988sub format_ref_marker {1989my($refs,$id) =@_;1990my$markers='';19911992if(defined$refs->{$id}) {1993foreachmy$ref(@{$refs->{$id}}) {1994# this code exploits the fact that non-lightweight tags are the1995# only indirect objects, and that they are the only objects for which1996# we want to use tag instead of shortlog as action1997my($type,$name) =qw();1998my$indirect= ($ref=~s/\^\{\}$//);1999# e.g. tags/v2.6.11 or heads/next2000if($ref=~m!^(.*?)s?/(.*)$!) {2001$type=$1;2002$name=$2;2003}else{2004$type="ref";2005$name=$ref;2006}20072008my$class=$type;2009$class.=" indirect"if$indirect;20102011my$dest_action="shortlog";20122013if($indirect) {2014$dest_action="tag"unless$actioneq"tag";2015}elsif($action=~/^(history|(short)?log)$/) {2016$dest_action=$action;2017}20182019my$dest="";2020$dest.="refs/"unless$ref=~ m!^refs/!;2021$dest.=$ref;20222023my$link=$cgi->a({2024-href => href(2025 action=>$dest_action,2026 hash=>$dest2027)},$name);20282029$markers.=" <span class=\"".esc_attr($class)."\"title=\"".esc_attr($ref)."\">".2030$link."</span>";2031}2032}20332034if($markers) {2035return' <span class="refs">'.$markers.'</span>';2036}else{2037return"";2038}2039}20402041# format, perhaps shortened and with markers, title line2042sub format_subject_html {2043my($long,$short,$href,$extra) =@_;2044$extra=''unlessdefined($extra);20452046if(length($short) <length($long)) {2047$long=~s/[[:cntrl:]]/?/g;2048return$cgi->a({-href =>$href, -class=>"list subject",2049-title => to_utf8($long)},2050 esc_html($short)) .$extra;2051}else{2052return$cgi->a({-href =>$href, -class=>"list subject"},2053 esc_html($long)) .$extra;2054}2055}20562057# Rather than recomputing the url for an email multiple times, we cache it2058# after the first hit. This gives a visible benefit in views where the avatar2059# for the same email is used repeatedly (e.g. shortlog).2060# The cache is shared by all avatar engines (currently gravatar only), which2061# are free to use it as preferred. Since only one avatar engine is used for any2062# given page, there's no risk for cache conflicts.2063our%avatar_cache= ();20642065# Compute the picon url for a given email, by using the picon search service over at2066# http://www.cs.indiana.edu/picons/search.html2067sub picon_url {2068my$email=lc shift;2069if(!$avatar_cache{$email}) {2070my($user,$domain) =split('@',$email);2071$avatar_cache{$email} =2072"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".2073"$domain/$user/".2074"users+domains+unknown/up/single";2075}2076return$avatar_cache{$email};2077}20782079# Compute the gravatar url for a given email, if it's not in the cache already.2080# Gravatar stores only the part of the URL before the size, since that's the2081# one computationally more expensive. This also allows reuse of the cache for2082# different sizes (for this particular engine).2083sub gravatar_url {2084my$email=lc shift;2085my$size=shift;2086$avatar_cache{$email} ||=2087"http://www.gravatar.com/avatar/".2088 Digest::MD5::md5_hex($email) ."?s=";2089return$avatar_cache{$email} .$size;2090}20912092# Insert an avatar for the given $email at the given $size if the feature2093# is enabled.2094sub git_get_avatar {2095my($email,%opts) =@_;2096my$pre_white= ($opts{-pad_before} ?" ":"");2097my$post_white= ($opts{-pad_after} ?" ":"");2098$opts{-size} ||='default';2099my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};2100my$url="";2101if($git_avatareq'gravatar') {2102$url= gravatar_url($email,$size);2103}elsif($git_avatareq'picon') {2104$url= picon_url($email);2105}2106# Other providers can be added by extending the if chain, defining $url2107# as needed. If no variant puts something in $url, we assume avatars2108# are completely disabled/unavailable.2109if($url) {2110return$pre_white.2111"<img width=\"$size\"".2112"class=\"avatar\"".2113"src=\"".esc_url($url)."\"".2114"alt=\"\"".2115"/>".$post_white;2116}else{2117return"";2118}2119}21202121sub format_search_author {2122my($author,$searchtype,$displaytext) =@_;2123my$have_search= gitweb_check_feature('search');21242125if($have_search) {2126my$performed="";2127if($searchtypeeq'author') {2128$performed="authored";2129}elsif($searchtypeeq'committer') {2130$performed="committed";2131}21322133return$cgi->a({-href => href(action=>"search", hash=>$hash,2134 searchtext=>$author,2135 searchtype=>$searchtype),class=>"list",2136 title=>"Search for commits$performedby$author"},2137$displaytext);21382139}else{2140return$displaytext;2141}2142}21432144# format the author name of the given commit with the given tag2145# the author name is chopped and escaped according to the other2146# optional parameters (see chop_str).2147sub format_author_html {2148my$tag=shift;2149my$co=shift;2150my$author= chop_and_escape_str($co->{'author_name'},@_);2151return"<$tagclass=\"author\">".2152 format_search_author($co->{'author_name'},"author",2153 git_get_avatar($co->{'author_email'}, -pad_after =>1) .2154$author) .2155"</$tag>";2156}21572158# format git diff header line, i.e. "diff --(git|combined|cc) ..."2159sub format_git_diff_header_line {2160my$line=shift;2161my$diffinfo=shift;2162my($from,$to) =@_;21632164if($diffinfo->{'nparents'}) {2165# combined diff2166$line=~s!^(diff (.*?) )"?.*$!$1!;2167if($to->{'href'}) {2168$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2169 esc_path($to->{'file'}));2170}else{# file was deleted (no href)2171$line.= esc_path($to->{'file'});2172}2173}else{2174# "ordinary" diff2175$line=~s!^(diff (.*?) )"?a/.*$!$1!;2176if($from->{'href'}) {2177$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},2178'a/'. esc_path($from->{'file'}));2179}else{# file was added (no href)2180$line.='a/'. esc_path($from->{'file'});2181}2182$line.=' ';2183if($to->{'href'}) {2184$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2185'b/'. esc_path($to->{'file'}));2186}else{# file was deleted2187$line.='b/'. esc_path($to->{'file'});2188}2189}21902191return"<div class=\"diff header\">$line</div>\n";2192}21932194# format extended diff header line, before patch itself2195sub format_extended_diff_header_line {2196my$line=shift;2197my$diffinfo=shift;2198my($from,$to) =@_;21992200# match <path>2201if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {2202$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},2203 esc_path($from->{'file'}));2204}2205if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {2206$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},2207 esc_path($to->{'file'}));2208}2209# match single <mode>2210if($line=~m/\s(\d{6})$/) {2211$line.='<span class="info"> ('.2212 file_type_long($1) .2213')</span>';2214}2215# match <hash>2216if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {2217# can match only for combined diff2218$line='index ';2219for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2220if($from->{'href'}[$i]) {2221$line.=$cgi->a({-href=>$from->{'href'}[$i],2222-class=>"hash"},2223substr($diffinfo->{'from_id'}[$i],0,7));2224}else{2225$line.='0' x 7;2226}2227# separator2228$line.=','if($i<$diffinfo->{'nparents'} -1);2229}2230$line.='..';2231if($to->{'href'}) {2232$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2233substr($diffinfo->{'to_id'},0,7));2234}else{2235$line.='0' x 7;2236}22372238}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {2239# can match only for ordinary diff2240my($from_link,$to_link);2241if($from->{'href'}) {2242$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},2243substr($diffinfo->{'from_id'},0,7));2244}else{2245$from_link='0' x 7;2246}2247if($to->{'href'}) {2248$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2249substr($diffinfo->{'to_id'},0,7));2250}else{2251$to_link='0' x 7;2252}2253my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});2254$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;2255}22562257return$line."<br/>\n";2258}22592260# format from-file/to-file diff header2261sub format_diff_from_to_header {2262my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;2263my$line;2264my$result='';22652266$line=$from_line;2267#assert($line =~ m/^---/) if DEBUG;2268# no extra formatting for "^--- /dev/null"2269if(!$diffinfo->{'nparents'}) {2270# ordinary (single parent) diff2271if($line=~m!^--- "?a/!) {2272if($from->{'href'}) {2273$line='--- a/'.2274$cgi->a({-href=>$from->{'href'}, -class=>"path"},2275 esc_path($from->{'file'}));2276}else{2277$line='--- a/'.2278 esc_path($from->{'file'});2279}2280}2281$result.= qq!<div class="diff from_file">$line</div>\n!;22822283}else{2284# combined diff (merge commit)2285for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2286if($from->{'href'}[$i]) {2287$line='--- '.2288$cgi->a({-href=>href(action=>"blobdiff",2289 hash_parent=>$diffinfo->{'from_id'}[$i],2290 hash_parent_base=>$parents[$i],2291 file_parent=>$from->{'file'}[$i],2292 hash=>$diffinfo->{'to_id'},2293 hash_base=>$hash,2294 file_name=>$to->{'file'}),2295-class=>"path",2296-title=>"diff". ($i+1)},2297$i+1) .2298'/'.2299$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2300 esc_path($from->{'file'}[$i]));2301}else{2302$line='--- /dev/null';2303}2304$result.= qq!<div class="diff from_file">$line</div>\n!;2305}2306}23072308$line=$to_line;2309#assert($line =~ m/^\+\+\+/) if DEBUG;2310# no extra formatting for "^+++ /dev/null"2311if($line=~m!^\+\+\+ "?b/!) {2312if($to->{'href'}) {2313$line='+++ b/'.2314$cgi->a({-href=>$to->{'href'}, -class=>"path"},2315 esc_path($to->{'file'}));2316}else{2317$line='+++ b/'.2318 esc_path($to->{'file'});2319}2320}2321$result.= qq!<div class="diff to_file">$line</div>\n!;23222323return$result;2324}23252326# create note for patch simplified by combined diff2327sub format_diff_cc_simplified {2328my($diffinfo,@parents) =@_;2329my$result='';23302331$result.="<div class=\"diff header\">".2332"diff --cc ";2333if(!is_deleted($diffinfo)) {2334$result.=$cgi->a({-href => href(action=>"blob",2335 hash_base=>$hash,2336 hash=>$diffinfo->{'to_id'},2337 file_name=>$diffinfo->{'to_file'}),2338-class=>"path"},2339 esc_path($diffinfo->{'to_file'}));2340}else{2341$result.= esc_path($diffinfo->{'to_file'});2342}2343$result.="</div>\n".# class="diff header"2344"<div class=\"diff nodifferences\">".2345"Simple merge".2346"</div>\n";# class="diff nodifferences"23472348return$result;2349}23502351sub diff_line_class {2352my($line,$from,$to) =@_;23532354# ordinary diff2355my$num_sign=1;2356# combined diff2357if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2358$num_sign=scalar@{$from->{'href'}};2359}23602361my@diff_line_classifier= (2362{ regexp =>qr/^\@\@{$num_sign} /,class=>"chunk_header"},2363{ regexp =>qr/^\\/,class=>"incomplete"},2364{ regexp =>qr/^ {$num_sign}/,class=>"ctx"},2365# classifier for context must come before classifier add/rem,2366# or we would have to use more complicated regexp, for example2367# qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;2368{ regexp =>qr/^[+ ]{$num_sign}/,class=>"add"},2369{ regexp =>qr/^[- ]{$num_sign}/,class=>"rem"},2370);2371formy$clsfy(@diff_line_classifier) {2372return$clsfy->{'class'}2373if($line=~$clsfy->{'regexp'});2374}23752376# fallback2377return"";2378}23792380# assumes that $from and $to are defined and correctly filled,2381# and that $line holds a line of chunk header for unified diff2382sub format_unidiff_chunk_header {2383my($line,$from,$to) =@_;23842385my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2386$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;23872388$from_lines=0unlessdefined$from_lines;2389$to_lines=0unlessdefined$to_lines;23902391if($from->{'href'}) {2392$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2393-class=>"list"},$from_text);2394}2395if($to->{'href'}) {2396$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2397-class=>"list"},$to_text);2398}2399$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2400"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2401return$line;2402}24032404# assumes that $from and $to are defined and correctly filled,2405# and that $line holds a line of chunk header for combined diff2406sub format_cc_diff_chunk_header {2407my($line,$from,$to) =@_;24082409my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2410my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);24112412@from_text=split(' ',$ranges);2413for(my$i=0;$i<@from_text; ++$i) {2414($from_start[$i],$from_nlines[$i]) =2415(split(',',substr($from_text[$i],1)),0);2416}24172418$to_text=pop@from_text;2419$to_start=pop@from_start;2420$to_nlines=pop@from_nlines;24212422$line="<span class=\"chunk_info\">$prefix";2423for(my$i=0;$i<@from_text; ++$i) {2424if($from->{'href'}[$i]) {2425$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2426-class=>"list"},$from_text[$i]);2427}else{2428$line.=$from_text[$i];2429}2430$line.=" ";2431}2432if($to->{'href'}) {2433$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2434-class=>"list"},$to_text);2435}else{2436$line.=$to_text;2437}2438$line.="$prefix</span>".2439"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2440return$line;2441}24422443# process patch (diff) line (not to be used for diff headers),2444# returning HTML-formatted (but not wrapped) line.2445# If the line is passed as a reference, it is treated as HTML and not2446# esc_html()'ed.2447sub format_diff_line {2448my($line,$diff_class,$from,$to) =@_;24492450if(ref($line)) {2451$line=$$line;2452}else{2453chomp$line;2454$line= untabify($line);24552456if($from&&$to&&$line=~m/^\@{2} /) {2457$line= format_unidiff_chunk_header($line,$from,$to);2458}elsif($from&&$to&&$line=~m/^\@{3}/) {2459$line= format_cc_diff_chunk_header($line,$from,$to);2460}else{2461$line= esc_html($line, -nbsp=>1);2462}2463}24642465my$diff_classes="diff";2466$diff_classes.="$diff_class"if($diff_class);2467$line="<div class=\"$diff_classes\">$line</div>\n";24682469return$line;2470}24712472# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2473# linked. Pass the hash of the tree/commit to snapshot.2474sub format_snapshot_links {2475my($hash) =@_;2476my$num_fmts=@snapshot_fmts;2477if($num_fmts>1) {2478# A parenthesized list of links bearing format names.2479# e.g. "snapshot (_tar.gz_ _zip_)"2480return"snapshot (".join(' ',map2481$cgi->a({2482-href => href(2483 action=>"snapshot",2484 hash=>$hash,2485 snapshot_format=>$_2486)2487},$known_snapshot_formats{$_}{'display'})2488,@snapshot_fmts) .")";2489}elsif($num_fmts==1) {2490# A single "snapshot" link whose tooltip bears the format name.2491# i.e. "_snapshot_"2492my($fmt) =@snapshot_fmts;2493return2494$cgi->a({2495-href => href(2496 action=>"snapshot",2497 hash=>$hash,2498 snapshot_format=>$fmt2499),2500-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2501},"snapshot");2502}else{# $num_fmts == 02503returnundef;2504}2505}25062507## ......................................................................2508## functions returning values to be passed, perhaps after some2509## transformation, to other functions; e.g. returning arguments to href()25102511# returns hash to be passed to href to generate gitweb URL2512# in -title key it returns description of link2513sub get_feed_info {2514my$format=shift||'Atom';2515my%res= (action =>lc($format));25162517# feed links are possible only for project views2518return unless(defined$project);2519# some views should link to OPML, or to generic project feed,2520# or don't have specific feed yet (so they should use generic)2521return if(!$action||$action=~/^(?:tags|heads|forks|tag|search)$/x);25222523my$branch;2524# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2525# from tag links; this also makes possible to detect branch links2526if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2527(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2528$branch=$1;2529}2530# find log type for feed description (title)2531my$type='log';2532if(defined$file_name) {2533$type="history of$file_name";2534$type.="/"if($actioneq'tree');2535$type.=" on '$branch'"if(defined$branch);2536}else{2537$type="log of$branch"if(defined$branch);2538}25392540$res{-title} =$type;2541$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2542$res{'file_name'} =$file_name;25432544return%res;2545}25462547## ----------------------------------------------------------------------2548## git utility subroutines, invoking git commands25492550# returns path to the core git executable and the --git-dir parameter as list2551sub git_cmd {2552$number_of_git_cmds++;2553return$GIT,'--git-dir='.$git_dir;2554}25552556# quote the given arguments for passing them to the shell2557# quote_command("command", "arg 1", "arg with ' and ! characters")2558# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2559# Try to avoid using this function wherever possible.2560sub quote_command {2561returnjoin(' ',2562map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2563}25642565# get HEAD ref of given project as hash2566sub git_get_head_hash {2567return git_get_full_hash(shift,'HEAD');2568}25692570sub git_get_full_hash {2571return git_get_hash(@_);2572}25732574sub git_get_short_hash {2575return git_get_hash(@_,'--short=7');2576}25772578sub git_get_hash {2579my($project,$hash,@options) =@_;2580my$o_git_dir=$git_dir;2581my$retval=undef;2582$git_dir="$projectroot/$project";2583if(open my$fd,'-|', git_cmd(),'rev-parse',2584'--verify','-q',@options,$hash) {2585$retval= <$fd>;2586chomp$retvalifdefined$retval;2587close$fd;2588}2589if(defined$o_git_dir) {2590$git_dir=$o_git_dir;2591}2592return$retval;2593}25942595# get type of given object2596sub git_get_type {2597my$hash=shift;25982599open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2600my$type= <$fd>;2601close$fdorreturn;2602chomp$type;2603return$type;2604}26052606# repository configuration2607our$config_file='';2608our%config;26092610# store multiple values for single key as anonymous array reference2611# single values stored directly in the hash, not as [ <value> ]2612sub hash_set_multi {2613my($hash,$key,$value) =@_;26142615if(!exists$hash->{$key}) {2616$hash->{$key} =$value;2617}elsif(!ref$hash->{$key}) {2618$hash->{$key} = [$hash->{$key},$value];2619}else{2620push@{$hash->{$key}},$value;2621}2622}26232624# return hash of git project configuration2625# optionally limited to some section, e.g. 'gitweb'2626sub git_parse_project_config {2627my$section_regexp=shift;2628my%config;26292630local$/="\0";26312632open my$fh,"-|", git_cmd(),"config",'-z','-l',2633orreturn;26342635while(my$keyval= <$fh>) {2636chomp$keyval;2637my($key,$value) =split(/\n/,$keyval,2);26382639 hash_set_multi(\%config,$key,$value)2640if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2641}2642close$fh;26432644return%config;2645}26462647# convert config value to boolean: 'true' or 'false'2648# no value, number > 0, 'true' and 'yes' values are true2649# rest of values are treated as false (never as error)2650sub config_to_bool {2651my$val=shift;26522653return1if!defined$val;# section.key26542655# strip leading and trailing whitespace2656$val=~s/^\s+//;2657$val=~s/\s+$//;26582659return(($val=~/^\d+$/&&$val) ||# section.key = 12660($val=~/^(?:true|yes)$/i));# section.key = true2661}26622663# convert config value to simple decimal number2664# an optional value suffix of 'k', 'm', or 'g' will cause the value2665# to be multiplied by 1024, 1048576, or 10737418242666sub config_to_int {2667my$val=shift;26682669# strip leading and trailing whitespace2670$val=~s/^\s+//;2671$val=~s/\s+$//;26722673if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2674$unit=lc($unit);2675# unknown unit is treated as 12676return$num* ($uniteq'g'?1073741824:2677$uniteq'm'?1048576:2678$uniteq'k'?1024:1);2679}2680return$val;2681}26822683# convert config value to array reference, if needed2684sub config_to_multi {2685my$val=shift;26862687returnref($val) ?$val: (defined($val) ? [$val] : []);2688}26892690sub git_get_project_config {2691my($key,$type) =@_;26922693return unlessdefined$git_dir;26942695# key sanity check2696return unless($key);2697# only subsection, if exists, is case sensitive,2698# and not lowercased by 'git config -z -l'2699if(my($hi,$mi,$lo) = ($key=~/^([^.]*)\.(.*)\.([^.]*)$/)) {2700$lo=~s/_//g;2701$key=join(".",lc($hi),$mi,lc($lo));2702return if($lo=~/\W/||$hi=~/\W/);2703}else{2704$key=lc($key);2705$key=~s/_//g;2706return if($key=~/\W/);2707}2708$key=~s/^gitweb\.//;27092710# type sanity check2711if(defined$type) {2712$type=~s/^--//;2713$type=undef2714unless($typeeq'bool'||$typeeq'int');2715}27162717# get config2718if(!defined$config_file||2719$config_filene"$git_dir/config") {2720%config= git_parse_project_config('gitweb');2721$config_file="$git_dir/config";2722}27232724# check if config variable (key) exists2725return unlessexists$config{"gitweb.$key"};27262727# ensure given type2728if(!defined$type) {2729return$config{"gitweb.$key"};2730}elsif($typeeq'bool') {2731# backward compatibility: 'git config --bool' returns true/false2732return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2733}elsif($typeeq'int') {2734return config_to_int($config{"gitweb.$key"});2735}2736return$config{"gitweb.$key"};2737}27382739# get hash of given path at given ref2740sub git_get_hash_by_path {2741my$base=shift;2742my$path=shift||returnundef;2743my$type=shift;27442745$path=~ s,/+$,,;27462747open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2748or die_error(500,"Open git-ls-tree failed");2749my$line= <$fd>;2750close$fdorreturnundef;27512752if(!defined$line) {2753# there is no tree or hash given by $path at $base2754returnundef;2755}27562757#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2758$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2759if(defined$type&&$typene$2) {2760# type doesn't match2761returnundef;2762}2763return$3;2764}27652766# get path of entry with given hash at given tree-ish (ref)2767# used to get 'from' filename for combined diff (merge commit) for renames2768sub git_get_path_by_hash {2769my$base=shift||return;2770my$hash=shift||return;27712772local$/="\0";27732774open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2775orreturnundef;2776while(my$line= <$fd>) {2777chomp$line;27782779#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2780#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2781if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2782close$fd;2783return$1;2784}2785}2786close$fd;2787returnundef;2788}27892790## ......................................................................2791## git utility functions, directly accessing git repository27922793# get the value of config variable either from file named as the variable2794# itself in the repository ($GIT_DIR/$name file), or from gitweb.$name2795# configuration variable in the repository config file.2796sub git_get_file_or_project_config {2797my($path,$name) =@_;27982799$git_dir="$projectroot/$path";2800open my$fd,'<',"$git_dir/$name"2801orreturn git_get_project_config($name);2802my$conf= <$fd>;2803close$fd;2804if(defined$conf) {2805chomp$conf;2806}2807return$conf;2808}28092810sub git_get_project_description {2811my$path=shift;2812return git_get_file_or_project_config($path,'description');2813}28142815sub git_get_project_category {2816my$path=shift;2817return git_get_file_or_project_config($path,'category');2818}281928202821# supported formats:2822# * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)2823# - if its contents is a number, use it as tag weight,2824# - otherwise add a tag with weight 12825# * $GIT_DIR/ctags file, each line is a tag (with weight 1)2826# the same value multiple times increases tag weight2827# * `gitweb.ctag' multi-valued repo config variable2828sub git_get_project_ctags {2829my$project=shift;2830my$ctags= {};28312832$git_dir="$projectroot/$project";2833if(opendir my$dh,"$git_dir/ctags") {2834my@files=grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh);2835foreachmy$tagfile(@files) {2836open my$ct,'<',$tagfile2837ornext;2838my$val= <$ct>;2839chomp$valif$val;2840close$ct;28412842(my$ctag=$tagfile) =~ s#.*/##;2843if($val=~/^\d+$/) {2844$ctags->{$ctag} =$val;2845}else{2846$ctags->{$ctag} =1;2847}2848}2849closedir$dh;28502851}elsif(open my$fh,'<',"$git_dir/ctags") {2852while(my$line= <$fh>) {2853chomp$line;2854$ctags->{$line}++if$line;2855}2856close$fh;28572858}else{2859my$taglist= config_to_multi(git_get_project_config('ctag'));2860foreachmy$tag(@$taglist) {2861$ctags->{$tag}++;2862}2863}28642865return$ctags;2866}28672868# return hash, where keys are content tags ('ctags'),2869# and values are sum of weights of given tag in every project2870sub git_gather_all_ctags {2871my$projects=shift;2872my$ctags= {};28732874foreachmy$p(@$projects) {2875foreachmy$ct(keys%{$p->{'ctags'}}) {2876$ctags->{$ct} +=$p->{'ctags'}->{$ct};2877}2878}28792880return$ctags;2881}28822883sub git_populate_project_tagcloud {2884my$ctags=shift;28852886# First, merge different-cased tags; tags vote on casing2887my%ctags_lc;2888foreach(keys%$ctags) {2889$ctags_lc{lc$_}->{count} +=$ctags->{$_};2890if(not$ctags_lc{lc$_}->{topcount}2891or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2892$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2893$ctags_lc{lc$_}->{topname} =$_;2894}2895}28962897my$cloud;2898my$matched=$input_params{'ctag'};2899if(eval{require HTML::TagCloud;1; }) {2900$cloud= HTML::TagCloud->new;2901foreachmy$ctag(sort keys%ctags_lc) {2902# Pad the title with spaces so that the cloud looks2903# less crammed.2904my$title= esc_html($ctags_lc{$ctag}->{topname});2905$title=~s/ / /g;2906$title=~s/^/ /g;2907$title=~s/$/ /g;2908if(defined$matched&&$matchedeq$ctag) {2909$title=qq(<span class="match">$title</span>);2910}2911$cloud->add($title, href(project=>undef, ctag=>$ctag),2912$ctags_lc{$ctag}->{count});2913}2914}else{2915$cloud= {};2916foreachmy$ctag(keys%ctags_lc) {2917my$title= esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);2918if(defined$matched&&$matchedeq$ctag) {2919$title=qq(<span class="match">$title</span>);2920}2921$cloud->{$ctag}{count} =$ctags_lc{$ctag}->{count};2922$cloud->{$ctag}{ctag} =2923$cgi->a({-href=>href(project=>undef, ctag=>$ctag)},$title);2924}2925}2926return$cloud;2927}29282929sub git_show_project_tagcloud {2930my($cloud,$count) =@_;2931if(ref$cloudeq'HTML::TagCloud') {2932return$cloud->html_and_css($count);2933}else{2934my@tags=sort{$cloud->{$a}->{'count'} <=>$cloud->{$b}->{'count'} }keys%$cloud;2935return2936'<div id="htmltagcloud"'.($project?'':' align="center"').'>'.2937join(', ',map{2938$cloud->{$_}->{'ctag'}2939}splice(@tags,0,$count)) .2940'</div>';2941}2942}29432944sub git_get_project_url_list {2945my$path=shift;29462947$git_dir="$projectroot/$path";2948open my$fd,'<',"$git_dir/cloneurl"2949orreturnwantarray?2950@{ config_to_multi(git_get_project_config('url')) } :2951 config_to_multi(git_get_project_config('url'));2952my@git_project_url_list=map{chomp;$_} <$fd>;2953close$fd;29542955returnwantarray?@git_project_url_list: \@git_project_url_list;2956}29572958sub git_get_projects_list {2959my$filter=shift||'';2960my$paranoid=shift;2961my@list;29622963if(-d $projects_list) {2964# search in directory2965my$dir=$projects_list;2966# remove the trailing "/"2967$dir=~s!/+$!!;2968my$pfxlen=length("$dir");2969my$pfxdepth= ($dir=~tr!/!!);2970# when filtering, search only given subdirectory2971if($filter&& !$paranoid) {2972$dir.="/$filter";2973$dir=~s!/+$!!;2974}29752976 File::Find::find({2977 follow_fast =>1,# follow symbolic links2978 follow_skip =>2,# ignore duplicates2979 dangling_symlinks =>0,# ignore dangling symlinks, silently2980 wanted =>sub{2981# global variables2982our$project_maxdepth;2983our$projectroot;2984# skip project-list toplevel, if we get it.2985return if(m!^[/.]$!);2986# only directories can be git repositories2987return unless(-d $_);2988# don't traverse too deep (Find is super slow on os x)2989# $project_maxdepth excludes depth of $projectroot2990if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2991$File::Find::prune =1;2992return;2993}29942995my$path=substr($File::Find::name,$pfxlen+1);2996# paranoidly only filter here2997if($paranoid&&$filter&&$path!~m!^\Q$filter\E/!) {2998next;2999}3000# we check related file in $projectroot3001if(check_export_ok("$projectroot/$path")) {3002push@list, { path =>$path};3003$File::Find::prune =1;3004}3005},3006},"$dir");30073008}elsif(-f $projects_list) {3009# read from file(url-encoded):3010# 'git%2Fgit.git Linus+Torvalds'3011# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'3012# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'3013open my$fd,'<',$projects_listorreturn;3014 PROJECT:3015while(my$line= <$fd>) {3016chomp$line;3017my($path,$owner) =split' ',$line;3018$path= unescape($path);3019$owner= unescape($owner);3020if(!defined$path) {3021next;3022}3023# if $filter is rpovided, check if $path begins with $filter3024if($filter&&$path!~m!^\Q$filter\E/!) {3025next;3026}3027if(check_export_ok("$projectroot/$path")) {3028my$pr= {3029 path =>$path3030};3031if($owner) {3032$pr->{'owner'} = to_utf8($owner);3033}3034push@list,$pr;3035}3036}3037close$fd;3038}3039return@list;3040}30413042# written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)3043# as side effects it sets 'forks' field to list of forks for forked projects3044sub filter_forks_from_projects_list {3045my$projects=shift;30463047my%trie;# prefix tree of directories (path components)3048# generate trie out of those directories that might contain forks3049foreachmy$pr(@$projects) {3050my$path=$pr->{'path'};3051$path=~s/\.git$//;# forks of 'repo.git' are in 'repo/' directory3052next if($path=~m!/$!);# skip non-bare repositories, e.g. 'repo/.git'3053next unless($path);# skip '.git' repository: tests, git-instaweb3054next unless(-d "$projectroot/$path");# containing directory exists3055$pr->{'forks'} = [];# there can be 0 or more forks of project30563057# add to trie3058my@dirs=split('/',$path);3059# walk the trie, until either runs out of components or out of trie3060my$ref= \%trie;3061while(scalar@dirs&&3062exists($ref->{$dirs[0]})) {3063$ref=$ref->{shift@dirs};3064}3065# create rest of trie structure from rest of components3066foreachmy$dir(@dirs) {3067$ref=$ref->{$dir} = {};3068}3069# create end marker, store $pr as a data3070$ref->{''} =$prif(!exists$ref->{''});3071}30723073# filter out forks, by finding shortest prefix match for paths3074my@filtered;3075 PROJECT:3076foreachmy$pr(@$projects) {3077# trie lookup3078my$ref= \%trie;3079 DIR:3080foreachmy$dir(split('/',$pr->{'path'})) {3081if(exists$ref->{''}) {3082# found [shortest] prefix, is a fork - skip it3083push@{$ref->{''}{'forks'}},$pr;3084next PROJECT;3085}3086if(!exists$ref->{$dir}) {3087# not in trie, cannot have prefix, not a fork3088push@filtered,$pr;3089next PROJECT;3090}3091# If the dir is there, we just walk one step down the trie.3092$ref=$ref->{$dir};3093}3094# we ran out of trie3095# (shouldn't happen: it's either no match, or end marker)3096push@filtered,$pr;3097}30983099return@filtered;3100}31013102# note: fill_project_list_info must be run first,3103# for 'descr_long' and 'ctags' to be filled3104sub search_projects_list {3105my($projlist,%opts) =@_;3106my$tagfilter=$opts{'tagfilter'};3107my$search_re=$opts{'search_regexp'};31083109return@$projlist3110unless($tagfilter||$search_re);31113112# searching projects require filling to be run before it;3113 fill_project_list_info($projlist,3114$tagfilter?'ctags': (),3115$search_re? ('path','descr') : ());3116my@projects;3117 PROJECT:3118foreachmy$pr(@$projlist) {31193120if($tagfilter) {3121next unlessref($pr->{'ctags'})eq'HASH';3122next unless3123grep{lc($_)eq lc($tagfilter) }keys%{$pr->{'ctags'}};3124}31253126if($search_re) {3127next unless3128$pr->{'path'} =~/$search_re/||3129$pr->{'descr_long'} =~/$search_re/;3130}31313132push@projects,$pr;3133}31343135return@projects;3136}31373138our$gitweb_project_owner=undef;3139sub git_get_project_list_from_file {31403141return if(defined$gitweb_project_owner);31423143$gitweb_project_owner= {};3144# read from file (url-encoded):3145# 'git%2Fgit.git Linus+Torvalds'3146# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'3147# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'3148if(-f $projects_list) {3149open(my$fd,'<',$projects_list);3150while(my$line= <$fd>) {3151chomp$line;3152my($pr,$ow) =split' ',$line;3153$pr= unescape($pr);3154$ow= unescape($ow);3155$gitweb_project_owner->{$pr} = to_utf8($ow);3156}3157close$fd;3158}3159}31603161sub git_get_project_owner {3162my$project=shift;3163my$owner;31643165returnundefunless$project;3166$git_dir="$projectroot/$project";31673168if(!defined$gitweb_project_owner) {3169 git_get_project_list_from_file();3170}31713172if(exists$gitweb_project_owner->{$project}) {3173$owner=$gitweb_project_owner->{$project};3174}3175if(!defined$owner){3176$owner= git_get_project_config('owner');3177}3178if(!defined$owner) {3179$owner= get_file_owner("$git_dir");3180}31813182return$owner;3183}31843185sub git_get_last_activity {3186my($path) =@_;3187my$fd;31883189$git_dir="$projectroot/$path";3190open($fd,"-|", git_cmd(),'for-each-ref',3191'--format=%(committer)',3192'--sort=-committerdate',3193'--count=1',3194'refs/heads')orreturn;3195my$most_recent= <$fd>;3196close$fdorreturn;3197if(defined$most_recent&&3198$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {3199my$timestamp=$1;3200my$age=time-$timestamp;3201return($age, age_string($age));3202}3203return(undef,undef);3204}32053206# Implementation note: when a single remote is wanted, we cannot use 'git3207# remote show -n' because that command always work (assuming it's a remote URL3208# if it's not defined), and we cannot use 'git remote show' because that would3209# try to make a network roundtrip. So the only way to find if that particular3210# remote is defined is to walk the list provided by 'git remote -v' and stop if3211# and when we find what we want.3212sub git_get_remotes_list {3213my$wanted=shift;3214my%remotes= ();32153216open my$fd,'-|', git_cmd(),'remote','-v';3217return unless$fd;3218while(my$remote= <$fd>) {3219chomp$remote;3220$remote=~s!\t(.*?)\s+\((\w+)\)$!!;3221next if$wantedand not$remoteeq$wanted;3222my($url,$key) = ($1,$2);32233224$remotes{$remote} ||= {'heads'=> () };3225$remotes{$remote}{$key} =$url;3226}3227close$fdorreturn;3228returnwantarray?%remotes: \%remotes;3229}32303231# Takes a hash of remotes as first parameter and fills it by adding the3232# available remote heads for each of the indicated remotes.3233sub fill_remote_heads {3234my$remotes=shift;3235my@heads=map{"remotes/$_"}keys%$remotes;3236my@remoteheads= git_get_heads_list(undef,@heads);3237foreachmy$remote(keys%$remotes) {3238$remotes->{$remote}{'heads'} = [grep{3239$_->{'name'} =~s!^$remote/!!3240}@remoteheads];3241}3242}32433244sub git_get_references {3245my$type=shift||"";3246my%refs;3247# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.113248# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}3249open my$fd,"-|", git_cmd(),"show-ref","--dereference",3250($type? ("--","refs/$type") : ())# use -- <pattern> if $type3251orreturn;32523253while(my$line= <$fd>) {3254chomp$line;3255if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {3256if(defined$refs{$1}) {3257push@{$refs{$1}},$2;3258}else{3259$refs{$1} = [$2];3260}3261}3262}3263close$fdorreturn;3264return \%refs;3265}32663267sub git_get_rev_name_tags {3268my$hash=shift||returnundef;32693270open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash3271orreturn;3272my$name_rev= <$fd>;3273close$fd;32743275if($name_rev=~ m|^$hash tags/(.*)$|) {3276return$1;3277}else{3278# catches also '$hash undefined' output3279returnundef;3280}3281}32823283## ----------------------------------------------------------------------3284## parse to hash functions32853286sub parse_date {3287my$epoch=shift;3288my$tz=shift||"-0000";32893290my%date;3291my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");3292my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");3293my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);3294$date{'hour'} =$hour;3295$date{'minute'} =$min;3296$date{'mday'} =$mday;3297$date{'day'} =$days[$wday];3298$date{'month'} =$months[$mon];3299$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",3300$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;3301$date{'mday-time'} =sprintf"%d%s%02d:%02d",3302$mday,$months[$mon],$hour,$min;3303$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",33041900+$year,1+$mon,$mday,$hour,$min,$sec;33053306my($tz_sign,$tz_hour,$tz_min) =3307($tz=~m/^([-+])(\d\d)(\d\d)$/);3308$tz_sign= ($tz_signeq'-'? -1: +1);3309my$local=$epoch+$tz_sign*((($tz_hour*60) +$tz_min)*60);3310($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);3311$date{'hour_local'} =$hour;3312$date{'minute_local'} =$min;3313$date{'tz_local'} =$tz;3314$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",33151900+$year,$mon+1,$mday,3316$hour,$min,$sec,$tz);3317return%date;3318}33193320sub parse_tag {3321my$tag_id=shift;3322my%tag;3323my@comment;33243325open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;3326$tag{'id'} =$tag_id;3327while(my$line= <$fd>) {3328chomp$line;3329if($line=~m/^object ([0-9a-fA-F]{40})$/) {3330$tag{'object'} =$1;3331}elsif($line=~m/^type (.+)$/) {3332$tag{'type'} =$1;3333}elsif($line=~m/^tag (.+)$/) {3334$tag{'name'} =$1;3335}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {3336$tag{'author'} =$1;3337$tag{'author_epoch'} =$2;3338$tag{'author_tz'} =$3;3339if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {3340$tag{'author_name'} =$1;3341$tag{'author_email'} =$2;3342}else{3343$tag{'author_name'} =$tag{'author'};3344}3345}elsif($line=~m/--BEGIN/) {3346push@comment,$line;3347last;3348}elsif($lineeq"") {3349last;3350}3351}3352push@comment, <$fd>;3353$tag{'comment'} = \@comment;3354close$fdorreturn;3355if(!defined$tag{'name'}) {3356return3357};3358return%tag3359}33603361sub parse_commit_text {3362my($commit_text,$withparents) =@_;3363my@commit_lines=split'\n',$commit_text;3364my%co;33653366pop@commit_lines;# Remove '\0'33673368if(!@commit_lines) {3369return;3370}33713372my$header=shift@commit_lines;3373if($header!~m/^[0-9a-fA-F]{40}/) {3374return;3375}3376($co{'id'},my@parents) =split' ',$header;3377while(my$line=shift@commit_lines) {3378last if$lineeq"\n";3379if($line=~m/^tree ([0-9a-fA-F]{40})$/) {3380$co{'tree'} =$1;3381}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {3382push@parents,$1;3383}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {3384$co{'author'} = to_utf8($1);3385$co{'author_epoch'} =$2;3386$co{'author_tz'} =$3;3387if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {3388$co{'author_name'} =$1;3389$co{'author_email'} =$2;3390}else{3391$co{'author_name'} =$co{'author'};3392}3393}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {3394$co{'committer'} = to_utf8($1);3395$co{'committer_epoch'} =$2;3396$co{'committer_tz'} =$3;3397if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {3398$co{'committer_name'} =$1;3399$co{'committer_email'} =$2;3400}else{3401$co{'committer_name'} =$co{'committer'};3402}3403}3404}3405if(!defined$co{'tree'}) {3406return;3407};3408$co{'parents'} = \@parents;3409$co{'parent'} =$parents[0];34103411foreachmy$title(@commit_lines) {3412$title=~s/^ //;3413if($titlene"") {3414$co{'title'} = chop_str($title,80,5);3415# remove leading stuff of merges to make the interesting part visible3416if(length($title) >50) {3417$title=~s/^Automatic //;3418$title=~s/^merge (of|with) /Merge ... /i;3419if(length($title) >50) {3420$title=~s/(http|rsync):\/\///;3421}3422if(length($title) >50) {3423$title=~s/(master|www|rsync)\.//;3424}3425if(length($title) >50) {3426$title=~s/kernel.org:?//;3427}3428if(length($title) >50) {3429$title=~s/\/pub\/scm//;3430}3431}3432$co{'title_short'} = chop_str($title,50,5);3433last;3434}3435}3436if(!defined$co{'title'} ||$co{'title'}eq"") {3437$co{'title'} =$co{'title_short'} ='(no commit message)';3438}3439# remove added spaces3440foreachmy$line(@commit_lines) {3441$line=~s/^ //;3442}3443$co{'comment'} = \@commit_lines;34443445my$age=time-$co{'committer_epoch'};3446$co{'age'} =$age;3447$co{'age_string'} = age_string($age);3448my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});3449if($age>60*60*24*7*2) {3450$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3451$co{'age_string_age'} =$co{'age_string'};3452}else{3453$co{'age_string_date'} =$co{'age_string'};3454$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3455}3456return%co;3457}34583459sub parse_commit {3460my($commit_id) =@_;3461my%co;34623463local$/="\0";34643465open my$fd,"-|", git_cmd(),"rev-list",3466"--parents",3467"--header",3468"--max-count=1",3469$commit_id,3470"--",3471or die_error(500,"Open git-rev-list failed");3472%co= parse_commit_text(<$fd>,1);3473close$fd;34743475return%co;3476}34773478sub parse_commits {3479my($commit_id,$maxcount,$skip,$filename,@args) =@_;3480my@cos;34813482$maxcount||=1;3483$skip||=0;34843485local$/="\0";34863487open my$fd,"-|", git_cmd(),"rev-list",3488"--header",3489@args,3490("--max-count=".$maxcount),3491("--skip=".$skip),3492@extra_options,3493$commit_id,3494"--",3495($filename? ($filename) : ())3496or die_error(500,"Open git-rev-list failed");3497while(my$line= <$fd>) {3498my%co= parse_commit_text($line);3499push@cos, \%co;3500}3501close$fd;35023503returnwantarray?@cos: \@cos;3504}35053506# parse line of git-diff-tree "raw" output3507sub parse_difftree_raw_line {3508my$line=shift;3509my%res;35103511# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'3512# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'3513if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {3514$res{'from_mode'} =$1;3515$res{'to_mode'} =$2;3516$res{'from_id'} =$3;3517$res{'to_id'} =$4;3518$res{'status'} =$5;3519$res{'similarity'} =$6;3520if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3521($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3522}else{3523$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3524}3525}3526# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3527# combined diff (for merge commit)3528elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3529$res{'nparents'} =length($1);3530$res{'from_mode'} = [split(' ',$2) ];3531$res{'to_mode'} =pop@{$res{'from_mode'}};3532$res{'from_id'} = [split(' ',$3) ];3533$res{'to_id'} =pop@{$res{'from_id'}};3534$res{'status'} = [split('',$4) ];3535$res{'to_file'} = unquote($5);3536}3537# 'c512b523472485aef4fff9e57b229d9d243c967f'3538elsif($line=~m/^([0-9a-fA-F]{40})$/) {3539$res{'commit'} =$1;3540}35413542returnwantarray?%res: \%res;3543}35443545# wrapper: return parsed line of git-diff-tree "raw" output3546# (the argument might be raw line, or parsed info)3547sub parsed_difftree_line {3548my$line_or_ref=shift;35493550if(ref($line_or_ref)eq"HASH") {3551# pre-parsed (or generated by hand)3552return$line_or_ref;3553}else{3554return parse_difftree_raw_line($line_or_ref);3555}3556}35573558# parse line of git-ls-tree output3559sub parse_ls_tree_line {3560my$line=shift;3561my%opts=@_;3562my%res;35633564if($opts{'-l'}) {3565#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3566$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;35673568$res{'mode'} =$1;3569$res{'type'} =$2;3570$res{'hash'} =$3;3571$res{'size'} =$4;3572if($opts{'-z'}) {3573$res{'name'} =$5;3574}else{3575$res{'name'} = unquote($5);3576}3577}else{3578#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3579$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;35803581$res{'mode'} =$1;3582$res{'type'} =$2;3583$res{'hash'} =$3;3584if($opts{'-z'}) {3585$res{'name'} =$4;3586}else{3587$res{'name'} = unquote($4);3588}3589}35903591returnwantarray?%res: \%res;3592}35933594# generates _two_ hashes, references to which are passed as 2 and 3 argument3595sub parse_from_to_diffinfo {3596my($diffinfo,$from,$to,@parents) =@_;35973598if($diffinfo->{'nparents'}) {3599# combined diff3600$from->{'file'} = [];3601$from->{'href'} = [];3602 fill_from_file_info($diffinfo,@parents)3603unlessexists$diffinfo->{'from_file'};3604for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3605$from->{'file'}[$i] =3606defined$diffinfo->{'from_file'}[$i] ?3607$diffinfo->{'from_file'}[$i] :3608$diffinfo->{'to_file'};3609if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3610$from->{'href'}[$i] = href(action=>"blob",3611 hash_base=>$parents[$i],3612 hash=>$diffinfo->{'from_id'}[$i],3613 file_name=>$from->{'file'}[$i]);3614}else{3615$from->{'href'}[$i] =undef;3616}3617}3618}else{3619# ordinary (not combined) diff3620$from->{'file'} =$diffinfo->{'from_file'};3621if($diffinfo->{'status'}ne"A") {# not new (added) file3622$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3623 hash=>$diffinfo->{'from_id'},3624 file_name=>$from->{'file'});3625}else{3626delete$from->{'href'};3627}3628}36293630$to->{'file'} =$diffinfo->{'to_file'};3631if(!is_deleted($diffinfo)) {# file exists in result3632$to->{'href'} = href(action=>"blob", hash_base=>$hash,3633 hash=>$diffinfo->{'to_id'},3634 file_name=>$to->{'file'});3635}else{3636delete$to->{'href'};3637}3638}36393640## ......................................................................3641## parse to array of hashes functions36423643sub git_get_heads_list {3644my($limit,@classes) =@_;3645@classes= ('heads')unless@classes;3646my@patterns=map{"refs/$_"}@classes;3647my@headslist;36483649open my$fd,'-|', git_cmd(),'for-each-ref',3650($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3651'--format=%(objectname) %(refname) %(subject)%00%(committer)',3652@patterns3653orreturn;3654while(my$line= <$fd>) {3655my%ref_item;36563657chomp$line;3658my($refinfo,$committerinfo) =split(/\0/,$line);3659my($hash,$name,$title) =split(' ',$refinfo,3);3660my($committer,$epoch,$tz) =3661($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3662$ref_item{'fullname'} =$name;3663$name=~s!^refs/(?:head|remote)s/!!;36643665$ref_item{'name'} =$name;3666$ref_item{'id'} =$hash;3667$ref_item{'title'} =$title||'(no commit message)';3668$ref_item{'epoch'} =$epoch;3669if($epoch) {3670$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3671}else{3672$ref_item{'age'} ="unknown";3673}36743675push@headslist, \%ref_item;3676}3677close$fd;36783679returnwantarray?@headslist: \@headslist;3680}36813682sub git_get_tags_list {3683my$limit=shift;3684my@tagslist;36853686open my$fd,'-|', git_cmd(),'for-each-ref',3687($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3688'--format=%(objectname) %(objecttype) %(refname) '.3689'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3690'refs/tags'3691orreturn;3692while(my$line= <$fd>) {3693my%ref_item;36943695chomp$line;3696my($refinfo,$creatorinfo) =split(/\0/,$line);3697my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3698my($creator,$epoch,$tz) =3699($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3700$ref_item{'fullname'} =$name;3701$name=~s!^refs/tags/!!;37023703$ref_item{'type'} =$type;3704$ref_item{'id'} =$id;3705$ref_item{'name'} =$name;3706if($typeeq"tag") {3707$ref_item{'subject'} =$title;3708$ref_item{'reftype'} =$reftype;3709$ref_item{'refid'} =$refid;3710}else{3711$ref_item{'reftype'} =$type;3712$ref_item{'refid'} =$id;3713}37143715if($typeeq"tag"||$typeeq"commit") {3716$ref_item{'epoch'} =$epoch;3717if($epoch) {3718$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3719}else{3720$ref_item{'age'} ="unknown";3721}3722}37233724push@tagslist, \%ref_item;3725}3726close$fd;37273728returnwantarray?@tagslist: \@tagslist;3729}37303731## ----------------------------------------------------------------------3732## filesystem-related functions37333734sub get_file_owner {3735my$path=shift;37363737my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3738my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3739if(!defined$gcos) {3740returnundef;3741}3742my$owner=$gcos;3743$owner=~s/[,;].*$//;3744return to_utf8($owner);3745}37463747# assume that file exists3748sub insert_file {3749my$filename=shift;37503751open my$fd,'<',$filename;3752print map{ to_utf8($_) } <$fd>;3753close$fd;3754}37553756## ......................................................................3757## mimetype related functions37583759sub mimetype_guess_file {3760my$filename=shift;3761my$mimemap=shift;3762-r $mimemaporreturnundef;37633764my%mimemap;3765open(my$mh,'<',$mimemap)orreturnundef;3766while(<$mh>) {3767next ifm/^#/;# skip comments3768my($mimetype,@exts) =split(/\s+/);3769foreachmy$ext(@exts) {3770$mimemap{$ext} =$mimetype;3771}3772}3773close($mh);37743775$filename=~/\.([^.]*)$/;3776return$mimemap{$1};3777}37783779sub mimetype_guess {3780my$filename=shift;3781my$mime;3782$filename=~/\./orreturnundef;37833784if($mimetypes_file) {3785my$file=$mimetypes_file;3786if($file!~m!^/!) {# if it is relative path3787# it is relative to project3788$file="$projectroot/$project/$file";3789}3790$mime= mimetype_guess_file($filename,$file);3791}3792$mime||= mimetype_guess_file($filename,'/etc/mime.types');3793return$mime;3794}37953796sub blob_mimetype {3797my$fd=shift;3798my$filename=shift;37993800if($filename) {3801my$mime= mimetype_guess($filename);3802$mimeandreturn$mime;3803}38043805# just in case3806return$default_blob_plain_mimetypeunless$fd;38073808if(-T $fd) {3809return'text/plain';3810}elsif(!$filename) {3811return'application/octet-stream';3812}elsif($filename=~m/\.png$/i) {3813return'image/png';3814}elsif($filename=~m/\.gif$/i) {3815return'image/gif';3816}elsif($filename=~m/\.jpe?g$/i) {3817return'image/jpeg';3818}else{3819return'application/octet-stream';3820}3821}38223823sub blob_contenttype {3824my($fd,$file_name,$type) =@_;38253826$type||= blob_mimetype($fd,$file_name);3827if($typeeq'text/plain'&&defined$default_text_plain_charset) {3828$type.="; charset=$default_text_plain_charset";3829}38303831return$type;3832}38333834# guess file syntax for syntax highlighting; return undef if no highlighting3835# the name of syntax can (in the future) depend on syntax highlighter used3836sub guess_file_syntax {3837my($highlight,$mimetype,$file_name) =@_;3838returnundefunless($highlight&&defined$file_name);3839my$basename= basename($file_name,'.in');3840return$highlight_basename{$basename}3841ifexists$highlight_basename{$basename};38423843$basename=~/\.([^.]*)$/;3844my$ext=$1orreturnundef;3845return$highlight_ext{$ext}3846ifexists$highlight_ext{$ext};38473848returnundef;3849}38503851# run highlighter and return FD of its output,3852# or return original FD if no highlighting3853sub run_highlighter {3854my($fd,$highlight,$syntax) =@_;3855return$fdunless($highlight&&defined$syntax);38563857close$fd;3858open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3859 quote_command($highlight_bin).3860" --replace-tabs=8 --fragment --syntax$syntax|"3861or die_error(500,"Couldn't open file or run syntax highlighter");3862return$fd;3863}38643865## ======================================================================3866## functions printing HTML: header, footer, error page38673868sub get_page_title {3869my$title= to_utf8($site_name);38703871unless(defined$project) {3872if(defined$project_filter) {3873$title.=" - projects in '". esc_path($project_filter) ."'";3874}3875return$title;3876}3877$title.=" - ". to_utf8($project);38783879return$titleunless(defined$action);3880$title.="/$action";# $action is US-ASCII (7bit ASCII)38813882return$titleunless(defined$file_name);3883$title.=" - ". esc_path($file_name);3884if($actioneq"tree"&&$file_name!~ m|/$|) {3885$title.="/";3886}38873888return$title;3889}38903891sub get_content_type_html {3892# require explicit support from the UA if we are to send the page as3893# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3894# we have to do this because MSIE sometimes globs '*/*', pretending to3895# support xhtml+xml but choking when it gets what it asked for.3896if(defined$cgi->http('HTTP_ACCEPT') &&3897$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3898$cgi->Accept('application/xhtml+xml') !=0) {3899return'application/xhtml+xml';3900}else{3901return'text/html';3902}3903}39043905sub print_feed_meta {3906if(defined$project) {3907my%href_params= get_feed_info();3908if(!exists$href_params{'-title'}) {3909$href_params{'-title'} ='log';3910}39113912foreachmy$format(qw(RSS Atom)) {3913my$type=lc($format);3914my%link_attr= (3915'-rel'=>'alternate',3916'-title'=> esc_attr("$project-$href_params{'-title'} -$formatfeed"),3917'-type'=>"application/$type+xml"3918);39193920$href_params{'extra_options'} =undef;3921$href_params{'action'} =$type;3922$link_attr{'-href'} = href(%href_params);3923print"<link ".3924"rel=\"$link_attr{'-rel'}\"".3925"title=\"$link_attr{'-title'}\"".3926"href=\"$link_attr{'-href'}\"".3927"type=\"$link_attr{'-type'}\"".3928"/>\n";39293930$href_params{'extra_options'} ='--no-merges';3931$link_attr{'-href'} = href(%href_params);3932$link_attr{'-title'} .=' (no merges)';3933print"<link ".3934"rel=\"$link_attr{'-rel'}\"".3935"title=\"$link_attr{'-title'}\"".3936"href=\"$link_attr{'-href'}\"".3937"type=\"$link_attr{'-type'}\"".3938"/>\n";3939}39403941}else{3942printf('<link rel="alternate" title="%sprojects list" '.3943'href="%s" type="text/plain; charset=utf-8" />'."\n",3944 esc_attr($site_name), href(project=>undef, action=>"project_index"));3945printf('<link rel="alternate" title="%sprojects feeds" '.3946'href="%s" type="text/x-opml" />'."\n",3947 esc_attr($site_name), href(project=>undef, action=>"opml"));3948}3949}39503951sub print_header_links {3952my$status=shift;39533954# print out each stylesheet that exist, providing backwards capability3955# for those people who defined $stylesheet in a config file3956if(defined$stylesheet) {3957print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3958}else{3959foreachmy$stylesheet(@stylesheets) {3960next unless$stylesheet;3961print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3962}3963}3964 print_feed_meta()3965if($statuseq'200 OK');3966if(defined$favicon) {3967printqq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);3968}3969}39703971sub print_nav_breadcrumbs_path {3972my$dirprefix=undef;3973while(my$part=shift) {3974$dirprefix.="/"ifdefined$dirprefix;3975$dirprefix.=$part;3976print$cgi->a({-href => href(project =>undef,3977 project_filter =>$dirprefix,3978 action =>"project_list")},3979 esc_html($part)) ." / ";3980}3981}39823983sub print_nav_breadcrumbs {3984my%opts=@_;39853986print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3987if(defined$project) {3988my@dirname=split'/',$project;3989my$projectbasename=pop@dirname;3990 print_nav_breadcrumbs_path(@dirname);3991print$cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));3992if(defined$action) {3993my$action_print=$action;3994if(defined$opts{-action_extra}) {3995$action_print=$cgi->a({-href => href(action=>$action)},3996$action);3997}3998print" /$action_print";3999}4000if(defined$opts{-action_extra}) {4001print" /$opts{-action_extra}";4002}4003print"\n";4004}elsif(defined$project_filter) {4005 print_nav_breadcrumbs_path(split'/',$project_filter);4006}4007}40084009sub print_search_form {4010if(!defined$searchtext) {4011$searchtext="";4012}4013my$search_hash;4014if(defined$hash_base) {4015$search_hash=$hash_base;4016}elsif(defined$hash) {4017$search_hash=$hash;4018}else{4019$search_hash="HEAD";4020}4021my$action=$my_uri;4022my$use_pathinfo= gitweb_check_feature('pathinfo');4023if($use_pathinfo) {4024$action.="/".esc_url($project);4025}4026print$cgi->startform(-method=>"get", -action =>$action) .4027"<div class=\"search\">\n".4028(!$use_pathinfo&&4029$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .4030$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".4031$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".4032$cgi->popup_menu(-name =>'st', -default=>'commit',4033-values=> ['commit','grep','author','committer','pickaxe']) .4034$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .4035" search:\n",4036$cgi->textfield(-name =>"s", -value =>$searchtext, -override =>1) ."\n".4037"<span title=\"Extended regular expression\">".4038$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',4039-checked =>$search_use_regexp) .4040"</span>".4041"</div>".4042$cgi->end_form() ."\n";4043}40444045sub git_header_html {4046my$status=shift||"200 OK";4047my$expires=shift;4048my%opts=@_;40494050my$title= get_page_title();4051my$content_type= get_content_type_html();4052print$cgi->header(-type=>$content_type, -charset =>'utf-8',4053-status=>$status, -expires =>$expires)4054unless($opts{'-no_http_header'});4055my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';4056print<<EOF;4057<?xml version="1.0" encoding="utf-8"?>4058<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">4059<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">4060<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->4061<!-- git core binaries version$git_version-->4062<head>4063<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>4064<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>4065<meta name="robots" content="index, nofollow"/>4066<title>$title</title>4067EOF4068# the stylesheet, favicon etc urls won't work correctly with path_info4069# unless we set the appropriate base URL4070if($ENV{'PATH_INFO'}) {4071print"<base href=\"".esc_url($base_url)."\"/>\n";4072}4073 print_header_links($status);40744075if(defined$site_html_head_string) {4076print to_utf8($site_html_head_string);4077}40784079print"</head>\n".4080"<body>\n";40814082if(defined$site_header&& -f $site_header) {4083 insert_file($site_header);4084}40854086print"<div class=\"page_header\">\n";4087if(defined$logo) {4088print$cgi->a({-href => esc_url($logo_url),4089-title =>$logo_label},4090$cgi->img({-src => esc_url($logo),4091-width =>72, -height =>27,4092-alt =>"git",4093-class=>"logo"}));4094}4095 print_nav_breadcrumbs(%opts);4096print"</div>\n";40974098my$have_search= gitweb_check_feature('search');4099if(defined$project&&$have_search) {4100 print_search_form();4101}4102}41034104sub git_footer_html {4105my$feed_class='rss_logo';41064107print"<div class=\"page_footer\">\n";4108if(defined$project) {4109my$descr= git_get_project_description($project);4110if(defined$descr) {4111print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";4112}41134114my%href_params= get_feed_info();4115if(!%href_params) {4116$feed_class.=' generic';4117}4118$href_params{'-title'} ||='log';41194120foreachmy$format(qw(RSS Atom)) {4121$href_params{'action'} =lc($format);4122print$cgi->a({-href => href(%href_params),4123-title =>"$href_params{'-title'}$formatfeed",4124-class=>$feed_class},$format)."\n";4125}41264127}else{4128print$cgi->a({-href => href(project=>undef, action=>"opml",4129 project_filter =>$project_filter),4130-class=>$feed_class},"OPML") ." ";4131print$cgi->a({-href => href(project=>undef, action=>"project_index",4132 project_filter =>$project_filter),4133-class=>$feed_class},"TXT") ."\n";4134}4135print"</div>\n";# class="page_footer"41364137if(defined$t0&& gitweb_check_feature('timed')) {4138print"<div id=\"generating_info\">\n";4139print'This page took '.4140'<span id="generating_time" class="time_span">'.4141 tv_interval($t0, [ gettimeofday() ]).4142' seconds </span>'.4143' and '.4144'<span id="generating_cmd">'.4145$number_of_git_cmds.4146'</span> git commands '.4147" to generate.\n";4148print"</div>\n";# class="page_footer"4149}41504151if(defined$site_footer&& -f $site_footer) {4152 insert_file($site_footer);4153}41544155print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;4156if(defined$action&&4157$actioneq'blame_incremental') {4158print qq!<script type="text/javascript">\n!.4159 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.4160 qq!"!. href() .qq!");\n!.4161 qq!</script>\n!;4162}else{4163my($jstimezone,$tz_cookie,$datetime_class) =4164 gitweb_get_feature('javascript-timezone');41654166print qq!<script type="text/javascript">\n!.4167 qq!window.onload = function () {\n!;4168if(gitweb_check_feature('javascript-actions')) {4169print qq! fixLinks();\n!;4170}4171if($jstimezone&&$tz_cookie&&$datetime_class) {4172print qq! var tz_cookie = { name:'$tz_cookie', expires:14, path:'/'};\n!.# in days4173 qq! onloadTZSetup('$jstimezone', tz_cookie,'$datetime_class');\n!;4174}4175print qq!};\n!.4176 qq!</script>\n!;4177}41784179print"</body>\n".4180"</html>";4181}41824183# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])4184# Example: die_error(404, 'Hash not found')4185# By convention, use the following status codes (as defined in RFC 2616):4186# 400: Invalid or missing CGI parameters, or4187# requested object exists but has wrong type.4188# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on4189# this server or project.4190# 404: Requested object/revision/project doesn't exist.4191# 500: The server isn't configured properly, or4192# an internal error occurred (e.g. failed assertions caused by bugs), or4193# an unknown error occurred (e.g. the git binary died unexpectedly).4194# 503: The server is currently unavailable (because it is overloaded,4195# or down for maintenance). Generally, this is a temporary state.4196sub die_error {4197my$status=shift||500;4198my$error= esc_html(shift) ||"Internal Server Error";4199my$extra=shift;4200my%opts=@_;42014202my%http_responses= (4203400=>'400 Bad Request',4204403=>'403 Forbidden',4205404=>'404 Not Found',4206500=>'500 Internal Server Error',4207503=>'503 Service Unavailable',4208);4209 git_header_html($http_responses{$status},undef,%opts);4210print<<EOF;4211<div class="page_body">4212<br /><br />4213$status-$error4214<br />4215EOF4216if(defined$extra) {4217print"<hr />\n".4218"$extra\n";4219}4220print"</div>\n";42214222 git_footer_html();4223goto DONE_GITWEB4224unless($opts{'-error_handler'});4225}42264227## ----------------------------------------------------------------------4228## functions printing or outputting HTML: navigation42294230sub git_print_page_nav {4231my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;4232$extra=''if!defined$extra;# pager or formats42334234my@navs=qw(summary shortlog log commit commitdiff tree);4235if($suppress) {4236@navs=grep{$_ne$suppress}@navs;4237}42384239my%arg=map{$_=> {action=>$_} }@navs;4240if(defined$head) {4241for(qw(commit commitdiff)) {4242$arg{$_}{'hash'} =$head;4243}4244if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {4245for(qw(shortlog log)) {4246$arg{$_}{'hash'} =$head;4247}4248}4249}42504251$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;4252$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;42534254my@actions= gitweb_get_feature('actions');4255my%repl= (4256'%'=>'%',4257'n'=>$project,# project name4258'f'=>$git_dir,# project path within filesystem4259'h'=>$treehead||'',# current hash ('h' parameter)4260'b'=>$treebase||'',# hash base ('hb' parameter)4261);4262while(@actions) {4263my($label,$link,$pos) =splice(@actions,0,3);4264# insert4265@navs=map{$_eq$pos? ($_,$label) :$_}@navs;4266# munch munch4267$link=~s/%([%nfhb])/$repl{$1}/g;4268$arg{$label}{'_href'} =$link;4269}42704271print"<div class=\"page_nav\">\n".4272(join" | ",4273map{$_eq$current?4274$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")4275}@navs);4276print"<br/>\n$extra<br/>\n".4277"</div>\n";4278}42794280# returns a submenu for the nagivation of the refs views (tags, heads,4281# remotes) with the current view disabled and the remotes view only4282# available if the feature is enabled4283sub format_ref_views {4284my($current) =@_;4285my@ref_views=qw{tags heads};4286push@ref_views,'remotes'if gitweb_check_feature('remote_heads');4287returnjoin" | ",map{4288$_eq$current?$_:4289$cgi->a({-href => href(action=>$_)},$_)4290}@ref_views4291}42924293sub format_paging_nav {4294my($action,$page,$has_next_link) =@_;4295my$paging_nav;429642974298if($page>0) {4299$paging_nav.=4300$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .4301" ⋅ ".4302$cgi->a({-href => href(-replay=>1, page=>$page-1),4303-accesskey =>"p", -title =>"Alt-p"},"prev");4304}else{4305$paging_nav.="first ⋅ prev";4306}43074308if($has_next_link) {4309$paging_nav.=" ⋅ ".4310$cgi->a({-href => href(-replay=>1, page=>$page+1),4311-accesskey =>"n", -title =>"Alt-n"},"next");4312}else{4313$paging_nav.=" ⋅ next";4314}43154316return$paging_nav;4317}43184319## ......................................................................4320## functions printing or outputting HTML: div43214322sub git_print_header_div {4323my($action,$title,$hash,$hash_base) =@_;4324my%args= ();43254326$args{'action'} =$action;4327$args{'hash'} =$hashif$hash;4328$args{'hash_base'} =$hash_baseif$hash_base;43294330print"<div class=\"header\">\n".4331$cgi->a({-href => href(%args), -class=>"title"},4332$title?$title:$action) .4333"\n</div>\n";4334}43354336sub format_repo_url {4337my($name,$url) =@_;4338return"<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";4339}43404341# Group output by placing it in a DIV element and adding a header.4342# Options for start_div() can be provided by passing a hash reference as the4343# first parameter to the function.4344# Options to git_print_header_div() can be provided by passing an array4345# reference. This must follow the options to start_div if they are present.4346# The content can be a scalar, which is output as-is, a scalar reference, which4347# is output after html escaping, an IO handle passed either as *handle or4348# *handle{IO}, or a function reference. In the latter case all following4349# parameters will be taken as argument to the content function call.4350sub git_print_section {4351my($div_args,$header_args,$content);4352my$arg=shift;4353if(ref($arg)eq'HASH') {4354$div_args=$arg;4355$arg=shift;4356}4357if(ref($arg)eq'ARRAY') {4358$header_args=$arg;4359$arg=shift;4360}4361$content=$arg;43624363print$cgi->start_div($div_args);4364 git_print_header_div(@$header_args);43654366if(ref($content)eq'CODE') {4367$content->(@_);4368}elsif(ref($content)eq'SCALAR') {4369print esc_html($$content);4370}elsif(ref($content)eq'GLOB'or ref($content)eq'IO::Handle') {4371print<$content>;4372}elsif(!ref($content) &&defined($content)) {4373print$content;4374}43754376print$cgi->end_div;4377}43784379sub format_timestamp_html {4380my$date=shift;4381my$strtime=$date->{'rfc2822'};43824383my(undef,undef,$datetime_class) =4384 gitweb_get_feature('javascript-timezone');4385if($datetime_class) {4386$strtime= qq!<span class="$datetime_class">$strtime</span>!;4387}43884389my$localtime_format='(%02d:%02d%s)';4390if($date->{'hour_local'} <6) {4391$localtime_format='(<span class="atnight">%02d:%02d</span>%s)';4392}4393$strtime.=' '.4394sprintf($localtime_format,4395$date->{'hour_local'},$date->{'minute_local'},$date->{'tz_local'});43964397return$strtime;4398}43994400# Outputs the author name and date in long form4401sub git_print_authorship {4402my$co=shift;4403my%opts=@_;4404my$tag=$opts{-tag} ||'div';4405my$author=$co->{'author_name'};44064407my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});4408print"<$tagclass=\"author_date\">".4409 format_search_author($author,"author", esc_html($author)) .4410" [".format_timestamp_html(\%ad)."]".4411 git_get_avatar($co->{'author_email'}, -pad_before =>1) .4412"</$tag>\n";4413}44144415# Outputs table rows containing the full author or committer information,4416# in the format expected for 'commit' view (& similar).4417# Parameters are a commit hash reference, followed by the list of people4418# to output information for. If the list is empty it defaults to both4419# author and committer.4420sub git_print_authorship_rows {4421my$co=shift;4422# too bad we can't use @people = @_ || ('author', 'committer')4423my@people=@_;4424@people= ('author','committer')unless@people;4425foreachmy$who(@people) {4426my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});4427print"<tr><td>$who</td><td>".4428 format_search_author($co->{"${who}_name"},$who,4429 esc_html($co->{"${who}_name"})) ." ".4430 format_search_author($co->{"${who}_email"},$who,4431 esc_html("<".$co->{"${who}_email"} .">")) .4432"</td><td rowspan=\"2\">".4433 git_get_avatar($co->{"${who}_email"}, -size =>'double') .4434"</td></tr>\n".4435"<tr>".4436"<td></td><td>".4437 format_timestamp_html(\%wd) .4438"</td>".4439"</tr>\n";4440}4441}44424443sub git_print_page_path {4444my$name=shift;4445my$type=shift;4446my$hb=shift;444744484449print"<div class=\"page_path\">";4450print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),4451-title =>'tree root'}, to_utf8("[$project]"));4452print" / ";4453if(defined$name) {4454my@dirname=split'/',$name;4455my$basename=pop@dirname;4456my$fullname='';44574458foreachmy$dir(@dirname) {4459$fullname.= ($fullname?'/':'') .$dir;4460print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,4461 hash_base=>$hb),4462-title =>$fullname}, esc_path($dir));4463print" / ";4464}4465if(defined$type&&$typeeq'blob') {4466print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,4467 hash_base=>$hb),4468-title =>$name}, esc_path($basename));4469}elsif(defined$type&&$typeeq'tree') {4470print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,4471 hash_base=>$hb),4472-title =>$name}, esc_path($basename));4473print" / ";4474}else{4475print esc_path($basename);4476}4477}4478print"<br/></div>\n";4479}44804481sub git_print_log {4482my$log=shift;4483my%opts=@_;44844485if($opts{'-remove_title'}) {4486# remove title, i.e. first line of log4487shift@$log;4488}4489# remove leading empty lines4490while(defined$log->[0] &&$log->[0]eq"") {4491shift@$log;4492}44934494# print log4495my$skip_blank_line=0;4496foreachmy$line(@$log) {4497if($line=~m/^\s*([A-Z][-A-Za-z]*-[Bb]y|C[Cc]): /) {4498if(!$opts{'-remove_signoff'}) {4499print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";4500$skip_blank_line=1;4501}4502next;4503}45044505if($line=~ m,\s*([a-z]*link): (https?://\S+),i) {4506if(!$opts{'-remove_signoff'}) {4507print"<span class=\"signoff\">". esc_html($1) .": ".4508"<a href=\"". esc_html($2) ."\">". esc_html($2) ."</a>".4509"</span><br/>\n";4510$skip_blank_line=1;4511}4512next;4513}45144515# print only one empty line4516# do not print empty line after signoff4517if($lineeq"") {4518next if($skip_blank_line);4519$skip_blank_line=1;4520}else{4521$skip_blank_line=0;4522}45234524print format_log_line_html($line) ."<br/>\n";4525}45264527if($opts{'-final_empty_line'}) {4528# end with single empty line4529print"<br/>\n"unless$skip_blank_line;4530}4531}45324533# return link target (what link points to)4534sub git_get_link_target {4535my$hash=shift;4536my$link_target;45374538# read link4539open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4540orreturn;4541{4542local$/=undef;4543$link_target= <$fd>;4544}4545close$fd4546orreturn;45474548return$link_target;4549}45504551# given link target, and the directory (basedir) the link is in,4552# return target of link relative to top directory (top tree);4553# return undef if it is not possible (including absolute links).4554sub normalize_link_target {4555my($link_target,$basedir) =@_;45564557# absolute symlinks (beginning with '/') cannot be normalized4558return if(substr($link_target,0,1)eq'/');45594560# normalize link target to path from top (root) tree (dir)4561my$path;4562if($basedir) {4563$path=$basedir.'/'.$link_target;4564}else{4565# we are in top (root) tree (dir)4566$path=$link_target;4567}45684569# remove //, /./, and /../4570my@path_parts;4571foreachmy$part(split('/',$path)) {4572# discard '.' and ''4573next if(!$part||$parteq'.');4574# handle '..'4575if($parteq'..') {4576if(@path_parts) {4577pop@path_parts;4578}else{4579# link leads outside repository (outside top dir)4580return;4581}4582}else{4583push@path_parts,$part;4584}4585}4586$path=join('/',@path_parts);45874588return$path;4589}45904591# print tree entry (row of git_tree), but without encompassing <tr> element4592sub git_print_tree_entry {4593my($t,$basedir,$hash_base,$have_blame) =@_;45944595my%base_key= ();4596$base_key{'hash_base'} =$hash_baseifdefined$hash_base;45974598# The format of a table row is: mode list link. Where mode is4599# the mode of the entry, list is the name of the entry, an href,4600# and link is the action links of the entry.46014602print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";4603if(exists$t->{'size'}) {4604print"<td class=\"size\">$t->{'size'}</td>\n";4605}4606if($t->{'type'}eq"blob") {4607print"<td class=\"list\">".4608$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4609 file_name=>"$basedir$t->{'name'}",%base_key),4610-class=>"list"}, esc_path($t->{'name'}));4611if(S_ISLNK(oct$t->{'mode'})) {4612my$link_target= git_get_link_target($t->{'hash'});4613if($link_target) {4614my$norm_target= normalize_link_target($link_target,$basedir);4615if(defined$norm_target) {4616print" -> ".4617$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,4618 file_name=>$norm_target),4619-title =>$norm_target}, esc_path($link_target));4620}else{4621print" -> ". esc_path($link_target);4622}4623}4624}4625print"</td>\n";4626print"<td class=\"link\">";4627print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4628 file_name=>"$basedir$t->{'name'}",%base_key)},4629"blob");4630if($have_blame) {4631print" | ".4632$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4633 file_name=>"$basedir$t->{'name'}",%base_key)},4634"blame");4635}4636if(defined$hash_base) {4637print" | ".4638$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4639 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4640"history");4641}4642print" | ".4643$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4644 file_name=>"$basedir$t->{'name'}")},4645"raw");4646print"</td>\n";46474648}elsif($t->{'type'}eq"tree") {4649print"<td class=\"list\">";4650print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4651 file_name=>"$basedir$t->{'name'}",4652%base_key)},4653 esc_path($t->{'name'}));4654print"</td>\n";4655print"<td class=\"link\">";4656print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4657 file_name=>"$basedir$t->{'name'}",4658%base_key)},4659"tree");4660if(defined$hash_base) {4661print" | ".4662$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4663 file_name=>"$basedir$t->{'name'}")},4664"history");4665}4666print"</td>\n";4667}else{4668# unknown object: we can only present history for it4669# (this includes 'commit' object, i.e. submodule support)4670print"<td class=\"list\">".4671 esc_path($t->{'name'}) .4672"</td>\n";4673print"<td class=\"link\">";4674if(defined$hash_base) {4675print$cgi->a({-href => href(action=>"history",4676 hash_base=>$hash_base,4677 file_name=>"$basedir$t->{'name'}")},4678"history");4679}4680print"</td>\n";4681}4682}46834684## ......................................................................4685## functions printing large fragments of HTML46864687# get pre-image filenames for merge (combined) diff4688sub fill_from_file_info {4689my($diff,@parents) =@_;46904691$diff->{'from_file'} = [ ];4692$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4693for(my$i=0;$i<$diff->{'nparents'};$i++) {4694if($diff->{'status'}[$i]eq'R'||4695$diff->{'status'}[$i]eq'C') {4696$diff->{'from_file'}[$i] =4697 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4698}4699}47004701return$diff;4702}47034704# is current raw difftree line of file deletion4705sub is_deleted {4706my$diffinfo=shift;47074708return$diffinfo->{'to_id'}eq('0' x 40);4709}47104711# does patch correspond to [previous] difftree raw line4712# $diffinfo - hashref of parsed raw diff format4713# $patchinfo - hashref of parsed patch diff format4714# (the same keys as in $diffinfo)4715sub is_patch_split {4716my($diffinfo,$patchinfo) =@_;47174718returndefined$diffinfo&&defined$patchinfo4719&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4720}472147224723sub git_difftree_body {4724my($difftree,$hash,@parents) =@_;4725my($parent) =$parents[0];4726my$have_blame= gitweb_check_feature('blame');4727print"<div class=\"list_head\">\n";4728if($#{$difftree} >10) {4729print(($#{$difftree} +1) ." files changed:\n");4730}4731print"</div>\n";47324733print"<table class=\"".4734(@parents>1?"combined ":"") .4735"diff_tree\">\n";47364737# header only for combined diff in 'commitdiff' view4738my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4739if($has_header) {4740# table header4741print"<thead><tr>\n".4742"<th></th><th></th>\n";# filename, patchN link4743for(my$i=0;$i<@parents;$i++) {4744my$par=$parents[$i];4745print"<th>".4746$cgi->a({-href => href(action=>"commitdiff",4747 hash=>$hash, hash_parent=>$par),4748-title =>'commitdiff to parent number '.4749($i+1) .': '.substr($par,0,7)},4750$i+1) .4751" </th>\n";4752}4753print"</tr></thead>\n<tbody>\n";4754}47554756my$alternate=1;4757my$patchno=0;4758foreachmy$line(@{$difftree}) {4759my$diff= parsed_difftree_line($line);47604761if($alternate) {4762print"<tr class=\"dark\">\n";4763}else{4764print"<tr class=\"light\">\n";4765}4766$alternate^=1;47674768if(exists$diff->{'nparents'}) {# combined diff47694770 fill_from_file_info($diff,@parents)4771unlessexists$diff->{'from_file'};47724773if(!is_deleted($diff)) {4774# file exists in the result (child) commit4775print"<td>".4776$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4777 file_name=>$diff->{'to_file'},4778 hash_base=>$hash),4779-class=>"list"}, esc_path($diff->{'to_file'})) .4780"</td>\n";4781}else{4782print"<td>".4783 esc_path($diff->{'to_file'}) .4784"</td>\n";4785}47864787if($actioneq'commitdiff') {4788# link to patch4789$patchno++;4790print"<td class=\"link\">".4791$cgi->a({-href => href(-anchor=>"patch$patchno")},4792"patch") .4793" | ".4794"</td>\n";4795}47964797my$has_history=0;4798my$not_deleted=0;4799for(my$i=0;$i<$diff->{'nparents'};$i++) {4800my$hash_parent=$parents[$i];4801my$from_hash=$diff->{'from_id'}[$i];4802my$from_path=$diff->{'from_file'}[$i];4803my$status=$diff->{'status'}[$i];48044805$has_history||= ($statusne'A');4806$not_deleted||= ($statusne'D');48074808if($statuseq'A') {4809print"<td class=\"link\"align=\"right\"> | </td>\n";4810}elsif($statuseq'D') {4811print"<td class=\"link\">".4812$cgi->a({-href => href(action=>"blob",4813 hash_base=>$hash,4814 hash=>$from_hash,4815 file_name=>$from_path)},4816"blob". ($i+1)) .4817" | </td>\n";4818}else{4819if($diff->{'to_id'}eq$from_hash) {4820print"<td class=\"link nochange\">";4821}else{4822print"<td class=\"link\">";4823}4824print$cgi->a({-href => href(action=>"blobdiff",4825 hash=>$diff->{'to_id'},4826 hash_parent=>$from_hash,4827 hash_base=>$hash,4828 hash_parent_base=>$hash_parent,4829 file_name=>$diff->{'to_file'},4830 file_parent=>$from_path)},4831"diff". ($i+1)) .4832" | </td>\n";4833}4834}48354836print"<td class=\"link\">";4837if($not_deleted) {4838print$cgi->a({-href => href(action=>"blob",4839 hash=>$diff->{'to_id'},4840 file_name=>$diff->{'to_file'},4841 hash_base=>$hash)},4842"blob");4843print" | "if($has_history);4844}4845if($has_history) {4846print$cgi->a({-href => href(action=>"history",4847 file_name=>$diff->{'to_file'},4848 hash_base=>$hash)},4849"history");4850}4851print"</td>\n";48524853print"</tr>\n";4854next;# instead of 'else' clause, to avoid extra indent4855}4856# else ordinary diff48574858my($to_mode_oct,$to_mode_str,$to_file_type);4859my($from_mode_oct,$from_mode_str,$from_file_type);4860if($diff->{'to_mode'}ne('0' x 6)) {4861$to_mode_oct=oct$diff->{'to_mode'};4862if(S_ISREG($to_mode_oct)) {# only for regular file4863$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4864}4865$to_file_type= file_type($diff->{'to_mode'});4866}4867if($diff->{'from_mode'}ne('0' x 6)) {4868$from_mode_oct=oct$diff->{'from_mode'};4869if(S_ISREG($from_mode_oct)) {# only for regular file4870$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4871}4872$from_file_type= file_type($diff->{'from_mode'});4873}48744875if($diff->{'status'}eq"A") {# created4876my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4877$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4878$mode_chng.="]</span>";4879print"<td>";4880print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4881 hash_base=>$hash, file_name=>$diff->{'file'}),4882-class=>"list"}, esc_path($diff->{'file'}));4883print"</td>\n";4884print"<td>$mode_chng</td>\n";4885print"<td class=\"link\">";4886if($actioneq'commitdiff') {4887# link to patch4888$patchno++;4889print$cgi->a({-href => href(-anchor=>"patch$patchno")},4890"patch") .4891" | ";4892}4893print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4894 hash_base=>$hash, file_name=>$diff->{'file'})},4895"blob");4896print"</td>\n";48974898}elsif($diff->{'status'}eq"D") {# deleted4899my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4900print"<td>";4901print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4902 hash_base=>$parent, file_name=>$diff->{'file'}),4903-class=>"list"}, esc_path($diff->{'file'}));4904print"</td>\n";4905print"<td>$mode_chng</td>\n";4906print"<td class=\"link\">";4907if($actioneq'commitdiff') {4908# link to patch4909$patchno++;4910print$cgi->a({-href => href(-anchor=>"patch$patchno")},4911"patch") .4912" | ";4913}4914print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4915 hash_base=>$parent, file_name=>$diff->{'file'})},4916"blob") ." | ";4917if($have_blame) {4918print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4919 file_name=>$diff->{'file'})},4920"blame") ." | ";4921}4922print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4923 file_name=>$diff->{'file'})},4924"history");4925print"</td>\n";49264927}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4928my$mode_chnge="";4929if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4930$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4931if($from_file_typene$to_file_type) {4932$mode_chnge.=" from$from_file_typeto$to_file_type";4933}4934if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4935if($from_mode_str&&$to_mode_str) {4936$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4937}elsif($to_mode_str) {4938$mode_chnge.=" mode:$to_mode_str";4939}4940}4941$mode_chnge.="]</span>\n";4942}4943print"<td>";4944print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4945 hash_base=>$hash, file_name=>$diff->{'file'}),4946-class=>"list"}, esc_path($diff->{'file'}));4947print"</td>\n";4948print"<td>$mode_chnge</td>\n";4949print"<td class=\"link\">";4950if($actioneq'commitdiff') {4951# link to patch4952$patchno++;4953print$cgi->a({-href => href(-anchor=>"patch$patchno")},4954"patch") .4955" | ";4956}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4957# "commit" view and modified file (not onlu mode changed)4958print$cgi->a({-href => href(action=>"blobdiff",4959 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4960 hash_base=>$hash, hash_parent_base=>$parent,4961 file_name=>$diff->{'file'})},4962"diff") .4963" | ";4964}4965print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4966 hash_base=>$hash, file_name=>$diff->{'file'})},4967"blob") ." | ";4968if($have_blame) {4969print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4970 file_name=>$diff->{'file'})},4971"blame") ." | ";4972}4973print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4974 file_name=>$diff->{'file'})},4975"history");4976print"</td>\n";49774978}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4979my%status_name= ('R'=>'moved','C'=>'copied');4980my$nstatus=$status_name{$diff->{'status'}};4981my$mode_chng="";4982if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4983# mode also for directories, so we cannot use $to_mode_str4984$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4985}4986print"<td>".4987$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4988 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4989-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4990"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4991$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4992 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4993-class=>"list"}, esc_path($diff->{'from_file'})) .4994" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4995"<td class=\"link\">";4996if($actioneq'commitdiff') {4997# link to patch4998$patchno++;4999print$cgi->a({-href => href(-anchor=>"patch$patchno")},5000"patch") .5001" | ";5002}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {5003# "commit" view and modified file (not only pure rename or copy)5004print$cgi->a({-href => href(action=>"blobdiff",5005 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},5006 hash_base=>$hash, hash_parent_base=>$parent,5007 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},5008"diff") .5009" | ";5010}5011print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},5012 hash_base=>$parent, file_name=>$diff->{'to_file'})},5013"blob") ." | ";5014if($have_blame) {5015print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,5016 file_name=>$diff->{'to_file'})},5017"blame") ." | ";5018}5019print$cgi->a({-href => href(action=>"history", hash_base=>$hash,5020 file_name=>$diff->{'to_file'})},5021"history");5022print"</td>\n";50235024}# we should not encounter Unmerged (U) or Unknown (X) status5025print"</tr>\n";5026}5027print"</tbody>"if$has_header;5028print"</table>\n";5029}50305031# Print context lines and then rem/add lines in a side-by-side manner.5032sub print_sidebyside_diff_lines {5033my($ctx,$rem,$add) =@_;50345035# print context block before add/rem block5036if(@$ctx) {5037print join'',5038'<div class="chunk_block ctx">',5039'<div class="old">',5040@$ctx,5041'</div>',5042'<div class="new">',5043@$ctx,5044'</div>',5045'</div>';5046}50475048if(!@$add) {5049# pure removal5050print join'',5051'<div class="chunk_block rem">',5052'<div class="old">',5053@$rem,5054'</div>',5055'</div>';5056}elsif(!@$rem) {5057# pure addition5058print join'',5059'<div class="chunk_block add">',5060'<div class="new">',5061@$add,5062'</div>',5063'</div>';5064}else{5065print join'',5066'<div class="chunk_block chg">',5067'<div class="old">',5068@$rem,5069'</div>',5070'<div class="new">',5071@$add,5072'</div>',5073'</div>';5074}5075}50765077# Print context lines and then rem/add lines in inline manner.5078sub print_inline_diff_lines {5079my($ctx,$rem,$add) =@_;50805081print@$ctx,@$rem,@$add;5082}50835084# Format removed and added line, mark changed part and HTML-format them.5085# Implementation is based on contrib/diff-highlight5086sub format_rem_add_lines_pair {5087my($rem,$add,$num_parents) =@_;50885089# We need to untabify lines before split()'ing them;5090# otherwise offsets would be invalid.5091chomp$rem;5092chomp$add;5093$rem= untabify($rem);5094$add= untabify($add);50955096my@rem=split(//,$rem);5097my@add=split(//,$add);5098my($esc_rem,$esc_add);5099# Ignore leading +/- characters for each parent.5100my($prefix_len,$suffix_len) = ($num_parents,0);5101my($prefix_has_nonspace,$suffix_has_nonspace);51025103my$shorter= (@rem<@add) ?@rem:@add;5104while($prefix_len<$shorter) {5105last if($rem[$prefix_len]ne$add[$prefix_len]);51065107$prefix_has_nonspace=1if($rem[$prefix_len] !~/\s/);5108$prefix_len++;5109}51105111while($prefix_len+$suffix_len<$shorter) {5112last if($rem[-1-$suffix_len]ne$add[-1-$suffix_len]);51135114$suffix_has_nonspace=1if($rem[-1-$suffix_len] !~/\s/);5115$suffix_len++;5116}51175118# Mark lines that are different from each other, but have some common5119# part that isn't whitespace. If lines are completely different, don't5120# mark them because that would make output unreadable, especially if5121# diff consists of multiple lines.5122if($prefix_has_nonspace||$suffix_has_nonspace) {5123$esc_rem= esc_html_hl_regions($rem,'marked',5124[$prefix_len,@rem-$suffix_len], -nbsp=>1);5125$esc_add= esc_html_hl_regions($add,'marked',5126[$prefix_len,@add-$suffix_len], -nbsp=>1);5127}else{5128$esc_rem= esc_html($rem, -nbsp=>1);5129$esc_add= esc_html($add, -nbsp=>1);5130}51315132return format_diff_line(\$esc_rem,'rem'),5133 format_diff_line(\$esc_add,'add');5134}51355136# HTML-format diff context, removed and added lines.5137sub format_ctx_rem_add_lines {5138my($ctx,$rem,$add,$num_parents) =@_;5139my(@new_ctx,@new_rem,@new_add);5140my$can_highlight=0;5141my$is_combined= ($num_parents>1);51425143# Highlight if every removed line has a corresponding added line.5144if(@$add>0&&@$add==@$rem) {5145$can_highlight=1;51465147# Highlight lines in combined diff only if the chunk contains5148# diff between the same version, e.g.5149#5150# - a5151# - b5152# + c5153# + d5154#5155# Otherwise the highlightling would be confusing.5156if($is_combined) {5157for(my$i=0;$i<@$add;$i++) {5158my$prefix_rem=substr($rem->[$i],0,$num_parents);5159my$prefix_add=substr($add->[$i],0,$num_parents);51605161$prefix_rem=~s/-/+/g;51625163if($prefix_remne$prefix_add) {5164$can_highlight=0;5165last;5166}5167}5168}5169}51705171if($can_highlight) {5172for(my$i=0;$i<@$add;$i++) {5173my($line_rem,$line_add) = format_rem_add_lines_pair(5174$rem->[$i],$add->[$i],$num_parents);5175push@new_rem,$line_rem;5176push@new_add,$line_add;5177}5178}else{5179@new_rem=map{ format_diff_line($_,'rem') }@$rem;5180@new_add=map{ format_diff_line($_,'add') }@$add;5181}51825183@new_ctx=map{ format_diff_line($_,'ctx') }@$ctx;51845185return(\@new_ctx, \@new_rem, \@new_add);5186}51875188# Print context lines and then rem/add lines.5189sub print_diff_lines {5190my($ctx,$rem,$add,$diff_style,$num_parents) =@_;5191my$is_combined=$num_parents>1;51925193($ctx,$rem,$add) = format_ctx_rem_add_lines($ctx,$rem,$add,5194$num_parents);51955196if($diff_styleeq'sidebyside'&& !$is_combined) {5197 print_sidebyside_diff_lines($ctx,$rem,$add);5198}else{5199# default 'inline' style and unknown styles5200 print_inline_diff_lines($ctx,$rem,$add);5201}5202}52035204sub print_diff_chunk {5205my($diff_style,$num_parents,$from,$to,@chunk) =@_;5206my(@ctx,@rem,@add);52075208# The class of the previous line.5209my$prev_class='';52105211return unless@chunk;52125213# incomplete last line might be among removed or added lines,5214# or both, or among context lines: find which5215for(my$i=1;$i<@chunk;$i++) {5216if($chunk[$i][0]eq'incomplete') {5217$chunk[$i][0] =$chunk[$i-1][0];5218}5219}52205221# guardian5222push@chunk, ["",""];52235224foreachmy$line_info(@chunk) {5225my($class,$line) =@$line_info;52265227# print chunk headers5228if($class&&$classeq'chunk_header') {5229print format_diff_line($line,$class,$from,$to);5230next;5231}52325233## print from accumulator when have some add/rem lines or end5234# of chunk (flush context lines), or when have add and rem5235# lines and new block is reached (otherwise add/rem lines could5236# be reordered)5237if(!$class|| ((@rem||@add) &&$classeq'ctx') ||5238(@rem&&@add&&$classne$prev_class)) {5239 print_diff_lines(\@ctx, \@rem, \@add,5240$diff_style,$num_parents);5241@ctx=@rem=@add= ();5242}52435244## adding lines to accumulator5245# guardian value5246last unless$line;5247# rem, add or change5248if($classeq'rem') {5249push@rem,$line;5250}elsif($classeq'add') {5251push@add,$line;5252}5253# context line5254if($classeq'ctx') {5255push@ctx,$line;5256}52575258$prev_class=$class;5259}5260}52615262sub git_patchset_body {5263my($fd,$diff_style,$difftree,$hash,@hash_parents) =@_;5264my($hash_parent) =$hash_parents[0];52655266my$is_combined= (@hash_parents>1);5267my$patch_idx=0;5268my$patch_number=0;5269my$patch_line;5270my$diffinfo;5271my$to_name;5272my(%from,%to);5273my@chunk;# for side-by-side diff52745275print"<div class=\"patchset\">\n";52765277# skip to first patch5278while($patch_line= <$fd>) {5279chomp$patch_line;52805281last if($patch_line=~m/^diff /);5282}52835284 PATCH:5285while($patch_line) {52865287# parse "git diff" header line5288if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {5289# $1 is from_name, which we do not use5290$to_name= unquote($2);5291$to_name=~s!^b/!!;5292}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {5293# $1 is 'cc' or 'combined', which we do not use5294$to_name= unquote($2);5295}else{5296$to_name=undef;5297}52985299# check if current patch belong to current raw line5300# and parse raw git-diff line if needed5301if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {5302# this is continuation of a split patch5303print"<div class=\"patch cont\">\n";5304}else{5305# advance raw git-diff output if needed5306$patch_idx++ifdefined$diffinfo;53075308# read and prepare patch information5309$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);53105311# compact combined diff output can have some patches skipped5312# find which patch (using pathname of result) we are at now;5313if($is_combined) {5314while($to_namene$diffinfo->{'to_file'}) {5315print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".5316 format_diff_cc_simplified($diffinfo,@hash_parents) .5317"</div>\n";# class="patch"53185319$patch_idx++;5320$patch_number++;53215322last if$patch_idx>$#$difftree;5323$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);5324}5325}53265327# modifies %from, %to hashes5328 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);53295330# this is first patch for raw difftree line with $patch_idx index5331# we index @$difftree array from 0, but number patches from 15332print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";5333}53345335# git diff header5336#assert($patch_line =~ m/^diff /) if DEBUG;5337#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed5338$patch_number++;5339# print "git diff" header5340print format_git_diff_header_line($patch_line,$diffinfo,5341 \%from, \%to);53425343# print extended diff header5344print"<div class=\"diff extended_header\">\n";5345 EXTENDED_HEADER:5346while($patch_line= <$fd>) {5347chomp$patch_line;53485349last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);53505351print format_extended_diff_header_line($patch_line,$diffinfo,5352 \%from, \%to);5353}5354print"</div>\n";# class="diff extended_header"53555356# from-file/to-file diff header5357if(!$patch_line) {5358print"</div>\n";# class="patch"5359last PATCH;5360}5361next PATCH if($patch_line=~m/^diff /);5362#assert($patch_line =~ m/^---/) if DEBUG;53635364my$last_patch_line=$patch_line;5365$patch_line= <$fd>;5366chomp$patch_line;5367#assert($patch_line =~ m/^\+\+\+/) if DEBUG;53685369print format_diff_from_to_header($last_patch_line,$patch_line,5370$diffinfo, \%from, \%to,5371@hash_parents);53725373# the patch itself5374 LINE:5375while($patch_line= <$fd>) {5376chomp$patch_line;53775378next PATCH if($patch_line=~m/^diff /);53795380my$class= diff_line_class($patch_line, \%from, \%to);53815382if($classeq'chunk_header') {5383 print_diff_chunk($diff_style,scalar@hash_parents, \%from, \%to,@chunk);5384@chunk= ();5385}53865387push@chunk, [$class,$patch_line];5388}53895390}continue{5391if(@chunk) {5392 print_diff_chunk($diff_style,scalar@hash_parents, \%from, \%to,@chunk);5393@chunk= ();5394}5395print"</div>\n";# class="patch"5396}53975398# for compact combined (--cc) format, with chunk and patch simplification5399# the patchset might be empty, but there might be unprocessed raw lines5400for(++$patch_idxif$patch_number>0;5401$patch_idx<@$difftree;5402++$patch_idx) {5403# read and prepare patch information5404$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);54055406# generate anchor for "patch" links in difftree / whatchanged part5407print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".5408 format_diff_cc_simplified($diffinfo,@hash_parents) .5409"</div>\n";# class="patch"54105411$patch_number++;5412}54135414if($patch_number==0) {5415if(@hash_parents>1) {5416print"<div class=\"diff nodifferences\">Trivial merge</div>\n";5417}else{5418print"<div class=\"diff nodifferences\">No differences found</div>\n";5419}5420}54215422print"</div>\n";# class="patchset"5423}54245425# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54265427sub git_project_search_form {5428my($searchtext,$search_use_regexp) =@_;54295430my$limit='';5431if($project_filter) {5432$limit=" in '$project_filter/'";5433}54345435print"<div class=\"projsearch\">\n";5436print$cgi->startform(-method=>'get', -action =>$my_uri) .5437$cgi->hidden(-name =>'a', -value =>'project_list') ."\n";5438print$cgi->hidden(-name =>'pf', -value =>$project_filter)."\n"5439if(defined$project_filter);5440print$cgi->textfield(-name =>'s', -value =>$searchtext,5441-title =>"Search project by name and description$limit",5442-size =>60) ."\n".5443"<span title=\"Extended regular expression\">".5444$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',5445-checked =>$search_use_regexp) .5446"</span>\n".5447$cgi->submit(-name =>'btnS', -value =>'Search') .5448$cgi->end_form() ."\n".5449$cgi->a({-href => href(project =>undef, searchtext =>undef,5450 project_filter =>$project_filter)},5451 esc_html("List all projects$limit")) ."<br />\n";5452print"</div>\n";5453}54545455# entry for given @keys needs filling if at least one of keys in list5456# is not present in %$project_info5457sub project_info_needs_filling {5458my($project_info,@keys) =@_;54595460# return List::MoreUtils::any { !exists $project_info->{$_} } @keys;5461foreachmy$key(@keys) {5462if(!exists$project_info->{$key}) {5463return1;5464}5465}5466return;5467}54685469# fills project list info (age, description, owner, category, forks, etc.)5470# for each project in the list, removing invalid projects from5471# returned list, or fill only specified info.5472#5473# Invalid projects are removed from the returned list if and only if you5474# ask 'age' or 'age_string' to be filled, because they are the only fields5475# that run unconditionally git command that requires repository, and5476# therefore do always check if project repository is invalid.5477#5478# USAGE:5479# * fill_project_list_info(\@project_list, 'descr_long', 'ctags')5480# ensures that 'descr_long' and 'ctags' fields are filled5481# * @project_list = fill_project_list_info(\@project_list)5482# ensures that all fields are filled (and invalid projects removed)5483#5484# NOTE: modifies $projlist, but does not remove entries from it5485sub fill_project_list_info {5486my($projlist,@wanted_keys) =@_;5487my@projects;5488my$filter_set=sub{return@_; };5489if(@wanted_keys) {5490my%wanted_keys=map{$_=>1}@wanted_keys;5491$filter_set=sub{returngrep{$wanted_keys{$_} }@_; };5492}54935494my$show_ctags= gitweb_check_feature('ctags');5495 PROJECT:5496foreachmy$pr(@$projlist) {5497if(project_info_needs_filling($pr,$filter_set->('age','age_string'))) {5498my(@activity) = git_get_last_activity($pr->{'path'});5499unless(@activity) {5500next PROJECT;5501}5502($pr->{'age'},$pr->{'age_string'}) =@activity;5503}5504if(project_info_needs_filling($pr,$filter_set->('descr','descr_long'))) {5505my$descr= git_get_project_description($pr->{'path'}) ||"";5506$descr= to_utf8($descr);5507$pr->{'descr_long'} =$descr;5508$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);5509}5510if(project_info_needs_filling($pr,$filter_set->('owner'))) {5511$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";5512}5513if($show_ctags&&5514 project_info_needs_filling($pr,$filter_set->('ctags'))) {5515$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});5516}5517if($projects_list_group_categories&&5518 project_info_needs_filling($pr,$filter_set->('category'))) {5519my$cat= git_get_project_category($pr->{'path'}) ||5520$project_list_default_category;5521$pr->{'category'} = to_utf8($cat);5522}55235524push@projects,$pr;5525}55265527return@projects;5528}55295530sub sort_projects_list {5531my($projlist,$order) =@_;5532my@projects;55335534my%order_info= (5535 project => { key =>'path', type =>'str'},5536 descr => { key =>'descr_long', type =>'str'},5537 owner => { key =>'owner', type =>'str'},5538 age => { key =>'age', type =>'num'}5539);5540my$oi=$order_info{$order};5541return@$projlistunlessdefined$oi;5542if($oi->{'type'}eq'str') {5543@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@$projlist;5544}else{5545@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@$projlist;5546}55475548return@projects;5549}55505551# returns a hash of categories, containing the list of project5552# belonging to each category5553sub build_projlist_by_category {5554my($projlist,$from,$to) =@_;5555my%categories;55565557$from=0unlessdefined$from;5558$to=$#$projlistif(!defined$to||$#$projlist<$to);55595560for(my$i=$from;$i<=$to;$i++) {5561my$pr=$projlist->[$i];5562push@{$categories{$pr->{'category'} }},$pr;5563}55645565returnwantarray?%categories: \%categories;5566}55675568# print 'sort by' <th> element, generating 'sort by $name' replay link5569# if that order is not selected5570sub print_sort_th {5571print format_sort_th(@_);5572}55735574sub format_sort_th {5575my($name,$order,$header) =@_;5576my$sort_th="";5577$header||=ucfirst($name);55785579if($ordereq$name) {5580$sort_th.="<th>$header</th>\n";5581}else{5582$sort_th.="<th>".5583$cgi->a({-href => href(-replay=>1, order=>$name),5584-class=>"header"},$header) .5585"</th>\n";5586}55875588return$sort_th;5589}55905591sub git_project_list_rows {5592my($projlist,$from,$to,$check_forks) =@_;55935594$from=0unlessdefined$from;5595$to=$#$projlistif(!defined$to||$#$projlist<$to);55965597my$alternate=1;5598for(my$i=$from;$i<=$to;$i++) {5599my$pr=$projlist->[$i];56005601if($alternate) {5602print"<tr class=\"dark\">\n";5603}else{5604print"<tr class=\"light\">\n";5605}5606$alternate^=1;56075608if($check_forks) {5609print"<td>";5610if($pr->{'forks'}) {5611my$nforks=scalar@{$pr->{'forks'}};5612if($nforks>0) {5613print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),5614-title =>"$nforksforks"},"+");5615}else{5616print$cgi->span({-title =>"$nforksforks"},"+");5617}5618}5619print"</td>\n";5620}5621print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5622-class=>"list"},5623 esc_html_match_hl($pr->{'path'},$search_regexp)) .5624"</td>\n".5625"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5626-class=>"list",5627-title =>$pr->{'descr_long'}},5628$search_regexp5629? esc_html_match_hl_chopped($pr->{'descr_long'},5630$pr->{'descr'},$search_regexp)5631: esc_html($pr->{'descr'})) .5632"</td>\n";5633unless($omit_owner) {5634print"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";5635}5636unless($omit_age_column) {5637print"<td class=\"". age_class($pr->{'age'}) ."\">".5638(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n";5639}5640print"<td class=\"link\">".5641$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".5642$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".5643$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".5644$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .5645($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .5646"</td>\n".5647"</tr>\n";5648}5649}56505651sub git_project_list_body {5652# actually uses global variable $project5653my($projlist,$order,$from,$to,$extra,$no_header) =@_;5654my@projects=@$projlist;56555656my$check_forks= gitweb_check_feature('forks');5657my$show_ctags= gitweb_check_feature('ctags');5658my$tagfilter=$show_ctags?$input_params{'ctag'} :undef;5659$check_forks=undef5660if($tagfilter||$search_regexp);56615662# filtering out forks before filling info allows to do less work5663@projects= filter_forks_from_projects_list(\@projects)5664if($check_forks);5665# search_projects_list pre-fills required info5666@projects= search_projects_list(\@projects,5667'search_regexp'=>$search_regexp,5668'tagfilter'=>$tagfilter)5669if($tagfilter||$search_regexp);5670# fill the rest5671my@all_fields= ('descr','descr_long','ctags','category');5672push@all_fields, ('age','age_string')unless($omit_age_column);5673push@all_fields,'owner'unless($omit_owner);5674@projects= fill_project_list_info(\@projects,@all_fields);56755676$order||=$default_projects_order;5677$from=0unlessdefined$from;5678$to=$#projectsif(!defined$to||$#projects<$to);56795680# short circuit5681if($from>$to) {5682print"<center>\n".5683"<b>No such projects found</b><br />\n".5684"Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".5685"</center>\n<br />\n";5686return;5687}56885689@projects= sort_projects_list(\@projects,$order);56905691if($show_ctags) {5692my$ctags= git_gather_all_ctags(\@projects);5693my$cloud= git_populate_project_tagcloud($ctags);5694print git_show_project_tagcloud($cloud,64);5695}56965697print"<table class=\"project_list\">\n";5698unless($no_header) {5699print"<tr>\n";5700if($check_forks) {5701print"<th></th>\n";5702}5703 print_sort_th('project',$order,'Project');5704 print_sort_th('descr',$order,'Description');5705 print_sort_th('owner',$order,'Owner')unless$omit_owner;5706 print_sort_th('age',$order,'Last Change')unless$omit_age_column;5707print"<th></th>\n".# for links5708"</tr>\n";5709}57105711if($projects_list_group_categories) {5712# only display categories with projects in the $from-$to window5713@projects=sort{$a->{'category'}cmp$b->{'category'}}@projects[$from..$to];5714my%categories= build_projlist_by_category(\@projects,$from,$to);5715foreachmy$cat(sort keys%categories) {5716unless($cateq"") {5717print"<tr>\n";5718if($check_forks) {5719print"<td></td>\n";5720}5721print"<td class=\"category\"colspan=\"5\">".esc_html($cat)."</td>\n";5722print"</tr>\n";5723}57245725 git_project_list_rows($categories{$cat},undef,undef,$check_forks);5726}5727}else{5728 git_project_list_rows(\@projects,$from,$to,$check_forks);5729}57305731if(defined$extra) {5732print"<tr>\n";5733if($check_forks) {5734print"<td></td>\n";5735}5736print"<td colspan=\"5\">$extra</td>\n".5737"</tr>\n";5738}5739print"</table>\n";5740}57415742sub git_log_body {5743# uses global variable $project5744my($commitlist,$from,$to,$refs,$extra) =@_;57455746$from=0unlessdefined$from;5747$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);57485749for(my$i=0;$i<=$to;$i++) {5750my%co= %{$commitlist->[$i]};5751next if!%co;5752my$commit=$co{'id'};5753my$ref= format_ref_marker($refs,$commit);5754 git_print_header_div('commit',5755"<span class=\"age\">$co{'age_string'}</span>".5756 esc_html($co{'title'}) .$ref,5757$commit);5758print"<div class=\"title_text\">\n".5759"<div class=\"log_link\">\n".5760$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5761" | ".5762$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5763" | ".5764$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5765"<br/>\n".5766"</div>\n";5767 git_print_authorship(\%co, -tag =>'span');5768print"<br/>\n</div>\n";57695770print"<div class=\"log_body\">\n";5771 git_print_log($co{'comment'}, -final_empty_line=>1);5772print"</div>\n";5773}5774if($extra) {5775print"<div class=\"page_nav\">\n";5776print"$extra\n";5777print"</div>\n";5778}5779}57805781sub git_shortlog_body {5782# uses global variable $project5783my($commitlist,$from,$to,$refs,$extra) =@_;57845785$from=0unlessdefined$from;5786$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);57875788print"<table class=\"shortlog\">\n";5789my$alternate=1;5790for(my$i=$from;$i<=$to;$i++) {5791my%co= %{$commitlist->[$i]};5792my$commit=$co{'id'};5793my$ref= format_ref_marker($refs,$commit);5794if($alternate) {5795print"<tr class=\"dark\">\n";5796}else{5797print"<tr class=\"light\">\n";5798}5799$alternate^=1;5800# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .5801print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5802 format_author_html('td', \%co,10) ."<td>";5803print format_subject_html($co{'title'},$co{'title_short'},5804 href(action=>"commit", hash=>$commit),$ref);5805print"</td>\n".5806"<td class=\"link\">".5807$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".5808$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".5809$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");5810my$snapshot_links= format_snapshot_links($commit);5811if(defined$snapshot_links) {5812print" | ".$snapshot_links;5813}5814print"</td>\n".5815"</tr>\n";5816}5817if(defined$extra) {5818print"<tr>\n".5819"<td colspan=\"4\">$extra</td>\n".5820"</tr>\n";5821}5822print"</table>\n";5823}58245825sub git_history_body {5826# Warning: assumes constant type (blob or tree) during history5827my($commitlist,$from,$to,$refs,$extra,5828$file_name,$file_hash,$ftype) =@_;58295830$from=0unlessdefined$from;5831$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});58325833print"<table class=\"history\">\n";5834my$alternate=1;5835for(my$i=$from;$i<=$to;$i++) {5836my%co= %{$commitlist->[$i]};5837if(!%co) {5838next;5839}5840my$commit=$co{'id'};58415842my$ref= format_ref_marker($refs,$commit);58435844if($alternate) {5845print"<tr class=\"dark\">\n";5846}else{5847print"<tr class=\"light\">\n";5848}5849$alternate^=1;5850print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5851# shortlog: format_author_html('td', \%co, 10)5852 format_author_html('td', \%co,15,3) ."<td>";5853# originally git_history used chop_str($co{'title'}, 50)5854print format_subject_html($co{'title'},$co{'title_short'},5855 href(action=>"commit", hash=>$commit),$ref);5856print"</td>\n".5857"<td class=\"link\">".5858$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".5859$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");58605861if($ftypeeq'blob') {5862my$blob_current=$file_hash;5863my$blob_parent= git_get_hash_by_path($commit,$file_name);5864if(defined$blob_current&&defined$blob_parent&&5865$blob_currentne$blob_parent) {5866print" | ".5867$cgi->a({-href => href(action=>"blobdiff",5868 hash=>$blob_current, hash_parent=>$blob_parent,5869 hash_base=>$hash_base, hash_parent_base=>$commit,5870 file_name=>$file_name)},5871"diff to current");5872}5873}5874print"</td>\n".5875"</tr>\n";5876}5877if(defined$extra) {5878print"<tr>\n".5879"<td colspan=\"4\">$extra</td>\n".5880"</tr>\n";5881}5882print"</table>\n";5883}58845885sub git_tags_body {5886# uses global variable $project5887my($taglist,$from,$to,$extra) =@_;5888$from=0unlessdefined$from;5889$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);58905891print"<table class=\"tags\">\n";5892my$alternate=1;5893for(my$i=$from;$i<=$to;$i++) {5894my$entry=$taglist->[$i];5895my%tag=%$entry;5896my$comment=$tag{'subject'};5897my$comment_short;5898if(defined$comment) {5899$comment_short= chop_str($comment,30,5);5900}5901if($alternate) {5902print"<tr class=\"dark\">\n";5903}else{5904print"<tr class=\"light\">\n";5905}5906$alternate^=1;5907if(defined$tag{'age'}) {5908print"<td><i>$tag{'age'}</i></td>\n";5909}else{5910print"<td></td>\n";5911}5912print"<td>".5913$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),5914-class=>"list name"}, esc_html($tag{'name'})) .5915"</td>\n".5916"<td>";5917if(defined$comment) {5918print format_subject_html($comment,$comment_short,5919 href(action=>"tag", hash=>$tag{'id'}));5920}5921print"</td>\n".5922"<td class=\"selflink\">";5923if($tag{'type'}eq"tag") {5924print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");5925}else{5926print" ";5927}5928print"</td>\n".5929"<td class=\"link\">"." | ".5930$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});5931if($tag{'reftype'}eq"commit") {5932print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .5933" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");5934}elsif($tag{'reftype'}eq"blob") {5935print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");5936}5937print"</td>\n".5938"</tr>";5939}5940if(defined$extra) {5941print"<tr>\n".5942"<td colspan=\"5\">$extra</td>\n".5943"</tr>\n";5944}5945print"</table>\n";5946}59475948sub git_heads_body {5949# uses global variable $project5950my($headlist,$head_at,$from,$to,$extra) =@_;5951$from=0unlessdefined$from;5952$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);59535954print"<table class=\"heads\">\n";5955my$alternate=1;5956for(my$i=$from;$i<=$to;$i++) {5957my$entry=$headlist->[$i];5958my%ref=%$entry;5959my$curr=defined$head_at&&$ref{'id'}eq$head_at;5960if($alternate) {5961print"<tr class=\"dark\">\n";5962}else{5963print"<tr class=\"light\">\n";5964}5965$alternate^=1;5966print"<td><i>$ref{'age'}</i></td>\n".5967($curr?"<td class=\"current_head\">":"<td>") .5968$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),5969-class=>"list name"},esc_html($ref{'name'})) .5970"</td>\n".5971"<td class=\"link\">".5972$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".5973$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".5974$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})},"tree") .5975"</td>\n".5976"</tr>";5977}5978if(defined$extra) {5979print"<tr>\n".5980"<td colspan=\"3\">$extra</td>\n".5981"</tr>\n";5982}5983print"</table>\n";5984}59855986# Display a single remote block5987sub git_remote_block {5988my($remote,$rdata,$limit,$head) =@_;59895990my$heads=$rdata->{'heads'};5991my$fetch=$rdata->{'fetch'};5992my$push=$rdata->{'push'};59935994my$urls_table="<table class=\"projects_list\">\n";59955996if(defined$fetch) {5997if($fetcheq$push) {5998$urls_table.= format_repo_url("URL",$fetch);5999}else{6000$urls_table.= format_repo_url("Fetch URL",$fetch);6001$urls_table.= format_repo_url("Push URL",$push)ifdefined$push;6002}6003}elsif(defined$push) {6004$urls_table.= format_repo_url("Push URL",$push);6005}else{6006$urls_table.= format_repo_url("","No remote URL");6007}60086009$urls_table.="</table>\n";60106011my$dots;6012if(defined$limit&&$limit<@$heads) {6013$dots=$cgi->a({-href => href(action=>"remotes", hash=>$remote)},"...");6014}60156016print$urls_table;6017 git_heads_body($heads,$head,0,$limit,$dots);6018}60196020# Display a list of remote names with the respective fetch and push URLs6021sub git_remotes_list {6022my($remotedata,$limit) =@_;6023print"<table class=\"heads\">\n";6024my$alternate=1;6025my@remotes=sort keys%$remotedata;60266027my$limited=$limit&&$limit<@remotes;60286029$#remotes=$limit-1if$limited;60306031while(my$remote=shift@remotes) {6032my$rdata=$remotedata->{$remote};6033my$fetch=$rdata->{'fetch'};6034my$push=$rdata->{'push'};6035if($alternate) {6036print"<tr class=\"dark\">\n";6037}else{6038print"<tr class=\"light\">\n";6039}6040$alternate^=1;6041print"<td>".6042$cgi->a({-href=> href(action=>'remotes', hash=>$remote),6043-class=>"list name"},esc_html($remote)) .6044"</td>";6045print"<td class=\"link\">".6046(defined$fetch?$cgi->a({-href=>$fetch},"fetch") :"fetch") .6047" | ".6048(defined$push?$cgi->a({-href=>$push},"push") :"push") .6049"</td>";60506051print"</tr>\n";6052}60536054if($limited) {6055print"<tr>\n".6056"<td colspan=\"3\">".6057$cgi->a({-href => href(action=>"remotes")},"...") .6058"</td>\n"."</tr>\n";6059}60606061print"</table>";6062}60636064# Display remote heads grouped by remote, unless there are too many6065# remotes, in which case we only display the remote names6066sub git_remotes_body {6067my($remotedata,$limit,$head) =@_;6068if($limitand$limit<keys%$remotedata) {6069 git_remotes_list($remotedata,$limit);6070}else{6071 fill_remote_heads($remotedata);6072while(my($remote,$rdata) =each%$remotedata) {6073 git_print_section({-class=>"remote", -id=>$remote},6074["remotes",$remote,$remote],sub{6075 git_remote_block($remote,$rdata,$limit,$head);6076});6077}6078}6079}60806081sub git_search_message {6082my%co=@_;60836084my$greptype;6085if($searchtypeeq'commit') {6086$greptype="--grep=";6087}elsif($searchtypeeq'author') {6088$greptype="--author=";6089}elsif($searchtypeeq'committer') {6090$greptype="--committer=";6091}6092$greptype.=$searchtext;6093my@commitlist= parse_commits($hash,101, (100*$page),undef,6094$greptype,'--regexp-ignore-case',6095$search_use_regexp?'--extended-regexp':'--fixed-strings');60966097my$paging_nav='';6098if($page>0) {6099$paging_nav.=6100$cgi->a({-href => href(-replay=>1, page=>undef)},6101"first") .6102" ⋅ ".6103$cgi->a({-href => href(-replay=>1, page=>$page-1),6104-accesskey =>"p", -title =>"Alt-p"},"prev");6105}else{6106$paging_nav.="first ⋅ prev";6107}6108my$next_link='';6109if($#commitlist>=100) {6110$next_link=6111$cgi->a({-href => href(-replay=>1, page=>$page+1),6112-accesskey =>"n", -title =>"Alt-n"},"next");6113$paging_nav.=" ⋅$next_link";6114}else{6115$paging_nav.=" ⋅ next";6116}61176118 git_header_html();61196120 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6121 git_print_header_div('commit', esc_html($co{'title'}),$hash);6122if($page==0&& !@commitlist) {6123print"<p>No match.</p>\n";6124}else{6125 git_search_grep_body(\@commitlist,0,99,$next_link);6126}61276128 git_footer_html();6129}61306131sub git_search_changes {6132my%co=@_;61336134local$/="\n";6135open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6136'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6137($search_use_regexp?'--pickaxe-regex': ())6138or die_error(500,"Open git-log failed");61396140 git_header_html();61416142 git_print_page_nav('','',$hash,$co{'tree'},$hash);6143 git_print_header_div('commit', esc_html($co{'title'}),$hash);61446145print"<table class=\"pickaxe search\">\n";6146my$alternate=1;6147undef%co;6148my@files;6149while(my$line= <$fd>) {6150chomp$line;6151next unless$line;61526153my%set= parse_difftree_raw_line($line);6154if(defined$set{'commit'}) {6155# finish previous commit6156if(%co) {6157print"</td>\n".6158"<td class=\"link\">".6159$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},6160"commit") .6161" | ".6162$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},6163 hash_base=>$co{'id'})},6164"tree") .6165"</td>\n".6166"</tr>\n";6167}61686169if($alternate) {6170print"<tr class=\"dark\">\n";6171}else{6172print"<tr class=\"light\">\n";6173}6174$alternate^=1;6175%co= parse_commit($set{'commit'});6176my$author= chop_and_escape_str($co{'author_name'},15,5);6177print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6178"<td><i>$author</i></td>\n".6179"<td>".6180$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6181-class=>"list subject"},6182 chop_and_escape_str($co{'title'},50) ."<br/>");6183}elsif(defined$set{'to_id'}) {6184next if($set{'to_id'} =~m/^0{40}$/);61856186print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6187 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6188-class=>"list"},6189"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6190"<br/>\n";6191}6192}6193close$fd;61946195# finish last commit (warning: repetition!)6196if(%co) {6197print"</td>\n".6198"<td class=\"link\">".6199$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},6200"commit") .6201" | ".6202$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},6203 hash_base=>$co{'id'})},6204"tree") .6205"</td>\n".6206"</tr>\n";6207}62086209print"</table>\n";62106211 git_footer_html();6212}62136214sub git_search_files {6215my%co=@_;62166217local$/="\n";6218open my$fd,"-|", git_cmd(),'grep','-n','-z',6219$search_use_regexp? ('-E','-i') :'-F',6220$searchtext,$co{'tree'}6221or die_error(500,"Open git-grep failed");62226223 git_header_html();62246225 git_print_page_nav('','',$hash,$co{'tree'},$hash);6226 git_print_header_div('commit', esc_html($co{'title'}),$hash);62276228print"<table class=\"grep_search\">\n";6229my$alternate=1;6230my$matches=0;6231my$lastfile='';6232my$file_href;6233while(my$line= <$fd>) {6234chomp$line;6235my($file,$lno,$ltext,$binary);6236last if($matches++>1000);6237if($line=~/^Binary file (.+) matches$/) {6238$file=$1;6239$binary=1;6240}else{6241($file,$lno,$ltext) =split(/\0/,$line,3);6242$file=~s/^$co{'tree'}://;6243}6244if($filene$lastfile) {6245$lastfileand print"</td></tr>\n";6246if($alternate++) {6247print"<tr class=\"dark\">\n";6248}else{6249print"<tr class=\"light\">\n";6250}6251$file_href= href(action=>"blob", hash_base=>$co{'id'},6252 file_name=>$file);6253print"<td class=\"list\">".6254$cgi->a({-href =>$file_href, -class=>"list"}, esc_path($file));6255print"</td><td>\n";6256$lastfile=$file;6257}6258if($binary) {6259print"<div class=\"binary\">Binary file</div>\n";6260}else{6261$ltext= untabify($ltext);6262if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6263$ltext= esc_html($1, -nbsp=>1);6264$ltext.='<span class="match">';6265$ltext.= esc_html($2, -nbsp=>1);6266$ltext.='</span>';6267$ltext.= esc_html($3, -nbsp=>1);6268}else{6269$ltext= esc_html($ltext, -nbsp=>1);6270}6271print"<div class=\"pre\">".6272$cgi->a({-href =>$file_href.'#l'.$lno,6273-class=>"linenr"},sprintf('%4i',$lno)) .6274' '.$ltext."</div>\n";6275}6276}6277if($lastfile) {6278print"</td></tr>\n";6279if($matches>1000) {6280print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6281}6282}else{6283print"<div class=\"diff nodifferences\">No matches found</div>\n";6284}6285close$fd;62866287print"</table>\n";62886289 git_footer_html();6290}62916292sub git_search_grep_body {6293my($commitlist,$from,$to,$extra) =@_;6294$from=0unlessdefined$from;6295$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);62966297print"<table class=\"commit_search\">\n";6298my$alternate=1;6299for(my$i=$from;$i<=$to;$i++) {6300my%co= %{$commitlist->[$i]};6301if(!%co) {6302next;6303}6304my$commit=$co{'id'};6305if($alternate) {6306print"<tr class=\"dark\">\n";6307}else{6308print"<tr class=\"light\">\n";6309}6310$alternate^=1;6311print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6312 format_author_html('td', \%co,15,5) .6313"<td>".6314$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6315-class=>"list subject"},6316 chop_and_escape_str($co{'title'},50) ."<br/>");6317my$comment=$co{'comment'};6318foreachmy$line(@$comment) {6319if($line=~m/^(.*?)($search_regexp)(.*)$/i) {6320my($lead,$match,$trail) = ($1,$2,$3);6321$match= chop_str($match,70,5,'center');6322my$contextlen=int((80-length($match))/2);6323$contextlen=30if($contextlen>30);6324$lead= chop_str($lead,$contextlen,10,'left');6325$trail= chop_str($trail,$contextlen,10,'right');63266327$lead= esc_html($lead);6328$match= esc_html($match);6329$trail= esc_html($trail);63306331print"$lead<span class=\"match\">$match</span>$trail<br />";6332}6333}6334print"</td>\n".6335"<td class=\"link\">".6336$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6337" | ".6338$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .6339" | ".6340$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6341print"</td>\n".6342"</tr>\n";6343}6344if(defined$extra) {6345print"<tr>\n".6346"<td colspan=\"3\">$extra</td>\n".6347"</tr>\n";6348}6349print"</table>\n";6350}63516352## ======================================================================6353## ======================================================================6354## actions63556356sub git_project_list {6357my$order=$input_params{'order'};6358if(defined$order&&$order!~m/none|project|descr|owner|age/) {6359 die_error(400,"Unknown order parameter");6360}63616362my@list= git_get_projects_list($project_filter,$strict_export);6363if(!@list) {6364 die_error(404,"No projects found");6365}63666367 git_header_html();6368if(defined$home_text&& -f $home_text) {6369print"<div class=\"index_include\">\n";6370 insert_file($home_text);6371print"</div>\n";6372}63736374 git_project_search_form($searchtext,$search_use_regexp);6375 git_project_list_body(\@list,$order);6376 git_footer_html();6377}63786379sub git_forks {6380my$order=$input_params{'order'};6381if(defined$order&&$order!~m/none|project|descr|owner|age/) {6382 die_error(400,"Unknown order parameter");6383}63846385my$filter=$project;6386$filter=~s/\.git$//;6387my@list= git_get_projects_list($filter);6388if(!@list) {6389 die_error(404,"No forks found");6390}63916392 git_header_html();6393 git_print_page_nav('','');6394 git_print_header_div('summary',"$projectforks");6395 git_project_list_body(\@list,$order);6396 git_footer_html();6397}63986399sub git_project_index {6400my@projects= git_get_projects_list($project_filter,$strict_export);6401if(!@projects) {6402 die_error(404,"No projects found");6403}64046405print$cgi->header(6406-type =>'text/plain',6407-charset =>'utf-8',6408-content_disposition =>'inline; filename="index.aux"');64096410foreachmy$pr(@projects) {6411if(!exists$pr->{'owner'}) {6412$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");6413}64146415my($path,$owner) = ($pr->{'path'},$pr->{'owner'});6416# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '6417$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;6418$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;6419$path=~s/ /\+/g;6420$owner=~s/ /\+/g;64216422print"$path$owner\n";6423}6424}64256426sub git_summary {6427my$descr= git_get_project_description($project) ||"none";6428my%co= parse_commit("HEAD");6429my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();6430my$head=$co{'id'};6431my$remote_heads= gitweb_check_feature('remote_heads');64326433my$owner= git_get_project_owner($project);64346435my$refs= git_get_references();6436# These get_*_list functions return one more to allow us to see if6437# there are more ...6438my@taglist= git_get_tags_list(16);6439my@headlist= git_get_heads_list(16);6440my%remotedata=$remote_heads? git_get_remotes_list() : ();6441my@forklist;6442my$check_forks= gitweb_check_feature('forks');64436444if($check_forks) {6445# find forks of a project6446my$filter=$project;6447$filter=~s/\.git$//;6448@forklist= git_get_projects_list($filter);6449# filter out forks of forks6450@forklist= filter_forks_from_projects_list(\@forklist)6451if(@forklist);6452}64536454 git_header_html();6455 git_print_page_nav('summary','',$head);64566457print"<div class=\"title\"> </div>\n";6458print"<table class=\"projects_list\">\n".6459"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n";6460unless($omit_owner) {6461print"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";6462}6463if(defined$cd{'rfc2822'}) {6464print"<tr id=\"metadata_lchange\"><td>last change</td>".6465"<td>".format_timestamp_html(\%cd)."</td></tr>\n";6466}64676468# use per project git URL list in $projectroot/$project/cloneurl6469# or make project git URL from git base URL and project name6470my$url_tag="URL";6471my@url_list= git_get_project_url_list($project);6472@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;6473foreachmy$git_url(@url_list) {6474next unless$git_url;6475print format_repo_url($url_tag,$git_url);6476$url_tag="";6477}64786479# Tag cloud6480my$show_ctags= gitweb_check_feature('ctags');6481if($show_ctags) {6482my$ctags= git_get_project_ctags($project);6483if(%$ctags) {6484# without ability to add tags, don't show if there are none6485my$cloud= git_populate_project_tagcloud($ctags);6486print"<tr id=\"metadata_ctags\">".6487"<td>content tags</td>".6488"<td>".git_show_project_tagcloud($cloud,48)."</td>".6489"</tr>\n";6490}6491}64926493print"</table>\n";64946495# If XSS prevention is on, we don't include README.html.6496# TODO: Allow a readme in some safe format.6497if(!$prevent_xss&& -s "$projectroot/$project/README.html") {6498print"<div class=\"title\">readme</div>\n".6499"<div class=\"readme\">\n";6500 insert_file("$projectroot/$project/README.html");6501print"\n</div>\n";# class="readme"6502}65036504# we need to request one more than 16 (0..15) to check if6505# those 16 are all6506my@commitlist=$head? parse_commits($head,17) : ();6507if(@commitlist) {6508 git_print_header_div('shortlog');6509 git_shortlog_body(\@commitlist,0,15,$refs,6510$#commitlist<=15?undef:6511$cgi->a({-href => href(action=>"shortlog")},"..."));6512}65136514if(@taglist) {6515 git_print_header_div('tags');6516 git_tags_body(\@taglist,0,15,6517$#taglist<=15?undef:6518$cgi->a({-href => href(action=>"tags")},"..."));6519}65206521if(@headlist) {6522 git_print_header_div('heads');6523 git_heads_body(\@headlist,$head,0,15,6524$#headlist<=15?undef:6525$cgi->a({-href => href(action=>"heads")},"..."));6526}65276528if(%remotedata) {6529 git_print_header_div('remotes');6530 git_remotes_body(\%remotedata,15,$head);6531}65326533if(@forklist) {6534 git_print_header_div('forks');6535 git_project_list_body(\@forklist,'age',0,15,6536$#forklist<=15?undef:6537$cgi->a({-href => href(action=>"forks")},"..."),6538'no_header');6539}65406541 git_footer_html();6542}65436544sub git_tag {6545my%tag= parse_tag($hash);65466547if(!%tag) {6548 die_error(404,"Unknown tag object");6549}65506551my$head= git_get_head_hash($project);6552 git_header_html();6553 git_print_page_nav('','',$head,undef,$head);6554 git_print_header_div('commit', esc_html($tag{'name'}),$hash);6555print"<div class=\"title_text\">\n".6556"<table class=\"object_header\">\n".6557"<tr>\n".6558"<td>object</td>\n".6559"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},6560$tag{'object'}) ."</td>\n".6561"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},6562$tag{'type'}) ."</td>\n".6563"</tr>\n";6564if(defined($tag{'author'})) {6565 git_print_authorship_rows(\%tag,'author');6566}6567print"</table>\n\n".6568"</div>\n";6569print"<div class=\"page_body\">";6570my$comment=$tag{'comment'};6571foreachmy$line(@$comment) {6572chomp$line;6573print esc_html($line, -nbsp=>1) ."<br/>\n";6574}6575print"</div>\n";6576 git_footer_html();6577}65786579sub git_blame_common {6580my$format=shift||'porcelain';6581if($formateq'porcelain'&&$input_params{'javascript'}) {6582$format='incremental';6583$action='blame_incremental';# for page title etc6584}65856586# permissions6587 gitweb_check_feature('blame')6588or die_error(403,"Blame view not allowed");65896590# error checking6591 die_error(400,"No file name given")unless$file_name;6592$hash_base||= git_get_head_hash($project);6593 die_error(404,"Couldn't find base commit")unless$hash_base;6594my%co= parse_commit($hash_base)6595or die_error(404,"Commit not found");6596my$ftype="blob";6597if(!defined$hash) {6598$hash= git_get_hash_by_path($hash_base,$file_name,"blob")6599or die_error(404,"Error looking up file");6600}else{6601$ftype= git_get_type($hash);6602if($ftype!~"blob") {6603 die_error(400,"Object is not a blob");6604}6605}66066607my$fd;6608if($formateq'incremental') {6609# get file contents (as base)6610open$fd,"-|", git_cmd(),'cat-file','blob',$hash6611or die_error(500,"Open git-cat-file failed");6612}elsif($formateq'data') {6613# run git-blame --incremental6614open$fd,"-|", git_cmd(),"blame","--incremental",6615$hash_base,"--",$file_name6616or die_error(500,"Open git-blame --incremental failed");6617}else{6618# run git-blame --porcelain6619open$fd,"-|", git_cmd(),"blame",'-p',6620$hash_base,'--',$file_name6621or die_error(500,"Open git-blame --porcelain failed");6622}66236624# incremental blame data returns early6625if($formateq'data') {6626print$cgi->header(6627-type=>"text/plain", -charset =>"utf-8",6628-status=>"200 OK");6629local$| =1;# output autoflush6630while(my$line= <$fd>) {6631print to_utf8($line);6632}6633close$fd6634or print"ERROR$!\n";66356636print'END';6637if(defined$t0&& gitweb_check_feature('timed')) {6638print' '.6639 tv_interval($t0, [ gettimeofday() ]).6640' '.$number_of_git_cmds;6641}6642print"\n";66436644return;6645}66466647# page header6648 git_header_html();6649my$formats_nav=6650$cgi->a({-href => href(action=>"blob", -replay=>1)},6651"blob") .6652" | ";6653if($formateq'incremental') {6654$formats_nav.=6655$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},6656"blame") ." (non-incremental)";6657}else{6658$formats_nav.=6659$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},6660"blame") ." (incremental)";6661}6662$formats_nav.=6663" | ".6664$cgi->a({-href => href(action=>"history", -replay=>1)},6665"history") .6666" | ".6667$cgi->a({-href => href(action=>$action, file_name=>$file_name)},6668"HEAD");6669 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6670 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6671 git_print_page_path($file_name,$ftype,$hash_base);66726673# page body6674if($formateq'incremental') {6675print"<noscript>\n<div class=\"error\"><center><b>\n".6676"This page requires JavaScript to run.\nUse ".6677$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},6678'this page').6679" instead.\n".6680"</b></center></div>\n</noscript>\n";66816682print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;6683}66846685print qq!<div class="page_body">\n!;6686print qq!<div id="progress_info">.../ ...</div>\n!6687if($formateq'incremental');6688print qq!<table id="blame_table"class="blame" width="100%">\n!.6689#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.6690 qq!<thead>\n!.6691 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.6692 qq!</thead>\n!.6693 qq!<tbody>\n!;66946695my@rev_color=qw(light dark);6696my$num_colors=scalar(@rev_color);6697my$current_color=0;66986699if($formateq'incremental') {6700my$color_class=$rev_color[$current_color];67016702#contents of a file6703my$linenr=0;6704 LINE:6705while(my$line= <$fd>) {6706chomp$line;6707$linenr++;67086709print qq!<tr id="l$linenr"class="$color_class">!.6710 qq!<td class="sha1"><a href=""> </a></td>!.6711 qq!<td class="linenr">!.6712 qq!<a class="linenr" href="">$linenr</a></td>!;6713print qq!<td class="pre">! . esc_html($line) ."</td>\n";6714print qq!</tr>\n!;6715}67166717}else{# porcelain, i.e. ordinary blame6718my%metainfo= ();# saves information about commits67196720# blame data6721 LINE:6722while(my$line= <$fd>) {6723chomp$line;6724# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]6725# no <lines in group> for subsequent lines in group of lines6726my($full_rev,$orig_lineno,$lineno,$group_size) =6727($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);6728if(!exists$metainfo{$full_rev}) {6729$metainfo{$full_rev} = {'nprevious'=>0};6730}6731my$meta=$metainfo{$full_rev};6732my$data;6733while($data= <$fd>) {6734chomp$data;6735last if($data=~s/^\t//);# contents of line6736if($data=~/^(\S+)(?: (.*))?$/) {6737$meta->{$1} =$2unlessexists$meta->{$1};6738}6739if($data=~/^previous /) {6740$meta->{'nprevious'}++;6741}6742}6743my$short_rev=substr($full_rev,0,8);6744my$author=$meta->{'author'};6745my%date=6746 parse_date($meta->{'author-time'},$meta->{'author-tz'});6747my$date=$date{'iso-tz'};6748if($group_size) {6749$current_color= ($current_color+1) %$num_colors;6750}6751my$tr_class=$rev_color[$current_color];6752$tr_class.=' boundary'if(exists$meta->{'boundary'});6753$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);6754$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);6755print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";6756if($group_size) {6757print"<td class=\"sha1\"";6758print" title=\"". esc_html($author) .",$date\"";6759print" rowspan=\"$group_size\""if($group_size>1);6760print">";6761print$cgi->a({-href => href(action=>"commit",6762 hash=>$full_rev,6763 file_name=>$file_name)},6764 esc_html($short_rev));6765if($group_size>=2) {6766my@author_initials= ($author=~/\b([[:upper:]])\B/g);6767if(@author_initials) {6768print"<br />".6769 esc_html(join('',@author_initials));6770# or join('.', ...)6771}6772}6773print"</td>\n";6774}6775# 'previous' <sha1 of parent commit> <filename at commit>6776if(exists$meta->{'previous'} &&6777$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {6778$meta->{'parent'} =$1;6779$meta->{'file_parent'} = unquote($2);6780}6781my$linenr_commit=6782exists($meta->{'parent'}) ?6783$meta->{'parent'} :$full_rev;6784my$linenr_filename=6785exists($meta->{'file_parent'}) ?6786$meta->{'file_parent'} : unquote($meta->{'filename'});6787my$blamed= href(action =>'blame',6788 file_name =>$linenr_filename,6789 hash_base =>$linenr_commit);6790print"<td class=\"linenr\">";6791print$cgi->a({ -href =>"$blamed#l$orig_lineno",6792-class=>"linenr"},6793 esc_html($lineno));6794print"</td>";6795print"<td class=\"pre\">". esc_html($data) ."</td>\n";6796print"</tr>\n";6797}# end while67986799}68006801# footer6802print"</tbody>\n".6803"</table>\n";# class="blame"6804print"</div>\n";# class="blame_body"6805close$fd6806or print"Reading blob failed\n";68076808 git_footer_html();6809}68106811sub git_blame {6812 git_blame_common();6813}68146815sub git_blame_incremental {6816 git_blame_common('incremental');6817}68186819sub git_blame_data {6820 git_blame_common('data');6821}68226823sub git_tags {6824my$head= git_get_head_hash($project);6825 git_header_html();6826 git_print_page_nav('','',$head,undef,$head,format_ref_views('tags'));6827 git_print_header_div('summary',$project);68286829my@tagslist= git_get_tags_list();6830if(@tagslist) {6831 git_tags_body(\@tagslist);6832}6833 git_footer_html();6834}68356836sub git_heads {6837my$head= git_get_head_hash($project);6838 git_header_html();6839 git_print_page_nav('','',$head,undef,$head,format_ref_views('heads'));6840 git_print_header_div('summary',$project);68416842my@headslist= git_get_heads_list();6843if(@headslist) {6844 git_heads_body(\@headslist,$head);6845}6846 git_footer_html();6847}68486849# used both for single remote view and for list of all the remotes6850sub git_remotes {6851 gitweb_check_feature('remote_heads')6852or die_error(403,"Remote heads view is disabled");68536854my$head= git_get_head_hash($project);6855my$remote=$input_params{'hash'};68566857my$remotedata= git_get_remotes_list($remote);6858 die_error(500,"Unable to get remote information")unlessdefined$remotedata;68596860unless(%$remotedata) {6861 die_error(404,defined$remote?6862"Remote$remotenot found":6863"No remotes found");6864}68656866 git_header_html(undef,undef, -action_extra =>$remote);6867 git_print_page_nav('','',$head,undef,$head,6868 format_ref_views($remote?'':'remotes'));68696870 fill_remote_heads($remotedata);6871if(defined$remote) {6872 git_print_header_div('remotes',"$remoteremote for$project");6873 git_remote_block($remote,$remotedata->{$remote},undef,$head);6874}else{6875 git_print_header_div('summary',"$projectremotes");6876 git_remotes_body($remotedata,undef,$head);6877}68786879 git_footer_html();6880}68816882sub git_blob_plain {6883my$type=shift;6884my$expires;68856886if(!defined$hash) {6887if(defined$file_name) {6888my$base=$hash_base|| git_get_head_hash($project);6889$hash= git_get_hash_by_path($base,$file_name,"blob")6890or die_error(404,"Cannot find file");6891}else{6892 die_error(400,"No file name defined");6893}6894}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6895# blobs defined by non-textual hash id's can be cached6896$expires="+1d";6897}68986899open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6900or die_error(500,"Open git-cat-file blob '$hash' failed");69016902# content-type (can include charset)6903$type= blob_contenttype($fd,$file_name,$type);69046905# "save as" filename, even when no $file_name is given6906my$save_as="$hash";6907if(defined$file_name) {6908$save_as=$file_name;6909}elsif($type=~m/^text\//) {6910$save_as.='.txt';6911}69126913# With XSS prevention on, blobs of all types except a few known safe6914# ones are served with "Content-Disposition: attachment" to make sure6915# they don't run in our security domain. For certain image types,6916# blob view writes an <img> tag referring to blob_plain view, and we6917# want to be sure not to break that by serving the image as an6918# attachment (though Firefox 3 doesn't seem to care).6919my$sandbox=$prevent_xss&&6920$type!~m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;69216922# serve text/* as text/plain6923if($prevent_xss&&6924($type=~m!^text/[a-z]+\b(.*)$!||6925($type=~m!^[a-z]+/[a-z]\+xml\b(.*)$!&& -T $fd))) {6926my$rest=$1;6927$rest=defined$rest?$rest:'';6928$type="text/plain$rest";6929}69306931print$cgi->header(6932-type =>$type,6933-expires =>$expires,6934-content_disposition =>6935($sandbox?'attachment':'inline')6936.'; filename="'.$save_as.'"');6937local$/=undef;6938binmode STDOUT,':raw';6939print<$fd>;6940binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi6941close$fd;6942}69436944sub git_blob {6945my$expires;69466947if(!defined$hash) {6948if(defined$file_name) {6949my$base=$hash_base|| git_get_head_hash($project);6950$hash= git_get_hash_by_path($base,$file_name,"blob")6951or die_error(404,"Cannot find file");6952}else{6953 die_error(400,"No file name defined");6954}6955}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6956# blobs defined by non-textual hash id's can be cached6957$expires="+1d";6958}69596960my$have_blame= gitweb_check_feature('blame');6961open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6962or die_error(500,"Couldn't cat$file_name,$hash");6963my$mimetype= blob_mimetype($fd,$file_name);6964# use 'blob_plain' (aka 'raw') view for files that cannot be displayed6965if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {6966close$fd;6967return git_blob_plain($mimetype);6968}6969# we can have blame only for text/* mimetype6970$have_blame&&= ($mimetype=~m!^text/!);69716972my$highlight= gitweb_check_feature('highlight');6973my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);6974$fd= run_highlighter($fd,$highlight,$syntax)6975if$syntax;69766977 git_header_html(undef,$expires);6978my$formats_nav='';6979if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6980if(defined$file_name) {6981if($have_blame) {6982$formats_nav.=6983$cgi->a({-href => href(action=>"blame", -replay=>1)},6984"blame") .6985" | ";6986}6987$formats_nav.=6988$cgi->a({-href => href(action=>"history", -replay=>1)},6989"history") .6990" | ".6991$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},6992"raw") .6993" | ".6994$cgi->a({-href => href(action=>"blob",6995 hash_base=>"HEAD", file_name=>$file_name)},6996"HEAD");6997}else{6998$formats_nav.=6999$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},7000"raw");7001}7002 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);7003 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);7004}else{7005print"<div class=\"page_nav\">\n".7006"<br/><br/></div>\n".7007"<div class=\"title\">".esc_html($hash)."</div>\n";7008}7009 git_print_page_path($file_name,"blob",$hash_base);7010print"<div class=\"page_body\">\n";7011if($mimetype=~m!^image/!) {7012print qq!<img type="!.esc_attr($mimetype).qq!"!;7013if($file_name) {7014print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;7015}7016print qq! src="! .7017 href(action=>"blob_plain", hash=>$hash,7018 hash_base=>$hash_base, file_name=>$file_name) .7019 qq!"/>\n!;7020}else{7021my$nr;7022while(my$line= <$fd>) {7023chomp$line;7024$nr++;7025$line= untabify($line);7026printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,7027$nr, esc_attr(href(-replay =>1)),$nr,$nr,7028$syntax? sanitize($line) : esc_html($line, -nbsp=>1);7029}7030}7031close$fd7032or print"Reading blob failed.\n";7033print"</div>";7034 git_footer_html();7035}70367037sub git_tree {7038if(!defined$hash_base) {7039$hash_base="HEAD";7040}7041if(!defined$hash) {7042if(defined$file_name) {7043$hash= git_get_hash_by_path($hash_base,$file_name,"tree");7044}else{7045$hash=$hash_base;7046}7047}7048 die_error(404,"No such tree")unlessdefined($hash);70497050my$show_sizes= gitweb_check_feature('show-sizes');7051my$have_blame= gitweb_check_feature('blame');70527053my@entries= ();7054{7055local$/="\0";7056open my$fd,"-|", git_cmd(),"ls-tree",'-z',7057($show_sizes?'-l': ()),@extra_options,$hash7058or die_error(500,"Open git-ls-tree failed");7059@entries=map{chomp;$_} <$fd>;7060close$fd7061or die_error(404,"Reading tree failed");7062}70637064my$refs= git_get_references();7065my$ref= format_ref_marker($refs,$hash_base);7066 git_header_html();7067my$basedir='';7068if(defined$hash_base&& (my%co= parse_commit($hash_base))) {7069my@views_nav= ();7070if(defined$file_name) {7071push@views_nav,7072$cgi->a({-href => href(action=>"history", -replay=>1)},7073"history"),7074$cgi->a({-href => href(action=>"tree",7075 hash_base=>"HEAD", file_name=>$file_name)},7076"HEAD"),7077}7078my$snapshot_links= format_snapshot_links($hash);7079if(defined$snapshot_links) {7080# FIXME: Should be available when we have no hash base as well.7081push@views_nav,$snapshot_links;7082}7083 git_print_page_nav('tree','',$hash_base,undef,undef,7084join(' | ',@views_nav));7085 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);7086}else{7087undef$hash_base;7088print"<div class=\"page_nav\">\n";7089print"<br/><br/></div>\n";7090print"<div class=\"title\">".esc_html($hash)."</div>\n";7091}7092if(defined$file_name) {7093$basedir=$file_name;7094if($basedirne''&&substr($basedir, -1)ne'/') {7095$basedir.='/';7096}7097 git_print_page_path($file_name,'tree',$hash_base);7098}7099print"<div class=\"page_body\">\n";7100print"<table class=\"tree\">\n";7101my$alternate=1;7102# '..' (top directory) link if possible7103if(defined$hash_base&&7104defined$file_name&&$file_name=~m![^/]+$!) {7105if($alternate) {7106print"<tr class=\"dark\">\n";7107}else{7108print"<tr class=\"light\">\n";7109}7110$alternate^=1;71117112my$up=$file_name;7113$up=~s!/?[^/]+$!!;7114undef$upunless$up;7115# based on git_print_tree_entry7116print'<td class="mode">'. mode_str('040000') ."</td>\n";7117print'<td class="size"> </td>'."\n"if$show_sizes;7118print'<td class="list">';7119print$cgi->a({-href => href(action=>"tree",7120 hash_base=>$hash_base,7121 file_name=>$up)},7122"..");7123print"</td>\n";7124print"<td class=\"link\"></td>\n";71257126print"</tr>\n";7127}7128foreachmy$line(@entries) {7129my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);71307131if($alternate) {7132print"<tr class=\"dark\">\n";7133}else{7134print"<tr class=\"light\">\n";7135}7136$alternate^=1;71377138 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);71397140print"</tr>\n";7141}7142print"</table>\n".7143"</div>";7144 git_footer_html();7145}71467147sub snapshot_name {7148my($project,$hash) =@_;71497150# path/to/project.git -> project7151# path/to/project/.git -> project7152my$name= to_utf8($project);7153$name=~ s,([^/])/*\.git$,$1,;7154$name= basename($name);7155# sanitize name7156$name=~s/[[:cntrl:]]/?/g;71577158my$ver=$hash;7159if($hash=~/^[0-9a-fA-F]+$/) {7160# shorten SHA-1 hash7161my$full_hash= git_get_full_hash($project,$hash);7162if($full_hash=~/^$hash/&&length($hash) >7) {7163$ver= git_get_short_hash($project,$hash);7164}7165}elsif($hash=~m!^refs/tags/(.*)$!) {7166# tags don't need shortened SHA-1 hash7167$ver=$1;7168}else{7169# branches and other need shortened SHA-1 hash7170if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {7171$ver=$1;7172}7173$ver.='-'. git_get_short_hash($project,$hash);7174}7175# in case of hierarchical branch names7176$ver=~s!/!.!g;71777178# name = project-version_string7179$name="$name-$ver";71807181returnwantarray? ($name,$name) :$name;7182}71837184sub exit_if_unmodified_since {7185my($latest_epoch) =@_;7186our$cgi;71877188my$if_modified=$cgi->http('IF_MODIFIED_SINCE');7189if(defined$if_modified) {7190my$since;7191if(eval{require HTTP::Date;1; }) {7192$since= HTTP::Date::str2time($if_modified);7193}elsif(eval{require Time::ParseDate;1; }) {7194$since= Time::ParseDate::parsedate($if_modified, GMT =>1);7195}7196if(defined$since&&$latest_epoch<=$since) {7197my%latest_date= parse_date($latest_epoch);7198print$cgi->header(7199-last_modified =>$latest_date{'rfc2822'},7200-status =>'304 Not Modified');7201goto DONE_GITWEB;7202}7203}7204}72057206sub git_snapshot {7207my$format=$input_params{'snapshot_format'};7208if(!@snapshot_fmts) {7209 die_error(403,"Snapshots not allowed");7210}7211# default to first supported snapshot format7212$format||=$snapshot_fmts[0];7213if($format!~m/^[a-z0-9]+$/) {7214 die_error(400,"Invalid snapshot format parameter");7215}elsif(!exists($known_snapshot_formats{$format})) {7216 die_error(400,"Unknown snapshot format");7217}elsif($known_snapshot_formats{$format}{'disabled'}) {7218 die_error(403,"Snapshot format not allowed");7219}elsif(!grep($_eq$format,@snapshot_fmts)) {7220 die_error(403,"Unsupported snapshot format");7221}72227223my$type= git_get_type("$hash^{}");7224if(!$type) {7225 die_error(404,'Object does not exist');7226}elsif($typeeq'blob') {7227 die_error(400,'Object is not a tree-ish');7228}72297230my($name,$prefix) = snapshot_name($project,$hash);7231my$filename="$name$known_snapshot_formats{$format}{'suffix'}";72327233my%co= parse_commit($hash);7234 exit_if_unmodified_since($co{'committer_epoch'})if%co;72357236my$cmd= quote_command(7237 git_cmd(),'archive',7238"--format=$known_snapshot_formats{$format}{'format'}",7239"--prefix=$prefix/",$hash);7240if(exists$known_snapshot_formats{$format}{'compressor'}) {7241$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});7242}72437244$filename=~s/(["\\])/\\$1/g;7245my%latest_date;7246if(%co) {7247%latest_date= parse_date($co{'committer_epoch'},$co{'committer_tz'});7248}72497250print$cgi->header(7251-type =>$known_snapshot_formats{$format}{'type'},7252-content_disposition =>'inline; filename="'.$filename.'"',7253%co? (-last_modified =>$latest_date{'rfc2822'}) : (),7254-status =>'200 OK');72557256open my$fd,"-|",$cmd7257or die_error(500,"Execute git-archive failed");7258binmode STDOUT,':raw';7259print<$fd>;7260binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi7261close$fd;7262}72637264sub git_log_generic {7265my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;72667267my$head= git_get_head_hash($project);7268if(!defined$base) {7269$base=$head;7270}7271if(!defined$page) {7272$page=0;7273}7274my$refs= git_get_references();72757276my$commit_hash=$base;7277if(defined$parent) {7278$commit_hash="$parent..$base";7279}7280my@commitlist=7281 parse_commits($commit_hash,101, (100*$page),7282defined$file_name? ($file_name,"--full-history") : ());72837284my$ftype;7285if(!defined$file_hash&&defined$file_name) {7286# some commits could have deleted file in question,7287# and not have it in tree, but one of them has to have it7288for(my$i=0;$i<@commitlist;$i++) {7289$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);7290last ifdefined$file_hash;7291}7292}7293if(defined$file_hash) {7294$ftype= git_get_type($file_hash);7295}7296if(defined$file_name&& !defined$ftype) {7297 die_error(500,"Unknown type of object");7298}7299my%co;7300if(defined$file_name) {7301%co= parse_commit($base)7302or die_error(404,"Unknown commit object");7303}730473057306my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);7307my$next_link='';7308if($#commitlist>=100) {7309$next_link=7310$cgi->a({-href => href(-replay=>1, page=>$page+1),7311-accesskey =>"n", -title =>"Alt-n"},"next");7312}7313my$patch_max= gitweb_get_feature('patches');7314if($patch_max&& !defined$file_name) {7315if($patch_max<0||@commitlist<=$patch_max) {7316$paging_nav.=" ⋅ ".7317$cgi->a({-href => href(action=>"patches", -replay=>1)},7318"patches");7319}7320}73217322 git_header_html();7323 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);7324if(defined$file_name) {7325 git_print_header_div('commit', esc_html($co{'title'}),$base);7326}else{7327 git_print_header_div('summary',$project)7328}7329 git_print_page_path($file_name,$ftype,$hash_base)7330if(defined$file_name);73317332$body_subr->(\@commitlist,0,99,$refs,$next_link,7333$file_name,$file_hash,$ftype);73347335 git_footer_html();7336}73377338sub git_log {7339 git_log_generic('log', \&git_log_body,7340$hash,$hash_parent);7341}73427343sub git_commit {7344$hash||=$hash_base||"HEAD";7345my%co= parse_commit($hash)7346or die_error(404,"Unknown commit object");73477348my$parent=$co{'parent'};7349my$parents=$co{'parents'};# listref73507351# we need to prepare $formats_nav before any parameter munging7352my$formats_nav;7353if(!defined$parent) {7354# --root commitdiff7355$formats_nav.='(initial)';7356}elsif(@$parents==1) {7357# single parent commit7358$formats_nav.=7359'(parent: '.7360$cgi->a({-href => href(action=>"commit",7361 hash=>$parent)},7362 esc_html(substr($parent,0,7))) .7363')';7364}else{7365# merge commit7366$formats_nav.=7367'(merge: '.7368join(' ',map{7369$cgi->a({-href => href(action=>"commit",7370 hash=>$_)},7371 esc_html(substr($_,0,7)));7372}@$parents) .7373')';7374}7375if(gitweb_check_feature('patches') &&@$parents<=1) {7376$formats_nav.=" | ".7377$cgi->a({-href => href(action=>"patch", -replay=>1)},7378"patch");7379}73807381if(!defined$parent) {7382$parent="--root";7383}7384my@difftree;7385open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",7386@diff_opts,7387(@$parents<=1?$parent:'-c'),7388$hash,"--"7389or die_error(500,"Open git-diff-tree failed");7390@difftree=map{chomp;$_} <$fd>;7391close$fdor die_error(404,"Reading git-diff-tree failed");73927393# non-textual hash id's can be cached7394my$expires;7395if($hash=~m/^[0-9a-fA-F]{40}$/) {7396$expires="+1d";7397}7398my$refs= git_get_references();7399my$ref= format_ref_marker($refs,$co{'id'});74007401 git_header_html(undef,$expires);7402 git_print_page_nav('commit','',7403$hash,$co{'tree'},$hash,7404$formats_nav);74057406if(defined$co{'parent'}) {7407 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);7408}else{7409 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);7410}7411print"<div class=\"title_text\">\n".7412"<table class=\"object_header\">\n";7413 git_print_authorship_rows(\%co);7414print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";7415print"<tr>".7416"<td>tree</td>".7417"<td class=\"sha1\">".7418$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),7419class=>"list"},$co{'tree'}) .7420"</td>".7421"<td class=\"link\">".7422$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},7423"tree");7424my$snapshot_links= format_snapshot_links($hash);7425if(defined$snapshot_links) {7426print" | ".$snapshot_links;7427}7428print"</td>".7429"</tr>\n";74307431foreachmy$par(@$parents) {7432print"<tr>".7433"<td>parent</td>".7434"<td class=\"sha1\">".7435$cgi->a({-href => href(action=>"commit", hash=>$par),7436class=>"list"},$par) .7437"</td>".7438"<td class=\"link\">".7439$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .7440" | ".7441$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .7442"</td>".7443"</tr>\n";7444}7445print"</table>".7446"</div>\n";74477448print"<div class=\"page_body\">\n";7449 git_print_log($co{'comment'});7450print"</div>\n";74517452 git_difftree_body(\@difftree,$hash,@$parents);74537454 git_footer_html();7455}74567457sub git_object {7458# object is defined by:7459# - hash or hash_base alone7460# - hash_base and file_name7461my$type;74627463# - hash or hash_base alone7464if($hash|| ($hash_base&& !defined$file_name)) {7465my$object_id=$hash||$hash_base;74667467open my$fd,"-|", quote_command(7468 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'7469or die_error(404,"Object does not exist");7470$type= <$fd>;7471chomp$type;7472close$fd7473or die_error(404,"Object does not exist");74747475# - hash_base and file_name7476}elsif($hash_base&&defined$file_name) {7477$file_name=~ s,/+$,,;74787479system(git_cmd(),"cat-file",'-e',$hash_base) ==07480or die_error(404,"Base object does not exist");74817482# here errors should not hapen7483open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name7484or die_error(500,"Open git-ls-tree failed");7485my$line= <$fd>;7486close$fd;74877488#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'7489unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {7490 die_error(404,"File or directory for given base does not exist");7491}7492$type=$2;7493$hash=$3;7494}else{7495 die_error(400,"Not enough information to find object");7496}74977498print$cgi->redirect(-uri => href(action=>$type, -full=>1,7499 hash=>$hash, hash_base=>$hash_base,7500 file_name=>$file_name),7501-status =>'302 Found');7502}75037504sub git_blobdiff {7505my$format=shift||'html';7506my$diff_style=$input_params{'diff_style'} ||'inline';75077508my$fd;7509my@difftree;7510my%diffinfo;7511my$expires;75127513# preparing $fd and %diffinfo for git_patchset_body7514# new style URI7515if(defined$hash_base&&defined$hash_parent_base) {7516if(defined$file_name) {7517# read raw output7518open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7519$hash_parent_base,$hash_base,7520"--", (defined$file_parent?$file_parent: ()),$file_name7521or die_error(500,"Open git-diff-tree failed");7522@difftree=map{chomp;$_} <$fd>;7523close$fd7524or die_error(404,"Reading git-diff-tree failed");7525@difftree7526or die_error(404,"Blob diff not found");75277528}elsif(defined$hash&&7529$hash=~/[0-9a-fA-F]{40}/) {7530# try to find filename from $hash75317532# read filtered raw output7533open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7534$hash_parent_base,$hash_base,"--"7535or die_error(500,"Open git-diff-tree failed");7536@difftree=7537# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'7538# $hash == to_id7539grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}7540map{chomp;$_} <$fd>;7541close$fd7542or die_error(404,"Reading git-diff-tree failed");7543@difftree7544or die_error(404,"Blob diff not found");75457546}else{7547 die_error(400,"Missing one of the blob diff parameters");7548}75497550if(@difftree>1) {7551 die_error(400,"Ambiguous blob diff specification");7552}75537554%diffinfo= parse_difftree_raw_line($difftree[0]);7555$file_parent||=$diffinfo{'from_file'} ||$file_name;7556$file_name||=$diffinfo{'to_file'};75577558$hash_parent||=$diffinfo{'from_id'};7559$hash||=$diffinfo{'to_id'};75607561# non-textual hash id's can be cached7562if($hash_base=~m/^[0-9a-fA-F]{40}$/&&7563$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {7564$expires='+1d';7565}75667567# open patch output7568open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7569'-p', ($formateq'html'?"--full-index": ()),7570$hash_parent_base,$hash_base,7571"--", (defined$file_parent?$file_parent: ()),$file_name7572or die_error(500,"Open git-diff-tree failed");7573}75747575# old/legacy style URI -- not generated anymore since 1.4.3.7576if(!%diffinfo) {7577 die_error('404 Not Found',"Missing one of the blob diff parameters")7578}75797580# header7581if($formateq'html') {7582my$formats_nav=7583$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},7584"raw");7585$formats_nav.= diff_style_nav($diff_style);7586 git_header_html(undef,$expires);7587if(defined$hash_base&& (my%co= parse_commit($hash_base))) {7588 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);7589 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);7590}else{7591print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";7592print"<div class=\"title\">".esc_html("$hashvs$hash_parent")."</div>\n";7593}7594if(defined$file_name) {7595 git_print_page_path($file_name,"blob",$hash_base);7596}else{7597print"<div class=\"page_path\"></div>\n";7598}75997600}elsif($formateq'plain') {7601print$cgi->header(7602-type =>'text/plain',7603-charset =>'utf-8',7604-expires =>$expires,7605-content_disposition =>'inline; filename="'."$file_name".'.patch"');76067607print"X-Git-Url: ".$cgi->self_url() ."\n\n";76087609}else{7610 die_error(400,"Unknown blobdiff format");7611}76127613# patch7614if($formateq'html') {7615print"<div class=\"page_body\">\n";76167617 git_patchset_body($fd,$diff_style,7618[ \%diffinfo],$hash_base,$hash_parent_base);7619close$fd;76207621print"</div>\n";# class="page_body"7622 git_footer_html();76237624}else{7625while(my$line= <$fd>) {7626$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;7627$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;76287629print$line;76307631last if$line=~m!^\+\+\+!;7632}7633local$/=undef;7634print<$fd>;7635close$fd;7636}7637}76387639sub git_blobdiff_plain {7640 git_blobdiff('plain');7641}76427643# assumes that it is added as later part of already existing navigation,7644# so it returns "| foo | bar" rather than just "foo | bar"7645sub diff_style_nav {7646my($diff_style,$is_combined) =@_;7647$diff_style||='inline';76487649return""if($is_combined);76507651my@styles= (inline =>'inline','sidebyside'=>'side by side');7652my%styles=@styles;7653@styles=7654@styles[map{$_*2}0..$#styles/2];76557656returnjoin'',7657map{" | ".$_}7658map{7659$_eq$diff_style?$styles{$_} :7660$cgi->a({-href => href(-replay=>1, diff_style =>$_)},$styles{$_})7661}@styles;7662}76637664sub git_commitdiff {7665my%params=@_;7666my$format=$params{-format} ||'html';7667my$diff_style=$input_params{'diff_style'} ||'inline';76687669my($patch_max) = gitweb_get_feature('patches');7670if($formateq'patch') {7671 die_error(403,"Patch view not allowed")unless$patch_max;7672}76737674$hash||=$hash_base||"HEAD";7675my%co= parse_commit($hash)7676or die_error(404,"Unknown commit object");76777678# choose format for commitdiff for merge7679if(!defined$hash_parent&& @{$co{'parents'}} >1) {7680$hash_parent='--cc';7681}7682# we need to prepare $formats_nav before almost any parameter munging7683my$formats_nav;7684if($formateq'html') {7685$formats_nav=7686$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},7687"raw");7688if($patch_max&& @{$co{'parents'}} <=1) {7689$formats_nav.=" | ".7690$cgi->a({-href => href(action=>"patch", -replay=>1)},7691"patch");7692}7693$formats_nav.= diff_style_nav($diff_style, @{$co{'parents'}} >1);76947695if(defined$hash_parent&&7696$hash_parentne'-c'&&$hash_parentne'--cc') {7697# commitdiff with two commits given7698my$hash_parent_short=$hash_parent;7699if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {7700$hash_parent_short=substr($hash_parent,0,7);7701}7702$formats_nav.=7703' (from';7704for(my$i=0;$i< @{$co{'parents'}};$i++) {7705if($co{'parents'}[$i]eq$hash_parent) {7706$formats_nav.=' parent '. ($i+1);7707last;7708}7709}7710$formats_nav.=': '.7711$cgi->a({-href => href(-replay=>1,7712 hash=>$hash_parent, hash_base=>undef)},7713 esc_html($hash_parent_short)) .7714')';7715}elsif(!$co{'parent'}) {7716# --root commitdiff7717$formats_nav.=' (initial)';7718}elsif(scalar@{$co{'parents'}} ==1) {7719# single parent commit7720$formats_nav.=7721' (parent: '.7722$cgi->a({-href => href(-replay=>1,7723 hash=>$co{'parent'}, hash_base=>undef)},7724 esc_html(substr($co{'parent'},0,7))) .7725')';7726}else{7727# merge commit7728if($hash_parenteq'--cc') {7729$formats_nav.=' | '.7730$cgi->a({-href => href(-replay=>1,7731 hash=>$hash, hash_parent=>'-c')},7732'combined');7733}else{# $hash_parent eq '-c'7734$formats_nav.=' | '.7735$cgi->a({-href => href(-replay=>1,7736 hash=>$hash, hash_parent=>'--cc')},7737'compact');7738}7739$formats_nav.=7740' (merge: '.7741join(' ',map{7742$cgi->a({-href => href(-replay=>1,7743 hash=>$_, hash_base=>undef)},7744 esc_html(substr($_,0,7)));7745} @{$co{'parents'}} ) .7746')';7747}7748}77497750my$hash_parent_param=$hash_parent;7751if(!defined$hash_parent_param) {7752# --cc for multiple parents, --root for parentless7753$hash_parent_param=7754@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';7755}77567757# read commitdiff7758my$fd;7759my@difftree;7760if($formateq'html') {7761open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7762"--no-commit-id","--patch-with-raw","--full-index",7763$hash_parent_param,$hash,"--"7764or die_error(500,"Open git-diff-tree failed");77657766while(my$line= <$fd>) {7767chomp$line;7768# empty line ends raw part of diff-tree output7769last unless$line;7770push@difftree,scalar parse_difftree_raw_line($line);7771}77727773}elsif($formateq'plain') {7774open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7775'-p',$hash_parent_param,$hash,"--"7776or die_error(500,"Open git-diff-tree failed");7777}elsif($formateq'patch') {7778# For commit ranges, we limit the output to the number of7779# patches specified in the 'patches' feature.7780# For single commits, we limit the output to a single patch,7781# diverging from the git-format-patch default.7782my@commit_spec= ();7783if($hash_parent) {7784if($patch_max>0) {7785push@commit_spec,"-$patch_max";7786}7787push@commit_spec,'-n',"$hash_parent..$hash";7788}else{7789if($params{-single}) {7790push@commit_spec,'-1';7791}else{7792if($patch_max>0) {7793push@commit_spec,"-$patch_max";7794}7795push@commit_spec,"-n";7796}7797push@commit_spec,'--root',$hash;7798}7799open$fd,"-|", git_cmd(),"format-patch",@diff_opts,7800'--encoding=utf8','--stdout',@commit_spec7801or die_error(500,"Open git-format-patch failed");7802}else{7803 die_error(400,"Unknown commitdiff format");7804}78057806# non-textual hash id's can be cached7807my$expires;7808if($hash=~m/^[0-9a-fA-F]{40}$/) {7809$expires="+1d";7810}78117812# write commit message7813if($formateq'html') {7814my$refs= git_get_references();7815my$ref= format_ref_marker($refs,$co{'id'});78167817 git_header_html(undef,$expires);7818 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);7819 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);7820print"<div class=\"title_text\">\n".7821"<table class=\"object_header\">\n";7822 git_print_authorship_rows(\%co);7823print"</table>".7824"</div>\n";7825print"<div class=\"page_body\">\n";7826if(@{$co{'comment'}} >1) {7827print"<div class=\"log\">\n";7828 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);7829print"</div>\n";# class="log"7830}78317832}elsif($formateq'plain') {7833my$refs= git_get_references("tags");7834my$tagname= git_get_rev_name_tags($hash);7835my$filename= basename($project) ."-$hash.patch";78367837print$cgi->header(7838-type =>'text/plain',7839-charset =>'utf-8',7840-expires =>$expires,7841-content_disposition =>'inline; filename="'."$filename".'"');7842my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});7843print"From: ". to_utf8($co{'author'}) ."\n";7844print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";7845print"Subject: ". to_utf8($co{'title'}) ."\n";78467847print"X-Git-Tag:$tagname\n"if$tagname;7848print"X-Git-Url: ".$cgi->self_url() ."\n\n";78497850foreachmy$line(@{$co{'comment'}}) {7851print to_utf8($line) ."\n";7852}7853print"---\n\n";7854}elsif($formateq'patch') {7855my$filename= basename($project) ."-$hash.patch";78567857print$cgi->header(7858-type =>'text/plain',7859-charset =>'utf-8',7860-expires =>$expires,7861-content_disposition =>'inline; filename="'."$filename".'"');7862}78637864# write patch7865if($formateq'html') {7866my$use_parents= !defined$hash_parent||7867$hash_parenteq'-c'||$hash_parenteq'--cc';7868 git_difftree_body(\@difftree,$hash,7869$use_parents? @{$co{'parents'}} :$hash_parent);7870print"<br/>\n";78717872 git_patchset_body($fd,$diff_style,7873 \@difftree,$hash,7874$use_parents? @{$co{'parents'}} :$hash_parent);7875close$fd;7876print"</div>\n";# class="page_body"7877 git_footer_html();78787879}elsif($formateq'plain') {7880local$/=undef;7881print<$fd>;7882close$fd7883or print"Reading git-diff-tree failed\n";7884}elsif($formateq'patch') {7885local$/=undef;7886print<$fd>;7887close$fd7888or print"Reading git-format-patch failed\n";7889}7890}78917892sub git_commitdiff_plain {7893 git_commitdiff(-format =>'plain');7894}78957896# format-patch-style patches7897sub git_patch {7898 git_commitdiff(-format =>'patch', -single =>1);7899}79007901sub git_patches {7902 git_commitdiff(-format =>'patch');7903}79047905sub git_history {7906 git_log_generic('history', \&git_history_body,7907$hash_base,$hash_parent_base,7908$file_name,$hash);7909}79107911sub git_search {7912$searchtype||='commit';79137914# check if appropriate features are enabled7915 gitweb_check_feature('search')7916or die_error(403,"Search is disabled");7917if($searchtypeeq'pickaxe') {7918# pickaxe may take all resources of your box and run for several minutes7919# with every query - so decide by yourself how public you make this feature7920 gitweb_check_feature('pickaxe')7921or die_error(403,"Pickaxe search is disabled");7922}7923if($searchtypeeq'grep') {7924# grep search might be potentially CPU-intensive, too7925 gitweb_check_feature('grep')7926or die_error(403,"Grep search is disabled");7927}79287929if(!defined$searchtext) {7930 die_error(400,"Text field is empty");7931}7932if(!defined$hash) {7933$hash= git_get_head_hash($project);7934}7935my%co= parse_commit($hash);7936if(!%co) {7937 die_error(404,"Unknown commit object");7938}7939if(!defined$page) {7940$page=0;7941}79427943if($searchtypeeq'commit'||7944$searchtypeeq'author'||7945$searchtypeeq'committer') {7946 git_search_message(%co);7947}elsif($searchtypeeq'pickaxe') {7948 git_search_changes(%co);7949}elsif($searchtypeeq'grep') {7950 git_search_files(%co);7951}else{7952 die_error(400,"Unknown search type");7953}7954}79557956sub git_search_help {7957 git_header_html();7958 git_print_page_nav('','',$hash,$hash,$hash);7959print<<EOT;7960<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without7961regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,7962the pattern entered is recognized as the POSIX extended7963<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case7964insensitive).</p>7965<dl>7966<dt><b>commit</b></dt>7967<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>7968EOT7969my$have_grep= gitweb_check_feature('grep');7970if($have_grep) {7971print<<EOT;7972<dt><b>grep</b></dt>7973<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing7974 a different one) are searched for the given pattern. On large trees, this search can take7975a while and put some strain on the server, so please use it with some consideration. Note that7976due to git-grep peculiarity, currently if regexp mode is turned off, the matches are7977case-sensitive.</dd>7978EOT7979}7980print<<EOT;7981<dt><b>author</b></dt>7982<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>7983<dt><b>committer</b></dt>7984<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>7985EOT7986my$have_pickaxe= gitweb_check_feature('pickaxe');7987if($have_pickaxe) {7988print<<EOT;7989<dt><b>pickaxe</b></dt>7990<dd>All commits that caused the string to appear or disappear from any file (changes that7991added, removed or "modified" the string) will be listed. This search can take a while and7992takes a lot of strain on the server, so please use it wisely. Note that since you may be7993interested even in changes just changing the case as well, this search is case sensitive.</dd>7994EOT7995}7996print"</dl>\n";7997 git_footer_html();7998}79998000sub git_shortlog {8001 git_log_generic('shortlog', \&git_shortlog_body,8002$hash,$hash_parent);8003}80048005## ......................................................................8006## feeds (RSS, Atom; OPML)80078008sub git_feed {8009my$format=shift||'atom';8010my$have_blame= gitweb_check_feature('blame');80118012# Atom: http://www.atomenabled.org/developers/syndication/8013# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ8014if($formatne'rss'&&$formatne'atom') {8015 die_error(400,"Unknown web feed format");8016}80178018# log/feed of current (HEAD) branch, log of given branch, history of file/directory8019my$head=$hash||'HEAD';8020my@commitlist= parse_commits($head,150,0,$file_name);80218022my%latest_commit;8023my%latest_date;8024my$content_type="application/$format+xml";8025if(defined$cgi->http('HTTP_ACCEPT') &&8026$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {8027# browser (feed reader) prefers text/xml8028$content_type='text/xml';8029}8030if(defined($commitlist[0])) {8031%latest_commit= %{$commitlist[0]};8032my$latest_epoch=$latest_commit{'committer_epoch'};8033 exit_if_unmodified_since($latest_epoch);8034%latest_date= parse_date($latest_epoch,$latest_commit{'committer_tz'});8035}8036print$cgi->header(8037-type =>$content_type,8038-charset =>'utf-8',8039%latest_date? (-last_modified =>$latest_date{'rfc2822'}) : (),8040-status =>'200 OK');80418042# Optimization: skip generating the body if client asks only8043# for Last-Modified date.8044return if($cgi->request_method()eq'HEAD');80458046# header variables8047my$title="$site_name-$project/$action";8048my$feed_type='log';8049if(defined$hash) {8050$title.=" - '$hash'";8051$feed_type='branch log';8052if(defined$file_name) {8053$title.=" ::$file_name";8054$feed_type='history';8055}8056}elsif(defined$file_name) {8057$title.=" -$file_name";8058$feed_type='history';8059}8060$title.="$feed_type";8061my$descr= git_get_project_description($project);8062if(defined$descr) {8063$descr= esc_html($descr);8064}else{8065$descr="$project".8066($formateq'rss'?'RSS':'Atom') .8067" feed";8068}8069my$owner= git_get_project_owner($project);8070$owner= esc_html($owner);80718072#header8073my$alt_url;8074if(defined$file_name) {8075$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);8076}elsif(defined$hash) {8077$alt_url= href(-full=>1, action=>"log", hash=>$hash);8078}else{8079$alt_url= href(-full=>1, action=>"summary");8080}8081print qq!<?xml version="1.0" encoding="utf-8"?>\n!;8082if($formateq'rss') {8083print<<XML;8084<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">8085<channel>8086XML8087print"<title>$title</title>\n".8088"<link>$alt_url</link>\n".8089"<description>$descr</description>\n".8090"<language>en</language>\n".8091# project owner is responsible for 'editorial' content8092"<managingEditor>$owner</managingEditor>\n";8093if(defined$logo||defined$favicon) {8094# prefer the logo to the favicon, since RSS8095# doesn't allow both8096my$img= esc_url($logo||$favicon);8097print"<image>\n".8098"<url>$img</url>\n".8099"<title>$title</title>\n".8100"<link>$alt_url</link>\n".8101"</image>\n";8102}8103if(%latest_date) {8104print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";8105print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";8106}8107print"<generator>gitweb v.$version/$git_version</generator>\n";8108}elsif($formateq'atom') {8109print<<XML;8110<feed xmlns="http://www.w3.org/2005/Atom">8111XML8112print"<title>$title</title>\n".8113"<subtitle>$descr</subtitle>\n".8114'<link rel="alternate" type="text/html" href="'.8115$alt_url.'" />'."\n".8116'<link rel="self" type="'.$content_type.'" href="'.8117$cgi->self_url() .'" />'."\n".8118"<id>". href(-full=>1) ."</id>\n".8119# use project owner for feed author8120"<author><name>$owner</name></author>\n";8121if(defined$favicon) {8122print"<icon>". esc_url($favicon) ."</icon>\n";8123}8124if(defined$logo) {8125# not twice as wide as tall: 72 x 27 pixels8126print"<logo>". esc_url($logo) ."</logo>\n";8127}8128if(!%latest_date) {8129# dummy date to keep the feed valid until commits trickle in:8130print"<updated>1970-01-01T00:00:00Z</updated>\n";8131}else{8132print"<updated>$latest_date{'iso-8601'}</updated>\n";8133}8134print"<generator version='$version/$git_version'>gitweb</generator>\n";8135}81368137# contents8138for(my$i=0;$i<=$#commitlist;$i++) {8139my%co= %{$commitlist[$i]};8140my$commit=$co{'id'};8141# we read 150, we always show 30 and the ones more recent than 48 hours8142if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {8143last;8144}8145my%cd= parse_date($co{'author_epoch'},$co{'author_tz'});81468147# get list of changed files8148open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,8149$co{'parent'} ||"--root",8150$co{'id'},"--", (defined$file_name?$file_name: ())8151ornext;8152my@difftree=map{chomp;$_} <$fd>;8153close$fd8154ornext;81558156# print element (entry, item)8157my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);8158if($formateq'rss') {8159print"<item>\n".8160"<title>". esc_html($co{'title'}) ."</title>\n".8161"<author>". esc_html($co{'author'}) ."</author>\n".8162"<pubDate>$cd{'rfc2822'}</pubDate>\n".8163"<guid isPermaLink=\"true\">$co_url</guid>\n".8164"<link>$co_url</link>\n".8165"<description>". esc_html($co{'title'}) ."</description>\n".8166"<content:encoded>".8167"<![CDATA[\n";8168}elsif($formateq'atom') {8169print"<entry>\n".8170"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".8171"<updated>$cd{'iso-8601'}</updated>\n".8172"<author>\n".8173" <name>". esc_html($co{'author_name'}) ."</name>\n";8174if($co{'author_email'}) {8175print" <email>". esc_html($co{'author_email'}) ."</email>\n";8176}8177print"</author>\n".8178# use committer for contributor8179"<contributor>\n".8180" <name>". esc_html($co{'committer_name'}) ."</name>\n";8181if($co{'committer_email'}) {8182print" <email>". esc_html($co{'committer_email'}) ."</email>\n";8183}8184print"</contributor>\n".8185"<published>$cd{'iso-8601'}</published>\n".8186"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".8187"<id>$co_url</id>\n".8188"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".8189"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";8190}8191my$comment=$co{'comment'};8192print"<pre>\n";8193foreachmy$line(@$comment) {8194$line= esc_html($line);8195print"$line\n";8196}8197print"</pre><ul>\n";8198foreachmy$difftree_line(@difftree) {8199my%difftree= parse_difftree_raw_line($difftree_line);8200next if!$difftree{'from_id'};82018202my$file=$difftree{'file'} ||$difftree{'to_file'};82038204print"<li>".8205"[".8206$cgi->a({-href => href(-full=>1, action=>"blobdiff",8207 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},8208 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},8209 file_name=>$file, file_parent=>$difftree{'from_file'}),8210-title =>"diff"},'D');8211if($have_blame) {8212print$cgi->a({-href => href(-full=>1, action=>"blame",8213 file_name=>$file, hash_base=>$commit),8214-title =>"blame"},'B');8215}8216# if this is not a feed of a file history8217if(!defined$file_name||$file_namene$file) {8218print$cgi->a({-href => href(-full=>1, action=>"history",8219 file_name=>$file, hash=>$commit),8220-title =>"history"},'H');8221}8222$file= esc_path($file);8223print"] ".8224"$file</li>\n";8225}8226if($formateq'rss') {8227print"</ul>]]>\n".8228"</content:encoded>\n".8229"</item>\n";8230}elsif($formateq'atom') {8231print"</ul>\n</div>\n".8232"</content>\n".8233"</entry>\n";8234}8235}82368237# end of feed8238if($formateq'rss') {8239print"</channel>\n</rss>\n";8240}elsif($formateq'atom') {8241print"</feed>\n";8242}8243}82448245sub git_rss {8246 git_feed('rss');8247}82488249sub git_atom {8250 git_feed('atom');8251}82528253sub git_opml {8254my@list= git_get_projects_list($project_filter,$strict_export);8255if(!@list) {8256 die_error(404,"No projects found");8257}82588259print$cgi->header(8260-type =>'text/xml',8261-charset =>'utf-8',8262-content_disposition =>'inline; filename="opml.xml"');82638264my$title= esc_html($site_name);8265my$filter=" within subdirectory ";8266if(defined$project_filter) {8267$filter.= esc_html($project_filter);8268}else{8269$filter="";8270}8271print<<XML;8272<?xml version="1.0" encoding="utf-8"?>8273<opml version="1.0">8274<head>8275 <title>$titleOPML Export$filter</title>8276</head>8277<body>8278<outline text="git RSS feeds">8279XML82808281foreachmy$pr(@list) {8282my%proj=%$pr;8283my$head= git_get_head_hash($proj{'path'});8284if(!defined$head) {8285next;8286}8287$git_dir="$projectroot/$proj{'path'}";8288my%co= parse_commit($head);8289if(!%co) {8290next;8291}82928293my$path= esc_html(chop_str($proj{'path'},25,5));8294my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);8295my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);8296print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";8297}8298print<<XML;8299</outline>8300</body>8301</opml>8302XML8303}