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 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. 822$input_params{'action'} ||="shortlog"; 823if(grep{$_eq$input_params{'action'} }@wants_base) { 824$input_params{'hash_base'} ||=$refname; 825}else{ 826$input_params{'hash'} ||=$refname; 827} 828} 829 830# next, handle the 'parent' part, if present 831if(defined$parentrefname) { 832# a missing pathspec defaults to the 'current' filename, allowing e.g. 833# someproject/blobdiff/oldrev..newrev:/filename 834if($parentpathname) { 835$parentpathname=~ s,^/+,,; 836$parentpathname=~ s,/$,,; 837$input_params{'file_parent'} ||=$parentpathname; 838}else{ 839$input_params{'file_parent'} ||=$input_params{'file_name'}; 840} 841# we assume that hash_parent_base is wanted if a path was specified, 842# or if the action wants hash_base instead of hash 843if(defined$input_params{'file_parent'} || 844grep{$_eq$input_params{'action'} }@wants_base) { 845$input_params{'hash_parent_base'} ||=$parentrefname; 846}else{ 847$input_params{'hash_parent'} ||=$parentrefname; 848} 849} 850 851# for the snapshot action, we allow URLs in the form 852# $project/snapshot/$hash.ext 853# where .ext determines the snapshot and gets removed from the 854# passed $refname to provide the $hash. 855# 856# To be able to tell that $refname includes the format extension, we 857# require the following two conditions to be satisfied: 858# - the hash input parameter MUST have been set from the $refname part 859# of the URL (i.e. they must be equal) 860# - the snapshot format MUST NOT have been defined already (e.g. from 861# CGI parameter sf) 862# It's also useless to try any matching unless $refname has a dot, 863# so we check for that too 864if(defined$input_params{'action'} && 865$input_params{'action'}eq'snapshot'&& 866defined$refname&&index($refname,'.') != -1&& 867$refnameeq$input_params{'hash'} && 868!defined$input_params{'snapshot_format'}) { 869# We loop over the known snapshot formats, checking for 870# extensions. Allowed extensions are both the defined suffix 871# (which includes the initial dot already) and the snapshot 872# format key itself, with a prepended dot 873while(my($fmt,$opt) =each%known_snapshot_formats) { 874my$hash=$refname; 875unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 876next; 877} 878my$sfx=$1; 879# a valid suffix was found, so set the snapshot format 880# and reset the hash parameter 881$input_params{'snapshot_format'} =$fmt; 882$input_params{'hash'} =$hash; 883# we also set the format suffix to the one requested 884# in the URL: this way a request for e.g. .tgz returns 885# a .tgz instead of a .tar.gz 886$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 887last; 888} 889} 890} 891 892our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 893$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 894$searchtext,$search_regexp); 895sub evaluate_and_validate_params { 896our$action=$input_params{'action'}; 897if(defined$action) { 898if(!validate_action($action)) { 899 die_error(400,"Invalid action parameter"); 900} 901} 902 903# parameters which are pathnames 904our$project=$input_params{'project'}; 905if(defined$project) { 906if(!validate_project($project)) { 907undef$project; 908 die_error(404,"No such project"); 909} 910} 911 912our$file_name=$input_params{'file_name'}; 913if(defined$file_name) { 914if(!validate_pathname($file_name)) { 915 die_error(400,"Invalid file parameter"); 916} 917} 918 919our$file_parent=$input_params{'file_parent'}; 920if(defined$file_parent) { 921if(!validate_pathname($file_parent)) { 922 die_error(400,"Invalid file parent parameter"); 923} 924} 925 926# parameters which are refnames 927our$hash=$input_params{'hash'}; 928if(defined$hash) { 929if(!validate_refname($hash)) { 930 die_error(400,"Invalid hash parameter"); 931} 932} 933 934our$hash_parent=$input_params{'hash_parent'}; 935if(defined$hash_parent) { 936if(!validate_refname($hash_parent)) { 937 die_error(400,"Invalid hash parent parameter"); 938} 939} 940 941our$hash_base=$input_params{'hash_base'}; 942if(defined$hash_base) { 943if(!validate_refname($hash_base)) { 944 die_error(400,"Invalid hash base parameter"); 945} 946} 947 948our@extra_options= @{$input_params{'extra_options'}}; 949# @extra_options is always defined, since it can only be (currently) set from 950# CGI, and $cgi->param() returns the empty array in array context if the param 951# is not set 952foreachmy$opt(@extra_options) { 953if(not exists$allowed_options{$opt}) { 954 die_error(400,"Invalid option parameter"); 955} 956if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 957 die_error(400,"Invalid option parameter for this action"); 958} 959} 960 961our$hash_parent_base=$input_params{'hash_parent_base'}; 962if(defined$hash_parent_base) { 963if(!validate_refname($hash_parent_base)) { 964 die_error(400,"Invalid hash parent base parameter"); 965} 966} 967 968# other parameters 969our$page=$input_params{'page'}; 970if(defined$page) { 971if($page=~m/[^0-9]/) { 972 die_error(400,"Invalid page parameter"); 973} 974} 975 976our$searchtype=$input_params{'searchtype'}; 977if(defined$searchtype) { 978if($searchtype=~m/[^a-z]/) { 979 die_error(400,"Invalid searchtype parameter"); 980} 981} 982 983our$search_use_regexp=$input_params{'search_use_regexp'}; 984 985our$searchtext=$input_params{'searchtext'}; 986our$search_regexp; 987if(defined$searchtext) { 988if(length($searchtext) <2) { 989 die_error(403,"At least two characters are required for search parameter"); 990} 991$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 992} 993} 994 995# path to the current git repository 996our$git_dir; 997sub evaluate_git_dir { 998our$git_dir="$projectroot/$project"if$project; 999}10001001our(@snapshot_fmts,$git_avatar);1002sub configure_gitweb_features {1003# list of supported snapshot formats1004our@snapshot_fmts= gitweb_get_feature('snapshot');1005@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);10061007# check that the avatar feature is set to a known provider name,1008# and for each provider check if the dependencies are satisfied.1009# if the provider name is invalid or the dependencies are not met,1010# reset $git_avatar to the empty string.1011our($git_avatar) = gitweb_get_feature('avatar');1012if($git_avatareq'gravatar') {1013$git_avatar=''unless(eval{require Digest::MD5;1; });1014}elsif($git_avatareq'picon') {1015# no dependencies1016}else{1017$git_avatar='';1018}1019}10201021# custom error handler: 'die <message>' is Internal Server Error1022sub handle_errors_html {1023my$msg=shift;# it is already HTML escaped10241025# to avoid infinite loop where error occurs in die_error,1026# change handler to default handler, disabling handle_errors_html1027 set_message("Error occured when inside die_error:\n$msg");10281029# you cannot jump out of die_error when called as error handler;1030# the subroutine set via CGI::Carp::set_message is called _after_1031# HTTP headers are already written, so it cannot write them itself1032 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1);1033}1034set_message(\&handle_errors_html);10351036# dispatch1037sub dispatch {1038if(!defined$action) {1039if(defined$hash) {1040$action= git_get_type($hash);1041}elsif(defined$hash_base&&defined$file_name) {1042$action= git_get_type("$hash_base:$file_name");1043}elsif(defined$project) {1044$action='summary';1045}else{1046$action='project_list';1047}1048}1049if(!defined($actions{$action})) {1050 die_error(400,"Unknown action");1051}1052if($action!~m/^(?:opml|project_list|project_index)$/&&1053!$project) {1054 die_error(400,"Project needed");1055}1056$actions{$action}->();1057}10581059sub reset_timer {1060our$t0= [Time::HiRes::gettimeofday()]1061ifdefined$t0;1062our$number_of_git_cmds=0;1063}10641065sub run_request {1066 reset_timer();10671068 evaluate_uri();1069 evaluate_gitweb_config();1070 check_loadavg();10711072# $projectroot and $projects_list might be set in gitweb config file1073$projects_list||=$projectroot;10741075 evaluate_query_params();1076 evaluate_path_info();1077 evaluate_and_validate_params();1078 evaluate_git_dir();10791080 configure_gitweb_features();10811082 dispatch();1083}10841085our$is_last_request=sub{1};1086our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1087our$CGI='CGI';1088our$cgi;1089sub configure_as_fcgi {1090require CGI::Fast;1091our$CGI='CGI::Fast';10921093my$request_number=0;1094# let each child service 100 requests1095our$is_last_request=sub{ ++$request_number>100};1096}1097sub evaluate_argv {1098my$script_name=$ENV{'SCRIPT_NAME'} ||$ENV{'SCRIPT_FILENAME'} || __FILE__;1099 configure_as_fcgi()1100if$script_name=~/\.fcgi$/;11011102return unless(@ARGV);11031104require Getopt::Long;1105 Getopt::Long::GetOptions(1106'fastcgi|fcgi|f'=> \&configure_as_fcgi,1107'nproc|n=i'=>sub{1108my($arg,$val) =@_;1109return unlesseval{require FCGI::ProcManager;1; };1110my$proc_manager= FCGI::ProcManager->new({1111 n_processes =>$val,1112});1113our$pre_listen_hook=sub{$proc_manager->pm_manage() };1114our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1115our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1116},1117);1118}11191120sub run {1121 evaluate_argv();1122 evaluate_git_version();11231124$pre_listen_hook->()1125if$pre_listen_hook;11261127 REQUEST:1128while($cgi=$CGI->new()) {1129$pre_dispatch_hook->()1130if$pre_dispatch_hook;11311132 run_request();11331134$post_dispatch_hook->()1135if$post_dispatch_hook;11361137last REQUEST if($is_last_request->());1138}11391140 DONE_GITWEB:11411;1142}11431144run();11451146if(defined caller) {1147# wrapped in a subroutine processing requests,1148# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1149return;1150}else{1151# pure CGI script, serving single request1152exit;1153}11541155## ======================================================================1156## action links11571158# possible values of extra options1159# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1160# -replay => 1 - start from a current view (replay with modifications)1161# -path_info => 0|1 - don't use/use path_info URL (if possible)1162sub href {1163my%params=@_;1164# default is to use -absolute url() i.e. $my_uri1165my$href=$params{-full} ?$my_url:$my_uri;11661167$params{'project'} =$projectunlessexists$params{'project'};11681169if($params{-replay}) {1170while(my($name,$symbol) =each%cgi_param_mapping) {1171if(!exists$params{$name}) {1172$params{$name} =$input_params{$name};1173}1174}1175}11761177my$use_pathinfo= gitweb_check_feature('pathinfo');1178if(defined$params{'project'} &&1179(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1180# try to put as many parameters as possible in PATH_INFO:1181# - project name1182# - action1183# - hash_parent or hash_parent_base:/file_parent1184# - hash or hash_base:/filename1185# - the snapshot_format as an appropriate suffix11861187# When the script is the root DirectoryIndex for the domain,1188# $href here would be something like http://gitweb.example.com/1189# Thus, we strip any trailing / from $href, to spare us double1190# slashes in the final URL1191$href=~ s,/$,,;11921193# Then add the project name, if present1194$href.="/".esc_url($params{'project'});1195delete$params{'project'};11961197# since we destructively absorb parameters, we keep this1198# boolean that remembers if we're handling a snapshot1199my$is_snapshot=$params{'action'}eq'snapshot';12001201# Summary just uses the project path URL, any other action is1202# added to the URL1203if(defined$params{'action'}) {1204$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1205delete$params{'action'};1206}12071208# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1209# stripping nonexistent or useless pieces1210$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1211||$params{'hash_parent'} ||$params{'hash'});1212if(defined$params{'hash_base'}) {1213if(defined$params{'hash_parent_base'}) {1214$href.= esc_url($params{'hash_parent_base'});1215# skip the file_parent if it's the same as the file_name1216if(defined$params{'file_parent'}) {1217if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1218delete$params{'file_parent'};1219}elsif($params{'file_parent'} !~/\.\./) {1220$href.=":/".esc_url($params{'file_parent'});1221delete$params{'file_parent'};1222}1223}1224$href.="..";1225delete$params{'hash_parent'};1226delete$params{'hash_parent_base'};1227}elsif(defined$params{'hash_parent'}) {1228$href.= esc_url($params{'hash_parent'})."..";1229delete$params{'hash_parent'};1230}12311232$href.= esc_url($params{'hash_base'});1233if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1234$href.=":/".esc_url($params{'file_name'});1235delete$params{'file_name'};1236}1237delete$params{'hash'};1238delete$params{'hash_base'};1239}elsif(defined$params{'hash'}) {1240$href.= esc_url($params{'hash'});1241delete$params{'hash'};1242}12431244# If the action was a snapshot, we can absorb the1245# snapshot_format parameter too1246if($is_snapshot) {1247my$fmt=$params{'snapshot_format'};1248# snapshot_format should always be defined when href()1249# is called, but just in case some code forgets, we1250# fall back to the default1251$fmt||=$snapshot_fmts[0];1252$href.=$known_snapshot_formats{$fmt}{'suffix'};1253delete$params{'snapshot_format'};1254}1255}12561257# now encode the parameters explicitly1258my@result= ();1259for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1260my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1261if(defined$params{$name}) {1262if(ref($params{$name})eq"ARRAY") {1263foreachmy$par(@{$params{$name}}) {1264push@result,$symbol."=". esc_param($par);1265}1266}else{1267push@result,$symbol."=". esc_param($params{$name});1268}1269}1270}1271$href.="?".join(';',@result)ifscalar@result;12721273return$href;1274}127512761277## ======================================================================1278## validation, quoting/unquoting and escaping12791280sub validate_action {1281my$input=shift||returnundef;1282returnundefunlessexists$actions{$input};1283return$input;1284}12851286sub validate_project {1287my$input=shift||returnundef;1288if(!validate_pathname($input) ||1289!(-d "$projectroot/$input") ||1290!check_export_ok("$projectroot/$input") ||1291($strict_export&& !project_in_list($input))) {1292returnundef;1293}else{1294return$input;1295}1296}12971298sub validate_pathname {1299my$input=shift||returnundef;13001301# no '.' or '..' as elements of path, i.e. no '.' nor '..'1302# at the beginning, at the end, and between slashes.1303# also this catches doubled slashes1304if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1305returnundef;1306}1307# no null characters1308if($input=~m!\0!) {1309returnundef;1310}1311return$input;1312}13131314sub validate_refname {1315my$input=shift||returnundef;13161317# textual hashes are O.K.1318if($input=~m/^[0-9a-fA-F]{40}$/) {1319return$input;1320}1321# it must be correct pathname1322$input= validate_pathname($input)1323orreturnundef;1324# restrictions on ref name according to git-check-ref-format1325if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1326returnundef;1327}1328return$input;1329}13301331# decode sequences of octets in utf8 into Perl's internal form,1332# which is utf-8 with utf8 flag set if needed. gitweb writes out1333# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1334sub to_utf8 {1335my$str=shift;1336returnundefunlessdefined$str;1337if(utf8::valid($str)) {1338 utf8::decode($str);1339return$str;1340}else{1341return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1342}1343}13441345# quote unsafe chars, but keep the slash, even when it's not1346# correct, but quoted slashes look too horrible in bookmarks1347sub esc_param {1348my$str=shift;1349returnundefunlessdefined$str;1350$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1351$str=~s/ /\+/g;1352return$str;1353}13541355# quote unsafe chars in whole URL, so some characters cannot be quoted1356sub esc_url {1357my$str=shift;1358returnundefunlessdefined$str;1359$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;1360$str=~s/ /\+/g;1361return$str;1362}13631364# replace invalid utf8 character with SUBSTITUTION sequence1365sub esc_html {1366my$str=shift;1367my%opts=@_;13681369returnundefunlessdefined$str;13701371$str= to_utf8($str);1372$str=$cgi->escapeHTML($str);1373if($opts{'-nbsp'}) {1374$str=~s/ / /g;1375}1376$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1377return$str;1378}13791380# quote control characters and escape filename to HTML1381sub esc_path {1382my$str=shift;1383my%opts=@_;13841385returnundefunlessdefined$str;13861387$str= to_utf8($str);1388$str=$cgi->escapeHTML($str);1389if($opts{'-nbsp'}) {1390$str=~s/ / /g;1391}1392$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1393return$str;1394}13951396# Make control characters "printable", using character escape codes (CEC)1397sub quot_cec {1398my$cntrl=shift;1399my%opts=@_;1400my%es= (# character escape codes, aka escape sequences1401"\t"=>'\t',# tab (HT)1402"\n"=>'\n',# line feed (LF)1403"\r"=>'\r',# carrige return (CR)1404"\f"=>'\f',# form feed (FF)1405"\b"=>'\b',# backspace (BS)1406"\a"=>'\a',# alarm (bell) (BEL)1407"\e"=>'\e',# escape (ESC)1408"\013"=>'\v',# vertical tab (VT)1409"\000"=>'\0',# nul character (NUL)1410);1411my$chr= ( (exists$es{$cntrl})1412?$es{$cntrl}1413:sprintf('\%2x',ord($cntrl)) );1414if($opts{-nohtml}) {1415return$chr;1416}else{1417return"<span class=\"cntrl\">$chr</span>";1418}1419}14201421# Alternatively use unicode control pictures codepoints,1422# Unicode "printable representation" (PR)1423sub quot_upr {1424my$cntrl=shift;1425my%opts=@_;14261427my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1428if($opts{-nohtml}) {1429return$chr;1430}else{1431return"<span class=\"cntrl\">$chr</span>";1432}1433}14341435# git may return quoted and escaped filenames1436sub unquote {1437my$str=shift;14381439sub unq {1440my$seq=shift;1441my%es= (# character escape codes, aka escape sequences1442't'=>"\t",# tab (HT, TAB)1443'n'=>"\n",# newline (NL)1444'r'=>"\r",# return (CR)1445'f'=>"\f",# form feed (FF)1446'b'=>"\b",# backspace (BS)1447'a'=>"\a",# alarm (bell) (BEL)1448'e'=>"\e",# escape (ESC)1449'v'=>"\013",# vertical tab (VT)1450);14511452if($seq=~m/^[0-7]{1,3}$/) {1453# octal char sequence1454returnchr(oct($seq));1455}elsif(exists$es{$seq}) {1456# C escape sequence, aka character escape code1457return$es{$seq};1458}1459# quoted ordinary character1460return$seq;1461}14621463if($str=~m/^"(.*)"$/) {1464# needs unquoting1465$str=$1;1466$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1467}1468return$str;1469}14701471# escape tabs (convert tabs to spaces)1472sub untabify {1473my$line=shift;14741475while((my$pos=index($line,"\t")) != -1) {1476if(my$count= (8- ($pos%8))) {1477my$spaces=' ' x $count;1478$line=~s/\t/$spaces/;1479}1480}14811482return$line;1483}14841485sub project_in_list {1486my$project=shift;1487my@list= git_get_projects_list();1488return@list&&scalar(grep{$_->{'path'}eq$project}@list);1489}14901491## ----------------------------------------------------------------------1492## HTML aware string manipulation14931494# Try to chop given string on a word boundary between position1495# $len and $len+$add_len. If there is no word boundary there,1496# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1497# (marking chopped part) would be longer than given string.1498sub chop_str {1499my$str=shift;1500my$len=shift;1501my$add_len=shift||10;1502my$where=shift||'right';# 'left' | 'center' | 'right'15031504# Make sure perl knows it is utf8 encoded so we don't1505# cut in the middle of a utf8 multibyte char.1506$str= to_utf8($str);15071508# allow only $len chars, but don't cut a word if it would fit in $add_len1509# if it doesn't fit, cut it if it's still longer than the dots we would add1510# remove chopped character entities entirely15111512# when chopping in the middle, distribute $len into left and right part1513# return early if chopping wouldn't make string shorter1514if($whereeq'center') {1515return$strif($len+5>=length($str));# filler is length 51516$len=int($len/2);1517}else{1518return$strif($len+4>=length($str));# filler is length 41519}15201521# regexps: ending and beginning with word part up to $add_len1522my$endre=qr/.{$len}\w{0,$add_len}/;1523my$begre=qr/\w{0,$add_len}.{$len}/;15241525if($whereeq'left') {1526$str=~m/^(.*?)($begre)$/;1527my($lead,$body) = ($1,$2);1528if(length($lead) >4) {1529$lead=" ...";1530}1531return"$lead$body";15321533}elsif($whereeq'center') {1534$str=~m/^($endre)(.*)$/;1535my($left,$str) = ($1,$2);1536$str=~m/^(.*?)($begre)$/;1537my($mid,$right) = ($1,$2);1538if(length($mid) >5) {1539$mid=" ... ";1540}1541return"$left$mid$right";15421543}else{1544$str=~m/^($endre)(.*)$/;1545my$body=$1;1546my$tail=$2;1547if(length($tail) >4) {1548$tail="... ";1549}1550return"$body$tail";1551}1552}15531554# takes the same arguments as chop_str, but also wraps a <span> around the1555# result with a title attribute if it does get chopped. Additionally, the1556# string is HTML-escaped.1557sub chop_and_escape_str {1558my($str) =@_;15591560my$chopped= chop_str(@_);1561if($choppedeq$str) {1562return esc_html($chopped);1563}else{1564$str=~s/[[:cntrl:]]/?/g;1565return$cgi->span({-title=>$str}, esc_html($chopped));1566}1567}15681569## ----------------------------------------------------------------------1570## functions returning short strings15711572# CSS class for given age value (in seconds)1573sub age_class {1574my$age=shift;15751576if(!defined$age) {1577return"noage";1578}elsif($age<60*60*2) {1579return"age0";1580}elsif($age<60*60*24*2) {1581return"age1";1582}else{1583return"age2";1584}1585}15861587# convert age in seconds to "nn units ago" string1588sub age_string {1589my$age=shift;1590my$age_str;15911592if($age>60*60*24*365*2) {1593$age_str= (int$age/60/60/24/365);1594$age_str.=" years ago";1595}elsif($age>60*60*24*(365/12)*2) {1596$age_str=int$age/60/60/24/(365/12);1597$age_str.=" months ago";1598}elsif($age>60*60*24*7*2) {1599$age_str=int$age/60/60/24/7;1600$age_str.=" weeks ago";1601}elsif($age>60*60*24*2) {1602$age_str=int$age/60/60/24;1603$age_str.=" days ago";1604}elsif($age>60*60*2) {1605$age_str=int$age/60/60;1606$age_str.=" hours ago";1607}elsif($age>60*2) {1608$age_str=int$age/60;1609$age_str.=" min ago";1610}elsif($age>2) {1611$age_str=int$age;1612$age_str.=" sec ago";1613}else{1614$age_str.=" right now";1615}1616return$age_str;1617}16181619useconstant{1620 S_IFINVALID =>0030000,1621 S_IFGITLINK =>0160000,1622};16231624# submodule/subproject, a commit object reference1625sub S_ISGITLINK {1626my$mode=shift;16271628return(($mode& S_IFMT) == S_IFGITLINK)1629}16301631# convert file mode in octal to symbolic file mode string1632sub mode_str {1633my$mode=oct shift;16341635if(S_ISGITLINK($mode)) {1636return'm---------';1637}elsif(S_ISDIR($mode& S_IFMT)) {1638return'drwxr-xr-x';1639}elsif(S_ISLNK($mode)) {1640return'lrwxrwxrwx';1641}elsif(S_ISREG($mode)) {1642# git cares only about the executable bit1643if($mode& S_IXUSR) {1644return'-rwxr-xr-x';1645}else{1646return'-rw-r--r--';1647};1648}else{1649return'----------';1650}1651}16521653# convert file mode in octal to file type string1654sub file_type {1655my$mode=shift;16561657if($mode!~m/^[0-7]+$/) {1658return$mode;1659}else{1660$mode=oct$mode;1661}16621663if(S_ISGITLINK($mode)) {1664return"submodule";1665}elsif(S_ISDIR($mode& S_IFMT)) {1666return"directory";1667}elsif(S_ISLNK($mode)) {1668return"symlink";1669}elsif(S_ISREG($mode)) {1670return"file";1671}else{1672return"unknown";1673}1674}16751676# convert file mode in octal to file type description string1677sub file_type_long {1678my$mode=shift;16791680if($mode!~m/^[0-7]+$/) {1681return$mode;1682}else{1683$mode=oct$mode;1684}16851686if(S_ISGITLINK($mode)) {1687return"submodule";1688}elsif(S_ISDIR($mode& S_IFMT)) {1689return"directory";1690}elsif(S_ISLNK($mode)) {1691return"symlink";1692}elsif(S_ISREG($mode)) {1693if($mode& S_IXUSR) {1694return"executable";1695}else{1696return"file";1697};1698}else{1699return"unknown";1700}1701}170217031704## ----------------------------------------------------------------------1705## functions returning short HTML fragments, or transforming HTML fragments1706## which don't belong to other sections17071708# format line of commit message.1709sub format_log_line_html {1710my$line=shift;17111712$line= esc_html($line, -nbsp=>1);1713$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1714$cgi->a({-href => href(action=>"object", hash=>$1),1715-class=>"text"},$1);1716}eg;17171718return$line;1719}17201721# format marker of refs pointing to given object17221723# the destination action is chosen based on object type and current context:1724# - for annotated tags, we choose the tag view unless it's the current view1725# already, in which case we go to shortlog view1726# - for other refs, we keep the current view if we're in history, shortlog or1727# log view, and select shortlog otherwise1728sub format_ref_marker {1729my($refs,$id) =@_;1730my$markers='';17311732if(defined$refs->{$id}) {1733foreachmy$ref(@{$refs->{$id}}) {1734# this code exploits the fact that non-lightweight tags are the1735# only indirect objects, and that they are the only objects for which1736# we want to use tag instead of shortlog as action1737my($type,$name) =qw();1738my$indirect= ($ref=~s/\^\{\}$//);1739# e.g. tags/v2.6.11 or heads/next1740if($ref=~m!^(.*?)s?/(.*)$!) {1741$type=$1;1742$name=$2;1743}else{1744$type="ref";1745$name=$ref;1746}17471748my$class=$type;1749$class.=" indirect"if$indirect;17501751my$dest_action="shortlog";17521753if($indirect) {1754$dest_action="tag"unless$actioneq"tag";1755}elsif($action=~/^(history|(short)?log)$/) {1756$dest_action=$action;1757}17581759my$dest="";1760$dest.="refs/"unless$ref=~ m!^refs/!;1761$dest.=$ref;17621763my$link=$cgi->a({1764-href => href(1765 action=>$dest_action,1766 hash=>$dest1767)},$name);17681769$markers.=" <span class=\"$class\"title=\"$ref\">".1770$link."</span>";1771}1772}17731774if($markers) {1775return' <span class="refs">'.$markers.'</span>';1776}else{1777return"";1778}1779}17801781# format, perhaps shortened and with markers, title line1782sub format_subject_html {1783my($long,$short,$href,$extra) =@_;1784$extra=''unlessdefined($extra);17851786if(length($short) <length($long)) {1787$long=~s/[[:cntrl:]]/?/g;1788return$cgi->a({-href =>$href, -class=>"list subject",1789-title => to_utf8($long)},1790 esc_html($short)) .$extra;1791}else{1792return$cgi->a({-href =>$href, -class=>"list subject"},1793 esc_html($long)) .$extra;1794}1795}17961797# Rather than recomputing the url for an email multiple times, we cache it1798# after the first hit. This gives a visible benefit in views where the avatar1799# for the same email is used repeatedly (e.g. shortlog).1800# The cache is shared by all avatar engines (currently gravatar only), which1801# are free to use it as preferred. Since only one avatar engine is used for any1802# given page, there's no risk for cache conflicts.1803our%avatar_cache= ();18041805# Compute the picon url for a given email, by using the picon search service over at1806# http://www.cs.indiana.edu/picons/search.html1807sub picon_url {1808my$email=lc shift;1809if(!$avatar_cache{$email}) {1810my($user,$domain) =split('@',$email);1811$avatar_cache{$email} =1812"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1813"$domain/$user/".1814"users+domains+unknown/up/single";1815}1816return$avatar_cache{$email};1817}18181819# Compute the gravatar url for a given email, if it's not in the cache already.1820# Gravatar stores only the part of the URL before the size, since that's the1821# one computationally more expensive. This also allows reuse of the cache for1822# different sizes (for this particular engine).1823sub gravatar_url {1824my$email=lc shift;1825my$size=shift;1826$avatar_cache{$email} ||=1827"http://www.gravatar.com/avatar/".1828 Digest::MD5::md5_hex($email) ."?s=";1829return$avatar_cache{$email} .$size;1830}18311832# Insert an avatar for the given $email at the given $size if the feature1833# is enabled.1834sub git_get_avatar {1835my($email,%opts) =@_;1836my$pre_white= ($opts{-pad_before} ?" ":"");1837my$post_white= ($opts{-pad_after} ?" ":"");1838$opts{-size} ||='default';1839my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1840my$url="";1841if($git_avatareq'gravatar') {1842$url= gravatar_url($email,$size);1843}elsif($git_avatareq'picon') {1844$url= picon_url($email);1845}1846# Other providers can be added by extending the if chain, defining $url1847# as needed. If no variant puts something in $url, we assume avatars1848# are completely disabled/unavailable.1849if($url) {1850return$pre_white.1851"<img width=\"$size\"".1852"class=\"avatar\"".1853"src=\"$url\"".1854"alt=\"\"".1855"/>".$post_white;1856}else{1857return"";1858}1859}18601861sub format_search_author {1862my($author,$searchtype,$displaytext) =@_;1863my$have_search= gitweb_check_feature('search');18641865if($have_search) {1866my$performed="";1867if($searchtypeeq'author') {1868$performed="authored";1869}elsif($searchtypeeq'committer') {1870$performed="committed";1871}18721873return$cgi->a({-href => href(action=>"search", hash=>$hash,1874 searchtext=>$author,1875 searchtype=>$searchtype),class=>"list",1876 title=>"Search for commits$performedby$author"},1877$displaytext);18781879}else{1880return$displaytext;1881}1882}18831884# format the author name of the given commit with the given tag1885# the author name is chopped and escaped according to the other1886# optional parameters (see chop_str).1887sub format_author_html {1888my$tag=shift;1889my$co=shift;1890my$author= chop_and_escape_str($co->{'author_name'},@_);1891return"<$tagclass=\"author\">".1892 format_search_author($co->{'author_name'},"author",1893 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1894$author) .1895"</$tag>";1896}18971898# format git diff header line, i.e. "diff --(git|combined|cc) ..."1899sub format_git_diff_header_line {1900my$line=shift;1901my$diffinfo=shift;1902my($from,$to) =@_;19031904if($diffinfo->{'nparents'}) {1905# combined diff1906$line=~s!^(diff (.*?) )"?.*$!$1!;1907if($to->{'href'}) {1908$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1909 esc_path($to->{'file'}));1910}else{# file was deleted (no href)1911$line.= esc_path($to->{'file'});1912}1913}else{1914# "ordinary" diff1915$line=~s!^(diff (.*?) )"?a/.*$!$1!;1916if($from->{'href'}) {1917$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1918'a/'. esc_path($from->{'file'}));1919}else{# file was added (no href)1920$line.='a/'. esc_path($from->{'file'});1921}1922$line.=' ';1923if($to->{'href'}) {1924$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1925'b/'. esc_path($to->{'file'}));1926}else{# file was deleted1927$line.='b/'. esc_path($to->{'file'});1928}1929}19301931return"<div class=\"diff header\">$line</div>\n";1932}19331934# format extended diff header line, before patch itself1935sub format_extended_diff_header_line {1936my$line=shift;1937my$diffinfo=shift;1938my($from,$to) =@_;19391940# match <path>1941if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1942$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1943 esc_path($from->{'file'}));1944}1945if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1946$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1947 esc_path($to->{'file'}));1948}1949# match single <mode>1950if($line=~m/\s(\d{6})$/) {1951$line.='<span class="info"> ('.1952 file_type_long($1) .1953')</span>';1954}1955# match <hash>1956if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1957# can match only for combined diff1958$line='index ';1959for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1960if($from->{'href'}[$i]) {1961$line.=$cgi->a({-href=>$from->{'href'}[$i],1962-class=>"hash"},1963substr($diffinfo->{'from_id'}[$i],0,7));1964}else{1965$line.='0' x 7;1966}1967# separator1968$line.=','if($i<$diffinfo->{'nparents'} -1);1969}1970$line.='..';1971if($to->{'href'}) {1972$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1973substr($diffinfo->{'to_id'},0,7));1974}else{1975$line.='0' x 7;1976}19771978}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1979# can match only for ordinary diff1980my($from_link,$to_link);1981if($from->{'href'}) {1982$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1983substr($diffinfo->{'from_id'},0,7));1984}else{1985$from_link='0' x 7;1986}1987if($to->{'href'}) {1988$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1989substr($diffinfo->{'to_id'},0,7));1990}else{1991$to_link='0' x 7;1992}1993my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1994$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1995}19961997return$line."<br/>\n";1998}19992000# format from-file/to-file diff header2001sub format_diff_from_to_header {2002my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;2003my$line;2004my$result='';20052006$line=$from_line;2007#assert($line =~ m/^---/) if DEBUG;2008# no extra formatting for "^--- /dev/null"2009if(!$diffinfo->{'nparents'}) {2010# ordinary (single parent) diff2011if($line=~m!^--- "?a/!) {2012if($from->{'href'}) {2013$line='--- a/'.2014$cgi->a({-href=>$from->{'href'}, -class=>"path"},2015 esc_path($from->{'file'}));2016}else{2017$line='--- a/'.2018 esc_path($from->{'file'});2019}2020}2021$result.= qq!<div class="diff from_file">$line</div>\n!;20222023}else{2024# combined diff (merge commit)2025for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2026if($from->{'href'}[$i]) {2027$line='--- '.2028$cgi->a({-href=>href(action=>"blobdiff",2029 hash_parent=>$diffinfo->{'from_id'}[$i],2030 hash_parent_base=>$parents[$i],2031 file_parent=>$from->{'file'}[$i],2032 hash=>$diffinfo->{'to_id'},2033 hash_base=>$hash,2034 file_name=>$to->{'file'}),2035-class=>"path",2036-title=>"diff". ($i+1)},2037$i+1) .2038'/'.2039$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2040 esc_path($from->{'file'}[$i]));2041}else{2042$line='--- /dev/null';2043}2044$result.= qq!<div class="diff from_file">$line</div>\n!;2045}2046}20472048$line=$to_line;2049#assert($line =~ m/^\+\+\+/) if DEBUG;2050# no extra formatting for "^+++ /dev/null"2051if($line=~m!^\+\+\+ "?b/!) {2052if($to->{'href'}) {2053$line='+++ b/'.2054$cgi->a({-href=>$to->{'href'}, -class=>"path"},2055 esc_path($to->{'file'}));2056}else{2057$line='+++ b/'.2058 esc_path($to->{'file'});2059}2060}2061$result.= qq!<div class="diff to_file">$line</div>\n!;20622063return$result;2064}20652066# create note for patch simplified by combined diff2067sub format_diff_cc_simplified {2068my($diffinfo,@parents) =@_;2069my$result='';20702071$result.="<div class=\"diff header\">".2072"diff --cc ";2073if(!is_deleted($diffinfo)) {2074$result.=$cgi->a({-href => href(action=>"blob",2075 hash_base=>$hash,2076 hash=>$diffinfo->{'to_id'},2077 file_name=>$diffinfo->{'to_file'}),2078-class=>"path"},2079 esc_path($diffinfo->{'to_file'}));2080}else{2081$result.= esc_path($diffinfo->{'to_file'});2082}2083$result.="</div>\n".# class="diff header"2084"<div class=\"diff nodifferences\">".2085"Simple merge".2086"</div>\n";# class="diff nodifferences"20872088return$result;2089}20902091# format patch (diff) line (not to be used for diff headers)2092sub format_diff_line {2093my$line=shift;2094my($from,$to) =@_;2095my$diff_class="";20962097chomp$line;20982099if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2100# combined diff2101my$prefix=substr($line,0,scalar@{$from->{'href'}});2102if($line=~m/^\@{3}/) {2103$diff_class=" chunk_header";2104}elsif($line=~m/^\\/) {2105$diff_class=" incomplete";2106}elsif($prefix=~tr/+/+/) {2107$diff_class=" add";2108}elsif($prefix=~tr/-/-/) {2109$diff_class=" rem";2110}2111}else{2112# assume ordinary diff2113my$char=substr($line,0,1);2114if($chareq'+') {2115$diff_class=" add";2116}elsif($chareq'-') {2117$diff_class=" rem";2118}elsif($chareq'@') {2119$diff_class=" chunk_header";2120}elsif($chareq"\\") {2121$diff_class=" incomplete";2122}2123}2124$line= untabify($line);2125if($from&&$to&&$line=~m/^\@{2} /) {2126my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2127$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;21282129$from_lines=0unlessdefined$from_lines;2130$to_lines=0unlessdefined$to_lines;21312132if($from->{'href'}) {2133$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2134-class=>"list"},$from_text);2135}2136if($to->{'href'}) {2137$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2138-class=>"list"},$to_text);2139}2140$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2141"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2142return"<div class=\"diff$diff_class\">$line</div>\n";2143}elsif($from&&$to&&$line=~m/^\@{3}/) {2144my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2145my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);21462147@from_text=split(' ',$ranges);2148for(my$i=0;$i<@from_text; ++$i) {2149($from_start[$i],$from_nlines[$i]) =2150(split(',',substr($from_text[$i],1)),0);2151}21522153$to_text=pop@from_text;2154$to_start=pop@from_start;2155$to_nlines=pop@from_nlines;21562157$line="<span class=\"chunk_info\">$prefix";2158for(my$i=0;$i<@from_text; ++$i) {2159if($from->{'href'}[$i]) {2160$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2161-class=>"list"},$from_text[$i]);2162}else{2163$line.=$from_text[$i];2164}2165$line.=" ";2166}2167if($to->{'href'}) {2168$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2169-class=>"list"},$to_text);2170}else{2171$line.=$to_text;2172}2173$line.="$prefix</span>".2174"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2175return"<div class=\"diff$diff_class\">$line</div>\n";2176}2177return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2178}21792180# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2181# linked. Pass the hash of the tree/commit to snapshot.2182sub format_snapshot_links {2183my($hash) =@_;2184my$num_fmts=@snapshot_fmts;2185if($num_fmts>1) {2186# A parenthesized list of links bearing format names.2187# e.g. "snapshot (_tar.gz_ _zip_)"2188return"snapshot (".join(' ',map2189$cgi->a({2190-href => href(2191 action=>"snapshot",2192 hash=>$hash,2193 snapshot_format=>$_2194)2195},$known_snapshot_formats{$_}{'display'})2196,@snapshot_fmts) .")";2197}elsif($num_fmts==1) {2198# A single "snapshot" link whose tooltip bears the format name.2199# i.e. "_snapshot_"2200my($fmt) =@snapshot_fmts;2201return2202$cgi->a({2203-href => href(2204 action=>"snapshot",2205 hash=>$hash,2206 snapshot_format=>$fmt2207),2208-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2209},"snapshot");2210}else{# $num_fmts == 02211returnundef;2212}2213}22142215## ......................................................................2216## functions returning values to be passed, perhaps after some2217## transformation, to other functions; e.g. returning arguments to href()22182219# returns hash to be passed to href to generate gitweb URL2220# in -title key it returns description of link2221sub get_feed_info {2222my$format=shift||'Atom';2223my%res= (action =>lc($format));22242225# feed links are possible only for project views2226return unless(defined$project);2227# some views should link to OPML, or to generic project feed,2228# or don't have specific feed yet (so they should use generic)2229return if($action=~/^(?:tags|heads|forks|tag|search)$/x);22302231my$branch;2232# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2233# from tag links; this also makes possible to detect branch links2234if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2235(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2236$branch=$1;2237}2238# find log type for feed description (title)2239my$type='log';2240if(defined$file_name) {2241$type="history of$file_name";2242$type.="/"if($actioneq'tree');2243$type.=" on '$branch'"if(defined$branch);2244}else{2245$type="log of$branch"if(defined$branch);2246}22472248$res{-title} =$type;2249$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2250$res{'file_name'} =$file_name;22512252return%res;2253}22542255## ----------------------------------------------------------------------2256## git utility subroutines, invoking git commands22572258# returns path to the core git executable and the --git-dir parameter as list2259sub git_cmd {2260$number_of_git_cmds++;2261return$GIT,'--git-dir='.$git_dir;2262}22632264# quote the given arguments for passing them to the shell2265# quote_command("command", "arg 1", "arg with ' and ! characters")2266# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2267# Try to avoid using this function wherever possible.2268sub quote_command {2269returnjoin(' ',2270map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2271}22722273# get HEAD ref of given project as hash2274sub git_get_head_hash {2275return git_get_full_hash(shift,'HEAD');2276}22772278sub git_get_full_hash {2279return git_get_hash(@_);2280}22812282sub git_get_short_hash {2283return git_get_hash(@_,'--short=7');2284}22852286sub git_get_hash {2287my($project,$hash,@options) =@_;2288my$o_git_dir=$git_dir;2289my$retval=undef;2290$git_dir="$projectroot/$project";2291if(open my$fd,'-|', git_cmd(),'rev-parse',2292'--verify','-q',@options,$hash) {2293$retval= <$fd>;2294chomp$retvalifdefined$retval;2295close$fd;2296}2297if(defined$o_git_dir) {2298$git_dir=$o_git_dir;2299}2300return$retval;2301}23022303# get type of given object2304sub git_get_type {2305my$hash=shift;23062307open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2308my$type= <$fd>;2309close$fdorreturn;2310chomp$type;2311return$type;2312}23132314# repository configuration2315our$config_file='';2316our%config;23172318# store multiple values for single key as anonymous array reference2319# single values stored directly in the hash, not as [ <value> ]2320sub hash_set_multi {2321my($hash,$key,$value) =@_;23222323if(!exists$hash->{$key}) {2324$hash->{$key} =$value;2325}elsif(!ref$hash->{$key}) {2326$hash->{$key} = [$hash->{$key},$value];2327}else{2328push@{$hash->{$key}},$value;2329}2330}23312332# return hash of git project configuration2333# optionally limited to some section, e.g. 'gitweb'2334sub git_parse_project_config {2335my$section_regexp=shift;2336my%config;23372338local$/="\0";23392340open my$fh,"-|", git_cmd(),"config",'-z','-l',2341orreturn;23422343while(my$keyval= <$fh>) {2344chomp$keyval;2345my($key,$value) =split(/\n/,$keyval,2);23462347 hash_set_multi(\%config,$key,$value)2348if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2349}2350close$fh;23512352return%config;2353}23542355# convert config value to boolean: 'true' or 'false'2356# no value, number > 0, 'true' and 'yes' values are true2357# rest of values are treated as false (never as error)2358sub config_to_bool {2359my$val=shift;23602361return1if!defined$val;# section.key23622363# strip leading and trailing whitespace2364$val=~s/^\s+//;2365$val=~s/\s+$//;23662367return(($val=~/^\d+$/&&$val) ||# section.key = 12368($val=~/^(?:true|yes)$/i));# section.key = true2369}23702371# convert config value to simple decimal number2372# an optional value suffix of 'k', 'm', or 'g' will cause the value2373# to be multiplied by 1024, 1048576, or 10737418242374sub config_to_int {2375my$val=shift;23762377# strip leading and trailing whitespace2378$val=~s/^\s+//;2379$val=~s/\s+$//;23802381if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2382$unit=lc($unit);2383# unknown unit is treated as 12384return$num* ($uniteq'g'?1073741824:2385$uniteq'm'?1048576:2386$uniteq'k'?1024:1);2387}2388return$val;2389}23902391# convert config value to array reference, if needed2392sub config_to_multi {2393my$val=shift;23942395returnref($val) ?$val: (defined($val) ? [$val] : []);2396}23972398sub git_get_project_config {2399my($key,$type) =@_;24002401return unlessdefined$git_dir;24022403# key sanity check2404return unless($key);2405$key=~s/^gitweb\.//;2406return if($key=~m/\W/);24072408# type sanity check2409if(defined$type) {2410$type=~s/^--//;2411$type=undef2412unless($typeeq'bool'||$typeeq'int');2413}24142415# get config2416if(!defined$config_file||2417$config_filene"$git_dir/config") {2418%config= git_parse_project_config('gitweb');2419$config_file="$git_dir/config";2420}24212422# check if config variable (key) exists2423return unlessexists$config{"gitweb.$key"};24242425# ensure given type2426if(!defined$type) {2427return$config{"gitweb.$key"};2428}elsif($typeeq'bool') {2429# backward compatibility: 'git config --bool' returns true/false2430return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2431}elsif($typeeq'int') {2432return config_to_int($config{"gitweb.$key"});2433}2434return$config{"gitweb.$key"};2435}24362437# get hash of given path at given ref2438sub git_get_hash_by_path {2439my$base=shift;2440my$path=shift||returnundef;2441my$type=shift;24422443$path=~ s,/+$,,;24442445open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2446or die_error(500,"Open git-ls-tree failed");2447my$line= <$fd>;2448close$fdorreturnundef;24492450if(!defined$line) {2451# there is no tree or hash given by $path at $base2452returnundef;2453}24542455#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2456$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2457if(defined$type&&$typene$2) {2458# type doesn't match2459returnundef;2460}2461return$3;2462}24632464# get path of entry with given hash at given tree-ish (ref)2465# used to get 'from' filename for combined diff (merge commit) for renames2466sub git_get_path_by_hash {2467my$base=shift||return;2468my$hash=shift||return;24692470local$/="\0";24712472open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2473orreturnundef;2474while(my$line= <$fd>) {2475chomp$line;24762477#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2478#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2479if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2480close$fd;2481return$1;2482}2483}2484close$fd;2485returnundef;2486}24872488## ......................................................................2489## git utility functions, directly accessing git repository24902491sub git_get_project_description {2492my$path=shift;24932494$git_dir="$projectroot/$path";2495open my$fd,'<',"$git_dir/description"2496orreturn git_get_project_config('description');2497my$descr= <$fd>;2498close$fd;2499if(defined$descr) {2500chomp$descr;2501}2502return$descr;2503}25042505sub git_get_project_ctags {2506my$path=shift;2507my$ctags= {};25082509$git_dir="$projectroot/$path";2510opendir my$dh,"$git_dir/ctags"2511orreturn$ctags;2512foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2513open my$ct,'<',$_ornext;2514my$val= <$ct>;2515chomp$val;2516close$ct;2517my$ctag=$_;$ctag=~ s#.*/##;2518$ctags->{$ctag} =$val;2519}2520closedir$dh;2521$ctags;2522}25232524sub git_populate_project_tagcloud {2525my$ctags=shift;25262527# First, merge different-cased tags; tags vote on casing2528my%ctags_lc;2529foreach(keys%$ctags) {2530$ctags_lc{lc$_}->{count} +=$ctags->{$_};2531if(not$ctags_lc{lc$_}->{topcount}2532or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2533$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2534$ctags_lc{lc$_}->{topname} =$_;2535}2536}25372538my$cloud;2539if(eval{require HTML::TagCloud;1; }) {2540$cloud= HTML::TagCloud->new;2541foreach(sort keys%ctags_lc) {2542# Pad the title with spaces so that the cloud looks2543# less crammed.2544my$title=$ctags_lc{$_}->{topname};2545$title=~s/ / /g;2546$title=~s/^/ /g;2547$title=~s/$/ /g;2548$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2549}2550}else{2551$cloud= \%ctags_lc;2552}2553$cloud;2554}25552556sub git_show_project_tagcloud {2557my($cloud,$count) =@_;2558print STDERR ref($cloud)."..\n";2559if(ref$cloudeq'HTML::TagCloud') {2560return$cloud->html_and_css($count);2561}else{2562my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2563return'<p align="center">'.join(', ',map{2564"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2565}splice(@tags,0,$count)) .'</p>';2566}2567}25682569sub git_get_project_url_list {2570my$path=shift;25712572$git_dir="$projectroot/$path";2573open my$fd,'<',"$git_dir/cloneurl"2574orreturnwantarray?2575@{ config_to_multi(git_get_project_config('url')) } :2576 config_to_multi(git_get_project_config('url'));2577my@git_project_url_list=map{chomp;$_} <$fd>;2578close$fd;25792580returnwantarray?@git_project_url_list: \@git_project_url_list;2581}25822583sub git_get_projects_list {2584my($filter) =@_;2585my@list;25862587$filter||='';2588$filter=~s/\.git$//;25892590my$check_forks= gitweb_check_feature('forks');25912592if(-d $projects_list) {2593# search in directory2594my$dir=$projects_list. ($filter?"/$filter":'');2595# remove the trailing "/"2596$dir=~s!/+$!!;2597my$pfxlen=length("$dir");2598my$pfxdepth= ($dir=~tr!/!!);25992600 File::Find::find({2601 follow_fast =>1,# follow symbolic links2602 follow_skip =>2,# ignore duplicates2603 dangling_symlinks =>0,# ignore dangling symlinks, silently2604 wanted =>sub{2605# global variables2606our$project_maxdepth;2607our$projectroot;2608# skip project-list toplevel, if we get it.2609return if(m!^[/.]$!);2610# only directories can be git repositories2611return unless(-d $_);2612# don't traverse too deep (Find is super slow on os x)2613if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2614$File::Find::prune =1;2615return;2616}26172618my$subdir=substr($File::Find::name,$pfxlen+1);2619# we check related file in $projectroot2620my$path= ($filter?"$filter/":'') .$subdir;2621if(check_export_ok("$projectroot/$path")) {2622push@list, { path =>$path};2623$File::Find::prune =1;2624}2625},2626},"$dir");26272628}elsif(-f $projects_list) {2629# read from file(url-encoded):2630# 'git%2Fgit.git Linus+Torvalds'2631# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2632# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2633my%paths;2634open my$fd,'<',$projects_listorreturn;2635 PROJECT:2636while(my$line= <$fd>) {2637chomp$line;2638my($path,$owner) =split' ',$line;2639$path= unescape($path);2640$owner= unescape($owner);2641if(!defined$path) {2642next;2643}2644if($filterne'') {2645# looking for forks;2646my$pfx=substr($path,0,length($filter));2647if($pfxne$filter) {2648next PROJECT;2649}2650my$sfx=substr($path,length($filter));2651if($sfx!~/^\/.*\.git$/) {2652next PROJECT;2653}2654}elsif($check_forks) {2655 PATH:2656foreachmy$filter(keys%paths) {2657# looking for forks;2658my$pfx=substr($path,0,length($filter));2659if($pfxne$filter) {2660next PATH;2661}2662my$sfx=substr($path,length($filter));2663if($sfx!~/^\/.*\.git$/) {2664next PATH;2665}2666# is a fork, don't include it in2667# the list2668next PROJECT;2669}2670}2671if(check_export_ok("$projectroot/$path")) {2672my$pr= {2673 path =>$path,2674 owner => to_utf8($owner),2675};2676push@list,$pr;2677(my$forks_path=$path) =~s/\.git$//;2678$paths{$forks_path}++;2679}2680}2681close$fd;2682}2683return@list;2684}26852686our$gitweb_project_owner=undef;2687sub git_get_project_list_from_file {26882689return if(defined$gitweb_project_owner);26902691$gitweb_project_owner= {};2692# read from file (url-encoded):2693# 'git%2Fgit.git Linus+Torvalds'2694# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2695# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2696if(-f $projects_list) {2697open(my$fd,'<',$projects_list);2698while(my$line= <$fd>) {2699chomp$line;2700my($pr,$ow) =split' ',$line;2701$pr= unescape($pr);2702$ow= unescape($ow);2703$gitweb_project_owner->{$pr} = to_utf8($ow);2704}2705close$fd;2706}2707}27082709sub git_get_project_owner {2710my$project=shift;2711my$owner;27122713returnundefunless$project;2714$git_dir="$projectroot/$project";27152716if(!defined$gitweb_project_owner) {2717 git_get_project_list_from_file();2718}27192720if(exists$gitweb_project_owner->{$project}) {2721$owner=$gitweb_project_owner->{$project};2722}2723if(!defined$owner){2724$owner= git_get_project_config('owner');2725}2726if(!defined$owner) {2727$owner= get_file_owner("$git_dir");2728}27292730return$owner;2731}27322733sub git_get_last_activity {2734my($path) =@_;2735my$fd;27362737$git_dir="$projectroot/$path";2738open($fd,"-|", git_cmd(),'for-each-ref',2739'--format=%(committer)',2740'--sort=-committerdate',2741'--count=1',2742'refs/heads')orreturn;2743my$most_recent= <$fd>;2744close$fdorreturn;2745if(defined$most_recent&&2746$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2747my$timestamp=$1;2748my$age=time-$timestamp;2749return($age, age_string($age));2750}2751return(undef,undef);2752}27532754sub git_get_references {2755my$type=shift||"";2756my%refs;2757# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112758# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2759open my$fd,"-|", git_cmd(),"show-ref","--dereference",2760($type? ("--","refs/$type") : ())# use -- <pattern> if $type2761orreturn;27622763while(my$line= <$fd>) {2764chomp$line;2765if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2766if(defined$refs{$1}) {2767push@{$refs{$1}},$2;2768}else{2769$refs{$1} = [$2];2770}2771}2772}2773close$fdorreturn;2774return \%refs;2775}27762777sub git_get_rev_name_tags {2778my$hash=shift||returnundef;27792780open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2781orreturn;2782my$name_rev= <$fd>;2783close$fd;27842785if($name_rev=~ m|^$hash tags/(.*)$|) {2786return$1;2787}else{2788# catches also '$hash undefined' output2789returnundef;2790}2791}27922793## ----------------------------------------------------------------------2794## parse to hash functions27952796sub parse_date {2797my$epoch=shift;2798my$tz=shift||"-0000";27992800my%date;2801my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2802my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2803my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2804$date{'hour'} =$hour;2805$date{'minute'} =$min;2806$date{'mday'} =$mday;2807$date{'day'} =$days[$wday];2808$date{'month'} =$months[$mon];2809$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2810$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2811$date{'mday-time'} =sprintf"%d%s%02d:%02d",2812$mday,$months[$mon],$hour,$min;2813$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",28141900+$year,1+$mon,$mday,$hour,$min,$sec;28152816$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2817my$local=$epoch+ ((int$1+ ($2/60)) *3600);2818($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2819$date{'hour_local'} =$hour;2820$date{'minute_local'} =$min;2821$date{'tz_local'} =$tz;2822$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",28231900+$year,$mon+1,$mday,2824$hour,$min,$sec,$tz);2825return%date;2826}28272828sub parse_tag {2829my$tag_id=shift;2830my%tag;2831my@comment;28322833open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2834$tag{'id'} =$tag_id;2835while(my$line= <$fd>) {2836chomp$line;2837if($line=~m/^object ([0-9a-fA-F]{40})$/) {2838$tag{'object'} =$1;2839}elsif($line=~m/^type (.+)$/) {2840$tag{'type'} =$1;2841}elsif($line=~m/^tag (.+)$/) {2842$tag{'name'} =$1;2843}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2844$tag{'author'} =$1;2845$tag{'author_epoch'} =$2;2846$tag{'author_tz'} =$3;2847if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2848$tag{'author_name'} =$1;2849$tag{'author_email'} =$2;2850}else{2851$tag{'author_name'} =$tag{'author'};2852}2853}elsif($line=~m/--BEGIN/) {2854push@comment,$line;2855last;2856}elsif($lineeq"") {2857last;2858}2859}2860push@comment, <$fd>;2861$tag{'comment'} = \@comment;2862close$fdorreturn;2863if(!defined$tag{'name'}) {2864return2865};2866return%tag2867}28682869sub parse_commit_text {2870my($commit_text,$withparents) =@_;2871my@commit_lines=split'\n',$commit_text;2872my%co;28732874pop@commit_lines;# Remove '\0'28752876if(!@commit_lines) {2877return;2878}28792880my$header=shift@commit_lines;2881if($header!~m/^[0-9a-fA-F]{40}/) {2882return;2883}2884($co{'id'},my@parents) =split' ',$header;2885while(my$line=shift@commit_lines) {2886last if$lineeq"\n";2887if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2888$co{'tree'} =$1;2889}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2890push@parents,$1;2891}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2892$co{'author'} = to_utf8($1);2893$co{'author_epoch'} =$2;2894$co{'author_tz'} =$3;2895if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2896$co{'author_name'} =$1;2897$co{'author_email'} =$2;2898}else{2899$co{'author_name'} =$co{'author'};2900}2901}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2902$co{'committer'} = to_utf8($1);2903$co{'committer_epoch'} =$2;2904$co{'committer_tz'} =$3;2905if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2906$co{'committer_name'} =$1;2907$co{'committer_email'} =$2;2908}else{2909$co{'committer_name'} =$co{'committer'};2910}2911}2912}2913if(!defined$co{'tree'}) {2914return;2915};2916$co{'parents'} = \@parents;2917$co{'parent'} =$parents[0];29182919foreachmy$title(@commit_lines) {2920$title=~s/^ //;2921if($titlene"") {2922$co{'title'} = chop_str($title,80,5);2923# remove leading stuff of merges to make the interesting part visible2924if(length($title) >50) {2925$title=~s/^Automatic //;2926$title=~s/^merge (of|with) /Merge ... /i;2927if(length($title) >50) {2928$title=~s/(http|rsync):\/\///;2929}2930if(length($title) >50) {2931$title=~s/(master|www|rsync)\.//;2932}2933if(length($title) >50) {2934$title=~s/kernel.org:?//;2935}2936if(length($title) >50) {2937$title=~s/\/pub\/scm//;2938}2939}2940$co{'title_short'} = chop_str($title,50,5);2941last;2942}2943}2944if(!defined$co{'title'} ||$co{'title'}eq"") {2945$co{'title'} =$co{'title_short'} ='(no commit message)';2946}2947# remove added spaces2948foreachmy$line(@commit_lines) {2949$line=~s/^ //;2950}2951$co{'comment'} = \@commit_lines;29522953my$age=time-$co{'committer_epoch'};2954$co{'age'} =$age;2955$co{'age_string'} = age_string($age);2956my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2957if($age>60*60*24*7*2) {2958$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2959$co{'age_string_age'} =$co{'age_string'};2960}else{2961$co{'age_string_date'} =$co{'age_string'};2962$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2963}2964return%co;2965}29662967sub parse_commit {2968my($commit_id) =@_;2969my%co;29702971local$/="\0";29722973open my$fd,"-|", git_cmd(),"rev-list",2974"--parents",2975"--header",2976"--max-count=1",2977$commit_id,2978"--",2979or die_error(500,"Open git-rev-list failed");2980%co= parse_commit_text(<$fd>,1);2981close$fd;29822983return%co;2984}29852986sub parse_commits {2987my($commit_id,$maxcount,$skip,$filename,@args) =@_;2988my@cos;29892990$maxcount||=1;2991$skip||=0;29922993local$/="\0";29942995open my$fd,"-|", git_cmd(),"rev-list",2996"--header",2997@args,2998("--max-count=".$maxcount),2999("--skip=".$skip),3000@extra_options,3001$commit_id,3002"--",3003($filename? ($filename) : ())3004or die_error(500,"Open git-rev-list failed");3005while(my$line= <$fd>) {3006my%co= parse_commit_text($line);3007push@cos, \%co;3008}3009close$fd;30103011returnwantarray?@cos: \@cos;3012}30133014# parse line of git-diff-tree "raw" output3015sub parse_difftree_raw_line {3016my$line=shift;3017my%res;30183019# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'3020# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'3021if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {3022$res{'from_mode'} =$1;3023$res{'to_mode'} =$2;3024$res{'from_id'} =$3;3025$res{'to_id'} =$4;3026$res{'status'} =$5;3027$res{'similarity'} =$6;3028if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3029($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3030}else{3031$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3032}3033}3034# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3035# combined diff (for merge commit)3036elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3037$res{'nparents'} =length($1);3038$res{'from_mode'} = [split(' ',$2) ];3039$res{'to_mode'} =pop@{$res{'from_mode'}};3040$res{'from_id'} = [split(' ',$3) ];3041$res{'to_id'} =pop@{$res{'from_id'}};3042$res{'status'} = [split('',$4) ];3043$res{'to_file'} = unquote($5);3044}3045# 'c512b523472485aef4fff9e57b229d9d243c967f'3046elsif($line=~m/^([0-9a-fA-F]{40})$/) {3047$res{'commit'} =$1;3048}30493050returnwantarray?%res: \%res;3051}30523053# wrapper: return parsed line of git-diff-tree "raw" output3054# (the argument might be raw line, or parsed info)3055sub parsed_difftree_line {3056my$line_or_ref=shift;30573058if(ref($line_or_ref)eq"HASH") {3059# pre-parsed (or generated by hand)3060return$line_or_ref;3061}else{3062return parse_difftree_raw_line($line_or_ref);3063}3064}30653066# parse line of git-ls-tree output3067sub parse_ls_tree_line {3068my$line=shift;3069my%opts=@_;3070my%res;30713072if($opts{'-l'}) {3073#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3074$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;30753076$res{'mode'} =$1;3077$res{'type'} =$2;3078$res{'hash'} =$3;3079$res{'size'} =$4;3080if($opts{'-z'}) {3081$res{'name'} =$5;3082}else{3083$res{'name'} = unquote($5);3084}3085}else{3086#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3087$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;30883089$res{'mode'} =$1;3090$res{'type'} =$2;3091$res{'hash'} =$3;3092if($opts{'-z'}) {3093$res{'name'} =$4;3094}else{3095$res{'name'} = unquote($4);3096}3097}30983099returnwantarray?%res: \%res;3100}31013102# generates _two_ hashes, references to which are passed as 2 and 3 argument3103sub parse_from_to_diffinfo {3104my($diffinfo,$from,$to,@parents) =@_;31053106if($diffinfo->{'nparents'}) {3107# combined diff3108$from->{'file'} = [];3109$from->{'href'} = [];3110 fill_from_file_info($diffinfo,@parents)3111unlessexists$diffinfo->{'from_file'};3112for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3113$from->{'file'}[$i] =3114defined$diffinfo->{'from_file'}[$i] ?3115$diffinfo->{'from_file'}[$i] :3116$diffinfo->{'to_file'};3117if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3118$from->{'href'}[$i] = href(action=>"blob",3119 hash_base=>$parents[$i],3120 hash=>$diffinfo->{'from_id'}[$i],3121 file_name=>$from->{'file'}[$i]);3122}else{3123$from->{'href'}[$i] =undef;3124}3125}3126}else{3127# ordinary (not combined) diff3128$from->{'file'} =$diffinfo->{'from_file'};3129if($diffinfo->{'status'}ne"A") {# not new (added) file3130$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3131 hash=>$diffinfo->{'from_id'},3132 file_name=>$from->{'file'});3133}else{3134delete$from->{'href'};3135}3136}31373138$to->{'file'} =$diffinfo->{'to_file'};3139if(!is_deleted($diffinfo)) {# file exists in result3140$to->{'href'} = href(action=>"blob", hash_base=>$hash,3141 hash=>$diffinfo->{'to_id'},3142 file_name=>$to->{'file'});3143}else{3144delete$to->{'href'};3145}3146}31473148## ......................................................................3149## parse to array of hashes functions31503151sub git_get_heads_list {3152my$limit=shift;3153my@headslist;31543155open my$fd,'-|', git_cmd(),'for-each-ref',3156($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3157'--format=%(objectname) %(refname) %(subject)%00%(committer)',3158'refs/heads'3159orreturn;3160while(my$line= <$fd>) {3161my%ref_item;31623163chomp$line;3164my($refinfo,$committerinfo) =split(/\0/,$line);3165my($hash,$name,$title) =split(' ',$refinfo,3);3166my($committer,$epoch,$tz) =3167($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3168$ref_item{'fullname'} =$name;3169$name=~s!^refs/heads/!!;31703171$ref_item{'name'} =$name;3172$ref_item{'id'} =$hash;3173$ref_item{'title'} =$title||'(no commit message)';3174$ref_item{'epoch'} =$epoch;3175if($epoch) {3176$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3177}else{3178$ref_item{'age'} ="unknown";3179}31803181push@headslist, \%ref_item;3182}3183close$fd;31843185returnwantarray?@headslist: \@headslist;3186}31873188sub git_get_tags_list {3189my$limit=shift;3190my@tagslist;31913192open my$fd,'-|', git_cmd(),'for-each-ref',3193($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3194'--format=%(objectname) %(objecttype) %(refname) '.3195'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3196'refs/tags'3197orreturn;3198while(my$line= <$fd>) {3199my%ref_item;32003201chomp$line;3202my($refinfo,$creatorinfo) =split(/\0/,$line);3203my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3204my($creator,$epoch,$tz) =3205($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3206$ref_item{'fullname'} =$name;3207$name=~s!^refs/tags/!!;32083209$ref_item{'type'} =$type;3210$ref_item{'id'} =$id;3211$ref_item{'name'} =$name;3212if($typeeq"tag") {3213$ref_item{'subject'} =$title;3214$ref_item{'reftype'} =$reftype;3215$ref_item{'refid'} =$refid;3216}else{3217$ref_item{'reftype'} =$type;3218$ref_item{'refid'} =$id;3219}32203221if($typeeq"tag"||$typeeq"commit") {3222$ref_item{'epoch'} =$epoch;3223if($epoch) {3224$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3225}else{3226$ref_item{'age'} ="unknown";3227}3228}32293230push@tagslist, \%ref_item;3231}3232close$fd;32333234returnwantarray?@tagslist: \@tagslist;3235}32363237## ----------------------------------------------------------------------3238## filesystem-related functions32393240sub get_file_owner {3241my$path=shift;32423243my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3244my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3245if(!defined$gcos) {3246returnundef;3247}3248my$owner=$gcos;3249$owner=~s/[,;].*$//;3250return to_utf8($owner);3251}32523253# assume that file exists3254sub insert_file {3255my$filename=shift;32563257open my$fd,'<',$filename;3258print map{ to_utf8($_) } <$fd>;3259close$fd;3260}32613262## ......................................................................3263## mimetype related functions32643265sub mimetype_guess_file {3266my$filename=shift;3267my$mimemap=shift;3268-r $mimemaporreturnundef;32693270my%mimemap;3271open(my$mh,'<',$mimemap)orreturnundef;3272while(<$mh>) {3273next ifm/^#/;# skip comments3274my($mimetype,$exts) =split(/\t+/);3275if(defined$exts) {3276my@exts=split(/\s+/,$exts);3277foreachmy$ext(@exts) {3278$mimemap{$ext} =$mimetype;3279}3280}3281}3282close($mh);32833284$filename=~/\.([^.]*)$/;3285return$mimemap{$1};3286}32873288sub mimetype_guess {3289my$filename=shift;3290my$mime;3291$filename=~/\./orreturnundef;32923293if($mimetypes_file) {3294my$file=$mimetypes_file;3295if($file!~m!^/!) {# if it is relative path3296# it is relative to project3297$file="$projectroot/$project/$file";3298}3299$mime= mimetype_guess_file($filename,$file);3300}3301$mime||= mimetype_guess_file($filename,'/etc/mime.types');3302return$mime;3303}33043305sub blob_mimetype {3306my$fd=shift;3307my$filename=shift;33083309if($filename) {3310my$mime= mimetype_guess($filename);3311$mimeandreturn$mime;3312}33133314# just in case3315return$default_blob_plain_mimetypeunless$fd;33163317if(-T $fd) {3318return'text/plain';3319}elsif(!$filename) {3320return'application/octet-stream';3321}elsif($filename=~m/\.png$/i) {3322return'image/png';3323}elsif($filename=~m/\.gif$/i) {3324return'image/gif';3325}elsif($filename=~m/\.jpe?g$/i) {3326return'image/jpeg';3327}else{3328return'application/octet-stream';3329}3330}33313332sub blob_contenttype {3333my($fd,$file_name,$type) =@_;33343335$type||= blob_mimetype($fd,$file_name);3336if($typeeq'text/plain'&&defined$default_text_plain_charset) {3337$type.="; charset=$default_text_plain_charset";3338}33393340return$type;3341}33423343# guess file syntax for syntax highlighting; return undef if no highlighting3344# the name of syntax can (in the future) depend on syntax highlighter used3345sub guess_file_syntax {3346my($highlight,$mimetype,$file_name) =@_;3347returnundefunless($highlight&&defined$file_name);3348my$basename= basename($file_name,'.in');3349return$highlight_basename{$basename}3350ifexists$highlight_basename{$basename};33513352$basename=~/\.([^.]*)$/;3353my$ext=$1orreturnundef;3354return$highlight_ext{$ext}3355ifexists$highlight_ext{$ext};33563357returnundef;3358}33593360# run highlighter and return FD of its output,3361# or return original FD if no highlighting3362sub run_highlighter {3363my($fd,$highlight,$syntax) =@_;3364return$fdunless($highlight&&defined$syntax);33653366close$fd3367or die_error(404,"Reading blob failed");3368open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3369 quote_command($highlight_bin).3370" --xhtml --fragment --syntax$syntax|"3371or die_error(500,"Couldn't open file or run syntax highlighter");3372return$fd;3373}33743375## ======================================================================3376## functions printing HTML: header, footer, error page33773378sub get_page_title {3379my$title= to_utf8($site_name);33803381return$titleunless(defined$project);3382$title.=" - ". to_utf8($project);33833384return$titleunless(defined$action);3385$title.="/$action";# $action is US-ASCII (7bit ASCII)33863387return$titleunless(defined$file_name);3388$title.=" - ". esc_path($file_name);3389if($actioneq"tree"&&$file_name!~ m|/$|) {3390$title.="/";3391}33923393return$title;3394}33953396sub git_header_html {3397my$status=shift||"200 OK";3398my$expires=shift;3399my%opts=@_;34003401my$title= get_page_title();3402my$content_type;3403# require explicit support from the UA if we are to send the page as3404# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3405# we have to do this because MSIE sometimes globs '*/*', pretending to3406# support xhtml+xml but choking when it gets what it asked for.3407if(defined$cgi->http('HTTP_ACCEPT') &&3408$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3409$cgi->Accept('application/xhtml+xml') !=0) {3410$content_type='application/xhtml+xml';3411}else{3412$content_type='text/html';3413}3414print$cgi->header(-type=>$content_type, -charset =>'utf-8',3415-status=>$status, -expires =>$expires)3416unless($opts{'-no_http_header'});3417my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3418print<<EOF;3419<?xml version="1.0" encoding="utf-8"?>3420<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3421<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3422<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3423<!-- git core binaries version$git_version-->3424<head>3425<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3426<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3427<meta name="robots" content="index, nofollow"/>3428<title>$title</title>3429EOF3430# the stylesheet, favicon etc urls won't work correctly with path_info3431# unless we set the appropriate base URL3432if($ENV{'PATH_INFO'}) {3433print"<base href=\"".esc_url($base_url)."\"/>\n";3434}3435# print out each stylesheet that exist, providing backwards capability3436# for those people who defined $stylesheet in a config file3437if(defined$stylesheet) {3438print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3439}else{3440foreachmy$stylesheet(@stylesheets) {3441next unless$stylesheet;3442print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3443}3444}3445if(defined$project) {3446my%href_params= get_feed_info();3447if(!exists$href_params{'-title'}) {3448$href_params{'-title'} ='log';3449}34503451foreachmy$formatqw(RSS Atom){3452my$type=lc($format);3453my%link_attr= (3454'-rel'=>'alternate',3455'-title'=>"$project-$href_params{'-title'} -$formatfeed",3456'-type'=>"application/$type+xml"3457);34583459$href_params{'action'} =$type;3460$link_attr{'-href'} = href(%href_params);3461print"<link ".3462"rel=\"$link_attr{'-rel'}\"".3463"title=\"$link_attr{'-title'}\"".3464"href=\"$link_attr{'-href'}\"".3465"type=\"$link_attr{'-type'}\"".3466"/>\n";34673468$href_params{'extra_options'} ='--no-merges';3469$link_attr{'-href'} = href(%href_params);3470$link_attr{'-title'} .=' (no merges)';3471print"<link ".3472"rel=\"$link_attr{'-rel'}\"".3473"title=\"$link_attr{'-title'}\"".3474"href=\"$link_attr{'-href'}\"".3475"type=\"$link_attr{'-type'}\"".3476"/>\n";3477}34783479}else{3480printf('<link rel="alternate" title="%sprojects list" '.3481'href="%s" type="text/plain; charset=utf-8" />'."\n",3482$site_name, href(project=>undef, action=>"project_index"));3483printf('<link rel="alternate" title="%sprojects feeds" '.3484'href="%s" type="text/x-opml" />'."\n",3485$site_name, href(project=>undef, action=>"opml"));3486}3487if(defined$favicon) {3488printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3489}34903491print"</head>\n".3492"<body>\n";34933494if(defined$site_header&& -f $site_header) {3495 insert_file($site_header);3496}34973498print"<div class=\"page_header\">\n".3499$cgi->a({-href => esc_url($logo_url),3500-title =>$logo_label},3501qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3502print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3503if(defined$project) {3504print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3505if(defined$action) {3506print" /$action";3507}3508print"\n";3509}3510print"</div>\n";35113512my$have_search= gitweb_check_feature('search');3513if(defined$project&&$have_search) {3514if(!defined$searchtext) {3515$searchtext="";3516}3517my$search_hash;3518if(defined$hash_base) {3519$search_hash=$hash_base;3520}elsif(defined$hash) {3521$search_hash=$hash;3522}else{3523$search_hash="HEAD";3524}3525my$action=$my_uri;3526my$use_pathinfo= gitweb_check_feature('pathinfo');3527if($use_pathinfo) {3528$action.="/".esc_url($project);3529}3530print$cgi->startform(-method=>"get", -action =>$action) .3531"<div class=\"search\">\n".3532(!$use_pathinfo&&3533$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3534$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3535$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3536$cgi->popup_menu(-name =>'st', -default=>'commit',3537-values=> ['commit','grep','author','committer','pickaxe']) .3538$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3539" search:\n",3540$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3541"<span title=\"Extended regular expression\">".3542$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3543-checked =>$search_use_regexp) .3544"</span>".3545"</div>".3546$cgi->end_form() ."\n";3547}3548}35493550sub git_footer_html {3551my$feed_class='rss_logo';35523553print"<div class=\"page_footer\">\n";3554if(defined$project) {3555my$descr= git_get_project_description($project);3556if(defined$descr) {3557print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3558}35593560my%href_params= get_feed_info();3561if(!%href_params) {3562$feed_class.=' generic';3563}3564$href_params{'-title'} ||='log';35653566foreachmy$formatqw(RSS Atom){3567$href_params{'action'} =lc($format);3568print$cgi->a({-href => href(%href_params),3569-title =>"$href_params{'-title'}$formatfeed",3570-class=>$feed_class},$format)."\n";3571}35723573}else{3574print$cgi->a({-href => href(project=>undef, action=>"opml"),3575-class=>$feed_class},"OPML") ." ";3576print$cgi->a({-href => href(project=>undef, action=>"project_index"),3577-class=>$feed_class},"TXT") ."\n";3578}3579print"</div>\n";# class="page_footer"35803581if(defined$t0&& gitweb_check_feature('timed')) {3582print"<div id=\"generating_info\">\n";3583print'This page took '.3584'<span id="generating_time" class="time_span">'.3585 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3586' seconds </span>'.3587' and '.3588'<span id="generating_cmd">'.3589$number_of_git_cmds.3590'</span> git commands '.3591" to generate.\n";3592print"</div>\n";# class="page_footer"3593}35943595if(defined$site_footer&& -f $site_footer) {3596 insert_file($site_footer);3597}35983599print qq!<script type="text/javascript" src="$javascript"></script>\n!;3600if(defined$action&&3601$actioneq'blame_incremental') {3602print qq!<script type="text/javascript">\n!.3603 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3604 qq!"!. href() .qq!");\n!.3605 qq!</script>\n!;3606}elsif(gitweb_check_feature('javascript-actions')) {3607print qq!<script type="text/javascript">\n!.3608 qq!window.onload = fixLinks;\n!.3609 qq!</script>\n!;3610}36113612print"</body>\n".3613"</html>";3614}36153616# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3617# Example: die_error(404, 'Hash not found')3618# By convention, use the following status codes (as defined in RFC 2616):3619# 400: Invalid or missing CGI parameters, or3620# requested object exists but has wrong type.3621# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3622# this server or project.3623# 404: Requested object/revision/project doesn't exist.3624# 500: The server isn't configured properly, or3625# an internal error occurred (e.g. failed assertions caused by bugs), or3626# an unknown error occurred (e.g. the git binary died unexpectedly).3627# 503: The server is currently unavailable (because it is overloaded,3628# or down for maintenance). Generally, this is a temporary state.3629sub die_error {3630my$status=shift||500;3631my$error= esc_html(shift) ||"Internal Server Error";3632my$extra=shift;3633my%opts=@_;36343635my%http_responses= (3636400=>'400 Bad Request',3637403=>'403 Forbidden',3638404=>'404 Not Found',3639500=>'500 Internal Server Error',3640503=>'503 Service Unavailable',3641);3642 git_header_html($http_responses{$status},undef,%opts);3643print<<EOF;3644<div class="page_body">3645<br /><br />3646$status-$error3647<br />3648EOF3649if(defined$extra) {3650print"<hr />\n".3651"$extra\n";3652}3653print"</div>\n";36543655 git_footer_html();3656goto DONE_GITWEB3657unless($opts{'-error_handler'});3658}36593660## ----------------------------------------------------------------------3661## functions printing or outputting HTML: navigation36623663sub git_print_page_nav {3664my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3665$extra=''if!defined$extra;# pager or formats36663667my@navs=qw(summary shortlog log commit commitdiff tree);3668if($suppress) {3669@navs=grep{$_ne$suppress}@navs;3670}36713672my%arg=map{$_=> {action=>$_} }@navs;3673if(defined$head) {3674for(qw(commit commitdiff)) {3675$arg{$_}{'hash'} =$head;3676}3677if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3678for(qw(shortlog log)) {3679$arg{$_}{'hash'} =$head;3680}3681}3682}36833684$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3685$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;36863687my@actions= gitweb_get_feature('actions');3688my%repl= (3689'%'=>'%',3690'n'=>$project,# project name3691'f'=>$git_dir,# project path within filesystem3692'h'=>$treehead||'',# current hash ('h' parameter)3693'b'=>$treebase||'',# hash base ('hb' parameter)3694);3695while(@actions) {3696my($label,$link,$pos) =splice(@actions,0,3);3697# insert3698@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3699# munch munch3700$link=~s/%([%nfhb])/$repl{$1}/g;3701$arg{$label}{'_href'} =$link;3702}37033704print"<div class=\"page_nav\">\n".3705(join" | ",3706map{$_eq$current?3707$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3708}@navs);3709print"<br/>\n$extra<br/>\n".3710"</div>\n";3711}37123713sub format_paging_nav {3714my($action,$page,$has_next_link) =@_;3715my$paging_nav;371637173718if($page>0) {3719$paging_nav.=3720$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3721" ⋅ ".3722$cgi->a({-href => href(-replay=>1, page=>$page-1),3723-accesskey =>"p", -title =>"Alt-p"},"prev");3724}else{3725$paging_nav.="first ⋅ prev";3726}37273728if($has_next_link) {3729$paging_nav.=" ⋅ ".3730$cgi->a({-href => href(-replay=>1, page=>$page+1),3731-accesskey =>"n", -title =>"Alt-n"},"next");3732}else{3733$paging_nav.=" ⋅ next";3734}37353736return$paging_nav;3737}37383739## ......................................................................3740## functions printing or outputting HTML: div37413742sub git_print_header_div {3743my($action,$title,$hash,$hash_base) =@_;3744my%args= ();37453746$args{'action'} =$action;3747$args{'hash'} =$hashif$hash;3748$args{'hash_base'} =$hash_baseif$hash_base;37493750print"<div class=\"header\">\n".3751$cgi->a({-href => href(%args), -class=>"title"},3752$title?$title:$action) .3753"\n</div>\n";3754}37553756sub print_local_time {3757print format_local_time(@_);3758}37593760sub format_local_time {3761my$localtime='';3762my%date=@_;3763if($date{'hour_local'} <6) {3764$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3765$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3766}else{3767$localtime.=sprintf(" (%02d:%02d%s)",3768$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3769}37703771return$localtime;3772}37733774# Outputs the author name and date in long form3775sub git_print_authorship {3776my$co=shift;3777my%opts=@_;3778my$tag=$opts{-tag} ||'div';3779my$author=$co->{'author_name'};37803781my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3782print"<$tagclass=\"author_date\">".3783 format_search_author($author,"author", esc_html($author)) .3784" [$ad{'rfc2822'}";3785 print_local_time(%ad)if($opts{-localtime});3786print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3787."</$tag>\n";3788}37893790# Outputs table rows containing the full author or committer information,3791# in the format expected for 'commit' view (& similar).3792# Parameters are a commit hash reference, followed by the list of people3793# to output information for. If the list is empty it defaults to both3794# author and committer.3795sub git_print_authorship_rows {3796my$co=shift;3797# too bad we can't use @people = @_ || ('author', 'committer')3798my@people=@_;3799@people= ('author','committer')unless@people;3800foreachmy$who(@people) {3801my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3802print"<tr><td>$who</td><td>".3803 format_search_author($co->{"${who}_name"},$who,3804 esc_html($co->{"${who}_name"})) ." ".3805 format_search_author($co->{"${who}_email"},$who,3806 esc_html("<".$co->{"${who}_email"} .">")) .3807"</td><td rowspan=\"2\">".3808 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3809"</td></tr>\n".3810"<tr>".3811"<td></td><td>$wd{'rfc2822'}";3812 print_local_time(%wd);3813print"</td>".3814"</tr>\n";3815}3816}38173818sub git_print_page_path {3819my$name=shift;3820my$type=shift;3821my$hb=shift;382238233824print"<div class=\"page_path\">";3825print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3826-title =>'tree root'}, to_utf8("[$project]"));3827print" / ";3828if(defined$name) {3829my@dirname=split'/',$name;3830my$basename=pop@dirname;3831my$fullname='';38323833foreachmy$dir(@dirname) {3834$fullname.= ($fullname?'/':'') .$dir;3835print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3836 hash_base=>$hb),3837-title =>$fullname}, esc_path($dir));3838print" / ";3839}3840if(defined$type&&$typeeq'blob') {3841print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3842 hash_base=>$hb),3843-title =>$name}, esc_path($basename));3844}elsif(defined$type&&$typeeq'tree') {3845print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3846 hash_base=>$hb),3847-title =>$name}, esc_path($basename));3848print" / ";3849}else{3850print esc_path($basename);3851}3852}3853print"<br/></div>\n";3854}38553856sub git_print_log {3857my$log=shift;3858my%opts=@_;38593860if($opts{'-remove_title'}) {3861# remove title, i.e. first line of log3862shift@$log;3863}3864# remove leading empty lines3865while(defined$log->[0] &&$log->[0]eq"") {3866shift@$log;3867}38683869# print log3870my$signoff=0;3871my$empty=0;3872foreachmy$line(@$log) {3873if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3874$signoff=1;3875$empty=0;3876if(!$opts{'-remove_signoff'}) {3877print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3878next;3879}else{3880# remove signoff lines3881next;3882}3883}else{3884$signoff=0;3885}38863887# print only one empty line3888# do not print empty line after signoff3889if($lineeq"") {3890next if($empty||$signoff);3891$empty=1;3892}else{3893$empty=0;3894}38953896print format_log_line_html($line) ."<br/>\n";3897}38983899if($opts{'-final_empty_line'}) {3900# end with single empty line3901print"<br/>\n"unless$empty;3902}3903}39043905# return link target (what link points to)3906sub git_get_link_target {3907my$hash=shift;3908my$link_target;39093910# read link3911open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3912orreturn;3913{3914local$/=undef;3915$link_target= <$fd>;3916}3917close$fd3918orreturn;39193920return$link_target;3921}39223923# given link target, and the directory (basedir) the link is in,3924# return target of link relative to top directory (top tree);3925# return undef if it is not possible (including absolute links).3926sub normalize_link_target {3927my($link_target,$basedir) =@_;39283929# absolute symlinks (beginning with '/') cannot be normalized3930return if(substr($link_target,0,1)eq'/');39313932# normalize link target to path from top (root) tree (dir)3933my$path;3934if($basedir) {3935$path=$basedir.'/'.$link_target;3936}else{3937# we are in top (root) tree (dir)3938$path=$link_target;3939}39403941# remove //, /./, and /../3942my@path_parts;3943foreachmy$part(split('/',$path)) {3944# discard '.' and ''3945next if(!$part||$parteq'.');3946# handle '..'3947if($parteq'..') {3948if(@path_parts) {3949pop@path_parts;3950}else{3951# link leads outside repository (outside top dir)3952return;3953}3954}else{3955push@path_parts,$part;3956}3957}3958$path=join('/',@path_parts);39593960return$path;3961}39623963# print tree entry (row of git_tree), but without encompassing <tr> element3964sub git_print_tree_entry {3965my($t,$basedir,$hash_base,$have_blame) =@_;39663967my%base_key= ();3968$base_key{'hash_base'} =$hash_baseifdefined$hash_base;39693970# The format of a table row is: mode list link. Where mode is3971# the mode of the entry, list is the name of the entry, an href,3972# and link is the action links of the entry.39733974print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3975if(exists$t->{'size'}) {3976print"<td class=\"size\">$t->{'size'}</td>\n";3977}3978if($t->{'type'}eq"blob") {3979print"<td class=\"list\">".3980$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3981 file_name=>"$basedir$t->{'name'}",%base_key),3982-class=>"list"}, esc_path($t->{'name'}));3983if(S_ISLNK(oct$t->{'mode'})) {3984my$link_target= git_get_link_target($t->{'hash'});3985if($link_target) {3986my$norm_target= normalize_link_target($link_target,$basedir);3987if(defined$norm_target) {3988print" -> ".3989$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3990 file_name=>$norm_target),3991-title =>$norm_target}, esc_path($link_target));3992}else{3993print" -> ". esc_path($link_target);3994}3995}3996}3997print"</td>\n";3998print"<td class=\"link\">";3999print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4000 file_name=>"$basedir$t->{'name'}",%base_key)},4001"blob");4002if($have_blame) {4003print" | ".4004$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4005 file_name=>"$basedir$t->{'name'}",%base_key)},4006"blame");4007}4008if(defined$hash_base) {4009print" | ".4010$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4011 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4012"history");4013}4014print" | ".4015$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4016 file_name=>"$basedir$t->{'name'}")},4017"raw");4018print"</td>\n";40194020}elsif($t->{'type'}eq"tree") {4021print"<td class=\"list\">";4022print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4023 file_name=>"$basedir$t->{'name'}",4024%base_key)},4025 esc_path($t->{'name'}));4026print"</td>\n";4027print"<td class=\"link\">";4028print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4029 file_name=>"$basedir$t->{'name'}",4030%base_key)},4031"tree");4032if(defined$hash_base) {4033print" | ".4034$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4035 file_name=>"$basedir$t->{'name'}")},4036"history");4037}4038print"</td>\n";4039}else{4040# unknown object: we can only present history for it4041# (this includes 'commit' object, i.e. submodule support)4042print"<td class=\"list\">".4043 esc_path($t->{'name'}) .4044"</td>\n";4045print"<td class=\"link\">";4046if(defined$hash_base) {4047print$cgi->a({-href => href(action=>"history",4048 hash_base=>$hash_base,4049 file_name=>"$basedir$t->{'name'}")},4050"history");4051}4052print"</td>\n";4053}4054}40554056## ......................................................................4057## functions printing large fragments of HTML40584059# get pre-image filenames for merge (combined) diff4060sub fill_from_file_info {4061my($diff,@parents) =@_;40624063$diff->{'from_file'} = [ ];4064$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4065for(my$i=0;$i<$diff->{'nparents'};$i++) {4066if($diff->{'status'}[$i]eq'R'||4067$diff->{'status'}[$i]eq'C') {4068$diff->{'from_file'}[$i] =4069 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4070}4071}40724073return$diff;4074}40754076# is current raw difftree line of file deletion4077sub is_deleted {4078my$diffinfo=shift;40794080return$diffinfo->{'to_id'}eq('0' x 40);4081}40824083# does patch correspond to [previous] difftree raw line4084# $diffinfo - hashref of parsed raw diff format4085# $patchinfo - hashref of parsed patch diff format4086# (the same keys as in $diffinfo)4087sub is_patch_split {4088my($diffinfo,$patchinfo) =@_;40894090returndefined$diffinfo&&defined$patchinfo4091&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4092}409340944095sub git_difftree_body {4096my($difftree,$hash,@parents) =@_;4097my($parent) =$parents[0];4098my$have_blame= gitweb_check_feature('blame');4099print"<div class=\"list_head\">\n";4100if($#{$difftree} >10) {4101print(($#{$difftree} +1) ." files changed:\n");4102}4103print"</div>\n";41044105print"<table class=\"".4106(@parents>1?"combined ":"") .4107"diff_tree\">\n";41084109# header only for combined diff in 'commitdiff' view4110my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4111if($has_header) {4112# table header4113print"<thead><tr>\n".4114"<th></th><th></th>\n";# filename, patchN link4115for(my$i=0;$i<@parents;$i++) {4116my$par=$parents[$i];4117print"<th>".4118$cgi->a({-href => href(action=>"commitdiff",4119 hash=>$hash, hash_parent=>$par),4120-title =>'commitdiff to parent number '.4121($i+1) .': '.substr($par,0,7)},4122$i+1) .4123" </th>\n";4124}4125print"</tr></thead>\n<tbody>\n";4126}41274128my$alternate=1;4129my$patchno=0;4130foreachmy$line(@{$difftree}) {4131my$diff= parsed_difftree_line($line);41324133if($alternate) {4134print"<tr class=\"dark\">\n";4135}else{4136print"<tr class=\"light\">\n";4137}4138$alternate^=1;41394140if(exists$diff->{'nparents'}) {# combined diff41414142 fill_from_file_info($diff,@parents)4143unlessexists$diff->{'from_file'};41444145if(!is_deleted($diff)) {4146# file exists in the result (child) commit4147print"<td>".4148$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4149 file_name=>$diff->{'to_file'},4150 hash_base=>$hash),4151-class=>"list"}, esc_path($diff->{'to_file'})) .4152"</td>\n";4153}else{4154print"<td>".4155 esc_path($diff->{'to_file'}) .4156"</td>\n";4157}41584159if($actioneq'commitdiff') {4160# link to patch4161$patchno++;4162print"<td class=\"link\">".4163$cgi->a({-href =>"#patch$patchno"},"patch") .4164" | ".4165"</td>\n";4166}41674168my$has_history=0;4169my$not_deleted=0;4170for(my$i=0;$i<$diff->{'nparents'};$i++) {4171my$hash_parent=$parents[$i];4172my$from_hash=$diff->{'from_id'}[$i];4173my$from_path=$diff->{'from_file'}[$i];4174my$status=$diff->{'status'}[$i];41754176$has_history||= ($statusne'A');4177$not_deleted||= ($statusne'D');41784179if($statuseq'A') {4180print"<td class=\"link\"align=\"right\"> | </td>\n";4181}elsif($statuseq'D') {4182print"<td class=\"link\">".4183$cgi->a({-href => href(action=>"blob",4184 hash_base=>$hash,4185 hash=>$from_hash,4186 file_name=>$from_path)},4187"blob". ($i+1)) .4188" | </td>\n";4189}else{4190if($diff->{'to_id'}eq$from_hash) {4191print"<td class=\"link nochange\">";4192}else{4193print"<td class=\"link\">";4194}4195print$cgi->a({-href => href(action=>"blobdiff",4196 hash=>$diff->{'to_id'},4197 hash_parent=>$from_hash,4198 hash_base=>$hash,4199 hash_parent_base=>$hash_parent,4200 file_name=>$diff->{'to_file'},4201 file_parent=>$from_path)},4202"diff". ($i+1)) .4203" | </td>\n";4204}4205}42064207print"<td class=\"link\">";4208if($not_deleted) {4209print$cgi->a({-href => href(action=>"blob",4210 hash=>$diff->{'to_id'},4211 file_name=>$diff->{'to_file'},4212 hash_base=>$hash)},4213"blob");4214print" | "if($has_history);4215}4216if($has_history) {4217print$cgi->a({-href => href(action=>"history",4218 file_name=>$diff->{'to_file'},4219 hash_base=>$hash)},4220"history");4221}4222print"</td>\n";42234224print"</tr>\n";4225next;# instead of 'else' clause, to avoid extra indent4226}4227# else ordinary diff42284229my($to_mode_oct,$to_mode_str,$to_file_type);4230my($from_mode_oct,$from_mode_str,$from_file_type);4231if($diff->{'to_mode'}ne('0' x 6)) {4232$to_mode_oct=oct$diff->{'to_mode'};4233if(S_ISREG($to_mode_oct)) {# only for regular file4234$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4235}4236$to_file_type= file_type($diff->{'to_mode'});4237}4238if($diff->{'from_mode'}ne('0' x 6)) {4239$from_mode_oct=oct$diff->{'from_mode'};4240if(S_ISREG($to_mode_oct)) {# only for regular file4241$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4242}4243$from_file_type= file_type($diff->{'from_mode'});4244}42454246if($diff->{'status'}eq"A") {# created4247my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4248$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4249$mode_chng.="]</span>";4250print"<td>";4251print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4252 hash_base=>$hash, file_name=>$diff->{'file'}),4253-class=>"list"}, esc_path($diff->{'file'}));4254print"</td>\n";4255print"<td>$mode_chng</td>\n";4256print"<td class=\"link\">";4257if($actioneq'commitdiff') {4258# link to patch4259$patchno++;4260print$cgi->a({-href =>"#patch$patchno"},"patch");4261print" | ";4262}4263print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4264 hash_base=>$hash, file_name=>$diff->{'file'})},4265"blob");4266print"</td>\n";42674268}elsif($diff->{'status'}eq"D") {# deleted4269my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4270print"<td>";4271print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4272 hash_base=>$parent, file_name=>$diff->{'file'}),4273-class=>"list"}, esc_path($diff->{'file'}));4274print"</td>\n";4275print"<td>$mode_chng</td>\n";4276print"<td class=\"link\">";4277if($actioneq'commitdiff') {4278# link to patch4279$patchno++;4280print$cgi->a({-href =>"#patch$patchno"},"patch");4281print" | ";4282}4283print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4284 hash_base=>$parent, file_name=>$diff->{'file'})},4285"blob") ." | ";4286if($have_blame) {4287print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4288 file_name=>$diff->{'file'})},4289"blame") ." | ";4290}4291print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4292 file_name=>$diff->{'file'})},4293"history");4294print"</td>\n";42954296}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4297my$mode_chnge="";4298if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4299$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4300if($from_file_typene$to_file_type) {4301$mode_chnge.=" from$from_file_typeto$to_file_type";4302}4303if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4304if($from_mode_str&&$to_mode_str) {4305$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4306}elsif($to_mode_str) {4307$mode_chnge.=" mode:$to_mode_str";4308}4309}4310$mode_chnge.="]</span>\n";4311}4312print"<td>";4313print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4314 hash_base=>$hash, file_name=>$diff->{'file'}),4315-class=>"list"}, esc_path($diff->{'file'}));4316print"</td>\n";4317print"<td>$mode_chnge</td>\n";4318print"<td class=\"link\">";4319if($actioneq'commitdiff') {4320# link to patch4321$patchno++;4322print$cgi->a({-href =>"#patch$patchno"},"patch") .4323" | ";4324}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4325# "commit" view and modified file (not onlu mode changed)4326print$cgi->a({-href => href(action=>"blobdiff",4327 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4328 hash_base=>$hash, hash_parent_base=>$parent,4329 file_name=>$diff->{'file'})},4330"diff") .4331" | ";4332}4333print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4334 hash_base=>$hash, file_name=>$diff->{'file'})},4335"blob") ." | ";4336if($have_blame) {4337print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4338 file_name=>$diff->{'file'})},4339"blame") ." | ";4340}4341print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4342 file_name=>$diff->{'file'})},4343"history");4344print"</td>\n";43454346}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4347my%status_name= ('R'=>'moved','C'=>'copied');4348my$nstatus=$status_name{$diff->{'status'}};4349my$mode_chng="";4350if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4351# mode also for directories, so we cannot use $to_mode_str4352$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4353}4354print"<td>".4355$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4356 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4357-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4358"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4359$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4360 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4361-class=>"list"}, esc_path($diff->{'from_file'})) .4362" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4363"<td class=\"link\">";4364if($actioneq'commitdiff') {4365# link to patch4366$patchno++;4367print$cgi->a({-href =>"#patch$patchno"},"patch") .4368" | ";4369}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4370# "commit" view and modified file (not only pure rename or copy)4371print$cgi->a({-href => href(action=>"blobdiff",4372 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4373 hash_base=>$hash, hash_parent_base=>$parent,4374 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4375"diff") .4376" | ";4377}4378print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4379 hash_base=>$parent, file_name=>$diff->{'to_file'})},4380"blob") ." | ";4381if($have_blame) {4382print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4383 file_name=>$diff->{'to_file'})},4384"blame") ." | ";4385}4386print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4387 file_name=>$diff->{'to_file'})},4388"history");4389print"</td>\n";43904391}# we should not encounter Unmerged (U) or Unknown (X) status4392print"</tr>\n";4393}4394print"</tbody>"if$has_header;4395print"</table>\n";4396}43974398sub git_patchset_body {4399my($fd,$difftree,$hash,@hash_parents) =@_;4400my($hash_parent) =$hash_parents[0];44014402my$is_combined= (@hash_parents>1);4403my$patch_idx=0;4404my$patch_number=0;4405my$patch_line;4406my$diffinfo;4407my$to_name;4408my(%from,%to);44094410print"<div class=\"patchset\">\n";44114412# skip to first patch4413while($patch_line= <$fd>) {4414chomp$patch_line;44154416last if($patch_line=~m/^diff /);4417}44184419 PATCH:4420while($patch_line) {44214422# parse "git diff" header line4423if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4424# $1 is from_name, which we do not use4425$to_name= unquote($2);4426$to_name=~s!^b/!!;4427}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4428# $1 is 'cc' or 'combined', which we do not use4429$to_name= unquote($2);4430}else{4431$to_name=undef;4432}44334434# check if current patch belong to current raw line4435# and parse raw git-diff line if needed4436if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4437# this is continuation of a split patch4438print"<div class=\"patch cont\">\n";4439}else{4440# advance raw git-diff output if needed4441$patch_idx++ifdefined$diffinfo;44424443# read and prepare patch information4444$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);44454446# compact combined diff output can have some patches skipped4447# find which patch (using pathname of result) we are at now;4448if($is_combined) {4449while($to_namene$diffinfo->{'to_file'}) {4450print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4451 format_diff_cc_simplified($diffinfo,@hash_parents) .4452"</div>\n";# class="patch"44534454$patch_idx++;4455$patch_number++;44564457last if$patch_idx>$#$difftree;4458$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4459}4460}44614462# modifies %from, %to hashes4463 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);44644465# this is first patch for raw difftree line with $patch_idx index4466# we index @$difftree array from 0, but number patches from 14467print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4468}44694470# git diff header4471#assert($patch_line =~ m/^diff /) if DEBUG;4472#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4473$patch_number++;4474# print "git diff" header4475print format_git_diff_header_line($patch_line,$diffinfo,4476 \%from, \%to);44774478# print extended diff header4479print"<div class=\"diff extended_header\">\n";4480 EXTENDED_HEADER:4481while($patch_line= <$fd>) {4482chomp$patch_line;44834484last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);44854486print format_extended_diff_header_line($patch_line,$diffinfo,4487 \%from, \%to);4488}4489print"</div>\n";# class="diff extended_header"44904491# from-file/to-file diff header4492if(!$patch_line) {4493print"</div>\n";# class="patch"4494last PATCH;4495}4496next PATCH if($patch_line=~m/^diff /);4497#assert($patch_line =~ m/^---/) if DEBUG;44984499my$last_patch_line=$patch_line;4500$patch_line= <$fd>;4501chomp$patch_line;4502#assert($patch_line =~ m/^\+\+\+/) if DEBUG;45034504print format_diff_from_to_header($last_patch_line,$patch_line,4505$diffinfo, \%from, \%to,4506@hash_parents);45074508# the patch itself4509 LINE:4510while($patch_line= <$fd>) {4511chomp$patch_line;45124513next PATCH if($patch_line=~m/^diff /);45144515print format_diff_line($patch_line, \%from, \%to);4516}45174518}continue{4519print"</div>\n";# class="patch"4520}45214522# for compact combined (--cc) format, with chunk and patch simplification4523# the patchset might be empty, but there might be unprocessed raw lines4524for(++$patch_idxif$patch_number>0;4525$patch_idx<@$difftree;4526++$patch_idx) {4527# read and prepare patch information4528$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);45294530# generate anchor for "patch" links in difftree / whatchanged part4531print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4532 format_diff_cc_simplified($diffinfo,@hash_parents) .4533"</div>\n";# class="patch"45344535$patch_number++;4536}45374538if($patch_number==0) {4539if(@hash_parents>1) {4540print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4541}else{4542print"<div class=\"diff nodifferences\">No differences found</div>\n";4543}4544}45454546print"</div>\n";# class="patchset"4547}45484549# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45504551# fills project list info (age, description, owner, forks) for each4552# project in the list, removing invalid projects from returned list4553# NOTE: modifies $projlist, but does not remove entries from it4554sub fill_project_list_info {4555my($projlist,$check_forks) =@_;4556my@projects;45574558my$show_ctags= gitweb_check_feature('ctags');4559 PROJECT:4560foreachmy$pr(@$projlist) {4561my(@activity) = git_get_last_activity($pr->{'path'});4562unless(@activity) {4563next PROJECT;4564}4565($pr->{'age'},$pr->{'age_string'}) =@activity;4566if(!defined$pr->{'descr'}) {4567my$descr= git_get_project_description($pr->{'path'}) ||"";4568$descr= to_utf8($descr);4569$pr->{'descr_long'} =$descr;4570$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4571}4572if(!defined$pr->{'owner'}) {4573$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4574}4575if($check_forks) {4576my$pname=$pr->{'path'};4577if(($pname=~s/\.git$//) &&4578($pname!~/\/$/) &&4579(-d "$projectroot/$pname")) {4580$pr->{'forks'} ="-d$projectroot/$pname";4581}else{4582$pr->{'forks'} =0;4583}4584}4585$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4586push@projects,$pr;4587}45884589return@projects;4590}45914592# print 'sort by' <th> element, generating 'sort by $name' replay link4593# if that order is not selected4594sub print_sort_th {4595print format_sort_th(@_);4596}45974598sub format_sort_th {4599my($name,$order,$header) =@_;4600my$sort_th="";4601$header||=ucfirst($name);46024603if($ordereq$name) {4604$sort_th.="<th>$header</th>\n";4605}else{4606$sort_th.="<th>".4607$cgi->a({-href => href(-replay=>1, order=>$name),4608-class=>"header"},$header) .4609"</th>\n";4610}46114612return$sort_th;4613}46144615sub git_project_list_body {4616# actually uses global variable $project4617my($projlist,$order,$from,$to,$extra,$no_header) =@_;46184619my$check_forks= gitweb_check_feature('forks');4620my@projects= fill_project_list_info($projlist,$check_forks);46214622$order||=$default_projects_order;4623$from=0unlessdefined$from;4624$to=$#projectsif(!defined$to||$#projects<$to);46254626my%order_info= (4627 project => { key =>'path', type =>'str'},4628 descr => { key =>'descr_long', type =>'str'},4629 owner => { key =>'owner', type =>'str'},4630 age => { key =>'age', type =>'num'}4631);4632my$oi=$order_info{$order};4633if($oi->{'type'}eq'str') {4634@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4635}else{4636@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4637}46384639my$show_ctags= gitweb_check_feature('ctags');4640if($show_ctags) {4641my%ctags;4642foreachmy$p(@projects) {4643foreachmy$ct(keys%{$p->{'ctags'}}) {4644$ctags{$ct} +=$p->{'ctags'}->{$ct};4645}4646}4647my$cloud= git_populate_project_tagcloud(\%ctags);4648print git_show_project_tagcloud($cloud,64);4649}46504651print"<table class=\"project_list\">\n";4652unless($no_header) {4653print"<tr>\n";4654if($check_forks) {4655print"<th></th>\n";4656}4657 print_sort_th('project',$order,'Project');4658 print_sort_th('descr',$order,'Description');4659 print_sort_th('owner',$order,'Owner');4660 print_sort_th('age',$order,'Last Change');4661print"<th></th>\n".# for links4662"</tr>\n";4663}4664my$alternate=1;4665my$tagfilter=$cgi->param('by_tag');4666for(my$i=$from;$i<=$to;$i++) {4667my$pr=$projects[$i];46684669next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4670next if$searchtextand not$pr->{'path'} =~/$searchtext/4671and not$pr->{'descr_long'} =~/$searchtext/;4672# Weed out forks or non-matching entries of search4673if($check_forks) {4674my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4675$forkbase="^$forkbase"if$forkbase;4676next ifnot$searchtextand not$tagfilterand$show_ctags4677and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4678}46794680if($alternate) {4681print"<tr class=\"dark\">\n";4682}else{4683print"<tr class=\"light\">\n";4684}4685$alternate^=1;4686if($check_forks) {4687print"<td>";4688if($pr->{'forks'}) {4689print"<!--$pr->{'forks'} -->\n";4690print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4691}4692print"</td>\n";4693}4694print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4695-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4696"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4697-class=>"list", -title =>$pr->{'descr_long'}},4698 esc_html($pr->{'descr'})) ."</td>\n".4699"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4700print"<td class=\"". age_class($pr->{'age'}) ."\">".4701(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4702"<td class=\"link\">".4703$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4704$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4705$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4706$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4707($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4708"</td>\n".4709"</tr>\n";4710}4711if(defined$extra) {4712print"<tr>\n";4713if($check_forks) {4714print"<td></td>\n";4715}4716print"<td colspan=\"5\">$extra</td>\n".4717"</tr>\n";4718}4719print"</table>\n";4720}47214722sub git_log_body {4723# uses global variable $project4724my($commitlist,$from,$to,$refs,$extra) =@_;47254726$from=0unlessdefined$from;4727$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47284729for(my$i=0;$i<=$to;$i++) {4730my%co= %{$commitlist->[$i]};4731next if!%co;4732my$commit=$co{'id'};4733my$ref= format_ref_marker($refs,$commit);4734my%ad= parse_date($co{'author_epoch'});4735 git_print_header_div('commit',4736"<span class=\"age\">$co{'age_string'}</span>".4737 esc_html($co{'title'}) .$ref,4738$commit);4739print"<div class=\"title_text\">\n".4740"<div class=\"log_link\">\n".4741$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4742" | ".4743$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4744" | ".4745$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4746"<br/>\n".4747"</div>\n";4748 git_print_authorship(\%co, -tag =>'span');4749print"<br/>\n</div>\n";47504751print"<div class=\"log_body\">\n";4752 git_print_log($co{'comment'}, -final_empty_line=>1);4753print"</div>\n";4754}4755if($extra) {4756print"<div class=\"page_nav\">\n";4757print"$extra\n";4758print"</div>\n";4759}4760}47614762sub git_shortlog_body {4763# uses global variable $project4764my($commitlist,$from,$to,$refs,$extra) =@_;47654766$from=0unlessdefined$from;4767$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47684769print"<table class=\"shortlog\">\n";4770my$alternate=1;4771for(my$i=$from;$i<=$to;$i++) {4772my%co= %{$commitlist->[$i]};4773my$commit=$co{'id'};4774my$ref= format_ref_marker($refs,$commit);4775if($alternate) {4776print"<tr class=\"dark\">\n";4777}else{4778print"<tr class=\"light\">\n";4779}4780$alternate^=1;4781# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4782print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4783 format_author_html('td', \%co,10) ."<td>";4784print format_subject_html($co{'title'},$co{'title_short'},4785 href(action=>"commit", hash=>$commit),$ref);4786print"</td>\n".4787"<td class=\"link\">".4788$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4789$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4790$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4791my$snapshot_links= format_snapshot_links($commit);4792if(defined$snapshot_links) {4793print" | ".$snapshot_links;4794}4795print"</td>\n".4796"</tr>\n";4797}4798if(defined$extra) {4799print"<tr>\n".4800"<td colspan=\"4\">$extra</td>\n".4801"</tr>\n";4802}4803print"</table>\n";4804}48054806sub git_history_body {4807# Warning: assumes constant type (blob or tree) during history4808my($commitlist,$from,$to,$refs,$extra,4809$file_name,$file_hash,$ftype) =@_;48104811$from=0unlessdefined$from;4812$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});48134814print"<table class=\"history\">\n";4815my$alternate=1;4816for(my$i=$from;$i<=$to;$i++) {4817my%co= %{$commitlist->[$i]};4818if(!%co) {4819next;4820}4821my$commit=$co{'id'};48224823my$ref= format_ref_marker($refs,$commit);48244825if($alternate) {4826print"<tr class=\"dark\">\n";4827}else{4828print"<tr class=\"light\">\n";4829}4830$alternate^=1;4831print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4832# shortlog: format_author_html('td', \%co, 10)4833 format_author_html('td', \%co,15,3) ."<td>";4834# originally git_history used chop_str($co{'title'}, 50)4835print format_subject_html($co{'title'},$co{'title_short'},4836 href(action=>"commit", hash=>$commit),$ref);4837print"</td>\n".4838"<td class=\"link\">".4839$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4840$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");48414842if($ftypeeq'blob') {4843my$blob_current=$file_hash;4844my$blob_parent= git_get_hash_by_path($commit,$file_name);4845if(defined$blob_current&&defined$blob_parent&&4846$blob_currentne$blob_parent) {4847print" | ".4848$cgi->a({-href => href(action=>"blobdiff",4849 hash=>$blob_current, hash_parent=>$blob_parent,4850 hash_base=>$hash_base, hash_parent_base=>$commit,4851 file_name=>$file_name)},4852"diff to current");4853}4854}4855print"</td>\n".4856"</tr>\n";4857}4858if(defined$extra) {4859print"<tr>\n".4860"<td colspan=\"4\">$extra</td>\n".4861"</tr>\n";4862}4863print"</table>\n";4864}48654866sub git_tags_body {4867# uses global variable $project4868my($taglist,$from,$to,$extra) =@_;4869$from=0unlessdefined$from;4870$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);48714872print"<table class=\"tags\">\n";4873my$alternate=1;4874for(my$i=$from;$i<=$to;$i++) {4875my$entry=$taglist->[$i];4876my%tag=%$entry;4877my$comment=$tag{'subject'};4878my$comment_short;4879if(defined$comment) {4880$comment_short= chop_str($comment,30,5);4881}4882if($alternate) {4883print"<tr class=\"dark\">\n";4884}else{4885print"<tr class=\"light\">\n";4886}4887$alternate^=1;4888if(defined$tag{'age'}) {4889print"<td><i>$tag{'age'}</i></td>\n";4890}else{4891print"<td></td>\n";4892}4893print"<td>".4894$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4895-class=>"list name"}, esc_html($tag{'name'})) .4896"</td>\n".4897"<td>";4898if(defined$comment) {4899print format_subject_html($comment,$comment_short,4900 href(action=>"tag", hash=>$tag{'id'}));4901}4902print"</td>\n".4903"<td class=\"selflink\">";4904if($tag{'type'}eq"tag") {4905print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4906}else{4907print" ";4908}4909print"</td>\n".4910"<td class=\"link\">"." | ".4911$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4912if($tag{'reftype'}eq"commit") {4913print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4914" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4915}elsif($tag{'reftype'}eq"blob") {4916print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4917}4918print"</td>\n".4919"</tr>";4920}4921if(defined$extra) {4922print"<tr>\n".4923"<td colspan=\"5\">$extra</td>\n".4924"</tr>\n";4925}4926print"</table>\n";4927}49284929sub git_heads_body {4930# uses global variable $project4931my($headlist,$head,$from,$to,$extra) =@_;4932$from=0unlessdefined$from;4933$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);49344935print"<table class=\"heads\">\n";4936my$alternate=1;4937for(my$i=$from;$i<=$to;$i++) {4938my$entry=$headlist->[$i];4939my%ref=%$entry;4940my$curr=$ref{'id'}eq$head;4941if($alternate) {4942print"<tr class=\"dark\">\n";4943}else{4944print"<tr class=\"light\">\n";4945}4946$alternate^=1;4947print"<td><i>$ref{'age'}</i></td>\n".4948($curr?"<td class=\"current_head\">":"<td>") .4949$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4950-class=>"list name"},esc_html($ref{'name'})) .4951"</td>\n".4952"<td class=\"link\">".4953$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4954$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4955$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4956"</td>\n".4957"</tr>";4958}4959if(defined$extra) {4960print"<tr>\n".4961"<td colspan=\"3\">$extra</td>\n".4962"</tr>\n";4963}4964print"</table>\n";4965}49664967sub git_search_grep_body {4968my($commitlist,$from,$to,$extra) =@_;4969$from=0unlessdefined$from;4970$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);49714972print"<table class=\"commit_search\">\n";4973my$alternate=1;4974for(my$i=$from;$i<=$to;$i++) {4975my%co= %{$commitlist->[$i]};4976if(!%co) {4977next;4978}4979my$commit=$co{'id'};4980if($alternate) {4981print"<tr class=\"dark\">\n";4982}else{4983print"<tr class=\"light\">\n";4984}4985$alternate^=1;4986print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4987 format_author_html('td', \%co,15,5) .4988"<td>".4989$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4990-class=>"list subject"},4991 chop_and_escape_str($co{'title'},50) ."<br/>");4992my$comment=$co{'comment'};4993foreachmy$line(@$comment) {4994if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4995my($lead,$match,$trail) = ($1,$2,$3);4996$match= chop_str($match,70,5,'center');4997my$contextlen=int((80-length($match))/2);4998$contextlen=30if($contextlen>30);4999$lead= chop_str($lead,$contextlen,10,'left');5000$trail= chop_str($trail,$contextlen,10,'right');50015002$lead= esc_html($lead);5003$match= esc_html($match);5004$trail= esc_html($trail);50055006print"$lead<span class=\"match\">$match</span>$trail<br />";5007}5008}5009print"</td>\n".5010"<td class=\"link\">".5011$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5012" | ".5013$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .5014" | ".5015$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5016print"</td>\n".5017"</tr>\n";5018}5019if(defined$extra) {5020print"<tr>\n".5021"<td colspan=\"3\">$extra</td>\n".5022"</tr>\n";5023}5024print"</table>\n";5025}50265027## ======================================================================5028## ======================================================================5029## actions50305031sub git_project_list {5032my$order=$input_params{'order'};5033if(defined$order&&$order!~m/none|project|descr|owner|age/) {5034 die_error(400,"Unknown order parameter");5035}50365037my@list= git_get_projects_list();5038if(!@list) {5039 die_error(404,"No projects found");5040}50415042 git_header_html();5043if(defined$home_text&& -f $home_text) {5044print"<div class=\"index_include\">\n";5045 insert_file($home_text);5046print"</div>\n";5047}5048print$cgi->startform(-method=>"get") .5049"<p class=\"projsearch\">Search:\n".5050$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".5051"</p>".5052$cgi->end_form() ."\n";5053 git_project_list_body(\@list,$order);5054 git_footer_html();5055}50565057sub git_forks {5058my$order=$input_params{'order'};5059if(defined$order&&$order!~m/none|project|descr|owner|age/) {5060 die_error(400,"Unknown order parameter");5061}50625063my@list= git_get_projects_list($project);5064if(!@list) {5065 die_error(404,"No forks found");5066}50675068 git_header_html();5069 git_print_page_nav('','');5070 git_print_header_div('summary',"$projectforks");5071 git_project_list_body(\@list,$order);5072 git_footer_html();5073}50745075sub git_project_index {5076my@projects= git_get_projects_list($project);50775078print$cgi->header(5079-type =>'text/plain',5080-charset =>'utf-8',5081-content_disposition =>'inline; filename="index.aux"');50825083foreachmy$pr(@projects) {5084if(!exists$pr->{'owner'}) {5085$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");5086}50875088my($path,$owner) = ($pr->{'path'},$pr->{'owner'});5089# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '5090$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5091$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5092$path=~s/ /\+/g;5093$owner=~s/ /\+/g;50945095print"$path$owner\n";5096}5097}50985099sub git_summary {5100my$descr= git_get_project_description($project) ||"none";5101my%co= parse_commit("HEAD");5102my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();5103my$head=$co{'id'};51045105my$owner= git_get_project_owner($project);51065107my$refs= git_get_references();5108# These get_*_list functions return one more to allow us to see if5109# there are more ...5110my@taglist= git_get_tags_list(16);5111my@headlist= git_get_heads_list(16);5112my@forklist;5113my$check_forks= gitweb_check_feature('forks');51145115if($check_forks) {5116@forklist= git_get_projects_list($project);5117}51185119 git_header_html();5120 git_print_page_nav('summary','',$head);51215122print"<div class=\"title\"> </div>\n";5123print"<table class=\"projects_list\">\n".5124"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".5125"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";5126if(defined$cd{'rfc2822'}) {5127print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";5128}51295130# use per project git URL list in $projectroot/$project/cloneurl5131# or make project git URL from git base URL and project name5132my$url_tag="URL";5133my@url_list= git_get_project_url_list($project);5134@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;5135foreachmy$git_url(@url_list) {5136next unless$git_url;5137print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";5138$url_tag="";5139}51405141# Tag cloud5142my$show_ctags= gitweb_check_feature('ctags');5143if($show_ctags) {5144my$ctags= git_get_project_ctags($project);5145my$cloud= git_populate_project_tagcloud($ctags);5146print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";5147print"</td>\n<td>"unless%$ctags;5148print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";5149print"</td>\n<td>"if%$ctags;5150print git_show_project_tagcloud($cloud,48);5151print"</td></tr>";5152}51535154print"</table>\n";51555156# If XSS prevention is on, we don't include README.html.5157# TODO: Allow a readme in some safe format.5158if(!$prevent_xss&& -s "$projectroot/$project/README.html") {5159print"<div class=\"title\">readme</div>\n".5160"<div class=\"readme\">\n";5161 insert_file("$projectroot/$project/README.html");5162print"\n</div>\n";# class="readme"5163}51645165# we need to request one more than 16 (0..15) to check if5166# those 16 are all5167my@commitlist=$head? parse_commits($head,17) : ();5168if(@commitlist) {5169 git_print_header_div('shortlog');5170 git_shortlog_body(\@commitlist,0,15,$refs,5171$#commitlist<=15?undef:5172$cgi->a({-href => href(action=>"shortlog")},"..."));5173}51745175if(@taglist) {5176 git_print_header_div('tags');5177 git_tags_body(\@taglist,0,15,5178$#taglist<=15?undef:5179$cgi->a({-href => href(action=>"tags")},"..."));5180}51815182if(@headlist) {5183 git_print_header_div('heads');5184 git_heads_body(\@headlist,$head,0,15,5185$#headlist<=15?undef:5186$cgi->a({-href => href(action=>"heads")},"..."));5187}51885189if(@forklist) {5190 git_print_header_div('forks');5191 git_project_list_body(\@forklist,'age',0,15,5192$#forklist<=15?undef:5193$cgi->a({-href => href(action=>"forks")},"..."),5194'no_header');5195}51965197 git_footer_html();5198}51995200sub git_tag {5201my%tag= parse_tag($hash);52025203if(!%tag) {5204 die_error(404,"Unknown tag object");5205}52065207my$head= git_get_head_hash($project);5208 git_header_html();5209 git_print_page_nav('','',$head,undef,$head);5210 git_print_header_div('commit', esc_html($tag{'name'}),$hash);5211print"<div class=\"title_text\">\n".5212"<table class=\"object_header\">\n".5213"<tr>\n".5214"<td>object</td>\n".5215"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5216$tag{'object'}) ."</td>\n".5217"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5218$tag{'type'}) ."</td>\n".5219"</tr>\n";5220if(defined($tag{'author'})) {5221 git_print_authorship_rows(\%tag,'author');5222}5223print"</table>\n\n".5224"</div>\n";5225print"<div class=\"page_body\">";5226my$comment=$tag{'comment'};5227foreachmy$line(@$comment) {5228chomp$line;5229print esc_html($line, -nbsp=>1) ."<br/>\n";5230}5231print"</div>\n";5232 git_footer_html();5233}52345235sub git_blame_common {5236my$format=shift||'porcelain';5237if($formateq'porcelain'&&$cgi->param('js')) {5238$format='incremental';5239$action='blame_incremental';# for page title etc5240}52415242# permissions5243 gitweb_check_feature('blame')5244or die_error(403,"Blame view not allowed");52455246# error checking5247 die_error(400,"No file name given")unless$file_name;5248$hash_base||= git_get_head_hash($project);5249 die_error(404,"Couldn't find base commit")unless$hash_base;5250my%co= parse_commit($hash_base)5251or die_error(404,"Commit not found");5252my$ftype="blob";5253if(!defined$hash) {5254$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5255or die_error(404,"Error looking up file");5256}else{5257$ftype= git_get_type($hash);5258if($ftype!~"blob") {5259 die_error(400,"Object is not a blob");5260}5261}52625263my$fd;5264if($formateq'incremental') {5265# get file contents (as base)5266open$fd,"-|", git_cmd(),'cat-file','blob',$hash5267or die_error(500,"Open git-cat-file failed");5268}elsif($formateq'data') {5269# run git-blame --incremental5270open$fd,"-|", git_cmd(),"blame","--incremental",5271$hash_base,"--",$file_name5272or die_error(500,"Open git-blame --incremental failed");5273}else{5274# run git-blame --porcelain5275open$fd,"-|", git_cmd(),"blame",'-p',5276$hash_base,'--',$file_name5277or die_error(500,"Open git-blame --porcelain failed");5278}52795280# incremental blame data returns early5281if($formateq'data') {5282print$cgi->header(5283-type=>"text/plain", -charset =>"utf-8",5284-status=>"200 OK");5285local$| =1;# output autoflush5286printwhile<$fd>;5287close$fd5288or print"ERROR$!\n";52895290print'END';5291if(defined$t0&& gitweb_check_feature('timed')) {5292print' '.5293 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5294' '.$number_of_git_cmds;5295}5296print"\n";52975298return;5299}53005301# page header5302 git_header_html();5303my$formats_nav=5304$cgi->a({-href => href(action=>"blob", -replay=>1)},5305"blob") .5306" | ";5307if($formateq'incremental') {5308$formats_nav.=5309$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5310"blame") ." (non-incremental)";5311}else{5312$formats_nav.=5313$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5314"blame") ." (incremental)";5315}5316$formats_nav.=5317" | ".5318$cgi->a({-href => href(action=>"history", -replay=>1)},5319"history") .5320" | ".5321$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5322"HEAD");5323 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5324 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5325 git_print_page_path($file_name,$ftype,$hash_base);53265327# page body5328if($formateq'incremental') {5329print"<noscript>\n<div class=\"error\"><center><b>\n".5330"This page requires JavaScript to run.\nUse ".5331$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5332'this page').5333" instead.\n".5334"</b></center></div>\n</noscript>\n";53355336print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5337}53385339print qq!<div class="page_body">\n!;5340print qq!<div id="progress_info">.../ ...</div>\n!5341if($formateq'incremental');5342print qq!<table id="blame_table"class="blame" width="100%">\n!.5343#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5344 qq!<thead>\n!.5345 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5346 qq!</thead>\n!.5347 qq!<tbody>\n!;53485349my@rev_color=qw(light dark);5350my$num_colors=scalar(@rev_color);5351my$current_color=0;53525353if($formateq'incremental') {5354my$color_class=$rev_color[$current_color];53555356#contents of a file5357my$linenr=0;5358 LINE:5359while(my$line= <$fd>) {5360chomp$line;5361$linenr++;53625363print qq!<tr id="l$linenr"class="$color_class">!.5364 qq!<td class="sha1"><a href=""> </a></td>!.5365 qq!<td class="linenr">!.5366 qq!<a class="linenr" href="">$linenr</a></td>!;5367print qq!<td class="pre">! . esc_html($line) ."</td>\n";5368print qq!</tr>\n!;5369}53705371}else{# porcelain, i.e. ordinary blame5372my%metainfo= ();# saves information about commits53735374# blame data5375 LINE:5376while(my$line= <$fd>) {5377chomp$line;5378# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5379# no <lines in group> for subsequent lines in group of lines5380my($full_rev,$orig_lineno,$lineno,$group_size) =5381($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5382if(!exists$metainfo{$full_rev}) {5383$metainfo{$full_rev} = {'nprevious'=>0};5384}5385my$meta=$metainfo{$full_rev};5386my$data;5387while($data= <$fd>) {5388chomp$data;5389last if($data=~s/^\t//);# contents of line5390if($data=~/^(\S+)(?: (.*))?$/) {5391$meta->{$1} =$2unlessexists$meta->{$1};5392}5393if($data=~/^previous /) {5394$meta->{'nprevious'}++;5395}5396}5397my$short_rev=substr($full_rev,0,8);5398my$author=$meta->{'author'};5399my%date=5400 parse_date($meta->{'author-time'},$meta->{'author-tz'});5401my$date=$date{'iso-tz'};5402if($group_size) {5403$current_color= ($current_color+1) %$num_colors;5404}5405my$tr_class=$rev_color[$current_color];5406$tr_class.=' boundary'if(exists$meta->{'boundary'});5407$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5408$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5409print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5410if($group_size) {5411print"<td class=\"sha1\"";5412print" title=\"". esc_html($author) .",$date\"";5413print" rowspan=\"$group_size\""if($group_size>1);5414print">";5415print$cgi->a({-href => href(action=>"commit",5416 hash=>$full_rev,5417 file_name=>$file_name)},5418 esc_html($short_rev));5419if($group_size>=2) {5420my@author_initials= ($author=~/\b([[:upper:]])\B/g);5421if(@author_initials) {5422print"<br />".5423 esc_html(join('',@author_initials));5424# or join('.', ...)5425}5426}5427print"</td>\n";5428}5429# 'previous' <sha1 of parent commit> <filename at commit>5430if(exists$meta->{'previous'} &&5431$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5432$meta->{'parent'} =$1;5433$meta->{'file_parent'} = unquote($2);5434}5435my$linenr_commit=5436exists($meta->{'parent'}) ?5437$meta->{'parent'} :$full_rev;5438my$linenr_filename=5439exists($meta->{'file_parent'}) ?5440$meta->{'file_parent'} : unquote($meta->{'filename'});5441my$blamed= href(action =>'blame',5442 file_name =>$linenr_filename,5443 hash_base =>$linenr_commit);5444print"<td class=\"linenr\">";5445print$cgi->a({ -href =>"$blamed#l$orig_lineno",5446-class=>"linenr"},5447 esc_html($lineno));5448print"</td>";5449print"<td class=\"pre\">". esc_html($data) ."</td>\n";5450print"</tr>\n";5451}# end while54525453}54545455# footer5456print"</tbody>\n".5457"</table>\n";# class="blame"5458print"</div>\n";# class="blame_body"5459close$fd5460or print"Reading blob failed\n";54615462 git_footer_html();5463}54645465sub git_blame {5466 git_blame_common();5467}54685469sub git_blame_incremental {5470 git_blame_common('incremental');5471}54725473sub git_blame_data {5474 git_blame_common('data');5475}54765477sub git_tags {5478my$head= git_get_head_hash($project);5479 git_header_html();5480 git_print_page_nav('','',$head,undef,$head);5481 git_print_header_div('summary',$project);54825483my@tagslist= git_get_tags_list();5484if(@tagslist) {5485 git_tags_body(\@tagslist);5486}5487 git_footer_html();5488}54895490sub git_heads {5491my$head= git_get_head_hash($project);5492 git_header_html();5493 git_print_page_nav('','',$head,undef,$head);5494 git_print_header_div('summary',$project);54955496my@headslist= git_get_heads_list();5497if(@headslist) {5498 git_heads_body(\@headslist,$head);5499}5500 git_footer_html();5501}55025503sub git_blob_plain {5504my$type=shift;5505my$expires;55065507if(!defined$hash) {5508if(defined$file_name) {5509my$base=$hash_base|| git_get_head_hash($project);5510$hash= git_get_hash_by_path($base,$file_name,"blob")5511or die_error(404,"Cannot find file");5512}else{5513 die_error(400,"No file name defined");5514}5515}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5516# blobs defined by non-textual hash id's can be cached5517$expires="+1d";5518}55195520open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5521or die_error(500,"Open git-cat-file blob '$hash' failed");55225523# content-type (can include charset)5524$type= blob_contenttype($fd,$file_name,$type);55255526# "save as" filename, even when no $file_name is given5527my$save_as="$hash";5528if(defined$file_name) {5529$save_as=$file_name;5530}elsif($type=~m/^text\//) {5531$save_as.='.txt';5532}55335534# With XSS prevention on, blobs of all types except a few known safe5535# ones are served with "Content-Disposition: attachment" to make sure5536# they don't run in our security domain. For certain image types,5537# blob view writes an <img> tag referring to blob_plain view, and we5538# want to be sure not to break that by serving the image as an5539# attachment (though Firefox 3 doesn't seem to care).5540my$sandbox=$prevent_xss&&5541$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;55425543print$cgi->header(5544-type =>$type,5545-expires =>$expires,5546-content_disposition =>5547($sandbox?'attachment':'inline')5548.'; filename="'.$save_as.'"');5549local$/=undef;5550binmode STDOUT,':raw';5551print<$fd>;5552binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5553close$fd;5554}55555556sub git_blob {5557my$expires;55585559if(!defined$hash) {5560if(defined$file_name) {5561my$base=$hash_base|| git_get_head_hash($project);5562$hash= git_get_hash_by_path($base,$file_name,"blob")5563or die_error(404,"Cannot find file");5564}else{5565 die_error(400,"No file name defined");5566}5567}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5568# blobs defined by non-textual hash id's can be cached5569$expires="+1d";5570}55715572my$have_blame= gitweb_check_feature('blame');5573open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5574or die_error(500,"Couldn't cat$file_name,$hash");5575my$mimetype= blob_mimetype($fd,$file_name);5576# use 'blob_plain' (aka 'raw') view for files that cannot be displayed5577if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5578close$fd;5579return git_blob_plain($mimetype);5580}5581# we can have blame only for text/* mimetype5582$have_blame&&= ($mimetype=~m!^text/!);55835584my$highlight= gitweb_check_feature('highlight');5585my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);5586$fd= run_highlighter($fd,$highlight,$syntax)5587if$syntax;55885589 git_header_html(undef,$expires);5590my$formats_nav='';5591if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5592if(defined$file_name) {5593if($have_blame) {5594$formats_nav.=5595$cgi->a({-href => href(action=>"blame", -replay=>1)},5596"blame") .5597" | ";5598}5599$formats_nav.=5600$cgi->a({-href => href(action=>"history", -replay=>1)},5601"history") .5602" | ".5603$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5604"raw") .5605" | ".5606$cgi->a({-href => href(action=>"blob",5607 hash_base=>"HEAD", file_name=>$file_name)},5608"HEAD");5609}else{5610$formats_nav.=5611$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5612"raw");5613}5614 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5615 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5616}else{5617print"<div class=\"page_nav\">\n".5618"<br/><br/></div>\n".5619"<div class=\"title\">$hash</div>\n";5620}5621 git_print_page_path($file_name,"blob",$hash_base);5622print"<div class=\"page_body\">\n";5623if($mimetype=~m!^image/!) {5624print qq!<img type="$mimetype"!;5625if($file_name) {5626print qq! alt="$file_name" title="$file_name"!;5627}5628print qq! src="! .5629 href(action=>"blob_plain", hash=>$hash,5630 hash_base=>$hash_base, file_name=>$file_name) .5631 qq!"/>\n!;5632}else{5633my$nr;5634while(my$line= <$fd>) {5635chomp$line;5636$nr++;5637$line= untabify($line);5638printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,5639$nr, href(-replay =>1),$nr,$nr,$syntax?$line: esc_html($line, -nbsp=>1);5640}5641}5642close$fd5643or print"Reading blob failed.\n";5644print"</div>";5645 git_footer_html();5646}56475648sub git_tree {5649if(!defined$hash_base) {5650$hash_base="HEAD";5651}5652if(!defined$hash) {5653if(defined$file_name) {5654$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5655}else{5656$hash=$hash_base;5657}5658}5659 die_error(404,"No such tree")unlessdefined($hash);56605661my$show_sizes= gitweb_check_feature('show-sizes');5662my$have_blame= gitweb_check_feature('blame');56635664my@entries= ();5665{5666local$/="\0";5667open my$fd,"-|", git_cmd(),"ls-tree",'-z',5668($show_sizes?'-l': ()),@extra_options,$hash5669or die_error(500,"Open git-ls-tree failed");5670@entries=map{chomp;$_} <$fd>;5671close$fd5672or die_error(404,"Reading tree failed");5673}56745675my$refs= git_get_references();5676my$ref= format_ref_marker($refs,$hash_base);5677 git_header_html();5678my$basedir='';5679if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5680my@views_nav= ();5681if(defined$file_name) {5682push@views_nav,5683$cgi->a({-href => href(action=>"history", -replay=>1)},5684"history"),5685$cgi->a({-href => href(action=>"tree",5686 hash_base=>"HEAD", file_name=>$file_name)},5687"HEAD"),5688}5689my$snapshot_links= format_snapshot_links($hash);5690if(defined$snapshot_links) {5691# FIXME: Should be available when we have no hash base as well.5692push@views_nav,$snapshot_links;5693}5694 git_print_page_nav('tree','',$hash_base,undef,undef,5695join(' | ',@views_nav));5696 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5697}else{5698undef$hash_base;5699print"<div class=\"page_nav\">\n";5700print"<br/><br/></div>\n";5701print"<div class=\"title\">$hash</div>\n";5702}5703if(defined$file_name) {5704$basedir=$file_name;5705if($basedirne''&&substr($basedir, -1)ne'/') {5706$basedir.='/';5707}5708 git_print_page_path($file_name,'tree',$hash_base);5709}5710print"<div class=\"page_body\">\n";5711print"<table class=\"tree\">\n";5712my$alternate=1;5713# '..' (top directory) link if possible5714if(defined$hash_base&&5715defined$file_name&&$file_name=~m![^/]+$!) {5716if($alternate) {5717print"<tr class=\"dark\">\n";5718}else{5719print"<tr class=\"light\">\n";5720}5721$alternate^=1;57225723my$up=$file_name;5724$up=~s!/?[^/]+$!!;5725undef$upunless$up;5726# based on git_print_tree_entry5727print'<td class="mode">'. mode_str('040000') ."</td>\n";5728print'<td class="size"> </td>'."\n"if$show_sizes;5729print'<td class="list">';5730print$cgi->a({-href => href(action=>"tree",5731 hash_base=>$hash_base,5732 file_name=>$up)},5733"..");5734print"</td>\n";5735print"<td class=\"link\"></td>\n";57365737print"</tr>\n";5738}5739foreachmy$line(@entries) {5740my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);57415742if($alternate) {5743print"<tr class=\"dark\">\n";5744}else{5745print"<tr class=\"light\">\n";5746}5747$alternate^=1;57485749 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);57505751print"</tr>\n";5752}5753print"</table>\n".5754"</div>";5755 git_footer_html();5756}57575758sub snapshot_name {5759my($project,$hash) =@_;57605761# path/to/project.git -> project5762# path/to/project/.git -> project5763my$name= to_utf8($project);5764$name=~ s,([^/])/*\.git$,$1,;5765$name= basename($name);5766# sanitize name5767$name=~s/[[:cntrl:]]/?/g;57685769my$ver=$hash;5770if($hash=~/^[0-9a-fA-F]+$/) {5771# shorten SHA-1 hash5772my$full_hash= git_get_full_hash($project,$hash);5773if($full_hash=~/^$hash/&&length($hash) >7) {5774$ver= git_get_short_hash($project,$hash);5775}5776}elsif($hash=~m!^refs/tags/(.*)$!) {5777# tags don't need shortened SHA-1 hash5778$ver=$1;5779}else{5780# branches and other need shortened SHA-1 hash5781if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5782$ver=$1;5783}5784$ver.='-'. git_get_short_hash($project,$hash);5785}5786# in case of hierarchical branch names5787$ver=~s!/!.!g;57885789# name = project-version_string5790$name="$name-$ver";57915792returnwantarray? ($name,$name) :$name;5793}57945795sub git_snapshot {5796my$format=$input_params{'snapshot_format'};5797if(!@snapshot_fmts) {5798 die_error(403,"Snapshots not allowed");5799}5800# default to first supported snapshot format5801$format||=$snapshot_fmts[0];5802if($format!~m/^[a-z0-9]+$/) {5803 die_error(400,"Invalid snapshot format parameter");5804}elsif(!exists($known_snapshot_formats{$format})) {5805 die_error(400,"Unknown snapshot format");5806}elsif($known_snapshot_formats{$format}{'disabled'}) {5807 die_error(403,"Snapshot format not allowed");5808}elsif(!grep($_eq$format,@snapshot_fmts)) {5809 die_error(403,"Unsupported snapshot format");5810}58115812my$type= git_get_type("$hash^{}");5813if(!$type) {5814 die_error(404,'Object does not exist');5815}elsif($typeeq'blob') {5816 die_error(400,'Object is not a tree-ish');5817}58185819my($name,$prefix) = snapshot_name($project,$hash);5820my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5821my$cmd= quote_command(5822 git_cmd(),'archive',5823"--format=$known_snapshot_formats{$format}{'format'}",5824"--prefix=$prefix/",$hash);5825if(exists$known_snapshot_formats{$format}{'compressor'}) {5826$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5827}58285829$filename=~s/(["\\])/\\$1/g;5830print$cgi->header(5831-type =>$known_snapshot_formats{$format}{'type'},5832-content_disposition =>'inline; filename="'.$filename.'"',5833-status =>'200 OK');58345835open my$fd,"-|",$cmd5836or die_error(500,"Execute git-archive failed");5837binmode STDOUT,':raw';5838print<$fd>;5839binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5840close$fd;5841}58425843sub git_log_generic {5844my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;58455846my$head= git_get_head_hash($project);5847if(!defined$base) {5848$base=$head;5849}5850if(!defined$page) {5851$page=0;5852}5853my$refs= git_get_references();58545855my$commit_hash=$base;5856if(defined$parent) {5857$commit_hash="$parent..$base";5858}5859my@commitlist=5860 parse_commits($commit_hash,101, (100*$page),5861defined$file_name? ($file_name,"--full-history") : ());58625863my$ftype;5864if(!defined$file_hash&&defined$file_name) {5865# some commits could have deleted file in question,5866# and not have it in tree, but one of them has to have it5867for(my$i=0;$i<@commitlist;$i++) {5868$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5869last ifdefined$file_hash;5870}5871}5872if(defined$file_hash) {5873$ftype= git_get_type($file_hash);5874}5875if(defined$file_name&& !defined$ftype) {5876 die_error(500,"Unknown type of object");5877}5878my%co;5879if(defined$file_name) {5880%co= parse_commit($base)5881or die_error(404,"Unknown commit object");5882}588358845885my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5886my$next_link='';5887if($#commitlist>=100) {5888$next_link=5889$cgi->a({-href => href(-replay=>1, page=>$page+1),5890-accesskey =>"n", -title =>"Alt-n"},"next");5891}5892my$patch_max= gitweb_get_feature('patches');5893if($patch_max&& !defined$file_name) {5894if($patch_max<0||@commitlist<=$patch_max) {5895$paging_nav.=" ⋅ ".5896$cgi->a({-href => href(action=>"patches", -replay=>1)},5897"patches");5898}5899}59005901 git_header_html();5902 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5903if(defined$file_name) {5904 git_print_header_div('commit', esc_html($co{'title'}),$base);5905}else{5906 git_print_header_div('summary',$project)5907}5908 git_print_page_path($file_name,$ftype,$hash_base)5909if(defined$file_name);59105911$body_subr->(\@commitlist,0,99,$refs,$next_link,5912$file_name,$file_hash,$ftype);59135914 git_footer_html();5915}59165917sub git_log {5918 git_log_generic('log', \&git_log_body,5919$hash,$hash_parent);5920}59215922sub git_commit {5923$hash||=$hash_base||"HEAD";5924my%co= parse_commit($hash)5925or die_error(404,"Unknown commit object");59265927my$parent=$co{'parent'};5928my$parents=$co{'parents'};# listref59295930# we need to prepare $formats_nav before any parameter munging5931my$formats_nav;5932if(!defined$parent) {5933# --root commitdiff5934$formats_nav.='(initial)';5935}elsif(@$parents==1) {5936# single parent commit5937$formats_nav.=5938'(parent: '.5939$cgi->a({-href => href(action=>"commit",5940 hash=>$parent)},5941 esc_html(substr($parent,0,7))) .5942')';5943}else{5944# merge commit5945$formats_nav.=5946'(merge: '.5947join(' ',map{5948$cgi->a({-href => href(action=>"commit",5949 hash=>$_)},5950 esc_html(substr($_,0,7)));5951}@$parents) .5952')';5953}5954if(gitweb_check_feature('patches') &&@$parents<=1) {5955$formats_nav.=" | ".5956$cgi->a({-href => href(action=>"patch", -replay=>1)},5957"patch");5958}59595960if(!defined$parent) {5961$parent="--root";5962}5963my@difftree;5964open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5965@diff_opts,5966(@$parents<=1?$parent:'-c'),5967$hash,"--"5968or die_error(500,"Open git-diff-tree failed");5969@difftree=map{chomp;$_} <$fd>;5970close$fdor die_error(404,"Reading git-diff-tree failed");59715972# non-textual hash id's can be cached5973my$expires;5974if($hash=~m/^[0-9a-fA-F]{40}$/) {5975$expires="+1d";5976}5977my$refs= git_get_references();5978my$ref= format_ref_marker($refs,$co{'id'});59795980 git_header_html(undef,$expires);5981 git_print_page_nav('commit','',5982$hash,$co{'tree'},$hash,5983$formats_nav);59845985if(defined$co{'parent'}) {5986 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5987}else{5988 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5989}5990print"<div class=\"title_text\">\n".5991"<table class=\"object_header\">\n";5992 git_print_authorship_rows(\%co);5993print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5994print"<tr>".5995"<td>tree</td>".5996"<td class=\"sha1\">".5997$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5998class=>"list"},$co{'tree'}) .5999"</td>".6000"<td class=\"link\">".6001$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},6002"tree");6003my$snapshot_links= format_snapshot_links($hash);6004if(defined$snapshot_links) {6005print" | ".$snapshot_links;6006}6007print"</td>".6008"</tr>\n";60096010foreachmy$par(@$parents) {6011print"<tr>".6012"<td>parent</td>".6013"<td class=\"sha1\">".6014$cgi->a({-href => href(action=>"commit", hash=>$par),6015class=>"list"},$par) .6016"</td>".6017"<td class=\"link\">".6018$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .6019" | ".6020$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .6021"</td>".6022"</tr>\n";6023}6024print"</table>".6025"</div>\n";60266027print"<div class=\"page_body\">\n";6028 git_print_log($co{'comment'});6029print"</div>\n";60306031 git_difftree_body(\@difftree,$hash,@$parents);60326033 git_footer_html();6034}60356036sub git_object {6037# object is defined by:6038# - hash or hash_base alone6039# - hash_base and file_name6040my$type;60416042# - hash or hash_base alone6043if($hash|| ($hash_base&& !defined$file_name)) {6044my$object_id=$hash||$hash_base;60456046open my$fd,"-|", quote_command(6047 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'6048or die_error(404,"Object does not exist");6049$type= <$fd>;6050chomp$type;6051close$fd6052or die_error(404,"Object does not exist");60536054# - hash_base and file_name6055}elsif($hash_base&&defined$file_name) {6056$file_name=~ s,/+$,,;60576058system(git_cmd(),"cat-file",'-e',$hash_base) ==06059or die_error(404,"Base object does not exist");60606061# here errors should not hapen6062open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name6063or die_error(500,"Open git-ls-tree failed");6064my$line= <$fd>;6065close$fd;60666067#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'6068unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {6069 die_error(404,"File or directory for given base does not exist");6070}6071$type=$2;6072$hash=$3;6073}else{6074 die_error(400,"Not enough information to find object");6075}60766077print$cgi->redirect(-uri => href(action=>$type, -full=>1,6078 hash=>$hash, hash_base=>$hash_base,6079 file_name=>$file_name),6080-status =>'302 Found');6081}60826083sub git_blobdiff {6084my$format=shift||'html';60856086my$fd;6087my@difftree;6088my%diffinfo;6089my$expires;60906091# preparing $fd and %diffinfo for git_patchset_body6092# new style URI6093if(defined$hash_base&&defined$hash_parent_base) {6094if(defined$file_name) {6095# read raw output6096open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6097$hash_parent_base,$hash_base,6098"--", (defined$file_parent?$file_parent: ()),$file_name6099or die_error(500,"Open git-diff-tree failed");6100@difftree=map{chomp;$_} <$fd>;6101close$fd6102or die_error(404,"Reading git-diff-tree failed");6103@difftree6104or die_error(404,"Blob diff not found");61056106}elsif(defined$hash&&6107$hash=~/[0-9a-fA-F]{40}/) {6108# try to find filename from $hash61096110# read filtered raw output6111open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6112$hash_parent_base,$hash_base,"--"6113or die_error(500,"Open git-diff-tree failed");6114@difftree=6115# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'6116# $hash == to_id6117grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}6118map{chomp;$_} <$fd>;6119close$fd6120or die_error(404,"Reading git-diff-tree failed");6121@difftree6122or die_error(404,"Blob diff not found");61236124}else{6125 die_error(400,"Missing one of the blob diff parameters");6126}61276128if(@difftree>1) {6129 die_error(400,"Ambiguous blob diff specification");6130}61316132%diffinfo= parse_difftree_raw_line($difftree[0]);6133$file_parent||=$diffinfo{'from_file'} ||$file_name;6134$file_name||=$diffinfo{'to_file'};61356136$hash_parent||=$diffinfo{'from_id'};6137$hash||=$diffinfo{'to_id'};61386139# non-textual hash id's can be cached6140if($hash_base=~m/^[0-9a-fA-F]{40}$/&&6141$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {6142$expires='+1d';6143}61446145# open patch output6146open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6147'-p', ($formateq'html'?"--full-index": ()),6148$hash_parent_base,$hash_base,6149"--", (defined$file_parent?$file_parent: ()),$file_name6150or die_error(500,"Open git-diff-tree failed");6151}61526153# old/legacy style URI -- not generated anymore since 1.4.3.6154if(!%diffinfo) {6155 die_error('404 Not Found',"Missing one of the blob diff parameters")6156}61576158# header6159if($formateq'html') {6160my$formats_nav=6161$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},6162"raw");6163 git_header_html(undef,$expires);6164if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6165 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6166 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6167}else{6168print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";6169print"<div class=\"title\">$hashvs$hash_parent</div>\n";6170}6171if(defined$file_name) {6172 git_print_page_path($file_name,"blob",$hash_base);6173}else{6174print"<div class=\"page_path\"></div>\n";6175}61766177}elsif($formateq'plain') {6178print$cgi->header(6179-type =>'text/plain',6180-charset =>'utf-8',6181-expires =>$expires,6182-content_disposition =>'inline; filename="'."$file_name".'.patch"');61836184print"X-Git-Url: ".$cgi->self_url() ."\n\n";61856186}else{6187 die_error(400,"Unknown blobdiff format");6188}61896190# patch6191if($formateq'html') {6192print"<div class=\"page_body\">\n";61936194 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);6195close$fd;61966197print"</div>\n";# class="page_body"6198 git_footer_html();61996200}else{6201while(my$line= <$fd>) {6202$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;6203$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;62046205print$line;62066207last if$line=~m!^\+\+\+!;6208}6209local$/=undef;6210print<$fd>;6211close$fd;6212}6213}62146215sub git_blobdiff_plain {6216 git_blobdiff('plain');6217}62186219sub git_commitdiff {6220my%params=@_;6221my$format=$params{-format} ||'html';62226223my($patch_max) = gitweb_get_feature('patches');6224if($formateq'patch') {6225 die_error(403,"Patch view not allowed")unless$patch_max;6226}62276228$hash||=$hash_base||"HEAD";6229my%co= parse_commit($hash)6230or die_error(404,"Unknown commit object");62316232# choose format for commitdiff for merge6233if(!defined$hash_parent&& @{$co{'parents'}} >1) {6234$hash_parent='--cc';6235}6236# we need to prepare $formats_nav before almost any parameter munging6237my$formats_nav;6238if($formateq'html') {6239$formats_nav=6240$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6241"raw");6242if($patch_max&& @{$co{'parents'}} <=1) {6243$formats_nav.=" | ".6244$cgi->a({-href => href(action=>"patch", -replay=>1)},6245"patch");6246}62476248if(defined$hash_parent&&6249$hash_parentne'-c'&&$hash_parentne'--cc') {6250# commitdiff with two commits given6251my$hash_parent_short=$hash_parent;6252if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6253$hash_parent_short=substr($hash_parent,0,7);6254}6255$formats_nav.=6256' (from';6257for(my$i=0;$i< @{$co{'parents'}};$i++) {6258if($co{'parents'}[$i]eq$hash_parent) {6259$formats_nav.=' parent '. ($i+1);6260last;6261}6262}6263$formats_nav.=': '.6264$cgi->a({-href => href(action=>"commitdiff",6265 hash=>$hash_parent)},6266 esc_html($hash_parent_short)) .6267')';6268}elsif(!$co{'parent'}) {6269# --root commitdiff6270$formats_nav.=' (initial)';6271}elsif(scalar@{$co{'parents'}} ==1) {6272# single parent commit6273$formats_nav.=6274' (parent: '.6275$cgi->a({-href => href(action=>"commitdiff",6276 hash=>$co{'parent'})},6277 esc_html(substr($co{'parent'},0,7))) .6278')';6279}else{6280# merge commit6281if($hash_parenteq'--cc') {6282$formats_nav.=' | '.6283$cgi->a({-href => href(action=>"commitdiff",6284 hash=>$hash, hash_parent=>'-c')},6285'combined');6286}else{# $hash_parent eq '-c'6287$formats_nav.=' | '.6288$cgi->a({-href => href(action=>"commitdiff",6289 hash=>$hash, hash_parent=>'--cc')},6290'compact');6291}6292$formats_nav.=6293' (merge: '.6294join(' ',map{6295$cgi->a({-href => href(action=>"commitdiff",6296 hash=>$_)},6297 esc_html(substr($_,0,7)));6298} @{$co{'parents'}} ) .6299')';6300}6301}63026303my$hash_parent_param=$hash_parent;6304if(!defined$hash_parent_param) {6305# --cc for multiple parents, --root for parentless6306$hash_parent_param=6307@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6308}63096310# read commitdiff6311my$fd;6312my@difftree;6313if($formateq'html') {6314open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6315"--no-commit-id","--patch-with-raw","--full-index",6316$hash_parent_param,$hash,"--"6317or die_error(500,"Open git-diff-tree failed");63186319while(my$line= <$fd>) {6320chomp$line;6321# empty line ends raw part of diff-tree output6322last unless$line;6323push@difftree,scalar parse_difftree_raw_line($line);6324}63256326}elsif($formateq'plain') {6327open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6328'-p',$hash_parent_param,$hash,"--"6329or die_error(500,"Open git-diff-tree failed");6330}elsif($formateq'patch') {6331# For commit ranges, we limit the output to the number of6332# patches specified in the 'patches' feature.6333# For single commits, we limit the output to a single patch,6334# diverging from the git-format-patch default.6335my@commit_spec= ();6336if($hash_parent) {6337if($patch_max>0) {6338push@commit_spec,"-$patch_max";6339}6340push@commit_spec,'-n',"$hash_parent..$hash";6341}else{6342if($params{-single}) {6343push@commit_spec,'-1';6344}else{6345if($patch_max>0) {6346push@commit_spec,"-$patch_max";6347}6348push@commit_spec,"-n";6349}6350push@commit_spec,'--root',$hash;6351}6352open$fd,"-|", git_cmd(),"format-patch",@diff_opts,6353'--encoding=utf8','--stdout',@commit_spec6354or die_error(500,"Open git-format-patch failed");6355}else{6356 die_error(400,"Unknown commitdiff format");6357}63586359# non-textual hash id's can be cached6360my$expires;6361if($hash=~m/^[0-9a-fA-F]{40}$/) {6362$expires="+1d";6363}63646365# write commit message6366if($formateq'html') {6367my$refs= git_get_references();6368my$ref= format_ref_marker($refs,$co{'id'});63696370 git_header_html(undef,$expires);6371 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6372 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6373print"<div class=\"title_text\">\n".6374"<table class=\"object_header\">\n";6375 git_print_authorship_rows(\%co);6376print"</table>".6377"</div>\n";6378print"<div class=\"page_body\">\n";6379if(@{$co{'comment'}} >1) {6380print"<div class=\"log\">\n";6381 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6382print"</div>\n";# class="log"6383}63846385}elsif($formateq'plain') {6386my$refs= git_get_references("tags");6387my$tagname= git_get_rev_name_tags($hash);6388my$filename= basename($project) ."-$hash.patch";63896390print$cgi->header(6391-type =>'text/plain',6392-charset =>'utf-8',6393-expires =>$expires,6394-content_disposition =>'inline; filename="'."$filename".'"');6395my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6396print"From: ". to_utf8($co{'author'}) ."\n";6397print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6398print"Subject: ". to_utf8($co{'title'}) ."\n";63996400print"X-Git-Tag:$tagname\n"if$tagname;6401print"X-Git-Url: ".$cgi->self_url() ."\n\n";64026403foreachmy$line(@{$co{'comment'}}) {6404print to_utf8($line) ."\n";6405}6406print"---\n\n";6407}elsif($formateq'patch') {6408my$filename= basename($project) ."-$hash.patch";64096410print$cgi->header(6411-type =>'text/plain',6412-charset =>'utf-8',6413-expires =>$expires,6414-content_disposition =>'inline; filename="'."$filename".'"');6415}64166417# write patch6418if($formateq'html') {6419my$use_parents= !defined$hash_parent||6420$hash_parenteq'-c'||$hash_parenteq'--cc';6421 git_difftree_body(\@difftree,$hash,6422$use_parents? @{$co{'parents'}} :$hash_parent);6423print"<br/>\n";64246425 git_patchset_body($fd, \@difftree,$hash,6426$use_parents? @{$co{'parents'}} :$hash_parent);6427close$fd;6428print"</div>\n";# class="page_body"6429 git_footer_html();64306431}elsif($formateq'plain') {6432local$/=undef;6433print<$fd>;6434close$fd6435or print"Reading git-diff-tree failed\n";6436}elsif($formateq'patch') {6437local$/=undef;6438print<$fd>;6439close$fd6440or print"Reading git-format-patch failed\n";6441}6442}64436444sub git_commitdiff_plain {6445 git_commitdiff(-format =>'plain');6446}64476448# format-patch-style patches6449sub git_patch {6450 git_commitdiff(-format =>'patch', -single =>1);6451}64526453sub git_patches {6454 git_commitdiff(-format =>'patch');6455}64566457sub git_history {6458 git_log_generic('history', \&git_history_body,6459$hash_base,$hash_parent_base,6460$file_name,$hash);6461}64626463sub git_search {6464 gitweb_check_feature('search')or die_error(403,"Search is disabled");6465if(!defined$searchtext) {6466 die_error(400,"Text field is empty");6467}6468if(!defined$hash) {6469$hash= git_get_head_hash($project);6470}6471my%co= parse_commit($hash);6472if(!%co) {6473 die_error(404,"Unknown commit object");6474}6475if(!defined$page) {6476$page=0;6477}64786479$searchtype||='commit';6480if($searchtypeeq'pickaxe') {6481# pickaxe may take all resources of your box and run for several minutes6482# with every query - so decide by yourself how public you make this feature6483 gitweb_check_feature('pickaxe')6484or die_error(403,"Pickaxe is disabled");6485}6486if($searchtypeeq'grep') {6487 gitweb_check_feature('grep')6488or die_error(403,"Grep is disabled");6489}64906491 git_header_html();64926493if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6494my$greptype;6495if($searchtypeeq'commit') {6496$greptype="--grep=";6497}elsif($searchtypeeq'author') {6498$greptype="--author=";6499}elsif($searchtypeeq'committer') {6500$greptype="--committer=";6501}6502$greptype.=$searchtext;6503my@commitlist= parse_commits($hash,101, (100*$page),undef,6504$greptype,'--regexp-ignore-case',6505$search_use_regexp?'--extended-regexp':'--fixed-strings');65066507my$paging_nav='';6508if($page>0) {6509$paging_nav.=6510$cgi->a({-href => href(action=>"search", hash=>$hash,6511 searchtext=>$searchtext,6512 searchtype=>$searchtype)},6513"first");6514$paging_nav.=" ⋅ ".6515$cgi->a({-href => href(-replay=>1, page=>$page-1),6516-accesskey =>"p", -title =>"Alt-p"},"prev");6517}else{6518$paging_nav.="first";6519$paging_nav.=" ⋅ prev";6520}6521my$next_link='';6522if($#commitlist>=100) {6523$next_link=6524$cgi->a({-href => href(-replay=>1, page=>$page+1),6525-accesskey =>"n", -title =>"Alt-n"},"next");6526$paging_nav.=" ⋅$next_link";6527}else{6528$paging_nav.=" ⋅ next";6529}65306531 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6532 git_print_header_div('commit', esc_html($co{'title'}),$hash);6533if($page==0&& !@commitlist) {6534print"<p>No match.</p>\n";6535}else{6536 git_search_grep_body(\@commitlist,0,99,$next_link);6537}6538}65396540if($searchtypeeq'pickaxe') {6541 git_print_page_nav('','',$hash,$co{'tree'},$hash);6542 git_print_header_div('commit', esc_html($co{'title'}),$hash);65436544print"<table class=\"pickaxe search\">\n";6545my$alternate=1;6546local$/="\n";6547open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6548'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6549($search_use_regexp?'--pickaxe-regex': ());6550undef%co;6551my@files;6552while(my$line= <$fd>) {6553chomp$line;6554next unless$line;65556556my%set= parse_difftree_raw_line($line);6557if(defined$set{'commit'}) {6558# finish previous commit6559if(%co) {6560print"</td>\n".6561"<td class=\"link\">".6562$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6563" | ".6564$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6565print"</td>\n".6566"</tr>\n";6567}65686569if($alternate) {6570print"<tr class=\"dark\">\n";6571}else{6572print"<tr class=\"light\">\n";6573}6574$alternate^=1;6575%co= parse_commit($set{'commit'});6576my$author= chop_and_escape_str($co{'author_name'},15,5);6577print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6578"<td><i>$author</i></td>\n".6579"<td>".6580$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6581-class=>"list subject"},6582 chop_and_escape_str($co{'title'},50) ."<br/>");6583}elsif(defined$set{'to_id'}) {6584next if($set{'to_id'} =~m/^0{40}$/);65856586print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6587 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6588-class=>"list"},6589"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6590"<br/>\n";6591}6592}6593close$fd;65946595# finish last commit (warning: repetition!)6596if(%co) {6597print"</td>\n".6598"<td class=\"link\">".6599$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6600" | ".6601$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6602print"</td>\n".6603"</tr>\n";6604}66056606print"</table>\n";6607}66086609if($searchtypeeq'grep') {6610 git_print_page_nav('','',$hash,$co{'tree'},$hash);6611 git_print_header_div('commit', esc_html($co{'title'}),$hash);66126613print"<table class=\"grep_search\">\n";6614my$alternate=1;6615my$matches=0;6616local$/="\n";6617open my$fd,"-|", git_cmd(),'grep','-n',6618$search_use_regexp? ('-E','-i') :'-F',6619$searchtext,$co{'tree'};6620my$lastfile='';6621while(my$line= <$fd>) {6622chomp$line;6623my($file,$lno,$ltext,$binary);6624last if($matches++>1000);6625if($line=~/^Binary file (.+) matches$/) {6626$file=$1;6627$binary=1;6628}else{6629(undef,$file,$lno,$ltext) =split(/:/,$line,4);6630}6631if($filene$lastfile) {6632$lastfileand print"</td></tr>\n";6633if($alternate++) {6634print"<tr class=\"dark\">\n";6635}else{6636print"<tr class=\"light\">\n";6637}6638print"<td class=\"list\">".6639$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6640 file_name=>"$file"),6641-class=>"list"}, esc_path($file));6642print"</td><td>\n";6643$lastfile=$file;6644}6645if($binary) {6646print"<div class=\"binary\">Binary file</div>\n";6647}else{6648$ltext= untabify($ltext);6649if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6650$ltext= esc_html($1, -nbsp=>1);6651$ltext.='<span class="match">';6652$ltext.= esc_html($2, -nbsp=>1);6653$ltext.='</span>';6654$ltext.= esc_html($3, -nbsp=>1);6655}else{6656$ltext= esc_html($ltext, -nbsp=>1);6657}6658print"<div class=\"pre\">".6659$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6660 file_name=>"$file").'#l'.$lno,6661-class=>"linenr"},sprintf('%4i',$lno))6662.' '.$ltext."</div>\n";6663}6664}6665if($lastfile) {6666print"</td></tr>\n";6667if($matches>1000) {6668print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6669}6670}else{6671print"<div class=\"diff nodifferences\">No matches found</div>\n";6672}6673close$fd;66746675print"</table>\n";6676}6677 git_footer_html();6678}66796680sub git_search_help {6681 git_header_html();6682 git_print_page_nav('','',$hash,$hash,$hash);6683print<<EOT;6684<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6685regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6686the pattern entered is recognized as the POSIX extended6687<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6688insensitive).</p>6689<dl>6690<dt><b>commit</b></dt>6691<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6692EOT6693my$have_grep= gitweb_check_feature('grep');6694if($have_grep) {6695print<<EOT;6696<dt><b>grep</b></dt>6697<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6698 a different one) are searched for the given pattern. On large trees, this search can take6699a while and put some strain on the server, so please use it with some consideration. Note that6700due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6701case-sensitive.</dd>6702EOT6703}6704print<<EOT;6705<dt><b>author</b></dt>6706<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6707<dt><b>committer</b></dt>6708<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6709EOT6710my$have_pickaxe= gitweb_check_feature('pickaxe');6711if($have_pickaxe) {6712print<<EOT;6713<dt><b>pickaxe</b></dt>6714<dd>All commits that caused the string to appear or disappear from any file (changes that6715added, removed or "modified" the string) will be listed. This search can take a while and6716takes a lot of strain on the server, so please use it wisely. Note that since you may be6717interested even in changes just changing the case as well, this search is case sensitive.</dd>6718EOT6719}6720print"</dl>\n";6721 git_footer_html();6722}67236724sub git_shortlog {6725 git_log_generic('shortlog', \&git_shortlog_body,6726$hash,$hash_parent);6727}67286729## ......................................................................6730## feeds (RSS, Atom; OPML)67316732sub git_feed {6733my$format=shift||'atom';6734my$have_blame= gitweb_check_feature('blame');67356736# Atom: http://www.atomenabled.org/developers/syndication/6737# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6738if($formatne'rss'&&$formatne'atom') {6739 die_error(400,"Unknown web feed format");6740}67416742# log/feed of current (HEAD) branch, log of given branch, history of file/directory6743my$head=$hash||'HEAD';6744my@commitlist= parse_commits($head,150,0,$file_name);67456746my%latest_commit;6747my%latest_date;6748my$content_type="application/$format+xml";6749if(defined$cgi->http('HTTP_ACCEPT') &&6750$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6751# browser (feed reader) prefers text/xml6752$content_type='text/xml';6753}6754if(defined($commitlist[0])) {6755%latest_commit= %{$commitlist[0]};6756my$latest_epoch=$latest_commit{'committer_epoch'};6757%latest_date= parse_date($latest_epoch);6758my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6759if(defined$if_modified) {6760my$since;6761if(eval{require HTTP::Date;1; }) {6762$since= HTTP::Date::str2time($if_modified);6763}elsif(eval{require Time::ParseDate;1; }) {6764$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6765}6766if(defined$since&&$latest_epoch<=$since) {6767print$cgi->header(6768-type =>$content_type,6769-charset =>'utf-8',6770-last_modified =>$latest_date{'rfc2822'},6771-status =>'304 Not Modified');6772return;6773}6774}6775print$cgi->header(6776-type =>$content_type,6777-charset =>'utf-8',6778-last_modified =>$latest_date{'rfc2822'});6779}else{6780print$cgi->header(6781-type =>$content_type,6782-charset =>'utf-8');6783}67846785# Optimization: skip generating the body if client asks only6786# for Last-Modified date.6787return if($cgi->request_method()eq'HEAD');67886789# header variables6790my$title="$site_name-$project/$action";6791my$feed_type='log';6792if(defined$hash) {6793$title.=" - '$hash'";6794$feed_type='branch log';6795if(defined$file_name) {6796$title.=" ::$file_name";6797$feed_type='history';6798}6799}elsif(defined$file_name) {6800$title.=" -$file_name";6801$feed_type='history';6802}6803$title.="$feed_type";6804my$descr= git_get_project_description($project);6805if(defined$descr) {6806$descr= esc_html($descr);6807}else{6808$descr="$project".6809($formateq'rss'?'RSS':'Atom') .6810" feed";6811}6812my$owner= git_get_project_owner($project);6813$owner= esc_html($owner);68146815#header6816my$alt_url;6817if(defined$file_name) {6818$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6819}elsif(defined$hash) {6820$alt_url= href(-full=>1, action=>"log", hash=>$hash);6821}else{6822$alt_url= href(-full=>1, action=>"summary");6823}6824print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6825if($formateq'rss') {6826print<<XML;6827<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6828<channel>6829XML6830print"<title>$title</title>\n".6831"<link>$alt_url</link>\n".6832"<description>$descr</description>\n".6833"<language>en</language>\n".6834# project owner is responsible for 'editorial' content6835"<managingEditor>$owner</managingEditor>\n";6836if(defined$logo||defined$favicon) {6837# prefer the logo to the favicon, since RSS6838# doesn't allow both6839my$img= esc_url($logo||$favicon);6840print"<image>\n".6841"<url>$img</url>\n".6842"<title>$title</title>\n".6843"<link>$alt_url</link>\n".6844"</image>\n";6845}6846if(%latest_date) {6847print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6848print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6849}6850print"<generator>gitweb v.$version/$git_version</generator>\n";6851}elsif($formateq'atom') {6852print<<XML;6853<feed xmlns="http://www.w3.org/2005/Atom">6854XML6855print"<title>$title</title>\n".6856"<subtitle>$descr</subtitle>\n".6857'<link rel="alternate" type="text/html" href="'.6858$alt_url.'" />'."\n".6859'<link rel="self" type="'.$content_type.'" href="'.6860$cgi->self_url() .'" />'."\n".6861"<id>". href(-full=>1) ."</id>\n".6862# use project owner for feed author6863"<author><name>$owner</name></author>\n";6864if(defined$favicon) {6865print"<icon>". esc_url($favicon) ."</icon>\n";6866}6867if(defined$logo_url) {6868# not twice as wide as tall: 72 x 27 pixels6869print"<logo>". esc_url($logo) ."</logo>\n";6870}6871if(!%latest_date) {6872# dummy date to keep the feed valid until commits trickle in:6873print"<updated>1970-01-01T00:00:00Z</updated>\n";6874}else{6875print"<updated>$latest_date{'iso-8601'}</updated>\n";6876}6877print"<generator version='$version/$git_version'>gitweb</generator>\n";6878}68796880# contents6881for(my$i=0;$i<=$#commitlist;$i++) {6882my%co= %{$commitlist[$i]};6883my$commit=$co{'id'};6884# we read 150, we always show 30 and the ones more recent than 48 hours6885if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6886last;6887}6888my%cd= parse_date($co{'author_epoch'});68896890# get list of changed files6891open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6892$co{'parent'} ||"--root",6893$co{'id'},"--", (defined$file_name?$file_name: ())6894ornext;6895my@difftree=map{chomp;$_} <$fd>;6896close$fd6897ornext;68986899# print element (entry, item)6900my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6901if($formateq'rss') {6902print"<item>\n".6903"<title>". esc_html($co{'title'}) ."</title>\n".6904"<author>". esc_html($co{'author'}) ."</author>\n".6905"<pubDate>$cd{'rfc2822'}</pubDate>\n".6906"<guid isPermaLink=\"true\">$co_url</guid>\n".6907"<link>$co_url</link>\n".6908"<description>". esc_html($co{'title'}) ."</description>\n".6909"<content:encoded>".6910"<![CDATA[\n";6911}elsif($formateq'atom') {6912print"<entry>\n".6913"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6914"<updated>$cd{'iso-8601'}</updated>\n".6915"<author>\n".6916" <name>". esc_html($co{'author_name'}) ."</name>\n";6917if($co{'author_email'}) {6918print" <email>". esc_html($co{'author_email'}) ."</email>\n";6919}6920print"</author>\n".6921# use committer for contributor6922"<contributor>\n".6923" <name>". esc_html($co{'committer_name'}) ."</name>\n";6924if($co{'committer_email'}) {6925print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6926}6927print"</contributor>\n".6928"<published>$cd{'iso-8601'}</published>\n".6929"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6930"<id>$co_url</id>\n".6931"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6932"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6933}6934my$comment=$co{'comment'};6935print"<pre>\n";6936foreachmy$line(@$comment) {6937$line= esc_html($line);6938print"$line\n";6939}6940print"</pre><ul>\n";6941foreachmy$difftree_line(@difftree) {6942my%difftree= parse_difftree_raw_line($difftree_line);6943next if!$difftree{'from_id'};69446945my$file=$difftree{'file'} ||$difftree{'to_file'};69466947print"<li>".6948"[".6949$cgi->a({-href => href(-full=>1, action=>"blobdiff",6950 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6951 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6952 file_name=>$file, file_parent=>$difftree{'from_file'}),6953-title =>"diff"},'D');6954if($have_blame) {6955print$cgi->a({-href => href(-full=>1, action=>"blame",6956 file_name=>$file, hash_base=>$commit),6957-title =>"blame"},'B');6958}6959# if this is not a feed of a file history6960if(!defined$file_name||$file_namene$file) {6961print$cgi->a({-href => href(-full=>1, action=>"history",6962 file_name=>$file, hash=>$commit),6963-title =>"history"},'H');6964}6965$file= esc_path($file);6966print"] ".6967"$file</li>\n";6968}6969if($formateq'rss') {6970print"</ul>]]>\n".6971"</content:encoded>\n".6972"</item>\n";6973}elsif($formateq'atom') {6974print"</ul>\n</div>\n".6975"</content>\n".6976"</entry>\n";6977}6978}69796980# end of feed6981if($formateq'rss') {6982print"</channel>\n</rss>\n";6983}elsif($formateq'atom') {6984print"</feed>\n";6985}6986}69876988sub git_rss {6989 git_feed('rss');6990}69916992sub git_atom {6993 git_feed('atom');6994}69956996sub git_opml {6997my@list= git_get_projects_list();69986999print$cgi->header(7000-type =>'text/xml',7001-charset =>'utf-8',7002-content_disposition =>'inline; filename="opml.xml"');70037004print<<XML;7005<?xml version="1.0" encoding="utf-8"?>7006<opml version="1.0">7007<head>7008 <title>$site_nameOPML Export</title>7009</head>7010<body>7011<outline text="git RSS feeds">7012XML70137014foreachmy$pr(@list) {7015my%proj=%$pr;7016my$head= git_get_head_hash($proj{'path'});7017if(!defined$head) {7018next;7019}7020$git_dir="$projectroot/$proj{'path'}";7021my%co= parse_commit($head);7022if(!%co) {7023next;7024}70257026my$path= esc_html(chop_str($proj{'path'},25,5));7027my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);7028my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);7029print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";7030}7031print<<XML;7032</outline>7033</body>7034</opml>7035XML7036}