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# this must be last entry (for manipulation from JavaScript) 764 javascript =>"js" 765); 766our%cgi_param_mapping=@cgi_param_mapping; 767 768# we will also need to know the possible actions, for validation 769our%actions= ( 770"blame"=> \&git_blame, 771"blame_incremental"=> \&git_blame_incremental, 772"blame_data"=> \&git_blame_data, 773"blobdiff"=> \&git_blobdiff, 774"blobdiff_plain"=> \&git_blobdiff_plain, 775"blob"=> \&git_blob, 776"blob_plain"=> \&git_blob_plain, 777"commitdiff"=> \&git_commitdiff, 778"commitdiff_plain"=> \&git_commitdiff_plain, 779"commit"=> \&git_commit, 780"forks"=> \&git_forks, 781"heads"=> \&git_heads, 782"history"=> \&git_history, 783"log"=> \&git_log, 784"patch"=> \&git_patch, 785"patches"=> \&git_patches, 786"remotes"=> \&git_remotes, 787"rss"=> \&git_rss, 788"atom"=> \&git_atom, 789"search"=> \&git_search, 790"search_help"=> \&git_search_help, 791"shortlog"=> \&git_shortlog, 792"summary"=> \&git_summary, 793"tag"=> \&git_tag, 794"tags"=> \&git_tags, 795"tree"=> \&git_tree, 796"snapshot"=> \&git_snapshot, 797"object"=> \&git_object, 798# those below don't need $project 799"opml"=> \&git_opml, 800"project_list"=> \&git_project_list, 801"project_index"=> \&git_project_index, 802); 803 804# finally, we have the hash of allowed extra_options for the commands that 805# allow them 806our%allowed_options= ( 807"--no-merges"=> [qw(rss atom log shortlog history)], 808); 809 810# fill %input_params with the CGI parameters. All values except for 'opt' 811# should be single values, but opt can be an array. We should probably 812# build an array of parameters that can be multi-valued, but since for the time 813# being it's only this one, we just single it out 814sub evaluate_query_params { 815our$cgi; 816 817while(my($name,$symbol) =each%cgi_param_mapping) { 818if($symboleq'opt') { 819$input_params{$name} = [map{ decode_utf8($_) }$cgi->param($symbol) ]; 820}else{ 821$input_params{$name} = decode_utf8($cgi->param($symbol)); 822} 823} 824} 825 826# now read PATH_INFO and update the parameter list for missing parameters 827sub evaluate_path_info { 828return ifdefined$input_params{'project'}; 829return if!$path_info; 830$path_info=~ s,^/+,,; 831return if!$path_info; 832 833# find which part of PATH_INFO is project 834my$project=$path_info; 835$project=~ s,/+$,,; 836while($project&& !check_head_link("$projectroot/$project")) { 837$project=~ s,/*[^/]*$,,; 838} 839return unless$project; 840$input_params{'project'} =$project; 841 842# do not change any parameters if an action is given using the query string 843return if$input_params{'action'}; 844$path_info=~ s,^\Q$project\E/*,,; 845 846# next, check if we have an action 847my$action=$path_info; 848$action=~ s,/.*$,,; 849if(exists$actions{$action}) { 850$path_info=~ s,^$action/*,,; 851$input_params{'action'} =$action; 852} 853 854# list of actions that want hash_base instead of hash, but can have no 855# pathname (f) parameter 856my@wants_base= ( 857'tree', 858'history', 859); 860 861# we want to catch, among others 862# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 863my($parentrefname,$parentpathname,$refname,$pathname) = 864($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/); 865 866# first, analyze the 'current' part 867if(defined$pathname) { 868# we got "branch:filename" or "branch:dir/" 869# we could use git_get_type(branch:pathname), but: 870# - it needs $git_dir 871# - it does a git() call 872# - the convention of terminating directories with a slash 873# makes it superfluous 874# - embedding the action in the PATH_INFO would make it even 875# more superfluous 876$pathname=~ s,^/+,,; 877if(!$pathname||substr($pathname, -1)eq"/") { 878$input_params{'action'} ||="tree"; 879$pathname=~ s,/$,,; 880}else{ 881# the default action depends on whether we had parent info 882# or not 883if($parentrefname) { 884$input_params{'action'} ||="blobdiff_plain"; 885}else{ 886$input_params{'action'} ||="blob_plain"; 887} 888} 889$input_params{'hash_base'} ||=$refname; 890$input_params{'file_name'} ||=$pathname; 891}elsif(defined$refname) { 892# we got "branch". In this case we have to choose if we have to 893# set hash or hash_base. 894# 895# Most of the actions without a pathname only want hash to be 896# set, except for the ones specified in @wants_base that want 897# hash_base instead. It should also be noted that hand-crafted 898# links having 'history' as an action and no pathname or hash 899# set will fail, but that happens regardless of PATH_INFO. 900if(defined$parentrefname) { 901# if there is parent let the default be 'shortlog' action 902# (for http://git.example.com/repo.git/A..B links); if there 903# is no parent, dispatch will detect type of object and set 904# action appropriately if required (if action is not set) 905$input_params{'action'} ||="shortlog"; 906} 907if($input_params{'action'} && 908grep{$_eq$input_params{'action'} }@wants_base) { 909$input_params{'hash_base'} ||=$refname; 910}else{ 911$input_params{'hash'} ||=$refname; 912} 913} 914 915# next, handle the 'parent' part, if present 916if(defined$parentrefname) { 917# a missing pathspec defaults to the 'current' filename, allowing e.g. 918# someproject/blobdiff/oldrev..newrev:/filename 919if($parentpathname) { 920$parentpathname=~ s,^/+,,; 921$parentpathname=~ s,/$,,; 922$input_params{'file_parent'} ||=$parentpathname; 923}else{ 924$input_params{'file_parent'} ||=$input_params{'file_name'}; 925} 926# we assume that hash_parent_base is wanted if a path was specified, 927# or if the action wants hash_base instead of hash 928if(defined$input_params{'file_parent'} || 929grep{$_eq$input_params{'action'} }@wants_base) { 930$input_params{'hash_parent_base'} ||=$parentrefname; 931}else{ 932$input_params{'hash_parent'} ||=$parentrefname; 933} 934} 935 936# for the snapshot action, we allow URLs in the form 937# $project/snapshot/$hash.ext 938# where .ext determines the snapshot and gets removed from the 939# passed $refname to provide the $hash. 940# 941# To be able to tell that $refname includes the format extension, we 942# require the following two conditions to be satisfied: 943# - the hash input parameter MUST have been set from the $refname part 944# of the URL (i.e. they must be equal) 945# - the snapshot format MUST NOT have been defined already (e.g. from 946# CGI parameter sf) 947# It's also useless to try any matching unless $refname has a dot, 948# so we check for that too 949if(defined$input_params{'action'} && 950$input_params{'action'}eq'snapshot'&& 951defined$refname&&index($refname,'.') != -1&& 952$refnameeq$input_params{'hash'} && 953!defined$input_params{'snapshot_format'}) { 954# We loop over the known snapshot formats, checking for 955# extensions. Allowed extensions are both the defined suffix 956# (which includes the initial dot already) and the snapshot 957# format key itself, with a prepended dot 958while(my($fmt,$opt) =each%known_snapshot_formats) { 959my$hash=$refname; 960unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 961next; 962} 963my$sfx=$1; 964# a valid suffix was found, so set the snapshot format 965# and reset the hash parameter 966$input_params{'snapshot_format'} =$fmt; 967$input_params{'hash'} =$hash; 968# we also set the format suffix to the one requested 969# in the URL: this way a request for e.g. .tgz returns 970# a .tgz instead of a .tar.gz 971$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 972last; 973} 974} 975} 976 977our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 978$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 979$searchtext,$search_regexp); 980sub evaluate_and_validate_params { 981our$action=$input_params{'action'}; 982if(defined$action) { 983if(!validate_action($action)) { 984 die_error(400,"Invalid action parameter"); 985} 986} 987 988# parameters which are pathnames 989our$project=$input_params{'project'}; 990if(defined$project) { 991if(!validate_project($project)) { 992undef$project; 993 die_error(404,"No such project"); 994} 995} 996 997our$file_name=$input_params{'file_name'}; 998if(defined$file_name) { 999if(!validate_pathname($file_name)) {1000 die_error(400,"Invalid file parameter");1001}1002}10031004our$file_parent=$input_params{'file_parent'};1005if(defined$file_parent) {1006if(!validate_pathname($file_parent)) {1007 die_error(400,"Invalid file parent parameter");1008}1009}10101011# parameters which are refnames1012our$hash=$input_params{'hash'};1013if(defined$hash) {1014if(!validate_refname($hash)) {1015 die_error(400,"Invalid hash parameter");1016}1017}10181019our$hash_parent=$input_params{'hash_parent'};1020if(defined$hash_parent) {1021if(!validate_refname($hash_parent)) {1022 die_error(400,"Invalid hash parent parameter");1023}1024}10251026our$hash_base=$input_params{'hash_base'};1027if(defined$hash_base) {1028if(!validate_refname($hash_base)) {1029 die_error(400,"Invalid hash base parameter");1030}1031}10321033our@extra_options= @{$input_params{'extra_options'}};1034# @extra_options is always defined, since it can only be (currently) set from1035# CGI, and $cgi->param() returns the empty array in array context if the param1036# is not set1037foreachmy$opt(@extra_options) {1038if(not exists$allowed_options{$opt}) {1039 die_error(400,"Invalid option parameter");1040}1041if(not grep(/^$action$/, @{$allowed_options{$opt}})) {1042 die_error(400,"Invalid option parameter for this action");1043}1044}10451046our$hash_parent_base=$input_params{'hash_parent_base'};1047if(defined$hash_parent_base) {1048if(!validate_refname($hash_parent_base)) {1049 die_error(400,"Invalid hash parent base parameter");1050}1051}10521053# other parameters1054our$page=$input_params{'page'};1055if(defined$page) {1056if($page=~m/[^0-9]/) {1057 die_error(400,"Invalid page parameter");1058}1059}10601061our$searchtype=$input_params{'searchtype'};1062if(defined$searchtype) {1063if($searchtype=~m/[^a-z]/) {1064 die_error(400,"Invalid searchtype parameter");1065}1066}10671068our$search_use_regexp=$input_params{'search_use_regexp'};10691070our$searchtext=$input_params{'searchtext'};1071our$search_regexp;1072if(defined$searchtext) {1073if(length($searchtext) <2) {1074 die_error(403,"At least two characters are required for search parameter");1075}1076if($search_use_regexp) {1077$search_regexp=$searchtext;1078if(!eval{qr/$search_regexp/;1; }) {1079(my$error=$@) =~s/ at \S+ line \d+.*\n?//;1080 die_error(400,"Invalid search regexp '$search_regexp'",1081 esc_html($error));1082}1083}else{1084$search_regexp=quotemeta$searchtext;1085}1086}1087}10881089# path to the current git repository1090our$git_dir;1091sub evaluate_git_dir {1092our$git_dir="$projectroot/$project"if$project;1093}10941095our(@snapshot_fmts,$git_avatar);1096sub configure_gitweb_features {1097# list of supported snapshot formats1098our@snapshot_fmts= gitweb_get_feature('snapshot');1099@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);11001101# check that the avatar feature is set to a known provider name,1102# and for each provider check if the dependencies are satisfied.1103# if the provider name is invalid or the dependencies are not met,1104# reset $git_avatar to the empty string.1105our($git_avatar) = gitweb_get_feature('avatar');1106if($git_avatareq'gravatar') {1107$git_avatar=''unless(eval{require Digest::MD5;1; });1108}elsif($git_avatareq'picon') {1109# no dependencies1110}else{1111$git_avatar='';1112}1113}11141115# custom error handler: 'die <message>' is Internal Server Error1116sub handle_errors_html {1117my$msg=shift;# it is already HTML escaped11181119# to avoid infinite loop where error occurs in die_error,1120# change handler to default handler, disabling handle_errors_html1121 set_message("Error occured when inside die_error:\n$msg");11221123# you cannot jump out of die_error when called as error handler;1124# the subroutine set via CGI::Carp::set_message is called _after_1125# HTTP headers are already written, so it cannot write them itself1126 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1);1127}1128set_message(\&handle_errors_html);11291130# dispatch1131sub dispatch {1132if(!defined$action) {1133if(defined$hash) {1134$action= git_get_type($hash);1135}elsif(defined$hash_base&&defined$file_name) {1136$action= git_get_type("$hash_base:$file_name");1137}elsif(defined$project) {1138$action='summary';1139}else{1140$action='project_list';1141}1142}1143if(!defined($actions{$action})) {1144 die_error(400,"Unknown action");1145}1146if($action!~m/^(?:opml|project_list|project_index)$/&&1147!$project) {1148 die_error(400,"Project needed");1149}1150$actions{$action}->();1151}11521153sub reset_timer {1154our$t0= [ gettimeofday() ]1155ifdefined$t0;1156our$number_of_git_cmds=0;1157}11581159our$first_request=1;1160sub run_request {1161 reset_timer();11621163 evaluate_uri();1164if($first_request) {1165 evaluate_gitweb_config();1166 evaluate_git_version();1167}1168if($per_request_config) {1169if(ref($per_request_config)eq'CODE') {1170$per_request_config->();1171}elsif(!$first_request) {1172 evaluate_gitweb_config();1173}1174}1175 check_loadavg();11761177# $projectroot and $projects_list might be set in gitweb config file1178$projects_list||=$projectroot;11791180 evaluate_query_params();1181 evaluate_path_info();1182 evaluate_and_validate_params();1183 evaluate_git_dir();11841185 configure_gitweb_features();11861187 dispatch();1188}11891190our$is_last_request=sub{1};1191our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1192our$CGI='CGI';1193our$cgi;1194sub configure_as_fcgi {1195require CGI::Fast;1196our$CGI='CGI::Fast';11971198my$request_number=0;1199# let each child service 100 requests1200our$is_last_request=sub{ ++$request_number>100};1201}1202sub evaluate_argv {1203my$script_name=$ENV{'SCRIPT_NAME'} ||$ENV{'SCRIPT_FILENAME'} || __FILE__;1204 configure_as_fcgi()1205if$script_name=~/\.fcgi$/;12061207return unless(@ARGV);12081209require Getopt::Long;1210 Getopt::Long::GetOptions(1211'fastcgi|fcgi|f'=> \&configure_as_fcgi,1212'nproc|n=i'=>sub{1213my($arg,$val) =@_;1214return unlesseval{require FCGI::ProcManager;1; };1215my$proc_manager= FCGI::ProcManager->new({1216 n_processes =>$val,1217});1218our$pre_listen_hook=sub{$proc_manager->pm_manage() };1219our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1220our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1221},1222);1223}12241225sub run {1226 evaluate_argv();12271228$first_request=1;1229$pre_listen_hook->()1230if$pre_listen_hook;12311232 REQUEST:1233while($cgi=$CGI->new()) {1234$pre_dispatch_hook->()1235if$pre_dispatch_hook;12361237 run_request();12381239$post_dispatch_hook->()1240if$post_dispatch_hook;1241$first_request=0;12421243last REQUEST if($is_last_request->());1244}12451246 DONE_GITWEB:12471;1248}12491250run();12511252if(defined caller) {1253# wrapped in a subroutine processing requests,1254# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1255return;1256}else{1257# pure CGI script, serving single request1258exit;1259}12601261## ======================================================================1262## action links12631264# possible values of extra options1265# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1266# -replay => 1 - start from a current view (replay with modifications)1267# -path_info => 0|1 - don't use/use path_info URL (if possible)1268# -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone1269sub href {1270my%params=@_;1271# default is to use -absolute url() i.e. $my_uri1272my$href=$params{-full} ?$my_url:$my_uri;12731274# implicit -replay, must be first of implicit params1275$params{-replay} =1if(keys%params==1&&$params{-anchor});12761277$params{'project'} =$projectunlessexists$params{'project'};12781279if($params{-replay}) {1280while(my($name,$symbol) =each%cgi_param_mapping) {1281if(!exists$params{$name}) {1282$params{$name} =$input_params{$name};1283}1284}1285}12861287my$use_pathinfo= gitweb_check_feature('pathinfo');1288if(defined$params{'project'} &&1289(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1290# try to put as many parameters as possible in PATH_INFO:1291# - project name1292# - action1293# - hash_parent or hash_parent_base:/file_parent1294# - hash or hash_base:/filename1295# - the snapshot_format as an appropriate suffix12961297# When the script is the root DirectoryIndex for the domain,1298# $href here would be something like http://gitweb.example.com/1299# Thus, we strip any trailing / from $href, to spare us double1300# slashes in the final URL1301$href=~ s,/$,,;13021303# Then add the project name, if present1304$href.="/".esc_path_info($params{'project'});1305delete$params{'project'};13061307# since we destructively absorb parameters, we keep this1308# boolean that remembers if we're handling a snapshot1309my$is_snapshot=$params{'action'}eq'snapshot';13101311# Summary just uses the project path URL, any other action is1312# added to the URL1313if(defined$params{'action'}) {1314$href.="/".esc_path_info($params{'action'})1315unless$params{'action'}eq'summary';1316delete$params{'action'};1317}13181319# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1320# stripping nonexistent or useless pieces1321$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1322||$params{'hash_parent'} ||$params{'hash'});1323if(defined$params{'hash_base'}) {1324if(defined$params{'hash_parent_base'}) {1325$href.= esc_path_info($params{'hash_parent_base'});1326# skip the file_parent if it's the same as the file_name1327if(defined$params{'file_parent'}) {1328if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1329delete$params{'file_parent'};1330}elsif($params{'file_parent'} !~/\.\./) {1331$href.=":/".esc_path_info($params{'file_parent'});1332delete$params{'file_parent'};1333}1334}1335$href.="..";1336delete$params{'hash_parent'};1337delete$params{'hash_parent_base'};1338}elsif(defined$params{'hash_parent'}) {1339$href.= esc_path_info($params{'hash_parent'})."..";1340delete$params{'hash_parent'};1341}13421343$href.= esc_path_info($params{'hash_base'});1344if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1345$href.=":/".esc_path_info($params{'file_name'});1346delete$params{'file_name'};1347}1348delete$params{'hash'};1349delete$params{'hash_base'};1350}elsif(defined$params{'hash'}) {1351$href.= esc_path_info($params{'hash'});1352delete$params{'hash'};1353}13541355# If the action was a snapshot, we can absorb the1356# snapshot_format parameter too1357if($is_snapshot) {1358my$fmt=$params{'snapshot_format'};1359# snapshot_format should always be defined when href()1360# is called, but just in case some code forgets, we1361# fall back to the default1362$fmt||=$snapshot_fmts[0];1363$href.=$known_snapshot_formats{$fmt}{'suffix'};1364delete$params{'snapshot_format'};1365}1366}13671368# now encode the parameters explicitly1369my@result= ();1370for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1371my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1372if(defined$params{$name}) {1373if(ref($params{$name})eq"ARRAY") {1374foreachmy$par(@{$params{$name}}) {1375push@result,$symbol."=". esc_param($par);1376}1377}else{1378push@result,$symbol."=". esc_param($params{$name});1379}1380}1381}1382$href.="?".join(';',@result)ifscalar@result;13831384# final transformation: trailing spaces must be escaped (URI-encoded)1385$href=~s/(\s+)$/CGI::escape($1)/e;13861387if($params{-anchor}) {1388$href.="#".esc_param($params{-anchor});1389}13901391return$href;1392}139313941395## ======================================================================1396## validation, quoting/unquoting and escaping13971398sub validate_action {1399my$input=shift||returnundef;1400returnundefunlessexists$actions{$input};1401return$input;1402}14031404sub validate_project {1405my$input=shift||returnundef;1406if(!validate_pathname($input) ||1407!(-d "$projectroot/$input") ||1408!check_export_ok("$projectroot/$input") ||1409($strict_export&& !project_in_list($input))) {1410returnundef;1411}else{1412return$input;1413}1414}14151416sub validate_pathname {1417my$input=shift||returnundef;14181419# no '.' or '..' as elements of path, i.e. no '.' nor '..'1420# at the beginning, at the end, and between slashes.1421# also this catches doubled slashes1422if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1423returnundef;1424}1425# no null characters1426if($input=~m!\0!) {1427returnundef;1428}1429return$input;1430}14311432sub validate_refname {1433my$input=shift||returnundef;14341435# textual hashes are O.K.1436if($input=~m/^[0-9a-fA-F]{40}$/) {1437return$input;1438}1439# it must be correct pathname1440$input= validate_pathname($input)1441orreturnundef;1442# restrictions on ref name according to git-check-ref-format1443if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1444returnundef;1445}1446return$input;1447}14481449# decode sequences of octets in utf8 into Perl's internal form,1450# which is utf-8 with utf8 flag set if needed. gitweb writes out1451# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1452sub to_utf8 {1453my$str=shift;1454returnundefunlessdefined$str;14551456if(utf8::is_utf8($str) || utf8::decode($str)) {1457return$str;1458}else{1459return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1460}1461}14621463# quote unsafe chars, but keep the slash, even when it's not1464# correct, but quoted slashes look too horrible in bookmarks1465sub esc_param {1466my$str=shift;1467returnundefunlessdefined$str;1468$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1469$str=~s/ /\+/g;1470return$str;1471}14721473# the quoting rules for path_info fragment are slightly different1474sub esc_path_info {1475my$str=shift;1476returnundefunlessdefined$str;14771478# path_info doesn't treat '+' as space (specially), but '?' must be escaped1479$str=~s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;14801481return$str;1482}14831484# quote unsafe chars in whole URL, so some characters cannot be quoted1485sub esc_url {1486my$str=shift;1487returnundefunlessdefined$str;1488$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;1489$str=~s/ /\+/g;1490return$str;1491}14921493# quote unsafe characters in HTML attributes1494sub esc_attr {14951496# for XHTML conformance escaping '"' to '"' is not enough1497return esc_html(@_);1498}14991500# replace invalid utf8 character with SUBSTITUTION sequence1501sub esc_html {1502my$str=shift;1503my%opts=@_;15041505returnundefunlessdefined$str;15061507$str= to_utf8($str);1508$str=$cgi->escapeHTML($str);1509if($opts{'-nbsp'}) {1510$str=~s/ / /g;1511}1512$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1513return$str;1514}15151516# quote control characters and escape filename to HTML1517sub esc_path {1518my$str=shift;1519my%opts=@_;15201521returnundefunlessdefined$str;15221523$str= to_utf8($str);1524$str=$cgi->escapeHTML($str);1525if($opts{'-nbsp'}) {1526$str=~s/ / /g;1527}1528$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1529return$str;1530}15311532# Sanitize for use in XHTML + application/xml+xhtm (valid XML 1.0)1533sub sanitize {1534my$str=shift;15351536returnundefunlessdefined$str;15371538$str= to_utf8($str);1539$str=~ s|([[:cntrl:]])|($1=~/[\t\n\r]/?$1: quot_cec($1))|eg;1540return$str;1541}15421543# Make control characters "printable", using character escape codes (CEC)1544sub quot_cec {1545my$cntrl=shift;1546my%opts=@_;1547my%es= (# character escape codes, aka escape sequences1548"\t"=>'\t',# tab (HT)1549"\n"=>'\n',# line feed (LF)1550"\r"=>'\r',# carrige return (CR)1551"\f"=>'\f',# form feed (FF)1552"\b"=>'\b',# backspace (BS)1553"\a"=>'\a',# alarm (bell) (BEL)1554"\e"=>'\e',# escape (ESC)1555"\013"=>'\v',# vertical tab (VT)1556"\000"=>'\0',# nul character (NUL)1557);1558my$chr= ( (exists$es{$cntrl})1559?$es{$cntrl}1560:sprintf('\%2x',ord($cntrl)) );1561if($opts{-nohtml}) {1562return$chr;1563}else{1564return"<span class=\"cntrl\">$chr</span>";1565}1566}15671568# Alternatively use unicode control pictures codepoints,1569# Unicode "printable representation" (PR)1570sub quot_upr {1571my$cntrl=shift;1572my%opts=@_;15731574my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1575if($opts{-nohtml}) {1576return$chr;1577}else{1578return"<span class=\"cntrl\">$chr</span>";1579}1580}15811582# git may return quoted and escaped filenames1583sub unquote {1584my$str=shift;15851586sub unq {1587my$seq=shift;1588my%es= (# character escape codes, aka escape sequences1589't'=>"\t",# tab (HT, TAB)1590'n'=>"\n",# newline (NL)1591'r'=>"\r",# return (CR)1592'f'=>"\f",# form feed (FF)1593'b'=>"\b",# backspace (BS)1594'a'=>"\a",# alarm (bell) (BEL)1595'e'=>"\e",# escape (ESC)1596'v'=>"\013",# vertical tab (VT)1597);15981599if($seq=~m/^[0-7]{1,3}$/) {1600# octal char sequence1601returnchr(oct($seq));1602}elsif(exists$es{$seq}) {1603# C escape sequence, aka character escape code1604return$es{$seq};1605}1606# quoted ordinary character1607return$seq;1608}16091610if($str=~m/^"(.*)"$/) {1611# needs unquoting1612$str=$1;1613$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1614}1615return$str;1616}16171618# escape tabs (convert tabs to spaces)1619sub untabify {1620my$line=shift;16211622while((my$pos=index($line,"\t")) != -1) {1623if(my$count= (8- ($pos%8))) {1624my$spaces=' ' x $count;1625$line=~s/\t/$spaces/;1626}1627}16281629return$line;1630}16311632sub project_in_list {1633my$project=shift;1634my@list= git_get_projects_list();1635return@list&&scalar(grep{$_->{'path'}eq$project}@list);1636}16371638## ----------------------------------------------------------------------1639## HTML aware string manipulation16401641# Try to chop given string on a word boundary between position1642# $len and $len+$add_len. If there is no word boundary there,1643# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1644# (marking chopped part) would be longer than given string.1645sub chop_str {1646my$str=shift;1647my$len=shift;1648my$add_len=shift||10;1649my$where=shift||'right';# 'left' | 'center' | 'right'16501651# Make sure perl knows it is utf8 encoded so we don't1652# cut in the middle of a utf8 multibyte char.1653$str= to_utf8($str);16541655# allow only $len chars, but don't cut a word if it would fit in $add_len1656# if it doesn't fit, cut it if it's still longer than the dots we would add1657# remove chopped character entities entirely16581659# when chopping in the middle, distribute $len into left and right part1660# return early if chopping wouldn't make string shorter1661if($whereeq'center') {1662return$strif($len+5>=length($str));# filler is length 51663$len=int($len/2);1664}else{1665return$strif($len+4>=length($str));# filler is length 41666}16671668# regexps: ending and beginning with word part up to $add_len1669my$endre=qr/.{$len}\w{0,$add_len}/;1670my$begre=qr/\w{0,$add_len}.{$len}/;16711672if($whereeq'left') {1673$str=~m/^(.*?)($begre)$/;1674my($lead,$body) = ($1,$2);1675if(length($lead) >4) {1676$lead=" ...";1677}1678return"$lead$body";16791680}elsif($whereeq'center') {1681$str=~m/^($endre)(.*)$/;1682my($left,$str) = ($1,$2);1683$str=~m/^(.*?)($begre)$/;1684my($mid,$right) = ($1,$2);1685if(length($mid) >5) {1686$mid=" ... ";1687}1688return"$left$mid$right";16891690}else{1691$str=~m/^($endre)(.*)$/;1692my$body=$1;1693my$tail=$2;1694if(length($tail) >4) {1695$tail="... ";1696}1697return"$body$tail";1698}1699}17001701# takes the same arguments as chop_str, but also wraps a <span> around the1702# result with a title attribute if it does get chopped. Additionally, the1703# string is HTML-escaped.1704sub chop_and_escape_str {1705my($str) =@_;17061707my$chopped= chop_str(@_);1708$str= to_utf8($str);1709if($choppedeq$str) {1710return esc_html($chopped);1711}else{1712$str=~s/[[:cntrl:]]/?/g;1713return$cgi->span({-title=>$str}, esc_html($chopped));1714}1715}17161717## ----------------------------------------------------------------------1718## functions returning short strings17191720# CSS class for given age value (in seconds)1721sub age_class {1722my$age=shift;17231724if(!defined$age) {1725return"noage";1726}elsif($age<60*60*2) {1727return"age0";1728}elsif($age<60*60*24*2) {1729return"age1";1730}else{1731return"age2";1732}1733}17341735# convert age in seconds to "nn units ago" string1736sub age_string {1737my$age=shift;1738my$age_str;17391740if($age>60*60*24*365*2) {1741$age_str= (int$age/60/60/24/365);1742$age_str.=" years ago";1743}elsif($age>60*60*24*(365/12)*2) {1744$age_str=int$age/60/60/24/(365/12);1745$age_str.=" months ago";1746}elsif($age>60*60*24*7*2) {1747$age_str=int$age/60/60/24/7;1748$age_str.=" weeks ago";1749}elsif($age>60*60*24*2) {1750$age_str=int$age/60/60/24;1751$age_str.=" days ago";1752}elsif($age>60*60*2) {1753$age_str=int$age/60/60;1754$age_str.=" hours ago";1755}elsif($age>60*2) {1756$age_str=int$age/60;1757$age_str.=" min ago";1758}elsif($age>2) {1759$age_str=int$age;1760$age_str.=" sec ago";1761}else{1762$age_str.=" right now";1763}1764return$age_str;1765}17661767useconstant{1768 S_IFINVALID =>0030000,1769 S_IFGITLINK =>0160000,1770};17711772# submodule/subproject, a commit object reference1773sub S_ISGITLINK {1774my$mode=shift;17751776return(($mode& S_IFMT) == S_IFGITLINK)1777}17781779# convert file mode in octal to symbolic file mode string1780sub mode_str {1781my$mode=oct shift;17821783if(S_ISGITLINK($mode)) {1784return'm---------';1785}elsif(S_ISDIR($mode& S_IFMT)) {1786return'drwxr-xr-x';1787}elsif(S_ISLNK($mode)) {1788return'lrwxrwxrwx';1789}elsif(S_ISREG($mode)) {1790# git cares only about the executable bit1791if($mode& S_IXUSR) {1792return'-rwxr-xr-x';1793}else{1794return'-rw-r--r--';1795};1796}else{1797return'----------';1798}1799}18001801# convert file mode in octal to file type string1802sub file_type {1803my$mode=shift;18041805if($mode!~m/^[0-7]+$/) {1806return$mode;1807}else{1808$mode=oct$mode;1809}18101811if(S_ISGITLINK($mode)) {1812return"submodule";1813}elsif(S_ISDIR($mode& S_IFMT)) {1814return"directory";1815}elsif(S_ISLNK($mode)) {1816return"symlink";1817}elsif(S_ISREG($mode)) {1818return"file";1819}else{1820return"unknown";1821}1822}18231824# convert file mode in octal to file type description string1825sub file_type_long {1826my$mode=shift;18271828if($mode!~m/^[0-7]+$/) {1829return$mode;1830}else{1831$mode=oct$mode;1832}18331834if(S_ISGITLINK($mode)) {1835return"submodule";1836}elsif(S_ISDIR($mode& S_IFMT)) {1837return"directory";1838}elsif(S_ISLNK($mode)) {1839return"symlink";1840}elsif(S_ISREG($mode)) {1841if($mode& S_IXUSR) {1842return"executable";1843}else{1844return"file";1845};1846}else{1847return"unknown";1848}1849}185018511852## ----------------------------------------------------------------------1853## functions returning short HTML fragments, or transforming HTML fragments1854## which don't belong to other sections18551856# format line of commit message.1857sub format_log_line_html {1858my$line=shift;18591860$line= esc_html($line, -nbsp=>1);1861$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1862$cgi->a({-href => href(action=>"object", hash=>$1),1863-class=>"text"},$1);1864}eg;18651866return$line;1867}18681869# format marker of refs pointing to given object18701871# the destination action is chosen based on object type and current context:1872# - for annotated tags, we choose the tag view unless it's the current view1873# already, in which case we go to shortlog view1874# - for other refs, we keep the current view if we're in history, shortlog or1875# log view, and select shortlog otherwise1876sub format_ref_marker {1877my($refs,$id) =@_;1878my$markers='';18791880if(defined$refs->{$id}) {1881foreachmy$ref(@{$refs->{$id}}) {1882# this code exploits the fact that non-lightweight tags are the1883# only indirect objects, and that they are the only objects for which1884# we want to use tag instead of shortlog as action1885my($type,$name) =qw();1886my$indirect= ($ref=~s/\^\{\}$//);1887# e.g. tags/v2.6.11 or heads/next1888if($ref=~m!^(.*?)s?/(.*)$!) {1889$type=$1;1890$name=$2;1891}else{1892$type="ref";1893$name=$ref;1894}18951896my$class=$type;1897$class.=" indirect"if$indirect;18981899my$dest_action="shortlog";19001901if($indirect) {1902$dest_action="tag"unless$actioneq"tag";1903}elsif($action=~/^(history|(short)?log)$/) {1904$dest_action=$action;1905}19061907my$dest="";1908$dest.="refs/"unless$ref=~ m!^refs/!;1909$dest.=$ref;19101911my$link=$cgi->a({1912-href => href(1913 action=>$dest_action,1914 hash=>$dest1915)},$name);19161917$markers.=" <span class=\"".esc_attr($class)."\"title=\"".esc_attr($ref)."\">".1918$link."</span>";1919}1920}19211922if($markers) {1923return' <span class="refs">'.$markers.'</span>';1924}else{1925return"";1926}1927}19281929# format, perhaps shortened and with markers, title line1930sub format_subject_html {1931my($long,$short,$href,$extra) =@_;1932$extra=''unlessdefined($extra);19331934if(length($short) <length($long)) {1935$long=~s/[[:cntrl:]]/?/g;1936return$cgi->a({-href =>$href, -class=>"list subject",1937-title => to_utf8($long)},1938 esc_html($short)) .$extra;1939}else{1940return$cgi->a({-href =>$href, -class=>"list subject"},1941 esc_html($long)) .$extra;1942}1943}19441945# Rather than recomputing the url for an email multiple times, we cache it1946# after the first hit. This gives a visible benefit in views where the avatar1947# for the same email is used repeatedly (e.g. shortlog).1948# The cache is shared by all avatar engines (currently gravatar only), which1949# are free to use it as preferred. Since only one avatar engine is used for any1950# given page, there's no risk for cache conflicts.1951our%avatar_cache= ();19521953# Compute the picon url for a given email, by using the picon search service over at1954# http://www.cs.indiana.edu/picons/search.html1955sub picon_url {1956my$email=lc shift;1957if(!$avatar_cache{$email}) {1958my($user,$domain) =split('@',$email);1959$avatar_cache{$email} =1960"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1961"$domain/$user/".1962"users+domains+unknown/up/single";1963}1964return$avatar_cache{$email};1965}19661967# Compute the gravatar url for a given email, if it's not in the cache already.1968# Gravatar stores only the part of the URL before the size, since that's the1969# one computationally more expensive. This also allows reuse of the cache for1970# different sizes (for this particular engine).1971sub gravatar_url {1972my$email=lc shift;1973my$size=shift;1974$avatar_cache{$email} ||=1975"http://www.gravatar.com/avatar/".1976 Digest::MD5::md5_hex($email) ."?s=";1977return$avatar_cache{$email} .$size;1978}19791980# Insert an avatar for the given $email at the given $size if the feature1981# is enabled.1982sub git_get_avatar {1983my($email,%opts) =@_;1984my$pre_white= ($opts{-pad_before} ?" ":"");1985my$post_white= ($opts{-pad_after} ?" ":"");1986$opts{-size} ||='default';1987my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1988my$url="";1989if($git_avatareq'gravatar') {1990$url= gravatar_url($email,$size);1991}elsif($git_avatareq'picon') {1992$url= picon_url($email);1993}1994# Other providers can be added by extending the if chain, defining $url1995# as needed. If no variant puts something in $url, we assume avatars1996# are completely disabled/unavailable.1997if($url) {1998return$pre_white.1999"<img width=\"$size\"".2000"class=\"avatar\"".2001"src=\"".esc_url($url)."\"".2002"alt=\"\"".2003"/>".$post_white;2004}else{2005return"";2006}2007}20082009sub format_search_author {2010my($author,$searchtype,$displaytext) =@_;2011my$have_search= gitweb_check_feature('search');20122013if($have_search) {2014my$performed="";2015if($searchtypeeq'author') {2016$performed="authored";2017}elsif($searchtypeeq'committer') {2018$performed="committed";2019}20202021return$cgi->a({-href => href(action=>"search", hash=>$hash,2022 searchtext=>$author,2023 searchtype=>$searchtype),class=>"list",2024 title=>"Search for commits$performedby$author"},2025$displaytext);20262027}else{2028return$displaytext;2029}2030}20312032# format the author name of the given commit with the given tag2033# the author name is chopped and escaped according to the other2034# optional parameters (see chop_str).2035sub format_author_html {2036my$tag=shift;2037my$co=shift;2038my$author= chop_and_escape_str($co->{'author_name'},@_);2039return"<$tagclass=\"author\">".2040 format_search_author($co->{'author_name'},"author",2041 git_get_avatar($co->{'author_email'}, -pad_after =>1) .2042$author) .2043"</$tag>";2044}20452046# format git diff header line, i.e. "diff --(git|combined|cc) ..."2047sub format_git_diff_header_line {2048my$line=shift;2049my$diffinfo=shift;2050my($from,$to) =@_;20512052if($diffinfo->{'nparents'}) {2053# combined diff2054$line=~s!^(diff (.*?) )"?.*$!$1!;2055if($to->{'href'}) {2056$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2057 esc_path($to->{'file'}));2058}else{# file was deleted (no href)2059$line.= esc_path($to->{'file'});2060}2061}else{2062# "ordinary" diff2063$line=~s!^(diff (.*?) )"?a/.*$!$1!;2064if($from->{'href'}) {2065$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},2066'a/'. esc_path($from->{'file'}));2067}else{# file was added (no href)2068$line.='a/'. esc_path($from->{'file'});2069}2070$line.=' ';2071if($to->{'href'}) {2072$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2073'b/'. esc_path($to->{'file'}));2074}else{# file was deleted2075$line.='b/'. esc_path($to->{'file'});2076}2077}20782079return"<div class=\"diff header\">$line</div>\n";2080}20812082# format extended diff header line, before patch itself2083sub format_extended_diff_header_line {2084my$line=shift;2085my$diffinfo=shift;2086my($from,$to) =@_;20872088# match <path>2089if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {2090$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},2091 esc_path($from->{'file'}));2092}2093if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {2094$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},2095 esc_path($to->{'file'}));2096}2097# match single <mode>2098if($line=~m/\s(\d{6})$/) {2099$line.='<span class="info"> ('.2100 file_type_long($1) .2101')</span>';2102}2103# match <hash>2104if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {2105# can match only for combined diff2106$line='index ';2107for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2108if($from->{'href'}[$i]) {2109$line.=$cgi->a({-href=>$from->{'href'}[$i],2110-class=>"hash"},2111substr($diffinfo->{'from_id'}[$i],0,7));2112}else{2113$line.='0' x 7;2114}2115# separator2116$line.=','if($i<$diffinfo->{'nparents'} -1);2117}2118$line.='..';2119if($to->{'href'}) {2120$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2121substr($diffinfo->{'to_id'},0,7));2122}else{2123$line.='0' x 7;2124}21252126}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {2127# can match only for ordinary diff2128my($from_link,$to_link);2129if($from->{'href'}) {2130$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},2131substr($diffinfo->{'from_id'},0,7));2132}else{2133$from_link='0' x 7;2134}2135if($to->{'href'}) {2136$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2137substr($diffinfo->{'to_id'},0,7));2138}else{2139$to_link='0' x 7;2140}2141my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});2142$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;2143}21442145return$line."<br/>\n";2146}21472148# format from-file/to-file diff header2149sub format_diff_from_to_header {2150my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;2151my$line;2152my$result='';21532154$line=$from_line;2155#assert($line =~ m/^---/) if DEBUG;2156# no extra formatting for "^--- /dev/null"2157if(!$diffinfo->{'nparents'}) {2158# ordinary (single parent) diff2159if($line=~m!^--- "?a/!) {2160if($from->{'href'}) {2161$line='--- a/'.2162$cgi->a({-href=>$from->{'href'}, -class=>"path"},2163 esc_path($from->{'file'}));2164}else{2165$line='--- a/'.2166 esc_path($from->{'file'});2167}2168}2169$result.= qq!<div class="diff from_file">$line</div>\n!;21702171}else{2172# combined diff (merge commit)2173for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2174if($from->{'href'}[$i]) {2175$line='--- '.2176$cgi->a({-href=>href(action=>"blobdiff",2177 hash_parent=>$diffinfo->{'from_id'}[$i],2178 hash_parent_base=>$parents[$i],2179 file_parent=>$from->{'file'}[$i],2180 hash=>$diffinfo->{'to_id'},2181 hash_base=>$hash,2182 file_name=>$to->{'file'}),2183-class=>"path",2184-title=>"diff". ($i+1)},2185$i+1) .2186'/'.2187$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2188 esc_path($from->{'file'}[$i]));2189}else{2190$line='--- /dev/null';2191}2192$result.= qq!<div class="diff from_file">$line</div>\n!;2193}2194}21952196$line=$to_line;2197#assert($line =~ m/^\+\+\+/) if DEBUG;2198# no extra formatting for "^+++ /dev/null"2199if($line=~m!^\+\+\+ "?b/!) {2200if($to->{'href'}) {2201$line='+++ b/'.2202$cgi->a({-href=>$to->{'href'}, -class=>"path"},2203 esc_path($to->{'file'}));2204}else{2205$line='+++ b/'.2206 esc_path($to->{'file'});2207}2208}2209$result.= qq!<div class="diff to_file">$line</div>\n!;22102211return$result;2212}22132214# create note for patch simplified by combined diff2215sub format_diff_cc_simplified {2216my($diffinfo,@parents) =@_;2217my$result='';22182219$result.="<div class=\"diff header\">".2220"diff --cc ";2221if(!is_deleted($diffinfo)) {2222$result.=$cgi->a({-href => href(action=>"blob",2223 hash_base=>$hash,2224 hash=>$diffinfo->{'to_id'},2225 file_name=>$diffinfo->{'to_file'}),2226-class=>"path"},2227 esc_path($diffinfo->{'to_file'}));2228}else{2229$result.= esc_path($diffinfo->{'to_file'});2230}2231$result.="</div>\n".# class="diff header"2232"<div class=\"diff nodifferences\">".2233"Simple merge".2234"</div>\n";# class="diff nodifferences"22352236return$result;2237}22382239sub diff_line_class {2240my($line,$from,$to) =@_;22412242# ordinary diff2243my$num_sign=1;2244# combined diff2245if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2246$num_sign=scalar@{$from->{'href'}};2247}22482249my@diff_line_classifier= (2250{ regexp =>qr/^\@\@{$num_sign} /,class=>"chunk_header"},2251{ regexp =>qr/^\\/,class=>"incomplete"},2252{ regexp =>qr/^ {$num_sign}/,class=>"ctx"},2253# classifier for context must come before classifier add/rem,2254# or we would have to use more complicated regexp, for example2255# qr/(?= {0,$m}\+)[+ ]{$num_sign}/, where $m = $num_sign - 1;2256{ regexp =>qr/^[+ ]{$num_sign}/,class=>"add"},2257{ regexp =>qr/^[- ]{$num_sign}/,class=>"rem"},2258);2259formy$clsfy(@diff_line_classifier) {2260return$clsfy->{'class'}2261if($line=~$clsfy->{'regexp'});2262}22632264# fallback2265return"";2266}22672268# assumes that $from and $to are defined and correctly filled,2269# and that $line holds a line of chunk header for unified diff2270sub format_unidiff_chunk_header {2271my($line,$from,$to) =@_;22722273my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2274$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;22752276$from_lines=0unlessdefined$from_lines;2277$to_lines=0unlessdefined$to_lines;22782279if($from->{'href'}) {2280$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2281-class=>"list"},$from_text);2282}2283if($to->{'href'}) {2284$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2285-class=>"list"},$to_text);2286}2287$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2288"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2289return$line;2290}22912292# assumes that $from and $to are defined and correctly filled,2293# and that $line holds a line of chunk header for combined diff2294sub format_cc_diff_chunk_header {2295my($line,$from,$to) =@_;22962297my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2298my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);22992300@from_text=split(' ',$ranges);2301for(my$i=0;$i<@from_text; ++$i) {2302($from_start[$i],$from_nlines[$i]) =2303(split(',',substr($from_text[$i],1)),0);2304}23052306$to_text=pop@from_text;2307$to_start=pop@from_start;2308$to_nlines=pop@from_nlines;23092310$line="<span class=\"chunk_info\">$prefix";2311for(my$i=0;$i<@from_text; ++$i) {2312if($from->{'href'}[$i]) {2313$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2314-class=>"list"},$from_text[$i]);2315}else{2316$line.=$from_text[$i];2317}2318$line.=" ";2319}2320if($to->{'href'}) {2321$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2322-class=>"list"},$to_text);2323}else{2324$line.=$to_text;2325}2326$line.="$prefix</span>".2327"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2328return$line;2329}23302331# process patch (diff) line (not to be used for diff headers),2332# returning class and HTML-formatted (but not wrapped) line2333sub process_diff_line {2334my$line=shift;2335my($from,$to) =@_;23362337my$diff_class= diff_line_class($line,$from,$to);23382339chomp$line;2340$line= untabify($line);23412342if($from&&$to&&$line=~m/^\@{2} /) {2343$line= format_unidiff_chunk_header($line,$from,$to);2344return$diff_class,$line;23452346}elsif($from&&$to&&$line=~m/^\@{3}/) {2347$line= format_cc_diff_chunk_header($line,$from,$to);2348return$diff_class,$line;23492350}2351return$diff_class, esc_html($line, -nbsp=>1);2352}23532354# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2355# linked. Pass the hash of the tree/commit to snapshot.2356sub format_snapshot_links {2357my($hash) =@_;2358my$num_fmts=@snapshot_fmts;2359if($num_fmts>1) {2360# A parenthesized list of links bearing format names.2361# e.g. "snapshot (_tar.gz_ _zip_)"2362return"snapshot (".join(' ',map2363$cgi->a({2364-href => href(2365 action=>"snapshot",2366 hash=>$hash,2367 snapshot_format=>$_2368)2369},$known_snapshot_formats{$_}{'display'})2370,@snapshot_fmts) .")";2371}elsif($num_fmts==1) {2372# A single "snapshot" link whose tooltip bears the format name.2373# i.e. "_snapshot_"2374my($fmt) =@snapshot_fmts;2375return2376$cgi->a({2377-href => href(2378 action=>"snapshot",2379 hash=>$hash,2380 snapshot_format=>$fmt2381),2382-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2383},"snapshot");2384}else{# $num_fmts == 02385returnundef;2386}2387}23882389## ......................................................................2390## functions returning values to be passed, perhaps after some2391## transformation, to other functions; e.g. returning arguments to href()23922393# returns hash to be passed to href to generate gitweb URL2394# in -title key it returns description of link2395sub get_feed_info {2396my$format=shift||'Atom';2397my%res= (action =>lc($format));23982399# feed links are possible only for project views2400return unless(defined$project);2401# some views should link to OPML, or to generic project feed,2402# or don't have specific feed yet (so they should use generic)2403return if($action=~/^(?:tags|heads|forks|tag|search)$/x);24042405my$branch;2406# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2407# from tag links; this also makes possible to detect branch links2408if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2409(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2410$branch=$1;2411}2412# find log type for feed description (title)2413my$type='log';2414if(defined$file_name) {2415$type="history of$file_name";2416$type.="/"if($actioneq'tree');2417$type.=" on '$branch'"if(defined$branch);2418}else{2419$type="log of$branch"if(defined$branch);2420}24212422$res{-title} =$type;2423$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2424$res{'file_name'} =$file_name;24252426return%res;2427}24282429## ----------------------------------------------------------------------2430## git utility subroutines, invoking git commands24312432# returns path to the core git executable and the --git-dir parameter as list2433sub git_cmd {2434$number_of_git_cmds++;2435return$GIT,'--git-dir='.$git_dir;2436}24372438# quote the given arguments for passing them to the shell2439# quote_command("command", "arg 1", "arg with ' and ! characters")2440# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2441# Try to avoid using this function wherever possible.2442sub quote_command {2443returnjoin(' ',2444map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2445}24462447# get HEAD ref of given project as hash2448sub git_get_head_hash {2449return git_get_full_hash(shift,'HEAD');2450}24512452sub git_get_full_hash {2453return git_get_hash(@_);2454}24552456sub git_get_short_hash {2457return git_get_hash(@_,'--short=7');2458}24592460sub git_get_hash {2461my($project,$hash,@options) =@_;2462my$o_git_dir=$git_dir;2463my$retval=undef;2464$git_dir="$projectroot/$project";2465if(open my$fd,'-|', git_cmd(),'rev-parse',2466'--verify','-q',@options,$hash) {2467$retval= <$fd>;2468chomp$retvalifdefined$retval;2469close$fd;2470}2471if(defined$o_git_dir) {2472$git_dir=$o_git_dir;2473}2474return$retval;2475}24762477# get type of given object2478sub git_get_type {2479my$hash=shift;24802481open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2482my$type= <$fd>;2483close$fdorreturn;2484chomp$type;2485return$type;2486}24872488# repository configuration2489our$config_file='';2490our%config;24912492# store multiple values for single key as anonymous array reference2493# single values stored directly in the hash, not as [ <value> ]2494sub hash_set_multi {2495my($hash,$key,$value) =@_;24962497if(!exists$hash->{$key}) {2498$hash->{$key} =$value;2499}elsif(!ref$hash->{$key}) {2500$hash->{$key} = [$hash->{$key},$value];2501}else{2502push@{$hash->{$key}},$value;2503}2504}25052506# return hash of git project configuration2507# optionally limited to some section, e.g. 'gitweb'2508sub git_parse_project_config {2509my$section_regexp=shift;2510my%config;25112512local$/="\0";25132514open my$fh,"-|", git_cmd(),"config",'-z','-l',2515orreturn;25162517while(my$keyval= <$fh>) {2518chomp$keyval;2519my($key,$value) =split(/\n/,$keyval,2);25202521 hash_set_multi(\%config,$key,$value)2522if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2523}2524close$fh;25252526return%config;2527}25282529# convert config value to boolean: 'true' or 'false'2530# no value, number > 0, 'true' and 'yes' values are true2531# rest of values are treated as false (never as error)2532sub config_to_bool {2533my$val=shift;25342535return1if!defined$val;# section.key25362537# strip leading and trailing whitespace2538$val=~s/^\s+//;2539$val=~s/\s+$//;25402541return(($val=~/^\d+$/&&$val) ||# section.key = 12542($val=~/^(?:true|yes)$/i));# section.key = true2543}25442545# convert config value to simple decimal number2546# an optional value suffix of 'k', 'm', or 'g' will cause the value2547# to be multiplied by 1024, 1048576, or 10737418242548sub config_to_int {2549my$val=shift;25502551# strip leading and trailing whitespace2552$val=~s/^\s+//;2553$val=~s/\s+$//;25542555if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2556$unit=lc($unit);2557# unknown unit is treated as 12558return$num* ($uniteq'g'?1073741824:2559$uniteq'm'?1048576:2560$uniteq'k'?1024:1);2561}2562return$val;2563}25642565# convert config value to array reference, if needed2566sub config_to_multi {2567my$val=shift;25682569returnref($val) ?$val: (defined($val) ? [$val] : []);2570}25712572sub git_get_project_config {2573my($key,$type) =@_;25742575return unlessdefined$git_dir;25762577# key sanity check2578return unless($key);2579# only subsection, if exists, is case sensitive,2580# and not lowercased by 'git config -z -l'2581if(my($hi,$mi,$lo) = ($key=~/^([^.]*)\.(.*)\.([^.]*)$/)) {2582$key=join(".",lc($hi),$mi,lc($lo));2583}else{2584$key=lc($key);2585}2586$key=~s/^gitweb\.//;2587return if($key=~m/\W/);25882589# type sanity check2590if(defined$type) {2591$type=~s/^--//;2592$type=undef2593unless($typeeq'bool'||$typeeq'int');2594}25952596# get config2597if(!defined$config_file||2598$config_filene"$git_dir/config") {2599%config= git_parse_project_config('gitweb');2600$config_file="$git_dir/config";2601}26022603# check if config variable (key) exists2604return unlessexists$config{"gitweb.$key"};26052606# ensure given type2607if(!defined$type) {2608return$config{"gitweb.$key"};2609}elsif($typeeq'bool') {2610# backward compatibility: 'git config --bool' returns true/false2611return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2612}elsif($typeeq'int') {2613return config_to_int($config{"gitweb.$key"});2614}2615return$config{"gitweb.$key"};2616}26172618# get hash of given path at given ref2619sub git_get_hash_by_path {2620my$base=shift;2621my$path=shift||returnundef;2622my$type=shift;26232624$path=~ s,/+$,,;26252626open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2627or die_error(500,"Open git-ls-tree failed");2628my$line= <$fd>;2629close$fdorreturnundef;26302631if(!defined$line) {2632# there is no tree or hash given by $path at $base2633returnundef;2634}26352636#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2637$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2638if(defined$type&&$typene$2) {2639# type doesn't match2640returnundef;2641}2642return$3;2643}26442645# get path of entry with given hash at given tree-ish (ref)2646# used to get 'from' filename for combined diff (merge commit) for renames2647sub git_get_path_by_hash {2648my$base=shift||return;2649my$hash=shift||return;26502651local$/="\0";26522653open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2654orreturnundef;2655while(my$line= <$fd>) {2656chomp$line;26572658#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2659#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2660if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2661close$fd;2662return$1;2663}2664}2665close$fd;2666returnundef;2667}26682669## ......................................................................2670## git utility functions, directly accessing git repository26712672# get the value of config variable either from file named as the variable2673# itself in the repository ($GIT_DIR/$name file), or from gitweb.$name2674# configuration variable in the repository config file.2675sub git_get_file_or_project_config {2676my($path,$name) =@_;26772678$git_dir="$projectroot/$path";2679open my$fd,'<',"$git_dir/$name"2680orreturn git_get_project_config($name);2681my$conf= <$fd>;2682close$fd;2683if(defined$conf) {2684chomp$conf;2685}2686return$conf;2687}26882689sub git_get_project_description {2690my$path=shift;2691return git_get_file_or_project_config($path,'description');2692}26932694sub git_get_project_category {2695my$path=shift;2696return git_get_file_or_project_config($path,'category');2697}269826992700# supported formats:2701# * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)2702# - if its contents is a number, use it as tag weight,2703# - otherwise add a tag with weight 12704# * $GIT_DIR/ctags file, each line is a tag (with weight 1)2705# the same value multiple times increases tag weight2706# * `gitweb.ctag' multi-valued repo config variable2707sub git_get_project_ctags {2708my$project=shift;2709my$ctags= {};27102711$git_dir="$projectroot/$project";2712if(opendir my$dh,"$git_dir/ctags") {2713my@files=grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh);2714foreachmy$tagfile(@files) {2715open my$ct,'<',$tagfile2716ornext;2717my$val= <$ct>;2718chomp$valif$val;2719close$ct;27202721(my$ctag=$tagfile) =~ s#.*/##;2722if($val=~/^\d+$/) {2723$ctags->{$ctag} =$val;2724}else{2725$ctags->{$ctag} =1;2726}2727}2728closedir$dh;27292730}elsif(open my$fh,'<',"$git_dir/ctags") {2731while(my$line= <$fh>) {2732chomp$line;2733$ctags->{$line}++if$line;2734}2735close$fh;27362737}else{2738my$taglist= config_to_multi(git_get_project_config('ctag'));2739foreachmy$tag(@$taglist) {2740$ctags->{$tag}++;2741}2742}27432744return$ctags;2745}27462747# return hash, where keys are content tags ('ctags'),2748# and values are sum of weights of given tag in every project2749sub git_gather_all_ctags {2750my$projects=shift;2751my$ctags= {};27522753foreachmy$p(@$projects) {2754foreachmy$ct(keys%{$p->{'ctags'}}) {2755$ctags->{$ct} +=$p->{'ctags'}->{$ct};2756}2757}27582759return$ctags;2760}27612762sub git_populate_project_tagcloud {2763my$ctags=shift;27642765# First, merge different-cased tags; tags vote on casing2766my%ctags_lc;2767foreach(keys%$ctags) {2768$ctags_lc{lc$_}->{count} +=$ctags->{$_};2769if(not$ctags_lc{lc$_}->{topcount}2770or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2771$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2772$ctags_lc{lc$_}->{topname} =$_;2773}2774}27752776my$cloud;2777my$matched=$input_params{'ctag'};2778if(eval{require HTML::TagCloud;1; }) {2779$cloud= HTML::TagCloud->new;2780foreachmy$ctag(sort keys%ctags_lc) {2781# Pad the title with spaces so that the cloud looks2782# less crammed.2783my$title= esc_html($ctags_lc{$ctag}->{topname});2784$title=~s/ / /g;2785$title=~s/^/ /g;2786$title=~s/$/ /g;2787if(defined$matched&&$matchedeq$ctag) {2788$title=qq(<span class="match">$title</span>);2789}2790$cloud->add($title, href(project=>undef, ctag=>$ctag),2791$ctags_lc{$ctag}->{count});2792}2793}else{2794$cloud= {};2795foreachmy$ctag(keys%ctags_lc) {2796my$title= esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);2797if(defined$matched&&$matchedeq$ctag) {2798$title=qq(<span class="match">$title</span>);2799}2800$cloud->{$ctag}{count} =$ctags_lc{$ctag}->{count};2801$cloud->{$ctag}{ctag} =2802$cgi->a({-href=>href(project=>undef, ctag=>$ctag)},$title);2803}2804}2805return$cloud;2806}28072808sub git_show_project_tagcloud {2809my($cloud,$count) =@_;2810if(ref$cloudeq'HTML::TagCloud') {2811return$cloud->html_and_css($count);2812}else{2813my@tags=sort{$cloud->{$a}->{'count'} <=>$cloud->{$b}->{'count'} }keys%$cloud;2814return2815'<div id="htmltagcloud"'.($project?'':' align="center"').'>'.2816join(', ',map{2817$cloud->{$_}->{'ctag'}2818}splice(@tags,0,$count)) .2819'</div>';2820}2821}28222823sub git_get_project_url_list {2824my$path=shift;28252826$git_dir="$projectroot/$path";2827open my$fd,'<',"$git_dir/cloneurl"2828orreturnwantarray?2829@{ config_to_multi(git_get_project_config('url')) } :2830 config_to_multi(git_get_project_config('url'));2831my@git_project_url_list=map{chomp;$_} <$fd>;2832close$fd;28332834returnwantarray?@git_project_url_list: \@git_project_url_list;2835}28362837sub git_get_projects_list {2838my$filter=shift||'';2839my@list;28402841$filter=~s/\.git$//;28422843if(-d $projects_list) {2844# search in directory2845my$dir=$projects_list;2846# remove the trailing "/"2847$dir=~s!/+$!!;2848my$pfxlen=length("$dir");2849my$pfxdepth= ($dir=~tr!/!!);2850# when filtering, search only given subdirectory2851if($filter) {2852$dir.="/$filter";2853$dir=~s!/+$!!;2854}28552856 File::Find::find({2857 follow_fast =>1,# follow symbolic links2858 follow_skip =>2,# ignore duplicates2859 dangling_symlinks =>0,# ignore dangling symlinks, silently2860 wanted =>sub{2861# global variables2862our$project_maxdepth;2863our$projectroot;2864# skip project-list toplevel, if we get it.2865return if(m!^[/.]$!);2866# only directories can be git repositories2867return unless(-d $_);2868# don't traverse too deep (Find is super slow on os x)2869# $project_maxdepth excludes depth of $projectroot2870if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2871$File::Find::prune =1;2872return;2873}28742875my$path=substr($File::Find::name,$pfxlen+1);2876# we check related file in $projectroot2877if(check_export_ok("$projectroot/$path")) {2878push@list, { path =>$path};2879$File::Find::prune =1;2880}2881},2882},"$dir");28832884}elsif(-f $projects_list) {2885# read from file(url-encoded):2886# 'git%2Fgit.git Linus+Torvalds'2887# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2888# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2889open my$fd,'<',$projects_listorreturn;2890 PROJECT:2891while(my$line= <$fd>) {2892chomp$line;2893my($path,$owner) =split' ',$line;2894$path= unescape($path);2895$owner= unescape($owner);2896if(!defined$path) {2897next;2898}2899# if $filter is rpovided, check if $path begins with $filter2900if($filter&&$path!~m!^\Q$filter\E/!) {2901next;2902}2903if(check_export_ok("$projectroot/$path")) {2904my$pr= {2905 path =>$path,2906 owner => to_utf8($owner),2907};2908push@list,$pr;2909}2910}2911close$fd;2912}2913return@list;2914}29152916# written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)2917# as side effects it sets 'forks' field to list of forks for forked projects2918sub filter_forks_from_projects_list {2919my$projects=shift;29202921my%trie;# prefix tree of directories (path components)2922# generate trie out of those directories that might contain forks2923foreachmy$pr(@$projects) {2924my$path=$pr->{'path'};2925$path=~s/\.git$//;# forks of 'repo.git' are in 'repo/' directory2926next if($path=~m!/$!);# skip non-bare repositories, e.g. 'repo/.git'2927next unless($path);# skip '.git' repository: tests, git-instaweb2928next unless(-d "$projectroot/$path");# containing directory exists2929$pr->{'forks'} = [];# there can be 0 or more forks of project29302931# add to trie2932my@dirs=split('/',$path);2933# walk the trie, until either runs out of components or out of trie2934my$ref= \%trie;2935while(scalar@dirs&&2936exists($ref->{$dirs[0]})) {2937$ref=$ref->{shift@dirs};2938}2939# create rest of trie structure from rest of components2940foreachmy$dir(@dirs) {2941$ref=$ref->{$dir} = {};2942}2943# create end marker, store $pr as a data2944$ref->{''} =$prif(!exists$ref->{''});2945}29462947# filter out forks, by finding shortest prefix match for paths2948my@filtered;2949 PROJECT:2950foreachmy$pr(@$projects) {2951# trie lookup2952my$ref= \%trie;2953 DIR:2954foreachmy$dir(split('/',$pr->{'path'})) {2955if(exists$ref->{''}) {2956# found [shortest] prefix, is a fork - skip it2957push@{$ref->{''}{'forks'}},$pr;2958next PROJECT;2959}2960if(!exists$ref->{$dir}) {2961# not in trie, cannot have prefix, not a fork2962push@filtered,$pr;2963next PROJECT;2964}2965# If the dir is there, we just walk one step down the trie.2966$ref=$ref->{$dir};2967}2968# we ran out of trie2969# (shouldn't happen: it's either no match, or end marker)2970push@filtered,$pr;2971}29722973return@filtered;2974}29752976# note: fill_project_list_info must be run first,2977# for 'descr_long' and 'ctags' to be filled2978sub search_projects_list {2979my($projlist,%opts) =@_;2980my$tagfilter=$opts{'tagfilter'};2981my$searchtext=$opts{'searchtext'};29822983return@$projlist2984unless($tagfilter||$searchtext);29852986my@projects;2987 PROJECT:2988foreachmy$pr(@$projlist) {29892990if($tagfilter) {2991next unlessref($pr->{'ctags'})eq'HASH';2992next unless2993grep{lc($_)eq lc($tagfilter) }keys%{$pr->{'ctags'}};2994}29952996if($searchtext) {2997next unless2998$pr->{'path'} =~/$searchtext/||2999$pr->{'descr_long'} =~/$searchtext/;3000}30013002push@projects,$pr;3003}30043005return@projects;3006}30073008our$gitweb_project_owner=undef;3009sub git_get_project_list_from_file {30103011return if(defined$gitweb_project_owner);30123013$gitweb_project_owner= {};3014# read from file (url-encoded):3015# 'git%2Fgit.git Linus+Torvalds'3016# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'3017# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'3018if(-f $projects_list) {3019open(my$fd,'<',$projects_list);3020while(my$line= <$fd>) {3021chomp$line;3022my($pr,$ow) =split' ',$line;3023$pr= unescape($pr);3024$ow= unescape($ow);3025$gitweb_project_owner->{$pr} = to_utf8($ow);3026}3027close$fd;3028}3029}30303031sub git_get_project_owner {3032my$project=shift;3033my$owner;30343035returnundefunless$project;3036$git_dir="$projectroot/$project";30373038if(!defined$gitweb_project_owner) {3039 git_get_project_list_from_file();3040}30413042if(exists$gitweb_project_owner->{$project}) {3043$owner=$gitweb_project_owner->{$project};3044}3045if(!defined$owner){3046$owner= git_get_project_config('owner');3047}3048if(!defined$owner) {3049$owner= get_file_owner("$git_dir");3050}30513052return$owner;3053}30543055sub git_get_last_activity {3056my($path) =@_;3057my$fd;30583059$git_dir="$projectroot/$path";3060open($fd,"-|", git_cmd(),'for-each-ref',3061'--format=%(committer)',3062'--sort=-committerdate',3063'--count=1',3064'refs/heads')orreturn;3065my$most_recent= <$fd>;3066close$fdorreturn;3067if(defined$most_recent&&3068$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {3069my$timestamp=$1;3070my$age=time-$timestamp;3071return($age, age_string($age));3072}3073return(undef,undef);3074}30753076# Implementation note: when a single remote is wanted, we cannot use 'git3077# remote show -n' because that command always work (assuming it's a remote URL3078# if it's not defined), and we cannot use 'git remote show' because that would3079# try to make a network roundtrip. So the only way to find if that particular3080# remote is defined is to walk the list provided by 'git remote -v' and stop if3081# and when we find what we want.3082sub git_get_remotes_list {3083my$wanted=shift;3084my%remotes= ();30853086open my$fd,'-|', git_cmd(),'remote','-v';3087return unless$fd;3088while(my$remote= <$fd>) {3089chomp$remote;3090$remote=~s!\t(.*?)\s+\((\w+)\)$!!;3091next if$wantedand not$remoteeq$wanted;3092my($url,$key) = ($1,$2);30933094$remotes{$remote} ||= {'heads'=> () };3095$remotes{$remote}{$key} =$url;3096}3097close$fdorreturn;3098returnwantarray?%remotes: \%remotes;3099}31003101# Takes a hash of remotes as first parameter and fills it by adding the3102# available remote heads for each of the indicated remotes.3103sub fill_remote_heads {3104my$remotes=shift;3105my@heads=map{"remotes/$_"}keys%$remotes;3106my@remoteheads= git_get_heads_list(undef,@heads);3107foreachmy$remote(keys%$remotes) {3108$remotes->{$remote}{'heads'} = [grep{3109$_->{'name'} =~s!^$remote/!!3110}@remoteheads];3111}3112}31133114sub git_get_references {3115my$type=shift||"";3116my%refs;3117# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.113118# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}3119open my$fd,"-|", git_cmd(),"show-ref","--dereference",3120($type? ("--","refs/$type") : ())# use -- <pattern> if $type3121orreturn;31223123while(my$line= <$fd>) {3124chomp$line;3125if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {3126if(defined$refs{$1}) {3127push@{$refs{$1}},$2;3128}else{3129$refs{$1} = [$2];3130}3131}3132}3133close$fdorreturn;3134return \%refs;3135}31363137sub git_get_rev_name_tags {3138my$hash=shift||returnundef;31393140open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash3141orreturn;3142my$name_rev= <$fd>;3143close$fd;31443145if($name_rev=~ m|^$hash tags/(.*)$|) {3146return$1;3147}else{3148# catches also '$hash undefined' output3149returnundef;3150}3151}31523153## ----------------------------------------------------------------------3154## parse to hash functions31553156sub parse_date {3157my$epoch=shift;3158my$tz=shift||"-0000";31593160my%date;3161my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");3162my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");3163my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);3164$date{'hour'} =$hour;3165$date{'minute'} =$min;3166$date{'mday'} =$mday;3167$date{'day'} =$days[$wday];3168$date{'month'} =$months[$mon];3169$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",3170$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;3171$date{'mday-time'} =sprintf"%d%s%02d:%02d",3172$mday,$months[$mon],$hour,$min;3173$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",31741900+$year,1+$mon,$mday,$hour,$min,$sec;31753176my($tz_sign,$tz_hour,$tz_min) =3177($tz=~m/^([-+])(\d\d)(\d\d)$/);3178$tz_sign= ($tz_signeq'-'? -1: +1);3179my$local=$epoch+$tz_sign*((($tz_hour*60) +$tz_min)*60);3180($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);3181$date{'hour_local'} =$hour;3182$date{'minute_local'} =$min;3183$date{'tz_local'} =$tz;3184$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",31851900+$year,$mon+1,$mday,3186$hour,$min,$sec,$tz);3187return%date;3188}31893190sub parse_tag {3191my$tag_id=shift;3192my%tag;3193my@comment;31943195open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;3196$tag{'id'} =$tag_id;3197while(my$line= <$fd>) {3198chomp$line;3199if($line=~m/^object ([0-9a-fA-F]{40})$/) {3200$tag{'object'} =$1;3201}elsif($line=~m/^type (.+)$/) {3202$tag{'type'} =$1;3203}elsif($line=~m/^tag (.+)$/) {3204$tag{'name'} =$1;3205}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {3206$tag{'author'} =$1;3207$tag{'author_epoch'} =$2;3208$tag{'author_tz'} =$3;3209if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {3210$tag{'author_name'} =$1;3211$tag{'author_email'} =$2;3212}else{3213$tag{'author_name'} =$tag{'author'};3214}3215}elsif($line=~m/--BEGIN/) {3216push@comment,$line;3217last;3218}elsif($lineeq"") {3219last;3220}3221}3222push@comment, <$fd>;3223$tag{'comment'} = \@comment;3224close$fdorreturn;3225if(!defined$tag{'name'}) {3226return3227};3228return%tag3229}32303231sub parse_commit_text {3232my($commit_text,$withparents) =@_;3233my@commit_lines=split'\n',$commit_text;3234my%co;32353236pop@commit_lines;# Remove '\0'32373238if(!@commit_lines) {3239return;3240}32413242my$header=shift@commit_lines;3243if($header!~m/^[0-9a-fA-F]{40}/) {3244return;3245}3246($co{'id'},my@parents) =split' ',$header;3247while(my$line=shift@commit_lines) {3248last if$lineeq"\n";3249if($line=~m/^tree ([0-9a-fA-F]{40})$/) {3250$co{'tree'} =$1;3251}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {3252push@parents,$1;3253}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {3254$co{'author'} = to_utf8($1);3255$co{'author_epoch'} =$2;3256$co{'author_tz'} =$3;3257if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {3258$co{'author_name'} =$1;3259$co{'author_email'} =$2;3260}else{3261$co{'author_name'} =$co{'author'};3262}3263}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {3264$co{'committer'} = to_utf8($1);3265$co{'committer_epoch'} =$2;3266$co{'committer_tz'} =$3;3267if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {3268$co{'committer_name'} =$1;3269$co{'committer_email'} =$2;3270}else{3271$co{'committer_name'} =$co{'committer'};3272}3273}3274}3275if(!defined$co{'tree'}) {3276return;3277};3278$co{'parents'} = \@parents;3279$co{'parent'} =$parents[0];32803281foreachmy$title(@commit_lines) {3282$title=~s/^ //;3283if($titlene"") {3284$co{'title'} = chop_str($title,80,5);3285# remove leading stuff of merges to make the interesting part visible3286if(length($title) >50) {3287$title=~s/^Automatic //;3288$title=~s/^merge (of|with) /Merge ... /i;3289if(length($title) >50) {3290$title=~s/(http|rsync):\/\///;3291}3292if(length($title) >50) {3293$title=~s/(master|www|rsync)\.//;3294}3295if(length($title) >50) {3296$title=~s/kernel.org:?//;3297}3298if(length($title) >50) {3299$title=~s/\/pub\/scm//;3300}3301}3302$co{'title_short'} = chop_str($title,50,5);3303last;3304}3305}3306if(!defined$co{'title'} ||$co{'title'}eq"") {3307$co{'title'} =$co{'title_short'} ='(no commit message)';3308}3309# remove added spaces3310foreachmy$line(@commit_lines) {3311$line=~s/^ //;3312}3313$co{'comment'} = \@commit_lines;33143315my$age=time-$co{'committer_epoch'};3316$co{'age'} =$age;3317$co{'age_string'} = age_string($age);3318my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});3319if($age>60*60*24*7*2) {3320$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3321$co{'age_string_age'} =$co{'age_string'};3322}else{3323$co{'age_string_date'} =$co{'age_string'};3324$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3325}3326return%co;3327}33283329sub parse_commit {3330my($commit_id) =@_;3331my%co;33323333local$/="\0";33343335open my$fd,"-|", git_cmd(),"rev-list",3336"--parents",3337"--header",3338"--max-count=1",3339$commit_id,3340"--",3341or die_error(500,"Open git-rev-list failed");3342%co= parse_commit_text(<$fd>,1);3343close$fd;33443345return%co;3346}33473348sub parse_commits {3349my($commit_id,$maxcount,$skip,$filename,@args) =@_;3350my@cos;33513352$maxcount||=1;3353$skip||=0;33543355local$/="\0";33563357open my$fd,"-|", git_cmd(),"rev-list",3358"--header",3359@args,3360("--max-count=".$maxcount),3361("--skip=".$skip),3362@extra_options,3363$commit_id,3364"--",3365($filename? ($filename) : ())3366or die_error(500,"Open git-rev-list failed");3367while(my$line= <$fd>) {3368my%co= parse_commit_text($line);3369push@cos, \%co;3370}3371close$fd;33723373returnwantarray?@cos: \@cos;3374}33753376# parse line of git-diff-tree "raw" output3377sub parse_difftree_raw_line {3378my$line=shift;3379my%res;33803381# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'3382# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'3383if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {3384$res{'from_mode'} =$1;3385$res{'to_mode'} =$2;3386$res{'from_id'} =$3;3387$res{'to_id'} =$4;3388$res{'status'} =$5;3389$res{'similarity'} =$6;3390if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3391($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3392}else{3393$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3394}3395}3396# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3397# combined diff (for merge commit)3398elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3399$res{'nparents'} =length($1);3400$res{'from_mode'} = [split(' ',$2) ];3401$res{'to_mode'} =pop@{$res{'from_mode'}};3402$res{'from_id'} = [split(' ',$3) ];3403$res{'to_id'} =pop@{$res{'from_id'}};3404$res{'status'} = [split('',$4) ];3405$res{'to_file'} = unquote($5);3406}3407# 'c512b523472485aef4fff9e57b229d9d243c967f'3408elsif($line=~m/^([0-9a-fA-F]{40})$/) {3409$res{'commit'} =$1;3410}34113412returnwantarray?%res: \%res;3413}34143415# wrapper: return parsed line of git-diff-tree "raw" output3416# (the argument might be raw line, or parsed info)3417sub parsed_difftree_line {3418my$line_or_ref=shift;34193420if(ref($line_or_ref)eq"HASH") {3421# pre-parsed (or generated by hand)3422return$line_or_ref;3423}else{3424return parse_difftree_raw_line($line_or_ref);3425}3426}34273428# parse line of git-ls-tree output3429sub parse_ls_tree_line {3430my$line=shift;3431my%opts=@_;3432my%res;34333434if($opts{'-l'}) {3435#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3436$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;34373438$res{'mode'} =$1;3439$res{'type'} =$2;3440$res{'hash'} =$3;3441$res{'size'} =$4;3442if($opts{'-z'}) {3443$res{'name'} =$5;3444}else{3445$res{'name'} = unquote($5);3446}3447}else{3448#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3449$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;34503451$res{'mode'} =$1;3452$res{'type'} =$2;3453$res{'hash'} =$3;3454if($opts{'-z'}) {3455$res{'name'} =$4;3456}else{3457$res{'name'} = unquote($4);3458}3459}34603461returnwantarray?%res: \%res;3462}34633464# generates _two_ hashes, references to which are passed as 2 and 3 argument3465sub parse_from_to_diffinfo {3466my($diffinfo,$from,$to,@parents) =@_;34673468if($diffinfo->{'nparents'}) {3469# combined diff3470$from->{'file'} = [];3471$from->{'href'} = [];3472 fill_from_file_info($diffinfo,@parents)3473unlessexists$diffinfo->{'from_file'};3474for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3475$from->{'file'}[$i] =3476defined$diffinfo->{'from_file'}[$i] ?3477$diffinfo->{'from_file'}[$i] :3478$diffinfo->{'to_file'};3479if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3480$from->{'href'}[$i] = href(action=>"blob",3481 hash_base=>$parents[$i],3482 hash=>$diffinfo->{'from_id'}[$i],3483 file_name=>$from->{'file'}[$i]);3484}else{3485$from->{'href'}[$i] =undef;3486}3487}3488}else{3489# ordinary (not combined) diff3490$from->{'file'} =$diffinfo->{'from_file'};3491if($diffinfo->{'status'}ne"A") {# not new (added) file3492$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3493 hash=>$diffinfo->{'from_id'},3494 file_name=>$from->{'file'});3495}else{3496delete$from->{'href'};3497}3498}34993500$to->{'file'} =$diffinfo->{'to_file'};3501if(!is_deleted($diffinfo)) {# file exists in result3502$to->{'href'} = href(action=>"blob", hash_base=>$hash,3503 hash=>$diffinfo->{'to_id'},3504 file_name=>$to->{'file'});3505}else{3506delete$to->{'href'};3507}3508}35093510## ......................................................................3511## parse to array of hashes functions35123513sub git_get_heads_list {3514my($limit,@classes) =@_;3515@classes= ('heads')unless@classes;3516my@patterns=map{"refs/$_"}@classes;3517my@headslist;35183519open my$fd,'-|', git_cmd(),'for-each-ref',3520($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3521'--format=%(objectname) %(refname) %(subject)%00%(committer)',3522@patterns3523orreturn;3524while(my$line= <$fd>) {3525my%ref_item;35263527chomp$line;3528my($refinfo,$committerinfo) =split(/\0/,$line);3529my($hash,$name,$title) =split(' ',$refinfo,3);3530my($committer,$epoch,$tz) =3531($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3532$ref_item{'fullname'} =$name;3533$name=~s!^refs/(?:head|remote)s/!!;35343535$ref_item{'name'} =$name;3536$ref_item{'id'} =$hash;3537$ref_item{'title'} =$title||'(no commit message)';3538$ref_item{'epoch'} =$epoch;3539if($epoch) {3540$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3541}else{3542$ref_item{'age'} ="unknown";3543}35443545push@headslist, \%ref_item;3546}3547close$fd;35483549returnwantarray?@headslist: \@headslist;3550}35513552sub git_get_tags_list {3553my$limit=shift;3554my@tagslist;35553556open my$fd,'-|', git_cmd(),'for-each-ref',3557($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3558'--format=%(objectname) %(objecttype) %(refname) '.3559'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3560'refs/tags'3561orreturn;3562while(my$line= <$fd>) {3563my%ref_item;35643565chomp$line;3566my($refinfo,$creatorinfo) =split(/\0/,$line);3567my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3568my($creator,$epoch,$tz) =3569($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3570$ref_item{'fullname'} =$name;3571$name=~s!^refs/tags/!!;35723573$ref_item{'type'} =$type;3574$ref_item{'id'} =$id;3575$ref_item{'name'} =$name;3576if($typeeq"tag") {3577$ref_item{'subject'} =$title;3578$ref_item{'reftype'} =$reftype;3579$ref_item{'refid'} =$refid;3580}else{3581$ref_item{'reftype'} =$type;3582$ref_item{'refid'} =$id;3583}35843585if($typeeq"tag"||$typeeq"commit") {3586$ref_item{'epoch'} =$epoch;3587if($epoch) {3588$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3589}else{3590$ref_item{'age'} ="unknown";3591}3592}35933594push@tagslist, \%ref_item;3595}3596close$fd;35973598returnwantarray?@tagslist: \@tagslist;3599}36003601## ----------------------------------------------------------------------3602## filesystem-related functions36033604sub get_file_owner {3605my$path=shift;36063607my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3608my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3609if(!defined$gcos) {3610returnundef;3611}3612my$owner=$gcos;3613$owner=~s/[,;].*$//;3614return to_utf8($owner);3615}36163617# assume that file exists3618sub insert_file {3619my$filename=shift;36203621open my$fd,'<',$filename;3622print map{ to_utf8($_) } <$fd>;3623close$fd;3624}36253626## ......................................................................3627## mimetype related functions36283629sub mimetype_guess_file {3630my$filename=shift;3631my$mimemap=shift;3632-r $mimemaporreturnundef;36333634my%mimemap;3635open(my$mh,'<',$mimemap)orreturnundef;3636while(<$mh>) {3637next ifm/^#/;# skip comments3638my($mimetype,@exts) =split(/\s+/);3639foreachmy$ext(@exts) {3640$mimemap{$ext} =$mimetype;3641}3642}3643close($mh);36443645$filename=~/\.([^.]*)$/;3646return$mimemap{$1};3647}36483649sub mimetype_guess {3650my$filename=shift;3651my$mime;3652$filename=~/\./orreturnundef;36533654if($mimetypes_file) {3655my$file=$mimetypes_file;3656if($file!~m!^/!) {# if it is relative path3657# it is relative to project3658$file="$projectroot/$project/$file";3659}3660$mime= mimetype_guess_file($filename,$file);3661}3662$mime||= mimetype_guess_file($filename,'/etc/mime.types');3663return$mime;3664}36653666sub blob_mimetype {3667my$fd=shift;3668my$filename=shift;36693670if($filename) {3671my$mime= mimetype_guess($filename);3672$mimeandreturn$mime;3673}36743675# just in case3676return$default_blob_plain_mimetypeunless$fd;36773678if(-T $fd) {3679return'text/plain';3680}elsif(!$filename) {3681return'application/octet-stream';3682}elsif($filename=~m/\.png$/i) {3683return'image/png';3684}elsif($filename=~m/\.gif$/i) {3685return'image/gif';3686}elsif($filename=~m/\.jpe?g$/i) {3687return'image/jpeg';3688}else{3689return'application/octet-stream';3690}3691}36923693sub blob_contenttype {3694my($fd,$file_name,$type) =@_;36953696$type||= blob_mimetype($fd,$file_name);3697if($typeeq'text/plain'&&defined$default_text_plain_charset) {3698$type.="; charset=$default_text_plain_charset";3699}37003701return$type;3702}37033704# guess file syntax for syntax highlighting; return undef if no highlighting3705# the name of syntax can (in the future) depend on syntax highlighter used3706sub guess_file_syntax {3707my($highlight,$mimetype,$file_name) =@_;3708returnundefunless($highlight&&defined$file_name);3709my$basename= basename($file_name,'.in');3710return$highlight_basename{$basename}3711ifexists$highlight_basename{$basename};37123713$basename=~/\.([^.]*)$/;3714my$ext=$1orreturnundef;3715return$highlight_ext{$ext}3716ifexists$highlight_ext{$ext};37173718returnundef;3719}37203721# run highlighter and return FD of its output,3722# or return original FD if no highlighting3723sub run_highlighter {3724my($fd,$highlight,$syntax) =@_;3725return$fdunless($highlight&&defined$syntax);37263727close$fd;3728open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3729 quote_command($highlight_bin).3730" --replace-tabs=8 --fragment --syntax$syntax|"3731or die_error(500,"Couldn't open file or run syntax highlighter");3732return$fd;3733}37343735## ======================================================================3736## functions printing HTML: header, footer, error page37373738sub get_page_title {3739my$title= to_utf8($site_name);37403741return$titleunless(defined$project);3742$title.=" - ". to_utf8($project);37433744return$titleunless(defined$action);3745$title.="/$action";# $action is US-ASCII (7bit ASCII)37463747return$titleunless(defined$file_name);3748$title.=" - ". esc_path($file_name);3749if($actioneq"tree"&&$file_name!~ m|/$|) {3750$title.="/";3751}37523753return$title;3754}37553756sub get_content_type_html {3757# require explicit support from the UA if we are to send the page as3758# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3759# we have to do this because MSIE sometimes globs '*/*', pretending to3760# support xhtml+xml but choking when it gets what it asked for.3761if(defined$cgi->http('HTTP_ACCEPT') &&3762$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3763$cgi->Accept('application/xhtml+xml') !=0) {3764return'application/xhtml+xml';3765}else{3766return'text/html';3767}3768}37693770sub print_feed_meta {3771if(defined$project) {3772my%href_params= get_feed_info();3773if(!exists$href_params{'-title'}) {3774$href_params{'-title'} ='log';3775}37763777foreachmy$format(qw(RSS Atom)) {3778my$type=lc($format);3779my%link_attr= (3780'-rel'=>'alternate',3781'-title'=> esc_attr("$project-$href_params{'-title'} -$formatfeed"),3782'-type'=>"application/$type+xml"3783);37843785$href_params{'action'} =$type;3786$link_attr{'-href'} = href(%href_params);3787print"<link ".3788"rel=\"$link_attr{'-rel'}\"".3789"title=\"$link_attr{'-title'}\"".3790"href=\"$link_attr{'-href'}\"".3791"type=\"$link_attr{'-type'}\"".3792"/>\n";37933794$href_params{'extra_options'} ='--no-merges';3795$link_attr{'-href'} = href(%href_params);3796$link_attr{'-title'} .=' (no merges)';3797print"<link ".3798"rel=\"$link_attr{'-rel'}\"".3799"title=\"$link_attr{'-title'}\"".3800"href=\"$link_attr{'-href'}\"".3801"type=\"$link_attr{'-type'}\"".3802"/>\n";3803}38043805}else{3806printf('<link rel="alternate" title="%sprojects list" '.3807'href="%s" type="text/plain; charset=utf-8" />'."\n",3808 esc_attr($site_name), href(project=>undef, action=>"project_index"));3809printf('<link rel="alternate" title="%sprojects feeds" '.3810'href="%s" type="text/x-opml" />'."\n",3811 esc_attr($site_name), href(project=>undef, action=>"opml"));3812}3813}38143815sub print_header_links {3816my$status=shift;38173818# print out each stylesheet that exist, providing backwards capability3819# for those people who defined $stylesheet in a config file3820if(defined$stylesheet) {3821print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3822}else{3823foreachmy$stylesheet(@stylesheets) {3824next unless$stylesheet;3825print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3826}3827}3828 print_feed_meta()3829if($statuseq'200 OK');3830if(defined$favicon) {3831printqq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);3832}3833}38343835sub print_nav_breadcrumbs {3836my%opts=@_;38373838print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3839if(defined$project) {3840print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3841if(defined$action) {3842my$action_print=$action;3843if(defined$opts{-action_extra}) {3844$action_print=$cgi->a({-href => href(action=>$action)},3845$action);3846}3847print" /$action_print";3848}3849if(defined$opts{-action_extra}) {3850print" /$opts{-action_extra}";3851}3852print"\n";3853}3854}38553856sub print_search_form {3857if(!defined$searchtext) {3858$searchtext="";3859}3860my$search_hash;3861if(defined$hash_base) {3862$search_hash=$hash_base;3863}elsif(defined$hash) {3864$search_hash=$hash;3865}else{3866$search_hash="HEAD";3867}3868my$action=$my_uri;3869my$use_pathinfo= gitweb_check_feature('pathinfo');3870if($use_pathinfo) {3871$action.="/".esc_url($project);3872}3873print$cgi->startform(-method=>"get", -action =>$action) .3874"<div class=\"search\">\n".3875(!$use_pathinfo&&3876$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3877$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3878$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3879$cgi->popup_menu(-name =>'st', -default=>'commit',3880-values=> ['commit','grep','author','committer','pickaxe']) .3881$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3882" search:\n",3883$cgi->textfield(-name =>"s", -value =>$searchtext, -override =>1) ."\n".3884"<span title=\"Extended regular expression\">".3885$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3886-checked =>$search_use_regexp) .3887"</span>".3888"</div>".3889$cgi->end_form() ."\n";3890}38913892sub git_header_html {3893my$status=shift||"200 OK";3894my$expires=shift;3895my%opts=@_;38963897my$title= get_page_title();3898my$content_type= get_content_type_html();3899print$cgi->header(-type=>$content_type, -charset =>'utf-8',3900-status=>$status, -expires =>$expires)3901unless($opts{'-no_http_header'});3902my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3903print<<EOF;3904<?xml version="1.0" encoding="utf-8"?>3905<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3906<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3907<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3908<!-- git core binaries version$git_version-->3909<head>3910<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3911<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3912<meta name="robots" content="index, nofollow"/>3913<title>$title</title>3914EOF3915# the stylesheet, favicon etc urls won't work correctly with path_info3916# unless we set the appropriate base URL3917if($ENV{'PATH_INFO'}) {3918print"<base href=\"".esc_url($base_url)."\"/>\n";3919}3920 print_header_links($status);39213922if(defined$site_html_head_string) {3923print to_utf8($site_html_head_string);3924}39253926print"</head>\n".3927"<body>\n";39283929if(defined$site_header&& -f $site_header) {3930 insert_file($site_header);3931}39323933print"<div class=\"page_header\">\n";3934if(defined$logo) {3935print$cgi->a({-href => esc_url($logo_url),3936-title =>$logo_label},3937$cgi->img({-src => esc_url($logo),3938-width =>72, -height =>27,3939-alt =>"git",3940-class=>"logo"}));3941}3942 print_nav_breadcrumbs(%opts);3943print"</div>\n";39443945my$have_search= gitweb_check_feature('search');3946if(defined$project&&$have_search) {3947 print_search_form();3948}3949}39503951sub git_footer_html {3952my$feed_class='rss_logo';39533954print"<div class=\"page_footer\">\n";3955if(defined$project) {3956my$descr= git_get_project_description($project);3957if(defined$descr) {3958print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3959}39603961my%href_params= get_feed_info();3962if(!%href_params) {3963$feed_class.=' generic';3964}3965$href_params{'-title'} ||='log';39663967foreachmy$format(qw(RSS Atom)) {3968$href_params{'action'} =lc($format);3969print$cgi->a({-href => href(%href_params),3970-title =>"$href_params{'-title'}$formatfeed",3971-class=>$feed_class},$format)."\n";3972}39733974}else{3975print$cgi->a({-href => href(project=>undef, action=>"opml"),3976-class=>$feed_class},"OPML") ." ";3977print$cgi->a({-href => href(project=>undef, action=>"project_index"),3978-class=>$feed_class},"TXT") ."\n";3979}3980print"</div>\n";# class="page_footer"39813982if(defined$t0&& gitweb_check_feature('timed')) {3983print"<div id=\"generating_info\">\n";3984print'This page took '.3985'<span id="generating_time" class="time_span">'.3986 tv_interval($t0, [ gettimeofday() ]).3987' seconds </span>'.3988' and '.3989'<span id="generating_cmd">'.3990$number_of_git_cmds.3991'</span> git commands '.3992" to generate.\n";3993print"</div>\n";# class="page_footer"3994}39953996if(defined$site_footer&& -f $site_footer) {3997 insert_file($site_footer);3998}39994000print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;4001if(defined$action&&4002$actioneq'blame_incremental') {4003print qq!<script type="text/javascript">\n!.4004 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.4005 qq!"!. href() .qq!");\n!.4006 qq!</script>\n!;4007}else{4008my($jstimezone,$tz_cookie,$datetime_class) =4009 gitweb_get_feature('javascript-timezone');40104011print qq!<script type="text/javascript">\n!.4012 qq!window.onload = function () {\n!;4013if(gitweb_check_feature('javascript-actions')) {4014print qq! fixLinks();\n!;4015}4016if($jstimezone&&$tz_cookie&&$datetime_class) {4017print qq! var tz_cookie = { name:'$tz_cookie', expires:14, path:'/'};\n!.# in days4018 qq! onloadTZSetup('$jstimezone', tz_cookie,'$datetime_class');\n!;4019}4020print qq!};\n!.4021 qq!</script>\n!;4022}40234024print"</body>\n".4025"</html>";4026}40274028# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])4029# Example: die_error(404, 'Hash not found')4030# By convention, use the following status codes (as defined in RFC 2616):4031# 400: Invalid or missing CGI parameters, or4032# requested object exists but has wrong type.4033# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on4034# this server or project.4035# 404: Requested object/revision/project doesn't exist.4036# 500: The server isn't configured properly, or4037# an internal error occurred (e.g. failed assertions caused by bugs), or4038# an unknown error occurred (e.g. the git binary died unexpectedly).4039# 503: The server is currently unavailable (because it is overloaded,4040# or down for maintenance). Generally, this is a temporary state.4041sub die_error {4042my$status=shift||500;4043my$error= esc_html(shift) ||"Internal Server Error";4044my$extra=shift;4045my%opts=@_;40464047my%http_responses= (4048400=>'400 Bad Request',4049403=>'403 Forbidden',4050404=>'404 Not Found',4051500=>'500 Internal Server Error',4052503=>'503 Service Unavailable',4053);4054 git_header_html($http_responses{$status},undef,%opts);4055print<<EOF;4056<div class="page_body">4057<br /><br />4058$status-$error4059<br />4060EOF4061if(defined$extra) {4062print"<hr />\n".4063"$extra\n";4064}4065print"</div>\n";40664067 git_footer_html();4068goto DONE_GITWEB4069unless($opts{'-error_handler'});4070}40714072## ----------------------------------------------------------------------4073## functions printing or outputting HTML: navigation40744075sub git_print_page_nav {4076my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;4077$extra=''if!defined$extra;# pager or formats40784079my@navs=qw(summary shortlog log commit commitdiff tree);4080if($suppress) {4081@navs=grep{$_ne$suppress}@navs;4082}40834084my%arg=map{$_=> {action=>$_} }@navs;4085if(defined$head) {4086for(qw(commit commitdiff)) {4087$arg{$_}{'hash'} =$head;4088}4089if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {4090for(qw(shortlog log)) {4091$arg{$_}{'hash'} =$head;4092}4093}4094}40954096$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;4097$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;40984099my@actions= gitweb_get_feature('actions');4100my%repl= (4101'%'=>'%',4102'n'=>$project,# project name4103'f'=>$git_dir,# project path within filesystem4104'h'=>$treehead||'',# current hash ('h' parameter)4105'b'=>$treebase||'',# hash base ('hb' parameter)4106);4107while(@actions) {4108my($label,$link,$pos) =splice(@actions,0,3);4109# insert4110@navs=map{$_eq$pos? ($_,$label) :$_}@navs;4111# munch munch4112$link=~s/%([%nfhb])/$repl{$1}/g;4113$arg{$label}{'_href'} =$link;4114}41154116print"<div class=\"page_nav\">\n".4117(join" | ",4118map{$_eq$current?4119$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")4120}@navs);4121print"<br/>\n$extra<br/>\n".4122"</div>\n";4123}41244125# returns a submenu for the nagivation of the refs views (tags, heads,4126# remotes) with the current view disabled and the remotes view only4127# available if the feature is enabled4128sub format_ref_views {4129my($current) =@_;4130my@ref_views=qw{tags heads};4131push@ref_views,'remotes'if gitweb_check_feature('remote_heads');4132returnjoin" | ",map{4133$_eq$current?$_:4134$cgi->a({-href => href(action=>$_)},$_)4135}@ref_views4136}41374138sub format_paging_nav {4139my($action,$page,$has_next_link) =@_;4140my$paging_nav;414141424143if($page>0) {4144$paging_nav.=4145$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .4146" ⋅ ".4147$cgi->a({-href => href(-replay=>1, page=>$page-1),4148-accesskey =>"p", -title =>"Alt-p"},"prev");4149}else{4150$paging_nav.="first ⋅ prev";4151}41524153if($has_next_link) {4154$paging_nav.=" ⋅ ".4155$cgi->a({-href => href(-replay=>1, page=>$page+1),4156-accesskey =>"n", -title =>"Alt-n"},"next");4157}else{4158$paging_nav.=" ⋅ next";4159}41604161return$paging_nav;4162}41634164## ......................................................................4165## functions printing or outputting HTML: div41664167sub git_print_header_div {4168my($action,$title,$hash,$hash_base) =@_;4169my%args= ();41704171$args{'action'} =$action;4172$args{'hash'} =$hashif$hash;4173$args{'hash_base'} =$hash_baseif$hash_base;41744175print"<div class=\"header\">\n".4176$cgi->a({-href => href(%args), -class=>"title"},4177$title?$title:$action) .4178"\n</div>\n";4179}41804181sub format_repo_url {4182my($name,$url) =@_;4183return"<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";4184}41854186# Group output by placing it in a DIV element and adding a header.4187# Options for start_div() can be provided by passing a hash reference as the4188# first parameter to the function.4189# Options to git_print_header_div() can be provided by passing an array4190# reference. This must follow the options to start_div if they are present.4191# The content can be a scalar, which is output as-is, a scalar reference, which4192# is output after html escaping, an IO handle passed either as *handle or4193# *handle{IO}, or a function reference. In the latter case all following4194# parameters will be taken as argument to the content function call.4195sub git_print_section {4196my($div_args,$header_args,$content);4197my$arg=shift;4198if(ref($arg)eq'HASH') {4199$div_args=$arg;4200$arg=shift;4201}4202if(ref($arg)eq'ARRAY') {4203$header_args=$arg;4204$arg=shift;4205}4206$content=$arg;42074208print$cgi->start_div($div_args);4209 git_print_header_div(@$header_args);42104211if(ref($content)eq'CODE') {4212$content->(@_);4213}elsif(ref($content)eq'SCALAR') {4214print esc_html($$content);4215}elsif(ref($content)eq'GLOB'or ref($content)eq'IO::Handle') {4216print<$content>;4217}elsif(!ref($content) &&defined($content)) {4218print$content;4219}42204221print$cgi->end_div;4222}42234224sub format_timestamp_html {4225my$date=shift;4226my$strtime=$date->{'rfc2822'};42274228my(undef,undef,$datetime_class) =4229 gitweb_get_feature('javascript-timezone');4230if($datetime_class) {4231$strtime= qq!<span class="$datetime_class">$strtime</span>!;4232}42334234my$localtime_format='(%02d:%02d%s)';4235if($date->{'hour_local'} <6) {4236$localtime_format='(<span class="atnight">%02d:%02d</span>%s)';4237}4238$strtime.=' '.4239sprintf($localtime_format,4240$date->{'hour_local'},$date->{'minute_local'},$date->{'tz_local'});42414242return$strtime;4243}42444245# Outputs the author name and date in long form4246sub git_print_authorship {4247my$co=shift;4248my%opts=@_;4249my$tag=$opts{-tag} ||'div';4250my$author=$co->{'author_name'};42514252my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});4253print"<$tagclass=\"author_date\">".4254 format_search_author($author,"author", esc_html($author)) .4255" [".format_timestamp_html(\%ad)."]".4256 git_get_avatar($co->{'author_email'}, -pad_before =>1) .4257"</$tag>\n";4258}42594260# Outputs table rows containing the full author or committer information,4261# in the format expected for 'commit' view (& similar).4262# Parameters are a commit hash reference, followed by the list of people4263# to output information for. If the list is empty it defaults to both4264# author and committer.4265sub git_print_authorship_rows {4266my$co=shift;4267# too bad we can't use @people = @_ || ('author', 'committer')4268my@people=@_;4269@people= ('author','committer')unless@people;4270foreachmy$who(@people) {4271my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});4272print"<tr><td>$who</td><td>".4273 format_search_author($co->{"${who}_name"},$who,4274 esc_html($co->{"${who}_name"})) ." ".4275 format_search_author($co->{"${who}_email"},$who,4276 esc_html("<".$co->{"${who}_email"} .">")) .4277"</td><td rowspan=\"2\">".4278 git_get_avatar($co->{"${who}_email"}, -size =>'double') .4279"</td></tr>\n".4280"<tr>".4281"<td></td><td>".4282 format_timestamp_html(\%wd) .4283"</td>".4284"</tr>\n";4285}4286}42874288sub git_print_page_path {4289my$name=shift;4290my$type=shift;4291my$hb=shift;429242934294print"<div class=\"page_path\">";4295print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),4296-title =>'tree root'}, to_utf8("[$project]"));4297print" / ";4298if(defined$name) {4299my@dirname=split'/',$name;4300my$basename=pop@dirname;4301my$fullname='';43024303foreachmy$dir(@dirname) {4304$fullname.= ($fullname?'/':'') .$dir;4305print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,4306 hash_base=>$hb),4307-title =>$fullname}, esc_path($dir));4308print" / ";4309}4310if(defined$type&&$typeeq'blob') {4311print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,4312 hash_base=>$hb),4313-title =>$name}, esc_path($basename));4314}elsif(defined$type&&$typeeq'tree') {4315print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,4316 hash_base=>$hb),4317-title =>$name}, esc_path($basename));4318print" / ";4319}else{4320print esc_path($basename);4321}4322}4323print"<br/></div>\n";4324}43254326sub git_print_log {4327my$log=shift;4328my%opts=@_;43294330if($opts{'-remove_title'}) {4331# remove title, i.e. first line of log4332shift@$log;4333}4334# remove leading empty lines4335while(defined$log->[0] &&$log->[0]eq"") {4336shift@$log;4337}43384339# print log4340my$signoff=0;4341my$empty=0;4342foreachmy$line(@$log) {4343if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {4344$signoff=1;4345$empty=0;4346if(!$opts{'-remove_signoff'}) {4347print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";4348next;4349}else{4350# remove signoff lines4351next;4352}4353}else{4354$signoff=0;4355}43564357# print only one empty line4358# do not print empty line after signoff4359if($lineeq"") {4360next if($empty||$signoff);4361$empty=1;4362}else{4363$empty=0;4364}43654366print format_log_line_html($line) ."<br/>\n";4367}43684369if($opts{'-final_empty_line'}) {4370# end with single empty line4371print"<br/>\n"unless$empty;4372}4373}43744375# return link target (what link points to)4376sub git_get_link_target {4377my$hash=shift;4378my$link_target;43794380# read link4381open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4382orreturn;4383{4384local$/=undef;4385$link_target= <$fd>;4386}4387close$fd4388orreturn;43894390return$link_target;4391}43924393# given link target, and the directory (basedir) the link is in,4394# return target of link relative to top directory (top tree);4395# return undef if it is not possible (including absolute links).4396sub normalize_link_target {4397my($link_target,$basedir) =@_;43984399# absolute symlinks (beginning with '/') cannot be normalized4400return if(substr($link_target,0,1)eq'/');44014402# normalize link target to path from top (root) tree (dir)4403my$path;4404if($basedir) {4405$path=$basedir.'/'.$link_target;4406}else{4407# we are in top (root) tree (dir)4408$path=$link_target;4409}44104411# remove //, /./, and /../4412my@path_parts;4413foreachmy$part(split('/',$path)) {4414# discard '.' and ''4415next if(!$part||$parteq'.');4416# handle '..'4417if($parteq'..') {4418if(@path_parts) {4419pop@path_parts;4420}else{4421# link leads outside repository (outside top dir)4422return;4423}4424}else{4425push@path_parts,$part;4426}4427}4428$path=join('/',@path_parts);44294430return$path;4431}44324433# print tree entry (row of git_tree), but without encompassing <tr> element4434sub git_print_tree_entry {4435my($t,$basedir,$hash_base,$have_blame) =@_;44364437my%base_key= ();4438$base_key{'hash_base'} =$hash_baseifdefined$hash_base;44394440# The format of a table row is: mode list link. Where mode is4441# the mode of the entry, list is the name of the entry, an href,4442# and link is the action links of the entry.44434444print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";4445if(exists$t->{'size'}) {4446print"<td class=\"size\">$t->{'size'}</td>\n";4447}4448if($t->{'type'}eq"blob") {4449print"<td class=\"list\">".4450$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4451 file_name=>"$basedir$t->{'name'}",%base_key),4452-class=>"list"}, esc_path($t->{'name'}));4453if(S_ISLNK(oct$t->{'mode'})) {4454my$link_target= git_get_link_target($t->{'hash'});4455if($link_target) {4456my$norm_target= normalize_link_target($link_target,$basedir);4457if(defined$norm_target) {4458print" -> ".4459$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,4460 file_name=>$norm_target),4461-title =>$norm_target}, esc_path($link_target));4462}else{4463print" -> ". esc_path($link_target);4464}4465}4466}4467print"</td>\n";4468print"<td class=\"link\">";4469print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4470 file_name=>"$basedir$t->{'name'}",%base_key)},4471"blob");4472if($have_blame) {4473print" | ".4474$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4475 file_name=>"$basedir$t->{'name'}",%base_key)},4476"blame");4477}4478if(defined$hash_base) {4479print" | ".4480$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4481 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4482"history");4483}4484print" | ".4485$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4486 file_name=>"$basedir$t->{'name'}")},4487"raw");4488print"</td>\n";44894490}elsif($t->{'type'}eq"tree") {4491print"<td class=\"list\">";4492print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4493 file_name=>"$basedir$t->{'name'}",4494%base_key)},4495 esc_path($t->{'name'}));4496print"</td>\n";4497print"<td class=\"link\">";4498print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4499 file_name=>"$basedir$t->{'name'}",4500%base_key)},4501"tree");4502if(defined$hash_base) {4503print" | ".4504$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4505 file_name=>"$basedir$t->{'name'}")},4506"history");4507}4508print"</td>\n";4509}else{4510# unknown object: we can only present history for it4511# (this includes 'commit' object, i.e. submodule support)4512print"<td class=\"list\">".4513 esc_path($t->{'name'}) .4514"</td>\n";4515print"<td class=\"link\">";4516if(defined$hash_base) {4517print$cgi->a({-href => href(action=>"history",4518 hash_base=>$hash_base,4519 file_name=>"$basedir$t->{'name'}")},4520"history");4521}4522print"</td>\n";4523}4524}45254526## ......................................................................4527## functions printing large fragments of HTML45284529# get pre-image filenames for merge (combined) diff4530sub fill_from_file_info {4531my($diff,@parents) =@_;45324533$diff->{'from_file'} = [ ];4534$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4535for(my$i=0;$i<$diff->{'nparents'};$i++) {4536if($diff->{'status'}[$i]eq'R'||4537$diff->{'status'}[$i]eq'C') {4538$diff->{'from_file'}[$i] =4539 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4540}4541}45424543return$diff;4544}45454546# is current raw difftree line of file deletion4547sub is_deleted {4548my$diffinfo=shift;45494550return$diffinfo->{'to_id'}eq('0' x 40);4551}45524553# does patch correspond to [previous] difftree raw line4554# $diffinfo - hashref of parsed raw diff format4555# $patchinfo - hashref of parsed patch diff format4556# (the same keys as in $diffinfo)4557sub is_patch_split {4558my($diffinfo,$patchinfo) =@_;45594560returndefined$diffinfo&&defined$patchinfo4561&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4562}456345644565sub git_difftree_body {4566my($difftree,$hash,@parents) =@_;4567my($parent) =$parents[0];4568my$have_blame= gitweb_check_feature('blame');4569print"<div class=\"list_head\">\n";4570if($#{$difftree} >10) {4571print(($#{$difftree} +1) ." files changed:\n");4572}4573print"</div>\n";45744575print"<table class=\"".4576(@parents>1?"combined ":"") .4577"diff_tree\">\n";45784579# header only for combined diff in 'commitdiff' view4580my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4581if($has_header) {4582# table header4583print"<thead><tr>\n".4584"<th></th><th></th>\n";# filename, patchN link4585for(my$i=0;$i<@parents;$i++) {4586my$par=$parents[$i];4587print"<th>".4588$cgi->a({-href => href(action=>"commitdiff",4589 hash=>$hash, hash_parent=>$par),4590-title =>'commitdiff to parent number '.4591($i+1) .': '.substr($par,0,7)},4592$i+1) .4593" </th>\n";4594}4595print"</tr></thead>\n<tbody>\n";4596}45974598my$alternate=1;4599my$patchno=0;4600foreachmy$line(@{$difftree}) {4601my$diff= parsed_difftree_line($line);46024603if($alternate) {4604print"<tr class=\"dark\">\n";4605}else{4606print"<tr class=\"light\">\n";4607}4608$alternate^=1;46094610if(exists$diff->{'nparents'}) {# combined diff46114612 fill_from_file_info($diff,@parents)4613unlessexists$diff->{'from_file'};46144615if(!is_deleted($diff)) {4616# file exists in the result (child) commit4617print"<td>".4618$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4619 file_name=>$diff->{'to_file'},4620 hash_base=>$hash),4621-class=>"list"}, esc_path($diff->{'to_file'})) .4622"</td>\n";4623}else{4624print"<td>".4625 esc_path($diff->{'to_file'}) .4626"</td>\n";4627}46284629if($actioneq'commitdiff') {4630# link to patch4631$patchno++;4632print"<td class=\"link\">".4633$cgi->a({-href => href(-anchor=>"patch$patchno")},4634"patch") .4635" | ".4636"</td>\n";4637}46384639my$has_history=0;4640my$not_deleted=0;4641for(my$i=0;$i<$diff->{'nparents'};$i++) {4642my$hash_parent=$parents[$i];4643my$from_hash=$diff->{'from_id'}[$i];4644my$from_path=$diff->{'from_file'}[$i];4645my$status=$diff->{'status'}[$i];46464647$has_history||= ($statusne'A');4648$not_deleted||= ($statusne'D');46494650if($statuseq'A') {4651print"<td class=\"link\"align=\"right\"> | </td>\n";4652}elsif($statuseq'D') {4653print"<td class=\"link\">".4654$cgi->a({-href => href(action=>"blob",4655 hash_base=>$hash,4656 hash=>$from_hash,4657 file_name=>$from_path)},4658"blob". ($i+1)) .4659" | </td>\n";4660}else{4661if($diff->{'to_id'}eq$from_hash) {4662print"<td class=\"link nochange\">";4663}else{4664print"<td class=\"link\">";4665}4666print$cgi->a({-href => href(action=>"blobdiff",4667 hash=>$diff->{'to_id'},4668 hash_parent=>$from_hash,4669 hash_base=>$hash,4670 hash_parent_base=>$hash_parent,4671 file_name=>$diff->{'to_file'},4672 file_parent=>$from_path)},4673"diff". ($i+1)) .4674" | </td>\n";4675}4676}46774678print"<td class=\"link\">";4679if($not_deleted) {4680print$cgi->a({-href => href(action=>"blob",4681 hash=>$diff->{'to_id'},4682 file_name=>$diff->{'to_file'},4683 hash_base=>$hash)},4684"blob");4685print" | "if($has_history);4686}4687if($has_history) {4688print$cgi->a({-href => href(action=>"history",4689 file_name=>$diff->{'to_file'},4690 hash_base=>$hash)},4691"history");4692}4693print"</td>\n";46944695print"</tr>\n";4696next;# instead of 'else' clause, to avoid extra indent4697}4698# else ordinary diff46994700my($to_mode_oct,$to_mode_str,$to_file_type);4701my($from_mode_oct,$from_mode_str,$from_file_type);4702if($diff->{'to_mode'}ne('0' x 6)) {4703$to_mode_oct=oct$diff->{'to_mode'};4704if(S_ISREG($to_mode_oct)) {# only for regular file4705$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4706}4707$to_file_type= file_type($diff->{'to_mode'});4708}4709if($diff->{'from_mode'}ne('0' x 6)) {4710$from_mode_oct=oct$diff->{'from_mode'};4711if(S_ISREG($from_mode_oct)) {# only for regular file4712$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4713}4714$from_file_type= file_type($diff->{'from_mode'});4715}47164717if($diff->{'status'}eq"A") {# created4718my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4719$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4720$mode_chng.="]</span>";4721print"<td>";4722print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4723 hash_base=>$hash, file_name=>$diff->{'file'}),4724-class=>"list"}, esc_path($diff->{'file'}));4725print"</td>\n";4726print"<td>$mode_chng</td>\n";4727print"<td class=\"link\">";4728if($actioneq'commitdiff') {4729# link to patch4730$patchno++;4731print$cgi->a({-href => href(-anchor=>"patch$patchno")},4732"patch") .4733" | ";4734}4735print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4736 hash_base=>$hash, file_name=>$diff->{'file'})},4737"blob");4738print"</td>\n";47394740}elsif($diff->{'status'}eq"D") {# deleted4741my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4742print"<td>";4743print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4744 hash_base=>$parent, file_name=>$diff->{'file'}),4745-class=>"list"}, esc_path($diff->{'file'}));4746print"</td>\n";4747print"<td>$mode_chng</td>\n";4748print"<td class=\"link\">";4749if($actioneq'commitdiff') {4750# link to patch4751$patchno++;4752print$cgi->a({-href => href(-anchor=>"patch$patchno")},4753"patch") .4754" | ";4755}4756print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4757 hash_base=>$parent, file_name=>$diff->{'file'})},4758"blob") ." | ";4759if($have_blame) {4760print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4761 file_name=>$diff->{'file'})},4762"blame") ." | ";4763}4764print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4765 file_name=>$diff->{'file'})},4766"history");4767print"</td>\n";47684769}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4770my$mode_chnge="";4771if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4772$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4773if($from_file_typene$to_file_type) {4774$mode_chnge.=" from$from_file_typeto$to_file_type";4775}4776if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4777if($from_mode_str&&$to_mode_str) {4778$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4779}elsif($to_mode_str) {4780$mode_chnge.=" mode:$to_mode_str";4781}4782}4783$mode_chnge.="]</span>\n";4784}4785print"<td>";4786print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4787 hash_base=>$hash, file_name=>$diff->{'file'}),4788-class=>"list"}, esc_path($diff->{'file'}));4789print"</td>\n";4790print"<td>$mode_chnge</td>\n";4791print"<td class=\"link\">";4792if($actioneq'commitdiff') {4793# link to patch4794$patchno++;4795print$cgi->a({-href => href(-anchor=>"patch$patchno")},4796"patch") .4797" | ";4798}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4799# "commit" view and modified file (not onlu mode changed)4800print$cgi->a({-href => href(action=>"blobdiff",4801 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4802 hash_base=>$hash, hash_parent_base=>$parent,4803 file_name=>$diff->{'file'})},4804"diff") .4805" | ";4806}4807print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4808 hash_base=>$hash, file_name=>$diff->{'file'})},4809"blob") ." | ";4810if($have_blame) {4811print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4812 file_name=>$diff->{'file'})},4813"blame") ." | ";4814}4815print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4816 file_name=>$diff->{'file'})},4817"history");4818print"</td>\n";48194820}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4821my%status_name= ('R'=>'moved','C'=>'copied');4822my$nstatus=$status_name{$diff->{'status'}};4823my$mode_chng="";4824if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4825# mode also for directories, so we cannot use $to_mode_str4826$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4827}4828print"<td>".4829$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4830 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4831-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4832"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4833$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4834 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4835-class=>"list"}, esc_path($diff->{'from_file'})) .4836" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4837"<td class=\"link\">";4838if($actioneq'commitdiff') {4839# link to patch4840$patchno++;4841print$cgi->a({-href => href(-anchor=>"patch$patchno")},4842"patch") .4843" | ";4844}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4845# "commit" view and modified file (not only pure rename or copy)4846print$cgi->a({-href => href(action=>"blobdiff",4847 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4848 hash_base=>$hash, hash_parent_base=>$parent,4849 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4850"diff") .4851" | ";4852}4853print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4854 hash_base=>$parent, file_name=>$diff->{'to_file'})},4855"blob") ." | ";4856if($have_blame) {4857print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4858 file_name=>$diff->{'to_file'})},4859"blame") ." | ";4860}4861print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4862 file_name=>$diff->{'to_file'})},4863"history");4864print"</td>\n";48654866}# we should not encounter Unmerged (U) or Unknown (X) status4867print"</tr>\n";4868}4869print"</tbody>"if$has_header;4870print"</table>\n";4871}48724873sub print_sidebyside_diff_chunk {4874my@chunk=@_;4875my(@ctx,@rem,@add);48764877return unless@chunk;48784879# incomplete last line might be among removed or added lines,4880# or both, or among context lines: find which4881for(my$i=1;$i<@chunk;$i++) {4882if($chunk[$i][0]eq'incomplete') {4883$chunk[$i][0] =$chunk[$i-1][0];4884}4885}48864887# guardian4888push@chunk, ["",""];48894890foreachmy$line_info(@chunk) {4891my($class,$line) =@$line_info;48924893# print chunk headers4894if($class&&$classeq'chunk_header') {4895print$line;4896next;4897}48984899## print from accumulator when type of class of lines change4900# empty contents block on start rem/add block, or end of chunk4901if(@ctx&& (!$class||$classeq'rem'||$classeq'add')) {4902print join'',4903'<div class="chunk_block ctx">',4904'<div class="old">',4905@ctx,4906'</div>',4907'<div class="new">',4908@ctx,4909'</div>',4910'</div>';4911@ctx= ();4912}4913# empty add/rem block on start context block, or end of chunk4914if((@rem||@add) && (!$class||$classeq'ctx')) {4915if(!@add) {4916# pure removal4917print join'',4918'<div class="chunk_block rem">',4919'<div class="old">',4920@rem,4921'</div>',4922'</div>';4923}elsif(!@rem) {4924# pure addition4925print join'',4926'<div class="chunk_block add">',4927'<div class="new">',4928@add,4929'</div>',4930'</div>';4931}else{4932# assume that it is change4933print join'',4934'<div class="chunk_block chg">',4935'<div class="old">',4936@rem,4937'</div>',4938'<div class="new">',4939@add,4940'</div>',4941'</div>';4942}4943@rem=@add= ();4944}49454946## adding lines to accumulator4947# guardian value4948last unless$line;4949# rem, add or change4950if($classeq'rem') {4951push@rem,$line;4952}elsif($classeq'add') {4953push@add,$line;4954}4955# context line4956if($classeq'ctx') {4957push@ctx,$line;4958}4959}4960}49614962sub git_patchset_body {4963my($fd,$diff_style,$difftree,$hash,@hash_parents) =@_;4964my($hash_parent) =$hash_parents[0];49654966my$is_combined= (@hash_parents>1);4967my$patch_idx=0;4968my$patch_number=0;4969my$patch_line;4970my$diffinfo;4971my$to_name;4972my(%from,%to);4973my@chunk;# for side-by-side diff49744975print"<div class=\"patchset\">\n";49764977# skip to first patch4978while($patch_line= <$fd>) {4979chomp$patch_line;49804981last if($patch_line=~m/^diff /);4982}49834984 PATCH:4985while($patch_line) {49864987# parse "git diff" header line4988if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4989# $1 is from_name, which we do not use4990$to_name= unquote($2);4991$to_name=~s!^b/!!;4992}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4993# $1 is 'cc' or 'combined', which we do not use4994$to_name= unquote($2);4995}else{4996$to_name=undef;4997}49984999# check if current patch belong to current raw line5000# and parse raw git-diff line if needed5001if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {5002# this is continuation of a split patch5003print"<div class=\"patch cont\">\n";5004}else{5005# advance raw git-diff output if needed5006$patch_idx++ifdefined$diffinfo;50075008# read and prepare patch information5009$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);50105011# compact combined diff output can have some patches skipped5012# find which patch (using pathname of result) we are at now;5013if($is_combined) {5014while($to_namene$diffinfo->{'to_file'}) {5015print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".5016 format_diff_cc_simplified($diffinfo,@hash_parents) .5017"</div>\n";# class="patch"50185019$patch_idx++;5020$patch_number++;50215022last if$patch_idx>$#$difftree;5023$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);5024}5025}50265027# modifies %from, %to hashes5028 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);50295030# this is first patch for raw difftree line with $patch_idx index5031# we index @$difftree array from 0, but number patches from 15032print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";5033}50345035# git diff header5036#assert($patch_line =~ m/^diff /) if DEBUG;5037#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed5038$patch_number++;5039# print "git diff" header5040print format_git_diff_header_line($patch_line,$diffinfo,5041 \%from, \%to);50425043# print extended diff header5044print"<div class=\"diff extended_header\">\n";5045 EXTENDED_HEADER:5046while($patch_line= <$fd>) {5047chomp$patch_line;50485049last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);50505051print format_extended_diff_header_line($patch_line,$diffinfo,5052 \%from, \%to);5053}5054print"</div>\n";# class="diff extended_header"50555056# from-file/to-file diff header5057if(!$patch_line) {5058print"</div>\n";# class="patch"5059last PATCH;5060}5061next PATCH if($patch_line=~m/^diff /);5062#assert($patch_line =~ m/^---/) if DEBUG;50635064my$last_patch_line=$patch_line;5065$patch_line= <$fd>;5066chomp$patch_line;5067#assert($patch_line =~ m/^\+\+\+/) if DEBUG;50685069print format_diff_from_to_header($last_patch_line,$patch_line,5070$diffinfo, \%from, \%to,5071@hash_parents);50725073# the patch itself5074 LINE:5075while($patch_line= <$fd>) {5076chomp$patch_line;50775078next PATCH if($patch_line=~m/^diff /);50795080my($class,$line) = process_diff_line($patch_line, \%from, \%to);5081my$diff_classes="diff";5082$diff_classes.="$class"if($class);5083$line="<div class=\"$diff_classes\">$line</div>\n";50845085if($diff_styleeq'sidebyside'&& !$is_combined) {5086if($classeq'chunk_header') {5087 print_sidebyside_diff_chunk(@chunk);5088@chunk= ( [$class,$line] );5089}else{5090push@chunk, [$class,$line];5091}5092}else{5093# default 'inline' style and unknown styles5094print$line;5095}5096}50975098}continue{5099if(@chunk) {5100 print_sidebyside_diff_chunk(@chunk);5101@chunk= ();5102}5103print"</div>\n";# class="patch"5104}51055106# for compact combined (--cc) format, with chunk and patch simplification5107# the patchset might be empty, but there might be unprocessed raw lines5108for(++$patch_idxif$patch_number>0;5109$patch_idx<@$difftree;5110++$patch_idx) {5111# read and prepare patch information5112$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);51135114# generate anchor for "patch" links in difftree / whatchanged part5115print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".5116 format_diff_cc_simplified($diffinfo,@hash_parents) .5117"</div>\n";# class="patch"51185119$patch_number++;5120}51215122if($patch_number==0) {5123if(@hash_parents>1) {5124print"<div class=\"diff nodifferences\">Trivial merge</div>\n";5125}else{5126print"<div class=\"diff nodifferences\">No differences found</div>\n";5127}5128}51295130print"</div>\n";# class="patchset"5131}51325133# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51345135# fills project list info (age, description, owner, category, forks)5136# for each project in the list, removing invalid projects from5137# returned list5138# NOTE: modifies $projlist, but does not remove entries from it5139sub fill_project_list_info {5140my$projlist=shift;5141my@projects;51425143my$show_ctags= gitweb_check_feature('ctags');5144 PROJECT:5145foreachmy$pr(@$projlist) {5146my(@activity) = git_get_last_activity($pr->{'path'});5147unless(@activity) {5148next PROJECT;5149}5150($pr->{'age'},$pr->{'age_string'}) =@activity;5151if(!defined$pr->{'descr'}) {5152my$descr= git_get_project_description($pr->{'path'}) ||"";5153$descr= to_utf8($descr);5154$pr->{'descr_long'} =$descr;5155$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);5156}5157if(!defined$pr->{'owner'}) {5158$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";5159}5160if($show_ctags) {5161$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});5162}5163if($projects_list_group_categories&& !defined$pr->{'category'}) {5164my$cat= git_get_project_category($pr->{'path'}) ||5165$project_list_default_category;5166$pr->{'category'} = to_utf8($cat);5167}51685169push@projects,$pr;5170}51715172return@projects;5173}51745175sub sort_projects_list {5176my($projlist,$order) =@_;5177my@projects;51785179my%order_info= (5180 project => { key =>'path', type =>'str'},5181 descr => { key =>'descr_long', type =>'str'},5182 owner => { key =>'owner', type =>'str'},5183 age => { key =>'age', type =>'num'}5184);5185my$oi=$order_info{$order};5186return@$projlistunlessdefined$oi;5187if($oi->{'type'}eq'str') {5188@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@$projlist;5189}else{5190@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@$projlist;5191}51925193return@projects;5194}51955196# returns a hash of categories, containing the list of project5197# belonging to each category5198sub build_projlist_by_category {5199my($projlist,$from,$to) =@_;5200my%categories;52015202$from=0unlessdefined$from;5203$to=$#$projlistif(!defined$to||$#$projlist<$to);52045205for(my$i=$from;$i<=$to;$i++) {5206my$pr=$projlist->[$i];5207push@{$categories{$pr->{'category'} }},$pr;5208}52095210returnwantarray?%categories: \%categories;5211}52125213# print 'sort by' <th> element, generating 'sort by $name' replay link5214# if that order is not selected5215sub print_sort_th {5216print format_sort_th(@_);5217}52185219sub format_sort_th {5220my($name,$order,$header) =@_;5221my$sort_th="";5222$header||=ucfirst($name);52235224if($ordereq$name) {5225$sort_th.="<th>$header</th>\n";5226}else{5227$sort_th.="<th>".5228$cgi->a({-href => href(-replay=>1, order=>$name),5229-class=>"header"},$header) .5230"</th>\n";5231}52325233return$sort_th;5234}52355236sub git_project_list_rows {5237my($projlist,$from,$to,$check_forks) =@_;52385239$from=0unlessdefined$from;5240$to=$#$projlistif(!defined$to||$#$projlist<$to);52415242my$alternate=1;5243for(my$i=$from;$i<=$to;$i++) {5244my$pr=$projlist->[$i];52455246if($alternate) {5247print"<tr class=\"dark\">\n";5248}else{5249print"<tr class=\"light\">\n";5250}5251$alternate^=1;52525253if($check_forks) {5254print"<td>";5255if($pr->{'forks'}) {5256my$nforks=scalar@{$pr->{'forks'}};5257if($nforks>0) {5258print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),5259-title =>"$nforksforks"},"+");5260}else{5261print$cgi->span({-title =>"$nforksforks"},"+");5262}5263}5264print"</td>\n";5265}5266print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5267-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".5268"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5269-class=>"list", -title =>$pr->{'descr_long'}},5270 esc_html($pr->{'descr'})) ."</td>\n".5271"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";5272print"<td class=\"". age_class($pr->{'age'}) ."\">".5273(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".5274"<td class=\"link\">".5275$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".5276$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".5277$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".5278$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .5279($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .5280"</td>\n".5281"</tr>\n";5282}5283}52845285sub git_project_list_body {5286# actually uses global variable $project5287my($projlist,$order,$from,$to,$extra,$no_header) =@_;5288my@projects=@$projlist;52895290my$check_forks= gitweb_check_feature('forks');5291my$show_ctags= gitweb_check_feature('ctags');5292my$tagfilter=$show_ctags?$input_params{'ctag'} :undef;5293$check_forks=undef5294if($tagfilter||$searchtext);52955296# filtering out forks before filling info allows to do less work5297@projects= filter_forks_from_projects_list(\@projects)5298if($check_forks);5299@projects= fill_project_list_info(\@projects);5300# searching projects require filling to be run before it5301@projects= search_projects_list(\@projects,5302'searchtext'=>$searchtext,5303'tagfilter'=>$tagfilter)5304if($tagfilter||$searchtext);53055306$order||=$default_projects_order;5307$from=0unlessdefined$from;5308$to=$#projectsif(!defined$to||$#projects<$to);53095310# short circuit5311if($from>$to) {5312print"<center>\n".5313"<b>No such projects found</b><br />\n".5314"Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".5315"</center>\n<br />\n";5316return;5317}53185319@projects= sort_projects_list(\@projects,$order);53205321if($show_ctags) {5322my$ctags= git_gather_all_ctags(\@projects);5323my$cloud= git_populate_project_tagcloud($ctags);5324print git_show_project_tagcloud($cloud,64);5325}53265327print"<table class=\"project_list\">\n";5328unless($no_header) {5329print"<tr>\n";5330if($check_forks) {5331print"<th></th>\n";5332}5333 print_sort_th('project',$order,'Project');5334 print_sort_th('descr',$order,'Description');5335 print_sort_th('owner',$order,'Owner');5336 print_sort_th('age',$order,'Last Change');5337print"<th></th>\n".# for links5338"</tr>\n";5339}53405341if($projects_list_group_categories) {5342# only display categories with projects in the $from-$to window5343@projects=sort{$a->{'category'}cmp$b->{'category'}}@projects[$from..$to];5344my%categories= build_projlist_by_category(\@projects,$from,$to);5345foreachmy$cat(sort keys%categories) {5346unless($cateq"") {5347print"<tr>\n";5348if($check_forks) {5349print"<td></td>\n";5350}5351print"<td class=\"category\"colspan=\"5\">".esc_html($cat)."</td>\n";5352print"</tr>\n";5353}53545355 git_project_list_rows($categories{$cat},undef,undef,$check_forks);5356}5357}else{5358 git_project_list_rows(\@projects,$from,$to,$check_forks);5359}53605361if(defined$extra) {5362print"<tr>\n";5363if($check_forks) {5364print"<td></td>\n";5365}5366print"<td colspan=\"5\">$extra</td>\n".5367"</tr>\n";5368}5369print"</table>\n";5370}53715372sub git_log_body {5373# uses global variable $project5374my($commitlist,$from,$to,$refs,$extra) =@_;53755376$from=0unlessdefined$from;5377$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);53785379for(my$i=0;$i<=$to;$i++) {5380my%co= %{$commitlist->[$i]};5381next if!%co;5382my$commit=$co{'id'};5383my$ref= format_ref_marker($refs,$commit);5384 git_print_header_div('commit',5385"<span class=\"age\">$co{'age_string'}</span>".5386 esc_html($co{'title'}) .$ref,5387$commit);5388print"<div class=\"title_text\">\n".5389"<div class=\"log_link\">\n".5390$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5391" | ".5392$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5393" | ".5394$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5395"<br/>\n".5396"</div>\n";5397 git_print_authorship(\%co, -tag =>'span');5398print"<br/>\n</div>\n";53995400print"<div class=\"log_body\">\n";5401 git_print_log($co{'comment'}, -final_empty_line=>1);5402print"</div>\n";5403}5404if($extra) {5405print"<div class=\"page_nav\">\n";5406print"$extra\n";5407print"</div>\n";5408}5409}54105411sub git_shortlog_body {5412# uses global variable $project5413my($commitlist,$from,$to,$refs,$extra) =@_;54145415$from=0unlessdefined$from;5416$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);54175418print"<table class=\"shortlog\">\n";5419my$alternate=1;5420for(my$i=$from;$i<=$to;$i++) {5421my%co= %{$commitlist->[$i]};5422my$commit=$co{'id'};5423my$ref= format_ref_marker($refs,$commit);5424if($alternate) {5425print"<tr class=\"dark\">\n";5426}else{5427print"<tr class=\"light\">\n";5428}5429$alternate^=1;5430# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .5431print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5432 format_author_html('td', \%co,10) ."<td>";5433print format_subject_html($co{'title'},$co{'title_short'},5434 href(action=>"commit", hash=>$commit),$ref);5435print"</td>\n".5436"<td class=\"link\">".5437$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".5438$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".5439$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");5440my$snapshot_links= format_snapshot_links($commit);5441if(defined$snapshot_links) {5442print" | ".$snapshot_links;5443}5444print"</td>\n".5445"</tr>\n";5446}5447if(defined$extra) {5448print"<tr>\n".5449"<td colspan=\"4\">$extra</td>\n".5450"</tr>\n";5451}5452print"</table>\n";5453}54545455sub git_history_body {5456# Warning: assumes constant type (blob or tree) during history5457my($commitlist,$from,$to,$refs,$extra,5458$file_name,$file_hash,$ftype) =@_;54595460$from=0unlessdefined$from;5461$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});54625463print"<table class=\"history\">\n";5464my$alternate=1;5465for(my$i=$from;$i<=$to;$i++) {5466my%co= %{$commitlist->[$i]};5467if(!%co) {5468next;5469}5470my$commit=$co{'id'};54715472my$ref= format_ref_marker($refs,$commit);54735474if($alternate) {5475print"<tr class=\"dark\">\n";5476}else{5477print"<tr class=\"light\">\n";5478}5479$alternate^=1;5480print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5481# shortlog: format_author_html('td', \%co, 10)5482 format_author_html('td', \%co,15,3) ."<td>";5483# originally git_history used chop_str($co{'title'}, 50)5484print format_subject_html($co{'title'},$co{'title_short'},5485 href(action=>"commit", hash=>$commit),$ref);5486print"</td>\n".5487"<td class=\"link\">".5488$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".5489$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");54905491if($ftypeeq'blob') {5492my$blob_current=$file_hash;5493my$blob_parent= git_get_hash_by_path($commit,$file_name);5494if(defined$blob_current&&defined$blob_parent&&5495$blob_currentne$blob_parent) {5496print" | ".5497$cgi->a({-href => href(action=>"blobdiff",5498 hash=>$blob_current, hash_parent=>$blob_parent,5499 hash_base=>$hash_base, hash_parent_base=>$commit,5500 file_name=>$file_name)},5501"diff to current");5502}5503}5504print"</td>\n".5505"</tr>\n";5506}5507if(defined$extra) {5508print"<tr>\n".5509"<td colspan=\"4\">$extra</td>\n".5510"</tr>\n";5511}5512print"</table>\n";5513}55145515sub git_tags_body {5516# uses global variable $project5517my($taglist,$from,$to,$extra) =@_;5518$from=0unlessdefined$from;5519$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);55205521print"<table class=\"tags\">\n";5522my$alternate=1;5523for(my$i=$from;$i<=$to;$i++) {5524my$entry=$taglist->[$i];5525my%tag=%$entry;5526my$comment=$tag{'subject'};5527my$comment_short;5528if(defined$comment) {5529$comment_short= chop_str($comment,30,5);5530}5531if($alternate) {5532print"<tr class=\"dark\">\n";5533}else{5534print"<tr class=\"light\">\n";5535}5536$alternate^=1;5537if(defined$tag{'age'}) {5538print"<td><i>$tag{'age'}</i></td>\n";5539}else{5540print"<td></td>\n";5541}5542print"<td>".5543$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),5544-class=>"list name"}, esc_html($tag{'name'})) .5545"</td>\n".5546"<td>";5547if(defined$comment) {5548print format_subject_html($comment,$comment_short,5549 href(action=>"tag", hash=>$tag{'id'}));5550}5551print"</td>\n".5552"<td class=\"selflink\">";5553if($tag{'type'}eq"tag") {5554print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");5555}else{5556print" ";5557}5558print"</td>\n".5559"<td class=\"link\">"." | ".5560$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});5561if($tag{'reftype'}eq"commit") {5562print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .5563" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");5564}elsif($tag{'reftype'}eq"blob") {5565print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");5566}5567print"</td>\n".5568"</tr>";5569}5570if(defined$extra) {5571print"<tr>\n".5572"<td colspan=\"5\">$extra</td>\n".5573"</tr>\n";5574}5575print"</table>\n";5576}55775578sub git_heads_body {5579# uses global variable $project5580my($headlist,$head_at,$from,$to,$extra) =@_;5581$from=0unlessdefined$from;5582$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);55835584print"<table class=\"heads\">\n";5585my$alternate=1;5586for(my$i=$from;$i<=$to;$i++) {5587my$entry=$headlist->[$i];5588my%ref=%$entry;5589my$curr=defined$head_at&&$ref{'id'}eq$head_at;5590if($alternate) {5591print"<tr class=\"dark\">\n";5592}else{5593print"<tr class=\"light\">\n";5594}5595$alternate^=1;5596print"<td><i>$ref{'age'}</i></td>\n".5597($curr?"<td class=\"current_head\">":"<td>") .5598$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),5599-class=>"list name"},esc_html($ref{'name'})) .5600"</td>\n".5601"<td class=\"link\">".5602$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".5603$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".5604$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})},"tree") .5605"</td>\n".5606"</tr>";5607}5608if(defined$extra) {5609print"<tr>\n".5610"<td colspan=\"3\">$extra</td>\n".5611"</tr>\n";5612}5613print"</table>\n";5614}56155616# Display a single remote block5617sub git_remote_block {5618my($remote,$rdata,$limit,$head) =@_;56195620my$heads=$rdata->{'heads'};5621my$fetch=$rdata->{'fetch'};5622my$push=$rdata->{'push'};56235624my$urls_table="<table class=\"projects_list\">\n";56255626if(defined$fetch) {5627if($fetcheq$push) {5628$urls_table.= format_repo_url("URL",$fetch);5629}else{5630$urls_table.= format_repo_url("Fetch URL",$fetch);5631$urls_table.= format_repo_url("Push URL",$push)ifdefined$push;5632}5633}elsif(defined$push) {5634$urls_table.= format_repo_url("Push URL",$push);5635}else{5636$urls_table.= format_repo_url("","No remote URL");5637}56385639$urls_table.="</table>\n";56405641my$dots;5642if(defined$limit&&$limit<@$heads) {5643$dots=$cgi->a({-href => href(action=>"remotes", hash=>$remote)},"...");5644}56455646print$urls_table;5647 git_heads_body($heads,$head,0,$limit,$dots);5648}56495650# Display a list of remote names with the respective fetch and push URLs5651sub git_remotes_list {5652my($remotedata,$limit) =@_;5653print"<table class=\"heads\">\n";5654my$alternate=1;5655my@remotes=sort keys%$remotedata;56565657my$limited=$limit&&$limit<@remotes;56585659$#remotes=$limit-1if$limited;56605661while(my$remote=shift@remotes) {5662my$rdata=$remotedata->{$remote};5663my$fetch=$rdata->{'fetch'};5664my$push=$rdata->{'push'};5665if($alternate) {5666print"<tr class=\"dark\">\n";5667}else{5668print"<tr class=\"light\">\n";5669}5670$alternate^=1;5671print"<td>".5672$cgi->a({-href=> href(action=>'remotes', hash=>$remote),5673-class=>"list name"},esc_html($remote)) .5674"</td>";5675print"<td class=\"link\">".5676(defined$fetch?$cgi->a({-href=>$fetch},"fetch") :"fetch") .5677" | ".5678(defined$push?$cgi->a({-href=>$push},"push") :"push") .5679"</td>";56805681print"</tr>\n";5682}56835684if($limited) {5685print"<tr>\n".5686"<td colspan=\"3\">".5687$cgi->a({-href => href(action=>"remotes")},"...") .5688"</td>\n"."</tr>\n";5689}56905691print"</table>";5692}56935694# Display remote heads grouped by remote, unless there are too many5695# remotes, in which case we only display the remote names5696sub git_remotes_body {5697my($remotedata,$limit,$head) =@_;5698if($limitand$limit<keys%$remotedata) {5699 git_remotes_list($remotedata,$limit);5700}else{5701 fill_remote_heads($remotedata);5702while(my($remote,$rdata) =each%$remotedata) {5703 git_print_section({-class=>"remote", -id=>$remote},5704["remotes",$remote,$remote],sub{5705 git_remote_block($remote,$rdata,$limit,$head);5706});5707}5708}5709}57105711sub git_search_message {5712my%co=@_;57135714my$greptype;5715if($searchtypeeq'commit') {5716$greptype="--grep=";5717}elsif($searchtypeeq'author') {5718$greptype="--author=";5719}elsif($searchtypeeq'committer') {5720$greptype="--committer=";5721}5722$greptype.=$searchtext;5723my@commitlist= parse_commits($hash,101, (100*$page),undef,5724$greptype,'--regexp-ignore-case',5725$search_use_regexp?'--extended-regexp':'--fixed-strings');57265727my$paging_nav='';5728if($page>0) {5729$paging_nav.=5730$cgi->a({-href => href(-replay=>1, page=>undef)},5731"first") .5732" ⋅ ".5733$cgi->a({-href => href(-replay=>1, page=>$page-1),5734-accesskey =>"p", -title =>"Alt-p"},"prev");5735}else{5736$paging_nav.="first ⋅ prev";5737}5738my$next_link='';5739if($#commitlist>=100) {5740$next_link=5741$cgi->a({-href => href(-replay=>1, page=>$page+1),5742-accesskey =>"n", -title =>"Alt-n"},"next");5743$paging_nav.=" ⋅$next_link";5744}else{5745$paging_nav.=" ⋅ next";5746}57475748 git_header_html();57495750 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5751 git_print_header_div('commit', esc_html($co{'title'}),$hash);5752if($page==0&& !@commitlist) {5753print"<p>No match.</p>\n";5754}else{5755 git_search_grep_body(\@commitlist,0,99,$next_link);5756}57575758 git_footer_html();5759}57605761sub git_search_changes {5762my%co=@_;57635764local$/="\n";5765open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5766'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5767($search_use_regexp?'--pickaxe-regex': ())5768or die_error(500,"Open git-log failed");57695770 git_header_html();57715772 git_print_page_nav('','',$hash,$co{'tree'},$hash);5773 git_print_header_div('commit', esc_html($co{'title'}),$hash);57745775print"<table class=\"pickaxe search\">\n";5776my$alternate=1;5777undef%co;5778my@files;5779while(my$line= <$fd>) {5780chomp$line;5781next unless$line;57825783my%set= parse_difftree_raw_line($line);5784if(defined$set{'commit'}) {5785# finish previous commit5786if(%co) {5787print"</td>\n".5788"<td class=\"link\">".5789$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},5790"commit") .5791" | ".5792$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},5793 hash_base=>$co{'id'})},5794"tree") .5795"</td>\n".5796"</tr>\n";5797}57985799if($alternate) {5800print"<tr class=\"dark\">\n";5801}else{5802print"<tr class=\"light\">\n";5803}5804$alternate^=1;5805%co= parse_commit($set{'commit'});5806my$author= chop_and_escape_str($co{'author_name'},15,5);5807print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5808"<td><i>$author</i></td>\n".5809"<td>".5810$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5811-class=>"list subject"},5812 chop_and_escape_str($co{'title'},50) ."<br/>");5813}elsif(defined$set{'to_id'}) {5814next if($set{'to_id'} =~m/^0{40}$/);58155816print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5817 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5818-class=>"list"},5819"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5820"<br/>\n";5821}5822}5823close$fd;58245825# finish last commit (warning: repetition!)5826if(%co) {5827print"</td>\n".5828"<td class=\"link\">".5829$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},5830"commit") .5831" | ".5832$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'},5833 hash_base=>$co{'id'})},5834"tree") .5835"</td>\n".5836"</tr>\n";5837}58385839print"</table>\n";58405841 git_footer_html();5842}58435844sub git_search_files {5845my%co=@_;58465847local$/="\n";5848open my$fd,"-|", git_cmd(),'grep','-n','-z',5849$search_use_regexp? ('-E','-i') :'-F',5850$searchtext,$co{'tree'}5851or die_error(500,"Open git-grep failed");58525853 git_header_html();58545855 git_print_page_nav('','',$hash,$co{'tree'},$hash);5856 git_print_header_div('commit', esc_html($co{'title'}),$hash);58575858print"<table class=\"grep_search\">\n";5859my$alternate=1;5860my$matches=0;5861my$lastfile='';5862my$file_href;5863while(my$line= <$fd>) {5864chomp$line;5865my($file,$lno,$ltext,$binary);5866last if($matches++>1000);5867if($line=~/^Binary file (.+) matches$/) {5868$file=$1;5869$binary=1;5870}else{5871($file,$lno,$ltext) =split(/\0/,$line,3);5872$file=~s/^$co{'tree'}://;5873}5874if($filene$lastfile) {5875$lastfileand print"</td></tr>\n";5876if($alternate++) {5877print"<tr class=\"dark\">\n";5878}else{5879print"<tr class=\"light\">\n";5880}5881$file_href= href(action=>"blob", hash_base=>$co{'id'},5882 file_name=>$file);5883print"<td class=\"list\">".5884$cgi->a({-href =>$file_href, -class=>"list"}, esc_path($file));5885print"</td><td>\n";5886$lastfile=$file;5887}5888if($binary) {5889print"<div class=\"binary\">Binary file</div>\n";5890}else{5891$ltext= untabify($ltext);5892if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5893$ltext= esc_html($1, -nbsp=>1);5894$ltext.='<span class="match">';5895$ltext.= esc_html($2, -nbsp=>1);5896$ltext.='</span>';5897$ltext.= esc_html($3, -nbsp=>1);5898}else{5899$ltext= esc_html($ltext, -nbsp=>1);5900}5901print"<div class=\"pre\">".5902$cgi->a({-href =>$file_href.'#l'.$lno,5903-class=>"linenr"},sprintf('%4i',$lno)) .5904' '.$ltext."</div>\n";5905}5906}5907if($lastfile) {5908print"</td></tr>\n";5909if($matches>1000) {5910print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5911}5912}else{5913print"<div class=\"diff nodifferences\">No matches found</div>\n";5914}5915close$fd;59165917print"</table>\n";59185919 git_footer_html();5920}59215922sub git_search_grep_body {5923my($commitlist,$from,$to,$extra) =@_;5924$from=0unlessdefined$from;5925$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);59265927print"<table class=\"commit_search\">\n";5928my$alternate=1;5929for(my$i=$from;$i<=$to;$i++) {5930my%co= %{$commitlist->[$i]};5931if(!%co) {5932next;5933}5934my$commit=$co{'id'};5935if($alternate) {5936print"<tr class=\"dark\">\n";5937}else{5938print"<tr class=\"light\">\n";5939}5940$alternate^=1;5941print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5942 format_author_html('td', \%co,15,5) .5943"<td>".5944$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5945-class=>"list subject"},5946 chop_and_escape_str($co{'title'},50) ."<br/>");5947my$comment=$co{'comment'};5948foreachmy$line(@$comment) {5949if($line=~m/^(.*?)($search_regexp)(.*)$/i) {5950my($lead,$match,$trail) = ($1,$2,$3);5951$match= chop_str($match,70,5,'center');5952my$contextlen=int((80-length($match))/2);5953$contextlen=30if($contextlen>30);5954$lead= chop_str($lead,$contextlen,10,'left');5955$trail= chop_str($trail,$contextlen,10,'right');59565957$lead= esc_html($lead);5958$match= esc_html($match);5959$trail= esc_html($trail);59605961print"$lead<span class=\"match\">$match</span>$trail<br />";5962}5963}5964print"</td>\n".5965"<td class=\"link\">".5966$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5967" | ".5968$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .5969" | ".5970$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5971print"</td>\n".5972"</tr>\n";5973}5974if(defined$extra) {5975print"<tr>\n".5976"<td colspan=\"3\">$extra</td>\n".5977"</tr>\n";5978}5979print"</table>\n";5980}59815982## ======================================================================5983## ======================================================================5984## actions59855986sub git_project_list {5987my$order=$input_params{'order'};5988if(defined$order&&$order!~m/none|project|descr|owner|age/) {5989 die_error(400,"Unknown order parameter");5990}59915992my@list= git_get_projects_list();5993if(!@list) {5994 die_error(404,"No projects found");5995}59965997 git_header_html();5998if(defined$home_text&& -f $home_text) {5999print"<div class=\"index_include\">\n";6000 insert_file($home_text);6001print"</div>\n";6002}6003print$cgi->startform(-method=>"get") .6004"<p class=\"projsearch\">Search:\n".6005$cgi->textfield(-name =>"s", -value =>$searchtext, -override =>1) ."\n".6006"</p>".6007$cgi->end_form() ."\n";6008 git_project_list_body(\@list,$order);6009 git_footer_html();6010}60116012sub git_forks {6013my$order=$input_params{'order'};6014if(defined$order&&$order!~m/none|project|descr|owner|age/) {6015 die_error(400,"Unknown order parameter");6016}60176018my@list= git_get_projects_list($project);6019if(!@list) {6020 die_error(404,"No forks found");6021}60226023 git_header_html();6024 git_print_page_nav('','');6025 git_print_header_div('summary',"$projectforks");6026 git_project_list_body(\@list,$order);6027 git_footer_html();6028}60296030sub git_project_index {6031my@projects= git_get_projects_list();6032if(!@projects) {6033 die_error(404,"No projects found");6034}60356036print$cgi->header(6037-type =>'text/plain',6038-charset =>'utf-8',6039-content_disposition =>'inline; filename="index.aux"');60406041foreachmy$pr(@projects) {6042if(!exists$pr->{'owner'}) {6043$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");6044}60456046my($path,$owner) = ($pr->{'path'},$pr->{'owner'});6047# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '6048$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;6049$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;6050$path=~s/ /\+/g;6051$owner=~s/ /\+/g;60526053print"$path$owner\n";6054}6055}60566057sub git_summary {6058my$descr= git_get_project_description($project) ||"none";6059my%co= parse_commit("HEAD");6060my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();6061my$head=$co{'id'};6062my$remote_heads= gitweb_check_feature('remote_heads');60636064my$owner= git_get_project_owner($project);60656066my$refs= git_get_references();6067# These get_*_list functions return one more to allow us to see if6068# there are more ...6069my@taglist= git_get_tags_list(16);6070my@headlist= git_get_heads_list(16);6071my%remotedata=$remote_heads? git_get_remotes_list() : ();6072my@forklist;6073my$check_forks= gitweb_check_feature('forks');60746075if($check_forks) {6076# find forks of a project6077@forklist= git_get_projects_list($project);6078# filter out forks of forks6079@forklist= filter_forks_from_projects_list(\@forklist)6080if(@forklist);6081}60826083 git_header_html();6084 git_print_page_nav('summary','',$head);60856086print"<div class=\"title\"> </div>\n";6087print"<table class=\"projects_list\">\n".6088"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".6089"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";6090if(defined$cd{'rfc2822'}) {6091print"<tr id=\"metadata_lchange\"><td>last change</td>".6092"<td>".format_timestamp_html(\%cd)."</td></tr>\n";6093}60946095# use per project git URL list in $projectroot/$project/cloneurl6096# or make project git URL from git base URL and project name6097my$url_tag="URL";6098my@url_list= git_get_project_url_list($project);6099@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;6100foreachmy$git_url(@url_list) {6101next unless$git_url;6102print format_repo_url($url_tag,$git_url);6103$url_tag="";6104}61056106# Tag cloud6107my$show_ctags= gitweb_check_feature('ctags');6108if($show_ctags) {6109my$ctags= git_get_project_ctags($project);6110if(%$ctags) {6111# without ability to add tags, don't show if there are none6112my$cloud= git_populate_project_tagcloud($ctags);6113print"<tr id=\"metadata_ctags\">".6114"<td>content tags</td>".6115"<td>".git_show_project_tagcloud($cloud,48)."</td>".6116"</tr>\n";6117}6118}61196120print"</table>\n";61216122# If XSS prevention is on, we don't include README.html.6123# TODO: Allow a readme in some safe format.6124if(!$prevent_xss&& -s "$projectroot/$project/README.html") {6125print"<div class=\"title\">readme</div>\n".6126"<div class=\"readme\">\n";6127 insert_file("$projectroot/$project/README.html");6128print"\n</div>\n";# class="readme"6129}61306131# we need to request one more than 16 (0..15) to check if6132# those 16 are all6133my@commitlist=$head? parse_commits($head,17) : ();6134if(@commitlist) {6135 git_print_header_div('shortlog');6136 git_shortlog_body(\@commitlist,0,15,$refs,6137$#commitlist<=15?undef:6138$cgi->a({-href => href(action=>"shortlog")},"..."));6139}61406141if(@taglist) {6142 git_print_header_div('tags');6143 git_tags_body(\@taglist,0,15,6144$#taglist<=15?undef:6145$cgi->a({-href => href(action=>"tags")},"..."));6146}61476148if(@headlist) {6149 git_print_header_div('heads');6150 git_heads_body(\@headlist,$head,0,15,6151$#headlist<=15?undef:6152$cgi->a({-href => href(action=>"heads")},"..."));6153}61546155if(%remotedata) {6156 git_print_header_div('remotes');6157 git_remotes_body(\%remotedata,15,$head);6158}61596160if(@forklist) {6161 git_print_header_div('forks');6162 git_project_list_body(\@forklist,'age',0,15,6163$#forklist<=15?undef:6164$cgi->a({-href => href(action=>"forks")},"..."),6165'no_header');6166}61676168 git_footer_html();6169}61706171sub git_tag {6172my%tag= parse_tag($hash);61736174if(!%tag) {6175 die_error(404,"Unknown tag object");6176}61776178my$head= git_get_head_hash($project);6179 git_header_html();6180 git_print_page_nav('','',$head,undef,$head);6181 git_print_header_div('commit', esc_html($tag{'name'}),$hash);6182print"<div class=\"title_text\">\n".6183"<table class=\"object_header\">\n".6184"<tr>\n".6185"<td>object</td>\n".6186"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},6187$tag{'object'}) ."</td>\n".6188"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},6189$tag{'type'}) ."</td>\n".6190"</tr>\n";6191if(defined($tag{'author'})) {6192 git_print_authorship_rows(\%tag,'author');6193}6194print"</table>\n\n".6195"</div>\n";6196print"<div class=\"page_body\">";6197my$comment=$tag{'comment'};6198foreachmy$line(@$comment) {6199chomp$line;6200print esc_html($line, -nbsp=>1) ."<br/>\n";6201}6202print"</div>\n";6203 git_footer_html();6204}62056206sub git_blame_common {6207my$format=shift||'porcelain';6208if($formateq'porcelain'&&$input_params{'javascript'}) {6209$format='incremental';6210$action='blame_incremental';# for page title etc6211}62126213# permissions6214 gitweb_check_feature('blame')6215or die_error(403,"Blame view not allowed");62166217# error checking6218 die_error(400,"No file name given")unless$file_name;6219$hash_base||= git_get_head_hash($project);6220 die_error(404,"Couldn't find base commit")unless$hash_base;6221my%co= parse_commit($hash_base)6222or die_error(404,"Commit not found");6223my$ftype="blob";6224if(!defined$hash) {6225$hash= git_get_hash_by_path($hash_base,$file_name,"blob")6226or die_error(404,"Error looking up file");6227}else{6228$ftype= git_get_type($hash);6229if($ftype!~"blob") {6230 die_error(400,"Object is not a blob");6231}6232}62336234my$fd;6235if($formateq'incremental') {6236# get file contents (as base)6237open$fd,"-|", git_cmd(),'cat-file','blob',$hash6238or die_error(500,"Open git-cat-file failed");6239}elsif($formateq'data') {6240# run git-blame --incremental6241open$fd,"-|", git_cmd(),"blame","--incremental",6242$hash_base,"--",$file_name6243or die_error(500,"Open git-blame --incremental failed");6244}else{6245# run git-blame --porcelain6246open$fd,"-|", git_cmd(),"blame",'-p',6247$hash_base,'--',$file_name6248or die_error(500,"Open git-blame --porcelain failed");6249}62506251# incremental blame data returns early6252if($formateq'data') {6253print$cgi->header(6254-type=>"text/plain", -charset =>"utf-8",6255-status=>"200 OK");6256local$| =1;# output autoflush6257while(my$line= <$fd>) {6258print to_utf8($line);6259}6260close$fd6261or print"ERROR$!\n";62626263print'END';6264if(defined$t0&& gitweb_check_feature('timed')) {6265print' '.6266 tv_interval($t0, [ gettimeofday() ]).6267' '.$number_of_git_cmds;6268}6269print"\n";62706271return;6272}62736274# page header6275 git_header_html();6276my$formats_nav=6277$cgi->a({-href => href(action=>"blob", -replay=>1)},6278"blob") .6279" | ";6280if($formateq'incremental') {6281$formats_nav.=6282$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},6283"blame") ." (non-incremental)";6284}else{6285$formats_nav.=6286$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},6287"blame") ." (incremental)";6288}6289$formats_nav.=6290" | ".6291$cgi->a({-href => href(action=>"history", -replay=>1)},6292"history") .6293" | ".6294$cgi->a({-href => href(action=>$action, file_name=>$file_name)},6295"HEAD");6296 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6297 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6298 git_print_page_path($file_name,$ftype,$hash_base);62996300# page body6301if($formateq'incremental') {6302print"<noscript>\n<div class=\"error\"><center><b>\n".6303"This page requires JavaScript to run.\nUse ".6304$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},6305'this page').6306" instead.\n".6307"</b></center></div>\n</noscript>\n";63086309print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;6310}63116312print qq!<div class="page_body">\n!;6313print qq!<div id="progress_info">.../ ...</div>\n!6314if($formateq'incremental');6315print qq!<table id="blame_table"class="blame" width="100%">\n!.6316#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.6317 qq!<thead>\n!.6318 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.6319 qq!</thead>\n!.6320 qq!<tbody>\n!;63216322my@rev_color=qw(light dark);6323my$num_colors=scalar(@rev_color);6324my$current_color=0;63256326if($formateq'incremental') {6327my$color_class=$rev_color[$current_color];63286329#contents of a file6330my$linenr=0;6331 LINE:6332while(my$line= <$fd>) {6333chomp$line;6334$linenr++;63356336print qq!<tr id="l$linenr"class="$color_class">!.6337 qq!<td class="sha1"><a href=""> </a></td>!.6338 qq!<td class="linenr">!.6339 qq!<a class="linenr" href="">$linenr</a></td>!;6340print qq!<td class="pre">! . esc_html($line) ."</td>\n";6341print qq!</tr>\n!;6342}63436344}else{# porcelain, i.e. ordinary blame6345my%metainfo= ();# saves information about commits63466347# blame data6348 LINE:6349while(my$line= <$fd>) {6350chomp$line;6351# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]6352# no <lines in group> for subsequent lines in group of lines6353my($full_rev,$orig_lineno,$lineno,$group_size) =6354($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);6355if(!exists$metainfo{$full_rev}) {6356$metainfo{$full_rev} = {'nprevious'=>0};6357}6358my$meta=$metainfo{$full_rev};6359my$data;6360while($data= <$fd>) {6361chomp$data;6362last if($data=~s/^\t//);# contents of line6363if($data=~/^(\S+)(?: (.*))?$/) {6364$meta->{$1} =$2unlessexists$meta->{$1};6365}6366if($data=~/^previous /) {6367$meta->{'nprevious'}++;6368}6369}6370my$short_rev=substr($full_rev,0,8);6371my$author=$meta->{'author'};6372my%date=6373 parse_date($meta->{'author-time'},$meta->{'author-tz'});6374my$date=$date{'iso-tz'};6375if($group_size) {6376$current_color= ($current_color+1) %$num_colors;6377}6378my$tr_class=$rev_color[$current_color];6379$tr_class.=' boundary'if(exists$meta->{'boundary'});6380$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);6381$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);6382print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";6383if($group_size) {6384print"<td class=\"sha1\"";6385print" title=\"". esc_html($author) .",$date\"";6386print" rowspan=\"$group_size\""if($group_size>1);6387print">";6388print$cgi->a({-href => href(action=>"commit",6389 hash=>$full_rev,6390 file_name=>$file_name)},6391 esc_html($short_rev));6392if($group_size>=2) {6393my@author_initials= ($author=~/\b([[:upper:]])\B/g);6394if(@author_initials) {6395print"<br />".6396 esc_html(join('',@author_initials));6397# or join('.', ...)6398}6399}6400print"</td>\n";6401}6402# 'previous' <sha1 of parent commit> <filename at commit>6403if(exists$meta->{'previous'} &&6404$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {6405$meta->{'parent'} =$1;6406$meta->{'file_parent'} = unquote($2);6407}6408my$linenr_commit=6409exists($meta->{'parent'}) ?6410$meta->{'parent'} :$full_rev;6411my$linenr_filename=6412exists($meta->{'file_parent'}) ?6413$meta->{'file_parent'} : unquote($meta->{'filename'});6414my$blamed= href(action =>'blame',6415 file_name =>$linenr_filename,6416 hash_base =>$linenr_commit);6417print"<td class=\"linenr\">";6418print$cgi->a({ -href =>"$blamed#l$orig_lineno",6419-class=>"linenr"},6420 esc_html($lineno));6421print"</td>";6422print"<td class=\"pre\">". esc_html($data) ."</td>\n";6423print"</tr>\n";6424}# end while64256426}64276428# footer6429print"</tbody>\n".6430"</table>\n";# class="blame"6431print"</div>\n";# class="blame_body"6432close$fd6433or print"Reading blob failed\n";64346435 git_footer_html();6436}64376438sub git_blame {6439 git_blame_common();6440}64416442sub git_blame_incremental {6443 git_blame_common('incremental');6444}64456446sub git_blame_data {6447 git_blame_common('data');6448}64496450sub git_tags {6451my$head= git_get_head_hash($project);6452 git_header_html();6453 git_print_page_nav('','',$head,undef,$head,format_ref_views('tags'));6454 git_print_header_div('summary',$project);64556456my@tagslist= git_get_tags_list();6457if(@tagslist) {6458 git_tags_body(\@tagslist);6459}6460 git_footer_html();6461}64626463sub git_heads {6464my$head= git_get_head_hash($project);6465 git_header_html();6466 git_print_page_nav('','',$head,undef,$head,format_ref_views('heads'));6467 git_print_header_div('summary',$project);64686469my@headslist= git_get_heads_list();6470if(@headslist) {6471 git_heads_body(\@headslist,$head);6472}6473 git_footer_html();6474}64756476# used both for single remote view and for list of all the remotes6477sub git_remotes {6478 gitweb_check_feature('remote_heads')6479or die_error(403,"Remote heads view is disabled");64806481my$head= git_get_head_hash($project);6482my$remote=$input_params{'hash'};64836484my$remotedata= git_get_remotes_list($remote);6485 die_error(500,"Unable to get remote information")unlessdefined$remotedata;64866487unless(%$remotedata) {6488 die_error(404,defined$remote?6489"Remote$remotenot found":6490"No remotes found");6491}64926493 git_header_html(undef,undef, -action_extra =>$remote);6494 git_print_page_nav('','',$head,undef,$head,6495 format_ref_views($remote?'':'remotes'));64966497 fill_remote_heads($remotedata);6498if(defined$remote) {6499 git_print_header_div('remotes',"$remoteremote for$project");6500 git_remote_block($remote,$remotedata->{$remote},undef,$head);6501}else{6502 git_print_header_div('summary',"$projectremotes");6503 git_remotes_body($remotedata,undef,$head);6504}65056506 git_footer_html();6507}65086509sub git_blob_plain {6510my$type=shift;6511my$expires;65126513if(!defined$hash) {6514if(defined$file_name) {6515my$base=$hash_base|| git_get_head_hash($project);6516$hash= git_get_hash_by_path($base,$file_name,"blob")6517or die_error(404,"Cannot find file");6518}else{6519 die_error(400,"No file name defined");6520}6521}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6522# blobs defined by non-textual hash id's can be cached6523$expires="+1d";6524}65256526open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6527or die_error(500,"Open git-cat-file blob '$hash' failed");65286529# content-type (can include charset)6530$type= blob_contenttype($fd,$file_name,$type);65316532# "save as" filename, even when no $file_name is given6533my$save_as="$hash";6534if(defined$file_name) {6535$save_as=$file_name;6536}elsif($type=~m/^text\//) {6537$save_as.='.txt';6538}65396540# With XSS prevention on, blobs of all types except a few known safe6541# ones are served with "Content-Disposition: attachment" to make sure6542# they don't run in our security domain. For certain image types,6543# blob view writes an <img> tag referring to blob_plain view, and we6544# want to be sure not to break that by serving the image as an6545# attachment (though Firefox 3 doesn't seem to care).6546my$sandbox=$prevent_xss&&6547$type!~m!^(?:text/[a-z]+|image/(?:gif|png|jpeg))(?:[ ;]|$)!;65486549# serve text/* as text/plain6550if($prevent_xss&&6551($type=~m!^text/[a-z]+\b(.*)$!||6552($type=~m!^[a-z]+/[a-z]\+xml\b(.*)$!&& -T $fd))) {6553my$rest=$1;6554$rest=defined$rest?$rest:'';6555$type="text/plain$rest";6556}65576558print$cgi->header(6559-type =>$type,6560-expires =>$expires,6561-content_disposition =>6562($sandbox?'attachment':'inline')6563.'; filename="'.$save_as.'"');6564local$/=undef;6565binmode STDOUT,':raw';6566print<$fd>;6567binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi6568close$fd;6569}65706571sub git_blob {6572my$expires;65736574if(!defined$hash) {6575if(defined$file_name) {6576my$base=$hash_base|| git_get_head_hash($project);6577$hash= git_get_hash_by_path($base,$file_name,"blob")6578or die_error(404,"Cannot find file");6579}else{6580 die_error(400,"No file name defined");6581}6582}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6583# blobs defined by non-textual hash id's can be cached6584$expires="+1d";6585}65866587my$have_blame= gitweb_check_feature('blame');6588open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6589or die_error(500,"Couldn't cat$file_name,$hash");6590my$mimetype= blob_mimetype($fd,$file_name);6591# use 'blob_plain' (aka 'raw') view for files that cannot be displayed6592if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {6593close$fd;6594return git_blob_plain($mimetype);6595}6596# we can have blame only for text/* mimetype6597$have_blame&&= ($mimetype=~m!^text/!);65986599my$highlight= gitweb_check_feature('highlight');6600my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);6601$fd= run_highlighter($fd,$highlight,$syntax)6602if$syntax;66036604 git_header_html(undef,$expires);6605my$formats_nav='';6606if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6607if(defined$file_name) {6608if($have_blame) {6609$formats_nav.=6610$cgi->a({-href => href(action=>"blame", -replay=>1)},6611"blame") .6612" | ";6613}6614$formats_nav.=6615$cgi->a({-href => href(action=>"history", -replay=>1)},6616"history") .6617" | ".6618$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},6619"raw") .6620" | ".6621$cgi->a({-href => href(action=>"blob",6622 hash_base=>"HEAD", file_name=>$file_name)},6623"HEAD");6624}else{6625$formats_nav.=6626$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},6627"raw");6628}6629 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6630 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6631}else{6632print"<div class=\"page_nav\">\n".6633"<br/><br/></div>\n".6634"<div class=\"title\">".esc_html($hash)."</div>\n";6635}6636 git_print_page_path($file_name,"blob",$hash_base);6637print"<div class=\"page_body\">\n";6638if($mimetype=~m!^image/!) {6639print qq!<img type="!.esc_attr($mimetype).qq!"!;6640if($file_name) {6641print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;6642}6643print qq! src="! .6644 href(action=>"blob_plain", hash=>$hash,6645 hash_base=>$hash_base, file_name=>$file_name) .6646 qq!"/>\n!;6647}else{6648my$nr;6649while(my$line= <$fd>) {6650chomp$line;6651$nr++;6652$line= untabify($line);6653printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,6654$nr, esc_attr(href(-replay =>1)),$nr,$nr,6655$syntax? sanitize($line) : esc_html($line, -nbsp=>1);6656}6657}6658close$fd6659or print"Reading blob failed.\n";6660print"</div>";6661 git_footer_html();6662}66636664sub git_tree {6665if(!defined$hash_base) {6666$hash_base="HEAD";6667}6668if(!defined$hash) {6669if(defined$file_name) {6670$hash= git_get_hash_by_path($hash_base,$file_name,"tree");6671}else{6672$hash=$hash_base;6673}6674}6675 die_error(404,"No such tree")unlessdefined($hash);66766677my$show_sizes= gitweb_check_feature('show-sizes');6678my$have_blame= gitweb_check_feature('blame');66796680my@entries= ();6681{6682local$/="\0";6683open my$fd,"-|", git_cmd(),"ls-tree",'-z',6684($show_sizes?'-l': ()),@extra_options,$hash6685or die_error(500,"Open git-ls-tree failed");6686@entries=map{chomp;$_} <$fd>;6687close$fd6688or die_error(404,"Reading tree failed");6689}66906691my$refs= git_get_references();6692my$ref= format_ref_marker($refs,$hash_base);6693 git_header_html();6694my$basedir='';6695if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6696my@views_nav= ();6697if(defined$file_name) {6698push@views_nav,6699$cgi->a({-href => href(action=>"history", -replay=>1)},6700"history"),6701$cgi->a({-href => href(action=>"tree",6702 hash_base=>"HEAD", file_name=>$file_name)},6703"HEAD"),6704}6705my$snapshot_links= format_snapshot_links($hash);6706if(defined$snapshot_links) {6707# FIXME: Should be available when we have no hash base as well.6708push@views_nav,$snapshot_links;6709}6710 git_print_page_nav('tree','',$hash_base,undef,undef,6711join(' | ',@views_nav));6712 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);6713}else{6714undef$hash_base;6715print"<div class=\"page_nav\">\n";6716print"<br/><br/></div>\n";6717print"<div class=\"title\">".esc_html($hash)."</div>\n";6718}6719if(defined$file_name) {6720$basedir=$file_name;6721if($basedirne''&&substr($basedir, -1)ne'/') {6722$basedir.='/';6723}6724 git_print_page_path($file_name,'tree',$hash_base);6725}6726print"<div class=\"page_body\">\n";6727print"<table class=\"tree\">\n";6728my$alternate=1;6729# '..' (top directory) link if possible6730if(defined$hash_base&&6731defined$file_name&&$file_name=~m![^/]+$!) {6732if($alternate) {6733print"<tr class=\"dark\">\n";6734}else{6735print"<tr class=\"light\">\n";6736}6737$alternate^=1;67386739my$up=$file_name;6740$up=~s!/?[^/]+$!!;6741undef$upunless$up;6742# based on git_print_tree_entry6743print'<td class="mode">'. mode_str('040000') ."</td>\n";6744print'<td class="size"> </td>'."\n"if$show_sizes;6745print'<td class="list">';6746print$cgi->a({-href => href(action=>"tree",6747 hash_base=>$hash_base,6748 file_name=>$up)},6749"..");6750print"</td>\n";6751print"<td class=\"link\"></td>\n";67526753print"</tr>\n";6754}6755foreachmy$line(@entries) {6756my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);67576758if($alternate) {6759print"<tr class=\"dark\">\n";6760}else{6761print"<tr class=\"light\">\n";6762}6763$alternate^=1;67646765 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);67666767print"</tr>\n";6768}6769print"</table>\n".6770"</div>";6771 git_footer_html();6772}67736774sub snapshot_name {6775my($project,$hash) =@_;67766777# path/to/project.git -> project6778# path/to/project/.git -> project6779my$name= to_utf8($project);6780$name=~ s,([^/])/*\.git$,$1,;6781$name= basename($name);6782# sanitize name6783$name=~s/[[:cntrl:]]/?/g;67846785my$ver=$hash;6786if($hash=~/^[0-9a-fA-F]+$/) {6787# shorten SHA-1 hash6788my$full_hash= git_get_full_hash($project,$hash);6789if($full_hash=~/^$hash/&&length($hash) >7) {6790$ver= git_get_short_hash($project,$hash);6791}6792}elsif($hash=~m!^refs/tags/(.*)$!) {6793# tags don't need shortened SHA-1 hash6794$ver=$1;6795}else{6796# branches and other need shortened SHA-1 hash6797if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {6798$ver=$1;6799}6800$ver.='-'. git_get_short_hash($project,$hash);6801}6802# in case of hierarchical branch names6803$ver=~s!/!.!g;68046805# name = project-version_string6806$name="$name-$ver";68076808returnwantarray? ($name,$name) :$name;6809}68106811sub git_snapshot {6812my$format=$input_params{'snapshot_format'};6813if(!@snapshot_fmts) {6814 die_error(403,"Snapshots not allowed");6815}6816# default to first supported snapshot format6817$format||=$snapshot_fmts[0];6818if($format!~m/^[a-z0-9]+$/) {6819 die_error(400,"Invalid snapshot format parameter");6820}elsif(!exists($known_snapshot_formats{$format})) {6821 die_error(400,"Unknown snapshot format");6822}elsif($known_snapshot_formats{$format}{'disabled'}) {6823 die_error(403,"Snapshot format not allowed");6824}elsif(!grep($_eq$format,@snapshot_fmts)) {6825 die_error(403,"Unsupported snapshot format");6826}68276828my$type= git_get_type("$hash^{}");6829if(!$type) {6830 die_error(404,'Object does not exist');6831}elsif($typeeq'blob') {6832 die_error(400,'Object is not a tree-ish');6833}68346835my($name,$prefix) = snapshot_name($project,$hash);6836my$filename="$name$known_snapshot_formats{$format}{'suffix'}";6837my$cmd= quote_command(6838 git_cmd(),'archive',6839"--format=$known_snapshot_formats{$format}{'format'}",6840"--prefix=$prefix/",$hash);6841if(exists$known_snapshot_formats{$format}{'compressor'}) {6842$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});6843}68446845$filename=~s/(["\\])/\\$1/g;6846print$cgi->header(6847-type =>$known_snapshot_formats{$format}{'type'},6848-content_disposition =>'inline; filename="'.$filename.'"',6849-status =>'200 OK');68506851open my$fd,"-|",$cmd6852or die_error(500,"Execute git-archive failed");6853binmode STDOUT,':raw';6854print<$fd>;6855binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi6856close$fd;6857}68586859sub git_log_generic {6860my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;68616862my$head= git_get_head_hash($project);6863if(!defined$base) {6864$base=$head;6865}6866if(!defined$page) {6867$page=0;6868}6869my$refs= git_get_references();68706871my$commit_hash=$base;6872if(defined$parent) {6873$commit_hash="$parent..$base";6874}6875my@commitlist=6876 parse_commits($commit_hash,101, (100*$page),6877defined$file_name? ($file_name,"--full-history") : ());68786879my$ftype;6880if(!defined$file_hash&&defined$file_name) {6881# some commits could have deleted file in question,6882# and not have it in tree, but one of them has to have it6883for(my$i=0;$i<@commitlist;$i++) {6884$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);6885last ifdefined$file_hash;6886}6887}6888if(defined$file_hash) {6889$ftype= git_get_type($file_hash);6890}6891if(defined$file_name&& !defined$ftype) {6892 die_error(500,"Unknown type of object");6893}6894my%co;6895if(defined$file_name) {6896%co= parse_commit($base)6897or die_error(404,"Unknown commit object");6898}689969006901my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);6902my$next_link='';6903if($#commitlist>=100) {6904$next_link=6905$cgi->a({-href => href(-replay=>1, page=>$page+1),6906-accesskey =>"n", -title =>"Alt-n"},"next");6907}6908my$patch_max= gitweb_get_feature('patches');6909if($patch_max&& !defined$file_name) {6910if($patch_max<0||@commitlist<=$patch_max) {6911$paging_nav.=" ⋅ ".6912$cgi->a({-href => href(action=>"patches", -replay=>1)},6913"patches");6914}6915}69166917 git_header_html();6918 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);6919if(defined$file_name) {6920 git_print_header_div('commit', esc_html($co{'title'}),$base);6921}else{6922 git_print_header_div('summary',$project)6923}6924 git_print_page_path($file_name,$ftype,$hash_base)6925if(defined$file_name);69266927$body_subr->(\@commitlist,0,99,$refs,$next_link,6928$file_name,$file_hash,$ftype);69296930 git_footer_html();6931}69326933sub git_log {6934 git_log_generic('log', \&git_log_body,6935$hash,$hash_parent);6936}69376938sub git_commit {6939$hash||=$hash_base||"HEAD";6940my%co= parse_commit($hash)6941or die_error(404,"Unknown commit object");69426943my$parent=$co{'parent'};6944my$parents=$co{'parents'};# listref69456946# we need to prepare $formats_nav before any parameter munging6947my$formats_nav;6948if(!defined$parent) {6949# --root commitdiff6950$formats_nav.='(initial)';6951}elsif(@$parents==1) {6952# single parent commit6953$formats_nav.=6954'(parent: '.6955$cgi->a({-href => href(action=>"commit",6956 hash=>$parent)},6957 esc_html(substr($parent,0,7))) .6958')';6959}else{6960# merge commit6961$formats_nav.=6962'(merge: '.6963join(' ',map{6964$cgi->a({-href => href(action=>"commit",6965 hash=>$_)},6966 esc_html(substr($_,0,7)));6967}@$parents) .6968')';6969}6970if(gitweb_check_feature('patches') &&@$parents<=1) {6971$formats_nav.=" | ".6972$cgi->a({-href => href(action=>"patch", -replay=>1)},6973"patch");6974}69756976if(!defined$parent) {6977$parent="--root";6978}6979my@difftree;6980open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",6981@diff_opts,6982(@$parents<=1?$parent:'-c'),6983$hash,"--"6984or die_error(500,"Open git-diff-tree failed");6985@difftree=map{chomp;$_} <$fd>;6986close$fdor die_error(404,"Reading git-diff-tree failed");69876988# non-textual hash id's can be cached6989my$expires;6990if($hash=~m/^[0-9a-fA-F]{40}$/) {6991$expires="+1d";6992}6993my$refs= git_get_references();6994my$ref= format_ref_marker($refs,$co{'id'});69956996 git_header_html(undef,$expires);6997 git_print_page_nav('commit','',6998$hash,$co{'tree'},$hash,6999$formats_nav);70007001if(defined$co{'parent'}) {7002 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);7003}else{7004 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);7005}7006print"<div class=\"title_text\">\n".7007"<table class=\"object_header\">\n";7008 git_print_authorship_rows(\%co);7009print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";7010print"<tr>".7011"<td>tree</td>".7012"<td class=\"sha1\">".7013$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),7014class=>"list"},$co{'tree'}) .7015"</td>".7016"<td class=\"link\">".7017$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},7018"tree");7019my$snapshot_links= format_snapshot_links($hash);7020if(defined$snapshot_links) {7021print" | ".$snapshot_links;7022}7023print"</td>".7024"</tr>\n";70257026foreachmy$par(@$parents) {7027print"<tr>".7028"<td>parent</td>".7029"<td class=\"sha1\">".7030$cgi->a({-href => href(action=>"commit", hash=>$par),7031class=>"list"},$par) .7032"</td>".7033"<td class=\"link\">".7034$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .7035" | ".7036$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .7037"</td>".7038"</tr>\n";7039}7040print"</table>".7041"</div>\n";70427043print"<div class=\"page_body\">\n";7044 git_print_log($co{'comment'});7045print"</div>\n";70467047 git_difftree_body(\@difftree,$hash,@$parents);70487049 git_footer_html();7050}70517052sub git_object {7053# object is defined by:7054# - hash or hash_base alone7055# - hash_base and file_name7056my$type;70577058# - hash or hash_base alone7059if($hash|| ($hash_base&& !defined$file_name)) {7060my$object_id=$hash||$hash_base;70617062open my$fd,"-|", quote_command(7063 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'7064or die_error(404,"Object does not exist");7065$type= <$fd>;7066chomp$type;7067close$fd7068or die_error(404,"Object does not exist");70697070# - hash_base and file_name7071}elsif($hash_base&&defined$file_name) {7072$file_name=~ s,/+$,,;70737074system(git_cmd(),"cat-file",'-e',$hash_base) ==07075or die_error(404,"Base object does not exist");70767077# here errors should not hapen7078open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name7079or die_error(500,"Open git-ls-tree failed");7080my$line= <$fd>;7081close$fd;70827083#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'7084unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {7085 die_error(404,"File or directory for given base does not exist");7086}7087$type=$2;7088$hash=$3;7089}else{7090 die_error(400,"Not enough information to find object");7091}70927093print$cgi->redirect(-uri => href(action=>$type, -full=>1,7094 hash=>$hash, hash_base=>$hash_base,7095 file_name=>$file_name),7096-status =>'302 Found');7097}70987099sub git_blobdiff {7100my$format=shift||'html';7101my$diff_style=$input_params{'diff_style'} ||'inline';71027103my$fd;7104my@difftree;7105my%diffinfo;7106my$expires;71077108# preparing $fd and %diffinfo for git_patchset_body7109# new style URI7110if(defined$hash_base&&defined$hash_parent_base) {7111if(defined$file_name) {7112# read raw output7113open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7114$hash_parent_base,$hash_base,7115"--", (defined$file_parent?$file_parent: ()),$file_name7116or die_error(500,"Open git-diff-tree failed");7117@difftree=map{chomp;$_} <$fd>;7118close$fd7119or die_error(404,"Reading git-diff-tree failed");7120@difftree7121or die_error(404,"Blob diff not found");71227123}elsif(defined$hash&&7124$hash=~/[0-9a-fA-F]{40}/) {7125# try to find filename from $hash71267127# read filtered raw output7128open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7129$hash_parent_base,$hash_base,"--"7130or die_error(500,"Open git-diff-tree failed");7131@difftree=7132# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'7133# $hash == to_id7134grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}7135map{chomp;$_} <$fd>;7136close$fd7137or die_error(404,"Reading git-diff-tree failed");7138@difftree7139or die_error(404,"Blob diff not found");71407141}else{7142 die_error(400,"Missing one of the blob diff parameters");7143}71447145if(@difftree>1) {7146 die_error(400,"Ambiguous blob diff specification");7147}71487149%diffinfo= parse_difftree_raw_line($difftree[0]);7150$file_parent||=$diffinfo{'from_file'} ||$file_name;7151$file_name||=$diffinfo{'to_file'};71527153$hash_parent||=$diffinfo{'from_id'};7154$hash||=$diffinfo{'to_id'};71557156# non-textual hash id's can be cached7157if($hash_base=~m/^[0-9a-fA-F]{40}$/&&7158$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {7159$expires='+1d';7160}71617162# open patch output7163open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7164'-p', ($formateq'html'?"--full-index": ()),7165$hash_parent_base,$hash_base,7166"--", (defined$file_parent?$file_parent: ()),$file_name7167or die_error(500,"Open git-diff-tree failed");7168}71697170# old/legacy style URI -- not generated anymore since 1.4.3.7171if(!%diffinfo) {7172 die_error('404 Not Found',"Missing one of the blob diff parameters")7173}71747175# header7176if($formateq'html') {7177my$formats_nav=7178$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},7179"raw");7180$formats_nav.= diff_style_nav($diff_style);7181 git_header_html(undef,$expires);7182if(defined$hash_base&& (my%co= parse_commit($hash_base))) {7183 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);7184 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);7185}else{7186print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";7187print"<div class=\"title\">".esc_html("$hashvs$hash_parent")."</div>\n";7188}7189if(defined$file_name) {7190 git_print_page_path($file_name,"blob",$hash_base);7191}else{7192print"<div class=\"page_path\"></div>\n";7193}71947195}elsif($formateq'plain') {7196print$cgi->header(7197-type =>'text/plain',7198-charset =>'utf-8',7199-expires =>$expires,7200-content_disposition =>'inline; filename="'."$file_name".'.patch"');72017202print"X-Git-Url: ".$cgi->self_url() ."\n\n";72037204}else{7205 die_error(400,"Unknown blobdiff format");7206}72077208# patch7209if($formateq'html') {7210print"<div class=\"page_body\">\n";72117212 git_patchset_body($fd,$diff_style,7213[ \%diffinfo],$hash_base,$hash_parent_base);7214close$fd;72157216print"</div>\n";# class="page_body"7217 git_footer_html();72187219}else{7220while(my$line= <$fd>) {7221$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;7222$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;72237224print$line;72257226last if$line=~m!^\+\+\+!;7227}7228local$/=undef;7229print<$fd>;7230close$fd;7231}7232}72337234sub git_blobdiff_plain {7235 git_blobdiff('plain');7236}72377238# assumes that it is added as later part of already existing navigation,7239# so it returns "| foo | bar" rather than just "foo | bar"7240sub diff_style_nav {7241my($diff_style,$is_combined) =@_;7242$diff_style||='inline';72437244return""if($is_combined);72457246my@styles= (inline =>'inline','sidebyside'=>'side by side');7247my%styles=@styles;7248@styles=7249@styles[map{$_*2}0..$#styles/2];72507251returnjoin'',7252map{" | ".$_}7253map{7254$_eq$diff_style?$styles{$_} :7255$cgi->a({-href => href(-replay=>1, diff_style =>$_)},$styles{$_})7256}@styles;7257}72587259sub git_commitdiff {7260my%params=@_;7261my$format=$params{-format} ||'html';7262my$diff_style=$input_params{'diff_style'} ||'inline';72637264my($patch_max) = gitweb_get_feature('patches');7265if($formateq'patch') {7266 die_error(403,"Patch view not allowed")unless$patch_max;7267}72687269$hash||=$hash_base||"HEAD";7270my%co= parse_commit($hash)7271or die_error(404,"Unknown commit object");72727273# choose format for commitdiff for merge7274if(!defined$hash_parent&& @{$co{'parents'}} >1) {7275$hash_parent='--cc';7276}7277# we need to prepare $formats_nav before almost any parameter munging7278my$formats_nav;7279if($formateq'html') {7280$formats_nav=7281$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},7282"raw");7283if($patch_max&& @{$co{'parents'}} <=1) {7284$formats_nav.=" | ".7285$cgi->a({-href => href(action=>"patch", -replay=>1)},7286"patch");7287}7288$formats_nav.= diff_style_nav($diff_style, @{$co{'parents'}} >1);72897290if(defined$hash_parent&&7291$hash_parentne'-c'&&$hash_parentne'--cc') {7292# commitdiff with two commits given7293my$hash_parent_short=$hash_parent;7294if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {7295$hash_parent_short=substr($hash_parent,0,7);7296}7297$formats_nav.=7298' (from';7299for(my$i=0;$i< @{$co{'parents'}};$i++) {7300if($co{'parents'}[$i]eq$hash_parent) {7301$formats_nav.=' parent '. ($i+1);7302last;7303}7304}7305$formats_nav.=': '.7306$cgi->a({-href => href(-replay=>1,7307 hash=>$hash_parent, hash_base=>undef)},7308 esc_html($hash_parent_short)) .7309')';7310}elsif(!$co{'parent'}) {7311# --root commitdiff7312$formats_nav.=' (initial)';7313}elsif(scalar@{$co{'parents'}} ==1) {7314# single parent commit7315$formats_nav.=7316' (parent: '.7317$cgi->a({-href => href(-replay=>1,7318 hash=>$co{'parent'}, hash_base=>undef)},7319 esc_html(substr($co{'parent'},0,7))) .7320')';7321}else{7322# merge commit7323if($hash_parenteq'--cc') {7324$formats_nav.=' | '.7325$cgi->a({-href => href(-replay=>1,7326 hash=>$hash, hash_parent=>'-c')},7327'combined');7328}else{# $hash_parent eq '-c'7329$formats_nav.=' | '.7330$cgi->a({-href => href(-replay=>1,7331 hash=>$hash, hash_parent=>'--cc')},7332'compact');7333}7334$formats_nav.=7335' (merge: '.7336join(' ',map{7337$cgi->a({-href => href(-replay=>1,7338 hash=>$_, hash_base=>undef)},7339 esc_html(substr($_,0,7)));7340} @{$co{'parents'}} ) .7341')';7342}7343}73447345my$hash_parent_param=$hash_parent;7346if(!defined$hash_parent_param) {7347# --cc for multiple parents, --root for parentless7348$hash_parent_param=7349@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';7350}73517352# read commitdiff7353my$fd;7354my@difftree;7355if($formateq'html') {7356open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7357"--no-commit-id","--patch-with-raw","--full-index",7358$hash_parent_param,$hash,"--"7359or die_error(500,"Open git-diff-tree failed");73607361while(my$line= <$fd>) {7362chomp$line;7363# empty line ends raw part of diff-tree output7364last unless$line;7365push@difftree,scalar parse_difftree_raw_line($line);7366}73677368}elsif($formateq'plain') {7369open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7370'-p',$hash_parent_param,$hash,"--"7371or die_error(500,"Open git-diff-tree failed");7372}elsif($formateq'patch') {7373# For commit ranges, we limit the output to the number of7374# patches specified in the 'patches' feature.7375# For single commits, we limit the output to a single patch,7376# diverging from the git-format-patch default.7377my@commit_spec= ();7378if($hash_parent) {7379if($patch_max>0) {7380push@commit_spec,"-$patch_max";7381}7382push@commit_spec,'-n',"$hash_parent..$hash";7383}else{7384if($params{-single}) {7385push@commit_spec,'-1';7386}else{7387if($patch_max>0) {7388push@commit_spec,"-$patch_max";7389}7390push@commit_spec,"-n";7391}7392push@commit_spec,'--root',$hash;7393}7394open$fd,"-|", git_cmd(),"format-patch",@diff_opts,7395'--encoding=utf8','--stdout',@commit_spec7396or die_error(500,"Open git-format-patch failed");7397}else{7398 die_error(400,"Unknown commitdiff format");7399}74007401# non-textual hash id's can be cached7402my$expires;7403if($hash=~m/^[0-9a-fA-F]{40}$/) {7404$expires="+1d";7405}74067407# write commit message7408if($formateq'html') {7409my$refs= git_get_references();7410my$ref= format_ref_marker($refs,$co{'id'});74117412 git_header_html(undef,$expires);7413 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);7414 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);7415print"<div class=\"title_text\">\n".7416"<table class=\"object_header\">\n";7417 git_print_authorship_rows(\%co);7418print"</table>".7419"</div>\n";7420print"<div class=\"page_body\">\n";7421if(@{$co{'comment'}} >1) {7422print"<div class=\"log\">\n";7423 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);7424print"</div>\n";# class="log"7425}74267427}elsif($formateq'plain') {7428my$refs= git_get_references("tags");7429my$tagname= git_get_rev_name_tags($hash);7430my$filename= basename($project) ."-$hash.patch";74317432print$cgi->header(7433-type =>'text/plain',7434-charset =>'utf-8',7435-expires =>$expires,7436-content_disposition =>'inline; filename="'."$filename".'"');7437my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});7438print"From: ". to_utf8($co{'author'}) ."\n";7439print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";7440print"Subject: ". to_utf8($co{'title'}) ."\n";74417442print"X-Git-Tag:$tagname\n"if$tagname;7443print"X-Git-Url: ".$cgi->self_url() ."\n\n";74447445foreachmy$line(@{$co{'comment'}}) {7446print to_utf8($line) ."\n";7447}7448print"---\n\n";7449}elsif($formateq'patch') {7450my$filename= basename($project) ."-$hash.patch";74517452print$cgi->header(7453-type =>'text/plain',7454-charset =>'utf-8',7455-expires =>$expires,7456-content_disposition =>'inline; filename="'."$filename".'"');7457}74587459# write patch7460if($formateq'html') {7461my$use_parents= !defined$hash_parent||7462$hash_parenteq'-c'||$hash_parenteq'--cc';7463 git_difftree_body(\@difftree,$hash,7464$use_parents? @{$co{'parents'}} :$hash_parent);7465print"<br/>\n";74667467 git_patchset_body($fd,$diff_style,7468 \@difftree,$hash,7469$use_parents? @{$co{'parents'}} :$hash_parent);7470close$fd;7471print"</div>\n";# class="page_body"7472 git_footer_html();74737474}elsif($formateq'plain') {7475local$/=undef;7476print<$fd>;7477close$fd7478or print"Reading git-diff-tree failed\n";7479}elsif($formateq'patch') {7480local$/=undef;7481print<$fd>;7482close$fd7483or print"Reading git-format-patch failed\n";7484}7485}74867487sub git_commitdiff_plain {7488 git_commitdiff(-format =>'plain');7489}74907491# format-patch-style patches7492sub git_patch {7493 git_commitdiff(-format =>'patch', -single =>1);7494}74957496sub git_patches {7497 git_commitdiff(-format =>'patch');7498}74997500sub git_history {7501 git_log_generic('history', \&git_history_body,7502$hash_base,$hash_parent_base,7503$file_name,$hash);7504}75057506sub git_search {7507$searchtype||='commit';75087509# check if appropriate features are enabled7510 gitweb_check_feature('search')7511or die_error(403,"Search is disabled");7512if($searchtypeeq'pickaxe') {7513# pickaxe may take all resources of your box and run for several minutes7514# with every query - so decide by yourself how public you make this feature7515 gitweb_check_feature('pickaxe')7516or die_error(403,"Pickaxe search is disabled");7517}7518if($searchtypeeq'grep') {7519# grep search might be potentially CPU-intensive, too7520 gitweb_check_feature('grep')7521or die_error(403,"Grep search is disabled");7522}75237524if(!defined$searchtext) {7525 die_error(400,"Text field is empty");7526}7527if(!defined$hash) {7528$hash= git_get_head_hash($project);7529}7530my%co= parse_commit($hash);7531if(!%co) {7532 die_error(404,"Unknown commit object");7533}7534if(!defined$page) {7535$page=0;7536}75377538if($searchtypeeq'commit'||7539$searchtypeeq'author'||7540$searchtypeeq'committer') {7541 git_search_message(%co);7542}elsif($searchtypeeq'pickaxe') {7543 git_search_changes(%co);7544}elsif($searchtypeeq'grep') {7545 git_search_files(%co);7546}else{7547 die_error(400,"Unknown search type");7548}7549}75507551sub git_search_help {7552 git_header_html();7553 git_print_page_nav('','',$hash,$hash,$hash);7554print<<EOT;7555<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without7556regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,7557the pattern entered is recognized as the POSIX extended7558<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case7559insensitive).</p>7560<dl>7561<dt><b>commit</b></dt>7562<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>7563EOT7564my$have_grep= gitweb_check_feature('grep');7565if($have_grep) {7566print<<EOT;7567<dt><b>grep</b></dt>7568<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing7569 a different one) are searched for the given pattern. On large trees, this search can take7570a while and put some strain on the server, so please use it with some consideration. Note that7571due to git-grep peculiarity, currently if regexp mode is turned off, the matches are7572case-sensitive.</dd>7573EOT7574}7575print<<EOT;7576<dt><b>author</b></dt>7577<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>7578<dt><b>committer</b></dt>7579<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>7580EOT7581my$have_pickaxe= gitweb_check_feature('pickaxe');7582if($have_pickaxe) {7583print<<EOT;7584<dt><b>pickaxe</b></dt>7585<dd>All commits that caused the string to appear or disappear from any file (changes that7586added, removed or "modified" the string) will be listed. This search can take a while and7587takes a lot of strain on the server, so please use it wisely. Note that since you may be7588interested even in changes just changing the case as well, this search is case sensitive.</dd>7589EOT7590}7591print"</dl>\n";7592 git_footer_html();7593}75947595sub git_shortlog {7596 git_log_generic('shortlog', \&git_shortlog_body,7597$hash,$hash_parent);7598}75997600## ......................................................................7601## feeds (RSS, Atom; OPML)76027603sub git_feed {7604my$format=shift||'atom';7605my$have_blame= gitweb_check_feature('blame');76067607# Atom: http://www.atomenabled.org/developers/syndication/7608# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ7609if($formatne'rss'&&$formatne'atom') {7610 die_error(400,"Unknown web feed format");7611}76127613# log/feed of current (HEAD) branch, log of given branch, history of file/directory7614my$head=$hash||'HEAD';7615my@commitlist= parse_commits($head,150,0,$file_name);76167617my%latest_commit;7618my%latest_date;7619my$content_type="application/$format+xml";7620if(defined$cgi->http('HTTP_ACCEPT') &&7621$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {7622# browser (feed reader) prefers text/xml7623$content_type='text/xml';7624}7625if(defined($commitlist[0])) {7626%latest_commit= %{$commitlist[0]};7627my$latest_epoch=$latest_commit{'committer_epoch'};7628%latest_date= parse_date($latest_epoch,$latest_commit{'comitter_tz'});7629my$if_modified=$cgi->http('IF_MODIFIED_SINCE');7630if(defined$if_modified) {7631my$since;7632if(eval{require HTTP::Date;1; }) {7633$since= HTTP::Date::str2time($if_modified);7634}elsif(eval{require Time::ParseDate;1; }) {7635$since= Time::ParseDate::parsedate($if_modified, GMT =>1);7636}7637if(defined$since&&$latest_epoch<=$since) {7638print$cgi->header(7639-type =>$content_type,7640-charset =>'utf-8',7641-last_modified =>$latest_date{'rfc2822'},7642-status =>'304 Not Modified');7643return;7644}7645}7646print$cgi->header(7647-type =>$content_type,7648-charset =>'utf-8',7649-last_modified =>$latest_date{'rfc2822'});7650}else{7651print$cgi->header(7652-type =>$content_type,7653-charset =>'utf-8');7654}76557656# Optimization: skip generating the body if client asks only7657# for Last-Modified date.7658return if($cgi->request_method()eq'HEAD');76597660# header variables7661my$title="$site_name-$project/$action";7662my$feed_type='log';7663if(defined$hash) {7664$title.=" - '$hash'";7665$feed_type='branch log';7666if(defined$file_name) {7667$title.=" ::$file_name";7668$feed_type='history';7669}7670}elsif(defined$file_name) {7671$title.=" -$file_name";7672$feed_type='history';7673}7674$title.="$feed_type";7675my$descr= git_get_project_description($project);7676if(defined$descr) {7677$descr= esc_html($descr);7678}else{7679$descr="$project".7680($formateq'rss'?'RSS':'Atom') .7681" feed";7682}7683my$owner= git_get_project_owner($project);7684$owner= esc_html($owner);76857686#header7687my$alt_url;7688if(defined$file_name) {7689$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);7690}elsif(defined$hash) {7691$alt_url= href(-full=>1, action=>"log", hash=>$hash);7692}else{7693$alt_url= href(-full=>1, action=>"summary");7694}7695print qq!<?xml version="1.0" encoding="utf-8"?>\n!;7696if($formateq'rss') {7697print<<XML;7698<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">7699<channel>7700XML7701print"<title>$title</title>\n".7702"<link>$alt_url</link>\n".7703"<description>$descr</description>\n".7704"<language>en</language>\n".7705# project owner is responsible for 'editorial' content7706"<managingEditor>$owner</managingEditor>\n";7707if(defined$logo||defined$favicon) {7708# prefer the logo to the favicon, since RSS7709# doesn't allow both7710my$img= esc_url($logo||$favicon);7711print"<image>\n".7712"<url>$img</url>\n".7713"<title>$title</title>\n".7714"<link>$alt_url</link>\n".7715"</image>\n";7716}7717if(%latest_date) {7718print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";7719print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";7720}7721print"<generator>gitweb v.$version/$git_version</generator>\n";7722}elsif($formateq'atom') {7723print<<XML;7724<feed xmlns="http://www.w3.org/2005/Atom">7725XML7726print"<title>$title</title>\n".7727"<subtitle>$descr</subtitle>\n".7728'<link rel="alternate" type="text/html" href="'.7729$alt_url.'" />'."\n".7730'<link rel="self" type="'.$content_type.'" href="'.7731$cgi->self_url() .'" />'."\n".7732"<id>". href(-full=>1) ."</id>\n".7733# use project owner for feed author7734"<author><name>$owner</name></author>\n";7735if(defined$favicon) {7736print"<icon>". esc_url($favicon) ."</icon>\n";7737}7738if(defined$logo) {7739# not twice as wide as tall: 72 x 27 pixels7740print"<logo>". esc_url($logo) ."</logo>\n";7741}7742if(!%latest_date) {7743# dummy date to keep the feed valid until commits trickle in:7744print"<updated>1970-01-01T00:00:00Z</updated>\n";7745}else{7746print"<updated>$latest_date{'iso-8601'}</updated>\n";7747}7748print"<generator version='$version/$git_version'>gitweb</generator>\n";7749}77507751# contents7752for(my$i=0;$i<=$#commitlist;$i++) {7753my%co= %{$commitlist[$i]};7754my$commit=$co{'id'};7755# we read 150, we always show 30 and the ones more recent than 48 hours7756if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {7757last;7758}7759my%cd= parse_date($co{'author_epoch'},$co{'author_tz'});77607761# get list of changed files7762open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7763$co{'parent'} ||"--root",7764$co{'id'},"--", (defined$file_name?$file_name: ())7765ornext;7766my@difftree=map{chomp;$_} <$fd>;7767close$fd7768ornext;77697770# print element (entry, item)7771my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);7772if($formateq'rss') {7773print"<item>\n".7774"<title>". esc_html($co{'title'}) ."</title>\n".7775"<author>". esc_html($co{'author'}) ."</author>\n".7776"<pubDate>$cd{'rfc2822'}</pubDate>\n".7777"<guid isPermaLink=\"true\">$co_url</guid>\n".7778"<link>$co_url</link>\n".7779"<description>". esc_html($co{'title'}) ."</description>\n".7780"<content:encoded>".7781"<![CDATA[\n";7782}elsif($formateq'atom') {7783print"<entry>\n".7784"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".7785"<updated>$cd{'iso-8601'}</updated>\n".7786"<author>\n".7787" <name>". esc_html($co{'author_name'}) ."</name>\n";7788if($co{'author_email'}) {7789print" <email>". esc_html($co{'author_email'}) ."</email>\n";7790}7791print"</author>\n".7792# use committer for contributor7793"<contributor>\n".7794" <name>". esc_html($co{'committer_name'}) ."</name>\n";7795if($co{'committer_email'}) {7796print" <email>". esc_html($co{'committer_email'}) ."</email>\n";7797}7798print"</contributor>\n".7799"<published>$cd{'iso-8601'}</published>\n".7800"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".7801"<id>$co_url</id>\n".7802"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".7803"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";7804}7805my$comment=$co{'comment'};7806print"<pre>\n";7807foreachmy$line(@$comment) {7808$line= esc_html($line);7809print"$line\n";7810}7811print"</pre><ul>\n";7812foreachmy$difftree_line(@difftree) {7813my%difftree= parse_difftree_raw_line($difftree_line);7814next if!$difftree{'from_id'};78157816my$file=$difftree{'file'} ||$difftree{'to_file'};78177818print"<li>".7819"[".7820$cgi->a({-href => href(-full=>1, action=>"blobdiff",7821 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},7822 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},7823 file_name=>$file, file_parent=>$difftree{'from_file'}),7824-title =>"diff"},'D');7825if($have_blame) {7826print$cgi->a({-href => href(-full=>1, action=>"blame",7827 file_name=>$file, hash_base=>$commit),7828-title =>"blame"},'B');7829}7830# if this is not a feed of a file history7831if(!defined$file_name||$file_namene$file) {7832print$cgi->a({-href => href(-full=>1, action=>"history",7833 file_name=>$file, hash=>$commit),7834-title =>"history"},'H');7835}7836$file= esc_path($file);7837print"] ".7838"$file</li>\n";7839}7840if($formateq'rss') {7841print"</ul>]]>\n".7842"</content:encoded>\n".7843"</item>\n";7844}elsif($formateq'atom') {7845print"</ul>\n</div>\n".7846"</content>\n".7847"</entry>\n";7848}7849}78507851# end of feed7852if($formateq'rss') {7853print"</channel>\n</rss>\n";7854}elsif($formateq'atom') {7855print"</feed>\n";7856}7857}78587859sub git_rss {7860 git_feed('rss');7861}78627863sub git_atom {7864 git_feed('atom');7865}78667867sub git_opml {7868my@list= git_get_projects_list();7869if(!@list) {7870 die_error(404,"No projects found");7871}78727873print$cgi->header(7874-type =>'text/xml',7875-charset =>'utf-8',7876-content_disposition =>'inline; filename="opml.xml"');78777878my$title= esc_html($site_name);7879print<<XML;7880<?xml version="1.0" encoding="utf-8"?>7881<opml version="1.0">7882<head>7883 <title>$titleOPML Export</title>7884</head>7885<body>7886<outline text="git RSS feeds">7887XML78887889foreachmy$pr(@list) {7890my%proj=%$pr;7891my$head= git_get_head_hash($proj{'path'});7892if(!defined$head) {7893next;7894}7895$git_dir="$projectroot/$proj{'path'}";7896my%co= parse_commit($head);7897if(!%co) {7898next;7899}79007901my$path= esc_html(chop_str($proj{'path'},25,5));7902my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);7903my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);7904print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";7905}7906print<<XML;7907</outline>7908</body>7909</opml>7910XML7911}