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) { 57if($my_url=~ s,\Q$path_info\E$,, && 58$my_uri=~ s,\Q$path_info\E$,, && 59defined$ENV{'SCRIPT_NAME'}) { 60$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 61} 62} 63 64# target of the home link on top of all pages 65our$home_link=$my_uri||"/"; 66} 67 68# core git executable to use 69# this can just be "git" if your webserver has a sensible PATH 70our$GIT="++GIT_BINDIR++/git"; 71 72# absolute fs-path which will be prepended to the project path 73#our $projectroot = "/pub/scm"; 74our$projectroot="++GITWEB_PROJECTROOT++"; 75 76# fs traversing limit for getting project list 77# the number is relative to the projectroot 78our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 79 80# string of the home link on top of all pages 81our$home_link_str="++GITWEB_HOME_LINK_STR++"; 82 83# name of your site or organization to appear in page titles 84# replace this with something more descriptive for clearer bookmarks 85our$site_name="++GITWEB_SITENAME++" 86|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 87 88# html snippet to include in the <head> section of each page 89our$site_html_head_string="++GITWEB_SITE_HTML_HEAD_STRING++"; 90# filename of html text to include at top of each page 91our$site_header="++GITWEB_SITE_HEADER++"; 92# html text to include at home page 93our$home_text="++GITWEB_HOMETEXT++"; 94# filename of html text to include at bottom of each page 95our$site_footer="++GITWEB_SITE_FOOTER++"; 96 97# URI of stylesheets 98our@stylesheets= ("++GITWEB_CSS++"); 99# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 100our$stylesheet=undef; 101# URI of GIT logo (72x27 size) 102our$logo="++GITWEB_LOGO++"; 103# URI of GIT favicon, assumed to be image/png type 104our$favicon="++GITWEB_FAVICON++"; 105# URI of gitweb.js (JavaScript code for gitweb) 106our$javascript="++GITWEB_JS++"; 107 108# URI and label (title) of GIT logo link 109#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 110#our $logo_label = "git documentation"; 111our$logo_url="http://git-scm.com/"; 112our$logo_label="git homepage"; 113 114# source of projects list 115our$projects_list="++GITWEB_LIST++"; 116 117# the width (in characters) of the projects list "Description" column 118our$projects_list_description_width=25; 119 120# group projects by category on the projects list 121# (enabled if this variable evaluates to true) 122our$projects_list_group_categories=0; 123 124# default category if none specified 125# (leave the empty string for no category) 126our$project_list_default_category=""; 127 128# default order of projects list 129# valid values are none, project, descr, owner, and age 130our$default_projects_order="project"; 131 132# show repository only if this file exists 133# (only effective if this variable evaluates to true) 134our$export_ok="++GITWEB_EXPORT_OK++"; 135 136# show repository only if this subroutine returns true 137# when given the path to the project, for example: 138# sub { return -e "$_[0]/git-daemon-export-ok"; } 139our$export_auth_hook=undef; 140 141# only allow viewing of repositories also shown on the overview page 142our$strict_export="++GITWEB_STRICT_EXPORT++"; 143 144# list of git base URLs used for URL to where fetch project from, 145# i.e. full URL is "$git_base_url/$project" 146our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 147 148# default blob_plain mimetype and default charset for text/plain blob 149our$default_blob_plain_mimetype='text/plain'; 150our$default_text_plain_charset=undef; 151 152# file to use for guessing MIME types before trying /etc/mime.types 153# (relative to the current git repository) 154our$mimetypes_file=undef; 155 156# assume this charset if line contains non-UTF-8 characters; 157# it should be valid encoding (see Encoding::Supported(3pm) for list), 158# for which encoding all byte sequences are valid, for example 159# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 160# could be even 'utf-8' for the old behavior) 161our$fallback_encoding='latin1'; 162 163# rename detection options for git-diff and git-diff-tree 164# - default is '-M', with the cost proportional to 165# (number of removed files) * (number of new files). 166# - more costly is '-C' (which implies '-M'), with the cost proportional to 167# (number of changed files + number of removed files) * (number of new files) 168# - even more costly is '-C', '--find-copies-harder' with cost 169# (number of files in the original tree) * (number of new files) 170# - one might want to include '-B' option, e.g. '-B', '-M' 171our@diff_opts= ('-M');# taken from git_commit 172 173# Disables features that would allow repository owners to inject script into 174# the gitweb domain. 175our$prevent_xss=0; 176 177# Path to the highlight executable to use (must be the one from 178# http://www.andre-simon.de due to assumptions about parameters and output). 179# Useful if highlight is not installed on your webserver's PATH. 180# [Default: highlight] 181our$highlight_bin="++HIGHLIGHT_BIN++"; 182 183# information about snapshot formats that gitweb is capable of serving 184our%known_snapshot_formats= ( 185# name => { 186# 'display' => display name, 187# 'type' => mime type, 188# 'suffix' => filename suffix, 189# 'format' => --format for git-archive, 190# 'compressor' => [compressor command and arguments] 191# (array reference, optional) 192# 'disabled' => boolean (optional)} 193# 194'tgz'=> { 195'display'=>'tar.gz', 196'type'=>'application/x-gzip', 197'suffix'=>'.tar.gz', 198'format'=>'tar', 199'compressor'=> ['gzip','-n']}, 200 201'tbz2'=> { 202'display'=>'tar.bz2', 203'type'=>'application/x-bzip2', 204'suffix'=>'.tar.bz2', 205'format'=>'tar', 206'compressor'=> ['bzip2']}, 207 208'txz'=> { 209'display'=>'tar.xz', 210'type'=>'application/x-xz', 211'suffix'=>'.tar.xz', 212'format'=>'tar', 213'compressor'=> ['xz'], 214'disabled'=>1}, 215 216'zip'=> { 217'display'=>'zip', 218'type'=>'application/x-zip', 219'suffix'=>'.zip', 220'format'=>'zip'}, 221); 222 223# Aliases so we understand old gitweb.snapshot values in repository 224# configuration. 225our%known_snapshot_format_aliases= ( 226'gzip'=>'tgz', 227'bzip2'=>'tbz2', 228'xz'=>'txz', 229 230# backward compatibility: legacy gitweb config support 231'x-gzip'=>undef,'gz'=>undef, 232'x-bzip2'=>undef,'bz2'=>undef, 233'x-zip'=>undef,''=>undef, 234); 235 236# Pixel sizes for icons and avatars. If the default font sizes or lineheights 237# are changed, it may be appropriate to change these values too via 238# $GITWEB_CONFIG. 239our%avatar_size= ( 240'default'=>16, 241'double'=>32 242); 243 244# Used to set the maximum load that we will still respond to gitweb queries. 245# If server load exceed this value then return "503 server busy" error. 246# If gitweb cannot determined server load, it is taken to be 0. 247# Leave it undefined (or set to 'undef') to turn off load checking. 248our$maxload=300; 249 250# configuration for 'highlight' (http://www.andre-simon.de/) 251# match by basename 252our%highlight_basename= ( 253#'Program' => 'py', 254#'Library' => 'py', 255'SConstruct'=>'py',# SCons equivalent of Makefile 256'Makefile'=>'make', 257); 258# match by extension 259our%highlight_ext= ( 260# main extensions, defining name of syntax; 261# see files in /usr/share/highlight/langDefs/ directory 262map{$_=>$_} 263qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl sql make), 264# alternate extensions, see /etc/highlight/filetypes.conf 265'h'=>'c', 266map{$_=>'sh'}qw(bash zsh ksh), 267map{$_=>'cpp'}qw(cxx c++ cc), 268map{$_=>'php'}qw(php3 php4 php5 phps), 269map{$_=>'pl'}qw(perl pm),# perhaps also 'cgi' 270map{$_=>'make'}qw(mak mk), 271map{$_=>'xml'}qw(xhtml html htm), 272); 273 274# You define site-wide feature defaults here; override them with 275# $GITWEB_CONFIG as necessary. 276our%feature= ( 277# feature => { 278# 'sub' => feature-sub (subroutine), 279# 'override' => allow-override (boolean), 280# 'default' => [ default options...] (array reference)} 281# 282# if feature is overridable (it means that allow-override has true value), 283# then feature-sub will be called with default options as parameters; 284# return value of feature-sub indicates if to enable specified feature 285# 286# if there is no 'sub' key (no feature-sub), then feature cannot be 287# overridden 288# 289# use gitweb_get_feature(<feature>) to retrieve the <feature> value 290# (an array) or gitweb_check_feature(<feature>) to check if <feature> 291# is enabled 292 293# Enable the 'blame' blob view, showing the last commit that modified 294# each line in the file. This can be very CPU-intensive. 295 296# To enable system wide have in $GITWEB_CONFIG 297# $feature{'blame'}{'default'} = [1]; 298# To have project specific config enable override in $GITWEB_CONFIG 299# $feature{'blame'}{'override'} = 1; 300# and in project config gitweb.blame = 0|1; 301'blame'=> { 302'sub'=>sub{ feature_bool('blame',@_) }, 303'override'=>0, 304'default'=> [0]}, 305 306# Enable the 'snapshot' link, providing a compressed archive of any 307# tree. This can potentially generate high traffic if you have large 308# project. 309 310# Value is a list of formats defined in %known_snapshot_formats that 311# you wish to offer. 312# To disable system wide have in $GITWEB_CONFIG 313# $feature{'snapshot'}{'default'} = []; 314# To have project specific config enable override in $GITWEB_CONFIG 315# $feature{'snapshot'}{'override'} = 1; 316# and in project config, a comma-separated list of formats or "none" 317# to disable. Example: gitweb.snapshot = tbz2,zip; 318'snapshot'=> { 319'sub'=> \&feature_snapshot, 320'override'=>0, 321'default'=> ['tgz']}, 322 323# Enable text search, which will list the commits which match author, 324# committer or commit text to a given string. Enabled by default. 325# Project specific override is not supported. 326# 327# Note that this controls all search features, which means that if 328# it is disabled, then 'grep' and 'pickaxe' search would also be 329# disabled. 330'search'=> { 331'override'=>0, 332'default'=> [1]}, 333 334# Enable grep search, which will list the files in currently selected 335# tree containing the given string. Enabled by default. This can be 336# potentially CPU-intensive, of course. 337# Note that you need to have 'search' feature enabled too. 338 339# To enable system wide have in $GITWEB_CONFIG 340# $feature{'grep'}{'default'} = [1]; 341# To have project specific config enable override in $GITWEB_CONFIG 342# $feature{'grep'}{'override'} = 1; 343# and in project config gitweb.grep = 0|1; 344'grep'=> { 345'sub'=>sub{ feature_bool('grep',@_) }, 346'override'=>0, 347'default'=> [1]}, 348 349# Enable the pickaxe search, which will list the commits that modified 350# a given string in a file. This can be practical and quite faster 351# alternative to 'blame', but still potentially CPU-intensive. 352# Note that you need to have 'search' feature enabled too. 353 354# To enable system wide have in $GITWEB_CONFIG 355# $feature{'pickaxe'}{'default'} = [1]; 356# To have project specific config enable override in $GITWEB_CONFIG 357# $feature{'pickaxe'}{'override'} = 1; 358# and in project config gitweb.pickaxe = 0|1; 359'pickaxe'=> { 360'sub'=>sub{ feature_bool('pickaxe',@_) }, 361'override'=>0, 362'default'=> [1]}, 363 364# Enable showing size of blobs in a 'tree' view, in a separate 365# column, similar to what 'ls -l' does. This cost a bit of IO. 366 367# To disable system wide have in $GITWEB_CONFIG 368# $feature{'show-sizes'}{'default'} = [0]; 369# To have project specific config enable override in $GITWEB_CONFIG 370# $feature{'show-sizes'}{'override'} = 1; 371# and in project config gitweb.showsizes = 0|1; 372'show-sizes'=> { 373'sub'=>sub{ feature_bool('showsizes',@_) }, 374'override'=>0, 375'default'=> [1]}, 376 377# Make gitweb use an alternative format of the URLs which can be 378# more readable and natural-looking: project name is embedded 379# directly in the path and the query string contains other 380# auxiliary information. All gitweb installations recognize 381# URL in either format; this configures in which formats gitweb 382# generates links. 383 384# To enable system wide have in $GITWEB_CONFIG 385# $feature{'pathinfo'}{'default'} = [1]; 386# Project specific override is not supported. 387 388# Note that you will need to change the default location of CSS, 389# favicon, logo and possibly other files to an absolute URL. Also, 390# if gitweb.cgi serves as your indexfile, you will need to force 391# $my_uri to contain the script name in your $GITWEB_CONFIG. 392'pathinfo'=> { 393'override'=>0, 394'default'=> [0]}, 395 396# Make gitweb consider projects in project root subdirectories 397# to be forks of existing projects. Given project $projname.git, 398# projects matching $projname/*.git will not be shown in the main 399# projects list, instead a '+' mark will be added to $projname 400# there and a 'forks' view will be enabled for the project, listing 401# all the forks. If project list is taken from a file, forks have 402# to be listed after the main project. 403 404# To enable system wide have in $GITWEB_CONFIG 405# $feature{'forks'}{'default'} = [1]; 406# Project specific override is not supported. 407'forks'=> { 408'override'=>0, 409'default'=> [0]}, 410 411# Insert custom links to the action bar of all project pages. 412# This enables you mainly to link to third-party scripts integrating 413# into gitweb; e.g. git-browser for graphical history representation 414# or custom web-based repository administration interface. 415 416# The 'default' value consists of a list of triplets in the form 417# (label, link, position) where position is the label after which 418# to insert the link and link is a format string where %n expands 419# to the project name, %f to the project path within the filesystem, 420# %h to the current hash (h gitweb parameter) and %b to the current 421# hash base (hb gitweb parameter); %% expands to %. 422 423# To enable system wide have in $GITWEB_CONFIG e.g. 424# $feature{'actions'}{'default'} = [('graphiclog', 425# '/git-browser/by-commit.html?r=%n', 'summary')]; 426# Project specific override is not supported. 427'actions'=> { 428'override'=>0, 429'default'=> []}, 430 431# Allow gitweb scan project content tags of project repository, 432# and display the popular Web 2.0-ish "tag cloud" near the projects 433# list. Note that this is something COMPLETELY different from the 434# normal Git tags. 435 436# gitweb by itself can show existing tags, but it does not handle 437# tagging itself; you need to do it externally, outside gitweb. 438# The format is described in git_get_project_ctags() subroutine. 439# You may want to install the HTML::TagCloud Perl module to get 440# a pretty tag cloud instead of just a list of tags. 441 442# To enable system wide have in $GITWEB_CONFIG 443# $feature{'ctags'}{'default'} = [1]; 444# Project specific override is not supported. 445 446# In the future whether ctags editing is enabled might depend 447# on the value, but using 1 should always mean no editing of ctags. 448'ctags'=> { 449'override'=>0, 450'default'=> [0]}, 451 452# The maximum number of patches in a patchset generated in patch 453# view. Set this to 0 or undef to disable patch view, or to a 454# negative number to remove any limit. 455 456# To disable system wide have in $GITWEB_CONFIG 457# $feature{'patches'}{'default'} = [0]; 458# To have project specific config enable override in $GITWEB_CONFIG 459# $feature{'patches'}{'override'} = 1; 460# and in project config gitweb.patches = 0|n; 461# where n is the maximum number of patches allowed in a patchset. 462'patches'=> { 463'sub'=> \&feature_patches, 464'override'=>0, 465'default'=> [16]}, 466 467# Avatar support. When this feature is enabled, views such as 468# shortlog or commit will display an avatar associated with 469# the email of the committer(s) and/or author(s). 470 471# Currently available providers are gravatar and picon. 472# If an unknown provider is specified, the feature is disabled. 473 474# Gravatar depends on Digest::MD5. 475# Picon currently relies on the indiana.edu database. 476 477# To enable system wide have in $GITWEB_CONFIG 478# $feature{'avatar'}{'default'} = ['<provider>']; 479# where <provider> is either gravatar or picon. 480# To have project specific config enable override in $GITWEB_CONFIG 481# $feature{'avatar'}{'override'} = 1; 482# and in project config gitweb.avatar = <provider>; 483'avatar'=> { 484'sub'=> \&feature_avatar, 485'override'=>0, 486'default'=> ['']}, 487 488# Enable displaying how much time and how many git commands 489# it took to generate and display page. Disabled by default. 490# Project specific override is not supported. 491'timed'=> { 492'override'=>0, 493'default'=> [0]}, 494 495# Enable turning some links into links to actions which require 496# JavaScript to run (like 'blame_incremental'). Not enabled by 497# default. Project specific override is currently not supported. 498'javascript-actions'=> { 499'override'=>0, 500'default'=> [0]}, 501 502# Enable and configure ability to change common timezone for dates 503# in gitweb output via JavaScript. Enabled by default. 504# Project specific override is not supported. 505'javascript-timezone'=> { 506'override'=>0, 507'default'=> [ 508'local',# default timezone: 'utc', 'local', or '(-|+)HHMM' format, 509# or undef to turn off this feature 510'gitweb_tz',# name of cookie where to store selected timezone 511'datetime',# CSS class used to mark up dates for manipulation 512]}, 513 514# Syntax highlighting support. This is based on Daniel Svensson's 515# and Sham Chukoury's work in gitweb-xmms2.git. 516# It requires the 'highlight' program present in $PATH, 517# and therefore is disabled by default. 518 519# To enable system wide have in $GITWEB_CONFIG 520# $feature{'highlight'}{'default'} = [1]; 521 522'highlight'=> { 523'sub'=>sub{ feature_bool('highlight',@_) }, 524'override'=>0, 525'default'=> [0]}, 526 527# Enable displaying of remote heads in the heads list 528 529# To enable system wide have in $GITWEB_CONFIG 530# $feature{'remote_heads'}{'default'} = [1]; 531# To have project specific config enable override in $GITWEB_CONFIG 532# $feature{'remote_heads'}{'override'} = 1; 533# and in project config gitweb.remote_heads = 0|1; 534'remote_heads'=> { 535'sub'=>sub{ feature_bool('remote_heads',@_) }, 536'override'=>0, 537'default'=> [0]}, 538); 539 540sub gitweb_get_feature { 541my($name) =@_; 542return unlessexists$feature{$name}; 543my($sub,$override,@defaults) = ( 544$feature{$name}{'sub'}, 545$feature{$name}{'override'}, 546@{$feature{$name}{'default'}}); 547# project specific override is possible only if we have project 548our$git_dir;# global variable, declared later 549if(!$override|| !defined$git_dir) { 550return@defaults; 551} 552if(!defined$sub) { 553warn"feature$nameis not overridable"; 554return@defaults; 555} 556return$sub->(@defaults); 557} 558 559# A wrapper to check if a given feature is enabled. 560# With this, you can say 561# 562# my $bool_feat = gitweb_check_feature('bool_feat'); 563# gitweb_check_feature('bool_feat') or somecode; 564# 565# instead of 566# 567# my ($bool_feat) = gitweb_get_feature('bool_feat'); 568# (gitweb_get_feature('bool_feat'))[0] or somecode; 569# 570sub gitweb_check_feature { 571return(gitweb_get_feature(@_))[0]; 572} 573 574 575sub feature_bool { 576my$key=shift; 577my($val) = git_get_project_config($key,'--bool'); 578 579if(!defined$val) { 580return($_[0]); 581}elsif($valeq'true') { 582return(1); 583}elsif($valeq'false') { 584return(0); 585} 586} 587 588sub feature_snapshot { 589my(@fmts) =@_; 590 591my($val) = git_get_project_config('snapshot'); 592 593if($val) { 594@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 595} 596 597return@fmts; 598} 599 600sub feature_patches { 601my@val= (git_get_project_config('patches','--int')); 602 603if(@val) { 604return@val; 605} 606 607return($_[0]); 608} 609 610sub feature_avatar { 611my@val= (git_get_project_config('avatar')); 612 613return@val?@val:@_; 614} 615 616# checking HEAD file with -e is fragile if the repository was 617# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 618# and then pruned. 619sub check_head_link { 620my($dir) =@_; 621my$headfile="$dir/HEAD"; 622return((-e $headfile) || 623(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 624} 625 626sub check_export_ok { 627my($dir) =@_; 628return(check_head_link($dir) && 629(!$export_ok|| -e "$dir/$export_ok") && 630(!$export_auth_hook||$export_auth_hook->($dir))); 631} 632 633# process alternate names for backward compatibility 634# filter out unsupported (unknown) snapshot formats 635sub filter_snapshot_fmts { 636my@fmts=@_; 637 638@fmts=map{ 639exists$known_snapshot_format_aliases{$_} ? 640$known_snapshot_format_aliases{$_} :$_}@fmts; 641@fmts=grep{ 642exists$known_snapshot_formats{$_} && 643!$known_snapshot_formats{$_}{'disabled'}}@fmts; 644} 645 646# If it is set to code reference, it is code that it is to be run once per 647# request, allowing updating configurations that change with each request, 648# while running other code in config file only once. 649# 650# Otherwise, if it is false then gitweb would process config file only once; 651# if it is true then gitweb config would be run for each request. 652our$per_request_config=1; 653 654# read and parse gitweb config file given by its parameter. 655# returns true on success, false on recoverable error, allowing 656# to chain this subroutine, using first file that exists. 657# dies on errors during parsing config file, as it is unrecoverable. 658sub read_config_file { 659my$filename=shift; 660return unlessdefined$filename; 661# die if there are errors parsing config file 662if(-e $filename) { 663do$filename; 664die$@if$@; 665return1; 666} 667return; 668} 669 670our($GITWEB_CONFIG,$GITWEB_CONFIG_SYSTEM,$GITWEB_CONFIG_COMMON); 671sub evaluate_gitweb_config { 672our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 673our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 674our$GITWEB_CONFIG_COMMON=$ENV{'GITWEB_CONFIG_COMMON'} ||"++GITWEB_CONFIG_COMMON++"; 675 676# Protect agains duplications of file names, to not read config twice. 677# Only one of $GITWEB_CONFIG and $GITWEB_CONFIG_SYSTEM is used, so 678# there possibility of duplication of filename there doesn't matter. 679$GITWEB_CONFIG=""if($GITWEB_CONFIGeq$GITWEB_CONFIG_COMMON); 680$GITWEB_CONFIG_SYSTEM=""if($GITWEB_CONFIG_SYSTEMeq$GITWEB_CONFIG_COMMON); 681 682# Common system-wide settings for convenience. 683# Those settings can be ovverriden by GITWEB_CONFIG or GITWEB_CONFIG_SYSTEM. 684 read_config_file($GITWEB_CONFIG_COMMON); 685 686# Use first config file that exists. This means use the per-instance 687# GITWEB_CONFIG if exists, otherwise use GITWEB_SYSTEM_CONFIG. 688 read_config_file($GITWEB_CONFIG)andreturn; 689 read_config_file($GITWEB_CONFIG_SYSTEM); 690} 691 692# Get loadavg of system, to compare against $maxload. 693# Currently it requires '/proc/loadavg' present to get loadavg; 694# if it is not present it returns 0, which means no load checking. 695sub get_loadavg { 696if( -e '/proc/loadavg'){ 697open my$fd,'<','/proc/loadavg' 698orreturn0; 699my@load=split(/\s+/,scalar<$fd>); 700close$fd; 701 702# The first three columns measure CPU and IO utilization of the last one, 703# five, and 10 minute periods. The fourth column shows the number of 704# currently running processes and the total number of processes in the m/n 705# format. The last column displays the last process ID used. 706return$load[0] ||0; 707} 708# additional checks for load average should go here for things that don't export 709# /proc/loadavg 710 711return0; 712} 713 714# version of the core git binary 715our$git_version; 716sub evaluate_git_version { 717our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 718$number_of_git_cmds++; 719} 720 721sub check_loadavg { 722if(defined$maxload&& get_loadavg() >$maxload) { 723 die_error(503,"The load average on the server is too high"); 724} 725} 726 727# ====================================================================== 728# input validation and dispatch 729 730# input parameters can be collected from a variety of sources (presently, CGI 731# and PATH_INFO), so we define an %input_params hash that collects them all 732# together during validation: this allows subsequent uses (e.g. href()) to be 733# agnostic of the parameter origin 734 735our%input_params= (); 736 737# input parameters are stored with the long parameter name as key. This will 738# also be used in the href subroutine to convert parameters to their CGI 739# equivalent, and since the href() usage is the most frequent one, we store 740# the name -> CGI key mapping here, instead of the reverse. 741# 742# XXX: Warning: If you touch this, check the search form for updating, 743# too. 744 745our@cgi_param_mapping= ( 746 project =>"p", 747 action =>"a", 748 file_name =>"f", 749 file_parent =>"fp", 750 hash =>"h", 751 hash_parent =>"hp", 752 hash_base =>"hb", 753 hash_parent_base =>"hpb", 754 page =>"pg", 755 order =>"o", 756 searchtext =>"s", 757 searchtype =>"st", 758 snapshot_format =>"sf", 759 extra_options =>"opt", 760 search_use_regexp =>"sr", 761 ctag =>"by_tag", 762 diff_style =>"ds", 763 project_filter =>"pf", 764# this must be last entry (for manipulation from JavaScript) 765 javascript =>"js" 766); 767our%cgi_param_mapping=@cgi_param_mapping; 768 769# we will also need to know the possible actions, for validation 770our%actions= ( 771"blame"=> \&git_blame, 772"blame_incremental"=> \&git_blame_incremental, 773"blame_data"=> \&git_blame_data, 774"blobdiff"=> \&git_blobdiff, 775"blobdiff_plain"=> \&git_blobdiff_plain, 776"blob"=> \&git_blob, 777"blob_plain"=> \&git_blob_plain, 778"commitdiff"=> \&git_commitdiff, 779"commitdiff_plain"=> \&git_commitdiff_plain, 780"commit"=> \&git_commit, 781"forks"=> \&git_forks, 782"heads"=> \&git_heads, 783"history"=> \&git_history, 784"log"=> \&git_log, 785"patch"=> \&git_patch, 786"patches"=> \&git_patches, 787"remotes"=> \&git_remotes, 788"rss"=> \&git_rss, 789"atom"=> \&git_atom, 790"search"=> \&git_search, 791"search_help"=> \&git_search_help, 792"shortlog"=> \&git_shortlog, 793"summary"=> \&git_summary, 794"tag"=> \&git_tag, 795"tags"=> \&git_tags, 796"tree"=> \&git_tree, 797"snapshot"=> \&git_snapshot, 798"object"=> \&git_object, 799# those below don't need $project 800"opml"=> \&git_opml, 801"project_list"=> \&git_project_list, 802"project_index"=> \&git_project_index, 803); 804 805# finally, we have the hash of allowed extra_options for the commands that 806# allow them 807our%allowed_options= ( 808"--no-merges"=> [qw(rss atom log shortlog history)], 809); 810 811# fill %input_params with the CGI parameters. All values except for 'opt' 812# should be single values, but opt can be an array. We should probably 813# build an array of parameters that can be multi-valued, but since for the time 814# being it's only this one, we just single it out 815sub evaluate_query_params { 816our$cgi; 817 818while(my($name,$symbol) =each%cgi_param_mapping) { 819if($symboleq'opt') { 820$input_params{$name} = [map{ decode_utf8($_) }$cgi->param($symbol) ]; 821}else{ 822$input_params{$name} = decode_utf8($cgi->param($symbol)); 823} 824} 825} 826 827# now read PATH_INFO and update the parameter list for missing parameters 828sub evaluate_path_info { 829return ifdefined$input_params{'project'}; 830return if!$path_info; 831$path_info=~ s,^/+,,; 832return if!$path_info; 833 834# find which part of PATH_INFO is project 835my$project=$path_info; 836$project=~ s,/+$,,; 837while($project&& !check_head_link("$projectroot/$project")) { 838$project=~ s,/*[^/]*$,,; 839} 840return unless$project; 841$input_params{'project'} =$project; 842 843# do not change any parameters if an action is given using the query string 844return if$input_params{'action'}; 845$path_info=~ s,^\Q$project\E/*,,; 846 847# next, check if we have an action 848my$action=$path_info; 849$action=~ s,/.*$,,; 850if(exists$actions{$action}) { 851$path_info=~ s,^$action/*,,; 852$input_params{'action'} =$action; 853} 854 855# list of actions that want hash_base instead of hash, but can have no 856# pathname (f) parameter 857my@wants_base= ( 858'tree', 859'history', 860); 861 862# we want to catch, among others 863# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 864my($parentrefname,$parentpathname,$refname,$pathname) = 865($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/); 866 867# first, analyze the 'current' part 868if(defined$pathname) { 869# we got "branch:filename" or "branch:dir/" 870# we could use git_get_type(branch:pathname), but: 871# - it needs $git_dir 872# - it does a git() call 873# - the convention of terminating directories with a slash 874# makes it superfluous 875# - embedding the action in the PATH_INFO would make it even 876# more superfluous 877$pathname=~ s,^/+,,; 878if(!$pathname||substr($pathname, -1)eq"/") { 879$input_params{'action'} ||="tree"; 880$pathname=~ s,/$,,; 881}else{ 882# the default action depends on whether we had parent info 883# or not 884if($parentrefname) { 885$input_params{'action'} ||="blobdiff_plain"; 886}else{ 887$input_params{'action'} ||="blob_plain"; 888} 889} 890$input_params{'hash_base'} ||=$refname; 891$input_params{'file_name'} ||=$pathname; 892}elsif(defined$refname) { 893# we got "branch". In this case we have to choose if we have to 894# set hash or hash_base. 895# 896# Most of the actions without a pathname only want hash to be 897# set, except for the ones specified in @wants_base that want 898# hash_base instead. It should also be noted that hand-crafted 899# links having 'history' as an action and no pathname or hash 900# set will fail, but that happens regardless of PATH_INFO. 901if(defined$parentrefname) { 902# if there is parent let the default be 'shortlog' action 903# (for http://git.example.com/repo.git/A..B links); if there 904# is no parent, dispatch will detect type of object and set 905# action appropriately if required (if action is not set) 906$input_params{'action'} ||="shortlog"; 907} 908if($input_params{'action'} && 909grep{$_eq$input_params{'action'} }@wants_base) { 910$input_params{'hash_base'} ||=$refname; 911}else{ 912$input_params{'hash'} ||=$refname; 913} 914} 915 916# next, handle the 'parent' part, if present 917if(defined$parentrefname) { 918# a missing pathspec defaults to the 'current' filename, allowing e.g. 919# someproject/blobdiff/oldrev..newrev:/filename 920if($parentpathname) { 921$parentpathname=~ s,^/+,,; 922$parentpathname=~ s,/$,,; 923$input_params{'file_parent'} ||=$parentpathname; 924}else{ 925$input_params{'file_parent'} ||=$input_params{'file_name'}; 926} 927# we assume that hash_parent_base is wanted if a path was specified, 928# or if the action wants hash_base instead of hash 929if(defined$input_params{'file_parent'} || 930grep{$_eq$input_params{'action'} }@wants_base) { 931$input_params{'hash_parent_base'} ||=$parentrefname; 932}else{ 933$input_params{'hash_parent'} ||=$parentrefname; 934} 935} 936 937# for the snapshot action, we allow URLs in the form 938# $project/snapshot/$hash.ext 939# where .ext determines the snapshot and gets removed from the 940# passed $refname to provide the $hash. 941# 942# To be able to tell that $refname includes the format extension, we 943# require the following two conditions to be satisfied: 944# - the hash input parameter MUST have been set from the $refname part 945# of the URL (i.e. they must be equal) 946# - the snapshot format MUST NOT have been defined already (e.g. from 947# CGI parameter sf) 948# It's also useless to try any matching unless $refname has a dot, 949# so we check for that too 950if(defined$input_params{'action'} && 951$input_params{'action'}eq'snapshot'&& 952defined$refname&&index($refname,'.') != -1&& 953$refnameeq$input_params{'hash'} && 954!defined$input_params{'snapshot_format'}) { 955# We loop over the known snapshot formats, checking for 956# extensions. Allowed extensions are both the defined suffix 957# (which includes the initial dot already) and the snapshot 958# format key itself, with a prepended dot 959while(my($fmt,$opt) =each%known_snapshot_formats) { 960my$hash=$refname; 961unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 962next; 963} 964my$sfx=$1; 965# a valid suffix was found, so set the snapshot format 966# and reset the hash parameter 967$input_params{'snapshot_format'} =$fmt; 968$input_params{'hash'} =$hash; 969# we also set the format suffix to the one requested 970# in the URL: this way a request for e.g. .tgz returns 971# a .tgz instead of a .tar.gz 972$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 973last; 974} 975} 976} 977 978our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 979$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 980$searchtext,$search_regexp,$project_filter); 981sub evaluate_and_validate_params { 982our$action=$input_params{'action'}; 983if(defined$action) { 984if(!validate_action($action)) { 985 die_error(400,"Invalid action parameter"); 986} 987} 988 989# parameters which are pathnames 990our$project=$input_params{'project'}; 991if(defined$project) { 992if(!validate_project($project)) { 993undef$project; 994 die_error(404,"No such project"); 995} 996} 997 998our$project_filter=$input_params{'project_filter'}; 999if(defined$project_filter) {1000if(!validate_pathname($project_filter)) {1001 die_error(404,"Invalid project_filter parameter");1002}1003}10041005our$file_name=$input_params{'file_name'};1006if(defined$file_name) {1007if(!validate_pathname($file_name)) {1008 die_error(400,"Invalid file parameter");1009}1010}10111012our$file_parent=$input_params{'file_parent'};1013if(defined$file_parent) {1014if(!validate_pathname($file_parent)) {1015 die_error(400,"Invalid file parent parameter");1016}1017}10181019# parameters which are refnames1020our$hash=$input_params{'hash'};1021if(defined$hash) {1022if(!validate_refname($hash)) {1023 die_error(400,"Invalid hash parameter");1024}1025}10261027our$hash_parent=$input_params{'hash_parent'};1028if(defined$hash_parent) {1029if(!validate_refname($hash_parent)) {1030 die_error(400,"Invalid hash parent parameter");1031}1032}10331034our$hash_base=$input_params{'hash_base'};1035if(defined$hash_base) {1036if(!validate_refname($hash_base)) {1037 die_error(400,"Invalid hash base parameter");1038}1039}10401041our@extra_options= @{$input_params{'extra_options'}};1042# @extra_options is always defined, since it can only be (currently) set from1043# CGI, and $cgi->param() returns the empty array in array context if the param1044# is not set1045foreachmy$opt(@extra_options) {1046if(not exists$allowed_options{$opt}) {1047 die_error(400,"Invalid option parameter");1048}1049if(not grep(/^$action$/, @{$allowed_options{$opt}})) {1050 die_error(400,"Invalid option parameter for this action");1051}1052}10531054our$hash_parent_base=$input_params{'hash_parent_base'};1055if(defined$hash_parent_base) {1056if(!validate_refname($hash_parent_base)) {1057 die_error(400,"Invalid hash parent base parameter");1058}1059}10601061# other parameters1062our$page=$input_params{'page'};1063if(defined$page) {1064if($page=~m/[^0-9]/) {1065 die_error(400,"Invalid page parameter");1066}1067}10681069our$searchtype=$input_params{'searchtype'};1070if(defined$searchtype) {1071if($searchtype=~m/[^a-z]/) {1072 die_error(400,"Invalid searchtype parameter");1073}1074}10751076our$search_use_regexp=$input_params{'search_use_regexp'};10771078our$searchtext=$input_params{'searchtext'};1079our$search_regexp;1080if(defined$searchtext) {1081if(length($searchtext) <2) {1082 die_error(403,"At least two characters are required for search parameter");1083}1084if($search_use_regexp) {1085$search_regexp=$searchtext;1086if(!eval{qr/$search_regexp/;1; }) {1087(my$error=$@) =~s/ at \S+ line \d+.*\n?//;1088 die_error(400,"Invalid search regexp '$search_regexp'",1089 esc_html($error));1090}1091}else{1092$search_regexp=quotemeta$searchtext;1093}1094}1095}10961097# path to the current git repository1098our$git_dir;1099sub evaluate_git_dir {1100our$git_dir="$projectroot/$project"if$project;1101}11021103our(@snapshot_fmts,$git_avatar);1104sub configure_gitweb_features {1105# list of supported snapshot formats1106our@snapshot_fmts= gitweb_get_feature('snapshot');1107@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);11081109# check that the avatar feature is set to a known provider name,1110# and for each provider check if the dependencies are satisfied.1111# if the provider name is invalid or the dependencies are not met,1112# reset $git_avatar to the empty string.1113our($git_avatar) = gitweb_get_feature('avatar');1114if($git_avatareq'gravatar') {1115$git_avatar=''unless(eval{require Digest::MD5;1; });1116}elsif($git_avatareq'picon') {1117# no dependencies1118}else{1119$git_avatar='';1120}1121}11221123# custom error handler: 'die <message>' is Internal Server Error1124sub handle_errors_html {1125my$msg=shift;# it is already HTML escaped11261127# to avoid infinite loop where error occurs in die_error,1128# change handler to default handler, disabling handle_errors_html1129 set_message("Error occured when inside die_error:\n$msg");11301131# you cannot jump out of die_error when called as error handler;1132# the subroutine set via CGI::Carp::set_message is called _after_1133# HTTP headers are already written, so it cannot write them itself1134 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1);1135}1136set_message(\&handle_errors_html);11371138# dispatch1139sub dispatch {1140if(!defined$action) {1141if(defined$hash) {1142$action= git_get_type($hash);1143$actionor die_error(404,"Object does not exist");1144}elsif(defined$hash_base&&defined$file_name) {1145$action= git_get_type("$hash_base:$file_name");1146$actionor die_error(404,"File or directory does not exist");1147}elsif(defined$project) {1148$action='summary';1149}else{1150$action='project_list';1151}1152}1153if(!defined($actions{$action})) {1154 die_error(400,"Unknown action");1155}1156if($action!~m/^(?:opml|project_list|project_index)$/&&1157!$project) {1158 die_error(400,"Project needed");1159}1160$actions{$action}->();1161}11621163sub reset_timer {1164our$t0= [ gettimeofday() ]1165ifdefined$t0;1166our$number_of_git_cmds=0;1167}11681169our$first_request=1;1170sub run_request {1171 reset_timer();11721173 evaluate_uri();1174if($first_request) {1175 evaluate_gitweb_config();1176 evaluate_git_version();1177}1178if($per_request_config) {1179if(ref($per_request_config)eq'CODE') {1180$per_request_config->();1181}elsif(!$first_request) {1182 evaluate_gitweb_config();1183}1184}1185 check_loadavg();11861187# $projectroot and $projects_list might be set in gitweb config file1188$projects_list||=$projectroot;11891190 evaluate_query_params();1191 evaluate_path_info();1192 evaluate_and_validate_params();1193 evaluate_git_dir();11941195 configure_gitweb_features();11961197 dispatch();1198}11991200our$is_last_request=sub{1};1201our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1202our$CGI='CGI';1203our$cgi;1204sub configure_as_fcgi {1205require CGI::Fast;1206our$CGI='CGI::Fast';12071208my$request_number=0;1209# let each child service 100 requests1210our$is_last_request=sub{ ++$request_number>100};1211}1212sub evaluate_argv {1213my$script_name=$ENV{'SCRIPT_NAME'} ||$ENV{'SCRIPT_FILENAME'} || __FILE__;1214 configure_as_fcgi()1215if$script_name=~/\.fcgi$/;12161217return unless(@ARGV);12181219require Getopt::Long;1220 Getopt::Long::GetOptions(1221'fastcgi|fcgi|f'=> \&configure_as_fcgi,1222'nproc|n=i'=>sub{1223my($arg,$val) =@_;1224return unlesseval{require FCGI::ProcManager;1; };1225my$proc_manager= FCGI::ProcManager->new({1226 n_processes =>$val,1227});1228our$pre_listen_hook=sub{$proc_manager->pm_manage() };1229our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1230our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1231},1232);1233}12341235sub run {1236 evaluate_argv();12371238$first_request=1;1239$pre_listen_hook->()1240if$pre_listen_hook;12411242 REQUEST:1243while($cgi=$CGI->new()) {1244$pre_dispatch_hook->()1245if$pre_dispatch_hook;12461247 run_request();12481249$post_dispatch_hook->()1250if$post_dispatch_hook;1251$first_request=0;12521253last REQUEST if($is_last_request->());1254}12551256 DONE_GITWEB:12571;1258}12591260run();12611262if(defined caller) {1263# wrapped in a subroutine processing requests,1264# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1265return;1266}else{1267# pure CGI script, serving single request1268exit;1269}12701271## ======================================================================1272## action links12731274# possible values of extra options1275# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1276# -replay => 1 - start from a current view (replay with modifications)1277# -path_info => 0|1 - don't use/use path_info URL (if possible)1278# -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone1279sub href {1280my%params=@_;1281# default is to use -absolute url() i.e. $my_uri1282my$href=$params{-full} ?$my_url:$my_uri;12831284# implicit -replay, must be first of implicit params1285$params{-replay} =1if(keys%params==1&&$params{-anchor});12861287$params{'project'} =$projectunlessexists$params{'project'};12881289if($params{-replay}) {1290while(my($name,$symbol) =each%cgi_param_mapping) {1291if(!exists$params{$name}) {1292$params{$name} =$input_params{$name};1293}1294}1295}12961297my$use_pathinfo= gitweb_check_feature('pathinfo');1298if(defined$params{'project'} &&1299(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1300# try to put as many parameters as possible in PATH_INFO:1301# - project name1302# - action1303# - hash_parent or hash_parent_base:/file_parent1304# - hash or hash_base:/filename1305# - the snapshot_format as an appropriate suffix13061307# When the script is the root DirectoryIndex for the domain,1308# $href here would be something like http://gitweb.example.com/1309# Thus, we strip any trailing / from $href, to spare us double1310# slashes in the final URL1311$href=~ s,/$,,;13121313# Then add the project name, if present1314$href.="/".esc_path_info($params{'project'});1315delete$params{'project'};13161317# since we destructively absorb parameters, we keep this1318# boolean that remembers if we're handling a snapshot1319my$is_snapshot=$params{'action'}eq'snapshot';13201321# Summary just uses the project path URL, any other action is1322# added to the URL1323if(defined$params{'action'}) {1324$href.="/".esc_path_info($params{'action'})1325unless$params{'action'}eq'summary';1326delete$params{'action'};1327}13281329# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1330# stripping nonexistent or useless pieces1331$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1332||$params{'hash_parent'} ||$params{'hash'});1333if(defined$params{'hash_base'}) {1334if(defined$params{'hash_parent_base'}) {1335$href.= esc_path_info($params{'hash_parent_base'});1336# skip the file_parent if it's the same as the file_name1337if(defined$params{'file_parent'}) {1338if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1339delete$params{'file_parent'};1340}elsif($params{'file_parent'} !~/\.\./) {1341$href.=":/".esc_path_info($params{'file_parent'});1342delete$params{'file_parent'};1343}1344}1345$href.="..";1346delete$params{'hash_parent'};1347delete$params{'hash_parent_base'};1348}elsif(defined$params{'hash_parent'}) {1349$href.= esc_path_info($params{'hash_parent'})."..";1350delete$params{'hash_parent'};1351}13521353$href.= esc_path_info($params{'hash_base'});1354if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1355$href.=":/".esc_path_info($params{'file_name'});1356delete$params{'file_name'};1357}1358delete$params{'hash'};1359delete$params{'hash_base'};1360}elsif(defined$params{'hash'}) {1361$href.= esc_path_info($params{'hash'});1362delete$params{'hash'};1363}13641365# If the action was a snapshot, we can absorb the1366# snapshot_format parameter too1367if($is_snapshot) {1368my$fmt=$params{'snapshot_format'};1369# snapshot_format should always be defined when href()1370# is called, but just in case some code forgets, we1371# fall back to the default1372$fmt||=$snapshot_fmts[0];1373$href.=$known_snapshot_formats{$fmt}{'suffix'};1374delete$params{'snapshot_format'};1375}1376}13771378# now encode the parameters explicitly1379my@result= ();1380for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1381my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1382if(defined$params{$name}) {1383if(ref($params{$name})eq"ARRAY") {1384foreachmy$par(@{$params{$name}}) {1385push@result,$symbol."=". esc_param($par);1386}1387}else{1388push@result,$symbol."=". esc_param($params{$name});1389}1390}1391}1392$href.="?".join(';',@result)ifscalar@result;13931394# final transformation: trailing spaces must be escaped (URI-encoded)1395$href=~s/(\s+)$/CGI::escape($1)/e;13961397if($params{-anchor}) {1398$href.="#".esc_param($params{-anchor});1399}14001401return$href;1402}140314041405## ======================================================================1406## validation, quoting/unquoting and escaping14071408sub validate_action {1409my$input=shift||returnundef;1410returnundefunlessexists$actions{$input};1411return$input;1412}14131414sub validate_project {1415my$input=shift||returnundef;1416if(!validate_pathname($input) ||1417!(-d "$projectroot/$input") ||1418!check_export_ok("$projectroot/$input") ||1419($strict_export&& !project_in_list($input))) {1420returnundef;1421}else{1422return$input;1423}1424}14251426sub validate_pathname {1427my$input=shift||returnundef;14281429# no '.' or '..' as elements of path, i.e. no '.' nor '..'1430# at the beginning, at the end, and between slashes.1431# also this catches doubled slashes1432if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1433returnundef;1434}1435# no null characters1436if($input=~m!\0!) {1437returnundef;1438}1439return$input;1440}14411442sub validate_refname {1443my$input=shift||returnundef;14441445# textual hashes are O.K.1446if($input=~m/^[0-9a-fA-F]{40}$/) {1447return$input;1448}1449# it must be correct pathname1450$input= validate_pathname($input)1451orreturnundef;1452# restrictions on ref name according to git-check-ref-format1453if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1454returnundef;1455}1456return$input;1457}14581459# decode sequences of octets in utf8 into Perl's internal form,1460# which is utf-8 with utf8 flag set if needed. gitweb writes out1461# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1462sub to_utf8 {1463my$str=shift;1464returnundefunlessdefined$str;14651466if(utf8::is_utf8($str) || utf8::decode($str)) {1467return$str;1468}else{1469return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1470}1471}14721473# quote unsafe chars, but keep the slash, even when it's not1474# correct, but quoted slashes look too horrible in bookmarks1475sub esc_param {1476my$str=shift;1477returnundefunlessdefined$str;1478$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1479$str=~s/ /\+/g;1480return$str;1481}14821483# the quoting rules for path_info fragment are slightly different1484sub esc_path_info {1485my$str=shift;1486returnundefunlessdefined$str;14871488# path_info doesn't treat '+' as space (specially), but '?' must be escaped1489$str=~s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;14901491return$str;1492}14931494# quote unsafe chars in whole URL, so some characters cannot be quoted1495sub esc_url {1496my$str=shift;1497returnundefunlessdefined$str;1498$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;1499$str=~s/ /\+/g;1500return$str;1501}15021503# quote unsafe characters in HTML attributes1504sub esc_attr {15051506# for XHTML conformance escaping '"' to '"' is not enough1507return esc_html(@_);1508}15091510# replace invalid utf8 character with SUBSTITUTION sequence1511sub esc_html {1512my$str=shift;1513my%opts=@_;15141515returnundefunlessdefined$str;15161517$str= to_utf8($str);1518$str=$cgi->escapeHTML($str);1519if($opts{'-nbsp'}) {1520$str=~s/ / /g;1521}1522$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1523return$str;1524}15251526# quote control characters and escape filename to HTML1527sub esc_path {1528my$str=shift;1529my%opts=@_;15301531returnundefunlessdefined$str;15321533$str= to_utf8($str);1534$str=$cgi->escapeHTML($str);1535if($opts{'-nbsp'}) {1536$str=~s/ / /g;1537}1538$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1539return$str;1540}15411542# Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)1543sub sanitize {1544my$str=shift;15451546returnundefunlessdefined$str;15471548$str= to_utf8($str);1549$str=~ s|([[:cntrl:]])|($1=~/[\t\n\r]/?$1: quot_cec($1))|eg;1550return$str;1551}15521553# Make control characters "printable", using character escape codes (CEC)1554sub quot_cec {1555my$cntrl=shift;1556my%opts=@_;1557my%es= (# character escape codes, aka escape sequences1558"\t"=>'\t',# tab (HT)1559"\n"=>'\n',# line feed (LF)1560"\r"=>'\r',# carrige return (CR)1561"\f"=>'\f',# form feed (FF)1562"\b"=>'\b',# backspace (BS)1563"\a"=>'\a',# alarm (bell) (BEL)1564"\e"=>'\e',# escape (ESC)1565"\013"=>'\v',# vertical tab (VT)1566"\000"=>'\0',# nul character (NUL)1567);1568my$chr= ( (exists$es{$cntrl})1569?$es{$cntrl}1570:sprintf('\%2x',ord($cntrl)) );1571if($opts{-nohtml}) {1572return$chr;1573}else{1574return"<span class=\"cntrl\">$chr</span>";1575}1576}15771578# Alternatively use unicode control pictures codepoints,1579# Unicode "printable representation" (PR)1580sub quot_upr {1581my$cntrl=shift;1582my%opts=@_;15831584my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1585if($opts{-nohtml}) {1586return$chr;1587}else{1588return"<span class=\"cntrl\">$chr</span>";1589}1590}15911592# git may return quoted and escaped filenames1593sub unquote {1594my$str=shift;15951596sub unq {1597my$seq=shift;1598my%es= (# character escape codes, aka escape sequences1599't'=>"\t",# tab (HT, TAB)1600'n'=>"\n",# newline (NL)1601'r'=>"\r",# return (CR)1602'f'=>"\f",# form feed (FF)1603'b'=>"\b",# backspace (BS)1604'a'=>"\a",# alarm (bell) (BEL)1605'e'=>"\e",# escape (ESC)1606'v'=>"\013",# vertical tab (VT)1607);16081609if($seq=~m/^[0-7]{1,3}$/) {1610# octal char sequence1611returnchr(oct($seq));1612}elsif(exists$es{$seq}) {1613# C escape sequence, aka character escape code1614return$es{$seq};1615}1616# quoted ordinary character1617return$seq;1618}16191620if($str=~m/^"(.*)"$/) {1621# needs unquoting1622$str=$1;1623$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1624}1625return$str;1626}16271628# escape tabs (convert tabs to spaces)1629sub untabify {1630my$line=shift;16311632while((my$pos=index($line,"\t")) != -1) {1633if(my$count= (8- ($pos%8))) {1634my$spaces=' ' x $count;1635$line=~s/\t/$spaces/;1636}1637}16381639return$line;1640}16411642sub project_in_list {1643my$project=shift;1644my@list= git_get_projects_list();1645return@list&&scalar(grep{$_->{'path'}eq$project}@list);1646}16471648## ----------------------------------------------------------------------1649## HTML aware string manipulation16501651# Try to chop given string on a word boundary between position1652# $len and $len+$add_len. If there is no word boundary there,1653# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1654# (marking chopped part) would be longer than given string.1655sub chop_str {1656my$str=shift;1657my$len=shift;1658my$add_len=shift||10;1659my$where=shift||'right';# 'left' | 'center' | 'right'16601661# Make sure perl knows it is utf8 encoded so we don't1662# cut in the middle of a utf8 multibyte char.1663$str= to_utf8($str);16641665# allow only $len chars, but don't cut a word if it would fit in $add_len1666# if it doesn't fit, cut it if it's still longer than the dots we would add1667# remove chopped character entities entirely16681669# when chopping in the middle, distribute $len into left and right part1670# return early if chopping wouldn't make string shorter1671if($whereeq'center') {1672return$strif($len+5>=length($str));# filler is length 51673$len=int($len/2);1674}else{1675return$strif($len+4>=length($str));# filler is length 41676}16771678# regexps: ending and beginning with word part up to $add_len1679my$endre=qr/.{$len}\w{0,$add_len}/;1680my$begre=qr/\w{0,$add_len}.{$len}/;16811682if($whereeq'left') {1683$str=~m/^(.*?)($begre)$/;1684my($lead,$body) = ($1,$2);1685if(length($lead) >4) {1686$lead=" ...";1687}1688return"$lead$body";16891690}elsif($whereeq'center') {1691$str=~m/^($endre)(.*)$/;1692my($left,$str) = ($1,$2);1693$str=~m/^(.*?)($begre)$/;1694my($mid,$right) = ($1,$2);1695if(length($mid) >5) {1696$mid=" ... ";1697}1698return"$left$mid$right";16991700}else{1701$str=~m/^($endre)(.*)$/;1702my$body=$1;1703my$tail=$2;1704if(length($tail) >4) {1705$tail="... ";1706}1707return"$body$tail";1708}1709}17101711# takes the same arguments as chop_str, but also wraps a <span> around the1712# result with a title attribute if it does get chopped. Additionally, the1713# string is HTML-escaped.1714sub chop_and_escape_str {1715my($str) =@_;17161717my$chopped= chop_str(@_);1718$str= to_utf8($str);1719if($choppedeq$str) {1720return esc_html($chopped);1721}else{1722$str=~s/[[:cntrl:]]/?/g;1723return$cgi->span({-title=>$str}, esc_html($chopped));1724}1725}17261727## ----------------------------------------------------------------------1728## functions returning short strings17291730# CSS class for given age value (in seconds)1731sub age_class {1732my$age=shift;17331734if(!defined$age) {1735return"noage";1736}elsif($age<60*60*2) {1737return"age0";1738}elsif($age<60*60*24*2) {1739return"age1";1740}else{1741return"age2";1742}1743}17441745# convert age in seconds to "nn units ago" string1746sub age_string {1747my$age=shift;1748my$age_str;17491750if($age>60*60*24*365*2) {1751$age_str= (int$age/60/60/24/365);1752$age_str.=" years ago";1753}elsif($age>60*60*24*(365/12)*2) {1754$age_str=int$age/60/60/24/(365/12);1755$age_str.=" months ago";1756}elsif($age>60*60*24*7*2) {1757$age_str=int$age/60/60/24/7;1758$age_str.=" weeks ago";1759}elsif($age>60*60*24*2) {1760$age_str=int$age/60/60/24;1761$age_str.=" days ago";1762}elsif($age>60*60*2) {1763$age_str=int$age/60/60;1764$age_str.=" hours ago";1765}elsif($age>60*2) {1766$age_str=int$age/60;1767$age_str.=" min ago";1768}elsif($age>2) {1769$age_str=int$age;1770$age_str.=" sec ago";1771}else{1772$age_str.=" right now";1773}1774return$age_str;1775}17761777useconstant{1778 S_IFINVALID =>0030000,1779 S_IFGITLINK =>0160000,1780};17811782# submodule/subproject, a commit object reference1783sub S_ISGITLINK {1784my$mode=shift;17851786return(($mode& S_IFMT) == S_IFGITLINK)1787}17881789# convert file mode in octal to symbolic file mode string1790sub mode_str {1791my$mode=oct shift;17921793if(S_ISGITLINK($mode)) {1794return'm---------';1795}elsif(S_ISDIR($mode& S_IFMT)) {1796return'drwxr-xr-x';1797}elsif(S_ISLNK($mode)) {1798return'lrwxrwxrwx';1799}elsif(S_ISREG($mode)) {1800# git cares only about the executable bit1801if($mode& S_IXUSR) {1802return'-rwxr-xr-x';1803}else{1804return'-rw-r--r--';1805};1806}else{1807return'----------';1808}1809}18101811# convert file mode in octal to file type string1812sub file_type {1813my$mode=shift;18141815if($mode!~m/^[0-7]+$/) {1816return$mode;1817}else{1818$mode=oct$mode;1819}18201821if(S_ISGITLINK($mode)) {1822return"submodule";1823}elsif(S_ISDIR($mode& S_IFMT)) {1824return"directory";1825}elsif(S_ISLNK($mode)) {1826return"symlink";1827}elsif(S_ISREG($mode)) {1828return"file";1829}else{1830return"unknown";1831}1832}18331834# convert file mode in octal to file type description string1835sub file_type_long {1836my$mode=shift;18371838if($mode!~m/^[0-7]+$/) {1839return$mode;1840}else{1841$mode=oct$mode;1842}18431844if(S_ISGITLINK($mode)) {1845return"submodule";1846}elsif(S_ISDIR($mode& S_IFMT)) {1847return"directory";1848}elsif(S_ISLNK($mode)) {1849return"symlink";1850}elsif(S_ISREG($mode)) {1851if($mode& S_IXUSR) {1852return"executable";1853}else{1854return"file";1855};1856}else{1857return"unknown";1858}1859}186018611862## ----------------------------------------------------------------------1863## functions returning short HTML fragments, or transforming HTML fragments1864## which don't belong to other sections18651866# format line of commit message.1867sub format_log_line_html {1868my$line=shift;18691870$line= esc_html($line, -nbsp=>1);1871$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1872$cgi->a({-href => href(action=>"object", hash=>$1),1873-class=>"text"},$1);1874}eg;18751876return$line;1877}18781879# format marker of refs pointing to given object18801881# the destination action is chosen based on object type and current context:1882# - for annotated tags, we choose the tag view unless it's the current view1883# already, in which case we go to shortlog view1884# - for other refs, we keep the current view if we're in history, shortlog or1885# log view, and select shortlog otherwise1886sub format_ref_marker {1887my($refs,$id) =@_;1888my$markers='';18891890if(defined$refs->{$id}) {1891foreachmy$ref(@{$refs->{$id}}) {1892# this code exploits the fact that non-lightweight tags are the1893# only indirect objects, and that they are the only objects for which1894# we want to use tag instead of shortlog as action1895my($type,$name) =qw();1896my$indirect= ($ref=~s/\^\{\}$//);1897# e.g. tags/v2.6.11 or heads/next1898if($ref=~m!^(.*?)s?/(.*)$!) {1899$type=$1;1900$name=$2;1901}else{1902$type="ref";1903$name=$ref;1904}19051906my$class=$type;1907$class.=" indirect"if$indirect;19081909my$dest_action="shortlog";19101911if($indirect) {1912$dest_action="tag"unless$actioneq"tag";1913}elsif($action=~/^(history|(short)?log)$/) {1914$dest_action=$action;1915}19161917my$dest="";1918$dest.="refs/"unless$ref=~ m!^refs/!;1919$dest.=$ref;19201921my$link=$cgi->a({1922-href => href(1923 action=>$dest_action,1924 hash=>$dest1925)},$name);19261927$markers.=" <span class=\"".esc_attr($class)."\"title=\"".esc_attr($ref)."\">".1928$link."</span>";1929}1930}19311932if($markers) {1933return' <span class="refs">'.$markers.'</span>';1934}else{1935return"";1936}1937}19381939# format, perhaps shortened and with markers, title line1940sub format_subject_html {1941my($long,$short,$href,$extra) =@_;1942$extra=''unlessdefined($extra);19431944if(length($short) <length($long)) {1945$long=~s/[[:cntrl:]]/?/g;1946return$cgi->a({-href =>$href, -class=>"list subject",1947-title => to_utf8($long)},1948 esc_html($short)) .$extra;1949}else{1950return$cgi->a({-href =>$href, -class=>"list subject"},1951 esc_html($long)) .$extra;1952}1953}19541955# Rather than recomputing the url for an email multiple times, we cache it1956# after the first hit. This gives a visible benefit in views where the avatar1957# for the same email is used repeatedly (e.g. shortlog).1958# The cache is shared by all avatar engines (currently gravatar only), which1959# are free to use it as preferred. Since only one avatar engine is used for any1960# given page, there's no risk for cache conflicts.1961our%avatar_cache= ();19621963# Compute the picon url for a given email, by using the picon search service over at1964# http://www.cs.indiana.edu/picons/search.html1965sub picon_url {1966my$email=lc shift;1967if(!$avatar_cache{$email}) {1968my($user,$domain) =split('@',$email);1969$avatar_cache{$email} =1970"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1971"$domain/$user/".1972"users+domains+unknown/up/single";1973}1974return$avatar_cache{$email};1975}19761977# Compute the gravatar url for a given email, if it's not in the cache already.1978# Gravatar stores only the part of the URL before the size, since that's the1979# one computationally more expensive. This also allows reuse of the cache for1980# different sizes (for this particular engine).1981sub gravatar_url {1982my$email=lc shift;1983my$size=shift;1984$avatar_cache{$email} ||=1985"http://www.gravatar.com/avatar/".1986 Digest::MD5::md5_hex($email) ."?s=";1987return$avatar_cache{$email} .$size;1988}19891990# Insert an avatar for the given $email at the given $size if the feature1991# is enabled.1992sub git_get_avatar {1993my($email,%opts) =@_;1994my$pre_white= ($opts{-pad_before} ?" ":"");1995my$post_white= ($opts{-pad_after} ?" ":"");1996$opts{-size} ||='default';1997my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1998my$url="";1999if($git_avatareq'gravatar') {2000$url= gravatar_url($email,$size);2001}elsif($git_avatareq'picon') {2002$url= picon_url($email);2003}2004# Other providers can be added by extending the if chain, defining $url2005# as needed. If no variant puts something in $url, we assume avatars2006# are completely disabled/unavailable.2007if($url) {2008return$pre_white.2009"<img width=\"$size\"".2010"class=\"avatar\"".2011"src=\"".esc_url($url)."\"".2012"alt=\"\"".2013"/>".$post_white;2014}else{2015return"";2016}2017}20182019sub format_search_author {2020my($author,$searchtype,$displaytext) =@_;2021my$have_search= gitweb_check_feature('search');20222023if($have_search) {2024my$performed="";2025if($searchtypeeq'author') {2026$performed="authored";2027}elsif($searchtypeeq'committer') {2028$performed="committed";2029}20302031return$cgi->a({-href => href(action=>"search", hash=>$hash,2032 searchtext=>$author,2033 searchtype=>$searchtype),class=>"list",2034 title=>"Search for commits$performedby$author"},2035$displaytext);20362037}else{2038return$displaytext;2039}2040}20412042# format the author name of the given commit with the given tag2043# the author name is chopped and escaped according to the other2044# optional parameters (see chop_str).2045sub format_author_html {2046my$tag=shift;2047my$co=shift;2048my$author= chop_and_escape_str($co->{'author_name'},@_);2049return"<$tagclass=\"author\">".2050 format_search_author($co->{'author_name'},"author",2051 git_get_avatar($co->{'author_email'}, -pad_after =>1) .2052$author) .2053"</$tag>";2054}20552056# format git diff header line, i.e. "diff --(git|combined|cc) ..."2057sub format_git_diff_header_line {2058my$line=shift;2059my$diffinfo=shift;2060my($from,$to) =@_;20612062if($diffinfo->{'nparents'}) {2063# combined diff2064$line=~s!^(diff (.*?) )"?.*$!$1!;2065if($to->{'href'}) {2066$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2067 esc_path($to->{'file'}));2068}else{# file was deleted (no href)2069$line.= esc_path($to->{'file'});2070}2071}else{2072# "ordinary" diff2073$line=~s!^(diff (.*?) )"?a/.*$!$1!;2074if($from->{'href'}) {2075$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},2076'a/'. esc_path($from->{'file'}));2077}else{# file was added (no href)2078$line.='a/'. esc_path($from->{'file'});2079}2080$line.=' ';2081if($to->{'href'}) {2082$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2083'b/'. esc_path($to->{'file'}));2084}else{# file was deleted2085$line.='b/'. esc_path($to->{'file'});2086}2087}20882089return"<div class=\"diff header\">$line</div>\n";2090}20912092# format extended diff header line, before patch itself2093sub format_extended_diff_header_line {2094my$line=shift;2095my$diffinfo=shift;2096my($from,$to) =@_;20972098# match <path>2099if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {2100$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},2101 esc_path($from->{'file'}));2102}2103if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {2104$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},2105 esc_path($to->{'file'}));2106}2107# match single <mode>2108if($line=~m/\s(\d{6})$/) {2109$line.='<span class="info"> ('.2110 file_type_long($1) .2111')</span>';2112}2113# match <hash>2114if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {2115# can match only for combined diff2116$line='index ';2117for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2118if($from->{'href'}[$i]) {2119$line.=$cgi->a({-href=>$from->{'href'}[$i],2120-class=>"hash"},2121substr($diffinfo->{'from_id'}[$i],0,7));2122}else{2123$line.='0' x 7;2124}2125# separator2126$line.=','if($i<$diffinfo->{'nparents'} -1);2127}2128$line.='..';2129if($to->{'href'}) {2130$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2131substr($diffinfo->{'to_id'},0,7));2132}else{2133$line.='0' x 7;2134}21352136}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {2137# can match only for ordinary diff2138my($from_link,$to_link);2139if($from->{'href'}) {2140$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},2141substr($diffinfo->{'from_id'},0,7));2142}else{2143$from_link='0' x 7;2144}2145if($to->{'href'}) {2146$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2147substr($diffinfo->{'to_id'},0,7));2148}else{2149$to_link='0' x 7;2150}2151my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});2152$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;2153}21542155return$line."<br/>\n";2156}21572158# format from-file/to-file diff header2159sub format_diff_from_to_header {2160my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;2161my$line;2162my$result='';21632164$line=$from_line;2165#assert($line =~ m/^---/) if DEBUG;2166# no extra formatting for "^--- /dev/null"2167if(!$diffinfo->{'nparents'}) {2168# ordinary (single parent) diff2169if($line=~m!^--- "?a/!) {2170if($from->{'href'}) {2171$line='--- a/'.2172$cgi->a({-href=>$from->{'href'}, -class=>"path"},2173 esc_path($from->{'file'}));2174}else{2175$line='--- a/'.2176 esc_path($from->{'file'});2177}2178}2179$result.= qq!<div class="diff from_file">$line</div>\n!;21802181}else{2182# combined diff (merge commit)2183for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2184if($from->{'href'}[$i]) {2185$line='--- '.2186$cgi->a({-href=>href(action=>"blobdiff",2187 hash_parent=>$diffinfo->{'from_id'}[$i],2188 hash_parent_base=>$parents[$i],2189 file_parent=>$from->{'file'}[$i],2190 hash=>$diffinfo->{'to_id'},2191 hash_base=>$hash,2192 file_name=>$to->{'file'}),2193-class=>"path",2194-title=>"diff". ($i+1)},2195$i+1) .2196'/'.2197$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2198 esc_path($from->{'file'}[$i]));2199}else{2200$line='--- /dev/null';2201}2202$result.= qq!<div class="diff from_file">$line</div>\n!;2203}2204}22052206$line=$to_line;2207#assert($line =~ m/^\+\+\+/) if DEBUG;2208# no extra formatting for "^+++ /dev/null"2209if($line=~m!^\+\+\+ "?b/!) {2210if($to->{'href'}) {2211$line='+++ b/'.2212$cgi->a({-href=>$to->{'href'}, -class=>"path"},2213 esc_path($to->{'file'}));2214}else{2215$line='+++ b/'.2216 esc_path($to->{'file'});2217}2218}2219$result.= qq!<div class="diff to_file">$line</div>\n!;22202221return$result;2222}22232224# create note for patch simplified by combined diff2225sub format_diff_cc_simplified {2226my($diffinfo,@parents) =@_;2227my$result='';22282229$result.="<div class=\"diff header\">".2230"diff --cc ";2231if(!is_deleted($diffinfo)) {2232$result.=$cgi->a({-href => href(action=>"blob",2233 hash_base=>$hash,2234 hash=>$diffinfo->{'to_id'},2235 file_name=>$diffinfo->{'to_file'}),2236-class=>"path"},2237 esc_path($diffinfo->{'to_file'}));2238}else{2239$result.= esc_path($diffinfo->{'to_file'});2240}2241$result.="</div>\n".# class="diff header"2242"<div class=\"diff nodifferences\">".2243"Simple merge".2244"</div>\n";# class="diff nodifferences"22452246return$result;2247}22482249sub diff_line_class {2250my($line,$from,$to) =@_;22512252# ordinary diff2253my$num_sign=1;2254# combined diff2255if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2256$num_sign=scalar@{$from->{'href'}};2257}22582259my@diff_line_classifier= (2260{ regexp =>qr/^\@\@{$num_sign} /,class=>"chunk_header"},2261{ regexp =>qr/^\\/,class=>"incomplete"},2262{ regexp =>qr/^ {$num_sign}/,class=>"ctx"},2263# classifier for context must come before classifier add/rem,2264# or we would have to use more complicated regexp, for example2265# qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;2266{ regexp =>qr/^[+ ]{$num_sign}/,class=>"add"},2267{ regexp =>qr/^[- ]{$num_sign}/,class=>"rem"},2268);2269formy$clsfy(@diff_line_classifier) {2270return$clsfy->{'class'}2271if($line=~$clsfy->{'regexp'});2272}22732274# fallback2275return"";2276}22772278# assumes that $from and $to are defined and correctly filled,2279# and that $line holds a line of chunk header for unified diff2280sub format_unidiff_chunk_header {2281my($line,$from,$to) =@_;22822283my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2284$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;22852286$from_lines=0unlessdefined$from_lines;2287$to_lines=0unlessdefined$to_lines;22882289if($from->{'href'}) {2290$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2291-class=>"list"},$from_text);2292}2293if($to->{'href'}) {2294$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2295-class=>"list"},$to_text);2296}2297$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2298"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2299return$line;2300}23012302# assumes that $from and $to are defined and correctly filled,2303# and that $line holds a line of chunk header for combined diff2304sub format_cc_diff_chunk_header {2305my($line,$from,$to) =@_;23062307my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2308my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);23092310@from_text=split(' ',$ranges);2311for(my$i=0;$i<@from_text; ++$i) {2312($from_start[$i],$from_nlines[$i]) =2313(split(',',substr($from_text[$i],1)),0);2314}23152316$to_text=pop@from_text;2317$to_start=pop@from_start;2318$to_nlines=pop@from_nlines;23192320$line="<span class=\"chunk_info\">$prefix";2321for(my$i=0;$i<@from_text; ++$i) {2322if($from->{'href'}[$i]) {2323$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2324-class=>"list"},$from_text[$i]);2325}else{2326$line.=$from_text[$i];2327}2328$line.=" ";2329}2330if($to->{'href'}) {2331$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2332-class=>"list"},$to_text);2333}else{2334$line.=$to_text;2335}2336$line.="$prefix</span>".2337"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2338return$line;2339}23402341# process patch (diff) line (not to be used for diff headers),2342# returning class and HTML-formatted (but not wrapped) line2343sub process_diff_line {2344my$line=shift;2345my($from,$to) =@_;23462347my$diff_class= diff_line_class($line,$from,$to);23482349chomp$line;2350$line= untabify($line);23512352if($from&&$to&&$line=~m/^\@{2} /) {2353$line= format_unidiff_chunk_header($line,$from,$to);2354return$diff_class,$line;23552356}elsif($from&&$to&&$line=~m/^\@{3}/) {2357$line= format_cc_diff_chunk_header($line,$from,$to);2358return$diff_class,$line;23592360}2361return$diff_class, esc_html($line, -nbsp=>1);2362}23632364# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2365# linked. Pass the hash of the tree/commit to snapshot.2366sub format_snapshot_links {2367my($hash) =@_;2368my$num_fmts=@snapshot_fmts;2369if($num_fmts>1) {2370# A parenthesized list of links bearing format names.2371# e.g. "snapshot (_tar.gz_ _zip_)"2372return"snapshot (".join(' ',map2373$cgi->a({2374-href => href(2375 action=>"snapshot",2376 hash=>$hash,2377 snapshot_format=>$_2378)2379},$known_snapshot_formats{$_}{'display'})2380,@snapshot_fmts) .")";2381}elsif($num_fmts==1) {2382# A single "snapshot" link whose tooltip bears the format name.2383# i.e. "_snapshot_"2384my($fmt) =@snapshot_fmts;2385return2386$cgi->a({2387-href => href(2388 action=>"snapshot",2389 hash=>$hash,2390 snapshot_format=>$fmt2391),2392-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2393},"snapshot");2394}else{# $num_fmts == 02395returnundef;2396}2397}23982399## ......................................................................2400## functions returning values to be passed, perhaps after some2401## transformation, to other functions; e.g. returning arguments to href()24022403# returns hash to be passed to href to generate gitweb URL2404# in -title key it returns description of link2405sub get_feed_info {2406my$format=shift||'Atom';2407my%res= (action =>lc($format));24082409# feed links are possible only for project views2410return unless(defined$project);2411# some views should link to OPML, or to generic project feed,2412# or don't have specific feed yet (so they should use generic)2413return if(!$action||$action=~/^(?:tags|heads|forks|tag|search)$/x);24142415my$branch;2416# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2417# from tag links; this also makes possible to detect branch links2418if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2419(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2420$branch=$1;2421}2422# find log type for feed description (title)2423my$type='log';2424if(defined$file_name) {2425$type="history of$file_name";2426$type.="/"if($actioneq'tree');2427$type.=" on '$branch'"if(defined$branch);2428}else{2429$type="log of$branch"if(defined$branch);2430}24312432$res{-title} =$type;2433$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2434$res{'file_name'} =$file_name;24352436return%res;2437}24382439## ----------------------------------------------------------------------2440## git utility subroutines, invoking git commands24412442# returns path to the core git executable and the --git-dir parameter as list2443sub git_cmd {2444$number_of_git_cmds++;2445return$GIT,'--git-dir='.$git_dir;2446}24472448# quote the given arguments for passing them to the shell2449# quote_command("command", "arg 1", "arg with ' and ! characters")2450# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2451# Try to avoid using this function wherever possible.2452sub quote_command {2453returnjoin(' ',2454map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2455}24562457# get HEAD ref of given project as hash2458sub git_get_head_hash {2459return git_get_full_hash(shift,'HEAD');2460}24612462sub git_get_full_hash {2463return git_get_hash(@_);2464}24652466sub git_get_short_hash {2467return git_get_hash(@_,'--short=7');2468}24692470sub git_get_hash {2471my($project,$hash,@options) =@_;2472my$o_git_dir=$git_dir;2473my$retval=undef;2474$git_dir="$projectroot/$project";2475if(open my$fd,'-|', git_cmd(),'rev-parse',2476'--verify','-q',@options,$hash) {2477$retval= <$fd>;2478chomp$retvalifdefined$retval;2479close$fd;2480}2481if(defined$o_git_dir) {2482$git_dir=$o_git_dir;2483}2484return$retval;2485}24862487# get type of given object2488sub git_get_type {2489my$hash=shift;24902491open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2492my$type= <$fd>;2493close$fdorreturn;2494chomp$type;2495return$type;2496}24972498# repository configuration2499our$config_file='';2500our%config;25012502# store multiple values for single key as anonymous array reference2503# single values stored directly in the hash, not as [ <value> ]2504sub hash_set_multi {2505my($hash,$key,$value) =@_;25062507if(!exists$hash->{$key}) {2508$hash->{$key} =$value;2509}elsif(!ref$hash->{$key}) {2510$hash->{$key} = [$hash->{$key},$value];2511}else{2512push@{$hash->{$key}},$value;2513}2514}25152516# return hash of git project configuration2517# optionally limited to some section, e.g. 'gitweb'2518sub git_parse_project_config {2519my$section_regexp=shift;2520my%config;25212522local$/="\0";25232524open my$fh,"-|", git_cmd(),"config",'-z','-l',2525orreturn;25262527while(my$keyval= <$fh>) {2528chomp$keyval;2529my($key,$value) =split(/\n/,$keyval,2);25302531 hash_set_multi(\%config,$key,$value)2532if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2533}2534close$fh;25352536return%config;2537}25382539# convert config value to boolean: 'true' or 'false'2540# no value, number > 0, 'true' and 'yes' values are true2541# rest of values are treated as false (never as error)2542sub config_to_bool {2543my$val=shift;25442545return1if!defined$val;# section.key25462547# strip leading and trailing whitespace2548$val=~s/^\s+//;2549$val=~s/\s+$//;25502551return(($val=~/^\d+$/&&$val) ||# section.key = 12552($val=~/^(?:true|yes)$/i));# section.key = true2553}25542555# convert config value to simple decimal number2556# an optional value suffix of 'k', 'm', or 'g' will cause the value2557# to be multiplied by 1024, 1048576, or 10737418242558sub config_to_int {2559my$val=shift;25602561# strip leading and trailing whitespace2562$val=~s/^\s+//;2563$val=~s/\s+$//;25642565if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2566$unit=lc($unit);2567# unknown unit is treated as 12568return$num* ($uniteq'g'?1073741824:2569$uniteq'm'?1048576:2570$uniteq'k'?1024:1);2571}2572return$val;2573}25742575# convert config value to array reference, if needed2576sub config_to_multi {2577my$val=shift;25782579returnref($val) ?$val: (defined($val) ? [$val] : []);2580}25812582sub git_get_project_config {2583my($key,$type) =@_;25842585return unlessdefined$git_dir;25862587# key sanity check2588return unless($key);2589# only subsection, if exists, is case sensitive,2590# and not lowercased by 'git config -z -l'2591if(my($hi,$mi,$lo) = ($key=~/^([^.]*)\.(.*)\.([^.]*)$/)) {2592$key=join(".",lc($hi),$mi,lc($lo));2593}else{2594$key=lc($key);2595}2596$key=~s/^gitweb\.//;2597return if($key=~m/\W/);25982599# type sanity check2600if(defined$type) {2601$type=~s/^--//;2602$type=undef2603unless($typeeq'bool'||$typeeq'int');2604}26052606# get config2607if(!defined$config_file||2608$config_filene"$git_dir/config") {2609%config= git_parse_project_config('gitweb');2610$config_file="$git_dir/config";2611}26122613# check if config variable (key) exists2614return unlessexists$config{"gitweb.$key"};26152616# ensure given type2617if(!defined$type) {2618return$config{"gitweb.$key"};2619}elsif($typeeq'bool') {2620# backward compatibility: 'git config --bool' returns true/false2621return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2622}elsif($typeeq'int') {2623return config_to_int($config{"gitweb.$key"});2624}2625return$config{"gitweb.$key"};2626}26272628# get hash of given path at given ref2629sub git_get_hash_by_path {2630my$base=shift;2631my$path=shift||returnundef;2632my$type=shift;26332634$path=~ s,/+$,,;26352636open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2637or die_error(500,"Open git-ls-tree failed");2638my$line= <$fd>;2639close$fdorreturnundef;26402641if(!defined$line) {2642# there is no tree or hash given by $path at $base2643returnundef;2644}26452646#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2647$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2648if(defined$type&&$typene$2) {2649# type doesn't match2650returnundef;2651}2652return$3;2653}26542655# get path of entry with given hash at given tree-ish (ref)2656# used to get 'from' filename for combined diff (merge commit) for renames2657sub git_get_path_by_hash {2658my$base=shift||return;2659my$hash=shift||return;26602661local$/="\0";26622663open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2664orreturnundef;2665while(my$line= <$fd>) {2666chomp$line;26672668#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2669#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2670if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2671close$fd;2672return$1;2673}2674}2675close$fd;2676returnundef;2677}26782679## ......................................................................2680## git utility functions, directly accessing git repository26812682# get the value of config variable either from file named as the variable2683# itself in the repository ($GIT_DIR/$name file), or from gitweb.$name2684# configuration variable in the repository config file.2685sub git_get_file_or_project_config {2686my($path,$name) =@_;26872688$git_dir="$projectroot/$path";2689open my$fd,'<',"$git_dir/$name"2690orreturn git_get_project_config($name);2691my$conf= <$fd>;2692close$fd;2693if(defined$conf) {2694chomp$conf;2695}2696return$conf;2697}26982699sub git_get_project_description {2700my$path=shift;2701return git_get_file_or_project_config($path,'description');2702}27032704sub git_get_project_category {2705my$path=shift;2706return git_get_file_or_project_config($path,'category');2707}270827092710# supported formats:2711# * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)2712# - if its contents is a number, use it as tag weight,2713# - otherwise add a tag with weight 12714# * $GIT_DIR/ctags file, each line is a tag (with weight 1)2715# the same value multiple times increases tag weight2716# * `gitweb.ctag' multi-valued repo config variable2717sub git_get_project_ctags {2718my$project=shift;2719my$ctags= {};27202721$git_dir="$projectroot/$project";2722if(opendir my$dh,"$git_dir/ctags") {2723my@files=grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh);2724foreachmy$tagfile(@files) {2725open my$ct,'<',$tagfile2726ornext;2727my$val= <$ct>;2728chomp$valif$val;2729close$ct;27302731(my$ctag=$tagfile) =~ s#.*/##;2732if($val=~/^\d+$/) {2733$ctags->{$ctag} =$val;2734}else{2735$ctags->{$ctag} =1;2736}2737}2738closedir$dh;27392740}elsif(open my$fh,'<',"$git_dir/ctags") {2741while(my$line= <$fh>) {2742chomp$line;2743$ctags->{$line}++if$line;2744}2745close$fh;27462747}else{2748my$taglist= config_to_multi(git_get_project_config('ctag'));2749foreachmy$tag(@$taglist) {2750$ctags->{$tag}++;2751}2752}27532754return$ctags;2755}27562757# return hash, where keys are content tags ('ctags'),2758# and values are sum of weights of given tag in every project2759sub git_gather_all_ctags {2760my$projects=shift;2761my$ctags= {};27622763foreachmy$p(@$projects) {2764foreachmy$ct(keys%{$p->{'ctags'}}) {2765$ctags->{$ct} +=$p->{'ctags'}->{$ct};2766}2767}27682769return$ctags;2770}27712772sub git_populate_project_tagcloud {2773my$ctags=shift;27742775# First, merge different-cased tags; tags vote on casing2776my%ctags_lc;2777foreach(keys%$ctags) {2778$ctags_lc{lc$_}->{count} +=$ctags->{$_};2779if(not$ctags_lc{lc$_}->{topcount}2780or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2781$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2782$ctags_lc{lc$_}->{topname} =$_;2783}2784}27852786my$cloud;2787my$matched=$input_params{'ctag'};2788if(eval{require HTML::TagCloud;1; }) {2789$cloud= HTML::TagCloud->new;2790foreachmy$ctag(sort keys%ctags_lc) {2791# Pad the title with spaces so that the cloud looks2792# less crammed.2793my$title= esc_html($ctags_lc{$ctag}->{topname});2794$title=~s/ / /g;2795$title=~s/^/ /g;2796$title=~s/$/ /g;2797if(defined$matched&&$matchedeq$ctag) {2798$title=qq(<span class="match">$title</span>);2799}2800$cloud->add($title, href(project=>undef, ctag=>$ctag),2801$ctags_lc{$ctag}->{count});2802}2803}else{2804$cloud= {};2805foreachmy$ctag(keys%ctags_lc) {2806my$title= esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);2807if(defined$matched&&$matchedeq$ctag) {2808$title=qq(<span class="match">$title</span>);2809}2810$cloud->{$ctag}{count} =$ctags_lc{$ctag}->{count};2811$cloud->{$ctag}{ctag} =2812$cgi->a({-href=>href(project=>undef, ctag=>$ctag)},$title);2813}2814}2815return$cloud;2816}28172818sub git_show_project_tagcloud {2819my($cloud,$count) =@_;2820if(ref$cloudeq'HTML::TagCloud') {2821return$cloud->html_and_css($count);2822}else{2823my@tags=sort{$cloud->{$a}->{'count'} <=>$cloud->{$b}->{'count'} }keys%$cloud;2824return2825'<div id="htmltagcloud"'.($project?'':' align="center"').'>'.2826join(', ',map{2827$cloud->{$_}->{'ctag'}2828}splice(@tags,0,$count)) .2829'</div>';2830}2831}28322833sub git_get_project_url_list {2834my$path=shift;28352836$git_dir="$projectroot/$path";2837open my$fd,'<',"$git_dir/cloneurl"2838orreturnwantarray?2839@{ config_to_multi(git_get_project_config('url')) } :2840 config_to_multi(git_get_project_config('url'));2841my@git_project_url_list=map{chomp;$_} <$fd>;2842close$fd;28432844returnwantarray?@git_project_url_list: \@git_project_url_list;2845}28462847sub git_get_projects_list {2848my$filter=shift||'';2849my$paranoid=shift;2850my@list;28512852if(-d $projects_list) {2853# search in directory2854my$dir=$projects_list;2855# remove the trailing "/"2856$dir=~s!/+$!!;2857my$pfxlen=length("$dir");2858my$pfxdepth= ($dir=~tr!/!!);2859# when filtering, search only given subdirectory2860if($filter&& !$paranoid) {2861$dir.="/$filter";2862$dir=~s!/+$!!;2863}28642865 File::Find::find({2866 follow_fast =>1,# follow symbolic links2867 follow_skip =>2,# ignore duplicates2868 dangling_symlinks =>0,# ignore dangling symlinks, silently2869 wanted =>sub{2870# global variables2871our$project_maxdepth;2872our$projectroot;2873# skip project-list toplevel, if we get it.2874return if(m!^[/.]$!);2875# only directories can be git repositories2876return unless(-d $_);2877# don't traverse too deep (Find is super slow on os x)2878# $project_maxdepth excludes depth of $projectroot2879if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2880$File::Find::prune =1;2881return;2882}28832884my$path=substr($File::Find::name,$pfxlen+1);2885# paranoidly only filter here2886if($paranoid&&$filter&&$path!~m!^\Q$filter\E/!) {2887next;2888}2889# we check related file in $projectroot2890if(check_export_ok("$projectroot/$path")) {2891push@list, { path =>$path};2892$File::Find::prune =1;2893}2894},2895},"$dir");28962897}elsif(-f $projects_list) {2898# read from file(url-encoded):2899# 'git%2Fgit.git Linus+Torvalds'2900# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2901# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2902open my$fd,'<',$projects_listorreturn;2903 PROJECT:2904while(my$line= <$fd>) {2905chomp$line;2906my($path,$owner) =split' ',$line;2907$path= unescape($path);2908$owner= unescape($owner);2909if(!defined$path) {2910next;2911}2912# if $filter is rpovided, check if $path begins with $filter2913if($filter&&$path!~m!^\Q$filter\E/!) {2914next;2915}2916if(check_export_ok("$projectroot/$path")) {2917my$pr= {2918 path =>$path,2919 owner => to_utf8($owner),2920};2921push@list,$pr;2922}2923}2924close$fd;2925}2926return@list;2927}29282929# written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)2930# as side effects it sets 'forks' field to list of forks for forked projects2931sub filter_forks_from_projects_list {2932my$projects=shift;29332934my%trie;# prefix tree of directories (path components)2935# generate trie out of those directories that might contain forks2936foreachmy$pr(@$projects) {2937my$path=$pr->{'path'};2938$path=~s/\.git$//;# forks of 'repo.git' are in 'repo/' directory2939next if($path=~m!/$!);# skip non-bare repositories, e.g. 'repo/.git'2940next unless($path);# skip '.git' repository: tests, git-instaweb2941next unless(-d "$projectroot/$path");# containing directory exists2942$pr->{'forks'} = [];# there can be 0 or more forks of project29432944# add to trie2945my@dirs=split('/',$path);2946# walk the trie, until either runs out of components or out of trie2947my$ref= \%trie;2948while(scalar@dirs&&2949exists($ref->{$dirs[0]})) {2950$ref=$ref->{shift@dirs};2951}2952# create rest of trie structure from rest of components2953foreachmy$dir(@dirs) {2954$ref=$ref->{$dir} = {};2955}2956# create end marker, store $pr as a data2957$ref->{''} =$prif(!exists$ref->{''});2958}29592960# filter out forks, by finding shortest prefix match for paths2961my@filtered;2962 PROJECT:2963foreachmy$pr(@$projects) {2964# trie lookup2965my$ref= \%trie;2966 DIR:2967foreachmy$dir(split('/',$pr->{'path'})) {2968if(exists$ref->{''}) {2969# found [shortest] prefix, is a fork - skip it2970push@{$ref->{''}{'forks'}},$pr;2971next PROJECT;2972}2973if(!exists$ref->{$dir}) {2974# not in trie, cannot have prefix, not a fork2975push@filtered,$pr;2976next PROJECT;2977}2978# If the dir is there, we just walk one step down the trie.2979$ref=$ref->{$dir};2980}2981# we ran out of trie2982# (shouldn't happen: it's either no match, or end marker)2983push@filtered,$pr;2984}29852986return@filtered;2987}29882989# note: fill_project_list_info must be run first,2990# for 'descr_long' and 'ctags' to be filled2991sub search_projects_list {2992my($projlist,%opts) =@_;2993my$tagfilter=$opts{'tagfilter'};2994my$searchtext=$opts{'searchtext'};29952996return@$projlist2997unless($tagfilter||$searchtext);29982999# searching projects require filling to be run before it;3000 fill_project_list_info($projlist,3001$tagfilter?'ctags': (),3002$searchtext? ('path','descr') : ());3003my@projects;3004 PROJECT:3005foreachmy$pr(@$projlist) {30063007if($tagfilter) {3008next unlessref($pr->{'ctags'})eq'HASH';3009next unless3010grep{lc($_)eq lc($tagfilter) }keys%{$pr->{'ctags'}};3011}30123013if($searchtext) {3014next unless3015$pr->{'path'} =~/$searchtext/||3016$pr->{'descr_long'} =~/$searchtext/;3017}30183019push@projects,$pr;3020}30213022return@projects;3023}30243025our$gitweb_project_owner=undef;3026sub git_get_project_list_from_file {30273028return if(defined$gitweb_project_owner);30293030$gitweb_project_owner= {};3031# read from file (url-encoded):3032# 'git%2Fgit.git Linus+Torvalds'3033# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'3034# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'3035if(-f $projects_list) {3036open(my$fd,'<',$projects_list);3037while(my$line= <$fd>) {3038chomp$line;3039my($pr,$ow) =split' ',$line;3040$pr= unescape($pr);3041$ow= unescape($ow);3042$gitweb_project_owner->{$pr} = to_utf8($ow);3043}3044close$fd;3045}3046}30473048sub git_get_project_owner {3049my$project=shift;3050my$owner;30513052returnundefunless$project;3053$git_dir="$projectroot/$project";30543055if(!defined$gitweb_project_owner) {3056 git_get_project_list_from_file();3057}30583059if(exists$gitweb_project_owner->{$project}) {3060$owner=$gitweb_project_owner->{$project};3061}3062if(!defined$owner){3063$owner= git_get_project_config('owner');3064}3065if(!defined$owner) {3066$owner= get_file_owner("$git_dir");3067}30683069return$owner;3070}30713072sub git_get_last_activity {3073my($path) =@_;3074my$fd;30753076$git_dir="$projectroot/$path";3077open($fd,"-|", git_cmd(),'for-each-ref',3078'--format=%(committer)',3079'--sort=-committerdate',3080'--count=1',3081'refs/heads')orreturn;3082my$most_recent= <$fd>;3083close$fdorreturn;3084if(defined$most_recent&&3085$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {3086my$timestamp=$1;3087my$age=time-$timestamp;3088return($age, age_string($age));3089}3090return(undef,undef);3091}30923093# Implementation note: when a single remote is wanted, we cannot use 'git3094# remote show -n' because that command always work (assuming it's a remote URL3095# if it's not defined), and we cannot use 'git remote show' because that would3096# try to make a network roundtrip. So the only way to find if that particular3097# remote is defined is to walk the list provided by 'git remote -v' and stop if3098# and when we find what we want.3099sub git_get_remotes_list {3100my$wanted=shift;3101my%remotes= ();31023103open my$fd,'-|', git_cmd(),'remote','-v';3104return unless$fd;3105while(my$remote= <$fd>) {3106chomp$remote;3107$remote=~s!\t(.*?)\s+\((\w+)\)$!!;3108next if$wantedand not$remoteeq$wanted;3109my($url,$key) = ($1,$2);31103111$remotes{$remote} ||= {'heads'=> () };3112$remotes{$remote}{$key} =$url;3113}3114close$fdorreturn;3115returnwantarray?%remotes: \%remotes;3116}31173118# Takes a hash of remotes as first parameter and fills it by adding the3119# available remote heads for each of the indicated remotes.3120sub fill_remote_heads {3121my$remotes=shift;3122my@heads=map{"remotes/$_"}keys%$remotes;3123my@remoteheads= git_get_heads_list(undef,@heads);3124foreachmy$remote(keys%$remotes) {3125$remotes->{$remote}{'heads'} = [grep{3126$_->{'name'} =~s!^$remote/!!3127}@remoteheads];3128}3129}31303131sub git_get_references {3132my$type=shift||"";3133my%refs;3134# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.113135# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}3136open my$fd,"-|", git_cmd(),"show-ref","--dereference",3137($type? ("--","refs/$type") : ())# use -- <pattern> if $type3138orreturn;31393140while(my$line= <$fd>) {3141chomp$line;3142if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {3143if(defined$refs{$1}) {3144push@{$refs{$1}},$2;3145}else{3146$refs{$1} = [$2];3147}3148}3149}3150close$fdorreturn;3151return \%refs;3152}31533154sub git_get_rev_name_tags {3155my$hash=shift||returnundef;31563157open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash3158orreturn;3159my$name_rev= <$fd>;3160close$fd;31613162if($name_rev=~ m|^$hash tags/(.*)$|) {3163return$1;3164}else{3165# catches also '$hash undefined' output3166returnundef;3167}3168}31693170## ----------------------------------------------------------------------3171## parse to hash functions31723173sub parse_date {3174my$epoch=shift;3175my$tz=shift||"-0000";31763177my%date;3178my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");3179my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");3180my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);3181$date{'hour'} =$hour;3182$date{'minute'} =$min;3183$date{'mday'} =$mday;3184$date{'day'} =$days[$wday];3185$date{'month'} =$months[$mon];3186$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",3187$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;3188$date{'mday-time'} =sprintf"%d%s%02d:%02d",3189$mday,$months[$mon],$hour,$min;3190$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",31911900+$year,1+$mon,$mday,$hour,$min,$sec;31923193my($tz_sign,$tz_hour,$tz_min) =3194($tz=~m/^([-+])(\d\d)(\d\d)$/);3195$tz_sign= ($tz_signeq'-'? -1: +1);3196my$local=$epoch+$tz_sign*((($tz_hour*60) +$tz_min)*60);3197($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);3198$date{'hour_local'} =$hour;3199$date{'minute_local'} =$min;3200$date{'tz_local'} =$tz;3201$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",32021900+$year,$mon+1,$mday,3203$hour,$min,$sec,$tz);3204return%date;3205}32063207sub parse_tag {3208my$tag_id=shift;3209my%tag;3210my@comment;32113212open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;3213$tag{'id'} =$tag_id;3214while(my$line= <$fd>) {3215chomp$line;3216if($line=~m/^object ([0-9a-fA-F]{40})$/) {3217$tag{'object'} =$1;3218}elsif($line=~m/^type (.+)$/) {3219$tag{'type'} =$1;3220}elsif($line=~m/^tag (.+)$/) {3221$tag{'name'} =$1;3222}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {3223$tag{'author'} =$1;3224$tag{'author_epoch'} =$2;3225$tag{'author_tz'} =$3;3226if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {3227$tag{'author_name'} =$1;3228$tag{'author_email'} =$2;3229}else{3230$tag{'author_name'} =$tag{'author'};3231}3232}elsif($line=~m/--BEGIN/) {3233push@comment,$line;3234last;3235}elsif($lineeq"") {3236last;3237}3238}3239push@comment, <$fd>;3240$tag{'comment'} = \@comment;3241close$fdorreturn;3242if(!defined$tag{'name'}) {3243return3244};3245return%tag3246}32473248sub parse_commit_text {3249my($commit_text,$withparents) =@_;3250my@commit_lines=split'\n',$commit_text;3251my%co;32523253pop@commit_lines;# Remove '\0'32543255if(!@commit_lines) {3256return;3257}32583259my$header=shift@commit_lines;3260if($header!~m/^[0-9a-fA-F]{40}/) {3261return;3262}3263($co{'id'},my@parents) =split' ',$header;3264while(my$line=shift@commit_lines) {3265last if$lineeq"\n";3266if($line=~m/^tree ([0-9a-fA-F]{40})$/) {3267$co{'tree'} =$1;3268}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {3269push@parents,$1;3270}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {3271$co{'author'} = to_utf8($1);3272$co{'author_epoch'} =$2;3273$co{'author_tz'} =$3;3274if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {3275$co{'author_name'} =$1;3276$co{'author_email'} =$2;3277}else{3278$co{'author_name'} =$co{'author'};3279}3280}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {3281$co{'committer'} = to_utf8($1);3282$co{'committer_epoch'} =$2;3283$co{'committer_tz'} =$3;3284if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {3285$co{'committer_name'} =$1;3286$co{'committer_email'} =$2;3287}else{3288$co{'committer_name'} =$co{'committer'};3289}3290}3291}3292if(!defined$co{'tree'}) {3293return;3294};3295$co{'parents'} = \@parents;3296$co{'parent'} =$parents[0];32973298foreachmy$title(@commit_lines) {3299$title=~s/^ //;3300if($titlene"") {3301$co{'title'} = chop_str($title,80,5);3302# remove leading stuff of merges to make the interesting part visible3303if(length($title) >50) {3304$title=~s/^Automatic //;3305$title=~s/^merge (of|with) /Merge ... /i;3306if(length($title) >50) {3307$title=~s/(http|rsync):\/\///;3308}3309if(length($title) >50) {3310$title=~s/(master|www|rsync)\.//;3311}3312if(length($title) >50) {3313$title=~s/kernel.org:?//;3314}3315if(length($title) >50) {3316$title=~s/\/pub\/scm//;3317}3318}3319$co{'title_short'} = chop_str($title,50,5);3320last;3321}3322}3323if(!defined$co{'title'} ||$co{'title'}eq"") {3324$co{'title'} =$co{'title_short'} ='(no commit message)';3325}3326# remove added spaces3327foreachmy$line(@commit_lines) {3328$line=~s/^ //;3329}3330$co{'comment'} = \@commit_lines;33313332my$age=time-$co{'committer_epoch'};3333$co{'age'} =$age;3334$co{'age_string'} = age_string($age);3335my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});3336if($age>60*60*24*7*2) {3337$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3338$co{'age_string_age'} =$co{'age_string'};3339}else{3340$co{'age_string_date'} =$co{'age_string'};3341$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3342}3343return%co;3344}33453346sub parse_commit {3347my($commit_id) =@_;3348my%co;33493350local$/="\0";33513352open my$fd,"-|", git_cmd(),"rev-list",3353"--parents",3354"--header",3355"--max-count=1",3356$commit_id,3357"--",3358or die_error(500,"Open git-rev-list failed");3359%co= parse_commit_text(<$fd>,1);3360close$fd;33613362return%co;3363}33643365sub parse_commits {3366my($commit_id,$maxcount,$skip,$filename,@args) =@_;3367my@cos;33683369$maxcount||=1;3370$skip||=0;33713372local$/="\0";33733374open my$fd,"-|", git_cmd(),"rev-list",3375"--header",3376@args,3377("--max-count=".$maxcount),3378("--skip=".$skip),3379@extra_options,3380$commit_id,3381"--",3382($filename? ($filename) : ())3383or die_error(500,"Open git-rev-list failed");3384while(my$line= <$fd>) {3385my%co= parse_commit_text($line);3386push@cos, \%co;3387}3388close$fd;33893390returnwantarray?@cos: \@cos;3391}33923393# parse line of git-diff-tree "raw" output3394sub parse_difftree_raw_line {3395my$line=shift;3396my%res;33973398# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'3399# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'3400if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {3401$res{'from_mode'} =$1;3402$res{'to_mode'} =$2;3403$res{'from_id'} =$3;3404$res{'to_id'} =$4;3405$res{'status'} =$5;3406$res{'similarity'} =$6;3407if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3408($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3409}else{3410$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3411}3412}3413# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3414# combined diff (for merge commit)3415elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3416$res{'nparents'} =length($1);3417$res{'from_mode'} = [split(' ',$2) ];3418$res{'to_mode'} =pop@{$res{'from_mode'}};3419$res{'from_id'} = [split(' ',$3) ];3420$res{'to_id'} =pop@{$res{'from_id'}};3421$res{'status'} = [split('',$4) ];3422$res{'to_file'} = unquote($5);3423}3424# 'c512b523472485aef4fff9e57b229d9d243c967f'3425elsif($line=~m/^([0-9a-fA-F]{40})$/) {3426$res{'commit'} =$1;3427}34283429returnwantarray?%res: \%res;3430}34313432# wrapper: return parsed line of git-diff-tree "raw" output3433# (the argument might be raw line, or parsed info)3434sub parsed_difftree_line {3435my$line_or_ref=shift;34363437if(ref($line_or_ref)eq"HASH") {3438# pre-parsed (or generated by hand)3439return$line_or_ref;3440}else{3441return parse_difftree_raw_line($line_or_ref);3442}3443}34443445# parse line of git-ls-tree output3446sub parse_ls_tree_line {3447my$line=shift;3448my%opts=@_;3449my%res;34503451if($opts{'-l'}) {3452#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3453$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;34543455$res{'mode'} =$1;3456$res{'type'} =$2;3457$res{'hash'} =$3;3458$res{'size'} =$4;3459if($opts{'-z'}) {3460$res{'name'} =$5;3461}else{3462$res{'name'} = unquote($5);3463}3464}else{3465#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3466$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;34673468$res{'mode'} =$1;3469$res{'type'} =$2;3470$res{'hash'} =$3;3471if($opts{'-z'}) {3472$res{'name'} =$4;3473}else{3474$res{'name'} = unquote($4);3475}3476}34773478returnwantarray?%res: \%res;3479}34803481# generates _two_ hashes, references to which are passed as 2 and 3 argument3482sub parse_from_to_diffinfo {3483my($diffinfo,$from,$to,@parents) =@_;34843485if($diffinfo->{'nparents'}) {3486# combined diff3487$from->{'file'} = [];3488$from->{'href'} = [];3489 fill_from_file_info($diffinfo,@parents)3490unlessexists$diffinfo->{'from_file'};3491for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3492$from->{'file'}[$i] =3493defined$diffinfo->{'from_file'}[$i] ?3494$diffinfo->{'from_file'}[$i] :3495$diffinfo->{'to_file'};3496if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3497$from->{'href'}[$i] = href(action=>"blob",3498 hash_base=>$parents[$i],3499 hash=>$diffinfo->{'from_id'}[$i],3500 file_name=>$from->{'file'}[$i]);3501}else{3502$from->{'href'}[$i] =undef;3503}3504}3505}else{3506# ordinary (not combined) diff3507$from->{'file'} =$diffinfo->{'from_file'};3508if($diffinfo->{'status'}ne"A") {# not new (added) file3509$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3510 hash=>$diffinfo->{'from_id'},3511 file_name=>$from->{'file'});3512}else{3513delete$from->{'href'};3514}3515}35163517$to->{'file'} =$diffinfo->{'to_file'};3518if(!is_deleted($diffinfo)) {# file exists in result3519$to->{'href'} = href(action=>"blob", hash_base=>$hash,3520 hash=>$diffinfo->{'to_id'},3521 file_name=>$to->{'file'});3522}else{3523delete$to->{'href'};3524}3525}35263527## ......................................................................3528## parse to array of hashes functions35293530sub git_get_heads_list {3531my($limit,@classes) =@_;3532@classes= ('heads')unless@classes;3533my@patterns=map{"refs/$_"}@classes;3534my@headslist;35353536open my$fd,'-|', git_cmd(),'for-each-ref',3537($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3538'--format=%(objectname) %(refname) %(subject)%00%(committer)',3539@patterns3540orreturn;3541while(my$line= <$fd>) {3542my%ref_item;35433544chomp$line;3545my($refinfo,$committerinfo) =split(/\0/,$line);3546my($hash,$name,$title) =split(' ',$refinfo,3);3547my($committer,$epoch,$tz) =3548($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3549$ref_item{'fullname'} =$name;3550$name=~s!^refs/(?:head|remote)s/!!;35513552$ref_item{'name'} =$name;3553$ref_item{'id'} =$hash;3554$ref_item{'title'} =$title||'(no commit message)';3555$ref_item{'epoch'} =$epoch;3556if($epoch) {3557$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3558}else{3559$ref_item{'age'} ="unknown";3560}35613562push@headslist, \%ref_item;3563}3564close$fd;35653566returnwantarray?@headslist: \@headslist;3567}35683569sub git_get_tags_list {3570my$limit=shift;3571my@tagslist;35723573open my$fd,'-|', git_cmd(),'for-each-ref',3574($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3575'--format=%(objectname) %(objecttype) %(refname) '.3576'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3577'refs/tags'3578orreturn;3579while(my$line= <$fd>) {3580my%ref_item;35813582chomp$line;3583my($refinfo,$creatorinfo) =split(/\0/,$line);3584my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3585my($creator,$epoch,$tz) =3586($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3587$ref_item{'fullname'} =$name;3588$name=~s!^refs/tags/!!;35893590$ref_item{'type'} =$type;3591$ref_item{'id'} =$id;3592$ref_item{'name'} =$name;3593if($typeeq"tag") {3594$ref_item{'subject'} =$title;3595$ref_item{'reftype'} =$reftype;3596$ref_item{'refid'} =$refid;3597}else{3598$ref_item{'reftype'} =$type;3599$ref_item{'refid'} =$id;3600}36013602if($typeeq"tag"||$typeeq"commit") {3603$ref_item{'epoch'} =$epoch;3604if($epoch) {3605$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3606}else{3607$ref_item{'age'} ="unknown";3608}3609}36103611push@tagslist, \%ref_item;3612}3613close$fd;36143615returnwantarray?@tagslist: \@tagslist;3616}36173618## ----------------------------------------------------------------------3619## filesystem-related functions36203621sub get_file_owner {3622my$path=shift;36233624my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3625my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3626if(!defined$gcos) {3627returnundef;3628}3629my$owner=$gcos;3630$owner=~s/[,;].*$//;3631return to_utf8($owner);3632}36333634# assume that file exists3635sub insert_file {3636my$filename=shift;36373638open my$fd,'<',$filename;3639print map{ to_utf8($_) } <$fd>;3640close$fd;3641}36423643## ......................................................................3644## mimetype related functions36453646sub mimetype_guess_file {3647my$filename=shift;3648my$mimemap=shift;3649-r $mimemaporreturnundef;36503651my%mimemap;3652open(my$mh,'<',$mimemap)orreturnundef;3653while(<$mh>) {3654next ifm/^#/;# skip comments3655my($mimetype,@exts) =split(/\s+/);3656foreachmy$ext(@exts) {3657$mimemap{$ext} =$mimetype;3658}3659}3660close($mh);36613662$filename=~/\.([^.]*)$/;3663return$mimemap{$1};3664}36653666sub mimetype_guess {3667my$filename=shift;3668my$mime;3669$filename=~/\./orreturnundef;36703671if($mimetypes_file) {3672my$file=$mimetypes_file;3673if($file!~m!^/!) {# if it is relative path3674# it is relative to project3675$file="$projectroot/$project/$file";3676}3677$mime= mimetype_guess_file($filename,$file);3678}3679$mime||= mimetype_guess_file($filename,'/etc/mime.types');3680return$mime;3681}36823683sub blob_mimetype {3684my$fd=shift;3685my$filename=shift;36863687if($filename) {3688my$mime= mimetype_guess($filename);3689$mimeandreturn$mime;3690}36913692# just in case3693return$default_blob_plain_mimetypeunless$fd;36943695if(-T $fd) {3696return'text/plain';3697}elsif(!$filename) {3698return'application/octet-stream';3699}elsif($filename=~m/\.png$/i) {3700return'image/png';3701}elsif($filename=~m/\.gif$/i) {3702return'image/gif';3703}elsif($filename=~m/\.jpe?g$/i) {3704return'image/jpeg';3705}else{3706return'application/octet-stream';3707}3708}37093710sub blob_contenttype {3711my($fd,$file_name,$type) =@_;37123713$type||= blob_mimetype($fd,$file_name);3714if($typeeq'text/plain'&&defined$default_text_plain_charset) {3715$type.="; charset=$default_text_plain_charset";3716}37173718return$type;3719}37203721# guess file syntax for syntax highlighting; return undef if no highlighting3722# the name of syntax can (in the future) depend on syntax highlighter used3723sub guess_file_syntax {3724my($highlight,$mimetype,$file_name) =@_;3725returnundefunless($highlight&&defined$file_name);3726my$basename= basename($file_name,'.in');3727return$highlight_basename{$basename}3728ifexists$highlight_basename{$basename};37293730$basename=~/\.([^.]*)$/;3731my$ext=$1orreturnundef;3732return$highlight_ext{$ext}3733ifexists$highlight_ext{$ext};37343735returnundef;3736}37373738# run highlighter and return FD of its output,3739# or return original FD if no highlighting3740sub run_highlighter {3741my($fd,$highlight,$syntax) =@_;3742return$fdunless($highlight&&defined$syntax);37433744close$fd;3745open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3746 quote_command($highlight_bin).3747" --replace-tabs=8 --fragment --syntax$syntax|"3748or die_error(500,"Couldn't open file or run syntax highlighter");3749return$fd;3750}37513752## ======================================================================3753## functions printing HTML: header, footer, error page37543755sub get_page_title {3756my$title= to_utf8($site_name);37573758unless(defined$project) {3759if(defined$project_filter) {3760$title.=" - projects in '". esc_path($project_filter) ."'";3761}3762return$title;3763}3764$title.=" - ". to_utf8($project);37653766return$titleunless(defined$action);3767$title.="/$action";# $action is US-ASCII (7bit ASCII)37683769return$titleunless(defined$file_name);3770$title.=" - ". esc_path($file_name);3771if($actioneq"tree"&&$file_name!~ m|/$|) {3772$title.="/";3773}37743775return$title;3776}37773778sub get_content_type_html {3779# require explicit support from the UA if we are to send the page as3780# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3781# we have to do this because MSIE sometimes globs '*/*', pretending to3782# support xhtml+xml but choking when it gets what it asked for.3783if(defined$cgi->http('HTTP_ACCEPT') &&3784$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3785$cgi->Accept('application/xhtml+xml') !=0) {3786return'application/xhtml+xml';3787}else{3788return'text/html';3789}3790}37913792sub print_feed_meta {3793if(defined$project) {3794my%href_params= get_feed_info();3795if(!exists$href_params{'-title'}) {3796$href_params{'-title'} ='log';3797}37983799foreachmy$format(qw(RSS Atom)) {3800my$type=lc($format);3801my%link_attr= (3802'-rel'=>'alternate',3803'-title'=> esc_attr("$project-$href_params{'-title'} -$formatfeed"),3804'-type'=>"application/$type+xml"3805);38063807$href_params{'action'} =$type;3808$link_attr{'-href'} = href(%href_params);3809print"<link ".3810"rel=\"$link_attr{'-rel'}\"".3811"title=\"$link_attr{'-title'}\"".3812"href=\"$link_attr{'-href'}\"".3813"type=\"$link_attr{'-type'}\"".3814"/>\n";38153816$href_params{'extra_options'} ='--no-merges';3817$link_attr{'-href'} = href(%href_params);3818$link_attr{'-title'} .=' (no merges)';3819print"<link ".3820"rel=\"$link_attr{'-rel'}\"".3821"title=\"$link_attr{'-title'}\"".3822"href=\"$link_attr{'-href'}\"".3823"type=\"$link_attr{'-type'}\"".3824"/>\n";3825}38263827}else{3828printf('<link rel="alternate" title="%sprojects list" '.3829'href="%s" type="text/plain; charset=utf-8" />'."\n",3830 esc_attr($site_name), href(project=>undef, action=>"project_index"));3831printf('<link rel="alternate" title="%sprojects feeds" '.3832'href="%s" type="text/x-opml" />'."\n",3833 esc_attr($site_name), href(project=>undef, action=>"opml"));3834}3835}38363837sub print_header_links {3838my$status=shift;38393840# print out each stylesheet that exist, providing backwards capability3841# for those people who defined $stylesheet in a config file3842if(defined$stylesheet) {3843print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3844}else{3845foreachmy$stylesheet(@stylesheets) {3846next unless$stylesheet;3847print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3848}3849}3850 print_feed_meta()3851if($statuseq'200 OK');3852if(defined$favicon) {3853printqq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);3854}3855}38563857sub print_nav_breadcrumbs_path {3858my$dirprefix=undef;3859while(my$part=shift) {3860$dirprefix.="/"ifdefined$dirprefix;3861$dirprefix.=$part;3862print$cgi->a({-href => href(project =>undef,3863 project_filter =>$dirprefix,3864 action =>"project_list")},3865 esc_html($part)) ." / ";3866}3867}38683869sub print_nav_breadcrumbs {3870my%opts=@_;38713872print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3873if(defined$project) {3874my@dirname=split'/',$project;3875my$projectbasename=pop@dirname;3876 print_nav_breadcrumbs_path(@dirname);3877print$cgi->a({-href => href(action=>"summary")}, esc_html($projectbasename));3878if(defined$action) {3879my$action_print=$action;3880if(defined$opts{-action_extra}) {3881$action_print=$cgi->a({-href => href(action=>$action)},3882$action);3883}3884print" /$action_print";3885}3886if(defined$opts{-action_extra}) {3887print" /$opts{-action_extra}";3888}3889print"\n";3890}elsif(defined$project_filter) {3891 print_nav_breadcrumbs_path(split'/',$project_filter);3892}3893}38943895sub print_search_form {3896if(!defined$searchtext) {3897$searchtext="";3898}3899my$search_hash;3900if(defined$hash_base) {3901$search_hash=$hash_base;3902}elsif(defined$hash) {3903$search_hash=$hash;3904}else{3905$search_hash="HEAD";3906}3907my$action=$my_uri;3908my$use_pathinfo= gitweb_check_feature('pathinfo');3909if($use_pathinfo) {3910$action.="/".esc_url($project);3911}3912print$cgi->startform(-method=>"get", -action =>$action) .3913"<div class=\"search\">\n".3914(!$use_pathinfo&&3915$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3916$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3917$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3918$cgi->popup_menu(-name =>'st', -default=>'commit',3919-values=> ['commit','grep','author','committer','pickaxe']) .3920$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3921" search:\n",3922$cgi->textfield(-name =>"s", -value =>$searchtext, -override =>1) ."\n".3923"<span title=\"Extended regular expression\">".3924$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3925-checked =>$search_use_regexp) .3926"</span>".3927"</div>".3928$cgi->end_form() ."\n";3929}39303931sub git_header_html {3932my$status=shift||"200 OK";3933my$expires=shift;3934my%opts=@_;39353936my$title= get_page_title();3937my$content_type= get_content_type_html();3938print$cgi->header(-type=>$content_type, -charset =>'utf-8',3939-status=>$status, -expires =>$expires)3940unless($opts{'-no_http_header'});3941my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3942print<<EOF;3943<?xml version="1.0" encoding="utf-8"?>3944<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3945<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3946<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3947<!-- git core binaries version$git_version-->3948<head>3949<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3950<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3951<meta name="robots" content="index, nofollow"/>3952<title>$title</title>3953EOF3954# the stylesheet, favicon etc urls won't work correctly with path_info3955# unless we set the appropriate base URL3956if($ENV{'PATH_INFO'}) {3957print"<base href=\"".esc_url($base_url)."\"/>\n";3958}3959 print_header_links($status);39603961if(defined$site_html_head_string) {3962print to_utf8($site_html_head_string);3963}39643965print"</head>\n".3966"<body>\n";39673968if(defined$site_header&& -f $site_header) {3969 insert_file($site_header);3970}39713972print"<div class=\"page_header\">\n";3973if(defined$logo) {3974print$cgi->a({-href => esc_url($logo_url),3975-title =>$logo_label},3976$cgi->img({-src => esc_url($logo),3977-width =>72, -height =>27,3978-alt =>"git",3979-class=>"logo"}));3980}3981 print_nav_breadcrumbs(%opts);3982print"</div>\n";39833984my$have_search= gitweb_check_feature('search');3985if(defined$project&&$have_search) {3986 print_search_form();3987}3988}39893990sub git_footer_html {3991my$feed_class='rss_logo';39923993print"<div class=\"page_footer\">\n";3994if(defined$project) {3995my$descr= git_get_project_description($project);3996if(defined$descr) {3997print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3998}39994000my%href_params= get_feed_info();4001if(!%href_params) {4002$feed_class.=' generic';4003}4004$href_params{'-title'} ||='log';40054006foreachmy$format(qw(RSS Atom)) {4007$href_params{'action'} =lc($format);4008print$cgi->a({-href => href(%href_params),4009-title =>"$href_params{'-title'}$formatfeed",4010-class=>$feed_class},$format)."\n";4011}40124013}else{4014print$cgi->a({-href => href(project=>undef, action=>"opml",4015 project_filter =>$project_filter),4016-class=>$feed_class},"OPML") ." ";4017print$cgi->a({-href => href(project=>undef, action=>"project_index",4018 project_filter =>$project_filter),4019-class=>$feed_class},"TXT") ."\n";4020}4021print"</div>\n";# class="page_footer"40224023if(defined$t0&& gitweb_check_feature('timed')) {4024print"<div id=\"generating_info\">\n";4025print'This page took '.4026'<span id="generating_time" class="time_span">'.4027 tv_interval($t0, [ gettimeofday() ]).4028' seconds </span>'.4029' and '.4030'<span id="generating_cmd">'.4031$number_of_git_cmds.4032'</span> git commands '.4033" to generate.\n";4034print"</div>\n";# class="page_footer"4035}40364037if(defined$site_footer&& -f $site_footer) {4038 insert_file($site_footer);4039}40404041print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;4042if(defined$action&&4043$actioneq'blame_incremental') {4044print qq!<script type="text/javascript">\n!.4045 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.4046 qq!"!. href() .qq!");\n!.4047 qq!</script>\n!;4048}else{4049my($jstimezone,$tz_cookie,$datetime_class) =4050 gitweb_get_feature('javascript-timezone');40514052print qq!<script type="text/javascript">\n!.4053 qq!window.onload = function () {\n!;4054if(gitweb_check_feature('javascript-actions')) {4055print qq! fixLinks();\n!;4056}4057if($jstimezone&&$tz_cookie&&$datetime_class) {4058print qq! var tz_cookie = { name:'$tz_cookie', expires:14, path:'/'};\n!.# in days4059 qq! onloadTZSetup('$jstimezone', tz_cookie,'$datetime_class');\n!;4060}4061print qq!};\n!.4062 qq!</script>\n!;4063}40644065print"</body>\n".4066"</html>";4067}40684069# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])4070# Example: die_error(404, 'Hash not found')4071# By convention, use the following status codes (as defined in RFC 2616):4072# 400: Invalid or missing CGI parameters, or4073# requested object exists but has wrong type.4074# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on4075# this server or project.4076# 404: Requested object/revision/project doesn't exist.4077# 500: The server isn't configured properly, or4078# an internal error occurred (e.g. failed assertions caused by bugs), or4079# an unknown error occurred (e.g. the git binary died unexpectedly).4080# 503: The server is currently unavailable (because it is overloaded,4081# or down for maintenance). Generally, this is a temporary state.4082sub die_error {4083my$status=shift||500;4084my$error= esc_html(shift) ||"Internal Server Error";4085my$extra=shift;4086my%opts=@_;40874088my%http_responses= (4089400=>'400 Bad Request',4090403=>'403 Forbidden',4091404=>'404 Not Found',4092500=>'500 Internal Server Error',4093503=>'503 Service Unavailable',4094);4095 git_header_html($http_responses{$status},undef,%opts);4096print<<EOF;4097<div class="page_body">4098<br /><br />4099$status-$error4100<br />4101EOF4102if(defined$extra) {4103print"<hr />\n".4104"$extra\n";4105}4106print"</div>\n";41074108 git_footer_html();4109goto DONE_GITWEB4110unless($opts{'-error_handler'});4111}41124113## ----------------------------------------------------------------------4114## functions printing or outputting HTML: navigation41154116sub git_print_page_nav {4117my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;4118$extra=''if!defined$extra;# pager or formats41194120my@navs=qw(summary shortlog log commit commitdiff tree);4121if($suppress) {4122@navs=grep{$_ne$suppress}@navs;4123}41244125my%arg=map{$_=> {action=>$_} }@navs;4126if(defined$head) {4127for(qw(commit commitdiff)) {4128$arg{$_}{'hash'} =$head;4129}4130if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {4131for(qw(shortlog log)) {4132$arg{$_}{'hash'} =$head;4133}4134}4135}41364137$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;4138$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;41394140my@actions= gitweb_get_feature('actions');4141my%repl= (4142'%'=>'%',4143'n'=>$project,# project name4144'f'=>$git_dir,# project path within filesystem4145'h'=>$treehead||'',# current hash ('h' parameter)4146'b'=>$treebase||'',# hash base ('hb' parameter)4147);4148while(@actions) {4149my($label,$link,$pos) =splice(@actions,0,3);4150# insert4151@navs=map{$_eq$pos? ($_,$label) :$_}@navs;4152# munch munch4153$link=~s/%([%nfhb])/$repl{$1}/g;4154$arg{$label}{'_href'} =$link;4155}41564157print"<div class=\"page_nav\">\n".4158(join" | ",4159map{$_eq$current?4160$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")4161}@navs);4162print"<br/>\n$extra<br/>\n".4163"</div>\n";4164}41654166# returns a submenu for the nagivation of the refs views (tags, heads,4167# remotes) with the current view disabled and the remotes view only4168# available if the feature is enabled4169sub format_ref_views {4170my($current) =@_;4171my@ref_views=qw{tags heads};4172push@ref_views,'remotes'if gitweb_check_feature('remote_heads');4173returnjoin" | ",map{4174$_eq$current?$_:4175$cgi->a({-href => href(action=>$_)},$_)4176}@ref_views4177}41784179sub format_paging_nav {4180my($action,$page,$has_next_link) =@_;4181my$paging_nav;418241834184if($page>0) {4185$paging_nav.=4186$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .4187" ⋅ ".4188$cgi->a({-href => href(-replay=>1, page=>$page-1),4189-accesskey =>"p", -title =>"Alt-p"},"prev");4190}else{4191$paging_nav.="first ⋅ prev";4192}41934194if($has_next_link) {4195$paging_nav.=" ⋅ ".4196$cgi->a({-href => href(-replay=>1, page=>$page+1),4197-accesskey =>"n", -title =>"Alt-n"},"next");4198}else{4199$paging_nav.=" ⋅ next";4200}42014202return$paging_nav;4203}42044205## ......................................................................4206## functions printing or outputting HTML: div42074208sub git_print_header_div {4209my($action,$title,$hash,$hash_base) =@_;4210my%args= ();42114212$args{'action'} =$action;4213$args{'hash'} =$hashif$hash;4214$args{'hash_base'} =$hash_baseif$hash_base;42154216print"<div class=\"header\">\n".4217$cgi->a({-href => href(%args), -class=>"title"},4218$title?$title:$action) .4219"\n</div>\n";4220}42214222sub format_repo_url {4223my($name,$url) =@_;4224return"<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";4225}42264227# Group output by placing it in a DIV element and adding a header.4228# Options for start_div() can be provided by passing a hash reference as the4229# first parameter to the function.4230# Options to git_print_header_div() can be provided by passing an array4231# reference. This must follow the options to start_div if they are present.4232# The content can be a scalar, which is output as-is, a scalar reference, which4233# is output after html escaping, an IO handle passed either as *handle or4234# *handle{IO}, or a function reference. In the latter case all following4235# parameters will be taken as argument to the content function call.4236sub git_print_section {4237my($div_args,$header_args,$content);4238my$arg=shift;4239if(ref($arg)eq'HASH') {4240$div_args=$arg;4241$arg=shift;4242}4243if(ref($arg)eq'ARRAY') {4244$header_args=$arg;4245$arg=shift;4246}4247$content=$arg;42484249print$cgi->start_div($div_args);4250 git_print_header_div(@$header_args);42514252if(ref($content)eq'CODE') {4253$content->(@_);4254}elsif(ref($content)eq'SCALAR') {4255print esc_html($$content);4256}elsif(ref($content)eq'GLOB'or ref($content)eq'IO::Handle') {4257print<$content>;4258}elsif(!ref($content) &&defined($content)) {4259print$content;4260}42614262print$cgi->end_div;4263}42644265sub format_timestamp_html {4266my$date=shift;4267my$strtime=$date->{'rfc2822'};42684269my(undef,undef,$datetime_class) =4270 gitweb_get_feature('javascript-timezone');4271if($datetime_class) {4272$strtime= qq!<span class="$datetime_class">$strtime</span>!;4273}42744275my$localtime_format='(%02d:%02d%s)';4276if($date->{'hour_local'} <6) {4277$localtime_format='(<span class="atnight">%02d:%02d</span>%s)';4278}4279$strtime.=' '.4280sprintf($localtime_format,4281$date->{'hour_local'},$date->{'minute_local'},$date->{'tz_local'});42824283return$strtime;4284}42854286# Outputs the author name and date in long form4287sub git_print_authorship {4288my$co=shift;4289my%opts=@_;4290my$tag=$opts{-tag} ||'div';4291my$author=$co->{'author_name'};42924293my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});4294print"<$tagclass=\"author_date\">".4295 format_search_author($author,"author", esc_html($author)) .4296" [".format_timestamp_html(\%ad)."]".4297 git_get_avatar($co->{'author_email'}, -pad_before =>1) .4298"</$tag>\n";4299}43004301# Outputs table rows containing the full author or committer information,4302# in the format expected for 'commit' view (& similar).4303# Parameters are a commit hash reference, followed by the list of people4304# to output information for. If the list is empty it defaults to both4305# author and committer.4306sub git_print_authorship_rows {4307my$co=shift;4308# too bad we can't use @people = @_ || ('author', 'committer')4309my@people=@_;4310@people= ('author','committer')unless@people;4311foreachmy$who(@people) {4312my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});4313print"<tr><td>$who</td><td>".4314 format_search_author($co->{"${who}_name"},$who,4315 esc_html($co->{"${who}_name"})) ." ".4316 format_search_author($co->{"${who}_email"},$who,4317 esc_html("<".$co->{"${who}_email"} .">")) .4318"</td><td rowspan=\"2\">".4319 git_get_avatar($co->{"${who}_email"}, -size =>'double') .4320"</td></tr>\n".4321"<tr>".4322"<td></td><td>".4323 format_timestamp_html(\%wd) .4324"</td>".4325"</tr>\n";4326}4327}43284329sub git_print_page_path {4330my$name=shift;4331my$type=shift;4332my$hb=shift;433343344335print"<div class=\"page_path\">";4336print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),4337-title =>'tree root'}, to_utf8("[$project]"));4338print" / ";4339if(defined$name) {4340my@dirname=split'/',$name;4341my$basename=pop@dirname;4342my$fullname='';43434344foreachmy$dir(@dirname) {4345$fullname.= ($fullname?'/':'') .$dir;4346print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,4347 hash_base=>$hb),4348-title =>$fullname}, esc_path($dir));4349print" / ";4350}4351if(defined$type&&$typeeq'blob') {4352print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,4353 hash_base=>$hb),4354-title =>$name}, esc_path($basename));4355}elsif(defined$type&&$typeeq'tree') {4356print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,4357 hash_base=>$hb),4358-title =>$name}, esc_path($basename));4359print" / ";4360}else{4361print esc_path($basename);4362}4363}4364print"<br/></div>\n";4365}43664367sub git_print_log {4368my$log=shift;4369my%opts=@_;43704371if($opts{'-remove_title'}) {4372# remove title, i.e. first line of log4373shift@$log;4374}4375# remove leading empty lines4376while(defined$log->[0] &&$log->[0]eq"") {4377shift@$log;4378}43794380# print log4381my$signoff=0;4382my$empty=0;4383foreachmy$line(@$log) {4384if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {4385$signoff=1;4386$empty=0;4387if(!$opts{'-remove_signoff'}) {4388print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";4389next;4390}else{4391# remove signoff lines4392next;4393}4394}else{4395$signoff=0;4396}43974398# print only one empty line4399# do not print empty line after signoff4400if($lineeq"") {4401next if($empty||$signoff);4402$empty=1;4403}else{4404$empty=0;4405}44064407print format_log_line_html($line) ."<br/>\n";4408}44094410if($opts{'-final_empty_line'}) {4411# end with single empty line4412print"<br/>\n"unless$empty;4413}4414}44154416# return link target (what link points to)4417sub git_get_link_target {4418my$hash=shift;4419my$link_target;44204421# read link4422open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4423orreturn;4424{4425local$/=undef;4426$link_target= <$fd>;4427}4428close$fd4429orreturn;44304431return$link_target;4432}44334434# given link target, and the directory (basedir) the link is in,4435# return target of link relative to top directory (top tree);4436# return undef if it is not possible (including absolute links).4437sub normalize_link_target {4438my($link_target,$basedir) =@_;44394440# absolute symlinks (beginning with '/') cannot be normalized4441return if(substr($link_target,0,1)eq'/');44424443# normalize link target to path from top (root) tree (dir)4444my$path;4445if($basedir) {4446$path=$basedir.'/'.$link_target;4447}else{4448# we are in top (root) tree (dir)4449$path=$link_target;4450}44514452# remove //, /./, and /../4453my@path_parts;4454foreachmy$part(split('/',$path)) {4455# discard '.' and ''4456next if(!$part||$parteq'.');4457# handle '..'4458if($parteq'..') {4459if(@path_parts) {4460pop@path_parts;4461}else{4462# link leads outside repository (outside top dir)4463return;4464}4465}else{4466push@path_parts,$part;4467}4468}4469$path=join('/',@path_parts);44704471return$path;4472}44734474# print tree entry (row of git_tree), but without encompassing <tr> element4475sub git_print_tree_entry {4476my($t,$basedir,$hash_base,$have_blame) =@_;44774478my%base_key= ();4479$base_key{'hash_base'} =$hash_baseifdefined$hash_base;44804481# The format of a table row is: mode list link. Where mode is4482# the mode of the entry, list is the name of the entry, an href,4483# and link is the action links of the entry.44844485print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";4486if(exists$t->{'size'}) {4487print"<td class=\"size\">$t->{'size'}</td>\n";4488}4489if($t->{'type'}eq"blob") {4490print"<td class=\"list\">".4491$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4492 file_name=>"$basedir$t->{'name'}",%base_key),4493-class=>"list"}, esc_path($t->{'name'}));4494if(S_ISLNK(oct$t->{'mode'})) {4495my$link_target= git_get_link_target($t->{'hash'});4496if($link_target) {4497my$norm_target= normalize_link_target($link_target,$basedir);4498if(defined$norm_target) {4499print" -> ".4500$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,4501 file_name=>$norm_target),4502-title =>$norm_target}, esc_path($link_target));4503}else{4504print" -> ". esc_path($link_target);4505}4506}4507}4508print"</td>\n";4509print"<td class=\"link\">";4510print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4511 file_name=>"$basedir$t->{'name'}",%base_key)},4512"blob");4513if($have_blame) {4514print" | ".4515$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4516 file_name=>"$basedir$t->{'name'}",%base_key)},4517"blame");4518}4519if(defined$hash_base) {4520print" | ".4521$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4522 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4523"history");4524}4525print" | ".4526$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4527 file_name=>"$basedir$t->{'name'}")},4528"raw");4529print"</td>\n";45304531}elsif($t->{'type'}eq"tree") {4532print"<td class=\"list\">";4533print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4534 file_name=>"$basedir$t->{'name'}",4535%base_key)},4536 esc_path($t->{'name'}));4537print"</td>\n";4538print"<td class=\"link\">";4539print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4540 file_name=>"$basedir$t->{'name'}",4541%base_key)},4542"tree");4543if(defined$hash_base) {4544print" | ".4545$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4546 file_name=>"$basedir$t->{'name'}")},4547"history");4548}4549print"</td>\n";4550}else{4551# unknown object: we can only present history for it4552# (this includes 'commit' object, i.e. submodule support)4553print"<td class=\"list\">".4554 esc_path($t->{'name'}) .4555"</td>\n";4556print"<td class=\"link\">";4557if(defined$hash_base) {4558print$cgi->a({-href => href(action=>"history",4559 hash_base=>$hash_base,4560 file_name=>"$basedir$t->{'name'}")},4561"history");4562}4563print"</td>\n";4564}4565}45664567## ......................................................................4568## functions printing large fragments of HTML45694570# get pre-image filenames for merge (combined) diff4571sub fill_from_file_info {4572my($diff,@parents) =@_;45734574$diff->{'from_file'} = [ ];4575$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4576for(my$i=0;$i<$diff->{'nparents'};$i++) {4577if($diff->{'status'}[$i]eq'R'||4578$diff->{'status'}[$i]eq'C') {4579$diff->{'from_file'}[$i] =4580 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4581}4582}45834584return$diff;4585}45864587# is current raw difftree line of file deletion4588sub is_deleted {4589my$diffinfo=shift;45904591return$diffinfo->{'to_id'}eq('0' x 40);4592}45934594# does patch correspond to [previous] difftree raw line4595# $diffinfo - hashref of parsed raw diff format4596# $patchinfo - hashref of parsed patch diff format4597# (the same keys as in $diffinfo)4598sub is_patch_split {4599my($diffinfo,$patchinfo) =@_;46004601returndefined$diffinfo&&defined$patchinfo4602&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4603}460446054606sub git_difftree_body {4607my($difftree,$hash,@parents) =@_;4608my($parent) =$parents[0];4609my$have_blame= gitweb_check_feature('blame');4610print"<div class=\"list_head\">\n";4611if($#{$difftree} >10) {4612print(($#{$difftree} +1) ." files changed:\n");4613}4614print"</div>\n";46154616print"<table class=\"".4617(@parents>1?"combined ":"") .4618"diff_tree\">\n";46194620# header only for combined diff in 'commitdiff' view4621my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4622if($has_header) {4623# table header4624print"<thead><tr>\n".4625"<th></th><th></th>\n";# filename, patchN link4626for(my$i=0;$i<@parents;$i++) {4627my$par=$parents[$i];4628print"<th>".4629$cgi->a({-href => href(action=>"commitdiff",4630 hash=>$hash, hash_parent=>$par),4631-title =>'commitdiff to parent number '.4632($i+1) .': '.substr($par,0,7)},4633$i+1) .4634" </th>\n";4635}4636print"</tr></thead>\n<tbody>\n";4637}46384639my$alternate=1;4640my$patchno=0;4641foreachmy$line(@{$difftree}) {4642my$diff= parsed_difftree_line($line);46434644if($alternate) {4645print"<tr class=\"dark\">\n";4646}else{4647print"<tr class=\"light\">\n";4648}4649$alternate^=1;46504651if(exists$diff->{'nparents'}) {# combined diff46524653 fill_from_file_info($diff,@parents)4654unlessexists$diff->{'from_file'};46554656if(!is_deleted($diff)) {4657# file exists in the result (child) commit4658print"<td>".4659$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4660 file_name=>$diff->{'to_file'},4661 hash_base=>$hash),4662-class=>"list"}, esc_path($diff->{'to_file'})) .4663"</td>\n";4664}else{4665print"<td>".4666 esc_path($diff->{'to_file'}) .4667"</td>\n";4668}46694670if($actioneq'commitdiff') {4671# link to patch4672$patchno++;4673print"<td class=\"link\">".4674$cgi->a({-href => href(-anchor=>"patch$patchno")},4675"patch") .4676" | ".4677"</td>\n";4678}46794680my$has_history=0;4681my$not_deleted=0;4682for(my$i=0;$i<$diff->{'nparents'};$i++) {4683my$hash_parent=$parents[$i];4684my$from_hash=$diff->{'from_id'}[$i];4685my$from_path=$diff->{'from_file'}[$i];4686my$status=$diff->{'status'}[$i];46874688$has_history||= ($statusne'A');4689$not_deleted||= ($statusne'D');46904691if($statuseq'A') {4692print"<td class=\"link\"align=\"right\"> | </td>\n";4693}elsif($statuseq'D') {4694print"<td class=\"link\">".4695$cgi->a({-href => href(action=>"blob",4696 hash_base=>$hash,4697 hash=>$from_hash,4698 file_name=>$from_path)},4699"blob". ($i+1)) .4700" | </td>\n";4701}else{4702if($diff->{'to_id'}eq$from_hash) {4703print"<td class=\"link nochange\">";4704}else{4705print"<td class=\"link\">";4706}4707print$cgi->a({-href => href(action=>"blobdiff",4708 hash=>$diff->{'to_id'},4709 hash_parent=>$from_hash,4710 hash_base=>$hash,4711 hash_parent_base=>$hash_parent,4712 file_name=>$diff->{'to_file'},4713 file_parent=>$from_path)},4714"diff". ($i+1)) .4715" | </td>\n";4716}4717}47184719print"<td class=\"link\">";4720if($not_deleted) {4721print$cgi->a({-href => href(action=>"blob",4722 hash=>$diff->{'to_id'},4723 file_name=>$diff->{'to_file'},4724 hash_base=>$hash)},4725"blob");4726print" | "if($has_history);4727}4728if($has_history) {4729print$cgi->a({-href => href(action=>"history",4730 file_name=>$diff->{'to_file'},4731 hash_base=>$hash)},4732"history");4733}4734print"</td>\n";47354736print"</tr>\n";4737next;# instead of 'else' clause, to avoid extra indent4738}4739# else ordinary diff47404741my($to_mode_oct,$to_mode_str,$to_file_type);4742my($from_mode_oct,$from_mode_str,$from_file_type);4743if($diff->{'to_mode'}ne('0' x 6)) {4744$to_mode_oct=oct$diff->{'to_mode'};4745if(S_ISREG($to_mode_oct)) {# only for regular file4746$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4747}4748$to_file_type= file_type($diff->{'to_mode'});4749}4750if($diff->{'from_mode'}ne('0' x 6)) {4751$from_mode_oct=oct$diff->{'from_mode'};4752if(S_ISREG($from_mode_oct)) {# only for regular file4753$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4754}4755$from_file_type= file_type($diff->{'from_mode'});4756}47574758if($diff->{'status'}eq"A") {# created4759my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4760$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4761$mode_chng.="]</span>";4762print"<td>";4763print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4764 hash_base=>$hash, file_name=>$diff->{'file'}),4765-class=>"list"}, esc_path($diff->{'file'}));4766print"</td>\n";4767print"<td>$mode_chng</td>\n";4768print"<td class=\"link\">";4769if($actioneq'commitdiff') {4770# link to patch4771$patchno++;4772print$cgi->a({-href => href(-anchor=>"patch$patchno")},4773"patch") .4774" | ";4775}4776print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4777 hash_base=>$hash, file_name=>$diff->{'file'})},4778"blob");4779print"</td>\n";47804781}elsif($diff->{'status'}eq"D") {# deleted4782my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4783print"<td>";4784print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4785 hash_base=>$parent, file_name=>$diff->{'file'}),4786-class=>"list"}, esc_path($diff->{'file'}));4787print"</td>\n";4788print"<td>$mode_chng</td>\n";4789print"<td class=\"link\">";4790if($actioneq'commitdiff') {4791# link to patch4792$patchno++;4793print$cgi->a({-href => href(-anchor=>"patch$patchno")},4794"patch") .4795" | ";4796}4797print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4798 hash_base=>$parent, file_name=>$diff->{'file'})},4799"blob") ." | ";4800if($have_blame) {4801print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4802 file_name=>$diff->{'file'})},4803"blame") ." | ";4804}4805print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4806 file_name=>$diff->{'file'})},4807"history");4808print"</td>\n";48094810}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4811my$mode_chnge="";4812if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4813$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4814if($from_file_typene$to_file_type) {4815$mode_chnge.=" from$from_file_typeto$to_file_type";4816}4817if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4818if($from_mode_str&&$to_mode_str) {4819$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4820}elsif($to_mode_str) {4821$mode_chnge.=" mode:$to_mode_str";4822}4823}4824$mode_chnge.="]</span>\n";4825}4826print"<td>";4827print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4828 hash_base=>$hash, file_name=>$diff->{'file'}),4829-class=>"list"}, esc_path($diff->{'file'}));4830print"</td>\n";4831print"<td>$mode_chnge</td>\n";4832print"<td class=\"link\">";4833if($actioneq'commitdiff') {4834# link to patch4835$patchno++;4836print$cgi->a({-href => href(-anchor=>"patch$patchno")},4837"patch") .4838" | ";4839}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4840# "commit" view and modified file (not onlu mode changed)4841print$cgi->a({-href => href(action=>"blobdiff",4842 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4843 hash_base=>$hash, hash_parent_base=>$parent,4844 file_name=>$diff->{'file'})},4845"diff") .4846" | ";4847}4848print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4849 hash_base=>$hash, file_name=>$diff->{'file'})},4850"blob") ." | ";4851if($have_blame) {4852print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4853 file_name=>$diff->{'file'})},4854"blame") ." | ";4855}4856print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4857 file_name=>$diff->{'file'})},4858"history");4859print"</td>\n";48604861}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4862my%status_name= ('R'=>'moved','C'=>'copied');4863my$nstatus=$status_name{$diff->{'status'}};4864my$mode_chng="";4865if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4866# mode also for directories, so we cannot use $to_mode_str4867$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4868}4869print"<td>".4870$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4871 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4872-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4873"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4874$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4875 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4876-class=>"list"}, esc_path($diff->{'from_file'})) .4877" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4878"<td class=\"link\">";4879if($actioneq'commitdiff') {4880# link to patch4881$patchno++;4882print$cgi->a({-href => href(-anchor=>"patch$patchno")},4883"patch") .4884" | ";4885}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4886# "commit" view and modified file (not only pure rename or copy)4887print$cgi->a({-href => href(action=>"blobdiff",4888 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4889 hash_base=>$hash, hash_parent_base=>$parent,4890 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4891"diff") .4892" | ";4893}4894print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4895 hash_base=>$parent, file_name=>$diff->{'to_file'})},4896"blob") ." | ";4897if($have_blame) {4898print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4899 file_name=>$diff->{'to_file'})},4900"blame") ." | ";4901}4902print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4903 file_name=>$diff->{'to_file'})},4904"history");4905print"</td>\n";49064907}# we should not encounter Unmerged (U) or Unknown (X) status4908print"</tr>\n";4909}4910print"</tbody>"if$has_header;4911print"</table>\n";4912}49134914sub print_sidebyside_diff_chunk {4915my@chunk=@_;4916my(@ctx,@rem,@add);49174918return unless@chunk;49194920# incomplete last line might be among removed or added lines,4921# or both, or among context lines: find which4922for(my$i=1;$i<@chunk;$i++) {4923if($chunk[$i][0]eq'incomplete') {4924$chunk[$i][0] =$chunk[$i-1][0];4925}4926}49274928# guardian4929push@chunk, ["",""];49304931foreachmy$line_info(@chunk) {4932my($class,$line) =@$line_info;49334934# print chunk headers4935if($class&&$classeq'chunk_header') {4936print$line;4937next;4938}49394940## print from accumulator when type of class of lines change4941# empty contents block on start rem/add block, or end of chunk4942if(@ctx&& (!$class||$classeq'rem'||$classeq'add')) {4943print join'',4944'<div class="chunk_block ctx">',4945'<div class="old">',4946@ctx,4947'</div>',4948'<div class="new">',4949@ctx,4950'</div>',4951'</div>';4952@ctx= ();4953}4954# empty add/rem block on start context block, or end of chunk4955if((@rem||@add) && (!$class||$classeq'ctx')) {4956if(!@add) {4957# pure removal4958print join'',4959'<div class="chunk_block rem">',4960'<div class="old">',4961@rem,4962'</div>',4963'</div>';4964}elsif(!@rem) {4965# pure addition4966print join'',4967'<div class="chunk_block add">',4968'<div class="new">',4969@add,4970'</div>',4971'</div>';4972}else{4973# assume that it is change4974print join'',4975'<div class="chunk_block chg">',4976'<div class="old">',4977@rem,4978'</div>',4979'<div class="new">',4980@add,4981'</div>',4982'</div>';4983}4984@rem=@add= ();4985}49864987## adding lines to accumulator4988# guardian value4989last unless$line;4990# rem, add or change4991if($classeq'rem') {4992push@rem,$line;4993}elsif($classeq'add') {4994push@add,$line;4995}4996# context line4997if($classeq'ctx') {4998push@ctx,$line;4999}5000}5001}50025003sub git_patchset_body {5004my($fd,$diff_style,$difftree,$hash,@hash_parents) =@_;5005my($hash_parent) =$hash_parents[0];50065007my$is_combined= (@hash_parents>1);5008my$patch_idx=0;5009my$patch_number=0;5010my$patch_line;5011my$diffinfo;5012my$to_name;5013my(%from,%to);5014my@chunk;# for side-by-side diff50155016print"<div class=\"patchset\">\n";50175018# skip to first patch5019while($patch_line= <$fd>) {5020chomp$patch_line;50215022last if($patch_line=~m/^diff /);5023}50245025 PATCH:5026while($patch_line) {50275028# parse "git diff" header line5029if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {5030# $1 is from_name, which we do not use5031$to_name= unquote($2);5032$to_name=~s!^b/!!;5033}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {5034# $1 is 'cc' or 'combined', which we do not use5035$to_name= unquote($2);5036}else{5037$to_name=undef;5038}50395040# check if current patch belong to current raw line5041# and parse raw git-diff line if needed5042if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {5043# this is continuation of a split patch5044print"<div class=\"patch cont\">\n";5045}else{5046# advance raw git-diff output if needed5047$patch_idx++ifdefined$diffinfo;50485049# read and prepare patch information5050$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);50515052# compact combined diff output can have some patches skipped5053# find which patch (using pathname of result) we are at now;5054if($is_combined) {5055while($to_namene$diffinfo->{'to_file'}) {5056print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".5057 format_diff_cc_simplified($diffinfo,@hash_parents) .5058"</div>\n";# class="patch"50595060$patch_idx++;5061$patch_number++;50625063last if$patch_idx>$#$difftree;5064$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);5065}5066}50675068# modifies %from, %to hashes5069 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);50705071# this is first patch for raw difftree line with $patch_idx index5072# we index @$difftree array from 0, but number patches from 15073print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";5074}50755076# git diff header5077#assert($patch_line =~ m/^diff /) if DEBUG;5078#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed5079$patch_number++;5080# print "git diff" header5081print format_git_diff_header_line($patch_line,$diffinfo,5082 \%from, \%to);50835084# print extended diff header5085print"<div class=\"diff extended_header\">\n";5086 EXTENDED_HEADER:5087while($patch_line= <$fd>) {5088chomp$patch_line;50895090last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);50915092print format_extended_diff_header_line($patch_line,$diffinfo,5093 \%from, \%to);5094}5095print"</div>\n";# class="diff extended_header"50965097# from-file/to-file diff header5098if(!$patch_line) {5099print"</div>\n";# class="patch"5100last PATCH;5101}5102next PATCH if($patch_line=~m/^diff /);5103#assert($patch_line =~ m/^---/) if DEBUG;51045105my$last_patch_line=$patch_line;5106$patch_line= <$fd>;5107chomp$patch_line;5108#assert($patch_line =~ m/^\+\+\+/) if DEBUG;51095110print format_diff_from_to_header($last_patch_line,$patch_line,5111$diffinfo, \%from, \%to,5112@hash_parents);51135114# the patch itself5115 LINE:5116while($patch_line= <$fd>) {5117chomp$patch_line;51185119next PATCH if($patch_line=~m/^diff /);51205121my($class,$line) = process_diff_line($patch_line, \%from, \%to);5122my$diff_classes="diff";5123$diff_classes.="$class"if($class);5124$line="<div class=\"$diff_classes\">$line</div>\n";51255126if($diff_styleeq'sidebyside'&& !$is_combined) {5127if($classeq'chunk_header') {5128 print_sidebyside_diff_chunk(@chunk);5129@chunk= ( [$class,$line] );5130}else{5131push@chunk, [$class,$line];5132}5133}else{5134# default 'inline' style and unknown styles5135print$line;5136}5137}51385139}continue{5140if(@chunk) {5141 print_sidebyside_diff_chunk(@chunk);5142@chunk= ();5143}5144print"</div>\n";# class="patch"5145}51465147# for compact combined (--cc) format, with chunk and patch simplification5148# the patchset might be empty, but there might be unprocessed raw lines5149for(++$patch_idxif$patch_number>0;5150$patch_idx<@$difftree;5151++$patch_idx) {5152# read and prepare patch information5153$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);51545155# generate anchor for "patch" links in difftree / whatchanged part5156print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".5157 format_diff_cc_simplified($diffinfo,@hash_parents) .5158"</div>\n";# class="patch"51595160$patch_number++;5161}51625163if($patch_number==0) {5164if(@hash_parents>1) {5165print"<div class=\"diff nodifferences\">Trivial merge</div>\n";5166}else{5167print"<div class=\"diff nodifferences\">No differences found</div>\n";5168}5169}51705171print"</div>\n";# class="patchset"5172}51735174# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51755176sub git_project_search_form {5177my($searchtext,$search_use_regexp);51785179my$limit='';5180if($project_filter) {5181$limit=" in '$project_filter/'";5182}51835184print"<div class=\"projsearch\">\n";5185print$cgi->startform(-method=>'get', -action =>$my_uri) .5186$cgi->hidden(-name =>'a', -value =>'project_list') ."\n";5187print$cgi->hidden(-name =>'pf', -value =>$project_filter)."\n"5188if(defined$project_filter);5189print$cgi->textfield(-name =>'s', -value =>$searchtext,5190-title =>"Search project by name and description$limit",5191-size =>60) ."\n".5192"<span title=\"Extended regular expression\">".5193$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',5194-checked =>$search_use_regexp) .5195"</span>\n".5196$cgi->submit(-name =>'btnS', -value =>'Search') .5197$cgi->end_form() ."\n".5198$cgi->a({-href => href(project =>undef, searchtext =>undef,5199 project_filter =>$project_filter)},5200 esc_html("List all projects$limit")) ."<br />\n";5201print"</div>\n";5202}52035204# entry for given @keys needs filling if at least one of keys in list5205# is not present in %$project_info5206sub project_info_needs_filling {5207my($project_info,@keys) =@_;52085209# return List::MoreUtils::any { !exists $project_info->{$_} } @keys;5210foreachmy$key(@keys) {5211if(!exists$project_info->{$key}) {5212return1;5213}5214}5215return;5216}52175218# fills project list info (age, description, owner, category, forks, etc.)5219# for each project in the list, removing invalid projects from5220# returned list, or fill only specified info.5221#5222# Invalid projects are removed from the returned list if and only if you5223# ask 'age' or 'age_string' to be filled, because they are the only fields5224# that run unconditionally git command that requires repository, and5225# therefore do always check if project repository is invalid.5226#5227# USAGE:5228# * fill_project_list_info(\@project_list, 'descr_long', 'ctags')5229# ensures that 'descr_long' and 'ctags' fields are filled5230# * @project_list = fill_project_list_info(\@project_list)5231# ensures that all fields are filled (and invalid projects removed)5232#5233# NOTE: modifies $projlist, but does not remove entries from it5234sub fill_project_list_info {5235my($projlist,@wanted_keys) =@_;5236my@projects;5237my$filter_set=sub{return@_; };5238if(@wanted_keys) {5239my%wanted_keys=map{$_=>1}@wanted_keys;5240$filter_set=sub{returngrep{$wanted_keys{$_} }@_; };5241}52425243my$show_ctags= gitweb_check_feature('ctags');5244 PROJECT:5245foreachmy$pr(@$projlist) {5246if(project_info_needs_filling($pr,$filter_set->('age','age_string'))) {5247my(@activity) = git_get_last_activity($pr->{'path'});5248unless(@activity) {5249next PROJECT;5250}5251($pr->{'age'},$pr->{'age_string'}) =@activity;5252}5253if(project_info_needs_filling($pr,$filter_set->('descr','descr_long'))) {5254my$descr= git_get_project_description($pr->{'path'}) ||"";5255$descr= to_utf8($descr);5256$pr->{'descr_long'} =$descr;5257$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);5258}5259if(project_info_needs_filling($pr,$filter_set->('owner'))) {5260$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";5261}5262if($show_ctags&&5263 project_info_needs_filling($pr,$filter_set->('ctags'))) {5264$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});5265}5266if($projects_list_group_categories&&5267 project_info_needs_filling($pr,$filter_set->('category'))) {5268my$cat= git_get_project_category($pr->{'path'}) ||5269$project_list_default_category;5270$pr->{'category'} = to_utf8($cat);5271}52725273push@projects,$pr;5274}52755276return@projects;5277}52785279sub sort_projects_list {5280my($projlist,$order) =@_;5281my@projects;52825283my%order_info= (5284 project => { key =>'path', type =>'str'},5285 descr => { key =>'descr_long', type =>'str'},5286 owner => { key =>'owner', type =>'str'},5287 age => { key =>'age', type =>'num'}5288);5289my$oi=$order_info{$order};5290return@$projlistunlessdefined$oi;5291if($oi->{'type'}eq'str') {5292@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@$projlist;5293}else{5294@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@$projlist;5295}52965297return@projects;5298}52995300# returns a hash of categories, containing the list of project5301# belonging to each category5302sub build_projlist_by_category {5303my($projlist,$from,$to) =@_;5304my%categories;53055306$from=0unlessdefined$from;5307$to=$#$projlistif(!defined$to||$#$projlist<$to);53085309for(my$i=$from;$i<=$to;$i++) {5310my$pr=$projlist->[$i];5311push@{$categories{$pr->{'category'} }},$pr;5312}53135314returnwantarray?%categories: \%categories;5315}53165317# print 'sort by' <th> element, generating 'sort by $name' replay link5318# if that order is not selected5319sub print_sort_th {5320print format_sort_th(@_);5321}53225323sub format_sort_th {5324my($name,$order,$header) =@_;5325my$sort_th="";5326$header||=ucfirst($name);53275328if($ordereq$name) {5329$sort_th.="<th>$header</th>\n";5330}else{5331$sort_th.="<th>".5332$cgi->a({-href => href(-replay=>1, order=>$name),5333-class=>"header"},$header) .5334"</th>\n";5335}53365337return$sort_th;5338}53395340sub git_project_list_rows {5341my($projlist,$from,$to,$check_forks) =@_;53425343$from=0unlessdefined$from;5344$to=$#$projlistif(!defined$to||$#$projlist<$to);53455346my$alternate=1;5347for(my$i=$from;$i<=$to;$i++) {5348my$pr=$projlist->[$i];53495350if($alternate) {5351print"<tr class=\"dark\">\n";5352}else{5353print"<tr class=\"light\">\n";5354}5355$alternate^=1;53565357if($check_forks) {5358print"<td>";5359if($pr->{'forks'}) {5360my$nforks=scalar@{$pr->{'forks'}};5361if($nforks>0) {5362print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),5363-title =>"$nforksforks"},"+");5364}else{5365print$cgi->span({-title =>"$nforksforks"},"+");5366}5367}5368print"</td>\n";5369}5370print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5371-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".5372"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5373-class=>"list", -title =>$pr->{'descr_long'}},5374 esc_html($pr->{'descr'})) ."</td>\n".5375"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";5376print"<td class=\"". age_class($pr->{'age'}) ."\">".5377(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".5378"<td class=\"link\">".5379$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".5380$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".5381$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".5382$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .5383($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .5384"</td>\n".5385"</tr>\n";5386}5387}53885389sub git_project_list_body {5390# actually uses global variable $project5391my($projlist,$order,$from,$to,$extra,$no_header) =@_;5392my@projects=@$projlist;53935394my$check_forks= gitweb_check_feature('forks');5395my$show_ctags= gitweb_check_feature('ctags');5396my$tagfilter=$show_ctags?$input_params{'ctag'} :undef;5397$check_forks=undef5398if($tagfilter||$searchtext);53995400# filtering out forks before filling info allows to do less work5401@projects= filter_forks_from_projects_list(\@projects)5402if($check_forks);5403# search_projects_list pre-fills required info5404@projects= search_projects_list(\@projects,5405'searchtext'=>$searchtext,5406'tagfilter'=>$tagfilter)5407if($tagfilter||$searchtext);5408# fill the rest5409@projects= fill_project_list_info(\@projects);54105411$order||=$default_projects_order;5412$from=0unlessdefined$from;5413$to=$#projectsif(!defined$to||$#projects<$to);54145415# short circuit5416if($from>$to) {5417print"<center>\n".5418"<b>No such projects found</b><br />\n".5419"Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".5420"</center>\n<br />\n";5421return;5422}54235424@projects= sort_projects_list(\@projects,$order);54255426if($show_ctags) {5427my$ctags= git_gather_all_ctags(\@projects);5428my$cloud= git_populate_project_tagcloud($ctags);5429print git_show_project_tagcloud($cloud,64);5430}54315432print"<table class=\"project_list\">\n";5433unless($no_header) {5434print"<tr>\n";5435if($check_forks) {5436print"<th></th>\n";5437}5438 print_sort_th('project',$order,'Project');5439 print_sort_th('descr',$order,'Description');5440 print_sort_th('owner',$order,'Owner');5441 print_sort_th('age',$order,'Last Change');5442print"<th></th>\n".# for links5443"</tr>\n";5444}54455446if($projects_list_group_categories) {5447# only display categories with projects in the $from-$to window5448@projects=sort{$a->{'category'}cmp$b->{'category'}}@projects[$from..$to];5449my%categories= build_projlist_by_category(\@projects,$from,$to);5450foreachmy$cat(sort keys%categories) {5451unless($cateq"") {5452print"<tr>\n";5453if($check_forks) {5454print"<td></td>\n";5455}5456print"<td class=\"category\"colspan=\"5\">".esc_html($cat)."</td>\n";5457print"</tr>\n";5458}54595460 git_project_list_rows($categories{$cat},undef,undef,$check_forks);5461}5462}else{5463 git_project_list_rows(\@projects,$from,$to,$check_forks);5464}54655466if(defined$extra) {5467print"<tr>\n";5468if($check_forks) {5469print"<td></td>\n";5470}5471print"<td colspan=\"5\">$extra</td>\n".5472"</tr>\n";5473}5474print"</table>\n";5475}54765477sub git_log_body {5478# uses global variable $project5479my($commitlist,$from,$to,$refs,$extra) =@_;54805481$from=0unlessdefined$from;5482$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);54835484for(my$i=0;$i<=$to;$i++) {5485my%co= %{$commitlist->[$i]};5486next if!%co;5487my$commit=$co{'id'};5488my$ref= format_ref_marker($refs,$commit);5489 git_print_header_div('commit',5490"<span class=\"age\">$co{'age_string'}</span>".5491 esc_html($co{'title'}) .$ref,5492$commit);5493print"<div class=\"title_text\">\n".5494"<div class=\"log_link\">\n".5495$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5496" | ".5497$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5498" | ".5499$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5500"<br/>\n".5501"</div>\n";5502 git_print_authorship(\%co, -tag =>'span');5503print"<br/>\n</div>\n";55045505print"<div class=\"log_body\">\n";5506 git_print_log($co{'comment'}, -final_empty_line=>1);5507print"</div>\n";5508}5509if($extra) {5510print"<div class=\"page_nav\">\n";5511print"$extra\n";5512print"</div>\n";5513}5514}55155516sub git_shortlog_body {5517# uses global variable $project5518my($commitlist,$from,$to,$refs,$extra) =@_;55195520$from=0unlessdefined$from;5521$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);55225523print"<table class=\"shortlog\">\n";5524my$alternate=1;5525for(my$i=$from;$i<=$to;$i++) {5526my%co= %{$commitlist->[$i]};5527my$commit=$co{'id'};5528my$ref= format_ref_marker($refs,$commit);5529if($alternate) {5530print"<tr class=\"dark\">\n";5531}else{5532print"<tr class=\"light\">\n";5533}5534$alternate^=1;5535# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .5536print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5537 format_author_html('td', \%co,10) ."<td>";5538print format_subject_html($co{'title'},$co{'title_short'},5539 href(action=>"commit", hash=>$commit),$ref);5540print"</td>\n".5541"<td class=\"link\">".5542$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".5543$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".5544$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");5545my$snapshot_links= format_snapshot_links($commit);5546if(defined$snapshot_links) {5547print" | ".$snapshot_links;5548}5549print"</td>\n".5550"</tr>\n";5551}5552if(defined$extra) {5553print"<tr>\n".5554"<td colspan=\"4\">$extra</td>\n".5555"</tr>\n";5556}5557print"</table>\n";5558}55595560sub git_history_body {5561# Warning: assumes constant type (blob or tree) during history5562my($commitlist,$from,$to,$refs,$extra,5563$file_name,$file_hash,$ftype) =@_;55645565$from=0unlessdefined$from;5566$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});55675568print"<table class=\"history\">\n";5569my$alternate=1;5570for(my$i=$from;$i<=$to;$i++) {5571my%co= %{$commitlist->[$i]};5572if(!%co) {5573next;5574}5575my$commit=$co{'id'};55765577my$ref= format_ref_marker($refs,$commit);55785579if($alternate) {5580print"<tr class=\"dark\">\n";5581}else{5582print"<tr class=\"light\">\n";5583}5584$alternate^=1;5585print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5586# shortlog: format_author_html('td', \%co, 10)5587 format_author_html('td', \%co,15,3) ."<td>";5588# originally git_history used chop_str($co{'title'}, 50)5589print format_subject_html($co{'title'},$co{'title_short'},5590 href(action=>"commit", hash=>$commit),$ref);5591print"</td>\n".5592"<td class=\"link\">".5593$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".5594$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");55955596if($ftypeeq'blob') {5597my$blob_current=$file_hash;5598my$blob_parent= git_get_hash_by_path($commit,$file_name);5599if(defined$blob_current&&defined$blob_parent&&5600$blob_currentne$blob_parent) {5601print" | ".5602$cgi->a({-href => href(action=>"blobdiff",5603 hash=>$blob_current, hash_parent=>$blob_parent,5604 hash_base=>$hash_base, hash_parent_base=>$commit,5605 file_name=>$file_name)},5606"diff to current");5607}5608}5609print"</td>\n".5610"</tr>\n";5611}5612if(defined$extra) {5613print"<tr>\n".5614"<td colspan=\"4\">$extra</td>\n".5615"</tr>\n";5616}5617print"</table>\n";5618}56195620sub git_tags_body {5621# uses global variable $project5622my($taglist,$from,$to,$extra) =@_;5623$from=0unlessdefined$from;5624$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);56255626print"<table class=\"tags\">\n";5627my$alternate=1;5628for(my$i=$from;$i<=$to;$i++) {5629my$entry=$taglist->[$i];5630my%tag=%$entry;5631my$comment=$tag{'subject'};5632my$comment_short;5633if(defined$comment) {5634$comment_short= chop_str($comment,30,5);5635}5636if($alternate) {5637print"<tr class=\"dark\">\n";5638}else{5639print"<tr class=\"light\">\n";5640}5641$alternate^=1;5642if(defined$tag{'age'}) {5643print"<td><i>$tag{'age'}</i></td>\n";5644}else{5645print"<td></td>\n";5646}5647print"<td>".5648$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),5649-class=>"list name"}, esc_html($tag{'name'})) .5650"</td>\n".5651"<td>";5652if(defined$comment) {5653print format_subject_html($comment,$comment_short,5654 href(action=>"tag", hash=>$tag{'id'}));5655}5656print"</td>\n".5657"<td class=\"selflink\">";5658if($tag{'type'}eq"tag") {5659print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");5660}else{5661print" ";5662}5663print"</td>\n".5664"<td class=\"link\">"." | ".5665$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});5666if($tag{'reftype'}eq"commit") {5667print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .5668" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");5669}elsif($tag{'reftype'}eq"blob") {5670print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");5671}5672print"</td>\n".5673"</tr>";5674}5675if(defined$extra) {5676print"<tr>\n".5677"<td colspan=\"5\">$extra</td>\n".5678"</tr>\n";5679}5680print"</table>\n";5681}56825683sub git_heads_body {5684# uses global variable $project5685my($headlist,$head_at,$from,$to,$extra) =@_;5686$from=0unlessdefined$from;5687$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);56885689print"<table class=\"heads\">\n";5690my$alternate=1;5691for(my$i=$from;$i<=$to;$i++) {5692my$entry=$headlist->[$i];5693my%ref=%$entry;5694my$curr=defined$head_at&&$ref{'id'}eq$head_at;5695if($alternate) {5696print"<tr class=\"dark\">\n";5697}else{5698print"<tr class=\"light\">\n";5699}5700$alternate^=1;5701print"<td><i>$ref{'age'}</i></td>\n".5702($curr?"<td class=\"current_head\">":"<td>") .5703$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),5704-class=>"list name"},esc_html($ref{'name'})) .5705"</td>\n".5706"<td class=\"link\">".5707$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".5708$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".5709$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})},"tree") .5710"</td>\n".5711"</tr>";5712}5713if(defined$extra) {5714print"<tr>\n".5715"<td colspan=\"3\">$extra</td>\n".5716"</tr>\n";5717}5718print"</table>\n";5719}57205721# Display a single remote block5722sub git_remote_block {5723my($remote,$rdata,$limit,$head) =@_;57245725my$heads=$rdata->{'heads'};5726my$fetch=$rdata->{'fetch'};5727my$push=$rdata->{'push'};57285729my$urls_table="<table class=\"projects_list\">\n";57305731if(defined$fetch) {5732if($fetcheq$push) {5733$urls_table.= format_repo_url("URL",$fetch);5734}else{5735$urls_table.= format_repo_url("Fetch URL",$fetch);5736$urls_table.= format_repo_url("Push URL",$push)ifdefined$push;5737}5738}elsif(defined$push) {5739$urls_table.= format_repo_url("Push URL",$push);5740}else{5741$urls_table.= format_repo_url("","No remote URL");5742}57435744$urls_table.="</table>\n";57455746my$dots;5747if(defined$limit&&$limit<@$heads) {5748$dots=$cgi->a({-href => href(action=>"remotes", hash=>$remote)},"...");5749}57505751print$urls_table;5752 git_heads_body($heads,$head,0,$limit,$dots);5753}57545755# Display a list of remote names with the respective fetch and push URLs5756sub git_remotes_list {5757my($remotedata,$limit) =@_;5758print"<table class=\"heads\">\n";5759my$alternate=1;5760my@remotes=sort keys%$remotedata;57615762my$limited=$limit&&$limit<@remotes;57635764$#remotes=$limit-1if$limited;57655766while(my$remote=shift@remotes) {5767my$rdata=$remotedata->{$remote};5768my$fetch=$rdata->{'fetch'};5769my$push=$rdata->{'push'};5770if($alternate) {5771print"<tr class=\"dark\">\n";5772}else{5773print"<tr class=\"light\">\n";5774}5775$alternate^=1;5776print"<td>".5777$cgi->a({-href=> href(action=>'remotes', hash=>$remote),5778-class=>"list name"},esc_html($remote)) .5779"</td>";5780print"<td class=\"link\">".5781(defined$fetch?$cgi->a({-href=>$fetch},"fetch") :"fetch") .5782" | ".5783(defined$push?$cgi->a({-href=>$push},"push") :"push") .5784"</td>";57855786print"</tr>\n";5787}57885789if($limited) {5790print"<tr>\n".5791"<td colspan=\"3\">".5792$cgi->a({-href => href(action=>"remotes")},"...") .5793"</td>\n"."</tr>\n";5794}57955796print"</table>";5797}57985799# Display remote heads grouped by remote, unless there are too many5800# remotes, in which case we only display the remote names5801sub git_remotes_body {5802my($remotedata,$limit,$head) =@_;5803if($limitand$limit<keys%$remotedata) {5804 git_remotes_list($remotedata,$limit);5805}else{5806 fill_remote_heads($remotedata);5807while(my($remote,$rdata) =each%$remotedata) {5808 git_print_section({-class=>"remote", -id=>$remote},5809["remotes",$remote,$remote],sub{5810 git_remote_block($remote,$rdata,$limit,$head);5811});5812}5813}5814}58155816sub git_search_message {5817my%co=@_;58185819my$greptype;5820if($searchtypeeq'commit') {5821$greptype="--grep=";5822}elsif($searchtypeeq'author') {5823$greptype="--author=";5824}elsif($searchtypeeq'committer') {5825$greptype="--committer=";5826}5827$greptype.=$searchtext;5828my@commitlist= parse_commits($hash,101, (100*$page),undef,5829$greptype,'--regexp-ignore-case',5830$search_use_regexp?'--extended-regexp':'--fixed-strings');58315832my$paging_nav='';5833if($page>0) {5834$paging_nav.=5835$cgi->a({-href => href(-replay=>1, page=>undef)},5836"first") .5837" ⋅ ".5838$cgi->a({-href => href(-replay=>1, page=>$page-1),5839-accesskey =>"p", -title =>"Alt-p"},"prev");5840}else{5841$paging_nav.="first ⋅ prev";5842}5843my$next_link='';5844if($#commitlist>=100) {5845$next_link=5846$cgi->a({-href => href(-replay=>1, page=>$page+1),5847-accesskey =>"n", -title =>"Alt-n"},"next");5848$paging_nav.=" ⋅$next_link";5849}else{5850$paging_nav.=" ⋅ next";5851}58525853 git_header_html();58545855 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5856 git_print_header_div('commit', esc_html($co{'title'}),$hash);5857if($page==0&& !@commitlist) {5858print"<p>No match.</p>\n";5859}else{5860 git_search_grep_body(\@commitlist,0,99,$next_link);5861}58625863 git_footer_html();5864}58655866sub git_search_changes {5867my%co=@_;58685869local$/="\n";5870open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5871'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5872($search_use_regexp?'--pickaxe-regex': ())5873or die_error(500,"Open git-log failed");58745875 git_header_html();58765877 git_print_page_nav('','',$hash,$co{'tree'},$hash);5878 git_print_header_div('commit', esc_html($co{'title'}),$hash);58795880print"<table class=\"pickaxe search\">\n";5881my$alternate=1;5882undef%co;5883my@files;5884while(my$line= <$fd>) {5885chomp$line;5886next unless$line;58875888my%set= parse_difftree_raw_line($line);5889if(defined$set{'commit'}) {5890# finish previous commit5891if(%co) {5892print"</td>\n".5893"<td class=\"link\">".5894$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},5895"commit") .5896" | ".5897$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},5898 hash_base=>$co{'id'})},5899"tree") .5900"</td>\n".5901"</tr>\n";5902}59035904if($alternate) {5905print"<tr class=\"dark\">\n";5906}else{5907print"<tr class=\"light\">\n";5908}5909$alternate^=1;5910%co= parse_commit($set{'commit'});5911my$author= chop_and_escape_str($co{'author_name'},15,5);5912print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5913"<td><i>$author</i></td>\n".5914"<td>".5915$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5916-class=>"list subject"},5917 chop_and_escape_str($co{'title'},50) ."<br/>");5918}elsif(defined$set{'to_id'}) {5919next if($set{'to_id'} =~m/^0{40}$/);59205921print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5922 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5923-class=>"list"},5924"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5925"<br/>\n";5926}5927}5928close$fd;59295930# finish last commit (warning: repetition!)5931if(%co) {5932print"</td>\n".5933"<td class=\"link\">".5934$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},5935"commit") .5936" | ".5937$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},5938 hash_base=>$co{'id'})},5939"tree") .5940"</td>\n".5941"</tr>\n";5942}59435944print"</table>\n";59455946 git_footer_html();5947}59485949sub git_search_files {5950my%co=@_;59515952local$/="\n";5953open my$fd,"-|", git_cmd(),'grep','-n','-z',5954$search_use_regexp? ('-E','-i') :'-F',5955$searchtext,$co{'tree'}5956or die_error(500,"Open git-grep failed");59575958 git_header_html();59595960 git_print_page_nav('','',$hash,$co{'tree'},$hash);5961 git_print_header_div('commit', esc_html($co{'title'}),$hash);59625963print"<table class=\"grep_search\">\n";5964my$alternate=1;5965my$matches=0;5966my$lastfile='';5967my$file_href;5968while(my$line= <$fd>) {5969chomp$line;5970my($file,$lno,$ltext,$binary);5971last if($matches++>1000);5972if($line=~/^Binary file (.+) matches$/) {5973$file=$1;5974$binary=1;5975}else{5976($file,$lno,$ltext) =split(/\0/,$line,3);5977$file=~s/^$co{'tree'}://;5978}5979if($filene$lastfile) {5980$lastfileand print"</td></tr>\n";5981if($alternate++) {5982print"<tr class=\"dark\">\n";5983}else{5984print"<tr class=\"light\">\n";5985}5986$file_href= href(action=>"blob", hash_base=>$co{'id'},5987 file_name=>$file);5988print"<td class=\"list\">".5989$cgi->a({-href =>$file_href, -class=>"list"}, esc_path($file));5990print"</td><td>\n";5991$lastfile=$file;5992}5993if($binary) {5994print"<div class=\"binary\">Binary file</div>\n";5995}else{5996$ltext= untabify($ltext);5997if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5998$ltext= esc_html($1, -nbsp=>1);5999$ltext.='<span class="match">';6000$ltext.= esc_html($2, -nbsp=>1);6001$ltext.='</span>';6002$ltext.= esc_html($3, -nbsp=>1);6003}else{6004$ltext= esc_html($ltext, -nbsp=>1);6005}6006print"<div class=\"pre\">".6007$cgi->a({-href =>$file_href.'#l'.$lno,6008-class=>"linenr"},sprintf('%4i',$lno)) .6009' '.$ltext."</div>\n";6010}6011}6012if($lastfile) {6013print"</td></tr>\n";6014if($matches>1000) {6015print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6016}6017}else{6018print"<div class=\"diff nodifferences\">No matches found</div>\n";6019}6020close$fd;60216022print"</table>\n";60236024 git_footer_html();6025}60266027sub git_search_grep_body {6028my($commitlist,$from,$to,$extra) =@_;6029$from=0unlessdefined$from;6030$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);60316032print"<table class=\"commit_search\">\n";6033my$alternate=1;6034for(my$i=$from;$i<=$to;$i++) {6035my%co= %{$commitlist->[$i]};6036if(!%co) {6037next;6038}6039my$commit=$co{'id'};6040if($alternate) {6041print"<tr class=\"dark\">\n";6042}else{6043print"<tr class=\"light\">\n";6044}6045$alternate^=1;6046print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6047 format_author_html('td', \%co,15,5) .6048"<td>".6049$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6050-class=>"list subject"},6051 chop_and_escape_str($co{'title'},50) ."<br/>");6052my$comment=$co{'comment'};6053foreachmy$line(@$comment) {6054if($line=~m/^(.*?)($search_regexp)(.*)$/i) {6055my($lead,$match,$trail) = ($1,$2,$3);6056$match= chop_str($match,70,5,'center');6057my$contextlen=int((80-length($match))/2);6058$contextlen=30if($contextlen>30);6059$lead= chop_str($lead,$contextlen,10,'left');6060$trail= chop_str($trail,$contextlen,10,'right');60616062$lead= esc_html($lead);6063$match= esc_html($match);6064$trail= esc_html($trail);60656066print"$lead<span class=\"match\">$match</span>$trail<br />";6067}6068}6069print"</td>\n".6070"<td class=\"link\">".6071$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6072" | ".6073$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .6074" | ".6075$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6076print"</td>\n".6077"</tr>\n";6078}6079if(defined$extra) {6080print"<tr>\n".6081"<td colspan=\"3\">$extra</td>\n".6082"</tr>\n";6083}6084print"</table>\n";6085}60866087## ======================================================================6088## ======================================================================6089## actions60906091sub git_project_list {6092my$order=$input_params{'order'};6093if(defined$order&&$order!~m/none|project|descr|owner|age/) {6094 die_error(400,"Unknown order parameter");6095}60966097my@list= git_get_projects_list($project_filter,$strict_export);6098if(!@list) {6099 die_error(404,"No projects found");6100}61016102 git_header_html();6103if(defined$home_text&& -f $home_text) {6104print"<div class=\"index_include\">\n";6105 insert_file($home_text);6106print"</div>\n";6107}61086109 git_project_search_form($searchtext,$search_use_regexp);6110 git_project_list_body(\@list,$order);6111 git_footer_html();6112}61136114sub git_forks {6115my$order=$input_params{'order'};6116if(defined$order&&$order!~m/none|project|descr|owner|age/) {6117 die_error(400,"Unknown order parameter");6118}61196120my$filter=$project;6121$filter=~s/\.git$//;6122my@list= git_get_projects_list($filter);6123if(!@list) {6124 die_error(404,"No forks found");6125}61266127 git_header_html();6128 git_print_page_nav('','');6129 git_print_header_div('summary',"$projectforks");6130 git_project_list_body(\@list,$order);6131 git_footer_html();6132}61336134sub git_project_index {6135my@projects= git_get_projects_list($project_filter,$strict_export);6136if(!@projects) {6137 die_error(404,"No projects found");6138}61396140print$cgi->header(6141-type =>'text/plain',6142-charset =>'utf-8',6143-content_disposition =>'inline; filename="index.aux"');61446145foreachmy$pr(@projects) {6146if(!exists$pr->{'owner'}) {6147$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");6148}61496150my($path,$owner) = ($pr->{'path'},$pr->{'owner'});6151# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '6152$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;6153$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;6154$path=~s/ /\+/g;6155$owner=~s/ /\+/g;61566157print"$path$owner\n";6158}6159}61606161sub git_summary {6162my$descr= git_get_project_description($project) ||"none";6163my%co= parse_commit("HEAD");6164my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();6165my$head=$co{'id'};6166my$remote_heads= gitweb_check_feature('remote_heads');61676168my$owner= git_get_project_owner($project);61696170my$refs= git_get_references();6171# These get_*_list functions return one more to allow us to see if6172# there are more ...6173my@taglist= git_get_tags_list(16);6174my@headlist= git_get_heads_list(16);6175my%remotedata=$remote_heads? git_get_remotes_list() : ();6176my@forklist;6177my$check_forks= gitweb_check_feature('forks');61786179if($check_forks) {6180# find forks of a project6181my$filter=$project;6182$filter=~s/\.git$//;6183@forklist= git_get_projects_list($filter);6184# filter out forks of forks6185@forklist= filter_forks_from_projects_list(\@forklist)6186if(@forklist);6187}61886189 git_header_html();6190 git_print_page_nav('summary','',$head);61916192print"<div class=\"title\"> </div>\n";6193print"<table class=\"projects_list\">\n".6194"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".6195"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";6196if(defined$cd{'rfc2822'}) {6197print"<tr id=\"metadata_lchange\"><td>last change</td>".6198"<td>".format_timestamp_html(\%cd)."</td></tr>\n";6199}62006201# use per project git URL list in $projectroot/$project/cloneurl6202# or make project git URL from git base URL and project name6203my$url_tag="URL";6204my@url_list= git_get_project_url_list($project);6205@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;6206foreachmy$git_url(@url_list) {6207next unless$git_url;6208print format_repo_url($url_tag,$git_url);6209$url_tag="";6210}62116212# Tag cloud6213my$show_ctags= gitweb_check_feature('ctags');6214if($show_ctags) {6215my$ctags= git_get_project_ctags($project);6216if(%$ctags) {6217# without ability to add tags, don't show if there are none6218my$cloud= git_populate_project_tagcloud($ctags);6219print"<tr id=\"metadata_ctags\">".6220"<td>content tags</td>".6221"<td>".git_show_project_tagcloud($cloud,48)."</td>".6222"</tr>\n";6223}6224}62256226print"</table>\n";62276228# If XSS prevention is on, we don't include README.html.6229# TODO: Allow a readme in some safe format.6230if(!$prevent_xss&& -s "$projectroot/$project/README.html") {6231print"<div class=\"title\">readme</div>\n".6232"<div class=\"readme\">\n";6233 insert_file("$projectroot/$project/README.html");6234print"\n</div>\n";# class="readme"6235}62366237# we need to request one more than 16 (0..15) to check if6238# those 16 are all6239my@commitlist=$head? parse_commits($head,17) : ();6240if(@commitlist) {6241 git_print_header_div('shortlog');6242 git_shortlog_body(\@commitlist,0,15,$refs,6243$#commitlist<=15?undef:6244$cgi->a({-href => href(action=>"shortlog")},"..."));6245}62466247if(@taglist) {6248 git_print_header_div('tags');6249 git_tags_body(\@taglist,0,15,6250$#taglist<=15?undef:6251$cgi->a({-href => href(action=>"tags")},"..."));6252}62536254if(@headlist) {6255 git_print_header_div('heads');6256 git_heads_body(\@headlist,$head,0,15,6257$#headlist<=15?undef:6258$cgi->a({-href => href(action=>"heads")},"..."));6259}62606261if(%remotedata) {6262 git_print_header_div('remotes');6263 git_remotes_body(\%remotedata,15,$head);6264}62656266if(@forklist) {6267 git_print_header_div('forks');6268 git_project_list_body(\@forklist,'age',0,15,6269$#forklist<=15?undef:6270$cgi->a({-href => href(action=>"forks")},"..."),6271'no_header');6272}62736274 git_footer_html();6275}62766277sub git_tag {6278my%tag= parse_tag($hash);62796280if(!%tag) {6281 die_error(404,"Unknown tag object");6282}62836284my$head= git_get_head_hash($project);6285 git_header_html();6286 git_print_page_nav('','',$head,undef,$head);6287 git_print_header_div('commit', esc_html($tag{'name'}),$hash);6288print"<div class=\"title_text\">\n".6289"<table class=\"object_header\">\n".6290"<tr>\n".6291"<td>object</td>\n".6292"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},6293$tag{'object'}) ."</td>\n".6294"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},6295$tag{'type'}) ."</td>\n".6296"</tr>\n";6297if(defined($tag{'author'})) {6298 git_print_authorship_rows(\%tag,'author');6299}6300print"</table>\n\n".6301"</div>\n";6302print"<div class=\"page_body\">";6303my$comment=$tag{'comment'};6304foreachmy$line(@$comment) {6305chomp$line;6306print esc_html($line, -nbsp=>1) ."<br/>\n";6307}6308print"</div>\n";6309 git_footer_html();6310}63116312sub git_blame_common {6313my$format=shift||'porcelain';6314if($formateq'porcelain'&&$input_params{'javascript'}) {6315$format='incremental';6316$action='blame_incremental';# for page title etc6317}63186319# permissions6320 gitweb_check_feature('blame')6321or die_error(403,"Blame view not allowed");63226323# error checking6324 die_error(400,"No file name given")unless$file_name;6325$hash_base||= git_get_head_hash($project);6326 die_error(404,"Couldn't find base commit")unless$hash_base;6327my%co= parse_commit($hash_base)6328or die_error(404,"Commit not found");6329my$ftype="blob";6330if(!defined$hash) {6331$hash= git_get_hash_by_path($hash_base,$file_name,"blob")6332or die_error(404,"Error looking up file");6333}else{6334$ftype= git_get_type($hash);6335if($ftype!~"blob") {6336 die_error(400,"Object is not a blob");6337}6338}63396340my$fd;6341if($formateq'incremental') {6342# get file contents (as base)6343open$fd,"-|", git_cmd(),'cat-file','blob',$hash6344or die_error(500,"Open git-cat-file failed");6345}elsif($formateq'data') {6346# run git-blame --incremental6347open$fd,"-|", git_cmd(),"blame","--incremental",6348$hash_base,"--",$file_name6349or die_error(500,"Open git-blame --incremental failed");6350}else{6351# run git-blame --porcelain6352open$fd,"-|", git_cmd(),"blame",'-p',6353$hash_base,'--',$file_name6354or die_error(500,"Open git-blame --porcelain failed");6355}63566357# incremental blame data returns early6358if($formateq'data') {6359print$cgi->header(6360-type=>"text/plain", -charset =>"utf-8",6361-status=>"200 OK");6362local$| =1;# output autoflush6363while(my$line= <$fd>) {6364print to_utf8($line);6365}6366close$fd6367or print"ERROR$!\n";63686369print'END';6370if(defined$t0&& gitweb_check_feature('timed')) {6371print' '.6372 tv_interval($t0, [ gettimeofday() ]).6373' '.$number_of_git_cmds;6374}6375print"\n";63766377return;6378}63796380# page header6381 git_header_html();6382my$formats_nav=6383$cgi->a({-href => href(action=>"blob", -replay=>1)},6384"blob") .6385" | ";6386if($formateq'incremental') {6387$formats_nav.=6388$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},6389"blame") ." (non-incremental)";6390}else{6391$formats_nav.=6392$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},6393"blame") ." (incremental)";6394}6395$formats_nav.=6396" | ".6397$cgi->a({-href => href(action=>"history", -replay=>1)},6398"history") .6399" | ".6400$cgi->a({-href => href(action=>$action, file_name=>$file_name)},6401"HEAD");6402 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6403 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6404 git_print_page_path($file_name,$ftype,$hash_base);64056406# page body6407if($formateq'incremental') {6408print"<noscript>\n<div class=\"error\"><center><b>\n".6409"This page requires JavaScript to run.\nUse ".6410$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},6411'this page').6412" instead.\n".6413"</b></center></div>\n</noscript>\n";64146415print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;6416}64176418print qq!<div class="page_body">\n!;6419print qq!<div id="progress_info">.../ ...</div>\n!6420if($formateq'incremental');6421print qq!<table id="blame_table"class="blame" width="100%">\n!.6422#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.6423 qq!<thead>\n!.6424 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.6425 qq!</thead>\n!.6426 qq!<tbody>\n!;64276428my@rev_color=qw(light dark);6429my$num_colors=scalar(@rev_color);6430my$current_color=0;64316432if($formateq'incremental') {6433my$color_class=$rev_color[$current_color];64346435#contents of a file6436my$linenr=0;6437 LINE:6438while(my$line= <$fd>) {6439chomp$line;6440$linenr++;64416442print qq!<tr id="l$linenr"class="$color_class">!.6443 qq!<td class="sha1"><a href=""> </a></td>!.6444 qq!<td class="linenr">!.6445 qq!<a class="linenr" href="">$linenr</a></td>!;6446print qq!<td class="pre">! . esc_html($line) ."</td>\n";6447print qq!</tr>\n!;6448}64496450}else{# porcelain, i.e. ordinary blame6451my%metainfo= ();# saves information about commits64526453# blame data6454 LINE:6455while(my$line= <$fd>) {6456chomp$line;6457# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]6458# no <lines in group> for subsequent lines in group of lines6459my($full_rev,$orig_lineno,$lineno,$group_size) =6460($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);6461if(!exists$metainfo{$full_rev}) {6462$metainfo{$full_rev} = {'nprevious'=>0};6463}6464my$meta=$metainfo{$full_rev};6465my$data;6466while($data= <$fd>) {6467chomp$data;6468last if($data=~s/^\t//);# contents of line6469if($data=~/^(\S+)(?: (.*))?$/) {6470$meta->{$1} =$2unlessexists$meta->{$1};6471}6472if($data=~/^previous /) {6473$meta->{'nprevious'}++;6474}6475}6476my$short_rev=substr($full_rev,0,8);6477my$author=$meta->{'author'};6478my%date=6479 parse_date($meta->{'author-time'},$meta->{'author-tz'});6480my$date=$date{'iso-tz'};6481if($group_size) {6482$current_color= ($current_color+1) %$num_colors;6483}6484my$tr_class=$rev_color[$current_color];6485$tr_class.=' boundary'if(exists$meta->{'boundary'});6486$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);6487$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);6488print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";6489if($group_size) {6490print"<td class=\"sha1\"";6491print" title=\"". esc_html($author) .",$date\"";6492print" rowspan=\"$group_size\""if($group_size>1);6493print">";6494print$cgi->a({-href => href(action=>"commit",6495 hash=>$full_rev,6496 file_name=>$file_name)},6497 esc_html($short_rev));6498if($group_size>=2) {6499my@author_initials= ($author=~/\b([[:upper:]])\B/g);6500if(@author_initials) {6501print"<br />".6502 esc_html(join('',@author_initials));6503# or join('.', ...)6504}6505}6506print"</td>\n";6507}6508# 'previous' <sha1 of parent commit> <filename at commit>6509if(exists$meta->{'previous'} &&6510$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {6511$meta->{'parent'} =$1;6512$meta->{'file_parent'} = unquote($2);6513}6514my$linenr_commit=6515exists($meta->{'parent'}) ?6516$meta->{'parent'} :$full_rev;6517my$linenr_filename=6518exists($meta->{'file_parent'}) ?6519$meta->{'file_parent'} : unquote($meta->{'filename'});6520my$blamed= href(action =>'blame',6521 file_name =>$linenr_filename,6522 hash_base =>$linenr_commit);6523print"<td class=\"linenr\">";6524print$cgi->a({ -href =>"$blamed#l$orig_lineno",6525-class=>"linenr"},6526 esc_html($lineno));6527print"</td>";6528print"<td class=\"pre\">". esc_html($data) ."</td>\n";6529print"</tr>\n";6530}# end while65316532}65336534# footer6535print"</tbody>\n".6536"</table>\n";# class="blame"6537print"</div>\n";# class="blame_body"6538close$fd6539or print"Reading blob failed\n";65406541 git_footer_html();6542}65436544sub git_blame {6545 git_blame_common();6546}65476548sub git_blame_incremental {6549 git_blame_common('incremental');6550}65516552sub git_blame_data {6553 git_blame_common('data');6554}65556556sub git_tags {6557my$head= git_get_head_hash($project);6558 git_header_html();6559 git_print_page_nav('','',$head,undef,$head,format_ref_views('tags'));6560 git_print_header_div('summary',$project);65616562my@tagslist= git_get_tags_list();6563if(@tagslist) {6564 git_tags_body(\@tagslist);6565}6566 git_footer_html();6567}65686569sub git_heads {6570my$head= git_get_head_hash($project);6571 git_header_html();6572 git_print_page_nav('','',$head,undef,$head,format_ref_views('heads'));6573 git_print_header_div('summary',$project);65746575my@headslist= git_get_heads_list();6576if(@headslist) {6577 git_heads_body(\@headslist,$head);6578}6579 git_footer_html();6580}65816582# used both for single remote view and for list of all the remotes6583sub git_remotes {6584 gitweb_check_feature('remote_heads')6585or die_error(403,"Remote heads view is disabled");65866587my$head= git_get_head_hash($project);6588my$remote=$input_params{'hash'};65896590my$remotedata= git_get_remotes_list($remote);6591 die_error(500,"Unable to get remote information")unlessdefined$remotedata;65926593unless(%$remotedata) {6594 die_error(404,defined$remote?6595"Remote$remotenot found":6596"No remotes found");6597}65986599 git_header_html(undef,undef, -action_extra =>$remote);6600 git_print_page_nav('','',$head,undef,$head,6601 format_ref_views($remote?'':'remotes'));66026603 fill_remote_heads($remotedata);6604if(defined$remote) {6605 git_print_header_div('remotes',"$remoteremote for$project");6606 git_remote_block($remote,$remotedata->{$remote},undef,$head);6607}else{6608 git_print_header_div('summary',"$projectremotes");6609 git_remotes_body($remotedata,undef,$head);6610}66116612 git_footer_html();6613}66146615sub git_blob_plain {6616my$type=shift;6617my$expires;66186619if(!defined$hash) {6620if(defined$file_name) {6621my$base=$hash_base|| git_get_head_hash($project);6622$hash= git_get_hash_by_path($base,$file_name,"blob")6623or die_error(404,"Cannot find file");6624}else{6625 die_error(400,"No file name defined");6626}6627}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6628# blobs defined by non-textual hash id's can be cached6629$expires="+1d";6630}66316632open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6633or die_error(500,"Open git-cat-file blob '$hash' failed");66346635# content-type (can include charset)6636$type= blob_contenttype($fd,$file_name,$type);66376638# "save as" filename, even when no $file_name is given6639my$save_as="$hash";6640if(defined$file_name) {6641$save_as=$file_name;6642}elsif($type=~m/^text\//) {6643$save_as.='.txt';6644}66456646# With XSS prevention on, blobs of all types except a few known safe6647# ones are served with "Content-Disposition: attachment" to make sure6648# they don't run in our security domain. For certain image types,6649# blob view writes an <img> tag referring to blob_plain view, and we6650# want to be sure not to break that by serving the image as an6651# attachment (though Firefox 3 doesn't seem to care).6652my$sandbox=$prevent_xss&&6653$type!~m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;66546655# serve text/* as text/plain6656if($prevent_xss&&6657($type=~m!^text/[a-z]+\b(.*)$!||6658($type=~m!^[a-z]+/[a-z]\+xml\b(.*)$!&& -T $fd))) {6659my$rest=$1;6660$rest=defined$rest?$rest:'';6661$type="text/plain$rest";6662}66636664print$cgi->header(6665-type =>$type,6666-expires =>$expires,6667-content_disposition =>6668($sandbox?'attachment':'inline')6669.'; filename="'.$save_as.'"');6670local$/=undef;6671binmode STDOUT,':raw';6672print<$fd>;6673binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi6674close$fd;6675}66766677sub git_blob {6678my$expires;66796680if(!defined$hash) {6681if(defined$file_name) {6682my$base=$hash_base|| git_get_head_hash($project);6683$hash= git_get_hash_by_path($base,$file_name,"blob")6684or die_error(404,"Cannot find file");6685}else{6686 die_error(400,"No file name defined");6687}6688}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6689# blobs defined by non-textual hash id's can be cached6690$expires="+1d";6691}66926693my$have_blame= gitweb_check_feature('blame');6694open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6695or die_error(500,"Couldn't cat$file_name,$hash");6696my$mimetype= blob_mimetype($fd,$file_name);6697# use 'blob_plain' (aka 'raw') view for files that cannot be displayed6698if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {6699close$fd;6700return git_blob_plain($mimetype);6701}6702# we can have blame only for text/* mimetype6703$have_blame&&= ($mimetype=~m!^text/!);67046705my$highlight= gitweb_check_feature('highlight');6706my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);6707$fd= run_highlighter($fd,$highlight,$syntax)6708if$syntax;67096710 git_header_html(undef,$expires);6711my$formats_nav='';6712if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6713if(defined$file_name) {6714if($have_blame) {6715$formats_nav.=6716$cgi->a({-href => href(action=>"blame", -replay=>1)},6717"blame") .6718" | ";6719}6720$formats_nav.=6721$cgi->a({-href => href(action=>"history", -replay=>1)},6722"history") .6723" | ".6724$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},6725"raw") .6726" | ".6727$cgi->a({-href => href(action=>"blob",6728 hash_base=>"HEAD", file_name=>$file_name)},6729"HEAD");6730}else{6731$formats_nav.=6732$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},6733"raw");6734}6735 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6736 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6737}else{6738print"<div class=\"page_nav\">\n".6739"<br/><br/></div>\n".6740"<div class=\"title\">".esc_html($hash)."</div>\n";6741}6742 git_print_page_path($file_name,"blob",$hash_base);6743print"<div class=\"page_body\">\n";6744if($mimetype=~m!^image/!) {6745print qq!<img type="!.esc_attr($mimetype).qq!"!;6746if($file_name) {6747print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;6748}6749print qq! src="! .6750 href(action=>"blob_plain", hash=>$hash,6751 hash_base=>$hash_base, file_name=>$file_name) .6752 qq!"/>\n!;6753}else{6754my$nr;6755while(my$line= <$fd>) {6756chomp$line;6757$nr++;6758$line= untabify($line);6759printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,6760$nr, esc_attr(href(-replay =>1)),$nr,$nr,6761$syntax? sanitize($line) : esc_html($line, -nbsp=>1);6762}6763}6764close$fd6765or print"Reading blob failed.\n";6766print"</div>";6767 git_footer_html();6768}67696770sub git_tree {6771if(!defined$hash_base) {6772$hash_base="HEAD";6773}6774if(!defined$hash) {6775if(defined$file_name) {6776$hash= git_get_hash_by_path($hash_base,$file_name,"tree");6777}else{6778$hash=$hash_base;6779}6780}6781 die_error(404,"No such tree")unlessdefined($hash);67826783my$show_sizes= gitweb_check_feature('show-sizes');6784my$have_blame= gitweb_check_feature('blame');67856786my@entries= ();6787{6788local$/="\0";6789open my$fd,"-|", git_cmd(),"ls-tree",'-z',6790($show_sizes?'-l': ()),@extra_options,$hash6791or die_error(500,"Open git-ls-tree failed");6792@entries=map{chomp;$_} <$fd>;6793close$fd6794or die_error(404,"Reading tree failed");6795}67966797my$refs= git_get_references();6798my$ref= format_ref_marker($refs,$hash_base);6799 git_header_html();6800my$basedir='';6801if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6802my@views_nav= ();6803if(defined$file_name) {6804push@views_nav,6805$cgi->a({-href => href(action=>"history", -replay=>1)},6806"history"),6807$cgi->a({-href => href(action=>"tree",6808 hash_base=>"HEAD", file_name=>$file_name)},6809"HEAD"),6810}6811my$snapshot_links= format_snapshot_links($hash);6812if(defined$snapshot_links) {6813# FIXME: Should be available when we have no hash base as well.6814push@views_nav,$snapshot_links;6815}6816 git_print_page_nav('tree','',$hash_base,undef,undef,6817join(' | ',@views_nav));6818 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);6819}else{6820undef$hash_base;6821print"<div class=\"page_nav\">\n";6822print"<br/><br/></div>\n";6823print"<div class=\"title\">".esc_html($hash)."</div>\n";6824}6825if(defined$file_name) {6826$basedir=$file_name;6827if($basedirne''&&substr($basedir, -1)ne'/') {6828$basedir.='/';6829}6830 git_print_page_path($file_name,'tree',$hash_base);6831}6832print"<div class=\"page_body\">\n";6833print"<table class=\"tree\">\n";6834my$alternate=1;6835# '..' (top directory) link if possible6836if(defined$hash_base&&6837defined$file_name&&$file_name=~m![^/]+$!) {6838if($alternate) {6839print"<tr class=\"dark\">\n";6840}else{6841print"<tr class=\"light\">\n";6842}6843$alternate^=1;68446845my$up=$file_name;6846$up=~s!/?[^/]+$!!;6847undef$upunless$up;6848# based on git_print_tree_entry6849print'<td class="mode">'. mode_str('040000') ."</td>\n";6850print'<td class="size"> </td>'."\n"if$show_sizes;6851print'<td class="list">';6852print$cgi->a({-href => href(action=>"tree",6853 hash_base=>$hash_base,6854 file_name=>$up)},6855"..");6856print"</td>\n";6857print"<td class=\"link\"></td>\n";68586859print"</tr>\n";6860}6861foreachmy$line(@entries) {6862my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);68636864if($alternate) {6865print"<tr class=\"dark\">\n";6866}else{6867print"<tr class=\"light\">\n";6868}6869$alternate^=1;68706871 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);68726873print"</tr>\n";6874}6875print"</table>\n".6876"</div>";6877 git_footer_html();6878}68796880sub snapshot_name {6881my($project,$hash) =@_;68826883# path/to/project.git -> project6884# path/to/project/.git -> project6885my$name= to_utf8($project);6886$name=~ s,([^/])/*\.git$,$1,;6887$name= basename($name);6888# sanitize name6889$name=~s/[[:cntrl:]]/?/g;68906891my$ver=$hash;6892if($hash=~/^[0-9a-fA-F]+$/) {6893# shorten SHA-1 hash6894my$full_hash= git_get_full_hash($project,$hash);6895if($full_hash=~/^$hash/&&length($hash) >7) {6896$ver= git_get_short_hash($project,$hash);6897}6898}elsif($hash=~m!^refs/tags/(.*)$!) {6899# tags don't need shortened SHA-1 hash6900$ver=$1;6901}else{6902# branches and other need shortened SHA-1 hash6903if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {6904$ver=$1;6905}6906$ver.='-'. git_get_short_hash($project,$hash);6907}6908# in case of hierarchical branch names6909$ver=~s!/!.!g;69106911# name = project-version_string6912$name="$name-$ver";69136914returnwantarray? ($name,$name) :$name;6915}69166917sub git_snapshot {6918my$format=$input_params{'snapshot_format'};6919if(!@snapshot_fmts) {6920 die_error(403,"Snapshots not allowed");6921}6922# default to first supported snapshot format6923$format||=$snapshot_fmts[0];6924if($format!~m/^[a-z0-9]+$/) {6925 die_error(400,"Invalid snapshot format parameter");6926}elsif(!exists($known_snapshot_formats{$format})) {6927 die_error(400,"Unknown snapshot format");6928}elsif($known_snapshot_formats{$format}{'disabled'}) {6929 die_error(403,"Snapshot format not allowed");6930}elsif(!grep($_eq$format,@snapshot_fmts)) {6931 die_error(403,"Unsupported snapshot format");6932}69336934my$type= git_get_type("$hash^{}");6935if(!$type) {6936 die_error(404,'Object does not exist');6937}elsif($typeeq'blob') {6938 die_error(400,'Object is not a tree-ish');6939}69406941my($name,$prefix) = snapshot_name($project,$hash);6942my$filename="$name$known_snapshot_formats{$format}{'suffix'}";6943my$cmd= quote_command(6944 git_cmd(),'archive',6945"--format=$known_snapshot_formats{$format}{'format'}",6946"--prefix=$prefix/",$hash);6947if(exists$known_snapshot_formats{$format}{'compressor'}) {6948$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});6949}69506951$filename=~s/(["\\])/\\$1/g;6952print$cgi->header(6953-type =>$known_snapshot_formats{$format}{'type'},6954-content_disposition =>'inline; filename="'.$filename.'"',6955-status =>'200 OK');69566957open my$fd,"-|",$cmd6958or die_error(500,"Execute git-archive failed");6959binmode STDOUT,':raw';6960print<$fd>;6961binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi6962close$fd;6963}69646965sub git_log_generic {6966my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;69676968my$head= git_get_head_hash($project);6969if(!defined$base) {6970$base=$head;6971}6972if(!defined$page) {6973$page=0;6974}6975my$refs= git_get_references();69766977my$commit_hash=$base;6978if(defined$parent) {6979$commit_hash="$parent..$base";6980}6981my@commitlist=6982 parse_commits($commit_hash,101, (100*$page),6983defined$file_name? ($file_name,"--full-history") : ());69846985my$ftype;6986if(!defined$file_hash&&defined$file_name) {6987# some commits could have deleted file in question,6988# and not have it in tree, but one of them has to have it6989for(my$i=0;$i<@commitlist;$i++) {6990$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);6991last ifdefined$file_hash;6992}6993}6994if(defined$file_hash) {6995$ftype= git_get_type($file_hash);6996}6997if(defined$file_name&& !defined$ftype) {6998 die_error(500,"Unknown type of object");6999}7000my%co;7001if(defined$file_name) {7002%co= parse_commit($base)7003or die_error(404,"Unknown commit object");7004}700570067007my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);7008my$next_link='';7009if($#commitlist>=100) {7010$next_link=7011$cgi->a({-href => href(-replay=>1, page=>$page+1),7012-accesskey =>"n", -title =>"Alt-n"},"next");7013}7014my$patch_max= gitweb_get_feature('patches');7015if($patch_max&& !defined$file_name) {7016if($patch_max<0||@commitlist<=$patch_max) {7017$paging_nav.=" ⋅ ".7018$cgi->a({-href => href(action=>"patches", -replay=>1)},7019"patches");7020}7021}70227023 git_header_html();7024 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);7025if(defined$file_name) {7026 git_print_header_div('commit', esc_html($co{'title'}),$base);7027}else{7028 git_print_header_div('summary',$project)7029}7030 git_print_page_path($file_name,$ftype,$hash_base)7031if(defined$file_name);70327033$body_subr->(\@commitlist,0,99,$refs,$next_link,7034$file_name,$file_hash,$ftype);70357036 git_footer_html();7037}70387039sub git_log {7040 git_log_generic('log', \&git_log_body,7041$hash,$hash_parent);7042}70437044sub git_commit {7045$hash||=$hash_base||"HEAD";7046my%co= parse_commit($hash)7047or die_error(404,"Unknown commit object");70487049my$parent=$co{'parent'};7050my$parents=$co{'parents'};# listref70517052# we need to prepare $formats_nav before any parameter munging7053my$formats_nav;7054if(!defined$parent) {7055# --root commitdiff7056$formats_nav.='(initial)';7057}elsif(@$parents==1) {7058# single parent commit7059$formats_nav.=7060'(parent: '.7061$cgi->a({-href => href(action=>"commit",7062 hash=>$parent)},7063 esc_html(substr($parent,0,7))) .7064')';7065}else{7066# merge commit7067$formats_nav.=7068'(merge: '.7069join(' ',map{7070$cgi->a({-href => href(action=>"commit",7071 hash=>$_)},7072 esc_html(substr($_,0,7)));7073}@$parents) .7074')';7075}7076if(gitweb_check_feature('patches') &&@$parents<=1) {7077$formats_nav.=" | ".7078$cgi->a({-href => href(action=>"patch", -replay=>1)},7079"patch");7080}70817082if(!defined$parent) {7083$parent="--root";7084}7085my@difftree;7086open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",7087@diff_opts,7088(@$parents<=1?$parent:'-c'),7089$hash,"--"7090or die_error(500,"Open git-diff-tree failed");7091@difftree=map{chomp;$_} <$fd>;7092close$fdor die_error(404,"Reading git-diff-tree failed");70937094# non-textual hash id's can be cached7095my$expires;7096if($hash=~m/^[0-9a-fA-F]{40}$/) {7097$expires="+1d";7098}7099my$refs= git_get_references();7100my$ref= format_ref_marker($refs,$co{'id'});71017102 git_header_html(undef,$expires);7103 git_print_page_nav('commit','',7104$hash,$co{'tree'},$hash,7105$formats_nav);71067107if(defined$co{'parent'}) {7108 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);7109}else{7110 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);7111}7112print"<div class=\"title_text\">\n".7113"<table class=\"object_header\">\n";7114 git_print_authorship_rows(\%co);7115print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";7116print"<tr>".7117"<td>tree</td>".7118"<td class=\"sha1\">".7119$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),7120class=>"list"},$co{'tree'}) .7121"</td>".7122"<td class=\"link\">".7123$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},7124"tree");7125my$snapshot_links= format_snapshot_links($hash);7126if(defined$snapshot_links) {7127print" | ".$snapshot_links;7128}7129print"</td>".7130"</tr>\n";71317132foreachmy$par(@$parents) {7133print"<tr>".7134"<td>parent</td>".7135"<td class=\"sha1\">".7136$cgi->a({-href => href(action=>"commit", hash=>$par),7137class=>"list"},$par) .7138"</td>".7139"<td class=\"link\">".7140$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .7141" | ".7142$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .7143"</td>".7144"</tr>\n";7145}7146print"</table>".7147"</div>\n";71487149print"<div class=\"page_body\">\n";7150 git_print_log($co{'comment'});7151print"</div>\n";71527153 git_difftree_body(\@difftree,$hash,@$parents);71547155 git_footer_html();7156}71577158sub git_object {7159# object is defined by:7160# - hash or hash_base alone7161# - hash_base and file_name7162my$type;71637164# - hash or hash_base alone7165if($hash|| ($hash_base&& !defined$file_name)) {7166my$object_id=$hash||$hash_base;71677168open my$fd,"-|", quote_command(7169 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'7170or die_error(404,"Object does not exist");7171$type= <$fd>;7172chomp$type;7173close$fd7174or die_error(404,"Object does not exist");71757176# - hash_base and file_name7177}elsif($hash_base&&defined$file_name) {7178$file_name=~ s,/+$,,;71797180system(git_cmd(),"cat-file",'-e',$hash_base) ==07181or die_error(404,"Base object does not exist");71827183# here errors should not hapen7184open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name7185or die_error(500,"Open git-ls-tree failed");7186my$line= <$fd>;7187close$fd;71887189#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'7190unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {7191 die_error(404,"File or directory for given base does not exist");7192}7193$type=$2;7194$hash=$3;7195}else{7196 die_error(400,"Not enough information to find object");7197}71987199print$cgi->redirect(-uri => href(action=>$type, -full=>1,7200 hash=>$hash, hash_base=>$hash_base,7201 file_name=>$file_name),7202-status =>'302 Found');7203}72047205sub git_blobdiff {7206my$format=shift||'html';7207my$diff_style=$input_params{'diff_style'} ||'inline';72087209my$fd;7210my@difftree;7211my%diffinfo;7212my$expires;72137214# preparing $fd and %diffinfo for git_patchset_body7215# new style URI7216if(defined$hash_base&&defined$hash_parent_base) {7217if(defined$file_name) {7218# read raw output7219open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7220$hash_parent_base,$hash_base,7221"--", (defined$file_parent?$file_parent: ()),$file_name7222or die_error(500,"Open git-diff-tree failed");7223@difftree=map{chomp;$_} <$fd>;7224close$fd7225or die_error(404,"Reading git-diff-tree failed");7226@difftree7227or die_error(404,"Blob diff not found");72287229}elsif(defined$hash&&7230$hash=~/[0-9a-fA-F]{40}/) {7231# try to find filename from $hash72327233# read filtered raw output7234open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7235$hash_parent_base,$hash_base,"--"7236or die_error(500,"Open git-diff-tree failed");7237@difftree=7238# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'7239# $hash == to_id7240grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}7241map{chomp;$_} <$fd>;7242close$fd7243or die_error(404,"Reading git-diff-tree failed");7244@difftree7245or die_error(404,"Blob diff not found");72467247}else{7248 die_error(400,"Missing one of the blob diff parameters");7249}72507251if(@difftree>1) {7252 die_error(400,"Ambiguous blob diff specification");7253}72547255%diffinfo= parse_difftree_raw_line($difftree[0]);7256$file_parent||=$diffinfo{'from_file'} ||$file_name;7257$file_name||=$diffinfo{'to_file'};72587259$hash_parent||=$diffinfo{'from_id'};7260$hash||=$diffinfo{'to_id'};72617262# non-textual hash id's can be cached7263if($hash_base=~m/^[0-9a-fA-F]{40}$/&&7264$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {7265$expires='+1d';7266}72677268# open patch output7269open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7270'-p', ($formateq'html'?"--full-index": ()),7271$hash_parent_base,$hash_base,7272"--", (defined$file_parent?$file_parent: ()),$file_name7273or die_error(500,"Open git-diff-tree failed");7274}72757276# old/legacy style URI -- not generated anymore since 1.4.3.7277if(!%diffinfo) {7278 die_error('404 Not Found',"Missing one of the blob diff parameters")7279}72807281# header7282if($formateq'html') {7283my$formats_nav=7284$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},7285"raw");7286$formats_nav.= diff_style_nav($diff_style);7287 git_header_html(undef,$expires);7288if(defined$hash_base&& (my%co= parse_commit($hash_base))) {7289 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);7290 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);7291}else{7292print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";7293print"<div class=\"title\">".esc_html("$hashvs$hash_parent")."</div>\n";7294}7295if(defined$file_name) {7296 git_print_page_path($file_name,"blob",$hash_base);7297}else{7298print"<div class=\"page_path\"></div>\n";7299}73007301}elsif($formateq'plain') {7302print$cgi->header(7303-type =>'text/plain',7304-charset =>'utf-8',7305-expires =>$expires,7306-content_disposition =>'inline; filename="'."$file_name".'.patch"');73077308print"X-Git-Url: ".$cgi->self_url() ."\n\n";73097310}else{7311 die_error(400,"Unknown blobdiff format");7312}73137314# patch7315if($formateq'html') {7316print"<div class=\"page_body\">\n";73177318 git_patchset_body($fd,$diff_style,7319[ \%diffinfo],$hash_base,$hash_parent_base);7320close$fd;73217322print"</div>\n";# class="page_body"7323 git_footer_html();73247325}else{7326while(my$line= <$fd>) {7327$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;7328$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;73297330print$line;73317332last if$line=~m!^\+\+\+!;7333}7334local$/=undef;7335print<$fd>;7336close$fd;7337}7338}73397340sub git_blobdiff_plain {7341 git_blobdiff('plain');7342}73437344# assumes that it is added as later part of already existing navigation,7345# so it returns "| foo | bar" rather than just "foo | bar"7346sub diff_style_nav {7347my($diff_style,$is_combined) =@_;7348$diff_style||='inline';73497350return""if($is_combined);73517352my@styles= (inline =>'inline','sidebyside'=>'side by side');7353my%styles=@styles;7354@styles=7355@styles[map{$_*2}0..$#styles/2];73567357returnjoin'',7358map{" | ".$_}7359map{7360$_eq$diff_style?$styles{$_} :7361$cgi->a({-href => href(-replay=>1, diff_style =>$_)},$styles{$_})7362}@styles;7363}73647365sub git_commitdiff {7366my%params=@_;7367my$format=$params{-format} ||'html';7368my$diff_style=$input_params{'diff_style'} ||'inline';73697370my($patch_max) = gitweb_get_feature('patches');7371if($formateq'patch') {7372 die_error(403,"Patch view not allowed")unless$patch_max;7373}73747375$hash||=$hash_base||"HEAD";7376my%co= parse_commit($hash)7377or die_error(404,"Unknown commit object");73787379# choose format for commitdiff for merge7380if(!defined$hash_parent&& @{$co{'parents'}} >1) {7381$hash_parent='--cc';7382}7383# we need to prepare $formats_nav before almost any parameter munging7384my$formats_nav;7385if($formateq'html') {7386$formats_nav=7387$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},7388"raw");7389if($patch_max&& @{$co{'parents'}} <=1) {7390$formats_nav.=" | ".7391$cgi->a({-href => href(action=>"patch", -replay=>1)},7392"patch");7393}7394$formats_nav.= diff_style_nav($diff_style, @{$co{'parents'}} >1);73957396if(defined$hash_parent&&7397$hash_parentne'-c'&&$hash_parentne'--cc') {7398# commitdiff with two commits given7399my$hash_parent_short=$hash_parent;7400if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {7401$hash_parent_short=substr($hash_parent,0,7);7402}7403$formats_nav.=7404' (from';7405for(my$i=0;$i< @{$co{'parents'}};$i++) {7406if($co{'parents'}[$i]eq$hash_parent) {7407$formats_nav.=' parent '. ($i+1);7408last;7409}7410}7411$formats_nav.=': '.7412$cgi->a({-href => href(-replay=>1,7413 hash=>$hash_parent, hash_base=>undef)},7414 esc_html($hash_parent_short)) .7415')';7416}elsif(!$co{'parent'}) {7417# --root commitdiff7418$formats_nav.=' (initial)';7419}elsif(scalar@{$co{'parents'}} ==1) {7420# single parent commit7421$formats_nav.=7422' (parent: '.7423$cgi->a({-href => href(-replay=>1,7424 hash=>$co{'parent'}, hash_base=>undef)},7425 esc_html(substr($co{'parent'},0,7))) .7426')';7427}else{7428# merge commit7429if($hash_parenteq'--cc') {7430$formats_nav.=' | '.7431$cgi->a({-href => href(-replay=>1,7432 hash=>$hash, hash_parent=>'-c')},7433'combined');7434}else{# $hash_parent eq '-c'7435$formats_nav.=' | '.7436$cgi->a({-href => href(-replay=>1,7437 hash=>$hash, hash_parent=>'--cc')},7438'compact');7439}7440$formats_nav.=7441' (merge: '.7442join(' ',map{7443$cgi->a({-href => href(-replay=>1,7444 hash=>$_, hash_base=>undef)},7445 esc_html(substr($_,0,7)));7446} @{$co{'parents'}} ) .7447')';7448}7449}74507451my$hash_parent_param=$hash_parent;7452if(!defined$hash_parent_param) {7453# --cc for multiple parents, --root for parentless7454$hash_parent_param=7455@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';7456}74577458# read commitdiff7459my$fd;7460my@difftree;7461if($formateq'html') {7462open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7463"--no-commit-id","--patch-with-raw","--full-index",7464$hash_parent_param,$hash,"--"7465or die_error(500,"Open git-diff-tree failed");74667467while(my$line= <$fd>) {7468chomp$line;7469# empty line ends raw part of diff-tree output7470last unless$line;7471push@difftree,scalar parse_difftree_raw_line($line);7472}74737474}elsif($formateq'plain') {7475open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7476'-p',$hash_parent_param,$hash,"--"7477or die_error(500,"Open git-diff-tree failed");7478}elsif($formateq'patch') {7479# For commit ranges, we limit the output to the number of7480# patches specified in the 'patches' feature.7481# For single commits, we limit the output to a single patch,7482# diverging from the git-format-patch default.7483my@commit_spec= ();7484if($hash_parent) {7485if($patch_max>0) {7486push@commit_spec,"-$patch_max";7487}7488push@commit_spec,'-n',"$hash_parent..$hash";7489}else{7490if($params{-single}) {7491push@commit_spec,'-1';7492}else{7493if($patch_max>0) {7494push@commit_spec,"-$patch_max";7495}7496push@commit_spec,"-n";7497}7498push@commit_spec,'--root',$hash;7499}7500open$fd,"-|", git_cmd(),"format-patch",@diff_opts,7501'--encoding=utf8','--stdout',@commit_spec7502or die_error(500,"Open git-format-patch failed");7503}else{7504 die_error(400,"Unknown commitdiff format");7505}75067507# non-textual hash id's can be cached7508my$expires;7509if($hash=~m/^[0-9a-fA-F]{40}$/) {7510$expires="+1d";7511}75127513# write commit message7514if($formateq'html') {7515my$refs= git_get_references();7516my$ref= format_ref_marker($refs,$co{'id'});75177518 git_header_html(undef,$expires);7519 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);7520 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);7521print"<div class=\"title_text\">\n".7522"<table class=\"object_header\">\n";7523 git_print_authorship_rows(\%co);7524print"</table>".7525"</div>\n";7526print"<div class=\"page_body\">\n";7527if(@{$co{'comment'}} >1) {7528print"<div class=\"log\">\n";7529 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);7530print"</div>\n";# class="log"7531}75327533}elsif($formateq'plain') {7534my$refs= git_get_references("tags");7535my$tagname= git_get_rev_name_tags($hash);7536my$filename= basename($project) ."-$hash.patch";75377538print$cgi->header(7539-type =>'text/plain',7540-charset =>'utf-8',7541-expires =>$expires,7542-content_disposition =>'inline; filename="'."$filename".'"');7543my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});7544print"From: ". to_utf8($co{'author'}) ."\n";7545print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";7546print"Subject: ". to_utf8($co{'title'}) ."\n";75477548print"X-Git-Tag:$tagname\n"if$tagname;7549print"X-Git-Url: ".$cgi->self_url() ."\n\n";75507551foreachmy$line(@{$co{'comment'}}) {7552print to_utf8($line) ."\n";7553}7554print"---\n\n";7555}elsif($formateq'patch') {7556my$filename= basename($project) ."-$hash.patch";75577558print$cgi->header(7559-type =>'text/plain',7560-charset =>'utf-8',7561-expires =>$expires,7562-content_disposition =>'inline; filename="'."$filename".'"');7563}75647565# write patch7566if($formateq'html') {7567my$use_parents= !defined$hash_parent||7568$hash_parenteq'-c'||$hash_parenteq'--cc';7569 git_difftree_body(\@difftree,$hash,7570$use_parents? @{$co{'parents'}} :$hash_parent);7571print"<br/>\n";75727573 git_patchset_body($fd,$diff_style,7574 \@difftree,$hash,7575$use_parents? @{$co{'parents'}} :$hash_parent);7576close$fd;7577print"</div>\n";# class="page_body"7578 git_footer_html();75797580}elsif($formateq'plain') {7581local$/=undef;7582print<$fd>;7583close$fd7584or print"Reading git-diff-tree failed\n";7585}elsif($formateq'patch') {7586local$/=undef;7587print<$fd>;7588close$fd7589or print"Reading git-format-patch failed\n";7590}7591}75927593sub git_commitdiff_plain {7594 git_commitdiff(-format =>'plain');7595}75967597# format-patch-style patches7598sub git_patch {7599 git_commitdiff(-format =>'patch', -single =>1);7600}76017602sub git_patches {7603 git_commitdiff(-format =>'patch');7604}76057606sub git_history {7607 git_log_generic('history', \&git_history_body,7608$hash_base,$hash_parent_base,7609$file_name,$hash);7610}76117612sub git_search {7613$searchtype||='commit';76147615# check if appropriate features are enabled7616 gitweb_check_feature('search')7617or die_error(403,"Search is disabled");7618if($searchtypeeq'pickaxe') {7619# pickaxe may take all resources of your box and run for several minutes7620# with every query - so decide by yourself how public you make this feature7621 gitweb_check_feature('pickaxe')7622or die_error(403,"Pickaxe search is disabled");7623}7624if($searchtypeeq'grep') {7625# grep search might be potentially CPU-intensive, too7626 gitweb_check_feature('grep')7627or die_error(403,"Grep search is disabled");7628}76297630if(!defined$searchtext) {7631 die_error(400,"Text field is empty");7632}7633if(!defined$hash) {7634$hash= git_get_head_hash($project);7635}7636my%co= parse_commit($hash);7637if(!%co) {7638 die_error(404,"Unknown commit object");7639}7640if(!defined$page) {7641$page=0;7642}76437644if($searchtypeeq'commit'||7645$searchtypeeq'author'||7646$searchtypeeq'committer') {7647 git_search_message(%co);7648}elsif($searchtypeeq'pickaxe') {7649 git_search_changes(%co);7650}elsif($searchtypeeq'grep') {7651 git_search_files(%co);7652}else{7653 die_error(400,"Unknown search type");7654}7655}76567657sub git_search_help {7658 git_header_html();7659 git_print_page_nav('','',$hash,$hash,$hash);7660print<<EOT;7661<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without7662regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,7663the pattern entered is recognized as the POSIX extended7664<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case7665insensitive).</p>7666<dl>7667<dt><b>commit</b></dt>7668<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>7669EOT7670my$have_grep= gitweb_check_feature('grep');7671if($have_grep) {7672print<<EOT;7673<dt><b>grep</b></dt>7674<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing7675 a different one) are searched for the given pattern. On large trees, this search can take7676a while and put some strain on the server, so please use it with some consideration. Note that7677due to git-grep peculiarity, currently if regexp mode is turned off, the matches are7678case-sensitive.</dd>7679EOT7680}7681print<<EOT;7682<dt><b>author</b></dt>7683<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>7684<dt><b>committer</b></dt>7685<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>7686EOT7687my$have_pickaxe= gitweb_check_feature('pickaxe');7688if($have_pickaxe) {7689print<<EOT;7690<dt><b>pickaxe</b></dt>7691<dd>All commits that caused the string to appear or disappear from any file (changes that7692added, removed or "modified" the string) will be listed. This search can take a while and7693takes a lot of strain on the server, so please use it wisely. Note that since you may be7694interested even in changes just changing the case as well, this search is case sensitive.</dd>7695EOT7696}7697print"</dl>\n";7698 git_footer_html();7699}77007701sub git_shortlog {7702 git_log_generic('shortlog', \&git_shortlog_body,7703$hash,$hash_parent);7704}77057706## ......................................................................7707## feeds (RSS, Atom; OPML)77087709sub git_feed {7710my$format=shift||'atom';7711my$have_blame= gitweb_check_feature('blame');77127713# Atom: http://www.atomenabled.org/developers/syndication/7714# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ7715if($formatne'rss'&&$formatne'atom') {7716 die_error(400,"Unknown web feed format");7717}77187719# log/feed of current (HEAD) branch, log of given branch, history of file/directory7720my$head=$hash||'HEAD';7721my@commitlist= parse_commits($head,150,0,$file_name);77227723my%latest_commit;7724my%latest_date;7725my$content_type="application/$format+xml";7726if(defined$cgi->http('HTTP_ACCEPT') &&7727$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {7728# browser (feed reader) prefers text/xml7729$content_type='text/xml';7730}7731if(defined($commitlist[0])) {7732%latest_commit= %{$commitlist[0]};7733my$latest_epoch=$latest_commit{'committer_epoch'};7734%latest_date= parse_date($latest_epoch,$latest_commit{'comitter_tz'});7735my$if_modified=$cgi->http('IF_MODIFIED_SINCE');7736if(defined$if_modified) {7737my$since;7738if(eval{require HTTP::Date;1; }) {7739$since= HTTP::Date::str2time($if_modified);7740}elsif(eval{require Time::ParseDate;1; }) {7741$since= Time::ParseDate::parsedate($if_modified, GMT =>1);7742}7743if(defined$since&&$latest_epoch<=$since) {7744print$cgi->header(7745-type =>$content_type,7746-charset =>'utf-8',7747-last_modified =>$latest_date{'rfc2822'},7748-status =>'304 Not Modified');7749return;7750}7751}7752print$cgi->header(7753-type =>$content_type,7754-charset =>'utf-8',7755-last_modified =>$latest_date{'rfc2822'});7756}else{7757print$cgi->header(7758-type =>$content_type,7759-charset =>'utf-8');7760}77617762# Optimization: skip generating the body if client asks only7763# for Last-Modified date.7764return if($cgi->request_method()eq'HEAD');77657766# header variables7767my$title="$site_name-$project/$action";7768my$feed_type='log';7769if(defined$hash) {7770$title.=" - '$hash'";7771$feed_type='branch log';7772if(defined$file_name) {7773$title.=" ::$file_name";7774$feed_type='history';7775}7776}elsif(defined$file_name) {7777$title.=" -$file_name";7778$feed_type='history';7779}7780$title.="$feed_type";7781my$descr= git_get_project_description($project);7782if(defined$descr) {7783$descr= esc_html($descr);7784}else{7785$descr="$project".7786($formateq'rss'?'RSS':'Atom') .7787" feed";7788}7789my$owner= git_get_project_owner($project);7790$owner= esc_html($owner);77917792#header7793my$alt_url;7794if(defined$file_name) {7795$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);7796}elsif(defined$hash) {7797$alt_url= href(-full=>1, action=>"log", hash=>$hash);7798}else{7799$alt_url= href(-full=>1, action=>"summary");7800}7801print qq!<?xml version="1.0" encoding="utf-8"?>\n!;7802if($formateq'rss') {7803print<<XML;7804<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">7805<channel>7806XML7807print"<title>$title</title>\n".7808"<link>$alt_url</link>\n".7809"<description>$descr</description>\n".7810"<language>en</language>\n".7811# project owner is responsible for 'editorial' content7812"<managingEditor>$owner</managingEditor>\n";7813if(defined$logo||defined$favicon) {7814# prefer the logo to the favicon, since RSS7815# doesn't allow both7816my$img= esc_url($logo||$favicon);7817print"<image>\n".7818"<url>$img</url>\n".7819"<title>$title</title>\n".7820"<link>$alt_url</link>\n".7821"</image>\n";7822}7823if(%latest_date) {7824print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";7825print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";7826}7827print"<generator>gitweb v.$version/$git_version</generator>\n";7828}elsif($formateq'atom') {7829print<<XML;7830<feed xmlns="http://www.w3.org/2005/Atom">7831XML7832print"<title>$title</title>\n".7833"<subtitle>$descr</subtitle>\n".7834'<link rel="alternate" type="text/html" href="'.7835$alt_url.'" />'."\n".7836'<link rel="self" type="'.$content_type.'" href="'.7837$cgi->self_url() .'" />'."\n".7838"<id>". href(-full=>1) ."</id>\n".7839# use project owner for feed author7840"<author><name>$owner</name></author>\n";7841if(defined$favicon) {7842print"<icon>". esc_url($favicon) ."</icon>\n";7843}7844if(defined$logo) {7845# not twice as wide as tall: 72 x 27 pixels7846print"<logo>". esc_url($logo) ."</logo>\n";7847}7848if(!%latest_date) {7849# dummy date to keep the feed valid until commits trickle in:7850print"<updated>1970-01-01T00:00:00Z</updated>\n";7851}else{7852print"<updated>$latest_date{'iso-8601'}</updated>\n";7853}7854print"<generator version='$version/$git_version'>gitweb</generator>\n";7855}78567857# contents7858for(my$i=0;$i<=$#commitlist;$i++) {7859my%co= %{$commitlist[$i]};7860my$commit=$co{'id'};7861# we read 150, we always show 30 and the ones more recent than 48 hours7862if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {7863last;7864}7865my%cd= parse_date($co{'author_epoch'},$co{'author_tz'});78667867# get list of changed files7868open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7869$co{'parent'} ||"--root",7870$co{'id'},"--", (defined$file_name?$file_name: ())7871ornext;7872my@difftree=map{chomp;$_} <$fd>;7873close$fd7874ornext;78757876# print element (entry, item)7877my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);7878if($formateq'rss') {7879print"<item>\n".7880"<title>". esc_html($co{'title'}) ."</title>\n".7881"<author>". esc_html($co{'author'}) ."</author>\n".7882"<pubDate>$cd{'rfc2822'}</pubDate>\n".7883"<guid isPermaLink=\"true\">$co_url</guid>\n".7884"<link>$co_url</link>\n".7885"<description>". esc_html($co{'title'}) ."</description>\n".7886"<content:encoded>".7887"<![CDATA[\n";7888}elsif($formateq'atom') {7889print"<entry>\n".7890"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".7891"<updated>$cd{'iso-8601'}</updated>\n".7892"<author>\n".7893" <name>". esc_html($co{'author_name'}) ."</name>\n";7894if($co{'author_email'}) {7895print" <email>". esc_html($co{'author_email'}) ."</email>\n";7896}7897print"</author>\n".7898# use committer for contributor7899"<contributor>\n".7900" <name>". esc_html($co{'committer_name'}) ."</name>\n";7901if($co{'committer_email'}) {7902print" <email>". esc_html($co{'committer_email'}) ."</email>\n";7903}7904print"</contributor>\n".7905"<published>$cd{'iso-8601'}</published>\n".7906"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".7907"<id>$co_url</id>\n".7908"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".7909"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";7910}7911my$comment=$co{'comment'};7912print"<pre>\n";7913foreachmy$line(@$comment) {7914$line= esc_html($line);7915print"$line\n";7916}7917print"</pre><ul>\n";7918foreachmy$difftree_line(@difftree) {7919my%difftree= parse_difftree_raw_line($difftree_line);7920next if!$difftree{'from_id'};79217922my$file=$difftree{'file'} ||$difftree{'to_file'};79237924print"<li>".7925"[".7926$cgi->a({-href => href(-full=>1, action=>"blobdiff",7927 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},7928 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},7929 file_name=>$file, file_parent=>$difftree{'from_file'}),7930-title =>"diff"},'D');7931if($have_blame) {7932print$cgi->a({-href => href(-full=>1, action=>"blame",7933 file_name=>$file, hash_base=>$commit),7934-title =>"blame"},'B');7935}7936# if this is not a feed of a file history7937if(!defined$file_name||$file_namene$file) {7938print$cgi->a({-href => href(-full=>1, action=>"history",7939 file_name=>$file, hash=>$commit),7940-title =>"history"},'H');7941}7942$file= esc_path($file);7943print"] ".7944"$file</li>\n";7945}7946if($formateq'rss') {7947print"</ul>]]>\n".7948"</content:encoded>\n".7949"</item>\n";7950}elsif($formateq'atom') {7951print"</ul>\n</div>\n".7952"</content>\n".7953"</entry>\n";7954}7955}79567957# end of feed7958if($formateq'rss') {7959print"</channel>\n</rss>\n";7960}elsif($formateq'atom') {7961print"</feed>\n";7962}7963}79647965sub git_rss {7966 git_feed('rss');7967}79687969sub git_atom {7970 git_feed('atom');7971}79727973sub git_opml {7974my@list= git_get_projects_list($project_filter,$strict_export);7975if(!@list) {7976 die_error(404,"No projects found");7977}79787979print$cgi->header(7980-type =>'text/xml',7981-charset =>'utf-8',7982-content_disposition =>'inline; filename="opml.xml"');79837984my$title= esc_html($site_name);7985my$filter=" within subdirectory ";7986if(defined$project_filter) {7987$filter.= esc_html($project_filter);7988}else{7989$filter="";7990}7991print<<XML;7992<?xml version="1.0" encoding="utf-8"?>7993<opml version="1.0">7994<head>7995 <title>$titleOPML Export$filter</title>7996</head>7997<body>7998<outline text="git RSS feeds">7999XML80008001foreachmy$pr(@list) {8002my%proj=%$pr;8003my$head= git_get_head_hash($proj{'path'});8004if(!defined$head) {8005next;8006}8007$git_dir="$projectroot/$proj{'path'}";8008my%co= parse_commit($head);8009if(!%co) {8010next;8011}80128013my$path= esc_html(chop_str($proj{'path'},25,5));8014my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);8015my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);8016print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";8017}8018print<<XML;8019</outline>8020</body>8021</opml>8022XML8023}