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++"; 553our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 554# die if there are errors parsing config file 555if(-e $GITWEB_CONFIG) { 556do$GITWEB_CONFIG; 557die$@if$@; 558}elsif(-e $GITWEB_CONFIG_SYSTEM) { 559do$GITWEB_CONFIG_SYSTEM; 560die$@if$@; 561} 562 563# Get loadavg of system, to compare against $maxload. 564# Currently it requires '/proc/loadavg' present to get loadavg; 565# if it is not present it returns 0, which means no load checking. 566sub get_loadavg { 567if( -e '/proc/loadavg'){ 568open my$fd,'<','/proc/loadavg' 569orreturn0; 570my@load=split(/\s+/,scalar<$fd>); 571close$fd; 572 573# The first three columns measure CPU and IO utilization of the last one, 574# five, and 10 minute periods. The fourth column shows the number of 575# currently running processes and the total number of processes in the m/n 576# format. The last column displays the last process ID used. 577return$load[0] ||0; 578} 579# additional checks for load average should go here for things that don't export 580# /proc/loadavg 581 582return0; 583} 584 585# version of the core git binary 586our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 587$number_of_git_cmds++; 588 589$projects_list||=$projectroot; 590 591if(defined$maxload&& get_loadavg() >$maxload) { 592 die_error(503,"The load average on the server is too high"); 593} 594 595# ====================================================================== 596# input validation and dispatch 597 598# input parameters can be collected from a variety of sources (presently, CGI 599# and PATH_INFO), so we define an %input_params hash that collects them all 600# together during validation: this allows subsequent uses (e.g. href()) to be 601# agnostic of the parameter origin 602 603our%input_params= (); 604 605# input parameters are stored with the long parameter name as key. This will 606# also be used in the href subroutine to convert parameters to their CGI 607# equivalent, and since the href() usage is the most frequent one, we store 608# the name -> CGI key mapping here, instead of the reverse. 609# 610# XXX: Warning: If you touch this, check the search form for updating, 611# too. 612 613our@cgi_param_mapping= ( 614 project =>"p", 615 action =>"a", 616 file_name =>"f", 617 file_parent =>"fp", 618 hash =>"h", 619 hash_parent =>"hp", 620 hash_base =>"hb", 621 hash_parent_base =>"hpb", 622 page =>"pg", 623 order =>"o", 624 searchtext =>"s", 625 searchtype =>"st", 626 snapshot_format =>"sf", 627 extra_options =>"opt", 628 search_use_regexp =>"sr", 629# this must be last entry (for manipulation from JavaScript) 630 javascript =>"js" 631); 632our%cgi_param_mapping=@cgi_param_mapping; 633 634# we will also need to know the possible actions, for validation 635our%actions= ( 636"blame"=> \&git_blame, 637"blame_incremental"=> \&git_blame_incremental, 638"blame_data"=> \&git_blame_data, 639"blobdiff"=> \&git_blobdiff, 640"blobdiff_plain"=> \&git_blobdiff_plain, 641"blob"=> \&git_blob, 642"blob_plain"=> \&git_blob_plain, 643"commitdiff"=> \&git_commitdiff, 644"commitdiff_plain"=> \&git_commitdiff_plain, 645"commit"=> \&git_commit, 646"forks"=> \&git_forks, 647"heads"=> \&git_heads, 648"history"=> \&git_history, 649"log"=> \&git_log, 650"patch"=> \&git_patch, 651"patches"=> \&git_patches, 652"rss"=> \&git_rss, 653"atom"=> \&git_atom, 654"search"=> \&git_search, 655"search_help"=> \&git_search_help, 656"shortlog"=> \&git_shortlog, 657"summary"=> \&git_summary, 658"tag"=> \&git_tag, 659"tags"=> \&git_tags, 660"tree"=> \&git_tree, 661"snapshot"=> \&git_snapshot, 662"object"=> \&git_object, 663# those below don't need $project 664"opml"=> \&git_opml, 665"project_list"=> \&git_project_list, 666"project_index"=> \&git_project_index, 667); 668 669# finally, we have the hash of allowed extra_options for the commands that 670# allow them 671our%allowed_options= ( 672"--no-merges"=> [qw(rss atom log shortlog history)], 673); 674 675# fill %input_params with the CGI parameters. All values except for 'opt' 676# should be single values, but opt can be an array. We should probably 677# build an array of parameters that can be multi-valued, but since for the time 678# being it's only this one, we just single it out 679while(my($name,$symbol) =each%cgi_param_mapping) { 680if($symboleq'opt') { 681$input_params{$name} = [$cgi->param($symbol) ]; 682}else{ 683$input_params{$name} =$cgi->param($symbol); 684} 685} 686 687# now read PATH_INFO and update the parameter list for missing parameters 688sub evaluate_path_info { 689return ifdefined$input_params{'project'}; 690return if!$path_info; 691$path_info=~ s,^/+,,; 692return if!$path_info; 693 694# find which part of PATH_INFO is project 695my$project=$path_info; 696$project=~ s,/+$,,; 697while($project&& !check_head_link("$projectroot/$project")) { 698$project=~ s,/*[^/]*$,,; 699} 700return unless$project; 701$input_params{'project'} =$project; 702 703# do not change any parameters if an action is given using the query string 704return if$input_params{'action'}; 705$path_info=~ s,^\Q$project\E/*,,; 706 707# next, check if we have an action 708my$action=$path_info; 709$action=~ s,/.*$,,; 710if(exists$actions{$action}) { 711$path_info=~ s,^$action/*,,; 712$input_params{'action'} =$action; 713} 714 715# list of actions that want hash_base instead of hash, but can have no 716# pathname (f) parameter 717my@wants_base= ( 718'tree', 719'history', 720); 721 722# we want to catch 723# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 724my($parentrefname,$parentpathname,$refname,$pathname) = 725($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 726 727# first, analyze the 'current' part 728if(defined$pathname) { 729# we got "branch:filename" or "branch:dir/" 730# we could use git_get_type(branch:pathname), but: 731# - it needs $git_dir 732# - it does a git() call 733# - the convention of terminating directories with a slash 734# makes it superfluous 735# - embedding the action in the PATH_INFO would make it even 736# more superfluous 737$pathname=~ s,^/+,,; 738if(!$pathname||substr($pathname, -1)eq"/") { 739$input_params{'action'} ||="tree"; 740$pathname=~ s,/$,,; 741}else{ 742# the default action depends on whether we had parent info 743# or not 744if($parentrefname) { 745$input_params{'action'} ||="blobdiff_plain"; 746}else{ 747$input_params{'action'} ||="blob_plain"; 748} 749} 750$input_params{'hash_base'} ||=$refname; 751$input_params{'file_name'} ||=$pathname; 752}elsif(defined$refname) { 753# we got "branch". In this case we have to choose if we have to 754# set hash or hash_base. 755# 756# Most of the actions without a pathname only want hash to be 757# set, except for the ones specified in @wants_base that want 758# hash_base instead. It should also be noted that hand-crafted 759# links having 'history' as an action and no pathname or hash 760# set will fail, but that happens regardless of PATH_INFO. 761$input_params{'action'} ||="shortlog"; 762if(grep{$_eq$input_params{'action'} }@wants_base) { 763$input_params{'hash_base'} ||=$refname; 764}else{ 765$input_params{'hash'} ||=$refname; 766} 767} 768 769# next, handle the 'parent' part, if present 770if(defined$parentrefname) { 771# a missing pathspec defaults to the 'current' filename, allowing e.g. 772# someproject/blobdiff/oldrev..newrev:/filename 773if($parentpathname) { 774$parentpathname=~ s,^/+,,; 775$parentpathname=~ s,/$,,; 776$input_params{'file_parent'} ||=$parentpathname; 777}else{ 778$input_params{'file_parent'} ||=$input_params{'file_name'}; 779} 780# we assume that hash_parent_base is wanted if a path was specified, 781# or if the action wants hash_base instead of hash 782if(defined$input_params{'file_parent'} || 783grep{$_eq$input_params{'action'} }@wants_base) { 784$input_params{'hash_parent_base'} ||=$parentrefname; 785}else{ 786$input_params{'hash_parent'} ||=$parentrefname; 787} 788} 789 790# for the snapshot action, we allow URLs in the form 791# $project/snapshot/$hash.ext 792# where .ext determines the snapshot and gets removed from the 793# passed $refname to provide the $hash. 794# 795# To be able to tell that $refname includes the format extension, we 796# require the following two conditions to be satisfied: 797# - the hash input parameter MUST have been set from the $refname part 798# of the URL (i.e. they must be equal) 799# - the snapshot format MUST NOT have been defined already (e.g. from 800# CGI parameter sf) 801# It's also useless to try any matching unless $refname has a dot, 802# so we check for that too 803if(defined$input_params{'action'} && 804$input_params{'action'}eq'snapshot'&& 805defined$refname&&index($refname,'.') != -1&& 806$refnameeq$input_params{'hash'} && 807!defined$input_params{'snapshot_format'}) { 808# We loop over the known snapshot formats, checking for 809# extensions. Allowed extensions are both the defined suffix 810# (which includes the initial dot already) and the snapshot 811# format key itself, with a prepended dot 812while(my($fmt,$opt) =each%known_snapshot_formats) { 813my$hash=$refname; 814unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 815next; 816} 817my$sfx=$1; 818# a valid suffix was found, so set the snapshot format 819# and reset the hash parameter 820$input_params{'snapshot_format'} =$fmt; 821$input_params{'hash'} =$hash; 822# we also set the format suffix to the one requested 823# in the URL: this way a request for e.g. .tgz returns 824# a .tgz instead of a .tar.gz 825$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 826last; 827} 828} 829} 830evaluate_path_info(); 831 832our$action=$input_params{'action'}; 833if(defined$action) { 834if(!validate_action($action)) { 835 die_error(400,"Invalid action parameter"); 836} 837} 838 839# parameters which are pathnames 840our$project=$input_params{'project'}; 841if(defined$project) { 842if(!validate_project($project)) { 843undef$project; 844 die_error(404,"No such project"); 845} 846} 847 848our$file_name=$input_params{'file_name'}; 849if(defined$file_name) { 850if(!validate_pathname($file_name)) { 851 die_error(400,"Invalid file parameter"); 852} 853} 854 855our$file_parent=$input_params{'file_parent'}; 856if(defined$file_parent) { 857if(!validate_pathname($file_parent)) { 858 die_error(400,"Invalid file parent parameter"); 859} 860} 861 862# parameters which are refnames 863our$hash=$input_params{'hash'}; 864if(defined$hash) { 865if(!validate_refname($hash)) { 866 die_error(400,"Invalid hash parameter"); 867} 868} 869 870our$hash_parent=$input_params{'hash_parent'}; 871if(defined$hash_parent) { 872if(!validate_refname($hash_parent)) { 873 die_error(400,"Invalid hash parent parameter"); 874} 875} 876 877our$hash_base=$input_params{'hash_base'}; 878if(defined$hash_base) { 879if(!validate_refname($hash_base)) { 880 die_error(400,"Invalid hash base parameter"); 881} 882} 883 884our@extra_options= @{$input_params{'extra_options'}}; 885# @extra_options is always defined, since it can only be (currently) set from 886# CGI, and $cgi->param() returns the empty array in array context if the param 887# is not set 888foreachmy$opt(@extra_options) { 889if(not exists$allowed_options{$opt}) { 890 die_error(400,"Invalid option parameter"); 891} 892if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 893 die_error(400,"Invalid option parameter for this action"); 894} 895} 896 897our$hash_parent_base=$input_params{'hash_parent_base'}; 898if(defined$hash_parent_base) { 899if(!validate_refname($hash_parent_base)) { 900 die_error(400,"Invalid hash parent base parameter"); 901} 902} 903 904# other parameters 905our$page=$input_params{'page'}; 906if(defined$page) { 907if($page=~m/[^0-9]/) { 908 die_error(400,"Invalid page parameter"); 909} 910} 911 912our$searchtype=$input_params{'searchtype'}; 913if(defined$searchtype) { 914if($searchtype=~m/[^a-z]/) { 915 die_error(400,"Invalid searchtype parameter"); 916} 917} 918 919our$search_use_regexp=$input_params{'search_use_regexp'}; 920 921our$searchtext=$input_params{'searchtext'}; 922our$search_regexp; 923if(defined$searchtext) { 924if(length($searchtext) <2) { 925 die_error(403,"At least two characters are required for search parameter"); 926} 927$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 928} 929 930# path to the current git repository 931our$git_dir; 932$git_dir="$projectroot/$project"if$project; 933 934# list of supported snapshot formats 935our@snapshot_fmts= gitweb_get_feature('snapshot'); 936@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 937 938# check that the avatar feature is set to a known provider name, 939# and for each provider check if the dependencies are satisfied. 940# if the provider name is invalid or the dependencies are not met, 941# reset $git_avatar to the empty string. 942our($git_avatar) = gitweb_get_feature('avatar'); 943if($git_avatareq'gravatar') { 944$git_avatar=''unless(eval{require Digest::MD5;1; }); 945}elsif($git_avatareq'picon') { 946# no dependencies 947}else{ 948$git_avatar=''; 949} 950 951# dispatch 952if(!defined$action) { 953if(defined$hash) { 954$action= git_get_type($hash); 955}elsif(defined$hash_base&&defined$file_name) { 956$action= git_get_type("$hash_base:$file_name"); 957}elsif(defined$project) { 958$action='summary'; 959}else{ 960$action='project_list'; 961} 962} 963if(!defined($actions{$action})) { 964 die_error(400,"Unknown action"); 965} 966if($action!~m/^(?:opml|project_list|project_index)$/&& 967!$project) { 968 die_error(400,"Project needed"); 969} 970$actions{$action}->(); 971exit; 972 973## ====================================================================== 974## action links 975 976sub href { 977my%params=@_; 978# default is to use -absolute url() i.e. $my_uri 979my$href=$params{-full} ?$my_url:$my_uri; 980 981$params{'project'} =$projectunlessexists$params{'project'}; 982 983if($params{-replay}) { 984while(my($name,$symbol) =each%cgi_param_mapping) { 985if(!exists$params{$name}) { 986$params{$name} =$input_params{$name}; 987} 988} 989} 990 991my$use_pathinfo= gitweb_check_feature('pathinfo'); 992if($use_pathinfoand defined$params{'project'}) { 993# try to put as many parameters as possible in PATH_INFO: 994# - project name 995# - action 996# - hash_parent or hash_parent_base:/file_parent 997# - hash or hash_base:/filename 998# - the snapshot_format as an appropriate suffix 9991000# When the script is the root DirectoryIndex for the domain,1001# $href here would be something like http://gitweb.example.com/1002# Thus, we strip any trailing / from $href, to spare us double1003# slashes in the final URL1004$href=~ s,/$,,;10051006# Then add the project name, if present1007$href.="/".esc_url($params{'project'});1008delete$params{'project'};10091010# since we destructively absorb parameters, we keep this1011# boolean that remembers if we're handling a snapshot1012my$is_snapshot=$params{'action'}eq'snapshot';10131014# Summary just uses the project path URL, any other action is1015# added to the URL1016if(defined$params{'action'}) {1017$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1018delete$params{'action'};1019}10201021# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1022# stripping nonexistent or useless pieces1023$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1024||$params{'hash_parent'} ||$params{'hash'});1025if(defined$params{'hash_base'}) {1026if(defined$params{'hash_parent_base'}) {1027$href.= esc_url($params{'hash_parent_base'});1028# skip the file_parent if it's the same as the file_name1029if(defined$params{'file_parent'}) {1030if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1031delete$params{'file_parent'};1032}elsif($params{'file_parent'} !~/\.\./) {1033$href.=":/".esc_url($params{'file_parent'});1034delete$params{'file_parent'};1035}1036}1037$href.="..";1038delete$params{'hash_parent'};1039delete$params{'hash_parent_base'};1040}elsif(defined$params{'hash_parent'}) {1041$href.= esc_url($params{'hash_parent'})."..";1042delete$params{'hash_parent'};1043}10441045$href.= esc_url($params{'hash_base'});1046if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1047$href.=":/".esc_url($params{'file_name'});1048delete$params{'file_name'};1049}1050delete$params{'hash'};1051delete$params{'hash_base'};1052}elsif(defined$params{'hash'}) {1053$href.= esc_url($params{'hash'});1054delete$params{'hash'};1055}10561057# If the action was a snapshot, we can absorb the1058# snapshot_format parameter too1059if($is_snapshot) {1060my$fmt=$params{'snapshot_format'};1061# snapshot_format should always be defined when href()1062# is called, but just in case some code forgets, we1063# fall back to the default1064$fmt||=$snapshot_fmts[0];1065$href.=$known_snapshot_formats{$fmt}{'suffix'};1066delete$params{'snapshot_format'};1067}1068}10691070# now encode the parameters explicitly1071my@result= ();1072for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1073my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1074if(defined$params{$name}) {1075if(ref($params{$name})eq"ARRAY") {1076foreachmy$par(@{$params{$name}}) {1077push@result,$symbol."=". esc_param($par);1078}1079}else{1080push@result,$symbol."=". esc_param($params{$name});1081}1082}1083}1084$href.="?".join(';',@result)ifscalar@result;10851086return$href;1087}108810891090## ======================================================================1091## validation, quoting/unquoting and escaping10921093sub validate_action {1094my$input=shift||returnundef;1095returnundefunlessexists$actions{$input};1096return$input;1097}10981099sub validate_project {1100my$input=shift||returnundef;1101if(!validate_pathname($input) ||1102!(-d "$projectroot/$input") ||1103!check_export_ok("$projectroot/$input") ||1104($strict_export&& !project_in_list($input))) {1105returnundef;1106}else{1107return$input;1108}1109}11101111sub validate_pathname {1112my$input=shift||returnundef;11131114# no '.' or '..' as elements of path, i.e. no '.' nor '..'1115# at the beginning, at the end, and between slashes.1116# also this catches doubled slashes1117if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1118returnundef;1119}1120# no null characters1121if($input=~m!\0!) {1122returnundef;1123}1124return$input;1125}11261127sub validate_refname {1128my$input=shift||returnundef;11291130# textual hashes are O.K.1131if($input=~m/^[0-9a-fA-F]{40}$/) {1132return$input;1133}1134# it must be correct pathname1135$input= validate_pathname($input)1136orreturnundef;1137# restrictions on ref name according to git-check-ref-format1138if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1139returnundef;1140}1141return$input;1142}11431144# decode sequences of octets in utf8 into Perl's internal form,1145# which is utf-8 with utf8 flag set if needed. gitweb writes out1146# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1147sub to_utf8 {1148my$str=shift;1149if(utf8::valid($str)) {1150 utf8::decode($str);1151return$str;1152}else{1153return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1154}1155}11561157# quote unsafe chars, but keep the slash, even when it's not1158# correct, but quoted slashes look too horrible in bookmarks1159sub esc_param {1160my$str=shift;1161$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1162$str=~s/ /\+/g;1163return$str;1164}11651166# quote unsafe chars in whole URL, so some charactrs cannot be quoted1167sub esc_url {1168my$str=shift;1169$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1170$str=~s/\+/%2B/g;1171$str=~s/ /\+/g;1172return$str;1173}11741175# replace invalid utf8 character with SUBSTITUTION sequence1176sub esc_html {1177my$str=shift;1178my%opts=@_;11791180$str= to_utf8($str);1181$str=$cgi->escapeHTML($str);1182if($opts{'-nbsp'}) {1183$str=~s/ / /g;1184}1185$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1186return$str;1187}11881189# quote control characters and escape filename to HTML1190sub esc_path {1191my$str=shift;1192my%opts=@_;11931194$str= to_utf8($str);1195$str=$cgi->escapeHTML($str);1196if($opts{'-nbsp'}) {1197$str=~s/ / /g;1198}1199$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1200return$str;1201}12021203# Make control characters "printable", using character escape codes (CEC)1204sub quot_cec {1205my$cntrl=shift;1206my%opts=@_;1207my%es= (# character escape codes, aka escape sequences1208"\t"=>'\t',# tab (HT)1209"\n"=>'\n',# line feed (LF)1210"\r"=>'\r',# carrige return (CR)1211"\f"=>'\f',# form feed (FF)1212"\b"=>'\b',# backspace (BS)1213"\a"=>'\a',# alarm (bell) (BEL)1214"\e"=>'\e',# escape (ESC)1215"\013"=>'\v',# vertical tab (VT)1216"\000"=>'\0',# nul character (NUL)1217);1218my$chr= ( (exists$es{$cntrl})1219?$es{$cntrl}1220:sprintf('\%2x',ord($cntrl)) );1221if($opts{-nohtml}) {1222return$chr;1223}else{1224return"<span class=\"cntrl\">$chr</span>";1225}1226}12271228# Alternatively use unicode control pictures codepoints,1229# Unicode "printable representation" (PR)1230sub quot_upr {1231my$cntrl=shift;1232my%opts=@_;12331234my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1235if($opts{-nohtml}) {1236return$chr;1237}else{1238return"<span class=\"cntrl\">$chr</span>";1239}1240}12411242# git may return quoted and escaped filenames1243sub unquote {1244my$str=shift;12451246sub unq {1247my$seq=shift;1248my%es= (# character escape codes, aka escape sequences1249't'=>"\t",# tab (HT, TAB)1250'n'=>"\n",# newline (NL)1251'r'=>"\r",# return (CR)1252'f'=>"\f",# form feed (FF)1253'b'=>"\b",# backspace (BS)1254'a'=>"\a",# alarm (bell) (BEL)1255'e'=>"\e",# escape (ESC)1256'v'=>"\013",# vertical tab (VT)1257);12581259if($seq=~m/^[0-7]{1,3}$/) {1260# octal char sequence1261returnchr(oct($seq));1262}elsif(exists$es{$seq}) {1263# C escape sequence, aka character escape code1264return$es{$seq};1265}1266# quoted ordinary character1267return$seq;1268}12691270if($str=~m/^"(.*)"$/) {1271# needs unquoting1272$str=$1;1273$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1274}1275return$str;1276}12771278# escape tabs (convert tabs to spaces)1279sub untabify {1280my$line=shift;12811282while((my$pos=index($line,"\t")) != -1) {1283if(my$count= (8- ($pos%8))) {1284my$spaces=' ' x $count;1285$line=~s/\t/$spaces/;1286}1287}12881289return$line;1290}12911292sub project_in_list {1293my$project=shift;1294my@list= git_get_projects_list();1295return@list&&scalar(grep{$_->{'path'}eq$project}@list);1296}12971298## ----------------------------------------------------------------------1299## HTML aware string manipulation13001301# Try to chop given string on a word boundary between position1302# $len and $len+$add_len. If there is no word boundary there,1303# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1304# (marking chopped part) would be longer than given string.1305sub chop_str {1306my$str=shift;1307my$len=shift;1308my$add_len=shift||10;1309my$where=shift||'right';# 'left' | 'center' | 'right'13101311# Make sure perl knows it is utf8 encoded so we don't1312# cut in the middle of a utf8 multibyte char.1313$str= to_utf8($str);13141315# allow only $len chars, but don't cut a word if it would fit in $add_len1316# if it doesn't fit, cut it if it's still longer than the dots we would add1317# remove chopped character entities entirely13181319# when chopping in the middle, distribute $len into left and right part1320# return early if chopping wouldn't make string shorter1321if($whereeq'center') {1322return$strif($len+5>=length($str));# filler is length 51323$len=int($len/2);1324}else{1325return$strif($len+4>=length($str));# filler is length 41326}13271328# regexps: ending and beginning with word part up to $add_len1329my$endre=qr/.{$len}\w{0,$add_len}/;1330my$begre=qr/\w{0,$add_len}.{$len}/;13311332if($whereeq'left') {1333$str=~m/^(.*?)($begre)$/;1334my($lead,$body) = ($1,$2);1335if(length($lead) >4) {1336$lead=" ...";1337}1338return"$lead$body";13391340}elsif($whereeq'center') {1341$str=~m/^($endre)(.*)$/;1342my($left,$str) = ($1,$2);1343$str=~m/^(.*?)($begre)$/;1344my($mid,$right) = ($1,$2);1345if(length($mid) >5) {1346$mid=" ... ";1347}1348return"$left$mid$right";13491350}else{1351$str=~m/^($endre)(.*)$/;1352my$body=$1;1353my$tail=$2;1354if(length($tail) >4) {1355$tail="... ";1356}1357return"$body$tail";1358}1359}13601361# takes the same arguments as chop_str, but also wraps a <span> around the1362# result with a title attribute if it does get chopped. Additionally, the1363# string is HTML-escaped.1364sub chop_and_escape_str {1365my($str) =@_;13661367my$chopped= chop_str(@_);1368if($choppedeq$str) {1369return esc_html($chopped);1370}else{1371$str=~s/[[:cntrl:]]/?/g;1372return$cgi->span({-title=>$str}, esc_html($chopped));1373}1374}13751376## ----------------------------------------------------------------------1377## functions returning short strings13781379# CSS class for given age value (in seconds)1380sub age_class {1381my$age=shift;13821383if(!defined$age) {1384return"noage";1385}elsif($age<60*60*2) {1386return"age0";1387}elsif($age<60*60*24*2) {1388return"age1";1389}else{1390return"age2";1391}1392}13931394# convert age in seconds to "nn units ago" string1395sub age_string {1396my$age=shift;1397my$age_str;13981399if($age>60*60*24*365*2) {1400$age_str= (int$age/60/60/24/365);1401$age_str.=" years ago";1402}elsif($age>60*60*24*(365/12)*2) {1403$age_str=int$age/60/60/24/(365/12);1404$age_str.=" months ago";1405}elsif($age>60*60*24*7*2) {1406$age_str=int$age/60/60/24/7;1407$age_str.=" weeks ago";1408}elsif($age>60*60*24*2) {1409$age_str=int$age/60/60/24;1410$age_str.=" days ago";1411}elsif($age>60*60*2) {1412$age_str=int$age/60/60;1413$age_str.=" hours ago";1414}elsif($age>60*2) {1415$age_str=int$age/60;1416$age_str.=" min ago";1417}elsif($age>2) {1418$age_str=int$age;1419$age_str.=" sec ago";1420}else{1421$age_str.=" right now";1422}1423return$age_str;1424}14251426useconstant{1427 S_IFINVALID =>0030000,1428 S_IFGITLINK =>0160000,1429};14301431# submodule/subproject, a commit object reference1432sub S_ISGITLINK {1433my$mode=shift;14341435return(($mode& S_IFMT) == S_IFGITLINK)1436}14371438# convert file mode in octal to symbolic file mode string1439sub mode_str {1440my$mode=oct shift;14411442if(S_ISGITLINK($mode)) {1443return'm---------';1444}elsif(S_ISDIR($mode& S_IFMT)) {1445return'drwxr-xr-x';1446}elsif(S_ISLNK($mode)) {1447return'lrwxrwxrwx';1448}elsif(S_ISREG($mode)) {1449# git cares only about the executable bit1450if($mode& S_IXUSR) {1451return'-rwxr-xr-x';1452}else{1453return'-rw-r--r--';1454};1455}else{1456return'----------';1457}1458}14591460# convert file mode in octal to file type string1461sub file_type {1462my$mode=shift;14631464if($mode!~m/^[0-7]+$/) {1465return$mode;1466}else{1467$mode=oct$mode;1468}14691470if(S_ISGITLINK($mode)) {1471return"submodule";1472}elsif(S_ISDIR($mode& S_IFMT)) {1473return"directory";1474}elsif(S_ISLNK($mode)) {1475return"symlink";1476}elsif(S_ISREG($mode)) {1477return"file";1478}else{1479return"unknown";1480}1481}14821483# convert file mode in octal to file type description string1484sub file_type_long {1485my$mode=shift;14861487if($mode!~m/^[0-7]+$/) {1488return$mode;1489}else{1490$mode=oct$mode;1491}14921493if(S_ISGITLINK($mode)) {1494return"submodule";1495}elsif(S_ISDIR($mode& S_IFMT)) {1496return"directory";1497}elsif(S_ISLNK($mode)) {1498return"symlink";1499}elsif(S_ISREG($mode)) {1500if($mode& S_IXUSR) {1501return"executable";1502}else{1503return"file";1504};1505}else{1506return"unknown";1507}1508}150915101511## ----------------------------------------------------------------------1512## functions returning short HTML fragments, or transforming HTML fragments1513## which don't belong to other sections15141515# format line of commit message.1516sub format_log_line_html {1517my$line=shift;15181519$line= esc_html($line, -nbsp=>1);1520$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1521$cgi->a({-href => href(action=>"object", hash=>$1),1522-class=>"text"},$1);1523}eg;15241525return$line;1526}15271528# format marker of refs pointing to given object15291530# the destination action is chosen based on object type and current context:1531# - for annotated tags, we choose the tag view unless it's the current view1532# already, in which case we go to shortlog view1533# - for other refs, we keep the current view if we're in history, shortlog or1534# log view, and select shortlog otherwise1535sub format_ref_marker {1536my($refs,$id) =@_;1537my$markers='';15381539if(defined$refs->{$id}) {1540foreachmy$ref(@{$refs->{$id}}) {1541# this code exploits the fact that non-lightweight tags are the1542# only indirect objects, and that they are the only objects for which1543# we want to use tag instead of shortlog as action1544my($type,$name) =qw();1545my$indirect= ($ref=~s/\^\{\}$//);1546# e.g. tags/v2.6.11 or heads/next1547if($ref=~m!^(.*?)s?/(.*)$!) {1548$type=$1;1549$name=$2;1550}else{1551$type="ref";1552$name=$ref;1553}15541555my$class=$type;1556$class.=" indirect"if$indirect;15571558my$dest_action="shortlog";15591560if($indirect) {1561$dest_action="tag"unless$actioneq"tag";1562}elsif($action=~/^(history|(short)?log)$/) {1563$dest_action=$action;1564}15651566my$dest="";1567$dest.="refs/"unless$ref=~ m!^refs/!;1568$dest.=$ref;15691570my$link=$cgi->a({1571-href => href(1572 action=>$dest_action,1573 hash=>$dest1574)},$name);15751576$markers.=" <span class=\"$class\"title=\"$ref\">".1577$link."</span>";1578}1579}15801581if($markers) {1582return' <span class="refs">'.$markers.'</span>';1583}else{1584return"";1585}1586}15871588# format, perhaps shortened and with markers, title line1589sub format_subject_html {1590my($long,$short,$href,$extra) =@_;1591$extra=''unlessdefined($extra);15921593if(length($short) <length($long)) {1594$long=~s/[[:cntrl:]]/?/g;1595return$cgi->a({-href =>$href, -class=>"list subject",1596-title => to_utf8($long)},1597 esc_html($short)) .$extra;1598}else{1599return$cgi->a({-href =>$href, -class=>"list subject"},1600 esc_html($long)) .$extra;1601}1602}16031604# Rather than recomputing the url for an email multiple times, we cache it1605# after the first hit. This gives a visible benefit in views where the avatar1606# for the same email is used repeatedly (e.g. shortlog).1607# The cache is shared by all avatar engines (currently gravatar only), which1608# are free to use it as preferred. Since only one avatar engine is used for any1609# given page, there's no risk for cache conflicts.1610our%avatar_cache= ();16111612# Compute the picon url for a given email, by using the picon search service over at1613# http://www.cs.indiana.edu/picons/search.html1614sub picon_url {1615my$email=lc shift;1616if(!$avatar_cache{$email}) {1617my($user,$domain) =split('@',$email);1618$avatar_cache{$email} =1619"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1620"$domain/$user/".1621"users+domains+unknown/up/single";1622}1623return$avatar_cache{$email};1624}16251626# Compute the gravatar url for a given email, if it's not in the cache already.1627# Gravatar stores only the part of the URL before the size, since that's the1628# one computationally more expensive. This also allows reuse of the cache for1629# different sizes (for this particular engine).1630sub gravatar_url {1631my$email=lc shift;1632my$size=shift;1633$avatar_cache{$email} ||=1634"http://www.gravatar.com/avatar/".1635 Digest::MD5::md5_hex($email) ."?s=";1636return$avatar_cache{$email} .$size;1637}16381639# Insert an avatar for the given $email at the given $size if the feature1640# is enabled.1641sub git_get_avatar {1642my($email,%opts) =@_;1643my$pre_white= ($opts{-pad_before} ?" ":"");1644my$post_white= ($opts{-pad_after} ?" ":"");1645$opts{-size} ||='default';1646my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1647my$url="";1648if($git_avatareq'gravatar') {1649$url= gravatar_url($email,$size);1650}elsif($git_avatareq'picon') {1651$url= picon_url($email);1652}1653# Other providers can be added by extending the if chain, defining $url1654# as needed. If no variant puts something in $url, we assume avatars1655# are completely disabled/unavailable.1656if($url) {1657return$pre_white.1658"<img width=\"$size\"".1659"class=\"avatar\"".1660"src=\"$url\"".1661"alt=\"\"".1662"/>".$post_white;1663}else{1664return"";1665}1666}16671668sub format_search_author {1669my($author,$searchtype,$displaytext) =@_;1670my$have_search= gitweb_check_feature('search');16711672if($have_search) {1673my$performed="";1674if($searchtypeeq'author') {1675$performed="authored";1676}elsif($searchtypeeq'committer') {1677$performed="committed";1678}16791680return$cgi->a({-href => href(action=>"search", hash=>$hash,1681 searchtext=>$author,1682 searchtype=>$searchtype),class=>"list",1683 title=>"Search for commits$performedby$author"},1684$displaytext);16851686}else{1687return$displaytext;1688}1689}16901691# format the author name of the given commit with the given tag1692# the author name is chopped and escaped according to the other1693# optional parameters (see chop_str).1694sub format_author_html {1695my$tag=shift;1696my$co=shift;1697my$author= chop_and_escape_str($co->{'author_name'},@_);1698return"<$tagclass=\"author\">".1699 format_search_author($co->{'author_name'},"author",1700 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1701$author) .1702"</$tag>";1703}17041705# format git diff header line, i.e. "diff --(git|combined|cc) ..."1706sub format_git_diff_header_line {1707my$line=shift;1708my$diffinfo=shift;1709my($from,$to) =@_;17101711if($diffinfo->{'nparents'}) {1712# combined diff1713$line=~s!^(diff (.*?) )"?.*$!$1!;1714if($to->{'href'}) {1715$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1716 esc_path($to->{'file'}));1717}else{# file was deleted (no href)1718$line.= esc_path($to->{'file'});1719}1720}else{1721# "ordinary" diff1722$line=~s!^(diff (.*?) )"?a/.*$!$1!;1723if($from->{'href'}) {1724$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1725'a/'. esc_path($from->{'file'}));1726}else{# file was added (no href)1727$line.='a/'. esc_path($from->{'file'});1728}1729$line.=' ';1730if($to->{'href'}) {1731$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1732'b/'. esc_path($to->{'file'}));1733}else{# file was deleted1734$line.='b/'. esc_path($to->{'file'});1735}1736}17371738return"<div class=\"diff header\">$line</div>\n";1739}17401741# format extended diff header line, before patch itself1742sub format_extended_diff_header_line {1743my$line=shift;1744my$diffinfo=shift;1745my($from,$to) =@_;17461747# match <path>1748if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1749$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1750 esc_path($from->{'file'}));1751}1752if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1753$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1754 esc_path($to->{'file'}));1755}1756# match single <mode>1757if($line=~m/\s(\d{6})$/) {1758$line.='<span class="info"> ('.1759 file_type_long($1) .1760')</span>';1761}1762# match <hash>1763if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1764# can match only for combined diff1765$line='index ';1766for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1767if($from->{'href'}[$i]) {1768$line.=$cgi->a({-href=>$from->{'href'}[$i],1769-class=>"hash"},1770substr($diffinfo->{'from_id'}[$i],0,7));1771}else{1772$line.='0' x 7;1773}1774# separator1775$line.=','if($i<$diffinfo->{'nparents'} -1);1776}1777$line.='..';1778if($to->{'href'}) {1779$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1780substr($diffinfo->{'to_id'},0,7));1781}else{1782$line.='0' x 7;1783}17841785}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1786# can match only for ordinary diff1787my($from_link,$to_link);1788if($from->{'href'}) {1789$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1790substr($diffinfo->{'from_id'},0,7));1791}else{1792$from_link='0' x 7;1793}1794if($to->{'href'}) {1795$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1796substr($diffinfo->{'to_id'},0,7));1797}else{1798$to_link='0' x 7;1799}1800my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1801$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1802}18031804return$line."<br/>\n";1805}18061807# format from-file/to-file diff header1808sub format_diff_from_to_header {1809my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1810my$line;1811my$result='';18121813$line=$from_line;1814#assert($line =~ m/^---/) if DEBUG;1815# no extra formatting for "^--- /dev/null"1816if(!$diffinfo->{'nparents'}) {1817# ordinary (single parent) diff1818if($line=~m!^--- "?a/!) {1819if($from->{'href'}) {1820$line='--- a/'.1821$cgi->a({-href=>$from->{'href'}, -class=>"path"},1822 esc_path($from->{'file'}));1823}else{1824$line='--- a/'.1825 esc_path($from->{'file'});1826}1827}1828$result.= qq!<div class="diff from_file">$line</div>\n!;18291830}else{1831# combined diff (merge commit)1832for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1833if($from->{'href'}[$i]) {1834$line='--- '.1835$cgi->a({-href=>href(action=>"blobdiff",1836 hash_parent=>$diffinfo->{'from_id'}[$i],1837 hash_parent_base=>$parents[$i],1838 file_parent=>$from->{'file'}[$i],1839 hash=>$diffinfo->{'to_id'},1840 hash_base=>$hash,1841 file_name=>$to->{'file'}),1842-class=>"path",1843-title=>"diff". ($i+1)},1844$i+1) .1845'/'.1846$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1847 esc_path($from->{'file'}[$i]));1848}else{1849$line='--- /dev/null';1850}1851$result.= qq!<div class="diff from_file">$line</div>\n!;1852}1853}18541855$line=$to_line;1856#assert($line =~ m/^\+\+\+/) if DEBUG;1857# no extra formatting for "^+++ /dev/null"1858if($line=~m!^\+\+\+ "?b/!) {1859if($to->{'href'}) {1860$line='+++ b/'.1861$cgi->a({-href=>$to->{'href'}, -class=>"path"},1862 esc_path($to->{'file'}));1863}else{1864$line='+++ b/'.1865 esc_path($to->{'file'});1866}1867}1868$result.= qq!<div class="diff to_file">$line</div>\n!;18691870return$result;1871}18721873# create note for patch simplified by combined diff1874sub format_diff_cc_simplified {1875my($diffinfo,@parents) =@_;1876my$result='';18771878$result.="<div class=\"diff header\">".1879"diff --cc ";1880if(!is_deleted($diffinfo)) {1881$result.=$cgi->a({-href => href(action=>"blob",1882 hash_base=>$hash,1883 hash=>$diffinfo->{'to_id'},1884 file_name=>$diffinfo->{'to_file'}),1885-class=>"path"},1886 esc_path($diffinfo->{'to_file'}));1887}else{1888$result.= esc_path($diffinfo->{'to_file'});1889}1890$result.="</div>\n".# class="diff header"1891"<div class=\"diff nodifferences\">".1892"Simple merge".1893"</div>\n";# class="diff nodifferences"18941895return$result;1896}18971898# format patch (diff) line (not to be used for diff headers)1899sub format_diff_line {1900my$line=shift;1901my($from,$to) =@_;1902my$diff_class="";19031904chomp$line;19051906if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1907# combined diff1908my$prefix=substr($line,0,scalar@{$from->{'href'}});1909if($line=~m/^\@{3}/) {1910$diff_class=" chunk_header";1911}elsif($line=~m/^\\/) {1912$diff_class=" incomplete";1913}elsif($prefix=~tr/+/+/) {1914$diff_class=" add";1915}elsif($prefix=~tr/-/-/) {1916$diff_class=" rem";1917}1918}else{1919# assume ordinary diff1920my$char=substr($line,0,1);1921if($chareq'+') {1922$diff_class=" add";1923}elsif($chareq'-') {1924$diff_class=" rem";1925}elsif($chareq'@') {1926$diff_class=" chunk_header";1927}elsif($chareq"\\") {1928$diff_class=" incomplete";1929}1930}1931$line= untabify($line);1932if($from&&$to&&$line=~m/^\@{2} /) {1933my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1934$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;19351936$from_lines=0unlessdefined$from_lines;1937$to_lines=0unlessdefined$to_lines;19381939if($from->{'href'}) {1940$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1941-class=>"list"},$from_text);1942}1943if($to->{'href'}) {1944$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1945-class=>"list"},$to_text);1946}1947$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1948"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1949return"<div class=\"diff$diff_class\">$line</div>\n";1950}elsif($from&&$to&&$line=~m/^\@{3}/) {1951my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1952my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);19531954@from_text=split(' ',$ranges);1955for(my$i=0;$i<@from_text; ++$i) {1956($from_start[$i],$from_nlines[$i]) =1957(split(',',substr($from_text[$i],1)),0);1958}19591960$to_text=pop@from_text;1961$to_start=pop@from_start;1962$to_nlines=pop@from_nlines;19631964$line="<span class=\"chunk_info\">$prefix";1965for(my$i=0;$i<@from_text; ++$i) {1966if($from->{'href'}[$i]) {1967$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1968-class=>"list"},$from_text[$i]);1969}else{1970$line.=$from_text[$i];1971}1972$line.=" ";1973}1974if($to->{'href'}) {1975$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1976-class=>"list"},$to_text);1977}else{1978$line.=$to_text;1979}1980$line.="$prefix</span>".1981"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1982return"<div class=\"diff$diff_class\">$line</div>\n";1983}1984return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1985}19861987# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1988# linked. Pass the hash of the tree/commit to snapshot.1989sub format_snapshot_links {1990my($hash) =@_;1991my$num_fmts=@snapshot_fmts;1992if($num_fmts>1) {1993# A parenthesized list of links bearing format names.1994# e.g. "snapshot (_tar.gz_ _zip_)"1995return"snapshot (".join(' ',map1996$cgi->a({1997-href => href(1998 action=>"snapshot",1999 hash=>$hash,2000 snapshot_format=>$_2001)2002},$known_snapshot_formats{$_}{'display'})2003,@snapshot_fmts) .")";2004}elsif($num_fmts==1) {2005# A single "snapshot" link whose tooltip bears the format name.2006# i.e. "_snapshot_"2007my($fmt) =@snapshot_fmts;2008return2009$cgi->a({2010-href => href(2011 action=>"snapshot",2012 hash=>$hash,2013 snapshot_format=>$fmt2014),2015-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2016},"snapshot");2017}else{# $num_fmts == 02018returnundef;2019}2020}20212022## ......................................................................2023## functions returning values to be passed, perhaps after some2024## transformation, to other functions; e.g. returning arguments to href()20252026# returns hash to be passed to href to generate gitweb URL2027# in -title key it returns description of link2028sub get_feed_info {2029my$format=shift||'Atom';2030my%res= (action =>lc($format));20312032# feed links are possible only for project views2033return unless(defined$project);2034# some views should link to OPML, or to generic project feed,2035# or don't have specific feed yet (so they should use generic)2036return if($action=~/^(?:tags|heads|forks|tag|search)$/x);20372038my$branch;2039# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2040# from tag links; this also makes possible to detect branch links2041if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2042(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2043$branch=$1;2044}2045# find log type for feed description (title)2046my$type='log';2047if(defined$file_name) {2048$type="history of$file_name";2049$type.="/"if($actioneq'tree');2050$type.=" on '$branch'"if(defined$branch);2051}else{2052$type="log of$branch"if(defined$branch);2053}20542055$res{-title} =$type;2056$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2057$res{'file_name'} =$file_name;20582059return%res;2060}20612062## ----------------------------------------------------------------------2063## git utility subroutines, invoking git commands20642065# returns path to the core git executable and the --git-dir parameter as list2066sub git_cmd {2067$number_of_git_cmds++;2068return$GIT,'--git-dir='.$git_dir;2069}20702071# quote the given arguments for passing them to the shell2072# quote_command("command", "arg 1", "arg with ' and ! characters")2073# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2074# Try to avoid using this function wherever possible.2075sub quote_command {2076returnjoin(' ',2077map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2078}20792080# get HEAD ref of given project as hash2081sub git_get_head_hash {2082return git_get_full_hash(shift,'HEAD');2083}20842085sub git_get_full_hash {2086return git_get_hash(@_);2087}20882089sub git_get_short_hash {2090return git_get_hash(@_,'--short=7');2091}20922093sub git_get_hash {2094my($project,$hash,@options) =@_;2095my$o_git_dir=$git_dir;2096my$retval=undef;2097$git_dir="$projectroot/$project";2098if(open my$fd,'-|', git_cmd(),'rev-parse',2099'--verify','-q',@options,$hash) {2100$retval= <$fd>;2101chomp$retvalifdefined$retval;2102close$fd;2103}2104if(defined$o_git_dir) {2105$git_dir=$o_git_dir;2106}2107return$retval;2108}21092110# get type of given object2111sub git_get_type {2112my$hash=shift;21132114open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2115my$type= <$fd>;2116close$fdorreturn;2117chomp$type;2118return$type;2119}21202121# repository configuration2122our$config_file='';2123our%config;21242125# store multiple values for single key as anonymous array reference2126# single values stored directly in the hash, not as [ <value> ]2127sub hash_set_multi {2128my($hash,$key,$value) =@_;21292130if(!exists$hash->{$key}) {2131$hash->{$key} =$value;2132}elsif(!ref$hash->{$key}) {2133$hash->{$key} = [$hash->{$key},$value];2134}else{2135push@{$hash->{$key}},$value;2136}2137}21382139# return hash of git project configuration2140# optionally limited to some section, e.g. 'gitweb'2141sub git_parse_project_config {2142my$section_regexp=shift;2143my%config;21442145local$/="\0";21462147open my$fh,"-|", git_cmd(),"config",'-z','-l',2148orreturn;21492150while(my$keyval= <$fh>) {2151chomp$keyval;2152my($key,$value) =split(/\n/,$keyval,2);21532154 hash_set_multi(\%config,$key,$value)2155if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2156}2157close$fh;21582159return%config;2160}21612162# convert config value to boolean: 'true' or 'false'2163# no value, number > 0, 'true' and 'yes' values are true2164# rest of values are treated as false (never as error)2165sub config_to_bool {2166my$val=shift;21672168return1if!defined$val;# section.key21692170# strip leading and trailing whitespace2171$val=~s/^\s+//;2172$val=~s/\s+$//;21732174return(($val=~/^\d+$/&&$val) ||# section.key = 12175($val=~/^(?:true|yes)$/i));# section.key = true2176}21772178# convert config value to simple decimal number2179# an optional value suffix of 'k', 'm', or 'g' will cause the value2180# to be multiplied by 1024, 1048576, or 10737418242181sub config_to_int {2182my$val=shift;21832184# strip leading and trailing whitespace2185$val=~s/^\s+//;2186$val=~s/\s+$//;21872188if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2189$unit=lc($unit);2190# unknown unit is treated as 12191return$num* ($uniteq'g'?1073741824:2192$uniteq'm'?1048576:2193$uniteq'k'?1024:1);2194}2195return$val;2196}21972198# convert config value to array reference, if needed2199sub config_to_multi {2200my$val=shift;22012202returnref($val) ?$val: (defined($val) ? [$val] : []);2203}22042205sub git_get_project_config {2206my($key,$type) =@_;22072208# key sanity check2209return unless($key);2210$key=~s/^gitweb\.//;2211return if($key=~m/\W/);22122213# type sanity check2214if(defined$type) {2215$type=~s/^--//;2216$type=undef2217unless($typeeq'bool'||$typeeq'int');2218}22192220# get config2221if(!defined$config_file||2222$config_filene"$git_dir/config") {2223%config= git_parse_project_config('gitweb');2224$config_file="$git_dir/config";2225}22262227# check if config variable (key) exists2228return unlessexists$config{"gitweb.$key"};22292230# ensure given type2231if(!defined$type) {2232return$config{"gitweb.$key"};2233}elsif($typeeq'bool') {2234# backward compatibility: 'git config --bool' returns true/false2235return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2236}elsif($typeeq'int') {2237return config_to_int($config{"gitweb.$key"});2238}2239return$config{"gitweb.$key"};2240}22412242# get hash of given path at given ref2243sub git_get_hash_by_path {2244my$base=shift;2245my$path=shift||returnundef;2246my$type=shift;22472248$path=~ s,/+$,,;22492250open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2251or die_error(500,"Open git-ls-tree failed");2252my$line= <$fd>;2253close$fdorreturnundef;22542255if(!defined$line) {2256# there is no tree or hash given by $path at $base2257returnundef;2258}22592260#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2261$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2262if(defined$type&&$typene$2) {2263# type doesn't match2264returnundef;2265}2266return$3;2267}22682269# get path of entry with given hash at given tree-ish (ref)2270# used to get 'from' filename for combined diff (merge commit) for renames2271sub git_get_path_by_hash {2272my$base=shift||return;2273my$hash=shift||return;22742275local$/="\0";22762277open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2278orreturnundef;2279while(my$line= <$fd>) {2280chomp$line;22812282#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2283#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2284if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2285close$fd;2286return$1;2287}2288}2289close$fd;2290returnundef;2291}22922293## ......................................................................2294## git utility functions, directly accessing git repository22952296sub git_get_project_description {2297my$path=shift;22982299$git_dir="$projectroot/$path";2300open my$fd,'<',"$git_dir/description"2301orreturn git_get_project_config('description');2302my$descr= <$fd>;2303close$fd;2304if(defined$descr) {2305chomp$descr;2306}2307return$descr;2308}23092310sub git_get_project_ctags {2311my$path=shift;2312my$ctags= {};23132314$git_dir="$projectroot/$path";2315opendir my$dh,"$git_dir/ctags"2316orreturn$ctags;2317foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2318open my$ct,'<',$_ornext;2319my$val= <$ct>;2320chomp$val;2321close$ct;2322my$ctag=$_;$ctag=~ s#.*/##;2323$ctags->{$ctag} =$val;2324}2325closedir$dh;2326$ctags;2327}23282329sub git_populate_project_tagcloud {2330my$ctags=shift;23312332# First, merge different-cased tags; tags vote on casing2333my%ctags_lc;2334foreach(keys%$ctags) {2335$ctags_lc{lc$_}->{count} +=$ctags->{$_};2336if(not$ctags_lc{lc$_}->{topcount}2337or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2338$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2339$ctags_lc{lc$_}->{topname} =$_;2340}2341}23422343my$cloud;2344if(eval{require HTML::TagCloud;1; }) {2345$cloud= HTML::TagCloud->new;2346foreach(sort keys%ctags_lc) {2347# Pad the title with spaces so that the cloud looks2348# less crammed.2349my$title=$ctags_lc{$_}->{topname};2350$title=~s/ / /g;2351$title=~s/^/ /g;2352$title=~s/$/ /g;2353$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2354}2355}else{2356$cloud= \%ctags_lc;2357}2358$cloud;2359}23602361sub git_show_project_tagcloud {2362my($cloud,$count) =@_;2363print STDERR ref($cloud)."..\n";2364if(ref$cloudeq'HTML::TagCloud') {2365return$cloud->html_and_css($count);2366}else{2367my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2368return'<p align="center">'.join(', ',map{2369"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2370}splice(@tags,0,$count)) .'</p>';2371}2372}23732374sub git_get_project_url_list {2375my$path=shift;23762377$git_dir="$projectroot/$path";2378open my$fd,'<',"$git_dir/cloneurl"2379orreturnwantarray?2380@{ config_to_multi(git_get_project_config('url')) } :2381 config_to_multi(git_get_project_config('url'));2382my@git_project_url_list=map{chomp;$_} <$fd>;2383close$fd;23842385returnwantarray?@git_project_url_list: \@git_project_url_list;2386}23872388sub git_get_projects_list {2389my($filter) =@_;2390my@list;23912392$filter||='';2393$filter=~s/\.git$//;23942395my$check_forks= gitweb_check_feature('forks');23962397if(-d $projects_list) {2398# search in directory2399my$dir=$projects_list. ($filter?"/$filter":'');2400# remove the trailing "/"2401$dir=~s!/+$!!;2402my$pfxlen=length("$dir");2403my$pfxdepth= ($dir=~tr!/!!);24042405 File::Find::find({2406 follow_fast =>1,# follow symbolic links2407 follow_skip =>2,# ignore duplicates2408 dangling_symlinks =>0,# ignore dangling symlinks, silently2409 wanted =>sub{2410# skip project-list toplevel, if we get it.2411return if(m!^[/.]$!);2412# only directories can be git repositories2413return unless(-d $_);2414# don't traverse too deep (Find is super slow on os x)2415if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2416$File::Find::prune =1;2417return;2418}24192420my$subdir=substr($File::Find::name,$pfxlen+1);2421# we check related file in $projectroot2422my$path= ($filter?"$filter/":'') .$subdir;2423if(check_export_ok("$projectroot/$path")) {2424push@list, { path =>$path};2425$File::Find::prune =1;2426}2427},2428},"$dir");24292430}elsif(-f $projects_list) {2431# read from file(url-encoded):2432# 'git%2Fgit.git Linus+Torvalds'2433# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2434# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2435my%paths;2436open my$fd,'<',$projects_listorreturn;2437 PROJECT:2438while(my$line= <$fd>) {2439chomp$line;2440my($path,$owner) =split' ',$line;2441$path= unescape($path);2442$owner= unescape($owner);2443if(!defined$path) {2444next;2445}2446if($filterne'') {2447# looking for forks;2448my$pfx=substr($path,0,length($filter));2449if($pfxne$filter) {2450next PROJECT;2451}2452my$sfx=substr($path,length($filter));2453if($sfx!~/^\/.*\.git$/) {2454next PROJECT;2455}2456}elsif($check_forks) {2457 PATH:2458foreachmy$filter(keys%paths) {2459# looking for forks;2460my$pfx=substr($path,0,length($filter));2461if($pfxne$filter) {2462next PATH;2463}2464my$sfx=substr($path,length($filter));2465if($sfx!~/^\/.*\.git$/) {2466next PATH;2467}2468# is a fork, don't include it in2469# the list2470next PROJECT;2471}2472}2473if(check_export_ok("$projectroot/$path")) {2474my$pr= {2475 path =>$path,2476 owner => to_utf8($owner),2477};2478push@list,$pr;2479(my$forks_path=$path) =~s/\.git$//;2480$paths{$forks_path}++;2481}2482}2483close$fd;2484}2485return@list;2486}24872488our$gitweb_project_owner=undef;2489sub git_get_project_list_from_file {24902491return if(defined$gitweb_project_owner);24922493$gitweb_project_owner= {};2494# read from file (url-encoded):2495# 'git%2Fgit.git Linus+Torvalds'2496# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2497# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2498if(-f $projects_list) {2499open(my$fd,'<',$projects_list);2500while(my$line= <$fd>) {2501chomp$line;2502my($pr,$ow) =split' ',$line;2503$pr= unescape($pr);2504$ow= unescape($ow);2505$gitweb_project_owner->{$pr} = to_utf8($ow);2506}2507close$fd;2508}2509}25102511sub git_get_project_owner {2512my$project=shift;2513my$owner;25142515returnundefunless$project;2516$git_dir="$projectroot/$project";25172518if(!defined$gitweb_project_owner) {2519 git_get_project_list_from_file();2520}25212522if(exists$gitweb_project_owner->{$project}) {2523$owner=$gitweb_project_owner->{$project};2524}2525if(!defined$owner){2526$owner= git_get_project_config('owner');2527}2528if(!defined$owner) {2529$owner= get_file_owner("$git_dir");2530}25312532return$owner;2533}25342535sub git_get_last_activity {2536my($path) =@_;2537my$fd;25382539$git_dir="$projectroot/$path";2540open($fd,"-|", git_cmd(),'for-each-ref',2541'--format=%(committer)',2542'--sort=-committerdate',2543'--count=1',2544'refs/heads')orreturn;2545my$most_recent= <$fd>;2546close$fdorreturn;2547if(defined$most_recent&&2548$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2549my$timestamp=$1;2550my$age=time-$timestamp;2551return($age, age_string($age));2552}2553return(undef,undef);2554}25552556sub git_get_references {2557my$type=shift||"";2558my%refs;2559# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112560# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2561open my$fd,"-|", git_cmd(),"show-ref","--dereference",2562($type? ("--","refs/$type") : ())# use -- <pattern> if $type2563orreturn;25642565while(my$line= <$fd>) {2566chomp$line;2567if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2568if(defined$refs{$1}) {2569push@{$refs{$1}},$2;2570}else{2571$refs{$1} = [$2];2572}2573}2574}2575close$fdorreturn;2576return \%refs;2577}25782579sub git_get_rev_name_tags {2580my$hash=shift||returnundef;25812582open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2583orreturn;2584my$name_rev= <$fd>;2585close$fd;25862587if($name_rev=~ m|^$hash tags/(.*)$|) {2588return$1;2589}else{2590# catches also '$hash undefined' output2591returnundef;2592}2593}25942595## ----------------------------------------------------------------------2596## parse to hash functions25972598sub parse_date {2599my$epoch=shift;2600my$tz=shift||"-0000";26012602my%date;2603my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2604my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2605my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2606$date{'hour'} =$hour;2607$date{'minute'} =$min;2608$date{'mday'} =$mday;2609$date{'day'} =$days[$wday];2610$date{'month'} =$months[$mon];2611$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2612$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2613$date{'mday-time'} =sprintf"%d%s%02d:%02d",2614$mday,$months[$mon],$hour,$min;2615$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",26161900+$year,1+$mon,$mday,$hour,$min,$sec;26172618$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2619my$local=$epoch+ ((int$1+ ($2/60)) *3600);2620($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2621$date{'hour_local'} =$hour;2622$date{'minute_local'} =$min;2623$date{'tz_local'} =$tz;2624$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",26251900+$year,$mon+1,$mday,2626$hour,$min,$sec,$tz);2627return%date;2628}26292630sub parse_tag {2631my$tag_id=shift;2632my%tag;2633my@comment;26342635open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2636$tag{'id'} =$tag_id;2637while(my$line= <$fd>) {2638chomp$line;2639if($line=~m/^object ([0-9a-fA-F]{40})$/) {2640$tag{'object'} =$1;2641}elsif($line=~m/^type (.+)$/) {2642$tag{'type'} =$1;2643}elsif($line=~m/^tag (.+)$/) {2644$tag{'name'} =$1;2645}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2646$tag{'author'} =$1;2647$tag{'author_epoch'} =$2;2648$tag{'author_tz'} =$3;2649if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2650$tag{'author_name'} =$1;2651$tag{'author_email'} =$2;2652}else{2653$tag{'author_name'} =$tag{'author'};2654}2655}elsif($line=~m/--BEGIN/) {2656push@comment,$line;2657last;2658}elsif($lineeq"") {2659last;2660}2661}2662push@comment, <$fd>;2663$tag{'comment'} = \@comment;2664close$fdorreturn;2665if(!defined$tag{'name'}) {2666return2667};2668return%tag2669}26702671sub parse_commit_text {2672my($commit_text,$withparents) =@_;2673my@commit_lines=split'\n',$commit_text;2674my%co;26752676pop@commit_lines;# Remove '\0'26772678if(!@commit_lines) {2679return;2680}26812682my$header=shift@commit_lines;2683if($header!~m/^[0-9a-fA-F]{40}/) {2684return;2685}2686($co{'id'},my@parents) =split' ',$header;2687while(my$line=shift@commit_lines) {2688last if$lineeq"\n";2689if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2690$co{'tree'} =$1;2691}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2692push@parents,$1;2693}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2694$co{'author'} = to_utf8($1);2695$co{'author_epoch'} =$2;2696$co{'author_tz'} =$3;2697if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2698$co{'author_name'} =$1;2699$co{'author_email'} =$2;2700}else{2701$co{'author_name'} =$co{'author'};2702}2703}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2704$co{'committer'} = to_utf8($1);2705$co{'committer_epoch'} =$2;2706$co{'committer_tz'} =$3;2707if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2708$co{'committer_name'} =$1;2709$co{'committer_email'} =$2;2710}else{2711$co{'committer_name'} =$co{'committer'};2712}2713}2714}2715if(!defined$co{'tree'}) {2716return;2717};2718$co{'parents'} = \@parents;2719$co{'parent'} =$parents[0];27202721foreachmy$title(@commit_lines) {2722$title=~s/^ //;2723if($titlene"") {2724$co{'title'} = chop_str($title,80,5);2725# remove leading stuff of merges to make the interesting part visible2726if(length($title) >50) {2727$title=~s/^Automatic //;2728$title=~s/^merge (of|with) /Merge ... /i;2729if(length($title) >50) {2730$title=~s/(http|rsync):\/\///;2731}2732if(length($title) >50) {2733$title=~s/(master|www|rsync)\.//;2734}2735if(length($title) >50) {2736$title=~s/kernel.org:?//;2737}2738if(length($title) >50) {2739$title=~s/\/pub\/scm//;2740}2741}2742$co{'title_short'} = chop_str($title,50,5);2743last;2744}2745}2746if(!defined$co{'title'} ||$co{'title'}eq"") {2747$co{'title'} =$co{'title_short'} ='(no commit message)';2748}2749# remove added spaces2750foreachmy$line(@commit_lines) {2751$line=~s/^ //;2752}2753$co{'comment'} = \@commit_lines;27542755my$age=time-$co{'committer_epoch'};2756$co{'age'} =$age;2757$co{'age_string'} = age_string($age);2758my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2759if($age>60*60*24*7*2) {2760$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2761$co{'age_string_age'} =$co{'age_string'};2762}else{2763$co{'age_string_date'} =$co{'age_string'};2764$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2765}2766return%co;2767}27682769sub parse_commit {2770my($commit_id) =@_;2771my%co;27722773local$/="\0";27742775open my$fd,"-|", git_cmd(),"rev-list",2776"--parents",2777"--header",2778"--max-count=1",2779$commit_id,2780"--",2781or die_error(500,"Open git-rev-list failed");2782%co= parse_commit_text(<$fd>,1);2783close$fd;27842785return%co;2786}27872788sub parse_commits {2789my($commit_id,$maxcount,$skip,$filename,@args) =@_;2790my@cos;27912792$maxcount||=1;2793$skip||=0;27942795local$/="\0";27962797open my$fd,"-|", git_cmd(),"rev-list",2798"--header",2799@args,2800("--max-count=".$maxcount),2801("--skip=".$skip),2802@extra_options,2803$commit_id,2804"--",2805($filename? ($filename) : ())2806or die_error(500,"Open git-rev-list failed");2807while(my$line= <$fd>) {2808my%co= parse_commit_text($line);2809push@cos, \%co;2810}2811close$fd;28122813returnwantarray?@cos: \@cos;2814}28152816# parse line of git-diff-tree "raw" output2817sub parse_difftree_raw_line {2818my$line=shift;2819my%res;28202821# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2822# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2823if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2824$res{'from_mode'} =$1;2825$res{'to_mode'} =$2;2826$res{'from_id'} =$3;2827$res{'to_id'} =$4;2828$res{'status'} =$5;2829$res{'similarity'} =$6;2830if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2831($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2832}else{2833$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2834}2835}2836# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2837# combined diff (for merge commit)2838elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2839$res{'nparents'} =length($1);2840$res{'from_mode'} = [split(' ',$2) ];2841$res{'to_mode'} =pop@{$res{'from_mode'}};2842$res{'from_id'} = [split(' ',$3) ];2843$res{'to_id'} =pop@{$res{'from_id'}};2844$res{'status'} = [split('',$4) ];2845$res{'to_file'} = unquote($5);2846}2847# 'c512b523472485aef4fff9e57b229d9d243c967f'2848elsif($line=~m/^([0-9a-fA-F]{40})$/) {2849$res{'commit'} =$1;2850}28512852returnwantarray?%res: \%res;2853}28542855# wrapper: return parsed line of git-diff-tree "raw" output2856# (the argument might be raw line, or parsed info)2857sub parsed_difftree_line {2858my$line_or_ref=shift;28592860if(ref($line_or_ref)eq"HASH") {2861# pre-parsed (or generated by hand)2862return$line_or_ref;2863}else{2864return parse_difftree_raw_line($line_or_ref);2865}2866}28672868# parse line of git-ls-tree output2869sub parse_ls_tree_line {2870my$line=shift;2871my%opts=@_;2872my%res;28732874if($opts{'-l'}) {2875#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'2876$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;28772878$res{'mode'} =$1;2879$res{'type'} =$2;2880$res{'hash'} =$3;2881$res{'size'} =$4;2882if($opts{'-z'}) {2883$res{'name'} =$5;2884}else{2885$res{'name'} = unquote($5);2886}2887}else{2888#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2889$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;28902891$res{'mode'} =$1;2892$res{'type'} =$2;2893$res{'hash'} =$3;2894if($opts{'-z'}) {2895$res{'name'} =$4;2896}else{2897$res{'name'} = unquote($4);2898}2899}29002901returnwantarray?%res: \%res;2902}29032904# generates _two_ hashes, references to which are passed as 2 and 3 argument2905sub parse_from_to_diffinfo {2906my($diffinfo,$from,$to,@parents) =@_;29072908if($diffinfo->{'nparents'}) {2909# combined diff2910$from->{'file'} = [];2911$from->{'href'} = [];2912 fill_from_file_info($diffinfo,@parents)2913unlessexists$diffinfo->{'from_file'};2914for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2915$from->{'file'}[$i] =2916defined$diffinfo->{'from_file'}[$i] ?2917$diffinfo->{'from_file'}[$i] :2918$diffinfo->{'to_file'};2919if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2920$from->{'href'}[$i] = href(action=>"blob",2921 hash_base=>$parents[$i],2922 hash=>$diffinfo->{'from_id'}[$i],2923 file_name=>$from->{'file'}[$i]);2924}else{2925$from->{'href'}[$i] =undef;2926}2927}2928}else{2929# ordinary (not combined) diff2930$from->{'file'} =$diffinfo->{'from_file'};2931if($diffinfo->{'status'}ne"A") {# not new (added) file2932$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2933 hash=>$diffinfo->{'from_id'},2934 file_name=>$from->{'file'});2935}else{2936delete$from->{'href'};2937}2938}29392940$to->{'file'} =$diffinfo->{'to_file'};2941if(!is_deleted($diffinfo)) {# file exists in result2942$to->{'href'} = href(action=>"blob", hash_base=>$hash,2943 hash=>$diffinfo->{'to_id'},2944 file_name=>$to->{'file'});2945}else{2946delete$to->{'href'};2947}2948}29492950## ......................................................................2951## parse to array of hashes functions29522953sub git_get_heads_list {2954my$limit=shift;2955my@headslist;29562957open my$fd,'-|', git_cmd(),'for-each-ref',2958($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2959'--format=%(objectname) %(refname) %(subject)%00%(committer)',2960'refs/heads'2961orreturn;2962while(my$line= <$fd>) {2963my%ref_item;29642965chomp$line;2966my($refinfo,$committerinfo) =split(/\0/,$line);2967my($hash,$name,$title) =split(' ',$refinfo,3);2968my($committer,$epoch,$tz) =2969($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2970$ref_item{'fullname'} =$name;2971$name=~s!^refs/heads/!!;29722973$ref_item{'name'} =$name;2974$ref_item{'id'} =$hash;2975$ref_item{'title'} =$title||'(no commit message)';2976$ref_item{'epoch'} =$epoch;2977if($epoch) {2978$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2979}else{2980$ref_item{'age'} ="unknown";2981}29822983push@headslist, \%ref_item;2984}2985close$fd;29862987returnwantarray?@headslist: \@headslist;2988}29892990sub git_get_tags_list {2991my$limit=shift;2992my@tagslist;29932994open my$fd,'-|', git_cmd(),'for-each-ref',2995($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2996'--format=%(objectname) %(objecttype) %(refname) '.2997'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2998'refs/tags'2999orreturn;3000while(my$line= <$fd>) {3001my%ref_item;30023003chomp$line;3004my($refinfo,$creatorinfo) =split(/\0/,$line);3005my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3006my($creator,$epoch,$tz) =3007($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3008$ref_item{'fullname'} =$name;3009$name=~s!^refs/tags/!!;30103011$ref_item{'type'} =$type;3012$ref_item{'id'} =$id;3013$ref_item{'name'} =$name;3014if($typeeq"tag") {3015$ref_item{'subject'} =$title;3016$ref_item{'reftype'} =$reftype;3017$ref_item{'refid'} =$refid;3018}else{3019$ref_item{'reftype'} =$type;3020$ref_item{'refid'} =$id;3021}30223023if($typeeq"tag"||$typeeq"commit") {3024$ref_item{'epoch'} =$epoch;3025if($epoch) {3026$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3027}else{3028$ref_item{'age'} ="unknown";3029}3030}30313032push@tagslist, \%ref_item;3033}3034close$fd;30353036returnwantarray?@tagslist: \@tagslist;3037}30383039## ----------------------------------------------------------------------3040## filesystem-related functions30413042sub get_file_owner {3043my$path=shift;30443045my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3046my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3047if(!defined$gcos) {3048returnundef;3049}3050my$owner=$gcos;3051$owner=~s/[,;].*$//;3052return to_utf8($owner);3053}30543055# assume that file exists3056sub insert_file {3057my$filename=shift;30583059open my$fd,'<',$filename;3060print map{ to_utf8($_) } <$fd>;3061close$fd;3062}30633064## ......................................................................3065## mimetype related functions30663067sub mimetype_guess_file {3068my$filename=shift;3069my$mimemap=shift;3070-r $mimemaporreturnundef;30713072my%mimemap;3073open(my$mh,'<',$mimemap)orreturnundef;3074while(<$mh>) {3075next ifm/^#/;# skip comments3076my($mimetype,$exts) =split(/\t+/);3077if(defined$exts) {3078my@exts=split(/\s+/,$exts);3079foreachmy$ext(@exts) {3080$mimemap{$ext} =$mimetype;3081}3082}3083}3084close($mh);30853086$filename=~/\.([^.]*)$/;3087return$mimemap{$1};3088}30893090sub mimetype_guess {3091my$filename=shift;3092my$mime;3093$filename=~/\./orreturnundef;30943095if($mimetypes_file) {3096my$file=$mimetypes_file;3097if($file!~m!^/!) {# if it is relative path3098# it is relative to project3099$file="$projectroot/$project/$file";3100}3101$mime= mimetype_guess_file($filename,$file);3102}3103$mime||= mimetype_guess_file($filename,'/etc/mime.types');3104return$mime;3105}31063107sub blob_mimetype {3108my$fd=shift;3109my$filename=shift;31103111if($filename) {3112my$mime= mimetype_guess($filename);3113$mimeandreturn$mime;3114}31153116# just in case3117return$default_blob_plain_mimetypeunless$fd;31183119if(-T $fd) {3120return'text/plain';3121}elsif(!$filename) {3122return'application/octet-stream';3123}elsif($filename=~m/\.png$/i) {3124return'image/png';3125}elsif($filename=~m/\.gif$/i) {3126return'image/gif';3127}elsif($filename=~m/\.jpe?g$/i) {3128return'image/jpeg';3129}else{3130return'application/octet-stream';3131}3132}31333134sub blob_contenttype {3135my($fd,$file_name,$type) =@_;31363137$type||= blob_mimetype($fd,$file_name);3138if($typeeq'text/plain'&&defined$default_text_plain_charset) {3139$type.="; charset=$default_text_plain_charset";3140}31413142return$type;3143}31443145## ======================================================================3146## functions printing HTML: header, footer, error page31473148sub git_header_html {3149my$status=shift||"200 OK";3150my$expires=shift;31513152my$title="$site_name";3153if(defined$project) {3154$title.=" - ". to_utf8($project);3155if(defined$action) {3156$title.="/$action";3157if(defined$file_name) {3158$title.=" - ". esc_path($file_name);3159if($actioneq"tree"&&$file_name!~ m|/$|) {3160$title.="/";3161}3162}3163}3164}3165my$content_type;3166# require explicit support from the UA if we are to send the page as3167# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3168# we have to do this because MSIE sometimes globs '*/*', pretending to3169# support xhtml+xml but choking when it gets what it asked for.3170if(defined$cgi->http('HTTP_ACCEPT') &&3171$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3172$cgi->Accept('application/xhtml+xml') !=0) {3173$content_type='application/xhtml+xml';3174}else{3175$content_type='text/html';3176}3177print$cgi->header(-type=>$content_type, -charset =>'utf-8',3178-status=>$status, -expires =>$expires);3179my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3180print<<EOF;3181<?xml version="1.0" encoding="utf-8"?>3182<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3183<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3184<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3185<!-- git core binaries version$git_version-->3186<head>3187<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3188<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3189<meta name="robots" content="index, nofollow"/>3190<title>$title</title>3191EOF3192# the stylesheet, favicon etc urls won't work correctly with path_info3193# unless we set the appropriate base URL3194if($ENV{'PATH_INFO'}) {3195print"<base href=\"".esc_url($base_url)."\"/>\n";3196}3197# print out each stylesheet that exist, providing backwards capability3198# for those people who defined $stylesheet in a config file3199if(defined$stylesheet) {3200print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3201}else{3202foreachmy$stylesheet(@stylesheets) {3203next unless$stylesheet;3204print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3205}3206}3207if(defined$project) {3208my%href_params= get_feed_info();3209if(!exists$href_params{'-title'}) {3210$href_params{'-title'} ='log';3211}32123213foreachmy$formatqw(RSS Atom){3214my$type=lc($format);3215my%link_attr= (3216'-rel'=>'alternate',3217'-title'=>"$project-$href_params{'-title'} -$formatfeed",3218'-type'=>"application/$type+xml"3219);32203221$href_params{'action'} =$type;3222$link_attr{'-href'} = href(%href_params);3223print"<link ".3224"rel=\"$link_attr{'-rel'}\"".3225"title=\"$link_attr{'-title'}\"".3226"href=\"$link_attr{'-href'}\"".3227"type=\"$link_attr{'-type'}\"".3228"/>\n";32293230$href_params{'extra_options'} ='--no-merges';3231$link_attr{'-href'} = href(%href_params);3232$link_attr{'-title'} .=' (no merges)';3233print"<link ".3234"rel=\"$link_attr{'-rel'}\"".3235"title=\"$link_attr{'-title'}\"".3236"href=\"$link_attr{'-href'}\"".3237"type=\"$link_attr{'-type'}\"".3238"/>\n";3239}32403241}else{3242printf('<link rel="alternate" title="%sprojects list" '.3243'href="%s" type="text/plain; charset=utf-8" />'."\n",3244$site_name, href(project=>undef, action=>"project_index"));3245printf('<link rel="alternate" title="%sprojects feeds" '.3246'href="%s" type="text/x-opml" />'."\n",3247$site_name, href(project=>undef, action=>"opml"));3248}3249if(defined$favicon) {3250printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3251}32523253print"</head>\n".3254"<body>\n";32553256if(defined$site_header&& -f $site_header) {3257 insert_file($site_header);3258}32593260print"<div class=\"page_header\">\n".3261$cgi->a({-href => esc_url($logo_url),3262-title =>$logo_label},3263qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3264print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3265if(defined$project) {3266print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3267if(defined$action) {3268print" /$action";3269}3270print"\n";3271}3272print"</div>\n";32733274my$have_search= gitweb_check_feature('search');3275if(defined$project&&$have_search) {3276if(!defined$searchtext) {3277$searchtext="";3278}3279my$search_hash;3280if(defined$hash_base) {3281$search_hash=$hash_base;3282}elsif(defined$hash) {3283$search_hash=$hash;3284}else{3285$search_hash="HEAD";3286}3287my$action=$my_uri;3288my$use_pathinfo= gitweb_check_feature('pathinfo');3289if($use_pathinfo) {3290$action.="/".esc_url($project);3291}3292print$cgi->startform(-method=>"get", -action =>$action) .3293"<div class=\"search\">\n".3294(!$use_pathinfo&&3295$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3296$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3297$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3298$cgi->popup_menu(-name =>'st', -default=>'commit',3299-values=> ['commit','grep','author','committer','pickaxe']) .3300$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3301" search:\n",3302$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3303"<span title=\"Extended regular expression\">".3304$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3305-checked =>$search_use_regexp) .3306"</span>".3307"</div>".3308$cgi->end_form() ."\n";3309}3310}33113312sub git_footer_html {3313my$feed_class='rss_logo';33143315print"<div class=\"page_footer\">\n";3316if(defined$project) {3317my$descr= git_get_project_description($project);3318if(defined$descr) {3319print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3320}33213322my%href_params= get_feed_info();3323if(!%href_params) {3324$feed_class.=' generic';3325}3326$href_params{'-title'} ||='log';33273328foreachmy$formatqw(RSS Atom){3329$href_params{'action'} =lc($format);3330print$cgi->a({-href => href(%href_params),3331-title =>"$href_params{'-title'}$formatfeed",3332-class=>$feed_class},$format)."\n";3333}33343335}else{3336print$cgi->a({-href => href(project=>undef, action=>"opml"),3337-class=>$feed_class},"OPML") ." ";3338print$cgi->a({-href => href(project=>undef, action=>"project_index"),3339-class=>$feed_class},"TXT") ."\n";3340}3341print"</div>\n";# class="page_footer"33423343if(defined$t0&& gitweb_check_feature('timed')) {3344print"<div id=\"generating_info\">\n";3345print'This page took '.3346'<span id="generating_time" class="time_span">'.3347 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3348' seconds </span>'.3349' and '.3350'<span id="generating_cmd">'.3351$number_of_git_cmds.3352'</span> git commands '.3353" to generate.\n";3354print"</div>\n";# class="page_footer"3355}33563357if(defined$site_footer&& -f $site_footer) {3358 insert_file($site_footer);3359}33603361print qq!<script type="text/javascript" src="$javascript"></script>\n!;3362if(defined$action&&3363$actioneq'blame_incremental') {3364print qq!<script type="text/javascript">\n!.3365 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3366 qq!"!. href() .qq!");\n!.3367 qq!</script>\n!;3368}elsif(gitweb_check_feature('javascript-actions')) {3369print qq!<script type="text/javascript">\n!.3370 qq!window.onload = fixLinks;\n!.3371 qq!</script>\n!;3372}33733374print"</body>\n".3375"</html>";3376}33773378# die_error(<http_status_code>, <error_message>)3379# Example: die_error(404, 'Hash not found')3380# By convention, use the following status codes (as defined in RFC 2616):3381# 400: Invalid or missing CGI parameters, or3382# requested object exists but has wrong type.3383# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3384# this server or project.3385# 404: Requested object/revision/project doesn't exist.3386# 500: The server isn't configured properly, or3387# an internal error occurred (e.g. failed assertions caused by bugs), or3388# an unknown error occurred (e.g. the git binary died unexpectedly).3389# 503: The server is currently unavailable (because it is overloaded,3390# or down for maintenance). Generally, this is a temporary state.3391sub die_error {3392my$status=shift||500;3393my$error=shift||"Internal server error";3394my$extra=shift;33953396my%http_responses= (3397400=>'400 Bad Request',3398403=>'403 Forbidden',3399404=>'404 Not Found',3400500=>'500 Internal Server Error',3401503=>'503 Service Unavailable',3402);3403 git_header_html($http_responses{$status});3404print<<EOF;3405<div class="page_body">3406<br /><br />3407$status-$error3408<br />3409EOF3410if(defined$extra) {3411print"<hr />\n".3412"$extra\n";3413}3414print"</div>\n";34153416 git_footer_html();3417exit;3418}34193420## ----------------------------------------------------------------------3421## functions printing or outputting HTML: navigation34223423sub git_print_page_nav {3424my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3425$extra=''if!defined$extra;# pager or formats34263427my@navs=qw(summary shortlog log commit commitdiff tree);3428if($suppress) {3429@navs=grep{$_ne$suppress}@navs;3430}34313432my%arg=map{$_=> {action=>$_} }@navs;3433if(defined$head) {3434for(qw(commit commitdiff)) {3435$arg{$_}{'hash'} =$head;3436}3437if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3438for(qw(shortlog log)) {3439$arg{$_}{'hash'} =$head;3440}3441}3442}34433444$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3445$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;34463447my@actions= gitweb_get_feature('actions');3448my%repl= (3449'%'=>'%',3450'n'=>$project,# project name3451'f'=>$git_dir,# project path within filesystem3452'h'=>$treehead||'',# current hash ('h' parameter)3453'b'=>$treebase||'',# hash base ('hb' parameter)3454);3455while(@actions) {3456my($label,$link,$pos) =splice(@actions,0,3);3457# insert3458@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3459# munch munch3460$link=~s/%([%nfhb])/$repl{$1}/g;3461$arg{$label}{'_href'} =$link;3462}34633464print"<div class=\"page_nav\">\n".3465(join" | ",3466map{$_eq$current?3467$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3468}@navs);3469print"<br/>\n$extra<br/>\n".3470"</div>\n";3471}34723473sub format_paging_nav {3474my($action,$page,$has_next_link) =@_;3475my$paging_nav;347634773478if($page>0) {3479$paging_nav.=3480$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3481" ⋅ ".3482$cgi->a({-href => href(-replay=>1, page=>$page-1),3483-accesskey =>"p", -title =>"Alt-p"},"prev");3484}else{3485$paging_nav.="first ⋅ prev";3486}34873488if($has_next_link) {3489$paging_nav.=" ⋅ ".3490$cgi->a({-href => href(-replay=>1, page=>$page+1),3491-accesskey =>"n", -title =>"Alt-n"},"next");3492}else{3493$paging_nav.=" ⋅ next";3494}34953496return$paging_nav;3497}34983499## ......................................................................3500## functions printing or outputting HTML: div35013502sub git_print_header_div {3503my($action,$title,$hash,$hash_base) =@_;3504my%args= ();35053506$args{'action'} =$action;3507$args{'hash'} =$hashif$hash;3508$args{'hash_base'} =$hash_baseif$hash_base;35093510print"<div class=\"header\">\n".3511$cgi->a({-href => href(%args), -class=>"title"},3512$title?$title:$action) .3513"\n</div>\n";3514}35153516sub print_local_time {3517print format_local_time(@_);3518}35193520sub format_local_time {3521my$localtime='';3522my%date=@_;3523if($date{'hour_local'} <6) {3524$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3525$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3526}else{3527$localtime.=sprintf(" (%02d:%02d%s)",3528$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3529}35303531return$localtime;3532}35333534# Outputs the author name and date in long form3535sub git_print_authorship {3536my$co=shift;3537my%opts=@_;3538my$tag=$opts{-tag} ||'div';3539my$author=$co->{'author_name'};35403541my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3542print"<$tagclass=\"author_date\">".3543 format_search_author($author,"author", esc_html($author)) .3544" [$ad{'rfc2822'}";3545 print_local_time(%ad)if($opts{-localtime});3546print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3547."</$tag>\n";3548}35493550# Outputs table rows containing the full author or committer information,3551# in the format expected for 'commit' view (& similia).3552# Parameters are a commit hash reference, followed by the list of people3553# to output information for. If the list is empty it defalts to both3554# author and committer.3555sub git_print_authorship_rows {3556my$co=shift;3557# too bad we can't use @people = @_ || ('author', 'committer')3558my@people=@_;3559@people= ('author','committer')unless@people;3560foreachmy$who(@people) {3561my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3562print"<tr><td>$who</td><td>".3563 format_search_author($co->{"${who}_name"},$who,3564 esc_html($co->{"${who}_name"})) ." ".3565 format_search_author($co->{"${who}_email"},$who,3566 esc_html("<".$co->{"${who}_email"} .">")) .3567"</td><td rowspan=\"2\">".3568 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3569"</td></tr>\n".3570"<tr>".3571"<td></td><td>$wd{'rfc2822'}";3572 print_local_time(%wd);3573print"</td>".3574"</tr>\n";3575}3576}35773578sub git_print_page_path {3579my$name=shift;3580my$type=shift;3581my$hb=shift;358235833584print"<div class=\"page_path\">";3585print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3586-title =>'tree root'}, to_utf8("[$project]"));3587print" / ";3588if(defined$name) {3589my@dirname=split'/',$name;3590my$basename=pop@dirname;3591my$fullname='';35923593foreachmy$dir(@dirname) {3594$fullname.= ($fullname?'/':'') .$dir;3595print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3596 hash_base=>$hb),3597-title =>$fullname}, esc_path($dir));3598print" / ";3599}3600if(defined$type&&$typeeq'blob') {3601print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3602 hash_base=>$hb),3603-title =>$name}, esc_path($basename));3604}elsif(defined$type&&$typeeq'tree') {3605print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3606 hash_base=>$hb),3607-title =>$name}, esc_path($basename));3608print" / ";3609}else{3610print esc_path($basename);3611}3612}3613print"<br/></div>\n";3614}36153616sub git_print_log {3617my$log=shift;3618my%opts=@_;36193620if($opts{'-remove_title'}) {3621# remove title, i.e. first line of log3622shift@$log;3623}3624# remove leading empty lines3625while(defined$log->[0] &&$log->[0]eq"") {3626shift@$log;3627}36283629# print log3630my$signoff=0;3631my$empty=0;3632foreachmy$line(@$log) {3633if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3634$signoff=1;3635$empty=0;3636if(!$opts{'-remove_signoff'}) {3637print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3638next;3639}else{3640# remove signoff lines3641next;3642}3643}else{3644$signoff=0;3645}36463647# print only one empty line3648# do not print empty line after signoff3649if($lineeq"") {3650next if($empty||$signoff);3651$empty=1;3652}else{3653$empty=0;3654}36553656print format_log_line_html($line) ."<br/>\n";3657}36583659if($opts{'-final_empty_line'}) {3660# end with single empty line3661print"<br/>\n"unless$empty;3662}3663}36643665# return link target (what link points to)3666sub git_get_link_target {3667my$hash=shift;3668my$link_target;36693670# read link3671open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3672orreturn;3673{3674local$/=undef;3675$link_target= <$fd>;3676}3677close$fd3678orreturn;36793680return$link_target;3681}36823683# given link target, and the directory (basedir) the link is in,3684# return target of link relative to top directory (top tree);3685# return undef if it is not possible (including absolute links).3686sub normalize_link_target {3687my($link_target,$basedir) =@_;36883689# absolute symlinks (beginning with '/') cannot be normalized3690return if(substr($link_target,0,1)eq'/');36913692# normalize link target to path from top (root) tree (dir)3693my$path;3694if($basedir) {3695$path=$basedir.'/'.$link_target;3696}else{3697# we are in top (root) tree (dir)3698$path=$link_target;3699}37003701# remove //, /./, and /../3702my@path_parts;3703foreachmy$part(split('/',$path)) {3704# discard '.' and ''3705next if(!$part||$parteq'.');3706# handle '..'3707if($parteq'..') {3708if(@path_parts) {3709pop@path_parts;3710}else{3711# link leads outside repository (outside top dir)3712return;3713}3714}else{3715push@path_parts,$part;3716}3717}3718$path=join('/',@path_parts);37193720return$path;3721}37223723# print tree entry (row of git_tree), but without encompassing <tr> element3724sub git_print_tree_entry {3725my($t,$basedir,$hash_base,$have_blame) =@_;37263727my%base_key= ();3728$base_key{'hash_base'} =$hash_baseifdefined$hash_base;37293730# The format of a table row is: mode list link. Where mode is3731# the mode of the entry, list is the name of the entry, an href,3732# and link is the action links of the entry.37333734print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3735if(exists$t->{'size'}) {3736print"<td class=\"size\">$t->{'size'}</td>\n";3737}3738if($t->{'type'}eq"blob") {3739print"<td class=\"list\">".3740$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3741 file_name=>"$basedir$t->{'name'}",%base_key),3742-class=>"list"}, esc_path($t->{'name'}));3743if(S_ISLNK(oct$t->{'mode'})) {3744my$link_target= git_get_link_target($t->{'hash'});3745if($link_target) {3746my$norm_target= normalize_link_target($link_target,$basedir);3747if(defined$norm_target) {3748print" -> ".3749$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3750 file_name=>$norm_target),3751-title =>$norm_target}, esc_path($link_target));3752}else{3753print" -> ". esc_path($link_target);3754}3755}3756}3757print"</td>\n";3758print"<td class=\"link\">";3759print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3760 file_name=>"$basedir$t->{'name'}",%base_key)},3761"blob");3762if($have_blame) {3763print" | ".3764$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3765 file_name=>"$basedir$t->{'name'}",%base_key)},3766"blame");3767}3768if(defined$hash_base) {3769print" | ".3770$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3771 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3772"history");3773}3774print" | ".3775$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3776 file_name=>"$basedir$t->{'name'}")},3777"raw");3778print"</td>\n";37793780}elsif($t->{'type'}eq"tree") {3781print"<td class=\"list\">";3782print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3783 file_name=>"$basedir$t->{'name'}",3784%base_key)},3785 esc_path($t->{'name'}));3786print"</td>\n";3787print"<td class=\"link\">";3788print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3789 file_name=>"$basedir$t->{'name'}",3790%base_key)},3791"tree");3792if(defined$hash_base) {3793print" | ".3794$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3795 file_name=>"$basedir$t->{'name'}")},3796"history");3797}3798print"</td>\n";3799}else{3800# unknown object: we can only present history for it3801# (this includes 'commit' object, i.e. submodule support)3802print"<td class=\"list\">".3803 esc_path($t->{'name'}) .3804"</td>\n";3805print"<td class=\"link\">";3806if(defined$hash_base) {3807print$cgi->a({-href => href(action=>"history",3808 hash_base=>$hash_base,3809 file_name=>"$basedir$t->{'name'}")},3810"history");3811}3812print"</td>\n";3813}3814}38153816## ......................................................................3817## functions printing large fragments of HTML38183819# get pre-image filenames for merge (combined) diff3820sub fill_from_file_info {3821my($diff,@parents) =@_;38223823$diff->{'from_file'} = [ ];3824$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3825for(my$i=0;$i<$diff->{'nparents'};$i++) {3826if($diff->{'status'}[$i]eq'R'||3827$diff->{'status'}[$i]eq'C') {3828$diff->{'from_file'}[$i] =3829 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3830}3831}38323833return$diff;3834}38353836# is current raw difftree line of file deletion3837sub is_deleted {3838my$diffinfo=shift;38393840return$diffinfo->{'to_id'}eq('0' x 40);3841}38423843# does patch correspond to [previous] difftree raw line3844# $diffinfo - hashref of parsed raw diff format3845# $patchinfo - hashref of parsed patch diff format3846# (the same keys as in $diffinfo)3847sub is_patch_split {3848my($diffinfo,$patchinfo) =@_;38493850returndefined$diffinfo&&defined$patchinfo3851&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3852}385338543855sub git_difftree_body {3856my($difftree,$hash,@parents) =@_;3857my($parent) =$parents[0];3858my$have_blame= gitweb_check_feature('blame');3859print"<div class=\"list_head\">\n";3860if($#{$difftree} >10) {3861print(($#{$difftree} +1) ." files changed:\n");3862}3863print"</div>\n";38643865print"<table class=\"".3866(@parents>1?"combined ":"") .3867"diff_tree\">\n";38683869# header only for combined diff in 'commitdiff' view3870my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3871if($has_header) {3872# table header3873print"<thead><tr>\n".3874"<th></th><th></th>\n";# filename, patchN link3875for(my$i=0;$i<@parents;$i++) {3876my$par=$parents[$i];3877print"<th>".3878$cgi->a({-href => href(action=>"commitdiff",3879 hash=>$hash, hash_parent=>$par),3880-title =>'commitdiff to parent number '.3881($i+1) .': '.substr($par,0,7)},3882$i+1) .3883" </th>\n";3884}3885print"</tr></thead>\n<tbody>\n";3886}38873888my$alternate=1;3889my$patchno=0;3890foreachmy$line(@{$difftree}) {3891my$diff= parsed_difftree_line($line);38923893if($alternate) {3894print"<tr class=\"dark\">\n";3895}else{3896print"<tr class=\"light\">\n";3897}3898$alternate^=1;38993900if(exists$diff->{'nparents'}) {# combined diff39013902 fill_from_file_info($diff,@parents)3903unlessexists$diff->{'from_file'};39043905if(!is_deleted($diff)) {3906# file exists in the result (child) commit3907print"<td>".3908$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3909 file_name=>$diff->{'to_file'},3910 hash_base=>$hash),3911-class=>"list"}, esc_path($diff->{'to_file'})) .3912"</td>\n";3913}else{3914print"<td>".3915 esc_path($diff->{'to_file'}) .3916"</td>\n";3917}39183919if($actioneq'commitdiff') {3920# link to patch3921$patchno++;3922print"<td class=\"link\">".3923$cgi->a({-href =>"#patch$patchno"},"patch") .3924" | ".3925"</td>\n";3926}39273928my$has_history=0;3929my$not_deleted=0;3930for(my$i=0;$i<$diff->{'nparents'};$i++) {3931my$hash_parent=$parents[$i];3932my$from_hash=$diff->{'from_id'}[$i];3933my$from_path=$diff->{'from_file'}[$i];3934my$status=$diff->{'status'}[$i];39353936$has_history||= ($statusne'A');3937$not_deleted||= ($statusne'D');39383939if($statuseq'A') {3940print"<td class=\"link\"align=\"right\"> | </td>\n";3941}elsif($statuseq'D') {3942print"<td class=\"link\">".3943$cgi->a({-href => href(action=>"blob",3944 hash_base=>$hash,3945 hash=>$from_hash,3946 file_name=>$from_path)},3947"blob". ($i+1)) .3948" | </td>\n";3949}else{3950if($diff->{'to_id'}eq$from_hash) {3951print"<td class=\"link nochange\">";3952}else{3953print"<td class=\"link\">";3954}3955print$cgi->a({-href => href(action=>"blobdiff",3956 hash=>$diff->{'to_id'},3957 hash_parent=>$from_hash,3958 hash_base=>$hash,3959 hash_parent_base=>$hash_parent,3960 file_name=>$diff->{'to_file'},3961 file_parent=>$from_path)},3962"diff". ($i+1)) .3963" | </td>\n";3964}3965}39663967print"<td class=\"link\">";3968if($not_deleted) {3969print$cgi->a({-href => href(action=>"blob",3970 hash=>$diff->{'to_id'},3971 file_name=>$diff->{'to_file'},3972 hash_base=>$hash)},3973"blob");3974print" | "if($has_history);3975}3976if($has_history) {3977print$cgi->a({-href => href(action=>"history",3978 file_name=>$diff->{'to_file'},3979 hash_base=>$hash)},3980"history");3981}3982print"</td>\n";39833984print"</tr>\n";3985next;# instead of 'else' clause, to avoid extra indent3986}3987# else ordinary diff39883989my($to_mode_oct,$to_mode_str,$to_file_type);3990my($from_mode_oct,$from_mode_str,$from_file_type);3991if($diff->{'to_mode'}ne('0' x 6)) {3992$to_mode_oct=oct$diff->{'to_mode'};3993if(S_ISREG($to_mode_oct)) {# only for regular file3994$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3995}3996$to_file_type= file_type($diff->{'to_mode'});3997}3998if($diff->{'from_mode'}ne('0' x 6)) {3999$from_mode_oct=oct$diff->{'from_mode'};4000if(S_ISREG($to_mode_oct)) {# only for regular file4001$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4002}4003$from_file_type= file_type($diff->{'from_mode'});4004}40054006if($diff->{'status'}eq"A") {# created4007my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4008$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4009$mode_chng.="]</span>";4010print"<td>";4011print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4012 hash_base=>$hash, file_name=>$diff->{'file'}),4013-class=>"list"}, esc_path($diff->{'file'}));4014print"</td>\n";4015print"<td>$mode_chng</td>\n";4016print"<td class=\"link\">";4017if($actioneq'commitdiff') {4018# link to patch4019$patchno++;4020print$cgi->a({-href =>"#patch$patchno"},"patch");4021print" | ";4022}4023print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4024 hash_base=>$hash, file_name=>$diff->{'file'})},4025"blob");4026print"</td>\n";40274028}elsif($diff->{'status'}eq"D") {# deleted4029my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4030print"<td>";4031print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4032 hash_base=>$parent, file_name=>$diff->{'file'}),4033-class=>"list"}, esc_path($diff->{'file'}));4034print"</td>\n";4035print"<td>$mode_chng</td>\n";4036print"<td class=\"link\">";4037if($actioneq'commitdiff') {4038# link to patch4039$patchno++;4040print$cgi->a({-href =>"#patch$patchno"},"patch");4041print" | ";4042}4043print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4044 hash_base=>$parent, file_name=>$diff->{'file'})},4045"blob") ." | ";4046if($have_blame) {4047print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4048 file_name=>$diff->{'file'})},4049"blame") ." | ";4050}4051print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4052 file_name=>$diff->{'file'})},4053"history");4054print"</td>\n";40554056}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4057my$mode_chnge="";4058if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4059$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4060if($from_file_typene$to_file_type) {4061$mode_chnge.=" from$from_file_typeto$to_file_type";4062}4063if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4064if($from_mode_str&&$to_mode_str) {4065$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4066}elsif($to_mode_str) {4067$mode_chnge.=" mode:$to_mode_str";4068}4069}4070$mode_chnge.="]</span>\n";4071}4072print"<td>";4073print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4074 hash_base=>$hash, file_name=>$diff->{'file'}),4075-class=>"list"}, esc_path($diff->{'file'}));4076print"</td>\n";4077print"<td>$mode_chnge</td>\n";4078print"<td class=\"link\">";4079if($actioneq'commitdiff') {4080# link to patch4081$patchno++;4082print$cgi->a({-href =>"#patch$patchno"},"patch") .4083" | ";4084}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4085# "commit" view and modified file (not onlu mode changed)4086print$cgi->a({-href => href(action=>"blobdiff",4087 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4088 hash_base=>$hash, hash_parent_base=>$parent,4089 file_name=>$diff->{'file'})},4090"diff") .4091" | ";4092}4093print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4094 hash_base=>$hash, file_name=>$diff->{'file'})},4095"blob") ." | ";4096if($have_blame) {4097print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4098 file_name=>$diff->{'file'})},4099"blame") ." | ";4100}4101print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4102 file_name=>$diff->{'file'})},4103"history");4104print"</td>\n";41054106}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4107my%status_name= ('R'=>'moved','C'=>'copied');4108my$nstatus=$status_name{$diff->{'status'}};4109my$mode_chng="";4110if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4111# mode also for directories, so we cannot use $to_mode_str4112$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4113}4114print"<td>".4115$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4116 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4117-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4118"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4119$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4120 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4121-class=>"list"}, esc_path($diff->{'from_file'})) .4122" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4123"<td class=\"link\">";4124if($actioneq'commitdiff') {4125# link to patch4126$patchno++;4127print$cgi->a({-href =>"#patch$patchno"},"patch") .4128" | ";4129}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4130# "commit" view and modified file (not only pure rename or copy)4131print$cgi->a({-href => href(action=>"blobdiff",4132 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4133 hash_base=>$hash, hash_parent_base=>$parent,4134 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4135"diff") .4136" | ";4137}4138print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4139 hash_base=>$parent, file_name=>$diff->{'to_file'})},4140"blob") ." | ";4141if($have_blame) {4142print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4143 file_name=>$diff->{'to_file'})},4144"blame") ." | ";4145}4146print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4147 file_name=>$diff->{'to_file'})},4148"history");4149print"</td>\n";41504151}# we should not encounter Unmerged (U) or Unknown (X) status4152print"</tr>\n";4153}4154print"</tbody>"if$has_header;4155print"</table>\n";4156}41574158sub git_patchset_body {4159my($fd,$difftree,$hash,@hash_parents) =@_;4160my($hash_parent) =$hash_parents[0];41614162my$is_combined= (@hash_parents>1);4163my$patch_idx=0;4164my$patch_number=0;4165my$patch_line;4166my$diffinfo;4167my$to_name;4168my(%from,%to);41694170print"<div class=\"patchset\">\n";41714172# skip to first patch4173while($patch_line= <$fd>) {4174chomp$patch_line;41754176last if($patch_line=~m/^diff /);4177}41784179 PATCH:4180while($patch_line) {41814182# parse "git diff" header line4183if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4184# $1 is from_name, which we do not use4185$to_name= unquote($2);4186$to_name=~s!^b/!!;4187}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4188# $1 is 'cc' or 'combined', which we do not use4189$to_name= unquote($2);4190}else{4191$to_name=undef;4192}41934194# check if current patch belong to current raw line4195# and parse raw git-diff line if needed4196if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4197# this is continuation of a split patch4198print"<div class=\"patch cont\">\n";4199}else{4200# advance raw git-diff output if needed4201$patch_idx++ifdefined$diffinfo;42024203# read and prepare patch information4204$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42054206# compact combined diff output can have some patches skipped4207# find which patch (using pathname of result) we are at now;4208if($is_combined) {4209while($to_namene$diffinfo->{'to_file'}) {4210print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4211 format_diff_cc_simplified($diffinfo,@hash_parents) .4212"</div>\n";# class="patch"42134214$patch_idx++;4215$patch_number++;42164217last if$patch_idx>$#$difftree;4218$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4219}4220}42214222# modifies %from, %to hashes4223 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);42244225# this is first patch for raw difftree line with $patch_idx index4226# we index @$difftree array from 0, but number patches from 14227print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4228}42294230# git diff header4231#assert($patch_line =~ m/^diff /) if DEBUG;4232#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4233$patch_number++;4234# print "git diff" header4235print format_git_diff_header_line($patch_line,$diffinfo,4236 \%from, \%to);42374238# print extended diff header4239print"<div class=\"diff extended_header\">\n";4240 EXTENDED_HEADER:4241while($patch_line= <$fd>) {4242chomp$patch_line;42434244last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);42454246print format_extended_diff_header_line($patch_line,$diffinfo,4247 \%from, \%to);4248}4249print"</div>\n";# class="diff extended_header"42504251# from-file/to-file diff header4252if(!$patch_line) {4253print"</div>\n";# class="patch"4254last PATCH;4255}4256next PATCH if($patch_line=~m/^diff /);4257#assert($patch_line =~ m/^---/) if DEBUG;42584259my$last_patch_line=$patch_line;4260$patch_line= <$fd>;4261chomp$patch_line;4262#assert($patch_line =~ m/^\+\+\+/) if DEBUG;42634264print format_diff_from_to_header($last_patch_line,$patch_line,4265$diffinfo, \%from, \%to,4266@hash_parents);42674268# the patch itself4269 LINE:4270while($patch_line= <$fd>) {4271chomp$patch_line;42724273next PATCH if($patch_line=~m/^diff /);42744275print format_diff_line($patch_line, \%from, \%to);4276}42774278}continue{4279print"</div>\n";# class="patch"4280}42814282# for compact combined (--cc) format, with chunk and patch simpliciaction4283# patchset might be empty, but there might be unprocessed raw lines4284for(++$patch_idxif$patch_number>0;4285$patch_idx<@$difftree;4286++$patch_idx) {4287# read and prepare patch information4288$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42894290# generate anchor for "patch" links in difftree / whatchanged part4291print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4292 format_diff_cc_simplified($diffinfo,@hash_parents) .4293"</div>\n";# class="patch"42944295$patch_number++;4296}42974298if($patch_number==0) {4299if(@hash_parents>1) {4300print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4301}else{4302print"<div class=\"diff nodifferences\">No differences found</div>\n";4303}4304}43054306print"</div>\n";# class="patchset"4307}43084309# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43104311# fills project list info (age, description, owner, forks) for each4312# project in the list, removing invalid projects from returned list4313# NOTE: modifies $projlist, but does not remove entries from it4314sub fill_project_list_info {4315my($projlist,$check_forks) =@_;4316my@projects;43174318my$show_ctags= gitweb_check_feature('ctags');4319 PROJECT:4320foreachmy$pr(@$projlist) {4321my(@activity) = git_get_last_activity($pr->{'path'});4322unless(@activity) {4323next PROJECT;4324}4325($pr->{'age'},$pr->{'age_string'}) =@activity;4326if(!defined$pr->{'descr'}) {4327my$descr= git_get_project_description($pr->{'path'}) ||"";4328$descr= to_utf8($descr);4329$pr->{'descr_long'} =$descr;4330$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4331}4332if(!defined$pr->{'owner'}) {4333$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4334}4335if($check_forks) {4336my$pname=$pr->{'path'};4337if(($pname=~s/\.git$//) &&4338($pname!~/\/$/) &&4339(-d "$projectroot/$pname")) {4340$pr->{'forks'} ="-d$projectroot/$pname";4341}else{4342$pr->{'forks'} =0;4343}4344}4345$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4346push@projects,$pr;4347}43484349return@projects;4350}43514352# print 'sort by' <th> element, generating 'sort by $name' replay link4353# if that order is not selected4354sub print_sort_th {4355print format_sort_th(@_);4356}43574358sub format_sort_th {4359my($name,$order,$header) =@_;4360my$sort_th="";4361$header||=ucfirst($name);43624363if($ordereq$name) {4364$sort_th.="<th>$header</th>\n";4365}else{4366$sort_th.="<th>".4367$cgi->a({-href => href(-replay=>1, order=>$name),4368-class=>"header"},$header) .4369"</th>\n";4370}43714372return$sort_th;4373}43744375sub git_project_list_body {4376# actually uses global variable $project4377my($projlist,$order,$from,$to,$extra,$no_header) =@_;43784379my$check_forks= gitweb_check_feature('forks');4380my@projects= fill_project_list_info($projlist,$check_forks);43814382$order||=$default_projects_order;4383$from=0unlessdefined$from;4384$to=$#projectsif(!defined$to||$#projects<$to);43854386my%order_info= (4387 project => { key =>'path', type =>'str'},4388 descr => { key =>'descr_long', type =>'str'},4389 owner => { key =>'owner', type =>'str'},4390 age => { key =>'age', type =>'num'}4391);4392my$oi=$order_info{$order};4393if($oi->{'type'}eq'str') {4394@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4395}else{4396@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4397}43984399my$show_ctags= gitweb_check_feature('ctags');4400if($show_ctags) {4401my%ctags;4402foreachmy$p(@projects) {4403foreachmy$ct(keys%{$p->{'ctags'}}) {4404$ctags{$ct} +=$p->{'ctags'}->{$ct};4405}4406}4407my$cloud= git_populate_project_tagcloud(\%ctags);4408print git_show_project_tagcloud($cloud,64);4409}44104411print"<table class=\"project_list\">\n";4412unless($no_header) {4413print"<tr>\n";4414if($check_forks) {4415print"<th></th>\n";4416}4417 print_sort_th('project',$order,'Project');4418 print_sort_th('descr',$order,'Description');4419 print_sort_th('owner',$order,'Owner');4420 print_sort_th('age',$order,'Last Change');4421print"<th></th>\n".# for links4422"</tr>\n";4423}4424my$alternate=1;4425my$tagfilter=$cgi->param('by_tag');4426for(my$i=$from;$i<=$to;$i++) {4427my$pr=$projects[$i];44284429next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4430next if$searchtextand not$pr->{'path'} =~/$searchtext/4431and not$pr->{'descr_long'} =~/$searchtext/;4432# Weed out forks or non-matching entries of search4433if($check_forks) {4434my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4435$forkbase="^$forkbase"if$forkbase;4436next ifnot$searchtextand not$tagfilterand$show_ctags4437and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4438}44394440if($alternate) {4441print"<tr class=\"dark\">\n";4442}else{4443print"<tr class=\"light\">\n";4444}4445$alternate^=1;4446if($check_forks) {4447print"<td>";4448if($pr->{'forks'}) {4449print"<!--$pr->{'forks'} -->\n";4450print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4451}4452print"</td>\n";4453}4454print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4455-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4456"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4457-class=>"list", -title =>$pr->{'descr_long'}},4458 esc_html($pr->{'descr'})) ."</td>\n".4459"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4460print"<td class=\"". age_class($pr->{'age'}) ."\">".4461(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4462"<td class=\"link\">".4463$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4464$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4465$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4466$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4467($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4468"</td>\n".4469"</tr>\n";4470}4471if(defined$extra) {4472print"<tr>\n";4473if($check_forks) {4474print"<td></td>\n";4475}4476print"<td colspan=\"5\">$extra</td>\n".4477"</tr>\n";4478}4479print"</table>\n";4480}44814482sub git_log_body {4483# uses global variable $project4484my($commitlist,$from,$to,$refs,$extra) =@_;44854486$from=0unlessdefined$from;4487$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);44884489for(my$i=0;$i<=$to;$i++) {4490my%co= %{$commitlist->[$i]};4491next if!%co;4492my$commit=$co{'id'};4493my$ref= format_ref_marker($refs,$commit);4494my%ad= parse_date($co{'author_epoch'});4495 git_print_header_div('commit',4496"<span class=\"age\">$co{'age_string'}</span>".4497 esc_html($co{'title'}) .$ref,4498$commit);4499print"<div class=\"title_text\">\n".4500"<div class=\"log_link\">\n".4501$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4502" | ".4503$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4504" | ".4505$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4506"<br/>\n".4507"</div>\n";4508 git_print_authorship(\%co, -tag =>'span');4509print"<br/>\n</div>\n";45104511print"<div class=\"log_body\">\n";4512 git_print_log($co{'comment'}, -final_empty_line=>1);4513print"</div>\n";4514}4515if($extra) {4516print"<div class=\"page_nav\">\n";4517print"$extra\n";4518print"</div>\n";4519}4520}45214522sub git_shortlog_body {4523# uses global variable $project4524my($commitlist,$from,$to,$refs,$extra) =@_;45254526$from=0unlessdefined$from;4527$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45284529print"<table class=\"shortlog\">\n";4530my$alternate=1;4531for(my$i=$from;$i<=$to;$i++) {4532my%co= %{$commitlist->[$i]};4533my$commit=$co{'id'};4534my$ref= format_ref_marker($refs,$commit);4535if($alternate) {4536print"<tr class=\"dark\">\n";4537}else{4538print"<tr class=\"light\">\n";4539}4540$alternate^=1;4541# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4542print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4543 format_author_html('td', \%co,10) ."<td>";4544print format_subject_html($co{'title'},$co{'title_short'},4545 href(action=>"commit", hash=>$commit),$ref);4546print"</td>\n".4547"<td class=\"link\">".4548$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4549$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4550$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4551my$snapshot_links= format_snapshot_links($commit);4552if(defined$snapshot_links) {4553print" | ".$snapshot_links;4554}4555print"</td>\n".4556"</tr>\n";4557}4558if(defined$extra) {4559print"<tr>\n".4560"<td colspan=\"4\">$extra</td>\n".4561"</tr>\n";4562}4563print"</table>\n";4564}45654566sub git_history_body {4567# Warning: assumes constant type (blob or tree) during history4568my($commitlist,$from,$to,$refs,$extra,4569$file_name,$file_hash,$ftype) =@_;45704571$from=0unlessdefined$from;4572$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});45734574print"<table class=\"history\">\n";4575my$alternate=1;4576for(my$i=$from;$i<=$to;$i++) {4577my%co= %{$commitlist->[$i]};4578if(!%co) {4579next;4580}4581my$commit=$co{'id'};45824583my$ref= format_ref_marker($refs,$commit);45844585if($alternate) {4586print"<tr class=\"dark\">\n";4587}else{4588print"<tr class=\"light\">\n";4589}4590$alternate^=1;4591print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4592# shortlog: format_author_html('td', \%co, 10)4593 format_author_html('td', \%co,15,3) ."<td>";4594# originally git_history used chop_str($co{'title'}, 50)4595print format_subject_html($co{'title'},$co{'title_short'},4596 href(action=>"commit", hash=>$commit),$ref);4597print"</td>\n".4598"<td class=\"link\">".4599$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4600$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");46014602if($ftypeeq'blob') {4603my$blob_current=$file_hash;4604my$blob_parent= git_get_hash_by_path($commit,$file_name);4605if(defined$blob_current&&defined$blob_parent&&4606$blob_currentne$blob_parent) {4607print" | ".4608$cgi->a({-href => href(action=>"blobdiff",4609 hash=>$blob_current, hash_parent=>$blob_parent,4610 hash_base=>$hash_base, hash_parent_base=>$commit,4611 file_name=>$file_name)},4612"diff to current");4613}4614}4615print"</td>\n".4616"</tr>\n";4617}4618if(defined$extra) {4619print"<tr>\n".4620"<td colspan=\"4\">$extra</td>\n".4621"</tr>\n";4622}4623print"</table>\n";4624}46254626sub git_tags_body {4627# uses global variable $project4628my($taglist,$from,$to,$extra) =@_;4629$from=0unlessdefined$from;4630$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);46314632print"<table class=\"tags\">\n";4633my$alternate=1;4634for(my$i=$from;$i<=$to;$i++) {4635my$entry=$taglist->[$i];4636my%tag=%$entry;4637my$comment=$tag{'subject'};4638my$comment_short;4639if(defined$comment) {4640$comment_short= chop_str($comment,30,5);4641}4642if($alternate) {4643print"<tr class=\"dark\">\n";4644}else{4645print"<tr class=\"light\">\n";4646}4647$alternate^=1;4648if(defined$tag{'age'}) {4649print"<td><i>$tag{'age'}</i></td>\n";4650}else{4651print"<td></td>\n";4652}4653print"<td>".4654$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4655-class=>"list name"}, esc_html($tag{'name'})) .4656"</td>\n".4657"<td>";4658if(defined$comment) {4659print format_subject_html($comment,$comment_short,4660 href(action=>"tag", hash=>$tag{'id'}));4661}4662print"</td>\n".4663"<td class=\"selflink\">";4664if($tag{'type'}eq"tag") {4665print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4666}else{4667print" ";4668}4669print"</td>\n".4670"<td class=\"link\">"." | ".4671$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4672if($tag{'reftype'}eq"commit") {4673print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4674" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4675}elsif($tag{'reftype'}eq"blob") {4676print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4677}4678print"</td>\n".4679"</tr>";4680}4681if(defined$extra) {4682print"<tr>\n".4683"<td colspan=\"5\">$extra</td>\n".4684"</tr>\n";4685}4686print"</table>\n";4687}46884689sub git_heads_body {4690# uses global variable $project4691my($headlist,$head,$from,$to,$extra) =@_;4692$from=0unlessdefined$from;4693$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);46944695print"<table class=\"heads\">\n";4696my$alternate=1;4697for(my$i=$from;$i<=$to;$i++) {4698my$entry=$headlist->[$i];4699my%ref=%$entry;4700my$curr=$ref{'id'}eq$head;4701if($alternate) {4702print"<tr class=\"dark\">\n";4703}else{4704print"<tr class=\"light\">\n";4705}4706$alternate^=1;4707print"<td><i>$ref{'age'}</i></td>\n".4708($curr?"<td class=\"current_head\">":"<td>") .4709$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4710-class=>"list name"},esc_html($ref{'name'})) .4711"</td>\n".4712"<td class=\"link\">".4713$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4714$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4715$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4716"</td>\n".4717"</tr>";4718}4719if(defined$extra) {4720print"<tr>\n".4721"<td colspan=\"3\">$extra</td>\n".4722"</tr>\n";4723}4724print"</table>\n";4725}47264727sub git_search_grep_body {4728my($commitlist,$from,$to,$extra) =@_;4729$from=0unlessdefined$from;4730$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47314732print"<table class=\"commit_search\">\n";4733my$alternate=1;4734for(my$i=$from;$i<=$to;$i++) {4735my%co= %{$commitlist->[$i]};4736if(!%co) {4737next;4738}4739my$commit=$co{'id'};4740if($alternate) {4741print"<tr class=\"dark\">\n";4742}else{4743print"<tr class=\"light\">\n";4744}4745$alternate^=1;4746print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4747 format_author_html('td', \%co,15,5) .4748"<td>".4749$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4750-class=>"list subject"},4751 chop_and_escape_str($co{'title'},50) ."<br/>");4752my$comment=$co{'comment'};4753foreachmy$line(@$comment) {4754if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4755my($lead,$match,$trail) = ($1,$2,$3);4756$match= chop_str($match,70,5,'center');4757my$contextlen=int((80-length($match))/2);4758$contextlen=30if($contextlen>30);4759$lead= chop_str($lead,$contextlen,10,'left');4760$trail= chop_str($trail,$contextlen,10,'right');47614762$lead= esc_html($lead);4763$match= esc_html($match);4764$trail= esc_html($trail);47654766print"$lead<span class=\"match\">$match</span>$trail<br />";4767}4768}4769print"</td>\n".4770"<td class=\"link\">".4771$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4772" | ".4773$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4774" | ".4775$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4776print"</td>\n".4777"</tr>\n";4778}4779if(defined$extra) {4780print"<tr>\n".4781"<td colspan=\"3\">$extra</td>\n".4782"</tr>\n";4783}4784print"</table>\n";4785}47864787## ======================================================================4788## ======================================================================4789## actions47904791sub git_project_list {4792my$order=$input_params{'order'};4793if(defined$order&&$order!~m/none|project|descr|owner|age/) {4794 die_error(400,"Unknown order parameter");4795}47964797my@list= git_get_projects_list();4798if(!@list) {4799 die_error(404,"No projects found");4800}48014802 git_header_html();4803if(defined$home_text&& -f $home_text) {4804print"<div class=\"index_include\">\n";4805 insert_file($home_text);4806print"</div>\n";4807}4808print$cgi->startform(-method=>"get") .4809"<p class=\"projsearch\">Search:\n".4810$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4811"</p>".4812$cgi->end_form() ."\n";4813 git_project_list_body(\@list,$order);4814 git_footer_html();4815}48164817sub git_forks {4818my$order=$input_params{'order'};4819if(defined$order&&$order!~m/none|project|descr|owner|age/) {4820 die_error(400,"Unknown order parameter");4821}48224823my@list= git_get_projects_list($project);4824if(!@list) {4825 die_error(404,"No forks found");4826}48274828 git_header_html();4829 git_print_page_nav('','');4830 git_print_header_div('summary',"$projectforks");4831 git_project_list_body(\@list,$order);4832 git_footer_html();4833}48344835sub git_project_index {4836my@projects= git_get_projects_list($project);48374838print$cgi->header(4839-type =>'text/plain',4840-charset =>'utf-8',4841-content_disposition =>'inline; filename="index.aux"');48424843foreachmy$pr(@projects) {4844if(!exists$pr->{'owner'}) {4845$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4846}48474848my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4849# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4850$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4851$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4852$path=~s/ /\+/g;4853$owner=~s/ /\+/g;48544855print"$path$owner\n";4856}4857}48584859sub git_summary {4860my$descr= git_get_project_description($project) ||"none";4861my%co= parse_commit("HEAD");4862my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4863my$head=$co{'id'};48644865my$owner= git_get_project_owner($project);48664867my$refs= git_get_references();4868# These get_*_list functions return one more to allow us to see if4869# there are more ...4870my@taglist= git_get_tags_list(16);4871my@headlist= git_get_heads_list(16);4872my@forklist;4873my$check_forks= gitweb_check_feature('forks');48744875if($check_forks) {4876@forklist= git_get_projects_list($project);4877}48784879 git_header_html();4880 git_print_page_nav('summary','',$head);48814882print"<div class=\"title\"> </div>\n";4883print"<table class=\"projects_list\">\n".4884"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4885"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4886if(defined$cd{'rfc2822'}) {4887print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4888}48894890# use per project git URL list in $projectroot/$project/cloneurl4891# or make project git URL from git base URL and project name4892my$url_tag="URL";4893my@url_list= git_get_project_url_list($project);4894@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4895foreachmy$git_url(@url_list) {4896next unless$git_url;4897print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4898$url_tag="";4899}49004901# Tag cloud4902my$show_ctags= gitweb_check_feature('ctags');4903if($show_ctags) {4904my$ctags= git_get_project_ctags($project);4905my$cloud= git_populate_project_tagcloud($ctags);4906print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4907print"</td>\n<td>"unless%$ctags;4908print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4909print"</td>\n<td>"if%$ctags;4910print git_show_project_tagcloud($cloud,48);4911print"</td></tr>";4912}49134914print"</table>\n";49154916# If XSS prevention is on, we don't include README.html.4917# TODO: Allow a readme in some safe format.4918if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4919print"<div class=\"title\">readme</div>\n".4920"<div class=\"readme\">\n";4921 insert_file("$projectroot/$project/README.html");4922print"\n</div>\n";# class="readme"4923}49244925# we need to request one more than 16 (0..15) to check if4926# those 16 are all4927my@commitlist=$head? parse_commits($head,17) : ();4928if(@commitlist) {4929 git_print_header_div('shortlog');4930 git_shortlog_body(\@commitlist,0,15,$refs,4931$#commitlist<=15?undef:4932$cgi->a({-href => href(action=>"shortlog")},"..."));4933}49344935if(@taglist) {4936 git_print_header_div('tags');4937 git_tags_body(\@taglist,0,15,4938$#taglist<=15?undef:4939$cgi->a({-href => href(action=>"tags")},"..."));4940}49414942if(@headlist) {4943 git_print_header_div('heads');4944 git_heads_body(\@headlist,$head,0,15,4945$#headlist<=15?undef:4946$cgi->a({-href => href(action=>"heads")},"..."));4947}49484949if(@forklist) {4950 git_print_header_div('forks');4951 git_project_list_body(\@forklist,'age',0,15,4952$#forklist<=15?undef:4953$cgi->a({-href => href(action=>"forks")},"..."),4954'no_header');4955}49564957 git_footer_html();4958}49594960sub git_tag {4961my$head= git_get_head_hash($project);4962 git_header_html();4963 git_print_page_nav('','',$head,undef,$head);4964my%tag= parse_tag($hash);49654966if(!%tag) {4967 die_error(404,"Unknown tag object");4968}49694970 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4971print"<div class=\"title_text\">\n".4972"<table class=\"object_header\">\n".4973"<tr>\n".4974"<td>object</td>\n".4975"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4976$tag{'object'}) ."</td>\n".4977"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4978$tag{'type'}) ."</td>\n".4979"</tr>\n";4980if(defined($tag{'author'})) {4981 git_print_authorship_rows(\%tag,'author');4982}4983print"</table>\n\n".4984"</div>\n";4985print"<div class=\"page_body\">";4986my$comment=$tag{'comment'};4987foreachmy$line(@$comment) {4988chomp$line;4989print esc_html($line, -nbsp=>1) ."<br/>\n";4990}4991print"</div>\n";4992 git_footer_html();4993}49944995sub git_blame_common {4996my$format=shift||'porcelain';4997if($formateq'porcelain'&&$cgi->param('js')) {4998$format='incremental';4999$action='blame_incremental';# for page title etc5000}50015002# permissions5003 gitweb_check_feature('blame')5004or die_error(403,"Blame view not allowed");50055006# error checking5007 die_error(400,"No file name given")unless$file_name;5008$hash_base||= git_get_head_hash($project);5009 die_error(404,"Couldn't find base commit")unless$hash_base;5010my%co= parse_commit($hash_base)5011or die_error(404,"Commit not found");5012my$ftype="blob";5013if(!defined$hash) {5014$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5015or die_error(404,"Error looking up file");5016}else{5017$ftype= git_get_type($hash);5018if($ftype!~"blob") {5019 die_error(400,"Object is not a blob");5020}5021}50225023my$fd;5024if($formateq'incremental') {5025# get file contents (as base)5026open$fd,"-|", git_cmd(),'cat-file','blob',$hash5027or die_error(500,"Open git-cat-file failed");5028}elsif($formateq'data') {5029# run git-blame --incremental5030open$fd,"-|", git_cmd(),"blame","--incremental",5031$hash_base,"--",$file_name5032or die_error(500,"Open git-blame --incremental failed");5033}else{5034# run git-blame --porcelain5035open$fd,"-|", git_cmd(),"blame",'-p',5036$hash_base,'--',$file_name5037or die_error(500,"Open git-blame --porcelain failed");5038}50395040# incremental blame data returns early5041if($formateq'data') {5042print$cgi->header(5043-type=>"text/plain", -charset =>"utf-8",5044-status=>"200 OK");5045local$| =1;# output autoflush5046printwhile<$fd>;5047close$fd5048or print"ERROR$!\n";50495050print'END';5051if(defined$t0&& gitweb_check_feature('timed')) {5052print' '.5053 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5054' '.$number_of_git_cmds;5055}5056print"\n";50575058return;5059}50605061# page header5062 git_header_html();5063my$formats_nav=5064$cgi->a({-href => href(action=>"blob", -replay=>1)},5065"blob") .5066" | ";5067if($formateq'incremental') {5068$formats_nav.=5069$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5070"blame") ." (non-incremental)";5071}else{5072$formats_nav.=5073$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5074"blame") ." (incremental)";5075}5076$formats_nav.=5077" | ".5078$cgi->a({-href => href(action=>"history", -replay=>1)},5079"history") .5080" | ".5081$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5082"HEAD");5083 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5084 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5085 git_print_page_path($file_name,$ftype,$hash_base);50865087# page body5088if($formateq'incremental') {5089print"<noscript>\n<div class=\"error\"><center><b>\n".5090"This page requires JavaScript to run.\nUse ".5091$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5092'this page').5093" instead.\n".5094"</b></center></div>\n</noscript>\n";50955096print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5097}50985099print qq!<div class="page_body">\n!;5100print qq!<div id="progress_info">.../ ...</div>\n!5101if($formateq'incremental');5102print qq!<table id="blame_table"class="blame" width="100%">\n!.5103#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5104 qq!<thead>\n!.5105 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5106 qq!</thead>\n!.5107 qq!<tbody>\n!;51085109my@rev_color=qw(light dark);5110my$num_colors=scalar(@rev_color);5111my$current_color=0;51125113if($formateq'incremental') {5114my$color_class=$rev_color[$current_color];51155116#contents of a file5117my$linenr=0;5118 LINE:5119while(my$line= <$fd>) {5120chomp$line;5121$linenr++;51225123print qq!<tr id="l$linenr"class="$color_class">!.5124 qq!<td class="sha1"><a href=""> </a></td>!.5125 qq!<td class="linenr">!.5126 qq!<a class="linenr" href="">$linenr</a></td>!;5127print qq!<td class="pre">! . esc_html($line) ."</td>\n";5128print qq!</tr>\n!;5129}51305131}else{# porcelain, i.e. ordinary blame5132my%metainfo= ();# saves information about commits51335134# blame data5135 LINE:5136while(my$line= <$fd>) {5137chomp$line;5138# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5139# no <lines in group> for subsequent lines in group of lines5140my($full_rev,$orig_lineno,$lineno,$group_size) =5141($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5142if(!exists$metainfo{$full_rev}) {5143$metainfo{$full_rev} = {'nprevious'=>0};5144}5145my$meta=$metainfo{$full_rev};5146my$data;5147while($data= <$fd>) {5148chomp$data;5149last if($data=~s/^\t//);# contents of line5150if($data=~/^(\S+)(?: (.*))?$/) {5151$meta->{$1} =$2unlessexists$meta->{$1};5152}5153if($data=~/^previous /) {5154$meta->{'nprevious'}++;5155}5156}5157my$short_rev=substr($full_rev,0,8);5158my$author=$meta->{'author'};5159my%date=5160 parse_date($meta->{'author-time'},$meta->{'author-tz'});5161my$date=$date{'iso-tz'};5162if($group_size) {5163$current_color= ($current_color+1) %$num_colors;5164}5165my$tr_class=$rev_color[$current_color];5166$tr_class.=' boundary'if(exists$meta->{'boundary'});5167$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5168$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5169print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5170if($group_size) {5171print"<td class=\"sha1\"";5172print" title=\"". esc_html($author) .",$date\"";5173print" rowspan=\"$group_size\""if($group_size>1);5174print">";5175print$cgi->a({-href => href(action=>"commit",5176 hash=>$full_rev,5177 file_name=>$file_name)},5178 esc_html($short_rev));5179if($group_size>=2) {5180my@author_initials= ($author=~/\b([[:upper:]])\B/g);5181if(@author_initials) {5182print"<br />".5183 esc_html(join('',@author_initials));5184# or join('.', ...)5185}5186}5187print"</td>\n";5188}5189# 'previous' <sha1 of parent commit> <filename at commit>5190if(exists$meta->{'previous'} &&5191$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5192$meta->{'parent'} =$1;5193$meta->{'file_parent'} = unquote($2);5194}5195my$linenr_commit=5196exists($meta->{'parent'}) ?5197$meta->{'parent'} :$full_rev;5198my$linenr_filename=5199exists($meta->{'file_parent'}) ?5200$meta->{'file_parent'} : unquote($meta->{'filename'});5201my$blamed= href(action =>'blame',5202 file_name =>$linenr_filename,5203 hash_base =>$linenr_commit);5204print"<td class=\"linenr\">";5205print$cgi->a({ -href =>"$blamed#l$orig_lineno",5206-class=>"linenr"},5207 esc_html($lineno));5208print"</td>";5209print"<td class=\"pre\">". esc_html($data) ."</td>\n";5210print"</tr>\n";5211}# end while52125213}52145215# footer5216print"</tbody>\n".5217"</table>\n";# class="blame"5218print"</div>\n";# class="blame_body"5219close$fd5220or print"Reading blob failed\n";52215222 git_footer_html();5223}52245225sub git_blame {5226 git_blame_common();5227}52285229sub git_blame_incremental {5230 git_blame_common('incremental');5231}52325233sub git_blame_data {5234 git_blame_common('data');5235}52365237sub git_tags {5238my$head= git_get_head_hash($project);5239 git_header_html();5240 git_print_page_nav('','',$head,undef,$head);5241 git_print_header_div('summary',$project);52425243my@tagslist= git_get_tags_list();5244if(@tagslist) {5245 git_tags_body(\@tagslist);5246}5247 git_footer_html();5248}52495250sub git_heads {5251my$head= git_get_head_hash($project);5252 git_header_html();5253 git_print_page_nav('','',$head,undef,$head);5254 git_print_header_div('summary',$project);52555256my@headslist= git_get_heads_list();5257if(@headslist) {5258 git_heads_body(\@headslist,$head);5259}5260 git_footer_html();5261}52625263sub git_blob_plain {5264my$type=shift;5265my$expires;52665267if(!defined$hash) {5268if(defined$file_name) {5269my$base=$hash_base|| git_get_head_hash($project);5270$hash= git_get_hash_by_path($base,$file_name,"blob")5271or die_error(404,"Cannot find file");5272}else{5273 die_error(400,"No file name defined");5274}5275}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5276# blobs defined by non-textual hash id's can be cached5277$expires="+1d";5278}52795280open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5281or die_error(500,"Open git-cat-file blob '$hash' failed");52825283# content-type (can include charset)5284$type= blob_contenttype($fd,$file_name,$type);52855286# "save as" filename, even when no $file_name is given5287my$save_as="$hash";5288if(defined$file_name) {5289$save_as=$file_name;5290}elsif($type=~m/^text\//) {5291$save_as.='.txt';5292}52935294# With XSS prevention on, blobs of all types except a few known safe5295# ones are served with "Content-Disposition: attachment" to make sure5296# they don't run in our security domain. For certain image types,5297# blob view writes an <img> tag referring to blob_plain view, and we5298# want to be sure not to break that by serving the image as an5299# attachment (though Firefox 3 doesn't seem to care).5300my$sandbox=$prevent_xss&&5301$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;53025303print$cgi->header(5304-type =>$type,5305-expires =>$expires,5306-content_disposition =>5307($sandbox?'attachment':'inline')5308.'; filename="'.$save_as.'"');5309local$/=undef;5310binmode STDOUT,':raw';5311print<$fd>;5312binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5313close$fd;5314}53155316sub git_blob {5317my$expires;53185319if(!defined$hash) {5320if(defined$file_name) {5321my$base=$hash_base|| git_get_head_hash($project);5322$hash= git_get_hash_by_path($base,$file_name,"blob")5323or die_error(404,"Cannot find file");5324}else{5325 die_error(400,"No file name defined");5326}5327}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5328# blobs defined by non-textual hash id's can be cached5329$expires="+1d";5330}53315332my$have_blame= gitweb_check_feature('blame');5333open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5334or die_error(500,"Couldn't cat$file_name,$hash");5335my$mimetype= blob_mimetype($fd,$file_name);5336if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5337close$fd;5338return git_blob_plain($mimetype);5339}5340# we can have blame only for text/* mimetype5341$have_blame&&= ($mimetype=~m!^text/!);53425343 git_header_html(undef,$expires);5344my$formats_nav='';5345if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5346if(defined$file_name) {5347if($have_blame) {5348$formats_nav.=5349$cgi->a({-href => href(action=>"blame", -replay=>1)},5350"blame") .5351" | ";5352}5353$formats_nav.=5354$cgi->a({-href => href(action=>"history", -replay=>1)},5355"history") .5356" | ".5357$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5358"raw") .5359" | ".5360$cgi->a({-href => href(action=>"blob",5361 hash_base=>"HEAD", file_name=>$file_name)},5362"HEAD");5363}else{5364$formats_nav.=5365$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5366"raw");5367}5368 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5369 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5370}else{5371print"<div class=\"page_nav\">\n".5372"<br/><br/></div>\n".5373"<div class=\"title\">$hash</div>\n";5374}5375 git_print_page_path($file_name,"blob",$hash_base);5376print"<div class=\"page_body\">\n";5377if($mimetype=~m!^image/!) {5378print qq!<img type="$mimetype"!;5379if($file_name) {5380print qq! alt="$file_name" title="$file_name"!;5381}5382print qq! src="! .5383 href(action=>"blob_plain", hash=>$hash,5384 hash_base=>$hash_base, file_name=>$file_name) .5385 qq!"/>\n!;5386}else{5387my$nr;5388while(my$line= <$fd>) {5389chomp$line;5390$nr++;5391$line= untabify($line);5392printf"<div class=\"pre\"><a id=\"l%i\"href=\"". href(-replay =>1)5393."#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5394$nr,$nr,$nr, esc_html($line, -nbsp=>1);5395}5396}5397close$fd5398or print"Reading blob failed.\n";5399print"</div>";5400 git_footer_html();5401}54025403sub git_tree {5404if(!defined$hash_base) {5405$hash_base="HEAD";5406}5407if(!defined$hash) {5408if(defined$file_name) {5409$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5410}else{5411$hash=$hash_base;5412}5413}5414 die_error(404,"No such tree")unlessdefined($hash);54155416my$show_sizes= gitweb_check_feature('show-sizes');5417my$have_blame= gitweb_check_feature('blame');54185419my@entries= ();5420{5421local$/="\0";5422open my$fd,"-|", git_cmd(),"ls-tree",'-z',5423($show_sizes?'-l': ()),@extra_options,$hash5424or die_error(500,"Open git-ls-tree failed");5425@entries=map{chomp;$_} <$fd>;5426close$fd5427or die_error(404,"Reading tree failed");5428}54295430my$refs= git_get_references();5431my$ref= format_ref_marker($refs,$hash_base);5432 git_header_html();5433my$basedir='';5434if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5435my@views_nav= ();5436if(defined$file_name) {5437push@views_nav,5438$cgi->a({-href => href(action=>"history", -replay=>1)},5439"history"),5440$cgi->a({-href => href(action=>"tree",5441 hash_base=>"HEAD", file_name=>$file_name)},5442"HEAD"),5443}5444my$snapshot_links= format_snapshot_links($hash);5445if(defined$snapshot_links) {5446# FIXME: Should be available when we have no hash base as well.5447push@views_nav,$snapshot_links;5448}5449 git_print_page_nav('tree','',$hash_base,undef,undef,5450join(' | ',@views_nav));5451 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5452}else{5453undef$hash_base;5454print"<div class=\"page_nav\">\n";5455print"<br/><br/></div>\n";5456print"<div class=\"title\">$hash</div>\n";5457}5458if(defined$file_name) {5459$basedir=$file_name;5460if($basedirne''&&substr($basedir, -1)ne'/') {5461$basedir.='/';5462}5463 git_print_page_path($file_name,'tree',$hash_base);5464}5465print"<div class=\"page_body\">\n";5466print"<table class=\"tree\">\n";5467my$alternate=1;5468# '..' (top directory) link if possible5469if(defined$hash_base&&5470defined$file_name&&$file_name=~m![^/]+$!) {5471if($alternate) {5472print"<tr class=\"dark\">\n";5473}else{5474print"<tr class=\"light\">\n";5475}5476$alternate^=1;54775478my$up=$file_name;5479$up=~s!/?[^/]+$!!;5480undef$upunless$up;5481# based on git_print_tree_entry5482print'<td class="mode">'. mode_str('040000') ."</td>\n";5483print'<td class="size"> </td>'."\n"if$show_sizes;5484print'<td class="list">';5485print$cgi->a({-href => href(action=>"tree",5486 hash_base=>$hash_base,5487 file_name=>$up)},5488"..");5489print"</td>\n";5490print"<td class=\"link\"></td>\n";54915492print"</tr>\n";5493}5494foreachmy$line(@entries) {5495my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);54965497if($alternate) {5498print"<tr class=\"dark\">\n";5499}else{5500print"<tr class=\"light\">\n";5501}5502$alternate^=1;55035504 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);55055506print"</tr>\n";5507}5508print"</table>\n".5509"</div>";5510 git_footer_html();5511}55125513sub snapshot_name {5514my($project,$hash) =@_;55155516# path/to/project.git -> project5517# path/to/project/.git -> project5518my$name= to_utf8($project);5519$name=~ s,([^/])/*\.git$,$1,;5520$name= basename($name);5521# sanitize name5522$name=~s/[[:cntrl:]]/?/g;55235524my$ver=$hash;5525if($hash=~/^[0-9a-fA-F]+$/) {5526# shorten SHA-1 hash5527my$full_hash= git_get_full_hash($project,$hash);5528if($full_hash=~/^$hash/&&length($hash) >7) {5529$ver= git_get_short_hash($project,$hash);5530}5531}elsif($hash=~m!^refs/tags/(.*)$!) {5532# tags don't need shortened SHA-1 hash5533$ver=$1;5534}else{5535# branches and other need shortened SHA-1 hash5536if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5537$ver=$1;5538}5539$ver.='-'. git_get_short_hash($project,$hash);5540}5541# in case of hierarchical branch names5542$ver=~s!/!.!g;55435544# name = project-version_string5545$name="$name-$ver";55465547returnwantarray? ($name,$name) :$name;5548}55495550sub git_snapshot {5551my$format=$input_params{'snapshot_format'};5552if(!@snapshot_fmts) {5553 die_error(403,"Snapshots not allowed");5554}5555# default to first supported snapshot format5556$format||=$snapshot_fmts[0];5557if($format!~m/^[a-z0-9]+$/) {5558 die_error(400,"Invalid snapshot format parameter");5559}elsif(!exists($known_snapshot_formats{$format})) {5560 die_error(400,"Unknown snapshot format");5561}elsif($known_snapshot_formats{$format}{'disabled'}) {5562 die_error(403,"Snapshot format not allowed");5563}elsif(!grep($_eq$format,@snapshot_fmts)) {5564 die_error(403,"Unsupported snapshot format");5565}55665567my$type= git_get_type("$hash^{}");5568if(!$type) {5569 die_error(404,'Object does not exist');5570}elsif($typeeq'blob') {5571 die_error(400,'Object is not a tree-ish');5572}55735574my($name,$prefix) = snapshot_name($project,$hash);5575my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5576my$cmd= quote_command(5577 git_cmd(),'archive',5578"--format=$known_snapshot_formats{$format}{'format'}",5579"--prefix=$prefix/",$hash);5580if(exists$known_snapshot_formats{$format}{'compressor'}) {5581$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5582}55835584$filename=~s/(["\\])/\\$1/g;5585print$cgi->header(5586-type =>$known_snapshot_formats{$format}{'type'},5587-content_disposition =>'inline; filename="'.$filename.'"',5588-status =>'200 OK');55895590open my$fd,"-|",$cmd5591or die_error(500,"Execute git-archive failed");5592binmode STDOUT,':raw';5593print<$fd>;5594binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5595close$fd;5596}55975598sub git_log_generic {5599my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;56005601my$head= git_get_head_hash($project);5602if(!defined$base) {5603$base=$head;5604}5605if(!defined$page) {5606$page=0;5607}5608my$refs= git_get_references();56095610my$commit_hash=$base;5611if(defined$parent) {5612$commit_hash="$parent..$base";5613}5614my@commitlist=5615 parse_commits($commit_hash,101, (100*$page),5616defined$file_name? ($file_name,"--full-history") : ());56175618my$ftype;5619if(!defined$file_hash&&defined$file_name) {5620# some commits could have deleted file in question,5621# and not have it in tree, but one of them has to have it5622for(my$i=0;$i<@commitlist;$i++) {5623$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5624last ifdefined$file_hash;5625}5626}5627if(defined$file_hash) {5628$ftype= git_get_type($file_hash);5629}5630if(defined$file_name&& !defined$ftype) {5631 die_error(500,"Unknown type of object");5632}5633my%co;5634if(defined$file_name) {5635%co= parse_commit($base)5636or die_error(404,"Unknown commit object");5637}563856395640my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5641my$next_link='';5642if($#commitlist>=100) {5643$next_link=5644$cgi->a({-href => href(-replay=>1, page=>$page+1),5645-accesskey =>"n", -title =>"Alt-n"},"next");5646}5647my$patch_max= gitweb_get_feature('patches');5648if($patch_max&& !defined$file_name) {5649if($patch_max<0||@commitlist<=$patch_max) {5650$paging_nav.=" ⋅ ".5651$cgi->a({-href => href(action=>"patches", -replay=>1)},5652"patches");5653}5654}56555656 git_header_html();5657 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5658if(defined$file_name) {5659 git_print_header_div('commit', esc_html($co{'title'}),$base);5660}else{5661 git_print_header_div('summary',$project)5662}5663 git_print_page_path($file_name,$ftype,$hash_base)5664if(defined$file_name);56655666$body_subr->(\@commitlist,0,99,$refs,$next_link,5667$file_name,$file_hash,$ftype);56685669 git_footer_html();5670}56715672sub git_log {5673 git_log_generic('log', \&git_log_body,5674$hash,$hash_parent);5675}56765677sub git_commit {5678$hash||=$hash_base||"HEAD";5679my%co= parse_commit($hash)5680or die_error(404,"Unknown commit object");56815682my$parent=$co{'parent'};5683my$parents=$co{'parents'};# listref56845685# we need to prepare $formats_nav before any parameter munging5686my$formats_nav;5687if(!defined$parent) {5688# --root commitdiff5689$formats_nav.='(initial)';5690}elsif(@$parents==1) {5691# single parent commit5692$formats_nav.=5693'(parent: '.5694$cgi->a({-href => href(action=>"commit",5695 hash=>$parent)},5696 esc_html(substr($parent,0,7))) .5697')';5698}else{5699# merge commit5700$formats_nav.=5701'(merge: '.5702join(' ',map{5703$cgi->a({-href => href(action=>"commit",5704 hash=>$_)},5705 esc_html(substr($_,0,7)));5706}@$parents) .5707')';5708}5709if(gitweb_check_feature('patches') &&@$parents<=1) {5710$formats_nav.=" | ".5711$cgi->a({-href => href(action=>"patch", -replay=>1)},5712"patch");5713}57145715if(!defined$parent) {5716$parent="--root";5717}5718my@difftree;5719open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5720@diff_opts,5721(@$parents<=1?$parent:'-c'),5722$hash,"--"5723or die_error(500,"Open git-diff-tree failed");5724@difftree=map{chomp;$_} <$fd>;5725close$fdor die_error(404,"Reading git-diff-tree failed");57265727# non-textual hash id's can be cached5728my$expires;5729if($hash=~m/^[0-9a-fA-F]{40}$/) {5730$expires="+1d";5731}5732my$refs= git_get_references();5733my$ref= format_ref_marker($refs,$co{'id'});57345735 git_header_html(undef,$expires);5736 git_print_page_nav('commit','',5737$hash,$co{'tree'},$hash,5738$formats_nav);57395740if(defined$co{'parent'}) {5741 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5742}else{5743 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5744}5745print"<div class=\"title_text\">\n".5746"<table class=\"object_header\">\n";5747 git_print_authorship_rows(\%co);5748print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5749print"<tr>".5750"<td>tree</td>".5751"<td class=\"sha1\">".5752$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5753class=>"list"},$co{'tree'}) .5754"</td>".5755"<td class=\"link\">".5756$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5757"tree");5758my$snapshot_links= format_snapshot_links($hash);5759if(defined$snapshot_links) {5760print" | ".$snapshot_links;5761}5762print"</td>".5763"</tr>\n";57645765foreachmy$par(@$parents) {5766print"<tr>".5767"<td>parent</td>".5768"<td class=\"sha1\">".5769$cgi->a({-href => href(action=>"commit", hash=>$par),5770class=>"list"},$par) .5771"</td>".5772"<td class=\"link\">".5773$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5774" | ".5775$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5776"</td>".5777"</tr>\n";5778}5779print"</table>".5780"</div>\n";57815782print"<div class=\"page_body\">\n";5783 git_print_log($co{'comment'});5784print"</div>\n";57855786 git_difftree_body(\@difftree,$hash,@$parents);57875788 git_footer_html();5789}57905791sub git_object {5792# object is defined by:5793# - hash or hash_base alone5794# - hash_base and file_name5795my$type;57965797# - hash or hash_base alone5798if($hash|| ($hash_base&& !defined$file_name)) {5799my$object_id=$hash||$hash_base;58005801open my$fd,"-|", quote_command(5802 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5803or die_error(404,"Object does not exist");5804$type= <$fd>;5805chomp$type;5806close$fd5807or die_error(404,"Object does not exist");58085809# - hash_base and file_name5810}elsif($hash_base&&defined$file_name) {5811$file_name=~ s,/+$,,;58125813system(git_cmd(),"cat-file",'-e',$hash_base) ==05814or die_error(404,"Base object does not exist");58155816# here errors should not hapen5817open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5818or die_error(500,"Open git-ls-tree failed");5819my$line= <$fd>;5820close$fd;58215822#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5823unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5824 die_error(404,"File or directory for given base does not exist");5825}5826$type=$2;5827$hash=$3;5828}else{5829 die_error(400,"Not enough information to find object");5830}58315832print$cgi->redirect(-uri => href(action=>$type, -full=>1,5833 hash=>$hash, hash_base=>$hash_base,5834 file_name=>$file_name),5835-status =>'302 Found');5836}58375838sub git_blobdiff {5839my$format=shift||'html';58405841my$fd;5842my@difftree;5843my%diffinfo;5844my$expires;58455846# preparing $fd and %diffinfo for git_patchset_body5847# new style URI5848if(defined$hash_base&&defined$hash_parent_base) {5849if(defined$file_name) {5850# read raw output5851open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5852$hash_parent_base,$hash_base,5853"--", (defined$file_parent?$file_parent: ()),$file_name5854or die_error(500,"Open git-diff-tree failed");5855@difftree=map{chomp;$_} <$fd>;5856close$fd5857or die_error(404,"Reading git-diff-tree failed");5858@difftree5859or die_error(404,"Blob diff not found");58605861}elsif(defined$hash&&5862$hash=~/[0-9a-fA-F]{40}/) {5863# try to find filename from $hash58645865# read filtered raw output5866open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5867$hash_parent_base,$hash_base,"--"5868or die_error(500,"Open git-diff-tree failed");5869@difftree=5870# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5871# $hash == to_id5872grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5873map{chomp;$_} <$fd>;5874close$fd5875or die_error(404,"Reading git-diff-tree failed");5876@difftree5877or die_error(404,"Blob diff not found");58785879}else{5880 die_error(400,"Missing one of the blob diff parameters");5881}58825883if(@difftree>1) {5884 die_error(400,"Ambiguous blob diff specification");5885}58865887%diffinfo= parse_difftree_raw_line($difftree[0]);5888$file_parent||=$diffinfo{'from_file'} ||$file_name;5889$file_name||=$diffinfo{'to_file'};58905891$hash_parent||=$diffinfo{'from_id'};5892$hash||=$diffinfo{'to_id'};58935894# non-textual hash id's can be cached5895if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5896$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5897$expires='+1d';5898}58995900# open patch output5901open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5902'-p', ($formateq'html'?"--full-index": ()),5903$hash_parent_base,$hash_base,5904"--", (defined$file_parent?$file_parent: ()),$file_name5905or die_error(500,"Open git-diff-tree failed");5906}59075908# old/legacy style URI -- not generated anymore since 1.4.3.5909if(!%diffinfo) {5910 die_error('404 Not Found',"Missing one of the blob diff parameters")5911}59125913# header5914if($formateq'html') {5915my$formats_nav=5916$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5917"raw");5918 git_header_html(undef,$expires);5919if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5920 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5921 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5922}else{5923print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5924print"<div class=\"title\">$hashvs$hash_parent</div>\n";5925}5926if(defined$file_name) {5927 git_print_page_path($file_name,"blob",$hash_base);5928}else{5929print"<div class=\"page_path\"></div>\n";5930}59315932}elsif($formateq'plain') {5933print$cgi->header(5934-type =>'text/plain',5935-charset =>'utf-8',5936-expires =>$expires,5937-content_disposition =>'inline; filename="'."$file_name".'.patch"');59385939print"X-Git-Url: ".$cgi->self_url() ."\n\n";59405941}else{5942 die_error(400,"Unknown blobdiff format");5943}59445945# patch5946if($formateq'html') {5947print"<div class=\"page_body\">\n";59485949 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5950close$fd;59515952print"</div>\n";# class="page_body"5953 git_footer_html();59545955}else{5956while(my$line= <$fd>) {5957$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5958$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;59595960print$line;59615962last if$line=~m!^\+\+\+!;5963}5964local$/=undef;5965print<$fd>;5966close$fd;5967}5968}59695970sub git_blobdiff_plain {5971 git_blobdiff('plain');5972}59735974sub git_commitdiff {5975my%params=@_;5976my$format=$params{-format} ||'html';59775978my($patch_max) = gitweb_get_feature('patches');5979if($formateq'patch') {5980 die_error(403,"Patch view not allowed")unless$patch_max;5981}59825983$hash||=$hash_base||"HEAD";5984my%co= parse_commit($hash)5985or die_error(404,"Unknown commit object");59865987# choose format for commitdiff for merge5988if(!defined$hash_parent&& @{$co{'parents'}} >1) {5989$hash_parent='--cc';5990}5991# we need to prepare $formats_nav before almost any parameter munging5992my$formats_nav;5993if($formateq'html') {5994$formats_nav=5995$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5996"raw");5997if($patch_max&& @{$co{'parents'}} <=1) {5998$formats_nav.=" | ".5999$cgi->a({-href => href(action=>"patch", -replay=>1)},6000"patch");6001}60026003if(defined$hash_parent&&6004$hash_parentne'-c'&&$hash_parentne'--cc') {6005# commitdiff with two commits given6006my$hash_parent_short=$hash_parent;6007if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6008$hash_parent_short=substr($hash_parent,0,7);6009}6010$formats_nav.=6011' (from';6012for(my$i=0;$i< @{$co{'parents'}};$i++) {6013if($co{'parents'}[$i]eq$hash_parent) {6014$formats_nav.=' parent '. ($i+1);6015last;6016}6017}6018$formats_nav.=': '.6019$cgi->a({-href => href(action=>"commitdiff",6020 hash=>$hash_parent)},6021 esc_html($hash_parent_short)) .6022')';6023}elsif(!$co{'parent'}) {6024# --root commitdiff6025$formats_nav.=' (initial)';6026}elsif(scalar@{$co{'parents'}} ==1) {6027# single parent commit6028$formats_nav.=6029' (parent: '.6030$cgi->a({-href => href(action=>"commitdiff",6031 hash=>$co{'parent'})},6032 esc_html(substr($co{'parent'},0,7))) .6033')';6034}else{6035# merge commit6036if($hash_parenteq'--cc') {6037$formats_nav.=' | '.6038$cgi->a({-href => href(action=>"commitdiff",6039 hash=>$hash, hash_parent=>'-c')},6040'combined');6041}else{# $hash_parent eq '-c'6042$formats_nav.=' | '.6043$cgi->a({-href => href(action=>"commitdiff",6044 hash=>$hash, hash_parent=>'--cc')},6045'compact');6046}6047$formats_nav.=6048' (merge: '.6049join(' ',map{6050$cgi->a({-href => href(action=>"commitdiff",6051 hash=>$_)},6052 esc_html(substr($_,0,7)));6053} @{$co{'parents'}} ) .6054')';6055}6056}60576058my$hash_parent_param=$hash_parent;6059if(!defined$hash_parent_param) {6060# --cc for multiple parents, --root for parentless6061$hash_parent_param=6062@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6063}60646065# read commitdiff6066my$fd;6067my@difftree;6068if($formateq'html') {6069open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6070"--no-commit-id","--patch-with-raw","--full-index",6071$hash_parent_param,$hash,"--"6072or die_error(500,"Open git-diff-tree failed");60736074while(my$line= <$fd>) {6075chomp$line;6076# empty line ends raw part of diff-tree output6077last unless$line;6078push@difftree,scalar parse_difftree_raw_line($line);6079}60806081}elsif($formateq'plain') {6082open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6083'-p',$hash_parent_param,$hash,"--"6084or die_error(500,"Open git-diff-tree failed");6085}elsif($formateq'patch') {6086# For commit ranges, we limit the output to the number of6087# patches specified in the 'patches' feature.6088# For single commits, we limit the output to a single patch,6089# diverging from the git-format-patch default.6090my@commit_spec= ();6091if($hash_parent) {6092if($patch_max>0) {6093push@commit_spec,"-$patch_max";6094}6095push@commit_spec,'-n',"$hash_parent..$hash";6096}else{6097if($params{-single}) {6098push@commit_spec,'-1';6099}else{6100if($patch_max>0) {6101push@commit_spec,"-$patch_max";6102}6103push@commit_spec,"-n";6104}6105push@commit_spec,'--root',$hash;6106}6107open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',6108'--stdout',@commit_spec6109or die_error(500,"Open git-format-patch failed");6110}else{6111 die_error(400,"Unknown commitdiff format");6112}61136114# non-textual hash id's can be cached6115my$expires;6116if($hash=~m/^[0-9a-fA-F]{40}$/) {6117$expires="+1d";6118}61196120# write commit message6121if($formateq'html') {6122my$refs= git_get_references();6123my$ref= format_ref_marker($refs,$co{'id'});61246125 git_header_html(undef,$expires);6126 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6127 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6128print"<div class=\"title_text\">\n".6129"<table class=\"object_header\">\n";6130 git_print_authorship_rows(\%co);6131print"</table>".6132"</div>\n";6133print"<div class=\"page_body\">\n";6134if(@{$co{'comment'}} >1) {6135print"<div class=\"log\">\n";6136 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6137print"</div>\n";# class="log"6138}61396140}elsif($formateq'plain') {6141my$refs= git_get_references("tags");6142my$tagname= git_get_rev_name_tags($hash);6143my$filename= basename($project) ."-$hash.patch";61446145print$cgi->header(6146-type =>'text/plain',6147-charset =>'utf-8',6148-expires =>$expires,6149-content_disposition =>'inline; filename="'."$filename".'"');6150my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6151print"From: ". to_utf8($co{'author'}) ."\n";6152print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6153print"Subject: ". to_utf8($co{'title'}) ."\n";61546155print"X-Git-Tag:$tagname\n"if$tagname;6156print"X-Git-Url: ".$cgi->self_url() ."\n\n";61576158foreachmy$line(@{$co{'comment'}}) {6159print to_utf8($line) ."\n";6160}6161print"---\n\n";6162}elsif($formateq'patch') {6163my$filename= basename($project) ."-$hash.patch";61646165print$cgi->header(6166-type =>'text/plain',6167-charset =>'utf-8',6168-expires =>$expires,6169-content_disposition =>'inline; filename="'."$filename".'"');6170}61716172# write patch6173if($formateq'html') {6174my$use_parents= !defined$hash_parent||6175$hash_parenteq'-c'||$hash_parenteq'--cc';6176 git_difftree_body(\@difftree,$hash,6177$use_parents? @{$co{'parents'}} :$hash_parent);6178print"<br/>\n";61796180 git_patchset_body($fd, \@difftree,$hash,6181$use_parents? @{$co{'parents'}} :$hash_parent);6182close$fd;6183print"</div>\n";# class="page_body"6184 git_footer_html();61856186}elsif($formateq'plain') {6187local$/=undef;6188print<$fd>;6189close$fd6190or print"Reading git-diff-tree failed\n";6191}elsif($formateq'patch') {6192local$/=undef;6193print<$fd>;6194close$fd6195or print"Reading git-format-patch failed\n";6196}6197}61986199sub git_commitdiff_plain {6200 git_commitdiff(-format =>'plain');6201}62026203# format-patch-style patches6204sub git_patch {6205 git_commitdiff(-format =>'patch', -single =>1);6206}62076208sub git_patches {6209 git_commitdiff(-format =>'patch');6210}62116212sub git_history {6213 git_log_generic('history', \&git_history_body,6214$hash_base,$hash_parent_base,6215$file_name,$hash);6216}62176218sub git_search {6219 gitweb_check_feature('search')or die_error(403,"Search is disabled");6220if(!defined$searchtext) {6221 die_error(400,"Text field is empty");6222}6223if(!defined$hash) {6224$hash= git_get_head_hash($project);6225}6226my%co= parse_commit($hash);6227if(!%co) {6228 die_error(404,"Unknown commit object");6229}6230if(!defined$page) {6231$page=0;6232}62336234$searchtype||='commit';6235if($searchtypeeq'pickaxe') {6236# pickaxe may take all resources of your box and run for several minutes6237# with every query - so decide by yourself how public you make this feature6238 gitweb_check_feature('pickaxe')6239or die_error(403,"Pickaxe is disabled");6240}6241if($searchtypeeq'grep') {6242 gitweb_check_feature('grep')6243or die_error(403,"Grep is disabled");6244}62456246 git_header_html();62476248if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6249my$greptype;6250if($searchtypeeq'commit') {6251$greptype="--grep=";6252}elsif($searchtypeeq'author') {6253$greptype="--author=";6254}elsif($searchtypeeq'committer') {6255$greptype="--committer=";6256}6257$greptype.=$searchtext;6258my@commitlist= parse_commits($hash,101, (100*$page),undef,6259$greptype,'--regexp-ignore-case',6260$search_use_regexp?'--extended-regexp':'--fixed-strings');62616262my$paging_nav='';6263if($page>0) {6264$paging_nav.=6265$cgi->a({-href => href(action=>"search", hash=>$hash,6266 searchtext=>$searchtext,6267 searchtype=>$searchtype)},6268"first");6269$paging_nav.=" ⋅ ".6270$cgi->a({-href => href(-replay=>1, page=>$page-1),6271-accesskey =>"p", -title =>"Alt-p"},"prev");6272}else{6273$paging_nav.="first";6274$paging_nav.=" ⋅ prev";6275}6276my$next_link='';6277if($#commitlist>=100) {6278$next_link=6279$cgi->a({-href => href(-replay=>1, page=>$page+1),6280-accesskey =>"n", -title =>"Alt-n"},"next");6281$paging_nav.=" ⋅$next_link";6282}else{6283$paging_nav.=" ⋅ next";6284}62856286if($#commitlist>=100) {6287}62886289 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6290 git_print_header_div('commit', esc_html($co{'title'}),$hash);6291 git_search_grep_body(\@commitlist,0,99,$next_link);6292}62936294if($searchtypeeq'pickaxe') {6295 git_print_page_nav('','',$hash,$co{'tree'},$hash);6296 git_print_header_div('commit', esc_html($co{'title'}),$hash);62976298print"<table class=\"pickaxe search\">\n";6299my$alternate=1;6300local$/="\n";6301open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6302'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6303($search_use_regexp?'--pickaxe-regex': ());6304undef%co;6305my@files;6306while(my$line= <$fd>) {6307chomp$line;6308next unless$line;63096310my%set= parse_difftree_raw_line($line);6311if(defined$set{'commit'}) {6312# finish previous commit6313if(%co) {6314print"</td>\n".6315"<td class=\"link\">".6316$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6317" | ".6318$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6319print"</td>\n".6320"</tr>\n";6321}63226323if($alternate) {6324print"<tr class=\"dark\">\n";6325}else{6326print"<tr class=\"light\">\n";6327}6328$alternate^=1;6329%co= parse_commit($set{'commit'});6330my$author= chop_and_escape_str($co{'author_name'},15,5);6331print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6332"<td><i>$author</i></td>\n".6333"<td>".6334$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6335-class=>"list subject"},6336 chop_and_escape_str($co{'title'},50) ."<br/>");6337}elsif(defined$set{'to_id'}) {6338next if($set{'to_id'} =~m/^0{40}$/);63396340print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6341 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6342-class=>"list"},6343"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6344"<br/>\n";6345}6346}6347close$fd;63486349# finish last commit (warning: repetition!)6350if(%co) {6351print"</td>\n".6352"<td class=\"link\">".6353$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6354" | ".6355$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6356print"</td>\n".6357"</tr>\n";6358}63596360print"</table>\n";6361}63626363if($searchtypeeq'grep') {6364 git_print_page_nav('','',$hash,$co{'tree'},$hash);6365 git_print_header_div('commit', esc_html($co{'title'}),$hash);63666367print"<table class=\"grep_search\">\n";6368my$alternate=1;6369my$matches=0;6370local$/="\n";6371open my$fd,"-|", git_cmd(),'grep','-n',6372$search_use_regexp? ('-E','-i') :'-F',6373$searchtext,$co{'tree'};6374my$lastfile='';6375while(my$line= <$fd>) {6376chomp$line;6377my($file,$lno,$ltext,$binary);6378last if($matches++>1000);6379if($line=~/^Binary file (.+) matches$/) {6380$file=$1;6381$binary=1;6382}else{6383(undef,$file,$lno,$ltext) =split(/:/,$line,4);6384}6385if($filene$lastfile) {6386$lastfileand print"</td></tr>\n";6387if($alternate++) {6388print"<tr class=\"dark\">\n";6389}else{6390print"<tr class=\"light\">\n";6391}6392print"<td class=\"list\">".6393$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6394 file_name=>"$file"),6395-class=>"list"}, esc_path($file));6396print"</td><td>\n";6397$lastfile=$file;6398}6399if($binary) {6400print"<div class=\"binary\">Binary file</div>\n";6401}else{6402$ltext= untabify($ltext);6403if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6404$ltext= esc_html($1, -nbsp=>1);6405$ltext.='<span class="match">';6406$ltext.= esc_html($2, -nbsp=>1);6407$ltext.='</span>';6408$ltext.= esc_html($3, -nbsp=>1);6409}else{6410$ltext= esc_html($ltext, -nbsp=>1);6411}6412print"<div class=\"pre\">".6413$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6414 file_name=>"$file").'#l'.$lno,6415-class=>"linenr"},sprintf('%4i',$lno))6416.' '.$ltext."</div>\n";6417}6418}6419if($lastfile) {6420print"</td></tr>\n";6421if($matches>1000) {6422print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6423}6424}else{6425print"<div class=\"diff nodifferences\">No matches found</div>\n";6426}6427close$fd;64286429print"</table>\n";6430}6431 git_footer_html();6432}64336434sub git_search_help {6435 git_header_html();6436 git_print_page_nav('','',$hash,$hash,$hash);6437print<<EOT;6438<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6439regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6440the pattern entered is recognized as the POSIX extended6441<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6442insensitive).</p>6443<dl>6444<dt><b>commit</b></dt>6445<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6446EOT6447my$have_grep= gitweb_check_feature('grep');6448if($have_grep) {6449print<<EOT;6450<dt><b>grep</b></dt>6451<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6452 a different one) are searched for the given pattern. On large trees, this search can take6453a while and put some strain on the server, so please use it with some consideration. Note that6454due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6455case-sensitive.</dd>6456EOT6457}6458print<<EOT;6459<dt><b>author</b></dt>6460<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6461<dt><b>committer</b></dt>6462<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6463EOT6464my$have_pickaxe= gitweb_check_feature('pickaxe');6465if($have_pickaxe) {6466print<<EOT;6467<dt><b>pickaxe</b></dt>6468<dd>All commits that caused the string to appear or disappear from any file (changes that6469added, removed or "modified" the string) will be listed. This search can take a while and6470takes a lot of strain on the server, so please use it wisely. Note that since you may be6471interested even in changes just changing the case as well, this search is case sensitive.</dd>6472EOT6473}6474print"</dl>\n";6475 git_footer_html();6476}64776478sub git_shortlog {6479 git_log_generic('shortlog', \&git_shortlog_body,6480$hash,$hash_parent);6481}64826483## ......................................................................6484## feeds (RSS, Atom; OPML)64856486sub git_feed {6487my$format=shift||'atom';6488my$have_blame= gitweb_check_feature('blame');64896490# Atom: http://www.atomenabled.org/developers/syndication/6491# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6492if($formatne'rss'&&$formatne'atom') {6493 die_error(400,"Unknown web feed format");6494}64956496# log/feed of current (HEAD) branch, log of given branch, history of file/directory6497my$head=$hash||'HEAD';6498my@commitlist= parse_commits($head,150,0,$file_name);64996500my%latest_commit;6501my%latest_date;6502my$content_type="application/$format+xml";6503if(defined$cgi->http('HTTP_ACCEPT') &&6504$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6505# browser (feed reader) prefers text/xml6506$content_type='text/xml';6507}6508if(defined($commitlist[0])) {6509%latest_commit= %{$commitlist[0]};6510my$latest_epoch=$latest_commit{'committer_epoch'};6511%latest_date= parse_date($latest_epoch);6512my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6513if(defined$if_modified) {6514my$since;6515if(eval{require HTTP::Date;1; }) {6516$since= HTTP::Date::str2time($if_modified);6517}elsif(eval{require Time::ParseDate;1; }) {6518$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6519}6520if(defined$since&&$latest_epoch<=$since) {6521print$cgi->header(6522-type =>$content_type,6523-charset =>'utf-8',6524-last_modified =>$latest_date{'rfc2822'},6525-status =>'304 Not Modified');6526return;6527}6528}6529print$cgi->header(6530-type =>$content_type,6531-charset =>'utf-8',6532-last_modified =>$latest_date{'rfc2822'});6533}else{6534print$cgi->header(6535-type =>$content_type,6536-charset =>'utf-8');6537}65386539# Optimization: skip generating the body if client asks only6540# for Last-Modified date.6541return if($cgi->request_method()eq'HEAD');65426543# header variables6544my$title="$site_name-$project/$action";6545my$feed_type='log';6546if(defined$hash) {6547$title.=" - '$hash'";6548$feed_type='branch log';6549if(defined$file_name) {6550$title.=" ::$file_name";6551$feed_type='history';6552}6553}elsif(defined$file_name) {6554$title.=" -$file_name";6555$feed_type='history';6556}6557$title.="$feed_type";6558my$descr= git_get_project_description($project);6559if(defined$descr) {6560$descr= esc_html($descr);6561}else{6562$descr="$project".6563($formateq'rss'?'RSS':'Atom') .6564" feed";6565}6566my$owner= git_get_project_owner($project);6567$owner= esc_html($owner);65686569#header6570my$alt_url;6571if(defined$file_name) {6572$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6573}elsif(defined$hash) {6574$alt_url= href(-full=>1, action=>"log", hash=>$hash);6575}else{6576$alt_url= href(-full=>1, action=>"summary");6577}6578print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6579if($formateq'rss') {6580print<<XML;6581<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6582<channel>6583XML6584print"<title>$title</title>\n".6585"<link>$alt_url</link>\n".6586"<description>$descr</description>\n".6587"<language>en</language>\n".6588# project owner is responsible for 'editorial' content6589"<managingEditor>$owner</managingEditor>\n";6590if(defined$logo||defined$favicon) {6591# prefer the logo to the favicon, since RSS6592# doesn't allow both6593my$img= esc_url($logo||$favicon);6594print"<image>\n".6595"<url>$img</url>\n".6596"<title>$title</title>\n".6597"<link>$alt_url</link>\n".6598"</image>\n";6599}6600if(%latest_date) {6601print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6602print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6603}6604print"<generator>gitweb v.$version/$git_version</generator>\n";6605}elsif($formateq'atom') {6606print<<XML;6607<feed xmlns="http://www.w3.org/2005/Atom">6608XML6609print"<title>$title</title>\n".6610"<subtitle>$descr</subtitle>\n".6611'<link rel="alternate" type="text/html" href="'.6612$alt_url.'" />'."\n".6613'<link rel="self" type="'.$content_type.'" href="'.6614$cgi->self_url() .'" />'."\n".6615"<id>". href(-full=>1) ."</id>\n".6616# use project owner for feed author6617"<author><name>$owner</name></author>\n";6618if(defined$favicon) {6619print"<icon>". esc_url($favicon) ."</icon>\n";6620}6621if(defined$logo_url) {6622# not twice as wide as tall: 72 x 27 pixels6623print"<logo>". esc_url($logo) ."</logo>\n";6624}6625if(!%latest_date) {6626# dummy date to keep the feed valid until commits trickle in:6627print"<updated>1970-01-01T00:00:00Z</updated>\n";6628}else{6629print"<updated>$latest_date{'iso-8601'}</updated>\n";6630}6631print"<generator version='$version/$git_version'>gitweb</generator>\n";6632}66336634# contents6635for(my$i=0;$i<=$#commitlist;$i++) {6636my%co= %{$commitlist[$i]};6637my$commit=$co{'id'};6638# we read 150, we always show 30 and the ones more recent than 48 hours6639if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6640last;6641}6642my%cd= parse_date($co{'author_epoch'});66436644# get list of changed files6645open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6646$co{'parent'} ||"--root",6647$co{'id'},"--", (defined$file_name?$file_name: ())6648ornext;6649my@difftree=map{chomp;$_} <$fd>;6650close$fd6651ornext;66526653# print element (entry, item)6654my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6655if($formateq'rss') {6656print"<item>\n".6657"<title>". esc_html($co{'title'}) ."</title>\n".6658"<author>". esc_html($co{'author'}) ."</author>\n".6659"<pubDate>$cd{'rfc2822'}</pubDate>\n".6660"<guid isPermaLink=\"true\">$co_url</guid>\n".6661"<link>$co_url</link>\n".6662"<description>". esc_html($co{'title'}) ."</description>\n".6663"<content:encoded>".6664"<![CDATA[\n";6665}elsif($formateq'atom') {6666print"<entry>\n".6667"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6668"<updated>$cd{'iso-8601'}</updated>\n".6669"<author>\n".6670" <name>". esc_html($co{'author_name'}) ."</name>\n";6671if($co{'author_email'}) {6672print" <email>". esc_html($co{'author_email'}) ."</email>\n";6673}6674print"</author>\n".6675# use committer for contributor6676"<contributor>\n".6677" <name>". esc_html($co{'committer_name'}) ."</name>\n";6678if($co{'committer_email'}) {6679print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6680}6681print"</contributor>\n".6682"<published>$cd{'iso-8601'}</published>\n".6683"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6684"<id>$co_url</id>\n".6685"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6686"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6687}6688my$comment=$co{'comment'};6689print"<pre>\n";6690foreachmy$line(@$comment) {6691$line= esc_html($line);6692print"$line\n";6693}6694print"</pre><ul>\n";6695foreachmy$difftree_line(@difftree) {6696my%difftree= parse_difftree_raw_line($difftree_line);6697next if!$difftree{'from_id'};66986699my$file=$difftree{'file'} ||$difftree{'to_file'};67006701print"<li>".6702"[".6703$cgi->a({-href => href(-full=>1, action=>"blobdiff",6704 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6705 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6706 file_name=>$file, file_parent=>$difftree{'from_file'}),6707-title =>"diff"},'D');6708if($have_blame) {6709print$cgi->a({-href => href(-full=>1, action=>"blame",6710 file_name=>$file, hash_base=>$commit),6711-title =>"blame"},'B');6712}6713# if this is not a feed of a file history6714if(!defined$file_name||$file_namene$file) {6715print$cgi->a({-href => href(-full=>1, action=>"history",6716 file_name=>$file, hash=>$commit),6717-title =>"history"},'H');6718}6719$file= esc_path($file);6720print"] ".6721"$file</li>\n";6722}6723if($formateq'rss') {6724print"</ul>]]>\n".6725"</content:encoded>\n".6726"</item>\n";6727}elsif($formateq'atom') {6728print"</ul>\n</div>\n".6729"</content>\n".6730"</entry>\n";6731}6732}67336734# end of feed6735if($formateq'rss') {6736print"</channel>\n</rss>\n";6737}elsif($formateq'atom') {6738print"</feed>\n";6739}6740}67416742sub git_rss {6743 git_feed('rss');6744}67456746sub git_atom {6747 git_feed('atom');6748}67496750sub git_opml {6751my@list= git_get_projects_list();67526753print$cgi->header(6754-type =>'text/xml',6755-charset =>'utf-8',6756-content_disposition =>'inline; filename="opml.xml"');67576758print<<XML;6759<?xml version="1.0" encoding="utf-8"?>6760<opml version="1.0">6761<head>6762 <title>$site_nameOPML Export</title>6763</head>6764<body>6765<outline text="git RSS feeds">6766XML67676768foreachmy$pr(@list) {6769my%proj=%$pr;6770my$head= git_get_head_hash($proj{'path'});6771if(!defined$head) {6772next;6773}6774$git_dir="$projectroot/$proj{'path'}";6775my%co= parse_commit($head);6776if(!%co) {6777next;6778}67796780my$path= esc_html(chop_str($proj{'path'},25,5));6781my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6782my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6783print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6784}6785print<<XML;6786</outline>6787</body>6788</opml>6789XML6790}