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# 'disabled' => boolean (optional)} 165# 166'tgz'=> { 167'display'=>'tar.gz', 168'type'=>'application/x-gzip', 169'suffix'=>'.tar.gz', 170'format'=>'tar', 171'compressor'=> ['gzip']}, 172 173'tbz2'=> { 174'display'=>'tar.bz2', 175'type'=>'application/x-bzip2', 176'suffix'=>'.tar.bz2', 177'format'=>'tar', 178'compressor'=> ['bzip2']}, 179 180'zip'=> { 181'display'=>'zip', 182'type'=>'application/x-zip', 183'suffix'=>'.zip', 184'format'=>'zip'}, 185); 186 187# Aliases so we understand old gitweb.snapshot values in repository 188# configuration. 189our%known_snapshot_format_aliases= ( 190'gzip'=>'tgz', 191'bzip2'=>'tbz2', 192 193# backward compatibility: legacy gitweb config support 194'x-gzip'=>undef,'gz'=>undef, 195'x-bzip2'=>undef,'bz2'=>undef, 196'x-zip'=>undef,''=>undef, 197); 198 199# Pixel sizes for icons and avatars. If the default font sizes or lineheights 200# are changed, it may be appropriate to change these values too via 201# $GITWEB_CONFIG. 202our%avatar_size= ( 203'default'=>16, 204'double'=>32 205); 206 207# You define site-wide feature defaults here; override them with 208# $GITWEB_CONFIG as necessary. 209our%feature= ( 210# feature => { 211# 'sub' => feature-sub (subroutine), 212# 'override' => allow-override (boolean), 213# 'default' => [ default options...] (array reference)} 214# 215# if feature is overridable (it means that allow-override has true value), 216# then feature-sub will be called with default options as parameters; 217# return value of feature-sub indicates if to enable specified feature 218# 219# if there is no 'sub' key (no feature-sub), then feature cannot be 220# overriden 221# 222# use gitweb_get_feature(<feature>) to retrieve the <feature> value 223# (an array) or gitweb_check_feature(<feature>) to check if <feature> 224# is enabled 225 226# Enable the 'blame' blob view, showing the last commit that modified 227# each line in the file. This can be very CPU-intensive. 228 229# To enable system wide have in $GITWEB_CONFIG 230# $feature{'blame'}{'default'} = [1]; 231# To have project specific config enable override in $GITWEB_CONFIG 232# $feature{'blame'}{'override'} = 1; 233# and in project config gitweb.blame = 0|1; 234'blame'=> { 235'sub'=>sub{ feature_bool('blame',@_) }, 236'override'=>0, 237'default'=> [0]}, 238 239# Enable the 'snapshot' link, providing a compressed archive of any 240# tree. This can potentially generate high traffic if you have large 241# project. 242 243# Value is a list of formats defined in %known_snapshot_formats that 244# you wish to offer. 245# To disable system wide have in $GITWEB_CONFIG 246# $feature{'snapshot'}{'default'} = []; 247# To have project specific config enable override in $GITWEB_CONFIG 248# $feature{'snapshot'}{'override'} = 1; 249# and in project config, a comma-separated list of formats or "none" 250# to disable. Example: gitweb.snapshot = tbz2,zip; 251'snapshot'=> { 252'sub'=> \&feature_snapshot, 253'override'=>0, 254'default'=> ['tgz']}, 255 256# Enable text search, which will list the commits which match author, 257# committer or commit text to a given string. Enabled by default. 258# Project specific override is not supported. 259'search'=> { 260'override'=>0, 261'default'=> [1]}, 262 263# Enable grep search, which will list the files in currently selected 264# tree containing the given string. Enabled by default. This can be 265# potentially CPU-intensive, of course. 266 267# To enable system wide have in $GITWEB_CONFIG 268# $feature{'grep'}{'default'} = [1]; 269# To have project specific config enable override in $GITWEB_CONFIG 270# $feature{'grep'}{'override'} = 1; 271# and in project config gitweb.grep = 0|1; 272'grep'=> { 273'sub'=>sub{ feature_bool('grep',@_) }, 274'override'=>0, 275'default'=> [1]}, 276 277# Enable the pickaxe search, which will list the commits that modified 278# a given string in a file. This can be practical and quite faster 279# alternative to 'blame', but still potentially CPU-intensive. 280 281# To enable system wide have in $GITWEB_CONFIG 282# $feature{'pickaxe'}{'default'} = [1]; 283# To have project specific config enable override in $GITWEB_CONFIG 284# $feature{'pickaxe'}{'override'} = 1; 285# and in project config gitweb.pickaxe = 0|1; 286'pickaxe'=> { 287'sub'=>sub{ feature_bool('pickaxe',@_) }, 288'override'=>0, 289'default'=> [1]}, 290 291# Make gitweb use an alternative format of the URLs which can be 292# more readable and natural-looking: project name is embedded 293# directly in the path and the query string contains other 294# auxiliary information. All gitweb installations recognize 295# URL in either format; this configures in which formats gitweb 296# generates links. 297 298# To enable system wide have in $GITWEB_CONFIG 299# $feature{'pathinfo'}{'default'} = [1]; 300# Project specific override is not supported. 301 302# Note that you will need to change the default location of CSS, 303# favicon, logo and possibly other files to an absolute URL. Also, 304# if gitweb.cgi serves as your indexfile, you will need to force 305# $my_uri to contain the script name in your $GITWEB_CONFIG. 306'pathinfo'=> { 307'override'=>0, 308'default'=> [0]}, 309 310# Make gitweb consider projects in project root subdirectories 311# to be forks of existing projects. Given project $projname.git, 312# projects matching $projname/*.git will not be shown in the main 313# projects list, instead a '+' mark will be added to $projname 314# there and a 'forks' view will be enabled for the project, listing 315# all the forks. If project list is taken from a file, forks have 316# to be listed after the main project. 317 318# To enable system wide have in $GITWEB_CONFIG 319# $feature{'forks'}{'default'} = [1]; 320# Project specific override is not supported. 321'forks'=> { 322'override'=>0, 323'default'=> [0]}, 324 325# Insert custom links to the action bar of all project pages. 326# This enables you mainly to link to third-party scripts integrating 327# into gitweb; e.g. git-browser for graphical history representation 328# or custom web-based repository administration interface. 329 330# The 'default' value consists of a list of triplets in the form 331# (label, link, position) where position is the label after which 332# to insert the link and link is a format string where %n expands 333# to the project name, %f to the project path within the filesystem, 334# %h to the current hash (h gitweb parameter) and %b to the current 335# hash base (hb gitweb parameter); %% expands to %. 336 337# To enable system wide have in $GITWEB_CONFIG e.g. 338# $feature{'actions'}{'default'} = [('graphiclog', 339# '/git-browser/by-commit.html?r=%n', 'summary')]; 340# Project specific override is not supported. 341'actions'=> { 342'override'=>0, 343'default'=> []}, 344 345# Allow gitweb scan project content tags described in ctags/ 346# of project repository, and display the popular Web 2.0-ish 347# "tag cloud" near the project list. Note that this is something 348# COMPLETELY different from the normal Git tags. 349 350# gitweb by itself can show existing tags, but it does not handle 351# tagging itself; you need an external application for that. 352# For an example script, check Girocco's cgi/tagproj.cgi. 353# You may want to install the HTML::TagCloud Perl module to get 354# a pretty tag cloud instead of just a list of tags. 355 356# To enable system wide have in $GITWEB_CONFIG 357# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 358# Project specific override is not supported. 359'ctags'=> { 360'override'=>0, 361'default'=> [0]}, 362 363# The maximum number of patches in a patchset generated in patch 364# view. Set this to 0 or undef to disable patch view, or to a 365# negative number to remove any limit. 366 367# To disable system wide have in $GITWEB_CONFIG 368# $feature{'patches'}{'default'} = [0]; 369# To have project specific config enable override in $GITWEB_CONFIG 370# $feature{'patches'}{'override'} = 1; 371# and in project config gitweb.patches = 0|n; 372# where n is the maximum number of patches allowed in a patchset. 373'patches'=> { 374'sub'=> \&feature_patches, 375'override'=>0, 376'default'=> [16]}, 377 378# Avatar support. When this feature is enabled, views such as 379# shortlog or commit will display an avatar associated with 380# the email of the committer(s) and/or author(s). 381 382# Currently available providers are gravatar and picon. 383# If an unknown provider is specified, the feature is disabled. 384 385# Gravatar depends on Digest::MD5. 386# Picon currently relies on the indiana.edu database. 387 388# To enable system wide have in $GITWEB_CONFIG 389# $feature{'avatar'}{'default'} = ['<provider>']; 390# where <provider> is either gravatar or picon. 391# To have project specific config enable override in $GITWEB_CONFIG 392# $feature{'avatar'}{'override'} = 1; 393# and in project config gitweb.avatar = <provider>; 394'avatar'=> { 395'sub'=> \&feature_avatar, 396'override'=>0, 397'default'=> ['']}, 398); 399 400sub gitweb_get_feature { 401my($name) =@_; 402return unlessexists$feature{$name}; 403my($sub,$override,@defaults) = ( 404$feature{$name}{'sub'}, 405$feature{$name}{'override'}, 406@{$feature{$name}{'default'}}); 407if(!$override) {return@defaults; } 408if(!defined$sub) { 409warn"feature$nameis not overrideable"; 410return@defaults; 411} 412return$sub->(@defaults); 413} 414 415# A wrapper to check if a given feature is enabled. 416# With this, you can say 417# 418# my $bool_feat = gitweb_check_feature('bool_feat'); 419# gitweb_check_feature('bool_feat') or somecode; 420# 421# instead of 422# 423# my ($bool_feat) = gitweb_get_feature('bool_feat'); 424# (gitweb_get_feature('bool_feat'))[0] or somecode; 425# 426sub gitweb_check_feature { 427return(gitweb_get_feature(@_))[0]; 428} 429 430 431sub feature_bool { 432my$key=shift; 433my($val) = git_get_project_config($key,'--bool'); 434 435if(!defined$val) { 436return($_[0]); 437}elsif($valeq'true') { 438return(1); 439}elsif($valeq'false') { 440return(0); 441} 442} 443 444sub feature_snapshot { 445my(@fmts) =@_; 446 447my($val) = git_get_project_config('snapshot'); 448 449if($val) { 450@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 451} 452 453return@fmts; 454} 455 456sub feature_patches { 457my@val= (git_get_project_config('patches','--int')); 458 459if(@val) { 460return@val; 461} 462 463return($_[0]); 464} 465 466sub feature_avatar { 467my@val= (git_get_project_config('avatar')); 468 469return@val?@val:@_; 470} 471 472# checking HEAD file with -e is fragile if the repository was 473# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 474# and then pruned. 475sub check_head_link { 476my($dir) =@_; 477my$headfile="$dir/HEAD"; 478return((-e $headfile) || 479(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 480} 481 482sub check_export_ok { 483my($dir) =@_; 484return(check_head_link($dir) && 485(!$export_ok|| -e "$dir/$export_ok") && 486(!$export_auth_hook||$export_auth_hook->($dir))); 487} 488 489# process alternate names for backward compatibility 490# filter out unsupported (unknown) snapshot formats 491sub filter_snapshot_fmts { 492my@fmts=@_; 493 494@fmts=map{ 495exists$known_snapshot_format_aliases{$_} ? 496$known_snapshot_format_aliases{$_} :$_}@fmts; 497@fmts=grep{ 498exists$known_snapshot_formats{$_} && 499!$known_snapshot_formats{$_}{'disabled'}}@fmts; 500} 501 502our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 503if(-e $GITWEB_CONFIG) { 504do$GITWEB_CONFIG; 505}else{ 506our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 507do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 508} 509 510# version of the core git binary 511our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 512 513$projects_list||=$projectroot; 514 515# ====================================================================== 516# input validation and dispatch 517 518# input parameters can be collected from a variety of sources (presently, CGI 519# and PATH_INFO), so we define an %input_params hash that collects them all 520# together during validation: this allows subsequent uses (e.g. href()) to be 521# agnostic of the parameter origin 522 523our%input_params= (); 524 525# input parameters are stored with the long parameter name as key. This will 526# also be used in the href subroutine to convert parameters to their CGI 527# equivalent, and since the href() usage is the most frequent one, we store 528# the name -> CGI key mapping here, instead of the reverse. 529# 530# XXX: Warning: If you touch this, check the search form for updating, 531# too. 532 533our@cgi_param_mapping= ( 534 project =>"p", 535 action =>"a", 536 file_name =>"f", 537 file_parent =>"fp", 538 hash =>"h", 539 hash_parent =>"hp", 540 hash_base =>"hb", 541 hash_parent_base =>"hpb", 542 page =>"pg", 543 order =>"o", 544 searchtext =>"s", 545 searchtype =>"st", 546 snapshot_format =>"sf", 547 extra_options =>"opt", 548 search_use_regexp =>"sr", 549); 550our%cgi_param_mapping=@cgi_param_mapping; 551 552# we will also need to know the possible actions, for validation 553our%actions= ( 554"blame"=> \&git_blame, 555"blobdiff"=> \&git_blobdiff, 556"blobdiff_plain"=> \&git_blobdiff_plain, 557"blob"=> \&git_blob, 558"blob_plain"=> \&git_blob_plain, 559"commitdiff"=> \&git_commitdiff, 560"commitdiff_plain"=> \&git_commitdiff_plain, 561"commit"=> \&git_commit, 562"forks"=> \&git_forks, 563"heads"=> \&git_heads, 564"history"=> \&git_history, 565"log"=> \&git_log, 566"patch"=> \&git_patch, 567"patches"=> \&git_patches, 568"rss"=> \&git_rss, 569"atom"=> \&git_atom, 570"search"=> \&git_search, 571"search_help"=> \&git_search_help, 572"shortlog"=> \&git_shortlog, 573"summary"=> \&git_summary, 574"tag"=> \&git_tag, 575"tags"=> \&git_tags, 576"tree"=> \&git_tree, 577"snapshot"=> \&git_snapshot, 578"object"=> \&git_object, 579# those below don't need $project 580"opml"=> \&git_opml, 581"project_list"=> \&git_project_list, 582"project_index"=> \&git_project_index, 583); 584 585# finally, we have the hash of allowed extra_options for the commands that 586# allow them 587our%allowed_options= ( 588"--no-merges"=> [qw(rss atom log shortlog history)], 589); 590 591# fill %input_params with the CGI parameters. All values except for 'opt' 592# should be single values, but opt can be an array. We should probably 593# build an array of parameters that can be multi-valued, but since for the time 594# being it's only this one, we just single it out 595while(my($name,$symbol) =each%cgi_param_mapping) { 596if($symboleq'opt') { 597$input_params{$name} = [$cgi->param($symbol) ]; 598}else{ 599$input_params{$name} =$cgi->param($symbol); 600} 601} 602 603# now read PATH_INFO and update the parameter list for missing parameters 604sub evaluate_path_info { 605return ifdefined$input_params{'project'}; 606return if!$path_info; 607$path_info=~ s,^/+,,; 608return if!$path_info; 609 610# find which part of PATH_INFO is project 611my$project=$path_info; 612$project=~ s,/+$,,; 613while($project&& !check_head_link("$projectroot/$project")) { 614$project=~ s,/*[^/]*$,,; 615} 616return unless$project; 617$input_params{'project'} =$project; 618 619# do not change any parameters if an action is given using the query string 620return if$input_params{'action'}; 621$path_info=~ s,^\Q$project\E/*,,; 622 623# next, check if we have an action 624my$action=$path_info; 625$action=~ s,/.*$,,; 626if(exists$actions{$action}) { 627$path_info=~ s,^$action/*,,; 628$input_params{'action'} =$action; 629} 630 631# list of actions that want hash_base instead of hash, but can have no 632# pathname (f) parameter 633my@wants_base= ( 634'tree', 635'history', 636); 637 638# we want to catch 639# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 640my($parentrefname,$parentpathname,$refname,$pathname) = 641($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 642 643# first, analyze the 'current' part 644if(defined$pathname) { 645# we got "branch:filename" or "branch:dir/" 646# we could use git_get_type(branch:pathname), but: 647# - it needs $git_dir 648# - it does a git() call 649# - the convention of terminating directories with a slash 650# makes it superfluous 651# - embedding the action in the PATH_INFO would make it even 652# more superfluous 653$pathname=~ s,^/+,,; 654if(!$pathname||substr($pathname, -1)eq"/") { 655$input_params{'action'} ||="tree"; 656$pathname=~ s,/$,,; 657}else{ 658# the default action depends on whether we had parent info 659# or not 660if($parentrefname) { 661$input_params{'action'} ||="blobdiff_plain"; 662}else{ 663$input_params{'action'} ||="blob_plain"; 664} 665} 666$input_params{'hash_base'} ||=$refname; 667$input_params{'file_name'} ||=$pathname; 668}elsif(defined$refname) { 669# we got "branch". In this case we have to choose if we have to 670# set hash or hash_base. 671# 672# Most of the actions without a pathname only want hash to be 673# set, except for the ones specified in @wants_base that want 674# hash_base instead. It should also be noted that hand-crafted 675# links having 'history' as an action and no pathname or hash 676# set will fail, but that happens regardless of PATH_INFO. 677$input_params{'action'} ||="shortlog"; 678if(grep{$_eq$input_params{'action'} }@wants_base) { 679$input_params{'hash_base'} ||=$refname; 680}else{ 681$input_params{'hash'} ||=$refname; 682} 683} 684 685# next, handle the 'parent' part, if present 686if(defined$parentrefname) { 687# a missing pathspec defaults to the 'current' filename, allowing e.g. 688# someproject/blobdiff/oldrev..newrev:/filename 689if($parentpathname) { 690$parentpathname=~ s,^/+,,; 691$parentpathname=~ s,/$,,; 692$input_params{'file_parent'} ||=$parentpathname; 693}else{ 694$input_params{'file_parent'} ||=$input_params{'file_name'}; 695} 696# we assume that hash_parent_base is wanted if a path was specified, 697# or if the action wants hash_base instead of hash 698if(defined$input_params{'file_parent'} || 699grep{$_eq$input_params{'action'} }@wants_base) { 700$input_params{'hash_parent_base'} ||=$parentrefname; 701}else{ 702$input_params{'hash_parent'} ||=$parentrefname; 703} 704} 705 706# for the snapshot action, we allow URLs in the form 707# $project/snapshot/$hash.ext 708# where .ext determines the snapshot and gets removed from the 709# passed $refname to provide the $hash. 710# 711# To be able to tell that $refname includes the format extension, we 712# require the following two conditions to be satisfied: 713# - the hash input parameter MUST have been set from the $refname part 714# of the URL (i.e. they must be equal) 715# - the snapshot format MUST NOT have been defined already (e.g. from 716# CGI parameter sf) 717# It's also useless to try any matching unless $refname has a dot, 718# so we check for that too 719if(defined$input_params{'action'} && 720$input_params{'action'}eq'snapshot'&& 721defined$refname&&index($refname,'.') != -1&& 722$refnameeq$input_params{'hash'} && 723!defined$input_params{'snapshot_format'}) { 724# We loop over the known snapshot formats, checking for 725# extensions. Allowed extensions are both the defined suffix 726# (which includes the initial dot already) and the snapshot 727# format key itself, with a prepended dot 728while(my($fmt,$opt) =each%known_snapshot_formats) { 729my$hash=$refname; 730unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 731next; 732} 733my$sfx=$1; 734# a valid suffix was found, so set the snapshot format 735# and reset the hash parameter 736$input_params{'snapshot_format'} =$fmt; 737$input_params{'hash'} =$hash; 738# we also set the format suffix to the one requested 739# in the URL: this way a request for e.g. .tgz returns 740# a .tgz instead of a .tar.gz 741$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 742last; 743} 744} 745} 746evaluate_path_info(); 747 748our$action=$input_params{'action'}; 749if(defined$action) { 750if(!validate_action($action)) { 751 die_error(400,"Invalid action parameter"); 752} 753} 754 755# parameters which are pathnames 756our$project=$input_params{'project'}; 757if(defined$project) { 758if(!validate_project($project)) { 759undef$project; 760 die_error(404,"No such project"); 761} 762} 763 764our$file_name=$input_params{'file_name'}; 765if(defined$file_name) { 766if(!validate_pathname($file_name)) { 767 die_error(400,"Invalid file parameter"); 768} 769} 770 771our$file_parent=$input_params{'file_parent'}; 772if(defined$file_parent) { 773if(!validate_pathname($file_parent)) { 774 die_error(400,"Invalid file parent parameter"); 775} 776} 777 778# parameters which are refnames 779our$hash=$input_params{'hash'}; 780if(defined$hash) { 781if(!validate_refname($hash)) { 782 die_error(400,"Invalid hash parameter"); 783} 784} 785 786our$hash_parent=$input_params{'hash_parent'}; 787if(defined$hash_parent) { 788if(!validate_refname($hash_parent)) { 789 die_error(400,"Invalid hash parent parameter"); 790} 791} 792 793our$hash_base=$input_params{'hash_base'}; 794if(defined$hash_base) { 795if(!validate_refname($hash_base)) { 796 die_error(400,"Invalid hash base parameter"); 797} 798} 799 800our@extra_options= @{$input_params{'extra_options'}}; 801# @extra_options is always defined, since it can only be (currently) set from 802# CGI, and $cgi->param() returns the empty array in array context if the param 803# is not set 804foreachmy$opt(@extra_options) { 805if(not exists$allowed_options{$opt}) { 806 die_error(400,"Invalid option parameter"); 807} 808if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 809 die_error(400,"Invalid option parameter for this action"); 810} 811} 812 813our$hash_parent_base=$input_params{'hash_parent_base'}; 814if(defined$hash_parent_base) { 815if(!validate_refname($hash_parent_base)) { 816 die_error(400,"Invalid hash parent base parameter"); 817} 818} 819 820# other parameters 821our$page=$input_params{'page'}; 822if(defined$page) { 823if($page=~m/[^0-9]/) { 824 die_error(400,"Invalid page parameter"); 825} 826} 827 828our$searchtype=$input_params{'searchtype'}; 829if(defined$searchtype) { 830if($searchtype=~m/[^a-z]/) { 831 die_error(400,"Invalid searchtype parameter"); 832} 833} 834 835our$search_use_regexp=$input_params{'search_use_regexp'}; 836 837our$searchtext=$input_params{'searchtext'}; 838our$search_regexp; 839if(defined$searchtext) { 840if(length($searchtext) <2) { 841 die_error(403,"At least two characters are required for search parameter"); 842} 843$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 844} 845 846# path to the current git repository 847our$git_dir; 848$git_dir="$projectroot/$project"if$project; 849 850# list of supported snapshot formats 851our@snapshot_fmts= gitweb_get_feature('snapshot'); 852@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 853 854# check that the avatar feature is set to a known provider name, 855# and for each provider check if the dependencies are satisfied. 856# if the provider name is invalid or the dependencies are not met, 857# reset $git_avatar to the empty string. 858our($git_avatar) = gitweb_get_feature('avatar'); 859if($git_avatareq'gravatar') { 860$git_avatar=''unless(eval{require Digest::MD5;1; }); 861}elsif($git_avatareq'picon') { 862# no dependencies 863}else{ 864$git_avatar=''; 865} 866 867# dispatch 868if(!defined$action) { 869if(defined$hash) { 870$action= git_get_type($hash); 871}elsif(defined$hash_base&&defined$file_name) { 872$action= git_get_type("$hash_base:$file_name"); 873}elsif(defined$project) { 874$action='summary'; 875}else{ 876$action='project_list'; 877} 878} 879if(!defined($actions{$action})) { 880 die_error(400,"Unknown action"); 881} 882if($action!~m/^(?:opml|project_list|project_index)$/&& 883!$project) { 884 die_error(400,"Project needed"); 885} 886$actions{$action}->(); 887exit; 888 889## ====================================================================== 890## action links 891 892sub href { 893my%params=@_; 894# default is to use -absolute url() i.e. $my_uri 895my$href=$params{-full} ?$my_url:$my_uri; 896 897$params{'project'} =$projectunlessexists$params{'project'}; 898 899if($params{-replay}) { 900while(my($name,$symbol) =each%cgi_param_mapping) { 901if(!exists$params{$name}) { 902$params{$name} =$input_params{$name}; 903} 904} 905} 906 907my$use_pathinfo= gitweb_check_feature('pathinfo'); 908if($use_pathinfoand defined$params{'project'}) { 909# try to put as many parameters as possible in PATH_INFO: 910# - project name 911# - action 912# - hash_parent or hash_parent_base:/file_parent 913# - hash or hash_base:/filename 914# - the snapshot_format as an appropriate suffix 915 916# When the script is the root DirectoryIndex for the domain, 917# $href here would be something like http://gitweb.example.com/ 918# Thus, we strip any trailing / from $href, to spare us double 919# slashes in the final URL 920$href=~ s,/$,,; 921 922# Then add the project name, if present 923$href.="/".esc_url($params{'project'}); 924delete$params{'project'}; 925 926# since we destructively absorb parameters, we keep this 927# boolean that remembers if we're handling a snapshot 928my$is_snapshot=$params{'action'}eq'snapshot'; 929 930# Summary just uses the project path URL, any other action is 931# added to the URL 932if(defined$params{'action'}) { 933$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 934delete$params{'action'}; 935} 936 937# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 938# stripping nonexistent or useless pieces 939$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 940||$params{'hash_parent'} ||$params{'hash'}); 941if(defined$params{'hash_base'}) { 942if(defined$params{'hash_parent_base'}) { 943$href.= esc_url($params{'hash_parent_base'}); 944# skip the file_parent if it's the same as the file_name 945if(defined$params{'file_parent'}) { 946if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) { 947delete$params{'file_parent'}; 948}elsif($params{'file_parent'} !~/\.\./) { 949$href.=":/".esc_url($params{'file_parent'}); 950delete$params{'file_parent'}; 951} 952} 953$href.=".."; 954delete$params{'hash_parent'}; 955delete$params{'hash_parent_base'}; 956}elsif(defined$params{'hash_parent'}) { 957$href.= esc_url($params{'hash_parent'}).".."; 958delete$params{'hash_parent'}; 959} 960 961$href.= esc_url($params{'hash_base'}); 962if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 963$href.=":/".esc_url($params{'file_name'}); 964delete$params{'file_name'}; 965} 966delete$params{'hash'}; 967delete$params{'hash_base'}; 968}elsif(defined$params{'hash'}) { 969$href.= esc_url($params{'hash'}); 970delete$params{'hash'}; 971} 972 973# If the action was a snapshot, we can absorb the 974# snapshot_format parameter too 975if($is_snapshot) { 976my$fmt=$params{'snapshot_format'}; 977# snapshot_format should always be defined when href() 978# is called, but just in case some code forgets, we 979# fall back to the default 980$fmt||=$snapshot_fmts[0]; 981$href.=$known_snapshot_formats{$fmt}{'suffix'}; 982delete$params{'snapshot_format'}; 983} 984} 985 986# now encode the parameters explicitly 987my@result= (); 988for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 989my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 990if(defined$params{$name}) { 991if(ref($params{$name})eq"ARRAY") { 992foreachmy$par(@{$params{$name}}) { 993push@result,$symbol."=". esc_param($par); 994} 995}else{ 996push@result,$symbol."=". esc_param($params{$name}); 997} 998} 999}1000$href.="?".join(';',@result)ifscalar@result;10011002return$href;1003}100410051006## ======================================================================1007## validation, quoting/unquoting and escaping10081009sub validate_action {1010my$input=shift||returnundef;1011returnundefunlessexists$actions{$input};1012return$input;1013}10141015sub validate_project {1016my$input=shift||returnundef;1017if(!validate_pathname($input) ||1018!(-d "$projectroot/$input") ||1019!check_export_ok("$projectroot/$input") ||1020($strict_export&& !project_in_list($input))) {1021returnundef;1022}else{1023return$input;1024}1025}10261027sub validate_pathname {1028my$input=shift||returnundef;10291030# no '.' or '..' as elements of path, i.e. no '.' nor '..'1031# at the beginning, at the end, and between slashes.1032# also this catches doubled slashes1033if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1034returnundef;1035}1036# no null characters1037if($input=~m!\0!) {1038returnundef;1039}1040return$input;1041}10421043sub validate_refname {1044my$input=shift||returnundef;10451046# textual hashes are O.K.1047if($input=~m/^[0-9a-fA-F]{40}$/) {1048return$input;1049}1050# it must be correct pathname1051$input= validate_pathname($input)1052orreturnundef;1053# restrictions on ref name according to git-check-ref-format1054if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1055returnundef;1056}1057return$input;1058}10591060# decode sequences of octets in utf8 into Perl's internal form,1061# which is utf-8 with utf8 flag set if needed. gitweb writes out1062# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1063sub to_utf8 {1064my$str=shift;1065if(utf8::valid($str)) {1066 utf8::decode($str);1067return$str;1068}else{1069return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1070}1071}10721073# quote unsafe chars, but keep the slash, even when it's not1074# correct, but quoted slashes look too horrible in bookmarks1075sub esc_param {1076my$str=shift;1077$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg;1078$str=~s/\+/%2B/g;1079$str=~s/ /\+/g;1080return$str;1081}10821083# quote unsafe chars in whole URL, so some charactrs cannot be quoted1084sub esc_url {1085my$str=shift;1086$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1087$str=~s/\+/%2B/g;1088$str=~s/ /\+/g;1089return$str;1090}10911092# replace invalid utf8 character with SUBSTITUTION sequence1093sub esc_html {1094my$str=shift;1095my%opts=@_;10961097$str= to_utf8($str);1098$str=$cgi->escapeHTML($str);1099if($opts{'-nbsp'}) {1100$str=~s/ / /g;1101}1102$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1103return$str;1104}11051106# quote control characters and escape filename to HTML1107sub esc_path {1108my$str=shift;1109my%opts=@_;11101111$str= to_utf8($str);1112$str=$cgi->escapeHTML($str);1113if($opts{'-nbsp'}) {1114$str=~s/ / /g;1115}1116$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1117return$str;1118}11191120# Make control characters "printable", using character escape codes (CEC)1121sub quot_cec {1122my$cntrl=shift;1123my%opts=@_;1124my%es= (# character escape codes, aka escape sequences1125"\t"=>'\t',# tab (HT)1126"\n"=>'\n',# line feed (LF)1127"\r"=>'\r',# carrige return (CR)1128"\f"=>'\f',# form feed (FF)1129"\b"=>'\b',# backspace (BS)1130"\a"=>'\a',# alarm (bell) (BEL)1131"\e"=>'\e',# escape (ESC)1132"\013"=>'\v',# vertical tab (VT)1133"\000"=>'\0',# nul character (NUL)1134);1135my$chr= ( (exists$es{$cntrl})1136?$es{$cntrl}1137:sprintf('\%2x',ord($cntrl)) );1138if($opts{-nohtml}) {1139return$chr;1140}else{1141return"<span class=\"cntrl\">$chr</span>";1142}1143}11441145# Alternatively use unicode control pictures codepoints,1146# Unicode "printable representation" (PR)1147sub quot_upr {1148my$cntrl=shift;1149my%opts=@_;11501151my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1152if($opts{-nohtml}) {1153return$chr;1154}else{1155return"<span class=\"cntrl\">$chr</span>";1156}1157}11581159# git may return quoted and escaped filenames1160sub unquote {1161my$str=shift;11621163sub unq {1164my$seq=shift;1165my%es= (# character escape codes, aka escape sequences1166't'=>"\t",# tab (HT, TAB)1167'n'=>"\n",# newline (NL)1168'r'=>"\r",# return (CR)1169'f'=>"\f",# form feed (FF)1170'b'=>"\b",# backspace (BS)1171'a'=>"\a",# alarm (bell) (BEL)1172'e'=>"\e",# escape (ESC)1173'v'=>"\013",# vertical tab (VT)1174);11751176if($seq=~m/^[0-7]{1,3}$/) {1177# octal char sequence1178returnchr(oct($seq));1179}elsif(exists$es{$seq}) {1180# C escape sequence, aka character escape code1181return$es{$seq};1182}1183# quoted ordinary character1184return$seq;1185}11861187if($str=~m/^"(.*)"$/) {1188# needs unquoting1189$str=$1;1190$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1191}1192return$str;1193}11941195# escape tabs (convert tabs to spaces)1196sub untabify {1197my$line=shift;11981199while((my$pos=index($line,"\t")) != -1) {1200if(my$count= (8- ($pos%8))) {1201my$spaces=' ' x $count;1202$line=~s/\t/$spaces/;1203}1204}12051206return$line;1207}12081209sub project_in_list {1210my$project=shift;1211my@list= git_get_projects_list();1212return@list&&scalar(grep{$_->{'path'}eq$project}@list);1213}12141215## ----------------------------------------------------------------------1216## HTML aware string manipulation12171218# Try to chop given string on a word boundary between position1219# $len and $len+$add_len. If there is no word boundary there,1220# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1221# (marking chopped part) would be longer than given string.1222sub chop_str {1223my$str=shift;1224my$len=shift;1225my$add_len=shift||10;1226my$where=shift||'right';# 'left' | 'center' | 'right'12271228# Make sure perl knows it is utf8 encoded so we don't1229# cut in the middle of a utf8 multibyte char.1230$str= to_utf8($str);12311232# allow only $len chars, but don't cut a word if it would fit in $add_len1233# if it doesn't fit, cut it if it's still longer than the dots we would add1234# remove chopped character entities entirely12351236# when chopping in the middle, distribute $len into left and right part1237# return early if chopping wouldn't make string shorter1238if($whereeq'center') {1239return$strif($len+5>=length($str));# filler is length 51240$len=int($len/2);1241}else{1242return$strif($len+4>=length($str));# filler is length 41243}12441245# regexps: ending and beginning with word part up to $add_len1246my$endre=qr/.{$len}\w{0,$add_len}/;1247my$begre=qr/\w{0,$add_len}.{$len}/;12481249if($whereeq'left') {1250$str=~m/^(.*?)($begre)$/;1251my($lead,$body) = ($1,$2);1252if(length($lead) >4) {1253$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1254$lead=" ...";1255}1256return"$lead$body";12571258}elsif($whereeq'center') {1259$str=~m/^($endre)(.*)$/;1260my($left,$str) = ($1,$2);1261$str=~m/^(.*?)($begre)$/;1262my($mid,$right) = ($1,$2);1263if(length($mid) >5) {1264$left=~s/&[^;]*$//;1265$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1266$mid=" ... ";1267}1268return"$left$mid$right";12691270}else{1271$str=~m/^($endre)(.*)$/;1272my$body=$1;1273my$tail=$2;1274if(length($tail) >4) {1275$body=~s/&[^;]*$//;1276$tail="... ";1277}1278return"$body$tail";1279}1280}12811282# takes the same arguments as chop_str, but also wraps a <span> around the1283# result with a title attribute if it does get chopped. Additionally, the1284# string is HTML-escaped.1285sub chop_and_escape_str {1286my($str) =@_;12871288my$chopped= chop_str(@_);1289if($choppedeq$str) {1290return esc_html($chopped);1291}else{1292$str=~s/[[:cntrl:]]/?/g;1293return$cgi->span({-title=>$str}, esc_html($chopped));1294}1295}12961297## ----------------------------------------------------------------------1298## functions returning short strings12991300# CSS class for given age value (in seconds)1301sub age_class {1302my$age=shift;13031304if(!defined$age) {1305return"noage";1306}elsif($age<60*60*2) {1307return"age0";1308}elsif($age<60*60*24*2) {1309return"age1";1310}else{1311return"age2";1312}1313}13141315# convert age in seconds to "nn units ago" string1316sub age_string {1317my$age=shift;1318my$age_str;13191320if($age>60*60*24*365*2) {1321$age_str= (int$age/60/60/24/365);1322$age_str.=" years ago";1323}elsif($age>60*60*24*(365/12)*2) {1324$age_str=int$age/60/60/24/(365/12);1325$age_str.=" months ago";1326}elsif($age>60*60*24*7*2) {1327$age_str=int$age/60/60/24/7;1328$age_str.=" weeks ago";1329}elsif($age>60*60*24*2) {1330$age_str=int$age/60/60/24;1331$age_str.=" days ago";1332}elsif($age>60*60*2) {1333$age_str=int$age/60/60;1334$age_str.=" hours ago";1335}elsif($age>60*2) {1336$age_str=int$age/60;1337$age_str.=" min ago";1338}elsif($age>2) {1339$age_str=int$age;1340$age_str.=" sec ago";1341}else{1342$age_str.=" right now";1343}1344return$age_str;1345}13461347useconstant{1348 S_IFINVALID =>0030000,1349 S_IFGITLINK =>0160000,1350};13511352# submodule/subproject, a commit object reference1353sub S_ISGITLINK {1354my$mode=shift;13551356return(($mode& S_IFMT) == S_IFGITLINK)1357}13581359# convert file mode in octal to symbolic file mode string1360sub mode_str {1361my$mode=oct shift;13621363if(S_ISGITLINK($mode)) {1364return'm---------';1365}elsif(S_ISDIR($mode& S_IFMT)) {1366return'drwxr-xr-x';1367}elsif(S_ISLNK($mode)) {1368return'lrwxrwxrwx';1369}elsif(S_ISREG($mode)) {1370# git cares only about the executable bit1371if($mode& S_IXUSR) {1372return'-rwxr-xr-x';1373}else{1374return'-rw-r--r--';1375};1376}else{1377return'----------';1378}1379}13801381# convert file mode in octal to file type string1382sub file_type {1383my$mode=shift;13841385if($mode!~m/^[0-7]+$/) {1386return$mode;1387}else{1388$mode=oct$mode;1389}13901391if(S_ISGITLINK($mode)) {1392return"submodule";1393}elsif(S_ISDIR($mode& S_IFMT)) {1394return"directory";1395}elsif(S_ISLNK($mode)) {1396return"symlink";1397}elsif(S_ISREG($mode)) {1398return"file";1399}else{1400return"unknown";1401}1402}14031404# convert file mode in octal to file type description string1405sub file_type_long {1406my$mode=shift;14071408if($mode!~m/^[0-7]+$/) {1409return$mode;1410}else{1411$mode=oct$mode;1412}14131414if(S_ISGITLINK($mode)) {1415return"submodule";1416}elsif(S_ISDIR($mode& S_IFMT)) {1417return"directory";1418}elsif(S_ISLNK($mode)) {1419return"symlink";1420}elsif(S_ISREG($mode)) {1421if($mode& S_IXUSR) {1422return"executable";1423}else{1424return"file";1425};1426}else{1427return"unknown";1428}1429}143014311432## ----------------------------------------------------------------------1433## functions returning short HTML fragments, or transforming HTML fragments1434## which don't belong to other sections14351436# format line of commit message.1437sub format_log_line_html {1438my$line=shift;14391440$line= esc_html($line, -nbsp=>1);1441$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1442$cgi->a({-href => href(action=>"object", hash=>$1),1443-class=>"text"},$1);1444}eg;14451446return$line;1447}14481449# format marker of refs pointing to given object14501451# the destination action is chosen based on object type and current context:1452# - for annotated tags, we choose the tag view unless it's the current view1453# already, in which case we go to shortlog view1454# - for other refs, we keep the current view if we're in history, shortlog or1455# log view, and select shortlog otherwise1456sub format_ref_marker {1457my($refs,$id) =@_;1458my$markers='';14591460if(defined$refs->{$id}) {1461foreachmy$ref(@{$refs->{$id}}) {1462# this code exploits the fact that non-lightweight tags are the1463# only indirect objects, and that they are the only objects for which1464# we want to use tag instead of shortlog as action1465my($type,$name) =qw();1466my$indirect= ($ref=~s/\^\{\}$//);1467# e.g. tags/v2.6.11 or heads/next1468if($ref=~m!^(.*?)s?/(.*)$!) {1469$type=$1;1470$name=$2;1471}else{1472$type="ref";1473$name=$ref;1474}14751476my$class=$type;1477$class.=" indirect"if$indirect;14781479my$dest_action="shortlog";14801481if($indirect) {1482$dest_action="tag"unless$actioneq"tag";1483}elsif($action=~/^(history|(short)?log)$/) {1484$dest_action=$action;1485}14861487my$dest="";1488$dest.="refs/"unless$ref=~ m!^refs/!;1489$dest.=$ref;14901491my$link=$cgi->a({1492-href => href(1493 action=>$dest_action,1494 hash=>$dest1495)},$name);14961497$markers.=" <span class=\"$class\"title=\"$ref\">".1498$link."</span>";1499}1500}15011502if($markers) {1503return' <span class="refs">'.$markers.'</span>';1504}else{1505return"";1506}1507}15081509# format, perhaps shortened and with markers, title line1510sub format_subject_html {1511my($long,$short,$href,$extra) =@_;1512$extra=''unlessdefined($extra);15131514if(length($short) <length($long)) {1515$long=~s/[[:cntrl:]]/?/g;1516return$cgi->a({-href =>$href, -class=>"list subject",1517-title => to_utf8($long)},1518 esc_html($short) .$extra);1519}else{1520return$cgi->a({-href =>$href, -class=>"list subject"},1521 esc_html($long) .$extra);1522}1523}15241525# Rather than recomputing the url for an email multiple times, we cache it1526# after the first hit. This gives a visible benefit in views where the avatar1527# for the same email is used repeatedly (e.g. shortlog).1528# The cache is shared by all avatar engines (currently gravatar only), which1529# are free to use it as preferred. Since only one avatar engine is used for any1530# given page, there's no risk for cache conflicts.1531our%avatar_cache= ();15321533# Compute the picon url for a given email, by using the picon search service over at1534# http://www.cs.indiana.edu/picons/search.html1535sub picon_url {1536my$email=lc shift;1537if(!$avatar_cache{$email}) {1538my($user,$domain) =split('@',$email);1539$avatar_cache{$email} =1540"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1541"$domain/$user/".1542"users+domains+unknown/up/single";1543}1544return$avatar_cache{$email};1545}15461547# Compute the gravatar url for a given email, if it's not in the cache already.1548# Gravatar stores only the part of the URL before the size, since that's the1549# one computationally more expensive. This also allows reuse of the cache for1550# different sizes (for this particular engine).1551sub gravatar_url {1552my$email=lc shift;1553my$size=shift;1554$avatar_cache{$email} ||=1555"http://www.gravatar.com/avatar/".1556 Digest::MD5::md5_hex($email) ."?s=";1557return$avatar_cache{$email} .$size;1558}15591560# Insert an avatar for the given $email at the given $size if the feature1561# is enabled.1562sub git_get_avatar {1563my($email,%opts) =@_;1564my$pre_white= ($opts{-pad_before} ?" ":"");1565my$post_white= ($opts{-pad_after} ?" ":"");1566$opts{-size} ||='default';1567my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1568my$url="";1569if($git_avatareq'gravatar') {1570$url= gravatar_url($email,$size);1571}elsif($git_avatareq'picon') {1572$url= picon_url($email);1573}1574# Other providers can be added by extending the if chain, defining $url1575# as needed. If no variant puts something in $url, we assume avatars1576# are completely disabled/unavailable.1577if($url) {1578return$pre_white.1579"<img width=\"$size\"".1580"class=\"avatar\"".1581"src=\"$url\"".1582"alt=\"\"".1583"/>".$post_white;1584}else{1585return"";1586}1587}15881589# format the author name of the given commit with the given tag1590# the author name is chopped and escaped according to the other1591# optional parameters (see chop_str).1592sub format_author_html {1593my$tag=shift;1594my$co=shift;1595my$author= chop_and_escape_str($co->{'author_name'},@_);1596return"<$tagclass=\"author\">".1597 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1598$author."</$tag>";1599}16001601# format git diff header line, i.e. "diff --(git|combined|cc) ..."1602sub format_git_diff_header_line {1603my$line=shift;1604my$diffinfo=shift;1605my($from,$to) =@_;16061607if($diffinfo->{'nparents'}) {1608# combined diff1609$line=~s!^(diff (.*?) )"?.*$!$1!;1610if($to->{'href'}) {1611$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1612 esc_path($to->{'file'}));1613}else{# file was deleted (no href)1614$line.= esc_path($to->{'file'});1615}1616}else{1617# "ordinary" diff1618$line=~s!^(diff (.*?) )"?a/.*$!$1!;1619if($from->{'href'}) {1620$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1621'a/'. esc_path($from->{'file'}));1622}else{# file was added (no href)1623$line.='a/'. esc_path($from->{'file'});1624}1625$line.=' ';1626if($to->{'href'}) {1627$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1628'b/'. esc_path($to->{'file'}));1629}else{# file was deleted1630$line.='b/'. esc_path($to->{'file'});1631}1632}16331634return"<div class=\"diff header\">$line</div>\n";1635}16361637# format extended diff header line, before patch itself1638sub format_extended_diff_header_line {1639my$line=shift;1640my$diffinfo=shift;1641my($from,$to) =@_;16421643# match <path>1644if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1645$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1646 esc_path($from->{'file'}));1647}1648if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1649$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1650 esc_path($to->{'file'}));1651}1652# match single <mode>1653if($line=~m/\s(\d{6})$/) {1654$line.='<span class="info"> ('.1655 file_type_long($1) .1656')</span>';1657}1658# match <hash>1659if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1660# can match only for combined diff1661$line='index ';1662for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1663if($from->{'href'}[$i]) {1664$line.=$cgi->a({-href=>$from->{'href'}[$i],1665-class=>"hash"},1666substr($diffinfo->{'from_id'}[$i],0,7));1667}else{1668$line.='0' x 7;1669}1670# separator1671$line.=','if($i<$diffinfo->{'nparents'} -1);1672}1673$line.='..';1674if($to->{'href'}) {1675$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1676substr($diffinfo->{'to_id'},0,7));1677}else{1678$line.='0' x 7;1679}16801681}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1682# can match only for ordinary diff1683my($from_link,$to_link);1684if($from->{'href'}) {1685$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1686substr($diffinfo->{'from_id'},0,7));1687}else{1688$from_link='0' x 7;1689}1690if($to->{'href'}) {1691$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1692substr($diffinfo->{'to_id'},0,7));1693}else{1694$to_link='0' x 7;1695}1696my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1697$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1698}16991700return$line."<br/>\n";1701}17021703# format from-file/to-file diff header1704sub format_diff_from_to_header {1705my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1706my$line;1707my$result='';17081709$line=$from_line;1710#assert($line =~ m/^---/) if DEBUG;1711# no extra formatting for "^--- /dev/null"1712if(!$diffinfo->{'nparents'}) {1713# ordinary (single parent) diff1714if($line=~m!^--- "?a/!) {1715if($from->{'href'}) {1716$line='--- a/'.1717$cgi->a({-href=>$from->{'href'}, -class=>"path"},1718 esc_path($from->{'file'}));1719}else{1720$line='--- a/'.1721 esc_path($from->{'file'});1722}1723}1724$result.= qq!<div class="diff from_file">$line</div>\n!;17251726}else{1727# combined diff (merge commit)1728for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1729if($from->{'href'}[$i]) {1730$line='--- '.1731$cgi->a({-href=>href(action=>"blobdiff",1732 hash_parent=>$diffinfo->{'from_id'}[$i],1733 hash_parent_base=>$parents[$i],1734 file_parent=>$from->{'file'}[$i],1735 hash=>$diffinfo->{'to_id'},1736 hash_base=>$hash,1737 file_name=>$to->{'file'}),1738-class=>"path",1739-title=>"diff". ($i+1)},1740$i+1) .1741'/'.1742$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1743 esc_path($from->{'file'}[$i]));1744}else{1745$line='--- /dev/null';1746}1747$result.= qq!<div class="diff from_file">$line</div>\n!;1748}1749}17501751$line=$to_line;1752#assert($line =~ m/^\+\+\+/) if DEBUG;1753# no extra formatting for "^+++ /dev/null"1754if($line=~m!^\+\+\+ "?b/!) {1755if($to->{'href'}) {1756$line='+++ b/'.1757$cgi->a({-href=>$to->{'href'}, -class=>"path"},1758 esc_path($to->{'file'}));1759}else{1760$line='+++ b/'.1761 esc_path($to->{'file'});1762}1763}1764$result.= qq!<div class="diff to_file">$line</div>\n!;17651766return$result;1767}17681769# create note for patch simplified by combined diff1770sub format_diff_cc_simplified {1771my($diffinfo,@parents) =@_;1772my$result='';17731774$result.="<div class=\"diff header\">".1775"diff --cc ";1776if(!is_deleted($diffinfo)) {1777$result.=$cgi->a({-href => href(action=>"blob",1778 hash_base=>$hash,1779 hash=>$diffinfo->{'to_id'},1780 file_name=>$diffinfo->{'to_file'}),1781-class=>"path"},1782 esc_path($diffinfo->{'to_file'}));1783}else{1784$result.= esc_path($diffinfo->{'to_file'});1785}1786$result.="</div>\n".# class="diff header"1787"<div class=\"diff nodifferences\">".1788"Simple merge".1789"</div>\n";# class="diff nodifferences"17901791return$result;1792}17931794# format patch (diff) line (not to be used for diff headers)1795sub format_diff_line {1796my$line=shift;1797my($from,$to) =@_;1798my$diff_class="";17991800chomp$line;18011802if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1803# combined diff1804my$prefix=substr($line,0,scalar@{$from->{'href'}});1805if($line=~m/^\@{3}/) {1806$diff_class=" chunk_header";1807}elsif($line=~m/^\\/) {1808$diff_class=" incomplete";1809}elsif($prefix=~tr/+/+/) {1810$diff_class=" add";1811}elsif($prefix=~tr/-/-/) {1812$diff_class=" rem";1813}1814}else{1815# assume ordinary diff1816my$char=substr($line,0,1);1817if($chareq'+') {1818$diff_class=" add";1819}elsif($chareq'-') {1820$diff_class=" rem";1821}elsif($chareq'@') {1822$diff_class=" chunk_header";1823}elsif($chareq"\\") {1824$diff_class=" incomplete";1825}1826}1827$line= untabify($line);1828if($from&&$to&&$line=~m/^\@{2} /) {1829my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1830$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;18311832$from_lines=0unlessdefined$from_lines;1833$to_lines=0unlessdefined$to_lines;18341835if($from->{'href'}) {1836$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1837-class=>"list"},$from_text);1838}1839if($to->{'href'}) {1840$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1841-class=>"list"},$to_text);1842}1843$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1844"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1845return"<div class=\"diff$diff_class\">$line</div>\n";1846}elsif($from&&$to&&$line=~m/^\@{3}/) {1847my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1848my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);18491850@from_text=split(' ',$ranges);1851for(my$i=0;$i<@from_text; ++$i) {1852($from_start[$i],$from_nlines[$i]) =1853(split(',',substr($from_text[$i],1)),0);1854}18551856$to_text=pop@from_text;1857$to_start=pop@from_start;1858$to_nlines=pop@from_nlines;18591860$line="<span class=\"chunk_info\">$prefix";1861for(my$i=0;$i<@from_text; ++$i) {1862if($from->{'href'}[$i]) {1863$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1864-class=>"list"},$from_text[$i]);1865}else{1866$line.=$from_text[$i];1867}1868$line.=" ";1869}1870if($to->{'href'}) {1871$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1872-class=>"list"},$to_text);1873}else{1874$line.=$to_text;1875}1876$line.="$prefix</span>".1877"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1878return"<div class=\"diff$diff_class\">$line</div>\n";1879}1880return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1881}18821883# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1884# linked. Pass the hash of the tree/commit to snapshot.1885sub format_snapshot_links {1886my($hash) =@_;1887my$num_fmts=@snapshot_fmts;1888if($num_fmts>1) {1889# A parenthesized list of links bearing format names.1890# e.g. "snapshot (_tar.gz_ _zip_)"1891return"snapshot (".join(' ',map1892$cgi->a({1893-href => href(1894 action=>"snapshot",1895 hash=>$hash,1896 snapshot_format=>$_1897)1898},$known_snapshot_formats{$_}{'display'})1899,@snapshot_fmts) .")";1900}elsif($num_fmts==1) {1901# A single "snapshot" link whose tooltip bears the format name.1902# i.e. "_snapshot_"1903my($fmt) =@snapshot_fmts;1904return1905$cgi->a({1906-href => href(1907 action=>"snapshot",1908 hash=>$hash,1909 snapshot_format=>$fmt1910),1911-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1912},"snapshot");1913}else{# $num_fmts == 01914returnundef;1915}1916}19171918## ......................................................................1919## functions returning values to be passed, perhaps after some1920## transformation, to other functions; e.g. returning arguments to href()19211922# returns hash to be passed to href to generate gitweb URL1923# in -title key it returns description of link1924sub get_feed_info {1925my$format=shift||'Atom';1926my%res= (action =>lc($format));19271928# feed links are possible only for project views1929return unless(defined$project);1930# some views should link to OPML, or to generic project feed,1931# or don't have specific feed yet (so they should use generic)1932return if($action=~/^(?:tags|heads|forks|tag|search)$/x);19331934my$branch;1935# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1936# from tag links; this also makes possible to detect branch links1937if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1938(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1939$branch=$1;1940}1941# find log type for feed description (title)1942my$type='log';1943if(defined$file_name) {1944$type="history of$file_name";1945$type.="/"if($actioneq'tree');1946$type.=" on '$branch'"if(defined$branch);1947}else{1948$type="log of$branch"if(defined$branch);1949}19501951$res{-title} =$type;1952$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1953$res{'file_name'} =$file_name;19541955return%res;1956}19571958## ----------------------------------------------------------------------1959## git utility subroutines, invoking git commands19601961# returns path to the core git executable and the --git-dir parameter as list1962sub git_cmd {1963return$GIT,'--git-dir='.$git_dir;1964}19651966# quote the given arguments for passing them to the shell1967# quote_command("command", "arg 1", "arg with ' and ! characters")1968# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1969# Try to avoid using this function wherever possible.1970sub quote_command {1971returnjoin(' ',1972map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);1973}19741975# get HEAD ref of given project as hash1976sub git_get_head_hash {1977my$project=shift;1978my$o_git_dir=$git_dir;1979my$retval=undef;1980$git_dir="$projectroot/$project";1981if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1982my$head= <$fd>;1983close$fd;1984if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1985$retval=$1;1986}1987}1988if(defined$o_git_dir) {1989$git_dir=$o_git_dir;1990}1991return$retval;1992}19931994# get type of given object1995sub git_get_type {1996my$hash=shift;19971998open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1999my$type= <$fd>;2000close$fdorreturn;2001chomp$type;2002return$type;2003}20042005# repository configuration2006our$config_file='';2007our%config;20082009# store multiple values for single key as anonymous array reference2010# single values stored directly in the hash, not as [ <value> ]2011sub hash_set_multi {2012my($hash,$key,$value) =@_;20132014if(!exists$hash->{$key}) {2015$hash->{$key} =$value;2016}elsif(!ref$hash->{$key}) {2017$hash->{$key} = [$hash->{$key},$value];2018}else{2019push@{$hash->{$key}},$value;2020}2021}20222023# return hash of git project configuration2024# optionally limited to some section, e.g. 'gitweb'2025sub git_parse_project_config {2026my$section_regexp=shift;2027my%config;20282029local$/="\0";20302031open my$fh,"-|", git_cmd(),"config",'-z','-l',2032orreturn;20332034while(my$keyval= <$fh>) {2035chomp$keyval;2036my($key,$value) =split(/\n/,$keyval,2);20372038 hash_set_multi(\%config,$key,$value)2039if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2040}2041close$fh;20422043return%config;2044}20452046# convert config value to boolean: 'true' or 'false'2047# no value, number > 0, 'true' and 'yes' values are true2048# rest of values are treated as false (never as error)2049sub config_to_bool {2050my$val=shift;20512052return1if!defined$val;# section.key20532054# strip leading and trailing whitespace2055$val=~s/^\s+//;2056$val=~s/\s+$//;20572058return(($val=~/^\d+$/&&$val) ||# section.key = 12059($val=~/^(?:true|yes)$/i));# section.key = true2060}20612062# convert config value to simple decimal number2063# an optional value suffix of 'k', 'm', or 'g' will cause the value2064# to be multiplied by 1024, 1048576, or 10737418242065sub config_to_int {2066my$val=shift;20672068# strip leading and trailing whitespace2069$val=~s/^\s+//;2070$val=~s/\s+$//;20712072if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2073$unit=lc($unit);2074# unknown unit is treated as 12075return$num* ($uniteq'g'?1073741824:2076$uniteq'm'?1048576:2077$uniteq'k'?1024:1);2078}2079return$val;2080}20812082# convert config value to array reference, if needed2083sub config_to_multi {2084my$val=shift;20852086returnref($val) ?$val: (defined($val) ? [$val] : []);2087}20882089sub git_get_project_config {2090my($key,$type) =@_;20912092# key sanity check2093return unless($key);2094$key=~s/^gitweb\.//;2095return if($key=~m/\W/);20962097# type sanity check2098if(defined$type) {2099$type=~s/^--//;2100$type=undef2101unless($typeeq'bool'||$typeeq'int');2102}21032104# get config2105if(!defined$config_file||2106$config_filene"$git_dir/config") {2107%config= git_parse_project_config('gitweb');2108$config_file="$git_dir/config";2109}21102111# check if config variable (key) exists2112return unlessexists$config{"gitweb.$key"};21132114# ensure given type2115if(!defined$type) {2116return$config{"gitweb.$key"};2117}elsif($typeeq'bool') {2118# backward compatibility: 'git config --bool' returns true/false2119return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2120}elsif($typeeq'int') {2121return config_to_int($config{"gitweb.$key"});2122}2123return$config{"gitweb.$key"};2124}21252126# get hash of given path at given ref2127sub git_get_hash_by_path {2128my$base=shift;2129my$path=shift||returnundef;2130my$type=shift;21312132$path=~ s,/+$,,;21332134open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2135or die_error(500,"Open git-ls-tree failed");2136my$line= <$fd>;2137close$fdorreturnundef;21382139if(!defined$line) {2140# there is no tree or hash given by $path at $base2141returnundef;2142}21432144#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2145$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2146if(defined$type&&$typene$2) {2147# type doesn't match2148returnundef;2149}2150return$3;2151}21522153# get path of entry with given hash at given tree-ish (ref)2154# used to get 'from' filename for combined diff (merge commit) for renames2155sub git_get_path_by_hash {2156my$base=shift||return;2157my$hash=shift||return;21582159local$/="\0";21602161open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2162orreturnundef;2163while(my$line= <$fd>) {2164chomp$line;21652166#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2167#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2168if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2169close$fd;2170return$1;2171}2172}2173close$fd;2174returnundef;2175}21762177## ......................................................................2178## git utility functions, directly accessing git repository21792180sub git_get_project_description {2181my$path=shift;21822183$git_dir="$projectroot/$path";2184open my$fd,'<',"$git_dir/description"2185orreturn git_get_project_config('description');2186my$descr= <$fd>;2187close$fd;2188if(defined$descr) {2189chomp$descr;2190}2191return$descr;2192}21932194sub git_get_project_ctags {2195my$path=shift;2196my$ctags= {};21972198$git_dir="$projectroot/$path";2199opendir my$dh,"$git_dir/ctags"2200orreturn$ctags;2201foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2202open my$ct,'<',$_ornext;2203my$val= <$ct>;2204chomp$val;2205close$ct;2206my$ctag=$_;$ctag=~ s#.*/##;2207$ctags->{$ctag} =$val;2208}2209closedir$dh;2210$ctags;2211}22122213sub git_populate_project_tagcloud {2214my$ctags=shift;22152216# First, merge different-cased tags; tags vote on casing2217my%ctags_lc;2218foreach(keys%$ctags) {2219$ctags_lc{lc$_}->{count} +=$ctags->{$_};2220if(not$ctags_lc{lc$_}->{topcount}2221or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2222$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2223$ctags_lc{lc$_}->{topname} =$_;2224}2225}22262227my$cloud;2228if(eval{require HTML::TagCloud;1; }) {2229$cloud= HTML::TagCloud->new;2230foreach(sort keys%ctags_lc) {2231# Pad the title with spaces so that the cloud looks2232# less crammed.2233my$title=$ctags_lc{$_}->{topname};2234$title=~s/ / /g;2235$title=~s/^/ /g;2236$title=~s/$/ /g;2237$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2238}2239}else{2240$cloud= \%ctags_lc;2241}2242$cloud;2243}22442245sub git_show_project_tagcloud {2246my($cloud,$count) =@_;2247print STDERR ref($cloud)."..\n";2248if(ref$cloudeq'HTML::TagCloud') {2249return$cloud->html_and_css($count);2250}else{2251my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2252return'<p align="center">'.join(', ',map{2253"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2254}splice(@tags,0,$count)) .'</p>';2255}2256}22572258sub git_get_project_url_list {2259my$path=shift;22602261$git_dir="$projectroot/$path";2262open my$fd,'<',"$git_dir/cloneurl"2263orreturnwantarray?2264@{ config_to_multi(git_get_project_config('url')) } :2265 config_to_multi(git_get_project_config('url'));2266my@git_project_url_list=map{chomp;$_} <$fd>;2267close$fd;22682269returnwantarray?@git_project_url_list: \@git_project_url_list;2270}22712272sub git_get_projects_list {2273my($filter) =@_;2274my@list;22752276$filter||='';2277$filter=~s/\.git$//;22782279my$check_forks= gitweb_check_feature('forks');22802281if(-d $projects_list) {2282# search in directory2283my$dir=$projects_list. ($filter?"/$filter":'');2284# remove the trailing "/"2285$dir=~s!/+$!!;2286my$pfxlen=length("$dir");2287my$pfxdepth= ($dir=~tr!/!!);22882289 File::Find::find({2290 follow_fast =>1,# follow symbolic links2291 follow_skip =>2,# ignore duplicates2292 dangling_symlinks =>0,# ignore dangling symlinks, silently2293 wanted =>sub{2294# skip project-list toplevel, if we get it.2295return if(m!^[/.]$!);2296# only directories can be git repositories2297return unless(-d $_);2298# don't traverse too deep (Find is super slow on os x)2299if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2300$File::Find::prune =1;2301return;2302}23032304my$subdir=substr($File::Find::name,$pfxlen+1);2305# we check related file in $projectroot2306my$path= ($filter?"$filter/":'') .$subdir;2307if(check_export_ok("$projectroot/$path")) {2308push@list, { path =>$path};2309$File::Find::prune =1;2310}2311},2312},"$dir");23132314}elsif(-f $projects_list) {2315# read from file(url-encoded):2316# 'git%2Fgit.git Linus+Torvalds'2317# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2318# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2319my%paths;2320open my$fd,'<',$projects_listorreturn;2321 PROJECT:2322while(my$line= <$fd>) {2323chomp$line;2324my($path,$owner) =split' ',$line;2325$path= unescape($path);2326$owner= unescape($owner);2327if(!defined$path) {2328next;2329}2330if($filterne'') {2331# looking for forks;2332my$pfx=substr($path,0,length($filter));2333if($pfxne$filter) {2334next PROJECT;2335}2336my$sfx=substr($path,length($filter));2337if($sfx!~/^\/.*\.git$/) {2338next PROJECT;2339}2340}elsif($check_forks) {2341 PATH:2342foreachmy$filter(keys%paths) {2343# looking for forks;2344my$pfx=substr($path,0,length($filter));2345if($pfxne$filter) {2346next PATH;2347}2348my$sfx=substr($path,length($filter));2349if($sfx!~/^\/.*\.git$/) {2350next PATH;2351}2352# is a fork, don't include it in2353# the list2354next PROJECT;2355}2356}2357if(check_export_ok("$projectroot/$path")) {2358my$pr= {2359 path =>$path,2360 owner => to_utf8($owner),2361};2362push@list,$pr;2363(my$forks_path=$path) =~s/\.git$//;2364$paths{$forks_path}++;2365}2366}2367close$fd;2368}2369return@list;2370}23712372our$gitweb_project_owner=undef;2373sub git_get_project_list_from_file {23742375return if(defined$gitweb_project_owner);23762377$gitweb_project_owner= {};2378# read from file (url-encoded):2379# 'git%2Fgit.git Linus+Torvalds'2380# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2381# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2382if(-f $projects_list) {2383open(my$fd,'<',$projects_list);2384while(my$line= <$fd>) {2385chomp$line;2386my($pr,$ow) =split' ',$line;2387$pr= unescape($pr);2388$ow= unescape($ow);2389$gitweb_project_owner->{$pr} = to_utf8($ow);2390}2391close$fd;2392}2393}23942395sub git_get_project_owner {2396my$project=shift;2397my$owner;23982399returnundefunless$project;2400$git_dir="$projectroot/$project";24012402if(!defined$gitweb_project_owner) {2403 git_get_project_list_from_file();2404}24052406if(exists$gitweb_project_owner->{$project}) {2407$owner=$gitweb_project_owner->{$project};2408}2409if(!defined$owner){2410$owner= git_get_project_config('owner');2411}2412if(!defined$owner) {2413$owner= get_file_owner("$git_dir");2414}24152416return$owner;2417}24182419sub git_get_last_activity {2420my($path) =@_;2421my$fd;24222423$git_dir="$projectroot/$path";2424open($fd,"-|", git_cmd(),'for-each-ref',2425'--format=%(committer)',2426'--sort=-committerdate',2427'--count=1',2428'refs/heads')orreturn;2429my$most_recent= <$fd>;2430close$fdorreturn;2431if(defined$most_recent&&2432$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2433my$timestamp=$1;2434my$age=time-$timestamp;2435return($age, age_string($age));2436}2437return(undef,undef);2438}24392440sub git_get_references {2441my$type=shift||"";2442my%refs;2443# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112444# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2445open my$fd,"-|", git_cmd(),"show-ref","--dereference",2446($type? ("--","refs/$type") : ())# use -- <pattern> if $type2447orreturn;24482449while(my$line= <$fd>) {2450chomp$line;2451if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2452if(defined$refs{$1}) {2453push@{$refs{$1}},$2;2454}else{2455$refs{$1} = [$2];2456}2457}2458}2459close$fdorreturn;2460return \%refs;2461}24622463sub git_get_rev_name_tags {2464my$hash=shift||returnundef;24652466open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2467orreturn;2468my$name_rev= <$fd>;2469close$fd;24702471if($name_rev=~ m|^$hash tags/(.*)$|) {2472return$1;2473}else{2474# catches also '$hash undefined' output2475returnundef;2476}2477}24782479## ----------------------------------------------------------------------2480## parse to hash functions24812482sub parse_date {2483my$epoch=shift;2484my$tz=shift||"-0000";24852486my%date;2487my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2488my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2489my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2490$date{'hour'} =$hour;2491$date{'minute'} =$min;2492$date{'mday'} =$mday;2493$date{'day'} =$days[$wday];2494$date{'month'} =$months[$mon];2495$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2496$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2497$date{'mday-time'} =sprintf"%d%s%02d:%02d",2498$mday,$months[$mon],$hour,$min;2499$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",25001900+$year,1+$mon,$mday,$hour,$min,$sec;25012502$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2503my$local=$epoch+ ((int$1+ ($2/60)) *3600);2504($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2505$date{'hour_local'} =$hour;2506$date{'minute_local'} =$min;2507$date{'tz_local'} =$tz;2508$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",25091900+$year,$mon+1,$mday,2510$hour,$min,$sec,$tz);2511return%date;2512}25132514sub parse_tag {2515my$tag_id=shift;2516my%tag;2517my@comment;25182519open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2520$tag{'id'} =$tag_id;2521while(my$line= <$fd>) {2522chomp$line;2523if($line=~m/^object ([0-9a-fA-F]{40})$/) {2524$tag{'object'} =$1;2525}elsif($line=~m/^type (.+)$/) {2526$tag{'type'} =$1;2527}elsif($line=~m/^tag (.+)$/) {2528$tag{'name'} =$1;2529}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2530$tag{'author'} =$1;2531$tag{'author_epoch'} =$2;2532$tag{'author_tz'} =$3;2533if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2534$tag{'author_name'} =$1;2535$tag{'author_email'} =$2;2536}else{2537$tag{'author_name'} =$tag{'author'};2538}2539}elsif($line=~m/--BEGIN/) {2540push@comment,$line;2541last;2542}elsif($lineeq"") {2543last;2544}2545}2546push@comment, <$fd>;2547$tag{'comment'} = \@comment;2548close$fdorreturn;2549if(!defined$tag{'name'}) {2550return2551};2552return%tag2553}25542555sub parse_commit_text {2556my($commit_text,$withparents) =@_;2557my@commit_lines=split'\n',$commit_text;2558my%co;25592560pop@commit_lines;# Remove '\0'25612562if(!@commit_lines) {2563return;2564}25652566my$header=shift@commit_lines;2567if($header!~m/^[0-9a-fA-F]{40}/) {2568return;2569}2570($co{'id'},my@parents) =split' ',$header;2571while(my$line=shift@commit_lines) {2572last if$lineeq"\n";2573if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2574$co{'tree'} =$1;2575}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2576push@parents,$1;2577}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2578$co{'author'} =$1;2579$co{'author_epoch'} =$2;2580$co{'author_tz'} =$3;2581if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2582$co{'author_name'} =$1;2583$co{'author_email'} =$2;2584}else{2585$co{'author_name'} =$co{'author'};2586}2587}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2588$co{'committer'} =$1;2589$co{'committer_epoch'} =$2;2590$co{'committer_tz'} =$3;2591$co{'committer_name'} =$co{'committer'};2592if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2593$co{'committer_name'} =$1;2594$co{'committer_email'} =$2;2595}else{2596$co{'committer_name'} =$co{'committer'};2597}2598}2599}2600if(!defined$co{'tree'}) {2601return;2602};2603$co{'parents'} = \@parents;2604$co{'parent'} =$parents[0];26052606foreachmy$title(@commit_lines) {2607$title=~s/^ //;2608if($titlene"") {2609$co{'title'} = chop_str($title,80,5);2610# remove leading stuff of merges to make the interesting part visible2611if(length($title) >50) {2612$title=~s/^Automatic //;2613$title=~s/^merge (of|with) /Merge ... /i;2614if(length($title) >50) {2615$title=~s/(http|rsync):\/\///;2616}2617if(length($title) >50) {2618$title=~s/(master|www|rsync)\.//;2619}2620if(length($title) >50) {2621$title=~s/kernel.org:?//;2622}2623if(length($title) >50) {2624$title=~s/\/pub\/scm//;2625}2626}2627$co{'title_short'} = chop_str($title,50,5);2628last;2629}2630}2631if(!defined$co{'title'} ||$co{'title'}eq"") {2632$co{'title'} =$co{'title_short'} ='(no commit message)';2633}2634# remove added spaces2635foreachmy$line(@commit_lines) {2636$line=~s/^ //;2637}2638$co{'comment'} = \@commit_lines;26392640my$age=time-$co{'committer_epoch'};2641$co{'age'} =$age;2642$co{'age_string'} = age_string($age);2643my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2644if($age>60*60*24*7*2) {2645$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2646$co{'age_string_age'} =$co{'age_string'};2647}else{2648$co{'age_string_date'} =$co{'age_string'};2649$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2650}2651return%co;2652}26532654sub parse_commit {2655my($commit_id) =@_;2656my%co;26572658local$/="\0";26592660open my$fd,"-|", git_cmd(),"rev-list",2661"--parents",2662"--header",2663"--max-count=1",2664$commit_id,2665"--",2666or die_error(500,"Open git-rev-list failed");2667%co= parse_commit_text(<$fd>,1);2668close$fd;26692670return%co;2671}26722673sub parse_commits {2674my($commit_id,$maxcount,$skip,$filename,@args) =@_;2675my@cos;26762677$maxcount||=1;2678$skip||=0;26792680local$/="\0";26812682open my$fd,"-|", git_cmd(),"rev-list",2683"--header",2684@args,2685("--max-count=".$maxcount),2686("--skip=".$skip),2687@extra_options,2688$commit_id,2689"--",2690($filename? ($filename) : ())2691or die_error(500,"Open git-rev-list failed");2692while(my$line= <$fd>) {2693my%co= parse_commit_text($line);2694push@cos, \%co;2695}2696close$fd;26972698returnwantarray?@cos: \@cos;2699}27002701# parse line of git-diff-tree "raw" output2702sub parse_difftree_raw_line {2703my$line=shift;2704my%res;27052706# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2707# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2708if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2709$res{'from_mode'} =$1;2710$res{'to_mode'} =$2;2711$res{'from_id'} =$3;2712$res{'to_id'} =$4;2713$res{'status'} =$5;2714$res{'similarity'} =$6;2715if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2716($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2717}else{2718$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2719}2720}2721# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2722# combined diff (for merge commit)2723elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2724$res{'nparents'} =length($1);2725$res{'from_mode'} = [split(' ',$2) ];2726$res{'to_mode'} =pop@{$res{'from_mode'}};2727$res{'from_id'} = [split(' ',$3) ];2728$res{'to_id'} =pop@{$res{'from_id'}};2729$res{'status'} = [split('',$4) ];2730$res{'to_file'} = unquote($5);2731}2732# 'c512b523472485aef4fff9e57b229d9d243c967f'2733elsif($line=~m/^([0-9a-fA-F]{40})$/) {2734$res{'commit'} =$1;2735}27362737returnwantarray?%res: \%res;2738}27392740# wrapper: return parsed line of git-diff-tree "raw" output2741# (the argument might be raw line, or parsed info)2742sub parsed_difftree_line {2743my$line_or_ref=shift;27442745if(ref($line_or_ref)eq"HASH") {2746# pre-parsed (or generated by hand)2747return$line_or_ref;2748}else{2749return parse_difftree_raw_line($line_or_ref);2750}2751}27522753# parse line of git-ls-tree output2754sub parse_ls_tree_line {2755my$line=shift;2756my%opts=@_;2757my%res;27582759#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2760$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;27612762$res{'mode'} =$1;2763$res{'type'} =$2;2764$res{'hash'} =$3;2765if($opts{'-z'}) {2766$res{'name'} =$4;2767}else{2768$res{'name'} = unquote($4);2769}27702771returnwantarray?%res: \%res;2772}27732774# generates _two_ hashes, references to which are passed as 2 and 3 argument2775sub parse_from_to_diffinfo {2776my($diffinfo,$from,$to,@parents) =@_;27772778if($diffinfo->{'nparents'}) {2779# combined diff2780$from->{'file'} = [];2781$from->{'href'} = [];2782 fill_from_file_info($diffinfo,@parents)2783unlessexists$diffinfo->{'from_file'};2784for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2785$from->{'file'}[$i] =2786defined$diffinfo->{'from_file'}[$i] ?2787$diffinfo->{'from_file'}[$i] :2788$diffinfo->{'to_file'};2789if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2790$from->{'href'}[$i] = href(action=>"blob",2791 hash_base=>$parents[$i],2792 hash=>$diffinfo->{'from_id'}[$i],2793 file_name=>$from->{'file'}[$i]);2794}else{2795$from->{'href'}[$i] =undef;2796}2797}2798}else{2799# ordinary (not combined) diff2800$from->{'file'} =$diffinfo->{'from_file'};2801if($diffinfo->{'status'}ne"A") {# not new (added) file2802$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2803 hash=>$diffinfo->{'from_id'},2804 file_name=>$from->{'file'});2805}else{2806delete$from->{'href'};2807}2808}28092810$to->{'file'} =$diffinfo->{'to_file'};2811if(!is_deleted($diffinfo)) {# file exists in result2812$to->{'href'} = href(action=>"blob", hash_base=>$hash,2813 hash=>$diffinfo->{'to_id'},2814 file_name=>$to->{'file'});2815}else{2816delete$to->{'href'};2817}2818}28192820## ......................................................................2821## parse to array of hashes functions28222823sub git_get_heads_list {2824my$limit=shift;2825my@headslist;28262827open my$fd,'-|', git_cmd(),'for-each-ref',2828($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2829'--format=%(objectname) %(refname) %(subject)%00%(committer)',2830'refs/heads'2831orreturn;2832while(my$line= <$fd>) {2833my%ref_item;28342835chomp$line;2836my($refinfo,$committerinfo) =split(/\0/,$line);2837my($hash,$name,$title) =split(' ',$refinfo,3);2838my($committer,$epoch,$tz) =2839($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2840$ref_item{'fullname'} =$name;2841$name=~s!^refs/heads/!!;28422843$ref_item{'name'} =$name;2844$ref_item{'id'} =$hash;2845$ref_item{'title'} =$title||'(no commit message)';2846$ref_item{'epoch'} =$epoch;2847if($epoch) {2848$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2849}else{2850$ref_item{'age'} ="unknown";2851}28522853push@headslist, \%ref_item;2854}2855close$fd;28562857returnwantarray?@headslist: \@headslist;2858}28592860sub git_get_tags_list {2861my$limit=shift;2862my@tagslist;28632864open my$fd,'-|', git_cmd(),'for-each-ref',2865($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2866'--format=%(objectname) %(objecttype) %(refname) '.2867'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2868'refs/tags'2869orreturn;2870while(my$line= <$fd>) {2871my%ref_item;28722873chomp$line;2874my($refinfo,$creatorinfo) =split(/\0/,$line);2875my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2876my($creator,$epoch,$tz) =2877($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2878$ref_item{'fullname'} =$name;2879$name=~s!^refs/tags/!!;28802881$ref_item{'type'} =$type;2882$ref_item{'id'} =$id;2883$ref_item{'name'} =$name;2884if($typeeq"tag") {2885$ref_item{'subject'} =$title;2886$ref_item{'reftype'} =$reftype;2887$ref_item{'refid'} =$refid;2888}else{2889$ref_item{'reftype'} =$type;2890$ref_item{'refid'} =$id;2891}28922893if($typeeq"tag"||$typeeq"commit") {2894$ref_item{'epoch'} =$epoch;2895if($epoch) {2896$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2897}else{2898$ref_item{'age'} ="unknown";2899}2900}29012902push@tagslist, \%ref_item;2903}2904close$fd;29052906returnwantarray?@tagslist: \@tagslist;2907}29082909## ----------------------------------------------------------------------2910## filesystem-related functions29112912sub get_file_owner {2913my$path=shift;29142915my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2916my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2917if(!defined$gcos) {2918returnundef;2919}2920my$owner=$gcos;2921$owner=~s/[,;].*$//;2922return to_utf8($owner);2923}29242925# assume that file exists2926sub insert_file {2927my$filename=shift;29282929open my$fd,'<',$filename;2930print map{ to_utf8($_) } <$fd>;2931close$fd;2932}29332934## ......................................................................2935## mimetype related functions29362937sub mimetype_guess_file {2938my$filename=shift;2939my$mimemap=shift;2940-r $mimemaporreturnundef;29412942my%mimemap;2943open(my$mh,'<',$mimemap)orreturnundef;2944while(<$mh>) {2945next ifm/^#/;# skip comments2946my($mimetype,$exts) =split(/\t+/);2947if(defined$exts) {2948my@exts=split(/\s+/,$exts);2949foreachmy$ext(@exts) {2950$mimemap{$ext} =$mimetype;2951}2952}2953}2954close($mh);29552956$filename=~/\.([^.]*)$/;2957return$mimemap{$1};2958}29592960sub mimetype_guess {2961my$filename=shift;2962my$mime;2963$filename=~/\./orreturnundef;29642965if($mimetypes_file) {2966my$file=$mimetypes_file;2967if($file!~m!^/!) {# if it is relative path2968# it is relative to project2969$file="$projectroot/$project/$file";2970}2971$mime= mimetype_guess_file($filename,$file);2972}2973$mime||= mimetype_guess_file($filename,'/etc/mime.types');2974return$mime;2975}29762977sub blob_mimetype {2978my$fd=shift;2979my$filename=shift;29802981if($filename) {2982my$mime= mimetype_guess($filename);2983$mimeandreturn$mime;2984}29852986# just in case2987return$default_blob_plain_mimetypeunless$fd;29882989if(-T $fd) {2990return'text/plain';2991}elsif(!$filename) {2992return'application/octet-stream';2993}elsif($filename=~m/\.png$/i) {2994return'image/png';2995}elsif($filename=~m/\.gif$/i) {2996return'image/gif';2997}elsif($filename=~m/\.jpe?g$/i) {2998return'image/jpeg';2999}else{3000return'application/octet-stream';3001}3002}30033004sub blob_contenttype {3005my($fd,$file_name,$type) =@_;30063007$type||= blob_mimetype($fd,$file_name);3008if($typeeq'text/plain'&&defined$default_text_plain_charset) {3009$type.="; charset=$default_text_plain_charset";3010}30113012return$type;3013}30143015## ======================================================================3016## functions printing HTML: header, footer, error page30173018sub git_header_html {3019my$status=shift||"200 OK";3020my$expires=shift;30213022my$title="$site_name";3023if(defined$project) {3024$title.=" - ". to_utf8($project);3025if(defined$action) {3026$title.="/$action";3027if(defined$file_name) {3028$title.=" - ". esc_path($file_name);3029if($actioneq"tree"&&$file_name!~ m|/$|) {3030$title.="/";3031}3032}3033}3034}3035my$content_type;3036# require explicit support from the UA if we are to send the page as3037# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3038# we have to do this because MSIE sometimes globs '*/*', pretending to3039# support xhtml+xml but choking when it gets what it asked for.3040if(defined$cgi->http('HTTP_ACCEPT') &&3041$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3042$cgi->Accept('application/xhtml+xml') !=0) {3043$content_type='application/xhtml+xml';3044}else{3045$content_type='text/html';3046}3047print$cgi->header(-type=>$content_type, -charset =>'utf-8',3048-status=>$status, -expires =>$expires);3049my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3050print<<EOF;3051<?xml version="1.0" encoding="utf-8"?>3052<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3053<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3054<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3055<!-- git core binaries version$git_version-->3056<head>3057<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3058<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3059<meta name="robots" content="index, nofollow"/>3060<title>$title</title>3061EOF3062# the stylesheet, favicon etc urls won't work correctly with path_info3063# unless we set the appropriate base URL3064if($ENV{'PATH_INFO'}) {3065print"<base href=\"".esc_url($base_url)."\"/>\n";3066}3067# print out each stylesheet that exist, providing backwards capability3068# for those people who defined $stylesheet in a config file3069if(defined$stylesheet) {3070print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3071}else{3072foreachmy$stylesheet(@stylesheets) {3073next unless$stylesheet;3074print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3075}3076}3077if(defined$project) {3078my%href_params= get_feed_info();3079if(!exists$href_params{'-title'}) {3080$href_params{'-title'} ='log';3081}30823083foreachmy$formatqw(RSS Atom){3084my$type=lc($format);3085my%link_attr= (3086'-rel'=>'alternate',3087'-title'=>"$project-$href_params{'-title'} -$formatfeed",3088'-type'=>"application/$type+xml"3089);30903091$href_params{'action'} =$type;3092$link_attr{'-href'} = href(%href_params);3093print"<link ".3094"rel=\"$link_attr{'-rel'}\"".3095"title=\"$link_attr{'-title'}\"".3096"href=\"$link_attr{'-href'}\"".3097"type=\"$link_attr{'-type'}\"".3098"/>\n";30993100$href_params{'extra_options'} ='--no-merges';3101$link_attr{'-href'} = href(%href_params);3102$link_attr{'-title'} .=' (no merges)';3103print"<link ".3104"rel=\"$link_attr{'-rel'}\"".3105"title=\"$link_attr{'-title'}\"".3106"href=\"$link_attr{'-href'}\"".3107"type=\"$link_attr{'-type'}\"".3108"/>\n";3109}31103111}else{3112printf('<link rel="alternate" title="%sprojects list" '.3113'href="%s" type="text/plain; charset=utf-8" />'."\n",3114$site_name, href(project=>undef, action=>"project_index"));3115printf('<link rel="alternate" title="%sprojects feeds" '.3116'href="%s" type="text/x-opml" />'."\n",3117$site_name, href(project=>undef, action=>"opml"));3118}3119if(defined$favicon) {3120printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3121}31223123print"</head>\n".3124"<body>\n";31253126if(-f $site_header) {3127 insert_file($site_header);3128}31293130print"<div class=\"page_header\">\n".3131$cgi->a({-href => esc_url($logo_url),3132-title =>$logo_label},3133qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3134print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3135if(defined$project) {3136print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3137if(defined$action) {3138print" /$action";3139}3140print"\n";3141}3142print"</div>\n";31433144my$have_search= gitweb_check_feature('search');3145if(defined$project&&$have_search) {3146if(!defined$searchtext) {3147$searchtext="";3148}3149my$search_hash;3150if(defined$hash_base) {3151$search_hash=$hash_base;3152}elsif(defined$hash) {3153$search_hash=$hash;3154}else{3155$search_hash="HEAD";3156}3157my$action=$my_uri;3158my$use_pathinfo= gitweb_check_feature('pathinfo');3159if($use_pathinfo) {3160$action.="/".esc_url($project);3161}3162print$cgi->startform(-method=>"get", -action =>$action) .3163"<div class=\"search\">\n".3164(!$use_pathinfo&&3165$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3166$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3167$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3168$cgi->popup_menu(-name =>'st', -default=>'commit',3169-values=> ['commit','grep','author','committer','pickaxe']) .3170$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3171" search:\n",3172$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3173"<span title=\"Extended regular expression\">".3174$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3175-checked =>$search_use_regexp) .3176"</span>".3177"</div>".3178$cgi->end_form() ."\n";3179}3180}31813182sub git_footer_html {3183my$feed_class='rss_logo';31843185print"<div class=\"page_footer\">\n";3186if(defined$project) {3187my$descr= git_get_project_description($project);3188if(defined$descr) {3189print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3190}31913192my%href_params= get_feed_info();3193if(!%href_params) {3194$feed_class.=' generic';3195}3196$href_params{'-title'} ||='log';31973198foreachmy$formatqw(RSS Atom){3199$href_params{'action'} =lc($format);3200print$cgi->a({-href => href(%href_params),3201-title =>"$href_params{'-title'}$formatfeed",3202-class=>$feed_class},$format)."\n";3203}32043205}else{3206print$cgi->a({-href => href(project=>undef, action=>"opml"),3207-class=>$feed_class},"OPML") ." ";3208print$cgi->a({-href => href(project=>undef, action=>"project_index"),3209-class=>$feed_class},"TXT") ."\n";3210}3211print"</div>\n";# class="page_footer"32123213if(-f $site_footer) {3214 insert_file($site_footer);3215}32163217print"</body>\n".3218"</html>";3219}32203221# die_error(<http_status_code>, <error_message>)3222# Example: die_error(404, 'Hash not found')3223# By convention, use the following status codes (as defined in RFC 2616):3224# 400: Invalid or missing CGI parameters, or3225# requested object exists but has wrong type.3226# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3227# this server or project.3228# 404: Requested object/revision/project doesn't exist.3229# 500: The server isn't configured properly, or3230# an internal error occurred (e.g. failed assertions caused by bugs), or3231# an unknown error occurred (e.g. the git binary died unexpectedly).3232sub die_error {3233my$status=shift||500;3234my$error=shift||"Internal server error";32353236my%http_responses= (400=>'400 Bad Request',3237403=>'403 Forbidden',3238404=>'404 Not Found',3239500=>'500 Internal Server Error');3240 git_header_html($http_responses{$status});3241print<<EOF;3242<div class="page_body">3243<br /><br />3244$status-$error3245<br />3246</div>3247EOF3248 git_footer_html();3249exit;3250}32513252## ----------------------------------------------------------------------3253## functions printing or outputting HTML: navigation32543255sub git_print_page_nav {3256my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3257$extra=''if!defined$extra;# pager or formats32583259my@navs=qw(summary shortlog log commit commitdiff tree);3260if($suppress) {3261@navs=grep{$_ne$suppress}@navs;3262}32633264my%arg=map{$_=> {action=>$_} }@navs;3265if(defined$head) {3266for(qw(commit commitdiff)) {3267$arg{$_}{'hash'} =$head;3268}3269if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3270for(qw(shortlog log)) {3271$arg{$_}{'hash'} =$head;3272}3273}3274}32753276$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3277$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;32783279my@actions= gitweb_get_feature('actions');3280my%repl= (3281'%'=>'%',3282'n'=>$project,# project name3283'f'=>$git_dir,# project path within filesystem3284'h'=>$treehead||'',# current hash ('h' parameter)3285'b'=>$treebase||'',# hash base ('hb' parameter)3286);3287while(@actions) {3288my($label,$link,$pos) =splice(@actions,0,3);3289# insert3290@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3291# munch munch3292$link=~s/%([%nfhb])/$repl{$1}/g;3293$arg{$label}{'_href'} =$link;3294}32953296print"<div class=\"page_nav\">\n".3297(join" | ",3298map{$_eq$current?3299$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3300}@navs);3301print"<br/>\n$extra<br/>\n".3302"</div>\n";3303}33043305sub format_paging_nav {3306my($action,$hash,$head,$page,$has_next_link) =@_;3307my$paging_nav;330833093310if($hashne$head||$page) {3311$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3312}else{3313$paging_nav.="HEAD";3314}33153316if($page>0) {3317$paging_nav.=" ⋅ ".3318$cgi->a({-href => href(-replay=>1, page=>$page-1),3319-accesskey =>"p", -title =>"Alt-p"},"prev");3320}else{3321$paging_nav.=" ⋅ prev";3322}33233324if($has_next_link) {3325$paging_nav.=" ⋅ ".3326$cgi->a({-href => href(-replay=>1, page=>$page+1),3327-accesskey =>"n", -title =>"Alt-n"},"next");3328}else{3329$paging_nav.=" ⋅ next";3330}33313332return$paging_nav;3333}33343335## ......................................................................3336## functions printing or outputting HTML: div33373338sub git_print_header_div {3339my($action,$title,$hash,$hash_base) =@_;3340my%args= ();33413342$args{'action'} =$action;3343$args{'hash'} =$hashif$hash;3344$args{'hash_base'} =$hash_baseif$hash_base;33453346print"<div class=\"header\">\n".3347$cgi->a({-href => href(%args), -class=>"title"},3348$title?$title:$action) .3349"\n</div>\n";3350}33513352sub print_local_time {3353my%date=@_;3354if($date{'hour_local'} <6) {3355printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3356$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3357}else{3358printf(" (%02d:%02d%s)",3359$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3360}3361}33623363# Outputs the author name and date in long form3364sub git_print_authorship {3365my$co=shift;3366my%opts=@_;3367my$tag=$opts{-tag} ||'div';33683369my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3370print"<$tagclass=\"author_date\">".3371 esc_html($co->{'author_name'}) .3372" [$ad{'rfc2822'}";3373 print_local_time(%ad)if($opts{-localtime});3374print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3375."</$tag>\n";3376}33773378# Outputs table rows containing the full author or committer information,3379# in the format expected for 'commit' view (& similia).3380# Parameters are a commit hash reference, followed by the list of people3381# to output information for. If the list is empty it defalts to both3382# author and committer.3383sub git_print_authorship_rows {3384my$co=shift;3385# too bad we can't use @people = @_ || ('author', 'committer')3386my@people=@_;3387@people= ('author','committer')unless@people;3388foreachmy$who(@people) {3389my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3390print"<tr><td>$who</td><td>". esc_html($co->{$who}) ."</td>".3391"<td rowspan=\"2\">".3392 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3393"</td></tr>\n".3394"<tr>".3395"<td></td><td>$wd{'rfc2822'}";3396 print_local_time(%wd);3397print"</td>".3398"</tr>\n";3399}3400}34013402sub git_print_page_path {3403my$name=shift;3404my$type=shift;3405my$hb=shift;340634073408print"<div class=\"page_path\">";3409print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3410-title =>'tree root'}, to_utf8("[$project]"));3411print" / ";3412if(defined$name) {3413my@dirname=split'/',$name;3414my$basename=pop@dirname;3415my$fullname='';34163417foreachmy$dir(@dirname) {3418$fullname.= ($fullname?'/':'') .$dir;3419print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3420 hash_base=>$hb),3421-title =>$fullname}, esc_path($dir));3422print" / ";3423}3424if(defined$type&&$typeeq'blob') {3425print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3426 hash_base=>$hb),3427-title =>$name}, esc_path($basename));3428}elsif(defined$type&&$typeeq'tree') {3429print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3430 hash_base=>$hb),3431-title =>$name}, esc_path($basename));3432print" / ";3433}else{3434print esc_path($basename);3435}3436}3437print"<br/></div>\n";3438}34393440sub git_print_log {3441my$log=shift;3442my%opts=@_;34433444if($opts{'-remove_title'}) {3445# remove title, i.e. first line of log3446shift@$log;3447}3448# remove leading empty lines3449while(defined$log->[0] &&$log->[0]eq"") {3450shift@$log;3451}34523453# print log3454my$signoff=0;3455my$empty=0;3456foreachmy$line(@$log) {3457if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3458$signoff=1;3459$empty=0;3460if(!$opts{'-remove_signoff'}) {3461print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3462next;3463}else{3464# remove signoff lines3465next;3466}3467}else{3468$signoff=0;3469}34703471# print only one empty line3472# do not print empty line after signoff3473if($lineeq"") {3474next if($empty||$signoff);3475$empty=1;3476}else{3477$empty=0;3478}34793480print format_log_line_html($line) ."<br/>\n";3481}34823483if($opts{'-final_empty_line'}) {3484# end with single empty line3485print"<br/>\n"unless$empty;3486}3487}34883489# return link target (what link points to)3490sub git_get_link_target {3491my$hash=shift;3492my$link_target;34933494# read link3495open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3496orreturn;3497{3498local$/=undef;3499$link_target= <$fd>;3500}3501close$fd3502orreturn;35033504return$link_target;3505}35063507# given link target, and the directory (basedir) the link is in,3508# return target of link relative to top directory (top tree);3509# return undef if it is not possible (including absolute links).3510sub normalize_link_target {3511my($link_target,$basedir) =@_;35123513# absolute symlinks (beginning with '/') cannot be normalized3514return if(substr($link_target,0,1)eq'/');35153516# normalize link target to path from top (root) tree (dir)3517my$path;3518if($basedir) {3519$path=$basedir.'/'.$link_target;3520}else{3521# we are in top (root) tree (dir)3522$path=$link_target;3523}35243525# remove //, /./, and /../3526my@path_parts;3527foreachmy$part(split('/',$path)) {3528# discard '.' and ''3529next if(!$part||$parteq'.');3530# handle '..'3531if($parteq'..') {3532if(@path_parts) {3533pop@path_parts;3534}else{3535# link leads outside repository (outside top dir)3536return;3537}3538}else{3539push@path_parts,$part;3540}3541}3542$path=join('/',@path_parts);35433544return$path;3545}35463547# print tree entry (row of git_tree), but without encompassing <tr> element3548sub git_print_tree_entry {3549my($t,$basedir,$hash_base,$have_blame) =@_;35503551my%base_key= ();3552$base_key{'hash_base'} =$hash_baseifdefined$hash_base;35533554# The format of a table row is: mode list link. Where mode is3555# the mode of the entry, list is the name of the entry, an href,3556# and link is the action links of the entry.35573558print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3559if($t->{'type'}eq"blob") {3560print"<td class=\"list\">".3561$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3562 file_name=>"$basedir$t->{'name'}",%base_key),3563-class=>"list"}, esc_path($t->{'name'}));3564if(S_ISLNK(oct$t->{'mode'})) {3565my$link_target= git_get_link_target($t->{'hash'});3566if($link_target) {3567my$norm_target= normalize_link_target($link_target,$basedir);3568if(defined$norm_target) {3569print" -> ".3570$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3571 file_name=>$norm_target),3572-title =>$norm_target}, esc_path($link_target));3573}else{3574print" -> ". esc_path($link_target);3575}3576}3577}3578print"</td>\n";3579print"<td class=\"link\">";3580print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3581 file_name=>"$basedir$t->{'name'}",%base_key)},3582"blob");3583if($have_blame) {3584print" | ".3585$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3586 file_name=>"$basedir$t->{'name'}",%base_key)},3587"blame");3588}3589if(defined$hash_base) {3590print" | ".3591$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3592 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3593"history");3594}3595print" | ".3596$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3597 file_name=>"$basedir$t->{'name'}")},3598"raw");3599print"</td>\n";36003601}elsif($t->{'type'}eq"tree") {3602print"<td class=\"list\">";3603print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3604 file_name=>"$basedir$t->{'name'}",%base_key)},3605 esc_path($t->{'name'}));3606print"</td>\n";3607print"<td class=\"link\">";3608print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3609 file_name=>"$basedir$t->{'name'}",%base_key)},3610"tree");3611if(defined$hash_base) {3612print" | ".3613$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3614 file_name=>"$basedir$t->{'name'}")},3615"history");3616}3617print"</td>\n";3618}else{3619# unknown object: we can only present history for it3620# (this includes 'commit' object, i.e. submodule support)3621print"<td class=\"list\">".3622 esc_path($t->{'name'}) .3623"</td>\n";3624print"<td class=\"link\">";3625if(defined$hash_base) {3626print$cgi->a({-href => href(action=>"history",3627 hash_base=>$hash_base,3628 file_name=>"$basedir$t->{'name'}")},3629"history");3630}3631print"</td>\n";3632}3633}36343635## ......................................................................3636## functions printing large fragments of HTML36373638# get pre-image filenames for merge (combined) diff3639sub fill_from_file_info {3640my($diff,@parents) =@_;36413642$diff->{'from_file'} = [ ];3643$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3644for(my$i=0;$i<$diff->{'nparents'};$i++) {3645if($diff->{'status'}[$i]eq'R'||3646$diff->{'status'}[$i]eq'C') {3647$diff->{'from_file'}[$i] =3648 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3649}3650}36513652return$diff;3653}36543655# is current raw difftree line of file deletion3656sub is_deleted {3657my$diffinfo=shift;36583659return$diffinfo->{'to_id'}eq('0' x 40);3660}36613662# does patch correspond to [previous] difftree raw line3663# $diffinfo - hashref of parsed raw diff format3664# $patchinfo - hashref of parsed patch diff format3665# (the same keys as in $diffinfo)3666sub is_patch_split {3667my($diffinfo,$patchinfo) =@_;36683669returndefined$diffinfo&&defined$patchinfo3670&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3671}367236733674sub git_difftree_body {3675my($difftree,$hash,@parents) =@_;3676my($parent) =$parents[0];3677my$have_blame= gitweb_check_feature('blame');3678print"<div class=\"list_head\">\n";3679if($#{$difftree} >10) {3680print(($#{$difftree} +1) ." files changed:\n");3681}3682print"</div>\n";36833684print"<table class=\"".3685(@parents>1?"combined ":"") .3686"diff_tree\">\n";36873688# header only for combined diff in 'commitdiff' view3689my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3690if($has_header) {3691# table header3692print"<thead><tr>\n".3693"<th></th><th></th>\n";# filename, patchN link3694for(my$i=0;$i<@parents;$i++) {3695my$par=$parents[$i];3696print"<th>".3697$cgi->a({-href => href(action=>"commitdiff",3698 hash=>$hash, hash_parent=>$par),3699-title =>'commitdiff to parent number '.3700($i+1) .': '.substr($par,0,7)},3701$i+1) .3702" </th>\n";3703}3704print"</tr></thead>\n<tbody>\n";3705}37063707my$alternate=1;3708my$patchno=0;3709foreachmy$line(@{$difftree}) {3710my$diff= parsed_difftree_line($line);37113712if($alternate) {3713print"<tr class=\"dark\">\n";3714}else{3715print"<tr class=\"light\">\n";3716}3717$alternate^=1;37183719if(exists$diff->{'nparents'}) {# combined diff37203721 fill_from_file_info($diff,@parents)3722unlessexists$diff->{'from_file'};37233724if(!is_deleted($diff)) {3725# file exists in the result (child) commit3726print"<td>".3727$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3728 file_name=>$diff->{'to_file'},3729 hash_base=>$hash),3730-class=>"list"}, esc_path($diff->{'to_file'})) .3731"</td>\n";3732}else{3733print"<td>".3734 esc_path($diff->{'to_file'}) .3735"</td>\n";3736}37373738if($actioneq'commitdiff') {3739# link to patch3740$patchno++;3741print"<td class=\"link\">".3742$cgi->a({-href =>"#patch$patchno"},"patch") .3743" | ".3744"</td>\n";3745}37463747my$has_history=0;3748my$not_deleted=0;3749for(my$i=0;$i<$diff->{'nparents'};$i++) {3750my$hash_parent=$parents[$i];3751my$from_hash=$diff->{'from_id'}[$i];3752my$from_path=$diff->{'from_file'}[$i];3753my$status=$diff->{'status'}[$i];37543755$has_history||= ($statusne'A');3756$not_deleted||= ($statusne'D');37573758if($statuseq'A') {3759print"<td class=\"link\"align=\"right\"> | </td>\n";3760}elsif($statuseq'D') {3761print"<td class=\"link\">".3762$cgi->a({-href => href(action=>"blob",3763 hash_base=>$hash,3764 hash=>$from_hash,3765 file_name=>$from_path)},3766"blob". ($i+1)) .3767" | </td>\n";3768}else{3769if($diff->{'to_id'}eq$from_hash) {3770print"<td class=\"link nochange\">";3771}else{3772print"<td class=\"link\">";3773}3774print$cgi->a({-href => href(action=>"blobdiff",3775 hash=>$diff->{'to_id'},3776 hash_parent=>$from_hash,3777 hash_base=>$hash,3778 hash_parent_base=>$hash_parent,3779 file_name=>$diff->{'to_file'},3780 file_parent=>$from_path)},3781"diff". ($i+1)) .3782" | </td>\n";3783}3784}37853786print"<td class=\"link\">";3787if($not_deleted) {3788print$cgi->a({-href => href(action=>"blob",3789 hash=>$diff->{'to_id'},3790 file_name=>$diff->{'to_file'},3791 hash_base=>$hash)},3792"blob");3793print" | "if($has_history);3794}3795if($has_history) {3796print$cgi->a({-href => href(action=>"history",3797 file_name=>$diff->{'to_file'},3798 hash_base=>$hash)},3799"history");3800}3801print"</td>\n";38023803print"</tr>\n";3804next;# instead of 'else' clause, to avoid extra indent3805}3806# else ordinary diff38073808my($to_mode_oct,$to_mode_str,$to_file_type);3809my($from_mode_oct,$from_mode_str,$from_file_type);3810if($diff->{'to_mode'}ne('0' x 6)) {3811$to_mode_oct=oct$diff->{'to_mode'};3812if(S_ISREG($to_mode_oct)) {# only for regular file3813$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3814}3815$to_file_type= file_type($diff->{'to_mode'});3816}3817if($diff->{'from_mode'}ne('0' x 6)) {3818$from_mode_oct=oct$diff->{'from_mode'};3819if(S_ISREG($to_mode_oct)) {# only for regular file3820$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3821}3822$from_file_type= file_type($diff->{'from_mode'});3823}38243825if($diff->{'status'}eq"A") {# created3826my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3827$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3828$mode_chng.="]</span>";3829print"<td>";3830print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3831 hash_base=>$hash, file_name=>$diff->{'file'}),3832-class=>"list"}, esc_path($diff->{'file'}));3833print"</td>\n";3834print"<td>$mode_chng</td>\n";3835print"<td class=\"link\">";3836if($actioneq'commitdiff') {3837# link to patch3838$patchno++;3839print$cgi->a({-href =>"#patch$patchno"},"patch");3840print" | ";3841}3842print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3843 hash_base=>$hash, file_name=>$diff->{'file'})},3844"blob");3845print"</td>\n";38463847}elsif($diff->{'status'}eq"D") {# deleted3848my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3849print"<td>";3850print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3851 hash_base=>$parent, file_name=>$diff->{'file'}),3852-class=>"list"}, esc_path($diff->{'file'}));3853print"</td>\n";3854print"<td>$mode_chng</td>\n";3855print"<td class=\"link\">";3856if($actioneq'commitdiff') {3857# link to patch3858$patchno++;3859print$cgi->a({-href =>"#patch$patchno"},"patch");3860print" | ";3861}3862print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3863 hash_base=>$parent, file_name=>$diff->{'file'})},3864"blob") ." | ";3865if($have_blame) {3866print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3867 file_name=>$diff->{'file'})},3868"blame") ." | ";3869}3870print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3871 file_name=>$diff->{'file'})},3872"history");3873print"</td>\n";38743875}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3876my$mode_chnge="";3877if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3878$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3879if($from_file_typene$to_file_type) {3880$mode_chnge.=" from$from_file_typeto$to_file_type";3881}3882if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3883if($from_mode_str&&$to_mode_str) {3884$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3885}elsif($to_mode_str) {3886$mode_chnge.=" mode:$to_mode_str";3887}3888}3889$mode_chnge.="]</span>\n";3890}3891print"<td>";3892print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3893 hash_base=>$hash, file_name=>$diff->{'file'}),3894-class=>"list"}, esc_path($diff->{'file'}));3895print"</td>\n";3896print"<td>$mode_chnge</td>\n";3897print"<td class=\"link\">";3898if($actioneq'commitdiff') {3899# link to patch3900$patchno++;3901print$cgi->a({-href =>"#patch$patchno"},"patch") .3902" | ";3903}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3904# "commit" view and modified file (not onlu mode changed)3905print$cgi->a({-href => href(action=>"blobdiff",3906 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3907 hash_base=>$hash, hash_parent_base=>$parent,3908 file_name=>$diff->{'file'})},3909"diff") .3910" | ";3911}3912print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3913 hash_base=>$hash, file_name=>$diff->{'file'})},3914"blob") ." | ";3915if($have_blame) {3916print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3917 file_name=>$diff->{'file'})},3918"blame") ." | ";3919}3920print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3921 file_name=>$diff->{'file'})},3922"history");3923print"</td>\n";39243925}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3926my%status_name= ('R'=>'moved','C'=>'copied');3927my$nstatus=$status_name{$diff->{'status'}};3928my$mode_chng="";3929if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3930# mode also for directories, so we cannot use $to_mode_str3931$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3932}3933print"<td>".3934$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3935 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3936-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3937"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3938$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3939 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3940-class=>"list"}, esc_path($diff->{'from_file'})) .3941" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3942"<td class=\"link\">";3943if($actioneq'commitdiff') {3944# link to patch3945$patchno++;3946print$cgi->a({-href =>"#patch$patchno"},"patch") .3947" | ";3948}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3949# "commit" view and modified file (not only pure rename or copy)3950print$cgi->a({-href => href(action=>"blobdiff",3951 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3952 hash_base=>$hash, hash_parent_base=>$parent,3953 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3954"diff") .3955" | ";3956}3957print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3958 hash_base=>$parent, file_name=>$diff->{'to_file'})},3959"blob") ." | ";3960if($have_blame) {3961print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3962 file_name=>$diff->{'to_file'})},3963"blame") ." | ";3964}3965print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3966 file_name=>$diff->{'to_file'})},3967"history");3968print"</td>\n";39693970}# we should not encounter Unmerged (U) or Unknown (X) status3971print"</tr>\n";3972}3973print"</tbody>"if$has_header;3974print"</table>\n";3975}39763977sub git_patchset_body {3978my($fd,$difftree,$hash,@hash_parents) =@_;3979my($hash_parent) =$hash_parents[0];39803981my$is_combined= (@hash_parents>1);3982my$patch_idx=0;3983my$patch_number=0;3984my$patch_line;3985my$diffinfo;3986my$to_name;3987my(%from,%to);39883989print"<div class=\"patchset\">\n";39903991# skip to first patch3992while($patch_line= <$fd>) {3993chomp$patch_line;39943995last if($patch_line=~m/^diff /);3996}39973998 PATCH:3999while($patch_line) {40004001# parse "git diff" header line4002if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4003# $1 is from_name, which we do not use4004$to_name= unquote($2);4005$to_name=~s!^b/!!;4006}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4007# $1 is 'cc' or 'combined', which we do not use4008$to_name= unquote($2);4009}else{4010$to_name=undef;4011}40124013# check if current patch belong to current raw line4014# and parse raw git-diff line if needed4015if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4016# this is continuation of a split patch4017print"<div class=\"patch cont\">\n";4018}else{4019# advance raw git-diff output if needed4020$patch_idx++ifdefined$diffinfo;40214022# read and prepare patch information4023$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);40244025# compact combined diff output can have some patches skipped4026# find which patch (using pathname of result) we are at now;4027if($is_combined) {4028while($to_namene$diffinfo->{'to_file'}) {4029print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4030 format_diff_cc_simplified($diffinfo,@hash_parents) .4031"</div>\n";# class="patch"40324033$patch_idx++;4034$patch_number++;40354036last if$patch_idx>$#$difftree;4037$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4038}4039}40404041# modifies %from, %to hashes4042 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);40434044# this is first patch for raw difftree line with $patch_idx index4045# we index @$difftree array from 0, but number patches from 14046print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4047}40484049# git diff header4050#assert($patch_line =~ m/^diff /) if DEBUG;4051#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4052$patch_number++;4053# print "git diff" header4054print format_git_diff_header_line($patch_line,$diffinfo,4055 \%from, \%to);40564057# print extended diff header4058print"<div class=\"diff extended_header\">\n";4059 EXTENDED_HEADER:4060while($patch_line= <$fd>) {4061chomp$patch_line;40624063last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);40644065print format_extended_diff_header_line($patch_line,$diffinfo,4066 \%from, \%to);4067}4068print"</div>\n";# class="diff extended_header"40694070# from-file/to-file diff header4071if(!$patch_line) {4072print"</div>\n";# class="patch"4073last PATCH;4074}4075next PATCH if($patch_line=~m/^diff /);4076#assert($patch_line =~ m/^---/) if DEBUG;40774078my$last_patch_line=$patch_line;4079$patch_line= <$fd>;4080chomp$patch_line;4081#assert($patch_line =~ m/^\+\+\+/) if DEBUG;40824083print format_diff_from_to_header($last_patch_line,$patch_line,4084$diffinfo, \%from, \%to,4085@hash_parents);40864087# the patch itself4088 LINE:4089while($patch_line= <$fd>) {4090chomp$patch_line;40914092next PATCH if($patch_line=~m/^diff /);40934094print format_diff_line($patch_line, \%from, \%to);4095}40964097}continue{4098print"</div>\n";# class="patch"4099}41004101# for compact combined (--cc) format, with chunk and patch simpliciaction4102# patchset might be empty, but there might be unprocessed raw lines4103for(++$patch_idxif$patch_number>0;4104$patch_idx<@$difftree;4105++$patch_idx) {4106# read and prepare patch information4107$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);41084109# generate anchor for "patch" links in difftree / whatchanged part4110print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4111 format_diff_cc_simplified($diffinfo,@hash_parents) .4112"</div>\n";# class="patch"41134114$patch_number++;4115}41164117if($patch_number==0) {4118if(@hash_parents>1) {4119print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4120}else{4121print"<div class=\"diff nodifferences\">No differences found</div>\n";4122}4123}41244125print"</div>\n";# class="patchset"4126}41274128# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41294130# fills project list info (age, description, owner, forks) for each4131# project in the list, removing invalid projects from returned list4132# NOTE: modifies $projlist, but does not remove entries from it4133sub fill_project_list_info {4134my($projlist,$check_forks) =@_;4135my@projects;41364137my$show_ctags= gitweb_check_feature('ctags');4138 PROJECT:4139foreachmy$pr(@$projlist) {4140my(@activity) = git_get_last_activity($pr->{'path'});4141unless(@activity) {4142next PROJECT;4143}4144($pr->{'age'},$pr->{'age_string'}) =@activity;4145if(!defined$pr->{'descr'}) {4146my$descr= git_get_project_description($pr->{'path'}) ||"";4147$descr= to_utf8($descr);4148$pr->{'descr_long'} =$descr;4149$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4150}4151if(!defined$pr->{'owner'}) {4152$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4153}4154if($check_forks) {4155my$pname=$pr->{'path'};4156if(($pname=~s/\.git$//) &&4157($pname!~/\/$/) &&4158(-d "$projectroot/$pname")) {4159$pr->{'forks'} ="-d$projectroot/$pname";4160}else{4161$pr->{'forks'} =0;4162}4163}4164$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4165push@projects,$pr;4166}41674168return@projects;4169}41704171# print 'sort by' <th> element, generating 'sort by $name' replay link4172# if that order is not selected4173sub print_sort_th {4174my($name,$order,$header) =@_;4175$header||=ucfirst($name);41764177if($ordereq$name) {4178print"<th>$header</th>\n";4179}else{4180print"<th>".4181$cgi->a({-href => href(-replay=>1, order=>$name),4182-class=>"header"},$header) .4183"</th>\n";4184}4185}41864187sub git_project_list_body {4188# actually uses global variable $project4189my($projlist,$order,$from,$to,$extra,$no_header) =@_;41904191my$check_forks= gitweb_check_feature('forks');4192my@projects= fill_project_list_info($projlist,$check_forks);41934194$order||=$default_projects_order;4195$from=0unlessdefined$from;4196$to=$#projectsif(!defined$to||$#projects<$to);41974198my%order_info= (4199 project => { key =>'path', type =>'str'},4200 descr => { key =>'descr_long', type =>'str'},4201 owner => { key =>'owner', type =>'str'},4202 age => { key =>'age', type =>'num'}4203);4204my$oi=$order_info{$order};4205if($oi->{'type'}eq'str') {4206@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4207}else{4208@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4209}42104211my$show_ctags= gitweb_check_feature('ctags');4212if($show_ctags) {4213my%ctags;4214foreachmy$p(@projects) {4215foreachmy$ct(keys%{$p->{'ctags'}}) {4216$ctags{$ct} +=$p->{'ctags'}->{$ct};4217}4218}4219my$cloud= git_populate_project_tagcloud(\%ctags);4220print git_show_project_tagcloud($cloud,64);4221}42224223print"<table class=\"project_list\">\n";4224unless($no_header) {4225print"<tr>\n";4226if($check_forks) {4227print"<th></th>\n";4228}4229 print_sort_th('project',$order,'Project');4230 print_sort_th('descr',$order,'Description');4231 print_sort_th('owner',$order,'Owner');4232 print_sort_th('age',$order,'Last Change');4233print"<th></th>\n".# for links4234"</tr>\n";4235}4236my$alternate=1;4237my$tagfilter=$cgi->param('by_tag');4238for(my$i=$from;$i<=$to;$i++) {4239my$pr=$projects[$i];42404241next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4242next if$searchtextand not$pr->{'path'} =~/$searchtext/4243and not$pr->{'descr_long'} =~/$searchtext/;4244# Weed out forks or non-matching entries of search4245if($check_forks) {4246my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4247$forkbase="^$forkbase"if$forkbase;4248next ifnot$searchtextand not$tagfilterand$show_ctags4249and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4250}42514252if($alternate) {4253print"<tr class=\"dark\">\n";4254}else{4255print"<tr class=\"light\">\n";4256}4257$alternate^=1;4258if($check_forks) {4259print"<td>";4260if($pr->{'forks'}) {4261print"<!--$pr->{'forks'} -->\n";4262print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4263}4264print"</td>\n";4265}4266print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4267-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4268"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4269-class=>"list", -title =>$pr->{'descr_long'}},4270 esc_html($pr->{'descr'})) ."</td>\n".4271"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4272print"<td class=\"". age_class($pr->{'age'}) ."\">".4273(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4274"<td class=\"link\">".4275$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4276$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4277$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4278$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4279($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4280"</td>\n".4281"</tr>\n";4282}4283if(defined$extra) {4284print"<tr>\n";4285if($check_forks) {4286print"<td></td>\n";4287}4288print"<td colspan=\"5\">$extra</td>\n".4289"</tr>\n";4290}4291print"</table>\n";4292}42934294sub git_shortlog_body {4295# uses global variable $project4296my($commitlist,$from,$to,$refs,$extra) =@_;42974298$from=0unlessdefined$from;4299$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43004301print"<table class=\"shortlog\">\n";4302my$alternate=1;4303for(my$i=$from;$i<=$to;$i++) {4304my%co= %{$commitlist->[$i]};4305my$commit=$co{'id'};4306my$ref= format_ref_marker($refs,$commit);4307if($alternate) {4308print"<tr class=\"dark\">\n";4309}else{4310print"<tr class=\"light\">\n";4311}4312$alternate^=1;4313# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4314print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4315 format_author_html('td', \%co,10) ."<td>";4316print format_subject_html($co{'title'},$co{'title_short'},4317 href(action=>"commit", hash=>$commit),$ref);4318print"</td>\n".4319"<td class=\"link\">".4320$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4321$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4322$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4323my$snapshot_links= format_snapshot_links($commit);4324if(defined$snapshot_links) {4325print" | ".$snapshot_links;4326}4327print"</td>\n".4328"</tr>\n";4329}4330if(defined$extra) {4331print"<tr>\n".4332"<td colspan=\"4\">$extra</td>\n".4333"</tr>\n";4334}4335print"</table>\n";4336}43374338sub git_history_body {4339# Warning: assumes constant type (blob or tree) during history4340my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;43414342$from=0unlessdefined$from;4343$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});43444345print"<table class=\"history\">\n";4346my$alternate=1;4347for(my$i=$from;$i<=$to;$i++) {4348my%co= %{$commitlist->[$i]};4349if(!%co) {4350next;4351}4352my$commit=$co{'id'};43534354my$ref= format_ref_marker($refs,$commit);43554356if($alternate) {4357print"<tr class=\"dark\">\n";4358}else{4359print"<tr class=\"light\">\n";4360}4361$alternate^=1;4362print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4363# shortlog: format_author_html('td', \%co, 10)4364 format_author_html('td', \%co,15,3) ."<td>";4365# originally git_history used chop_str($co{'title'}, 50)4366print format_subject_html($co{'title'},$co{'title_short'},4367 href(action=>"commit", hash=>$commit),$ref);4368print"</td>\n".4369"<td class=\"link\">".4370$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4371$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");43724373if($ftypeeq'blob') {4374my$blob_current= git_get_hash_by_path($hash_base,$file_name);4375my$blob_parent= git_get_hash_by_path($commit,$file_name);4376if(defined$blob_current&&defined$blob_parent&&4377$blob_currentne$blob_parent) {4378print" | ".4379$cgi->a({-href => href(action=>"blobdiff",4380 hash=>$blob_current, hash_parent=>$blob_parent,4381 hash_base=>$hash_base, hash_parent_base=>$commit,4382 file_name=>$file_name)},4383"diff to current");4384}4385}4386print"</td>\n".4387"</tr>\n";4388}4389if(defined$extra) {4390print"<tr>\n".4391"<td colspan=\"4\">$extra</td>\n".4392"</tr>\n";4393}4394print"</table>\n";4395}43964397sub git_tags_body {4398# uses global variable $project4399my($taglist,$from,$to,$extra) =@_;4400$from=0unlessdefined$from;4401$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);44024403print"<table class=\"tags\">\n";4404my$alternate=1;4405for(my$i=$from;$i<=$to;$i++) {4406my$entry=$taglist->[$i];4407my%tag=%$entry;4408my$comment=$tag{'subject'};4409my$comment_short;4410if(defined$comment) {4411$comment_short= chop_str($comment,30,5);4412}4413if($alternate) {4414print"<tr class=\"dark\">\n";4415}else{4416print"<tr class=\"light\">\n";4417}4418$alternate^=1;4419if(defined$tag{'age'}) {4420print"<td><i>$tag{'age'}</i></td>\n";4421}else{4422print"<td></td>\n";4423}4424print"<td>".4425$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4426-class=>"list name"}, esc_html($tag{'name'})) .4427"</td>\n".4428"<td>";4429if(defined$comment) {4430print format_subject_html($comment,$comment_short,4431 href(action=>"tag", hash=>$tag{'id'}));4432}4433print"</td>\n".4434"<td class=\"selflink\">";4435if($tag{'type'}eq"tag") {4436print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4437}else{4438print" ";4439}4440print"</td>\n".4441"<td class=\"link\">"." | ".4442$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4443if($tag{'reftype'}eq"commit") {4444print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4445" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4446}elsif($tag{'reftype'}eq"blob") {4447print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4448}4449print"</td>\n".4450"</tr>";4451}4452if(defined$extra) {4453print"<tr>\n".4454"<td colspan=\"5\">$extra</td>\n".4455"</tr>\n";4456}4457print"</table>\n";4458}44594460sub git_heads_body {4461# uses global variable $project4462my($headlist,$head,$from,$to,$extra) =@_;4463$from=0unlessdefined$from;4464$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);44654466print"<table class=\"heads\">\n";4467my$alternate=1;4468for(my$i=$from;$i<=$to;$i++) {4469my$entry=$headlist->[$i];4470my%ref=%$entry;4471my$curr=$ref{'id'}eq$head;4472if($alternate) {4473print"<tr class=\"dark\">\n";4474}else{4475print"<tr class=\"light\">\n";4476}4477$alternate^=1;4478print"<td><i>$ref{'age'}</i></td>\n".4479($curr?"<td class=\"current_head\">":"<td>") .4480$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4481-class=>"list name"},esc_html($ref{'name'})) .4482"</td>\n".4483"<td class=\"link\">".4484$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4485$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4486$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4487"</td>\n".4488"</tr>";4489}4490if(defined$extra) {4491print"<tr>\n".4492"<td colspan=\"3\">$extra</td>\n".4493"</tr>\n";4494}4495print"</table>\n";4496}44974498sub git_search_grep_body {4499my($commitlist,$from,$to,$extra) =@_;4500$from=0unlessdefined$from;4501$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45024503print"<table class=\"commit_search\">\n";4504my$alternate=1;4505for(my$i=$from;$i<=$to;$i++) {4506my%co= %{$commitlist->[$i]};4507if(!%co) {4508next;4509}4510my$commit=$co{'id'};4511if($alternate) {4512print"<tr class=\"dark\">\n";4513}else{4514print"<tr class=\"light\">\n";4515}4516$alternate^=1;4517print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4518 format_author_html('td', \%co,15,5) .4519"<td>".4520$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4521-class=>"list subject"},4522 chop_and_escape_str($co{'title'},50) ."<br/>");4523my$comment=$co{'comment'};4524foreachmy$line(@$comment) {4525if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4526my($lead,$match,$trail) = ($1,$2,$3);4527$match= chop_str($match,70,5,'center');4528my$contextlen=int((80-length($match))/2);4529$contextlen=30if($contextlen>30);4530$lead= chop_str($lead,$contextlen,10,'left');4531$trail= chop_str($trail,$contextlen,10,'right');45324533$lead= esc_html($lead);4534$match= esc_html($match);4535$trail= esc_html($trail);45364537print"$lead<span class=\"match\">$match</span>$trail<br />";4538}4539}4540print"</td>\n".4541"<td class=\"link\">".4542$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4543" | ".4544$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4545" | ".4546$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4547print"</td>\n".4548"</tr>\n";4549}4550if(defined$extra) {4551print"<tr>\n".4552"<td colspan=\"3\">$extra</td>\n".4553"</tr>\n";4554}4555print"</table>\n";4556}45574558## ======================================================================4559## ======================================================================4560## actions45614562sub git_project_list {4563my$order=$input_params{'order'};4564if(defined$order&&$order!~m/none|project|descr|owner|age/) {4565 die_error(400,"Unknown order parameter");4566}45674568my@list= git_get_projects_list();4569if(!@list) {4570 die_error(404,"No projects found");4571}45724573 git_header_html();4574if(-f $home_text) {4575print"<div class=\"index_include\">\n";4576 insert_file($home_text);4577print"</div>\n";4578}4579print$cgi->startform(-method=>"get") .4580"<p class=\"projsearch\">Search:\n".4581$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4582"</p>".4583$cgi->end_form() ."\n";4584 git_project_list_body(\@list,$order);4585 git_footer_html();4586}45874588sub git_forks {4589my$order=$input_params{'order'};4590if(defined$order&&$order!~m/none|project|descr|owner|age/) {4591 die_error(400,"Unknown order parameter");4592}45934594my@list= git_get_projects_list($project);4595if(!@list) {4596 die_error(404,"No forks found");4597}45984599 git_header_html();4600 git_print_page_nav('','');4601 git_print_header_div('summary',"$projectforks");4602 git_project_list_body(\@list,$order);4603 git_footer_html();4604}46054606sub git_project_index {4607my@projects= git_get_projects_list($project);46084609print$cgi->header(4610-type =>'text/plain',4611-charset =>'utf-8',4612-content_disposition =>'inline; filename="index.aux"');46134614foreachmy$pr(@projects) {4615if(!exists$pr->{'owner'}) {4616$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4617}46184619my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4620# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4621$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4622$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4623$path=~s/ /\+/g;4624$owner=~s/ /\+/g;46254626print"$path$owner\n";4627}4628}46294630sub git_summary {4631my$descr= git_get_project_description($project) ||"none";4632my%co= parse_commit("HEAD");4633my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4634my$head=$co{'id'};46354636my$owner= git_get_project_owner($project);46374638my$refs= git_get_references();4639# These get_*_list functions return one more to allow us to see if4640# there are more ...4641my@taglist= git_get_tags_list(16);4642my@headlist= git_get_heads_list(16);4643my@forklist;4644my$check_forks= gitweb_check_feature('forks');46454646if($check_forks) {4647@forklist= git_get_projects_list($project);4648}46494650 git_header_html();4651 git_print_page_nav('summary','',$head);46524653print"<div class=\"title\"> </div>\n";4654print"<table class=\"projects_list\">\n".4655"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4656"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4657if(defined$cd{'rfc2822'}) {4658print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4659}46604661# use per project git URL list in $projectroot/$project/cloneurl4662# or make project git URL from git base URL and project name4663my$url_tag="URL";4664my@url_list= git_get_project_url_list($project);4665@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4666foreachmy$git_url(@url_list) {4667next unless$git_url;4668print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4669$url_tag="";4670}46714672# Tag cloud4673my$show_ctags= gitweb_check_feature('ctags');4674if($show_ctags) {4675my$ctags= git_get_project_ctags($project);4676my$cloud= git_populate_project_tagcloud($ctags);4677print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4678print"</td>\n<td>"unless%$ctags;4679print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4680print"</td>\n<td>"if%$ctags;4681print git_show_project_tagcloud($cloud,48);4682print"</td></tr>";4683}46844685print"</table>\n";46864687# If XSS prevention is on, we don't include README.html.4688# TODO: Allow a readme in some safe format.4689if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4690print"<div class=\"title\">readme</div>\n".4691"<div class=\"readme\">\n";4692 insert_file("$projectroot/$project/README.html");4693print"\n</div>\n";# class="readme"4694}46954696# we need to request one more than 16 (0..15) to check if4697# those 16 are all4698my@commitlist=$head? parse_commits($head,17) : ();4699if(@commitlist) {4700 git_print_header_div('shortlog');4701 git_shortlog_body(\@commitlist,0,15,$refs,4702$#commitlist<=15?undef:4703$cgi->a({-href => href(action=>"shortlog")},"..."));4704}47054706if(@taglist) {4707 git_print_header_div('tags');4708 git_tags_body(\@taglist,0,15,4709$#taglist<=15?undef:4710$cgi->a({-href => href(action=>"tags")},"..."));4711}47124713if(@headlist) {4714 git_print_header_div('heads');4715 git_heads_body(\@headlist,$head,0,15,4716$#headlist<=15?undef:4717$cgi->a({-href => href(action=>"heads")},"..."));4718}47194720if(@forklist) {4721 git_print_header_div('forks');4722 git_project_list_body(\@forklist,'age',0,15,4723$#forklist<=15?undef:4724$cgi->a({-href => href(action=>"forks")},"..."),4725'no_header');4726}47274728 git_footer_html();4729}47304731sub git_tag {4732my$head= git_get_head_hash($project);4733 git_header_html();4734 git_print_page_nav('','',$head,undef,$head);4735my%tag= parse_tag($hash);47364737if(!%tag) {4738 die_error(404,"Unknown tag object");4739}47404741 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4742print"<div class=\"title_text\">\n".4743"<table class=\"object_header\">\n".4744"<tr>\n".4745"<td>object</td>\n".4746"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4747$tag{'object'}) ."</td>\n".4748"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4749$tag{'type'}) ."</td>\n".4750"</tr>\n";4751if(defined($tag{'author'})) {4752 git_print_authorship_rows(\%tag,'author');4753}4754print"</table>\n\n".4755"</div>\n";4756print"<div class=\"page_body\">";4757my$comment=$tag{'comment'};4758foreachmy$line(@$comment) {4759chomp$line;4760print esc_html($line, -nbsp=>1) ."<br/>\n";4761}4762print"</div>\n";4763 git_footer_html();4764}47654766sub git_blame {4767# permissions4768 gitweb_check_feature('blame')4769or die_error(403,"Blame view not allowed");47704771# error checking4772 die_error(400,"No file name given")unless$file_name;4773$hash_base||= git_get_head_hash($project);4774 die_error(404,"Couldn't find base commit")unless$hash_base;4775my%co= parse_commit($hash_base)4776or die_error(404,"Commit not found");4777my$ftype="blob";4778if(!defined$hash) {4779$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4780or die_error(404,"Error looking up file");4781}else{4782$ftype= git_get_type($hash);4783if($ftype!~"blob") {4784 die_error(400,"Object is not a blob");4785}4786}47874788# run git-blame --porcelain4789open my$fd,"-|", git_cmd(),"blame",'-p',4790$hash_base,'--',$file_name4791or die_error(500,"Open git-blame failed");47924793# page header4794 git_header_html();4795my$formats_nav=4796$cgi->a({-href => href(action=>"blob", -replay=>1)},4797"blob") .4798" | ".4799$cgi->a({-href => href(action=>"history", -replay=>1)},4800"history") .4801" | ".4802$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4803"HEAD");4804 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4805 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4806 git_print_page_path($file_name,$ftype,$hash_base);48074808# page body4809my@rev_color=qw(light2 dark2);4810my$num_colors=scalar(@rev_color);4811my$current_color=0;4812my%metainfo= ();48134814print<<HTML;4815<div class="page_body">4816<table class="blame">4817<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4818HTML4819 LINE:4820while(my$line= <$fd>) {4821chomp$line;4822# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4823# no <lines in group> for subsequent lines in group of lines4824my($full_rev,$orig_lineno,$lineno,$group_size) =4825($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4826if(!exists$metainfo{$full_rev}) {4827$metainfo{$full_rev} = {};4828}4829my$meta=$metainfo{$full_rev};4830my$data;4831while($data= <$fd>) {4832chomp$data;4833last if($data=~s/^\t//);# contents of line4834if($data=~/^(\S+) (.*)$/) {4835$meta->{$1} =$2;4836}4837}4838my$short_rev=substr($full_rev,0,8);4839my$author=$meta->{'author'};4840my%date=4841 parse_date($meta->{'author-time'},$meta->{'author-tz'});4842my$date=$date{'iso-tz'};4843if($group_size) {4844$current_color= ($current_color+1) %$num_colors;4845}4846print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4847if($group_size) {4848print"<td class=\"sha1\"";4849print" title=\"". esc_html($author) .",$date\"";4850print" rowspan=\"$group_size\""if($group_size>1);4851print">";4852print$cgi->a({-href => href(action=>"commit",4853 hash=>$full_rev,4854 file_name=>$file_name)},4855 esc_html($short_rev));4856print"</td>\n";4857}4858my$parent_commit;4859if(!exists$meta->{'parent'}) {4860open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4861or die_error(500,"Open git-rev-parse failed");4862$parent_commit= <$dd>;4863close$dd;4864chomp($parent_commit);4865$meta->{'parent'} =$parent_commit;4866}else{4867$parent_commit=$meta->{'parent'};4868}4869my$blamed= href(action =>'blame',4870 file_name =>$meta->{'filename'},4871 hash_base =>$parent_commit);4872print"<td class=\"linenr\">";4873print$cgi->a({ -href =>"$blamed#l$orig_lineno",4874-class=>"linenr"},4875 esc_html($lineno));4876print"</td>";4877print"<td class=\"pre\">". esc_html($data) ."</td>\n";4878print"</tr>\n";4879}4880print"</table>\n";4881print"</div>";4882close$fd4883or print"Reading blob failed\n";48844885# page footer4886 git_footer_html();4887}48884889sub git_tags {4890my$head= git_get_head_hash($project);4891 git_header_html();4892 git_print_page_nav('','',$head,undef,$head);4893 git_print_header_div('summary',$project);48944895my@tagslist= git_get_tags_list();4896if(@tagslist) {4897 git_tags_body(\@tagslist);4898}4899 git_footer_html();4900}49014902sub git_heads {4903my$head= git_get_head_hash($project);4904 git_header_html();4905 git_print_page_nav('','',$head,undef,$head);4906 git_print_header_div('summary',$project);49074908my@headslist= git_get_heads_list();4909if(@headslist) {4910 git_heads_body(\@headslist,$head);4911}4912 git_footer_html();4913}49144915sub git_blob_plain {4916my$type=shift;4917my$expires;49184919if(!defined$hash) {4920if(defined$file_name) {4921my$base=$hash_base|| git_get_head_hash($project);4922$hash= git_get_hash_by_path($base,$file_name,"blob")4923or die_error(404,"Cannot find file");4924}else{4925 die_error(400,"No file name defined");4926}4927}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4928# blobs defined by non-textual hash id's can be cached4929$expires="+1d";4930}49314932open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4933or die_error(500,"Open git-cat-file blob '$hash' failed");49344935# content-type (can include charset)4936$type= blob_contenttype($fd,$file_name,$type);49374938# "save as" filename, even when no $file_name is given4939my$save_as="$hash";4940if(defined$file_name) {4941$save_as=$file_name;4942}elsif($type=~m/^text\//) {4943$save_as.='.txt';4944}49454946# With XSS prevention on, blobs of all types except a few known safe4947# ones are served with "Content-Disposition: attachment" to make sure4948# they don't run in our security domain. For certain image types,4949# blob view writes an <img> tag referring to blob_plain view, and we4950# want to be sure not to break that by serving the image as an4951# attachment (though Firefox 3 doesn't seem to care).4952my$sandbox=$prevent_xss&&4953$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;49544955print$cgi->header(4956-type =>$type,4957-expires =>$expires,4958-content_disposition =>4959($sandbox?'attachment':'inline')4960.'; filename="'.$save_as.'"');4961local$/=undef;4962binmode STDOUT,':raw';4963print<$fd>;4964binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4965close$fd;4966}49674968sub git_blob {4969my$expires;49704971if(!defined$hash) {4972if(defined$file_name) {4973my$base=$hash_base|| git_get_head_hash($project);4974$hash= git_get_hash_by_path($base,$file_name,"blob")4975or die_error(404,"Cannot find file");4976}else{4977 die_error(400,"No file name defined");4978}4979}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4980# blobs defined by non-textual hash id's can be cached4981$expires="+1d";4982}49834984my$have_blame= gitweb_check_feature('blame');4985open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4986or die_error(500,"Couldn't cat$file_name,$hash");4987my$mimetype= blob_mimetype($fd,$file_name);4988if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4989close$fd;4990return git_blob_plain($mimetype);4991}4992# we can have blame only for text/* mimetype4993$have_blame&&= ($mimetype=~m!^text/!);49944995 git_header_html(undef,$expires);4996my$formats_nav='';4997if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4998if(defined$file_name) {4999if($have_blame) {5000$formats_nav.=5001$cgi->a({-href => href(action=>"blame", -replay=>1)},5002"blame") .5003" | ";5004}5005$formats_nav.=5006$cgi->a({-href => href(action=>"history", -replay=>1)},5007"history") .5008" | ".5009$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5010"raw") .5011" | ".5012$cgi->a({-href => href(action=>"blob",5013 hash_base=>"HEAD", file_name=>$file_name)},5014"HEAD");5015}else{5016$formats_nav.=5017$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5018"raw");5019}5020 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5021 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5022}else{5023print"<div class=\"page_nav\">\n".5024"<br/><br/></div>\n".5025"<div class=\"title\">$hash</div>\n";5026}5027 git_print_page_path($file_name,"blob",$hash_base);5028print"<div class=\"page_body\">\n";5029if($mimetype=~m!^image/!) {5030print qq!<img type="$mimetype"!;5031if($file_name) {5032print qq! alt="$file_name" title="$file_name"!;5033}5034print qq! src="! .5035 href(action=>"blob_plain", hash=>$hash,5036 hash_base=>$hash_base, file_name=>$file_name) .5037 qq!"/>\n!;5038}else{5039my$nr;5040while(my$line= <$fd>) {5041chomp$line;5042$nr++;5043$line= untabify($line);5044printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5045$nr,$nr,$nr, esc_html($line, -nbsp=>1);5046}5047}5048close$fd5049or print"Reading blob failed.\n";5050print"</div>";5051 git_footer_html();5052}50535054sub git_tree {5055if(!defined$hash_base) {5056$hash_base="HEAD";5057}5058if(!defined$hash) {5059if(defined$file_name) {5060$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5061}else{5062$hash=$hash_base;5063}5064}5065 die_error(404,"No such tree")unlessdefined($hash);50665067my@entries= ();5068{5069local$/="\0";5070open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash5071or die_error(500,"Open git-ls-tree failed");5072@entries=map{chomp;$_} <$fd>;5073close$fd5074or die_error(404,"Reading tree failed");5075}50765077my$refs= git_get_references();5078my$ref= format_ref_marker($refs,$hash_base);5079 git_header_html();5080my$basedir='';5081my$have_blame= gitweb_check_feature('blame');5082if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5083my@views_nav= ();5084if(defined$file_name) {5085push@views_nav,5086$cgi->a({-href => href(action=>"history", -replay=>1)},5087"history"),5088$cgi->a({-href => href(action=>"tree",5089 hash_base=>"HEAD", file_name=>$file_name)},5090"HEAD"),5091}5092my$snapshot_links= format_snapshot_links($hash);5093if(defined$snapshot_links) {5094# FIXME: Should be available when we have no hash base as well.5095push@views_nav,$snapshot_links;5096}5097 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));5098 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5099}else{5100undef$hash_base;5101print"<div class=\"page_nav\">\n";5102print"<br/><br/></div>\n";5103print"<div class=\"title\">$hash</div>\n";5104}5105if(defined$file_name) {5106$basedir=$file_name;5107if($basedirne''&&substr($basedir, -1)ne'/') {5108$basedir.='/';5109}5110 git_print_page_path($file_name,'tree',$hash_base);5111}5112print"<div class=\"page_body\">\n";5113print"<table class=\"tree\">\n";5114my$alternate=1;5115# '..' (top directory) link if possible5116if(defined$hash_base&&5117defined$file_name&&$file_name=~m![^/]+$!) {5118if($alternate) {5119print"<tr class=\"dark\">\n";5120}else{5121print"<tr class=\"light\">\n";5122}5123$alternate^=1;51245125my$up=$file_name;5126$up=~s!/?[^/]+$!!;5127undef$upunless$up;5128# based on git_print_tree_entry5129print'<td class="mode">'. mode_str('040000') ."</td>\n";5130print'<td class="list">';5131print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,5132 file_name=>$up)},5133"..");5134print"</td>\n";5135print"<td class=\"link\"></td>\n";51365137print"</tr>\n";5138}5139foreachmy$line(@entries) {5140my%t= parse_ls_tree_line($line, -z =>1);51415142if($alternate) {5143print"<tr class=\"dark\">\n";5144}else{5145print"<tr class=\"light\">\n";5146}5147$alternate^=1;51485149 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);51505151print"</tr>\n";5152}5153print"</table>\n".5154"</div>";5155 git_footer_html();5156}51575158sub git_snapshot {5159my$format=$input_params{'snapshot_format'};5160if(!@snapshot_fmts) {5161 die_error(403,"Snapshots not allowed");5162}5163# default to first supported snapshot format5164$format||=$snapshot_fmts[0];5165if($format!~m/^[a-z0-9]+$/) {5166 die_error(400,"Invalid snapshot format parameter");5167}elsif(!exists($known_snapshot_formats{$format})) {5168 die_error(400,"Unknown snapshot format");5169}elsif(!grep($_eq$format,@snapshot_fmts)) {5170 die_error(403,"Unsupported snapshot format");5171}elsif($known_snapshot_formats{$format}{'disabled'}) {5172 die_error(403,"Snapshot format not allowed");5173}51745175if(!defined$hash) {5176$hash= git_get_head_hash($project);5177}51785179my$name=$project;5180$name=~ s,([^/])/*\.git$,$1,;5181$name= basename($name);5182my$filename= to_utf8($name);5183$name=~s/\047/\047\\\047\047/g;5184my$cmd;5185$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5186$cmd= quote_command(5187 git_cmd(),'archive',5188"--format=$known_snapshot_formats{$format}{'format'}",5189"--prefix=$name/",$hash);5190if(exists$known_snapshot_formats{$format}{'compressor'}) {5191$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5192}51935194print$cgi->header(5195-type =>$known_snapshot_formats{$format}{'type'},5196-content_disposition =>'inline; filename="'."$filename".'"',5197-status =>'200 OK');51985199open my$fd,"-|",$cmd5200or die_error(500,"Execute git-archive failed");5201binmode STDOUT,':raw';5202print<$fd>;5203binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5204close$fd;5205}52065207sub git_log {5208my$head= git_get_head_hash($project);5209if(!defined$hash) {5210$hash=$head;5211}5212if(!defined$page) {5213$page=0;5214}5215my$refs= git_get_references();52165217my@commitlist= parse_commits($hash,101, (100*$page));52185219my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);52205221my($patch_max) = gitweb_get_feature('patches');5222if($patch_max) {5223if($patch_max<0||@commitlist<=$patch_max) {5224$paging_nav.=" ⋅ ".5225$cgi->a({-href => href(action=>"patches", -replay=>1)},5226"patches");5227}5228}52295230 git_header_html();5231 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);52325233if(!@commitlist) {5234my%co= parse_commit($hash);52355236 git_print_header_div('summary',$project);5237print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5238}5239my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5240for(my$i=0;$i<=$to;$i++) {5241my%co= %{$commitlist[$i]};5242next if!%co;5243my$commit=$co{'id'};5244my$ref= format_ref_marker($refs,$commit);5245my%ad= parse_date($co{'author_epoch'});5246 git_print_header_div('commit',5247"<span class=\"age\">$co{'age_string'}</span>".5248 esc_html($co{'title'}) .$ref,5249$commit);5250print"<div class=\"title_text\">\n".5251"<div class=\"log_link\">\n".5252$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5253" | ".5254$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5255" | ".5256$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5257"<br/>\n".5258"</div>\n";5259 git_print_authorship(\%co, -tag =>'span');5260print"<br/>\n</div>\n";52615262print"<div class=\"log_body\">\n";5263 git_print_log($co{'comment'}, -final_empty_line=>1);5264print"</div>\n";5265}5266if($#commitlist>=100) {5267print"<div class=\"page_nav\">\n";5268print$cgi->a({-href => href(-replay=>1, page=>$page+1),5269-accesskey =>"n", -title =>"Alt-n"},"next");5270print"</div>\n";5271}5272 git_footer_html();5273}52745275sub git_commit {5276$hash||=$hash_base||"HEAD";5277my%co= parse_commit($hash)5278or die_error(404,"Unknown commit object");52795280my$parent=$co{'parent'};5281my$parents=$co{'parents'};# listref52825283# we need to prepare $formats_nav before any parameter munging5284my$formats_nav;5285if(!defined$parent) {5286# --root commitdiff5287$formats_nav.='(initial)';5288}elsif(@$parents==1) {5289# single parent commit5290$formats_nav.=5291'(parent: '.5292$cgi->a({-href => href(action=>"commit",5293 hash=>$parent)},5294 esc_html(substr($parent,0,7))) .5295')';5296}else{5297# merge commit5298$formats_nav.=5299'(merge: '.5300join(' ',map{5301$cgi->a({-href => href(action=>"commit",5302 hash=>$_)},5303 esc_html(substr($_,0,7)));5304}@$parents) .5305')';5306}5307if(gitweb_check_feature('patches')) {5308$formats_nav.=" | ".5309$cgi->a({-href => href(action=>"patch", -replay=>1)},5310"patch");5311}53125313if(!defined$parent) {5314$parent="--root";5315}5316my@difftree;5317open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5318@diff_opts,5319(@$parents<=1?$parent:'-c'),5320$hash,"--"5321or die_error(500,"Open git-diff-tree failed");5322@difftree=map{chomp;$_} <$fd>;5323close$fdor die_error(404,"Reading git-diff-tree failed");53245325# non-textual hash id's can be cached5326my$expires;5327if($hash=~m/^[0-9a-fA-F]{40}$/) {5328$expires="+1d";5329}5330my$refs= git_get_references();5331my$ref= format_ref_marker($refs,$co{'id'});53325333 git_header_html(undef,$expires);5334 git_print_page_nav('commit','',5335$hash,$co{'tree'},$hash,5336$formats_nav);53375338if(defined$co{'parent'}) {5339 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5340}else{5341 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5342}5343print"<div class=\"title_text\">\n".5344"<table class=\"object_header\">\n";5345 git_print_authorship_rows(\%co);5346print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5347print"<tr>".5348"<td>tree</td>".5349"<td class=\"sha1\">".5350$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5351class=>"list"},$co{'tree'}) .5352"</td>".5353"<td class=\"link\">".5354$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5355"tree");5356my$snapshot_links= format_snapshot_links($hash);5357if(defined$snapshot_links) {5358print" | ".$snapshot_links;5359}5360print"</td>".5361"</tr>\n";53625363foreachmy$par(@$parents) {5364print"<tr>".5365"<td>parent</td>".5366"<td class=\"sha1\">".5367$cgi->a({-href => href(action=>"commit", hash=>$par),5368class=>"list"},$par) .5369"</td>".5370"<td class=\"link\">".5371$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5372" | ".5373$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5374"</td>".5375"</tr>\n";5376}5377print"</table>".5378"</div>\n";53795380print"<div class=\"page_body\">\n";5381 git_print_log($co{'comment'});5382print"</div>\n";53835384 git_difftree_body(\@difftree,$hash,@$parents);53855386 git_footer_html();5387}53885389sub git_object {5390# object is defined by:5391# - hash or hash_base alone5392# - hash_base and file_name5393my$type;53945395# - hash or hash_base alone5396if($hash|| ($hash_base&& !defined$file_name)) {5397my$object_id=$hash||$hash_base;53985399open my$fd,"-|", quote_command(5400 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5401or die_error(404,"Object does not exist");5402$type= <$fd>;5403chomp$type;5404close$fd5405or die_error(404,"Object does not exist");54065407# - hash_base and file_name5408}elsif($hash_base&&defined$file_name) {5409$file_name=~ s,/+$,,;54105411system(git_cmd(),"cat-file",'-e',$hash_base) ==05412or die_error(404,"Base object does not exist");54135414# here errors should not hapen5415open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5416or die_error(500,"Open git-ls-tree failed");5417my$line= <$fd>;5418close$fd;54195420#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5421unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5422 die_error(404,"File or directory for given base does not exist");5423}5424$type=$2;5425$hash=$3;5426}else{5427 die_error(400,"Not enough information to find object");5428}54295430print$cgi->redirect(-uri => href(action=>$type, -full=>1,5431 hash=>$hash, hash_base=>$hash_base,5432 file_name=>$file_name),5433-status =>'302 Found');5434}54355436sub git_blobdiff {5437my$format=shift||'html';54385439my$fd;5440my@difftree;5441my%diffinfo;5442my$expires;54435444# preparing $fd and %diffinfo for git_patchset_body5445# new style URI5446if(defined$hash_base&&defined$hash_parent_base) {5447if(defined$file_name) {5448# read raw output5449open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5450$hash_parent_base,$hash_base,5451"--", (defined$file_parent?$file_parent: ()),$file_name5452or die_error(500,"Open git-diff-tree failed");5453@difftree=map{chomp;$_} <$fd>;5454close$fd5455or die_error(404,"Reading git-diff-tree failed");5456@difftree5457or die_error(404,"Blob diff not found");54585459}elsif(defined$hash&&5460$hash=~/[0-9a-fA-F]{40}/) {5461# try to find filename from $hash54625463# read filtered raw output5464open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5465$hash_parent_base,$hash_base,"--"5466or die_error(500,"Open git-diff-tree failed");5467@difftree=5468# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5469# $hash == to_id5470grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5471map{chomp;$_} <$fd>;5472close$fd5473or die_error(404,"Reading git-diff-tree failed");5474@difftree5475or die_error(404,"Blob diff not found");54765477}else{5478 die_error(400,"Missing one of the blob diff parameters");5479}54805481if(@difftree>1) {5482 die_error(400,"Ambiguous blob diff specification");5483}54845485%diffinfo= parse_difftree_raw_line($difftree[0]);5486$file_parent||=$diffinfo{'from_file'} ||$file_name;5487$file_name||=$diffinfo{'to_file'};54885489$hash_parent||=$diffinfo{'from_id'};5490$hash||=$diffinfo{'to_id'};54915492# non-textual hash id's can be cached5493if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5494$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5495$expires='+1d';5496}54975498# open patch output5499open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5500'-p', ($formateq'html'?"--full-index": ()),5501$hash_parent_base,$hash_base,5502"--", (defined$file_parent?$file_parent: ()),$file_name5503or die_error(500,"Open git-diff-tree failed");5504}55055506# old/legacy style URI -- not generated anymore since 1.4.3.5507if(!%diffinfo) {5508 die_error('404 Not Found',"Missing one of the blob diff parameters")5509}55105511# header5512if($formateq'html') {5513my$formats_nav=5514$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5515"raw");5516 git_header_html(undef,$expires);5517if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5518 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5519 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5520}else{5521print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5522print"<div class=\"title\">$hashvs$hash_parent</div>\n";5523}5524if(defined$file_name) {5525 git_print_page_path($file_name,"blob",$hash_base);5526}else{5527print"<div class=\"page_path\"></div>\n";5528}55295530}elsif($formateq'plain') {5531print$cgi->header(5532-type =>'text/plain',5533-charset =>'utf-8',5534-expires =>$expires,5535-content_disposition =>'inline; filename="'."$file_name".'.patch"');55365537print"X-Git-Url: ".$cgi->self_url() ."\n\n";55385539}else{5540 die_error(400,"Unknown blobdiff format");5541}55425543# patch5544if($formateq'html') {5545print"<div class=\"page_body\">\n";55465547 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5548close$fd;55495550print"</div>\n";# class="page_body"5551 git_footer_html();55525553}else{5554while(my$line= <$fd>) {5555$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5556$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;55575558print$line;55595560last if$line=~m!^\+\+\+!;5561}5562local$/=undef;5563print<$fd>;5564close$fd;5565}5566}55675568sub git_blobdiff_plain {5569 git_blobdiff('plain');5570}55715572sub git_commitdiff {5573my%params=@_;5574my$format=$params{-format} ||'html';55755576my($patch_max) = gitweb_get_feature('patches');5577if($formateq'patch') {5578 die_error(403,"Patch view not allowed")unless$patch_max;5579}55805581$hash||=$hash_base||"HEAD";5582my%co= parse_commit($hash)5583or die_error(404,"Unknown commit object");55845585# choose format for commitdiff for merge5586if(!defined$hash_parent&& @{$co{'parents'}} >1) {5587$hash_parent='--cc';5588}5589# we need to prepare $formats_nav before almost any parameter munging5590my$formats_nav;5591if($formateq'html') {5592$formats_nav=5593$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5594"raw");5595if($patch_max) {5596$formats_nav.=" | ".5597$cgi->a({-href => href(action=>"patch", -replay=>1)},5598"patch");5599}56005601if(defined$hash_parent&&5602$hash_parentne'-c'&&$hash_parentne'--cc') {5603# commitdiff with two commits given5604my$hash_parent_short=$hash_parent;5605if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5606$hash_parent_short=substr($hash_parent,0,7);5607}5608$formats_nav.=5609' (from';5610for(my$i=0;$i< @{$co{'parents'}};$i++) {5611if($co{'parents'}[$i]eq$hash_parent) {5612$formats_nav.=' parent '. ($i+1);5613last;5614}5615}5616$formats_nav.=': '.5617$cgi->a({-href => href(action=>"commitdiff",5618 hash=>$hash_parent)},5619 esc_html($hash_parent_short)) .5620')';5621}elsif(!$co{'parent'}) {5622# --root commitdiff5623$formats_nav.=' (initial)';5624}elsif(scalar@{$co{'parents'}} ==1) {5625# single parent commit5626$formats_nav.=5627' (parent: '.5628$cgi->a({-href => href(action=>"commitdiff",5629 hash=>$co{'parent'})},5630 esc_html(substr($co{'parent'},0,7))) .5631')';5632}else{5633# merge commit5634if($hash_parenteq'--cc') {5635$formats_nav.=' | '.5636$cgi->a({-href => href(action=>"commitdiff",5637 hash=>$hash, hash_parent=>'-c')},5638'combined');5639}else{# $hash_parent eq '-c'5640$formats_nav.=' | '.5641$cgi->a({-href => href(action=>"commitdiff",5642 hash=>$hash, hash_parent=>'--cc')},5643'compact');5644}5645$formats_nav.=5646' (merge: '.5647join(' ',map{5648$cgi->a({-href => href(action=>"commitdiff",5649 hash=>$_)},5650 esc_html(substr($_,0,7)));5651} @{$co{'parents'}} ) .5652')';5653}5654}56555656my$hash_parent_param=$hash_parent;5657if(!defined$hash_parent_param) {5658# --cc for multiple parents, --root for parentless5659$hash_parent_param=5660@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5661}56625663# read commitdiff5664my$fd;5665my@difftree;5666if($formateq'html') {5667open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5668"--no-commit-id","--patch-with-raw","--full-index",5669$hash_parent_param,$hash,"--"5670or die_error(500,"Open git-diff-tree failed");56715672while(my$line= <$fd>) {5673chomp$line;5674# empty line ends raw part of diff-tree output5675last unless$line;5676push@difftree,scalar parse_difftree_raw_line($line);5677}56785679}elsif($formateq'plain') {5680open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5681'-p',$hash_parent_param,$hash,"--"5682or die_error(500,"Open git-diff-tree failed");5683}elsif($formateq'patch') {5684# For commit ranges, we limit the output to the number of5685# patches specified in the 'patches' feature.5686# For single commits, we limit the output to a single patch,5687# diverging from the git-format-patch default.5688my@commit_spec= ();5689if($hash_parent) {5690if($patch_max>0) {5691push@commit_spec,"-$patch_max";5692}5693push@commit_spec,'-n',"$hash_parent..$hash";5694}else{5695if($params{-single}) {5696push@commit_spec,'-1';5697}else{5698if($patch_max>0) {5699push@commit_spec,"-$patch_max";5700}5701push@commit_spec,"-n";5702}5703push@commit_spec,'--root',$hash;5704}5705open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5706'--stdout',@commit_spec5707or die_error(500,"Open git-format-patch failed");5708}else{5709 die_error(400,"Unknown commitdiff format");5710}57115712# non-textual hash id's can be cached5713my$expires;5714if($hash=~m/^[0-9a-fA-F]{40}$/) {5715$expires="+1d";5716}57175718# write commit message5719if($formateq'html') {5720my$refs= git_get_references();5721my$ref= format_ref_marker($refs,$co{'id'});57225723 git_header_html(undef,$expires);5724 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5725 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5726print"<div class=\"title_text\">\n".5727"<table class=\"object_header\">\n";5728 git_print_authorship_rows(\%co);5729print"</table>".5730"</div>\n";5731print"<div class=\"page_body\">\n";5732if(@{$co{'comment'}} >1) {5733print"<div class=\"log\">\n";5734 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5735print"</div>\n";# class="log"5736}57375738}elsif($formateq'plain') {5739my$refs= git_get_references("tags");5740my$tagname= git_get_rev_name_tags($hash);5741my$filename= basename($project) ."-$hash.patch";57425743print$cgi->header(5744-type =>'text/plain',5745-charset =>'utf-8',5746-expires =>$expires,5747-content_disposition =>'inline; filename="'."$filename".'"');5748my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5749print"From: ". to_utf8($co{'author'}) ."\n";5750print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5751print"Subject: ". to_utf8($co{'title'}) ."\n";57525753print"X-Git-Tag:$tagname\n"if$tagname;5754print"X-Git-Url: ".$cgi->self_url() ."\n\n";57555756foreachmy$line(@{$co{'comment'}}) {5757print to_utf8($line) ."\n";5758}5759print"---\n\n";5760}elsif($formateq'patch') {5761my$filename= basename($project) ."-$hash.patch";57625763print$cgi->header(5764-type =>'text/plain',5765-charset =>'utf-8',5766-expires =>$expires,5767-content_disposition =>'inline; filename="'."$filename".'"');5768}57695770# write patch5771if($formateq'html') {5772my$use_parents= !defined$hash_parent||5773$hash_parenteq'-c'||$hash_parenteq'--cc';5774 git_difftree_body(\@difftree,$hash,5775$use_parents? @{$co{'parents'}} :$hash_parent);5776print"<br/>\n";57775778 git_patchset_body($fd, \@difftree,$hash,5779$use_parents? @{$co{'parents'}} :$hash_parent);5780close$fd;5781print"</div>\n";# class="page_body"5782 git_footer_html();57835784}elsif($formateq'plain') {5785local$/=undef;5786print<$fd>;5787close$fd5788or print"Reading git-diff-tree failed\n";5789}elsif($formateq'patch') {5790local$/=undef;5791print<$fd>;5792close$fd5793or print"Reading git-format-patch failed\n";5794}5795}57965797sub git_commitdiff_plain {5798 git_commitdiff(-format =>'plain');5799}58005801# format-patch-style patches5802sub git_patch {5803 git_commitdiff(-format =>'patch', -single=>1);5804}58055806sub git_patches {5807 git_commitdiff(-format =>'patch');5808}58095810sub git_history {5811if(!defined$hash_base) {5812$hash_base= git_get_head_hash($project);5813}5814if(!defined$page) {5815$page=0;5816}5817my$ftype;5818my%co= parse_commit($hash_base)5819or die_error(404,"Unknown commit object");58205821my$refs= git_get_references();5822my$limit=sprintf("--max-count=%i", (100* ($page+1)));58235824my@commitlist= parse_commits($hash_base,101, (100*$page),5825$file_name,"--full-history")5826or die_error(404,"No such file or directory on given branch");58275828if(!defined$hash&&defined$file_name) {5829# some commits could have deleted file in question,5830# and not have it in tree, but one of them has to have it5831for(my$i=0;$i<=@commitlist;$i++) {5832$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5833last ifdefined$hash;5834}5835}5836if(defined$hash) {5837$ftype= git_get_type($hash);5838}5839if(!defined$ftype) {5840 die_error(500,"Unknown type of object");5841}58425843my$paging_nav='';5844if($page>0) {5845$paging_nav.=5846$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5847 file_name=>$file_name)},5848"first");5849$paging_nav.=" ⋅ ".5850$cgi->a({-href => href(-replay=>1, page=>$page-1),5851-accesskey =>"p", -title =>"Alt-p"},"prev");5852}else{5853$paging_nav.="first";5854$paging_nav.=" ⋅ prev";5855}5856my$next_link='';5857if($#commitlist>=100) {5858$next_link=5859$cgi->a({-href => href(-replay=>1, page=>$page+1),5860-accesskey =>"n", -title =>"Alt-n"},"next");5861$paging_nav.=" ⋅$next_link";5862}else{5863$paging_nav.=" ⋅ next";5864}58655866 git_header_html();5867 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5868 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5869 git_print_page_path($file_name,$ftype,$hash_base);58705871 git_history_body(\@commitlist,0,99,5872$refs,$hash_base,$ftype,$next_link);58735874 git_footer_html();5875}58765877sub git_search {5878 gitweb_check_feature('search')or die_error(403,"Search is disabled");5879if(!defined$searchtext) {5880 die_error(400,"Text field is empty");5881}5882if(!defined$hash) {5883$hash= git_get_head_hash($project);5884}5885my%co= parse_commit($hash);5886if(!%co) {5887 die_error(404,"Unknown commit object");5888}5889if(!defined$page) {5890$page=0;5891}58925893$searchtype||='commit';5894if($searchtypeeq'pickaxe') {5895# pickaxe may take all resources of your box and run for several minutes5896# with every query - so decide by yourself how public you make this feature5897 gitweb_check_feature('pickaxe')5898or die_error(403,"Pickaxe is disabled");5899}5900if($searchtypeeq'grep') {5901 gitweb_check_feature('grep')5902or die_error(403,"Grep is disabled");5903}59045905 git_header_html();59065907if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5908my$greptype;5909if($searchtypeeq'commit') {5910$greptype="--grep=";5911}elsif($searchtypeeq'author') {5912$greptype="--author=";5913}elsif($searchtypeeq'committer') {5914$greptype="--committer=";5915}5916$greptype.=$searchtext;5917my@commitlist= parse_commits($hash,101, (100*$page),undef,5918$greptype,'--regexp-ignore-case',5919$search_use_regexp?'--extended-regexp':'--fixed-strings');59205921my$paging_nav='';5922if($page>0) {5923$paging_nav.=5924$cgi->a({-href => href(action=>"search", hash=>$hash,5925 searchtext=>$searchtext,5926 searchtype=>$searchtype)},5927"first");5928$paging_nav.=" ⋅ ".5929$cgi->a({-href => href(-replay=>1, page=>$page-1),5930-accesskey =>"p", -title =>"Alt-p"},"prev");5931}else{5932$paging_nav.="first";5933$paging_nav.=" ⋅ prev";5934}5935my$next_link='';5936if($#commitlist>=100) {5937$next_link=5938$cgi->a({-href => href(-replay=>1, page=>$page+1),5939-accesskey =>"n", -title =>"Alt-n"},"next");5940$paging_nav.=" ⋅$next_link";5941}else{5942$paging_nav.=" ⋅ next";5943}59445945if($#commitlist>=100) {5946}59475948 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5949 git_print_header_div('commit', esc_html($co{'title'}),$hash);5950 git_search_grep_body(\@commitlist,0,99,$next_link);5951}59525953if($searchtypeeq'pickaxe') {5954 git_print_page_nav('','',$hash,$co{'tree'},$hash);5955 git_print_header_div('commit', esc_html($co{'title'}),$hash);59565957print"<table class=\"pickaxe search\">\n";5958my$alternate=1;5959local$/="\n";5960open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5961'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5962($search_use_regexp?'--pickaxe-regex': ());5963undef%co;5964my@files;5965while(my$line= <$fd>) {5966chomp$line;5967next unless$line;59685969my%set= parse_difftree_raw_line($line);5970if(defined$set{'commit'}) {5971# finish previous commit5972if(%co) {5973print"</td>\n".5974"<td class=\"link\">".5975$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5976" | ".5977$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5978print"</td>\n".5979"</tr>\n";5980}59815982if($alternate) {5983print"<tr class=\"dark\">\n";5984}else{5985print"<tr class=\"light\">\n";5986}5987$alternate^=1;5988%co= parse_commit($set{'commit'});5989my$author= chop_and_escape_str($co{'author_name'},15,5);5990print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5991"<td><i>$author</i></td>\n".5992"<td>".5993$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5994-class=>"list subject"},5995 chop_and_escape_str($co{'title'},50) ."<br/>");5996}elsif(defined$set{'to_id'}) {5997next if($set{'to_id'} =~m/^0{40}$/);59985999print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6000 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6001-class=>"list"},6002"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6003"<br/>\n";6004}6005}6006close$fd;60076008# finish last commit (warning: repetition!)6009if(%co) {6010print"</td>\n".6011"<td class=\"link\">".6012$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6013" | ".6014$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6015print"</td>\n".6016"</tr>\n";6017}60186019print"</table>\n";6020}60216022if($searchtypeeq'grep') {6023 git_print_page_nav('','',$hash,$co{'tree'},$hash);6024 git_print_header_div('commit', esc_html($co{'title'}),$hash);60256026print"<table class=\"grep_search\">\n";6027my$alternate=1;6028my$matches=0;6029local$/="\n";6030open my$fd,"-|", git_cmd(),'grep','-n',6031$search_use_regexp? ('-E','-i') :'-F',6032$searchtext,$co{'tree'};6033my$lastfile='';6034while(my$line= <$fd>) {6035chomp$line;6036my($file,$lno,$ltext,$binary);6037last if($matches++>1000);6038if($line=~/^Binary file (.+) matches$/) {6039$file=$1;6040$binary=1;6041}else{6042(undef,$file,$lno,$ltext) =split(/:/,$line,4);6043}6044if($filene$lastfile) {6045$lastfileand print"</td></tr>\n";6046if($alternate++) {6047print"<tr class=\"dark\">\n";6048}else{6049print"<tr class=\"light\">\n";6050}6051print"<td class=\"list\">".6052$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6053 file_name=>"$file"),6054-class=>"list"}, esc_path($file));6055print"</td><td>\n";6056$lastfile=$file;6057}6058if($binary) {6059print"<div class=\"binary\">Binary file</div>\n";6060}else{6061$ltext= untabify($ltext);6062if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6063$ltext= esc_html($1, -nbsp=>1);6064$ltext.='<span class="match">';6065$ltext.= esc_html($2, -nbsp=>1);6066$ltext.='</span>';6067$ltext.= esc_html($3, -nbsp=>1);6068}else{6069$ltext= esc_html($ltext, -nbsp=>1);6070}6071print"<div class=\"pre\">".6072$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6073 file_name=>"$file").'#l'.$lno,6074-class=>"linenr"},sprintf('%4i',$lno))6075.' '.$ltext."</div>\n";6076}6077}6078if($lastfile) {6079print"</td></tr>\n";6080if($matches>1000) {6081print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6082}6083}else{6084print"<div class=\"diff nodifferences\">No matches found</div>\n";6085}6086close$fd;60876088print"</table>\n";6089}6090 git_footer_html();6091}60926093sub git_search_help {6094 git_header_html();6095 git_print_page_nav('','',$hash,$hash,$hash);6096print<<EOT;6097<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6098regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6099the pattern entered is recognized as the POSIX extended6100<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6101insensitive).</p>6102<dl>6103<dt><b>commit</b></dt>6104<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6105EOT6106my$have_grep= gitweb_check_feature('grep');6107if($have_grep) {6108print<<EOT;6109<dt><b>grep</b></dt>6110<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6111 a different one) are searched for the given pattern. On large trees, this search can take6112a while and put some strain on the server, so please use it with some consideration. Note that6113due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6114case-sensitive.</dd>6115EOT6116}6117print<<EOT;6118<dt><b>author</b></dt>6119<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6120<dt><b>committer</b></dt>6121<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6122EOT6123my$have_pickaxe= gitweb_check_feature('pickaxe');6124if($have_pickaxe) {6125print<<EOT;6126<dt><b>pickaxe</b></dt>6127<dd>All commits that caused the string to appear or disappear from any file (changes that6128added, removed or "modified" the string) will be listed. This search can take a while and6129takes a lot of strain on the server, so please use it wisely. Note that since you may be6130interested even in changes just changing the case as well, this search is case sensitive.</dd>6131EOT6132}6133print"</dl>\n";6134 git_footer_html();6135}61366137sub git_shortlog {6138my$head= git_get_head_hash($project);6139if(!defined$hash) {6140$hash=$head;6141}6142if(!defined$page) {6143$page=0;6144}6145my$refs= git_get_references();61466147my$commit_hash=$hash;6148if(defined$hash_parent) {6149$commit_hash="$hash_parent..$hash";6150}6151my@commitlist= parse_commits($commit_hash,101, (100*$page));61526153my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);6154my$next_link='';6155if($#commitlist>=100) {6156$next_link=6157$cgi->a({-href => href(-replay=>1, page=>$page+1),6158-accesskey =>"n", -title =>"Alt-n"},"next");6159}6160my$patch_max= gitweb_check_feature('patches');6161if($patch_max) {6162if($patch_max<0||@commitlist<=$patch_max) {6163$paging_nav.=" ⋅ ".6164$cgi->a({-href => href(action=>"patches", -replay=>1)},6165"patches");6166}6167}61686169 git_header_html();6170 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6171 git_print_header_div('summary',$project);61726173 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);61746175 git_footer_html();6176}61776178## ......................................................................6179## feeds (RSS, Atom; OPML)61806181sub git_feed {6182my$format=shift||'atom';6183my$have_blame= gitweb_check_feature('blame');61846185# Atom: http://www.atomenabled.org/developers/syndication/6186# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6187if($formatne'rss'&&$formatne'atom') {6188 die_error(400,"Unknown web feed format");6189}61906191# log/feed of current (HEAD) branch, log of given branch, history of file/directory6192my$head=$hash||'HEAD';6193my@commitlist= parse_commits($head,150,0,$file_name);61946195my%latest_commit;6196my%latest_date;6197my$content_type="application/$format+xml";6198if(defined$cgi->http('HTTP_ACCEPT') &&6199$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6200# browser (feed reader) prefers text/xml6201$content_type='text/xml';6202}6203if(defined($commitlist[0])) {6204%latest_commit= %{$commitlist[0]};6205my$latest_epoch=$latest_commit{'committer_epoch'};6206%latest_date= parse_date($latest_epoch);6207my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6208if(defined$if_modified) {6209my$since;6210if(eval{require HTTP::Date;1; }) {6211$since= HTTP::Date::str2time($if_modified);6212}elsif(eval{require Time::ParseDate;1; }) {6213$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6214}6215if(defined$since&&$latest_epoch<=$since) {6216print$cgi->header(6217-type =>$content_type,6218-charset =>'utf-8',6219-last_modified =>$latest_date{'rfc2822'},6220-status =>'304 Not Modified');6221return;6222}6223}6224print$cgi->header(6225-type =>$content_type,6226-charset =>'utf-8',6227-last_modified =>$latest_date{'rfc2822'});6228}else{6229print$cgi->header(6230-type =>$content_type,6231-charset =>'utf-8');6232}62336234# Optimization: skip generating the body if client asks only6235# for Last-Modified date.6236return if($cgi->request_method()eq'HEAD');62376238# header variables6239my$title="$site_name-$project/$action";6240my$feed_type='log';6241if(defined$hash) {6242$title.=" - '$hash'";6243$feed_type='branch log';6244if(defined$file_name) {6245$title.=" ::$file_name";6246$feed_type='history';6247}6248}elsif(defined$file_name) {6249$title.=" -$file_name";6250$feed_type='history';6251}6252$title.="$feed_type";6253my$descr= git_get_project_description($project);6254if(defined$descr) {6255$descr= esc_html($descr);6256}else{6257$descr="$project".6258($formateq'rss'?'RSS':'Atom') .6259" feed";6260}6261my$owner= git_get_project_owner($project);6262$owner= esc_html($owner);62636264#header6265my$alt_url;6266if(defined$file_name) {6267$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6268}elsif(defined$hash) {6269$alt_url= href(-full=>1, action=>"log", hash=>$hash);6270}else{6271$alt_url= href(-full=>1, action=>"summary");6272}6273print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6274if($formateq'rss') {6275print<<XML;6276<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6277<channel>6278XML6279print"<title>$title</title>\n".6280"<link>$alt_url</link>\n".6281"<description>$descr</description>\n".6282"<language>en</language>\n".6283# project owner is responsible for 'editorial' content6284"<managingEditor>$owner</managingEditor>\n";6285if(defined$logo||defined$favicon) {6286# prefer the logo to the favicon, since RSS6287# doesn't allow both6288my$img= esc_url($logo||$favicon);6289print"<image>\n".6290"<url>$img</url>\n".6291"<title>$title</title>\n".6292"<link>$alt_url</link>\n".6293"</image>\n";6294}6295if(%latest_date) {6296print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6297print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6298}6299print"<generator>gitweb v.$version/$git_version</generator>\n";6300}elsif($formateq'atom') {6301print<<XML;6302<feed xmlns="http://www.w3.org/2005/Atom">6303XML6304print"<title>$title</title>\n".6305"<subtitle>$descr</subtitle>\n".6306'<link rel="alternate" type="text/html" href="'.6307$alt_url.'" />'."\n".6308'<link rel="self" type="'.$content_type.'" href="'.6309$cgi->self_url() .'" />'."\n".6310"<id>". href(-full=>1) ."</id>\n".6311# use project owner for feed author6312"<author><name>$owner</name></author>\n";6313if(defined$favicon) {6314print"<icon>". esc_url($favicon) ."</icon>\n";6315}6316if(defined$logo_url) {6317# not twice as wide as tall: 72 x 27 pixels6318print"<logo>". esc_url($logo) ."</logo>\n";6319}6320if(!%latest_date) {6321# dummy date to keep the feed valid until commits trickle in:6322print"<updated>1970-01-01T00:00:00Z</updated>\n";6323}else{6324print"<updated>$latest_date{'iso-8601'}</updated>\n";6325}6326print"<generator version='$version/$git_version'>gitweb</generator>\n";6327}63286329# contents6330for(my$i=0;$i<=$#commitlist;$i++) {6331my%co= %{$commitlist[$i]};6332my$commit=$co{'id'};6333# we read 150, we always show 30 and the ones more recent than 48 hours6334if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6335last;6336}6337my%cd= parse_date($co{'author_epoch'});63386339# get list of changed files6340open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6341$co{'parent'} ||"--root",6342$co{'id'},"--", (defined$file_name?$file_name: ())6343ornext;6344my@difftree=map{chomp;$_} <$fd>;6345close$fd6346ornext;63476348# print element (entry, item)6349my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6350if($formateq'rss') {6351print"<item>\n".6352"<title>". esc_html($co{'title'}) ."</title>\n".6353"<author>". esc_html($co{'author'}) ."</author>\n".6354"<pubDate>$cd{'rfc2822'}</pubDate>\n".6355"<guid isPermaLink=\"true\">$co_url</guid>\n".6356"<link>$co_url</link>\n".6357"<description>". esc_html($co{'title'}) ."</description>\n".6358"<content:encoded>".6359"<![CDATA[\n";6360}elsif($formateq'atom') {6361print"<entry>\n".6362"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6363"<updated>$cd{'iso-8601'}</updated>\n".6364"<author>\n".6365" <name>". esc_html($co{'author_name'}) ."</name>\n";6366if($co{'author_email'}) {6367print" <email>". esc_html($co{'author_email'}) ."</email>\n";6368}6369print"</author>\n".6370# use committer for contributor6371"<contributor>\n".6372" <name>". esc_html($co{'committer_name'}) ."</name>\n";6373if($co{'committer_email'}) {6374print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6375}6376print"</contributor>\n".6377"<published>$cd{'iso-8601'}</published>\n".6378"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6379"<id>$co_url</id>\n".6380"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6381"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6382}6383my$comment=$co{'comment'};6384print"<pre>\n";6385foreachmy$line(@$comment) {6386$line= esc_html($line);6387print"$line\n";6388}6389print"</pre><ul>\n";6390foreachmy$difftree_line(@difftree) {6391my%difftree= parse_difftree_raw_line($difftree_line);6392next if!$difftree{'from_id'};63936394my$file=$difftree{'file'} ||$difftree{'to_file'};63956396print"<li>".6397"[".6398$cgi->a({-href => href(-full=>1, action=>"blobdiff",6399 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6400 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6401 file_name=>$file, file_parent=>$difftree{'from_file'}),6402-title =>"diff"},'D');6403if($have_blame) {6404print$cgi->a({-href => href(-full=>1, action=>"blame",6405 file_name=>$file, hash_base=>$commit),6406-title =>"blame"},'B');6407}6408# if this is not a feed of a file history6409if(!defined$file_name||$file_namene$file) {6410print$cgi->a({-href => href(-full=>1, action=>"history",6411 file_name=>$file, hash=>$commit),6412-title =>"history"},'H');6413}6414$file= esc_path($file);6415print"] ".6416"$file</li>\n";6417}6418if($formateq'rss') {6419print"</ul>]]>\n".6420"</content:encoded>\n".6421"</item>\n";6422}elsif($formateq'atom') {6423print"</ul>\n</div>\n".6424"</content>\n".6425"</entry>\n";6426}6427}64286429# end of feed6430if($formateq'rss') {6431print"</channel>\n</rss>\n";6432}elsif($formateq'atom') {6433print"</feed>\n";6434}6435}64366437sub git_rss {6438 git_feed('rss');6439}64406441sub git_atom {6442 git_feed('atom');6443}64446445sub git_opml {6446my@list= git_get_projects_list();64476448print$cgi->header(6449-type =>'text/xml',6450-charset =>'utf-8',6451-content_disposition =>'inline; filename="opml.xml"');64526453print<<XML;6454<?xml version="1.0" encoding="utf-8"?>6455<opml version="1.0">6456<head>6457 <title>$site_nameOPML Export</title>6458</head>6459<body>6460<outline text="git RSS feeds">6461XML64626463foreachmy$pr(@list) {6464my%proj=%$pr;6465my$head= git_get_head_hash($proj{'path'});6466if(!defined$head) {6467next;6468}6469$git_dir="$projectroot/$proj{'path'}";6470my%co= parse_commit($head);6471if(!%co) {6472next;6473}64746475my$path= esc_html(chop_str($proj{'path'},25,5));6476my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6477my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6478print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6479}6480print<<XML;6481</outline>6482</body>6483</opml>6484XML6485}