1#!/usr/bin/perl 2 3# gitweb - simple web interface to track changes in git repositories 4# 5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org> 6# (C) 2005, Christian Gierke 7# 8# This program is licensed under the GPLv2 9 10use strict; 11use warnings; 12use CGI qw(:standard :escapeHTML -nosticky); 13use CGI::Util qw(unescape); 14use CGI::Carp qw(fatalsToBrowser); 15use Encode; 16use Fcntl ':mode'; 17use File::Find qw(); 18use File::Basename qw(basename); 19binmode STDOUT,':utf8'; 20 21our$t0; 22if(eval{require Time::HiRes;1; }) { 23$t0= [Time::HiRes::gettimeofday()]; 24} 25our$number_of_git_cmds=0; 26 27BEGIN{ 28 CGI->compile()if$ENV{'MOD_PERL'}; 29} 30 31our$cgi= new CGI; 32our$version="++GIT_VERSION++"; 33our$my_url=$cgi->url(); 34our$my_uri=$cgi->url(-absolute =>1); 35 36# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 37# needed and used only for URLs with nonempty PATH_INFO 38our$base_url=$my_url; 39 40# When the script is used as DirectoryIndex, the URL does not contain the name 41# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 42# have to do it ourselves. We make $path_info global because it's also used 43# later on. 44# 45# Another issue with the script being the DirectoryIndex is that the resulting 46# $my_url data is not the full script URL: this is good, because we want 47# generated links to keep implying the script name if it wasn't explicitly 48# indicated in the URL we're handling, but it means that $my_url cannot be used 49# as base URL. 50# Therefore, if we needed to strip PATH_INFO, then we know that we have 51# to build the base URL ourselves: 52our$path_info=$ENV{"PATH_INFO"}; 53if($path_info) { 54if($my_url=~ s,\Q$path_info\E$,, && 55$my_uri=~ s,\Q$path_info\E$,, && 56defined$ENV{'SCRIPT_NAME'}) { 57$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 58} 59} 60 61# core git executable to use 62# this can just be "git" if your webserver has a sensible PATH 63our$GIT="++GIT_BINDIR++/git"; 64 65# absolute fs-path which will be prepended to the project path 66#our $projectroot = "/pub/scm"; 67our$projectroot="++GITWEB_PROJECTROOT++"; 68 69# fs traversing limit for getting project list 70# the number is relative to the projectroot 71our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 72 73# target of the home link on top of all pages 74our$home_link=$my_uri||"/"; 75 76# string of the home link on top of all pages 77our$home_link_str="++GITWEB_HOME_LINK_STR++"; 78 79# name of your site or organization to appear in page titles 80# replace this with something more descriptive for clearer bookmarks 81our$site_name="++GITWEB_SITENAME++" 82|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 83 84# filename of html text to include at top of each page 85our$site_header="++GITWEB_SITE_HEADER++"; 86# html text to include at home page 87our$home_text="++GITWEB_HOMETEXT++"; 88# filename of html text to include at bottom of each page 89our$site_footer="++GITWEB_SITE_FOOTER++"; 90 91# URI of stylesheets 92our@stylesheets= ("++GITWEB_CSS++"); 93# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 94our$stylesheet=undef; 95# URI of GIT logo (72x27 size) 96our$logo="++GITWEB_LOGO++"; 97# URI of GIT favicon, assumed to be image/png type 98our$favicon="++GITWEB_FAVICON++"; 99# URI of gitweb.js (JavaScript code for gitweb) 100our$javascript="++GITWEB_JS++"; 101 102# URI and label (title) of GIT logo link 103#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 104#our $logo_label = "git documentation"; 105our$logo_url="http://git-scm.com/"; 106our$logo_label="git homepage"; 107 108# source of projects list 109our$projects_list="++GITWEB_LIST++"; 110 111# the width (in characters) of the projects list "Description" column 112our$projects_list_description_width=25; 113 114# default order of projects list 115# valid values are none, project, descr, owner, and age 116our$default_projects_order="project"; 117 118# show repository only if this file exists 119# (only effective if this variable evaluates to true) 120our$export_ok="++GITWEB_EXPORT_OK++"; 121 122# show repository only if this subroutine returns true 123# when given the path to the project, for example: 124# sub { return -e "$_[0]/git-daemon-export-ok"; } 125our$export_auth_hook=undef; 126 127# only allow viewing of repositories also shown on the overview page 128our$strict_export="++GITWEB_STRICT_EXPORT++"; 129 130# list of git base URLs used for URL to where fetch project from, 131# i.e. full URL is "$git_base_url/$project" 132our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 133 134# default blob_plain mimetype and default charset for text/plain blob 135our$default_blob_plain_mimetype='text/plain'; 136our$default_text_plain_charset=undef; 137 138# file to use for guessing MIME types before trying /etc/mime.types 139# (relative to the current git repository) 140our$mimetypes_file=undef; 141 142# assume this charset if line contains non-UTF-8 characters; 143# it should be valid encoding (see Encoding::Supported(3pm) for list), 144# for which encoding all byte sequences are valid, for example 145# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 146# could be even 'utf-8' for the old behavior) 147our$fallback_encoding='latin1'; 148 149# rename detection options for git-diff and git-diff-tree 150# - default is '-M', with the cost proportional to 151# (number of removed files) * (number of new files). 152# - more costly is '-C' (which implies '-M'), with the cost proportional to 153# (number of changed files + number of removed files) * (number of new files) 154# - even more costly is '-C', '--find-copies-harder' with cost 155# (number of files in the original tree) * (number of new files) 156# - one might want to include '-B' option, e.g. '-B', '-M' 157our@diff_opts= ('-M');# taken from git_commit 158 159# Disables features that would allow repository owners to inject script into 160# the gitweb domain. 161our$prevent_xss=0; 162 163# information about snapshot formats that gitweb is capable of serving 164our%known_snapshot_formats= ( 165# name => { 166# 'display' => display name, 167# 'type' => mime type, 168# 'suffix' => filename suffix, 169# 'format' => --format for git-archive, 170# 'compressor' => [compressor command and arguments] 171# (array reference, optional) 172# 'disabled' => boolean (optional)} 173# 174'tgz'=> { 175'display'=>'tar.gz', 176'type'=>'application/x-gzip', 177'suffix'=>'.tar.gz', 178'format'=>'tar', 179'compressor'=> ['gzip']}, 180 181'tbz2'=> { 182'display'=>'tar.bz2', 183'type'=>'application/x-bzip2', 184'suffix'=>'.tar.bz2', 185'format'=>'tar', 186'compressor'=> ['bzip2']}, 187 188'txz'=> { 189'display'=>'tar.xz', 190'type'=>'application/x-xz', 191'suffix'=>'.tar.xz', 192'format'=>'tar', 193'compressor'=> ['xz'], 194'disabled'=>1}, 195 196'zip'=> { 197'display'=>'zip', 198'type'=>'application/x-zip', 199'suffix'=>'.zip', 200'format'=>'zip'}, 201); 202 203# Aliases so we understand old gitweb.snapshot values in repository 204# configuration. 205our%known_snapshot_format_aliases= ( 206'gzip'=>'tgz', 207'bzip2'=>'tbz2', 208'xz'=>'txz', 209 210# backward compatibility: legacy gitweb config support 211'x-gzip'=>undef,'gz'=>undef, 212'x-bzip2'=>undef,'bz2'=>undef, 213'x-zip'=>undef,''=>undef, 214); 215 216# Pixel sizes for icons and avatars. If the default font sizes or lineheights 217# are changed, it may be appropriate to change these values too via 218# $GITWEB_CONFIG. 219our%avatar_size= ( 220'default'=>16, 221'double'=>32 222); 223 224# Used to set the maximum load that we will still respond to gitweb queries. 225# If server load exceed this value then return "503 server busy" error. 226# If gitweb cannot determined server load, it is taken to be 0. 227# Leave it undefined (or set to 'undef') to turn off load checking. 228our$maxload=300; 229 230# You define site-wide feature defaults here; override them with 231# $GITWEB_CONFIG as necessary. 232our%feature= ( 233# feature => { 234# 'sub' => feature-sub (subroutine), 235# 'override' => allow-override (boolean), 236# 'default' => [ default options...] (array reference)} 237# 238# if feature is overridable (it means that allow-override has true value), 239# then feature-sub will be called with default options as parameters; 240# return value of feature-sub indicates if to enable specified feature 241# 242# if there is no 'sub' key (no feature-sub), then feature cannot be 243# overriden 244# 245# use gitweb_get_feature(<feature>) to retrieve the <feature> value 246# (an array) or gitweb_check_feature(<feature>) to check if <feature> 247# is enabled 248 249# Enable the 'blame' blob view, showing the last commit that modified 250# each line in the file. This can be very CPU-intensive. 251 252# To enable system wide have in $GITWEB_CONFIG 253# $feature{'blame'}{'default'} = [1]; 254# To have project specific config enable override in $GITWEB_CONFIG 255# $feature{'blame'}{'override'} = 1; 256# and in project config gitweb.blame = 0|1; 257'blame'=> { 258'sub'=>sub{ feature_bool('blame',@_) }, 259'override'=>0, 260'default'=> [0]}, 261 262# Enable the 'snapshot' link, providing a compressed archive of any 263# tree. This can potentially generate high traffic if you have large 264# project. 265 266# Value is a list of formats defined in %known_snapshot_formats that 267# you wish to offer. 268# To disable system wide have in $GITWEB_CONFIG 269# $feature{'snapshot'}{'default'} = []; 270# To have project specific config enable override in $GITWEB_CONFIG 271# $feature{'snapshot'}{'override'} = 1; 272# and in project config, a comma-separated list of formats or "none" 273# to disable. Example: gitweb.snapshot = tbz2,zip; 274'snapshot'=> { 275'sub'=> \&feature_snapshot, 276'override'=>0, 277'default'=> ['tgz']}, 278 279# Enable text search, which will list the commits which match author, 280# committer or commit text to a given string. Enabled by default. 281# Project specific override is not supported. 282'search'=> { 283'override'=>0, 284'default'=> [1]}, 285 286# Enable grep search, which will list the files in currently selected 287# tree containing the given string. Enabled by default. This can be 288# potentially CPU-intensive, of course. 289 290# To enable system wide have in $GITWEB_CONFIG 291# $feature{'grep'}{'default'} = [1]; 292# To have project specific config enable override in $GITWEB_CONFIG 293# $feature{'grep'}{'override'} = 1; 294# and in project config gitweb.grep = 0|1; 295'grep'=> { 296'sub'=>sub{ feature_bool('grep',@_) }, 297'override'=>0, 298'default'=> [1]}, 299 300# Enable the pickaxe search, which will list the commits that modified 301# a given string in a file. This can be practical and quite faster 302# alternative to 'blame', but still potentially CPU-intensive. 303 304# To enable system wide have in $GITWEB_CONFIG 305# $feature{'pickaxe'}{'default'} = [1]; 306# To have project specific config enable override in $GITWEB_CONFIG 307# $feature{'pickaxe'}{'override'} = 1; 308# and in project config gitweb.pickaxe = 0|1; 309'pickaxe'=> { 310'sub'=>sub{ feature_bool('pickaxe',@_) }, 311'override'=>0, 312'default'=> [1]}, 313 314# Enable showing size of blobs in a 'tree' view, in a separate 315# column, similar to what 'ls -l' does. This cost a bit of IO. 316 317# To disable system wide have in $GITWEB_CONFIG 318# $feature{'show-sizes'}{'default'} = [0]; 319# To have project specific config enable override in $GITWEB_CONFIG 320# $feature{'show-sizes'}{'override'} = 1; 321# and in project config gitweb.showsizes = 0|1; 322'show-sizes'=> { 323'sub'=>sub{ feature_bool('showsizes',@_) }, 324'override'=>0, 325'default'=> [1]}, 326 327# Make gitweb use an alternative format of the URLs which can be 328# more readable and natural-looking: project name is embedded 329# directly in the path and the query string contains other 330# auxiliary information. All gitweb installations recognize 331# URL in either format; this configures in which formats gitweb 332# generates links. 333 334# To enable system wide have in $GITWEB_CONFIG 335# $feature{'pathinfo'}{'default'} = [1]; 336# Project specific override is not supported. 337 338# Note that you will need to change the default location of CSS, 339# favicon, logo and possibly other files to an absolute URL. Also, 340# if gitweb.cgi serves as your indexfile, you will need to force 341# $my_uri to contain the script name in your $GITWEB_CONFIG. 342'pathinfo'=> { 343'override'=>0, 344'default'=> [0]}, 345 346# Make gitweb consider projects in project root subdirectories 347# to be forks of existing projects. Given project $projname.git, 348# projects matching $projname/*.git will not be shown in the main 349# projects list, instead a '+' mark will be added to $projname 350# there and a 'forks' view will be enabled for the project, listing 351# all the forks. If project list is taken from a file, forks have 352# to be listed after the main project. 353 354# To enable system wide have in $GITWEB_CONFIG 355# $feature{'forks'}{'default'} = [1]; 356# Project specific override is not supported. 357'forks'=> { 358'override'=>0, 359'default'=> [0]}, 360 361# Insert custom links to the action bar of all project pages. 362# This enables you mainly to link to third-party scripts integrating 363# into gitweb; e.g. git-browser for graphical history representation 364# or custom web-based repository administration interface. 365 366# The 'default' value consists of a list of triplets in the form 367# (label, link, position) where position is the label after which 368# to insert the link and link is a format string where %n expands 369# to the project name, %f to the project path within the filesystem, 370# %h to the current hash (h gitweb parameter) and %b to the current 371# hash base (hb gitweb parameter); %% expands to %. 372 373# To enable system wide have in $GITWEB_CONFIG e.g. 374# $feature{'actions'}{'default'} = [('graphiclog', 375# '/git-browser/by-commit.html?r=%n', 'summary')]; 376# Project specific override is not supported. 377'actions'=> { 378'override'=>0, 379'default'=> []}, 380 381# Allow gitweb scan project content tags described in ctags/ 382# of project repository, and display the popular Web 2.0-ish 383# "tag cloud" near the project list. Note that this is something 384# COMPLETELY different from the normal Git tags. 385 386# gitweb by itself can show existing tags, but it does not handle 387# tagging itself; you need an external application for that. 388# For an example script, check Girocco's cgi/tagproj.cgi. 389# You may want to install the HTML::TagCloud Perl module to get 390# a pretty tag cloud instead of just a list of tags. 391 392# To enable system wide have in $GITWEB_CONFIG 393# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 394# Project specific override is not supported. 395'ctags'=> { 396'override'=>0, 397'default'=> [0]}, 398 399# The maximum number of patches in a patchset generated in patch 400# view. Set this to 0 or undef to disable patch view, or to a 401# negative number to remove any limit. 402 403# To disable system wide have in $GITWEB_CONFIG 404# $feature{'patches'}{'default'} = [0]; 405# To have project specific config enable override in $GITWEB_CONFIG 406# $feature{'patches'}{'override'} = 1; 407# and in project config gitweb.patches = 0|n; 408# where n is the maximum number of patches allowed in a patchset. 409'patches'=> { 410'sub'=> \&feature_patches, 411'override'=>0, 412'default'=> [16]}, 413 414# Avatar support. When this feature is enabled, views such as 415# shortlog or commit will display an avatar associated with 416# the email of the committer(s) and/or author(s). 417 418# Currently available providers are gravatar and picon. 419# If an unknown provider is specified, the feature is disabled. 420 421# Gravatar depends on Digest::MD5. 422# Picon currently relies on the indiana.edu database. 423 424# To enable system wide have in $GITWEB_CONFIG 425# $feature{'avatar'}{'default'} = ['<provider>']; 426# where <provider> is either gravatar or picon. 427# To have project specific config enable override in $GITWEB_CONFIG 428# $feature{'avatar'}{'override'} = 1; 429# and in project config gitweb.avatar = <provider>; 430'avatar'=> { 431'sub'=> \&feature_avatar, 432'override'=>0, 433'default'=> ['']}, 434 435# Enable displaying how much time and how many git commands 436# it took to generate and display page. Disabled by default. 437# Project specific override is not supported. 438'timed'=> { 439'override'=>0, 440'default'=> [0]}, 441 442# Enable turning some links into links to actions which require 443# JavaScript to run (like 'blame_incremental'). Not enabled by 444# default. Project specific override is currently not supported. 445'javascript-actions'=> { 446'override'=>0, 447'default'=> [0]}, 448); 449 450sub gitweb_get_feature { 451my($name) =@_; 452return unlessexists$feature{$name}; 453my($sub,$override,@defaults) = ( 454$feature{$name}{'sub'}, 455$feature{$name}{'override'}, 456@{$feature{$name}{'default'}}); 457if(!$override) {return@defaults; } 458if(!defined$sub) { 459warn"feature$nameis not overridable"; 460return@defaults; 461} 462return$sub->(@defaults); 463} 464 465# A wrapper to check if a given feature is enabled. 466# With this, you can say 467# 468# my $bool_feat = gitweb_check_feature('bool_feat'); 469# gitweb_check_feature('bool_feat') or somecode; 470# 471# instead of 472# 473# my ($bool_feat) = gitweb_get_feature('bool_feat'); 474# (gitweb_get_feature('bool_feat'))[0] or somecode; 475# 476sub gitweb_check_feature { 477return(gitweb_get_feature(@_))[0]; 478} 479 480 481sub feature_bool { 482my$key=shift; 483my($val) = git_get_project_config($key,'--bool'); 484 485if(!defined$val) { 486return($_[0]); 487}elsif($valeq'true') { 488return(1); 489}elsif($valeq'false') { 490return(0); 491} 492} 493 494sub feature_snapshot { 495my(@fmts) =@_; 496 497my($val) = git_get_project_config('snapshot'); 498 499if($val) { 500@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 501} 502 503return@fmts; 504} 505 506sub feature_patches { 507my@val= (git_get_project_config('patches','--int')); 508 509if(@val) { 510return@val; 511} 512 513return($_[0]); 514} 515 516sub feature_avatar { 517my@val= (git_get_project_config('avatar')); 518 519return@val?@val:@_; 520} 521 522# checking HEAD file with -e is fragile if the repository was 523# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 524# and then pruned. 525sub check_head_link { 526my($dir) =@_; 527my$headfile="$dir/HEAD"; 528return((-e $headfile) || 529(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 530} 531 532sub check_export_ok { 533my($dir) =@_; 534return(check_head_link($dir) && 535(!$export_ok|| -e "$dir/$export_ok") && 536(!$export_auth_hook||$export_auth_hook->($dir))); 537} 538 539# process alternate names for backward compatibility 540# filter out unsupported (unknown) snapshot formats 541sub filter_snapshot_fmts { 542my@fmts=@_; 543 544@fmts=map{ 545exists$known_snapshot_format_aliases{$_} ? 546$known_snapshot_format_aliases{$_} :$_}@fmts; 547@fmts=grep{ 548exists$known_snapshot_formats{$_} && 549!$known_snapshot_formats{$_}{'disabled'}}@fmts; 550} 551 552our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 553if(-e $GITWEB_CONFIG) { 554do$GITWEB_CONFIG; 555}else{ 556our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 557do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 558} 559 560# Get loadavg of system, to compare against $maxload. 561# Currently it requires '/proc/loadavg' present to get loadavg; 562# if it is not present it returns 0, which means no load checking. 563sub get_loadavg { 564if( -e '/proc/loadavg'){ 565open my$fd,'<','/proc/loadavg' 566orreturn0; 567my@load=split(/\s+/,scalar<$fd>); 568close$fd; 569 570# The first three columns measure CPU and IO utilization of the last one, 571# five, and 10 minute periods. The fourth column shows the number of 572# currently running processes and the total number of processes in the m/n 573# format. The last column displays the last process ID used. 574return$load[0] ||0; 575} 576# additional checks for load average should go here for things that don't export 577# /proc/loadavg 578 579return0; 580} 581 582# version of the core git binary 583our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 584$number_of_git_cmds++; 585 586$projects_list||=$projectroot; 587 588if(defined$maxload&& get_loadavg() >$maxload) { 589 die_error(503,"The load average on the server is too high"); 590} 591 592# ====================================================================== 593# input validation and dispatch 594 595# input parameters can be collected from a variety of sources (presently, CGI 596# and PATH_INFO), so we define an %input_params hash that collects them all 597# together during validation: this allows subsequent uses (e.g. href()) to be 598# agnostic of the parameter origin 599 600our%input_params= (); 601 602# input parameters are stored with the long parameter name as key. This will 603# also be used in the href subroutine to convert parameters to their CGI 604# equivalent, and since the href() usage is the most frequent one, we store 605# the name -> CGI key mapping here, instead of the reverse. 606# 607# XXX: Warning: If you touch this, check the search form for updating, 608# too. 609 610our@cgi_param_mapping= ( 611 project =>"p", 612 action =>"a", 613 file_name =>"f", 614 file_parent =>"fp", 615 hash =>"h", 616 hash_parent =>"hp", 617 hash_base =>"hb", 618 hash_parent_base =>"hpb", 619 page =>"pg", 620 order =>"o", 621 searchtext =>"s", 622 searchtype =>"st", 623 snapshot_format =>"sf", 624 extra_options =>"opt", 625 search_use_regexp =>"sr", 626# this must be last entry (for manipulation from JavaScript) 627 javascript =>"js" 628); 629our%cgi_param_mapping=@cgi_param_mapping; 630 631# we will also need to know the possible actions, for validation 632our%actions= ( 633"blame"=> \&git_blame, 634"blame_incremental"=> \&git_blame_incremental, 635"blame_data"=> \&git_blame_data, 636"blobdiff"=> \&git_blobdiff, 637"blobdiff_plain"=> \&git_blobdiff_plain, 638"blob"=> \&git_blob, 639"blob_plain"=> \&git_blob_plain, 640"commitdiff"=> \&git_commitdiff, 641"commitdiff_plain"=> \&git_commitdiff_plain, 642"commit"=> \&git_commit, 643"forks"=> \&git_forks, 644"heads"=> \&git_heads, 645"history"=> \&git_history, 646"log"=> \&git_log, 647"patch"=> \&git_patch, 648"patches"=> \&git_patches, 649"rss"=> \&git_rss, 650"atom"=> \&git_atom, 651"search"=> \&git_search, 652"search_help"=> \&git_search_help, 653"shortlog"=> \&git_shortlog, 654"summary"=> \&git_summary, 655"tag"=> \&git_tag, 656"tags"=> \&git_tags, 657"tree"=> \&git_tree, 658"snapshot"=> \&git_snapshot, 659"object"=> \&git_object, 660# those below don't need $project 661"opml"=> \&git_opml, 662"project_list"=> \&git_project_list, 663"project_index"=> \&git_project_index, 664); 665 666# finally, we have the hash of allowed extra_options for the commands that 667# allow them 668our%allowed_options= ( 669"--no-merges"=> [qw(rss atom log shortlog history)], 670); 671 672# fill %input_params with the CGI parameters. All values except for 'opt' 673# should be single values, but opt can be an array. We should probably 674# build an array of parameters that can be multi-valued, but since for the time 675# being it's only this one, we just single it out 676while(my($name,$symbol) =each%cgi_param_mapping) { 677if($symboleq'opt') { 678$input_params{$name} = [$cgi->param($symbol) ]; 679}else{ 680$input_params{$name} =$cgi->param($symbol); 681} 682} 683 684# now read PATH_INFO and update the parameter list for missing parameters 685sub evaluate_path_info { 686return ifdefined$input_params{'project'}; 687return if!$path_info; 688$path_info=~ s,^/+,,; 689return if!$path_info; 690 691# find which part of PATH_INFO is project 692my$project=$path_info; 693$project=~ s,/+$,,; 694while($project&& !check_head_link("$projectroot/$project")) { 695$project=~ s,/*[^/]*$,,; 696} 697return unless$project; 698$input_params{'project'} =$project; 699 700# do not change any parameters if an action is given using the query string 701return if$input_params{'action'}; 702$path_info=~ s,^\Q$project\E/*,,; 703 704# next, check if we have an action 705my$action=$path_info; 706$action=~ s,/.*$,,; 707if(exists$actions{$action}) { 708$path_info=~ s,^$action/*,,; 709$input_params{'action'} =$action; 710} 711 712# list of actions that want hash_base instead of hash, but can have no 713# pathname (f) parameter 714my@wants_base= ( 715'tree', 716'history', 717); 718 719# we want to catch 720# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 721my($parentrefname,$parentpathname,$refname,$pathname) = 722($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 723 724# first, analyze the 'current' part 725if(defined$pathname) { 726# we got "branch:filename" or "branch:dir/" 727# we could use git_get_type(branch:pathname), but: 728# - it needs $git_dir 729# - it does a git() call 730# - the convention of terminating directories with a slash 731# makes it superfluous 732# - embedding the action in the PATH_INFO would make it even 733# more superfluous 734$pathname=~ s,^/+,,; 735if(!$pathname||substr($pathname, -1)eq"/") { 736$input_params{'action'} ||="tree"; 737$pathname=~ s,/$,,; 738}else{ 739# the default action depends on whether we had parent info 740# or not 741if($parentrefname) { 742$input_params{'action'} ||="blobdiff_plain"; 743}else{ 744$input_params{'action'} ||="blob_plain"; 745} 746} 747$input_params{'hash_base'} ||=$refname; 748$input_params{'file_name'} ||=$pathname; 749}elsif(defined$refname) { 750# we got "branch". In this case we have to choose if we have to 751# set hash or hash_base. 752# 753# Most of the actions without a pathname only want hash to be 754# set, except for the ones specified in @wants_base that want 755# hash_base instead. It should also be noted that hand-crafted 756# links having 'history' as an action and no pathname or hash 757# set will fail, but that happens regardless of PATH_INFO. 758$input_params{'action'} ||="shortlog"; 759if(grep{$_eq$input_params{'action'} }@wants_base) { 760$input_params{'hash_base'} ||=$refname; 761}else{ 762$input_params{'hash'} ||=$refname; 763} 764} 765 766# next, handle the 'parent' part, if present 767if(defined$parentrefname) { 768# a missing pathspec defaults to the 'current' filename, allowing e.g. 769# someproject/blobdiff/oldrev..newrev:/filename 770if($parentpathname) { 771$parentpathname=~ s,^/+,,; 772$parentpathname=~ s,/$,,; 773$input_params{'file_parent'} ||=$parentpathname; 774}else{ 775$input_params{'file_parent'} ||=$input_params{'file_name'}; 776} 777# we assume that hash_parent_base is wanted if a path was specified, 778# or if the action wants hash_base instead of hash 779if(defined$input_params{'file_parent'} || 780grep{$_eq$input_params{'action'} }@wants_base) { 781$input_params{'hash_parent_base'} ||=$parentrefname; 782}else{ 783$input_params{'hash_parent'} ||=$parentrefname; 784} 785} 786 787# for the snapshot action, we allow URLs in the form 788# $project/snapshot/$hash.ext 789# where .ext determines the snapshot and gets removed from the 790# passed $refname to provide the $hash. 791# 792# To be able to tell that $refname includes the format extension, we 793# require the following two conditions to be satisfied: 794# - the hash input parameter MUST have been set from the $refname part 795# of the URL (i.e. they must be equal) 796# - the snapshot format MUST NOT have been defined already (e.g. from 797# CGI parameter sf) 798# It's also useless to try any matching unless $refname has a dot, 799# so we check for that too 800if(defined$input_params{'action'} && 801$input_params{'action'}eq'snapshot'&& 802defined$refname&&index($refname,'.') != -1&& 803$refnameeq$input_params{'hash'} && 804!defined$input_params{'snapshot_format'}) { 805# We loop over the known snapshot formats, checking for 806# extensions. Allowed extensions are both the defined suffix 807# (which includes the initial dot already) and the snapshot 808# format key itself, with a prepended dot 809while(my($fmt,$opt) =each%known_snapshot_formats) { 810my$hash=$refname; 811unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 812next; 813} 814my$sfx=$1; 815# a valid suffix was found, so set the snapshot format 816# and reset the hash parameter 817$input_params{'snapshot_format'} =$fmt; 818$input_params{'hash'} =$hash; 819# we also set the format suffix to the one requested 820# in the URL: this way a request for e.g. .tgz returns 821# a .tgz instead of a .tar.gz 822$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 823last; 824} 825} 826} 827evaluate_path_info(); 828 829our$action=$input_params{'action'}; 830if(defined$action) { 831if(!validate_action($action)) { 832 die_error(400,"Invalid action parameter"); 833} 834} 835 836# parameters which are pathnames 837our$project=$input_params{'project'}; 838if(defined$project) { 839if(!validate_project($project)) { 840undef$project; 841 die_error(404,"No such project"); 842} 843} 844 845our$file_name=$input_params{'file_name'}; 846if(defined$file_name) { 847if(!validate_pathname($file_name)) { 848 die_error(400,"Invalid file parameter"); 849} 850} 851 852our$file_parent=$input_params{'file_parent'}; 853if(defined$file_parent) { 854if(!validate_pathname($file_parent)) { 855 die_error(400,"Invalid file parent parameter"); 856} 857} 858 859# parameters which are refnames 860our$hash=$input_params{'hash'}; 861if(defined$hash) { 862if(!validate_refname($hash)) { 863 die_error(400,"Invalid hash parameter"); 864} 865} 866 867our$hash_parent=$input_params{'hash_parent'}; 868if(defined$hash_parent) { 869if(!validate_refname($hash_parent)) { 870 die_error(400,"Invalid hash parent parameter"); 871} 872} 873 874our$hash_base=$input_params{'hash_base'}; 875if(defined$hash_base) { 876if(!validate_refname($hash_base)) { 877 die_error(400,"Invalid hash base parameter"); 878} 879} 880 881our@extra_options= @{$input_params{'extra_options'}}; 882# @extra_options is always defined, since it can only be (currently) set from 883# CGI, and $cgi->param() returns the empty array in array context if the param 884# is not set 885foreachmy$opt(@extra_options) { 886if(not exists$allowed_options{$opt}) { 887 die_error(400,"Invalid option parameter"); 888} 889if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 890 die_error(400,"Invalid option parameter for this action"); 891} 892} 893 894our$hash_parent_base=$input_params{'hash_parent_base'}; 895if(defined$hash_parent_base) { 896if(!validate_refname($hash_parent_base)) { 897 die_error(400,"Invalid hash parent base parameter"); 898} 899} 900 901# other parameters 902our$page=$input_params{'page'}; 903if(defined$page) { 904if($page=~m/[^0-9]/) { 905 die_error(400,"Invalid page parameter"); 906} 907} 908 909our$searchtype=$input_params{'searchtype'}; 910if(defined$searchtype) { 911if($searchtype=~m/[^a-z]/) { 912 die_error(400,"Invalid searchtype parameter"); 913} 914} 915 916our$search_use_regexp=$input_params{'search_use_regexp'}; 917 918our$searchtext=$input_params{'searchtext'}; 919our$search_regexp; 920if(defined$searchtext) { 921if(length($searchtext) <2) { 922 die_error(403,"At least two characters are required for search parameter"); 923} 924$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 925} 926 927# path to the current git repository 928our$git_dir; 929$git_dir="$projectroot/$project"if$project; 930 931# list of supported snapshot formats 932our@snapshot_fmts= gitweb_get_feature('snapshot'); 933@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 934 935# check that the avatar feature is set to a known provider name, 936# and for each provider check if the dependencies are satisfied. 937# if the provider name is invalid or the dependencies are not met, 938# reset $git_avatar to the empty string. 939our($git_avatar) = gitweb_get_feature('avatar'); 940if($git_avatareq'gravatar') { 941$git_avatar=''unless(eval{require Digest::MD5;1; }); 942}elsif($git_avatareq'picon') { 943# no dependencies 944}else{ 945$git_avatar=''; 946} 947 948# dispatch 949if(!defined$action) { 950if(defined$hash) { 951$action= git_get_type($hash); 952}elsif(defined$hash_base&&defined$file_name) { 953$action= git_get_type("$hash_base:$file_name"); 954}elsif(defined$project) { 955$action='summary'; 956}else{ 957$action='project_list'; 958} 959} 960if(!defined($actions{$action})) { 961 die_error(400,"Unknown action"); 962} 963if($action!~m/^(?:opml|project_list|project_index)$/&& 964!$project) { 965 die_error(400,"Project needed"); 966} 967$actions{$action}->(); 968exit; 969 970## ====================================================================== 971## action links 972 973sub href { 974my%params=@_; 975# default is to use -absolute url() i.e. $my_uri 976my$href=$params{-full} ?$my_url:$my_uri; 977 978$params{'project'} =$projectunlessexists$params{'project'}; 979 980if($params{-replay}) { 981while(my($name,$symbol) =each%cgi_param_mapping) { 982if(!exists$params{$name}) { 983$params{$name} =$input_params{$name}; 984} 985} 986} 987 988my$use_pathinfo= gitweb_check_feature('pathinfo'); 989if($use_pathinfoand defined$params{'project'}) { 990# try to put as many parameters as possible in PATH_INFO: 991# - project name 992# - action 993# - hash_parent or hash_parent_base:/file_parent 994# - hash or hash_base:/filename 995# - the snapshot_format as an appropriate suffix 996 997# When the script is the root DirectoryIndex for the domain, 998# $href here would be something like http://gitweb.example.com/ 999# Thus, we strip any trailing / from $href, to spare us double1000# slashes in the final URL1001$href=~ s,/$,,;10021003# Then add the project name, if present1004$href.="/".esc_url($params{'project'});1005delete$params{'project'};10061007# since we destructively absorb parameters, we keep this1008# boolean that remembers if we're handling a snapshot1009my$is_snapshot=$params{'action'}eq'snapshot';10101011# Summary just uses the project path URL, any other action is1012# added to the URL1013if(defined$params{'action'}) {1014$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1015delete$params{'action'};1016}10171018# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1019# stripping nonexistent or useless pieces1020$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1021||$params{'hash_parent'} ||$params{'hash'});1022if(defined$params{'hash_base'}) {1023if(defined$params{'hash_parent_base'}) {1024$href.= esc_url($params{'hash_parent_base'});1025# skip the file_parent if it's the same as the file_name1026if(defined$params{'file_parent'}) {1027if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1028delete$params{'file_parent'};1029}elsif($params{'file_parent'} !~/\.\./) {1030$href.=":/".esc_url($params{'file_parent'});1031delete$params{'file_parent'};1032}1033}1034$href.="..";1035delete$params{'hash_parent'};1036delete$params{'hash_parent_base'};1037}elsif(defined$params{'hash_parent'}) {1038$href.= esc_url($params{'hash_parent'})."..";1039delete$params{'hash_parent'};1040}10411042$href.= esc_url($params{'hash_base'});1043if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1044$href.=":/".esc_url($params{'file_name'});1045delete$params{'file_name'};1046}1047delete$params{'hash'};1048delete$params{'hash_base'};1049}elsif(defined$params{'hash'}) {1050$href.= esc_url($params{'hash'});1051delete$params{'hash'};1052}10531054# If the action was a snapshot, we can absorb the1055# snapshot_format parameter too1056if($is_snapshot) {1057my$fmt=$params{'snapshot_format'};1058# snapshot_format should always be defined when href()1059# is called, but just in case some code forgets, we1060# fall back to the default1061$fmt||=$snapshot_fmts[0];1062$href.=$known_snapshot_formats{$fmt}{'suffix'};1063delete$params{'snapshot_format'};1064}1065}10661067# now encode the parameters explicitly1068my@result= ();1069for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1070my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1071if(defined$params{$name}) {1072if(ref($params{$name})eq"ARRAY") {1073foreachmy$par(@{$params{$name}}) {1074push@result,$symbol."=". esc_param($par);1075}1076}else{1077push@result,$symbol."=". esc_param($params{$name});1078}1079}1080}1081$href.="?".join(';',@result)ifscalar@result;10821083return$href;1084}108510861087## ======================================================================1088## validation, quoting/unquoting and escaping10891090sub validate_action {1091my$input=shift||returnundef;1092returnundefunlessexists$actions{$input};1093return$input;1094}10951096sub validate_project {1097my$input=shift||returnundef;1098if(!validate_pathname($input) ||1099!(-d "$projectroot/$input") ||1100!check_export_ok("$projectroot/$input") ||1101($strict_export&& !project_in_list($input))) {1102returnundef;1103}else{1104return$input;1105}1106}11071108sub validate_pathname {1109my$input=shift||returnundef;11101111# no '.' or '..' as elements of path, i.e. no '.' nor '..'1112# at the beginning, at the end, and between slashes.1113# also this catches doubled slashes1114if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1115returnundef;1116}1117# no null characters1118if($input=~m!\0!) {1119returnundef;1120}1121return$input;1122}11231124sub validate_refname {1125my$input=shift||returnundef;11261127# textual hashes are O.K.1128if($input=~m/^[0-9a-fA-F]{40}$/) {1129return$input;1130}1131# it must be correct pathname1132$input= validate_pathname($input)1133orreturnundef;1134# restrictions on ref name according to git-check-ref-format1135if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1136returnundef;1137}1138return$input;1139}11401141# decode sequences of octets in utf8 into Perl's internal form,1142# which is utf-8 with utf8 flag set if needed. gitweb writes out1143# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1144sub to_utf8 {1145my$str=shift;1146if(utf8::valid($str)) {1147 utf8::decode($str);1148return$str;1149}else{1150return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1151}1152}11531154# quote unsafe chars, but keep the slash, even when it's not1155# correct, but quoted slashes look too horrible in bookmarks1156sub esc_param {1157my$str=shift;1158$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1159$str=~s/ /\+/g;1160return$str;1161}11621163# quote unsafe chars in whole URL, so some charactrs cannot be quoted1164sub esc_url {1165my$str=shift;1166$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1167$str=~s/\+/%2B/g;1168$str=~s/ /\+/g;1169return$str;1170}11711172# replace invalid utf8 character with SUBSTITUTION sequence1173sub esc_html {1174my$str=shift;1175my%opts=@_;11761177$str= to_utf8($str);1178$str=$cgi->escapeHTML($str);1179if($opts{'-nbsp'}) {1180$str=~s/ / /g;1181}1182$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1183return$str;1184}11851186# quote control characters and escape filename to HTML1187sub esc_path {1188my$str=shift;1189my%opts=@_;11901191$str= to_utf8($str);1192$str=$cgi->escapeHTML($str);1193if($opts{'-nbsp'}) {1194$str=~s/ / /g;1195}1196$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1197return$str;1198}11991200# Make control characters "printable", using character escape codes (CEC)1201sub quot_cec {1202my$cntrl=shift;1203my%opts=@_;1204my%es= (# character escape codes, aka escape sequences1205"\t"=>'\t',# tab (HT)1206"\n"=>'\n',# line feed (LF)1207"\r"=>'\r',# carrige return (CR)1208"\f"=>'\f',# form feed (FF)1209"\b"=>'\b',# backspace (BS)1210"\a"=>'\a',# alarm (bell) (BEL)1211"\e"=>'\e',# escape (ESC)1212"\013"=>'\v',# vertical tab (VT)1213"\000"=>'\0',# nul character (NUL)1214);1215my$chr= ( (exists$es{$cntrl})1216?$es{$cntrl}1217:sprintf('\%2x',ord($cntrl)) );1218if($opts{-nohtml}) {1219return$chr;1220}else{1221return"<span class=\"cntrl\">$chr</span>";1222}1223}12241225# Alternatively use unicode control pictures codepoints,1226# Unicode "printable representation" (PR)1227sub quot_upr {1228my$cntrl=shift;1229my%opts=@_;12301231my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1232if($opts{-nohtml}) {1233return$chr;1234}else{1235return"<span class=\"cntrl\">$chr</span>";1236}1237}12381239# git may return quoted and escaped filenames1240sub unquote {1241my$str=shift;12421243sub unq {1244my$seq=shift;1245my%es= (# character escape codes, aka escape sequences1246't'=>"\t",# tab (HT, TAB)1247'n'=>"\n",# newline (NL)1248'r'=>"\r",# return (CR)1249'f'=>"\f",# form feed (FF)1250'b'=>"\b",# backspace (BS)1251'a'=>"\a",# alarm (bell) (BEL)1252'e'=>"\e",# escape (ESC)1253'v'=>"\013",# vertical tab (VT)1254);12551256if($seq=~m/^[0-7]{1,3}$/) {1257# octal char sequence1258returnchr(oct($seq));1259}elsif(exists$es{$seq}) {1260# C escape sequence, aka character escape code1261return$es{$seq};1262}1263# quoted ordinary character1264return$seq;1265}12661267if($str=~m/^"(.*)"$/) {1268# needs unquoting1269$str=$1;1270$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1271}1272return$str;1273}12741275# escape tabs (convert tabs to spaces)1276sub untabify {1277my$line=shift;12781279while((my$pos=index($line,"\t")) != -1) {1280if(my$count= (8- ($pos%8))) {1281my$spaces=' ' x $count;1282$line=~s/\t/$spaces/;1283}1284}12851286return$line;1287}12881289sub project_in_list {1290my$project=shift;1291my@list= git_get_projects_list();1292return@list&&scalar(grep{$_->{'path'}eq$project}@list);1293}12941295## ----------------------------------------------------------------------1296## HTML aware string manipulation12971298# Try to chop given string on a word boundary between position1299# $len and $len+$add_len. If there is no word boundary there,1300# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1301# (marking chopped part) would be longer than given string.1302sub chop_str {1303my$str=shift;1304my$len=shift;1305my$add_len=shift||10;1306my$where=shift||'right';# 'left' | 'center' | 'right'13071308# Make sure perl knows it is utf8 encoded so we don't1309# cut in the middle of a utf8 multibyte char.1310$str= to_utf8($str);13111312# allow only $len chars, but don't cut a word if it would fit in $add_len1313# if it doesn't fit, cut it if it's still longer than the dots we would add1314# remove chopped character entities entirely13151316# when chopping in the middle, distribute $len into left and right part1317# return early if chopping wouldn't make string shorter1318if($whereeq'center') {1319return$strif($len+5>=length($str));# filler is length 51320$len=int($len/2);1321}else{1322return$strif($len+4>=length($str));# filler is length 41323}13241325# regexps: ending and beginning with word part up to $add_len1326my$endre=qr/.{$len}\w{0,$add_len}/;1327my$begre=qr/\w{0,$add_len}.{$len}/;13281329if($whereeq'left') {1330$str=~m/^(.*?)($begre)$/;1331my($lead,$body) = ($1,$2);1332if(length($lead) >4) {1333$lead=" ...";1334}1335return"$lead$body";13361337}elsif($whereeq'center') {1338$str=~m/^($endre)(.*)$/;1339my($left,$str) = ($1,$2);1340$str=~m/^(.*?)($begre)$/;1341my($mid,$right) = ($1,$2);1342if(length($mid) >5) {1343$mid=" ... ";1344}1345return"$left$mid$right";13461347}else{1348$str=~m/^($endre)(.*)$/;1349my$body=$1;1350my$tail=$2;1351if(length($tail) >4) {1352$tail="... ";1353}1354return"$body$tail";1355}1356}13571358# takes the same arguments as chop_str, but also wraps a <span> around the1359# result with a title attribute if it does get chopped. Additionally, the1360# string is HTML-escaped.1361sub chop_and_escape_str {1362my($str) =@_;13631364my$chopped= chop_str(@_);1365if($choppedeq$str) {1366return esc_html($chopped);1367}else{1368$str=~s/[[:cntrl:]]/?/g;1369return$cgi->span({-title=>$str}, esc_html($chopped));1370}1371}13721373## ----------------------------------------------------------------------1374## functions returning short strings13751376# CSS class for given age value (in seconds)1377sub age_class {1378my$age=shift;13791380if(!defined$age) {1381return"noage";1382}elsif($age<60*60*2) {1383return"age0";1384}elsif($age<60*60*24*2) {1385return"age1";1386}else{1387return"age2";1388}1389}13901391# convert age in seconds to "nn units ago" string1392sub age_string {1393my$age=shift;1394my$age_str;13951396if($age>60*60*24*365*2) {1397$age_str= (int$age/60/60/24/365);1398$age_str.=" years ago";1399}elsif($age>60*60*24*(365/12)*2) {1400$age_str=int$age/60/60/24/(365/12);1401$age_str.=" months ago";1402}elsif($age>60*60*24*7*2) {1403$age_str=int$age/60/60/24/7;1404$age_str.=" weeks ago";1405}elsif($age>60*60*24*2) {1406$age_str=int$age/60/60/24;1407$age_str.=" days ago";1408}elsif($age>60*60*2) {1409$age_str=int$age/60/60;1410$age_str.=" hours ago";1411}elsif($age>60*2) {1412$age_str=int$age/60;1413$age_str.=" min ago";1414}elsif($age>2) {1415$age_str=int$age;1416$age_str.=" sec ago";1417}else{1418$age_str.=" right now";1419}1420return$age_str;1421}14221423useconstant{1424 S_IFINVALID =>0030000,1425 S_IFGITLINK =>0160000,1426};14271428# submodule/subproject, a commit object reference1429sub S_ISGITLINK {1430my$mode=shift;14311432return(($mode& S_IFMT) == S_IFGITLINK)1433}14341435# convert file mode in octal to symbolic file mode string1436sub mode_str {1437my$mode=oct shift;14381439if(S_ISGITLINK($mode)) {1440return'm---------';1441}elsif(S_ISDIR($mode& S_IFMT)) {1442return'drwxr-xr-x';1443}elsif(S_ISLNK($mode)) {1444return'lrwxrwxrwx';1445}elsif(S_ISREG($mode)) {1446# git cares only about the executable bit1447if($mode& S_IXUSR) {1448return'-rwxr-xr-x';1449}else{1450return'-rw-r--r--';1451};1452}else{1453return'----------';1454}1455}14561457# convert file mode in octal to file type string1458sub file_type {1459my$mode=shift;14601461if($mode!~m/^[0-7]+$/) {1462return$mode;1463}else{1464$mode=oct$mode;1465}14661467if(S_ISGITLINK($mode)) {1468return"submodule";1469}elsif(S_ISDIR($mode& S_IFMT)) {1470return"directory";1471}elsif(S_ISLNK($mode)) {1472return"symlink";1473}elsif(S_ISREG($mode)) {1474return"file";1475}else{1476return"unknown";1477}1478}14791480# convert file mode in octal to file type description string1481sub file_type_long {1482my$mode=shift;14831484if($mode!~m/^[0-7]+$/) {1485return$mode;1486}else{1487$mode=oct$mode;1488}14891490if(S_ISGITLINK($mode)) {1491return"submodule";1492}elsif(S_ISDIR($mode& S_IFMT)) {1493return"directory";1494}elsif(S_ISLNK($mode)) {1495return"symlink";1496}elsif(S_ISREG($mode)) {1497if($mode& S_IXUSR) {1498return"executable";1499}else{1500return"file";1501};1502}else{1503return"unknown";1504}1505}150615071508## ----------------------------------------------------------------------1509## functions returning short HTML fragments, or transforming HTML fragments1510## which don't belong to other sections15111512# format line of commit message.1513sub format_log_line_html {1514my$line=shift;15151516$line= esc_html($line, -nbsp=>1);1517$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1518$cgi->a({-href => href(action=>"object", hash=>$1),1519-class=>"text"},$1);1520}eg;15211522return$line;1523}15241525# format marker of refs pointing to given object15261527# the destination action is chosen based on object type and current context:1528# - for annotated tags, we choose the tag view unless it's the current view1529# already, in which case we go to shortlog view1530# - for other refs, we keep the current view if we're in history, shortlog or1531# log view, and select shortlog otherwise1532sub format_ref_marker {1533my($refs,$id) =@_;1534my$markers='';15351536if(defined$refs->{$id}) {1537foreachmy$ref(@{$refs->{$id}}) {1538# this code exploits the fact that non-lightweight tags are the1539# only indirect objects, and that they are the only objects for which1540# we want to use tag instead of shortlog as action1541my($type,$name) =qw();1542my$indirect= ($ref=~s/\^\{\}$//);1543# e.g. tags/v2.6.11 or heads/next1544if($ref=~m!^(.*?)s?/(.*)$!) {1545$type=$1;1546$name=$2;1547}else{1548$type="ref";1549$name=$ref;1550}15511552my$class=$type;1553$class.=" indirect"if$indirect;15541555my$dest_action="shortlog";15561557if($indirect) {1558$dest_action="tag"unless$actioneq"tag";1559}elsif($action=~/^(history|(short)?log)$/) {1560$dest_action=$action;1561}15621563my$dest="";1564$dest.="refs/"unless$ref=~ m!^refs/!;1565$dest.=$ref;15661567my$link=$cgi->a({1568-href => href(1569 action=>$dest_action,1570 hash=>$dest1571)},$name);15721573$markers.=" <span class=\"$class\"title=\"$ref\">".1574$link."</span>";1575}1576}15771578if($markers) {1579return' <span class="refs">'.$markers.'</span>';1580}else{1581return"";1582}1583}15841585# format, perhaps shortened and with markers, title line1586sub format_subject_html {1587my($long,$short,$href,$extra) =@_;1588$extra=''unlessdefined($extra);15891590if(length($short) <length($long)) {1591$long=~s/[[:cntrl:]]/?/g;1592return$cgi->a({-href =>$href, -class=>"list subject",1593-title => to_utf8($long)},1594 esc_html($short)) .$extra;1595}else{1596return$cgi->a({-href =>$href, -class=>"list subject"},1597 esc_html($long)) .$extra;1598}1599}16001601# Rather than recomputing the url for an email multiple times, we cache it1602# after the first hit. This gives a visible benefit in views where the avatar1603# for the same email is used repeatedly (e.g. shortlog).1604# The cache is shared by all avatar engines (currently gravatar only), which1605# are free to use it as preferred. Since only one avatar engine is used for any1606# given page, there's no risk for cache conflicts.1607our%avatar_cache= ();16081609# Compute the picon url for a given email, by using the picon search service over at1610# http://www.cs.indiana.edu/picons/search.html1611sub picon_url {1612my$email=lc shift;1613if(!$avatar_cache{$email}) {1614my($user,$domain) =split('@',$email);1615$avatar_cache{$email} =1616"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1617"$domain/$user/".1618"users+domains+unknown/up/single";1619}1620return$avatar_cache{$email};1621}16221623# Compute the gravatar url for a given email, if it's not in the cache already.1624# Gravatar stores only the part of the URL before the size, since that's the1625# one computationally more expensive. This also allows reuse of the cache for1626# different sizes (for this particular engine).1627sub gravatar_url {1628my$email=lc shift;1629my$size=shift;1630$avatar_cache{$email} ||=1631"http://www.gravatar.com/avatar/".1632 Digest::MD5::md5_hex($email) ."?s=";1633return$avatar_cache{$email} .$size;1634}16351636# Insert an avatar for the given $email at the given $size if the feature1637# is enabled.1638sub git_get_avatar {1639my($email,%opts) =@_;1640my$pre_white= ($opts{-pad_before} ?" ":"");1641my$post_white= ($opts{-pad_after} ?" ":"");1642$opts{-size} ||='default';1643my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1644my$url="";1645if($git_avatareq'gravatar') {1646$url= gravatar_url($email,$size);1647}elsif($git_avatareq'picon') {1648$url= picon_url($email);1649}1650# Other providers can be added by extending the if chain, defining $url1651# as needed. If no variant puts something in $url, we assume avatars1652# are completely disabled/unavailable.1653if($url) {1654return$pre_white.1655"<img width=\"$size\"".1656"class=\"avatar\"".1657"src=\"$url\"".1658"alt=\"\"".1659"/>".$post_white;1660}else{1661return"";1662}1663}16641665sub format_search_author {1666my($author,$searchtype,$displaytext) =@_;1667my$have_search= gitweb_check_feature('search');16681669if($have_search) {1670my$performed="";1671if($searchtypeeq'author') {1672$performed="authored";1673}elsif($searchtypeeq'committer') {1674$performed="committed";1675}16761677return$cgi->a({-href => href(action=>"search", hash=>$hash,1678 searchtext=>$author,1679 searchtype=>$searchtype),class=>"list",1680 title=>"Search for commits$performedby$author"},1681$displaytext);16821683}else{1684return$displaytext;1685}1686}16871688# format the author name of the given commit with the given tag1689# the author name is chopped and escaped according to the other1690# optional parameters (see chop_str).1691sub format_author_html {1692my$tag=shift;1693my$co=shift;1694my$author= chop_and_escape_str($co->{'author_name'},@_);1695return"<$tagclass=\"author\">".1696 format_search_author($co->{'author_name'},"author",1697 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1698$author) .1699"</$tag>";1700}17011702# format git diff header line, i.e. "diff --(git|combined|cc) ..."1703sub format_git_diff_header_line {1704my$line=shift;1705my$diffinfo=shift;1706my($from,$to) =@_;17071708if($diffinfo->{'nparents'}) {1709# combined diff1710$line=~s!^(diff (.*?) )"?.*$!$1!;1711if($to->{'href'}) {1712$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1713 esc_path($to->{'file'}));1714}else{# file was deleted (no href)1715$line.= esc_path($to->{'file'});1716}1717}else{1718# "ordinary" diff1719$line=~s!^(diff (.*?) )"?a/.*$!$1!;1720if($from->{'href'}) {1721$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1722'a/'. esc_path($from->{'file'}));1723}else{# file was added (no href)1724$line.='a/'. esc_path($from->{'file'});1725}1726$line.=' ';1727if($to->{'href'}) {1728$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1729'b/'. esc_path($to->{'file'}));1730}else{# file was deleted1731$line.='b/'. esc_path($to->{'file'});1732}1733}17341735return"<div class=\"diff header\">$line</div>\n";1736}17371738# format extended diff header line, before patch itself1739sub format_extended_diff_header_line {1740my$line=shift;1741my$diffinfo=shift;1742my($from,$to) =@_;17431744# match <path>1745if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1746$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1747 esc_path($from->{'file'}));1748}1749if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1750$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1751 esc_path($to->{'file'}));1752}1753# match single <mode>1754if($line=~m/\s(\d{6})$/) {1755$line.='<span class="info"> ('.1756 file_type_long($1) .1757')</span>';1758}1759# match <hash>1760if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1761# can match only for combined diff1762$line='index ';1763for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1764if($from->{'href'}[$i]) {1765$line.=$cgi->a({-href=>$from->{'href'}[$i],1766-class=>"hash"},1767substr($diffinfo->{'from_id'}[$i],0,7));1768}else{1769$line.='0' x 7;1770}1771# separator1772$line.=','if($i<$diffinfo->{'nparents'} -1);1773}1774$line.='..';1775if($to->{'href'}) {1776$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1777substr($diffinfo->{'to_id'},0,7));1778}else{1779$line.='0' x 7;1780}17811782}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1783# can match only for ordinary diff1784my($from_link,$to_link);1785if($from->{'href'}) {1786$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1787substr($diffinfo->{'from_id'},0,7));1788}else{1789$from_link='0' x 7;1790}1791if($to->{'href'}) {1792$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1793substr($diffinfo->{'to_id'},0,7));1794}else{1795$to_link='0' x 7;1796}1797my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1798$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1799}18001801return$line."<br/>\n";1802}18031804# format from-file/to-file diff header1805sub format_diff_from_to_header {1806my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1807my$line;1808my$result='';18091810$line=$from_line;1811#assert($line =~ m/^---/) if DEBUG;1812# no extra formatting for "^--- /dev/null"1813if(!$diffinfo->{'nparents'}) {1814# ordinary (single parent) diff1815if($line=~m!^--- "?a/!) {1816if($from->{'href'}) {1817$line='--- a/'.1818$cgi->a({-href=>$from->{'href'}, -class=>"path"},1819 esc_path($from->{'file'}));1820}else{1821$line='--- a/'.1822 esc_path($from->{'file'});1823}1824}1825$result.= qq!<div class="diff from_file">$line</div>\n!;18261827}else{1828# combined diff (merge commit)1829for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1830if($from->{'href'}[$i]) {1831$line='--- '.1832$cgi->a({-href=>href(action=>"blobdiff",1833 hash_parent=>$diffinfo->{'from_id'}[$i],1834 hash_parent_base=>$parents[$i],1835 file_parent=>$from->{'file'}[$i],1836 hash=>$diffinfo->{'to_id'},1837 hash_base=>$hash,1838 file_name=>$to->{'file'}),1839-class=>"path",1840-title=>"diff". ($i+1)},1841$i+1) .1842'/'.1843$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1844 esc_path($from->{'file'}[$i]));1845}else{1846$line='--- /dev/null';1847}1848$result.= qq!<div class="diff from_file">$line</div>\n!;1849}1850}18511852$line=$to_line;1853#assert($line =~ m/^\+\+\+/) if DEBUG;1854# no extra formatting for "^+++ /dev/null"1855if($line=~m!^\+\+\+ "?b/!) {1856if($to->{'href'}) {1857$line='+++ b/'.1858$cgi->a({-href=>$to->{'href'}, -class=>"path"},1859 esc_path($to->{'file'}));1860}else{1861$line='+++ b/'.1862 esc_path($to->{'file'});1863}1864}1865$result.= qq!<div class="diff to_file">$line</div>\n!;18661867return$result;1868}18691870# create note for patch simplified by combined diff1871sub format_diff_cc_simplified {1872my($diffinfo,@parents) =@_;1873my$result='';18741875$result.="<div class=\"diff header\">".1876"diff --cc ";1877if(!is_deleted($diffinfo)) {1878$result.=$cgi->a({-href => href(action=>"blob",1879 hash_base=>$hash,1880 hash=>$diffinfo->{'to_id'},1881 file_name=>$diffinfo->{'to_file'}),1882-class=>"path"},1883 esc_path($diffinfo->{'to_file'}));1884}else{1885$result.= esc_path($diffinfo->{'to_file'});1886}1887$result.="</div>\n".# class="diff header"1888"<div class=\"diff nodifferences\">".1889"Simple merge".1890"</div>\n";# class="diff nodifferences"18911892return$result;1893}18941895# format patch (diff) line (not to be used for diff headers)1896sub format_diff_line {1897my$line=shift;1898my($from,$to) =@_;1899my$diff_class="";19001901chomp$line;19021903if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1904# combined diff1905my$prefix=substr($line,0,scalar@{$from->{'href'}});1906if($line=~m/^\@{3}/) {1907$diff_class=" chunk_header";1908}elsif($line=~m/^\\/) {1909$diff_class=" incomplete";1910}elsif($prefix=~tr/+/+/) {1911$diff_class=" add";1912}elsif($prefix=~tr/-/-/) {1913$diff_class=" rem";1914}1915}else{1916# assume ordinary diff1917my$char=substr($line,0,1);1918if($chareq'+') {1919$diff_class=" add";1920}elsif($chareq'-') {1921$diff_class=" rem";1922}elsif($chareq'@') {1923$diff_class=" chunk_header";1924}elsif($chareq"\\") {1925$diff_class=" incomplete";1926}1927}1928$line= untabify($line);1929if($from&&$to&&$line=~m/^\@{2} /) {1930my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1931$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;19321933$from_lines=0unlessdefined$from_lines;1934$to_lines=0unlessdefined$to_lines;19351936if($from->{'href'}) {1937$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1938-class=>"list"},$from_text);1939}1940if($to->{'href'}) {1941$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1942-class=>"list"},$to_text);1943}1944$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1945"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1946return"<div class=\"diff$diff_class\">$line</div>\n";1947}elsif($from&&$to&&$line=~m/^\@{3}/) {1948my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1949my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);19501951@from_text=split(' ',$ranges);1952for(my$i=0;$i<@from_text; ++$i) {1953($from_start[$i],$from_nlines[$i]) =1954(split(',',substr($from_text[$i],1)),0);1955}19561957$to_text=pop@from_text;1958$to_start=pop@from_start;1959$to_nlines=pop@from_nlines;19601961$line="<span class=\"chunk_info\">$prefix";1962for(my$i=0;$i<@from_text; ++$i) {1963if($from->{'href'}[$i]) {1964$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1965-class=>"list"},$from_text[$i]);1966}else{1967$line.=$from_text[$i];1968}1969$line.=" ";1970}1971if($to->{'href'}) {1972$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1973-class=>"list"},$to_text);1974}else{1975$line.=$to_text;1976}1977$line.="$prefix</span>".1978"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1979return"<div class=\"diff$diff_class\">$line</div>\n";1980}1981return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1982}19831984# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1985# linked. Pass the hash of the tree/commit to snapshot.1986sub format_snapshot_links {1987my($hash) =@_;1988my$num_fmts=@snapshot_fmts;1989if($num_fmts>1) {1990# A parenthesized list of links bearing format names.1991# e.g. "snapshot (_tar.gz_ _zip_)"1992return"snapshot (".join(' ',map1993$cgi->a({1994-href => href(1995 action=>"snapshot",1996 hash=>$hash,1997 snapshot_format=>$_1998)1999},$known_snapshot_formats{$_}{'display'})2000,@snapshot_fmts) .")";2001}elsif($num_fmts==1) {2002# A single "snapshot" link whose tooltip bears the format name.2003# i.e. "_snapshot_"2004my($fmt) =@snapshot_fmts;2005return2006$cgi->a({2007-href => href(2008 action=>"snapshot",2009 hash=>$hash,2010 snapshot_format=>$fmt2011),2012-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2013},"snapshot");2014}else{# $num_fmts == 02015returnundef;2016}2017}20182019## ......................................................................2020## functions returning values to be passed, perhaps after some2021## transformation, to other functions; e.g. returning arguments to href()20222023# returns hash to be passed to href to generate gitweb URL2024# in -title key it returns description of link2025sub get_feed_info {2026my$format=shift||'Atom';2027my%res= (action =>lc($format));20282029# feed links are possible only for project views2030return unless(defined$project);2031# some views should link to OPML, or to generic project feed,2032# or don't have specific feed yet (so they should use generic)2033return if($action=~/^(?:tags|heads|forks|tag|search)$/x);20342035my$branch;2036# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2037# from tag links; this also makes possible to detect branch links2038if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2039(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2040$branch=$1;2041}2042# find log type for feed description (title)2043my$type='log';2044if(defined$file_name) {2045$type="history of$file_name";2046$type.="/"if($actioneq'tree');2047$type.=" on '$branch'"if(defined$branch);2048}else{2049$type="log of$branch"if(defined$branch);2050}20512052$res{-title} =$type;2053$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2054$res{'file_name'} =$file_name;20552056return%res;2057}20582059## ----------------------------------------------------------------------2060## git utility subroutines, invoking git commands20612062# returns path to the core git executable and the --git-dir parameter as list2063sub git_cmd {2064$number_of_git_cmds++;2065return$GIT,'--git-dir='.$git_dir;2066}20672068# quote the given arguments for passing them to the shell2069# quote_command("command", "arg 1", "arg with ' and ! characters")2070# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2071# Try to avoid using this function wherever possible.2072sub quote_command {2073returnjoin(' ',2074map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2075}20762077# get HEAD ref of given project as hash2078sub git_get_head_hash {2079return git_get_full_hash(shift,'HEAD');2080}20812082sub git_get_full_hash {2083return git_get_hash(@_);2084}20852086sub git_get_short_hash {2087return git_get_hash(@_,'--short=7');2088}20892090sub git_get_hash {2091my($project,$hash,@options) =@_;2092my$o_git_dir=$git_dir;2093my$retval=undef;2094$git_dir="$projectroot/$project";2095if(open my$fd,'-|', git_cmd(),'rev-parse',2096'--verify','-q',@options,$hash) {2097$retval= <$fd>;2098chomp$retvalifdefined$retval;2099close$fd;2100}2101if(defined$o_git_dir) {2102$git_dir=$o_git_dir;2103}2104return$retval;2105}21062107# get type of given object2108sub git_get_type {2109my$hash=shift;21102111open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2112my$type= <$fd>;2113close$fdorreturn;2114chomp$type;2115return$type;2116}21172118# repository configuration2119our$config_file='';2120our%config;21212122# store multiple values for single key as anonymous array reference2123# single values stored directly in the hash, not as [ <value> ]2124sub hash_set_multi {2125my($hash,$key,$value) =@_;21262127if(!exists$hash->{$key}) {2128$hash->{$key} =$value;2129}elsif(!ref$hash->{$key}) {2130$hash->{$key} = [$hash->{$key},$value];2131}else{2132push@{$hash->{$key}},$value;2133}2134}21352136# return hash of git project configuration2137# optionally limited to some section, e.g. 'gitweb'2138sub git_parse_project_config {2139my$section_regexp=shift;2140my%config;21412142local$/="\0";21432144open my$fh,"-|", git_cmd(),"config",'-z','-l',2145orreturn;21462147while(my$keyval= <$fh>) {2148chomp$keyval;2149my($key,$value) =split(/\n/,$keyval,2);21502151 hash_set_multi(\%config,$key,$value)2152if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2153}2154close$fh;21552156return%config;2157}21582159# convert config value to boolean: 'true' or 'false'2160# no value, number > 0, 'true' and 'yes' values are true2161# rest of values are treated as false (never as error)2162sub config_to_bool {2163my$val=shift;21642165return1if!defined$val;# section.key21662167# strip leading and trailing whitespace2168$val=~s/^\s+//;2169$val=~s/\s+$//;21702171return(($val=~/^\d+$/&&$val) ||# section.key = 12172($val=~/^(?:true|yes)$/i));# section.key = true2173}21742175# convert config value to simple decimal number2176# an optional value suffix of 'k', 'm', or 'g' will cause the value2177# to be multiplied by 1024, 1048576, or 10737418242178sub config_to_int {2179my$val=shift;21802181# strip leading and trailing whitespace2182$val=~s/^\s+//;2183$val=~s/\s+$//;21842185if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2186$unit=lc($unit);2187# unknown unit is treated as 12188return$num* ($uniteq'g'?1073741824:2189$uniteq'm'?1048576:2190$uniteq'k'?1024:1);2191}2192return$val;2193}21942195# convert config value to array reference, if needed2196sub config_to_multi {2197my$val=shift;21982199returnref($val) ?$val: (defined($val) ? [$val] : []);2200}22012202sub git_get_project_config {2203my($key,$type) =@_;22042205# key sanity check2206return unless($key);2207$key=~s/^gitweb\.//;2208return if($key=~m/\W/);22092210# type sanity check2211if(defined$type) {2212$type=~s/^--//;2213$type=undef2214unless($typeeq'bool'||$typeeq'int');2215}22162217# get config2218if(!defined$config_file||2219$config_filene"$git_dir/config") {2220%config= git_parse_project_config('gitweb');2221$config_file="$git_dir/config";2222}22232224# check if config variable (key) exists2225return unlessexists$config{"gitweb.$key"};22262227# ensure given type2228if(!defined$type) {2229return$config{"gitweb.$key"};2230}elsif($typeeq'bool') {2231# backward compatibility: 'git config --bool' returns true/false2232return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2233}elsif($typeeq'int') {2234return config_to_int($config{"gitweb.$key"});2235}2236return$config{"gitweb.$key"};2237}22382239# get hash of given path at given ref2240sub git_get_hash_by_path {2241my$base=shift;2242my$path=shift||returnundef;2243my$type=shift;22442245$path=~ s,/+$,,;22462247open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2248or die_error(500,"Open git-ls-tree failed");2249my$line= <$fd>;2250close$fdorreturnundef;22512252if(!defined$line) {2253# there is no tree or hash given by $path at $base2254returnundef;2255}22562257#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2258$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2259if(defined$type&&$typene$2) {2260# type doesn't match2261returnundef;2262}2263return$3;2264}22652266# get path of entry with given hash at given tree-ish (ref)2267# used to get 'from' filename for combined diff (merge commit) for renames2268sub git_get_path_by_hash {2269my$base=shift||return;2270my$hash=shift||return;22712272local$/="\0";22732274open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2275orreturnundef;2276while(my$line= <$fd>) {2277chomp$line;22782279#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2280#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2281if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2282close$fd;2283return$1;2284}2285}2286close$fd;2287returnundef;2288}22892290## ......................................................................2291## git utility functions, directly accessing git repository22922293sub git_get_project_description {2294my$path=shift;22952296$git_dir="$projectroot/$path";2297open my$fd,'<',"$git_dir/description"2298orreturn git_get_project_config('description');2299my$descr= <$fd>;2300close$fd;2301if(defined$descr) {2302chomp$descr;2303}2304return$descr;2305}23062307sub git_get_project_ctags {2308my$path=shift;2309my$ctags= {};23102311$git_dir="$projectroot/$path";2312opendir my$dh,"$git_dir/ctags"2313orreturn$ctags;2314foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2315open my$ct,'<',$_ornext;2316my$val= <$ct>;2317chomp$val;2318close$ct;2319my$ctag=$_;$ctag=~ s#.*/##;2320$ctags->{$ctag} =$val;2321}2322closedir$dh;2323$ctags;2324}23252326sub git_populate_project_tagcloud {2327my$ctags=shift;23282329# First, merge different-cased tags; tags vote on casing2330my%ctags_lc;2331foreach(keys%$ctags) {2332$ctags_lc{lc$_}->{count} +=$ctags->{$_};2333if(not$ctags_lc{lc$_}->{topcount}2334or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2335$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2336$ctags_lc{lc$_}->{topname} =$_;2337}2338}23392340my$cloud;2341if(eval{require HTML::TagCloud;1; }) {2342$cloud= HTML::TagCloud->new;2343foreach(sort keys%ctags_lc) {2344# Pad the title with spaces so that the cloud looks2345# less crammed.2346my$title=$ctags_lc{$_}->{topname};2347$title=~s/ / /g;2348$title=~s/^/ /g;2349$title=~s/$/ /g;2350$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2351}2352}else{2353$cloud= \%ctags_lc;2354}2355$cloud;2356}23572358sub git_show_project_tagcloud {2359my($cloud,$count) =@_;2360print STDERR ref($cloud)."..\n";2361if(ref$cloudeq'HTML::TagCloud') {2362return$cloud->html_and_css($count);2363}else{2364my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2365return'<p align="center">'.join(', ',map{2366"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2367}splice(@tags,0,$count)) .'</p>';2368}2369}23702371sub git_get_project_url_list {2372my$path=shift;23732374$git_dir="$projectroot/$path";2375open my$fd,'<',"$git_dir/cloneurl"2376orreturnwantarray?2377@{ config_to_multi(git_get_project_config('url')) } :2378 config_to_multi(git_get_project_config('url'));2379my@git_project_url_list=map{chomp;$_} <$fd>;2380close$fd;23812382returnwantarray?@git_project_url_list: \@git_project_url_list;2383}23842385sub git_get_projects_list {2386my($filter) =@_;2387my@list;23882389$filter||='';2390$filter=~s/\.git$//;23912392my$check_forks= gitweb_check_feature('forks');23932394if(-d $projects_list) {2395# search in directory2396my$dir=$projects_list. ($filter?"/$filter":'');2397# remove the trailing "/"2398$dir=~s!/+$!!;2399my$pfxlen=length("$dir");2400my$pfxdepth= ($dir=~tr!/!!);24012402 File::Find::find({2403 follow_fast =>1,# follow symbolic links2404 follow_skip =>2,# ignore duplicates2405 dangling_symlinks =>0,# ignore dangling symlinks, silently2406 wanted =>sub{2407# skip project-list toplevel, if we get it.2408return if(m!^[/.]$!);2409# only directories can be git repositories2410return unless(-d $_);2411# don't traverse too deep (Find is super slow on os x)2412if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2413$File::Find::prune =1;2414return;2415}24162417my$subdir=substr($File::Find::name,$pfxlen+1);2418# we check related file in $projectroot2419my$path= ($filter?"$filter/":'') .$subdir;2420if(check_export_ok("$projectroot/$path")) {2421push@list, { path =>$path};2422$File::Find::prune =1;2423}2424},2425},"$dir");24262427}elsif(-f $projects_list) {2428# read from file(url-encoded):2429# 'git%2Fgit.git Linus+Torvalds'2430# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2431# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2432my%paths;2433open my$fd,'<',$projects_listorreturn;2434 PROJECT:2435while(my$line= <$fd>) {2436chomp$line;2437my($path,$owner) =split' ',$line;2438$path= unescape($path);2439$owner= unescape($owner);2440if(!defined$path) {2441next;2442}2443if($filterne'') {2444# looking for forks;2445my$pfx=substr($path,0,length($filter));2446if($pfxne$filter) {2447next PROJECT;2448}2449my$sfx=substr($path,length($filter));2450if($sfx!~/^\/.*\.git$/) {2451next PROJECT;2452}2453}elsif($check_forks) {2454 PATH:2455foreachmy$filter(keys%paths) {2456# looking for forks;2457my$pfx=substr($path,0,length($filter));2458if($pfxne$filter) {2459next PATH;2460}2461my$sfx=substr($path,length($filter));2462if($sfx!~/^\/.*\.git$/) {2463next PATH;2464}2465# is a fork, don't include it in2466# the list2467next PROJECT;2468}2469}2470if(check_export_ok("$projectroot/$path")) {2471my$pr= {2472 path =>$path,2473 owner => to_utf8($owner),2474};2475push@list,$pr;2476(my$forks_path=$path) =~s/\.git$//;2477$paths{$forks_path}++;2478}2479}2480close$fd;2481}2482return@list;2483}24842485our$gitweb_project_owner=undef;2486sub git_get_project_list_from_file {24872488return if(defined$gitweb_project_owner);24892490$gitweb_project_owner= {};2491# read from file (url-encoded):2492# 'git%2Fgit.git Linus+Torvalds'2493# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2494# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2495if(-f $projects_list) {2496open(my$fd,'<',$projects_list);2497while(my$line= <$fd>) {2498chomp$line;2499my($pr,$ow) =split' ',$line;2500$pr= unescape($pr);2501$ow= unescape($ow);2502$gitweb_project_owner->{$pr} = to_utf8($ow);2503}2504close$fd;2505}2506}25072508sub git_get_project_owner {2509my$project=shift;2510my$owner;25112512returnundefunless$project;2513$git_dir="$projectroot/$project";25142515if(!defined$gitweb_project_owner) {2516 git_get_project_list_from_file();2517}25182519if(exists$gitweb_project_owner->{$project}) {2520$owner=$gitweb_project_owner->{$project};2521}2522if(!defined$owner){2523$owner= git_get_project_config('owner');2524}2525if(!defined$owner) {2526$owner= get_file_owner("$git_dir");2527}25282529return$owner;2530}25312532sub git_get_last_activity {2533my($path) =@_;2534my$fd;25352536$git_dir="$projectroot/$path";2537open($fd,"-|", git_cmd(),'for-each-ref',2538'--format=%(committer)',2539'--sort=-committerdate',2540'--count=1',2541'refs/heads')orreturn;2542my$most_recent= <$fd>;2543close$fdorreturn;2544if(defined$most_recent&&2545$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2546my$timestamp=$1;2547my$age=time-$timestamp;2548return($age, age_string($age));2549}2550return(undef,undef);2551}25522553sub git_get_references {2554my$type=shift||"";2555my%refs;2556# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112557# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2558open my$fd,"-|", git_cmd(),"show-ref","--dereference",2559($type? ("--","refs/$type") : ())# use -- <pattern> if $type2560orreturn;25612562while(my$line= <$fd>) {2563chomp$line;2564if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2565if(defined$refs{$1}) {2566push@{$refs{$1}},$2;2567}else{2568$refs{$1} = [$2];2569}2570}2571}2572close$fdorreturn;2573return \%refs;2574}25752576sub git_get_rev_name_tags {2577my$hash=shift||returnundef;25782579open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2580orreturn;2581my$name_rev= <$fd>;2582close$fd;25832584if($name_rev=~ m|^$hash tags/(.*)$|) {2585return$1;2586}else{2587# catches also '$hash undefined' output2588returnundef;2589}2590}25912592## ----------------------------------------------------------------------2593## parse to hash functions25942595sub parse_date {2596my$epoch=shift;2597my$tz=shift||"-0000";25982599my%date;2600my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2601my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2602my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2603$date{'hour'} =$hour;2604$date{'minute'} =$min;2605$date{'mday'} =$mday;2606$date{'day'} =$days[$wday];2607$date{'month'} =$months[$mon];2608$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2609$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2610$date{'mday-time'} =sprintf"%d%s%02d:%02d",2611$mday,$months[$mon],$hour,$min;2612$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",26131900+$year,1+$mon,$mday,$hour,$min,$sec;26142615$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2616my$local=$epoch+ ((int$1+ ($2/60)) *3600);2617($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2618$date{'hour_local'} =$hour;2619$date{'minute_local'} =$min;2620$date{'tz_local'} =$tz;2621$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",26221900+$year,$mon+1,$mday,2623$hour,$min,$sec,$tz);2624return%date;2625}26262627sub parse_tag {2628my$tag_id=shift;2629my%tag;2630my@comment;26312632open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2633$tag{'id'} =$tag_id;2634while(my$line= <$fd>) {2635chomp$line;2636if($line=~m/^object ([0-9a-fA-F]{40})$/) {2637$tag{'object'} =$1;2638}elsif($line=~m/^type (.+)$/) {2639$tag{'type'} =$1;2640}elsif($line=~m/^tag (.+)$/) {2641$tag{'name'} =$1;2642}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2643$tag{'author'} =$1;2644$tag{'author_epoch'} =$2;2645$tag{'author_tz'} =$3;2646if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2647$tag{'author_name'} =$1;2648$tag{'author_email'} =$2;2649}else{2650$tag{'author_name'} =$tag{'author'};2651}2652}elsif($line=~m/--BEGIN/) {2653push@comment,$line;2654last;2655}elsif($lineeq"") {2656last;2657}2658}2659push@comment, <$fd>;2660$tag{'comment'} = \@comment;2661close$fdorreturn;2662if(!defined$tag{'name'}) {2663return2664};2665return%tag2666}26672668sub parse_commit_text {2669my($commit_text,$withparents) =@_;2670my@commit_lines=split'\n',$commit_text;2671my%co;26722673pop@commit_lines;# Remove '\0'26742675if(!@commit_lines) {2676return;2677}26782679my$header=shift@commit_lines;2680if($header!~m/^[0-9a-fA-F]{40}/) {2681return;2682}2683($co{'id'},my@parents) =split' ',$header;2684while(my$line=shift@commit_lines) {2685last if$lineeq"\n";2686if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2687$co{'tree'} =$1;2688}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2689push@parents,$1;2690}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2691$co{'author'} = to_utf8($1);2692$co{'author_epoch'} =$2;2693$co{'author_tz'} =$3;2694if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2695$co{'author_name'} =$1;2696$co{'author_email'} =$2;2697}else{2698$co{'author_name'} =$co{'author'};2699}2700}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2701$co{'committer'} = to_utf8($1);2702$co{'committer_epoch'} =$2;2703$co{'committer_tz'} =$3;2704if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2705$co{'committer_name'} =$1;2706$co{'committer_email'} =$2;2707}else{2708$co{'committer_name'} =$co{'committer'};2709}2710}2711}2712if(!defined$co{'tree'}) {2713return;2714};2715$co{'parents'} = \@parents;2716$co{'parent'} =$parents[0];27172718foreachmy$title(@commit_lines) {2719$title=~s/^ //;2720if($titlene"") {2721$co{'title'} = chop_str($title,80,5);2722# remove leading stuff of merges to make the interesting part visible2723if(length($title) >50) {2724$title=~s/^Automatic //;2725$title=~s/^merge (of|with) /Merge ... /i;2726if(length($title) >50) {2727$title=~s/(http|rsync):\/\///;2728}2729if(length($title) >50) {2730$title=~s/(master|www|rsync)\.//;2731}2732if(length($title) >50) {2733$title=~s/kernel.org:?//;2734}2735if(length($title) >50) {2736$title=~s/\/pub\/scm//;2737}2738}2739$co{'title_short'} = chop_str($title,50,5);2740last;2741}2742}2743if(!defined$co{'title'} ||$co{'title'}eq"") {2744$co{'title'} =$co{'title_short'} ='(no commit message)';2745}2746# remove added spaces2747foreachmy$line(@commit_lines) {2748$line=~s/^ //;2749}2750$co{'comment'} = \@commit_lines;27512752my$age=time-$co{'committer_epoch'};2753$co{'age'} =$age;2754$co{'age_string'} = age_string($age);2755my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2756if($age>60*60*24*7*2) {2757$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2758$co{'age_string_age'} =$co{'age_string'};2759}else{2760$co{'age_string_date'} =$co{'age_string'};2761$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2762}2763return%co;2764}27652766sub parse_commit {2767my($commit_id) =@_;2768my%co;27692770local$/="\0";27712772open my$fd,"-|", git_cmd(),"rev-list",2773"--parents",2774"--header",2775"--max-count=1",2776$commit_id,2777"--",2778or die_error(500,"Open git-rev-list failed");2779%co= parse_commit_text(<$fd>,1);2780close$fd;27812782return%co;2783}27842785sub parse_commits {2786my($commit_id,$maxcount,$skip,$filename,@args) =@_;2787my@cos;27882789$maxcount||=1;2790$skip||=0;27912792local$/="\0";27932794open my$fd,"-|", git_cmd(),"rev-list",2795"--header",2796@args,2797("--max-count=".$maxcount),2798("--skip=".$skip),2799@extra_options,2800$commit_id,2801"--",2802($filename? ($filename) : ())2803or die_error(500,"Open git-rev-list failed");2804while(my$line= <$fd>) {2805my%co= parse_commit_text($line);2806push@cos, \%co;2807}2808close$fd;28092810returnwantarray?@cos: \@cos;2811}28122813# parse line of git-diff-tree "raw" output2814sub parse_difftree_raw_line {2815my$line=shift;2816my%res;28172818# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2819# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2820if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2821$res{'from_mode'} =$1;2822$res{'to_mode'} =$2;2823$res{'from_id'} =$3;2824$res{'to_id'} =$4;2825$res{'status'} =$5;2826$res{'similarity'} =$6;2827if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2828($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2829}else{2830$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2831}2832}2833# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2834# combined diff (for merge commit)2835elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2836$res{'nparents'} =length($1);2837$res{'from_mode'} = [split(' ',$2) ];2838$res{'to_mode'} =pop@{$res{'from_mode'}};2839$res{'from_id'} = [split(' ',$3) ];2840$res{'to_id'} =pop@{$res{'from_id'}};2841$res{'status'} = [split('',$4) ];2842$res{'to_file'} = unquote($5);2843}2844# 'c512b523472485aef4fff9e57b229d9d243c967f'2845elsif($line=~m/^([0-9a-fA-F]{40})$/) {2846$res{'commit'} =$1;2847}28482849returnwantarray?%res: \%res;2850}28512852# wrapper: return parsed line of git-diff-tree "raw" output2853# (the argument might be raw line, or parsed info)2854sub parsed_difftree_line {2855my$line_or_ref=shift;28562857if(ref($line_or_ref)eq"HASH") {2858# pre-parsed (or generated by hand)2859return$line_or_ref;2860}else{2861return parse_difftree_raw_line($line_or_ref);2862}2863}28642865# parse line of git-ls-tree output2866sub parse_ls_tree_line {2867my$line=shift;2868my%opts=@_;2869my%res;28702871if($opts{'-l'}) {2872#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'2873$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;28742875$res{'mode'} =$1;2876$res{'type'} =$2;2877$res{'hash'} =$3;2878$res{'size'} =$4;2879if($opts{'-z'}) {2880$res{'name'} =$5;2881}else{2882$res{'name'} = unquote($5);2883}2884}else{2885#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2886$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;28872888$res{'mode'} =$1;2889$res{'type'} =$2;2890$res{'hash'} =$3;2891if($opts{'-z'}) {2892$res{'name'} =$4;2893}else{2894$res{'name'} = unquote($4);2895}2896}28972898returnwantarray?%res: \%res;2899}29002901# generates _two_ hashes, references to which are passed as 2 and 3 argument2902sub parse_from_to_diffinfo {2903my($diffinfo,$from,$to,@parents) =@_;29042905if($diffinfo->{'nparents'}) {2906# combined diff2907$from->{'file'} = [];2908$from->{'href'} = [];2909 fill_from_file_info($diffinfo,@parents)2910unlessexists$diffinfo->{'from_file'};2911for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2912$from->{'file'}[$i] =2913defined$diffinfo->{'from_file'}[$i] ?2914$diffinfo->{'from_file'}[$i] :2915$diffinfo->{'to_file'};2916if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2917$from->{'href'}[$i] = href(action=>"blob",2918 hash_base=>$parents[$i],2919 hash=>$diffinfo->{'from_id'}[$i],2920 file_name=>$from->{'file'}[$i]);2921}else{2922$from->{'href'}[$i] =undef;2923}2924}2925}else{2926# ordinary (not combined) diff2927$from->{'file'} =$diffinfo->{'from_file'};2928if($diffinfo->{'status'}ne"A") {# not new (added) file2929$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2930 hash=>$diffinfo->{'from_id'},2931 file_name=>$from->{'file'});2932}else{2933delete$from->{'href'};2934}2935}29362937$to->{'file'} =$diffinfo->{'to_file'};2938if(!is_deleted($diffinfo)) {# file exists in result2939$to->{'href'} = href(action=>"blob", hash_base=>$hash,2940 hash=>$diffinfo->{'to_id'},2941 file_name=>$to->{'file'});2942}else{2943delete$to->{'href'};2944}2945}29462947## ......................................................................2948## parse to array of hashes functions29492950sub git_get_heads_list {2951my$limit=shift;2952my@headslist;29532954open my$fd,'-|', git_cmd(),'for-each-ref',2955($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2956'--format=%(objectname) %(refname) %(subject)%00%(committer)',2957'refs/heads'2958orreturn;2959while(my$line= <$fd>) {2960my%ref_item;29612962chomp$line;2963my($refinfo,$committerinfo) =split(/\0/,$line);2964my($hash,$name,$title) =split(' ',$refinfo,3);2965my($committer,$epoch,$tz) =2966($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2967$ref_item{'fullname'} =$name;2968$name=~s!^refs/heads/!!;29692970$ref_item{'name'} =$name;2971$ref_item{'id'} =$hash;2972$ref_item{'title'} =$title||'(no commit message)';2973$ref_item{'epoch'} =$epoch;2974if($epoch) {2975$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2976}else{2977$ref_item{'age'} ="unknown";2978}29792980push@headslist, \%ref_item;2981}2982close$fd;29832984returnwantarray?@headslist: \@headslist;2985}29862987sub git_get_tags_list {2988my$limit=shift;2989my@tagslist;29902991open my$fd,'-|', git_cmd(),'for-each-ref',2992($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2993'--format=%(objectname) %(objecttype) %(refname) '.2994'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2995'refs/tags'2996orreturn;2997while(my$line= <$fd>) {2998my%ref_item;29993000chomp$line;3001my($refinfo,$creatorinfo) =split(/\0/,$line);3002my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3003my($creator,$epoch,$tz) =3004($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3005$ref_item{'fullname'} =$name;3006$name=~s!^refs/tags/!!;30073008$ref_item{'type'} =$type;3009$ref_item{'id'} =$id;3010$ref_item{'name'} =$name;3011if($typeeq"tag") {3012$ref_item{'subject'} =$title;3013$ref_item{'reftype'} =$reftype;3014$ref_item{'refid'} =$refid;3015}else{3016$ref_item{'reftype'} =$type;3017$ref_item{'refid'} =$id;3018}30193020if($typeeq"tag"||$typeeq"commit") {3021$ref_item{'epoch'} =$epoch;3022if($epoch) {3023$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3024}else{3025$ref_item{'age'} ="unknown";3026}3027}30283029push@tagslist, \%ref_item;3030}3031close$fd;30323033returnwantarray?@tagslist: \@tagslist;3034}30353036## ----------------------------------------------------------------------3037## filesystem-related functions30383039sub get_file_owner {3040my$path=shift;30413042my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3043my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3044if(!defined$gcos) {3045returnundef;3046}3047my$owner=$gcos;3048$owner=~s/[,;].*$//;3049return to_utf8($owner);3050}30513052# assume that file exists3053sub insert_file {3054my$filename=shift;30553056open my$fd,'<',$filename;3057print map{ to_utf8($_) } <$fd>;3058close$fd;3059}30603061## ......................................................................3062## mimetype related functions30633064sub mimetype_guess_file {3065my$filename=shift;3066my$mimemap=shift;3067-r $mimemaporreturnundef;30683069my%mimemap;3070open(my$mh,'<',$mimemap)orreturnundef;3071while(<$mh>) {3072next ifm/^#/;# skip comments3073my($mimetype,$exts) =split(/\t+/);3074if(defined$exts) {3075my@exts=split(/\s+/,$exts);3076foreachmy$ext(@exts) {3077$mimemap{$ext} =$mimetype;3078}3079}3080}3081close($mh);30823083$filename=~/\.([^.]*)$/;3084return$mimemap{$1};3085}30863087sub mimetype_guess {3088my$filename=shift;3089my$mime;3090$filename=~/\./orreturnundef;30913092if($mimetypes_file) {3093my$file=$mimetypes_file;3094if($file!~m!^/!) {# if it is relative path3095# it is relative to project3096$file="$projectroot/$project/$file";3097}3098$mime= mimetype_guess_file($filename,$file);3099}3100$mime||= mimetype_guess_file($filename,'/etc/mime.types');3101return$mime;3102}31033104sub blob_mimetype {3105my$fd=shift;3106my$filename=shift;31073108if($filename) {3109my$mime= mimetype_guess($filename);3110$mimeandreturn$mime;3111}31123113# just in case3114return$default_blob_plain_mimetypeunless$fd;31153116if(-T $fd) {3117return'text/plain';3118}elsif(!$filename) {3119return'application/octet-stream';3120}elsif($filename=~m/\.png$/i) {3121return'image/png';3122}elsif($filename=~m/\.gif$/i) {3123return'image/gif';3124}elsif($filename=~m/\.jpe?g$/i) {3125return'image/jpeg';3126}else{3127return'application/octet-stream';3128}3129}31303131sub blob_contenttype {3132my($fd,$file_name,$type) =@_;31333134$type||= blob_mimetype($fd,$file_name);3135if($typeeq'text/plain'&&defined$default_text_plain_charset) {3136$type.="; charset=$default_text_plain_charset";3137}31383139return$type;3140}31413142## ======================================================================3143## functions printing HTML: header, footer, error page31443145sub git_header_html {3146my$status=shift||"200 OK";3147my$expires=shift;31483149my$title="$site_name";3150if(defined$project) {3151$title.=" - ". to_utf8($project);3152if(defined$action) {3153$title.="/$action";3154if(defined$file_name) {3155$title.=" - ". esc_path($file_name);3156if($actioneq"tree"&&$file_name!~ m|/$|) {3157$title.="/";3158}3159}3160}3161}3162my$content_type;3163# require explicit support from the UA if we are to send the page as3164# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3165# we have to do this because MSIE sometimes globs '*/*', pretending to3166# support xhtml+xml but choking when it gets what it asked for.3167if(defined$cgi->http('HTTP_ACCEPT') &&3168$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3169$cgi->Accept('application/xhtml+xml') !=0) {3170$content_type='application/xhtml+xml';3171}else{3172$content_type='text/html';3173}3174print$cgi->header(-type=>$content_type, -charset =>'utf-8',3175-status=>$status, -expires =>$expires);3176my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3177print<<EOF;3178<?xml version="1.0" encoding="utf-8"?>3179<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3180<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3181<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3182<!-- git core binaries version$git_version-->3183<head>3184<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3185<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3186<meta name="robots" content="index, nofollow"/>3187<title>$title</title>3188EOF3189# the stylesheet, favicon etc urls won't work correctly with path_info3190# unless we set the appropriate base URL3191if($ENV{'PATH_INFO'}) {3192print"<base href=\"".esc_url($base_url)."\"/>\n";3193}3194# print out each stylesheet that exist, providing backwards capability3195# for those people who defined $stylesheet in a config file3196if(defined$stylesheet) {3197print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3198}else{3199foreachmy$stylesheet(@stylesheets) {3200next unless$stylesheet;3201print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3202}3203}3204if(defined$project) {3205my%href_params= get_feed_info();3206if(!exists$href_params{'-title'}) {3207$href_params{'-title'} ='log';3208}32093210foreachmy$formatqw(RSS Atom){3211my$type=lc($format);3212my%link_attr= (3213'-rel'=>'alternate',3214'-title'=>"$project-$href_params{'-title'} -$formatfeed",3215'-type'=>"application/$type+xml"3216);32173218$href_params{'action'} =$type;3219$link_attr{'-href'} = href(%href_params);3220print"<link ".3221"rel=\"$link_attr{'-rel'}\"".3222"title=\"$link_attr{'-title'}\"".3223"href=\"$link_attr{'-href'}\"".3224"type=\"$link_attr{'-type'}\"".3225"/>\n";32263227$href_params{'extra_options'} ='--no-merges';3228$link_attr{'-href'} = href(%href_params);3229$link_attr{'-title'} .=' (no merges)';3230print"<link ".3231"rel=\"$link_attr{'-rel'}\"".3232"title=\"$link_attr{'-title'}\"".3233"href=\"$link_attr{'-href'}\"".3234"type=\"$link_attr{'-type'}\"".3235"/>\n";3236}32373238}else{3239printf('<link rel="alternate" title="%sprojects list" '.3240'href="%s" type="text/plain; charset=utf-8" />'."\n",3241$site_name, href(project=>undef, action=>"project_index"));3242printf('<link rel="alternate" title="%sprojects feeds" '.3243'href="%s" type="text/x-opml" />'."\n",3244$site_name, href(project=>undef, action=>"opml"));3245}3246if(defined$favicon) {3247printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3248}32493250print"</head>\n".3251"<body>\n";32523253if(defined$site_header&& -f $site_header) {3254 insert_file($site_header);3255}32563257print"<div class=\"page_header\">\n".3258$cgi->a({-href => esc_url($logo_url),3259-title =>$logo_label},3260qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3261print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3262if(defined$project) {3263print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3264if(defined$action) {3265print" /$action";3266}3267print"\n";3268}3269print"</div>\n";32703271my$have_search= gitweb_check_feature('search');3272if(defined$project&&$have_search) {3273if(!defined$searchtext) {3274$searchtext="";3275}3276my$search_hash;3277if(defined$hash_base) {3278$search_hash=$hash_base;3279}elsif(defined$hash) {3280$search_hash=$hash;3281}else{3282$search_hash="HEAD";3283}3284my$action=$my_uri;3285my$use_pathinfo= gitweb_check_feature('pathinfo');3286if($use_pathinfo) {3287$action.="/".esc_url($project);3288}3289print$cgi->startform(-method=>"get", -action =>$action) .3290"<div class=\"search\">\n".3291(!$use_pathinfo&&3292$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3293$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3294$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3295$cgi->popup_menu(-name =>'st', -default=>'commit',3296-values=> ['commit','grep','author','committer','pickaxe']) .3297$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3298" search:\n",3299$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3300"<span title=\"Extended regular expression\">".3301$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3302-checked =>$search_use_regexp) .3303"</span>".3304"</div>".3305$cgi->end_form() ."\n";3306}3307}33083309sub git_footer_html {3310my$feed_class='rss_logo';33113312print"<div class=\"page_footer\">\n";3313if(defined$project) {3314my$descr= git_get_project_description($project);3315if(defined$descr) {3316print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3317}33183319my%href_params= get_feed_info();3320if(!%href_params) {3321$feed_class.=' generic';3322}3323$href_params{'-title'} ||='log';33243325foreachmy$formatqw(RSS Atom){3326$href_params{'action'} =lc($format);3327print$cgi->a({-href => href(%href_params),3328-title =>"$href_params{'-title'}$formatfeed",3329-class=>$feed_class},$format)."\n";3330}33313332}else{3333print$cgi->a({-href => href(project=>undef, action=>"opml"),3334-class=>$feed_class},"OPML") ." ";3335print$cgi->a({-href => href(project=>undef, action=>"project_index"),3336-class=>$feed_class},"TXT") ."\n";3337}3338print"</div>\n";# class="page_footer"33393340if(defined$t0&& gitweb_check_feature('timed')) {3341print"<div id=\"generating_info\">\n";3342print'This page took '.3343'<span id="generating_time" class="time_span">'.3344 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3345' seconds </span>'.3346' and '.3347'<span id="generating_cmd">'.3348$number_of_git_cmds.3349'</span> git commands '.3350" to generate.\n";3351print"</div>\n";# class="page_footer"3352}33533354if(defined$site_footer&& -f $site_footer) {3355 insert_file($site_footer);3356}33573358print qq!<script type="text/javascript" src="$javascript"></script>\n!;3359if(defined$action&&3360$actioneq'blame_incremental') {3361print qq!<script type="text/javascript">\n!.3362 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3363 qq!"!. href() .qq!");\n!.3364 qq!</script>\n!;3365}elsif(gitweb_check_feature('javascript-actions')) {3366print qq!<script type="text/javascript">\n!.3367 qq!window.onload = fixLinks;\n!.3368 qq!</script>\n!;3369}33703371print"</body>\n".3372"</html>";3373}33743375# die_error(<http_status_code>, <error_message>)3376# Example: die_error(404, 'Hash not found')3377# By convention, use the following status codes (as defined in RFC 2616):3378# 400: Invalid or missing CGI parameters, or3379# requested object exists but has wrong type.3380# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3381# this server or project.3382# 404: Requested object/revision/project doesn't exist.3383# 500: The server isn't configured properly, or3384# an internal error occurred (e.g. failed assertions caused by bugs), or3385# an unknown error occurred (e.g. the git binary died unexpectedly).3386# 503: The server is currently unavailable (because it is overloaded,3387# or down for maintenance). Generally, this is a temporary state.3388sub die_error {3389my$status=shift||500;3390my$error=shift||"Internal server error";3391my$extra=shift;33923393my%http_responses= (3394400=>'400 Bad Request',3395403=>'403 Forbidden',3396404=>'404 Not Found',3397500=>'500 Internal Server Error',3398503=>'503 Service Unavailable',3399);3400 git_header_html($http_responses{$status});3401print<<EOF;3402<div class="page_body">3403<br /><br />3404$status-$error3405<br />3406EOF3407if(defined$extra) {3408print"<hr />\n".3409"$extra\n";3410}3411print"</div>\n";34123413 git_footer_html();3414exit;3415}34163417## ----------------------------------------------------------------------3418## functions printing or outputting HTML: navigation34193420sub git_print_page_nav {3421my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3422$extra=''if!defined$extra;# pager or formats34233424my@navs=qw(summary shortlog log commit commitdiff tree);3425if($suppress) {3426@navs=grep{$_ne$suppress}@navs;3427}34283429my%arg=map{$_=> {action=>$_} }@navs;3430if(defined$head) {3431for(qw(commit commitdiff)) {3432$arg{$_}{'hash'} =$head;3433}3434if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3435for(qw(shortlog log)) {3436$arg{$_}{'hash'} =$head;3437}3438}3439}34403441$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3442$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;34433444my@actions= gitweb_get_feature('actions');3445my%repl= (3446'%'=>'%',3447'n'=>$project,# project name3448'f'=>$git_dir,# project path within filesystem3449'h'=>$treehead||'',# current hash ('h' parameter)3450'b'=>$treebase||'',# hash base ('hb' parameter)3451);3452while(@actions) {3453my($label,$link,$pos) =splice(@actions,0,3);3454# insert3455@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3456# munch munch3457$link=~s/%([%nfhb])/$repl{$1}/g;3458$arg{$label}{'_href'} =$link;3459}34603461print"<div class=\"page_nav\">\n".3462(join" | ",3463map{$_eq$current?3464$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3465}@navs);3466print"<br/>\n$extra<br/>\n".3467"</div>\n";3468}34693470sub format_paging_nav {3471my($action,$page,$has_next_link) =@_;3472my$paging_nav;347334743475if($page>0) {3476$paging_nav.=3477$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3478" ⋅ ".3479$cgi->a({-href => href(-replay=>1, page=>$page-1),3480-accesskey =>"p", -title =>"Alt-p"},"prev");3481}else{3482$paging_nav.="first ⋅ prev";3483}34843485if($has_next_link) {3486$paging_nav.=" ⋅ ".3487$cgi->a({-href => href(-replay=>1, page=>$page+1),3488-accesskey =>"n", -title =>"Alt-n"},"next");3489}else{3490$paging_nav.=" ⋅ next";3491}34923493return$paging_nav;3494}34953496## ......................................................................3497## functions printing or outputting HTML: div34983499sub git_print_header_div {3500my($action,$title,$hash,$hash_base) =@_;3501my%args= ();35023503$args{'action'} =$action;3504$args{'hash'} =$hashif$hash;3505$args{'hash_base'} =$hash_baseif$hash_base;35063507print"<div class=\"header\">\n".3508$cgi->a({-href => href(%args), -class=>"title"},3509$title?$title:$action) .3510"\n</div>\n";3511}35123513sub print_local_time {3514print format_local_time(@_);3515}35163517sub format_local_time {3518my$localtime='';3519my%date=@_;3520if($date{'hour_local'} <6) {3521$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3522$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3523}else{3524$localtime.=sprintf(" (%02d:%02d%s)",3525$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3526}35273528return$localtime;3529}35303531# Outputs the author name and date in long form3532sub git_print_authorship {3533my$co=shift;3534my%opts=@_;3535my$tag=$opts{-tag} ||'div';3536my$author=$co->{'author_name'};35373538my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3539print"<$tagclass=\"author_date\">".3540 format_search_author($author,"author", esc_html($author)) .3541" [$ad{'rfc2822'}";3542 print_local_time(%ad)if($opts{-localtime});3543print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3544."</$tag>\n";3545}35463547# Outputs table rows containing the full author or committer information,3548# in the format expected for 'commit' view (& similia).3549# Parameters are a commit hash reference, followed by the list of people3550# to output information for. If the list is empty it defalts to both3551# author and committer.3552sub git_print_authorship_rows {3553my$co=shift;3554# too bad we can't use @people = @_ || ('author', 'committer')3555my@people=@_;3556@people= ('author','committer')unless@people;3557foreachmy$who(@people) {3558my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3559print"<tr><td>$who</td><td>".3560 format_search_author($co->{"${who}_name"},$who,3561 esc_html($co->{"${who}_name"})) ." ".3562 format_search_author($co->{"${who}_email"},$who,3563 esc_html("<".$co->{"${who}_email"} .">")) .3564"</td><td rowspan=\"2\">".3565 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3566"</td></tr>\n".3567"<tr>".3568"<td></td><td>$wd{'rfc2822'}";3569 print_local_time(%wd);3570print"</td>".3571"</tr>\n";3572}3573}35743575sub git_print_page_path {3576my$name=shift;3577my$type=shift;3578my$hb=shift;357935803581print"<div class=\"page_path\">";3582print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3583-title =>'tree root'}, to_utf8("[$project]"));3584print" / ";3585if(defined$name) {3586my@dirname=split'/',$name;3587my$basename=pop@dirname;3588my$fullname='';35893590foreachmy$dir(@dirname) {3591$fullname.= ($fullname?'/':'') .$dir;3592print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3593 hash_base=>$hb),3594-title =>$fullname}, esc_path($dir));3595print" / ";3596}3597if(defined$type&&$typeeq'blob') {3598print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3599 hash_base=>$hb),3600-title =>$name}, esc_path($basename));3601}elsif(defined$type&&$typeeq'tree') {3602print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3603 hash_base=>$hb),3604-title =>$name}, esc_path($basename));3605print" / ";3606}else{3607print esc_path($basename);3608}3609}3610print"<br/></div>\n";3611}36123613sub git_print_log {3614my$log=shift;3615my%opts=@_;36163617if($opts{'-remove_title'}) {3618# remove title, i.e. first line of log3619shift@$log;3620}3621# remove leading empty lines3622while(defined$log->[0] &&$log->[0]eq"") {3623shift@$log;3624}36253626# print log3627my$signoff=0;3628my$empty=0;3629foreachmy$line(@$log) {3630if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3631$signoff=1;3632$empty=0;3633if(!$opts{'-remove_signoff'}) {3634print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3635next;3636}else{3637# remove signoff lines3638next;3639}3640}else{3641$signoff=0;3642}36433644# print only one empty line3645# do not print empty line after signoff3646if($lineeq"") {3647next if($empty||$signoff);3648$empty=1;3649}else{3650$empty=0;3651}36523653print format_log_line_html($line) ."<br/>\n";3654}36553656if($opts{'-final_empty_line'}) {3657# end with single empty line3658print"<br/>\n"unless$empty;3659}3660}36613662# return link target (what link points to)3663sub git_get_link_target {3664my$hash=shift;3665my$link_target;36663667# read link3668open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3669orreturn;3670{3671local$/=undef;3672$link_target= <$fd>;3673}3674close$fd3675orreturn;36763677return$link_target;3678}36793680# given link target, and the directory (basedir) the link is in,3681# return target of link relative to top directory (top tree);3682# return undef if it is not possible (including absolute links).3683sub normalize_link_target {3684my($link_target,$basedir) =@_;36853686# absolute symlinks (beginning with '/') cannot be normalized3687return if(substr($link_target,0,1)eq'/');36883689# normalize link target to path from top (root) tree (dir)3690my$path;3691if($basedir) {3692$path=$basedir.'/'.$link_target;3693}else{3694# we are in top (root) tree (dir)3695$path=$link_target;3696}36973698# remove //, /./, and /../3699my@path_parts;3700foreachmy$part(split('/',$path)) {3701# discard '.' and ''3702next if(!$part||$parteq'.');3703# handle '..'3704if($parteq'..') {3705if(@path_parts) {3706pop@path_parts;3707}else{3708# link leads outside repository (outside top dir)3709return;3710}3711}else{3712push@path_parts,$part;3713}3714}3715$path=join('/',@path_parts);37163717return$path;3718}37193720# print tree entry (row of git_tree), but without encompassing <tr> element3721sub git_print_tree_entry {3722my($t,$basedir,$hash_base,$have_blame) =@_;37233724my%base_key= ();3725$base_key{'hash_base'} =$hash_baseifdefined$hash_base;37263727# The format of a table row is: mode list link. Where mode is3728# the mode of the entry, list is the name of the entry, an href,3729# and link is the action links of the entry.37303731print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3732if(exists$t->{'size'}) {3733print"<td class=\"size\">$t->{'size'}</td>\n";3734}3735if($t->{'type'}eq"blob") {3736print"<td class=\"list\">".3737$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3738 file_name=>"$basedir$t->{'name'}",%base_key),3739-class=>"list"}, esc_path($t->{'name'}));3740if(S_ISLNK(oct$t->{'mode'})) {3741my$link_target= git_get_link_target($t->{'hash'});3742if($link_target) {3743my$norm_target= normalize_link_target($link_target,$basedir);3744if(defined$norm_target) {3745print" -> ".3746$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3747 file_name=>$norm_target),3748-title =>$norm_target}, esc_path($link_target));3749}else{3750print" -> ". esc_path($link_target);3751}3752}3753}3754print"</td>\n";3755print"<td class=\"link\">";3756print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3757 file_name=>"$basedir$t->{'name'}",%base_key)},3758"blob");3759if($have_blame) {3760print" | ".3761$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3762 file_name=>"$basedir$t->{'name'}",%base_key)},3763"blame");3764}3765if(defined$hash_base) {3766print" | ".3767$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3768 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3769"history");3770}3771print" | ".3772$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3773 file_name=>"$basedir$t->{'name'}")},3774"raw");3775print"</td>\n";37763777}elsif($t->{'type'}eq"tree") {3778print"<td class=\"list\">";3779print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3780 file_name=>"$basedir$t->{'name'}",3781%base_key)},3782 esc_path($t->{'name'}));3783print"</td>\n";3784print"<td class=\"link\">";3785print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3786 file_name=>"$basedir$t->{'name'}",3787%base_key)},3788"tree");3789if(defined$hash_base) {3790print" | ".3791$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3792 file_name=>"$basedir$t->{'name'}")},3793"history");3794}3795print"</td>\n";3796}else{3797# unknown object: we can only present history for it3798# (this includes 'commit' object, i.e. submodule support)3799print"<td class=\"list\">".3800 esc_path($t->{'name'}) .3801"</td>\n";3802print"<td class=\"link\">";3803if(defined$hash_base) {3804print$cgi->a({-href => href(action=>"history",3805 hash_base=>$hash_base,3806 file_name=>"$basedir$t->{'name'}")},3807"history");3808}3809print"</td>\n";3810}3811}38123813## ......................................................................3814## functions printing large fragments of HTML38153816# get pre-image filenames for merge (combined) diff3817sub fill_from_file_info {3818my($diff,@parents) =@_;38193820$diff->{'from_file'} = [ ];3821$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3822for(my$i=0;$i<$diff->{'nparents'};$i++) {3823if($diff->{'status'}[$i]eq'R'||3824$diff->{'status'}[$i]eq'C') {3825$diff->{'from_file'}[$i] =3826 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3827}3828}38293830return$diff;3831}38323833# is current raw difftree line of file deletion3834sub is_deleted {3835my$diffinfo=shift;38363837return$diffinfo->{'to_id'}eq('0' x 40);3838}38393840# does patch correspond to [previous] difftree raw line3841# $diffinfo - hashref of parsed raw diff format3842# $patchinfo - hashref of parsed patch diff format3843# (the same keys as in $diffinfo)3844sub is_patch_split {3845my($diffinfo,$patchinfo) =@_;38463847returndefined$diffinfo&&defined$patchinfo3848&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3849}385038513852sub git_difftree_body {3853my($difftree,$hash,@parents) =@_;3854my($parent) =$parents[0];3855my$have_blame= gitweb_check_feature('blame');3856print"<div class=\"list_head\">\n";3857if($#{$difftree} >10) {3858print(($#{$difftree} +1) ." files changed:\n");3859}3860print"</div>\n";38613862print"<table class=\"".3863(@parents>1?"combined ":"") .3864"diff_tree\">\n";38653866# header only for combined diff in 'commitdiff' view3867my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3868if($has_header) {3869# table header3870print"<thead><tr>\n".3871"<th></th><th></th>\n";# filename, patchN link3872for(my$i=0;$i<@parents;$i++) {3873my$par=$parents[$i];3874print"<th>".3875$cgi->a({-href => href(action=>"commitdiff",3876 hash=>$hash, hash_parent=>$par),3877-title =>'commitdiff to parent number '.3878($i+1) .': '.substr($par,0,7)},3879$i+1) .3880" </th>\n";3881}3882print"</tr></thead>\n<tbody>\n";3883}38843885my$alternate=1;3886my$patchno=0;3887foreachmy$line(@{$difftree}) {3888my$diff= parsed_difftree_line($line);38893890if($alternate) {3891print"<tr class=\"dark\">\n";3892}else{3893print"<tr class=\"light\">\n";3894}3895$alternate^=1;38963897if(exists$diff->{'nparents'}) {# combined diff38983899 fill_from_file_info($diff,@parents)3900unlessexists$diff->{'from_file'};39013902if(!is_deleted($diff)) {3903# file exists in the result (child) commit3904print"<td>".3905$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3906 file_name=>$diff->{'to_file'},3907 hash_base=>$hash),3908-class=>"list"}, esc_path($diff->{'to_file'})) .3909"</td>\n";3910}else{3911print"<td>".3912 esc_path($diff->{'to_file'}) .3913"</td>\n";3914}39153916if($actioneq'commitdiff') {3917# link to patch3918$patchno++;3919print"<td class=\"link\">".3920$cgi->a({-href =>"#patch$patchno"},"patch") .3921" | ".3922"</td>\n";3923}39243925my$has_history=0;3926my$not_deleted=0;3927for(my$i=0;$i<$diff->{'nparents'};$i++) {3928my$hash_parent=$parents[$i];3929my$from_hash=$diff->{'from_id'}[$i];3930my$from_path=$diff->{'from_file'}[$i];3931my$status=$diff->{'status'}[$i];39323933$has_history||= ($statusne'A');3934$not_deleted||= ($statusne'D');39353936if($statuseq'A') {3937print"<td class=\"link\"align=\"right\"> | </td>\n";3938}elsif($statuseq'D') {3939print"<td class=\"link\">".3940$cgi->a({-href => href(action=>"blob",3941 hash_base=>$hash,3942 hash=>$from_hash,3943 file_name=>$from_path)},3944"blob". ($i+1)) .3945" | </td>\n";3946}else{3947if($diff->{'to_id'}eq$from_hash) {3948print"<td class=\"link nochange\">";3949}else{3950print"<td class=\"link\">";3951}3952print$cgi->a({-href => href(action=>"blobdiff",3953 hash=>$diff->{'to_id'},3954 hash_parent=>$from_hash,3955 hash_base=>$hash,3956 hash_parent_base=>$hash_parent,3957 file_name=>$diff->{'to_file'},3958 file_parent=>$from_path)},3959"diff". ($i+1)) .3960" | </td>\n";3961}3962}39633964print"<td class=\"link\">";3965if($not_deleted) {3966print$cgi->a({-href => href(action=>"blob",3967 hash=>$diff->{'to_id'},3968 file_name=>$diff->{'to_file'},3969 hash_base=>$hash)},3970"blob");3971print" | "if($has_history);3972}3973if($has_history) {3974print$cgi->a({-href => href(action=>"history",3975 file_name=>$diff->{'to_file'},3976 hash_base=>$hash)},3977"history");3978}3979print"</td>\n";39803981print"</tr>\n";3982next;# instead of 'else' clause, to avoid extra indent3983}3984# else ordinary diff39853986my($to_mode_oct,$to_mode_str,$to_file_type);3987my($from_mode_oct,$from_mode_str,$from_file_type);3988if($diff->{'to_mode'}ne('0' x 6)) {3989$to_mode_oct=oct$diff->{'to_mode'};3990if(S_ISREG($to_mode_oct)) {# only for regular file3991$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3992}3993$to_file_type= file_type($diff->{'to_mode'});3994}3995if($diff->{'from_mode'}ne('0' x 6)) {3996$from_mode_oct=oct$diff->{'from_mode'};3997if(S_ISREG($to_mode_oct)) {# only for regular file3998$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3999}4000$from_file_type= file_type($diff->{'from_mode'});4001}40024003if($diff->{'status'}eq"A") {# created4004my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4005$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4006$mode_chng.="]</span>";4007print"<td>";4008print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4009 hash_base=>$hash, file_name=>$diff->{'file'}),4010-class=>"list"}, esc_path($diff->{'file'}));4011print"</td>\n";4012print"<td>$mode_chng</td>\n";4013print"<td class=\"link\">";4014if($actioneq'commitdiff') {4015# link to patch4016$patchno++;4017print$cgi->a({-href =>"#patch$patchno"},"patch");4018print" | ";4019}4020print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4021 hash_base=>$hash, file_name=>$diff->{'file'})},4022"blob");4023print"</td>\n";40244025}elsif($diff->{'status'}eq"D") {# deleted4026my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4027print"<td>";4028print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4029 hash_base=>$parent, file_name=>$diff->{'file'}),4030-class=>"list"}, esc_path($diff->{'file'}));4031print"</td>\n";4032print"<td>$mode_chng</td>\n";4033print"<td class=\"link\">";4034if($actioneq'commitdiff') {4035# link to patch4036$patchno++;4037print$cgi->a({-href =>"#patch$patchno"},"patch");4038print" | ";4039}4040print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4041 hash_base=>$parent, file_name=>$diff->{'file'})},4042"blob") ." | ";4043if($have_blame) {4044print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4045 file_name=>$diff->{'file'})},4046"blame") ." | ";4047}4048print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4049 file_name=>$diff->{'file'})},4050"history");4051print"</td>\n";40524053}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4054my$mode_chnge="";4055if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4056$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4057if($from_file_typene$to_file_type) {4058$mode_chnge.=" from$from_file_typeto$to_file_type";4059}4060if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4061if($from_mode_str&&$to_mode_str) {4062$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4063}elsif($to_mode_str) {4064$mode_chnge.=" mode:$to_mode_str";4065}4066}4067$mode_chnge.="]</span>\n";4068}4069print"<td>";4070print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4071 hash_base=>$hash, file_name=>$diff->{'file'}),4072-class=>"list"}, esc_path($diff->{'file'}));4073print"</td>\n";4074print"<td>$mode_chnge</td>\n";4075print"<td class=\"link\">";4076if($actioneq'commitdiff') {4077# link to patch4078$patchno++;4079print$cgi->a({-href =>"#patch$patchno"},"patch") .4080" | ";4081}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4082# "commit" view and modified file (not onlu mode changed)4083print$cgi->a({-href => href(action=>"blobdiff",4084 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4085 hash_base=>$hash, hash_parent_base=>$parent,4086 file_name=>$diff->{'file'})},4087"diff") .4088" | ";4089}4090print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4091 hash_base=>$hash, file_name=>$diff->{'file'})},4092"blob") ." | ";4093if($have_blame) {4094print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4095 file_name=>$diff->{'file'})},4096"blame") ." | ";4097}4098print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4099 file_name=>$diff->{'file'})},4100"history");4101print"</td>\n";41024103}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4104my%status_name= ('R'=>'moved','C'=>'copied');4105my$nstatus=$status_name{$diff->{'status'}};4106my$mode_chng="";4107if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4108# mode also for directories, so we cannot use $to_mode_str4109$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4110}4111print"<td>".4112$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4113 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4114-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4115"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4116$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4117 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4118-class=>"list"}, esc_path($diff->{'from_file'})) .4119" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4120"<td class=\"link\">";4121if($actioneq'commitdiff') {4122# link to patch4123$patchno++;4124print$cgi->a({-href =>"#patch$patchno"},"patch") .4125" | ";4126}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4127# "commit" view and modified file (not only pure rename or copy)4128print$cgi->a({-href => href(action=>"blobdiff",4129 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4130 hash_base=>$hash, hash_parent_base=>$parent,4131 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4132"diff") .4133" | ";4134}4135print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4136 hash_base=>$parent, file_name=>$diff->{'to_file'})},4137"blob") ." | ";4138if($have_blame) {4139print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4140 file_name=>$diff->{'to_file'})},4141"blame") ." | ";4142}4143print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4144 file_name=>$diff->{'to_file'})},4145"history");4146print"</td>\n";41474148}# we should not encounter Unmerged (U) or Unknown (X) status4149print"</tr>\n";4150}4151print"</tbody>"if$has_header;4152print"</table>\n";4153}41544155sub git_patchset_body {4156my($fd,$difftree,$hash,@hash_parents) =@_;4157my($hash_parent) =$hash_parents[0];41584159my$is_combined= (@hash_parents>1);4160my$patch_idx=0;4161my$patch_number=0;4162my$patch_line;4163my$diffinfo;4164my$to_name;4165my(%from,%to);41664167print"<div class=\"patchset\">\n";41684169# skip to first patch4170while($patch_line= <$fd>) {4171chomp$patch_line;41724173last if($patch_line=~m/^diff /);4174}41754176 PATCH:4177while($patch_line) {41784179# parse "git diff" header line4180if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4181# $1 is from_name, which we do not use4182$to_name= unquote($2);4183$to_name=~s!^b/!!;4184}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4185# $1 is 'cc' or 'combined', which we do not use4186$to_name= unquote($2);4187}else{4188$to_name=undef;4189}41904191# check if current patch belong to current raw line4192# and parse raw git-diff line if needed4193if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4194# this is continuation of a split patch4195print"<div class=\"patch cont\">\n";4196}else{4197# advance raw git-diff output if needed4198$patch_idx++ifdefined$diffinfo;41994200# read and prepare patch information4201$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42024203# compact combined diff output can have some patches skipped4204# find which patch (using pathname of result) we are at now;4205if($is_combined) {4206while($to_namene$diffinfo->{'to_file'}) {4207print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4208 format_diff_cc_simplified($diffinfo,@hash_parents) .4209"</div>\n";# class="patch"42104211$patch_idx++;4212$patch_number++;42134214last if$patch_idx>$#$difftree;4215$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4216}4217}42184219# modifies %from, %to hashes4220 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);42214222# this is first patch for raw difftree line with $patch_idx index4223# we index @$difftree array from 0, but number patches from 14224print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4225}42264227# git diff header4228#assert($patch_line =~ m/^diff /) if DEBUG;4229#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4230$patch_number++;4231# print "git diff" header4232print format_git_diff_header_line($patch_line,$diffinfo,4233 \%from, \%to);42344235# print extended diff header4236print"<div class=\"diff extended_header\">\n";4237 EXTENDED_HEADER:4238while($patch_line= <$fd>) {4239chomp$patch_line;42404241last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);42424243print format_extended_diff_header_line($patch_line,$diffinfo,4244 \%from, \%to);4245}4246print"</div>\n";# class="diff extended_header"42474248# from-file/to-file diff header4249if(!$patch_line) {4250print"</div>\n";# class="patch"4251last PATCH;4252}4253next PATCH if($patch_line=~m/^diff /);4254#assert($patch_line =~ m/^---/) if DEBUG;42554256my$last_patch_line=$patch_line;4257$patch_line= <$fd>;4258chomp$patch_line;4259#assert($patch_line =~ m/^\+\+\+/) if DEBUG;42604261print format_diff_from_to_header($last_patch_line,$patch_line,4262$diffinfo, \%from, \%to,4263@hash_parents);42644265# the patch itself4266 LINE:4267while($patch_line= <$fd>) {4268chomp$patch_line;42694270next PATCH if($patch_line=~m/^diff /);42714272print format_diff_line($patch_line, \%from, \%to);4273}42744275}continue{4276print"</div>\n";# class="patch"4277}42784279# for compact combined (--cc) format, with chunk and patch simpliciaction4280# patchset might be empty, but there might be unprocessed raw lines4281for(++$patch_idxif$patch_number>0;4282$patch_idx<@$difftree;4283++$patch_idx) {4284# read and prepare patch information4285$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42864287# generate anchor for "patch" links in difftree / whatchanged part4288print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4289 format_diff_cc_simplified($diffinfo,@hash_parents) .4290"</div>\n";# class="patch"42914292$patch_number++;4293}42944295if($patch_number==0) {4296if(@hash_parents>1) {4297print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4298}else{4299print"<div class=\"diff nodifferences\">No differences found</div>\n";4300}4301}43024303print"</div>\n";# class="patchset"4304}43054306# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43074308# fills project list info (age, description, owner, forks) for each4309# project in the list, removing invalid projects from returned list4310# NOTE: modifies $projlist, but does not remove entries from it4311sub fill_project_list_info {4312my($projlist,$check_forks) =@_;4313my@projects;43144315my$show_ctags= gitweb_check_feature('ctags');4316 PROJECT:4317foreachmy$pr(@$projlist) {4318my(@activity) = git_get_last_activity($pr->{'path'});4319unless(@activity) {4320next PROJECT;4321}4322($pr->{'age'},$pr->{'age_string'}) =@activity;4323if(!defined$pr->{'descr'}) {4324my$descr= git_get_project_description($pr->{'path'}) ||"";4325$descr= to_utf8($descr);4326$pr->{'descr_long'} =$descr;4327$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4328}4329if(!defined$pr->{'owner'}) {4330$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4331}4332if($check_forks) {4333my$pname=$pr->{'path'};4334if(($pname=~s/\.git$//) &&4335($pname!~/\/$/) &&4336(-d "$projectroot/$pname")) {4337$pr->{'forks'} ="-d$projectroot/$pname";4338}else{4339$pr->{'forks'} =0;4340}4341}4342$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4343push@projects,$pr;4344}43454346return@projects;4347}43484349# print 'sort by' <th> element, generating 'sort by $name' replay link4350# if that order is not selected4351sub print_sort_th {4352print format_sort_th(@_);4353}43544355sub format_sort_th {4356my($name,$order,$header) =@_;4357my$sort_th="";4358$header||=ucfirst($name);43594360if($ordereq$name) {4361$sort_th.="<th>$header</th>\n";4362}else{4363$sort_th.="<th>".4364$cgi->a({-href => href(-replay=>1, order=>$name),4365-class=>"header"},$header) .4366"</th>\n";4367}43684369return$sort_th;4370}43714372sub git_project_list_body {4373# actually uses global variable $project4374my($projlist,$order,$from,$to,$extra,$no_header) =@_;43754376my$check_forks= gitweb_check_feature('forks');4377my@projects= fill_project_list_info($projlist,$check_forks);43784379$order||=$default_projects_order;4380$from=0unlessdefined$from;4381$to=$#projectsif(!defined$to||$#projects<$to);43824383my%order_info= (4384 project => { key =>'path', type =>'str'},4385 descr => { key =>'descr_long', type =>'str'},4386 owner => { key =>'owner', type =>'str'},4387 age => { key =>'age', type =>'num'}4388);4389my$oi=$order_info{$order};4390if($oi->{'type'}eq'str') {4391@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4392}else{4393@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4394}43954396my$show_ctags= gitweb_check_feature('ctags');4397if($show_ctags) {4398my%ctags;4399foreachmy$p(@projects) {4400foreachmy$ct(keys%{$p->{'ctags'}}) {4401$ctags{$ct} +=$p->{'ctags'}->{$ct};4402}4403}4404my$cloud= git_populate_project_tagcloud(\%ctags);4405print git_show_project_tagcloud($cloud,64);4406}44074408print"<table class=\"project_list\">\n";4409unless($no_header) {4410print"<tr>\n";4411if($check_forks) {4412print"<th></th>\n";4413}4414 print_sort_th('project',$order,'Project');4415 print_sort_th('descr',$order,'Description');4416 print_sort_th('owner',$order,'Owner');4417 print_sort_th('age',$order,'Last Change');4418print"<th></th>\n".# for links4419"</tr>\n";4420}4421my$alternate=1;4422my$tagfilter=$cgi->param('by_tag');4423for(my$i=$from;$i<=$to;$i++) {4424my$pr=$projects[$i];44254426next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4427next if$searchtextand not$pr->{'path'} =~/$searchtext/4428and not$pr->{'descr_long'} =~/$searchtext/;4429# Weed out forks or non-matching entries of search4430if($check_forks) {4431my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4432$forkbase="^$forkbase"if$forkbase;4433next ifnot$searchtextand not$tagfilterand$show_ctags4434and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4435}44364437if($alternate) {4438print"<tr class=\"dark\">\n";4439}else{4440print"<tr class=\"light\">\n";4441}4442$alternate^=1;4443if($check_forks) {4444print"<td>";4445if($pr->{'forks'}) {4446print"<!--$pr->{'forks'} -->\n";4447print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4448}4449print"</td>\n";4450}4451print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4452-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4453"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4454-class=>"list", -title =>$pr->{'descr_long'}},4455 esc_html($pr->{'descr'})) ."</td>\n".4456"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4457print"<td class=\"". age_class($pr->{'age'}) ."\">".4458(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4459"<td class=\"link\">".4460$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4461$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4462$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4463$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4464($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4465"</td>\n".4466"</tr>\n";4467}4468if(defined$extra) {4469print"<tr>\n";4470if($check_forks) {4471print"<td></td>\n";4472}4473print"<td colspan=\"5\">$extra</td>\n".4474"</tr>\n";4475}4476print"</table>\n";4477}44784479sub git_log_body {4480# uses global variable $project4481my($commitlist,$from,$to,$refs,$extra) =@_;44824483$from=0unlessdefined$from;4484$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);44854486for(my$i=0;$i<=$to;$i++) {4487my%co= %{$commitlist->[$i]};4488next if!%co;4489my$commit=$co{'id'};4490my$ref= format_ref_marker($refs,$commit);4491my%ad= parse_date($co{'author_epoch'});4492 git_print_header_div('commit',4493"<span class=\"age\">$co{'age_string'}</span>".4494 esc_html($co{'title'}) .$ref,4495$commit);4496print"<div class=\"title_text\">\n".4497"<div class=\"log_link\">\n".4498$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4499" | ".4500$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4501" | ".4502$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4503"<br/>\n".4504"</div>\n";4505 git_print_authorship(\%co, -tag =>'span');4506print"<br/>\n</div>\n";45074508print"<div class=\"log_body\">\n";4509 git_print_log($co{'comment'}, -final_empty_line=>1);4510print"</div>\n";4511}4512if($extra) {4513print"<div class=\"page_nav\">\n";4514print"$extra\n";4515print"</div>\n";4516}4517}45184519sub git_shortlog_body {4520# uses global variable $project4521my($commitlist,$from,$to,$refs,$extra) =@_;45224523$from=0unlessdefined$from;4524$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45254526print"<table class=\"shortlog\">\n";4527my$alternate=1;4528for(my$i=$from;$i<=$to;$i++) {4529my%co= %{$commitlist->[$i]};4530my$commit=$co{'id'};4531my$ref= format_ref_marker($refs,$commit);4532if($alternate) {4533print"<tr class=\"dark\">\n";4534}else{4535print"<tr class=\"light\">\n";4536}4537$alternate^=1;4538# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4539print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4540 format_author_html('td', \%co,10) ."<td>";4541print format_subject_html($co{'title'},$co{'title_short'},4542 href(action=>"commit", hash=>$commit),$ref);4543print"</td>\n".4544"<td class=\"link\">".4545$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4546$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4547$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4548my$snapshot_links= format_snapshot_links($commit);4549if(defined$snapshot_links) {4550print" | ".$snapshot_links;4551}4552print"</td>\n".4553"</tr>\n";4554}4555if(defined$extra) {4556print"<tr>\n".4557"<td colspan=\"4\">$extra</td>\n".4558"</tr>\n";4559}4560print"</table>\n";4561}45624563sub git_history_body {4564# Warning: assumes constant type (blob or tree) during history4565my($commitlist,$from,$to,$refs,$extra,4566$file_name,$file_hash,$ftype) =@_;45674568$from=0unlessdefined$from;4569$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});45704571print"<table class=\"history\">\n";4572my$alternate=1;4573for(my$i=$from;$i<=$to;$i++) {4574my%co= %{$commitlist->[$i]};4575if(!%co) {4576next;4577}4578my$commit=$co{'id'};45794580my$ref= format_ref_marker($refs,$commit);45814582if($alternate) {4583print"<tr class=\"dark\">\n";4584}else{4585print"<tr class=\"light\">\n";4586}4587$alternate^=1;4588print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4589# shortlog: format_author_html('td', \%co, 10)4590 format_author_html('td', \%co,15,3) ."<td>";4591# originally git_history used chop_str($co{'title'}, 50)4592print format_subject_html($co{'title'},$co{'title_short'},4593 href(action=>"commit", hash=>$commit),$ref);4594print"</td>\n".4595"<td class=\"link\">".4596$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4597$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");45984599if($ftypeeq'blob') {4600my$blob_current=$file_hash;4601my$blob_parent= git_get_hash_by_path($commit,$file_name);4602if(defined$blob_current&&defined$blob_parent&&4603$blob_currentne$blob_parent) {4604print" | ".4605$cgi->a({-href => href(action=>"blobdiff",4606 hash=>$blob_current, hash_parent=>$blob_parent,4607 hash_base=>$hash_base, hash_parent_base=>$commit,4608 file_name=>$file_name)},4609"diff to current");4610}4611}4612print"</td>\n".4613"</tr>\n";4614}4615if(defined$extra) {4616print"<tr>\n".4617"<td colspan=\"4\">$extra</td>\n".4618"</tr>\n";4619}4620print"</table>\n";4621}46224623sub git_tags_body {4624# uses global variable $project4625my($taglist,$from,$to,$extra) =@_;4626$from=0unlessdefined$from;4627$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);46284629print"<table class=\"tags\">\n";4630my$alternate=1;4631for(my$i=$from;$i<=$to;$i++) {4632my$entry=$taglist->[$i];4633my%tag=%$entry;4634my$comment=$tag{'subject'};4635my$comment_short;4636if(defined$comment) {4637$comment_short= chop_str($comment,30,5);4638}4639if($alternate) {4640print"<tr class=\"dark\">\n";4641}else{4642print"<tr class=\"light\">\n";4643}4644$alternate^=1;4645if(defined$tag{'age'}) {4646print"<td><i>$tag{'age'}</i></td>\n";4647}else{4648print"<td></td>\n";4649}4650print"<td>".4651$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4652-class=>"list name"}, esc_html($tag{'name'})) .4653"</td>\n".4654"<td>";4655if(defined$comment) {4656print format_subject_html($comment,$comment_short,4657 href(action=>"tag", hash=>$tag{'id'}));4658}4659print"</td>\n".4660"<td class=\"selflink\">";4661if($tag{'type'}eq"tag") {4662print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4663}else{4664print" ";4665}4666print"</td>\n".4667"<td class=\"link\">"." | ".4668$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4669if($tag{'reftype'}eq"commit") {4670print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4671" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4672}elsif($tag{'reftype'}eq"blob") {4673print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4674}4675print"</td>\n".4676"</tr>";4677}4678if(defined$extra) {4679print"<tr>\n".4680"<td colspan=\"5\">$extra</td>\n".4681"</tr>\n";4682}4683print"</table>\n";4684}46854686sub git_heads_body {4687# uses global variable $project4688my($headlist,$head,$from,$to,$extra) =@_;4689$from=0unlessdefined$from;4690$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);46914692print"<table class=\"heads\">\n";4693my$alternate=1;4694for(my$i=$from;$i<=$to;$i++) {4695my$entry=$headlist->[$i];4696my%ref=%$entry;4697my$curr=$ref{'id'}eq$head;4698if($alternate) {4699print"<tr class=\"dark\">\n";4700}else{4701print"<tr class=\"light\">\n";4702}4703$alternate^=1;4704print"<td><i>$ref{'age'}</i></td>\n".4705($curr?"<td class=\"current_head\">":"<td>") .4706$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4707-class=>"list name"},esc_html($ref{'name'})) .4708"</td>\n".4709"<td class=\"link\">".4710$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4711$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4712$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4713"</td>\n".4714"</tr>";4715}4716if(defined$extra) {4717print"<tr>\n".4718"<td colspan=\"3\">$extra</td>\n".4719"</tr>\n";4720}4721print"</table>\n";4722}47234724sub git_search_grep_body {4725my($commitlist,$from,$to,$extra) =@_;4726$from=0unlessdefined$from;4727$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47284729print"<table class=\"commit_search\">\n";4730my$alternate=1;4731for(my$i=$from;$i<=$to;$i++) {4732my%co= %{$commitlist->[$i]};4733if(!%co) {4734next;4735}4736my$commit=$co{'id'};4737if($alternate) {4738print"<tr class=\"dark\">\n";4739}else{4740print"<tr class=\"light\">\n";4741}4742$alternate^=1;4743print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4744 format_author_html('td', \%co,15,5) .4745"<td>".4746$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4747-class=>"list subject"},4748 chop_and_escape_str($co{'title'},50) ."<br/>");4749my$comment=$co{'comment'};4750foreachmy$line(@$comment) {4751if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4752my($lead,$match,$trail) = ($1,$2,$3);4753$match= chop_str($match,70,5,'center');4754my$contextlen=int((80-length($match))/2);4755$contextlen=30if($contextlen>30);4756$lead= chop_str($lead,$contextlen,10,'left');4757$trail= chop_str($trail,$contextlen,10,'right');47584759$lead= esc_html($lead);4760$match= esc_html($match);4761$trail= esc_html($trail);47624763print"$lead<span class=\"match\">$match</span>$trail<br />";4764}4765}4766print"</td>\n".4767"<td class=\"link\">".4768$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4769" | ".4770$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4771" | ".4772$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4773print"</td>\n".4774"</tr>\n";4775}4776if(defined$extra) {4777print"<tr>\n".4778"<td colspan=\"3\">$extra</td>\n".4779"</tr>\n";4780}4781print"</table>\n";4782}47834784## ======================================================================4785## ======================================================================4786## actions47874788sub git_project_list {4789my$order=$input_params{'order'};4790if(defined$order&&$order!~m/none|project|descr|owner|age/) {4791 die_error(400,"Unknown order parameter");4792}47934794my@list= git_get_projects_list();4795if(!@list) {4796 die_error(404,"No projects found");4797}47984799 git_header_html();4800if(defined$home_text&& -f $home_text) {4801print"<div class=\"index_include\">\n";4802 insert_file($home_text);4803print"</div>\n";4804}4805print$cgi->startform(-method=>"get") .4806"<p class=\"projsearch\">Search:\n".4807$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4808"</p>".4809$cgi->end_form() ."\n";4810 git_project_list_body(\@list,$order);4811 git_footer_html();4812}48134814sub git_forks {4815my$order=$input_params{'order'};4816if(defined$order&&$order!~m/none|project|descr|owner|age/) {4817 die_error(400,"Unknown order parameter");4818}48194820my@list= git_get_projects_list($project);4821if(!@list) {4822 die_error(404,"No forks found");4823}48244825 git_header_html();4826 git_print_page_nav('','');4827 git_print_header_div('summary',"$projectforks");4828 git_project_list_body(\@list,$order);4829 git_footer_html();4830}48314832sub git_project_index {4833my@projects= git_get_projects_list($project);48344835print$cgi->header(4836-type =>'text/plain',4837-charset =>'utf-8',4838-content_disposition =>'inline; filename="index.aux"');48394840foreachmy$pr(@projects) {4841if(!exists$pr->{'owner'}) {4842$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4843}48444845my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4846# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4847$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4848$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4849$path=~s/ /\+/g;4850$owner=~s/ /\+/g;48514852print"$path$owner\n";4853}4854}48554856sub git_summary {4857my$descr= git_get_project_description($project) ||"none";4858my%co= parse_commit("HEAD");4859my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4860my$head=$co{'id'};48614862my$owner= git_get_project_owner($project);48634864my$refs= git_get_references();4865# These get_*_list functions return one more to allow us to see if4866# there are more ...4867my@taglist= git_get_tags_list(16);4868my@headlist= git_get_heads_list(16);4869my@forklist;4870my$check_forks= gitweb_check_feature('forks');48714872if($check_forks) {4873@forklist= git_get_projects_list($project);4874}48754876 git_header_html();4877 git_print_page_nav('summary','',$head);48784879print"<div class=\"title\"> </div>\n";4880print"<table class=\"projects_list\">\n".4881"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4882"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4883if(defined$cd{'rfc2822'}) {4884print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4885}48864887# use per project git URL list in $projectroot/$project/cloneurl4888# or make project git URL from git base URL and project name4889my$url_tag="URL";4890my@url_list= git_get_project_url_list($project);4891@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4892foreachmy$git_url(@url_list) {4893next unless$git_url;4894print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4895$url_tag="";4896}48974898# Tag cloud4899my$show_ctags= gitweb_check_feature('ctags');4900if($show_ctags) {4901my$ctags= git_get_project_ctags($project);4902my$cloud= git_populate_project_tagcloud($ctags);4903print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4904print"</td>\n<td>"unless%$ctags;4905print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4906print"</td>\n<td>"if%$ctags;4907print git_show_project_tagcloud($cloud,48);4908print"</td></tr>";4909}49104911print"</table>\n";49124913# If XSS prevention is on, we don't include README.html.4914# TODO: Allow a readme in some safe format.4915if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4916print"<div class=\"title\">readme</div>\n".4917"<div class=\"readme\">\n";4918 insert_file("$projectroot/$project/README.html");4919print"\n</div>\n";# class="readme"4920}49214922# we need to request one more than 16 (0..15) to check if4923# those 16 are all4924my@commitlist=$head? parse_commits($head,17) : ();4925if(@commitlist) {4926 git_print_header_div('shortlog');4927 git_shortlog_body(\@commitlist,0,15,$refs,4928$#commitlist<=15?undef:4929$cgi->a({-href => href(action=>"shortlog")},"..."));4930}49314932if(@taglist) {4933 git_print_header_div('tags');4934 git_tags_body(\@taglist,0,15,4935$#taglist<=15?undef:4936$cgi->a({-href => href(action=>"tags")},"..."));4937}49384939if(@headlist) {4940 git_print_header_div('heads');4941 git_heads_body(\@headlist,$head,0,15,4942$#headlist<=15?undef:4943$cgi->a({-href => href(action=>"heads")},"..."));4944}49454946if(@forklist) {4947 git_print_header_div('forks');4948 git_project_list_body(\@forklist,'age',0,15,4949$#forklist<=15?undef:4950$cgi->a({-href => href(action=>"forks")},"..."),4951'no_header');4952}49534954 git_footer_html();4955}49564957sub git_tag {4958my$head= git_get_head_hash($project);4959 git_header_html();4960 git_print_page_nav('','',$head,undef,$head);4961my%tag= parse_tag($hash);49624963if(!%tag) {4964 die_error(404,"Unknown tag object");4965}49664967 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4968print"<div class=\"title_text\">\n".4969"<table class=\"object_header\">\n".4970"<tr>\n".4971"<td>object</td>\n".4972"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4973$tag{'object'}) ."</td>\n".4974"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4975$tag{'type'}) ."</td>\n".4976"</tr>\n";4977if(defined($tag{'author'})) {4978 git_print_authorship_rows(\%tag,'author');4979}4980print"</table>\n\n".4981"</div>\n";4982print"<div class=\"page_body\">";4983my$comment=$tag{'comment'};4984foreachmy$line(@$comment) {4985chomp$line;4986print esc_html($line, -nbsp=>1) ."<br/>\n";4987}4988print"</div>\n";4989 git_footer_html();4990}49914992sub git_blame_common {4993my$format=shift||'porcelain';4994if($formateq'porcelain'&&$cgi->param('js')) {4995$format='incremental';4996$action='blame_incremental';# for page title etc4997}49984999# permissions5000 gitweb_check_feature('blame')5001or die_error(403,"Blame view not allowed");50025003# error checking5004 die_error(400,"No file name given")unless$file_name;5005$hash_base||= git_get_head_hash($project);5006 die_error(404,"Couldn't find base commit")unless$hash_base;5007my%co= parse_commit($hash_base)5008or die_error(404,"Commit not found");5009my$ftype="blob";5010if(!defined$hash) {5011$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5012or die_error(404,"Error looking up file");5013}else{5014$ftype= git_get_type($hash);5015if($ftype!~"blob") {5016 die_error(400,"Object is not a blob");5017}5018}50195020my$fd;5021if($formateq'incremental') {5022# get file contents (as base)5023open$fd,"-|", git_cmd(),'cat-file','blob',$hash5024or die_error(500,"Open git-cat-file failed");5025}elsif($formateq'data') {5026# run git-blame --incremental5027open$fd,"-|", git_cmd(),"blame","--incremental",5028$hash_base,"--",$file_name5029or die_error(500,"Open git-blame --incremental failed");5030}else{5031# run git-blame --porcelain5032open$fd,"-|", git_cmd(),"blame",'-p',5033$hash_base,'--',$file_name5034or die_error(500,"Open git-blame --porcelain failed");5035}50365037# incremental blame data returns early5038if($formateq'data') {5039print$cgi->header(5040-type=>"text/plain", -charset =>"utf-8",5041-status=>"200 OK");5042local$| =1;# output autoflush5043printwhile<$fd>;5044close$fd5045or print"ERROR$!\n";50465047print'END';5048if(defined$t0&& gitweb_check_feature('timed')) {5049print' '.5050 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5051' '.$number_of_git_cmds;5052}5053print"\n";50545055return;5056}50575058# page header5059 git_header_html();5060my$formats_nav=5061$cgi->a({-href => href(action=>"blob", -replay=>1)},5062"blob") .5063" | ";5064if($formateq'incremental') {5065$formats_nav.=5066$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5067"blame") ." (non-incremental)";5068}else{5069$formats_nav.=5070$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5071"blame") ." (incremental)";5072}5073$formats_nav.=5074" | ".5075$cgi->a({-href => href(action=>"history", -replay=>1)},5076"history") .5077" | ".5078$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5079"HEAD");5080 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5081 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5082 git_print_page_path($file_name,$ftype,$hash_base);50835084# page body5085if($formateq'incremental') {5086print"<noscript>\n<div class=\"error\"><center><b>\n".5087"This page requires JavaScript to run.\nUse ".5088$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5089'this page').5090" instead.\n".5091"</b></center></div>\n</noscript>\n";50925093print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5094}50955096print qq!<div class="page_body">\n!;5097print qq!<div id="progress_info">.../ ...</div>\n!5098if($formateq'incremental');5099print qq!<table id="blame_table"class="blame" width="100%">\n!.5100#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5101 qq!<thead>\n!.5102 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5103 qq!</thead>\n!.5104 qq!<tbody>\n!;51055106my@rev_color=qw(light dark);5107my$num_colors=scalar(@rev_color);5108my$current_color=0;51095110if($formateq'incremental') {5111my$color_class=$rev_color[$current_color];51125113#contents of a file5114my$linenr=0;5115 LINE:5116while(my$line= <$fd>) {5117chomp$line;5118$linenr++;51195120print qq!<tr id="l$linenr"class="$color_class">!.5121 qq!<td class="sha1"><a href=""> </a></td>!.5122 qq!<td class="linenr">!.5123 qq!<a class="linenr" href="">$linenr</a></td>!;5124print qq!<td class="pre">! . esc_html($line) ."</td>\n";5125print qq!</tr>\n!;5126}51275128}else{# porcelain, i.e. ordinary blame5129my%metainfo= ();# saves information about commits51305131# blame data5132 LINE:5133while(my$line= <$fd>) {5134chomp$line;5135# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5136# no <lines in group> for subsequent lines in group of lines5137my($full_rev,$orig_lineno,$lineno,$group_size) =5138($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5139if(!exists$metainfo{$full_rev}) {5140$metainfo{$full_rev} = {'nprevious'=>0};5141}5142my$meta=$metainfo{$full_rev};5143my$data;5144while($data= <$fd>) {5145chomp$data;5146last if($data=~s/^\t//);# contents of line5147if($data=~/^(\S+)(?: (.*))?$/) {5148$meta->{$1} =$2unlessexists$meta->{$1};5149}5150if($data=~/^previous /) {5151$meta->{'nprevious'}++;5152}5153}5154my$short_rev=substr($full_rev,0,8);5155my$author=$meta->{'author'};5156my%date=5157 parse_date($meta->{'author-time'},$meta->{'author-tz'});5158my$date=$date{'iso-tz'};5159if($group_size) {5160$current_color= ($current_color+1) %$num_colors;5161}5162my$tr_class=$rev_color[$current_color];5163$tr_class.=' boundary'if(exists$meta->{'boundary'});5164$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5165$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5166print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5167if($group_size) {5168print"<td class=\"sha1\"";5169print" title=\"". esc_html($author) .",$date\"";5170print" rowspan=\"$group_size\""if($group_size>1);5171print">";5172print$cgi->a({-href => href(action=>"commit",5173 hash=>$full_rev,5174 file_name=>$file_name)},5175 esc_html($short_rev));5176if($group_size>=2) {5177my@author_initials= ($author=~/\b([[:upper:]])\B/g);5178if(@author_initials) {5179print"<br />".5180 esc_html(join('',@author_initials));5181# or join('.', ...)5182}5183}5184print"</td>\n";5185}5186# 'previous' <sha1 of parent commit> <filename at commit>5187if(exists$meta->{'previous'} &&5188$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5189$meta->{'parent'} =$1;5190$meta->{'file_parent'} = unquote($2);5191}5192my$linenr_commit=5193exists($meta->{'parent'}) ?5194$meta->{'parent'} :$full_rev;5195my$linenr_filename=5196exists($meta->{'file_parent'}) ?5197$meta->{'file_parent'} : unquote($meta->{'filename'});5198my$blamed= href(action =>'blame',5199 file_name =>$linenr_filename,5200 hash_base =>$linenr_commit);5201print"<td class=\"linenr\">";5202print$cgi->a({ -href =>"$blamed#l$orig_lineno",5203-class=>"linenr"},5204 esc_html($lineno));5205print"</td>";5206print"<td class=\"pre\">". esc_html($data) ."</td>\n";5207print"</tr>\n";5208}# end while52095210}52115212# footer5213print"</tbody>\n".5214"</table>\n";# class="blame"5215print"</div>\n";# class="blame_body"5216close$fd5217or print"Reading blob failed\n";52185219 git_footer_html();5220}52215222sub git_blame {5223 git_blame_common();5224}52255226sub git_blame_incremental {5227 git_blame_common('incremental');5228}52295230sub git_blame_data {5231 git_blame_common('data');5232}52335234sub git_tags {5235my$head= git_get_head_hash($project);5236 git_header_html();5237 git_print_page_nav('','',$head,undef,$head);5238 git_print_header_div('summary',$project);52395240my@tagslist= git_get_tags_list();5241if(@tagslist) {5242 git_tags_body(\@tagslist);5243}5244 git_footer_html();5245}52465247sub git_heads {5248my$head= git_get_head_hash($project);5249 git_header_html();5250 git_print_page_nav('','',$head,undef,$head);5251 git_print_header_div('summary',$project);52525253my@headslist= git_get_heads_list();5254if(@headslist) {5255 git_heads_body(\@headslist,$head);5256}5257 git_footer_html();5258}52595260sub git_blob_plain {5261my$type=shift;5262my$expires;52635264if(!defined$hash) {5265if(defined$file_name) {5266my$base=$hash_base|| git_get_head_hash($project);5267$hash= git_get_hash_by_path($base,$file_name,"blob")5268or die_error(404,"Cannot find file");5269}else{5270 die_error(400,"No file name defined");5271}5272}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5273# blobs defined by non-textual hash id's can be cached5274$expires="+1d";5275}52765277open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5278or die_error(500,"Open git-cat-file blob '$hash' failed");52795280# content-type (can include charset)5281$type= blob_contenttype($fd,$file_name,$type);52825283# "save as" filename, even when no $file_name is given5284my$save_as="$hash";5285if(defined$file_name) {5286$save_as=$file_name;5287}elsif($type=~m/^text\//) {5288$save_as.='.txt';5289}52905291# With XSS prevention on, blobs of all types except a few known safe5292# ones are served with "Content-Disposition: attachment" to make sure5293# they don't run in our security domain. For certain image types,5294# blob view writes an <img> tag referring to blob_plain view, and we5295# want to be sure not to break that by serving the image as an5296# attachment (though Firefox 3 doesn't seem to care).5297my$sandbox=$prevent_xss&&5298$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;52995300print$cgi->header(5301-type =>$type,5302-expires =>$expires,5303-content_disposition =>5304($sandbox?'attachment':'inline')5305.'; filename="'.$save_as.'"');5306local$/=undef;5307binmode STDOUT,':raw';5308print<$fd>;5309binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5310close$fd;5311}53125313sub git_blob {5314my$expires;53155316if(!defined$hash) {5317if(defined$file_name) {5318my$base=$hash_base|| git_get_head_hash($project);5319$hash= git_get_hash_by_path($base,$file_name,"blob")5320or die_error(404,"Cannot find file");5321}else{5322 die_error(400,"No file name defined");5323}5324}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5325# blobs defined by non-textual hash id's can be cached5326$expires="+1d";5327}53285329my$have_blame= gitweb_check_feature('blame');5330open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5331or die_error(500,"Couldn't cat$file_name,$hash");5332my$mimetype= blob_mimetype($fd,$file_name);5333if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5334close$fd;5335return git_blob_plain($mimetype);5336}5337# we can have blame only for text/* mimetype5338$have_blame&&= ($mimetype=~m!^text/!);53395340 git_header_html(undef,$expires);5341my$formats_nav='';5342if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5343if(defined$file_name) {5344if($have_blame) {5345$formats_nav.=5346$cgi->a({-href => href(action=>"blame", -replay=>1)},5347"blame") .5348" | ";5349}5350$formats_nav.=5351$cgi->a({-href => href(action=>"history", -replay=>1)},5352"history") .5353" | ".5354$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5355"raw") .5356" | ".5357$cgi->a({-href => href(action=>"blob",5358 hash_base=>"HEAD", file_name=>$file_name)},5359"HEAD");5360}else{5361$formats_nav.=5362$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5363"raw");5364}5365 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5366 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5367}else{5368print"<div class=\"page_nav\">\n".5369"<br/><br/></div>\n".5370"<div class=\"title\">$hash</div>\n";5371}5372 git_print_page_path($file_name,"blob",$hash_base);5373print"<div class=\"page_body\">\n";5374if($mimetype=~m!^image/!) {5375print qq!<img type="$mimetype"!;5376if($file_name) {5377print qq! alt="$file_name" title="$file_name"!;5378}5379print qq! src="! .5380 href(action=>"blob_plain", hash=>$hash,5381 hash_base=>$hash_base, file_name=>$file_name) .5382 qq!"/>\n!;5383}else{5384my$nr;5385while(my$line= <$fd>) {5386chomp$line;5387$nr++;5388$line= untabify($line);5389printf"<div class=\"pre\"><a id=\"l%i\"href=\"". href(-replay =>1)5390."#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5391$nr,$nr,$nr, esc_html($line, -nbsp=>1);5392}5393}5394close$fd5395or print"Reading blob failed.\n";5396print"</div>";5397 git_footer_html();5398}53995400sub git_tree {5401if(!defined$hash_base) {5402$hash_base="HEAD";5403}5404if(!defined$hash) {5405if(defined$file_name) {5406$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5407}else{5408$hash=$hash_base;5409}5410}5411 die_error(404,"No such tree")unlessdefined($hash);54125413my$show_sizes= gitweb_check_feature('show-sizes');5414my$have_blame= gitweb_check_feature('blame');54155416my@entries= ();5417{5418local$/="\0";5419open my$fd,"-|", git_cmd(),"ls-tree",'-z',5420($show_sizes?'-l': ()),@extra_options,$hash5421or die_error(500,"Open git-ls-tree failed");5422@entries=map{chomp;$_} <$fd>;5423close$fd5424or die_error(404,"Reading tree failed");5425}54265427my$refs= git_get_references();5428my$ref= format_ref_marker($refs,$hash_base);5429 git_header_html();5430my$basedir='';5431if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5432my@views_nav= ();5433if(defined$file_name) {5434push@views_nav,5435$cgi->a({-href => href(action=>"history", -replay=>1)},5436"history"),5437$cgi->a({-href => href(action=>"tree",5438 hash_base=>"HEAD", file_name=>$file_name)},5439"HEAD"),5440}5441my$snapshot_links= format_snapshot_links($hash);5442if(defined$snapshot_links) {5443# FIXME: Should be available when we have no hash base as well.5444push@views_nav,$snapshot_links;5445}5446 git_print_page_nav('tree','',$hash_base,undef,undef,5447join(' | ',@views_nav));5448 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5449}else{5450undef$hash_base;5451print"<div class=\"page_nav\">\n";5452print"<br/><br/></div>\n";5453print"<div class=\"title\">$hash</div>\n";5454}5455if(defined$file_name) {5456$basedir=$file_name;5457if($basedirne''&&substr($basedir, -1)ne'/') {5458$basedir.='/';5459}5460 git_print_page_path($file_name,'tree',$hash_base);5461}5462print"<div class=\"page_body\">\n";5463print"<table class=\"tree\">\n";5464my$alternate=1;5465# '..' (top directory) link if possible5466if(defined$hash_base&&5467defined$file_name&&$file_name=~m![^/]+$!) {5468if($alternate) {5469print"<tr class=\"dark\">\n";5470}else{5471print"<tr class=\"light\">\n";5472}5473$alternate^=1;54745475my$up=$file_name;5476$up=~s!/?[^/]+$!!;5477undef$upunless$up;5478# based on git_print_tree_entry5479print'<td class="mode">'. mode_str('040000') ."</td>\n";5480print'<td class="size"> </td>'."\n"if$show_sizes;5481print'<td class="list">';5482print$cgi->a({-href => href(action=>"tree",5483 hash_base=>$hash_base,5484 file_name=>$up)},5485"..");5486print"</td>\n";5487print"<td class=\"link\"></td>\n";54885489print"</tr>\n";5490}5491foreachmy$line(@entries) {5492my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);54935494if($alternate) {5495print"<tr class=\"dark\">\n";5496}else{5497print"<tr class=\"light\">\n";5498}5499$alternate^=1;55005501 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);55025503print"</tr>\n";5504}5505print"</table>\n".5506"</div>";5507 git_footer_html();5508}55095510sub snapshot_name {5511my($project,$hash) =@_;55125513# path/to/project.git -> project5514# path/to/project/.git -> project5515my$name= to_utf8($project);5516$name=~ s,([^/])/*\.git$,$1,;5517$name= basename($name);5518# sanitize name5519$name=~s/[[:cntrl:]]/?/g;55205521my$ver=$hash;5522if($hash=~/^[0-9a-fA-F]+$/) {5523# shorten SHA-1 hash5524my$full_hash= git_get_full_hash($project,$hash);5525if($full_hash=~/^$hash/&&length($hash) >7) {5526$ver= git_get_short_hash($project,$hash);5527}5528}elsif($hash=~m!^refs/tags/(.*)$!) {5529# tags don't need shortened SHA-1 hash5530$ver=$1;5531}else{5532# branches and other need shortened SHA-1 hash5533if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5534$ver=$1;5535}5536$ver.='-'. git_get_short_hash($project,$hash);5537}5538# in case of hierarchical branch names5539$ver=~s!/!.!g;55405541# name = project-version_string5542$name="$name-$ver";55435544returnwantarray? ($name,$name) :$name;5545}55465547sub git_snapshot {5548my$format=$input_params{'snapshot_format'};5549if(!@snapshot_fmts) {5550 die_error(403,"Snapshots not allowed");5551}5552# default to first supported snapshot format5553$format||=$snapshot_fmts[0];5554if($format!~m/^[a-z0-9]+$/) {5555 die_error(400,"Invalid snapshot format parameter");5556}elsif(!exists($known_snapshot_formats{$format})) {5557 die_error(400,"Unknown snapshot format");5558}elsif($known_snapshot_formats{$format}{'disabled'}) {5559 die_error(403,"Snapshot format not allowed");5560}elsif(!grep($_eq$format,@snapshot_fmts)) {5561 die_error(403,"Unsupported snapshot format");5562}55635564my$type= git_get_type("$hash^{}");5565if(!$type) {5566 die_error(404,'Object does not exist');5567}elsif($typeeq'blob') {5568 die_error(400,'Object is not a tree-ish');5569}55705571my($name,$prefix) = snapshot_name($project,$hash);5572my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5573my$cmd= quote_command(5574 git_cmd(),'archive',5575"--format=$known_snapshot_formats{$format}{'format'}",5576"--prefix=$prefix/",$hash);5577if(exists$known_snapshot_formats{$format}{'compressor'}) {5578$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5579}55805581$filename=~s/(["\\])/\\$1/g;5582print$cgi->header(5583-type =>$known_snapshot_formats{$format}{'type'},5584-content_disposition =>'inline; filename="'.$filename.'"',5585-status =>'200 OK');55865587open my$fd,"-|",$cmd5588or die_error(500,"Execute git-archive failed");5589binmode STDOUT,':raw';5590print<$fd>;5591binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5592close$fd;5593}55945595sub git_log_generic {5596my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;55975598my$head= git_get_head_hash($project);5599if(!defined$base) {5600$base=$head;5601}5602if(!defined$page) {5603$page=0;5604}5605my$refs= git_get_references();56065607my$commit_hash=$base;5608if(defined$parent) {5609$commit_hash="$parent..$base";5610}5611my@commitlist=5612 parse_commits($commit_hash,101, (100*$page),5613defined$file_name? ($file_name,"--full-history") : ());56145615my$ftype;5616if(!defined$file_hash&&defined$file_name) {5617# some commits could have deleted file in question,5618# and not have it in tree, but one of them has to have it5619for(my$i=0;$i<@commitlist;$i++) {5620$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5621last ifdefined$file_hash;5622}5623}5624if(defined$file_hash) {5625$ftype= git_get_type($file_hash);5626}5627if(defined$file_name&& !defined$ftype) {5628 die_error(500,"Unknown type of object");5629}5630my%co;5631if(defined$file_name) {5632%co= parse_commit($base)5633or die_error(404,"Unknown commit object");5634}563556365637my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5638my$next_link='';5639if($#commitlist>=100) {5640$next_link=5641$cgi->a({-href => href(-replay=>1, page=>$page+1),5642-accesskey =>"n", -title =>"Alt-n"},"next");5643}5644my$patch_max= gitweb_get_feature('patches');5645if($patch_max&& !defined$file_name) {5646if($patch_max<0||@commitlist<=$patch_max) {5647$paging_nav.=" ⋅ ".5648$cgi->a({-href => href(action=>"patches", -replay=>1)},5649"patches");5650}5651}56525653 git_header_html();5654 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5655if(defined$file_name) {5656 git_print_header_div('commit', esc_html($co{'title'}),$base);5657}else{5658 git_print_header_div('summary',$project)5659}5660 git_print_page_path($file_name,$ftype,$hash_base)5661if(defined$file_name);56625663$body_subr->(\@commitlist,0,99,$refs,$next_link,5664$file_name,$file_hash,$ftype);56655666 git_footer_html();5667}56685669sub git_log {5670 git_log_generic('log', \&git_log_body,5671$hash,$hash_parent);5672}56735674sub git_commit {5675$hash||=$hash_base||"HEAD";5676my%co= parse_commit($hash)5677or die_error(404,"Unknown commit object");56785679my$parent=$co{'parent'};5680my$parents=$co{'parents'};# listref56815682# we need to prepare $formats_nav before any parameter munging5683my$formats_nav;5684if(!defined$parent) {5685# --root commitdiff5686$formats_nav.='(initial)';5687}elsif(@$parents==1) {5688# single parent commit5689$formats_nav.=5690'(parent: '.5691$cgi->a({-href => href(action=>"commit",5692 hash=>$parent)},5693 esc_html(substr($parent,0,7))) .5694')';5695}else{5696# merge commit5697$formats_nav.=5698'(merge: '.5699join(' ',map{5700$cgi->a({-href => href(action=>"commit",5701 hash=>$_)},5702 esc_html(substr($_,0,7)));5703}@$parents) .5704')';5705}5706if(gitweb_check_feature('patches') &&@$parents<=1) {5707$formats_nav.=" | ".5708$cgi->a({-href => href(action=>"patch", -replay=>1)},5709"patch");5710}57115712if(!defined$parent) {5713$parent="--root";5714}5715my@difftree;5716open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5717@diff_opts,5718(@$parents<=1?$parent:'-c'),5719$hash,"--"5720or die_error(500,"Open git-diff-tree failed");5721@difftree=map{chomp;$_} <$fd>;5722close$fdor die_error(404,"Reading git-diff-tree failed");57235724# non-textual hash id's can be cached5725my$expires;5726if($hash=~m/^[0-9a-fA-F]{40}$/) {5727$expires="+1d";5728}5729my$refs= git_get_references();5730my$ref= format_ref_marker($refs,$co{'id'});57315732 git_header_html(undef,$expires);5733 git_print_page_nav('commit','',5734$hash,$co{'tree'},$hash,5735$formats_nav);57365737if(defined$co{'parent'}) {5738 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5739}else{5740 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5741}5742print"<div class=\"title_text\">\n".5743"<table class=\"object_header\">\n";5744 git_print_authorship_rows(\%co);5745print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5746print"<tr>".5747"<td>tree</td>".5748"<td class=\"sha1\">".5749$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5750class=>"list"},$co{'tree'}) .5751"</td>".5752"<td class=\"link\">".5753$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5754"tree");5755my$snapshot_links= format_snapshot_links($hash);5756if(defined$snapshot_links) {5757print" | ".$snapshot_links;5758}5759print"</td>".5760"</tr>\n";57615762foreachmy$par(@$parents) {5763print"<tr>".5764"<td>parent</td>".5765"<td class=\"sha1\">".5766$cgi->a({-href => href(action=>"commit", hash=>$par),5767class=>"list"},$par) .5768"</td>".5769"<td class=\"link\">".5770$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5771" | ".5772$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5773"</td>".5774"</tr>\n";5775}5776print"</table>".5777"</div>\n";57785779print"<div class=\"page_body\">\n";5780 git_print_log($co{'comment'});5781print"</div>\n";57825783 git_difftree_body(\@difftree,$hash,@$parents);57845785 git_footer_html();5786}57875788sub git_object {5789# object is defined by:5790# - hash or hash_base alone5791# - hash_base and file_name5792my$type;57935794# - hash or hash_base alone5795if($hash|| ($hash_base&& !defined$file_name)) {5796my$object_id=$hash||$hash_base;57975798open my$fd,"-|", quote_command(5799 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5800or die_error(404,"Object does not exist");5801$type= <$fd>;5802chomp$type;5803close$fd5804or die_error(404,"Object does not exist");58055806# - hash_base and file_name5807}elsif($hash_base&&defined$file_name) {5808$file_name=~ s,/+$,,;58095810system(git_cmd(),"cat-file",'-e',$hash_base) ==05811or die_error(404,"Base object does not exist");58125813# here errors should not hapen5814open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5815or die_error(500,"Open git-ls-tree failed");5816my$line= <$fd>;5817close$fd;58185819#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5820unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5821 die_error(404,"File or directory for given base does not exist");5822}5823$type=$2;5824$hash=$3;5825}else{5826 die_error(400,"Not enough information to find object");5827}58285829print$cgi->redirect(-uri => href(action=>$type, -full=>1,5830 hash=>$hash, hash_base=>$hash_base,5831 file_name=>$file_name),5832-status =>'302 Found');5833}58345835sub git_blobdiff {5836my$format=shift||'html';58375838my$fd;5839my@difftree;5840my%diffinfo;5841my$expires;58425843# preparing $fd and %diffinfo for git_patchset_body5844# new style URI5845if(defined$hash_base&&defined$hash_parent_base) {5846if(defined$file_name) {5847# read raw output5848open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5849$hash_parent_base,$hash_base,5850"--", (defined$file_parent?$file_parent: ()),$file_name5851or die_error(500,"Open git-diff-tree failed");5852@difftree=map{chomp;$_} <$fd>;5853close$fd5854or die_error(404,"Reading git-diff-tree failed");5855@difftree5856or die_error(404,"Blob diff not found");58575858}elsif(defined$hash&&5859$hash=~/[0-9a-fA-F]{40}/) {5860# try to find filename from $hash58615862# read filtered raw output5863open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5864$hash_parent_base,$hash_base,"--"5865or die_error(500,"Open git-diff-tree failed");5866@difftree=5867# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5868# $hash == to_id5869grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5870map{chomp;$_} <$fd>;5871close$fd5872or die_error(404,"Reading git-diff-tree failed");5873@difftree5874or die_error(404,"Blob diff not found");58755876}else{5877 die_error(400,"Missing one of the blob diff parameters");5878}58795880if(@difftree>1) {5881 die_error(400,"Ambiguous blob diff specification");5882}58835884%diffinfo= parse_difftree_raw_line($difftree[0]);5885$file_parent||=$diffinfo{'from_file'} ||$file_name;5886$file_name||=$diffinfo{'to_file'};58875888$hash_parent||=$diffinfo{'from_id'};5889$hash||=$diffinfo{'to_id'};58905891# non-textual hash id's can be cached5892if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5893$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5894$expires='+1d';5895}58965897# open patch output5898open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5899'-p', ($formateq'html'?"--full-index": ()),5900$hash_parent_base,$hash_base,5901"--", (defined$file_parent?$file_parent: ()),$file_name5902or die_error(500,"Open git-diff-tree failed");5903}59045905# old/legacy style URI -- not generated anymore since 1.4.3.5906if(!%diffinfo) {5907 die_error('404 Not Found',"Missing one of the blob diff parameters")5908}59095910# header5911if($formateq'html') {5912my$formats_nav=5913$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5914"raw");5915 git_header_html(undef,$expires);5916if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5917 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5918 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5919}else{5920print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5921print"<div class=\"title\">$hashvs$hash_parent</div>\n";5922}5923if(defined$file_name) {5924 git_print_page_path($file_name,"blob",$hash_base);5925}else{5926print"<div class=\"page_path\"></div>\n";5927}59285929}elsif($formateq'plain') {5930print$cgi->header(5931-type =>'text/plain',5932-charset =>'utf-8',5933-expires =>$expires,5934-content_disposition =>'inline; filename="'."$file_name".'.patch"');59355936print"X-Git-Url: ".$cgi->self_url() ."\n\n";59375938}else{5939 die_error(400,"Unknown blobdiff format");5940}59415942# patch5943if($formateq'html') {5944print"<div class=\"page_body\">\n";59455946 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5947close$fd;59485949print"</div>\n";# class="page_body"5950 git_footer_html();59515952}else{5953while(my$line= <$fd>) {5954$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5955$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;59565957print$line;59585959last if$line=~m!^\+\+\+!;5960}5961local$/=undef;5962print<$fd>;5963close$fd;5964}5965}59665967sub git_blobdiff_plain {5968 git_blobdiff('plain');5969}59705971sub git_commitdiff {5972my%params=@_;5973my$format=$params{-format} ||'html';59745975my($patch_max) = gitweb_get_feature('patches');5976if($formateq'patch') {5977 die_error(403,"Patch view not allowed")unless$patch_max;5978}59795980$hash||=$hash_base||"HEAD";5981my%co= parse_commit($hash)5982or die_error(404,"Unknown commit object");59835984# choose format for commitdiff for merge5985if(!defined$hash_parent&& @{$co{'parents'}} >1) {5986$hash_parent='--cc';5987}5988# we need to prepare $formats_nav before almost any parameter munging5989my$formats_nav;5990if($formateq'html') {5991$formats_nav=5992$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5993"raw");5994if($patch_max&& @{$co{'parents'}} <=1) {5995$formats_nav.=" | ".5996$cgi->a({-href => href(action=>"patch", -replay=>1)},5997"patch");5998}59996000if(defined$hash_parent&&6001$hash_parentne'-c'&&$hash_parentne'--cc') {6002# commitdiff with two commits given6003my$hash_parent_short=$hash_parent;6004if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6005$hash_parent_short=substr($hash_parent,0,7);6006}6007$formats_nav.=6008' (from';6009for(my$i=0;$i< @{$co{'parents'}};$i++) {6010if($co{'parents'}[$i]eq$hash_parent) {6011$formats_nav.=' parent '. ($i+1);6012last;6013}6014}6015$formats_nav.=': '.6016$cgi->a({-href => href(action=>"commitdiff",6017 hash=>$hash_parent)},6018 esc_html($hash_parent_short)) .6019')';6020}elsif(!$co{'parent'}) {6021# --root commitdiff6022$formats_nav.=' (initial)';6023}elsif(scalar@{$co{'parents'}} ==1) {6024# single parent commit6025$formats_nav.=6026' (parent: '.6027$cgi->a({-href => href(action=>"commitdiff",6028 hash=>$co{'parent'})},6029 esc_html(substr($co{'parent'},0,7))) .6030')';6031}else{6032# merge commit6033if($hash_parenteq'--cc') {6034$formats_nav.=' | '.6035$cgi->a({-href => href(action=>"commitdiff",6036 hash=>$hash, hash_parent=>'-c')},6037'combined');6038}else{# $hash_parent eq '-c'6039$formats_nav.=' | '.6040$cgi->a({-href => href(action=>"commitdiff",6041 hash=>$hash, hash_parent=>'--cc')},6042'compact');6043}6044$formats_nav.=6045' (merge: '.6046join(' ',map{6047$cgi->a({-href => href(action=>"commitdiff",6048 hash=>$_)},6049 esc_html(substr($_,0,7)));6050} @{$co{'parents'}} ) .6051')';6052}6053}60546055my$hash_parent_param=$hash_parent;6056if(!defined$hash_parent_param) {6057# --cc for multiple parents, --root for parentless6058$hash_parent_param=6059@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6060}60616062# read commitdiff6063my$fd;6064my@difftree;6065if($formateq'html') {6066open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6067"--no-commit-id","--patch-with-raw","--full-index",6068$hash_parent_param,$hash,"--"6069or die_error(500,"Open git-diff-tree failed");60706071while(my$line= <$fd>) {6072chomp$line;6073# empty line ends raw part of diff-tree output6074last unless$line;6075push@difftree,scalar parse_difftree_raw_line($line);6076}60776078}elsif($formateq'plain') {6079open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6080'-p',$hash_parent_param,$hash,"--"6081or die_error(500,"Open git-diff-tree failed");6082}elsif($formateq'patch') {6083# For commit ranges, we limit the output to the number of6084# patches specified in the 'patches' feature.6085# For single commits, we limit the output to a single patch,6086# diverging from the git-format-patch default.6087my@commit_spec= ();6088if($hash_parent) {6089if($patch_max>0) {6090push@commit_spec,"-$patch_max";6091}6092push@commit_spec,'-n',"$hash_parent..$hash";6093}else{6094if($params{-single}) {6095push@commit_spec,'-1';6096}else{6097if($patch_max>0) {6098push@commit_spec,"-$patch_max";6099}6100push@commit_spec,"-n";6101}6102push@commit_spec,'--root',$hash;6103}6104open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',6105'--stdout',@commit_spec6106or die_error(500,"Open git-format-patch failed");6107}else{6108 die_error(400,"Unknown commitdiff format");6109}61106111# non-textual hash id's can be cached6112my$expires;6113if($hash=~m/^[0-9a-fA-F]{40}$/) {6114$expires="+1d";6115}61166117# write commit message6118if($formateq'html') {6119my$refs= git_get_references();6120my$ref= format_ref_marker($refs,$co{'id'});61216122 git_header_html(undef,$expires);6123 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6124 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6125print"<div class=\"title_text\">\n".6126"<table class=\"object_header\">\n";6127 git_print_authorship_rows(\%co);6128print"</table>".6129"</div>\n";6130print"<div class=\"page_body\">\n";6131if(@{$co{'comment'}} >1) {6132print"<div class=\"log\">\n";6133 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6134print"</div>\n";# class="log"6135}61366137}elsif($formateq'plain') {6138my$refs= git_get_references("tags");6139my$tagname= git_get_rev_name_tags($hash);6140my$filename= basename($project) ."-$hash.patch";61416142print$cgi->header(6143-type =>'text/plain',6144-charset =>'utf-8',6145-expires =>$expires,6146-content_disposition =>'inline; filename="'."$filename".'"');6147my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6148print"From: ". to_utf8($co{'author'}) ."\n";6149print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6150print"Subject: ". to_utf8($co{'title'}) ."\n";61516152print"X-Git-Tag:$tagname\n"if$tagname;6153print"X-Git-Url: ".$cgi->self_url() ."\n\n";61546155foreachmy$line(@{$co{'comment'}}) {6156print to_utf8($line) ."\n";6157}6158print"---\n\n";6159}elsif($formateq'patch') {6160my$filename= basename($project) ."-$hash.patch";61616162print$cgi->header(6163-type =>'text/plain',6164-charset =>'utf-8',6165-expires =>$expires,6166-content_disposition =>'inline; filename="'."$filename".'"');6167}61686169# write patch6170if($formateq'html') {6171my$use_parents= !defined$hash_parent||6172$hash_parenteq'-c'||$hash_parenteq'--cc';6173 git_difftree_body(\@difftree,$hash,6174$use_parents? @{$co{'parents'}} :$hash_parent);6175print"<br/>\n";61766177 git_patchset_body($fd, \@difftree,$hash,6178$use_parents? @{$co{'parents'}} :$hash_parent);6179close$fd;6180print"</div>\n";# class="page_body"6181 git_footer_html();61826183}elsif($formateq'plain') {6184local$/=undef;6185print<$fd>;6186close$fd6187or print"Reading git-diff-tree failed\n";6188}elsif($formateq'patch') {6189local$/=undef;6190print<$fd>;6191close$fd6192or print"Reading git-format-patch failed\n";6193}6194}61956196sub git_commitdiff_plain {6197 git_commitdiff(-format =>'plain');6198}61996200# format-patch-style patches6201sub git_patch {6202 git_commitdiff(-format =>'patch', -single =>1);6203}62046205sub git_patches {6206 git_commitdiff(-format =>'patch');6207}62086209sub git_history {6210 git_log_generic('history', \&git_history_body,6211$hash_base,$hash_parent_base,6212$file_name,$hash);6213}62146215sub git_search {6216 gitweb_check_feature('search')or die_error(403,"Search is disabled");6217if(!defined$searchtext) {6218 die_error(400,"Text field is empty");6219}6220if(!defined$hash) {6221$hash= git_get_head_hash($project);6222}6223my%co= parse_commit($hash);6224if(!%co) {6225 die_error(404,"Unknown commit object");6226}6227if(!defined$page) {6228$page=0;6229}62306231$searchtype||='commit';6232if($searchtypeeq'pickaxe') {6233# pickaxe may take all resources of your box and run for several minutes6234# with every query - so decide by yourself how public you make this feature6235 gitweb_check_feature('pickaxe')6236or die_error(403,"Pickaxe is disabled");6237}6238if($searchtypeeq'grep') {6239 gitweb_check_feature('grep')6240or die_error(403,"Grep is disabled");6241}62426243 git_header_html();62446245if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6246my$greptype;6247if($searchtypeeq'commit') {6248$greptype="--grep=";6249}elsif($searchtypeeq'author') {6250$greptype="--author=";6251}elsif($searchtypeeq'committer') {6252$greptype="--committer=";6253}6254$greptype.=$searchtext;6255my@commitlist= parse_commits($hash,101, (100*$page),undef,6256$greptype,'--regexp-ignore-case',6257$search_use_regexp?'--extended-regexp':'--fixed-strings');62586259my$paging_nav='';6260if($page>0) {6261$paging_nav.=6262$cgi->a({-href => href(action=>"search", hash=>$hash,6263 searchtext=>$searchtext,6264 searchtype=>$searchtype)},6265"first");6266$paging_nav.=" ⋅ ".6267$cgi->a({-href => href(-replay=>1, page=>$page-1),6268-accesskey =>"p", -title =>"Alt-p"},"prev");6269}else{6270$paging_nav.="first";6271$paging_nav.=" ⋅ prev";6272}6273my$next_link='';6274if($#commitlist>=100) {6275$next_link=6276$cgi->a({-href => href(-replay=>1, page=>$page+1),6277-accesskey =>"n", -title =>"Alt-n"},"next");6278$paging_nav.=" ⋅$next_link";6279}else{6280$paging_nav.=" ⋅ next";6281}62826283if($#commitlist>=100) {6284}62856286 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6287 git_print_header_div('commit', esc_html($co{'title'}),$hash);6288 git_search_grep_body(\@commitlist,0,99,$next_link);6289}62906291if($searchtypeeq'pickaxe') {6292 git_print_page_nav('','',$hash,$co{'tree'},$hash);6293 git_print_header_div('commit', esc_html($co{'title'}),$hash);62946295print"<table class=\"pickaxe search\">\n";6296my$alternate=1;6297local$/="\n";6298open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6299'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6300($search_use_regexp?'--pickaxe-regex': ());6301undef%co;6302my@files;6303while(my$line= <$fd>) {6304chomp$line;6305next unless$line;63066307my%set= parse_difftree_raw_line($line);6308if(defined$set{'commit'}) {6309# finish previous commit6310if(%co) {6311print"</td>\n".6312"<td class=\"link\">".6313$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6314" | ".6315$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6316print"</td>\n".6317"</tr>\n";6318}63196320if($alternate) {6321print"<tr class=\"dark\">\n";6322}else{6323print"<tr class=\"light\">\n";6324}6325$alternate^=1;6326%co= parse_commit($set{'commit'});6327my$author= chop_and_escape_str($co{'author_name'},15,5);6328print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6329"<td><i>$author</i></td>\n".6330"<td>".6331$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6332-class=>"list subject"},6333 chop_and_escape_str($co{'title'},50) ."<br/>");6334}elsif(defined$set{'to_id'}) {6335next if($set{'to_id'} =~m/^0{40}$/);63366337print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6338 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6339-class=>"list"},6340"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6341"<br/>\n";6342}6343}6344close$fd;63456346# finish last commit (warning: repetition!)6347if(%co) {6348print"</td>\n".6349"<td class=\"link\">".6350$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6351" | ".6352$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6353print"</td>\n".6354"</tr>\n";6355}63566357print"</table>\n";6358}63596360if($searchtypeeq'grep') {6361 git_print_page_nav('','',$hash,$co{'tree'},$hash);6362 git_print_header_div('commit', esc_html($co{'title'}),$hash);63636364print"<table class=\"grep_search\">\n";6365my$alternate=1;6366my$matches=0;6367local$/="\n";6368open my$fd,"-|", git_cmd(),'grep','-n',6369$search_use_regexp? ('-E','-i') :'-F',6370$searchtext,$co{'tree'};6371my$lastfile='';6372while(my$line= <$fd>) {6373chomp$line;6374my($file,$lno,$ltext,$binary);6375last if($matches++>1000);6376if($line=~/^Binary file (.+) matches$/) {6377$file=$1;6378$binary=1;6379}else{6380(undef,$file,$lno,$ltext) =split(/:/,$line,4);6381}6382if($filene$lastfile) {6383$lastfileand print"</td></tr>\n";6384if($alternate++) {6385print"<tr class=\"dark\">\n";6386}else{6387print"<tr class=\"light\">\n";6388}6389print"<td class=\"list\">".6390$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6391 file_name=>"$file"),6392-class=>"list"}, esc_path($file));6393print"</td><td>\n";6394$lastfile=$file;6395}6396if($binary) {6397print"<div class=\"binary\">Binary file</div>\n";6398}else{6399$ltext= untabify($ltext);6400if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6401$ltext= esc_html($1, -nbsp=>1);6402$ltext.='<span class="match">';6403$ltext.= esc_html($2, -nbsp=>1);6404$ltext.='</span>';6405$ltext.= esc_html($3, -nbsp=>1);6406}else{6407$ltext= esc_html($ltext, -nbsp=>1);6408}6409print"<div class=\"pre\">".6410$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6411 file_name=>"$file").'#l'.$lno,6412-class=>"linenr"},sprintf('%4i',$lno))6413.' '.$ltext."</div>\n";6414}6415}6416if($lastfile) {6417print"</td></tr>\n";6418if($matches>1000) {6419print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6420}6421}else{6422print"<div class=\"diff nodifferences\">No matches found</div>\n";6423}6424close$fd;64256426print"</table>\n";6427}6428 git_footer_html();6429}64306431sub git_search_help {6432 git_header_html();6433 git_print_page_nav('','',$hash,$hash,$hash);6434print<<EOT;6435<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6436regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6437the pattern entered is recognized as the POSIX extended6438<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6439insensitive).</p>6440<dl>6441<dt><b>commit</b></dt>6442<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6443EOT6444my$have_grep= gitweb_check_feature('grep');6445if($have_grep) {6446print<<EOT;6447<dt><b>grep</b></dt>6448<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6449 a different one) are searched for the given pattern. On large trees, this search can take6450a while and put some strain on the server, so please use it with some consideration. Note that6451due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6452case-sensitive.</dd>6453EOT6454}6455print<<EOT;6456<dt><b>author</b></dt>6457<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6458<dt><b>committer</b></dt>6459<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6460EOT6461my$have_pickaxe= gitweb_check_feature('pickaxe');6462if($have_pickaxe) {6463print<<EOT;6464<dt><b>pickaxe</b></dt>6465<dd>All commits that caused the string to appear or disappear from any file (changes that6466added, removed or "modified" the string) will be listed. This search can take a while and6467takes a lot of strain on the server, so please use it wisely. Note that since you may be6468interested even in changes just changing the case as well, this search is case sensitive.</dd>6469EOT6470}6471print"</dl>\n";6472 git_footer_html();6473}64746475sub git_shortlog {6476 git_log_generic('shortlog', \&git_shortlog_body,6477$hash,$hash_parent);6478}64796480## ......................................................................6481## feeds (RSS, Atom; OPML)64826483sub git_feed {6484my$format=shift||'atom';6485my$have_blame= gitweb_check_feature('blame');64866487# Atom: http://www.atomenabled.org/developers/syndication/6488# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6489if($formatne'rss'&&$formatne'atom') {6490 die_error(400,"Unknown web feed format");6491}64926493# log/feed of current (HEAD) branch, log of given branch, history of file/directory6494my$head=$hash||'HEAD';6495my@commitlist= parse_commits($head,150,0,$file_name);64966497my%latest_commit;6498my%latest_date;6499my$content_type="application/$format+xml";6500if(defined$cgi->http('HTTP_ACCEPT') &&6501$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6502# browser (feed reader) prefers text/xml6503$content_type='text/xml';6504}6505if(defined($commitlist[0])) {6506%latest_commit= %{$commitlist[0]};6507my$latest_epoch=$latest_commit{'committer_epoch'};6508%latest_date= parse_date($latest_epoch);6509my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6510if(defined$if_modified) {6511my$since;6512if(eval{require HTTP::Date;1; }) {6513$since= HTTP::Date::str2time($if_modified);6514}elsif(eval{require Time::ParseDate;1; }) {6515$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6516}6517if(defined$since&&$latest_epoch<=$since) {6518print$cgi->header(6519-type =>$content_type,6520-charset =>'utf-8',6521-last_modified =>$latest_date{'rfc2822'},6522-status =>'304 Not Modified');6523return;6524}6525}6526print$cgi->header(6527-type =>$content_type,6528-charset =>'utf-8',6529-last_modified =>$latest_date{'rfc2822'});6530}else{6531print$cgi->header(6532-type =>$content_type,6533-charset =>'utf-8');6534}65356536# Optimization: skip generating the body if client asks only6537# for Last-Modified date.6538return if($cgi->request_method()eq'HEAD');65396540# header variables6541my$title="$site_name-$project/$action";6542my$feed_type='log';6543if(defined$hash) {6544$title.=" - '$hash'";6545$feed_type='branch log';6546if(defined$file_name) {6547$title.=" ::$file_name";6548$feed_type='history';6549}6550}elsif(defined$file_name) {6551$title.=" -$file_name";6552$feed_type='history';6553}6554$title.="$feed_type";6555my$descr= git_get_project_description($project);6556if(defined$descr) {6557$descr= esc_html($descr);6558}else{6559$descr="$project".6560($formateq'rss'?'RSS':'Atom') .6561" feed";6562}6563my$owner= git_get_project_owner($project);6564$owner= esc_html($owner);65656566#header6567my$alt_url;6568if(defined$file_name) {6569$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6570}elsif(defined$hash) {6571$alt_url= href(-full=>1, action=>"log", hash=>$hash);6572}else{6573$alt_url= href(-full=>1, action=>"summary");6574}6575print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6576if($formateq'rss') {6577print<<XML;6578<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6579<channel>6580XML6581print"<title>$title</title>\n".6582"<link>$alt_url</link>\n".6583"<description>$descr</description>\n".6584"<language>en</language>\n".6585# project owner is responsible for 'editorial' content6586"<managingEditor>$owner</managingEditor>\n";6587if(defined$logo||defined$favicon) {6588# prefer the logo to the favicon, since RSS6589# doesn't allow both6590my$img= esc_url($logo||$favicon);6591print"<image>\n".6592"<url>$img</url>\n".6593"<title>$title</title>\n".6594"<link>$alt_url</link>\n".6595"</image>\n";6596}6597if(%latest_date) {6598print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6599print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6600}6601print"<generator>gitweb v.$version/$git_version</generator>\n";6602}elsif($formateq'atom') {6603print<<XML;6604<feed xmlns="http://www.w3.org/2005/Atom">6605XML6606print"<title>$title</title>\n".6607"<subtitle>$descr</subtitle>\n".6608'<link rel="alternate" type="text/html" href="'.6609$alt_url.'" />'."\n".6610'<link rel="self" type="'.$content_type.'" href="'.6611$cgi->self_url() .'" />'."\n".6612"<id>". href(-full=>1) ."</id>\n".6613# use project owner for feed author6614"<author><name>$owner</name></author>\n";6615if(defined$favicon) {6616print"<icon>". esc_url($favicon) ."</icon>\n";6617}6618if(defined$logo_url) {6619# not twice as wide as tall: 72 x 27 pixels6620print"<logo>". esc_url($logo) ."</logo>\n";6621}6622if(!%latest_date) {6623# dummy date to keep the feed valid until commits trickle in:6624print"<updated>1970-01-01T00:00:00Z</updated>\n";6625}else{6626print"<updated>$latest_date{'iso-8601'}</updated>\n";6627}6628print"<generator version='$version/$git_version'>gitweb</generator>\n";6629}66306631# contents6632for(my$i=0;$i<=$#commitlist;$i++) {6633my%co= %{$commitlist[$i]};6634my$commit=$co{'id'};6635# we read 150, we always show 30 and the ones more recent than 48 hours6636if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6637last;6638}6639my%cd= parse_date($co{'author_epoch'});66406641# get list of changed files6642open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6643$co{'parent'} ||"--root",6644$co{'id'},"--", (defined$file_name?$file_name: ())6645ornext;6646my@difftree=map{chomp;$_} <$fd>;6647close$fd6648ornext;66496650# print element (entry, item)6651my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6652if($formateq'rss') {6653print"<item>\n".6654"<title>". esc_html($co{'title'}) ."</title>\n".6655"<author>". esc_html($co{'author'}) ."</author>\n".6656"<pubDate>$cd{'rfc2822'}</pubDate>\n".6657"<guid isPermaLink=\"true\">$co_url</guid>\n".6658"<link>$co_url</link>\n".6659"<description>". esc_html($co{'title'}) ."</description>\n".6660"<content:encoded>".6661"<![CDATA[\n";6662}elsif($formateq'atom') {6663print"<entry>\n".6664"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6665"<updated>$cd{'iso-8601'}</updated>\n".6666"<author>\n".6667" <name>". esc_html($co{'author_name'}) ."</name>\n";6668if($co{'author_email'}) {6669print" <email>". esc_html($co{'author_email'}) ."</email>\n";6670}6671print"</author>\n".6672# use committer for contributor6673"<contributor>\n".6674" <name>". esc_html($co{'committer_name'}) ."</name>\n";6675if($co{'committer_email'}) {6676print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6677}6678print"</contributor>\n".6679"<published>$cd{'iso-8601'}</published>\n".6680"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6681"<id>$co_url</id>\n".6682"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6683"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6684}6685my$comment=$co{'comment'};6686print"<pre>\n";6687foreachmy$line(@$comment) {6688$line= esc_html($line);6689print"$line\n";6690}6691print"</pre><ul>\n";6692foreachmy$difftree_line(@difftree) {6693my%difftree= parse_difftree_raw_line($difftree_line);6694next if!$difftree{'from_id'};66956696my$file=$difftree{'file'} ||$difftree{'to_file'};66976698print"<li>".6699"[".6700$cgi->a({-href => href(-full=>1, action=>"blobdiff",6701 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6702 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6703 file_name=>$file, file_parent=>$difftree{'from_file'}),6704-title =>"diff"},'D');6705if($have_blame) {6706print$cgi->a({-href => href(-full=>1, action=>"blame",6707 file_name=>$file, hash_base=>$commit),6708-title =>"blame"},'B');6709}6710# if this is not a feed of a file history6711if(!defined$file_name||$file_namene$file) {6712print$cgi->a({-href => href(-full=>1, action=>"history",6713 file_name=>$file, hash=>$commit),6714-title =>"history"},'H');6715}6716$file= esc_path($file);6717print"] ".6718"$file</li>\n";6719}6720if($formateq'rss') {6721print"</ul>]]>\n".6722"</content:encoded>\n".6723"</item>\n";6724}elsif($formateq'atom') {6725print"</ul>\n</div>\n".6726"</content>\n".6727"</entry>\n";6728}6729}67306731# end of feed6732if($formateq'rss') {6733print"</channel>\n</rss>\n";6734}elsif($formateq'atom') {6735print"</feed>\n";6736}6737}67386739sub git_rss {6740 git_feed('rss');6741}67426743sub git_atom {6744 git_feed('atom');6745}67466747sub git_opml {6748my@list= git_get_projects_list();67496750print$cgi->header(6751-type =>'text/xml',6752-charset =>'utf-8',6753-content_disposition =>'inline; filename="opml.xml"');67546755print<<XML;6756<?xml version="1.0" encoding="utf-8"?>6757<opml version="1.0">6758<head>6759 <title>$site_nameOPML Export</title>6760</head>6761<body>6762<outline text="git RSS feeds">6763XML67646765foreachmy$pr(@list) {6766my%proj=%$pr;6767my$head= git_get_head_hash($proj{'path'});6768if(!defined$head) {6769next;6770}6771$git_dir="$projectroot/$proj{'path'}";6772my%co= parse_commit($head);6773if(!%co) {6774next;6775}67766777my$path= esc_html(chop_str($proj{'path'},25,5));6778my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6779my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6780print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6781}6782print<<XML;6783</outline>6784</body>6785</opml>6786XML6787}