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 21our$t0; 22if(eval{require Time::HiRes;1; }) { 23$t0= [Time::HiRes::gettimeofday()]; 24} 25our$number_of_git_cmds=0; 26 27BEGIN{ 28 CGI->compile()if$ENV{'MOD_PERL'}; 29} 30 31our$cgi= new CGI; 32our$version="++GIT_VERSION++"; 33our$my_url=$cgi->url(); 34our$my_uri=$cgi->url(-absolute =>1); 35 36# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 37# needed and used only for URLs with nonempty PATH_INFO 38our$base_url=$my_url; 39 40# When the script is used as DirectoryIndex, the URL does not contain the name 41# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 42# have to do it ourselves. We make $path_info global because it's also used 43# later on. 44# 45# Another issue with the script being the DirectoryIndex is that the resulting 46# $my_url data is not the full script URL: this is good, because we want 47# generated links to keep implying the script name if it wasn't explicitly 48# indicated in the URL we're handling, but it means that $my_url cannot be used 49# as base URL. 50# Therefore, if we needed to strip PATH_INFO, then we know that we have 51# to build the base URL ourselves: 52our$path_info=$ENV{"PATH_INFO"}; 53if($path_info) { 54if($my_url=~ s,\Q$path_info\E$,, && 55$my_uri=~ s,\Q$path_info\E$,, && 56defined$ENV{'SCRIPT_NAME'}) { 57$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 58} 59} 60 61# core git executable to use 62# this can just be "git" if your webserver has a sensible PATH 63our$GIT="++GIT_BINDIR++/git"; 64 65# absolute fs-path which will be prepended to the project path 66#our $projectroot = "/pub/scm"; 67our$projectroot="++GITWEB_PROJECTROOT++"; 68 69# fs traversing limit for getting project list 70# the number is relative to the projectroot 71our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 72 73# target of the home link on top of all pages 74our$home_link=$my_uri||"/"; 75 76# string of the home link on top of all pages 77our$home_link_str="++GITWEB_HOME_LINK_STR++"; 78 79# name of your site or organization to appear in page titles 80# replace this with something more descriptive for clearer bookmarks 81our$site_name="++GITWEB_SITENAME++" 82|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 83 84# filename of html text to include at top of each page 85our$site_header="++GITWEB_SITE_HEADER++"; 86# html text to include at home page 87our$home_text="++GITWEB_HOMETEXT++"; 88# filename of html text to include at bottom of each page 89our$site_footer="++GITWEB_SITE_FOOTER++"; 90 91# URI of stylesheets 92our@stylesheets= ("++GITWEB_CSS++"); 93# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 94our$stylesheet=undef; 95# URI of GIT logo (72x27 size) 96our$logo="++GITWEB_LOGO++"; 97# URI of GIT favicon, assumed to be image/png type 98our$favicon="++GITWEB_FAVICON++"; 99# URI of gitweb.js (JavaScript code for gitweb) 100our$javascript="++GITWEB_JS++"; 101 102# URI and label (title) of GIT logo link 103#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 104#our $logo_label = "git documentation"; 105our$logo_url="http://git-scm.com/"; 106our$logo_label="git homepage"; 107 108# source of projects list 109our$projects_list="++GITWEB_LIST++"; 110 111# the width (in characters) of the projects list "Description" column 112our$projects_list_description_width=25; 113 114# default order of projects list 115# valid values are none, project, descr, owner, and age 116our$default_projects_order="project"; 117 118# show repository only if this file exists 119# (only effective if this variable evaluates to true) 120our$export_ok="++GITWEB_EXPORT_OK++"; 121 122# show repository only if this subroutine returns true 123# when given the path to the project, for example: 124# sub { return -e "$_[0]/git-daemon-export-ok"; } 125our$export_auth_hook=undef; 126 127# only allow viewing of repositories also shown on the overview page 128our$strict_export="++GITWEB_STRICT_EXPORT++"; 129 130# list of git base URLs used for URL to where fetch project from, 131# i.e. full URL is "$git_base_url/$project" 132our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 133 134# default blob_plain mimetype and default charset for text/plain blob 135our$default_blob_plain_mimetype='text/plain'; 136our$default_text_plain_charset=undef; 137 138# file to use for guessing MIME types before trying /etc/mime.types 139# (relative to the current git repository) 140our$mimetypes_file=undef; 141 142# assume this charset if line contains non-UTF-8 characters; 143# it should be valid encoding (see Encoding::Supported(3pm) for list), 144# for which encoding all byte sequences are valid, for example 145# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 146# could be even 'utf-8' for the old behavior) 147our$fallback_encoding='latin1'; 148 149# rename detection options for git-diff and git-diff-tree 150# - default is '-M', with the cost proportional to 151# (number of removed files) * (number of new files). 152# - more costly is '-C' (which implies '-M'), with the cost proportional to 153# (number of changed files + number of removed files) * (number of new files) 154# - even more costly is '-C', '--find-copies-harder' with cost 155# (number of files in the original tree) * (number of new files) 156# - one might want to include '-B' option, e.g. '-B', '-M' 157our@diff_opts= ('-M');# taken from git_commit 158 159# Disables features that would allow repository owners to inject script into 160# the gitweb domain. 161our$prevent_xss=0; 162 163# information about snapshot formats that gitweb is capable of serving 164our%known_snapshot_formats= ( 165# name => { 166# 'display' => display name, 167# 'type' => mime type, 168# 'suffix' => filename suffix, 169# 'format' => --format for git-archive, 170# 'compressor' => [compressor command and arguments] 171# (array reference, optional) 172# 'disabled' => boolean (optional)} 173# 174'tgz'=> { 175'display'=>'tar.gz', 176'type'=>'application/x-gzip', 177'suffix'=>'.tar.gz', 178'format'=>'tar', 179'compressor'=> ['gzip']}, 180 181'tbz2'=> { 182'display'=>'tar.bz2', 183'type'=>'application/x-bzip2', 184'suffix'=>'.tar.bz2', 185'format'=>'tar', 186'compressor'=> ['bzip2']}, 187 188'txz'=> { 189'display'=>'tar.xz', 190'type'=>'application/x-xz', 191'suffix'=>'.tar.xz', 192'format'=>'tar', 193'compressor'=> ['xz'], 194'disabled'=>1}, 195 196'zip'=> { 197'display'=>'zip', 198'type'=>'application/x-zip', 199'suffix'=>'.zip', 200'format'=>'zip'}, 201); 202 203# Aliases so we understand old gitweb.snapshot values in repository 204# configuration. 205our%known_snapshot_format_aliases= ( 206'gzip'=>'tgz', 207'bzip2'=>'tbz2', 208'xz'=>'txz', 209 210# backward compatibility: legacy gitweb config support 211'x-gzip'=>undef,'gz'=>undef, 212'x-bzip2'=>undef,'bz2'=>undef, 213'x-zip'=>undef,''=>undef, 214); 215 216# Pixel sizes for icons and avatars. If the default font sizes or lineheights 217# are changed, it may be appropriate to change these values too via 218# $GITWEB_CONFIG. 219our%avatar_size= ( 220'default'=>16, 221'double'=>32 222); 223 224# Used to set the maximum load that we will still respond to gitweb queries. 225# If server load exceed this value then return "503 server busy" error. 226# If gitweb cannot determined server load, it is taken to be 0. 227# Leave it undefined (or set to 'undef') to turn off load checking. 228our$maxload=300; 229 230# You define site-wide feature defaults here; override them with 231# $GITWEB_CONFIG as necessary. 232our%feature= ( 233# feature => { 234# 'sub' => feature-sub (subroutine), 235# 'override' => allow-override (boolean), 236# 'default' => [ default options...] (array reference)} 237# 238# if feature is overridable (it means that allow-override has true value), 239# then feature-sub will be called with default options as parameters; 240# return value of feature-sub indicates if to enable specified feature 241# 242# if there is no 'sub' key (no feature-sub), then feature cannot be 243# overriden 244# 245# use gitweb_get_feature(<feature>) to retrieve the <feature> value 246# (an array) or gitweb_check_feature(<feature>) to check if <feature> 247# is enabled 248 249# Enable the 'blame' blob view, showing the last commit that modified 250# each line in the file. This can be very CPU-intensive. 251 252# To enable system wide have in $GITWEB_CONFIG 253# $feature{'blame'}{'default'} = [1]; 254# To have project specific config enable override in $GITWEB_CONFIG 255# $feature{'blame'}{'override'} = 1; 256# and in project config gitweb.blame = 0|1; 257'blame'=> { 258'sub'=>sub{ feature_bool('blame',@_) }, 259'override'=>0, 260'default'=> [0]}, 261 262# Enable the 'snapshot' link, providing a compressed archive of any 263# tree. This can potentially generate high traffic if you have large 264# project. 265 266# Value is a list of formats defined in %known_snapshot_formats that 267# you wish to offer. 268# To disable system wide have in $GITWEB_CONFIG 269# $feature{'snapshot'}{'default'} = []; 270# To have project specific config enable override in $GITWEB_CONFIG 271# $feature{'snapshot'}{'override'} = 1; 272# and in project config, a comma-separated list of formats or "none" 273# to disable. Example: gitweb.snapshot = tbz2,zip; 274'snapshot'=> { 275'sub'=> \&feature_snapshot, 276'override'=>0, 277'default'=> ['tgz']}, 278 279# Enable text search, which will list the commits which match author, 280# committer or commit text to a given string. Enabled by default. 281# Project specific override is not supported. 282'search'=> { 283'override'=>0, 284'default'=> [1]}, 285 286# Enable grep search, which will list the files in currently selected 287# tree containing the given string. Enabled by default. This can be 288# potentially CPU-intensive, of course. 289 290# To enable system wide have in $GITWEB_CONFIG 291# $feature{'grep'}{'default'} = [1]; 292# To have project specific config enable override in $GITWEB_CONFIG 293# $feature{'grep'}{'override'} = 1; 294# and in project config gitweb.grep = 0|1; 295'grep'=> { 296'sub'=>sub{ feature_bool('grep',@_) }, 297'override'=>0, 298'default'=> [1]}, 299 300# Enable the pickaxe search, which will list the commits that modified 301# a given string in a file. This can be practical and quite faster 302# alternative to 'blame', but still potentially CPU-intensive. 303 304# To enable system wide have in $GITWEB_CONFIG 305# $feature{'pickaxe'}{'default'} = [1]; 306# To have project specific config enable override in $GITWEB_CONFIG 307# $feature{'pickaxe'}{'override'} = 1; 308# and in project config gitweb.pickaxe = 0|1; 309'pickaxe'=> { 310'sub'=>sub{ feature_bool('pickaxe',@_) }, 311'override'=>0, 312'default'=> [1]}, 313 314# Enable showing size of blobs in a 'tree' view, in a separate 315# column, similar to what 'ls -l' does. This cost a bit of IO. 316 317# To disable system wide have in $GITWEB_CONFIG 318# $feature{'show-sizes'}{'default'} = [0]; 319# To have project specific config enable override in $GITWEB_CONFIG 320# $feature{'show-sizes'}{'override'} = 1; 321# and in project config gitweb.showsizes = 0|1; 322'show-sizes'=> { 323'sub'=>sub{ feature_bool('showsizes',@_) }, 324'override'=>0, 325'default'=> [1]}, 326 327# Make gitweb use an alternative format of the URLs which can be 328# more readable and natural-looking: project name is embedded 329# directly in the path and the query string contains other 330# auxiliary information. All gitweb installations recognize 331# URL in either format; this configures in which formats gitweb 332# generates links. 333 334# To enable system wide have in $GITWEB_CONFIG 335# $feature{'pathinfo'}{'default'} = [1]; 336# Project specific override is not supported. 337 338# Note that you will need to change the default location of CSS, 339# favicon, logo and possibly other files to an absolute URL. Also, 340# if gitweb.cgi serves as your indexfile, you will need to force 341# $my_uri to contain the script name in your $GITWEB_CONFIG. 342'pathinfo'=> { 343'override'=>0, 344'default'=> [0]}, 345 346# Make gitweb consider projects in project root subdirectories 347# to be forks of existing projects. Given project $projname.git, 348# projects matching $projname/*.git will not be shown in the main 349# projects list, instead a '+' mark will be added to $projname 350# there and a 'forks' view will be enabled for the project, listing 351# all the forks. If project list is taken from a file, forks have 352# to be listed after the main project. 353 354# To enable system wide have in $GITWEB_CONFIG 355# $feature{'forks'}{'default'} = [1]; 356# Project specific override is not supported. 357'forks'=> { 358'override'=>0, 359'default'=> [0]}, 360 361# Insert custom links to the action bar of all project pages. 362# This enables you mainly to link to third-party scripts integrating 363# into gitweb; e.g. git-browser for graphical history representation 364# or custom web-based repository administration interface. 365 366# The 'default' value consists of a list of triplets in the form 367# (label, link, position) where position is the label after which 368# to insert the link and link is a format string where %n expands 369# to the project name, %f to the project path within the filesystem, 370# %h to the current hash (h gitweb parameter) and %b to the current 371# hash base (hb gitweb parameter); %% expands to %. 372 373# To enable system wide have in $GITWEB_CONFIG e.g. 374# $feature{'actions'}{'default'} = [('graphiclog', 375# '/git-browser/by-commit.html?r=%n', 'summary')]; 376# Project specific override is not supported. 377'actions'=> { 378'override'=>0, 379'default'=> []}, 380 381# Allow gitweb scan project content tags described in ctags/ 382# of project repository, and display the popular Web 2.0-ish 383# "tag cloud" near the project list. Note that this is something 384# COMPLETELY different from the normal Git tags. 385 386# gitweb by itself can show existing tags, but it does not handle 387# tagging itself; you need an external application for that. 388# For an example script, check Girocco's cgi/tagproj.cgi. 389# You may want to install the HTML::TagCloud Perl module to get 390# a pretty tag cloud instead of just a list of tags. 391 392# To enable system wide have in $GITWEB_CONFIG 393# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 394# Project specific override is not supported. 395'ctags'=> { 396'override'=>0, 397'default'=> [0]}, 398 399# The maximum number of patches in a patchset generated in patch 400# view. Set this to 0 or undef to disable patch view, or to a 401# negative number to remove any limit. 402 403# To disable system wide have in $GITWEB_CONFIG 404# $feature{'patches'}{'default'} = [0]; 405# To have project specific config enable override in $GITWEB_CONFIG 406# $feature{'patches'}{'override'} = 1; 407# and in project config gitweb.patches = 0|n; 408# where n is the maximum number of patches allowed in a patchset. 409'patches'=> { 410'sub'=> \&feature_patches, 411'override'=>0, 412'default'=> [16]}, 413 414# Avatar support. When this feature is enabled, views such as 415# shortlog or commit will display an avatar associated with 416# the email of the committer(s) and/or author(s). 417 418# Currently available providers are gravatar and picon. 419# If an unknown provider is specified, the feature is disabled. 420 421# Gravatar depends on Digest::MD5. 422# Picon currently relies on the indiana.edu database. 423 424# To enable system wide have in $GITWEB_CONFIG 425# $feature{'avatar'}{'default'} = ['<provider>']; 426# where <provider> is either gravatar or picon. 427# To have project specific config enable override in $GITWEB_CONFIG 428# $feature{'avatar'}{'override'} = 1; 429# and in project config gitweb.avatar = <provider>; 430'avatar'=> { 431'sub'=> \&feature_avatar, 432'override'=>0, 433'default'=> ['']}, 434 435# Enable displaying how much time and how many git commands 436# it took to generate and display page. Disabled by default. 437# Project specific override is not supported. 438'timed'=> { 439'override'=>0, 440'default'=> [0]}, 441 442# Enable turning some links into links to actions which require 443# JavaScript to run (like 'blame_incremental'). Not enabled by 444# default. Project specific override is currently not supported. 445'javascript-actions'=> { 446'override'=>0, 447'default'=> [0]}, 448); 449 450sub gitweb_get_feature { 451my($name) =@_; 452return unlessexists$feature{$name}; 453my($sub,$override,@defaults) = ( 454$feature{$name}{'sub'}, 455$feature{$name}{'override'}, 456@{$feature{$name}{'default'}}); 457if(!$override) {return@defaults; } 458if(!defined$sub) { 459warn"feature$nameis not overridable"; 460return@defaults; 461} 462return$sub->(@defaults); 463} 464 465# A wrapper to check if a given feature is enabled. 466# With this, you can say 467# 468# my $bool_feat = gitweb_check_feature('bool_feat'); 469# gitweb_check_feature('bool_feat') or somecode; 470# 471# instead of 472# 473# my ($bool_feat) = gitweb_get_feature('bool_feat'); 474# (gitweb_get_feature('bool_feat'))[0] or somecode; 475# 476sub gitweb_check_feature { 477return(gitweb_get_feature(@_))[0]; 478} 479 480 481sub feature_bool { 482my$key=shift; 483my($val) = git_get_project_config($key,'--bool'); 484 485if(!defined$val) { 486return($_[0]); 487}elsif($valeq'true') { 488return(1); 489}elsif($valeq'false') { 490return(0); 491} 492} 493 494sub feature_snapshot { 495my(@fmts) =@_; 496 497my($val) = git_get_project_config('snapshot'); 498 499if($val) { 500@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 501} 502 503return@fmts; 504} 505 506sub feature_patches { 507my@val= (git_get_project_config('patches','--int')); 508 509if(@val) { 510return@val; 511} 512 513return($_[0]); 514} 515 516sub feature_avatar { 517my@val= (git_get_project_config('avatar')); 518 519return@val?@val:@_; 520} 521 522# checking HEAD file with -e is fragile if the repository was 523# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 524# and then pruned. 525sub check_head_link { 526my($dir) =@_; 527my$headfile="$dir/HEAD"; 528return((-e $headfile) || 529(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 530} 531 532sub check_export_ok { 533my($dir) =@_; 534return(check_head_link($dir) && 535(!$export_ok|| -e "$dir/$export_ok") && 536(!$export_auth_hook||$export_auth_hook->($dir))); 537} 538 539# process alternate names for backward compatibility 540# filter out unsupported (unknown) snapshot formats 541sub filter_snapshot_fmts { 542my@fmts=@_; 543 544@fmts=map{ 545exists$known_snapshot_format_aliases{$_} ? 546$known_snapshot_format_aliases{$_} :$_}@fmts; 547@fmts=grep{ 548exists$known_snapshot_formats{$_} && 549!$known_snapshot_formats{$_}{'disabled'}}@fmts; 550} 551 552our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 553if(-e $GITWEB_CONFIG) { 554do$GITWEB_CONFIG; 555}else{ 556our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 557do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 558} 559 560# Get loadavg of system, to compare against $maxload. 561# Currently it requires '/proc/loadavg' present to get loadavg; 562# if it is not present it returns 0, which means no load checking. 563sub get_loadavg { 564if( -e '/proc/loadavg'){ 565open my$fd,'<','/proc/loadavg' 566orreturn0; 567my@load=split(/\s+/,scalar<$fd>); 568close$fd; 569 570# The first three columns measure CPU and IO utilization of the last one, 571# five, and 10 minute periods. The fourth column shows the number of 572# currently running processes and the total number of processes in the m/n 573# format. The last column displays the last process ID used. 574return$load[0] ||0; 575} 576# additional checks for load average should go here for things that don't export 577# /proc/loadavg 578 579return0; 580} 581 582# version of the core git binary 583our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 584$number_of_git_cmds++; 585 586$projects_list||=$projectroot; 587 588if(defined$maxload&& get_loadavg() >$maxload) { 589 die_error(503,"The load average on the server is too high"); 590} 591 592# ====================================================================== 593# input validation and dispatch 594 595# input parameters can be collected from a variety of sources (presently, CGI 596# and PATH_INFO), so we define an %input_params hash that collects them all 597# together during validation: this allows subsequent uses (e.g. href()) to be 598# agnostic of the parameter origin 599 600our%input_params= (); 601 602# input parameters are stored with the long parameter name as key. This will 603# also be used in the href subroutine to convert parameters to their CGI 604# equivalent, and since the href() usage is the most frequent one, we store 605# the name -> CGI key mapping here, instead of the reverse. 606# 607# XXX: Warning: If you touch this, check the search form for updating, 608# too. 609 610our@cgi_param_mapping= ( 611 project =>"p", 612 action =>"a", 613 file_name =>"f", 614 file_parent =>"fp", 615 hash =>"h", 616 hash_parent =>"hp", 617 hash_base =>"hb", 618 hash_parent_base =>"hpb", 619 page =>"pg", 620 order =>"o", 621 searchtext =>"s", 622 searchtype =>"st", 623 snapshot_format =>"sf", 624 extra_options =>"opt", 625 search_use_regexp =>"sr", 626# this must be last entry (for manipulation from JavaScript) 627 javascript =>"js" 628); 629our%cgi_param_mapping=@cgi_param_mapping; 630 631# we will also need to know the possible actions, for validation 632our%actions= ( 633"blame"=> \&git_blame, 634"blame_incremental"=> \&git_blame_incremental, 635"blame_data"=> \&git_blame_data, 636"blobdiff"=> \&git_blobdiff, 637"blobdiff_plain"=> \&git_blobdiff_plain, 638"blob"=> \&git_blob, 639"blob_plain"=> \&git_blob_plain, 640"commitdiff"=> \&git_commitdiff, 641"commitdiff_plain"=> \&git_commitdiff_plain, 642"commit"=> \&git_commit, 643"forks"=> \&git_forks, 644"heads"=> \&git_heads, 645"history"=> \&git_history, 646"log"=> \&git_log, 647"patch"=> \&git_patch, 648"patches"=> \&git_patches, 649"rss"=> \&git_rss, 650"atom"=> \&git_atom, 651"search"=> \&git_search, 652"search_help"=> \&git_search_help, 653"shortlog"=> \&git_shortlog, 654"summary"=> \&git_summary, 655"tag"=> \&git_tag, 656"tags"=> \&git_tags, 657"tree"=> \&git_tree, 658"snapshot"=> \&git_snapshot, 659"object"=> \&git_object, 660# those below don't need $project 661"opml"=> \&git_opml, 662"project_list"=> \&git_project_list, 663"project_index"=> \&git_project_index, 664); 665 666# finally, we have the hash of allowed extra_options for the commands that 667# allow them 668our%allowed_options= ( 669"--no-merges"=> [qw(rss atom log shortlog history)], 670); 671 672# fill %input_params with the CGI parameters. All values except for 'opt' 673# should be single values, but opt can be an array. We should probably 674# build an array of parameters that can be multi-valued, but since for the time 675# being it's only this one, we just single it out 676while(my($name,$symbol) =each%cgi_param_mapping) { 677if($symboleq'opt') { 678$input_params{$name} = [$cgi->param($symbol) ]; 679}else{ 680$input_params{$name} =$cgi->param($symbol); 681} 682} 683 684# now read PATH_INFO and update the parameter list for missing parameters 685sub evaluate_path_info { 686return ifdefined$input_params{'project'}; 687return if!$path_info; 688$path_info=~ s,^/+,,; 689return if!$path_info; 690 691# find which part of PATH_INFO is project 692my$project=$path_info; 693$project=~ s,/+$,,; 694while($project&& !check_head_link("$projectroot/$project")) { 695$project=~ s,/*[^/]*$,,; 696} 697return unless$project; 698$input_params{'project'} =$project; 699 700# do not change any parameters if an action is given using the query string 701return if$input_params{'action'}; 702$path_info=~ s,^\Q$project\E/*,,; 703 704# next, check if we have an action 705my$action=$path_info; 706$action=~ s,/.*$,,; 707if(exists$actions{$action}) { 708$path_info=~ s,^$action/*,,; 709$input_params{'action'} =$action; 710} 711 712# list of actions that want hash_base instead of hash, but can have no 713# pathname (f) parameter 714my@wants_base= ( 715'tree', 716'history', 717); 718 719# we want to catch 720# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 721my($parentrefname,$parentpathname,$refname,$pathname) = 722($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 723 724# first, analyze the 'current' part 725if(defined$pathname) { 726# we got "branch:filename" or "branch:dir/" 727# we could use git_get_type(branch:pathname), but: 728# - it needs $git_dir 729# - it does a git() call 730# - the convention of terminating directories with a slash 731# makes it superfluous 732# - embedding the action in the PATH_INFO would make it even 733# more superfluous 734$pathname=~ s,^/+,,; 735if(!$pathname||substr($pathname, -1)eq"/") { 736$input_params{'action'} ||="tree"; 737$pathname=~ s,/$,,; 738}else{ 739# the default action depends on whether we had parent info 740# or not 741if($parentrefname) { 742$input_params{'action'} ||="blobdiff_plain"; 743}else{ 744$input_params{'action'} ||="blob_plain"; 745} 746} 747$input_params{'hash_base'} ||=$refname; 748$input_params{'file_name'} ||=$pathname; 749}elsif(defined$refname) { 750# we got "branch". In this case we have to choose if we have to 751# set hash or hash_base. 752# 753# Most of the actions without a pathname only want hash to be 754# set, except for the ones specified in @wants_base that want 755# hash_base instead. It should also be noted that hand-crafted 756# links having 'history' as an action and no pathname or hash 757# set will fail, but that happens regardless of PATH_INFO. 758$input_params{'action'} ||="shortlog"; 759if(grep{$_eq$input_params{'action'} }@wants_base) { 760$input_params{'hash_base'} ||=$refname; 761}else{ 762$input_params{'hash'} ||=$refname; 763} 764} 765 766# next, handle the 'parent' part, if present 767if(defined$parentrefname) { 768# a missing pathspec defaults to the 'current' filename, allowing e.g. 769# someproject/blobdiff/oldrev..newrev:/filename 770if($parentpathname) { 771$parentpathname=~ s,^/+,,; 772$parentpathname=~ s,/$,,; 773$input_params{'file_parent'} ||=$parentpathname; 774}else{ 775$input_params{'file_parent'} ||=$input_params{'file_name'}; 776} 777# we assume that hash_parent_base is wanted if a path was specified, 778# or if the action wants hash_base instead of hash 779if(defined$input_params{'file_parent'} || 780grep{$_eq$input_params{'action'} }@wants_base) { 781$input_params{'hash_parent_base'} ||=$parentrefname; 782}else{ 783$input_params{'hash_parent'} ||=$parentrefname; 784} 785} 786 787# for the snapshot action, we allow URLs in the form 788# $project/snapshot/$hash.ext 789# where .ext determines the snapshot and gets removed from the 790# passed $refname to provide the $hash. 791# 792# To be able to tell that $refname includes the format extension, we 793# require the following two conditions to be satisfied: 794# - the hash input parameter MUST have been set from the $refname part 795# of the URL (i.e. they must be equal) 796# - the snapshot format MUST NOT have been defined already (e.g. from 797# CGI parameter sf) 798# It's also useless to try any matching unless $refname has a dot, 799# so we check for that too 800if(defined$input_params{'action'} && 801$input_params{'action'}eq'snapshot'&& 802defined$refname&&index($refname,'.') != -1&& 803$refnameeq$input_params{'hash'} && 804!defined$input_params{'snapshot_format'}) { 805# We loop over the known snapshot formats, checking for 806# extensions. Allowed extensions are both the defined suffix 807# (which includes the initial dot already) and the snapshot 808# format key itself, with a prepended dot 809while(my($fmt,$opt) =each%known_snapshot_formats) { 810my$hash=$refname; 811unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 812next; 813} 814my$sfx=$1; 815# a valid suffix was found, so set the snapshot format 816# and reset the hash parameter 817$input_params{'snapshot_format'} =$fmt; 818$input_params{'hash'} =$hash; 819# we also set the format suffix to the one requested 820# in the URL: this way a request for e.g. .tgz returns 821# a .tgz instead of a .tar.gz 822$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 823last; 824} 825} 826} 827evaluate_path_info(); 828 829our$action=$input_params{'action'}; 830if(defined$action) { 831if(!validate_action($action)) { 832 die_error(400,"Invalid action parameter"); 833} 834} 835 836# parameters which are pathnames 837our$project=$input_params{'project'}; 838if(defined$project) { 839if(!validate_project($project)) { 840undef$project; 841 die_error(404,"No such project"); 842} 843} 844 845our$file_name=$input_params{'file_name'}; 846if(defined$file_name) { 847if(!validate_pathname($file_name)) { 848 die_error(400,"Invalid file parameter"); 849} 850} 851 852our$file_parent=$input_params{'file_parent'}; 853if(defined$file_parent) { 854if(!validate_pathname($file_parent)) { 855 die_error(400,"Invalid file parent parameter"); 856} 857} 858 859# parameters which are refnames 860our$hash=$input_params{'hash'}; 861if(defined$hash) { 862if(!validate_refname($hash)) { 863 die_error(400,"Invalid hash parameter"); 864} 865} 866 867our$hash_parent=$input_params{'hash_parent'}; 868if(defined$hash_parent) { 869if(!validate_refname($hash_parent)) { 870 die_error(400,"Invalid hash parent parameter"); 871} 872} 873 874our$hash_base=$input_params{'hash_base'}; 875if(defined$hash_base) { 876if(!validate_refname($hash_base)) { 877 die_error(400,"Invalid hash base parameter"); 878} 879} 880 881our@extra_options= @{$input_params{'extra_options'}}; 882# @extra_options is always defined, since it can only be (currently) set from 883# CGI, and $cgi->param() returns the empty array in array context if the param 884# is not set 885foreachmy$opt(@extra_options) { 886if(not exists$allowed_options{$opt}) { 887 die_error(400,"Invalid option parameter"); 888} 889if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 890 die_error(400,"Invalid option parameter for this action"); 891} 892} 893 894our$hash_parent_base=$input_params{'hash_parent_base'}; 895if(defined$hash_parent_base) { 896if(!validate_refname($hash_parent_base)) { 897 die_error(400,"Invalid hash parent base parameter"); 898} 899} 900 901# other parameters 902our$page=$input_params{'page'}; 903if(defined$page) { 904if($page=~m/[^0-9]/) { 905 die_error(400,"Invalid page parameter"); 906} 907} 908 909our$searchtype=$input_params{'searchtype'}; 910if(defined$searchtype) { 911if($searchtype=~m/[^a-z]/) { 912 die_error(400,"Invalid searchtype parameter"); 913} 914} 915 916our$search_use_regexp=$input_params{'search_use_regexp'}; 917 918our$searchtext=$input_params{'searchtext'}; 919our$search_regexp; 920if(defined$searchtext) { 921if(length($searchtext) <2) { 922 die_error(403,"At least two characters are required for search parameter"); 923} 924$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 925} 926 927# path to the current git repository 928our$git_dir; 929$git_dir="$projectroot/$project"if$project; 930 931# list of supported snapshot formats 932our@snapshot_fmts= gitweb_get_feature('snapshot'); 933@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 934 935# check that the avatar feature is set to a known provider name, 936# and for each provider check if the dependencies are satisfied. 937# if the provider name is invalid or the dependencies are not met, 938# reset $git_avatar to the empty string. 939our($git_avatar) = gitweb_get_feature('avatar'); 940if($git_avatareq'gravatar') { 941$git_avatar=''unless(eval{require Digest::MD5;1; }); 942}elsif($git_avatareq'picon') { 943# no dependencies 944}else{ 945$git_avatar=''; 946} 947 948# dispatch 949if(!defined$action) { 950if(defined$hash) { 951$action= git_get_type($hash); 952}elsif(defined$hash_base&&defined$file_name) { 953$action= git_get_type("$hash_base:$file_name"); 954}elsif(defined$project) { 955$action='summary'; 956}else{ 957$action='project_list'; 958} 959} 960if(!defined($actions{$action})) { 961 die_error(400,"Unknown action"); 962} 963if($action!~m/^(?:opml|project_list|project_index)$/&& 964!$project) { 965 die_error(400,"Project needed"); 966} 967$actions{$action}->(); 968exit; 969 970## ====================================================================== 971## action links 972 973sub href { 974my%params=@_; 975# default is to use -absolute url() i.e. $my_uri 976my$href=$params{-full} ?$my_url:$my_uri; 977 978$params{'project'} =$projectunlessexists$params{'project'}; 979 980if($params{-replay}) { 981while(my($name,$symbol) =each%cgi_param_mapping) { 982if(!exists$params{$name}) { 983$params{$name} =$input_params{$name}; 984} 985} 986} 987 988my$use_pathinfo= gitweb_check_feature('pathinfo'); 989if($use_pathinfoand defined$params{'project'}) { 990# try to put as many parameters as possible in PATH_INFO: 991# - project name 992# - action 993# - hash_parent or hash_parent_base:/file_parent 994# - hash or hash_base:/filename 995# - the snapshot_format as an appropriate suffix 996 997# When the script is the root DirectoryIndex for the domain, 998# $href here would be something like http://gitweb.example.com/ 999# Thus, we strip any trailing / from $href, to spare us double1000# slashes in the final URL1001$href=~ s,/$,,;10021003# Then add the project name, if present1004$href.="/".esc_url($params{'project'});1005delete$params{'project'};10061007# since we destructively absorb parameters, we keep this1008# boolean that remembers if we're handling a snapshot1009my$is_snapshot=$params{'action'}eq'snapshot';10101011# Summary just uses the project path URL, any other action is1012# added to the URL1013if(defined$params{'action'}) {1014$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1015delete$params{'action'};1016}10171018# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1019# stripping nonexistent or useless pieces1020$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1021||$params{'hash_parent'} ||$params{'hash'});1022if(defined$params{'hash_base'}) {1023if(defined$params{'hash_parent_base'}) {1024$href.= esc_url($params{'hash_parent_base'});1025# skip the file_parent if it's the same as the file_name1026if(defined$params{'file_parent'}) {1027if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1028delete$params{'file_parent'};1029}elsif($params{'file_parent'} !~/\.\./) {1030$href.=":/".esc_url($params{'file_parent'});1031delete$params{'file_parent'};1032}1033}1034$href.="..";1035delete$params{'hash_parent'};1036delete$params{'hash_parent_base'};1037}elsif(defined$params{'hash_parent'}) {1038$href.= esc_url($params{'hash_parent'})."..";1039delete$params{'hash_parent'};1040}10411042$href.= esc_url($params{'hash_base'});1043if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1044$href.=":/".esc_url($params{'file_name'});1045delete$params{'file_name'};1046}1047delete$params{'hash'};1048delete$params{'hash_base'};1049}elsif(defined$params{'hash'}) {1050$href.= esc_url($params{'hash'});1051delete$params{'hash'};1052}10531054# If the action was a snapshot, we can absorb the1055# snapshot_format parameter too1056if($is_snapshot) {1057my$fmt=$params{'snapshot_format'};1058# snapshot_format should always be defined when href()1059# is called, but just in case some code forgets, we1060# fall back to the default1061$fmt||=$snapshot_fmts[0];1062$href.=$known_snapshot_formats{$fmt}{'suffix'};1063delete$params{'snapshot_format'};1064}1065}10661067# now encode the parameters explicitly1068my@result= ();1069for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1070my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1071if(defined$params{$name}) {1072if(ref($params{$name})eq"ARRAY") {1073foreachmy$par(@{$params{$name}}) {1074push@result,$symbol."=". esc_param($par);1075}1076}else{1077push@result,$symbol."=". esc_param($params{$name});1078}1079}1080}1081$href.="?".join(';',@result)ifscalar@result;10821083return$href;1084}108510861087## ======================================================================1088## validation, quoting/unquoting and escaping10891090sub validate_action {1091my$input=shift||returnundef;1092returnundefunlessexists$actions{$input};1093return$input;1094}10951096sub validate_project {1097my$input=shift||returnundef;1098if(!validate_pathname($input) ||1099!(-d "$projectroot/$input") ||1100!check_export_ok("$projectroot/$input") ||1101($strict_export&& !project_in_list($input))) {1102returnundef;1103}else{1104return$input;1105}1106}11071108sub validate_pathname {1109my$input=shift||returnundef;11101111# no '.' or '..' as elements of path, i.e. no '.' nor '..'1112# at the beginning, at the end, and between slashes.1113# also this catches doubled slashes1114if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1115returnundef;1116}1117# no null characters1118if($input=~m!\0!) {1119returnundef;1120}1121return$input;1122}11231124sub validate_refname {1125my$input=shift||returnundef;11261127# textual hashes are O.K.1128if($input=~m/^[0-9a-fA-F]{40}$/) {1129return$input;1130}1131# it must be correct pathname1132$input= validate_pathname($input)1133orreturnundef;1134# restrictions on ref name according to git-check-ref-format1135if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1136returnundef;1137}1138return$input;1139}11401141# decode sequences of octets in utf8 into Perl's internal form,1142# which is utf-8 with utf8 flag set if needed. gitweb writes out1143# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1144sub to_utf8 {1145my$str=shift;1146if(utf8::valid($str)) {1147 utf8::decode($str);1148return$str;1149}else{1150return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1151}1152}11531154# quote unsafe chars, but keep the slash, even when it's not1155# correct, but quoted slashes look too horrible in bookmarks1156sub esc_param {1157my$str=shift;1158$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1159$str=~s/ /\+/g;1160return$str;1161}11621163# quote unsafe chars in whole URL, so some charactrs cannot be quoted1164sub esc_url {1165my$str=shift;1166$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1167$str=~s/\+/%2B/g;1168$str=~s/ /\+/g;1169return$str;1170}11711172# replace invalid utf8 character with SUBSTITUTION sequence1173sub esc_html {1174my$str=shift;1175my%opts=@_;11761177$str= to_utf8($str);1178$str=$cgi->escapeHTML($str);1179if($opts{'-nbsp'}) {1180$str=~s/ / /g;1181}1182$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1183return$str;1184}11851186# quote control characters and escape filename to HTML1187sub esc_path {1188my$str=shift;1189my%opts=@_;11901191$str= to_utf8($str);1192$str=$cgi->escapeHTML($str);1193if($opts{'-nbsp'}) {1194$str=~s/ / /g;1195}1196$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1197return$str;1198}11991200# Make control characters "printable", using character escape codes (CEC)1201sub quot_cec {1202my$cntrl=shift;1203my%opts=@_;1204my%es= (# character escape codes, aka escape sequences1205"\t"=>'\t',# tab (HT)1206"\n"=>'\n',# line feed (LF)1207"\r"=>'\r',# carrige return (CR)1208"\f"=>'\f',# form feed (FF)1209"\b"=>'\b',# backspace (BS)1210"\a"=>'\a',# alarm (bell) (BEL)1211"\e"=>'\e',# escape (ESC)1212"\013"=>'\v',# vertical tab (VT)1213"\000"=>'\0',# nul character (NUL)1214);1215my$chr= ( (exists$es{$cntrl})1216?$es{$cntrl}1217:sprintf('\%2x',ord($cntrl)) );1218if($opts{-nohtml}) {1219return$chr;1220}else{1221return"<span class=\"cntrl\">$chr</span>";1222}1223}12241225# Alternatively use unicode control pictures codepoints,1226# Unicode "printable representation" (PR)1227sub quot_upr {1228my$cntrl=shift;1229my%opts=@_;12301231my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1232if($opts{-nohtml}) {1233return$chr;1234}else{1235return"<span class=\"cntrl\">$chr</span>";1236}1237}12381239# git may return quoted and escaped filenames1240sub unquote {1241my$str=shift;12421243sub unq {1244my$seq=shift;1245my%es= (# character escape codes, aka escape sequences1246't'=>"\t",# tab (HT, TAB)1247'n'=>"\n",# newline (NL)1248'r'=>"\r",# return (CR)1249'f'=>"\f",# form feed (FF)1250'b'=>"\b",# backspace (BS)1251'a'=>"\a",# alarm (bell) (BEL)1252'e'=>"\e",# escape (ESC)1253'v'=>"\013",# vertical tab (VT)1254);12551256if($seq=~m/^[0-7]{1,3}$/) {1257# octal char sequence1258returnchr(oct($seq));1259}elsif(exists$es{$seq}) {1260# C escape sequence, aka character escape code1261return$es{$seq};1262}1263# quoted ordinary character1264return$seq;1265}12661267if($str=~m/^"(.*)"$/) {1268# needs unquoting1269$str=$1;1270$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1271}1272return$str;1273}12741275# escape tabs (convert tabs to spaces)1276sub untabify {1277my$line=shift;12781279while((my$pos=index($line,"\t")) != -1) {1280if(my$count= (8- ($pos%8))) {1281my$spaces=' ' x $count;1282$line=~s/\t/$spaces/;1283}1284}12851286return$line;1287}12881289sub project_in_list {1290my$project=shift;1291my@list= git_get_projects_list();1292return@list&&scalar(grep{$_->{'path'}eq$project}@list);1293}12941295## ----------------------------------------------------------------------1296## HTML aware string manipulation12971298# Try to chop given string on a word boundary between position1299# $len and $len+$add_len. If there is no word boundary there,1300# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1301# (marking chopped part) would be longer than given string.1302sub chop_str {1303my$str=shift;1304my$len=shift;1305my$add_len=shift||10;1306my$where=shift||'right';# 'left' | 'center' | 'right'13071308# Make sure perl knows it is utf8 encoded so we don't1309# cut in the middle of a utf8 multibyte char.1310$str= to_utf8($str);13111312# allow only $len chars, but don't cut a word if it would fit in $add_len1313# if it doesn't fit, cut it if it's still longer than the dots we would add1314# remove chopped character entities entirely13151316# when chopping in the middle, distribute $len into left and right part1317# return early if chopping wouldn't make string shorter1318if($whereeq'center') {1319return$strif($len+5>=length($str));# filler is length 51320$len=int($len/2);1321}else{1322return$strif($len+4>=length($str));# filler is length 41323}13241325# regexps: ending and beginning with word part up to $add_len1326my$endre=qr/.{$len}\w{0,$add_len}/;1327my$begre=qr/\w{0,$add_len}.{$len}/;13281329if($whereeq'left') {1330$str=~m/^(.*?)($begre)$/;1331my($lead,$body) = ($1,$2);1332if(length($lead) >4) {1333$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1334$lead=" ...";1335}1336return"$lead$body";13371338}elsif($whereeq'center') {1339$str=~m/^($endre)(.*)$/;1340my($left,$str) = ($1,$2);1341$str=~m/^(.*?)($begre)$/;1342my($mid,$right) = ($1,$2);1343if(length($mid) >5) {1344$left=~s/&[^;]*$//;1345$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1346$mid=" ... ";1347}1348return"$left$mid$right";13491350}else{1351$str=~m/^($endre)(.*)$/;1352my$body=$1;1353my$tail=$2;1354if(length($tail) >4) {1355$body=~s/&[^;]*$//;1356$tail="... ";1357}1358return"$body$tail";1359}1360}13611362# takes the same arguments as chop_str, but also wraps a <span> around the1363# result with a title attribute if it does get chopped. Additionally, the1364# string is HTML-escaped.1365sub chop_and_escape_str {1366my($str) =@_;13671368my$chopped= chop_str(@_);1369if($choppedeq$str) {1370return esc_html($chopped);1371}else{1372$str=~s/[[:cntrl:]]/?/g;1373return$cgi->span({-title=>$str}, esc_html($chopped));1374}1375}13761377## ----------------------------------------------------------------------1378## functions returning short strings13791380# CSS class for given age value (in seconds)1381sub age_class {1382my$age=shift;13831384if(!defined$age) {1385return"noage";1386}elsif($age<60*60*2) {1387return"age0";1388}elsif($age<60*60*24*2) {1389return"age1";1390}else{1391return"age2";1392}1393}13941395# convert age in seconds to "nn units ago" string1396sub age_string {1397my$age=shift;1398my$age_str;13991400if($age>60*60*24*365*2) {1401$age_str= (int$age/60/60/24/365);1402$age_str.=" years ago";1403}elsif($age>60*60*24*(365/12)*2) {1404$age_str=int$age/60/60/24/(365/12);1405$age_str.=" months ago";1406}elsif($age>60*60*24*7*2) {1407$age_str=int$age/60/60/24/7;1408$age_str.=" weeks ago";1409}elsif($age>60*60*24*2) {1410$age_str=int$age/60/60/24;1411$age_str.=" days ago";1412}elsif($age>60*60*2) {1413$age_str=int$age/60/60;1414$age_str.=" hours ago";1415}elsif($age>60*2) {1416$age_str=int$age/60;1417$age_str.=" min ago";1418}elsif($age>2) {1419$age_str=int$age;1420$age_str.=" sec ago";1421}else{1422$age_str.=" right now";1423}1424return$age_str;1425}14261427useconstant{1428 S_IFINVALID =>0030000,1429 S_IFGITLINK =>0160000,1430};14311432# submodule/subproject, a commit object reference1433sub S_ISGITLINK {1434my$mode=shift;14351436return(($mode& S_IFMT) == S_IFGITLINK)1437}14381439# convert file mode in octal to symbolic file mode string1440sub mode_str {1441my$mode=oct shift;14421443if(S_ISGITLINK($mode)) {1444return'm---------';1445}elsif(S_ISDIR($mode& S_IFMT)) {1446return'drwxr-xr-x';1447}elsif(S_ISLNK($mode)) {1448return'lrwxrwxrwx';1449}elsif(S_ISREG($mode)) {1450# git cares only about the executable bit1451if($mode& S_IXUSR) {1452return'-rwxr-xr-x';1453}else{1454return'-rw-r--r--';1455};1456}else{1457return'----------';1458}1459}14601461# convert file mode in octal to file type string1462sub file_type {1463my$mode=shift;14641465if($mode!~m/^[0-7]+$/) {1466return$mode;1467}else{1468$mode=oct$mode;1469}14701471if(S_ISGITLINK($mode)) {1472return"submodule";1473}elsif(S_ISDIR($mode& S_IFMT)) {1474return"directory";1475}elsif(S_ISLNK($mode)) {1476return"symlink";1477}elsif(S_ISREG($mode)) {1478return"file";1479}else{1480return"unknown";1481}1482}14831484# convert file mode in octal to file type description string1485sub file_type_long {1486my$mode=shift;14871488if($mode!~m/^[0-7]+$/) {1489return$mode;1490}else{1491$mode=oct$mode;1492}14931494if(S_ISGITLINK($mode)) {1495return"submodule";1496}elsif(S_ISDIR($mode& S_IFMT)) {1497return"directory";1498}elsif(S_ISLNK($mode)) {1499return"symlink";1500}elsif(S_ISREG($mode)) {1501if($mode& S_IXUSR) {1502return"executable";1503}else{1504return"file";1505};1506}else{1507return"unknown";1508}1509}151015111512## ----------------------------------------------------------------------1513## functions returning short HTML fragments, or transforming HTML fragments1514## which don't belong to other sections15151516# format line of commit message.1517sub format_log_line_html {1518my$line=shift;15191520$line= esc_html($line, -nbsp=>1);1521$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1522$cgi->a({-href => href(action=>"object", hash=>$1),1523-class=>"text"},$1);1524}eg;15251526return$line;1527}15281529# format marker of refs pointing to given object15301531# the destination action is chosen based on object type and current context:1532# - for annotated tags, we choose the tag view unless it's the current view1533# already, in which case we go to shortlog view1534# - for other refs, we keep the current view if we're in history, shortlog or1535# log view, and select shortlog otherwise1536sub format_ref_marker {1537my($refs,$id) =@_;1538my$markers='';15391540if(defined$refs->{$id}) {1541foreachmy$ref(@{$refs->{$id}}) {1542# this code exploits the fact that non-lightweight tags are the1543# only indirect objects, and that they are the only objects for which1544# we want to use tag instead of shortlog as action1545my($type,$name) =qw();1546my$indirect= ($ref=~s/\^\{\}$//);1547# e.g. tags/v2.6.11 or heads/next1548if($ref=~m!^(.*?)s?/(.*)$!) {1549$type=$1;1550$name=$2;1551}else{1552$type="ref";1553$name=$ref;1554}15551556my$class=$type;1557$class.=" indirect"if$indirect;15581559my$dest_action="shortlog";15601561if($indirect) {1562$dest_action="tag"unless$actioneq"tag";1563}elsif($action=~/^(history|(short)?log)$/) {1564$dest_action=$action;1565}15661567my$dest="";1568$dest.="refs/"unless$ref=~ m!^refs/!;1569$dest.=$ref;15701571my$link=$cgi->a({1572-href => href(1573 action=>$dest_action,1574 hash=>$dest1575)},$name);15761577$markers.=" <span class=\"$class\"title=\"$ref\">".1578$link."</span>";1579}1580}15811582if($markers) {1583return' <span class="refs">'.$markers.'</span>';1584}else{1585return"";1586}1587}15881589# format, perhaps shortened and with markers, title line1590sub format_subject_html {1591my($long,$short,$href,$extra) =@_;1592$extra=''unlessdefined($extra);15931594if(length($short) <length($long)) {1595$long=~s/[[:cntrl:]]/?/g;1596return$cgi->a({-href =>$href, -class=>"list subject",1597-title => to_utf8($long)},1598 esc_html($short)) .$extra;1599}else{1600return$cgi->a({-href =>$href, -class=>"list subject"},1601 esc_html($long)) .$extra;1602}1603}16041605# Rather than recomputing the url for an email multiple times, we cache it1606# after the first hit. This gives a visible benefit in views where the avatar1607# for the same email is used repeatedly (e.g. shortlog).1608# The cache is shared by all avatar engines (currently gravatar only), which1609# are free to use it as preferred. Since only one avatar engine is used for any1610# given page, there's no risk for cache conflicts.1611our%avatar_cache= ();16121613# Compute the picon url for a given email, by using the picon search service over at1614# http://www.cs.indiana.edu/picons/search.html1615sub picon_url {1616my$email=lc shift;1617if(!$avatar_cache{$email}) {1618my($user,$domain) =split('@',$email);1619$avatar_cache{$email} =1620"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1621"$domain/$user/".1622"users+domains+unknown/up/single";1623}1624return$avatar_cache{$email};1625}16261627# Compute the gravatar url for a given email, if it's not in the cache already.1628# Gravatar stores only the part of the URL before the size, since that's the1629# one computationally more expensive. This also allows reuse of the cache for1630# different sizes (for this particular engine).1631sub gravatar_url {1632my$email=lc shift;1633my$size=shift;1634$avatar_cache{$email} ||=1635"http://www.gravatar.com/avatar/".1636 Digest::MD5::md5_hex($email) ."?s=";1637return$avatar_cache{$email} .$size;1638}16391640# Insert an avatar for the given $email at the given $size if the feature1641# is enabled.1642sub git_get_avatar {1643my($email,%opts) =@_;1644my$pre_white= ($opts{-pad_before} ?" ":"");1645my$post_white= ($opts{-pad_after} ?" ":"");1646$opts{-size} ||='default';1647my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1648my$url="";1649if($git_avatareq'gravatar') {1650$url= gravatar_url($email,$size);1651}elsif($git_avatareq'picon') {1652$url= picon_url($email);1653}1654# Other providers can be added by extending the if chain, defining $url1655# as needed. If no variant puts something in $url, we assume avatars1656# are completely disabled/unavailable.1657if($url) {1658return$pre_white.1659"<img width=\"$size\"".1660"class=\"avatar\"".1661"src=\"$url\"".1662"alt=\"\"".1663"/>".$post_white;1664}else{1665return"";1666}1667}16681669sub format_search_author {1670my($author,$searchtype,$displaytext) =@_;1671my$have_search= gitweb_check_feature('search');16721673if($have_search) {1674my$performed="";1675if($searchtypeeq'author') {1676$performed="authored";1677}elsif($searchtypeeq'committer') {1678$performed="committed";1679}16801681return$cgi->a({-href => href(action=>"search", hash=>$hash,1682 searchtext=>$author,1683 searchtype=>$searchtype),class=>"list",1684 title=>"Search for commits$performedby$author"},1685$displaytext);16861687}else{1688return$displaytext;1689}1690}16911692# format the author name of the given commit with the given tag1693# the author name is chopped and escaped according to the other1694# optional parameters (see chop_str).1695sub format_author_html {1696my$tag=shift;1697my$co=shift;1698my$author= chop_and_escape_str($co->{'author_name'},@_);1699return"<$tagclass=\"author\">".1700 format_search_author($co->{'author_name'},"author",1701 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1702$author) .1703"</$tag>";1704}17051706# format git diff header line, i.e. "diff --(git|combined|cc) ..."1707sub format_git_diff_header_line {1708my$line=shift;1709my$diffinfo=shift;1710my($from,$to) =@_;17111712if($diffinfo->{'nparents'}) {1713# combined diff1714$line=~s!^(diff (.*?) )"?.*$!$1!;1715if($to->{'href'}) {1716$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1717 esc_path($to->{'file'}));1718}else{# file was deleted (no href)1719$line.= esc_path($to->{'file'});1720}1721}else{1722# "ordinary" diff1723$line=~s!^(diff (.*?) )"?a/.*$!$1!;1724if($from->{'href'}) {1725$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1726'a/'. esc_path($from->{'file'}));1727}else{# file was added (no href)1728$line.='a/'. esc_path($from->{'file'});1729}1730$line.=' ';1731if($to->{'href'}) {1732$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1733'b/'. esc_path($to->{'file'}));1734}else{# file was deleted1735$line.='b/'. esc_path($to->{'file'});1736}1737}17381739return"<div class=\"diff header\">$line</div>\n";1740}17411742# format extended diff header line, before patch itself1743sub format_extended_diff_header_line {1744my$line=shift;1745my$diffinfo=shift;1746my($from,$to) =@_;17471748# match <path>1749if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1750$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1751 esc_path($from->{'file'}));1752}1753if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1754$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1755 esc_path($to->{'file'}));1756}1757# match single <mode>1758if($line=~m/\s(\d{6})$/) {1759$line.='<span class="info"> ('.1760 file_type_long($1) .1761')</span>';1762}1763# match <hash>1764if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1765# can match only for combined diff1766$line='index ';1767for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1768if($from->{'href'}[$i]) {1769$line.=$cgi->a({-href=>$from->{'href'}[$i],1770-class=>"hash"},1771substr($diffinfo->{'from_id'}[$i],0,7));1772}else{1773$line.='0' x 7;1774}1775# separator1776$line.=','if($i<$diffinfo->{'nparents'} -1);1777}1778$line.='..';1779if($to->{'href'}) {1780$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1781substr($diffinfo->{'to_id'},0,7));1782}else{1783$line.='0' x 7;1784}17851786}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1787# can match only for ordinary diff1788my($from_link,$to_link);1789if($from->{'href'}) {1790$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1791substr($diffinfo->{'from_id'},0,7));1792}else{1793$from_link='0' x 7;1794}1795if($to->{'href'}) {1796$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1797substr($diffinfo->{'to_id'},0,7));1798}else{1799$to_link='0' x 7;1800}1801my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1802$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1803}18041805return$line."<br/>\n";1806}18071808# format from-file/to-file diff header1809sub format_diff_from_to_header {1810my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1811my$line;1812my$result='';18131814$line=$from_line;1815#assert($line =~ m/^---/) if DEBUG;1816# no extra formatting for "^--- /dev/null"1817if(!$diffinfo->{'nparents'}) {1818# ordinary (single parent) diff1819if($line=~m!^--- "?a/!) {1820if($from->{'href'}) {1821$line='--- a/'.1822$cgi->a({-href=>$from->{'href'}, -class=>"path"},1823 esc_path($from->{'file'}));1824}else{1825$line='--- a/'.1826 esc_path($from->{'file'});1827}1828}1829$result.= qq!<div class="diff from_file">$line</div>\n!;18301831}else{1832# combined diff (merge commit)1833for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1834if($from->{'href'}[$i]) {1835$line='--- '.1836$cgi->a({-href=>href(action=>"blobdiff",1837 hash_parent=>$diffinfo->{'from_id'}[$i],1838 hash_parent_base=>$parents[$i],1839 file_parent=>$from->{'file'}[$i],1840 hash=>$diffinfo->{'to_id'},1841 hash_base=>$hash,1842 file_name=>$to->{'file'}),1843-class=>"path",1844-title=>"diff". ($i+1)},1845$i+1) .1846'/'.1847$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1848 esc_path($from->{'file'}[$i]));1849}else{1850$line='--- /dev/null';1851}1852$result.= qq!<div class="diff from_file">$line</div>\n!;1853}1854}18551856$line=$to_line;1857#assert($line =~ m/^\+\+\+/) if DEBUG;1858# no extra formatting for "^+++ /dev/null"1859if($line=~m!^\+\+\+ "?b/!) {1860if($to->{'href'}) {1861$line='+++ b/'.1862$cgi->a({-href=>$to->{'href'}, -class=>"path"},1863 esc_path($to->{'file'}));1864}else{1865$line='+++ b/'.1866 esc_path($to->{'file'});1867}1868}1869$result.= qq!<div class="diff to_file">$line</div>\n!;18701871return$result;1872}18731874# create note for patch simplified by combined diff1875sub format_diff_cc_simplified {1876my($diffinfo,@parents) =@_;1877my$result='';18781879$result.="<div class=\"diff header\">".1880"diff --cc ";1881if(!is_deleted($diffinfo)) {1882$result.=$cgi->a({-href => href(action=>"blob",1883 hash_base=>$hash,1884 hash=>$diffinfo->{'to_id'},1885 file_name=>$diffinfo->{'to_file'}),1886-class=>"path"},1887 esc_path($diffinfo->{'to_file'}));1888}else{1889$result.= esc_path($diffinfo->{'to_file'});1890}1891$result.="</div>\n".# class="diff header"1892"<div class=\"diff nodifferences\">".1893"Simple merge".1894"</div>\n";# class="diff nodifferences"18951896return$result;1897}18981899# format patch (diff) line (not to be used for diff headers)1900sub format_diff_line {1901my$line=shift;1902my($from,$to) =@_;1903my$diff_class="";19041905chomp$line;19061907if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1908# combined diff1909my$prefix=substr($line,0,scalar@{$from->{'href'}});1910if($line=~m/^\@{3}/) {1911$diff_class=" chunk_header";1912}elsif($line=~m/^\\/) {1913$diff_class=" incomplete";1914}elsif($prefix=~tr/+/+/) {1915$diff_class=" add";1916}elsif($prefix=~tr/-/-/) {1917$diff_class=" rem";1918}1919}else{1920# assume ordinary diff1921my$char=substr($line,0,1);1922if($chareq'+') {1923$diff_class=" add";1924}elsif($chareq'-') {1925$diff_class=" rem";1926}elsif($chareq'@') {1927$diff_class=" chunk_header";1928}elsif($chareq"\\") {1929$diff_class=" incomplete";1930}1931}1932$line= untabify($line);1933if($from&&$to&&$line=~m/^\@{2} /) {1934my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1935$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;19361937$from_lines=0unlessdefined$from_lines;1938$to_lines=0unlessdefined$to_lines;19391940if($from->{'href'}) {1941$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1942-class=>"list"},$from_text);1943}1944if($to->{'href'}) {1945$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1946-class=>"list"},$to_text);1947}1948$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1949"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1950return"<div class=\"diff$diff_class\">$line</div>\n";1951}elsif($from&&$to&&$line=~m/^\@{3}/) {1952my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1953my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);19541955@from_text=split(' ',$ranges);1956for(my$i=0;$i<@from_text; ++$i) {1957($from_start[$i],$from_nlines[$i]) =1958(split(',',substr($from_text[$i],1)),0);1959}19601961$to_text=pop@from_text;1962$to_start=pop@from_start;1963$to_nlines=pop@from_nlines;19641965$line="<span class=\"chunk_info\">$prefix";1966for(my$i=0;$i<@from_text; ++$i) {1967if($from->{'href'}[$i]) {1968$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1969-class=>"list"},$from_text[$i]);1970}else{1971$line.=$from_text[$i];1972}1973$line.=" ";1974}1975if($to->{'href'}) {1976$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1977-class=>"list"},$to_text);1978}else{1979$line.=$to_text;1980}1981$line.="$prefix</span>".1982"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1983return"<div class=\"diff$diff_class\">$line</div>\n";1984}1985return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1986}19871988# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1989# linked. Pass the hash of the tree/commit to snapshot.1990sub format_snapshot_links {1991my($hash) =@_;1992my$num_fmts=@snapshot_fmts;1993if($num_fmts>1) {1994# A parenthesized list of links bearing format names.1995# e.g. "snapshot (_tar.gz_ _zip_)"1996return"snapshot (".join(' ',map1997$cgi->a({1998-href => href(1999 action=>"snapshot",2000 hash=>$hash,2001 snapshot_format=>$_2002)2003},$known_snapshot_formats{$_}{'display'})2004,@snapshot_fmts) .")";2005}elsif($num_fmts==1) {2006# A single "snapshot" link whose tooltip bears the format name.2007# i.e. "_snapshot_"2008my($fmt) =@snapshot_fmts;2009return2010$cgi->a({2011-href => href(2012 action=>"snapshot",2013 hash=>$hash,2014 snapshot_format=>$fmt2015),2016-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2017},"snapshot");2018}else{# $num_fmts == 02019returnundef;2020}2021}20222023## ......................................................................2024## functions returning values to be passed, perhaps after some2025## transformation, to other functions; e.g. returning arguments to href()20262027# returns hash to be passed to href to generate gitweb URL2028# in -title key it returns description of link2029sub get_feed_info {2030my$format=shift||'Atom';2031my%res= (action =>lc($format));20322033# feed links are possible only for project views2034return unless(defined$project);2035# some views should link to OPML, or to generic project feed,2036# or don't have specific feed yet (so they should use generic)2037return if($action=~/^(?:tags|heads|forks|tag|search)$/x);20382039my$branch;2040# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2041# from tag links; this also makes possible to detect branch links2042if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2043(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2044$branch=$1;2045}2046# find log type for feed description (title)2047my$type='log';2048if(defined$file_name) {2049$type="history of$file_name";2050$type.="/"if($actioneq'tree');2051$type.=" on '$branch'"if(defined$branch);2052}else{2053$type="log of$branch"if(defined$branch);2054}20552056$res{-title} =$type;2057$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2058$res{'file_name'} =$file_name;20592060return%res;2061}20622063## ----------------------------------------------------------------------2064## git utility subroutines, invoking git commands20652066# returns path to the core git executable and the --git-dir parameter as list2067sub git_cmd {2068$number_of_git_cmds++;2069return$GIT,'--git-dir='.$git_dir;2070}20712072# quote the given arguments for passing them to the shell2073# quote_command("command", "arg 1", "arg with ' and ! characters")2074# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2075# Try to avoid using this function wherever possible.2076sub quote_command {2077returnjoin(' ',2078map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2079}20802081# get HEAD ref of given project as hash2082sub git_get_head_hash {2083return git_get_full_hash(shift,'HEAD');2084}20852086sub git_get_full_hash {2087return git_get_hash(@_);2088}20892090sub git_get_short_hash {2091return git_get_hash(@_,'--short=7');2092}20932094sub git_get_hash {2095my($project,$hash,@options) =@_;2096my$o_git_dir=$git_dir;2097my$retval=undef;2098$git_dir="$projectroot/$project";2099if(open my$fd,'-|', git_cmd(),'rev-parse',2100'--verify','-q',@options,$hash) {2101$retval= <$fd>;2102chomp$retvalifdefined$retval;2103close$fd;2104}2105if(defined$o_git_dir) {2106$git_dir=$o_git_dir;2107}2108return$retval;2109}21102111# get type of given object2112sub git_get_type {2113my$hash=shift;21142115open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2116my$type= <$fd>;2117close$fdorreturn;2118chomp$type;2119return$type;2120}21212122# repository configuration2123our$config_file='';2124our%config;21252126# store multiple values for single key as anonymous array reference2127# single values stored directly in the hash, not as [ <value> ]2128sub hash_set_multi {2129my($hash,$key,$value) =@_;21302131if(!exists$hash->{$key}) {2132$hash->{$key} =$value;2133}elsif(!ref$hash->{$key}) {2134$hash->{$key} = [$hash->{$key},$value];2135}else{2136push@{$hash->{$key}},$value;2137}2138}21392140# return hash of git project configuration2141# optionally limited to some section, e.g. 'gitweb'2142sub git_parse_project_config {2143my$section_regexp=shift;2144my%config;21452146local$/="\0";21472148open my$fh,"-|", git_cmd(),"config",'-z','-l',2149orreturn;21502151while(my$keyval= <$fh>) {2152chomp$keyval;2153my($key,$value) =split(/\n/,$keyval,2);21542155 hash_set_multi(\%config,$key,$value)2156if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2157}2158close$fh;21592160return%config;2161}21622163# convert config value to boolean: 'true' or 'false'2164# no value, number > 0, 'true' and 'yes' values are true2165# rest of values are treated as false (never as error)2166sub config_to_bool {2167my$val=shift;21682169return1if!defined$val;# section.key21702171# strip leading and trailing whitespace2172$val=~s/^\s+//;2173$val=~s/\s+$//;21742175return(($val=~/^\d+$/&&$val) ||# section.key = 12176($val=~/^(?:true|yes)$/i));# section.key = true2177}21782179# convert config value to simple decimal number2180# an optional value suffix of 'k', 'm', or 'g' will cause the value2181# to be multiplied by 1024, 1048576, or 10737418242182sub config_to_int {2183my$val=shift;21842185# strip leading and trailing whitespace2186$val=~s/^\s+//;2187$val=~s/\s+$//;21882189if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2190$unit=lc($unit);2191# unknown unit is treated as 12192return$num* ($uniteq'g'?1073741824:2193$uniteq'm'?1048576:2194$uniteq'k'?1024:1);2195}2196return$val;2197}21982199# convert config value to array reference, if needed2200sub config_to_multi {2201my$val=shift;22022203returnref($val) ?$val: (defined($val) ? [$val] : []);2204}22052206sub git_get_project_config {2207my($key,$type) =@_;22082209# key sanity check2210return unless($key);2211$key=~s/^gitweb\.//;2212return if($key=~m/\W/);22132214# type sanity check2215if(defined$type) {2216$type=~s/^--//;2217$type=undef2218unless($typeeq'bool'||$typeeq'int');2219}22202221# get config2222if(!defined$config_file||2223$config_filene"$git_dir/config") {2224%config= git_parse_project_config('gitweb');2225$config_file="$git_dir/config";2226}22272228# check if config variable (key) exists2229return unlessexists$config{"gitweb.$key"};22302231# ensure given type2232if(!defined$type) {2233return$config{"gitweb.$key"};2234}elsif($typeeq'bool') {2235# backward compatibility: 'git config --bool' returns true/false2236return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2237}elsif($typeeq'int') {2238return config_to_int($config{"gitweb.$key"});2239}2240return$config{"gitweb.$key"};2241}22422243# get hash of given path at given ref2244sub git_get_hash_by_path {2245my$base=shift;2246my$path=shift||returnundef;2247my$type=shift;22482249$path=~ s,/+$,,;22502251open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2252or die_error(500,"Open git-ls-tree failed");2253my$line= <$fd>;2254close$fdorreturnundef;22552256if(!defined$line) {2257# there is no tree or hash given by $path at $base2258returnundef;2259}22602261#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2262$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2263if(defined$type&&$typene$2) {2264# type doesn't match2265returnundef;2266}2267return$3;2268}22692270# get path of entry with given hash at given tree-ish (ref)2271# used to get 'from' filename for combined diff (merge commit) for renames2272sub git_get_path_by_hash {2273my$base=shift||return;2274my$hash=shift||return;22752276local$/="\0";22772278open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2279orreturnundef;2280while(my$line= <$fd>) {2281chomp$line;22822283#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2284#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2285if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2286close$fd;2287return$1;2288}2289}2290close$fd;2291returnundef;2292}22932294## ......................................................................2295## git utility functions, directly accessing git repository22962297sub git_get_project_description {2298my$path=shift;22992300$git_dir="$projectroot/$path";2301open my$fd,'<',"$git_dir/description"2302orreturn git_get_project_config('description');2303my$descr= <$fd>;2304close$fd;2305if(defined$descr) {2306chomp$descr;2307}2308return$descr;2309}23102311sub git_get_project_ctags {2312my$path=shift;2313my$ctags= {};23142315$git_dir="$projectroot/$path";2316opendir my$dh,"$git_dir/ctags"2317orreturn$ctags;2318foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2319open my$ct,'<',$_ornext;2320my$val= <$ct>;2321chomp$val;2322close$ct;2323my$ctag=$_;$ctag=~ s#.*/##;2324$ctags->{$ctag} =$val;2325}2326closedir$dh;2327$ctags;2328}23292330sub git_populate_project_tagcloud {2331my$ctags=shift;23322333# First, merge different-cased tags; tags vote on casing2334my%ctags_lc;2335foreach(keys%$ctags) {2336$ctags_lc{lc$_}->{count} +=$ctags->{$_};2337if(not$ctags_lc{lc$_}->{topcount}2338or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2339$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2340$ctags_lc{lc$_}->{topname} =$_;2341}2342}23432344my$cloud;2345if(eval{require HTML::TagCloud;1; }) {2346$cloud= HTML::TagCloud->new;2347foreach(sort keys%ctags_lc) {2348# Pad the title with spaces so that the cloud looks2349# less crammed.2350my$title=$ctags_lc{$_}->{topname};2351$title=~s/ / /g;2352$title=~s/^/ /g;2353$title=~s/$/ /g;2354$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2355}2356}else{2357$cloud= \%ctags_lc;2358}2359$cloud;2360}23612362sub git_show_project_tagcloud {2363my($cloud,$count) =@_;2364print STDERR ref($cloud)."..\n";2365if(ref$cloudeq'HTML::TagCloud') {2366return$cloud->html_and_css($count);2367}else{2368my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2369return'<p align="center">'.join(', ',map{2370"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2371}splice(@tags,0,$count)) .'</p>';2372}2373}23742375sub git_get_project_url_list {2376my$path=shift;23772378$git_dir="$projectroot/$path";2379open my$fd,'<',"$git_dir/cloneurl"2380orreturnwantarray?2381@{ config_to_multi(git_get_project_config('url')) } :2382 config_to_multi(git_get_project_config('url'));2383my@git_project_url_list=map{chomp;$_} <$fd>;2384close$fd;23852386returnwantarray?@git_project_url_list: \@git_project_url_list;2387}23882389sub git_get_projects_list {2390my($filter) =@_;2391my@list;23922393$filter||='';2394$filter=~s/\.git$//;23952396my$check_forks= gitweb_check_feature('forks');23972398if(-d $projects_list) {2399# search in directory2400my$dir=$projects_list. ($filter?"/$filter":'');2401# remove the trailing "/"2402$dir=~s!/+$!!;2403my$pfxlen=length("$dir");2404my$pfxdepth= ($dir=~tr!/!!);24052406 File::Find::find({2407 follow_fast =>1,# follow symbolic links2408 follow_skip =>2,# ignore duplicates2409 dangling_symlinks =>0,# ignore dangling symlinks, silently2410 wanted =>sub{2411# skip project-list toplevel, if we get it.2412return if(m!^[/.]$!);2413# only directories can be git repositories2414return unless(-d $_);2415# don't traverse too deep (Find is super slow on os x)2416if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2417$File::Find::prune =1;2418return;2419}24202421my$subdir=substr($File::Find::name,$pfxlen+1);2422# we check related file in $projectroot2423my$path= ($filter?"$filter/":'') .$subdir;2424if(check_export_ok("$projectroot/$path")) {2425push@list, { path =>$path};2426$File::Find::prune =1;2427}2428},2429},"$dir");24302431}elsif(-f $projects_list) {2432# read from file(url-encoded):2433# 'git%2Fgit.git Linus+Torvalds'2434# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2435# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2436my%paths;2437open my$fd,'<',$projects_listorreturn;2438 PROJECT:2439while(my$line= <$fd>) {2440chomp$line;2441my($path,$owner) =split' ',$line;2442$path= unescape($path);2443$owner= unescape($owner);2444if(!defined$path) {2445next;2446}2447if($filterne'') {2448# looking for forks;2449my$pfx=substr($path,0,length($filter));2450if($pfxne$filter) {2451next PROJECT;2452}2453my$sfx=substr($path,length($filter));2454if($sfx!~/^\/.*\.git$/) {2455next PROJECT;2456}2457}elsif($check_forks) {2458 PATH:2459foreachmy$filter(keys%paths) {2460# looking for forks;2461my$pfx=substr($path,0,length($filter));2462if($pfxne$filter) {2463next PATH;2464}2465my$sfx=substr($path,length($filter));2466if($sfx!~/^\/.*\.git$/) {2467next PATH;2468}2469# is a fork, don't include it in2470# the list2471next PROJECT;2472}2473}2474if(check_export_ok("$projectroot/$path")) {2475my$pr= {2476 path =>$path,2477 owner => to_utf8($owner),2478};2479push@list,$pr;2480(my$forks_path=$path) =~s/\.git$//;2481$paths{$forks_path}++;2482}2483}2484close$fd;2485}2486return@list;2487}24882489our$gitweb_project_owner=undef;2490sub git_get_project_list_from_file {24912492return if(defined$gitweb_project_owner);24932494$gitweb_project_owner= {};2495# read from file (url-encoded):2496# 'git%2Fgit.git Linus+Torvalds'2497# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2498# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2499if(-f $projects_list) {2500open(my$fd,'<',$projects_list);2501while(my$line= <$fd>) {2502chomp$line;2503my($pr,$ow) =split' ',$line;2504$pr= unescape($pr);2505$ow= unescape($ow);2506$gitweb_project_owner->{$pr} = to_utf8($ow);2507}2508close$fd;2509}2510}25112512sub git_get_project_owner {2513my$project=shift;2514my$owner;25152516returnundefunless$project;2517$git_dir="$projectroot/$project";25182519if(!defined$gitweb_project_owner) {2520 git_get_project_list_from_file();2521}25222523if(exists$gitweb_project_owner->{$project}) {2524$owner=$gitweb_project_owner->{$project};2525}2526if(!defined$owner){2527$owner= git_get_project_config('owner');2528}2529if(!defined$owner) {2530$owner= get_file_owner("$git_dir");2531}25322533return$owner;2534}25352536sub git_get_last_activity {2537my($path) =@_;2538my$fd;25392540$git_dir="$projectroot/$path";2541open($fd,"-|", git_cmd(),'for-each-ref',2542'--format=%(committer)',2543'--sort=-committerdate',2544'--count=1',2545'refs/heads')orreturn;2546my$most_recent= <$fd>;2547close$fdorreturn;2548if(defined$most_recent&&2549$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2550my$timestamp=$1;2551my$age=time-$timestamp;2552return($age, age_string($age));2553}2554return(undef,undef);2555}25562557sub git_get_references {2558my$type=shift||"";2559my%refs;2560# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112561# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2562open my$fd,"-|", git_cmd(),"show-ref","--dereference",2563($type? ("--","refs/$type") : ())# use -- <pattern> if $type2564orreturn;25652566while(my$line= <$fd>) {2567chomp$line;2568if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2569if(defined$refs{$1}) {2570push@{$refs{$1}},$2;2571}else{2572$refs{$1} = [$2];2573}2574}2575}2576close$fdorreturn;2577return \%refs;2578}25792580sub git_get_rev_name_tags {2581my$hash=shift||returnundef;25822583open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2584orreturn;2585my$name_rev= <$fd>;2586close$fd;25872588if($name_rev=~ m|^$hash tags/(.*)$|) {2589return$1;2590}else{2591# catches also '$hash undefined' output2592returnundef;2593}2594}25952596## ----------------------------------------------------------------------2597## parse to hash functions25982599sub parse_date {2600my$epoch=shift;2601my$tz=shift||"-0000";26022603my%date;2604my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2605my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2606my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2607$date{'hour'} =$hour;2608$date{'minute'} =$min;2609$date{'mday'} =$mday;2610$date{'day'} =$days[$wday];2611$date{'month'} =$months[$mon];2612$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2613$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2614$date{'mday-time'} =sprintf"%d%s%02d:%02d",2615$mday,$months[$mon],$hour,$min;2616$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",26171900+$year,1+$mon,$mday,$hour,$min,$sec;26182619$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2620my$local=$epoch+ ((int$1+ ($2/60)) *3600);2621($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2622$date{'hour_local'} =$hour;2623$date{'minute_local'} =$min;2624$date{'tz_local'} =$tz;2625$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",26261900+$year,$mon+1,$mday,2627$hour,$min,$sec,$tz);2628return%date;2629}26302631sub parse_tag {2632my$tag_id=shift;2633my%tag;2634my@comment;26352636open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2637$tag{'id'} =$tag_id;2638while(my$line= <$fd>) {2639chomp$line;2640if($line=~m/^object ([0-9a-fA-F]{40})$/) {2641$tag{'object'} =$1;2642}elsif($line=~m/^type (.+)$/) {2643$tag{'type'} =$1;2644}elsif($line=~m/^tag (.+)$/) {2645$tag{'name'} =$1;2646}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2647$tag{'author'} =$1;2648$tag{'author_epoch'} =$2;2649$tag{'author_tz'} =$3;2650if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2651$tag{'author_name'} =$1;2652$tag{'author_email'} =$2;2653}else{2654$tag{'author_name'} =$tag{'author'};2655}2656}elsif($line=~m/--BEGIN/) {2657push@comment,$line;2658last;2659}elsif($lineeq"") {2660last;2661}2662}2663push@comment, <$fd>;2664$tag{'comment'} = \@comment;2665close$fdorreturn;2666if(!defined$tag{'name'}) {2667return2668};2669return%tag2670}26712672sub parse_commit_text {2673my($commit_text,$withparents) =@_;2674my@commit_lines=split'\n',$commit_text;2675my%co;26762677pop@commit_lines;# Remove '\0'26782679if(!@commit_lines) {2680return;2681}26822683my$header=shift@commit_lines;2684if($header!~m/^[0-9a-fA-F]{40}/) {2685return;2686}2687($co{'id'},my@parents) =split' ',$header;2688while(my$line=shift@commit_lines) {2689last if$lineeq"\n";2690if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2691$co{'tree'} =$1;2692}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2693push@parents,$1;2694}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2695$co{'author'} = to_utf8($1);2696$co{'author_epoch'} =$2;2697$co{'author_tz'} =$3;2698if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2699$co{'author_name'} =$1;2700$co{'author_email'} =$2;2701}else{2702$co{'author_name'} =$co{'author'};2703}2704}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2705$co{'committer'} = to_utf8($1);2706$co{'committer_epoch'} =$2;2707$co{'committer_tz'} =$3;2708if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2709$co{'committer_name'} =$1;2710$co{'committer_email'} =$2;2711}else{2712$co{'committer_name'} =$co{'committer'};2713}2714}2715}2716if(!defined$co{'tree'}) {2717return;2718};2719$co{'parents'} = \@parents;2720$co{'parent'} =$parents[0];27212722foreachmy$title(@commit_lines) {2723$title=~s/^ //;2724if($titlene"") {2725$co{'title'} = chop_str($title,80,5);2726# remove leading stuff of merges to make the interesting part visible2727if(length($title) >50) {2728$title=~s/^Automatic //;2729$title=~s/^merge (of|with) /Merge ... /i;2730if(length($title) >50) {2731$title=~s/(http|rsync):\/\///;2732}2733if(length($title) >50) {2734$title=~s/(master|www|rsync)\.//;2735}2736if(length($title) >50) {2737$title=~s/kernel.org:?//;2738}2739if(length($title) >50) {2740$title=~s/\/pub\/scm//;2741}2742}2743$co{'title_short'} = chop_str($title,50,5);2744last;2745}2746}2747if(!defined$co{'title'} ||$co{'title'}eq"") {2748$co{'title'} =$co{'title_short'} ='(no commit message)';2749}2750# remove added spaces2751foreachmy$line(@commit_lines) {2752$line=~s/^ //;2753}2754$co{'comment'} = \@commit_lines;27552756my$age=time-$co{'committer_epoch'};2757$co{'age'} =$age;2758$co{'age_string'} = age_string($age);2759my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2760if($age>60*60*24*7*2) {2761$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2762$co{'age_string_age'} =$co{'age_string'};2763}else{2764$co{'age_string_date'} =$co{'age_string'};2765$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2766}2767return%co;2768}27692770sub parse_commit {2771my($commit_id) =@_;2772my%co;27732774local$/="\0";27752776open my$fd,"-|", git_cmd(),"rev-list",2777"--parents",2778"--header",2779"--max-count=1",2780$commit_id,2781"--",2782or die_error(500,"Open git-rev-list failed");2783%co= parse_commit_text(<$fd>,1);2784close$fd;27852786return%co;2787}27882789sub parse_commits {2790my($commit_id,$maxcount,$skip,$filename,@args) =@_;2791my@cos;27922793$maxcount||=1;2794$skip||=0;27952796local$/="\0";27972798open my$fd,"-|", git_cmd(),"rev-list",2799"--header",2800@args,2801("--max-count=".$maxcount),2802("--skip=".$skip),2803@extra_options,2804$commit_id,2805"--",2806($filename? ($filename) : ())2807or die_error(500,"Open git-rev-list failed");2808while(my$line= <$fd>) {2809my%co= parse_commit_text($line);2810push@cos, \%co;2811}2812close$fd;28132814returnwantarray?@cos: \@cos;2815}28162817# parse line of git-diff-tree "raw" output2818sub parse_difftree_raw_line {2819my$line=shift;2820my%res;28212822# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2823# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2824if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2825$res{'from_mode'} =$1;2826$res{'to_mode'} =$2;2827$res{'from_id'} =$3;2828$res{'to_id'} =$4;2829$res{'status'} =$5;2830$res{'similarity'} =$6;2831if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2832($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2833}else{2834$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2835}2836}2837# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2838# combined diff (for merge commit)2839elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2840$res{'nparents'} =length($1);2841$res{'from_mode'} = [split(' ',$2) ];2842$res{'to_mode'} =pop@{$res{'from_mode'}};2843$res{'from_id'} = [split(' ',$3) ];2844$res{'to_id'} =pop@{$res{'from_id'}};2845$res{'status'} = [split('',$4) ];2846$res{'to_file'} = unquote($5);2847}2848# 'c512b523472485aef4fff9e57b229d9d243c967f'2849elsif($line=~m/^([0-9a-fA-F]{40})$/) {2850$res{'commit'} =$1;2851}28522853returnwantarray?%res: \%res;2854}28552856# wrapper: return parsed line of git-diff-tree "raw" output2857# (the argument might be raw line, or parsed info)2858sub parsed_difftree_line {2859my$line_or_ref=shift;28602861if(ref($line_or_ref)eq"HASH") {2862# pre-parsed (or generated by hand)2863return$line_or_ref;2864}else{2865return parse_difftree_raw_line($line_or_ref);2866}2867}28682869# parse line of git-ls-tree output2870sub parse_ls_tree_line {2871my$line=shift;2872my%opts=@_;2873my%res;28742875if($opts{'-l'}) {2876#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'2877$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;28782879$res{'mode'} =$1;2880$res{'type'} =$2;2881$res{'hash'} =$3;2882$res{'size'} =$4;2883if($opts{'-z'}) {2884$res{'name'} =$5;2885}else{2886$res{'name'} = unquote($5);2887}2888}else{2889#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2890$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;28912892$res{'mode'} =$1;2893$res{'type'} =$2;2894$res{'hash'} =$3;2895if($opts{'-z'}) {2896$res{'name'} =$4;2897}else{2898$res{'name'} = unquote($4);2899}2900}29012902returnwantarray?%res: \%res;2903}29042905# generates _two_ hashes, references to which are passed as 2 and 3 argument2906sub parse_from_to_diffinfo {2907my($diffinfo,$from,$to,@parents) =@_;29082909if($diffinfo->{'nparents'}) {2910# combined diff2911$from->{'file'} = [];2912$from->{'href'} = [];2913 fill_from_file_info($diffinfo,@parents)2914unlessexists$diffinfo->{'from_file'};2915for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2916$from->{'file'}[$i] =2917defined$diffinfo->{'from_file'}[$i] ?2918$diffinfo->{'from_file'}[$i] :2919$diffinfo->{'to_file'};2920if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2921$from->{'href'}[$i] = href(action=>"blob",2922 hash_base=>$parents[$i],2923 hash=>$diffinfo->{'from_id'}[$i],2924 file_name=>$from->{'file'}[$i]);2925}else{2926$from->{'href'}[$i] =undef;2927}2928}2929}else{2930# ordinary (not combined) diff2931$from->{'file'} =$diffinfo->{'from_file'};2932if($diffinfo->{'status'}ne"A") {# not new (added) file2933$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2934 hash=>$diffinfo->{'from_id'},2935 file_name=>$from->{'file'});2936}else{2937delete$from->{'href'};2938}2939}29402941$to->{'file'} =$diffinfo->{'to_file'};2942if(!is_deleted($diffinfo)) {# file exists in result2943$to->{'href'} = href(action=>"blob", hash_base=>$hash,2944 hash=>$diffinfo->{'to_id'},2945 file_name=>$to->{'file'});2946}else{2947delete$to->{'href'};2948}2949}29502951## ......................................................................2952## parse to array of hashes functions29532954sub git_get_heads_list {2955my$limit=shift;2956my@headslist;29572958open my$fd,'-|', git_cmd(),'for-each-ref',2959($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2960'--format=%(objectname) %(refname) %(subject)%00%(committer)',2961'refs/heads'2962orreturn;2963while(my$line= <$fd>) {2964my%ref_item;29652966chomp$line;2967my($refinfo,$committerinfo) =split(/\0/,$line);2968my($hash,$name,$title) =split(' ',$refinfo,3);2969my($committer,$epoch,$tz) =2970($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2971$ref_item{'fullname'} =$name;2972$name=~s!^refs/heads/!!;29732974$ref_item{'name'} =$name;2975$ref_item{'id'} =$hash;2976$ref_item{'title'} =$title||'(no commit message)';2977$ref_item{'epoch'} =$epoch;2978if($epoch) {2979$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2980}else{2981$ref_item{'age'} ="unknown";2982}29832984push@headslist, \%ref_item;2985}2986close$fd;29872988returnwantarray?@headslist: \@headslist;2989}29902991sub git_get_tags_list {2992my$limit=shift;2993my@tagslist;29942995open my$fd,'-|', git_cmd(),'for-each-ref',2996($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2997'--format=%(objectname) %(objecttype) %(refname) '.2998'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2999'refs/tags'3000orreturn;3001while(my$line= <$fd>) {3002my%ref_item;30033004chomp$line;3005my($refinfo,$creatorinfo) =split(/\0/,$line);3006my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3007my($creator,$epoch,$tz) =3008($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3009$ref_item{'fullname'} =$name;3010$name=~s!^refs/tags/!!;30113012$ref_item{'type'} =$type;3013$ref_item{'id'} =$id;3014$ref_item{'name'} =$name;3015if($typeeq"tag") {3016$ref_item{'subject'} =$title;3017$ref_item{'reftype'} =$reftype;3018$ref_item{'refid'} =$refid;3019}else{3020$ref_item{'reftype'} =$type;3021$ref_item{'refid'} =$id;3022}30233024if($typeeq"tag"||$typeeq"commit") {3025$ref_item{'epoch'} =$epoch;3026if($epoch) {3027$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3028}else{3029$ref_item{'age'} ="unknown";3030}3031}30323033push@tagslist, \%ref_item;3034}3035close$fd;30363037returnwantarray?@tagslist: \@tagslist;3038}30393040## ----------------------------------------------------------------------3041## filesystem-related functions30423043sub get_file_owner {3044my$path=shift;30453046my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3047my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3048if(!defined$gcos) {3049returnundef;3050}3051my$owner=$gcos;3052$owner=~s/[,;].*$//;3053return to_utf8($owner);3054}30553056# assume that file exists3057sub insert_file {3058my$filename=shift;30593060open my$fd,'<',$filename;3061print map{ to_utf8($_) } <$fd>;3062close$fd;3063}30643065## ......................................................................3066## mimetype related functions30673068sub mimetype_guess_file {3069my$filename=shift;3070my$mimemap=shift;3071-r $mimemaporreturnundef;30723073my%mimemap;3074open(my$mh,'<',$mimemap)orreturnundef;3075while(<$mh>) {3076next ifm/^#/;# skip comments3077my($mimetype,$exts) =split(/\t+/);3078if(defined$exts) {3079my@exts=split(/\s+/,$exts);3080foreachmy$ext(@exts) {3081$mimemap{$ext} =$mimetype;3082}3083}3084}3085close($mh);30863087$filename=~/\.([^.]*)$/;3088return$mimemap{$1};3089}30903091sub mimetype_guess {3092my$filename=shift;3093my$mime;3094$filename=~/\./orreturnundef;30953096if($mimetypes_file) {3097my$file=$mimetypes_file;3098if($file!~m!^/!) {# if it is relative path3099# it is relative to project3100$file="$projectroot/$project/$file";3101}3102$mime= mimetype_guess_file($filename,$file);3103}3104$mime||= mimetype_guess_file($filename,'/etc/mime.types');3105return$mime;3106}31073108sub blob_mimetype {3109my$fd=shift;3110my$filename=shift;31113112if($filename) {3113my$mime= mimetype_guess($filename);3114$mimeandreturn$mime;3115}31163117# just in case3118return$default_blob_plain_mimetypeunless$fd;31193120if(-T $fd) {3121return'text/plain';3122}elsif(!$filename) {3123return'application/octet-stream';3124}elsif($filename=~m/\.png$/i) {3125return'image/png';3126}elsif($filename=~m/\.gif$/i) {3127return'image/gif';3128}elsif($filename=~m/\.jpe?g$/i) {3129return'image/jpeg';3130}else{3131return'application/octet-stream';3132}3133}31343135sub blob_contenttype {3136my($fd,$file_name,$type) =@_;31373138$type||= blob_mimetype($fd,$file_name);3139if($typeeq'text/plain'&&defined$default_text_plain_charset) {3140$type.="; charset=$default_text_plain_charset";3141}31423143return$type;3144}31453146## ======================================================================3147## functions printing HTML: header, footer, error page31483149sub git_header_html {3150my$status=shift||"200 OK";3151my$expires=shift;31523153my$title="$site_name";3154if(defined$project) {3155$title.=" - ". to_utf8($project);3156if(defined$action) {3157$title.="/$action";3158if(defined$file_name) {3159$title.=" - ". esc_path($file_name);3160if($actioneq"tree"&&$file_name!~ m|/$|) {3161$title.="/";3162}3163}3164}3165}3166my$content_type;3167# require explicit support from the UA if we are to send the page as3168# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3169# we have to do this because MSIE sometimes globs '*/*', pretending to3170# support xhtml+xml but choking when it gets what it asked for.3171if(defined$cgi->http('HTTP_ACCEPT') &&3172$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3173$cgi->Accept('application/xhtml+xml') !=0) {3174$content_type='application/xhtml+xml';3175}else{3176$content_type='text/html';3177}3178print$cgi->header(-type=>$content_type, -charset =>'utf-8',3179-status=>$status, -expires =>$expires);3180my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3181print<<EOF;3182<?xml version="1.0" encoding="utf-8"?>3183<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3184<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3185<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3186<!-- git core binaries version$git_version-->3187<head>3188<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3189<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3190<meta name="robots" content="index, nofollow"/>3191<title>$title</title>3192EOF3193# the stylesheet, favicon etc urls won't work correctly with path_info3194# unless we set the appropriate base URL3195if($ENV{'PATH_INFO'}) {3196print"<base href=\"".esc_url($base_url)."\"/>\n";3197}3198# print out each stylesheet that exist, providing backwards capability3199# for those people who defined $stylesheet in a config file3200if(defined$stylesheet) {3201print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3202}else{3203foreachmy$stylesheet(@stylesheets) {3204next unless$stylesheet;3205print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3206}3207}3208if(defined$project) {3209my%href_params= get_feed_info();3210if(!exists$href_params{'-title'}) {3211$href_params{'-title'} ='log';3212}32133214foreachmy$formatqw(RSS Atom){3215my$type=lc($format);3216my%link_attr= (3217'-rel'=>'alternate',3218'-title'=>"$project-$href_params{'-title'} -$formatfeed",3219'-type'=>"application/$type+xml"3220);32213222$href_params{'action'} =$type;3223$link_attr{'-href'} = href(%href_params);3224print"<link ".3225"rel=\"$link_attr{'-rel'}\"".3226"title=\"$link_attr{'-title'}\"".3227"href=\"$link_attr{'-href'}\"".3228"type=\"$link_attr{'-type'}\"".3229"/>\n";32303231$href_params{'extra_options'} ='--no-merges';3232$link_attr{'-href'} = href(%href_params);3233$link_attr{'-title'} .=' (no merges)';3234print"<link ".3235"rel=\"$link_attr{'-rel'}\"".3236"title=\"$link_attr{'-title'}\"".3237"href=\"$link_attr{'-href'}\"".3238"type=\"$link_attr{'-type'}\"".3239"/>\n";3240}32413242}else{3243printf('<link rel="alternate" title="%sprojects list" '.3244'href="%s" type="text/plain; charset=utf-8" />'."\n",3245$site_name, href(project=>undef, action=>"project_index"));3246printf('<link rel="alternate" title="%sprojects feeds" '.3247'href="%s" type="text/x-opml" />'."\n",3248$site_name, href(project=>undef, action=>"opml"));3249}3250if(defined$favicon) {3251printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3252}32533254print"</head>\n".3255"<body>\n";32563257if(defined$site_header&& -f $site_header) {3258 insert_file($site_header);3259}32603261print"<div class=\"page_header\">\n".3262$cgi->a({-href => esc_url($logo_url),3263-title =>$logo_label},3264qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3265print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3266if(defined$project) {3267print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3268if(defined$action) {3269print" /$action";3270}3271print"\n";3272}3273print"</div>\n";32743275my$have_search= gitweb_check_feature('search');3276if(defined$project&&$have_search) {3277if(!defined$searchtext) {3278$searchtext="";3279}3280my$search_hash;3281if(defined$hash_base) {3282$search_hash=$hash_base;3283}elsif(defined$hash) {3284$search_hash=$hash;3285}else{3286$search_hash="HEAD";3287}3288my$action=$my_uri;3289my$use_pathinfo= gitweb_check_feature('pathinfo');3290if($use_pathinfo) {3291$action.="/".esc_url($project);3292}3293print$cgi->startform(-method=>"get", -action =>$action) .3294"<div class=\"search\">\n".3295(!$use_pathinfo&&3296$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3297$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3298$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3299$cgi->popup_menu(-name =>'st', -default=>'commit',3300-values=> ['commit','grep','author','committer','pickaxe']) .3301$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3302" search:\n",3303$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3304"<span title=\"Extended regular expression\">".3305$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3306-checked =>$search_use_regexp) .3307"</span>".3308"</div>".3309$cgi->end_form() ."\n";3310}3311}33123313sub git_footer_html {3314my$feed_class='rss_logo';33153316print"<div class=\"page_footer\">\n";3317if(defined$project) {3318my$descr= git_get_project_description($project);3319if(defined$descr) {3320print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3321}33223323my%href_params= get_feed_info();3324if(!%href_params) {3325$feed_class.=' generic';3326}3327$href_params{'-title'} ||='log';33283329foreachmy$formatqw(RSS Atom){3330$href_params{'action'} =lc($format);3331print$cgi->a({-href => href(%href_params),3332-title =>"$href_params{'-title'}$formatfeed",3333-class=>$feed_class},$format)."\n";3334}33353336}else{3337print$cgi->a({-href => href(project=>undef, action=>"opml"),3338-class=>$feed_class},"OPML") ." ";3339print$cgi->a({-href => href(project=>undef, action=>"project_index"),3340-class=>$feed_class},"TXT") ."\n";3341}3342print"</div>\n";# class="page_footer"33433344if(defined$t0&& gitweb_check_feature('timed')) {3345print"<div id=\"generating_info\">\n";3346print'This page took '.3347'<span id="generating_time" class="time_span">'.3348 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3349' seconds </span>'.3350' and '.3351'<span id="generating_cmd">'.3352$number_of_git_cmds.3353'</span> git commands '.3354" to generate.\n";3355print"</div>\n";# class="page_footer"3356}33573358if(defined$site_footer&& -f $site_footer) {3359 insert_file($site_footer);3360}33613362print qq!<script type="text/javascript" src="$javascript"></script>\n!;3363if(defined$action&&3364$actioneq'blame_incremental') {3365print qq!<script type="text/javascript">\n!.3366 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3367 qq!"!. href() .qq!");\n!.3368 qq!</script>\n!;3369}elsif(gitweb_check_feature('javascript-actions')) {3370print qq!<script type="text/javascript">\n!.3371 qq!window.onload = fixLinks;\n!.3372 qq!</script>\n!;3373}33743375print"</body>\n".3376"</html>";3377}33783379# die_error(<http_status_code>, <error_message>)3380# Example: die_error(404, 'Hash not found')3381# By convention, use the following status codes (as defined in RFC 2616):3382# 400: Invalid or missing CGI parameters, or3383# requested object exists but has wrong type.3384# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3385# this server or project.3386# 404: Requested object/revision/project doesn't exist.3387# 500: The server isn't configured properly, or3388# an internal error occurred (e.g. failed assertions caused by bugs), or3389# an unknown error occurred (e.g. the git binary died unexpectedly).3390# 503: The server is currently unavailable (because it is overloaded,3391# or down for maintenance). Generally, this is a temporary state.3392sub die_error {3393my$status=shift||500;3394my$error=shift||"Internal server error";33953396my%http_responses= (3397400=>'400 Bad Request',3398403=>'403 Forbidden',3399404=>'404 Not Found',3400500=>'500 Internal Server Error',3401503=>'503 Service Unavailable',3402);3403 git_header_html($http_responses{$status});3404print<<EOF;3405<div class="page_body">3406<br /><br />3407$status-$error3408<br />3409</div>3410EOF3411 git_footer_html();3412exit;3413}34143415## ----------------------------------------------------------------------3416## functions printing or outputting HTML: navigation34173418sub git_print_page_nav {3419my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3420$extra=''if!defined$extra;# pager or formats34213422my@navs=qw(summary shortlog log commit commitdiff tree);3423if($suppress) {3424@navs=grep{$_ne$suppress}@navs;3425}34263427my%arg=map{$_=> {action=>$_} }@navs;3428if(defined$head) {3429for(qw(commit commitdiff)) {3430$arg{$_}{'hash'} =$head;3431}3432if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3433for(qw(shortlog log)) {3434$arg{$_}{'hash'} =$head;3435}3436}3437}34383439$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3440$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;34413442my@actions= gitweb_get_feature('actions');3443my%repl= (3444'%'=>'%',3445'n'=>$project,# project name3446'f'=>$git_dir,# project path within filesystem3447'h'=>$treehead||'',# current hash ('h' parameter)3448'b'=>$treebase||'',# hash base ('hb' parameter)3449);3450while(@actions) {3451my($label,$link,$pos) =splice(@actions,0,3);3452# insert3453@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3454# munch munch3455$link=~s/%([%nfhb])/$repl{$1}/g;3456$arg{$label}{'_href'} =$link;3457}34583459print"<div class=\"page_nav\">\n".3460(join" | ",3461map{$_eq$current?3462$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3463}@navs);3464print"<br/>\n$extra<br/>\n".3465"</div>\n";3466}34673468sub format_paging_nav {3469my($action,$page,$has_next_link) =@_;3470my$paging_nav;347134723473if($page>0) {3474$paging_nav.=3475$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3476" ⋅ ".3477$cgi->a({-href => href(-replay=>1, page=>$page-1),3478-accesskey =>"p", -title =>"Alt-p"},"prev");3479}else{3480$paging_nav.="first ⋅ prev";3481}34823483if($has_next_link) {3484$paging_nav.=" ⋅ ".3485$cgi->a({-href => href(-replay=>1, page=>$page+1),3486-accesskey =>"n", -title =>"Alt-n"},"next");3487}else{3488$paging_nav.=" ⋅ next";3489}34903491return$paging_nav;3492}34933494## ......................................................................3495## functions printing or outputting HTML: div34963497sub git_print_header_div {3498my($action,$title,$hash,$hash_base) =@_;3499my%args= ();35003501$args{'action'} =$action;3502$args{'hash'} =$hashif$hash;3503$args{'hash_base'} =$hash_baseif$hash_base;35043505print"<div class=\"header\">\n".3506$cgi->a({-href => href(%args), -class=>"title"},3507$title?$title:$action) .3508"\n</div>\n";3509}35103511sub print_local_time {3512print format_local_time(@_);3513}35143515sub format_local_time {3516my$localtime='';3517my%date=@_;3518if($date{'hour_local'} <6) {3519$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3520$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3521}else{3522$localtime.=sprintf(" (%02d:%02d%s)",3523$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3524}35253526return$localtime;3527}35283529# Outputs the author name and date in long form3530sub git_print_authorship {3531my$co=shift;3532my%opts=@_;3533my$tag=$opts{-tag} ||'div';3534my$author=$co->{'author_name'};35353536my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3537print"<$tagclass=\"author_date\">".3538 format_search_author($author,"author", esc_html($author)) .3539" [$ad{'rfc2822'}";3540 print_local_time(%ad)if($opts{-localtime});3541print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3542."</$tag>\n";3543}35443545# Outputs table rows containing the full author or committer information,3546# in the format expected for 'commit' view (& similia).3547# Parameters are a commit hash reference, followed by the list of people3548# to output information for. If the list is empty it defalts to both3549# author and committer.3550sub git_print_authorship_rows {3551my$co=shift;3552# too bad we can't use @people = @_ || ('author', 'committer')3553my@people=@_;3554@people= ('author','committer')unless@people;3555foreachmy$who(@people) {3556my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3557print"<tr><td>$who</td><td>".3558 format_search_author($co->{"${who}_name"},$who,3559 esc_html($co->{"${who}_name"})) ." ".3560 format_search_author($co->{"${who}_email"},$who,3561 esc_html("<".$co->{"${who}_email"} .">")) .3562"</td><td rowspan=\"2\">".3563 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3564"</td></tr>\n".3565"<tr>".3566"<td></td><td>$wd{'rfc2822'}";3567 print_local_time(%wd);3568print"</td>".3569"</tr>\n";3570}3571}35723573sub git_print_page_path {3574my$name=shift;3575my$type=shift;3576my$hb=shift;357735783579print"<div class=\"page_path\">";3580print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3581-title =>'tree root'}, to_utf8("[$project]"));3582print" / ";3583if(defined$name) {3584my@dirname=split'/',$name;3585my$basename=pop@dirname;3586my$fullname='';35873588foreachmy$dir(@dirname) {3589$fullname.= ($fullname?'/':'') .$dir;3590print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3591 hash_base=>$hb),3592-title =>$fullname}, esc_path($dir));3593print" / ";3594}3595if(defined$type&&$typeeq'blob') {3596print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3597 hash_base=>$hb),3598-title =>$name}, esc_path($basename));3599}elsif(defined$type&&$typeeq'tree') {3600print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3601 hash_base=>$hb),3602-title =>$name}, esc_path($basename));3603print" / ";3604}else{3605print esc_path($basename);3606}3607}3608print"<br/></div>\n";3609}36103611sub git_print_log {3612my$log=shift;3613my%opts=@_;36143615if($opts{'-remove_title'}) {3616# remove title, i.e. first line of log3617shift@$log;3618}3619# remove leading empty lines3620while(defined$log->[0] &&$log->[0]eq"") {3621shift@$log;3622}36233624# print log3625my$signoff=0;3626my$empty=0;3627foreachmy$line(@$log) {3628if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3629$signoff=1;3630$empty=0;3631if(!$opts{'-remove_signoff'}) {3632print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3633next;3634}else{3635# remove signoff lines3636next;3637}3638}else{3639$signoff=0;3640}36413642# print only one empty line3643# do not print empty line after signoff3644if($lineeq"") {3645next if($empty||$signoff);3646$empty=1;3647}else{3648$empty=0;3649}36503651print format_log_line_html($line) ."<br/>\n";3652}36533654if($opts{'-final_empty_line'}) {3655# end with single empty line3656print"<br/>\n"unless$empty;3657}3658}36593660# return link target (what link points to)3661sub git_get_link_target {3662my$hash=shift;3663my$link_target;36643665# read link3666open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3667orreturn;3668{3669local$/=undef;3670$link_target= <$fd>;3671}3672close$fd3673orreturn;36743675return$link_target;3676}36773678# given link target, and the directory (basedir) the link is in,3679# return target of link relative to top directory (top tree);3680# return undef if it is not possible (including absolute links).3681sub normalize_link_target {3682my($link_target,$basedir) =@_;36833684# absolute symlinks (beginning with '/') cannot be normalized3685return if(substr($link_target,0,1)eq'/');36863687# normalize link target to path from top (root) tree (dir)3688my$path;3689if($basedir) {3690$path=$basedir.'/'.$link_target;3691}else{3692# we are in top (root) tree (dir)3693$path=$link_target;3694}36953696# remove //, /./, and /../3697my@path_parts;3698foreachmy$part(split('/',$path)) {3699# discard '.' and ''3700next if(!$part||$parteq'.');3701# handle '..'3702if($parteq'..') {3703if(@path_parts) {3704pop@path_parts;3705}else{3706# link leads outside repository (outside top dir)3707return;3708}3709}else{3710push@path_parts,$part;3711}3712}3713$path=join('/',@path_parts);37143715return$path;3716}37173718# print tree entry (row of git_tree), but without encompassing <tr> element3719sub git_print_tree_entry {3720my($t,$basedir,$hash_base,$have_blame) =@_;37213722my%base_key= ();3723$base_key{'hash_base'} =$hash_baseifdefined$hash_base;37243725# The format of a table row is: mode list link. Where mode is3726# the mode of the entry, list is the name of the entry, an href,3727# and link is the action links of the entry.37283729print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3730if(exists$t->{'size'}) {3731print"<td class=\"size\">$t->{'size'}</td>\n";3732}3733if($t->{'type'}eq"blob") {3734print"<td class=\"list\">".3735$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3736 file_name=>"$basedir$t->{'name'}",%base_key),3737-class=>"list"}, esc_path($t->{'name'}));3738if(S_ISLNK(oct$t->{'mode'})) {3739my$link_target= git_get_link_target($t->{'hash'});3740if($link_target) {3741my$norm_target= normalize_link_target($link_target,$basedir);3742if(defined$norm_target) {3743print" -> ".3744$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3745 file_name=>$norm_target),3746-title =>$norm_target}, esc_path($link_target));3747}else{3748print" -> ". esc_path($link_target);3749}3750}3751}3752print"</td>\n";3753print"<td class=\"link\">";3754print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3755 file_name=>"$basedir$t->{'name'}",%base_key)},3756"blob");3757if($have_blame) {3758print" | ".3759$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3760 file_name=>"$basedir$t->{'name'}",%base_key)},3761"blame");3762}3763if(defined$hash_base) {3764print" | ".3765$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3766 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3767"history");3768}3769print" | ".3770$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3771 file_name=>"$basedir$t->{'name'}")},3772"raw");3773print"</td>\n";37743775}elsif($t->{'type'}eq"tree") {3776print"<td class=\"list\">";3777print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3778 file_name=>"$basedir$t->{'name'}",3779%base_key)},3780 esc_path($t->{'name'}));3781print"</td>\n";3782print"<td class=\"link\">";3783print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3784 file_name=>"$basedir$t->{'name'}",3785%base_key)},3786"tree");3787if(defined$hash_base) {3788print" | ".3789$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3790 file_name=>"$basedir$t->{'name'}")},3791"history");3792}3793print"</td>\n";3794}else{3795# unknown object: we can only present history for it3796# (this includes 'commit' object, i.e. submodule support)3797print"<td class=\"list\">".3798 esc_path($t->{'name'}) .3799"</td>\n";3800print"<td class=\"link\">";3801if(defined$hash_base) {3802print$cgi->a({-href => href(action=>"history",3803 hash_base=>$hash_base,3804 file_name=>"$basedir$t->{'name'}")},3805"history");3806}3807print"</td>\n";3808}3809}38103811## ......................................................................3812## functions printing large fragments of HTML38133814# get pre-image filenames for merge (combined) diff3815sub fill_from_file_info {3816my($diff,@parents) =@_;38173818$diff->{'from_file'} = [ ];3819$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3820for(my$i=0;$i<$diff->{'nparents'};$i++) {3821if($diff->{'status'}[$i]eq'R'||3822$diff->{'status'}[$i]eq'C') {3823$diff->{'from_file'}[$i] =3824 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3825}3826}38273828return$diff;3829}38303831# is current raw difftree line of file deletion3832sub is_deleted {3833my$diffinfo=shift;38343835return$diffinfo->{'to_id'}eq('0' x 40);3836}38373838# does patch correspond to [previous] difftree raw line3839# $diffinfo - hashref of parsed raw diff format3840# $patchinfo - hashref of parsed patch diff format3841# (the same keys as in $diffinfo)3842sub is_patch_split {3843my($diffinfo,$patchinfo) =@_;38443845returndefined$diffinfo&&defined$patchinfo3846&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3847}384838493850sub git_difftree_body {3851my($difftree,$hash,@parents) =@_;3852my($parent) =$parents[0];3853my$have_blame= gitweb_check_feature('blame');3854print"<div class=\"list_head\">\n";3855if($#{$difftree} >10) {3856print(($#{$difftree} +1) ." files changed:\n");3857}3858print"</div>\n";38593860print"<table class=\"".3861(@parents>1?"combined ":"") .3862"diff_tree\">\n";38633864# header only for combined diff in 'commitdiff' view3865my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3866if($has_header) {3867# table header3868print"<thead><tr>\n".3869"<th></th><th></th>\n";# filename, patchN link3870for(my$i=0;$i<@parents;$i++) {3871my$par=$parents[$i];3872print"<th>".3873$cgi->a({-href => href(action=>"commitdiff",3874 hash=>$hash, hash_parent=>$par),3875-title =>'commitdiff to parent number '.3876($i+1) .': '.substr($par,0,7)},3877$i+1) .3878" </th>\n";3879}3880print"</tr></thead>\n<tbody>\n";3881}38823883my$alternate=1;3884my$patchno=0;3885foreachmy$line(@{$difftree}) {3886my$diff= parsed_difftree_line($line);38873888if($alternate) {3889print"<tr class=\"dark\">\n";3890}else{3891print"<tr class=\"light\">\n";3892}3893$alternate^=1;38943895if(exists$diff->{'nparents'}) {# combined diff38963897 fill_from_file_info($diff,@parents)3898unlessexists$diff->{'from_file'};38993900if(!is_deleted($diff)) {3901# file exists in the result (child) commit3902print"<td>".3903$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3904 file_name=>$diff->{'to_file'},3905 hash_base=>$hash),3906-class=>"list"}, esc_path($diff->{'to_file'})) .3907"</td>\n";3908}else{3909print"<td>".3910 esc_path($diff->{'to_file'}) .3911"</td>\n";3912}39133914if($actioneq'commitdiff') {3915# link to patch3916$patchno++;3917print"<td class=\"link\">".3918$cgi->a({-href =>"#patch$patchno"},"patch") .3919" | ".3920"</td>\n";3921}39223923my$has_history=0;3924my$not_deleted=0;3925for(my$i=0;$i<$diff->{'nparents'};$i++) {3926my$hash_parent=$parents[$i];3927my$from_hash=$diff->{'from_id'}[$i];3928my$from_path=$diff->{'from_file'}[$i];3929my$status=$diff->{'status'}[$i];39303931$has_history||= ($statusne'A');3932$not_deleted||= ($statusne'D');39333934if($statuseq'A') {3935print"<td class=\"link\"align=\"right\"> | </td>\n";3936}elsif($statuseq'D') {3937print"<td class=\"link\">".3938$cgi->a({-href => href(action=>"blob",3939 hash_base=>$hash,3940 hash=>$from_hash,3941 file_name=>$from_path)},3942"blob". ($i+1)) .3943" | </td>\n";3944}else{3945if($diff->{'to_id'}eq$from_hash) {3946print"<td class=\"link nochange\">";3947}else{3948print"<td class=\"link\">";3949}3950print$cgi->a({-href => href(action=>"blobdiff",3951 hash=>$diff->{'to_id'},3952 hash_parent=>$from_hash,3953 hash_base=>$hash,3954 hash_parent_base=>$hash_parent,3955 file_name=>$diff->{'to_file'},3956 file_parent=>$from_path)},3957"diff". ($i+1)) .3958" | </td>\n";3959}3960}39613962print"<td class=\"link\">";3963if($not_deleted) {3964print$cgi->a({-href => href(action=>"blob",3965 hash=>$diff->{'to_id'},3966 file_name=>$diff->{'to_file'},3967 hash_base=>$hash)},3968"blob");3969print" | "if($has_history);3970}3971if($has_history) {3972print$cgi->a({-href => href(action=>"history",3973 file_name=>$diff->{'to_file'},3974 hash_base=>$hash)},3975"history");3976}3977print"</td>\n";39783979print"</tr>\n";3980next;# instead of 'else' clause, to avoid extra indent3981}3982# else ordinary diff39833984my($to_mode_oct,$to_mode_str,$to_file_type);3985my($from_mode_oct,$from_mode_str,$from_file_type);3986if($diff->{'to_mode'}ne('0' x 6)) {3987$to_mode_oct=oct$diff->{'to_mode'};3988if(S_ISREG($to_mode_oct)) {# only for regular file3989$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3990}3991$to_file_type= file_type($diff->{'to_mode'});3992}3993if($diff->{'from_mode'}ne('0' x 6)) {3994$from_mode_oct=oct$diff->{'from_mode'};3995if(S_ISREG($to_mode_oct)) {# only for regular file3996$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3997}3998$from_file_type= file_type($diff->{'from_mode'});3999}40004001if($diff->{'status'}eq"A") {# created4002my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4003$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4004$mode_chng.="]</span>";4005print"<td>";4006print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4007 hash_base=>$hash, file_name=>$diff->{'file'}),4008-class=>"list"}, esc_path($diff->{'file'}));4009print"</td>\n";4010print"<td>$mode_chng</td>\n";4011print"<td class=\"link\">";4012if($actioneq'commitdiff') {4013# link to patch4014$patchno++;4015print$cgi->a({-href =>"#patch$patchno"},"patch");4016print" | ";4017}4018print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4019 hash_base=>$hash, file_name=>$diff->{'file'})},4020"blob");4021print"</td>\n";40224023}elsif($diff->{'status'}eq"D") {# deleted4024my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4025print"<td>";4026print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4027 hash_base=>$parent, file_name=>$diff->{'file'}),4028-class=>"list"}, esc_path($diff->{'file'}));4029print"</td>\n";4030print"<td>$mode_chng</td>\n";4031print"<td class=\"link\">";4032if($actioneq'commitdiff') {4033# link to patch4034$patchno++;4035print$cgi->a({-href =>"#patch$patchno"},"patch");4036print" | ";4037}4038print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4039 hash_base=>$parent, file_name=>$diff->{'file'})},4040"blob") ." | ";4041if($have_blame) {4042print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4043 file_name=>$diff->{'file'})},4044"blame") ." | ";4045}4046print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4047 file_name=>$diff->{'file'})},4048"history");4049print"</td>\n";40504051}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4052my$mode_chnge="";4053if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4054$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4055if($from_file_typene$to_file_type) {4056$mode_chnge.=" from$from_file_typeto$to_file_type";4057}4058if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4059if($from_mode_str&&$to_mode_str) {4060$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4061}elsif($to_mode_str) {4062$mode_chnge.=" mode:$to_mode_str";4063}4064}4065$mode_chnge.="]</span>\n";4066}4067print"<td>";4068print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4069 hash_base=>$hash, file_name=>$diff->{'file'}),4070-class=>"list"}, esc_path($diff->{'file'}));4071print"</td>\n";4072print"<td>$mode_chnge</td>\n";4073print"<td class=\"link\">";4074if($actioneq'commitdiff') {4075# link to patch4076$patchno++;4077print$cgi->a({-href =>"#patch$patchno"},"patch") .4078" | ";4079}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4080# "commit" view and modified file (not onlu mode changed)4081print$cgi->a({-href => href(action=>"blobdiff",4082 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4083 hash_base=>$hash, hash_parent_base=>$parent,4084 file_name=>$diff->{'file'})},4085"diff") .4086" | ";4087}4088print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4089 hash_base=>$hash, file_name=>$diff->{'file'})},4090"blob") ." | ";4091if($have_blame) {4092print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4093 file_name=>$diff->{'file'})},4094"blame") ." | ";4095}4096print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4097 file_name=>$diff->{'file'})},4098"history");4099print"</td>\n";41004101}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4102my%status_name= ('R'=>'moved','C'=>'copied');4103my$nstatus=$status_name{$diff->{'status'}};4104my$mode_chng="";4105if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4106# mode also for directories, so we cannot use $to_mode_str4107$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4108}4109print"<td>".4110$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4111 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4112-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4113"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4114$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4115 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4116-class=>"list"}, esc_path($diff->{'from_file'})) .4117" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4118"<td class=\"link\">";4119if($actioneq'commitdiff') {4120# link to patch4121$patchno++;4122print$cgi->a({-href =>"#patch$patchno"},"patch") .4123" | ";4124}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4125# "commit" view and modified file (not only pure rename or copy)4126print$cgi->a({-href => href(action=>"blobdiff",4127 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4128 hash_base=>$hash, hash_parent_base=>$parent,4129 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4130"diff") .4131" | ";4132}4133print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4134 hash_base=>$parent, file_name=>$diff->{'to_file'})},4135"blob") ." | ";4136if($have_blame) {4137print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4138 file_name=>$diff->{'to_file'})},4139"blame") ." | ";4140}4141print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4142 file_name=>$diff->{'to_file'})},4143"history");4144print"</td>\n";41454146}# we should not encounter Unmerged (U) or Unknown (X) status4147print"</tr>\n";4148}4149print"</tbody>"if$has_header;4150print"</table>\n";4151}41524153sub git_patchset_body {4154my($fd,$difftree,$hash,@hash_parents) =@_;4155my($hash_parent) =$hash_parents[0];41564157my$is_combined= (@hash_parents>1);4158my$patch_idx=0;4159my$patch_number=0;4160my$patch_line;4161my$diffinfo;4162my$to_name;4163my(%from,%to);41644165print"<div class=\"patchset\">\n";41664167# skip to first patch4168while($patch_line= <$fd>) {4169chomp$patch_line;41704171last if($patch_line=~m/^diff /);4172}41734174 PATCH:4175while($patch_line) {41764177# parse "git diff" header line4178if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4179# $1 is from_name, which we do not use4180$to_name= unquote($2);4181$to_name=~s!^b/!!;4182}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4183# $1 is 'cc' or 'combined', which we do not use4184$to_name= unquote($2);4185}else{4186$to_name=undef;4187}41884189# check if current patch belong to current raw line4190# and parse raw git-diff line if needed4191if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4192# this is continuation of a split patch4193print"<div class=\"patch cont\">\n";4194}else{4195# advance raw git-diff output if needed4196$patch_idx++ifdefined$diffinfo;41974198# read and prepare patch information4199$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42004201# compact combined diff output can have some patches skipped4202# find which patch (using pathname of result) we are at now;4203if($is_combined) {4204while($to_namene$diffinfo->{'to_file'}) {4205print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4206 format_diff_cc_simplified($diffinfo,@hash_parents) .4207"</div>\n";# class="patch"42084209$patch_idx++;4210$patch_number++;42114212last if$patch_idx>$#$difftree;4213$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4214}4215}42164217# modifies %from, %to hashes4218 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);42194220# this is first patch for raw difftree line with $patch_idx index4221# we index @$difftree array from 0, but number patches from 14222print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4223}42244225# git diff header4226#assert($patch_line =~ m/^diff /) if DEBUG;4227#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4228$patch_number++;4229# print "git diff" header4230print format_git_diff_header_line($patch_line,$diffinfo,4231 \%from, \%to);42324233# print extended diff header4234print"<div class=\"diff extended_header\">\n";4235 EXTENDED_HEADER:4236while($patch_line= <$fd>) {4237chomp$patch_line;42384239last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);42404241print format_extended_diff_header_line($patch_line,$diffinfo,4242 \%from, \%to);4243}4244print"</div>\n";# class="diff extended_header"42454246# from-file/to-file diff header4247if(!$patch_line) {4248print"</div>\n";# class="patch"4249last PATCH;4250}4251next PATCH if($patch_line=~m/^diff /);4252#assert($patch_line =~ m/^---/) if DEBUG;42534254my$last_patch_line=$patch_line;4255$patch_line= <$fd>;4256chomp$patch_line;4257#assert($patch_line =~ m/^\+\+\+/) if DEBUG;42584259print format_diff_from_to_header($last_patch_line,$patch_line,4260$diffinfo, \%from, \%to,4261@hash_parents);42624263# the patch itself4264 LINE:4265while($patch_line= <$fd>) {4266chomp$patch_line;42674268next PATCH if($patch_line=~m/^diff /);42694270print format_diff_line($patch_line, \%from, \%to);4271}42724273}continue{4274print"</div>\n";# class="patch"4275}42764277# for compact combined (--cc) format, with chunk and patch simpliciaction4278# patchset might be empty, but there might be unprocessed raw lines4279for(++$patch_idxif$patch_number>0;4280$patch_idx<@$difftree;4281++$patch_idx) {4282# read and prepare patch information4283$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42844285# generate anchor for "patch" links in difftree / whatchanged part4286print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4287 format_diff_cc_simplified($diffinfo,@hash_parents) .4288"</div>\n";# class="patch"42894290$patch_number++;4291}42924293if($patch_number==0) {4294if(@hash_parents>1) {4295print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4296}else{4297print"<div class=\"diff nodifferences\">No differences found</div>\n";4298}4299}43004301print"</div>\n";# class="patchset"4302}43034304# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43054306# fills project list info (age, description, owner, forks) for each4307# project in the list, removing invalid projects from returned list4308# NOTE: modifies $projlist, but does not remove entries from it4309sub fill_project_list_info {4310my($projlist,$check_forks) =@_;4311my@projects;43124313my$show_ctags= gitweb_check_feature('ctags');4314 PROJECT:4315foreachmy$pr(@$projlist) {4316my(@activity) = git_get_last_activity($pr->{'path'});4317unless(@activity) {4318next PROJECT;4319}4320($pr->{'age'},$pr->{'age_string'}) =@activity;4321if(!defined$pr->{'descr'}) {4322my$descr= git_get_project_description($pr->{'path'}) ||"";4323$descr= to_utf8($descr);4324$pr->{'descr_long'} =$descr;4325$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4326}4327if(!defined$pr->{'owner'}) {4328$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4329}4330if($check_forks) {4331my$pname=$pr->{'path'};4332if(($pname=~s/\.git$//) &&4333($pname!~/\/$/) &&4334(-d "$projectroot/$pname")) {4335$pr->{'forks'} ="-d$projectroot/$pname";4336}else{4337$pr->{'forks'} =0;4338}4339}4340$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4341push@projects,$pr;4342}43434344return@projects;4345}43464347# print 'sort by' <th> element, generating 'sort by $name' replay link4348# if that order is not selected4349sub print_sort_th {4350my($name,$order,$header) =@_;4351$header||=ucfirst($name);43524353if($ordereq$name) {4354print"<th>$header</th>\n";4355}else{4356print"<th>".4357$cgi->a({-href => href(-replay=>1, order=>$name),4358-class=>"header"},$header) .4359"</th>\n";4360}4361}43624363sub git_project_list_body {4364# actually uses global variable $project4365my($projlist,$order,$from,$to,$extra,$no_header) =@_;43664367my$check_forks= gitweb_check_feature('forks');4368my@projects= fill_project_list_info($projlist,$check_forks);43694370$order||=$default_projects_order;4371$from=0unlessdefined$from;4372$to=$#projectsif(!defined$to||$#projects<$to);43734374my%order_info= (4375 project => { key =>'path', type =>'str'},4376 descr => { key =>'descr_long', type =>'str'},4377 owner => { key =>'owner', type =>'str'},4378 age => { key =>'age', type =>'num'}4379);4380my$oi=$order_info{$order};4381if($oi->{'type'}eq'str') {4382@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4383}else{4384@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4385}43864387my$show_ctags= gitweb_check_feature('ctags');4388if($show_ctags) {4389my%ctags;4390foreachmy$p(@projects) {4391foreachmy$ct(keys%{$p->{'ctags'}}) {4392$ctags{$ct} +=$p->{'ctags'}->{$ct};4393}4394}4395my$cloud= git_populate_project_tagcloud(\%ctags);4396print git_show_project_tagcloud($cloud,64);4397}43984399print"<table class=\"project_list\">\n";4400unless($no_header) {4401print"<tr>\n";4402if($check_forks) {4403print"<th></th>\n";4404}4405 print_sort_th('project',$order,'Project');4406 print_sort_th('descr',$order,'Description');4407 print_sort_th('owner',$order,'Owner');4408 print_sort_th('age',$order,'Last Change');4409print"<th></th>\n".# for links4410"</tr>\n";4411}4412my$alternate=1;4413my$tagfilter=$cgi->param('by_tag');4414for(my$i=$from;$i<=$to;$i++) {4415my$pr=$projects[$i];44164417next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4418next if$searchtextand not$pr->{'path'} =~/$searchtext/4419and not$pr->{'descr_long'} =~/$searchtext/;4420# Weed out forks or non-matching entries of search4421if($check_forks) {4422my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4423$forkbase="^$forkbase"if$forkbase;4424next ifnot$searchtextand not$tagfilterand$show_ctags4425and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4426}44274428if($alternate) {4429print"<tr class=\"dark\">\n";4430}else{4431print"<tr class=\"light\">\n";4432}4433$alternate^=1;4434if($check_forks) {4435print"<td>";4436if($pr->{'forks'}) {4437print"<!--$pr->{'forks'} -->\n";4438print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4439}4440print"</td>\n";4441}4442print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4443-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4444"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4445-class=>"list", -title =>$pr->{'descr_long'}},4446 esc_html($pr->{'descr'})) ."</td>\n".4447"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4448print"<td class=\"". age_class($pr->{'age'}) ."\">".4449(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4450"<td class=\"link\">".4451$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4452$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4453$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4454$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4455($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4456"</td>\n".4457"</tr>\n";4458}4459if(defined$extra) {4460print"<tr>\n";4461if($check_forks) {4462print"<td></td>\n";4463}4464print"<td colspan=\"5\">$extra</td>\n".4465"</tr>\n";4466}4467print"</table>\n";4468}44694470sub git_log_body {4471# uses global variable $project4472my($commitlist,$from,$to,$refs,$extra) =@_;44734474$from=0unlessdefined$from;4475$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);44764477for(my$i=0;$i<=$to;$i++) {4478my%co= %{$commitlist->[$i]};4479next if!%co;4480my$commit=$co{'id'};4481my$ref= format_ref_marker($refs,$commit);4482my%ad= parse_date($co{'author_epoch'});4483 git_print_header_div('commit',4484"<span class=\"age\">$co{'age_string'}</span>".4485 esc_html($co{'title'}) .$ref,4486$commit);4487print"<div class=\"title_text\">\n".4488"<div class=\"log_link\">\n".4489$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4490" | ".4491$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4492" | ".4493$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4494"<br/>\n".4495"</div>\n";4496 git_print_authorship(\%co, -tag =>'span');4497print"<br/>\n</div>\n";44984499print"<div class=\"log_body\">\n";4500 git_print_log($co{'comment'}, -final_empty_line=>1);4501print"</div>\n";4502}4503if($extra) {4504print"<div class=\"page_nav\">\n";4505print"$extra\n";4506print"</div>\n";4507}4508}45094510sub git_shortlog_body {4511# uses global variable $project4512my($commitlist,$from,$to,$refs,$extra) =@_;45134514$from=0unlessdefined$from;4515$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45164517print"<table class=\"shortlog\">\n";4518my$alternate=1;4519for(my$i=$from;$i<=$to;$i++) {4520my%co= %{$commitlist->[$i]};4521my$commit=$co{'id'};4522my$ref= format_ref_marker($refs,$commit);4523if($alternate) {4524print"<tr class=\"dark\">\n";4525}else{4526print"<tr class=\"light\">\n";4527}4528$alternate^=1;4529# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4530print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4531 format_author_html('td', \%co,10) ."<td>";4532print format_subject_html($co{'title'},$co{'title_short'},4533 href(action=>"commit", hash=>$commit),$ref);4534print"</td>\n".4535"<td class=\"link\">".4536$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4537$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4538$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4539my$snapshot_links= format_snapshot_links($commit);4540if(defined$snapshot_links) {4541print" | ".$snapshot_links;4542}4543print"</td>\n".4544"</tr>\n";4545}4546if(defined$extra) {4547print"<tr>\n".4548"<td colspan=\"4\">$extra</td>\n".4549"</tr>\n";4550}4551print"</table>\n";4552}45534554sub git_history_body {4555# Warning: assumes constant type (blob or tree) during history4556my($commitlist,$from,$to,$refs,$extra,4557$file_name,$file_hash,$ftype) =@_;45584559$from=0unlessdefined$from;4560$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});45614562print"<table class=\"history\">\n";4563my$alternate=1;4564for(my$i=$from;$i<=$to;$i++) {4565my%co= %{$commitlist->[$i]};4566if(!%co) {4567next;4568}4569my$commit=$co{'id'};45704571my$ref= format_ref_marker($refs,$commit);45724573if($alternate) {4574print"<tr class=\"dark\">\n";4575}else{4576print"<tr class=\"light\">\n";4577}4578$alternate^=1;4579print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4580# shortlog: format_author_html('td', \%co, 10)4581 format_author_html('td', \%co,15,3) ."<td>";4582# originally git_history used chop_str($co{'title'}, 50)4583print format_subject_html($co{'title'},$co{'title_short'},4584 href(action=>"commit", hash=>$commit),$ref);4585print"</td>\n".4586"<td class=\"link\">".4587$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4588$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");45894590if($ftypeeq'blob') {4591my$blob_current=$file_hash;4592my$blob_parent= git_get_hash_by_path($commit,$file_name);4593if(defined$blob_current&&defined$blob_parent&&4594$blob_currentne$blob_parent) {4595print" | ".4596$cgi->a({-href => href(action=>"blobdiff",4597 hash=>$blob_current, hash_parent=>$blob_parent,4598 hash_base=>$hash_base, hash_parent_base=>$commit,4599 file_name=>$file_name)},4600"diff to current");4601}4602}4603print"</td>\n".4604"</tr>\n";4605}4606if(defined$extra) {4607print"<tr>\n".4608"<td colspan=\"4\">$extra</td>\n".4609"</tr>\n";4610}4611print"</table>\n";4612}46134614sub git_tags_body {4615# uses global variable $project4616my($taglist,$from,$to,$extra) =@_;4617$from=0unlessdefined$from;4618$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);46194620print"<table class=\"tags\">\n";4621my$alternate=1;4622for(my$i=$from;$i<=$to;$i++) {4623my$entry=$taglist->[$i];4624my%tag=%$entry;4625my$comment=$tag{'subject'};4626my$comment_short;4627if(defined$comment) {4628$comment_short= chop_str($comment,30,5);4629}4630if($alternate) {4631print"<tr class=\"dark\">\n";4632}else{4633print"<tr class=\"light\">\n";4634}4635$alternate^=1;4636if(defined$tag{'age'}) {4637print"<td><i>$tag{'age'}</i></td>\n";4638}else{4639print"<td></td>\n";4640}4641print"<td>".4642$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4643-class=>"list name"}, esc_html($tag{'name'})) .4644"</td>\n".4645"<td>";4646if(defined$comment) {4647print format_subject_html($comment,$comment_short,4648 href(action=>"tag", hash=>$tag{'id'}));4649}4650print"</td>\n".4651"<td class=\"selflink\">";4652if($tag{'type'}eq"tag") {4653print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4654}else{4655print" ";4656}4657print"</td>\n".4658"<td class=\"link\">"." | ".4659$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4660if($tag{'reftype'}eq"commit") {4661print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4662" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4663}elsif($tag{'reftype'}eq"blob") {4664print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4665}4666print"</td>\n".4667"</tr>";4668}4669if(defined$extra) {4670print"<tr>\n".4671"<td colspan=\"5\">$extra</td>\n".4672"</tr>\n";4673}4674print"</table>\n";4675}46764677sub git_heads_body {4678# uses global variable $project4679my($headlist,$head,$from,$to,$extra) =@_;4680$from=0unlessdefined$from;4681$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);46824683print"<table class=\"heads\">\n";4684my$alternate=1;4685for(my$i=$from;$i<=$to;$i++) {4686my$entry=$headlist->[$i];4687my%ref=%$entry;4688my$curr=$ref{'id'}eq$head;4689if($alternate) {4690print"<tr class=\"dark\">\n";4691}else{4692print"<tr class=\"light\">\n";4693}4694$alternate^=1;4695print"<td><i>$ref{'age'}</i></td>\n".4696($curr?"<td class=\"current_head\">":"<td>") .4697$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4698-class=>"list name"},esc_html($ref{'name'})) .4699"</td>\n".4700"<td class=\"link\">".4701$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4702$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4703$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4704"</td>\n".4705"</tr>";4706}4707if(defined$extra) {4708print"<tr>\n".4709"<td colspan=\"3\">$extra</td>\n".4710"</tr>\n";4711}4712print"</table>\n";4713}47144715sub git_search_grep_body {4716my($commitlist,$from,$to,$extra) =@_;4717$from=0unlessdefined$from;4718$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47194720print"<table class=\"commit_search\">\n";4721my$alternate=1;4722for(my$i=$from;$i<=$to;$i++) {4723my%co= %{$commitlist->[$i]};4724if(!%co) {4725next;4726}4727my$commit=$co{'id'};4728if($alternate) {4729print"<tr class=\"dark\">\n";4730}else{4731print"<tr class=\"light\">\n";4732}4733$alternate^=1;4734print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4735 format_author_html('td', \%co,15,5) .4736"<td>".4737$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4738-class=>"list subject"},4739 chop_and_escape_str($co{'title'},50) ."<br/>");4740my$comment=$co{'comment'};4741foreachmy$line(@$comment) {4742if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4743my($lead,$match,$trail) = ($1,$2,$3);4744$match= chop_str($match,70,5,'center');4745my$contextlen=int((80-length($match))/2);4746$contextlen=30if($contextlen>30);4747$lead= chop_str($lead,$contextlen,10,'left');4748$trail= chop_str($trail,$contextlen,10,'right');47494750$lead= esc_html($lead);4751$match= esc_html($match);4752$trail= esc_html($trail);47534754print"$lead<span class=\"match\">$match</span>$trail<br />";4755}4756}4757print"</td>\n".4758"<td class=\"link\">".4759$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4760" | ".4761$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4762" | ".4763$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4764print"</td>\n".4765"</tr>\n";4766}4767if(defined$extra) {4768print"<tr>\n".4769"<td colspan=\"3\">$extra</td>\n".4770"</tr>\n";4771}4772print"</table>\n";4773}47744775## ======================================================================4776## ======================================================================4777## actions47784779sub git_project_list {4780my$order=$input_params{'order'};4781if(defined$order&&$order!~m/none|project|descr|owner|age/) {4782 die_error(400,"Unknown order parameter");4783}47844785my@list= git_get_projects_list();4786if(!@list) {4787 die_error(404,"No projects found");4788}47894790 git_header_html();4791if(defined$home_text&& -f $home_text) {4792print"<div class=\"index_include\">\n";4793 insert_file($home_text);4794print"</div>\n";4795}4796print$cgi->startform(-method=>"get") .4797"<p class=\"projsearch\">Search:\n".4798$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4799"</p>".4800$cgi->end_form() ."\n";4801 git_project_list_body(\@list,$order);4802 git_footer_html();4803}48044805sub git_forks {4806my$order=$input_params{'order'};4807if(defined$order&&$order!~m/none|project|descr|owner|age/) {4808 die_error(400,"Unknown order parameter");4809}48104811my@list= git_get_projects_list($project);4812if(!@list) {4813 die_error(404,"No forks found");4814}48154816 git_header_html();4817 git_print_page_nav('','');4818 git_print_header_div('summary',"$projectforks");4819 git_project_list_body(\@list,$order);4820 git_footer_html();4821}48224823sub git_project_index {4824my@projects= git_get_projects_list($project);48254826print$cgi->header(4827-type =>'text/plain',4828-charset =>'utf-8',4829-content_disposition =>'inline; filename="index.aux"');48304831foreachmy$pr(@projects) {4832if(!exists$pr->{'owner'}) {4833$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4834}48354836my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4837# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4838$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4839$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4840$path=~s/ /\+/g;4841$owner=~s/ /\+/g;48424843print"$path$owner\n";4844}4845}48464847sub git_summary {4848my$descr= git_get_project_description($project) ||"none";4849my%co= parse_commit("HEAD");4850my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4851my$head=$co{'id'};48524853my$owner= git_get_project_owner($project);48544855my$refs= git_get_references();4856# These get_*_list functions return one more to allow us to see if4857# there are more ...4858my@taglist= git_get_tags_list(16);4859my@headlist= git_get_heads_list(16);4860my@forklist;4861my$check_forks= gitweb_check_feature('forks');48624863if($check_forks) {4864@forklist= git_get_projects_list($project);4865}48664867 git_header_html();4868 git_print_page_nav('summary','',$head);48694870print"<div class=\"title\"> </div>\n";4871print"<table class=\"projects_list\">\n".4872"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4873"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4874if(defined$cd{'rfc2822'}) {4875print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4876}48774878# use per project git URL list in $projectroot/$project/cloneurl4879# or make project git URL from git base URL and project name4880my$url_tag="URL";4881my@url_list= git_get_project_url_list($project);4882@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4883foreachmy$git_url(@url_list) {4884next unless$git_url;4885print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4886$url_tag="";4887}48884889# Tag cloud4890my$show_ctags= gitweb_check_feature('ctags');4891if($show_ctags) {4892my$ctags= git_get_project_ctags($project);4893my$cloud= git_populate_project_tagcloud($ctags);4894print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4895print"</td>\n<td>"unless%$ctags;4896print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4897print"</td>\n<td>"if%$ctags;4898print git_show_project_tagcloud($cloud,48);4899print"</td></tr>";4900}49014902print"</table>\n";49034904# If XSS prevention is on, we don't include README.html.4905# TODO: Allow a readme in some safe format.4906if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4907print"<div class=\"title\">readme</div>\n".4908"<div class=\"readme\">\n";4909 insert_file("$projectroot/$project/README.html");4910print"\n</div>\n";# class="readme"4911}49124913# we need to request one more than 16 (0..15) to check if4914# those 16 are all4915my@commitlist=$head? parse_commits($head,17) : ();4916if(@commitlist) {4917 git_print_header_div('shortlog');4918 git_shortlog_body(\@commitlist,0,15,$refs,4919$#commitlist<=15?undef:4920$cgi->a({-href => href(action=>"shortlog")},"..."));4921}49224923if(@taglist) {4924 git_print_header_div('tags');4925 git_tags_body(\@taglist,0,15,4926$#taglist<=15?undef:4927$cgi->a({-href => href(action=>"tags")},"..."));4928}49294930if(@headlist) {4931 git_print_header_div('heads');4932 git_heads_body(\@headlist,$head,0,15,4933$#headlist<=15?undef:4934$cgi->a({-href => href(action=>"heads")},"..."));4935}49364937if(@forklist) {4938 git_print_header_div('forks');4939 git_project_list_body(\@forklist,'age',0,15,4940$#forklist<=15?undef:4941$cgi->a({-href => href(action=>"forks")},"..."),4942'no_header');4943}49444945 git_footer_html();4946}49474948sub git_tag {4949my$head= git_get_head_hash($project);4950 git_header_html();4951 git_print_page_nav('','',$head,undef,$head);4952my%tag= parse_tag($hash);49534954if(!%tag) {4955 die_error(404,"Unknown tag object");4956}49574958 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4959print"<div class=\"title_text\">\n".4960"<table class=\"object_header\">\n".4961"<tr>\n".4962"<td>object</td>\n".4963"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4964$tag{'object'}) ."</td>\n".4965"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4966$tag{'type'}) ."</td>\n".4967"</tr>\n";4968if(defined($tag{'author'})) {4969 git_print_authorship_rows(\%tag,'author');4970}4971print"</table>\n\n".4972"</div>\n";4973print"<div class=\"page_body\">";4974my$comment=$tag{'comment'};4975foreachmy$line(@$comment) {4976chomp$line;4977print esc_html($line, -nbsp=>1) ."<br/>\n";4978}4979print"</div>\n";4980 git_footer_html();4981}49824983sub git_blame_common {4984my$format=shift||'porcelain';4985if($formateq'porcelain'&&$cgi->param('js')) {4986$format='incremental';4987$action='blame_incremental';# for page title etc4988}49894990# permissions4991 gitweb_check_feature('blame')4992or die_error(403,"Blame view not allowed");49934994# error checking4995 die_error(400,"No file name given")unless$file_name;4996$hash_base||= git_get_head_hash($project);4997 die_error(404,"Couldn't find base commit")unless$hash_base;4998my%co= parse_commit($hash_base)4999or die_error(404,"Commit not found");5000my$ftype="blob";5001if(!defined$hash) {5002$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5003or die_error(404,"Error looking up file");5004}else{5005$ftype= git_get_type($hash);5006if($ftype!~"blob") {5007 die_error(400,"Object is not a blob");5008}5009}50105011my$fd;5012if($formateq'incremental') {5013# get file contents (as base)5014open$fd,"-|", git_cmd(),'cat-file','blob',$hash5015or die_error(500,"Open git-cat-file failed");5016}elsif($formateq'data') {5017# run git-blame --incremental5018open$fd,"-|", git_cmd(),"blame","--incremental",5019$hash_base,"--",$file_name5020or die_error(500,"Open git-blame --incremental failed");5021}else{5022# run git-blame --porcelain5023open$fd,"-|", git_cmd(),"blame",'-p',5024$hash_base,'--',$file_name5025or die_error(500,"Open git-blame --porcelain failed");5026}50275028# incremental blame data returns early5029if($formateq'data') {5030print$cgi->header(5031-type=>"text/plain", -charset =>"utf-8",5032-status=>"200 OK");5033local$| =1;# output autoflush5034printwhile<$fd>;5035close$fd5036or print"ERROR$!\n";50375038print'END';5039if(defined$t0&& gitweb_check_feature('timed')) {5040print' '.5041 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5042' '.$number_of_git_cmds;5043}5044print"\n";50455046return;5047}50485049# page header5050 git_header_html();5051my$formats_nav=5052$cgi->a({-href => href(action=>"blob", -replay=>1)},5053"blob") .5054" | ";5055if($formateq'incremental') {5056$formats_nav.=5057$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5058"blame") ." (non-incremental)";5059}else{5060$formats_nav.=5061$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5062"blame") ." (incremental)";5063}5064$formats_nav.=5065" | ".5066$cgi->a({-href => href(action=>"history", -replay=>1)},5067"history") .5068" | ".5069$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5070"HEAD");5071 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5072 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5073 git_print_page_path($file_name,$ftype,$hash_base);50745075# page body5076if($formateq'incremental') {5077print"<noscript>\n<div class=\"error\"><center><b>\n".5078"This page requires JavaScript to run.\nUse ".5079$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5080'this page').5081" instead.\n".5082"</b></center></div>\n</noscript>\n";50835084print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5085}50865087print qq!<div class="page_body">\n!;5088print qq!<div id="progress_info">.../ ...</div>\n!5089if($formateq'incremental');5090print qq!<table id="blame_table"class="blame" width="100%">\n!.5091#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5092 qq!<thead>\n!.5093 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5094 qq!</thead>\n!.5095 qq!<tbody>\n!;50965097my@rev_color=qw(light dark);5098my$num_colors=scalar(@rev_color);5099my$current_color=0;51005101if($formateq'incremental') {5102my$color_class=$rev_color[$current_color];51035104#contents of a file5105my$linenr=0;5106 LINE:5107while(my$line= <$fd>) {5108chomp$line;5109$linenr++;51105111print qq!<tr id="l$linenr"class="$color_class">!.5112 qq!<td class="sha1"><a href=""> </a></td>!.5113 qq!<td class="linenr">!.5114 qq!<a class="linenr" href="">$linenr</a></td>!;5115print qq!<td class="pre">! . esc_html($line) ."</td>\n";5116print qq!</tr>\n!;5117}51185119}else{# porcelain, i.e. ordinary blame5120my%metainfo= ();# saves information about commits51215122# blame data5123 LINE:5124while(my$line= <$fd>) {5125chomp$line;5126# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5127# no <lines in group> for subsequent lines in group of lines5128my($full_rev,$orig_lineno,$lineno,$group_size) =5129($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5130if(!exists$metainfo{$full_rev}) {5131$metainfo{$full_rev} = {'nprevious'=>0};5132}5133my$meta=$metainfo{$full_rev};5134my$data;5135while($data= <$fd>) {5136chomp$data;5137last if($data=~s/^\t//);# contents of line5138if($data=~/^(\S+)(?: (.*))?$/) {5139$meta->{$1} =$2unlessexists$meta->{$1};5140}5141if($data=~/^previous /) {5142$meta->{'nprevious'}++;5143}5144}5145my$short_rev=substr($full_rev,0,8);5146my$author=$meta->{'author'};5147my%date=5148 parse_date($meta->{'author-time'},$meta->{'author-tz'});5149my$date=$date{'iso-tz'};5150if($group_size) {5151$current_color= ($current_color+1) %$num_colors;5152}5153my$tr_class=$rev_color[$current_color];5154$tr_class.=' boundary'if(exists$meta->{'boundary'});5155$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5156$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5157print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5158if($group_size) {5159print"<td class=\"sha1\"";5160print" title=\"". esc_html($author) .",$date\"";5161print" rowspan=\"$group_size\""if($group_size>1);5162print">";5163print$cgi->a({-href => href(action=>"commit",5164 hash=>$full_rev,5165 file_name=>$file_name)},5166 esc_html($short_rev));5167if($group_size>=2) {5168my@author_initials= ($author=~/\b([[:upper:]])\B/g);5169if(@author_initials) {5170print"<br />".5171 esc_html(join('',@author_initials));5172# or join('.', ...)5173}5174}5175print"</td>\n";5176}5177# 'previous' <sha1 of parent commit> <filename at commit>5178if(exists$meta->{'previous'} &&5179$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5180$meta->{'parent'} =$1;5181$meta->{'file_parent'} = unquote($2);5182}5183my$linenr_commit=5184exists($meta->{'parent'}) ?5185$meta->{'parent'} :$full_rev;5186my$linenr_filename=5187exists($meta->{'file_parent'}) ?5188$meta->{'file_parent'} : unquote($meta->{'filename'});5189my$blamed= href(action =>'blame',5190 file_name =>$linenr_filename,5191 hash_base =>$linenr_commit);5192print"<td class=\"linenr\">";5193print$cgi->a({ -href =>"$blamed#l$orig_lineno",5194-class=>"linenr"},5195 esc_html($lineno));5196print"</td>";5197print"<td class=\"pre\">". esc_html($data) ."</td>\n";5198print"</tr>\n";5199}# end while52005201}52025203# footer5204print"</tbody>\n".5205"</table>\n";# class="blame"5206print"</div>\n";# class="blame_body"5207close$fd5208or print"Reading blob failed\n";52095210 git_footer_html();5211}52125213sub git_blame {5214 git_blame_common();5215}52165217sub git_blame_incremental {5218 git_blame_common('incremental');5219}52205221sub git_blame_data {5222 git_blame_common('data');5223}52245225sub git_tags {5226my$head= git_get_head_hash($project);5227 git_header_html();5228 git_print_page_nav('','',$head,undef,$head);5229 git_print_header_div('summary',$project);52305231my@tagslist= git_get_tags_list();5232if(@tagslist) {5233 git_tags_body(\@tagslist);5234}5235 git_footer_html();5236}52375238sub git_heads {5239my$head= git_get_head_hash($project);5240 git_header_html();5241 git_print_page_nav('','',$head,undef,$head);5242 git_print_header_div('summary',$project);52435244my@headslist= git_get_heads_list();5245if(@headslist) {5246 git_heads_body(\@headslist,$head);5247}5248 git_footer_html();5249}52505251sub git_blob_plain {5252my$type=shift;5253my$expires;52545255if(!defined$hash) {5256if(defined$file_name) {5257my$base=$hash_base|| git_get_head_hash($project);5258$hash= git_get_hash_by_path($base,$file_name,"blob")5259or die_error(404,"Cannot find file");5260}else{5261 die_error(400,"No file name defined");5262}5263}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5264# blobs defined by non-textual hash id's can be cached5265$expires="+1d";5266}52675268open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5269or die_error(500,"Open git-cat-file blob '$hash' failed");52705271# content-type (can include charset)5272$type= blob_contenttype($fd,$file_name,$type);52735274# "save as" filename, even when no $file_name is given5275my$save_as="$hash";5276if(defined$file_name) {5277$save_as=$file_name;5278}elsif($type=~m/^text\//) {5279$save_as.='.txt';5280}52815282# With XSS prevention on, blobs of all types except a few known safe5283# ones are served with "Content-Disposition: attachment" to make sure5284# they don't run in our security domain. For certain image types,5285# blob view writes an <img> tag referring to blob_plain view, and we5286# want to be sure not to break that by serving the image as an5287# attachment (though Firefox 3 doesn't seem to care).5288my$sandbox=$prevent_xss&&5289$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;52905291print$cgi->header(5292-type =>$type,5293-expires =>$expires,5294-content_disposition =>5295($sandbox?'attachment':'inline')5296.'; filename="'.$save_as.'"');5297local$/=undef;5298binmode STDOUT,':raw';5299print<$fd>;5300binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5301close$fd;5302}53035304sub git_blob {5305my$expires;53065307if(!defined$hash) {5308if(defined$file_name) {5309my$base=$hash_base|| git_get_head_hash($project);5310$hash= git_get_hash_by_path($base,$file_name,"blob")5311or die_error(404,"Cannot find file");5312}else{5313 die_error(400,"No file name defined");5314}5315}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5316# blobs defined by non-textual hash id's can be cached5317$expires="+1d";5318}53195320my$have_blame= gitweb_check_feature('blame');5321open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5322or die_error(500,"Couldn't cat$file_name,$hash");5323my$mimetype= blob_mimetype($fd,$file_name);5324if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5325close$fd;5326return git_blob_plain($mimetype);5327}5328# we can have blame only for text/* mimetype5329$have_blame&&= ($mimetype=~m!^text/!);53305331 git_header_html(undef,$expires);5332my$formats_nav='';5333if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5334if(defined$file_name) {5335if($have_blame) {5336$formats_nav.=5337$cgi->a({-href => href(action=>"blame", -replay=>1)},5338"blame") .5339" | ";5340}5341$formats_nav.=5342$cgi->a({-href => href(action=>"history", -replay=>1)},5343"history") .5344" | ".5345$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5346"raw") .5347" | ".5348$cgi->a({-href => href(action=>"blob",5349 hash_base=>"HEAD", file_name=>$file_name)},5350"HEAD");5351}else{5352$formats_nav.=5353$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5354"raw");5355}5356 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5357 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5358}else{5359print"<div class=\"page_nav\">\n".5360"<br/><br/></div>\n".5361"<div class=\"title\">$hash</div>\n";5362}5363 git_print_page_path($file_name,"blob",$hash_base);5364print"<div class=\"page_body\">\n";5365if($mimetype=~m!^image/!) {5366print qq!<img type="$mimetype"!;5367if($file_name) {5368print qq! alt="$file_name" title="$file_name"!;5369}5370print qq! src="! .5371 href(action=>"blob_plain", hash=>$hash,5372 hash_base=>$hash_base, file_name=>$file_name) .5373 qq!"/>\n!;5374}else{5375my$nr;5376while(my$line= <$fd>) {5377chomp$line;5378$nr++;5379$line= untabify($line);5380printf"<div class=\"pre\"><a id=\"l%i\"href=\"". href(-replay =>1)5381."#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5382$nr,$nr,$nr, esc_html($line, -nbsp=>1);5383}5384}5385close$fd5386or print"Reading blob failed.\n";5387print"</div>";5388 git_footer_html();5389}53905391sub git_tree {5392if(!defined$hash_base) {5393$hash_base="HEAD";5394}5395if(!defined$hash) {5396if(defined$file_name) {5397$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5398}else{5399$hash=$hash_base;5400}5401}5402 die_error(404,"No such tree")unlessdefined($hash);54035404my$show_sizes= gitweb_check_feature('show-sizes');5405my$have_blame= gitweb_check_feature('blame');54065407my@entries= ();5408{5409local$/="\0";5410open my$fd,"-|", git_cmd(),"ls-tree",'-z',5411($show_sizes?'-l': ()),@extra_options,$hash5412or die_error(500,"Open git-ls-tree failed");5413@entries=map{chomp;$_} <$fd>;5414close$fd5415or die_error(404,"Reading tree failed");5416}54175418my$refs= git_get_references();5419my$ref= format_ref_marker($refs,$hash_base);5420 git_header_html();5421my$basedir='';5422if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5423my@views_nav= ();5424if(defined$file_name) {5425push@views_nav,5426$cgi->a({-href => href(action=>"history", -replay=>1)},5427"history"),5428$cgi->a({-href => href(action=>"tree",5429 hash_base=>"HEAD", file_name=>$file_name)},5430"HEAD"),5431}5432my$snapshot_links= format_snapshot_links($hash);5433if(defined$snapshot_links) {5434# FIXME: Should be available when we have no hash base as well.5435push@views_nav,$snapshot_links;5436}5437 git_print_page_nav('tree','',$hash_base,undef,undef,5438join(' | ',@views_nav));5439 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5440}else{5441undef$hash_base;5442print"<div class=\"page_nav\">\n";5443print"<br/><br/></div>\n";5444print"<div class=\"title\">$hash</div>\n";5445}5446if(defined$file_name) {5447$basedir=$file_name;5448if($basedirne''&&substr($basedir, -1)ne'/') {5449$basedir.='/';5450}5451 git_print_page_path($file_name,'tree',$hash_base);5452}5453print"<div class=\"page_body\">\n";5454print"<table class=\"tree\">\n";5455my$alternate=1;5456# '..' (top directory) link if possible5457if(defined$hash_base&&5458defined$file_name&&$file_name=~m![^/]+$!) {5459if($alternate) {5460print"<tr class=\"dark\">\n";5461}else{5462print"<tr class=\"light\">\n";5463}5464$alternate^=1;54655466my$up=$file_name;5467$up=~s!/?[^/]+$!!;5468undef$upunless$up;5469# based on git_print_tree_entry5470print'<td class="mode">'. mode_str('040000') ."</td>\n";5471print'<td class="size"> </td>'."\n"if$show_sizes;5472print'<td class="list">';5473print$cgi->a({-href => href(action=>"tree",5474 hash_base=>$hash_base,5475 file_name=>$up)},5476"..");5477print"</td>\n";5478print"<td class=\"link\"></td>\n";54795480print"</tr>\n";5481}5482foreachmy$line(@entries) {5483my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);54845485if($alternate) {5486print"<tr class=\"dark\">\n";5487}else{5488print"<tr class=\"light\">\n";5489}5490$alternate^=1;54915492 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);54935494print"</tr>\n";5495}5496print"</table>\n".5497"</div>";5498 git_footer_html();5499}55005501sub snapshot_name {5502my($project,$hash) =@_;55035504# path/to/project.git -> project5505# path/to/project/.git -> project5506my$name= to_utf8($project);5507$name=~ s,([^/])/*\.git$,$1,;5508$name= basename($name);5509# sanitize name5510$name=~s/[[:cntrl:]]/?/g;55115512my$ver=$hash;5513if($hash=~/^[0-9a-fA-F]+$/) {5514# shorten SHA-1 hash5515my$full_hash= git_get_full_hash($project,$hash);5516if($full_hash=~/^$hash/&&length($hash) >7) {5517$ver= git_get_short_hash($project,$hash);5518}5519}elsif($hash=~m!^refs/tags/(.*)$!) {5520# tags don't need shortened SHA-1 hash5521$ver=$1;5522}else{5523# branches and other need shortened SHA-1 hash5524if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5525$ver=$1;5526}5527$ver.='-'. git_get_short_hash($project,$hash);5528}5529# in case of hierarchical branch names5530$ver=~s!/!.!g;55315532# name = project-version_string5533$name="$name-$ver";55345535returnwantarray? ($name,$name) :$name;5536}55375538sub git_snapshot {5539my$format=$input_params{'snapshot_format'};5540if(!@snapshot_fmts) {5541 die_error(403,"Snapshots not allowed");5542}5543# default to first supported snapshot format5544$format||=$snapshot_fmts[0];5545if($format!~m/^[a-z0-9]+$/) {5546 die_error(400,"Invalid snapshot format parameter");5547}elsif(!exists($known_snapshot_formats{$format})) {5548 die_error(400,"Unknown snapshot format");5549}elsif($known_snapshot_formats{$format}{'disabled'}) {5550 die_error(403,"Snapshot format not allowed");5551}elsif(!grep($_eq$format,@snapshot_fmts)) {5552 die_error(403,"Unsupported snapshot format");5553}55545555my$type= git_get_type("$hash^{}");5556if(!$type) {5557 die_error(404,'Object does not exist');5558}elsif($typeeq'blob') {5559 die_error(400,'Object is not a tree-ish');5560}55615562my($name,$prefix) = snapshot_name($project,$hash);5563my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5564my$cmd= quote_command(5565 git_cmd(),'archive',5566"--format=$known_snapshot_formats{$format}{'format'}",5567"--prefix=$prefix/",$hash);5568if(exists$known_snapshot_formats{$format}{'compressor'}) {5569$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5570}55715572$filename=~s/(["\\])/\\$1/g;5573print$cgi->header(5574-type =>$known_snapshot_formats{$format}{'type'},5575-content_disposition =>'inline; filename="'.$filename.'"',5576-status =>'200 OK');55775578open my$fd,"-|",$cmd5579or die_error(500,"Execute git-archive failed");5580binmode STDOUT,':raw';5581print<$fd>;5582binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5583close$fd;5584}55855586sub git_log_generic {5587my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;55885589my$head= git_get_head_hash($project);5590if(!defined$base) {5591$base=$head;5592}5593if(!defined$page) {5594$page=0;5595}5596my$refs= git_get_references();55975598my$commit_hash=$base;5599if(defined$parent) {5600$commit_hash="$parent..$base";5601}5602my@commitlist=5603 parse_commits($commit_hash,101, (100*$page),5604defined$file_name? ($file_name,"--full-history") : ());56055606my$ftype;5607if(!defined$file_hash&&defined$file_name) {5608# some commits could have deleted file in question,5609# and not have it in tree, but one of them has to have it5610for(my$i=0;$i<@commitlist;$i++) {5611$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5612last ifdefined$file_hash;5613}5614}5615if(defined$file_hash) {5616$ftype= git_get_type($file_hash);5617}5618if(defined$file_name&& !defined$ftype) {5619 die_error(500,"Unknown type of object");5620}5621my%co;5622if(defined$file_name) {5623%co= parse_commit($base)5624or die_error(404,"Unknown commit object");5625}562656275628my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5629my$next_link='';5630if($#commitlist>=100) {5631$next_link=5632$cgi->a({-href => href(-replay=>1, page=>$page+1),5633-accesskey =>"n", -title =>"Alt-n"},"next");5634}5635my$patch_max= gitweb_get_feature('patches');5636if($patch_max&& !defined$file_name) {5637if($patch_max<0||@commitlist<=$patch_max) {5638$paging_nav.=" ⋅ ".5639$cgi->a({-href => href(action=>"patches", -replay=>1)},5640"patches");5641}5642}56435644 git_header_html();5645 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5646if(defined$file_name) {5647 git_print_header_div('commit', esc_html($co{'title'}),$base);5648}else{5649 git_print_header_div('summary',$project)5650}5651 git_print_page_path($file_name,$ftype,$hash_base)5652if(defined$file_name);56535654$body_subr->(\@commitlist,0,99,$refs,$next_link,5655$file_name,$file_hash,$ftype);56565657 git_footer_html();5658}56595660sub git_log {5661 git_log_generic('log', \&git_log_body,5662$hash,$hash_parent);5663}56645665sub git_commit {5666$hash||=$hash_base||"HEAD";5667my%co= parse_commit($hash)5668or die_error(404,"Unknown commit object");56695670my$parent=$co{'parent'};5671my$parents=$co{'parents'};# listref56725673# we need to prepare $formats_nav before any parameter munging5674my$formats_nav;5675if(!defined$parent) {5676# --root commitdiff5677$formats_nav.='(initial)';5678}elsif(@$parents==1) {5679# single parent commit5680$formats_nav.=5681'(parent: '.5682$cgi->a({-href => href(action=>"commit",5683 hash=>$parent)},5684 esc_html(substr($parent,0,7))) .5685')';5686}else{5687# merge commit5688$formats_nav.=5689'(merge: '.5690join(' ',map{5691$cgi->a({-href => href(action=>"commit",5692 hash=>$_)},5693 esc_html(substr($_,0,7)));5694}@$parents) .5695')';5696}5697if(gitweb_check_feature('patches') &&@$parents<=1) {5698$formats_nav.=" | ".5699$cgi->a({-href => href(action=>"patch", -replay=>1)},5700"patch");5701}57025703if(!defined$parent) {5704$parent="--root";5705}5706my@difftree;5707open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5708@diff_opts,5709(@$parents<=1?$parent:'-c'),5710$hash,"--"5711or die_error(500,"Open git-diff-tree failed");5712@difftree=map{chomp;$_} <$fd>;5713close$fdor die_error(404,"Reading git-diff-tree failed");57145715# non-textual hash id's can be cached5716my$expires;5717if($hash=~m/^[0-9a-fA-F]{40}$/) {5718$expires="+1d";5719}5720my$refs= git_get_references();5721my$ref= format_ref_marker($refs,$co{'id'});57225723 git_header_html(undef,$expires);5724 git_print_page_nav('commit','',5725$hash,$co{'tree'},$hash,5726$formats_nav);57275728if(defined$co{'parent'}) {5729 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5730}else{5731 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5732}5733print"<div class=\"title_text\">\n".5734"<table class=\"object_header\">\n";5735 git_print_authorship_rows(\%co);5736print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5737print"<tr>".5738"<td>tree</td>".5739"<td class=\"sha1\">".5740$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5741class=>"list"},$co{'tree'}) .5742"</td>".5743"<td class=\"link\">".5744$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5745"tree");5746my$snapshot_links= format_snapshot_links($hash);5747if(defined$snapshot_links) {5748print" | ".$snapshot_links;5749}5750print"</td>".5751"</tr>\n";57525753foreachmy$par(@$parents) {5754print"<tr>".5755"<td>parent</td>".5756"<td class=\"sha1\">".5757$cgi->a({-href => href(action=>"commit", hash=>$par),5758class=>"list"},$par) .5759"</td>".5760"<td class=\"link\">".5761$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5762" | ".5763$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5764"</td>".5765"</tr>\n";5766}5767print"</table>".5768"</div>\n";57695770print"<div class=\"page_body\">\n";5771 git_print_log($co{'comment'});5772print"</div>\n";57735774 git_difftree_body(\@difftree,$hash,@$parents);57755776 git_footer_html();5777}57785779sub git_object {5780# object is defined by:5781# - hash or hash_base alone5782# - hash_base and file_name5783my$type;57845785# - hash or hash_base alone5786if($hash|| ($hash_base&& !defined$file_name)) {5787my$object_id=$hash||$hash_base;57885789open my$fd,"-|", quote_command(5790 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5791or die_error(404,"Object does not exist");5792$type= <$fd>;5793chomp$type;5794close$fd5795or die_error(404,"Object does not exist");57965797# - hash_base and file_name5798}elsif($hash_base&&defined$file_name) {5799$file_name=~ s,/+$,,;58005801system(git_cmd(),"cat-file",'-e',$hash_base) ==05802or die_error(404,"Base object does not exist");58035804# here errors should not hapen5805open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5806or die_error(500,"Open git-ls-tree failed");5807my$line= <$fd>;5808close$fd;58095810#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5811unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5812 die_error(404,"File or directory for given base does not exist");5813}5814$type=$2;5815$hash=$3;5816}else{5817 die_error(400,"Not enough information to find object");5818}58195820print$cgi->redirect(-uri => href(action=>$type, -full=>1,5821 hash=>$hash, hash_base=>$hash_base,5822 file_name=>$file_name),5823-status =>'302 Found');5824}58255826sub git_blobdiff {5827my$format=shift||'html';58285829my$fd;5830my@difftree;5831my%diffinfo;5832my$expires;58335834# preparing $fd and %diffinfo for git_patchset_body5835# new style URI5836if(defined$hash_base&&defined$hash_parent_base) {5837if(defined$file_name) {5838# read raw output5839open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5840$hash_parent_base,$hash_base,5841"--", (defined$file_parent?$file_parent: ()),$file_name5842or die_error(500,"Open git-diff-tree failed");5843@difftree=map{chomp;$_} <$fd>;5844close$fd5845or die_error(404,"Reading git-diff-tree failed");5846@difftree5847or die_error(404,"Blob diff not found");58485849}elsif(defined$hash&&5850$hash=~/[0-9a-fA-F]{40}/) {5851# try to find filename from $hash58525853# read filtered raw output5854open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5855$hash_parent_base,$hash_base,"--"5856or die_error(500,"Open git-diff-tree failed");5857@difftree=5858# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5859# $hash == to_id5860grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5861map{chomp;$_} <$fd>;5862close$fd5863or die_error(404,"Reading git-diff-tree failed");5864@difftree5865or die_error(404,"Blob diff not found");58665867}else{5868 die_error(400,"Missing one of the blob diff parameters");5869}58705871if(@difftree>1) {5872 die_error(400,"Ambiguous blob diff specification");5873}58745875%diffinfo= parse_difftree_raw_line($difftree[0]);5876$file_parent||=$diffinfo{'from_file'} ||$file_name;5877$file_name||=$diffinfo{'to_file'};58785879$hash_parent||=$diffinfo{'from_id'};5880$hash||=$diffinfo{'to_id'};58815882# non-textual hash id's can be cached5883if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5884$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5885$expires='+1d';5886}58875888# open patch output5889open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5890'-p', ($formateq'html'?"--full-index": ()),5891$hash_parent_base,$hash_base,5892"--", (defined$file_parent?$file_parent: ()),$file_name5893or die_error(500,"Open git-diff-tree failed");5894}58955896# old/legacy style URI -- not generated anymore since 1.4.3.5897if(!%diffinfo) {5898 die_error('404 Not Found',"Missing one of the blob diff parameters")5899}59005901# header5902if($formateq'html') {5903my$formats_nav=5904$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5905"raw");5906 git_header_html(undef,$expires);5907if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5908 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5909 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5910}else{5911print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5912print"<div class=\"title\">$hashvs$hash_parent</div>\n";5913}5914if(defined$file_name) {5915 git_print_page_path($file_name,"blob",$hash_base);5916}else{5917print"<div class=\"page_path\"></div>\n";5918}59195920}elsif($formateq'plain') {5921print$cgi->header(5922-type =>'text/plain',5923-charset =>'utf-8',5924-expires =>$expires,5925-content_disposition =>'inline; filename="'."$file_name".'.patch"');59265927print"X-Git-Url: ".$cgi->self_url() ."\n\n";59285929}else{5930 die_error(400,"Unknown blobdiff format");5931}59325933# patch5934if($formateq'html') {5935print"<div class=\"page_body\">\n";59365937 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5938close$fd;59395940print"</div>\n";# class="page_body"5941 git_footer_html();59425943}else{5944while(my$line= <$fd>) {5945$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5946$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;59475948print$line;59495950last if$line=~m!^\+\+\+!;5951}5952local$/=undef;5953print<$fd>;5954close$fd;5955}5956}59575958sub git_blobdiff_plain {5959 git_blobdiff('plain');5960}59615962sub git_commitdiff {5963my%params=@_;5964my$format=$params{-format} ||'html';59655966my($patch_max) = gitweb_get_feature('patches');5967if($formateq'patch') {5968 die_error(403,"Patch view not allowed")unless$patch_max;5969}59705971$hash||=$hash_base||"HEAD";5972my%co= parse_commit($hash)5973or die_error(404,"Unknown commit object");59745975# choose format for commitdiff for merge5976if(!defined$hash_parent&& @{$co{'parents'}} >1) {5977$hash_parent='--cc';5978}5979# we need to prepare $formats_nav before almost any parameter munging5980my$formats_nav;5981if($formateq'html') {5982$formats_nav=5983$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5984"raw");5985if($patch_max&& @{$co{'parents'}} <=1) {5986$formats_nav.=" | ".5987$cgi->a({-href => href(action=>"patch", -replay=>1)},5988"patch");5989}59905991if(defined$hash_parent&&5992$hash_parentne'-c'&&$hash_parentne'--cc') {5993# commitdiff with two commits given5994my$hash_parent_short=$hash_parent;5995if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5996$hash_parent_short=substr($hash_parent,0,7);5997}5998$formats_nav.=5999' (from';6000for(my$i=0;$i< @{$co{'parents'}};$i++) {6001if($co{'parents'}[$i]eq$hash_parent) {6002$formats_nav.=' parent '. ($i+1);6003last;6004}6005}6006$formats_nav.=': '.6007$cgi->a({-href => href(action=>"commitdiff",6008 hash=>$hash_parent)},6009 esc_html($hash_parent_short)) .6010')';6011}elsif(!$co{'parent'}) {6012# --root commitdiff6013$formats_nav.=' (initial)';6014}elsif(scalar@{$co{'parents'}} ==1) {6015# single parent commit6016$formats_nav.=6017' (parent: '.6018$cgi->a({-href => href(action=>"commitdiff",6019 hash=>$co{'parent'})},6020 esc_html(substr($co{'parent'},0,7))) .6021')';6022}else{6023# merge commit6024if($hash_parenteq'--cc') {6025$formats_nav.=' | '.6026$cgi->a({-href => href(action=>"commitdiff",6027 hash=>$hash, hash_parent=>'-c')},6028'combined');6029}else{# $hash_parent eq '-c'6030$formats_nav.=' | '.6031$cgi->a({-href => href(action=>"commitdiff",6032 hash=>$hash, hash_parent=>'--cc')},6033'compact');6034}6035$formats_nav.=6036' (merge: '.6037join(' ',map{6038$cgi->a({-href => href(action=>"commitdiff",6039 hash=>$_)},6040 esc_html(substr($_,0,7)));6041} @{$co{'parents'}} ) .6042')';6043}6044}60456046my$hash_parent_param=$hash_parent;6047if(!defined$hash_parent_param) {6048# --cc for multiple parents, --root for parentless6049$hash_parent_param=6050@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6051}60526053# read commitdiff6054my$fd;6055my@difftree;6056if($formateq'html') {6057open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6058"--no-commit-id","--patch-with-raw","--full-index",6059$hash_parent_param,$hash,"--"6060or die_error(500,"Open git-diff-tree failed");60616062while(my$line= <$fd>) {6063chomp$line;6064# empty line ends raw part of diff-tree output6065last unless$line;6066push@difftree,scalar parse_difftree_raw_line($line);6067}60686069}elsif($formateq'plain') {6070open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6071'-p',$hash_parent_param,$hash,"--"6072or die_error(500,"Open git-diff-tree failed");6073}elsif($formateq'patch') {6074# For commit ranges, we limit the output to the number of6075# patches specified in the 'patches' feature.6076# For single commits, we limit the output to a single patch,6077# diverging from the git-format-patch default.6078my@commit_spec= ();6079if($hash_parent) {6080if($patch_max>0) {6081push@commit_spec,"-$patch_max";6082}6083push@commit_spec,'-n',"$hash_parent..$hash";6084}else{6085if($params{-single}) {6086push@commit_spec,'-1';6087}else{6088if($patch_max>0) {6089push@commit_spec,"-$patch_max";6090}6091push@commit_spec,"-n";6092}6093push@commit_spec,'--root',$hash;6094}6095open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',6096'--stdout',@commit_spec6097or die_error(500,"Open git-format-patch failed");6098}else{6099 die_error(400,"Unknown commitdiff format");6100}61016102# non-textual hash id's can be cached6103my$expires;6104if($hash=~m/^[0-9a-fA-F]{40}$/) {6105$expires="+1d";6106}61076108# write commit message6109if($formateq'html') {6110my$refs= git_get_references();6111my$ref= format_ref_marker($refs,$co{'id'});61126113 git_header_html(undef,$expires);6114 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6115 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6116print"<div class=\"title_text\">\n".6117"<table class=\"object_header\">\n";6118 git_print_authorship_rows(\%co);6119print"</table>".6120"</div>\n";6121print"<div class=\"page_body\">\n";6122if(@{$co{'comment'}} >1) {6123print"<div class=\"log\">\n";6124 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6125print"</div>\n";# class="log"6126}61276128}elsif($formateq'plain') {6129my$refs= git_get_references("tags");6130my$tagname= git_get_rev_name_tags($hash);6131my$filename= basename($project) ."-$hash.patch";61326133print$cgi->header(6134-type =>'text/plain',6135-charset =>'utf-8',6136-expires =>$expires,6137-content_disposition =>'inline; filename="'."$filename".'"');6138my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6139print"From: ". to_utf8($co{'author'}) ."\n";6140print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6141print"Subject: ". to_utf8($co{'title'}) ."\n";61426143print"X-Git-Tag:$tagname\n"if$tagname;6144print"X-Git-Url: ".$cgi->self_url() ."\n\n";61456146foreachmy$line(@{$co{'comment'}}) {6147print to_utf8($line) ."\n";6148}6149print"---\n\n";6150}elsif($formateq'patch') {6151my$filename= basename($project) ."-$hash.patch";61526153print$cgi->header(6154-type =>'text/plain',6155-charset =>'utf-8',6156-expires =>$expires,6157-content_disposition =>'inline; filename="'."$filename".'"');6158}61596160# write patch6161if($formateq'html') {6162my$use_parents= !defined$hash_parent||6163$hash_parenteq'-c'||$hash_parenteq'--cc';6164 git_difftree_body(\@difftree,$hash,6165$use_parents? @{$co{'parents'}} :$hash_parent);6166print"<br/>\n";61676168 git_patchset_body($fd, \@difftree,$hash,6169$use_parents? @{$co{'parents'}} :$hash_parent);6170close$fd;6171print"</div>\n";# class="page_body"6172 git_footer_html();61736174}elsif($formateq'plain') {6175local$/=undef;6176print<$fd>;6177close$fd6178or print"Reading git-diff-tree failed\n";6179}elsif($formateq'patch') {6180local$/=undef;6181print<$fd>;6182close$fd6183or print"Reading git-format-patch failed\n";6184}6185}61866187sub git_commitdiff_plain {6188 git_commitdiff(-format =>'plain');6189}61906191# format-patch-style patches6192sub git_patch {6193 git_commitdiff(-format =>'patch', -single =>1);6194}61956196sub git_patches {6197 git_commitdiff(-format =>'patch');6198}61996200sub git_history {6201 git_log_generic('history', \&git_history_body,6202$hash_base,$hash_parent_base,6203$file_name,$hash);6204}62056206sub git_search {6207 gitweb_check_feature('search')or die_error(403,"Search is disabled");6208if(!defined$searchtext) {6209 die_error(400,"Text field is empty");6210}6211if(!defined$hash) {6212$hash= git_get_head_hash($project);6213}6214my%co= parse_commit($hash);6215if(!%co) {6216 die_error(404,"Unknown commit object");6217}6218if(!defined$page) {6219$page=0;6220}62216222$searchtype||='commit';6223if($searchtypeeq'pickaxe') {6224# pickaxe may take all resources of your box and run for several minutes6225# with every query - so decide by yourself how public you make this feature6226 gitweb_check_feature('pickaxe')6227or die_error(403,"Pickaxe is disabled");6228}6229if($searchtypeeq'grep') {6230 gitweb_check_feature('grep')6231or die_error(403,"Grep is disabled");6232}62336234 git_header_html();62356236if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6237my$greptype;6238if($searchtypeeq'commit') {6239$greptype="--grep=";6240}elsif($searchtypeeq'author') {6241$greptype="--author=";6242}elsif($searchtypeeq'committer') {6243$greptype="--committer=";6244}6245$greptype.=$searchtext;6246my@commitlist= parse_commits($hash,101, (100*$page),undef,6247$greptype,'--regexp-ignore-case',6248$search_use_regexp?'--extended-regexp':'--fixed-strings');62496250my$paging_nav='';6251if($page>0) {6252$paging_nav.=6253$cgi->a({-href => href(action=>"search", hash=>$hash,6254 searchtext=>$searchtext,6255 searchtype=>$searchtype)},6256"first");6257$paging_nav.=" ⋅ ".6258$cgi->a({-href => href(-replay=>1, page=>$page-1),6259-accesskey =>"p", -title =>"Alt-p"},"prev");6260}else{6261$paging_nav.="first";6262$paging_nav.=" ⋅ prev";6263}6264my$next_link='';6265if($#commitlist>=100) {6266$next_link=6267$cgi->a({-href => href(-replay=>1, page=>$page+1),6268-accesskey =>"n", -title =>"Alt-n"},"next");6269$paging_nav.=" ⋅$next_link";6270}else{6271$paging_nav.=" ⋅ next";6272}62736274if($#commitlist>=100) {6275}62766277 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6278 git_print_header_div('commit', esc_html($co{'title'}),$hash);6279 git_search_grep_body(\@commitlist,0,99,$next_link);6280}62816282if($searchtypeeq'pickaxe') {6283 git_print_page_nav('','',$hash,$co{'tree'},$hash);6284 git_print_header_div('commit', esc_html($co{'title'}),$hash);62856286print"<table class=\"pickaxe search\">\n";6287my$alternate=1;6288local$/="\n";6289open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6290'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6291($search_use_regexp?'--pickaxe-regex': ());6292undef%co;6293my@files;6294while(my$line= <$fd>) {6295chomp$line;6296next unless$line;62976298my%set= parse_difftree_raw_line($line);6299if(defined$set{'commit'}) {6300# finish previous commit6301if(%co) {6302print"</td>\n".6303"<td class=\"link\">".6304$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6305" | ".6306$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6307print"</td>\n".6308"</tr>\n";6309}63106311if($alternate) {6312print"<tr class=\"dark\">\n";6313}else{6314print"<tr class=\"light\">\n";6315}6316$alternate^=1;6317%co= parse_commit($set{'commit'});6318my$author= chop_and_escape_str($co{'author_name'},15,5);6319print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6320"<td><i>$author</i></td>\n".6321"<td>".6322$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6323-class=>"list subject"},6324 chop_and_escape_str($co{'title'},50) ."<br/>");6325}elsif(defined$set{'to_id'}) {6326next if($set{'to_id'} =~m/^0{40}$/);63276328print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6329 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6330-class=>"list"},6331"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6332"<br/>\n";6333}6334}6335close$fd;63366337# finish last commit (warning: repetition!)6338if(%co) {6339print"</td>\n".6340"<td class=\"link\">".6341$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6342" | ".6343$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6344print"</td>\n".6345"</tr>\n";6346}63476348print"</table>\n";6349}63506351if($searchtypeeq'grep') {6352 git_print_page_nav('','',$hash,$co{'tree'},$hash);6353 git_print_header_div('commit', esc_html($co{'title'}),$hash);63546355print"<table class=\"grep_search\">\n";6356my$alternate=1;6357my$matches=0;6358local$/="\n";6359open my$fd,"-|", git_cmd(),'grep','-n',6360$search_use_regexp? ('-E','-i') :'-F',6361$searchtext,$co{'tree'};6362my$lastfile='';6363while(my$line= <$fd>) {6364chomp$line;6365my($file,$lno,$ltext,$binary);6366last if($matches++>1000);6367if($line=~/^Binary file (.+) matches$/) {6368$file=$1;6369$binary=1;6370}else{6371(undef,$file,$lno,$ltext) =split(/:/,$line,4);6372}6373if($filene$lastfile) {6374$lastfileand print"</td></tr>\n";6375if($alternate++) {6376print"<tr class=\"dark\">\n";6377}else{6378print"<tr class=\"light\">\n";6379}6380print"<td class=\"list\">".6381$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6382 file_name=>"$file"),6383-class=>"list"}, esc_path($file));6384print"</td><td>\n";6385$lastfile=$file;6386}6387if($binary) {6388print"<div class=\"binary\">Binary file</div>\n";6389}else{6390$ltext= untabify($ltext);6391if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6392$ltext= esc_html($1, -nbsp=>1);6393$ltext.='<span class="match">';6394$ltext.= esc_html($2, -nbsp=>1);6395$ltext.='</span>';6396$ltext.= esc_html($3, -nbsp=>1);6397}else{6398$ltext= esc_html($ltext, -nbsp=>1);6399}6400print"<div class=\"pre\">".6401$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6402 file_name=>"$file").'#l'.$lno,6403-class=>"linenr"},sprintf('%4i',$lno))6404.' '.$ltext."</div>\n";6405}6406}6407if($lastfile) {6408print"</td></tr>\n";6409if($matches>1000) {6410print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6411}6412}else{6413print"<div class=\"diff nodifferences\">No matches found</div>\n";6414}6415close$fd;64166417print"</table>\n";6418}6419 git_footer_html();6420}64216422sub git_search_help {6423 git_header_html();6424 git_print_page_nav('','',$hash,$hash,$hash);6425print<<EOT;6426<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6427regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6428the pattern entered is recognized as the POSIX extended6429<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6430insensitive).</p>6431<dl>6432<dt><b>commit</b></dt>6433<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6434EOT6435my$have_grep= gitweb_check_feature('grep');6436if($have_grep) {6437print<<EOT;6438<dt><b>grep</b></dt>6439<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6440 a different one) are searched for the given pattern. On large trees, this search can take6441a while and put some strain on the server, so please use it with some consideration. Note that6442due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6443case-sensitive.</dd>6444EOT6445}6446print<<EOT;6447<dt><b>author</b></dt>6448<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6449<dt><b>committer</b></dt>6450<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6451EOT6452my$have_pickaxe= gitweb_check_feature('pickaxe');6453if($have_pickaxe) {6454print<<EOT;6455<dt><b>pickaxe</b></dt>6456<dd>All commits that caused the string to appear or disappear from any file (changes that6457added, removed or "modified" the string) will be listed. This search can take a while and6458takes a lot of strain on the server, so please use it wisely. Note that since you may be6459interested even in changes just changing the case as well, this search is case sensitive.</dd>6460EOT6461}6462print"</dl>\n";6463 git_footer_html();6464}64656466sub git_shortlog {6467 git_log_generic('shortlog', \&git_shortlog_body,6468$hash,$hash_parent);6469}64706471## ......................................................................6472## feeds (RSS, Atom; OPML)64736474sub git_feed {6475my$format=shift||'atom';6476my$have_blame= gitweb_check_feature('blame');64776478# Atom: http://www.atomenabled.org/developers/syndication/6479# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6480if($formatne'rss'&&$formatne'atom') {6481 die_error(400,"Unknown web feed format");6482}64836484# log/feed of current (HEAD) branch, log of given branch, history of file/directory6485my$head=$hash||'HEAD';6486my@commitlist= parse_commits($head,150,0,$file_name);64876488my%latest_commit;6489my%latest_date;6490my$content_type="application/$format+xml";6491if(defined$cgi->http('HTTP_ACCEPT') &&6492$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6493# browser (feed reader) prefers text/xml6494$content_type='text/xml';6495}6496if(defined($commitlist[0])) {6497%latest_commit= %{$commitlist[0]};6498my$latest_epoch=$latest_commit{'committer_epoch'};6499%latest_date= parse_date($latest_epoch);6500my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6501if(defined$if_modified) {6502my$since;6503if(eval{require HTTP::Date;1; }) {6504$since= HTTP::Date::str2time($if_modified);6505}elsif(eval{require Time::ParseDate;1; }) {6506$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6507}6508if(defined$since&&$latest_epoch<=$since) {6509print$cgi->header(6510-type =>$content_type,6511-charset =>'utf-8',6512-last_modified =>$latest_date{'rfc2822'},6513-status =>'304 Not Modified');6514return;6515}6516}6517print$cgi->header(6518-type =>$content_type,6519-charset =>'utf-8',6520-last_modified =>$latest_date{'rfc2822'});6521}else{6522print$cgi->header(6523-type =>$content_type,6524-charset =>'utf-8');6525}65266527# Optimization: skip generating the body if client asks only6528# for Last-Modified date.6529return if($cgi->request_method()eq'HEAD');65306531# header variables6532my$title="$site_name-$project/$action";6533my$feed_type='log';6534if(defined$hash) {6535$title.=" - '$hash'";6536$feed_type='branch log';6537if(defined$file_name) {6538$title.=" ::$file_name";6539$feed_type='history';6540}6541}elsif(defined$file_name) {6542$title.=" -$file_name";6543$feed_type='history';6544}6545$title.="$feed_type";6546my$descr= git_get_project_description($project);6547if(defined$descr) {6548$descr= esc_html($descr);6549}else{6550$descr="$project".6551($formateq'rss'?'RSS':'Atom') .6552" feed";6553}6554my$owner= git_get_project_owner($project);6555$owner= esc_html($owner);65566557#header6558my$alt_url;6559if(defined$file_name) {6560$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6561}elsif(defined$hash) {6562$alt_url= href(-full=>1, action=>"log", hash=>$hash);6563}else{6564$alt_url= href(-full=>1, action=>"summary");6565}6566print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6567if($formateq'rss') {6568print<<XML;6569<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6570<channel>6571XML6572print"<title>$title</title>\n".6573"<link>$alt_url</link>\n".6574"<description>$descr</description>\n".6575"<language>en</language>\n".6576# project owner is responsible for 'editorial' content6577"<managingEditor>$owner</managingEditor>\n";6578if(defined$logo||defined$favicon) {6579# prefer the logo to the favicon, since RSS6580# doesn't allow both6581my$img= esc_url($logo||$favicon);6582print"<image>\n".6583"<url>$img</url>\n".6584"<title>$title</title>\n".6585"<link>$alt_url</link>\n".6586"</image>\n";6587}6588if(%latest_date) {6589print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6590print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6591}6592print"<generator>gitweb v.$version/$git_version</generator>\n";6593}elsif($formateq'atom') {6594print<<XML;6595<feed xmlns="http://www.w3.org/2005/Atom">6596XML6597print"<title>$title</title>\n".6598"<subtitle>$descr</subtitle>\n".6599'<link rel="alternate" type="text/html" href="'.6600$alt_url.'" />'."\n".6601'<link rel="self" type="'.$content_type.'" href="'.6602$cgi->self_url() .'" />'."\n".6603"<id>". href(-full=>1) ."</id>\n".6604# use project owner for feed author6605"<author><name>$owner</name></author>\n";6606if(defined$favicon) {6607print"<icon>". esc_url($favicon) ."</icon>\n";6608}6609if(defined$logo_url) {6610# not twice as wide as tall: 72 x 27 pixels6611print"<logo>". esc_url($logo) ."</logo>\n";6612}6613if(!%latest_date) {6614# dummy date to keep the feed valid until commits trickle in:6615print"<updated>1970-01-01T00:00:00Z</updated>\n";6616}else{6617print"<updated>$latest_date{'iso-8601'}</updated>\n";6618}6619print"<generator version='$version/$git_version'>gitweb</generator>\n";6620}66216622# contents6623for(my$i=0;$i<=$#commitlist;$i++) {6624my%co= %{$commitlist[$i]};6625my$commit=$co{'id'};6626# we read 150, we always show 30 and the ones more recent than 48 hours6627if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6628last;6629}6630my%cd= parse_date($co{'author_epoch'});66316632# get list of changed files6633open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6634$co{'parent'} ||"--root",6635$co{'id'},"--", (defined$file_name?$file_name: ())6636ornext;6637my@difftree=map{chomp;$_} <$fd>;6638close$fd6639ornext;66406641# print element (entry, item)6642my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6643if($formateq'rss') {6644print"<item>\n".6645"<title>". esc_html($co{'title'}) ."</title>\n".6646"<author>". esc_html($co{'author'}) ."</author>\n".6647"<pubDate>$cd{'rfc2822'}</pubDate>\n".6648"<guid isPermaLink=\"true\">$co_url</guid>\n".6649"<link>$co_url</link>\n".6650"<description>". esc_html($co{'title'}) ."</description>\n".6651"<content:encoded>".6652"<![CDATA[\n";6653}elsif($formateq'atom') {6654print"<entry>\n".6655"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6656"<updated>$cd{'iso-8601'}</updated>\n".6657"<author>\n".6658" <name>". esc_html($co{'author_name'}) ."</name>\n";6659if($co{'author_email'}) {6660print" <email>". esc_html($co{'author_email'}) ."</email>\n";6661}6662print"</author>\n".6663# use committer for contributor6664"<contributor>\n".6665" <name>". esc_html($co{'committer_name'}) ."</name>\n";6666if($co{'committer_email'}) {6667print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6668}6669print"</contributor>\n".6670"<published>$cd{'iso-8601'}</published>\n".6671"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6672"<id>$co_url</id>\n".6673"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6674"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6675}6676my$comment=$co{'comment'};6677print"<pre>\n";6678foreachmy$line(@$comment) {6679$line= esc_html($line);6680print"$line\n";6681}6682print"</pre><ul>\n";6683foreachmy$difftree_line(@difftree) {6684my%difftree= parse_difftree_raw_line($difftree_line);6685next if!$difftree{'from_id'};66866687my$file=$difftree{'file'} ||$difftree{'to_file'};66886689print"<li>".6690"[".6691$cgi->a({-href => href(-full=>1, action=>"blobdiff",6692 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6693 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6694 file_name=>$file, file_parent=>$difftree{'from_file'}),6695-title =>"diff"},'D');6696if($have_blame) {6697print$cgi->a({-href => href(-full=>1, action=>"blame",6698 file_name=>$file, hash_base=>$commit),6699-title =>"blame"},'B');6700}6701# if this is not a feed of a file history6702if(!defined$file_name||$file_namene$file) {6703print$cgi->a({-href => href(-full=>1, action=>"history",6704 file_name=>$file, hash=>$commit),6705-title =>"history"},'H');6706}6707$file= esc_path($file);6708print"] ".6709"$file</li>\n";6710}6711if($formateq'rss') {6712print"</ul>]]>\n".6713"</content:encoded>\n".6714"</item>\n";6715}elsif($formateq'atom') {6716print"</ul>\n</div>\n".6717"</content>\n".6718"</entry>\n";6719}6720}67216722# end of feed6723if($formateq'rss') {6724print"</channel>\n</rss>\n";6725}elsif($formateq'atom') {6726print"</feed>\n";6727}6728}67296730sub git_rss {6731 git_feed('rss');6732}67336734sub git_atom {6735 git_feed('atom');6736}67376738sub git_opml {6739my@list= git_get_projects_list();67406741print$cgi->header(6742-type =>'text/xml',6743-charset =>'utf-8',6744-content_disposition =>'inline; filename="opml.xml"');67456746print<<XML;6747<?xml version="1.0" encoding="utf-8"?>6748<opml version="1.0">6749<head>6750 <title>$site_nameOPML Export</title>6751</head>6752<body>6753<outline text="git RSS feeds">6754XML67556756foreachmy$pr(@list) {6757my%proj=%$pr;6758my$head= git_get_head_hash($proj{'path'});6759if(!defined$head) {6760next;6761}6762$git_dir="$projectroot/$proj{'path'}";6763my%co= parse_commit($head);6764if(!%co) {6765next;6766}67676768my$path= esc_html(chop_str($proj{'path'},25,5));6769my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6770my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6771print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6772}6773print<<XML;6774</outline>6775</body>6776</opml>6777XML6778}