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 overrideable"; 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# replace invalid utf8 character with SUBSTITUTION sequence1088sub esc_html {1089my$str=shift;1090my%opts=@_;10911092$str= to_utf8($str);1093$str=$cgi->escapeHTML($str);1094if($opts{'-nbsp'}) {1095$str=~s/ / /g;1096}1097$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1098return$str;1099}11001101# quote control characters and escape filename to HTML1102sub esc_path {1103my$str=shift;1104my%opts=@_;11051106$str= to_utf8($str);1107$str=$cgi->escapeHTML($str);1108if($opts{'-nbsp'}) {1109$str=~s/ / /g;1110}1111$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1112return$str;1113}11141115# Make control characters "printable", using character escape codes (CEC)1116sub quot_cec {1117my$cntrl=shift;1118my%opts=@_;1119my%es= (# character escape codes, aka escape sequences1120"\t"=>'\t',# tab (HT)1121"\n"=>'\n',# line feed (LF)1122"\r"=>'\r',# carrige return (CR)1123"\f"=>'\f',# form feed (FF)1124"\b"=>'\b',# backspace (BS)1125"\a"=>'\a',# alarm (bell) (BEL)1126"\e"=>'\e',# escape (ESC)1127"\013"=>'\v',# vertical tab (VT)1128"\000"=>'\0',# nul character (NUL)1129);1130my$chr= ( (exists$es{$cntrl})1131?$es{$cntrl}1132:sprintf('\%2x',ord($cntrl)) );1133if($opts{-nohtml}) {1134return$chr;1135}else{1136return"<span class=\"cntrl\">$chr</span>";1137}1138}11391140# Alternatively use unicode control pictures codepoints,1141# Unicode "printable representation" (PR)1142sub quot_upr {1143my$cntrl=shift;1144my%opts=@_;11451146my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1147if($opts{-nohtml}) {1148return$chr;1149}else{1150return"<span class=\"cntrl\">$chr</span>";1151}1152}11531154# git may return quoted and escaped filenames1155sub unquote {1156my$str=shift;11571158sub unq {1159my$seq=shift;1160my%es= (# character escape codes, aka escape sequences1161't'=>"\t",# tab (HT, TAB)1162'n'=>"\n",# newline (NL)1163'r'=>"\r",# return (CR)1164'f'=>"\f",# form feed (FF)1165'b'=>"\b",# backspace (BS)1166'a'=>"\a",# alarm (bell) (BEL)1167'e'=>"\e",# escape (ESC)1168'v'=>"\013",# vertical tab (VT)1169);11701171if($seq=~m/^[0-7]{1,3}$/) {1172# octal char sequence1173returnchr(oct($seq));1174}elsif(exists$es{$seq}) {1175# C escape sequence, aka character escape code1176return$es{$seq};1177}1178# quoted ordinary character1179return$seq;1180}11811182if($str=~m/^"(.*)"$/) {1183# needs unquoting1184$str=$1;1185$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1186}1187return$str;1188}11891190# escape tabs (convert tabs to spaces)1191sub untabify {1192my$line=shift;11931194while((my$pos=index($line,"\t")) != -1) {1195if(my$count= (8- ($pos%8))) {1196my$spaces=' ' x $count;1197$line=~s/\t/$spaces/;1198}1199}12001201return$line;1202}12031204sub project_in_list {1205my$project=shift;1206my@list= git_get_projects_list();1207return@list&&scalar(grep{$_->{'path'}eq$project}@list);1208}12091210## ----------------------------------------------------------------------1211## HTML aware string manipulation12121213# Try to chop given string on a word boundary between position1214# $len and $len+$add_len. If there is no word boundary there,1215# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1216# (marking chopped part) would be longer than given string.1217sub chop_str {1218my$str=shift;1219my$len=shift;1220my$add_len=shift||10;1221my$where=shift||'right';# 'left' | 'center' | 'right'12221223# Make sure perl knows it is utf8 encoded so we don't1224# cut in the middle of a utf8 multibyte char.1225$str= to_utf8($str);12261227# allow only $len chars, but don't cut a word if it would fit in $add_len1228# if it doesn't fit, cut it if it's still longer than the dots we would add1229# remove chopped character entities entirely12301231# when chopping in the middle, distribute $len into left and right part1232# return early if chopping wouldn't make string shorter1233if($whereeq'center') {1234return$strif($len+5>=length($str));# filler is length 51235$len=int($len/2);1236}else{1237return$strif($len+4>=length($str));# filler is length 41238}12391240# regexps: ending and beginning with word part up to $add_len1241my$endre=qr/.{$len}\w{0,$add_len}/;1242my$begre=qr/\w{0,$add_len}.{$len}/;12431244if($whereeq'left') {1245$str=~m/^(.*?)($begre)$/;1246my($lead,$body) = ($1,$2);1247if(length($lead) >4) {1248$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1249$lead=" ...";1250}1251return"$lead$body";12521253}elsif($whereeq'center') {1254$str=~m/^($endre)(.*)$/;1255my($left,$str) = ($1,$2);1256$str=~m/^(.*?)($begre)$/;1257my($mid,$right) = ($1,$2);1258if(length($mid) >5) {1259$left=~s/&[^;]*$//;1260$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1261$mid=" ... ";1262}1263return"$left$mid$right";12641265}else{1266$str=~m/^($endre)(.*)$/;1267my$body=$1;1268my$tail=$2;1269if(length($tail) >4) {1270$body=~s/&[^;]*$//;1271$tail="... ";1272}1273return"$body$tail";1274}1275}12761277# takes the same arguments as chop_str, but also wraps a <span> around the1278# result with a title attribute if it does get chopped. Additionally, the1279# string is HTML-escaped.1280sub chop_and_escape_str {1281my($str) =@_;12821283my$chopped= chop_str(@_);1284if($choppedeq$str) {1285return esc_html($chopped);1286}else{1287$str=~s/[[:cntrl:]]/?/g;1288return$cgi->span({-title=>$str}, esc_html($chopped));1289}1290}12911292## ----------------------------------------------------------------------1293## functions returning short strings12941295# CSS class for given age value (in seconds)1296sub age_class {1297my$age=shift;12981299if(!defined$age) {1300return"noage";1301}elsif($age<60*60*2) {1302return"age0";1303}elsif($age<60*60*24*2) {1304return"age1";1305}else{1306return"age2";1307}1308}13091310# convert age in seconds to "nn units ago" string1311sub age_string {1312my$age=shift;1313my$age_str;13141315if($age>60*60*24*365*2) {1316$age_str= (int$age/60/60/24/365);1317$age_str.=" years ago";1318}elsif($age>60*60*24*(365/12)*2) {1319$age_str=int$age/60/60/24/(365/12);1320$age_str.=" months ago";1321}elsif($age>60*60*24*7*2) {1322$age_str=int$age/60/60/24/7;1323$age_str.=" weeks ago";1324}elsif($age>60*60*24*2) {1325$age_str=int$age/60/60/24;1326$age_str.=" days ago";1327}elsif($age>60*60*2) {1328$age_str=int$age/60/60;1329$age_str.=" hours ago";1330}elsif($age>60*2) {1331$age_str=int$age/60;1332$age_str.=" min ago";1333}elsif($age>2) {1334$age_str=int$age;1335$age_str.=" sec ago";1336}else{1337$age_str.=" right now";1338}1339return$age_str;1340}13411342useconstant{1343 S_IFINVALID =>0030000,1344 S_IFGITLINK =>0160000,1345};13461347# submodule/subproject, a commit object reference1348sub S_ISGITLINK {1349my$mode=shift;13501351return(($mode& S_IFMT) == S_IFGITLINK)1352}13531354# convert file mode in octal to symbolic file mode string1355sub mode_str {1356my$mode=oct shift;13571358if(S_ISGITLINK($mode)) {1359return'm---------';1360}elsif(S_ISDIR($mode& S_IFMT)) {1361return'drwxr-xr-x';1362}elsif(S_ISLNK($mode)) {1363return'lrwxrwxrwx';1364}elsif(S_ISREG($mode)) {1365# git cares only about the executable bit1366if($mode& S_IXUSR) {1367return'-rwxr-xr-x';1368}else{1369return'-rw-r--r--';1370};1371}else{1372return'----------';1373}1374}13751376# convert file mode in octal to file type string1377sub file_type {1378my$mode=shift;13791380if($mode!~m/^[0-7]+$/) {1381return$mode;1382}else{1383$mode=oct$mode;1384}13851386if(S_ISGITLINK($mode)) {1387return"submodule";1388}elsif(S_ISDIR($mode& S_IFMT)) {1389return"directory";1390}elsif(S_ISLNK($mode)) {1391return"symlink";1392}elsif(S_ISREG($mode)) {1393return"file";1394}else{1395return"unknown";1396}1397}13981399# convert file mode in octal to file type description string1400sub file_type_long {1401my$mode=shift;14021403if($mode!~m/^[0-7]+$/) {1404return$mode;1405}else{1406$mode=oct$mode;1407}14081409if(S_ISGITLINK($mode)) {1410return"submodule";1411}elsif(S_ISDIR($mode& S_IFMT)) {1412return"directory";1413}elsif(S_ISLNK($mode)) {1414return"symlink";1415}elsif(S_ISREG($mode)) {1416if($mode& S_IXUSR) {1417return"executable";1418}else{1419return"file";1420};1421}else{1422return"unknown";1423}1424}142514261427## ----------------------------------------------------------------------1428## functions returning short HTML fragments, or transforming HTML fragments1429## which don't belong to other sections14301431# format line of commit message.1432sub format_log_line_html {1433my$line=shift;14341435$line= esc_html($line, -nbsp=>1);1436$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1437$cgi->a({-href => href(action=>"object", hash=>$1),1438-class=>"text"},$1);1439}eg;14401441return$line;1442}14431444# format marker of refs pointing to given object14451446# the destination action is chosen based on object type and current context:1447# - for annotated tags, we choose the tag view unless it's the current view1448# already, in which case we go to shortlog view1449# - for other refs, we keep the current view if we're in history, shortlog or1450# log view, and select shortlog otherwise1451sub format_ref_marker {1452my($refs,$id) =@_;1453my$markers='';14541455if(defined$refs->{$id}) {1456foreachmy$ref(@{$refs->{$id}}) {1457# this code exploits the fact that non-lightweight tags are the1458# only indirect objects, and that they are the only objects for which1459# we want to use tag instead of shortlog as action1460my($type,$name) =qw();1461my$indirect= ($ref=~s/\^\{\}$//);1462# e.g. tags/v2.6.11 or heads/next1463if($ref=~m!^(.*?)s?/(.*)$!) {1464$type=$1;1465$name=$2;1466}else{1467$type="ref";1468$name=$ref;1469}14701471my$class=$type;1472$class.=" indirect"if$indirect;14731474my$dest_action="shortlog";14751476if($indirect) {1477$dest_action="tag"unless$actioneq"tag";1478}elsif($action=~/^(history|(short)?log)$/) {1479$dest_action=$action;1480}14811482my$dest="";1483$dest.="refs/"unless$ref=~ m!^refs/!;1484$dest.=$ref;14851486my$link=$cgi->a({1487-href => href(1488 action=>$dest_action,1489 hash=>$dest1490)},$name);14911492$markers.=" <span class=\"$class\"title=\"$ref\">".1493$link."</span>";1494}1495}14961497if($markers) {1498return' <span class="refs">'.$markers.'</span>';1499}else{1500return"";1501}1502}15031504# format, perhaps shortened and with markers, title line1505sub format_subject_html {1506my($long,$short,$href,$extra) =@_;1507$extra=''unlessdefined($extra);15081509if(length($short) <length($long)) {1510$long=~s/[[:cntrl:]]/?/g;1511return$cgi->a({-href =>$href, -class=>"list subject",1512-title => to_utf8($long)},1513 esc_html($short) .$extra);1514}else{1515return$cgi->a({-href =>$href, -class=>"list subject"},1516 esc_html($long) .$extra);1517}1518}15191520# Rather than recomputing the url for an email multiple times, we cache it1521# after the first hit. This gives a visible benefit in views where the avatar1522# for the same email is used repeatedly (e.g. shortlog).1523# The cache is shared by all avatar engines (currently gravatar only), which1524# are free to use it as preferred. Since only one avatar engine is used for any1525# given page, there's no risk for cache conflicts.1526our%avatar_cache= ();15271528# Compute the picon url for a given email, by using the picon search service over at1529# http://www.cs.indiana.edu/picons/search.html1530sub picon_url {1531my$email=lc shift;1532if(!$avatar_cache{$email}) {1533my($user,$domain) =split('@',$email);1534$avatar_cache{$email} =1535"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1536"$domain/$user/".1537"users+domains+unknown/up/single";1538}1539return$avatar_cache{$email};1540}15411542# Compute the gravatar url for a given email, if it's not in the cache already.1543# Gravatar stores only the part of the URL before the size, since that's the1544# one computationally more expensive. This also allows reuse of the cache for1545# different sizes (for this particular engine).1546sub gravatar_url {1547my$email=lc shift;1548my$size=shift;1549$avatar_cache{$email} ||=1550"http://www.gravatar.com/avatar/".1551 Digest::MD5::md5_hex($email) ."?s=";1552return$avatar_cache{$email} .$size;1553}15541555# Insert an avatar for the given $email at the given $size if the feature1556# is enabled.1557sub git_get_avatar {1558my($email,%opts) =@_;1559my$pre_white= ($opts{-pad_before} ?" ":"");1560my$post_white= ($opts{-pad_after} ?" ":"");1561$opts{-size} ||='default';1562my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1563my$url="";1564if($git_avatareq'gravatar') {1565$url= gravatar_url($email,$size);1566}elsif($git_avatareq'picon') {1567$url= picon_url($email);1568}1569# Other providers can be added by extending the if chain, defining $url1570# as needed. If no variant puts something in $url, we assume avatars1571# are completely disabled/unavailable.1572if($url) {1573return$pre_white.1574"<img width=\"$size\"".1575"class=\"avatar\"".1576"src=\"$url\"".1577"alt=\"\"".1578"/>".$post_white;1579}else{1580return"";1581}1582}15831584# format the author name of the given commit with the given tag1585# the author name is chopped and escaped according to the other1586# optional parameters (see chop_str).1587sub format_author_html {1588my$tag=shift;1589my$co=shift;1590my$author= chop_and_escape_str($co->{'author_name'},@_);1591return"<$tagclass=\"author\">".1592 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1593$author."</$tag>";1594}15951596# format git diff header line, i.e. "diff --(git|combined|cc) ..."1597sub format_git_diff_header_line {1598my$line=shift;1599my$diffinfo=shift;1600my($from,$to) =@_;16011602if($diffinfo->{'nparents'}) {1603# combined diff1604$line=~s!^(diff (.*?) )"?.*$!$1!;1605if($to->{'href'}) {1606$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1607 esc_path($to->{'file'}));1608}else{# file was deleted (no href)1609$line.= esc_path($to->{'file'});1610}1611}else{1612# "ordinary" diff1613$line=~s!^(diff (.*?) )"?a/.*$!$1!;1614if($from->{'href'}) {1615$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1616'a/'. esc_path($from->{'file'}));1617}else{# file was added (no href)1618$line.='a/'. esc_path($from->{'file'});1619}1620$line.=' ';1621if($to->{'href'}) {1622$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1623'b/'. esc_path($to->{'file'}));1624}else{# file was deleted1625$line.='b/'. esc_path($to->{'file'});1626}1627}16281629return"<div class=\"diff header\">$line</div>\n";1630}16311632# format extended diff header line, before patch itself1633sub format_extended_diff_header_line {1634my$line=shift;1635my$diffinfo=shift;1636my($from,$to) =@_;16371638# match <path>1639if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1640$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1641 esc_path($from->{'file'}));1642}1643if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1644$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1645 esc_path($to->{'file'}));1646}1647# match single <mode>1648if($line=~m/\s(\d{6})$/) {1649$line.='<span class="info"> ('.1650 file_type_long($1) .1651')</span>';1652}1653# match <hash>1654if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1655# can match only for combined diff1656$line='index ';1657for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1658if($from->{'href'}[$i]) {1659$line.=$cgi->a({-href=>$from->{'href'}[$i],1660-class=>"hash"},1661substr($diffinfo->{'from_id'}[$i],0,7));1662}else{1663$line.='0' x 7;1664}1665# separator1666$line.=','if($i<$diffinfo->{'nparents'} -1);1667}1668$line.='..';1669if($to->{'href'}) {1670$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1671substr($diffinfo->{'to_id'},0,7));1672}else{1673$line.='0' x 7;1674}16751676}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1677# can match only for ordinary diff1678my($from_link,$to_link);1679if($from->{'href'}) {1680$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1681substr($diffinfo->{'from_id'},0,7));1682}else{1683$from_link='0' x 7;1684}1685if($to->{'href'}) {1686$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1687substr($diffinfo->{'to_id'},0,7));1688}else{1689$to_link='0' x 7;1690}1691my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1692$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1693}16941695return$line."<br/>\n";1696}16971698# format from-file/to-file diff header1699sub format_diff_from_to_header {1700my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1701my$line;1702my$result='';17031704$line=$from_line;1705#assert($line =~ m/^---/) if DEBUG;1706# no extra formatting for "^--- /dev/null"1707if(!$diffinfo->{'nparents'}) {1708# ordinary (single parent) diff1709if($line=~m!^--- "?a/!) {1710if($from->{'href'}) {1711$line='--- a/'.1712$cgi->a({-href=>$from->{'href'}, -class=>"path"},1713 esc_path($from->{'file'}));1714}else{1715$line='--- a/'.1716 esc_path($from->{'file'});1717}1718}1719$result.= qq!<div class="diff from_file">$line</div>\n!;17201721}else{1722# combined diff (merge commit)1723for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1724if($from->{'href'}[$i]) {1725$line='--- '.1726$cgi->a({-href=>href(action=>"blobdiff",1727 hash_parent=>$diffinfo->{'from_id'}[$i],1728 hash_parent_base=>$parents[$i],1729 file_parent=>$from->{'file'}[$i],1730 hash=>$diffinfo->{'to_id'},1731 hash_base=>$hash,1732 file_name=>$to->{'file'}),1733-class=>"path",1734-title=>"diff". ($i+1)},1735$i+1) .1736'/'.1737$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1738 esc_path($from->{'file'}[$i]));1739}else{1740$line='--- /dev/null';1741}1742$result.= qq!<div class="diff from_file">$line</div>\n!;1743}1744}17451746$line=$to_line;1747#assert($line =~ m/^\+\+\+/) if DEBUG;1748# no extra formatting for "^+++ /dev/null"1749if($line=~m!^\+\+\+ "?b/!) {1750if($to->{'href'}) {1751$line='+++ b/'.1752$cgi->a({-href=>$to->{'href'}, -class=>"path"},1753 esc_path($to->{'file'}));1754}else{1755$line='+++ b/'.1756 esc_path($to->{'file'});1757}1758}1759$result.= qq!<div class="diff to_file">$line</div>\n!;17601761return$result;1762}17631764# create note for patch simplified by combined diff1765sub format_diff_cc_simplified {1766my($diffinfo,@parents) =@_;1767my$result='';17681769$result.="<div class=\"diff header\">".1770"diff --cc ";1771if(!is_deleted($diffinfo)) {1772$result.=$cgi->a({-href => href(action=>"blob",1773 hash_base=>$hash,1774 hash=>$diffinfo->{'to_id'},1775 file_name=>$diffinfo->{'to_file'}),1776-class=>"path"},1777 esc_path($diffinfo->{'to_file'}));1778}else{1779$result.= esc_path($diffinfo->{'to_file'});1780}1781$result.="</div>\n".# class="diff header"1782"<div class=\"diff nodifferences\">".1783"Simple merge".1784"</div>\n";# class="diff nodifferences"17851786return$result;1787}17881789# format patch (diff) line (not to be used for diff headers)1790sub format_diff_line {1791my$line=shift;1792my($from,$to) =@_;1793my$diff_class="";17941795chomp$line;17961797if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1798# combined diff1799my$prefix=substr($line,0,scalar@{$from->{'href'}});1800if($line=~m/^\@{3}/) {1801$diff_class=" chunk_header";1802}elsif($line=~m/^\\/) {1803$diff_class=" incomplete";1804}elsif($prefix=~tr/+/+/) {1805$diff_class=" add";1806}elsif($prefix=~tr/-/-/) {1807$diff_class=" rem";1808}1809}else{1810# assume ordinary diff1811my$char=substr($line,0,1);1812if($chareq'+') {1813$diff_class=" add";1814}elsif($chareq'-') {1815$diff_class=" rem";1816}elsif($chareq'@') {1817$diff_class=" chunk_header";1818}elsif($chareq"\\") {1819$diff_class=" incomplete";1820}1821}1822$line= untabify($line);1823if($from&&$to&&$line=~m/^\@{2} /) {1824my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1825$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;18261827$from_lines=0unlessdefined$from_lines;1828$to_lines=0unlessdefined$to_lines;18291830if($from->{'href'}) {1831$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1832-class=>"list"},$from_text);1833}1834if($to->{'href'}) {1835$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1836-class=>"list"},$to_text);1837}1838$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1839"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1840return"<div class=\"diff$diff_class\">$line</div>\n";1841}elsif($from&&$to&&$line=~m/^\@{3}/) {1842my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1843my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);18441845@from_text=split(' ',$ranges);1846for(my$i=0;$i<@from_text; ++$i) {1847($from_start[$i],$from_nlines[$i]) =1848(split(',',substr($from_text[$i],1)),0);1849}18501851$to_text=pop@from_text;1852$to_start=pop@from_start;1853$to_nlines=pop@from_nlines;18541855$line="<span class=\"chunk_info\">$prefix";1856for(my$i=0;$i<@from_text; ++$i) {1857if($from->{'href'}[$i]) {1858$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1859-class=>"list"},$from_text[$i]);1860}else{1861$line.=$from_text[$i];1862}1863$line.=" ";1864}1865if($to->{'href'}) {1866$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1867-class=>"list"},$to_text);1868}else{1869$line.=$to_text;1870}1871$line.="$prefix</span>".1872"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1873return"<div class=\"diff$diff_class\">$line</div>\n";1874}1875return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1876}18771878# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1879# linked. Pass the hash of the tree/commit to snapshot.1880sub format_snapshot_links {1881my($hash) =@_;1882my$num_fmts=@snapshot_fmts;1883if($num_fmts>1) {1884# A parenthesized list of links bearing format names.1885# e.g. "snapshot (_tar.gz_ _zip_)"1886return"snapshot (".join(' ',map1887$cgi->a({1888-href => href(1889 action=>"snapshot",1890 hash=>$hash,1891 snapshot_format=>$_1892)1893},$known_snapshot_formats{$_}{'display'})1894,@snapshot_fmts) .")";1895}elsif($num_fmts==1) {1896# A single "snapshot" link whose tooltip bears the format name.1897# i.e. "_snapshot_"1898my($fmt) =@snapshot_fmts;1899return1900$cgi->a({1901-href => href(1902 action=>"snapshot",1903 hash=>$hash,1904 snapshot_format=>$fmt1905),1906-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1907},"snapshot");1908}else{# $num_fmts == 01909returnundef;1910}1911}19121913## ......................................................................1914## functions returning values to be passed, perhaps after some1915## transformation, to other functions; e.g. returning arguments to href()19161917# returns hash to be passed to href to generate gitweb URL1918# in -title key it returns description of link1919sub get_feed_info {1920my$format=shift||'Atom';1921my%res= (action =>lc($format));19221923# feed links are possible only for project views1924return unless(defined$project);1925# some views should link to OPML, or to generic project feed,1926# or don't have specific feed yet (so they should use generic)1927return if($action=~/^(?:tags|heads|forks|tag|search)$/x);19281929my$branch;1930# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1931# from tag links; this also makes possible to detect branch links1932if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1933(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1934$branch=$1;1935}1936# find log type for feed description (title)1937my$type='log';1938if(defined$file_name) {1939$type="history of$file_name";1940$type.="/"if($actioneq'tree');1941$type.=" on '$branch'"if(defined$branch);1942}else{1943$type="log of$branch"if(defined$branch);1944}19451946$res{-title} =$type;1947$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1948$res{'file_name'} =$file_name;19491950return%res;1951}19521953## ----------------------------------------------------------------------1954## git utility subroutines, invoking git commands19551956# returns path to the core git executable and the --git-dir parameter as list1957sub git_cmd {1958return$GIT,'--git-dir='.$git_dir;1959}19601961# quote the given arguments for passing them to the shell1962# quote_command("command", "arg 1", "arg with ' and ! characters")1963# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1964# Try to avoid using this function wherever possible.1965sub quote_command {1966returnjoin(' ',1967map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);1968}19691970# get HEAD ref of given project as hash1971sub git_get_head_hash {1972my$project=shift;1973my$o_git_dir=$git_dir;1974my$retval=undef;1975$git_dir="$projectroot/$project";1976if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1977my$head= <$fd>;1978close$fd;1979if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1980$retval=$1;1981}1982}1983if(defined$o_git_dir) {1984$git_dir=$o_git_dir;1985}1986return$retval;1987}19881989# get type of given object1990sub git_get_type {1991my$hash=shift;19921993open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1994my$type= <$fd>;1995close$fdorreturn;1996chomp$type;1997return$type;1998}19992000# repository configuration2001our$config_file='';2002our%config;20032004# store multiple values for single key as anonymous array reference2005# single values stored directly in the hash, not as [ <value> ]2006sub hash_set_multi {2007my($hash,$key,$value) =@_;20082009if(!exists$hash->{$key}) {2010$hash->{$key} =$value;2011}elsif(!ref$hash->{$key}) {2012$hash->{$key} = [$hash->{$key},$value];2013}else{2014push@{$hash->{$key}},$value;2015}2016}20172018# return hash of git project configuration2019# optionally limited to some section, e.g. 'gitweb'2020sub git_parse_project_config {2021my$section_regexp=shift;2022my%config;20232024local$/="\0";20252026open my$fh,"-|", git_cmd(),"config",'-z','-l',2027orreturn;20282029while(my$keyval= <$fh>) {2030chomp$keyval;2031my($key,$value) =split(/\n/,$keyval,2);20322033 hash_set_multi(\%config,$key,$value)2034if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2035}2036close$fh;20372038return%config;2039}20402041# convert config value to boolean: 'true' or 'false'2042# no value, number > 0, 'true' and 'yes' values are true2043# rest of values are treated as false (never as error)2044sub config_to_bool {2045my$val=shift;20462047return1if!defined$val;# section.key20482049# strip leading and trailing whitespace2050$val=~s/^\s+//;2051$val=~s/\s+$//;20522053return(($val=~/^\d+$/&&$val) ||# section.key = 12054($val=~/^(?:true|yes)$/i));# section.key = true2055}20562057# convert config value to simple decimal number2058# an optional value suffix of 'k', 'm', or 'g' will cause the value2059# to be multiplied by 1024, 1048576, or 10737418242060sub config_to_int {2061my$val=shift;20622063# strip leading and trailing whitespace2064$val=~s/^\s+//;2065$val=~s/\s+$//;20662067if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2068$unit=lc($unit);2069# unknown unit is treated as 12070return$num* ($uniteq'g'?1073741824:2071$uniteq'm'?1048576:2072$uniteq'k'?1024:1);2073}2074return$val;2075}20762077# convert config value to array reference, if needed2078sub config_to_multi {2079my$val=shift;20802081returnref($val) ?$val: (defined($val) ? [$val] : []);2082}20832084sub git_get_project_config {2085my($key,$type) =@_;20862087# key sanity check2088return unless($key);2089$key=~s/^gitweb\.//;2090return if($key=~m/\W/);20912092# type sanity check2093if(defined$type) {2094$type=~s/^--//;2095$type=undef2096unless($typeeq'bool'||$typeeq'int');2097}20982099# get config2100if(!defined$config_file||2101$config_filene"$git_dir/config") {2102%config= git_parse_project_config('gitweb');2103$config_file="$git_dir/config";2104}21052106# check if config variable (key) exists2107return unlessexists$config{"gitweb.$key"};21082109# ensure given type2110if(!defined$type) {2111return$config{"gitweb.$key"};2112}elsif($typeeq'bool') {2113# backward compatibility: 'git config --bool' returns true/false2114return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2115}elsif($typeeq'int') {2116return config_to_int($config{"gitweb.$key"});2117}2118return$config{"gitweb.$key"};2119}21202121# get hash of given path at given ref2122sub git_get_hash_by_path {2123my$base=shift;2124my$path=shift||returnundef;2125my$type=shift;21262127$path=~ s,/+$,,;21282129open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2130or die_error(500,"Open git-ls-tree failed");2131my$line= <$fd>;2132close$fdorreturnundef;21332134if(!defined$line) {2135# there is no tree or hash given by $path at $base2136returnundef;2137}21382139#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2140$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2141if(defined$type&&$typene$2) {2142# type doesn't match2143returnundef;2144}2145return$3;2146}21472148# get path of entry with given hash at given tree-ish (ref)2149# used to get 'from' filename for combined diff (merge commit) for renames2150sub git_get_path_by_hash {2151my$base=shift||return;2152my$hash=shift||return;21532154local$/="\0";21552156open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2157orreturnundef;2158while(my$line= <$fd>) {2159chomp$line;21602161#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2162#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2163if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2164close$fd;2165return$1;2166}2167}2168close$fd;2169returnundef;2170}21712172## ......................................................................2173## git utility functions, directly accessing git repository21742175sub git_get_project_description {2176my$path=shift;21772178$git_dir="$projectroot/$path";2179open my$fd,'<',"$git_dir/description"2180orreturn git_get_project_config('description');2181my$descr= <$fd>;2182close$fd;2183if(defined$descr) {2184chomp$descr;2185}2186return$descr;2187}21882189sub git_get_project_ctags {2190my$path=shift;2191my$ctags= {};21922193$git_dir="$projectroot/$path";2194opendir my$dh,"$git_dir/ctags"2195orreturn$ctags;2196foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2197open my$ct,'<',$_ornext;2198my$val= <$ct>;2199chomp$val;2200close$ct;2201my$ctag=$_;$ctag=~ s#.*/##;2202$ctags->{$ctag} =$val;2203}2204closedir$dh;2205$ctags;2206}22072208sub git_populate_project_tagcloud {2209my$ctags=shift;22102211# First, merge different-cased tags; tags vote on casing2212my%ctags_lc;2213foreach(keys%$ctags) {2214$ctags_lc{lc$_}->{count} +=$ctags->{$_};2215if(not$ctags_lc{lc$_}->{topcount}2216or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2217$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2218$ctags_lc{lc$_}->{topname} =$_;2219}2220}22212222my$cloud;2223if(eval{require HTML::TagCloud;1; }) {2224$cloud= HTML::TagCloud->new;2225foreach(sort keys%ctags_lc) {2226# Pad the title with spaces so that the cloud looks2227# less crammed.2228my$title=$ctags_lc{$_}->{topname};2229$title=~s/ / /g;2230$title=~s/^/ /g;2231$title=~s/$/ /g;2232$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2233}2234}else{2235$cloud= \%ctags_lc;2236}2237$cloud;2238}22392240sub git_show_project_tagcloud {2241my($cloud,$count) =@_;2242print STDERR ref($cloud)."..\n";2243if(ref$cloudeq'HTML::TagCloud') {2244return$cloud->html_and_css($count);2245}else{2246my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2247return'<p align="center">'.join(', ',map{2248"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2249}splice(@tags,0,$count)) .'</p>';2250}2251}22522253sub git_get_project_url_list {2254my$path=shift;22552256$git_dir="$projectroot/$path";2257open my$fd,'<',"$git_dir/cloneurl"2258orreturnwantarray?2259@{ config_to_multi(git_get_project_config('url')) } :2260 config_to_multi(git_get_project_config('url'));2261my@git_project_url_list=map{chomp;$_} <$fd>;2262close$fd;22632264returnwantarray?@git_project_url_list: \@git_project_url_list;2265}22662267sub git_get_projects_list {2268my($filter) =@_;2269my@list;22702271$filter||='';2272$filter=~s/\.git$//;22732274my$check_forks= gitweb_check_feature('forks');22752276if(-d $projects_list) {2277# search in directory2278my$dir=$projects_list. ($filter?"/$filter":'');2279# remove the trailing "/"2280$dir=~s!/+$!!;2281my$pfxlen=length("$dir");2282my$pfxdepth= ($dir=~tr!/!!);22832284 File::Find::find({2285 follow_fast =>1,# follow symbolic links2286 follow_skip =>2,# ignore duplicates2287 dangling_symlinks =>0,# ignore dangling symlinks, silently2288 wanted =>sub{2289# skip project-list toplevel, if we get it.2290return if(m!^[/.]$!);2291# only directories can be git repositories2292return unless(-d $_);2293# don't traverse too deep (Find is super slow on os x)2294if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2295$File::Find::prune =1;2296return;2297}22982299my$subdir=substr($File::Find::name,$pfxlen+1);2300# we check related file in $projectroot2301my$path= ($filter?"$filter/":'') .$subdir;2302if(check_export_ok("$projectroot/$path")) {2303push@list, { path =>$path};2304$File::Find::prune =1;2305}2306},2307},"$dir");23082309}elsif(-f $projects_list) {2310# read from file(url-encoded):2311# 'git%2Fgit.git Linus+Torvalds'2312# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2313# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2314my%paths;2315open my$fd,'<',$projects_listorreturn;2316 PROJECT:2317while(my$line= <$fd>) {2318chomp$line;2319my($path,$owner) =split' ',$line;2320$path= unescape($path);2321$owner= unescape($owner);2322if(!defined$path) {2323next;2324}2325if($filterne'') {2326# looking for forks;2327my$pfx=substr($path,0,length($filter));2328if($pfxne$filter) {2329next PROJECT;2330}2331my$sfx=substr($path,length($filter));2332if($sfx!~/^\/.*\.git$/) {2333next PROJECT;2334}2335}elsif($check_forks) {2336 PATH:2337foreachmy$filter(keys%paths) {2338# looking for forks;2339my$pfx=substr($path,0,length($filter));2340if($pfxne$filter) {2341next PATH;2342}2343my$sfx=substr($path,length($filter));2344if($sfx!~/^\/.*\.git$/) {2345next PATH;2346}2347# is a fork, don't include it in2348# the list2349next PROJECT;2350}2351}2352if(check_export_ok("$projectroot/$path")) {2353my$pr= {2354 path =>$path,2355 owner => to_utf8($owner),2356};2357push@list,$pr;2358(my$forks_path=$path) =~s/\.git$//;2359$paths{$forks_path}++;2360}2361}2362close$fd;2363}2364return@list;2365}23662367our$gitweb_project_owner=undef;2368sub git_get_project_list_from_file {23692370return if(defined$gitweb_project_owner);23712372$gitweb_project_owner= {};2373# read from file (url-encoded):2374# 'git%2Fgit.git Linus+Torvalds'2375# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2376# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2377if(-f $projects_list) {2378open(my$fd,'<',$projects_list);2379while(my$line= <$fd>) {2380chomp$line;2381my($pr,$ow) =split' ',$line;2382$pr= unescape($pr);2383$ow= unescape($ow);2384$gitweb_project_owner->{$pr} = to_utf8($ow);2385}2386close$fd;2387}2388}23892390sub git_get_project_owner {2391my$project=shift;2392my$owner;23932394returnundefunless$project;2395$git_dir="$projectroot/$project";23962397if(!defined$gitweb_project_owner) {2398 git_get_project_list_from_file();2399}24002401if(exists$gitweb_project_owner->{$project}) {2402$owner=$gitweb_project_owner->{$project};2403}2404if(!defined$owner){2405$owner= git_get_project_config('owner');2406}2407if(!defined$owner) {2408$owner= get_file_owner("$git_dir");2409}24102411return$owner;2412}24132414sub git_get_last_activity {2415my($path) =@_;2416my$fd;24172418$git_dir="$projectroot/$path";2419open($fd,"-|", git_cmd(),'for-each-ref',2420'--format=%(committer)',2421'--sort=-committerdate',2422'--count=1',2423'refs/heads')orreturn;2424my$most_recent= <$fd>;2425close$fdorreturn;2426if(defined$most_recent&&2427$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2428my$timestamp=$1;2429my$age=time-$timestamp;2430return($age, age_string($age));2431}2432return(undef,undef);2433}24342435sub git_get_references {2436my$type=shift||"";2437my%refs;2438# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112439# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2440open my$fd,"-|", git_cmd(),"show-ref","--dereference",2441($type? ("--","refs/$type") : ())# use -- <pattern> if $type2442orreturn;24432444while(my$line= <$fd>) {2445chomp$line;2446if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2447if(defined$refs{$1}) {2448push@{$refs{$1}},$2;2449}else{2450$refs{$1} = [$2];2451}2452}2453}2454close$fdorreturn;2455return \%refs;2456}24572458sub git_get_rev_name_tags {2459my$hash=shift||returnundef;24602461open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2462orreturn;2463my$name_rev= <$fd>;2464close$fd;24652466if($name_rev=~ m|^$hash tags/(.*)$|) {2467return$1;2468}else{2469# catches also '$hash undefined' output2470returnundef;2471}2472}24732474## ----------------------------------------------------------------------2475## parse to hash functions24762477sub parse_date {2478my$epoch=shift;2479my$tz=shift||"-0000";24802481my%date;2482my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2483my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2484my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2485$date{'hour'} =$hour;2486$date{'minute'} =$min;2487$date{'mday'} =$mday;2488$date{'day'} =$days[$wday];2489$date{'month'} =$months[$mon];2490$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2491$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2492$date{'mday-time'} =sprintf"%d%s%02d:%02d",2493$mday,$months[$mon],$hour,$min;2494$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",24951900+$year,1+$mon,$mday,$hour,$min,$sec;24962497$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2498my$local=$epoch+ ((int$1+ ($2/60)) *3600);2499($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2500$date{'hour_local'} =$hour;2501$date{'minute_local'} =$min;2502$date{'tz_local'} =$tz;2503$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",25041900+$year,$mon+1,$mday,2505$hour,$min,$sec,$tz);2506return%date;2507}25082509sub parse_tag {2510my$tag_id=shift;2511my%tag;2512my@comment;25132514open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2515$tag{'id'} =$tag_id;2516while(my$line= <$fd>) {2517chomp$line;2518if($line=~m/^object ([0-9a-fA-F]{40})$/) {2519$tag{'object'} =$1;2520}elsif($line=~m/^type (.+)$/) {2521$tag{'type'} =$1;2522}elsif($line=~m/^tag (.+)$/) {2523$tag{'name'} =$1;2524}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2525$tag{'author'} =$1;2526$tag{'author_epoch'} =$2;2527$tag{'author_tz'} =$3;2528if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2529$tag{'author_name'} =$1;2530$tag{'author_email'} =$2;2531}else{2532$tag{'author_name'} =$tag{'author'};2533}2534}elsif($line=~m/--BEGIN/) {2535push@comment,$line;2536last;2537}elsif($lineeq"") {2538last;2539}2540}2541push@comment, <$fd>;2542$tag{'comment'} = \@comment;2543close$fdorreturn;2544if(!defined$tag{'name'}) {2545return2546};2547return%tag2548}25492550sub parse_commit_text {2551my($commit_text,$withparents) =@_;2552my@commit_lines=split'\n',$commit_text;2553my%co;25542555pop@commit_lines;# Remove '\0'25562557if(!@commit_lines) {2558return;2559}25602561my$header=shift@commit_lines;2562if($header!~m/^[0-9a-fA-F]{40}/) {2563return;2564}2565($co{'id'},my@parents) =split' ',$header;2566while(my$line=shift@commit_lines) {2567last if$lineeq"\n";2568if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2569$co{'tree'} =$1;2570}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2571push@parents,$1;2572}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2573$co{'author'} =$1;2574$co{'author_epoch'} =$2;2575$co{'author_tz'} =$3;2576if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2577$co{'author_name'} =$1;2578$co{'author_email'} =$2;2579}else{2580$co{'author_name'} =$co{'author'};2581}2582}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2583$co{'committer'} =$1;2584$co{'committer_epoch'} =$2;2585$co{'committer_tz'} =$3;2586$co{'committer_name'} =$co{'committer'};2587if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2588$co{'committer_name'} =$1;2589$co{'committer_email'} =$2;2590}else{2591$co{'committer_name'} =$co{'committer'};2592}2593}2594}2595if(!defined$co{'tree'}) {2596return;2597};2598$co{'parents'} = \@parents;2599$co{'parent'} =$parents[0];26002601foreachmy$title(@commit_lines) {2602$title=~s/^ //;2603if($titlene"") {2604$co{'title'} = chop_str($title,80,5);2605# remove leading stuff of merges to make the interesting part visible2606if(length($title) >50) {2607$title=~s/^Automatic //;2608$title=~s/^merge (of|with) /Merge ... /i;2609if(length($title) >50) {2610$title=~s/(http|rsync):\/\///;2611}2612if(length($title) >50) {2613$title=~s/(master|www|rsync)\.//;2614}2615if(length($title) >50) {2616$title=~s/kernel.org:?//;2617}2618if(length($title) >50) {2619$title=~s/\/pub\/scm//;2620}2621}2622$co{'title_short'} = chop_str($title,50,5);2623last;2624}2625}2626if(!defined$co{'title'} ||$co{'title'}eq"") {2627$co{'title'} =$co{'title_short'} ='(no commit message)';2628}2629# remove added spaces2630foreachmy$line(@commit_lines) {2631$line=~s/^ //;2632}2633$co{'comment'} = \@commit_lines;26342635my$age=time-$co{'committer_epoch'};2636$co{'age'} =$age;2637$co{'age_string'} = age_string($age);2638my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2639if($age>60*60*24*7*2) {2640$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2641$co{'age_string_age'} =$co{'age_string'};2642}else{2643$co{'age_string_date'} =$co{'age_string'};2644$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2645}2646return%co;2647}26482649sub parse_commit {2650my($commit_id) =@_;2651my%co;26522653local$/="\0";26542655open my$fd,"-|", git_cmd(),"rev-list",2656"--parents",2657"--header",2658"--max-count=1",2659$commit_id,2660"--",2661or die_error(500,"Open git-rev-list failed");2662%co= parse_commit_text(<$fd>,1);2663close$fd;26642665return%co;2666}26672668sub parse_commits {2669my($commit_id,$maxcount,$skip,$filename,@args) =@_;2670my@cos;26712672$maxcount||=1;2673$skip||=0;26742675local$/="\0";26762677open my$fd,"-|", git_cmd(),"rev-list",2678"--header",2679@args,2680("--max-count=".$maxcount),2681("--skip=".$skip),2682@extra_options,2683$commit_id,2684"--",2685($filename? ($filename) : ())2686or die_error(500,"Open git-rev-list failed");2687while(my$line= <$fd>) {2688my%co= parse_commit_text($line);2689push@cos, \%co;2690}2691close$fd;26922693returnwantarray?@cos: \@cos;2694}26952696# parse line of git-diff-tree "raw" output2697sub parse_difftree_raw_line {2698my$line=shift;2699my%res;27002701# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2702# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2703if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2704$res{'from_mode'} =$1;2705$res{'to_mode'} =$2;2706$res{'from_id'} =$3;2707$res{'to_id'} =$4;2708$res{'status'} =$5;2709$res{'similarity'} =$6;2710if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2711($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2712}else{2713$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2714}2715}2716# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2717# combined diff (for merge commit)2718elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2719$res{'nparents'} =length($1);2720$res{'from_mode'} = [split(' ',$2) ];2721$res{'to_mode'} =pop@{$res{'from_mode'}};2722$res{'from_id'} = [split(' ',$3) ];2723$res{'to_id'} =pop@{$res{'from_id'}};2724$res{'status'} = [split('',$4) ];2725$res{'to_file'} = unquote($5);2726}2727# 'c512b523472485aef4fff9e57b229d9d243c967f'2728elsif($line=~m/^([0-9a-fA-F]{40})$/) {2729$res{'commit'} =$1;2730}27312732returnwantarray?%res: \%res;2733}27342735# wrapper: return parsed line of git-diff-tree "raw" output2736# (the argument might be raw line, or parsed info)2737sub parsed_difftree_line {2738my$line_or_ref=shift;27392740if(ref($line_or_ref)eq"HASH") {2741# pre-parsed (or generated by hand)2742return$line_or_ref;2743}else{2744return parse_difftree_raw_line($line_or_ref);2745}2746}27472748# parse line of git-ls-tree output2749sub parse_ls_tree_line {2750my$line=shift;2751my%opts=@_;2752my%res;27532754#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2755$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;27562757$res{'mode'} =$1;2758$res{'type'} =$2;2759$res{'hash'} =$3;2760if($opts{'-z'}) {2761$res{'name'} =$4;2762}else{2763$res{'name'} = unquote($4);2764}27652766returnwantarray?%res: \%res;2767}27682769# generates _two_ hashes, references to which are passed as 2 and 3 argument2770sub parse_from_to_diffinfo {2771my($diffinfo,$from,$to,@parents) =@_;27722773if($diffinfo->{'nparents'}) {2774# combined diff2775$from->{'file'} = [];2776$from->{'href'} = [];2777 fill_from_file_info($diffinfo,@parents)2778unlessexists$diffinfo->{'from_file'};2779for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2780$from->{'file'}[$i] =2781defined$diffinfo->{'from_file'}[$i] ?2782$diffinfo->{'from_file'}[$i] :2783$diffinfo->{'to_file'};2784if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2785$from->{'href'}[$i] = href(action=>"blob",2786 hash_base=>$parents[$i],2787 hash=>$diffinfo->{'from_id'}[$i],2788 file_name=>$from->{'file'}[$i]);2789}else{2790$from->{'href'}[$i] =undef;2791}2792}2793}else{2794# ordinary (not combined) diff2795$from->{'file'} =$diffinfo->{'from_file'};2796if($diffinfo->{'status'}ne"A") {# not new (added) file2797$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2798 hash=>$diffinfo->{'from_id'},2799 file_name=>$from->{'file'});2800}else{2801delete$from->{'href'};2802}2803}28042805$to->{'file'} =$diffinfo->{'to_file'};2806if(!is_deleted($diffinfo)) {# file exists in result2807$to->{'href'} = href(action=>"blob", hash_base=>$hash,2808 hash=>$diffinfo->{'to_id'},2809 file_name=>$to->{'file'});2810}else{2811delete$to->{'href'};2812}2813}28142815## ......................................................................2816## parse to array of hashes functions28172818sub git_get_heads_list {2819my$limit=shift;2820my@headslist;28212822open my$fd,'-|', git_cmd(),'for-each-ref',2823($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2824'--format=%(objectname) %(refname) %(subject)%00%(committer)',2825'refs/heads'2826orreturn;2827while(my$line= <$fd>) {2828my%ref_item;28292830chomp$line;2831my($refinfo,$committerinfo) =split(/\0/,$line);2832my($hash,$name,$title) =split(' ',$refinfo,3);2833my($committer,$epoch,$tz) =2834($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2835$ref_item{'fullname'} =$name;2836$name=~s!^refs/heads/!!;28372838$ref_item{'name'} =$name;2839$ref_item{'id'} =$hash;2840$ref_item{'title'} =$title||'(no commit message)';2841$ref_item{'epoch'} =$epoch;2842if($epoch) {2843$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2844}else{2845$ref_item{'age'} ="unknown";2846}28472848push@headslist, \%ref_item;2849}2850close$fd;28512852returnwantarray?@headslist: \@headslist;2853}28542855sub git_get_tags_list {2856my$limit=shift;2857my@tagslist;28582859open my$fd,'-|', git_cmd(),'for-each-ref',2860($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2861'--format=%(objectname) %(objecttype) %(refname) '.2862'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2863'refs/tags'2864orreturn;2865while(my$line= <$fd>) {2866my%ref_item;28672868chomp$line;2869my($refinfo,$creatorinfo) =split(/\0/,$line);2870my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2871my($creator,$epoch,$tz) =2872($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2873$ref_item{'fullname'} =$name;2874$name=~s!^refs/tags/!!;28752876$ref_item{'type'} =$type;2877$ref_item{'id'} =$id;2878$ref_item{'name'} =$name;2879if($typeeq"tag") {2880$ref_item{'subject'} =$title;2881$ref_item{'reftype'} =$reftype;2882$ref_item{'refid'} =$refid;2883}else{2884$ref_item{'reftype'} =$type;2885$ref_item{'refid'} =$id;2886}28872888if($typeeq"tag"||$typeeq"commit") {2889$ref_item{'epoch'} =$epoch;2890if($epoch) {2891$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2892}else{2893$ref_item{'age'} ="unknown";2894}2895}28962897push@tagslist, \%ref_item;2898}2899close$fd;29002901returnwantarray?@tagslist: \@tagslist;2902}29032904## ----------------------------------------------------------------------2905## filesystem-related functions29062907sub get_file_owner {2908my$path=shift;29092910my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2911my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2912if(!defined$gcos) {2913returnundef;2914}2915my$owner=$gcos;2916$owner=~s/[,;].*$//;2917return to_utf8($owner);2918}29192920# assume that file exists2921sub insert_file {2922my$filename=shift;29232924open my$fd,'<',$filename;2925print map{ to_utf8($_) } <$fd>;2926close$fd;2927}29282929## ......................................................................2930## mimetype related functions29312932sub mimetype_guess_file {2933my$filename=shift;2934my$mimemap=shift;2935-r $mimemaporreturnundef;29362937my%mimemap;2938open(my$mh,'<',$mimemap)orreturnundef;2939while(<$mh>) {2940next ifm/^#/;# skip comments2941my($mimetype,$exts) =split(/\t+/);2942if(defined$exts) {2943my@exts=split(/\s+/,$exts);2944foreachmy$ext(@exts) {2945$mimemap{$ext} =$mimetype;2946}2947}2948}2949close($mh);29502951$filename=~/\.([^.]*)$/;2952return$mimemap{$1};2953}29542955sub mimetype_guess {2956my$filename=shift;2957my$mime;2958$filename=~/\./orreturnundef;29592960if($mimetypes_file) {2961my$file=$mimetypes_file;2962if($file!~m!^/!) {# if it is relative path2963# it is relative to project2964$file="$projectroot/$project/$file";2965}2966$mime= mimetype_guess_file($filename,$file);2967}2968$mime||= mimetype_guess_file($filename,'/etc/mime.types');2969return$mime;2970}29712972sub blob_mimetype {2973my$fd=shift;2974my$filename=shift;29752976if($filename) {2977my$mime= mimetype_guess($filename);2978$mimeandreturn$mime;2979}29802981# just in case2982return$default_blob_plain_mimetypeunless$fd;29832984if(-T $fd) {2985return'text/plain';2986}elsif(!$filename) {2987return'application/octet-stream';2988}elsif($filename=~m/\.png$/i) {2989return'image/png';2990}elsif($filename=~m/\.gif$/i) {2991return'image/gif';2992}elsif($filename=~m/\.jpe?g$/i) {2993return'image/jpeg';2994}else{2995return'application/octet-stream';2996}2997}29982999sub blob_contenttype {3000my($fd,$file_name,$type) =@_;30013002$type||= blob_mimetype($fd,$file_name);3003if($typeeq'text/plain'&&defined$default_text_plain_charset) {3004$type.="; charset=$default_text_plain_charset";3005}30063007return$type;3008}30093010## ======================================================================3011## functions printing HTML: header, footer, error page30123013sub git_header_html {3014my$status=shift||"200 OK";3015my$expires=shift;30163017my$title="$site_name";3018if(defined$project) {3019$title.=" - ". to_utf8($project);3020if(defined$action) {3021$title.="/$action";3022if(defined$file_name) {3023$title.=" - ". esc_path($file_name);3024if($actioneq"tree"&&$file_name!~ m|/$|) {3025$title.="/";3026}3027}3028}3029}3030my$content_type;3031# require explicit support from the UA if we are to send the page as3032# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3033# we have to do this because MSIE sometimes globs '*/*', pretending to3034# support xhtml+xml but choking when it gets what it asked for.3035if(defined$cgi->http('HTTP_ACCEPT') &&3036$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3037$cgi->Accept('application/xhtml+xml') !=0) {3038$content_type='application/xhtml+xml';3039}else{3040$content_type='text/html';3041}3042print$cgi->header(-type=>$content_type, -charset =>'utf-8',3043-status=>$status, -expires =>$expires);3044my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3045print<<EOF;3046<?xml version="1.0" encoding="utf-8"?>3047<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3048<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3049<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3050<!-- git core binaries version$git_version-->3051<head>3052<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3053<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3054<meta name="robots" content="index, nofollow"/>3055<title>$title</title>3056EOF3057# the stylesheet, favicon etc urls won't work correctly with path_info3058# unless we set the appropriate base URL3059if($ENV{'PATH_INFO'}) {3060print"<base href=\"".esc_url($base_url)."\"/>\n";3061}3062# print out each stylesheet that exist, providing backwards capability3063# for those people who defined $stylesheet in a config file3064if(defined$stylesheet) {3065print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3066}else{3067foreachmy$stylesheet(@stylesheets) {3068next unless$stylesheet;3069print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3070}3071}3072if(defined$project) {3073my%href_params= get_feed_info();3074if(!exists$href_params{'-title'}) {3075$href_params{'-title'} ='log';3076}30773078foreachmy$formatqw(RSS Atom){3079my$type=lc($format);3080my%link_attr= (3081'-rel'=>'alternate',3082'-title'=>"$project-$href_params{'-title'} -$formatfeed",3083'-type'=>"application/$type+xml"3084);30853086$href_params{'action'} =$type;3087$link_attr{'-href'} = href(%href_params);3088print"<link ".3089"rel=\"$link_attr{'-rel'}\"".3090"title=\"$link_attr{'-title'}\"".3091"href=\"$link_attr{'-href'}\"".3092"type=\"$link_attr{'-type'}\"".3093"/>\n";30943095$href_params{'extra_options'} ='--no-merges';3096$link_attr{'-href'} = href(%href_params);3097$link_attr{'-title'} .=' (no merges)';3098print"<link ".3099"rel=\"$link_attr{'-rel'}\"".3100"title=\"$link_attr{'-title'}\"".3101"href=\"$link_attr{'-href'}\"".3102"type=\"$link_attr{'-type'}\"".3103"/>\n";3104}31053106}else{3107printf('<link rel="alternate" title="%sprojects list" '.3108'href="%s" type="text/plain; charset=utf-8" />'."\n",3109$site_name, href(project=>undef, action=>"project_index"));3110printf('<link rel="alternate" title="%sprojects feeds" '.3111'href="%s" type="text/x-opml" />'."\n",3112$site_name, href(project=>undef, action=>"opml"));3113}3114if(defined$favicon) {3115printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3116}31173118print"</head>\n".3119"<body>\n";31203121if(-f $site_header) {3122 insert_file($site_header);3123}31243125print"<div class=\"page_header\">\n".3126$cgi->a({-href => esc_url($logo_url),3127-title =>$logo_label},3128qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3129print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3130if(defined$project) {3131print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3132if(defined$action) {3133print" /$action";3134}3135print"\n";3136}3137print"</div>\n";31383139my$have_search= gitweb_check_feature('search');3140if(defined$project&&$have_search) {3141if(!defined$searchtext) {3142$searchtext="";3143}3144my$search_hash;3145if(defined$hash_base) {3146$search_hash=$hash_base;3147}elsif(defined$hash) {3148$search_hash=$hash;3149}else{3150$search_hash="HEAD";3151}3152my$action=$my_uri;3153my$use_pathinfo= gitweb_check_feature('pathinfo');3154if($use_pathinfo) {3155$action.="/".esc_url($project);3156}3157print$cgi->startform(-method=>"get", -action =>$action) .3158"<div class=\"search\">\n".3159(!$use_pathinfo&&3160$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3161$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3162$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3163$cgi->popup_menu(-name =>'st', -default=>'commit',3164-values=> ['commit','grep','author','committer','pickaxe']) .3165$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3166" search:\n",3167$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3168"<span title=\"Extended regular expression\">".3169$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3170-checked =>$search_use_regexp) .3171"</span>".3172"</div>".3173$cgi->end_form() ."\n";3174}3175}31763177sub git_footer_html {3178my$feed_class='rss_logo';31793180print"<div class=\"page_footer\">\n";3181if(defined$project) {3182my$descr= git_get_project_description($project);3183if(defined$descr) {3184print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3185}31863187my%href_params= get_feed_info();3188if(!%href_params) {3189$feed_class.=' generic';3190}3191$href_params{'-title'} ||='log';31923193foreachmy$formatqw(RSS Atom){3194$href_params{'action'} =lc($format);3195print$cgi->a({-href => href(%href_params),3196-title =>"$href_params{'-title'}$formatfeed",3197-class=>$feed_class},$format)."\n";3198}31993200}else{3201print$cgi->a({-href => href(project=>undef, action=>"opml"),3202-class=>$feed_class},"OPML") ." ";3203print$cgi->a({-href => href(project=>undef, action=>"project_index"),3204-class=>$feed_class},"TXT") ."\n";3205}3206print"</div>\n";# class="page_footer"32073208if(-f $site_footer) {3209 insert_file($site_footer);3210}32113212print"</body>\n".3213"</html>";3214}32153216# die_error(<http_status_code>, <error_message>)3217# Example: die_error(404, 'Hash not found')3218# By convention, use the following status codes (as defined in RFC 2616):3219# 400: Invalid or missing CGI parameters, or3220# requested object exists but has wrong type.3221# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3222# this server or project.3223# 404: Requested object/revision/project doesn't exist.3224# 500: The server isn't configured properly, or3225# an internal error occurred (e.g. failed assertions caused by bugs), or3226# an unknown error occurred (e.g. the git binary died unexpectedly).3227sub die_error {3228my$status=shift||500;3229my$error=shift||"Internal server error";32303231my%http_responses= (400=>'400 Bad Request',3232403=>'403 Forbidden',3233404=>'404 Not Found',3234500=>'500 Internal Server Error');3235 git_header_html($http_responses{$status});3236print<<EOF;3237<div class="page_body">3238<br /><br />3239$status-$error3240<br />3241</div>3242EOF3243 git_footer_html();3244exit;3245}32463247## ----------------------------------------------------------------------3248## functions printing or outputting HTML: navigation32493250sub git_print_page_nav {3251my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3252$extra=''if!defined$extra;# pager or formats32533254my@navs=qw(summary shortlog log commit commitdiff tree);3255if($suppress) {3256@navs=grep{$_ne$suppress}@navs;3257}32583259my%arg=map{$_=> {action=>$_} }@navs;3260if(defined$head) {3261for(qw(commit commitdiff)) {3262$arg{$_}{'hash'} =$head;3263}3264if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3265for(qw(shortlog log)) {3266$arg{$_}{'hash'} =$head;3267}3268}3269}32703271$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3272$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;32733274my@actions= gitweb_get_feature('actions');3275my%repl= (3276'%'=>'%',3277'n'=>$project,# project name3278'f'=>$git_dir,# project path within filesystem3279'h'=>$treehead||'',# current hash ('h' parameter)3280'b'=>$treebase||'',# hash base ('hb' parameter)3281);3282while(@actions) {3283my($label,$link,$pos) =splice(@actions,0,3);3284# insert3285@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3286# munch munch3287$link=~s/%([%nfhb])/$repl{$1}/g;3288$arg{$label}{'_href'} =$link;3289}32903291print"<div class=\"page_nav\">\n".3292(join" | ",3293map{$_eq$current?3294$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3295}@navs);3296print"<br/>\n$extra<br/>\n".3297"</div>\n";3298}32993300sub format_paging_nav {3301my($action,$hash,$head,$page,$has_next_link) =@_;3302my$paging_nav;330333043305if($hashne$head||$page) {3306$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3307}else{3308$paging_nav.="HEAD";3309}33103311if($page>0) {3312$paging_nav.=" ⋅ ".3313$cgi->a({-href => href(-replay=>1, page=>$page-1),3314-accesskey =>"p", -title =>"Alt-p"},"prev");3315}else{3316$paging_nav.=" ⋅ prev";3317}33183319if($has_next_link) {3320$paging_nav.=" ⋅ ".3321$cgi->a({-href => href(-replay=>1, page=>$page+1),3322-accesskey =>"n", -title =>"Alt-n"},"next");3323}else{3324$paging_nav.=" ⋅ next";3325}33263327return$paging_nav;3328}33293330## ......................................................................3331## functions printing or outputting HTML: div33323333sub git_print_header_div {3334my($action,$title,$hash,$hash_base) =@_;3335my%args= ();33363337$args{'action'} =$action;3338$args{'hash'} =$hashif$hash;3339$args{'hash_base'} =$hash_baseif$hash_base;33403341print"<div class=\"header\">\n".3342$cgi->a({-href => href(%args), -class=>"title"},3343$title?$title:$action) .3344"\n</div>\n";3345}33463347sub print_local_time {3348my%date=@_;3349if($date{'hour_local'} <6) {3350printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3351$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3352}else{3353printf(" (%02d:%02d%s)",3354$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3355}3356}33573358# Outputs the author name and date in long form3359sub git_print_authorship {3360my$co=shift;3361my%opts=@_;3362my$tag=$opts{-tag} ||'div';33633364my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3365print"<$tagclass=\"author_date\">".3366 esc_html($co->{'author_name'}) .3367" [$ad{'rfc2822'}";3368 print_local_time(%ad)if($opts{-localtime});3369print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3370."</$tag>\n";3371}33723373# Outputs table rows containing the full author or committer information,3374# in the format expected for 'commit' view (& similia).3375# Parameters are a commit hash reference, followed by the list of people3376# to output information for. If the list is empty it defalts to both3377# author and committer.3378sub git_print_authorship_rows {3379my$co=shift;3380# too bad we can't use @people = @_ || ('author', 'committer')3381my@people=@_;3382@people= ('author','committer')unless@people;3383foreachmy$who(@people) {3384my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3385print"<tr><td>$who</td><td>". esc_html($co->{$who}) ."</td>".3386"<td rowspan=\"2\">".3387 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3388"</td></tr>\n".3389"<tr>".3390"<td></td><td>$wd{'rfc2822'}";3391 print_local_time(%wd);3392print"</td>".3393"</tr>\n";3394}3395}33963397sub git_print_page_path {3398my$name=shift;3399my$type=shift;3400my$hb=shift;340134023403print"<div class=\"page_path\">";3404print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3405-title =>'tree root'}, to_utf8("[$project]"));3406print" / ";3407if(defined$name) {3408my@dirname=split'/',$name;3409my$basename=pop@dirname;3410my$fullname='';34113412foreachmy$dir(@dirname) {3413$fullname.= ($fullname?'/':'') .$dir;3414print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3415 hash_base=>$hb),3416-title =>$fullname}, esc_path($dir));3417print" / ";3418}3419if(defined$type&&$typeeq'blob') {3420print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3421 hash_base=>$hb),3422-title =>$name}, esc_path($basename));3423}elsif(defined$type&&$typeeq'tree') {3424print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3425 hash_base=>$hb),3426-title =>$name}, esc_path($basename));3427print" / ";3428}else{3429print esc_path($basename);3430}3431}3432print"<br/></div>\n";3433}34343435sub git_print_log {3436my$log=shift;3437my%opts=@_;34383439if($opts{'-remove_title'}) {3440# remove title, i.e. first line of log3441shift@$log;3442}3443# remove leading empty lines3444while(defined$log->[0] &&$log->[0]eq"") {3445shift@$log;3446}34473448# print log3449my$signoff=0;3450my$empty=0;3451foreachmy$line(@$log) {3452if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3453$signoff=1;3454$empty=0;3455if(!$opts{'-remove_signoff'}) {3456print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3457next;3458}else{3459# remove signoff lines3460next;3461}3462}else{3463$signoff=0;3464}34653466# print only one empty line3467# do not print empty line after signoff3468if($lineeq"") {3469next if($empty||$signoff);3470$empty=1;3471}else{3472$empty=0;3473}34743475print format_log_line_html($line) ."<br/>\n";3476}34773478if($opts{'-final_empty_line'}) {3479# end with single empty line3480print"<br/>\n"unless$empty;3481}3482}34833484# return link target (what link points to)3485sub git_get_link_target {3486my$hash=shift;3487my$link_target;34883489# read link3490open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3491orreturn;3492{3493local$/=undef;3494$link_target= <$fd>;3495}3496close$fd3497orreturn;34983499return$link_target;3500}35013502# given link target, and the directory (basedir) the link is in,3503# return target of link relative to top directory (top tree);3504# return undef if it is not possible (including absolute links).3505sub normalize_link_target {3506my($link_target,$basedir) =@_;35073508# absolute symlinks (beginning with '/') cannot be normalized3509return if(substr($link_target,0,1)eq'/');35103511# normalize link target to path from top (root) tree (dir)3512my$path;3513if($basedir) {3514$path=$basedir.'/'.$link_target;3515}else{3516# we are in top (root) tree (dir)3517$path=$link_target;3518}35193520# remove //, /./, and /../3521my@path_parts;3522foreachmy$part(split('/',$path)) {3523# discard '.' and ''3524next if(!$part||$parteq'.');3525# handle '..'3526if($parteq'..') {3527if(@path_parts) {3528pop@path_parts;3529}else{3530# link leads outside repository (outside top dir)3531return;3532}3533}else{3534push@path_parts,$part;3535}3536}3537$path=join('/',@path_parts);35383539return$path;3540}35413542# print tree entry (row of git_tree), but without encompassing <tr> element3543sub git_print_tree_entry {3544my($t,$basedir,$hash_base,$have_blame) =@_;35453546my%base_key= ();3547$base_key{'hash_base'} =$hash_baseifdefined$hash_base;35483549# The format of a table row is: mode list link. Where mode is3550# the mode of the entry, list is the name of the entry, an href,3551# and link is the action links of the entry.35523553print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3554if($t->{'type'}eq"blob") {3555print"<td class=\"list\">".3556$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3557 file_name=>"$basedir$t->{'name'}",%base_key),3558-class=>"list"}, esc_path($t->{'name'}));3559if(S_ISLNK(oct$t->{'mode'})) {3560my$link_target= git_get_link_target($t->{'hash'});3561if($link_target) {3562my$norm_target= normalize_link_target($link_target,$basedir);3563if(defined$norm_target) {3564print" -> ".3565$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3566 file_name=>$norm_target),3567-title =>$norm_target}, esc_path($link_target));3568}else{3569print" -> ". esc_path($link_target);3570}3571}3572}3573print"</td>\n";3574print"<td class=\"link\">";3575print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3576 file_name=>"$basedir$t->{'name'}",%base_key)},3577"blob");3578if($have_blame) {3579print" | ".3580$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3581 file_name=>"$basedir$t->{'name'}",%base_key)},3582"blame");3583}3584if(defined$hash_base) {3585print" | ".3586$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3587 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3588"history");3589}3590print" | ".3591$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3592 file_name=>"$basedir$t->{'name'}")},3593"raw");3594print"</td>\n";35953596}elsif($t->{'type'}eq"tree") {3597print"<td class=\"list\">";3598print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3599 file_name=>"$basedir$t->{'name'}",%base_key)},3600 esc_path($t->{'name'}));3601print"</td>\n";3602print"<td class=\"link\">";3603print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3604 file_name=>"$basedir$t->{'name'}",%base_key)},3605"tree");3606if(defined$hash_base) {3607print" | ".3608$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3609 file_name=>"$basedir$t->{'name'}")},3610"history");3611}3612print"</td>\n";3613}else{3614# unknown object: we can only present history for it3615# (this includes 'commit' object, i.e. submodule support)3616print"<td class=\"list\">".3617 esc_path($t->{'name'}) .3618"</td>\n";3619print"<td class=\"link\">";3620if(defined$hash_base) {3621print$cgi->a({-href => href(action=>"history",3622 hash_base=>$hash_base,3623 file_name=>"$basedir$t->{'name'}")},3624"history");3625}3626print"</td>\n";3627}3628}36293630## ......................................................................3631## functions printing large fragments of HTML36323633# get pre-image filenames for merge (combined) diff3634sub fill_from_file_info {3635my($diff,@parents) =@_;36363637$diff->{'from_file'} = [ ];3638$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3639for(my$i=0;$i<$diff->{'nparents'};$i++) {3640if($diff->{'status'}[$i]eq'R'||3641$diff->{'status'}[$i]eq'C') {3642$diff->{'from_file'}[$i] =3643 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3644}3645}36463647return$diff;3648}36493650# is current raw difftree line of file deletion3651sub is_deleted {3652my$diffinfo=shift;36533654return$diffinfo->{'to_id'}eq('0' x 40);3655}36563657# does patch correspond to [previous] difftree raw line3658# $diffinfo - hashref of parsed raw diff format3659# $patchinfo - hashref of parsed patch diff format3660# (the same keys as in $diffinfo)3661sub is_patch_split {3662my($diffinfo,$patchinfo) =@_;36633664returndefined$diffinfo&&defined$patchinfo3665&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3666}366736683669sub git_difftree_body {3670my($difftree,$hash,@parents) =@_;3671my($parent) =$parents[0];3672my$have_blame= gitweb_check_feature('blame');3673print"<div class=\"list_head\">\n";3674if($#{$difftree} >10) {3675print(($#{$difftree} +1) ." files changed:\n");3676}3677print"</div>\n";36783679print"<table class=\"".3680(@parents>1?"combined ":"") .3681"diff_tree\">\n";36823683# header only for combined diff in 'commitdiff' view3684my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3685if($has_header) {3686# table header3687print"<thead><tr>\n".3688"<th></th><th></th>\n";# filename, patchN link3689for(my$i=0;$i<@parents;$i++) {3690my$par=$parents[$i];3691print"<th>".3692$cgi->a({-href => href(action=>"commitdiff",3693 hash=>$hash, hash_parent=>$par),3694-title =>'commitdiff to parent number '.3695($i+1) .': '.substr($par,0,7)},3696$i+1) .3697" </th>\n";3698}3699print"</tr></thead>\n<tbody>\n";3700}37013702my$alternate=1;3703my$patchno=0;3704foreachmy$line(@{$difftree}) {3705my$diff= parsed_difftree_line($line);37063707if($alternate) {3708print"<tr class=\"dark\">\n";3709}else{3710print"<tr class=\"light\">\n";3711}3712$alternate^=1;37133714if(exists$diff->{'nparents'}) {# combined diff37153716 fill_from_file_info($diff,@parents)3717unlessexists$diff->{'from_file'};37183719if(!is_deleted($diff)) {3720# file exists in the result (child) commit3721print"<td>".3722$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3723 file_name=>$diff->{'to_file'},3724 hash_base=>$hash),3725-class=>"list"}, esc_path($diff->{'to_file'})) .3726"</td>\n";3727}else{3728print"<td>".3729 esc_path($diff->{'to_file'}) .3730"</td>\n";3731}37323733if($actioneq'commitdiff') {3734# link to patch3735$patchno++;3736print"<td class=\"link\">".3737$cgi->a({-href =>"#patch$patchno"},"patch") .3738" | ".3739"</td>\n";3740}37413742my$has_history=0;3743my$not_deleted=0;3744for(my$i=0;$i<$diff->{'nparents'};$i++) {3745my$hash_parent=$parents[$i];3746my$from_hash=$diff->{'from_id'}[$i];3747my$from_path=$diff->{'from_file'}[$i];3748my$status=$diff->{'status'}[$i];37493750$has_history||= ($statusne'A');3751$not_deleted||= ($statusne'D');37523753if($statuseq'A') {3754print"<td class=\"link\"align=\"right\"> | </td>\n";3755}elsif($statuseq'D') {3756print"<td class=\"link\">".3757$cgi->a({-href => href(action=>"blob",3758 hash_base=>$hash,3759 hash=>$from_hash,3760 file_name=>$from_path)},3761"blob". ($i+1)) .3762" | </td>\n";3763}else{3764if($diff->{'to_id'}eq$from_hash) {3765print"<td class=\"link nochange\">";3766}else{3767print"<td class=\"link\">";3768}3769print$cgi->a({-href => href(action=>"blobdiff",3770 hash=>$diff->{'to_id'},3771 hash_parent=>$from_hash,3772 hash_base=>$hash,3773 hash_parent_base=>$hash_parent,3774 file_name=>$diff->{'to_file'},3775 file_parent=>$from_path)},3776"diff". ($i+1)) .3777" | </td>\n";3778}3779}37803781print"<td class=\"link\">";3782if($not_deleted) {3783print$cgi->a({-href => href(action=>"blob",3784 hash=>$diff->{'to_id'},3785 file_name=>$diff->{'to_file'},3786 hash_base=>$hash)},3787"blob");3788print" | "if($has_history);3789}3790if($has_history) {3791print$cgi->a({-href => href(action=>"history",3792 file_name=>$diff->{'to_file'},3793 hash_base=>$hash)},3794"history");3795}3796print"</td>\n";37973798print"</tr>\n";3799next;# instead of 'else' clause, to avoid extra indent3800}3801# else ordinary diff38023803my($to_mode_oct,$to_mode_str,$to_file_type);3804my($from_mode_oct,$from_mode_str,$from_file_type);3805if($diff->{'to_mode'}ne('0' x 6)) {3806$to_mode_oct=oct$diff->{'to_mode'};3807if(S_ISREG($to_mode_oct)) {# only for regular file3808$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3809}3810$to_file_type= file_type($diff->{'to_mode'});3811}3812if($diff->{'from_mode'}ne('0' x 6)) {3813$from_mode_oct=oct$diff->{'from_mode'};3814if(S_ISREG($to_mode_oct)) {# only for regular file3815$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3816}3817$from_file_type= file_type($diff->{'from_mode'});3818}38193820if($diff->{'status'}eq"A") {# created3821my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3822$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3823$mode_chng.="]</span>";3824print"<td>";3825print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3826 hash_base=>$hash, file_name=>$diff->{'file'}),3827-class=>"list"}, esc_path($diff->{'file'}));3828print"</td>\n";3829print"<td>$mode_chng</td>\n";3830print"<td class=\"link\">";3831if($actioneq'commitdiff') {3832# link to patch3833$patchno++;3834print$cgi->a({-href =>"#patch$patchno"},"patch");3835print" | ";3836}3837print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3838 hash_base=>$hash, file_name=>$diff->{'file'})},3839"blob");3840print"</td>\n";38413842}elsif($diff->{'status'}eq"D") {# deleted3843my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3844print"<td>";3845print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3846 hash_base=>$parent, file_name=>$diff->{'file'}),3847-class=>"list"}, esc_path($diff->{'file'}));3848print"</td>\n";3849print"<td>$mode_chng</td>\n";3850print"<td class=\"link\">";3851if($actioneq'commitdiff') {3852# link to patch3853$patchno++;3854print$cgi->a({-href =>"#patch$patchno"},"patch");3855print" | ";3856}3857print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3858 hash_base=>$parent, file_name=>$diff->{'file'})},3859"blob") ." | ";3860if($have_blame) {3861print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3862 file_name=>$diff->{'file'})},3863"blame") ." | ";3864}3865print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3866 file_name=>$diff->{'file'})},3867"history");3868print"</td>\n";38693870}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3871my$mode_chnge="";3872if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3873$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3874if($from_file_typene$to_file_type) {3875$mode_chnge.=" from$from_file_typeto$to_file_type";3876}3877if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3878if($from_mode_str&&$to_mode_str) {3879$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3880}elsif($to_mode_str) {3881$mode_chnge.=" mode:$to_mode_str";3882}3883}3884$mode_chnge.="]</span>\n";3885}3886print"<td>";3887print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3888 hash_base=>$hash, file_name=>$diff->{'file'}),3889-class=>"list"}, esc_path($diff->{'file'}));3890print"</td>\n";3891print"<td>$mode_chnge</td>\n";3892print"<td class=\"link\">";3893if($actioneq'commitdiff') {3894# link to patch3895$patchno++;3896print$cgi->a({-href =>"#patch$patchno"},"patch") .3897" | ";3898}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3899# "commit" view and modified file (not onlu mode changed)3900print$cgi->a({-href => href(action=>"blobdiff",3901 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3902 hash_base=>$hash, hash_parent_base=>$parent,3903 file_name=>$diff->{'file'})},3904"diff") .3905" | ";3906}3907print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3908 hash_base=>$hash, file_name=>$diff->{'file'})},3909"blob") ." | ";3910if($have_blame) {3911print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3912 file_name=>$diff->{'file'})},3913"blame") ." | ";3914}3915print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3916 file_name=>$diff->{'file'})},3917"history");3918print"</td>\n";39193920}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3921my%status_name= ('R'=>'moved','C'=>'copied');3922my$nstatus=$status_name{$diff->{'status'}};3923my$mode_chng="";3924if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3925# mode also for directories, so we cannot use $to_mode_str3926$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3927}3928print"<td>".3929$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3930 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3931-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3932"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3933$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3934 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3935-class=>"list"}, esc_path($diff->{'from_file'})) .3936" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3937"<td class=\"link\">";3938if($actioneq'commitdiff') {3939# link to patch3940$patchno++;3941print$cgi->a({-href =>"#patch$patchno"},"patch") .3942" | ";3943}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3944# "commit" view and modified file (not only pure rename or copy)3945print$cgi->a({-href => href(action=>"blobdiff",3946 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3947 hash_base=>$hash, hash_parent_base=>$parent,3948 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3949"diff") .3950" | ";3951}3952print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3953 hash_base=>$parent, file_name=>$diff->{'to_file'})},3954"blob") ." | ";3955if($have_blame) {3956print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3957 file_name=>$diff->{'to_file'})},3958"blame") ." | ";3959}3960print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3961 file_name=>$diff->{'to_file'})},3962"history");3963print"</td>\n";39643965}# we should not encounter Unmerged (U) or Unknown (X) status3966print"</tr>\n";3967}3968print"</tbody>"if$has_header;3969print"</table>\n";3970}39713972sub git_patchset_body {3973my($fd,$difftree,$hash,@hash_parents) =@_;3974my($hash_parent) =$hash_parents[0];39753976my$is_combined= (@hash_parents>1);3977my$patch_idx=0;3978my$patch_number=0;3979my$patch_line;3980my$diffinfo;3981my$to_name;3982my(%from,%to);39833984print"<div class=\"patchset\">\n";39853986# skip to first patch3987while($patch_line= <$fd>) {3988chomp$patch_line;39893990last if($patch_line=~m/^diff /);3991}39923993 PATCH:3994while($patch_line) {39953996# parse "git diff" header line3997if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3998# $1 is from_name, which we do not use3999$to_name= unquote($2);4000$to_name=~s!^b/!!;4001}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4002# $1 is 'cc' or 'combined', which we do not use4003$to_name= unquote($2);4004}else{4005$to_name=undef;4006}40074008# check if current patch belong to current raw line4009# and parse raw git-diff line if needed4010if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4011# this is continuation of a split patch4012print"<div class=\"patch cont\">\n";4013}else{4014# advance raw git-diff output if needed4015$patch_idx++ifdefined$diffinfo;40164017# read and prepare patch information4018$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);40194020# compact combined diff output can have some patches skipped4021# find which patch (using pathname of result) we are at now;4022if($is_combined) {4023while($to_namene$diffinfo->{'to_file'}) {4024print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4025 format_diff_cc_simplified($diffinfo,@hash_parents) .4026"</div>\n";# class="patch"40274028$patch_idx++;4029$patch_number++;40304031last if$patch_idx>$#$difftree;4032$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4033}4034}40354036# modifies %from, %to hashes4037 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);40384039# this is first patch for raw difftree line with $patch_idx index4040# we index @$difftree array from 0, but number patches from 14041print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4042}40434044# git diff header4045#assert($patch_line =~ m/^diff /) if DEBUG;4046#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4047$patch_number++;4048# print "git diff" header4049print format_git_diff_header_line($patch_line,$diffinfo,4050 \%from, \%to);40514052# print extended diff header4053print"<div class=\"diff extended_header\">\n";4054 EXTENDED_HEADER:4055while($patch_line= <$fd>) {4056chomp$patch_line;40574058last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);40594060print format_extended_diff_header_line($patch_line,$diffinfo,4061 \%from, \%to);4062}4063print"</div>\n";# class="diff extended_header"40644065# from-file/to-file diff header4066if(!$patch_line) {4067print"</div>\n";# class="patch"4068last PATCH;4069}4070next PATCH if($patch_line=~m/^diff /);4071#assert($patch_line =~ m/^---/) if DEBUG;40724073my$last_patch_line=$patch_line;4074$patch_line= <$fd>;4075chomp$patch_line;4076#assert($patch_line =~ m/^\+\+\+/) if DEBUG;40774078print format_diff_from_to_header($last_patch_line,$patch_line,4079$diffinfo, \%from, \%to,4080@hash_parents);40814082# the patch itself4083 LINE:4084while($patch_line= <$fd>) {4085chomp$patch_line;40864087next PATCH if($patch_line=~m/^diff /);40884089print format_diff_line($patch_line, \%from, \%to);4090}40914092}continue{4093print"</div>\n";# class="patch"4094}40954096# for compact combined (--cc) format, with chunk and patch simpliciaction4097# patchset might be empty, but there might be unprocessed raw lines4098for(++$patch_idxif$patch_number>0;4099$patch_idx<@$difftree;4100++$patch_idx) {4101# read and prepare patch information4102$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);41034104# generate anchor for "patch" links in difftree / whatchanged part4105print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4106 format_diff_cc_simplified($diffinfo,@hash_parents) .4107"</div>\n";# class="patch"41084109$patch_number++;4110}41114112if($patch_number==0) {4113if(@hash_parents>1) {4114print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4115}else{4116print"<div class=\"diff nodifferences\">No differences found</div>\n";4117}4118}41194120print"</div>\n";# class="patchset"4121}41224123# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41244125# fills project list info (age, description, owner, forks) for each4126# project in the list, removing invalid projects from returned list4127# NOTE: modifies $projlist, but does not remove entries from it4128sub fill_project_list_info {4129my($projlist,$check_forks) =@_;4130my@projects;41314132my$show_ctags= gitweb_check_feature('ctags');4133 PROJECT:4134foreachmy$pr(@$projlist) {4135my(@activity) = git_get_last_activity($pr->{'path'});4136unless(@activity) {4137next PROJECT;4138}4139($pr->{'age'},$pr->{'age_string'}) =@activity;4140if(!defined$pr->{'descr'}) {4141my$descr= git_get_project_description($pr->{'path'}) ||"";4142$descr= to_utf8($descr);4143$pr->{'descr_long'} =$descr;4144$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4145}4146if(!defined$pr->{'owner'}) {4147$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4148}4149if($check_forks) {4150my$pname=$pr->{'path'};4151if(($pname=~s/\.git$//) &&4152($pname!~/\/$/) &&4153(-d "$projectroot/$pname")) {4154$pr->{'forks'} ="-d$projectroot/$pname";4155}else{4156$pr->{'forks'} =0;4157}4158}4159$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4160push@projects,$pr;4161}41624163return@projects;4164}41654166# print 'sort by' <th> element, generating 'sort by $name' replay link4167# if that order is not selected4168sub print_sort_th {4169my($name,$order,$header) =@_;4170$header||=ucfirst($name);41714172if($ordereq$name) {4173print"<th>$header</th>\n";4174}else{4175print"<th>".4176$cgi->a({-href => href(-replay=>1, order=>$name),4177-class=>"header"},$header) .4178"</th>\n";4179}4180}41814182sub git_project_list_body {4183# actually uses global variable $project4184my($projlist,$order,$from,$to,$extra,$no_header) =@_;41854186my$check_forks= gitweb_check_feature('forks');4187my@projects= fill_project_list_info($projlist,$check_forks);41884189$order||=$default_projects_order;4190$from=0unlessdefined$from;4191$to=$#projectsif(!defined$to||$#projects<$to);41924193my%order_info= (4194 project => { key =>'path', type =>'str'},4195 descr => { key =>'descr_long', type =>'str'},4196 owner => { key =>'owner', type =>'str'},4197 age => { key =>'age', type =>'num'}4198);4199my$oi=$order_info{$order};4200if($oi->{'type'}eq'str') {4201@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4202}else{4203@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4204}42054206my$show_ctags= gitweb_check_feature('ctags');4207if($show_ctags) {4208my%ctags;4209foreachmy$p(@projects) {4210foreachmy$ct(keys%{$p->{'ctags'}}) {4211$ctags{$ct} +=$p->{'ctags'}->{$ct};4212}4213}4214my$cloud= git_populate_project_tagcloud(\%ctags);4215print git_show_project_tagcloud($cloud,64);4216}42174218print"<table class=\"project_list\">\n";4219unless($no_header) {4220print"<tr>\n";4221if($check_forks) {4222print"<th></th>\n";4223}4224 print_sort_th('project',$order,'Project');4225 print_sort_th('descr',$order,'Description');4226 print_sort_th('owner',$order,'Owner');4227 print_sort_th('age',$order,'Last Change');4228print"<th></th>\n".# for links4229"</tr>\n";4230}4231my$alternate=1;4232my$tagfilter=$cgi->param('by_tag');4233for(my$i=$from;$i<=$to;$i++) {4234my$pr=$projects[$i];42354236next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4237next if$searchtextand not$pr->{'path'} =~/$searchtext/4238and not$pr->{'descr_long'} =~/$searchtext/;4239# Weed out forks or non-matching entries of search4240if($check_forks) {4241my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4242$forkbase="^$forkbase"if$forkbase;4243next ifnot$searchtextand not$tagfilterand$show_ctags4244and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4245}42464247if($alternate) {4248print"<tr class=\"dark\">\n";4249}else{4250print"<tr class=\"light\">\n";4251}4252$alternate^=1;4253if($check_forks) {4254print"<td>";4255if($pr->{'forks'}) {4256print"<!--$pr->{'forks'} -->\n";4257print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4258}4259print"</td>\n";4260}4261print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4262-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4263"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4264-class=>"list", -title =>$pr->{'descr_long'}},4265 esc_html($pr->{'descr'})) ."</td>\n".4266"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4267print"<td class=\"". age_class($pr->{'age'}) ."\">".4268(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4269"<td class=\"link\">".4270$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4271$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4272$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4273$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4274($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4275"</td>\n".4276"</tr>\n";4277}4278if(defined$extra) {4279print"<tr>\n";4280if($check_forks) {4281print"<td></td>\n";4282}4283print"<td colspan=\"5\">$extra</td>\n".4284"</tr>\n";4285}4286print"</table>\n";4287}42884289sub git_shortlog_body {4290# uses global variable $project4291my($commitlist,$from,$to,$refs,$extra) =@_;42924293$from=0unlessdefined$from;4294$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);42954296print"<table class=\"shortlog\">\n";4297my$alternate=1;4298for(my$i=$from;$i<=$to;$i++) {4299my%co= %{$commitlist->[$i]};4300my$commit=$co{'id'};4301my$ref= format_ref_marker($refs,$commit);4302if($alternate) {4303print"<tr class=\"dark\">\n";4304}else{4305print"<tr class=\"light\">\n";4306}4307$alternate^=1;4308# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4309print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4310 format_author_html('td', \%co,10) ."<td>";4311print format_subject_html($co{'title'},$co{'title_short'},4312 href(action=>"commit", hash=>$commit),$ref);4313print"</td>\n".4314"<td class=\"link\">".4315$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4316$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4317$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4318my$snapshot_links= format_snapshot_links($commit);4319if(defined$snapshot_links) {4320print" | ".$snapshot_links;4321}4322print"</td>\n".4323"</tr>\n";4324}4325if(defined$extra) {4326print"<tr>\n".4327"<td colspan=\"4\">$extra</td>\n".4328"</tr>\n";4329}4330print"</table>\n";4331}43324333sub git_history_body {4334# Warning: assumes constant type (blob or tree) during history4335my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;43364337$from=0unlessdefined$from;4338$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});43394340print"<table class=\"history\">\n";4341my$alternate=1;4342for(my$i=$from;$i<=$to;$i++) {4343my%co= %{$commitlist->[$i]};4344if(!%co) {4345next;4346}4347my$commit=$co{'id'};43484349my$ref= format_ref_marker($refs,$commit);43504351if($alternate) {4352print"<tr class=\"dark\">\n";4353}else{4354print"<tr class=\"light\">\n";4355}4356$alternate^=1;4357print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4358# shortlog: format_author_html('td', \%co, 10)4359 format_author_html('td', \%co,15,3) ."<td>";4360# originally git_history used chop_str($co{'title'}, 50)4361print format_subject_html($co{'title'},$co{'title_short'},4362 href(action=>"commit", hash=>$commit),$ref);4363print"</td>\n".4364"<td class=\"link\">".4365$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4366$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");43674368if($ftypeeq'blob') {4369my$blob_current= git_get_hash_by_path($hash_base,$file_name);4370my$blob_parent= git_get_hash_by_path($commit,$file_name);4371if(defined$blob_current&&defined$blob_parent&&4372$blob_currentne$blob_parent) {4373print" | ".4374$cgi->a({-href => href(action=>"blobdiff",4375 hash=>$blob_current, hash_parent=>$blob_parent,4376 hash_base=>$hash_base, hash_parent_base=>$commit,4377 file_name=>$file_name)},4378"diff to current");4379}4380}4381print"</td>\n".4382"</tr>\n";4383}4384if(defined$extra) {4385print"<tr>\n".4386"<td colspan=\"4\">$extra</td>\n".4387"</tr>\n";4388}4389print"</table>\n";4390}43914392sub git_tags_body {4393# uses global variable $project4394my($taglist,$from,$to,$extra) =@_;4395$from=0unlessdefined$from;4396$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);43974398print"<table class=\"tags\">\n";4399my$alternate=1;4400for(my$i=$from;$i<=$to;$i++) {4401my$entry=$taglist->[$i];4402my%tag=%$entry;4403my$comment=$tag{'subject'};4404my$comment_short;4405if(defined$comment) {4406$comment_short= chop_str($comment,30,5);4407}4408if($alternate) {4409print"<tr class=\"dark\">\n";4410}else{4411print"<tr class=\"light\">\n";4412}4413$alternate^=1;4414if(defined$tag{'age'}) {4415print"<td><i>$tag{'age'}</i></td>\n";4416}else{4417print"<td></td>\n";4418}4419print"<td>".4420$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4421-class=>"list name"}, esc_html($tag{'name'})) .4422"</td>\n".4423"<td>";4424if(defined$comment) {4425print format_subject_html($comment,$comment_short,4426 href(action=>"tag", hash=>$tag{'id'}));4427}4428print"</td>\n".4429"<td class=\"selflink\">";4430if($tag{'type'}eq"tag") {4431print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4432}else{4433print" ";4434}4435print"</td>\n".4436"<td class=\"link\">"." | ".4437$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4438if($tag{'reftype'}eq"commit") {4439print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4440" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4441}elsif($tag{'reftype'}eq"blob") {4442print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4443}4444print"</td>\n".4445"</tr>";4446}4447if(defined$extra) {4448print"<tr>\n".4449"<td colspan=\"5\">$extra</td>\n".4450"</tr>\n";4451}4452print"</table>\n";4453}44544455sub git_heads_body {4456# uses global variable $project4457my($headlist,$head,$from,$to,$extra) =@_;4458$from=0unlessdefined$from;4459$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);44604461print"<table class=\"heads\">\n";4462my$alternate=1;4463for(my$i=$from;$i<=$to;$i++) {4464my$entry=$headlist->[$i];4465my%ref=%$entry;4466my$curr=$ref{'id'}eq$head;4467if($alternate) {4468print"<tr class=\"dark\">\n";4469}else{4470print"<tr class=\"light\">\n";4471}4472$alternate^=1;4473print"<td><i>$ref{'age'}</i></td>\n".4474($curr?"<td class=\"current_head\">":"<td>") .4475$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4476-class=>"list name"},esc_html($ref{'name'})) .4477"</td>\n".4478"<td class=\"link\">".4479$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4480$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4481$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4482"</td>\n".4483"</tr>";4484}4485if(defined$extra) {4486print"<tr>\n".4487"<td colspan=\"3\">$extra</td>\n".4488"</tr>\n";4489}4490print"</table>\n";4491}44924493sub git_search_grep_body {4494my($commitlist,$from,$to,$extra) =@_;4495$from=0unlessdefined$from;4496$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);44974498print"<table class=\"commit_search\">\n";4499my$alternate=1;4500for(my$i=$from;$i<=$to;$i++) {4501my%co= %{$commitlist->[$i]};4502if(!%co) {4503next;4504}4505my$commit=$co{'id'};4506if($alternate) {4507print"<tr class=\"dark\">\n";4508}else{4509print"<tr class=\"light\">\n";4510}4511$alternate^=1;4512print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4513 format_author_html('td', \%co,15,5) .4514"<td>".4515$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4516-class=>"list subject"},4517 chop_and_escape_str($co{'title'},50) ."<br/>");4518my$comment=$co{'comment'};4519foreachmy$line(@$comment) {4520if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4521my($lead,$match,$trail) = ($1,$2,$3);4522$match= chop_str($match,70,5,'center');4523my$contextlen=int((80-length($match))/2);4524$contextlen=30if($contextlen>30);4525$lead= chop_str($lead,$contextlen,10,'left');4526$trail= chop_str($trail,$contextlen,10,'right');45274528$lead= esc_html($lead);4529$match= esc_html($match);4530$trail= esc_html($trail);45314532print"$lead<span class=\"match\">$match</span>$trail<br />";4533}4534}4535print"</td>\n".4536"<td class=\"link\">".4537$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4538" | ".4539$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4540" | ".4541$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4542print"</td>\n".4543"</tr>\n";4544}4545if(defined$extra) {4546print"<tr>\n".4547"<td colspan=\"3\">$extra</td>\n".4548"</tr>\n";4549}4550print"</table>\n";4551}45524553## ======================================================================4554## ======================================================================4555## actions45564557sub git_project_list {4558my$order=$input_params{'order'};4559if(defined$order&&$order!~m/none|project|descr|owner|age/) {4560 die_error(400,"Unknown order parameter");4561}45624563my@list= git_get_projects_list();4564if(!@list) {4565 die_error(404,"No projects found");4566}45674568 git_header_html();4569if(-f $home_text) {4570print"<div class=\"index_include\">\n";4571 insert_file($home_text);4572print"</div>\n";4573}4574print$cgi->startform(-method=>"get") .4575"<p class=\"projsearch\">Search:\n".4576$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4577"</p>".4578$cgi->end_form() ."\n";4579 git_project_list_body(\@list,$order);4580 git_footer_html();4581}45824583sub git_forks {4584my$order=$input_params{'order'};4585if(defined$order&&$order!~m/none|project|descr|owner|age/) {4586 die_error(400,"Unknown order parameter");4587}45884589my@list= git_get_projects_list($project);4590if(!@list) {4591 die_error(404,"No forks found");4592}45934594 git_header_html();4595 git_print_page_nav('','');4596 git_print_header_div('summary',"$projectforks");4597 git_project_list_body(\@list,$order);4598 git_footer_html();4599}46004601sub git_project_index {4602my@projects= git_get_projects_list($project);46034604print$cgi->header(4605-type =>'text/plain',4606-charset =>'utf-8',4607-content_disposition =>'inline; filename="index.aux"');46084609foreachmy$pr(@projects) {4610if(!exists$pr->{'owner'}) {4611$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4612}46134614my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4615# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4616$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4617$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4618$path=~s/ /\+/g;4619$owner=~s/ /\+/g;46204621print"$path$owner\n";4622}4623}46244625sub git_summary {4626my$descr= git_get_project_description($project) ||"none";4627my%co= parse_commit("HEAD");4628my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4629my$head=$co{'id'};46304631my$owner= git_get_project_owner($project);46324633my$refs= git_get_references();4634# These get_*_list functions return one more to allow us to see if4635# there are more ...4636my@taglist= git_get_tags_list(16);4637my@headlist= git_get_heads_list(16);4638my@forklist;4639my$check_forks= gitweb_check_feature('forks');46404641if($check_forks) {4642@forklist= git_get_projects_list($project);4643}46444645 git_header_html();4646 git_print_page_nav('summary','',$head);46474648print"<div class=\"title\"> </div>\n";4649print"<table class=\"projects_list\">\n".4650"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4651"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4652if(defined$cd{'rfc2822'}) {4653print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4654}46554656# use per project git URL list in $projectroot/$project/cloneurl4657# or make project git URL from git base URL and project name4658my$url_tag="URL";4659my@url_list= git_get_project_url_list($project);4660@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4661foreachmy$git_url(@url_list) {4662next unless$git_url;4663print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4664$url_tag="";4665}46664667# Tag cloud4668my$show_ctags= gitweb_check_feature('ctags');4669if($show_ctags) {4670my$ctags= git_get_project_ctags($project);4671my$cloud= git_populate_project_tagcloud($ctags);4672print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4673print"</td>\n<td>"unless%$ctags;4674print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4675print"</td>\n<td>"if%$ctags;4676print git_show_project_tagcloud($cloud,48);4677print"</td></tr>";4678}46794680print"</table>\n";46814682# If XSS prevention is on, we don't include README.html.4683# TODO: Allow a readme in some safe format.4684if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4685print"<div class=\"title\">readme</div>\n".4686"<div class=\"readme\">\n";4687 insert_file("$projectroot/$project/README.html");4688print"\n</div>\n";# class="readme"4689}46904691# we need to request one more than 16 (0..15) to check if4692# those 16 are all4693my@commitlist=$head? parse_commits($head,17) : ();4694if(@commitlist) {4695 git_print_header_div('shortlog');4696 git_shortlog_body(\@commitlist,0,15,$refs,4697$#commitlist<=15?undef:4698$cgi->a({-href => href(action=>"shortlog")},"..."));4699}47004701if(@taglist) {4702 git_print_header_div('tags');4703 git_tags_body(\@taglist,0,15,4704$#taglist<=15?undef:4705$cgi->a({-href => href(action=>"tags")},"..."));4706}47074708if(@headlist) {4709 git_print_header_div('heads');4710 git_heads_body(\@headlist,$head,0,15,4711$#headlist<=15?undef:4712$cgi->a({-href => href(action=>"heads")},"..."));4713}47144715if(@forklist) {4716 git_print_header_div('forks');4717 git_project_list_body(\@forklist,'age',0,15,4718$#forklist<=15?undef:4719$cgi->a({-href => href(action=>"forks")},"..."),4720'no_header');4721}47224723 git_footer_html();4724}47254726sub git_tag {4727my$head= git_get_head_hash($project);4728 git_header_html();4729 git_print_page_nav('','',$head,undef,$head);4730my%tag= parse_tag($hash);47314732if(!%tag) {4733 die_error(404,"Unknown tag object");4734}47354736 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4737print"<div class=\"title_text\">\n".4738"<table class=\"object_header\">\n".4739"<tr>\n".4740"<td>object</td>\n".4741"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4742$tag{'object'}) ."</td>\n".4743"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4744$tag{'type'}) ."</td>\n".4745"</tr>\n";4746if(defined($tag{'author'})) {4747 git_print_authorship_rows(\%tag,'author');4748}4749print"</table>\n\n".4750"</div>\n";4751print"<div class=\"page_body\">";4752my$comment=$tag{'comment'};4753foreachmy$line(@$comment) {4754chomp$line;4755print esc_html($line, -nbsp=>1) ."<br/>\n";4756}4757print"</div>\n";4758 git_footer_html();4759}47604761sub git_blame {4762# permissions4763 gitweb_check_feature('blame')4764or die_error(403,"Blame view not allowed");47654766# error checking4767 die_error(400,"No file name given")unless$file_name;4768$hash_base||= git_get_head_hash($project);4769 die_error(404,"Couldn't find base commit")unless$hash_base;4770my%co= parse_commit($hash_base)4771or die_error(404,"Commit not found");4772my$ftype="blob";4773if(!defined$hash) {4774$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4775or die_error(404,"Error looking up file");4776}else{4777$ftype= git_get_type($hash);4778if($ftype!~"blob") {4779 die_error(400,"Object is not a blob");4780}4781}47824783# run git-blame --porcelain4784open my$fd,"-|", git_cmd(),"blame",'-p',4785$hash_base,'--',$file_name4786or die_error(500,"Open git-blame failed");47874788# page header4789 git_header_html();4790my$formats_nav=4791$cgi->a({-href => href(action=>"blob", -replay=>1)},4792"blob") .4793" | ".4794$cgi->a({-href => href(action=>"history", -replay=>1)},4795"history") .4796" | ".4797$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4798"HEAD");4799 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4800 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4801 git_print_page_path($file_name,$ftype,$hash_base);48024803# page body4804my@rev_color=qw(light2 dark2);4805my$num_colors=scalar(@rev_color);4806my$current_color=0;4807my%metainfo= ();48084809print<<HTML;4810<div class="page_body">4811<table class="blame">4812<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4813HTML4814 LINE:4815while(my$line= <$fd>) {4816chomp$line;4817# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4818# no <lines in group> for subsequent lines in group of lines4819my($full_rev,$orig_lineno,$lineno,$group_size) =4820($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4821if(!exists$metainfo{$full_rev}) {4822$metainfo{$full_rev} = {};4823}4824my$meta=$metainfo{$full_rev};4825my$data;4826while($data= <$fd>) {4827chomp$data;4828last if($data=~s/^\t//);# contents of line4829if($data=~/^(\S+) (.*)$/) {4830$meta->{$1} =$2;4831}4832}4833my$short_rev=substr($full_rev,0,8);4834my$author=$meta->{'author'};4835my%date=4836 parse_date($meta->{'author-time'},$meta->{'author-tz'});4837my$date=$date{'iso-tz'};4838if($group_size) {4839$current_color= ($current_color+1) %$num_colors;4840}4841print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4842if($group_size) {4843print"<td class=\"sha1\"";4844print" title=\"". esc_html($author) .",$date\"";4845print" rowspan=\"$group_size\""if($group_size>1);4846print">";4847print$cgi->a({-href => href(action=>"commit",4848 hash=>$full_rev,4849 file_name=>$file_name)},4850 esc_html($short_rev));4851print"</td>\n";4852}4853my$parent_commit;4854if(!exists$meta->{'parent'}) {4855open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4856or die_error(500,"Open git-rev-parse failed");4857$parent_commit= <$dd>;4858close$dd;4859chomp($parent_commit);4860$meta->{'parent'} =$parent_commit;4861}else{4862$parent_commit=$meta->{'parent'};4863}4864my$blamed= href(action =>'blame',4865 file_name =>$meta->{'filename'},4866 hash_base =>$parent_commit);4867print"<td class=\"linenr\">";4868print$cgi->a({ -href =>"$blamed#l$orig_lineno",4869-class=>"linenr"},4870 esc_html($lineno));4871print"</td>";4872print"<td class=\"pre\">". esc_html($data) ."</td>\n";4873print"</tr>\n";4874}4875print"</table>\n";4876print"</div>";4877close$fd4878or print"Reading blob failed\n";48794880# page footer4881 git_footer_html();4882}48834884sub git_tags {4885my$head= git_get_head_hash($project);4886 git_header_html();4887 git_print_page_nav('','',$head,undef,$head);4888 git_print_header_div('summary',$project);48894890my@tagslist= git_get_tags_list();4891if(@tagslist) {4892 git_tags_body(\@tagslist);4893}4894 git_footer_html();4895}48964897sub git_heads {4898my$head= git_get_head_hash($project);4899 git_header_html();4900 git_print_page_nav('','',$head,undef,$head);4901 git_print_header_div('summary',$project);49024903my@headslist= git_get_heads_list();4904if(@headslist) {4905 git_heads_body(\@headslist,$head);4906}4907 git_footer_html();4908}49094910sub git_blob_plain {4911my$type=shift;4912my$expires;49134914if(!defined$hash) {4915if(defined$file_name) {4916my$base=$hash_base|| git_get_head_hash($project);4917$hash= git_get_hash_by_path($base,$file_name,"blob")4918or die_error(404,"Cannot find file");4919}else{4920 die_error(400,"No file name defined");4921}4922}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4923# blobs defined by non-textual hash id's can be cached4924$expires="+1d";4925}49264927open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4928or die_error(500,"Open git-cat-file blob '$hash' failed");49294930# content-type (can include charset)4931$type= blob_contenttype($fd,$file_name,$type);49324933# "save as" filename, even when no $file_name is given4934my$save_as="$hash";4935if(defined$file_name) {4936$save_as=$file_name;4937}elsif($type=~m/^text\//) {4938$save_as.='.txt';4939}49404941# With XSS prevention on, blobs of all types except a few known safe4942# ones are served with "Content-Disposition: attachment" to make sure4943# they don't run in our security domain. For certain image types,4944# blob view writes an <img> tag referring to blob_plain view, and we4945# want to be sure not to break that by serving the image as an4946# attachment (though Firefox 3 doesn't seem to care).4947my$sandbox=$prevent_xss&&4948$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;49494950print$cgi->header(4951-type =>$type,4952-expires =>$expires,4953-content_disposition =>4954($sandbox?'attachment':'inline')4955.'; filename="'.$save_as.'"');4956local$/=undef;4957binmode STDOUT,':raw';4958print<$fd>;4959binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4960close$fd;4961}49624963sub git_blob {4964my$expires;49654966if(!defined$hash) {4967if(defined$file_name) {4968my$base=$hash_base|| git_get_head_hash($project);4969$hash= git_get_hash_by_path($base,$file_name,"blob")4970or die_error(404,"Cannot find file");4971}else{4972 die_error(400,"No file name defined");4973}4974}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4975# blobs defined by non-textual hash id's can be cached4976$expires="+1d";4977}49784979my$have_blame= gitweb_check_feature('blame');4980open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4981or die_error(500,"Couldn't cat$file_name,$hash");4982my$mimetype= blob_mimetype($fd,$file_name);4983if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4984close$fd;4985return git_blob_plain($mimetype);4986}4987# we can have blame only for text/* mimetype4988$have_blame&&= ($mimetype=~m!^text/!);49894990 git_header_html(undef,$expires);4991my$formats_nav='';4992if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4993if(defined$file_name) {4994if($have_blame) {4995$formats_nav.=4996$cgi->a({-href => href(action=>"blame", -replay=>1)},4997"blame") .4998" | ";4999}5000$formats_nav.=5001$cgi->a({-href => href(action=>"history", -replay=>1)},5002"history") .5003" | ".5004$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5005"raw") .5006" | ".5007$cgi->a({-href => href(action=>"blob",5008 hash_base=>"HEAD", file_name=>$file_name)},5009"HEAD");5010}else{5011$formats_nav.=5012$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5013"raw");5014}5015 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5016 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5017}else{5018print"<div class=\"page_nav\">\n".5019"<br/><br/></div>\n".5020"<div class=\"title\">$hash</div>\n";5021}5022 git_print_page_path($file_name,"blob",$hash_base);5023print"<div class=\"page_body\">\n";5024if($mimetype=~m!^image/!) {5025print qq!<img type="$mimetype"!;5026if($file_name) {5027print qq! alt="$file_name" title="$file_name"!;5028}5029print qq! src="! .5030 href(action=>"blob_plain", hash=>$hash,5031 hash_base=>$hash_base, file_name=>$file_name) .5032 qq!"/>\n!;5033}else{5034my$nr;5035while(my$line= <$fd>) {5036chomp$line;5037$nr++;5038$line= untabify($line);5039printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5040$nr,$nr,$nr, esc_html($line, -nbsp=>1);5041}5042}5043close$fd5044or print"Reading blob failed.\n";5045print"</div>";5046 git_footer_html();5047}50485049sub git_tree {5050if(!defined$hash_base) {5051$hash_base="HEAD";5052}5053if(!defined$hash) {5054if(defined$file_name) {5055$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5056}else{5057$hash=$hash_base;5058}5059}5060 die_error(404,"No such tree")unlessdefined($hash);50615062my@entries= ();5063{5064local$/="\0";5065open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash5066or die_error(500,"Open git-ls-tree failed");5067@entries=map{chomp;$_} <$fd>;5068close$fd5069or die_error(404,"Reading tree failed");5070}50715072my$refs= git_get_references();5073my$ref= format_ref_marker($refs,$hash_base);5074 git_header_html();5075my$basedir='';5076my$have_blame= gitweb_check_feature('blame');5077if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5078my@views_nav= ();5079if(defined$file_name) {5080push@views_nav,5081$cgi->a({-href => href(action=>"history", -replay=>1)},5082"history"),5083$cgi->a({-href => href(action=>"tree",5084 hash_base=>"HEAD", file_name=>$file_name)},5085"HEAD"),5086}5087my$snapshot_links= format_snapshot_links($hash);5088if(defined$snapshot_links) {5089# FIXME: Should be available when we have no hash base as well.5090push@views_nav,$snapshot_links;5091}5092 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));5093 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5094}else{5095undef$hash_base;5096print"<div class=\"page_nav\">\n";5097print"<br/><br/></div>\n";5098print"<div class=\"title\">$hash</div>\n";5099}5100if(defined$file_name) {5101$basedir=$file_name;5102if($basedirne''&&substr($basedir, -1)ne'/') {5103$basedir.='/';5104}5105 git_print_page_path($file_name,'tree',$hash_base);5106}5107print"<div class=\"page_body\">\n";5108print"<table class=\"tree\">\n";5109my$alternate=1;5110# '..' (top directory) link if possible5111if(defined$hash_base&&5112defined$file_name&&$file_name=~m![^/]+$!) {5113if($alternate) {5114print"<tr class=\"dark\">\n";5115}else{5116print"<tr class=\"light\">\n";5117}5118$alternate^=1;51195120my$up=$file_name;5121$up=~s!/?[^/]+$!!;5122undef$upunless$up;5123# based on git_print_tree_entry5124print'<td class="mode">'. mode_str('040000') ."</td>\n";5125print'<td class="list">';5126print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,5127 file_name=>$up)},5128"..");5129print"</td>\n";5130print"<td class=\"link\"></td>\n";51315132print"</tr>\n";5133}5134foreachmy$line(@entries) {5135my%t= parse_ls_tree_line($line, -z =>1);51365137if($alternate) {5138print"<tr class=\"dark\">\n";5139}else{5140print"<tr class=\"light\">\n";5141}5142$alternate^=1;51435144 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);51455146print"</tr>\n";5147}5148print"</table>\n".5149"</div>";5150 git_footer_html();5151}51525153sub git_snapshot {5154my$format=$input_params{'snapshot_format'};5155if(!@snapshot_fmts) {5156 die_error(403,"Snapshots not allowed");5157}5158# default to first supported snapshot format5159$format||=$snapshot_fmts[0];5160if($format!~m/^[a-z0-9]+$/) {5161 die_error(400,"Invalid snapshot format parameter");5162}elsif(!exists($known_snapshot_formats{$format})) {5163 die_error(400,"Unknown snapshot format");5164}elsif(!grep($_eq$format,@snapshot_fmts)) {5165 die_error(403,"Unsupported snapshot format");5166}51675168if(!defined$hash) {5169$hash= git_get_head_hash($project);5170}51715172my$name=$project;5173$name=~ s,([^/])/*\.git$,$1,;5174$name= basename($name);5175my$filename= to_utf8($name);5176$name=~s/\047/\047\\\047\047/g;5177my$cmd;5178$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5179$cmd= quote_command(5180 git_cmd(),'archive',5181"--format=$known_snapshot_formats{$format}{'format'}",5182"--prefix=$name/",$hash);5183if(exists$known_snapshot_formats{$format}{'compressor'}) {5184$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5185}51865187print$cgi->header(5188-type =>$known_snapshot_formats{$format}{'type'},5189-content_disposition =>'inline; filename="'."$filename".'"',5190-status =>'200 OK');51915192open my$fd,"-|",$cmd5193or die_error(500,"Execute git-archive failed");5194binmode STDOUT,':raw';5195print<$fd>;5196binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5197close$fd;5198}51995200sub git_log {5201my$head= git_get_head_hash($project);5202if(!defined$hash) {5203$hash=$head;5204}5205if(!defined$page) {5206$page=0;5207}5208my$refs= git_get_references();52095210my@commitlist= parse_commits($hash,101, (100*$page));52115212my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);52135214my($patch_max) = gitweb_get_feature('patches');5215if($patch_max) {5216if($patch_max<0||@commitlist<=$patch_max) {5217$paging_nav.=" ⋅ ".5218$cgi->a({-href => href(action=>"patches", -replay=>1)},5219"patches");5220}5221}52225223 git_header_html();5224 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);52255226if(!@commitlist) {5227my%co= parse_commit($hash);52285229 git_print_header_div('summary',$project);5230print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5231}5232my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5233for(my$i=0;$i<=$to;$i++) {5234my%co= %{$commitlist[$i]};5235next if!%co;5236my$commit=$co{'id'};5237my$ref= format_ref_marker($refs,$commit);5238my%ad= parse_date($co{'author_epoch'});5239 git_print_header_div('commit',5240"<span class=\"age\">$co{'age_string'}</span>".5241 esc_html($co{'title'}) .$ref,5242$commit);5243print"<div class=\"title_text\">\n".5244"<div class=\"log_link\">\n".5245$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5246" | ".5247$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5248" | ".5249$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5250"<br/>\n".5251"</div>\n";5252 git_print_authorship(\%co, -tag =>'span');5253print"<br/>\n</div>\n";52545255print"<div class=\"log_body\">\n";5256 git_print_log($co{'comment'}, -final_empty_line=>1);5257print"</div>\n";5258}5259if($#commitlist>=100) {5260print"<div class=\"page_nav\">\n";5261print$cgi->a({-href => href(-replay=>1, page=>$page+1),5262-accesskey =>"n", -title =>"Alt-n"},"next");5263print"</div>\n";5264}5265 git_footer_html();5266}52675268sub git_commit {5269$hash||=$hash_base||"HEAD";5270my%co= parse_commit($hash)5271or die_error(404,"Unknown commit object");52725273my$parent=$co{'parent'};5274my$parents=$co{'parents'};# listref52755276# we need to prepare $formats_nav before any parameter munging5277my$formats_nav;5278if(!defined$parent) {5279# --root commitdiff5280$formats_nav.='(initial)';5281}elsif(@$parents==1) {5282# single parent commit5283$formats_nav.=5284'(parent: '.5285$cgi->a({-href => href(action=>"commit",5286 hash=>$parent)},5287 esc_html(substr($parent,0,7))) .5288')';5289}else{5290# merge commit5291$formats_nav.=5292'(merge: '.5293join(' ',map{5294$cgi->a({-href => href(action=>"commit",5295 hash=>$_)},5296 esc_html(substr($_,0,7)));5297}@$parents) .5298')';5299}5300if(gitweb_check_feature('patches')) {5301$formats_nav.=" | ".5302$cgi->a({-href => href(action=>"patch", -replay=>1)},5303"patch");5304}53055306if(!defined$parent) {5307$parent="--root";5308}5309my@difftree;5310open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5311@diff_opts,5312(@$parents<=1?$parent:'-c'),5313$hash,"--"5314or die_error(500,"Open git-diff-tree failed");5315@difftree=map{chomp;$_} <$fd>;5316close$fdor die_error(404,"Reading git-diff-tree failed");53175318# non-textual hash id's can be cached5319my$expires;5320if($hash=~m/^[0-9a-fA-F]{40}$/) {5321$expires="+1d";5322}5323my$refs= git_get_references();5324my$ref= format_ref_marker($refs,$co{'id'});53255326 git_header_html(undef,$expires);5327 git_print_page_nav('commit','',5328$hash,$co{'tree'},$hash,5329$formats_nav);53305331if(defined$co{'parent'}) {5332 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5333}else{5334 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5335}5336print"<div class=\"title_text\">\n".5337"<table class=\"object_header\">\n";5338 git_print_authorship_rows(\%co);5339print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5340print"<tr>".5341"<td>tree</td>".5342"<td class=\"sha1\">".5343$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5344class=>"list"},$co{'tree'}) .5345"</td>".5346"<td class=\"link\">".5347$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5348"tree");5349my$snapshot_links= format_snapshot_links($hash);5350if(defined$snapshot_links) {5351print" | ".$snapshot_links;5352}5353print"</td>".5354"</tr>\n";53555356foreachmy$par(@$parents) {5357print"<tr>".5358"<td>parent</td>".5359"<td class=\"sha1\">".5360$cgi->a({-href => href(action=>"commit", hash=>$par),5361class=>"list"},$par) .5362"</td>".5363"<td class=\"link\">".5364$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5365" | ".5366$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5367"</td>".5368"</tr>\n";5369}5370print"</table>".5371"</div>\n";53725373print"<div class=\"page_body\">\n";5374 git_print_log($co{'comment'});5375print"</div>\n";53765377 git_difftree_body(\@difftree,$hash,@$parents);53785379 git_footer_html();5380}53815382sub git_object {5383# object is defined by:5384# - hash or hash_base alone5385# - hash_base and file_name5386my$type;53875388# - hash or hash_base alone5389if($hash|| ($hash_base&& !defined$file_name)) {5390my$object_id=$hash||$hash_base;53915392open my$fd,"-|", quote_command(5393 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5394or die_error(404,"Object does not exist");5395$type= <$fd>;5396chomp$type;5397close$fd5398or die_error(404,"Object does not exist");53995400# - hash_base and file_name5401}elsif($hash_base&&defined$file_name) {5402$file_name=~ s,/+$,,;54035404system(git_cmd(),"cat-file",'-e',$hash_base) ==05405or die_error(404,"Base object does not exist");54065407# here errors should not hapen5408open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5409or die_error(500,"Open git-ls-tree failed");5410my$line= <$fd>;5411close$fd;54125413#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5414unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5415 die_error(404,"File or directory for given base does not exist");5416}5417$type=$2;5418$hash=$3;5419}else{5420 die_error(400,"Not enough information to find object");5421}54225423print$cgi->redirect(-uri => href(action=>$type, -full=>1,5424 hash=>$hash, hash_base=>$hash_base,5425 file_name=>$file_name),5426-status =>'302 Found');5427}54285429sub git_blobdiff {5430my$format=shift||'html';54315432my$fd;5433my@difftree;5434my%diffinfo;5435my$expires;54365437# preparing $fd and %diffinfo for git_patchset_body5438# new style URI5439if(defined$hash_base&&defined$hash_parent_base) {5440if(defined$file_name) {5441# read raw output5442open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5443$hash_parent_base,$hash_base,5444"--", (defined$file_parent?$file_parent: ()),$file_name5445or die_error(500,"Open git-diff-tree failed");5446@difftree=map{chomp;$_} <$fd>;5447close$fd5448or die_error(404,"Reading git-diff-tree failed");5449@difftree5450or die_error(404,"Blob diff not found");54515452}elsif(defined$hash&&5453$hash=~/[0-9a-fA-F]{40}/) {5454# try to find filename from $hash54555456# read filtered raw output5457open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5458$hash_parent_base,$hash_base,"--"5459or die_error(500,"Open git-diff-tree failed");5460@difftree=5461# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5462# $hash == to_id5463grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5464map{chomp;$_} <$fd>;5465close$fd5466or die_error(404,"Reading git-diff-tree failed");5467@difftree5468or die_error(404,"Blob diff not found");54695470}else{5471 die_error(400,"Missing one of the blob diff parameters");5472}54735474if(@difftree>1) {5475 die_error(400,"Ambiguous blob diff specification");5476}54775478%diffinfo= parse_difftree_raw_line($difftree[0]);5479$file_parent||=$diffinfo{'from_file'} ||$file_name;5480$file_name||=$diffinfo{'to_file'};54815482$hash_parent||=$diffinfo{'from_id'};5483$hash||=$diffinfo{'to_id'};54845485# non-textual hash id's can be cached5486if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5487$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5488$expires='+1d';5489}54905491# open patch output5492open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5493'-p', ($formateq'html'?"--full-index": ()),5494$hash_parent_base,$hash_base,5495"--", (defined$file_parent?$file_parent: ()),$file_name5496or die_error(500,"Open git-diff-tree failed");5497}54985499# old/legacy style URI -- not generated anymore since 1.4.3.5500if(!%diffinfo) {5501 die_error('404 Not Found',"Missing one of the blob diff parameters")5502}55035504# header5505if($formateq'html') {5506my$formats_nav=5507$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5508"raw");5509 git_header_html(undef,$expires);5510if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5511 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5512 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5513}else{5514print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5515print"<div class=\"title\">$hashvs$hash_parent</div>\n";5516}5517if(defined$file_name) {5518 git_print_page_path($file_name,"blob",$hash_base);5519}else{5520print"<div class=\"page_path\"></div>\n";5521}55225523}elsif($formateq'plain') {5524print$cgi->header(5525-type =>'text/plain',5526-charset =>'utf-8',5527-expires =>$expires,5528-content_disposition =>'inline; filename="'."$file_name".'.patch"');55295530print"X-Git-Url: ".$cgi->self_url() ."\n\n";55315532}else{5533 die_error(400,"Unknown blobdiff format");5534}55355536# patch5537if($formateq'html') {5538print"<div class=\"page_body\">\n";55395540 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5541close$fd;55425543print"</div>\n";# class="page_body"5544 git_footer_html();55455546}else{5547while(my$line= <$fd>) {5548$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5549$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;55505551print$line;55525553last if$line=~m!^\+\+\+!;5554}5555local$/=undef;5556print<$fd>;5557close$fd;5558}5559}55605561sub git_blobdiff_plain {5562 git_blobdiff('plain');5563}55645565sub git_commitdiff {5566my%params=@_;5567my$format=$params{-format} ||'html';55685569my($patch_max) = gitweb_get_feature('patches');5570if($formateq'patch') {5571 die_error(403,"Patch view not allowed")unless$patch_max;5572}55735574$hash||=$hash_base||"HEAD";5575my%co= parse_commit($hash)5576or die_error(404,"Unknown commit object");55775578# choose format for commitdiff for merge5579if(!defined$hash_parent&& @{$co{'parents'}} >1) {5580$hash_parent='--cc';5581}5582# we need to prepare $formats_nav before almost any parameter munging5583my$formats_nav;5584if($formateq'html') {5585$formats_nav=5586$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5587"raw");5588if($patch_max) {5589$formats_nav.=" | ".5590$cgi->a({-href => href(action=>"patch", -replay=>1)},5591"patch");5592}55935594if(defined$hash_parent&&5595$hash_parentne'-c'&&$hash_parentne'--cc') {5596# commitdiff with two commits given5597my$hash_parent_short=$hash_parent;5598if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5599$hash_parent_short=substr($hash_parent,0,7);5600}5601$formats_nav.=5602' (from';5603for(my$i=0;$i< @{$co{'parents'}};$i++) {5604if($co{'parents'}[$i]eq$hash_parent) {5605$formats_nav.=' parent '. ($i+1);5606last;5607}5608}5609$formats_nav.=': '.5610$cgi->a({-href => href(action=>"commitdiff",5611 hash=>$hash_parent)},5612 esc_html($hash_parent_short)) .5613')';5614}elsif(!$co{'parent'}) {5615# --root commitdiff5616$formats_nav.=' (initial)';5617}elsif(scalar@{$co{'parents'}} ==1) {5618# single parent commit5619$formats_nav.=5620' (parent: '.5621$cgi->a({-href => href(action=>"commitdiff",5622 hash=>$co{'parent'})},5623 esc_html(substr($co{'parent'},0,7))) .5624')';5625}else{5626# merge commit5627if($hash_parenteq'--cc') {5628$formats_nav.=' | '.5629$cgi->a({-href => href(action=>"commitdiff",5630 hash=>$hash, hash_parent=>'-c')},5631'combined');5632}else{# $hash_parent eq '-c'5633$formats_nav.=' | '.5634$cgi->a({-href => href(action=>"commitdiff",5635 hash=>$hash, hash_parent=>'--cc')},5636'compact');5637}5638$formats_nav.=5639' (merge: '.5640join(' ',map{5641$cgi->a({-href => href(action=>"commitdiff",5642 hash=>$_)},5643 esc_html(substr($_,0,7)));5644} @{$co{'parents'}} ) .5645')';5646}5647}56485649my$hash_parent_param=$hash_parent;5650if(!defined$hash_parent_param) {5651# --cc for multiple parents, --root for parentless5652$hash_parent_param=5653@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5654}56555656# read commitdiff5657my$fd;5658my@difftree;5659if($formateq'html') {5660open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5661"--no-commit-id","--patch-with-raw","--full-index",5662$hash_parent_param,$hash,"--"5663or die_error(500,"Open git-diff-tree failed");56645665while(my$line= <$fd>) {5666chomp$line;5667# empty line ends raw part of diff-tree output5668last unless$line;5669push@difftree,scalar parse_difftree_raw_line($line);5670}56715672}elsif($formateq'plain') {5673open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5674'-p',$hash_parent_param,$hash,"--"5675or die_error(500,"Open git-diff-tree failed");5676}elsif($formateq'patch') {5677# For commit ranges, we limit the output to the number of5678# patches specified in the 'patches' feature.5679# For single commits, we limit the output to a single patch,5680# diverging from the git-format-patch default.5681my@commit_spec= ();5682if($hash_parent) {5683if($patch_max>0) {5684push@commit_spec,"-$patch_max";5685}5686push@commit_spec,'-n',"$hash_parent..$hash";5687}else{5688if($params{-single}) {5689push@commit_spec,'-1';5690}else{5691if($patch_max>0) {5692push@commit_spec,"-$patch_max";5693}5694push@commit_spec,"-n";5695}5696push@commit_spec,'--root',$hash;5697}5698open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5699'--stdout',@commit_spec5700or die_error(500,"Open git-format-patch failed");5701}else{5702 die_error(400,"Unknown commitdiff format");5703}57045705# non-textual hash id's can be cached5706my$expires;5707if($hash=~m/^[0-9a-fA-F]{40}$/) {5708$expires="+1d";5709}57105711# write commit message5712if($formateq'html') {5713my$refs= git_get_references();5714my$ref= format_ref_marker($refs,$co{'id'});57155716 git_header_html(undef,$expires);5717 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5718 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5719print"<div class=\"title_text\">\n".5720"<table class=\"object_header\">\n";5721 git_print_authorship_rows(\%co);5722print"</table>".5723"</div>\n";5724print"<div class=\"page_body\">\n";5725if(@{$co{'comment'}} >1) {5726print"<div class=\"log\">\n";5727 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5728print"</div>\n";# class="log"5729}57305731}elsif($formateq'plain') {5732my$refs= git_get_references("tags");5733my$tagname= git_get_rev_name_tags($hash);5734my$filename= basename($project) ."-$hash.patch";57355736print$cgi->header(5737-type =>'text/plain',5738-charset =>'utf-8',5739-expires =>$expires,5740-content_disposition =>'inline; filename="'."$filename".'"');5741my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5742print"From: ". to_utf8($co{'author'}) ."\n";5743print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5744print"Subject: ". to_utf8($co{'title'}) ."\n";57455746print"X-Git-Tag:$tagname\n"if$tagname;5747print"X-Git-Url: ".$cgi->self_url() ."\n\n";57485749foreachmy$line(@{$co{'comment'}}) {5750print to_utf8($line) ."\n";5751}5752print"---\n\n";5753}elsif($formateq'patch') {5754my$filename= basename($project) ."-$hash.patch";57555756print$cgi->header(5757-type =>'text/plain',5758-charset =>'utf-8',5759-expires =>$expires,5760-content_disposition =>'inline; filename="'."$filename".'"');5761}57625763# write patch5764if($formateq'html') {5765my$use_parents= !defined$hash_parent||5766$hash_parenteq'-c'||$hash_parenteq'--cc';5767 git_difftree_body(\@difftree,$hash,5768$use_parents? @{$co{'parents'}} :$hash_parent);5769print"<br/>\n";57705771 git_patchset_body($fd, \@difftree,$hash,5772$use_parents? @{$co{'parents'}} :$hash_parent);5773close$fd;5774print"</div>\n";# class="page_body"5775 git_footer_html();57765777}elsif($formateq'plain') {5778local$/=undef;5779print<$fd>;5780close$fd5781or print"Reading git-diff-tree failed\n";5782}elsif($formateq'patch') {5783local$/=undef;5784print<$fd>;5785close$fd5786or print"Reading git-format-patch failed\n";5787}5788}57895790sub git_commitdiff_plain {5791 git_commitdiff(-format =>'plain');5792}57935794# format-patch-style patches5795sub git_patch {5796 git_commitdiff(-format =>'patch', -single=>1);5797}57985799sub git_patches {5800 git_commitdiff(-format =>'patch');5801}58025803sub git_history {5804if(!defined$hash_base) {5805$hash_base= git_get_head_hash($project);5806}5807if(!defined$page) {5808$page=0;5809}5810my$ftype;5811my%co= parse_commit($hash_base)5812or die_error(404,"Unknown commit object");58135814my$refs= git_get_references();5815my$limit=sprintf("--max-count=%i", (100* ($page+1)));58165817my@commitlist= parse_commits($hash_base,101, (100*$page),5818$file_name,"--full-history")5819or die_error(404,"No such file or directory on given branch");58205821if(!defined$hash&&defined$file_name) {5822# some commits could have deleted file in question,5823# and not have it in tree, but one of them has to have it5824for(my$i=0;$i<=@commitlist;$i++) {5825$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5826last ifdefined$hash;5827}5828}5829if(defined$hash) {5830$ftype= git_get_type($hash);5831}5832if(!defined$ftype) {5833 die_error(500,"Unknown type of object");5834}58355836my$paging_nav='';5837if($page>0) {5838$paging_nav.=5839$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5840 file_name=>$file_name)},5841"first");5842$paging_nav.=" ⋅ ".5843$cgi->a({-href => href(-replay=>1, page=>$page-1),5844-accesskey =>"p", -title =>"Alt-p"},"prev");5845}else{5846$paging_nav.="first";5847$paging_nav.=" ⋅ prev";5848}5849my$next_link='';5850if($#commitlist>=100) {5851$next_link=5852$cgi->a({-href => href(-replay=>1, page=>$page+1),5853-accesskey =>"n", -title =>"Alt-n"},"next");5854$paging_nav.=" ⋅$next_link";5855}else{5856$paging_nav.=" ⋅ next";5857}58585859 git_header_html();5860 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5861 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5862 git_print_page_path($file_name,$ftype,$hash_base);58635864 git_history_body(\@commitlist,0,99,5865$refs,$hash_base,$ftype,$next_link);58665867 git_footer_html();5868}58695870sub git_search {5871 gitweb_check_feature('search')or die_error(403,"Search is disabled");5872if(!defined$searchtext) {5873 die_error(400,"Text field is empty");5874}5875if(!defined$hash) {5876$hash= git_get_head_hash($project);5877}5878my%co= parse_commit($hash);5879if(!%co) {5880 die_error(404,"Unknown commit object");5881}5882if(!defined$page) {5883$page=0;5884}58855886$searchtype||='commit';5887if($searchtypeeq'pickaxe') {5888# pickaxe may take all resources of your box and run for several minutes5889# with every query - so decide by yourself how public you make this feature5890 gitweb_check_feature('pickaxe')5891or die_error(403,"Pickaxe is disabled");5892}5893if($searchtypeeq'grep') {5894 gitweb_check_feature('grep')5895or die_error(403,"Grep is disabled");5896}58975898 git_header_html();58995900if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5901my$greptype;5902if($searchtypeeq'commit') {5903$greptype="--grep=";5904}elsif($searchtypeeq'author') {5905$greptype="--author=";5906}elsif($searchtypeeq'committer') {5907$greptype="--committer=";5908}5909$greptype.=$searchtext;5910my@commitlist= parse_commits($hash,101, (100*$page),undef,5911$greptype,'--regexp-ignore-case',5912$search_use_regexp?'--extended-regexp':'--fixed-strings');59135914my$paging_nav='';5915if($page>0) {5916$paging_nav.=5917$cgi->a({-href => href(action=>"search", hash=>$hash,5918 searchtext=>$searchtext,5919 searchtype=>$searchtype)},5920"first");5921$paging_nav.=" ⋅ ".5922$cgi->a({-href => href(-replay=>1, page=>$page-1),5923-accesskey =>"p", -title =>"Alt-p"},"prev");5924}else{5925$paging_nav.="first";5926$paging_nav.=" ⋅ prev";5927}5928my$next_link='';5929if($#commitlist>=100) {5930$next_link=5931$cgi->a({-href => href(-replay=>1, page=>$page+1),5932-accesskey =>"n", -title =>"Alt-n"},"next");5933$paging_nav.=" ⋅$next_link";5934}else{5935$paging_nav.=" ⋅ next";5936}59375938if($#commitlist>=100) {5939}59405941 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5942 git_print_header_div('commit', esc_html($co{'title'}),$hash);5943 git_search_grep_body(\@commitlist,0,99,$next_link);5944}59455946if($searchtypeeq'pickaxe') {5947 git_print_page_nav('','',$hash,$co{'tree'},$hash);5948 git_print_header_div('commit', esc_html($co{'title'}),$hash);59495950print"<table class=\"pickaxe search\">\n";5951my$alternate=1;5952local$/="\n";5953open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5954'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5955($search_use_regexp?'--pickaxe-regex': ());5956undef%co;5957my@files;5958while(my$line= <$fd>) {5959chomp$line;5960next unless$line;59615962my%set= parse_difftree_raw_line($line);5963if(defined$set{'commit'}) {5964# finish previous commit5965if(%co) {5966print"</td>\n".5967"<td class=\"link\">".5968$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5969" | ".5970$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5971print"</td>\n".5972"</tr>\n";5973}59745975if($alternate) {5976print"<tr class=\"dark\">\n";5977}else{5978print"<tr class=\"light\">\n";5979}5980$alternate^=1;5981%co= parse_commit($set{'commit'});5982my$author= chop_and_escape_str($co{'author_name'},15,5);5983print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5984"<td><i>$author</i></td>\n".5985"<td>".5986$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5987-class=>"list subject"},5988 chop_and_escape_str($co{'title'},50) ."<br/>");5989}elsif(defined$set{'to_id'}) {5990next if($set{'to_id'} =~m/^0{40}$/);59915992print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5993 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5994-class=>"list"},5995"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5996"<br/>\n";5997}5998}5999close$fd;60006001# finish last commit (warning: repetition!)6002if(%co) {6003print"</td>\n".6004"<td class=\"link\">".6005$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6006" | ".6007$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6008print"</td>\n".6009"</tr>\n";6010}60116012print"</table>\n";6013}60146015if($searchtypeeq'grep') {6016 git_print_page_nav('','',$hash,$co{'tree'},$hash);6017 git_print_header_div('commit', esc_html($co{'title'}),$hash);60186019print"<table class=\"grep_search\">\n";6020my$alternate=1;6021my$matches=0;6022local$/="\n";6023open my$fd,"-|", git_cmd(),'grep','-n',6024$search_use_regexp? ('-E','-i') :'-F',6025$searchtext,$co{'tree'};6026my$lastfile='';6027while(my$line= <$fd>) {6028chomp$line;6029my($file,$lno,$ltext,$binary);6030last if($matches++>1000);6031if($line=~/^Binary file (.+) matches$/) {6032$file=$1;6033$binary=1;6034}else{6035(undef,$file,$lno,$ltext) =split(/:/,$line,4);6036}6037if($filene$lastfile) {6038$lastfileand print"</td></tr>\n";6039if($alternate++) {6040print"<tr class=\"dark\">\n";6041}else{6042print"<tr class=\"light\">\n";6043}6044print"<td class=\"list\">".6045$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6046 file_name=>"$file"),6047-class=>"list"}, esc_path($file));6048print"</td><td>\n";6049$lastfile=$file;6050}6051if($binary) {6052print"<div class=\"binary\">Binary file</div>\n";6053}else{6054$ltext= untabify($ltext);6055if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6056$ltext= esc_html($1, -nbsp=>1);6057$ltext.='<span class="match">';6058$ltext.= esc_html($2, -nbsp=>1);6059$ltext.='</span>';6060$ltext.= esc_html($3, -nbsp=>1);6061}else{6062$ltext= esc_html($ltext, -nbsp=>1);6063}6064print"<div class=\"pre\">".6065$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6066 file_name=>"$file").'#l'.$lno,6067-class=>"linenr"},sprintf('%4i',$lno))6068.' '.$ltext."</div>\n";6069}6070}6071if($lastfile) {6072print"</td></tr>\n";6073if($matches>1000) {6074print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6075}6076}else{6077print"<div class=\"diff nodifferences\">No matches found</div>\n";6078}6079close$fd;60806081print"</table>\n";6082}6083 git_footer_html();6084}60856086sub git_search_help {6087 git_header_html();6088 git_print_page_nav('','',$hash,$hash,$hash);6089print<<EOT;6090<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6091regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6092the pattern entered is recognized as the POSIX extended6093<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6094insensitive).</p>6095<dl>6096<dt><b>commit</b></dt>6097<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6098EOT6099my$have_grep= gitweb_check_feature('grep');6100if($have_grep) {6101print<<EOT;6102<dt><b>grep</b></dt>6103<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6104 a different one) are searched for the given pattern. On large trees, this search can take6105a while and put some strain on the server, so please use it with some consideration. Note that6106due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6107case-sensitive.</dd>6108EOT6109}6110print<<EOT;6111<dt><b>author</b></dt>6112<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6113<dt><b>committer</b></dt>6114<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6115EOT6116my$have_pickaxe= gitweb_check_feature('pickaxe');6117if($have_pickaxe) {6118print<<EOT;6119<dt><b>pickaxe</b></dt>6120<dd>All commits that caused the string to appear or disappear from any file (changes that6121added, removed or "modified" the string) will be listed. This search can take a while and6122takes a lot of strain on the server, so please use it wisely. Note that since you may be6123interested even in changes just changing the case as well, this search is case sensitive.</dd>6124EOT6125}6126print"</dl>\n";6127 git_footer_html();6128}61296130sub git_shortlog {6131my$head= git_get_head_hash($project);6132if(!defined$hash) {6133$hash=$head;6134}6135if(!defined$page) {6136$page=0;6137}6138my$refs= git_get_references();61396140my$commit_hash=$hash;6141if(defined$hash_parent) {6142$commit_hash="$hash_parent..$hash";6143}6144my@commitlist= parse_commits($commit_hash,101, (100*$page));61456146my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);6147my$next_link='';6148if($#commitlist>=100) {6149$next_link=6150$cgi->a({-href => href(-replay=>1, page=>$page+1),6151-accesskey =>"n", -title =>"Alt-n"},"next");6152}6153my$patch_max= gitweb_check_feature('patches');6154if($patch_max) {6155if($patch_max<0||@commitlist<=$patch_max) {6156$paging_nav.=" ⋅ ".6157$cgi->a({-href => href(action=>"patches", -replay=>1)},6158"patches");6159}6160}61616162 git_header_html();6163 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6164 git_print_header_div('summary',$project);61656166 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);61676168 git_footer_html();6169}61706171## ......................................................................6172## feeds (RSS, Atom; OPML)61736174sub git_feed {6175my$format=shift||'atom';6176my$have_blame= gitweb_check_feature('blame');61776178# Atom: http://www.atomenabled.org/developers/syndication/6179# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6180if($formatne'rss'&&$formatne'atom') {6181 die_error(400,"Unknown web feed format");6182}61836184# log/feed of current (HEAD) branch, log of given branch, history of file/directory6185my$head=$hash||'HEAD';6186my@commitlist= parse_commits($head,150,0,$file_name);61876188my%latest_commit;6189my%latest_date;6190my$content_type="application/$format+xml";6191if(defined$cgi->http('HTTP_ACCEPT') &&6192$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6193# browser (feed reader) prefers text/xml6194$content_type='text/xml';6195}6196if(defined($commitlist[0])) {6197%latest_commit= %{$commitlist[0]};6198my$latest_epoch=$latest_commit{'committer_epoch'};6199%latest_date= parse_date($latest_epoch);6200my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6201if(defined$if_modified) {6202my$since;6203if(eval{require HTTP::Date;1; }) {6204$since= HTTP::Date::str2time($if_modified);6205}elsif(eval{require Time::ParseDate;1; }) {6206$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6207}6208if(defined$since&&$latest_epoch<=$since) {6209print$cgi->header(6210-type =>$content_type,6211-charset =>'utf-8',6212-last_modified =>$latest_date{'rfc2822'},6213-status =>'304 Not Modified');6214return;6215}6216}6217print$cgi->header(6218-type =>$content_type,6219-charset =>'utf-8',6220-last_modified =>$latest_date{'rfc2822'});6221}else{6222print$cgi->header(6223-type =>$content_type,6224-charset =>'utf-8');6225}62266227# Optimization: skip generating the body if client asks only6228# for Last-Modified date.6229return if($cgi->request_method()eq'HEAD');62306231# header variables6232my$title="$site_name-$project/$action";6233my$feed_type='log';6234if(defined$hash) {6235$title.=" - '$hash'";6236$feed_type='branch log';6237if(defined$file_name) {6238$title.=" ::$file_name";6239$feed_type='history';6240}6241}elsif(defined$file_name) {6242$title.=" -$file_name";6243$feed_type='history';6244}6245$title.="$feed_type";6246my$descr= git_get_project_description($project);6247if(defined$descr) {6248$descr= esc_html($descr);6249}else{6250$descr="$project".6251($formateq'rss'?'RSS':'Atom') .6252" feed";6253}6254my$owner= git_get_project_owner($project);6255$owner= esc_html($owner);62566257#header6258my$alt_url;6259if(defined$file_name) {6260$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6261}elsif(defined$hash) {6262$alt_url= href(-full=>1, action=>"log", hash=>$hash);6263}else{6264$alt_url= href(-full=>1, action=>"summary");6265}6266print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6267if($formateq'rss') {6268print<<XML;6269<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6270<channel>6271XML6272print"<title>$title</title>\n".6273"<link>$alt_url</link>\n".6274"<description>$descr</description>\n".6275"<language>en</language>\n".6276# project owner is responsible for 'editorial' content6277"<managingEditor>$owner</managingEditor>\n";6278if(defined$logo||defined$favicon) {6279# prefer the logo to the favicon, since RSS6280# doesn't allow both6281my$img= esc_url($logo||$favicon);6282print"<image>\n".6283"<url>$img</url>\n".6284"<title>$title</title>\n".6285"<link>$alt_url</link>\n".6286"</image>\n";6287}6288if(%latest_date) {6289print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6290print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6291}6292print"<generator>gitweb v.$version/$git_version</generator>\n";6293}elsif($formateq'atom') {6294print<<XML;6295<feed xmlns="http://www.w3.org/2005/Atom">6296XML6297print"<title>$title</title>\n".6298"<subtitle>$descr</subtitle>\n".6299'<link rel="alternate" type="text/html" href="'.6300$alt_url.'" />'."\n".6301'<link rel="self" type="'.$content_type.'" href="'.6302$cgi->self_url() .'" />'."\n".6303"<id>". href(-full=>1) ."</id>\n".6304# use project owner for feed author6305"<author><name>$owner</name></author>\n";6306if(defined$favicon) {6307print"<icon>". esc_url($favicon) ."</icon>\n";6308}6309if(defined$logo_url) {6310# not twice as wide as tall: 72 x 27 pixels6311print"<logo>". esc_url($logo) ."</logo>\n";6312}6313if(!%latest_date) {6314# dummy date to keep the feed valid until commits trickle in:6315print"<updated>1970-01-01T00:00:00Z</updated>\n";6316}else{6317print"<updated>$latest_date{'iso-8601'}</updated>\n";6318}6319print"<generator version='$version/$git_version'>gitweb</generator>\n";6320}63216322# contents6323for(my$i=0;$i<=$#commitlist;$i++) {6324my%co= %{$commitlist[$i]};6325my$commit=$co{'id'};6326# we read 150, we always show 30 and the ones more recent than 48 hours6327if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6328last;6329}6330my%cd= parse_date($co{'author_epoch'});63316332# get list of changed files6333open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6334$co{'parent'} ||"--root",6335$co{'id'},"--", (defined$file_name?$file_name: ())6336ornext;6337my@difftree=map{chomp;$_} <$fd>;6338close$fd6339ornext;63406341# print element (entry, item)6342my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6343if($formateq'rss') {6344print"<item>\n".6345"<title>". esc_html($co{'title'}) ."</title>\n".6346"<author>". esc_html($co{'author'}) ."</author>\n".6347"<pubDate>$cd{'rfc2822'}</pubDate>\n".6348"<guid isPermaLink=\"true\">$co_url</guid>\n".6349"<link>$co_url</link>\n".6350"<description>". esc_html($co{'title'}) ."</description>\n".6351"<content:encoded>".6352"<![CDATA[\n";6353}elsif($formateq'atom') {6354print"<entry>\n".6355"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6356"<updated>$cd{'iso-8601'}</updated>\n".6357"<author>\n".6358" <name>". esc_html($co{'author_name'}) ."</name>\n";6359if($co{'author_email'}) {6360print" <email>". esc_html($co{'author_email'}) ."</email>\n";6361}6362print"</author>\n".6363# use committer for contributor6364"<contributor>\n".6365" <name>". esc_html($co{'committer_name'}) ."</name>\n";6366if($co{'committer_email'}) {6367print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6368}6369print"</contributor>\n".6370"<published>$cd{'iso-8601'}</published>\n".6371"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6372"<id>$co_url</id>\n".6373"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6374"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6375}6376my$comment=$co{'comment'};6377print"<pre>\n";6378foreachmy$line(@$comment) {6379$line= esc_html($line);6380print"$line\n";6381}6382print"</pre><ul>\n";6383foreachmy$difftree_line(@difftree) {6384my%difftree= parse_difftree_raw_line($difftree_line);6385next if!$difftree{'from_id'};63866387my$file=$difftree{'file'} ||$difftree{'to_file'};63886389print"<li>".6390"[".6391$cgi->a({-href => href(-full=>1, action=>"blobdiff",6392 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6393 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6394 file_name=>$file, file_parent=>$difftree{'from_file'}),6395-title =>"diff"},'D');6396if($have_blame) {6397print$cgi->a({-href => href(-full=>1, action=>"blame",6398 file_name=>$file, hash_base=>$commit),6399-title =>"blame"},'B');6400}6401# if this is not a feed of a file history6402if(!defined$file_name||$file_namene$file) {6403print$cgi->a({-href => href(-full=>1, action=>"history",6404 file_name=>$file, hash=>$commit),6405-title =>"history"},'H');6406}6407$file= esc_path($file);6408print"] ".6409"$file</li>\n";6410}6411if($formateq'rss') {6412print"</ul>]]>\n".6413"</content:encoded>\n".6414"</item>\n";6415}elsif($formateq'atom') {6416print"</ul>\n</div>\n".6417"</content>\n".6418"</entry>\n";6419}6420}64216422# end of feed6423if($formateq'rss') {6424print"</channel>\n</rss>\n";6425}elsif($formateq'atom') {6426print"</feed>\n";6427}6428}64296430sub git_rss {6431 git_feed('rss');6432}64336434sub git_atom {6435 git_feed('atom');6436}64376438sub git_opml {6439my@list= git_get_projects_list();64406441print$cgi->header(6442-type =>'text/xml',6443-charset =>'utf-8',6444-content_disposition =>'inline; filename="opml.xml"');64456446print<<XML;6447<?xml version="1.0" encoding="utf-8"?>6448<opml version="1.0">6449<head>6450 <title>$site_nameOPML Export</title>6451</head>6452<body>6453<outline text="git RSS feeds">6454XML64556456foreachmy$pr(@list) {6457my%proj=%$pr;6458my$head= git_get_head_hash($proj{'path'});6459if(!defined$head) {6460next;6461}6462$git_dir="$projectroot/$proj{'path'}";6463my%co= parse_commit($head);6464if(!%co) {6465next;6466}64676468my$path= esc_html(chop_str($proj{'path'},25,5));6469my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6470my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6471print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6472}6473print<<XML;6474</outline>6475</body>6476</opml>6477XML6478}