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=$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# filename of html text to include at top of each page 89our$site_header="++GITWEB_SITE_HEADER++"; 90# html text to include at home page 91our$home_text="++GITWEB_HOMETEXT++"; 92# filename of html text to include at bottom of each page 93our$site_footer="++GITWEB_SITE_FOOTER++"; 94 95# URI of stylesheets 96our@stylesheets= ("++GITWEB_CSS++"); 97# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 98our$stylesheet=undef; 99# URI of GIT logo (72x27 size) 100our$logo="++GITWEB_LOGO++"; 101# URI of GIT favicon, assumed to be image/png type 102our$favicon="++GITWEB_FAVICON++"; 103# URI of gitweb.js (JavaScript code for gitweb) 104our$javascript="++GITWEB_JS++"; 105 106# URI and label (title) of GIT logo link 107#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 108#our $logo_label = "git documentation"; 109our$logo_url="http://git-scm.com/"; 110our$logo_label="git homepage"; 111 112# source of projects list 113our$projects_list="++GITWEB_LIST++"; 114 115# the width (in characters) of the projects list "Description" column 116our$projects_list_description_width=25; 117 118# default order of projects list 119# valid values are none, project, descr, owner, and age 120our$default_projects_order="project"; 121 122# show repository only if this file exists 123# (only effective if this variable evaluates to true) 124our$export_ok="++GITWEB_EXPORT_OK++"; 125 126# show repository only if this subroutine returns true 127# when given the path to the project, for example: 128# sub { return -e "$_[0]/git-daemon-export-ok"; } 129our$export_auth_hook=undef; 130 131# only allow viewing of repositories also shown on the overview page 132our$strict_export="++GITWEB_STRICT_EXPORT++"; 133 134# list of git base URLs used for URL to where fetch project from, 135# i.e. full URL is "$git_base_url/$project" 136our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 137 138# default blob_plain mimetype and default charset for text/plain blob 139our$default_blob_plain_mimetype='text/plain'; 140our$default_text_plain_charset=undef; 141 142# file to use for guessing MIME types before trying /etc/mime.types 143# (relative to the current git repository) 144our$mimetypes_file=undef; 145 146# assume this charset if line contains non-UTF-8 characters; 147# it should be valid encoding (see Encoding::Supported(3pm) for list), 148# for which encoding all byte sequences are valid, for example 149# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 150# could be even 'utf-8' for the old behavior) 151our$fallback_encoding='latin1'; 152 153# rename detection options for git-diff and git-diff-tree 154# - default is '-M', with the cost proportional to 155# (number of removed files) * (number of new files). 156# - more costly is '-C' (which implies '-M'), with the cost proportional to 157# (number of changed files + number of removed files) * (number of new files) 158# - even more costly is '-C', '--find-copies-harder' with cost 159# (number of files in the original tree) * (number of new files) 160# - one might want to include '-B' option, e.g. '-B', '-M' 161our@diff_opts= ('-M');# taken from git_commit 162 163# Disables features that would allow repository owners to inject script into 164# the gitweb domain. 165our$prevent_xss=0; 166 167# Path to the highlight executable to use (must be the one from 168# http://www.andre-simon.de due to assumptions about parameters and output). 169# Useful if highlight is not installed on your webserver's PATH. 170# [Default: highlight] 171our$highlight_bin="++HIGHLIGHT_BIN++"; 172 173# information about snapshot formats that gitweb is capable of serving 174our%known_snapshot_formats= ( 175# name => { 176# 'display' => display name, 177# 'type' => mime type, 178# 'suffix' => filename suffix, 179# 'format' => --format for git-archive, 180# 'compressor' => [compressor command and arguments] 181# (array reference, optional) 182# 'disabled' => boolean (optional)} 183# 184'tgz'=> { 185'display'=>'tar.gz', 186'type'=>'application/x-gzip', 187'suffix'=>'.tar.gz', 188'format'=>'tar', 189'compressor'=> ['gzip','-n']}, 190 191'tbz2'=> { 192'display'=>'tar.bz2', 193'type'=>'application/x-bzip2', 194'suffix'=>'.tar.bz2', 195'format'=>'tar', 196'compressor'=> ['bzip2']}, 197 198'txz'=> { 199'display'=>'tar.xz', 200'type'=>'application/x-xz', 201'suffix'=>'.tar.xz', 202'format'=>'tar', 203'compressor'=> ['xz'], 204'disabled'=>1}, 205 206'zip'=> { 207'display'=>'zip', 208'type'=>'application/x-zip', 209'suffix'=>'.zip', 210'format'=>'zip'}, 211); 212 213# Aliases so we understand old gitweb.snapshot values in repository 214# configuration. 215our%known_snapshot_format_aliases= ( 216'gzip'=>'tgz', 217'bzip2'=>'tbz2', 218'xz'=>'txz', 219 220# backward compatibility: legacy gitweb config support 221'x-gzip'=>undef,'gz'=>undef, 222'x-bzip2'=>undef,'bz2'=>undef, 223'x-zip'=>undef,''=>undef, 224); 225 226# Pixel sizes for icons and avatars. If the default font sizes or lineheights 227# are changed, it may be appropriate to change these values too via 228# $GITWEB_CONFIG. 229our%avatar_size= ( 230'default'=>16, 231'double'=>32 232); 233 234# Used to set the maximum load that we will still respond to gitweb queries. 235# If server load exceed this value then return "503 server busy" error. 236# If gitweb cannot determined server load, it is taken to be 0. 237# Leave it undefined (or set to 'undef') to turn off load checking. 238our$maxload=300; 239 240# configuration for 'highlight' (http://www.andre-simon.de/) 241# match by basename 242our%highlight_basename= ( 243#'Program' => 'py', 244#'Library' => 'py', 245'SConstruct'=>'py',# SCons equivalent of Makefile 246'Makefile'=>'make', 247); 248# match by extension 249our%highlight_ext= ( 250# main extensions, defining name of syntax; 251# see files in /usr/share/highlight/langDefs/ directory 252map{$_=>$_} 253qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl sql make), 254# alternate extensions, see /etc/highlight/filetypes.conf 255'h'=>'c', 256map{$_=>'sh'}qw(bash zsh ksh), 257map{$_=>'cpp'}qw(cxx c++ cc), 258map{$_=>'php'}qw(php3 php4 php5 phps), 259map{$_=>'pl'}qw(perl pm),# perhaps also 'cgi' 260map{$_=>'make'}qw(mak mk), 261map{$_=>'xml'}qw(xhtml html htm), 262); 263 264# You define site-wide feature defaults here; override them with 265# $GITWEB_CONFIG as necessary. 266our%feature= ( 267# feature => { 268# 'sub' => feature-sub (subroutine), 269# 'override' => allow-override (boolean), 270# 'default' => [ default options...] (array reference)} 271# 272# if feature is overridable (it means that allow-override has true value), 273# then feature-sub will be called with default options as parameters; 274# return value of feature-sub indicates if to enable specified feature 275# 276# if there is no 'sub' key (no feature-sub), then feature cannot be 277# overridden 278# 279# use gitweb_get_feature(<feature>) to retrieve the <feature> value 280# (an array) or gitweb_check_feature(<feature>) to check if <feature> 281# is enabled 282 283# Enable the 'blame' blob view, showing the last commit that modified 284# each line in the file. This can be very CPU-intensive. 285 286# To enable system wide have in $GITWEB_CONFIG 287# $feature{'blame'}{'default'} = [1]; 288# To have project specific config enable override in $GITWEB_CONFIG 289# $feature{'blame'}{'override'} = 1; 290# and in project config gitweb.blame = 0|1; 291'blame'=> { 292'sub'=>sub{ feature_bool('blame',@_) }, 293'override'=>0, 294'default'=> [0]}, 295 296# Enable the 'snapshot' link, providing a compressed archive of any 297# tree. This can potentially generate high traffic if you have large 298# project. 299 300# Value is a list of formats defined in %known_snapshot_formats that 301# you wish to offer. 302# To disable system wide have in $GITWEB_CONFIG 303# $feature{'snapshot'}{'default'} = []; 304# To have project specific config enable override in $GITWEB_CONFIG 305# $feature{'snapshot'}{'override'} = 1; 306# and in project config, a comma-separated list of formats or "none" 307# to disable. Example: gitweb.snapshot = tbz2,zip; 308'snapshot'=> { 309'sub'=> \&feature_snapshot, 310'override'=>0, 311'default'=> ['tgz']}, 312 313# Enable text search, which will list the commits which match author, 314# committer or commit text to a given string. Enabled by default. 315# Project specific override is not supported. 316'search'=> { 317'override'=>0, 318'default'=> [1]}, 319 320# Enable grep search, which will list the files in currently selected 321# tree containing the given string. Enabled by default. This can be 322# potentially CPU-intensive, of course. 323 324# To enable system wide have in $GITWEB_CONFIG 325# $feature{'grep'}{'default'} = [1]; 326# To have project specific config enable override in $GITWEB_CONFIG 327# $feature{'grep'}{'override'} = 1; 328# and in project config gitweb.grep = 0|1; 329'grep'=> { 330'sub'=>sub{ feature_bool('grep',@_) }, 331'override'=>0, 332'default'=> [1]}, 333 334# Enable the pickaxe search, which will list the commits that modified 335# a given string in a file. This can be practical and quite faster 336# alternative to 'blame', but still potentially CPU-intensive. 337 338# To enable system wide have in $GITWEB_CONFIG 339# $feature{'pickaxe'}{'default'} = [1]; 340# To have project specific config enable override in $GITWEB_CONFIG 341# $feature{'pickaxe'}{'override'} = 1; 342# and in project config gitweb.pickaxe = 0|1; 343'pickaxe'=> { 344'sub'=>sub{ feature_bool('pickaxe',@_) }, 345'override'=>0, 346'default'=> [1]}, 347 348# Enable showing size of blobs in a 'tree' view, in a separate 349# column, similar to what 'ls -l' does. This cost a bit of IO. 350 351# To disable system wide have in $GITWEB_CONFIG 352# $feature{'show-sizes'}{'default'} = [0]; 353# To have project specific config enable override in $GITWEB_CONFIG 354# $feature{'show-sizes'}{'override'} = 1; 355# and in project config gitweb.showsizes = 0|1; 356'show-sizes'=> { 357'sub'=>sub{ feature_bool('showsizes',@_) }, 358'override'=>0, 359'default'=> [1]}, 360 361# Make gitweb use an alternative format of the URLs which can be 362# more readable and natural-looking: project name is embedded 363# directly in the path and the query string contains other 364# auxiliary information. All gitweb installations recognize 365# URL in either format; this configures in which formats gitweb 366# generates links. 367 368# To enable system wide have in $GITWEB_CONFIG 369# $feature{'pathinfo'}{'default'} = [1]; 370# Project specific override is not supported. 371 372# Note that you will need to change the default location of CSS, 373# favicon, logo and possibly other files to an absolute URL. Also, 374# if gitweb.cgi serves as your indexfile, you will need to force 375# $my_uri to contain the script name in your $GITWEB_CONFIG. 376'pathinfo'=> { 377'override'=>0, 378'default'=> [0]}, 379 380# Make gitweb consider projects in project root subdirectories 381# to be forks of existing projects. Given project $projname.git, 382# projects matching $projname/*.git will not be shown in the main 383# projects list, instead a '+' mark will be added to $projname 384# there and a 'forks' view will be enabled for the project, listing 385# all the forks. If project list is taken from a file, forks have 386# to be listed after the main project. 387 388# To enable system wide have in $GITWEB_CONFIG 389# $feature{'forks'}{'default'} = [1]; 390# Project specific override is not supported. 391'forks'=> { 392'override'=>0, 393'default'=> [0]}, 394 395# Insert custom links to the action bar of all project pages. 396# This enables you mainly to link to third-party scripts integrating 397# into gitweb; e.g. git-browser for graphical history representation 398# or custom web-based repository administration interface. 399 400# The 'default' value consists of a list of triplets in the form 401# (label, link, position) where position is the label after which 402# to insert the link and link is a format string where %n expands 403# to the project name, %f to the project path within the filesystem, 404# %h to the current hash (h gitweb parameter) and %b to the current 405# hash base (hb gitweb parameter); %% expands to %. 406 407# To enable system wide have in $GITWEB_CONFIG e.g. 408# $feature{'actions'}{'default'} = [('graphiclog', 409# '/git-browser/by-commit.html?r=%n', 'summary')]; 410# Project specific override is not supported. 411'actions'=> { 412'override'=>0, 413'default'=> []}, 414 415# Allow gitweb scan project content tags of project repository, 416# and display the popular Web 2.0-ish "tag cloud" near the projects 417# list. Note that this is something COMPLETELY different from the 418# normal Git tags. 419 420# gitweb by itself can show existing tags, but it does not handle 421# tagging itself; you need to do it externally, outside gitweb. 422# The format is described in git_get_project_ctags() subroutine. 423# You may want to install the HTML::TagCloud Perl module to get 424# a pretty tag cloud instead of just a list of tags. 425 426# To enable system wide have in $GITWEB_CONFIG 427# $feature{'ctags'}{'default'} = [1]; 428# Project specific override is not supported. 429 430# In the future whether ctags editing is enabled might depend 431# on the value, but using 1 should always mean no editing of ctags. 432'ctags'=> { 433'override'=>0, 434'default'=> [0]}, 435 436# The maximum number of patches in a patchset generated in patch 437# view. Set this to 0 or undef to disable patch view, or to a 438# negative number to remove any limit. 439 440# To disable system wide have in $GITWEB_CONFIG 441# $feature{'patches'}{'default'} = [0]; 442# To have project specific config enable override in $GITWEB_CONFIG 443# $feature{'patches'}{'override'} = 1; 444# and in project config gitweb.patches = 0|n; 445# where n is the maximum number of patches allowed in a patchset. 446'patches'=> { 447'sub'=> \&feature_patches, 448'override'=>0, 449'default'=> [16]}, 450 451# Avatar support. When this feature is enabled, views such as 452# shortlog or commit will display an avatar associated with 453# the email of the committer(s) and/or author(s). 454 455# Currently available providers are gravatar and picon. 456# If an unknown provider is specified, the feature is disabled. 457 458# Gravatar depends on Digest::MD5. 459# Picon currently relies on the indiana.edu database. 460 461# To enable system wide have in $GITWEB_CONFIG 462# $feature{'avatar'}{'default'} = ['<provider>']; 463# where <provider> is either gravatar or picon. 464# To have project specific config enable override in $GITWEB_CONFIG 465# $feature{'avatar'}{'override'} = 1; 466# and in project config gitweb.avatar = <provider>; 467'avatar'=> { 468'sub'=> \&feature_avatar, 469'override'=>0, 470'default'=> ['']}, 471 472# Enable displaying how much time and how many git commands 473# it took to generate and display page. Disabled by default. 474# Project specific override is not supported. 475'timed'=> { 476'override'=>0, 477'default'=> [0]}, 478 479# Enable turning some links into links to actions which require 480# JavaScript to run (like 'blame_incremental'). Not enabled by 481# default. Project specific override is currently not supported. 482'javascript-actions'=> { 483'override'=>0, 484'default'=> [0]}, 485 486# Syntax highlighting support. This is based on Daniel Svensson's 487# and Sham Chukoury's work in gitweb-xmms2.git. 488# It requires the 'highlight' program present in $PATH, 489# and therefore is disabled by default. 490 491# To enable system wide have in $GITWEB_CONFIG 492# $feature{'highlight'}{'default'} = [1]; 493 494'highlight'=> { 495'sub'=>sub{ feature_bool('highlight',@_) }, 496'override'=>0, 497'default'=> [0]}, 498 499# Enable displaying of remote heads in the heads list 500 501# To enable system wide have in $GITWEB_CONFIG 502# $feature{'remote_heads'}{'default'} = [1]; 503# To have project specific config enable override in $GITWEB_CONFIG 504# $feature{'remote_heads'}{'override'} = 1; 505# and in project config gitweb.remote_heads = 0|1; 506'remote_heads'=> { 507'sub'=>sub{ feature_bool('remote_heads',@_) }, 508'override'=>0, 509'default'=> [0]}, 510); 511 512sub gitweb_get_feature { 513my($name) =@_; 514return unlessexists$feature{$name}; 515my($sub,$override,@defaults) = ( 516$feature{$name}{'sub'}, 517$feature{$name}{'override'}, 518@{$feature{$name}{'default'}}); 519# project specific override is possible only if we have project 520our$git_dir;# global variable, declared later 521if(!$override|| !defined$git_dir) { 522return@defaults; 523} 524if(!defined$sub) { 525warn"feature$nameis not overridable"; 526return@defaults; 527} 528return$sub->(@defaults); 529} 530 531# A wrapper to check if a given feature is enabled. 532# With this, you can say 533# 534# my $bool_feat = gitweb_check_feature('bool_feat'); 535# gitweb_check_feature('bool_feat') or somecode; 536# 537# instead of 538# 539# my ($bool_feat) = gitweb_get_feature('bool_feat'); 540# (gitweb_get_feature('bool_feat'))[0] or somecode; 541# 542sub gitweb_check_feature { 543return(gitweb_get_feature(@_))[0]; 544} 545 546 547sub feature_bool { 548my$key=shift; 549my($val) = git_get_project_config($key,'--bool'); 550 551if(!defined$val) { 552return($_[0]); 553}elsif($valeq'true') { 554return(1); 555}elsif($valeq'false') { 556return(0); 557} 558} 559 560sub feature_snapshot { 561my(@fmts) =@_; 562 563my($val) = git_get_project_config('snapshot'); 564 565if($val) { 566@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 567} 568 569return@fmts; 570} 571 572sub feature_patches { 573my@val= (git_get_project_config('patches','--int')); 574 575if(@val) { 576return@val; 577} 578 579return($_[0]); 580} 581 582sub feature_avatar { 583my@val= (git_get_project_config('avatar')); 584 585return@val?@val:@_; 586} 587 588# checking HEAD file with -e is fragile if the repository was 589# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 590# and then pruned. 591sub check_head_link { 592my($dir) =@_; 593my$headfile="$dir/HEAD"; 594return((-e $headfile) || 595(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 596} 597 598sub check_export_ok { 599my($dir) =@_; 600return(check_head_link($dir) && 601(!$export_ok|| -e "$dir/$export_ok") && 602(!$export_auth_hook||$export_auth_hook->($dir))); 603} 604 605# process alternate names for backward compatibility 606# filter out unsupported (unknown) snapshot formats 607sub filter_snapshot_fmts { 608my@fmts=@_; 609 610@fmts=map{ 611exists$known_snapshot_format_aliases{$_} ? 612$known_snapshot_format_aliases{$_} :$_}@fmts; 613@fmts=grep{ 614exists$known_snapshot_formats{$_} && 615!$known_snapshot_formats{$_}{'disabled'}}@fmts; 616} 617 618# If it is set to code reference, it is code that it is to be run once per 619# request, allowing updating configurations that change with each request, 620# while running other code in config file only once. 621# 622# Otherwise, if it is false then gitweb would process config file only once; 623# if it is true then gitweb config would be run for each request. 624our$per_request_config=1; 625 626our($GITWEB_CONFIG,$GITWEB_CONFIG_SYSTEM); 627sub evaluate_gitweb_config { 628our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 629our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 630# die if there are errors parsing config file 631if(-e $GITWEB_CONFIG) { 632do$GITWEB_CONFIG; 633die$@if$@; 634}elsif(-e $GITWEB_CONFIG_SYSTEM) { 635do$GITWEB_CONFIG_SYSTEM; 636die$@if$@; 637} 638} 639 640# Get loadavg of system, to compare against $maxload. 641# Currently it requires '/proc/loadavg' present to get loadavg; 642# if it is not present it returns 0, which means no load checking. 643sub get_loadavg { 644if( -e '/proc/loadavg'){ 645open my$fd,'<','/proc/loadavg' 646orreturn0; 647my@load=split(/\s+/,scalar<$fd>); 648close$fd; 649 650# The first three columns measure CPU and IO utilization of the last one, 651# five, and 10 minute periods. The fourth column shows the number of 652# currently running processes and the total number of processes in the m/n 653# format. The last column displays the last process ID used. 654return$load[0] ||0; 655} 656# additional checks for load average should go here for things that don't export 657# /proc/loadavg 658 659return0; 660} 661 662# version of the core git binary 663our$git_version; 664sub evaluate_git_version { 665our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 666$number_of_git_cmds++; 667} 668 669sub check_loadavg { 670if(defined$maxload&& get_loadavg() >$maxload) { 671 die_error(503,"The load average on the server is too high"); 672} 673} 674 675# ====================================================================== 676# input validation and dispatch 677 678# input parameters can be collected from a variety of sources (presently, CGI 679# and PATH_INFO), so we define an %input_params hash that collects them all 680# together during validation: this allows subsequent uses (e.g. href()) to be 681# agnostic of the parameter origin 682 683our%input_params= (); 684 685# input parameters are stored with the long parameter name as key. This will 686# also be used in the href subroutine to convert parameters to their CGI 687# equivalent, and since the href() usage is the most frequent one, we store 688# the name -> CGI key mapping here, instead of the reverse. 689# 690# XXX: Warning: If you touch this, check the search form for updating, 691# too. 692 693our@cgi_param_mapping= ( 694 project =>"p", 695 action =>"a", 696 file_name =>"f", 697 file_parent =>"fp", 698 hash =>"h", 699 hash_parent =>"hp", 700 hash_base =>"hb", 701 hash_parent_base =>"hpb", 702 page =>"pg", 703 order =>"o", 704 searchtext =>"s", 705 searchtype =>"st", 706 snapshot_format =>"sf", 707 extra_options =>"opt", 708 search_use_regexp =>"sr", 709 ctag =>"by_tag", 710# this must be last entry (for manipulation from JavaScript) 711 javascript =>"js" 712); 713our%cgi_param_mapping=@cgi_param_mapping; 714 715# we will also need to know the possible actions, for validation 716our%actions= ( 717"blame"=> \&git_blame, 718"blame_incremental"=> \&git_blame_incremental, 719"blame_data"=> \&git_blame_data, 720"blobdiff"=> \&git_blobdiff, 721"blobdiff_plain"=> \&git_blobdiff_plain, 722"blob"=> \&git_blob, 723"blob_plain"=> \&git_blob_plain, 724"commitdiff"=> \&git_commitdiff, 725"commitdiff_plain"=> \&git_commitdiff_plain, 726"commit"=> \&git_commit, 727"forks"=> \&git_forks, 728"heads"=> \&git_heads, 729"history"=> \&git_history, 730"log"=> \&git_log, 731"patch"=> \&git_patch, 732"patches"=> \&git_patches, 733"remotes"=> \&git_remotes, 734"rss"=> \&git_rss, 735"atom"=> \&git_atom, 736"search"=> \&git_search, 737"search_help"=> \&git_search_help, 738"shortlog"=> \&git_shortlog, 739"summary"=> \&git_summary, 740"tag"=> \&git_tag, 741"tags"=> \&git_tags, 742"tree"=> \&git_tree, 743"snapshot"=> \&git_snapshot, 744"object"=> \&git_object, 745# those below don't need $project 746"opml"=> \&git_opml, 747"project_list"=> \&git_project_list, 748"project_index"=> \&git_project_index, 749); 750 751# finally, we have the hash of allowed extra_options for the commands that 752# allow them 753our%allowed_options= ( 754"--no-merges"=> [qw(rss atom log shortlog history)], 755); 756 757# fill %input_params with the CGI parameters. All values except for 'opt' 758# should be single values, but opt can be an array. We should probably 759# build an array of parameters that can be multi-valued, but since for the time 760# being it's only this one, we just single it out 761sub evaluate_query_params { 762our$cgi; 763 764while(my($name,$symbol) =each%cgi_param_mapping) { 765if($symboleq'opt') { 766$input_params{$name} = [$cgi->param($symbol) ]; 767}else{ 768$input_params{$name} =$cgi->param($symbol); 769} 770} 771} 772 773# now read PATH_INFO and update the parameter list for missing parameters 774sub evaluate_path_info { 775return ifdefined$input_params{'project'}; 776return if!$path_info; 777$path_info=~ s,^/+,,; 778return if!$path_info; 779 780# find which part of PATH_INFO is project 781my$project=$path_info; 782$project=~ s,/+$,,; 783while($project&& !check_head_link("$projectroot/$project")) { 784$project=~ s,/*[^/]*$,,; 785} 786return unless$project; 787$input_params{'project'} =$project; 788 789# do not change any parameters if an action is given using the query string 790return if$input_params{'action'}; 791$path_info=~ s,^\Q$project\E/*,,; 792 793# next, check if we have an action 794my$action=$path_info; 795$action=~ s,/.*$,,; 796if(exists$actions{$action}) { 797$path_info=~ s,^$action/*,,; 798$input_params{'action'} =$action; 799} 800 801# list of actions that want hash_base instead of hash, but can have no 802# pathname (f) parameter 803my@wants_base= ( 804'tree', 805'history', 806); 807 808# we want to catch, among others 809# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 810my($parentrefname,$parentpathname,$refname,$pathname) = 811($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/); 812 813# first, analyze the 'current' part 814if(defined$pathname) { 815# we got "branch:filename" or "branch:dir/" 816# we could use git_get_type(branch:pathname), but: 817# - it needs $git_dir 818# - it does a git() call 819# - the convention of terminating directories with a slash 820# makes it superfluous 821# - embedding the action in the PATH_INFO would make it even 822# more superfluous 823$pathname=~ s,^/+,,; 824if(!$pathname||substr($pathname, -1)eq"/") { 825$input_params{'action'} ||="tree"; 826$pathname=~ s,/$,,; 827}else{ 828# the default action depends on whether we had parent info 829# or not 830if($parentrefname) { 831$input_params{'action'} ||="blobdiff_plain"; 832}else{ 833$input_params{'action'} ||="blob_plain"; 834} 835} 836$input_params{'hash_base'} ||=$refname; 837$input_params{'file_name'} ||=$pathname; 838}elsif(defined$refname) { 839# we got "branch". In this case we have to choose if we have to 840# set hash or hash_base. 841# 842# Most of the actions without a pathname only want hash to be 843# set, except for the ones specified in @wants_base that want 844# hash_base instead. It should also be noted that hand-crafted 845# links having 'history' as an action and no pathname or hash 846# set will fail, but that happens regardless of PATH_INFO. 847if(defined$parentrefname) { 848# if there is parent let the default be 'shortlog' action 849# (for http://git.example.com/repo.git/A..B links); if there 850# is no parent, dispatch will detect type of object and set 851# action appropriately if required (if action is not set) 852$input_params{'action'} ||="shortlog"; 853} 854if($input_params{'action'} && 855grep{$_eq$input_params{'action'} }@wants_base) { 856$input_params{'hash_base'} ||=$refname; 857}else{ 858$input_params{'hash'} ||=$refname; 859} 860} 861 862# next, handle the 'parent' part, if present 863if(defined$parentrefname) { 864# a missing pathspec defaults to the 'current' filename, allowing e.g. 865# someproject/blobdiff/oldrev..newrev:/filename 866if($parentpathname) { 867$parentpathname=~ s,^/+,,; 868$parentpathname=~ s,/$,,; 869$input_params{'file_parent'} ||=$parentpathname; 870}else{ 871$input_params{'file_parent'} ||=$input_params{'file_name'}; 872} 873# we assume that hash_parent_base is wanted if a path was specified, 874# or if the action wants hash_base instead of hash 875if(defined$input_params{'file_parent'} || 876grep{$_eq$input_params{'action'} }@wants_base) { 877$input_params{'hash_parent_base'} ||=$parentrefname; 878}else{ 879$input_params{'hash_parent'} ||=$parentrefname; 880} 881} 882 883# for the snapshot action, we allow URLs in the form 884# $project/snapshot/$hash.ext 885# where .ext determines the snapshot and gets removed from the 886# passed $refname to provide the $hash. 887# 888# To be able to tell that $refname includes the format extension, we 889# require the following two conditions to be satisfied: 890# - the hash input parameter MUST have been set from the $refname part 891# of the URL (i.e. they must be equal) 892# - the snapshot format MUST NOT have been defined already (e.g. from 893# CGI parameter sf) 894# It's also useless to try any matching unless $refname has a dot, 895# so we check for that too 896if(defined$input_params{'action'} && 897$input_params{'action'}eq'snapshot'&& 898defined$refname&&index($refname,'.') != -1&& 899$refnameeq$input_params{'hash'} && 900!defined$input_params{'snapshot_format'}) { 901# We loop over the known snapshot formats, checking for 902# extensions. Allowed extensions are both the defined suffix 903# (which includes the initial dot already) and the snapshot 904# format key itself, with a prepended dot 905while(my($fmt,$opt) =each%known_snapshot_formats) { 906my$hash=$refname; 907unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 908next; 909} 910my$sfx=$1; 911# a valid suffix was found, so set the snapshot format 912# and reset the hash parameter 913$input_params{'snapshot_format'} =$fmt; 914$input_params{'hash'} =$hash; 915# we also set the format suffix to the one requested 916# in the URL: this way a request for e.g. .tgz returns 917# a .tgz instead of a .tar.gz 918$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 919last; 920} 921} 922} 923 924our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 925$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 926$searchtext,$search_regexp); 927sub evaluate_and_validate_params { 928our$action=$input_params{'action'}; 929if(defined$action) { 930if(!validate_action($action)) { 931 die_error(400,"Invalid action parameter"); 932} 933} 934 935# parameters which are pathnames 936our$project=$input_params{'project'}; 937if(defined$project) { 938if(!validate_project($project)) { 939undef$project; 940 die_error(404,"No such project"); 941} 942} 943 944our$file_name=$input_params{'file_name'}; 945if(defined$file_name) { 946if(!validate_pathname($file_name)) { 947 die_error(400,"Invalid file parameter"); 948} 949} 950 951our$file_parent=$input_params{'file_parent'}; 952if(defined$file_parent) { 953if(!validate_pathname($file_parent)) { 954 die_error(400,"Invalid file parent parameter"); 955} 956} 957 958# parameters which are refnames 959our$hash=$input_params{'hash'}; 960if(defined$hash) { 961if(!validate_refname($hash)) { 962 die_error(400,"Invalid hash parameter"); 963} 964} 965 966our$hash_parent=$input_params{'hash_parent'}; 967if(defined$hash_parent) { 968if(!validate_refname($hash_parent)) { 969 die_error(400,"Invalid hash parent parameter"); 970} 971} 972 973our$hash_base=$input_params{'hash_base'}; 974if(defined$hash_base) { 975if(!validate_refname($hash_base)) { 976 die_error(400,"Invalid hash base parameter"); 977} 978} 979 980our@extra_options= @{$input_params{'extra_options'}}; 981# @extra_options is always defined, since it can only be (currently) set from 982# CGI, and $cgi->param() returns the empty array in array context if the param 983# is not set 984foreachmy$opt(@extra_options) { 985if(not exists$allowed_options{$opt}) { 986 die_error(400,"Invalid option parameter"); 987} 988if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 989 die_error(400,"Invalid option parameter for this action"); 990} 991} 992 993our$hash_parent_base=$input_params{'hash_parent_base'}; 994if(defined$hash_parent_base) { 995if(!validate_refname($hash_parent_base)) { 996 die_error(400,"Invalid hash parent base parameter"); 997} 998} 9991000# other parameters1001our$page=$input_params{'page'};1002if(defined$page) {1003if($page=~m/[^0-9]/) {1004 die_error(400,"Invalid page parameter");1005}1006}10071008our$searchtype=$input_params{'searchtype'};1009if(defined$searchtype) {1010if($searchtype=~m/[^a-z]/) {1011 die_error(400,"Invalid searchtype parameter");1012}1013}10141015our$search_use_regexp=$input_params{'search_use_regexp'};10161017our$searchtext=$input_params{'searchtext'};1018our$search_regexp;1019if(defined$searchtext) {1020if(length($searchtext) <2) {1021 die_error(403,"At least two characters are required for search parameter");1022}1023$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext;1024}1025}10261027# path to the current git repository1028our$git_dir;1029sub evaluate_git_dir {1030our$git_dir="$projectroot/$project"if$project;1031}10321033our(@snapshot_fmts,$git_avatar);1034sub configure_gitweb_features {1035# list of supported snapshot formats1036our@snapshot_fmts= gitweb_get_feature('snapshot');1037@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);10381039# check that the avatar feature is set to a known provider name,1040# and for each provider check if the dependencies are satisfied.1041# if the provider name is invalid or the dependencies are not met,1042# reset $git_avatar to the empty string.1043our($git_avatar) = gitweb_get_feature('avatar');1044if($git_avatareq'gravatar') {1045$git_avatar=''unless(eval{require Digest::MD5;1; });1046}elsif($git_avatareq'picon') {1047# no dependencies1048}else{1049$git_avatar='';1050}1051}10521053# custom error handler: 'die <message>' is Internal Server Error1054sub handle_errors_html {1055my$msg=shift;# it is already HTML escaped10561057# to avoid infinite loop where error occurs in die_error,1058# change handler to default handler, disabling handle_errors_html1059 set_message("Error occured when inside die_error:\n$msg");10601061# you cannot jump out of die_error when called as error handler;1062# the subroutine set via CGI::Carp::set_message is called _after_1063# HTTP headers are already written, so it cannot write them itself1064 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1);1065}1066set_message(\&handle_errors_html);10671068# dispatch1069sub dispatch {1070if(!defined$action) {1071if(defined$hash) {1072$action= git_get_type($hash);1073}elsif(defined$hash_base&&defined$file_name) {1074$action= git_get_type("$hash_base:$file_name");1075}elsif(defined$project) {1076$action='summary';1077}else{1078$action='project_list';1079}1080}1081if(!defined($actions{$action})) {1082 die_error(400,"Unknown action");1083}1084if($action!~m/^(?:opml|project_list|project_index)$/&&1085!$project) {1086 die_error(400,"Project needed");1087}1088$actions{$action}->();1089}10901091sub reset_timer {1092our$t0= [ gettimeofday() ]1093ifdefined$t0;1094our$number_of_git_cmds=0;1095}10961097our$first_request=1;1098sub run_request {1099 reset_timer();11001101 evaluate_uri();1102if($first_request) {1103 evaluate_gitweb_config();1104 evaluate_git_version();1105}1106if($per_request_config) {1107if(ref($per_request_config)eq'CODE') {1108$per_request_config->();1109}elsif(!$first_request) {1110 evaluate_gitweb_config();1111}1112}1113 check_loadavg();11141115# $projectroot and $projects_list might be set in gitweb config file1116$projects_list||=$projectroot;11171118 evaluate_query_params();1119 evaluate_path_info();1120 evaluate_and_validate_params();1121 evaluate_git_dir();11221123 configure_gitweb_features();11241125 dispatch();1126}11271128our$is_last_request=sub{1};1129our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1130our$CGI='CGI';1131our$cgi;1132sub configure_as_fcgi {1133require CGI::Fast;1134our$CGI='CGI::Fast';11351136my$request_number=0;1137# let each child service 100 requests1138our$is_last_request=sub{ ++$request_number>100};1139}1140sub evaluate_argv {1141my$script_name=$ENV{'SCRIPT_NAME'} ||$ENV{'SCRIPT_FILENAME'} || __FILE__;1142 configure_as_fcgi()1143if$script_name=~/\.fcgi$/;11441145return unless(@ARGV);11461147require Getopt::Long;1148 Getopt::Long::GetOptions(1149'fastcgi|fcgi|f'=> \&configure_as_fcgi,1150'nproc|n=i'=>sub{1151my($arg,$val) =@_;1152return unlesseval{require FCGI::ProcManager;1; };1153my$proc_manager= FCGI::ProcManager->new({1154 n_processes =>$val,1155});1156our$pre_listen_hook=sub{$proc_manager->pm_manage() };1157our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1158our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1159},1160);1161}11621163sub run {1164 evaluate_argv();11651166$first_request=1;1167$pre_listen_hook->()1168if$pre_listen_hook;11691170 REQUEST:1171while($cgi=$CGI->new()) {1172$pre_dispatch_hook->()1173if$pre_dispatch_hook;11741175 run_request();11761177$post_dispatch_hook->()1178if$post_dispatch_hook;1179$first_request=0;11801181last REQUEST if($is_last_request->());1182}11831184 DONE_GITWEB:11851;1186}11871188run();11891190if(defined caller) {1191# wrapped in a subroutine processing requests,1192# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1193return;1194}else{1195# pure CGI script, serving single request1196exit;1197}11981199## ======================================================================1200## action links12011202# possible values of extra options1203# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1204# -replay => 1 - start from a current view (replay with modifications)1205# -path_info => 0|1 - don't use/use path_info URL (if possible)1206# -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone1207sub href {1208my%params=@_;1209# default is to use -absolute url() i.e. $my_uri1210my$href=$params{-full} ?$my_url:$my_uri;12111212# implicit -replay, must be first of implicit params1213$params{-replay} =1if(keys%params==1&&$params{-anchor});12141215$params{'project'} =$projectunlessexists$params{'project'};12161217if($params{-replay}) {1218while(my($name,$symbol) =each%cgi_param_mapping) {1219if(!exists$params{$name}) {1220$params{$name} =$input_params{$name};1221}1222}1223}12241225my$use_pathinfo= gitweb_check_feature('pathinfo');1226if(defined$params{'project'} &&1227(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1228# try to put as many parameters as possible in PATH_INFO:1229# - project name1230# - action1231# - hash_parent or hash_parent_base:/file_parent1232# - hash or hash_base:/filename1233# - the snapshot_format as an appropriate suffix12341235# When the script is the root DirectoryIndex for the domain,1236# $href here would be something like http://gitweb.example.com/1237# Thus, we strip any trailing / from $href, to spare us double1238# slashes in the final URL1239$href=~ s,/$,,;12401241# Then add the project name, if present1242$href.="/".esc_path_info($params{'project'});1243delete$params{'project'};12441245# since we destructively absorb parameters, we keep this1246# boolean that remembers if we're handling a snapshot1247my$is_snapshot=$params{'action'}eq'snapshot';12481249# Summary just uses the project path URL, any other action is1250# added to the URL1251if(defined$params{'action'}) {1252$href.="/".esc_path_info($params{'action'})1253unless$params{'action'}eq'summary';1254delete$params{'action'};1255}12561257# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1258# stripping nonexistent or useless pieces1259$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1260||$params{'hash_parent'} ||$params{'hash'});1261if(defined$params{'hash_base'}) {1262if(defined$params{'hash_parent_base'}) {1263$href.= esc_path_info($params{'hash_parent_base'});1264# skip the file_parent if it's the same as the file_name1265if(defined$params{'file_parent'}) {1266if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1267delete$params{'file_parent'};1268}elsif($params{'file_parent'} !~/\.\./) {1269$href.=":/".esc_path_info($params{'file_parent'});1270delete$params{'file_parent'};1271}1272}1273$href.="..";1274delete$params{'hash_parent'};1275delete$params{'hash_parent_base'};1276}elsif(defined$params{'hash_parent'}) {1277$href.= esc_path_info($params{'hash_parent'})."..";1278delete$params{'hash_parent'};1279}12801281$href.= esc_path_info($params{'hash_base'});1282if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1283$href.=":/".esc_path_info($params{'file_name'});1284delete$params{'file_name'};1285}1286delete$params{'hash'};1287delete$params{'hash_base'};1288}elsif(defined$params{'hash'}) {1289$href.= esc_path_info($params{'hash'});1290delete$params{'hash'};1291}12921293# If the action was a snapshot, we can absorb the1294# snapshot_format parameter too1295if($is_snapshot) {1296my$fmt=$params{'snapshot_format'};1297# snapshot_format should always be defined when href()1298# is called, but just in case some code forgets, we1299# fall back to the default1300$fmt||=$snapshot_fmts[0];1301$href.=$known_snapshot_formats{$fmt}{'suffix'};1302delete$params{'snapshot_format'};1303}1304}13051306# now encode the parameters explicitly1307my@result= ();1308for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1309my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1310if(defined$params{$name}) {1311if(ref($params{$name})eq"ARRAY") {1312foreachmy$par(@{$params{$name}}) {1313push@result,$symbol."=". esc_param($par);1314}1315}else{1316push@result,$symbol."=". esc_param($params{$name});1317}1318}1319}1320$href.="?".join(';',@result)ifscalar@result;13211322# final transformation: trailing spaces must be escaped (URI-encoded)1323$href=~s/(\s+)$/CGI::escape($1)/e;13241325if($params{-anchor}) {1326$href.="#".esc_param($params{-anchor});1327}13281329return$href;1330}133113321333## ======================================================================1334## validation, quoting/unquoting and escaping13351336sub validate_action {1337my$input=shift||returnundef;1338returnundefunlessexists$actions{$input};1339return$input;1340}13411342sub validate_project {1343my$input=shift||returnundef;1344if(!validate_pathname($input) ||1345!(-d "$projectroot/$input") ||1346!check_export_ok("$projectroot/$input") ||1347($strict_export&& !project_in_list($input))) {1348returnundef;1349}else{1350return$input;1351}1352}13531354sub validate_pathname {1355my$input=shift||returnundef;13561357# no '.' or '..' as elements of path, i.e. no '.' nor '..'1358# at the beginning, at the end, and between slashes.1359# also this catches doubled slashes1360if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1361returnundef;1362}1363# no null characters1364if($input=~m!\0!) {1365returnundef;1366}1367return$input;1368}13691370sub validate_refname {1371my$input=shift||returnundef;13721373# textual hashes are O.K.1374if($input=~m/^[0-9a-fA-F]{40}$/) {1375return$input;1376}1377# it must be correct pathname1378$input= validate_pathname($input)1379orreturnundef;1380# restrictions on ref name according to git-check-ref-format1381if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1382returnundef;1383}1384return$input;1385}13861387# decode sequences of octets in utf8 into Perl's internal form,1388# which is utf-8 with utf8 flag set if needed. gitweb writes out1389# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1390sub to_utf8 {1391my$str=shift;1392returnundefunlessdefined$str;1393if(utf8::valid($str)) {1394 utf8::decode($str);1395return$str;1396}else{1397return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1398}1399}14001401# quote unsafe chars, but keep the slash, even when it's not1402# correct, but quoted slashes look too horrible in bookmarks1403sub esc_param {1404my$str=shift;1405returnundefunlessdefined$str;1406$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1407$str=~s/ /\+/g;1408return$str;1409}14101411# the quoting rules for path_info fragment are slightly different1412sub esc_path_info {1413my$str=shift;1414returnundefunlessdefined$str;14151416# path_info doesn't treat '+' as space (specially), but '?' must be escaped1417$str=~s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;14181419return$str;1420}14211422# quote unsafe chars in whole URL, so some characters cannot be quoted1423sub esc_url {1424my$str=shift;1425returnundefunlessdefined$str;1426$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;1427$str=~s/ /\+/g;1428return$str;1429}14301431# quote unsafe characters in HTML attributes1432sub esc_attr {14331434# for XHTML conformance escaping '"' to '"' is not enough1435return esc_html(@_);1436}14371438# replace invalid utf8 character with SUBSTITUTION sequence1439sub esc_html {1440my$str=shift;1441my%opts=@_;14421443returnundefunlessdefined$str;14441445$str= to_utf8($str);1446$str=$cgi->escapeHTML($str);1447if($opts{'-nbsp'}) {1448$str=~s/ / /g;1449}1450$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1451return$str;1452}14531454# quote control characters and escape filename to HTML1455sub esc_path {1456my$str=shift;1457my%opts=@_;14581459returnundefunlessdefined$str;14601461$str= to_utf8($str);1462$str=$cgi->escapeHTML($str);1463if($opts{'-nbsp'}) {1464$str=~s/ / /g;1465}1466$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1467return$str;1468}14691470# Make control characters "printable", using character escape codes (CEC)1471sub quot_cec {1472my$cntrl=shift;1473my%opts=@_;1474my%es= (# character escape codes, aka escape sequences1475"\t"=>'\t',# tab (HT)1476"\n"=>'\n',# line feed (LF)1477"\r"=>'\r',# carrige return (CR)1478"\f"=>'\f',# form feed (FF)1479"\b"=>'\b',# backspace (BS)1480"\a"=>'\a',# alarm (bell) (BEL)1481"\e"=>'\e',# escape (ESC)1482"\013"=>'\v',# vertical tab (VT)1483"\000"=>'\0',# nul character (NUL)1484);1485my$chr= ( (exists$es{$cntrl})1486?$es{$cntrl}1487:sprintf('\%2x',ord($cntrl)) );1488if($opts{-nohtml}) {1489return$chr;1490}else{1491return"<span class=\"cntrl\">$chr</span>";1492}1493}14941495# Alternatively use unicode control pictures codepoints,1496# Unicode "printable representation" (PR)1497sub quot_upr {1498my$cntrl=shift;1499my%opts=@_;15001501my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1502if($opts{-nohtml}) {1503return$chr;1504}else{1505return"<span class=\"cntrl\">$chr</span>";1506}1507}15081509# git may return quoted and escaped filenames1510sub unquote {1511my$str=shift;15121513sub unq {1514my$seq=shift;1515my%es= (# character escape codes, aka escape sequences1516't'=>"\t",# tab (HT, TAB)1517'n'=>"\n",# newline (NL)1518'r'=>"\r",# return (CR)1519'f'=>"\f",# form feed (FF)1520'b'=>"\b",# backspace (BS)1521'a'=>"\a",# alarm (bell) (BEL)1522'e'=>"\e",# escape (ESC)1523'v'=>"\013",# vertical tab (VT)1524);15251526if($seq=~m/^[0-7]{1,3}$/) {1527# octal char sequence1528returnchr(oct($seq));1529}elsif(exists$es{$seq}) {1530# C escape sequence, aka character escape code1531return$es{$seq};1532}1533# quoted ordinary character1534return$seq;1535}15361537if($str=~m/^"(.*)"$/) {1538# needs unquoting1539$str=$1;1540$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1541}1542return$str;1543}15441545# escape tabs (convert tabs to spaces)1546sub untabify {1547my$line=shift;15481549while((my$pos=index($line,"\t")) != -1) {1550if(my$count= (8- ($pos%8))) {1551my$spaces=' ' x $count;1552$line=~s/\t/$spaces/;1553}1554}15551556return$line;1557}15581559sub project_in_list {1560my$project=shift;1561my@list= git_get_projects_list();1562return@list&&scalar(grep{$_->{'path'}eq$project}@list);1563}15641565## ----------------------------------------------------------------------1566## HTML aware string manipulation15671568# Try to chop given string on a word boundary between position1569# $len and $len+$add_len. If there is no word boundary there,1570# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1571# (marking chopped part) would be longer than given string.1572sub chop_str {1573my$str=shift;1574my$len=shift;1575my$add_len=shift||10;1576my$where=shift||'right';# 'left' | 'center' | 'right'15771578# Make sure perl knows it is utf8 encoded so we don't1579# cut in the middle of a utf8 multibyte char.1580$str= to_utf8($str);15811582# allow only $len chars, but don't cut a word if it would fit in $add_len1583# if it doesn't fit, cut it if it's still longer than the dots we would add1584# remove chopped character entities entirely15851586# when chopping in the middle, distribute $len into left and right part1587# return early if chopping wouldn't make string shorter1588if($whereeq'center') {1589return$strif($len+5>=length($str));# filler is length 51590$len=int($len/2);1591}else{1592return$strif($len+4>=length($str));# filler is length 41593}15941595# regexps: ending and beginning with word part up to $add_len1596my$endre=qr/.{$len}\w{0,$add_len}/;1597my$begre=qr/\w{0,$add_len}.{$len}/;15981599if($whereeq'left') {1600$str=~m/^(.*?)($begre)$/;1601my($lead,$body) = ($1,$2);1602if(length($lead) >4) {1603$lead=" ...";1604}1605return"$lead$body";16061607}elsif($whereeq'center') {1608$str=~m/^($endre)(.*)$/;1609my($left,$str) = ($1,$2);1610$str=~m/^(.*?)($begre)$/;1611my($mid,$right) = ($1,$2);1612if(length($mid) >5) {1613$mid=" ... ";1614}1615return"$left$mid$right";16161617}else{1618$str=~m/^($endre)(.*)$/;1619my$body=$1;1620my$tail=$2;1621if(length($tail) >4) {1622$tail="... ";1623}1624return"$body$tail";1625}1626}16271628# takes the same arguments as chop_str, but also wraps a <span> around the1629# result with a title attribute if it does get chopped. Additionally, the1630# string is HTML-escaped.1631sub chop_and_escape_str {1632my($str) =@_;16331634my$chopped= chop_str(@_);1635if($choppedeq$str) {1636return esc_html($chopped);1637}else{1638$str=~s/[[:cntrl:]]/?/g;1639return$cgi->span({-title=>$str}, esc_html($chopped));1640}1641}16421643## ----------------------------------------------------------------------1644## functions returning short strings16451646# CSS class for given age value (in seconds)1647sub age_class {1648my$age=shift;16491650if(!defined$age) {1651return"noage";1652}elsif($age<60*60*2) {1653return"age0";1654}elsif($age<60*60*24*2) {1655return"age1";1656}else{1657return"age2";1658}1659}16601661# convert age in seconds to "nn units ago" string1662sub age_string {1663my$age=shift;1664my$age_str;16651666if($age>60*60*24*365*2) {1667$age_str= (int$age/60/60/24/365);1668$age_str.=" years ago";1669}elsif($age>60*60*24*(365/12)*2) {1670$age_str=int$age/60/60/24/(365/12);1671$age_str.=" months ago";1672}elsif($age>60*60*24*7*2) {1673$age_str=int$age/60/60/24/7;1674$age_str.=" weeks ago";1675}elsif($age>60*60*24*2) {1676$age_str=int$age/60/60/24;1677$age_str.=" days ago";1678}elsif($age>60*60*2) {1679$age_str=int$age/60/60;1680$age_str.=" hours ago";1681}elsif($age>60*2) {1682$age_str=int$age/60;1683$age_str.=" min ago";1684}elsif($age>2) {1685$age_str=int$age;1686$age_str.=" sec ago";1687}else{1688$age_str.=" right now";1689}1690return$age_str;1691}16921693useconstant{1694 S_IFINVALID =>0030000,1695 S_IFGITLINK =>0160000,1696};16971698# submodule/subproject, a commit object reference1699sub S_ISGITLINK {1700my$mode=shift;17011702return(($mode& S_IFMT) == S_IFGITLINK)1703}17041705# convert file mode in octal to symbolic file mode string1706sub mode_str {1707my$mode=oct shift;17081709if(S_ISGITLINK($mode)) {1710return'm---------';1711}elsif(S_ISDIR($mode& S_IFMT)) {1712return'drwxr-xr-x';1713}elsif(S_ISLNK($mode)) {1714return'lrwxrwxrwx';1715}elsif(S_ISREG($mode)) {1716# git cares only about the executable bit1717if($mode& S_IXUSR) {1718return'-rwxr-xr-x';1719}else{1720return'-rw-r--r--';1721};1722}else{1723return'----------';1724}1725}17261727# convert file mode in octal to file type string1728sub file_type {1729my$mode=shift;17301731if($mode!~m/^[0-7]+$/) {1732return$mode;1733}else{1734$mode=oct$mode;1735}17361737if(S_ISGITLINK($mode)) {1738return"submodule";1739}elsif(S_ISDIR($mode& S_IFMT)) {1740return"directory";1741}elsif(S_ISLNK($mode)) {1742return"symlink";1743}elsif(S_ISREG($mode)) {1744return"file";1745}else{1746return"unknown";1747}1748}17491750# convert file mode in octal to file type description string1751sub file_type_long {1752my$mode=shift;17531754if($mode!~m/^[0-7]+$/) {1755return$mode;1756}else{1757$mode=oct$mode;1758}17591760if(S_ISGITLINK($mode)) {1761return"submodule";1762}elsif(S_ISDIR($mode& S_IFMT)) {1763return"directory";1764}elsif(S_ISLNK($mode)) {1765return"symlink";1766}elsif(S_ISREG($mode)) {1767if($mode& S_IXUSR) {1768return"executable";1769}else{1770return"file";1771};1772}else{1773return"unknown";1774}1775}177617771778## ----------------------------------------------------------------------1779## functions returning short HTML fragments, or transforming HTML fragments1780## which don't belong to other sections17811782# format line of commit message.1783sub format_log_line_html {1784my$line=shift;17851786$line= esc_html($line, -nbsp=>1);1787$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1788$cgi->a({-href => href(action=>"object", hash=>$1),1789-class=>"text"},$1);1790}eg;17911792return$line;1793}17941795# format marker of refs pointing to given object17961797# the destination action is chosen based on object type and current context:1798# - for annotated tags, we choose the tag view unless it's the current view1799# already, in which case we go to shortlog view1800# - for other refs, we keep the current view if we're in history, shortlog or1801# log view, and select shortlog otherwise1802sub format_ref_marker {1803my($refs,$id) =@_;1804my$markers='';18051806if(defined$refs->{$id}) {1807foreachmy$ref(@{$refs->{$id}}) {1808# this code exploits the fact that non-lightweight tags are the1809# only indirect objects, and that they are the only objects for which1810# we want to use tag instead of shortlog as action1811my($type,$name) =qw();1812my$indirect= ($ref=~s/\^\{\}$//);1813# e.g. tags/v2.6.11 or heads/next1814if($ref=~m!^(.*?)s?/(.*)$!) {1815$type=$1;1816$name=$2;1817}else{1818$type="ref";1819$name=$ref;1820}18211822my$class=$type;1823$class.=" indirect"if$indirect;18241825my$dest_action="shortlog";18261827if($indirect) {1828$dest_action="tag"unless$actioneq"tag";1829}elsif($action=~/^(history|(short)?log)$/) {1830$dest_action=$action;1831}18321833my$dest="";1834$dest.="refs/"unless$ref=~ m!^refs/!;1835$dest.=$ref;18361837my$link=$cgi->a({1838-href => href(1839 action=>$dest_action,1840 hash=>$dest1841)},$name);18421843$markers.=" <span class=\"".esc_attr($class)."\"title=\"".esc_attr($ref)."\">".1844$link."</span>";1845}1846}18471848if($markers) {1849return' <span class="refs">'.$markers.'</span>';1850}else{1851return"";1852}1853}18541855# format, perhaps shortened and with markers, title line1856sub format_subject_html {1857my($long,$short,$href,$extra) =@_;1858$extra=''unlessdefined($extra);18591860if(length($short) <length($long)) {1861$long=~s/[[:cntrl:]]/?/g;1862return$cgi->a({-href =>$href, -class=>"list subject",1863-title => to_utf8($long)},1864 esc_html($short)) .$extra;1865}else{1866return$cgi->a({-href =>$href, -class=>"list subject"},1867 esc_html($long)) .$extra;1868}1869}18701871# Rather than recomputing the url for an email multiple times, we cache it1872# after the first hit. This gives a visible benefit in views where the avatar1873# for the same email is used repeatedly (e.g. shortlog).1874# The cache is shared by all avatar engines (currently gravatar only), which1875# are free to use it as preferred. Since only one avatar engine is used for any1876# given page, there's no risk for cache conflicts.1877our%avatar_cache= ();18781879# Compute the picon url for a given email, by using the picon search service over at1880# http://www.cs.indiana.edu/picons/search.html1881sub picon_url {1882my$email=lc shift;1883if(!$avatar_cache{$email}) {1884my($user,$domain) =split('@',$email);1885$avatar_cache{$email} =1886"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1887"$domain/$user/".1888"users+domains+unknown/up/single";1889}1890return$avatar_cache{$email};1891}18921893# Compute the gravatar url for a given email, if it's not in the cache already.1894# Gravatar stores only the part of the URL before the size, since that's the1895# one computationally more expensive. This also allows reuse of the cache for1896# different sizes (for this particular engine).1897sub gravatar_url {1898my$email=lc shift;1899my$size=shift;1900$avatar_cache{$email} ||=1901"http://www.gravatar.com/avatar/".1902 Digest::MD5::md5_hex($email) ."?s=";1903return$avatar_cache{$email} .$size;1904}19051906# Insert an avatar for the given $email at the given $size if the feature1907# is enabled.1908sub git_get_avatar {1909my($email,%opts) =@_;1910my$pre_white= ($opts{-pad_before} ?" ":"");1911my$post_white= ($opts{-pad_after} ?" ":"");1912$opts{-size} ||='default';1913my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1914my$url="";1915if($git_avatareq'gravatar') {1916$url= gravatar_url($email,$size);1917}elsif($git_avatareq'picon') {1918$url= picon_url($email);1919}1920# Other providers can be added by extending the if chain, defining $url1921# as needed. If no variant puts something in $url, we assume avatars1922# are completely disabled/unavailable.1923if($url) {1924return$pre_white.1925"<img width=\"$size\"".1926"class=\"avatar\"".1927"src=\"".esc_url($url)."\"".1928"alt=\"\"".1929"/>".$post_white;1930}else{1931return"";1932}1933}19341935sub format_search_author {1936my($author,$searchtype,$displaytext) =@_;1937my$have_search= gitweb_check_feature('search');19381939if($have_search) {1940my$performed="";1941if($searchtypeeq'author') {1942$performed="authored";1943}elsif($searchtypeeq'committer') {1944$performed="committed";1945}19461947return$cgi->a({-href => href(action=>"search", hash=>$hash,1948 searchtext=>$author,1949 searchtype=>$searchtype),class=>"list",1950 title=>"Search for commits$performedby$author"},1951$displaytext);19521953}else{1954return$displaytext;1955}1956}19571958# format the author name of the given commit with the given tag1959# the author name is chopped and escaped according to the other1960# optional parameters (see chop_str).1961sub format_author_html {1962my$tag=shift;1963my$co=shift;1964my$author= chop_and_escape_str($co->{'author_name'},@_);1965return"<$tagclass=\"author\">".1966 format_search_author($co->{'author_name'},"author",1967 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1968$author) .1969"</$tag>";1970}19711972# format git diff header line, i.e. "diff --(git|combined|cc) ..."1973sub format_git_diff_header_line {1974my$line=shift;1975my$diffinfo=shift;1976my($from,$to) =@_;19771978if($diffinfo->{'nparents'}) {1979# combined diff1980$line=~s!^(diff (.*?) )"?.*$!$1!;1981if($to->{'href'}) {1982$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1983 esc_path($to->{'file'}));1984}else{# file was deleted (no href)1985$line.= esc_path($to->{'file'});1986}1987}else{1988# "ordinary" diff1989$line=~s!^(diff (.*?) )"?a/.*$!$1!;1990if($from->{'href'}) {1991$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1992'a/'. esc_path($from->{'file'}));1993}else{# file was added (no href)1994$line.='a/'. esc_path($from->{'file'});1995}1996$line.=' ';1997if($to->{'href'}) {1998$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1999'b/'. esc_path($to->{'file'}));2000}else{# file was deleted2001$line.='b/'. esc_path($to->{'file'});2002}2003}20042005return"<div class=\"diff header\">$line</div>\n";2006}20072008# format extended diff header line, before patch itself2009sub format_extended_diff_header_line {2010my$line=shift;2011my$diffinfo=shift;2012my($from,$to) =@_;20132014# match <path>2015if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {2016$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},2017 esc_path($from->{'file'}));2018}2019if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {2020$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},2021 esc_path($to->{'file'}));2022}2023# match single <mode>2024if($line=~m/\s(\d{6})$/) {2025$line.='<span class="info"> ('.2026 file_type_long($1) .2027')</span>';2028}2029# match <hash>2030if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {2031# can match only for combined diff2032$line='index ';2033for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2034if($from->{'href'}[$i]) {2035$line.=$cgi->a({-href=>$from->{'href'}[$i],2036-class=>"hash"},2037substr($diffinfo->{'from_id'}[$i],0,7));2038}else{2039$line.='0' x 7;2040}2041# separator2042$line.=','if($i<$diffinfo->{'nparents'} -1);2043}2044$line.='..';2045if($to->{'href'}) {2046$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2047substr($diffinfo->{'to_id'},0,7));2048}else{2049$line.='0' x 7;2050}20512052}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {2053# can match only for ordinary diff2054my($from_link,$to_link);2055if($from->{'href'}) {2056$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},2057substr($diffinfo->{'from_id'},0,7));2058}else{2059$from_link='0' x 7;2060}2061if($to->{'href'}) {2062$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2063substr($diffinfo->{'to_id'},0,7));2064}else{2065$to_link='0' x 7;2066}2067my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});2068$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;2069}20702071return$line."<br/>\n";2072}20732074# format from-file/to-file diff header2075sub format_diff_from_to_header {2076my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;2077my$line;2078my$result='';20792080$line=$from_line;2081#assert($line =~ m/^---/) if DEBUG;2082# no extra formatting for "^--- /dev/null"2083if(!$diffinfo->{'nparents'}) {2084# ordinary (single parent) diff2085if($line=~m!^--- "?a/!) {2086if($from->{'href'}) {2087$line='--- a/'.2088$cgi->a({-href=>$from->{'href'}, -class=>"path"},2089 esc_path($from->{'file'}));2090}else{2091$line='--- a/'.2092 esc_path($from->{'file'});2093}2094}2095$result.= qq!<div class="diff from_file">$line</div>\n!;20962097}else{2098# combined diff (merge commit)2099for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2100if($from->{'href'}[$i]) {2101$line='--- '.2102$cgi->a({-href=>href(action=>"blobdiff",2103 hash_parent=>$diffinfo->{'from_id'}[$i],2104 hash_parent_base=>$parents[$i],2105 file_parent=>$from->{'file'}[$i],2106 hash=>$diffinfo->{'to_id'},2107 hash_base=>$hash,2108 file_name=>$to->{'file'}),2109-class=>"path",2110-title=>"diff". ($i+1)},2111$i+1) .2112'/'.2113$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2114 esc_path($from->{'file'}[$i]));2115}else{2116$line='--- /dev/null';2117}2118$result.= qq!<div class="diff from_file">$line</div>\n!;2119}2120}21212122$line=$to_line;2123#assert($line =~ m/^\+\+\+/) if DEBUG;2124# no extra formatting for "^+++ /dev/null"2125if($line=~m!^\+\+\+ "?b/!) {2126if($to->{'href'}) {2127$line='+++ b/'.2128$cgi->a({-href=>$to->{'href'}, -class=>"path"},2129 esc_path($to->{'file'}));2130}else{2131$line='+++ b/'.2132 esc_path($to->{'file'});2133}2134}2135$result.= qq!<div class="diff to_file">$line</div>\n!;21362137return$result;2138}21392140# create note for patch simplified by combined diff2141sub format_diff_cc_simplified {2142my($diffinfo,@parents) =@_;2143my$result='';21442145$result.="<div class=\"diff header\">".2146"diff --cc ";2147if(!is_deleted($diffinfo)) {2148$result.=$cgi->a({-href => href(action=>"blob",2149 hash_base=>$hash,2150 hash=>$diffinfo->{'to_id'},2151 file_name=>$diffinfo->{'to_file'}),2152-class=>"path"},2153 esc_path($diffinfo->{'to_file'}));2154}else{2155$result.= esc_path($diffinfo->{'to_file'});2156}2157$result.="</div>\n".# class="diff header"2158"<div class=\"diff nodifferences\">".2159"Simple merge".2160"</div>\n";# class="diff nodifferences"21612162return$result;2163}21642165# format patch (diff) line (not to be used for diff headers)2166sub format_diff_line {2167my$line=shift;2168my($from,$to) =@_;2169my$diff_class="";21702171chomp$line;21722173if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2174# combined diff2175my$prefix=substr($line,0,scalar@{$from->{'href'}});2176if($line=~m/^\@{3}/) {2177$diff_class=" chunk_header";2178}elsif($line=~m/^\\/) {2179$diff_class=" incomplete";2180}elsif($prefix=~tr/+/+/) {2181$diff_class=" add";2182}elsif($prefix=~tr/-/-/) {2183$diff_class=" rem";2184}2185}else{2186# assume ordinary diff2187my$char=substr($line,0,1);2188if($chareq'+') {2189$diff_class=" add";2190}elsif($chareq'-') {2191$diff_class=" rem";2192}elsif($chareq'@') {2193$diff_class=" chunk_header";2194}elsif($chareq"\\") {2195$diff_class=" incomplete";2196}2197}2198$line= untabify($line);2199if($from&&$to&&$line=~m/^\@{2} /) {2200my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2201$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;22022203$from_lines=0unlessdefined$from_lines;2204$to_lines=0unlessdefined$to_lines;22052206if($from->{'href'}) {2207$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2208-class=>"list"},$from_text);2209}2210if($to->{'href'}) {2211$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2212-class=>"list"},$to_text);2213}2214$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2215"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2216return"<div class=\"diff$diff_class\">$line</div>\n";2217}elsif($from&&$to&&$line=~m/^\@{3}/) {2218my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2219my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);22202221@from_text=split(' ',$ranges);2222for(my$i=0;$i<@from_text; ++$i) {2223($from_start[$i],$from_nlines[$i]) =2224(split(',',substr($from_text[$i],1)),0);2225}22262227$to_text=pop@from_text;2228$to_start=pop@from_start;2229$to_nlines=pop@from_nlines;22302231$line="<span class=\"chunk_info\">$prefix";2232for(my$i=0;$i<@from_text; ++$i) {2233if($from->{'href'}[$i]) {2234$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2235-class=>"list"},$from_text[$i]);2236}else{2237$line.=$from_text[$i];2238}2239$line.=" ";2240}2241if($to->{'href'}) {2242$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2243-class=>"list"},$to_text);2244}else{2245$line.=$to_text;2246}2247$line.="$prefix</span>".2248"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2249return"<div class=\"diff$diff_class\">$line</div>\n";2250}2251return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2252}22532254# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2255# linked. Pass the hash of the tree/commit to snapshot.2256sub format_snapshot_links {2257my($hash) =@_;2258my$num_fmts=@snapshot_fmts;2259if($num_fmts>1) {2260# A parenthesized list of links bearing format names.2261# e.g. "snapshot (_tar.gz_ _zip_)"2262return"snapshot (".join(' ',map2263$cgi->a({2264-href => href(2265 action=>"snapshot",2266 hash=>$hash,2267 snapshot_format=>$_2268)2269},$known_snapshot_formats{$_}{'display'})2270,@snapshot_fmts) .")";2271}elsif($num_fmts==1) {2272# A single "snapshot" link whose tooltip bears the format name.2273# i.e. "_snapshot_"2274my($fmt) =@snapshot_fmts;2275return2276$cgi->a({2277-href => href(2278 action=>"snapshot",2279 hash=>$hash,2280 snapshot_format=>$fmt2281),2282-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2283},"snapshot");2284}else{# $num_fmts == 02285returnundef;2286}2287}22882289## ......................................................................2290## functions returning values to be passed, perhaps after some2291## transformation, to other functions; e.g. returning arguments to href()22922293# returns hash to be passed to href to generate gitweb URL2294# in -title key it returns description of link2295sub get_feed_info {2296my$format=shift||'Atom';2297my%res= (action =>lc($format));22982299# feed links are possible only for project views2300return unless(defined$project);2301# some views should link to OPML, or to generic project feed,2302# or don't have specific feed yet (so they should use generic)2303return if($action=~/^(?:tags|heads|forks|tag|search)$/x);23042305my$branch;2306# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2307# from tag links; this also makes possible to detect branch links2308if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2309(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2310$branch=$1;2311}2312# find log type for feed description (title)2313my$type='log';2314if(defined$file_name) {2315$type="history of$file_name";2316$type.="/"if($actioneq'tree');2317$type.=" on '$branch'"if(defined$branch);2318}else{2319$type="log of$branch"if(defined$branch);2320}23212322$res{-title} =$type;2323$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2324$res{'file_name'} =$file_name;23252326return%res;2327}23282329## ----------------------------------------------------------------------2330## git utility subroutines, invoking git commands23312332# returns path to the core git executable and the --git-dir parameter as list2333sub git_cmd {2334$number_of_git_cmds++;2335return$GIT,'--git-dir='.$git_dir;2336}23372338# quote the given arguments for passing them to the shell2339# quote_command("command", "arg 1", "arg with ' and ! characters")2340# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2341# Try to avoid using this function wherever possible.2342sub quote_command {2343returnjoin(' ',2344map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2345}23462347# get HEAD ref of given project as hash2348sub git_get_head_hash {2349return git_get_full_hash(shift,'HEAD');2350}23512352sub git_get_full_hash {2353return git_get_hash(@_);2354}23552356sub git_get_short_hash {2357return git_get_hash(@_,'--short=7');2358}23592360sub git_get_hash {2361my($project,$hash,@options) =@_;2362my$o_git_dir=$git_dir;2363my$retval=undef;2364$git_dir="$projectroot/$project";2365if(open my$fd,'-|', git_cmd(),'rev-parse',2366'--verify','-q',@options,$hash) {2367$retval= <$fd>;2368chomp$retvalifdefined$retval;2369close$fd;2370}2371if(defined$o_git_dir) {2372$git_dir=$o_git_dir;2373}2374return$retval;2375}23762377# get type of given object2378sub git_get_type {2379my$hash=shift;23802381open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2382my$type= <$fd>;2383close$fdorreturn;2384chomp$type;2385return$type;2386}23872388# repository configuration2389our$config_file='';2390our%config;23912392# store multiple values for single key as anonymous array reference2393# single values stored directly in the hash, not as [ <value> ]2394sub hash_set_multi {2395my($hash,$key,$value) =@_;23962397if(!exists$hash->{$key}) {2398$hash->{$key} =$value;2399}elsif(!ref$hash->{$key}) {2400$hash->{$key} = [$hash->{$key},$value];2401}else{2402push@{$hash->{$key}},$value;2403}2404}24052406# return hash of git project configuration2407# optionally limited to some section, e.g. 'gitweb'2408sub git_parse_project_config {2409my$section_regexp=shift;2410my%config;24112412local$/="\0";24132414open my$fh,"-|", git_cmd(),"config",'-z','-l',2415orreturn;24162417while(my$keyval= <$fh>) {2418chomp$keyval;2419my($key,$value) =split(/\n/,$keyval,2);24202421 hash_set_multi(\%config,$key,$value)2422if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2423}2424close$fh;24252426return%config;2427}24282429# convert config value to boolean: 'true' or 'false'2430# no value, number > 0, 'true' and 'yes' values are true2431# rest of values are treated as false (never as error)2432sub config_to_bool {2433my$val=shift;24342435return1if!defined$val;# section.key24362437# strip leading and trailing whitespace2438$val=~s/^\s+//;2439$val=~s/\s+$//;24402441return(($val=~/^\d+$/&&$val) ||# section.key = 12442($val=~/^(?:true|yes)$/i));# section.key = true2443}24442445# convert config value to simple decimal number2446# an optional value suffix of 'k', 'm', or 'g' will cause the value2447# to be multiplied by 1024, 1048576, or 10737418242448sub config_to_int {2449my$val=shift;24502451# strip leading and trailing whitespace2452$val=~s/^\s+//;2453$val=~s/\s+$//;24542455if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2456$unit=lc($unit);2457# unknown unit is treated as 12458return$num* ($uniteq'g'?1073741824:2459$uniteq'm'?1048576:2460$uniteq'k'?1024:1);2461}2462return$val;2463}24642465# convert config value to array reference, if needed2466sub config_to_multi {2467my$val=shift;24682469returnref($val) ?$val: (defined($val) ? [$val] : []);2470}24712472sub git_get_project_config {2473my($key,$type) =@_;24742475return unlessdefined$git_dir;24762477# key sanity check2478return unless($key);2479$key=~s/^gitweb\.//;2480return if($key=~m/\W/);24812482# type sanity check2483if(defined$type) {2484$type=~s/^--//;2485$type=undef2486unless($typeeq'bool'||$typeeq'int');2487}24882489# get config2490if(!defined$config_file||2491$config_filene"$git_dir/config") {2492%config= git_parse_project_config('gitweb');2493$config_file="$git_dir/config";2494}24952496# check if config variable (key) exists2497return unlessexists$config{"gitweb.$key"};24982499# ensure given type2500if(!defined$type) {2501return$config{"gitweb.$key"};2502}elsif($typeeq'bool') {2503# backward compatibility: 'git config --bool' returns true/false2504return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2505}elsif($typeeq'int') {2506return config_to_int($config{"gitweb.$key"});2507}2508return$config{"gitweb.$key"};2509}25102511# get hash of given path at given ref2512sub git_get_hash_by_path {2513my$base=shift;2514my$path=shift||returnundef;2515my$type=shift;25162517$path=~ s,/+$,,;25182519open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2520or die_error(500,"Open git-ls-tree failed");2521my$line= <$fd>;2522close$fdorreturnundef;25232524if(!defined$line) {2525# there is no tree or hash given by $path at $base2526returnundef;2527}25282529#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2530$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2531if(defined$type&&$typene$2) {2532# type doesn't match2533returnundef;2534}2535return$3;2536}25372538# get path of entry with given hash at given tree-ish (ref)2539# used to get 'from' filename for combined diff (merge commit) for renames2540sub git_get_path_by_hash {2541my$base=shift||return;2542my$hash=shift||return;25432544local$/="\0";25452546open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2547orreturnundef;2548while(my$line= <$fd>) {2549chomp$line;25502551#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2552#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2553if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2554close$fd;2555return$1;2556}2557}2558close$fd;2559returnundef;2560}25612562## ......................................................................2563## git utility functions, directly accessing git repository25642565sub git_get_project_description {2566my$path=shift;25672568$git_dir="$projectroot/$path";2569open my$fd,'<',"$git_dir/description"2570orreturn git_get_project_config('description');2571my$descr= <$fd>;2572close$fd;2573if(defined$descr) {2574chomp$descr;2575}2576return$descr;2577}25782579# supported formats:2580# * $GIT_DIR/ctags/<tagname> file (in 'ctags' subdirectory)2581# - if its contents is a number, use it as tag weight,2582# - otherwise add a tag with weight 12583# * $GIT_DIR/ctags file, each line is a tag (with weight 1)2584# the same value multiple times increases tag weight2585# * `gitweb.ctag' multi-valued repo config variable2586sub git_get_project_ctags {2587my$project=shift;2588my$ctags= {};25892590$git_dir="$projectroot/$project";2591if(opendir my$dh,"$git_dir/ctags") {2592my@files=grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh);2593foreachmy$tagfile(@files) {2594open my$ct,'<',$tagfile2595ornext;2596my$val= <$ct>;2597chomp$valif$val;2598close$ct;25992600(my$ctag=$tagfile) =~ s#.*/##;2601if($val=~/\d+/) {2602$ctags->{$ctag} =$val;2603}else{2604$ctags->{$ctag} =1;2605}2606}2607closedir$dh;26082609}elsif(open my$fh,'<',"$git_dir/ctags") {2610while(my$line= <$fh>) {2611chomp$line;2612$ctags->{$line}++if$line;2613}2614close$fh;26152616}else{2617my$taglist= config_to_multi(git_get_project_config('ctag'));2618foreachmy$tag(@$taglist) {2619$ctags->{$tag}++;2620}2621}26222623return$ctags;2624}26252626# return hash, where keys are content tags ('ctags'),2627# and values are sum of weights of given tag in every project2628sub git_gather_all_ctags {2629my$projects=shift;2630my$ctags= {};26312632foreachmy$p(@$projects) {2633foreachmy$ct(keys%{$p->{'ctags'}}) {2634$ctags->{$ct} +=$p->{'ctags'}->{$ct};2635}2636}26372638return$ctags;2639}26402641sub git_populate_project_tagcloud {2642my$ctags=shift;26432644# First, merge different-cased tags; tags vote on casing2645my%ctags_lc;2646foreach(keys%$ctags) {2647$ctags_lc{lc$_}->{count} +=$ctags->{$_};2648if(not$ctags_lc{lc$_}->{topcount}2649or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2650$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2651$ctags_lc{lc$_}->{topname} =$_;2652}2653}26542655my$cloud;2656my$matched=$cgi->param('by_tag');2657if(eval{require HTML::TagCloud;1; }) {2658$cloud= HTML::TagCloud->new;2659foreachmy$ctag(sort keys%ctags_lc) {2660# Pad the title with spaces so that the cloud looks2661# less crammed.2662my$title= esc_html($ctags_lc{$ctag}->{topname});2663$title=~s/ / /g;2664$title=~s/^/ /g;2665$title=~s/$/ /g;2666if(defined$matched&&$matchedeq$ctag) {2667$title=qq(<span class="match">$title</span>);2668}2669$cloud->add($title, href(project=>undef, ctag=>$ctag),2670$ctags_lc{$ctag}->{count});2671}2672}else{2673$cloud= {};2674foreachmy$ctag(keys%ctags_lc) {2675my$title= esc_html($ctags_lc{$ctag}->{topname}, -nbsp=>1);2676if(defined$matched&&$matchedeq$ctag) {2677$title=qq(<span class="match">$title</span>);2678}2679$cloud->{$ctag}{count} =$ctags_lc{$ctag}->{count};2680$cloud->{$ctag}{ctag} =2681$cgi->a({-href=>href(project=>undef, ctag=>$ctag)},$title);2682}2683}2684return$cloud;2685}26862687sub git_show_project_tagcloud {2688my($cloud,$count) =@_;2689if(ref$cloudeq'HTML::TagCloud') {2690return$cloud->html_and_css($count);2691}else{2692my@tags=sort{$cloud->{$a}->{'count'} <=>$cloud->{$b}->{'count'} }keys%$cloud;2693return2694'<div id="htmltagcloud"'.($project?'':' align="center"').'>'.2695join(', ',map{2696$cloud->{$_}->{'ctag'}2697}splice(@tags,0,$count)) .2698'</div>';2699}2700}27012702sub git_get_project_url_list {2703my$path=shift;27042705$git_dir="$projectroot/$path";2706open my$fd,'<',"$git_dir/cloneurl"2707orreturnwantarray?2708@{ config_to_multi(git_get_project_config('url')) } :2709 config_to_multi(git_get_project_config('url'));2710my@git_project_url_list=map{chomp;$_} <$fd>;2711close$fd;27122713returnwantarray?@git_project_url_list: \@git_project_url_list;2714}27152716sub git_get_projects_list {2717my$filter=shift||'';2718my@list;27192720$filter=~s/\.git$//;27212722if(-d $projects_list) {2723# search in directory2724my$dir=$projects_list;2725# remove the trailing "/"2726$dir=~s!/+$!!;2727my$pfxlen=length("$projects_list");2728my$pfxdepth= ($projects_list=~tr!/!!);2729# when filtering, search only given subdirectory2730if($filter) {2731$dir.="/$filter";2732$dir=~s!/+$!!;2733}27342735 File::Find::find({2736 follow_fast =>1,# follow symbolic links2737 follow_skip =>2,# ignore duplicates2738 dangling_symlinks =>0,# ignore dangling symlinks, silently2739 wanted =>sub{2740# global variables2741our$project_maxdepth;2742our$projectroot;2743# skip project-list toplevel, if we get it.2744return if(m!^[/.]$!);2745# only directories can be git repositories2746return unless(-d $_);2747# don't traverse too deep (Find is super slow on os x)2748# $project_maxdepth excludes depth of $projectroot2749if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2750$File::Find::prune =1;2751return;2752}27532754my$path=substr($File::Find::name,$pfxlen+1);2755# we check related file in $projectroot2756if(check_export_ok("$projectroot/$path")) {2757push@list, { path =>$path};2758$File::Find::prune =1;2759}2760},2761},"$dir");27622763}elsif(-f $projects_list) {2764# read from file(url-encoded):2765# 'git%2Fgit.git Linus+Torvalds'2766# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2767# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2768open my$fd,'<',$projects_listorreturn;2769 PROJECT:2770while(my$line= <$fd>) {2771chomp$line;2772my($path,$owner) =split' ',$line;2773$path= unescape($path);2774$owner= unescape($owner);2775if(!defined$path) {2776next;2777}2778# if $filter is rpovided, check if $path begins with $filter2779if($filter&&$path!~m!^\Q$filter\E/!) {2780next;2781}2782if(check_export_ok("$projectroot/$path")) {2783my$pr= {2784 path =>$path,2785 owner => to_utf8($owner),2786};2787push@list,$pr;2788}2789}2790close$fd;2791}2792return@list;2793}27942795# written with help of Tree::Trie module (Perl Artistic License, GPL compatibile)2796# as side effects it sets 'forks' field to list of forks for forked projects2797sub filter_forks_from_projects_list {2798my$projects=shift;27992800my%trie;# prefix tree of directories (path components)2801# generate trie out of those directories that might contain forks2802foreachmy$pr(@$projects) {2803my$path=$pr->{'path'};2804$path=~s/\.git$//;# forks of 'repo.git' are in 'repo/' directory2805next if($path=~m!/$!);# skip non-bare repositories, e.g. 'repo/.git'2806next unless($path);# skip '.git' repository: tests, git-instaweb2807next unless(-d $path);# containing directory exists2808$pr->{'forks'} = [];# there can be 0 or more forks of project28092810# add to trie2811my@dirs=split('/',$path);2812# walk the trie, until either runs out of components or out of trie2813my$ref= \%trie;2814while(scalar@dirs&&2815exists($ref->{$dirs[0]})) {2816$ref=$ref->{shift@dirs};2817}2818# create rest of trie structure from rest of components2819foreachmy$dir(@dirs) {2820$ref=$ref->{$dir} = {};2821}2822# create end marker, store $pr as a data2823$ref->{''} =$prif(!exists$ref->{''});2824}28252826# filter out forks, by finding shortest prefix match for paths2827my@filtered;2828 PROJECT:2829foreachmy$pr(@$projects) {2830# trie lookup2831my$ref= \%trie;2832 DIR:2833foreachmy$dir(split('/',$pr->{'path'})) {2834if(exists$ref->{''}) {2835# found [shortest] prefix, is a fork - skip it2836push@{$ref->{''}{'forks'}},$pr;2837next PROJECT;2838}2839if(!exists$ref->{$dir}) {2840# not in trie, cannot have prefix, not a fork2841push@filtered,$pr;2842next PROJECT;2843}2844# If the dir is there, we just walk one step down the trie.2845$ref=$ref->{$dir};2846}2847# we ran out of trie2848# (shouldn't happen: it's either no match, or end marker)2849push@filtered,$pr;2850}28512852return@filtered;2853}28542855# note: fill_project_list_info must be run first,2856# for 'descr_long' and 'ctags' to be filled2857sub search_projects_list {2858my($projlist,%opts) =@_;2859my$tagfilter=$opts{'tagfilter'};2860my$searchtext=$opts{'searchtext'};28612862return@$projlist2863unless($tagfilter||$searchtext);28642865my@projects;2866 PROJECT:2867foreachmy$pr(@$projlist) {28682869if($tagfilter) {2870next unlessref($pr->{'ctags'})eq'HASH';2871next unless2872grep{lc($_)eq lc($tagfilter) }keys%{$pr->{'ctags'}};2873}28742875if($searchtext) {2876next unless2877$pr->{'path'} =~/$searchtext/||2878$pr->{'descr_long'} =~/$searchtext/;2879}28802881push@projects,$pr;2882}28832884return@projects;2885}28862887our$gitweb_project_owner=undef;2888sub git_get_project_list_from_file {28892890return if(defined$gitweb_project_owner);28912892$gitweb_project_owner= {};2893# read from file (url-encoded):2894# 'git%2Fgit.git Linus+Torvalds'2895# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2896# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2897if(-f $projects_list) {2898open(my$fd,'<',$projects_list);2899while(my$line= <$fd>) {2900chomp$line;2901my($pr,$ow) =split' ',$line;2902$pr= unescape($pr);2903$ow= unescape($ow);2904$gitweb_project_owner->{$pr} = to_utf8($ow);2905}2906close$fd;2907}2908}29092910sub git_get_project_owner {2911my$project=shift;2912my$owner;29132914returnundefunless$project;2915$git_dir="$projectroot/$project";29162917if(!defined$gitweb_project_owner) {2918 git_get_project_list_from_file();2919}29202921if(exists$gitweb_project_owner->{$project}) {2922$owner=$gitweb_project_owner->{$project};2923}2924if(!defined$owner){2925$owner= git_get_project_config('owner');2926}2927if(!defined$owner) {2928$owner= get_file_owner("$git_dir");2929}29302931return$owner;2932}29332934sub git_get_last_activity {2935my($path) =@_;2936my$fd;29372938$git_dir="$projectroot/$path";2939open($fd,"-|", git_cmd(),'for-each-ref',2940'--format=%(committer)',2941'--sort=-committerdate',2942'--count=1',2943'refs/heads')orreturn;2944my$most_recent= <$fd>;2945close$fdorreturn;2946if(defined$most_recent&&2947$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2948my$timestamp=$1;2949my$age=time-$timestamp;2950return($age, age_string($age));2951}2952return(undef,undef);2953}29542955# Implementation note: when a single remote is wanted, we cannot use 'git2956# remote show -n' because that command always work (assuming it's a remote URL2957# if it's not defined), and we cannot use 'git remote show' because that would2958# try to make a network roundtrip. So the only way to find if that particular2959# remote is defined is to walk the list provided by 'git remote -v' and stop if2960# and when we find what we want.2961sub git_get_remotes_list {2962my$wanted=shift;2963my%remotes= ();29642965open my$fd,'-|', git_cmd(),'remote','-v';2966return unless$fd;2967while(my$remote= <$fd>) {2968chomp$remote;2969$remote=~s!\t(.*?)\s+\((\w+)\)$!!;2970next if$wantedand not$remoteeq$wanted;2971my($url,$key) = ($1,$2);29722973$remotes{$remote} ||= {'heads'=> () };2974$remotes{$remote}{$key} =$url;2975}2976close$fdorreturn;2977returnwantarray?%remotes: \%remotes;2978}29792980# Takes a hash of remotes as first parameter and fills it by adding the2981# available remote heads for each of the indicated remotes.2982sub fill_remote_heads {2983my$remotes=shift;2984my@heads=map{"remotes/$_"}keys%$remotes;2985my@remoteheads= git_get_heads_list(undef,@heads);2986foreachmy$remote(keys%$remotes) {2987$remotes->{$remote}{'heads'} = [grep{2988$_->{'name'} =~s!^$remote/!!2989}@remoteheads];2990}2991}29922993sub git_get_references {2994my$type=shift||"";2995my%refs;2996# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112997# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2998open my$fd,"-|", git_cmd(),"show-ref","--dereference",2999($type? ("--","refs/$type") : ())# use -- <pattern> if $type3000orreturn;30013002while(my$line= <$fd>) {3003chomp$line;3004if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {3005if(defined$refs{$1}) {3006push@{$refs{$1}},$2;3007}else{3008$refs{$1} = [$2];3009}3010}3011}3012close$fdorreturn;3013return \%refs;3014}30153016sub git_get_rev_name_tags {3017my$hash=shift||returnundef;30183019open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash3020orreturn;3021my$name_rev= <$fd>;3022close$fd;30233024if($name_rev=~ m|^$hash tags/(.*)$|) {3025return$1;3026}else{3027# catches also '$hash undefined' output3028returnundef;3029}3030}30313032## ----------------------------------------------------------------------3033## parse to hash functions30343035sub parse_date {3036my$epoch=shift;3037my$tz=shift||"-0000";30383039my%date;3040my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");3041my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");3042my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);3043$date{'hour'} =$hour;3044$date{'minute'} =$min;3045$date{'mday'} =$mday;3046$date{'day'} =$days[$wday];3047$date{'month'} =$months[$mon];3048$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",3049$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;3050$date{'mday-time'} =sprintf"%d%s%02d:%02d",3051$mday,$months[$mon],$hour,$min;3052$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",30531900+$year,1+$mon,$mday,$hour,$min,$sec;30543055my($tz_sign,$tz_hour,$tz_min) =3056($tz=~m/^([-+])(\d\d)(\d\d)$/);3057$tz_sign= ($tz_signeq'-'? -1: +1);3058my$local=$epoch+$tz_sign*((($tz_hour*60) +$tz_min)*60);3059($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);3060$date{'hour_local'} =$hour;3061$date{'minute_local'} =$min;3062$date{'tz_local'} =$tz;3063$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",30641900+$year,$mon+1,$mday,3065$hour,$min,$sec,$tz);3066return%date;3067}30683069sub parse_tag {3070my$tag_id=shift;3071my%tag;3072my@comment;30733074open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;3075$tag{'id'} =$tag_id;3076while(my$line= <$fd>) {3077chomp$line;3078if($line=~m/^object ([0-9a-fA-F]{40})$/) {3079$tag{'object'} =$1;3080}elsif($line=~m/^type (.+)$/) {3081$tag{'type'} =$1;3082}elsif($line=~m/^tag (.+)$/) {3083$tag{'name'} =$1;3084}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {3085$tag{'author'} =$1;3086$tag{'author_epoch'} =$2;3087$tag{'author_tz'} =$3;3088if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {3089$tag{'author_name'} =$1;3090$tag{'author_email'} =$2;3091}else{3092$tag{'author_name'} =$tag{'author'};3093}3094}elsif($line=~m/--BEGIN/) {3095push@comment,$line;3096last;3097}elsif($lineeq"") {3098last;3099}3100}3101push@comment, <$fd>;3102$tag{'comment'} = \@comment;3103close$fdorreturn;3104if(!defined$tag{'name'}) {3105return3106};3107return%tag3108}31093110sub parse_commit_text {3111my($commit_text,$withparents) =@_;3112my@commit_lines=split'\n',$commit_text;3113my%co;31143115pop@commit_lines;# Remove '\0'31163117if(!@commit_lines) {3118return;3119}31203121my$header=shift@commit_lines;3122if($header!~m/^[0-9a-fA-F]{40}/) {3123return;3124}3125($co{'id'},my@parents) =split' ',$header;3126while(my$line=shift@commit_lines) {3127last if$lineeq"\n";3128if($line=~m/^tree ([0-9a-fA-F]{40})$/) {3129$co{'tree'} =$1;3130}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {3131push@parents,$1;3132}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {3133$co{'author'} = to_utf8($1);3134$co{'author_epoch'} =$2;3135$co{'author_tz'} =$3;3136if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {3137$co{'author_name'} =$1;3138$co{'author_email'} =$2;3139}else{3140$co{'author_name'} =$co{'author'};3141}3142}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {3143$co{'committer'} = to_utf8($1);3144$co{'committer_epoch'} =$2;3145$co{'committer_tz'} =$3;3146if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {3147$co{'committer_name'} =$1;3148$co{'committer_email'} =$2;3149}else{3150$co{'committer_name'} =$co{'committer'};3151}3152}3153}3154if(!defined$co{'tree'}) {3155return;3156};3157$co{'parents'} = \@parents;3158$co{'parent'} =$parents[0];31593160foreachmy$title(@commit_lines) {3161$title=~s/^ //;3162if($titlene"") {3163$co{'title'} = chop_str($title,80,5);3164# remove leading stuff of merges to make the interesting part visible3165if(length($title) >50) {3166$title=~s/^Automatic //;3167$title=~s/^merge (of|with) /Merge ... /i;3168if(length($title) >50) {3169$title=~s/(http|rsync):\/\///;3170}3171if(length($title) >50) {3172$title=~s/(master|www|rsync)\.//;3173}3174if(length($title) >50) {3175$title=~s/kernel.org:?//;3176}3177if(length($title) >50) {3178$title=~s/\/pub\/scm//;3179}3180}3181$co{'title_short'} = chop_str($title,50,5);3182last;3183}3184}3185if(!defined$co{'title'} ||$co{'title'}eq"") {3186$co{'title'} =$co{'title_short'} ='(no commit message)';3187}3188# remove added spaces3189foreachmy$line(@commit_lines) {3190$line=~s/^ //;3191}3192$co{'comment'} = \@commit_lines;31933194my$age=time-$co{'committer_epoch'};3195$co{'age'} =$age;3196$co{'age_string'} = age_string($age);3197my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});3198if($age>60*60*24*7*2) {3199$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3200$co{'age_string_age'} =$co{'age_string'};3201}else{3202$co{'age_string_date'} =$co{'age_string'};3203$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3204}3205return%co;3206}32073208sub parse_commit {3209my($commit_id) =@_;3210my%co;32113212local$/="\0";32133214open my$fd,"-|", git_cmd(),"rev-list",3215"--parents",3216"--header",3217"--max-count=1",3218$commit_id,3219"--",3220or die_error(500,"Open git-rev-list failed");3221%co= parse_commit_text(<$fd>,1);3222close$fd;32233224return%co;3225}32263227sub parse_commits {3228my($commit_id,$maxcount,$skip,$filename,@args) =@_;3229my@cos;32303231$maxcount||=1;3232$skip||=0;32333234local$/="\0";32353236open my$fd,"-|", git_cmd(),"rev-list",3237"--header",3238@args,3239("--max-count=".$maxcount),3240("--skip=".$skip),3241@extra_options,3242$commit_id,3243"--",3244($filename? ($filename) : ())3245or die_error(500,"Open git-rev-list failed");3246while(my$line= <$fd>) {3247my%co= parse_commit_text($line);3248push@cos, \%co;3249}3250close$fd;32513252returnwantarray?@cos: \@cos;3253}32543255# parse line of git-diff-tree "raw" output3256sub parse_difftree_raw_line {3257my$line=shift;3258my%res;32593260# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'3261# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'3262if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {3263$res{'from_mode'} =$1;3264$res{'to_mode'} =$2;3265$res{'from_id'} =$3;3266$res{'to_id'} =$4;3267$res{'status'} =$5;3268$res{'similarity'} =$6;3269if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3270($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3271}else{3272$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3273}3274}3275# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3276# combined diff (for merge commit)3277elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3278$res{'nparents'} =length($1);3279$res{'from_mode'} = [split(' ',$2) ];3280$res{'to_mode'} =pop@{$res{'from_mode'}};3281$res{'from_id'} = [split(' ',$3) ];3282$res{'to_id'} =pop@{$res{'from_id'}};3283$res{'status'} = [split('',$4) ];3284$res{'to_file'} = unquote($5);3285}3286# 'c512b523472485aef4fff9e57b229d9d243c967f'3287elsif($line=~m/^([0-9a-fA-F]{40})$/) {3288$res{'commit'} =$1;3289}32903291returnwantarray?%res: \%res;3292}32933294# wrapper: return parsed line of git-diff-tree "raw" output3295# (the argument might be raw line, or parsed info)3296sub parsed_difftree_line {3297my$line_or_ref=shift;32983299if(ref($line_or_ref)eq"HASH") {3300# pre-parsed (or generated by hand)3301return$line_or_ref;3302}else{3303return parse_difftree_raw_line($line_or_ref);3304}3305}33063307# parse line of git-ls-tree output3308sub parse_ls_tree_line {3309my$line=shift;3310my%opts=@_;3311my%res;33123313if($opts{'-l'}) {3314#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3315$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;33163317$res{'mode'} =$1;3318$res{'type'} =$2;3319$res{'hash'} =$3;3320$res{'size'} =$4;3321if($opts{'-z'}) {3322$res{'name'} =$5;3323}else{3324$res{'name'} = unquote($5);3325}3326}else{3327#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3328$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;33293330$res{'mode'} =$1;3331$res{'type'} =$2;3332$res{'hash'} =$3;3333if($opts{'-z'}) {3334$res{'name'} =$4;3335}else{3336$res{'name'} = unquote($4);3337}3338}33393340returnwantarray?%res: \%res;3341}33423343# generates _two_ hashes, references to which are passed as 2 and 3 argument3344sub parse_from_to_diffinfo {3345my($diffinfo,$from,$to,@parents) =@_;33463347if($diffinfo->{'nparents'}) {3348# combined diff3349$from->{'file'} = [];3350$from->{'href'} = [];3351 fill_from_file_info($diffinfo,@parents)3352unlessexists$diffinfo->{'from_file'};3353for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3354$from->{'file'}[$i] =3355defined$diffinfo->{'from_file'}[$i] ?3356$diffinfo->{'from_file'}[$i] :3357$diffinfo->{'to_file'};3358if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3359$from->{'href'}[$i] = href(action=>"blob",3360 hash_base=>$parents[$i],3361 hash=>$diffinfo->{'from_id'}[$i],3362 file_name=>$from->{'file'}[$i]);3363}else{3364$from->{'href'}[$i] =undef;3365}3366}3367}else{3368# ordinary (not combined) diff3369$from->{'file'} =$diffinfo->{'from_file'};3370if($diffinfo->{'status'}ne"A") {# not new (added) file3371$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3372 hash=>$diffinfo->{'from_id'},3373 file_name=>$from->{'file'});3374}else{3375delete$from->{'href'};3376}3377}33783379$to->{'file'} =$diffinfo->{'to_file'};3380if(!is_deleted($diffinfo)) {# file exists in result3381$to->{'href'} = href(action=>"blob", hash_base=>$hash,3382 hash=>$diffinfo->{'to_id'},3383 file_name=>$to->{'file'});3384}else{3385delete$to->{'href'};3386}3387}33883389## ......................................................................3390## parse to array of hashes functions33913392sub git_get_heads_list {3393my($limit,@classes) =@_;3394@classes= ('heads')unless@classes;3395my@patterns=map{"refs/$_"}@classes;3396my@headslist;33973398open my$fd,'-|', git_cmd(),'for-each-ref',3399($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3400'--format=%(objectname) %(refname) %(subject)%00%(committer)',3401@patterns3402orreturn;3403while(my$line= <$fd>) {3404my%ref_item;34053406chomp$line;3407my($refinfo,$committerinfo) =split(/\0/,$line);3408my($hash,$name,$title) =split(' ',$refinfo,3);3409my($committer,$epoch,$tz) =3410($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3411$ref_item{'fullname'} =$name;3412$name=~s!^refs/(?:head|remote)s/!!;34133414$ref_item{'name'} =$name;3415$ref_item{'id'} =$hash;3416$ref_item{'title'} =$title||'(no commit message)';3417$ref_item{'epoch'} =$epoch;3418if($epoch) {3419$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3420}else{3421$ref_item{'age'} ="unknown";3422}34233424push@headslist, \%ref_item;3425}3426close$fd;34273428returnwantarray?@headslist: \@headslist;3429}34303431sub git_get_tags_list {3432my$limit=shift;3433my@tagslist;34343435open my$fd,'-|', git_cmd(),'for-each-ref',3436($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3437'--format=%(objectname) %(objecttype) %(refname) '.3438'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3439'refs/tags'3440orreturn;3441while(my$line= <$fd>) {3442my%ref_item;34433444chomp$line;3445my($refinfo,$creatorinfo) =split(/\0/,$line);3446my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3447my($creator,$epoch,$tz) =3448($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3449$ref_item{'fullname'} =$name;3450$name=~s!^refs/tags/!!;34513452$ref_item{'type'} =$type;3453$ref_item{'id'} =$id;3454$ref_item{'name'} =$name;3455if($typeeq"tag") {3456$ref_item{'subject'} =$title;3457$ref_item{'reftype'} =$reftype;3458$ref_item{'refid'} =$refid;3459}else{3460$ref_item{'reftype'} =$type;3461$ref_item{'refid'} =$id;3462}34633464if($typeeq"tag"||$typeeq"commit") {3465$ref_item{'epoch'} =$epoch;3466if($epoch) {3467$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3468}else{3469$ref_item{'age'} ="unknown";3470}3471}34723473push@tagslist, \%ref_item;3474}3475close$fd;34763477returnwantarray?@tagslist: \@tagslist;3478}34793480## ----------------------------------------------------------------------3481## filesystem-related functions34823483sub get_file_owner {3484my$path=shift;34853486my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3487my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3488if(!defined$gcos) {3489returnundef;3490}3491my$owner=$gcos;3492$owner=~s/[,;].*$//;3493return to_utf8($owner);3494}34953496# assume that file exists3497sub insert_file {3498my$filename=shift;34993500open my$fd,'<',$filename;3501print map{ to_utf8($_) } <$fd>;3502close$fd;3503}35043505## ......................................................................3506## mimetype related functions35073508sub mimetype_guess_file {3509my$filename=shift;3510my$mimemap=shift;3511-r $mimemaporreturnundef;35123513my%mimemap;3514open(my$mh,'<',$mimemap)orreturnundef;3515while(<$mh>) {3516next ifm/^#/;# skip comments3517my($mimetype,$exts) =split(/\t+/);3518if(defined$exts) {3519my@exts=split(/\s+/,$exts);3520foreachmy$ext(@exts) {3521$mimemap{$ext} =$mimetype;3522}3523}3524}3525close($mh);35263527$filename=~/\.([^.]*)$/;3528return$mimemap{$1};3529}35303531sub mimetype_guess {3532my$filename=shift;3533my$mime;3534$filename=~/\./orreturnundef;35353536if($mimetypes_file) {3537my$file=$mimetypes_file;3538if($file!~m!^/!) {# if it is relative path3539# it is relative to project3540$file="$projectroot/$project/$file";3541}3542$mime= mimetype_guess_file($filename,$file);3543}3544$mime||= mimetype_guess_file($filename,'/etc/mime.types');3545return$mime;3546}35473548sub blob_mimetype {3549my$fd=shift;3550my$filename=shift;35513552if($filename) {3553my$mime= mimetype_guess($filename);3554$mimeandreturn$mime;3555}35563557# just in case3558return$default_blob_plain_mimetypeunless$fd;35593560if(-T $fd) {3561return'text/plain';3562}elsif(!$filename) {3563return'application/octet-stream';3564}elsif($filename=~m/\.png$/i) {3565return'image/png';3566}elsif($filename=~m/\.gif$/i) {3567return'image/gif';3568}elsif($filename=~m/\.jpe?g$/i) {3569return'image/jpeg';3570}else{3571return'application/octet-stream';3572}3573}35743575sub blob_contenttype {3576my($fd,$file_name,$type) =@_;35773578$type||= blob_mimetype($fd,$file_name);3579if($typeeq'text/plain'&&defined$default_text_plain_charset) {3580$type.="; charset=$default_text_plain_charset";3581}35823583return$type;3584}35853586# guess file syntax for syntax highlighting; return undef if no highlighting3587# the name of syntax can (in the future) depend on syntax highlighter used3588sub guess_file_syntax {3589my($highlight,$mimetype,$file_name) =@_;3590returnundefunless($highlight&&defined$file_name);3591my$basename= basename($file_name,'.in');3592return$highlight_basename{$basename}3593ifexists$highlight_basename{$basename};35943595$basename=~/\.([^.]*)$/;3596my$ext=$1orreturnundef;3597return$highlight_ext{$ext}3598ifexists$highlight_ext{$ext};35993600returnundef;3601}36023603# run highlighter and return FD of its output,3604# or return original FD if no highlighting3605sub run_highlighter {3606my($fd,$highlight,$syntax) =@_;3607return$fdunless($highlight&&defined$syntax);36083609close$fd;3610open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3611 quote_command($highlight_bin).3612" --replace-tabs=8 --fragment --syntax$syntax|"3613or die_error(500,"Couldn't open file or run syntax highlighter");3614return$fd;3615}36163617## ======================================================================3618## functions printing HTML: header, footer, error page36193620sub get_page_title {3621my$title= to_utf8($site_name);36223623return$titleunless(defined$project);3624$title.=" - ". to_utf8($project);36253626return$titleunless(defined$action);3627$title.="/$action";# $action is US-ASCII (7bit ASCII)36283629return$titleunless(defined$file_name);3630$title.=" - ". esc_path($file_name);3631if($actioneq"tree"&&$file_name!~ m|/$|) {3632$title.="/";3633}36343635return$title;3636}36373638sub print_feed_meta {3639if(defined$project) {3640my%href_params= get_feed_info();3641if(!exists$href_params{'-title'}) {3642$href_params{'-title'} ='log';3643}36443645foreachmy$format(qw(RSS Atom)) {3646my$type=lc($format);3647my%link_attr= (3648'-rel'=>'alternate',3649'-title'=> esc_attr("$project-$href_params{'-title'} -$formatfeed"),3650'-type'=>"application/$type+xml"3651);36523653$href_params{'action'} =$type;3654$link_attr{'-href'} = href(%href_params);3655print"<link ".3656"rel=\"$link_attr{'-rel'}\"".3657"title=\"$link_attr{'-title'}\"".3658"href=\"$link_attr{'-href'}\"".3659"type=\"$link_attr{'-type'}\"".3660"/>\n";36613662$href_params{'extra_options'} ='--no-merges';3663$link_attr{'-href'} = href(%href_params);3664$link_attr{'-title'} .=' (no merges)';3665print"<link ".3666"rel=\"$link_attr{'-rel'}\"".3667"title=\"$link_attr{'-title'}\"".3668"href=\"$link_attr{'-href'}\"".3669"type=\"$link_attr{'-type'}\"".3670"/>\n";3671}36723673}else{3674printf('<link rel="alternate" title="%sprojects list" '.3675'href="%s" type="text/plain; charset=utf-8" />'."\n",3676 esc_attr($site_name), href(project=>undef, action=>"project_index"));3677printf('<link rel="alternate" title="%sprojects feeds" '.3678'href="%s" type="text/x-opml" />'."\n",3679 esc_attr($site_name), href(project=>undef, action=>"opml"));3680}3681}36823683sub git_header_html {3684my$status=shift||"200 OK";3685my$expires=shift;3686my%opts=@_;36873688my$title= get_page_title();3689my$content_type;3690# require explicit support from the UA if we are to send the page as3691# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3692# we have to do this because MSIE sometimes globs '*/*', pretending to3693# support xhtml+xml but choking when it gets what it asked for.3694if(defined$cgi->http('HTTP_ACCEPT') &&3695$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3696$cgi->Accept('application/xhtml+xml') !=0) {3697$content_type='application/xhtml+xml';3698}else{3699$content_type='text/html';3700}3701print$cgi->header(-type=>$content_type, -charset =>'utf-8',3702-status=>$status, -expires =>$expires)3703unless($opts{'-no_http_header'});3704my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3705print<<EOF;3706<?xml version="1.0" encoding="utf-8"?>3707<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3708<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3709<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3710<!-- git core binaries version$git_version-->3711<head>3712<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3713<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3714<meta name="robots" content="index, nofollow"/>3715<title>$title</title>3716EOF3717# the stylesheet, favicon etc urls won't work correctly with path_info3718# unless we set the appropriate base URL3719if($ENV{'PATH_INFO'}) {3720print"<base href=\"".esc_url($base_url)."\"/>\n";3721}3722# print out each stylesheet that exist, providing backwards capability3723# for those people who defined $stylesheet in a config file3724if(defined$stylesheet) {3725print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3726}else{3727foreachmy$stylesheet(@stylesheets) {3728next unless$stylesheet;3729print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3730}3731}3732 print_feed_meta()3733if($statuseq'200 OK');3734if(defined$favicon) {3735printqq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);3736}37373738print"</head>\n".3739"<body>\n";37403741if(defined$site_header&& -f $site_header) {3742 insert_file($site_header);3743}37443745print"<div class=\"page_header\">\n";3746if(defined$logo) {3747print$cgi->a({-href => esc_url($logo_url),3748-title =>$logo_label},3749$cgi->img({-src => esc_url($logo),3750-width =>72, -height =>27,3751-alt =>"git",3752-class=>"logo"}));3753}3754print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3755if(defined$project) {3756print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3757if(defined$action) {3758my$action_print=$action;3759if(defined$opts{-action_extra}) {3760$action_print=$cgi->a({-href => href(action=>$action)},3761$action);3762}3763print" /$action_print";3764}3765if(defined$opts{-action_extra}) {3766print" /$opts{-action_extra}";3767}3768print"\n";3769}3770print"</div>\n";37713772my$have_search= gitweb_check_feature('search');3773if(defined$project&&$have_search) {3774if(!defined$searchtext) {3775$searchtext="";3776}3777my$search_hash;3778if(defined$hash_base) {3779$search_hash=$hash_base;3780}elsif(defined$hash) {3781$search_hash=$hash;3782}else{3783$search_hash="HEAD";3784}3785my$action=$my_uri;3786my$use_pathinfo= gitweb_check_feature('pathinfo');3787if($use_pathinfo) {3788$action.="/".esc_url($project);3789}3790print$cgi->startform(-method=>"get", -action =>$action) .3791"<div class=\"search\">\n".3792(!$use_pathinfo&&3793$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3794$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3795$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3796$cgi->popup_menu(-name =>'st', -default=>'commit',3797-values=> ['commit','grep','author','committer','pickaxe']) .3798$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3799" search:\n",3800$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3801"<span title=\"Extended regular expression\">".3802$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3803-checked =>$search_use_regexp) .3804"</span>".3805"</div>".3806$cgi->end_form() ."\n";3807}3808}38093810sub git_footer_html {3811my$feed_class='rss_logo';38123813print"<div class=\"page_footer\">\n";3814if(defined$project) {3815my$descr= git_get_project_description($project);3816if(defined$descr) {3817print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3818}38193820my%href_params= get_feed_info();3821if(!%href_params) {3822$feed_class.=' generic';3823}3824$href_params{'-title'} ||='log';38253826foreachmy$format(qw(RSS Atom)) {3827$href_params{'action'} =lc($format);3828print$cgi->a({-href => href(%href_params),3829-title =>"$href_params{'-title'}$formatfeed",3830-class=>$feed_class},$format)."\n";3831}38323833}else{3834print$cgi->a({-href => href(project=>undef, action=>"opml"),3835-class=>$feed_class},"OPML") ." ";3836print$cgi->a({-href => href(project=>undef, action=>"project_index"),3837-class=>$feed_class},"TXT") ."\n";3838}3839print"</div>\n";# class="page_footer"38403841if(defined$t0&& gitweb_check_feature('timed')) {3842print"<div id=\"generating_info\">\n";3843print'This page took '.3844'<span id="generating_time" class="time_span">'.3845 tv_interval($t0, [ gettimeofday() ]).3846' seconds </span>'.3847' and '.3848'<span id="generating_cmd">'.3849$number_of_git_cmds.3850'</span> git commands '.3851" to generate.\n";3852print"</div>\n";# class="page_footer"3853}38543855if(defined$site_footer&& -f $site_footer) {3856 insert_file($site_footer);3857}38583859print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;3860if(defined$action&&3861$actioneq'blame_incremental') {3862print qq!<script type="text/javascript">\n!.3863 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3864 qq!"!. href() .qq!");\n!.3865 qq!</script>\n!;3866}elsif(gitweb_check_feature('javascript-actions')) {3867print qq!<script type="text/javascript">\n!.3868 qq!window.onload = fixLinks;\n!.3869 qq!</script>\n!;3870}38713872print"</body>\n".3873"</html>";3874}38753876# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3877# Example: die_error(404, 'Hash not found')3878# By convention, use the following status codes (as defined in RFC 2616):3879# 400: Invalid or missing CGI parameters, or3880# requested object exists but has wrong type.3881# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3882# this server or project.3883# 404: Requested object/revision/project doesn't exist.3884# 500: The server isn't configured properly, or3885# an internal error occurred (e.g. failed assertions caused by bugs), or3886# an unknown error occurred (e.g. the git binary died unexpectedly).3887# 503: The server is currently unavailable (because it is overloaded,3888# or down for maintenance). Generally, this is a temporary state.3889sub die_error {3890my$status=shift||500;3891my$error= esc_html(shift) ||"Internal Server Error";3892my$extra=shift;3893my%opts=@_;38943895my%http_responses= (3896400=>'400 Bad Request',3897403=>'403 Forbidden',3898404=>'404 Not Found',3899500=>'500 Internal Server Error',3900503=>'503 Service Unavailable',3901);3902 git_header_html($http_responses{$status},undef,%opts);3903print<<EOF;3904<div class="page_body">3905<br /><br />3906$status-$error3907<br />3908EOF3909if(defined$extra) {3910print"<hr />\n".3911"$extra\n";3912}3913print"</div>\n";39143915 git_footer_html();3916goto DONE_GITWEB3917unless($opts{'-error_handler'});3918}39193920## ----------------------------------------------------------------------3921## functions printing or outputting HTML: navigation39223923sub git_print_page_nav {3924my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3925$extra=''if!defined$extra;# pager or formats39263927my@navs=qw(summary shortlog log commit commitdiff tree);3928if($suppress) {3929@navs=grep{$_ne$suppress}@navs;3930}39313932my%arg=map{$_=> {action=>$_} }@navs;3933if(defined$head) {3934for(qw(commit commitdiff)) {3935$arg{$_}{'hash'} =$head;3936}3937if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3938for(qw(shortlog log)) {3939$arg{$_}{'hash'} =$head;3940}3941}3942}39433944$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3945$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;39463947my@actions= gitweb_get_feature('actions');3948my%repl= (3949'%'=>'%',3950'n'=>$project,# project name3951'f'=>$git_dir,# project path within filesystem3952'h'=>$treehead||'',# current hash ('h' parameter)3953'b'=>$treebase||'',# hash base ('hb' parameter)3954);3955while(@actions) {3956my($label,$link,$pos) =splice(@actions,0,3);3957# insert3958@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3959# munch munch3960$link=~s/%([%nfhb])/$repl{$1}/g;3961$arg{$label}{'_href'} =$link;3962}39633964print"<div class=\"page_nav\">\n".3965(join" | ",3966map{$_eq$current?3967$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3968}@navs);3969print"<br/>\n$extra<br/>\n".3970"</div>\n";3971}39723973# returns a submenu for the nagivation of the refs views (tags, heads,3974# remotes) with the current view disabled and the remotes view only3975# available if the feature is enabled3976sub format_ref_views {3977my($current) =@_;3978my@ref_views=qw{tags heads};3979push@ref_views,'remotes'if gitweb_check_feature('remote_heads');3980returnjoin" | ",map{3981$_eq$current?$_:3982$cgi->a({-href => href(action=>$_)},$_)3983}@ref_views3984}39853986sub format_paging_nav {3987my($action,$page,$has_next_link) =@_;3988my$paging_nav;398939903991if($page>0) {3992$paging_nav.=3993$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3994" ⋅ ".3995$cgi->a({-href => href(-replay=>1, page=>$page-1),3996-accesskey =>"p", -title =>"Alt-p"},"prev");3997}else{3998$paging_nav.="first ⋅ prev";3999}40004001if($has_next_link) {4002$paging_nav.=" ⋅ ".4003$cgi->a({-href => href(-replay=>1, page=>$page+1),4004-accesskey =>"n", -title =>"Alt-n"},"next");4005}else{4006$paging_nav.=" ⋅ next";4007}40084009return$paging_nav;4010}40114012## ......................................................................4013## functions printing or outputting HTML: div40144015sub git_print_header_div {4016my($action,$title,$hash,$hash_base) =@_;4017my%args= ();40184019$args{'action'} =$action;4020$args{'hash'} =$hashif$hash;4021$args{'hash_base'} =$hash_baseif$hash_base;40224023print"<div class=\"header\">\n".4024$cgi->a({-href => href(%args), -class=>"title"},4025$title?$title:$action) .4026"\n</div>\n";4027}40284029sub format_repo_url {4030my($name,$url) =@_;4031return"<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";4032}40334034# Group output by placing it in a DIV element and adding a header.4035# Options for start_div() can be provided by passing a hash reference as the4036# first parameter to the function.4037# Options to git_print_header_div() can be provided by passing an array4038# reference. This must follow the options to start_div if they are present.4039# The content can be a scalar, which is output as-is, a scalar reference, which4040# is output after html escaping, an IO handle passed either as *handle or4041# *handle{IO}, or a function reference. In the latter case all following4042# parameters will be taken as argument to the content function call.4043sub git_print_section {4044my($div_args,$header_args,$content);4045my$arg=shift;4046if(ref($arg)eq'HASH') {4047$div_args=$arg;4048$arg=shift;4049}4050if(ref($arg)eq'ARRAY') {4051$header_args=$arg;4052$arg=shift;4053}4054$content=$arg;40554056print$cgi->start_div($div_args);4057 git_print_header_div(@$header_args);40584059if(ref($content)eq'CODE') {4060$content->(@_);4061}elsif(ref($content)eq'SCALAR') {4062print esc_html($$content);4063}elsif(ref($content)eq'GLOB'or ref($content)eq'IO::Handle') {4064print<$content>;4065}elsif(!ref($content) &&defined($content)) {4066print$content;4067}40684069print$cgi->end_div;4070}40714072sub print_local_time {4073print format_local_time(@_);4074}40754076sub format_local_time {4077my$localtime='';4078my%date=@_;4079if($date{'hour_local'} <6) {4080$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",4081$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});4082}else{4083$localtime.=sprintf(" (%02d:%02d%s)",4084$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});4085}40864087return$localtime;4088}40894090# Outputs the author name and date in long form4091sub git_print_authorship {4092my$co=shift;4093my%opts=@_;4094my$tag=$opts{-tag} ||'div';4095my$author=$co->{'author_name'};40964097my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});4098print"<$tagclass=\"author_date\">".4099 format_search_author($author,"author", esc_html($author)) .4100" [$ad{'rfc2822'}";4101 print_local_time(%ad)if($opts{-localtime});4102print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)4103."</$tag>\n";4104}41054106# Outputs table rows containing the full author or committer information,4107# in the format expected for 'commit' view (& similar).4108# Parameters are a commit hash reference, followed by the list of people4109# to output information for. If the list is empty it defaults to both4110# author and committer.4111sub git_print_authorship_rows {4112my$co=shift;4113# too bad we can't use @people = @_ || ('author', 'committer')4114my@people=@_;4115@people= ('author','committer')unless@people;4116foreachmy$who(@people) {4117my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});4118print"<tr><td>$who</td><td>".4119 format_search_author($co->{"${who}_name"},$who,4120 esc_html($co->{"${who}_name"})) ." ".4121 format_search_author($co->{"${who}_email"},$who,4122 esc_html("<".$co->{"${who}_email"} .">")) .4123"</td><td rowspan=\"2\">".4124 git_get_avatar($co->{"${who}_email"}, -size =>'double') .4125"</td></tr>\n".4126"<tr>".4127"<td></td><td>$wd{'rfc2822'}";4128 print_local_time(%wd);4129print"</td>".4130"</tr>\n";4131}4132}41334134sub git_print_page_path {4135my$name=shift;4136my$type=shift;4137my$hb=shift;413841394140print"<div class=\"page_path\">";4141print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),4142-title =>'tree root'}, to_utf8("[$project]"));4143print" / ";4144if(defined$name) {4145my@dirname=split'/',$name;4146my$basename=pop@dirname;4147my$fullname='';41484149foreachmy$dir(@dirname) {4150$fullname.= ($fullname?'/':'') .$dir;4151print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,4152 hash_base=>$hb),4153-title =>$fullname}, esc_path($dir));4154print" / ";4155}4156if(defined$type&&$typeeq'blob') {4157print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,4158 hash_base=>$hb),4159-title =>$name}, esc_path($basename));4160}elsif(defined$type&&$typeeq'tree') {4161print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,4162 hash_base=>$hb),4163-title =>$name}, esc_path($basename));4164print" / ";4165}else{4166print esc_path($basename);4167}4168}4169print"<br/></div>\n";4170}41714172sub git_print_log {4173my$log=shift;4174my%opts=@_;41754176if($opts{'-remove_title'}) {4177# remove title, i.e. first line of log4178shift@$log;4179}4180# remove leading empty lines4181while(defined$log->[0] &&$log->[0]eq"") {4182shift@$log;4183}41844185# print log4186my$signoff=0;4187my$empty=0;4188foreachmy$line(@$log) {4189if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {4190$signoff=1;4191$empty=0;4192if(!$opts{'-remove_signoff'}) {4193print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";4194next;4195}else{4196# remove signoff lines4197next;4198}4199}else{4200$signoff=0;4201}42024203# print only one empty line4204# do not print empty line after signoff4205if($lineeq"") {4206next if($empty||$signoff);4207$empty=1;4208}else{4209$empty=0;4210}42114212print format_log_line_html($line) ."<br/>\n";4213}42144215if($opts{'-final_empty_line'}) {4216# end with single empty line4217print"<br/>\n"unless$empty;4218}4219}42204221# return link target (what link points to)4222sub git_get_link_target {4223my$hash=shift;4224my$link_target;42254226# read link4227open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4228orreturn;4229{4230local$/=undef;4231$link_target= <$fd>;4232}4233close$fd4234orreturn;42354236return$link_target;4237}42384239# given link target, and the directory (basedir) the link is in,4240# return target of link relative to top directory (top tree);4241# return undef if it is not possible (including absolute links).4242sub normalize_link_target {4243my($link_target,$basedir) =@_;42444245# absolute symlinks (beginning with '/') cannot be normalized4246return if(substr($link_target,0,1)eq'/');42474248# normalize link target to path from top (root) tree (dir)4249my$path;4250if($basedir) {4251$path=$basedir.'/'.$link_target;4252}else{4253# we are in top (root) tree (dir)4254$path=$link_target;4255}42564257# remove //, /./, and /../4258my@path_parts;4259foreachmy$part(split('/',$path)) {4260# discard '.' and ''4261next if(!$part||$parteq'.');4262# handle '..'4263if($parteq'..') {4264if(@path_parts) {4265pop@path_parts;4266}else{4267# link leads outside repository (outside top dir)4268return;4269}4270}else{4271push@path_parts,$part;4272}4273}4274$path=join('/',@path_parts);42754276return$path;4277}42784279# print tree entry (row of git_tree), but without encompassing <tr> element4280sub git_print_tree_entry {4281my($t,$basedir,$hash_base,$have_blame) =@_;42824283my%base_key= ();4284$base_key{'hash_base'} =$hash_baseifdefined$hash_base;42854286# The format of a table row is: mode list link. Where mode is4287# the mode of the entry, list is the name of the entry, an href,4288# and link is the action links of the entry.42894290print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";4291if(exists$t->{'size'}) {4292print"<td class=\"size\">$t->{'size'}</td>\n";4293}4294if($t->{'type'}eq"blob") {4295print"<td class=\"list\">".4296$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4297 file_name=>"$basedir$t->{'name'}",%base_key),4298-class=>"list"}, esc_path($t->{'name'}));4299if(S_ISLNK(oct$t->{'mode'})) {4300my$link_target= git_get_link_target($t->{'hash'});4301if($link_target) {4302my$norm_target= normalize_link_target($link_target,$basedir);4303if(defined$norm_target) {4304print" -> ".4305$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,4306 file_name=>$norm_target),4307-title =>$norm_target}, esc_path($link_target));4308}else{4309print" -> ". esc_path($link_target);4310}4311}4312}4313print"</td>\n";4314print"<td class=\"link\">";4315print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4316 file_name=>"$basedir$t->{'name'}",%base_key)},4317"blob");4318if($have_blame) {4319print" | ".4320$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4321 file_name=>"$basedir$t->{'name'}",%base_key)},4322"blame");4323}4324if(defined$hash_base) {4325print" | ".4326$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4327 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4328"history");4329}4330print" | ".4331$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4332 file_name=>"$basedir$t->{'name'}")},4333"raw");4334print"</td>\n";43354336}elsif($t->{'type'}eq"tree") {4337print"<td class=\"list\">";4338print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4339 file_name=>"$basedir$t->{'name'}",4340%base_key)},4341 esc_path($t->{'name'}));4342print"</td>\n";4343print"<td class=\"link\">";4344print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4345 file_name=>"$basedir$t->{'name'}",4346%base_key)},4347"tree");4348if(defined$hash_base) {4349print" | ".4350$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4351 file_name=>"$basedir$t->{'name'}")},4352"history");4353}4354print"</td>\n";4355}else{4356# unknown object: we can only present history for it4357# (this includes 'commit' object, i.e. submodule support)4358print"<td class=\"list\">".4359 esc_path($t->{'name'}) .4360"</td>\n";4361print"<td class=\"link\">";4362if(defined$hash_base) {4363print$cgi->a({-href => href(action=>"history",4364 hash_base=>$hash_base,4365 file_name=>"$basedir$t->{'name'}")},4366"history");4367}4368print"</td>\n";4369}4370}43714372## ......................................................................4373## functions printing large fragments of HTML43744375# get pre-image filenames for merge (combined) diff4376sub fill_from_file_info {4377my($diff,@parents) =@_;43784379$diff->{'from_file'} = [ ];4380$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4381for(my$i=0;$i<$diff->{'nparents'};$i++) {4382if($diff->{'status'}[$i]eq'R'||4383$diff->{'status'}[$i]eq'C') {4384$diff->{'from_file'}[$i] =4385 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4386}4387}43884389return$diff;4390}43914392# is current raw difftree line of file deletion4393sub is_deleted {4394my$diffinfo=shift;43954396return$diffinfo->{'to_id'}eq('0' x 40);4397}43984399# does patch correspond to [previous] difftree raw line4400# $diffinfo - hashref of parsed raw diff format4401# $patchinfo - hashref of parsed patch diff format4402# (the same keys as in $diffinfo)4403sub is_patch_split {4404my($diffinfo,$patchinfo) =@_;44054406returndefined$diffinfo&&defined$patchinfo4407&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4408}440944104411sub git_difftree_body {4412my($difftree,$hash,@parents) =@_;4413my($parent) =$parents[0];4414my$have_blame= gitweb_check_feature('blame');4415print"<div class=\"list_head\">\n";4416if($#{$difftree} >10) {4417print(($#{$difftree} +1) ." files changed:\n");4418}4419print"</div>\n";44204421print"<table class=\"".4422(@parents>1?"combined ":"") .4423"diff_tree\">\n";44244425# header only for combined diff in 'commitdiff' view4426my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4427if($has_header) {4428# table header4429print"<thead><tr>\n".4430"<th></th><th></th>\n";# filename, patchN link4431for(my$i=0;$i<@parents;$i++) {4432my$par=$parents[$i];4433print"<th>".4434$cgi->a({-href => href(action=>"commitdiff",4435 hash=>$hash, hash_parent=>$par),4436-title =>'commitdiff to parent number '.4437($i+1) .': '.substr($par,0,7)},4438$i+1) .4439" </th>\n";4440}4441print"</tr></thead>\n<tbody>\n";4442}44434444my$alternate=1;4445my$patchno=0;4446foreachmy$line(@{$difftree}) {4447my$diff= parsed_difftree_line($line);44484449if($alternate) {4450print"<tr class=\"dark\">\n";4451}else{4452print"<tr class=\"light\">\n";4453}4454$alternate^=1;44554456if(exists$diff->{'nparents'}) {# combined diff44574458 fill_from_file_info($diff,@parents)4459unlessexists$diff->{'from_file'};44604461if(!is_deleted($diff)) {4462# file exists in the result (child) commit4463print"<td>".4464$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4465 file_name=>$diff->{'to_file'},4466 hash_base=>$hash),4467-class=>"list"}, esc_path($diff->{'to_file'})) .4468"</td>\n";4469}else{4470print"<td>".4471 esc_path($diff->{'to_file'}) .4472"</td>\n";4473}44744475if($actioneq'commitdiff') {4476# link to patch4477$patchno++;4478print"<td class=\"link\">".4479$cgi->a({-href => href(-anchor=>"patch$patchno")},4480"patch") .4481" | ".4482"</td>\n";4483}44844485my$has_history=0;4486my$not_deleted=0;4487for(my$i=0;$i<$diff->{'nparents'};$i++) {4488my$hash_parent=$parents[$i];4489my$from_hash=$diff->{'from_id'}[$i];4490my$from_path=$diff->{'from_file'}[$i];4491my$status=$diff->{'status'}[$i];44924493$has_history||= ($statusne'A');4494$not_deleted||= ($statusne'D');44954496if($statuseq'A') {4497print"<td class=\"link\"align=\"right\"> | </td>\n";4498}elsif($statuseq'D') {4499print"<td class=\"link\">".4500$cgi->a({-href => href(action=>"blob",4501 hash_base=>$hash,4502 hash=>$from_hash,4503 file_name=>$from_path)},4504"blob". ($i+1)) .4505" | </td>\n";4506}else{4507if($diff->{'to_id'}eq$from_hash) {4508print"<td class=\"link nochange\">";4509}else{4510print"<td class=\"link\">";4511}4512print$cgi->a({-href => href(action=>"blobdiff",4513 hash=>$diff->{'to_id'},4514 hash_parent=>$from_hash,4515 hash_base=>$hash,4516 hash_parent_base=>$hash_parent,4517 file_name=>$diff->{'to_file'},4518 file_parent=>$from_path)},4519"diff". ($i+1)) .4520" | </td>\n";4521}4522}45234524print"<td class=\"link\">";4525if($not_deleted) {4526print$cgi->a({-href => href(action=>"blob",4527 hash=>$diff->{'to_id'},4528 file_name=>$diff->{'to_file'},4529 hash_base=>$hash)},4530"blob");4531print" | "if($has_history);4532}4533if($has_history) {4534print$cgi->a({-href => href(action=>"history",4535 file_name=>$diff->{'to_file'},4536 hash_base=>$hash)},4537"history");4538}4539print"</td>\n";45404541print"</tr>\n";4542next;# instead of 'else' clause, to avoid extra indent4543}4544# else ordinary diff45454546my($to_mode_oct,$to_mode_str,$to_file_type);4547my($from_mode_oct,$from_mode_str,$from_file_type);4548if($diff->{'to_mode'}ne('0' x 6)) {4549$to_mode_oct=oct$diff->{'to_mode'};4550if(S_ISREG($to_mode_oct)) {# only for regular file4551$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4552}4553$to_file_type= file_type($diff->{'to_mode'});4554}4555if($diff->{'from_mode'}ne('0' x 6)) {4556$from_mode_oct=oct$diff->{'from_mode'};4557if(S_ISREG($from_mode_oct)) {# only for regular file4558$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4559}4560$from_file_type= file_type($diff->{'from_mode'});4561}45624563if($diff->{'status'}eq"A") {# created4564my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4565$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4566$mode_chng.="]</span>";4567print"<td>";4568print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4569 hash_base=>$hash, file_name=>$diff->{'file'}),4570-class=>"list"}, esc_path($diff->{'file'}));4571print"</td>\n";4572print"<td>$mode_chng</td>\n";4573print"<td class=\"link\">";4574if($actioneq'commitdiff') {4575# link to patch4576$patchno++;4577print$cgi->a({-href => href(-anchor=>"patch$patchno")},4578"patch") .4579" | ";4580}4581print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4582 hash_base=>$hash, file_name=>$diff->{'file'})},4583"blob");4584print"</td>\n";45854586}elsif($diff->{'status'}eq"D") {# deleted4587my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4588print"<td>";4589print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4590 hash_base=>$parent, file_name=>$diff->{'file'}),4591-class=>"list"}, esc_path($diff->{'file'}));4592print"</td>\n";4593print"<td>$mode_chng</td>\n";4594print"<td class=\"link\">";4595if($actioneq'commitdiff') {4596# link to patch4597$patchno++;4598print$cgi->a({-href => href(-anchor=>"patch$patchno")},4599"patch") .4600" | ";4601}4602print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4603 hash_base=>$parent, file_name=>$diff->{'file'})},4604"blob") ." | ";4605if($have_blame) {4606print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4607 file_name=>$diff->{'file'})},4608"blame") ." | ";4609}4610print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4611 file_name=>$diff->{'file'})},4612"history");4613print"</td>\n";46144615}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4616my$mode_chnge="";4617if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4618$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4619if($from_file_typene$to_file_type) {4620$mode_chnge.=" from$from_file_typeto$to_file_type";4621}4622if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4623if($from_mode_str&&$to_mode_str) {4624$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4625}elsif($to_mode_str) {4626$mode_chnge.=" mode:$to_mode_str";4627}4628}4629$mode_chnge.="]</span>\n";4630}4631print"<td>";4632print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4633 hash_base=>$hash, file_name=>$diff->{'file'}),4634-class=>"list"}, esc_path($diff->{'file'}));4635print"</td>\n";4636print"<td>$mode_chnge</td>\n";4637print"<td class=\"link\">";4638if($actioneq'commitdiff') {4639# link to patch4640$patchno++;4641print$cgi->a({-href => href(-anchor=>"patch$patchno")},4642"patch") .4643" | ";4644}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4645# "commit" view and modified file (not onlu mode changed)4646print$cgi->a({-href => href(action=>"blobdiff",4647 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4648 hash_base=>$hash, hash_parent_base=>$parent,4649 file_name=>$diff->{'file'})},4650"diff") .4651" | ";4652}4653print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4654 hash_base=>$hash, file_name=>$diff->{'file'})},4655"blob") ." | ";4656if($have_blame) {4657print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4658 file_name=>$diff->{'file'})},4659"blame") ." | ";4660}4661print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4662 file_name=>$diff->{'file'})},4663"history");4664print"</td>\n";46654666}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4667my%status_name= ('R'=>'moved','C'=>'copied');4668my$nstatus=$status_name{$diff->{'status'}};4669my$mode_chng="";4670if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4671# mode also for directories, so we cannot use $to_mode_str4672$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4673}4674print"<td>".4675$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4676 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4677-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4678"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4679$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4680 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4681-class=>"list"}, esc_path($diff->{'from_file'})) .4682" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4683"<td class=\"link\">";4684if($actioneq'commitdiff') {4685# link to patch4686$patchno++;4687print$cgi->a({-href => href(-anchor=>"patch$patchno")},4688"patch") .4689" | ";4690}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4691# "commit" view and modified file (not only pure rename or copy)4692print$cgi->a({-href => href(action=>"blobdiff",4693 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4694 hash_base=>$hash, hash_parent_base=>$parent,4695 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4696"diff") .4697" | ";4698}4699print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4700 hash_base=>$parent, file_name=>$diff->{'to_file'})},4701"blob") ." | ";4702if($have_blame) {4703print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4704 file_name=>$diff->{'to_file'})},4705"blame") ." | ";4706}4707print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4708 file_name=>$diff->{'to_file'})},4709"history");4710print"</td>\n";47114712}# we should not encounter Unmerged (U) or Unknown (X) status4713print"</tr>\n";4714}4715print"</tbody>"if$has_header;4716print"</table>\n";4717}47184719sub git_patchset_body {4720my($fd,$difftree,$hash,@hash_parents) =@_;4721my($hash_parent) =$hash_parents[0];47224723my$is_combined= (@hash_parents>1);4724my$patch_idx=0;4725my$patch_number=0;4726my$patch_line;4727my$diffinfo;4728my$to_name;4729my(%from,%to);47304731print"<div class=\"patchset\">\n";47324733# skip to first patch4734while($patch_line= <$fd>) {4735chomp$patch_line;47364737last if($patch_line=~m/^diff /);4738}47394740 PATCH:4741while($patch_line) {47424743# parse "git diff" header line4744if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4745# $1 is from_name, which we do not use4746$to_name= unquote($2);4747$to_name=~s!^b/!!;4748}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4749# $1 is 'cc' or 'combined', which we do not use4750$to_name= unquote($2);4751}else{4752$to_name=undef;4753}47544755# check if current patch belong to current raw line4756# and parse raw git-diff line if needed4757if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4758# this is continuation of a split patch4759print"<div class=\"patch cont\">\n";4760}else{4761# advance raw git-diff output if needed4762$patch_idx++ifdefined$diffinfo;47634764# read and prepare patch information4765$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);47664767# compact combined diff output can have some patches skipped4768# find which patch (using pathname of result) we are at now;4769if($is_combined) {4770while($to_namene$diffinfo->{'to_file'}) {4771print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4772 format_diff_cc_simplified($diffinfo,@hash_parents) .4773"</div>\n";# class="patch"47744775$patch_idx++;4776$patch_number++;47774778last if$patch_idx>$#$difftree;4779$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4780}4781}47824783# modifies %from, %to hashes4784 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);47854786# this is first patch for raw difftree line with $patch_idx index4787# we index @$difftree array from 0, but number patches from 14788print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4789}47904791# git diff header4792#assert($patch_line =~ m/^diff /) if DEBUG;4793#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4794$patch_number++;4795# print "git diff" header4796print format_git_diff_header_line($patch_line,$diffinfo,4797 \%from, \%to);47984799# print extended diff header4800print"<div class=\"diff extended_header\">\n";4801 EXTENDED_HEADER:4802while($patch_line= <$fd>) {4803chomp$patch_line;48044805last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);48064807print format_extended_diff_header_line($patch_line,$diffinfo,4808 \%from, \%to);4809}4810print"</div>\n";# class="diff extended_header"48114812# from-file/to-file diff header4813if(!$patch_line) {4814print"</div>\n";# class="patch"4815last PATCH;4816}4817next PATCH if($patch_line=~m/^diff /);4818#assert($patch_line =~ m/^---/) if DEBUG;48194820my$last_patch_line=$patch_line;4821$patch_line= <$fd>;4822chomp$patch_line;4823#assert($patch_line =~ m/^\+\+\+/) if DEBUG;48244825print format_diff_from_to_header($last_patch_line,$patch_line,4826$diffinfo, \%from, \%to,4827@hash_parents);48284829# the patch itself4830 LINE:4831while($patch_line= <$fd>) {4832chomp$patch_line;48334834next PATCH if($patch_line=~m/^diff /);48354836print format_diff_line($patch_line, \%from, \%to);4837}48384839}continue{4840print"</div>\n";# class="patch"4841}48424843# for compact combined (--cc) format, with chunk and patch simplification4844# the patchset might be empty, but there might be unprocessed raw lines4845for(++$patch_idxif$patch_number>0;4846$patch_idx<@$difftree;4847++$patch_idx) {4848# read and prepare patch information4849$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);48504851# generate anchor for "patch" links in difftree / whatchanged part4852print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4853 format_diff_cc_simplified($diffinfo,@hash_parents) .4854"</div>\n";# class="patch"48554856$patch_number++;4857}48584859if($patch_number==0) {4860if(@hash_parents>1) {4861print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4862}else{4863print"<div class=\"diff nodifferences\">No differences found</div>\n";4864}4865}48664867print"</div>\n";# class="patchset"4868}48694870# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48714872# fills project list info (age, description, owner, forks) for each4873# project in the list, removing invalid projects from returned list4874# NOTE: modifies $projlist, but does not remove entries from it4875sub fill_project_list_info {4876my$projlist=shift;4877my@projects;48784879my$show_ctags= gitweb_check_feature('ctags');4880 PROJECT:4881foreachmy$pr(@$projlist) {4882my(@activity) = git_get_last_activity($pr->{'path'});4883unless(@activity) {4884next PROJECT;4885}4886($pr->{'age'},$pr->{'age_string'}) =@activity;4887if(!defined$pr->{'descr'}) {4888my$descr= git_get_project_description($pr->{'path'}) ||"";4889$descr= to_utf8($descr);4890$pr->{'descr_long'} =$descr;4891$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4892}4893if(!defined$pr->{'owner'}) {4894$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4895}4896if($show_ctags) {4897$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4898}4899push@projects,$pr;4900}49014902return@projects;4903}49044905sub sort_projects_list {4906my($projlist,$order) =@_;4907my@projects;49084909my%order_info= (4910 project => { key =>'path', type =>'str'},4911 descr => { key =>'descr_long', type =>'str'},4912 owner => { key =>'owner', type =>'str'},4913 age => { key =>'age', type =>'num'}4914);4915my$oi=$order_info{$order};4916return@$projlistunlessdefined$oi;4917if($oi->{'type'}eq'str') {4918@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@$projlist;4919}else{4920@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@$projlist;4921}49224923return@projects;4924}49254926# print 'sort by' <th> element, generating 'sort by $name' replay link4927# if that order is not selected4928sub print_sort_th {4929print format_sort_th(@_);4930}49314932sub format_sort_th {4933my($name,$order,$header) =@_;4934my$sort_th="";4935$header||=ucfirst($name);49364937if($ordereq$name) {4938$sort_th.="<th>$header</th>\n";4939}else{4940$sort_th.="<th>".4941$cgi->a({-href => href(-replay=>1, order=>$name),4942-class=>"header"},$header) .4943"</th>\n";4944}49454946return$sort_th;4947}49484949sub git_project_list_body {4950# actually uses global variable $project4951my($projlist,$order,$from,$to,$extra,$no_header) =@_;4952my@projects=@$projlist;49534954my$check_forks= gitweb_check_feature('forks');4955my$show_ctags= gitweb_check_feature('ctags');4956my$tagfilter=$show_ctags?$cgi->param('by_tag') :undef;4957$check_forks=undef4958if($tagfilter||$searchtext);49594960# filtering out forks before filling info allows to do less work4961@projects= filter_forks_from_projects_list(\@projects)4962if($check_forks);4963@projects= fill_project_list_info(\@projects);4964# searching projects require filling to be run before it4965@projects= search_projects_list(\@projects,4966'searchtext'=>$searchtext,4967'tagfilter'=>$tagfilter)4968if($tagfilter||$searchtext);49694970$order||=$default_projects_order;4971$from=0unlessdefined$from;4972$to=$#projectsif(!defined$to||$#projects<$to);49734974# short circuit4975if($from>$to) {4976print"<center>\n".4977"<b>No such projects found</b><br />\n".4978"Click ".$cgi->a({-href=>href(project=>undef)},"here")." to view all projects<br />\n".4979"</center>\n<br />\n";4980return;4981}49824983@projects= sort_projects_list(\@projects,$order);49844985if($show_ctags) {4986my$ctags= git_gather_all_ctags(\@projects);4987my$cloud= git_populate_project_tagcloud($ctags);4988print git_show_project_tagcloud($cloud,64);4989}49904991print"<table class=\"project_list\">\n";4992unless($no_header) {4993print"<tr>\n";4994if($check_forks) {4995print"<th></th>\n";4996}4997 print_sort_th('project',$order,'Project');4998 print_sort_th('descr',$order,'Description');4999 print_sort_th('owner',$order,'Owner');5000 print_sort_th('age',$order,'Last Change');5001print"<th></th>\n".# for links5002"</tr>\n";5003}5004my$alternate=1;5005for(my$i=$from;$i<=$to;$i++) {5006my$pr=$projects[$i];50075008if($alternate) {5009print"<tr class=\"dark\">\n";5010}else{5011print"<tr class=\"light\">\n";5012}5013$alternate^=1;50145015if($check_forks) {5016print"<td>";5017if($pr->{'forks'}) {5018my$nforks=scalar@{$pr->{'forks'}};5019if($nforks>0) {5020print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks"),5021-title =>"$nforksforks"},"+");5022}else{5023print$cgi->span({-title =>"$nforksforks"},"+");5024}5025}5026print"</td>\n";5027}5028print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5029-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".5030"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),5031-class=>"list", -title =>$pr->{'descr_long'}},5032 esc_html($pr->{'descr'})) ."</td>\n".5033"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";5034print"<td class=\"". age_class($pr->{'age'}) ."\">".5035(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".5036"<td class=\"link\">".5037$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".5038$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".5039$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".5040$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .5041($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .5042"</td>\n".5043"</tr>\n";5044}5045if(defined$extra) {5046print"<tr>\n";5047if($check_forks) {5048print"<td></td>\n";5049}5050print"<td colspan=\"5\">$extra</td>\n".5051"</tr>\n";5052}5053print"</table>\n";5054}50555056sub git_log_body {5057# uses global variable $project5058my($commitlist,$from,$to,$refs,$extra) =@_;50595060$from=0unlessdefined$from;5061$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);50625063for(my$i=0;$i<=$to;$i++) {5064my%co= %{$commitlist->[$i]};5065next if!%co;5066my$commit=$co{'id'};5067my$ref= format_ref_marker($refs,$commit);5068 git_print_header_div('commit',5069"<span class=\"age\">$co{'age_string'}</span>".5070 esc_html($co{'title'}) .$ref,5071$commit);5072print"<div class=\"title_text\">\n".5073"<div class=\"log_link\">\n".5074$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5075" | ".5076$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5077" | ".5078$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5079"<br/>\n".5080"</div>\n";5081 git_print_authorship(\%co, -tag =>'span');5082print"<br/>\n</div>\n";50835084print"<div class=\"log_body\">\n";5085 git_print_log($co{'comment'}, -final_empty_line=>1);5086print"</div>\n";5087}5088if($extra) {5089print"<div class=\"page_nav\">\n";5090print"$extra\n";5091print"</div>\n";5092}5093}50945095sub git_shortlog_body {5096# uses global variable $project5097my($commitlist,$from,$to,$refs,$extra) =@_;50985099$from=0unlessdefined$from;5100$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);51015102print"<table class=\"shortlog\">\n";5103my$alternate=1;5104for(my$i=$from;$i<=$to;$i++) {5105my%co= %{$commitlist->[$i]};5106my$commit=$co{'id'};5107my$ref= format_ref_marker($refs,$commit);5108if($alternate) {5109print"<tr class=\"dark\">\n";5110}else{5111print"<tr class=\"light\">\n";5112}5113$alternate^=1;5114# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .5115print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5116 format_author_html('td', \%co,10) ."<td>";5117print format_subject_html($co{'title'},$co{'title_short'},5118 href(action=>"commit", hash=>$commit),$ref);5119print"</td>\n".5120"<td class=\"link\">".5121$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".5122$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".5123$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");5124my$snapshot_links= format_snapshot_links($commit);5125if(defined$snapshot_links) {5126print" | ".$snapshot_links;5127}5128print"</td>\n".5129"</tr>\n";5130}5131if(defined$extra) {5132print"<tr>\n".5133"<td colspan=\"4\">$extra</td>\n".5134"</tr>\n";5135}5136print"</table>\n";5137}51385139sub git_history_body {5140# Warning: assumes constant type (blob or tree) during history5141my($commitlist,$from,$to,$refs,$extra,5142$file_name,$file_hash,$ftype) =@_;51435144$from=0unlessdefined$from;5145$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});51465147print"<table class=\"history\">\n";5148my$alternate=1;5149for(my$i=$from;$i<=$to;$i++) {5150my%co= %{$commitlist->[$i]};5151if(!%co) {5152next;5153}5154my$commit=$co{'id'};51555156my$ref= format_ref_marker($refs,$commit);51575158if($alternate) {5159print"<tr class=\"dark\">\n";5160}else{5161print"<tr class=\"light\">\n";5162}5163$alternate^=1;5164print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5165# shortlog: format_author_html('td', \%co, 10)5166 format_author_html('td', \%co,15,3) ."<td>";5167# originally git_history used chop_str($co{'title'}, 50)5168print format_subject_html($co{'title'},$co{'title_short'},5169 href(action=>"commit", hash=>$commit),$ref);5170print"</td>\n".5171"<td class=\"link\">".5172$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".5173$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");51745175if($ftypeeq'blob') {5176my$blob_current=$file_hash;5177my$blob_parent= git_get_hash_by_path($commit,$file_name);5178if(defined$blob_current&&defined$blob_parent&&5179$blob_currentne$blob_parent) {5180print" | ".5181$cgi->a({-href => href(action=>"blobdiff",5182 hash=>$blob_current, hash_parent=>$blob_parent,5183 hash_base=>$hash_base, hash_parent_base=>$commit,5184 file_name=>$file_name)},5185"diff to current");5186}5187}5188print"</td>\n".5189"</tr>\n";5190}5191if(defined$extra) {5192print"<tr>\n".5193"<td colspan=\"4\">$extra</td>\n".5194"</tr>\n";5195}5196print"</table>\n";5197}51985199sub git_tags_body {5200# uses global variable $project5201my($taglist,$from,$to,$extra) =@_;5202$from=0unlessdefined$from;5203$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);52045205print"<table class=\"tags\">\n";5206my$alternate=1;5207for(my$i=$from;$i<=$to;$i++) {5208my$entry=$taglist->[$i];5209my%tag=%$entry;5210my$comment=$tag{'subject'};5211my$comment_short;5212if(defined$comment) {5213$comment_short= chop_str($comment,30,5);5214}5215if($alternate) {5216print"<tr class=\"dark\">\n";5217}else{5218print"<tr class=\"light\">\n";5219}5220$alternate^=1;5221if(defined$tag{'age'}) {5222print"<td><i>$tag{'age'}</i></td>\n";5223}else{5224print"<td></td>\n";5225}5226print"<td>".5227$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),5228-class=>"list name"}, esc_html($tag{'name'})) .5229"</td>\n".5230"<td>";5231if(defined$comment) {5232print format_subject_html($comment,$comment_short,5233 href(action=>"tag", hash=>$tag{'id'}));5234}5235print"</td>\n".5236"<td class=\"selflink\">";5237if($tag{'type'}eq"tag") {5238print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");5239}else{5240print" ";5241}5242print"</td>\n".5243"<td class=\"link\">"." | ".5244$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});5245if($tag{'reftype'}eq"commit") {5246print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .5247" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");5248}elsif($tag{'reftype'}eq"blob") {5249print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");5250}5251print"</td>\n".5252"</tr>";5253}5254if(defined$extra) {5255print"<tr>\n".5256"<td colspan=\"5\">$extra</td>\n".5257"</tr>\n";5258}5259print"</table>\n";5260}52615262sub git_heads_body {5263# uses global variable $project5264my($headlist,$head,$from,$to,$extra) =@_;5265$from=0unlessdefined$from;5266$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);52675268print"<table class=\"heads\">\n";5269my$alternate=1;5270for(my$i=$from;$i<=$to;$i++) {5271my$entry=$headlist->[$i];5272my%ref=%$entry;5273my$curr=$ref{'id'}eq$head;5274if($alternate) {5275print"<tr class=\"dark\">\n";5276}else{5277print"<tr class=\"light\">\n";5278}5279$alternate^=1;5280print"<td><i>$ref{'age'}</i></td>\n".5281($curr?"<td class=\"current_head\">":"<td>") .5282$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),5283-class=>"list name"},esc_html($ref{'name'})) .5284"</td>\n".5285"<td class=\"link\">".5286$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".5287$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".5288$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})},"tree") .5289"</td>\n".5290"</tr>";5291}5292if(defined$extra) {5293print"<tr>\n".5294"<td colspan=\"3\">$extra</td>\n".5295"</tr>\n";5296}5297print"</table>\n";5298}52995300# Display a single remote block5301sub git_remote_block {5302my($remote,$rdata,$limit,$head) =@_;53035304my$heads=$rdata->{'heads'};5305my$fetch=$rdata->{'fetch'};5306my$push=$rdata->{'push'};53075308my$urls_table="<table class=\"projects_list\">\n";53095310if(defined$fetch) {5311if($fetcheq$push) {5312$urls_table.= format_repo_url("URL",$fetch);5313}else{5314$urls_table.= format_repo_url("Fetch URL",$fetch);5315$urls_table.= format_repo_url("Push URL",$push)ifdefined$push;5316}5317}elsif(defined$push) {5318$urls_table.= format_repo_url("Push URL",$push);5319}else{5320$urls_table.= format_repo_url("","No remote URL");5321}53225323$urls_table.="</table>\n";53245325my$dots;5326if(defined$limit&&$limit<@$heads) {5327$dots=$cgi->a({-href => href(action=>"remotes", hash=>$remote)},"...");5328}53295330print$urls_table;5331 git_heads_body($heads,$head,0,$limit,$dots);5332}53335334# Display a list of remote names with the respective fetch and push URLs5335sub git_remotes_list {5336my($remotedata,$limit) =@_;5337print"<table class=\"heads\">\n";5338my$alternate=1;5339my@remotes=sort keys%$remotedata;53405341my$limited=$limit&&$limit<@remotes;53425343$#remotes=$limit-1if$limited;53445345while(my$remote=shift@remotes) {5346my$rdata=$remotedata->{$remote};5347my$fetch=$rdata->{'fetch'};5348my$push=$rdata->{'push'};5349if($alternate) {5350print"<tr class=\"dark\">\n";5351}else{5352print"<tr class=\"light\">\n";5353}5354$alternate^=1;5355print"<td>".5356$cgi->a({-href=> href(action=>'remotes', hash=>$remote),5357-class=>"list name"},esc_html($remote)) .5358"</td>";5359print"<td class=\"link\">".5360(defined$fetch?$cgi->a({-href=>$fetch},"fetch") :"fetch") .5361" | ".5362(defined$push?$cgi->a({-href=>$push},"push") :"push") .5363"</td>";53645365print"</tr>\n";5366}53675368if($limited) {5369print"<tr>\n".5370"<td colspan=\"3\">".5371$cgi->a({-href => href(action=>"remotes")},"...") .5372"</td>\n"."</tr>\n";5373}53745375print"</table>";5376}53775378# Display remote heads grouped by remote, unless there are too many5379# remotes, in which case we only display the remote names5380sub git_remotes_body {5381my($remotedata,$limit,$head) =@_;5382if($limitand$limit<keys%$remotedata) {5383 git_remotes_list($remotedata,$limit);5384}else{5385 fill_remote_heads($remotedata);5386while(my($remote,$rdata) =each%$remotedata) {5387 git_print_section({-class=>"remote", -id=>$remote},5388["remotes",$remote,$remote],sub{5389 git_remote_block($remote,$rdata,$limit,$head);5390});5391}5392}5393}53945395sub git_search_grep_body {5396my($commitlist,$from,$to,$extra) =@_;5397$from=0unlessdefined$from;5398$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);53995400print"<table class=\"commit_search\">\n";5401my$alternate=1;5402for(my$i=$from;$i<=$to;$i++) {5403my%co= %{$commitlist->[$i]};5404if(!%co) {5405next;5406}5407my$commit=$co{'id'};5408if($alternate) {5409print"<tr class=\"dark\">\n";5410}else{5411print"<tr class=\"light\">\n";5412}5413$alternate^=1;5414print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5415 format_author_html('td', \%co,15,5) .5416"<td>".5417$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5418-class=>"list subject"},5419 chop_and_escape_str($co{'title'},50) ."<br/>");5420my$comment=$co{'comment'};5421foreachmy$line(@$comment) {5422if($line=~m/^(.*?)($search_regexp)(.*)$/i) {5423my($lead,$match,$trail) = ($1,$2,$3);5424$match= chop_str($match,70,5,'center');5425my$contextlen=int((80-length($match))/2);5426$contextlen=30if($contextlen>30);5427$lead= chop_str($lead,$contextlen,10,'left');5428$trail= chop_str($trail,$contextlen,10,'right');54295430$lead= esc_html($lead);5431$match= esc_html($match);5432$trail= esc_html($trail);54335434print"$lead<span class=\"match\">$match</span>$trail<br />";5435}5436}5437print"</td>\n".5438"<td class=\"link\">".5439$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5440" | ".5441$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .5442" | ".5443$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5444print"</td>\n".5445"</tr>\n";5446}5447if(defined$extra) {5448print"<tr>\n".5449"<td colspan=\"3\">$extra</td>\n".5450"</tr>\n";5451}5452print"</table>\n";5453}54545455## ======================================================================5456## ======================================================================5457## actions54585459sub git_project_list {5460my$order=$input_params{'order'};5461if(defined$order&&$order!~m/none|project|descr|owner|age/) {5462 die_error(400,"Unknown order parameter");5463}54645465my@list= git_get_projects_list();5466if(!@list) {5467 die_error(404,"No projects found");5468}54695470 git_header_html();5471if(defined$home_text&& -f $home_text) {5472print"<div class=\"index_include\">\n";5473 insert_file($home_text);5474print"</div>\n";5475}5476print$cgi->startform(-method=>"get") .5477"<p class=\"projsearch\">Search:\n".5478$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".5479"</p>".5480$cgi->end_form() ."\n";5481 git_project_list_body(\@list,$order);5482 git_footer_html();5483}54845485sub git_forks {5486my$order=$input_params{'order'};5487if(defined$order&&$order!~m/none|project|descr|owner|age/) {5488 die_error(400,"Unknown order parameter");5489}54905491my@list= git_get_projects_list($project);5492if(!@list) {5493 die_error(404,"No forks found");5494}54955496 git_header_html();5497 git_print_page_nav('','');5498 git_print_header_div('summary',"$projectforks");5499 git_project_list_body(\@list,$order);5500 git_footer_html();5501}55025503sub git_project_index {5504my@projects= git_get_projects_list();5505if(!@projects) {5506 die_error(404,"No projects found");5507}55085509print$cgi->header(5510-type =>'text/plain',5511-charset =>'utf-8',5512-content_disposition =>'inline; filename="index.aux"');55135514foreachmy$pr(@projects) {5515if(!exists$pr->{'owner'}) {5516$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");5517}55185519my($path,$owner) = ($pr->{'path'},$pr->{'owner'});5520# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '5521$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5522$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5523$path=~s/ /\+/g;5524$owner=~s/ /\+/g;55255526print"$path$owner\n";5527}5528}55295530sub git_summary {5531my$descr= git_get_project_description($project) ||"none";5532my%co= parse_commit("HEAD");5533my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();5534my$head=$co{'id'};5535my$remote_heads= gitweb_check_feature('remote_heads');55365537my$owner= git_get_project_owner($project);55385539my$refs= git_get_references();5540# These get_*_list functions return one more to allow us to see if5541# there are more ...5542my@taglist= git_get_tags_list(16);5543my@headlist= git_get_heads_list(16);5544my%remotedata=$remote_heads? git_get_remotes_list() : ();5545my@forklist;5546my$check_forks= gitweb_check_feature('forks');55475548if($check_forks) {5549# find forks of a project5550@forklist= git_get_projects_list($project);5551# filter out forks of forks5552@forklist= filter_forks_from_projects_list(\@forklist)5553if(@forklist);5554}55555556 git_header_html();5557 git_print_page_nav('summary','',$head);55585559print"<div class=\"title\"> </div>\n";5560print"<table class=\"projects_list\">\n".5561"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".5562"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";5563if(defined$cd{'rfc2822'}) {5564print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";5565}55665567# use per project git URL list in $projectroot/$project/cloneurl5568# or make project git URL from git base URL and project name5569my$url_tag="URL";5570my@url_list= git_get_project_url_list($project);5571@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;5572foreachmy$git_url(@url_list) {5573next unless$git_url;5574print format_repo_url($url_tag,$git_url);5575$url_tag="";5576}55775578# Tag cloud5579my$show_ctags= gitweb_check_feature('ctags');5580if($show_ctags) {5581my$ctags= git_get_project_ctags($project);5582if(%$ctags) {5583# without ability to add tags, don't show if there are none5584my$cloud= git_populate_project_tagcloud($ctags);5585print"<tr id=\"metadata_ctags\">".5586"<td>content tags</td>".5587"<td>".git_show_project_tagcloud($cloud,48)."</td>".5588"</tr>\n";5589}5590}55915592print"</table>\n";55935594# If XSS prevention is on, we don't include README.html.5595# TODO: Allow a readme in some safe format.5596if(!$prevent_xss&& -s "$projectroot/$project/README.html") {5597print"<div class=\"title\">readme</div>\n".5598"<div class=\"readme\">\n";5599 insert_file("$projectroot/$project/README.html");5600print"\n</div>\n";# class="readme"5601}56025603# we need to request one more than 16 (0..15) to check if5604# those 16 are all5605my@commitlist=$head? parse_commits($head,17) : ();5606if(@commitlist) {5607 git_print_header_div('shortlog');5608 git_shortlog_body(\@commitlist,0,15,$refs,5609$#commitlist<=15?undef:5610$cgi->a({-href => href(action=>"shortlog")},"..."));5611}56125613if(@taglist) {5614 git_print_header_div('tags');5615 git_tags_body(\@taglist,0,15,5616$#taglist<=15?undef:5617$cgi->a({-href => href(action=>"tags")},"..."));5618}56195620if(@headlist) {5621 git_print_header_div('heads');5622 git_heads_body(\@headlist,$head,0,15,5623$#headlist<=15?undef:5624$cgi->a({-href => href(action=>"heads")},"..."));5625}56265627if(%remotedata) {5628 git_print_header_div('remotes');5629 git_remotes_body(\%remotedata,15,$head);5630}56315632if(@forklist) {5633 git_print_header_div('forks');5634 git_project_list_body(\@forklist,'age',0,15,5635$#forklist<=15?undef:5636$cgi->a({-href => href(action=>"forks")},"..."),5637'no_header');5638}56395640 git_footer_html();5641}56425643sub git_tag {5644my%tag= parse_tag($hash);56455646if(!%tag) {5647 die_error(404,"Unknown tag object");5648}56495650my$head= git_get_head_hash($project);5651 git_header_html();5652 git_print_page_nav('','',$head,undef,$head);5653 git_print_header_div('commit', esc_html($tag{'name'}),$hash);5654print"<div class=\"title_text\">\n".5655"<table class=\"object_header\">\n".5656"<tr>\n".5657"<td>object</td>\n".5658"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5659$tag{'object'}) ."</td>\n".5660"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5661$tag{'type'}) ."</td>\n".5662"</tr>\n";5663if(defined($tag{'author'})) {5664 git_print_authorship_rows(\%tag,'author');5665}5666print"</table>\n\n".5667"</div>\n";5668print"<div class=\"page_body\">";5669my$comment=$tag{'comment'};5670foreachmy$line(@$comment) {5671chomp$line;5672print esc_html($line, -nbsp=>1) ."<br/>\n";5673}5674print"</div>\n";5675 git_footer_html();5676}56775678sub git_blame_common {5679my$format=shift||'porcelain';5680if($formateq'porcelain'&&$cgi->param('js')) {5681$format='incremental';5682$action='blame_incremental';# for page title etc5683}56845685# permissions5686 gitweb_check_feature('blame')5687or die_error(403,"Blame view not allowed");56885689# error checking5690 die_error(400,"No file name given")unless$file_name;5691$hash_base||= git_get_head_hash($project);5692 die_error(404,"Couldn't find base commit")unless$hash_base;5693my%co= parse_commit($hash_base)5694or die_error(404,"Commit not found");5695my$ftype="blob";5696if(!defined$hash) {5697$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5698or die_error(404,"Error looking up file");5699}else{5700$ftype= git_get_type($hash);5701if($ftype!~"blob") {5702 die_error(400,"Object is not a blob");5703}5704}57055706my$fd;5707if($formateq'incremental') {5708# get file contents (as base)5709open$fd,"-|", git_cmd(),'cat-file','blob',$hash5710or die_error(500,"Open git-cat-file failed");5711}elsif($formateq'data') {5712# run git-blame --incremental5713open$fd,"-|", git_cmd(),"blame","--incremental",5714$hash_base,"--",$file_name5715or die_error(500,"Open git-blame --incremental failed");5716}else{5717# run git-blame --porcelain5718open$fd,"-|", git_cmd(),"blame",'-p',5719$hash_base,'--',$file_name5720or die_error(500,"Open git-blame --porcelain failed");5721}57225723# incremental blame data returns early5724if($formateq'data') {5725print$cgi->header(5726-type=>"text/plain", -charset =>"utf-8",5727-status=>"200 OK");5728local$| =1;# output autoflush5729printwhile<$fd>;5730close$fd5731or print"ERROR$!\n";57325733print'END';5734if(defined$t0&& gitweb_check_feature('timed')) {5735print' '.5736 tv_interval($t0, [ gettimeofday() ]).5737' '.$number_of_git_cmds;5738}5739print"\n";57405741return;5742}57435744# page header5745 git_header_html();5746my$formats_nav=5747$cgi->a({-href => href(action=>"blob", -replay=>1)},5748"blob") .5749" | ";5750if($formateq'incremental') {5751$formats_nav.=5752$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5753"blame") ." (non-incremental)";5754}else{5755$formats_nav.=5756$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5757"blame") ." (incremental)";5758}5759$formats_nav.=5760" | ".5761$cgi->a({-href => href(action=>"history", -replay=>1)},5762"history") .5763" | ".5764$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5765"HEAD");5766 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5767 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5768 git_print_page_path($file_name,$ftype,$hash_base);57695770# page body5771if($formateq'incremental') {5772print"<noscript>\n<div class=\"error\"><center><b>\n".5773"This page requires JavaScript to run.\nUse ".5774$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5775'this page').5776" instead.\n".5777"</b></center></div>\n</noscript>\n";57785779print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5780}57815782print qq!<div class="page_body">\n!;5783print qq!<div id="progress_info">.../ ...</div>\n!5784if($formateq'incremental');5785print qq!<table id="blame_table"class="blame" width="100%">\n!.5786#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5787 qq!<thead>\n!.5788 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5789 qq!</thead>\n!.5790 qq!<tbody>\n!;57915792my@rev_color=qw(light dark);5793my$num_colors=scalar(@rev_color);5794my$current_color=0;57955796if($formateq'incremental') {5797my$color_class=$rev_color[$current_color];57985799#contents of a file5800my$linenr=0;5801 LINE:5802while(my$line= <$fd>) {5803chomp$line;5804$linenr++;58055806print qq!<tr id="l$linenr"class="$color_class">!.5807 qq!<td class="sha1"><a href=""> </a></td>!.5808 qq!<td class="linenr">!.5809 qq!<a class="linenr" href="">$linenr</a></td>!;5810print qq!<td class="pre">! . esc_html($line) ."</td>\n";5811print qq!</tr>\n!;5812}58135814}else{# porcelain, i.e. ordinary blame5815my%metainfo= ();# saves information about commits58165817# blame data5818 LINE:5819while(my$line= <$fd>) {5820chomp$line;5821# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5822# no <lines in group> for subsequent lines in group of lines5823my($full_rev,$orig_lineno,$lineno,$group_size) =5824($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5825if(!exists$metainfo{$full_rev}) {5826$metainfo{$full_rev} = {'nprevious'=>0};5827}5828my$meta=$metainfo{$full_rev};5829my$data;5830while($data= <$fd>) {5831chomp$data;5832last if($data=~s/^\t//);# contents of line5833if($data=~/^(\S+)(?: (.*))?$/) {5834$meta->{$1} =$2unlessexists$meta->{$1};5835}5836if($data=~/^previous /) {5837$meta->{'nprevious'}++;5838}5839}5840my$short_rev=substr($full_rev,0,8);5841my$author=$meta->{'author'};5842my%date=5843 parse_date($meta->{'author-time'},$meta->{'author-tz'});5844my$date=$date{'iso-tz'};5845if($group_size) {5846$current_color= ($current_color+1) %$num_colors;5847}5848my$tr_class=$rev_color[$current_color];5849$tr_class.=' boundary'if(exists$meta->{'boundary'});5850$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5851$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5852print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5853if($group_size) {5854print"<td class=\"sha1\"";5855print" title=\"". esc_html($author) .",$date\"";5856print" rowspan=\"$group_size\""if($group_size>1);5857print">";5858print$cgi->a({-href => href(action=>"commit",5859 hash=>$full_rev,5860 file_name=>$file_name)},5861 esc_html($short_rev));5862if($group_size>=2) {5863my@author_initials= ($author=~/\b([[:upper:]])\B/g);5864if(@author_initials) {5865print"<br />".5866 esc_html(join('',@author_initials));5867# or join('.', ...)5868}5869}5870print"</td>\n";5871}5872# 'previous' <sha1 of parent commit> <filename at commit>5873if(exists$meta->{'previous'} &&5874$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5875$meta->{'parent'} =$1;5876$meta->{'file_parent'} = unquote($2);5877}5878my$linenr_commit=5879exists($meta->{'parent'}) ?5880$meta->{'parent'} :$full_rev;5881my$linenr_filename=5882exists($meta->{'file_parent'}) ?5883$meta->{'file_parent'} : unquote($meta->{'filename'});5884my$blamed= href(action =>'blame',5885 file_name =>$linenr_filename,5886 hash_base =>$linenr_commit);5887print"<td class=\"linenr\">";5888print$cgi->a({ -href =>"$blamed#l$orig_lineno",5889-class=>"linenr"},5890 esc_html($lineno));5891print"</td>";5892print"<td class=\"pre\">". esc_html($data) ."</td>\n";5893print"</tr>\n";5894}# end while58955896}58975898# footer5899print"</tbody>\n".5900"</table>\n";# class="blame"5901print"</div>\n";# class="blame_body"5902close$fd5903or print"Reading blob failed\n";59045905 git_footer_html();5906}59075908sub git_blame {5909 git_blame_common();5910}59115912sub git_blame_incremental {5913 git_blame_common('incremental');5914}59155916sub git_blame_data {5917 git_blame_common('data');5918}59195920sub git_tags {5921my$head= git_get_head_hash($project);5922 git_header_html();5923 git_print_page_nav('','',$head,undef,$head,format_ref_views('tags'));5924 git_print_header_div('summary',$project);59255926my@tagslist= git_get_tags_list();5927if(@tagslist) {5928 git_tags_body(\@tagslist);5929}5930 git_footer_html();5931}59325933sub git_heads {5934my$head= git_get_head_hash($project);5935 git_header_html();5936 git_print_page_nav('','',$head,undef,$head,format_ref_views('heads'));5937 git_print_header_div('summary',$project);59385939my@headslist= git_get_heads_list();5940if(@headslist) {5941 git_heads_body(\@headslist,$head);5942}5943 git_footer_html();5944}59455946# used both for single remote view and for list of all the remotes5947sub git_remotes {5948 gitweb_check_feature('remote_heads')5949or die_error(403,"Remote heads view is disabled");59505951my$head= git_get_head_hash($project);5952my$remote=$input_params{'hash'};59535954my$remotedata= git_get_remotes_list($remote);5955 die_error(500,"Unable to get remote information")unlessdefined$remotedata;59565957unless(%$remotedata) {5958 die_error(404,defined$remote?5959"Remote$remotenot found":5960"No remotes found");5961}59625963 git_header_html(undef,undef, -action_extra =>$remote);5964 git_print_page_nav('','',$head,undef,$head,5965 format_ref_views($remote?'':'remotes'));59665967 fill_remote_heads($remotedata);5968if(defined$remote) {5969 git_print_header_div('remotes',"$remoteremote for$project");5970 git_remote_block($remote,$remotedata->{$remote},undef,$head);5971}else{5972 git_print_header_div('summary',"$projectremotes");5973 git_remotes_body($remotedata,undef,$head);5974}59755976 git_footer_html();5977}59785979sub git_blob_plain {5980my$type=shift;5981my$expires;59825983if(!defined$hash) {5984if(defined$file_name) {5985my$base=$hash_base|| git_get_head_hash($project);5986$hash= git_get_hash_by_path($base,$file_name,"blob")5987or die_error(404,"Cannot find file");5988}else{5989 die_error(400,"No file name defined");5990}5991}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5992# blobs defined by non-textual hash id's can be cached5993$expires="+1d";5994}59955996open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5997or die_error(500,"Open git-cat-file blob '$hash' failed");59985999# content-type (can include charset)6000$type= blob_contenttype($fd,$file_name,$type);60016002# "save as" filename, even when no $file_name is given6003my$save_as="$hash";6004if(defined$file_name) {6005$save_as=$file_name;6006}elsif($type=~m/^text\//) {6007$save_as.='.txt';6008}60096010# With XSS prevention on, blobs of all types except a few known safe6011# ones are served with "Content-Disposition: attachment" to make sure6012# they don't run in our security domain. For certain image types,6013# blob view writes an <img> tag referring to blob_plain view, and we6014# want to be sure not to break that by serving the image as an6015# attachment (though Firefox 3 doesn't seem to care).6016my$sandbox=$prevent_xss&&6017$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;60186019print$cgi->header(6020-type =>$type,6021-expires =>$expires,6022-content_disposition =>6023($sandbox?'attachment':'inline')6024.'; filename="'.$save_as.'"');6025local$/=undef;6026binmode STDOUT,':raw';6027print<$fd>;6028binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi6029close$fd;6030}60316032sub git_blob {6033my$expires;60346035if(!defined$hash) {6036if(defined$file_name) {6037my$base=$hash_base|| git_get_head_hash($project);6038$hash= git_get_hash_by_path($base,$file_name,"blob")6039or die_error(404,"Cannot find file");6040}else{6041 die_error(400,"No file name defined");6042}6043}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {6044# blobs defined by non-textual hash id's can be cached6045$expires="+1d";6046}60476048my$have_blame= gitweb_check_feature('blame');6049open my$fd,"-|", git_cmd(),"cat-file","blob",$hash6050or die_error(500,"Couldn't cat$file_name,$hash");6051my$mimetype= blob_mimetype($fd,$file_name);6052# use 'blob_plain' (aka 'raw') view for files that cannot be displayed6053if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {6054close$fd;6055return git_blob_plain($mimetype);6056}6057# we can have blame only for text/* mimetype6058$have_blame&&= ($mimetype=~m!^text/!);60596060my$highlight= gitweb_check_feature('highlight');6061my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);6062$fd= run_highlighter($fd,$highlight,$syntax)6063if$syntax;60646065 git_header_html(undef,$expires);6066my$formats_nav='';6067if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6068if(defined$file_name) {6069if($have_blame) {6070$formats_nav.=6071$cgi->a({-href => href(action=>"blame", -replay=>1)},6072"blame") .6073" | ";6074}6075$formats_nav.=6076$cgi->a({-href => href(action=>"history", -replay=>1)},6077"history") .6078" | ".6079$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},6080"raw") .6081" | ".6082$cgi->a({-href => href(action=>"blob",6083 hash_base=>"HEAD", file_name=>$file_name)},6084"HEAD");6085}else{6086$formats_nav.=6087$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},6088"raw");6089}6090 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6091 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6092}else{6093print"<div class=\"page_nav\">\n".6094"<br/><br/></div>\n".6095"<div class=\"title\">".esc_html($hash)."</div>\n";6096}6097 git_print_page_path($file_name,"blob",$hash_base);6098print"<div class=\"page_body\">\n";6099if($mimetype=~m!^image/!) {6100print qq!<img type="!.esc_attr($mimetype).qq!"!;6101if($file_name) {6102print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;6103}6104print qq! src="! .6105 href(action=>"blob_plain", hash=>$hash,6106 hash_base=>$hash_base, file_name=>$file_name) .6107 qq!"/>\n!;6108}else{6109my$nr;6110while(my$line= <$fd>) {6111chomp$line;6112$nr++;6113$line= untabify($line);6114printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,6115$nr, esc_attr(href(-replay =>1)),$nr,$nr,$syntax?$line: esc_html($line, -nbsp=>1);6116}6117}6118close$fd6119or print"Reading blob failed.\n";6120print"</div>";6121 git_footer_html();6122}61236124sub git_tree {6125if(!defined$hash_base) {6126$hash_base="HEAD";6127}6128if(!defined$hash) {6129if(defined$file_name) {6130$hash= git_get_hash_by_path($hash_base,$file_name,"tree");6131}else{6132$hash=$hash_base;6133}6134}6135 die_error(404,"No such tree")unlessdefined($hash);61366137my$show_sizes= gitweb_check_feature('show-sizes');6138my$have_blame= gitweb_check_feature('blame');61396140my@entries= ();6141{6142local$/="\0";6143open my$fd,"-|", git_cmd(),"ls-tree",'-z',6144($show_sizes?'-l': ()),@extra_options,$hash6145or die_error(500,"Open git-ls-tree failed");6146@entries=map{chomp;$_} <$fd>;6147close$fd6148or die_error(404,"Reading tree failed");6149}61506151my$refs= git_get_references();6152my$ref= format_ref_marker($refs,$hash_base);6153 git_header_html();6154my$basedir='';6155if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6156my@views_nav= ();6157if(defined$file_name) {6158push@views_nav,6159$cgi->a({-href => href(action=>"history", -replay=>1)},6160"history"),6161$cgi->a({-href => href(action=>"tree",6162 hash_base=>"HEAD", file_name=>$file_name)},6163"HEAD"),6164}6165my$snapshot_links= format_snapshot_links($hash);6166if(defined$snapshot_links) {6167# FIXME: Should be available when we have no hash base as well.6168push@views_nav,$snapshot_links;6169}6170 git_print_page_nav('tree','',$hash_base,undef,undef,6171join(' | ',@views_nav));6172 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);6173}else{6174undef$hash_base;6175print"<div class=\"page_nav\">\n";6176print"<br/><br/></div>\n";6177print"<div class=\"title\">".esc_html($hash)."</div>\n";6178}6179if(defined$file_name) {6180$basedir=$file_name;6181if($basedirne''&&substr($basedir, -1)ne'/') {6182$basedir.='/';6183}6184 git_print_page_path($file_name,'tree',$hash_base);6185}6186print"<div class=\"page_body\">\n";6187print"<table class=\"tree\">\n";6188my$alternate=1;6189# '..' (top directory) link if possible6190if(defined$hash_base&&6191defined$file_name&&$file_name=~m![^/]+$!) {6192if($alternate) {6193print"<tr class=\"dark\">\n";6194}else{6195print"<tr class=\"light\">\n";6196}6197$alternate^=1;61986199my$up=$file_name;6200$up=~s!/?[^/]+$!!;6201undef$upunless$up;6202# based on git_print_tree_entry6203print'<td class="mode">'. mode_str('040000') ."</td>\n";6204print'<td class="size"> </td>'."\n"if$show_sizes;6205print'<td class="list">';6206print$cgi->a({-href => href(action=>"tree",6207 hash_base=>$hash_base,6208 file_name=>$up)},6209"..");6210print"</td>\n";6211print"<td class=\"link\"></td>\n";62126213print"</tr>\n";6214}6215foreachmy$line(@entries) {6216my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);62176218if($alternate) {6219print"<tr class=\"dark\">\n";6220}else{6221print"<tr class=\"light\">\n";6222}6223$alternate^=1;62246225 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);62266227print"</tr>\n";6228}6229print"</table>\n".6230"</div>";6231 git_footer_html();6232}62336234sub snapshot_name {6235my($project,$hash) =@_;62366237# path/to/project.git -> project6238# path/to/project/.git -> project6239my$name= to_utf8($project);6240$name=~ s,([^/])/*\.git$,$1,;6241$name= basename($name);6242# sanitize name6243$name=~s/[[:cntrl:]]/?/g;62446245my$ver=$hash;6246if($hash=~/^[0-9a-fA-F]+$/) {6247# shorten SHA-1 hash6248my$full_hash= git_get_full_hash($project,$hash);6249if($full_hash=~/^$hash/&&length($hash) >7) {6250$ver= git_get_short_hash($project,$hash);6251}6252}elsif($hash=~m!^refs/tags/(.*)$!) {6253# tags don't need shortened SHA-1 hash6254$ver=$1;6255}else{6256# branches and other need shortened SHA-1 hash6257if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {6258$ver=$1;6259}6260$ver.='-'. git_get_short_hash($project,$hash);6261}6262# in case of hierarchical branch names6263$ver=~s!/!.!g;62646265# name = project-version_string6266$name="$name-$ver";62676268returnwantarray? ($name,$name) :$name;6269}62706271sub git_snapshot {6272my$format=$input_params{'snapshot_format'};6273if(!@snapshot_fmts) {6274 die_error(403,"Snapshots not allowed");6275}6276# default to first supported snapshot format6277$format||=$snapshot_fmts[0];6278if($format!~m/^[a-z0-9]+$/) {6279 die_error(400,"Invalid snapshot format parameter");6280}elsif(!exists($known_snapshot_formats{$format})) {6281 die_error(400,"Unknown snapshot format");6282}elsif($known_snapshot_formats{$format}{'disabled'}) {6283 die_error(403,"Snapshot format not allowed");6284}elsif(!grep($_eq$format,@snapshot_fmts)) {6285 die_error(403,"Unsupported snapshot format");6286}62876288my$type= git_get_type("$hash^{}");6289if(!$type) {6290 die_error(404,'Object does not exist');6291}elsif($typeeq'blob') {6292 die_error(400,'Object is not a tree-ish');6293}62946295my($name,$prefix) = snapshot_name($project,$hash);6296my$filename="$name$known_snapshot_formats{$format}{'suffix'}";6297my$cmd= quote_command(6298 git_cmd(),'archive',6299"--format=$known_snapshot_formats{$format}{'format'}",6300"--prefix=$prefix/",$hash);6301if(exists$known_snapshot_formats{$format}{'compressor'}) {6302$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});6303}63046305$filename=~s/(["\\])/\\$1/g;6306print$cgi->header(6307-type =>$known_snapshot_formats{$format}{'type'},6308-content_disposition =>'inline; filename="'.$filename.'"',6309-status =>'200 OK');63106311open my$fd,"-|",$cmd6312or die_error(500,"Execute git-archive failed");6313binmode STDOUT,':raw';6314print<$fd>;6315binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi6316close$fd;6317}63186319sub git_log_generic {6320my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;63216322my$head= git_get_head_hash($project);6323if(!defined$base) {6324$base=$head;6325}6326if(!defined$page) {6327$page=0;6328}6329my$refs= git_get_references();63306331my$commit_hash=$base;6332if(defined$parent) {6333$commit_hash="$parent..$base";6334}6335my@commitlist=6336 parse_commits($commit_hash,101, (100*$page),6337defined$file_name? ($file_name,"--full-history") : ());63386339my$ftype;6340if(!defined$file_hash&&defined$file_name) {6341# some commits could have deleted file in question,6342# and not have it in tree, but one of them has to have it6343for(my$i=0;$i<@commitlist;$i++) {6344$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);6345last ifdefined$file_hash;6346}6347}6348if(defined$file_hash) {6349$ftype= git_get_type($file_hash);6350}6351if(defined$file_name&& !defined$ftype) {6352 die_error(500,"Unknown type of object");6353}6354my%co;6355if(defined$file_name) {6356%co= parse_commit($base)6357or die_error(404,"Unknown commit object");6358}635963606361my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);6362my$next_link='';6363if($#commitlist>=100) {6364$next_link=6365$cgi->a({-href => href(-replay=>1, page=>$page+1),6366-accesskey =>"n", -title =>"Alt-n"},"next");6367}6368my$patch_max= gitweb_get_feature('patches');6369if($patch_max&& !defined$file_name) {6370if($patch_max<0||@commitlist<=$patch_max) {6371$paging_nav.=" ⋅ ".6372$cgi->a({-href => href(action=>"patches", -replay=>1)},6373"patches");6374}6375}63766377 git_header_html();6378 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);6379if(defined$file_name) {6380 git_print_header_div('commit', esc_html($co{'title'}),$base);6381}else{6382 git_print_header_div('summary',$project)6383}6384 git_print_page_path($file_name,$ftype,$hash_base)6385if(defined$file_name);63866387$body_subr->(\@commitlist,0,99,$refs,$next_link,6388$file_name,$file_hash,$ftype);63896390 git_footer_html();6391}63926393sub git_log {6394 git_log_generic('log', \&git_log_body,6395$hash,$hash_parent);6396}63976398sub git_commit {6399$hash||=$hash_base||"HEAD";6400my%co= parse_commit($hash)6401or die_error(404,"Unknown commit object");64026403my$parent=$co{'parent'};6404my$parents=$co{'parents'};# listref64056406# we need to prepare $formats_nav before any parameter munging6407my$formats_nav;6408if(!defined$parent) {6409# --root commitdiff6410$formats_nav.='(initial)';6411}elsif(@$parents==1) {6412# single parent commit6413$formats_nav.=6414'(parent: '.6415$cgi->a({-href => href(action=>"commit",6416 hash=>$parent)},6417 esc_html(substr($parent,0,7))) .6418')';6419}else{6420# merge commit6421$formats_nav.=6422'(merge: '.6423join(' ',map{6424$cgi->a({-href => href(action=>"commit",6425 hash=>$_)},6426 esc_html(substr($_,0,7)));6427}@$parents) .6428')';6429}6430if(gitweb_check_feature('patches') &&@$parents<=1) {6431$formats_nav.=" | ".6432$cgi->a({-href => href(action=>"patch", -replay=>1)},6433"patch");6434}64356436if(!defined$parent) {6437$parent="--root";6438}6439my@difftree;6440open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",6441@diff_opts,6442(@$parents<=1?$parent:'-c'),6443$hash,"--"6444or die_error(500,"Open git-diff-tree failed");6445@difftree=map{chomp;$_} <$fd>;6446close$fdor die_error(404,"Reading git-diff-tree failed");64476448# non-textual hash id's can be cached6449my$expires;6450if($hash=~m/^[0-9a-fA-F]{40}$/) {6451$expires="+1d";6452}6453my$refs= git_get_references();6454my$ref= format_ref_marker($refs,$co{'id'});64556456 git_header_html(undef,$expires);6457 git_print_page_nav('commit','',6458$hash,$co{'tree'},$hash,6459$formats_nav);64606461if(defined$co{'parent'}) {6462 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);6463}else{6464 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);6465}6466print"<div class=\"title_text\">\n".6467"<table class=\"object_header\">\n";6468 git_print_authorship_rows(\%co);6469print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";6470print"<tr>".6471"<td>tree</td>".6472"<td class=\"sha1\">".6473$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),6474class=>"list"},$co{'tree'}) .6475"</td>".6476"<td class=\"link\">".6477$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},6478"tree");6479my$snapshot_links= format_snapshot_links($hash);6480if(defined$snapshot_links) {6481print" | ".$snapshot_links;6482}6483print"</td>".6484"</tr>\n";64856486foreachmy$par(@$parents) {6487print"<tr>".6488"<td>parent</td>".6489"<td class=\"sha1\">".6490$cgi->a({-href => href(action=>"commit", hash=>$par),6491class=>"list"},$par) .6492"</td>".6493"<td class=\"link\">".6494$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .6495" | ".6496$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .6497"</td>".6498"</tr>\n";6499}6500print"</table>".6501"</div>\n";65026503print"<div class=\"page_body\">\n";6504 git_print_log($co{'comment'});6505print"</div>\n";65066507 git_difftree_body(\@difftree,$hash,@$parents);65086509 git_footer_html();6510}65116512sub git_object {6513# object is defined by:6514# - hash or hash_base alone6515# - hash_base and file_name6516my$type;65176518# - hash or hash_base alone6519if($hash|| ($hash_base&& !defined$file_name)) {6520my$object_id=$hash||$hash_base;65216522open my$fd,"-|", quote_command(6523 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'6524or die_error(404,"Object does not exist");6525$type= <$fd>;6526chomp$type;6527close$fd6528or die_error(404,"Object does not exist");65296530# - hash_base and file_name6531}elsif($hash_base&&defined$file_name) {6532$file_name=~ s,/+$,,;65336534system(git_cmd(),"cat-file",'-e',$hash_base) ==06535or die_error(404,"Base object does not exist");65366537# here errors should not hapen6538open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name6539or die_error(500,"Open git-ls-tree failed");6540my$line= <$fd>;6541close$fd;65426543#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'6544unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {6545 die_error(404,"File or directory for given base does not exist");6546}6547$type=$2;6548$hash=$3;6549}else{6550 die_error(400,"Not enough information to find object");6551}65526553print$cgi->redirect(-uri => href(action=>$type, -full=>1,6554 hash=>$hash, hash_base=>$hash_base,6555 file_name=>$file_name),6556-status =>'302 Found');6557}65586559sub git_blobdiff {6560my$format=shift||'html';65616562my$fd;6563my@difftree;6564my%diffinfo;6565my$expires;65666567# preparing $fd and %diffinfo for git_patchset_body6568# new style URI6569if(defined$hash_base&&defined$hash_parent_base) {6570if(defined$file_name) {6571# read raw output6572open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6573$hash_parent_base,$hash_base,6574"--", (defined$file_parent?$file_parent: ()),$file_name6575or die_error(500,"Open git-diff-tree failed");6576@difftree=map{chomp;$_} <$fd>;6577close$fd6578or die_error(404,"Reading git-diff-tree failed");6579@difftree6580or die_error(404,"Blob diff not found");65816582}elsif(defined$hash&&6583$hash=~/[0-9a-fA-F]{40}/) {6584# try to find filename from $hash65856586# read filtered raw output6587open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6588$hash_parent_base,$hash_base,"--"6589or die_error(500,"Open git-diff-tree failed");6590@difftree=6591# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'6592# $hash == to_id6593grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}6594map{chomp;$_} <$fd>;6595close$fd6596or die_error(404,"Reading git-diff-tree failed");6597@difftree6598or die_error(404,"Blob diff not found");65996600}else{6601 die_error(400,"Missing one of the blob diff parameters");6602}66036604if(@difftree>1) {6605 die_error(400,"Ambiguous blob diff specification");6606}66076608%diffinfo= parse_difftree_raw_line($difftree[0]);6609$file_parent||=$diffinfo{'from_file'} ||$file_name;6610$file_name||=$diffinfo{'to_file'};66116612$hash_parent||=$diffinfo{'from_id'};6613$hash||=$diffinfo{'to_id'};66146615# non-textual hash id's can be cached6616if($hash_base=~m/^[0-9a-fA-F]{40}$/&&6617$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {6618$expires='+1d';6619}66206621# open patch output6622open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6623'-p', ($formateq'html'?"--full-index": ()),6624$hash_parent_base,$hash_base,6625"--", (defined$file_parent?$file_parent: ()),$file_name6626or die_error(500,"Open git-diff-tree failed");6627}66286629# old/legacy style URI -- not generated anymore since 1.4.3.6630if(!%diffinfo) {6631 die_error('404 Not Found',"Missing one of the blob diff parameters")6632}66336634# header6635if($formateq'html') {6636my$formats_nav=6637$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},6638"raw");6639 git_header_html(undef,$expires);6640if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6641 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6642 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6643}else{6644print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";6645print"<div class=\"title\">".esc_html("$hashvs$hash_parent")."</div>\n";6646}6647if(defined$file_name) {6648 git_print_page_path($file_name,"blob",$hash_base);6649}else{6650print"<div class=\"page_path\"></div>\n";6651}66526653}elsif($formateq'plain') {6654print$cgi->header(6655-type =>'text/plain',6656-charset =>'utf-8',6657-expires =>$expires,6658-content_disposition =>'inline; filename="'."$file_name".'.patch"');66596660print"X-Git-Url: ".$cgi->self_url() ."\n\n";66616662}else{6663 die_error(400,"Unknown blobdiff format");6664}66656666# patch6667if($formateq'html') {6668print"<div class=\"page_body\">\n";66696670 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);6671close$fd;66726673print"</div>\n";# class="page_body"6674 git_footer_html();66756676}else{6677while(my$line= <$fd>) {6678$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;6679$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;66806681print$line;66826683last if$line=~m!^\+\+\+!;6684}6685local$/=undef;6686print<$fd>;6687close$fd;6688}6689}66906691sub git_blobdiff_plain {6692 git_blobdiff('plain');6693}66946695sub git_commitdiff {6696my%params=@_;6697my$format=$params{-format} ||'html';66986699my($patch_max) = gitweb_get_feature('patches');6700if($formateq'patch') {6701 die_error(403,"Patch view not allowed")unless$patch_max;6702}67036704$hash||=$hash_base||"HEAD";6705my%co= parse_commit($hash)6706or die_error(404,"Unknown commit object");67076708# choose format for commitdiff for merge6709if(!defined$hash_parent&& @{$co{'parents'}} >1) {6710$hash_parent='--cc';6711}6712# we need to prepare $formats_nav before almost any parameter munging6713my$formats_nav;6714if($formateq'html') {6715$formats_nav=6716$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6717"raw");6718if($patch_max&& @{$co{'parents'}} <=1) {6719$formats_nav.=" | ".6720$cgi->a({-href => href(action=>"patch", -replay=>1)},6721"patch");6722}67236724if(defined$hash_parent&&6725$hash_parentne'-c'&&$hash_parentne'--cc') {6726# commitdiff with two commits given6727my$hash_parent_short=$hash_parent;6728if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6729$hash_parent_short=substr($hash_parent,0,7);6730}6731$formats_nav.=6732' (from';6733for(my$i=0;$i< @{$co{'parents'}};$i++) {6734if($co{'parents'}[$i]eq$hash_parent) {6735$formats_nav.=' parent '. ($i+1);6736last;6737}6738}6739$formats_nav.=': '.6740$cgi->a({-href => href(action=>"commitdiff",6741 hash=>$hash_parent)},6742 esc_html($hash_parent_short)) .6743')';6744}elsif(!$co{'parent'}) {6745# --root commitdiff6746$formats_nav.=' (initial)';6747}elsif(scalar@{$co{'parents'}} ==1) {6748# single parent commit6749$formats_nav.=6750' (parent: '.6751$cgi->a({-href => href(action=>"commitdiff",6752 hash=>$co{'parent'})},6753 esc_html(substr($co{'parent'},0,7))) .6754')';6755}else{6756# merge commit6757if($hash_parenteq'--cc') {6758$formats_nav.=' | '.6759$cgi->a({-href => href(action=>"commitdiff",6760 hash=>$hash, hash_parent=>'-c')},6761'combined');6762}else{# $hash_parent eq '-c'6763$formats_nav.=' | '.6764$cgi->a({-href => href(action=>"commitdiff",6765 hash=>$hash, hash_parent=>'--cc')},6766'compact');6767}6768$formats_nav.=6769' (merge: '.6770join(' ',map{6771$cgi->a({-href => href(action=>"commitdiff",6772 hash=>$_)},6773 esc_html(substr($_,0,7)));6774} @{$co{'parents'}} ) .6775')';6776}6777}67786779my$hash_parent_param=$hash_parent;6780if(!defined$hash_parent_param) {6781# --cc for multiple parents, --root for parentless6782$hash_parent_param=6783@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6784}67856786# read commitdiff6787my$fd;6788my@difftree;6789if($formateq'html') {6790open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6791"--no-commit-id","--patch-with-raw","--full-index",6792$hash_parent_param,$hash,"--"6793or die_error(500,"Open git-diff-tree failed");67946795while(my$line= <$fd>) {6796chomp$line;6797# empty line ends raw part of diff-tree output6798last unless$line;6799push@difftree,scalar parse_difftree_raw_line($line);6800}68016802}elsif($formateq'plain') {6803open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6804'-p',$hash_parent_param,$hash,"--"6805or die_error(500,"Open git-diff-tree failed");6806}elsif($formateq'patch') {6807# For commit ranges, we limit the output to the number of6808# patches specified in the 'patches' feature.6809# For single commits, we limit the output to a single patch,6810# diverging from the git-format-patch default.6811my@commit_spec= ();6812if($hash_parent) {6813if($patch_max>0) {6814push@commit_spec,"-$patch_max";6815}6816push@commit_spec,'-n',"$hash_parent..$hash";6817}else{6818if($params{-single}) {6819push@commit_spec,'-1';6820}else{6821if($patch_max>0) {6822push@commit_spec,"-$patch_max";6823}6824push@commit_spec,"-n";6825}6826push@commit_spec,'--root',$hash;6827}6828open$fd,"-|", git_cmd(),"format-patch",@diff_opts,6829'--encoding=utf8','--stdout',@commit_spec6830or die_error(500,"Open git-format-patch failed");6831}else{6832 die_error(400,"Unknown commitdiff format");6833}68346835# non-textual hash id's can be cached6836my$expires;6837if($hash=~m/^[0-9a-fA-F]{40}$/) {6838$expires="+1d";6839}68406841# write commit message6842if($formateq'html') {6843my$refs= git_get_references();6844my$ref= format_ref_marker($refs,$co{'id'});68456846 git_header_html(undef,$expires);6847 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6848 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6849print"<div class=\"title_text\">\n".6850"<table class=\"object_header\">\n";6851 git_print_authorship_rows(\%co);6852print"</table>".6853"</div>\n";6854print"<div class=\"page_body\">\n";6855if(@{$co{'comment'}} >1) {6856print"<div class=\"log\">\n";6857 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6858print"</div>\n";# class="log"6859}68606861}elsif($formateq'plain') {6862my$refs= git_get_references("tags");6863my$tagname= git_get_rev_name_tags($hash);6864my$filename= basename($project) ."-$hash.patch";68656866print$cgi->header(6867-type =>'text/plain',6868-charset =>'utf-8',6869-expires =>$expires,6870-content_disposition =>'inline; filename="'."$filename".'"');6871my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6872print"From: ". to_utf8($co{'author'}) ."\n";6873print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6874print"Subject: ". to_utf8($co{'title'}) ."\n";68756876print"X-Git-Tag:$tagname\n"if$tagname;6877print"X-Git-Url: ".$cgi->self_url() ."\n\n";68786879foreachmy$line(@{$co{'comment'}}) {6880print to_utf8($line) ."\n";6881}6882print"---\n\n";6883}elsif($formateq'patch') {6884my$filename= basename($project) ."-$hash.patch";68856886print$cgi->header(6887-type =>'text/plain',6888-charset =>'utf-8',6889-expires =>$expires,6890-content_disposition =>'inline; filename="'."$filename".'"');6891}68926893# write patch6894if($formateq'html') {6895my$use_parents= !defined$hash_parent||6896$hash_parenteq'-c'||$hash_parenteq'--cc';6897 git_difftree_body(\@difftree,$hash,6898$use_parents? @{$co{'parents'}} :$hash_parent);6899print"<br/>\n";69006901 git_patchset_body($fd, \@difftree,$hash,6902$use_parents? @{$co{'parents'}} :$hash_parent);6903close$fd;6904print"</div>\n";# class="page_body"6905 git_footer_html();69066907}elsif($formateq'plain') {6908local$/=undef;6909print<$fd>;6910close$fd6911or print"Reading git-diff-tree failed\n";6912}elsif($formateq'patch') {6913local$/=undef;6914print<$fd>;6915close$fd6916or print"Reading git-format-patch failed\n";6917}6918}69196920sub git_commitdiff_plain {6921 git_commitdiff(-format =>'plain');6922}69236924# format-patch-style patches6925sub git_patch {6926 git_commitdiff(-format =>'patch', -single =>1);6927}69286929sub git_patches {6930 git_commitdiff(-format =>'patch');6931}69326933sub git_history {6934 git_log_generic('history', \&git_history_body,6935$hash_base,$hash_parent_base,6936$file_name,$hash);6937}69386939sub git_search {6940 gitweb_check_feature('search')or die_error(403,"Search is disabled");6941if(!defined$searchtext) {6942 die_error(400,"Text field is empty");6943}6944if(!defined$hash) {6945$hash= git_get_head_hash($project);6946}6947my%co= parse_commit($hash);6948if(!%co) {6949 die_error(404,"Unknown commit object");6950}6951if(!defined$page) {6952$page=0;6953}69546955$searchtype||='commit';6956if($searchtypeeq'pickaxe') {6957# pickaxe may take all resources of your box and run for several minutes6958# with every query - so decide by yourself how public you make this feature6959 gitweb_check_feature('pickaxe')6960or die_error(403,"Pickaxe is disabled");6961}6962if($searchtypeeq'grep') {6963 gitweb_check_feature('grep')6964or die_error(403,"Grep is disabled");6965}69666967 git_header_html();69686969if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6970my$greptype;6971if($searchtypeeq'commit') {6972$greptype="--grep=";6973}elsif($searchtypeeq'author') {6974$greptype="--author=";6975}elsif($searchtypeeq'committer') {6976$greptype="--committer=";6977}6978$greptype.=$searchtext;6979my@commitlist= parse_commits($hash,101, (100*$page),undef,6980$greptype,'--regexp-ignore-case',6981$search_use_regexp?'--extended-regexp':'--fixed-strings');69826983my$paging_nav='';6984if($page>0) {6985$paging_nav.=6986$cgi->a({-href => href(action=>"search", hash=>$hash,6987 searchtext=>$searchtext,6988 searchtype=>$searchtype)},6989"first");6990$paging_nav.=" ⋅ ".6991$cgi->a({-href => href(-replay=>1, page=>$page-1),6992-accesskey =>"p", -title =>"Alt-p"},"prev");6993}else{6994$paging_nav.="first";6995$paging_nav.=" ⋅ prev";6996}6997my$next_link='';6998if($#commitlist>=100) {6999$next_link=7000$cgi->a({-href => href(-replay=>1, page=>$page+1),7001-accesskey =>"n", -title =>"Alt-n"},"next");7002$paging_nav.=" ⋅$next_link";7003}else{7004$paging_nav.=" ⋅ next";7005}70067007 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);7008 git_print_header_div('commit', esc_html($co{'title'}),$hash);7009if($page==0&& !@commitlist) {7010print"<p>No match.</p>\n";7011}else{7012 git_search_grep_body(\@commitlist,0,99,$next_link);7013}7014}70157016if($searchtypeeq'pickaxe') {7017 git_print_page_nav('','',$hash,$co{'tree'},$hash);7018 git_print_header_div('commit', esc_html($co{'title'}),$hash);70197020print"<table class=\"pickaxe search\">\n";7021my$alternate=1;7022local$/="\n";7023open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,7024'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",7025($search_use_regexp?'--pickaxe-regex': ());7026undef%co;7027my@files;7028while(my$line= <$fd>) {7029chomp$line;7030next unless$line;70317032my%set= parse_difftree_raw_line($line);7033if(defined$set{'commit'}) {7034# finish previous commit7035if(%co) {7036print"</td>\n".7037"<td class=\"link\">".7038$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .7039" | ".7040$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");7041print"</td>\n".7042"</tr>\n";7043}70447045if($alternate) {7046print"<tr class=\"dark\">\n";7047}else{7048print"<tr class=\"light\">\n";7049}7050$alternate^=1;7051%co= parse_commit($set{'commit'});7052my$author= chop_and_escape_str($co{'author_name'},15,5);7053print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".7054"<td><i>$author</i></td>\n".7055"<td>".7056$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),7057-class=>"list subject"},7058 chop_and_escape_str($co{'title'},50) ."<br/>");7059}elsif(defined$set{'to_id'}) {7060next if($set{'to_id'} =~m/^0{40}$/);70617062print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},7063 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),7064-class=>"list"},7065"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .7066"<br/>\n";7067}7068}7069close$fd;70707071# finish last commit (warning: repetition!)7072if(%co) {7073print"</td>\n".7074"<td class=\"link\">".7075$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .7076" | ".7077$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");7078print"</td>\n".7079"</tr>\n";7080}70817082print"</table>\n";7083}70847085if($searchtypeeq'grep') {7086 git_print_page_nav('','',$hash,$co{'tree'},$hash);7087 git_print_header_div('commit', esc_html($co{'title'}),$hash);70887089print"<table class=\"grep_search\">\n";7090my$alternate=1;7091my$matches=0;7092local$/="\n";7093open my$fd,"-|", git_cmd(),'grep','-n',7094$search_use_regexp? ('-E','-i') :'-F',7095$searchtext,$co{'tree'};7096my$lastfile='';7097while(my$line= <$fd>) {7098chomp$line;7099my($file,$lno,$ltext,$binary);7100last if($matches++>1000);7101if($line=~/^Binary file (.+) matches$/) {7102$file=$1;7103$binary=1;7104}else{7105(undef,$file,$lno,$ltext) =split(/:/,$line,4);7106}7107if($filene$lastfile) {7108$lastfileand print"</td></tr>\n";7109if($alternate++) {7110print"<tr class=\"dark\">\n";7111}else{7112print"<tr class=\"light\">\n";7113}7114print"<td class=\"list\">".7115$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},7116 file_name=>"$file"),7117-class=>"list"}, esc_path($file));7118print"</td><td>\n";7119$lastfile=$file;7120}7121if($binary) {7122print"<div class=\"binary\">Binary file</div>\n";7123}else{7124$ltext= untabify($ltext);7125if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {7126$ltext= esc_html($1, -nbsp=>1);7127$ltext.='<span class="match">';7128$ltext.= esc_html($2, -nbsp=>1);7129$ltext.='</span>';7130$ltext.= esc_html($3, -nbsp=>1);7131}else{7132$ltext= esc_html($ltext, -nbsp=>1);7133}7134print"<div class=\"pre\">".7135$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},7136 file_name=>"$file").'#l'.$lno,7137-class=>"linenr"},sprintf('%4i',$lno))7138.' '.$ltext."</div>\n";7139}7140}7141if($lastfile) {7142print"</td></tr>\n";7143if($matches>1000) {7144print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";7145}7146}else{7147print"<div class=\"diff nodifferences\">No matches found</div>\n";7148}7149close$fd;71507151print"</table>\n";7152}7153 git_footer_html();7154}71557156sub git_search_help {7157 git_header_html();7158 git_print_page_nav('','',$hash,$hash,$hash);7159print<<EOT;7160<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without7161regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,7162the pattern entered is recognized as the POSIX extended7163<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case7164insensitive).</p>7165<dl>7166<dt><b>commit</b></dt>7167<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>7168EOT7169my$have_grep= gitweb_check_feature('grep');7170if($have_grep) {7171print<<EOT;7172<dt><b>grep</b></dt>7173<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing7174 a different one) are searched for the given pattern. On large trees, this search can take7175a while and put some strain on the server, so please use it with some consideration. Note that7176due to git-grep peculiarity, currently if regexp mode is turned off, the matches are7177case-sensitive.</dd>7178EOT7179}7180print<<EOT;7181<dt><b>author</b></dt>7182<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>7183<dt><b>committer</b></dt>7184<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>7185EOT7186my$have_pickaxe= gitweb_check_feature('pickaxe');7187if($have_pickaxe) {7188print<<EOT;7189<dt><b>pickaxe</b></dt>7190<dd>All commits that caused the string to appear or disappear from any file (changes that7191added, removed or "modified" the string) will be listed. This search can take a while and7192takes a lot of strain on the server, so please use it wisely. Note that since you may be7193interested even in changes just changing the case as well, this search is case sensitive.</dd>7194EOT7195}7196print"</dl>\n";7197 git_footer_html();7198}71997200sub git_shortlog {7201 git_log_generic('shortlog', \&git_shortlog_body,7202$hash,$hash_parent);7203}72047205## ......................................................................7206## feeds (RSS, Atom; OPML)72077208sub git_feed {7209my$format=shift||'atom';7210my$have_blame= gitweb_check_feature('blame');72117212# Atom: http://www.atomenabled.org/developers/syndication/7213# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ7214if($formatne'rss'&&$formatne'atom') {7215 die_error(400,"Unknown web feed format");7216}72177218# log/feed of current (HEAD) branch, log of given branch, history of file/directory7219my$head=$hash||'HEAD';7220my@commitlist= parse_commits($head,150,0,$file_name);72217222my%latest_commit;7223my%latest_date;7224my$content_type="application/$format+xml";7225if(defined$cgi->http('HTTP_ACCEPT') &&7226$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {7227# browser (feed reader) prefers text/xml7228$content_type='text/xml';7229}7230if(defined($commitlist[0])) {7231%latest_commit= %{$commitlist[0]};7232my$latest_epoch=$latest_commit{'committer_epoch'};7233%latest_date= parse_date($latest_epoch,$latest_commit{'comitter_tz'});7234my$if_modified=$cgi->http('IF_MODIFIED_SINCE');7235if(defined$if_modified) {7236my$since;7237if(eval{require HTTP::Date;1; }) {7238$since= HTTP::Date::str2time($if_modified);7239}elsif(eval{require Time::ParseDate;1; }) {7240$since= Time::ParseDate::parsedate($if_modified, GMT =>1);7241}7242if(defined$since&&$latest_epoch<=$since) {7243print$cgi->header(7244-type =>$content_type,7245-charset =>'utf-8',7246-last_modified =>$latest_date{'rfc2822'},7247-status =>'304 Not Modified');7248return;7249}7250}7251print$cgi->header(7252-type =>$content_type,7253-charset =>'utf-8',7254-last_modified =>$latest_date{'rfc2822'});7255}else{7256print$cgi->header(7257-type =>$content_type,7258-charset =>'utf-8');7259}72607261# Optimization: skip generating the body if client asks only7262# for Last-Modified date.7263return if($cgi->request_method()eq'HEAD');72647265# header variables7266my$title="$site_name-$project/$action";7267my$feed_type='log';7268if(defined$hash) {7269$title.=" - '$hash'";7270$feed_type='branch log';7271if(defined$file_name) {7272$title.=" ::$file_name";7273$feed_type='history';7274}7275}elsif(defined$file_name) {7276$title.=" -$file_name";7277$feed_type='history';7278}7279$title.="$feed_type";7280my$descr= git_get_project_description($project);7281if(defined$descr) {7282$descr= esc_html($descr);7283}else{7284$descr="$project".7285($formateq'rss'?'RSS':'Atom') .7286" feed";7287}7288my$owner= git_get_project_owner($project);7289$owner= esc_html($owner);72907291#header7292my$alt_url;7293if(defined$file_name) {7294$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);7295}elsif(defined$hash) {7296$alt_url= href(-full=>1, action=>"log", hash=>$hash);7297}else{7298$alt_url= href(-full=>1, action=>"summary");7299}7300print qq!<?xml version="1.0" encoding="utf-8"?>\n!;7301if($formateq'rss') {7302print<<XML;7303<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">7304<channel>7305XML7306print"<title>$title</title>\n".7307"<link>$alt_url</link>\n".7308"<description>$descr</description>\n".7309"<language>en</language>\n".7310# project owner is responsible for 'editorial' content7311"<managingEditor>$owner</managingEditor>\n";7312if(defined$logo||defined$favicon) {7313# prefer the logo to the favicon, since RSS7314# doesn't allow both7315my$img= esc_url($logo||$favicon);7316print"<image>\n".7317"<url>$img</url>\n".7318"<title>$title</title>\n".7319"<link>$alt_url</link>\n".7320"</image>\n";7321}7322if(%latest_date) {7323print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";7324print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";7325}7326print"<generator>gitweb v.$version/$git_version</generator>\n";7327}elsif($formateq'atom') {7328print<<XML;7329<feed xmlns="http://www.w3.org/2005/Atom">7330XML7331print"<title>$title</title>\n".7332"<subtitle>$descr</subtitle>\n".7333'<link rel="alternate" type="text/html" href="'.7334$alt_url.'" />'."\n".7335'<link rel="self" type="'.$content_type.'" href="'.7336$cgi->self_url() .'" />'."\n".7337"<id>". href(-full=>1) ."</id>\n".7338# use project owner for feed author7339"<author><name>$owner</name></author>\n";7340if(defined$favicon) {7341print"<icon>". esc_url($favicon) ."</icon>\n";7342}7343if(defined$logo) {7344# not twice as wide as tall: 72 x 27 pixels7345print"<logo>". esc_url($logo) ."</logo>\n";7346}7347if(!%latest_date) {7348# dummy date to keep the feed valid until commits trickle in:7349print"<updated>1970-01-01T00:00:00Z</updated>\n";7350}else{7351print"<updated>$latest_date{'iso-8601'}</updated>\n";7352}7353print"<generator version='$version/$git_version'>gitweb</generator>\n";7354}73557356# contents7357for(my$i=0;$i<=$#commitlist;$i++) {7358my%co= %{$commitlist[$i]};7359my$commit=$co{'id'};7360# we read 150, we always show 30 and the ones more recent than 48 hours7361if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {7362last;7363}7364my%cd= parse_date($co{'author_epoch'},$co{'author_tz'});73657366# get list of changed files7367open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7368$co{'parent'} ||"--root",7369$co{'id'},"--", (defined$file_name?$file_name: ())7370ornext;7371my@difftree=map{chomp;$_} <$fd>;7372close$fd7373ornext;73747375# print element (entry, item)7376my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);7377if($formateq'rss') {7378print"<item>\n".7379"<title>". esc_html($co{'title'}) ."</title>\n".7380"<author>". esc_html($co{'author'}) ."</author>\n".7381"<pubDate>$cd{'rfc2822'}</pubDate>\n".7382"<guid isPermaLink=\"true\">$co_url</guid>\n".7383"<link>$co_url</link>\n".7384"<description>". esc_html($co{'title'}) ."</description>\n".7385"<content:encoded>".7386"<![CDATA[\n";7387}elsif($formateq'atom') {7388print"<entry>\n".7389"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".7390"<updated>$cd{'iso-8601'}</updated>\n".7391"<author>\n".7392" <name>". esc_html($co{'author_name'}) ."</name>\n";7393if($co{'author_email'}) {7394print" <email>". esc_html($co{'author_email'}) ."</email>\n";7395}7396print"</author>\n".7397# use committer for contributor7398"<contributor>\n".7399" <name>". esc_html($co{'committer_name'}) ."</name>\n";7400if($co{'committer_email'}) {7401print" <email>". esc_html($co{'committer_email'}) ."</email>\n";7402}7403print"</contributor>\n".7404"<published>$cd{'iso-8601'}</published>\n".7405"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".7406"<id>$co_url</id>\n".7407"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".7408"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";7409}7410my$comment=$co{'comment'};7411print"<pre>\n";7412foreachmy$line(@$comment) {7413$line= esc_html($line);7414print"$line\n";7415}7416print"</pre><ul>\n";7417foreachmy$difftree_line(@difftree) {7418my%difftree= parse_difftree_raw_line($difftree_line);7419next if!$difftree{'from_id'};74207421my$file=$difftree{'file'} ||$difftree{'to_file'};74227423print"<li>".7424"[".7425$cgi->a({-href => href(-full=>1, action=>"blobdiff",7426 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},7427 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},7428 file_name=>$file, file_parent=>$difftree{'from_file'}),7429-title =>"diff"},'D');7430if($have_blame) {7431print$cgi->a({-href => href(-full=>1, action=>"blame",7432 file_name=>$file, hash_base=>$commit),7433-title =>"blame"},'B');7434}7435# if this is not a feed of a file history7436if(!defined$file_name||$file_namene$file) {7437print$cgi->a({-href => href(-full=>1, action=>"history",7438 file_name=>$file, hash=>$commit),7439-title =>"history"},'H');7440}7441$file= esc_path($file);7442print"] ".7443"$file</li>\n";7444}7445if($formateq'rss') {7446print"</ul>]]>\n".7447"</content:encoded>\n".7448"</item>\n";7449}elsif($formateq'atom') {7450print"</ul>\n</div>\n".7451"</content>\n".7452"</entry>\n";7453}7454}74557456# end of feed7457if($formateq'rss') {7458print"</channel>\n</rss>\n";7459}elsif($formateq'atom') {7460print"</feed>\n";7461}7462}74637464sub git_rss {7465 git_feed('rss');7466}74677468sub git_atom {7469 git_feed('atom');7470}74717472sub git_opml {7473my@list= git_get_projects_list();7474if(!@list) {7475 die_error(404,"No projects found");7476}74777478print$cgi->header(7479-type =>'text/xml',7480-charset =>'utf-8',7481-content_disposition =>'inline; filename="opml.xml"');74827483print<<XML;7484<?xml version="1.0" encoding="utf-8"?>7485<opml version="1.0">7486<head>7487 <title>$site_nameOPML Export</title>7488</head>7489<body>7490<outline text="git RSS feeds">7491XML74927493foreachmy$pr(@list) {7494my%proj=%$pr;7495my$head= git_get_head_hash($proj{'path'});7496if(!defined$head) {7497next;7498}7499$git_dir="$projectroot/$proj{'path'}";7500my%co= parse_commit($head);7501if(!%co) {7502next;7503}75047505my$path= esc_html(chop_str($proj{'path'},25,5));7506my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);7507my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);7508print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";7509}7510print<<XML;7511</outline>7512</body>7513</opml>7514XML7515}