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;1146returnundefunlessdefined$str;1147if(utf8::valid($str)) {1148 utf8::decode($str);1149return$str;1150}else{1151return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1152}1153}11541155# quote unsafe chars, but keep the slash, even when it's not1156# correct, but quoted slashes look too horrible in bookmarks1157sub esc_param {1158my$str=shift;1159returnundefunlessdefined$str;1160$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1161$str=~s/ /\+/g;1162return$str;1163}11641165# quote unsafe chars in whole URL, so some charactrs cannot be quoted1166sub esc_url {1167my$str=shift;1168returnundefunlessdefined$str;1169$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1170$str=~s/\+/%2B/g;1171$str=~s/ /\+/g;1172return$str;1173}11741175# replace invalid utf8 character with SUBSTITUTION sequence1176sub esc_html {1177my$str=shift;1178my%opts=@_;11791180returnundefunlessdefined$str;11811182$str= to_utf8($str);1183$str=$cgi->escapeHTML($str);1184if($opts{'-nbsp'}) {1185$str=~s/ / /g;1186}1187$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1188return$str;1189}11901191# quote control characters and escape filename to HTML1192sub esc_path {1193my$str=shift;1194my%opts=@_;11951196returnundefunlessdefined$str;11971198$str= to_utf8($str);1199$str=$cgi->escapeHTML($str);1200if($opts{'-nbsp'}) {1201$str=~s/ / /g;1202}1203$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1204return$str;1205}12061207# Make control characters "printable", using character escape codes (CEC)1208sub quot_cec {1209my$cntrl=shift;1210my%opts=@_;1211my%es= (# character escape codes, aka escape sequences1212"\t"=>'\t',# tab (HT)1213"\n"=>'\n',# line feed (LF)1214"\r"=>'\r',# carrige return (CR)1215"\f"=>'\f',# form feed (FF)1216"\b"=>'\b',# backspace (BS)1217"\a"=>'\a',# alarm (bell) (BEL)1218"\e"=>'\e',# escape (ESC)1219"\013"=>'\v',# vertical tab (VT)1220"\000"=>'\0',# nul character (NUL)1221);1222my$chr= ( (exists$es{$cntrl})1223?$es{$cntrl}1224:sprintf('\%2x',ord($cntrl)) );1225if($opts{-nohtml}) {1226return$chr;1227}else{1228return"<span class=\"cntrl\">$chr</span>";1229}1230}12311232# Alternatively use unicode control pictures codepoints,1233# Unicode "printable representation" (PR)1234sub quot_upr {1235my$cntrl=shift;1236my%opts=@_;12371238my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1239if($opts{-nohtml}) {1240return$chr;1241}else{1242return"<span class=\"cntrl\">$chr</span>";1243}1244}12451246# git may return quoted and escaped filenames1247sub unquote {1248my$str=shift;12491250sub unq {1251my$seq=shift;1252my%es= (# character escape codes, aka escape sequences1253't'=>"\t",# tab (HT, TAB)1254'n'=>"\n",# newline (NL)1255'r'=>"\r",# return (CR)1256'f'=>"\f",# form feed (FF)1257'b'=>"\b",# backspace (BS)1258'a'=>"\a",# alarm (bell) (BEL)1259'e'=>"\e",# escape (ESC)1260'v'=>"\013",# vertical tab (VT)1261);12621263if($seq=~m/^[0-7]{1,3}$/) {1264# octal char sequence1265returnchr(oct($seq));1266}elsif(exists$es{$seq}) {1267# C escape sequence, aka character escape code1268return$es{$seq};1269}1270# quoted ordinary character1271return$seq;1272}12731274if($str=~m/^"(.*)"$/) {1275# needs unquoting1276$str=$1;1277$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1278}1279return$str;1280}12811282# escape tabs (convert tabs to spaces)1283sub untabify {1284my$line=shift;12851286while((my$pos=index($line,"\t")) != -1) {1287if(my$count= (8- ($pos%8))) {1288my$spaces=' ' x $count;1289$line=~s/\t/$spaces/;1290}1291}12921293return$line;1294}12951296sub project_in_list {1297my$project=shift;1298my@list= git_get_projects_list();1299return@list&&scalar(grep{$_->{'path'}eq$project}@list);1300}13011302## ----------------------------------------------------------------------1303## HTML aware string manipulation13041305# Try to chop given string on a word boundary between position1306# $len and $len+$add_len. If there is no word boundary there,1307# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1308# (marking chopped part) would be longer than given string.1309sub chop_str {1310my$str=shift;1311my$len=shift;1312my$add_len=shift||10;1313my$where=shift||'right';# 'left' | 'center' | 'right'13141315# Make sure perl knows it is utf8 encoded so we don't1316# cut in the middle of a utf8 multibyte char.1317$str= to_utf8($str);13181319# allow only $len chars, but don't cut a word if it would fit in $add_len1320# if it doesn't fit, cut it if it's still longer than the dots we would add1321# remove chopped character entities entirely13221323# when chopping in the middle, distribute $len into left and right part1324# return early if chopping wouldn't make string shorter1325if($whereeq'center') {1326return$strif($len+5>=length($str));# filler is length 51327$len=int($len/2);1328}else{1329return$strif($len+4>=length($str));# filler is length 41330}13311332# regexps: ending and beginning with word part up to $add_len1333my$endre=qr/.{$len}\w{0,$add_len}/;1334my$begre=qr/\w{0,$add_len}.{$len}/;13351336if($whereeq'left') {1337$str=~m/^(.*?)($begre)$/;1338my($lead,$body) = ($1,$2);1339if(length($lead) >4) {1340$lead=" ...";1341}1342return"$lead$body";13431344}elsif($whereeq'center') {1345$str=~m/^($endre)(.*)$/;1346my($left,$str) = ($1,$2);1347$str=~m/^(.*?)($begre)$/;1348my($mid,$right) = ($1,$2);1349if(length($mid) >5) {1350$mid=" ... ";1351}1352return"$left$mid$right";13531354}else{1355$str=~m/^($endre)(.*)$/;1356my$body=$1;1357my$tail=$2;1358if(length($tail) >4) {1359$tail="... ";1360}1361return"$body$tail";1362}1363}13641365# takes the same arguments as chop_str, but also wraps a <span> around the1366# result with a title attribute if it does get chopped. Additionally, the1367# string is HTML-escaped.1368sub chop_and_escape_str {1369my($str) =@_;13701371my$chopped= chop_str(@_);1372if($choppedeq$str) {1373return esc_html($chopped);1374}else{1375$str=~s/[[:cntrl:]]/?/g;1376return$cgi->span({-title=>$str}, esc_html($chopped));1377}1378}13791380## ----------------------------------------------------------------------1381## functions returning short strings13821383# CSS class for given age value (in seconds)1384sub age_class {1385my$age=shift;13861387if(!defined$age) {1388return"noage";1389}elsif($age<60*60*2) {1390return"age0";1391}elsif($age<60*60*24*2) {1392return"age1";1393}else{1394return"age2";1395}1396}13971398# convert age in seconds to "nn units ago" string1399sub age_string {1400my$age=shift;1401my$age_str;14021403if($age>60*60*24*365*2) {1404$age_str= (int$age/60/60/24/365);1405$age_str.=" years ago";1406}elsif($age>60*60*24*(365/12)*2) {1407$age_str=int$age/60/60/24/(365/12);1408$age_str.=" months ago";1409}elsif($age>60*60*24*7*2) {1410$age_str=int$age/60/60/24/7;1411$age_str.=" weeks ago";1412}elsif($age>60*60*24*2) {1413$age_str=int$age/60/60/24;1414$age_str.=" days ago";1415}elsif($age>60*60*2) {1416$age_str=int$age/60/60;1417$age_str.=" hours ago";1418}elsif($age>60*2) {1419$age_str=int$age/60;1420$age_str.=" min ago";1421}elsif($age>2) {1422$age_str=int$age;1423$age_str.=" sec ago";1424}else{1425$age_str.=" right now";1426}1427return$age_str;1428}14291430useconstant{1431 S_IFINVALID =>0030000,1432 S_IFGITLINK =>0160000,1433};14341435# submodule/subproject, a commit object reference1436sub S_ISGITLINK {1437my$mode=shift;14381439return(($mode& S_IFMT) == S_IFGITLINK)1440}14411442# convert file mode in octal to symbolic file mode string1443sub mode_str {1444my$mode=oct shift;14451446if(S_ISGITLINK($mode)) {1447return'm---------';1448}elsif(S_ISDIR($mode& S_IFMT)) {1449return'drwxr-xr-x';1450}elsif(S_ISLNK($mode)) {1451return'lrwxrwxrwx';1452}elsif(S_ISREG($mode)) {1453# git cares only about the executable bit1454if($mode& S_IXUSR) {1455return'-rwxr-xr-x';1456}else{1457return'-rw-r--r--';1458};1459}else{1460return'----------';1461}1462}14631464# convert file mode in octal to file type string1465sub file_type {1466my$mode=shift;14671468if($mode!~m/^[0-7]+$/) {1469return$mode;1470}else{1471$mode=oct$mode;1472}14731474if(S_ISGITLINK($mode)) {1475return"submodule";1476}elsif(S_ISDIR($mode& S_IFMT)) {1477return"directory";1478}elsif(S_ISLNK($mode)) {1479return"symlink";1480}elsif(S_ISREG($mode)) {1481return"file";1482}else{1483return"unknown";1484}1485}14861487# convert file mode in octal to file type description string1488sub file_type_long {1489my$mode=shift;14901491if($mode!~m/^[0-7]+$/) {1492return$mode;1493}else{1494$mode=oct$mode;1495}14961497if(S_ISGITLINK($mode)) {1498return"submodule";1499}elsif(S_ISDIR($mode& S_IFMT)) {1500return"directory";1501}elsif(S_ISLNK($mode)) {1502return"symlink";1503}elsif(S_ISREG($mode)) {1504if($mode& S_IXUSR) {1505return"executable";1506}else{1507return"file";1508};1509}else{1510return"unknown";1511}1512}151315141515## ----------------------------------------------------------------------1516## functions returning short HTML fragments, or transforming HTML fragments1517## which don't belong to other sections15181519# format line of commit message.1520sub format_log_line_html {1521my$line=shift;15221523$line= esc_html($line, -nbsp=>1);1524$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1525$cgi->a({-href => href(action=>"object", hash=>$1),1526-class=>"text"},$1);1527}eg;15281529return$line;1530}15311532# format marker of refs pointing to given object15331534# the destination action is chosen based on object type and current context:1535# - for annotated tags, we choose the tag view unless it's the current view1536# already, in which case we go to shortlog view1537# - for other refs, we keep the current view if we're in history, shortlog or1538# log view, and select shortlog otherwise1539sub format_ref_marker {1540my($refs,$id) =@_;1541my$markers='';15421543if(defined$refs->{$id}) {1544foreachmy$ref(@{$refs->{$id}}) {1545# this code exploits the fact that non-lightweight tags are the1546# only indirect objects, and that they are the only objects for which1547# we want to use tag instead of shortlog as action1548my($type,$name) =qw();1549my$indirect= ($ref=~s/\^\{\}$//);1550# e.g. tags/v2.6.11 or heads/next1551if($ref=~m!^(.*?)s?/(.*)$!) {1552$type=$1;1553$name=$2;1554}else{1555$type="ref";1556$name=$ref;1557}15581559my$class=$type;1560$class.=" indirect"if$indirect;15611562my$dest_action="shortlog";15631564if($indirect) {1565$dest_action="tag"unless$actioneq"tag";1566}elsif($action=~/^(history|(short)?log)$/) {1567$dest_action=$action;1568}15691570my$dest="";1571$dest.="refs/"unless$ref=~ m!^refs/!;1572$dest.=$ref;15731574my$link=$cgi->a({1575-href => href(1576 action=>$dest_action,1577 hash=>$dest1578)},$name);15791580$markers.=" <span class=\"$class\"title=\"$ref\">".1581$link."</span>";1582}1583}15841585if($markers) {1586return' <span class="refs">'.$markers.'</span>';1587}else{1588return"";1589}1590}15911592# format, perhaps shortened and with markers, title line1593sub format_subject_html {1594my($long,$short,$href,$extra) =@_;1595$extra=''unlessdefined($extra);15961597if(length($short) <length($long)) {1598$long=~s/[[:cntrl:]]/?/g;1599return$cgi->a({-href =>$href, -class=>"list subject",1600-title => to_utf8($long)},1601 esc_html($short)) .$extra;1602}else{1603return$cgi->a({-href =>$href, -class=>"list subject"},1604 esc_html($long)) .$extra;1605}1606}16071608# Rather than recomputing the url for an email multiple times, we cache it1609# after the first hit. This gives a visible benefit in views where the avatar1610# for the same email is used repeatedly (e.g. shortlog).1611# The cache is shared by all avatar engines (currently gravatar only), which1612# are free to use it as preferred. Since only one avatar engine is used for any1613# given page, there's no risk for cache conflicts.1614our%avatar_cache= ();16151616# Compute the picon url for a given email, by using the picon search service over at1617# http://www.cs.indiana.edu/picons/search.html1618sub picon_url {1619my$email=lc shift;1620if(!$avatar_cache{$email}) {1621my($user,$domain) =split('@',$email);1622$avatar_cache{$email} =1623"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1624"$domain/$user/".1625"users+domains+unknown/up/single";1626}1627return$avatar_cache{$email};1628}16291630# Compute the gravatar url for a given email, if it's not in the cache already.1631# Gravatar stores only the part of the URL before the size, since that's the1632# one computationally more expensive. This also allows reuse of the cache for1633# different sizes (for this particular engine).1634sub gravatar_url {1635my$email=lc shift;1636my$size=shift;1637$avatar_cache{$email} ||=1638"http://www.gravatar.com/avatar/".1639 Digest::MD5::md5_hex($email) ."?s=";1640return$avatar_cache{$email} .$size;1641}16421643# Insert an avatar for the given $email at the given $size if the feature1644# is enabled.1645sub git_get_avatar {1646my($email,%opts) =@_;1647my$pre_white= ($opts{-pad_before} ?" ":"");1648my$post_white= ($opts{-pad_after} ?" ":"");1649$opts{-size} ||='default';1650my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1651my$url="";1652if($git_avatareq'gravatar') {1653$url= gravatar_url($email,$size);1654}elsif($git_avatareq'picon') {1655$url= picon_url($email);1656}1657# Other providers can be added by extending the if chain, defining $url1658# as needed. If no variant puts something in $url, we assume avatars1659# are completely disabled/unavailable.1660if($url) {1661return$pre_white.1662"<img width=\"$size\"".1663"class=\"avatar\"".1664"src=\"$url\"".1665"alt=\"\"".1666"/>".$post_white;1667}else{1668return"";1669}1670}16711672sub format_search_author {1673my($author,$searchtype,$displaytext) =@_;1674my$have_search= gitweb_check_feature('search');16751676if($have_search) {1677my$performed="";1678if($searchtypeeq'author') {1679$performed="authored";1680}elsif($searchtypeeq'committer') {1681$performed="committed";1682}16831684return$cgi->a({-href => href(action=>"search", hash=>$hash,1685 searchtext=>$author,1686 searchtype=>$searchtype),class=>"list",1687 title=>"Search for commits$performedby$author"},1688$displaytext);16891690}else{1691return$displaytext;1692}1693}16941695# format the author name of the given commit with the given tag1696# the author name is chopped and escaped according to the other1697# optional parameters (see chop_str).1698sub format_author_html {1699my$tag=shift;1700my$co=shift;1701my$author= chop_and_escape_str($co->{'author_name'},@_);1702return"<$tagclass=\"author\">".1703 format_search_author($co->{'author_name'},"author",1704 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1705$author) .1706"</$tag>";1707}17081709# format git diff header line, i.e. "diff --(git|combined|cc) ..."1710sub format_git_diff_header_line {1711my$line=shift;1712my$diffinfo=shift;1713my($from,$to) =@_;17141715if($diffinfo->{'nparents'}) {1716# combined diff1717$line=~s!^(diff (.*?) )"?.*$!$1!;1718if($to->{'href'}) {1719$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1720 esc_path($to->{'file'}));1721}else{# file was deleted (no href)1722$line.= esc_path($to->{'file'});1723}1724}else{1725# "ordinary" diff1726$line=~s!^(diff (.*?) )"?a/.*$!$1!;1727if($from->{'href'}) {1728$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1729'a/'. esc_path($from->{'file'}));1730}else{# file was added (no href)1731$line.='a/'. esc_path($from->{'file'});1732}1733$line.=' ';1734if($to->{'href'}) {1735$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1736'b/'. esc_path($to->{'file'}));1737}else{# file was deleted1738$line.='b/'. esc_path($to->{'file'});1739}1740}17411742return"<div class=\"diff header\">$line</div>\n";1743}17441745# format extended diff header line, before patch itself1746sub format_extended_diff_header_line {1747my$line=shift;1748my$diffinfo=shift;1749my($from,$to) =@_;17501751# match <path>1752if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1753$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1754 esc_path($from->{'file'}));1755}1756if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1757$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1758 esc_path($to->{'file'}));1759}1760# match single <mode>1761if($line=~m/\s(\d{6})$/) {1762$line.='<span class="info"> ('.1763 file_type_long($1) .1764')</span>';1765}1766# match <hash>1767if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1768# can match only for combined diff1769$line='index ';1770for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1771if($from->{'href'}[$i]) {1772$line.=$cgi->a({-href=>$from->{'href'}[$i],1773-class=>"hash"},1774substr($diffinfo->{'from_id'}[$i],0,7));1775}else{1776$line.='0' x 7;1777}1778# separator1779$line.=','if($i<$diffinfo->{'nparents'} -1);1780}1781$line.='..';1782if($to->{'href'}) {1783$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1784substr($diffinfo->{'to_id'},0,7));1785}else{1786$line.='0' x 7;1787}17881789}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1790# can match only for ordinary diff1791my($from_link,$to_link);1792if($from->{'href'}) {1793$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1794substr($diffinfo->{'from_id'},0,7));1795}else{1796$from_link='0' x 7;1797}1798if($to->{'href'}) {1799$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1800substr($diffinfo->{'to_id'},0,7));1801}else{1802$to_link='0' x 7;1803}1804my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1805$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1806}18071808return$line."<br/>\n";1809}18101811# format from-file/to-file diff header1812sub format_diff_from_to_header {1813my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1814my$line;1815my$result='';18161817$line=$from_line;1818#assert($line =~ m/^---/) if DEBUG;1819# no extra formatting for "^--- /dev/null"1820if(!$diffinfo->{'nparents'}) {1821# ordinary (single parent) diff1822if($line=~m!^--- "?a/!) {1823if($from->{'href'}) {1824$line='--- a/'.1825$cgi->a({-href=>$from->{'href'}, -class=>"path"},1826 esc_path($from->{'file'}));1827}else{1828$line='--- a/'.1829 esc_path($from->{'file'});1830}1831}1832$result.= qq!<div class="diff from_file">$line</div>\n!;18331834}else{1835# combined diff (merge commit)1836for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1837if($from->{'href'}[$i]) {1838$line='--- '.1839$cgi->a({-href=>href(action=>"blobdiff",1840 hash_parent=>$diffinfo->{'from_id'}[$i],1841 hash_parent_base=>$parents[$i],1842 file_parent=>$from->{'file'}[$i],1843 hash=>$diffinfo->{'to_id'},1844 hash_base=>$hash,1845 file_name=>$to->{'file'}),1846-class=>"path",1847-title=>"diff". ($i+1)},1848$i+1) .1849'/'.1850$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1851 esc_path($from->{'file'}[$i]));1852}else{1853$line='--- /dev/null';1854}1855$result.= qq!<div class="diff from_file">$line</div>\n!;1856}1857}18581859$line=$to_line;1860#assert($line =~ m/^\+\+\+/) if DEBUG;1861# no extra formatting for "^+++ /dev/null"1862if($line=~m!^\+\+\+ "?b/!) {1863if($to->{'href'}) {1864$line='+++ b/'.1865$cgi->a({-href=>$to->{'href'}, -class=>"path"},1866 esc_path($to->{'file'}));1867}else{1868$line='+++ b/'.1869 esc_path($to->{'file'});1870}1871}1872$result.= qq!<div class="diff to_file">$line</div>\n!;18731874return$result;1875}18761877# create note for patch simplified by combined diff1878sub format_diff_cc_simplified {1879my($diffinfo,@parents) =@_;1880my$result='';18811882$result.="<div class=\"diff header\">".1883"diff --cc ";1884if(!is_deleted($diffinfo)) {1885$result.=$cgi->a({-href => href(action=>"blob",1886 hash_base=>$hash,1887 hash=>$diffinfo->{'to_id'},1888 file_name=>$diffinfo->{'to_file'}),1889-class=>"path"},1890 esc_path($diffinfo->{'to_file'}));1891}else{1892$result.= esc_path($diffinfo->{'to_file'});1893}1894$result.="</div>\n".# class="diff header"1895"<div class=\"diff nodifferences\">".1896"Simple merge".1897"</div>\n";# class="diff nodifferences"18981899return$result;1900}19011902# format patch (diff) line (not to be used for diff headers)1903sub format_diff_line {1904my$line=shift;1905my($from,$to) =@_;1906my$diff_class="";19071908chomp$line;19091910if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1911# combined diff1912my$prefix=substr($line,0,scalar@{$from->{'href'}});1913if($line=~m/^\@{3}/) {1914$diff_class=" chunk_header";1915}elsif($line=~m/^\\/) {1916$diff_class=" incomplete";1917}elsif($prefix=~tr/+/+/) {1918$diff_class=" add";1919}elsif($prefix=~tr/-/-/) {1920$diff_class=" rem";1921}1922}else{1923# assume ordinary diff1924my$char=substr($line,0,1);1925if($chareq'+') {1926$diff_class=" add";1927}elsif($chareq'-') {1928$diff_class=" rem";1929}elsif($chareq'@') {1930$diff_class=" chunk_header";1931}elsif($chareq"\\") {1932$diff_class=" incomplete";1933}1934}1935$line= untabify($line);1936if($from&&$to&&$line=~m/^\@{2} /) {1937my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1938$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;19391940$from_lines=0unlessdefined$from_lines;1941$to_lines=0unlessdefined$to_lines;19421943if($from->{'href'}) {1944$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1945-class=>"list"},$from_text);1946}1947if($to->{'href'}) {1948$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1949-class=>"list"},$to_text);1950}1951$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1952"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1953return"<div class=\"diff$diff_class\">$line</div>\n";1954}elsif($from&&$to&&$line=~m/^\@{3}/) {1955my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1956my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);19571958@from_text=split(' ',$ranges);1959for(my$i=0;$i<@from_text; ++$i) {1960($from_start[$i],$from_nlines[$i]) =1961(split(',',substr($from_text[$i],1)),0);1962}19631964$to_text=pop@from_text;1965$to_start=pop@from_start;1966$to_nlines=pop@from_nlines;19671968$line="<span class=\"chunk_info\">$prefix";1969for(my$i=0;$i<@from_text; ++$i) {1970if($from->{'href'}[$i]) {1971$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1972-class=>"list"},$from_text[$i]);1973}else{1974$line.=$from_text[$i];1975}1976$line.=" ";1977}1978if($to->{'href'}) {1979$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1980-class=>"list"},$to_text);1981}else{1982$line.=$to_text;1983}1984$line.="$prefix</span>".1985"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1986return"<div class=\"diff$diff_class\">$line</div>\n";1987}1988return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1989}19901991# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1992# linked. Pass the hash of the tree/commit to snapshot.1993sub format_snapshot_links {1994my($hash) =@_;1995my$num_fmts=@snapshot_fmts;1996if($num_fmts>1) {1997# A parenthesized list of links bearing format names.1998# e.g. "snapshot (_tar.gz_ _zip_)"1999return"snapshot (".join(' ',map2000$cgi->a({2001-href => href(2002 action=>"snapshot",2003 hash=>$hash,2004 snapshot_format=>$_2005)2006},$known_snapshot_formats{$_}{'display'})2007,@snapshot_fmts) .")";2008}elsif($num_fmts==1) {2009# A single "snapshot" link whose tooltip bears the format name.2010# i.e. "_snapshot_"2011my($fmt) =@snapshot_fmts;2012return2013$cgi->a({2014-href => href(2015 action=>"snapshot",2016 hash=>$hash,2017 snapshot_format=>$fmt2018),2019-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2020},"snapshot");2021}else{# $num_fmts == 02022returnundef;2023}2024}20252026## ......................................................................2027## functions returning values to be passed, perhaps after some2028## transformation, to other functions; e.g. returning arguments to href()20292030# returns hash to be passed to href to generate gitweb URL2031# in -title key it returns description of link2032sub get_feed_info {2033my$format=shift||'Atom';2034my%res= (action =>lc($format));20352036# feed links are possible only for project views2037return unless(defined$project);2038# some views should link to OPML, or to generic project feed,2039# or don't have specific feed yet (so they should use generic)2040return if($action=~/^(?:tags|heads|forks|tag|search)$/x);20412042my$branch;2043# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2044# from tag links; this also makes possible to detect branch links2045if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2046(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2047$branch=$1;2048}2049# find log type for feed description (title)2050my$type='log';2051if(defined$file_name) {2052$type="history of$file_name";2053$type.="/"if($actioneq'tree');2054$type.=" on '$branch'"if(defined$branch);2055}else{2056$type="log of$branch"if(defined$branch);2057}20582059$res{-title} =$type;2060$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2061$res{'file_name'} =$file_name;20622063return%res;2064}20652066## ----------------------------------------------------------------------2067## git utility subroutines, invoking git commands20682069# returns path to the core git executable and the --git-dir parameter as list2070sub git_cmd {2071$number_of_git_cmds++;2072return$GIT,'--git-dir='.$git_dir;2073}20742075# quote the given arguments for passing them to the shell2076# quote_command("command", "arg 1", "arg with ' and ! characters")2077# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2078# Try to avoid using this function wherever possible.2079sub quote_command {2080returnjoin(' ',2081map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2082}20832084# get HEAD ref of given project as hash2085sub git_get_head_hash {2086return git_get_full_hash(shift,'HEAD');2087}20882089sub git_get_full_hash {2090return git_get_hash(@_);2091}20922093sub git_get_short_hash {2094return git_get_hash(@_,'--short=7');2095}20962097sub git_get_hash {2098my($project,$hash,@options) =@_;2099my$o_git_dir=$git_dir;2100my$retval=undef;2101$git_dir="$projectroot/$project";2102if(open my$fd,'-|', git_cmd(),'rev-parse',2103'--verify','-q',@options,$hash) {2104$retval= <$fd>;2105chomp$retvalifdefined$retval;2106close$fd;2107}2108if(defined$o_git_dir) {2109$git_dir=$o_git_dir;2110}2111return$retval;2112}21132114# get type of given object2115sub git_get_type {2116my$hash=shift;21172118open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2119my$type= <$fd>;2120close$fdorreturn;2121chomp$type;2122return$type;2123}21242125# repository configuration2126our$config_file='';2127our%config;21282129# store multiple values for single key as anonymous array reference2130# single values stored directly in the hash, not as [ <value> ]2131sub hash_set_multi {2132my($hash,$key,$value) =@_;21332134if(!exists$hash->{$key}) {2135$hash->{$key} =$value;2136}elsif(!ref$hash->{$key}) {2137$hash->{$key} = [$hash->{$key},$value];2138}else{2139push@{$hash->{$key}},$value;2140}2141}21422143# return hash of git project configuration2144# optionally limited to some section, e.g. 'gitweb'2145sub git_parse_project_config {2146my$section_regexp=shift;2147my%config;21482149local$/="\0";21502151open my$fh,"-|", git_cmd(),"config",'-z','-l',2152orreturn;21532154while(my$keyval= <$fh>) {2155chomp$keyval;2156my($key,$value) =split(/\n/,$keyval,2);21572158 hash_set_multi(\%config,$key,$value)2159if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2160}2161close$fh;21622163return%config;2164}21652166# convert config value to boolean: 'true' or 'false'2167# no value, number > 0, 'true' and 'yes' values are true2168# rest of values are treated as false (never as error)2169sub config_to_bool {2170my$val=shift;21712172return1if!defined$val;# section.key21732174# strip leading and trailing whitespace2175$val=~s/^\s+//;2176$val=~s/\s+$//;21772178return(($val=~/^\d+$/&&$val) ||# section.key = 12179($val=~/^(?:true|yes)$/i));# section.key = true2180}21812182# convert config value to simple decimal number2183# an optional value suffix of 'k', 'm', or 'g' will cause the value2184# to be multiplied by 1024, 1048576, or 10737418242185sub config_to_int {2186my$val=shift;21872188# strip leading and trailing whitespace2189$val=~s/^\s+//;2190$val=~s/\s+$//;21912192if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2193$unit=lc($unit);2194# unknown unit is treated as 12195return$num* ($uniteq'g'?1073741824:2196$uniteq'm'?1048576:2197$uniteq'k'?1024:1);2198}2199return$val;2200}22012202# convert config value to array reference, if needed2203sub config_to_multi {2204my$val=shift;22052206returnref($val) ?$val: (defined($val) ? [$val] : []);2207}22082209sub git_get_project_config {2210my($key,$type) =@_;22112212# key sanity check2213return unless($key);2214$key=~s/^gitweb\.//;2215return if($key=~m/\W/);22162217# type sanity check2218if(defined$type) {2219$type=~s/^--//;2220$type=undef2221unless($typeeq'bool'||$typeeq'int');2222}22232224# get config2225if(!defined$config_file||2226$config_filene"$git_dir/config") {2227%config= git_parse_project_config('gitweb');2228$config_file="$git_dir/config";2229}22302231# check if config variable (key) exists2232return unlessexists$config{"gitweb.$key"};22332234# ensure given type2235if(!defined$type) {2236return$config{"gitweb.$key"};2237}elsif($typeeq'bool') {2238# backward compatibility: 'git config --bool' returns true/false2239return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2240}elsif($typeeq'int') {2241return config_to_int($config{"gitweb.$key"});2242}2243return$config{"gitweb.$key"};2244}22452246# get hash of given path at given ref2247sub git_get_hash_by_path {2248my$base=shift;2249my$path=shift||returnundef;2250my$type=shift;22512252$path=~ s,/+$,,;22532254open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2255or die_error(500,"Open git-ls-tree failed");2256my$line= <$fd>;2257close$fdorreturnundef;22582259if(!defined$line) {2260# there is no tree or hash given by $path at $base2261returnundef;2262}22632264#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2265$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2266if(defined$type&&$typene$2) {2267# type doesn't match2268returnundef;2269}2270return$3;2271}22722273# get path of entry with given hash at given tree-ish (ref)2274# used to get 'from' filename for combined diff (merge commit) for renames2275sub git_get_path_by_hash {2276my$base=shift||return;2277my$hash=shift||return;22782279local$/="\0";22802281open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2282orreturnundef;2283while(my$line= <$fd>) {2284chomp$line;22852286#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2287#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2288if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2289close$fd;2290return$1;2291}2292}2293close$fd;2294returnundef;2295}22962297## ......................................................................2298## git utility functions, directly accessing git repository22992300sub git_get_project_description {2301my$path=shift;23022303$git_dir="$projectroot/$path";2304open my$fd,'<',"$git_dir/description"2305orreturn git_get_project_config('description');2306my$descr= <$fd>;2307close$fd;2308if(defined$descr) {2309chomp$descr;2310}2311return$descr;2312}23132314sub git_get_project_ctags {2315my$path=shift;2316my$ctags= {};23172318$git_dir="$projectroot/$path";2319opendir my$dh,"$git_dir/ctags"2320orreturn$ctags;2321foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2322open my$ct,'<',$_ornext;2323my$val= <$ct>;2324chomp$val;2325close$ct;2326my$ctag=$_;$ctag=~ s#.*/##;2327$ctags->{$ctag} =$val;2328}2329closedir$dh;2330$ctags;2331}23322333sub git_populate_project_tagcloud {2334my$ctags=shift;23352336# First, merge different-cased tags; tags vote on casing2337my%ctags_lc;2338foreach(keys%$ctags) {2339$ctags_lc{lc$_}->{count} +=$ctags->{$_};2340if(not$ctags_lc{lc$_}->{topcount}2341or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2342$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2343$ctags_lc{lc$_}->{topname} =$_;2344}2345}23462347my$cloud;2348if(eval{require HTML::TagCloud;1; }) {2349$cloud= HTML::TagCloud->new;2350foreach(sort keys%ctags_lc) {2351# Pad the title with spaces so that the cloud looks2352# less crammed.2353my$title=$ctags_lc{$_}->{topname};2354$title=~s/ / /g;2355$title=~s/^/ /g;2356$title=~s/$/ /g;2357$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2358}2359}else{2360$cloud= \%ctags_lc;2361}2362$cloud;2363}23642365sub git_show_project_tagcloud {2366my($cloud,$count) =@_;2367print STDERR ref($cloud)."..\n";2368if(ref$cloudeq'HTML::TagCloud') {2369return$cloud->html_and_css($count);2370}else{2371my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2372return'<p align="center">'.join(', ',map{2373"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2374}splice(@tags,0,$count)) .'</p>';2375}2376}23772378sub git_get_project_url_list {2379my$path=shift;23802381$git_dir="$projectroot/$path";2382open my$fd,'<',"$git_dir/cloneurl"2383orreturnwantarray?2384@{ config_to_multi(git_get_project_config('url')) } :2385 config_to_multi(git_get_project_config('url'));2386my@git_project_url_list=map{chomp;$_} <$fd>;2387close$fd;23882389returnwantarray?@git_project_url_list: \@git_project_url_list;2390}23912392sub git_get_projects_list {2393my($filter) =@_;2394my@list;23952396$filter||='';2397$filter=~s/\.git$//;23982399my$check_forks= gitweb_check_feature('forks');24002401if(-d $projects_list) {2402# search in directory2403my$dir=$projects_list. ($filter?"/$filter":'');2404# remove the trailing "/"2405$dir=~s!/+$!!;2406my$pfxlen=length("$dir");2407my$pfxdepth= ($dir=~tr!/!!);24082409 File::Find::find({2410 follow_fast =>1,# follow symbolic links2411 follow_skip =>2,# ignore duplicates2412 dangling_symlinks =>0,# ignore dangling symlinks, silently2413 wanted =>sub{2414# skip project-list toplevel, if we get it.2415return if(m!^[/.]$!);2416# only directories can be git repositories2417return unless(-d $_);2418# don't traverse too deep (Find is super slow on os x)2419if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2420$File::Find::prune =1;2421return;2422}24232424my$subdir=substr($File::Find::name,$pfxlen+1);2425# we check related file in $projectroot2426my$path= ($filter?"$filter/":'') .$subdir;2427if(check_export_ok("$projectroot/$path")) {2428push@list, { path =>$path};2429$File::Find::prune =1;2430}2431},2432},"$dir");24332434}elsif(-f $projects_list) {2435# read from file(url-encoded):2436# 'git%2Fgit.git Linus+Torvalds'2437# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2438# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2439my%paths;2440open my$fd,'<',$projects_listorreturn;2441 PROJECT:2442while(my$line= <$fd>) {2443chomp$line;2444my($path,$owner) =split' ',$line;2445$path= unescape($path);2446$owner= unescape($owner);2447if(!defined$path) {2448next;2449}2450if($filterne'') {2451# looking for forks;2452my$pfx=substr($path,0,length($filter));2453if($pfxne$filter) {2454next PROJECT;2455}2456my$sfx=substr($path,length($filter));2457if($sfx!~/^\/.*\.git$/) {2458next PROJECT;2459}2460}elsif($check_forks) {2461 PATH:2462foreachmy$filter(keys%paths) {2463# looking for forks;2464my$pfx=substr($path,0,length($filter));2465if($pfxne$filter) {2466next PATH;2467}2468my$sfx=substr($path,length($filter));2469if($sfx!~/^\/.*\.git$/) {2470next PATH;2471}2472# is a fork, don't include it in2473# the list2474next PROJECT;2475}2476}2477if(check_export_ok("$projectroot/$path")) {2478my$pr= {2479 path =>$path,2480 owner => to_utf8($owner),2481};2482push@list,$pr;2483(my$forks_path=$path) =~s/\.git$//;2484$paths{$forks_path}++;2485}2486}2487close$fd;2488}2489return@list;2490}24912492our$gitweb_project_owner=undef;2493sub git_get_project_list_from_file {24942495return if(defined$gitweb_project_owner);24962497$gitweb_project_owner= {};2498# read from file (url-encoded):2499# 'git%2Fgit.git Linus+Torvalds'2500# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2501# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2502if(-f $projects_list) {2503open(my$fd,'<',$projects_list);2504while(my$line= <$fd>) {2505chomp$line;2506my($pr,$ow) =split' ',$line;2507$pr= unescape($pr);2508$ow= unescape($ow);2509$gitweb_project_owner->{$pr} = to_utf8($ow);2510}2511close$fd;2512}2513}25142515sub git_get_project_owner {2516my$project=shift;2517my$owner;25182519returnundefunless$project;2520$git_dir="$projectroot/$project";25212522if(!defined$gitweb_project_owner) {2523 git_get_project_list_from_file();2524}25252526if(exists$gitweb_project_owner->{$project}) {2527$owner=$gitweb_project_owner->{$project};2528}2529if(!defined$owner){2530$owner= git_get_project_config('owner');2531}2532if(!defined$owner) {2533$owner= get_file_owner("$git_dir");2534}25352536return$owner;2537}25382539sub git_get_last_activity {2540my($path) =@_;2541my$fd;25422543$git_dir="$projectroot/$path";2544open($fd,"-|", git_cmd(),'for-each-ref',2545'--format=%(committer)',2546'--sort=-committerdate',2547'--count=1',2548'refs/heads')orreturn;2549my$most_recent= <$fd>;2550close$fdorreturn;2551if(defined$most_recent&&2552$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2553my$timestamp=$1;2554my$age=time-$timestamp;2555return($age, age_string($age));2556}2557return(undef,undef);2558}25592560sub git_get_references {2561my$type=shift||"";2562my%refs;2563# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112564# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2565open my$fd,"-|", git_cmd(),"show-ref","--dereference",2566($type? ("--","refs/$type") : ())# use -- <pattern> if $type2567orreturn;25682569while(my$line= <$fd>) {2570chomp$line;2571if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2572if(defined$refs{$1}) {2573push@{$refs{$1}},$2;2574}else{2575$refs{$1} = [$2];2576}2577}2578}2579close$fdorreturn;2580return \%refs;2581}25822583sub git_get_rev_name_tags {2584my$hash=shift||returnundef;25852586open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2587orreturn;2588my$name_rev= <$fd>;2589close$fd;25902591if($name_rev=~ m|^$hash tags/(.*)$|) {2592return$1;2593}else{2594# catches also '$hash undefined' output2595returnundef;2596}2597}25982599## ----------------------------------------------------------------------2600## parse to hash functions26012602sub parse_date {2603my$epoch=shift;2604my$tz=shift||"-0000";26052606my%date;2607my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2608my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2609my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2610$date{'hour'} =$hour;2611$date{'minute'} =$min;2612$date{'mday'} =$mday;2613$date{'day'} =$days[$wday];2614$date{'month'} =$months[$mon];2615$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2616$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2617$date{'mday-time'} =sprintf"%d%s%02d:%02d",2618$mday,$months[$mon],$hour,$min;2619$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",26201900+$year,1+$mon,$mday,$hour,$min,$sec;26212622$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2623my$local=$epoch+ ((int$1+ ($2/60)) *3600);2624($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2625$date{'hour_local'} =$hour;2626$date{'minute_local'} =$min;2627$date{'tz_local'} =$tz;2628$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",26291900+$year,$mon+1,$mday,2630$hour,$min,$sec,$tz);2631return%date;2632}26332634sub parse_tag {2635my$tag_id=shift;2636my%tag;2637my@comment;26382639open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2640$tag{'id'} =$tag_id;2641while(my$line= <$fd>) {2642chomp$line;2643if($line=~m/^object ([0-9a-fA-F]{40})$/) {2644$tag{'object'} =$1;2645}elsif($line=~m/^type (.+)$/) {2646$tag{'type'} =$1;2647}elsif($line=~m/^tag (.+)$/) {2648$tag{'name'} =$1;2649}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2650$tag{'author'} =$1;2651$tag{'author_epoch'} =$2;2652$tag{'author_tz'} =$3;2653if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2654$tag{'author_name'} =$1;2655$tag{'author_email'} =$2;2656}else{2657$tag{'author_name'} =$tag{'author'};2658}2659}elsif($line=~m/--BEGIN/) {2660push@comment,$line;2661last;2662}elsif($lineeq"") {2663last;2664}2665}2666push@comment, <$fd>;2667$tag{'comment'} = \@comment;2668close$fdorreturn;2669if(!defined$tag{'name'}) {2670return2671};2672return%tag2673}26742675sub parse_commit_text {2676my($commit_text,$withparents) =@_;2677my@commit_lines=split'\n',$commit_text;2678my%co;26792680pop@commit_lines;# Remove '\0'26812682if(!@commit_lines) {2683return;2684}26852686my$header=shift@commit_lines;2687if($header!~m/^[0-9a-fA-F]{40}/) {2688return;2689}2690($co{'id'},my@parents) =split' ',$header;2691while(my$line=shift@commit_lines) {2692last if$lineeq"\n";2693if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2694$co{'tree'} =$1;2695}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2696push@parents,$1;2697}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2698$co{'author'} = to_utf8($1);2699$co{'author_epoch'} =$2;2700$co{'author_tz'} =$3;2701if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2702$co{'author_name'} =$1;2703$co{'author_email'} =$2;2704}else{2705$co{'author_name'} =$co{'author'};2706}2707}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2708$co{'committer'} = to_utf8($1);2709$co{'committer_epoch'} =$2;2710$co{'committer_tz'} =$3;2711if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2712$co{'committer_name'} =$1;2713$co{'committer_email'} =$2;2714}else{2715$co{'committer_name'} =$co{'committer'};2716}2717}2718}2719if(!defined$co{'tree'}) {2720return;2721};2722$co{'parents'} = \@parents;2723$co{'parent'} =$parents[0];27242725foreachmy$title(@commit_lines) {2726$title=~s/^ //;2727if($titlene"") {2728$co{'title'} = chop_str($title,80,5);2729# remove leading stuff of merges to make the interesting part visible2730if(length($title) >50) {2731$title=~s/^Automatic //;2732$title=~s/^merge (of|with) /Merge ... /i;2733if(length($title) >50) {2734$title=~s/(http|rsync):\/\///;2735}2736if(length($title) >50) {2737$title=~s/(master|www|rsync)\.//;2738}2739if(length($title) >50) {2740$title=~s/kernel.org:?//;2741}2742if(length($title) >50) {2743$title=~s/\/pub\/scm//;2744}2745}2746$co{'title_short'} = chop_str($title,50,5);2747last;2748}2749}2750if(!defined$co{'title'} ||$co{'title'}eq"") {2751$co{'title'} =$co{'title_short'} ='(no commit message)';2752}2753# remove added spaces2754foreachmy$line(@commit_lines) {2755$line=~s/^ //;2756}2757$co{'comment'} = \@commit_lines;27582759my$age=time-$co{'committer_epoch'};2760$co{'age'} =$age;2761$co{'age_string'} = age_string($age);2762my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2763if($age>60*60*24*7*2) {2764$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2765$co{'age_string_age'} =$co{'age_string'};2766}else{2767$co{'age_string_date'} =$co{'age_string'};2768$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2769}2770return%co;2771}27722773sub parse_commit {2774my($commit_id) =@_;2775my%co;27762777local$/="\0";27782779open my$fd,"-|", git_cmd(),"rev-list",2780"--parents",2781"--header",2782"--max-count=1",2783$commit_id,2784"--",2785or die_error(500,"Open git-rev-list failed");2786%co= parse_commit_text(<$fd>,1);2787close$fd;27882789return%co;2790}27912792sub parse_commits {2793my($commit_id,$maxcount,$skip,$filename,@args) =@_;2794my@cos;27952796$maxcount||=1;2797$skip||=0;27982799local$/="\0";28002801open my$fd,"-|", git_cmd(),"rev-list",2802"--header",2803@args,2804("--max-count=".$maxcount),2805("--skip=".$skip),2806@extra_options,2807$commit_id,2808"--",2809($filename? ($filename) : ())2810or die_error(500,"Open git-rev-list failed");2811while(my$line= <$fd>) {2812my%co= parse_commit_text($line);2813push@cos, \%co;2814}2815close$fd;28162817returnwantarray?@cos: \@cos;2818}28192820# parse line of git-diff-tree "raw" output2821sub parse_difftree_raw_line {2822my$line=shift;2823my%res;28242825# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2826# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2827if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2828$res{'from_mode'} =$1;2829$res{'to_mode'} =$2;2830$res{'from_id'} =$3;2831$res{'to_id'} =$4;2832$res{'status'} =$5;2833$res{'similarity'} =$6;2834if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2835($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2836}else{2837$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2838}2839}2840# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2841# combined diff (for merge commit)2842elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2843$res{'nparents'} =length($1);2844$res{'from_mode'} = [split(' ',$2) ];2845$res{'to_mode'} =pop@{$res{'from_mode'}};2846$res{'from_id'} = [split(' ',$3) ];2847$res{'to_id'} =pop@{$res{'from_id'}};2848$res{'status'} = [split('',$4) ];2849$res{'to_file'} = unquote($5);2850}2851# 'c512b523472485aef4fff9e57b229d9d243c967f'2852elsif($line=~m/^([0-9a-fA-F]{40})$/) {2853$res{'commit'} =$1;2854}28552856returnwantarray?%res: \%res;2857}28582859# wrapper: return parsed line of git-diff-tree "raw" output2860# (the argument might be raw line, or parsed info)2861sub parsed_difftree_line {2862my$line_or_ref=shift;28632864if(ref($line_or_ref)eq"HASH") {2865# pre-parsed (or generated by hand)2866return$line_or_ref;2867}else{2868return parse_difftree_raw_line($line_or_ref);2869}2870}28712872# parse line of git-ls-tree output2873sub parse_ls_tree_line {2874my$line=shift;2875my%opts=@_;2876my%res;28772878if($opts{'-l'}) {2879#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'2880$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;28812882$res{'mode'} =$1;2883$res{'type'} =$2;2884$res{'hash'} =$3;2885$res{'size'} =$4;2886if($opts{'-z'}) {2887$res{'name'} =$5;2888}else{2889$res{'name'} = unquote($5);2890}2891}else{2892#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2893$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;28942895$res{'mode'} =$1;2896$res{'type'} =$2;2897$res{'hash'} =$3;2898if($opts{'-z'}) {2899$res{'name'} =$4;2900}else{2901$res{'name'} = unquote($4);2902}2903}29042905returnwantarray?%res: \%res;2906}29072908# generates _two_ hashes, references to which are passed as 2 and 3 argument2909sub parse_from_to_diffinfo {2910my($diffinfo,$from,$to,@parents) =@_;29112912if($diffinfo->{'nparents'}) {2913# combined diff2914$from->{'file'} = [];2915$from->{'href'} = [];2916 fill_from_file_info($diffinfo,@parents)2917unlessexists$diffinfo->{'from_file'};2918for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2919$from->{'file'}[$i] =2920defined$diffinfo->{'from_file'}[$i] ?2921$diffinfo->{'from_file'}[$i] :2922$diffinfo->{'to_file'};2923if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2924$from->{'href'}[$i] = href(action=>"blob",2925 hash_base=>$parents[$i],2926 hash=>$diffinfo->{'from_id'}[$i],2927 file_name=>$from->{'file'}[$i]);2928}else{2929$from->{'href'}[$i] =undef;2930}2931}2932}else{2933# ordinary (not combined) diff2934$from->{'file'} =$diffinfo->{'from_file'};2935if($diffinfo->{'status'}ne"A") {# not new (added) file2936$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2937 hash=>$diffinfo->{'from_id'},2938 file_name=>$from->{'file'});2939}else{2940delete$from->{'href'};2941}2942}29432944$to->{'file'} =$diffinfo->{'to_file'};2945if(!is_deleted($diffinfo)) {# file exists in result2946$to->{'href'} = href(action=>"blob", hash_base=>$hash,2947 hash=>$diffinfo->{'to_id'},2948 file_name=>$to->{'file'});2949}else{2950delete$to->{'href'};2951}2952}29532954## ......................................................................2955## parse to array of hashes functions29562957sub git_get_heads_list {2958my$limit=shift;2959my@headslist;29602961open my$fd,'-|', git_cmd(),'for-each-ref',2962($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2963'--format=%(objectname) %(refname) %(subject)%00%(committer)',2964'refs/heads'2965orreturn;2966while(my$line= <$fd>) {2967my%ref_item;29682969chomp$line;2970my($refinfo,$committerinfo) =split(/\0/,$line);2971my($hash,$name,$title) =split(' ',$refinfo,3);2972my($committer,$epoch,$tz) =2973($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2974$ref_item{'fullname'} =$name;2975$name=~s!^refs/heads/!!;29762977$ref_item{'name'} =$name;2978$ref_item{'id'} =$hash;2979$ref_item{'title'} =$title||'(no commit message)';2980$ref_item{'epoch'} =$epoch;2981if($epoch) {2982$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2983}else{2984$ref_item{'age'} ="unknown";2985}29862987push@headslist, \%ref_item;2988}2989close$fd;29902991returnwantarray?@headslist: \@headslist;2992}29932994sub git_get_tags_list {2995my$limit=shift;2996my@tagslist;29972998open my$fd,'-|', git_cmd(),'for-each-ref',2999($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3000'--format=%(objectname) %(objecttype) %(refname) '.3001'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3002'refs/tags'3003orreturn;3004while(my$line= <$fd>) {3005my%ref_item;30063007chomp$line;3008my($refinfo,$creatorinfo) =split(/\0/,$line);3009my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3010my($creator,$epoch,$tz) =3011($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3012$ref_item{'fullname'} =$name;3013$name=~s!^refs/tags/!!;30143015$ref_item{'type'} =$type;3016$ref_item{'id'} =$id;3017$ref_item{'name'} =$name;3018if($typeeq"tag") {3019$ref_item{'subject'} =$title;3020$ref_item{'reftype'} =$reftype;3021$ref_item{'refid'} =$refid;3022}else{3023$ref_item{'reftype'} =$type;3024$ref_item{'refid'} =$id;3025}30263027if($typeeq"tag"||$typeeq"commit") {3028$ref_item{'epoch'} =$epoch;3029if($epoch) {3030$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3031}else{3032$ref_item{'age'} ="unknown";3033}3034}30353036push@tagslist, \%ref_item;3037}3038close$fd;30393040returnwantarray?@tagslist: \@tagslist;3041}30423043## ----------------------------------------------------------------------3044## filesystem-related functions30453046sub get_file_owner {3047my$path=shift;30483049my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3050my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3051if(!defined$gcos) {3052returnundef;3053}3054my$owner=$gcos;3055$owner=~s/[,;].*$//;3056return to_utf8($owner);3057}30583059# assume that file exists3060sub insert_file {3061my$filename=shift;30623063open my$fd,'<',$filename;3064print map{ to_utf8($_) } <$fd>;3065close$fd;3066}30673068## ......................................................................3069## mimetype related functions30703071sub mimetype_guess_file {3072my$filename=shift;3073my$mimemap=shift;3074-r $mimemaporreturnundef;30753076my%mimemap;3077open(my$mh,'<',$mimemap)orreturnundef;3078while(<$mh>) {3079next ifm/^#/;# skip comments3080my($mimetype,$exts) =split(/\t+/);3081if(defined$exts) {3082my@exts=split(/\s+/,$exts);3083foreachmy$ext(@exts) {3084$mimemap{$ext} =$mimetype;3085}3086}3087}3088close($mh);30893090$filename=~/\.([^.]*)$/;3091return$mimemap{$1};3092}30933094sub mimetype_guess {3095my$filename=shift;3096my$mime;3097$filename=~/\./orreturnundef;30983099if($mimetypes_file) {3100my$file=$mimetypes_file;3101if($file!~m!^/!) {# if it is relative path3102# it is relative to project3103$file="$projectroot/$project/$file";3104}3105$mime= mimetype_guess_file($filename,$file);3106}3107$mime||= mimetype_guess_file($filename,'/etc/mime.types');3108return$mime;3109}31103111sub blob_mimetype {3112my$fd=shift;3113my$filename=shift;31143115if($filename) {3116my$mime= mimetype_guess($filename);3117$mimeandreturn$mime;3118}31193120# just in case3121return$default_blob_plain_mimetypeunless$fd;31223123if(-T $fd) {3124return'text/plain';3125}elsif(!$filename) {3126return'application/octet-stream';3127}elsif($filename=~m/\.png$/i) {3128return'image/png';3129}elsif($filename=~m/\.gif$/i) {3130return'image/gif';3131}elsif($filename=~m/\.jpe?g$/i) {3132return'image/jpeg';3133}else{3134return'application/octet-stream';3135}3136}31373138sub blob_contenttype {3139my($fd,$file_name,$type) =@_;31403141$type||= blob_mimetype($fd,$file_name);3142if($typeeq'text/plain'&&defined$default_text_plain_charset) {3143$type.="; charset=$default_text_plain_charset";3144}31453146return$type;3147}31483149## ======================================================================3150## functions printing HTML: header, footer, error page31513152sub git_header_html {3153my$status=shift||"200 OK";3154my$expires=shift;31553156my$title="$site_name";3157if(defined$project) {3158$title.=" - ". to_utf8($project);3159if(defined$action) {3160$title.="/$action";3161if(defined$file_name) {3162$title.=" - ". esc_path($file_name);3163if($actioneq"tree"&&$file_name!~ m|/$|) {3164$title.="/";3165}3166}3167}3168}3169my$content_type;3170# require explicit support from the UA if we are to send the page as3171# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3172# we have to do this because MSIE sometimes globs '*/*', pretending to3173# support xhtml+xml but choking when it gets what it asked for.3174if(defined$cgi->http('HTTP_ACCEPT') &&3175$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3176$cgi->Accept('application/xhtml+xml') !=0) {3177$content_type='application/xhtml+xml';3178}else{3179$content_type='text/html';3180}3181print$cgi->header(-type=>$content_type, -charset =>'utf-8',3182-status=>$status, -expires =>$expires);3183my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3184print<<EOF;3185<?xml version="1.0" encoding="utf-8"?>3186<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3187<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3188<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3189<!-- git core binaries version$git_version-->3190<head>3191<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3192<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3193<meta name="robots" content="index, nofollow"/>3194<title>$title</title>3195EOF3196# the stylesheet, favicon etc urls won't work correctly with path_info3197# unless we set the appropriate base URL3198if($ENV{'PATH_INFO'}) {3199print"<base href=\"".esc_url($base_url)."\"/>\n";3200}3201# print out each stylesheet that exist, providing backwards capability3202# for those people who defined $stylesheet in a config file3203if(defined$stylesheet) {3204print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3205}else{3206foreachmy$stylesheet(@stylesheets) {3207next unless$stylesheet;3208print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3209}3210}3211if(defined$project) {3212my%href_params= get_feed_info();3213if(!exists$href_params{'-title'}) {3214$href_params{'-title'} ='log';3215}32163217foreachmy$formatqw(RSS Atom){3218my$type=lc($format);3219my%link_attr= (3220'-rel'=>'alternate',3221'-title'=>"$project-$href_params{'-title'} -$formatfeed",3222'-type'=>"application/$type+xml"3223);32243225$href_params{'action'} =$type;3226$link_attr{'-href'} = href(%href_params);3227print"<link ".3228"rel=\"$link_attr{'-rel'}\"".3229"title=\"$link_attr{'-title'}\"".3230"href=\"$link_attr{'-href'}\"".3231"type=\"$link_attr{'-type'}\"".3232"/>\n";32333234$href_params{'extra_options'} ='--no-merges';3235$link_attr{'-href'} = href(%href_params);3236$link_attr{'-title'} .=' (no merges)';3237print"<link ".3238"rel=\"$link_attr{'-rel'}\"".3239"title=\"$link_attr{'-title'}\"".3240"href=\"$link_attr{'-href'}\"".3241"type=\"$link_attr{'-type'}\"".3242"/>\n";3243}32443245}else{3246printf('<link rel="alternate" title="%sprojects list" '.3247'href="%s" type="text/plain; charset=utf-8" />'."\n",3248$site_name, href(project=>undef, action=>"project_index"));3249printf('<link rel="alternate" title="%sprojects feeds" '.3250'href="%s" type="text/x-opml" />'."\n",3251$site_name, href(project=>undef, action=>"opml"));3252}3253if(defined$favicon) {3254printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3255}32563257print"</head>\n".3258"<body>\n";32593260if(defined$site_header&& -f $site_header) {3261 insert_file($site_header);3262}32633264print"<div class=\"page_header\">\n".3265$cgi->a({-href => esc_url($logo_url),3266-title =>$logo_label},3267qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3268print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3269if(defined$project) {3270print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3271if(defined$action) {3272print" /$action";3273}3274print"\n";3275}3276print"</div>\n";32773278my$have_search= gitweb_check_feature('search');3279if(defined$project&&$have_search) {3280if(!defined$searchtext) {3281$searchtext="";3282}3283my$search_hash;3284if(defined$hash_base) {3285$search_hash=$hash_base;3286}elsif(defined$hash) {3287$search_hash=$hash;3288}else{3289$search_hash="HEAD";3290}3291my$action=$my_uri;3292my$use_pathinfo= gitweb_check_feature('pathinfo');3293if($use_pathinfo) {3294$action.="/".esc_url($project);3295}3296print$cgi->startform(-method=>"get", -action =>$action) .3297"<div class=\"search\">\n".3298(!$use_pathinfo&&3299$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3300$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3301$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3302$cgi->popup_menu(-name =>'st', -default=>'commit',3303-values=> ['commit','grep','author','committer','pickaxe']) .3304$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3305" search:\n",3306$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3307"<span title=\"Extended regular expression\">".3308$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3309-checked =>$search_use_regexp) .3310"</span>".3311"</div>".3312$cgi->end_form() ."\n";3313}3314}33153316sub git_footer_html {3317my$feed_class='rss_logo';33183319print"<div class=\"page_footer\">\n";3320if(defined$project) {3321my$descr= git_get_project_description($project);3322if(defined$descr) {3323print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3324}33253326my%href_params= get_feed_info();3327if(!%href_params) {3328$feed_class.=' generic';3329}3330$href_params{'-title'} ||='log';33313332foreachmy$formatqw(RSS Atom){3333$href_params{'action'} =lc($format);3334print$cgi->a({-href => href(%href_params),3335-title =>"$href_params{'-title'}$formatfeed",3336-class=>$feed_class},$format)."\n";3337}33383339}else{3340print$cgi->a({-href => href(project=>undef, action=>"opml"),3341-class=>$feed_class},"OPML") ." ";3342print$cgi->a({-href => href(project=>undef, action=>"project_index"),3343-class=>$feed_class},"TXT") ."\n";3344}3345print"</div>\n";# class="page_footer"33463347if(defined$t0&& gitweb_check_feature('timed')) {3348print"<div id=\"generating_info\">\n";3349print'This page took '.3350'<span id="generating_time" class="time_span">'.3351 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3352' seconds </span>'.3353' and '.3354'<span id="generating_cmd">'.3355$number_of_git_cmds.3356'</span> git commands '.3357" to generate.\n";3358print"</div>\n";# class="page_footer"3359}33603361if(defined$site_footer&& -f $site_footer) {3362 insert_file($site_footer);3363}33643365print qq!<script type="text/javascript" src="$javascript"></script>\n!;3366if(defined$action&&3367$actioneq'blame_incremental') {3368print qq!<script type="text/javascript">\n!.3369 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3370 qq!"!. href() .qq!");\n!.3371 qq!</script>\n!;3372}elsif(gitweb_check_feature('javascript-actions')) {3373print qq!<script type="text/javascript">\n!.3374 qq!window.onload = fixLinks;\n!.3375 qq!</script>\n!;3376}33773378print"</body>\n".3379"</html>";3380}33813382# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3383# Example: die_error(404, 'Hash not found')3384# By convention, use the following status codes (as defined in RFC 2616):3385# 400: Invalid or missing CGI parameters, or3386# requested object exists but has wrong type.3387# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3388# this server or project.3389# 404: Requested object/revision/project doesn't exist.3390# 500: The server isn't configured properly, or3391# an internal error occurred (e.g. failed assertions caused by bugs), or3392# an unknown error occurred (e.g. the git binary died unexpectedly).3393# 503: The server is currently unavailable (because it is overloaded,3394# or down for maintenance). Generally, this is a temporary state.3395sub die_error {3396my$status=shift||500;3397my$error= esc_html(shift) ||"Internal Server Error";3398my$extra=shift;33993400my%http_responses= (3401400=>'400 Bad Request',3402403=>'403 Forbidden',3403404=>'404 Not Found',3404500=>'500 Internal Server Error',3405503=>'503 Service Unavailable',3406);3407 git_header_html($http_responses{$status});3408print<<EOF;3409<div class="page_body">3410<br /><br />3411$status-$error3412<br />3413EOF3414if(defined$extra) {3415print"<hr />\n".3416"$extra\n";3417}3418print"</div>\n";34193420 git_footer_html();3421exit;3422}34233424## ----------------------------------------------------------------------3425## functions printing or outputting HTML: navigation34263427sub git_print_page_nav {3428my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3429$extra=''if!defined$extra;# pager or formats34303431my@navs=qw(summary shortlog log commit commitdiff tree);3432if($suppress) {3433@navs=grep{$_ne$suppress}@navs;3434}34353436my%arg=map{$_=> {action=>$_} }@navs;3437if(defined$head) {3438for(qw(commit commitdiff)) {3439$arg{$_}{'hash'} =$head;3440}3441if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3442for(qw(shortlog log)) {3443$arg{$_}{'hash'} =$head;3444}3445}3446}34473448$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3449$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;34503451my@actions= gitweb_get_feature('actions');3452my%repl= (3453'%'=>'%',3454'n'=>$project,# project name3455'f'=>$git_dir,# project path within filesystem3456'h'=>$treehead||'',# current hash ('h' parameter)3457'b'=>$treebase||'',# hash base ('hb' parameter)3458);3459while(@actions) {3460my($label,$link,$pos) =splice(@actions,0,3);3461# insert3462@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3463# munch munch3464$link=~s/%([%nfhb])/$repl{$1}/g;3465$arg{$label}{'_href'} =$link;3466}34673468print"<div class=\"page_nav\">\n".3469(join" | ",3470map{$_eq$current?3471$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3472}@navs);3473print"<br/>\n$extra<br/>\n".3474"</div>\n";3475}34763477sub format_paging_nav {3478my($action,$page,$has_next_link) =@_;3479my$paging_nav;348034813482if($page>0) {3483$paging_nav.=3484$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3485" ⋅ ".3486$cgi->a({-href => href(-replay=>1, page=>$page-1),3487-accesskey =>"p", -title =>"Alt-p"},"prev");3488}else{3489$paging_nav.="first ⋅ prev";3490}34913492if($has_next_link) {3493$paging_nav.=" ⋅ ".3494$cgi->a({-href => href(-replay=>1, page=>$page+1),3495-accesskey =>"n", -title =>"Alt-n"},"next");3496}else{3497$paging_nav.=" ⋅ next";3498}34993500return$paging_nav;3501}35023503## ......................................................................3504## functions printing or outputting HTML: div35053506sub git_print_header_div {3507my($action,$title,$hash,$hash_base) =@_;3508my%args= ();35093510$args{'action'} =$action;3511$args{'hash'} =$hashif$hash;3512$args{'hash_base'} =$hash_baseif$hash_base;35133514print"<div class=\"header\">\n".3515$cgi->a({-href => href(%args), -class=>"title"},3516$title?$title:$action) .3517"\n</div>\n";3518}35193520sub print_local_time {3521print format_local_time(@_);3522}35233524sub format_local_time {3525my$localtime='';3526my%date=@_;3527if($date{'hour_local'} <6) {3528$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3529$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3530}else{3531$localtime.=sprintf(" (%02d:%02d%s)",3532$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3533}35343535return$localtime;3536}35373538# Outputs the author name and date in long form3539sub git_print_authorship {3540my$co=shift;3541my%opts=@_;3542my$tag=$opts{-tag} ||'div';3543my$author=$co->{'author_name'};35443545my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3546print"<$tagclass=\"author_date\">".3547 format_search_author($author,"author", esc_html($author)) .3548" [$ad{'rfc2822'}";3549 print_local_time(%ad)if($opts{-localtime});3550print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3551."</$tag>\n";3552}35533554# Outputs table rows containing the full author or committer information,3555# in the format expected for 'commit' view (& similia).3556# Parameters are a commit hash reference, followed by the list of people3557# to output information for. If the list is empty it defalts to both3558# author and committer.3559sub git_print_authorship_rows {3560my$co=shift;3561# too bad we can't use @people = @_ || ('author', 'committer')3562my@people=@_;3563@people= ('author','committer')unless@people;3564foreachmy$who(@people) {3565my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3566print"<tr><td>$who</td><td>".3567 format_search_author($co->{"${who}_name"},$who,3568 esc_html($co->{"${who}_name"})) ." ".3569 format_search_author($co->{"${who}_email"},$who,3570 esc_html("<".$co->{"${who}_email"} .">")) .3571"</td><td rowspan=\"2\">".3572 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3573"</td></tr>\n".3574"<tr>".3575"<td></td><td>$wd{'rfc2822'}";3576 print_local_time(%wd);3577print"</td>".3578"</tr>\n";3579}3580}35813582sub git_print_page_path {3583my$name=shift;3584my$type=shift;3585my$hb=shift;358635873588print"<div class=\"page_path\">";3589print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3590-title =>'tree root'}, to_utf8("[$project]"));3591print" / ";3592if(defined$name) {3593my@dirname=split'/',$name;3594my$basename=pop@dirname;3595my$fullname='';35963597foreachmy$dir(@dirname) {3598$fullname.= ($fullname?'/':'') .$dir;3599print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3600 hash_base=>$hb),3601-title =>$fullname}, esc_path($dir));3602print" / ";3603}3604if(defined$type&&$typeeq'blob') {3605print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3606 hash_base=>$hb),3607-title =>$name}, esc_path($basename));3608}elsif(defined$type&&$typeeq'tree') {3609print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3610 hash_base=>$hb),3611-title =>$name}, esc_path($basename));3612print" / ";3613}else{3614print esc_path($basename);3615}3616}3617print"<br/></div>\n";3618}36193620sub git_print_log {3621my$log=shift;3622my%opts=@_;36233624if($opts{'-remove_title'}) {3625# remove title, i.e. first line of log3626shift@$log;3627}3628# remove leading empty lines3629while(defined$log->[0] &&$log->[0]eq"") {3630shift@$log;3631}36323633# print log3634my$signoff=0;3635my$empty=0;3636foreachmy$line(@$log) {3637if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3638$signoff=1;3639$empty=0;3640if(!$opts{'-remove_signoff'}) {3641print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3642next;3643}else{3644# remove signoff lines3645next;3646}3647}else{3648$signoff=0;3649}36503651# print only one empty line3652# do not print empty line after signoff3653if($lineeq"") {3654next if($empty||$signoff);3655$empty=1;3656}else{3657$empty=0;3658}36593660print format_log_line_html($line) ."<br/>\n";3661}36623663if($opts{'-final_empty_line'}) {3664# end with single empty line3665print"<br/>\n"unless$empty;3666}3667}36683669# return link target (what link points to)3670sub git_get_link_target {3671my$hash=shift;3672my$link_target;36733674# read link3675open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3676orreturn;3677{3678local$/=undef;3679$link_target= <$fd>;3680}3681close$fd3682orreturn;36833684return$link_target;3685}36863687# given link target, and the directory (basedir) the link is in,3688# return target of link relative to top directory (top tree);3689# return undef if it is not possible (including absolute links).3690sub normalize_link_target {3691my($link_target,$basedir) =@_;36923693# absolute symlinks (beginning with '/') cannot be normalized3694return if(substr($link_target,0,1)eq'/');36953696# normalize link target to path from top (root) tree (dir)3697my$path;3698if($basedir) {3699$path=$basedir.'/'.$link_target;3700}else{3701# we are in top (root) tree (dir)3702$path=$link_target;3703}37043705# remove //, /./, and /../3706my@path_parts;3707foreachmy$part(split('/',$path)) {3708# discard '.' and ''3709next if(!$part||$parteq'.');3710# handle '..'3711if($parteq'..') {3712if(@path_parts) {3713pop@path_parts;3714}else{3715# link leads outside repository (outside top dir)3716return;3717}3718}else{3719push@path_parts,$part;3720}3721}3722$path=join('/',@path_parts);37233724return$path;3725}37263727# print tree entry (row of git_tree), but without encompassing <tr> element3728sub git_print_tree_entry {3729my($t,$basedir,$hash_base,$have_blame) =@_;37303731my%base_key= ();3732$base_key{'hash_base'} =$hash_baseifdefined$hash_base;37333734# The format of a table row is: mode list link. Where mode is3735# the mode of the entry, list is the name of the entry, an href,3736# and link is the action links of the entry.37373738print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3739if(exists$t->{'size'}) {3740print"<td class=\"size\">$t->{'size'}</td>\n";3741}3742if($t->{'type'}eq"blob") {3743print"<td class=\"list\">".3744$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3745 file_name=>"$basedir$t->{'name'}",%base_key),3746-class=>"list"}, esc_path($t->{'name'}));3747if(S_ISLNK(oct$t->{'mode'})) {3748my$link_target= git_get_link_target($t->{'hash'});3749if($link_target) {3750my$norm_target= normalize_link_target($link_target,$basedir);3751if(defined$norm_target) {3752print" -> ".3753$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3754 file_name=>$norm_target),3755-title =>$norm_target}, esc_path($link_target));3756}else{3757print" -> ". esc_path($link_target);3758}3759}3760}3761print"</td>\n";3762print"<td class=\"link\">";3763print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3764 file_name=>"$basedir$t->{'name'}",%base_key)},3765"blob");3766if($have_blame) {3767print" | ".3768$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3769 file_name=>"$basedir$t->{'name'}",%base_key)},3770"blame");3771}3772if(defined$hash_base) {3773print" | ".3774$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3775 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3776"history");3777}3778print" | ".3779$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3780 file_name=>"$basedir$t->{'name'}")},3781"raw");3782print"</td>\n";37833784}elsif($t->{'type'}eq"tree") {3785print"<td class=\"list\">";3786print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3787 file_name=>"$basedir$t->{'name'}",3788%base_key)},3789 esc_path($t->{'name'}));3790print"</td>\n";3791print"<td class=\"link\">";3792print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3793 file_name=>"$basedir$t->{'name'}",3794%base_key)},3795"tree");3796if(defined$hash_base) {3797print" | ".3798$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3799 file_name=>"$basedir$t->{'name'}")},3800"history");3801}3802print"</td>\n";3803}else{3804# unknown object: we can only present history for it3805# (this includes 'commit' object, i.e. submodule support)3806print"<td class=\"list\">".3807 esc_path($t->{'name'}) .3808"</td>\n";3809print"<td class=\"link\">";3810if(defined$hash_base) {3811print$cgi->a({-href => href(action=>"history",3812 hash_base=>$hash_base,3813 file_name=>"$basedir$t->{'name'}")},3814"history");3815}3816print"</td>\n";3817}3818}38193820## ......................................................................3821## functions printing large fragments of HTML38223823# get pre-image filenames for merge (combined) diff3824sub fill_from_file_info {3825my($diff,@parents) =@_;38263827$diff->{'from_file'} = [ ];3828$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3829for(my$i=0;$i<$diff->{'nparents'};$i++) {3830if($diff->{'status'}[$i]eq'R'||3831$diff->{'status'}[$i]eq'C') {3832$diff->{'from_file'}[$i] =3833 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3834}3835}38363837return$diff;3838}38393840# is current raw difftree line of file deletion3841sub is_deleted {3842my$diffinfo=shift;38433844return$diffinfo->{'to_id'}eq('0' x 40);3845}38463847# does patch correspond to [previous] difftree raw line3848# $diffinfo - hashref of parsed raw diff format3849# $patchinfo - hashref of parsed patch diff format3850# (the same keys as in $diffinfo)3851sub is_patch_split {3852my($diffinfo,$patchinfo) =@_;38533854returndefined$diffinfo&&defined$patchinfo3855&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3856}385738583859sub git_difftree_body {3860my($difftree,$hash,@parents) =@_;3861my($parent) =$parents[0];3862my$have_blame= gitweb_check_feature('blame');3863print"<div class=\"list_head\">\n";3864if($#{$difftree} >10) {3865print(($#{$difftree} +1) ." files changed:\n");3866}3867print"</div>\n";38683869print"<table class=\"".3870(@parents>1?"combined ":"") .3871"diff_tree\">\n";38723873# header only for combined diff in 'commitdiff' view3874my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3875if($has_header) {3876# table header3877print"<thead><tr>\n".3878"<th></th><th></th>\n";# filename, patchN link3879for(my$i=0;$i<@parents;$i++) {3880my$par=$parents[$i];3881print"<th>".3882$cgi->a({-href => href(action=>"commitdiff",3883 hash=>$hash, hash_parent=>$par),3884-title =>'commitdiff to parent number '.3885($i+1) .': '.substr($par,0,7)},3886$i+1) .3887" </th>\n";3888}3889print"</tr></thead>\n<tbody>\n";3890}38913892my$alternate=1;3893my$patchno=0;3894foreachmy$line(@{$difftree}) {3895my$diff= parsed_difftree_line($line);38963897if($alternate) {3898print"<tr class=\"dark\">\n";3899}else{3900print"<tr class=\"light\">\n";3901}3902$alternate^=1;39033904if(exists$diff->{'nparents'}) {# combined diff39053906 fill_from_file_info($diff,@parents)3907unlessexists$diff->{'from_file'};39083909if(!is_deleted($diff)) {3910# file exists in the result (child) commit3911print"<td>".3912$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3913 file_name=>$diff->{'to_file'},3914 hash_base=>$hash),3915-class=>"list"}, esc_path($diff->{'to_file'})) .3916"</td>\n";3917}else{3918print"<td>".3919 esc_path($diff->{'to_file'}) .3920"</td>\n";3921}39223923if($actioneq'commitdiff') {3924# link to patch3925$patchno++;3926print"<td class=\"link\">".3927$cgi->a({-href =>"#patch$patchno"},"patch") .3928" | ".3929"</td>\n";3930}39313932my$has_history=0;3933my$not_deleted=0;3934for(my$i=0;$i<$diff->{'nparents'};$i++) {3935my$hash_parent=$parents[$i];3936my$from_hash=$diff->{'from_id'}[$i];3937my$from_path=$diff->{'from_file'}[$i];3938my$status=$diff->{'status'}[$i];39393940$has_history||= ($statusne'A');3941$not_deleted||= ($statusne'D');39423943if($statuseq'A') {3944print"<td class=\"link\"align=\"right\"> | </td>\n";3945}elsif($statuseq'D') {3946print"<td class=\"link\">".3947$cgi->a({-href => href(action=>"blob",3948 hash_base=>$hash,3949 hash=>$from_hash,3950 file_name=>$from_path)},3951"blob". ($i+1)) .3952" | </td>\n";3953}else{3954if($diff->{'to_id'}eq$from_hash) {3955print"<td class=\"link nochange\">";3956}else{3957print"<td class=\"link\">";3958}3959print$cgi->a({-href => href(action=>"blobdiff",3960 hash=>$diff->{'to_id'},3961 hash_parent=>$from_hash,3962 hash_base=>$hash,3963 hash_parent_base=>$hash_parent,3964 file_name=>$diff->{'to_file'},3965 file_parent=>$from_path)},3966"diff". ($i+1)) .3967" | </td>\n";3968}3969}39703971print"<td class=\"link\">";3972if($not_deleted) {3973print$cgi->a({-href => href(action=>"blob",3974 hash=>$diff->{'to_id'},3975 file_name=>$diff->{'to_file'},3976 hash_base=>$hash)},3977"blob");3978print" | "if($has_history);3979}3980if($has_history) {3981print$cgi->a({-href => href(action=>"history",3982 file_name=>$diff->{'to_file'},3983 hash_base=>$hash)},3984"history");3985}3986print"</td>\n";39873988print"</tr>\n";3989next;# instead of 'else' clause, to avoid extra indent3990}3991# else ordinary diff39923993my($to_mode_oct,$to_mode_str,$to_file_type);3994my($from_mode_oct,$from_mode_str,$from_file_type);3995if($diff->{'to_mode'}ne('0' x 6)) {3996$to_mode_oct=oct$diff->{'to_mode'};3997if(S_ISREG($to_mode_oct)) {# only for regular file3998$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3999}4000$to_file_type= file_type($diff->{'to_mode'});4001}4002if($diff->{'from_mode'}ne('0' x 6)) {4003$from_mode_oct=oct$diff->{'from_mode'};4004if(S_ISREG($to_mode_oct)) {# only for regular file4005$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4006}4007$from_file_type= file_type($diff->{'from_mode'});4008}40094010if($diff->{'status'}eq"A") {# created4011my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4012$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4013$mode_chng.="]</span>";4014print"<td>";4015print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4016 hash_base=>$hash, file_name=>$diff->{'file'}),4017-class=>"list"}, esc_path($diff->{'file'}));4018print"</td>\n";4019print"<td>$mode_chng</td>\n";4020print"<td class=\"link\">";4021if($actioneq'commitdiff') {4022# link to patch4023$patchno++;4024print$cgi->a({-href =>"#patch$patchno"},"patch");4025print" | ";4026}4027print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4028 hash_base=>$hash, file_name=>$diff->{'file'})},4029"blob");4030print"</td>\n";40314032}elsif($diff->{'status'}eq"D") {# deleted4033my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4034print"<td>";4035print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4036 hash_base=>$parent, file_name=>$diff->{'file'}),4037-class=>"list"}, esc_path($diff->{'file'}));4038print"</td>\n";4039print"<td>$mode_chng</td>\n";4040print"<td class=\"link\">";4041if($actioneq'commitdiff') {4042# link to patch4043$patchno++;4044print$cgi->a({-href =>"#patch$patchno"},"patch");4045print" | ";4046}4047print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4048 hash_base=>$parent, file_name=>$diff->{'file'})},4049"blob") ." | ";4050if($have_blame) {4051print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4052 file_name=>$diff->{'file'})},4053"blame") ." | ";4054}4055print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4056 file_name=>$diff->{'file'})},4057"history");4058print"</td>\n";40594060}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4061my$mode_chnge="";4062if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4063$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4064if($from_file_typene$to_file_type) {4065$mode_chnge.=" from$from_file_typeto$to_file_type";4066}4067if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4068if($from_mode_str&&$to_mode_str) {4069$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4070}elsif($to_mode_str) {4071$mode_chnge.=" mode:$to_mode_str";4072}4073}4074$mode_chnge.="]</span>\n";4075}4076print"<td>";4077print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4078 hash_base=>$hash, file_name=>$diff->{'file'}),4079-class=>"list"}, esc_path($diff->{'file'}));4080print"</td>\n";4081print"<td>$mode_chnge</td>\n";4082print"<td class=\"link\">";4083if($actioneq'commitdiff') {4084# link to patch4085$patchno++;4086print$cgi->a({-href =>"#patch$patchno"},"patch") .4087" | ";4088}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4089# "commit" view and modified file (not onlu mode changed)4090print$cgi->a({-href => href(action=>"blobdiff",4091 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4092 hash_base=>$hash, hash_parent_base=>$parent,4093 file_name=>$diff->{'file'})},4094"diff") .4095" | ";4096}4097print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4098 hash_base=>$hash, file_name=>$diff->{'file'})},4099"blob") ." | ";4100if($have_blame) {4101print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4102 file_name=>$diff->{'file'})},4103"blame") ." | ";4104}4105print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4106 file_name=>$diff->{'file'})},4107"history");4108print"</td>\n";41094110}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4111my%status_name= ('R'=>'moved','C'=>'copied');4112my$nstatus=$status_name{$diff->{'status'}};4113my$mode_chng="";4114if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4115# mode also for directories, so we cannot use $to_mode_str4116$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4117}4118print"<td>".4119$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4120 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4121-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4122"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4123$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4124 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4125-class=>"list"}, esc_path($diff->{'from_file'})) .4126" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4127"<td class=\"link\">";4128if($actioneq'commitdiff') {4129# link to patch4130$patchno++;4131print$cgi->a({-href =>"#patch$patchno"},"patch") .4132" | ";4133}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4134# "commit" view and modified file (not only pure rename or copy)4135print$cgi->a({-href => href(action=>"blobdiff",4136 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4137 hash_base=>$hash, hash_parent_base=>$parent,4138 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4139"diff") .4140" | ";4141}4142print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4143 hash_base=>$parent, file_name=>$diff->{'to_file'})},4144"blob") ." | ";4145if($have_blame) {4146print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4147 file_name=>$diff->{'to_file'})},4148"blame") ." | ";4149}4150print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4151 file_name=>$diff->{'to_file'})},4152"history");4153print"</td>\n";41544155}# we should not encounter Unmerged (U) or Unknown (X) status4156print"</tr>\n";4157}4158print"</tbody>"if$has_header;4159print"</table>\n";4160}41614162sub git_patchset_body {4163my($fd,$difftree,$hash,@hash_parents) =@_;4164my($hash_parent) =$hash_parents[0];41654166my$is_combined= (@hash_parents>1);4167my$patch_idx=0;4168my$patch_number=0;4169my$patch_line;4170my$diffinfo;4171my$to_name;4172my(%from,%to);41734174print"<div class=\"patchset\">\n";41754176# skip to first patch4177while($patch_line= <$fd>) {4178chomp$patch_line;41794180last if($patch_line=~m/^diff /);4181}41824183 PATCH:4184while($patch_line) {41854186# parse "git diff" header line4187if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4188# $1 is from_name, which we do not use4189$to_name= unquote($2);4190$to_name=~s!^b/!!;4191}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4192# $1 is 'cc' or 'combined', which we do not use4193$to_name= unquote($2);4194}else{4195$to_name=undef;4196}41974198# check if current patch belong to current raw line4199# and parse raw git-diff line if needed4200if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4201# this is continuation of a split patch4202print"<div class=\"patch cont\">\n";4203}else{4204# advance raw git-diff output if needed4205$patch_idx++ifdefined$diffinfo;42064207# read and prepare patch information4208$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42094210# compact combined diff output can have some patches skipped4211# find which patch (using pathname of result) we are at now;4212if($is_combined) {4213while($to_namene$diffinfo->{'to_file'}) {4214print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4215 format_diff_cc_simplified($diffinfo,@hash_parents) .4216"</div>\n";# class="patch"42174218$patch_idx++;4219$patch_number++;42204221last if$patch_idx>$#$difftree;4222$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4223}4224}42254226# modifies %from, %to hashes4227 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);42284229# this is first patch for raw difftree line with $patch_idx index4230# we index @$difftree array from 0, but number patches from 14231print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4232}42334234# git diff header4235#assert($patch_line =~ m/^diff /) if DEBUG;4236#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4237$patch_number++;4238# print "git diff" header4239print format_git_diff_header_line($patch_line,$diffinfo,4240 \%from, \%to);42414242# print extended diff header4243print"<div class=\"diff extended_header\">\n";4244 EXTENDED_HEADER:4245while($patch_line= <$fd>) {4246chomp$patch_line;42474248last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);42494250print format_extended_diff_header_line($patch_line,$diffinfo,4251 \%from, \%to);4252}4253print"</div>\n";# class="diff extended_header"42544255# from-file/to-file diff header4256if(!$patch_line) {4257print"</div>\n";# class="patch"4258last PATCH;4259}4260next PATCH if($patch_line=~m/^diff /);4261#assert($patch_line =~ m/^---/) if DEBUG;42624263my$last_patch_line=$patch_line;4264$patch_line= <$fd>;4265chomp$patch_line;4266#assert($patch_line =~ m/^\+\+\+/) if DEBUG;42674268print format_diff_from_to_header($last_patch_line,$patch_line,4269$diffinfo, \%from, \%to,4270@hash_parents);42714272# the patch itself4273 LINE:4274while($patch_line= <$fd>) {4275chomp$patch_line;42764277next PATCH if($patch_line=~m/^diff /);42784279print format_diff_line($patch_line, \%from, \%to);4280}42814282}continue{4283print"</div>\n";# class="patch"4284}42854286# for compact combined (--cc) format, with chunk and patch simpliciaction4287# patchset might be empty, but there might be unprocessed raw lines4288for(++$patch_idxif$patch_number>0;4289$patch_idx<@$difftree;4290++$patch_idx) {4291# read and prepare patch information4292$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42934294# generate anchor for "patch" links in difftree / whatchanged part4295print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4296 format_diff_cc_simplified($diffinfo,@hash_parents) .4297"</div>\n";# class="patch"42984299$patch_number++;4300}43014302if($patch_number==0) {4303if(@hash_parents>1) {4304print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4305}else{4306print"<div class=\"diff nodifferences\">No differences found</div>\n";4307}4308}43094310print"</div>\n";# class="patchset"4311}43124313# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43144315# fills project list info (age, description, owner, forks) for each4316# project in the list, removing invalid projects from returned list4317# NOTE: modifies $projlist, but does not remove entries from it4318sub fill_project_list_info {4319my($projlist,$check_forks) =@_;4320my@projects;43214322my$show_ctags= gitweb_check_feature('ctags');4323 PROJECT:4324foreachmy$pr(@$projlist) {4325my(@activity) = git_get_last_activity($pr->{'path'});4326unless(@activity) {4327next PROJECT;4328}4329($pr->{'age'},$pr->{'age_string'}) =@activity;4330if(!defined$pr->{'descr'}) {4331my$descr= git_get_project_description($pr->{'path'}) ||"";4332$descr= to_utf8($descr);4333$pr->{'descr_long'} =$descr;4334$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4335}4336if(!defined$pr->{'owner'}) {4337$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4338}4339if($check_forks) {4340my$pname=$pr->{'path'};4341if(($pname=~s/\.git$//) &&4342($pname!~/\/$/) &&4343(-d "$projectroot/$pname")) {4344$pr->{'forks'} ="-d$projectroot/$pname";4345}else{4346$pr->{'forks'} =0;4347}4348}4349$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4350push@projects,$pr;4351}43524353return@projects;4354}43554356# print 'sort by' <th> element, generating 'sort by $name' replay link4357# if that order is not selected4358sub print_sort_th {4359print format_sort_th(@_);4360}43614362sub format_sort_th {4363my($name,$order,$header) =@_;4364my$sort_th="";4365$header||=ucfirst($name);43664367if($ordereq$name) {4368$sort_th.="<th>$header</th>\n";4369}else{4370$sort_th.="<th>".4371$cgi->a({-href => href(-replay=>1, order=>$name),4372-class=>"header"},$header) .4373"</th>\n";4374}43754376return$sort_th;4377}43784379sub git_project_list_body {4380# actually uses global variable $project4381my($projlist,$order,$from,$to,$extra,$no_header) =@_;43824383my$check_forks= gitweb_check_feature('forks');4384my@projects= fill_project_list_info($projlist,$check_forks);43854386$order||=$default_projects_order;4387$from=0unlessdefined$from;4388$to=$#projectsif(!defined$to||$#projects<$to);43894390my%order_info= (4391 project => { key =>'path', type =>'str'},4392 descr => { key =>'descr_long', type =>'str'},4393 owner => { key =>'owner', type =>'str'},4394 age => { key =>'age', type =>'num'}4395);4396my$oi=$order_info{$order};4397if($oi->{'type'}eq'str') {4398@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4399}else{4400@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4401}44024403my$show_ctags= gitweb_check_feature('ctags');4404if($show_ctags) {4405my%ctags;4406foreachmy$p(@projects) {4407foreachmy$ct(keys%{$p->{'ctags'}}) {4408$ctags{$ct} +=$p->{'ctags'}->{$ct};4409}4410}4411my$cloud= git_populate_project_tagcloud(\%ctags);4412print git_show_project_tagcloud($cloud,64);4413}44144415print"<table class=\"project_list\">\n";4416unless($no_header) {4417print"<tr>\n";4418if($check_forks) {4419print"<th></th>\n";4420}4421 print_sort_th('project',$order,'Project');4422 print_sort_th('descr',$order,'Description');4423 print_sort_th('owner',$order,'Owner');4424 print_sort_th('age',$order,'Last Change');4425print"<th></th>\n".# for links4426"</tr>\n";4427}4428my$alternate=1;4429my$tagfilter=$cgi->param('by_tag');4430for(my$i=$from;$i<=$to;$i++) {4431my$pr=$projects[$i];44324433next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4434next if$searchtextand not$pr->{'path'} =~/$searchtext/4435and not$pr->{'descr_long'} =~/$searchtext/;4436# Weed out forks or non-matching entries of search4437if($check_forks) {4438my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4439$forkbase="^$forkbase"if$forkbase;4440next ifnot$searchtextand not$tagfilterand$show_ctags4441and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4442}44434444if($alternate) {4445print"<tr class=\"dark\">\n";4446}else{4447print"<tr class=\"light\">\n";4448}4449$alternate^=1;4450if($check_forks) {4451print"<td>";4452if($pr->{'forks'}) {4453print"<!--$pr->{'forks'} -->\n";4454print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4455}4456print"</td>\n";4457}4458print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4459-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4460"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4461-class=>"list", -title =>$pr->{'descr_long'}},4462 esc_html($pr->{'descr'})) ."</td>\n".4463"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4464print"<td class=\"". age_class($pr->{'age'}) ."\">".4465(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4466"<td class=\"link\">".4467$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4468$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4469$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4470$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4471($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4472"</td>\n".4473"</tr>\n";4474}4475if(defined$extra) {4476print"<tr>\n";4477if($check_forks) {4478print"<td></td>\n";4479}4480print"<td colspan=\"5\">$extra</td>\n".4481"</tr>\n";4482}4483print"</table>\n";4484}44854486sub git_log_body {4487# uses global variable $project4488my($commitlist,$from,$to,$refs,$extra) =@_;44894490$from=0unlessdefined$from;4491$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);44924493for(my$i=0;$i<=$to;$i++) {4494my%co= %{$commitlist->[$i]};4495next if!%co;4496my$commit=$co{'id'};4497my$ref= format_ref_marker($refs,$commit);4498my%ad= parse_date($co{'author_epoch'});4499 git_print_header_div('commit',4500"<span class=\"age\">$co{'age_string'}</span>".4501 esc_html($co{'title'}) .$ref,4502$commit);4503print"<div class=\"title_text\">\n".4504"<div class=\"log_link\">\n".4505$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4506" | ".4507$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4508" | ".4509$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4510"<br/>\n".4511"</div>\n";4512 git_print_authorship(\%co, -tag =>'span');4513print"<br/>\n</div>\n";45144515print"<div class=\"log_body\">\n";4516 git_print_log($co{'comment'}, -final_empty_line=>1);4517print"</div>\n";4518}4519if($extra) {4520print"<div class=\"page_nav\">\n";4521print"$extra\n";4522print"</div>\n";4523}4524}45254526sub git_shortlog_body {4527# uses global variable $project4528my($commitlist,$from,$to,$refs,$extra) =@_;45294530$from=0unlessdefined$from;4531$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45324533print"<table class=\"shortlog\">\n";4534my$alternate=1;4535for(my$i=$from;$i<=$to;$i++) {4536my%co= %{$commitlist->[$i]};4537my$commit=$co{'id'};4538my$ref= format_ref_marker($refs,$commit);4539if($alternate) {4540print"<tr class=\"dark\">\n";4541}else{4542print"<tr class=\"light\">\n";4543}4544$alternate^=1;4545# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4546print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4547 format_author_html('td', \%co,10) ."<td>";4548print format_subject_html($co{'title'},$co{'title_short'},4549 href(action=>"commit", hash=>$commit),$ref);4550print"</td>\n".4551"<td class=\"link\">".4552$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4553$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4554$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4555my$snapshot_links= format_snapshot_links($commit);4556if(defined$snapshot_links) {4557print" | ".$snapshot_links;4558}4559print"</td>\n".4560"</tr>\n";4561}4562if(defined$extra) {4563print"<tr>\n".4564"<td colspan=\"4\">$extra</td>\n".4565"</tr>\n";4566}4567print"</table>\n";4568}45694570sub git_history_body {4571# Warning: assumes constant type (blob or tree) during history4572my($commitlist,$from,$to,$refs,$extra,4573$file_name,$file_hash,$ftype) =@_;45744575$from=0unlessdefined$from;4576$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});45774578print"<table class=\"history\">\n";4579my$alternate=1;4580for(my$i=$from;$i<=$to;$i++) {4581my%co= %{$commitlist->[$i]};4582if(!%co) {4583next;4584}4585my$commit=$co{'id'};45864587my$ref= format_ref_marker($refs,$commit);45884589if($alternate) {4590print"<tr class=\"dark\">\n";4591}else{4592print"<tr class=\"light\">\n";4593}4594$alternate^=1;4595print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4596# shortlog: format_author_html('td', \%co, 10)4597 format_author_html('td', \%co,15,3) ."<td>";4598# originally git_history used chop_str($co{'title'}, 50)4599print format_subject_html($co{'title'},$co{'title_short'},4600 href(action=>"commit", hash=>$commit),$ref);4601print"</td>\n".4602"<td class=\"link\">".4603$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4604$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");46054606if($ftypeeq'blob') {4607my$blob_current=$file_hash;4608my$blob_parent= git_get_hash_by_path($commit,$file_name);4609if(defined$blob_current&&defined$blob_parent&&4610$blob_currentne$blob_parent) {4611print" | ".4612$cgi->a({-href => href(action=>"blobdiff",4613 hash=>$blob_current, hash_parent=>$blob_parent,4614 hash_base=>$hash_base, hash_parent_base=>$commit,4615 file_name=>$file_name)},4616"diff to current");4617}4618}4619print"</td>\n".4620"</tr>\n";4621}4622if(defined$extra) {4623print"<tr>\n".4624"<td colspan=\"4\">$extra</td>\n".4625"</tr>\n";4626}4627print"</table>\n";4628}46294630sub git_tags_body {4631# uses global variable $project4632my($taglist,$from,$to,$extra) =@_;4633$from=0unlessdefined$from;4634$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);46354636print"<table class=\"tags\">\n";4637my$alternate=1;4638for(my$i=$from;$i<=$to;$i++) {4639my$entry=$taglist->[$i];4640my%tag=%$entry;4641my$comment=$tag{'subject'};4642my$comment_short;4643if(defined$comment) {4644$comment_short= chop_str($comment,30,5);4645}4646if($alternate) {4647print"<tr class=\"dark\">\n";4648}else{4649print"<tr class=\"light\">\n";4650}4651$alternate^=1;4652if(defined$tag{'age'}) {4653print"<td><i>$tag{'age'}</i></td>\n";4654}else{4655print"<td></td>\n";4656}4657print"<td>".4658$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4659-class=>"list name"}, esc_html($tag{'name'})) .4660"</td>\n".4661"<td>";4662if(defined$comment) {4663print format_subject_html($comment,$comment_short,4664 href(action=>"tag", hash=>$tag{'id'}));4665}4666print"</td>\n".4667"<td class=\"selflink\">";4668if($tag{'type'}eq"tag") {4669print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4670}else{4671print" ";4672}4673print"</td>\n".4674"<td class=\"link\">"." | ".4675$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4676if($tag{'reftype'}eq"commit") {4677print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4678" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4679}elsif($tag{'reftype'}eq"blob") {4680print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4681}4682print"</td>\n".4683"</tr>";4684}4685if(defined$extra) {4686print"<tr>\n".4687"<td colspan=\"5\">$extra</td>\n".4688"</tr>\n";4689}4690print"</table>\n";4691}46924693sub git_heads_body {4694# uses global variable $project4695my($headlist,$head,$from,$to,$extra) =@_;4696$from=0unlessdefined$from;4697$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);46984699print"<table class=\"heads\">\n";4700my$alternate=1;4701for(my$i=$from;$i<=$to;$i++) {4702my$entry=$headlist->[$i];4703my%ref=%$entry;4704my$curr=$ref{'id'}eq$head;4705if($alternate) {4706print"<tr class=\"dark\">\n";4707}else{4708print"<tr class=\"light\">\n";4709}4710$alternate^=1;4711print"<td><i>$ref{'age'}</i></td>\n".4712($curr?"<td class=\"current_head\">":"<td>") .4713$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4714-class=>"list name"},esc_html($ref{'name'})) .4715"</td>\n".4716"<td class=\"link\">".4717$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4718$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4719$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4720"</td>\n".4721"</tr>";4722}4723if(defined$extra) {4724print"<tr>\n".4725"<td colspan=\"3\">$extra</td>\n".4726"</tr>\n";4727}4728print"</table>\n";4729}47304731sub git_search_grep_body {4732my($commitlist,$from,$to,$extra) =@_;4733$from=0unlessdefined$from;4734$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47354736print"<table class=\"commit_search\">\n";4737my$alternate=1;4738for(my$i=$from;$i<=$to;$i++) {4739my%co= %{$commitlist->[$i]};4740if(!%co) {4741next;4742}4743my$commit=$co{'id'};4744if($alternate) {4745print"<tr class=\"dark\">\n";4746}else{4747print"<tr class=\"light\">\n";4748}4749$alternate^=1;4750print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4751 format_author_html('td', \%co,15,5) .4752"<td>".4753$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4754-class=>"list subject"},4755 chop_and_escape_str($co{'title'},50) ."<br/>");4756my$comment=$co{'comment'};4757foreachmy$line(@$comment) {4758if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4759my($lead,$match,$trail) = ($1,$2,$3);4760$match= chop_str($match,70,5,'center');4761my$contextlen=int((80-length($match))/2);4762$contextlen=30if($contextlen>30);4763$lead= chop_str($lead,$contextlen,10,'left');4764$trail= chop_str($trail,$contextlen,10,'right');47654766$lead= esc_html($lead);4767$match= esc_html($match);4768$trail= esc_html($trail);47694770print"$lead<span class=\"match\">$match</span>$trail<br />";4771}4772}4773print"</td>\n".4774"<td class=\"link\">".4775$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4776" | ".4777$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4778" | ".4779$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4780print"</td>\n".4781"</tr>\n";4782}4783if(defined$extra) {4784print"<tr>\n".4785"<td colspan=\"3\">$extra</td>\n".4786"</tr>\n";4787}4788print"</table>\n";4789}47904791## ======================================================================4792## ======================================================================4793## actions47944795sub git_project_list {4796my$order=$input_params{'order'};4797if(defined$order&&$order!~m/none|project|descr|owner|age/) {4798 die_error(400,"Unknown order parameter");4799}48004801my@list= git_get_projects_list();4802if(!@list) {4803 die_error(404,"No projects found");4804}48054806 git_header_html();4807if(defined$home_text&& -f $home_text) {4808print"<div class=\"index_include\">\n";4809 insert_file($home_text);4810print"</div>\n";4811}4812print$cgi->startform(-method=>"get") .4813"<p class=\"projsearch\">Search:\n".4814$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4815"</p>".4816$cgi->end_form() ."\n";4817 git_project_list_body(\@list,$order);4818 git_footer_html();4819}48204821sub git_forks {4822my$order=$input_params{'order'};4823if(defined$order&&$order!~m/none|project|descr|owner|age/) {4824 die_error(400,"Unknown order parameter");4825}48264827my@list= git_get_projects_list($project);4828if(!@list) {4829 die_error(404,"No forks found");4830}48314832 git_header_html();4833 git_print_page_nav('','');4834 git_print_header_div('summary',"$projectforks");4835 git_project_list_body(\@list,$order);4836 git_footer_html();4837}48384839sub git_project_index {4840my@projects= git_get_projects_list($project);48414842print$cgi->header(4843-type =>'text/plain',4844-charset =>'utf-8',4845-content_disposition =>'inline; filename="index.aux"');48464847foreachmy$pr(@projects) {4848if(!exists$pr->{'owner'}) {4849$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4850}48514852my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4853# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4854$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4855$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4856$path=~s/ /\+/g;4857$owner=~s/ /\+/g;48584859print"$path$owner\n";4860}4861}48624863sub git_summary {4864my$descr= git_get_project_description($project) ||"none";4865my%co= parse_commit("HEAD");4866my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4867my$head=$co{'id'};48684869my$owner= git_get_project_owner($project);48704871my$refs= git_get_references();4872# These get_*_list functions return one more to allow us to see if4873# there are more ...4874my@taglist= git_get_tags_list(16);4875my@headlist= git_get_heads_list(16);4876my@forklist;4877my$check_forks= gitweb_check_feature('forks');48784879if($check_forks) {4880@forklist= git_get_projects_list($project);4881}48824883 git_header_html();4884 git_print_page_nav('summary','',$head);48854886print"<div class=\"title\"> </div>\n";4887print"<table class=\"projects_list\">\n".4888"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4889"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4890if(defined$cd{'rfc2822'}) {4891print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4892}48934894# use per project git URL list in $projectroot/$project/cloneurl4895# or make project git URL from git base URL and project name4896my$url_tag="URL";4897my@url_list= git_get_project_url_list($project);4898@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4899foreachmy$git_url(@url_list) {4900next unless$git_url;4901print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4902$url_tag="";4903}49044905# Tag cloud4906my$show_ctags= gitweb_check_feature('ctags');4907if($show_ctags) {4908my$ctags= git_get_project_ctags($project);4909my$cloud= git_populate_project_tagcloud($ctags);4910print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4911print"</td>\n<td>"unless%$ctags;4912print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4913print"</td>\n<td>"if%$ctags;4914print git_show_project_tagcloud($cloud,48);4915print"</td></tr>";4916}49174918print"</table>\n";49194920# If XSS prevention is on, we don't include README.html.4921# TODO: Allow a readme in some safe format.4922if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4923print"<div class=\"title\">readme</div>\n".4924"<div class=\"readme\">\n";4925 insert_file("$projectroot/$project/README.html");4926print"\n</div>\n";# class="readme"4927}49284929# we need to request one more than 16 (0..15) to check if4930# those 16 are all4931my@commitlist=$head? parse_commits($head,17) : ();4932if(@commitlist) {4933 git_print_header_div('shortlog');4934 git_shortlog_body(\@commitlist,0,15,$refs,4935$#commitlist<=15?undef:4936$cgi->a({-href => href(action=>"shortlog")},"..."));4937}49384939if(@taglist) {4940 git_print_header_div('tags');4941 git_tags_body(\@taglist,0,15,4942$#taglist<=15?undef:4943$cgi->a({-href => href(action=>"tags")},"..."));4944}49454946if(@headlist) {4947 git_print_header_div('heads');4948 git_heads_body(\@headlist,$head,0,15,4949$#headlist<=15?undef:4950$cgi->a({-href => href(action=>"heads")},"..."));4951}49524953if(@forklist) {4954 git_print_header_div('forks');4955 git_project_list_body(\@forklist,'age',0,15,4956$#forklist<=15?undef:4957$cgi->a({-href => href(action=>"forks")},"..."),4958'no_header');4959}49604961 git_footer_html();4962}49634964sub git_tag {4965my$head= git_get_head_hash($project);4966 git_header_html();4967 git_print_page_nav('','',$head,undef,$head);4968my%tag= parse_tag($hash);49694970if(!%tag) {4971 die_error(404,"Unknown tag object");4972}49734974 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4975print"<div class=\"title_text\">\n".4976"<table class=\"object_header\">\n".4977"<tr>\n".4978"<td>object</td>\n".4979"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4980$tag{'object'}) ."</td>\n".4981"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4982$tag{'type'}) ."</td>\n".4983"</tr>\n";4984if(defined($tag{'author'})) {4985 git_print_authorship_rows(\%tag,'author');4986}4987print"</table>\n\n".4988"</div>\n";4989print"<div class=\"page_body\">";4990my$comment=$tag{'comment'};4991foreachmy$line(@$comment) {4992chomp$line;4993print esc_html($line, -nbsp=>1) ."<br/>\n";4994}4995print"</div>\n";4996 git_footer_html();4997}49984999sub git_blame_common {5000my$format=shift||'porcelain';5001if($formateq'porcelain'&&$cgi->param('js')) {5002$format='incremental';5003$action='blame_incremental';# for page title etc5004}50055006# permissions5007 gitweb_check_feature('blame')5008or die_error(403,"Blame view not allowed");50095010# error checking5011 die_error(400,"No file name given")unless$file_name;5012$hash_base||= git_get_head_hash($project);5013 die_error(404,"Couldn't find base commit")unless$hash_base;5014my%co= parse_commit($hash_base)5015or die_error(404,"Commit not found");5016my$ftype="blob";5017if(!defined$hash) {5018$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5019or die_error(404,"Error looking up file");5020}else{5021$ftype= git_get_type($hash);5022if($ftype!~"blob") {5023 die_error(400,"Object is not a blob");5024}5025}50265027my$fd;5028if($formateq'incremental') {5029# get file contents (as base)5030open$fd,"-|", git_cmd(),'cat-file','blob',$hash5031or die_error(500,"Open git-cat-file failed");5032}elsif($formateq'data') {5033# run git-blame --incremental5034open$fd,"-|", git_cmd(),"blame","--incremental",5035$hash_base,"--",$file_name5036or die_error(500,"Open git-blame --incremental failed");5037}else{5038# run git-blame --porcelain5039open$fd,"-|", git_cmd(),"blame",'-p',5040$hash_base,'--',$file_name5041or die_error(500,"Open git-blame --porcelain failed");5042}50435044# incremental blame data returns early5045if($formateq'data') {5046print$cgi->header(5047-type=>"text/plain", -charset =>"utf-8",5048-status=>"200 OK");5049local$| =1;# output autoflush5050printwhile<$fd>;5051close$fd5052or print"ERROR$!\n";50535054print'END';5055if(defined$t0&& gitweb_check_feature('timed')) {5056print' '.5057 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5058' '.$number_of_git_cmds;5059}5060print"\n";50615062return;5063}50645065# page header5066 git_header_html();5067my$formats_nav=5068$cgi->a({-href => href(action=>"blob", -replay=>1)},5069"blob") .5070" | ";5071if($formateq'incremental') {5072$formats_nav.=5073$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5074"blame") ." (non-incremental)";5075}else{5076$formats_nav.=5077$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5078"blame") ." (incremental)";5079}5080$formats_nav.=5081" | ".5082$cgi->a({-href => href(action=>"history", -replay=>1)},5083"history") .5084" | ".5085$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5086"HEAD");5087 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5088 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5089 git_print_page_path($file_name,$ftype,$hash_base);50905091# page body5092if($formateq'incremental') {5093print"<noscript>\n<div class=\"error\"><center><b>\n".5094"This page requires JavaScript to run.\nUse ".5095$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5096'this page').5097" instead.\n".5098"</b></center></div>\n</noscript>\n";50995100print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5101}51025103print qq!<div class="page_body">\n!;5104print qq!<div id="progress_info">.../ ...</div>\n!5105if($formateq'incremental');5106print qq!<table id="blame_table"class="blame" width="100%">\n!.5107#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5108 qq!<thead>\n!.5109 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5110 qq!</thead>\n!.5111 qq!<tbody>\n!;51125113my@rev_color=qw(light dark);5114my$num_colors=scalar(@rev_color);5115my$current_color=0;51165117if($formateq'incremental') {5118my$color_class=$rev_color[$current_color];51195120#contents of a file5121my$linenr=0;5122 LINE:5123while(my$line= <$fd>) {5124chomp$line;5125$linenr++;51265127print qq!<tr id="l$linenr"class="$color_class">!.5128 qq!<td class="sha1"><a href=""> </a></td>!.5129 qq!<td class="linenr">!.5130 qq!<a class="linenr" href="">$linenr</a></td>!;5131print qq!<td class="pre">! . esc_html($line) ."</td>\n";5132print qq!</tr>\n!;5133}51345135}else{# porcelain, i.e. ordinary blame5136my%metainfo= ();# saves information about commits51375138# blame data5139 LINE:5140while(my$line= <$fd>) {5141chomp$line;5142# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5143# no <lines in group> for subsequent lines in group of lines5144my($full_rev,$orig_lineno,$lineno,$group_size) =5145($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5146if(!exists$metainfo{$full_rev}) {5147$metainfo{$full_rev} = {'nprevious'=>0};5148}5149my$meta=$metainfo{$full_rev};5150my$data;5151while($data= <$fd>) {5152chomp$data;5153last if($data=~s/^\t//);# contents of line5154if($data=~/^(\S+)(?: (.*))?$/) {5155$meta->{$1} =$2unlessexists$meta->{$1};5156}5157if($data=~/^previous /) {5158$meta->{'nprevious'}++;5159}5160}5161my$short_rev=substr($full_rev,0,8);5162my$author=$meta->{'author'};5163my%date=5164 parse_date($meta->{'author-time'},$meta->{'author-tz'});5165my$date=$date{'iso-tz'};5166if($group_size) {5167$current_color= ($current_color+1) %$num_colors;5168}5169my$tr_class=$rev_color[$current_color];5170$tr_class.=' boundary'if(exists$meta->{'boundary'});5171$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5172$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5173print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5174if($group_size) {5175print"<td class=\"sha1\"";5176print" title=\"". esc_html($author) .",$date\"";5177print" rowspan=\"$group_size\""if($group_size>1);5178print">";5179print$cgi->a({-href => href(action=>"commit",5180 hash=>$full_rev,5181 file_name=>$file_name)},5182 esc_html($short_rev));5183if($group_size>=2) {5184my@author_initials= ($author=~/\b([[:upper:]])\B/g);5185if(@author_initials) {5186print"<br />".5187 esc_html(join('',@author_initials));5188# or join('.', ...)5189}5190}5191print"</td>\n";5192}5193# 'previous' <sha1 of parent commit> <filename at commit>5194if(exists$meta->{'previous'} &&5195$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5196$meta->{'parent'} =$1;5197$meta->{'file_parent'} = unquote($2);5198}5199my$linenr_commit=5200exists($meta->{'parent'}) ?5201$meta->{'parent'} :$full_rev;5202my$linenr_filename=5203exists($meta->{'file_parent'}) ?5204$meta->{'file_parent'} : unquote($meta->{'filename'});5205my$blamed= href(action =>'blame',5206 file_name =>$linenr_filename,5207 hash_base =>$linenr_commit);5208print"<td class=\"linenr\">";5209print$cgi->a({ -href =>"$blamed#l$orig_lineno",5210-class=>"linenr"},5211 esc_html($lineno));5212print"</td>";5213print"<td class=\"pre\">". esc_html($data) ."</td>\n";5214print"</tr>\n";5215}# end while52165217}52185219# footer5220print"</tbody>\n".5221"</table>\n";# class="blame"5222print"</div>\n";# class="blame_body"5223close$fd5224or print"Reading blob failed\n";52255226 git_footer_html();5227}52285229sub git_blame {5230 git_blame_common();5231}52325233sub git_blame_incremental {5234 git_blame_common('incremental');5235}52365237sub git_blame_data {5238 git_blame_common('data');5239}52405241sub git_tags {5242my$head= git_get_head_hash($project);5243 git_header_html();5244 git_print_page_nav('','',$head,undef,$head);5245 git_print_header_div('summary',$project);52465247my@tagslist= git_get_tags_list();5248if(@tagslist) {5249 git_tags_body(\@tagslist);5250}5251 git_footer_html();5252}52535254sub git_heads {5255my$head= git_get_head_hash($project);5256 git_header_html();5257 git_print_page_nav('','',$head,undef,$head);5258 git_print_header_div('summary',$project);52595260my@headslist= git_get_heads_list();5261if(@headslist) {5262 git_heads_body(\@headslist,$head);5263}5264 git_footer_html();5265}52665267sub git_blob_plain {5268my$type=shift;5269my$expires;52705271if(!defined$hash) {5272if(defined$file_name) {5273my$base=$hash_base|| git_get_head_hash($project);5274$hash= git_get_hash_by_path($base,$file_name,"blob")5275or die_error(404,"Cannot find file");5276}else{5277 die_error(400,"No file name defined");5278}5279}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5280# blobs defined by non-textual hash id's can be cached5281$expires="+1d";5282}52835284open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5285or die_error(500,"Open git-cat-file blob '$hash' failed");52865287# content-type (can include charset)5288$type= blob_contenttype($fd,$file_name,$type);52895290# "save as" filename, even when no $file_name is given5291my$save_as="$hash";5292if(defined$file_name) {5293$save_as=$file_name;5294}elsif($type=~m/^text\//) {5295$save_as.='.txt';5296}52975298# With XSS prevention on, blobs of all types except a few known safe5299# ones are served with "Content-Disposition: attachment" to make sure5300# they don't run in our security domain. For certain image types,5301# blob view writes an <img> tag referring to blob_plain view, and we5302# want to be sure not to break that by serving the image as an5303# attachment (though Firefox 3 doesn't seem to care).5304my$sandbox=$prevent_xss&&5305$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;53065307print$cgi->header(5308-type =>$type,5309-expires =>$expires,5310-content_disposition =>5311($sandbox?'attachment':'inline')5312.'; filename="'.$save_as.'"');5313local$/=undef;5314binmode STDOUT,':raw';5315print<$fd>;5316binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5317close$fd;5318}53195320sub git_blob {5321my$expires;53225323if(!defined$hash) {5324if(defined$file_name) {5325my$base=$hash_base|| git_get_head_hash($project);5326$hash= git_get_hash_by_path($base,$file_name,"blob")5327or die_error(404,"Cannot find file");5328}else{5329 die_error(400,"No file name defined");5330}5331}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5332# blobs defined by non-textual hash id's can be cached5333$expires="+1d";5334}53355336my$have_blame= gitweb_check_feature('blame');5337open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5338or die_error(500,"Couldn't cat$file_name,$hash");5339my$mimetype= blob_mimetype($fd,$file_name);5340if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5341close$fd;5342return git_blob_plain($mimetype);5343}5344# we can have blame only for text/* mimetype5345$have_blame&&= ($mimetype=~m!^text/!);53465347 git_header_html(undef,$expires);5348my$formats_nav='';5349if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5350if(defined$file_name) {5351if($have_blame) {5352$formats_nav.=5353$cgi->a({-href => href(action=>"blame", -replay=>1)},5354"blame") .5355" | ";5356}5357$formats_nav.=5358$cgi->a({-href => href(action=>"history", -replay=>1)},5359"history") .5360" | ".5361$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5362"raw") .5363" | ".5364$cgi->a({-href => href(action=>"blob",5365 hash_base=>"HEAD", file_name=>$file_name)},5366"HEAD");5367}else{5368$formats_nav.=5369$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5370"raw");5371}5372 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5373 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5374}else{5375print"<div class=\"page_nav\">\n".5376"<br/><br/></div>\n".5377"<div class=\"title\">$hash</div>\n";5378}5379 git_print_page_path($file_name,"blob",$hash_base);5380print"<div class=\"page_body\">\n";5381if($mimetype=~m!^image/!) {5382print qq!<img type="$mimetype"!;5383if($file_name) {5384print qq! alt="$file_name" title="$file_name"!;5385}5386print qq! src="! .5387 href(action=>"blob_plain", hash=>$hash,5388 hash_base=>$hash_base, file_name=>$file_name) .5389 qq!"/>\n!;5390}else{5391my$nr;5392while(my$line= <$fd>) {5393chomp$line;5394$nr++;5395$line= untabify($line);5396printf"<div class=\"pre\"><a id=\"l%i\"href=\"". href(-replay =>1)5397."#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5398$nr,$nr,$nr, esc_html($line, -nbsp=>1);5399}5400}5401close$fd5402or print"Reading blob failed.\n";5403print"</div>";5404 git_footer_html();5405}54065407sub git_tree {5408if(!defined$hash_base) {5409$hash_base="HEAD";5410}5411if(!defined$hash) {5412if(defined$file_name) {5413$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5414}else{5415$hash=$hash_base;5416}5417}5418 die_error(404,"No such tree")unlessdefined($hash);54195420my$show_sizes= gitweb_check_feature('show-sizes');5421my$have_blame= gitweb_check_feature('blame');54225423my@entries= ();5424{5425local$/="\0";5426open my$fd,"-|", git_cmd(),"ls-tree",'-z',5427($show_sizes?'-l': ()),@extra_options,$hash5428or die_error(500,"Open git-ls-tree failed");5429@entries=map{chomp;$_} <$fd>;5430close$fd5431or die_error(404,"Reading tree failed");5432}54335434my$refs= git_get_references();5435my$ref= format_ref_marker($refs,$hash_base);5436 git_header_html();5437my$basedir='';5438if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5439my@views_nav= ();5440if(defined$file_name) {5441push@views_nav,5442$cgi->a({-href => href(action=>"history", -replay=>1)},5443"history"),5444$cgi->a({-href => href(action=>"tree",5445 hash_base=>"HEAD", file_name=>$file_name)},5446"HEAD"),5447}5448my$snapshot_links= format_snapshot_links($hash);5449if(defined$snapshot_links) {5450# FIXME: Should be available when we have no hash base as well.5451push@views_nav,$snapshot_links;5452}5453 git_print_page_nav('tree','',$hash_base,undef,undef,5454join(' | ',@views_nav));5455 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5456}else{5457undef$hash_base;5458print"<div class=\"page_nav\">\n";5459print"<br/><br/></div>\n";5460print"<div class=\"title\">$hash</div>\n";5461}5462if(defined$file_name) {5463$basedir=$file_name;5464if($basedirne''&&substr($basedir, -1)ne'/') {5465$basedir.='/';5466}5467 git_print_page_path($file_name,'tree',$hash_base);5468}5469print"<div class=\"page_body\">\n";5470print"<table class=\"tree\">\n";5471my$alternate=1;5472# '..' (top directory) link if possible5473if(defined$hash_base&&5474defined$file_name&&$file_name=~m![^/]+$!) {5475if($alternate) {5476print"<tr class=\"dark\">\n";5477}else{5478print"<tr class=\"light\">\n";5479}5480$alternate^=1;54815482my$up=$file_name;5483$up=~s!/?[^/]+$!!;5484undef$upunless$up;5485# based on git_print_tree_entry5486print'<td class="mode">'. mode_str('040000') ."</td>\n";5487print'<td class="size"> </td>'."\n"if$show_sizes;5488print'<td class="list">';5489print$cgi->a({-href => href(action=>"tree",5490 hash_base=>$hash_base,5491 file_name=>$up)},5492"..");5493print"</td>\n";5494print"<td class=\"link\"></td>\n";54955496print"</tr>\n";5497}5498foreachmy$line(@entries) {5499my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);55005501if($alternate) {5502print"<tr class=\"dark\">\n";5503}else{5504print"<tr class=\"light\">\n";5505}5506$alternate^=1;55075508 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);55095510print"</tr>\n";5511}5512print"</table>\n".5513"</div>";5514 git_footer_html();5515}55165517sub snapshot_name {5518my($project,$hash) =@_;55195520# path/to/project.git -> project5521# path/to/project/.git -> project5522my$name= to_utf8($project);5523$name=~ s,([^/])/*\.git$,$1,;5524$name= basename($name);5525# sanitize name5526$name=~s/[[:cntrl:]]/?/g;55275528my$ver=$hash;5529if($hash=~/^[0-9a-fA-F]+$/) {5530# shorten SHA-1 hash5531my$full_hash= git_get_full_hash($project,$hash);5532if($full_hash=~/^$hash/&&length($hash) >7) {5533$ver= git_get_short_hash($project,$hash);5534}5535}elsif($hash=~m!^refs/tags/(.*)$!) {5536# tags don't need shortened SHA-1 hash5537$ver=$1;5538}else{5539# branches and other need shortened SHA-1 hash5540if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5541$ver=$1;5542}5543$ver.='-'. git_get_short_hash($project,$hash);5544}5545# in case of hierarchical branch names5546$ver=~s!/!.!g;55475548# name = project-version_string5549$name="$name-$ver";55505551returnwantarray? ($name,$name) :$name;5552}55535554sub git_snapshot {5555my$format=$input_params{'snapshot_format'};5556if(!@snapshot_fmts) {5557 die_error(403,"Snapshots not allowed");5558}5559# default to first supported snapshot format5560$format||=$snapshot_fmts[0];5561if($format!~m/^[a-z0-9]+$/) {5562 die_error(400,"Invalid snapshot format parameter");5563}elsif(!exists($known_snapshot_formats{$format})) {5564 die_error(400,"Unknown snapshot format");5565}elsif($known_snapshot_formats{$format}{'disabled'}) {5566 die_error(403,"Snapshot format not allowed");5567}elsif(!grep($_eq$format,@snapshot_fmts)) {5568 die_error(403,"Unsupported snapshot format");5569}55705571my$type= git_get_type("$hash^{}");5572if(!$type) {5573 die_error(404,'Object does not exist');5574}elsif($typeeq'blob') {5575 die_error(400,'Object is not a tree-ish');5576}55775578my($name,$prefix) = snapshot_name($project,$hash);5579my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5580my$cmd= quote_command(5581 git_cmd(),'archive',5582"--format=$known_snapshot_formats{$format}{'format'}",5583"--prefix=$prefix/",$hash);5584if(exists$known_snapshot_formats{$format}{'compressor'}) {5585$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5586}55875588$filename=~s/(["\\])/\\$1/g;5589print$cgi->header(5590-type =>$known_snapshot_formats{$format}{'type'},5591-content_disposition =>'inline; filename="'.$filename.'"',5592-status =>'200 OK');55935594open my$fd,"-|",$cmd5595or die_error(500,"Execute git-archive failed");5596binmode STDOUT,':raw';5597print<$fd>;5598binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5599close$fd;5600}56015602sub git_log_generic {5603my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;56045605my$head= git_get_head_hash($project);5606if(!defined$base) {5607$base=$head;5608}5609if(!defined$page) {5610$page=0;5611}5612my$refs= git_get_references();56135614my$commit_hash=$base;5615if(defined$parent) {5616$commit_hash="$parent..$base";5617}5618my@commitlist=5619 parse_commits($commit_hash,101, (100*$page),5620defined$file_name? ($file_name,"--full-history") : ());56215622my$ftype;5623if(!defined$file_hash&&defined$file_name) {5624# some commits could have deleted file in question,5625# and not have it in tree, but one of them has to have it5626for(my$i=0;$i<@commitlist;$i++) {5627$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5628last ifdefined$file_hash;5629}5630}5631if(defined$file_hash) {5632$ftype= git_get_type($file_hash);5633}5634if(defined$file_name&& !defined$ftype) {5635 die_error(500,"Unknown type of object");5636}5637my%co;5638if(defined$file_name) {5639%co= parse_commit($base)5640or die_error(404,"Unknown commit object");5641}564256435644my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5645my$next_link='';5646if($#commitlist>=100) {5647$next_link=5648$cgi->a({-href => href(-replay=>1, page=>$page+1),5649-accesskey =>"n", -title =>"Alt-n"},"next");5650}5651my$patch_max= gitweb_get_feature('patches');5652if($patch_max&& !defined$file_name) {5653if($patch_max<0||@commitlist<=$patch_max) {5654$paging_nav.=" ⋅ ".5655$cgi->a({-href => href(action=>"patches", -replay=>1)},5656"patches");5657}5658}56595660 git_header_html();5661 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5662if(defined$file_name) {5663 git_print_header_div('commit', esc_html($co{'title'}),$base);5664}else{5665 git_print_header_div('summary',$project)5666}5667 git_print_page_path($file_name,$ftype,$hash_base)5668if(defined$file_name);56695670$body_subr->(\@commitlist,0,99,$refs,$next_link,5671$file_name,$file_hash,$ftype);56725673 git_footer_html();5674}56755676sub git_log {5677 git_log_generic('log', \&git_log_body,5678$hash,$hash_parent);5679}56805681sub git_commit {5682$hash||=$hash_base||"HEAD";5683my%co= parse_commit($hash)5684or die_error(404,"Unknown commit object");56855686my$parent=$co{'parent'};5687my$parents=$co{'parents'};# listref56885689# we need to prepare $formats_nav before any parameter munging5690my$formats_nav;5691if(!defined$parent) {5692# --root commitdiff5693$formats_nav.='(initial)';5694}elsif(@$parents==1) {5695# single parent commit5696$formats_nav.=5697'(parent: '.5698$cgi->a({-href => href(action=>"commit",5699 hash=>$parent)},5700 esc_html(substr($parent,0,7))) .5701')';5702}else{5703# merge commit5704$formats_nav.=5705'(merge: '.5706join(' ',map{5707$cgi->a({-href => href(action=>"commit",5708 hash=>$_)},5709 esc_html(substr($_,0,7)));5710}@$parents) .5711')';5712}5713if(gitweb_check_feature('patches') &&@$parents<=1) {5714$formats_nav.=" | ".5715$cgi->a({-href => href(action=>"patch", -replay=>1)},5716"patch");5717}57185719if(!defined$parent) {5720$parent="--root";5721}5722my@difftree;5723open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5724@diff_opts,5725(@$parents<=1?$parent:'-c'),5726$hash,"--"5727or die_error(500,"Open git-diff-tree failed");5728@difftree=map{chomp;$_} <$fd>;5729close$fdor die_error(404,"Reading git-diff-tree failed");57305731# non-textual hash id's can be cached5732my$expires;5733if($hash=~m/^[0-9a-fA-F]{40}$/) {5734$expires="+1d";5735}5736my$refs= git_get_references();5737my$ref= format_ref_marker($refs,$co{'id'});57385739 git_header_html(undef,$expires);5740 git_print_page_nav('commit','',5741$hash,$co{'tree'},$hash,5742$formats_nav);57435744if(defined$co{'parent'}) {5745 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5746}else{5747 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5748}5749print"<div class=\"title_text\">\n".5750"<table class=\"object_header\">\n";5751 git_print_authorship_rows(\%co);5752print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5753print"<tr>".5754"<td>tree</td>".5755"<td class=\"sha1\">".5756$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5757class=>"list"},$co{'tree'}) .5758"</td>".5759"<td class=\"link\">".5760$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5761"tree");5762my$snapshot_links= format_snapshot_links($hash);5763if(defined$snapshot_links) {5764print" | ".$snapshot_links;5765}5766print"</td>".5767"</tr>\n";57685769foreachmy$par(@$parents) {5770print"<tr>".5771"<td>parent</td>".5772"<td class=\"sha1\">".5773$cgi->a({-href => href(action=>"commit", hash=>$par),5774class=>"list"},$par) .5775"</td>".5776"<td class=\"link\">".5777$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5778" | ".5779$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5780"</td>".5781"</tr>\n";5782}5783print"</table>".5784"</div>\n";57855786print"<div class=\"page_body\">\n";5787 git_print_log($co{'comment'});5788print"</div>\n";57895790 git_difftree_body(\@difftree,$hash,@$parents);57915792 git_footer_html();5793}57945795sub git_object {5796# object is defined by:5797# - hash or hash_base alone5798# - hash_base and file_name5799my$type;58005801# - hash or hash_base alone5802if($hash|| ($hash_base&& !defined$file_name)) {5803my$object_id=$hash||$hash_base;58045805open my$fd,"-|", quote_command(5806 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5807or die_error(404,"Object does not exist");5808$type= <$fd>;5809chomp$type;5810close$fd5811or die_error(404,"Object does not exist");58125813# - hash_base and file_name5814}elsif($hash_base&&defined$file_name) {5815$file_name=~ s,/+$,,;58165817system(git_cmd(),"cat-file",'-e',$hash_base) ==05818or die_error(404,"Base object does not exist");58195820# here errors should not hapen5821open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5822or die_error(500,"Open git-ls-tree failed");5823my$line= <$fd>;5824close$fd;58255826#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5827unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5828 die_error(404,"File or directory for given base does not exist");5829}5830$type=$2;5831$hash=$3;5832}else{5833 die_error(400,"Not enough information to find object");5834}58355836print$cgi->redirect(-uri => href(action=>$type, -full=>1,5837 hash=>$hash, hash_base=>$hash_base,5838 file_name=>$file_name),5839-status =>'302 Found');5840}58415842sub git_blobdiff {5843my$format=shift||'html';58445845my$fd;5846my@difftree;5847my%diffinfo;5848my$expires;58495850# preparing $fd and %diffinfo for git_patchset_body5851# new style URI5852if(defined$hash_base&&defined$hash_parent_base) {5853if(defined$file_name) {5854# read raw output5855open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5856$hash_parent_base,$hash_base,5857"--", (defined$file_parent?$file_parent: ()),$file_name5858or die_error(500,"Open git-diff-tree failed");5859@difftree=map{chomp;$_} <$fd>;5860close$fd5861or die_error(404,"Reading git-diff-tree failed");5862@difftree5863or die_error(404,"Blob diff not found");58645865}elsif(defined$hash&&5866$hash=~/[0-9a-fA-F]{40}/) {5867# try to find filename from $hash58685869# read filtered raw output5870open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5871$hash_parent_base,$hash_base,"--"5872or die_error(500,"Open git-diff-tree failed");5873@difftree=5874# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5875# $hash == to_id5876grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5877map{chomp;$_} <$fd>;5878close$fd5879or die_error(404,"Reading git-diff-tree failed");5880@difftree5881or die_error(404,"Blob diff not found");58825883}else{5884 die_error(400,"Missing one of the blob diff parameters");5885}58865887if(@difftree>1) {5888 die_error(400,"Ambiguous blob diff specification");5889}58905891%diffinfo= parse_difftree_raw_line($difftree[0]);5892$file_parent||=$diffinfo{'from_file'} ||$file_name;5893$file_name||=$diffinfo{'to_file'};58945895$hash_parent||=$diffinfo{'from_id'};5896$hash||=$diffinfo{'to_id'};58975898# non-textual hash id's can be cached5899if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5900$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5901$expires='+1d';5902}59035904# open patch output5905open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5906'-p', ($formateq'html'?"--full-index": ()),5907$hash_parent_base,$hash_base,5908"--", (defined$file_parent?$file_parent: ()),$file_name5909or die_error(500,"Open git-diff-tree failed");5910}59115912# old/legacy style URI -- not generated anymore since 1.4.3.5913if(!%diffinfo) {5914 die_error('404 Not Found',"Missing one of the blob diff parameters")5915}59165917# header5918if($formateq'html') {5919my$formats_nav=5920$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5921"raw");5922 git_header_html(undef,$expires);5923if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5924 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5925 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5926}else{5927print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5928print"<div class=\"title\">$hashvs$hash_parent</div>\n";5929}5930if(defined$file_name) {5931 git_print_page_path($file_name,"blob",$hash_base);5932}else{5933print"<div class=\"page_path\"></div>\n";5934}59355936}elsif($formateq'plain') {5937print$cgi->header(5938-type =>'text/plain',5939-charset =>'utf-8',5940-expires =>$expires,5941-content_disposition =>'inline; filename="'."$file_name".'.patch"');59425943print"X-Git-Url: ".$cgi->self_url() ."\n\n";59445945}else{5946 die_error(400,"Unknown blobdiff format");5947}59485949# patch5950if($formateq'html') {5951print"<div class=\"page_body\">\n";59525953 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5954close$fd;59555956print"</div>\n";# class="page_body"5957 git_footer_html();59585959}else{5960while(my$line= <$fd>) {5961$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5962$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;59635964print$line;59655966last if$line=~m!^\+\+\+!;5967}5968local$/=undef;5969print<$fd>;5970close$fd;5971}5972}59735974sub git_blobdiff_plain {5975 git_blobdiff('plain');5976}59775978sub git_commitdiff {5979my%params=@_;5980my$format=$params{-format} ||'html';59815982my($patch_max) = gitweb_get_feature('patches');5983if($formateq'patch') {5984 die_error(403,"Patch view not allowed")unless$patch_max;5985}59865987$hash||=$hash_base||"HEAD";5988my%co= parse_commit($hash)5989or die_error(404,"Unknown commit object");59905991# choose format for commitdiff for merge5992if(!defined$hash_parent&& @{$co{'parents'}} >1) {5993$hash_parent='--cc';5994}5995# we need to prepare $formats_nav before almost any parameter munging5996my$formats_nav;5997if($formateq'html') {5998$formats_nav=5999$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6000"raw");6001if($patch_max&& @{$co{'parents'}} <=1) {6002$formats_nav.=" | ".6003$cgi->a({-href => href(action=>"patch", -replay=>1)},6004"patch");6005}60066007if(defined$hash_parent&&6008$hash_parentne'-c'&&$hash_parentne'--cc') {6009# commitdiff with two commits given6010my$hash_parent_short=$hash_parent;6011if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6012$hash_parent_short=substr($hash_parent,0,7);6013}6014$formats_nav.=6015' (from';6016for(my$i=0;$i< @{$co{'parents'}};$i++) {6017if($co{'parents'}[$i]eq$hash_parent) {6018$formats_nav.=' parent '. ($i+1);6019last;6020}6021}6022$formats_nav.=': '.6023$cgi->a({-href => href(action=>"commitdiff",6024 hash=>$hash_parent)},6025 esc_html($hash_parent_short)) .6026')';6027}elsif(!$co{'parent'}) {6028# --root commitdiff6029$formats_nav.=' (initial)';6030}elsif(scalar@{$co{'parents'}} ==1) {6031# single parent commit6032$formats_nav.=6033' (parent: '.6034$cgi->a({-href => href(action=>"commitdiff",6035 hash=>$co{'parent'})},6036 esc_html(substr($co{'parent'},0,7))) .6037')';6038}else{6039# merge commit6040if($hash_parenteq'--cc') {6041$formats_nav.=' | '.6042$cgi->a({-href => href(action=>"commitdiff",6043 hash=>$hash, hash_parent=>'-c')},6044'combined');6045}else{# $hash_parent eq '-c'6046$formats_nav.=' | '.6047$cgi->a({-href => href(action=>"commitdiff",6048 hash=>$hash, hash_parent=>'--cc')},6049'compact');6050}6051$formats_nav.=6052' (merge: '.6053join(' ',map{6054$cgi->a({-href => href(action=>"commitdiff",6055 hash=>$_)},6056 esc_html(substr($_,0,7)));6057} @{$co{'parents'}} ) .6058')';6059}6060}60616062my$hash_parent_param=$hash_parent;6063if(!defined$hash_parent_param) {6064# --cc for multiple parents, --root for parentless6065$hash_parent_param=6066@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6067}60686069# read commitdiff6070my$fd;6071my@difftree;6072if($formateq'html') {6073open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6074"--no-commit-id","--patch-with-raw","--full-index",6075$hash_parent_param,$hash,"--"6076or die_error(500,"Open git-diff-tree failed");60776078while(my$line= <$fd>) {6079chomp$line;6080# empty line ends raw part of diff-tree output6081last unless$line;6082push@difftree,scalar parse_difftree_raw_line($line);6083}60846085}elsif($formateq'plain') {6086open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6087'-p',$hash_parent_param,$hash,"--"6088or die_error(500,"Open git-diff-tree failed");6089}elsif($formateq'patch') {6090# For commit ranges, we limit the output to the number of6091# patches specified in the 'patches' feature.6092# For single commits, we limit the output to a single patch,6093# diverging from the git-format-patch default.6094my@commit_spec= ();6095if($hash_parent) {6096if($patch_max>0) {6097push@commit_spec,"-$patch_max";6098}6099push@commit_spec,'-n',"$hash_parent..$hash";6100}else{6101if($params{-single}) {6102push@commit_spec,'-1';6103}else{6104if($patch_max>0) {6105push@commit_spec,"-$patch_max";6106}6107push@commit_spec,"-n";6108}6109push@commit_spec,'--root',$hash;6110}6111open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',6112'--stdout',@commit_spec6113or die_error(500,"Open git-format-patch failed");6114}else{6115 die_error(400,"Unknown commitdiff format");6116}61176118# non-textual hash id's can be cached6119my$expires;6120if($hash=~m/^[0-9a-fA-F]{40}$/) {6121$expires="+1d";6122}61236124# write commit message6125if($formateq'html') {6126my$refs= git_get_references();6127my$ref= format_ref_marker($refs,$co{'id'});61286129 git_header_html(undef,$expires);6130 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6131 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6132print"<div class=\"title_text\">\n".6133"<table class=\"object_header\">\n";6134 git_print_authorship_rows(\%co);6135print"</table>".6136"</div>\n";6137print"<div class=\"page_body\">\n";6138if(@{$co{'comment'}} >1) {6139print"<div class=\"log\">\n";6140 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6141print"</div>\n";# class="log"6142}61436144}elsif($formateq'plain') {6145my$refs= git_get_references("tags");6146my$tagname= git_get_rev_name_tags($hash);6147my$filename= basename($project) ."-$hash.patch";61486149print$cgi->header(6150-type =>'text/plain',6151-charset =>'utf-8',6152-expires =>$expires,6153-content_disposition =>'inline; filename="'."$filename".'"');6154my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6155print"From: ". to_utf8($co{'author'}) ."\n";6156print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6157print"Subject: ". to_utf8($co{'title'}) ."\n";61586159print"X-Git-Tag:$tagname\n"if$tagname;6160print"X-Git-Url: ".$cgi->self_url() ."\n\n";61616162foreachmy$line(@{$co{'comment'}}) {6163print to_utf8($line) ."\n";6164}6165print"---\n\n";6166}elsif($formateq'patch') {6167my$filename= basename($project) ."-$hash.patch";61686169print$cgi->header(6170-type =>'text/plain',6171-charset =>'utf-8',6172-expires =>$expires,6173-content_disposition =>'inline; filename="'."$filename".'"');6174}61756176# write patch6177if($formateq'html') {6178my$use_parents= !defined$hash_parent||6179$hash_parenteq'-c'||$hash_parenteq'--cc';6180 git_difftree_body(\@difftree,$hash,6181$use_parents? @{$co{'parents'}} :$hash_parent);6182print"<br/>\n";61836184 git_patchset_body($fd, \@difftree,$hash,6185$use_parents? @{$co{'parents'}} :$hash_parent);6186close$fd;6187print"</div>\n";# class="page_body"6188 git_footer_html();61896190}elsif($formateq'plain') {6191local$/=undef;6192print<$fd>;6193close$fd6194or print"Reading git-diff-tree failed\n";6195}elsif($formateq'patch') {6196local$/=undef;6197print<$fd>;6198close$fd6199or print"Reading git-format-patch failed\n";6200}6201}62026203sub git_commitdiff_plain {6204 git_commitdiff(-format =>'plain');6205}62066207# format-patch-style patches6208sub git_patch {6209 git_commitdiff(-format =>'patch', -single =>1);6210}62116212sub git_patches {6213 git_commitdiff(-format =>'patch');6214}62156216sub git_history {6217 git_log_generic('history', \&git_history_body,6218$hash_base,$hash_parent_base,6219$file_name,$hash);6220}62216222sub git_search {6223 gitweb_check_feature('search')or die_error(403,"Search is disabled");6224if(!defined$searchtext) {6225 die_error(400,"Text field is empty");6226}6227if(!defined$hash) {6228$hash= git_get_head_hash($project);6229}6230my%co= parse_commit($hash);6231if(!%co) {6232 die_error(404,"Unknown commit object");6233}6234if(!defined$page) {6235$page=0;6236}62376238$searchtype||='commit';6239if($searchtypeeq'pickaxe') {6240# pickaxe may take all resources of your box and run for several minutes6241# with every query - so decide by yourself how public you make this feature6242 gitweb_check_feature('pickaxe')6243or die_error(403,"Pickaxe is disabled");6244}6245if($searchtypeeq'grep') {6246 gitweb_check_feature('grep')6247or die_error(403,"Grep is disabled");6248}62496250 git_header_html();62516252if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6253my$greptype;6254if($searchtypeeq'commit') {6255$greptype="--grep=";6256}elsif($searchtypeeq'author') {6257$greptype="--author=";6258}elsif($searchtypeeq'committer') {6259$greptype="--committer=";6260}6261$greptype.=$searchtext;6262my@commitlist= parse_commits($hash,101, (100*$page),undef,6263$greptype,'--regexp-ignore-case',6264$search_use_regexp?'--extended-regexp':'--fixed-strings');62656266my$paging_nav='';6267if($page>0) {6268$paging_nav.=6269$cgi->a({-href => href(action=>"search", hash=>$hash,6270 searchtext=>$searchtext,6271 searchtype=>$searchtype)},6272"first");6273$paging_nav.=" ⋅ ".6274$cgi->a({-href => href(-replay=>1, page=>$page-1),6275-accesskey =>"p", -title =>"Alt-p"},"prev");6276}else{6277$paging_nav.="first";6278$paging_nav.=" ⋅ prev";6279}6280my$next_link='';6281if($#commitlist>=100) {6282$next_link=6283$cgi->a({-href => href(-replay=>1, page=>$page+1),6284-accesskey =>"n", -title =>"Alt-n"},"next");6285$paging_nav.=" ⋅$next_link";6286}else{6287$paging_nav.=" ⋅ next";6288}62896290if($#commitlist>=100) {6291}62926293 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6294 git_print_header_div('commit', esc_html($co{'title'}),$hash);6295 git_search_grep_body(\@commitlist,0,99,$next_link);6296}62976298if($searchtypeeq'pickaxe') {6299 git_print_page_nav('','',$hash,$co{'tree'},$hash);6300 git_print_header_div('commit', esc_html($co{'title'}),$hash);63016302print"<table class=\"pickaxe search\">\n";6303my$alternate=1;6304local$/="\n";6305open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6306'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6307($search_use_regexp?'--pickaxe-regex': ());6308undef%co;6309my@files;6310while(my$line= <$fd>) {6311chomp$line;6312next unless$line;63136314my%set= parse_difftree_raw_line($line);6315if(defined$set{'commit'}) {6316# finish previous commit6317if(%co) {6318print"</td>\n".6319"<td class=\"link\">".6320$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6321" | ".6322$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6323print"</td>\n".6324"</tr>\n";6325}63266327if($alternate) {6328print"<tr class=\"dark\">\n";6329}else{6330print"<tr class=\"light\">\n";6331}6332$alternate^=1;6333%co= parse_commit($set{'commit'});6334my$author= chop_and_escape_str($co{'author_name'},15,5);6335print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6336"<td><i>$author</i></td>\n".6337"<td>".6338$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6339-class=>"list subject"},6340 chop_and_escape_str($co{'title'},50) ."<br/>");6341}elsif(defined$set{'to_id'}) {6342next if($set{'to_id'} =~m/^0{40}$/);63436344print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6345 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6346-class=>"list"},6347"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6348"<br/>\n";6349}6350}6351close$fd;63526353# finish last commit (warning: repetition!)6354if(%co) {6355print"</td>\n".6356"<td class=\"link\">".6357$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6358" | ".6359$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6360print"</td>\n".6361"</tr>\n";6362}63636364print"</table>\n";6365}63666367if($searchtypeeq'grep') {6368 git_print_page_nav('','',$hash,$co{'tree'},$hash);6369 git_print_header_div('commit', esc_html($co{'title'}),$hash);63706371print"<table class=\"grep_search\">\n";6372my$alternate=1;6373my$matches=0;6374local$/="\n";6375open my$fd,"-|", git_cmd(),'grep','-n',6376$search_use_regexp? ('-E','-i') :'-F',6377$searchtext,$co{'tree'};6378my$lastfile='';6379while(my$line= <$fd>) {6380chomp$line;6381my($file,$lno,$ltext,$binary);6382last if($matches++>1000);6383if($line=~/^Binary file (.+) matches$/) {6384$file=$1;6385$binary=1;6386}else{6387(undef,$file,$lno,$ltext) =split(/:/,$line,4);6388}6389if($filene$lastfile) {6390$lastfileand print"</td></tr>\n";6391if($alternate++) {6392print"<tr class=\"dark\">\n";6393}else{6394print"<tr class=\"light\">\n";6395}6396print"<td class=\"list\">".6397$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6398 file_name=>"$file"),6399-class=>"list"}, esc_path($file));6400print"</td><td>\n";6401$lastfile=$file;6402}6403if($binary) {6404print"<div class=\"binary\">Binary file</div>\n";6405}else{6406$ltext= untabify($ltext);6407if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6408$ltext= esc_html($1, -nbsp=>1);6409$ltext.='<span class="match">';6410$ltext.= esc_html($2, -nbsp=>1);6411$ltext.='</span>';6412$ltext.= esc_html($3, -nbsp=>1);6413}else{6414$ltext= esc_html($ltext, -nbsp=>1);6415}6416print"<div class=\"pre\">".6417$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6418 file_name=>"$file").'#l'.$lno,6419-class=>"linenr"},sprintf('%4i',$lno))6420.' '.$ltext."</div>\n";6421}6422}6423if($lastfile) {6424print"</td></tr>\n";6425if($matches>1000) {6426print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6427}6428}else{6429print"<div class=\"diff nodifferences\">No matches found</div>\n";6430}6431close$fd;64326433print"</table>\n";6434}6435 git_footer_html();6436}64376438sub git_search_help {6439 git_header_html();6440 git_print_page_nav('','',$hash,$hash,$hash);6441print<<EOT;6442<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6443regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6444the pattern entered is recognized as the POSIX extended6445<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6446insensitive).</p>6447<dl>6448<dt><b>commit</b></dt>6449<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6450EOT6451my$have_grep= gitweb_check_feature('grep');6452if($have_grep) {6453print<<EOT;6454<dt><b>grep</b></dt>6455<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6456 a different one) are searched for the given pattern. On large trees, this search can take6457a while and put some strain on the server, so please use it with some consideration. Note that6458due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6459case-sensitive.</dd>6460EOT6461}6462print<<EOT;6463<dt><b>author</b></dt>6464<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6465<dt><b>committer</b></dt>6466<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6467EOT6468my$have_pickaxe= gitweb_check_feature('pickaxe');6469if($have_pickaxe) {6470print<<EOT;6471<dt><b>pickaxe</b></dt>6472<dd>All commits that caused the string to appear or disappear from any file (changes that6473added, removed or "modified" the string) will be listed. This search can take a while and6474takes a lot of strain on the server, so please use it wisely. Note that since you may be6475interested even in changes just changing the case as well, this search is case sensitive.</dd>6476EOT6477}6478print"</dl>\n";6479 git_footer_html();6480}64816482sub git_shortlog {6483 git_log_generic('shortlog', \&git_shortlog_body,6484$hash,$hash_parent);6485}64866487## ......................................................................6488## feeds (RSS, Atom; OPML)64896490sub git_feed {6491my$format=shift||'atom';6492my$have_blame= gitweb_check_feature('blame');64936494# Atom: http://www.atomenabled.org/developers/syndication/6495# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6496if($formatne'rss'&&$formatne'atom') {6497 die_error(400,"Unknown web feed format");6498}64996500# log/feed of current (HEAD) branch, log of given branch, history of file/directory6501my$head=$hash||'HEAD';6502my@commitlist= parse_commits($head,150,0,$file_name);65036504my%latest_commit;6505my%latest_date;6506my$content_type="application/$format+xml";6507if(defined$cgi->http('HTTP_ACCEPT') &&6508$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6509# browser (feed reader) prefers text/xml6510$content_type='text/xml';6511}6512if(defined($commitlist[0])) {6513%latest_commit= %{$commitlist[0]};6514my$latest_epoch=$latest_commit{'committer_epoch'};6515%latest_date= parse_date($latest_epoch);6516my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6517if(defined$if_modified) {6518my$since;6519if(eval{require HTTP::Date;1; }) {6520$since= HTTP::Date::str2time($if_modified);6521}elsif(eval{require Time::ParseDate;1; }) {6522$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6523}6524if(defined$since&&$latest_epoch<=$since) {6525print$cgi->header(6526-type =>$content_type,6527-charset =>'utf-8',6528-last_modified =>$latest_date{'rfc2822'},6529-status =>'304 Not Modified');6530return;6531}6532}6533print$cgi->header(6534-type =>$content_type,6535-charset =>'utf-8',6536-last_modified =>$latest_date{'rfc2822'});6537}else{6538print$cgi->header(6539-type =>$content_type,6540-charset =>'utf-8');6541}65426543# Optimization: skip generating the body if client asks only6544# for Last-Modified date.6545return if($cgi->request_method()eq'HEAD');65466547# header variables6548my$title="$site_name-$project/$action";6549my$feed_type='log';6550if(defined$hash) {6551$title.=" - '$hash'";6552$feed_type='branch log';6553if(defined$file_name) {6554$title.=" ::$file_name";6555$feed_type='history';6556}6557}elsif(defined$file_name) {6558$title.=" -$file_name";6559$feed_type='history';6560}6561$title.="$feed_type";6562my$descr= git_get_project_description($project);6563if(defined$descr) {6564$descr= esc_html($descr);6565}else{6566$descr="$project".6567($formateq'rss'?'RSS':'Atom') .6568" feed";6569}6570my$owner= git_get_project_owner($project);6571$owner= esc_html($owner);65726573#header6574my$alt_url;6575if(defined$file_name) {6576$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6577}elsif(defined$hash) {6578$alt_url= href(-full=>1, action=>"log", hash=>$hash);6579}else{6580$alt_url= href(-full=>1, action=>"summary");6581}6582print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6583if($formateq'rss') {6584print<<XML;6585<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6586<channel>6587XML6588print"<title>$title</title>\n".6589"<link>$alt_url</link>\n".6590"<description>$descr</description>\n".6591"<language>en</language>\n".6592# project owner is responsible for 'editorial' content6593"<managingEditor>$owner</managingEditor>\n";6594if(defined$logo||defined$favicon) {6595# prefer the logo to the favicon, since RSS6596# doesn't allow both6597my$img= esc_url($logo||$favicon);6598print"<image>\n".6599"<url>$img</url>\n".6600"<title>$title</title>\n".6601"<link>$alt_url</link>\n".6602"</image>\n";6603}6604if(%latest_date) {6605print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6606print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6607}6608print"<generator>gitweb v.$version/$git_version</generator>\n";6609}elsif($formateq'atom') {6610print<<XML;6611<feed xmlns="http://www.w3.org/2005/Atom">6612XML6613print"<title>$title</title>\n".6614"<subtitle>$descr</subtitle>\n".6615'<link rel="alternate" type="text/html" href="'.6616$alt_url.'" />'."\n".6617'<link rel="self" type="'.$content_type.'" href="'.6618$cgi->self_url() .'" />'."\n".6619"<id>". href(-full=>1) ."</id>\n".6620# use project owner for feed author6621"<author><name>$owner</name></author>\n";6622if(defined$favicon) {6623print"<icon>". esc_url($favicon) ."</icon>\n";6624}6625if(defined$logo_url) {6626# not twice as wide as tall: 72 x 27 pixels6627print"<logo>". esc_url($logo) ."</logo>\n";6628}6629if(!%latest_date) {6630# dummy date to keep the feed valid until commits trickle in:6631print"<updated>1970-01-01T00:00:00Z</updated>\n";6632}else{6633print"<updated>$latest_date{'iso-8601'}</updated>\n";6634}6635print"<generator version='$version/$git_version'>gitweb</generator>\n";6636}66376638# contents6639for(my$i=0;$i<=$#commitlist;$i++) {6640my%co= %{$commitlist[$i]};6641my$commit=$co{'id'};6642# we read 150, we always show 30 and the ones more recent than 48 hours6643if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6644last;6645}6646my%cd= parse_date($co{'author_epoch'});66476648# get list of changed files6649open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6650$co{'parent'} ||"--root",6651$co{'id'},"--", (defined$file_name?$file_name: ())6652ornext;6653my@difftree=map{chomp;$_} <$fd>;6654close$fd6655ornext;66566657# print element (entry, item)6658my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6659if($formateq'rss') {6660print"<item>\n".6661"<title>". esc_html($co{'title'}) ."</title>\n".6662"<author>". esc_html($co{'author'}) ."</author>\n".6663"<pubDate>$cd{'rfc2822'}</pubDate>\n".6664"<guid isPermaLink=\"true\">$co_url</guid>\n".6665"<link>$co_url</link>\n".6666"<description>". esc_html($co{'title'}) ."</description>\n".6667"<content:encoded>".6668"<![CDATA[\n";6669}elsif($formateq'atom') {6670print"<entry>\n".6671"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6672"<updated>$cd{'iso-8601'}</updated>\n".6673"<author>\n".6674" <name>". esc_html($co{'author_name'}) ."</name>\n";6675if($co{'author_email'}) {6676print" <email>". esc_html($co{'author_email'}) ."</email>\n";6677}6678print"</author>\n".6679# use committer for contributor6680"<contributor>\n".6681" <name>". esc_html($co{'committer_name'}) ."</name>\n";6682if($co{'committer_email'}) {6683print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6684}6685print"</contributor>\n".6686"<published>$cd{'iso-8601'}</published>\n".6687"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6688"<id>$co_url</id>\n".6689"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6690"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6691}6692my$comment=$co{'comment'};6693print"<pre>\n";6694foreachmy$line(@$comment) {6695$line= esc_html($line);6696print"$line\n";6697}6698print"</pre><ul>\n";6699foreachmy$difftree_line(@difftree) {6700my%difftree= parse_difftree_raw_line($difftree_line);6701next if!$difftree{'from_id'};67026703my$file=$difftree{'file'} ||$difftree{'to_file'};67046705print"<li>".6706"[".6707$cgi->a({-href => href(-full=>1, action=>"blobdiff",6708 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6709 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6710 file_name=>$file, file_parent=>$difftree{'from_file'}),6711-title =>"diff"},'D');6712if($have_blame) {6713print$cgi->a({-href => href(-full=>1, action=>"blame",6714 file_name=>$file, hash_base=>$commit),6715-title =>"blame"},'B');6716}6717# if this is not a feed of a file history6718if(!defined$file_name||$file_namene$file) {6719print$cgi->a({-href => href(-full=>1, action=>"history",6720 file_name=>$file, hash=>$commit),6721-title =>"history"},'H');6722}6723$file= esc_path($file);6724print"] ".6725"$file</li>\n";6726}6727if($formateq'rss') {6728print"</ul>]]>\n".6729"</content:encoded>\n".6730"</item>\n";6731}elsif($formateq'atom') {6732print"</ul>\n</div>\n".6733"</content>\n".6734"</entry>\n";6735}6736}67376738# end of feed6739if($formateq'rss') {6740print"</channel>\n</rss>\n";6741}elsif($formateq'atom') {6742print"</feed>\n";6743}6744}67456746sub git_rss {6747 git_feed('rss');6748}67496750sub git_atom {6751 git_feed('atom');6752}67536754sub git_opml {6755my@list= git_get_projects_list();67566757print$cgi->header(6758-type =>'text/xml',6759-charset =>'utf-8',6760-content_disposition =>'inline; filename="opml.xml"');67616762print<<XML;6763<?xml version="1.0" encoding="utf-8"?>6764<opml version="1.0">6765<head>6766 <title>$site_nameOPML Export</title>6767</head>6768<body>6769<outline text="git RSS feeds">6770XML67716772foreachmy$pr(@list) {6773my%proj=%$pr;6774my$head= git_get_head_hash($proj{'path'});6775if(!defined$head) {6776next;6777}6778$git_dir="$projectroot/$proj{'path'}";6779my%co= parse_commit($head);6780if(!%co) {6781next;6782}67836784my$path= esc_html(chop_str($proj{'path'},25,5));6785my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6786my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6787print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6788}6789print<<XML;6790</outline>6791</body>6792</opml>6793XML6794}