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 10use strict; 11use warnings; 12use CGI qw(:standard :escapeHTML -nosticky); 13use CGI::Util qw(unescape); 14use CGI::Carp qw(fatalsToBrowser set_message); 15use Encode; 16use Fcntl ':mode'; 17use File::Find qw(); 18use File::Basename qw(basename); 19binmode STDOUT,':utf8'; 20 21our$t0; 22if(eval{require Time::HiRes;1; }) { 23$t0= [Time::HiRes::gettimeofday()]; 24} 25our$number_of_git_cmds=0; 26 27BEGIN{ 28 CGI->compile()if$ENV{'MOD_PERL'}; 29} 30 31our$version="++GIT_VERSION++"; 32 33our($my_url,$my_uri,$base_url,$path_info,$home_link); 34sub evaluate_uri { 35our$cgi; 36 37our$my_url=$cgi->url(); 38our$my_uri=$cgi->url(-absolute =>1); 39 40# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 41# needed and used only for URLs with nonempty PATH_INFO 42our$base_url=$my_url; 43 44# When the script is used as DirectoryIndex, the URL does not contain the name 45# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 46# have to do it ourselves. We make $path_info global because it's also used 47# later on. 48# 49# Another issue with the script being the DirectoryIndex is that the resulting 50# $my_url data is not the full script URL: this is good, because we want 51# generated links to keep implying the script name if it wasn't explicitly 52# indicated in the URL we're handling, but it means that $my_url cannot be used 53# as base URL. 54# Therefore, if we needed to strip PATH_INFO, then we know that we have 55# to build the base URL ourselves: 56our$path_info=$ENV{"PATH_INFO"}; 57if($path_info) { 58if($my_url=~ s,\Q$path_info\E$,, && 59$my_uri=~ s,\Q$path_info\E$,, && 60defined$ENV{'SCRIPT_NAME'}) { 61$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 62} 63} 64 65# target of the home link on top of all pages 66our$home_link=$my_uri||"/"; 67} 68 69# core git executable to use 70# this can just be "git" if your webserver has a sensible PATH 71our$GIT="++GIT_BINDIR++/git"; 72 73# absolute fs-path which will be prepended to the project path 74#our $projectroot = "/pub/scm"; 75our$projectroot="++GITWEB_PROJECTROOT++"; 76 77# fs traversing limit for getting project list 78# the number is relative to the projectroot 79our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 80 81# string of the home link on top of all pages 82our$home_link_str="++GITWEB_HOME_LINK_STR++"; 83 84# name of your site or organization to appear in page titles 85# replace this with something more descriptive for clearer bookmarks 86our$site_name="++GITWEB_SITENAME++" 87|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 88 89# filename of html text to include at top of each page 90our$site_header="++GITWEB_SITE_HEADER++"; 91# html text to include at home page 92our$home_text="++GITWEB_HOMETEXT++"; 93# filename of html text to include at bottom of each page 94our$site_footer="++GITWEB_SITE_FOOTER++"; 95 96# URI of stylesheets 97our@stylesheets= ("++GITWEB_CSS++"); 98# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 99our$stylesheet=undef; 100# URI of GIT logo (72x27 size) 101our$logo="++GITWEB_LOGO++"; 102# URI of GIT favicon, assumed to be image/png type 103our$favicon="++GITWEB_FAVICON++"; 104# URI of gitweb.js (JavaScript code for gitweb) 105our$javascript="++GITWEB_JS++"; 106 107# URI and label (title) of GIT logo link 108#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 109#our $logo_label = "git documentation"; 110our$logo_url="http://git-scm.com/"; 111our$logo_label="git homepage"; 112 113# source of projects list 114our$projects_list="++GITWEB_LIST++"; 115 116# the width (in characters) of the projects list "Description" column 117our$projects_list_description_width=25; 118 119# default order of projects list 120# valid values are none, project, descr, owner, and age 121our$default_projects_order="project"; 122 123# show repository only if this file exists 124# (only effective if this variable evaluates to true) 125our$export_ok="++GITWEB_EXPORT_OK++"; 126 127# show repository only if this subroutine returns true 128# when given the path to the project, for example: 129# sub { return -e "$_[0]/git-daemon-export-ok"; } 130our$export_auth_hook=undef; 131 132# only allow viewing of repositories also shown on the overview page 133our$strict_export="++GITWEB_STRICT_EXPORT++"; 134 135# list of git base URLs used for URL to where fetch project from, 136# i.e. full URL is "$git_base_url/$project" 137our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 138 139# default blob_plain mimetype and default charset for text/plain blob 140our$default_blob_plain_mimetype='text/plain'; 141our$default_text_plain_charset=undef; 142 143# file to use for guessing MIME types before trying /etc/mime.types 144# (relative to the current git repository) 145our$mimetypes_file=undef; 146 147# assume this charset if line contains non-UTF-8 characters; 148# it should be valid encoding (see Encoding::Supported(3pm) for list), 149# for which encoding all byte sequences are valid, for example 150# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 151# could be even 'utf-8' for the old behavior) 152our$fallback_encoding='latin1'; 153 154# rename detection options for git-diff and git-diff-tree 155# - default is '-M', with the cost proportional to 156# (number of removed files) * (number of new files). 157# - more costly is '-C' (which implies '-M'), with the cost proportional to 158# (number of changed files + number of removed files) * (number of new files) 159# - even more costly is '-C', '--find-copies-harder' with cost 160# (number of files in the original tree) * (number of new files) 161# - one might want to include '-B' option, e.g. '-B', '-M' 162our@diff_opts= ('-M');# taken from git_commit 163 164# Disables features that would allow repository owners to inject script into 165# the gitweb domain. 166our$prevent_xss=0; 167 168# Path to the highlight executable to use (must be the one from 169# http://www.andre-simon.de due to assumptions about parameters and output). 170# Useful if highlight is not installed on your webserver's PATH. 171# [Default: highlight] 172our$highlight_bin="++HIGHLIGHT_BIN++"; 173 174# information about snapshot formats that gitweb is capable of serving 175our%known_snapshot_formats= ( 176# name => { 177# 'display' => display name, 178# 'type' => mime type, 179# 'suffix' => filename suffix, 180# 'format' => --format for git-archive, 181# 'compressor' => [compressor command and arguments] 182# (array reference, optional) 183# 'disabled' => boolean (optional)} 184# 185'tgz'=> { 186'display'=>'tar.gz', 187'type'=>'application/x-gzip', 188'suffix'=>'.tar.gz', 189'format'=>'tar', 190'compressor'=> ['gzip']}, 191 192'tbz2'=> { 193'display'=>'tar.bz2', 194'type'=>'application/x-bzip2', 195'suffix'=>'.tar.bz2', 196'format'=>'tar', 197'compressor'=> ['bzip2']}, 198 199'txz'=> { 200'display'=>'tar.xz', 201'type'=>'application/x-xz', 202'suffix'=>'.tar.xz', 203'format'=>'tar', 204'compressor'=> ['xz'], 205'disabled'=>1}, 206 207'zip'=> { 208'display'=>'zip', 209'type'=>'application/x-zip', 210'suffix'=>'.zip', 211'format'=>'zip'}, 212); 213 214# Aliases so we understand old gitweb.snapshot values in repository 215# configuration. 216our%known_snapshot_format_aliases= ( 217'gzip'=>'tgz', 218'bzip2'=>'tbz2', 219'xz'=>'txz', 220 221# backward compatibility: legacy gitweb config support 222'x-gzip'=>undef,'gz'=>undef, 223'x-bzip2'=>undef,'bz2'=>undef, 224'x-zip'=>undef,''=>undef, 225); 226 227# Pixel sizes for icons and avatars. If the default font sizes or lineheights 228# are changed, it may be appropriate to change these values too via 229# $GITWEB_CONFIG. 230our%avatar_size= ( 231'default'=>16, 232'double'=>32 233); 234 235# Used to set the maximum load that we will still respond to gitweb queries. 236# If server load exceed this value then return "503 server busy" error. 237# If gitweb cannot determined server load, it is taken to be 0. 238# Leave it undefined (or set to 'undef') to turn off load checking. 239our$maxload=300; 240 241# configuration for 'highlight' (http://www.andre-simon.de/) 242# match by basename 243our%highlight_basename= ( 244#'Program' => 'py', 245#'Library' => 'py', 246'SConstruct'=>'py',# SCons equivalent of Makefile 247'Makefile'=>'make', 248); 249# match by extension 250our%highlight_ext= ( 251# main extensions, defining name of syntax; 252# see files in /usr/share/highlight/langDefs/ directory 253map{$_=>$_} 254qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl), 255# alternate extensions, see /etc/highlight/filetypes.conf 256'h'=>'c', 257map{$_=>'cpp'}qw(cxx c++ cc), 258map{$_=>'php'}qw(php3 php4), 259map{$_=>'pl'}qw(perl pm),# perhaps also 'cgi' 260'mak'=>'make', 261map{$_=>'xml'}qw(xhtml html htm), 262); 263 264# You define site-wide feature defaults here; override them with 265# $GITWEB_CONFIG as necessary. 266our%feature= ( 267# feature => { 268# 'sub' => feature-sub (subroutine), 269# 'override' => allow-override (boolean), 270# 'default' => [ default options...] (array reference)} 271# 272# if feature is overridable (it means that allow-override has true value), 273# then feature-sub will be called with default options as parameters; 274# return value of feature-sub indicates if to enable specified feature 275# 276# if there is no 'sub' key (no feature-sub), then feature cannot be 277# overridden 278# 279# use gitweb_get_feature(<feature>) to retrieve the <feature> value 280# (an array) or gitweb_check_feature(<feature>) to check if <feature> 281# is enabled 282 283# Enable the 'blame' blob view, showing the last commit that modified 284# each line in the file. This can be very CPU-intensive. 285 286# To enable system wide have in $GITWEB_CONFIG 287# $feature{'blame'}{'default'} = [1]; 288# To have project specific config enable override in $GITWEB_CONFIG 289# $feature{'blame'}{'override'} = 1; 290# and in project config gitweb.blame = 0|1; 291'blame'=> { 292'sub'=>sub{ feature_bool('blame',@_) }, 293'override'=>0, 294'default'=> [0]}, 295 296# Enable the 'snapshot' link, providing a compressed archive of any 297# tree. This can potentially generate high traffic if you have large 298# project. 299 300# Value is a list of formats defined in %known_snapshot_formats that 301# you wish to offer. 302# To disable system wide have in $GITWEB_CONFIG 303# $feature{'snapshot'}{'default'} = []; 304# To have project specific config enable override in $GITWEB_CONFIG 305# $feature{'snapshot'}{'override'} = 1; 306# and in project config, a comma-separated list of formats or "none" 307# to disable. Example: gitweb.snapshot = tbz2,zip; 308'snapshot'=> { 309'sub'=> \&feature_snapshot, 310'override'=>0, 311'default'=> ['tgz']}, 312 313# Enable text search, which will list the commits which match author, 314# committer or commit text to a given string. Enabled by default. 315# Project specific override is not supported. 316'search'=> { 317'override'=>0, 318'default'=> [1]}, 319 320# Enable grep search, which will list the files in currently selected 321# tree containing the given string. Enabled by default. This can be 322# potentially CPU-intensive, of course. 323 324# To enable system wide have in $GITWEB_CONFIG 325# $feature{'grep'}{'default'} = [1]; 326# To have project specific config enable override in $GITWEB_CONFIG 327# $feature{'grep'}{'override'} = 1; 328# and in project config gitweb.grep = 0|1; 329'grep'=> { 330'sub'=>sub{ feature_bool('grep',@_) }, 331'override'=>0, 332'default'=> [1]}, 333 334# Enable the pickaxe search, which will list the commits that modified 335# a given string in a file. This can be practical and quite faster 336# alternative to 'blame', but still potentially CPU-intensive. 337 338# To enable system wide have in $GITWEB_CONFIG 339# $feature{'pickaxe'}{'default'} = [1]; 340# To have project specific config enable override in $GITWEB_CONFIG 341# $feature{'pickaxe'}{'override'} = 1; 342# and in project config gitweb.pickaxe = 0|1; 343'pickaxe'=> { 344'sub'=>sub{ feature_bool('pickaxe',@_) }, 345'override'=>0, 346'default'=> [1]}, 347 348# Enable showing size of blobs in a 'tree' view, in a separate 349# column, similar to what 'ls -l' does. This cost a bit of IO. 350 351# To disable system wide have in $GITWEB_CONFIG 352# $feature{'show-sizes'}{'default'} = [0]; 353# To have project specific config enable override in $GITWEB_CONFIG 354# $feature{'show-sizes'}{'override'} = 1; 355# and in project config gitweb.showsizes = 0|1; 356'show-sizes'=> { 357'sub'=>sub{ feature_bool('showsizes',@_) }, 358'override'=>0, 359'default'=> [1]}, 360 361# Make gitweb use an alternative format of the URLs which can be 362# more readable and natural-looking: project name is embedded 363# directly in the path and the query string contains other 364# auxiliary information. All gitweb installations recognize 365# URL in either format; this configures in which formats gitweb 366# generates links. 367 368# To enable system wide have in $GITWEB_CONFIG 369# $feature{'pathinfo'}{'default'} = [1]; 370# Project specific override is not supported. 371 372# Note that you will need to change the default location of CSS, 373# favicon, logo and possibly other files to an absolute URL. Also, 374# if gitweb.cgi serves as your indexfile, you will need to force 375# $my_uri to contain the script name in your $GITWEB_CONFIG. 376'pathinfo'=> { 377'override'=>0, 378'default'=> [0]}, 379 380# Make gitweb consider projects in project root subdirectories 381# to be forks of existing projects. Given project $projname.git, 382# projects matching $projname/*.git will not be shown in the main 383# projects list, instead a '+' mark will be added to $projname 384# there and a 'forks' view will be enabled for the project, listing 385# all the forks. If project list is taken from a file, forks have 386# to be listed after the main project. 387 388# To enable system wide have in $GITWEB_CONFIG 389# $feature{'forks'}{'default'} = [1]; 390# Project specific override is not supported. 391'forks'=> { 392'override'=>0, 393'default'=> [0]}, 394 395# Insert custom links to the action bar of all project pages. 396# This enables you mainly to link to third-party scripts integrating 397# into gitweb; e.g. git-browser for graphical history representation 398# or custom web-based repository administration interface. 399 400# The 'default' value consists of a list of triplets in the form 401# (label, link, position) where position is the label after which 402# to insert the link and link is a format string where %n expands 403# to the project name, %f to the project path within the filesystem, 404# %h to the current hash (h gitweb parameter) and %b to the current 405# hash base (hb gitweb parameter); %% expands to %. 406 407# To enable system wide have in $GITWEB_CONFIG e.g. 408# $feature{'actions'}{'default'} = [('graphiclog', 409# '/git-browser/by-commit.html?r=%n', 'summary')]; 410# Project specific override is not supported. 411'actions'=> { 412'override'=>0, 413'default'=> []}, 414 415# Allow gitweb scan project content tags described in ctags/ 416# of project repository, and display the popular Web 2.0-ish 417# "tag cloud" near the project list. Note that this is something 418# COMPLETELY different from the normal Git tags. 419 420# gitweb by itself can show existing tags, but it does not handle 421# tagging itself; you need an external application for that. 422# For an example script, check Girocco's cgi/tagproj.cgi. 423# You may want to install the HTML::TagCloud Perl module to get 424# a pretty tag cloud instead of just a list of tags. 425 426# To enable system wide have in $GITWEB_CONFIG 427# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 428# Project specific override is not supported. 429'ctags'=> { 430'override'=>0, 431'default'=> [0]}, 432 433# The maximum number of patches in a patchset generated in patch 434# view. Set this to 0 or undef to disable patch view, or to a 435# negative number to remove any limit. 436 437# To disable system wide have in $GITWEB_CONFIG 438# $feature{'patches'}{'default'} = [0]; 439# To have project specific config enable override in $GITWEB_CONFIG 440# $feature{'patches'}{'override'} = 1; 441# and in project config gitweb.patches = 0|n; 442# where n is the maximum number of patches allowed in a patchset. 443'patches'=> { 444'sub'=> \&feature_patches, 445'override'=>0, 446'default'=> [16]}, 447 448# Avatar support. When this feature is enabled, views such as 449# shortlog or commit will display an avatar associated with 450# the email of the committer(s) and/or author(s). 451 452# Currently available providers are gravatar and picon. 453# If an unknown provider is specified, the feature is disabled. 454 455# Gravatar depends on Digest::MD5. 456# Picon currently relies on the indiana.edu database. 457 458# To enable system wide have in $GITWEB_CONFIG 459# $feature{'avatar'}{'default'} = ['<provider>']; 460# where <provider> is either gravatar or picon. 461# To have project specific config enable override in $GITWEB_CONFIG 462# $feature{'avatar'}{'override'} = 1; 463# and in project config gitweb.avatar = <provider>; 464'avatar'=> { 465'sub'=> \&feature_avatar, 466'override'=>0, 467'default'=> ['']}, 468 469# Enable displaying how much time and how many git commands 470# it took to generate and display page. Disabled by default. 471# Project specific override is not supported. 472'timed'=> { 473'override'=>0, 474'default'=> [0]}, 475 476# Enable turning some links into links to actions which require 477# JavaScript to run (like 'blame_incremental'). Not enabled by 478# default. Project specific override is currently not supported. 479'javascript-actions'=> { 480'override'=>0, 481'default'=> [0]}, 482 483# Syntax highlighting support. This is based on Daniel Svensson's 484# and Sham Chukoury's work in gitweb-xmms2.git. 485# It requires the 'highlight' program present in $PATH, 486# and therefore is disabled by default. 487 488# To enable system wide have in $GITWEB_CONFIG 489# $feature{'highlight'}{'default'} = [1]; 490 491'highlight'=> { 492'sub'=>sub{ feature_bool('highlight',@_) }, 493'override'=>0, 494'default'=> [0]}, 495); 496 497sub gitweb_get_feature { 498my($name) =@_; 499return unlessexists$feature{$name}; 500my($sub,$override,@defaults) = ( 501$feature{$name}{'sub'}, 502$feature{$name}{'override'}, 503@{$feature{$name}{'default'}}); 504# project specific override is possible only if we have project 505our$git_dir;# global variable, declared later 506if(!$override|| !defined$git_dir) { 507return@defaults; 508} 509if(!defined$sub) { 510warn"feature$nameis not overridable"; 511return@defaults; 512} 513return$sub->(@defaults); 514} 515 516# A wrapper to check if a given feature is enabled. 517# With this, you can say 518# 519# my $bool_feat = gitweb_check_feature('bool_feat'); 520# gitweb_check_feature('bool_feat') or somecode; 521# 522# instead of 523# 524# my ($bool_feat) = gitweb_get_feature('bool_feat'); 525# (gitweb_get_feature('bool_feat'))[0] or somecode; 526# 527sub gitweb_check_feature { 528return(gitweb_get_feature(@_))[0]; 529} 530 531 532sub feature_bool { 533my$key=shift; 534my($val) = git_get_project_config($key,'--bool'); 535 536if(!defined$val) { 537return($_[0]); 538}elsif($valeq'true') { 539return(1); 540}elsif($valeq'false') { 541return(0); 542} 543} 544 545sub feature_snapshot { 546my(@fmts) =@_; 547 548my($val) = git_get_project_config('snapshot'); 549 550if($val) { 551@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 552} 553 554return@fmts; 555} 556 557sub feature_patches { 558my@val= (git_get_project_config('patches','--int')); 559 560if(@val) { 561return@val; 562} 563 564return($_[0]); 565} 566 567sub feature_avatar { 568my@val= (git_get_project_config('avatar')); 569 570return@val?@val:@_; 571} 572 573# checking HEAD file with -e is fragile if the repository was 574# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 575# and then pruned. 576sub check_head_link { 577my($dir) =@_; 578my$headfile="$dir/HEAD"; 579return((-e $headfile) || 580(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 581} 582 583sub check_export_ok { 584my($dir) =@_; 585return(check_head_link($dir) && 586(!$export_ok|| -e "$dir/$export_ok") && 587(!$export_auth_hook||$export_auth_hook->($dir))); 588} 589 590# process alternate names for backward compatibility 591# filter out unsupported (unknown) snapshot formats 592sub filter_snapshot_fmts { 593my@fmts=@_; 594 595@fmts=map{ 596exists$known_snapshot_format_aliases{$_} ? 597$known_snapshot_format_aliases{$_} :$_}@fmts; 598@fmts=grep{ 599exists$known_snapshot_formats{$_} && 600!$known_snapshot_formats{$_}{'disabled'}}@fmts; 601} 602 603our($GITWEB_CONFIG,$GITWEB_CONFIG_SYSTEM); 604sub evaluate_gitweb_config { 605our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 606our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 607# die if there are errors parsing config file 608if(-e $GITWEB_CONFIG) { 609do$GITWEB_CONFIG; 610die$@if$@; 611}elsif(-e $GITWEB_CONFIG_SYSTEM) { 612do$GITWEB_CONFIG_SYSTEM; 613die$@if$@; 614} 615} 616 617# Get loadavg of system, to compare against $maxload. 618# Currently it requires '/proc/loadavg' present to get loadavg; 619# if it is not present it returns 0, which means no load checking. 620sub get_loadavg { 621if( -e '/proc/loadavg'){ 622open my$fd,'<','/proc/loadavg' 623orreturn0; 624my@load=split(/\s+/,scalar<$fd>); 625close$fd; 626 627# The first three columns measure CPU and IO utilization of the last one, 628# five, and 10 minute periods. The fourth column shows the number of 629# currently running processes and the total number of processes in the m/n 630# format. The last column displays the last process ID used. 631return$load[0] ||0; 632} 633# additional checks for load average should go here for things that don't export 634# /proc/loadavg 635 636return0; 637} 638 639# version of the core git binary 640our$git_version; 641sub evaluate_git_version { 642our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 643$number_of_git_cmds++; 644} 645 646sub check_loadavg { 647if(defined$maxload&& get_loadavg() >$maxload) { 648 die_error(503,"The load average on the server is too high"); 649} 650} 651 652# ====================================================================== 653# input validation and dispatch 654 655# input parameters can be collected from a variety of sources (presently, CGI 656# and PATH_INFO), so we define an %input_params hash that collects them all 657# together during validation: this allows subsequent uses (e.g. href()) to be 658# agnostic of the parameter origin 659 660our%input_params= (); 661 662# input parameters are stored with the long parameter name as key. This will 663# also be used in the href subroutine to convert parameters to their CGI 664# equivalent, and since the href() usage is the most frequent one, we store 665# the name -> CGI key mapping here, instead of the reverse. 666# 667# XXX: Warning: If you touch this, check the search form for updating, 668# too. 669 670our@cgi_param_mapping= ( 671 project =>"p", 672 action =>"a", 673 file_name =>"f", 674 file_parent =>"fp", 675 hash =>"h", 676 hash_parent =>"hp", 677 hash_base =>"hb", 678 hash_parent_base =>"hpb", 679 page =>"pg", 680 order =>"o", 681 searchtext =>"s", 682 searchtype =>"st", 683 snapshot_format =>"sf", 684 extra_options =>"opt", 685 search_use_regexp =>"sr", 686# this must be last entry (for manipulation from JavaScript) 687 javascript =>"js" 688); 689our%cgi_param_mapping=@cgi_param_mapping; 690 691# we will also need to know the possible actions, for validation 692our%actions= ( 693"blame"=> \&git_blame, 694"blame_incremental"=> \&git_blame_incremental, 695"blame_data"=> \&git_blame_data, 696"blobdiff"=> \&git_blobdiff, 697"blobdiff_plain"=> \&git_blobdiff_plain, 698"blob"=> \&git_blob, 699"blob_plain"=> \&git_blob_plain, 700"commitdiff"=> \&git_commitdiff, 701"commitdiff_plain"=> \&git_commitdiff_plain, 702"commit"=> \&git_commit, 703"forks"=> \&git_forks, 704"heads"=> \&git_heads, 705"history"=> \&git_history, 706"log"=> \&git_log, 707"patch"=> \&git_patch, 708"patches"=> \&git_patches, 709"rss"=> \&git_rss, 710"atom"=> \&git_atom, 711"search"=> \&git_search, 712"search_help"=> \&git_search_help, 713"shortlog"=> \&git_shortlog, 714"summary"=> \&git_summary, 715"tag"=> \&git_tag, 716"tags"=> \&git_tags, 717"tree"=> \&git_tree, 718"snapshot"=> \&git_snapshot, 719"object"=> \&git_object, 720# those below don't need $project 721"opml"=> \&git_opml, 722"project_list"=> \&git_project_list, 723"project_index"=> \&git_project_index, 724); 725 726# finally, we have the hash of allowed extra_options for the commands that 727# allow them 728our%allowed_options= ( 729"--no-merges"=> [qw(rss atom log shortlog history)], 730); 731 732# fill %input_params with the CGI parameters. All values except for 'opt' 733# should be single values, but opt can be an array. We should probably 734# build an array of parameters that can be multi-valued, but since for the time 735# being it's only this one, we just single it out 736sub evaluate_query_params { 737our$cgi; 738 739while(my($name,$symbol) =each%cgi_param_mapping) { 740if($symboleq'opt') { 741$input_params{$name} = [$cgi->param($symbol) ]; 742}else{ 743$input_params{$name} =$cgi->param($symbol); 744} 745} 746} 747 748# now read PATH_INFO and update the parameter list for missing parameters 749sub evaluate_path_info { 750return ifdefined$input_params{'project'}; 751return if!$path_info; 752$path_info=~ s,^/+,,; 753return if!$path_info; 754 755# find which part of PATH_INFO is project 756my$project=$path_info; 757$project=~ s,/+$,,; 758while($project&& !check_head_link("$projectroot/$project")) { 759$project=~ s,/*[^/]*$,,; 760} 761return unless$project; 762$input_params{'project'} =$project; 763 764# do not change any parameters if an action is given using the query string 765return if$input_params{'action'}; 766$path_info=~ s,^\Q$project\E/*,,; 767 768# next, check if we have an action 769my$action=$path_info; 770$action=~ s,/.*$,,; 771if(exists$actions{$action}) { 772$path_info=~ s,^$action/*,,; 773$input_params{'action'} =$action; 774} 775 776# list of actions that want hash_base instead of hash, but can have no 777# pathname (f) parameter 778my@wants_base= ( 779'tree', 780'history', 781); 782 783# we want to catch, among others 784# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 785my($parentrefname,$parentpathname,$refname,$pathname) = 786($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/); 787 788# first, analyze the 'current' part 789if(defined$pathname) { 790# we got "branch:filename" or "branch:dir/" 791# we could use git_get_type(branch:pathname), but: 792# - it needs $git_dir 793# - it does a git() call 794# - the convention of terminating directories with a slash 795# makes it superfluous 796# - embedding the action in the PATH_INFO would make it even 797# more superfluous 798$pathname=~ s,^/+,,; 799if(!$pathname||substr($pathname, -1)eq"/") { 800$input_params{'action'} ||="tree"; 801$pathname=~ s,/$,,; 802}else{ 803# the default action depends on whether we had parent info 804# or not 805if($parentrefname) { 806$input_params{'action'} ||="blobdiff_plain"; 807}else{ 808$input_params{'action'} ||="blob_plain"; 809} 810} 811$input_params{'hash_base'} ||=$refname; 812$input_params{'file_name'} ||=$pathname; 813}elsif(defined$refname) { 814# we got "branch". In this case we have to choose if we have to 815# set hash or hash_base. 816# 817# Most of the actions without a pathname only want hash to be 818# set, except for the ones specified in @wants_base that want 819# hash_base instead. It should also be noted that hand-crafted 820# links having 'history' as an action and no pathname or hash 821# set will fail, but that happens regardless of PATH_INFO. 822if(defined$parentrefname) { 823# if there is parent let the default be 'shortlog' action 824# (for http://git.example.com/repo.git/A..B links); if there 825# is no parent, dispatch will detect type of object and set 826# action appropriately if required (if action is not set) 827$input_params{'action'} ||="shortlog"; 828} 829if($input_params{'action'} && 830grep{$_eq$input_params{'action'} }@wants_base) { 831$input_params{'hash_base'} ||=$refname; 832}else{ 833$input_params{'hash'} ||=$refname; 834} 835} 836 837# next, handle the 'parent' part, if present 838if(defined$parentrefname) { 839# a missing pathspec defaults to the 'current' filename, allowing e.g. 840# someproject/blobdiff/oldrev..newrev:/filename 841if($parentpathname) { 842$parentpathname=~ s,^/+,,; 843$parentpathname=~ s,/$,,; 844$input_params{'file_parent'} ||=$parentpathname; 845}else{ 846$input_params{'file_parent'} ||=$input_params{'file_name'}; 847} 848# we assume that hash_parent_base is wanted if a path was specified, 849# or if the action wants hash_base instead of hash 850if(defined$input_params{'file_parent'} || 851grep{$_eq$input_params{'action'} }@wants_base) { 852$input_params{'hash_parent_base'} ||=$parentrefname; 853}else{ 854$input_params{'hash_parent'} ||=$parentrefname; 855} 856} 857 858# for the snapshot action, we allow URLs in the form 859# $project/snapshot/$hash.ext 860# where .ext determines the snapshot and gets removed from the 861# passed $refname to provide the $hash. 862# 863# To be able to tell that $refname includes the format extension, we 864# require the following two conditions to be satisfied: 865# - the hash input parameter MUST have been set from the $refname part 866# of the URL (i.e. they must be equal) 867# - the snapshot format MUST NOT have been defined already (e.g. from 868# CGI parameter sf) 869# It's also useless to try any matching unless $refname has a dot, 870# so we check for that too 871if(defined$input_params{'action'} && 872$input_params{'action'}eq'snapshot'&& 873defined$refname&&index($refname,'.') != -1&& 874$refnameeq$input_params{'hash'} && 875!defined$input_params{'snapshot_format'}) { 876# We loop over the known snapshot formats, checking for 877# extensions. Allowed extensions are both the defined suffix 878# (which includes the initial dot already) and the snapshot 879# format key itself, with a prepended dot 880while(my($fmt,$opt) =each%known_snapshot_formats) { 881my$hash=$refname; 882unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 883next; 884} 885my$sfx=$1; 886# a valid suffix was found, so set the snapshot format 887# and reset the hash parameter 888$input_params{'snapshot_format'} =$fmt; 889$input_params{'hash'} =$hash; 890# we also set the format suffix to the one requested 891# in the URL: this way a request for e.g. .tgz returns 892# a .tgz instead of a .tar.gz 893$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 894last; 895} 896} 897} 898 899our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 900$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 901$searchtext,$search_regexp); 902sub evaluate_and_validate_params { 903our$action=$input_params{'action'}; 904if(defined$action) { 905if(!validate_action($action)) { 906 die_error(400,"Invalid action parameter"); 907} 908} 909 910# parameters which are pathnames 911our$project=$input_params{'project'}; 912if(defined$project) { 913if(!validate_project($project)) { 914undef$project; 915 die_error(404,"No such project"); 916} 917} 918 919our$file_name=$input_params{'file_name'}; 920if(defined$file_name) { 921if(!validate_pathname($file_name)) { 922 die_error(400,"Invalid file parameter"); 923} 924} 925 926our$file_parent=$input_params{'file_parent'}; 927if(defined$file_parent) { 928if(!validate_pathname($file_parent)) { 929 die_error(400,"Invalid file parent parameter"); 930} 931} 932 933# parameters which are refnames 934our$hash=$input_params{'hash'}; 935if(defined$hash) { 936if(!validate_refname($hash)) { 937 die_error(400,"Invalid hash parameter"); 938} 939} 940 941our$hash_parent=$input_params{'hash_parent'}; 942if(defined$hash_parent) { 943if(!validate_refname($hash_parent)) { 944 die_error(400,"Invalid hash parent parameter"); 945} 946} 947 948our$hash_base=$input_params{'hash_base'}; 949if(defined$hash_base) { 950if(!validate_refname($hash_base)) { 951 die_error(400,"Invalid hash base parameter"); 952} 953} 954 955our@extra_options= @{$input_params{'extra_options'}}; 956# @extra_options is always defined, since it can only be (currently) set from 957# CGI, and $cgi->param() returns the empty array in array context if the param 958# is not set 959foreachmy$opt(@extra_options) { 960if(not exists$allowed_options{$opt}) { 961 die_error(400,"Invalid option parameter"); 962} 963if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 964 die_error(400,"Invalid option parameter for this action"); 965} 966} 967 968our$hash_parent_base=$input_params{'hash_parent_base'}; 969if(defined$hash_parent_base) { 970if(!validate_refname($hash_parent_base)) { 971 die_error(400,"Invalid hash parent base parameter"); 972} 973} 974 975# other parameters 976our$page=$input_params{'page'}; 977if(defined$page) { 978if($page=~m/[^0-9]/) { 979 die_error(400,"Invalid page parameter"); 980} 981} 982 983our$searchtype=$input_params{'searchtype'}; 984if(defined$searchtype) { 985if($searchtype=~m/[^a-z]/) { 986 die_error(400,"Invalid searchtype parameter"); 987} 988} 989 990our$search_use_regexp=$input_params{'search_use_regexp'}; 991 992our$searchtext=$input_params{'searchtext'}; 993our$search_regexp; 994if(defined$searchtext) { 995if(length($searchtext) <2) { 996 die_error(403,"At least two characters are required for search parameter"); 997} 998$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 999}1000}10011002# path to the current git repository1003our$git_dir;1004sub evaluate_git_dir {1005our$git_dir="$projectroot/$project"if$project;1006}10071008our(@snapshot_fmts,$git_avatar);1009sub configure_gitweb_features {1010# list of supported snapshot formats1011our@snapshot_fmts= gitweb_get_feature('snapshot');1012@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);10131014# check that the avatar feature is set to a known provider name,1015# and for each provider check if the dependencies are satisfied.1016# if the provider name is invalid or the dependencies are not met,1017# reset $git_avatar to the empty string.1018our($git_avatar) = gitweb_get_feature('avatar');1019if($git_avatareq'gravatar') {1020$git_avatar=''unless(eval{require Digest::MD5;1; });1021}elsif($git_avatareq'picon') {1022# no dependencies1023}else{1024$git_avatar='';1025}1026}10271028# custom error handler: 'die <message>' is Internal Server Error1029sub handle_errors_html {1030my$msg=shift;# it is already HTML escaped10311032# to avoid infinite loop where error occurs in die_error,1033# change handler to default handler, disabling handle_errors_html1034 set_message("Error occured when inside die_error:\n$msg");10351036# you cannot jump out of die_error when called as error handler;1037# the subroutine set via CGI::Carp::set_message is called _after_1038# HTTP headers are already written, so it cannot write them itself1039 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1);1040}1041set_message(\&handle_errors_html);10421043# dispatch1044sub dispatch {1045if(!defined$action) {1046if(defined$hash) {1047$action= git_get_type($hash);1048}elsif(defined$hash_base&&defined$file_name) {1049$action= git_get_type("$hash_base:$file_name");1050}elsif(defined$project) {1051$action='summary';1052}else{1053$action='project_list';1054}1055}1056if(!defined($actions{$action})) {1057 die_error(400,"Unknown action");1058}1059if($action!~m/^(?:opml|project_list|project_index)$/&&1060!$project) {1061 die_error(400,"Project needed");1062}1063$actions{$action}->();1064}10651066sub reset_timer {1067our$t0= [Time::HiRes::gettimeofday()]1068ifdefined$t0;1069our$number_of_git_cmds=0;1070}10711072sub run_request {1073 reset_timer();10741075 evaluate_uri();1076 evaluate_gitweb_config();1077 check_loadavg();10781079# $projectroot and $projects_list might be set in gitweb config file1080$projects_list||=$projectroot;10811082 evaluate_query_params();1083 evaluate_path_info();1084 evaluate_and_validate_params();1085 evaluate_git_dir();10861087 configure_gitweb_features();10881089 dispatch();1090}10911092our$is_last_request=sub{1};1093our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1094our$CGI='CGI';1095our$cgi;1096sub configure_as_fcgi {1097require CGI::Fast;1098our$CGI='CGI::Fast';10991100my$request_number=0;1101# let each child service 100 requests1102our$is_last_request=sub{ ++$request_number>100};1103}1104sub evaluate_argv {1105my$script_name=$ENV{'SCRIPT_NAME'} ||$ENV{'SCRIPT_FILENAME'} || __FILE__;1106 configure_as_fcgi()1107if$script_name=~/\.fcgi$/;11081109return unless(@ARGV);11101111require Getopt::Long;1112 Getopt::Long::GetOptions(1113'fastcgi|fcgi|f'=> \&configure_as_fcgi,1114'nproc|n=i'=>sub{1115my($arg,$val) =@_;1116return unlesseval{require FCGI::ProcManager;1; };1117my$proc_manager= FCGI::ProcManager->new({1118 n_processes =>$val,1119});1120our$pre_listen_hook=sub{$proc_manager->pm_manage() };1121our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1122our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1123},1124);1125}11261127sub run {1128 evaluate_argv();1129 evaluate_git_version();11301131$pre_listen_hook->()1132if$pre_listen_hook;11331134 REQUEST:1135while($cgi=$CGI->new()) {1136$pre_dispatch_hook->()1137if$pre_dispatch_hook;11381139 run_request();11401141$post_dispatch_hook->()1142if$post_dispatch_hook;11431144last REQUEST if($is_last_request->());1145}11461147 DONE_GITWEB:11481;1149}11501151run();11521153if(defined caller) {1154# wrapped in a subroutine processing requests,1155# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1156return;1157}else{1158# pure CGI script, serving single request1159exit;1160}11611162## ======================================================================1163## action links11641165# possible values of extra options1166# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1167# -replay => 1 - start from a current view (replay with modifications)1168# -path_info => 0|1 - don't use/use path_info URL (if possible)1169sub href {1170my%params=@_;1171# default is to use -absolute url() i.e. $my_uri1172my$href=$params{-full} ?$my_url:$my_uri;11731174$params{'project'} =$projectunlessexists$params{'project'};11751176if($params{-replay}) {1177while(my($name,$symbol) =each%cgi_param_mapping) {1178if(!exists$params{$name}) {1179$params{$name} =$input_params{$name};1180}1181}1182}11831184my$use_pathinfo= gitweb_check_feature('pathinfo');1185if(defined$params{'project'} &&1186(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1187# try to put as many parameters as possible in PATH_INFO:1188# - project name1189# - action1190# - hash_parent or hash_parent_base:/file_parent1191# - hash or hash_base:/filename1192# - the snapshot_format as an appropriate suffix11931194# When the script is the root DirectoryIndex for the domain,1195# $href here would be something like http://gitweb.example.com/1196# Thus, we strip any trailing / from $href, to spare us double1197# slashes in the final URL1198$href=~ s,/$,,;11991200# Then add the project name, if present1201$href.="/".esc_url($params{'project'});1202delete$params{'project'};12031204# since we destructively absorb parameters, we keep this1205# boolean that remembers if we're handling a snapshot1206my$is_snapshot=$params{'action'}eq'snapshot';12071208# Summary just uses the project path URL, any other action is1209# added to the URL1210if(defined$params{'action'}) {1211$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1212delete$params{'action'};1213}12141215# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1216# stripping nonexistent or useless pieces1217$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1218||$params{'hash_parent'} ||$params{'hash'});1219if(defined$params{'hash_base'}) {1220if(defined$params{'hash_parent_base'}) {1221$href.= esc_url($params{'hash_parent_base'});1222# skip the file_parent if it's the same as the file_name1223if(defined$params{'file_parent'}) {1224if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1225delete$params{'file_parent'};1226}elsif($params{'file_parent'} !~/\.\./) {1227$href.=":/".esc_url($params{'file_parent'});1228delete$params{'file_parent'};1229}1230}1231$href.="..";1232delete$params{'hash_parent'};1233delete$params{'hash_parent_base'};1234}elsif(defined$params{'hash_parent'}) {1235$href.= esc_url($params{'hash_parent'})."..";1236delete$params{'hash_parent'};1237}12381239$href.= esc_url($params{'hash_base'});1240if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1241$href.=":/".esc_url($params{'file_name'});1242delete$params{'file_name'};1243}1244delete$params{'hash'};1245delete$params{'hash_base'};1246}elsif(defined$params{'hash'}) {1247$href.= esc_url($params{'hash'});1248delete$params{'hash'};1249}12501251# If the action was a snapshot, we can absorb the1252# snapshot_format parameter too1253if($is_snapshot) {1254my$fmt=$params{'snapshot_format'};1255# snapshot_format should always be defined when href()1256# is called, but just in case some code forgets, we1257# fall back to the default1258$fmt||=$snapshot_fmts[0];1259$href.=$known_snapshot_formats{$fmt}{'suffix'};1260delete$params{'snapshot_format'};1261}1262}12631264# now encode the parameters explicitly1265my@result= ();1266for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1267my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1268if(defined$params{$name}) {1269if(ref($params{$name})eq"ARRAY") {1270foreachmy$par(@{$params{$name}}) {1271push@result,$symbol."=". esc_param($par);1272}1273}else{1274push@result,$symbol."=". esc_param($params{$name});1275}1276}1277}1278$href.="?".join(';',@result)ifscalar@result;12791280return$href;1281}128212831284## ======================================================================1285## validation, quoting/unquoting and escaping12861287sub validate_action {1288my$input=shift||returnundef;1289returnundefunlessexists$actions{$input};1290return$input;1291}12921293sub validate_project {1294my$input=shift||returnundef;1295if(!validate_pathname($input) ||1296!(-d "$projectroot/$input") ||1297!check_export_ok("$projectroot/$input") ||1298($strict_export&& !project_in_list($input))) {1299returnundef;1300}else{1301return$input;1302}1303}13041305sub validate_pathname {1306my$input=shift||returnundef;13071308# no '.' or '..' as elements of path, i.e. no '.' nor '..'1309# at the beginning, at the end, and between slashes.1310# also this catches doubled slashes1311if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1312returnundef;1313}1314# no null characters1315if($input=~m!\0!) {1316returnundef;1317}1318return$input;1319}13201321sub validate_refname {1322my$input=shift||returnundef;13231324# textual hashes are O.K.1325if($input=~m/^[0-9a-fA-F]{40}$/) {1326return$input;1327}1328# it must be correct pathname1329$input= validate_pathname($input)1330orreturnundef;1331# restrictions on ref name according to git-check-ref-format1332if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1333returnundef;1334}1335return$input;1336}13371338# decode sequences of octets in utf8 into Perl's internal form,1339# which is utf-8 with utf8 flag set if needed. gitweb writes out1340# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1341sub to_utf8 {1342my$str=shift;1343returnundefunlessdefined$str;1344if(utf8::valid($str)) {1345 utf8::decode($str);1346return$str;1347}else{1348return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1349}1350}13511352# quote unsafe chars, but keep the slash, even when it's not1353# correct, but quoted slashes look too horrible in bookmarks1354sub esc_param {1355my$str=shift;1356returnundefunlessdefined$str;1357$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1358$str=~s/ /\+/g;1359return$str;1360}13611362# quote unsafe chars in whole URL, so some characters cannot be quoted1363sub esc_url {1364my$str=shift;1365returnundefunlessdefined$str;1366$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;1367$str=~s/ /\+/g;1368return$str;1369}13701371# replace invalid utf8 character with SUBSTITUTION sequence1372sub esc_html {1373my$str=shift;1374my%opts=@_;13751376returnundefunlessdefined$str;13771378$str= to_utf8($str);1379$str=$cgi->escapeHTML($str);1380if($opts{'-nbsp'}) {1381$str=~s/ / /g;1382}1383$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1384return$str;1385}13861387# quote control characters and escape filename to HTML1388sub esc_path {1389my$str=shift;1390my%opts=@_;13911392returnundefunlessdefined$str;13931394$str= to_utf8($str);1395$str=$cgi->escapeHTML($str);1396if($opts{'-nbsp'}) {1397$str=~s/ / /g;1398}1399$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1400return$str;1401}14021403# Make control characters "printable", using character escape codes (CEC)1404sub quot_cec {1405my$cntrl=shift;1406my%opts=@_;1407my%es= (# character escape codes, aka escape sequences1408"\t"=>'\t',# tab (HT)1409"\n"=>'\n',# line feed (LF)1410"\r"=>'\r',# carrige return (CR)1411"\f"=>'\f',# form feed (FF)1412"\b"=>'\b',# backspace (BS)1413"\a"=>'\a',# alarm (bell) (BEL)1414"\e"=>'\e',# escape (ESC)1415"\013"=>'\v',# vertical tab (VT)1416"\000"=>'\0',# nul character (NUL)1417);1418my$chr= ( (exists$es{$cntrl})1419?$es{$cntrl}1420:sprintf('\%2x',ord($cntrl)) );1421if($opts{-nohtml}) {1422return$chr;1423}else{1424return"<span class=\"cntrl\">$chr</span>";1425}1426}14271428# Alternatively use unicode control pictures codepoints,1429# Unicode "printable representation" (PR)1430sub quot_upr {1431my$cntrl=shift;1432my%opts=@_;14331434my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1435if($opts{-nohtml}) {1436return$chr;1437}else{1438return"<span class=\"cntrl\">$chr</span>";1439}1440}14411442# git may return quoted and escaped filenames1443sub unquote {1444my$str=shift;14451446sub unq {1447my$seq=shift;1448my%es= (# character escape codes, aka escape sequences1449't'=>"\t",# tab (HT, TAB)1450'n'=>"\n",# newline (NL)1451'r'=>"\r",# return (CR)1452'f'=>"\f",# form feed (FF)1453'b'=>"\b",# backspace (BS)1454'a'=>"\a",# alarm (bell) (BEL)1455'e'=>"\e",# escape (ESC)1456'v'=>"\013",# vertical tab (VT)1457);14581459if($seq=~m/^[0-7]{1,3}$/) {1460# octal char sequence1461returnchr(oct($seq));1462}elsif(exists$es{$seq}) {1463# C escape sequence, aka character escape code1464return$es{$seq};1465}1466# quoted ordinary character1467return$seq;1468}14691470if($str=~m/^"(.*)"$/) {1471# needs unquoting1472$str=$1;1473$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1474}1475return$str;1476}14771478# escape tabs (convert tabs to spaces)1479sub untabify {1480my$line=shift;14811482while((my$pos=index($line,"\t")) != -1) {1483if(my$count= (8- ($pos%8))) {1484my$spaces=' ' x $count;1485$line=~s/\t/$spaces/;1486}1487}14881489return$line;1490}14911492sub project_in_list {1493my$project=shift;1494my@list= git_get_projects_list();1495return@list&&scalar(grep{$_->{'path'}eq$project}@list);1496}14971498## ----------------------------------------------------------------------1499## HTML aware string manipulation15001501# Try to chop given string on a word boundary between position1502# $len and $len+$add_len. If there is no word boundary there,1503# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1504# (marking chopped part) would be longer than given string.1505sub chop_str {1506my$str=shift;1507my$len=shift;1508my$add_len=shift||10;1509my$where=shift||'right';# 'left' | 'center' | 'right'15101511# Make sure perl knows it is utf8 encoded so we don't1512# cut in the middle of a utf8 multibyte char.1513$str= to_utf8($str);15141515# allow only $len chars, but don't cut a word if it would fit in $add_len1516# if it doesn't fit, cut it if it's still longer than the dots we would add1517# remove chopped character entities entirely15181519# when chopping in the middle, distribute $len into left and right part1520# return early if chopping wouldn't make string shorter1521if($whereeq'center') {1522return$strif($len+5>=length($str));# filler is length 51523$len=int($len/2);1524}else{1525return$strif($len+4>=length($str));# filler is length 41526}15271528# regexps: ending and beginning with word part up to $add_len1529my$endre=qr/.{$len}\w{0,$add_len}/;1530my$begre=qr/\w{0,$add_len}.{$len}/;15311532if($whereeq'left') {1533$str=~m/^(.*?)($begre)$/;1534my($lead,$body) = ($1,$2);1535if(length($lead) >4) {1536$lead=" ...";1537}1538return"$lead$body";15391540}elsif($whereeq'center') {1541$str=~m/^($endre)(.*)$/;1542my($left,$str) = ($1,$2);1543$str=~m/^(.*?)($begre)$/;1544my($mid,$right) = ($1,$2);1545if(length($mid) >5) {1546$mid=" ... ";1547}1548return"$left$mid$right";15491550}else{1551$str=~m/^($endre)(.*)$/;1552my$body=$1;1553my$tail=$2;1554if(length($tail) >4) {1555$tail="... ";1556}1557return"$body$tail";1558}1559}15601561# takes the same arguments as chop_str, but also wraps a <span> around the1562# result with a title attribute if it does get chopped. Additionally, the1563# string is HTML-escaped.1564sub chop_and_escape_str {1565my($str) =@_;15661567my$chopped= chop_str(@_);1568if($choppedeq$str) {1569return esc_html($chopped);1570}else{1571$str=~s/[[:cntrl:]]/?/g;1572return$cgi->span({-title=>$str}, esc_html($chopped));1573}1574}15751576## ----------------------------------------------------------------------1577## functions returning short strings15781579# CSS class for given age value (in seconds)1580sub age_class {1581my$age=shift;15821583if(!defined$age) {1584return"noage";1585}elsif($age<60*60*2) {1586return"age0";1587}elsif($age<60*60*24*2) {1588return"age1";1589}else{1590return"age2";1591}1592}15931594# convert age in seconds to "nn units ago" string1595sub age_string {1596my$age=shift;1597my$age_str;15981599if($age>60*60*24*365*2) {1600$age_str= (int$age/60/60/24/365);1601$age_str.=" years ago";1602}elsif($age>60*60*24*(365/12)*2) {1603$age_str=int$age/60/60/24/(365/12);1604$age_str.=" months ago";1605}elsif($age>60*60*24*7*2) {1606$age_str=int$age/60/60/24/7;1607$age_str.=" weeks ago";1608}elsif($age>60*60*24*2) {1609$age_str=int$age/60/60/24;1610$age_str.=" days ago";1611}elsif($age>60*60*2) {1612$age_str=int$age/60/60;1613$age_str.=" hours ago";1614}elsif($age>60*2) {1615$age_str=int$age/60;1616$age_str.=" min ago";1617}elsif($age>2) {1618$age_str=int$age;1619$age_str.=" sec ago";1620}else{1621$age_str.=" right now";1622}1623return$age_str;1624}16251626useconstant{1627 S_IFINVALID =>0030000,1628 S_IFGITLINK =>0160000,1629};16301631# submodule/subproject, a commit object reference1632sub S_ISGITLINK {1633my$mode=shift;16341635return(($mode& S_IFMT) == S_IFGITLINK)1636}16371638# convert file mode in octal to symbolic file mode string1639sub mode_str {1640my$mode=oct shift;16411642if(S_ISGITLINK($mode)) {1643return'm---------';1644}elsif(S_ISDIR($mode& S_IFMT)) {1645return'drwxr-xr-x';1646}elsif(S_ISLNK($mode)) {1647return'lrwxrwxrwx';1648}elsif(S_ISREG($mode)) {1649# git cares only about the executable bit1650if($mode& S_IXUSR) {1651return'-rwxr-xr-x';1652}else{1653return'-rw-r--r--';1654};1655}else{1656return'----------';1657}1658}16591660# convert file mode in octal to file type string1661sub file_type {1662my$mode=shift;16631664if($mode!~m/^[0-7]+$/) {1665return$mode;1666}else{1667$mode=oct$mode;1668}16691670if(S_ISGITLINK($mode)) {1671return"submodule";1672}elsif(S_ISDIR($mode& S_IFMT)) {1673return"directory";1674}elsif(S_ISLNK($mode)) {1675return"symlink";1676}elsif(S_ISREG($mode)) {1677return"file";1678}else{1679return"unknown";1680}1681}16821683# convert file mode in octal to file type description string1684sub file_type_long {1685my$mode=shift;16861687if($mode!~m/^[0-7]+$/) {1688return$mode;1689}else{1690$mode=oct$mode;1691}16921693if(S_ISGITLINK($mode)) {1694return"submodule";1695}elsif(S_ISDIR($mode& S_IFMT)) {1696return"directory";1697}elsif(S_ISLNK($mode)) {1698return"symlink";1699}elsif(S_ISREG($mode)) {1700if($mode& S_IXUSR) {1701return"executable";1702}else{1703return"file";1704};1705}else{1706return"unknown";1707}1708}170917101711## ----------------------------------------------------------------------1712## functions returning short HTML fragments, or transforming HTML fragments1713## which don't belong to other sections17141715# format line of commit message.1716sub format_log_line_html {1717my$line=shift;17181719$line= esc_html($line, -nbsp=>1);1720$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1721$cgi->a({-href => href(action=>"object", hash=>$1),1722-class=>"text"},$1);1723}eg;17241725return$line;1726}17271728# format marker of refs pointing to given object17291730# the destination action is chosen based on object type and current context:1731# - for annotated tags, we choose the tag view unless it's the current view1732# already, in which case we go to shortlog view1733# - for other refs, we keep the current view if we're in history, shortlog or1734# log view, and select shortlog otherwise1735sub format_ref_marker {1736my($refs,$id) =@_;1737my$markers='';17381739if(defined$refs->{$id}) {1740foreachmy$ref(@{$refs->{$id}}) {1741# this code exploits the fact that non-lightweight tags are the1742# only indirect objects, and that they are the only objects for which1743# we want to use tag instead of shortlog as action1744my($type,$name) =qw();1745my$indirect= ($ref=~s/\^\{\}$//);1746# e.g. tags/v2.6.11 or heads/next1747if($ref=~m!^(.*?)s?/(.*)$!) {1748$type=$1;1749$name=$2;1750}else{1751$type="ref";1752$name=$ref;1753}17541755my$class=$type;1756$class.=" indirect"if$indirect;17571758my$dest_action="shortlog";17591760if($indirect) {1761$dest_action="tag"unless$actioneq"tag";1762}elsif($action=~/^(history|(short)?log)$/) {1763$dest_action=$action;1764}17651766my$dest="";1767$dest.="refs/"unless$ref=~ m!^refs/!;1768$dest.=$ref;17691770my$link=$cgi->a({1771-href => href(1772 action=>$dest_action,1773 hash=>$dest1774)},$name);17751776$markers.=" <span class=\"$class\"title=\"$ref\">".1777$link."</span>";1778}1779}17801781if($markers) {1782return' <span class="refs">'.$markers.'</span>';1783}else{1784return"";1785}1786}17871788# format, perhaps shortened and with markers, title line1789sub format_subject_html {1790my($long,$short,$href,$extra) =@_;1791$extra=''unlessdefined($extra);17921793if(length($short) <length($long)) {1794$long=~s/[[:cntrl:]]/?/g;1795return$cgi->a({-href =>$href, -class=>"list subject",1796-title => to_utf8($long)},1797 esc_html($short)) .$extra;1798}else{1799return$cgi->a({-href =>$href, -class=>"list subject"},1800 esc_html($long)) .$extra;1801}1802}18031804# Rather than recomputing the url for an email multiple times, we cache it1805# after the first hit. This gives a visible benefit in views where the avatar1806# for the same email is used repeatedly (e.g. shortlog).1807# The cache is shared by all avatar engines (currently gravatar only), which1808# are free to use it as preferred. Since only one avatar engine is used for any1809# given page, there's no risk for cache conflicts.1810our%avatar_cache= ();18111812# Compute the picon url for a given email, by using the picon search service over at1813# http://www.cs.indiana.edu/picons/search.html1814sub picon_url {1815my$email=lc shift;1816if(!$avatar_cache{$email}) {1817my($user,$domain) =split('@',$email);1818$avatar_cache{$email} =1819"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1820"$domain/$user/".1821"users+domains+unknown/up/single";1822}1823return$avatar_cache{$email};1824}18251826# Compute the gravatar url for a given email, if it's not in the cache already.1827# Gravatar stores only the part of the URL before the size, since that's the1828# one computationally more expensive. This also allows reuse of the cache for1829# different sizes (for this particular engine).1830sub gravatar_url {1831my$email=lc shift;1832my$size=shift;1833$avatar_cache{$email} ||=1834"http://www.gravatar.com/avatar/".1835 Digest::MD5::md5_hex($email) ."?s=";1836return$avatar_cache{$email} .$size;1837}18381839# Insert an avatar for the given $email at the given $size if the feature1840# is enabled.1841sub git_get_avatar {1842my($email,%opts) =@_;1843my$pre_white= ($opts{-pad_before} ?" ":"");1844my$post_white= ($opts{-pad_after} ?" ":"");1845$opts{-size} ||='default';1846my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1847my$url="";1848if($git_avatareq'gravatar') {1849$url= gravatar_url($email,$size);1850}elsif($git_avatareq'picon') {1851$url= picon_url($email);1852}1853# Other providers can be added by extending the if chain, defining $url1854# as needed. If no variant puts something in $url, we assume avatars1855# are completely disabled/unavailable.1856if($url) {1857return$pre_white.1858"<img width=\"$size\"".1859"class=\"avatar\"".1860"src=\"$url\"".1861"alt=\"\"".1862"/>".$post_white;1863}else{1864return"";1865}1866}18671868sub format_search_author {1869my($author,$searchtype,$displaytext) =@_;1870my$have_search= gitweb_check_feature('search');18711872if($have_search) {1873my$performed="";1874if($searchtypeeq'author') {1875$performed="authored";1876}elsif($searchtypeeq'committer') {1877$performed="committed";1878}18791880return$cgi->a({-href => href(action=>"search", hash=>$hash,1881 searchtext=>$author,1882 searchtype=>$searchtype),class=>"list",1883 title=>"Search for commits$performedby$author"},1884$displaytext);18851886}else{1887return$displaytext;1888}1889}18901891# format the author name of the given commit with the given tag1892# the author name is chopped and escaped according to the other1893# optional parameters (see chop_str).1894sub format_author_html {1895my$tag=shift;1896my$co=shift;1897my$author= chop_and_escape_str($co->{'author_name'},@_);1898return"<$tagclass=\"author\">".1899 format_search_author($co->{'author_name'},"author",1900 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1901$author) .1902"</$tag>";1903}19041905# format git diff header line, i.e. "diff --(git|combined|cc) ..."1906sub format_git_diff_header_line {1907my$line=shift;1908my$diffinfo=shift;1909my($from,$to) =@_;19101911if($diffinfo->{'nparents'}) {1912# combined diff1913$line=~s!^(diff (.*?) )"?.*$!$1!;1914if($to->{'href'}) {1915$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1916 esc_path($to->{'file'}));1917}else{# file was deleted (no href)1918$line.= esc_path($to->{'file'});1919}1920}else{1921# "ordinary" diff1922$line=~s!^(diff (.*?) )"?a/.*$!$1!;1923if($from->{'href'}) {1924$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1925'a/'. esc_path($from->{'file'}));1926}else{# file was added (no href)1927$line.='a/'. esc_path($from->{'file'});1928}1929$line.=' ';1930if($to->{'href'}) {1931$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1932'b/'. esc_path($to->{'file'}));1933}else{# file was deleted1934$line.='b/'. esc_path($to->{'file'});1935}1936}19371938return"<div class=\"diff header\">$line</div>\n";1939}19401941# format extended diff header line, before patch itself1942sub format_extended_diff_header_line {1943my$line=shift;1944my$diffinfo=shift;1945my($from,$to) =@_;19461947# match <path>1948if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1949$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1950 esc_path($from->{'file'}));1951}1952if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1953$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1954 esc_path($to->{'file'}));1955}1956# match single <mode>1957if($line=~m/\s(\d{6})$/) {1958$line.='<span class="info"> ('.1959 file_type_long($1) .1960')</span>';1961}1962# match <hash>1963if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1964# can match only for combined diff1965$line='index ';1966for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1967if($from->{'href'}[$i]) {1968$line.=$cgi->a({-href=>$from->{'href'}[$i],1969-class=>"hash"},1970substr($diffinfo->{'from_id'}[$i],0,7));1971}else{1972$line.='0' x 7;1973}1974# separator1975$line.=','if($i<$diffinfo->{'nparents'} -1);1976}1977$line.='..';1978if($to->{'href'}) {1979$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1980substr($diffinfo->{'to_id'},0,7));1981}else{1982$line.='0' x 7;1983}19841985}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1986# can match only for ordinary diff1987my($from_link,$to_link);1988if($from->{'href'}) {1989$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1990substr($diffinfo->{'from_id'},0,7));1991}else{1992$from_link='0' x 7;1993}1994if($to->{'href'}) {1995$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1996substr($diffinfo->{'to_id'},0,7));1997}else{1998$to_link='0' x 7;1999}2000my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});2001$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;2002}20032004return$line."<br/>\n";2005}20062007# format from-file/to-file diff header2008sub format_diff_from_to_header {2009my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;2010my$line;2011my$result='';20122013$line=$from_line;2014#assert($line =~ m/^---/) if DEBUG;2015# no extra formatting for "^--- /dev/null"2016if(!$diffinfo->{'nparents'}) {2017# ordinary (single parent) diff2018if($line=~m!^--- "?a/!) {2019if($from->{'href'}) {2020$line='--- a/'.2021$cgi->a({-href=>$from->{'href'}, -class=>"path"},2022 esc_path($from->{'file'}));2023}else{2024$line='--- a/'.2025 esc_path($from->{'file'});2026}2027}2028$result.= qq!<div class="diff from_file">$line</div>\n!;20292030}else{2031# combined diff (merge commit)2032for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2033if($from->{'href'}[$i]) {2034$line='--- '.2035$cgi->a({-href=>href(action=>"blobdiff",2036 hash_parent=>$diffinfo->{'from_id'}[$i],2037 hash_parent_base=>$parents[$i],2038 file_parent=>$from->{'file'}[$i],2039 hash=>$diffinfo->{'to_id'},2040 hash_base=>$hash,2041 file_name=>$to->{'file'}),2042-class=>"path",2043-title=>"diff". ($i+1)},2044$i+1) .2045'/'.2046$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2047 esc_path($from->{'file'}[$i]));2048}else{2049$line='--- /dev/null';2050}2051$result.= qq!<div class="diff from_file">$line</div>\n!;2052}2053}20542055$line=$to_line;2056#assert($line =~ m/^\+\+\+/) if DEBUG;2057# no extra formatting for "^+++ /dev/null"2058if($line=~m!^\+\+\+ "?b/!) {2059if($to->{'href'}) {2060$line='+++ b/'.2061$cgi->a({-href=>$to->{'href'}, -class=>"path"},2062 esc_path($to->{'file'}));2063}else{2064$line='+++ b/'.2065 esc_path($to->{'file'});2066}2067}2068$result.= qq!<div class="diff to_file">$line</div>\n!;20692070return$result;2071}20722073# create note for patch simplified by combined diff2074sub format_diff_cc_simplified {2075my($diffinfo,@parents) =@_;2076my$result='';20772078$result.="<div class=\"diff header\">".2079"diff --cc ";2080if(!is_deleted($diffinfo)) {2081$result.=$cgi->a({-href => href(action=>"blob",2082 hash_base=>$hash,2083 hash=>$diffinfo->{'to_id'},2084 file_name=>$diffinfo->{'to_file'}),2085-class=>"path"},2086 esc_path($diffinfo->{'to_file'}));2087}else{2088$result.= esc_path($diffinfo->{'to_file'});2089}2090$result.="</div>\n".# class="diff header"2091"<div class=\"diff nodifferences\">".2092"Simple merge".2093"</div>\n";# class="diff nodifferences"20942095return$result;2096}20972098# format patch (diff) line (not to be used for diff headers)2099sub format_diff_line {2100my$line=shift;2101my($from,$to) =@_;2102my$diff_class="";21032104chomp$line;21052106if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2107# combined diff2108my$prefix=substr($line,0,scalar@{$from->{'href'}});2109if($line=~m/^\@{3}/) {2110$diff_class=" chunk_header";2111}elsif($line=~m/^\\/) {2112$diff_class=" incomplete";2113}elsif($prefix=~tr/+/+/) {2114$diff_class=" add";2115}elsif($prefix=~tr/-/-/) {2116$diff_class=" rem";2117}2118}else{2119# assume ordinary diff2120my$char=substr($line,0,1);2121if($chareq'+') {2122$diff_class=" add";2123}elsif($chareq'-') {2124$diff_class=" rem";2125}elsif($chareq'@') {2126$diff_class=" chunk_header";2127}elsif($chareq"\\") {2128$diff_class=" incomplete";2129}2130}2131$line= untabify($line);2132if($from&&$to&&$line=~m/^\@{2} /) {2133my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2134$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;21352136$from_lines=0unlessdefined$from_lines;2137$to_lines=0unlessdefined$to_lines;21382139if($from->{'href'}) {2140$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2141-class=>"list"},$from_text);2142}2143if($to->{'href'}) {2144$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2145-class=>"list"},$to_text);2146}2147$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2148"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2149return"<div class=\"diff$diff_class\">$line</div>\n";2150}elsif($from&&$to&&$line=~m/^\@{3}/) {2151my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2152my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);21532154@from_text=split(' ',$ranges);2155for(my$i=0;$i<@from_text; ++$i) {2156($from_start[$i],$from_nlines[$i]) =2157(split(',',substr($from_text[$i],1)),0);2158}21592160$to_text=pop@from_text;2161$to_start=pop@from_start;2162$to_nlines=pop@from_nlines;21632164$line="<span class=\"chunk_info\">$prefix";2165for(my$i=0;$i<@from_text; ++$i) {2166if($from->{'href'}[$i]) {2167$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2168-class=>"list"},$from_text[$i]);2169}else{2170$line.=$from_text[$i];2171}2172$line.=" ";2173}2174if($to->{'href'}) {2175$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2176-class=>"list"},$to_text);2177}else{2178$line.=$to_text;2179}2180$line.="$prefix</span>".2181"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2182return"<div class=\"diff$diff_class\">$line</div>\n";2183}2184return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2185}21862187# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2188# linked. Pass the hash of the tree/commit to snapshot.2189sub format_snapshot_links {2190my($hash) =@_;2191my$num_fmts=@snapshot_fmts;2192if($num_fmts>1) {2193# A parenthesized list of links bearing format names.2194# e.g. "snapshot (_tar.gz_ _zip_)"2195return"snapshot (".join(' ',map2196$cgi->a({2197-href => href(2198 action=>"snapshot",2199 hash=>$hash,2200 snapshot_format=>$_2201)2202},$known_snapshot_formats{$_}{'display'})2203,@snapshot_fmts) .")";2204}elsif($num_fmts==1) {2205# A single "snapshot" link whose tooltip bears the format name.2206# i.e. "_snapshot_"2207my($fmt) =@snapshot_fmts;2208return2209$cgi->a({2210-href => href(2211 action=>"snapshot",2212 hash=>$hash,2213 snapshot_format=>$fmt2214),2215-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2216},"snapshot");2217}else{# $num_fmts == 02218returnundef;2219}2220}22212222## ......................................................................2223## functions returning values to be passed, perhaps after some2224## transformation, to other functions; e.g. returning arguments to href()22252226# returns hash to be passed to href to generate gitweb URL2227# in -title key it returns description of link2228sub get_feed_info {2229my$format=shift||'Atom';2230my%res= (action =>lc($format));22312232# feed links are possible only for project views2233return unless(defined$project);2234# some views should link to OPML, or to generic project feed,2235# or don't have specific feed yet (so they should use generic)2236return if($action=~/^(?:tags|heads|forks|tag|search)$/x);22372238my$branch;2239# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2240# from tag links; this also makes possible to detect branch links2241if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2242(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2243$branch=$1;2244}2245# find log type for feed description (title)2246my$type='log';2247if(defined$file_name) {2248$type="history of$file_name";2249$type.="/"if($actioneq'tree');2250$type.=" on '$branch'"if(defined$branch);2251}else{2252$type="log of$branch"if(defined$branch);2253}22542255$res{-title} =$type;2256$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2257$res{'file_name'} =$file_name;22582259return%res;2260}22612262## ----------------------------------------------------------------------2263## git utility subroutines, invoking git commands22642265# returns path to the core git executable and the --git-dir parameter as list2266sub git_cmd {2267$number_of_git_cmds++;2268return$GIT,'--git-dir='.$git_dir;2269}22702271# quote the given arguments for passing them to the shell2272# quote_command("command", "arg 1", "arg with ' and ! characters")2273# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2274# Try to avoid using this function wherever possible.2275sub quote_command {2276returnjoin(' ',2277map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2278}22792280# get HEAD ref of given project as hash2281sub git_get_head_hash {2282return git_get_full_hash(shift,'HEAD');2283}22842285sub git_get_full_hash {2286return git_get_hash(@_);2287}22882289sub git_get_short_hash {2290return git_get_hash(@_,'--short=7');2291}22922293sub git_get_hash {2294my($project,$hash,@options) =@_;2295my$o_git_dir=$git_dir;2296my$retval=undef;2297$git_dir="$projectroot/$project";2298if(open my$fd,'-|', git_cmd(),'rev-parse',2299'--verify','-q',@options,$hash) {2300$retval= <$fd>;2301chomp$retvalifdefined$retval;2302close$fd;2303}2304if(defined$o_git_dir) {2305$git_dir=$o_git_dir;2306}2307return$retval;2308}23092310# get type of given object2311sub git_get_type {2312my$hash=shift;23132314open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2315my$type= <$fd>;2316close$fdorreturn;2317chomp$type;2318return$type;2319}23202321# repository configuration2322our$config_file='';2323our%config;23242325# store multiple values for single key as anonymous array reference2326# single values stored directly in the hash, not as [ <value> ]2327sub hash_set_multi {2328my($hash,$key,$value) =@_;23292330if(!exists$hash->{$key}) {2331$hash->{$key} =$value;2332}elsif(!ref$hash->{$key}) {2333$hash->{$key} = [$hash->{$key},$value];2334}else{2335push@{$hash->{$key}},$value;2336}2337}23382339# return hash of git project configuration2340# optionally limited to some section, e.g. 'gitweb'2341sub git_parse_project_config {2342my$section_regexp=shift;2343my%config;23442345local$/="\0";23462347open my$fh,"-|", git_cmd(),"config",'-z','-l',2348orreturn;23492350while(my$keyval= <$fh>) {2351chomp$keyval;2352my($key,$value) =split(/\n/,$keyval,2);23532354 hash_set_multi(\%config,$key,$value)2355if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2356}2357close$fh;23582359return%config;2360}23612362# convert config value to boolean: 'true' or 'false'2363# no value, number > 0, 'true' and 'yes' values are true2364# rest of values are treated as false (never as error)2365sub config_to_bool {2366my$val=shift;23672368return1if!defined$val;# section.key23692370# strip leading and trailing whitespace2371$val=~s/^\s+//;2372$val=~s/\s+$//;23732374return(($val=~/^\d+$/&&$val) ||# section.key = 12375($val=~/^(?:true|yes)$/i));# section.key = true2376}23772378# convert config value to simple decimal number2379# an optional value suffix of 'k', 'm', or 'g' will cause the value2380# to be multiplied by 1024, 1048576, or 10737418242381sub config_to_int {2382my$val=shift;23832384# strip leading and trailing whitespace2385$val=~s/^\s+//;2386$val=~s/\s+$//;23872388if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2389$unit=lc($unit);2390# unknown unit is treated as 12391return$num* ($uniteq'g'?1073741824:2392$uniteq'm'?1048576:2393$uniteq'k'?1024:1);2394}2395return$val;2396}23972398# convert config value to array reference, if needed2399sub config_to_multi {2400my$val=shift;24012402returnref($val) ?$val: (defined($val) ? [$val] : []);2403}24042405sub git_get_project_config {2406my($key,$type) =@_;24072408return unlessdefined$git_dir;24092410# key sanity check2411return unless($key);2412$key=~s/^gitweb\.//;2413return if($key=~m/\W/);24142415# type sanity check2416if(defined$type) {2417$type=~s/^--//;2418$type=undef2419unless($typeeq'bool'||$typeeq'int');2420}24212422# get config2423if(!defined$config_file||2424$config_filene"$git_dir/config") {2425%config= git_parse_project_config('gitweb');2426$config_file="$git_dir/config";2427}24282429# check if config variable (key) exists2430return unlessexists$config{"gitweb.$key"};24312432# ensure given type2433if(!defined$type) {2434return$config{"gitweb.$key"};2435}elsif($typeeq'bool') {2436# backward compatibility: 'git config --bool' returns true/false2437return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2438}elsif($typeeq'int') {2439return config_to_int($config{"gitweb.$key"});2440}2441return$config{"gitweb.$key"};2442}24432444# get hash of given path at given ref2445sub git_get_hash_by_path {2446my$base=shift;2447my$path=shift||returnundef;2448my$type=shift;24492450$path=~ s,/+$,,;24512452open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2453or die_error(500,"Open git-ls-tree failed");2454my$line= <$fd>;2455close$fdorreturnundef;24562457if(!defined$line) {2458# there is no tree or hash given by $path at $base2459returnundef;2460}24612462#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2463$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2464if(defined$type&&$typene$2) {2465# type doesn't match2466returnundef;2467}2468return$3;2469}24702471# get path of entry with given hash at given tree-ish (ref)2472# used to get 'from' filename for combined diff (merge commit) for renames2473sub git_get_path_by_hash {2474my$base=shift||return;2475my$hash=shift||return;24762477local$/="\0";24782479open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2480orreturnundef;2481while(my$line= <$fd>) {2482chomp$line;24832484#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2485#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2486if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2487close$fd;2488return$1;2489}2490}2491close$fd;2492returnundef;2493}24942495## ......................................................................2496## git utility functions, directly accessing git repository24972498sub git_get_project_description {2499my$path=shift;25002501$git_dir="$projectroot/$path";2502open my$fd,'<',"$git_dir/description"2503orreturn git_get_project_config('description');2504my$descr= <$fd>;2505close$fd;2506if(defined$descr) {2507chomp$descr;2508}2509return$descr;2510}25112512sub git_get_project_ctags {2513my$path=shift;2514my$ctags= {};25152516$git_dir="$projectroot/$path";2517opendir my$dh,"$git_dir/ctags"2518orreturn$ctags;2519foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2520open my$ct,'<',$_ornext;2521my$val= <$ct>;2522chomp$val;2523close$ct;2524my$ctag=$_;$ctag=~ s#.*/##;2525$ctags->{$ctag} =$val;2526}2527closedir$dh;2528$ctags;2529}25302531sub git_populate_project_tagcloud {2532my$ctags=shift;25332534# First, merge different-cased tags; tags vote on casing2535my%ctags_lc;2536foreach(keys%$ctags) {2537$ctags_lc{lc$_}->{count} +=$ctags->{$_};2538if(not$ctags_lc{lc$_}->{topcount}2539or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2540$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2541$ctags_lc{lc$_}->{topname} =$_;2542}2543}25442545my$cloud;2546if(eval{require HTML::TagCloud;1; }) {2547$cloud= HTML::TagCloud->new;2548foreach(sort keys%ctags_lc) {2549# Pad the title with spaces so that the cloud looks2550# less crammed.2551my$title=$ctags_lc{$_}->{topname};2552$title=~s/ / /g;2553$title=~s/^/ /g;2554$title=~s/$/ /g;2555$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2556}2557}else{2558$cloud= \%ctags_lc;2559}2560$cloud;2561}25622563sub git_show_project_tagcloud {2564my($cloud,$count) =@_;2565print STDERR ref($cloud)."..\n";2566if(ref$cloudeq'HTML::TagCloud') {2567return$cloud->html_and_css($count);2568}else{2569my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2570return'<p align="center">'.join(', ',map{2571"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2572}splice(@tags,0,$count)) .'</p>';2573}2574}25752576sub git_get_project_url_list {2577my$path=shift;25782579$git_dir="$projectroot/$path";2580open my$fd,'<',"$git_dir/cloneurl"2581orreturnwantarray?2582@{ config_to_multi(git_get_project_config('url')) } :2583 config_to_multi(git_get_project_config('url'));2584my@git_project_url_list=map{chomp;$_} <$fd>;2585close$fd;25862587returnwantarray?@git_project_url_list: \@git_project_url_list;2588}25892590sub git_get_projects_list {2591my($filter) =@_;2592my@list;25932594$filter||='';2595$filter=~s/\.git$//;25962597my$check_forks= gitweb_check_feature('forks');25982599if(-d $projects_list) {2600# search in directory2601my$dir=$projects_list. ($filter?"/$filter":'');2602# remove the trailing "/"2603$dir=~s!/+$!!;2604my$pfxlen=length("$dir");2605my$pfxdepth= ($dir=~tr!/!!);26062607 File::Find::find({2608 follow_fast =>1,# follow symbolic links2609 follow_skip =>2,# ignore duplicates2610 dangling_symlinks =>0,# ignore dangling symlinks, silently2611 wanted =>sub{2612# global variables2613our$project_maxdepth;2614our$projectroot;2615# skip project-list toplevel, if we get it.2616return if(m!^[/.]$!);2617# only directories can be git repositories2618return unless(-d $_);2619# don't traverse too deep (Find is super slow on os x)2620if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2621$File::Find::prune =1;2622return;2623}26242625my$subdir=substr($File::Find::name,$pfxlen+1);2626# we check related file in $projectroot2627my$path= ($filter?"$filter/":'') .$subdir;2628if(check_export_ok("$projectroot/$path")) {2629push@list, { path =>$path};2630$File::Find::prune =1;2631}2632},2633},"$dir");26342635}elsif(-f $projects_list) {2636# read from file(url-encoded):2637# 'git%2Fgit.git Linus+Torvalds'2638# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2639# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2640my%paths;2641open my$fd,'<',$projects_listorreturn;2642 PROJECT:2643while(my$line= <$fd>) {2644chomp$line;2645my($path,$owner) =split' ',$line;2646$path= unescape($path);2647$owner= unescape($owner);2648if(!defined$path) {2649next;2650}2651if($filterne'') {2652# looking for forks;2653my$pfx=substr($path,0,length($filter));2654if($pfxne$filter) {2655next PROJECT;2656}2657my$sfx=substr($path,length($filter));2658if($sfx!~/^\/.*\.git$/) {2659next PROJECT;2660}2661}elsif($check_forks) {2662 PATH:2663foreachmy$filter(keys%paths) {2664# looking for forks;2665my$pfx=substr($path,0,length($filter));2666if($pfxne$filter) {2667next PATH;2668}2669my$sfx=substr($path,length($filter));2670if($sfx!~/^\/.*\.git$/) {2671next PATH;2672}2673# is a fork, don't include it in2674# the list2675next PROJECT;2676}2677}2678if(check_export_ok("$projectroot/$path")) {2679my$pr= {2680 path =>$path,2681 owner => to_utf8($owner),2682};2683push@list,$pr;2684(my$forks_path=$path) =~s/\.git$//;2685$paths{$forks_path}++;2686}2687}2688close$fd;2689}2690return@list;2691}26922693our$gitweb_project_owner=undef;2694sub git_get_project_list_from_file {26952696return if(defined$gitweb_project_owner);26972698$gitweb_project_owner= {};2699# read from file (url-encoded):2700# 'git%2Fgit.git Linus+Torvalds'2701# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2702# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2703if(-f $projects_list) {2704open(my$fd,'<',$projects_list);2705while(my$line= <$fd>) {2706chomp$line;2707my($pr,$ow) =split' ',$line;2708$pr= unescape($pr);2709$ow= unescape($ow);2710$gitweb_project_owner->{$pr} = to_utf8($ow);2711}2712close$fd;2713}2714}27152716sub git_get_project_owner {2717my$project=shift;2718my$owner;27192720returnundefunless$project;2721$git_dir="$projectroot/$project";27222723if(!defined$gitweb_project_owner) {2724 git_get_project_list_from_file();2725}27262727if(exists$gitweb_project_owner->{$project}) {2728$owner=$gitweb_project_owner->{$project};2729}2730if(!defined$owner){2731$owner= git_get_project_config('owner');2732}2733if(!defined$owner) {2734$owner= get_file_owner("$git_dir");2735}27362737return$owner;2738}27392740sub git_get_last_activity {2741my($path) =@_;2742my$fd;27432744$git_dir="$projectroot/$path";2745open($fd,"-|", git_cmd(),'for-each-ref',2746'--format=%(committer)',2747'--sort=-committerdate',2748'--count=1',2749'refs/heads')orreturn;2750my$most_recent= <$fd>;2751close$fdorreturn;2752if(defined$most_recent&&2753$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2754my$timestamp=$1;2755my$age=time-$timestamp;2756return($age, age_string($age));2757}2758return(undef,undef);2759}27602761sub git_get_references {2762my$type=shift||"";2763my%refs;2764# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112765# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2766open my$fd,"-|", git_cmd(),"show-ref","--dereference",2767($type? ("--","refs/$type") : ())# use -- <pattern> if $type2768orreturn;27692770while(my$line= <$fd>) {2771chomp$line;2772if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2773if(defined$refs{$1}) {2774push@{$refs{$1}},$2;2775}else{2776$refs{$1} = [$2];2777}2778}2779}2780close$fdorreturn;2781return \%refs;2782}27832784sub git_get_rev_name_tags {2785my$hash=shift||returnundef;27862787open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2788orreturn;2789my$name_rev= <$fd>;2790close$fd;27912792if($name_rev=~ m|^$hash tags/(.*)$|) {2793return$1;2794}else{2795# catches also '$hash undefined' output2796returnundef;2797}2798}27992800## ----------------------------------------------------------------------2801## parse to hash functions28022803sub parse_date {2804my$epoch=shift;2805my$tz=shift||"-0000";28062807my%date;2808my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2809my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2810my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2811$date{'hour'} =$hour;2812$date{'minute'} =$min;2813$date{'mday'} =$mday;2814$date{'day'} =$days[$wday];2815$date{'month'} =$months[$mon];2816$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2817$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2818$date{'mday-time'} =sprintf"%d%s%02d:%02d",2819$mday,$months[$mon],$hour,$min;2820$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",28211900+$year,1+$mon,$mday,$hour,$min,$sec;28222823$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2824my$local=$epoch+ ((int$1+ ($2/60)) *3600);2825($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2826$date{'hour_local'} =$hour;2827$date{'minute_local'} =$min;2828$date{'tz_local'} =$tz;2829$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",28301900+$year,$mon+1,$mday,2831$hour,$min,$sec,$tz);2832return%date;2833}28342835sub parse_tag {2836my$tag_id=shift;2837my%tag;2838my@comment;28392840open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2841$tag{'id'} =$tag_id;2842while(my$line= <$fd>) {2843chomp$line;2844if($line=~m/^object ([0-9a-fA-F]{40})$/) {2845$tag{'object'} =$1;2846}elsif($line=~m/^type (.+)$/) {2847$tag{'type'} =$1;2848}elsif($line=~m/^tag (.+)$/) {2849$tag{'name'} =$1;2850}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2851$tag{'author'} =$1;2852$tag{'author_epoch'} =$2;2853$tag{'author_tz'} =$3;2854if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2855$tag{'author_name'} =$1;2856$tag{'author_email'} =$2;2857}else{2858$tag{'author_name'} =$tag{'author'};2859}2860}elsif($line=~m/--BEGIN/) {2861push@comment,$line;2862last;2863}elsif($lineeq"") {2864last;2865}2866}2867push@comment, <$fd>;2868$tag{'comment'} = \@comment;2869close$fdorreturn;2870if(!defined$tag{'name'}) {2871return2872};2873return%tag2874}28752876sub parse_commit_text {2877my($commit_text,$withparents) =@_;2878my@commit_lines=split'\n',$commit_text;2879my%co;28802881pop@commit_lines;# Remove '\0'28822883if(!@commit_lines) {2884return;2885}28862887my$header=shift@commit_lines;2888if($header!~m/^[0-9a-fA-F]{40}/) {2889return;2890}2891($co{'id'},my@parents) =split' ',$header;2892while(my$line=shift@commit_lines) {2893last if$lineeq"\n";2894if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2895$co{'tree'} =$1;2896}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2897push@parents,$1;2898}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2899$co{'author'} = to_utf8($1);2900$co{'author_epoch'} =$2;2901$co{'author_tz'} =$3;2902if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2903$co{'author_name'} =$1;2904$co{'author_email'} =$2;2905}else{2906$co{'author_name'} =$co{'author'};2907}2908}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2909$co{'committer'} = to_utf8($1);2910$co{'committer_epoch'} =$2;2911$co{'committer_tz'} =$3;2912if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2913$co{'committer_name'} =$1;2914$co{'committer_email'} =$2;2915}else{2916$co{'committer_name'} =$co{'committer'};2917}2918}2919}2920if(!defined$co{'tree'}) {2921return;2922};2923$co{'parents'} = \@parents;2924$co{'parent'} =$parents[0];29252926foreachmy$title(@commit_lines) {2927$title=~s/^ //;2928if($titlene"") {2929$co{'title'} = chop_str($title,80,5);2930# remove leading stuff of merges to make the interesting part visible2931if(length($title) >50) {2932$title=~s/^Automatic //;2933$title=~s/^merge (of|with) /Merge ... /i;2934if(length($title) >50) {2935$title=~s/(http|rsync):\/\///;2936}2937if(length($title) >50) {2938$title=~s/(master|www|rsync)\.//;2939}2940if(length($title) >50) {2941$title=~s/kernel.org:?//;2942}2943if(length($title) >50) {2944$title=~s/\/pub\/scm//;2945}2946}2947$co{'title_short'} = chop_str($title,50,5);2948last;2949}2950}2951if(!defined$co{'title'} ||$co{'title'}eq"") {2952$co{'title'} =$co{'title_short'} ='(no commit message)';2953}2954# remove added spaces2955foreachmy$line(@commit_lines) {2956$line=~s/^ //;2957}2958$co{'comment'} = \@commit_lines;29592960my$age=time-$co{'committer_epoch'};2961$co{'age'} =$age;2962$co{'age_string'} = age_string($age);2963my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2964if($age>60*60*24*7*2) {2965$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2966$co{'age_string_age'} =$co{'age_string'};2967}else{2968$co{'age_string_date'} =$co{'age_string'};2969$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2970}2971return%co;2972}29732974sub parse_commit {2975my($commit_id) =@_;2976my%co;29772978local$/="\0";29792980open my$fd,"-|", git_cmd(),"rev-list",2981"--parents",2982"--header",2983"--max-count=1",2984$commit_id,2985"--",2986or die_error(500,"Open git-rev-list failed");2987%co= parse_commit_text(<$fd>,1);2988close$fd;29892990return%co;2991}29922993sub parse_commits {2994my($commit_id,$maxcount,$skip,$filename,@args) =@_;2995my@cos;29962997$maxcount||=1;2998$skip||=0;29993000local$/="\0";30013002open my$fd,"-|", git_cmd(),"rev-list",3003"--header",3004@args,3005("--max-count=".$maxcount),3006("--skip=".$skip),3007@extra_options,3008$commit_id,3009"--",3010($filename? ($filename) : ())3011or die_error(500,"Open git-rev-list failed");3012while(my$line= <$fd>) {3013my%co= parse_commit_text($line);3014push@cos, \%co;3015}3016close$fd;30173018returnwantarray?@cos: \@cos;3019}30203021# parse line of git-diff-tree "raw" output3022sub parse_difftree_raw_line {3023my$line=shift;3024my%res;30253026# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'3027# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'3028if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {3029$res{'from_mode'} =$1;3030$res{'to_mode'} =$2;3031$res{'from_id'} =$3;3032$res{'to_id'} =$4;3033$res{'status'} =$5;3034$res{'similarity'} =$6;3035if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3036($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3037}else{3038$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3039}3040}3041# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3042# combined diff (for merge commit)3043elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3044$res{'nparents'} =length($1);3045$res{'from_mode'} = [split(' ',$2) ];3046$res{'to_mode'} =pop@{$res{'from_mode'}};3047$res{'from_id'} = [split(' ',$3) ];3048$res{'to_id'} =pop@{$res{'from_id'}};3049$res{'status'} = [split('',$4) ];3050$res{'to_file'} = unquote($5);3051}3052# 'c512b523472485aef4fff9e57b229d9d243c967f'3053elsif($line=~m/^([0-9a-fA-F]{40})$/) {3054$res{'commit'} =$1;3055}30563057returnwantarray?%res: \%res;3058}30593060# wrapper: return parsed line of git-diff-tree "raw" output3061# (the argument might be raw line, or parsed info)3062sub parsed_difftree_line {3063my$line_or_ref=shift;30643065if(ref($line_or_ref)eq"HASH") {3066# pre-parsed (or generated by hand)3067return$line_or_ref;3068}else{3069return parse_difftree_raw_line($line_or_ref);3070}3071}30723073# parse line of git-ls-tree output3074sub parse_ls_tree_line {3075my$line=shift;3076my%opts=@_;3077my%res;30783079if($opts{'-l'}) {3080#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3081$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;30823083$res{'mode'} =$1;3084$res{'type'} =$2;3085$res{'hash'} =$3;3086$res{'size'} =$4;3087if($opts{'-z'}) {3088$res{'name'} =$5;3089}else{3090$res{'name'} = unquote($5);3091}3092}else{3093#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3094$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;30953096$res{'mode'} =$1;3097$res{'type'} =$2;3098$res{'hash'} =$3;3099if($opts{'-z'}) {3100$res{'name'} =$4;3101}else{3102$res{'name'} = unquote($4);3103}3104}31053106returnwantarray?%res: \%res;3107}31083109# generates _two_ hashes, references to which are passed as 2 and 3 argument3110sub parse_from_to_diffinfo {3111my($diffinfo,$from,$to,@parents) =@_;31123113if($diffinfo->{'nparents'}) {3114# combined diff3115$from->{'file'} = [];3116$from->{'href'} = [];3117 fill_from_file_info($diffinfo,@parents)3118unlessexists$diffinfo->{'from_file'};3119for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3120$from->{'file'}[$i] =3121defined$diffinfo->{'from_file'}[$i] ?3122$diffinfo->{'from_file'}[$i] :3123$diffinfo->{'to_file'};3124if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3125$from->{'href'}[$i] = href(action=>"blob",3126 hash_base=>$parents[$i],3127 hash=>$diffinfo->{'from_id'}[$i],3128 file_name=>$from->{'file'}[$i]);3129}else{3130$from->{'href'}[$i] =undef;3131}3132}3133}else{3134# ordinary (not combined) diff3135$from->{'file'} =$diffinfo->{'from_file'};3136if($diffinfo->{'status'}ne"A") {# not new (added) file3137$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3138 hash=>$diffinfo->{'from_id'},3139 file_name=>$from->{'file'});3140}else{3141delete$from->{'href'};3142}3143}31443145$to->{'file'} =$diffinfo->{'to_file'};3146if(!is_deleted($diffinfo)) {# file exists in result3147$to->{'href'} = href(action=>"blob", hash_base=>$hash,3148 hash=>$diffinfo->{'to_id'},3149 file_name=>$to->{'file'});3150}else{3151delete$to->{'href'};3152}3153}31543155## ......................................................................3156## parse to array of hashes functions31573158sub git_get_heads_list {3159my$limit=shift;3160my@headslist;31613162open my$fd,'-|', git_cmd(),'for-each-ref',3163($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3164'--format=%(objectname) %(refname) %(subject)%00%(committer)',3165'refs/heads'3166orreturn;3167while(my$line= <$fd>) {3168my%ref_item;31693170chomp$line;3171my($refinfo,$committerinfo) =split(/\0/,$line);3172my($hash,$name,$title) =split(' ',$refinfo,3);3173my($committer,$epoch,$tz) =3174($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3175$ref_item{'fullname'} =$name;3176$name=~s!^refs/heads/!!;31773178$ref_item{'name'} =$name;3179$ref_item{'id'} =$hash;3180$ref_item{'title'} =$title||'(no commit message)';3181$ref_item{'epoch'} =$epoch;3182if($epoch) {3183$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3184}else{3185$ref_item{'age'} ="unknown";3186}31873188push@headslist, \%ref_item;3189}3190close$fd;31913192returnwantarray?@headslist: \@headslist;3193}31943195sub git_get_tags_list {3196my$limit=shift;3197my@tagslist;31983199open my$fd,'-|', git_cmd(),'for-each-ref',3200($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3201'--format=%(objectname) %(objecttype) %(refname) '.3202'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3203'refs/tags'3204orreturn;3205while(my$line= <$fd>) {3206my%ref_item;32073208chomp$line;3209my($refinfo,$creatorinfo) =split(/\0/,$line);3210my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3211my($creator,$epoch,$tz) =3212($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3213$ref_item{'fullname'} =$name;3214$name=~s!^refs/tags/!!;32153216$ref_item{'type'} =$type;3217$ref_item{'id'} =$id;3218$ref_item{'name'} =$name;3219if($typeeq"tag") {3220$ref_item{'subject'} =$title;3221$ref_item{'reftype'} =$reftype;3222$ref_item{'refid'} =$refid;3223}else{3224$ref_item{'reftype'} =$type;3225$ref_item{'refid'} =$id;3226}32273228if($typeeq"tag"||$typeeq"commit") {3229$ref_item{'epoch'} =$epoch;3230if($epoch) {3231$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3232}else{3233$ref_item{'age'} ="unknown";3234}3235}32363237push@tagslist, \%ref_item;3238}3239close$fd;32403241returnwantarray?@tagslist: \@tagslist;3242}32433244## ----------------------------------------------------------------------3245## filesystem-related functions32463247sub get_file_owner {3248my$path=shift;32493250my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3251my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3252if(!defined$gcos) {3253returnundef;3254}3255my$owner=$gcos;3256$owner=~s/[,;].*$//;3257return to_utf8($owner);3258}32593260# assume that file exists3261sub insert_file {3262my$filename=shift;32633264open my$fd,'<',$filename;3265print map{ to_utf8($_) } <$fd>;3266close$fd;3267}32683269## ......................................................................3270## mimetype related functions32713272sub mimetype_guess_file {3273my$filename=shift;3274my$mimemap=shift;3275-r $mimemaporreturnundef;32763277my%mimemap;3278open(my$mh,'<',$mimemap)orreturnundef;3279while(<$mh>) {3280next ifm/^#/;# skip comments3281my($mimetype,$exts) =split(/\t+/);3282if(defined$exts) {3283my@exts=split(/\s+/,$exts);3284foreachmy$ext(@exts) {3285$mimemap{$ext} =$mimetype;3286}3287}3288}3289close($mh);32903291$filename=~/\.([^.]*)$/;3292return$mimemap{$1};3293}32943295sub mimetype_guess {3296my$filename=shift;3297my$mime;3298$filename=~/\./orreturnundef;32993300if($mimetypes_file) {3301my$file=$mimetypes_file;3302if($file!~m!^/!) {# if it is relative path3303# it is relative to project3304$file="$projectroot/$project/$file";3305}3306$mime= mimetype_guess_file($filename,$file);3307}3308$mime||= mimetype_guess_file($filename,'/etc/mime.types');3309return$mime;3310}33113312sub blob_mimetype {3313my$fd=shift;3314my$filename=shift;33153316if($filename) {3317my$mime= mimetype_guess($filename);3318$mimeandreturn$mime;3319}33203321# just in case3322return$default_blob_plain_mimetypeunless$fd;33233324if(-T $fd) {3325return'text/plain';3326}elsif(!$filename) {3327return'application/octet-stream';3328}elsif($filename=~m/\.png$/i) {3329return'image/png';3330}elsif($filename=~m/\.gif$/i) {3331return'image/gif';3332}elsif($filename=~m/\.jpe?g$/i) {3333return'image/jpeg';3334}else{3335return'application/octet-stream';3336}3337}33383339sub blob_contenttype {3340my($fd,$file_name,$type) =@_;33413342$type||= blob_mimetype($fd,$file_name);3343if($typeeq'text/plain'&&defined$default_text_plain_charset) {3344$type.="; charset=$default_text_plain_charset";3345}33463347return$type;3348}33493350# guess file syntax for syntax highlighting; return undef if no highlighting3351# the name of syntax can (in the future) depend on syntax highlighter used3352sub guess_file_syntax {3353my($highlight,$mimetype,$file_name) =@_;3354returnundefunless($highlight&&defined$file_name);3355my$basename= basename($file_name,'.in');3356return$highlight_basename{$basename}3357ifexists$highlight_basename{$basename};33583359$basename=~/\.([^.]*)$/;3360my$ext=$1orreturnundef;3361return$highlight_ext{$ext}3362ifexists$highlight_ext{$ext};33633364returnundef;3365}33663367# run highlighter and return FD of its output,3368# or return original FD if no highlighting3369sub run_highlighter {3370my($fd,$highlight,$syntax) =@_;3371return$fdunless($highlight&&defined$syntax);33723373close$fd3374or die_error(404,"Reading blob failed");3375open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3376 quote_command($highlight_bin).3377" --xhtml --fragment --syntax$syntax|"3378or die_error(500,"Couldn't open file or run syntax highlighter");3379return$fd;3380}33813382## ======================================================================3383## functions printing HTML: header, footer, error page33843385sub get_page_title {3386my$title= to_utf8($site_name);33873388return$titleunless(defined$project);3389$title.=" - ". to_utf8($project);33903391return$titleunless(defined$action);3392$title.="/$action";# $action is US-ASCII (7bit ASCII)33933394return$titleunless(defined$file_name);3395$title.=" - ". esc_path($file_name);3396if($actioneq"tree"&&$file_name!~ m|/$|) {3397$title.="/";3398}33993400return$title;3401}34023403sub git_header_html {3404my$status=shift||"200 OK";3405my$expires=shift;3406my%opts=@_;34073408my$title= get_page_title();3409my$content_type;3410# require explicit support from the UA if we are to send the page as3411# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3412# we have to do this because MSIE sometimes globs '*/*', pretending to3413# support xhtml+xml but choking when it gets what it asked for.3414if(defined$cgi->http('HTTP_ACCEPT') &&3415$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3416$cgi->Accept('application/xhtml+xml') !=0) {3417$content_type='application/xhtml+xml';3418}else{3419$content_type='text/html';3420}3421print$cgi->header(-type=>$content_type, -charset =>'utf-8',3422-status=>$status, -expires =>$expires)3423unless($opts{'-no_http_header'});3424my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3425print<<EOF;3426<?xml version="1.0" encoding="utf-8"?>3427<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3428<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3429<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3430<!-- git core binaries version$git_version-->3431<head>3432<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3433<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3434<meta name="robots" content="index, nofollow"/>3435<title>$title</title>3436EOF3437# the stylesheet, favicon etc urls won't work correctly with path_info3438# unless we set the appropriate base URL3439if($ENV{'PATH_INFO'}) {3440print"<base href=\"".esc_url($base_url)."\"/>\n";3441}3442# print out each stylesheet that exist, providing backwards capability3443# for those people who defined $stylesheet in a config file3444if(defined$stylesheet) {3445print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3446}else{3447foreachmy$stylesheet(@stylesheets) {3448next unless$stylesheet;3449print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3450}3451}3452if(defined$project) {3453my%href_params= get_feed_info();3454if(!exists$href_params{'-title'}) {3455$href_params{'-title'} ='log';3456}34573458foreachmy$formatqw(RSS Atom){3459my$type=lc($format);3460my%link_attr= (3461'-rel'=>'alternate',3462'-title'=>"$project-$href_params{'-title'} -$formatfeed",3463'-type'=>"application/$type+xml"3464);34653466$href_params{'action'} =$type;3467$link_attr{'-href'} = href(%href_params);3468print"<link ".3469"rel=\"$link_attr{'-rel'}\"".3470"title=\"$link_attr{'-title'}\"".3471"href=\"$link_attr{'-href'}\"".3472"type=\"$link_attr{'-type'}\"".3473"/>\n";34743475$href_params{'extra_options'} ='--no-merges';3476$link_attr{'-href'} = href(%href_params);3477$link_attr{'-title'} .=' (no merges)';3478print"<link ".3479"rel=\"$link_attr{'-rel'}\"".3480"title=\"$link_attr{'-title'}\"".3481"href=\"$link_attr{'-href'}\"".3482"type=\"$link_attr{'-type'}\"".3483"/>\n";3484}34853486}else{3487printf('<link rel="alternate" title="%sprojects list" '.3488'href="%s" type="text/plain; charset=utf-8" />'."\n",3489$site_name, href(project=>undef, action=>"project_index"));3490printf('<link rel="alternate" title="%sprojects feeds" '.3491'href="%s" type="text/x-opml" />'."\n",3492$site_name, href(project=>undef, action=>"opml"));3493}3494if(defined$favicon) {3495printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3496}34973498print"</head>\n".3499"<body>\n";35003501if(defined$site_header&& -f $site_header) {3502 insert_file($site_header);3503}35043505print"<div class=\"page_header\">\n".3506$cgi->a({-href => esc_url($logo_url),3507-title =>$logo_label},3508qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3509print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3510if(defined$project) {3511print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3512if(defined$action) {3513print" /$action";3514}3515print"\n";3516}3517print"</div>\n";35183519my$have_search= gitweb_check_feature('search');3520if(defined$project&&$have_search) {3521if(!defined$searchtext) {3522$searchtext="";3523}3524my$search_hash;3525if(defined$hash_base) {3526$search_hash=$hash_base;3527}elsif(defined$hash) {3528$search_hash=$hash;3529}else{3530$search_hash="HEAD";3531}3532my$action=$my_uri;3533my$use_pathinfo= gitweb_check_feature('pathinfo');3534if($use_pathinfo) {3535$action.="/".esc_url($project);3536}3537print$cgi->startform(-method=>"get", -action =>$action) .3538"<div class=\"search\">\n".3539(!$use_pathinfo&&3540$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3541$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3542$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3543$cgi->popup_menu(-name =>'st', -default=>'commit',3544-values=> ['commit','grep','author','committer','pickaxe']) .3545$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3546" search:\n",3547$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3548"<span title=\"Extended regular expression\">".3549$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3550-checked =>$search_use_regexp) .3551"</span>".3552"</div>".3553$cgi->end_form() ."\n";3554}3555}35563557sub git_footer_html {3558my$feed_class='rss_logo';35593560print"<div class=\"page_footer\">\n";3561if(defined$project) {3562my$descr= git_get_project_description($project);3563if(defined$descr) {3564print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3565}35663567my%href_params= get_feed_info();3568if(!%href_params) {3569$feed_class.=' generic';3570}3571$href_params{'-title'} ||='log';35723573foreachmy$formatqw(RSS Atom){3574$href_params{'action'} =lc($format);3575print$cgi->a({-href => href(%href_params),3576-title =>"$href_params{'-title'}$formatfeed",3577-class=>$feed_class},$format)."\n";3578}35793580}else{3581print$cgi->a({-href => href(project=>undef, action=>"opml"),3582-class=>$feed_class},"OPML") ." ";3583print$cgi->a({-href => href(project=>undef, action=>"project_index"),3584-class=>$feed_class},"TXT") ."\n";3585}3586print"</div>\n";# class="page_footer"35873588if(defined$t0&& gitweb_check_feature('timed')) {3589print"<div id=\"generating_info\">\n";3590print'This page took '.3591'<span id="generating_time" class="time_span">'.3592 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3593' seconds </span>'.3594' and '.3595'<span id="generating_cmd">'.3596$number_of_git_cmds.3597'</span> git commands '.3598" to generate.\n";3599print"</div>\n";# class="page_footer"3600}36013602if(defined$site_footer&& -f $site_footer) {3603 insert_file($site_footer);3604}36053606print qq!<script type="text/javascript" src="$javascript"></script>\n!;3607if(defined$action&&3608$actioneq'blame_incremental') {3609print qq!<script type="text/javascript">\n!.3610 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3611 qq!"!. href() .qq!");\n!.3612 qq!</script>\n!;3613}elsif(gitweb_check_feature('javascript-actions')) {3614print qq!<script type="text/javascript">\n!.3615 qq!window.onload = fixLinks;\n!.3616 qq!</script>\n!;3617}36183619print"</body>\n".3620"</html>";3621}36223623# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3624# Example: die_error(404, 'Hash not found')3625# By convention, use the following status codes (as defined in RFC 2616):3626# 400: Invalid or missing CGI parameters, or3627# requested object exists but has wrong type.3628# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3629# this server or project.3630# 404: Requested object/revision/project doesn't exist.3631# 500: The server isn't configured properly, or3632# an internal error occurred (e.g. failed assertions caused by bugs), or3633# an unknown error occurred (e.g. the git binary died unexpectedly).3634# 503: The server is currently unavailable (because it is overloaded,3635# or down for maintenance). Generally, this is a temporary state.3636sub die_error {3637my$status=shift||500;3638my$error= esc_html(shift) ||"Internal Server Error";3639my$extra=shift;3640my%opts=@_;36413642my%http_responses= (3643400=>'400 Bad Request',3644403=>'403 Forbidden',3645404=>'404 Not Found',3646500=>'500 Internal Server Error',3647503=>'503 Service Unavailable',3648);3649 git_header_html($http_responses{$status},undef,%opts);3650print<<EOF;3651<div class="page_body">3652<br /><br />3653$status-$error3654<br />3655EOF3656if(defined$extra) {3657print"<hr />\n".3658"$extra\n";3659}3660print"</div>\n";36613662 git_footer_html();3663goto DONE_GITWEB3664unless($opts{'-error_handler'});3665}36663667## ----------------------------------------------------------------------3668## functions printing or outputting HTML: navigation36693670sub git_print_page_nav {3671my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3672$extra=''if!defined$extra;# pager or formats36733674my@navs=qw(summary shortlog log commit commitdiff tree);3675if($suppress) {3676@navs=grep{$_ne$suppress}@navs;3677}36783679my%arg=map{$_=> {action=>$_} }@navs;3680if(defined$head) {3681for(qw(commit commitdiff)) {3682$arg{$_}{'hash'} =$head;3683}3684if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3685for(qw(shortlog log)) {3686$arg{$_}{'hash'} =$head;3687}3688}3689}36903691$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3692$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;36933694my@actions= gitweb_get_feature('actions');3695my%repl= (3696'%'=>'%',3697'n'=>$project,# project name3698'f'=>$git_dir,# project path within filesystem3699'h'=>$treehead||'',# current hash ('h' parameter)3700'b'=>$treebase||'',# hash base ('hb' parameter)3701);3702while(@actions) {3703my($label,$link,$pos) =splice(@actions,0,3);3704# insert3705@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3706# munch munch3707$link=~s/%([%nfhb])/$repl{$1}/g;3708$arg{$label}{'_href'} =$link;3709}37103711print"<div class=\"page_nav\">\n".3712(join" | ",3713map{$_eq$current?3714$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3715}@navs);3716print"<br/>\n$extra<br/>\n".3717"</div>\n";3718}37193720sub format_paging_nav {3721my($action,$page,$has_next_link) =@_;3722my$paging_nav;372337243725if($page>0) {3726$paging_nav.=3727$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3728" ⋅ ".3729$cgi->a({-href => href(-replay=>1, page=>$page-1),3730-accesskey =>"p", -title =>"Alt-p"},"prev");3731}else{3732$paging_nav.="first ⋅ prev";3733}37343735if($has_next_link) {3736$paging_nav.=" ⋅ ".3737$cgi->a({-href => href(-replay=>1, page=>$page+1),3738-accesskey =>"n", -title =>"Alt-n"},"next");3739}else{3740$paging_nav.=" ⋅ next";3741}37423743return$paging_nav;3744}37453746## ......................................................................3747## functions printing or outputting HTML: div37483749sub git_print_header_div {3750my($action,$title,$hash,$hash_base) =@_;3751my%args= ();37523753$args{'action'} =$action;3754$args{'hash'} =$hashif$hash;3755$args{'hash_base'} =$hash_baseif$hash_base;37563757print"<div class=\"header\">\n".3758$cgi->a({-href => href(%args), -class=>"title"},3759$title?$title:$action) .3760"\n</div>\n";3761}37623763sub print_local_time {3764print format_local_time(@_);3765}37663767sub format_local_time {3768my$localtime='';3769my%date=@_;3770if($date{'hour_local'} <6) {3771$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3772$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3773}else{3774$localtime.=sprintf(" (%02d:%02d%s)",3775$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3776}37773778return$localtime;3779}37803781# Outputs the author name and date in long form3782sub git_print_authorship {3783my$co=shift;3784my%opts=@_;3785my$tag=$opts{-tag} ||'div';3786my$author=$co->{'author_name'};37873788my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3789print"<$tagclass=\"author_date\">".3790 format_search_author($author,"author", esc_html($author)) .3791" [$ad{'rfc2822'}";3792 print_local_time(%ad)if($opts{-localtime});3793print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3794."</$tag>\n";3795}37963797# Outputs table rows containing the full author or committer information,3798# in the format expected for 'commit' view (& similar).3799# Parameters are a commit hash reference, followed by the list of people3800# to output information for. If the list is empty it defaults to both3801# author and committer.3802sub git_print_authorship_rows {3803my$co=shift;3804# too bad we can't use @people = @_ || ('author', 'committer')3805my@people=@_;3806@people= ('author','committer')unless@people;3807foreachmy$who(@people) {3808my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3809print"<tr><td>$who</td><td>".3810 format_search_author($co->{"${who}_name"},$who,3811 esc_html($co->{"${who}_name"})) ." ".3812 format_search_author($co->{"${who}_email"},$who,3813 esc_html("<".$co->{"${who}_email"} .">")) .3814"</td><td rowspan=\"2\">".3815 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3816"</td></tr>\n".3817"<tr>".3818"<td></td><td>$wd{'rfc2822'}";3819 print_local_time(%wd);3820print"</td>".3821"</tr>\n";3822}3823}38243825sub git_print_page_path {3826my$name=shift;3827my$type=shift;3828my$hb=shift;382938303831print"<div class=\"page_path\">";3832print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3833-title =>'tree root'}, to_utf8("[$project]"));3834print" / ";3835if(defined$name) {3836my@dirname=split'/',$name;3837my$basename=pop@dirname;3838my$fullname='';38393840foreachmy$dir(@dirname) {3841$fullname.= ($fullname?'/':'') .$dir;3842print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3843 hash_base=>$hb),3844-title =>$fullname}, esc_path($dir));3845print" / ";3846}3847if(defined$type&&$typeeq'blob') {3848print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3849 hash_base=>$hb),3850-title =>$name}, esc_path($basename));3851}elsif(defined$type&&$typeeq'tree') {3852print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3853 hash_base=>$hb),3854-title =>$name}, esc_path($basename));3855print" / ";3856}else{3857print esc_path($basename);3858}3859}3860print"<br/></div>\n";3861}38623863sub git_print_log {3864my$log=shift;3865my%opts=@_;38663867if($opts{'-remove_title'}) {3868# remove title, i.e. first line of log3869shift@$log;3870}3871# remove leading empty lines3872while(defined$log->[0] &&$log->[0]eq"") {3873shift@$log;3874}38753876# print log3877my$signoff=0;3878my$empty=0;3879foreachmy$line(@$log) {3880if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3881$signoff=1;3882$empty=0;3883if(!$opts{'-remove_signoff'}) {3884print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3885next;3886}else{3887# remove signoff lines3888next;3889}3890}else{3891$signoff=0;3892}38933894# print only one empty line3895# do not print empty line after signoff3896if($lineeq"") {3897next if($empty||$signoff);3898$empty=1;3899}else{3900$empty=0;3901}39023903print format_log_line_html($line) ."<br/>\n";3904}39053906if($opts{'-final_empty_line'}) {3907# end with single empty line3908print"<br/>\n"unless$empty;3909}3910}39113912# return link target (what link points to)3913sub git_get_link_target {3914my$hash=shift;3915my$link_target;39163917# read link3918open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3919orreturn;3920{3921local$/=undef;3922$link_target= <$fd>;3923}3924close$fd3925orreturn;39263927return$link_target;3928}39293930# given link target, and the directory (basedir) the link is in,3931# return target of link relative to top directory (top tree);3932# return undef if it is not possible (including absolute links).3933sub normalize_link_target {3934my($link_target,$basedir) =@_;39353936# absolute symlinks (beginning with '/') cannot be normalized3937return if(substr($link_target,0,1)eq'/');39383939# normalize link target to path from top (root) tree (dir)3940my$path;3941if($basedir) {3942$path=$basedir.'/'.$link_target;3943}else{3944# we are in top (root) tree (dir)3945$path=$link_target;3946}39473948# remove //, /./, and /../3949my@path_parts;3950foreachmy$part(split('/',$path)) {3951# discard '.' and ''3952next if(!$part||$parteq'.');3953# handle '..'3954if($parteq'..') {3955if(@path_parts) {3956pop@path_parts;3957}else{3958# link leads outside repository (outside top dir)3959return;3960}3961}else{3962push@path_parts,$part;3963}3964}3965$path=join('/',@path_parts);39663967return$path;3968}39693970# print tree entry (row of git_tree), but without encompassing <tr> element3971sub git_print_tree_entry {3972my($t,$basedir,$hash_base,$have_blame) =@_;39733974my%base_key= ();3975$base_key{'hash_base'} =$hash_baseifdefined$hash_base;39763977# The format of a table row is: mode list link. Where mode is3978# the mode of the entry, list is the name of the entry, an href,3979# and link is the action links of the entry.39803981print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3982if(exists$t->{'size'}) {3983print"<td class=\"size\">$t->{'size'}</td>\n";3984}3985if($t->{'type'}eq"blob") {3986print"<td class=\"list\">".3987$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3988 file_name=>"$basedir$t->{'name'}",%base_key),3989-class=>"list"}, esc_path($t->{'name'}));3990if(S_ISLNK(oct$t->{'mode'})) {3991my$link_target= git_get_link_target($t->{'hash'});3992if($link_target) {3993my$norm_target= normalize_link_target($link_target,$basedir);3994if(defined$norm_target) {3995print" -> ".3996$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3997 file_name=>$norm_target),3998-title =>$norm_target}, esc_path($link_target));3999}else{4000print" -> ". esc_path($link_target);4001}4002}4003}4004print"</td>\n";4005print"<td class=\"link\">";4006print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4007 file_name=>"$basedir$t->{'name'}",%base_key)},4008"blob");4009if($have_blame) {4010print" | ".4011$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4012 file_name=>"$basedir$t->{'name'}",%base_key)},4013"blame");4014}4015if(defined$hash_base) {4016print" | ".4017$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4018 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4019"history");4020}4021print" | ".4022$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4023 file_name=>"$basedir$t->{'name'}")},4024"raw");4025print"</td>\n";40264027}elsif($t->{'type'}eq"tree") {4028print"<td class=\"list\">";4029print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4030 file_name=>"$basedir$t->{'name'}",4031%base_key)},4032 esc_path($t->{'name'}));4033print"</td>\n";4034print"<td class=\"link\">";4035print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4036 file_name=>"$basedir$t->{'name'}",4037%base_key)},4038"tree");4039if(defined$hash_base) {4040print" | ".4041$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4042 file_name=>"$basedir$t->{'name'}")},4043"history");4044}4045print"</td>\n";4046}else{4047# unknown object: we can only present history for it4048# (this includes 'commit' object, i.e. submodule support)4049print"<td class=\"list\">".4050 esc_path($t->{'name'}) .4051"</td>\n";4052print"<td class=\"link\">";4053if(defined$hash_base) {4054print$cgi->a({-href => href(action=>"history",4055 hash_base=>$hash_base,4056 file_name=>"$basedir$t->{'name'}")},4057"history");4058}4059print"</td>\n";4060}4061}40624063## ......................................................................4064## functions printing large fragments of HTML40654066# get pre-image filenames for merge (combined) diff4067sub fill_from_file_info {4068my($diff,@parents) =@_;40694070$diff->{'from_file'} = [ ];4071$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4072for(my$i=0;$i<$diff->{'nparents'};$i++) {4073if($diff->{'status'}[$i]eq'R'||4074$diff->{'status'}[$i]eq'C') {4075$diff->{'from_file'}[$i] =4076 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4077}4078}40794080return$diff;4081}40824083# is current raw difftree line of file deletion4084sub is_deleted {4085my$diffinfo=shift;40864087return$diffinfo->{'to_id'}eq('0' x 40);4088}40894090# does patch correspond to [previous] difftree raw line4091# $diffinfo - hashref of parsed raw diff format4092# $patchinfo - hashref of parsed patch diff format4093# (the same keys as in $diffinfo)4094sub is_patch_split {4095my($diffinfo,$patchinfo) =@_;40964097returndefined$diffinfo&&defined$patchinfo4098&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4099}410041014102sub git_difftree_body {4103my($difftree,$hash,@parents) =@_;4104my($parent) =$parents[0];4105my$have_blame= gitweb_check_feature('blame');4106print"<div class=\"list_head\">\n";4107if($#{$difftree} >10) {4108print(($#{$difftree} +1) ." files changed:\n");4109}4110print"</div>\n";41114112print"<table class=\"".4113(@parents>1?"combined ":"") .4114"diff_tree\">\n";41154116# header only for combined diff in 'commitdiff' view4117my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4118if($has_header) {4119# table header4120print"<thead><tr>\n".4121"<th></th><th></th>\n";# filename, patchN link4122for(my$i=0;$i<@parents;$i++) {4123my$par=$parents[$i];4124print"<th>".4125$cgi->a({-href => href(action=>"commitdiff",4126 hash=>$hash, hash_parent=>$par),4127-title =>'commitdiff to parent number '.4128($i+1) .': '.substr($par,0,7)},4129$i+1) .4130" </th>\n";4131}4132print"</tr></thead>\n<tbody>\n";4133}41344135my$alternate=1;4136my$patchno=0;4137foreachmy$line(@{$difftree}) {4138my$diff= parsed_difftree_line($line);41394140if($alternate) {4141print"<tr class=\"dark\">\n";4142}else{4143print"<tr class=\"light\">\n";4144}4145$alternate^=1;41464147if(exists$diff->{'nparents'}) {# combined diff41484149 fill_from_file_info($diff,@parents)4150unlessexists$diff->{'from_file'};41514152if(!is_deleted($diff)) {4153# file exists in the result (child) commit4154print"<td>".4155$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4156 file_name=>$diff->{'to_file'},4157 hash_base=>$hash),4158-class=>"list"}, esc_path($diff->{'to_file'})) .4159"</td>\n";4160}else{4161print"<td>".4162 esc_path($diff->{'to_file'}) .4163"</td>\n";4164}41654166if($actioneq'commitdiff') {4167# link to patch4168$patchno++;4169print"<td class=\"link\">".4170$cgi->a({-href =>"#patch$patchno"},"patch") .4171" | ".4172"</td>\n";4173}41744175my$has_history=0;4176my$not_deleted=0;4177for(my$i=0;$i<$diff->{'nparents'};$i++) {4178my$hash_parent=$parents[$i];4179my$from_hash=$diff->{'from_id'}[$i];4180my$from_path=$diff->{'from_file'}[$i];4181my$status=$diff->{'status'}[$i];41824183$has_history||= ($statusne'A');4184$not_deleted||= ($statusne'D');41854186if($statuseq'A') {4187print"<td class=\"link\"align=\"right\"> | </td>\n";4188}elsif($statuseq'D') {4189print"<td class=\"link\">".4190$cgi->a({-href => href(action=>"blob",4191 hash_base=>$hash,4192 hash=>$from_hash,4193 file_name=>$from_path)},4194"blob". ($i+1)) .4195" | </td>\n";4196}else{4197if($diff->{'to_id'}eq$from_hash) {4198print"<td class=\"link nochange\">";4199}else{4200print"<td class=\"link\">";4201}4202print$cgi->a({-href => href(action=>"blobdiff",4203 hash=>$diff->{'to_id'},4204 hash_parent=>$from_hash,4205 hash_base=>$hash,4206 hash_parent_base=>$hash_parent,4207 file_name=>$diff->{'to_file'},4208 file_parent=>$from_path)},4209"diff". ($i+1)) .4210" | </td>\n";4211}4212}42134214print"<td class=\"link\">";4215if($not_deleted) {4216print$cgi->a({-href => href(action=>"blob",4217 hash=>$diff->{'to_id'},4218 file_name=>$diff->{'to_file'},4219 hash_base=>$hash)},4220"blob");4221print" | "if($has_history);4222}4223if($has_history) {4224print$cgi->a({-href => href(action=>"history",4225 file_name=>$diff->{'to_file'},4226 hash_base=>$hash)},4227"history");4228}4229print"</td>\n";42304231print"</tr>\n";4232next;# instead of 'else' clause, to avoid extra indent4233}4234# else ordinary diff42354236my($to_mode_oct,$to_mode_str,$to_file_type);4237my($from_mode_oct,$from_mode_str,$from_file_type);4238if($diff->{'to_mode'}ne('0' x 6)) {4239$to_mode_oct=oct$diff->{'to_mode'};4240if(S_ISREG($to_mode_oct)) {# only for regular file4241$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4242}4243$to_file_type= file_type($diff->{'to_mode'});4244}4245if($diff->{'from_mode'}ne('0' x 6)) {4246$from_mode_oct=oct$diff->{'from_mode'};4247if(S_ISREG($to_mode_oct)) {# only for regular file4248$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4249}4250$from_file_type= file_type($diff->{'from_mode'});4251}42524253if($diff->{'status'}eq"A") {# created4254my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4255$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4256$mode_chng.="]</span>";4257print"<td>";4258print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4259 hash_base=>$hash, file_name=>$diff->{'file'}),4260-class=>"list"}, esc_path($diff->{'file'}));4261print"</td>\n";4262print"<td>$mode_chng</td>\n";4263print"<td class=\"link\">";4264if($actioneq'commitdiff') {4265# link to patch4266$patchno++;4267print$cgi->a({-href =>"#patch$patchno"},"patch");4268print" | ";4269}4270print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4271 hash_base=>$hash, file_name=>$diff->{'file'})},4272"blob");4273print"</td>\n";42744275}elsif($diff->{'status'}eq"D") {# deleted4276my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4277print"<td>";4278print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4279 hash_base=>$parent, file_name=>$diff->{'file'}),4280-class=>"list"}, esc_path($diff->{'file'}));4281print"</td>\n";4282print"<td>$mode_chng</td>\n";4283print"<td class=\"link\">";4284if($actioneq'commitdiff') {4285# link to patch4286$patchno++;4287print$cgi->a({-href =>"#patch$patchno"},"patch");4288print" | ";4289}4290print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4291 hash_base=>$parent, file_name=>$diff->{'file'})},4292"blob") ." | ";4293if($have_blame) {4294print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4295 file_name=>$diff->{'file'})},4296"blame") ." | ";4297}4298print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4299 file_name=>$diff->{'file'})},4300"history");4301print"</td>\n";43024303}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4304my$mode_chnge="";4305if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4306$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4307if($from_file_typene$to_file_type) {4308$mode_chnge.=" from$from_file_typeto$to_file_type";4309}4310if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4311if($from_mode_str&&$to_mode_str) {4312$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4313}elsif($to_mode_str) {4314$mode_chnge.=" mode:$to_mode_str";4315}4316}4317$mode_chnge.="]</span>\n";4318}4319print"<td>";4320print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4321 hash_base=>$hash, file_name=>$diff->{'file'}),4322-class=>"list"}, esc_path($diff->{'file'}));4323print"</td>\n";4324print"<td>$mode_chnge</td>\n";4325print"<td class=\"link\">";4326if($actioneq'commitdiff') {4327# link to patch4328$patchno++;4329print$cgi->a({-href =>"#patch$patchno"},"patch") .4330" | ";4331}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4332# "commit" view and modified file (not onlu mode changed)4333print$cgi->a({-href => href(action=>"blobdiff",4334 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4335 hash_base=>$hash, hash_parent_base=>$parent,4336 file_name=>$diff->{'file'})},4337"diff") .4338" | ";4339}4340print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4341 hash_base=>$hash, file_name=>$diff->{'file'})},4342"blob") ." | ";4343if($have_blame) {4344print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4345 file_name=>$diff->{'file'})},4346"blame") ." | ";4347}4348print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4349 file_name=>$diff->{'file'})},4350"history");4351print"</td>\n";43524353}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4354my%status_name= ('R'=>'moved','C'=>'copied');4355my$nstatus=$status_name{$diff->{'status'}};4356my$mode_chng="";4357if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4358# mode also for directories, so we cannot use $to_mode_str4359$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4360}4361print"<td>".4362$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4363 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4364-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4365"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4366$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4367 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4368-class=>"list"}, esc_path($diff->{'from_file'})) .4369" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4370"<td class=\"link\">";4371if($actioneq'commitdiff') {4372# link to patch4373$patchno++;4374print$cgi->a({-href =>"#patch$patchno"},"patch") .4375" | ";4376}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4377# "commit" view and modified file (not only pure rename or copy)4378print$cgi->a({-href => href(action=>"blobdiff",4379 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4380 hash_base=>$hash, hash_parent_base=>$parent,4381 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4382"diff") .4383" | ";4384}4385print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4386 hash_base=>$parent, file_name=>$diff->{'to_file'})},4387"blob") ." | ";4388if($have_blame) {4389print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4390 file_name=>$diff->{'to_file'})},4391"blame") ." | ";4392}4393print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4394 file_name=>$diff->{'to_file'})},4395"history");4396print"</td>\n";43974398}# we should not encounter Unmerged (U) or Unknown (X) status4399print"</tr>\n";4400}4401print"</tbody>"if$has_header;4402print"</table>\n";4403}44044405sub git_patchset_body {4406my($fd,$difftree,$hash,@hash_parents) =@_;4407my($hash_parent) =$hash_parents[0];44084409my$is_combined= (@hash_parents>1);4410my$patch_idx=0;4411my$patch_number=0;4412my$patch_line;4413my$diffinfo;4414my$to_name;4415my(%from,%to);44164417print"<div class=\"patchset\">\n";44184419# skip to first patch4420while($patch_line= <$fd>) {4421chomp$patch_line;44224423last if($patch_line=~m/^diff /);4424}44254426 PATCH:4427while($patch_line) {44284429# parse "git diff" header line4430if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4431# $1 is from_name, which we do not use4432$to_name= unquote($2);4433$to_name=~s!^b/!!;4434}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4435# $1 is 'cc' or 'combined', which we do not use4436$to_name= unquote($2);4437}else{4438$to_name=undef;4439}44404441# check if current patch belong to current raw line4442# and parse raw git-diff line if needed4443if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4444# this is continuation of a split patch4445print"<div class=\"patch cont\">\n";4446}else{4447# advance raw git-diff output if needed4448$patch_idx++ifdefined$diffinfo;44494450# read and prepare patch information4451$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);44524453# compact combined diff output can have some patches skipped4454# find which patch (using pathname of result) we are at now;4455if($is_combined) {4456while($to_namene$diffinfo->{'to_file'}) {4457print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4458 format_diff_cc_simplified($diffinfo,@hash_parents) .4459"</div>\n";# class="patch"44604461$patch_idx++;4462$patch_number++;44634464last if$patch_idx>$#$difftree;4465$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4466}4467}44684469# modifies %from, %to hashes4470 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);44714472# this is first patch for raw difftree line with $patch_idx index4473# we index @$difftree array from 0, but number patches from 14474print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4475}44764477# git diff header4478#assert($patch_line =~ m/^diff /) if DEBUG;4479#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4480$patch_number++;4481# print "git diff" header4482print format_git_diff_header_line($patch_line,$diffinfo,4483 \%from, \%to);44844485# print extended diff header4486print"<div class=\"diff extended_header\">\n";4487 EXTENDED_HEADER:4488while($patch_line= <$fd>) {4489chomp$patch_line;44904491last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);44924493print format_extended_diff_header_line($patch_line,$diffinfo,4494 \%from, \%to);4495}4496print"</div>\n";# class="diff extended_header"44974498# from-file/to-file diff header4499if(!$patch_line) {4500print"</div>\n";# class="patch"4501last PATCH;4502}4503next PATCH if($patch_line=~m/^diff /);4504#assert($patch_line =~ m/^---/) if DEBUG;45054506my$last_patch_line=$patch_line;4507$patch_line= <$fd>;4508chomp$patch_line;4509#assert($patch_line =~ m/^\+\+\+/) if DEBUG;45104511print format_diff_from_to_header($last_patch_line,$patch_line,4512$diffinfo, \%from, \%to,4513@hash_parents);45144515# the patch itself4516 LINE:4517while($patch_line= <$fd>) {4518chomp$patch_line;45194520next PATCH if($patch_line=~m/^diff /);45214522print format_diff_line($patch_line, \%from, \%to);4523}45244525}continue{4526print"</div>\n";# class="patch"4527}45284529# for compact combined (--cc) format, with chunk and patch simplification4530# the patchset might be empty, but there might be unprocessed raw lines4531for(++$patch_idxif$patch_number>0;4532$patch_idx<@$difftree;4533++$patch_idx) {4534# read and prepare patch information4535$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);45364537# generate anchor for "patch" links in difftree / whatchanged part4538print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4539 format_diff_cc_simplified($diffinfo,@hash_parents) .4540"</div>\n";# class="patch"45414542$patch_number++;4543}45444545if($patch_number==0) {4546if(@hash_parents>1) {4547print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4548}else{4549print"<div class=\"diff nodifferences\">No differences found</div>\n";4550}4551}45524553print"</div>\n";# class="patchset"4554}45554556# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45574558# fills project list info (age, description, owner, forks) for each4559# project in the list, removing invalid projects from returned list4560# NOTE: modifies $projlist, but does not remove entries from it4561sub fill_project_list_info {4562my($projlist,$check_forks) =@_;4563my@projects;45644565my$show_ctags= gitweb_check_feature('ctags');4566 PROJECT:4567foreachmy$pr(@$projlist) {4568my(@activity) = git_get_last_activity($pr->{'path'});4569unless(@activity) {4570next PROJECT;4571}4572($pr->{'age'},$pr->{'age_string'}) =@activity;4573if(!defined$pr->{'descr'}) {4574my$descr= git_get_project_description($pr->{'path'}) ||"";4575$descr= to_utf8($descr);4576$pr->{'descr_long'} =$descr;4577$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4578}4579if(!defined$pr->{'owner'}) {4580$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4581}4582if($check_forks) {4583my$pname=$pr->{'path'};4584if(($pname=~s/\.git$//) &&4585($pname!~/\/$/) &&4586(-d "$projectroot/$pname")) {4587$pr->{'forks'} ="-d$projectroot/$pname";4588}else{4589$pr->{'forks'} =0;4590}4591}4592$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4593push@projects,$pr;4594}45954596return@projects;4597}45984599# print 'sort by' <th> element, generating 'sort by $name' replay link4600# if that order is not selected4601sub print_sort_th {4602print format_sort_th(@_);4603}46044605sub format_sort_th {4606my($name,$order,$header) =@_;4607my$sort_th="";4608$header||=ucfirst($name);46094610if($ordereq$name) {4611$sort_th.="<th>$header</th>\n";4612}else{4613$sort_th.="<th>".4614$cgi->a({-href => href(-replay=>1, order=>$name),4615-class=>"header"},$header) .4616"</th>\n";4617}46184619return$sort_th;4620}46214622sub git_project_list_body {4623# actually uses global variable $project4624my($projlist,$order,$from,$to,$extra,$no_header) =@_;46254626my$check_forks= gitweb_check_feature('forks');4627my@projects= fill_project_list_info($projlist,$check_forks);46284629$order||=$default_projects_order;4630$from=0unlessdefined$from;4631$to=$#projectsif(!defined$to||$#projects<$to);46324633my%order_info= (4634 project => { key =>'path', type =>'str'},4635 descr => { key =>'descr_long', type =>'str'},4636 owner => { key =>'owner', type =>'str'},4637 age => { key =>'age', type =>'num'}4638);4639my$oi=$order_info{$order};4640if($oi->{'type'}eq'str') {4641@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4642}else{4643@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4644}46454646my$show_ctags= gitweb_check_feature('ctags');4647if($show_ctags) {4648my%ctags;4649foreachmy$p(@projects) {4650foreachmy$ct(keys%{$p->{'ctags'}}) {4651$ctags{$ct} +=$p->{'ctags'}->{$ct};4652}4653}4654my$cloud= git_populate_project_tagcloud(\%ctags);4655print git_show_project_tagcloud($cloud,64);4656}46574658print"<table class=\"project_list\">\n";4659unless($no_header) {4660print"<tr>\n";4661if($check_forks) {4662print"<th></th>\n";4663}4664 print_sort_th('project',$order,'Project');4665 print_sort_th('descr',$order,'Description');4666 print_sort_th('owner',$order,'Owner');4667 print_sort_th('age',$order,'Last Change');4668print"<th></th>\n".# for links4669"</tr>\n";4670}4671my$alternate=1;4672my$tagfilter=$cgi->param('by_tag');4673for(my$i=$from;$i<=$to;$i++) {4674my$pr=$projects[$i];46754676next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4677next if$searchtextand not$pr->{'path'} =~/$searchtext/4678and not$pr->{'descr_long'} =~/$searchtext/;4679# Weed out forks or non-matching entries of search4680if($check_forks) {4681my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4682$forkbase="^$forkbase"if$forkbase;4683next ifnot$searchtextand not$tagfilterand$show_ctags4684and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4685}46864687if($alternate) {4688print"<tr class=\"dark\">\n";4689}else{4690print"<tr class=\"light\">\n";4691}4692$alternate^=1;4693if($check_forks) {4694print"<td>";4695if($pr->{'forks'}) {4696print"<!--$pr->{'forks'} -->\n";4697print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4698}4699print"</td>\n";4700}4701print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4702-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4703"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4704-class=>"list", -title =>$pr->{'descr_long'}},4705 esc_html($pr->{'descr'})) ."</td>\n".4706"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4707print"<td class=\"". age_class($pr->{'age'}) ."\">".4708(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4709"<td class=\"link\">".4710$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4711$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4712$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4713$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4714($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4715"</td>\n".4716"</tr>\n";4717}4718if(defined$extra) {4719print"<tr>\n";4720if($check_forks) {4721print"<td></td>\n";4722}4723print"<td colspan=\"5\">$extra</td>\n".4724"</tr>\n";4725}4726print"</table>\n";4727}47284729sub git_log_body {4730# uses global variable $project4731my($commitlist,$from,$to,$refs,$extra) =@_;47324733$from=0unlessdefined$from;4734$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47354736for(my$i=0;$i<=$to;$i++) {4737my%co= %{$commitlist->[$i]};4738next if!%co;4739my$commit=$co{'id'};4740my$ref= format_ref_marker($refs,$commit);4741my%ad= parse_date($co{'author_epoch'});4742 git_print_header_div('commit',4743"<span class=\"age\">$co{'age_string'}</span>".4744 esc_html($co{'title'}) .$ref,4745$commit);4746print"<div class=\"title_text\">\n".4747"<div class=\"log_link\">\n".4748$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4749" | ".4750$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4751" | ".4752$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4753"<br/>\n".4754"</div>\n";4755 git_print_authorship(\%co, -tag =>'span');4756print"<br/>\n</div>\n";47574758print"<div class=\"log_body\">\n";4759 git_print_log($co{'comment'}, -final_empty_line=>1);4760print"</div>\n";4761}4762if($extra) {4763print"<div class=\"page_nav\">\n";4764print"$extra\n";4765print"</div>\n";4766}4767}47684769sub git_shortlog_body {4770# uses global variable $project4771my($commitlist,$from,$to,$refs,$extra) =@_;47724773$from=0unlessdefined$from;4774$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47754776print"<table class=\"shortlog\">\n";4777my$alternate=1;4778for(my$i=$from;$i<=$to;$i++) {4779my%co= %{$commitlist->[$i]};4780my$commit=$co{'id'};4781my$ref= format_ref_marker($refs,$commit);4782if($alternate) {4783print"<tr class=\"dark\">\n";4784}else{4785print"<tr class=\"light\">\n";4786}4787$alternate^=1;4788# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4789print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4790 format_author_html('td', \%co,10) ."<td>";4791print format_subject_html($co{'title'},$co{'title_short'},4792 href(action=>"commit", hash=>$commit),$ref);4793print"</td>\n".4794"<td class=\"link\">".4795$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4796$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4797$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4798my$snapshot_links= format_snapshot_links($commit);4799if(defined$snapshot_links) {4800print" | ".$snapshot_links;4801}4802print"</td>\n".4803"</tr>\n";4804}4805if(defined$extra) {4806print"<tr>\n".4807"<td colspan=\"4\">$extra</td>\n".4808"</tr>\n";4809}4810print"</table>\n";4811}48124813sub git_history_body {4814# Warning: assumes constant type (blob or tree) during history4815my($commitlist,$from,$to,$refs,$extra,4816$file_name,$file_hash,$ftype) =@_;48174818$from=0unlessdefined$from;4819$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});48204821print"<table class=\"history\">\n";4822my$alternate=1;4823for(my$i=$from;$i<=$to;$i++) {4824my%co= %{$commitlist->[$i]};4825if(!%co) {4826next;4827}4828my$commit=$co{'id'};48294830my$ref= format_ref_marker($refs,$commit);48314832if($alternate) {4833print"<tr class=\"dark\">\n";4834}else{4835print"<tr class=\"light\">\n";4836}4837$alternate^=1;4838print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4839# shortlog: format_author_html('td', \%co, 10)4840 format_author_html('td', \%co,15,3) ."<td>";4841# originally git_history used chop_str($co{'title'}, 50)4842print format_subject_html($co{'title'},$co{'title_short'},4843 href(action=>"commit", hash=>$commit),$ref);4844print"</td>\n".4845"<td class=\"link\">".4846$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4847$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");48484849if($ftypeeq'blob') {4850my$blob_current=$file_hash;4851my$blob_parent= git_get_hash_by_path($commit,$file_name);4852if(defined$blob_current&&defined$blob_parent&&4853$blob_currentne$blob_parent) {4854print" | ".4855$cgi->a({-href => href(action=>"blobdiff",4856 hash=>$blob_current, hash_parent=>$blob_parent,4857 hash_base=>$hash_base, hash_parent_base=>$commit,4858 file_name=>$file_name)},4859"diff to current");4860}4861}4862print"</td>\n".4863"</tr>\n";4864}4865if(defined$extra) {4866print"<tr>\n".4867"<td colspan=\"4\">$extra</td>\n".4868"</tr>\n";4869}4870print"</table>\n";4871}48724873sub git_tags_body {4874# uses global variable $project4875my($taglist,$from,$to,$extra) =@_;4876$from=0unlessdefined$from;4877$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);48784879print"<table class=\"tags\">\n";4880my$alternate=1;4881for(my$i=$from;$i<=$to;$i++) {4882my$entry=$taglist->[$i];4883my%tag=%$entry;4884my$comment=$tag{'subject'};4885my$comment_short;4886if(defined$comment) {4887$comment_short= chop_str($comment,30,5);4888}4889if($alternate) {4890print"<tr class=\"dark\">\n";4891}else{4892print"<tr class=\"light\">\n";4893}4894$alternate^=1;4895if(defined$tag{'age'}) {4896print"<td><i>$tag{'age'}</i></td>\n";4897}else{4898print"<td></td>\n";4899}4900print"<td>".4901$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4902-class=>"list name"}, esc_html($tag{'name'})) .4903"</td>\n".4904"<td>";4905if(defined$comment) {4906print format_subject_html($comment,$comment_short,4907 href(action=>"tag", hash=>$tag{'id'}));4908}4909print"</td>\n".4910"<td class=\"selflink\">";4911if($tag{'type'}eq"tag") {4912print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4913}else{4914print" ";4915}4916print"</td>\n".4917"<td class=\"link\">"." | ".4918$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4919if($tag{'reftype'}eq"commit") {4920print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4921" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4922}elsif($tag{'reftype'}eq"blob") {4923print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4924}4925print"</td>\n".4926"</tr>";4927}4928if(defined$extra) {4929print"<tr>\n".4930"<td colspan=\"5\">$extra</td>\n".4931"</tr>\n";4932}4933print"</table>\n";4934}49354936sub git_heads_body {4937# uses global variable $project4938my($headlist,$head,$from,$to,$extra) =@_;4939$from=0unlessdefined$from;4940$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);49414942print"<table class=\"heads\">\n";4943my$alternate=1;4944for(my$i=$from;$i<=$to;$i++) {4945my$entry=$headlist->[$i];4946my%ref=%$entry;4947my$curr=$ref{'id'}eq$head;4948if($alternate) {4949print"<tr class=\"dark\">\n";4950}else{4951print"<tr class=\"light\">\n";4952}4953$alternate^=1;4954print"<td><i>$ref{'age'}</i></td>\n".4955($curr?"<td class=\"current_head\">":"<td>") .4956$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4957-class=>"list name"},esc_html($ref{'name'})) .4958"</td>\n".4959"<td class=\"link\">".4960$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4961$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4962$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4963"</td>\n".4964"</tr>";4965}4966if(defined$extra) {4967print"<tr>\n".4968"<td colspan=\"3\">$extra</td>\n".4969"</tr>\n";4970}4971print"</table>\n";4972}49734974sub git_search_grep_body {4975my($commitlist,$from,$to,$extra) =@_;4976$from=0unlessdefined$from;4977$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);49784979print"<table class=\"commit_search\">\n";4980my$alternate=1;4981for(my$i=$from;$i<=$to;$i++) {4982my%co= %{$commitlist->[$i]};4983if(!%co) {4984next;4985}4986my$commit=$co{'id'};4987if($alternate) {4988print"<tr class=\"dark\">\n";4989}else{4990print"<tr class=\"light\">\n";4991}4992$alternate^=1;4993print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4994 format_author_html('td', \%co,15,5) .4995"<td>".4996$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4997-class=>"list subject"},4998 chop_and_escape_str($co{'title'},50) ."<br/>");4999my$comment=$co{'comment'};5000foreachmy$line(@$comment) {5001if($line=~m/^(.*?)($search_regexp)(.*)$/i) {5002my($lead,$match,$trail) = ($1,$2,$3);5003$match= chop_str($match,70,5,'center');5004my$contextlen=int((80-length($match))/2);5005$contextlen=30if($contextlen>30);5006$lead= chop_str($lead,$contextlen,10,'left');5007$trail= chop_str($trail,$contextlen,10,'right');50085009$lead= esc_html($lead);5010$match= esc_html($match);5011$trail= esc_html($trail);50125013print"$lead<span class=\"match\">$match</span>$trail<br />";5014}5015}5016print"</td>\n".5017"<td class=\"link\">".5018$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5019" | ".5020$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .5021" | ".5022$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5023print"</td>\n".5024"</tr>\n";5025}5026if(defined$extra) {5027print"<tr>\n".5028"<td colspan=\"3\">$extra</td>\n".5029"</tr>\n";5030}5031print"</table>\n";5032}50335034## ======================================================================5035## ======================================================================5036## actions50375038sub git_project_list {5039my$order=$input_params{'order'};5040if(defined$order&&$order!~m/none|project|descr|owner|age/) {5041 die_error(400,"Unknown order parameter");5042}50435044my@list= git_get_projects_list();5045if(!@list) {5046 die_error(404,"No projects found");5047}50485049 git_header_html();5050if(defined$home_text&& -f $home_text) {5051print"<div class=\"index_include\">\n";5052 insert_file($home_text);5053print"</div>\n";5054}5055print$cgi->startform(-method=>"get") .5056"<p class=\"projsearch\">Search:\n".5057$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".5058"</p>".5059$cgi->end_form() ."\n";5060 git_project_list_body(\@list,$order);5061 git_footer_html();5062}50635064sub git_forks {5065my$order=$input_params{'order'};5066if(defined$order&&$order!~m/none|project|descr|owner|age/) {5067 die_error(400,"Unknown order parameter");5068}50695070my@list= git_get_projects_list($project);5071if(!@list) {5072 die_error(404,"No forks found");5073}50745075 git_header_html();5076 git_print_page_nav('','');5077 git_print_header_div('summary',"$projectforks");5078 git_project_list_body(\@list,$order);5079 git_footer_html();5080}50815082sub git_project_index {5083my@projects= git_get_projects_list($project);50845085print$cgi->header(5086-type =>'text/plain',5087-charset =>'utf-8',5088-content_disposition =>'inline; filename="index.aux"');50895090foreachmy$pr(@projects) {5091if(!exists$pr->{'owner'}) {5092$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");5093}50945095my($path,$owner) = ($pr->{'path'},$pr->{'owner'});5096# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '5097$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5098$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5099$path=~s/ /\+/g;5100$owner=~s/ /\+/g;51015102print"$path$owner\n";5103}5104}51055106sub git_summary {5107my$descr= git_get_project_description($project) ||"none";5108my%co= parse_commit("HEAD");5109my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();5110my$head=$co{'id'};51115112my$owner= git_get_project_owner($project);51135114my$refs= git_get_references();5115# These get_*_list functions return one more to allow us to see if5116# there are more ...5117my@taglist= git_get_tags_list(16);5118my@headlist= git_get_heads_list(16);5119my@forklist;5120my$check_forks= gitweb_check_feature('forks');51215122if($check_forks) {5123@forklist= git_get_projects_list($project);5124}51255126 git_header_html();5127 git_print_page_nav('summary','',$head);51285129print"<div class=\"title\"> </div>\n";5130print"<table class=\"projects_list\">\n".5131"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".5132"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";5133if(defined$cd{'rfc2822'}) {5134print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";5135}51365137# use per project git URL list in $projectroot/$project/cloneurl5138# or make project git URL from git base URL and project name5139my$url_tag="URL";5140my@url_list= git_get_project_url_list($project);5141@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;5142foreachmy$git_url(@url_list) {5143next unless$git_url;5144print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";5145$url_tag="";5146}51475148# Tag cloud5149my$show_ctags= gitweb_check_feature('ctags');5150if($show_ctags) {5151my$ctags= git_get_project_ctags($project);5152my$cloud= git_populate_project_tagcloud($ctags);5153print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";5154print"</td>\n<td>"unless%$ctags;5155print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";5156print"</td>\n<td>"if%$ctags;5157print git_show_project_tagcloud($cloud,48);5158print"</td></tr>";5159}51605161print"</table>\n";51625163# If XSS prevention is on, we don't include README.html.5164# TODO: Allow a readme in some safe format.5165if(!$prevent_xss&& -s "$projectroot/$project/README.html") {5166print"<div class=\"title\">readme</div>\n".5167"<div class=\"readme\">\n";5168 insert_file("$projectroot/$project/README.html");5169print"\n</div>\n";# class="readme"5170}51715172# we need to request one more than 16 (0..15) to check if5173# those 16 are all5174my@commitlist=$head? parse_commits($head,17) : ();5175if(@commitlist) {5176 git_print_header_div('shortlog');5177 git_shortlog_body(\@commitlist,0,15,$refs,5178$#commitlist<=15?undef:5179$cgi->a({-href => href(action=>"shortlog")},"..."));5180}51815182if(@taglist) {5183 git_print_header_div('tags');5184 git_tags_body(\@taglist,0,15,5185$#taglist<=15?undef:5186$cgi->a({-href => href(action=>"tags")},"..."));5187}51885189if(@headlist) {5190 git_print_header_div('heads');5191 git_heads_body(\@headlist,$head,0,15,5192$#headlist<=15?undef:5193$cgi->a({-href => href(action=>"heads")},"..."));5194}51955196if(@forklist) {5197 git_print_header_div('forks');5198 git_project_list_body(\@forklist,'age',0,15,5199$#forklist<=15?undef:5200$cgi->a({-href => href(action=>"forks")},"..."),5201'no_header');5202}52035204 git_footer_html();5205}52065207sub git_tag {5208my%tag= parse_tag($hash);52095210if(!%tag) {5211 die_error(404,"Unknown tag object");5212}52135214my$head= git_get_head_hash($project);5215 git_header_html();5216 git_print_page_nav('','',$head,undef,$head);5217 git_print_header_div('commit', esc_html($tag{'name'}),$hash);5218print"<div class=\"title_text\">\n".5219"<table class=\"object_header\">\n".5220"<tr>\n".5221"<td>object</td>\n".5222"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5223$tag{'object'}) ."</td>\n".5224"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5225$tag{'type'}) ."</td>\n".5226"</tr>\n";5227if(defined($tag{'author'})) {5228 git_print_authorship_rows(\%tag,'author');5229}5230print"</table>\n\n".5231"</div>\n";5232print"<div class=\"page_body\">";5233my$comment=$tag{'comment'};5234foreachmy$line(@$comment) {5235chomp$line;5236print esc_html($line, -nbsp=>1) ."<br/>\n";5237}5238print"</div>\n";5239 git_footer_html();5240}52415242sub git_blame_common {5243my$format=shift||'porcelain';5244if($formateq'porcelain'&&$cgi->param('js')) {5245$format='incremental';5246$action='blame_incremental';# for page title etc5247}52485249# permissions5250 gitweb_check_feature('blame')5251or die_error(403,"Blame view not allowed");52525253# error checking5254 die_error(400,"No file name given")unless$file_name;5255$hash_base||= git_get_head_hash($project);5256 die_error(404,"Couldn't find base commit")unless$hash_base;5257my%co= parse_commit($hash_base)5258or die_error(404,"Commit not found");5259my$ftype="blob";5260if(!defined$hash) {5261$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5262or die_error(404,"Error looking up file");5263}else{5264$ftype= git_get_type($hash);5265if($ftype!~"blob") {5266 die_error(400,"Object is not a blob");5267}5268}52695270my$fd;5271if($formateq'incremental') {5272# get file contents (as base)5273open$fd,"-|", git_cmd(),'cat-file','blob',$hash5274or die_error(500,"Open git-cat-file failed");5275}elsif($formateq'data') {5276# run git-blame --incremental5277open$fd,"-|", git_cmd(),"blame","--incremental",5278$hash_base,"--",$file_name5279or die_error(500,"Open git-blame --incremental failed");5280}else{5281# run git-blame --porcelain5282open$fd,"-|", git_cmd(),"blame",'-p',5283$hash_base,'--',$file_name5284or die_error(500,"Open git-blame --porcelain failed");5285}52865287# incremental blame data returns early5288if($formateq'data') {5289print$cgi->header(5290-type=>"text/plain", -charset =>"utf-8",5291-status=>"200 OK");5292local$| =1;# output autoflush5293printwhile<$fd>;5294close$fd5295or print"ERROR$!\n";52965297print'END';5298if(defined$t0&& gitweb_check_feature('timed')) {5299print' '.5300 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5301' '.$number_of_git_cmds;5302}5303print"\n";53045305return;5306}53075308# page header5309 git_header_html();5310my$formats_nav=5311$cgi->a({-href => href(action=>"blob", -replay=>1)},5312"blob") .5313" | ";5314if($formateq'incremental') {5315$formats_nav.=5316$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5317"blame") ." (non-incremental)";5318}else{5319$formats_nav.=5320$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5321"blame") ." (incremental)";5322}5323$formats_nav.=5324" | ".5325$cgi->a({-href => href(action=>"history", -replay=>1)},5326"history") .5327" | ".5328$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5329"HEAD");5330 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5331 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5332 git_print_page_path($file_name,$ftype,$hash_base);53335334# page body5335if($formateq'incremental') {5336print"<noscript>\n<div class=\"error\"><center><b>\n".5337"This page requires JavaScript to run.\nUse ".5338$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5339'this page').5340" instead.\n".5341"</b></center></div>\n</noscript>\n";53425343print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5344}53455346print qq!<div class="page_body">\n!;5347print qq!<div id="progress_info">.../ ...</div>\n!5348if($formateq'incremental');5349print qq!<table id="blame_table"class="blame" width="100%">\n!.5350#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5351 qq!<thead>\n!.5352 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5353 qq!</thead>\n!.5354 qq!<tbody>\n!;53555356my@rev_color=qw(light dark);5357my$num_colors=scalar(@rev_color);5358my$current_color=0;53595360if($formateq'incremental') {5361my$color_class=$rev_color[$current_color];53625363#contents of a file5364my$linenr=0;5365 LINE:5366while(my$line= <$fd>) {5367chomp$line;5368$linenr++;53695370print qq!<tr id="l$linenr"class="$color_class">!.5371 qq!<td class="sha1"><a href=""> </a></td>!.5372 qq!<td class="linenr">!.5373 qq!<a class="linenr" href="">$linenr</a></td>!;5374print qq!<td class="pre">! . esc_html($line) ."</td>\n";5375print qq!</tr>\n!;5376}53775378}else{# porcelain, i.e. ordinary blame5379my%metainfo= ();# saves information about commits53805381# blame data5382 LINE:5383while(my$line= <$fd>) {5384chomp$line;5385# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5386# no <lines in group> for subsequent lines in group of lines5387my($full_rev,$orig_lineno,$lineno,$group_size) =5388($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5389if(!exists$metainfo{$full_rev}) {5390$metainfo{$full_rev} = {'nprevious'=>0};5391}5392my$meta=$metainfo{$full_rev};5393my$data;5394while($data= <$fd>) {5395chomp$data;5396last if($data=~s/^\t//);# contents of line5397if($data=~/^(\S+)(?: (.*))?$/) {5398$meta->{$1} =$2unlessexists$meta->{$1};5399}5400if($data=~/^previous /) {5401$meta->{'nprevious'}++;5402}5403}5404my$short_rev=substr($full_rev,0,8);5405my$author=$meta->{'author'};5406my%date=5407 parse_date($meta->{'author-time'},$meta->{'author-tz'});5408my$date=$date{'iso-tz'};5409if($group_size) {5410$current_color= ($current_color+1) %$num_colors;5411}5412my$tr_class=$rev_color[$current_color];5413$tr_class.=' boundary'if(exists$meta->{'boundary'});5414$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5415$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5416print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5417if($group_size) {5418print"<td class=\"sha1\"";5419print" title=\"". esc_html($author) .",$date\"";5420print" rowspan=\"$group_size\""if($group_size>1);5421print">";5422print$cgi->a({-href => href(action=>"commit",5423 hash=>$full_rev,5424 file_name=>$file_name)},5425 esc_html($short_rev));5426if($group_size>=2) {5427my@author_initials= ($author=~/\b([[:upper:]])\B/g);5428if(@author_initials) {5429print"<br />".5430 esc_html(join('',@author_initials));5431# or join('.', ...)5432}5433}5434print"</td>\n";5435}5436# 'previous' <sha1 of parent commit> <filename at commit>5437if(exists$meta->{'previous'} &&5438$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5439$meta->{'parent'} =$1;5440$meta->{'file_parent'} = unquote($2);5441}5442my$linenr_commit=5443exists($meta->{'parent'}) ?5444$meta->{'parent'} :$full_rev;5445my$linenr_filename=5446exists($meta->{'file_parent'}) ?5447$meta->{'file_parent'} : unquote($meta->{'filename'});5448my$blamed= href(action =>'blame',5449 file_name =>$linenr_filename,5450 hash_base =>$linenr_commit);5451print"<td class=\"linenr\">";5452print$cgi->a({ -href =>"$blamed#l$orig_lineno",5453-class=>"linenr"},5454 esc_html($lineno));5455print"</td>";5456print"<td class=\"pre\">". esc_html($data) ."</td>\n";5457print"</tr>\n";5458}# end while54595460}54615462# footer5463print"</tbody>\n".5464"</table>\n";# class="blame"5465print"</div>\n";# class="blame_body"5466close$fd5467or print"Reading blob failed\n";54685469 git_footer_html();5470}54715472sub git_blame {5473 git_blame_common();5474}54755476sub git_blame_incremental {5477 git_blame_common('incremental');5478}54795480sub git_blame_data {5481 git_blame_common('data');5482}54835484sub git_tags {5485my$head= git_get_head_hash($project);5486 git_header_html();5487 git_print_page_nav('','',$head,undef,$head);5488 git_print_header_div('summary',$project);54895490my@tagslist= git_get_tags_list();5491if(@tagslist) {5492 git_tags_body(\@tagslist);5493}5494 git_footer_html();5495}54965497sub git_heads {5498my$head= git_get_head_hash($project);5499 git_header_html();5500 git_print_page_nav('','',$head,undef,$head);5501 git_print_header_div('summary',$project);55025503my@headslist= git_get_heads_list();5504if(@headslist) {5505 git_heads_body(\@headslist,$head);5506}5507 git_footer_html();5508}55095510sub git_blob_plain {5511my$type=shift;5512my$expires;55135514if(!defined$hash) {5515if(defined$file_name) {5516my$base=$hash_base|| git_get_head_hash($project);5517$hash= git_get_hash_by_path($base,$file_name,"blob")5518or die_error(404,"Cannot find file");5519}else{5520 die_error(400,"No file name defined");5521}5522}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5523# blobs defined by non-textual hash id's can be cached5524$expires="+1d";5525}55265527open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5528or die_error(500,"Open git-cat-file blob '$hash' failed");55295530# content-type (can include charset)5531$type= blob_contenttype($fd,$file_name,$type);55325533# "save as" filename, even when no $file_name is given5534my$save_as="$hash";5535if(defined$file_name) {5536$save_as=$file_name;5537}elsif($type=~m/^text\//) {5538$save_as.='.txt';5539}55405541# With XSS prevention on, blobs of all types except a few known safe5542# ones are served with "Content-Disposition: attachment" to make sure5543# they don't run in our security domain. For certain image types,5544# blob view writes an <img> tag referring to blob_plain view, and we5545# want to be sure not to break that by serving the image as an5546# attachment (though Firefox 3 doesn't seem to care).5547my$sandbox=$prevent_xss&&5548$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;55495550print$cgi->header(5551-type =>$type,5552-expires =>$expires,5553-content_disposition =>5554($sandbox?'attachment':'inline')5555.'; filename="'.$save_as.'"');5556local$/=undef;5557binmode STDOUT,':raw';5558print<$fd>;5559binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5560close$fd;5561}55625563sub git_blob {5564my$expires;55655566if(!defined$hash) {5567if(defined$file_name) {5568my$base=$hash_base|| git_get_head_hash($project);5569$hash= git_get_hash_by_path($base,$file_name,"blob")5570or die_error(404,"Cannot find file");5571}else{5572 die_error(400,"No file name defined");5573}5574}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5575# blobs defined by non-textual hash id's can be cached5576$expires="+1d";5577}55785579my$have_blame= gitweb_check_feature('blame');5580open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5581or die_error(500,"Couldn't cat$file_name,$hash");5582my$mimetype= blob_mimetype($fd,$file_name);5583# use 'blob_plain' (aka 'raw') view for files that cannot be displayed5584if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5585close$fd;5586return git_blob_plain($mimetype);5587}5588# we can have blame only for text/* mimetype5589$have_blame&&= ($mimetype=~m!^text/!);55905591my$highlight= gitweb_check_feature('highlight');5592my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);5593$fd= run_highlighter($fd,$highlight,$syntax)5594if$syntax;55955596 git_header_html(undef,$expires);5597my$formats_nav='';5598if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5599if(defined$file_name) {5600if($have_blame) {5601$formats_nav.=5602$cgi->a({-href => href(action=>"blame", -replay=>1)},5603"blame") .5604" | ";5605}5606$formats_nav.=5607$cgi->a({-href => href(action=>"history", -replay=>1)},5608"history") .5609" | ".5610$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5611"raw") .5612" | ".5613$cgi->a({-href => href(action=>"blob",5614 hash_base=>"HEAD", file_name=>$file_name)},5615"HEAD");5616}else{5617$formats_nav.=5618$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5619"raw");5620}5621 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5622 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5623}else{5624print"<div class=\"page_nav\">\n".5625"<br/><br/></div>\n".5626"<div class=\"title\">$hash</div>\n";5627}5628 git_print_page_path($file_name,"blob",$hash_base);5629print"<div class=\"page_body\">\n";5630if($mimetype=~m!^image/!) {5631print qq!<img type="$mimetype"!;5632if($file_name) {5633print qq! alt="$file_name" title="$file_name"!;5634}5635print qq! src="! .5636 href(action=>"blob_plain", hash=>$hash,5637 hash_base=>$hash_base, file_name=>$file_name) .5638 qq!"/>\n!;5639}else{5640my$nr;5641while(my$line= <$fd>) {5642chomp$line;5643$nr++;5644$line= untabify($line);5645printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,5646$nr, href(-replay =>1),$nr,$nr,$syntax?$line: esc_html($line, -nbsp=>1);5647}5648}5649close$fd5650or print"Reading blob failed.\n";5651print"</div>";5652 git_footer_html();5653}56545655sub git_tree {5656if(!defined$hash_base) {5657$hash_base="HEAD";5658}5659if(!defined$hash) {5660if(defined$file_name) {5661$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5662}else{5663$hash=$hash_base;5664}5665}5666 die_error(404,"No such tree")unlessdefined($hash);56675668my$show_sizes= gitweb_check_feature('show-sizes');5669my$have_blame= gitweb_check_feature('blame');56705671my@entries= ();5672{5673local$/="\0";5674open my$fd,"-|", git_cmd(),"ls-tree",'-z',5675($show_sizes?'-l': ()),@extra_options,$hash5676or die_error(500,"Open git-ls-tree failed");5677@entries=map{chomp;$_} <$fd>;5678close$fd5679or die_error(404,"Reading tree failed");5680}56815682my$refs= git_get_references();5683my$ref= format_ref_marker($refs,$hash_base);5684 git_header_html();5685my$basedir='';5686if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5687my@views_nav= ();5688if(defined$file_name) {5689push@views_nav,5690$cgi->a({-href => href(action=>"history", -replay=>1)},5691"history"),5692$cgi->a({-href => href(action=>"tree",5693 hash_base=>"HEAD", file_name=>$file_name)},5694"HEAD"),5695}5696my$snapshot_links= format_snapshot_links($hash);5697if(defined$snapshot_links) {5698# FIXME: Should be available when we have no hash base as well.5699push@views_nav,$snapshot_links;5700}5701 git_print_page_nav('tree','',$hash_base,undef,undef,5702join(' | ',@views_nav));5703 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5704}else{5705undef$hash_base;5706print"<div class=\"page_nav\">\n";5707print"<br/><br/></div>\n";5708print"<div class=\"title\">$hash</div>\n";5709}5710if(defined$file_name) {5711$basedir=$file_name;5712if($basedirne''&&substr($basedir, -1)ne'/') {5713$basedir.='/';5714}5715 git_print_page_path($file_name,'tree',$hash_base);5716}5717print"<div class=\"page_body\">\n";5718print"<table class=\"tree\">\n";5719my$alternate=1;5720# '..' (top directory) link if possible5721if(defined$hash_base&&5722defined$file_name&&$file_name=~m![^/]+$!) {5723if($alternate) {5724print"<tr class=\"dark\">\n";5725}else{5726print"<tr class=\"light\">\n";5727}5728$alternate^=1;57295730my$up=$file_name;5731$up=~s!/?[^/]+$!!;5732undef$upunless$up;5733# based on git_print_tree_entry5734print'<td class="mode">'. mode_str('040000') ."</td>\n";5735print'<td class="size"> </td>'."\n"if$show_sizes;5736print'<td class="list">';5737print$cgi->a({-href => href(action=>"tree",5738 hash_base=>$hash_base,5739 file_name=>$up)},5740"..");5741print"</td>\n";5742print"<td class=\"link\"></td>\n";57435744print"</tr>\n";5745}5746foreachmy$line(@entries) {5747my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);57485749if($alternate) {5750print"<tr class=\"dark\">\n";5751}else{5752print"<tr class=\"light\">\n";5753}5754$alternate^=1;57555756 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);57575758print"</tr>\n";5759}5760print"</table>\n".5761"</div>";5762 git_footer_html();5763}57645765sub snapshot_name {5766my($project,$hash) =@_;57675768# path/to/project.git -> project5769# path/to/project/.git -> project5770my$name= to_utf8($project);5771$name=~ s,([^/])/*\.git$,$1,;5772$name= basename($name);5773# sanitize name5774$name=~s/[[:cntrl:]]/?/g;57755776my$ver=$hash;5777if($hash=~/^[0-9a-fA-F]+$/) {5778# shorten SHA-1 hash5779my$full_hash= git_get_full_hash($project,$hash);5780if($full_hash=~/^$hash/&&length($hash) >7) {5781$ver= git_get_short_hash($project,$hash);5782}5783}elsif($hash=~m!^refs/tags/(.*)$!) {5784# tags don't need shortened SHA-1 hash5785$ver=$1;5786}else{5787# branches and other need shortened SHA-1 hash5788if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5789$ver=$1;5790}5791$ver.='-'. git_get_short_hash($project,$hash);5792}5793# in case of hierarchical branch names5794$ver=~s!/!.!g;57955796# name = project-version_string5797$name="$name-$ver";57985799returnwantarray? ($name,$name) :$name;5800}58015802sub git_snapshot {5803my$format=$input_params{'snapshot_format'};5804if(!@snapshot_fmts) {5805 die_error(403,"Snapshots not allowed");5806}5807# default to first supported snapshot format5808$format||=$snapshot_fmts[0];5809if($format!~m/^[a-z0-9]+$/) {5810 die_error(400,"Invalid snapshot format parameter");5811}elsif(!exists($known_snapshot_formats{$format})) {5812 die_error(400,"Unknown snapshot format");5813}elsif($known_snapshot_formats{$format}{'disabled'}) {5814 die_error(403,"Snapshot format not allowed");5815}elsif(!grep($_eq$format,@snapshot_fmts)) {5816 die_error(403,"Unsupported snapshot format");5817}58185819my$type= git_get_type("$hash^{}");5820if(!$type) {5821 die_error(404,'Object does not exist');5822}elsif($typeeq'blob') {5823 die_error(400,'Object is not a tree-ish');5824}58255826my($name,$prefix) = snapshot_name($project,$hash);5827my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5828my$cmd= quote_command(5829 git_cmd(),'archive',5830"--format=$known_snapshot_formats{$format}{'format'}",5831"--prefix=$prefix/",$hash);5832if(exists$known_snapshot_formats{$format}{'compressor'}) {5833$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5834}58355836$filename=~s/(["\\])/\\$1/g;5837print$cgi->header(5838-type =>$known_snapshot_formats{$format}{'type'},5839-content_disposition =>'inline; filename="'.$filename.'"',5840-status =>'200 OK');58415842open my$fd,"-|",$cmd5843or die_error(500,"Execute git-archive failed");5844binmode STDOUT,':raw';5845print<$fd>;5846binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5847close$fd;5848}58495850sub git_log_generic {5851my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;58525853my$head= git_get_head_hash($project);5854if(!defined$base) {5855$base=$head;5856}5857if(!defined$page) {5858$page=0;5859}5860my$refs= git_get_references();58615862my$commit_hash=$base;5863if(defined$parent) {5864$commit_hash="$parent..$base";5865}5866my@commitlist=5867 parse_commits($commit_hash,101, (100*$page),5868defined$file_name? ($file_name,"--full-history") : ());58695870my$ftype;5871if(!defined$file_hash&&defined$file_name) {5872# some commits could have deleted file in question,5873# and not have it in tree, but one of them has to have it5874for(my$i=0;$i<@commitlist;$i++) {5875$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5876last ifdefined$file_hash;5877}5878}5879if(defined$file_hash) {5880$ftype= git_get_type($file_hash);5881}5882if(defined$file_name&& !defined$ftype) {5883 die_error(500,"Unknown type of object");5884}5885my%co;5886if(defined$file_name) {5887%co= parse_commit($base)5888or die_error(404,"Unknown commit object");5889}589058915892my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5893my$next_link='';5894if($#commitlist>=100) {5895$next_link=5896$cgi->a({-href => href(-replay=>1, page=>$page+1),5897-accesskey =>"n", -title =>"Alt-n"},"next");5898}5899my$patch_max= gitweb_get_feature('patches');5900if($patch_max&& !defined$file_name) {5901if($patch_max<0||@commitlist<=$patch_max) {5902$paging_nav.=" ⋅ ".5903$cgi->a({-href => href(action=>"patches", -replay=>1)},5904"patches");5905}5906}59075908 git_header_html();5909 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5910if(defined$file_name) {5911 git_print_header_div('commit', esc_html($co{'title'}),$base);5912}else{5913 git_print_header_div('summary',$project)5914}5915 git_print_page_path($file_name,$ftype,$hash_base)5916if(defined$file_name);59175918$body_subr->(\@commitlist,0,99,$refs,$next_link,5919$file_name,$file_hash,$ftype);59205921 git_footer_html();5922}59235924sub git_log {5925 git_log_generic('log', \&git_log_body,5926$hash,$hash_parent);5927}59285929sub git_commit {5930$hash||=$hash_base||"HEAD";5931my%co= parse_commit($hash)5932or die_error(404,"Unknown commit object");59335934my$parent=$co{'parent'};5935my$parents=$co{'parents'};# listref59365937# we need to prepare $formats_nav before any parameter munging5938my$formats_nav;5939if(!defined$parent) {5940# --root commitdiff5941$formats_nav.='(initial)';5942}elsif(@$parents==1) {5943# single parent commit5944$formats_nav.=5945'(parent: '.5946$cgi->a({-href => href(action=>"commit",5947 hash=>$parent)},5948 esc_html(substr($parent,0,7))) .5949')';5950}else{5951# merge commit5952$formats_nav.=5953'(merge: '.5954join(' ',map{5955$cgi->a({-href => href(action=>"commit",5956 hash=>$_)},5957 esc_html(substr($_,0,7)));5958}@$parents) .5959')';5960}5961if(gitweb_check_feature('patches') &&@$parents<=1) {5962$formats_nav.=" | ".5963$cgi->a({-href => href(action=>"patch", -replay=>1)},5964"patch");5965}59665967if(!defined$parent) {5968$parent="--root";5969}5970my@difftree;5971open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5972@diff_opts,5973(@$parents<=1?$parent:'-c'),5974$hash,"--"5975or die_error(500,"Open git-diff-tree failed");5976@difftree=map{chomp;$_} <$fd>;5977close$fdor die_error(404,"Reading git-diff-tree failed");59785979# non-textual hash id's can be cached5980my$expires;5981if($hash=~m/^[0-9a-fA-F]{40}$/) {5982$expires="+1d";5983}5984my$refs= git_get_references();5985my$ref= format_ref_marker($refs,$co{'id'});59865987 git_header_html(undef,$expires);5988 git_print_page_nav('commit','',5989$hash,$co{'tree'},$hash,5990$formats_nav);59915992if(defined$co{'parent'}) {5993 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5994}else{5995 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5996}5997print"<div class=\"title_text\">\n".5998"<table class=\"object_header\">\n";5999 git_print_authorship_rows(\%co);6000print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";6001print"<tr>".6002"<td>tree</td>".6003"<td class=\"sha1\">".6004$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),6005class=>"list"},$co{'tree'}) .6006"</td>".6007"<td class=\"link\">".6008$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},6009"tree");6010my$snapshot_links= format_snapshot_links($hash);6011if(defined$snapshot_links) {6012print" | ".$snapshot_links;6013}6014print"</td>".6015"</tr>\n";60166017foreachmy$par(@$parents) {6018print"<tr>".6019"<td>parent</td>".6020"<td class=\"sha1\">".6021$cgi->a({-href => href(action=>"commit", hash=>$par),6022class=>"list"},$par) .6023"</td>".6024"<td class=\"link\">".6025$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .6026" | ".6027$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .6028"</td>".6029"</tr>\n";6030}6031print"</table>".6032"</div>\n";60336034print"<div class=\"page_body\">\n";6035 git_print_log($co{'comment'});6036print"</div>\n";60376038 git_difftree_body(\@difftree,$hash,@$parents);60396040 git_footer_html();6041}60426043sub git_object {6044# object is defined by:6045# - hash or hash_base alone6046# - hash_base and file_name6047my$type;60486049# - hash or hash_base alone6050if($hash|| ($hash_base&& !defined$file_name)) {6051my$object_id=$hash||$hash_base;60526053open my$fd,"-|", quote_command(6054 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'6055or die_error(404,"Object does not exist");6056$type= <$fd>;6057chomp$type;6058close$fd6059or die_error(404,"Object does not exist");60606061# - hash_base and file_name6062}elsif($hash_base&&defined$file_name) {6063$file_name=~ s,/+$,,;60646065system(git_cmd(),"cat-file",'-e',$hash_base) ==06066or die_error(404,"Base object does not exist");60676068# here errors should not hapen6069open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name6070or die_error(500,"Open git-ls-tree failed");6071my$line= <$fd>;6072close$fd;60736074#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'6075unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {6076 die_error(404,"File or directory for given base does not exist");6077}6078$type=$2;6079$hash=$3;6080}else{6081 die_error(400,"Not enough information to find object");6082}60836084print$cgi->redirect(-uri => href(action=>$type, -full=>1,6085 hash=>$hash, hash_base=>$hash_base,6086 file_name=>$file_name),6087-status =>'302 Found');6088}60896090sub git_blobdiff {6091my$format=shift||'html';60926093my$fd;6094my@difftree;6095my%diffinfo;6096my$expires;60976098# preparing $fd and %diffinfo for git_patchset_body6099# new style URI6100if(defined$hash_base&&defined$hash_parent_base) {6101if(defined$file_name) {6102# read raw output6103open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6104$hash_parent_base,$hash_base,6105"--", (defined$file_parent?$file_parent: ()),$file_name6106or die_error(500,"Open git-diff-tree failed");6107@difftree=map{chomp;$_} <$fd>;6108close$fd6109or die_error(404,"Reading git-diff-tree failed");6110@difftree6111or die_error(404,"Blob diff not found");61126113}elsif(defined$hash&&6114$hash=~/[0-9a-fA-F]{40}/) {6115# try to find filename from $hash61166117# read filtered raw output6118open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6119$hash_parent_base,$hash_base,"--"6120or die_error(500,"Open git-diff-tree failed");6121@difftree=6122# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'6123# $hash == to_id6124grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}6125map{chomp;$_} <$fd>;6126close$fd6127or die_error(404,"Reading git-diff-tree failed");6128@difftree6129or die_error(404,"Blob diff not found");61306131}else{6132 die_error(400,"Missing one of the blob diff parameters");6133}61346135if(@difftree>1) {6136 die_error(400,"Ambiguous blob diff specification");6137}61386139%diffinfo= parse_difftree_raw_line($difftree[0]);6140$file_parent||=$diffinfo{'from_file'} ||$file_name;6141$file_name||=$diffinfo{'to_file'};61426143$hash_parent||=$diffinfo{'from_id'};6144$hash||=$diffinfo{'to_id'};61456146# non-textual hash id's can be cached6147if($hash_base=~m/^[0-9a-fA-F]{40}$/&&6148$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {6149$expires='+1d';6150}61516152# open patch output6153open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6154'-p', ($formateq'html'?"--full-index": ()),6155$hash_parent_base,$hash_base,6156"--", (defined$file_parent?$file_parent: ()),$file_name6157or die_error(500,"Open git-diff-tree failed");6158}61596160# old/legacy style URI -- not generated anymore since 1.4.3.6161if(!%diffinfo) {6162 die_error('404 Not Found',"Missing one of the blob diff parameters")6163}61646165# header6166if($formateq'html') {6167my$formats_nav=6168$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},6169"raw");6170 git_header_html(undef,$expires);6171if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6172 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6173 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6174}else{6175print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";6176print"<div class=\"title\">$hashvs$hash_parent</div>\n";6177}6178if(defined$file_name) {6179 git_print_page_path($file_name,"blob",$hash_base);6180}else{6181print"<div class=\"page_path\"></div>\n";6182}61836184}elsif($formateq'plain') {6185print$cgi->header(6186-type =>'text/plain',6187-charset =>'utf-8',6188-expires =>$expires,6189-content_disposition =>'inline; filename="'."$file_name".'.patch"');61906191print"X-Git-Url: ".$cgi->self_url() ."\n\n";61926193}else{6194 die_error(400,"Unknown blobdiff format");6195}61966197# patch6198if($formateq'html') {6199print"<div class=\"page_body\">\n";62006201 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);6202close$fd;62036204print"</div>\n";# class="page_body"6205 git_footer_html();62066207}else{6208while(my$line= <$fd>) {6209$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;6210$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;62116212print$line;62136214last if$line=~m!^\+\+\+!;6215}6216local$/=undef;6217print<$fd>;6218close$fd;6219}6220}62216222sub git_blobdiff_plain {6223 git_blobdiff('plain');6224}62256226sub git_commitdiff {6227my%params=@_;6228my$format=$params{-format} ||'html';62296230my($patch_max) = gitweb_get_feature('patches');6231if($formateq'patch') {6232 die_error(403,"Patch view not allowed")unless$patch_max;6233}62346235$hash||=$hash_base||"HEAD";6236my%co= parse_commit($hash)6237or die_error(404,"Unknown commit object");62386239# choose format for commitdiff for merge6240if(!defined$hash_parent&& @{$co{'parents'}} >1) {6241$hash_parent='--cc';6242}6243# we need to prepare $formats_nav before almost any parameter munging6244my$formats_nav;6245if($formateq'html') {6246$formats_nav=6247$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6248"raw");6249if($patch_max&& @{$co{'parents'}} <=1) {6250$formats_nav.=" | ".6251$cgi->a({-href => href(action=>"patch", -replay=>1)},6252"patch");6253}62546255if(defined$hash_parent&&6256$hash_parentne'-c'&&$hash_parentne'--cc') {6257# commitdiff with two commits given6258my$hash_parent_short=$hash_parent;6259if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6260$hash_parent_short=substr($hash_parent,0,7);6261}6262$formats_nav.=6263' (from';6264for(my$i=0;$i< @{$co{'parents'}};$i++) {6265if($co{'parents'}[$i]eq$hash_parent) {6266$formats_nav.=' parent '. ($i+1);6267last;6268}6269}6270$formats_nav.=': '.6271$cgi->a({-href => href(action=>"commitdiff",6272 hash=>$hash_parent)},6273 esc_html($hash_parent_short)) .6274')';6275}elsif(!$co{'parent'}) {6276# --root commitdiff6277$formats_nav.=' (initial)';6278}elsif(scalar@{$co{'parents'}} ==1) {6279# single parent commit6280$formats_nav.=6281' (parent: '.6282$cgi->a({-href => href(action=>"commitdiff",6283 hash=>$co{'parent'})},6284 esc_html(substr($co{'parent'},0,7))) .6285')';6286}else{6287# merge commit6288if($hash_parenteq'--cc') {6289$formats_nav.=' | '.6290$cgi->a({-href => href(action=>"commitdiff",6291 hash=>$hash, hash_parent=>'-c')},6292'combined');6293}else{# $hash_parent eq '-c'6294$formats_nav.=' | '.6295$cgi->a({-href => href(action=>"commitdiff",6296 hash=>$hash, hash_parent=>'--cc')},6297'compact');6298}6299$formats_nav.=6300' (merge: '.6301join(' ',map{6302$cgi->a({-href => href(action=>"commitdiff",6303 hash=>$_)},6304 esc_html(substr($_,0,7)));6305} @{$co{'parents'}} ) .6306')';6307}6308}63096310my$hash_parent_param=$hash_parent;6311if(!defined$hash_parent_param) {6312# --cc for multiple parents, --root for parentless6313$hash_parent_param=6314@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6315}63166317# read commitdiff6318my$fd;6319my@difftree;6320if($formateq'html') {6321open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6322"--no-commit-id","--patch-with-raw","--full-index",6323$hash_parent_param,$hash,"--"6324or die_error(500,"Open git-diff-tree failed");63256326while(my$line= <$fd>) {6327chomp$line;6328# empty line ends raw part of diff-tree output6329last unless$line;6330push@difftree,scalar parse_difftree_raw_line($line);6331}63326333}elsif($formateq'plain') {6334open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6335'-p',$hash_parent_param,$hash,"--"6336or die_error(500,"Open git-diff-tree failed");6337}elsif($formateq'patch') {6338# For commit ranges, we limit the output to the number of6339# patches specified in the 'patches' feature.6340# For single commits, we limit the output to a single patch,6341# diverging from the git-format-patch default.6342my@commit_spec= ();6343if($hash_parent) {6344if($patch_max>0) {6345push@commit_spec,"-$patch_max";6346}6347push@commit_spec,'-n',"$hash_parent..$hash";6348}else{6349if($params{-single}) {6350push@commit_spec,'-1';6351}else{6352if($patch_max>0) {6353push@commit_spec,"-$patch_max";6354}6355push@commit_spec,"-n";6356}6357push@commit_spec,'--root',$hash;6358}6359open$fd,"-|", git_cmd(),"format-patch",@diff_opts,6360'--encoding=utf8','--stdout',@commit_spec6361or die_error(500,"Open git-format-patch failed");6362}else{6363 die_error(400,"Unknown commitdiff format");6364}63656366# non-textual hash id's can be cached6367my$expires;6368if($hash=~m/^[0-9a-fA-F]{40}$/) {6369$expires="+1d";6370}63716372# write commit message6373if($formateq'html') {6374my$refs= git_get_references();6375my$ref= format_ref_marker($refs,$co{'id'});63766377 git_header_html(undef,$expires);6378 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6379 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6380print"<div class=\"title_text\">\n".6381"<table class=\"object_header\">\n";6382 git_print_authorship_rows(\%co);6383print"</table>".6384"</div>\n";6385print"<div class=\"page_body\">\n";6386if(@{$co{'comment'}} >1) {6387print"<div class=\"log\">\n";6388 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6389print"</div>\n";# class="log"6390}63916392}elsif($formateq'plain') {6393my$refs= git_get_references("tags");6394my$tagname= git_get_rev_name_tags($hash);6395my$filename= basename($project) ."-$hash.patch";63966397print$cgi->header(6398-type =>'text/plain',6399-charset =>'utf-8',6400-expires =>$expires,6401-content_disposition =>'inline; filename="'."$filename".'"');6402my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6403print"From: ". to_utf8($co{'author'}) ."\n";6404print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6405print"Subject: ". to_utf8($co{'title'}) ."\n";64066407print"X-Git-Tag:$tagname\n"if$tagname;6408print"X-Git-Url: ".$cgi->self_url() ."\n\n";64096410foreachmy$line(@{$co{'comment'}}) {6411print to_utf8($line) ."\n";6412}6413print"---\n\n";6414}elsif($formateq'patch') {6415my$filename= basename($project) ."-$hash.patch";64166417print$cgi->header(6418-type =>'text/plain',6419-charset =>'utf-8',6420-expires =>$expires,6421-content_disposition =>'inline; filename="'."$filename".'"');6422}64236424# write patch6425if($formateq'html') {6426my$use_parents= !defined$hash_parent||6427$hash_parenteq'-c'||$hash_parenteq'--cc';6428 git_difftree_body(\@difftree,$hash,6429$use_parents? @{$co{'parents'}} :$hash_parent);6430print"<br/>\n";64316432 git_patchset_body($fd, \@difftree,$hash,6433$use_parents? @{$co{'parents'}} :$hash_parent);6434close$fd;6435print"</div>\n";# class="page_body"6436 git_footer_html();64376438}elsif($formateq'plain') {6439local$/=undef;6440print<$fd>;6441close$fd6442or print"Reading git-diff-tree failed\n";6443}elsif($formateq'patch') {6444local$/=undef;6445print<$fd>;6446close$fd6447or print"Reading git-format-patch failed\n";6448}6449}64506451sub git_commitdiff_plain {6452 git_commitdiff(-format =>'plain');6453}64546455# format-patch-style patches6456sub git_patch {6457 git_commitdiff(-format =>'patch', -single =>1);6458}64596460sub git_patches {6461 git_commitdiff(-format =>'patch');6462}64636464sub git_history {6465 git_log_generic('history', \&git_history_body,6466$hash_base,$hash_parent_base,6467$file_name,$hash);6468}64696470sub git_search {6471 gitweb_check_feature('search')or die_error(403,"Search is disabled");6472if(!defined$searchtext) {6473 die_error(400,"Text field is empty");6474}6475if(!defined$hash) {6476$hash= git_get_head_hash($project);6477}6478my%co= parse_commit($hash);6479if(!%co) {6480 die_error(404,"Unknown commit object");6481}6482if(!defined$page) {6483$page=0;6484}64856486$searchtype||='commit';6487if($searchtypeeq'pickaxe') {6488# pickaxe may take all resources of your box and run for several minutes6489# with every query - so decide by yourself how public you make this feature6490 gitweb_check_feature('pickaxe')6491or die_error(403,"Pickaxe is disabled");6492}6493if($searchtypeeq'grep') {6494 gitweb_check_feature('grep')6495or die_error(403,"Grep is disabled");6496}64976498 git_header_html();64996500if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6501my$greptype;6502if($searchtypeeq'commit') {6503$greptype="--grep=";6504}elsif($searchtypeeq'author') {6505$greptype="--author=";6506}elsif($searchtypeeq'committer') {6507$greptype="--committer=";6508}6509$greptype.=$searchtext;6510my@commitlist= parse_commits($hash,101, (100*$page),undef,6511$greptype,'--regexp-ignore-case',6512$search_use_regexp?'--extended-regexp':'--fixed-strings');65136514my$paging_nav='';6515if($page>0) {6516$paging_nav.=6517$cgi->a({-href => href(action=>"search", hash=>$hash,6518 searchtext=>$searchtext,6519 searchtype=>$searchtype)},6520"first");6521$paging_nav.=" ⋅ ".6522$cgi->a({-href => href(-replay=>1, page=>$page-1),6523-accesskey =>"p", -title =>"Alt-p"},"prev");6524}else{6525$paging_nav.="first";6526$paging_nav.=" ⋅ prev";6527}6528my$next_link='';6529if($#commitlist>=100) {6530$next_link=6531$cgi->a({-href => href(-replay=>1, page=>$page+1),6532-accesskey =>"n", -title =>"Alt-n"},"next");6533$paging_nav.=" ⋅$next_link";6534}else{6535$paging_nav.=" ⋅ next";6536}65376538 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6539 git_print_header_div('commit', esc_html($co{'title'}),$hash);6540if($page==0&& !@commitlist) {6541print"<p>No match.</p>\n";6542}else{6543 git_search_grep_body(\@commitlist,0,99,$next_link);6544}6545}65466547if($searchtypeeq'pickaxe') {6548 git_print_page_nav('','',$hash,$co{'tree'},$hash);6549 git_print_header_div('commit', esc_html($co{'title'}),$hash);65506551print"<table class=\"pickaxe search\">\n";6552my$alternate=1;6553local$/="\n";6554open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6555'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6556($search_use_regexp?'--pickaxe-regex': ());6557undef%co;6558my@files;6559while(my$line= <$fd>) {6560chomp$line;6561next unless$line;65626563my%set= parse_difftree_raw_line($line);6564if(defined$set{'commit'}) {6565# finish previous commit6566if(%co) {6567print"</td>\n".6568"<td class=\"link\">".6569$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6570" | ".6571$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6572print"</td>\n".6573"</tr>\n";6574}65756576if($alternate) {6577print"<tr class=\"dark\">\n";6578}else{6579print"<tr class=\"light\">\n";6580}6581$alternate^=1;6582%co= parse_commit($set{'commit'});6583my$author= chop_and_escape_str($co{'author_name'},15,5);6584print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6585"<td><i>$author</i></td>\n".6586"<td>".6587$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6588-class=>"list subject"},6589 chop_and_escape_str($co{'title'},50) ."<br/>");6590}elsif(defined$set{'to_id'}) {6591next if($set{'to_id'} =~m/^0{40}$/);65926593print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6594 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6595-class=>"list"},6596"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6597"<br/>\n";6598}6599}6600close$fd;66016602# finish last commit (warning: repetition!)6603if(%co) {6604print"</td>\n".6605"<td class=\"link\">".6606$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6607" | ".6608$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6609print"</td>\n".6610"</tr>\n";6611}66126613print"</table>\n";6614}66156616if($searchtypeeq'grep') {6617 git_print_page_nav('','',$hash,$co{'tree'},$hash);6618 git_print_header_div('commit', esc_html($co{'title'}),$hash);66196620print"<table class=\"grep_search\">\n";6621my$alternate=1;6622my$matches=0;6623local$/="\n";6624open my$fd,"-|", git_cmd(),'grep','-n',6625$search_use_regexp? ('-E','-i') :'-F',6626$searchtext,$co{'tree'};6627my$lastfile='';6628while(my$line= <$fd>) {6629chomp$line;6630my($file,$lno,$ltext,$binary);6631last if($matches++>1000);6632if($line=~/^Binary file (.+) matches$/) {6633$file=$1;6634$binary=1;6635}else{6636(undef,$file,$lno,$ltext) =split(/:/,$line,4);6637}6638if($filene$lastfile) {6639$lastfileand print"</td></tr>\n";6640if($alternate++) {6641print"<tr class=\"dark\">\n";6642}else{6643print"<tr class=\"light\">\n";6644}6645print"<td class=\"list\">".6646$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6647 file_name=>"$file"),6648-class=>"list"}, esc_path($file));6649print"</td><td>\n";6650$lastfile=$file;6651}6652if($binary) {6653print"<div class=\"binary\">Binary file</div>\n";6654}else{6655$ltext= untabify($ltext);6656if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6657$ltext= esc_html($1, -nbsp=>1);6658$ltext.='<span class="match">';6659$ltext.= esc_html($2, -nbsp=>1);6660$ltext.='</span>';6661$ltext.= esc_html($3, -nbsp=>1);6662}else{6663$ltext= esc_html($ltext, -nbsp=>1);6664}6665print"<div class=\"pre\">".6666$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6667 file_name=>"$file").'#l'.$lno,6668-class=>"linenr"},sprintf('%4i',$lno))6669.' '.$ltext."</div>\n";6670}6671}6672if($lastfile) {6673print"</td></tr>\n";6674if($matches>1000) {6675print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6676}6677}else{6678print"<div class=\"diff nodifferences\">No matches found</div>\n";6679}6680close$fd;66816682print"</table>\n";6683}6684 git_footer_html();6685}66866687sub git_search_help {6688 git_header_html();6689 git_print_page_nav('','',$hash,$hash,$hash);6690print<<EOT;6691<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6692regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6693the pattern entered is recognized as the POSIX extended6694<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6695insensitive).</p>6696<dl>6697<dt><b>commit</b></dt>6698<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6699EOT6700my$have_grep= gitweb_check_feature('grep');6701if($have_grep) {6702print<<EOT;6703<dt><b>grep</b></dt>6704<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6705 a different one) are searched for the given pattern. On large trees, this search can take6706a while and put some strain on the server, so please use it with some consideration. Note that6707due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6708case-sensitive.</dd>6709EOT6710}6711print<<EOT;6712<dt><b>author</b></dt>6713<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6714<dt><b>committer</b></dt>6715<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6716EOT6717my$have_pickaxe= gitweb_check_feature('pickaxe');6718if($have_pickaxe) {6719print<<EOT;6720<dt><b>pickaxe</b></dt>6721<dd>All commits that caused the string to appear or disappear from any file (changes that6722added, removed or "modified" the string) will be listed. This search can take a while and6723takes a lot of strain on the server, so please use it wisely. Note that since you may be6724interested even in changes just changing the case as well, this search is case sensitive.</dd>6725EOT6726}6727print"</dl>\n";6728 git_footer_html();6729}67306731sub git_shortlog {6732 git_log_generic('shortlog', \&git_shortlog_body,6733$hash,$hash_parent);6734}67356736## ......................................................................6737## feeds (RSS, Atom; OPML)67386739sub git_feed {6740my$format=shift||'atom';6741my$have_blame= gitweb_check_feature('blame');67426743# Atom: http://www.atomenabled.org/developers/syndication/6744# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6745if($formatne'rss'&&$formatne'atom') {6746 die_error(400,"Unknown web feed format");6747}67486749# log/feed of current (HEAD) branch, log of given branch, history of file/directory6750my$head=$hash||'HEAD';6751my@commitlist= parse_commits($head,150,0,$file_name);67526753my%latest_commit;6754my%latest_date;6755my$content_type="application/$format+xml";6756if(defined$cgi->http('HTTP_ACCEPT') &&6757$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6758# browser (feed reader) prefers text/xml6759$content_type='text/xml';6760}6761if(defined($commitlist[0])) {6762%latest_commit= %{$commitlist[0]};6763my$latest_epoch=$latest_commit{'committer_epoch'};6764%latest_date= parse_date($latest_epoch);6765my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6766if(defined$if_modified) {6767my$since;6768if(eval{require HTTP::Date;1; }) {6769$since= HTTP::Date::str2time($if_modified);6770}elsif(eval{require Time::ParseDate;1; }) {6771$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6772}6773if(defined$since&&$latest_epoch<=$since) {6774print$cgi->header(6775-type =>$content_type,6776-charset =>'utf-8',6777-last_modified =>$latest_date{'rfc2822'},6778-status =>'304 Not Modified');6779return;6780}6781}6782print$cgi->header(6783-type =>$content_type,6784-charset =>'utf-8',6785-last_modified =>$latest_date{'rfc2822'});6786}else{6787print$cgi->header(6788-type =>$content_type,6789-charset =>'utf-8');6790}67916792# Optimization: skip generating the body if client asks only6793# for Last-Modified date.6794return if($cgi->request_method()eq'HEAD');67956796# header variables6797my$title="$site_name-$project/$action";6798my$feed_type='log';6799if(defined$hash) {6800$title.=" - '$hash'";6801$feed_type='branch log';6802if(defined$file_name) {6803$title.=" ::$file_name";6804$feed_type='history';6805}6806}elsif(defined$file_name) {6807$title.=" -$file_name";6808$feed_type='history';6809}6810$title.="$feed_type";6811my$descr= git_get_project_description($project);6812if(defined$descr) {6813$descr= esc_html($descr);6814}else{6815$descr="$project".6816($formateq'rss'?'RSS':'Atom') .6817" feed";6818}6819my$owner= git_get_project_owner($project);6820$owner= esc_html($owner);68216822#header6823my$alt_url;6824if(defined$file_name) {6825$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6826}elsif(defined$hash) {6827$alt_url= href(-full=>1, action=>"log", hash=>$hash);6828}else{6829$alt_url= href(-full=>1, action=>"summary");6830}6831print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6832if($formateq'rss') {6833print<<XML;6834<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6835<channel>6836XML6837print"<title>$title</title>\n".6838"<link>$alt_url</link>\n".6839"<description>$descr</description>\n".6840"<language>en</language>\n".6841# project owner is responsible for 'editorial' content6842"<managingEditor>$owner</managingEditor>\n";6843if(defined$logo||defined$favicon) {6844# prefer the logo to the favicon, since RSS6845# doesn't allow both6846my$img= esc_url($logo||$favicon);6847print"<image>\n".6848"<url>$img</url>\n".6849"<title>$title</title>\n".6850"<link>$alt_url</link>\n".6851"</image>\n";6852}6853if(%latest_date) {6854print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6855print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6856}6857print"<generator>gitweb v.$version/$git_version</generator>\n";6858}elsif($formateq'atom') {6859print<<XML;6860<feed xmlns="http://www.w3.org/2005/Atom">6861XML6862print"<title>$title</title>\n".6863"<subtitle>$descr</subtitle>\n".6864'<link rel="alternate" type="text/html" href="'.6865$alt_url.'" />'."\n".6866'<link rel="self" type="'.$content_type.'" href="'.6867$cgi->self_url() .'" />'."\n".6868"<id>". href(-full=>1) ."</id>\n".6869# use project owner for feed author6870"<author><name>$owner</name></author>\n";6871if(defined$favicon) {6872print"<icon>". esc_url($favicon) ."</icon>\n";6873}6874if(defined$logo_url) {6875# not twice as wide as tall: 72 x 27 pixels6876print"<logo>". esc_url($logo) ."</logo>\n";6877}6878if(!%latest_date) {6879# dummy date to keep the feed valid until commits trickle in:6880print"<updated>1970-01-01T00:00:00Z</updated>\n";6881}else{6882print"<updated>$latest_date{'iso-8601'}</updated>\n";6883}6884print"<generator version='$version/$git_version'>gitweb</generator>\n";6885}68866887# contents6888for(my$i=0;$i<=$#commitlist;$i++) {6889my%co= %{$commitlist[$i]};6890my$commit=$co{'id'};6891# we read 150, we always show 30 and the ones more recent than 48 hours6892if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6893last;6894}6895my%cd= parse_date($co{'author_epoch'});68966897# get list of changed files6898open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6899$co{'parent'} ||"--root",6900$co{'id'},"--", (defined$file_name?$file_name: ())6901ornext;6902my@difftree=map{chomp;$_} <$fd>;6903close$fd6904ornext;69056906# print element (entry, item)6907my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6908if($formateq'rss') {6909print"<item>\n".6910"<title>". esc_html($co{'title'}) ."</title>\n".6911"<author>". esc_html($co{'author'}) ."</author>\n".6912"<pubDate>$cd{'rfc2822'}</pubDate>\n".6913"<guid isPermaLink=\"true\">$co_url</guid>\n".6914"<link>$co_url</link>\n".6915"<description>". esc_html($co{'title'}) ."</description>\n".6916"<content:encoded>".6917"<![CDATA[\n";6918}elsif($formateq'atom') {6919print"<entry>\n".6920"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6921"<updated>$cd{'iso-8601'}</updated>\n".6922"<author>\n".6923" <name>". esc_html($co{'author_name'}) ."</name>\n";6924if($co{'author_email'}) {6925print" <email>". esc_html($co{'author_email'}) ."</email>\n";6926}6927print"</author>\n".6928# use committer for contributor6929"<contributor>\n".6930" <name>". esc_html($co{'committer_name'}) ."</name>\n";6931if($co{'committer_email'}) {6932print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6933}6934print"</contributor>\n".6935"<published>$cd{'iso-8601'}</published>\n".6936"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6937"<id>$co_url</id>\n".6938"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6939"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6940}6941my$comment=$co{'comment'};6942print"<pre>\n";6943foreachmy$line(@$comment) {6944$line= esc_html($line);6945print"$line\n";6946}6947print"</pre><ul>\n";6948foreachmy$difftree_line(@difftree) {6949my%difftree= parse_difftree_raw_line($difftree_line);6950next if!$difftree{'from_id'};69516952my$file=$difftree{'file'} ||$difftree{'to_file'};69536954print"<li>".6955"[".6956$cgi->a({-href => href(-full=>1, action=>"blobdiff",6957 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6958 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6959 file_name=>$file, file_parent=>$difftree{'from_file'}),6960-title =>"diff"},'D');6961if($have_blame) {6962print$cgi->a({-href => href(-full=>1, action=>"blame",6963 file_name=>$file, hash_base=>$commit),6964-title =>"blame"},'B');6965}6966# if this is not a feed of a file history6967if(!defined$file_name||$file_namene$file) {6968print$cgi->a({-href => href(-full=>1, action=>"history",6969 file_name=>$file, hash=>$commit),6970-title =>"history"},'H');6971}6972$file= esc_path($file);6973print"] ".6974"$file</li>\n";6975}6976if($formateq'rss') {6977print"</ul>]]>\n".6978"</content:encoded>\n".6979"</item>\n";6980}elsif($formateq'atom') {6981print"</ul>\n</div>\n".6982"</content>\n".6983"</entry>\n";6984}6985}69866987# end of feed6988if($formateq'rss') {6989print"</channel>\n</rss>\n";6990}elsif($formateq'atom') {6991print"</feed>\n";6992}6993}69946995sub git_rss {6996 git_feed('rss');6997}69986999sub git_atom {7000 git_feed('atom');7001}70027003sub git_opml {7004my@list= git_get_projects_list();70057006print$cgi->header(7007-type =>'text/xml',7008-charset =>'utf-8',7009-content_disposition =>'inline; filename="opml.xml"');70107011print<<XML;7012<?xml version="1.0" encoding="utf-8"?>7013<opml version="1.0">7014<head>7015 <title>$site_nameOPML Export</title>7016</head>7017<body>7018<outline text="git RSS feeds">7019XML70207021foreachmy$pr(@list) {7022my%proj=%$pr;7023my$head= git_get_head_hash($proj{'path'});7024if(!defined$head) {7025next;7026}7027$git_dir="$projectroot/$proj{'path'}";7028my%co= parse_commit($head);7029if(!%co) {7030next;7031}70327033my$path= esc_html(chop_str($proj{'path'},25,5));7034my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);7035my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);7036print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";7037}7038print<<XML;7039</outline>7040</body>7041</opml>7042XML7043}