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); 15use Encode; 16use Fcntl ':mode'; 17use File::Find qw(); 18use File::Basename qw(basename); 19binmode STDOUT,':utf8'; 20 21BEGIN{ 22 CGI->compile()if$ENV{'MOD_PERL'}; 23} 24 25our$cgi= new CGI; 26our$version="++GIT_VERSION++"; 27our$my_url=$cgi->url(); 28our$my_uri=$cgi->url(-absolute =>1); 29 30# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 31# needed and used only for URLs with nonempty PATH_INFO 32our$base_url=$my_url; 33 34# When the script is used as DirectoryIndex, the URL does not contain the name 35# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 36# have to do it ourselves. We make $path_info global because it's also used 37# later on. 38# 39# Another issue with the script being the DirectoryIndex is that the resulting 40# $my_url data is not the full script URL: this is good, because we want 41# generated links to keep implying the script name if it wasn't explicitly 42# indicated in the URL we're handling, but it means that $my_url cannot be used 43# as base URL. 44# Therefore, if we needed to strip PATH_INFO, then we know that we have 45# to build the base URL ourselves: 46our$path_info=$ENV{"PATH_INFO"}; 47if($path_info) { 48if($my_url=~ s,\Q$path_info\E$,, && 49$my_uri=~ s,\Q$path_info\E$,, && 50defined$ENV{'SCRIPT_NAME'}) { 51$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 52} 53} 54 55# core git executable to use 56# this can just be "git" if your webserver has a sensible PATH 57our$GIT="++GIT_BINDIR++/git"; 58 59# absolute fs-path which will be prepended to the project path 60#our $projectroot = "/pub/scm"; 61our$projectroot="++GITWEB_PROJECTROOT++"; 62 63# fs traversing limit for getting project list 64# the number is relative to the projectroot 65our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 66 67# target of the home link on top of all pages 68our$home_link=$my_uri||"/"; 69 70# string of the home link on top of all pages 71our$home_link_str="++GITWEB_HOME_LINK_STR++"; 72 73# name of your site or organization to appear in page titles 74# replace this with something more descriptive for clearer bookmarks 75our$site_name="++GITWEB_SITENAME++" 76|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 77 78# filename of html text to include at top of each page 79our$site_header="++GITWEB_SITE_HEADER++"; 80# html text to include at home page 81our$home_text="++GITWEB_HOMETEXT++"; 82# filename of html text to include at bottom of each page 83our$site_footer="++GITWEB_SITE_FOOTER++"; 84 85# URI of stylesheets 86our@stylesheets= ("++GITWEB_CSS++"); 87# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 88our$stylesheet=undef; 89# URI of GIT logo (72x27 size) 90our$logo="++GITWEB_LOGO++"; 91# URI of GIT favicon, assumed to be image/png type 92our$favicon="++GITWEB_FAVICON++"; 93 94# URI and label (title) of GIT logo link 95#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 96#our $logo_label = "git documentation"; 97our$logo_url="http://git.or.cz/"; 98our$logo_label="git homepage"; 99 100# source of projects list 101our$projects_list="++GITWEB_LIST++"; 102 103# the width (in characters) of the projects list "Description" column 104our$projects_list_description_width=25; 105 106# default order of projects list 107# valid values are none, project, descr, owner, and age 108our$default_projects_order="project"; 109 110# show repository only if this file exists 111# (only effective if this variable evaluates to true) 112our$export_ok="++GITWEB_EXPORT_OK++"; 113 114# show repository only if this subroutine returns true 115# when given the path to the project, for example: 116# sub { return -e "$_[0]/git-daemon-export-ok"; } 117our$export_auth_hook=undef; 118 119# only allow viewing of repositories also shown on the overview page 120our$strict_export="++GITWEB_STRICT_EXPORT++"; 121 122# list of git base URLs used for URL to where fetch project from, 123# i.e. full URL is "$git_base_url/$project" 124our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 125 126# default blob_plain mimetype and default charset for text/plain blob 127our$default_blob_plain_mimetype='text/plain'; 128our$default_text_plain_charset=undef; 129 130# file to use for guessing MIME types before trying /etc/mime.types 131# (relative to the current git repository) 132our$mimetypes_file=undef; 133 134# assume this charset if line contains non-UTF-8 characters; 135# it should be valid encoding (see Encoding::Supported(3pm) for list), 136# for which encoding all byte sequences are valid, for example 137# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 138# could be even 'utf-8' for the old behavior) 139our$fallback_encoding='latin1'; 140 141# rename detection options for git-diff and git-diff-tree 142# - default is '-M', with the cost proportional to 143# (number of removed files) * (number of new files). 144# - more costly is '-C' (which implies '-M'), with the cost proportional to 145# (number of changed files + number of removed files) * (number of new files) 146# - even more costly is '-C', '--find-copies-harder' with cost 147# (number of files in the original tree) * (number of new files) 148# - one might want to include '-B' option, e.g. '-B', '-M' 149our@diff_opts= ('-M');# taken from git_commit 150 151# Disables features that would allow repository owners to inject script into 152# the gitweb domain. 153our$prevent_xss=0; 154 155# information about snapshot formats that gitweb is capable of serving 156our%known_snapshot_formats= ( 157# name => { 158# 'display' => display name, 159# 'type' => mime type, 160# 'suffix' => filename suffix, 161# 'format' => --format for git-archive, 162# 'compressor' => [compressor command and arguments] 163# (array reference, optional)} 164# 165'tgz'=> { 166'display'=>'tar.gz', 167'type'=>'application/x-gzip', 168'suffix'=>'.tar.gz', 169'format'=>'tar', 170'compressor'=> ['gzip']}, 171 172'tbz2'=> { 173'display'=>'tar.bz2', 174'type'=>'application/x-bzip2', 175'suffix'=>'.tar.bz2', 176'format'=>'tar', 177'compressor'=> ['bzip2']}, 178 179'zip'=> { 180'display'=>'zip', 181'type'=>'application/x-zip', 182'suffix'=>'.zip', 183'format'=>'zip'}, 184); 185 186# Aliases so we understand old gitweb.snapshot values in repository 187# configuration. 188our%known_snapshot_format_aliases= ( 189'gzip'=>'tgz', 190'bzip2'=>'tbz2', 191 192# backward compatibility: legacy gitweb config support 193'x-gzip'=>undef,'gz'=>undef, 194'x-bzip2'=>undef,'bz2'=>undef, 195'x-zip'=>undef,''=>undef, 196); 197 198# Pixel sizes for icons and avatars. If the default font sizes or lineheights 199# are changed, it may be appropriate to change these values too via 200# $GITWEB_CONFIG. 201our%avatar_size= ( 202'default'=>16, 203'double'=>32 204); 205 206# You define site-wide feature defaults here; override them with 207# $GITWEB_CONFIG as necessary. 208our%feature= ( 209# feature => { 210# 'sub' => feature-sub (subroutine), 211# 'override' => allow-override (boolean), 212# 'default' => [ default options...] (array reference)} 213# 214# if feature is overridable (it means that allow-override has true value), 215# then feature-sub will be called with default options as parameters; 216# return value of feature-sub indicates if to enable specified feature 217# 218# if there is no 'sub' key (no feature-sub), then feature cannot be 219# overriden 220# 221# use gitweb_get_feature(<feature>) to retrieve the <feature> value 222# (an array) or gitweb_check_feature(<feature>) to check if <feature> 223# is enabled 224 225# Enable the 'blame' blob view, showing the last commit that modified 226# each line in the file. This can be very CPU-intensive. 227 228# To enable system wide have in $GITWEB_CONFIG 229# $feature{'blame'}{'default'} = [1]; 230# To have project specific config enable override in $GITWEB_CONFIG 231# $feature{'blame'}{'override'} = 1; 232# and in project config gitweb.blame = 0|1; 233'blame'=> { 234'sub'=>sub{ feature_bool('blame',@_) }, 235'override'=>0, 236'default'=> [0]}, 237 238# Enable the 'snapshot' link, providing a compressed archive of any 239# tree. This can potentially generate high traffic if you have large 240# project. 241 242# Value is a list of formats defined in %known_snapshot_formats that 243# you wish to offer. 244# To disable system wide have in $GITWEB_CONFIG 245# $feature{'snapshot'}{'default'} = []; 246# To have project specific config enable override in $GITWEB_CONFIG 247# $feature{'snapshot'}{'override'} = 1; 248# and in project config, a comma-separated list of formats or "none" 249# to disable. Example: gitweb.snapshot = tbz2,zip; 250'snapshot'=> { 251'sub'=> \&feature_snapshot, 252'override'=>0, 253'default'=> ['tgz']}, 254 255# Enable text search, which will list the commits which match author, 256# committer or commit text to a given string. Enabled by default. 257# Project specific override is not supported. 258'search'=> { 259'override'=>0, 260'default'=> [1]}, 261 262# Enable grep search, which will list the files in currently selected 263# tree containing the given string. Enabled by default. This can be 264# potentially CPU-intensive, of course. 265 266# To enable system wide have in $GITWEB_CONFIG 267# $feature{'grep'}{'default'} = [1]; 268# To have project specific config enable override in $GITWEB_CONFIG 269# $feature{'grep'}{'override'} = 1; 270# and in project config gitweb.grep = 0|1; 271'grep'=> { 272'sub'=>sub{ feature_bool('grep',@_) }, 273'override'=>0, 274'default'=> [1]}, 275 276# Enable the pickaxe search, which will list the commits that modified 277# a given string in a file. This can be practical and quite faster 278# alternative to 'blame', but still potentially CPU-intensive. 279 280# To enable system wide have in $GITWEB_CONFIG 281# $feature{'pickaxe'}{'default'} = [1]; 282# To have project specific config enable override in $GITWEB_CONFIG 283# $feature{'pickaxe'}{'override'} = 1; 284# and in project config gitweb.pickaxe = 0|1; 285'pickaxe'=> { 286'sub'=>sub{ feature_bool('pickaxe',@_) }, 287'override'=>0, 288'default'=> [1]}, 289 290# Make gitweb use an alternative format of the URLs which can be 291# more readable and natural-looking: project name is embedded 292# directly in the path and the query string contains other 293# auxiliary information. All gitweb installations recognize 294# URL in either format; this configures in which formats gitweb 295# generates links. 296 297# To enable system wide have in $GITWEB_CONFIG 298# $feature{'pathinfo'}{'default'} = [1]; 299# Project specific override is not supported. 300 301# Note that you will need to change the default location of CSS, 302# favicon, logo and possibly other files to an absolute URL. Also, 303# if gitweb.cgi serves as your indexfile, you will need to force 304# $my_uri to contain the script name in your $GITWEB_CONFIG. 305'pathinfo'=> { 306'override'=>0, 307'default'=> [0]}, 308 309# Make gitweb consider projects in project root subdirectories 310# to be forks of existing projects. Given project $projname.git, 311# projects matching $projname/*.git will not be shown in the main 312# projects list, instead a '+' mark will be added to $projname 313# there and a 'forks' view will be enabled for the project, listing 314# all the forks. If project list is taken from a file, forks have 315# to be listed after the main project. 316 317# To enable system wide have in $GITWEB_CONFIG 318# $feature{'forks'}{'default'} = [1]; 319# Project specific override is not supported. 320'forks'=> { 321'override'=>0, 322'default'=> [0]}, 323 324# Insert custom links to the action bar of all project pages. 325# This enables you mainly to link to third-party scripts integrating 326# into gitweb; e.g. git-browser for graphical history representation 327# or custom web-based repository administration interface. 328 329# The 'default' value consists of a list of triplets in the form 330# (label, link, position) where position is the label after which 331# to insert the link and link is a format string where %n expands 332# to the project name, %f to the project path within the filesystem, 333# %h to the current hash (h gitweb parameter) and %b to the current 334# hash base (hb gitweb parameter); %% expands to %. 335 336# To enable system wide have in $GITWEB_CONFIG e.g. 337# $feature{'actions'}{'default'} = [('graphiclog', 338# '/git-browser/by-commit.html?r=%n', 'summary')]; 339# Project specific override is not supported. 340'actions'=> { 341'override'=>0, 342'default'=> []}, 343 344# Allow gitweb scan project content tags described in ctags/ 345# of project repository, and display the popular Web 2.0-ish 346# "tag cloud" near the project list. Note that this is something 347# COMPLETELY different from the normal Git tags. 348 349# gitweb by itself can show existing tags, but it does not handle 350# tagging itself; you need an external application for that. 351# For an example script, check Girocco's cgi/tagproj.cgi. 352# You may want to install the HTML::TagCloud Perl module to get 353# a pretty tag cloud instead of just a list of tags. 354 355# To enable system wide have in $GITWEB_CONFIG 356# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 357# Project specific override is not supported. 358'ctags'=> { 359'override'=>0, 360'default'=> [0]}, 361 362# The maximum number of patches in a patchset generated in patch 363# view. Set this to 0 or undef to disable patch view, or to a 364# negative number to remove any limit. 365 366# To disable system wide have in $GITWEB_CONFIG 367# $feature{'patches'}{'default'} = [0]; 368# To have project specific config enable override in $GITWEB_CONFIG 369# $feature{'patches'}{'override'} = 1; 370# and in project config gitweb.patches = 0|n; 371# where n is the maximum number of patches allowed in a patchset. 372'patches'=> { 373'sub'=> \&feature_patches, 374'override'=>0, 375'default'=> [16]}, 376 377# Avatar support. When this feature is enabled, views such as 378# shortlog or commit will display an avatar associated with 379# the email of the committer(s) and/or author(s). 380 381# Currently only the gravatar provider is available, and it 382# depends on Digest::MD5. If an unknown provider is specified, 383# the feature is disabled. 384 385# To enable system wide have in $GITWEB_CONFIG 386# $feature{'avatar'}{'default'} = ['gravatar']; 387# To have project specific config enable override in $GITWEB_CONFIG 388# $feature{'avatar'}{'override'} = 1; 389# and in project config gitweb.avatar = gravatar; 390'avatar'=> { 391'sub'=> \&feature_avatar, 392'override'=>0, 393'default'=> ['']}, 394); 395 396sub gitweb_get_feature { 397my($name) =@_; 398return unlessexists$feature{$name}; 399my($sub,$override,@defaults) = ( 400$feature{$name}{'sub'}, 401$feature{$name}{'override'}, 402@{$feature{$name}{'default'}}); 403if(!$override) {return@defaults; } 404if(!defined$sub) { 405warn"feature$nameis not overrideable"; 406return@defaults; 407} 408return$sub->(@defaults); 409} 410 411# A wrapper to check if a given feature is enabled. 412# With this, you can say 413# 414# my $bool_feat = gitweb_check_feature('bool_feat'); 415# gitweb_check_feature('bool_feat') or somecode; 416# 417# instead of 418# 419# my ($bool_feat) = gitweb_get_feature('bool_feat'); 420# (gitweb_get_feature('bool_feat'))[0] or somecode; 421# 422sub gitweb_check_feature { 423return(gitweb_get_feature(@_))[0]; 424} 425 426 427sub feature_bool { 428my$key=shift; 429my($val) = git_get_project_config($key,'--bool'); 430 431if(!defined$val) { 432return($_[0]); 433}elsif($valeq'true') { 434return(1); 435}elsif($valeq'false') { 436return(0); 437} 438} 439 440sub feature_snapshot { 441my(@fmts) =@_; 442 443my($val) = git_get_project_config('snapshot'); 444 445if($val) { 446@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 447} 448 449return@fmts; 450} 451 452sub feature_patches { 453my@val= (git_get_project_config('patches','--int')); 454 455if(@val) { 456return@val; 457} 458 459return($_[0]); 460} 461 462sub feature_avatar { 463my@val= (git_get_project_config('avatar')); 464 465return@val?@val:@_; 466} 467 468# checking HEAD file with -e is fragile if the repository was 469# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 470# and then pruned. 471sub check_head_link { 472my($dir) =@_; 473my$headfile="$dir/HEAD"; 474return((-e $headfile) || 475(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 476} 477 478sub check_export_ok { 479my($dir) =@_; 480return(check_head_link($dir) && 481(!$export_ok|| -e "$dir/$export_ok") && 482(!$export_auth_hook||$export_auth_hook->($dir))); 483} 484 485# process alternate names for backward compatibility 486# filter out unsupported (unknown) snapshot formats 487sub filter_snapshot_fmts { 488my@fmts=@_; 489 490@fmts=map{ 491exists$known_snapshot_format_aliases{$_} ? 492$known_snapshot_format_aliases{$_} :$_}@fmts; 493@fmts=grep{ 494exists$known_snapshot_formats{$_} }@fmts; 495} 496 497our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 498if(-e $GITWEB_CONFIG) { 499do$GITWEB_CONFIG; 500}else{ 501our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 502do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 503} 504 505# version of the core git binary 506our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 507 508$projects_list||=$projectroot; 509 510# ====================================================================== 511# input validation and dispatch 512 513# input parameters can be collected from a variety of sources (presently, CGI 514# and PATH_INFO), so we define an %input_params hash that collects them all 515# together during validation: this allows subsequent uses (e.g. href()) to be 516# agnostic of the parameter origin 517 518our%input_params= (); 519 520# input parameters are stored with the long parameter name as key. This will 521# also be used in the href subroutine to convert parameters to their CGI 522# equivalent, and since the href() usage is the most frequent one, we store 523# the name -> CGI key mapping here, instead of the reverse. 524# 525# XXX: Warning: If you touch this, check the search form for updating, 526# too. 527 528our@cgi_param_mapping= ( 529 project =>"p", 530 action =>"a", 531 file_name =>"f", 532 file_parent =>"fp", 533 hash =>"h", 534 hash_parent =>"hp", 535 hash_base =>"hb", 536 hash_parent_base =>"hpb", 537 page =>"pg", 538 order =>"o", 539 searchtext =>"s", 540 searchtype =>"st", 541 snapshot_format =>"sf", 542 extra_options =>"opt", 543 search_use_regexp =>"sr", 544); 545our%cgi_param_mapping=@cgi_param_mapping; 546 547# we will also need to know the possible actions, for validation 548our%actions= ( 549"blame"=> \&git_blame, 550"blobdiff"=> \&git_blobdiff, 551"blobdiff_plain"=> \&git_blobdiff_plain, 552"blob"=> \&git_blob, 553"blob_plain"=> \&git_blob_plain, 554"commitdiff"=> \&git_commitdiff, 555"commitdiff_plain"=> \&git_commitdiff_plain, 556"commit"=> \&git_commit, 557"forks"=> \&git_forks, 558"heads"=> \&git_heads, 559"history"=> \&git_history, 560"log"=> \&git_log, 561"patch"=> \&git_patch, 562"patches"=> \&git_patches, 563"rss"=> \&git_rss, 564"atom"=> \&git_atom, 565"search"=> \&git_search, 566"search_help"=> \&git_search_help, 567"shortlog"=> \&git_shortlog, 568"summary"=> \&git_summary, 569"tag"=> \&git_tag, 570"tags"=> \&git_tags, 571"tree"=> \&git_tree, 572"snapshot"=> \&git_snapshot, 573"object"=> \&git_object, 574# those below don't need $project 575"opml"=> \&git_opml, 576"project_list"=> \&git_project_list, 577"project_index"=> \&git_project_index, 578); 579 580# finally, we have the hash of allowed extra_options for the commands that 581# allow them 582our%allowed_options= ( 583"--no-merges"=> [qw(rss atom log shortlog history)], 584); 585 586# fill %input_params with the CGI parameters. All values except for 'opt' 587# should be single values, but opt can be an array. We should probably 588# build an array of parameters that can be multi-valued, but since for the time 589# being it's only this one, we just single it out 590while(my($name,$symbol) =each%cgi_param_mapping) { 591if($symboleq'opt') { 592$input_params{$name} = [$cgi->param($symbol) ]; 593}else{ 594$input_params{$name} =$cgi->param($symbol); 595} 596} 597 598# now read PATH_INFO and update the parameter list for missing parameters 599sub evaluate_path_info { 600return ifdefined$input_params{'project'}; 601return if!$path_info; 602$path_info=~ s,^/+,,; 603return if!$path_info; 604 605# find which part of PATH_INFO is project 606my$project=$path_info; 607$project=~ s,/+$,,; 608while($project&& !check_head_link("$projectroot/$project")) { 609$project=~ s,/*[^/]*$,,; 610} 611return unless$project; 612$input_params{'project'} =$project; 613 614# do not change any parameters if an action is given using the query string 615return if$input_params{'action'}; 616$path_info=~ s,^\Q$project\E/*,,; 617 618# next, check if we have an action 619my$action=$path_info; 620$action=~ s,/.*$,,; 621if(exists$actions{$action}) { 622$path_info=~ s,^$action/*,,; 623$input_params{'action'} =$action; 624} 625 626# list of actions that want hash_base instead of hash, but can have no 627# pathname (f) parameter 628my@wants_base= ( 629'tree', 630'history', 631); 632 633# we want to catch 634# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 635my($parentrefname,$parentpathname,$refname,$pathname) = 636($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 637 638# first, analyze the 'current' part 639if(defined$pathname) { 640# we got "branch:filename" or "branch:dir/" 641# we could use git_get_type(branch:pathname), but: 642# - it needs $git_dir 643# - it does a git() call 644# - the convention of terminating directories with a slash 645# makes it superfluous 646# - embedding the action in the PATH_INFO would make it even 647# more superfluous 648$pathname=~ s,^/+,,; 649if(!$pathname||substr($pathname, -1)eq"/") { 650$input_params{'action'} ||="tree"; 651$pathname=~ s,/$,,; 652}else{ 653# the default action depends on whether we had parent info 654# or not 655if($parentrefname) { 656$input_params{'action'} ||="blobdiff_plain"; 657}else{ 658$input_params{'action'} ||="blob_plain"; 659} 660} 661$input_params{'hash_base'} ||=$refname; 662$input_params{'file_name'} ||=$pathname; 663}elsif(defined$refname) { 664# we got "branch". In this case we have to choose if we have to 665# set hash or hash_base. 666# 667# Most of the actions without a pathname only want hash to be 668# set, except for the ones specified in @wants_base that want 669# hash_base instead. It should also be noted that hand-crafted 670# links having 'history' as an action and no pathname or hash 671# set will fail, but that happens regardless of PATH_INFO. 672$input_params{'action'} ||="shortlog"; 673if(grep{$_eq$input_params{'action'} }@wants_base) { 674$input_params{'hash_base'} ||=$refname; 675}else{ 676$input_params{'hash'} ||=$refname; 677} 678} 679 680# next, handle the 'parent' part, if present 681if(defined$parentrefname) { 682# a missing pathspec defaults to the 'current' filename, allowing e.g. 683# someproject/blobdiff/oldrev..newrev:/filename 684if($parentpathname) { 685$parentpathname=~ s,^/+,,; 686$parentpathname=~ s,/$,,; 687$input_params{'file_parent'} ||=$parentpathname; 688}else{ 689$input_params{'file_parent'} ||=$input_params{'file_name'}; 690} 691# we assume that hash_parent_base is wanted if a path was specified, 692# or if the action wants hash_base instead of hash 693if(defined$input_params{'file_parent'} || 694grep{$_eq$input_params{'action'} }@wants_base) { 695$input_params{'hash_parent_base'} ||=$parentrefname; 696}else{ 697$input_params{'hash_parent'} ||=$parentrefname; 698} 699} 700 701# for the snapshot action, we allow URLs in the form 702# $project/snapshot/$hash.ext 703# where .ext determines the snapshot and gets removed from the 704# passed $refname to provide the $hash. 705# 706# To be able to tell that $refname includes the format extension, we 707# require the following two conditions to be satisfied: 708# - the hash input parameter MUST have been set from the $refname part 709# of the URL (i.e. they must be equal) 710# - the snapshot format MUST NOT have been defined already (e.g. from 711# CGI parameter sf) 712# It's also useless to try any matching unless $refname has a dot, 713# so we check for that too 714if(defined$input_params{'action'} && 715$input_params{'action'}eq'snapshot'&& 716defined$refname&&index($refname,'.') != -1&& 717$refnameeq$input_params{'hash'} && 718!defined$input_params{'snapshot_format'}) { 719# We loop over the known snapshot formats, checking for 720# extensions. Allowed extensions are both the defined suffix 721# (which includes the initial dot already) and the snapshot 722# format key itself, with a prepended dot 723while(my($fmt,$opt) =each%known_snapshot_formats) { 724my$hash=$refname; 725unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 726next; 727} 728my$sfx=$1; 729# a valid suffix was found, so set the snapshot format 730# and reset the hash parameter 731$input_params{'snapshot_format'} =$fmt; 732$input_params{'hash'} =$hash; 733# we also set the format suffix to the one requested 734# in the URL: this way a request for e.g. .tgz returns 735# a .tgz instead of a .tar.gz 736$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 737last; 738} 739} 740} 741evaluate_path_info(); 742 743our$action=$input_params{'action'}; 744if(defined$action) { 745if(!validate_action($action)) { 746 die_error(400,"Invalid action parameter"); 747} 748} 749 750# parameters which are pathnames 751our$project=$input_params{'project'}; 752if(defined$project) { 753if(!validate_project($project)) { 754undef$project; 755 die_error(404,"No such project"); 756} 757} 758 759our$file_name=$input_params{'file_name'}; 760if(defined$file_name) { 761if(!validate_pathname($file_name)) { 762 die_error(400,"Invalid file parameter"); 763} 764} 765 766our$file_parent=$input_params{'file_parent'}; 767if(defined$file_parent) { 768if(!validate_pathname($file_parent)) { 769 die_error(400,"Invalid file parent parameter"); 770} 771} 772 773# parameters which are refnames 774our$hash=$input_params{'hash'}; 775if(defined$hash) { 776if(!validate_refname($hash)) { 777 die_error(400,"Invalid hash parameter"); 778} 779} 780 781our$hash_parent=$input_params{'hash_parent'}; 782if(defined$hash_parent) { 783if(!validate_refname($hash_parent)) { 784 die_error(400,"Invalid hash parent parameter"); 785} 786} 787 788our$hash_base=$input_params{'hash_base'}; 789if(defined$hash_base) { 790if(!validate_refname($hash_base)) { 791 die_error(400,"Invalid hash base parameter"); 792} 793} 794 795our@extra_options= @{$input_params{'extra_options'}}; 796# @extra_options is always defined, since it can only be (currently) set from 797# CGI, and $cgi->param() returns the empty array in array context if the param 798# is not set 799foreachmy$opt(@extra_options) { 800if(not exists$allowed_options{$opt}) { 801 die_error(400,"Invalid option parameter"); 802} 803if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 804 die_error(400,"Invalid option parameter for this action"); 805} 806} 807 808our$hash_parent_base=$input_params{'hash_parent_base'}; 809if(defined$hash_parent_base) { 810if(!validate_refname($hash_parent_base)) { 811 die_error(400,"Invalid hash parent base parameter"); 812} 813} 814 815# other parameters 816our$page=$input_params{'page'}; 817if(defined$page) { 818if($page=~m/[^0-9]/) { 819 die_error(400,"Invalid page parameter"); 820} 821} 822 823our$searchtype=$input_params{'searchtype'}; 824if(defined$searchtype) { 825if($searchtype=~m/[^a-z]/) { 826 die_error(400,"Invalid searchtype parameter"); 827} 828} 829 830our$search_use_regexp=$input_params{'search_use_regexp'}; 831 832our$searchtext=$input_params{'searchtext'}; 833our$search_regexp; 834if(defined$searchtext) { 835if(length($searchtext) <2) { 836 die_error(403,"At least two characters are required for search parameter"); 837} 838$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 839} 840 841# path to the current git repository 842our$git_dir; 843$git_dir="$projectroot/$project"if$project; 844 845# list of supported snapshot formats 846our@snapshot_fmts= gitweb_get_feature('snapshot'); 847@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 848 849# check that the avatar feature is set to a known provider name, 850# and for each provider check if the dependencies are satisfied. 851# if the provider name is invalid or the dependencies are not met, 852# reset $git_avatar to the empty string. 853our($git_avatar) = gitweb_get_feature('avatar'); 854if($git_avatareq'gravatar') { 855$git_avatar=''unless(eval{require Digest::MD5;1; }); 856}else{ 857$git_avatar=''; 858} 859 860# dispatch 861if(!defined$action) { 862if(defined$hash) { 863$action= git_get_type($hash); 864}elsif(defined$hash_base&&defined$file_name) { 865$action= git_get_type("$hash_base:$file_name"); 866}elsif(defined$project) { 867$action='summary'; 868}else{ 869$action='project_list'; 870} 871} 872if(!defined($actions{$action})) { 873 die_error(400,"Unknown action"); 874} 875if($action!~m/^(?:opml|project_list|project_index)$/&& 876!$project) { 877 die_error(400,"Project needed"); 878} 879$actions{$action}->(); 880exit; 881 882## ====================================================================== 883## action links 884 885sub href { 886my%params=@_; 887# default is to use -absolute url() i.e. $my_uri 888my$href=$params{-full} ?$my_url:$my_uri; 889 890$params{'project'} =$projectunlessexists$params{'project'}; 891 892if($params{-replay}) { 893while(my($name,$symbol) =each%cgi_param_mapping) { 894if(!exists$params{$name}) { 895$params{$name} =$input_params{$name}; 896} 897} 898} 899 900my$use_pathinfo= gitweb_check_feature('pathinfo'); 901if($use_pathinfoand defined$params{'project'}) { 902# try to put as many parameters as possible in PATH_INFO: 903# - project name 904# - action 905# - hash_parent or hash_parent_base:/file_parent 906# - hash or hash_base:/filename 907# - the snapshot_format as an appropriate suffix 908 909# When the script is the root DirectoryIndex for the domain, 910# $href here would be something like http://gitweb.example.com/ 911# Thus, we strip any trailing / from $href, to spare us double 912# slashes in the final URL 913$href=~ s,/$,,; 914 915# Then add the project name, if present 916$href.="/".esc_url($params{'project'}); 917delete$params{'project'}; 918 919# since we destructively absorb parameters, we keep this 920# boolean that remembers if we're handling a snapshot 921my$is_snapshot=$params{'action'}eq'snapshot'; 922 923# Summary just uses the project path URL, any other action is 924# added to the URL 925if(defined$params{'action'}) { 926$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 927delete$params{'action'}; 928} 929 930# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 931# stripping nonexistent or useless pieces 932$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 933||$params{'hash_parent'} ||$params{'hash'}); 934if(defined$params{'hash_base'}) { 935if(defined$params{'hash_parent_base'}) { 936$href.= esc_url($params{'hash_parent_base'}); 937# skip the file_parent if it's the same as the file_name 938delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 939if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 940$href.=":/".esc_url($params{'file_parent'}); 941delete$params{'file_parent'}; 942} 943$href.=".."; 944delete$params{'hash_parent'}; 945delete$params{'hash_parent_base'}; 946}elsif(defined$params{'hash_parent'}) { 947$href.= esc_url($params{'hash_parent'}).".."; 948delete$params{'hash_parent'}; 949} 950 951$href.= esc_url($params{'hash_base'}); 952if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 953$href.=":/".esc_url($params{'file_name'}); 954delete$params{'file_name'}; 955} 956delete$params{'hash'}; 957delete$params{'hash_base'}; 958}elsif(defined$params{'hash'}) { 959$href.= esc_url($params{'hash'}); 960delete$params{'hash'}; 961} 962 963# If the action was a snapshot, we can absorb the 964# snapshot_format parameter too 965if($is_snapshot) { 966my$fmt=$params{'snapshot_format'}; 967# snapshot_format should always be defined when href() 968# is called, but just in case some code forgets, we 969# fall back to the default 970$fmt||=$snapshot_fmts[0]; 971$href.=$known_snapshot_formats{$fmt}{'suffix'}; 972delete$params{'snapshot_format'}; 973} 974} 975 976# now encode the parameters explicitly 977my@result= (); 978for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 979my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 980if(defined$params{$name}) { 981if(ref($params{$name})eq"ARRAY") { 982foreachmy$par(@{$params{$name}}) { 983push@result,$symbol."=". esc_param($par); 984} 985}else{ 986push@result,$symbol."=". esc_param($params{$name}); 987} 988} 989} 990$href.="?".join(';',@result)ifscalar@result; 991 992return$href; 993} 994 995 996## ====================================================================== 997## validation, quoting/unquoting and escaping 998 999sub validate_action {1000my$input=shift||returnundef;1001returnundefunlessexists$actions{$input};1002return$input;1003}10041005sub validate_project {1006my$input=shift||returnundef;1007if(!validate_pathname($input) ||1008!(-d "$projectroot/$input") ||1009!check_export_ok("$projectroot/$input") ||1010($strict_export&& !project_in_list($input))) {1011returnundef;1012}else{1013return$input;1014}1015}10161017sub validate_pathname {1018my$input=shift||returnundef;10191020# no '.' or '..' as elements of path, i.e. no '.' nor '..'1021# at the beginning, at the end, and between slashes.1022# also this catches doubled slashes1023if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1024returnundef;1025}1026# no null characters1027if($input=~m!\0!) {1028returnundef;1029}1030return$input;1031}10321033sub validate_refname {1034my$input=shift||returnundef;10351036# textual hashes are O.K.1037if($input=~m/^[0-9a-fA-F]{40}$/) {1038return$input;1039}1040# it must be correct pathname1041$input= validate_pathname($input)1042orreturnundef;1043# restrictions on ref name according to git-check-ref-format1044if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1045returnundef;1046}1047return$input;1048}10491050# decode sequences of octets in utf8 into Perl's internal form,1051# which is utf-8 with utf8 flag set if needed. gitweb writes out1052# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1053sub to_utf8 {1054my$str=shift;1055if(utf8::valid($str)) {1056 utf8::decode($str);1057return$str;1058}else{1059return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1060}1061}10621063# quote unsafe chars, but keep the slash, even when it's not1064# correct, but quoted slashes look too horrible in bookmarks1065sub esc_param {1066my$str=shift;1067$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg;1068$str=~s/\+/%2B/g;1069$str=~s/ /\+/g;1070return$str;1071}10721073# quote unsafe chars in whole URL, so some charactrs cannot be quoted1074sub esc_url {1075my$str=shift;1076$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1077$str=~s/\+/%2B/g;1078$str=~s/ /\+/g;1079return$str;1080}10811082# replace invalid utf8 character with SUBSTITUTION sequence1083sub esc_html {1084my$str=shift;1085my%opts=@_;10861087$str= to_utf8($str);1088$str=$cgi->escapeHTML($str);1089if($opts{'-nbsp'}) {1090$str=~s/ / /g;1091}1092$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1093return$str;1094}10951096# quote control characters and escape filename to HTML1097sub esc_path {1098my$str=shift;1099my%opts=@_;11001101$str= to_utf8($str);1102$str=$cgi->escapeHTML($str);1103if($opts{'-nbsp'}) {1104$str=~s/ / /g;1105}1106$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1107return$str;1108}11091110# Make control characters "printable", using character escape codes (CEC)1111sub quot_cec {1112my$cntrl=shift;1113my%opts=@_;1114my%es= (# character escape codes, aka escape sequences1115"\t"=>'\t',# tab (HT)1116"\n"=>'\n',# line feed (LF)1117"\r"=>'\r',# carrige return (CR)1118"\f"=>'\f',# form feed (FF)1119"\b"=>'\b',# backspace (BS)1120"\a"=>'\a',# alarm (bell) (BEL)1121"\e"=>'\e',# escape (ESC)1122"\013"=>'\v',# vertical tab (VT)1123"\000"=>'\0',# nul character (NUL)1124);1125my$chr= ( (exists$es{$cntrl})1126?$es{$cntrl}1127:sprintf('\%2x',ord($cntrl)) );1128if($opts{-nohtml}) {1129return$chr;1130}else{1131return"<span class=\"cntrl\">$chr</span>";1132}1133}11341135# Alternatively use unicode control pictures codepoints,1136# Unicode "printable representation" (PR)1137sub quot_upr {1138my$cntrl=shift;1139my%opts=@_;11401141my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1142if($opts{-nohtml}) {1143return$chr;1144}else{1145return"<span class=\"cntrl\">$chr</span>";1146}1147}11481149# git may return quoted and escaped filenames1150sub unquote {1151my$str=shift;11521153sub unq {1154my$seq=shift;1155my%es= (# character escape codes, aka escape sequences1156't'=>"\t",# tab (HT, TAB)1157'n'=>"\n",# newline (NL)1158'r'=>"\r",# return (CR)1159'f'=>"\f",# form feed (FF)1160'b'=>"\b",# backspace (BS)1161'a'=>"\a",# alarm (bell) (BEL)1162'e'=>"\e",# escape (ESC)1163'v'=>"\013",# vertical tab (VT)1164);11651166if($seq=~m/^[0-7]{1,3}$/) {1167# octal char sequence1168returnchr(oct($seq));1169}elsif(exists$es{$seq}) {1170# C escape sequence, aka character escape code1171return$es{$seq};1172}1173# quoted ordinary character1174return$seq;1175}11761177if($str=~m/^"(.*)"$/) {1178# needs unquoting1179$str=$1;1180$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1181}1182return$str;1183}11841185# escape tabs (convert tabs to spaces)1186sub untabify {1187my$line=shift;11881189while((my$pos=index($line,"\t")) != -1) {1190if(my$count= (8- ($pos%8))) {1191my$spaces=' ' x $count;1192$line=~s/\t/$spaces/;1193}1194}11951196return$line;1197}11981199sub project_in_list {1200my$project=shift;1201my@list= git_get_projects_list();1202return@list&&scalar(grep{$_->{'path'}eq$project}@list);1203}12041205## ----------------------------------------------------------------------1206## HTML aware string manipulation12071208# Try to chop given string on a word boundary between position1209# $len and $len+$add_len. If there is no word boundary there,1210# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1211# (marking chopped part) would be longer than given string.1212sub chop_str {1213my$str=shift;1214my$len=shift;1215my$add_len=shift||10;1216my$where=shift||'right';# 'left' | 'center' | 'right'12171218# Make sure perl knows it is utf8 encoded so we don't1219# cut in the middle of a utf8 multibyte char.1220$str= to_utf8($str);12211222# allow only $len chars, but don't cut a word if it would fit in $add_len1223# if it doesn't fit, cut it if it's still longer than the dots we would add1224# remove chopped character entities entirely12251226# when chopping in the middle, distribute $len into left and right part1227# return early if chopping wouldn't make string shorter1228if($whereeq'center') {1229return$strif($len+5>=length($str));# filler is length 51230$len=int($len/2);1231}else{1232return$strif($len+4>=length($str));# filler is length 41233}12341235# regexps: ending and beginning with word part up to $add_len1236my$endre=qr/.{$len}\w{0,$add_len}/;1237my$begre=qr/\w{0,$add_len}.{$len}/;12381239if($whereeq'left') {1240$str=~m/^(.*?)($begre)$/;1241my($lead,$body) = ($1,$2);1242if(length($lead) >4) {1243$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1244$lead=" ...";1245}1246return"$lead$body";12471248}elsif($whereeq'center') {1249$str=~m/^($endre)(.*)$/;1250my($left,$str) = ($1,$2);1251$str=~m/^(.*?)($begre)$/;1252my($mid,$right) = ($1,$2);1253if(length($mid) >5) {1254$left=~s/&[^;]*$//;1255$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1256$mid=" ... ";1257}1258return"$left$mid$right";12591260}else{1261$str=~m/^($endre)(.*)$/;1262my$body=$1;1263my$tail=$2;1264if(length($tail) >4) {1265$body=~s/&[^;]*$//;1266$tail="... ";1267}1268return"$body$tail";1269}1270}12711272# takes the same arguments as chop_str, but also wraps a <span> around the1273# result with a title attribute if it does get chopped. Additionally, the1274# string is HTML-escaped.1275sub chop_and_escape_str {1276my($str) =@_;12771278my$chopped= chop_str(@_);1279if($choppedeq$str) {1280return esc_html($chopped);1281}else{1282$str=~s/[[:cntrl:]]/?/g;1283return$cgi->span({-title=>$str}, esc_html($chopped));1284}1285}12861287## ----------------------------------------------------------------------1288## functions returning short strings12891290# CSS class for given age value (in seconds)1291sub age_class {1292my$age=shift;12931294if(!defined$age) {1295return"noage";1296}elsif($age<60*60*2) {1297return"age0";1298}elsif($age<60*60*24*2) {1299return"age1";1300}else{1301return"age2";1302}1303}13041305# convert age in seconds to "nn units ago" string1306sub age_string {1307my$age=shift;1308my$age_str;13091310if($age>60*60*24*365*2) {1311$age_str= (int$age/60/60/24/365);1312$age_str.=" years ago";1313}elsif($age>60*60*24*(365/12)*2) {1314$age_str=int$age/60/60/24/(365/12);1315$age_str.=" months ago";1316}elsif($age>60*60*24*7*2) {1317$age_str=int$age/60/60/24/7;1318$age_str.=" weeks ago";1319}elsif($age>60*60*24*2) {1320$age_str=int$age/60/60/24;1321$age_str.=" days ago";1322}elsif($age>60*60*2) {1323$age_str=int$age/60/60;1324$age_str.=" hours ago";1325}elsif($age>60*2) {1326$age_str=int$age/60;1327$age_str.=" min ago";1328}elsif($age>2) {1329$age_str=int$age;1330$age_str.=" sec ago";1331}else{1332$age_str.=" right now";1333}1334return$age_str;1335}13361337useconstant{1338 S_IFINVALID =>0030000,1339 S_IFGITLINK =>0160000,1340};13411342# submodule/subproject, a commit object reference1343sub S_ISGITLINK {1344my$mode=shift;13451346return(($mode& S_IFMT) == S_IFGITLINK)1347}13481349# convert file mode in octal to symbolic file mode string1350sub mode_str {1351my$mode=oct shift;13521353if(S_ISGITLINK($mode)) {1354return'm---------';1355}elsif(S_ISDIR($mode& S_IFMT)) {1356return'drwxr-xr-x';1357}elsif(S_ISLNK($mode)) {1358return'lrwxrwxrwx';1359}elsif(S_ISREG($mode)) {1360# git cares only about the executable bit1361if($mode& S_IXUSR) {1362return'-rwxr-xr-x';1363}else{1364return'-rw-r--r--';1365};1366}else{1367return'----------';1368}1369}13701371# convert file mode in octal to file type string1372sub file_type {1373my$mode=shift;13741375if($mode!~m/^[0-7]+$/) {1376return$mode;1377}else{1378$mode=oct$mode;1379}13801381if(S_ISGITLINK($mode)) {1382return"submodule";1383}elsif(S_ISDIR($mode& S_IFMT)) {1384return"directory";1385}elsif(S_ISLNK($mode)) {1386return"symlink";1387}elsif(S_ISREG($mode)) {1388return"file";1389}else{1390return"unknown";1391}1392}13931394# convert file mode in octal to file type description string1395sub file_type_long {1396my$mode=shift;13971398if($mode!~m/^[0-7]+$/) {1399return$mode;1400}else{1401$mode=oct$mode;1402}14031404if(S_ISGITLINK($mode)) {1405return"submodule";1406}elsif(S_ISDIR($mode& S_IFMT)) {1407return"directory";1408}elsif(S_ISLNK($mode)) {1409return"symlink";1410}elsif(S_ISREG($mode)) {1411if($mode& S_IXUSR) {1412return"executable";1413}else{1414return"file";1415};1416}else{1417return"unknown";1418}1419}142014211422## ----------------------------------------------------------------------1423## functions returning short HTML fragments, or transforming HTML fragments1424## which don't belong to other sections14251426# format line of commit message.1427sub format_log_line_html {1428my$line=shift;14291430$line= esc_html($line, -nbsp=>1);1431$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1432$cgi->a({-href => href(action=>"object", hash=>$1),1433-class=>"text"},$1);1434}eg;14351436return$line;1437}14381439# format marker of refs pointing to given object14401441# the destination action is chosen based on object type and current context:1442# - for annotated tags, we choose the tag view unless it's the current view1443# already, in which case we go to shortlog view1444# - for other refs, we keep the current view if we're in history, shortlog or1445# log view, and select shortlog otherwise1446sub format_ref_marker {1447my($refs,$id) =@_;1448my$markers='';14491450if(defined$refs->{$id}) {1451foreachmy$ref(@{$refs->{$id}}) {1452# this code exploits the fact that non-lightweight tags are the1453# only indirect objects, and that they are the only objects for which1454# we want to use tag instead of shortlog as action1455my($type,$name) =qw();1456my$indirect= ($ref=~s/\^\{\}$//);1457# e.g. tags/v2.6.11 or heads/next1458if($ref=~m!^(.*?)s?/(.*)$!) {1459$type=$1;1460$name=$2;1461}else{1462$type="ref";1463$name=$ref;1464}14651466my$class=$type;1467$class.=" indirect"if$indirect;14681469my$dest_action="shortlog";14701471if($indirect) {1472$dest_action="tag"unless$actioneq"tag";1473}elsif($action=~/^(history|(short)?log)$/) {1474$dest_action=$action;1475}14761477my$dest="";1478$dest.="refs/"unless$ref=~ m!^refs/!;1479$dest.=$ref;14801481my$link=$cgi->a({1482-href => href(1483 action=>$dest_action,1484 hash=>$dest1485)},$name);14861487$markers.=" <span class=\"$class\"title=\"$ref\">".1488$link."</span>";1489}1490}14911492if($markers) {1493return' <span class="refs">'.$markers.'</span>';1494}else{1495return"";1496}1497}14981499# format, perhaps shortened and with markers, title line1500sub format_subject_html {1501my($long,$short,$href,$extra) =@_;1502$extra=''unlessdefined($extra);15031504if(length($short) <length($long)) {1505$long=~s/[[:cntrl:]]/?/g;1506return$cgi->a({-href =>$href, -class=>"list subject",1507-title => to_utf8($long)},1508 esc_html($short) .$extra);1509}else{1510return$cgi->a({-href =>$href, -class=>"list subject"},1511 esc_html($long) .$extra);1512}1513}15141515# Insert an avatar for the given $email at the given $size if the feature1516# is enabled.1517sub git_get_avatar {1518my($email,%opts) =@_;1519my$pre_white= ($opts{-pad_before} ?" ":"");1520my$post_white= ($opts{-pad_after} ?" ":"");1521$opts{-size} ||='default';1522my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1523my$url="";1524if($git_avatareq'gravatar') {1525$url="http://www.gravatar.com/avatar/".1526 Digest::MD5::md5_hex(lc$email) ."?s=$size";1527}1528# Currently only gravatars are supported, but other forms such as1529# picons can be added by putting an else up here and defining $url1530# as needed. If no variant puts something in $url, we assume avatars1531# are completely disabled/unavailable.1532if($url) {1533return$pre_white.1534"<img width=\"$size\"".1535"class=\"avatar\"".1536"src=\"$url\"".1537"/>".$post_white;1538}else{1539return"";1540}1541}15421543# format the author name of the given commit with the given tag1544# the author name is chopped and escaped according to the other1545# optional parameters (see chop_str).1546sub format_author_html {1547my$tag=shift;1548my$co=shift;1549my$author= chop_and_escape_str($co->{'author_name'},@_);1550return"<$tagclass=\"author\">".1551 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1552$author."</$tag>";1553}15541555# format git diff header line, i.e. "diff --(git|combined|cc) ..."1556sub format_git_diff_header_line {1557my$line=shift;1558my$diffinfo=shift;1559my($from,$to) =@_;15601561if($diffinfo->{'nparents'}) {1562# combined diff1563$line=~s!^(diff (.*?) )"?.*$!$1!;1564if($to->{'href'}) {1565$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1566 esc_path($to->{'file'}));1567}else{# file was deleted (no href)1568$line.= esc_path($to->{'file'});1569}1570}else{1571# "ordinary" diff1572$line=~s!^(diff (.*?) )"?a/.*$!$1!;1573if($from->{'href'}) {1574$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1575'a/'. esc_path($from->{'file'}));1576}else{# file was added (no href)1577$line.='a/'. esc_path($from->{'file'});1578}1579$line.=' ';1580if($to->{'href'}) {1581$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1582'b/'. esc_path($to->{'file'}));1583}else{# file was deleted1584$line.='b/'. esc_path($to->{'file'});1585}1586}15871588return"<div class=\"diff header\">$line</div>\n";1589}15901591# format extended diff header line, before patch itself1592sub format_extended_diff_header_line {1593my$line=shift;1594my$diffinfo=shift;1595my($from,$to) =@_;15961597# match <path>1598if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1599$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1600 esc_path($from->{'file'}));1601}1602if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1603$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1604 esc_path($to->{'file'}));1605}1606# match single <mode>1607if($line=~m/\s(\d{6})$/) {1608$line.='<span class="info"> ('.1609 file_type_long($1) .1610')</span>';1611}1612# match <hash>1613if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1614# can match only for combined diff1615$line='index ';1616for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1617if($from->{'href'}[$i]) {1618$line.=$cgi->a({-href=>$from->{'href'}[$i],1619-class=>"hash"},1620substr($diffinfo->{'from_id'}[$i],0,7));1621}else{1622$line.='0' x 7;1623}1624# separator1625$line.=','if($i<$diffinfo->{'nparents'} -1);1626}1627$line.='..';1628if($to->{'href'}) {1629$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1630substr($diffinfo->{'to_id'},0,7));1631}else{1632$line.='0' x 7;1633}16341635}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1636# can match only for ordinary diff1637my($from_link,$to_link);1638if($from->{'href'}) {1639$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1640substr($diffinfo->{'from_id'},0,7));1641}else{1642$from_link='0' x 7;1643}1644if($to->{'href'}) {1645$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1646substr($diffinfo->{'to_id'},0,7));1647}else{1648$to_link='0' x 7;1649}1650my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1651$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1652}16531654return$line."<br/>\n";1655}16561657# format from-file/to-file diff header1658sub format_diff_from_to_header {1659my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1660my$line;1661my$result='';16621663$line=$from_line;1664#assert($line =~ m/^---/) if DEBUG;1665# no extra formatting for "^--- /dev/null"1666if(!$diffinfo->{'nparents'}) {1667# ordinary (single parent) diff1668if($line=~m!^--- "?a/!) {1669if($from->{'href'}) {1670$line='--- a/'.1671$cgi->a({-href=>$from->{'href'}, -class=>"path"},1672 esc_path($from->{'file'}));1673}else{1674$line='--- a/'.1675 esc_path($from->{'file'});1676}1677}1678$result.= qq!<div class="diff from_file">$line</div>\n!;16791680}else{1681# combined diff (merge commit)1682for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1683if($from->{'href'}[$i]) {1684$line='--- '.1685$cgi->a({-href=>href(action=>"blobdiff",1686 hash_parent=>$diffinfo->{'from_id'}[$i],1687 hash_parent_base=>$parents[$i],1688 file_parent=>$from->{'file'}[$i],1689 hash=>$diffinfo->{'to_id'},1690 hash_base=>$hash,1691 file_name=>$to->{'file'}),1692-class=>"path",1693-title=>"diff". ($i+1)},1694$i+1) .1695'/'.1696$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1697 esc_path($from->{'file'}[$i]));1698}else{1699$line='--- /dev/null';1700}1701$result.= qq!<div class="diff from_file">$line</div>\n!;1702}1703}17041705$line=$to_line;1706#assert($line =~ m/^\+\+\+/) if DEBUG;1707# no extra formatting for "^+++ /dev/null"1708if($line=~m!^\+\+\+ "?b/!) {1709if($to->{'href'}) {1710$line='+++ b/'.1711$cgi->a({-href=>$to->{'href'}, -class=>"path"},1712 esc_path($to->{'file'}));1713}else{1714$line='+++ b/'.1715 esc_path($to->{'file'});1716}1717}1718$result.= qq!<div class="diff to_file">$line</div>\n!;17191720return$result;1721}17221723# create note for patch simplified by combined diff1724sub format_diff_cc_simplified {1725my($diffinfo,@parents) =@_;1726my$result='';17271728$result.="<div class=\"diff header\">".1729"diff --cc ";1730if(!is_deleted($diffinfo)) {1731$result.=$cgi->a({-href => href(action=>"blob",1732 hash_base=>$hash,1733 hash=>$diffinfo->{'to_id'},1734 file_name=>$diffinfo->{'to_file'}),1735-class=>"path"},1736 esc_path($diffinfo->{'to_file'}));1737}else{1738$result.= esc_path($diffinfo->{'to_file'});1739}1740$result.="</div>\n".# class="diff header"1741"<div class=\"diff nodifferences\">".1742"Simple merge".1743"</div>\n";# class="diff nodifferences"17441745return$result;1746}17471748# format patch (diff) line (not to be used for diff headers)1749sub format_diff_line {1750my$line=shift;1751my($from,$to) =@_;1752my$diff_class="";17531754chomp$line;17551756if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1757# combined diff1758my$prefix=substr($line,0,scalar@{$from->{'href'}});1759if($line=~m/^\@{3}/) {1760$diff_class=" chunk_header";1761}elsif($line=~m/^\\/) {1762$diff_class=" incomplete";1763}elsif($prefix=~tr/+/+/) {1764$diff_class=" add";1765}elsif($prefix=~tr/-/-/) {1766$diff_class=" rem";1767}1768}else{1769# assume ordinary diff1770my$char=substr($line,0,1);1771if($chareq'+') {1772$diff_class=" add";1773}elsif($chareq'-') {1774$diff_class=" rem";1775}elsif($chareq'@') {1776$diff_class=" chunk_header";1777}elsif($chareq"\\") {1778$diff_class=" incomplete";1779}1780}1781$line= untabify($line);1782if($from&&$to&&$line=~m/^\@{2} /) {1783my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1784$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;17851786$from_lines=0unlessdefined$from_lines;1787$to_lines=0unlessdefined$to_lines;17881789if($from->{'href'}) {1790$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1791-class=>"list"},$from_text);1792}1793if($to->{'href'}) {1794$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1795-class=>"list"},$to_text);1796}1797$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1798"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1799return"<div class=\"diff$diff_class\">$line</div>\n";1800}elsif($from&&$to&&$line=~m/^\@{3}/) {1801my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1802my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);18031804@from_text=split(' ',$ranges);1805for(my$i=0;$i<@from_text; ++$i) {1806($from_start[$i],$from_nlines[$i]) =1807(split(',',substr($from_text[$i],1)),0);1808}18091810$to_text=pop@from_text;1811$to_start=pop@from_start;1812$to_nlines=pop@from_nlines;18131814$line="<span class=\"chunk_info\">$prefix";1815for(my$i=0;$i<@from_text; ++$i) {1816if($from->{'href'}[$i]) {1817$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1818-class=>"list"},$from_text[$i]);1819}else{1820$line.=$from_text[$i];1821}1822$line.=" ";1823}1824if($to->{'href'}) {1825$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1826-class=>"list"},$to_text);1827}else{1828$line.=$to_text;1829}1830$line.="$prefix</span>".1831"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1832return"<div class=\"diff$diff_class\">$line</div>\n";1833}1834return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1835}18361837# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1838# linked. Pass the hash of the tree/commit to snapshot.1839sub format_snapshot_links {1840my($hash) =@_;1841my$num_fmts=@snapshot_fmts;1842if($num_fmts>1) {1843# A parenthesized list of links bearing format names.1844# e.g. "snapshot (_tar.gz_ _zip_)"1845return"snapshot (".join(' ',map1846$cgi->a({1847-href => href(1848 action=>"snapshot",1849 hash=>$hash,1850 snapshot_format=>$_1851)1852},$known_snapshot_formats{$_}{'display'})1853,@snapshot_fmts) .")";1854}elsif($num_fmts==1) {1855# A single "snapshot" link whose tooltip bears the format name.1856# i.e. "_snapshot_"1857my($fmt) =@snapshot_fmts;1858return1859$cgi->a({1860-href => href(1861 action=>"snapshot",1862 hash=>$hash,1863 snapshot_format=>$fmt1864),1865-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1866},"snapshot");1867}else{# $num_fmts == 01868returnundef;1869}1870}18711872## ......................................................................1873## functions returning values to be passed, perhaps after some1874## transformation, to other functions; e.g. returning arguments to href()18751876# returns hash to be passed to href to generate gitweb URL1877# in -title key it returns description of link1878sub get_feed_info {1879my$format=shift||'Atom';1880my%res= (action =>lc($format));18811882# feed links are possible only for project views1883return unless(defined$project);1884# some views should link to OPML, or to generic project feed,1885# or don't have specific feed yet (so they should use generic)1886return if($action=~/^(?:tags|heads|forks|tag|search)$/x);18871888my$branch;1889# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1890# from tag links; this also makes possible to detect branch links1891if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1892(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1893$branch=$1;1894}1895# find log type for feed description (title)1896my$type='log';1897if(defined$file_name) {1898$type="history of$file_name";1899$type.="/"if($actioneq'tree');1900$type.=" on '$branch'"if(defined$branch);1901}else{1902$type="log of$branch"if(defined$branch);1903}19041905$res{-title} =$type;1906$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1907$res{'file_name'} =$file_name;19081909return%res;1910}19111912## ----------------------------------------------------------------------1913## git utility subroutines, invoking git commands19141915# returns path to the core git executable and the --git-dir parameter as list1916sub git_cmd {1917return$GIT,'--git-dir='.$git_dir;1918}19191920# quote the given arguments for passing them to the shell1921# quote_command("command", "arg 1", "arg with ' and ! characters")1922# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1923# Try to avoid using this function wherever possible.1924sub quote_command {1925returnjoin(' ',1926map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);1927}19281929# get HEAD ref of given project as hash1930sub git_get_head_hash {1931my$project=shift;1932my$o_git_dir=$git_dir;1933my$retval=undef;1934$git_dir="$projectroot/$project";1935if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1936my$head= <$fd>;1937close$fd;1938if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1939$retval=$1;1940}1941}1942if(defined$o_git_dir) {1943$git_dir=$o_git_dir;1944}1945return$retval;1946}19471948# get type of given object1949sub git_get_type {1950my$hash=shift;19511952open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1953my$type= <$fd>;1954close$fdorreturn;1955chomp$type;1956return$type;1957}19581959# repository configuration1960our$config_file='';1961our%config;19621963# store multiple values for single key as anonymous array reference1964# single values stored directly in the hash, not as [ <value> ]1965sub hash_set_multi {1966my($hash,$key,$value) =@_;19671968if(!exists$hash->{$key}) {1969$hash->{$key} =$value;1970}elsif(!ref$hash->{$key}) {1971$hash->{$key} = [$hash->{$key},$value];1972}else{1973push@{$hash->{$key}},$value;1974}1975}19761977# return hash of git project configuration1978# optionally limited to some section, e.g. 'gitweb'1979sub git_parse_project_config {1980my$section_regexp=shift;1981my%config;19821983local$/="\0";19841985open my$fh,"-|", git_cmd(),"config",'-z','-l',1986orreturn;19871988while(my$keyval= <$fh>) {1989chomp$keyval;1990my($key,$value) =split(/\n/,$keyval,2);19911992 hash_set_multi(\%config,$key,$value)1993if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1994}1995close$fh;19961997return%config;1998}19992000# convert config value to boolean: 'true' or 'false'2001# no value, number > 0, 'true' and 'yes' values are true2002# rest of values are treated as false (never as error)2003sub config_to_bool {2004my$val=shift;20052006return1if!defined$val;# section.key20072008# strip leading and trailing whitespace2009$val=~s/^\s+//;2010$val=~s/\s+$//;20112012return(($val=~/^\d+$/&&$val) ||# section.key = 12013($val=~/^(?:true|yes)$/i));# section.key = true2014}20152016# convert config value to simple decimal number2017# an optional value suffix of 'k', 'm', or 'g' will cause the value2018# to be multiplied by 1024, 1048576, or 10737418242019sub config_to_int {2020my$val=shift;20212022# strip leading and trailing whitespace2023$val=~s/^\s+//;2024$val=~s/\s+$//;20252026if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2027$unit=lc($unit);2028# unknown unit is treated as 12029return$num* ($uniteq'g'?1073741824:2030$uniteq'm'?1048576:2031$uniteq'k'?1024:1);2032}2033return$val;2034}20352036# convert config value to array reference, if needed2037sub config_to_multi {2038my$val=shift;20392040returnref($val) ?$val: (defined($val) ? [$val] : []);2041}20422043sub git_get_project_config {2044my($key,$type) =@_;20452046# key sanity check2047return unless($key);2048$key=~s/^gitweb\.//;2049return if($key=~m/\W/);20502051# type sanity check2052if(defined$type) {2053$type=~s/^--//;2054$type=undef2055unless($typeeq'bool'||$typeeq'int');2056}20572058# get config2059if(!defined$config_file||2060$config_filene"$git_dir/config") {2061%config= git_parse_project_config('gitweb');2062$config_file="$git_dir/config";2063}20642065# check if config variable (key) exists2066return unlessexists$config{"gitweb.$key"};20672068# ensure given type2069if(!defined$type) {2070return$config{"gitweb.$key"};2071}elsif($typeeq'bool') {2072# backward compatibility: 'git config --bool' returns true/false2073return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2074}elsif($typeeq'int') {2075return config_to_int($config{"gitweb.$key"});2076}2077return$config{"gitweb.$key"};2078}20792080# get hash of given path at given ref2081sub git_get_hash_by_path {2082my$base=shift;2083my$path=shift||returnundef;2084my$type=shift;20852086$path=~ s,/+$,,;20872088open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2089or die_error(500,"Open git-ls-tree failed");2090my$line= <$fd>;2091close$fdorreturnundef;20922093if(!defined$line) {2094# there is no tree or hash given by $path at $base2095returnundef;2096}20972098#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2099$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2100if(defined$type&&$typene$2) {2101# type doesn't match2102returnundef;2103}2104return$3;2105}21062107# get path of entry with given hash at given tree-ish (ref)2108# used to get 'from' filename for combined diff (merge commit) for renames2109sub git_get_path_by_hash {2110my$base=shift||return;2111my$hash=shift||return;21122113local$/="\0";21142115open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2116orreturnundef;2117while(my$line= <$fd>) {2118chomp$line;21192120#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2121#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2122if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2123close$fd;2124return$1;2125}2126}2127close$fd;2128returnundef;2129}21302131## ......................................................................2132## git utility functions, directly accessing git repository21332134sub git_get_project_description {2135my$path=shift;21362137$git_dir="$projectroot/$path";2138open my$fd,'<',"$git_dir/description"2139orreturn git_get_project_config('description');2140my$descr= <$fd>;2141close$fd;2142if(defined$descr) {2143chomp$descr;2144}2145return$descr;2146}21472148sub git_get_project_ctags {2149my$path=shift;2150my$ctags= {};21512152$git_dir="$projectroot/$path";2153opendir my$dh,"$git_dir/ctags"2154orreturn$ctags;2155foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2156open my$ct,'<',$_ornext;2157my$val= <$ct>;2158chomp$val;2159close$ct;2160my$ctag=$_;$ctag=~ s#.*/##;2161$ctags->{$ctag} =$val;2162}2163closedir$dh;2164$ctags;2165}21662167sub git_populate_project_tagcloud {2168my$ctags=shift;21692170# First, merge different-cased tags; tags vote on casing2171my%ctags_lc;2172foreach(keys%$ctags) {2173$ctags_lc{lc$_}->{count} +=$ctags->{$_};2174if(not$ctags_lc{lc$_}->{topcount}2175or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2176$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2177$ctags_lc{lc$_}->{topname} =$_;2178}2179}21802181my$cloud;2182if(eval{require HTML::TagCloud;1; }) {2183$cloud= HTML::TagCloud->new;2184foreach(sort keys%ctags_lc) {2185# Pad the title with spaces so that the cloud looks2186# less crammed.2187my$title=$ctags_lc{$_}->{topname};2188$title=~s/ / /g;2189$title=~s/^/ /g;2190$title=~s/$/ /g;2191$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2192}2193}else{2194$cloud= \%ctags_lc;2195}2196$cloud;2197}21982199sub git_show_project_tagcloud {2200my($cloud,$count) =@_;2201print STDERR ref($cloud)."..\n";2202if(ref$cloudeq'HTML::TagCloud') {2203return$cloud->html_and_css($count);2204}else{2205my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2206return'<p align="center">'.join(', ',map{2207"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2208}splice(@tags,0,$count)) .'</p>';2209}2210}22112212sub git_get_project_url_list {2213my$path=shift;22142215$git_dir="$projectroot/$path";2216open my$fd,'<',"$git_dir/cloneurl"2217orreturnwantarray?2218@{ config_to_multi(git_get_project_config('url')) } :2219 config_to_multi(git_get_project_config('url'));2220my@git_project_url_list=map{chomp;$_} <$fd>;2221close$fd;22222223returnwantarray?@git_project_url_list: \@git_project_url_list;2224}22252226sub git_get_projects_list {2227my($filter) =@_;2228my@list;22292230$filter||='';2231$filter=~s/\.git$//;22322233my$check_forks= gitweb_check_feature('forks');22342235if(-d $projects_list) {2236# search in directory2237my$dir=$projects_list. ($filter?"/$filter":'');2238# remove the trailing "/"2239$dir=~s!/+$!!;2240my$pfxlen=length("$dir");2241my$pfxdepth= ($dir=~tr!/!!);22422243 File::Find::find({2244 follow_fast =>1,# follow symbolic links2245 follow_skip =>2,# ignore duplicates2246 dangling_symlinks =>0,# ignore dangling symlinks, silently2247 wanted =>sub{2248# skip project-list toplevel, if we get it.2249return if(m!^[/.]$!);2250# only directories can be git repositories2251return unless(-d $_);2252# don't traverse too deep (Find is super slow on os x)2253if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2254$File::Find::prune =1;2255return;2256}22572258my$subdir=substr($File::Find::name,$pfxlen+1);2259# we check related file in $projectroot2260my$path= ($filter?"$filter/":'') .$subdir;2261if(check_export_ok("$projectroot/$path")) {2262push@list, { path =>$path};2263$File::Find::prune =1;2264}2265},2266},"$dir");22672268}elsif(-f $projects_list) {2269# read from file(url-encoded):2270# 'git%2Fgit.git Linus+Torvalds'2271# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2272# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2273my%paths;2274open my$fd,'<',$projects_listorreturn;2275 PROJECT:2276while(my$line= <$fd>) {2277chomp$line;2278my($path,$owner) =split' ',$line;2279$path= unescape($path);2280$owner= unescape($owner);2281if(!defined$path) {2282next;2283}2284if($filterne'') {2285# looking for forks;2286my$pfx=substr($path,0,length($filter));2287if($pfxne$filter) {2288next PROJECT;2289}2290my$sfx=substr($path,length($filter));2291if($sfx!~/^\/.*\.git$/) {2292next PROJECT;2293}2294}elsif($check_forks) {2295 PATH:2296foreachmy$filter(keys%paths) {2297# looking for forks;2298my$pfx=substr($path,0,length($filter));2299if($pfxne$filter) {2300next PATH;2301}2302my$sfx=substr($path,length($filter));2303if($sfx!~/^\/.*\.git$/) {2304next PATH;2305}2306# is a fork, don't include it in2307# the list2308next PROJECT;2309}2310}2311if(check_export_ok("$projectroot/$path")) {2312my$pr= {2313 path =>$path,2314 owner => to_utf8($owner),2315};2316push@list,$pr;2317(my$forks_path=$path) =~s/\.git$//;2318$paths{$forks_path}++;2319}2320}2321close$fd;2322}2323return@list;2324}23252326our$gitweb_project_owner=undef;2327sub git_get_project_list_from_file {23282329return if(defined$gitweb_project_owner);23302331$gitweb_project_owner= {};2332# read from file (url-encoded):2333# 'git%2Fgit.git Linus+Torvalds'2334# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2335# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2336if(-f $projects_list) {2337open(my$fd,'<',$projects_list);2338while(my$line= <$fd>) {2339chomp$line;2340my($pr,$ow) =split' ',$line;2341$pr= unescape($pr);2342$ow= unescape($ow);2343$gitweb_project_owner->{$pr} = to_utf8($ow);2344}2345close$fd;2346}2347}23482349sub git_get_project_owner {2350my$project=shift;2351my$owner;23522353returnundefunless$project;2354$git_dir="$projectroot/$project";23552356if(!defined$gitweb_project_owner) {2357 git_get_project_list_from_file();2358}23592360if(exists$gitweb_project_owner->{$project}) {2361$owner=$gitweb_project_owner->{$project};2362}2363if(!defined$owner){2364$owner= git_get_project_config('owner');2365}2366if(!defined$owner) {2367$owner= get_file_owner("$git_dir");2368}23692370return$owner;2371}23722373sub git_get_last_activity {2374my($path) =@_;2375my$fd;23762377$git_dir="$projectroot/$path";2378open($fd,"-|", git_cmd(),'for-each-ref',2379'--format=%(committer)',2380'--sort=-committerdate',2381'--count=1',2382'refs/heads')orreturn;2383my$most_recent= <$fd>;2384close$fdorreturn;2385if(defined$most_recent&&2386$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2387my$timestamp=$1;2388my$age=time-$timestamp;2389return($age, age_string($age));2390}2391return(undef,undef);2392}23932394sub git_get_references {2395my$type=shift||"";2396my%refs;2397# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112398# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2399open my$fd,"-|", git_cmd(),"show-ref","--dereference",2400($type? ("--","refs/$type") : ())# use -- <pattern> if $type2401orreturn;24022403while(my$line= <$fd>) {2404chomp$line;2405if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2406if(defined$refs{$1}) {2407push@{$refs{$1}},$2;2408}else{2409$refs{$1} = [$2];2410}2411}2412}2413close$fdorreturn;2414return \%refs;2415}24162417sub git_get_rev_name_tags {2418my$hash=shift||returnundef;24192420open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2421orreturn;2422my$name_rev= <$fd>;2423close$fd;24242425if($name_rev=~ m|^$hash tags/(.*)$|) {2426return$1;2427}else{2428# catches also '$hash undefined' output2429returnundef;2430}2431}24322433## ----------------------------------------------------------------------2434## parse to hash functions24352436sub parse_date {2437my$epoch=shift;2438my$tz=shift||"-0000";24392440my%date;2441my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2442my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2443my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2444$date{'hour'} =$hour;2445$date{'minute'} =$min;2446$date{'mday'} =$mday;2447$date{'day'} =$days[$wday];2448$date{'month'} =$months[$mon];2449$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2450$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2451$date{'mday-time'} =sprintf"%d%s%02d:%02d",2452$mday,$months[$mon],$hour,$min;2453$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",24541900+$year,1+$mon,$mday,$hour,$min,$sec;24552456$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2457my$local=$epoch+ ((int$1+ ($2/60)) *3600);2458($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2459$date{'hour_local'} =$hour;2460$date{'minute_local'} =$min;2461$date{'tz_local'} =$tz;2462$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",24631900+$year,$mon+1,$mday,2464$hour,$min,$sec,$tz);2465return%date;2466}24672468sub parse_tag {2469my$tag_id=shift;2470my%tag;2471my@comment;24722473open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2474$tag{'id'} =$tag_id;2475while(my$line= <$fd>) {2476chomp$line;2477if($line=~m/^object ([0-9a-fA-F]{40})$/) {2478$tag{'object'} =$1;2479}elsif($line=~m/^type (.+)$/) {2480$tag{'type'} =$1;2481}elsif($line=~m/^tag (.+)$/) {2482$tag{'name'} =$1;2483}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2484$tag{'author'} =$1;2485$tag{'author_epoch'} =$2;2486$tag{'author_tz'} =$3;2487if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2488$tag{'author_name'} =$1;2489$tag{'author_email'} =$2;2490}else{2491$tag{'author_name'} =$tag{'author'};2492}2493}elsif($line=~m/--BEGIN/) {2494push@comment,$line;2495last;2496}elsif($lineeq"") {2497last;2498}2499}2500push@comment, <$fd>;2501$tag{'comment'} = \@comment;2502close$fdorreturn;2503if(!defined$tag{'name'}) {2504return2505};2506return%tag2507}25082509sub parse_commit_text {2510my($commit_text,$withparents) =@_;2511my@commit_lines=split'\n',$commit_text;2512my%co;25132514pop@commit_lines;# Remove '\0'25152516if(!@commit_lines) {2517return;2518}25192520my$header=shift@commit_lines;2521if($header!~m/^[0-9a-fA-F]{40}/) {2522return;2523}2524($co{'id'},my@parents) =split' ',$header;2525while(my$line=shift@commit_lines) {2526last if$lineeq"\n";2527if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2528$co{'tree'} =$1;2529}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2530push@parents,$1;2531}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2532$co{'author'} =$1;2533$co{'author_epoch'} =$2;2534$co{'author_tz'} =$3;2535if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2536$co{'author_name'} =$1;2537$co{'author_email'} =$2;2538}else{2539$co{'author_name'} =$co{'author'};2540}2541}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2542$co{'committer'} =$1;2543$co{'committer_epoch'} =$2;2544$co{'committer_tz'} =$3;2545$co{'committer_name'} =$co{'committer'};2546if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2547$co{'committer_name'} =$1;2548$co{'committer_email'} =$2;2549}else{2550$co{'committer_name'} =$co{'committer'};2551}2552}2553}2554if(!defined$co{'tree'}) {2555return;2556};2557$co{'parents'} = \@parents;2558$co{'parent'} =$parents[0];25592560foreachmy$title(@commit_lines) {2561$title=~s/^ //;2562if($titlene"") {2563$co{'title'} = chop_str($title,80,5);2564# remove leading stuff of merges to make the interesting part visible2565if(length($title) >50) {2566$title=~s/^Automatic //;2567$title=~s/^merge (of|with) /Merge ... /i;2568if(length($title) >50) {2569$title=~s/(http|rsync):\/\///;2570}2571if(length($title) >50) {2572$title=~s/(master|www|rsync)\.//;2573}2574if(length($title) >50) {2575$title=~s/kernel.org:?//;2576}2577if(length($title) >50) {2578$title=~s/\/pub\/scm//;2579}2580}2581$co{'title_short'} = chop_str($title,50,5);2582last;2583}2584}2585if(!defined$co{'title'} ||$co{'title'}eq"") {2586$co{'title'} =$co{'title_short'} ='(no commit message)';2587}2588# remove added spaces2589foreachmy$line(@commit_lines) {2590$line=~s/^ //;2591}2592$co{'comment'} = \@commit_lines;25932594my$age=time-$co{'committer_epoch'};2595$co{'age'} =$age;2596$co{'age_string'} = age_string($age);2597my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2598if($age>60*60*24*7*2) {2599$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2600$co{'age_string_age'} =$co{'age_string'};2601}else{2602$co{'age_string_date'} =$co{'age_string'};2603$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2604}2605return%co;2606}26072608sub parse_commit {2609my($commit_id) =@_;2610my%co;26112612local$/="\0";26132614open my$fd,"-|", git_cmd(),"rev-list",2615"--parents",2616"--header",2617"--max-count=1",2618$commit_id,2619"--",2620or die_error(500,"Open git-rev-list failed");2621%co= parse_commit_text(<$fd>,1);2622close$fd;26232624return%co;2625}26262627sub parse_commits {2628my($commit_id,$maxcount,$skip,$filename,@args) =@_;2629my@cos;26302631$maxcount||=1;2632$skip||=0;26332634local$/="\0";26352636open my$fd,"-|", git_cmd(),"rev-list",2637"--header",2638@args,2639("--max-count=".$maxcount),2640("--skip=".$skip),2641@extra_options,2642$commit_id,2643"--",2644($filename? ($filename) : ())2645or die_error(500,"Open git-rev-list failed");2646while(my$line= <$fd>) {2647my%co= parse_commit_text($line);2648push@cos, \%co;2649}2650close$fd;26512652returnwantarray?@cos: \@cos;2653}26542655# parse line of git-diff-tree "raw" output2656sub parse_difftree_raw_line {2657my$line=shift;2658my%res;26592660# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2661# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2662if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2663$res{'from_mode'} =$1;2664$res{'to_mode'} =$2;2665$res{'from_id'} =$3;2666$res{'to_id'} =$4;2667$res{'status'} =$5;2668$res{'similarity'} =$6;2669if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2670($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2671}else{2672$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2673}2674}2675# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2676# combined diff (for merge commit)2677elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2678$res{'nparents'} =length($1);2679$res{'from_mode'} = [split(' ',$2) ];2680$res{'to_mode'} =pop@{$res{'from_mode'}};2681$res{'from_id'} = [split(' ',$3) ];2682$res{'to_id'} =pop@{$res{'from_id'}};2683$res{'status'} = [split('',$4) ];2684$res{'to_file'} = unquote($5);2685}2686# 'c512b523472485aef4fff9e57b229d9d243c967f'2687elsif($line=~m/^([0-9a-fA-F]{40})$/) {2688$res{'commit'} =$1;2689}26902691returnwantarray?%res: \%res;2692}26932694# wrapper: return parsed line of git-diff-tree "raw" output2695# (the argument might be raw line, or parsed info)2696sub parsed_difftree_line {2697my$line_or_ref=shift;26982699if(ref($line_or_ref)eq"HASH") {2700# pre-parsed (or generated by hand)2701return$line_or_ref;2702}else{2703return parse_difftree_raw_line($line_or_ref);2704}2705}27062707# parse line of git-ls-tree output2708sub parse_ls_tree_line {2709my$line=shift;2710my%opts=@_;2711my%res;27122713#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2714$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;27152716$res{'mode'} =$1;2717$res{'type'} =$2;2718$res{'hash'} =$3;2719if($opts{'-z'}) {2720$res{'name'} =$4;2721}else{2722$res{'name'} = unquote($4);2723}27242725returnwantarray?%res: \%res;2726}27272728# generates _two_ hashes, references to which are passed as 2 and 3 argument2729sub parse_from_to_diffinfo {2730my($diffinfo,$from,$to,@parents) =@_;27312732if($diffinfo->{'nparents'}) {2733# combined diff2734$from->{'file'} = [];2735$from->{'href'} = [];2736 fill_from_file_info($diffinfo,@parents)2737unlessexists$diffinfo->{'from_file'};2738for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2739$from->{'file'}[$i] =2740defined$diffinfo->{'from_file'}[$i] ?2741$diffinfo->{'from_file'}[$i] :2742$diffinfo->{'to_file'};2743if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2744$from->{'href'}[$i] = href(action=>"blob",2745 hash_base=>$parents[$i],2746 hash=>$diffinfo->{'from_id'}[$i],2747 file_name=>$from->{'file'}[$i]);2748}else{2749$from->{'href'}[$i] =undef;2750}2751}2752}else{2753# ordinary (not combined) diff2754$from->{'file'} =$diffinfo->{'from_file'};2755if($diffinfo->{'status'}ne"A") {# not new (added) file2756$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2757 hash=>$diffinfo->{'from_id'},2758 file_name=>$from->{'file'});2759}else{2760delete$from->{'href'};2761}2762}27632764$to->{'file'} =$diffinfo->{'to_file'};2765if(!is_deleted($diffinfo)) {# file exists in result2766$to->{'href'} = href(action=>"blob", hash_base=>$hash,2767 hash=>$diffinfo->{'to_id'},2768 file_name=>$to->{'file'});2769}else{2770delete$to->{'href'};2771}2772}27732774## ......................................................................2775## parse to array of hashes functions27762777sub git_get_heads_list {2778my$limit=shift;2779my@headslist;27802781open my$fd,'-|', git_cmd(),'for-each-ref',2782($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2783'--format=%(objectname) %(refname) %(subject)%00%(committer)',2784'refs/heads'2785orreturn;2786while(my$line= <$fd>) {2787my%ref_item;27882789chomp$line;2790my($refinfo,$committerinfo) =split(/\0/,$line);2791my($hash,$name,$title) =split(' ',$refinfo,3);2792my($committer,$epoch,$tz) =2793($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2794$ref_item{'fullname'} =$name;2795$name=~s!^refs/heads/!!;27962797$ref_item{'name'} =$name;2798$ref_item{'id'} =$hash;2799$ref_item{'title'} =$title||'(no commit message)';2800$ref_item{'epoch'} =$epoch;2801if($epoch) {2802$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2803}else{2804$ref_item{'age'} ="unknown";2805}28062807push@headslist, \%ref_item;2808}2809close$fd;28102811returnwantarray?@headslist: \@headslist;2812}28132814sub git_get_tags_list {2815my$limit=shift;2816my@tagslist;28172818open my$fd,'-|', git_cmd(),'for-each-ref',2819($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2820'--format=%(objectname) %(objecttype) %(refname) '.2821'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2822'refs/tags'2823orreturn;2824while(my$line= <$fd>) {2825my%ref_item;28262827chomp$line;2828my($refinfo,$creatorinfo) =split(/\0/,$line);2829my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2830my($creator,$epoch,$tz) =2831($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2832$ref_item{'fullname'} =$name;2833$name=~s!^refs/tags/!!;28342835$ref_item{'type'} =$type;2836$ref_item{'id'} =$id;2837$ref_item{'name'} =$name;2838if($typeeq"tag") {2839$ref_item{'subject'} =$title;2840$ref_item{'reftype'} =$reftype;2841$ref_item{'refid'} =$refid;2842}else{2843$ref_item{'reftype'} =$type;2844$ref_item{'refid'} =$id;2845}28462847if($typeeq"tag"||$typeeq"commit") {2848$ref_item{'epoch'} =$epoch;2849if($epoch) {2850$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2851}else{2852$ref_item{'age'} ="unknown";2853}2854}28552856push@tagslist, \%ref_item;2857}2858close$fd;28592860returnwantarray?@tagslist: \@tagslist;2861}28622863## ----------------------------------------------------------------------2864## filesystem-related functions28652866sub get_file_owner {2867my$path=shift;28682869my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2870my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2871if(!defined$gcos) {2872returnundef;2873}2874my$owner=$gcos;2875$owner=~s/[,;].*$//;2876return to_utf8($owner);2877}28782879# assume that file exists2880sub insert_file {2881my$filename=shift;28822883open my$fd,'<',$filename;2884print map{ to_utf8($_) } <$fd>;2885close$fd;2886}28872888## ......................................................................2889## mimetype related functions28902891sub mimetype_guess_file {2892my$filename=shift;2893my$mimemap=shift;2894-r $mimemaporreturnundef;28952896my%mimemap;2897open(my$mh,'<',$mimemap)orreturnundef;2898while(<$mh>) {2899next ifm/^#/;# skip comments2900my($mimetype,$exts) =split(/\t+/);2901if(defined$exts) {2902my@exts=split(/\s+/,$exts);2903foreachmy$ext(@exts) {2904$mimemap{$ext} =$mimetype;2905}2906}2907}2908close($mh);29092910$filename=~/\.([^.]*)$/;2911return$mimemap{$1};2912}29132914sub mimetype_guess {2915my$filename=shift;2916my$mime;2917$filename=~/\./orreturnundef;29182919if($mimetypes_file) {2920my$file=$mimetypes_file;2921if($file!~m!^/!) {# if it is relative path2922# it is relative to project2923$file="$projectroot/$project/$file";2924}2925$mime= mimetype_guess_file($filename,$file);2926}2927$mime||= mimetype_guess_file($filename,'/etc/mime.types');2928return$mime;2929}29302931sub blob_mimetype {2932my$fd=shift;2933my$filename=shift;29342935if($filename) {2936my$mime= mimetype_guess($filename);2937$mimeandreturn$mime;2938}29392940# just in case2941return$default_blob_plain_mimetypeunless$fd;29422943if(-T $fd) {2944return'text/plain';2945}elsif(!$filename) {2946return'application/octet-stream';2947}elsif($filename=~m/\.png$/i) {2948return'image/png';2949}elsif($filename=~m/\.gif$/i) {2950return'image/gif';2951}elsif($filename=~m/\.jpe?g$/i) {2952return'image/jpeg';2953}else{2954return'application/octet-stream';2955}2956}29572958sub blob_contenttype {2959my($fd,$file_name,$type) =@_;29602961$type||= blob_mimetype($fd,$file_name);2962if($typeeq'text/plain'&&defined$default_text_plain_charset) {2963$type.="; charset=$default_text_plain_charset";2964}29652966return$type;2967}29682969## ======================================================================2970## functions printing HTML: header, footer, error page29712972sub git_header_html {2973my$status=shift||"200 OK";2974my$expires=shift;29752976my$title="$site_name";2977if(defined$project) {2978$title.=" - ". to_utf8($project);2979if(defined$action) {2980$title.="/$action";2981if(defined$file_name) {2982$title.=" - ". esc_path($file_name);2983if($actioneq"tree"&&$file_name!~ m|/$|) {2984$title.="/";2985}2986}2987}2988}2989my$content_type;2990# require explicit support from the UA if we are to send the page as2991# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2992# we have to do this because MSIE sometimes globs '*/*', pretending to2993# support xhtml+xml but choking when it gets what it asked for.2994if(defined$cgi->http('HTTP_ACCEPT') &&2995$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2996$cgi->Accept('application/xhtml+xml') !=0) {2997$content_type='application/xhtml+xml';2998}else{2999$content_type='text/html';3000}3001print$cgi->header(-type=>$content_type, -charset =>'utf-8',3002-status=>$status, -expires =>$expires);3003my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3004print<<EOF;3005<?xml version="1.0" encoding="utf-8"?>3006<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3007<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3008<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3009<!-- git core binaries version$git_version-->3010<head>3011<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3012<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3013<meta name="robots" content="index, nofollow"/>3014<title>$title</title>3015EOF3016# the stylesheet, favicon etc urls won't work correctly with path_info3017# unless we set the appropriate base URL3018if($ENV{'PATH_INFO'}) {3019print"<base href=\"".esc_url($base_url)."\"/>\n";3020}3021# print out each stylesheet that exist, providing backwards capability3022# for those people who defined $stylesheet in a config file3023if(defined$stylesheet) {3024print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3025}else{3026foreachmy$stylesheet(@stylesheets) {3027next unless$stylesheet;3028print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3029}3030}3031if(defined$project) {3032my%href_params= get_feed_info();3033if(!exists$href_params{'-title'}) {3034$href_params{'-title'} ='log';3035}30363037foreachmy$formatqw(RSS Atom){3038my$type=lc($format);3039my%link_attr= (3040'-rel'=>'alternate',3041'-title'=>"$project-$href_params{'-title'} -$formatfeed",3042'-type'=>"application/$type+xml"3043);30443045$href_params{'action'} =$type;3046$link_attr{'-href'} = href(%href_params);3047print"<link ".3048"rel=\"$link_attr{'-rel'}\"".3049"title=\"$link_attr{'-title'}\"".3050"href=\"$link_attr{'-href'}\"".3051"type=\"$link_attr{'-type'}\"".3052"/>\n";30533054$href_params{'extra_options'} ='--no-merges';3055$link_attr{'-href'} = href(%href_params);3056$link_attr{'-title'} .=' (no merges)';3057print"<link ".3058"rel=\"$link_attr{'-rel'}\"".3059"title=\"$link_attr{'-title'}\"".3060"href=\"$link_attr{'-href'}\"".3061"type=\"$link_attr{'-type'}\"".3062"/>\n";3063}30643065}else{3066printf('<link rel="alternate" title="%sprojects list" '.3067'href="%s" type="text/plain; charset=utf-8" />'."\n",3068$site_name, href(project=>undef, action=>"project_index"));3069printf('<link rel="alternate" title="%sprojects feeds" '.3070'href="%s" type="text/x-opml" />'."\n",3071$site_name, href(project=>undef, action=>"opml"));3072}3073if(defined$favicon) {3074printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3075}30763077print"</head>\n".3078"<body>\n";30793080if(-f $site_header) {3081 insert_file($site_header);3082}30833084print"<div class=\"page_header\">\n".3085$cgi->a({-href => esc_url($logo_url),3086-title =>$logo_label},3087qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3088print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3089if(defined$project) {3090print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3091if(defined$action) {3092print" /$action";3093}3094print"\n";3095}3096print"</div>\n";30973098my$have_search= gitweb_check_feature('search');3099if(defined$project&&$have_search) {3100if(!defined$searchtext) {3101$searchtext="";3102}3103my$search_hash;3104if(defined$hash_base) {3105$search_hash=$hash_base;3106}elsif(defined$hash) {3107$search_hash=$hash;3108}else{3109$search_hash="HEAD";3110}3111my$action=$my_uri;3112my$use_pathinfo= gitweb_check_feature('pathinfo');3113if($use_pathinfo) {3114$action.="/".esc_url($project);3115}3116print$cgi->startform(-method=>"get", -action =>$action) .3117"<div class=\"search\">\n".3118(!$use_pathinfo&&3119$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3120$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3121$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3122$cgi->popup_menu(-name =>'st', -default=>'commit',3123-values=> ['commit','grep','author','committer','pickaxe']) .3124$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3125" search:\n",3126$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3127"<span title=\"Extended regular expression\">".3128$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3129-checked =>$search_use_regexp) .3130"</span>".3131"</div>".3132$cgi->end_form() ."\n";3133}3134}31353136sub git_footer_html {3137my$feed_class='rss_logo';31383139print"<div class=\"page_footer\">\n";3140if(defined$project) {3141my$descr= git_get_project_description($project);3142if(defined$descr) {3143print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3144}31453146my%href_params= get_feed_info();3147if(!%href_params) {3148$feed_class.=' generic';3149}3150$href_params{'-title'} ||='log';31513152foreachmy$formatqw(RSS Atom){3153$href_params{'action'} =lc($format);3154print$cgi->a({-href => href(%href_params),3155-title =>"$href_params{'-title'}$formatfeed",3156-class=>$feed_class},$format)."\n";3157}31583159}else{3160print$cgi->a({-href => href(project=>undef, action=>"opml"),3161-class=>$feed_class},"OPML") ." ";3162print$cgi->a({-href => href(project=>undef, action=>"project_index"),3163-class=>$feed_class},"TXT") ."\n";3164}3165print"</div>\n";# class="page_footer"31663167if(-f $site_footer) {3168 insert_file($site_footer);3169}31703171print"</body>\n".3172"</html>";3173}31743175# die_error(<http_status_code>, <error_message>)3176# Example: die_error(404, 'Hash not found')3177# By convention, use the following status codes (as defined in RFC 2616):3178# 400: Invalid or missing CGI parameters, or3179# requested object exists but has wrong type.3180# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3181# this server or project.3182# 404: Requested object/revision/project doesn't exist.3183# 500: The server isn't configured properly, or3184# an internal error occurred (e.g. failed assertions caused by bugs), or3185# an unknown error occurred (e.g. the git binary died unexpectedly).3186sub die_error {3187my$status=shift||500;3188my$error=shift||"Internal server error";31893190my%http_responses= (400=>'400 Bad Request',3191403=>'403 Forbidden',3192404=>'404 Not Found',3193500=>'500 Internal Server Error');3194 git_header_html($http_responses{$status});3195print<<EOF;3196<div class="page_body">3197<br /><br />3198$status-$error3199<br />3200</div>3201EOF3202 git_footer_html();3203exit;3204}32053206## ----------------------------------------------------------------------3207## functions printing or outputting HTML: navigation32083209sub git_print_page_nav {3210my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3211$extra=''if!defined$extra;# pager or formats32123213my@navs=qw(summary shortlog log commit commitdiff tree);3214if($suppress) {3215@navs=grep{$_ne$suppress}@navs;3216}32173218my%arg=map{$_=> {action=>$_} }@navs;3219if(defined$head) {3220for(qw(commit commitdiff)) {3221$arg{$_}{'hash'} =$head;3222}3223if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3224for(qw(shortlog log)) {3225$arg{$_}{'hash'} =$head;3226}3227}3228}32293230$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3231$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;32323233my@actions= gitweb_get_feature('actions');3234my%repl= (3235'%'=>'%',3236'n'=>$project,# project name3237'f'=>$git_dir,# project path within filesystem3238'h'=>$treehead||'',# current hash ('h' parameter)3239'b'=>$treebase||'',# hash base ('hb' parameter)3240);3241while(@actions) {3242my($label,$link,$pos) =splice(@actions,0,3);3243# insert3244@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3245# munch munch3246$link=~s/%([%nfhb])/$repl{$1}/g;3247$arg{$label}{'_href'} =$link;3248}32493250print"<div class=\"page_nav\">\n".3251(join" | ",3252map{$_eq$current?3253$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3254}@navs);3255print"<br/>\n$extra<br/>\n".3256"</div>\n";3257}32583259sub format_paging_nav {3260my($action,$hash,$head,$page,$has_next_link) =@_;3261my$paging_nav;326232633264if($hashne$head||$page) {3265$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3266}else{3267$paging_nav.="HEAD";3268}32693270if($page>0) {3271$paging_nav.=" ⋅ ".3272$cgi->a({-href => href(-replay=>1, page=>$page-1),3273-accesskey =>"p", -title =>"Alt-p"},"prev");3274}else{3275$paging_nav.=" ⋅ prev";3276}32773278if($has_next_link) {3279$paging_nav.=" ⋅ ".3280$cgi->a({-href => href(-replay=>1, page=>$page+1),3281-accesskey =>"n", -title =>"Alt-n"},"next");3282}else{3283$paging_nav.=" ⋅ next";3284}32853286return$paging_nav;3287}32883289## ......................................................................3290## functions printing or outputting HTML: div32913292sub git_print_header_div {3293my($action,$title,$hash,$hash_base) =@_;3294my%args= ();32953296$args{'action'} =$action;3297$args{'hash'} =$hashif$hash;3298$args{'hash_base'} =$hash_baseif$hash_base;32993300print"<div class=\"header\">\n".3301$cgi->a({-href => href(%args), -class=>"title"},3302$title?$title:$action) .3303"\n</div>\n";3304}33053306sub print_local_time {3307my%date=@_;3308if($date{'hour_local'} <6) {3309printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3310$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3311}else{3312printf(" (%02d:%02d%s)",3313$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3314}3315}33163317# Outputs the author name and date in long form3318sub git_print_authorship {3319my$co=shift;3320my%opts=@_;3321my$tag=$opts{-tag} ||'div';33223323my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3324print"<$tagclass=\"author_date\">".3325 esc_html($co->{'author_name'}) .3326" [$ad{'rfc2822'}";3327 print_local_time(%ad)if($opts{-localtime});3328print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3329."</$tag>\n";3330}33313332# Outputs table rows containing the full author or committer information,3333# in the format expected for 'commit' view (& similia).3334# Parameters are a commit hash reference, followed by the list of people3335# to output information for. If the list is empty it defalts to both3336# author and committer.3337sub git_print_authorship_rows {3338my$co=shift;3339# too bad we can't use @people = @_ || ('author', 'committer')3340my@people=@_;3341@people= ('author','committer')unless@people;3342foreachmy$who(@people) {3343my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3344print"<tr><td>$who</td><td>". esc_html($co->{$who}) ."</td>".3345"<td rowspan=\"2\">".3346 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3347"</td></tr>\n".3348"<tr>".3349"<td></td><td>$wd{'rfc2822'}";3350 print_local_time(%wd);3351print"</td>".3352"</tr>\n";3353}3354}33553356sub git_print_page_path {3357my$name=shift;3358my$type=shift;3359my$hb=shift;336033613362print"<div class=\"page_path\">";3363print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3364-title =>'tree root'}, to_utf8("[$project]"));3365print" / ";3366if(defined$name) {3367my@dirname=split'/',$name;3368my$basename=pop@dirname;3369my$fullname='';33703371foreachmy$dir(@dirname) {3372$fullname.= ($fullname?'/':'') .$dir;3373print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3374 hash_base=>$hb),3375-title =>$fullname}, esc_path($dir));3376print" / ";3377}3378if(defined$type&&$typeeq'blob') {3379print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3380 hash_base=>$hb),3381-title =>$name}, esc_path($basename));3382}elsif(defined$type&&$typeeq'tree') {3383print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3384 hash_base=>$hb),3385-title =>$name}, esc_path($basename));3386print" / ";3387}else{3388print esc_path($basename);3389}3390}3391print"<br/></div>\n";3392}33933394sub git_print_log {3395my$log=shift;3396my%opts=@_;33973398if($opts{'-remove_title'}) {3399# remove title, i.e. first line of log3400shift@$log;3401}3402# remove leading empty lines3403while(defined$log->[0] &&$log->[0]eq"") {3404shift@$log;3405}34063407# print log3408my$signoff=0;3409my$empty=0;3410foreachmy$line(@$log) {3411if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3412$signoff=1;3413$empty=0;3414if(!$opts{'-remove_signoff'}) {3415print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3416next;3417}else{3418# remove signoff lines3419next;3420}3421}else{3422$signoff=0;3423}34243425# print only one empty line3426# do not print empty line after signoff3427if($lineeq"") {3428next if($empty||$signoff);3429$empty=1;3430}else{3431$empty=0;3432}34333434print format_log_line_html($line) ."<br/>\n";3435}34363437if($opts{'-final_empty_line'}) {3438# end with single empty line3439print"<br/>\n"unless$empty;3440}3441}34423443# return link target (what link points to)3444sub git_get_link_target {3445my$hash=shift;3446my$link_target;34473448# read link3449open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3450orreturn;3451{3452local$/=undef;3453$link_target= <$fd>;3454}3455close$fd3456orreturn;34573458return$link_target;3459}34603461# given link target, and the directory (basedir) the link is in,3462# return target of link relative to top directory (top tree);3463# return undef if it is not possible (including absolute links).3464sub normalize_link_target {3465my($link_target,$basedir) =@_;34663467# absolute symlinks (beginning with '/') cannot be normalized3468return if(substr($link_target,0,1)eq'/');34693470# normalize link target to path from top (root) tree (dir)3471my$path;3472if($basedir) {3473$path=$basedir.'/'.$link_target;3474}else{3475# we are in top (root) tree (dir)3476$path=$link_target;3477}34783479# remove //, /./, and /../3480my@path_parts;3481foreachmy$part(split('/',$path)) {3482# discard '.' and ''3483next if(!$part||$parteq'.');3484# handle '..'3485if($parteq'..') {3486if(@path_parts) {3487pop@path_parts;3488}else{3489# link leads outside repository (outside top dir)3490return;3491}3492}else{3493push@path_parts,$part;3494}3495}3496$path=join('/',@path_parts);34973498return$path;3499}35003501# print tree entry (row of git_tree), but without encompassing <tr> element3502sub git_print_tree_entry {3503my($t,$basedir,$hash_base,$have_blame) =@_;35043505my%base_key= ();3506$base_key{'hash_base'} =$hash_baseifdefined$hash_base;35073508# The format of a table row is: mode list link. Where mode is3509# the mode of the entry, list is the name of the entry, an href,3510# and link is the action links of the entry.35113512print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3513if($t->{'type'}eq"blob") {3514print"<td class=\"list\">".3515$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3516 file_name=>"$basedir$t->{'name'}",%base_key),3517-class=>"list"}, esc_path($t->{'name'}));3518if(S_ISLNK(oct$t->{'mode'})) {3519my$link_target= git_get_link_target($t->{'hash'});3520if($link_target) {3521my$norm_target= normalize_link_target($link_target,$basedir);3522if(defined$norm_target) {3523print" -> ".3524$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3525 file_name=>$norm_target),3526-title =>$norm_target}, esc_path($link_target));3527}else{3528print" -> ". esc_path($link_target);3529}3530}3531}3532print"</td>\n";3533print"<td class=\"link\">";3534print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3535 file_name=>"$basedir$t->{'name'}",%base_key)},3536"blob");3537if($have_blame) {3538print" | ".3539$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3540 file_name=>"$basedir$t->{'name'}",%base_key)},3541"blame");3542}3543if(defined$hash_base) {3544print" | ".3545$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3546 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3547"history");3548}3549print" | ".3550$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3551 file_name=>"$basedir$t->{'name'}")},3552"raw");3553print"</td>\n";35543555}elsif($t->{'type'}eq"tree") {3556print"<td class=\"list\">";3557print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3558 file_name=>"$basedir$t->{'name'}",%base_key)},3559 esc_path($t->{'name'}));3560print"</td>\n";3561print"<td class=\"link\">";3562print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3563 file_name=>"$basedir$t->{'name'}",%base_key)},3564"tree");3565if(defined$hash_base) {3566print" | ".3567$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3568 file_name=>"$basedir$t->{'name'}")},3569"history");3570}3571print"</td>\n";3572}else{3573# unknown object: we can only present history for it3574# (this includes 'commit' object, i.e. submodule support)3575print"<td class=\"list\">".3576 esc_path($t->{'name'}) .3577"</td>\n";3578print"<td class=\"link\">";3579if(defined$hash_base) {3580print$cgi->a({-href => href(action=>"history",3581 hash_base=>$hash_base,3582 file_name=>"$basedir$t->{'name'}")},3583"history");3584}3585print"</td>\n";3586}3587}35883589## ......................................................................3590## functions printing large fragments of HTML35913592# get pre-image filenames for merge (combined) diff3593sub fill_from_file_info {3594my($diff,@parents) =@_;35953596$diff->{'from_file'} = [ ];3597$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3598for(my$i=0;$i<$diff->{'nparents'};$i++) {3599if($diff->{'status'}[$i]eq'R'||3600$diff->{'status'}[$i]eq'C') {3601$diff->{'from_file'}[$i] =3602 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3603}3604}36053606return$diff;3607}36083609# is current raw difftree line of file deletion3610sub is_deleted {3611my$diffinfo=shift;36123613return$diffinfo->{'to_id'}eq('0' x 40);3614}36153616# does patch correspond to [previous] difftree raw line3617# $diffinfo - hashref of parsed raw diff format3618# $patchinfo - hashref of parsed patch diff format3619# (the same keys as in $diffinfo)3620sub is_patch_split {3621my($diffinfo,$patchinfo) =@_;36223623returndefined$diffinfo&&defined$patchinfo3624&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3625}362636273628sub git_difftree_body {3629my($difftree,$hash,@parents) =@_;3630my($parent) =$parents[0];3631my$have_blame= gitweb_check_feature('blame');3632print"<div class=\"list_head\">\n";3633if($#{$difftree} >10) {3634print(($#{$difftree} +1) ." files changed:\n");3635}3636print"</div>\n";36373638print"<table class=\"".3639(@parents>1?"combined ":"") .3640"diff_tree\">\n";36413642# header only for combined diff in 'commitdiff' view3643my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3644if($has_header) {3645# table header3646print"<thead><tr>\n".3647"<th></th><th></th>\n";# filename, patchN link3648for(my$i=0;$i<@parents;$i++) {3649my$par=$parents[$i];3650print"<th>".3651$cgi->a({-href => href(action=>"commitdiff",3652 hash=>$hash, hash_parent=>$par),3653-title =>'commitdiff to parent number '.3654($i+1) .': '.substr($par,0,7)},3655$i+1) .3656" </th>\n";3657}3658print"</tr></thead>\n<tbody>\n";3659}36603661my$alternate=1;3662my$patchno=0;3663foreachmy$line(@{$difftree}) {3664my$diff= parsed_difftree_line($line);36653666if($alternate) {3667print"<tr class=\"dark\">\n";3668}else{3669print"<tr class=\"light\">\n";3670}3671$alternate^=1;36723673if(exists$diff->{'nparents'}) {# combined diff36743675 fill_from_file_info($diff,@parents)3676unlessexists$diff->{'from_file'};36773678if(!is_deleted($diff)) {3679# file exists in the result (child) commit3680print"<td>".3681$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3682 file_name=>$diff->{'to_file'},3683 hash_base=>$hash),3684-class=>"list"}, esc_path($diff->{'to_file'})) .3685"</td>\n";3686}else{3687print"<td>".3688 esc_path($diff->{'to_file'}) .3689"</td>\n";3690}36913692if($actioneq'commitdiff') {3693# link to patch3694$patchno++;3695print"<td class=\"link\">".3696$cgi->a({-href =>"#patch$patchno"},"patch") .3697" | ".3698"</td>\n";3699}37003701my$has_history=0;3702my$not_deleted=0;3703for(my$i=0;$i<$diff->{'nparents'};$i++) {3704my$hash_parent=$parents[$i];3705my$from_hash=$diff->{'from_id'}[$i];3706my$from_path=$diff->{'from_file'}[$i];3707my$status=$diff->{'status'}[$i];37083709$has_history||= ($statusne'A');3710$not_deleted||= ($statusne'D');37113712if($statuseq'A') {3713print"<td class=\"link\"align=\"right\"> | </td>\n";3714}elsif($statuseq'D') {3715print"<td class=\"link\">".3716$cgi->a({-href => href(action=>"blob",3717 hash_base=>$hash,3718 hash=>$from_hash,3719 file_name=>$from_path)},3720"blob". ($i+1)) .3721" | </td>\n";3722}else{3723if($diff->{'to_id'}eq$from_hash) {3724print"<td class=\"link nochange\">";3725}else{3726print"<td class=\"link\">";3727}3728print$cgi->a({-href => href(action=>"blobdiff",3729 hash=>$diff->{'to_id'},3730 hash_parent=>$from_hash,3731 hash_base=>$hash,3732 hash_parent_base=>$hash_parent,3733 file_name=>$diff->{'to_file'},3734 file_parent=>$from_path)},3735"diff". ($i+1)) .3736" | </td>\n";3737}3738}37393740print"<td class=\"link\">";3741if($not_deleted) {3742print$cgi->a({-href => href(action=>"blob",3743 hash=>$diff->{'to_id'},3744 file_name=>$diff->{'to_file'},3745 hash_base=>$hash)},3746"blob");3747print" | "if($has_history);3748}3749if($has_history) {3750print$cgi->a({-href => href(action=>"history",3751 file_name=>$diff->{'to_file'},3752 hash_base=>$hash)},3753"history");3754}3755print"</td>\n";37563757print"</tr>\n";3758next;# instead of 'else' clause, to avoid extra indent3759}3760# else ordinary diff37613762my($to_mode_oct,$to_mode_str,$to_file_type);3763my($from_mode_oct,$from_mode_str,$from_file_type);3764if($diff->{'to_mode'}ne('0' x 6)) {3765$to_mode_oct=oct$diff->{'to_mode'};3766if(S_ISREG($to_mode_oct)) {# only for regular file3767$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3768}3769$to_file_type= file_type($diff->{'to_mode'});3770}3771if($diff->{'from_mode'}ne('0' x 6)) {3772$from_mode_oct=oct$diff->{'from_mode'};3773if(S_ISREG($to_mode_oct)) {# only for regular file3774$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3775}3776$from_file_type= file_type($diff->{'from_mode'});3777}37783779if($diff->{'status'}eq"A") {# created3780my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3781$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3782$mode_chng.="]</span>";3783print"<td>";3784print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3785 hash_base=>$hash, file_name=>$diff->{'file'}),3786-class=>"list"}, esc_path($diff->{'file'}));3787print"</td>\n";3788print"<td>$mode_chng</td>\n";3789print"<td class=\"link\">";3790if($actioneq'commitdiff') {3791# link to patch3792$patchno++;3793print$cgi->a({-href =>"#patch$patchno"},"patch");3794print" | ";3795}3796print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3797 hash_base=>$hash, file_name=>$diff->{'file'})},3798"blob");3799print"</td>\n";38003801}elsif($diff->{'status'}eq"D") {# deleted3802my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3803print"<td>";3804print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3805 hash_base=>$parent, file_name=>$diff->{'file'}),3806-class=>"list"}, esc_path($diff->{'file'}));3807print"</td>\n";3808print"<td>$mode_chng</td>\n";3809print"<td class=\"link\">";3810if($actioneq'commitdiff') {3811# link to patch3812$patchno++;3813print$cgi->a({-href =>"#patch$patchno"},"patch");3814print" | ";3815}3816print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3817 hash_base=>$parent, file_name=>$diff->{'file'})},3818"blob") ." | ";3819if($have_blame) {3820print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3821 file_name=>$diff->{'file'})},3822"blame") ." | ";3823}3824print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3825 file_name=>$diff->{'file'})},3826"history");3827print"</td>\n";38283829}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3830my$mode_chnge="";3831if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3832$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3833if($from_file_typene$to_file_type) {3834$mode_chnge.=" from$from_file_typeto$to_file_type";3835}3836if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3837if($from_mode_str&&$to_mode_str) {3838$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3839}elsif($to_mode_str) {3840$mode_chnge.=" mode:$to_mode_str";3841}3842}3843$mode_chnge.="]</span>\n";3844}3845print"<td>";3846print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3847 hash_base=>$hash, file_name=>$diff->{'file'}),3848-class=>"list"}, esc_path($diff->{'file'}));3849print"</td>\n";3850print"<td>$mode_chnge</td>\n";3851print"<td class=\"link\">";3852if($actioneq'commitdiff') {3853# link to patch3854$patchno++;3855print$cgi->a({-href =>"#patch$patchno"},"patch") .3856" | ";3857}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3858# "commit" view and modified file (not onlu mode changed)3859print$cgi->a({-href => href(action=>"blobdiff",3860 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3861 hash_base=>$hash, hash_parent_base=>$parent,3862 file_name=>$diff->{'file'})},3863"diff") .3864" | ";3865}3866print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3867 hash_base=>$hash, file_name=>$diff->{'file'})},3868"blob") ." | ";3869if($have_blame) {3870print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3871 file_name=>$diff->{'file'})},3872"blame") ." | ";3873}3874print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3875 file_name=>$diff->{'file'})},3876"history");3877print"</td>\n";38783879}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3880my%status_name= ('R'=>'moved','C'=>'copied');3881my$nstatus=$status_name{$diff->{'status'}};3882my$mode_chng="";3883if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3884# mode also for directories, so we cannot use $to_mode_str3885$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3886}3887print"<td>".3888$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3889 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3890-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3891"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3892$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3893 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3894-class=>"list"}, esc_path($diff->{'from_file'})) .3895" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3896"<td class=\"link\">";3897if($actioneq'commitdiff') {3898# link to patch3899$patchno++;3900print$cgi->a({-href =>"#patch$patchno"},"patch") .3901" | ";3902}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3903# "commit" view and modified file (not only pure rename or copy)3904print$cgi->a({-href => href(action=>"blobdiff",3905 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3906 hash_base=>$hash, hash_parent_base=>$parent,3907 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3908"diff") .3909" | ";3910}3911print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3912 hash_base=>$parent, file_name=>$diff->{'to_file'})},3913"blob") ." | ";3914if($have_blame) {3915print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3916 file_name=>$diff->{'to_file'})},3917"blame") ." | ";3918}3919print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3920 file_name=>$diff->{'to_file'})},3921"history");3922print"</td>\n";39233924}# we should not encounter Unmerged (U) or Unknown (X) status3925print"</tr>\n";3926}3927print"</tbody>"if$has_header;3928print"</table>\n";3929}39303931sub git_patchset_body {3932my($fd,$difftree,$hash,@hash_parents) =@_;3933my($hash_parent) =$hash_parents[0];39343935my$is_combined= (@hash_parents>1);3936my$patch_idx=0;3937my$patch_number=0;3938my$patch_line;3939my$diffinfo;3940my$to_name;3941my(%from,%to);39423943print"<div class=\"patchset\">\n";39443945# skip to first patch3946while($patch_line= <$fd>) {3947chomp$patch_line;39483949last if($patch_line=~m/^diff /);3950}39513952 PATCH:3953while($patch_line) {39543955# parse "git diff" header line3956if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3957# $1 is from_name, which we do not use3958$to_name= unquote($2);3959$to_name=~s!^b/!!;3960}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3961# $1 is 'cc' or 'combined', which we do not use3962$to_name= unquote($2);3963}else{3964$to_name=undef;3965}39663967# check if current patch belong to current raw line3968# and parse raw git-diff line if needed3969if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3970# this is continuation of a split patch3971print"<div class=\"patch cont\">\n";3972}else{3973# advance raw git-diff output if needed3974$patch_idx++ifdefined$diffinfo;39753976# read and prepare patch information3977$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);39783979# compact combined diff output can have some patches skipped3980# find which patch (using pathname of result) we are at now;3981if($is_combined) {3982while($to_namene$diffinfo->{'to_file'}) {3983print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3984 format_diff_cc_simplified($diffinfo,@hash_parents) .3985"</div>\n";# class="patch"39863987$patch_idx++;3988$patch_number++;39893990last if$patch_idx>$#$difftree;3991$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3992}3993}39943995# modifies %from, %to hashes3996 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);39973998# this is first patch for raw difftree line with $patch_idx index3999# we index @$difftree array from 0, but number patches from 14000print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4001}40024003# git diff header4004#assert($patch_line =~ m/^diff /) if DEBUG;4005#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4006$patch_number++;4007# print "git diff" header4008print format_git_diff_header_line($patch_line,$diffinfo,4009 \%from, \%to);40104011# print extended diff header4012print"<div class=\"diff extended_header\">\n";4013 EXTENDED_HEADER:4014while($patch_line= <$fd>) {4015chomp$patch_line;40164017last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);40184019print format_extended_diff_header_line($patch_line,$diffinfo,4020 \%from, \%to);4021}4022print"</div>\n";# class="diff extended_header"40234024# from-file/to-file diff header4025if(!$patch_line) {4026print"</div>\n";# class="patch"4027last PATCH;4028}4029next PATCH if($patch_line=~m/^diff /);4030#assert($patch_line =~ m/^---/) if DEBUG;40314032my$last_patch_line=$patch_line;4033$patch_line= <$fd>;4034chomp$patch_line;4035#assert($patch_line =~ m/^\+\+\+/) if DEBUG;40364037print format_diff_from_to_header($last_patch_line,$patch_line,4038$diffinfo, \%from, \%to,4039@hash_parents);40404041# the patch itself4042 LINE:4043while($patch_line= <$fd>) {4044chomp$patch_line;40454046next PATCH if($patch_line=~m/^diff /);40474048print format_diff_line($patch_line, \%from, \%to);4049}40504051}continue{4052print"</div>\n";# class="patch"4053}40544055# for compact combined (--cc) format, with chunk and patch simpliciaction4056# patchset might be empty, but there might be unprocessed raw lines4057for(++$patch_idxif$patch_number>0;4058$patch_idx<@$difftree;4059++$patch_idx) {4060# read and prepare patch information4061$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);40624063# generate anchor for "patch" links in difftree / whatchanged part4064print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4065 format_diff_cc_simplified($diffinfo,@hash_parents) .4066"</div>\n";# class="patch"40674068$patch_number++;4069}40704071if($patch_number==0) {4072if(@hash_parents>1) {4073print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4074}else{4075print"<div class=\"diff nodifferences\">No differences found</div>\n";4076}4077}40784079print"</div>\n";# class="patchset"4080}40814082# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40834084# fills project list info (age, description, owner, forks) for each4085# project in the list, removing invalid projects from returned list4086# NOTE: modifies $projlist, but does not remove entries from it4087sub fill_project_list_info {4088my($projlist,$check_forks) =@_;4089my@projects;40904091my$show_ctags= gitweb_check_feature('ctags');4092 PROJECT:4093foreachmy$pr(@$projlist) {4094my(@activity) = git_get_last_activity($pr->{'path'});4095unless(@activity) {4096next PROJECT;4097}4098($pr->{'age'},$pr->{'age_string'}) =@activity;4099if(!defined$pr->{'descr'}) {4100my$descr= git_get_project_description($pr->{'path'}) ||"";4101$descr= to_utf8($descr);4102$pr->{'descr_long'} =$descr;4103$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4104}4105if(!defined$pr->{'owner'}) {4106$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4107}4108if($check_forks) {4109my$pname=$pr->{'path'};4110if(($pname=~s/\.git$//) &&4111($pname!~/\/$/) &&4112(-d "$projectroot/$pname")) {4113$pr->{'forks'} ="-d$projectroot/$pname";4114}else{4115$pr->{'forks'} =0;4116}4117}4118$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4119push@projects,$pr;4120}41214122return@projects;4123}41244125# print 'sort by' <th> element, generating 'sort by $name' replay link4126# if that order is not selected4127sub print_sort_th {4128my($name,$order,$header) =@_;4129$header||=ucfirst($name);41304131if($ordereq$name) {4132print"<th>$header</th>\n";4133}else{4134print"<th>".4135$cgi->a({-href => href(-replay=>1, order=>$name),4136-class=>"header"},$header) .4137"</th>\n";4138}4139}41404141sub git_project_list_body {4142# actually uses global variable $project4143my($projlist,$order,$from,$to,$extra,$no_header) =@_;41444145my$check_forks= gitweb_check_feature('forks');4146my@projects= fill_project_list_info($projlist,$check_forks);41474148$order||=$default_projects_order;4149$from=0unlessdefined$from;4150$to=$#projectsif(!defined$to||$#projects<$to);41514152my%order_info= (4153 project => { key =>'path', type =>'str'},4154 descr => { key =>'descr_long', type =>'str'},4155 owner => { key =>'owner', type =>'str'},4156 age => { key =>'age', type =>'num'}4157);4158my$oi=$order_info{$order};4159if($oi->{'type'}eq'str') {4160@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4161}else{4162@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4163}41644165my$show_ctags= gitweb_check_feature('ctags');4166if($show_ctags) {4167my%ctags;4168foreachmy$p(@projects) {4169foreachmy$ct(keys%{$p->{'ctags'}}) {4170$ctags{$ct} +=$p->{'ctags'}->{$ct};4171}4172}4173my$cloud= git_populate_project_tagcloud(\%ctags);4174print git_show_project_tagcloud($cloud,64);4175}41764177print"<table class=\"project_list\">\n";4178unless($no_header) {4179print"<tr>\n";4180if($check_forks) {4181print"<th></th>\n";4182}4183 print_sort_th('project',$order,'Project');4184 print_sort_th('descr',$order,'Description');4185 print_sort_th('owner',$order,'Owner');4186 print_sort_th('age',$order,'Last Change');4187print"<th></th>\n".# for links4188"</tr>\n";4189}4190my$alternate=1;4191my$tagfilter=$cgi->param('by_tag');4192for(my$i=$from;$i<=$to;$i++) {4193my$pr=$projects[$i];41944195next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4196next if$searchtextand not$pr->{'path'} =~/$searchtext/4197and not$pr->{'descr_long'} =~/$searchtext/;4198# Weed out forks or non-matching entries of search4199if($check_forks) {4200my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4201$forkbase="^$forkbase"if$forkbase;4202next ifnot$searchtextand not$tagfilterand$show_ctags4203and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4204}42054206if($alternate) {4207print"<tr class=\"dark\">\n";4208}else{4209print"<tr class=\"light\">\n";4210}4211$alternate^=1;4212if($check_forks) {4213print"<td>";4214if($pr->{'forks'}) {4215print"<!--$pr->{'forks'} -->\n";4216print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4217}4218print"</td>\n";4219}4220print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4221-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4222"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4223-class=>"list", -title =>$pr->{'descr_long'}},4224 esc_html($pr->{'descr'})) ."</td>\n".4225"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4226print"<td class=\"". age_class($pr->{'age'}) ."\">".4227(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4228"<td class=\"link\">".4229$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4230$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4231$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4232$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4233($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4234"</td>\n".4235"</tr>\n";4236}4237if(defined$extra) {4238print"<tr>\n";4239if($check_forks) {4240print"<td></td>\n";4241}4242print"<td colspan=\"5\">$extra</td>\n".4243"</tr>\n";4244}4245print"</table>\n";4246}42474248sub git_shortlog_body {4249# uses global variable $project4250my($commitlist,$from,$to,$refs,$extra) =@_;42514252$from=0unlessdefined$from;4253$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);42544255print"<table class=\"shortlog\">\n";4256my$alternate=1;4257for(my$i=$from;$i<=$to;$i++) {4258my%co= %{$commitlist->[$i]};4259my$commit=$co{'id'};4260my$ref= format_ref_marker($refs,$commit);4261if($alternate) {4262print"<tr class=\"dark\">\n";4263}else{4264print"<tr class=\"light\">\n";4265}4266$alternate^=1;4267# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4268print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4269 format_author_html('td', \%co,10) ."<td>";4270print format_subject_html($co{'title'},$co{'title_short'},4271 href(action=>"commit", hash=>$commit),$ref);4272print"</td>\n".4273"<td class=\"link\">".4274$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4275$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4276$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4277my$snapshot_links= format_snapshot_links($commit);4278if(defined$snapshot_links) {4279print" | ".$snapshot_links;4280}4281print"</td>\n".4282"</tr>\n";4283}4284if(defined$extra) {4285print"<tr>\n".4286"<td colspan=\"4\">$extra</td>\n".4287"</tr>\n";4288}4289print"</table>\n";4290}42914292sub git_history_body {4293# Warning: assumes constant type (blob or tree) during history4294my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;42954296$from=0unlessdefined$from;4297$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});42984299print"<table class=\"history\">\n";4300my$alternate=1;4301for(my$i=$from;$i<=$to;$i++) {4302my%co= %{$commitlist->[$i]};4303if(!%co) {4304next;4305}4306my$commit=$co{'id'};43074308my$ref= format_ref_marker($refs,$commit);43094310if($alternate) {4311print"<tr class=\"dark\">\n";4312}else{4313print"<tr class=\"light\">\n";4314}4315$alternate^=1;4316print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4317# shortlog: format_author_html('td', \%co, 10)4318 format_author_html('td', \%co,15,3) ."<td>";4319# originally git_history used chop_str($co{'title'}, 50)4320print format_subject_html($co{'title'},$co{'title_short'},4321 href(action=>"commit", hash=>$commit),$ref);4322print"</td>\n".4323"<td class=\"link\">".4324$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4325$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");43264327if($ftypeeq'blob') {4328my$blob_current= git_get_hash_by_path($hash_base,$file_name);4329my$blob_parent= git_get_hash_by_path($commit,$file_name);4330if(defined$blob_current&&defined$blob_parent&&4331$blob_currentne$blob_parent) {4332print" | ".4333$cgi->a({-href => href(action=>"blobdiff",4334 hash=>$blob_current, hash_parent=>$blob_parent,4335 hash_base=>$hash_base, hash_parent_base=>$commit,4336 file_name=>$file_name)},4337"diff to current");4338}4339}4340print"</td>\n".4341"</tr>\n";4342}4343if(defined$extra) {4344print"<tr>\n".4345"<td colspan=\"4\">$extra</td>\n".4346"</tr>\n";4347}4348print"</table>\n";4349}43504351sub git_tags_body {4352# uses global variable $project4353my($taglist,$from,$to,$extra) =@_;4354$from=0unlessdefined$from;4355$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);43564357print"<table class=\"tags\">\n";4358my$alternate=1;4359for(my$i=$from;$i<=$to;$i++) {4360my$entry=$taglist->[$i];4361my%tag=%$entry;4362my$comment=$tag{'subject'};4363my$comment_short;4364if(defined$comment) {4365$comment_short= chop_str($comment,30,5);4366}4367if($alternate) {4368print"<tr class=\"dark\">\n";4369}else{4370print"<tr class=\"light\">\n";4371}4372$alternate^=1;4373if(defined$tag{'age'}) {4374print"<td><i>$tag{'age'}</i></td>\n";4375}else{4376print"<td></td>\n";4377}4378print"<td>".4379$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4380-class=>"list name"}, esc_html($tag{'name'})) .4381"</td>\n".4382"<td>";4383if(defined$comment) {4384print format_subject_html($comment,$comment_short,4385 href(action=>"tag", hash=>$tag{'id'}));4386}4387print"</td>\n".4388"<td class=\"selflink\">";4389if($tag{'type'}eq"tag") {4390print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4391}else{4392print" ";4393}4394print"</td>\n".4395"<td class=\"link\">"." | ".4396$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4397if($tag{'reftype'}eq"commit") {4398print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4399" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4400}elsif($tag{'reftype'}eq"blob") {4401print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4402}4403print"</td>\n".4404"</tr>";4405}4406if(defined$extra) {4407print"<tr>\n".4408"<td colspan=\"5\">$extra</td>\n".4409"</tr>\n";4410}4411print"</table>\n";4412}44134414sub git_heads_body {4415# uses global variable $project4416my($headlist,$head,$from,$to,$extra) =@_;4417$from=0unlessdefined$from;4418$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);44194420print"<table class=\"heads\">\n";4421my$alternate=1;4422for(my$i=$from;$i<=$to;$i++) {4423my$entry=$headlist->[$i];4424my%ref=%$entry;4425my$curr=$ref{'id'}eq$head;4426if($alternate) {4427print"<tr class=\"dark\">\n";4428}else{4429print"<tr class=\"light\">\n";4430}4431$alternate^=1;4432print"<td><i>$ref{'age'}</i></td>\n".4433($curr?"<td class=\"current_head\">":"<td>") .4434$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4435-class=>"list name"},esc_html($ref{'name'})) .4436"</td>\n".4437"<td class=\"link\">".4438$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4439$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4440$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4441"</td>\n".4442"</tr>";4443}4444if(defined$extra) {4445print"<tr>\n".4446"<td colspan=\"3\">$extra</td>\n".4447"</tr>\n";4448}4449print"</table>\n";4450}44514452sub git_search_grep_body {4453my($commitlist,$from,$to,$extra) =@_;4454$from=0unlessdefined$from;4455$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);44564457print"<table class=\"commit_search\">\n";4458my$alternate=1;4459for(my$i=$from;$i<=$to;$i++) {4460my%co= %{$commitlist->[$i]};4461if(!%co) {4462next;4463}4464my$commit=$co{'id'};4465if($alternate) {4466print"<tr class=\"dark\">\n";4467}else{4468print"<tr class=\"light\">\n";4469}4470$alternate^=1;4471print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4472 format_author_html('td', \%co,15,5) .4473"<td>".4474$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4475-class=>"list subject"},4476 chop_and_escape_str($co{'title'},50) ."<br/>");4477my$comment=$co{'comment'};4478foreachmy$line(@$comment) {4479if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4480my($lead,$match,$trail) = ($1,$2,$3);4481$match= chop_str($match,70,5,'center');4482my$contextlen=int((80-length($match))/2);4483$contextlen=30if($contextlen>30);4484$lead= chop_str($lead,$contextlen,10,'left');4485$trail= chop_str($trail,$contextlen,10,'right');44864487$lead= esc_html($lead);4488$match= esc_html($match);4489$trail= esc_html($trail);44904491print"$lead<span class=\"match\">$match</span>$trail<br />";4492}4493}4494print"</td>\n".4495"<td class=\"link\">".4496$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4497" | ".4498$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4499" | ".4500$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4501print"</td>\n".4502"</tr>\n";4503}4504if(defined$extra) {4505print"<tr>\n".4506"<td colspan=\"3\">$extra</td>\n".4507"</tr>\n";4508}4509print"</table>\n";4510}45114512## ======================================================================4513## ======================================================================4514## actions45154516sub git_project_list {4517my$order=$input_params{'order'};4518if(defined$order&&$order!~m/none|project|descr|owner|age/) {4519 die_error(400,"Unknown order parameter");4520}45214522my@list= git_get_projects_list();4523if(!@list) {4524 die_error(404,"No projects found");4525}45264527 git_header_html();4528if(-f $home_text) {4529print"<div class=\"index_include\">\n";4530 insert_file($home_text);4531print"</div>\n";4532}4533print$cgi->startform(-method=>"get") .4534"<p class=\"projsearch\">Search:\n".4535$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4536"</p>".4537$cgi->end_form() ."\n";4538 git_project_list_body(\@list,$order);4539 git_footer_html();4540}45414542sub git_forks {4543my$order=$input_params{'order'};4544if(defined$order&&$order!~m/none|project|descr|owner|age/) {4545 die_error(400,"Unknown order parameter");4546}45474548my@list= git_get_projects_list($project);4549if(!@list) {4550 die_error(404,"No forks found");4551}45524553 git_header_html();4554 git_print_page_nav('','');4555 git_print_header_div('summary',"$projectforks");4556 git_project_list_body(\@list,$order);4557 git_footer_html();4558}45594560sub git_project_index {4561my@projects= git_get_projects_list($project);45624563print$cgi->header(4564-type =>'text/plain',4565-charset =>'utf-8',4566-content_disposition =>'inline; filename="index.aux"');45674568foreachmy$pr(@projects) {4569if(!exists$pr->{'owner'}) {4570$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4571}45724573my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4574# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4575$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4576$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4577$path=~s/ /\+/g;4578$owner=~s/ /\+/g;45794580print"$path$owner\n";4581}4582}45834584sub git_summary {4585my$descr= git_get_project_description($project) ||"none";4586my%co= parse_commit("HEAD");4587my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4588my$head=$co{'id'};45894590my$owner= git_get_project_owner($project);45914592my$refs= git_get_references();4593# These get_*_list functions return one more to allow us to see if4594# there are more ...4595my@taglist= git_get_tags_list(16);4596my@headlist= git_get_heads_list(16);4597my@forklist;4598my$check_forks= gitweb_check_feature('forks');45994600if($check_forks) {4601@forklist= git_get_projects_list($project);4602}46034604 git_header_html();4605 git_print_page_nav('summary','',$head);46064607print"<div class=\"title\"> </div>\n";4608print"<table class=\"projects_list\">\n".4609"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4610"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4611if(defined$cd{'rfc2822'}) {4612print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4613}46144615# use per project git URL list in $projectroot/$project/cloneurl4616# or make project git URL from git base URL and project name4617my$url_tag="URL";4618my@url_list= git_get_project_url_list($project);4619@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4620foreachmy$git_url(@url_list) {4621next unless$git_url;4622print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4623$url_tag="";4624}46254626# Tag cloud4627my$show_ctags= gitweb_check_feature('ctags');4628if($show_ctags) {4629my$ctags= git_get_project_ctags($project);4630my$cloud= git_populate_project_tagcloud($ctags);4631print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4632print"</td>\n<td>"unless%$ctags;4633print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4634print"</td>\n<td>"if%$ctags;4635print git_show_project_tagcloud($cloud,48);4636print"</td></tr>";4637}46384639print"</table>\n";46404641# If XSS prevention is on, we don't include README.html.4642# TODO: Allow a readme in some safe format.4643if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4644print"<div class=\"title\">readme</div>\n".4645"<div class=\"readme\">\n";4646 insert_file("$projectroot/$project/README.html");4647print"\n</div>\n";# class="readme"4648}46494650# we need to request one more than 16 (0..15) to check if4651# those 16 are all4652my@commitlist=$head? parse_commits($head,17) : ();4653if(@commitlist) {4654 git_print_header_div('shortlog');4655 git_shortlog_body(\@commitlist,0,15,$refs,4656$#commitlist<=15?undef:4657$cgi->a({-href => href(action=>"shortlog")},"..."));4658}46594660if(@taglist) {4661 git_print_header_div('tags');4662 git_tags_body(\@taglist,0,15,4663$#taglist<=15?undef:4664$cgi->a({-href => href(action=>"tags")},"..."));4665}46664667if(@headlist) {4668 git_print_header_div('heads');4669 git_heads_body(\@headlist,$head,0,15,4670$#headlist<=15?undef:4671$cgi->a({-href => href(action=>"heads")},"..."));4672}46734674if(@forklist) {4675 git_print_header_div('forks');4676 git_project_list_body(\@forklist,'age',0,15,4677$#forklist<=15?undef:4678$cgi->a({-href => href(action=>"forks")},"..."),4679'no_header');4680}46814682 git_footer_html();4683}46844685sub git_tag {4686my$head= git_get_head_hash($project);4687 git_header_html();4688 git_print_page_nav('','',$head,undef,$head);4689my%tag= parse_tag($hash);46904691if(!%tag) {4692 die_error(404,"Unknown tag object");4693}46944695 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4696print"<div class=\"title_text\">\n".4697"<table class=\"object_header\">\n".4698"<tr>\n".4699"<td>object</td>\n".4700"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4701$tag{'object'}) ."</td>\n".4702"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4703$tag{'type'}) ."</td>\n".4704"</tr>\n";4705if(defined($tag{'author'})) {4706 git_print_authorship_rows(\%tag,'author');4707}4708print"</table>\n\n".4709"</div>\n";4710print"<div class=\"page_body\">";4711my$comment=$tag{'comment'};4712foreachmy$line(@$comment) {4713chomp$line;4714print esc_html($line, -nbsp=>1) ."<br/>\n";4715}4716print"</div>\n";4717 git_footer_html();4718}47194720sub git_blame {4721# permissions4722 gitweb_check_feature('blame')4723or die_error(403,"Blame view not allowed");47244725# error checking4726 die_error(400,"No file name given")unless$file_name;4727$hash_base||= git_get_head_hash($project);4728 die_error(404,"Couldn't find base commit")unless$hash_base;4729my%co= parse_commit($hash_base)4730or die_error(404,"Commit not found");4731my$ftype="blob";4732if(!defined$hash) {4733$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4734or die_error(404,"Error looking up file");4735}else{4736$ftype= git_get_type($hash);4737if($ftype!~"blob") {4738 die_error(400,"Object is not a blob");4739}4740}47414742# run git-blame --porcelain4743open my$fd,"-|", git_cmd(),"blame",'-p',4744$hash_base,'--',$file_name4745or die_error(500,"Open git-blame failed");47464747# page header4748 git_header_html();4749my$formats_nav=4750$cgi->a({-href => href(action=>"blob", -replay=>1)},4751"blob") .4752" | ".4753$cgi->a({-href => href(action=>"history", -replay=>1)},4754"history") .4755" | ".4756$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4757"HEAD");4758 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4759 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4760 git_print_page_path($file_name,$ftype,$hash_base);47614762# page body4763my@rev_color=qw(light2 dark2);4764my$num_colors=scalar(@rev_color);4765my$current_color=0;4766my%metainfo= ();47674768print<<HTML;4769<div class="page_body">4770<table class="blame">4771<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4772HTML4773 LINE:4774while(my$line= <$fd>) {4775chomp$line;4776# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4777# no <lines in group> for subsequent lines in group of lines4778my($full_rev,$orig_lineno,$lineno,$group_size) =4779($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4780if(!exists$metainfo{$full_rev}) {4781$metainfo{$full_rev} = {};4782}4783my$meta=$metainfo{$full_rev};4784my$data;4785while($data= <$fd>) {4786chomp$data;4787last if($data=~s/^\t//);# contents of line4788if($data=~/^(\S+) (.*)$/) {4789$meta->{$1} =$2;4790}4791}4792my$short_rev=substr($full_rev,0,8);4793my$author=$meta->{'author'};4794my%date=4795 parse_date($meta->{'author-time'},$meta->{'author-tz'});4796my$date=$date{'iso-tz'};4797if($group_size) {4798$current_color= ($current_color+1) %$num_colors;4799}4800print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4801if($group_size) {4802print"<td class=\"sha1\"";4803print" title=\"". esc_html($author) .",$date\"";4804print" rowspan=\"$group_size\""if($group_size>1);4805print">";4806print$cgi->a({-href => href(action=>"commit",4807 hash=>$full_rev,4808 file_name=>$file_name)},4809 esc_html($short_rev));4810print"</td>\n";4811}4812my$parent_commit;4813if(!exists$meta->{'parent'}) {4814open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4815or die_error(500,"Open git-rev-parse failed");4816$parent_commit= <$dd>;4817close$dd;4818chomp($parent_commit);4819$meta->{'parent'} =$parent_commit;4820}else{4821$parent_commit=$meta->{'parent'};4822}4823my$blamed= href(action =>'blame',4824 file_name =>$meta->{'filename'},4825 hash_base =>$parent_commit);4826print"<td class=\"linenr\">";4827print$cgi->a({ -href =>"$blamed#l$orig_lineno",4828-class=>"linenr"},4829 esc_html($lineno));4830print"</td>";4831print"<td class=\"pre\">". esc_html($data) ."</td>\n";4832print"</tr>\n";4833}4834print"</table>\n";4835print"</div>";4836close$fd4837or print"Reading blob failed\n";48384839# page footer4840 git_footer_html();4841}48424843sub git_tags {4844my$head= git_get_head_hash($project);4845 git_header_html();4846 git_print_page_nav('','',$head,undef,$head);4847 git_print_header_div('summary',$project);48484849my@tagslist= git_get_tags_list();4850if(@tagslist) {4851 git_tags_body(\@tagslist);4852}4853 git_footer_html();4854}48554856sub git_heads {4857my$head= git_get_head_hash($project);4858 git_header_html();4859 git_print_page_nav('','',$head,undef,$head);4860 git_print_header_div('summary',$project);48614862my@headslist= git_get_heads_list();4863if(@headslist) {4864 git_heads_body(\@headslist,$head);4865}4866 git_footer_html();4867}48684869sub git_blob_plain {4870my$type=shift;4871my$expires;48724873if(!defined$hash) {4874if(defined$file_name) {4875my$base=$hash_base|| git_get_head_hash($project);4876$hash= git_get_hash_by_path($base,$file_name,"blob")4877or die_error(404,"Cannot find file");4878}else{4879 die_error(400,"No file name defined");4880}4881}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4882# blobs defined by non-textual hash id's can be cached4883$expires="+1d";4884}48854886open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4887or die_error(500,"Open git-cat-file blob '$hash' failed");48884889# content-type (can include charset)4890$type= blob_contenttype($fd,$file_name,$type);48914892# "save as" filename, even when no $file_name is given4893my$save_as="$hash";4894if(defined$file_name) {4895$save_as=$file_name;4896}elsif($type=~m/^text\//) {4897$save_as.='.txt';4898}48994900# With XSS prevention on, blobs of all types except a few known safe4901# ones are served with "Content-Disposition: attachment" to make sure4902# they don't run in our security domain. For certain image types,4903# blob view writes an <img> tag referring to blob_plain view, and we4904# want to be sure not to break that by serving the image as an4905# attachment (though Firefox 3 doesn't seem to care).4906my$sandbox=$prevent_xss&&4907$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;49084909print$cgi->header(4910-type =>$type,4911-expires =>$expires,4912-content_disposition =>4913($sandbox?'attachment':'inline')4914.'; filename="'.$save_as.'"');4915local$/=undef;4916binmode STDOUT,':raw';4917print<$fd>;4918binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4919close$fd;4920}49214922sub git_blob {4923my$expires;49244925if(!defined$hash) {4926if(defined$file_name) {4927my$base=$hash_base|| git_get_head_hash($project);4928$hash= git_get_hash_by_path($base,$file_name,"blob")4929or die_error(404,"Cannot find file");4930}else{4931 die_error(400,"No file name defined");4932}4933}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4934# blobs defined by non-textual hash id's can be cached4935$expires="+1d";4936}49374938my$have_blame= gitweb_check_feature('blame');4939open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4940or die_error(500,"Couldn't cat$file_name,$hash");4941my$mimetype= blob_mimetype($fd,$file_name);4942if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4943close$fd;4944return git_blob_plain($mimetype);4945}4946# we can have blame only for text/* mimetype4947$have_blame&&= ($mimetype=~m!^text/!);49484949 git_header_html(undef,$expires);4950my$formats_nav='';4951if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4952if(defined$file_name) {4953if($have_blame) {4954$formats_nav.=4955$cgi->a({-href => href(action=>"blame", -replay=>1)},4956"blame") .4957" | ";4958}4959$formats_nav.=4960$cgi->a({-href => href(action=>"history", -replay=>1)},4961"history") .4962" | ".4963$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4964"raw") .4965" | ".4966$cgi->a({-href => href(action=>"blob",4967 hash_base=>"HEAD", file_name=>$file_name)},4968"HEAD");4969}else{4970$formats_nav.=4971$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4972"raw");4973}4974 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4975 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4976}else{4977print"<div class=\"page_nav\">\n".4978"<br/><br/></div>\n".4979"<div class=\"title\">$hash</div>\n";4980}4981 git_print_page_path($file_name,"blob",$hash_base);4982print"<div class=\"page_body\">\n";4983if($mimetype=~m!^image/!) {4984print qq!<img type="$mimetype"!;4985if($file_name) {4986print qq! alt="$file_name" title="$file_name"!;4987}4988print qq! src="! .4989 href(action=>"blob_plain", hash=>$hash,4990 hash_base=>$hash_base, file_name=>$file_name) .4991 qq!"/>\n!;4992}else{4993my$nr;4994while(my$line= <$fd>) {4995chomp$line;4996$nr++;4997$line= untabify($line);4998printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4999$nr,$nr,$nr, esc_html($line, -nbsp=>1);5000}5001}5002close$fd5003or print"Reading blob failed.\n";5004print"</div>";5005 git_footer_html();5006}50075008sub git_tree {5009if(!defined$hash_base) {5010$hash_base="HEAD";5011}5012if(!defined$hash) {5013if(defined$file_name) {5014$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5015}else{5016$hash=$hash_base;5017}5018}5019 die_error(404,"No such tree")unlessdefined($hash);50205021my@entries= ();5022{5023local$/="\0";5024open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash5025or die_error(500,"Open git-ls-tree failed");5026@entries=map{chomp;$_} <$fd>;5027close$fd5028or die_error(404,"Reading tree failed");5029}50305031my$refs= git_get_references();5032my$ref= format_ref_marker($refs,$hash_base);5033 git_header_html();5034my$basedir='';5035my$have_blame= gitweb_check_feature('blame');5036if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5037my@views_nav= ();5038if(defined$file_name) {5039push@views_nav,5040$cgi->a({-href => href(action=>"history", -replay=>1)},5041"history"),5042$cgi->a({-href => href(action=>"tree",5043 hash_base=>"HEAD", file_name=>$file_name)},5044"HEAD"),5045}5046my$snapshot_links= format_snapshot_links($hash);5047if(defined$snapshot_links) {5048# FIXME: Should be available when we have no hash base as well.5049push@views_nav,$snapshot_links;5050}5051 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));5052 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5053}else{5054undef$hash_base;5055print"<div class=\"page_nav\">\n";5056print"<br/><br/></div>\n";5057print"<div class=\"title\">$hash</div>\n";5058}5059if(defined$file_name) {5060$basedir=$file_name;5061if($basedirne''&&substr($basedir, -1)ne'/') {5062$basedir.='/';5063}5064 git_print_page_path($file_name,'tree',$hash_base);5065}5066print"<div class=\"page_body\">\n";5067print"<table class=\"tree\">\n";5068my$alternate=1;5069# '..' (top directory) link if possible5070if(defined$hash_base&&5071defined$file_name&&$file_name=~m![^/]+$!) {5072if($alternate) {5073print"<tr class=\"dark\">\n";5074}else{5075print"<tr class=\"light\">\n";5076}5077$alternate^=1;50785079my$up=$file_name;5080$up=~s!/?[^/]+$!!;5081undef$upunless$up;5082# based on git_print_tree_entry5083print'<td class="mode">'. mode_str('040000') ."</td>\n";5084print'<td class="list">';5085print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,5086 file_name=>$up)},5087"..");5088print"</td>\n";5089print"<td class=\"link\"></td>\n";50905091print"</tr>\n";5092}5093foreachmy$line(@entries) {5094my%t= parse_ls_tree_line($line, -z =>1);50955096if($alternate) {5097print"<tr class=\"dark\">\n";5098}else{5099print"<tr class=\"light\">\n";5100}5101$alternate^=1;51025103 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);51045105print"</tr>\n";5106}5107print"</table>\n".5108"</div>";5109 git_footer_html();5110}51115112sub git_snapshot {5113my$format=$input_params{'snapshot_format'};5114if(!@snapshot_fmts) {5115 die_error(403,"Snapshots not allowed");5116}5117# default to first supported snapshot format5118$format||=$snapshot_fmts[0];5119if($format!~m/^[a-z0-9]+$/) {5120 die_error(400,"Invalid snapshot format parameter");5121}elsif(!exists($known_snapshot_formats{$format})) {5122 die_error(400,"Unknown snapshot format");5123}elsif(!grep($_eq$format,@snapshot_fmts)) {5124 die_error(403,"Unsupported snapshot format");5125}51265127if(!defined$hash) {5128$hash= git_get_head_hash($project);5129}51305131my$name=$project;5132$name=~ s,([^/])/*\.git$,$1,;5133$name= basename($name);5134my$filename= to_utf8($name);5135$name=~s/\047/\047\\\047\047/g;5136my$cmd;5137$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5138$cmd= quote_command(5139 git_cmd(),'archive',5140"--format=$known_snapshot_formats{$format}{'format'}",5141"--prefix=$name/",$hash);5142if(exists$known_snapshot_formats{$format}{'compressor'}) {5143$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5144}51455146print$cgi->header(5147-type =>$known_snapshot_formats{$format}{'type'},5148-content_disposition =>'inline; filename="'."$filename".'"',5149-status =>'200 OK');51505151open my$fd,"-|",$cmd5152or die_error(500,"Execute git-archive failed");5153binmode STDOUT,':raw';5154print<$fd>;5155binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5156close$fd;5157}51585159sub git_log {5160my$head= git_get_head_hash($project);5161if(!defined$hash) {5162$hash=$head;5163}5164if(!defined$page) {5165$page=0;5166}5167my$refs= git_get_references();51685169my@commitlist= parse_commits($hash,101, (100*$page));51705171my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);51725173my($patch_max) = gitweb_get_feature('patches');5174if($patch_max) {5175if($patch_max<0||@commitlist<=$patch_max) {5176$paging_nav.=" ⋅ ".5177$cgi->a({-href => href(action=>"patches", -replay=>1)},5178"patches");5179}5180}51815182 git_header_html();5183 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);51845185if(!@commitlist) {5186my%co= parse_commit($hash);51875188 git_print_header_div('summary',$project);5189print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5190}5191my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5192for(my$i=0;$i<=$to;$i++) {5193my%co= %{$commitlist[$i]};5194next if!%co;5195my$commit=$co{'id'};5196my$ref= format_ref_marker($refs,$commit);5197my%ad= parse_date($co{'author_epoch'});5198 git_print_header_div('commit',5199"<span class=\"age\">$co{'age_string'}</span>".5200 esc_html($co{'title'}) .$ref,5201$commit);5202print"<div class=\"title_text\">\n".5203"<div class=\"log_link\">\n".5204$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5205" | ".5206$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5207" | ".5208$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5209"<br/>\n".5210"</div>\n";5211 git_print_authorship(\%co, -tag =>'span');5212print"<br/>\n</div>\n";52135214print"<div class=\"log_body\">\n";5215 git_print_log($co{'comment'}, -final_empty_line=>1);5216print"</div>\n";5217}5218if($#commitlist>=100) {5219print"<div class=\"page_nav\">\n";5220print$cgi->a({-href => href(-replay=>1, page=>$page+1),5221-accesskey =>"n", -title =>"Alt-n"},"next");5222print"</div>\n";5223}5224 git_footer_html();5225}52265227sub git_commit {5228$hash||=$hash_base||"HEAD";5229my%co= parse_commit($hash)5230or die_error(404,"Unknown commit object");52315232my$parent=$co{'parent'};5233my$parents=$co{'parents'};# listref52345235# we need to prepare $formats_nav before any parameter munging5236my$formats_nav;5237if(!defined$parent) {5238# --root commitdiff5239$formats_nav.='(initial)';5240}elsif(@$parents==1) {5241# single parent commit5242$formats_nav.=5243'(parent: '.5244$cgi->a({-href => href(action=>"commit",5245 hash=>$parent)},5246 esc_html(substr($parent,0,7))) .5247')';5248}else{5249# merge commit5250$formats_nav.=5251'(merge: '.5252join(' ',map{5253$cgi->a({-href => href(action=>"commit",5254 hash=>$_)},5255 esc_html(substr($_,0,7)));5256}@$parents) .5257')';5258}5259if(gitweb_check_feature('patches')) {5260$formats_nav.=" | ".5261$cgi->a({-href => href(action=>"patch", -replay=>1)},5262"patch");5263}52645265if(!defined$parent) {5266$parent="--root";5267}5268my@difftree;5269open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5270@diff_opts,5271(@$parents<=1?$parent:'-c'),5272$hash,"--"5273or die_error(500,"Open git-diff-tree failed");5274@difftree=map{chomp;$_} <$fd>;5275close$fdor die_error(404,"Reading git-diff-tree failed");52765277# non-textual hash id's can be cached5278my$expires;5279if($hash=~m/^[0-9a-fA-F]{40}$/) {5280$expires="+1d";5281}5282my$refs= git_get_references();5283my$ref= format_ref_marker($refs,$co{'id'});52845285 git_header_html(undef,$expires);5286 git_print_page_nav('commit','',5287$hash,$co{'tree'},$hash,5288$formats_nav);52895290if(defined$co{'parent'}) {5291 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5292}else{5293 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5294}5295print"<div class=\"title_text\">\n".5296"<table class=\"object_header\">\n";5297 git_print_authorship_rows(\%co);5298print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5299print"<tr>".5300"<td>tree</td>".5301"<td class=\"sha1\">".5302$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5303class=>"list"},$co{'tree'}) .5304"</td>".5305"<td class=\"link\">".5306$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5307"tree");5308my$snapshot_links= format_snapshot_links($hash);5309if(defined$snapshot_links) {5310print" | ".$snapshot_links;5311}5312print"</td>".5313"</tr>\n";53145315foreachmy$par(@$parents) {5316print"<tr>".5317"<td>parent</td>".5318"<td class=\"sha1\">".5319$cgi->a({-href => href(action=>"commit", hash=>$par),5320class=>"list"},$par) .5321"</td>".5322"<td class=\"link\">".5323$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5324" | ".5325$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5326"</td>".5327"</tr>\n";5328}5329print"</table>".5330"</div>\n";53315332print"<div class=\"page_body\">\n";5333 git_print_log($co{'comment'});5334print"</div>\n";53355336 git_difftree_body(\@difftree,$hash,@$parents);53375338 git_footer_html();5339}53405341sub git_object {5342# object is defined by:5343# - hash or hash_base alone5344# - hash_base and file_name5345my$type;53465347# - hash or hash_base alone5348if($hash|| ($hash_base&& !defined$file_name)) {5349my$object_id=$hash||$hash_base;53505351open my$fd,"-|", quote_command(5352 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5353or die_error(404,"Object does not exist");5354$type= <$fd>;5355chomp$type;5356close$fd5357or die_error(404,"Object does not exist");53585359# - hash_base and file_name5360}elsif($hash_base&&defined$file_name) {5361$file_name=~ s,/+$,,;53625363system(git_cmd(),"cat-file",'-e',$hash_base) ==05364or die_error(404,"Base object does not exist");53655366# here errors should not hapen5367open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5368or die_error(500,"Open git-ls-tree failed");5369my$line= <$fd>;5370close$fd;53715372#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5373unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5374 die_error(404,"File or directory for given base does not exist");5375}5376$type=$2;5377$hash=$3;5378}else{5379 die_error(400,"Not enough information to find object");5380}53815382print$cgi->redirect(-uri => href(action=>$type, -full=>1,5383 hash=>$hash, hash_base=>$hash_base,5384 file_name=>$file_name),5385-status =>'302 Found');5386}53875388sub git_blobdiff {5389my$format=shift||'html';53905391my$fd;5392my@difftree;5393my%diffinfo;5394my$expires;53955396# preparing $fd and %diffinfo for git_patchset_body5397# new style URI5398if(defined$hash_base&&defined$hash_parent_base) {5399if(defined$file_name) {5400# read raw output5401open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5402$hash_parent_base,$hash_base,5403"--", (defined$file_parent?$file_parent: ()),$file_name5404or die_error(500,"Open git-diff-tree failed");5405@difftree=map{chomp;$_} <$fd>;5406close$fd5407or die_error(404,"Reading git-diff-tree failed");5408@difftree5409or die_error(404,"Blob diff not found");54105411}elsif(defined$hash&&5412$hash=~/[0-9a-fA-F]{40}/) {5413# try to find filename from $hash54145415# read filtered raw output5416open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5417$hash_parent_base,$hash_base,"--"5418or die_error(500,"Open git-diff-tree failed");5419@difftree=5420# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5421# $hash == to_id5422grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5423map{chomp;$_} <$fd>;5424close$fd5425or die_error(404,"Reading git-diff-tree failed");5426@difftree5427or die_error(404,"Blob diff not found");54285429}else{5430 die_error(400,"Missing one of the blob diff parameters");5431}54325433if(@difftree>1) {5434 die_error(400,"Ambiguous blob diff specification");5435}54365437%diffinfo= parse_difftree_raw_line($difftree[0]);5438$file_parent||=$diffinfo{'from_file'} ||$file_name;5439$file_name||=$diffinfo{'to_file'};54405441$hash_parent||=$diffinfo{'from_id'};5442$hash||=$diffinfo{'to_id'};54435444# non-textual hash id's can be cached5445if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5446$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5447$expires='+1d';5448}54495450# open patch output5451open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5452'-p', ($formateq'html'?"--full-index": ()),5453$hash_parent_base,$hash_base,5454"--", (defined$file_parent?$file_parent: ()),$file_name5455or die_error(500,"Open git-diff-tree failed");5456}54575458# old/legacy style URI -- not generated anymore since 1.4.3.5459if(!%diffinfo) {5460 die_error('404 Not Found',"Missing one of the blob diff parameters")5461}54625463# header5464if($formateq'html') {5465my$formats_nav=5466$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5467"raw");5468 git_header_html(undef,$expires);5469if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5470 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5471 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5472}else{5473print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5474print"<div class=\"title\">$hashvs$hash_parent</div>\n";5475}5476if(defined$file_name) {5477 git_print_page_path($file_name,"blob",$hash_base);5478}else{5479print"<div class=\"page_path\"></div>\n";5480}54815482}elsif($formateq'plain') {5483print$cgi->header(5484-type =>'text/plain',5485-charset =>'utf-8',5486-expires =>$expires,5487-content_disposition =>'inline; filename="'."$file_name".'.patch"');54885489print"X-Git-Url: ".$cgi->self_url() ."\n\n";54905491}else{5492 die_error(400,"Unknown blobdiff format");5493}54945495# patch5496if($formateq'html') {5497print"<div class=\"page_body\">\n";54985499 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5500close$fd;55015502print"</div>\n";# class="page_body"5503 git_footer_html();55045505}else{5506while(my$line= <$fd>) {5507$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5508$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;55095510print$line;55115512last if$line=~m!^\+\+\+!;5513}5514local$/=undef;5515print<$fd>;5516close$fd;5517}5518}55195520sub git_blobdiff_plain {5521 git_blobdiff('plain');5522}55235524sub git_commitdiff {5525my%params=@_;5526my$format=$params{-format} ||'html';55275528my($patch_max) = gitweb_get_feature('patches');5529if($formateq'patch') {5530 die_error(403,"Patch view not allowed")unless$patch_max;5531}55325533$hash||=$hash_base||"HEAD";5534my%co= parse_commit($hash)5535or die_error(404,"Unknown commit object");55365537# choose format for commitdiff for merge5538if(!defined$hash_parent&& @{$co{'parents'}} >1) {5539$hash_parent='--cc';5540}5541# we need to prepare $formats_nav before almost any parameter munging5542my$formats_nav;5543if($formateq'html') {5544$formats_nav=5545$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5546"raw");5547if($patch_max) {5548$formats_nav.=" | ".5549$cgi->a({-href => href(action=>"patch", -replay=>1)},5550"patch");5551}55525553if(defined$hash_parent&&5554$hash_parentne'-c'&&$hash_parentne'--cc') {5555# commitdiff with two commits given5556my$hash_parent_short=$hash_parent;5557if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5558$hash_parent_short=substr($hash_parent,0,7);5559}5560$formats_nav.=5561' (from';5562for(my$i=0;$i< @{$co{'parents'}};$i++) {5563if($co{'parents'}[$i]eq$hash_parent) {5564$formats_nav.=' parent '. ($i+1);5565last;5566}5567}5568$formats_nav.=': '.5569$cgi->a({-href => href(action=>"commitdiff",5570 hash=>$hash_parent)},5571 esc_html($hash_parent_short)) .5572')';5573}elsif(!$co{'parent'}) {5574# --root commitdiff5575$formats_nav.=' (initial)';5576}elsif(scalar@{$co{'parents'}} ==1) {5577# single parent commit5578$formats_nav.=5579' (parent: '.5580$cgi->a({-href => href(action=>"commitdiff",5581 hash=>$co{'parent'})},5582 esc_html(substr($co{'parent'},0,7))) .5583')';5584}else{5585# merge commit5586if($hash_parenteq'--cc') {5587$formats_nav.=' | '.5588$cgi->a({-href => href(action=>"commitdiff",5589 hash=>$hash, hash_parent=>'-c')},5590'combined');5591}else{# $hash_parent eq '-c'5592$formats_nav.=' | '.5593$cgi->a({-href => href(action=>"commitdiff",5594 hash=>$hash, hash_parent=>'--cc')},5595'compact');5596}5597$formats_nav.=5598' (merge: '.5599join(' ',map{5600$cgi->a({-href => href(action=>"commitdiff",5601 hash=>$_)},5602 esc_html(substr($_,0,7)));5603} @{$co{'parents'}} ) .5604')';5605}5606}56075608my$hash_parent_param=$hash_parent;5609if(!defined$hash_parent_param) {5610# --cc for multiple parents, --root for parentless5611$hash_parent_param=5612@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5613}56145615# read commitdiff5616my$fd;5617my@difftree;5618if($formateq'html') {5619open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5620"--no-commit-id","--patch-with-raw","--full-index",5621$hash_parent_param,$hash,"--"5622or die_error(500,"Open git-diff-tree failed");56235624while(my$line= <$fd>) {5625chomp$line;5626# empty line ends raw part of diff-tree output5627last unless$line;5628push@difftree,scalar parse_difftree_raw_line($line);5629}56305631}elsif($formateq'plain') {5632open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5633'-p',$hash_parent_param,$hash,"--"5634or die_error(500,"Open git-diff-tree failed");5635}elsif($formateq'patch') {5636# For commit ranges, we limit the output to the number of5637# patches specified in the 'patches' feature.5638# For single commits, we limit the output to a single patch,5639# diverging from the git-format-patch default.5640my@commit_spec= ();5641if($hash_parent) {5642if($patch_max>0) {5643push@commit_spec,"-$patch_max";5644}5645push@commit_spec,'-n',"$hash_parent..$hash";5646}else{5647if($params{-single}) {5648push@commit_spec,'-1';5649}else{5650if($patch_max>0) {5651push@commit_spec,"-$patch_max";5652}5653push@commit_spec,"-n";5654}5655push@commit_spec,'--root',$hash;5656}5657open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5658'--stdout',@commit_spec5659or die_error(500,"Open git-format-patch failed");5660}else{5661 die_error(400,"Unknown commitdiff format");5662}56635664# non-textual hash id's can be cached5665my$expires;5666if($hash=~m/^[0-9a-fA-F]{40}$/) {5667$expires="+1d";5668}56695670# write commit message5671if($formateq'html') {5672my$refs= git_get_references();5673my$ref= format_ref_marker($refs,$co{'id'});56745675 git_header_html(undef,$expires);5676 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5677 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5678print"<div class=\"title_text\">\n".5679"<table class=\"object_header\">\n";5680 git_print_authorship_rows(\%co);5681print"</table>".5682"</div>\n";5683print"<div class=\"page_body\">\n";5684if(@{$co{'comment'}} >1) {5685print"<div class=\"log\">\n";5686 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5687print"</div>\n";# class="log"5688}56895690}elsif($formateq'plain') {5691my$refs= git_get_references("tags");5692my$tagname= git_get_rev_name_tags($hash);5693my$filename= basename($project) ."-$hash.patch";56945695print$cgi->header(5696-type =>'text/plain',5697-charset =>'utf-8',5698-expires =>$expires,5699-content_disposition =>'inline; filename="'."$filename".'"');5700my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5701print"From: ". to_utf8($co{'author'}) ."\n";5702print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5703print"Subject: ". to_utf8($co{'title'}) ."\n";57045705print"X-Git-Tag:$tagname\n"if$tagname;5706print"X-Git-Url: ".$cgi->self_url() ."\n\n";57075708foreachmy$line(@{$co{'comment'}}) {5709print to_utf8($line) ."\n";5710}5711print"---\n\n";5712}elsif($formateq'patch') {5713my$filename= basename($project) ."-$hash.patch";57145715print$cgi->header(5716-type =>'text/plain',5717-charset =>'utf-8',5718-expires =>$expires,5719-content_disposition =>'inline; filename="'."$filename".'"');5720}57215722# write patch5723if($formateq'html') {5724my$use_parents= !defined$hash_parent||5725$hash_parenteq'-c'||$hash_parenteq'--cc';5726 git_difftree_body(\@difftree,$hash,5727$use_parents? @{$co{'parents'}} :$hash_parent);5728print"<br/>\n";57295730 git_patchset_body($fd, \@difftree,$hash,5731$use_parents? @{$co{'parents'}} :$hash_parent);5732close$fd;5733print"</div>\n";# class="page_body"5734 git_footer_html();57355736}elsif($formateq'plain') {5737local$/=undef;5738print<$fd>;5739close$fd5740or print"Reading git-diff-tree failed\n";5741}elsif($formateq'patch') {5742local$/=undef;5743print<$fd>;5744close$fd5745or print"Reading git-format-patch failed\n";5746}5747}57485749sub git_commitdiff_plain {5750 git_commitdiff(-format =>'plain');5751}57525753# format-patch-style patches5754sub git_patch {5755 git_commitdiff(-format =>'patch', -single=>1);5756}57575758sub git_patches {5759 git_commitdiff(-format =>'patch');5760}57615762sub git_history {5763if(!defined$hash_base) {5764$hash_base= git_get_head_hash($project);5765}5766if(!defined$page) {5767$page=0;5768}5769my$ftype;5770my%co= parse_commit($hash_base)5771or die_error(404,"Unknown commit object");57725773my$refs= git_get_references();5774my$limit=sprintf("--max-count=%i", (100* ($page+1)));57755776my@commitlist= parse_commits($hash_base,101, (100*$page),5777$file_name,"--full-history")5778or die_error(404,"No such file or directory on given branch");57795780if(!defined$hash&&defined$file_name) {5781# some commits could have deleted file in question,5782# and not have it in tree, but one of them has to have it5783for(my$i=0;$i<=@commitlist;$i++) {5784$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5785last ifdefined$hash;5786}5787}5788if(defined$hash) {5789$ftype= git_get_type($hash);5790}5791if(!defined$ftype) {5792 die_error(500,"Unknown type of object");5793}57945795my$paging_nav='';5796if($page>0) {5797$paging_nav.=5798$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5799 file_name=>$file_name)},5800"first");5801$paging_nav.=" ⋅ ".5802$cgi->a({-href => href(-replay=>1, page=>$page-1),5803-accesskey =>"p", -title =>"Alt-p"},"prev");5804}else{5805$paging_nav.="first";5806$paging_nav.=" ⋅ prev";5807}5808my$next_link='';5809if($#commitlist>=100) {5810$next_link=5811$cgi->a({-href => href(-replay=>1, page=>$page+1),5812-accesskey =>"n", -title =>"Alt-n"},"next");5813$paging_nav.=" ⋅$next_link";5814}else{5815$paging_nav.=" ⋅ next";5816}58175818 git_header_html();5819 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5820 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5821 git_print_page_path($file_name,$ftype,$hash_base);58225823 git_history_body(\@commitlist,0,99,5824$refs,$hash_base,$ftype,$next_link);58255826 git_footer_html();5827}58285829sub git_search {5830 gitweb_check_feature('search')or die_error(403,"Search is disabled");5831if(!defined$searchtext) {5832 die_error(400,"Text field is empty");5833}5834if(!defined$hash) {5835$hash= git_get_head_hash($project);5836}5837my%co= parse_commit($hash);5838if(!%co) {5839 die_error(404,"Unknown commit object");5840}5841if(!defined$page) {5842$page=0;5843}58445845$searchtype||='commit';5846if($searchtypeeq'pickaxe') {5847# pickaxe may take all resources of your box and run for several minutes5848# with every query - so decide by yourself how public you make this feature5849 gitweb_check_feature('pickaxe')5850or die_error(403,"Pickaxe is disabled");5851}5852if($searchtypeeq'grep') {5853 gitweb_check_feature('grep')5854or die_error(403,"Grep is disabled");5855}58565857 git_header_html();58585859if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5860my$greptype;5861if($searchtypeeq'commit') {5862$greptype="--grep=";5863}elsif($searchtypeeq'author') {5864$greptype="--author=";5865}elsif($searchtypeeq'committer') {5866$greptype="--committer=";5867}5868$greptype.=$searchtext;5869my@commitlist= parse_commits($hash,101, (100*$page),undef,5870$greptype,'--regexp-ignore-case',5871$search_use_regexp?'--extended-regexp':'--fixed-strings');58725873my$paging_nav='';5874if($page>0) {5875$paging_nav.=5876$cgi->a({-href => href(action=>"search", hash=>$hash,5877 searchtext=>$searchtext,5878 searchtype=>$searchtype)},5879"first");5880$paging_nav.=" ⋅ ".5881$cgi->a({-href => href(-replay=>1, page=>$page-1),5882-accesskey =>"p", -title =>"Alt-p"},"prev");5883}else{5884$paging_nav.="first";5885$paging_nav.=" ⋅ prev";5886}5887my$next_link='';5888if($#commitlist>=100) {5889$next_link=5890$cgi->a({-href => href(-replay=>1, page=>$page+1),5891-accesskey =>"n", -title =>"Alt-n"},"next");5892$paging_nav.=" ⋅$next_link";5893}else{5894$paging_nav.=" ⋅ next";5895}58965897if($#commitlist>=100) {5898}58995900 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5901 git_print_header_div('commit', esc_html($co{'title'}),$hash);5902 git_search_grep_body(\@commitlist,0,99,$next_link);5903}59045905if($searchtypeeq'pickaxe') {5906 git_print_page_nav('','',$hash,$co{'tree'},$hash);5907 git_print_header_div('commit', esc_html($co{'title'}),$hash);59085909print"<table class=\"pickaxe search\">\n";5910my$alternate=1;5911local$/="\n";5912open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5913'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5914($search_use_regexp?'--pickaxe-regex': ());5915undef%co;5916my@files;5917while(my$line= <$fd>) {5918chomp$line;5919next unless$line;59205921my%set= parse_difftree_raw_line($line);5922if(defined$set{'commit'}) {5923# finish previous commit5924if(%co) {5925print"</td>\n".5926"<td class=\"link\">".5927$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5928" | ".5929$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5930print"</td>\n".5931"</tr>\n";5932}59335934if($alternate) {5935print"<tr class=\"dark\">\n";5936}else{5937print"<tr class=\"light\">\n";5938}5939$alternate^=1;5940%co= parse_commit($set{'commit'});5941my$author= chop_and_escape_str($co{'author_name'},15,5);5942print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5943"<td><i>$author</i></td>\n".5944"<td>".5945$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5946-class=>"list subject"},5947 chop_and_escape_str($co{'title'},50) ."<br/>");5948}elsif(defined$set{'to_id'}) {5949next if($set{'to_id'} =~m/^0{40}$/);59505951print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5952 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5953-class=>"list"},5954"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5955"<br/>\n";5956}5957}5958close$fd;59595960# finish last commit (warning: repetition!)5961if(%co) {5962print"</td>\n".5963"<td class=\"link\">".5964$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5965" | ".5966$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5967print"</td>\n".5968"</tr>\n";5969}59705971print"</table>\n";5972}59735974if($searchtypeeq'grep') {5975 git_print_page_nav('','',$hash,$co{'tree'},$hash);5976 git_print_header_div('commit', esc_html($co{'title'}),$hash);59775978print"<table class=\"grep_search\">\n";5979my$alternate=1;5980my$matches=0;5981local$/="\n";5982open my$fd,"-|", git_cmd(),'grep','-n',5983$search_use_regexp? ('-E','-i') :'-F',5984$searchtext,$co{'tree'};5985my$lastfile='';5986while(my$line= <$fd>) {5987chomp$line;5988my($file,$lno,$ltext,$binary);5989last if($matches++>1000);5990if($line=~/^Binary file (.+) matches$/) {5991$file=$1;5992$binary=1;5993}else{5994(undef,$file,$lno,$ltext) =split(/:/,$line,4);5995}5996if($filene$lastfile) {5997$lastfileand print"</td></tr>\n";5998if($alternate++) {5999print"<tr class=\"dark\">\n";6000}else{6001print"<tr class=\"light\">\n";6002}6003print"<td class=\"list\">".6004$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6005 file_name=>"$file"),6006-class=>"list"}, esc_path($file));6007print"</td><td>\n";6008$lastfile=$file;6009}6010if($binary) {6011print"<div class=\"binary\">Binary file</div>\n";6012}else{6013$ltext= untabify($ltext);6014if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6015$ltext= esc_html($1, -nbsp=>1);6016$ltext.='<span class="match">';6017$ltext.= esc_html($2, -nbsp=>1);6018$ltext.='</span>';6019$ltext.= esc_html($3, -nbsp=>1);6020}else{6021$ltext= esc_html($ltext, -nbsp=>1);6022}6023print"<div class=\"pre\">".6024$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6025 file_name=>"$file").'#l'.$lno,6026-class=>"linenr"},sprintf('%4i',$lno))6027.' '.$ltext."</div>\n";6028}6029}6030if($lastfile) {6031print"</td></tr>\n";6032if($matches>1000) {6033print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6034}6035}else{6036print"<div class=\"diff nodifferences\">No matches found</div>\n";6037}6038close$fd;60396040print"</table>\n";6041}6042 git_footer_html();6043}60446045sub git_search_help {6046 git_header_html();6047 git_print_page_nav('','',$hash,$hash,$hash);6048print<<EOT;6049<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6050regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6051the pattern entered is recognized as the POSIX extended6052<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6053insensitive).</p>6054<dl>6055<dt><b>commit</b></dt>6056<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6057EOT6058my$have_grep= gitweb_check_feature('grep');6059if($have_grep) {6060print<<EOT;6061<dt><b>grep</b></dt>6062<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6063 a different one) are searched for the given pattern. On large trees, this search can take6064a while and put some strain on the server, so please use it with some consideration. Note that6065due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6066case-sensitive.</dd>6067EOT6068}6069print<<EOT;6070<dt><b>author</b></dt>6071<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6072<dt><b>committer</b></dt>6073<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6074EOT6075my$have_pickaxe= gitweb_check_feature('pickaxe');6076if($have_pickaxe) {6077print<<EOT;6078<dt><b>pickaxe</b></dt>6079<dd>All commits that caused the string to appear or disappear from any file (changes that6080added, removed or "modified" the string) will be listed. This search can take a while and6081takes a lot of strain on the server, so please use it wisely. Note that since you may be6082interested even in changes just changing the case as well, this search is case sensitive.</dd>6083EOT6084}6085print"</dl>\n";6086 git_footer_html();6087}60886089sub git_shortlog {6090my$head= git_get_head_hash($project);6091if(!defined$hash) {6092$hash=$head;6093}6094if(!defined$page) {6095$page=0;6096}6097my$refs= git_get_references();60986099my$commit_hash=$hash;6100if(defined$hash_parent) {6101$commit_hash="$hash_parent..$hash";6102}6103my@commitlist= parse_commits($commit_hash,101, (100*$page));61046105my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);6106my$next_link='';6107if($#commitlist>=100) {6108$next_link=6109$cgi->a({-href => href(-replay=>1, page=>$page+1),6110-accesskey =>"n", -title =>"Alt-n"},"next");6111}6112my$patch_max= gitweb_check_feature('patches');6113if($patch_max) {6114if($patch_max<0||@commitlist<=$patch_max) {6115$paging_nav.=" ⋅ ".6116$cgi->a({-href => href(action=>"patches", -replay=>1)},6117"patches");6118}6119}61206121 git_header_html();6122 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6123 git_print_header_div('summary',$project);61246125 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);61266127 git_footer_html();6128}61296130## ......................................................................6131## feeds (RSS, Atom; OPML)61326133sub git_feed {6134my$format=shift||'atom';6135my$have_blame= gitweb_check_feature('blame');61366137# Atom: http://www.atomenabled.org/developers/syndication/6138# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6139if($formatne'rss'&&$formatne'atom') {6140 die_error(400,"Unknown web feed format");6141}61426143# log/feed of current (HEAD) branch, log of given branch, history of file/directory6144my$head=$hash||'HEAD';6145my@commitlist= parse_commits($head,150,0,$file_name);61466147my%latest_commit;6148my%latest_date;6149my$content_type="application/$format+xml";6150if(defined$cgi->http('HTTP_ACCEPT') &&6151$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6152# browser (feed reader) prefers text/xml6153$content_type='text/xml';6154}6155if(defined($commitlist[0])) {6156%latest_commit= %{$commitlist[0]};6157my$latest_epoch=$latest_commit{'committer_epoch'};6158%latest_date= parse_date($latest_epoch);6159my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6160if(defined$if_modified) {6161my$since;6162if(eval{require HTTP::Date;1; }) {6163$since= HTTP::Date::str2time($if_modified);6164}elsif(eval{require Time::ParseDate;1; }) {6165$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6166}6167if(defined$since&&$latest_epoch<=$since) {6168print$cgi->header(6169-type =>$content_type,6170-charset =>'utf-8',6171-last_modified =>$latest_date{'rfc2822'},6172-status =>'304 Not Modified');6173return;6174}6175}6176print$cgi->header(6177-type =>$content_type,6178-charset =>'utf-8',6179-last_modified =>$latest_date{'rfc2822'});6180}else{6181print$cgi->header(6182-type =>$content_type,6183-charset =>'utf-8');6184}61856186# Optimization: skip generating the body if client asks only6187# for Last-Modified date.6188return if($cgi->request_method()eq'HEAD');61896190# header variables6191my$title="$site_name-$project/$action";6192my$feed_type='log';6193if(defined$hash) {6194$title.=" - '$hash'";6195$feed_type='branch log';6196if(defined$file_name) {6197$title.=" ::$file_name";6198$feed_type='history';6199}6200}elsif(defined$file_name) {6201$title.=" -$file_name";6202$feed_type='history';6203}6204$title.="$feed_type";6205my$descr= git_get_project_description($project);6206if(defined$descr) {6207$descr= esc_html($descr);6208}else{6209$descr="$project".6210($formateq'rss'?'RSS':'Atom') .6211" feed";6212}6213my$owner= git_get_project_owner($project);6214$owner= esc_html($owner);62156216#header6217my$alt_url;6218if(defined$file_name) {6219$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6220}elsif(defined$hash) {6221$alt_url= href(-full=>1, action=>"log", hash=>$hash);6222}else{6223$alt_url= href(-full=>1, action=>"summary");6224}6225print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6226if($formateq'rss') {6227print<<XML;6228<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6229<channel>6230XML6231print"<title>$title</title>\n".6232"<link>$alt_url</link>\n".6233"<description>$descr</description>\n".6234"<language>en</language>\n".6235# project owner is responsible for 'editorial' content6236"<managingEditor>$owner</managingEditor>\n";6237if(defined$logo||defined$favicon) {6238# prefer the logo to the favicon, since RSS6239# doesn't allow both6240my$img= esc_url($logo||$favicon);6241print"<image>\n".6242"<url>$img</url>\n".6243"<title>$title</title>\n".6244"<link>$alt_url</link>\n".6245"</image>\n";6246}6247if(%latest_date) {6248print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6249print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6250}6251print"<generator>gitweb v.$version/$git_version</generator>\n";6252}elsif($formateq'atom') {6253print<<XML;6254<feed xmlns="http://www.w3.org/2005/Atom">6255XML6256print"<title>$title</title>\n".6257"<subtitle>$descr</subtitle>\n".6258'<link rel="alternate" type="text/html" href="'.6259$alt_url.'" />'."\n".6260'<link rel="self" type="'.$content_type.'" href="'.6261$cgi->self_url() .'" />'."\n".6262"<id>". href(-full=>1) ."</id>\n".6263# use project owner for feed author6264"<author><name>$owner</name></author>\n";6265if(defined$favicon) {6266print"<icon>". esc_url($favicon) ."</icon>\n";6267}6268if(defined$logo_url) {6269# not twice as wide as tall: 72 x 27 pixels6270print"<logo>". esc_url($logo) ."</logo>\n";6271}6272if(!%latest_date) {6273# dummy date to keep the feed valid until commits trickle in:6274print"<updated>1970-01-01T00:00:00Z</updated>\n";6275}else{6276print"<updated>$latest_date{'iso-8601'}</updated>\n";6277}6278print"<generator version='$version/$git_version'>gitweb</generator>\n";6279}62806281# contents6282for(my$i=0;$i<=$#commitlist;$i++) {6283my%co= %{$commitlist[$i]};6284my$commit=$co{'id'};6285# we read 150, we always show 30 and the ones more recent than 48 hours6286if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6287last;6288}6289my%cd= parse_date($co{'author_epoch'});62906291# get list of changed files6292open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6293$co{'parent'} ||"--root",6294$co{'id'},"--", (defined$file_name?$file_name: ())6295ornext;6296my@difftree=map{chomp;$_} <$fd>;6297close$fd6298ornext;62996300# print element (entry, item)6301my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6302if($formateq'rss') {6303print"<item>\n".6304"<title>". esc_html($co{'title'}) ."</title>\n".6305"<author>". esc_html($co{'author'}) ."</author>\n".6306"<pubDate>$cd{'rfc2822'}</pubDate>\n".6307"<guid isPermaLink=\"true\">$co_url</guid>\n".6308"<link>$co_url</link>\n".6309"<description>". esc_html($co{'title'}) ."</description>\n".6310"<content:encoded>".6311"<![CDATA[\n";6312}elsif($formateq'atom') {6313print"<entry>\n".6314"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6315"<updated>$cd{'iso-8601'}</updated>\n".6316"<author>\n".6317" <name>". esc_html($co{'author_name'}) ."</name>\n";6318if($co{'author_email'}) {6319print" <email>". esc_html($co{'author_email'}) ."</email>\n";6320}6321print"</author>\n".6322# use committer for contributor6323"<contributor>\n".6324" <name>". esc_html($co{'committer_name'}) ."</name>\n";6325if($co{'committer_email'}) {6326print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6327}6328print"</contributor>\n".6329"<published>$cd{'iso-8601'}</published>\n".6330"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6331"<id>$co_url</id>\n".6332"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6333"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6334}6335my$comment=$co{'comment'};6336print"<pre>\n";6337foreachmy$line(@$comment) {6338$line= esc_html($line);6339print"$line\n";6340}6341print"</pre><ul>\n";6342foreachmy$difftree_line(@difftree) {6343my%difftree= parse_difftree_raw_line($difftree_line);6344next if!$difftree{'from_id'};63456346my$file=$difftree{'file'} ||$difftree{'to_file'};63476348print"<li>".6349"[".6350$cgi->a({-href => href(-full=>1, action=>"blobdiff",6351 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6352 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6353 file_name=>$file, file_parent=>$difftree{'from_file'}),6354-title =>"diff"},'D');6355if($have_blame) {6356print$cgi->a({-href => href(-full=>1, action=>"blame",6357 file_name=>$file, hash_base=>$commit),6358-title =>"blame"},'B');6359}6360# if this is not a feed of a file history6361if(!defined$file_name||$file_namene$file) {6362print$cgi->a({-href => href(-full=>1, action=>"history",6363 file_name=>$file, hash=>$commit),6364-title =>"history"},'H');6365}6366$file= esc_path($file);6367print"] ".6368"$file</li>\n";6369}6370if($formateq'rss') {6371print"</ul>]]>\n".6372"</content:encoded>\n".6373"</item>\n";6374}elsif($formateq'atom') {6375print"</ul>\n</div>\n".6376"</content>\n".6377"</entry>\n";6378}6379}63806381# end of feed6382if($formateq'rss') {6383print"</channel>\n</rss>\n";6384}elsif($formateq'atom') {6385print"</feed>\n";6386}6387}63886389sub git_rss {6390 git_feed('rss');6391}63926393sub git_atom {6394 git_feed('atom');6395}63966397sub git_opml {6398my@list= git_get_projects_list();63996400print$cgi->header(6401-type =>'text/xml',6402-charset =>'utf-8',6403-content_disposition =>'inline; filename="opml.xml"');64046405print<<XML;6406<?xml version="1.0" encoding="utf-8"?>6407<opml version="1.0">6408<head>6409 <title>$site_nameOPML Export</title>6410</head>6411<body>6412<outline text="git RSS feeds">6413XML64146415foreachmy$pr(@list) {6416my%proj=%$pr;6417my$head= git_get_head_hash($proj{'path'});6418if(!defined$head) {6419next;6420}6421$git_dir="$projectroot/$proj{'path'}";6422my%co= parse_commit($head);6423if(!%co) {6424next;6425}64266427my$path= esc_html(chop_str($proj{'path'},25,5));6428my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6429my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6430print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6431}6432print<<XML;6433</outline>6434</body>6435</opml>6436XML6437}