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 943if(defined$params{'file_parent'}) { 944if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) { 945delete$params{'file_parent'}; 946}elsif($params{'file_parent'} !~/\.\./) { 947$href.=":/".esc_url($params{'file_parent'}); 948delete$params{'file_parent'}; 949} 950} 951$href.=".."; 952delete$params{'hash_parent'}; 953delete$params{'hash_parent_base'}; 954}elsif(defined$params{'hash_parent'}) { 955$href.= esc_url($params{'hash_parent'}).".."; 956delete$params{'hash_parent'}; 957} 958 959$href.= esc_url($params{'hash_base'}); 960if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 961$href.=":/".esc_url($params{'file_name'}); 962delete$params{'file_name'}; 963} 964delete$params{'hash'}; 965delete$params{'hash_base'}; 966}elsif(defined$params{'hash'}) { 967$href.= esc_url($params{'hash'}); 968delete$params{'hash'}; 969} 970 971# If the action was a snapshot, we can absorb the 972# snapshot_format parameter too 973if($is_snapshot) { 974my$fmt=$params{'snapshot_format'}; 975# snapshot_format should always be defined when href() 976# is called, but just in case some code forgets, we 977# fall back to the default 978$fmt||=$snapshot_fmts[0]; 979$href.=$known_snapshot_formats{$fmt}{'suffix'}; 980delete$params{'snapshot_format'}; 981} 982} 983 984# now encode the parameters explicitly 985my@result= (); 986for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 987my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 988if(defined$params{$name}) { 989if(ref($params{$name})eq"ARRAY") { 990foreachmy$par(@{$params{$name}}) { 991push@result,$symbol."=". esc_param($par); 992} 993}else{ 994push@result,$symbol."=". esc_param($params{$name}); 995} 996} 997} 998$href.="?".join(';',@result)ifscalar@result; 9991000return$href;1001}100210031004## ======================================================================1005## validation, quoting/unquoting and escaping10061007sub validate_action {1008my$input=shift||returnundef;1009returnundefunlessexists$actions{$input};1010return$input;1011}10121013sub validate_project {1014my$input=shift||returnundef;1015if(!validate_pathname($input) ||1016!(-d "$projectroot/$input") ||1017!check_export_ok("$projectroot/$input") ||1018($strict_export&& !project_in_list($input))) {1019returnundef;1020}else{1021return$input;1022}1023}10241025sub validate_pathname {1026my$input=shift||returnundef;10271028# no '.' or '..' as elements of path, i.e. no '.' nor '..'1029# at the beginning, at the end, and between slashes.1030# also this catches doubled slashes1031if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1032returnundef;1033}1034# no null characters1035if($input=~m!\0!) {1036returnundef;1037}1038return$input;1039}10401041sub validate_refname {1042my$input=shift||returnundef;10431044# textual hashes are O.K.1045if($input=~m/^[0-9a-fA-F]{40}$/) {1046return$input;1047}1048# it must be correct pathname1049$input= validate_pathname($input)1050orreturnundef;1051# restrictions on ref name according to git-check-ref-format1052if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1053returnundef;1054}1055return$input;1056}10571058# decode sequences of octets in utf8 into Perl's internal form,1059# which is utf-8 with utf8 flag set if needed. gitweb writes out1060# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1061sub to_utf8 {1062my$str=shift;1063if(utf8::valid($str)) {1064 utf8::decode($str);1065return$str;1066}else{1067return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1068}1069}10701071# quote unsafe chars, but keep the slash, even when it's not1072# correct, but quoted slashes look too horrible in bookmarks1073sub esc_param {1074my$str=shift;1075$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg;1076$str=~s/\+/%2B/g;1077$str=~s/ /\+/g;1078return$str;1079}10801081# quote unsafe chars in whole URL, so some charactrs cannot be quoted1082sub esc_url {1083my$str=shift;1084$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1085$str=~s/\+/%2B/g;1086$str=~s/ /\+/g;1087return$str;1088}10891090# replace invalid utf8 character with SUBSTITUTION sequence1091sub esc_html {1092my$str=shift;1093my%opts=@_;10941095$str= to_utf8($str);1096$str=$cgi->escapeHTML($str);1097if($opts{'-nbsp'}) {1098$str=~s/ / /g;1099}1100$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1101return$str;1102}11031104# quote control characters and escape filename to HTML1105sub esc_path {1106my$str=shift;1107my%opts=@_;11081109$str= to_utf8($str);1110$str=$cgi->escapeHTML($str);1111if($opts{'-nbsp'}) {1112$str=~s/ / /g;1113}1114$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1115return$str;1116}11171118# Make control characters "printable", using character escape codes (CEC)1119sub quot_cec {1120my$cntrl=shift;1121my%opts=@_;1122my%es= (# character escape codes, aka escape sequences1123"\t"=>'\t',# tab (HT)1124"\n"=>'\n',# line feed (LF)1125"\r"=>'\r',# carrige return (CR)1126"\f"=>'\f',# form feed (FF)1127"\b"=>'\b',# backspace (BS)1128"\a"=>'\a',# alarm (bell) (BEL)1129"\e"=>'\e',# escape (ESC)1130"\013"=>'\v',# vertical tab (VT)1131"\000"=>'\0',# nul character (NUL)1132);1133my$chr= ( (exists$es{$cntrl})1134?$es{$cntrl}1135:sprintf('\%2x',ord($cntrl)) );1136if($opts{-nohtml}) {1137return$chr;1138}else{1139return"<span class=\"cntrl\">$chr</span>";1140}1141}11421143# Alternatively use unicode control pictures codepoints,1144# Unicode "printable representation" (PR)1145sub quot_upr {1146my$cntrl=shift;1147my%opts=@_;11481149my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1150if($opts{-nohtml}) {1151return$chr;1152}else{1153return"<span class=\"cntrl\">$chr</span>";1154}1155}11561157# git may return quoted and escaped filenames1158sub unquote {1159my$str=shift;11601161sub unq {1162my$seq=shift;1163my%es= (# character escape codes, aka escape sequences1164't'=>"\t",# tab (HT, TAB)1165'n'=>"\n",# newline (NL)1166'r'=>"\r",# return (CR)1167'f'=>"\f",# form feed (FF)1168'b'=>"\b",# backspace (BS)1169'a'=>"\a",# alarm (bell) (BEL)1170'e'=>"\e",# escape (ESC)1171'v'=>"\013",# vertical tab (VT)1172);11731174if($seq=~m/^[0-7]{1,3}$/) {1175# octal char sequence1176returnchr(oct($seq));1177}elsif(exists$es{$seq}) {1178# C escape sequence, aka character escape code1179return$es{$seq};1180}1181# quoted ordinary character1182return$seq;1183}11841185if($str=~m/^"(.*)"$/) {1186# needs unquoting1187$str=$1;1188$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1189}1190return$str;1191}11921193# escape tabs (convert tabs to spaces)1194sub untabify {1195my$line=shift;11961197while((my$pos=index($line,"\t")) != -1) {1198if(my$count= (8- ($pos%8))) {1199my$spaces=' ' x $count;1200$line=~s/\t/$spaces/;1201}1202}12031204return$line;1205}12061207sub project_in_list {1208my$project=shift;1209my@list= git_get_projects_list();1210return@list&&scalar(grep{$_->{'path'}eq$project}@list);1211}12121213## ----------------------------------------------------------------------1214## HTML aware string manipulation12151216# Try to chop given string on a word boundary between position1217# $len and $len+$add_len. If there is no word boundary there,1218# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1219# (marking chopped part) would be longer than given string.1220sub chop_str {1221my$str=shift;1222my$len=shift;1223my$add_len=shift||10;1224my$where=shift||'right';# 'left' | 'center' | 'right'12251226# Make sure perl knows it is utf8 encoded so we don't1227# cut in the middle of a utf8 multibyte char.1228$str= to_utf8($str);12291230# allow only $len chars, but don't cut a word if it would fit in $add_len1231# if it doesn't fit, cut it if it's still longer than the dots we would add1232# remove chopped character entities entirely12331234# when chopping in the middle, distribute $len into left and right part1235# return early if chopping wouldn't make string shorter1236if($whereeq'center') {1237return$strif($len+5>=length($str));# filler is length 51238$len=int($len/2);1239}else{1240return$strif($len+4>=length($str));# filler is length 41241}12421243# regexps: ending and beginning with word part up to $add_len1244my$endre=qr/.{$len}\w{0,$add_len}/;1245my$begre=qr/\w{0,$add_len}.{$len}/;12461247if($whereeq'left') {1248$str=~m/^(.*?)($begre)$/;1249my($lead,$body) = ($1,$2);1250if(length($lead) >4) {1251$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1252$lead=" ...";1253}1254return"$lead$body";12551256}elsif($whereeq'center') {1257$str=~m/^($endre)(.*)$/;1258my($left,$str) = ($1,$2);1259$str=~m/^(.*?)($begre)$/;1260my($mid,$right) = ($1,$2);1261if(length($mid) >5) {1262$left=~s/&[^;]*$//;1263$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1264$mid=" ... ";1265}1266return"$left$mid$right";12671268}else{1269$str=~m/^($endre)(.*)$/;1270my$body=$1;1271my$tail=$2;1272if(length($tail) >4) {1273$body=~s/&[^;]*$//;1274$tail="... ";1275}1276return"$body$tail";1277}1278}12791280# takes the same arguments as chop_str, but also wraps a <span> around the1281# result with a title attribute if it does get chopped. Additionally, the1282# string is HTML-escaped.1283sub chop_and_escape_str {1284my($str) =@_;12851286my$chopped= chop_str(@_);1287if($choppedeq$str) {1288return esc_html($chopped);1289}else{1290$str=~s/[[:cntrl:]]/?/g;1291return$cgi->span({-title=>$str}, esc_html($chopped));1292}1293}12941295## ----------------------------------------------------------------------1296## functions returning short strings12971298# CSS class for given age value (in seconds)1299sub age_class {1300my$age=shift;13011302if(!defined$age) {1303return"noage";1304}elsif($age<60*60*2) {1305return"age0";1306}elsif($age<60*60*24*2) {1307return"age1";1308}else{1309return"age2";1310}1311}13121313# convert age in seconds to "nn units ago" string1314sub age_string {1315my$age=shift;1316my$age_str;13171318if($age>60*60*24*365*2) {1319$age_str= (int$age/60/60/24/365);1320$age_str.=" years ago";1321}elsif($age>60*60*24*(365/12)*2) {1322$age_str=int$age/60/60/24/(365/12);1323$age_str.=" months ago";1324}elsif($age>60*60*24*7*2) {1325$age_str=int$age/60/60/24/7;1326$age_str.=" weeks ago";1327}elsif($age>60*60*24*2) {1328$age_str=int$age/60/60/24;1329$age_str.=" days ago";1330}elsif($age>60*60*2) {1331$age_str=int$age/60/60;1332$age_str.=" hours ago";1333}elsif($age>60*2) {1334$age_str=int$age/60;1335$age_str.=" min ago";1336}elsif($age>2) {1337$age_str=int$age;1338$age_str.=" sec ago";1339}else{1340$age_str.=" right now";1341}1342return$age_str;1343}13441345useconstant{1346 S_IFINVALID =>0030000,1347 S_IFGITLINK =>0160000,1348};13491350# submodule/subproject, a commit object reference1351sub S_ISGITLINK {1352my$mode=shift;13531354return(($mode& S_IFMT) == S_IFGITLINK)1355}13561357# convert file mode in octal to symbolic file mode string1358sub mode_str {1359my$mode=oct shift;13601361if(S_ISGITLINK($mode)) {1362return'm---------';1363}elsif(S_ISDIR($mode& S_IFMT)) {1364return'drwxr-xr-x';1365}elsif(S_ISLNK($mode)) {1366return'lrwxrwxrwx';1367}elsif(S_ISREG($mode)) {1368# git cares only about the executable bit1369if($mode& S_IXUSR) {1370return'-rwxr-xr-x';1371}else{1372return'-rw-r--r--';1373};1374}else{1375return'----------';1376}1377}13781379# convert file mode in octal to file type string1380sub file_type {1381my$mode=shift;13821383if($mode!~m/^[0-7]+$/) {1384return$mode;1385}else{1386$mode=oct$mode;1387}13881389if(S_ISGITLINK($mode)) {1390return"submodule";1391}elsif(S_ISDIR($mode& S_IFMT)) {1392return"directory";1393}elsif(S_ISLNK($mode)) {1394return"symlink";1395}elsif(S_ISREG($mode)) {1396return"file";1397}else{1398return"unknown";1399}1400}14011402# convert file mode in octal to file type description string1403sub file_type_long {1404my$mode=shift;14051406if($mode!~m/^[0-7]+$/) {1407return$mode;1408}else{1409$mode=oct$mode;1410}14111412if(S_ISGITLINK($mode)) {1413return"submodule";1414}elsif(S_ISDIR($mode& S_IFMT)) {1415return"directory";1416}elsif(S_ISLNK($mode)) {1417return"symlink";1418}elsif(S_ISREG($mode)) {1419if($mode& S_IXUSR) {1420return"executable";1421}else{1422return"file";1423};1424}else{1425return"unknown";1426}1427}142814291430## ----------------------------------------------------------------------1431## functions returning short HTML fragments, or transforming HTML fragments1432## which don't belong to other sections14331434# format line of commit message.1435sub format_log_line_html {1436my$line=shift;14371438$line= esc_html($line, -nbsp=>1);1439$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1440$cgi->a({-href => href(action=>"object", hash=>$1),1441-class=>"text"},$1);1442}eg;14431444return$line;1445}14461447# format marker of refs pointing to given object14481449# the destination action is chosen based on object type and current context:1450# - for annotated tags, we choose the tag view unless it's the current view1451# already, in which case we go to shortlog view1452# - for other refs, we keep the current view if we're in history, shortlog or1453# log view, and select shortlog otherwise1454sub format_ref_marker {1455my($refs,$id) =@_;1456my$markers='';14571458if(defined$refs->{$id}) {1459foreachmy$ref(@{$refs->{$id}}) {1460# this code exploits the fact that non-lightweight tags are the1461# only indirect objects, and that they are the only objects for which1462# we want to use tag instead of shortlog as action1463my($type,$name) =qw();1464my$indirect= ($ref=~s/\^\{\}$//);1465# e.g. tags/v2.6.11 or heads/next1466if($ref=~m!^(.*?)s?/(.*)$!) {1467$type=$1;1468$name=$2;1469}else{1470$type="ref";1471$name=$ref;1472}14731474my$class=$type;1475$class.=" indirect"if$indirect;14761477my$dest_action="shortlog";14781479if($indirect) {1480$dest_action="tag"unless$actioneq"tag";1481}elsif($action=~/^(history|(short)?log)$/) {1482$dest_action=$action;1483}14841485my$dest="";1486$dest.="refs/"unless$ref=~ m!^refs/!;1487$dest.=$ref;14881489my$link=$cgi->a({1490-href => href(1491 action=>$dest_action,1492 hash=>$dest1493)},$name);14941495$markers.=" <span class=\"$class\"title=\"$ref\">".1496$link."</span>";1497}1498}14991500if($markers) {1501return' <span class="refs">'.$markers.'</span>';1502}else{1503return"";1504}1505}15061507# format, perhaps shortened and with markers, title line1508sub format_subject_html {1509my($long,$short,$href,$extra) =@_;1510$extra=''unlessdefined($extra);15111512if(length($short) <length($long)) {1513$long=~s/[[:cntrl:]]/?/g;1514return$cgi->a({-href =>$href, -class=>"list subject",1515-title => to_utf8($long)},1516 esc_html($short) .$extra);1517}else{1518return$cgi->a({-href =>$href, -class=>"list subject"},1519 esc_html($long) .$extra);1520}1521}15221523# Rather than recomputing the url for an email multiple times, we cache it1524# after the first hit. This gives a visible benefit in views where the avatar1525# for the same email is used repeatedly (e.g. shortlog).1526# The cache is shared by all avatar engines (currently gravatar only), which1527# are free to use it as preferred. Since only one avatar engine is used for any1528# given page, there's no risk for cache conflicts.1529our%avatar_cache= ();15301531# Compute the picon url for a given email, by using the picon search service over at1532# http://www.cs.indiana.edu/picons/search.html1533sub picon_url {1534my$email=lc shift;1535if(!$avatar_cache{$email}) {1536my($user,$domain) =split('@',$email);1537$avatar_cache{$email} =1538"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1539"$domain/$user/".1540"users+domains+unknown/up/single";1541}1542return$avatar_cache{$email};1543}15441545# Compute the gravatar url for a given email, if it's not in the cache already.1546# Gravatar stores only the part of the URL before the size, since that's the1547# one computationally more expensive. This also allows reuse of the cache for1548# different sizes (for this particular engine).1549sub gravatar_url {1550my$email=lc shift;1551my$size=shift;1552$avatar_cache{$email} ||=1553"http://www.gravatar.com/avatar/".1554 Digest::MD5::md5_hex($email) ."?s=";1555return$avatar_cache{$email} .$size;1556}15571558# Insert an avatar for the given $email at the given $size if the feature1559# is enabled.1560sub git_get_avatar {1561my($email,%opts) =@_;1562my$pre_white= ($opts{-pad_before} ?" ":"");1563my$post_white= ($opts{-pad_after} ?" ":"");1564$opts{-size} ||='default';1565my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1566my$url="";1567if($git_avatareq'gravatar') {1568$url= gravatar_url($email,$size);1569}elsif($git_avatareq'picon') {1570$url= picon_url($email);1571}1572# Other providers can be added by extending the if chain, defining $url1573# as needed. If no variant puts something in $url, we assume avatars1574# are completely disabled/unavailable.1575if($url) {1576return$pre_white.1577"<img width=\"$size\"".1578"class=\"avatar\"".1579"src=\"$url\"".1580"alt=\"\"".1581"/>".$post_white;1582}else{1583return"";1584}1585}15861587# format the author name of the given commit with the given tag1588# the author name is chopped and escaped according to the other1589# optional parameters (see chop_str).1590sub format_author_html {1591my$tag=shift;1592my$co=shift;1593my$author= chop_and_escape_str($co->{'author_name'},@_);1594return"<$tagclass=\"author\">".1595 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1596$author."</$tag>";1597}15981599# format git diff header line, i.e. "diff --(git|combined|cc) ..."1600sub format_git_diff_header_line {1601my$line=shift;1602my$diffinfo=shift;1603my($from,$to) =@_;16041605if($diffinfo->{'nparents'}) {1606# combined diff1607$line=~s!^(diff (.*?) )"?.*$!$1!;1608if($to->{'href'}) {1609$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1610 esc_path($to->{'file'}));1611}else{# file was deleted (no href)1612$line.= esc_path($to->{'file'});1613}1614}else{1615# "ordinary" diff1616$line=~s!^(diff (.*?) )"?a/.*$!$1!;1617if($from->{'href'}) {1618$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1619'a/'. esc_path($from->{'file'}));1620}else{# file was added (no href)1621$line.='a/'. esc_path($from->{'file'});1622}1623$line.=' ';1624if($to->{'href'}) {1625$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1626'b/'. esc_path($to->{'file'}));1627}else{# file was deleted1628$line.='b/'. esc_path($to->{'file'});1629}1630}16311632return"<div class=\"diff header\">$line</div>\n";1633}16341635# format extended diff header line, before patch itself1636sub format_extended_diff_header_line {1637my$line=shift;1638my$diffinfo=shift;1639my($from,$to) =@_;16401641# match <path>1642if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1643$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1644 esc_path($from->{'file'}));1645}1646if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1647$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1648 esc_path($to->{'file'}));1649}1650# match single <mode>1651if($line=~m/\s(\d{6})$/) {1652$line.='<span class="info"> ('.1653 file_type_long($1) .1654')</span>';1655}1656# match <hash>1657if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1658# can match only for combined diff1659$line='index ';1660for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1661if($from->{'href'}[$i]) {1662$line.=$cgi->a({-href=>$from->{'href'}[$i],1663-class=>"hash"},1664substr($diffinfo->{'from_id'}[$i],0,7));1665}else{1666$line.='0' x 7;1667}1668# separator1669$line.=','if($i<$diffinfo->{'nparents'} -1);1670}1671$line.='..';1672if($to->{'href'}) {1673$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1674substr($diffinfo->{'to_id'},0,7));1675}else{1676$line.='0' x 7;1677}16781679}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1680# can match only for ordinary diff1681my($from_link,$to_link);1682if($from->{'href'}) {1683$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1684substr($diffinfo->{'from_id'},0,7));1685}else{1686$from_link='0' x 7;1687}1688if($to->{'href'}) {1689$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1690substr($diffinfo->{'to_id'},0,7));1691}else{1692$to_link='0' x 7;1693}1694my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1695$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1696}16971698return$line."<br/>\n";1699}17001701# format from-file/to-file diff header1702sub format_diff_from_to_header {1703my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1704my$line;1705my$result='';17061707$line=$from_line;1708#assert($line =~ m/^---/) if DEBUG;1709# no extra formatting for "^--- /dev/null"1710if(!$diffinfo->{'nparents'}) {1711# ordinary (single parent) diff1712if($line=~m!^--- "?a/!) {1713if($from->{'href'}) {1714$line='--- a/'.1715$cgi->a({-href=>$from->{'href'}, -class=>"path"},1716 esc_path($from->{'file'}));1717}else{1718$line='--- a/'.1719 esc_path($from->{'file'});1720}1721}1722$result.= qq!<div class="diff from_file">$line</div>\n!;17231724}else{1725# combined diff (merge commit)1726for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1727if($from->{'href'}[$i]) {1728$line='--- '.1729$cgi->a({-href=>href(action=>"blobdiff",1730 hash_parent=>$diffinfo->{'from_id'}[$i],1731 hash_parent_base=>$parents[$i],1732 file_parent=>$from->{'file'}[$i],1733 hash=>$diffinfo->{'to_id'},1734 hash_base=>$hash,1735 file_name=>$to->{'file'}),1736-class=>"path",1737-title=>"diff". ($i+1)},1738$i+1) .1739'/'.1740$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1741 esc_path($from->{'file'}[$i]));1742}else{1743$line='--- /dev/null';1744}1745$result.= qq!<div class="diff from_file">$line</div>\n!;1746}1747}17481749$line=$to_line;1750#assert($line =~ m/^\+\+\+/) if DEBUG;1751# no extra formatting for "^+++ /dev/null"1752if($line=~m!^\+\+\+ "?b/!) {1753if($to->{'href'}) {1754$line='+++ b/'.1755$cgi->a({-href=>$to->{'href'}, -class=>"path"},1756 esc_path($to->{'file'}));1757}else{1758$line='+++ b/'.1759 esc_path($to->{'file'});1760}1761}1762$result.= qq!<div class="diff to_file">$line</div>\n!;17631764return$result;1765}17661767# create note for patch simplified by combined diff1768sub format_diff_cc_simplified {1769my($diffinfo,@parents) =@_;1770my$result='';17711772$result.="<div class=\"diff header\">".1773"diff --cc ";1774if(!is_deleted($diffinfo)) {1775$result.=$cgi->a({-href => href(action=>"blob",1776 hash_base=>$hash,1777 hash=>$diffinfo->{'to_id'},1778 file_name=>$diffinfo->{'to_file'}),1779-class=>"path"},1780 esc_path($diffinfo->{'to_file'}));1781}else{1782$result.= esc_path($diffinfo->{'to_file'});1783}1784$result.="</div>\n".# class="diff header"1785"<div class=\"diff nodifferences\">".1786"Simple merge".1787"</div>\n";# class="diff nodifferences"17881789return$result;1790}17911792# format patch (diff) line (not to be used for diff headers)1793sub format_diff_line {1794my$line=shift;1795my($from,$to) =@_;1796my$diff_class="";17971798chomp$line;17991800if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1801# combined diff1802my$prefix=substr($line,0,scalar@{$from->{'href'}});1803if($line=~m/^\@{3}/) {1804$diff_class=" chunk_header";1805}elsif($line=~m/^\\/) {1806$diff_class=" incomplete";1807}elsif($prefix=~tr/+/+/) {1808$diff_class=" add";1809}elsif($prefix=~tr/-/-/) {1810$diff_class=" rem";1811}1812}else{1813# assume ordinary diff1814my$char=substr($line,0,1);1815if($chareq'+') {1816$diff_class=" add";1817}elsif($chareq'-') {1818$diff_class=" rem";1819}elsif($chareq'@') {1820$diff_class=" chunk_header";1821}elsif($chareq"\\") {1822$diff_class=" incomplete";1823}1824}1825$line= untabify($line);1826if($from&&$to&&$line=~m/^\@{2} /) {1827my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1828$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;18291830$from_lines=0unlessdefined$from_lines;1831$to_lines=0unlessdefined$to_lines;18321833if($from->{'href'}) {1834$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1835-class=>"list"},$from_text);1836}1837if($to->{'href'}) {1838$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1839-class=>"list"},$to_text);1840}1841$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1842"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1843return"<div class=\"diff$diff_class\">$line</div>\n";1844}elsif($from&&$to&&$line=~m/^\@{3}/) {1845my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1846my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);18471848@from_text=split(' ',$ranges);1849for(my$i=0;$i<@from_text; ++$i) {1850($from_start[$i],$from_nlines[$i]) =1851(split(',',substr($from_text[$i],1)),0);1852}18531854$to_text=pop@from_text;1855$to_start=pop@from_start;1856$to_nlines=pop@from_nlines;18571858$line="<span class=\"chunk_info\">$prefix";1859for(my$i=0;$i<@from_text; ++$i) {1860if($from->{'href'}[$i]) {1861$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1862-class=>"list"},$from_text[$i]);1863}else{1864$line.=$from_text[$i];1865}1866$line.=" ";1867}1868if($to->{'href'}) {1869$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1870-class=>"list"},$to_text);1871}else{1872$line.=$to_text;1873}1874$line.="$prefix</span>".1875"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1876return"<div class=\"diff$diff_class\">$line</div>\n";1877}1878return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1879}18801881# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1882# linked. Pass the hash of the tree/commit to snapshot.1883sub format_snapshot_links {1884my($hash) =@_;1885my$num_fmts=@snapshot_fmts;1886if($num_fmts>1) {1887# A parenthesized list of links bearing format names.1888# e.g. "snapshot (_tar.gz_ _zip_)"1889return"snapshot (".join(' ',map1890$cgi->a({1891-href => href(1892 action=>"snapshot",1893 hash=>$hash,1894 snapshot_format=>$_1895)1896},$known_snapshot_formats{$_}{'display'})1897,@snapshot_fmts) .")";1898}elsif($num_fmts==1) {1899# A single "snapshot" link whose tooltip bears the format name.1900# i.e. "_snapshot_"1901my($fmt) =@snapshot_fmts;1902return1903$cgi->a({1904-href => href(1905 action=>"snapshot",1906 hash=>$hash,1907 snapshot_format=>$fmt1908),1909-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1910},"snapshot");1911}else{# $num_fmts == 01912returnundef;1913}1914}19151916## ......................................................................1917## functions returning values to be passed, perhaps after some1918## transformation, to other functions; e.g. returning arguments to href()19191920# returns hash to be passed to href to generate gitweb URL1921# in -title key it returns description of link1922sub get_feed_info {1923my$format=shift||'Atom';1924my%res= (action =>lc($format));19251926# feed links are possible only for project views1927return unless(defined$project);1928# some views should link to OPML, or to generic project feed,1929# or don't have specific feed yet (so they should use generic)1930return if($action=~/^(?:tags|heads|forks|tag|search)$/x);19311932my$branch;1933# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1934# from tag links; this also makes possible to detect branch links1935if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1936(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1937$branch=$1;1938}1939# find log type for feed description (title)1940my$type='log';1941if(defined$file_name) {1942$type="history of$file_name";1943$type.="/"if($actioneq'tree');1944$type.=" on '$branch'"if(defined$branch);1945}else{1946$type="log of$branch"if(defined$branch);1947}19481949$res{-title} =$type;1950$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1951$res{'file_name'} =$file_name;19521953return%res;1954}19551956## ----------------------------------------------------------------------1957## git utility subroutines, invoking git commands19581959# returns path to the core git executable and the --git-dir parameter as list1960sub git_cmd {1961return$GIT,'--git-dir='.$git_dir;1962}19631964# quote the given arguments for passing them to the shell1965# quote_command("command", "arg 1", "arg with ' and ! characters")1966# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1967# Try to avoid using this function wherever possible.1968sub quote_command {1969returnjoin(' ',1970map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);1971}19721973# get HEAD ref of given project as hash1974sub git_get_head_hash {1975my$project=shift;1976my$o_git_dir=$git_dir;1977my$retval=undef;1978$git_dir="$projectroot/$project";1979if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1980my$head= <$fd>;1981close$fd;1982if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1983$retval=$1;1984}1985}1986if(defined$o_git_dir) {1987$git_dir=$o_git_dir;1988}1989return$retval;1990}19911992# get type of given object1993sub git_get_type {1994my$hash=shift;19951996open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1997my$type= <$fd>;1998close$fdorreturn;1999chomp$type;2000return$type;2001}20022003# repository configuration2004our$config_file='';2005our%config;20062007# store multiple values for single key as anonymous array reference2008# single values stored directly in the hash, not as [ <value> ]2009sub hash_set_multi {2010my($hash,$key,$value) =@_;20112012if(!exists$hash->{$key}) {2013$hash->{$key} =$value;2014}elsif(!ref$hash->{$key}) {2015$hash->{$key} = [$hash->{$key},$value];2016}else{2017push@{$hash->{$key}},$value;2018}2019}20202021# return hash of git project configuration2022# optionally limited to some section, e.g. 'gitweb'2023sub git_parse_project_config {2024my$section_regexp=shift;2025my%config;20262027local$/="\0";20282029open my$fh,"-|", git_cmd(),"config",'-z','-l',2030orreturn;20312032while(my$keyval= <$fh>) {2033chomp$keyval;2034my($key,$value) =split(/\n/,$keyval,2);20352036 hash_set_multi(\%config,$key,$value)2037if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2038}2039close$fh;20402041return%config;2042}20432044# convert config value to boolean: 'true' or 'false'2045# no value, number > 0, 'true' and 'yes' values are true2046# rest of values are treated as false (never as error)2047sub config_to_bool {2048my$val=shift;20492050return1if!defined$val;# section.key20512052# strip leading and trailing whitespace2053$val=~s/^\s+//;2054$val=~s/\s+$//;20552056return(($val=~/^\d+$/&&$val) ||# section.key = 12057($val=~/^(?:true|yes)$/i));# section.key = true2058}20592060# convert config value to simple decimal number2061# an optional value suffix of 'k', 'm', or 'g' will cause the value2062# to be multiplied by 1024, 1048576, or 10737418242063sub config_to_int {2064my$val=shift;20652066# strip leading and trailing whitespace2067$val=~s/^\s+//;2068$val=~s/\s+$//;20692070if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2071$unit=lc($unit);2072# unknown unit is treated as 12073return$num* ($uniteq'g'?1073741824:2074$uniteq'm'?1048576:2075$uniteq'k'?1024:1);2076}2077return$val;2078}20792080# convert config value to array reference, if needed2081sub config_to_multi {2082my$val=shift;20832084returnref($val) ?$val: (defined($val) ? [$val] : []);2085}20862087sub git_get_project_config {2088my($key,$type) =@_;20892090# key sanity check2091return unless($key);2092$key=~s/^gitweb\.//;2093return if($key=~m/\W/);20942095# type sanity check2096if(defined$type) {2097$type=~s/^--//;2098$type=undef2099unless($typeeq'bool'||$typeeq'int');2100}21012102# get config2103if(!defined$config_file||2104$config_filene"$git_dir/config") {2105%config= git_parse_project_config('gitweb');2106$config_file="$git_dir/config";2107}21082109# check if config variable (key) exists2110return unlessexists$config{"gitweb.$key"};21112112# ensure given type2113if(!defined$type) {2114return$config{"gitweb.$key"};2115}elsif($typeeq'bool') {2116# backward compatibility: 'git config --bool' returns true/false2117return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2118}elsif($typeeq'int') {2119return config_to_int($config{"gitweb.$key"});2120}2121return$config{"gitweb.$key"};2122}21232124# get hash of given path at given ref2125sub git_get_hash_by_path {2126my$base=shift;2127my$path=shift||returnundef;2128my$type=shift;21292130$path=~ s,/+$,,;21312132open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2133or die_error(500,"Open git-ls-tree failed");2134my$line= <$fd>;2135close$fdorreturnundef;21362137if(!defined$line) {2138# there is no tree or hash given by $path at $base2139returnundef;2140}21412142#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2143$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2144if(defined$type&&$typene$2) {2145# type doesn't match2146returnundef;2147}2148return$3;2149}21502151# get path of entry with given hash at given tree-ish (ref)2152# used to get 'from' filename for combined diff (merge commit) for renames2153sub git_get_path_by_hash {2154my$base=shift||return;2155my$hash=shift||return;21562157local$/="\0";21582159open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2160orreturnundef;2161while(my$line= <$fd>) {2162chomp$line;21632164#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2165#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2166if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2167close$fd;2168return$1;2169}2170}2171close$fd;2172returnundef;2173}21742175## ......................................................................2176## git utility functions, directly accessing git repository21772178sub git_get_project_description {2179my$path=shift;21802181$git_dir="$projectroot/$path";2182open my$fd,'<',"$git_dir/description"2183orreturn git_get_project_config('description');2184my$descr= <$fd>;2185close$fd;2186if(defined$descr) {2187chomp$descr;2188}2189return$descr;2190}21912192sub git_get_project_ctags {2193my$path=shift;2194my$ctags= {};21952196$git_dir="$projectroot/$path";2197opendir my$dh,"$git_dir/ctags"2198orreturn$ctags;2199foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2200open my$ct,'<',$_ornext;2201my$val= <$ct>;2202chomp$val;2203close$ct;2204my$ctag=$_;$ctag=~ s#.*/##;2205$ctags->{$ctag} =$val;2206}2207closedir$dh;2208$ctags;2209}22102211sub git_populate_project_tagcloud {2212my$ctags=shift;22132214# First, merge different-cased tags; tags vote on casing2215my%ctags_lc;2216foreach(keys%$ctags) {2217$ctags_lc{lc$_}->{count} +=$ctags->{$_};2218if(not$ctags_lc{lc$_}->{topcount}2219or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2220$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2221$ctags_lc{lc$_}->{topname} =$_;2222}2223}22242225my$cloud;2226if(eval{require HTML::TagCloud;1; }) {2227$cloud= HTML::TagCloud->new;2228foreach(sort keys%ctags_lc) {2229# Pad the title with spaces so that the cloud looks2230# less crammed.2231my$title=$ctags_lc{$_}->{topname};2232$title=~s/ / /g;2233$title=~s/^/ /g;2234$title=~s/$/ /g;2235$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2236}2237}else{2238$cloud= \%ctags_lc;2239}2240$cloud;2241}22422243sub git_show_project_tagcloud {2244my($cloud,$count) =@_;2245print STDERR ref($cloud)."..\n";2246if(ref$cloudeq'HTML::TagCloud') {2247return$cloud->html_and_css($count);2248}else{2249my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2250return'<p align="center">'.join(', ',map{2251"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2252}splice(@tags,0,$count)) .'</p>';2253}2254}22552256sub git_get_project_url_list {2257my$path=shift;22582259$git_dir="$projectroot/$path";2260open my$fd,'<',"$git_dir/cloneurl"2261orreturnwantarray?2262@{ config_to_multi(git_get_project_config('url')) } :2263 config_to_multi(git_get_project_config('url'));2264my@git_project_url_list=map{chomp;$_} <$fd>;2265close$fd;22662267returnwantarray?@git_project_url_list: \@git_project_url_list;2268}22692270sub git_get_projects_list {2271my($filter) =@_;2272my@list;22732274$filter||='';2275$filter=~s/\.git$//;22762277my$check_forks= gitweb_check_feature('forks');22782279if(-d $projects_list) {2280# search in directory2281my$dir=$projects_list. ($filter?"/$filter":'');2282# remove the trailing "/"2283$dir=~s!/+$!!;2284my$pfxlen=length("$dir");2285my$pfxdepth= ($dir=~tr!/!!);22862287 File::Find::find({2288 follow_fast =>1,# follow symbolic links2289 follow_skip =>2,# ignore duplicates2290 dangling_symlinks =>0,# ignore dangling symlinks, silently2291 wanted =>sub{2292# skip project-list toplevel, if we get it.2293return if(m!^[/.]$!);2294# only directories can be git repositories2295return unless(-d $_);2296# don't traverse too deep (Find is super slow on os x)2297if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2298$File::Find::prune =1;2299return;2300}23012302my$subdir=substr($File::Find::name,$pfxlen+1);2303# we check related file in $projectroot2304my$path= ($filter?"$filter/":'') .$subdir;2305if(check_export_ok("$projectroot/$path")) {2306push@list, { path =>$path};2307$File::Find::prune =1;2308}2309},2310},"$dir");23112312}elsif(-f $projects_list) {2313# read from file(url-encoded):2314# 'git%2Fgit.git Linus+Torvalds'2315# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2316# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2317my%paths;2318open my$fd,'<',$projects_listorreturn;2319 PROJECT:2320while(my$line= <$fd>) {2321chomp$line;2322my($path,$owner) =split' ',$line;2323$path= unescape($path);2324$owner= unescape($owner);2325if(!defined$path) {2326next;2327}2328if($filterne'') {2329# looking for forks;2330my$pfx=substr($path,0,length($filter));2331if($pfxne$filter) {2332next PROJECT;2333}2334my$sfx=substr($path,length($filter));2335if($sfx!~/^\/.*\.git$/) {2336next PROJECT;2337}2338}elsif($check_forks) {2339 PATH:2340foreachmy$filter(keys%paths) {2341# looking for forks;2342my$pfx=substr($path,0,length($filter));2343if($pfxne$filter) {2344next PATH;2345}2346my$sfx=substr($path,length($filter));2347if($sfx!~/^\/.*\.git$/) {2348next PATH;2349}2350# is a fork, don't include it in2351# the list2352next PROJECT;2353}2354}2355if(check_export_ok("$projectroot/$path")) {2356my$pr= {2357 path =>$path,2358 owner => to_utf8($owner),2359};2360push@list,$pr;2361(my$forks_path=$path) =~s/\.git$//;2362$paths{$forks_path}++;2363}2364}2365close$fd;2366}2367return@list;2368}23692370our$gitweb_project_owner=undef;2371sub git_get_project_list_from_file {23722373return if(defined$gitweb_project_owner);23742375$gitweb_project_owner= {};2376# read from file (url-encoded):2377# 'git%2Fgit.git Linus+Torvalds'2378# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2379# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2380if(-f $projects_list) {2381open(my$fd,'<',$projects_list);2382while(my$line= <$fd>) {2383chomp$line;2384my($pr,$ow) =split' ',$line;2385$pr= unescape($pr);2386$ow= unescape($ow);2387$gitweb_project_owner->{$pr} = to_utf8($ow);2388}2389close$fd;2390}2391}23922393sub git_get_project_owner {2394my$project=shift;2395my$owner;23962397returnundefunless$project;2398$git_dir="$projectroot/$project";23992400if(!defined$gitweb_project_owner) {2401 git_get_project_list_from_file();2402}24032404if(exists$gitweb_project_owner->{$project}) {2405$owner=$gitweb_project_owner->{$project};2406}2407if(!defined$owner){2408$owner= git_get_project_config('owner');2409}2410if(!defined$owner) {2411$owner= get_file_owner("$git_dir");2412}24132414return$owner;2415}24162417sub git_get_last_activity {2418my($path) =@_;2419my$fd;24202421$git_dir="$projectroot/$path";2422open($fd,"-|", git_cmd(),'for-each-ref',2423'--format=%(committer)',2424'--sort=-committerdate',2425'--count=1',2426'refs/heads')orreturn;2427my$most_recent= <$fd>;2428close$fdorreturn;2429if(defined$most_recent&&2430$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2431my$timestamp=$1;2432my$age=time-$timestamp;2433return($age, age_string($age));2434}2435return(undef,undef);2436}24372438sub git_get_references {2439my$type=shift||"";2440my%refs;2441# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112442# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2443open my$fd,"-|", git_cmd(),"show-ref","--dereference",2444($type? ("--","refs/$type") : ())# use -- <pattern> if $type2445orreturn;24462447while(my$line= <$fd>) {2448chomp$line;2449if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2450if(defined$refs{$1}) {2451push@{$refs{$1}},$2;2452}else{2453$refs{$1} = [$2];2454}2455}2456}2457close$fdorreturn;2458return \%refs;2459}24602461sub git_get_rev_name_tags {2462my$hash=shift||returnundef;24632464open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2465orreturn;2466my$name_rev= <$fd>;2467close$fd;24682469if($name_rev=~ m|^$hash tags/(.*)$|) {2470return$1;2471}else{2472# catches also '$hash undefined' output2473returnundef;2474}2475}24762477## ----------------------------------------------------------------------2478## parse to hash functions24792480sub parse_date {2481my$epoch=shift;2482my$tz=shift||"-0000";24832484my%date;2485my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2486my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2487my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2488$date{'hour'} =$hour;2489$date{'minute'} =$min;2490$date{'mday'} =$mday;2491$date{'day'} =$days[$wday];2492$date{'month'} =$months[$mon];2493$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2494$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2495$date{'mday-time'} =sprintf"%d%s%02d:%02d",2496$mday,$months[$mon],$hour,$min;2497$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",24981900+$year,1+$mon,$mday,$hour,$min,$sec;24992500$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2501my$local=$epoch+ ((int$1+ ($2/60)) *3600);2502($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2503$date{'hour_local'} =$hour;2504$date{'minute_local'} =$min;2505$date{'tz_local'} =$tz;2506$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",25071900+$year,$mon+1,$mday,2508$hour,$min,$sec,$tz);2509return%date;2510}25112512sub parse_tag {2513my$tag_id=shift;2514my%tag;2515my@comment;25162517open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2518$tag{'id'} =$tag_id;2519while(my$line= <$fd>) {2520chomp$line;2521if($line=~m/^object ([0-9a-fA-F]{40})$/) {2522$tag{'object'} =$1;2523}elsif($line=~m/^type (.+)$/) {2524$tag{'type'} =$1;2525}elsif($line=~m/^tag (.+)$/) {2526$tag{'name'} =$1;2527}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2528$tag{'author'} =$1;2529$tag{'author_epoch'} =$2;2530$tag{'author_tz'} =$3;2531if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2532$tag{'author_name'} =$1;2533$tag{'author_email'} =$2;2534}else{2535$tag{'author_name'} =$tag{'author'};2536}2537}elsif($line=~m/--BEGIN/) {2538push@comment,$line;2539last;2540}elsif($lineeq"") {2541last;2542}2543}2544push@comment, <$fd>;2545$tag{'comment'} = \@comment;2546close$fdorreturn;2547if(!defined$tag{'name'}) {2548return2549};2550return%tag2551}25522553sub parse_commit_text {2554my($commit_text,$withparents) =@_;2555my@commit_lines=split'\n',$commit_text;2556my%co;25572558pop@commit_lines;# Remove '\0'25592560if(!@commit_lines) {2561return;2562}25632564my$header=shift@commit_lines;2565if($header!~m/^[0-9a-fA-F]{40}/) {2566return;2567}2568($co{'id'},my@parents) =split' ',$header;2569while(my$line=shift@commit_lines) {2570last if$lineeq"\n";2571if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2572$co{'tree'} =$1;2573}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2574push@parents,$1;2575}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2576$co{'author'} =$1;2577$co{'author_epoch'} =$2;2578$co{'author_tz'} =$3;2579if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2580$co{'author_name'} =$1;2581$co{'author_email'} =$2;2582}else{2583$co{'author_name'} =$co{'author'};2584}2585}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2586$co{'committer'} =$1;2587$co{'committer_epoch'} =$2;2588$co{'committer_tz'} =$3;2589$co{'committer_name'} =$co{'committer'};2590if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2591$co{'committer_name'} =$1;2592$co{'committer_email'} =$2;2593}else{2594$co{'committer_name'} =$co{'committer'};2595}2596}2597}2598if(!defined$co{'tree'}) {2599return;2600};2601$co{'parents'} = \@parents;2602$co{'parent'} =$parents[0];26032604foreachmy$title(@commit_lines) {2605$title=~s/^ //;2606if($titlene"") {2607$co{'title'} = chop_str($title,80,5);2608# remove leading stuff of merges to make the interesting part visible2609if(length($title) >50) {2610$title=~s/^Automatic //;2611$title=~s/^merge (of|with) /Merge ... /i;2612if(length($title) >50) {2613$title=~s/(http|rsync):\/\///;2614}2615if(length($title) >50) {2616$title=~s/(master|www|rsync)\.//;2617}2618if(length($title) >50) {2619$title=~s/kernel.org:?//;2620}2621if(length($title) >50) {2622$title=~s/\/pub\/scm//;2623}2624}2625$co{'title_short'} = chop_str($title,50,5);2626last;2627}2628}2629if(!defined$co{'title'} ||$co{'title'}eq"") {2630$co{'title'} =$co{'title_short'} ='(no commit message)';2631}2632# remove added spaces2633foreachmy$line(@commit_lines) {2634$line=~s/^ //;2635}2636$co{'comment'} = \@commit_lines;26372638my$age=time-$co{'committer_epoch'};2639$co{'age'} =$age;2640$co{'age_string'} = age_string($age);2641my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2642if($age>60*60*24*7*2) {2643$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2644$co{'age_string_age'} =$co{'age_string'};2645}else{2646$co{'age_string_date'} =$co{'age_string'};2647$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2648}2649return%co;2650}26512652sub parse_commit {2653my($commit_id) =@_;2654my%co;26552656local$/="\0";26572658open my$fd,"-|", git_cmd(),"rev-list",2659"--parents",2660"--header",2661"--max-count=1",2662$commit_id,2663"--",2664or die_error(500,"Open git-rev-list failed");2665%co= parse_commit_text(<$fd>,1);2666close$fd;26672668return%co;2669}26702671sub parse_commits {2672my($commit_id,$maxcount,$skip,$filename,@args) =@_;2673my@cos;26742675$maxcount||=1;2676$skip||=0;26772678local$/="\0";26792680open my$fd,"-|", git_cmd(),"rev-list",2681"--header",2682@args,2683("--max-count=".$maxcount),2684("--skip=".$skip),2685@extra_options,2686$commit_id,2687"--",2688($filename? ($filename) : ())2689or die_error(500,"Open git-rev-list failed");2690while(my$line= <$fd>) {2691my%co= parse_commit_text($line);2692push@cos, \%co;2693}2694close$fd;26952696returnwantarray?@cos: \@cos;2697}26982699# parse line of git-diff-tree "raw" output2700sub parse_difftree_raw_line {2701my$line=shift;2702my%res;27032704# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2705# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2706if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2707$res{'from_mode'} =$1;2708$res{'to_mode'} =$2;2709$res{'from_id'} =$3;2710$res{'to_id'} =$4;2711$res{'status'} =$5;2712$res{'similarity'} =$6;2713if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2714($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2715}else{2716$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2717}2718}2719# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2720# combined diff (for merge commit)2721elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2722$res{'nparents'} =length($1);2723$res{'from_mode'} = [split(' ',$2) ];2724$res{'to_mode'} =pop@{$res{'from_mode'}};2725$res{'from_id'} = [split(' ',$3) ];2726$res{'to_id'} =pop@{$res{'from_id'}};2727$res{'status'} = [split('',$4) ];2728$res{'to_file'} = unquote($5);2729}2730# 'c512b523472485aef4fff9e57b229d9d243c967f'2731elsif($line=~m/^([0-9a-fA-F]{40})$/) {2732$res{'commit'} =$1;2733}27342735returnwantarray?%res: \%res;2736}27372738# wrapper: return parsed line of git-diff-tree "raw" output2739# (the argument might be raw line, or parsed info)2740sub parsed_difftree_line {2741my$line_or_ref=shift;27422743if(ref($line_or_ref)eq"HASH") {2744# pre-parsed (or generated by hand)2745return$line_or_ref;2746}else{2747return parse_difftree_raw_line($line_or_ref);2748}2749}27502751# parse line of git-ls-tree output2752sub parse_ls_tree_line {2753my$line=shift;2754my%opts=@_;2755my%res;27562757#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2758$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;27592760$res{'mode'} =$1;2761$res{'type'} =$2;2762$res{'hash'} =$3;2763if($opts{'-z'}) {2764$res{'name'} =$4;2765}else{2766$res{'name'} = unquote($4);2767}27682769returnwantarray?%res: \%res;2770}27712772# generates _two_ hashes, references to which are passed as 2 and 3 argument2773sub parse_from_to_diffinfo {2774my($diffinfo,$from,$to,@parents) =@_;27752776if($diffinfo->{'nparents'}) {2777# combined diff2778$from->{'file'} = [];2779$from->{'href'} = [];2780 fill_from_file_info($diffinfo,@parents)2781unlessexists$diffinfo->{'from_file'};2782for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2783$from->{'file'}[$i] =2784defined$diffinfo->{'from_file'}[$i] ?2785$diffinfo->{'from_file'}[$i] :2786$diffinfo->{'to_file'};2787if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2788$from->{'href'}[$i] = href(action=>"blob",2789 hash_base=>$parents[$i],2790 hash=>$diffinfo->{'from_id'}[$i],2791 file_name=>$from->{'file'}[$i]);2792}else{2793$from->{'href'}[$i] =undef;2794}2795}2796}else{2797# ordinary (not combined) diff2798$from->{'file'} =$diffinfo->{'from_file'};2799if($diffinfo->{'status'}ne"A") {# not new (added) file2800$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2801 hash=>$diffinfo->{'from_id'},2802 file_name=>$from->{'file'});2803}else{2804delete$from->{'href'};2805}2806}28072808$to->{'file'} =$diffinfo->{'to_file'};2809if(!is_deleted($diffinfo)) {# file exists in result2810$to->{'href'} = href(action=>"blob", hash_base=>$hash,2811 hash=>$diffinfo->{'to_id'},2812 file_name=>$to->{'file'});2813}else{2814delete$to->{'href'};2815}2816}28172818## ......................................................................2819## parse to array of hashes functions28202821sub git_get_heads_list {2822my$limit=shift;2823my@headslist;28242825open my$fd,'-|', git_cmd(),'for-each-ref',2826($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2827'--format=%(objectname) %(refname) %(subject)%00%(committer)',2828'refs/heads'2829orreturn;2830while(my$line= <$fd>) {2831my%ref_item;28322833chomp$line;2834my($refinfo,$committerinfo) =split(/\0/,$line);2835my($hash,$name,$title) =split(' ',$refinfo,3);2836my($committer,$epoch,$tz) =2837($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2838$ref_item{'fullname'} =$name;2839$name=~s!^refs/heads/!!;28402841$ref_item{'name'} =$name;2842$ref_item{'id'} =$hash;2843$ref_item{'title'} =$title||'(no commit message)';2844$ref_item{'epoch'} =$epoch;2845if($epoch) {2846$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2847}else{2848$ref_item{'age'} ="unknown";2849}28502851push@headslist, \%ref_item;2852}2853close$fd;28542855returnwantarray?@headslist: \@headslist;2856}28572858sub git_get_tags_list {2859my$limit=shift;2860my@tagslist;28612862open my$fd,'-|', git_cmd(),'for-each-ref',2863($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2864'--format=%(objectname) %(objecttype) %(refname) '.2865'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2866'refs/tags'2867orreturn;2868while(my$line= <$fd>) {2869my%ref_item;28702871chomp$line;2872my($refinfo,$creatorinfo) =split(/\0/,$line);2873my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2874my($creator,$epoch,$tz) =2875($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2876$ref_item{'fullname'} =$name;2877$name=~s!^refs/tags/!!;28782879$ref_item{'type'} =$type;2880$ref_item{'id'} =$id;2881$ref_item{'name'} =$name;2882if($typeeq"tag") {2883$ref_item{'subject'} =$title;2884$ref_item{'reftype'} =$reftype;2885$ref_item{'refid'} =$refid;2886}else{2887$ref_item{'reftype'} =$type;2888$ref_item{'refid'} =$id;2889}28902891if($typeeq"tag"||$typeeq"commit") {2892$ref_item{'epoch'} =$epoch;2893if($epoch) {2894$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2895}else{2896$ref_item{'age'} ="unknown";2897}2898}28992900push@tagslist, \%ref_item;2901}2902close$fd;29032904returnwantarray?@tagslist: \@tagslist;2905}29062907## ----------------------------------------------------------------------2908## filesystem-related functions29092910sub get_file_owner {2911my$path=shift;29122913my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2914my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2915if(!defined$gcos) {2916returnundef;2917}2918my$owner=$gcos;2919$owner=~s/[,;].*$//;2920return to_utf8($owner);2921}29222923# assume that file exists2924sub insert_file {2925my$filename=shift;29262927open my$fd,'<',$filename;2928print map{ to_utf8($_) } <$fd>;2929close$fd;2930}29312932## ......................................................................2933## mimetype related functions29342935sub mimetype_guess_file {2936my$filename=shift;2937my$mimemap=shift;2938-r $mimemaporreturnundef;29392940my%mimemap;2941open(my$mh,'<',$mimemap)orreturnundef;2942while(<$mh>) {2943next ifm/^#/;# skip comments2944my($mimetype,$exts) =split(/\t+/);2945if(defined$exts) {2946my@exts=split(/\s+/,$exts);2947foreachmy$ext(@exts) {2948$mimemap{$ext} =$mimetype;2949}2950}2951}2952close($mh);29532954$filename=~/\.([^.]*)$/;2955return$mimemap{$1};2956}29572958sub mimetype_guess {2959my$filename=shift;2960my$mime;2961$filename=~/\./orreturnundef;29622963if($mimetypes_file) {2964my$file=$mimetypes_file;2965if($file!~m!^/!) {# if it is relative path2966# it is relative to project2967$file="$projectroot/$project/$file";2968}2969$mime= mimetype_guess_file($filename,$file);2970}2971$mime||= mimetype_guess_file($filename,'/etc/mime.types');2972return$mime;2973}29742975sub blob_mimetype {2976my$fd=shift;2977my$filename=shift;29782979if($filename) {2980my$mime= mimetype_guess($filename);2981$mimeandreturn$mime;2982}29832984# just in case2985return$default_blob_plain_mimetypeunless$fd;29862987if(-T $fd) {2988return'text/plain';2989}elsif(!$filename) {2990return'application/octet-stream';2991}elsif($filename=~m/\.png$/i) {2992return'image/png';2993}elsif($filename=~m/\.gif$/i) {2994return'image/gif';2995}elsif($filename=~m/\.jpe?g$/i) {2996return'image/jpeg';2997}else{2998return'application/octet-stream';2999}3000}30013002sub blob_contenttype {3003my($fd,$file_name,$type) =@_;30043005$type||= blob_mimetype($fd,$file_name);3006if($typeeq'text/plain'&&defined$default_text_plain_charset) {3007$type.="; charset=$default_text_plain_charset";3008}30093010return$type;3011}30123013## ======================================================================3014## functions printing HTML: header, footer, error page30153016sub git_header_html {3017my$status=shift||"200 OK";3018my$expires=shift;30193020my$title="$site_name";3021if(defined$project) {3022$title.=" - ". to_utf8($project);3023if(defined$action) {3024$title.="/$action";3025if(defined$file_name) {3026$title.=" - ". esc_path($file_name);3027if($actioneq"tree"&&$file_name!~ m|/$|) {3028$title.="/";3029}3030}3031}3032}3033my$content_type;3034# require explicit support from the UA if we are to send the page as3035# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3036# we have to do this because MSIE sometimes globs '*/*', pretending to3037# support xhtml+xml but choking when it gets what it asked for.3038if(defined$cgi->http('HTTP_ACCEPT') &&3039$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3040$cgi->Accept('application/xhtml+xml') !=0) {3041$content_type='application/xhtml+xml';3042}else{3043$content_type='text/html';3044}3045print$cgi->header(-type=>$content_type, -charset =>'utf-8',3046-status=>$status, -expires =>$expires);3047my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3048print<<EOF;3049<?xml version="1.0" encoding="utf-8"?>3050<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3051<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3052<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3053<!-- git core binaries version$git_version-->3054<head>3055<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3056<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3057<meta name="robots" content="index, nofollow"/>3058<title>$title</title>3059EOF3060# the stylesheet, favicon etc urls won't work correctly with path_info3061# unless we set the appropriate base URL3062if($ENV{'PATH_INFO'}) {3063print"<base href=\"".esc_url($base_url)."\"/>\n";3064}3065# print out each stylesheet that exist, providing backwards capability3066# for those people who defined $stylesheet in a config file3067if(defined$stylesheet) {3068print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3069}else{3070foreachmy$stylesheet(@stylesheets) {3071next unless$stylesheet;3072print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3073}3074}3075if(defined$project) {3076my%href_params= get_feed_info();3077if(!exists$href_params{'-title'}) {3078$href_params{'-title'} ='log';3079}30803081foreachmy$formatqw(RSS Atom){3082my$type=lc($format);3083my%link_attr= (3084'-rel'=>'alternate',3085'-title'=>"$project-$href_params{'-title'} -$formatfeed",3086'-type'=>"application/$type+xml"3087);30883089$href_params{'action'} =$type;3090$link_attr{'-href'} = href(%href_params);3091print"<link ".3092"rel=\"$link_attr{'-rel'}\"".3093"title=\"$link_attr{'-title'}\"".3094"href=\"$link_attr{'-href'}\"".3095"type=\"$link_attr{'-type'}\"".3096"/>\n";30973098$href_params{'extra_options'} ='--no-merges';3099$link_attr{'-href'} = href(%href_params);3100$link_attr{'-title'} .=' (no merges)';3101print"<link ".3102"rel=\"$link_attr{'-rel'}\"".3103"title=\"$link_attr{'-title'}\"".3104"href=\"$link_attr{'-href'}\"".3105"type=\"$link_attr{'-type'}\"".3106"/>\n";3107}31083109}else{3110printf('<link rel="alternate" title="%sprojects list" '.3111'href="%s" type="text/plain; charset=utf-8" />'."\n",3112$site_name, href(project=>undef, action=>"project_index"));3113printf('<link rel="alternate" title="%sprojects feeds" '.3114'href="%s" type="text/x-opml" />'."\n",3115$site_name, href(project=>undef, action=>"opml"));3116}3117if(defined$favicon) {3118printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3119}31203121print"</head>\n".3122"<body>\n";31233124if(-f $site_header) {3125 insert_file($site_header);3126}31273128print"<div class=\"page_header\">\n".3129$cgi->a({-href => esc_url($logo_url),3130-title =>$logo_label},3131qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3132print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3133if(defined$project) {3134print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3135if(defined$action) {3136print" /$action";3137}3138print"\n";3139}3140print"</div>\n";31413142my$have_search= gitweb_check_feature('search');3143if(defined$project&&$have_search) {3144if(!defined$searchtext) {3145$searchtext="";3146}3147my$search_hash;3148if(defined$hash_base) {3149$search_hash=$hash_base;3150}elsif(defined$hash) {3151$search_hash=$hash;3152}else{3153$search_hash="HEAD";3154}3155my$action=$my_uri;3156my$use_pathinfo= gitweb_check_feature('pathinfo');3157if($use_pathinfo) {3158$action.="/".esc_url($project);3159}3160print$cgi->startform(-method=>"get", -action =>$action) .3161"<div class=\"search\">\n".3162(!$use_pathinfo&&3163$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3164$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3165$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3166$cgi->popup_menu(-name =>'st', -default=>'commit',3167-values=> ['commit','grep','author','committer','pickaxe']) .3168$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3169" search:\n",3170$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3171"<span title=\"Extended regular expression\">".3172$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3173-checked =>$search_use_regexp) .3174"</span>".3175"</div>".3176$cgi->end_form() ."\n";3177}3178}31793180sub git_footer_html {3181my$feed_class='rss_logo';31823183print"<div class=\"page_footer\">\n";3184if(defined$project) {3185my$descr= git_get_project_description($project);3186if(defined$descr) {3187print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3188}31893190my%href_params= get_feed_info();3191if(!%href_params) {3192$feed_class.=' generic';3193}3194$href_params{'-title'} ||='log';31953196foreachmy$formatqw(RSS Atom){3197$href_params{'action'} =lc($format);3198print$cgi->a({-href => href(%href_params),3199-title =>"$href_params{'-title'}$formatfeed",3200-class=>$feed_class},$format)."\n";3201}32023203}else{3204print$cgi->a({-href => href(project=>undef, action=>"opml"),3205-class=>$feed_class},"OPML") ." ";3206print$cgi->a({-href => href(project=>undef, action=>"project_index"),3207-class=>$feed_class},"TXT") ."\n";3208}3209print"</div>\n";# class="page_footer"32103211if(-f $site_footer) {3212 insert_file($site_footer);3213}32143215print"</body>\n".3216"</html>";3217}32183219# die_error(<http_status_code>, <error_message>)3220# Example: die_error(404, 'Hash not found')3221# By convention, use the following status codes (as defined in RFC 2616):3222# 400: Invalid or missing CGI parameters, or3223# requested object exists but has wrong type.3224# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3225# this server or project.3226# 404: Requested object/revision/project doesn't exist.3227# 500: The server isn't configured properly, or3228# an internal error occurred (e.g. failed assertions caused by bugs), or3229# an unknown error occurred (e.g. the git binary died unexpectedly).3230sub die_error {3231my$status=shift||500;3232my$error=shift||"Internal server error";32333234my%http_responses= (400=>'400 Bad Request',3235403=>'403 Forbidden',3236404=>'404 Not Found',3237500=>'500 Internal Server Error');3238 git_header_html($http_responses{$status});3239print<<EOF;3240<div class="page_body">3241<br /><br />3242$status-$error3243<br />3244</div>3245EOF3246 git_footer_html();3247exit;3248}32493250## ----------------------------------------------------------------------3251## functions printing or outputting HTML: navigation32523253sub git_print_page_nav {3254my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3255$extra=''if!defined$extra;# pager or formats32563257my@navs=qw(summary shortlog log commit commitdiff tree);3258if($suppress) {3259@navs=grep{$_ne$suppress}@navs;3260}32613262my%arg=map{$_=> {action=>$_} }@navs;3263if(defined$head) {3264for(qw(commit commitdiff)) {3265$arg{$_}{'hash'} =$head;3266}3267if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3268for(qw(shortlog log)) {3269$arg{$_}{'hash'} =$head;3270}3271}3272}32733274$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3275$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;32763277my@actions= gitweb_get_feature('actions');3278my%repl= (3279'%'=>'%',3280'n'=>$project,# project name3281'f'=>$git_dir,# project path within filesystem3282'h'=>$treehead||'',# current hash ('h' parameter)3283'b'=>$treebase||'',# hash base ('hb' parameter)3284);3285while(@actions) {3286my($label,$link,$pos) =splice(@actions,0,3);3287# insert3288@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3289# munch munch3290$link=~s/%([%nfhb])/$repl{$1}/g;3291$arg{$label}{'_href'} =$link;3292}32933294print"<div class=\"page_nav\">\n".3295(join" | ",3296map{$_eq$current?3297$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3298}@navs);3299print"<br/>\n$extra<br/>\n".3300"</div>\n";3301}33023303sub format_paging_nav {3304my($action,$hash,$head,$page,$has_next_link) =@_;3305my$paging_nav;330633073308if($hashne$head||$page) {3309$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3310}else{3311$paging_nav.="HEAD";3312}33133314if($page>0) {3315$paging_nav.=" ⋅ ".3316$cgi->a({-href => href(-replay=>1, page=>$page-1),3317-accesskey =>"p", -title =>"Alt-p"},"prev");3318}else{3319$paging_nav.=" ⋅ prev";3320}33213322if($has_next_link) {3323$paging_nav.=" ⋅ ".3324$cgi->a({-href => href(-replay=>1, page=>$page+1),3325-accesskey =>"n", -title =>"Alt-n"},"next");3326}else{3327$paging_nav.=" ⋅ next";3328}33293330return$paging_nav;3331}33323333## ......................................................................3334## functions printing or outputting HTML: div33353336sub git_print_header_div {3337my($action,$title,$hash,$hash_base) =@_;3338my%args= ();33393340$args{'action'} =$action;3341$args{'hash'} =$hashif$hash;3342$args{'hash_base'} =$hash_baseif$hash_base;33433344print"<div class=\"header\">\n".3345$cgi->a({-href => href(%args), -class=>"title"},3346$title?$title:$action) .3347"\n</div>\n";3348}33493350sub print_local_time {3351my%date=@_;3352if($date{'hour_local'} <6) {3353printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3354$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3355}else{3356printf(" (%02d:%02d%s)",3357$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3358}3359}33603361# Outputs the author name and date in long form3362sub git_print_authorship {3363my$co=shift;3364my%opts=@_;3365my$tag=$opts{-tag} ||'div';33663367my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3368print"<$tagclass=\"author_date\">".3369 esc_html($co->{'author_name'}) .3370" [$ad{'rfc2822'}";3371 print_local_time(%ad)if($opts{-localtime});3372print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3373."</$tag>\n";3374}33753376# Outputs table rows containing the full author or committer information,3377# in the format expected for 'commit' view (& similia).3378# Parameters are a commit hash reference, followed by the list of people3379# to output information for. If the list is empty it defalts to both3380# author and committer.3381sub git_print_authorship_rows {3382my$co=shift;3383# too bad we can't use @people = @_ || ('author', 'committer')3384my@people=@_;3385@people= ('author','committer')unless@people;3386foreachmy$who(@people) {3387my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3388print"<tr><td>$who</td><td>". esc_html($co->{$who}) ."</td>".3389"<td rowspan=\"2\">".3390 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3391"</td></tr>\n".3392"<tr>".3393"<td></td><td>$wd{'rfc2822'}";3394 print_local_time(%wd);3395print"</td>".3396"</tr>\n";3397}3398}33993400sub git_print_page_path {3401my$name=shift;3402my$type=shift;3403my$hb=shift;340434053406print"<div class=\"page_path\">";3407print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3408-title =>'tree root'}, to_utf8("[$project]"));3409print" / ";3410if(defined$name) {3411my@dirname=split'/',$name;3412my$basename=pop@dirname;3413my$fullname='';34143415foreachmy$dir(@dirname) {3416$fullname.= ($fullname?'/':'') .$dir;3417print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3418 hash_base=>$hb),3419-title =>$fullname}, esc_path($dir));3420print" / ";3421}3422if(defined$type&&$typeeq'blob') {3423print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3424 hash_base=>$hb),3425-title =>$name}, esc_path($basename));3426}elsif(defined$type&&$typeeq'tree') {3427print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3428 hash_base=>$hb),3429-title =>$name}, esc_path($basename));3430print" / ";3431}else{3432print esc_path($basename);3433}3434}3435print"<br/></div>\n";3436}34373438sub git_print_log {3439my$log=shift;3440my%opts=@_;34413442if($opts{'-remove_title'}) {3443# remove title, i.e. first line of log3444shift@$log;3445}3446# remove leading empty lines3447while(defined$log->[0] &&$log->[0]eq"") {3448shift@$log;3449}34503451# print log3452my$signoff=0;3453my$empty=0;3454foreachmy$line(@$log) {3455if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3456$signoff=1;3457$empty=0;3458if(!$opts{'-remove_signoff'}) {3459print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3460next;3461}else{3462# remove signoff lines3463next;3464}3465}else{3466$signoff=0;3467}34683469# print only one empty line3470# do not print empty line after signoff3471if($lineeq"") {3472next if($empty||$signoff);3473$empty=1;3474}else{3475$empty=0;3476}34773478print format_log_line_html($line) ."<br/>\n";3479}34803481if($opts{'-final_empty_line'}) {3482# end with single empty line3483print"<br/>\n"unless$empty;3484}3485}34863487# return link target (what link points to)3488sub git_get_link_target {3489my$hash=shift;3490my$link_target;34913492# read link3493open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3494orreturn;3495{3496local$/=undef;3497$link_target= <$fd>;3498}3499close$fd3500orreturn;35013502return$link_target;3503}35043505# given link target, and the directory (basedir) the link is in,3506# return target of link relative to top directory (top tree);3507# return undef if it is not possible (including absolute links).3508sub normalize_link_target {3509my($link_target,$basedir) =@_;35103511# absolute symlinks (beginning with '/') cannot be normalized3512return if(substr($link_target,0,1)eq'/');35133514# normalize link target to path from top (root) tree (dir)3515my$path;3516if($basedir) {3517$path=$basedir.'/'.$link_target;3518}else{3519# we are in top (root) tree (dir)3520$path=$link_target;3521}35223523# remove //, /./, and /../3524my@path_parts;3525foreachmy$part(split('/',$path)) {3526# discard '.' and ''3527next if(!$part||$parteq'.');3528# handle '..'3529if($parteq'..') {3530if(@path_parts) {3531pop@path_parts;3532}else{3533# link leads outside repository (outside top dir)3534return;3535}3536}else{3537push@path_parts,$part;3538}3539}3540$path=join('/',@path_parts);35413542return$path;3543}35443545# print tree entry (row of git_tree), but without encompassing <tr> element3546sub git_print_tree_entry {3547my($t,$basedir,$hash_base,$have_blame) =@_;35483549my%base_key= ();3550$base_key{'hash_base'} =$hash_baseifdefined$hash_base;35513552# The format of a table row is: mode list link. Where mode is3553# the mode of the entry, list is the name of the entry, an href,3554# and link is the action links of the entry.35553556print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3557if($t->{'type'}eq"blob") {3558print"<td class=\"list\">".3559$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3560 file_name=>"$basedir$t->{'name'}",%base_key),3561-class=>"list"}, esc_path($t->{'name'}));3562if(S_ISLNK(oct$t->{'mode'})) {3563my$link_target= git_get_link_target($t->{'hash'});3564if($link_target) {3565my$norm_target= normalize_link_target($link_target,$basedir);3566if(defined$norm_target) {3567print" -> ".3568$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3569 file_name=>$norm_target),3570-title =>$norm_target}, esc_path($link_target));3571}else{3572print" -> ". esc_path($link_target);3573}3574}3575}3576print"</td>\n";3577print"<td class=\"link\">";3578print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3579 file_name=>"$basedir$t->{'name'}",%base_key)},3580"blob");3581if($have_blame) {3582print" | ".3583$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3584 file_name=>"$basedir$t->{'name'}",%base_key)},3585"blame");3586}3587if(defined$hash_base) {3588print" | ".3589$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3590 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3591"history");3592}3593print" | ".3594$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3595 file_name=>"$basedir$t->{'name'}")},3596"raw");3597print"</td>\n";35983599}elsif($t->{'type'}eq"tree") {3600print"<td class=\"list\">";3601print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3602 file_name=>"$basedir$t->{'name'}",%base_key)},3603 esc_path($t->{'name'}));3604print"</td>\n";3605print"<td class=\"link\">";3606print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3607 file_name=>"$basedir$t->{'name'}",%base_key)},3608"tree");3609if(defined$hash_base) {3610print" | ".3611$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3612 file_name=>"$basedir$t->{'name'}")},3613"history");3614}3615print"</td>\n";3616}else{3617# unknown object: we can only present history for it3618# (this includes 'commit' object, i.e. submodule support)3619print"<td class=\"list\">".3620 esc_path($t->{'name'}) .3621"</td>\n";3622print"<td class=\"link\">";3623if(defined$hash_base) {3624print$cgi->a({-href => href(action=>"history",3625 hash_base=>$hash_base,3626 file_name=>"$basedir$t->{'name'}")},3627"history");3628}3629print"</td>\n";3630}3631}36323633## ......................................................................3634## functions printing large fragments of HTML36353636# get pre-image filenames for merge (combined) diff3637sub fill_from_file_info {3638my($diff,@parents) =@_;36393640$diff->{'from_file'} = [ ];3641$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3642for(my$i=0;$i<$diff->{'nparents'};$i++) {3643if($diff->{'status'}[$i]eq'R'||3644$diff->{'status'}[$i]eq'C') {3645$diff->{'from_file'}[$i] =3646 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3647}3648}36493650return$diff;3651}36523653# is current raw difftree line of file deletion3654sub is_deleted {3655my$diffinfo=shift;36563657return$diffinfo->{'to_id'}eq('0' x 40);3658}36593660# does patch correspond to [previous] difftree raw line3661# $diffinfo - hashref of parsed raw diff format3662# $patchinfo - hashref of parsed patch diff format3663# (the same keys as in $diffinfo)3664sub is_patch_split {3665my($diffinfo,$patchinfo) =@_;36663667returndefined$diffinfo&&defined$patchinfo3668&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3669}367036713672sub git_difftree_body {3673my($difftree,$hash,@parents) =@_;3674my($parent) =$parents[0];3675my$have_blame= gitweb_check_feature('blame');3676print"<div class=\"list_head\">\n";3677if($#{$difftree} >10) {3678print(($#{$difftree} +1) ." files changed:\n");3679}3680print"</div>\n";36813682print"<table class=\"".3683(@parents>1?"combined ":"") .3684"diff_tree\">\n";36853686# header only for combined diff in 'commitdiff' view3687my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3688if($has_header) {3689# table header3690print"<thead><tr>\n".3691"<th></th><th></th>\n";# filename, patchN link3692for(my$i=0;$i<@parents;$i++) {3693my$par=$parents[$i];3694print"<th>".3695$cgi->a({-href => href(action=>"commitdiff",3696 hash=>$hash, hash_parent=>$par),3697-title =>'commitdiff to parent number '.3698($i+1) .': '.substr($par,0,7)},3699$i+1) .3700" </th>\n";3701}3702print"</tr></thead>\n<tbody>\n";3703}37043705my$alternate=1;3706my$patchno=0;3707foreachmy$line(@{$difftree}) {3708my$diff= parsed_difftree_line($line);37093710if($alternate) {3711print"<tr class=\"dark\">\n";3712}else{3713print"<tr class=\"light\">\n";3714}3715$alternate^=1;37163717if(exists$diff->{'nparents'}) {# combined diff37183719 fill_from_file_info($diff,@parents)3720unlessexists$diff->{'from_file'};37213722if(!is_deleted($diff)) {3723# file exists in the result (child) commit3724print"<td>".3725$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3726 file_name=>$diff->{'to_file'},3727 hash_base=>$hash),3728-class=>"list"}, esc_path($diff->{'to_file'})) .3729"</td>\n";3730}else{3731print"<td>".3732 esc_path($diff->{'to_file'}) .3733"</td>\n";3734}37353736if($actioneq'commitdiff') {3737# link to patch3738$patchno++;3739print"<td class=\"link\">".3740$cgi->a({-href =>"#patch$patchno"},"patch") .3741" | ".3742"</td>\n";3743}37443745my$has_history=0;3746my$not_deleted=0;3747for(my$i=0;$i<$diff->{'nparents'};$i++) {3748my$hash_parent=$parents[$i];3749my$from_hash=$diff->{'from_id'}[$i];3750my$from_path=$diff->{'from_file'}[$i];3751my$status=$diff->{'status'}[$i];37523753$has_history||= ($statusne'A');3754$not_deleted||= ($statusne'D');37553756if($statuseq'A') {3757print"<td class=\"link\"align=\"right\"> | </td>\n";3758}elsif($statuseq'D') {3759print"<td class=\"link\">".3760$cgi->a({-href => href(action=>"blob",3761 hash_base=>$hash,3762 hash=>$from_hash,3763 file_name=>$from_path)},3764"blob". ($i+1)) .3765" | </td>\n";3766}else{3767if($diff->{'to_id'}eq$from_hash) {3768print"<td class=\"link nochange\">";3769}else{3770print"<td class=\"link\">";3771}3772print$cgi->a({-href => href(action=>"blobdiff",3773 hash=>$diff->{'to_id'},3774 hash_parent=>$from_hash,3775 hash_base=>$hash,3776 hash_parent_base=>$hash_parent,3777 file_name=>$diff->{'to_file'},3778 file_parent=>$from_path)},3779"diff". ($i+1)) .3780" | </td>\n";3781}3782}37833784print"<td class=\"link\">";3785if($not_deleted) {3786print$cgi->a({-href => href(action=>"blob",3787 hash=>$diff->{'to_id'},3788 file_name=>$diff->{'to_file'},3789 hash_base=>$hash)},3790"blob");3791print" | "if($has_history);3792}3793if($has_history) {3794print$cgi->a({-href => href(action=>"history",3795 file_name=>$diff->{'to_file'},3796 hash_base=>$hash)},3797"history");3798}3799print"</td>\n";38003801print"</tr>\n";3802next;# instead of 'else' clause, to avoid extra indent3803}3804# else ordinary diff38053806my($to_mode_oct,$to_mode_str,$to_file_type);3807my($from_mode_oct,$from_mode_str,$from_file_type);3808if($diff->{'to_mode'}ne('0' x 6)) {3809$to_mode_oct=oct$diff->{'to_mode'};3810if(S_ISREG($to_mode_oct)) {# only for regular file3811$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3812}3813$to_file_type= file_type($diff->{'to_mode'});3814}3815if($diff->{'from_mode'}ne('0' x 6)) {3816$from_mode_oct=oct$diff->{'from_mode'};3817if(S_ISREG($to_mode_oct)) {# only for regular file3818$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3819}3820$from_file_type= file_type($diff->{'from_mode'});3821}38223823if($diff->{'status'}eq"A") {# created3824my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3825$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3826$mode_chng.="]</span>";3827print"<td>";3828print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3829 hash_base=>$hash, file_name=>$diff->{'file'}),3830-class=>"list"}, esc_path($diff->{'file'}));3831print"</td>\n";3832print"<td>$mode_chng</td>\n";3833print"<td class=\"link\">";3834if($actioneq'commitdiff') {3835# link to patch3836$patchno++;3837print$cgi->a({-href =>"#patch$patchno"},"patch");3838print" | ";3839}3840print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3841 hash_base=>$hash, file_name=>$diff->{'file'})},3842"blob");3843print"</td>\n";38443845}elsif($diff->{'status'}eq"D") {# deleted3846my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3847print"<td>";3848print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3849 hash_base=>$parent, file_name=>$diff->{'file'}),3850-class=>"list"}, esc_path($diff->{'file'}));3851print"</td>\n";3852print"<td>$mode_chng</td>\n";3853print"<td class=\"link\">";3854if($actioneq'commitdiff') {3855# link to patch3856$patchno++;3857print$cgi->a({-href =>"#patch$patchno"},"patch");3858print" | ";3859}3860print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3861 hash_base=>$parent, file_name=>$diff->{'file'})},3862"blob") ." | ";3863if($have_blame) {3864print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3865 file_name=>$diff->{'file'})},3866"blame") ." | ";3867}3868print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3869 file_name=>$diff->{'file'})},3870"history");3871print"</td>\n";38723873}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3874my$mode_chnge="";3875if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3876$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3877if($from_file_typene$to_file_type) {3878$mode_chnge.=" from$from_file_typeto$to_file_type";3879}3880if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3881if($from_mode_str&&$to_mode_str) {3882$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3883}elsif($to_mode_str) {3884$mode_chnge.=" mode:$to_mode_str";3885}3886}3887$mode_chnge.="]</span>\n";3888}3889print"<td>";3890print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3891 hash_base=>$hash, file_name=>$diff->{'file'}),3892-class=>"list"}, esc_path($diff->{'file'}));3893print"</td>\n";3894print"<td>$mode_chnge</td>\n";3895print"<td class=\"link\">";3896if($actioneq'commitdiff') {3897# link to patch3898$patchno++;3899print$cgi->a({-href =>"#patch$patchno"},"patch") .3900" | ";3901}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3902# "commit" view and modified file (not onlu mode changed)3903print$cgi->a({-href => href(action=>"blobdiff",3904 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3905 hash_base=>$hash, hash_parent_base=>$parent,3906 file_name=>$diff->{'file'})},3907"diff") .3908" | ";3909}3910print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3911 hash_base=>$hash, file_name=>$diff->{'file'})},3912"blob") ." | ";3913if($have_blame) {3914print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3915 file_name=>$diff->{'file'})},3916"blame") ." | ";3917}3918print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3919 file_name=>$diff->{'file'})},3920"history");3921print"</td>\n";39223923}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3924my%status_name= ('R'=>'moved','C'=>'copied');3925my$nstatus=$status_name{$diff->{'status'}};3926my$mode_chng="";3927if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3928# mode also for directories, so we cannot use $to_mode_str3929$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3930}3931print"<td>".3932$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3933 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3934-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3935"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3936$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3937 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3938-class=>"list"}, esc_path($diff->{'from_file'})) .3939" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3940"<td class=\"link\">";3941if($actioneq'commitdiff') {3942# link to patch3943$patchno++;3944print$cgi->a({-href =>"#patch$patchno"},"patch") .3945" | ";3946}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3947# "commit" view and modified file (not only pure rename or copy)3948print$cgi->a({-href => href(action=>"blobdiff",3949 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3950 hash_base=>$hash, hash_parent_base=>$parent,3951 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3952"diff") .3953" | ";3954}3955print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3956 hash_base=>$parent, file_name=>$diff->{'to_file'})},3957"blob") ." | ";3958if($have_blame) {3959print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3960 file_name=>$diff->{'to_file'})},3961"blame") ." | ";3962}3963print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3964 file_name=>$diff->{'to_file'})},3965"history");3966print"</td>\n";39673968}# we should not encounter Unmerged (U) or Unknown (X) status3969print"</tr>\n";3970}3971print"</tbody>"if$has_header;3972print"</table>\n";3973}39743975sub git_patchset_body {3976my($fd,$difftree,$hash,@hash_parents) =@_;3977my($hash_parent) =$hash_parents[0];39783979my$is_combined= (@hash_parents>1);3980my$patch_idx=0;3981my$patch_number=0;3982my$patch_line;3983my$diffinfo;3984my$to_name;3985my(%from,%to);39863987print"<div class=\"patchset\">\n";39883989# skip to first patch3990while($patch_line= <$fd>) {3991chomp$patch_line;39923993last if($patch_line=~m/^diff /);3994}39953996 PATCH:3997while($patch_line) {39983999# parse "git diff" header line4000if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4001# $1 is from_name, which we do not use4002$to_name= unquote($2);4003$to_name=~s!^b/!!;4004}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4005# $1 is 'cc' or 'combined', which we do not use4006$to_name= unquote($2);4007}else{4008$to_name=undef;4009}40104011# check if current patch belong to current raw line4012# and parse raw git-diff line if needed4013if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4014# this is continuation of a split patch4015print"<div class=\"patch cont\">\n";4016}else{4017# advance raw git-diff output if needed4018$patch_idx++ifdefined$diffinfo;40194020# read and prepare patch information4021$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);40224023# compact combined diff output can have some patches skipped4024# find which patch (using pathname of result) we are at now;4025if($is_combined) {4026while($to_namene$diffinfo->{'to_file'}) {4027print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4028 format_diff_cc_simplified($diffinfo,@hash_parents) .4029"</div>\n";# class="patch"40304031$patch_idx++;4032$patch_number++;40334034last if$patch_idx>$#$difftree;4035$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4036}4037}40384039# modifies %from, %to hashes4040 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);40414042# this is first patch for raw difftree line with $patch_idx index4043# we index @$difftree array from 0, but number patches from 14044print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4045}40464047# git diff header4048#assert($patch_line =~ m/^diff /) if DEBUG;4049#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4050$patch_number++;4051# print "git diff" header4052print format_git_diff_header_line($patch_line,$diffinfo,4053 \%from, \%to);40544055# print extended diff header4056print"<div class=\"diff extended_header\">\n";4057 EXTENDED_HEADER:4058while($patch_line= <$fd>) {4059chomp$patch_line;40604061last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);40624063print format_extended_diff_header_line($patch_line,$diffinfo,4064 \%from, \%to);4065}4066print"</div>\n";# class="diff extended_header"40674068# from-file/to-file diff header4069if(!$patch_line) {4070print"</div>\n";# class="patch"4071last PATCH;4072}4073next PATCH if($patch_line=~m/^diff /);4074#assert($patch_line =~ m/^---/) if DEBUG;40754076my$last_patch_line=$patch_line;4077$patch_line= <$fd>;4078chomp$patch_line;4079#assert($patch_line =~ m/^\+\+\+/) if DEBUG;40804081print format_diff_from_to_header($last_patch_line,$patch_line,4082$diffinfo, \%from, \%to,4083@hash_parents);40844085# the patch itself4086 LINE:4087while($patch_line= <$fd>) {4088chomp$patch_line;40894090next PATCH if($patch_line=~m/^diff /);40914092print format_diff_line($patch_line, \%from, \%to);4093}40944095}continue{4096print"</div>\n";# class="patch"4097}40984099# for compact combined (--cc) format, with chunk and patch simpliciaction4100# patchset might be empty, but there might be unprocessed raw lines4101for(++$patch_idxif$patch_number>0;4102$patch_idx<@$difftree;4103++$patch_idx) {4104# read and prepare patch information4105$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);41064107# generate anchor for "patch" links in difftree / whatchanged part4108print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4109 format_diff_cc_simplified($diffinfo,@hash_parents) .4110"</div>\n";# class="patch"41114112$patch_number++;4113}41144115if($patch_number==0) {4116if(@hash_parents>1) {4117print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4118}else{4119print"<div class=\"diff nodifferences\">No differences found</div>\n";4120}4121}41224123print"</div>\n";# class="patchset"4124}41254126# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41274128# fills project list info (age, description, owner, forks) for each4129# project in the list, removing invalid projects from returned list4130# NOTE: modifies $projlist, but does not remove entries from it4131sub fill_project_list_info {4132my($projlist,$check_forks) =@_;4133my@projects;41344135my$show_ctags= gitweb_check_feature('ctags');4136 PROJECT:4137foreachmy$pr(@$projlist) {4138my(@activity) = git_get_last_activity($pr->{'path'});4139unless(@activity) {4140next PROJECT;4141}4142($pr->{'age'},$pr->{'age_string'}) =@activity;4143if(!defined$pr->{'descr'}) {4144my$descr= git_get_project_description($pr->{'path'}) ||"";4145$descr= to_utf8($descr);4146$pr->{'descr_long'} =$descr;4147$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4148}4149if(!defined$pr->{'owner'}) {4150$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4151}4152if($check_forks) {4153my$pname=$pr->{'path'};4154if(($pname=~s/\.git$//) &&4155($pname!~/\/$/) &&4156(-d "$projectroot/$pname")) {4157$pr->{'forks'} ="-d$projectroot/$pname";4158}else{4159$pr->{'forks'} =0;4160}4161}4162$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4163push@projects,$pr;4164}41654166return@projects;4167}41684169# print 'sort by' <th> element, generating 'sort by $name' replay link4170# if that order is not selected4171sub print_sort_th {4172my($name,$order,$header) =@_;4173$header||=ucfirst($name);41744175if($ordereq$name) {4176print"<th>$header</th>\n";4177}else{4178print"<th>".4179$cgi->a({-href => href(-replay=>1, order=>$name),4180-class=>"header"},$header) .4181"</th>\n";4182}4183}41844185sub git_project_list_body {4186# actually uses global variable $project4187my($projlist,$order,$from,$to,$extra,$no_header) =@_;41884189my$check_forks= gitweb_check_feature('forks');4190my@projects= fill_project_list_info($projlist,$check_forks);41914192$order||=$default_projects_order;4193$from=0unlessdefined$from;4194$to=$#projectsif(!defined$to||$#projects<$to);41954196my%order_info= (4197 project => { key =>'path', type =>'str'},4198 descr => { key =>'descr_long', type =>'str'},4199 owner => { key =>'owner', type =>'str'},4200 age => { key =>'age', type =>'num'}4201);4202my$oi=$order_info{$order};4203if($oi->{'type'}eq'str') {4204@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4205}else{4206@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4207}42084209my$show_ctags= gitweb_check_feature('ctags');4210if($show_ctags) {4211my%ctags;4212foreachmy$p(@projects) {4213foreachmy$ct(keys%{$p->{'ctags'}}) {4214$ctags{$ct} +=$p->{'ctags'}->{$ct};4215}4216}4217my$cloud= git_populate_project_tagcloud(\%ctags);4218print git_show_project_tagcloud($cloud,64);4219}42204221print"<table class=\"project_list\">\n";4222unless($no_header) {4223print"<tr>\n";4224if($check_forks) {4225print"<th></th>\n";4226}4227 print_sort_th('project',$order,'Project');4228 print_sort_th('descr',$order,'Description');4229 print_sort_th('owner',$order,'Owner');4230 print_sort_th('age',$order,'Last Change');4231print"<th></th>\n".# for links4232"</tr>\n";4233}4234my$alternate=1;4235my$tagfilter=$cgi->param('by_tag');4236for(my$i=$from;$i<=$to;$i++) {4237my$pr=$projects[$i];42384239next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4240next if$searchtextand not$pr->{'path'} =~/$searchtext/4241and not$pr->{'descr_long'} =~/$searchtext/;4242# Weed out forks or non-matching entries of search4243if($check_forks) {4244my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4245$forkbase="^$forkbase"if$forkbase;4246next ifnot$searchtextand not$tagfilterand$show_ctags4247and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4248}42494250if($alternate) {4251print"<tr class=\"dark\">\n";4252}else{4253print"<tr class=\"light\">\n";4254}4255$alternate^=1;4256if($check_forks) {4257print"<td>";4258if($pr->{'forks'}) {4259print"<!--$pr->{'forks'} -->\n";4260print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4261}4262print"</td>\n";4263}4264print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4265-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4266"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4267-class=>"list", -title =>$pr->{'descr_long'}},4268 esc_html($pr->{'descr'})) ."</td>\n".4269"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4270print"<td class=\"". age_class($pr->{'age'}) ."\">".4271(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4272"<td class=\"link\">".4273$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4274$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4275$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4276$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4277($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4278"</td>\n".4279"</tr>\n";4280}4281if(defined$extra) {4282print"<tr>\n";4283if($check_forks) {4284print"<td></td>\n";4285}4286print"<td colspan=\"5\">$extra</td>\n".4287"</tr>\n";4288}4289print"</table>\n";4290}42914292sub git_shortlog_body {4293# uses global variable $project4294my($commitlist,$from,$to,$refs,$extra) =@_;42954296$from=0unlessdefined$from;4297$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);42984299print"<table class=\"shortlog\">\n";4300my$alternate=1;4301for(my$i=$from;$i<=$to;$i++) {4302my%co= %{$commitlist->[$i]};4303my$commit=$co{'id'};4304my$ref= format_ref_marker($refs,$commit);4305if($alternate) {4306print"<tr class=\"dark\">\n";4307}else{4308print"<tr class=\"light\">\n";4309}4310$alternate^=1;4311# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4312print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4313 format_author_html('td', \%co,10) ."<td>";4314print format_subject_html($co{'title'},$co{'title_short'},4315 href(action=>"commit", hash=>$commit),$ref);4316print"</td>\n".4317"<td class=\"link\">".4318$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4319$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4320$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4321my$snapshot_links= format_snapshot_links($commit);4322if(defined$snapshot_links) {4323print" | ".$snapshot_links;4324}4325print"</td>\n".4326"</tr>\n";4327}4328if(defined$extra) {4329print"<tr>\n".4330"<td colspan=\"4\">$extra</td>\n".4331"</tr>\n";4332}4333print"</table>\n";4334}43354336sub git_history_body {4337# Warning: assumes constant type (blob or tree) during history4338my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;43394340$from=0unlessdefined$from;4341$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});43424343print"<table class=\"history\">\n";4344my$alternate=1;4345for(my$i=$from;$i<=$to;$i++) {4346my%co= %{$commitlist->[$i]};4347if(!%co) {4348next;4349}4350my$commit=$co{'id'};43514352my$ref= format_ref_marker($refs,$commit);43534354if($alternate) {4355print"<tr class=\"dark\">\n";4356}else{4357print"<tr class=\"light\">\n";4358}4359$alternate^=1;4360print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4361# shortlog: format_author_html('td', \%co, 10)4362 format_author_html('td', \%co,15,3) ."<td>";4363# originally git_history used chop_str($co{'title'}, 50)4364print format_subject_html($co{'title'},$co{'title_short'},4365 href(action=>"commit", hash=>$commit),$ref);4366print"</td>\n".4367"<td class=\"link\">".4368$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4369$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");43704371if($ftypeeq'blob') {4372my$blob_current= git_get_hash_by_path($hash_base,$file_name);4373my$blob_parent= git_get_hash_by_path($commit,$file_name);4374if(defined$blob_current&&defined$blob_parent&&4375$blob_currentne$blob_parent) {4376print" | ".4377$cgi->a({-href => href(action=>"blobdiff",4378 hash=>$blob_current, hash_parent=>$blob_parent,4379 hash_base=>$hash_base, hash_parent_base=>$commit,4380 file_name=>$file_name)},4381"diff to current");4382}4383}4384print"</td>\n".4385"</tr>\n";4386}4387if(defined$extra) {4388print"<tr>\n".4389"<td colspan=\"4\">$extra</td>\n".4390"</tr>\n";4391}4392print"</table>\n";4393}43944395sub git_tags_body {4396# uses global variable $project4397my($taglist,$from,$to,$extra) =@_;4398$from=0unlessdefined$from;4399$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);44004401print"<table class=\"tags\">\n";4402my$alternate=1;4403for(my$i=$from;$i<=$to;$i++) {4404my$entry=$taglist->[$i];4405my%tag=%$entry;4406my$comment=$tag{'subject'};4407my$comment_short;4408if(defined$comment) {4409$comment_short= chop_str($comment,30,5);4410}4411if($alternate) {4412print"<tr class=\"dark\">\n";4413}else{4414print"<tr class=\"light\">\n";4415}4416$alternate^=1;4417if(defined$tag{'age'}) {4418print"<td><i>$tag{'age'}</i></td>\n";4419}else{4420print"<td></td>\n";4421}4422print"<td>".4423$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4424-class=>"list name"}, esc_html($tag{'name'})) .4425"</td>\n".4426"<td>";4427if(defined$comment) {4428print format_subject_html($comment,$comment_short,4429 href(action=>"tag", hash=>$tag{'id'}));4430}4431print"</td>\n".4432"<td class=\"selflink\">";4433if($tag{'type'}eq"tag") {4434print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4435}else{4436print" ";4437}4438print"</td>\n".4439"<td class=\"link\">"." | ".4440$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4441if($tag{'reftype'}eq"commit") {4442print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4443" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4444}elsif($tag{'reftype'}eq"blob") {4445print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4446}4447print"</td>\n".4448"</tr>";4449}4450if(defined$extra) {4451print"<tr>\n".4452"<td colspan=\"5\">$extra</td>\n".4453"</tr>\n";4454}4455print"</table>\n";4456}44574458sub git_heads_body {4459# uses global variable $project4460my($headlist,$head,$from,$to,$extra) =@_;4461$from=0unlessdefined$from;4462$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);44634464print"<table class=\"heads\">\n";4465my$alternate=1;4466for(my$i=$from;$i<=$to;$i++) {4467my$entry=$headlist->[$i];4468my%ref=%$entry;4469my$curr=$ref{'id'}eq$head;4470if($alternate) {4471print"<tr class=\"dark\">\n";4472}else{4473print"<tr class=\"light\">\n";4474}4475$alternate^=1;4476print"<td><i>$ref{'age'}</i></td>\n".4477($curr?"<td class=\"current_head\">":"<td>") .4478$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4479-class=>"list name"},esc_html($ref{'name'})) .4480"</td>\n".4481"<td class=\"link\">".4482$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4483$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4484$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4485"</td>\n".4486"</tr>";4487}4488if(defined$extra) {4489print"<tr>\n".4490"<td colspan=\"3\">$extra</td>\n".4491"</tr>\n";4492}4493print"</table>\n";4494}44954496sub git_search_grep_body {4497my($commitlist,$from,$to,$extra) =@_;4498$from=0unlessdefined$from;4499$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45004501print"<table class=\"commit_search\">\n";4502my$alternate=1;4503for(my$i=$from;$i<=$to;$i++) {4504my%co= %{$commitlist->[$i]};4505if(!%co) {4506next;4507}4508my$commit=$co{'id'};4509if($alternate) {4510print"<tr class=\"dark\">\n";4511}else{4512print"<tr class=\"light\">\n";4513}4514$alternate^=1;4515print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4516 format_author_html('td', \%co,15,5) .4517"<td>".4518$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4519-class=>"list subject"},4520 chop_and_escape_str($co{'title'},50) ."<br/>");4521my$comment=$co{'comment'};4522foreachmy$line(@$comment) {4523if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4524my($lead,$match,$trail) = ($1,$2,$3);4525$match= chop_str($match,70,5,'center');4526my$contextlen=int((80-length($match))/2);4527$contextlen=30if($contextlen>30);4528$lead= chop_str($lead,$contextlen,10,'left');4529$trail= chop_str($trail,$contextlen,10,'right');45304531$lead= esc_html($lead);4532$match= esc_html($match);4533$trail= esc_html($trail);45344535print"$lead<span class=\"match\">$match</span>$trail<br />";4536}4537}4538print"</td>\n".4539"<td class=\"link\">".4540$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4541" | ".4542$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4543" | ".4544$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4545print"</td>\n".4546"</tr>\n";4547}4548if(defined$extra) {4549print"<tr>\n".4550"<td colspan=\"3\">$extra</td>\n".4551"</tr>\n";4552}4553print"</table>\n";4554}45554556## ======================================================================4557## ======================================================================4558## actions45594560sub git_project_list {4561my$order=$input_params{'order'};4562if(defined$order&&$order!~m/none|project|descr|owner|age/) {4563 die_error(400,"Unknown order parameter");4564}45654566my@list= git_get_projects_list();4567if(!@list) {4568 die_error(404,"No projects found");4569}45704571 git_header_html();4572if(-f $home_text) {4573print"<div class=\"index_include\">\n";4574 insert_file($home_text);4575print"</div>\n";4576}4577print$cgi->startform(-method=>"get") .4578"<p class=\"projsearch\">Search:\n".4579$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4580"</p>".4581$cgi->end_form() ."\n";4582 git_project_list_body(\@list,$order);4583 git_footer_html();4584}45854586sub git_forks {4587my$order=$input_params{'order'};4588if(defined$order&&$order!~m/none|project|descr|owner|age/) {4589 die_error(400,"Unknown order parameter");4590}45914592my@list= git_get_projects_list($project);4593if(!@list) {4594 die_error(404,"No forks found");4595}45964597 git_header_html();4598 git_print_page_nav('','');4599 git_print_header_div('summary',"$projectforks");4600 git_project_list_body(\@list,$order);4601 git_footer_html();4602}46034604sub git_project_index {4605my@projects= git_get_projects_list($project);46064607print$cgi->header(4608-type =>'text/plain',4609-charset =>'utf-8',4610-content_disposition =>'inline; filename="index.aux"');46114612foreachmy$pr(@projects) {4613if(!exists$pr->{'owner'}) {4614$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4615}46164617my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4618# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4619$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4620$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4621$path=~s/ /\+/g;4622$owner=~s/ /\+/g;46234624print"$path$owner\n";4625}4626}46274628sub git_summary {4629my$descr= git_get_project_description($project) ||"none";4630my%co= parse_commit("HEAD");4631my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4632my$head=$co{'id'};46334634my$owner= git_get_project_owner($project);46354636my$refs= git_get_references();4637# These get_*_list functions return one more to allow us to see if4638# there are more ...4639my@taglist= git_get_tags_list(16);4640my@headlist= git_get_heads_list(16);4641my@forklist;4642my$check_forks= gitweb_check_feature('forks');46434644if($check_forks) {4645@forklist= git_get_projects_list($project);4646}46474648 git_header_html();4649 git_print_page_nav('summary','',$head);46504651print"<div class=\"title\"> </div>\n";4652print"<table class=\"projects_list\">\n".4653"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4654"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4655if(defined$cd{'rfc2822'}) {4656print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4657}46584659# use per project git URL list in $projectroot/$project/cloneurl4660# or make project git URL from git base URL and project name4661my$url_tag="URL";4662my@url_list= git_get_project_url_list($project);4663@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4664foreachmy$git_url(@url_list) {4665next unless$git_url;4666print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4667$url_tag="";4668}46694670# Tag cloud4671my$show_ctags= gitweb_check_feature('ctags');4672if($show_ctags) {4673my$ctags= git_get_project_ctags($project);4674my$cloud= git_populate_project_tagcloud($ctags);4675print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4676print"</td>\n<td>"unless%$ctags;4677print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4678print"</td>\n<td>"if%$ctags;4679print git_show_project_tagcloud($cloud,48);4680print"</td></tr>";4681}46824683print"</table>\n";46844685# If XSS prevention is on, we don't include README.html.4686# TODO: Allow a readme in some safe format.4687if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4688print"<div class=\"title\">readme</div>\n".4689"<div class=\"readme\">\n";4690 insert_file("$projectroot/$project/README.html");4691print"\n</div>\n";# class="readme"4692}46934694# we need to request one more than 16 (0..15) to check if4695# those 16 are all4696my@commitlist=$head? parse_commits($head,17) : ();4697if(@commitlist) {4698 git_print_header_div('shortlog');4699 git_shortlog_body(\@commitlist,0,15,$refs,4700$#commitlist<=15?undef:4701$cgi->a({-href => href(action=>"shortlog")},"..."));4702}47034704if(@taglist) {4705 git_print_header_div('tags');4706 git_tags_body(\@taglist,0,15,4707$#taglist<=15?undef:4708$cgi->a({-href => href(action=>"tags")},"..."));4709}47104711if(@headlist) {4712 git_print_header_div('heads');4713 git_heads_body(\@headlist,$head,0,15,4714$#headlist<=15?undef:4715$cgi->a({-href => href(action=>"heads")},"..."));4716}47174718if(@forklist) {4719 git_print_header_div('forks');4720 git_project_list_body(\@forklist,'age',0,15,4721$#forklist<=15?undef:4722$cgi->a({-href => href(action=>"forks")},"..."),4723'no_header');4724}47254726 git_footer_html();4727}47284729sub git_tag {4730my$head= git_get_head_hash($project);4731 git_header_html();4732 git_print_page_nav('','',$head,undef,$head);4733my%tag= parse_tag($hash);47344735if(!%tag) {4736 die_error(404,"Unknown tag object");4737}47384739 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4740print"<div class=\"title_text\">\n".4741"<table class=\"object_header\">\n".4742"<tr>\n".4743"<td>object</td>\n".4744"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4745$tag{'object'}) ."</td>\n".4746"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4747$tag{'type'}) ."</td>\n".4748"</tr>\n";4749if(defined($tag{'author'})) {4750 git_print_authorship_rows(\%tag,'author');4751}4752print"</table>\n\n".4753"</div>\n";4754print"<div class=\"page_body\">";4755my$comment=$tag{'comment'};4756foreachmy$line(@$comment) {4757chomp$line;4758print esc_html($line, -nbsp=>1) ."<br/>\n";4759}4760print"</div>\n";4761 git_footer_html();4762}47634764sub git_blame {4765# permissions4766 gitweb_check_feature('blame')4767or die_error(403,"Blame view not allowed");47684769# error checking4770 die_error(400,"No file name given")unless$file_name;4771$hash_base||= git_get_head_hash($project);4772 die_error(404,"Couldn't find base commit")unless$hash_base;4773my%co= parse_commit($hash_base)4774or die_error(404,"Commit not found");4775my$ftype="blob";4776if(!defined$hash) {4777$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4778or die_error(404,"Error looking up file");4779}else{4780$ftype= git_get_type($hash);4781if($ftype!~"blob") {4782 die_error(400,"Object is not a blob");4783}4784}47854786# run git-blame --porcelain4787open my$fd,"-|", git_cmd(),"blame",'-p',4788$hash_base,'--',$file_name4789or die_error(500,"Open git-blame failed");47904791# page header4792 git_header_html();4793my$formats_nav=4794$cgi->a({-href => href(action=>"blob", -replay=>1)},4795"blob") .4796" | ".4797$cgi->a({-href => href(action=>"history", -replay=>1)},4798"history") .4799" | ".4800$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4801"HEAD");4802 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4803 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4804 git_print_page_path($file_name,$ftype,$hash_base);48054806# page body4807my@rev_color=qw(light2 dark2);4808my$num_colors=scalar(@rev_color);4809my$current_color=0;4810my%metainfo= ();48114812print<<HTML;4813<div class="page_body">4814<table class="blame">4815<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4816HTML4817 LINE:4818while(my$line= <$fd>) {4819chomp$line;4820# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4821# no <lines in group> for subsequent lines in group of lines4822my($full_rev,$orig_lineno,$lineno,$group_size) =4823($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4824if(!exists$metainfo{$full_rev}) {4825$metainfo{$full_rev} = {};4826}4827my$meta=$metainfo{$full_rev};4828my$data;4829while($data= <$fd>) {4830chomp$data;4831last if($data=~s/^\t//);# contents of line4832if($data=~/^(\S+) (.*)$/) {4833$meta->{$1} =$2;4834}4835}4836my$short_rev=substr($full_rev,0,8);4837my$author=$meta->{'author'};4838my%date=4839 parse_date($meta->{'author-time'},$meta->{'author-tz'});4840my$date=$date{'iso-tz'};4841if($group_size) {4842$current_color= ($current_color+1) %$num_colors;4843}4844print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4845if($group_size) {4846print"<td class=\"sha1\"";4847print" title=\"". esc_html($author) .",$date\"";4848print" rowspan=\"$group_size\""if($group_size>1);4849print">";4850print$cgi->a({-href => href(action=>"commit",4851 hash=>$full_rev,4852 file_name=>$file_name)},4853 esc_html($short_rev));4854print"</td>\n";4855}4856my$parent_commit;4857if(!exists$meta->{'parent'}) {4858open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4859or die_error(500,"Open git-rev-parse failed");4860$parent_commit= <$dd>;4861close$dd;4862chomp($parent_commit);4863$meta->{'parent'} =$parent_commit;4864}else{4865$parent_commit=$meta->{'parent'};4866}4867my$blamed= href(action =>'blame',4868 file_name =>$meta->{'filename'},4869 hash_base =>$parent_commit);4870print"<td class=\"linenr\">";4871print$cgi->a({ -href =>"$blamed#l$orig_lineno",4872-class=>"linenr"},4873 esc_html($lineno));4874print"</td>";4875print"<td class=\"pre\">". esc_html($data) ."</td>\n";4876print"</tr>\n";4877}4878print"</table>\n";4879print"</div>";4880close$fd4881or print"Reading blob failed\n";48824883# page footer4884 git_footer_html();4885}48864887sub git_tags {4888my$head= git_get_head_hash($project);4889 git_header_html();4890 git_print_page_nav('','',$head,undef,$head);4891 git_print_header_div('summary',$project);48924893my@tagslist= git_get_tags_list();4894if(@tagslist) {4895 git_tags_body(\@tagslist);4896}4897 git_footer_html();4898}48994900sub git_heads {4901my$head= git_get_head_hash($project);4902 git_header_html();4903 git_print_page_nav('','',$head,undef,$head);4904 git_print_header_div('summary',$project);49054906my@headslist= git_get_heads_list();4907if(@headslist) {4908 git_heads_body(\@headslist,$head);4909}4910 git_footer_html();4911}49124913sub git_blob_plain {4914my$type=shift;4915my$expires;49164917if(!defined$hash) {4918if(defined$file_name) {4919my$base=$hash_base|| git_get_head_hash($project);4920$hash= git_get_hash_by_path($base,$file_name,"blob")4921or die_error(404,"Cannot find file");4922}else{4923 die_error(400,"No file name defined");4924}4925}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4926# blobs defined by non-textual hash id's can be cached4927$expires="+1d";4928}49294930open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4931or die_error(500,"Open git-cat-file blob '$hash' failed");49324933# content-type (can include charset)4934$type= blob_contenttype($fd,$file_name,$type);49354936# "save as" filename, even when no $file_name is given4937my$save_as="$hash";4938if(defined$file_name) {4939$save_as=$file_name;4940}elsif($type=~m/^text\//) {4941$save_as.='.txt';4942}49434944# With XSS prevention on, blobs of all types except a few known safe4945# ones are served with "Content-Disposition: attachment" to make sure4946# they don't run in our security domain. For certain image types,4947# blob view writes an <img> tag referring to blob_plain view, and we4948# want to be sure not to break that by serving the image as an4949# attachment (though Firefox 3 doesn't seem to care).4950my$sandbox=$prevent_xss&&4951$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;49524953print$cgi->header(4954-type =>$type,4955-expires =>$expires,4956-content_disposition =>4957($sandbox?'attachment':'inline')4958.'; filename="'.$save_as.'"');4959local$/=undef;4960binmode STDOUT,':raw';4961print<$fd>;4962binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4963close$fd;4964}49654966sub git_blob {4967my$expires;49684969if(!defined$hash) {4970if(defined$file_name) {4971my$base=$hash_base|| git_get_head_hash($project);4972$hash= git_get_hash_by_path($base,$file_name,"blob")4973or die_error(404,"Cannot find file");4974}else{4975 die_error(400,"No file name defined");4976}4977}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4978# blobs defined by non-textual hash id's can be cached4979$expires="+1d";4980}49814982my$have_blame= gitweb_check_feature('blame');4983open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4984or die_error(500,"Couldn't cat$file_name,$hash");4985my$mimetype= blob_mimetype($fd,$file_name);4986if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4987close$fd;4988return git_blob_plain($mimetype);4989}4990# we can have blame only for text/* mimetype4991$have_blame&&= ($mimetype=~m!^text/!);49924993 git_header_html(undef,$expires);4994my$formats_nav='';4995if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4996if(defined$file_name) {4997if($have_blame) {4998$formats_nav.=4999$cgi->a({-href => href(action=>"blame", -replay=>1)},5000"blame") .5001" | ";5002}5003$formats_nav.=5004$cgi->a({-href => href(action=>"history", -replay=>1)},5005"history") .5006" | ".5007$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5008"raw") .5009" | ".5010$cgi->a({-href => href(action=>"blob",5011 hash_base=>"HEAD", file_name=>$file_name)},5012"HEAD");5013}else{5014$formats_nav.=5015$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5016"raw");5017}5018 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5019 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5020}else{5021print"<div class=\"page_nav\">\n".5022"<br/><br/></div>\n".5023"<div class=\"title\">$hash</div>\n";5024}5025 git_print_page_path($file_name,"blob",$hash_base);5026print"<div class=\"page_body\">\n";5027if($mimetype=~m!^image/!) {5028print qq!<img type="$mimetype"!;5029if($file_name) {5030print qq! alt="$file_name" title="$file_name"!;5031}5032print qq! src="! .5033 href(action=>"blob_plain", hash=>$hash,5034 hash_base=>$hash_base, file_name=>$file_name) .5035 qq!"/>\n!;5036}else{5037my$nr;5038while(my$line= <$fd>) {5039chomp$line;5040$nr++;5041$line= untabify($line);5042printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5043$nr,$nr,$nr, esc_html($line, -nbsp=>1);5044}5045}5046close$fd5047or print"Reading blob failed.\n";5048print"</div>";5049 git_footer_html();5050}50515052sub git_tree {5053if(!defined$hash_base) {5054$hash_base="HEAD";5055}5056if(!defined$hash) {5057if(defined$file_name) {5058$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5059}else{5060$hash=$hash_base;5061}5062}5063 die_error(404,"No such tree")unlessdefined($hash);50645065my@entries= ();5066{5067local$/="\0";5068open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash5069or die_error(500,"Open git-ls-tree failed");5070@entries=map{chomp;$_} <$fd>;5071close$fd5072or die_error(404,"Reading tree failed");5073}50745075my$refs= git_get_references();5076my$ref= format_ref_marker($refs,$hash_base);5077 git_header_html();5078my$basedir='';5079my$have_blame= gitweb_check_feature('blame');5080if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5081my@views_nav= ();5082if(defined$file_name) {5083push@views_nav,5084$cgi->a({-href => href(action=>"history", -replay=>1)},5085"history"),5086$cgi->a({-href => href(action=>"tree",5087 hash_base=>"HEAD", file_name=>$file_name)},5088"HEAD"),5089}5090my$snapshot_links= format_snapshot_links($hash);5091if(defined$snapshot_links) {5092# FIXME: Should be available when we have no hash base as well.5093push@views_nav,$snapshot_links;5094}5095 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));5096 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5097}else{5098undef$hash_base;5099print"<div class=\"page_nav\">\n";5100print"<br/><br/></div>\n";5101print"<div class=\"title\">$hash</div>\n";5102}5103if(defined$file_name) {5104$basedir=$file_name;5105if($basedirne''&&substr($basedir, -1)ne'/') {5106$basedir.='/';5107}5108 git_print_page_path($file_name,'tree',$hash_base);5109}5110print"<div class=\"page_body\">\n";5111print"<table class=\"tree\">\n";5112my$alternate=1;5113# '..' (top directory) link if possible5114if(defined$hash_base&&5115defined$file_name&&$file_name=~m![^/]+$!) {5116if($alternate) {5117print"<tr class=\"dark\">\n";5118}else{5119print"<tr class=\"light\">\n";5120}5121$alternate^=1;51225123my$up=$file_name;5124$up=~s!/?[^/]+$!!;5125undef$upunless$up;5126# based on git_print_tree_entry5127print'<td class="mode">'. mode_str('040000') ."</td>\n";5128print'<td class="list">';5129print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,5130 file_name=>$up)},5131"..");5132print"</td>\n";5133print"<td class=\"link\"></td>\n";51345135print"</tr>\n";5136}5137foreachmy$line(@entries) {5138my%t= parse_ls_tree_line($line, -z =>1);51395140if($alternate) {5141print"<tr class=\"dark\">\n";5142}else{5143print"<tr class=\"light\">\n";5144}5145$alternate^=1;51465147 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);51485149print"</tr>\n";5150}5151print"</table>\n".5152"</div>";5153 git_footer_html();5154}51555156sub git_snapshot {5157my$format=$input_params{'snapshot_format'};5158if(!@snapshot_fmts) {5159 die_error(403,"Snapshots not allowed");5160}5161# default to first supported snapshot format5162$format||=$snapshot_fmts[0];5163if($format!~m/^[a-z0-9]+$/) {5164 die_error(400,"Invalid snapshot format parameter");5165}elsif(!exists($known_snapshot_formats{$format})) {5166 die_error(400,"Unknown snapshot format");5167}elsif(!grep($_eq$format,@snapshot_fmts)) {5168 die_error(403,"Unsupported snapshot format");5169}51705171if(!defined$hash) {5172$hash= git_get_head_hash($project);5173}51745175my$name=$project;5176$name=~ s,([^/])/*\.git$,$1,;5177$name= basename($name);5178my$filename= to_utf8($name);5179$name=~s/\047/\047\\\047\047/g;5180my$cmd;5181$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5182$cmd= quote_command(5183 git_cmd(),'archive',5184"--format=$known_snapshot_formats{$format}{'format'}",5185"--prefix=$name/",$hash);5186if(exists$known_snapshot_formats{$format}{'compressor'}) {5187$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5188}51895190print$cgi->header(5191-type =>$known_snapshot_formats{$format}{'type'},5192-content_disposition =>'inline; filename="'."$filename".'"',5193-status =>'200 OK');51945195open my$fd,"-|",$cmd5196or die_error(500,"Execute git-archive failed");5197binmode STDOUT,':raw';5198print<$fd>;5199binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5200close$fd;5201}52025203sub git_log {5204my$head= git_get_head_hash($project);5205if(!defined$hash) {5206$hash=$head;5207}5208if(!defined$page) {5209$page=0;5210}5211my$refs= git_get_references();52125213my@commitlist= parse_commits($hash,101, (100*$page));52145215my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);52165217my($patch_max) = gitweb_get_feature('patches');5218if($patch_max) {5219if($patch_max<0||@commitlist<=$patch_max) {5220$paging_nav.=" ⋅ ".5221$cgi->a({-href => href(action=>"patches", -replay=>1)},5222"patches");5223}5224}52255226 git_header_html();5227 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);52285229if(!@commitlist) {5230my%co= parse_commit($hash);52315232 git_print_header_div('summary',$project);5233print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5234}5235my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5236for(my$i=0;$i<=$to;$i++) {5237my%co= %{$commitlist[$i]};5238next if!%co;5239my$commit=$co{'id'};5240my$ref= format_ref_marker($refs,$commit);5241my%ad= parse_date($co{'author_epoch'});5242 git_print_header_div('commit',5243"<span class=\"age\">$co{'age_string'}</span>".5244 esc_html($co{'title'}) .$ref,5245$commit);5246print"<div class=\"title_text\">\n".5247"<div class=\"log_link\">\n".5248$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5249" | ".5250$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5251" | ".5252$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5253"<br/>\n".5254"</div>\n";5255 git_print_authorship(\%co, -tag =>'span');5256print"<br/>\n</div>\n";52575258print"<div class=\"log_body\">\n";5259 git_print_log($co{'comment'}, -final_empty_line=>1);5260print"</div>\n";5261}5262if($#commitlist>=100) {5263print"<div class=\"page_nav\">\n";5264print$cgi->a({-href => href(-replay=>1, page=>$page+1),5265-accesskey =>"n", -title =>"Alt-n"},"next");5266print"</div>\n";5267}5268 git_footer_html();5269}52705271sub git_commit {5272$hash||=$hash_base||"HEAD";5273my%co= parse_commit($hash)5274or die_error(404,"Unknown commit object");52755276my$parent=$co{'parent'};5277my$parents=$co{'parents'};# listref52785279# we need to prepare $formats_nav before any parameter munging5280my$formats_nav;5281if(!defined$parent) {5282# --root commitdiff5283$formats_nav.='(initial)';5284}elsif(@$parents==1) {5285# single parent commit5286$formats_nav.=5287'(parent: '.5288$cgi->a({-href => href(action=>"commit",5289 hash=>$parent)},5290 esc_html(substr($parent,0,7))) .5291')';5292}else{5293# merge commit5294$formats_nav.=5295'(merge: '.5296join(' ',map{5297$cgi->a({-href => href(action=>"commit",5298 hash=>$_)},5299 esc_html(substr($_,0,7)));5300}@$parents) .5301')';5302}5303if(gitweb_check_feature('patches')) {5304$formats_nav.=" | ".5305$cgi->a({-href => href(action=>"patch", -replay=>1)},5306"patch");5307}53085309if(!defined$parent) {5310$parent="--root";5311}5312my@difftree;5313open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5314@diff_opts,5315(@$parents<=1?$parent:'-c'),5316$hash,"--"5317or die_error(500,"Open git-diff-tree failed");5318@difftree=map{chomp;$_} <$fd>;5319close$fdor die_error(404,"Reading git-diff-tree failed");53205321# non-textual hash id's can be cached5322my$expires;5323if($hash=~m/^[0-9a-fA-F]{40}$/) {5324$expires="+1d";5325}5326my$refs= git_get_references();5327my$ref= format_ref_marker($refs,$co{'id'});53285329 git_header_html(undef,$expires);5330 git_print_page_nav('commit','',5331$hash,$co{'tree'},$hash,5332$formats_nav);53335334if(defined$co{'parent'}) {5335 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5336}else{5337 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5338}5339print"<div class=\"title_text\">\n".5340"<table class=\"object_header\">\n";5341 git_print_authorship_rows(\%co);5342print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5343print"<tr>".5344"<td>tree</td>".5345"<td class=\"sha1\">".5346$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5347class=>"list"},$co{'tree'}) .5348"</td>".5349"<td class=\"link\">".5350$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5351"tree");5352my$snapshot_links= format_snapshot_links($hash);5353if(defined$snapshot_links) {5354print" | ".$snapshot_links;5355}5356print"</td>".5357"</tr>\n";53585359foreachmy$par(@$parents) {5360print"<tr>".5361"<td>parent</td>".5362"<td class=\"sha1\">".5363$cgi->a({-href => href(action=>"commit", hash=>$par),5364class=>"list"},$par) .5365"</td>".5366"<td class=\"link\">".5367$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5368" | ".5369$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5370"</td>".5371"</tr>\n";5372}5373print"</table>".5374"</div>\n";53755376print"<div class=\"page_body\">\n";5377 git_print_log($co{'comment'});5378print"</div>\n";53795380 git_difftree_body(\@difftree,$hash,@$parents);53815382 git_footer_html();5383}53845385sub git_object {5386# object is defined by:5387# - hash or hash_base alone5388# - hash_base and file_name5389my$type;53905391# - hash or hash_base alone5392if($hash|| ($hash_base&& !defined$file_name)) {5393my$object_id=$hash||$hash_base;53945395open my$fd,"-|", quote_command(5396 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5397or die_error(404,"Object does not exist");5398$type= <$fd>;5399chomp$type;5400close$fd5401or die_error(404,"Object does not exist");54025403# - hash_base and file_name5404}elsif($hash_base&&defined$file_name) {5405$file_name=~ s,/+$,,;54065407system(git_cmd(),"cat-file",'-e',$hash_base) ==05408or die_error(404,"Base object does not exist");54095410# here errors should not hapen5411open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5412or die_error(500,"Open git-ls-tree failed");5413my$line= <$fd>;5414close$fd;54155416#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5417unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5418 die_error(404,"File or directory for given base does not exist");5419}5420$type=$2;5421$hash=$3;5422}else{5423 die_error(400,"Not enough information to find object");5424}54255426print$cgi->redirect(-uri => href(action=>$type, -full=>1,5427 hash=>$hash, hash_base=>$hash_base,5428 file_name=>$file_name),5429-status =>'302 Found');5430}54315432sub git_blobdiff {5433my$format=shift||'html';54345435my$fd;5436my@difftree;5437my%diffinfo;5438my$expires;54395440# preparing $fd and %diffinfo for git_patchset_body5441# new style URI5442if(defined$hash_base&&defined$hash_parent_base) {5443if(defined$file_name) {5444# read raw output5445open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5446$hash_parent_base,$hash_base,5447"--", (defined$file_parent?$file_parent: ()),$file_name5448or die_error(500,"Open git-diff-tree failed");5449@difftree=map{chomp;$_} <$fd>;5450close$fd5451or die_error(404,"Reading git-diff-tree failed");5452@difftree5453or die_error(404,"Blob diff not found");54545455}elsif(defined$hash&&5456$hash=~/[0-9a-fA-F]{40}/) {5457# try to find filename from $hash54585459# read filtered raw output5460open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5461$hash_parent_base,$hash_base,"--"5462or die_error(500,"Open git-diff-tree failed");5463@difftree=5464# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5465# $hash == to_id5466grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5467map{chomp;$_} <$fd>;5468close$fd5469or die_error(404,"Reading git-diff-tree failed");5470@difftree5471or die_error(404,"Blob diff not found");54725473}else{5474 die_error(400,"Missing one of the blob diff parameters");5475}54765477if(@difftree>1) {5478 die_error(400,"Ambiguous blob diff specification");5479}54805481%diffinfo= parse_difftree_raw_line($difftree[0]);5482$file_parent||=$diffinfo{'from_file'} ||$file_name;5483$file_name||=$diffinfo{'to_file'};54845485$hash_parent||=$diffinfo{'from_id'};5486$hash||=$diffinfo{'to_id'};54875488# non-textual hash id's can be cached5489if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5490$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5491$expires='+1d';5492}54935494# open patch output5495open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5496'-p', ($formateq'html'?"--full-index": ()),5497$hash_parent_base,$hash_base,5498"--", (defined$file_parent?$file_parent: ()),$file_name5499or die_error(500,"Open git-diff-tree failed");5500}55015502# old/legacy style URI -- not generated anymore since 1.4.3.5503if(!%diffinfo) {5504 die_error('404 Not Found',"Missing one of the blob diff parameters")5505}55065507# header5508if($formateq'html') {5509my$formats_nav=5510$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5511"raw");5512 git_header_html(undef,$expires);5513if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5514 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5515 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5516}else{5517print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5518print"<div class=\"title\">$hashvs$hash_parent</div>\n";5519}5520if(defined$file_name) {5521 git_print_page_path($file_name,"blob",$hash_base);5522}else{5523print"<div class=\"page_path\"></div>\n";5524}55255526}elsif($formateq'plain') {5527print$cgi->header(5528-type =>'text/plain',5529-charset =>'utf-8',5530-expires =>$expires,5531-content_disposition =>'inline; filename="'."$file_name".'.patch"');55325533print"X-Git-Url: ".$cgi->self_url() ."\n\n";55345535}else{5536 die_error(400,"Unknown blobdiff format");5537}55385539# patch5540if($formateq'html') {5541print"<div class=\"page_body\">\n";55425543 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5544close$fd;55455546print"</div>\n";# class="page_body"5547 git_footer_html();55485549}else{5550while(my$line= <$fd>) {5551$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5552$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;55535554print$line;55555556last if$line=~m!^\+\+\+!;5557}5558local$/=undef;5559print<$fd>;5560close$fd;5561}5562}55635564sub git_blobdiff_plain {5565 git_blobdiff('plain');5566}55675568sub git_commitdiff {5569my%params=@_;5570my$format=$params{-format} ||'html';55715572my($patch_max) = gitweb_get_feature('patches');5573if($formateq'patch') {5574 die_error(403,"Patch view not allowed")unless$patch_max;5575}55765577$hash||=$hash_base||"HEAD";5578my%co= parse_commit($hash)5579or die_error(404,"Unknown commit object");55805581# choose format for commitdiff for merge5582if(!defined$hash_parent&& @{$co{'parents'}} >1) {5583$hash_parent='--cc';5584}5585# we need to prepare $formats_nav before almost any parameter munging5586my$formats_nav;5587if($formateq'html') {5588$formats_nav=5589$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5590"raw");5591if($patch_max) {5592$formats_nav.=" | ".5593$cgi->a({-href => href(action=>"patch", -replay=>1)},5594"patch");5595}55965597if(defined$hash_parent&&5598$hash_parentne'-c'&&$hash_parentne'--cc') {5599# commitdiff with two commits given5600my$hash_parent_short=$hash_parent;5601if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5602$hash_parent_short=substr($hash_parent,0,7);5603}5604$formats_nav.=5605' (from';5606for(my$i=0;$i< @{$co{'parents'}};$i++) {5607if($co{'parents'}[$i]eq$hash_parent) {5608$formats_nav.=' parent '. ($i+1);5609last;5610}5611}5612$formats_nav.=': '.5613$cgi->a({-href => href(action=>"commitdiff",5614 hash=>$hash_parent)},5615 esc_html($hash_parent_short)) .5616')';5617}elsif(!$co{'parent'}) {5618# --root commitdiff5619$formats_nav.=' (initial)';5620}elsif(scalar@{$co{'parents'}} ==1) {5621# single parent commit5622$formats_nav.=5623' (parent: '.5624$cgi->a({-href => href(action=>"commitdiff",5625 hash=>$co{'parent'})},5626 esc_html(substr($co{'parent'},0,7))) .5627')';5628}else{5629# merge commit5630if($hash_parenteq'--cc') {5631$formats_nav.=' | '.5632$cgi->a({-href => href(action=>"commitdiff",5633 hash=>$hash, hash_parent=>'-c')},5634'combined');5635}else{# $hash_parent eq '-c'5636$formats_nav.=' | '.5637$cgi->a({-href => href(action=>"commitdiff",5638 hash=>$hash, hash_parent=>'--cc')},5639'compact');5640}5641$formats_nav.=5642' (merge: '.5643join(' ',map{5644$cgi->a({-href => href(action=>"commitdiff",5645 hash=>$_)},5646 esc_html(substr($_,0,7)));5647} @{$co{'parents'}} ) .5648')';5649}5650}56515652my$hash_parent_param=$hash_parent;5653if(!defined$hash_parent_param) {5654# --cc for multiple parents, --root for parentless5655$hash_parent_param=5656@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5657}56585659# read commitdiff5660my$fd;5661my@difftree;5662if($formateq'html') {5663open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5664"--no-commit-id","--patch-with-raw","--full-index",5665$hash_parent_param,$hash,"--"5666or die_error(500,"Open git-diff-tree failed");56675668while(my$line= <$fd>) {5669chomp$line;5670# empty line ends raw part of diff-tree output5671last unless$line;5672push@difftree,scalar parse_difftree_raw_line($line);5673}56745675}elsif($formateq'plain') {5676open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5677'-p',$hash_parent_param,$hash,"--"5678or die_error(500,"Open git-diff-tree failed");5679}elsif($formateq'patch') {5680# For commit ranges, we limit the output to the number of5681# patches specified in the 'patches' feature.5682# For single commits, we limit the output to a single patch,5683# diverging from the git-format-patch default.5684my@commit_spec= ();5685if($hash_parent) {5686if($patch_max>0) {5687push@commit_spec,"-$patch_max";5688}5689push@commit_spec,'-n',"$hash_parent..$hash";5690}else{5691if($params{-single}) {5692push@commit_spec,'-1';5693}else{5694if($patch_max>0) {5695push@commit_spec,"-$patch_max";5696}5697push@commit_spec,"-n";5698}5699push@commit_spec,'--root',$hash;5700}5701open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5702'--stdout',@commit_spec5703or die_error(500,"Open git-format-patch failed");5704}else{5705 die_error(400,"Unknown commitdiff format");5706}57075708# non-textual hash id's can be cached5709my$expires;5710if($hash=~m/^[0-9a-fA-F]{40}$/) {5711$expires="+1d";5712}57135714# write commit message5715if($formateq'html') {5716my$refs= git_get_references();5717my$ref= format_ref_marker($refs,$co{'id'});57185719 git_header_html(undef,$expires);5720 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5721 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5722print"<div class=\"title_text\">\n".5723"<table class=\"object_header\">\n";5724 git_print_authorship_rows(\%co);5725print"</table>".5726"</div>\n";5727print"<div class=\"page_body\">\n";5728if(@{$co{'comment'}} >1) {5729print"<div class=\"log\">\n";5730 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5731print"</div>\n";# class="log"5732}57335734}elsif($formateq'plain') {5735my$refs= git_get_references("tags");5736my$tagname= git_get_rev_name_tags($hash);5737my$filename= basename($project) ."-$hash.patch";57385739print$cgi->header(5740-type =>'text/plain',5741-charset =>'utf-8',5742-expires =>$expires,5743-content_disposition =>'inline; filename="'."$filename".'"');5744my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5745print"From: ". to_utf8($co{'author'}) ."\n";5746print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5747print"Subject: ". to_utf8($co{'title'}) ."\n";57485749print"X-Git-Tag:$tagname\n"if$tagname;5750print"X-Git-Url: ".$cgi->self_url() ."\n\n";57515752foreachmy$line(@{$co{'comment'}}) {5753print to_utf8($line) ."\n";5754}5755print"---\n\n";5756}elsif($formateq'patch') {5757my$filename= basename($project) ."-$hash.patch";57585759print$cgi->header(5760-type =>'text/plain',5761-charset =>'utf-8',5762-expires =>$expires,5763-content_disposition =>'inline; filename="'."$filename".'"');5764}57655766# write patch5767if($formateq'html') {5768my$use_parents= !defined$hash_parent||5769$hash_parenteq'-c'||$hash_parenteq'--cc';5770 git_difftree_body(\@difftree,$hash,5771$use_parents? @{$co{'parents'}} :$hash_parent);5772print"<br/>\n";57735774 git_patchset_body($fd, \@difftree,$hash,5775$use_parents? @{$co{'parents'}} :$hash_parent);5776close$fd;5777print"</div>\n";# class="page_body"5778 git_footer_html();57795780}elsif($formateq'plain') {5781local$/=undef;5782print<$fd>;5783close$fd5784or print"Reading git-diff-tree failed\n";5785}elsif($formateq'patch') {5786local$/=undef;5787print<$fd>;5788close$fd5789or print"Reading git-format-patch failed\n";5790}5791}57925793sub git_commitdiff_plain {5794 git_commitdiff(-format =>'plain');5795}57965797# format-patch-style patches5798sub git_patch {5799 git_commitdiff(-format =>'patch', -single=>1);5800}58015802sub git_patches {5803 git_commitdiff(-format =>'patch');5804}58055806sub git_history {5807if(!defined$hash_base) {5808$hash_base= git_get_head_hash($project);5809}5810if(!defined$page) {5811$page=0;5812}5813my$ftype;5814my%co= parse_commit($hash_base)5815or die_error(404,"Unknown commit object");58165817my$refs= git_get_references();5818my$limit=sprintf("--max-count=%i", (100* ($page+1)));58195820my@commitlist= parse_commits($hash_base,101, (100*$page),5821$file_name,"--full-history")5822or die_error(404,"No such file or directory on given branch");58235824if(!defined$hash&&defined$file_name) {5825# some commits could have deleted file in question,5826# and not have it in tree, but one of them has to have it5827for(my$i=0;$i<=@commitlist;$i++) {5828$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5829last ifdefined$hash;5830}5831}5832if(defined$hash) {5833$ftype= git_get_type($hash);5834}5835if(!defined$ftype) {5836 die_error(500,"Unknown type of object");5837}58385839my$paging_nav='';5840if($page>0) {5841$paging_nav.=5842$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5843 file_name=>$file_name)},5844"first");5845$paging_nav.=" ⋅ ".5846$cgi->a({-href => href(-replay=>1, page=>$page-1),5847-accesskey =>"p", -title =>"Alt-p"},"prev");5848}else{5849$paging_nav.="first";5850$paging_nav.=" ⋅ prev";5851}5852my$next_link='';5853if($#commitlist>=100) {5854$next_link=5855$cgi->a({-href => href(-replay=>1, page=>$page+1),5856-accesskey =>"n", -title =>"Alt-n"},"next");5857$paging_nav.=" ⋅$next_link";5858}else{5859$paging_nav.=" ⋅ next";5860}58615862 git_header_html();5863 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5864 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5865 git_print_page_path($file_name,$ftype,$hash_base);58665867 git_history_body(\@commitlist,0,99,5868$refs,$hash_base,$ftype,$next_link);58695870 git_footer_html();5871}58725873sub git_search {5874 gitweb_check_feature('search')or die_error(403,"Search is disabled");5875if(!defined$searchtext) {5876 die_error(400,"Text field is empty");5877}5878if(!defined$hash) {5879$hash= git_get_head_hash($project);5880}5881my%co= parse_commit($hash);5882if(!%co) {5883 die_error(404,"Unknown commit object");5884}5885if(!defined$page) {5886$page=0;5887}58885889$searchtype||='commit';5890if($searchtypeeq'pickaxe') {5891# pickaxe may take all resources of your box and run for several minutes5892# with every query - so decide by yourself how public you make this feature5893 gitweb_check_feature('pickaxe')5894or die_error(403,"Pickaxe is disabled");5895}5896if($searchtypeeq'grep') {5897 gitweb_check_feature('grep')5898or die_error(403,"Grep is disabled");5899}59005901 git_header_html();59025903if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5904my$greptype;5905if($searchtypeeq'commit') {5906$greptype="--grep=";5907}elsif($searchtypeeq'author') {5908$greptype="--author=";5909}elsif($searchtypeeq'committer') {5910$greptype="--committer=";5911}5912$greptype.=$searchtext;5913my@commitlist= parse_commits($hash,101, (100*$page),undef,5914$greptype,'--regexp-ignore-case',5915$search_use_regexp?'--extended-regexp':'--fixed-strings');59165917my$paging_nav='';5918if($page>0) {5919$paging_nav.=5920$cgi->a({-href => href(action=>"search", hash=>$hash,5921 searchtext=>$searchtext,5922 searchtype=>$searchtype)},5923"first");5924$paging_nav.=" ⋅ ".5925$cgi->a({-href => href(-replay=>1, page=>$page-1),5926-accesskey =>"p", -title =>"Alt-p"},"prev");5927}else{5928$paging_nav.="first";5929$paging_nav.=" ⋅ prev";5930}5931my$next_link='';5932if($#commitlist>=100) {5933$next_link=5934$cgi->a({-href => href(-replay=>1, page=>$page+1),5935-accesskey =>"n", -title =>"Alt-n"},"next");5936$paging_nav.=" ⋅$next_link";5937}else{5938$paging_nav.=" ⋅ next";5939}59405941if($#commitlist>=100) {5942}59435944 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5945 git_print_header_div('commit', esc_html($co{'title'}),$hash);5946 git_search_grep_body(\@commitlist,0,99,$next_link);5947}59485949if($searchtypeeq'pickaxe') {5950 git_print_page_nav('','',$hash,$co{'tree'},$hash);5951 git_print_header_div('commit', esc_html($co{'title'}),$hash);59525953print"<table class=\"pickaxe search\">\n";5954my$alternate=1;5955local$/="\n";5956open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5957'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5958($search_use_regexp?'--pickaxe-regex': ());5959undef%co;5960my@files;5961while(my$line= <$fd>) {5962chomp$line;5963next unless$line;59645965my%set= parse_difftree_raw_line($line);5966if(defined$set{'commit'}) {5967# finish previous commit5968if(%co) {5969print"</td>\n".5970"<td class=\"link\">".5971$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5972" | ".5973$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5974print"</td>\n".5975"</tr>\n";5976}59775978if($alternate) {5979print"<tr class=\"dark\">\n";5980}else{5981print"<tr class=\"light\">\n";5982}5983$alternate^=1;5984%co= parse_commit($set{'commit'});5985my$author= chop_and_escape_str($co{'author_name'},15,5);5986print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5987"<td><i>$author</i></td>\n".5988"<td>".5989$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5990-class=>"list subject"},5991 chop_and_escape_str($co{'title'},50) ."<br/>");5992}elsif(defined$set{'to_id'}) {5993next if($set{'to_id'} =~m/^0{40}$/);59945995print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5996 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5997-class=>"list"},5998"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5999"<br/>\n";6000}6001}6002close$fd;60036004# finish last commit (warning: repetition!)6005if(%co) {6006print"</td>\n".6007"<td class=\"link\">".6008$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6009" | ".6010$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6011print"</td>\n".6012"</tr>\n";6013}60146015print"</table>\n";6016}60176018if($searchtypeeq'grep') {6019 git_print_page_nav('','',$hash,$co{'tree'},$hash);6020 git_print_header_div('commit', esc_html($co{'title'}),$hash);60216022print"<table class=\"grep_search\">\n";6023my$alternate=1;6024my$matches=0;6025local$/="\n";6026open my$fd,"-|", git_cmd(),'grep','-n',6027$search_use_regexp? ('-E','-i') :'-F',6028$searchtext,$co{'tree'};6029my$lastfile='';6030while(my$line= <$fd>) {6031chomp$line;6032my($file,$lno,$ltext,$binary);6033last if($matches++>1000);6034if($line=~/^Binary file (.+) matches$/) {6035$file=$1;6036$binary=1;6037}else{6038(undef,$file,$lno,$ltext) =split(/:/,$line,4);6039}6040if($filene$lastfile) {6041$lastfileand print"</td></tr>\n";6042if($alternate++) {6043print"<tr class=\"dark\">\n";6044}else{6045print"<tr class=\"light\">\n";6046}6047print"<td class=\"list\">".6048$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6049 file_name=>"$file"),6050-class=>"list"}, esc_path($file));6051print"</td><td>\n";6052$lastfile=$file;6053}6054if($binary) {6055print"<div class=\"binary\">Binary file</div>\n";6056}else{6057$ltext= untabify($ltext);6058if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6059$ltext= esc_html($1, -nbsp=>1);6060$ltext.='<span class="match">';6061$ltext.= esc_html($2, -nbsp=>1);6062$ltext.='</span>';6063$ltext.= esc_html($3, -nbsp=>1);6064}else{6065$ltext= esc_html($ltext, -nbsp=>1);6066}6067print"<div class=\"pre\">".6068$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6069 file_name=>"$file").'#l'.$lno,6070-class=>"linenr"},sprintf('%4i',$lno))6071.' '.$ltext."</div>\n";6072}6073}6074if($lastfile) {6075print"</td></tr>\n";6076if($matches>1000) {6077print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6078}6079}else{6080print"<div class=\"diff nodifferences\">No matches found</div>\n";6081}6082close$fd;60836084print"</table>\n";6085}6086 git_footer_html();6087}60886089sub git_search_help {6090 git_header_html();6091 git_print_page_nav('','',$hash,$hash,$hash);6092print<<EOT;6093<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6094regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6095the pattern entered is recognized as the POSIX extended6096<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6097insensitive).</p>6098<dl>6099<dt><b>commit</b></dt>6100<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6101EOT6102my$have_grep= gitweb_check_feature('grep');6103if($have_grep) {6104print<<EOT;6105<dt><b>grep</b></dt>6106<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6107 a different one) are searched for the given pattern. On large trees, this search can take6108a while and put some strain on the server, so please use it with some consideration. Note that6109due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6110case-sensitive.</dd>6111EOT6112}6113print<<EOT;6114<dt><b>author</b></dt>6115<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6116<dt><b>committer</b></dt>6117<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6118EOT6119my$have_pickaxe= gitweb_check_feature('pickaxe');6120if($have_pickaxe) {6121print<<EOT;6122<dt><b>pickaxe</b></dt>6123<dd>All commits that caused the string to appear or disappear from any file (changes that6124added, removed or "modified" the string) will be listed. This search can take a while and6125takes a lot of strain on the server, so please use it wisely. Note that since you may be6126interested even in changes just changing the case as well, this search is case sensitive.</dd>6127EOT6128}6129print"</dl>\n";6130 git_footer_html();6131}61326133sub git_shortlog {6134my$head= git_get_head_hash($project);6135if(!defined$hash) {6136$hash=$head;6137}6138if(!defined$page) {6139$page=0;6140}6141my$refs= git_get_references();61426143my$commit_hash=$hash;6144if(defined$hash_parent) {6145$commit_hash="$hash_parent..$hash";6146}6147my@commitlist= parse_commits($commit_hash,101, (100*$page));61486149my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);6150my$next_link='';6151if($#commitlist>=100) {6152$next_link=6153$cgi->a({-href => href(-replay=>1, page=>$page+1),6154-accesskey =>"n", -title =>"Alt-n"},"next");6155}6156my$patch_max= gitweb_check_feature('patches');6157if($patch_max) {6158if($patch_max<0||@commitlist<=$patch_max) {6159$paging_nav.=" ⋅ ".6160$cgi->a({-href => href(action=>"patches", -replay=>1)},6161"patches");6162}6163}61646165 git_header_html();6166 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6167 git_print_header_div('summary',$project);61686169 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);61706171 git_footer_html();6172}61736174## ......................................................................6175## feeds (RSS, Atom; OPML)61766177sub git_feed {6178my$format=shift||'atom';6179my$have_blame= gitweb_check_feature('blame');61806181# Atom: http://www.atomenabled.org/developers/syndication/6182# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6183if($formatne'rss'&&$formatne'atom') {6184 die_error(400,"Unknown web feed format");6185}61866187# log/feed of current (HEAD) branch, log of given branch, history of file/directory6188my$head=$hash||'HEAD';6189my@commitlist= parse_commits($head,150,0,$file_name);61906191my%latest_commit;6192my%latest_date;6193my$content_type="application/$format+xml";6194if(defined$cgi->http('HTTP_ACCEPT') &&6195$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6196# browser (feed reader) prefers text/xml6197$content_type='text/xml';6198}6199if(defined($commitlist[0])) {6200%latest_commit= %{$commitlist[0]};6201my$latest_epoch=$latest_commit{'committer_epoch'};6202%latest_date= parse_date($latest_epoch);6203my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6204if(defined$if_modified) {6205my$since;6206if(eval{require HTTP::Date;1; }) {6207$since= HTTP::Date::str2time($if_modified);6208}elsif(eval{require Time::ParseDate;1; }) {6209$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6210}6211if(defined$since&&$latest_epoch<=$since) {6212print$cgi->header(6213-type =>$content_type,6214-charset =>'utf-8',6215-last_modified =>$latest_date{'rfc2822'},6216-status =>'304 Not Modified');6217return;6218}6219}6220print$cgi->header(6221-type =>$content_type,6222-charset =>'utf-8',6223-last_modified =>$latest_date{'rfc2822'});6224}else{6225print$cgi->header(6226-type =>$content_type,6227-charset =>'utf-8');6228}62296230# Optimization: skip generating the body if client asks only6231# for Last-Modified date.6232return if($cgi->request_method()eq'HEAD');62336234# header variables6235my$title="$site_name-$project/$action";6236my$feed_type='log';6237if(defined$hash) {6238$title.=" - '$hash'";6239$feed_type='branch log';6240if(defined$file_name) {6241$title.=" ::$file_name";6242$feed_type='history';6243}6244}elsif(defined$file_name) {6245$title.=" -$file_name";6246$feed_type='history';6247}6248$title.="$feed_type";6249my$descr= git_get_project_description($project);6250if(defined$descr) {6251$descr= esc_html($descr);6252}else{6253$descr="$project".6254($formateq'rss'?'RSS':'Atom') .6255" feed";6256}6257my$owner= git_get_project_owner($project);6258$owner= esc_html($owner);62596260#header6261my$alt_url;6262if(defined$file_name) {6263$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6264}elsif(defined$hash) {6265$alt_url= href(-full=>1, action=>"log", hash=>$hash);6266}else{6267$alt_url= href(-full=>1, action=>"summary");6268}6269print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6270if($formateq'rss') {6271print<<XML;6272<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6273<channel>6274XML6275print"<title>$title</title>\n".6276"<link>$alt_url</link>\n".6277"<description>$descr</description>\n".6278"<language>en</language>\n".6279# project owner is responsible for 'editorial' content6280"<managingEditor>$owner</managingEditor>\n";6281if(defined$logo||defined$favicon) {6282# prefer the logo to the favicon, since RSS6283# doesn't allow both6284my$img= esc_url($logo||$favicon);6285print"<image>\n".6286"<url>$img</url>\n".6287"<title>$title</title>\n".6288"<link>$alt_url</link>\n".6289"</image>\n";6290}6291if(%latest_date) {6292print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6293print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6294}6295print"<generator>gitweb v.$version/$git_version</generator>\n";6296}elsif($formateq'atom') {6297print<<XML;6298<feed xmlns="http://www.w3.org/2005/Atom">6299XML6300print"<title>$title</title>\n".6301"<subtitle>$descr</subtitle>\n".6302'<link rel="alternate" type="text/html" href="'.6303$alt_url.'" />'."\n".6304'<link rel="self" type="'.$content_type.'" href="'.6305$cgi->self_url() .'" />'."\n".6306"<id>". href(-full=>1) ."</id>\n".6307# use project owner for feed author6308"<author><name>$owner</name></author>\n";6309if(defined$favicon) {6310print"<icon>". esc_url($favicon) ."</icon>\n";6311}6312if(defined$logo_url) {6313# not twice as wide as tall: 72 x 27 pixels6314print"<logo>". esc_url($logo) ."</logo>\n";6315}6316if(!%latest_date) {6317# dummy date to keep the feed valid until commits trickle in:6318print"<updated>1970-01-01T00:00:00Z</updated>\n";6319}else{6320print"<updated>$latest_date{'iso-8601'}</updated>\n";6321}6322print"<generator version='$version/$git_version'>gitweb</generator>\n";6323}63246325# contents6326for(my$i=0;$i<=$#commitlist;$i++) {6327my%co= %{$commitlist[$i]};6328my$commit=$co{'id'};6329# we read 150, we always show 30 and the ones more recent than 48 hours6330if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6331last;6332}6333my%cd= parse_date($co{'author_epoch'});63346335# get list of changed files6336open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6337$co{'parent'} ||"--root",6338$co{'id'},"--", (defined$file_name?$file_name: ())6339ornext;6340my@difftree=map{chomp;$_} <$fd>;6341close$fd6342ornext;63436344# print element (entry, item)6345my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6346if($formateq'rss') {6347print"<item>\n".6348"<title>". esc_html($co{'title'}) ."</title>\n".6349"<author>". esc_html($co{'author'}) ."</author>\n".6350"<pubDate>$cd{'rfc2822'}</pubDate>\n".6351"<guid isPermaLink=\"true\">$co_url</guid>\n".6352"<link>$co_url</link>\n".6353"<description>". esc_html($co{'title'}) ."</description>\n".6354"<content:encoded>".6355"<![CDATA[\n";6356}elsif($formateq'atom') {6357print"<entry>\n".6358"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6359"<updated>$cd{'iso-8601'}</updated>\n".6360"<author>\n".6361" <name>". esc_html($co{'author_name'}) ."</name>\n";6362if($co{'author_email'}) {6363print" <email>". esc_html($co{'author_email'}) ."</email>\n";6364}6365print"</author>\n".6366# use committer for contributor6367"<contributor>\n".6368" <name>". esc_html($co{'committer_name'}) ."</name>\n";6369if($co{'committer_email'}) {6370print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6371}6372print"</contributor>\n".6373"<published>$cd{'iso-8601'}</published>\n".6374"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6375"<id>$co_url</id>\n".6376"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6377"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6378}6379my$comment=$co{'comment'};6380print"<pre>\n";6381foreachmy$line(@$comment) {6382$line= esc_html($line);6383print"$line\n";6384}6385print"</pre><ul>\n";6386foreachmy$difftree_line(@difftree) {6387my%difftree= parse_difftree_raw_line($difftree_line);6388next if!$difftree{'from_id'};63896390my$file=$difftree{'file'} ||$difftree{'to_file'};63916392print"<li>".6393"[".6394$cgi->a({-href => href(-full=>1, action=>"blobdiff",6395 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6396 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6397 file_name=>$file, file_parent=>$difftree{'from_file'}),6398-title =>"diff"},'D');6399if($have_blame) {6400print$cgi->a({-href => href(-full=>1, action=>"blame",6401 file_name=>$file, hash_base=>$commit),6402-title =>"blame"},'B');6403}6404# if this is not a feed of a file history6405if(!defined$file_name||$file_namene$file) {6406print$cgi->a({-href => href(-full=>1, action=>"history",6407 file_name=>$file, hash=>$commit),6408-title =>"history"},'H');6409}6410$file= esc_path($file);6411print"] ".6412"$file</li>\n";6413}6414if($formateq'rss') {6415print"</ul>]]>\n".6416"</content:encoded>\n".6417"</item>\n";6418}elsif($formateq'atom') {6419print"</ul>\n</div>\n".6420"</content>\n".6421"</entry>\n";6422}6423}64246425# end of feed6426if($formateq'rss') {6427print"</channel>\n</rss>\n";6428}elsif($formateq'atom') {6429print"</feed>\n";6430}6431}64326433sub git_rss {6434 git_feed('rss');6435}64366437sub git_atom {6438 git_feed('atom');6439}64406441sub git_opml {6442my@list= git_get_projects_list();64436444print$cgi->header(6445-type =>'text/xml',6446-charset =>'utf-8',6447-content_disposition =>'inline; filename="opml.xml"');64486449print<<XML;6450<?xml version="1.0" encoding="utf-8"?>6451<opml version="1.0">6452<head>6453 <title>$site_nameOPML Export</title>6454</head>6455<body>6456<outline text="git RSS feeds">6457XML64586459foreachmy$pr(@list) {6460my%proj=%$pr;6461my$head= git_get_head_hash($proj{'path'});6462if(!defined$head) {6463next;6464}6465$git_dir="$projectroot/$proj{'path'}";6466my%co= parse_commit($head);6467if(!%co) {6468next;6469}64706471my$path= esc_html(chop_str($proj{'path'},25,5));6472my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6473my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6474print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6475}6476print<<XML;6477</outline>6478</body>6479</opml>6480XML6481}