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-scm.com/"; 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 available providers are gravatar and picon. 382# If an unknown provider is specified, the feature is disabled. 383 384# Gravatar depends on Digest::MD5. 385# Picon currently relies on the indiana.edu database. 386 387# To enable system wide have in $GITWEB_CONFIG 388# $feature{'avatar'}{'default'} = ['<provider>']; 389# where <provider> is either gravatar or picon. 390# To have project specific config enable override in $GITWEB_CONFIG 391# $feature{'avatar'}{'override'} = 1; 392# and in project config gitweb.avatar = <provider>; 393'avatar'=> { 394'sub'=> \&feature_avatar, 395'override'=>0, 396'default'=> ['']}, 397); 398 399sub gitweb_get_feature { 400my($name) =@_; 401return unlessexists$feature{$name}; 402my($sub,$override,@defaults) = ( 403$feature{$name}{'sub'}, 404$feature{$name}{'override'}, 405@{$feature{$name}{'default'}}); 406if(!$override) {return@defaults; } 407if(!defined$sub) { 408warn"feature$nameis not overridable"; 409return@defaults; 410} 411return$sub->(@defaults); 412} 413 414# A wrapper to check if a given feature is enabled. 415# With this, you can say 416# 417# my $bool_feat = gitweb_check_feature('bool_feat'); 418# gitweb_check_feature('bool_feat') or somecode; 419# 420# instead of 421# 422# my ($bool_feat) = gitweb_get_feature('bool_feat'); 423# (gitweb_get_feature('bool_feat'))[0] or somecode; 424# 425sub gitweb_check_feature { 426return(gitweb_get_feature(@_))[0]; 427} 428 429 430sub feature_bool { 431my$key=shift; 432my($val) = git_get_project_config($key,'--bool'); 433 434if(!defined$val) { 435return($_[0]); 436}elsif($valeq'true') { 437return(1); 438}elsif($valeq'false') { 439return(0); 440} 441} 442 443sub feature_snapshot { 444my(@fmts) =@_; 445 446my($val) = git_get_project_config('snapshot'); 447 448if($val) { 449@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 450} 451 452return@fmts; 453} 454 455sub feature_patches { 456my@val= (git_get_project_config('patches','--int')); 457 458if(@val) { 459return@val; 460} 461 462return($_[0]); 463} 464 465sub feature_avatar { 466my@val= (git_get_project_config('avatar')); 467 468return@val?@val:@_; 469} 470 471# checking HEAD file with -e is fragile if the repository was 472# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 473# and then pruned. 474sub check_head_link { 475my($dir) =@_; 476my$headfile="$dir/HEAD"; 477return((-e $headfile) || 478(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 479} 480 481sub check_export_ok { 482my($dir) =@_; 483return(check_head_link($dir) && 484(!$export_ok|| -e "$dir/$export_ok") && 485(!$export_auth_hook||$export_auth_hook->($dir))); 486} 487 488# process alternate names for backward compatibility 489# filter out unsupported (unknown) snapshot formats 490sub filter_snapshot_fmts { 491my@fmts=@_; 492 493@fmts=map{ 494exists$known_snapshot_format_aliases{$_} ? 495$known_snapshot_format_aliases{$_} :$_}@fmts; 496@fmts=grep{ 497exists$known_snapshot_formats{$_} }@fmts; 498} 499 500our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 501if(-e $GITWEB_CONFIG) { 502do$GITWEB_CONFIG; 503}else{ 504our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 505do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 506} 507 508# version of the core git binary 509our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 510 511$projects_list||=$projectroot; 512 513# ====================================================================== 514# input validation and dispatch 515 516# input parameters can be collected from a variety of sources (presently, CGI 517# and PATH_INFO), so we define an %input_params hash that collects them all 518# together during validation: this allows subsequent uses (e.g. href()) to be 519# agnostic of the parameter origin 520 521our%input_params= (); 522 523# input parameters are stored with the long parameter name as key. This will 524# also be used in the href subroutine to convert parameters to their CGI 525# equivalent, and since the href() usage is the most frequent one, we store 526# the name -> CGI key mapping here, instead of the reverse. 527# 528# XXX: Warning: If you touch this, check the search form for updating, 529# too. 530 531our@cgi_param_mapping= ( 532 project =>"p", 533 action =>"a", 534 file_name =>"f", 535 file_parent =>"fp", 536 hash =>"h", 537 hash_parent =>"hp", 538 hash_base =>"hb", 539 hash_parent_base =>"hpb", 540 page =>"pg", 541 order =>"o", 542 searchtext =>"s", 543 searchtype =>"st", 544 snapshot_format =>"sf", 545 extra_options =>"opt", 546 search_use_regexp =>"sr", 547); 548our%cgi_param_mapping=@cgi_param_mapping; 549 550# we will also need to know the possible actions, for validation 551our%actions= ( 552"blame"=> \&git_blame, 553"blobdiff"=> \&git_blobdiff, 554"blobdiff_plain"=> \&git_blobdiff_plain, 555"blob"=> \&git_blob, 556"blob_plain"=> \&git_blob_plain, 557"commitdiff"=> \&git_commitdiff, 558"commitdiff_plain"=> \&git_commitdiff_plain, 559"commit"=> \&git_commit, 560"forks"=> \&git_forks, 561"heads"=> \&git_heads, 562"history"=> \&git_history, 563"log"=> \&git_log, 564"patch"=> \&git_patch, 565"patches"=> \&git_patches, 566"rss"=> \&git_rss, 567"atom"=> \&git_atom, 568"search"=> \&git_search, 569"search_help"=> \&git_search_help, 570"shortlog"=> \&git_shortlog, 571"summary"=> \&git_summary, 572"tag"=> \&git_tag, 573"tags"=> \&git_tags, 574"tree"=> \&git_tree, 575"snapshot"=> \&git_snapshot, 576"object"=> \&git_object, 577# those below don't need $project 578"opml"=> \&git_opml, 579"project_list"=> \&git_project_list, 580"project_index"=> \&git_project_index, 581); 582 583# finally, we have the hash of allowed extra_options for the commands that 584# allow them 585our%allowed_options= ( 586"--no-merges"=> [qw(rss atom log shortlog history)], 587); 588 589# fill %input_params with the CGI parameters. All values except for 'opt' 590# should be single values, but opt can be an array. We should probably 591# build an array of parameters that can be multi-valued, but since for the time 592# being it's only this one, we just single it out 593while(my($name,$symbol) =each%cgi_param_mapping) { 594if($symboleq'opt') { 595$input_params{$name} = [$cgi->param($symbol) ]; 596}else{ 597$input_params{$name} =$cgi->param($symbol); 598} 599} 600 601# now read PATH_INFO and update the parameter list for missing parameters 602sub evaluate_path_info { 603return ifdefined$input_params{'project'}; 604return if!$path_info; 605$path_info=~ s,^/+,,; 606return if!$path_info; 607 608# find which part of PATH_INFO is project 609my$project=$path_info; 610$project=~ s,/+$,,; 611while($project&& !check_head_link("$projectroot/$project")) { 612$project=~ s,/*[^/]*$,,; 613} 614return unless$project; 615$input_params{'project'} =$project; 616 617# do not change any parameters if an action is given using the query string 618return if$input_params{'action'}; 619$path_info=~ s,^\Q$project\E/*,,; 620 621# next, check if we have an action 622my$action=$path_info; 623$action=~ s,/.*$,,; 624if(exists$actions{$action}) { 625$path_info=~ s,^$action/*,,; 626$input_params{'action'} =$action; 627} 628 629# list of actions that want hash_base instead of hash, but can have no 630# pathname (f) parameter 631my@wants_base= ( 632'tree', 633'history', 634); 635 636# we want to catch 637# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 638my($parentrefname,$parentpathname,$refname,$pathname) = 639($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 640 641# first, analyze the 'current' part 642if(defined$pathname) { 643# we got "branch:filename" or "branch:dir/" 644# we could use git_get_type(branch:pathname), but: 645# - it needs $git_dir 646# - it does a git() call 647# - the convention of terminating directories with a slash 648# makes it superfluous 649# - embedding the action in the PATH_INFO would make it even 650# more superfluous 651$pathname=~ s,^/+,,; 652if(!$pathname||substr($pathname, -1)eq"/") { 653$input_params{'action'} ||="tree"; 654$pathname=~ s,/$,,; 655}else{ 656# the default action depends on whether we had parent info 657# or not 658if($parentrefname) { 659$input_params{'action'} ||="blobdiff_plain"; 660}else{ 661$input_params{'action'} ||="blob_plain"; 662} 663} 664$input_params{'hash_base'} ||=$refname; 665$input_params{'file_name'} ||=$pathname; 666}elsif(defined$refname) { 667# we got "branch". In this case we have to choose if we have to 668# set hash or hash_base. 669# 670# Most of the actions without a pathname only want hash to be 671# set, except for the ones specified in @wants_base that want 672# hash_base instead. It should also be noted that hand-crafted 673# links having 'history' as an action and no pathname or hash 674# set will fail, but that happens regardless of PATH_INFO. 675$input_params{'action'} ||="shortlog"; 676if(grep{$_eq$input_params{'action'} }@wants_base) { 677$input_params{'hash_base'} ||=$refname; 678}else{ 679$input_params{'hash'} ||=$refname; 680} 681} 682 683# next, handle the 'parent' part, if present 684if(defined$parentrefname) { 685# a missing pathspec defaults to the 'current' filename, allowing e.g. 686# someproject/blobdiff/oldrev..newrev:/filename 687if($parentpathname) { 688$parentpathname=~ s,^/+,,; 689$parentpathname=~ s,/$,,; 690$input_params{'file_parent'} ||=$parentpathname; 691}else{ 692$input_params{'file_parent'} ||=$input_params{'file_name'}; 693} 694# we assume that hash_parent_base is wanted if a path was specified, 695# or if the action wants hash_base instead of hash 696if(defined$input_params{'file_parent'} || 697grep{$_eq$input_params{'action'} }@wants_base) { 698$input_params{'hash_parent_base'} ||=$parentrefname; 699}else{ 700$input_params{'hash_parent'} ||=$parentrefname; 701} 702} 703 704# for the snapshot action, we allow URLs in the form 705# $project/snapshot/$hash.ext 706# where .ext determines the snapshot and gets removed from the 707# passed $refname to provide the $hash. 708# 709# To be able to tell that $refname includes the format extension, we 710# require the following two conditions to be satisfied: 711# - the hash input parameter MUST have been set from the $refname part 712# of the URL (i.e. they must be equal) 713# - the snapshot format MUST NOT have been defined already (e.g. from 714# CGI parameter sf) 715# It's also useless to try any matching unless $refname has a dot, 716# so we check for that too 717if(defined$input_params{'action'} && 718$input_params{'action'}eq'snapshot'&& 719defined$refname&&index($refname,'.') != -1&& 720$refnameeq$input_params{'hash'} && 721!defined$input_params{'snapshot_format'}) { 722# We loop over the known snapshot formats, checking for 723# extensions. Allowed extensions are both the defined suffix 724# (which includes the initial dot already) and the snapshot 725# format key itself, with a prepended dot 726while(my($fmt,$opt) =each%known_snapshot_formats) { 727my$hash=$refname; 728unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 729next; 730} 731my$sfx=$1; 732# a valid suffix was found, so set the snapshot format 733# and reset the hash parameter 734$input_params{'snapshot_format'} =$fmt; 735$input_params{'hash'} =$hash; 736# we also set the format suffix to the one requested 737# in the URL: this way a request for e.g. .tgz returns 738# a .tgz instead of a .tar.gz 739$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 740last; 741} 742} 743} 744evaluate_path_info(); 745 746our$action=$input_params{'action'}; 747if(defined$action) { 748if(!validate_action($action)) { 749 die_error(400,"Invalid action parameter"); 750} 751} 752 753# parameters which are pathnames 754our$project=$input_params{'project'}; 755if(defined$project) { 756if(!validate_project($project)) { 757undef$project; 758 die_error(404,"No such project"); 759} 760} 761 762our$file_name=$input_params{'file_name'}; 763if(defined$file_name) { 764if(!validate_pathname($file_name)) { 765 die_error(400,"Invalid file parameter"); 766} 767} 768 769our$file_parent=$input_params{'file_parent'}; 770if(defined$file_parent) { 771if(!validate_pathname($file_parent)) { 772 die_error(400,"Invalid file parent parameter"); 773} 774} 775 776# parameters which are refnames 777our$hash=$input_params{'hash'}; 778if(defined$hash) { 779if(!validate_refname($hash)) { 780 die_error(400,"Invalid hash parameter"); 781} 782} 783 784our$hash_parent=$input_params{'hash_parent'}; 785if(defined$hash_parent) { 786if(!validate_refname($hash_parent)) { 787 die_error(400,"Invalid hash parent parameter"); 788} 789} 790 791our$hash_base=$input_params{'hash_base'}; 792if(defined$hash_base) { 793if(!validate_refname($hash_base)) { 794 die_error(400,"Invalid hash base parameter"); 795} 796} 797 798our@extra_options= @{$input_params{'extra_options'}}; 799# @extra_options is always defined, since it can only be (currently) set from 800# CGI, and $cgi->param() returns the empty array in array context if the param 801# is not set 802foreachmy$opt(@extra_options) { 803if(not exists$allowed_options{$opt}) { 804 die_error(400,"Invalid option parameter"); 805} 806if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 807 die_error(400,"Invalid option parameter for this action"); 808} 809} 810 811our$hash_parent_base=$input_params{'hash_parent_base'}; 812if(defined$hash_parent_base) { 813if(!validate_refname($hash_parent_base)) { 814 die_error(400,"Invalid hash parent base parameter"); 815} 816} 817 818# other parameters 819our$page=$input_params{'page'}; 820if(defined$page) { 821if($page=~m/[^0-9]/) { 822 die_error(400,"Invalid page parameter"); 823} 824} 825 826our$searchtype=$input_params{'searchtype'}; 827if(defined$searchtype) { 828if($searchtype=~m/[^a-z]/) { 829 die_error(400,"Invalid searchtype parameter"); 830} 831} 832 833our$search_use_regexp=$input_params{'search_use_regexp'}; 834 835our$searchtext=$input_params{'searchtext'}; 836our$search_regexp; 837if(defined$searchtext) { 838if(length($searchtext) <2) { 839 die_error(403,"At least two characters are required for search parameter"); 840} 841$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 842} 843 844# path to the current git repository 845our$git_dir; 846$git_dir="$projectroot/$project"if$project; 847 848# list of supported snapshot formats 849our@snapshot_fmts= gitweb_get_feature('snapshot'); 850@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 851 852# check that the avatar feature is set to a known provider name, 853# and for each provider check if the dependencies are satisfied. 854# if the provider name is invalid or the dependencies are not met, 855# reset $git_avatar to the empty string. 856our($git_avatar) = gitweb_get_feature('avatar'); 857if($git_avatareq'gravatar') { 858$git_avatar=''unless(eval{require Digest::MD5;1; }); 859}elsif($git_avatareq'picon') { 860# no dependencies 861}else{ 862$git_avatar=''; 863} 864 865# dispatch 866if(!defined$action) { 867if(defined$hash) { 868$action= git_get_type($hash); 869}elsif(defined$hash_base&&defined$file_name) { 870$action= git_get_type("$hash_base:$file_name"); 871}elsif(defined$project) { 872$action='summary'; 873}else{ 874$action='project_list'; 875} 876} 877if(!defined($actions{$action})) { 878 die_error(400,"Unknown action"); 879} 880if($action!~m/^(?:opml|project_list|project_index)$/&& 881!$project) { 882 die_error(400,"Project needed"); 883} 884$actions{$action}->(); 885exit; 886 887## ====================================================================== 888## action links 889 890sub href { 891my%params=@_; 892# default is to use -absolute url() i.e. $my_uri 893my$href=$params{-full} ?$my_url:$my_uri; 894 895$params{'project'} =$projectunlessexists$params{'project'}; 896 897if($params{-replay}) { 898while(my($name,$symbol) =each%cgi_param_mapping) { 899if(!exists$params{$name}) { 900$params{$name} =$input_params{$name}; 901} 902} 903} 904 905my$use_pathinfo= gitweb_check_feature('pathinfo'); 906if($use_pathinfoand defined$params{'project'}) { 907# try to put as many parameters as possible in PATH_INFO: 908# - project name 909# - action 910# - hash_parent or hash_parent_base:/file_parent 911# - hash or hash_base:/filename 912# - the snapshot_format as an appropriate suffix 913 914# When the script is the root DirectoryIndex for the domain, 915# $href here would be something like http://gitweb.example.com/ 916# Thus, we strip any trailing / from $href, to spare us double 917# slashes in the final URL 918$href=~ s,/$,,; 919 920# Then add the project name, if present 921$href.="/".esc_url($params{'project'}); 922delete$params{'project'}; 923 924# since we destructively absorb parameters, we keep this 925# boolean that remembers if we're handling a snapshot 926my$is_snapshot=$params{'action'}eq'snapshot'; 927 928# Summary just uses the project path URL, any other action is 929# added to the URL 930if(defined$params{'action'}) { 931$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 932delete$params{'action'}; 933} 934 935# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 936# stripping nonexistent or useless pieces 937$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 938||$params{'hash_parent'} ||$params{'hash'}); 939if(defined$params{'hash_base'}) { 940if(defined$params{'hash_parent_base'}) { 941$href.= esc_url($params{'hash_parent_base'}); 942# skip the file_parent if it's the same as the file_name 943delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 944if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 945$href.=":/".esc_url($params{'file_parent'}); 946delete$params{'file_parent'}; 947} 948$href.=".."; 949delete$params{'hash_parent'}; 950delete$params{'hash_parent_base'}; 951}elsif(defined$params{'hash_parent'}) { 952$href.= esc_url($params{'hash_parent'}).".."; 953delete$params{'hash_parent'}; 954} 955 956$href.= esc_url($params{'hash_base'}); 957if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 958$href.=":/".esc_url($params{'file_name'}); 959delete$params{'file_name'}; 960} 961delete$params{'hash'}; 962delete$params{'hash_base'}; 963}elsif(defined$params{'hash'}) { 964$href.= esc_url($params{'hash'}); 965delete$params{'hash'}; 966} 967 968# If the action was a snapshot, we can absorb the 969# snapshot_format parameter too 970if($is_snapshot) { 971my$fmt=$params{'snapshot_format'}; 972# snapshot_format should always be defined when href() 973# is called, but just in case some code forgets, we 974# fall back to the default 975$fmt||=$snapshot_fmts[0]; 976$href.=$known_snapshot_formats{$fmt}{'suffix'}; 977delete$params{'snapshot_format'}; 978} 979} 980 981# now encode the parameters explicitly 982my@result= (); 983for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 984my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 985if(defined$params{$name}) { 986if(ref($params{$name})eq"ARRAY") { 987foreachmy$par(@{$params{$name}}) { 988push@result,$symbol."=". esc_param($par); 989} 990}else{ 991push@result,$symbol."=". esc_param($params{$name}); 992} 993} 994} 995$href.="?".join(';',@result)ifscalar@result; 996 997return$href; 998} 99910001001## ======================================================================1002## validation, quoting/unquoting and escaping10031004sub validate_action {1005my$input=shift||returnundef;1006returnundefunlessexists$actions{$input};1007return$input;1008}10091010sub validate_project {1011my$input=shift||returnundef;1012if(!validate_pathname($input) ||1013!(-d "$projectroot/$input") ||1014!check_export_ok("$projectroot/$input") ||1015($strict_export&& !project_in_list($input))) {1016returnundef;1017}else{1018return$input;1019}1020}10211022sub validate_pathname {1023my$input=shift||returnundef;10241025# no '.' or '..' as elements of path, i.e. no '.' nor '..'1026# at the beginning, at the end, and between slashes.1027# also this catches doubled slashes1028if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1029returnundef;1030}1031# no null characters1032if($input=~m!\0!) {1033returnundef;1034}1035return$input;1036}10371038sub validate_refname {1039my$input=shift||returnundef;10401041# textual hashes are O.K.1042if($input=~m/^[0-9a-fA-F]{40}$/) {1043return$input;1044}1045# it must be correct pathname1046$input= validate_pathname($input)1047orreturnundef;1048# restrictions on ref name according to git-check-ref-format1049if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1050returnundef;1051}1052return$input;1053}10541055# decode sequences of octets in utf8 into Perl's internal form,1056# which is utf-8 with utf8 flag set if needed. gitweb writes out1057# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1058sub to_utf8 {1059my$str=shift;1060if(utf8::valid($str)) {1061 utf8::decode($str);1062return$str;1063}else{1064return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1065}1066}10671068# quote unsafe chars, but keep the slash, even when it's not1069# correct, but quoted slashes look too horrible in bookmarks1070sub esc_param {1071my$str=shift;1072$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg;1073$str=~s/\+/%2B/g;1074$str=~s/ /\+/g;1075return$str;1076}10771078# quote unsafe chars in whole URL, so some charactrs cannot be quoted1079sub esc_url {1080my$str=shift;1081$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1082$str=~s/\+/%2B/g;1083$str=~s/ /\+/g;1084return$str;1085}10861087# quote unsafe characters in HTML attributes1088sub esc_attr {10891090# for XHTML conformance escaping '"' to '"' is not enough1091return esc_html(@_);1092}10931094# replace invalid utf8 character with SUBSTITUTION sequence1095sub esc_html {1096my$str=shift;1097my%opts=@_;10981099$str= to_utf8($str);1100$str=$cgi->escapeHTML($str);1101if($opts{'-nbsp'}) {1102$str=~s/ / /g;1103}1104$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1105return$str;1106}11071108# quote control characters and escape filename to HTML1109sub esc_path {1110my$str=shift;1111my%opts=@_;11121113$str= to_utf8($str);1114$str=$cgi->escapeHTML($str);1115if($opts{'-nbsp'}) {1116$str=~s/ / /g;1117}1118$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1119return$str;1120}11211122# Make control characters "printable", using character escape codes (CEC)1123sub quot_cec {1124my$cntrl=shift;1125my%opts=@_;1126my%es= (# character escape codes, aka escape sequences1127"\t"=>'\t',# tab (HT)1128"\n"=>'\n',# line feed (LF)1129"\r"=>'\r',# carrige return (CR)1130"\f"=>'\f',# form feed (FF)1131"\b"=>'\b',# backspace (BS)1132"\a"=>'\a',# alarm (bell) (BEL)1133"\e"=>'\e',# escape (ESC)1134"\013"=>'\v',# vertical tab (VT)1135"\000"=>'\0',# nul character (NUL)1136);1137my$chr= ( (exists$es{$cntrl})1138?$es{$cntrl}1139:sprintf('\%2x',ord($cntrl)) );1140if($opts{-nohtml}) {1141return$chr;1142}else{1143return"<span class=\"cntrl\">$chr</span>";1144}1145}11461147# Alternatively use unicode control pictures codepoints,1148# Unicode "printable representation" (PR)1149sub quot_upr {1150my$cntrl=shift;1151my%opts=@_;11521153my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1154if($opts{-nohtml}) {1155return$chr;1156}else{1157return"<span class=\"cntrl\">$chr</span>";1158}1159}11601161# git may return quoted and escaped filenames1162sub unquote {1163my$str=shift;11641165sub unq {1166my$seq=shift;1167my%es= (# character escape codes, aka escape sequences1168't'=>"\t",# tab (HT, TAB)1169'n'=>"\n",# newline (NL)1170'r'=>"\r",# return (CR)1171'f'=>"\f",# form feed (FF)1172'b'=>"\b",# backspace (BS)1173'a'=>"\a",# alarm (bell) (BEL)1174'e'=>"\e",# escape (ESC)1175'v'=>"\013",# vertical tab (VT)1176);11771178if($seq=~m/^[0-7]{1,3}$/) {1179# octal char sequence1180returnchr(oct($seq));1181}elsif(exists$es{$seq}) {1182# C escape sequence, aka character escape code1183return$es{$seq};1184}1185# quoted ordinary character1186return$seq;1187}11881189if($str=~m/^"(.*)"$/) {1190# needs unquoting1191$str=$1;1192$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1193}1194return$str;1195}11961197# escape tabs (convert tabs to spaces)1198sub untabify {1199my$line=shift;12001201while((my$pos=index($line,"\t")) != -1) {1202if(my$count= (8- ($pos%8))) {1203my$spaces=' ' x $count;1204$line=~s/\t/$spaces/;1205}1206}12071208return$line;1209}12101211sub project_in_list {1212my$project=shift;1213my@list= git_get_projects_list();1214return@list&&scalar(grep{$_->{'path'}eq$project}@list);1215}12161217## ----------------------------------------------------------------------1218## HTML aware string manipulation12191220# Try to chop given string on a word boundary between position1221# $len and $len+$add_len. If there is no word boundary there,1222# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1223# (marking chopped part) would be longer than given string.1224sub chop_str {1225my$str=shift;1226my$len=shift;1227my$add_len=shift||10;1228my$where=shift||'right';# 'left' | 'center' | 'right'12291230# Make sure perl knows it is utf8 encoded so we don't1231# cut in the middle of a utf8 multibyte char.1232$str= to_utf8($str);12331234# allow only $len chars, but don't cut a word if it would fit in $add_len1235# if it doesn't fit, cut it if it's still longer than the dots we would add1236# remove chopped character entities entirely12371238# when chopping in the middle, distribute $len into left and right part1239# return early if chopping wouldn't make string shorter1240if($whereeq'center') {1241return$strif($len+5>=length($str));# filler is length 51242$len=int($len/2);1243}else{1244return$strif($len+4>=length($str));# filler is length 41245}12461247# regexps: ending and beginning with word part up to $add_len1248my$endre=qr/.{$len}\w{0,$add_len}/;1249my$begre=qr/\w{0,$add_len}.{$len}/;12501251if($whereeq'left') {1252$str=~m/^(.*?)($begre)$/;1253my($lead,$body) = ($1,$2);1254if(length($lead) >4) {1255$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1256$lead=" ...";1257}1258return"$lead$body";12591260}elsif($whereeq'center') {1261$str=~m/^($endre)(.*)$/;1262my($left,$str) = ($1,$2);1263$str=~m/^(.*?)($begre)$/;1264my($mid,$right) = ($1,$2);1265if(length($mid) >5) {1266$left=~s/&[^;]*$//;1267$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1268$mid=" ... ";1269}1270return"$left$mid$right";12711272}else{1273$str=~m/^($endre)(.*)$/;1274my$body=$1;1275my$tail=$2;1276if(length($tail) >4) {1277$body=~s/&[^;]*$//;1278$tail="... ";1279}1280return"$body$tail";1281}1282}12831284# takes the same arguments as chop_str, but also wraps a <span> around the1285# result with a title attribute if it does get chopped. Additionally, the1286# string is HTML-escaped.1287sub chop_and_escape_str {1288my($str) =@_;12891290my$chopped= chop_str(@_);1291if($choppedeq$str) {1292return esc_html($chopped);1293}else{1294$str=~s/[[:cntrl:]]/?/g;1295return$cgi->span({-title=>$str}, esc_html($chopped));1296}1297}12981299## ----------------------------------------------------------------------1300## functions returning short strings13011302# CSS class for given age value (in seconds)1303sub age_class {1304my$age=shift;13051306if(!defined$age) {1307return"noage";1308}elsif($age<60*60*2) {1309return"age0";1310}elsif($age<60*60*24*2) {1311return"age1";1312}else{1313return"age2";1314}1315}13161317# convert age in seconds to "nn units ago" string1318sub age_string {1319my$age=shift;1320my$age_str;13211322if($age>60*60*24*365*2) {1323$age_str= (int$age/60/60/24/365);1324$age_str.=" years ago";1325}elsif($age>60*60*24*(365/12)*2) {1326$age_str=int$age/60/60/24/(365/12);1327$age_str.=" months ago";1328}elsif($age>60*60*24*7*2) {1329$age_str=int$age/60/60/24/7;1330$age_str.=" weeks ago";1331}elsif($age>60*60*24*2) {1332$age_str=int$age/60/60/24;1333$age_str.=" days ago";1334}elsif($age>60*60*2) {1335$age_str=int$age/60/60;1336$age_str.=" hours ago";1337}elsif($age>60*2) {1338$age_str=int$age/60;1339$age_str.=" min ago";1340}elsif($age>2) {1341$age_str=int$age;1342$age_str.=" sec ago";1343}else{1344$age_str.=" right now";1345}1346return$age_str;1347}13481349useconstant{1350 S_IFINVALID =>0030000,1351 S_IFGITLINK =>0160000,1352};13531354# submodule/subproject, a commit object reference1355sub S_ISGITLINK {1356my$mode=shift;13571358return(($mode& S_IFMT) == S_IFGITLINK)1359}13601361# convert file mode in octal to symbolic file mode string1362sub mode_str {1363my$mode=oct shift;13641365if(S_ISGITLINK($mode)) {1366return'm---------';1367}elsif(S_ISDIR($mode& S_IFMT)) {1368return'drwxr-xr-x';1369}elsif(S_ISLNK($mode)) {1370return'lrwxrwxrwx';1371}elsif(S_ISREG($mode)) {1372# git cares only about the executable bit1373if($mode& S_IXUSR) {1374return'-rwxr-xr-x';1375}else{1376return'-rw-r--r--';1377};1378}else{1379return'----------';1380}1381}13821383# convert file mode in octal to file type string1384sub file_type {1385my$mode=shift;13861387if($mode!~m/^[0-7]+$/) {1388return$mode;1389}else{1390$mode=oct$mode;1391}13921393if(S_ISGITLINK($mode)) {1394return"submodule";1395}elsif(S_ISDIR($mode& S_IFMT)) {1396return"directory";1397}elsif(S_ISLNK($mode)) {1398return"symlink";1399}elsif(S_ISREG($mode)) {1400return"file";1401}else{1402return"unknown";1403}1404}14051406# convert file mode in octal to file type description string1407sub file_type_long {1408my$mode=shift;14091410if($mode!~m/^[0-7]+$/) {1411return$mode;1412}else{1413$mode=oct$mode;1414}14151416if(S_ISGITLINK($mode)) {1417return"submodule";1418}elsif(S_ISDIR($mode& S_IFMT)) {1419return"directory";1420}elsif(S_ISLNK($mode)) {1421return"symlink";1422}elsif(S_ISREG($mode)) {1423if($mode& S_IXUSR) {1424return"executable";1425}else{1426return"file";1427};1428}else{1429return"unknown";1430}1431}143214331434## ----------------------------------------------------------------------1435## functions returning short HTML fragments, or transforming HTML fragments1436## which don't belong to other sections14371438# format line of commit message.1439sub format_log_line_html {1440my$line=shift;14411442$line= esc_html($line, -nbsp=>1);1443$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1444$cgi->a({-href => href(action=>"object", hash=>$1),1445-class=>"text"},$1);1446}eg;14471448return$line;1449}14501451# format marker of refs pointing to given object14521453# the destination action is chosen based on object type and current context:1454# - for annotated tags, we choose the tag view unless it's the current view1455# already, in which case we go to shortlog view1456# - for other refs, we keep the current view if we're in history, shortlog or1457# log view, and select shortlog otherwise1458sub format_ref_marker {1459my($refs,$id) =@_;1460my$markers='';14611462if(defined$refs->{$id}) {1463foreachmy$ref(@{$refs->{$id}}) {1464# this code exploits the fact that non-lightweight tags are the1465# only indirect objects, and that they are the only objects for which1466# we want to use tag instead of shortlog as action1467my($type,$name) =qw();1468my$indirect= ($ref=~s/\^\{\}$//);1469# e.g. tags/v2.6.11 or heads/next1470if($ref=~m!^(.*?)s?/(.*)$!) {1471$type=$1;1472$name=$2;1473}else{1474$type="ref";1475$name=$ref;1476}14771478my$class=$type;1479$class.=" indirect"if$indirect;14801481my$dest_action="shortlog";14821483if($indirect) {1484$dest_action="tag"unless$actioneq"tag";1485}elsif($action=~/^(history|(short)?log)$/) {1486$dest_action=$action;1487}14881489my$dest="";1490$dest.="refs/"unless$ref=~ m!^refs/!;1491$dest.=$ref;14921493my$link=$cgi->a({1494-href => href(1495 action=>$dest_action,1496 hash=>$dest1497)},$name);14981499$markers.=" <span class=\"".esc_attr($class)."\"title=\"".esc_attr($ref)."\">".1500$link."</span>";1501}1502}15031504if($markers) {1505return' <span class="refs">'.$markers.'</span>';1506}else{1507return"";1508}1509}15101511# format, perhaps shortened and with markers, title line1512sub format_subject_html {1513my($long,$short,$href,$extra) =@_;1514$extra=''unlessdefined($extra);15151516if(length($short) <length($long)) {1517$long=~s/[[:cntrl:]]/?/g;1518return$cgi->a({-href =>$href, -class=>"list subject",1519-title => to_utf8($long)},1520 esc_html($short) .$extra);1521}else{1522return$cgi->a({-href =>$href, -class=>"list subject"},1523 esc_html($long) .$extra);1524}1525}15261527# Rather than recomputing the url for an email multiple times, we cache it1528# after the first hit. This gives a visible benefit in views where the avatar1529# for the same email is used repeatedly (e.g. shortlog).1530# The cache is shared by all avatar engines (currently gravatar only), which1531# are free to use it as preferred. Since only one avatar engine is used for any1532# given page, there's no risk for cache conflicts.1533our%avatar_cache= ();15341535# Compute the picon url for a given email, by using the picon search service over at1536# http://www.cs.indiana.edu/picons/search.html1537sub picon_url {1538my$email=lc shift;1539if(!$avatar_cache{$email}) {1540my($user,$domain) =split('@',$email);1541$avatar_cache{$email} =1542"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1543"$domain/$user/".1544"users+domains+unknown/up/single";1545}1546return$avatar_cache{$email};1547}15481549# Compute the gravatar url for a given email, if it's not in the cache already.1550# Gravatar stores only the part of the URL before the size, since that's the1551# one computationally more expensive. This also allows reuse of the cache for1552# different sizes (for this particular engine).1553sub gravatar_url {1554my$email=lc shift;1555my$size=shift;1556$avatar_cache{$email} ||=1557"http://www.gravatar.com/avatar/".1558 Digest::MD5::md5_hex($email) ."?s=";1559return$avatar_cache{$email} .$size;1560}15611562# Insert an avatar for the given $email at the given $size if the feature1563# is enabled.1564sub git_get_avatar {1565my($email,%opts) =@_;1566my$pre_white= ($opts{-pad_before} ?" ":"");1567my$post_white= ($opts{-pad_after} ?" ":"");1568$opts{-size} ||='default';1569my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1570my$url="";1571if($git_avatareq'gravatar') {1572$url= gravatar_url($email,$size);1573}elsif($git_avatareq'picon') {1574$url= picon_url($email);1575}1576# Other providers can be added by extending the if chain, defining $url1577# as needed. If no variant puts something in $url, we assume avatars1578# are completely disabled/unavailable.1579if($url) {1580return$pre_white.1581"<img width=\"$size\"".1582"class=\"avatar\"".1583"src=\"".esc_url($url)."\"".1584"alt=\"\"".1585"/>".$post_white;1586}else{1587return"";1588}1589}15901591# format the author name of the given commit with the given tag1592# the author name is chopped and escaped according to the other1593# optional parameters (see chop_str).1594sub format_author_html {1595my$tag=shift;1596my$co=shift;1597my$author= chop_and_escape_str($co->{'author_name'},@_);1598return"<$tagclass=\"author\">".1599 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1600$author."</$tag>";1601}16021603# format git diff header line, i.e. "diff --(git|combined|cc) ..."1604sub format_git_diff_header_line {1605my$line=shift;1606my$diffinfo=shift;1607my($from,$to) =@_;16081609if($diffinfo->{'nparents'}) {1610# combined diff1611$line=~s!^(diff (.*?) )"?.*$!$1!;1612if($to->{'href'}) {1613$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1614 esc_path($to->{'file'}));1615}else{# file was deleted (no href)1616$line.= esc_path($to->{'file'});1617}1618}else{1619# "ordinary" diff1620$line=~s!^(diff (.*?) )"?a/.*$!$1!;1621if($from->{'href'}) {1622$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1623'a/'. esc_path($from->{'file'}));1624}else{# file was added (no href)1625$line.='a/'. esc_path($from->{'file'});1626}1627$line.=' ';1628if($to->{'href'}) {1629$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1630'b/'. esc_path($to->{'file'}));1631}else{# file was deleted1632$line.='b/'. esc_path($to->{'file'});1633}1634}16351636return"<div class=\"diff header\">$line</div>\n";1637}16381639# format extended diff header line, before patch itself1640sub format_extended_diff_header_line {1641my$line=shift;1642my$diffinfo=shift;1643my($from,$to) =@_;16441645# match <path>1646if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1647$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1648 esc_path($from->{'file'}));1649}1650if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1651$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1652 esc_path($to->{'file'}));1653}1654# match single <mode>1655if($line=~m/\s(\d{6})$/) {1656$line.='<span class="info"> ('.1657 file_type_long($1) .1658')</span>';1659}1660# match <hash>1661if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1662# can match only for combined diff1663$line='index ';1664for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1665if($from->{'href'}[$i]) {1666$line.=$cgi->a({-href=>$from->{'href'}[$i],1667-class=>"hash"},1668substr($diffinfo->{'from_id'}[$i],0,7));1669}else{1670$line.='0' x 7;1671}1672# separator1673$line.=','if($i<$diffinfo->{'nparents'} -1);1674}1675$line.='..';1676if($to->{'href'}) {1677$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1678substr($diffinfo->{'to_id'},0,7));1679}else{1680$line.='0' x 7;1681}16821683}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1684# can match only for ordinary diff1685my($from_link,$to_link);1686if($from->{'href'}) {1687$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1688substr($diffinfo->{'from_id'},0,7));1689}else{1690$from_link='0' x 7;1691}1692if($to->{'href'}) {1693$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1694substr($diffinfo->{'to_id'},0,7));1695}else{1696$to_link='0' x 7;1697}1698my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1699$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1700}17011702return$line."<br/>\n";1703}17041705# format from-file/to-file diff header1706sub format_diff_from_to_header {1707my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1708my$line;1709my$result='';17101711$line=$from_line;1712#assert($line =~ m/^---/) if DEBUG;1713# no extra formatting for "^--- /dev/null"1714if(!$diffinfo->{'nparents'}) {1715# ordinary (single parent) diff1716if($line=~m!^--- "?a/!) {1717if($from->{'href'}) {1718$line='--- a/'.1719$cgi->a({-href=>$from->{'href'}, -class=>"path"},1720 esc_path($from->{'file'}));1721}else{1722$line='--- a/'.1723 esc_path($from->{'file'});1724}1725}1726$result.= qq!<div class="diff from_file">$line</div>\n!;17271728}else{1729# combined diff (merge commit)1730for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1731if($from->{'href'}[$i]) {1732$line='--- '.1733$cgi->a({-href=>href(action=>"blobdiff",1734 hash_parent=>$diffinfo->{'from_id'}[$i],1735 hash_parent_base=>$parents[$i],1736 file_parent=>$from->{'file'}[$i],1737 hash=>$diffinfo->{'to_id'},1738 hash_base=>$hash,1739 file_name=>$to->{'file'}),1740-class=>"path",1741-title=>"diff". ($i+1)},1742$i+1) .1743'/'.1744$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1745 esc_path($from->{'file'}[$i]));1746}else{1747$line='--- /dev/null';1748}1749$result.= qq!<div class="diff from_file">$line</div>\n!;1750}1751}17521753$line=$to_line;1754#assert($line =~ m/^\+\+\+/) if DEBUG;1755# no extra formatting for "^+++ /dev/null"1756if($line=~m!^\+\+\+ "?b/!) {1757if($to->{'href'}) {1758$line='+++ b/'.1759$cgi->a({-href=>$to->{'href'}, -class=>"path"},1760 esc_path($to->{'file'}));1761}else{1762$line='+++ b/'.1763 esc_path($to->{'file'});1764}1765}1766$result.= qq!<div class="diff to_file">$line</div>\n!;17671768return$result;1769}17701771# create note for patch simplified by combined diff1772sub format_diff_cc_simplified {1773my($diffinfo,@parents) =@_;1774my$result='';17751776$result.="<div class=\"diff header\">".1777"diff --cc ";1778if(!is_deleted($diffinfo)) {1779$result.=$cgi->a({-href => href(action=>"blob",1780 hash_base=>$hash,1781 hash=>$diffinfo->{'to_id'},1782 file_name=>$diffinfo->{'to_file'}),1783-class=>"path"},1784 esc_path($diffinfo->{'to_file'}));1785}else{1786$result.= esc_path($diffinfo->{'to_file'});1787}1788$result.="</div>\n".# class="diff header"1789"<div class=\"diff nodifferences\">".1790"Simple merge".1791"</div>\n";# class="diff nodifferences"17921793return$result;1794}17951796# format patch (diff) line (not to be used for diff headers)1797sub format_diff_line {1798my$line=shift;1799my($from,$to) =@_;1800my$diff_class="";18011802chomp$line;18031804if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1805# combined diff1806my$prefix=substr($line,0,scalar@{$from->{'href'}});1807if($line=~m/^\@{3}/) {1808$diff_class=" chunk_header";1809}elsif($line=~m/^\\/) {1810$diff_class=" incomplete";1811}elsif($prefix=~tr/+/+/) {1812$diff_class=" add";1813}elsif($prefix=~tr/-/-/) {1814$diff_class=" rem";1815}1816}else{1817# assume ordinary diff1818my$char=substr($line,0,1);1819if($chareq'+') {1820$diff_class=" add";1821}elsif($chareq'-') {1822$diff_class=" rem";1823}elsif($chareq'@') {1824$diff_class=" chunk_header";1825}elsif($chareq"\\") {1826$diff_class=" incomplete";1827}1828}1829$line= untabify($line);1830if($from&&$to&&$line=~m/^\@{2} /) {1831my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1832$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;18331834$from_lines=0unlessdefined$from_lines;1835$to_lines=0unlessdefined$to_lines;18361837if($from->{'href'}) {1838$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1839-class=>"list"},$from_text);1840}1841if($to->{'href'}) {1842$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1843-class=>"list"},$to_text);1844}1845$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1846"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1847return"<div class=\"diff$diff_class\">$line</div>\n";1848}elsif($from&&$to&&$line=~m/^\@{3}/) {1849my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1850my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);18511852@from_text=split(' ',$ranges);1853for(my$i=0;$i<@from_text; ++$i) {1854($from_start[$i],$from_nlines[$i]) =1855(split(',',substr($from_text[$i],1)),0);1856}18571858$to_text=pop@from_text;1859$to_start=pop@from_start;1860$to_nlines=pop@from_nlines;18611862$line="<span class=\"chunk_info\">$prefix";1863for(my$i=0;$i<@from_text; ++$i) {1864if($from->{'href'}[$i]) {1865$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1866-class=>"list"},$from_text[$i]);1867}else{1868$line.=$from_text[$i];1869}1870$line.=" ";1871}1872if($to->{'href'}) {1873$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1874-class=>"list"},$to_text);1875}else{1876$line.=$to_text;1877}1878$line.="$prefix</span>".1879"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1880return"<div class=\"diff$diff_class\">$line</div>\n";1881}1882return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1883}18841885# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1886# linked. Pass the hash of the tree/commit to snapshot.1887sub format_snapshot_links {1888my($hash) =@_;1889my$num_fmts=@snapshot_fmts;1890if($num_fmts>1) {1891# A parenthesized list of links bearing format names.1892# e.g. "snapshot (_tar.gz_ _zip_)"1893return"snapshot (".join(' ',map1894$cgi->a({1895-href => href(1896 action=>"snapshot",1897 hash=>$hash,1898 snapshot_format=>$_1899)1900},$known_snapshot_formats{$_}{'display'})1901,@snapshot_fmts) .")";1902}elsif($num_fmts==1) {1903# A single "snapshot" link whose tooltip bears the format name.1904# i.e. "_snapshot_"1905my($fmt) =@snapshot_fmts;1906return1907$cgi->a({1908-href => href(1909 action=>"snapshot",1910 hash=>$hash,1911 snapshot_format=>$fmt1912),1913-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1914},"snapshot");1915}else{# $num_fmts == 01916returnundef;1917}1918}19191920## ......................................................................1921## functions returning values to be passed, perhaps after some1922## transformation, to other functions; e.g. returning arguments to href()19231924# returns hash to be passed to href to generate gitweb URL1925# in -title key it returns description of link1926sub get_feed_info {1927my$format=shift||'Atom';1928my%res= (action =>lc($format));19291930# feed links are possible only for project views1931return unless(defined$project);1932# some views should link to OPML, or to generic project feed,1933# or don't have specific feed yet (so they should use generic)1934return if($action=~/^(?:tags|heads|forks|tag|search)$/x);19351936my$branch;1937# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1938# from tag links; this also makes possible to detect branch links1939if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1940(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1941$branch=$1;1942}1943# find log type for feed description (title)1944my$type='log';1945if(defined$file_name) {1946$type="history of$file_name";1947$type.="/"if($actioneq'tree');1948$type.=" on '$branch'"if(defined$branch);1949}else{1950$type="log of$branch"if(defined$branch);1951}19521953$res{-title} =$type;1954$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1955$res{'file_name'} =$file_name;19561957return%res;1958}19591960## ----------------------------------------------------------------------1961## git utility subroutines, invoking git commands19621963# returns path to the core git executable and the --git-dir parameter as list1964sub git_cmd {1965return$GIT,'--git-dir='.$git_dir;1966}19671968# quote the given arguments for passing them to the shell1969# quote_command("command", "arg 1", "arg with ' and ! characters")1970# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1971# Try to avoid using this function wherever possible.1972sub quote_command {1973returnjoin(' ',1974map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);1975}19761977# get HEAD ref of given project as hash1978sub git_get_head_hash {1979my$project=shift;1980my$o_git_dir=$git_dir;1981my$retval=undef;1982$git_dir="$projectroot/$project";1983if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1984my$head= <$fd>;1985close$fd;1986if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1987$retval=$1;1988}1989}1990if(defined$o_git_dir) {1991$git_dir=$o_git_dir;1992}1993return$retval;1994}19951996# get type of given object1997sub git_get_type {1998my$hash=shift;19992000open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2001my$type= <$fd>;2002close$fdorreturn;2003chomp$type;2004return$type;2005}20062007# repository configuration2008our$config_file='';2009our%config;20102011# store multiple values for single key as anonymous array reference2012# single values stored directly in the hash, not as [ <value> ]2013sub hash_set_multi {2014my($hash,$key,$value) =@_;20152016if(!exists$hash->{$key}) {2017$hash->{$key} =$value;2018}elsif(!ref$hash->{$key}) {2019$hash->{$key} = [$hash->{$key},$value];2020}else{2021push@{$hash->{$key}},$value;2022}2023}20242025# return hash of git project configuration2026# optionally limited to some section, e.g. 'gitweb'2027sub git_parse_project_config {2028my$section_regexp=shift;2029my%config;20302031local$/="\0";20322033open my$fh,"-|", git_cmd(),"config",'-z','-l',2034orreturn;20352036while(my$keyval= <$fh>) {2037chomp$keyval;2038my($key,$value) =split(/\n/,$keyval,2);20392040 hash_set_multi(\%config,$key,$value)2041if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2042}2043close$fh;20442045return%config;2046}20472048# convert config value to boolean: 'true' or 'false'2049# no value, number > 0, 'true' and 'yes' values are true2050# rest of values are treated as false (never as error)2051sub config_to_bool {2052my$val=shift;20532054return1if!defined$val;# section.key20552056# strip leading and trailing whitespace2057$val=~s/^\s+//;2058$val=~s/\s+$//;20592060return(($val=~/^\d+$/&&$val) ||# section.key = 12061($val=~/^(?:true|yes)$/i));# section.key = true2062}20632064# convert config value to simple decimal number2065# an optional value suffix of 'k', 'm', or 'g' will cause the value2066# to be multiplied by 1024, 1048576, or 10737418242067sub config_to_int {2068my$val=shift;20692070# strip leading and trailing whitespace2071$val=~s/^\s+//;2072$val=~s/\s+$//;20732074if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2075$unit=lc($unit);2076# unknown unit is treated as 12077return$num* ($uniteq'g'?1073741824:2078$uniteq'm'?1048576:2079$uniteq'k'?1024:1);2080}2081return$val;2082}20832084# convert config value to array reference, if needed2085sub config_to_multi {2086my$val=shift;20872088returnref($val) ?$val: (defined($val) ? [$val] : []);2089}20902091sub git_get_project_config {2092my($key,$type) =@_;20932094# key sanity check2095return unless($key);2096$key=~s/^gitweb\.//;2097return if($key=~m/\W/);20982099# type sanity check2100if(defined$type) {2101$type=~s/^--//;2102$type=undef2103unless($typeeq'bool'||$typeeq'int');2104}21052106# get config2107if(!defined$config_file||2108$config_filene"$git_dir/config") {2109%config= git_parse_project_config('gitweb');2110$config_file="$git_dir/config";2111}21122113# check if config variable (key) exists2114return unlessexists$config{"gitweb.$key"};21152116# ensure given type2117if(!defined$type) {2118return$config{"gitweb.$key"};2119}elsif($typeeq'bool') {2120# backward compatibility: 'git config --bool' returns true/false2121return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2122}elsif($typeeq'int') {2123return config_to_int($config{"gitweb.$key"});2124}2125return$config{"gitweb.$key"};2126}21272128# get hash of given path at given ref2129sub git_get_hash_by_path {2130my$base=shift;2131my$path=shift||returnundef;2132my$type=shift;21332134$path=~ s,/+$,,;21352136open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2137or die_error(500,"Open git-ls-tree failed");2138my$line= <$fd>;2139close$fdorreturnundef;21402141if(!defined$line) {2142# there is no tree or hash given by $path at $base2143returnundef;2144}21452146#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2147$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2148if(defined$type&&$typene$2) {2149# type doesn't match2150returnundef;2151}2152return$3;2153}21542155# get path of entry with given hash at given tree-ish (ref)2156# used to get 'from' filename for combined diff (merge commit) for renames2157sub git_get_path_by_hash {2158my$base=shift||return;2159my$hash=shift||return;21602161local$/="\0";21622163open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2164orreturnundef;2165while(my$line= <$fd>) {2166chomp$line;21672168#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2169#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2170if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2171close$fd;2172return$1;2173}2174}2175close$fd;2176returnundef;2177}21782179## ......................................................................2180## git utility functions, directly accessing git repository21812182sub git_get_project_description {2183my$path=shift;21842185$git_dir="$projectroot/$path";2186open my$fd,'<',"$git_dir/description"2187orreturn git_get_project_config('description');2188my$descr= <$fd>;2189close$fd;2190if(defined$descr) {2191chomp$descr;2192}2193return$descr;2194}21952196sub git_get_project_ctags {2197my$path=shift;2198my$ctags= {};21992200$git_dir="$projectroot/$path";2201opendir my$dh,"$git_dir/ctags"2202orreturn$ctags;2203foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2204open my$ct,'<',$_ornext;2205my$val= <$ct>;2206chomp$val;2207close$ct;2208my$ctag=$_;$ctag=~ s#.*/##;2209$ctags->{$ctag} =$val;2210}2211closedir$dh;2212$ctags;2213}22142215sub git_populate_project_tagcloud {2216my$ctags=shift;22172218# First, merge different-cased tags; tags vote on casing2219my%ctags_lc;2220foreach(keys%$ctags) {2221$ctags_lc{lc$_}->{count} +=$ctags->{$_};2222if(not$ctags_lc{lc$_}->{topcount}2223or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2224$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2225$ctags_lc{lc$_}->{topname} =$_;2226}2227}22282229my$cloud;2230if(eval{require HTML::TagCloud;1; }) {2231$cloud= HTML::TagCloud->new;2232foreach(sort keys%ctags_lc) {2233# Pad the title with spaces so that the cloud looks2234# less crammed.2235my$title=$ctags_lc{$_}->{topname};2236$title=~s/ / /g;2237$title=~s/^/ /g;2238$title=~s/$/ /g;2239$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2240}2241}else{2242$cloud= \%ctags_lc;2243}2244$cloud;2245}22462247sub git_show_project_tagcloud {2248my($cloud,$count) =@_;2249print STDERR ref($cloud)."..\n";2250if(ref$cloudeq'HTML::TagCloud') {2251return$cloud->html_and_css($count);2252}else{2253my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2254return'<p align="center">'.join(', ',map{2255$cgi->a({-href=>"$home_link?by_tag=$_"},$cloud->{$_}->{topname})2256}splice(@tags,0,$count)) .'</p>';2257}2258}22592260sub git_get_project_url_list {2261my$path=shift;22622263$git_dir="$projectroot/$path";2264open my$fd,'<',"$git_dir/cloneurl"2265orreturnwantarray?2266@{ config_to_multi(git_get_project_config('url')) } :2267 config_to_multi(git_get_project_config('url'));2268my@git_project_url_list=map{chomp;$_} <$fd>;2269close$fd;22702271returnwantarray?@git_project_url_list: \@git_project_url_list;2272}22732274sub git_get_projects_list {2275my($filter) =@_;2276my@list;22772278$filter||='';2279$filter=~s/\.git$//;22802281my$check_forks= gitweb_check_feature('forks');22822283if(-d $projects_list) {2284# search in directory2285my$dir=$projects_list. ($filter?"/$filter":'');2286# remove the trailing "/"2287$dir=~s!/+$!!;2288my$pfxlen=length("$dir");2289my$pfxdepth= ($dir=~tr!/!!);22902291 File::Find::find({2292 follow_fast =>1,# follow symbolic links2293 follow_skip =>2,# ignore duplicates2294 dangling_symlinks =>0,# ignore dangling symlinks, silently2295 wanted =>sub{2296# skip project-list toplevel, if we get it.2297return if(m!^[/.]$!);2298# only directories can be git repositories2299return unless(-d $_);2300# don't traverse too deep (Find is super slow on os x)2301if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2302$File::Find::prune =1;2303return;2304}23052306my$subdir=substr($File::Find::name,$pfxlen+1);2307# we check related file in $projectroot2308my$path= ($filter?"$filter/":'') .$subdir;2309if(check_export_ok("$projectroot/$path")) {2310push@list, { path =>$path};2311$File::Find::prune =1;2312}2313},2314},"$dir");23152316}elsif(-f $projects_list) {2317# read from file(url-encoded):2318# 'git%2Fgit.git Linus+Torvalds'2319# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2320# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2321my%paths;2322open my$fd,'<',$projects_listorreturn;2323 PROJECT:2324while(my$line= <$fd>) {2325chomp$line;2326my($path,$owner) =split' ',$line;2327$path= unescape($path);2328$owner= unescape($owner);2329if(!defined$path) {2330next;2331}2332if($filterne'') {2333# looking for forks;2334my$pfx=substr($path,0,length($filter));2335if($pfxne$filter) {2336next PROJECT;2337}2338my$sfx=substr($path,length($filter));2339if($sfx!~/^\/.*\.git$/) {2340next PROJECT;2341}2342}elsif($check_forks) {2343 PATH:2344foreachmy$filter(keys%paths) {2345# looking for forks;2346my$pfx=substr($path,0,length($filter));2347if($pfxne$filter) {2348next PATH;2349}2350my$sfx=substr($path,length($filter));2351if($sfx!~/^\/.*\.git$/) {2352next PATH;2353}2354# is a fork, don't include it in2355# the list2356next PROJECT;2357}2358}2359if(check_export_ok("$projectroot/$path")) {2360my$pr= {2361 path =>$path,2362 owner => to_utf8($owner),2363};2364push@list,$pr;2365(my$forks_path=$path) =~s/\.git$//;2366$paths{$forks_path}++;2367}2368}2369close$fd;2370}2371return@list;2372}23732374our$gitweb_project_owner=undef;2375sub git_get_project_list_from_file {23762377return if(defined$gitweb_project_owner);23782379$gitweb_project_owner= {};2380# read from file (url-encoded):2381# 'git%2Fgit.git Linus+Torvalds'2382# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2383# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2384if(-f $projects_list) {2385open(my$fd,'<',$projects_list);2386while(my$line= <$fd>) {2387chomp$line;2388my($pr,$ow) =split' ',$line;2389$pr= unescape($pr);2390$ow= unescape($ow);2391$gitweb_project_owner->{$pr} = to_utf8($ow);2392}2393close$fd;2394}2395}23962397sub git_get_project_owner {2398my$project=shift;2399my$owner;24002401returnundefunless$project;2402$git_dir="$projectroot/$project";24032404if(!defined$gitweb_project_owner) {2405 git_get_project_list_from_file();2406}24072408if(exists$gitweb_project_owner->{$project}) {2409$owner=$gitweb_project_owner->{$project};2410}2411if(!defined$owner){2412$owner= git_get_project_config('owner');2413}2414if(!defined$owner) {2415$owner= get_file_owner("$git_dir");2416}24172418return$owner;2419}24202421sub git_get_last_activity {2422my($path) =@_;2423my$fd;24242425$git_dir="$projectroot/$path";2426open($fd,"-|", git_cmd(),'for-each-ref',2427'--format=%(committer)',2428'--sort=-committerdate',2429'--count=1',2430'refs/heads')orreturn;2431my$most_recent= <$fd>;2432close$fdorreturn;2433if(defined$most_recent&&2434$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2435my$timestamp=$1;2436my$age=time-$timestamp;2437return($age, age_string($age));2438}2439return(undef,undef);2440}24412442sub git_get_references {2443my$type=shift||"";2444my%refs;2445# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112446# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2447open my$fd,"-|", git_cmd(),"show-ref","--dereference",2448($type? ("--","refs/$type") : ())# use -- <pattern> if $type2449orreturn;24502451while(my$line= <$fd>) {2452chomp$line;2453if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2454if(defined$refs{$1}) {2455push@{$refs{$1}},$2;2456}else{2457$refs{$1} = [$2];2458}2459}2460}2461close$fdorreturn;2462return \%refs;2463}24642465sub git_get_rev_name_tags {2466my$hash=shift||returnundef;24672468open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2469orreturn;2470my$name_rev= <$fd>;2471close$fd;24722473if($name_rev=~ m|^$hash tags/(.*)$|) {2474return$1;2475}else{2476# catches also '$hash undefined' output2477returnundef;2478}2479}24802481## ----------------------------------------------------------------------2482## parse to hash functions24832484sub parse_date {2485my$epoch=shift;2486my$tz=shift||"-0000";24872488my%date;2489my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2490my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2491my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2492$date{'hour'} =$hour;2493$date{'minute'} =$min;2494$date{'mday'} =$mday;2495$date{'day'} =$days[$wday];2496$date{'month'} =$months[$mon];2497$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2498$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2499$date{'mday-time'} =sprintf"%d%s%02d:%02d",2500$mday,$months[$mon],$hour,$min;2501$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",25021900+$year,1+$mon,$mday,$hour,$min,$sec;25032504$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2505my$local=$epoch+ ((int$1+ ($2/60)) *3600);2506($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2507$date{'hour_local'} =$hour;2508$date{'minute_local'} =$min;2509$date{'tz_local'} =$tz;2510$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",25111900+$year,$mon+1,$mday,2512$hour,$min,$sec,$tz);2513return%date;2514}25152516sub parse_tag {2517my$tag_id=shift;2518my%tag;2519my@comment;25202521open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2522$tag{'id'} =$tag_id;2523while(my$line= <$fd>) {2524chomp$line;2525if($line=~m/^object ([0-9a-fA-F]{40})$/) {2526$tag{'object'} =$1;2527}elsif($line=~m/^type (.+)$/) {2528$tag{'type'} =$1;2529}elsif($line=~m/^tag (.+)$/) {2530$tag{'name'} =$1;2531}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2532$tag{'author'} =$1;2533$tag{'author_epoch'} =$2;2534$tag{'author_tz'} =$3;2535if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2536$tag{'author_name'} =$1;2537$tag{'author_email'} =$2;2538}else{2539$tag{'author_name'} =$tag{'author'};2540}2541}elsif($line=~m/--BEGIN/) {2542push@comment,$line;2543last;2544}elsif($lineeq"") {2545last;2546}2547}2548push@comment, <$fd>;2549$tag{'comment'} = \@comment;2550close$fdorreturn;2551if(!defined$tag{'name'}) {2552return2553};2554return%tag2555}25562557sub parse_commit_text {2558my($commit_text,$withparents) =@_;2559my@commit_lines=split'\n',$commit_text;2560my%co;25612562pop@commit_lines;# Remove '\0'25632564if(!@commit_lines) {2565return;2566}25672568my$header=shift@commit_lines;2569if($header!~m/^[0-9a-fA-F]{40}/) {2570return;2571}2572($co{'id'},my@parents) =split' ',$header;2573while(my$line=shift@commit_lines) {2574last if$lineeq"\n";2575if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2576$co{'tree'} =$1;2577}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2578push@parents,$1;2579}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2580$co{'author'} = to_utf8($1);2581$co{'author_epoch'} =$2;2582$co{'author_tz'} =$3;2583if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2584$co{'author_name'} =$1;2585$co{'author_email'} =$2;2586}else{2587$co{'author_name'} =$co{'author'};2588}2589}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2590$co{'committer'} = to_utf8($1);2591$co{'committer_epoch'} =$2;2592$co{'committer_tz'} =$3;2593if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2594$co{'committer_name'} =$1;2595$co{'committer_email'} =$2;2596}else{2597$co{'committer_name'} =$co{'committer'};2598}2599}2600}2601if(!defined$co{'tree'}) {2602return;2603};2604$co{'parents'} = \@parents;2605$co{'parent'} =$parents[0];26062607foreachmy$title(@commit_lines) {2608$title=~s/^ //;2609if($titlene"") {2610$co{'title'} = chop_str($title,80,5);2611# remove leading stuff of merges to make the interesting part visible2612if(length($title) >50) {2613$title=~s/^Automatic //;2614$title=~s/^merge (of|with) /Merge ... /i;2615if(length($title) >50) {2616$title=~s/(http|rsync):\/\///;2617}2618if(length($title) >50) {2619$title=~s/(master|www|rsync)\.//;2620}2621if(length($title) >50) {2622$title=~s/kernel.org:?//;2623}2624if(length($title) >50) {2625$title=~s/\/pub\/scm//;2626}2627}2628$co{'title_short'} = chop_str($title,50,5);2629last;2630}2631}2632if(!defined$co{'title'} ||$co{'title'}eq"") {2633$co{'title'} =$co{'title_short'} ='(no commit message)';2634}2635# remove added spaces2636foreachmy$line(@commit_lines) {2637$line=~s/^ //;2638}2639$co{'comment'} = \@commit_lines;26402641my$age=time-$co{'committer_epoch'};2642$co{'age'} =$age;2643$co{'age_string'} = age_string($age);2644my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2645if($age>60*60*24*7*2) {2646$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2647$co{'age_string_age'} =$co{'age_string'};2648}else{2649$co{'age_string_date'} =$co{'age_string'};2650$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2651}2652return%co;2653}26542655sub parse_commit {2656my($commit_id) =@_;2657my%co;26582659local$/="\0";26602661open my$fd,"-|", git_cmd(),"rev-list",2662"--parents",2663"--header",2664"--max-count=1",2665$commit_id,2666"--",2667or die_error(500,"Open git-rev-list failed");2668%co= parse_commit_text(<$fd>,1);2669close$fd;26702671return%co;2672}26732674sub parse_commits {2675my($commit_id,$maxcount,$skip,$filename,@args) =@_;2676my@cos;26772678$maxcount||=1;2679$skip||=0;26802681local$/="\0";26822683open my$fd,"-|", git_cmd(),"rev-list",2684"--header",2685@args,2686("--max-count=".$maxcount),2687("--skip=".$skip),2688@extra_options,2689$commit_id,2690"--",2691($filename? ($filename) : ())2692or die_error(500,"Open git-rev-list failed");2693while(my$line= <$fd>) {2694my%co= parse_commit_text($line);2695push@cos, \%co;2696}2697close$fd;26982699returnwantarray?@cos: \@cos;2700}27012702# parse line of git-diff-tree "raw" output2703sub parse_difftree_raw_line {2704my$line=shift;2705my%res;27062707# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2708# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2709if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2710$res{'from_mode'} =$1;2711$res{'to_mode'} =$2;2712$res{'from_id'} =$3;2713$res{'to_id'} =$4;2714$res{'status'} =$5;2715$res{'similarity'} =$6;2716if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2717($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2718}else{2719$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2720}2721}2722# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2723# combined diff (for merge commit)2724elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2725$res{'nparents'} =length($1);2726$res{'from_mode'} = [split(' ',$2) ];2727$res{'to_mode'} =pop@{$res{'from_mode'}};2728$res{'from_id'} = [split(' ',$3) ];2729$res{'to_id'} =pop@{$res{'from_id'}};2730$res{'status'} = [split('',$4) ];2731$res{'to_file'} = unquote($5);2732}2733# 'c512b523472485aef4fff9e57b229d9d243c967f'2734elsif($line=~m/^([0-9a-fA-F]{40})$/) {2735$res{'commit'} =$1;2736}27372738returnwantarray?%res: \%res;2739}27402741# wrapper: return parsed line of git-diff-tree "raw" output2742# (the argument might be raw line, or parsed info)2743sub parsed_difftree_line {2744my$line_or_ref=shift;27452746if(ref($line_or_ref)eq"HASH") {2747# pre-parsed (or generated by hand)2748return$line_or_ref;2749}else{2750return parse_difftree_raw_line($line_or_ref);2751}2752}27532754# parse line of git-ls-tree output2755sub parse_ls_tree_line {2756my$line=shift;2757my%opts=@_;2758my%res;27592760#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2761$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;27622763$res{'mode'} =$1;2764$res{'type'} =$2;2765$res{'hash'} =$3;2766if($opts{'-z'}) {2767$res{'name'} =$4;2768}else{2769$res{'name'} = unquote($4);2770}27712772returnwantarray?%res: \%res;2773}27742775# generates _two_ hashes, references to which are passed as 2 and 3 argument2776sub parse_from_to_diffinfo {2777my($diffinfo,$from,$to,@parents) =@_;27782779if($diffinfo->{'nparents'}) {2780# combined diff2781$from->{'file'} = [];2782$from->{'href'} = [];2783 fill_from_file_info($diffinfo,@parents)2784unlessexists$diffinfo->{'from_file'};2785for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2786$from->{'file'}[$i] =2787defined$diffinfo->{'from_file'}[$i] ?2788$diffinfo->{'from_file'}[$i] :2789$diffinfo->{'to_file'};2790if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2791$from->{'href'}[$i] = href(action=>"blob",2792 hash_base=>$parents[$i],2793 hash=>$diffinfo->{'from_id'}[$i],2794 file_name=>$from->{'file'}[$i]);2795}else{2796$from->{'href'}[$i] =undef;2797}2798}2799}else{2800# ordinary (not combined) diff2801$from->{'file'} =$diffinfo->{'from_file'};2802if($diffinfo->{'status'}ne"A") {# not new (added) file2803$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2804 hash=>$diffinfo->{'from_id'},2805 file_name=>$from->{'file'});2806}else{2807delete$from->{'href'};2808}2809}28102811$to->{'file'} =$diffinfo->{'to_file'};2812if(!is_deleted($diffinfo)) {# file exists in result2813$to->{'href'} = href(action=>"blob", hash_base=>$hash,2814 hash=>$diffinfo->{'to_id'},2815 file_name=>$to->{'file'});2816}else{2817delete$to->{'href'};2818}2819}28202821## ......................................................................2822## parse to array of hashes functions28232824sub git_get_heads_list {2825my$limit=shift;2826my@headslist;28272828open my$fd,'-|', git_cmd(),'for-each-ref',2829($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2830'--format=%(objectname) %(refname) %(subject)%00%(committer)',2831'refs/heads'2832orreturn;2833while(my$line= <$fd>) {2834my%ref_item;28352836chomp$line;2837my($refinfo,$committerinfo) =split(/\0/,$line);2838my($hash,$name,$title) =split(' ',$refinfo,3);2839my($committer,$epoch,$tz) =2840($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2841$ref_item{'fullname'} =$name;2842$name=~s!^refs/heads/!!;28432844$ref_item{'name'} =$name;2845$ref_item{'id'} =$hash;2846$ref_item{'title'} =$title||'(no commit message)';2847$ref_item{'epoch'} =$epoch;2848if($epoch) {2849$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2850}else{2851$ref_item{'age'} ="unknown";2852}28532854push@headslist, \%ref_item;2855}2856close$fd;28572858returnwantarray?@headslist: \@headslist;2859}28602861sub git_get_tags_list {2862my$limit=shift;2863my@tagslist;28642865open my$fd,'-|', git_cmd(),'for-each-ref',2866($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2867'--format=%(objectname) %(objecttype) %(refname) '.2868'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2869'refs/tags'2870orreturn;2871while(my$line= <$fd>) {2872my%ref_item;28732874chomp$line;2875my($refinfo,$creatorinfo) =split(/\0/,$line);2876my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2877my($creator,$epoch,$tz) =2878($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2879$ref_item{'fullname'} =$name;2880$name=~s!^refs/tags/!!;28812882$ref_item{'type'} =$type;2883$ref_item{'id'} =$id;2884$ref_item{'name'} =$name;2885if($typeeq"tag") {2886$ref_item{'subject'} =$title;2887$ref_item{'reftype'} =$reftype;2888$ref_item{'refid'} =$refid;2889}else{2890$ref_item{'reftype'} =$type;2891$ref_item{'refid'} =$id;2892}28932894if($typeeq"tag"||$typeeq"commit") {2895$ref_item{'epoch'} =$epoch;2896if($epoch) {2897$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2898}else{2899$ref_item{'age'} ="unknown";2900}2901}29022903push@tagslist, \%ref_item;2904}2905close$fd;29062907returnwantarray?@tagslist: \@tagslist;2908}29092910## ----------------------------------------------------------------------2911## filesystem-related functions29122913sub get_file_owner {2914my$path=shift;29152916my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2917my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2918if(!defined$gcos) {2919returnundef;2920}2921my$owner=$gcos;2922$owner=~s/[,;].*$//;2923return to_utf8($owner);2924}29252926# assume that file exists2927sub insert_file {2928my$filename=shift;29292930open my$fd,'<',$filename;2931print map{ to_utf8($_) } <$fd>;2932close$fd;2933}29342935## ......................................................................2936## mimetype related functions29372938sub mimetype_guess_file {2939my$filename=shift;2940my$mimemap=shift;2941-r $mimemaporreturnundef;29422943my%mimemap;2944open(my$mh,'<',$mimemap)orreturnundef;2945while(<$mh>) {2946next ifm/^#/;# skip comments2947my($mimetype,$exts) =split(/\t+/);2948if(defined$exts) {2949my@exts=split(/\s+/,$exts);2950foreachmy$ext(@exts) {2951$mimemap{$ext} =$mimetype;2952}2953}2954}2955close($mh);29562957$filename=~/\.([^.]*)$/;2958return$mimemap{$1};2959}29602961sub mimetype_guess {2962my$filename=shift;2963my$mime;2964$filename=~/\./orreturnundef;29652966if($mimetypes_file) {2967my$file=$mimetypes_file;2968if($file!~m!^/!) {# if it is relative path2969# it is relative to project2970$file="$projectroot/$project/$file";2971}2972$mime= mimetype_guess_file($filename,$file);2973}2974$mime||= mimetype_guess_file($filename,'/etc/mime.types');2975return$mime;2976}29772978sub blob_mimetype {2979my$fd=shift;2980my$filename=shift;29812982if($filename) {2983my$mime= mimetype_guess($filename);2984$mimeandreturn$mime;2985}29862987# just in case2988return$default_blob_plain_mimetypeunless$fd;29892990if(-T $fd) {2991return'text/plain';2992}elsif(!$filename) {2993return'application/octet-stream';2994}elsif($filename=~m/\.png$/i) {2995return'image/png';2996}elsif($filename=~m/\.gif$/i) {2997return'image/gif';2998}elsif($filename=~m/\.jpe?g$/i) {2999return'image/jpeg';3000}else{3001return'application/octet-stream';3002}3003}30043005sub blob_contenttype {3006my($fd,$file_name,$type) =@_;30073008$type||= blob_mimetype($fd,$file_name);3009if($typeeq'text/plain'&&defined$default_text_plain_charset) {3010$type.="; charset=$default_text_plain_charset";3011}30123013return$type;3014}30153016## ======================================================================3017## functions printing HTML: header, footer, error page30183019sub git_header_html {3020my$status=shift||"200 OK";3021my$expires=shift;30223023my$title="$site_name";3024if(defined$project) {3025$title.=" - ". to_utf8($project);3026if(defined$action) {3027$title.="/$action";3028if(defined$file_name) {3029$title.=" - ". esc_path($file_name);3030if($actioneq"tree"&&$file_name!~ m|/$|) {3031$title.="/";3032}3033}3034}3035}3036my$content_type;3037# require explicit support from the UA if we are to send the page as3038# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3039# we have to do this because MSIE sometimes globs '*/*', pretending to3040# support xhtml+xml but choking when it gets what it asked for.3041if(defined$cgi->http('HTTP_ACCEPT') &&3042$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3043$cgi->Accept('application/xhtml+xml') !=0) {3044$content_type='application/xhtml+xml';3045}else{3046$content_type='text/html';3047}3048print$cgi->header(-type=>$content_type, -charset =>'utf-8',3049-status=>$status, -expires =>$expires);3050my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3051print<<EOF;3052<?xml version="1.0" encoding="utf-8"?>3053<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3054<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3055<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3056<!-- git core binaries version$git_version-->3057<head>3058<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3059<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3060<meta name="robots" content="index, nofollow"/>3061<title>$title</title>3062EOF3063# the stylesheet, favicon etc urls won't work correctly with path_info3064# unless we set the appropriate base URL3065if($ENV{'PATH_INFO'}) {3066print"<base href=\"".esc_url($base_url)."\"/>\n";3067}3068# print out each stylesheet that exist, providing backwards capability3069# for those people who defined $stylesheet in a config file3070if(defined$stylesheet) {3071print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3072}else{3073foreachmy$stylesheet(@stylesheets) {3074next unless$stylesheet;3075print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3076}3077}3078if(defined$project) {3079my%href_params= get_feed_info();3080if(!exists$href_params{'-title'}) {3081$href_params{'-title'} ='log';3082}30833084foreachmy$formatqw(RSS Atom){3085my$type=lc($format);3086my%link_attr= (3087'-rel'=>'alternate',3088'-title'=> esc_attr("$project-$href_params{'-title'} -$formatfeed"),3089'-type'=>"application/$type+xml"3090);30913092$href_params{'action'} =$type;3093$link_attr{'-href'} = href(%href_params);3094print"<link ".3095"rel=\"$link_attr{'-rel'}\"".3096"title=\"$link_attr{'-title'}\"".3097"href=\"$link_attr{'-href'}\"".3098"type=\"$link_attr{'-type'}\"".3099"/>\n";31003101$href_params{'extra_options'} ='--no-merges';3102$link_attr{'-href'} = href(%href_params);3103$link_attr{'-title'} .=' (no merges)';3104print"<link ".3105"rel=\"$link_attr{'-rel'}\"".3106"title=\"$link_attr{'-title'}\"".3107"href=\"$link_attr{'-href'}\"".3108"type=\"$link_attr{'-type'}\"".3109"/>\n";3110}31113112}else{3113printf('<link rel="alternate" title="%sprojects list" '.3114'href="%s" type="text/plain; charset=utf-8" />'."\n",3115 esc_attr($site_name), href(project=>undef, action=>"project_index"));3116printf('<link rel="alternate" title="%sprojects feeds" '.3117'href="%s" type="text/x-opml" />'."\n",3118 esc_attr($site_name), href(project=>undef, action=>"opml"));3119}3120if(defined$favicon) {3121printqq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);3122}31233124print"</head>\n".3125"<body>\n";31263127if(-f $site_header) {3128 insert_file($site_header);3129}31303131print"<div class=\"page_header\">\n".3132$cgi->a({-href => esc_url($logo_url),3133-title =>$logo_label},3134qq(<img src=").esc_url($logo).qq(" width="72" height="27" alt="git" class="logo"/>));3135print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3136if(defined$project) {3137print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3138if(defined$action) {3139print" /$action";3140}3141print"\n";3142}3143print"</div>\n";31443145my$have_search= gitweb_check_feature('search');3146if(defined$project&&$have_search) {3147if(!defined$searchtext) {3148$searchtext="";3149}3150my$search_hash;3151if(defined$hash_base) {3152$search_hash=$hash_base;3153}elsif(defined$hash) {3154$search_hash=$hash;3155}else{3156$search_hash="HEAD";3157}3158my$action=$my_uri;3159my$use_pathinfo= gitweb_check_feature('pathinfo');3160if($use_pathinfo) {3161$action.="/".esc_url($project);3162}3163print$cgi->startform(-method=>"get", -action =>$action) .3164"<div class=\"search\">\n".3165(!$use_pathinfo&&3166$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3167$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3168$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3169$cgi->popup_menu(-name =>'st', -default=>'commit',3170-values=> ['commit','grep','author','committer','pickaxe']) .3171$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3172" search:\n",3173$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3174"<span title=\"Extended regular expression\">".3175$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3176-checked =>$search_use_regexp) .3177"</span>".3178"</div>".3179$cgi->end_form() ."\n";3180}3181}31823183sub git_footer_html {3184my$feed_class='rss_logo';31853186print"<div class=\"page_footer\">\n";3187if(defined$project) {3188my$descr= git_get_project_description($project);3189if(defined$descr) {3190print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3191}31923193my%href_params= get_feed_info();3194if(!%href_params) {3195$feed_class.=' generic';3196}3197$href_params{'-title'} ||='log';31983199foreachmy$formatqw(RSS Atom){3200$href_params{'action'} =lc($format);3201print$cgi->a({-href => href(%href_params),3202-title =>"$href_params{'-title'}$formatfeed",3203-class=>$feed_class},$format)."\n";3204}32053206}else{3207print$cgi->a({-href => href(project=>undef, action=>"opml"),3208-class=>$feed_class},"OPML") ." ";3209print$cgi->a({-href => href(project=>undef, action=>"project_index"),3210-class=>$feed_class},"TXT") ."\n";3211}3212print"</div>\n";# class="page_footer"32133214if(-f $site_footer) {3215 insert_file($site_footer);3216}32173218print"</body>\n".3219"</html>";3220}32213222# die_error(<http_status_code>, <error_message>)3223# Example: die_error(404, 'Hash not found')3224# By convention, use the following status codes (as defined in RFC 2616):3225# 400: Invalid or missing CGI parameters, or3226# requested object exists but has wrong type.3227# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3228# this server or project.3229# 404: Requested object/revision/project doesn't exist.3230# 500: The server isn't configured properly, or3231# an internal error occurred (e.g. failed assertions caused by bugs), or3232# an unknown error occurred (e.g. the git binary died unexpectedly).3233sub die_error {3234my$status=shift||500;3235my$error=shift||"Internal server error";32363237my%http_responses= (400=>'400 Bad Request',3238403=>'403 Forbidden',3239404=>'404 Not Found',3240500=>'500 Internal Server Error');3241 git_header_html($http_responses{$status});3242print<<EOF;3243<div class="page_body">3244<br /><br />3245$status-$error3246<br />3247</div>3248EOF3249 git_footer_html();3250exit;3251}32523253## ----------------------------------------------------------------------3254## functions printing or outputting HTML: navigation32553256sub git_print_page_nav {3257my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3258$extra=''if!defined$extra;# pager or formats32593260my@navs=qw(summary shortlog log commit commitdiff tree);3261if($suppress) {3262@navs=grep{$_ne$suppress}@navs;3263}32643265my%arg=map{$_=> {action=>$_} }@navs;3266if(defined$head) {3267for(qw(commit commitdiff)) {3268$arg{$_}{'hash'} =$head;3269}3270if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3271for(qw(shortlog log)) {3272$arg{$_}{'hash'} =$head;3273}3274}3275}32763277$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3278$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;32793280my@actions= gitweb_get_feature('actions');3281my%repl= (3282'%'=>'%',3283'n'=>$project,# project name3284'f'=>$git_dir,# project path within filesystem3285'h'=>$treehead||'',# current hash ('h' parameter)3286'b'=>$treebase||'',# hash base ('hb' parameter)3287);3288while(@actions) {3289my($label,$link,$pos) =splice(@actions,0,3);3290# insert3291@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3292# munch munch3293$link=~s/%([%nfhb])/$repl{$1}/g;3294$arg{$label}{'_href'} =$link;3295}32963297print"<div class=\"page_nav\">\n".3298(join" | ",3299map{$_eq$current?3300$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3301}@navs);3302print"<br/>\n$extra<br/>\n".3303"</div>\n";3304}33053306sub format_paging_nav {3307my($action,$hash,$head,$page,$has_next_link) =@_;3308my$paging_nav;330933103311if($hashne$head||$page) {3312$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3313}else{3314$paging_nav.="HEAD";3315}33163317if($page>0) {3318$paging_nav.=" ⋅ ".3319$cgi->a({-href => href(-replay=>1, page=>$page-1),3320-accesskey =>"p", -title =>"Alt-p"},"prev");3321}else{3322$paging_nav.=" ⋅ prev";3323}33243325if($has_next_link) {3326$paging_nav.=" ⋅ ".3327$cgi->a({-href => href(-replay=>1, page=>$page+1),3328-accesskey =>"n", -title =>"Alt-n"},"next");3329}else{3330$paging_nav.=" ⋅ next";3331}33323333return$paging_nav;3334}33353336## ......................................................................3337## functions printing or outputting HTML: div33383339sub git_print_header_div {3340my($action,$title,$hash,$hash_base) =@_;3341my%args= ();33423343$args{'action'} =$action;3344$args{'hash'} =$hashif$hash;3345$args{'hash_base'} =$hash_baseif$hash_base;33463347print"<div class=\"header\">\n".3348$cgi->a({-href => href(%args), -class=>"title"},3349$title?$title:$action) .3350"\n</div>\n";3351}33523353sub print_local_time {3354my%date=@_;3355if($date{'hour_local'} <6) {3356printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3357$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3358}else{3359printf(" (%02d:%02d%s)",3360$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3361}3362}33633364# Outputs the author name and date in long form3365sub git_print_authorship {3366my$co=shift;3367my%opts=@_;3368my$tag=$opts{-tag} ||'div';33693370my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3371print"<$tagclass=\"author_date\">".3372 esc_html($co->{'author_name'}) .3373" [$ad{'rfc2822'}";3374 print_local_time(%ad)if($opts{-localtime});3375print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3376."</$tag>\n";3377}33783379# Outputs table rows containing the full author or committer information,3380# in the format expected for 'commit' view (& similia).3381# Parameters are a commit hash reference, followed by the list of people3382# to output information for. If the list is empty it defalts to both3383# author and committer.3384sub git_print_authorship_rows {3385my$co=shift;3386# too bad we can't use @people = @_ || ('author', 'committer')3387my@people=@_;3388@people= ('author','committer')unless@people;3389foreachmy$who(@people) {3390my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3391print"<tr><td>$who</td><td>". esc_html($co->{$who}) ."</td>".3392"<td rowspan=\"2\">".3393 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3394"</td></tr>\n".3395"<tr>".3396"<td></td><td>$wd{'rfc2822'}";3397 print_local_time(%wd);3398print"</td>".3399"</tr>\n";3400}3401}34023403sub git_print_page_path {3404my$name=shift;3405my$type=shift;3406my$hb=shift;340734083409print"<div class=\"page_path\">";3410print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3411-title =>'tree root'}, to_utf8("[$project]"));3412print" / ";3413if(defined$name) {3414my@dirname=split'/',$name;3415my$basename=pop@dirname;3416my$fullname='';34173418foreachmy$dir(@dirname) {3419$fullname.= ($fullname?'/':'') .$dir;3420print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3421 hash_base=>$hb),3422-title =>$fullname}, esc_path($dir));3423print" / ";3424}3425if(defined$type&&$typeeq'blob') {3426print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3427 hash_base=>$hb),3428-title =>$name}, esc_path($basename));3429}elsif(defined$type&&$typeeq'tree') {3430print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3431 hash_base=>$hb),3432-title =>$name}, esc_path($basename));3433print" / ";3434}else{3435print esc_path($basename);3436}3437}3438print"<br/></div>\n";3439}34403441sub git_print_log {3442my$log=shift;3443my%opts=@_;34443445if($opts{'-remove_title'}) {3446# remove title, i.e. first line of log3447shift@$log;3448}3449# remove leading empty lines3450while(defined$log->[0] &&$log->[0]eq"") {3451shift@$log;3452}34533454# print log3455my$signoff=0;3456my$empty=0;3457foreachmy$line(@$log) {3458if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3459$signoff=1;3460$empty=0;3461if(!$opts{'-remove_signoff'}) {3462print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3463next;3464}else{3465# remove signoff lines3466next;3467}3468}else{3469$signoff=0;3470}34713472# print only one empty line3473# do not print empty line after signoff3474if($lineeq"") {3475next if($empty||$signoff);3476$empty=1;3477}else{3478$empty=0;3479}34803481print format_log_line_html($line) ."<br/>\n";3482}34833484if($opts{'-final_empty_line'}) {3485# end with single empty line3486print"<br/>\n"unless$empty;3487}3488}34893490# return link target (what link points to)3491sub git_get_link_target {3492my$hash=shift;3493my$link_target;34943495# read link3496open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3497orreturn;3498{3499local$/=undef;3500$link_target= <$fd>;3501}3502close$fd3503orreturn;35043505return$link_target;3506}35073508# given link target, and the directory (basedir) the link is in,3509# return target of link relative to top directory (top tree);3510# return undef if it is not possible (including absolute links).3511sub normalize_link_target {3512my($link_target,$basedir) =@_;35133514# absolute symlinks (beginning with '/') cannot be normalized3515return if(substr($link_target,0,1)eq'/');35163517# normalize link target to path from top (root) tree (dir)3518my$path;3519if($basedir) {3520$path=$basedir.'/'.$link_target;3521}else{3522# we are in top (root) tree (dir)3523$path=$link_target;3524}35253526# remove //, /./, and /../3527my@path_parts;3528foreachmy$part(split('/',$path)) {3529# discard '.' and ''3530next if(!$part||$parteq'.');3531# handle '..'3532if($parteq'..') {3533if(@path_parts) {3534pop@path_parts;3535}else{3536# link leads outside repository (outside top dir)3537return;3538}3539}else{3540push@path_parts,$part;3541}3542}3543$path=join('/',@path_parts);35443545return$path;3546}35473548# print tree entry (row of git_tree), but without encompassing <tr> element3549sub git_print_tree_entry {3550my($t,$basedir,$hash_base,$have_blame) =@_;35513552my%base_key= ();3553$base_key{'hash_base'} =$hash_baseifdefined$hash_base;35543555# The format of a table row is: mode list link. Where mode is3556# the mode of the entry, list is the name of the entry, an href,3557# and link is the action links of the entry.35583559print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3560if($t->{'type'}eq"blob") {3561print"<td class=\"list\">".3562$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3563 file_name=>"$basedir$t->{'name'}",%base_key),3564-class=>"list"}, esc_path($t->{'name'}));3565if(S_ISLNK(oct$t->{'mode'})) {3566my$link_target= git_get_link_target($t->{'hash'});3567if($link_target) {3568my$norm_target= normalize_link_target($link_target,$basedir);3569if(defined$norm_target) {3570print" -> ".3571$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3572 file_name=>$norm_target),3573-title =>$norm_target}, esc_path($link_target));3574}else{3575print" -> ". esc_path($link_target);3576}3577}3578}3579print"</td>\n";3580print"<td class=\"link\">";3581print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3582 file_name=>"$basedir$t->{'name'}",%base_key)},3583"blob");3584if($have_blame) {3585print" | ".3586$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3587 file_name=>"$basedir$t->{'name'}",%base_key)},3588"blame");3589}3590if(defined$hash_base) {3591print" | ".3592$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3593 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3594"history");3595}3596print" | ".3597$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3598 file_name=>"$basedir$t->{'name'}")},3599"raw");3600print"</td>\n";36013602}elsif($t->{'type'}eq"tree") {3603print"<td class=\"list\">";3604print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3605 file_name=>"$basedir$t->{'name'}",%base_key)},3606 esc_path($t->{'name'}));3607print"</td>\n";3608print"<td class=\"link\">";3609print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3610 file_name=>"$basedir$t->{'name'}",%base_key)},3611"tree");3612if(defined$hash_base) {3613print" | ".3614$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3615 file_name=>"$basedir$t->{'name'}")},3616"history");3617}3618print"</td>\n";3619}else{3620# unknown object: we can only present history for it3621# (this includes 'commit' object, i.e. submodule support)3622print"<td class=\"list\">".3623 esc_path($t->{'name'}) .3624"</td>\n";3625print"<td class=\"link\">";3626if(defined$hash_base) {3627print$cgi->a({-href => href(action=>"history",3628 hash_base=>$hash_base,3629 file_name=>"$basedir$t->{'name'}")},3630"history");3631}3632print"</td>\n";3633}3634}36353636## ......................................................................3637## functions printing large fragments of HTML36383639# get pre-image filenames for merge (combined) diff3640sub fill_from_file_info {3641my($diff,@parents) =@_;36423643$diff->{'from_file'} = [ ];3644$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3645for(my$i=0;$i<$diff->{'nparents'};$i++) {3646if($diff->{'status'}[$i]eq'R'||3647$diff->{'status'}[$i]eq'C') {3648$diff->{'from_file'}[$i] =3649 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3650}3651}36523653return$diff;3654}36553656# is current raw difftree line of file deletion3657sub is_deleted {3658my$diffinfo=shift;36593660return$diffinfo->{'to_id'}eq('0' x 40);3661}36623663# does patch correspond to [previous] difftree raw line3664# $diffinfo - hashref of parsed raw diff format3665# $patchinfo - hashref of parsed patch diff format3666# (the same keys as in $diffinfo)3667sub is_patch_split {3668my($diffinfo,$patchinfo) =@_;36693670returndefined$diffinfo&&defined$patchinfo3671&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3672}367336743675sub git_difftree_body {3676my($difftree,$hash,@parents) =@_;3677my($parent) =$parents[0];3678my$have_blame= gitweb_check_feature('blame');3679print"<div class=\"list_head\">\n";3680if($#{$difftree} >10) {3681print(($#{$difftree} +1) ." files changed:\n");3682}3683print"</div>\n";36843685print"<table class=\"".3686(@parents>1?"combined ":"") .3687"diff_tree\">\n";36883689# header only for combined diff in 'commitdiff' view3690my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3691if($has_header) {3692# table header3693print"<thead><tr>\n".3694"<th></th><th></th>\n";# filename, patchN link3695for(my$i=0;$i<@parents;$i++) {3696my$par=$parents[$i];3697print"<th>".3698$cgi->a({-href => href(action=>"commitdiff",3699 hash=>$hash, hash_parent=>$par),3700-title =>'commitdiff to parent number '.3701($i+1) .': '.substr($par,0,7)},3702$i+1) .3703" </th>\n";3704}3705print"</tr></thead>\n<tbody>\n";3706}37073708my$alternate=1;3709my$patchno=0;3710foreachmy$line(@{$difftree}) {3711my$diff= parsed_difftree_line($line);37123713if($alternate) {3714print"<tr class=\"dark\">\n";3715}else{3716print"<tr class=\"light\">\n";3717}3718$alternate^=1;37193720if(exists$diff->{'nparents'}) {# combined diff37213722 fill_from_file_info($diff,@parents)3723unlessexists$diff->{'from_file'};37243725if(!is_deleted($diff)) {3726# file exists in the result (child) commit3727print"<td>".3728$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3729 file_name=>$diff->{'to_file'},3730 hash_base=>$hash),3731-class=>"list"}, esc_path($diff->{'to_file'})) .3732"</td>\n";3733}else{3734print"<td>".3735 esc_path($diff->{'to_file'}) .3736"</td>\n";3737}37383739if($actioneq'commitdiff') {3740# link to patch3741$patchno++;3742print"<td class=\"link\">".3743$cgi->a({-href =>"#patch$patchno"},"patch") .3744" | ".3745"</td>\n";3746}37473748my$has_history=0;3749my$not_deleted=0;3750for(my$i=0;$i<$diff->{'nparents'};$i++) {3751my$hash_parent=$parents[$i];3752my$from_hash=$diff->{'from_id'}[$i];3753my$from_path=$diff->{'from_file'}[$i];3754my$status=$diff->{'status'}[$i];37553756$has_history||= ($statusne'A');3757$not_deleted||= ($statusne'D');37583759if($statuseq'A') {3760print"<td class=\"link\"align=\"right\"> | </td>\n";3761}elsif($statuseq'D') {3762print"<td class=\"link\">".3763$cgi->a({-href => href(action=>"blob",3764 hash_base=>$hash,3765 hash=>$from_hash,3766 file_name=>$from_path)},3767"blob". ($i+1)) .3768" | </td>\n";3769}else{3770if($diff->{'to_id'}eq$from_hash) {3771print"<td class=\"link nochange\">";3772}else{3773print"<td class=\"link\">";3774}3775print$cgi->a({-href => href(action=>"blobdiff",3776 hash=>$diff->{'to_id'},3777 hash_parent=>$from_hash,3778 hash_base=>$hash,3779 hash_parent_base=>$hash_parent,3780 file_name=>$diff->{'to_file'},3781 file_parent=>$from_path)},3782"diff". ($i+1)) .3783" | </td>\n";3784}3785}37863787print"<td class=\"link\">";3788if($not_deleted) {3789print$cgi->a({-href => href(action=>"blob",3790 hash=>$diff->{'to_id'},3791 file_name=>$diff->{'to_file'},3792 hash_base=>$hash)},3793"blob");3794print" | "if($has_history);3795}3796if($has_history) {3797print$cgi->a({-href => href(action=>"history",3798 file_name=>$diff->{'to_file'},3799 hash_base=>$hash)},3800"history");3801}3802print"</td>\n";38033804print"</tr>\n";3805next;# instead of 'else' clause, to avoid extra indent3806}3807# else ordinary diff38083809my($to_mode_oct,$to_mode_str,$to_file_type);3810my($from_mode_oct,$from_mode_str,$from_file_type);3811if($diff->{'to_mode'}ne('0' x 6)) {3812$to_mode_oct=oct$diff->{'to_mode'};3813if(S_ISREG($to_mode_oct)) {# only for regular file3814$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3815}3816$to_file_type= file_type($diff->{'to_mode'});3817}3818if($diff->{'from_mode'}ne('0' x 6)) {3819$from_mode_oct=oct$diff->{'from_mode'};3820if(S_ISREG($to_mode_oct)) {# only for regular file3821$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3822}3823$from_file_type= file_type($diff->{'from_mode'});3824}38253826if($diff->{'status'}eq"A") {# created3827my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3828$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3829$mode_chng.="]</span>";3830print"<td>";3831print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3832 hash_base=>$hash, file_name=>$diff->{'file'}),3833-class=>"list"}, esc_path($diff->{'file'}));3834print"</td>\n";3835print"<td>$mode_chng</td>\n";3836print"<td class=\"link\">";3837if($actioneq'commitdiff') {3838# link to patch3839$patchno++;3840print$cgi->a({-href =>"#patch$patchno"},"patch");3841print" | ";3842}3843print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3844 hash_base=>$hash, file_name=>$diff->{'file'})},3845"blob");3846print"</td>\n";38473848}elsif($diff->{'status'}eq"D") {# deleted3849my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3850print"<td>";3851print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3852 hash_base=>$parent, file_name=>$diff->{'file'}),3853-class=>"list"}, esc_path($diff->{'file'}));3854print"</td>\n";3855print"<td>$mode_chng</td>\n";3856print"<td class=\"link\">";3857if($actioneq'commitdiff') {3858# link to patch3859$patchno++;3860print$cgi->a({-href =>"#patch$patchno"},"patch");3861print" | ";3862}3863print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3864 hash_base=>$parent, file_name=>$diff->{'file'})},3865"blob") ." | ";3866if($have_blame) {3867print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3868 file_name=>$diff->{'file'})},3869"blame") ." | ";3870}3871print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3872 file_name=>$diff->{'file'})},3873"history");3874print"</td>\n";38753876}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3877my$mode_chnge="";3878if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3879$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3880if($from_file_typene$to_file_type) {3881$mode_chnge.=" from$from_file_typeto$to_file_type";3882}3883if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3884if($from_mode_str&&$to_mode_str) {3885$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3886}elsif($to_mode_str) {3887$mode_chnge.=" mode:$to_mode_str";3888}3889}3890$mode_chnge.="]</span>\n";3891}3892print"<td>";3893print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3894 hash_base=>$hash, file_name=>$diff->{'file'}),3895-class=>"list"}, esc_path($diff->{'file'}));3896print"</td>\n";3897print"<td>$mode_chnge</td>\n";3898print"<td class=\"link\">";3899if($actioneq'commitdiff') {3900# link to patch3901$patchno++;3902print$cgi->a({-href =>"#patch$patchno"},"patch") .3903" | ";3904}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3905# "commit" view and modified file (not onlu mode changed)3906print$cgi->a({-href => href(action=>"blobdiff",3907 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3908 hash_base=>$hash, hash_parent_base=>$parent,3909 file_name=>$diff->{'file'})},3910"diff") .3911" | ";3912}3913print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3914 hash_base=>$hash, file_name=>$diff->{'file'})},3915"blob") ." | ";3916if($have_blame) {3917print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3918 file_name=>$diff->{'file'})},3919"blame") ." | ";3920}3921print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3922 file_name=>$diff->{'file'})},3923"history");3924print"</td>\n";39253926}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3927my%status_name= ('R'=>'moved','C'=>'copied');3928my$nstatus=$status_name{$diff->{'status'}};3929my$mode_chng="";3930if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3931# mode also for directories, so we cannot use $to_mode_str3932$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3933}3934print"<td>".3935$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3936 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3937-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3938"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3939$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3940 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3941-class=>"list"}, esc_path($diff->{'from_file'})) .3942" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3943"<td class=\"link\">";3944if($actioneq'commitdiff') {3945# link to patch3946$patchno++;3947print$cgi->a({-href =>"#patch$patchno"},"patch") .3948" | ";3949}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3950# "commit" view and modified file (not only pure rename or copy)3951print$cgi->a({-href => href(action=>"blobdiff",3952 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3953 hash_base=>$hash, hash_parent_base=>$parent,3954 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3955"diff") .3956" | ";3957}3958print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3959 hash_base=>$parent, file_name=>$diff->{'to_file'})},3960"blob") ." | ";3961if($have_blame) {3962print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3963 file_name=>$diff->{'to_file'})},3964"blame") ." | ";3965}3966print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3967 file_name=>$diff->{'to_file'})},3968"history");3969print"</td>\n";39703971}# we should not encounter Unmerged (U) or Unknown (X) status3972print"</tr>\n";3973}3974print"</tbody>"if$has_header;3975print"</table>\n";3976}39773978sub git_patchset_body {3979my($fd,$difftree,$hash,@hash_parents) =@_;3980my($hash_parent) =$hash_parents[0];39813982my$is_combined= (@hash_parents>1);3983my$patch_idx=0;3984my$patch_number=0;3985my$patch_line;3986my$diffinfo;3987my$to_name;3988my(%from,%to);39893990print"<div class=\"patchset\">\n";39913992# skip to first patch3993while($patch_line= <$fd>) {3994chomp$patch_line;39953996last if($patch_line=~m/^diff /);3997}39983999 PATCH:4000while($patch_line) {40014002# parse "git diff" header line4003if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4004# $1 is from_name, which we do not use4005$to_name= unquote($2);4006$to_name=~s!^b/!!;4007}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4008# $1 is 'cc' or 'combined', which we do not use4009$to_name= unquote($2);4010}else{4011$to_name=undef;4012}40134014# check if current patch belong to current raw line4015# and parse raw git-diff line if needed4016if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4017# this is continuation of a split patch4018print"<div class=\"patch cont\">\n";4019}else{4020# advance raw git-diff output if needed4021$patch_idx++ifdefined$diffinfo;40224023# read and prepare patch information4024$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);40254026# compact combined diff output can have some patches skipped4027# find which patch (using pathname of result) we are at now;4028if($is_combined) {4029while($to_namene$diffinfo->{'to_file'}) {4030print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4031 format_diff_cc_simplified($diffinfo,@hash_parents) .4032"</div>\n";# class="patch"40334034$patch_idx++;4035$patch_number++;40364037last if$patch_idx>$#$difftree;4038$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4039}4040}40414042# modifies %from, %to hashes4043 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);40444045# this is first patch for raw difftree line with $patch_idx index4046# we index @$difftree array from 0, but number patches from 14047print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4048}40494050# git diff header4051#assert($patch_line =~ m/^diff /) if DEBUG;4052#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4053$patch_number++;4054# print "git diff" header4055print format_git_diff_header_line($patch_line,$diffinfo,4056 \%from, \%to);40574058# print extended diff header4059print"<div class=\"diff extended_header\">\n";4060 EXTENDED_HEADER:4061while($patch_line= <$fd>) {4062chomp$patch_line;40634064last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);40654066print format_extended_diff_header_line($patch_line,$diffinfo,4067 \%from, \%to);4068}4069print"</div>\n";# class="diff extended_header"40704071# from-file/to-file diff header4072if(!$patch_line) {4073print"</div>\n";# class="patch"4074last PATCH;4075}4076next PATCH if($patch_line=~m/^diff /);4077#assert($patch_line =~ m/^---/) if DEBUG;40784079my$last_patch_line=$patch_line;4080$patch_line= <$fd>;4081chomp$patch_line;4082#assert($patch_line =~ m/^\+\+\+/) if DEBUG;40834084print format_diff_from_to_header($last_patch_line,$patch_line,4085$diffinfo, \%from, \%to,4086@hash_parents);40874088# the patch itself4089 LINE:4090while($patch_line= <$fd>) {4091chomp$patch_line;40924093next PATCH if($patch_line=~m/^diff /);40944095print format_diff_line($patch_line, \%from, \%to);4096}40974098}continue{4099print"</div>\n";# class="patch"4100}41014102# for compact combined (--cc) format, with chunk and patch simpliciaction4103# patchset might be empty, but there might be unprocessed raw lines4104for(++$patch_idxif$patch_number>0;4105$patch_idx<@$difftree;4106++$patch_idx) {4107# read and prepare patch information4108$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);41094110# generate anchor for "patch" links in difftree / whatchanged part4111print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4112 format_diff_cc_simplified($diffinfo,@hash_parents) .4113"</div>\n";# class="patch"41144115$patch_number++;4116}41174118if($patch_number==0) {4119if(@hash_parents>1) {4120print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4121}else{4122print"<div class=\"diff nodifferences\">No differences found</div>\n";4123}4124}41254126print"</div>\n";# class="patchset"4127}41284129# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41304131# fills project list info (age, description, owner, forks) for each4132# project in the list, removing invalid projects from returned list4133# NOTE: modifies $projlist, but does not remove entries from it4134sub fill_project_list_info {4135my($projlist,$check_forks) =@_;4136my@projects;41374138my$show_ctags= gitweb_check_feature('ctags');4139 PROJECT:4140foreachmy$pr(@$projlist) {4141my(@activity) = git_get_last_activity($pr->{'path'});4142unless(@activity) {4143next PROJECT;4144}4145($pr->{'age'},$pr->{'age_string'}) =@activity;4146if(!defined$pr->{'descr'}) {4147my$descr= git_get_project_description($pr->{'path'}) ||"";4148$descr= to_utf8($descr);4149$pr->{'descr_long'} =$descr;4150$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4151}4152if(!defined$pr->{'owner'}) {4153$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4154}4155if($check_forks) {4156my$pname=$pr->{'path'};4157if(($pname=~s/\.git$//) &&4158($pname!~/\/$/) &&4159(-d "$projectroot/$pname")) {4160$pr->{'forks'} ="-d$projectroot/$pname";4161}else{4162$pr->{'forks'} =0;4163}4164}4165$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4166push@projects,$pr;4167}41684169return@projects;4170}41714172# print 'sort by' <th> element, generating 'sort by $name' replay link4173# if that order is not selected4174sub print_sort_th {4175my($name,$order,$header) =@_;4176$header||=ucfirst($name);41774178if($ordereq$name) {4179print"<th>$header</th>\n";4180}else{4181print"<th>".4182$cgi->a({-href => href(-replay=>1, order=>$name),4183-class=>"header"},$header) .4184"</th>\n";4185}4186}41874188sub git_project_list_body {4189# actually uses global variable $project4190my($projlist,$order,$from,$to,$extra,$no_header) =@_;41914192my$check_forks= gitweb_check_feature('forks');4193my@projects= fill_project_list_info($projlist,$check_forks);41944195$order||=$default_projects_order;4196$from=0unlessdefined$from;4197$to=$#projectsif(!defined$to||$#projects<$to);41984199my%order_info= (4200 project => { key =>'path', type =>'str'},4201 descr => { key =>'descr_long', type =>'str'},4202 owner => { key =>'owner', type =>'str'},4203 age => { key =>'age', type =>'num'}4204);4205my$oi=$order_info{$order};4206if($oi->{'type'}eq'str') {4207@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4208}else{4209@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4210}42114212my$show_ctags= gitweb_check_feature('ctags');4213if($show_ctags) {4214my%ctags;4215foreachmy$p(@projects) {4216foreachmy$ct(keys%{$p->{'ctags'}}) {4217$ctags{$ct} +=$p->{'ctags'}->{$ct};4218}4219}4220my$cloud= git_populate_project_tagcloud(\%ctags);4221print git_show_project_tagcloud($cloud,64);4222}42234224print"<table class=\"project_list\">\n";4225unless($no_header) {4226print"<tr>\n";4227if($check_forks) {4228print"<th></th>\n";4229}4230 print_sort_th('project',$order,'Project');4231 print_sort_th('descr',$order,'Description');4232 print_sort_th('owner',$order,'Owner');4233 print_sort_th('age',$order,'Last Change');4234print"<th></th>\n".# for links4235"</tr>\n";4236}4237my$alternate=1;4238my$tagfilter=$cgi->param('by_tag');4239for(my$i=$from;$i<=$to;$i++) {4240my$pr=$projects[$i];42414242next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4243next if$searchtextand not$pr->{'path'} =~/$searchtext/4244and not$pr->{'descr_long'} =~/$searchtext/;4245# Weed out forks or non-matching entries of search4246if($check_forks) {4247my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4248$forkbase="^$forkbase"if$forkbase;4249next ifnot$searchtextand not$tagfilterand$show_ctags4250and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4251}42524253if($alternate) {4254print"<tr class=\"dark\">\n";4255}else{4256print"<tr class=\"light\">\n";4257}4258$alternate^=1;4259if($check_forks) {4260print"<td>";4261if($pr->{'forks'}) {4262print"<!--$pr->{'forks'} -->\n";4263print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4264}4265print"</td>\n";4266}4267print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4268-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4269"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4270-class=>"list", -title =>$pr->{'descr_long'}},4271 esc_html($pr->{'descr'})) ."</td>\n".4272"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4273print"<td class=\"". age_class($pr->{'age'}) ."\">".4274(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4275"<td class=\"link\">".4276$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4277$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4278$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4279$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4280($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4281"</td>\n".4282"</tr>\n";4283}4284if(defined$extra) {4285print"<tr>\n";4286if($check_forks) {4287print"<td></td>\n";4288}4289print"<td colspan=\"5\">$extra</td>\n".4290"</tr>\n";4291}4292print"</table>\n";4293}42944295sub git_shortlog_body {4296# uses global variable $project4297my($commitlist,$from,$to,$refs,$extra) =@_;42984299$from=0unlessdefined$from;4300$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43014302print"<table class=\"shortlog\">\n";4303my$alternate=1;4304for(my$i=$from;$i<=$to;$i++) {4305my%co= %{$commitlist->[$i]};4306my$commit=$co{'id'};4307my$ref= format_ref_marker($refs,$commit);4308if($alternate) {4309print"<tr class=\"dark\">\n";4310}else{4311print"<tr class=\"light\">\n";4312}4313$alternate^=1;4314# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4315print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4316 format_author_html('td', \%co,10) ."<td>";4317print format_subject_html($co{'title'},$co{'title_short'},4318 href(action=>"commit", hash=>$commit),$ref);4319print"</td>\n".4320"<td class=\"link\">".4321$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4322$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4323$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4324my$snapshot_links= format_snapshot_links($commit);4325if(defined$snapshot_links) {4326print" | ".$snapshot_links;4327}4328print"</td>\n".4329"</tr>\n";4330}4331if(defined$extra) {4332print"<tr>\n".4333"<td colspan=\"4\">$extra</td>\n".4334"</tr>\n";4335}4336print"</table>\n";4337}43384339sub git_history_body {4340# Warning: assumes constant type (blob or tree) during history4341my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;43424343$from=0unlessdefined$from;4344$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});43454346print"<table class=\"history\">\n";4347my$alternate=1;4348for(my$i=$from;$i<=$to;$i++) {4349my%co= %{$commitlist->[$i]};4350if(!%co) {4351next;4352}4353my$commit=$co{'id'};43544355my$ref= format_ref_marker($refs,$commit);43564357if($alternate) {4358print"<tr class=\"dark\">\n";4359}else{4360print"<tr class=\"light\">\n";4361}4362$alternate^=1;4363print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4364# shortlog: format_author_html('td', \%co, 10)4365 format_author_html('td', \%co,15,3) ."<td>";4366# originally git_history used chop_str($co{'title'}, 50)4367print format_subject_html($co{'title'},$co{'title_short'},4368 href(action=>"commit", hash=>$commit),$ref);4369print"</td>\n".4370"<td class=\"link\">".4371$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4372$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");43734374if($ftypeeq'blob') {4375my$blob_current= git_get_hash_by_path($hash_base,$file_name);4376my$blob_parent= git_get_hash_by_path($commit,$file_name);4377if(defined$blob_current&&defined$blob_parent&&4378$blob_currentne$blob_parent) {4379print" | ".4380$cgi->a({-href => href(action=>"blobdiff",4381 hash=>$blob_current, hash_parent=>$blob_parent,4382 hash_base=>$hash_base, hash_parent_base=>$commit,4383 file_name=>$file_name)},4384"diff to current");4385}4386}4387print"</td>\n".4388"</tr>\n";4389}4390if(defined$extra) {4391print"<tr>\n".4392"<td colspan=\"4\">$extra</td>\n".4393"</tr>\n";4394}4395print"</table>\n";4396}43974398sub git_tags_body {4399# uses global variable $project4400my($taglist,$from,$to,$extra) =@_;4401$from=0unlessdefined$from;4402$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);44034404print"<table class=\"tags\">\n";4405my$alternate=1;4406for(my$i=$from;$i<=$to;$i++) {4407my$entry=$taglist->[$i];4408my%tag=%$entry;4409my$comment=$tag{'subject'};4410my$comment_short;4411if(defined$comment) {4412$comment_short= chop_str($comment,30,5);4413}4414if($alternate) {4415print"<tr class=\"dark\">\n";4416}else{4417print"<tr class=\"light\">\n";4418}4419$alternate^=1;4420if(defined$tag{'age'}) {4421print"<td><i>$tag{'age'}</i></td>\n";4422}else{4423print"<td></td>\n";4424}4425print"<td>".4426$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4427-class=>"list name"}, esc_html($tag{'name'})) .4428"</td>\n".4429"<td>";4430if(defined$comment) {4431print format_subject_html($comment,$comment_short,4432 href(action=>"tag", hash=>$tag{'id'}));4433}4434print"</td>\n".4435"<td class=\"selflink\">";4436if($tag{'type'}eq"tag") {4437print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4438}else{4439print" ";4440}4441print"</td>\n".4442"<td class=\"link\">"." | ".4443$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4444if($tag{'reftype'}eq"commit") {4445print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4446" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4447}elsif($tag{'reftype'}eq"blob") {4448print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4449}4450print"</td>\n".4451"</tr>";4452}4453if(defined$extra) {4454print"<tr>\n".4455"<td colspan=\"5\">$extra</td>\n".4456"</tr>\n";4457}4458print"</table>\n";4459}44604461sub git_heads_body {4462# uses global variable $project4463my($headlist,$head,$from,$to,$extra) =@_;4464$from=0unlessdefined$from;4465$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);44664467print"<table class=\"heads\">\n";4468my$alternate=1;4469for(my$i=$from;$i<=$to;$i++) {4470my$entry=$headlist->[$i];4471my%ref=%$entry;4472my$curr=$ref{'id'}eq$head;4473if($alternate) {4474print"<tr class=\"dark\">\n";4475}else{4476print"<tr class=\"light\">\n";4477}4478$alternate^=1;4479print"<td><i>$ref{'age'}</i></td>\n".4480($curr?"<td class=\"current_head\">":"<td>") .4481$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4482-class=>"list name"},esc_html($ref{'name'})) .4483"</td>\n".4484"<td class=\"link\">".4485$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4486$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4487$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4488"</td>\n".4489"</tr>";4490}4491if(defined$extra) {4492print"<tr>\n".4493"<td colspan=\"3\">$extra</td>\n".4494"</tr>\n";4495}4496print"</table>\n";4497}44984499sub git_search_grep_body {4500my($commitlist,$from,$to,$extra) =@_;4501$from=0unlessdefined$from;4502$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45034504print"<table class=\"commit_search\">\n";4505my$alternate=1;4506for(my$i=$from;$i<=$to;$i++) {4507my%co= %{$commitlist->[$i]};4508if(!%co) {4509next;4510}4511my$commit=$co{'id'};4512if($alternate) {4513print"<tr class=\"dark\">\n";4514}else{4515print"<tr class=\"light\">\n";4516}4517$alternate^=1;4518print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4519 format_author_html('td', \%co,15,5) .4520"<td>".4521$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4522-class=>"list subject"},4523 chop_and_escape_str($co{'title'},50) ."<br/>");4524my$comment=$co{'comment'};4525foreachmy$line(@$comment) {4526if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4527my($lead,$match,$trail) = ($1,$2,$3);4528$match= chop_str($match,70,5,'center');4529my$contextlen=int((80-length($match))/2);4530$contextlen=30if($contextlen>30);4531$lead= chop_str($lead,$contextlen,10,'left');4532$trail= chop_str($trail,$contextlen,10,'right');45334534$lead= esc_html($lead);4535$match= esc_html($match);4536$trail= esc_html($trail);45374538print"$lead<span class=\"match\">$match</span>$trail<br />";4539}4540}4541print"</td>\n".4542"<td class=\"link\">".4543$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4544" | ".4545$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4546" | ".4547$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4548print"</td>\n".4549"</tr>\n";4550}4551if(defined$extra) {4552print"<tr>\n".4553"<td colspan=\"3\">$extra</td>\n".4554"</tr>\n";4555}4556print"</table>\n";4557}45584559## ======================================================================4560## ======================================================================4561## actions45624563sub git_project_list {4564my$order=$input_params{'order'};4565if(defined$order&&$order!~m/none|project|descr|owner|age/) {4566 die_error(400,"Unknown order parameter");4567}45684569my@list= git_get_projects_list();4570if(!@list) {4571 die_error(404,"No projects found");4572}45734574 git_header_html();4575if(-f $home_text) {4576print"<div class=\"index_include\">\n";4577 insert_file($home_text);4578print"</div>\n";4579}4580print$cgi->startform(-method=>"get") .4581"<p class=\"projsearch\">Search:\n".4582$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4583"</p>".4584$cgi->end_form() ."\n";4585 git_project_list_body(\@list,$order);4586 git_footer_html();4587}45884589sub git_forks {4590my$order=$input_params{'order'};4591if(defined$order&&$order!~m/none|project|descr|owner|age/) {4592 die_error(400,"Unknown order parameter");4593}45944595my@list= git_get_projects_list($project);4596if(!@list) {4597 die_error(404,"No forks found");4598}45994600 git_header_html();4601 git_print_page_nav('','');4602 git_print_header_div('summary',"$projectforks");4603 git_project_list_body(\@list,$order);4604 git_footer_html();4605}46064607sub git_project_index {4608my@projects= git_get_projects_list($project);46094610print$cgi->header(4611-type =>'text/plain',4612-charset =>'utf-8',4613-content_disposition =>'inline; filename="index.aux"');46144615foreachmy$pr(@projects) {4616if(!exists$pr->{'owner'}) {4617$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4618}46194620my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4621# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4622$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4623$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4624$path=~s/ /\+/g;4625$owner=~s/ /\+/g;46264627print"$path$owner\n";4628}4629}46304631sub git_summary {4632my$descr= git_get_project_description($project) ||"none";4633my%co= parse_commit("HEAD");4634my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4635my$head=$co{'id'};46364637my$owner= git_get_project_owner($project);46384639my$refs= git_get_references();4640# These get_*_list functions return one more to allow us to see if4641# there are more ...4642my@taglist= git_get_tags_list(16);4643my@headlist= git_get_heads_list(16);4644my@forklist;4645my$check_forks= gitweb_check_feature('forks');46464647if($check_forks) {4648@forklist= git_get_projects_list($project);4649}46504651 git_header_html();4652 git_print_page_nav('summary','',$head);46534654print"<div class=\"title\"> </div>\n";4655print"<table class=\"projects_list\">\n".4656"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4657"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4658if(defined$cd{'rfc2822'}) {4659print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4660}46614662# use per project git URL list in $projectroot/$project/cloneurl4663# or make project git URL from git base URL and project name4664my$url_tag="URL";4665my@url_list= git_get_project_url_list($project);4666@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4667foreachmy$git_url(@url_list) {4668next unless$git_url;4669print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4670$url_tag="";4671}46724673# Tag cloud4674my$show_ctags= gitweb_check_feature('ctags');4675if($show_ctags) {4676my$ctags= git_get_project_ctags($project);4677my$cloud= git_populate_project_tagcloud($ctags);4678print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4679print"</td>\n<td>"unless%$ctags;4680print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4681print"</td>\n<td>"if%$ctags;4682print git_show_project_tagcloud($cloud,48);4683print"</td></tr>";4684}46854686print"</table>\n";46874688# If XSS prevention is on, we don't include README.html.4689# TODO: Allow a readme in some safe format.4690if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4691print"<div class=\"title\">readme</div>\n".4692"<div class=\"readme\">\n";4693 insert_file("$projectroot/$project/README.html");4694print"\n</div>\n";# class="readme"4695}46964697# we need to request one more than 16 (0..15) to check if4698# those 16 are all4699my@commitlist=$head? parse_commits($head,17) : ();4700if(@commitlist) {4701 git_print_header_div('shortlog');4702 git_shortlog_body(\@commitlist,0,15,$refs,4703$#commitlist<=15?undef:4704$cgi->a({-href => href(action=>"shortlog")},"..."));4705}47064707if(@taglist) {4708 git_print_header_div('tags');4709 git_tags_body(\@taglist,0,15,4710$#taglist<=15?undef:4711$cgi->a({-href => href(action=>"tags")},"..."));4712}47134714if(@headlist) {4715 git_print_header_div('heads');4716 git_heads_body(\@headlist,$head,0,15,4717$#headlist<=15?undef:4718$cgi->a({-href => href(action=>"heads")},"..."));4719}47204721if(@forklist) {4722 git_print_header_div('forks');4723 git_project_list_body(\@forklist,'age',0,15,4724$#forklist<=15?undef:4725$cgi->a({-href => href(action=>"forks")},"..."),4726'no_header');4727}47284729 git_footer_html();4730}47314732sub git_tag {4733my$head= git_get_head_hash($project);4734 git_header_html();4735 git_print_page_nav('','',$head,undef,$head);4736my%tag= parse_tag($hash);47374738if(!%tag) {4739 die_error(404,"Unknown tag object");4740}47414742 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4743print"<div class=\"title_text\">\n".4744"<table class=\"object_header\">\n".4745"<tr>\n".4746"<td>object</td>\n".4747"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4748$tag{'object'}) ."</td>\n".4749"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4750$tag{'type'}) ."</td>\n".4751"</tr>\n";4752if(defined($tag{'author'})) {4753 git_print_authorship_rows(\%tag,'author');4754}4755print"</table>\n\n".4756"</div>\n";4757print"<div class=\"page_body\">";4758my$comment=$tag{'comment'};4759foreachmy$line(@$comment) {4760chomp$line;4761print esc_html($line, -nbsp=>1) ."<br/>\n";4762}4763print"</div>\n";4764 git_footer_html();4765}47664767sub git_blame {4768# permissions4769 gitweb_check_feature('blame')4770or die_error(403,"Blame view not allowed");47714772# error checking4773 die_error(400,"No file name given")unless$file_name;4774$hash_base||= git_get_head_hash($project);4775 die_error(404,"Couldn't find base commit")unless$hash_base;4776my%co= parse_commit($hash_base)4777or die_error(404,"Commit not found");4778my$ftype="blob";4779if(!defined$hash) {4780$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4781or die_error(404,"Error looking up file");4782}else{4783$ftype= git_get_type($hash);4784if($ftype!~"blob") {4785 die_error(400,"Object is not a blob");4786}4787}47884789# run git-blame --porcelain4790open my$fd,"-|", git_cmd(),"blame",'-p',4791$hash_base,'--',$file_name4792or die_error(500,"Open git-blame failed");47934794# page header4795 git_header_html();4796my$formats_nav=4797$cgi->a({-href => href(action=>"blob", -replay=>1)},4798"blob") .4799" | ".4800$cgi->a({-href => href(action=>"history", -replay=>1)},4801"history") .4802" | ".4803$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4804"HEAD");4805 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4806 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4807 git_print_page_path($file_name,$ftype,$hash_base);48084809# page body4810my@rev_color=qw(light2 dark2);4811my$num_colors=scalar(@rev_color);4812my$current_color=0;4813my%metainfo= ();48144815print<<HTML;4816<div class="page_body">4817<table class="blame">4818<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4819HTML4820 LINE:4821while(my$line= <$fd>) {4822chomp$line;4823# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4824# no <lines in group> for subsequent lines in group of lines4825my($full_rev,$orig_lineno,$lineno,$group_size) =4826($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4827if(!exists$metainfo{$full_rev}) {4828$metainfo{$full_rev} = {};4829}4830my$meta=$metainfo{$full_rev};4831my$data;4832while($data= <$fd>) {4833chomp$data;4834last if($data=~s/^\t//);# contents of line4835if($data=~/^(\S+) (.*)$/) {4836$meta->{$1} =$2;4837}4838}4839my$short_rev=substr($full_rev,0,8);4840my$author=$meta->{'author'};4841my%date=4842 parse_date($meta->{'author-time'},$meta->{'author-tz'});4843my$date=$date{'iso-tz'};4844if($group_size) {4845$current_color= ($current_color+1) %$num_colors;4846}4847print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4848if($group_size) {4849print"<td class=\"sha1\"";4850print" title=\"". esc_html($author) .",$date\"";4851print" rowspan=\"$group_size\""if($group_size>1);4852print">";4853print$cgi->a({-href => href(action=>"commit",4854 hash=>$full_rev,4855 file_name=>$file_name)},4856 esc_html($short_rev));4857print"</td>\n";4858}4859my$parent_commit;4860if(!exists$meta->{'parent'}) {4861open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4862or die_error(500,"Open git-rev-parse failed");4863$parent_commit= <$dd>;4864close$dd;4865chomp($parent_commit);4866$meta->{'parent'} =$parent_commit;4867}else{4868$parent_commit=$meta->{'parent'};4869}4870my$blamed= href(action =>'blame',4871 file_name =>$meta->{'filename'},4872 hash_base =>$parent_commit);4873print"<td class=\"linenr\">";4874print$cgi->a({ -href =>"$blamed#l$orig_lineno",4875-class=>"linenr"},4876 esc_html($lineno));4877print"</td>";4878print"<td class=\"pre\">". esc_html($data) ."</td>\n";4879print"</tr>\n";4880}4881print"</table>\n";4882print"</div>";4883close$fd4884or print"Reading blob failed\n";48854886# page footer4887 git_footer_html();4888}48894890sub git_tags {4891my$head= git_get_head_hash($project);4892 git_header_html();4893 git_print_page_nav('','',$head,undef,$head);4894 git_print_header_div('summary',$project);48954896my@tagslist= git_get_tags_list();4897if(@tagslist) {4898 git_tags_body(\@tagslist);4899}4900 git_footer_html();4901}49024903sub git_heads {4904my$head= git_get_head_hash($project);4905 git_header_html();4906 git_print_page_nav('','',$head,undef,$head);4907 git_print_header_div('summary',$project);49084909my@headslist= git_get_heads_list();4910if(@headslist) {4911 git_heads_body(\@headslist,$head);4912}4913 git_footer_html();4914}49154916sub git_blob_plain {4917my$type=shift;4918my$expires;49194920if(!defined$hash) {4921if(defined$file_name) {4922my$base=$hash_base|| git_get_head_hash($project);4923$hash= git_get_hash_by_path($base,$file_name,"blob")4924or die_error(404,"Cannot find file");4925}else{4926 die_error(400,"No file name defined");4927}4928}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4929# blobs defined by non-textual hash id's can be cached4930$expires="+1d";4931}49324933open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4934or die_error(500,"Open git-cat-file blob '$hash' failed");49354936# content-type (can include charset)4937$type= blob_contenttype($fd,$file_name,$type);49384939# "save as" filename, even when no $file_name is given4940my$save_as="$hash";4941if(defined$file_name) {4942$save_as=$file_name;4943}elsif($type=~m/^text\//) {4944$save_as.='.txt';4945}49464947# With XSS prevention on, blobs of all types except a few known safe4948# ones are served with "Content-Disposition: attachment" to make sure4949# they don't run in our security domain. For certain image types,4950# blob view writes an <img> tag referring to blob_plain view, and we4951# want to be sure not to break that by serving the image as an4952# attachment (though Firefox 3 doesn't seem to care).4953my$sandbox=$prevent_xss&&4954$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;49554956print$cgi->header(4957-type =>$type,4958-expires =>$expires,4959-content_disposition =>4960($sandbox?'attachment':'inline')4961.'; filename="'.$save_as.'"');4962local$/=undef;4963binmode STDOUT,':raw';4964print<$fd>;4965binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4966close$fd;4967}49684969sub git_blob {4970my$expires;49714972if(!defined$hash) {4973if(defined$file_name) {4974my$base=$hash_base|| git_get_head_hash($project);4975$hash= git_get_hash_by_path($base,$file_name,"blob")4976or die_error(404,"Cannot find file");4977}else{4978 die_error(400,"No file name defined");4979}4980}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4981# blobs defined by non-textual hash id's can be cached4982$expires="+1d";4983}49844985my$have_blame= gitweb_check_feature('blame');4986open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4987or die_error(500,"Couldn't cat$file_name,$hash");4988my$mimetype= blob_mimetype($fd,$file_name);4989if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4990close$fd;4991return git_blob_plain($mimetype);4992}4993# we can have blame only for text/* mimetype4994$have_blame&&= ($mimetype=~m!^text/!);49954996 git_header_html(undef,$expires);4997my$formats_nav='';4998if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4999if(defined$file_name) {5000if($have_blame) {5001$formats_nav.=5002$cgi->a({-href => href(action=>"blame", -replay=>1)},5003"blame") .5004" | ";5005}5006$formats_nav.=5007$cgi->a({-href => href(action=>"history", -replay=>1)},5008"history") .5009" | ".5010$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5011"raw") .5012" | ".5013$cgi->a({-href => href(action=>"blob",5014 hash_base=>"HEAD", file_name=>$file_name)},5015"HEAD");5016}else{5017$formats_nav.=5018$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5019"raw");5020}5021 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5022 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5023}else{5024print"<div class=\"page_nav\">\n".5025"<br/><br/></div>\n".5026"<div class=\"title\">".esc_html($hash)."</div>\n";5027}5028 git_print_page_path($file_name,"blob",$hash_base);5029print"<div class=\"page_body\">\n";5030if($mimetype=~m!^image/!) {5031print qq!<img type="!.esc_attr($mimetype).qq!"!;5032if($file_name) {5033print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;5034}5035print qq! src="! .5036 href(action=>"blob_plain", hash=>$hash,5037 hash_base=>$hash_base, file_name=>$file_name) .5038 qq!"/>\n!;5039}else{5040my$nr;5041while(my$line= <$fd>) {5042chomp$line;5043$nr++;5044$line= untabify($line);5045printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5046$nr,$nr,$nr, esc_html($line, -nbsp=>1);5047}5048}5049close$fd5050or print"Reading blob failed.\n";5051print"</div>";5052 git_footer_html();5053}50545055sub git_tree {5056if(!defined$hash_base) {5057$hash_base="HEAD";5058}5059if(!defined$hash) {5060if(defined$file_name) {5061$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5062}else{5063$hash=$hash_base;5064}5065}5066 die_error(404,"No such tree")unlessdefined($hash);50675068my@entries= ();5069{5070local$/="\0";5071open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash5072or die_error(500,"Open git-ls-tree failed");5073@entries=map{chomp;$_} <$fd>;5074close$fd5075or die_error(404,"Reading tree failed");5076}50775078my$refs= git_get_references();5079my$ref= format_ref_marker($refs,$hash_base);5080 git_header_html();5081my$basedir='';5082my$have_blame= gitweb_check_feature('blame');5083if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5084my@views_nav= ();5085if(defined$file_name) {5086push@views_nav,5087$cgi->a({-href => href(action=>"history", -replay=>1)},5088"history"),5089$cgi->a({-href => href(action=>"tree",5090 hash_base=>"HEAD", file_name=>$file_name)},5091"HEAD"),5092}5093my$snapshot_links= format_snapshot_links($hash);5094if(defined$snapshot_links) {5095# FIXME: Should be available when we have no hash base as well.5096push@views_nav,$snapshot_links;5097}5098 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));5099 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5100}else{5101undef$hash_base;5102print"<div class=\"page_nav\">\n";5103print"<br/><br/></div>\n";5104print"<div class=\"title\">".esc_html($hash)."</div>\n";5105}5106if(defined$file_name) {5107$basedir=$file_name;5108if($basedirne''&&substr($basedir, -1)ne'/') {5109$basedir.='/';5110}5111 git_print_page_path($file_name,'tree',$hash_base);5112}5113print"<div class=\"page_body\">\n";5114print"<table class=\"tree\">\n";5115my$alternate=1;5116# '..' (top directory) link if possible5117if(defined$hash_base&&5118defined$file_name&&$file_name=~m![^/]+$!) {5119if($alternate) {5120print"<tr class=\"dark\">\n";5121}else{5122print"<tr class=\"light\">\n";5123}5124$alternate^=1;51255126my$up=$file_name;5127$up=~s!/?[^/]+$!!;5128undef$upunless$up;5129# based on git_print_tree_entry5130print'<td class="mode">'. mode_str('040000') ."</td>\n";5131print'<td class="list">';5132print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,5133 file_name=>$up)},5134"..");5135print"</td>\n";5136print"<td class=\"link\"></td>\n";51375138print"</tr>\n";5139}5140foreachmy$line(@entries) {5141my%t= parse_ls_tree_line($line, -z =>1);51425143if($alternate) {5144print"<tr class=\"dark\">\n";5145}else{5146print"<tr class=\"light\">\n";5147}5148$alternate^=1;51495150 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);51515152print"</tr>\n";5153}5154print"</table>\n".5155"</div>";5156 git_footer_html();5157}51585159sub git_snapshot {5160my$format=$input_params{'snapshot_format'};5161if(!@snapshot_fmts) {5162 die_error(403,"Snapshots not allowed");5163}5164# default to first supported snapshot format5165$format||=$snapshot_fmts[0];5166if($format!~m/^[a-z0-9]+$/) {5167 die_error(400,"Invalid snapshot format parameter");5168}elsif(!exists($known_snapshot_formats{$format})) {5169 die_error(400,"Unknown snapshot format");5170}elsif(!grep($_eq$format,@snapshot_fmts)) {5171 die_error(403,"Unsupported snapshot format");5172}51735174if(!defined$hash) {5175$hash= git_get_head_hash($project);5176}51775178my$name=$project;5179$name=~ s,([^/])/*\.git$,$1,;5180$name= basename($name);5181my$filename= to_utf8($name);5182$name=~s/\047/\047\\\047\047/g;5183my$cmd;5184$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5185$cmd= quote_command(5186 git_cmd(),'archive',5187"--format=$known_snapshot_formats{$format}{'format'}",5188"--prefix=$name/",$hash);5189if(exists$known_snapshot_formats{$format}{'compressor'}) {5190$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5191}51925193print$cgi->header(5194-type =>$known_snapshot_formats{$format}{'type'},5195-content_disposition =>'inline; filename="'."$filename".'"',5196-status =>'200 OK');51975198open my$fd,"-|",$cmd5199or die_error(500,"Execute git-archive failed");5200binmode STDOUT,':raw';5201print<$fd>;5202binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5203close$fd;5204}52055206sub git_log {5207my$head= git_get_head_hash($project);5208if(!defined$hash) {5209$hash=$head;5210}5211if(!defined$page) {5212$page=0;5213}5214my$refs= git_get_references();52155216my@commitlist= parse_commits($hash,101, (100*$page));52175218my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);52195220my($patch_max) = gitweb_get_feature('patches');5221if($patch_max) {5222if($patch_max<0||@commitlist<=$patch_max) {5223$paging_nav.=" ⋅ ".5224$cgi->a({-href => href(action=>"patches", -replay=>1)},5225"patches");5226}5227}52285229 git_header_html();5230 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);52315232if(!@commitlist) {5233my%co= parse_commit($hash);52345235 git_print_header_div('summary',$project);5236print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5237}5238my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5239for(my$i=0;$i<=$to;$i++) {5240my%co= %{$commitlist[$i]};5241next if!%co;5242my$commit=$co{'id'};5243my$ref= format_ref_marker($refs,$commit);5244my%ad= parse_date($co{'author_epoch'});5245 git_print_header_div('commit',5246"<span class=\"age\">$co{'age_string'}</span>".5247 esc_html($co{'title'}) .$ref,5248$commit);5249print"<div class=\"title_text\">\n".5250"<div class=\"log_link\">\n".5251$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5252" | ".5253$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5254" | ".5255$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5256"<br/>\n".5257"</div>\n";5258 git_print_authorship(\%co, -tag =>'span');5259print"<br/>\n</div>\n";52605261print"<div class=\"log_body\">\n";5262 git_print_log($co{'comment'}, -final_empty_line=>1);5263print"</div>\n";5264}5265if($#commitlist>=100) {5266print"<div class=\"page_nav\">\n";5267print$cgi->a({-href => href(-replay=>1, page=>$page+1),5268-accesskey =>"n", -title =>"Alt-n"},"next");5269print"</div>\n";5270}5271 git_footer_html();5272}52735274sub git_commit {5275$hash||=$hash_base||"HEAD";5276my%co= parse_commit($hash)5277or die_error(404,"Unknown commit object");52785279my$parent=$co{'parent'};5280my$parents=$co{'parents'};# listref52815282# we need to prepare $formats_nav before any parameter munging5283my$formats_nav;5284if(!defined$parent) {5285# --root commitdiff5286$formats_nav.='(initial)';5287}elsif(@$parents==1) {5288# single parent commit5289$formats_nav.=5290'(parent: '.5291$cgi->a({-href => href(action=>"commit",5292 hash=>$parent)},5293 esc_html(substr($parent,0,7))) .5294')';5295}else{5296# merge commit5297$formats_nav.=5298'(merge: '.5299join(' ',map{5300$cgi->a({-href => href(action=>"commit",5301 hash=>$_)},5302 esc_html(substr($_,0,7)));5303}@$parents) .5304')';5305}5306if(gitweb_check_feature('patches')) {5307$formats_nav.=" | ".5308$cgi->a({-href => href(action=>"patch", -replay=>1)},5309"patch");5310}53115312if(!defined$parent) {5313$parent="--root";5314}5315my@difftree;5316open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5317@diff_opts,5318(@$parents<=1?$parent:'-c'),5319$hash,"--"5320or die_error(500,"Open git-diff-tree failed");5321@difftree=map{chomp;$_} <$fd>;5322close$fdor die_error(404,"Reading git-diff-tree failed");53235324# non-textual hash id's can be cached5325my$expires;5326if($hash=~m/^[0-9a-fA-F]{40}$/) {5327$expires="+1d";5328}5329my$refs= git_get_references();5330my$ref= format_ref_marker($refs,$co{'id'});53315332 git_header_html(undef,$expires);5333 git_print_page_nav('commit','',5334$hash,$co{'tree'},$hash,5335$formats_nav);53365337if(defined$co{'parent'}) {5338 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5339}else{5340 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5341}5342print"<div class=\"title_text\">\n".5343"<table class=\"object_header\">\n";5344 git_print_authorship_rows(\%co);5345print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5346print"<tr>".5347"<td>tree</td>".5348"<td class=\"sha1\">".5349$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5350class=>"list"},$co{'tree'}) .5351"</td>".5352"<td class=\"link\">".5353$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5354"tree");5355my$snapshot_links= format_snapshot_links($hash);5356if(defined$snapshot_links) {5357print" | ".$snapshot_links;5358}5359print"</td>".5360"</tr>\n";53615362foreachmy$par(@$parents) {5363print"<tr>".5364"<td>parent</td>".5365"<td class=\"sha1\">".5366$cgi->a({-href => href(action=>"commit", hash=>$par),5367class=>"list"},$par) .5368"</td>".5369"<td class=\"link\">".5370$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5371" | ".5372$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5373"</td>".5374"</tr>\n";5375}5376print"</table>".5377"</div>\n";53785379print"<div class=\"page_body\">\n";5380 git_print_log($co{'comment'});5381print"</div>\n";53825383 git_difftree_body(\@difftree,$hash,@$parents);53845385 git_footer_html();5386}53875388sub git_object {5389# object is defined by:5390# - hash or hash_base alone5391# - hash_base and file_name5392my$type;53935394# - hash or hash_base alone5395if($hash|| ($hash_base&& !defined$file_name)) {5396my$object_id=$hash||$hash_base;53975398open my$fd,"-|", quote_command(5399 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5400or die_error(404,"Object does not exist");5401$type= <$fd>;5402chomp$type;5403close$fd5404or die_error(404,"Object does not exist");54055406# - hash_base and file_name5407}elsif($hash_base&&defined$file_name) {5408$file_name=~ s,/+$,,;54095410system(git_cmd(),"cat-file",'-e',$hash_base) ==05411or die_error(404,"Base object does not exist");54125413# here errors should not hapen5414open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5415or die_error(500,"Open git-ls-tree failed");5416my$line= <$fd>;5417close$fd;54185419#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5420unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5421 die_error(404,"File or directory for given base does not exist");5422}5423$type=$2;5424$hash=$3;5425}else{5426 die_error(400,"Not enough information to find object");5427}54285429print$cgi->redirect(-uri => href(action=>$type, -full=>1,5430 hash=>$hash, hash_base=>$hash_base,5431 file_name=>$file_name),5432-status =>'302 Found');5433}54345435sub git_blobdiff {5436my$format=shift||'html';54375438my$fd;5439my@difftree;5440my%diffinfo;5441my$expires;54425443# preparing $fd and %diffinfo for git_patchset_body5444# new style URI5445if(defined$hash_base&&defined$hash_parent_base) {5446if(defined$file_name) {5447# read raw output5448open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5449$hash_parent_base,$hash_base,5450"--", (defined$file_parent?$file_parent: ()),$file_name5451or die_error(500,"Open git-diff-tree failed");5452@difftree=map{chomp;$_} <$fd>;5453close$fd5454or die_error(404,"Reading git-diff-tree failed");5455@difftree5456or die_error(404,"Blob diff not found");54575458}elsif(defined$hash&&5459$hash=~/[0-9a-fA-F]{40}/) {5460# try to find filename from $hash54615462# read filtered raw output5463open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5464$hash_parent_base,$hash_base,"--"5465or die_error(500,"Open git-diff-tree failed");5466@difftree=5467# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5468# $hash == to_id5469grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5470map{chomp;$_} <$fd>;5471close$fd5472or die_error(404,"Reading git-diff-tree failed");5473@difftree5474or die_error(404,"Blob diff not found");54755476}else{5477 die_error(400,"Missing one of the blob diff parameters");5478}54795480if(@difftree>1) {5481 die_error(400,"Ambiguous blob diff specification");5482}54835484%diffinfo= parse_difftree_raw_line($difftree[0]);5485$file_parent||=$diffinfo{'from_file'} ||$file_name;5486$file_name||=$diffinfo{'to_file'};54875488$hash_parent||=$diffinfo{'from_id'};5489$hash||=$diffinfo{'to_id'};54905491# non-textual hash id's can be cached5492if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5493$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5494$expires='+1d';5495}54965497# open patch output5498open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5499'-p', ($formateq'html'?"--full-index": ()),5500$hash_parent_base,$hash_base,5501"--", (defined$file_parent?$file_parent: ()),$file_name5502or die_error(500,"Open git-diff-tree failed");5503}55045505# old/legacy style URI -- not generated anymore since 1.4.3.5506if(!%diffinfo) {5507 die_error('404 Not Found',"Missing one of the blob diff parameters")5508}55095510# header5511if($formateq'html') {5512my$formats_nav=5513$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5514"raw");5515 git_header_html(undef,$expires);5516if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5517 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5518 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5519}else{5520print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5521print"<div class=\"title\">".esc_html("$hashvs$hash_parent")."</div>\n";5522}5523if(defined$file_name) {5524 git_print_page_path($file_name,"blob",$hash_base);5525}else{5526print"<div class=\"page_path\"></div>\n";5527}55285529}elsif($formateq'plain') {5530print$cgi->header(5531-type =>'text/plain',5532-charset =>'utf-8',5533-expires =>$expires,5534-content_disposition =>'inline; filename="'."$file_name".'.patch"');55355536print"X-Git-Url: ".$cgi->self_url() ."\n\n";55375538}else{5539 die_error(400,"Unknown blobdiff format");5540}55415542# patch5543if($formateq'html') {5544print"<div class=\"page_body\">\n";55455546 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5547close$fd;55485549print"</div>\n";# class="page_body"5550 git_footer_html();55515552}else{5553while(my$line= <$fd>) {5554$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5555$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;55565557print$line;55585559last if$line=~m!^\+\+\+!;5560}5561local$/=undef;5562print<$fd>;5563close$fd;5564}5565}55665567sub git_blobdiff_plain {5568 git_blobdiff('plain');5569}55705571sub git_commitdiff {5572my%params=@_;5573my$format=$params{-format} ||'html';55745575my($patch_max) = gitweb_get_feature('patches');5576if($formateq'patch') {5577 die_error(403,"Patch view not allowed")unless$patch_max;5578}55795580$hash||=$hash_base||"HEAD";5581my%co= parse_commit($hash)5582or die_error(404,"Unknown commit object");55835584# choose format for commitdiff for merge5585if(!defined$hash_parent&& @{$co{'parents'}} >1) {5586$hash_parent='--cc';5587}5588# we need to prepare $formats_nav before almost any parameter munging5589my$formats_nav;5590if($formateq'html') {5591$formats_nav=5592$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5593"raw");5594if($patch_max) {5595$formats_nav.=" | ".5596$cgi->a({-href => href(action=>"patch", -replay=>1)},5597"patch");5598}55995600if(defined$hash_parent&&5601$hash_parentne'-c'&&$hash_parentne'--cc') {5602# commitdiff with two commits given5603my$hash_parent_short=$hash_parent;5604if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5605$hash_parent_short=substr($hash_parent,0,7);5606}5607$formats_nav.=5608' (from';5609for(my$i=0;$i< @{$co{'parents'}};$i++) {5610if($co{'parents'}[$i]eq$hash_parent) {5611$formats_nav.=' parent '. ($i+1);5612last;5613}5614}5615$formats_nav.=': '.5616$cgi->a({-href => href(action=>"commitdiff",5617 hash=>$hash_parent)},5618 esc_html($hash_parent_short)) .5619')';5620}elsif(!$co{'parent'}) {5621# --root commitdiff5622$formats_nav.=' (initial)';5623}elsif(scalar@{$co{'parents'}} ==1) {5624# single parent commit5625$formats_nav.=5626' (parent: '.5627$cgi->a({-href => href(action=>"commitdiff",5628 hash=>$co{'parent'})},5629 esc_html(substr($co{'parent'},0,7))) .5630')';5631}else{5632# merge commit5633if($hash_parenteq'--cc') {5634$formats_nav.=' | '.5635$cgi->a({-href => href(action=>"commitdiff",5636 hash=>$hash, hash_parent=>'-c')},5637'combined');5638}else{# $hash_parent eq '-c'5639$formats_nav.=' | '.5640$cgi->a({-href => href(action=>"commitdiff",5641 hash=>$hash, hash_parent=>'--cc')},5642'compact');5643}5644$formats_nav.=5645' (merge: '.5646join(' ',map{5647$cgi->a({-href => href(action=>"commitdiff",5648 hash=>$_)},5649 esc_html(substr($_,0,7)));5650} @{$co{'parents'}} ) .5651')';5652}5653}56545655my$hash_parent_param=$hash_parent;5656if(!defined$hash_parent_param) {5657# --cc for multiple parents, --root for parentless5658$hash_parent_param=5659@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5660}56615662# read commitdiff5663my$fd;5664my@difftree;5665if($formateq'html') {5666open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5667"--no-commit-id","--patch-with-raw","--full-index",5668$hash_parent_param,$hash,"--"5669or die_error(500,"Open git-diff-tree failed");56705671while(my$line= <$fd>) {5672chomp$line;5673# empty line ends raw part of diff-tree output5674last unless$line;5675push@difftree,scalar parse_difftree_raw_line($line);5676}56775678}elsif($formateq'plain') {5679open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5680'-p',$hash_parent_param,$hash,"--"5681or die_error(500,"Open git-diff-tree failed");5682}elsif($formateq'patch') {5683# For commit ranges, we limit the output to the number of5684# patches specified in the 'patches' feature.5685# For single commits, we limit the output to a single patch,5686# diverging from the git-format-patch default.5687my@commit_spec= ();5688if($hash_parent) {5689if($patch_max>0) {5690push@commit_spec,"-$patch_max";5691}5692push@commit_spec,'-n',"$hash_parent..$hash";5693}else{5694if($params{-single}) {5695push@commit_spec,'-1';5696}else{5697if($patch_max>0) {5698push@commit_spec,"-$patch_max";5699}5700push@commit_spec,"-n";5701}5702push@commit_spec,'--root',$hash;5703}5704open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5705'--stdout',@commit_spec5706or die_error(500,"Open git-format-patch failed");5707}else{5708 die_error(400,"Unknown commitdiff format");5709}57105711# non-textual hash id's can be cached5712my$expires;5713if($hash=~m/^[0-9a-fA-F]{40}$/) {5714$expires="+1d";5715}57165717# write commit message5718if($formateq'html') {5719my$refs= git_get_references();5720my$ref= format_ref_marker($refs,$co{'id'});57215722 git_header_html(undef,$expires);5723 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5724 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5725print"<div class=\"title_text\">\n".5726"<table class=\"object_header\">\n";5727 git_print_authorship_rows(\%co);5728print"</table>".5729"</div>\n";5730print"<div class=\"page_body\">\n";5731if(@{$co{'comment'}} >1) {5732print"<div class=\"log\">\n";5733 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5734print"</div>\n";# class="log"5735}57365737}elsif($formateq'plain') {5738my$refs= git_get_references("tags");5739my$tagname= git_get_rev_name_tags($hash);5740my$filename= basename($project) ."-$hash.patch";57415742print$cgi->header(5743-type =>'text/plain',5744-charset =>'utf-8',5745-expires =>$expires,5746-content_disposition =>'inline; filename="'."$filename".'"');5747my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5748print"From: ". to_utf8($co{'author'}) ."\n";5749print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5750print"Subject: ". to_utf8($co{'title'}) ."\n";57515752print"X-Git-Tag:$tagname\n"if$tagname;5753print"X-Git-Url: ".$cgi->self_url() ."\n\n";57545755foreachmy$line(@{$co{'comment'}}) {5756print to_utf8($line) ."\n";5757}5758print"---\n\n";5759}elsif($formateq'patch') {5760my$filename= basename($project) ."-$hash.patch";57615762print$cgi->header(5763-type =>'text/plain',5764-charset =>'utf-8',5765-expires =>$expires,5766-content_disposition =>'inline; filename="'."$filename".'"');5767}57685769# write patch5770if($formateq'html') {5771my$use_parents= !defined$hash_parent||5772$hash_parenteq'-c'||$hash_parenteq'--cc';5773 git_difftree_body(\@difftree,$hash,5774$use_parents? @{$co{'parents'}} :$hash_parent);5775print"<br/>\n";57765777 git_patchset_body($fd, \@difftree,$hash,5778$use_parents? @{$co{'parents'}} :$hash_parent);5779close$fd;5780print"</div>\n";# class="page_body"5781 git_footer_html();57825783}elsif($formateq'plain') {5784local$/=undef;5785print<$fd>;5786close$fd5787or print"Reading git-diff-tree failed\n";5788}elsif($formateq'patch') {5789local$/=undef;5790print<$fd>;5791close$fd5792or print"Reading git-format-patch failed\n";5793}5794}57955796sub git_commitdiff_plain {5797 git_commitdiff(-format =>'plain');5798}57995800# format-patch-style patches5801sub git_patch {5802 git_commitdiff(-format =>'patch', -single=>1);5803}58045805sub git_patches {5806 git_commitdiff(-format =>'patch');5807}58085809sub git_history {5810if(!defined$hash_base) {5811$hash_base= git_get_head_hash($project);5812}5813if(!defined$page) {5814$page=0;5815}5816my$ftype;5817my%co= parse_commit($hash_base)5818or die_error(404,"Unknown commit object");58195820my$refs= git_get_references();5821my$limit=sprintf("--max-count=%i", (100* ($page+1)));58225823my@commitlist= parse_commits($hash_base,101, (100*$page),5824$file_name,"--full-history")5825or die_error(404,"No such file or directory on given branch");58265827if(!defined$hash&&defined$file_name) {5828# some commits could have deleted file in question,5829# and not have it in tree, but one of them has to have it5830for(my$i=0;$i<=@commitlist;$i++) {5831$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5832last ifdefined$hash;5833}5834}5835if(defined$hash) {5836$ftype= git_get_type($hash);5837}5838if(!defined$ftype) {5839 die_error(500,"Unknown type of object");5840}58415842my$paging_nav='';5843if($page>0) {5844$paging_nav.=5845$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5846 file_name=>$file_name)},5847"first");5848$paging_nav.=" ⋅ ".5849$cgi->a({-href => href(-replay=>1, page=>$page-1),5850-accesskey =>"p", -title =>"Alt-p"},"prev");5851}else{5852$paging_nav.="first";5853$paging_nav.=" ⋅ prev";5854}5855my$next_link='';5856if($#commitlist>=100) {5857$next_link=5858$cgi->a({-href => href(-replay=>1, page=>$page+1),5859-accesskey =>"n", -title =>"Alt-n"},"next");5860$paging_nav.=" ⋅$next_link";5861}else{5862$paging_nav.=" ⋅ next";5863}58645865 git_header_html();5866 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5867 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5868 git_print_page_path($file_name,$ftype,$hash_base);58695870 git_history_body(\@commitlist,0,99,5871$refs,$hash_base,$ftype,$next_link);58725873 git_footer_html();5874}58755876sub git_search {5877 gitweb_check_feature('search')or die_error(403,"Search is disabled");5878if(!defined$searchtext) {5879 die_error(400,"Text field is empty");5880}5881if(!defined$hash) {5882$hash= git_get_head_hash($project);5883}5884my%co= parse_commit($hash);5885if(!%co) {5886 die_error(404,"Unknown commit object");5887}5888if(!defined$page) {5889$page=0;5890}58915892$searchtype||='commit';5893if($searchtypeeq'pickaxe') {5894# pickaxe may take all resources of your box and run for several minutes5895# with every query - so decide by yourself how public you make this feature5896 gitweb_check_feature('pickaxe')5897or die_error(403,"Pickaxe is disabled");5898}5899if($searchtypeeq'grep') {5900 gitweb_check_feature('grep')5901or die_error(403,"Grep is disabled");5902}59035904 git_header_html();59055906if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5907my$greptype;5908if($searchtypeeq'commit') {5909$greptype="--grep=";5910}elsif($searchtypeeq'author') {5911$greptype="--author=";5912}elsif($searchtypeeq'committer') {5913$greptype="--committer=";5914}5915$greptype.=$searchtext;5916my@commitlist= parse_commits($hash,101, (100*$page),undef,5917$greptype,'--regexp-ignore-case',5918$search_use_regexp?'--extended-regexp':'--fixed-strings');59195920my$paging_nav='';5921if($page>0) {5922$paging_nav.=5923$cgi->a({-href => href(action=>"search", hash=>$hash,5924 searchtext=>$searchtext,5925 searchtype=>$searchtype)},5926"first");5927$paging_nav.=" ⋅ ".5928$cgi->a({-href => href(-replay=>1, page=>$page-1),5929-accesskey =>"p", -title =>"Alt-p"},"prev");5930}else{5931$paging_nav.="first";5932$paging_nav.=" ⋅ prev";5933}5934my$next_link='';5935if($#commitlist>=100) {5936$next_link=5937$cgi->a({-href => href(-replay=>1, page=>$page+1),5938-accesskey =>"n", -title =>"Alt-n"},"next");5939$paging_nav.=" ⋅$next_link";5940}else{5941$paging_nav.=" ⋅ next";5942}59435944if($#commitlist>=100) {5945}59465947 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5948 git_print_header_div('commit', esc_html($co{'title'}),$hash);5949 git_search_grep_body(\@commitlist,0,99,$next_link);5950}59515952if($searchtypeeq'pickaxe') {5953 git_print_page_nav('','',$hash,$co{'tree'},$hash);5954 git_print_header_div('commit', esc_html($co{'title'}),$hash);59555956print"<table class=\"pickaxe search\">\n";5957my$alternate=1;5958local$/="\n";5959open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5960'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5961($search_use_regexp?'--pickaxe-regex': ());5962undef%co;5963my@files;5964while(my$line= <$fd>) {5965chomp$line;5966next unless$line;59675968my%set= parse_difftree_raw_line($line);5969if(defined$set{'commit'}) {5970# finish previous commit5971if(%co) {5972print"</td>\n".5973"<td class=\"link\">".5974$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5975" | ".5976$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5977print"</td>\n".5978"</tr>\n";5979}59805981if($alternate) {5982print"<tr class=\"dark\">\n";5983}else{5984print"<tr class=\"light\">\n";5985}5986$alternate^=1;5987%co= parse_commit($set{'commit'});5988my$author= chop_and_escape_str($co{'author_name'},15,5);5989print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5990"<td><i>$author</i></td>\n".5991"<td>".5992$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5993-class=>"list subject"},5994 chop_and_escape_str($co{'title'},50) ."<br/>");5995}elsif(defined$set{'to_id'}) {5996next if($set{'to_id'} =~m/^0{40}$/);59975998print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5999 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6000-class=>"list"},6001"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6002"<br/>\n";6003}6004}6005close$fd;60066007# finish last commit (warning: repetition!)6008if(%co) {6009print"</td>\n".6010"<td class=\"link\">".6011$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6012" | ".6013$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6014print"</td>\n".6015"</tr>\n";6016}60176018print"</table>\n";6019}60206021if($searchtypeeq'grep') {6022 git_print_page_nav('','',$hash,$co{'tree'},$hash);6023 git_print_header_div('commit', esc_html($co{'title'}),$hash);60246025print"<table class=\"grep_search\">\n";6026my$alternate=1;6027my$matches=0;6028local$/="\n";6029open my$fd,"-|", git_cmd(),'grep','-n',6030$search_use_regexp? ('-E','-i') :'-F',6031$searchtext,$co{'tree'};6032my$lastfile='';6033while(my$line= <$fd>) {6034chomp$line;6035my($file,$lno,$ltext,$binary);6036last if($matches++>1000);6037if($line=~/^Binary file (.+) matches$/) {6038$file=$1;6039$binary=1;6040}else{6041(undef,$file,$lno,$ltext) =split(/:/,$line,4);6042}6043if($filene$lastfile) {6044$lastfileand print"</td></tr>\n";6045if($alternate++) {6046print"<tr class=\"dark\">\n";6047}else{6048print"<tr class=\"light\">\n";6049}6050print"<td class=\"list\">".6051$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6052 file_name=>"$file"),6053-class=>"list"}, esc_path($file));6054print"</td><td>\n";6055$lastfile=$file;6056}6057if($binary) {6058print"<div class=\"binary\">Binary file</div>\n";6059}else{6060$ltext= untabify($ltext);6061if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6062$ltext= esc_html($1, -nbsp=>1);6063$ltext.='<span class="match">';6064$ltext.= esc_html($2, -nbsp=>1);6065$ltext.='</span>';6066$ltext.= esc_html($3, -nbsp=>1);6067}else{6068$ltext= esc_html($ltext, -nbsp=>1);6069}6070print"<div class=\"pre\">".6071$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6072 file_name=>"$file").'#l'.$lno,6073-class=>"linenr"},sprintf('%4i',$lno))6074.' '.$ltext."</div>\n";6075}6076}6077if($lastfile) {6078print"</td></tr>\n";6079if($matches>1000) {6080print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6081}6082}else{6083print"<div class=\"diff nodifferences\">No matches found</div>\n";6084}6085close$fd;60866087print"</table>\n";6088}6089 git_footer_html();6090}60916092sub git_search_help {6093 git_header_html();6094 git_print_page_nav('','',$hash,$hash,$hash);6095print<<EOT;6096<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6097regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6098the pattern entered is recognized as the POSIX extended6099<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6100insensitive).</p>6101<dl>6102<dt><b>commit</b></dt>6103<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6104EOT6105my$have_grep= gitweb_check_feature('grep');6106if($have_grep) {6107print<<EOT;6108<dt><b>grep</b></dt>6109<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6110 a different one) are searched for the given pattern. On large trees, this search can take6111a while and put some strain on the server, so please use it with some consideration. Note that6112due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6113case-sensitive.</dd>6114EOT6115}6116print<<EOT;6117<dt><b>author</b></dt>6118<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6119<dt><b>committer</b></dt>6120<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6121EOT6122my$have_pickaxe= gitweb_check_feature('pickaxe');6123if($have_pickaxe) {6124print<<EOT;6125<dt><b>pickaxe</b></dt>6126<dd>All commits that caused the string to appear or disappear from any file (changes that6127added, removed or "modified" the string) will be listed. This search can take a while and6128takes a lot of strain on the server, so please use it wisely. Note that since you may be6129interested even in changes just changing the case as well, this search is case sensitive.</dd>6130EOT6131}6132print"</dl>\n";6133 git_footer_html();6134}61356136sub git_shortlog {6137my$head= git_get_head_hash($project);6138if(!defined$hash) {6139$hash=$head;6140}6141if(!defined$page) {6142$page=0;6143}6144my$refs= git_get_references();61456146my$commit_hash=$hash;6147if(defined$hash_parent) {6148$commit_hash="$hash_parent..$hash";6149}6150my@commitlist= parse_commits($commit_hash,101, (100*$page));61516152my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);6153my$next_link='';6154if($#commitlist>=100) {6155$next_link=6156$cgi->a({-href => href(-replay=>1, page=>$page+1),6157-accesskey =>"n", -title =>"Alt-n"},"next");6158}6159my$patch_max= gitweb_check_feature('patches');6160if($patch_max) {6161if($patch_max<0||@commitlist<=$patch_max) {6162$paging_nav.=" ⋅ ".6163$cgi->a({-href => href(action=>"patches", -replay=>1)},6164"patches");6165}6166}61676168 git_header_html();6169 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6170 git_print_header_div('summary',$project);61716172 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);61736174 git_footer_html();6175}61766177## ......................................................................6178## feeds (RSS, Atom; OPML)61796180sub git_feed {6181my$format=shift||'atom';6182my$have_blame= gitweb_check_feature('blame');61836184# Atom: http://www.atomenabled.org/developers/syndication/6185# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6186if($formatne'rss'&&$formatne'atom') {6187 die_error(400,"Unknown web feed format");6188}61896190# log/feed of current (HEAD) branch, log of given branch, history of file/directory6191my$head=$hash||'HEAD';6192my@commitlist= parse_commits($head,150,0,$file_name);61936194my%latest_commit;6195my%latest_date;6196my$content_type="application/$format+xml";6197if(defined$cgi->http('HTTP_ACCEPT') &&6198$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6199# browser (feed reader) prefers text/xml6200$content_type='text/xml';6201}6202if(defined($commitlist[0])) {6203%latest_commit= %{$commitlist[0]};6204my$latest_epoch=$latest_commit{'committer_epoch'};6205%latest_date= parse_date($latest_epoch);6206my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6207if(defined$if_modified) {6208my$since;6209if(eval{require HTTP::Date;1; }) {6210$since= HTTP::Date::str2time($if_modified);6211}elsif(eval{require Time::ParseDate;1; }) {6212$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6213}6214if(defined$since&&$latest_epoch<=$since) {6215print$cgi->header(6216-type =>$content_type,6217-charset =>'utf-8',6218-last_modified =>$latest_date{'rfc2822'},6219-status =>'304 Not Modified');6220return;6221}6222}6223print$cgi->header(6224-type =>$content_type,6225-charset =>'utf-8',6226-last_modified =>$latest_date{'rfc2822'});6227}else{6228print$cgi->header(6229-type =>$content_type,6230-charset =>'utf-8');6231}62326233# Optimization: skip generating the body if client asks only6234# for Last-Modified date.6235return if($cgi->request_method()eq'HEAD');62366237# header variables6238my$title="$site_name-$project/$action";6239my$feed_type='log';6240if(defined$hash) {6241$title.=" - '$hash'";6242$feed_type='branch log';6243if(defined$file_name) {6244$title.=" ::$file_name";6245$feed_type='history';6246}6247}elsif(defined$file_name) {6248$title.=" -$file_name";6249$feed_type='history';6250}6251$title.="$feed_type";6252my$descr= git_get_project_description($project);6253if(defined$descr) {6254$descr= esc_html($descr);6255}else{6256$descr="$project".6257($formateq'rss'?'RSS':'Atom') .6258" feed";6259}6260my$owner= git_get_project_owner($project);6261$owner= esc_html($owner);62626263#header6264my$alt_url;6265if(defined$file_name) {6266$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6267}elsif(defined$hash) {6268$alt_url= href(-full=>1, action=>"log", hash=>$hash);6269}else{6270$alt_url= href(-full=>1, action=>"summary");6271}6272print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6273if($formateq'rss') {6274print<<XML;6275<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6276<channel>6277XML6278print"<title>$title</title>\n".6279"<link>$alt_url</link>\n".6280"<description>$descr</description>\n".6281"<language>en</language>\n".6282# project owner is responsible for 'editorial' content6283"<managingEditor>$owner</managingEditor>\n";6284if(defined$logo||defined$favicon) {6285# prefer the logo to the favicon, since RSS6286# doesn't allow both6287my$img= esc_url($logo||$favicon);6288print"<image>\n".6289"<url>$img</url>\n".6290"<title>$title</title>\n".6291"<link>$alt_url</link>\n".6292"</image>\n";6293}6294if(%latest_date) {6295print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6296print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6297}6298print"<generator>gitweb v.$version/$git_version</generator>\n";6299}elsif($formateq'atom') {6300print<<XML;6301<feed xmlns="http://www.w3.org/2005/Atom">6302XML6303print"<title>$title</title>\n".6304"<subtitle>$descr</subtitle>\n".6305'<link rel="alternate" type="text/html" href="'.6306$alt_url.'" />'."\n".6307'<link rel="self" type="'.$content_type.'" href="'.6308$cgi->self_url() .'" />'."\n".6309"<id>". href(-full=>1) ."</id>\n".6310# use project owner for feed author6311"<author><name>$owner</name></author>\n";6312if(defined$favicon) {6313print"<icon>". esc_url($favicon) ."</icon>\n";6314}6315if(defined$logo_url) {6316# not twice as wide as tall: 72 x 27 pixels6317print"<logo>". esc_url($logo) ."</logo>\n";6318}6319if(!%latest_date) {6320# dummy date to keep the feed valid until commits trickle in:6321print"<updated>1970-01-01T00:00:00Z</updated>\n";6322}else{6323print"<updated>$latest_date{'iso-8601'}</updated>\n";6324}6325print"<generator version='$version/$git_version'>gitweb</generator>\n";6326}63276328# contents6329for(my$i=0;$i<=$#commitlist;$i++) {6330my%co= %{$commitlist[$i]};6331my$commit=$co{'id'};6332# we read 150, we always show 30 and the ones more recent than 48 hours6333if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6334last;6335}6336my%cd= parse_date($co{'author_epoch'});63376338# get list of changed files6339open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6340$co{'parent'} ||"--root",6341$co{'id'},"--", (defined$file_name?$file_name: ())6342ornext;6343my@difftree=map{chomp;$_} <$fd>;6344close$fd6345ornext;63466347# print element (entry, item)6348my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6349if($formateq'rss') {6350print"<item>\n".6351"<title>". esc_html($co{'title'}) ."</title>\n".6352"<author>". esc_html($co{'author'}) ."</author>\n".6353"<pubDate>$cd{'rfc2822'}</pubDate>\n".6354"<guid isPermaLink=\"true\">$co_url</guid>\n".6355"<link>$co_url</link>\n".6356"<description>". esc_html($co{'title'}) ."</description>\n".6357"<content:encoded>".6358"<![CDATA[\n";6359}elsif($formateq'atom') {6360print"<entry>\n".6361"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6362"<updated>$cd{'iso-8601'}</updated>\n".6363"<author>\n".6364" <name>". esc_html($co{'author_name'}) ."</name>\n";6365if($co{'author_email'}) {6366print" <email>". esc_html($co{'author_email'}) ."</email>\n";6367}6368print"</author>\n".6369# use committer for contributor6370"<contributor>\n".6371" <name>". esc_html($co{'committer_name'}) ."</name>\n";6372if($co{'committer_email'}) {6373print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6374}6375print"</contributor>\n".6376"<published>$cd{'iso-8601'}</published>\n".6377"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6378"<id>$co_url</id>\n".6379"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6380"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6381}6382my$comment=$co{'comment'};6383print"<pre>\n";6384foreachmy$line(@$comment) {6385$line= esc_html($line);6386print"$line\n";6387}6388print"</pre><ul>\n";6389foreachmy$difftree_line(@difftree) {6390my%difftree= parse_difftree_raw_line($difftree_line);6391next if!$difftree{'from_id'};63926393my$file=$difftree{'file'} ||$difftree{'to_file'};63946395print"<li>".6396"[".6397$cgi->a({-href => href(-full=>1, action=>"blobdiff",6398 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6399 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6400 file_name=>$file, file_parent=>$difftree{'from_file'}),6401-title =>"diff"},'D');6402if($have_blame) {6403print$cgi->a({-href => href(-full=>1, action=>"blame",6404 file_name=>$file, hash_base=>$commit),6405-title =>"blame"},'B');6406}6407# if this is not a feed of a file history6408if(!defined$file_name||$file_namene$file) {6409print$cgi->a({-href => href(-full=>1, action=>"history",6410 file_name=>$file, hash=>$commit),6411-title =>"history"},'H');6412}6413$file= esc_path($file);6414print"] ".6415"$file</li>\n";6416}6417if($formateq'rss') {6418print"</ul>]]>\n".6419"</content:encoded>\n".6420"</item>\n";6421}elsif($formateq'atom') {6422print"</ul>\n</div>\n".6423"</content>\n".6424"</entry>\n";6425}6426}64276428# end of feed6429if($formateq'rss') {6430print"</channel>\n</rss>\n";6431}elsif($formateq'atom') {6432print"</feed>\n";6433}6434}64356436sub git_rss {6437 git_feed('rss');6438}64396440sub git_atom {6441 git_feed('atom');6442}64436444sub git_opml {6445my@list= git_get_projects_list();64466447print$cgi->header(6448-type =>'text/xml',6449-charset =>'utf-8',6450-content_disposition =>'inline; filename="opml.xml"');64516452print<<XML;6453<?xml version="1.0" encoding="utf-8"?>6454<opml version="1.0">6455<head>6456 <title>$site_nameOPML Export</title>6457</head>6458<body>6459<outline text="git RSS feeds">6460XML64616462foreachmy$pr(@list) {6463my%proj=%$pr;6464my$head= git_get_head_hash($proj{'path'});6465if(!defined$head) {6466next;6467}6468$git_dir="$projectroot/$proj{'path'}";6469my%co= parse_commit($head);6470if(!%co) {6471next;6472}64736474my$path= esc_html(chop_str($proj{'path'},25,5));6475my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6476my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6477print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6478}6479print<<XML;6480</outline>6481</body>6482</opml>6483XML6484}