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']}, 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), 254# alternate extensions, see /etc/highlight/filetypes.conf 255'h'=>'c', 256map{$_=>'cpp'}qw(cxx c++ cc), 257map{$_=>'php'}qw(php3 php4), 258map{$_=>'pl'}qw(perl pm),# perhaps also 'cgi' 259'mak'=>'make', 260map{$_=>'xml'}qw(xhtml html htm), 261); 262 263# You define site-wide feature defaults here; override them with 264# $GITWEB_CONFIG as necessary. 265our%feature= ( 266# feature => { 267# 'sub' => feature-sub (subroutine), 268# 'override' => allow-override (boolean), 269# 'default' => [ default options...] (array reference)} 270# 271# if feature is overridable (it means that allow-override has true value), 272# then feature-sub will be called with default options as parameters; 273# return value of feature-sub indicates if to enable specified feature 274# 275# if there is no 'sub' key (no feature-sub), then feature cannot be 276# overridden 277# 278# use gitweb_get_feature(<feature>) to retrieve the <feature> value 279# (an array) or gitweb_check_feature(<feature>) to check if <feature> 280# is enabled 281 282# Enable the 'blame' blob view, showing the last commit that modified 283# each line in the file. This can be very CPU-intensive. 284 285# To enable system wide have in $GITWEB_CONFIG 286# $feature{'blame'}{'default'} = [1]; 287# To have project specific config enable override in $GITWEB_CONFIG 288# $feature{'blame'}{'override'} = 1; 289# and in project config gitweb.blame = 0|1; 290'blame'=> { 291'sub'=>sub{ feature_bool('blame',@_) }, 292'override'=>0, 293'default'=> [0]}, 294 295# Enable the 'snapshot' link, providing a compressed archive of any 296# tree. This can potentially generate high traffic if you have large 297# project. 298 299# Value is a list of formats defined in %known_snapshot_formats that 300# you wish to offer. 301# To disable system wide have in $GITWEB_CONFIG 302# $feature{'snapshot'}{'default'} = []; 303# To have project specific config enable override in $GITWEB_CONFIG 304# $feature{'snapshot'}{'override'} = 1; 305# and in project config, a comma-separated list of formats or "none" 306# to disable. Example: gitweb.snapshot = tbz2,zip; 307'snapshot'=> { 308'sub'=> \&feature_snapshot, 309'override'=>0, 310'default'=> ['tgz']}, 311 312# Enable text search, which will list the commits which match author, 313# committer or commit text to a given string. Enabled by default. 314# Project specific override is not supported. 315'search'=> { 316'override'=>0, 317'default'=> [1]}, 318 319# Enable grep search, which will list the files in currently selected 320# tree containing the given string. Enabled by default. This can be 321# potentially CPU-intensive, of course. 322 323# To enable system wide have in $GITWEB_CONFIG 324# $feature{'grep'}{'default'} = [1]; 325# To have project specific config enable override in $GITWEB_CONFIG 326# $feature{'grep'}{'override'} = 1; 327# and in project config gitweb.grep = 0|1; 328'grep'=> { 329'sub'=>sub{ feature_bool('grep',@_) }, 330'override'=>0, 331'default'=> [1]}, 332 333# Enable the pickaxe search, which will list the commits that modified 334# a given string in a file. This can be practical and quite faster 335# alternative to 'blame', but still potentially CPU-intensive. 336 337# To enable system wide have in $GITWEB_CONFIG 338# $feature{'pickaxe'}{'default'} = [1]; 339# To have project specific config enable override in $GITWEB_CONFIG 340# $feature{'pickaxe'}{'override'} = 1; 341# and in project config gitweb.pickaxe = 0|1; 342'pickaxe'=> { 343'sub'=>sub{ feature_bool('pickaxe',@_) }, 344'override'=>0, 345'default'=> [1]}, 346 347# Enable showing size of blobs in a 'tree' view, in a separate 348# column, similar to what 'ls -l' does. This cost a bit of IO. 349 350# To disable system wide have in $GITWEB_CONFIG 351# $feature{'show-sizes'}{'default'} = [0]; 352# To have project specific config enable override in $GITWEB_CONFIG 353# $feature{'show-sizes'}{'override'} = 1; 354# and in project config gitweb.showsizes = 0|1; 355'show-sizes'=> { 356'sub'=>sub{ feature_bool('showsizes',@_) }, 357'override'=>0, 358'default'=> [1]}, 359 360# Make gitweb use an alternative format of the URLs which can be 361# more readable and natural-looking: project name is embedded 362# directly in the path and the query string contains other 363# auxiliary information. All gitweb installations recognize 364# URL in either format; this configures in which formats gitweb 365# generates links. 366 367# To enable system wide have in $GITWEB_CONFIG 368# $feature{'pathinfo'}{'default'} = [1]; 369# Project specific override is not supported. 370 371# Note that you will need to change the default location of CSS, 372# favicon, logo and possibly other files to an absolute URL. Also, 373# if gitweb.cgi serves as your indexfile, you will need to force 374# $my_uri to contain the script name in your $GITWEB_CONFIG. 375'pathinfo'=> { 376'override'=>0, 377'default'=> [0]}, 378 379# Make gitweb consider projects in project root subdirectories 380# to be forks of existing projects. Given project $projname.git, 381# projects matching $projname/*.git will not be shown in the main 382# projects list, instead a '+' mark will be added to $projname 383# there and a 'forks' view will be enabled for the project, listing 384# all the forks. If project list is taken from a file, forks have 385# to be listed after the main project. 386 387# To enable system wide have in $GITWEB_CONFIG 388# $feature{'forks'}{'default'} = [1]; 389# Project specific override is not supported. 390'forks'=> { 391'override'=>0, 392'default'=> [0]}, 393 394# Insert custom links to the action bar of all project pages. 395# This enables you mainly to link to third-party scripts integrating 396# into gitweb; e.g. git-browser for graphical history representation 397# or custom web-based repository administration interface. 398 399# The 'default' value consists of a list of triplets in the form 400# (label, link, position) where position is the label after which 401# to insert the link and link is a format string where %n expands 402# to the project name, %f to the project path within the filesystem, 403# %h to the current hash (h gitweb parameter) and %b to the current 404# hash base (hb gitweb parameter); %% expands to %. 405 406# To enable system wide have in $GITWEB_CONFIG e.g. 407# $feature{'actions'}{'default'} = [('graphiclog', 408# '/git-browser/by-commit.html?r=%n', 'summary')]; 409# Project specific override is not supported. 410'actions'=> { 411'override'=>0, 412'default'=> []}, 413 414# Allow gitweb scan project content tags described in ctags/ 415# of project repository, and display the popular Web 2.0-ish 416# "tag cloud" near the project list. Note that this is something 417# COMPLETELY different from the normal Git tags. 418 419# gitweb by itself can show existing tags, but it does not handle 420# tagging itself; you need an external application for that. 421# For an example script, check Girocco's cgi/tagproj.cgi. 422# You may want to install the HTML::TagCloud Perl module to get 423# a pretty tag cloud instead of just a list of tags. 424 425# To enable system wide have in $GITWEB_CONFIG 426# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 427# Project specific override is not supported. 428'ctags'=> { 429'override'=>0, 430'default'=> [0]}, 431 432# The maximum number of patches in a patchset generated in patch 433# view. Set this to 0 or undef to disable patch view, or to a 434# negative number to remove any limit. 435 436# To disable system wide have in $GITWEB_CONFIG 437# $feature{'patches'}{'default'} = [0]; 438# To have project specific config enable override in $GITWEB_CONFIG 439# $feature{'patches'}{'override'} = 1; 440# and in project config gitweb.patches = 0|n; 441# where n is the maximum number of patches allowed in a patchset. 442'patches'=> { 443'sub'=> \&feature_patches, 444'override'=>0, 445'default'=> [16]}, 446 447# Avatar support. When this feature is enabled, views such as 448# shortlog or commit will display an avatar associated with 449# the email of the committer(s) and/or author(s). 450 451# Currently available providers are gravatar and picon. 452# If an unknown provider is specified, the feature is disabled. 453 454# Gravatar depends on Digest::MD5. 455# Picon currently relies on the indiana.edu database. 456 457# To enable system wide have in $GITWEB_CONFIG 458# $feature{'avatar'}{'default'} = ['<provider>']; 459# where <provider> is either gravatar or picon. 460# To have project specific config enable override in $GITWEB_CONFIG 461# $feature{'avatar'}{'override'} = 1; 462# and in project config gitweb.avatar = <provider>; 463'avatar'=> { 464'sub'=> \&feature_avatar, 465'override'=>0, 466'default'=> ['']}, 467 468# Enable displaying how much time and how many git commands 469# it took to generate and display page. Disabled by default. 470# Project specific override is not supported. 471'timed'=> { 472'override'=>0, 473'default'=> [0]}, 474 475# Enable turning some links into links to actions which require 476# JavaScript to run (like 'blame_incremental'). Not enabled by 477# default. Project specific override is currently not supported. 478'javascript-actions'=> { 479'override'=>0, 480'default'=> [0]}, 481 482# Syntax highlighting support. This is based on Daniel Svensson's 483# and Sham Chukoury's work in gitweb-xmms2.git. 484# It requires the 'highlight' program present in $PATH, 485# and therefore is disabled by default. 486 487# To enable system wide have in $GITWEB_CONFIG 488# $feature{'highlight'}{'default'} = [1]; 489 490'highlight'=> { 491'sub'=>sub{ feature_bool('highlight',@_) }, 492'override'=>0, 493'default'=> [0]}, 494); 495 496sub gitweb_get_feature { 497my($name) =@_; 498return unlessexists$feature{$name}; 499my($sub,$override,@defaults) = ( 500$feature{$name}{'sub'}, 501$feature{$name}{'override'}, 502@{$feature{$name}{'default'}}); 503# project specific override is possible only if we have project 504our$git_dir;# global variable, declared later 505if(!$override|| !defined$git_dir) { 506return@defaults; 507} 508if(!defined$sub) { 509warn"feature$nameis not overridable"; 510return@defaults; 511} 512return$sub->(@defaults); 513} 514 515# A wrapper to check if a given feature is enabled. 516# With this, you can say 517# 518# my $bool_feat = gitweb_check_feature('bool_feat'); 519# gitweb_check_feature('bool_feat') or somecode; 520# 521# instead of 522# 523# my ($bool_feat) = gitweb_get_feature('bool_feat'); 524# (gitweb_get_feature('bool_feat'))[0] or somecode; 525# 526sub gitweb_check_feature { 527return(gitweb_get_feature(@_))[0]; 528} 529 530 531sub feature_bool { 532my$key=shift; 533my($val) = git_get_project_config($key,'--bool'); 534 535if(!defined$val) { 536return($_[0]); 537}elsif($valeq'true') { 538return(1); 539}elsif($valeq'false') { 540return(0); 541} 542} 543 544sub feature_snapshot { 545my(@fmts) =@_; 546 547my($val) = git_get_project_config('snapshot'); 548 549if($val) { 550@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 551} 552 553return@fmts; 554} 555 556sub feature_patches { 557my@val= (git_get_project_config('patches','--int')); 558 559if(@val) { 560return@val; 561} 562 563return($_[0]); 564} 565 566sub feature_avatar { 567my@val= (git_get_project_config('avatar')); 568 569return@val?@val:@_; 570} 571 572# checking HEAD file with -e is fragile if the repository was 573# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 574# and then pruned. 575sub check_head_link { 576my($dir) =@_; 577my$headfile="$dir/HEAD"; 578return((-e $headfile) || 579(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 580} 581 582sub check_export_ok { 583my($dir) =@_; 584return(check_head_link($dir) && 585(!$export_ok|| -e "$dir/$export_ok") && 586(!$export_auth_hook||$export_auth_hook->($dir))); 587} 588 589# process alternate names for backward compatibility 590# filter out unsupported (unknown) snapshot formats 591sub filter_snapshot_fmts { 592my@fmts=@_; 593 594@fmts=map{ 595exists$known_snapshot_format_aliases{$_} ? 596$known_snapshot_format_aliases{$_} :$_}@fmts; 597@fmts=grep{ 598exists$known_snapshot_formats{$_} && 599!$known_snapshot_formats{$_}{'disabled'}}@fmts; 600} 601 602our($GITWEB_CONFIG,$GITWEB_CONFIG_SYSTEM); 603sub evaluate_gitweb_config { 604our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 605our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 606# die if there are errors parsing config file 607if(-e $GITWEB_CONFIG) { 608do$GITWEB_CONFIG; 609die$@if$@; 610}elsif(-e $GITWEB_CONFIG_SYSTEM) { 611do$GITWEB_CONFIG_SYSTEM; 612die$@if$@; 613} 614} 615 616# Get loadavg of system, to compare against $maxload. 617# Currently it requires '/proc/loadavg' present to get loadavg; 618# if it is not present it returns 0, which means no load checking. 619sub get_loadavg { 620if( -e '/proc/loadavg'){ 621open my$fd,'<','/proc/loadavg' 622orreturn0; 623my@load=split(/\s+/,scalar<$fd>); 624close$fd; 625 626# The first three columns measure CPU and IO utilization of the last one, 627# five, and 10 minute periods. The fourth column shows the number of 628# currently running processes and the total number of processes in the m/n 629# format. The last column displays the last process ID used. 630return$load[0] ||0; 631} 632# additional checks for load average should go here for things that don't export 633# /proc/loadavg 634 635return0; 636} 637 638# version of the core git binary 639our$git_version; 640sub evaluate_git_version { 641our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 642$number_of_git_cmds++; 643} 644 645sub check_loadavg { 646if(defined$maxload&& get_loadavg() >$maxload) { 647 die_error(503,"The load average on the server is too high"); 648} 649} 650 651# ====================================================================== 652# input validation and dispatch 653 654# input parameters can be collected from a variety of sources (presently, CGI 655# and PATH_INFO), so we define an %input_params hash that collects them all 656# together during validation: this allows subsequent uses (e.g. href()) to be 657# agnostic of the parameter origin 658 659our%input_params= (); 660 661# input parameters are stored with the long parameter name as key. This will 662# also be used in the href subroutine to convert parameters to their CGI 663# equivalent, and since the href() usage is the most frequent one, we store 664# the name -> CGI key mapping here, instead of the reverse. 665# 666# XXX: Warning: If you touch this, check the search form for updating, 667# too. 668 669our@cgi_param_mapping= ( 670 project =>"p", 671 action =>"a", 672 file_name =>"f", 673 file_parent =>"fp", 674 hash =>"h", 675 hash_parent =>"hp", 676 hash_base =>"hb", 677 hash_parent_base =>"hpb", 678 page =>"pg", 679 order =>"o", 680 searchtext =>"s", 681 searchtype =>"st", 682 snapshot_format =>"sf", 683 extra_options =>"opt", 684 search_use_regexp =>"sr", 685# this must be last entry (for manipulation from JavaScript) 686 javascript =>"js" 687); 688our%cgi_param_mapping=@cgi_param_mapping; 689 690# we will also need to know the possible actions, for validation 691our%actions= ( 692"blame"=> \&git_blame, 693"blame_incremental"=> \&git_blame_incremental, 694"blame_data"=> \&git_blame_data, 695"blobdiff"=> \&git_blobdiff, 696"blobdiff_plain"=> \&git_blobdiff_plain, 697"blob"=> \&git_blob, 698"blob_plain"=> \&git_blob_plain, 699"commitdiff"=> \&git_commitdiff, 700"commitdiff_plain"=> \&git_commitdiff_plain, 701"commit"=> \&git_commit, 702"forks"=> \&git_forks, 703"heads"=> \&git_heads, 704"history"=> \&git_history, 705"log"=> \&git_log, 706"patch"=> \&git_patch, 707"patches"=> \&git_patches, 708"rss"=> \&git_rss, 709"atom"=> \&git_atom, 710"search"=> \&git_search, 711"search_help"=> \&git_search_help, 712"shortlog"=> \&git_shortlog, 713"summary"=> \&git_summary, 714"tag"=> \&git_tag, 715"tags"=> \&git_tags, 716"tree"=> \&git_tree, 717"snapshot"=> \&git_snapshot, 718"object"=> \&git_object, 719# those below don't need $project 720"opml"=> \&git_opml, 721"project_list"=> \&git_project_list, 722"project_index"=> \&git_project_index, 723); 724 725# finally, we have the hash of allowed extra_options for the commands that 726# allow them 727our%allowed_options= ( 728"--no-merges"=> [qw(rss atom log shortlog history)], 729); 730 731# fill %input_params with the CGI parameters. All values except for 'opt' 732# should be single values, but opt can be an array. We should probably 733# build an array of parameters that can be multi-valued, but since for the time 734# being it's only this one, we just single it out 735sub evaluate_query_params { 736our$cgi; 737 738while(my($name,$symbol) =each%cgi_param_mapping) { 739if($symboleq'opt') { 740$input_params{$name} = [$cgi->param($symbol) ]; 741}else{ 742$input_params{$name} =$cgi->param($symbol); 743} 744} 745} 746 747# now read PATH_INFO and update the parameter list for missing parameters 748sub evaluate_path_info { 749return ifdefined$input_params{'project'}; 750return if!$path_info; 751$path_info=~ s,^/+,,; 752return if!$path_info; 753 754# find which part of PATH_INFO is project 755my$project=$path_info; 756$project=~ s,/+$,,; 757while($project&& !check_head_link("$projectroot/$project")) { 758$project=~ s,/*[^/]*$,,; 759} 760return unless$project; 761$input_params{'project'} =$project; 762 763# do not change any parameters if an action is given using the query string 764return if$input_params{'action'}; 765$path_info=~ s,^\Q$project\E/*,,; 766 767# next, check if we have an action 768my$action=$path_info; 769$action=~ s,/.*$,,; 770if(exists$actions{$action}) { 771$path_info=~ s,^$action/*,,; 772$input_params{'action'} =$action; 773} 774 775# list of actions that want hash_base instead of hash, but can have no 776# pathname (f) parameter 777my@wants_base= ( 778'tree', 779'history', 780); 781 782# we want to catch, among others 783# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 784my($parentrefname,$parentpathname,$refname,$pathname) = 785($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/); 786 787# first, analyze the 'current' part 788if(defined$pathname) { 789# we got "branch:filename" or "branch:dir/" 790# we could use git_get_type(branch:pathname), but: 791# - it needs $git_dir 792# - it does a git() call 793# - the convention of terminating directories with a slash 794# makes it superfluous 795# - embedding the action in the PATH_INFO would make it even 796# more superfluous 797$pathname=~ s,^/+,,; 798if(!$pathname||substr($pathname, -1)eq"/") { 799$input_params{'action'} ||="tree"; 800$pathname=~ s,/$,,; 801}else{ 802# the default action depends on whether we had parent info 803# or not 804if($parentrefname) { 805$input_params{'action'} ||="blobdiff_plain"; 806}else{ 807$input_params{'action'} ||="blob_plain"; 808} 809} 810$input_params{'hash_base'} ||=$refname; 811$input_params{'file_name'} ||=$pathname; 812}elsif(defined$refname) { 813# we got "branch". In this case we have to choose if we have to 814# set hash or hash_base. 815# 816# Most of the actions without a pathname only want hash to be 817# set, except for the ones specified in @wants_base that want 818# hash_base instead. It should also be noted that hand-crafted 819# links having 'history' as an action and no pathname or hash 820# set will fail, but that happens regardless of PATH_INFO. 821if(defined$parentrefname) { 822# if there is parent let the default be 'shortlog' action 823# (for http://git.example.com/repo.git/A..B links); if there 824# is no parent, dispatch will detect type of object and set 825# action appropriately if required (if action is not set) 826$input_params{'action'} ||="shortlog"; 827} 828if($input_params{'action'} && 829grep{$_eq$input_params{'action'} }@wants_base) { 830$input_params{'hash_base'} ||=$refname; 831}else{ 832$input_params{'hash'} ||=$refname; 833} 834} 835 836# next, handle the 'parent' part, if present 837if(defined$parentrefname) { 838# a missing pathspec defaults to the 'current' filename, allowing e.g. 839# someproject/blobdiff/oldrev..newrev:/filename 840if($parentpathname) { 841$parentpathname=~ s,^/+,,; 842$parentpathname=~ s,/$,,; 843$input_params{'file_parent'} ||=$parentpathname; 844}else{ 845$input_params{'file_parent'} ||=$input_params{'file_name'}; 846} 847# we assume that hash_parent_base is wanted if a path was specified, 848# or if the action wants hash_base instead of hash 849if(defined$input_params{'file_parent'} || 850grep{$_eq$input_params{'action'} }@wants_base) { 851$input_params{'hash_parent_base'} ||=$parentrefname; 852}else{ 853$input_params{'hash_parent'} ||=$parentrefname; 854} 855} 856 857# for the snapshot action, we allow URLs in the form 858# $project/snapshot/$hash.ext 859# where .ext determines the snapshot and gets removed from the 860# passed $refname to provide the $hash. 861# 862# To be able to tell that $refname includes the format extension, we 863# require the following two conditions to be satisfied: 864# - the hash input parameter MUST have been set from the $refname part 865# of the URL (i.e. they must be equal) 866# - the snapshot format MUST NOT have been defined already (e.g. from 867# CGI parameter sf) 868# It's also useless to try any matching unless $refname has a dot, 869# so we check for that too 870if(defined$input_params{'action'} && 871$input_params{'action'}eq'snapshot'&& 872defined$refname&&index($refname,'.') != -1&& 873$refnameeq$input_params{'hash'} && 874!defined$input_params{'snapshot_format'}) { 875# We loop over the known snapshot formats, checking for 876# extensions. Allowed extensions are both the defined suffix 877# (which includes the initial dot already) and the snapshot 878# format key itself, with a prepended dot 879while(my($fmt,$opt) =each%known_snapshot_formats) { 880my$hash=$refname; 881unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 882next; 883} 884my$sfx=$1; 885# a valid suffix was found, so set the snapshot format 886# and reset the hash parameter 887$input_params{'snapshot_format'} =$fmt; 888$input_params{'hash'} =$hash; 889# we also set the format suffix to the one requested 890# in the URL: this way a request for e.g. .tgz returns 891# a .tgz instead of a .tar.gz 892$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 893last; 894} 895} 896} 897 898our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 899$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 900$searchtext,$search_regexp); 901sub evaluate_and_validate_params { 902our$action=$input_params{'action'}; 903if(defined$action) { 904if(!validate_action($action)) { 905 die_error(400,"Invalid action parameter"); 906} 907} 908 909# parameters which are pathnames 910our$project=$input_params{'project'}; 911if(defined$project) { 912if(!validate_project($project)) { 913undef$project; 914 die_error(404,"No such project"); 915} 916} 917 918our$file_name=$input_params{'file_name'}; 919if(defined$file_name) { 920if(!validate_pathname($file_name)) { 921 die_error(400,"Invalid file parameter"); 922} 923} 924 925our$file_parent=$input_params{'file_parent'}; 926if(defined$file_parent) { 927if(!validate_pathname($file_parent)) { 928 die_error(400,"Invalid file parent parameter"); 929} 930} 931 932# parameters which are refnames 933our$hash=$input_params{'hash'}; 934if(defined$hash) { 935if(!validate_refname($hash)) { 936 die_error(400,"Invalid hash parameter"); 937} 938} 939 940our$hash_parent=$input_params{'hash_parent'}; 941if(defined$hash_parent) { 942if(!validate_refname($hash_parent)) { 943 die_error(400,"Invalid hash parent parameter"); 944} 945} 946 947our$hash_base=$input_params{'hash_base'}; 948if(defined$hash_base) { 949if(!validate_refname($hash_base)) { 950 die_error(400,"Invalid hash base parameter"); 951} 952} 953 954our@extra_options= @{$input_params{'extra_options'}}; 955# @extra_options is always defined, since it can only be (currently) set from 956# CGI, and $cgi->param() returns the empty array in array context if the param 957# is not set 958foreachmy$opt(@extra_options) { 959if(not exists$allowed_options{$opt}) { 960 die_error(400,"Invalid option parameter"); 961} 962if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 963 die_error(400,"Invalid option parameter for this action"); 964} 965} 966 967our$hash_parent_base=$input_params{'hash_parent_base'}; 968if(defined$hash_parent_base) { 969if(!validate_refname($hash_parent_base)) { 970 die_error(400,"Invalid hash parent base parameter"); 971} 972} 973 974# other parameters 975our$page=$input_params{'page'}; 976if(defined$page) { 977if($page=~m/[^0-9]/) { 978 die_error(400,"Invalid page parameter"); 979} 980} 981 982our$searchtype=$input_params{'searchtype'}; 983if(defined$searchtype) { 984if($searchtype=~m/[^a-z]/) { 985 die_error(400,"Invalid searchtype parameter"); 986} 987} 988 989our$search_use_regexp=$input_params{'search_use_regexp'}; 990 991our$searchtext=$input_params{'searchtext'}; 992our$search_regexp; 993if(defined$searchtext) { 994if(length($searchtext) <2) { 995 die_error(403,"At least two characters are required for search parameter"); 996} 997$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 998} 999}10001001# path to the current git repository1002our$git_dir;1003sub evaluate_git_dir {1004our$git_dir="$projectroot/$project"if$project;1005}10061007our(@snapshot_fmts,$git_avatar);1008sub configure_gitweb_features {1009# list of supported snapshot formats1010our@snapshot_fmts= gitweb_get_feature('snapshot');1011@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);10121013# check that the avatar feature is set to a known provider name,1014# and for each provider check if the dependencies are satisfied.1015# if the provider name is invalid or the dependencies are not met,1016# reset $git_avatar to the empty string.1017our($git_avatar) = gitweb_get_feature('avatar');1018if($git_avatareq'gravatar') {1019$git_avatar=''unless(eval{require Digest::MD5;1; });1020}elsif($git_avatareq'picon') {1021# no dependencies1022}else{1023$git_avatar='';1024}1025}10261027# custom error handler: 'die <message>' is Internal Server Error1028sub handle_errors_html {1029my$msg=shift;# it is already HTML escaped10301031# to avoid infinite loop where error occurs in die_error,1032# change handler to default handler, disabling handle_errors_html1033 set_message("Error occured when inside die_error:\n$msg");10341035# you cannot jump out of die_error when called as error handler;1036# the subroutine set via CGI::Carp::set_message is called _after_1037# HTTP headers are already written, so it cannot write them itself1038 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1);1039}1040set_message(\&handle_errors_html);10411042# dispatch1043sub dispatch {1044if(!defined$action) {1045if(defined$hash) {1046$action= git_get_type($hash);1047}elsif(defined$hash_base&&defined$file_name) {1048$action= git_get_type("$hash_base:$file_name");1049}elsif(defined$project) {1050$action='summary';1051}else{1052$action='project_list';1053}1054}1055if(!defined($actions{$action})) {1056 die_error(400,"Unknown action");1057}1058if($action!~m/^(?:opml|project_list|project_index)$/&&1059!$project) {1060 die_error(400,"Project needed");1061}1062$actions{$action}->();1063}10641065sub reset_timer {1066our$t0= [ gettimeofday() ]1067ifdefined$t0;1068our$number_of_git_cmds=0;1069}10701071sub run_request {1072 reset_timer();10731074 evaluate_uri();1075 evaluate_gitweb_config();1076 check_loadavg();10771078# $projectroot and $projects_list might be set in gitweb config file1079$projects_list||=$projectroot;10801081 evaluate_query_params();1082 evaluate_path_info();1083 evaluate_and_validate_params();1084 evaluate_git_dir();10851086 configure_gitweb_features();10871088 dispatch();1089}10901091our$is_last_request=sub{1};1092our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1093our$CGI='CGI';1094our$cgi;1095sub configure_as_fcgi {1096require CGI::Fast;1097our$CGI='CGI::Fast';10981099my$request_number=0;1100# let each child service 100 requests1101our$is_last_request=sub{ ++$request_number>100};1102}1103sub evaluate_argv {1104my$script_name=$ENV{'SCRIPT_NAME'} ||$ENV{'SCRIPT_FILENAME'} || __FILE__;1105 configure_as_fcgi()1106if$script_name=~/\.fcgi$/;11071108return unless(@ARGV);11091110require Getopt::Long;1111 Getopt::Long::GetOptions(1112'fastcgi|fcgi|f'=> \&configure_as_fcgi,1113'nproc|n=i'=>sub{1114my($arg,$val) =@_;1115return unlesseval{require FCGI::ProcManager;1; };1116my$proc_manager= FCGI::ProcManager->new({1117 n_processes =>$val,1118});1119our$pre_listen_hook=sub{$proc_manager->pm_manage() };1120our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1121our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1122},1123);1124}11251126sub run {1127 evaluate_argv();1128 evaluate_git_version();11291130$pre_listen_hook->()1131if$pre_listen_hook;11321133 REQUEST:1134while($cgi=$CGI->new()) {1135$pre_dispatch_hook->()1136if$pre_dispatch_hook;11371138 run_request();11391140$post_dispatch_hook->()1141if$post_dispatch_hook;11421143last REQUEST if($is_last_request->());1144}11451146 DONE_GITWEB:11471;1148}11491150run();11511152if(defined caller) {1153# wrapped in a subroutine processing requests,1154# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1155return;1156}else{1157# pure CGI script, serving single request1158exit;1159}11601161## ======================================================================1162## action links11631164# possible values of extra options1165# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1166# -replay => 1 - start from a current view (replay with modifications)1167# -path_info => 0|1 - don't use/use path_info URL (if possible)1168sub href {1169my%params=@_;1170# default is to use -absolute url() i.e. $my_uri1171my$href=$params{-full} ?$my_url:$my_uri;11721173$params{'project'} =$projectunlessexists$params{'project'};11741175if($params{-replay}) {1176while(my($name,$symbol) =each%cgi_param_mapping) {1177if(!exists$params{$name}) {1178$params{$name} =$input_params{$name};1179}1180}1181}11821183my$use_pathinfo= gitweb_check_feature('pathinfo');1184if(defined$params{'project'} &&1185(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1186# try to put as many parameters as possible in PATH_INFO:1187# - project name1188# - action1189# - hash_parent or hash_parent_base:/file_parent1190# - hash or hash_base:/filename1191# - the snapshot_format as an appropriate suffix11921193# When the script is the root DirectoryIndex for the domain,1194# $href here would be something like http://gitweb.example.com/1195# Thus, we strip any trailing / from $href, to spare us double1196# slashes in the final URL1197$href=~ s,/$,,;11981199# Then add the project name, if present1200$href.="/".esc_url($params{'project'});1201delete$params{'project'};12021203# since we destructively absorb parameters, we keep this1204# boolean that remembers if we're handling a snapshot1205my$is_snapshot=$params{'action'}eq'snapshot';12061207# Summary just uses the project path URL, any other action is1208# added to the URL1209if(defined$params{'action'}) {1210$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1211delete$params{'action'};1212}12131214# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1215# stripping nonexistent or useless pieces1216$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1217||$params{'hash_parent'} ||$params{'hash'});1218if(defined$params{'hash_base'}) {1219if(defined$params{'hash_parent_base'}) {1220$href.= esc_url($params{'hash_parent_base'});1221# skip the file_parent if it's the same as the file_name1222if(defined$params{'file_parent'}) {1223if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1224delete$params{'file_parent'};1225}elsif($params{'file_parent'} !~/\.\./) {1226$href.=":/".esc_url($params{'file_parent'});1227delete$params{'file_parent'};1228}1229}1230$href.="..";1231delete$params{'hash_parent'};1232delete$params{'hash_parent_base'};1233}elsif(defined$params{'hash_parent'}) {1234$href.= esc_url($params{'hash_parent'})."..";1235delete$params{'hash_parent'};1236}12371238$href.= esc_url($params{'hash_base'});1239if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1240$href.=":/".esc_url($params{'file_name'});1241delete$params{'file_name'};1242}1243delete$params{'hash'};1244delete$params{'hash_base'};1245}elsif(defined$params{'hash'}) {1246$href.= esc_url($params{'hash'});1247delete$params{'hash'};1248}12491250# If the action was a snapshot, we can absorb the1251# snapshot_format parameter too1252if($is_snapshot) {1253my$fmt=$params{'snapshot_format'};1254# snapshot_format should always be defined when href()1255# is called, but just in case some code forgets, we1256# fall back to the default1257$fmt||=$snapshot_fmts[0];1258$href.=$known_snapshot_formats{$fmt}{'suffix'};1259delete$params{'snapshot_format'};1260}1261}12621263# now encode the parameters explicitly1264my@result= ();1265for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1266my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1267if(defined$params{$name}) {1268if(ref($params{$name})eq"ARRAY") {1269foreachmy$par(@{$params{$name}}) {1270push@result,$symbol."=". esc_param($par);1271}1272}else{1273push@result,$symbol."=". esc_param($params{$name});1274}1275}1276}1277$href.="?".join(';',@result)ifscalar@result;12781279return$href;1280}128112821283## ======================================================================1284## validation, quoting/unquoting and escaping12851286sub validate_action {1287my$input=shift||returnundef;1288returnundefunlessexists$actions{$input};1289return$input;1290}12911292sub validate_project {1293my$input=shift||returnundef;1294if(!validate_pathname($input) ||1295!(-d "$projectroot/$input") ||1296!check_export_ok("$projectroot/$input") ||1297($strict_export&& !project_in_list($input))) {1298returnundef;1299}else{1300return$input;1301}1302}13031304sub validate_pathname {1305my$input=shift||returnundef;13061307# no '.' or '..' as elements of path, i.e. no '.' nor '..'1308# at the beginning, at the end, and between slashes.1309# also this catches doubled slashes1310if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1311returnundef;1312}1313# no null characters1314if($input=~m!\0!) {1315returnundef;1316}1317return$input;1318}13191320sub validate_refname {1321my$input=shift||returnundef;13221323# textual hashes are O.K.1324if($input=~m/^[0-9a-fA-F]{40}$/) {1325return$input;1326}1327# it must be correct pathname1328$input= validate_pathname($input)1329orreturnundef;1330# restrictions on ref name according to git-check-ref-format1331if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1332returnundef;1333}1334return$input;1335}13361337# decode sequences of octets in utf8 into Perl's internal form,1338# which is utf-8 with utf8 flag set if needed. gitweb writes out1339# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1340sub to_utf8 {1341my$str=shift;1342returnundefunlessdefined$str;1343if(utf8::valid($str)) {1344 utf8::decode($str);1345return$str;1346}else{1347return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1348}1349}13501351# quote unsafe chars, but keep the slash, even when it's not1352# correct, but quoted slashes look too horrible in bookmarks1353sub esc_param {1354my$str=shift;1355returnundefunlessdefined$str;1356$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1357$str=~s/ /\+/g;1358return$str;1359}13601361# quote unsafe chars in whole URL, so some characters cannot be quoted1362sub esc_url {1363my$str=shift;1364returnundefunlessdefined$str;1365$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;1366$str=~s/ /\+/g;1367return$str;1368}13691370# replace invalid utf8 character with SUBSTITUTION sequence1371sub esc_html {1372my$str=shift;1373my%opts=@_;13741375returnundefunlessdefined$str;13761377$str= to_utf8($str);1378$str=$cgi->escapeHTML($str);1379if($opts{'-nbsp'}) {1380$str=~s/ / /g;1381}1382$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1383return$str;1384}13851386# quote control characters and escape filename to HTML1387sub esc_path {1388my$str=shift;1389my%opts=@_;13901391returnundefunlessdefined$str;13921393$str= to_utf8($str);1394$str=$cgi->escapeHTML($str);1395if($opts{'-nbsp'}) {1396$str=~s/ / /g;1397}1398$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1399return$str;1400}14011402# Make control characters "printable", using character escape codes (CEC)1403sub quot_cec {1404my$cntrl=shift;1405my%opts=@_;1406my%es= (# character escape codes, aka escape sequences1407"\t"=>'\t',# tab (HT)1408"\n"=>'\n',# line feed (LF)1409"\r"=>'\r',# carrige return (CR)1410"\f"=>'\f',# form feed (FF)1411"\b"=>'\b',# backspace (BS)1412"\a"=>'\a',# alarm (bell) (BEL)1413"\e"=>'\e',# escape (ESC)1414"\013"=>'\v',# vertical tab (VT)1415"\000"=>'\0',# nul character (NUL)1416);1417my$chr= ( (exists$es{$cntrl})1418?$es{$cntrl}1419:sprintf('\%2x',ord($cntrl)) );1420if($opts{-nohtml}) {1421return$chr;1422}else{1423return"<span class=\"cntrl\">$chr</span>";1424}1425}14261427# Alternatively use unicode control pictures codepoints,1428# Unicode "printable representation" (PR)1429sub quot_upr {1430my$cntrl=shift;1431my%opts=@_;14321433my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1434if($opts{-nohtml}) {1435return$chr;1436}else{1437return"<span class=\"cntrl\">$chr</span>";1438}1439}14401441# git may return quoted and escaped filenames1442sub unquote {1443my$str=shift;14441445sub unq {1446my$seq=shift;1447my%es= (# character escape codes, aka escape sequences1448't'=>"\t",# tab (HT, TAB)1449'n'=>"\n",# newline (NL)1450'r'=>"\r",# return (CR)1451'f'=>"\f",# form feed (FF)1452'b'=>"\b",# backspace (BS)1453'a'=>"\a",# alarm (bell) (BEL)1454'e'=>"\e",# escape (ESC)1455'v'=>"\013",# vertical tab (VT)1456);14571458if($seq=~m/^[0-7]{1,3}$/) {1459# octal char sequence1460returnchr(oct($seq));1461}elsif(exists$es{$seq}) {1462# C escape sequence, aka character escape code1463return$es{$seq};1464}1465# quoted ordinary character1466return$seq;1467}14681469if($str=~m/^"(.*)"$/) {1470# needs unquoting1471$str=$1;1472$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1473}1474return$str;1475}14761477# escape tabs (convert tabs to spaces)1478sub untabify {1479my$line=shift;14801481while((my$pos=index($line,"\t")) != -1) {1482if(my$count= (8- ($pos%8))) {1483my$spaces=' ' x $count;1484$line=~s/\t/$spaces/;1485}1486}14871488return$line;1489}14901491sub project_in_list {1492my$project=shift;1493my@list= git_get_projects_list();1494return@list&&scalar(grep{$_->{'path'}eq$project}@list);1495}14961497## ----------------------------------------------------------------------1498## HTML aware string manipulation14991500# Try to chop given string on a word boundary between position1501# $len and $len+$add_len. If there is no word boundary there,1502# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1503# (marking chopped part) would be longer than given string.1504sub chop_str {1505my$str=shift;1506my$len=shift;1507my$add_len=shift||10;1508my$where=shift||'right';# 'left' | 'center' | 'right'15091510# Make sure perl knows it is utf8 encoded so we don't1511# cut in the middle of a utf8 multibyte char.1512$str= to_utf8($str);15131514# allow only $len chars, but don't cut a word if it would fit in $add_len1515# if it doesn't fit, cut it if it's still longer than the dots we would add1516# remove chopped character entities entirely15171518# when chopping in the middle, distribute $len into left and right part1519# return early if chopping wouldn't make string shorter1520if($whereeq'center') {1521return$strif($len+5>=length($str));# filler is length 51522$len=int($len/2);1523}else{1524return$strif($len+4>=length($str));# filler is length 41525}15261527# regexps: ending and beginning with word part up to $add_len1528my$endre=qr/.{$len}\w{0,$add_len}/;1529my$begre=qr/\w{0,$add_len}.{$len}/;15301531if($whereeq'left') {1532$str=~m/^(.*?)($begre)$/;1533my($lead,$body) = ($1,$2);1534if(length($lead) >4) {1535$lead=" ...";1536}1537return"$lead$body";15381539}elsif($whereeq'center') {1540$str=~m/^($endre)(.*)$/;1541my($left,$str) = ($1,$2);1542$str=~m/^(.*?)($begre)$/;1543my($mid,$right) = ($1,$2);1544if(length($mid) >5) {1545$mid=" ... ";1546}1547return"$left$mid$right";15481549}else{1550$str=~m/^($endre)(.*)$/;1551my$body=$1;1552my$tail=$2;1553if(length($tail) >4) {1554$tail="... ";1555}1556return"$body$tail";1557}1558}15591560# takes the same arguments as chop_str, but also wraps a <span> around the1561# result with a title attribute if it does get chopped. Additionally, the1562# string is HTML-escaped.1563sub chop_and_escape_str {1564my($str) =@_;15651566my$chopped= chop_str(@_);1567if($choppedeq$str) {1568return esc_html($chopped);1569}else{1570$str=~s/[[:cntrl:]]/?/g;1571return$cgi->span({-title=>$str}, esc_html($chopped));1572}1573}15741575## ----------------------------------------------------------------------1576## functions returning short strings15771578# CSS class for given age value (in seconds)1579sub age_class {1580my$age=shift;15811582if(!defined$age) {1583return"noage";1584}elsif($age<60*60*2) {1585return"age0";1586}elsif($age<60*60*24*2) {1587return"age1";1588}else{1589return"age2";1590}1591}15921593# convert age in seconds to "nn units ago" string1594sub age_string {1595my$age=shift;1596my$age_str;15971598if($age>60*60*24*365*2) {1599$age_str= (int$age/60/60/24/365);1600$age_str.=" years ago";1601}elsif($age>60*60*24*(365/12)*2) {1602$age_str=int$age/60/60/24/(365/12);1603$age_str.=" months ago";1604}elsif($age>60*60*24*7*2) {1605$age_str=int$age/60/60/24/7;1606$age_str.=" weeks ago";1607}elsif($age>60*60*24*2) {1608$age_str=int$age/60/60/24;1609$age_str.=" days ago";1610}elsif($age>60*60*2) {1611$age_str=int$age/60/60;1612$age_str.=" hours ago";1613}elsif($age>60*2) {1614$age_str=int$age/60;1615$age_str.=" min ago";1616}elsif($age>2) {1617$age_str=int$age;1618$age_str.=" sec ago";1619}else{1620$age_str.=" right now";1621}1622return$age_str;1623}16241625useconstant{1626 S_IFINVALID =>0030000,1627 S_IFGITLINK =>0160000,1628};16291630# submodule/subproject, a commit object reference1631sub S_ISGITLINK {1632my$mode=shift;16331634return(($mode& S_IFMT) == S_IFGITLINK)1635}16361637# convert file mode in octal to symbolic file mode string1638sub mode_str {1639my$mode=oct shift;16401641if(S_ISGITLINK($mode)) {1642return'm---------';1643}elsif(S_ISDIR($mode& S_IFMT)) {1644return'drwxr-xr-x';1645}elsif(S_ISLNK($mode)) {1646return'lrwxrwxrwx';1647}elsif(S_ISREG($mode)) {1648# git cares only about the executable bit1649if($mode& S_IXUSR) {1650return'-rwxr-xr-x';1651}else{1652return'-rw-r--r--';1653};1654}else{1655return'----------';1656}1657}16581659# convert file mode in octal to file type string1660sub file_type {1661my$mode=shift;16621663if($mode!~m/^[0-7]+$/) {1664return$mode;1665}else{1666$mode=oct$mode;1667}16681669if(S_ISGITLINK($mode)) {1670return"submodule";1671}elsif(S_ISDIR($mode& S_IFMT)) {1672return"directory";1673}elsif(S_ISLNK($mode)) {1674return"symlink";1675}elsif(S_ISREG($mode)) {1676return"file";1677}else{1678return"unknown";1679}1680}16811682# convert file mode in octal to file type description string1683sub file_type_long {1684my$mode=shift;16851686if($mode!~m/^[0-7]+$/) {1687return$mode;1688}else{1689$mode=oct$mode;1690}16911692if(S_ISGITLINK($mode)) {1693return"submodule";1694}elsif(S_ISDIR($mode& S_IFMT)) {1695return"directory";1696}elsif(S_ISLNK($mode)) {1697return"symlink";1698}elsif(S_ISREG($mode)) {1699if($mode& S_IXUSR) {1700return"executable";1701}else{1702return"file";1703};1704}else{1705return"unknown";1706}1707}170817091710## ----------------------------------------------------------------------1711## functions returning short HTML fragments, or transforming HTML fragments1712## which don't belong to other sections17131714# format line of commit message.1715sub format_log_line_html {1716my$line=shift;17171718$line= esc_html($line, -nbsp=>1);1719$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1720$cgi->a({-href => href(action=>"object", hash=>$1),1721-class=>"text"},$1);1722}eg;17231724return$line;1725}17261727# format marker of refs pointing to given object17281729# the destination action is chosen based on object type and current context:1730# - for annotated tags, we choose the tag view unless it's the current view1731# already, in which case we go to shortlog view1732# - for other refs, we keep the current view if we're in history, shortlog or1733# log view, and select shortlog otherwise1734sub format_ref_marker {1735my($refs,$id) =@_;1736my$markers='';17371738if(defined$refs->{$id}) {1739foreachmy$ref(@{$refs->{$id}}) {1740# this code exploits the fact that non-lightweight tags are the1741# only indirect objects, and that they are the only objects for which1742# we want to use tag instead of shortlog as action1743my($type,$name) =qw();1744my$indirect= ($ref=~s/\^\{\}$//);1745# e.g. tags/v2.6.11 or heads/next1746if($ref=~m!^(.*?)s?/(.*)$!) {1747$type=$1;1748$name=$2;1749}else{1750$type="ref";1751$name=$ref;1752}17531754my$class=$type;1755$class.=" indirect"if$indirect;17561757my$dest_action="shortlog";17581759if($indirect) {1760$dest_action="tag"unless$actioneq"tag";1761}elsif($action=~/^(history|(short)?log)$/) {1762$dest_action=$action;1763}17641765my$dest="";1766$dest.="refs/"unless$ref=~ m!^refs/!;1767$dest.=$ref;17681769my$link=$cgi->a({1770-href => href(1771 action=>$dest_action,1772 hash=>$dest1773)},$name);17741775$markers.=" <span class=\"$class\"title=\"$ref\">".1776$link."</span>";1777}1778}17791780if($markers) {1781return' <span class="refs">'.$markers.'</span>';1782}else{1783return"";1784}1785}17861787# format, perhaps shortened and with markers, title line1788sub format_subject_html {1789my($long,$short,$href,$extra) =@_;1790$extra=''unlessdefined($extra);17911792if(length($short) <length($long)) {1793$long=~s/[[:cntrl:]]/?/g;1794return$cgi->a({-href =>$href, -class=>"list subject",1795-title => to_utf8($long)},1796 esc_html($short)) .$extra;1797}else{1798return$cgi->a({-href =>$href, -class=>"list subject"},1799 esc_html($long)) .$extra;1800}1801}18021803# Rather than recomputing the url for an email multiple times, we cache it1804# after the first hit. This gives a visible benefit in views where the avatar1805# for the same email is used repeatedly (e.g. shortlog).1806# The cache is shared by all avatar engines (currently gravatar only), which1807# are free to use it as preferred. Since only one avatar engine is used for any1808# given page, there's no risk for cache conflicts.1809our%avatar_cache= ();18101811# Compute the picon url for a given email, by using the picon search service over at1812# http://www.cs.indiana.edu/picons/search.html1813sub picon_url {1814my$email=lc shift;1815if(!$avatar_cache{$email}) {1816my($user,$domain) =split('@',$email);1817$avatar_cache{$email} =1818"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1819"$domain/$user/".1820"users+domains+unknown/up/single";1821}1822return$avatar_cache{$email};1823}18241825# Compute the gravatar url for a given email, if it's not in the cache already.1826# Gravatar stores only the part of the URL before the size, since that's the1827# one computationally more expensive. This also allows reuse of the cache for1828# different sizes (for this particular engine).1829sub gravatar_url {1830my$email=lc shift;1831my$size=shift;1832$avatar_cache{$email} ||=1833"http://www.gravatar.com/avatar/".1834 Digest::MD5::md5_hex($email) ."?s=";1835return$avatar_cache{$email} .$size;1836}18371838# Insert an avatar for the given $email at the given $size if the feature1839# is enabled.1840sub git_get_avatar {1841my($email,%opts) =@_;1842my$pre_white= ($opts{-pad_before} ?" ":"");1843my$post_white= ($opts{-pad_after} ?" ":"");1844$opts{-size} ||='default';1845my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1846my$url="";1847if($git_avatareq'gravatar') {1848$url= gravatar_url($email,$size);1849}elsif($git_avatareq'picon') {1850$url= picon_url($email);1851}1852# Other providers can be added by extending the if chain, defining $url1853# as needed. If no variant puts something in $url, we assume avatars1854# are completely disabled/unavailable.1855if($url) {1856return$pre_white.1857"<img width=\"$size\"".1858"class=\"avatar\"".1859"src=\"$url\"".1860"alt=\"\"".1861"/>".$post_white;1862}else{1863return"";1864}1865}18661867sub format_search_author {1868my($author,$searchtype,$displaytext) =@_;1869my$have_search= gitweb_check_feature('search');18701871if($have_search) {1872my$performed="";1873if($searchtypeeq'author') {1874$performed="authored";1875}elsif($searchtypeeq'committer') {1876$performed="committed";1877}18781879return$cgi->a({-href => href(action=>"search", hash=>$hash,1880 searchtext=>$author,1881 searchtype=>$searchtype),class=>"list",1882 title=>"Search for commits$performedby$author"},1883$displaytext);18841885}else{1886return$displaytext;1887}1888}18891890# format the author name of the given commit with the given tag1891# the author name is chopped and escaped according to the other1892# optional parameters (see chop_str).1893sub format_author_html {1894my$tag=shift;1895my$co=shift;1896my$author= chop_and_escape_str($co->{'author_name'},@_);1897return"<$tagclass=\"author\">".1898 format_search_author($co->{'author_name'},"author",1899 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1900$author) .1901"</$tag>";1902}19031904# format git diff header line, i.e. "diff --(git|combined|cc) ..."1905sub format_git_diff_header_line {1906my$line=shift;1907my$diffinfo=shift;1908my($from,$to) =@_;19091910if($diffinfo->{'nparents'}) {1911# combined diff1912$line=~s!^(diff (.*?) )"?.*$!$1!;1913if($to->{'href'}) {1914$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1915 esc_path($to->{'file'}));1916}else{# file was deleted (no href)1917$line.= esc_path($to->{'file'});1918}1919}else{1920# "ordinary" diff1921$line=~s!^(diff (.*?) )"?a/.*$!$1!;1922if($from->{'href'}) {1923$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1924'a/'. esc_path($from->{'file'}));1925}else{# file was added (no href)1926$line.='a/'. esc_path($from->{'file'});1927}1928$line.=' ';1929if($to->{'href'}) {1930$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1931'b/'. esc_path($to->{'file'}));1932}else{# file was deleted1933$line.='b/'. esc_path($to->{'file'});1934}1935}19361937return"<div class=\"diff header\">$line</div>\n";1938}19391940# format extended diff header line, before patch itself1941sub format_extended_diff_header_line {1942my$line=shift;1943my$diffinfo=shift;1944my($from,$to) =@_;19451946# match <path>1947if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1948$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1949 esc_path($from->{'file'}));1950}1951if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1952$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1953 esc_path($to->{'file'}));1954}1955# match single <mode>1956if($line=~m/\s(\d{6})$/) {1957$line.='<span class="info"> ('.1958 file_type_long($1) .1959')</span>';1960}1961# match <hash>1962if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1963# can match only for combined diff1964$line='index ';1965for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1966if($from->{'href'}[$i]) {1967$line.=$cgi->a({-href=>$from->{'href'}[$i],1968-class=>"hash"},1969substr($diffinfo->{'from_id'}[$i],0,7));1970}else{1971$line.='0' x 7;1972}1973# separator1974$line.=','if($i<$diffinfo->{'nparents'} -1);1975}1976$line.='..';1977if($to->{'href'}) {1978$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1979substr($diffinfo->{'to_id'},0,7));1980}else{1981$line.='0' x 7;1982}19831984}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1985# can match only for ordinary diff1986my($from_link,$to_link);1987if($from->{'href'}) {1988$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1989substr($diffinfo->{'from_id'},0,7));1990}else{1991$from_link='0' x 7;1992}1993if($to->{'href'}) {1994$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1995substr($diffinfo->{'to_id'},0,7));1996}else{1997$to_link='0' x 7;1998}1999my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});2000$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;2001}20022003return$line."<br/>\n";2004}20052006# format from-file/to-file diff header2007sub format_diff_from_to_header {2008my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;2009my$line;2010my$result='';20112012$line=$from_line;2013#assert($line =~ m/^---/) if DEBUG;2014# no extra formatting for "^--- /dev/null"2015if(!$diffinfo->{'nparents'}) {2016# ordinary (single parent) diff2017if($line=~m!^--- "?a/!) {2018if($from->{'href'}) {2019$line='--- a/'.2020$cgi->a({-href=>$from->{'href'}, -class=>"path"},2021 esc_path($from->{'file'}));2022}else{2023$line='--- a/'.2024 esc_path($from->{'file'});2025}2026}2027$result.= qq!<div class="diff from_file">$line</div>\n!;20282029}else{2030# combined diff (merge commit)2031for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2032if($from->{'href'}[$i]) {2033$line='--- '.2034$cgi->a({-href=>href(action=>"blobdiff",2035 hash_parent=>$diffinfo->{'from_id'}[$i],2036 hash_parent_base=>$parents[$i],2037 file_parent=>$from->{'file'}[$i],2038 hash=>$diffinfo->{'to_id'},2039 hash_base=>$hash,2040 file_name=>$to->{'file'}),2041-class=>"path",2042-title=>"diff". ($i+1)},2043$i+1) .2044'/'.2045$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2046 esc_path($from->{'file'}[$i]));2047}else{2048$line='--- /dev/null';2049}2050$result.= qq!<div class="diff from_file">$line</div>\n!;2051}2052}20532054$line=$to_line;2055#assert($line =~ m/^\+\+\+/) if DEBUG;2056# no extra formatting for "^+++ /dev/null"2057if($line=~m!^\+\+\+ "?b/!) {2058if($to->{'href'}) {2059$line='+++ b/'.2060$cgi->a({-href=>$to->{'href'}, -class=>"path"},2061 esc_path($to->{'file'}));2062}else{2063$line='+++ b/'.2064 esc_path($to->{'file'});2065}2066}2067$result.= qq!<div class="diff to_file">$line</div>\n!;20682069return$result;2070}20712072# create note for patch simplified by combined diff2073sub format_diff_cc_simplified {2074my($diffinfo,@parents) =@_;2075my$result='';20762077$result.="<div class=\"diff header\">".2078"diff --cc ";2079if(!is_deleted($diffinfo)) {2080$result.=$cgi->a({-href => href(action=>"blob",2081 hash_base=>$hash,2082 hash=>$diffinfo->{'to_id'},2083 file_name=>$diffinfo->{'to_file'}),2084-class=>"path"},2085 esc_path($diffinfo->{'to_file'}));2086}else{2087$result.= esc_path($diffinfo->{'to_file'});2088}2089$result.="</div>\n".# class="diff header"2090"<div class=\"diff nodifferences\">".2091"Simple merge".2092"</div>\n";# class="diff nodifferences"20932094return$result;2095}20962097# format patch (diff) line (not to be used for diff headers)2098sub format_diff_line {2099my$line=shift;2100my($from,$to) =@_;2101my$diff_class="";21022103chomp$line;21042105if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2106# combined diff2107my$prefix=substr($line,0,scalar@{$from->{'href'}});2108if($line=~m/^\@{3}/) {2109$diff_class=" chunk_header";2110}elsif($line=~m/^\\/) {2111$diff_class=" incomplete";2112}elsif($prefix=~tr/+/+/) {2113$diff_class=" add";2114}elsif($prefix=~tr/-/-/) {2115$diff_class=" rem";2116}2117}else{2118# assume ordinary diff2119my$char=substr($line,0,1);2120if($chareq'+') {2121$diff_class=" add";2122}elsif($chareq'-') {2123$diff_class=" rem";2124}elsif($chareq'@') {2125$diff_class=" chunk_header";2126}elsif($chareq"\\") {2127$diff_class=" incomplete";2128}2129}2130$line= untabify($line);2131if($from&&$to&&$line=~m/^\@{2} /) {2132my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2133$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;21342135$from_lines=0unlessdefined$from_lines;2136$to_lines=0unlessdefined$to_lines;21372138if($from->{'href'}) {2139$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2140-class=>"list"},$from_text);2141}2142if($to->{'href'}) {2143$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2144-class=>"list"},$to_text);2145}2146$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2147"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2148return"<div class=\"diff$diff_class\">$line</div>\n";2149}elsif($from&&$to&&$line=~m/^\@{3}/) {2150my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2151my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);21522153@from_text=split(' ',$ranges);2154for(my$i=0;$i<@from_text; ++$i) {2155($from_start[$i],$from_nlines[$i]) =2156(split(',',substr($from_text[$i],1)),0);2157}21582159$to_text=pop@from_text;2160$to_start=pop@from_start;2161$to_nlines=pop@from_nlines;21622163$line="<span class=\"chunk_info\">$prefix";2164for(my$i=0;$i<@from_text; ++$i) {2165if($from->{'href'}[$i]) {2166$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2167-class=>"list"},$from_text[$i]);2168}else{2169$line.=$from_text[$i];2170}2171$line.=" ";2172}2173if($to->{'href'}) {2174$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2175-class=>"list"},$to_text);2176}else{2177$line.=$to_text;2178}2179$line.="$prefix</span>".2180"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2181return"<div class=\"diff$diff_class\">$line</div>\n";2182}2183return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2184}21852186# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2187# linked. Pass the hash of the tree/commit to snapshot.2188sub format_snapshot_links {2189my($hash) =@_;2190my$num_fmts=@snapshot_fmts;2191if($num_fmts>1) {2192# A parenthesized list of links bearing format names.2193# e.g. "snapshot (_tar.gz_ _zip_)"2194return"snapshot (".join(' ',map2195$cgi->a({2196-href => href(2197 action=>"snapshot",2198 hash=>$hash,2199 snapshot_format=>$_2200)2201},$known_snapshot_formats{$_}{'display'})2202,@snapshot_fmts) .")";2203}elsif($num_fmts==1) {2204# A single "snapshot" link whose tooltip bears the format name.2205# i.e. "_snapshot_"2206my($fmt) =@snapshot_fmts;2207return2208$cgi->a({2209-href => href(2210 action=>"snapshot",2211 hash=>$hash,2212 snapshot_format=>$fmt2213),2214-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2215},"snapshot");2216}else{# $num_fmts == 02217returnundef;2218}2219}22202221## ......................................................................2222## functions returning values to be passed, perhaps after some2223## transformation, to other functions; e.g. returning arguments to href()22242225# returns hash to be passed to href to generate gitweb URL2226# in -title key it returns description of link2227sub get_feed_info {2228my$format=shift||'Atom';2229my%res= (action =>lc($format));22302231# feed links are possible only for project views2232return unless(defined$project);2233# some views should link to OPML, or to generic project feed,2234# or don't have specific feed yet (so they should use generic)2235return if($action=~/^(?:tags|heads|forks|tag|search)$/x);22362237my$branch;2238# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2239# from tag links; this also makes possible to detect branch links2240if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2241(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2242$branch=$1;2243}2244# find log type for feed description (title)2245my$type='log';2246if(defined$file_name) {2247$type="history of$file_name";2248$type.="/"if($actioneq'tree');2249$type.=" on '$branch'"if(defined$branch);2250}else{2251$type="log of$branch"if(defined$branch);2252}22532254$res{-title} =$type;2255$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2256$res{'file_name'} =$file_name;22572258return%res;2259}22602261## ----------------------------------------------------------------------2262## git utility subroutines, invoking git commands22632264# returns path to the core git executable and the --git-dir parameter as list2265sub git_cmd {2266$number_of_git_cmds++;2267return$GIT,'--git-dir='.$git_dir;2268}22692270# quote the given arguments for passing them to the shell2271# quote_command("command", "arg 1", "arg with ' and ! characters")2272# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2273# Try to avoid using this function wherever possible.2274sub quote_command {2275returnjoin(' ',2276map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2277}22782279# get HEAD ref of given project as hash2280sub git_get_head_hash {2281return git_get_full_hash(shift,'HEAD');2282}22832284sub git_get_full_hash {2285return git_get_hash(@_);2286}22872288sub git_get_short_hash {2289return git_get_hash(@_,'--short=7');2290}22912292sub git_get_hash {2293my($project,$hash,@options) =@_;2294my$o_git_dir=$git_dir;2295my$retval=undef;2296$git_dir="$projectroot/$project";2297if(open my$fd,'-|', git_cmd(),'rev-parse',2298'--verify','-q',@options,$hash) {2299$retval= <$fd>;2300chomp$retvalifdefined$retval;2301close$fd;2302}2303if(defined$o_git_dir) {2304$git_dir=$o_git_dir;2305}2306return$retval;2307}23082309# get type of given object2310sub git_get_type {2311my$hash=shift;23122313open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2314my$type= <$fd>;2315close$fdorreturn;2316chomp$type;2317return$type;2318}23192320# repository configuration2321our$config_file='';2322our%config;23232324# store multiple values for single key as anonymous array reference2325# single values stored directly in the hash, not as [ <value> ]2326sub hash_set_multi {2327my($hash,$key,$value) =@_;23282329if(!exists$hash->{$key}) {2330$hash->{$key} =$value;2331}elsif(!ref$hash->{$key}) {2332$hash->{$key} = [$hash->{$key},$value];2333}else{2334push@{$hash->{$key}},$value;2335}2336}23372338# return hash of git project configuration2339# optionally limited to some section, e.g. 'gitweb'2340sub git_parse_project_config {2341my$section_regexp=shift;2342my%config;23432344local$/="\0";23452346open my$fh,"-|", git_cmd(),"config",'-z','-l',2347orreturn;23482349while(my$keyval= <$fh>) {2350chomp$keyval;2351my($key,$value) =split(/\n/,$keyval,2);23522353 hash_set_multi(\%config,$key,$value)2354if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2355}2356close$fh;23572358return%config;2359}23602361# convert config value to boolean: 'true' or 'false'2362# no value, number > 0, 'true' and 'yes' values are true2363# rest of values are treated as false (never as error)2364sub config_to_bool {2365my$val=shift;23662367return1if!defined$val;# section.key23682369# strip leading and trailing whitespace2370$val=~s/^\s+//;2371$val=~s/\s+$//;23722373return(($val=~/^\d+$/&&$val) ||# section.key = 12374($val=~/^(?:true|yes)$/i));# section.key = true2375}23762377# convert config value to simple decimal number2378# an optional value suffix of 'k', 'm', or 'g' will cause the value2379# to be multiplied by 1024, 1048576, or 10737418242380sub config_to_int {2381my$val=shift;23822383# strip leading and trailing whitespace2384$val=~s/^\s+//;2385$val=~s/\s+$//;23862387if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2388$unit=lc($unit);2389# unknown unit is treated as 12390return$num* ($uniteq'g'?1073741824:2391$uniteq'm'?1048576:2392$uniteq'k'?1024:1);2393}2394return$val;2395}23962397# convert config value to array reference, if needed2398sub config_to_multi {2399my$val=shift;24002401returnref($val) ?$val: (defined($val) ? [$val] : []);2402}24032404sub git_get_project_config {2405my($key,$type) =@_;24062407return unlessdefined$git_dir;24082409# key sanity check2410return unless($key);2411$key=~s/^gitweb\.//;2412return if($key=~m/\W/);24132414# type sanity check2415if(defined$type) {2416$type=~s/^--//;2417$type=undef2418unless($typeeq'bool'||$typeeq'int');2419}24202421# get config2422if(!defined$config_file||2423$config_filene"$git_dir/config") {2424%config= git_parse_project_config('gitweb');2425$config_file="$git_dir/config";2426}24272428# check if config variable (key) exists2429return unlessexists$config{"gitweb.$key"};24302431# ensure given type2432if(!defined$type) {2433return$config{"gitweb.$key"};2434}elsif($typeeq'bool') {2435# backward compatibility: 'git config --bool' returns true/false2436return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2437}elsif($typeeq'int') {2438return config_to_int($config{"gitweb.$key"});2439}2440return$config{"gitweb.$key"};2441}24422443# get hash of given path at given ref2444sub git_get_hash_by_path {2445my$base=shift;2446my$path=shift||returnundef;2447my$type=shift;24482449$path=~ s,/+$,,;24502451open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2452or die_error(500,"Open git-ls-tree failed");2453my$line= <$fd>;2454close$fdorreturnundef;24552456if(!defined$line) {2457# there is no tree or hash given by $path at $base2458returnundef;2459}24602461#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2462$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2463if(defined$type&&$typene$2) {2464# type doesn't match2465returnundef;2466}2467return$3;2468}24692470# get path of entry with given hash at given tree-ish (ref)2471# used to get 'from' filename for combined diff (merge commit) for renames2472sub git_get_path_by_hash {2473my$base=shift||return;2474my$hash=shift||return;24752476local$/="\0";24772478open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2479orreturnundef;2480while(my$line= <$fd>) {2481chomp$line;24822483#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2484#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2485if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2486close$fd;2487return$1;2488}2489}2490close$fd;2491returnundef;2492}24932494## ......................................................................2495## git utility functions, directly accessing git repository24962497sub git_get_project_description {2498my$path=shift;24992500$git_dir="$projectroot/$path";2501open my$fd,'<',"$git_dir/description"2502orreturn git_get_project_config('description');2503my$descr= <$fd>;2504close$fd;2505if(defined$descr) {2506chomp$descr;2507}2508return$descr;2509}25102511sub git_get_project_ctags {2512my$path=shift;2513my$ctags= {};25142515$git_dir="$projectroot/$path";2516opendir my$dh,"$git_dir/ctags"2517orreturn$ctags;2518foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2519open my$ct,'<',$_ornext;2520my$val= <$ct>;2521chomp$val;2522close$ct;2523my$ctag=$_;$ctag=~ s#.*/##;2524$ctags->{$ctag} =$val;2525}2526closedir$dh;2527$ctags;2528}25292530sub git_populate_project_tagcloud {2531my$ctags=shift;25322533# First, merge different-cased tags; tags vote on casing2534my%ctags_lc;2535foreach(keys%$ctags) {2536$ctags_lc{lc$_}->{count} +=$ctags->{$_};2537if(not$ctags_lc{lc$_}->{topcount}2538or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2539$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2540$ctags_lc{lc$_}->{topname} =$_;2541}2542}25432544my$cloud;2545if(eval{require HTML::TagCloud;1; }) {2546$cloud= HTML::TagCloud->new;2547foreach(sort keys%ctags_lc) {2548# Pad the title with spaces so that the cloud looks2549# less crammed.2550my$title=$ctags_lc{$_}->{topname};2551$title=~s/ / /g;2552$title=~s/^/ /g;2553$title=~s/$/ /g;2554$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2555}2556}else{2557$cloud= \%ctags_lc;2558}2559$cloud;2560}25612562sub git_show_project_tagcloud {2563my($cloud,$count) =@_;2564print STDERR ref($cloud)."..\n";2565if(ref$cloudeq'HTML::TagCloud') {2566return$cloud->html_and_css($count);2567}else{2568my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2569return'<p align="center">'.join(', ',map{2570"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2571}splice(@tags,0,$count)) .'</p>';2572}2573}25742575sub git_get_project_url_list {2576my$path=shift;25772578$git_dir="$projectroot/$path";2579open my$fd,'<',"$git_dir/cloneurl"2580orreturnwantarray?2581@{ config_to_multi(git_get_project_config('url')) } :2582 config_to_multi(git_get_project_config('url'));2583my@git_project_url_list=map{chomp;$_} <$fd>;2584close$fd;25852586returnwantarray?@git_project_url_list: \@git_project_url_list;2587}25882589sub git_get_projects_list {2590my($filter) =@_;2591my@list;25922593$filter||='';2594$filter=~s/\.git$//;25952596my$check_forks= gitweb_check_feature('forks');25972598if(-d $projects_list) {2599# search in directory2600my$dir=$projects_list. ($filter?"/$filter":'');2601# remove the trailing "/"2602$dir=~s!/+$!!;2603my$pfxlen=length("$dir");2604my$pfxdepth= ($dir=~tr!/!!);26052606 File::Find::find({2607 follow_fast =>1,# follow symbolic links2608 follow_skip =>2,# ignore duplicates2609 dangling_symlinks =>0,# ignore dangling symlinks, silently2610 wanted =>sub{2611# global variables2612our$project_maxdepth;2613our$projectroot;2614# skip project-list toplevel, if we get it.2615return if(m!^[/.]$!);2616# only directories can be git repositories2617return unless(-d $_);2618# don't traverse too deep (Find is super slow on os x)2619if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2620$File::Find::prune =1;2621return;2622}26232624my$subdir=substr($File::Find::name,$pfxlen+1);2625# we check related file in $projectroot2626my$path= ($filter?"$filter/":'') .$subdir;2627if(check_export_ok("$projectroot/$path")) {2628push@list, { path =>$path};2629$File::Find::prune =1;2630}2631},2632},"$dir");26332634}elsif(-f $projects_list) {2635# read from file(url-encoded):2636# 'git%2Fgit.git Linus+Torvalds'2637# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2638# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2639my%paths;2640open my$fd,'<',$projects_listorreturn;2641 PROJECT:2642while(my$line= <$fd>) {2643chomp$line;2644my($path,$owner) =split' ',$line;2645$path= unescape($path);2646$owner= unescape($owner);2647if(!defined$path) {2648next;2649}2650if($filterne'') {2651# looking for forks;2652my$pfx=substr($path,0,length($filter));2653if($pfxne$filter) {2654next PROJECT;2655}2656my$sfx=substr($path,length($filter));2657if($sfx!~/^\/.*\.git$/) {2658next PROJECT;2659}2660}elsif($check_forks) {2661 PATH:2662foreachmy$filter(keys%paths) {2663# looking for forks;2664my$pfx=substr($path,0,length($filter));2665if($pfxne$filter) {2666next PATH;2667}2668my$sfx=substr($path,length($filter));2669if($sfx!~/^\/.*\.git$/) {2670next PATH;2671}2672# is a fork, don't include it in2673# the list2674next PROJECT;2675}2676}2677if(check_export_ok("$projectroot/$path")) {2678my$pr= {2679 path =>$path,2680 owner => to_utf8($owner),2681};2682push@list,$pr;2683(my$forks_path=$path) =~s/\.git$//;2684$paths{$forks_path}++;2685}2686}2687close$fd;2688}2689return@list;2690}26912692our$gitweb_project_owner=undef;2693sub git_get_project_list_from_file {26942695return if(defined$gitweb_project_owner);26962697$gitweb_project_owner= {};2698# read from file (url-encoded):2699# 'git%2Fgit.git Linus+Torvalds'2700# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2701# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2702if(-f $projects_list) {2703open(my$fd,'<',$projects_list);2704while(my$line= <$fd>) {2705chomp$line;2706my($pr,$ow) =split' ',$line;2707$pr= unescape($pr);2708$ow= unescape($ow);2709$gitweb_project_owner->{$pr} = to_utf8($ow);2710}2711close$fd;2712}2713}27142715sub git_get_project_owner {2716my$project=shift;2717my$owner;27182719returnundefunless$project;2720$git_dir="$projectroot/$project";27212722if(!defined$gitweb_project_owner) {2723 git_get_project_list_from_file();2724}27252726if(exists$gitweb_project_owner->{$project}) {2727$owner=$gitweb_project_owner->{$project};2728}2729if(!defined$owner){2730$owner= git_get_project_config('owner');2731}2732if(!defined$owner) {2733$owner= get_file_owner("$git_dir");2734}27352736return$owner;2737}27382739sub git_get_last_activity {2740my($path) =@_;2741my$fd;27422743$git_dir="$projectroot/$path";2744open($fd,"-|", git_cmd(),'for-each-ref',2745'--format=%(committer)',2746'--sort=-committerdate',2747'--count=1',2748'refs/heads')orreturn;2749my$most_recent= <$fd>;2750close$fdorreturn;2751if(defined$most_recent&&2752$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2753my$timestamp=$1;2754my$age=time-$timestamp;2755return($age, age_string($age));2756}2757return(undef,undef);2758}27592760sub git_get_references {2761my$type=shift||"";2762my%refs;2763# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112764# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2765open my$fd,"-|", git_cmd(),"show-ref","--dereference",2766($type? ("--","refs/$type") : ())# use -- <pattern> if $type2767orreturn;27682769while(my$line= <$fd>) {2770chomp$line;2771if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2772if(defined$refs{$1}) {2773push@{$refs{$1}},$2;2774}else{2775$refs{$1} = [$2];2776}2777}2778}2779close$fdorreturn;2780return \%refs;2781}27822783sub git_get_rev_name_tags {2784my$hash=shift||returnundef;27852786open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2787orreturn;2788my$name_rev= <$fd>;2789close$fd;27902791if($name_rev=~ m|^$hash tags/(.*)$|) {2792return$1;2793}else{2794# catches also '$hash undefined' output2795returnundef;2796}2797}27982799## ----------------------------------------------------------------------2800## parse to hash functions28012802sub parse_date {2803my$epoch=shift;2804my$tz=shift||"-0000";28052806my%date;2807my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2808my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2809my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2810$date{'hour'} =$hour;2811$date{'minute'} =$min;2812$date{'mday'} =$mday;2813$date{'day'} =$days[$wday];2814$date{'month'} =$months[$mon];2815$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2816$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2817$date{'mday-time'} =sprintf"%d%s%02d:%02d",2818$mday,$months[$mon],$hour,$min;2819$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",28201900+$year,1+$mon,$mday,$hour,$min,$sec;28212822$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2823my$local=$epoch+ ((int$1+ ($2/60)) *3600);2824($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2825$date{'hour_local'} =$hour;2826$date{'minute_local'} =$min;2827$date{'tz_local'} =$tz;2828$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",28291900+$year,$mon+1,$mday,2830$hour,$min,$sec,$tz);2831return%date;2832}28332834sub parse_tag {2835my$tag_id=shift;2836my%tag;2837my@comment;28382839open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2840$tag{'id'} =$tag_id;2841while(my$line= <$fd>) {2842chomp$line;2843if($line=~m/^object ([0-9a-fA-F]{40})$/) {2844$tag{'object'} =$1;2845}elsif($line=~m/^type (.+)$/) {2846$tag{'type'} =$1;2847}elsif($line=~m/^tag (.+)$/) {2848$tag{'name'} =$1;2849}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2850$tag{'author'} =$1;2851$tag{'author_epoch'} =$2;2852$tag{'author_tz'} =$3;2853if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2854$tag{'author_name'} =$1;2855$tag{'author_email'} =$2;2856}else{2857$tag{'author_name'} =$tag{'author'};2858}2859}elsif($line=~m/--BEGIN/) {2860push@comment,$line;2861last;2862}elsif($lineeq"") {2863last;2864}2865}2866push@comment, <$fd>;2867$tag{'comment'} = \@comment;2868close$fdorreturn;2869if(!defined$tag{'name'}) {2870return2871};2872return%tag2873}28742875sub parse_commit_text {2876my($commit_text,$withparents) =@_;2877my@commit_lines=split'\n',$commit_text;2878my%co;28792880pop@commit_lines;# Remove '\0'28812882if(!@commit_lines) {2883return;2884}28852886my$header=shift@commit_lines;2887if($header!~m/^[0-9a-fA-F]{40}/) {2888return;2889}2890($co{'id'},my@parents) =split' ',$header;2891while(my$line=shift@commit_lines) {2892last if$lineeq"\n";2893if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2894$co{'tree'} =$1;2895}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2896push@parents,$1;2897}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2898$co{'author'} = to_utf8($1);2899$co{'author_epoch'} =$2;2900$co{'author_tz'} =$3;2901if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2902$co{'author_name'} =$1;2903$co{'author_email'} =$2;2904}else{2905$co{'author_name'} =$co{'author'};2906}2907}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2908$co{'committer'} = to_utf8($1);2909$co{'committer_epoch'} =$2;2910$co{'committer_tz'} =$3;2911if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2912$co{'committer_name'} =$1;2913$co{'committer_email'} =$2;2914}else{2915$co{'committer_name'} =$co{'committer'};2916}2917}2918}2919if(!defined$co{'tree'}) {2920return;2921};2922$co{'parents'} = \@parents;2923$co{'parent'} =$parents[0];29242925foreachmy$title(@commit_lines) {2926$title=~s/^ //;2927if($titlene"") {2928$co{'title'} = chop_str($title,80,5);2929# remove leading stuff of merges to make the interesting part visible2930if(length($title) >50) {2931$title=~s/^Automatic //;2932$title=~s/^merge (of|with) /Merge ... /i;2933if(length($title) >50) {2934$title=~s/(http|rsync):\/\///;2935}2936if(length($title) >50) {2937$title=~s/(master|www|rsync)\.//;2938}2939if(length($title) >50) {2940$title=~s/kernel.org:?//;2941}2942if(length($title) >50) {2943$title=~s/\/pub\/scm//;2944}2945}2946$co{'title_short'} = chop_str($title,50,5);2947last;2948}2949}2950if(!defined$co{'title'} ||$co{'title'}eq"") {2951$co{'title'} =$co{'title_short'} ='(no commit message)';2952}2953# remove added spaces2954foreachmy$line(@commit_lines) {2955$line=~s/^ //;2956}2957$co{'comment'} = \@commit_lines;29582959my$age=time-$co{'committer_epoch'};2960$co{'age'} =$age;2961$co{'age_string'} = age_string($age);2962my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2963if($age>60*60*24*7*2) {2964$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2965$co{'age_string_age'} =$co{'age_string'};2966}else{2967$co{'age_string_date'} =$co{'age_string'};2968$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2969}2970return%co;2971}29722973sub parse_commit {2974my($commit_id) =@_;2975my%co;29762977local$/="\0";29782979open my$fd,"-|", git_cmd(),"rev-list",2980"--parents",2981"--header",2982"--max-count=1",2983$commit_id,2984"--",2985or die_error(500,"Open git-rev-list failed");2986%co= parse_commit_text(<$fd>,1);2987close$fd;29882989return%co;2990}29912992sub parse_commits {2993my($commit_id,$maxcount,$skip,$filename,@args) =@_;2994my@cos;29952996$maxcount||=1;2997$skip||=0;29982999local$/="\0";30003001open my$fd,"-|", git_cmd(),"rev-list",3002"--header",3003@args,3004("--max-count=".$maxcount),3005("--skip=".$skip),3006@extra_options,3007$commit_id,3008"--",3009($filename? ($filename) : ())3010or die_error(500,"Open git-rev-list failed");3011while(my$line= <$fd>) {3012my%co= parse_commit_text($line);3013push@cos, \%co;3014}3015close$fd;30163017returnwantarray?@cos: \@cos;3018}30193020# parse line of git-diff-tree "raw" output3021sub parse_difftree_raw_line {3022my$line=shift;3023my%res;30243025# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'3026# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'3027if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {3028$res{'from_mode'} =$1;3029$res{'to_mode'} =$2;3030$res{'from_id'} =$3;3031$res{'to_id'} =$4;3032$res{'status'} =$5;3033$res{'similarity'} =$6;3034if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3035($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3036}else{3037$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3038}3039}3040# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3041# combined diff (for merge commit)3042elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3043$res{'nparents'} =length($1);3044$res{'from_mode'} = [split(' ',$2) ];3045$res{'to_mode'} =pop@{$res{'from_mode'}};3046$res{'from_id'} = [split(' ',$3) ];3047$res{'to_id'} =pop@{$res{'from_id'}};3048$res{'status'} = [split('',$4) ];3049$res{'to_file'} = unquote($5);3050}3051# 'c512b523472485aef4fff9e57b229d9d243c967f'3052elsif($line=~m/^([0-9a-fA-F]{40})$/) {3053$res{'commit'} =$1;3054}30553056returnwantarray?%res: \%res;3057}30583059# wrapper: return parsed line of git-diff-tree "raw" output3060# (the argument might be raw line, or parsed info)3061sub parsed_difftree_line {3062my$line_or_ref=shift;30633064if(ref($line_or_ref)eq"HASH") {3065# pre-parsed (or generated by hand)3066return$line_or_ref;3067}else{3068return parse_difftree_raw_line($line_or_ref);3069}3070}30713072# parse line of git-ls-tree output3073sub parse_ls_tree_line {3074my$line=shift;3075my%opts=@_;3076my%res;30773078if($opts{'-l'}) {3079#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3080$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;30813082$res{'mode'} =$1;3083$res{'type'} =$2;3084$res{'hash'} =$3;3085$res{'size'} =$4;3086if($opts{'-z'}) {3087$res{'name'} =$5;3088}else{3089$res{'name'} = unquote($5);3090}3091}else{3092#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3093$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;30943095$res{'mode'} =$1;3096$res{'type'} =$2;3097$res{'hash'} =$3;3098if($opts{'-z'}) {3099$res{'name'} =$4;3100}else{3101$res{'name'} = unquote($4);3102}3103}31043105returnwantarray?%res: \%res;3106}31073108# generates _two_ hashes, references to which are passed as 2 and 3 argument3109sub parse_from_to_diffinfo {3110my($diffinfo,$from,$to,@parents) =@_;31113112if($diffinfo->{'nparents'}) {3113# combined diff3114$from->{'file'} = [];3115$from->{'href'} = [];3116 fill_from_file_info($diffinfo,@parents)3117unlessexists$diffinfo->{'from_file'};3118for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3119$from->{'file'}[$i] =3120defined$diffinfo->{'from_file'}[$i] ?3121$diffinfo->{'from_file'}[$i] :3122$diffinfo->{'to_file'};3123if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3124$from->{'href'}[$i] = href(action=>"blob",3125 hash_base=>$parents[$i],3126 hash=>$diffinfo->{'from_id'}[$i],3127 file_name=>$from->{'file'}[$i]);3128}else{3129$from->{'href'}[$i] =undef;3130}3131}3132}else{3133# ordinary (not combined) diff3134$from->{'file'} =$diffinfo->{'from_file'};3135if($diffinfo->{'status'}ne"A") {# not new (added) file3136$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3137 hash=>$diffinfo->{'from_id'},3138 file_name=>$from->{'file'});3139}else{3140delete$from->{'href'};3141}3142}31433144$to->{'file'} =$diffinfo->{'to_file'};3145if(!is_deleted($diffinfo)) {# file exists in result3146$to->{'href'} = href(action=>"blob", hash_base=>$hash,3147 hash=>$diffinfo->{'to_id'},3148 file_name=>$to->{'file'});3149}else{3150delete$to->{'href'};3151}3152}31533154## ......................................................................3155## parse to array of hashes functions31563157sub git_get_heads_list {3158my$limit=shift;3159my@headslist;31603161open my$fd,'-|', git_cmd(),'for-each-ref',3162($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3163'--format=%(objectname) %(refname) %(subject)%00%(committer)',3164'refs/heads'3165orreturn;3166while(my$line= <$fd>) {3167my%ref_item;31683169chomp$line;3170my($refinfo,$committerinfo) =split(/\0/,$line);3171my($hash,$name,$title) =split(' ',$refinfo,3);3172my($committer,$epoch,$tz) =3173($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3174$ref_item{'fullname'} =$name;3175$name=~s!^refs/heads/!!;31763177$ref_item{'name'} =$name;3178$ref_item{'id'} =$hash;3179$ref_item{'title'} =$title||'(no commit message)';3180$ref_item{'epoch'} =$epoch;3181if($epoch) {3182$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3183}else{3184$ref_item{'age'} ="unknown";3185}31863187push@headslist, \%ref_item;3188}3189close$fd;31903191returnwantarray?@headslist: \@headslist;3192}31933194sub git_get_tags_list {3195my$limit=shift;3196my@tagslist;31973198open my$fd,'-|', git_cmd(),'for-each-ref',3199($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3200'--format=%(objectname) %(objecttype) %(refname) '.3201'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3202'refs/tags'3203orreturn;3204while(my$line= <$fd>) {3205my%ref_item;32063207chomp$line;3208my($refinfo,$creatorinfo) =split(/\0/,$line);3209my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3210my($creator,$epoch,$tz) =3211($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3212$ref_item{'fullname'} =$name;3213$name=~s!^refs/tags/!!;32143215$ref_item{'type'} =$type;3216$ref_item{'id'} =$id;3217$ref_item{'name'} =$name;3218if($typeeq"tag") {3219$ref_item{'subject'} =$title;3220$ref_item{'reftype'} =$reftype;3221$ref_item{'refid'} =$refid;3222}else{3223$ref_item{'reftype'} =$type;3224$ref_item{'refid'} =$id;3225}32263227if($typeeq"tag"||$typeeq"commit") {3228$ref_item{'epoch'} =$epoch;3229if($epoch) {3230$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3231}else{3232$ref_item{'age'} ="unknown";3233}3234}32353236push@tagslist, \%ref_item;3237}3238close$fd;32393240returnwantarray?@tagslist: \@tagslist;3241}32423243## ----------------------------------------------------------------------3244## filesystem-related functions32453246sub get_file_owner {3247my$path=shift;32483249my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3250my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3251if(!defined$gcos) {3252returnundef;3253}3254my$owner=$gcos;3255$owner=~s/[,;].*$//;3256return to_utf8($owner);3257}32583259# assume that file exists3260sub insert_file {3261my$filename=shift;32623263open my$fd,'<',$filename;3264print map{ to_utf8($_) } <$fd>;3265close$fd;3266}32673268## ......................................................................3269## mimetype related functions32703271sub mimetype_guess_file {3272my$filename=shift;3273my$mimemap=shift;3274-r $mimemaporreturnundef;32753276my%mimemap;3277open(my$mh,'<',$mimemap)orreturnundef;3278while(<$mh>) {3279next ifm/^#/;# skip comments3280my($mimetype,$exts) =split(/\t+/);3281if(defined$exts) {3282my@exts=split(/\s+/,$exts);3283foreachmy$ext(@exts) {3284$mimemap{$ext} =$mimetype;3285}3286}3287}3288close($mh);32893290$filename=~/\.([^.]*)$/;3291return$mimemap{$1};3292}32933294sub mimetype_guess {3295my$filename=shift;3296my$mime;3297$filename=~/\./orreturnundef;32983299if($mimetypes_file) {3300my$file=$mimetypes_file;3301if($file!~m!^/!) {# if it is relative path3302# it is relative to project3303$file="$projectroot/$project/$file";3304}3305$mime= mimetype_guess_file($filename,$file);3306}3307$mime||= mimetype_guess_file($filename,'/etc/mime.types');3308return$mime;3309}33103311sub blob_mimetype {3312my$fd=shift;3313my$filename=shift;33143315if($filename) {3316my$mime= mimetype_guess($filename);3317$mimeandreturn$mime;3318}33193320# just in case3321return$default_blob_plain_mimetypeunless$fd;33223323if(-T $fd) {3324return'text/plain';3325}elsif(!$filename) {3326return'application/octet-stream';3327}elsif($filename=~m/\.png$/i) {3328return'image/png';3329}elsif($filename=~m/\.gif$/i) {3330return'image/gif';3331}elsif($filename=~m/\.jpe?g$/i) {3332return'image/jpeg';3333}else{3334return'application/octet-stream';3335}3336}33373338sub blob_contenttype {3339my($fd,$file_name,$type) =@_;33403341$type||= blob_mimetype($fd,$file_name);3342if($typeeq'text/plain'&&defined$default_text_plain_charset) {3343$type.="; charset=$default_text_plain_charset";3344}33453346return$type;3347}33483349# guess file syntax for syntax highlighting; return undef if no highlighting3350# the name of syntax can (in the future) depend on syntax highlighter used3351sub guess_file_syntax {3352my($highlight,$mimetype,$file_name) =@_;3353returnundefunless($highlight&&defined$file_name);3354my$basename= basename($file_name,'.in');3355return$highlight_basename{$basename}3356ifexists$highlight_basename{$basename};33573358$basename=~/\.([^.]*)$/;3359my$ext=$1orreturnundef;3360return$highlight_ext{$ext}3361ifexists$highlight_ext{$ext};33623363returnundef;3364}33653366# run highlighter and return FD of its output,3367# or return original FD if no highlighting3368sub run_highlighter {3369my($fd,$highlight,$syntax) =@_;3370return$fdunless($highlight&&defined$syntax);33713372close$fd3373or die_error(404,"Reading blob failed");3374open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3375 quote_command($highlight_bin).3376" --xhtml --fragment --syntax$syntax|"3377or die_error(500,"Couldn't open file or run syntax highlighter");3378return$fd;3379}33803381## ======================================================================3382## functions printing HTML: header, footer, error page33833384sub get_page_title {3385my$title= to_utf8($site_name);33863387return$titleunless(defined$project);3388$title.=" - ". to_utf8($project);33893390return$titleunless(defined$action);3391$title.="/$action";# $action is US-ASCII (7bit ASCII)33923393return$titleunless(defined$file_name);3394$title.=" - ". esc_path($file_name);3395if($actioneq"tree"&&$file_name!~ m|/$|) {3396$title.="/";3397}33983399return$title;3400}34013402sub git_header_html {3403my$status=shift||"200 OK";3404my$expires=shift;3405my%opts=@_;34063407my$title= get_page_title();3408my$content_type;3409# require explicit support from the UA if we are to send the page as3410# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3411# we have to do this because MSIE sometimes globs '*/*', pretending to3412# support xhtml+xml but choking when it gets what it asked for.3413if(defined$cgi->http('HTTP_ACCEPT') &&3414$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3415$cgi->Accept('application/xhtml+xml') !=0) {3416$content_type='application/xhtml+xml';3417}else{3418$content_type='text/html';3419}3420print$cgi->header(-type=>$content_type, -charset =>'utf-8',3421-status=>$status, -expires =>$expires)3422unless($opts{'-no_http_header'});3423my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3424print<<EOF;3425<?xml version="1.0" encoding="utf-8"?>3426<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3427<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3428<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3429<!-- git core binaries version$git_version-->3430<head>3431<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3432<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3433<meta name="robots" content="index, nofollow"/>3434<title>$title</title>3435EOF3436# the stylesheet, favicon etc urls won't work correctly with path_info3437# unless we set the appropriate base URL3438if($ENV{'PATH_INFO'}) {3439print"<base href=\"".esc_url($base_url)."\"/>\n";3440}3441# print out each stylesheet that exist, providing backwards capability3442# for those people who defined $stylesheet in a config file3443if(defined$stylesheet) {3444print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3445}else{3446foreachmy$stylesheet(@stylesheets) {3447next unless$stylesheet;3448print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3449}3450}3451if(defined$project) {3452my%href_params= get_feed_info();3453if(!exists$href_params{'-title'}) {3454$href_params{'-title'} ='log';3455}34563457foreachmy$formatqw(RSS Atom){3458my$type=lc($format);3459my%link_attr= (3460'-rel'=>'alternate',3461'-title'=>"$project-$href_params{'-title'} -$formatfeed",3462'-type'=>"application/$type+xml"3463);34643465$href_params{'action'} =$type;3466$link_attr{'-href'} = href(%href_params);3467print"<link ".3468"rel=\"$link_attr{'-rel'}\"".3469"title=\"$link_attr{'-title'}\"".3470"href=\"$link_attr{'-href'}\"".3471"type=\"$link_attr{'-type'}\"".3472"/>\n";34733474$href_params{'extra_options'} ='--no-merges';3475$link_attr{'-href'} = href(%href_params);3476$link_attr{'-title'} .=' (no merges)';3477print"<link ".3478"rel=\"$link_attr{'-rel'}\"".3479"title=\"$link_attr{'-title'}\"".3480"href=\"$link_attr{'-href'}\"".3481"type=\"$link_attr{'-type'}\"".3482"/>\n";3483}34843485}else{3486printf('<link rel="alternate" title="%sprojects list" '.3487'href="%s" type="text/plain; charset=utf-8" />'."\n",3488$site_name, href(project=>undef, action=>"project_index"));3489printf('<link rel="alternate" title="%sprojects feeds" '.3490'href="%s" type="text/x-opml" />'."\n",3491$site_name, href(project=>undef, action=>"opml"));3492}3493if(defined$favicon) {3494printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3495}34963497print"</head>\n".3498"<body>\n";34993500if(defined$site_header&& -f $site_header) {3501 insert_file($site_header);3502}35033504print"<div class=\"page_header\">\n".3505$cgi->a({-href => esc_url($logo_url),3506-title =>$logo_label},3507qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3508print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3509if(defined$project) {3510print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3511if(defined$action) {3512print" /$action";3513}3514print"\n";3515}3516print"</div>\n";35173518my$have_search= gitweb_check_feature('search');3519if(defined$project&&$have_search) {3520if(!defined$searchtext) {3521$searchtext="";3522}3523my$search_hash;3524if(defined$hash_base) {3525$search_hash=$hash_base;3526}elsif(defined$hash) {3527$search_hash=$hash;3528}else{3529$search_hash="HEAD";3530}3531my$action=$my_uri;3532my$use_pathinfo= gitweb_check_feature('pathinfo');3533if($use_pathinfo) {3534$action.="/".esc_url($project);3535}3536print$cgi->startform(-method=>"get", -action =>$action) .3537"<div class=\"search\">\n".3538(!$use_pathinfo&&3539$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3540$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3541$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3542$cgi->popup_menu(-name =>'st', -default=>'commit',3543-values=> ['commit','grep','author','committer','pickaxe']) .3544$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3545" search:\n",3546$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3547"<span title=\"Extended regular expression\">".3548$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3549-checked =>$search_use_regexp) .3550"</span>".3551"</div>".3552$cgi->end_form() ."\n";3553}3554}35553556sub git_footer_html {3557my$feed_class='rss_logo';35583559print"<div class=\"page_footer\">\n";3560if(defined$project) {3561my$descr= git_get_project_description($project);3562if(defined$descr) {3563print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3564}35653566my%href_params= get_feed_info();3567if(!%href_params) {3568$feed_class.=' generic';3569}3570$href_params{'-title'} ||='log';35713572foreachmy$formatqw(RSS Atom){3573$href_params{'action'} =lc($format);3574print$cgi->a({-href => href(%href_params),3575-title =>"$href_params{'-title'}$formatfeed",3576-class=>$feed_class},$format)."\n";3577}35783579}else{3580print$cgi->a({-href => href(project=>undef, action=>"opml"),3581-class=>$feed_class},"OPML") ." ";3582print$cgi->a({-href => href(project=>undef, action=>"project_index"),3583-class=>$feed_class},"TXT") ."\n";3584}3585print"</div>\n";# class="page_footer"35863587if(defined$t0&& gitweb_check_feature('timed')) {3588print"<div id=\"generating_info\">\n";3589print'This page took '.3590'<span id="generating_time" class="time_span">'.3591 tv_interval($t0, [ gettimeofday() ]).3592' seconds </span>'.3593' and '.3594'<span id="generating_cmd">'.3595$number_of_git_cmds.3596'</span> git commands '.3597" to generate.\n";3598print"</div>\n";# class="page_footer"3599}36003601if(defined$site_footer&& -f $site_footer) {3602 insert_file($site_footer);3603}36043605print qq!<script type="text/javascript" src="$javascript"></script>\n!;3606if(defined$action&&3607$actioneq'blame_incremental') {3608print qq!<script type="text/javascript">\n!.3609 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3610 qq!"!. href() .qq!");\n!.3611 qq!</script>\n!;3612}elsif(gitweb_check_feature('javascript-actions')) {3613print qq!<script type="text/javascript">\n!.3614 qq!window.onload = fixLinks;\n!.3615 qq!</script>\n!;3616}36173618print"</body>\n".3619"</html>";3620}36213622# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3623# Example: die_error(404, 'Hash not found')3624# By convention, use the following status codes (as defined in RFC 2616):3625# 400: Invalid or missing CGI parameters, or3626# requested object exists but has wrong type.3627# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3628# this server or project.3629# 404: Requested object/revision/project doesn't exist.3630# 500: The server isn't configured properly, or3631# an internal error occurred (e.g. failed assertions caused by bugs), or3632# an unknown error occurred (e.g. the git binary died unexpectedly).3633# 503: The server is currently unavailable (because it is overloaded,3634# or down for maintenance). Generally, this is a temporary state.3635sub die_error {3636my$status=shift||500;3637my$error= esc_html(shift) ||"Internal Server Error";3638my$extra=shift;3639my%opts=@_;36403641my%http_responses= (3642400=>'400 Bad Request',3643403=>'403 Forbidden',3644404=>'404 Not Found',3645500=>'500 Internal Server Error',3646503=>'503 Service Unavailable',3647);3648 git_header_html($http_responses{$status},undef,%opts);3649print<<EOF;3650<div class="page_body">3651<br /><br />3652$status-$error3653<br />3654EOF3655if(defined$extra) {3656print"<hr />\n".3657"$extra\n";3658}3659print"</div>\n";36603661 git_footer_html();3662goto DONE_GITWEB3663unless($opts{'-error_handler'});3664}36653666## ----------------------------------------------------------------------3667## functions printing or outputting HTML: navigation36683669sub git_print_page_nav {3670my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3671$extra=''if!defined$extra;# pager or formats36723673my@navs=qw(summary shortlog log commit commitdiff tree);3674if($suppress) {3675@navs=grep{$_ne$suppress}@navs;3676}36773678my%arg=map{$_=> {action=>$_} }@navs;3679if(defined$head) {3680for(qw(commit commitdiff)) {3681$arg{$_}{'hash'} =$head;3682}3683if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3684for(qw(shortlog log)) {3685$arg{$_}{'hash'} =$head;3686}3687}3688}36893690$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3691$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;36923693my@actions= gitweb_get_feature('actions');3694my%repl= (3695'%'=>'%',3696'n'=>$project,# project name3697'f'=>$git_dir,# project path within filesystem3698'h'=>$treehead||'',# current hash ('h' parameter)3699'b'=>$treebase||'',# hash base ('hb' parameter)3700);3701while(@actions) {3702my($label,$link,$pos) =splice(@actions,0,3);3703# insert3704@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3705# munch munch3706$link=~s/%([%nfhb])/$repl{$1}/g;3707$arg{$label}{'_href'} =$link;3708}37093710print"<div class=\"page_nav\">\n".3711(join" | ",3712map{$_eq$current?3713$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3714}@navs);3715print"<br/>\n$extra<br/>\n".3716"</div>\n";3717}37183719sub format_paging_nav {3720my($action,$page,$has_next_link) =@_;3721my$paging_nav;372237233724if($page>0) {3725$paging_nav.=3726$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3727" ⋅ ".3728$cgi->a({-href => href(-replay=>1, page=>$page-1),3729-accesskey =>"p", -title =>"Alt-p"},"prev");3730}else{3731$paging_nav.="first ⋅ prev";3732}37333734if($has_next_link) {3735$paging_nav.=" ⋅ ".3736$cgi->a({-href => href(-replay=>1, page=>$page+1),3737-accesskey =>"n", -title =>"Alt-n"},"next");3738}else{3739$paging_nav.=" ⋅ next";3740}37413742return$paging_nav;3743}37443745## ......................................................................3746## functions printing or outputting HTML: div37473748sub git_print_header_div {3749my($action,$title,$hash,$hash_base) =@_;3750my%args= ();37513752$args{'action'} =$action;3753$args{'hash'} =$hashif$hash;3754$args{'hash_base'} =$hash_baseif$hash_base;37553756print"<div class=\"header\">\n".3757$cgi->a({-href => href(%args), -class=>"title"},3758$title?$title:$action) .3759"\n</div>\n";3760}37613762sub print_local_time {3763print format_local_time(@_);3764}37653766sub format_local_time {3767my$localtime='';3768my%date=@_;3769if($date{'hour_local'} <6) {3770$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3771$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3772}else{3773$localtime.=sprintf(" (%02d:%02d%s)",3774$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3775}37763777return$localtime;3778}37793780# Outputs the author name and date in long form3781sub git_print_authorship {3782my$co=shift;3783my%opts=@_;3784my$tag=$opts{-tag} ||'div';3785my$author=$co->{'author_name'};37863787my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3788print"<$tagclass=\"author_date\">".3789 format_search_author($author,"author", esc_html($author)) .3790" [$ad{'rfc2822'}";3791 print_local_time(%ad)if($opts{-localtime});3792print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3793."</$tag>\n";3794}37953796# Outputs table rows containing the full author or committer information,3797# in the format expected for 'commit' view (& similar).3798# Parameters are a commit hash reference, followed by the list of people3799# to output information for. If the list is empty it defaults to both3800# author and committer.3801sub git_print_authorship_rows {3802my$co=shift;3803# too bad we can't use @people = @_ || ('author', 'committer')3804my@people=@_;3805@people= ('author','committer')unless@people;3806foreachmy$who(@people) {3807my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3808print"<tr><td>$who</td><td>".3809 format_search_author($co->{"${who}_name"},$who,3810 esc_html($co->{"${who}_name"})) ." ".3811 format_search_author($co->{"${who}_email"},$who,3812 esc_html("<".$co->{"${who}_email"} .">")) .3813"</td><td rowspan=\"2\">".3814 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3815"</td></tr>\n".3816"<tr>".3817"<td></td><td>$wd{'rfc2822'}";3818 print_local_time(%wd);3819print"</td>".3820"</tr>\n";3821}3822}38233824sub git_print_page_path {3825my$name=shift;3826my$type=shift;3827my$hb=shift;382838293830print"<div class=\"page_path\">";3831print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3832-title =>'tree root'}, to_utf8("[$project]"));3833print" / ";3834if(defined$name) {3835my@dirname=split'/',$name;3836my$basename=pop@dirname;3837my$fullname='';38383839foreachmy$dir(@dirname) {3840$fullname.= ($fullname?'/':'') .$dir;3841print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3842 hash_base=>$hb),3843-title =>$fullname}, esc_path($dir));3844print" / ";3845}3846if(defined$type&&$typeeq'blob') {3847print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3848 hash_base=>$hb),3849-title =>$name}, esc_path($basename));3850}elsif(defined$type&&$typeeq'tree') {3851print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3852 hash_base=>$hb),3853-title =>$name}, esc_path($basename));3854print" / ";3855}else{3856print esc_path($basename);3857}3858}3859print"<br/></div>\n";3860}38613862sub git_print_log {3863my$log=shift;3864my%opts=@_;38653866if($opts{'-remove_title'}) {3867# remove title, i.e. first line of log3868shift@$log;3869}3870# remove leading empty lines3871while(defined$log->[0] &&$log->[0]eq"") {3872shift@$log;3873}38743875# print log3876my$signoff=0;3877my$empty=0;3878foreachmy$line(@$log) {3879if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3880$signoff=1;3881$empty=0;3882if(!$opts{'-remove_signoff'}) {3883print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3884next;3885}else{3886# remove signoff lines3887next;3888}3889}else{3890$signoff=0;3891}38923893# print only one empty line3894# do not print empty line after signoff3895if($lineeq"") {3896next if($empty||$signoff);3897$empty=1;3898}else{3899$empty=0;3900}39013902print format_log_line_html($line) ."<br/>\n";3903}39043905if($opts{'-final_empty_line'}) {3906# end with single empty line3907print"<br/>\n"unless$empty;3908}3909}39103911# return link target (what link points to)3912sub git_get_link_target {3913my$hash=shift;3914my$link_target;39153916# read link3917open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3918orreturn;3919{3920local$/=undef;3921$link_target= <$fd>;3922}3923close$fd3924orreturn;39253926return$link_target;3927}39283929# given link target, and the directory (basedir) the link is in,3930# return target of link relative to top directory (top tree);3931# return undef if it is not possible (including absolute links).3932sub normalize_link_target {3933my($link_target,$basedir) =@_;39343935# absolute symlinks (beginning with '/') cannot be normalized3936return if(substr($link_target,0,1)eq'/');39373938# normalize link target to path from top (root) tree (dir)3939my$path;3940if($basedir) {3941$path=$basedir.'/'.$link_target;3942}else{3943# we are in top (root) tree (dir)3944$path=$link_target;3945}39463947# remove //, /./, and /../3948my@path_parts;3949foreachmy$part(split('/',$path)) {3950# discard '.' and ''3951next if(!$part||$parteq'.');3952# handle '..'3953if($parteq'..') {3954if(@path_parts) {3955pop@path_parts;3956}else{3957# link leads outside repository (outside top dir)3958return;3959}3960}else{3961push@path_parts,$part;3962}3963}3964$path=join('/',@path_parts);39653966return$path;3967}39683969# print tree entry (row of git_tree), but without encompassing <tr> element3970sub git_print_tree_entry {3971my($t,$basedir,$hash_base,$have_blame) =@_;39723973my%base_key= ();3974$base_key{'hash_base'} =$hash_baseifdefined$hash_base;39753976# The format of a table row is: mode list link. Where mode is3977# the mode of the entry, list is the name of the entry, an href,3978# and link is the action links of the entry.39793980print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3981if(exists$t->{'size'}) {3982print"<td class=\"size\">$t->{'size'}</td>\n";3983}3984if($t->{'type'}eq"blob") {3985print"<td class=\"list\">".3986$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3987 file_name=>"$basedir$t->{'name'}",%base_key),3988-class=>"list"}, esc_path($t->{'name'}));3989if(S_ISLNK(oct$t->{'mode'})) {3990my$link_target= git_get_link_target($t->{'hash'});3991if($link_target) {3992my$norm_target= normalize_link_target($link_target,$basedir);3993if(defined$norm_target) {3994print" -> ".3995$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3996 file_name=>$norm_target),3997-title =>$norm_target}, esc_path($link_target));3998}else{3999print" -> ". esc_path($link_target);4000}4001}4002}4003print"</td>\n";4004print"<td class=\"link\">";4005print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4006 file_name=>"$basedir$t->{'name'}",%base_key)},4007"blob");4008if($have_blame) {4009print" | ".4010$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4011 file_name=>"$basedir$t->{'name'}",%base_key)},4012"blame");4013}4014if(defined$hash_base) {4015print" | ".4016$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4017 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4018"history");4019}4020print" | ".4021$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4022 file_name=>"$basedir$t->{'name'}")},4023"raw");4024print"</td>\n";40254026}elsif($t->{'type'}eq"tree") {4027print"<td class=\"list\">";4028print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4029 file_name=>"$basedir$t->{'name'}",4030%base_key)},4031 esc_path($t->{'name'}));4032print"</td>\n";4033print"<td class=\"link\">";4034print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4035 file_name=>"$basedir$t->{'name'}",4036%base_key)},4037"tree");4038if(defined$hash_base) {4039print" | ".4040$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4041 file_name=>"$basedir$t->{'name'}")},4042"history");4043}4044print"</td>\n";4045}else{4046# unknown object: we can only present history for it4047# (this includes 'commit' object, i.e. submodule support)4048print"<td class=\"list\">".4049 esc_path($t->{'name'}) .4050"</td>\n";4051print"<td class=\"link\">";4052if(defined$hash_base) {4053print$cgi->a({-href => href(action=>"history",4054 hash_base=>$hash_base,4055 file_name=>"$basedir$t->{'name'}")},4056"history");4057}4058print"</td>\n";4059}4060}40614062## ......................................................................4063## functions printing large fragments of HTML40644065# get pre-image filenames for merge (combined) diff4066sub fill_from_file_info {4067my($diff,@parents) =@_;40684069$diff->{'from_file'} = [ ];4070$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4071for(my$i=0;$i<$diff->{'nparents'};$i++) {4072if($diff->{'status'}[$i]eq'R'||4073$diff->{'status'}[$i]eq'C') {4074$diff->{'from_file'}[$i] =4075 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4076}4077}40784079return$diff;4080}40814082# is current raw difftree line of file deletion4083sub is_deleted {4084my$diffinfo=shift;40854086return$diffinfo->{'to_id'}eq('0' x 40);4087}40884089# does patch correspond to [previous] difftree raw line4090# $diffinfo - hashref of parsed raw diff format4091# $patchinfo - hashref of parsed patch diff format4092# (the same keys as in $diffinfo)4093sub is_patch_split {4094my($diffinfo,$patchinfo) =@_;40954096returndefined$diffinfo&&defined$patchinfo4097&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4098}409941004101sub git_difftree_body {4102my($difftree,$hash,@parents) =@_;4103my($parent) =$parents[0];4104my$have_blame= gitweb_check_feature('blame');4105print"<div class=\"list_head\">\n";4106if($#{$difftree} >10) {4107print(($#{$difftree} +1) ." files changed:\n");4108}4109print"</div>\n";41104111print"<table class=\"".4112(@parents>1?"combined ":"") .4113"diff_tree\">\n";41144115# header only for combined diff in 'commitdiff' view4116my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4117if($has_header) {4118# table header4119print"<thead><tr>\n".4120"<th></th><th></th>\n";# filename, patchN link4121for(my$i=0;$i<@parents;$i++) {4122my$par=$parents[$i];4123print"<th>".4124$cgi->a({-href => href(action=>"commitdiff",4125 hash=>$hash, hash_parent=>$par),4126-title =>'commitdiff to parent number '.4127($i+1) .': '.substr($par,0,7)},4128$i+1) .4129" </th>\n";4130}4131print"</tr></thead>\n<tbody>\n";4132}41334134my$alternate=1;4135my$patchno=0;4136foreachmy$line(@{$difftree}) {4137my$diff= parsed_difftree_line($line);41384139if($alternate) {4140print"<tr class=\"dark\">\n";4141}else{4142print"<tr class=\"light\">\n";4143}4144$alternate^=1;41454146if(exists$diff->{'nparents'}) {# combined diff41474148 fill_from_file_info($diff,@parents)4149unlessexists$diff->{'from_file'};41504151if(!is_deleted($diff)) {4152# file exists in the result (child) commit4153print"<td>".4154$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4155 file_name=>$diff->{'to_file'},4156 hash_base=>$hash),4157-class=>"list"}, esc_path($diff->{'to_file'})) .4158"</td>\n";4159}else{4160print"<td>".4161 esc_path($diff->{'to_file'}) .4162"</td>\n";4163}41644165if($actioneq'commitdiff') {4166# link to patch4167$patchno++;4168print"<td class=\"link\">".4169$cgi->a({-href =>"#patch$patchno"},"patch") .4170" | ".4171"</td>\n";4172}41734174my$has_history=0;4175my$not_deleted=0;4176for(my$i=0;$i<$diff->{'nparents'};$i++) {4177my$hash_parent=$parents[$i];4178my$from_hash=$diff->{'from_id'}[$i];4179my$from_path=$diff->{'from_file'}[$i];4180my$status=$diff->{'status'}[$i];41814182$has_history||= ($statusne'A');4183$not_deleted||= ($statusne'D');41844185if($statuseq'A') {4186print"<td class=\"link\"align=\"right\"> | </td>\n";4187}elsif($statuseq'D') {4188print"<td class=\"link\">".4189$cgi->a({-href => href(action=>"blob",4190 hash_base=>$hash,4191 hash=>$from_hash,4192 file_name=>$from_path)},4193"blob". ($i+1)) .4194" | </td>\n";4195}else{4196if($diff->{'to_id'}eq$from_hash) {4197print"<td class=\"link nochange\">";4198}else{4199print"<td class=\"link\">";4200}4201print$cgi->a({-href => href(action=>"blobdiff",4202 hash=>$diff->{'to_id'},4203 hash_parent=>$from_hash,4204 hash_base=>$hash,4205 hash_parent_base=>$hash_parent,4206 file_name=>$diff->{'to_file'},4207 file_parent=>$from_path)},4208"diff". ($i+1)) .4209" | </td>\n";4210}4211}42124213print"<td class=\"link\">";4214if($not_deleted) {4215print$cgi->a({-href => href(action=>"blob",4216 hash=>$diff->{'to_id'},4217 file_name=>$diff->{'to_file'},4218 hash_base=>$hash)},4219"blob");4220print" | "if($has_history);4221}4222if($has_history) {4223print$cgi->a({-href => href(action=>"history",4224 file_name=>$diff->{'to_file'},4225 hash_base=>$hash)},4226"history");4227}4228print"</td>\n";42294230print"</tr>\n";4231next;# instead of 'else' clause, to avoid extra indent4232}4233# else ordinary diff42344235my($to_mode_oct,$to_mode_str,$to_file_type);4236my($from_mode_oct,$from_mode_str,$from_file_type);4237if($diff->{'to_mode'}ne('0' x 6)) {4238$to_mode_oct=oct$diff->{'to_mode'};4239if(S_ISREG($to_mode_oct)) {# only for regular file4240$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4241}4242$to_file_type= file_type($diff->{'to_mode'});4243}4244if($diff->{'from_mode'}ne('0' x 6)) {4245$from_mode_oct=oct$diff->{'from_mode'};4246if(S_ISREG($to_mode_oct)) {# only for regular file4247$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4248}4249$from_file_type= file_type($diff->{'from_mode'});4250}42514252if($diff->{'status'}eq"A") {# created4253my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4254$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4255$mode_chng.="]</span>";4256print"<td>";4257print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4258 hash_base=>$hash, file_name=>$diff->{'file'}),4259-class=>"list"}, esc_path($diff->{'file'}));4260print"</td>\n";4261print"<td>$mode_chng</td>\n";4262print"<td class=\"link\">";4263if($actioneq'commitdiff') {4264# link to patch4265$patchno++;4266print$cgi->a({-href =>"#patch$patchno"},"patch");4267print" | ";4268}4269print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4270 hash_base=>$hash, file_name=>$diff->{'file'})},4271"blob");4272print"</td>\n";42734274}elsif($diff->{'status'}eq"D") {# deleted4275my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4276print"<td>";4277print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4278 hash_base=>$parent, file_name=>$diff->{'file'}),4279-class=>"list"}, esc_path($diff->{'file'}));4280print"</td>\n";4281print"<td>$mode_chng</td>\n";4282print"<td class=\"link\">";4283if($actioneq'commitdiff') {4284# link to patch4285$patchno++;4286print$cgi->a({-href =>"#patch$patchno"},"patch");4287print" | ";4288}4289print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4290 hash_base=>$parent, file_name=>$diff->{'file'})},4291"blob") ." | ";4292if($have_blame) {4293print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4294 file_name=>$diff->{'file'})},4295"blame") ." | ";4296}4297print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4298 file_name=>$diff->{'file'})},4299"history");4300print"</td>\n";43014302}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4303my$mode_chnge="";4304if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4305$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4306if($from_file_typene$to_file_type) {4307$mode_chnge.=" from$from_file_typeto$to_file_type";4308}4309if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4310if($from_mode_str&&$to_mode_str) {4311$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4312}elsif($to_mode_str) {4313$mode_chnge.=" mode:$to_mode_str";4314}4315}4316$mode_chnge.="]</span>\n";4317}4318print"<td>";4319print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4320 hash_base=>$hash, file_name=>$diff->{'file'}),4321-class=>"list"}, esc_path($diff->{'file'}));4322print"</td>\n";4323print"<td>$mode_chnge</td>\n";4324print"<td class=\"link\">";4325if($actioneq'commitdiff') {4326# link to patch4327$patchno++;4328print$cgi->a({-href =>"#patch$patchno"},"patch") .4329" | ";4330}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4331# "commit" view and modified file (not onlu mode changed)4332print$cgi->a({-href => href(action=>"blobdiff",4333 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4334 hash_base=>$hash, hash_parent_base=>$parent,4335 file_name=>$diff->{'file'})},4336"diff") .4337" | ";4338}4339print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4340 hash_base=>$hash, file_name=>$diff->{'file'})},4341"blob") ." | ";4342if($have_blame) {4343print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4344 file_name=>$diff->{'file'})},4345"blame") ." | ";4346}4347print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4348 file_name=>$diff->{'file'})},4349"history");4350print"</td>\n";43514352}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4353my%status_name= ('R'=>'moved','C'=>'copied');4354my$nstatus=$status_name{$diff->{'status'}};4355my$mode_chng="";4356if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4357# mode also for directories, so we cannot use $to_mode_str4358$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4359}4360print"<td>".4361$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4362 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4363-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4364"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4365$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4366 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4367-class=>"list"}, esc_path($diff->{'from_file'})) .4368" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4369"<td class=\"link\">";4370if($actioneq'commitdiff') {4371# link to patch4372$patchno++;4373print$cgi->a({-href =>"#patch$patchno"},"patch") .4374" | ";4375}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4376# "commit" view and modified file (not only pure rename or copy)4377print$cgi->a({-href => href(action=>"blobdiff",4378 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4379 hash_base=>$hash, hash_parent_base=>$parent,4380 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4381"diff") .4382" | ";4383}4384print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4385 hash_base=>$parent, file_name=>$diff->{'to_file'})},4386"blob") ." | ";4387if($have_blame) {4388print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4389 file_name=>$diff->{'to_file'})},4390"blame") ." | ";4391}4392print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4393 file_name=>$diff->{'to_file'})},4394"history");4395print"</td>\n";43964397}# we should not encounter Unmerged (U) or Unknown (X) status4398print"</tr>\n";4399}4400print"</tbody>"if$has_header;4401print"</table>\n";4402}44034404sub git_patchset_body {4405my($fd,$difftree,$hash,@hash_parents) =@_;4406my($hash_parent) =$hash_parents[0];44074408my$is_combined= (@hash_parents>1);4409my$patch_idx=0;4410my$patch_number=0;4411my$patch_line;4412my$diffinfo;4413my$to_name;4414my(%from,%to);44154416print"<div class=\"patchset\">\n";44174418# skip to first patch4419while($patch_line= <$fd>) {4420chomp$patch_line;44214422last if($patch_line=~m/^diff /);4423}44244425 PATCH:4426while($patch_line) {44274428# parse "git diff" header line4429if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4430# $1 is from_name, which we do not use4431$to_name= unquote($2);4432$to_name=~s!^b/!!;4433}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4434# $1 is 'cc' or 'combined', which we do not use4435$to_name= unquote($2);4436}else{4437$to_name=undef;4438}44394440# check if current patch belong to current raw line4441# and parse raw git-diff line if needed4442if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4443# this is continuation of a split patch4444print"<div class=\"patch cont\">\n";4445}else{4446# advance raw git-diff output if needed4447$patch_idx++ifdefined$diffinfo;44484449# read and prepare patch information4450$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);44514452# compact combined diff output can have some patches skipped4453# find which patch (using pathname of result) we are at now;4454if($is_combined) {4455while($to_namene$diffinfo->{'to_file'}) {4456print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4457 format_diff_cc_simplified($diffinfo,@hash_parents) .4458"</div>\n";# class="patch"44594460$patch_idx++;4461$patch_number++;44624463last if$patch_idx>$#$difftree;4464$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4465}4466}44674468# modifies %from, %to hashes4469 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);44704471# this is first patch for raw difftree line with $patch_idx index4472# we index @$difftree array from 0, but number patches from 14473print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4474}44754476# git diff header4477#assert($patch_line =~ m/^diff /) if DEBUG;4478#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4479$patch_number++;4480# print "git diff" header4481print format_git_diff_header_line($patch_line,$diffinfo,4482 \%from, \%to);44834484# print extended diff header4485print"<div class=\"diff extended_header\">\n";4486 EXTENDED_HEADER:4487while($patch_line= <$fd>) {4488chomp$patch_line;44894490last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);44914492print format_extended_diff_header_line($patch_line,$diffinfo,4493 \%from, \%to);4494}4495print"</div>\n";# class="diff extended_header"44964497# from-file/to-file diff header4498if(!$patch_line) {4499print"</div>\n";# class="patch"4500last PATCH;4501}4502next PATCH if($patch_line=~m/^diff /);4503#assert($patch_line =~ m/^---/) if DEBUG;45044505my$last_patch_line=$patch_line;4506$patch_line= <$fd>;4507chomp$patch_line;4508#assert($patch_line =~ m/^\+\+\+/) if DEBUG;45094510print format_diff_from_to_header($last_patch_line,$patch_line,4511$diffinfo, \%from, \%to,4512@hash_parents);45134514# the patch itself4515 LINE:4516while($patch_line= <$fd>) {4517chomp$patch_line;45184519next PATCH if($patch_line=~m/^diff /);45204521print format_diff_line($patch_line, \%from, \%to);4522}45234524}continue{4525print"</div>\n";# class="patch"4526}45274528# for compact combined (--cc) format, with chunk and patch simplification4529# the patchset might be empty, but there might be unprocessed raw lines4530for(++$patch_idxif$patch_number>0;4531$patch_idx<@$difftree;4532++$patch_idx) {4533# read and prepare patch information4534$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);45354536# generate anchor for "patch" links in difftree / whatchanged part4537print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4538 format_diff_cc_simplified($diffinfo,@hash_parents) .4539"</div>\n";# class="patch"45404541$patch_number++;4542}45434544if($patch_number==0) {4545if(@hash_parents>1) {4546print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4547}else{4548print"<div class=\"diff nodifferences\">No differences found</div>\n";4549}4550}45514552print"</div>\n";# class="patchset"4553}45544555# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45564557# fills project list info (age, description, owner, forks) for each4558# project in the list, removing invalid projects from returned list4559# NOTE: modifies $projlist, but does not remove entries from it4560sub fill_project_list_info {4561my($projlist,$check_forks) =@_;4562my@projects;45634564my$show_ctags= gitweb_check_feature('ctags');4565 PROJECT:4566foreachmy$pr(@$projlist) {4567my(@activity) = git_get_last_activity($pr->{'path'});4568unless(@activity) {4569next PROJECT;4570}4571($pr->{'age'},$pr->{'age_string'}) =@activity;4572if(!defined$pr->{'descr'}) {4573my$descr= git_get_project_description($pr->{'path'}) ||"";4574$descr= to_utf8($descr);4575$pr->{'descr_long'} =$descr;4576$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4577}4578if(!defined$pr->{'owner'}) {4579$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4580}4581if($check_forks) {4582my$pname=$pr->{'path'};4583if(($pname=~s/\.git$//) &&4584($pname!~/\/$/) &&4585(-d "$projectroot/$pname")) {4586$pr->{'forks'} ="-d$projectroot/$pname";4587}else{4588$pr->{'forks'} =0;4589}4590}4591$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4592push@projects,$pr;4593}45944595return@projects;4596}45974598# print 'sort by' <th> element, generating 'sort by $name' replay link4599# if that order is not selected4600sub print_sort_th {4601print format_sort_th(@_);4602}46034604sub format_sort_th {4605my($name,$order,$header) =@_;4606my$sort_th="";4607$header||=ucfirst($name);46084609if($ordereq$name) {4610$sort_th.="<th>$header</th>\n";4611}else{4612$sort_th.="<th>".4613$cgi->a({-href => href(-replay=>1, order=>$name),4614-class=>"header"},$header) .4615"</th>\n";4616}46174618return$sort_th;4619}46204621sub git_project_list_body {4622# actually uses global variable $project4623my($projlist,$order,$from,$to,$extra,$no_header) =@_;46244625my$check_forks= gitweb_check_feature('forks');4626my@projects= fill_project_list_info($projlist,$check_forks);46274628$order||=$default_projects_order;4629$from=0unlessdefined$from;4630$to=$#projectsif(!defined$to||$#projects<$to);46314632my%order_info= (4633 project => { key =>'path', type =>'str'},4634 descr => { key =>'descr_long', type =>'str'},4635 owner => { key =>'owner', type =>'str'},4636 age => { key =>'age', type =>'num'}4637);4638my$oi=$order_info{$order};4639if($oi->{'type'}eq'str') {4640@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4641}else{4642@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4643}46444645my$show_ctags= gitweb_check_feature('ctags');4646if($show_ctags) {4647my%ctags;4648foreachmy$p(@projects) {4649foreachmy$ct(keys%{$p->{'ctags'}}) {4650$ctags{$ct} +=$p->{'ctags'}->{$ct};4651}4652}4653my$cloud= git_populate_project_tagcloud(\%ctags);4654print git_show_project_tagcloud($cloud,64);4655}46564657print"<table class=\"project_list\">\n";4658unless($no_header) {4659print"<tr>\n";4660if($check_forks) {4661print"<th></th>\n";4662}4663 print_sort_th('project',$order,'Project');4664 print_sort_th('descr',$order,'Description');4665 print_sort_th('owner',$order,'Owner');4666 print_sort_th('age',$order,'Last Change');4667print"<th></th>\n".# for links4668"</tr>\n";4669}4670my$alternate=1;4671my$tagfilter=$cgi->param('by_tag');4672for(my$i=$from;$i<=$to;$i++) {4673my$pr=$projects[$i];46744675next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4676next if$searchtextand not$pr->{'path'} =~/$searchtext/4677and not$pr->{'descr_long'} =~/$searchtext/;4678# Weed out forks or non-matching entries of search4679if($check_forks) {4680my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4681$forkbase="^$forkbase"if$forkbase;4682next ifnot$searchtextand not$tagfilterand$show_ctags4683and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4684}46854686if($alternate) {4687print"<tr class=\"dark\">\n";4688}else{4689print"<tr class=\"light\">\n";4690}4691$alternate^=1;4692if($check_forks) {4693print"<td>";4694if($pr->{'forks'}) {4695print"<!--$pr->{'forks'} -->\n";4696print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4697}4698print"</td>\n";4699}4700print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4701-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4702"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4703-class=>"list", -title =>$pr->{'descr_long'}},4704 esc_html($pr->{'descr'})) ."</td>\n".4705"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4706print"<td class=\"". age_class($pr->{'age'}) ."\">".4707(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4708"<td class=\"link\">".4709$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4710$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4711$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4712$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4713($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4714"</td>\n".4715"</tr>\n";4716}4717if(defined$extra) {4718print"<tr>\n";4719if($check_forks) {4720print"<td></td>\n";4721}4722print"<td colspan=\"5\">$extra</td>\n".4723"</tr>\n";4724}4725print"</table>\n";4726}47274728sub git_log_body {4729# uses global variable $project4730my($commitlist,$from,$to,$refs,$extra) =@_;47314732$from=0unlessdefined$from;4733$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47344735for(my$i=0;$i<=$to;$i++) {4736my%co= %{$commitlist->[$i]};4737next if!%co;4738my$commit=$co{'id'};4739my$ref= format_ref_marker($refs,$commit);4740my%ad= parse_date($co{'author_epoch'});4741 git_print_header_div('commit',4742"<span class=\"age\">$co{'age_string'}</span>".4743 esc_html($co{'title'}) .$ref,4744$commit);4745print"<div class=\"title_text\">\n".4746"<div class=\"log_link\">\n".4747$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4748" | ".4749$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4750" | ".4751$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4752"<br/>\n".4753"</div>\n";4754 git_print_authorship(\%co, -tag =>'span');4755print"<br/>\n</div>\n";47564757print"<div class=\"log_body\">\n";4758 git_print_log($co{'comment'}, -final_empty_line=>1);4759print"</div>\n";4760}4761if($extra) {4762print"<div class=\"page_nav\">\n";4763print"$extra\n";4764print"</div>\n";4765}4766}47674768sub git_shortlog_body {4769# uses global variable $project4770my($commitlist,$from,$to,$refs,$extra) =@_;47714772$from=0unlessdefined$from;4773$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47744775print"<table class=\"shortlog\">\n";4776my$alternate=1;4777for(my$i=$from;$i<=$to;$i++) {4778my%co= %{$commitlist->[$i]};4779my$commit=$co{'id'};4780my$ref= format_ref_marker($refs,$commit);4781if($alternate) {4782print"<tr class=\"dark\">\n";4783}else{4784print"<tr class=\"light\">\n";4785}4786$alternate^=1;4787# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4788print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4789 format_author_html('td', \%co,10) ."<td>";4790print format_subject_html($co{'title'},$co{'title_short'},4791 href(action=>"commit", hash=>$commit),$ref);4792print"</td>\n".4793"<td class=\"link\">".4794$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4795$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4796$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4797my$snapshot_links= format_snapshot_links($commit);4798if(defined$snapshot_links) {4799print" | ".$snapshot_links;4800}4801print"</td>\n".4802"</tr>\n";4803}4804if(defined$extra) {4805print"<tr>\n".4806"<td colspan=\"4\">$extra</td>\n".4807"</tr>\n";4808}4809print"</table>\n";4810}48114812sub git_history_body {4813# Warning: assumes constant type (blob or tree) during history4814my($commitlist,$from,$to,$refs,$extra,4815$file_name,$file_hash,$ftype) =@_;48164817$from=0unlessdefined$from;4818$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});48194820print"<table class=\"history\">\n";4821my$alternate=1;4822for(my$i=$from;$i<=$to;$i++) {4823my%co= %{$commitlist->[$i]};4824if(!%co) {4825next;4826}4827my$commit=$co{'id'};48284829my$ref= format_ref_marker($refs,$commit);48304831if($alternate) {4832print"<tr class=\"dark\">\n";4833}else{4834print"<tr class=\"light\">\n";4835}4836$alternate^=1;4837print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4838# shortlog: format_author_html('td', \%co, 10)4839 format_author_html('td', \%co,15,3) ."<td>";4840# originally git_history used chop_str($co{'title'}, 50)4841print format_subject_html($co{'title'},$co{'title_short'},4842 href(action=>"commit", hash=>$commit),$ref);4843print"</td>\n".4844"<td class=\"link\">".4845$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4846$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");48474848if($ftypeeq'blob') {4849my$blob_current=$file_hash;4850my$blob_parent= git_get_hash_by_path($commit,$file_name);4851if(defined$blob_current&&defined$blob_parent&&4852$blob_currentne$blob_parent) {4853print" | ".4854$cgi->a({-href => href(action=>"blobdiff",4855 hash=>$blob_current, hash_parent=>$blob_parent,4856 hash_base=>$hash_base, hash_parent_base=>$commit,4857 file_name=>$file_name)},4858"diff to current");4859}4860}4861print"</td>\n".4862"</tr>\n";4863}4864if(defined$extra) {4865print"<tr>\n".4866"<td colspan=\"4\">$extra</td>\n".4867"</tr>\n";4868}4869print"</table>\n";4870}48714872sub git_tags_body {4873# uses global variable $project4874my($taglist,$from,$to,$extra) =@_;4875$from=0unlessdefined$from;4876$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);48774878print"<table class=\"tags\">\n";4879my$alternate=1;4880for(my$i=$from;$i<=$to;$i++) {4881my$entry=$taglist->[$i];4882my%tag=%$entry;4883my$comment=$tag{'subject'};4884my$comment_short;4885if(defined$comment) {4886$comment_short= chop_str($comment,30,5);4887}4888if($alternate) {4889print"<tr class=\"dark\">\n";4890}else{4891print"<tr class=\"light\">\n";4892}4893$alternate^=1;4894if(defined$tag{'age'}) {4895print"<td><i>$tag{'age'}</i></td>\n";4896}else{4897print"<td></td>\n";4898}4899print"<td>".4900$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4901-class=>"list name"}, esc_html($tag{'name'})) .4902"</td>\n".4903"<td>";4904if(defined$comment) {4905print format_subject_html($comment,$comment_short,4906 href(action=>"tag", hash=>$tag{'id'}));4907}4908print"</td>\n".4909"<td class=\"selflink\">";4910if($tag{'type'}eq"tag") {4911print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4912}else{4913print" ";4914}4915print"</td>\n".4916"<td class=\"link\">"." | ".4917$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4918if($tag{'reftype'}eq"commit") {4919print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4920" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4921}elsif($tag{'reftype'}eq"blob") {4922print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4923}4924print"</td>\n".4925"</tr>";4926}4927if(defined$extra) {4928print"<tr>\n".4929"<td colspan=\"5\">$extra</td>\n".4930"</tr>\n";4931}4932print"</table>\n";4933}49344935sub git_heads_body {4936# uses global variable $project4937my($headlist,$head,$from,$to,$extra) =@_;4938$from=0unlessdefined$from;4939$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);49404941print"<table class=\"heads\">\n";4942my$alternate=1;4943for(my$i=$from;$i<=$to;$i++) {4944my$entry=$headlist->[$i];4945my%ref=%$entry;4946my$curr=$ref{'id'}eq$head;4947if($alternate) {4948print"<tr class=\"dark\">\n";4949}else{4950print"<tr class=\"light\">\n";4951}4952$alternate^=1;4953print"<td><i>$ref{'age'}</i></td>\n".4954($curr?"<td class=\"current_head\">":"<td>") .4955$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4956-class=>"list name"},esc_html($ref{'name'})) .4957"</td>\n".4958"<td class=\"link\">".4959$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4960$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4961$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4962"</td>\n".4963"</tr>";4964}4965if(defined$extra) {4966print"<tr>\n".4967"<td colspan=\"3\">$extra</td>\n".4968"</tr>\n";4969}4970print"</table>\n";4971}49724973sub git_search_grep_body {4974my($commitlist,$from,$to,$extra) =@_;4975$from=0unlessdefined$from;4976$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);49774978print"<table class=\"commit_search\">\n";4979my$alternate=1;4980for(my$i=$from;$i<=$to;$i++) {4981my%co= %{$commitlist->[$i]};4982if(!%co) {4983next;4984}4985my$commit=$co{'id'};4986if($alternate) {4987print"<tr class=\"dark\">\n";4988}else{4989print"<tr class=\"light\">\n";4990}4991$alternate^=1;4992print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4993 format_author_html('td', \%co,15,5) .4994"<td>".4995$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4996-class=>"list subject"},4997 chop_and_escape_str($co{'title'},50) ."<br/>");4998my$comment=$co{'comment'};4999foreachmy$line(@$comment) {5000if($line=~m/^(.*?)($search_regexp)(.*)$/i) {5001my($lead,$match,$trail) = ($1,$2,$3);5002$match= chop_str($match,70,5,'center');5003my$contextlen=int((80-length($match))/2);5004$contextlen=30if($contextlen>30);5005$lead= chop_str($lead,$contextlen,10,'left');5006$trail= chop_str($trail,$contextlen,10,'right');50075008$lead= esc_html($lead);5009$match= esc_html($match);5010$trail= esc_html($trail);50115012print"$lead<span class=\"match\">$match</span>$trail<br />";5013}5014}5015print"</td>\n".5016"<td class=\"link\">".5017$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5018" | ".5019$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .5020" | ".5021$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5022print"</td>\n".5023"</tr>\n";5024}5025if(defined$extra) {5026print"<tr>\n".5027"<td colspan=\"3\">$extra</td>\n".5028"</tr>\n";5029}5030print"</table>\n";5031}50325033## ======================================================================5034## ======================================================================5035## actions50365037sub git_project_list {5038my$order=$input_params{'order'};5039if(defined$order&&$order!~m/none|project|descr|owner|age/) {5040 die_error(400,"Unknown order parameter");5041}50425043my@list= git_get_projects_list();5044if(!@list) {5045 die_error(404,"No projects found");5046}50475048 git_header_html();5049if(defined$home_text&& -f $home_text) {5050print"<div class=\"index_include\">\n";5051 insert_file($home_text);5052print"</div>\n";5053}5054print$cgi->startform(-method=>"get") .5055"<p class=\"projsearch\">Search:\n".5056$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".5057"</p>".5058$cgi->end_form() ."\n";5059 git_project_list_body(\@list,$order);5060 git_footer_html();5061}50625063sub git_forks {5064my$order=$input_params{'order'};5065if(defined$order&&$order!~m/none|project|descr|owner|age/) {5066 die_error(400,"Unknown order parameter");5067}50685069my@list= git_get_projects_list($project);5070if(!@list) {5071 die_error(404,"No forks found");5072}50735074 git_header_html();5075 git_print_page_nav('','');5076 git_print_header_div('summary',"$projectforks");5077 git_project_list_body(\@list,$order);5078 git_footer_html();5079}50805081sub git_project_index {5082my@projects= git_get_projects_list($project);50835084print$cgi->header(5085-type =>'text/plain',5086-charset =>'utf-8',5087-content_disposition =>'inline; filename="index.aux"');50885089foreachmy$pr(@projects) {5090if(!exists$pr->{'owner'}) {5091$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");5092}50935094my($path,$owner) = ($pr->{'path'},$pr->{'owner'});5095# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '5096$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5097$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5098$path=~s/ /\+/g;5099$owner=~s/ /\+/g;51005101print"$path$owner\n";5102}5103}51045105sub git_summary {5106my$descr= git_get_project_description($project) ||"none";5107my%co= parse_commit("HEAD");5108my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();5109my$head=$co{'id'};51105111my$owner= git_get_project_owner($project);51125113my$refs= git_get_references();5114# These get_*_list functions return one more to allow us to see if5115# there are more ...5116my@taglist= git_get_tags_list(16);5117my@headlist= git_get_heads_list(16);5118my@forklist;5119my$check_forks= gitweb_check_feature('forks');51205121if($check_forks) {5122@forklist= git_get_projects_list($project);5123}51245125 git_header_html();5126 git_print_page_nav('summary','',$head);51275128print"<div class=\"title\"> </div>\n";5129print"<table class=\"projects_list\">\n".5130"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".5131"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";5132if(defined$cd{'rfc2822'}) {5133print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";5134}51355136# use per project git URL list in $projectroot/$project/cloneurl5137# or make project git URL from git base URL and project name5138my$url_tag="URL";5139my@url_list= git_get_project_url_list($project);5140@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;5141foreachmy$git_url(@url_list) {5142next unless$git_url;5143print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";5144$url_tag="";5145}51465147# Tag cloud5148my$show_ctags= gitweb_check_feature('ctags');5149if($show_ctags) {5150my$ctags= git_get_project_ctags($project);5151my$cloud= git_populate_project_tagcloud($ctags);5152print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";5153print"</td>\n<td>"unless%$ctags;5154print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";5155print"</td>\n<td>"if%$ctags;5156print git_show_project_tagcloud($cloud,48);5157print"</td></tr>";5158}51595160print"</table>\n";51615162# If XSS prevention is on, we don't include README.html.5163# TODO: Allow a readme in some safe format.5164if(!$prevent_xss&& -s "$projectroot/$project/README.html") {5165print"<div class=\"title\">readme</div>\n".5166"<div class=\"readme\">\n";5167 insert_file("$projectroot/$project/README.html");5168print"\n</div>\n";# class="readme"5169}51705171# we need to request one more than 16 (0..15) to check if5172# those 16 are all5173my@commitlist=$head? parse_commits($head,17) : ();5174if(@commitlist) {5175 git_print_header_div('shortlog');5176 git_shortlog_body(\@commitlist,0,15,$refs,5177$#commitlist<=15?undef:5178$cgi->a({-href => href(action=>"shortlog")},"..."));5179}51805181if(@taglist) {5182 git_print_header_div('tags');5183 git_tags_body(\@taglist,0,15,5184$#taglist<=15?undef:5185$cgi->a({-href => href(action=>"tags")},"..."));5186}51875188if(@headlist) {5189 git_print_header_div('heads');5190 git_heads_body(\@headlist,$head,0,15,5191$#headlist<=15?undef:5192$cgi->a({-href => href(action=>"heads")},"..."));5193}51945195if(@forklist) {5196 git_print_header_div('forks');5197 git_project_list_body(\@forklist,'age',0,15,5198$#forklist<=15?undef:5199$cgi->a({-href => href(action=>"forks")},"..."),5200'no_header');5201}52025203 git_footer_html();5204}52055206sub git_tag {5207my%tag= parse_tag($hash);52085209if(!%tag) {5210 die_error(404,"Unknown tag object");5211}52125213my$head= git_get_head_hash($project);5214 git_header_html();5215 git_print_page_nav('','',$head,undef,$head);5216 git_print_header_div('commit', esc_html($tag{'name'}),$hash);5217print"<div class=\"title_text\">\n".5218"<table class=\"object_header\">\n".5219"<tr>\n".5220"<td>object</td>\n".5221"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5222$tag{'object'}) ."</td>\n".5223"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5224$tag{'type'}) ."</td>\n".5225"</tr>\n";5226if(defined($tag{'author'})) {5227 git_print_authorship_rows(\%tag,'author');5228}5229print"</table>\n\n".5230"</div>\n";5231print"<div class=\"page_body\">";5232my$comment=$tag{'comment'};5233foreachmy$line(@$comment) {5234chomp$line;5235print esc_html($line, -nbsp=>1) ."<br/>\n";5236}5237print"</div>\n";5238 git_footer_html();5239}52405241sub git_blame_common {5242my$format=shift||'porcelain';5243if($formateq'porcelain'&&$cgi->param('js')) {5244$format='incremental';5245$action='blame_incremental';# for page title etc5246}52475248# permissions5249 gitweb_check_feature('blame')5250or die_error(403,"Blame view not allowed");52515252# error checking5253 die_error(400,"No file name given")unless$file_name;5254$hash_base||= git_get_head_hash($project);5255 die_error(404,"Couldn't find base commit")unless$hash_base;5256my%co= parse_commit($hash_base)5257or die_error(404,"Commit not found");5258my$ftype="blob";5259if(!defined$hash) {5260$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5261or die_error(404,"Error looking up file");5262}else{5263$ftype= git_get_type($hash);5264if($ftype!~"blob") {5265 die_error(400,"Object is not a blob");5266}5267}52685269my$fd;5270if($formateq'incremental') {5271# get file contents (as base)5272open$fd,"-|", git_cmd(),'cat-file','blob',$hash5273or die_error(500,"Open git-cat-file failed");5274}elsif($formateq'data') {5275# run git-blame --incremental5276open$fd,"-|", git_cmd(),"blame","--incremental",5277$hash_base,"--",$file_name5278or die_error(500,"Open git-blame --incremental failed");5279}else{5280# run git-blame --porcelain5281open$fd,"-|", git_cmd(),"blame",'-p',5282$hash_base,'--',$file_name5283or die_error(500,"Open git-blame --porcelain failed");5284}52855286# incremental blame data returns early5287if($formateq'data') {5288print$cgi->header(5289-type=>"text/plain", -charset =>"utf-8",5290-status=>"200 OK");5291local$| =1;# output autoflush5292printwhile<$fd>;5293close$fd5294or print"ERROR$!\n";52955296print'END';5297if(defined$t0&& gitweb_check_feature('timed')) {5298print' '.5299 tv_interval($t0, [ gettimeofday() ]).5300' '.$number_of_git_cmds;5301}5302print"\n";53035304return;5305}53065307# page header5308 git_header_html();5309my$formats_nav=5310$cgi->a({-href => href(action=>"blob", -replay=>1)},5311"blob") .5312" | ";5313if($formateq'incremental') {5314$formats_nav.=5315$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5316"blame") ." (non-incremental)";5317}else{5318$formats_nav.=5319$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5320"blame") ." (incremental)";5321}5322$formats_nav.=5323" | ".5324$cgi->a({-href => href(action=>"history", -replay=>1)},5325"history") .5326" | ".5327$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5328"HEAD");5329 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5330 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5331 git_print_page_path($file_name,$ftype,$hash_base);53325333# page body5334if($formateq'incremental') {5335print"<noscript>\n<div class=\"error\"><center><b>\n".5336"This page requires JavaScript to run.\nUse ".5337$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5338'this page').5339" instead.\n".5340"</b></center></div>\n</noscript>\n";53415342print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5343}53445345print qq!<div class="page_body">\n!;5346print qq!<div id="progress_info">.../ ...</div>\n!5347if($formateq'incremental');5348print qq!<table id="blame_table"class="blame" width="100%">\n!.5349#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5350 qq!<thead>\n!.5351 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5352 qq!</thead>\n!.5353 qq!<tbody>\n!;53545355my@rev_color=qw(light dark);5356my$num_colors=scalar(@rev_color);5357my$current_color=0;53585359if($formateq'incremental') {5360my$color_class=$rev_color[$current_color];53615362#contents of a file5363my$linenr=0;5364 LINE:5365while(my$line= <$fd>) {5366chomp$line;5367$linenr++;53685369print qq!<tr id="l$linenr"class="$color_class">!.5370 qq!<td class="sha1"><a href=""> </a></td>!.5371 qq!<td class="linenr">!.5372 qq!<a class="linenr" href="">$linenr</a></td>!;5373print qq!<td class="pre">! . esc_html($line) ."</td>\n";5374print qq!</tr>\n!;5375}53765377}else{# porcelain, i.e. ordinary blame5378my%metainfo= ();# saves information about commits53795380# blame data5381 LINE:5382while(my$line= <$fd>) {5383chomp$line;5384# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5385# no <lines in group> for subsequent lines in group of lines5386my($full_rev,$orig_lineno,$lineno,$group_size) =5387($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5388if(!exists$metainfo{$full_rev}) {5389$metainfo{$full_rev} = {'nprevious'=>0};5390}5391my$meta=$metainfo{$full_rev};5392my$data;5393while($data= <$fd>) {5394chomp$data;5395last if($data=~s/^\t//);# contents of line5396if($data=~/^(\S+)(?: (.*))?$/) {5397$meta->{$1} =$2unlessexists$meta->{$1};5398}5399if($data=~/^previous /) {5400$meta->{'nprevious'}++;5401}5402}5403my$short_rev=substr($full_rev,0,8);5404my$author=$meta->{'author'};5405my%date=5406 parse_date($meta->{'author-time'},$meta->{'author-tz'});5407my$date=$date{'iso-tz'};5408if($group_size) {5409$current_color= ($current_color+1) %$num_colors;5410}5411my$tr_class=$rev_color[$current_color];5412$tr_class.=' boundary'if(exists$meta->{'boundary'});5413$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5414$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5415print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5416if($group_size) {5417print"<td class=\"sha1\"";5418print" title=\"". esc_html($author) .",$date\"";5419print" rowspan=\"$group_size\""if($group_size>1);5420print">";5421print$cgi->a({-href => href(action=>"commit",5422 hash=>$full_rev,5423 file_name=>$file_name)},5424 esc_html($short_rev));5425if($group_size>=2) {5426my@author_initials= ($author=~/\b([[:upper:]])\B/g);5427if(@author_initials) {5428print"<br />".5429 esc_html(join('',@author_initials));5430# or join('.', ...)5431}5432}5433print"</td>\n";5434}5435# 'previous' <sha1 of parent commit> <filename at commit>5436if(exists$meta->{'previous'} &&5437$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5438$meta->{'parent'} =$1;5439$meta->{'file_parent'} = unquote($2);5440}5441my$linenr_commit=5442exists($meta->{'parent'}) ?5443$meta->{'parent'} :$full_rev;5444my$linenr_filename=5445exists($meta->{'file_parent'}) ?5446$meta->{'file_parent'} : unquote($meta->{'filename'});5447my$blamed= href(action =>'blame',5448 file_name =>$linenr_filename,5449 hash_base =>$linenr_commit);5450print"<td class=\"linenr\">";5451print$cgi->a({ -href =>"$blamed#l$orig_lineno",5452-class=>"linenr"},5453 esc_html($lineno));5454print"</td>";5455print"<td class=\"pre\">". esc_html($data) ."</td>\n";5456print"</tr>\n";5457}# end while54585459}54605461# footer5462print"</tbody>\n".5463"</table>\n";# class="blame"5464print"</div>\n";# class="blame_body"5465close$fd5466or print"Reading blob failed\n";54675468 git_footer_html();5469}54705471sub git_blame {5472 git_blame_common();5473}54745475sub git_blame_incremental {5476 git_blame_common('incremental');5477}54785479sub git_blame_data {5480 git_blame_common('data');5481}54825483sub git_tags {5484my$head= git_get_head_hash($project);5485 git_header_html();5486 git_print_page_nav('','',$head,undef,$head);5487 git_print_header_div('summary',$project);54885489my@tagslist= git_get_tags_list();5490if(@tagslist) {5491 git_tags_body(\@tagslist);5492}5493 git_footer_html();5494}54955496sub git_heads {5497my$head= git_get_head_hash($project);5498 git_header_html();5499 git_print_page_nav('','',$head,undef,$head);5500 git_print_header_div('summary',$project);55015502my@headslist= git_get_heads_list();5503if(@headslist) {5504 git_heads_body(\@headslist,$head);5505}5506 git_footer_html();5507}55085509sub git_blob_plain {5510my$type=shift;5511my$expires;55125513if(!defined$hash) {5514if(defined$file_name) {5515my$base=$hash_base|| git_get_head_hash($project);5516$hash= git_get_hash_by_path($base,$file_name,"blob")5517or die_error(404,"Cannot find file");5518}else{5519 die_error(400,"No file name defined");5520}5521}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5522# blobs defined by non-textual hash id's can be cached5523$expires="+1d";5524}55255526open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5527or die_error(500,"Open git-cat-file blob '$hash' failed");55285529# content-type (can include charset)5530$type= blob_contenttype($fd,$file_name,$type);55315532# "save as" filename, even when no $file_name is given5533my$save_as="$hash";5534if(defined$file_name) {5535$save_as=$file_name;5536}elsif($type=~m/^text\//) {5537$save_as.='.txt';5538}55395540# With XSS prevention on, blobs of all types except a few known safe5541# ones are served with "Content-Disposition: attachment" to make sure5542# they don't run in our security domain. For certain image types,5543# blob view writes an <img> tag referring to blob_plain view, and we5544# want to be sure not to break that by serving the image as an5545# attachment (though Firefox 3 doesn't seem to care).5546my$sandbox=$prevent_xss&&5547$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;55485549print$cgi->header(5550-type =>$type,5551-expires =>$expires,5552-content_disposition =>5553($sandbox?'attachment':'inline')5554.'; filename="'.$save_as.'"');5555local$/=undef;5556binmode STDOUT,':raw';5557print<$fd>;5558binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5559close$fd;5560}55615562sub git_blob {5563my$expires;55645565if(!defined$hash) {5566if(defined$file_name) {5567my$base=$hash_base|| git_get_head_hash($project);5568$hash= git_get_hash_by_path($base,$file_name,"blob")5569or die_error(404,"Cannot find file");5570}else{5571 die_error(400,"No file name defined");5572}5573}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5574# blobs defined by non-textual hash id's can be cached5575$expires="+1d";5576}55775578my$have_blame= gitweb_check_feature('blame');5579open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5580or die_error(500,"Couldn't cat$file_name,$hash");5581my$mimetype= blob_mimetype($fd,$file_name);5582# use 'blob_plain' (aka 'raw') view for files that cannot be displayed5583if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5584close$fd;5585return git_blob_plain($mimetype);5586}5587# we can have blame only for text/* mimetype5588$have_blame&&= ($mimetype=~m!^text/!);55895590my$highlight= gitweb_check_feature('highlight');5591my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);5592$fd= run_highlighter($fd,$highlight,$syntax)5593if$syntax;55945595 git_header_html(undef,$expires);5596my$formats_nav='';5597if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5598if(defined$file_name) {5599if($have_blame) {5600$formats_nav.=5601$cgi->a({-href => href(action=>"blame", -replay=>1)},5602"blame") .5603" | ";5604}5605$formats_nav.=5606$cgi->a({-href => href(action=>"history", -replay=>1)},5607"history") .5608" | ".5609$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5610"raw") .5611" | ".5612$cgi->a({-href => href(action=>"blob",5613 hash_base=>"HEAD", file_name=>$file_name)},5614"HEAD");5615}else{5616$formats_nav.=5617$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5618"raw");5619}5620 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5621 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5622}else{5623print"<div class=\"page_nav\">\n".5624"<br/><br/></div>\n".5625"<div class=\"title\">$hash</div>\n";5626}5627 git_print_page_path($file_name,"blob",$hash_base);5628print"<div class=\"page_body\">\n";5629if($mimetype=~m!^image/!) {5630print qq!<img type="$mimetype"!;5631if($file_name) {5632print qq! alt="$file_name" title="$file_name"!;5633}5634print qq! src="! .5635 href(action=>"blob_plain", hash=>$hash,5636 hash_base=>$hash_base, file_name=>$file_name) .5637 qq!"/>\n!;5638}else{5639my$nr;5640while(my$line= <$fd>) {5641chomp$line;5642$nr++;5643$line= untabify($line);5644printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,5645$nr, href(-replay =>1),$nr,$nr,$syntax?$line: esc_html($line, -nbsp=>1);5646}5647}5648close$fd5649or print"Reading blob failed.\n";5650print"</div>";5651 git_footer_html();5652}56535654sub git_tree {5655if(!defined$hash_base) {5656$hash_base="HEAD";5657}5658if(!defined$hash) {5659if(defined$file_name) {5660$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5661}else{5662$hash=$hash_base;5663}5664}5665 die_error(404,"No such tree")unlessdefined($hash);56665667my$show_sizes= gitweb_check_feature('show-sizes');5668my$have_blame= gitweb_check_feature('blame');56695670my@entries= ();5671{5672local$/="\0";5673open my$fd,"-|", git_cmd(),"ls-tree",'-z',5674($show_sizes?'-l': ()),@extra_options,$hash5675or die_error(500,"Open git-ls-tree failed");5676@entries=map{chomp;$_} <$fd>;5677close$fd5678or die_error(404,"Reading tree failed");5679}56805681my$refs= git_get_references();5682my$ref= format_ref_marker($refs,$hash_base);5683 git_header_html();5684my$basedir='';5685if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5686my@views_nav= ();5687if(defined$file_name) {5688push@views_nav,5689$cgi->a({-href => href(action=>"history", -replay=>1)},5690"history"),5691$cgi->a({-href => href(action=>"tree",5692 hash_base=>"HEAD", file_name=>$file_name)},5693"HEAD"),5694}5695my$snapshot_links= format_snapshot_links($hash);5696if(defined$snapshot_links) {5697# FIXME: Should be available when we have no hash base as well.5698push@views_nav,$snapshot_links;5699}5700 git_print_page_nav('tree','',$hash_base,undef,undef,5701join(' | ',@views_nav));5702 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5703}else{5704undef$hash_base;5705print"<div class=\"page_nav\">\n";5706print"<br/><br/></div>\n";5707print"<div class=\"title\">$hash</div>\n";5708}5709if(defined$file_name) {5710$basedir=$file_name;5711if($basedirne''&&substr($basedir, -1)ne'/') {5712$basedir.='/';5713}5714 git_print_page_path($file_name,'tree',$hash_base);5715}5716print"<div class=\"page_body\">\n";5717print"<table class=\"tree\">\n";5718my$alternate=1;5719# '..' (top directory) link if possible5720if(defined$hash_base&&5721defined$file_name&&$file_name=~m![^/]+$!) {5722if($alternate) {5723print"<tr class=\"dark\">\n";5724}else{5725print"<tr class=\"light\">\n";5726}5727$alternate^=1;57285729my$up=$file_name;5730$up=~s!/?[^/]+$!!;5731undef$upunless$up;5732# based on git_print_tree_entry5733print'<td class="mode">'. mode_str('040000') ."</td>\n";5734print'<td class="size"> </td>'."\n"if$show_sizes;5735print'<td class="list">';5736print$cgi->a({-href => href(action=>"tree",5737 hash_base=>$hash_base,5738 file_name=>$up)},5739"..");5740print"</td>\n";5741print"<td class=\"link\"></td>\n";57425743print"</tr>\n";5744}5745foreachmy$line(@entries) {5746my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);57475748if($alternate) {5749print"<tr class=\"dark\">\n";5750}else{5751print"<tr class=\"light\">\n";5752}5753$alternate^=1;57545755 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);57565757print"</tr>\n";5758}5759print"</table>\n".5760"</div>";5761 git_footer_html();5762}57635764sub snapshot_name {5765my($project,$hash) =@_;57665767# path/to/project.git -> project5768# path/to/project/.git -> project5769my$name= to_utf8($project);5770$name=~ s,([^/])/*\.git$,$1,;5771$name= basename($name);5772# sanitize name5773$name=~s/[[:cntrl:]]/?/g;57745775my$ver=$hash;5776if($hash=~/^[0-9a-fA-F]+$/) {5777# shorten SHA-1 hash5778my$full_hash= git_get_full_hash($project,$hash);5779if($full_hash=~/^$hash/&&length($hash) >7) {5780$ver= git_get_short_hash($project,$hash);5781}5782}elsif($hash=~m!^refs/tags/(.*)$!) {5783# tags don't need shortened SHA-1 hash5784$ver=$1;5785}else{5786# branches and other need shortened SHA-1 hash5787if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5788$ver=$1;5789}5790$ver.='-'. git_get_short_hash($project,$hash);5791}5792# in case of hierarchical branch names5793$ver=~s!/!.!g;57945795# name = project-version_string5796$name="$name-$ver";57975798returnwantarray? ($name,$name) :$name;5799}58005801sub git_snapshot {5802my$format=$input_params{'snapshot_format'};5803if(!@snapshot_fmts) {5804 die_error(403,"Snapshots not allowed");5805}5806# default to first supported snapshot format5807$format||=$snapshot_fmts[0];5808if($format!~m/^[a-z0-9]+$/) {5809 die_error(400,"Invalid snapshot format parameter");5810}elsif(!exists($known_snapshot_formats{$format})) {5811 die_error(400,"Unknown snapshot format");5812}elsif($known_snapshot_formats{$format}{'disabled'}) {5813 die_error(403,"Snapshot format not allowed");5814}elsif(!grep($_eq$format,@snapshot_fmts)) {5815 die_error(403,"Unsupported snapshot format");5816}58175818my$type= git_get_type("$hash^{}");5819if(!$type) {5820 die_error(404,'Object does not exist');5821}elsif($typeeq'blob') {5822 die_error(400,'Object is not a tree-ish');5823}58245825my($name,$prefix) = snapshot_name($project,$hash);5826my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5827my$cmd= quote_command(5828 git_cmd(),'archive',5829"--format=$known_snapshot_formats{$format}{'format'}",5830"--prefix=$prefix/",$hash);5831if(exists$known_snapshot_formats{$format}{'compressor'}) {5832$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5833}58345835$filename=~s/(["\\])/\\$1/g;5836print$cgi->header(5837-type =>$known_snapshot_formats{$format}{'type'},5838-content_disposition =>'inline; filename="'.$filename.'"',5839-status =>'200 OK');58405841open my$fd,"-|",$cmd5842or die_error(500,"Execute git-archive failed");5843binmode STDOUT,':raw';5844print<$fd>;5845binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5846close$fd;5847}58485849sub git_log_generic {5850my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;58515852my$head= git_get_head_hash($project);5853if(!defined$base) {5854$base=$head;5855}5856if(!defined$page) {5857$page=0;5858}5859my$refs= git_get_references();58605861my$commit_hash=$base;5862if(defined$parent) {5863$commit_hash="$parent..$base";5864}5865my@commitlist=5866 parse_commits($commit_hash,101, (100*$page),5867defined$file_name? ($file_name,"--full-history") : ());58685869my$ftype;5870if(!defined$file_hash&&defined$file_name) {5871# some commits could have deleted file in question,5872# and not have it in tree, but one of them has to have it5873for(my$i=0;$i<@commitlist;$i++) {5874$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5875last ifdefined$file_hash;5876}5877}5878if(defined$file_hash) {5879$ftype= git_get_type($file_hash);5880}5881if(defined$file_name&& !defined$ftype) {5882 die_error(500,"Unknown type of object");5883}5884my%co;5885if(defined$file_name) {5886%co= parse_commit($base)5887or die_error(404,"Unknown commit object");5888}588958905891my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5892my$next_link='';5893if($#commitlist>=100) {5894$next_link=5895$cgi->a({-href => href(-replay=>1, page=>$page+1),5896-accesskey =>"n", -title =>"Alt-n"},"next");5897}5898my$patch_max= gitweb_get_feature('patches');5899if($patch_max&& !defined$file_name) {5900if($patch_max<0||@commitlist<=$patch_max) {5901$paging_nav.=" ⋅ ".5902$cgi->a({-href => href(action=>"patches", -replay=>1)},5903"patches");5904}5905}59065907 git_header_html();5908 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5909if(defined$file_name) {5910 git_print_header_div('commit', esc_html($co{'title'}),$base);5911}else{5912 git_print_header_div('summary',$project)5913}5914 git_print_page_path($file_name,$ftype,$hash_base)5915if(defined$file_name);59165917$body_subr->(\@commitlist,0,99,$refs,$next_link,5918$file_name,$file_hash,$ftype);59195920 git_footer_html();5921}59225923sub git_log {5924 git_log_generic('log', \&git_log_body,5925$hash,$hash_parent);5926}59275928sub git_commit {5929$hash||=$hash_base||"HEAD";5930my%co= parse_commit($hash)5931or die_error(404,"Unknown commit object");59325933my$parent=$co{'parent'};5934my$parents=$co{'parents'};# listref59355936# we need to prepare $formats_nav before any parameter munging5937my$formats_nav;5938if(!defined$parent) {5939# --root commitdiff5940$formats_nav.='(initial)';5941}elsif(@$parents==1) {5942# single parent commit5943$formats_nav.=5944'(parent: '.5945$cgi->a({-href => href(action=>"commit",5946 hash=>$parent)},5947 esc_html(substr($parent,0,7))) .5948')';5949}else{5950# merge commit5951$formats_nav.=5952'(merge: '.5953join(' ',map{5954$cgi->a({-href => href(action=>"commit",5955 hash=>$_)},5956 esc_html(substr($_,0,7)));5957}@$parents) .5958')';5959}5960if(gitweb_check_feature('patches') &&@$parents<=1) {5961$formats_nav.=" | ".5962$cgi->a({-href => href(action=>"patch", -replay=>1)},5963"patch");5964}59655966if(!defined$parent) {5967$parent="--root";5968}5969my@difftree;5970open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5971@diff_opts,5972(@$parents<=1?$parent:'-c'),5973$hash,"--"5974or die_error(500,"Open git-diff-tree failed");5975@difftree=map{chomp;$_} <$fd>;5976close$fdor die_error(404,"Reading git-diff-tree failed");59775978# non-textual hash id's can be cached5979my$expires;5980if($hash=~m/^[0-9a-fA-F]{40}$/) {5981$expires="+1d";5982}5983my$refs= git_get_references();5984my$ref= format_ref_marker($refs,$co{'id'});59855986 git_header_html(undef,$expires);5987 git_print_page_nav('commit','',5988$hash,$co{'tree'},$hash,5989$formats_nav);59905991if(defined$co{'parent'}) {5992 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5993}else{5994 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5995}5996print"<div class=\"title_text\">\n".5997"<table class=\"object_header\">\n";5998 git_print_authorship_rows(\%co);5999print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";6000print"<tr>".6001"<td>tree</td>".6002"<td class=\"sha1\">".6003$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),6004class=>"list"},$co{'tree'}) .6005"</td>".6006"<td class=\"link\">".6007$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},6008"tree");6009my$snapshot_links= format_snapshot_links($hash);6010if(defined$snapshot_links) {6011print" | ".$snapshot_links;6012}6013print"</td>".6014"</tr>\n";60156016foreachmy$par(@$parents) {6017print"<tr>".6018"<td>parent</td>".6019"<td class=\"sha1\">".6020$cgi->a({-href => href(action=>"commit", hash=>$par),6021class=>"list"},$par) .6022"</td>".6023"<td class=\"link\">".6024$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .6025" | ".6026$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .6027"</td>".6028"</tr>\n";6029}6030print"</table>".6031"</div>\n";60326033print"<div class=\"page_body\">\n";6034 git_print_log($co{'comment'});6035print"</div>\n";60366037 git_difftree_body(\@difftree,$hash,@$parents);60386039 git_footer_html();6040}60416042sub git_object {6043# object is defined by:6044# - hash or hash_base alone6045# - hash_base and file_name6046my$type;60476048# - hash or hash_base alone6049if($hash|| ($hash_base&& !defined$file_name)) {6050my$object_id=$hash||$hash_base;60516052open my$fd,"-|", quote_command(6053 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'6054or die_error(404,"Object does not exist");6055$type= <$fd>;6056chomp$type;6057close$fd6058or die_error(404,"Object does not exist");60596060# - hash_base and file_name6061}elsif($hash_base&&defined$file_name) {6062$file_name=~ s,/+$,,;60636064system(git_cmd(),"cat-file",'-e',$hash_base) ==06065or die_error(404,"Base object does not exist");60666067# here errors should not hapen6068open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name6069or die_error(500,"Open git-ls-tree failed");6070my$line= <$fd>;6071close$fd;60726073#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'6074unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {6075 die_error(404,"File or directory for given base does not exist");6076}6077$type=$2;6078$hash=$3;6079}else{6080 die_error(400,"Not enough information to find object");6081}60826083print$cgi->redirect(-uri => href(action=>$type, -full=>1,6084 hash=>$hash, hash_base=>$hash_base,6085 file_name=>$file_name),6086-status =>'302 Found');6087}60886089sub git_blobdiff {6090my$format=shift||'html';60916092my$fd;6093my@difftree;6094my%diffinfo;6095my$expires;60966097# preparing $fd and %diffinfo for git_patchset_body6098# new style URI6099if(defined$hash_base&&defined$hash_parent_base) {6100if(defined$file_name) {6101# read raw output6102open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6103$hash_parent_base,$hash_base,6104"--", (defined$file_parent?$file_parent: ()),$file_name6105or die_error(500,"Open git-diff-tree failed");6106@difftree=map{chomp;$_} <$fd>;6107close$fd6108or die_error(404,"Reading git-diff-tree failed");6109@difftree6110or die_error(404,"Blob diff not found");61116112}elsif(defined$hash&&6113$hash=~/[0-9a-fA-F]{40}/) {6114# try to find filename from $hash61156116# read filtered raw output6117open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6118$hash_parent_base,$hash_base,"--"6119or die_error(500,"Open git-diff-tree failed");6120@difftree=6121# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'6122# $hash == to_id6123grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}6124map{chomp;$_} <$fd>;6125close$fd6126or die_error(404,"Reading git-diff-tree failed");6127@difftree6128or die_error(404,"Blob diff not found");61296130}else{6131 die_error(400,"Missing one of the blob diff parameters");6132}61336134if(@difftree>1) {6135 die_error(400,"Ambiguous blob diff specification");6136}61376138%diffinfo= parse_difftree_raw_line($difftree[0]);6139$file_parent||=$diffinfo{'from_file'} ||$file_name;6140$file_name||=$diffinfo{'to_file'};61416142$hash_parent||=$diffinfo{'from_id'};6143$hash||=$diffinfo{'to_id'};61446145# non-textual hash id's can be cached6146if($hash_base=~m/^[0-9a-fA-F]{40}$/&&6147$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {6148$expires='+1d';6149}61506151# open patch output6152open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6153'-p', ($formateq'html'?"--full-index": ()),6154$hash_parent_base,$hash_base,6155"--", (defined$file_parent?$file_parent: ()),$file_name6156or die_error(500,"Open git-diff-tree failed");6157}61586159# old/legacy style URI -- not generated anymore since 1.4.3.6160if(!%diffinfo) {6161 die_error('404 Not Found',"Missing one of the blob diff parameters")6162}61636164# header6165if($formateq'html') {6166my$formats_nav=6167$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},6168"raw");6169 git_header_html(undef,$expires);6170if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6171 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6172 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6173}else{6174print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";6175print"<div class=\"title\">$hashvs$hash_parent</div>\n";6176}6177if(defined$file_name) {6178 git_print_page_path($file_name,"blob",$hash_base);6179}else{6180print"<div class=\"page_path\"></div>\n";6181}61826183}elsif($formateq'plain') {6184print$cgi->header(6185-type =>'text/plain',6186-charset =>'utf-8',6187-expires =>$expires,6188-content_disposition =>'inline; filename="'."$file_name".'.patch"');61896190print"X-Git-Url: ".$cgi->self_url() ."\n\n";61916192}else{6193 die_error(400,"Unknown blobdiff format");6194}61956196# patch6197if($formateq'html') {6198print"<div class=\"page_body\">\n";61996200 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);6201close$fd;62026203print"</div>\n";# class="page_body"6204 git_footer_html();62056206}else{6207while(my$line= <$fd>) {6208$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;6209$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;62106211print$line;62126213last if$line=~m!^\+\+\+!;6214}6215local$/=undef;6216print<$fd>;6217close$fd;6218}6219}62206221sub git_blobdiff_plain {6222 git_blobdiff('plain');6223}62246225sub git_commitdiff {6226my%params=@_;6227my$format=$params{-format} ||'html';62286229my($patch_max) = gitweb_get_feature('patches');6230if($formateq'patch') {6231 die_error(403,"Patch view not allowed")unless$patch_max;6232}62336234$hash||=$hash_base||"HEAD";6235my%co= parse_commit($hash)6236or die_error(404,"Unknown commit object");62376238# choose format for commitdiff for merge6239if(!defined$hash_parent&& @{$co{'parents'}} >1) {6240$hash_parent='--cc';6241}6242# we need to prepare $formats_nav before almost any parameter munging6243my$formats_nav;6244if($formateq'html') {6245$formats_nav=6246$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6247"raw");6248if($patch_max&& @{$co{'parents'}} <=1) {6249$formats_nav.=" | ".6250$cgi->a({-href => href(action=>"patch", -replay=>1)},6251"patch");6252}62536254if(defined$hash_parent&&6255$hash_parentne'-c'&&$hash_parentne'--cc') {6256# commitdiff with two commits given6257my$hash_parent_short=$hash_parent;6258if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6259$hash_parent_short=substr($hash_parent,0,7);6260}6261$formats_nav.=6262' (from';6263for(my$i=0;$i< @{$co{'parents'}};$i++) {6264if($co{'parents'}[$i]eq$hash_parent) {6265$formats_nav.=' parent '. ($i+1);6266last;6267}6268}6269$formats_nav.=': '.6270$cgi->a({-href => href(action=>"commitdiff",6271 hash=>$hash_parent)},6272 esc_html($hash_parent_short)) .6273')';6274}elsif(!$co{'parent'}) {6275# --root commitdiff6276$formats_nav.=' (initial)';6277}elsif(scalar@{$co{'parents'}} ==1) {6278# single parent commit6279$formats_nav.=6280' (parent: '.6281$cgi->a({-href => href(action=>"commitdiff",6282 hash=>$co{'parent'})},6283 esc_html(substr($co{'parent'},0,7))) .6284')';6285}else{6286# merge commit6287if($hash_parenteq'--cc') {6288$formats_nav.=' | '.6289$cgi->a({-href => href(action=>"commitdiff",6290 hash=>$hash, hash_parent=>'-c')},6291'combined');6292}else{# $hash_parent eq '-c'6293$formats_nav.=' | '.6294$cgi->a({-href => href(action=>"commitdiff",6295 hash=>$hash, hash_parent=>'--cc')},6296'compact');6297}6298$formats_nav.=6299' (merge: '.6300join(' ',map{6301$cgi->a({-href => href(action=>"commitdiff",6302 hash=>$_)},6303 esc_html(substr($_,0,7)));6304} @{$co{'parents'}} ) .6305')';6306}6307}63086309my$hash_parent_param=$hash_parent;6310if(!defined$hash_parent_param) {6311# --cc for multiple parents, --root for parentless6312$hash_parent_param=6313@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6314}63156316# read commitdiff6317my$fd;6318my@difftree;6319if($formateq'html') {6320open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6321"--no-commit-id","--patch-with-raw","--full-index",6322$hash_parent_param,$hash,"--"6323or die_error(500,"Open git-diff-tree failed");63246325while(my$line= <$fd>) {6326chomp$line;6327# empty line ends raw part of diff-tree output6328last unless$line;6329push@difftree,scalar parse_difftree_raw_line($line);6330}63316332}elsif($formateq'plain') {6333open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6334'-p',$hash_parent_param,$hash,"--"6335or die_error(500,"Open git-diff-tree failed");6336}elsif($formateq'patch') {6337# For commit ranges, we limit the output to the number of6338# patches specified in the 'patches' feature.6339# For single commits, we limit the output to a single patch,6340# diverging from the git-format-patch default.6341my@commit_spec= ();6342if($hash_parent) {6343if($patch_max>0) {6344push@commit_spec,"-$patch_max";6345}6346push@commit_spec,'-n',"$hash_parent..$hash";6347}else{6348if($params{-single}) {6349push@commit_spec,'-1';6350}else{6351if($patch_max>0) {6352push@commit_spec,"-$patch_max";6353}6354push@commit_spec,"-n";6355}6356push@commit_spec,'--root',$hash;6357}6358open$fd,"-|", git_cmd(),"format-patch",@diff_opts,6359'--encoding=utf8','--stdout',@commit_spec6360or die_error(500,"Open git-format-patch failed");6361}else{6362 die_error(400,"Unknown commitdiff format");6363}63646365# non-textual hash id's can be cached6366my$expires;6367if($hash=~m/^[0-9a-fA-F]{40}$/) {6368$expires="+1d";6369}63706371# write commit message6372if($formateq'html') {6373my$refs= git_get_references();6374my$ref= format_ref_marker($refs,$co{'id'});63756376 git_header_html(undef,$expires);6377 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6378 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6379print"<div class=\"title_text\">\n".6380"<table class=\"object_header\">\n";6381 git_print_authorship_rows(\%co);6382print"</table>".6383"</div>\n";6384print"<div class=\"page_body\">\n";6385if(@{$co{'comment'}} >1) {6386print"<div class=\"log\">\n";6387 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6388print"</div>\n";# class="log"6389}63906391}elsif($formateq'plain') {6392my$refs= git_get_references("tags");6393my$tagname= git_get_rev_name_tags($hash);6394my$filename= basename($project) ."-$hash.patch";63956396print$cgi->header(6397-type =>'text/plain',6398-charset =>'utf-8',6399-expires =>$expires,6400-content_disposition =>'inline; filename="'."$filename".'"');6401my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6402print"From: ". to_utf8($co{'author'}) ."\n";6403print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6404print"Subject: ". to_utf8($co{'title'}) ."\n";64056406print"X-Git-Tag:$tagname\n"if$tagname;6407print"X-Git-Url: ".$cgi->self_url() ."\n\n";64086409foreachmy$line(@{$co{'comment'}}) {6410print to_utf8($line) ."\n";6411}6412print"---\n\n";6413}elsif($formateq'patch') {6414my$filename= basename($project) ."-$hash.patch";64156416print$cgi->header(6417-type =>'text/plain',6418-charset =>'utf-8',6419-expires =>$expires,6420-content_disposition =>'inline; filename="'."$filename".'"');6421}64226423# write patch6424if($formateq'html') {6425my$use_parents= !defined$hash_parent||6426$hash_parenteq'-c'||$hash_parenteq'--cc';6427 git_difftree_body(\@difftree,$hash,6428$use_parents? @{$co{'parents'}} :$hash_parent);6429print"<br/>\n";64306431 git_patchset_body($fd, \@difftree,$hash,6432$use_parents? @{$co{'parents'}} :$hash_parent);6433close$fd;6434print"</div>\n";# class="page_body"6435 git_footer_html();64366437}elsif($formateq'plain') {6438local$/=undef;6439print<$fd>;6440close$fd6441or print"Reading git-diff-tree failed\n";6442}elsif($formateq'patch') {6443local$/=undef;6444print<$fd>;6445close$fd6446or print"Reading git-format-patch failed\n";6447}6448}64496450sub git_commitdiff_plain {6451 git_commitdiff(-format =>'plain');6452}64536454# format-patch-style patches6455sub git_patch {6456 git_commitdiff(-format =>'patch', -single =>1);6457}64586459sub git_patches {6460 git_commitdiff(-format =>'patch');6461}64626463sub git_history {6464 git_log_generic('history', \&git_history_body,6465$hash_base,$hash_parent_base,6466$file_name,$hash);6467}64686469sub git_search {6470 gitweb_check_feature('search')or die_error(403,"Search is disabled");6471if(!defined$searchtext) {6472 die_error(400,"Text field is empty");6473}6474if(!defined$hash) {6475$hash= git_get_head_hash($project);6476}6477my%co= parse_commit($hash);6478if(!%co) {6479 die_error(404,"Unknown commit object");6480}6481if(!defined$page) {6482$page=0;6483}64846485$searchtype||='commit';6486if($searchtypeeq'pickaxe') {6487# pickaxe may take all resources of your box and run for several minutes6488# with every query - so decide by yourself how public you make this feature6489 gitweb_check_feature('pickaxe')6490or die_error(403,"Pickaxe is disabled");6491}6492if($searchtypeeq'grep') {6493 gitweb_check_feature('grep')6494or die_error(403,"Grep is disabled");6495}64966497 git_header_html();64986499if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6500my$greptype;6501if($searchtypeeq'commit') {6502$greptype="--grep=";6503}elsif($searchtypeeq'author') {6504$greptype="--author=";6505}elsif($searchtypeeq'committer') {6506$greptype="--committer=";6507}6508$greptype.=$searchtext;6509my@commitlist= parse_commits($hash,101, (100*$page),undef,6510$greptype,'--regexp-ignore-case',6511$search_use_regexp?'--extended-regexp':'--fixed-strings');65126513my$paging_nav='';6514if($page>0) {6515$paging_nav.=6516$cgi->a({-href => href(action=>"search", hash=>$hash,6517 searchtext=>$searchtext,6518 searchtype=>$searchtype)},6519"first");6520$paging_nav.=" ⋅ ".6521$cgi->a({-href => href(-replay=>1, page=>$page-1),6522-accesskey =>"p", -title =>"Alt-p"},"prev");6523}else{6524$paging_nav.="first";6525$paging_nav.=" ⋅ prev";6526}6527my$next_link='';6528if($#commitlist>=100) {6529$next_link=6530$cgi->a({-href => href(-replay=>1, page=>$page+1),6531-accesskey =>"n", -title =>"Alt-n"},"next");6532$paging_nav.=" ⋅$next_link";6533}else{6534$paging_nav.=" ⋅ next";6535}65366537 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6538 git_print_header_div('commit', esc_html($co{'title'}),$hash);6539if($page==0&& !@commitlist) {6540print"<p>No match.</p>\n";6541}else{6542 git_search_grep_body(\@commitlist,0,99,$next_link);6543}6544}65456546if($searchtypeeq'pickaxe') {6547 git_print_page_nav('','',$hash,$co{'tree'},$hash);6548 git_print_header_div('commit', esc_html($co{'title'}),$hash);65496550print"<table class=\"pickaxe search\">\n";6551my$alternate=1;6552local$/="\n";6553open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6554'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6555($search_use_regexp?'--pickaxe-regex': ());6556undef%co;6557my@files;6558while(my$line= <$fd>) {6559chomp$line;6560next unless$line;65616562my%set= parse_difftree_raw_line($line);6563if(defined$set{'commit'}) {6564# finish previous commit6565if(%co) {6566print"</td>\n".6567"<td class=\"link\">".6568$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6569" | ".6570$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6571print"</td>\n".6572"</tr>\n";6573}65746575if($alternate) {6576print"<tr class=\"dark\">\n";6577}else{6578print"<tr class=\"light\">\n";6579}6580$alternate^=1;6581%co= parse_commit($set{'commit'});6582my$author= chop_and_escape_str($co{'author_name'},15,5);6583print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6584"<td><i>$author</i></td>\n".6585"<td>".6586$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6587-class=>"list subject"},6588 chop_and_escape_str($co{'title'},50) ."<br/>");6589}elsif(defined$set{'to_id'}) {6590next if($set{'to_id'} =~m/^0{40}$/);65916592print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6593 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6594-class=>"list"},6595"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6596"<br/>\n";6597}6598}6599close$fd;66006601# finish last commit (warning: repetition!)6602if(%co) {6603print"</td>\n".6604"<td class=\"link\">".6605$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6606" | ".6607$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6608print"</td>\n".6609"</tr>\n";6610}66116612print"</table>\n";6613}66146615if($searchtypeeq'grep') {6616 git_print_page_nav('','',$hash,$co{'tree'},$hash);6617 git_print_header_div('commit', esc_html($co{'title'}),$hash);66186619print"<table class=\"grep_search\">\n";6620my$alternate=1;6621my$matches=0;6622local$/="\n";6623open my$fd,"-|", git_cmd(),'grep','-n',6624$search_use_regexp? ('-E','-i') :'-F',6625$searchtext,$co{'tree'};6626my$lastfile='';6627while(my$line= <$fd>) {6628chomp$line;6629my($file,$lno,$ltext,$binary);6630last if($matches++>1000);6631if($line=~/^Binary file (.+) matches$/) {6632$file=$1;6633$binary=1;6634}else{6635(undef,$file,$lno,$ltext) =split(/:/,$line,4);6636}6637if($filene$lastfile) {6638$lastfileand print"</td></tr>\n";6639if($alternate++) {6640print"<tr class=\"dark\">\n";6641}else{6642print"<tr class=\"light\">\n";6643}6644print"<td class=\"list\">".6645$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6646 file_name=>"$file"),6647-class=>"list"}, esc_path($file));6648print"</td><td>\n";6649$lastfile=$file;6650}6651if($binary) {6652print"<div class=\"binary\">Binary file</div>\n";6653}else{6654$ltext= untabify($ltext);6655if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6656$ltext= esc_html($1, -nbsp=>1);6657$ltext.='<span class="match">';6658$ltext.= esc_html($2, -nbsp=>1);6659$ltext.='</span>';6660$ltext.= esc_html($3, -nbsp=>1);6661}else{6662$ltext= esc_html($ltext, -nbsp=>1);6663}6664print"<div class=\"pre\">".6665$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6666 file_name=>"$file").'#l'.$lno,6667-class=>"linenr"},sprintf('%4i',$lno))6668.' '.$ltext."</div>\n";6669}6670}6671if($lastfile) {6672print"</td></tr>\n";6673if($matches>1000) {6674print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6675}6676}else{6677print"<div class=\"diff nodifferences\">No matches found</div>\n";6678}6679close$fd;66806681print"</table>\n";6682}6683 git_footer_html();6684}66856686sub git_search_help {6687 git_header_html();6688 git_print_page_nav('','',$hash,$hash,$hash);6689print<<EOT;6690<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6691regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6692the pattern entered is recognized as the POSIX extended6693<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6694insensitive).</p>6695<dl>6696<dt><b>commit</b></dt>6697<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6698EOT6699my$have_grep= gitweb_check_feature('grep');6700if($have_grep) {6701print<<EOT;6702<dt><b>grep</b></dt>6703<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6704 a different one) are searched for the given pattern. On large trees, this search can take6705a while and put some strain on the server, so please use it with some consideration. Note that6706due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6707case-sensitive.</dd>6708EOT6709}6710print<<EOT;6711<dt><b>author</b></dt>6712<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6713<dt><b>committer</b></dt>6714<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6715EOT6716my$have_pickaxe= gitweb_check_feature('pickaxe');6717if($have_pickaxe) {6718print<<EOT;6719<dt><b>pickaxe</b></dt>6720<dd>All commits that caused the string to appear or disappear from any file (changes that6721added, removed or "modified" the string) will be listed. This search can take a while and6722takes a lot of strain on the server, so please use it wisely. Note that since you may be6723interested even in changes just changing the case as well, this search is case sensitive.</dd>6724EOT6725}6726print"</dl>\n";6727 git_footer_html();6728}67296730sub git_shortlog {6731 git_log_generic('shortlog', \&git_shortlog_body,6732$hash,$hash_parent);6733}67346735## ......................................................................6736## feeds (RSS, Atom; OPML)67376738sub git_feed {6739my$format=shift||'atom';6740my$have_blame= gitweb_check_feature('blame');67416742# Atom: http://www.atomenabled.org/developers/syndication/6743# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6744if($formatne'rss'&&$formatne'atom') {6745 die_error(400,"Unknown web feed format");6746}67476748# log/feed of current (HEAD) branch, log of given branch, history of file/directory6749my$head=$hash||'HEAD';6750my@commitlist= parse_commits($head,150,0,$file_name);67516752my%latest_commit;6753my%latest_date;6754my$content_type="application/$format+xml";6755if(defined$cgi->http('HTTP_ACCEPT') &&6756$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6757# browser (feed reader) prefers text/xml6758$content_type='text/xml';6759}6760if(defined($commitlist[0])) {6761%latest_commit= %{$commitlist[0]};6762my$latest_epoch=$latest_commit{'committer_epoch'};6763%latest_date= parse_date($latest_epoch);6764my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6765if(defined$if_modified) {6766my$since;6767if(eval{require HTTP::Date;1; }) {6768$since= HTTP::Date::str2time($if_modified);6769}elsif(eval{require Time::ParseDate;1; }) {6770$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6771}6772if(defined$since&&$latest_epoch<=$since) {6773print$cgi->header(6774-type =>$content_type,6775-charset =>'utf-8',6776-last_modified =>$latest_date{'rfc2822'},6777-status =>'304 Not Modified');6778return;6779}6780}6781print$cgi->header(6782-type =>$content_type,6783-charset =>'utf-8',6784-last_modified =>$latest_date{'rfc2822'});6785}else{6786print$cgi->header(6787-type =>$content_type,6788-charset =>'utf-8');6789}67906791# Optimization: skip generating the body if client asks only6792# for Last-Modified date.6793return if($cgi->request_method()eq'HEAD');67946795# header variables6796my$title="$site_name-$project/$action";6797my$feed_type='log';6798if(defined$hash) {6799$title.=" - '$hash'";6800$feed_type='branch log';6801if(defined$file_name) {6802$title.=" ::$file_name";6803$feed_type='history';6804}6805}elsif(defined$file_name) {6806$title.=" -$file_name";6807$feed_type='history';6808}6809$title.="$feed_type";6810my$descr= git_get_project_description($project);6811if(defined$descr) {6812$descr= esc_html($descr);6813}else{6814$descr="$project".6815($formateq'rss'?'RSS':'Atom') .6816" feed";6817}6818my$owner= git_get_project_owner($project);6819$owner= esc_html($owner);68206821#header6822my$alt_url;6823if(defined$file_name) {6824$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6825}elsif(defined$hash) {6826$alt_url= href(-full=>1, action=>"log", hash=>$hash);6827}else{6828$alt_url= href(-full=>1, action=>"summary");6829}6830print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6831if($formateq'rss') {6832print<<XML;6833<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6834<channel>6835XML6836print"<title>$title</title>\n".6837"<link>$alt_url</link>\n".6838"<description>$descr</description>\n".6839"<language>en</language>\n".6840# project owner is responsible for 'editorial' content6841"<managingEditor>$owner</managingEditor>\n";6842if(defined$logo||defined$favicon) {6843# prefer the logo to the favicon, since RSS6844# doesn't allow both6845my$img= esc_url($logo||$favicon);6846print"<image>\n".6847"<url>$img</url>\n".6848"<title>$title</title>\n".6849"<link>$alt_url</link>\n".6850"</image>\n";6851}6852if(%latest_date) {6853print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6854print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6855}6856print"<generator>gitweb v.$version/$git_version</generator>\n";6857}elsif($formateq'atom') {6858print<<XML;6859<feed xmlns="http://www.w3.org/2005/Atom">6860XML6861print"<title>$title</title>\n".6862"<subtitle>$descr</subtitle>\n".6863'<link rel="alternate" type="text/html" href="'.6864$alt_url.'" />'."\n".6865'<link rel="self" type="'.$content_type.'" href="'.6866$cgi->self_url() .'" />'."\n".6867"<id>". href(-full=>1) ."</id>\n".6868# use project owner for feed author6869"<author><name>$owner</name></author>\n";6870if(defined$favicon) {6871print"<icon>". esc_url($favicon) ."</icon>\n";6872}6873if(defined$logo_url) {6874# not twice as wide as tall: 72 x 27 pixels6875print"<logo>". esc_url($logo) ."</logo>\n";6876}6877if(!%latest_date) {6878# dummy date to keep the feed valid until commits trickle in:6879print"<updated>1970-01-01T00:00:00Z</updated>\n";6880}else{6881print"<updated>$latest_date{'iso-8601'}</updated>\n";6882}6883print"<generator version='$version/$git_version'>gitweb</generator>\n";6884}68856886# contents6887for(my$i=0;$i<=$#commitlist;$i++) {6888my%co= %{$commitlist[$i]};6889my$commit=$co{'id'};6890# we read 150, we always show 30 and the ones more recent than 48 hours6891if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6892last;6893}6894my%cd= parse_date($co{'author_epoch'});68956896# get list of changed files6897open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6898$co{'parent'} ||"--root",6899$co{'id'},"--", (defined$file_name?$file_name: ())6900ornext;6901my@difftree=map{chomp;$_} <$fd>;6902close$fd6903ornext;69046905# print element (entry, item)6906my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6907if($formateq'rss') {6908print"<item>\n".6909"<title>". esc_html($co{'title'}) ."</title>\n".6910"<author>". esc_html($co{'author'}) ."</author>\n".6911"<pubDate>$cd{'rfc2822'}</pubDate>\n".6912"<guid isPermaLink=\"true\">$co_url</guid>\n".6913"<link>$co_url</link>\n".6914"<description>". esc_html($co{'title'}) ."</description>\n".6915"<content:encoded>".6916"<![CDATA[\n";6917}elsif($formateq'atom') {6918print"<entry>\n".6919"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6920"<updated>$cd{'iso-8601'}</updated>\n".6921"<author>\n".6922" <name>". esc_html($co{'author_name'}) ."</name>\n";6923if($co{'author_email'}) {6924print" <email>". esc_html($co{'author_email'}) ."</email>\n";6925}6926print"</author>\n".6927# use committer for contributor6928"<contributor>\n".6929" <name>". esc_html($co{'committer_name'}) ."</name>\n";6930if($co{'committer_email'}) {6931print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6932}6933print"</contributor>\n".6934"<published>$cd{'iso-8601'}</published>\n".6935"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6936"<id>$co_url</id>\n".6937"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6938"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6939}6940my$comment=$co{'comment'};6941print"<pre>\n";6942foreachmy$line(@$comment) {6943$line= esc_html($line);6944print"$line\n";6945}6946print"</pre><ul>\n";6947foreachmy$difftree_line(@difftree) {6948my%difftree= parse_difftree_raw_line($difftree_line);6949next if!$difftree{'from_id'};69506951my$file=$difftree{'file'} ||$difftree{'to_file'};69526953print"<li>".6954"[".6955$cgi->a({-href => href(-full=>1, action=>"blobdiff",6956 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6957 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6958 file_name=>$file, file_parent=>$difftree{'from_file'}),6959-title =>"diff"},'D');6960if($have_blame) {6961print$cgi->a({-href => href(-full=>1, action=>"blame",6962 file_name=>$file, hash_base=>$commit),6963-title =>"blame"},'B');6964}6965# if this is not a feed of a file history6966if(!defined$file_name||$file_namene$file) {6967print$cgi->a({-href => href(-full=>1, action=>"history",6968 file_name=>$file, hash=>$commit),6969-title =>"history"},'H');6970}6971$file= esc_path($file);6972print"] ".6973"$file</li>\n";6974}6975if($formateq'rss') {6976print"</ul>]]>\n".6977"</content:encoded>\n".6978"</item>\n";6979}elsif($formateq'atom') {6980print"</ul>\n</div>\n".6981"</content>\n".6982"</entry>\n";6983}6984}69856986# end of feed6987if($formateq'rss') {6988print"</channel>\n</rss>\n";6989}elsif($formateq'atom') {6990print"</feed>\n";6991}6992}69936994sub git_rss {6995 git_feed('rss');6996}69976998sub git_atom {6999 git_feed('atom');7000}70017002sub git_opml {7003my@list= git_get_projects_list();70047005print$cgi->header(7006-type =>'text/xml',7007-charset =>'utf-8',7008-content_disposition =>'inline; filename="opml.xml"');70097010print<<XML;7011<?xml version="1.0" encoding="utf-8"?>7012<opml version="1.0">7013<head>7014 <title>$site_nameOPML Export</title>7015</head>7016<body>7017<outline text="git RSS feeds">7018XML70197020foreachmy$pr(@list) {7021my%proj=%$pr;7022my$head= git_get_head_hash($proj{'path'});7023if(!defined$head) {7024next;7025}7026$git_dir="$projectroot/$proj{'path'}";7027my%co= parse_commit($head);7028if(!%co) {7029next;7030}70317032my$path= esc_html(chop_str($proj{'path'},25,5));7033my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);7034my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);7035print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";7036}7037print<<XML;7038</outline>7039</body>7040</opml>7041XML7042}