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'}}); 457# project specific override is possible only if we have project 458our$git_dir;# global variable, declared later 459if(!$override|| !defined$git_dir) { 460return@defaults; 461} 462if(!defined$sub) { 463warn"feature$nameis not overridable"; 464return@defaults; 465} 466return$sub->(@defaults); 467} 468 469# A wrapper to check if a given feature is enabled. 470# With this, you can say 471# 472# my $bool_feat = gitweb_check_feature('bool_feat'); 473# gitweb_check_feature('bool_feat') or somecode; 474# 475# instead of 476# 477# my ($bool_feat) = gitweb_get_feature('bool_feat'); 478# (gitweb_get_feature('bool_feat'))[0] or somecode; 479# 480sub gitweb_check_feature { 481return(gitweb_get_feature(@_))[0]; 482} 483 484 485sub feature_bool { 486my$key=shift; 487my($val) = git_get_project_config($key,'--bool'); 488 489if(!defined$val) { 490return($_[0]); 491}elsif($valeq'true') { 492return(1); 493}elsif($valeq'false') { 494return(0); 495} 496} 497 498sub feature_snapshot { 499my(@fmts) =@_; 500 501my($val) = git_get_project_config('snapshot'); 502 503if($val) { 504@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 505} 506 507return@fmts; 508} 509 510sub feature_patches { 511my@val= (git_get_project_config('patches','--int')); 512 513if(@val) { 514return@val; 515} 516 517return($_[0]); 518} 519 520sub feature_avatar { 521my@val= (git_get_project_config('avatar')); 522 523return@val?@val:@_; 524} 525 526# checking HEAD file with -e is fragile if the repository was 527# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 528# and then pruned. 529sub check_head_link { 530my($dir) =@_; 531my$headfile="$dir/HEAD"; 532return((-e $headfile) || 533(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 534} 535 536sub check_export_ok { 537my($dir) =@_; 538return(check_head_link($dir) && 539(!$export_ok|| -e "$dir/$export_ok") && 540(!$export_auth_hook||$export_auth_hook->($dir))); 541} 542 543# process alternate names for backward compatibility 544# filter out unsupported (unknown) snapshot formats 545sub filter_snapshot_fmts { 546my@fmts=@_; 547 548@fmts=map{ 549exists$known_snapshot_format_aliases{$_} ? 550$known_snapshot_format_aliases{$_} :$_}@fmts; 551@fmts=grep{ 552exists$known_snapshot_formats{$_} && 553!$known_snapshot_formats{$_}{'disabled'}}@fmts; 554} 555 556our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 557our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 558# die if there are errors parsing config file 559if(-e $GITWEB_CONFIG) { 560do$GITWEB_CONFIG; 561die$@if$@; 562}elsif(-e $GITWEB_CONFIG_SYSTEM) { 563do$GITWEB_CONFIG_SYSTEM; 564die$@if$@; 565} 566 567# Get loadavg of system, to compare against $maxload. 568# Currently it requires '/proc/loadavg' present to get loadavg; 569# if it is not present it returns 0, which means no load checking. 570sub get_loadavg { 571if( -e '/proc/loadavg'){ 572open my$fd,'<','/proc/loadavg' 573orreturn0; 574my@load=split(/\s+/,scalar<$fd>); 575close$fd; 576 577# The first three columns measure CPU and IO utilization of the last one, 578# five, and 10 minute periods. The fourth column shows the number of 579# currently running processes and the total number of processes in the m/n 580# format. The last column displays the last process ID used. 581return$load[0] ||0; 582} 583# additional checks for load average should go here for things that don't export 584# /proc/loadavg 585 586return0; 587} 588 589# version of the core git binary 590our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 591$number_of_git_cmds++; 592 593$projects_list||=$projectroot; 594 595if(defined$maxload&& get_loadavg() >$maxload) { 596 die_error(503,"The load average on the server is too high"); 597} 598 599# ====================================================================== 600# input validation and dispatch 601 602# input parameters can be collected from a variety of sources (presently, CGI 603# and PATH_INFO), so we define an %input_params hash that collects them all 604# together during validation: this allows subsequent uses (e.g. href()) to be 605# agnostic of the parameter origin 606 607our%input_params= (); 608 609# input parameters are stored with the long parameter name as key. This will 610# also be used in the href subroutine to convert parameters to their CGI 611# equivalent, and since the href() usage is the most frequent one, we store 612# the name -> CGI key mapping here, instead of the reverse. 613# 614# XXX: Warning: If you touch this, check the search form for updating, 615# too. 616 617our@cgi_param_mapping= ( 618 project =>"p", 619 action =>"a", 620 file_name =>"f", 621 file_parent =>"fp", 622 hash =>"h", 623 hash_parent =>"hp", 624 hash_base =>"hb", 625 hash_parent_base =>"hpb", 626 page =>"pg", 627 order =>"o", 628 searchtext =>"s", 629 searchtype =>"st", 630 snapshot_format =>"sf", 631 extra_options =>"opt", 632 search_use_regexp =>"sr", 633# this must be last entry (for manipulation from JavaScript) 634 javascript =>"js" 635); 636our%cgi_param_mapping=@cgi_param_mapping; 637 638# we will also need to know the possible actions, for validation 639our%actions= ( 640"blame"=> \&git_blame, 641"blame_incremental"=> \&git_blame_incremental, 642"blame_data"=> \&git_blame_data, 643"blobdiff"=> \&git_blobdiff, 644"blobdiff_plain"=> \&git_blobdiff_plain, 645"blob"=> \&git_blob, 646"blob_plain"=> \&git_blob_plain, 647"commitdiff"=> \&git_commitdiff, 648"commitdiff_plain"=> \&git_commitdiff_plain, 649"commit"=> \&git_commit, 650"forks"=> \&git_forks, 651"heads"=> \&git_heads, 652"history"=> \&git_history, 653"log"=> \&git_log, 654"patch"=> \&git_patch, 655"patches"=> \&git_patches, 656"rss"=> \&git_rss, 657"atom"=> \&git_atom, 658"search"=> \&git_search, 659"search_help"=> \&git_search_help, 660"shortlog"=> \&git_shortlog, 661"summary"=> \&git_summary, 662"tag"=> \&git_tag, 663"tags"=> \&git_tags, 664"tree"=> \&git_tree, 665"snapshot"=> \&git_snapshot, 666"object"=> \&git_object, 667# those below don't need $project 668"opml"=> \&git_opml, 669"project_list"=> \&git_project_list, 670"project_index"=> \&git_project_index, 671); 672 673# finally, we have the hash of allowed extra_options for the commands that 674# allow them 675our%allowed_options= ( 676"--no-merges"=> [qw(rss atom log shortlog history)], 677); 678 679# fill %input_params with the CGI parameters. All values except for 'opt' 680# should be single values, but opt can be an array. We should probably 681# build an array of parameters that can be multi-valued, but since for the time 682# being it's only this one, we just single it out 683while(my($name,$symbol) =each%cgi_param_mapping) { 684if($symboleq'opt') { 685$input_params{$name} = [$cgi->param($symbol) ]; 686}else{ 687$input_params{$name} =$cgi->param($symbol); 688} 689} 690 691# now read PATH_INFO and update the parameter list for missing parameters 692sub evaluate_path_info { 693return ifdefined$input_params{'project'}; 694return if!$path_info; 695$path_info=~ s,^/+,,; 696return if!$path_info; 697 698# find which part of PATH_INFO is project 699my$project=$path_info; 700$project=~ s,/+$,,; 701while($project&& !check_head_link("$projectroot/$project")) { 702$project=~ s,/*[^/]*$,,; 703} 704return unless$project; 705$input_params{'project'} =$project; 706 707# do not change any parameters if an action is given using the query string 708return if$input_params{'action'}; 709$path_info=~ s,^\Q$project\E/*,,; 710 711# next, check if we have an action 712my$action=$path_info; 713$action=~ s,/.*$,,; 714if(exists$actions{$action}) { 715$path_info=~ s,^$action/*,,; 716$input_params{'action'} =$action; 717} 718 719# list of actions that want hash_base instead of hash, but can have no 720# pathname (f) parameter 721my@wants_base= ( 722'tree', 723'history', 724); 725 726# we want to catch 727# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 728my($parentrefname,$parentpathname,$refname,$pathname) = 729($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 730 731# first, analyze the 'current' part 732if(defined$pathname) { 733# we got "branch:filename" or "branch:dir/" 734# we could use git_get_type(branch:pathname), but: 735# - it needs $git_dir 736# - it does a git() call 737# - the convention of terminating directories with a slash 738# makes it superfluous 739# - embedding the action in the PATH_INFO would make it even 740# more superfluous 741$pathname=~ s,^/+,,; 742if(!$pathname||substr($pathname, -1)eq"/") { 743$input_params{'action'} ||="tree"; 744$pathname=~ s,/$,,; 745}else{ 746# the default action depends on whether we had parent info 747# or not 748if($parentrefname) { 749$input_params{'action'} ||="blobdiff_plain"; 750}else{ 751$input_params{'action'} ||="blob_plain"; 752} 753} 754$input_params{'hash_base'} ||=$refname; 755$input_params{'file_name'} ||=$pathname; 756}elsif(defined$refname) { 757# we got "branch". In this case we have to choose if we have to 758# set hash or hash_base. 759# 760# Most of the actions without a pathname only want hash to be 761# set, except for the ones specified in @wants_base that want 762# hash_base instead. It should also be noted that hand-crafted 763# links having 'history' as an action and no pathname or hash 764# set will fail, but that happens regardless of PATH_INFO. 765$input_params{'action'} ||="shortlog"; 766if(grep{$_eq$input_params{'action'} }@wants_base) { 767$input_params{'hash_base'} ||=$refname; 768}else{ 769$input_params{'hash'} ||=$refname; 770} 771} 772 773# next, handle the 'parent' part, if present 774if(defined$parentrefname) { 775# a missing pathspec defaults to the 'current' filename, allowing e.g. 776# someproject/blobdiff/oldrev..newrev:/filename 777if($parentpathname) { 778$parentpathname=~ s,^/+,,; 779$parentpathname=~ s,/$,,; 780$input_params{'file_parent'} ||=$parentpathname; 781}else{ 782$input_params{'file_parent'} ||=$input_params{'file_name'}; 783} 784# we assume that hash_parent_base is wanted if a path was specified, 785# or if the action wants hash_base instead of hash 786if(defined$input_params{'file_parent'} || 787grep{$_eq$input_params{'action'} }@wants_base) { 788$input_params{'hash_parent_base'} ||=$parentrefname; 789}else{ 790$input_params{'hash_parent'} ||=$parentrefname; 791} 792} 793 794# for the snapshot action, we allow URLs in the form 795# $project/snapshot/$hash.ext 796# where .ext determines the snapshot and gets removed from the 797# passed $refname to provide the $hash. 798# 799# To be able to tell that $refname includes the format extension, we 800# require the following two conditions to be satisfied: 801# - the hash input parameter MUST have been set from the $refname part 802# of the URL (i.e. they must be equal) 803# - the snapshot format MUST NOT have been defined already (e.g. from 804# CGI parameter sf) 805# It's also useless to try any matching unless $refname has a dot, 806# so we check for that too 807if(defined$input_params{'action'} && 808$input_params{'action'}eq'snapshot'&& 809defined$refname&&index($refname,'.') != -1&& 810$refnameeq$input_params{'hash'} && 811!defined$input_params{'snapshot_format'}) { 812# We loop over the known snapshot formats, checking for 813# extensions. Allowed extensions are both the defined suffix 814# (which includes the initial dot already) and the snapshot 815# format key itself, with a prepended dot 816while(my($fmt,$opt) =each%known_snapshot_formats) { 817my$hash=$refname; 818unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 819next; 820} 821my$sfx=$1; 822# a valid suffix was found, so set the snapshot format 823# and reset the hash parameter 824$input_params{'snapshot_format'} =$fmt; 825$input_params{'hash'} =$hash; 826# we also set the format suffix to the one requested 827# in the URL: this way a request for e.g. .tgz returns 828# a .tgz instead of a .tar.gz 829$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 830last; 831} 832} 833} 834evaluate_path_info(); 835 836our$action=$input_params{'action'}; 837if(defined$action) { 838if(!validate_action($action)) { 839 die_error(400,"Invalid action parameter"); 840} 841} 842 843# parameters which are pathnames 844our$project=$input_params{'project'}; 845if(defined$project) { 846if(!validate_project($project)) { 847undef$project; 848 die_error(404,"No such project"); 849} 850} 851 852our$file_name=$input_params{'file_name'}; 853if(defined$file_name) { 854if(!validate_pathname($file_name)) { 855 die_error(400,"Invalid file parameter"); 856} 857} 858 859our$file_parent=$input_params{'file_parent'}; 860if(defined$file_parent) { 861if(!validate_pathname($file_parent)) { 862 die_error(400,"Invalid file parent parameter"); 863} 864} 865 866# parameters which are refnames 867our$hash=$input_params{'hash'}; 868if(defined$hash) { 869if(!validate_refname($hash)) { 870 die_error(400,"Invalid hash parameter"); 871} 872} 873 874our$hash_parent=$input_params{'hash_parent'}; 875if(defined$hash_parent) { 876if(!validate_refname($hash_parent)) { 877 die_error(400,"Invalid hash parent parameter"); 878} 879} 880 881our$hash_base=$input_params{'hash_base'}; 882if(defined$hash_base) { 883if(!validate_refname($hash_base)) { 884 die_error(400,"Invalid hash base parameter"); 885} 886} 887 888our@extra_options= @{$input_params{'extra_options'}}; 889# @extra_options is always defined, since it can only be (currently) set from 890# CGI, and $cgi->param() returns the empty array in array context if the param 891# is not set 892foreachmy$opt(@extra_options) { 893if(not exists$allowed_options{$opt}) { 894 die_error(400,"Invalid option parameter"); 895} 896if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 897 die_error(400,"Invalid option parameter for this action"); 898} 899} 900 901our$hash_parent_base=$input_params{'hash_parent_base'}; 902if(defined$hash_parent_base) { 903if(!validate_refname($hash_parent_base)) { 904 die_error(400,"Invalid hash parent base parameter"); 905} 906} 907 908# other parameters 909our$page=$input_params{'page'}; 910if(defined$page) { 911if($page=~m/[^0-9]/) { 912 die_error(400,"Invalid page parameter"); 913} 914} 915 916our$searchtype=$input_params{'searchtype'}; 917if(defined$searchtype) { 918if($searchtype=~m/[^a-z]/) { 919 die_error(400,"Invalid searchtype parameter"); 920} 921} 922 923our$search_use_regexp=$input_params{'search_use_regexp'}; 924 925our$searchtext=$input_params{'searchtext'}; 926our$search_regexp; 927if(defined$searchtext) { 928if(length($searchtext) <2) { 929 die_error(403,"At least two characters are required for search parameter"); 930} 931$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 932} 933 934# path to the current git repository 935our$git_dir; 936$git_dir="$projectroot/$project"if$project; 937 938# list of supported snapshot formats 939our@snapshot_fmts= gitweb_get_feature('snapshot'); 940@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 941 942# check that the avatar feature is set to a known provider name, 943# and for each provider check if the dependencies are satisfied. 944# if the provider name is invalid or the dependencies are not met, 945# reset $git_avatar to the empty string. 946our($git_avatar) = gitweb_get_feature('avatar'); 947if($git_avatareq'gravatar') { 948$git_avatar=''unless(eval{require Digest::MD5;1; }); 949}elsif($git_avatareq'picon') { 950# no dependencies 951}else{ 952$git_avatar=''; 953} 954 955# dispatch 956if(!defined$action) { 957if(defined$hash) { 958$action= git_get_type($hash); 959}elsif(defined$hash_base&&defined$file_name) { 960$action= git_get_type("$hash_base:$file_name"); 961}elsif(defined$project) { 962$action='summary'; 963}else{ 964$action='project_list'; 965} 966} 967if(!defined($actions{$action})) { 968 die_error(400,"Unknown action"); 969} 970if($action!~m/^(?:opml|project_list|project_index)$/&& 971!$project) { 972 die_error(400,"Project needed"); 973} 974$actions{$action}->(); 975exit; 976 977## ====================================================================== 978## action links 979 980# possible values of extra options 981# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base) 982# -replay => 1 - start from a current view (replay with modifications) 983# -path_info => 0|1 - don't use/use path_info URL (if possible) 984sub href { 985my%params=@_; 986# default is to use -absolute url() i.e. $my_uri 987my$href=$params{-full} ?$my_url:$my_uri; 988 989$params{'project'} =$projectunlessexists$params{'project'}; 990 991if($params{-replay}) { 992while(my($name,$symbol) =each%cgi_param_mapping) { 993if(!exists$params{$name}) { 994$params{$name} =$input_params{$name}; 995} 996} 997} 998 999my$use_pathinfo= gitweb_check_feature('pathinfo');1000if(defined$params{'project'} &&1001(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1002# try to put as many parameters as possible in PATH_INFO:1003# - project name1004# - action1005# - hash_parent or hash_parent_base:/file_parent1006# - hash or hash_base:/filename1007# - the snapshot_format as an appropriate suffix10081009# When the script is the root DirectoryIndex for the domain,1010# $href here would be something like http://gitweb.example.com/1011# Thus, we strip any trailing / from $href, to spare us double1012# slashes in the final URL1013$href=~ s,/$,,;10141015# Then add the project name, if present1016$href.="/".esc_url($params{'project'});1017delete$params{'project'};10181019# since we destructively absorb parameters, we keep this1020# boolean that remembers if we're handling a snapshot1021my$is_snapshot=$params{'action'}eq'snapshot';10221023# Summary just uses the project path URL, any other action is1024# added to the URL1025if(defined$params{'action'}) {1026$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1027delete$params{'action'};1028}10291030# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1031# stripping nonexistent or useless pieces1032$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1033||$params{'hash_parent'} ||$params{'hash'});1034if(defined$params{'hash_base'}) {1035if(defined$params{'hash_parent_base'}) {1036$href.= esc_url($params{'hash_parent_base'});1037# skip the file_parent if it's the same as the file_name1038if(defined$params{'file_parent'}) {1039if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1040delete$params{'file_parent'};1041}elsif($params{'file_parent'} !~/\.\./) {1042$href.=":/".esc_url($params{'file_parent'});1043delete$params{'file_parent'};1044}1045}1046$href.="..";1047delete$params{'hash_parent'};1048delete$params{'hash_parent_base'};1049}elsif(defined$params{'hash_parent'}) {1050$href.= esc_url($params{'hash_parent'})."..";1051delete$params{'hash_parent'};1052}10531054$href.= esc_url($params{'hash_base'});1055if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1056$href.=":/".esc_url($params{'file_name'});1057delete$params{'file_name'};1058}1059delete$params{'hash'};1060delete$params{'hash_base'};1061}elsif(defined$params{'hash'}) {1062$href.= esc_url($params{'hash'});1063delete$params{'hash'};1064}10651066# If the action was a snapshot, we can absorb the1067# snapshot_format parameter too1068if($is_snapshot) {1069my$fmt=$params{'snapshot_format'};1070# snapshot_format should always be defined when href()1071# is called, but just in case some code forgets, we1072# fall back to the default1073$fmt||=$snapshot_fmts[0];1074$href.=$known_snapshot_formats{$fmt}{'suffix'};1075delete$params{'snapshot_format'};1076}1077}10781079# now encode the parameters explicitly1080my@result= ();1081for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1082my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1083if(defined$params{$name}) {1084if(ref($params{$name})eq"ARRAY") {1085foreachmy$par(@{$params{$name}}) {1086push@result,$symbol."=". esc_param($par);1087}1088}else{1089push@result,$symbol."=". esc_param($params{$name});1090}1091}1092}1093$href.="?".join(';',@result)ifscalar@result;10941095return$href;1096}109710981099## ======================================================================1100## validation, quoting/unquoting and escaping11011102sub validate_action {1103my$input=shift||returnundef;1104returnundefunlessexists$actions{$input};1105return$input;1106}11071108sub validate_project {1109my$input=shift||returnundef;1110if(!validate_pathname($input) ||1111!(-d "$projectroot/$input") ||1112!check_export_ok("$projectroot/$input") ||1113($strict_export&& !project_in_list($input))) {1114returnundef;1115}else{1116return$input;1117}1118}11191120sub validate_pathname {1121my$input=shift||returnundef;11221123# no '.' or '..' as elements of path, i.e. no '.' nor '..'1124# at the beginning, at the end, and between slashes.1125# also this catches doubled slashes1126if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1127returnundef;1128}1129# no null characters1130if($input=~m!\0!) {1131returnundef;1132}1133return$input;1134}11351136sub validate_refname {1137my$input=shift||returnundef;11381139# textual hashes are O.K.1140if($input=~m/^[0-9a-fA-F]{40}$/) {1141return$input;1142}1143# it must be correct pathname1144$input= validate_pathname($input)1145orreturnundef;1146# restrictions on ref name according to git-check-ref-format1147if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1148returnundef;1149}1150return$input;1151}11521153# decode sequences of octets in utf8 into Perl's internal form,1154# which is utf-8 with utf8 flag set if needed. gitweb writes out1155# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1156sub to_utf8 {1157my$str=shift;1158returnundefunlessdefined$str;1159if(utf8::valid($str)) {1160 utf8::decode($str);1161return$str;1162}else{1163return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1164}1165}11661167# quote unsafe chars, but keep the slash, even when it's not1168# correct, but quoted slashes look too horrible in bookmarks1169sub esc_param {1170my$str=shift;1171returnundefunlessdefined$str;1172$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1173$str=~s/ /\+/g;1174return$str;1175}11761177# quote unsafe chars in whole URL, so some charactrs cannot be quoted1178sub esc_url {1179my$str=shift;1180returnundefunlessdefined$str;1181$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1182$str=~s/\+/%2B/g;1183$str=~s/ /\+/g;1184return$str;1185}11861187# replace invalid utf8 character with SUBSTITUTION sequence1188sub esc_html {1189my$str=shift;1190my%opts=@_;11911192returnundefunlessdefined$str;11931194$str= to_utf8($str);1195$str=$cgi->escapeHTML($str);1196if($opts{'-nbsp'}) {1197$str=~s/ / /g;1198}1199$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1200return$str;1201}12021203# quote control characters and escape filename to HTML1204sub esc_path {1205my$str=shift;1206my%opts=@_;12071208returnundefunlessdefined$str;12091210$str= to_utf8($str);1211$str=$cgi->escapeHTML($str);1212if($opts{'-nbsp'}) {1213$str=~s/ / /g;1214}1215$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1216return$str;1217}12181219# Make control characters "printable", using character escape codes (CEC)1220sub quot_cec {1221my$cntrl=shift;1222my%opts=@_;1223my%es= (# character escape codes, aka escape sequences1224"\t"=>'\t',# tab (HT)1225"\n"=>'\n',# line feed (LF)1226"\r"=>'\r',# carrige return (CR)1227"\f"=>'\f',# form feed (FF)1228"\b"=>'\b',# backspace (BS)1229"\a"=>'\a',# alarm (bell) (BEL)1230"\e"=>'\e',# escape (ESC)1231"\013"=>'\v',# vertical tab (VT)1232"\000"=>'\0',# nul character (NUL)1233);1234my$chr= ( (exists$es{$cntrl})1235?$es{$cntrl}1236:sprintf('\%2x',ord($cntrl)) );1237if($opts{-nohtml}) {1238return$chr;1239}else{1240return"<span class=\"cntrl\">$chr</span>";1241}1242}12431244# Alternatively use unicode control pictures codepoints,1245# Unicode "printable representation" (PR)1246sub quot_upr {1247my$cntrl=shift;1248my%opts=@_;12491250my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1251if($opts{-nohtml}) {1252return$chr;1253}else{1254return"<span class=\"cntrl\">$chr</span>";1255}1256}12571258# git may return quoted and escaped filenames1259sub unquote {1260my$str=shift;12611262sub unq {1263my$seq=shift;1264my%es= (# character escape codes, aka escape sequences1265't'=>"\t",# tab (HT, TAB)1266'n'=>"\n",# newline (NL)1267'r'=>"\r",# return (CR)1268'f'=>"\f",# form feed (FF)1269'b'=>"\b",# backspace (BS)1270'a'=>"\a",# alarm (bell) (BEL)1271'e'=>"\e",# escape (ESC)1272'v'=>"\013",# vertical tab (VT)1273);12741275if($seq=~m/^[0-7]{1,3}$/) {1276# octal char sequence1277returnchr(oct($seq));1278}elsif(exists$es{$seq}) {1279# C escape sequence, aka character escape code1280return$es{$seq};1281}1282# quoted ordinary character1283return$seq;1284}12851286if($str=~m/^"(.*)"$/) {1287# needs unquoting1288$str=$1;1289$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1290}1291return$str;1292}12931294# escape tabs (convert tabs to spaces)1295sub untabify {1296my$line=shift;12971298while((my$pos=index($line,"\t")) != -1) {1299if(my$count= (8- ($pos%8))) {1300my$spaces=' ' x $count;1301$line=~s/\t/$spaces/;1302}1303}13041305return$line;1306}13071308sub project_in_list {1309my$project=shift;1310my@list= git_get_projects_list();1311return@list&&scalar(grep{$_->{'path'}eq$project}@list);1312}13131314## ----------------------------------------------------------------------1315## HTML aware string manipulation13161317# Try to chop given string on a word boundary between position1318# $len and $len+$add_len. If there is no word boundary there,1319# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1320# (marking chopped part) would be longer than given string.1321sub chop_str {1322my$str=shift;1323my$len=shift;1324my$add_len=shift||10;1325my$where=shift||'right';# 'left' | 'center' | 'right'13261327# Make sure perl knows it is utf8 encoded so we don't1328# cut in the middle of a utf8 multibyte char.1329$str= to_utf8($str);13301331# allow only $len chars, but don't cut a word if it would fit in $add_len1332# if it doesn't fit, cut it if it's still longer than the dots we would add1333# remove chopped character entities entirely13341335# when chopping in the middle, distribute $len into left and right part1336# return early if chopping wouldn't make string shorter1337if($whereeq'center') {1338return$strif($len+5>=length($str));# filler is length 51339$len=int($len/2);1340}else{1341return$strif($len+4>=length($str));# filler is length 41342}13431344# regexps: ending and beginning with word part up to $add_len1345my$endre=qr/.{$len}\w{0,$add_len}/;1346my$begre=qr/\w{0,$add_len}.{$len}/;13471348if($whereeq'left') {1349$str=~m/^(.*?)($begre)$/;1350my($lead,$body) = ($1,$2);1351if(length($lead) >4) {1352$lead=" ...";1353}1354return"$lead$body";13551356}elsif($whereeq'center') {1357$str=~m/^($endre)(.*)$/;1358my($left,$str) = ($1,$2);1359$str=~m/^(.*?)($begre)$/;1360my($mid,$right) = ($1,$2);1361if(length($mid) >5) {1362$mid=" ... ";1363}1364return"$left$mid$right";13651366}else{1367$str=~m/^($endre)(.*)$/;1368my$body=$1;1369my$tail=$2;1370if(length($tail) >4) {1371$tail="... ";1372}1373return"$body$tail";1374}1375}13761377# takes the same arguments as chop_str, but also wraps a <span> around the1378# result with a title attribute if it does get chopped. Additionally, the1379# string is HTML-escaped.1380sub chop_and_escape_str {1381my($str) =@_;13821383my$chopped= chop_str(@_);1384if($choppedeq$str) {1385return esc_html($chopped);1386}else{1387$str=~s/[[:cntrl:]]/?/g;1388return$cgi->span({-title=>$str}, esc_html($chopped));1389}1390}13911392## ----------------------------------------------------------------------1393## functions returning short strings13941395# CSS class for given age value (in seconds)1396sub age_class {1397my$age=shift;13981399if(!defined$age) {1400return"noage";1401}elsif($age<60*60*2) {1402return"age0";1403}elsif($age<60*60*24*2) {1404return"age1";1405}else{1406return"age2";1407}1408}14091410# convert age in seconds to "nn units ago" string1411sub age_string {1412my$age=shift;1413my$age_str;14141415if($age>60*60*24*365*2) {1416$age_str= (int$age/60/60/24/365);1417$age_str.=" years ago";1418}elsif($age>60*60*24*(365/12)*2) {1419$age_str=int$age/60/60/24/(365/12);1420$age_str.=" months ago";1421}elsif($age>60*60*24*7*2) {1422$age_str=int$age/60/60/24/7;1423$age_str.=" weeks ago";1424}elsif($age>60*60*24*2) {1425$age_str=int$age/60/60/24;1426$age_str.=" days ago";1427}elsif($age>60*60*2) {1428$age_str=int$age/60/60;1429$age_str.=" hours ago";1430}elsif($age>60*2) {1431$age_str=int$age/60;1432$age_str.=" min ago";1433}elsif($age>2) {1434$age_str=int$age;1435$age_str.=" sec ago";1436}else{1437$age_str.=" right now";1438}1439return$age_str;1440}14411442useconstant{1443 S_IFINVALID =>0030000,1444 S_IFGITLINK =>0160000,1445};14461447# submodule/subproject, a commit object reference1448sub S_ISGITLINK {1449my$mode=shift;14501451return(($mode& S_IFMT) == S_IFGITLINK)1452}14531454# convert file mode in octal to symbolic file mode string1455sub mode_str {1456my$mode=oct shift;14571458if(S_ISGITLINK($mode)) {1459return'm---------';1460}elsif(S_ISDIR($mode& S_IFMT)) {1461return'drwxr-xr-x';1462}elsif(S_ISLNK($mode)) {1463return'lrwxrwxrwx';1464}elsif(S_ISREG($mode)) {1465# git cares only about the executable bit1466if($mode& S_IXUSR) {1467return'-rwxr-xr-x';1468}else{1469return'-rw-r--r--';1470};1471}else{1472return'----------';1473}1474}14751476# convert file mode in octal to file type string1477sub file_type {1478my$mode=shift;14791480if($mode!~m/^[0-7]+$/) {1481return$mode;1482}else{1483$mode=oct$mode;1484}14851486if(S_ISGITLINK($mode)) {1487return"submodule";1488}elsif(S_ISDIR($mode& S_IFMT)) {1489return"directory";1490}elsif(S_ISLNK($mode)) {1491return"symlink";1492}elsif(S_ISREG($mode)) {1493return"file";1494}else{1495return"unknown";1496}1497}14981499# convert file mode in octal to file type description string1500sub file_type_long {1501my$mode=shift;15021503if($mode!~m/^[0-7]+$/) {1504return$mode;1505}else{1506$mode=oct$mode;1507}15081509if(S_ISGITLINK($mode)) {1510return"submodule";1511}elsif(S_ISDIR($mode& S_IFMT)) {1512return"directory";1513}elsif(S_ISLNK($mode)) {1514return"symlink";1515}elsif(S_ISREG($mode)) {1516if($mode& S_IXUSR) {1517return"executable";1518}else{1519return"file";1520};1521}else{1522return"unknown";1523}1524}152515261527## ----------------------------------------------------------------------1528## functions returning short HTML fragments, or transforming HTML fragments1529## which don't belong to other sections15301531# format line of commit message.1532sub format_log_line_html {1533my$line=shift;15341535$line= esc_html($line, -nbsp=>1);1536$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1537$cgi->a({-href => href(action=>"object", hash=>$1),1538-class=>"text"},$1);1539}eg;15401541return$line;1542}15431544# format marker of refs pointing to given object15451546# the destination action is chosen based on object type and current context:1547# - for annotated tags, we choose the tag view unless it's the current view1548# already, in which case we go to shortlog view1549# - for other refs, we keep the current view if we're in history, shortlog or1550# log view, and select shortlog otherwise1551sub format_ref_marker {1552my($refs,$id) =@_;1553my$markers='';15541555if(defined$refs->{$id}) {1556foreachmy$ref(@{$refs->{$id}}) {1557# this code exploits the fact that non-lightweight tags are the1558# only indirect objects, and that they are the only objects for which1559# we want to use tag instead of shortlog as action1560my($type,$name) =qw();1561my$indirect= ($ref=~s/\^\{\}$//);1562# e.g. tags/v2.6.11 or heads/next1563if($ref=~m!^(.*?)s?/(.*)$!) {1564$type=$1;1565$name=$2;1566}else{1567$type="ref";1568$name=$ref;1569}15701571my$class=$type;1572$class.=" indirect"if$indirect;15731574my$dest_action="shortlog";15751576if($indirect) {1577$dest_action="tag"unless$actioneq"tag";1578}elsif($action=~/^(history|(short)?log)$/) {1579$dest_action=$action;1580}15811582my$dest="";1583$dest.="refs/"unless$ref=~ m!^refs/!;1584$dest.=$ref;15851586my$link=$cgi->a({1587-href => href(1588 action=>$dest_action,1589 hash=>$dest1590)},$name);15911592$markers.=" <span class=\"$class\"title=\"$ref\">".1593$link."</span>";1594}1595}15961597if($markers) {1598return' <span class="refs">'.$markers.'</span>';1599}else{1600return"";1601}1602}16031604# format, perhaps shortened and with markers, title line1605sub format_subject_html {1606my($long,$short,$href,$extra) =@_;1607$extra=''unlessdefined($extra);16081609if(length($short) <length($long)) {1610$long=~s/[[:cntrl:]]/?/g;1611return$cgi->a({-href =>$href, -class=>"list subject",1612-title => to_utf8($long)},1613 esc_html($short)) .$extra;1614}else{1615return$cgi->a({-href =>$href, -class=>"list subject"},1616 esc_html($long)) .$extra;1617}1618}16191620# Rather than recomputing the url for an email multiple times, we cache it1621# after the first hit. This gives a visible benefit in views where the avatar1622# for the same email is used repeatedly (e.g. shortlog).1623# The cache is shared by all avatar engines (currently gravatar only), which1624# are free to use it as preferred. Since only one avatar engine is used for any1625# given page, there's no risk for cache conflicts.1626our%avatar_cache= ();16271628# Compute the picon url for a given email, by using the picon search service over at1629# http://www.cs.indiana.edu/picons/search.html1630sub picon_url {1631my$email=lc shift;1632if(!$avatar_cache{$email}) {1633my($user,$domain) =split('@',$email);1634$avatar_cache{$email} =1635"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1636"$domain/$user/".1637"users+domains+unknown/up/single";1638}1639return$avatar_cache{$email};1640}16411642# Compute the gravatar url for a given email, if it's not in the cache already.1643# Gravatar stores only the part of the URL before the size, since that's the1644# one computationally more expensive. This also allows reuse of the cache for1645# different sizes (for this particular engine).1646sub gravatar_url {1647my$email=lc shift;1648my$size=shift;1649$avatar_cache{$email} ||=1650"http://www.gravatar.com/avatar/".1651 Digest::MD5::md5_hex($email) ."?s=";1652return$avatar_cache{$email} .$size;1653}16541655# Insert an avatar for the given $email at the given $size if the feature1656# is enabled.1657sub git_get_avatar {1658my($email,%opts) =@_;1659my$pre_white= ($opts{-pad_before} ?" ":"");1660my$post_white= ($opts{-pad_after} ?" ":"");1661$opts{-size} ||='default';1662my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1663my$url="";1664if($git_avatareq'gravatar') {1665$url= gravatar_url($email,$size);1666}elsif($git_avatareq'picon') {1667$url= picon_url($email);1668}1669# Other providers can be added by extending the if chain, defining $url1670# as needed. If no variant puts something in $url, we assume avatars1671# are completely disabled/unavailable.1672if($url) {1673return$pre_white.1674"<img width=\"$size\"".1675"class=\"avatar\"".1676"src=\"$url\"".1677"alt=\"\"".1678"/>".$post_white;1679}else{1680return"";1681}1682}16831684sub format_search_author {1685my($author,$searchtype,$displaytext) =@_;1686my$have_search= gitweb_check_feature('search');16871688if($have_search) {1689my$performed="";1690if($searchtypeeq'author') {1691$performed="authored";1692}elsif($searchtypeeq'committer') {1693$performed="committed";1694}16951696return$cgi->a({-href => href(action=>"search", hash=>$hash,1697 searchtext=>$author,1698 searchtype=>$searchtype),class=>"list",1699 title=>"Search for commits$performedby$author"},1700$displaytext);17011702}else{1703return$displaytext;1704}1705}17061707# format the author name of the given commit with the given tag1708# the author name is chopped and escaped according to the other1709# optional parameters (see chop_str).1710sub format_author_html {1711my$tag=shift;1712my$co=shift;1713my$author= chop_and_escape_str($co->{'author_name'},@_);1714return"<$tagclass=\"author\">".1715 format_search_author($co->{'author_name'},"author",1716 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1717$author) .1718"</$tag>";1719}17201721# format git diff header line, i.e. "diff --(git|combined|cc) ..."1722sub format_git_diff_header_line {1723my$line=shift;1724my$diffinfo=shift;1725my($from,$to) =@_;17261727if($diffinfo->{'nparents'}) {1728# combined diff1729$line=~s!^(diff (.*?) )"?.*$!$1!;1730if($to->{'href'}) {1731$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1732 esc_path($to->{'file'}));1733}else{# file was deleted (no href)1734$line.= esc_path($to->{'file'});1735}1736}else{1737# "ordinary" diff1738$line=~s!^(diff (.*?) )"?a/.*$!$1!;1739if($from->{'href'}) {1740$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1741'a/'. esc_path($from->{'file'}));1742}else{# file was added (no href)1743$line.='a/'. esc_path($from->{'file'});1744}1745$line.=' ';1746if($to->{'href'}) {1747$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1748'b/'. esc_path($to->{'file'}));1749}else{# file was deleted1750$line.='b/'. esc_path($to->{'file'});1751}1752}17531754return"<div class=\"diff header\">$line</div>\n";1755}17561757# format extended diff header line, before patch itself1758sub format_extended_diff_header_line {1759my$line=shift;1760my$diffinfo=shift;1761my($from,$to) =@_;17621763# match <path>1764if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1765$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1766 esc_path($from->{'file'}));1767}1768if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1769$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1770 esc_path($to->{'file'}));1771}1772# match single <mode>1773if($line=~m/\s(\d{6})$/) {1774$line.='<span class="info"> ('.1775 file_type_long($1) .1776')</span>';1777}1778# match <hash>1779if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1780# can match only for combined diff1781$line='index ';1782for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1783if($from->{'href'}[$i]) {1784$line.=$cgi->a({-href=>$from->{'href'}[$i],1785-class=>"hash"},1786substr($diffinfo->{'from_id'}[$i],0,7));1787}else{1788$line.='0' x 7;1789}1790# separator1791$line.=','if($i<$diffinfo->{'nparents'} -1);1792}1793$line.='..';1794if($to->{'href'}) {1795$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1796substr($diffinfo->{'to_id'},0,7));1797}else{1798$line.='0' x 7;1799}18001801}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1802# can match only for ordinary diff1803my($from_link,$to_link);1804if($from->{'href'}) {1805$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1806substr($diffinfo->{'from_id'},0,7));1807}else{1808$from_link='0' x 7;1809}1810if($to->{'href'}) {1811$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1812substr($diffinfo->{'to_id'},0,7));1813}else{1814$to_link='0' x 7;1815}1816my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1817$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1818}18191820return$line."<br/>\n";1821}18221823# format from-file/to-file diff header1824sub format_diff_from_to_header {1825my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1826my$line;1827my$result='';18281829$line=$from_line;1830#assert($line =~ m/^---/) if DEBUG;1831# no extra formatting for "^--- /dev/null"1832if(!$diffinfo->{'nparents'}) {1833# ordinary (single parent) diff1834if($line=~m!^--- "?a/!) {1835if($from->{'href'}) {1836$line='--- a/'.1837$cgi->a({-href=>$from->{'href'}, -class=>"path"},1838 esc_path($from->{'file'}));1839}else{1840$line='--- a/'.1841 esc_path($from->{'file'});1842}1843}1844$result.= qq!<div class="diff from_file">$line</div>\n!;18451846}else{1847# combined diff (merge commit)1848for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1849if($from->{'href'}[$i]) {1850$line='--- '.1851$cgi->a({-href=>href(action=>"blobdiff",1852 hash_parent=>$diffinfo->{'from_id'}[$i],1853 hash_parent_base=>$parents[$i],1854 file_parent=>$from->{'file'}[$i],1855 hash=>$diffinfo->{'to_id'},1856 hash_base=>$hash,1857 file_name=>$to->{'file'}),1858-class=>"path",1859-title=>"diff". ($i+1)},1860$i+1) .1861'/'.1862$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1863 esc_path($from->{'file'}[$i]));1864}else{1865$line='--- /dev/null';1866}1867$result.= qq!<div class="diff from_file">$line</div>\n!;1868}1869}18701871$line=$to_line;1872#assert($line =~ m/^\+\+\+/) if DEBUG;1873# no extra formatting for "^+++ /dev/null"1874if($line=~m!^\+\+\+ "?b/!) {1875if($to->{'href'}) {1876$line='+++ b/'.1877$cgi->a({-href=>$to->{'href'}, -class=>"path"},1878 esc_path($to->{'file'}));1879}else{1880$line='+++ b/'.1881 esc_path($to->{'file'});1882}1883}1884$result.= qq!<div class="diff to_file">$line</div>\n!;18851886return$result;1887}18881889# create note for patch simplified by combined diff1890sub format_diff_cc_simplified {1891my($diffinfo,@parents) =@_;1892my$result='';18931894$result.="<div class=\"diff header\">".1895"diff --cc ";1896if(!is_deleted($diffinfo)) {1897$result.=$cgi->a({-href => href(action=>"blob",1898 hash_base=>$hash,1899 hash=>$diffinfo->{'to_id'},1900 file_name=>$diffinfo->{'to_file'}),1901-class=>"path"},1902 esc_path($diffinfo->{'to_file'}));1903}else{1904$result.= esc_path($diffinfo->{'to_file'});1905}1906$result.="</div>\n".# class="diff header"1907"<div class=\"diff nodifferences\">".1908"Simple merge".1909"</div>\n";# class="diff nodifferences"19101911return$result;1912}19131914# format patch (diff) line (not to be used for diff headers)1915sub format_diff_line {1916my$line=shift;1917my($from,$to) =@_;1918my$diff_class="";19191920chomp$line;19211922if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1923# combined diff1924my$prefix=substr($line,0,scalar@{$from->{'href'}});1925if($line=~m/^\@{3}/) {1926$diff_class=" chunk_header";1927}elsif($line=~m/^\\/) {1928$diff_class=" incomplete";1929}elsif($prefix=~tr/+/+/) {1930$diff_class=" add";1931}elsif($prefix=~tr/-/-/) {1932$diff_class=" rem";1933}1934}else{1935# assume ordinary diff1936my$char=substr($line,0,1);1937if($chareq'+') {1938$diff_class=" add";1939}elsif($chareq'-') {1940$diff_class=" rem";1941}elsif($chareq'@') {1942$diff_class=" chunk_header";1943}elsif($chareq"\\") {1944$diff_class=" incomplete";1945}1946}1947$line= untabify($line);1948if($from&&$to&&$line=~m/^\@{2} /) {1949my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1950$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;19511952$from_lines=0unlessdefined$from_lines;1953$to_lines=0unlessdefined$to_lines;19541955if($from->{'href'}) {1956$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1957-class=>"list"},$from_text);1958}1959if($to->{'href'}) {1960$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1961-class=>"list"},$to_text);1962}1963$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1964"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1965return"<div class=\"diff$diff_class\">$line</div>\n";1966}elsif($from&&$to&&$line=~m/^\@{3}/) {1967my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1968my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);19691970@from_text=split(' ',$ranges);1971for(my$i=0;$i<@from_text; ++$i) {1972($from_start[$i],$from_nlines[$i]) =1973(split(',',substr($from_text[$i],1)),0);1974}19751976$to_text=pop@from_text;1977$to_start=pop@from_start;1978$to_nlines=pop@from_nlines;19791980$line="<span class=\"chunk_info\">$prefix";1981for(my$i=0;$i<@from_text; ++$i) {1982if($from->{'href'}[$i]) {1983$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1984-class=>"list"},$from_text[$i]);1985}else{1986$line.=$from_text[$i];1987}1988$line.=" ";1989}1990if($to->{'href'}) {1991$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1992-class=>"list"},$to_text);1993}else{1994$line.=$to_text;1995}1996$line.="$prefix</span>".1997"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1998return"<div class=\"diff$diff_class\">$line</div>\n";1999}2000return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2001}20022003# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2004# linked. Pass the hash of the tree/commit to snapshot.2005sub format_snapshot_links {2006my($hash) =@_;2007my$num_fmts=@snapshot_fmts;2008if($num_fmts>1) {2009# A parenthesized list of links bearing format names.2010# e.g. "snapshot (_tar.gz_ _zip_)"2011return"snapshot (".join(' ',map2012$cgi->a({2013-href => href(2014 action=>"snapshot",2015 hash=>$hash,2016 snapshot_format=>$_2017)2018},$known_snapshot_formats{$_}{'display'})2019,@snapshot_fmts) .")";2020}elsif($num_fmts==1) {2021# A single "snapshot" link whose tooltip bears the format name.2022# i.e. "_snapshot_"2023my($fmt) =@snapshot_fmts;2024return2025$cgi->a({2026-href => href(2027 action=>"snapshot",2028 hash=>$hash,2029 snapshot_format=>$fmt2030),2031-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2032},"snapshot");2033}else{# $num_fmts == 02034returnundef;2035}2036}20372038## ......................................................................2039## functions returning values to be passed, perhaps after some2040## transformation, to other functions; e.g. returning arguments to href()20412042# returns hash to be passed to href to generate gitweb URL2043# in -title key it returns description of link2044sub get_feed_info {2045my$format=shift||'Atom';2046my%res= (action =>lc($format));20472048# feed links are possible only for project views2049return unless(defined$project);2050# some views should link to OPML, or to generic project feed,2051# or don't have specific feed yet (so they should use generic)2052return if($action=~/^(?:tags|heads|forks|tag|search)$/x);20532054my$branch;2055# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2056# from tag links; this also makes possible to detect branch links2057if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2058(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2059$branch=$1;2060}2061# find log type for feed description (title)2062my$type='log';2063if(defined$file_name) {2064$type="history of$file_name";2065$type.="/"if($actioneq'tree');2066$type.=" on '$branch'"if(defined$branch);2067}else{2068$type="log of$branch"if(defined$branch);2069}20702071$res{-title} =$type;2072$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2073$res{'file_name'} =$file_name;20742075return%res;2076}20772078## ----------------------------------------------------------------------2079## git utility subroutines, invoking git commands20802081# returns path to the core git executable and the --git-dir parameter as list2082sub git_cmd {2083$number_of_git_cmds++;2084return$GIT,'--git-dir='.$git_dir;2085}20862087# quote the given arguments for passing them to the shell2088# quote_command("command", "arg 1", "arg with ' and ! characters")2089# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2090# Try to avoid using this function wherever possible.2091sub quote_command {2092returnjoin(' ',2093map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2094}20952096# get HEAD ref of given project as hash2097sub git_get_head_hash {2098return git_get_full_hash(shift,'HEAD');2099}21002101sub git_get_full_hash {2102return git_get_hash(@_);2103}21042105sub git_get_short_hash {2106return git_get_hash(@_,'--short=7');2107}21082109sub git_get_hash {2110my($project,$hash,@options) =@_;2111my$o_git_dir=$git_dir;2112my$retval=undef;2113$git_dir="$projectroot/$project";2114if(open my$fd,'-|', git_cmd(),'rev-parse',2115'--verify','-q',@options,$hash) {2116$retval= <$fd>;2117chomp$retvalifdefined$retval;2118close$fd;2119}2120if(defined$o_git_dir) {2121$git_dir=$o_git_dir;2122}2123return$retval;2124}21252126# get type of given object2127sub git_get_type {2128my$hash=shift;21292130open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2131my$type= <$fd>;2132close$fdorreturn;2133chomp$type;2134return$type;2135}21362137# repository configuration2138our$config_file='';2139our%config;21402141# store multiple values for single key as anonymous array reference2142# single values stored directly in the hash, not as [ <value> ]2143sub hash_set_multi {2144my($hash,$key,$value) =@_;21452146if(!exists$hash->{$key}) {2147$hash->{$key} =$value;2148}elsif(!ref$hash->{$key}) {2149$hash->{$key} = [$hash->{$key},$value];2150}else{2151push@{$hash->{$key}},$value;2152}2153}21542155# return hash of git project configuration2156# optionally limited to some section, e.g. 'gitweb'2157sub git_parse_project_config {2158my$section_regexp=shift;2159my%config;21602161local$/="\0";21622163open my$fh,"-|", git_cmd(),"config",'-z','-l',2164orreturn;21652166while(my$keyval= <$fh>) {2167chomp$keyval;2168my($key,$value) =split(/\n/,$keyval,2);21692170 hash_set_multi(\%config,$key,$value)2171if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2172}2173close$fh;21742175return%config;2176}21772178# convert config value to boolean: 'true' or 'false'2179# no value, number > 0, 'true' and 'yes' values are true2180# rest of values are treated as false (never as error)2181sub config_to_bool {2182my$val=shift;21832184return1if!defined$val;# section.key21852186# strip leading and trailing whitespace2187$val=~s/^\s+//;2188$val=~s/\s+$//;21892190return(($val=~/^\d+$/&&$val) ||# section.key = 12191($val=~/^(?:true|yes)$/i));# section.key = true2192}21932194# convert config value to simple decimal number2195# an optional value suffix of 'k', 'm', or 'g' will cause the value2196# to be multiplied by 1024, 1048576, or 10737418242197sub config_to_int {2198my$val=shift;21992200# strip leading and trailing whitespace2201$val=~s/^\s+//;2202$val=~s/\s+$//;22032204if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2205$unit=lc($unit);2206# unknown unit is treated as 12207return$num* ($uniteq'g'?1073741824:2208$uniteq'm'?1048576:2209$uniteq'k'?1024:1);2210}2211return$val;2212}22132214# convert config value to array reference, if needed2215sub config_to_multi {2216my$val=shift;22172218returnref($val) ?$val: (defined($val) ? [$val] : []);2219}22202221sub git_get_project_config {2222my($key,$type) =@_;22232224return unlessdefined$git_dir;22252226# key sanity check2227return unless($key);2228$key=~s/^gitweb\.//;2229return if($key=~m/\W/);22302231# type sanity check2232if(defined$type) {2233$type=~s/^--//;2234$type=undef2235unless($typeeq'bool'||$typeeq'int');2236}22372238# get config2239if(!defined$config_file||2240$config_filene"$git_dir/config") {2241%config= git_parse_project_config('gitweb');2242$config_file="$git_dir/config";2243}22442245# check if config variable (key) exists2246return unlessexists$config{"gitweb.$key"};22472248# ensure given type2249if(!defined$type) {2250return$config{"gitweb.$key"};2251}elsif($typeeq'bool') {2252# backward compatibility: 'git config --bool' returns true/false2253return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2254}elsif($typeeq'int') {2255return config_to_int($config{"gitweb.$key"});2256}2257return$config{"gitweb.$key"};2258}22592260# get hash of given path at given ref2261sub git_get_hash_by_path {2262my$base=shift;2263my$path=shift||returnundef;2264my$type=shift;22652266$path=~ s,/+$,,;22672268open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2269or die_error(500,"Open git-ls-tree failed");2270my$line= <$fd>;2271close$fdorreturnundef;22722273if(!defined$line) {2274# there is no tree or hash given by $path at $base2275returnundef;2276}22772278#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2279$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2280if(defined$type&&$typene$2) {2281# type doesn't match2282returnundef;2283}2284return$3;2285}22862287# get path of entry with given hash at given tree-ish (ref)2288# used to get 'from' filename for combined diff (merge commit) for renames2289sub git_get_path_by_hash {2290my$base=shift||return;2291my$hash=shift||return;22922293local$/="\0";22942295open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2296orreturnundef;2297while(my$line= <$fd>) {2298chomp$line;22992300#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2301#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2302if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2303close$fd;2304return$1;2305}2306}2307close$fd;2308returnundef;2309}23102311## ......................................................................2312## git utility functions, directly accessing git repository23132314sub git_get_project_description {2315my$path=shift;23162317$git_dir="$projectroot/$path";2318open my$fd,'<',"$git_dir/description"2319orreturn git_get_project_config('description');2320my$descr= <$fd>;2321close$fd;2322if(defined$descr) {2323chomp$descr;2324}2325return$descr;2326}23272328sub git_get_project_ctags {2329my$path=shift;2330my$ctags= {};23312332$git_dir="$projectroot/$path";2333opendir my$dh,"$git_dir/ctags"2334orreturn$ctags;2335foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2336open my$ct,'<',$_ornext;2337my$val= <$ct>;2338chomp$val;2339close$ct;2340my$ctag=$_;$ctag=~ s#.*/##;2341$ctags->{$ctag} =$val;2342}2343closedir$dh;2344$ctags;2345}23462347sub git_populate_project_tagcloud {2348my$ctags=shift;23492350# First, merge different-cased tags; tags vote on casing2351my%ctags_lc;2352foreach(keys%$ctags) {2353$ctags_lc{lc$_}->{count} +=$ctags->{$_};2354if(not$ctags_lc{lc$_}->{topcount}2355or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2356$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2357$ctags_lc{lc$_}->{topname} =$_;2358}2359}23602361my$cloud;2362if(eval{require HTML::TagCloud;1; }) {2363$cloud= HTML::TagCloud->new;2364foreach(sort keys%ctags_lc) {2365# Pad the title with spaces so that the cloud looks2366# less crammed.2367my$title=$ctags_lc{$_}->{topname};2368$title=~s/ / /g;2369$title=~s/^/ /g;2370$title=~s/$/ /g;2371$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2372}2373}else{2374$cloud= \%ctags_lc;2375}2376$cloud;2377}23782379sub git_show_project_tagcloud {2380my($cloud,$count) =@_;2381print STDERR ref($cloud)."..\n";2382if(ref$cloudeq'HTML::TagCloud') {2383return$cloud->html_and_css($count);2384}else{2385my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2386return'<p align="center">'.join(', ',map{2387"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2388}splice(@tags,0,$count)) .'</p>';2389}2390}23912392sub git_get_project_url_list {2393my$path=shift;23942395$git_dir="$projectroot/$path";2396open my$fd,'<',"$git_dir/cloneurl"2397orreturnwantarray?2398@{ config_to_multi(git_get_project_config('url')) } :2399 config_to_multi(git_get_project_config('url'));2400my@git_project_url_list=map{chomp;$_} <$fd>;2401close$fd;24022403returnwantarray?@git_project_url_list: \@git_project_url_list;2404}24052406sub git_get_projects_list {2407my($filter) =@_;2408my@list;24092410$filter||='';2411$filter=~s/\.git$//;24122413my$check_forks= gitweb_check_feature('forks');24142415if(-d $projects_list) {2416# search in directory2417my$dir=$projects_list. ($filter?"/$filter":'');2418# remove the trailing "/"2419$dir=~s!/+$!!;2420my$pfxlen=length("$dir");2421my$pfxdepth= ($dir=~tr!/!!);24222423 File::Find::find({2424 follow_fast =>1,# follow symbolic links2425 follow_skip =>2,# ignore duplicates2426 dangling_symlinks =>0,# ignore dangling symlinks, silently2427 wanted =>sub{2428# skip project-list toplevel, if we get it.2429return if(m!^[/.]$!);2430# only directories can be git repositories2431return unless(-d $_);2432# don't traverse too deep (Find is super slow on os x)2433if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2434$File::Find::prune =1;2435return;2436}24372438my$subdir=substr($File::Find::name,$pfxlen+1);2439# we check related file in $projectroot2440my$path= ($filter?"$filter/":'') .$subdir;2441if(check_export_ok("$projectroot/$path")) {2442push@list, { path =>$path};2443$File::Find::prune =1;2444}2445},2446},"$dir");24472448}elsif(-f $projects_list) {2449# read from file(url-encoded):2450# 'git%2Fgit.git Linus+Torvalds'2451# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2452# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2453my%paths;2454open my$fd,'<',$projects_listorreturn;2455 PROJECT:2456while(my$line= <$fd>) {2457chomp$line;2458my($path,$owner) =split' ',$line;2459$path= unescape($path);2460$owner= unescape($owner);2461if(!defined$path) {2462next;2463}2464if($filterne'') {2465# looking for forks;2466my$pfx=substr($path,0,length($filter));2467if($pfxne$filter) {2468next PROJECT;2469}2470my$sfx=substr($path,length($filter));2471if($sfx!~/^\/.*\.git$/) {2472next PROJECT;2473}2474}elsif($check_forks) {2475 PATH:2476foreachmy$filter(keys%paths) {2477# looking for forks;2478my$pfx=substr($path,0,length($filter));2479if($pfxne$filter) {2480next PATH;2481}2482my$sfx=substr($path,length($filter));2483if($sfx!~/^\/.*\.git$/) {2484next PATH;2485}2486# is a fork, don't include it in2487# the list2488next PROJECT;2489}2490}2491if(check_export_ok("$projectroot/$path")) {2492my$pr= {2493 path =>$path,2494 owner => to_utf8($owner),2495};2496push@list,$pr;2497(my$forks_path=$path) =~s/\.git$//;2498$paths{$forks_path}++;2499}2500}2501close$fd;2502}2503return@list;2504}25052506our$gitweb_project_owner=undef;2507sub git_get_project_list_from_file {25082509return if(defined$gitweb_project_owner);25102511$gitweb_project_owner= {};2512# read from file (url-encoded):2513# 'git%2Fgit.git Linus+Torvalds'2514# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2515# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2516if(-f $projects_list) {2517open(my$fd,'<',$projects_list);2518while(my$line= <$fd>) {2519chomp$line;2520my($pr,$ow) =split' ',$line;2521$pr= unescape($pr);2522$ow= unescape($ow);2523$gitweb_project_owner->{$pr} = to_utf8($ow);2524}2525close$fd;2526}2527}25282529sub git_get_project_owner {2530my$project=shift;2531my$owner;25322533returnundefunless$project;2534$git_dir="$projectroot/$project";25352536if(!defined$gitweb_project_owner) {2537 git_get_project_list_from_file();2538}25392540if(exists$gitweb_project_owner->{$project}) {2541$owner=$gitweb_project_owner->{$project};2542}2543if(!defined$owner){2544$owner= git_get_project_config('owner');2545}2546if(!defined$owner) {2547$owner= get_file_owner("$git_dir");2548}25492550return$owner;2551}25522553sub git_get_last_activity {2554my($path) =@_;2555my$fd;25562557$git_dir="$projectroot/$path";2558open($fd,"-|", git_cmd(),'for-each-ref',2559'--format=%(committer)',2560'--sort=-committerdate',2561'--count=1',2562'refs/heads')orreturn;2563my$most_recent= <$fd>;2564close$fdorreturn;2565if(defined$most_recent&&2566$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2567my$timestamp=$1;2568my$age=time-$timestamp;2569return($age, age_string($age));2570}2571return(undef,undef);2572}25732574sub git_get_references {2575my$type=shift||"";2576my%refs;2577# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112578# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2579open my$fd,"-|", git_cmd(),"show-ref","--dereference",2580($type? ("--","refs/$type") : ())# use -- <pattern> if $type2581orreturn;25822583while(my$line= <$fd>) {2584chomp$line;2585if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2586if(defined$refs{$1}) {2587push@{$refs{$1}},$2;2588}else{2589$refs{$1} = [$2];2590}2591}2592}2593close$fdorreturn;2594return \%refs;2595}25962597sub git_get_rev_name_tags {2598my$hash=shift||returnundef;25992600open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2601orreturn;2602my$name_rev= <$fd>;2603close$fd;26042605if($name_rev=~ m|^$hash tags/(.*)$|) {2606return$1;2607}else{2608# catches also '$hash undefined' output2609returnundef;2610}2611}26122613## ----------------------------------------------------------------------2614## parse to hash functions26152616sub parse_date {2617my$epoch=shift;2618my$tz=shift||"-0000";26192620my%date;2621my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2622my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2623my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2624$date{'hour'} =$hour;2625$date{'minute'} =$min;2626$date{'mday'} =$mday;2627$date{'day'} =$days[$wday];2628$date{'month'} =$months[$mon];2629$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2630$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2631$date{'mday-time'} =sprintf"%d%s%02d:%02d",2632$mday,$months[$mon],$hour,$min;2633$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",26341900+$year,1+$mon,$mday,$hour,$min,$sec;26352636$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2637my$local=$epoch+ ((int$1+ ($2/60)) *3600);2638($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2639$date{'hour_local'} =$hour;2640$date{'minute_local'} =$min;2641$date{'tz_local'} =$tz;2642$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",26431900+$year,$mon+1,$mday,2644$hour,$min,$sec,$tz);2645return%date;2646}26472648sub parse_tag {2649my$tag_id=shift;2650my%tag;2651my@comment;26522653open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2654$tag{'id'} =$tag_id;2655while(my$line= <$fd>) {2656chomp$line;2657if($line=~m/^object ([0-9a-fA-F]{40})$/) {2658$tag{'object'} =$1;2659}elsif($line=~m/^type (.+)$/) {2660$tag{'type'} =$1;2661}elsif($line=~m/^tag (.+)$/) {2662$tag{'name'} =$1;2663}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2664$tag{'author'} =$1;2665$tag{'author_epoch'} =$2;2666$tag{'author_tz'} =$3;2667if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2668$tag{'author_name'} =$1;2669$tag{'author_email'} =$2;2670}else{2671$tag{'author_name'} =$tag{'author'};2672}2673}elsif($line=~m/--BEGIN/) {2674push@comment,$line;2675last;2676}elsif($lineeq"") {2677last;2678}2679}2680push@comment, <$fd>;2681$tag{'comment'} = \@comment;2682close$fdorreturn;2683if(!defined$tag{'name'}) {2684return2685};2686return%tag2687}26882689sub parse_commit_text {2690my($commit_text,$withparents) =@_;2691my@commit_lines=split'\n',$commit_text;2692my%co;26932694pop@commit_lines;# Remove '\0'26952696if(!@commit_lines) {2697return;2698}26992700my$header=shift@commit_lines;2701if($header!~m/^[0-9a-fA-F]{40}/) {2702return;2703}2704($co{'id'},my@parents) =split' ',$header;2705while(my$line=shift@commit_lines) {2706last if$lineeq"\n";2707if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2708$co{'tree'} =$1;2709}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2710push@parents,$1;2711}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2712$co{'author'} = to_utf8($1);2713$co{'author_epoch'} =$2;2714$co{'author_tz'} =$3;2715if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2716$co{'author_name'} =$1;2717$co{'author_email'} =$2;2718}else{2719$co{'author_name'} =$co{'author'};2720}2721}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2722$co{'committer'} = to_utf8($1);2723$co{'committer_epoch'} =$2;2724$co{'committer_tz'} =$3;2725if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2726$co{'committer_name'} =$1;2727$co{'committer_email'} =$2;2728}else{2729$co{'committer_name'} =$co{'committer'};2730}2731}2732}2733if(!defined$co{'tree'}) {2734return;2735};2736$co{'parents'} = \@parents;2737$co{'parent'} =$parents[0];27382739foreachmy$title(@commit_lines) {2740$title=~s/^ //;2741if($titlene"") {2742$co{'title'} = chop_str($title,80,5);2743# remove leading stuff of merges to make the interesting part visible2744if(length($title) >50) {2745$title=~s/^Automatic //;2746$title=~s/^merge (of|with) /Merge ... /i;2747if(length($title) >50) {2748$title=~s/(http|rsync):\/\///;2749}2750if(length($title) >50) {2751$title=~s/(master|www|rsync)\.//;2752}2753if(length($title) >50) {2754$title=~s/kernel.org:?//;2755}2756if(length($title) >50) {2757$title=~s/\/pub\/scm//;2758}2759}2760$co{'title_short'} = chop_str($title,50,5);2761last;2762}2763}2764if(!defined$co{'title'} ||$co{'title'}eq"") {2765$co{'title'} =$co{'title_short'} ='(no commit message)';2766}2767# remove added spaces2768foreachmy$line(@commit_lines) {2769$line=~s/^ //;2770}2771$co{'comment'} = \@commit_lines;27722773my$age=time-$co{'committer_epoch'};2774$co{'age'} =$age;2775$co{'age_string'} = age_string($age);2776my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2777if($age>60*60*24*7*2) {2778$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2779$co{'age_string_age'} =$co{'age_string'};2780}else{2781$co{'age_string_date'} =$co{'age_string'};2782$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2783}2784return%co;2785}27862787sub parse_commit {2788my($commit_id) =@_;2789my%co;27902791local$/="\0";27922793open my$fd,"-|", git_cmd(),"rev-list",2794"--parents",2795"--header",2796"--max-count=1",2797$commit_id,2798"--",2799or die_error(500,"Open git-rev-list failed");2800%co= parse_commit_text(<$fd>,1);2801close$fd;28022803return%co;2804}28052806sub parse_commits {2807my($commit_id,$maxcount,$skip,$filename,@args) =@_;2808my@cos;28092810$maxcount||=1;2811$skip||=0;28122813local$/="\0";28142815open my$fd,"-|", git_cmd(),"rev-list",2816"--header",2817@args,2818("--max-count=".$maxcount),2819("--skip=".$skip),2820@extra_options,2821$commit_id,2822"--",2823($filename? ($filename) : ())2824or die_error(500,"Open git-rev-list failed");2825while(my$line= <$fd>) {2826my%co= parse_commit_text($line);2827push@cos, \%co;2828}2829close$fd;28302831returnwantarray?@cos: \@cos;2832}28332834# parse line of git-diff-tree "raw" output2835sub parse_difftree_raw_line {2836my$line=shift;2837my%res;28382839# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2840# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2841if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2842$res{'from_mode'} =$1;2843$res{'to_mode'} =$2;2844$res{'from_id'} =$3;2845$res{'to_id'} =$4;2846$res{'status'} =$5;2847$res{'similarity'} =$6;2848if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2849($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2850}else{2851$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2852}2853}2854# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2855# combined diff (for merge commit)2856elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2857$res{'nparents'} =length($1);2858$res{'from_mode'} = [split(' ',$2) ];2859$res{'to_mode'} =pop@{$res{'from_mode'}};2860$res{'from_id'} = [split(' ',$3) ];2861$res{'to_id'} =pop@{$res{'from_id'}};2862$res{'status'} = [split('',$4) ];2863$res{'to_file'} = unquote($5);2864}2865# 'c512b523472485aef4fff9e57b229d9d243c967f'2866elsif($line=~m/^([0-9a-fA-F]{40})$/) {2867$res{'commit'} =$1;2868}28692870returnwantarray?%res: \%res;2871}28722873# wrapper: return parsed line of git-diff-tree "raw" output2874# (the argument might be raw line, or parsed info)2875sub parsed_difftree_line {2876my$line_or_ref=shift;28772878if(ref($line_or_ref)eq"HASH") {2879# pre-parsed (or generated by hand)2880return$line_or_ref;2881}else{2882return parse_difftree_raw_line($line_or_ref);2883}2884}28852886# parse line of git-ls-tree output2887sub parse_ls_tree_line {2888my$line=shift;2889my%opts=@_;2890my%res;28912892if($opts{'-l'}) {2893#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'2894$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;28952896$res{'mode'} =$1;2897$res{'type'} =$2;2898$res{'hash'} =$3;2899$res{'size'} =$4;2900if($opts{'-z'}) {2901$res{'name'} =$5;2902}else{2903$res{'name'} = unquote($5);2904}2905}else{2906#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2907$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;29082909$res{'mode'} =$1;2910$res{'type'} =$2;2911$res{'hash'} =$3;2912if($opts{'-z'}) {2913$res{'name'} =$4;2914}else{2915$res{'name'} = unquote($4);2916}2917}29182919returnwantarray?%res: \%res;2920}29212922# generates _two_ hashes, references to which are passed as 2 and 3 argument2923sub parse_from_to_diffinfo {2924my($diffinfo,$from,$to,@parents) =@_;29252926if($diffinfo->{'nparents'}) {2927# combined diff2928$from->{'file'} = [];2929$from->{'href'} = [];2930 fill_from_file_info($diffinfo,@parents)2931unlessexists$diffinfo->{'from_file'};2932for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2933$from->{'file'}[$i] =2934defined$diffinfo->{'from_file'}[$i] ?2935$diffinfo->{'from_file'}[$i] :2936$diffinfo->{'to_file'};2937if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2938$from->{'href'}[$i] = href(action=>"blob",2939 hash_base=>$parents[$i],2940 hash=>$diffinfo->{'from_id'}[$i],2941 file_name=>$from->{'file'}[$i]);2942}else{2943$from->{'href'}[$i] =undef;2944}2945}2946}else{2947# ordinary (not combined) diff2948$from->{'file'} =$diffinfo->{'from_file'};2949if($diffinfo->{'status'}ne"A") {# not new (added) file2950$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2951 hash=>$diffinfo->{'from_id'},2952 file_name=>$from->{'file'});2953}else{2954delete$from->{'href'};2955}2956}29572958$to->{'file'} =$diffinfo->{'to_file'};2959if(!is_deleted($diffinfo)) {# file exists in result2960$to->{'href'} = href(action=>"blob", hash_base=>$hash,2961 hash=>$diffinfo->{'to_id'},2962 file_name=>$to->{'file'});2963}else{2964delete$to->{'href'};2965}2966}29672968## ......................................................................2969## parse to array of hashes functions29702971sub git_get_heads_list {2972my$limit=shift;2973my@headslist;29742975open my$fd,'-|', git_cmd(),'for-each-ref',2976($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2977'--format=%(objectname) %(refname) %(subject)%00%(committer)',2978'refs/heads'2979orreturn;2980while(my$line= <$fd>) {2981my%ref_item;29822983chomp$line;2984my($refinfo,$committerinfo) =split(/\0/,$line);2985my($hash,$name,$title) =split(' ',$refinfo,3);2986my($committer,$epoch,$tz) =2987($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2988$ref_item{'fullname'} =$name;2989$name=~s!^refs/heads/!!;29902991$ref_item{'name'} =$name;2992$ref_item{'id'} =$hash;2993$ref_item{'title'} =$title||'(no commit message)';2994$ref_item{'epoch'} =$epoch;2995if($epoch) {2996$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2997}else{2998$ref_item{'age'} ="unknown";2999}30003001push@headslist, \%ref_item;3002}3003close$fd;30043005returnwantarray?@headslist: \@headslist;3006}30073008sub git_get_tags_list {3009my$limit=shift;3010my@tagslist;30113012open my$fd,'-|', git_cmd(),'for-each-ref',3013($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3014'--format=%(objectname) %(objecttype) %(refname) '.3015'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3016'refs/tags'3017orreturn;3018while(my$line= <$fd>) {3019my%ref_item;30203021chomp$line;3022my($refinfo,$creatorinfo) =split(/\0/,$line);3023my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3024my($creator,$epoch,$tz) =3025($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3026$ref_item{'fullname'} =$name;3027$name=~s!^refs/tags/!!;30283029$ref_item{'type'} =$type;3030$ref_item{'id'} =$id;3031$ref_item{'name'} =$name;3032if($typeeq"tag") {3033$ref_item{'subject'} =$title;3034$ref_item{'reftype'} =$reftype;3035$ref_item{'refid'} =$refid;3036}else{3037$ref_item{'reftype'} =$type;3038$ref_item{'refid'} =$id;3039}30403041if($typeeq"tag"||$typeeq"commit") {3042$ref_item{'epoch'} =$epoch;3043if($epoch) {3044$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3045}else{3046$ref_item{'age'} ="unknown";3047}3048}30493050push@tagslist, \%ref_item;3051}3052close$fd;30533054returnwantarray?@tagslist: \@tagslist;3055}30563057## ----------------------------------------------------------------------3058## filesystem-related functions30593060sub get_file_owner {3061my$path=shift;30623063my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3064my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3065if(!defined$gcos) {3066returnundef;3067}3068my$owner=$gcos;3069$owner=~s/[,;].*$//;3070return to_utf8($owner);3071}30723073# assume that file exists3074sub insert_file {3075my$filename=shift;30763077open my$fd,'<',$filename;3078print map{ to_utf8($_) } <$fd>;3079close$fd;3080}30813082## ......................................................................3083## mimetype related functions30843085sub mimetype_guess_file {3086my$filename=shift;3087my$mimemap=shift;3088-r $mimemaporreturnundef;30893090my%mimemap;3091open(my$mh,'<',$mimemap)orreturnundef;3092while(<$mh>) {3093next ifm/^#/;# skip comments3094my($mimetype,$exts) =split(/\t+/);3095if(defined$exts) {3096my@exts=split(/\s+/,$exts);3097foreachmy$ext(@exts) {3098$mimemap{$ext} =$mimetype;3099}3100}3101}3102close($mh);31033104$filename=~/\.([^.]*)$/;3105return$mimemap{$1};3106}31073108sub mimetype_guess {3109my$filename=shift;3110my$mime;3111$filename=~/\./orreturnundef;31123113if($mimetypes_file) {3114my$file=$mimetypes_file;3115if($file!~m!^/!) {# if it is relative path3116# it is relative to project3117$file="$projectroot/$project/$file";3118}3119$mime= mimetype_guess_file($filename,$file);3120}3121$mime||= mimetype_guess_file($filename,'/etc/mime.types');3122return$mime;3123}31243125sub blob_mimetype {3126my$fd=shift;3127my$filename=shift;31283129if($filename) {3130my$mime= mimetype_guess($filename);3131$mimeandreturn$mime;3132}31333134# just in case3135return$default_blob_plain_mimetypeunless$fd;31363137if(-T $fd) {3138return'text/plain';3139}elsif(!$filename) {3140return'application/octet-stream';3141}elsif($filename=~m/\.png$/i) {3142return'image/png';3143}elsif($filename=~m/\.gif$/i) {3144return'image/gif';3145}elsif($filename=~m/\.jpe?g$/i) {3146return'image/jpeg';3147}else{3148return'application/octet-stream';3149}3150}31513152sub blob_contenttype {3153my($fd,$file_name,$type) =@_;31543155$type||= blob_mimetype($fd,$file_name);3156if($typeeq'text/plain'&&defined$default_text_plain_charset) {3157$type.="; charset=$default_text_plain_charset";3158}31593160return$type;3161}31623163## ======================================================================3164## functions printing HTML: header, footer, error page31653166sub git_header_html {3167my$status=shift||"200 OK";3168my$expires=shift;31693170my$title="$site_name";3171if(defined$project) {3172$title.=" - ". to_utf8($project);3173if(defined$action) {3174$title.="/$action";3175if(defined$file_name) {3176$title.=" - ". esc_path($file_name);3177if($actioneq"tree"&&$file_name!~ m|/$|) {3178$title.="/";3179}3180}3181}3182}3183my$content_type;3184# require explicit support from the UA if we are to send the page as3185# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3186# we have to do this because MSIE sometimes globs '*/*', pretending to3187# support xhtml+xml but choking when it gets what it asked for.3188if(defined$cgi->http('HTTP_ACCEPT') &&3189$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3190$cgi->Accept('application/xhtml+xml') !=0) {3191$content_type='application/xhtml+xml';3192}else{3193$content_type='text/html';3194}3195print$cgi->header(-type=>$content_type, -charset =>'utf-8',3196-status=>$status, -expires =>$expires);3197my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3198print<<EOF;3199<?xml version="1.0" encoding="utf-8"?>3200<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3201<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3202<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3203<!-- git core binaries version$git_version-->3204<head>3205<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3206<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3207<meta name="robots" content="index, nofollow"/>3208<title>$title</title>3209EOF3210# the stylesheet, favicon etc urls won't work correctly with path_info3211# unless we set the appropriate base URL3212if($ENV{'PATH_INFO'}) {3213print"<base href=\"".esc_url($base_url)."\"/>\n";3214}3215# print out each stylesheet that exist, providing backwards capability3216# for those people who defined $stylesheet in a config file3217if(defined$stylesheet) {3218print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3219}else{3220foreachmy$stylesheet(@stylesheets) {3221next unless$stylesheet;3222print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3223}3224}3225if(defined$project) {3226my%href_params= get_feed_info();3227if(!exists$href_params{'-title'}) {3228$href_params{'-title'} ='log';3229}32303231foreachmy$formatqw(RSS Atom){3232my$type=lc($format);3233my%link_attr= (3234'-rel'=>'alternate',3235'-title'=>"$project-$href_params{'-title'} -$formatfeed",3236'-type'=>"application/$type+xml"3237);32383239$href_params{'action'} =$type;3240$link_attr{'-href'} = href(%href_params);3241print"<link ".3242"rel=\"$link_attr{'-rel'}\"".3243"title=\"$link_attr{'-title'}\"".3244"href=\"$link_attr{'-href'}\"".3245"type=\"$link_attr{'-type'}\"".3246"/>\n";32473248$href_params{'extra_options'} ='--no-merges';3249$link_attr{'-href'} = href(%href_params);3250$link_attr{'-title'} .=' (no merges)';3251print"<link ".3252"rel=\"$link_attr{'-rel'}\"".3253"title=\"$link_attr{'-title'}\"".3254"href=\"$link_attr{'-href'}\"".3255"type=\"$link_attr{'-type'}\"".3256"/>\n";3257}32583259}else{3260printf('<link rel="alternate" title="%sprojects list" '.3261'href="%s" type="text/plain; charset=utf-8" />'."\n",3262$site_name, href(project=>undef, action=>"project_index"));3263printf('<link rel="alternate" title="%sprojects feeds" '.3264'href="%s" type="text/x-opml" />'."\n",3265$site_name, href(project=>undef, action=>"opml"));3266}3267if(defined$favicon) {3268printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3269}32703271print"</head>\n".3272"<body>\n";32733274if(defined$site_header&& -f $site_header) {3275 insert_file($site_header);3276}32773278print"<div class=\"page_header\">\n".3279$cgi->a({-href => esc_url($logo_url),3280-title =>$logo_label},3281qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3282print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3283if(defined$project) {3284print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3285if(defined$action) {3286print" /$action";3287}3288print"\n";3289}3290print"</div>\n";32913292my$have_search= gitweb_check_feature('search');3293if(defined$project&&$have_search) {3294if(!defined$searchtext) {3295$searchtext="";3296}3297my$search_hash;3298if(defined$hash_base) {3299$search_hash=$hash_base;3300}elsif(defined$hash) {3301$search_hash=$hash;3302}else{3303$search_hash="HEAD";3304}3305my$action=$my_uri;3306my$use_pathinfo= gitweb_check_feature('pathinfo');3307if($use_pathinfo) {3308$action.="/".esc_url($project);3309}3310print$cgi->startform(-method=>"get", -action =>$action) .3311"<div class=\"search\">\n".3312(!$use_pathinfo&&3313$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3314$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3315$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3316$cgi->popup_menu(-name =>'st', -default=>'commit',3317-values=> ['commit','grep','author','committer','pickaxe']) .3318$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3319" search:\n",3320$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3321"<span title=\"Extended regular expression\">".3322$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3323-checked =>$search_use_regexp) .3324"</span>".3325"</div>".3326$cgi->end_form() ."\n";3327}3328}33293330sub git_footer_html {3331my$feed_class='rss_logo';33323333print"<div class=\"page_footer\">\n";3334if(defined$project) {3335my$descr= git_get_project_description($project);3336if(defined$descr) {3337print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3338}33393340my%href_params= get_feed_info();3341if(!%href_params) {3342$feed_class.=' generic';3343}3344$href_params{'-title'} ||='log';33453346foreachmy$formatqw(RSS Atom){3347$href_params{'action'} =lc($format);3348print$cgi->a({-href => href(%href_params),3349-title =>"$href_params{'-title'}$formatfeed",3350-class=>$feed_class},$format)."\n";3351}33523353}else{3354print$cgi->a({-href => href(project=>undef, action=>"opml"),3355-class=>$feed_class},"OPML") ." ";3356print$cgi->a({-href => href(project=>undef, action=>"project_index"),3357-class=>$feed_class},"TXT") ."\n";3358}3359print"</div>\n";# class="page_footer"33603361if(defined$t0&& gitweb_check_feature('timed')) {3362print"<div id=\"generating_info\">\n";3363print'This page took '.3364'<span id="generating_time" class="time_span">'.3365 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3366' seconds </span>'.3367' and '.3368'<span id="generating_cmd">'.3369$number_of_git_cmds.3370'</span> git commands '.3371" to generate.\n";3372print"</div>\n";# class="page_footer"3373}33743375if(defined$site_footer&& -f $site_footer) {3376 insert_file($site_footer);3377}33783379print qq!<script type="text/javascript" src="$javascript"></script>\n!;3380if(defined$action&&3381$actioneq'blame_incremental') {3382print qq!<script type="text/javascript">\n!.3383 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3384 qq!"!. href() .qq!");\n!.3385 qq!</script>\n!;3386}elsif(gitweb_check_feature('javascript-actions')) {3387print qq!<script type="text/javascript">\n!.3388 qq!window.onload = fixLinks;\n!.3389 qq!</script>\n!;3390}33913392print"</body>\n".3393"</html>";3394}33953396# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3397# Example: die_error(404, 'Hash not found')3398# By convention, use the following status codes (as defined in RFC 2616):3399# 400: Invalid or missing CGI parameters, or3400# requested object exists but has wrong type.3401# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3402# this server or project.3403# 404: Requested object/revision/project doesn't exist.3404# 500: The server isn't configured properly, or3405# an internal error occurred (e.g. failed assertions caused by bugs), or3406# an unknown error occurred (e.g. the git binary died unexpectedly).3407# 503: The server is currently unavailable (because it is overloaded,3408# or down for maintenance). Generally, this is a temporary state.3409sub die_error {3410my$status=shift||500;3411my$error= esc_html(shift) ||"Internal Server Error";3412my$extra=shift;34133414my%http_responses= (3415400=>'400 Bad Request',3416403=>'403 Forbidden',3417404=>'404 Not Found',3418500=>'500 Internal Server Error',3419503=>'503 Service Unavailable',3420);3421 git_header_html($http_responses{$status});3422print<<EOF;3423<div class="page_body">3424<br /><br />3425$status-$error3426<br />3427EOF3428if(defined$extra) {3429print"<hr />\n".3430"$extra\n";3431}3432print"</div>\n";34333434 git_footer_html();3435exit;3436}34373438## ----------------------------------------------------------------------3439## functions printing or outputting HTML: navigation34403441sub git_print_page_nav {3442my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3443$extra=''if!defined$extra;# pager or formats34443445my@navs=qw(summary shortlog log commit commitdiff tree);3446if($suppress) {3447@navs=grep{$_ne$suppress}@navs;3448}34493450my%arg=map{$_=> {action=>$_} }@navs;3451if(defined$head) {3452for(qw(commit commitdiff)) {3453$arg{$_}{'hash'} =$head;3454}3455if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3456for(qw(shortlog log)) {3457$arg{$_}{'hash'} =$head;3458}3459}3460}34613462$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3463$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;34643465my@actions= gitweb_get_feature('actions');3466my%repl= (3467'%'=>'%',3468'n'=>$project,# project name3469'f'=>$git_dir,# project path within filesystem3470'h'=>$treehead||'',# current hash ('h' parameter)3471'b'=>$treebase||'',# hash base ('hb' parameter)3472);3473while(@actions) {3474my($label,$link,$pos) =splice(@actions,0,3);3475# insert3476@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3477# munch munch3478$link=~s/%([%nfhb])/$repl{$1}/g;3479$arg{$label}{'_href'} =$link;3480}34813482print"<div class=\"page_nav\">\n".3483(join" | ",3484map{$_eq$current?3485$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3486}@navs);3487print"<br/>\n$extra<br/>\n".3488"</div>\n";3489}34903491sub format_paging_nav {3492my($action,$page,$has_next_link) =@_;3493my$paging_nav;349434953496if($page>0) {3497$paging_nav.=3498$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3499" ⋅ ".3500$cgi->a({-href => href(-replay=>1, page=>$page-1),3501-accesskey =>"p", -title =>"Alt-p"},"prev");3502}else{3503$paging_nav.="first ⋅ prev";3504}35053506if($has_next_link) {3507$paging_nav.=" ⋅ ".3508$cgi->a({-href => href(-replay=>1, page=>$page+1),3509-accesskey =>"n", -title =>"Alt-n"},"next");3510}else{3511$paging_nav.=" ⋅ next";3512}35133514return$paging_nav;3515}35163517## ......................................................................3518## functions printing or outputting HTML: div35193520sub git_print_header_div {3521my($action,$title,$hash,$hash_base) =@_;3522my%args= ();35233524$args{'action'} =$action;3525$args{'hash'} =$hashif$hash;3526$args{'hash_base'} =$hash_baseif$hash_base;35273528print"<div class=\"header\">\n".3529$cgi->a({-href => href(%args), -class=>"title"},3530$title?$title:$action) .3531"\n</div>\n";3532}35333534sub print_local_time {3535print format_local_time(@_);3536}35373538sub format_local_time {3539my$localtime='';3540my%date=@_;3541if($date{'hour_local'} <6) {3542$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3543$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3544}else{3545$localtime.=sprintf(" (%02d:%02d%s)",3546$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3547}35483549return$localtime;3550}35513552# Outputs the author name and date in long form3553sub git_print_authorship {3554my$co=shift;3555my%opts=@_;3556my$tag=$opts{-tag} ||'div';3557my$author=$co->{'author_name'};35583559my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3560print"<$tagclass=\"author_date\">".3561 format_search_author($author,"author", esc_html($author)) .3562" [$ad{'rfc2822'}";3563 print_local_time(%ad)if($opts{-localtime});3564print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3565."</$tag>\n";3566}35673568# Outputs table rows containing the full author or committer information,3569# in the format expected for 'commit' view (& similia).3570# Parameters are a commit hash reference, followed by the list of people3571# to output information for. If the list is empty it defalts to both3572# author and committer.3573sub git_print_authorship_rows {3574my$co=shift;3575# too bad we can't use @people = @_ || ('author', 'committer')3576my@people=@_;3577@people= ('author','committer')unless@people;3578foreachmy$who(@people) {3579my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3580print"<tr><td>$who</td><td>".3581 format_search_author($co->{"${who}_name"},$who,3582 esc_html($co->{"${who}_name"})) ." ".3583 format_search_author($co->{"${who}_email"},$who,3584 esc_html("<".$co->{"${who}_email"} .">")) .3585"</td><td rowspan=\"2\">".3586 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3587"</td></tr>\n".3588"<tr>".3589"<td></td><td>$wd{'rfc2822'}";3590 print_local_time(%wd);3591print"</td>".3592"</tr>\n";3593}3594}35953596sub git_print_page_path {3597my$name=shift;3598my$type=shift;3599my$hb=shift;360036013602print"<div class=\"page_path\">";3603print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3604-title =>'tree root'}, to_utf8("[$project]"));3605print" / ";3606if(defined$name) {3607my@dirname=split'/',$name;3608my$basename=pop@dirname;3609my$fullname='';36103611foreachmy$dir(@dirname) {3612$fullname.= ($fullname?'/':'') .$dir;3613print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3614 hash_base=>$hb),3615-title =>$fullname}, esc_path($dir));3616print" / ";3617}3618if(defined$type&&$typeeq'blob') {3619print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3620 hash_base=>$hb),3621-title =>$name}, esc_path($basename));3622}elsif(defined$type&&$typeeq'tree') {3623print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3624 hash_base=>$hb),3625-title =>$name}, esc_path($basename));3626print" / ";3627}else{3628print esc_path($basename);3629}3630}3631print"<br/></div>\n";3632}36333634sub git_print_log {3635my$log=shift;3636my%opts=@_;36373638if($opts{'-remove_title'}) {3639# remove title, i.e. first line of log3640shift@$log;3641}3642# remove leading empty lines3643while(defined$log->[0] &&$log->[0]eq"") {3644shift@$log;3645}36463647# print log3648my$signoff=0;3649my$empty=0;3650foreachmy$line(@$log) {3651if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3652$signoff=1;3653$empty=0;3654if(!$opts{'-remove_signoff'}) {3655print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3656next;3657}else{3658# remove signoff lines3659next;3660}3661}else{3662$signoff=0;3663}36643665# print only one empty line3666# do not print empty line after signoff3667if($lineeq"") {3668next if($empty||$signoff);3669$empty=1;3670}else{3671$empty=0;3672}36733674print format_log_line_html($line) ."<br/>\n";3675}36763677if($opts{'-final_empty_line'}) {3678# end with single empty line3679print"<br/>\n"unless$empty;3680}3681}36823683# return link target (what link points to)3684sub git_get_link_target {3685my$hash=shift;3686my$link_target;36873688# read link3689open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3690orreturn;3691{3692local$/=undef;3693$link_target= <$fd>;3694}3695close$fd3696orreturn;36973698return$link_target;3699}37003701# given link target, and the directory (basedir) the link is in,3702# return target of link relative to top directory (top tree);3703# return undef if it is not possible (including absolute links).3704sub normalize_link_target {3705my($link_target,$basedir) =@_;37063707# absolute symlinks (beginning with '/') cannot be normalized3708return if(substr($link_target,0,1)eq'/');37093710# normalize link target to path from top (root) tree (dir)3711my$path;3712if($basedir) {3713$path=$basedir.'/'.$link_target;3714}else{3715# we are in top (root) tree (dir)3716$path=$link_target;3717}37183719# remove //, /./, and /../3720my@path_parts;3721foreachmy$part(split('/',$path)) {3722# discard '.' and ''3723next if(!$part||$parteq'.');3724# handle '..'3725if($parteq'..') {3726if(@path_parts) {3727pop@path_parts;3728}else{3729# link leads outside repository (outside top dir)3730return;3731}3732}else{3733push@path_parts,$part;3734}3735}3736$path=join('/',@path_parts);37373738return$path;3739}37403741# print tree entry (row of git_tree), but without encompassing <tr> element3742sub git_print_tree_entry {3743my($t,$basedir,$hash_base,$have_blame) =@_;37443745my%base_key= ();3746$base_key{'hash_base'} =$hash_baseifdefined$hash_base;37473748# The format of a table row is: mode list link. Where mode is3749# the mode of the entry, list is the name of the entry, an href,3750# and link is the action links of the entry.37513752print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3753if(exists$t->{'size'}) {3754print"<td class=\"size\">$t->{'size'}</td>\n";3755}3756if($t->{'type'}eq"blob") {3757print"<td class=\"list\">".3758$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3759 file_name=>"$basedir$t->{'name'}",%base_key),3760-class=>"list"}, esc_path($t->{'name'}));3761if(S_ISLNK(oct$t->{'mode'})) {3762my$link_target= git_get_link_target($t->{'hash'});3763if($link_target) {3764my$norm_target= normalize_link_target($link_target,$basedir);3765if(defined$norm_target) {3766print" -> ".3767$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3768 file_name=>$norm_target),3769-title =>$norm_target}, esc_path($link_target));3770}else{3771print" -> ". esc_path($link_target);3772}3773}3774}3775print"</td>\n";3776print"<td class=\"link\">";3777print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3778 file_name=>"$basedir$t->{'name'}",%base_key)},3779"blob");3780if($have_blame) {3781print" | ".3782$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3783 file_name=>"$basedir$t->{'name'}",%base_key)},3784"blame");3785}3786if(defined$hash_base) {3787print" | ".3788$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3789 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3790"history");3791}3792print" | ".3793$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3794 file_name=>"$basedir$t->{'name'}")},3795"raw");3796print"</td>\n";37973798}elsif($t->{'type'}eq"tree") {3799print"<td class=\"list\">";3800print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3801 file_name=>"$basedir$t->{'name'}",3802%base_key)},3803 esc_path($t->{'name'}));3804print"</td>\n";3805print"<td class=\"link\">";3806print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3807 file_name=>"$basedir$t->{'name'}",3808%base_key)},3809"tree");3810if(defined$hash_base) {3811print" | ".3812$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3813 file_name=>"$basedir$t->{'name'}")},3814"history");3815}3816print"</td>\n";3817}else{3818# unknown object: we can only present history for it3819# (this includes 'commit' object, i.e. submodule support)3820print"<td class=\"list\">".3821 esc_path($t->{'name'}) .3822"</td>\n";3823print"<td class=\"link\">";3824if(defined$hash_base) {3825print$cgi->a({-href => href(action=>"history",3826 hash_base=>$hash_base,3827 file_name=>"$basedir$t->{'name'}")},3828"history");3829}3830print"</td>\n";3831}3832}38333834## ......................................................................3835## functions printing large fragments of HTML38363837# get pre-image filenames for merge (combined) diff3838sub fill_from_file_info {3839my($diff,@parents) =@_;38403841$diff->{'from_file'} = [ ];3842$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3843for(my$i=0;$i<$diff->{'nparents'};$i++) {3844if($diff->{'status'}[$i]eq'R'||3845$diff->{'status'}[$i]eq'C') {3846$diff->{'from_file'}[$i] =3847 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3848}3849}38503851return$diff;3852}38533854# is current raw difftree line of file deletion3855sub is_deleted {3856my$diffinfo=shift;38573858return$diffinfo->{'to_id'}eq('0' x 40);3859}38603861# does patch correspond to [previous] difftree raw line3862# $diffinfo - hashref of parsed raw diff format3863# $patchinfo - hashref of parsed patch diff format3864# (the same keys as in $diffinfo)3865sub is_patch_split {3866my($diffinfo,$patchinfo) =@_;38673868returndefined$diffinfo&&defined$patchinfo3869&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3870}387138723873sub git_difftree_body {3874my($difftree,$hash,@parents) =@_;3875my($parent) =$parents[0];3876my$have_blame= gitweb_check_feature('blame');3877print"<div class=\"list_head\">\n";3878if($#{$difftree} >10) {3879print(($#{$difftree} +1) ." files changed:\n");3880}3881print"</div>\n";38823883print"<table class=\"".3884(@parents>1?"combined ":"") .3885"diff_tree\">\n";38863887# header only for combined diff in 'commitdiff' view3888my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3889if($has_header) {3890# table header3891print"<thead><tr>\n".3892"<th></th><th></th>\n";# filename, patchN link3893for(my$i=0;$i<@parents;$i++) {3894my$par=$parents[$i];3895print"<th>".3896$cgi->a({-href => href(action=>"commitdiff",3897 hash=>$hash, hash_parent=>$par),3898-title =>'commitdiff to parent number '.3899($i+1) .': '.substr($par,0,7)},3900$i+1) .3901" </th>\n";3902}3903print"</tr></thead>\n<tbody>\n";3904}39053906my$alternate=1;3907my$patchno=0;3908foreachmy$line(@{$difftree}) {3909my$diff= parsed_difftree_line($line);39103911if($alternate) {3912print"<tr class=\"dark\">\n";3913}else{3914print"<tr class=\"light\">\n";3915}3916$alternate^=1;39173918if(exists$diff->{'nparents'}) {# combined diff39193920 fill_from_file_info($diff,@parents)3921unlessexists$diff->{'from_file'};39223923if(!is_deleted($diff)) {3924# file exists in the result (child) commit3925print"<td>".3926$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3927 file_name=>$diff->{'to_file'},3928 hash_base=>$hash),3929-class=>"list"}, esc_path($diff->{'to_file'})) .3930"</td>\n";3931}else{3932print"<td>".3933 esc_path($diff->{'to_file'}) .3934"</td>\n";3935}39363937if($actioneq'commitdiff') {3938# link to patch3939$patchno++;3940print"<td class=\"link\">".3941$cgi->a({-href =>"#patch$patchno"},"patch") .3942" | ".3943"</td>\n";3944}39453946my$has_history=0;3947my$not_deleted=0;3948for(my$i=0;$i<$diff->{'nparents'};$i++) {3949my$hash_parent=$parents[$i];3950my$from_hash=$diff->{'from_id'}[$i];3951my$from_path=$diff->{'from_file'}[$i];3952my$status=$diff->{'status'}[$i];39533954$has_history||= ($statusne'A');3955$not_deleted||= ($statusne'D');39563957if($statuseq'A') {3958print"<td class=\"link\"align=\"right\"> | </td>\n";3959}elsif($statuseq'D') {3960print"<td class=\"link\">".3961$cgi->a({-href => href(action=>"blob",3962 hash_base=>$hash,3963 hash=>$from_hash,3964 file_name=>$from_path)},3965"blob". ($i+1)) .3966" | </td>\n";3967}else{3968if($diff->{'to_id'}eq$from_hash) {3969print"<td class=\"link nochange\">";3970}else{3971print"<td class=\"link\">";3972}3973print$cgi->a({-href => href(action=>"blobdiff",3974 hash=>$diff->{'to_id'},3975 hash_parent=>$from_hash,3976 hash_base=>$hash,3977 hash_parent_base=>$hash_parent,3978 file_name=>$diff->{'to_file'},3979 file_parent=>$from_path)},3980"diff". ($i+1)) .3981" | </td>\n";3982}3983}39843985print"<td class=\"link\">";3986if($not_deleted) {3987print$cgi->a({-href => href(action=>"blob",3988 hash=>$diff->{'to_id'},3989 file_name=>$diff->{'to_file'},3990 hash_base=>$hash)},3991"blob");3992print" | "if($has_history);3993}3994if($has_history) {3995print$cgi->a({-href => href(action=>"history",3996 file_name=>$diff->{'to_file'},3997 hash_base=>$hash)},3998"history");3999}4000print"</td>\n";40014002print"</tr>\n";4003next;# instead of 'else' clause, to avoid extra indent4004}4005# else ordinary diff40064007my($to_mode_oct,$to_mode_str,$to_file_type);4008my($from_mode_oct,$from_mode_str,$from_file_type);4009if($diff->{'to_mode'}ne('0' x 6)) {4010$to_mode_oct=oct$diff->{'to_mode'};4011if(S_ISREG($to_mode_oct)) {# only for regular file4012$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4013}4014$to_file_type= file_type($diff->{'to_mode'});4015}4016if($diff->{'from_mode'}ne('0' x 6)) {4017$from_mode_oct=oct$diff->{'from_mode'};4018if(S_ISREG($to_mode_oct)) {# only for regular file4019$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4020}4021$from_file_type= file_type($diff->{'from_mode'});4022}40234024if($diff->{'status'}eq"A") {# created4025my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4026$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4027$mode_chng.="]</span>";4028print"<td>";4029print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4030 hash_base=>$hash, file_name=>$diff->{'file'}),4031-class=>"list"}, esc_path($diff->{'file'}));4032print"</td>\n";4033print"<td>$mode_chng</td>\n";4034print"<td class=\"link\">";4035if($actioneq'commitdiff') {4036# link to patch4037$patchno++;4038print$cgi->a({-href =>"#patch$patchno"},"patch");4039print" | ";4040}4041print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4042 hash_base=>$hash, file_name=>$diff->{'file'})},4043"blob");4044print"</td>\n";40454046}elsif($diff->{'status'}eq"D") {# deleted4047my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4048print"<td>";4049print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4050 hash_base=>$parent, file_name=>$diff->{'file'}),4051-class=>"list"}, esc_path($diff->{'file'}));4052print"</td>\n";4053print"<td>$mode_chng</td>\n";4054print"<td class=\"link\">";4055if($actioneq'commitdiff') {4056# link to patch4057$patchno++;4058print$cgi->a({-href =>"#patch$patchno"},"patch");4059print" | ";4060}4061print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4062 hash_base=>$parent, file_name=>$diff->{'file'})},4063"blob") ." | ";4064if($have_blame) {4065print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4066 file_name=>$diff->{'file'})},4067"blame") ." | ";4068}4069print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4070 file_name=>$diff->{'file'})},4071"history");4072print"</td>\n";40734074}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4075my$mode_chnge="";4076if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4077$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4078if($from_file_typene$to_file_type) {4079$mode_chnge.=" from$from_file_typeto$to_file_type";4080}4081if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4082if($from_mode_str&&$to_mode_str) {4083$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4084}elsif($to_mode_str) {4085$mode_chnge.=" mode:$to_mode_str";4086}4087}4088$mode_chnge.="]</span>\n";4089}4090print"<td>";4091print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4092 hash_base=>$hash, file_name=>$diff->{'file'}),4093-class=>"list"}, esc_path($diff->{'file'}));4094print"</td>\n";4095print"<td>$mode_chnge</td>\n";4096print"<td class=\"link\">";4097if($actioneq'commitdiff') {4098# link to patch4099$patchno++;4100print$cgi->a({-href =>"#patch$patchno"},"patch") .4101" | ";4102}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4103# "commit" view and modified file (not onlu mode changed)4104print$cgi->a({-href => href(action=>"blobdiff",4105 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4106 hash_base=>$hash, hash_parent_base=>$parent,4107 file_name=>$diff->{'file'})},4108"diff") .4109" | ";4110}4111print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4112 hash_base=>$hash, file_name=>$diff->{'file'})},4113"blob") ." | ";4114if($have_blame) {4115print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4116 file_name=>$diff->{'file'})},4117"blame") ." | ";4118}4119print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4120 file_name=>$diff->{'file'})},4121"history");4122print"</td>\n";41234124}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4125my%status_name= ('R'=>'moved','C'=>'copied');4126my$nstatus=$status_name{$diff->{'status'}};4127my$mode_chng="";4128if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4129# mode also for directories, so we cannot use $to_mode_str4130$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4131}4132print"<td>".4133$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4134 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4135-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4136"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4137$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4138 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4139-class=>"list"}, esc_path($diff->{'from_file'})) .4140" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4141"<td class=\"link\">";4142if($actioneq'commitdiff') {4143# link to patch4144$patchno++;4145print$cgi->a({-href =>"#patch$patchno"},"patch") .4146" | ";4147}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4148# "commit" view and modified file (not only pure rename or copy)4149print$cgi->a({-href => href(action=>"blobdiff",4150 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4151 hash_base=>$hash, hash_parent_base=>$parent,4152 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4153"diff") .4154" | ";4155}4156print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4157 hash_base=>$parent, file_name=>$diff->{'to_file'})},4158"blob") ." | ";4159if($have_blame) {4160print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4161 file_name=>$diff->{'to_file'})},4162"blame") ." | ";4163}4164print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4165 file_name=>$diff->{'to_file'})},4166"history");4167print"</td>\n";41684169}# we should not encounter Unmerged (U) or Unknown (X) status4170print"</tr>\n";4171}4172print"</tbody>"if$has_header;4173print"</table>\n";4174}41754176sub git_patchset_body {4177my($fd,$difftree,$hash,@hash_parents) =@_;4178my($hash_parent) =$hash_parents[0];41794180my$is_combined= (@hash_parents>1);4181my$patch_idx=0;4182my$patch_number=0;4183my$patch_line;4184my$diffinfo;4185my$to_name;4186my(%from,%to);41874188print"<div class=\"patchset\">\n";41894190# skip to first patch4191while($patch_line= <$fd>) {4192chomp$patch_line;41934194last if($patch_line=~m/^diff /);4195}41964197 PATCH:4198while($patch_line) {41994200# parse "git diff" header line4201if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4202# $1 is from_name, which we do not use4203$to_name= unquote($2);4204$to_name=~s!^b/!!;4205}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4206# $1 is 'cc' or 'combined', which we do not use4207$to_name= unquote($2);4208}else{4209$to_name=undef;4210}42114212# check if current patch belong to current raw line4213# and parse raw git-diff line if needed4214if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4215# this is continuation of a split patch4216print"<div class=\"patch cont\">\n";4217}else{4218# advance raw git-diff output if needed4219$patch_idx++ifdefined$diffinfo;42204221# read and prepare patch information4222$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42234224# compact combined diff output can have some patches skipped4225# find which patch (using pathname of result) we are at now;4226if($is_combined) {4227while($to_namene$diffinfo->{'to_file'}) {4228print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4229 format_diff_cc_simplified($diffinfo,@hash_parents) .4230"</div>\n";# class="patch"42314232$patch_idx++;4233$patch_number++;42344235last if$patch_idx>$#$difftree;4236$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4237}4238}42394240# modifies %from, %to hashes4241 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);42424243# this is first patch for raw difftree line with $patch_idx index4244# we index @$difftree array from 0, but number patches from 14245print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4246}42474248# git diff header4249#assert($patch_line =~ m/^diff /) if DEBUG;4250#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4251$patch_number++;4252# print "git diff" header4253print format_git_diff_header_line($patch_line,$diffinfo,4254 \%from, \%to);42554256# print extended diff header4257print"<div class=\"diff extended_header\">\n";4258 EXTENDED_HEADER:4259while($patch_line= <$fd>) {4260chomp$patch_line;42614262last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);42634264print format_extended_diff_header_line($patch_line,$diffinfo,4265 \%from, \%to);4266}4267print"</div>\n";# class="diff extended_header"42684269# from-file/to-file diff header4270if(!$patch_line) {4271print"</div>\n";# class="patch"4272last PATCH;4273}4274next PATCH if($patch_line=~m/^diff /);4275#assert($patch_line =~ m/^---/) if DEBUG;42764277my$last_patch_line=$patch_line;4278$patch_line= <$fd>;4279chomp$patch_line;4280#assert($patch_line =~ m/^\+\+\+/) if DEBUG;42814282print format_diff_from_to_header($last_patch_line,$patch_line,4283$diffinfo, \%from, \%to,4284@hash_parents);42854286# the patch itself4287 LINE:4288while($patch_line= <$fd>) {4289chomp$patch_line;42904291next PATCH if($patch_line=~m/^diff /);42924293print format_diff_line($patch_line, \%from, \%to);4294}42954296}continue{4297print"</div>\n";# class="patch"4298}42994300# for compact combined (--cc) format, with chunk and patch simpliciaction4301# patchset might be empty, but there might be unprocessed raw lines4302for(++$patch_idxif$patch_number>0;4303$patch_idx<@$difftree;4304++$patch_idx) {4305# read and prepare patch information4306$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);43074308# generate anchor for "patch" links in difftree / whatchanged part4309print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4310 format_diff_cc_simplified($diffinfo,@hash_parents) .4311"</div>\n";# class="patch"43124313$patch_number++;4314}43154316if($patch_number==0) {4317if(@hash_parents>1) {4318print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4319}else{4320print"<div class=\"diff nodifferences\">No differences found</div>\n";4321}4322}43234324print"</div>\n";# class="patchset"4325}43264327# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43284329# fills project list info (age, description, owner, forks) for each4330# project in the list, removing invalid projects from returned list4331# NOTE: modifies $projlist, but does not remove entries from it4332sub fill_project_list_info {4333my($projlist,$check_forks) =@_;4334my@projects;43354336my$show_ctags= gitweb_check_feature('ctags');4337 PROJECT:4338foreachmy$pr(@$projlist) {4339my(@activity) = git_get_last_activity($pr->{'path'});4340unless(@activity) {4341next PROJECT;4342}4343($pr->{'age'},$pr->{'age_string'}) =@activity;4344if(!defined$pr->{'descr'}) {4345my$descr= git_get_project_description($pr->{'path'}) ||"";4346$descr= to_utf8($descr);4347$pr->{'descr_long'} =$descr;4348$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4349}4350if(!defined$pr->{'owner'}) {4351$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4352}4353if($check_forks) {4354my$pname=$pr->{'path'};4355if(($pname=~s/\.git$//) &&4356($pname!~/\/$/) &&4357(-d "$projectroot/$pname")) {4358$pr->{'forks'} ="-d$projectroot/$pname";4359}else{4360$pr->{'forks'} =0;4361}4362}4363$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4364push@projects,$pr;4365}43664367return@projects;4368}43694370# print 'sort by' <th> element, generating 'sort by $name' replay link4371# if that order is not selected4372sub print_sort_th {4373print format_sort_th(@_);4374}43754376sub format_sort_th {4377my($name,$order,$header) =@_;4378my$sort_th="";4379$header||=ucfirst($name);43804381if($ordereq$name) {4382$sort_th.="<th>$header</th>\n";4383}else{4384$sort_th.="<th>".4385$cgi->a({-href => href(-replay=>1, order=>$name),4386-class=>"header"},$header) .4387"</th>\n";4388}43894390return$sort_th;4391}43924393sub git_project_list_body {4394# actually uses global variable $project4395my($projlist,$order,$from,$to,$extra,$no_header) =@_;43964397my$check_forks= gitweb_check_feature('forks');4398my@projects= fill_project_list_info($projlist,$check_forks);43994400$order||=$default_projects_order;4401$from=0unlessdefined$from;4402$to=$#projectsif(!defined$to||$#projects<$to);44034404my%order_info= (4405 project => { key =>'path', type =>'str'},4406 descr => { key =>'descr_long', type =>'str'},4407 owner => { key =>'owner', type =>'str'},4408 age => { key =>'age', type =>'num'}4409);4410my$oi=$order_info{$order};4411if($oi->{'type'}eq'str') {4412@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4413}else{4414@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4415}44164417my$show_ctags= gitweb_check_feature('ctags');4418if($show_ctags) {4419my%ctags;4420foreachmy$p(@projects) {4421foreachmy$ct(keys%{$p->{'ctags'}}) {4422$ctags{$ct} +=$p->{'ctags'}->{$ct};4423}4424}4425my$cloud= git_populate_project_tagcloud(\%ctags);4426print git_show_project_tagcloud($cloud,64);4427}44284429print"<table class=\"project_list\">\n";4430unless($no_header) {4431print"<tr>\n";4432if($check_forks) {4433print"<th></th>\n";4434}4435 print_sort_th('project',$order,'Project');4436 print_sort_th('descr',$order,'Description');4437 print_sort_th('owner',$order,'Owner');4438 print_sort_th('age',$order,'Last Change');4439print"<th></th>\n".# for links4440"</tr>\n";4441}4442my$alternate=1;4443my$tagfilter=$cgi->param('by_tag');4444for(my$i=$from;$i<=$to;$i++) {4445my$pr=$projects[$i];44464447next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4448next if$searchtextand not$pr->{'path'} =~/$searchtext/4449and not$pr->{'descr_long'} =~/$searchtext/;4450# Weed out forks or non-matching entries of search4451if($check_forks) {4452my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4453$forkbase="^$forkbase"if$forkbase;4454next ifnot$searchtextand not$tagfilterand$show_ctags4455and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4456}44574458if($alternate) {4459print"<tr class=\"dark\">\n";4460}else{4461print"<tr class=\"light\">\n";4462}4463$alternate^=1;4464if($check_forks) {4465print"<td>";4466if($pr->{'forks'}) {4467print"<!--$pr->{'forks'} -->\n";4468print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4469}4470print"</td>\n";4471}4472print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4473-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4474"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4475-class=>"list", -title =>$pr->{'descr_long'}},4476 esc_html($pr->{'descr'})) ."</td>\n".4477"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4478print"<td class=\"". age_class($pr->{'age'}) ."\">".4479(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4480"<td class=\"link\">".4481$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4482$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4483$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4484$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4485($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4486"</td>\n".4487"</tr>\n";4488}4489if(defined$extra) {4490print"<tr>\n";4491if($check_forks) {4492print"<td></td>\n";4493}4494print"<td colspan=\"5\">$extra</td>\n".4495"</tr>\n";4496}4497print"</table>\n";4498}44994500sub git_log_body {4501# uses global variable $project4502my($commitlist,$from,$to,$refs,$extra) =@_;45034504$from=0unlessdefined$from;4505$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45064507for(my$i=0;$i<=$to;$i++) {4508my%co= %{$commitlist->[$i]};4509next if!%co;4510my$commit=$co{'id'};4511my$ref= format_ref_marker($refs,$commit);4512my%ad= parse_date($co{'author_epoch'});4513 git_print_header_div('commit',4514"<span class=\"age\">$co{'age_string'}</span>".4515 esc_html($co{'title'}) .$ref,4516$commit);4517print"<div class=\"title_text\">\n".4518"<div class=\"log_link\">\n".4519$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4520" | ".4521$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4522" | ".4523$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4524"<br/>\n".4525"</div>\n";4526 git_print_authorship(\%co, -tag =>'span');4527print"<br/>\n</div>\n";45284529print"<div class=\"log_body\">\n";4530 git_print_log($co{'comment'}, -final_empty_line=>1);4531print"</div>\n";4532}4533if($extra) {4534print"<div class=\"page_nav\">\n";4535print"$extra\n";4536print"</div>\n";4537}4538}45394540sub git_shortlog_body {4541# uses global variable $project4542my($commitlist,$from,$to,$refs,$extra) =@_;45434544$from=0unlessdefined$from;4545$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45464547print"<table class=\"shortlog\">\n";4548my$alternate=1;4549for(my$i=$from;$i<=$to;$i++) {4550my%co= %{$commitlist->[$i]};4551my$commit=$co{'id'};4552my$ref= format_ref_marker($refs,$commit);4553if($alternate) {4554print"<tr class=\"dark\">\n";4555}else{4556print"<tr class=\"light\">\n";4557}4558$alternate^=1;4559# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4560print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4561 format_author_html('td', \%co,10) ."<td>";4562print format_subject_html($co{'title'},$co{'title_short'},4563 href(action=>"commit", hash=>$commit),$ref);4564print"</td>\n".4565"<td class=\"link\">".4566$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4567$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4568$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4569my$snapshot_links= format_snapshot_links($commit);4570if(defined$snapshot_links) {4571print" | ".$snapshot_links;4572}4573print"</td>\n".4574"</tr>\n";4575}4576if(defined$extra) {4577print"<tr>\n".4578"<td colspan=\"4\">$extra</td>\n".4579"</tr>\n";4580}4581print"</table>\n";4582}45834584sub git_history_body {4585# Warning: assumes constant type (blob or tree) during history4586my($commitlist,$from,$to,$refs,$extra,4587$file_name,$file_hash,$ftype) =@_;45884589$from=0unlessdefined$from;4590$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});45914592print"<table class=\"history\">\n";4593my$alternate=1;4594for(my$i=$from;$i<=$to;$i++) {4595my%co= %{$commitlist->[$i]};4596if(!%co) {4597next;4598}4599my$commit=$co{'id'};46004601my$ref= format_ref_marker($refs,$commit);46024603if($alternate) {4604print"<tr class=\"dark\">\n";4605}else{4606print"<tr class=\"light\">\n";4607}4608$alternate^=1;4609print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4610# shortlog: format_author_html('td', \%co, 10)4611 format_author_html('td', \%co,15,3) ."<td>";4612# originally git_history used chop_str($co{'title'}, 50)4613print format_subject_html($co{'title'},$co{'title_short'},4614 href(action=>"commit", hash=>$commit),$ref);4615print"</td>\n".4616"<td class=\"link\">".4617$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4618$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");46194620if($ftypeeq'blob') {4621my$blob_current=$file_hash;4622my$blob_parent= git_get_hash_by_path($commit,$file_name);4623if(defined$blob_current&&defined$blob_parent&&4624$blob_currentne$blob_parent) {4625print" | ".4626$cgi->a({-href => href(action=>"blobdiff",4627 hash=>$blob_current, hash_parent=>$blob_parent,4628 hash_base=>$hash_base, hash_parent_base=>$commit,4629 file_name=>$file_name)},4630"diff to current");4631}4632}4633print"</td>\n".4634"</tr>\n";4635}4636if(defined$extra) {4637print"<tr>\n".4638"<td colspan=\"4\">$extra</td>\n".4639"</tr>\n";4640}4641print"</table>\n";4642}46434644sub git_tags_body {4645# uses global variable $project4646my($taglist,$from,$to,$extra) =@_;4647$from=0unlessdefined$from;4648$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);46494650print"<table class=\"tags\">\n";4651my$alternate=1;4652for(my$i=$from;$i<=$to;$i++) {4653my$entry=$taglist->[$i];4654my%tag=%$entry;4655my$comment=$tag{'subject'};4656my$comment_short;4657if(defined$comment) {4658$comment_short= chop_str($comment,30,5);4659}4660if($alternate) {4661print"<tr class=\"dark\">\n";4662}else{4663print"<tr class=\"light\">\n";4664}4665$alternate^=1;4666if(defined$tag{'age'}) {4667print"<td><i>$tag{'age'}</i></td>\n";4668}else{4669print"<td></td>\n";4670}4671print"<td>".4672$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4673-class=>"list name"}, esc_html($tag{'name'})) .4674"</td>\n".4675"<td>";4676if(defined$comment) {4677print format_subject_html($comment,$comment_short,4678 href(action=>"tag", hash=>$tag{'id'}));4679}4680print"</td>\n".4681"<td class=\"selflink\">";4682if($tag{'type'}eq"tag") {4683print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4684}else{4685print" ";4686}4687print"</td>\n".4688"<td class=\"link\">"." | ".4689$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4690if($tag{'reftype'}eq"commit") {4691print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4692" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4693}elsif($tag{'reftype'}eq"blob") {4694print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4695}4696print"</td>\n".4697"</tr>";4698}4699if(defined$extra) {4700print"<tr>\n".4701"<td colspan=\"5\">$extra</td>\n".4702"</tr>\n";4703}4704print"</table>\n";4705}47064707sub git_heads_body {4708# uses global variable $project4709my($headlist,$head,$from,$to,$extra) =@_;4710$from=0unlessdefined$from;4711$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);47124713print"<table class=\"heads\">\n";4714my$alternate=1;4715for(my$i=$from;$i<=$to;$i++) {4716my$entry=$headlist->[$i];4717my%ref=%$entry;4718my$curr=$ref{'id'}eq$head;4719if($alternate) {4720print"<tr class=\"dark\">\n";4721}else{4722print"<tr class=\"light\">\n";4723}4724$alternate^=1;4725print"<td><i>$ref{'age'}</i></td>\n".4726($curr?"<td class=\"current_head\">":"<td>") .4727$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4728-class=>"list name"},esc_html($ref{'name'})) .4729"</td>\n".4730"<td class=\"link\">".4731$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4732$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4733$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4734"</td>\n".4735"</tr>";4736}4737if(defined$extra) {4738print"<tr>\n".4739"<td colspan=\"3\">$extra</td>\n".4740"</tr>\n";4741}4742print"</table>\n";4743}47444745sub git_search_grep_body {4746my($commitlist,$from,$to,$extra) =@_;4747$from=0unlessdefined$from;4748$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47494750print"<table class=\"commit_search\">\n";4751my$alternate=1;4752for(my$i=$from;$i<=$to;$i++) {4753my%co= %{$commitlist->[$i]};4754if(!%co) {4755next;4756}4757my$commit=$co{'id'};4758if($alternate) {4759print"<tr class=\"dark\">\n";4760}else{4761print"<tr class=\"light\">\n";4762}4763$alternate^=1;4764print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4765 format_author_html('td', \%co,15,5) .4766"<td>".4767$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4768-class=>"list subject"},4769 chop_and_escape_str($co{'title'},50) ."<br/>");4770my$comment=$co{'comment'};4771foreachmy$line(@$comment) {4772if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4773my($lead,$match,$trail) = ($1,$2,$3);4774$match= chop_str($match,70,5,'center');4775my$contextlen=int((80-length($match))/2);4776$contextlen=30if($contextlen>30);4777$lead= chop_str($lead,$contextlen,10,'left');4778$trail= chop_str($trail,$contextlen,10,'right');47794780$lead= esc_html($lead);4781$match= esc_html($match);4782$trail= esc_html($trail);47834784print"$lead<span class=\"match\">$match</span>$trail<br />";4785}4786}4787print"</td>\n".4788"<td class=\"link\">".4789$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4790" | ".4791$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4792" | ".4793$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4794print"</td>\n".4795"</tr>\n";4796}4797if(defined$extra) {4798print"<tr>\n".4799"<td colspan=\"3\">$extra</td>\n".4800"</tr>\n";4801}4802print"</table>\n";4803}48044805## ======================================================================4806## ======================================================================4807## actions48084809sub git_project_list {4810my$order=$input_params{'order'};4811if(defined$order&&$order!~m/none|project|descr|owner|age/) {4812 die_error(400,"Unknown order parameter");4813}48144815my@list= git_get_projects_list();4816if(!@list) {4817 die_error(404,"No projects found");4818}48194820 git_header_html();4821if(defined$home_text&& -f $home_text) {4822print"<div class=\"index_include\">\n";4823 insert_file($home_text);4824print"</div>\n";4825}4826print$cgi->startform(-method=>"get") .4827"<p class=\"projsearch\">Search:\n".4828$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4829"</p>".4830$cgi->end_form() ."\n";4831 git_project_list_body(\@list,$order);4832 git_footer_html();4833}48344835sub git_forks {4836my$order=$input_params{'order'};4837if(defined$order&&$order!~m/none|project|descr|owner|age/) {4838 die_error(400,"Unknown order parameter");4839}48404841my@list= git_get_projects_list($project);4842if(!@list) {4843 die_error(404,"No forks found");4844}48454846 git_header_html();4847 git_print_page_nav('','');4848 git_print_header_div('summary',"$projectforks");4849 git_project_list_body(\@list,$order);4850 git_footer_html();4851}48524853sub git_project_index {4854my@projects= git_get_projects_list($project);48554856print$cgi->header(4857-type =>'text/plain',4858-charset =>'utf-8',4859-content_disposition =>'inline; filename="index.aux"');48604861foreachmy$pr(@projects) {4862if(!exists$pr->{'owner'}) {4863$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4864}48654866my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4867# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4868$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4869$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4870$path=~s/ /\+/g;4871$owner=~s/ /\+/g;48724873print"$path$owner\n";4874}4875}48764877sub git_summary {4878my$descr= git_get_project_description($project) ||"none";4879my%co= parse_commit("HEAD");4880my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4881my$head=$co{'id'};48824883my$owner= git_get_project_owner($project);48844885my$refs= git_get_references();4886# These get_*_list functions return one more to allow us to see if4887# there are more ...4888my@taglist= git_get_tags_list(16);4889my@headlist= git_get_heads_list(16);4890my@forklist;4891my$check_forks= gitweb_check_feature('forks');48924893if($check_forks) {4894@forklist= git_get_projects_list($project);4895}48964897 git_header_html();4898 git_print_page_nav('summary','',$head);48994900print"<div class=\"title\"> </div>\n";4901print"<table class=\"projects_list\">\n".4902"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4903"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4904if(defined$cd{'rfc2822'}) {4905print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4906}49074908# use per project git URL list in $projectroot/$project/cloneurl4909# or make project git URL from git base URL and project name4910my$url_tag="URL";4911my@url_list= git_get_project_url_list($project);4912@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4913foreachmy$git_url(@url_list) {4914next unless$git_url;4915print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4916$url_tag="";4917}49184919# Tag cloud4920my$show_ctags= gitweb_check_feature('ctags');4921if($show_ctags) {4922my$ctags= git_get_project_ctags($project);4923my$cloud= git_populate_project_tagcloud($ctags);4924print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4925print"</td>\n<td>"unless%$ctags;4926print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4927print"</td>\n<td>"if%$ctags;4928print git_show_project_tagcloud($cloud,48);4929print"</td></tr>";4930}49314932print"</table>\n";49334934# If XSS prevention is on, we don't include README.html.4935# TODO: Allow a readme in some safe format.4936if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4937print"<div class=\"title\">readme</div>\n".4938"<div class=\"readme\">\n";4939 insert_file("$projectroot/$project/README.html");4940print"\n</div>\n";# class="readme"4941}49424943# we need to request one more than 16 (0..15) to check if4944# those 16 are all4945my@commitlist=$head? parse_commits($head,17) : ();4946if(@commitlist) {4947 git_print_header_div('shortlog');4948 git_shortlog_body(\@commitlist,0,15,$refs,4949$#commitlist<=15?undef:4950$cgi->a({-href => href(action=>"shortlog")},"..."));4951}49524953if(@taglist) {4954 git_print_header_div('tags');4955 git_tags_body(\@taglist,0,15,4956$#taglist<=15?undef:4957$cgi->a({-href => href(action=>"tags")},"..."));4958}49594960if(@headlist) {4961 git_print_header_div('heads');4962 git_heads_body(\@headlist,$head,0,15,4963$#headlist<=15?undef:4964$cgi->a({-href => href(action=>"heads")},"..."));4965}49664967if(@forklist) {4968 git_print_header_div('forks');4969 git_project_list_body(\@forklist,'age',0,15,4970$#forklist<=15?undef:4971$cgi->a({-href => href(action=>"forks")},"..."),4972'no_header');4973}49744975 git_footer_html();4976}49774978sub git_tag {4979my$head= git_get_head_hash($project);4980 git_header_html();4981 git_print_page_nav('','',$head,undef,$head);4982my%tag= parse_tag($hash);49834984if(!%tag) {4985 die_error(404,"Unknown tag object");4986}49874988 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4989print"<div class=\"title_text\">\n".4990"<table class=\"object_header\">\n".4991"<tr>\n".4992"<td>object</td>\n".4993"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4994$tag{'object'}) ."</td>\n".4995"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4996$tag{'type'}) ."</td>\n".4997"</tr>\n";4998if(defined($tag{'author'})) {4999 git_print_authorship_rows(\%tag,'author');5000}5001print"</table>\n\n".5002"</div>\n";5003print"<div class=\"page_body\">";5004my$comment=$tag{'comment'};5005foreachmy$line(@$comment) {5006chomp$line;5007print esc_html($line, -nbsp=>1) ."<br/>\n";5008}5009print"</div>\n";5010 git_footer_html();5011}50125013sub git_blame_common {5014my$format=shift||'porcelain';5015if($formateq'porcelain'&&$cgi->param('js')) {5016$format='incremental';5017$action='blame_incremental';# for page title etc5018}50195020# permissions5021 gitweb_check_feature('blame')5022or die_error(403,"Blame view not allowed");50235024# error checking5025 die_error(400,"No file name given")unless$file_name;5026$hash_base||= git_get_head_hash($project);5027 die_error(404,"Couldn't find base commit")unless$hash_base;5028my%co= parse_commit($hash_base)5029or die_error(404,"Commit not found");5030my$ftype="blob";5031if(!defined$hash) {5032$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5033or die_error(404,"Error looking up file");5034}else{5035$ftype= git_get_type($hash);5036if($ftype!~"blob") {5037 die_error(400,"Object is not a blob");5038}5039}50405041my$fd;5042if($formateq'incremental') {5043# get file contents (as base)5044open$fd,"-|", git_cmd(),'cat-file','blob',$hash5045or die_error(500,"Open git-cat-file failed");5046}elsif($formateq'data') {5047# run git-blame --incremental5048open$fd,"-|", git_cmd(),"blame","--incremental",5049$hash_base,"--",$file_name5050or die_error(500,"Open git-blame --incremental failed");5051}else{5052# run git-blame --porcelain5053open$fd,"-|", git_cmd(),"blame",'-p',5054$hash_base,'--',$file_name5055or die_error(500,"Open git-blame --porcelain failed");5056}50575058# incremental blame data returns early5059if($formateq'data') {5060print$cgi->header(5061-type=>"text/plain", -charset =>"utf-8",5062-status=>"200 OK");5063local$| =1;# output autoflush5064printwhile<$fd>;5065close$fd5066or print"ERROR$!\n";50675068print'END';5069if(defined$t0&& gitweb_check_feature('timed')) {5070print' '.5071 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5072' '.$number_of_git_cmds;5073}5074print"\n";50755076return;5077}50785079# page header5080 git_header_html();5081my$formats_nav=5082$cgi->a({-href => href(action=>"blob", -replay=>1)},5083"blob") .5084" | ";5085if($formateq'incremental') {5086$formats_nav.=5087$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5088"blame") ." (non-incremental)";5089}else{5090$formats_nav.=5091$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5092"blame") ." (incremental)";5093}5094$formats_nav.=5095" | ".5096$cgi->a({-href => href(action=>"history", -replay=>1)},5097"history") .5098" | ".5099$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5100"HEAD");5101 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5102 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5103 git_print_page_path($file_name,$ftype,$hash_base);51045105# page body5106if($formateq'incremental') {5107print"<noscript>\n<div class=\"error\"><center><b>\n".5108"This page requires JavaScript to run.\nUse ".5109$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5110'this page').5111" instead.\n".5112"</b></center></div>\n</noscript>\n";51135114print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5115}51165117print qq!<div class="page_body">\n!;5118print qq!<div id="progress_info">.../ ...</div>\n!5119if($formateq'incremental');5120print qq!<table id="blame_table"class="blame" width="100%">\n!.5121#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5122 qq!<thead>\n!.5123 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5124 qq!</thead>\n!.5125 qq!<tbody>\n!;51265127my@rev_color=qw(light dark);5128my$num_colors=scalar(@rev_color);5129my$current_color=0;51305131if($formateq'incremental') {5132my$color_class=$rev_color[$current_color];51335134#contents of a file5135my$linenr=0;5136 LINE:5137while(my$line= <$fd>) {5138chomp$line;5139$linenr++;51405141print qq!<tr id="l$linenr"class="$color_class">!.5142 qq!<td class="sha1"><a href=""> </a></td>!.5143 qq!<td class="linenr">!.5144 qq!<a class="linenr" href="">$linenr</a></td>!;5145print qq!<td class="pre">! . esc_html($line) ."</td>\n";5146print qq!</tr>\n!;5147}51485149}else{# porcelain, i.e. ordinary blame5150my%metainfo= ();# saves information about commits51515152# blame data5153 LINE:5154while(my$line= <$fd>) {5155chomp$line;5156# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5157# no <lines in group> for subsequent lines in group of lines5158my($full_rev,$orig_lineno,$lineno,$group_size) =5159($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5160if(!exists$metainfo{$full_rev}) {5161$metainfo{$full_rev} = {'nprevious'=>0};5162}5163my$meta=$metainfo{$full_rev};5164my$data;5165while($data= <$fd>) {5166chomp$data;5167last if($data=~s/^\t//);# contents of line5168if($data=~/^(\S+)(?: (.*))?$/) {5169$meta->{$1} =$2unlessexists$meta->{$1};5170}5171if($data=~/^previous /) {5172$meta->{'nprevious'}++;5173}5174}5175my$short_rev=substr($full_rev,0,8);5176my$author=$meta->{'author'};5177my%date=5178 parse_date($meta->{'author-time'},$meta->{'author-tz'});5179my$date=$date{'iso-tz'};5180if($group_size) {5181$current_color= ($current_color+1) %$num_colors;5182}5183my$tr_class=$rev_color[$current_color];5184$tr_class.=' boundary'if(exists$meta->{'boundary'});5185$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5186$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5187print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5188if($group_size) {5189print"<td class=\"sha1\"";5190print" title=\"". esc_html($author) .",$date\"";5191print" rowspan=\"$group_size\""if($group_size>1);5192print">";5193print$cgi->a({-href => href(action=>"commit",5194 hash=>$full_rev,5195 file_name=>$file_name)},5196 esc_html($short_rev));5197if($group_size>=2) {5198my@author_initials= ($author=~/\b([[:upper:]])\B/g);5199if(@author_initials) {5200print"<br />".5201 esc_html(join('',@author_initials));5202# or join('.', ...)5203}5204}5205print"</td>\n";5206}5207# 'previous' <sha1 of parent commit> <filename at commit>5208if(exists$meta->{'previous'} &&5209$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5210$meta->{'parent'} =$1;5211$meta->{'file_parent'} = unquote($2);5212}5213my$linenr_commit=5214exists($meta->{'parent'}) ?5215$meta->{'parent'} :$full_rev;5216my$linenr_filename=5217exists($meta->{'file_parent'}) ?5218$meta->{'file_parent'} : unquote($meta->{'filename'});5219my$blamed= href(action =>'blame',5220 file_name =>$linenr_filename,5221 hash_base =>$linenr_commit);5222print"<td class=\"linenr\">";5223print$cgi->a({ -href =>"$blamed#l$orig_lineno",5224-class=>"linenr"},5225 esc_html($lineno));5226print"</td>";5227print"<td class=\"pre\">". esc_html($data) ."</td>\n";5228print"</tr>\n";5229}# end while52305231}52325233# footer5234print"</tbody>\n".5235"</table>\n";# class="blame"5236print"</div>\n";# class="blame_body"5237close$fd5238or print"Reading blob failed\n";52395240 git_footer_html();5241}52425243sub git_blame {5244 git_blame_common();5245}52465247sub git_blame_incremental {5248 git_blame_common('incremental');5249}52505251sub git_blame_data {5252 git_blame_common('data');5253}52545255sub git_tags {5256my$head= git_get_head_hash($project);5257 git_header_html();5258 git_print_page_nav('','',$head,undef,$head);5259 git_print_header_div('summary',$project);52605261my@tagslist= git_get_tags_list();5262if(@tagslist) {5263 git_tags_body(\@tagslist);5264}5265 git_footer_html();5266}52675268sub git_heads {5269my$head= git_get_head_hash($project);5270 git_header_html();5271 git_print_page_nav('','',$head,undef,$head);5272 git_print_header_div('summary',$project);52735274my@headslist= git_get_heads_list();5275if(@headslist) {5276 git_heads_body(\@headslist,$head);5277}5278 git_footer_html();5279}52805281sub git_blob_plain {5282my$type=shift;5283my$expires;52845285if(!defined$hash) {5286if(defined$file_name) {5287my$base=$hash_base|| git_get_head_hash($project);5288$hash= git_get_hash_by_path($base,$file_name,"blob")5289or die_error(404,"Cannot find file");5290}else{5291 die_error(400,"No file name defined");5292}5293}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5294# blobs defined by non-textual hash id's can be cached5295$expires="+1d";5296}52975298open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5299or die_error(500,"Open git-cat-file blob '$hash' failed");53005301# content-type (can include charset)5302$type= blob_contenttype($fd,$file_name,$type);53035304# "save as" filename, even when no $file_name is given5305my$save_as="$hash";5306if(defined$file_name) {5307$save_as=$file_name;5308}elsif($type=~m/^text\//) {5309$save_as.='.txt';5310}53115312# With XSS prevention on, blobs of all types except a few known safe5313# ones are served with "Content-Disposition: attachment" to make sure5314# they don't run in our security domain. For certain image types,5315# blob view writes an <img> tag referring to blob_plain view, and we5316# want to be sure not to break that by serving the image as an5317# attachment (though Firefox 3 doesn't seem to care).5318my$sandbox=$prevent_xss&&5319$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;53205321print$cgi->header(5322-type =>$type,5323-expires =>$expires,5324-content_disposition =>5325($sandbox?'attachment':'inline')5326.'; filename="'.$save_as.'"');5327local$/=undef;5328binmode STDOUT,':raw';5329print<$fd>;5330binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5331close$fd;5332}53335334sub git_blob {5335my$expires;53365337if(!defined$hash) {5338if(defined$file_name) {5339my$base=$hash_base|| git_get_head_hash($project);5340$hash= git_get_hash_by_path($base,$file_name,"blob")5341or die_error(404,"Cannot find file");5342}else{5343 die_error(400,"No file name defined");5344}5345}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5346# blobs defined by non-textual hash id's can be cached5347$expires="+1d";5348}53495350my$have_blame= gitweb_check_feature('blame');5351open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5352or die_error(500,"Couldn't cat$file_name,$hash");5353my$mimetype= blob_mimetype($fd,$file_name);5354if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5355close$fd;5356return git_blob_plain($mimetype);5357}5358# we can have blame only for text/* mimetype5359$have_blame&&= ($mimetype=~m!^text/!);53605361 git_header_html(undef,$expires);5362my$formats_nav='';5363if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5364if(defined$file_name) {5365if($have_blame) {5366$formats_nav.=5367$cgi->a({-href => href(action=>"blame", -replay=>1)},5368"blame") .5369" | ";5370}5371$formats_nav.=5372$cgi->a({-href => href(action=>"history", -replay=>1)},5373"history") .5374" | ".5375$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5376"raw") .5377" | ".5378$cgi->a({-href => href(action=>"blob",5379 hash_base=>"HEAD", file_name=>$file_name)},5380"HEAD");5381}else{5382$formats_nav.=5383$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5384"raw");5385}5386 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5387 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5388}else{5389print"<div class=\"page_nav\">\n".5390"<br/><br/></div>\n".5391"<div class=\"title\">$hash</div>\n";5392}5393 git_print_page_path($file_name,"blob",$hash_base);5394print"<div class=\"page_body\">\n";5395if($mimetype=~m!^image/!) {5396print qq!<img type="$mimetype"!;5397if($file_name) {5398print qq! alt="$file_name" title="$file_name"!;5399}5400print qq! src="! .5401 href(action=>"blob_plain", hash=>$hash,5402 hash_base=>$hash_base, file_name=>$file_name) .5403 qq!"/>\n!;5404}else{5405my$nr;5406while(my$line= <$fd>) {5407chomp$line;5408$nr++;5409$line= untabify($line);5410printf"<div class=\"pre\"><a id=\"l%i\"href=\"". href(-replay =>1)5411."#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5412$nr,$nr,$nr, esc_html($line, -nbsp=>1);5413}5414}5415close$fd5416or print"Reading blob failed.\n";5417print"</div>";5418 git_footer_html();5419}54205421sub git_tree {5422if(!defined$hash_base) {5423$hash_base="HEAD";5424}5425if(!defined$hash) {5426if(defined$file_name) {5427$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5428}else{5429$hash=$hash_base;5430}5431}5432 die_error(404,"No such tree")unlessdefined($hash);54335434my$show_sizes= gitweb_check_feature('show-sizes');5435my$have_blame= gitweb_check_feature('blame');54365437my@entries= ();5438{5439local$/="\0";5440open my$fd,"-|", git_cmd(),"ls-tree",'-z',5441($show_sizes?'-l': ()),@extra_options,$hash5442or die_error(500,"Open git-ls-tree failed");5443@entries=map{chomp;$_} <$fd>;5444close$fd5445or die_error(404,"Reading tree failed");5446}54475448my$refs= git_get_references();5449my$ref= format_ref_marker($refs,$hash_base);5450 git_header_html();5451my$basedir='';5452if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5453my@views_nav= ();5454if(defined$file_name) {5455push@views_nav,5456$cgi->a({-href => href(action=>"history", -replay=>1)},5457"history"),5458$cgi->a({-href => href(action=>"tree",5459 hash_base=>"HEAD", file_name=>$file_name)},5460"HEAD"),5461}5462my$snapshot_links= format_snapshot_links($hash);5463if(defined$snapshot_links) {5464# FIXME: Should be available when we have no hash base as well.5465push@views_nav,$snapshot_links;5466}5467 git_print_page_nav('tree','',$hash_base,undef,undef,5468join(' | ',@views_nav));5469 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5470}else{5471undef$hash_base;5472print"<div class=\"page_nav\">\n";5473print"<br/><br/></div>\n";5474print"<div class=\"title\">$hash</div>\n";5475}5476if(defined$file_name) {5477$basedir=$file_name;5478if($basedirne''&&substr($basedir, -1)ne'/') {5479$basedir.='/';5480}5481 git_print_page_path($file_name,'tree',$hash_base);5482}5483print"<div class=\"page_body\">\n";5484print"<table class=\"tree\">\n";5485my$alternate=1;5486# '..' (top directory) link if possible5487if(defined$hash_base&&5488defined$file_name&&$file_name=~m![^/]+$!) {5489if($alternate) {5490print"<tr class=\"dark\">\n";5491}else{5492print"<tr class=\"light\">\n";5493}5494$alternate^=1;54955496my$up=$file_name;5497$up=~s!/?[^/]+$!!;5498undef$upunless$up;5499# based on git_print_tree_entry5500print'<td class="mode">'. mode_str('040000') ."</td>\n";5501print'<td class="size"> </td>'."\n"if$show_sizes;5502print'<td class="list">';5503print$cgi->a({-href => href(action=>"tree",5504 hash_base=>$hash_base,5505 file_name=>$up)},5506"..");5507print"</td>\n";5508print"<td class=\"link\"></td>\n";55095510print"</tr>\n";5511}5512foreachmy$line(@entries) {5513my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);55145515if($alternate) {5516print"<tr class=\"dark\">\n";5517}else{5518print"<tr class=\"light\">\n";5519}5520$alternate^=1;55215522 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);55235524print"</tr>\n";5525}5526print"</table>\n".5527"</div>";5528 git_footer_html();5529}55305531sub snapshot_name {5532my($project,$hash) =@_;55335534# path/to/project.git -> project5535# path/to/project/.git -> project5536my$name= to_utf8($project);5537$name=~ s,([^/])/*\.git$,$1,;5538$name= basename($name);5539# sanitize name5540$name=~s/[[:cntrl:]]/?/g;55415542my$ver=$hash;5543if($hash=~/^[0-9a-fA-F]+$/) {5544# shorten SHA-1 hash5545my$full_hash= git_get_full_hash($project,$hash);5546if($full_hash=~/^$hash/&&length($hash) >7) {5547$ver= git_get_short_hash($project,$hash);5548}5549}elsif($hash=~m!^refs/tags/(.*)$!) {5550# tags don't need shortened SHA-1 hash5551$ver=$1;5552}else{5553# branches and other need shortened SHA-1 hash5554if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5555$ver=$1;5556}5557$ver.='-'. git_get_short_hash($project,$hash);5558}5559# in case of hierarchical branch names5560$ver=~s!/!.!g;55615562# name = project-version_string5563$name="$name-$ver";55645565returnwantarray? ($name,$name) :$name;5566}55675568sub git_snapshot {5569my$format=$input_params{'snapshot_format'};5570if(!@snapshot_fmts) {5571 die_error(403,"Snapshots not allowed");5572}5573# default to first supported snapshot format5574$format||=$snapshot_fmts[0];5575if($format!~m/^[a-z0-9]+$/) {5576 die_error(400,"Invalid snapshot format parameter");5577}elsif(!exists($known_snapshot_formats{$format})) {5578 die_error(400,"Unknown snapshot format");5579}elsif($known_snapshot_formats{$format}{'disabled'}) {5580 die_error(403,"Snapshot format not allowed");5581}elsif(!grep($_eq$format,@snapshot_fmts)) {5582 die_error(403,"Unsupported snapshot format");5583}55845585my$type= git_get_type("$hash^{}");5586if(!$type) {5587 die_error(404,'Object does not exist');5588}elsif($typeeq'blob') {5589 die_error(400,'Object is not a tree-ish');5590}55915592my($name,$prefix) = snapshot_name($project,$hash);5593my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5594my$cmd= quote_command(5595 git_cmd(),'archive',5596"--format=$known_snapshot_formats{$format}{'format'}",5597"--prefix=$prefix/",$hash);5598if(exists$known_snapshot_formats{$format}{'compressor'}) {5599$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5600}56015602$filename=~s/(["\\])/\\$1/g;5603print$cgi->header(5604-type =>$known_snapshot_formats{$format}{'type'},5605-content_disposition =>'inline; filename="'.$filename.'"',5606-status =>'200 OK');56075608open my$fd,"-|",$cmd5609or die_error(500,"Execute git-archive failed");5610binmode STDOUT,':raw';5611print<$fd>;5612binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5613close$fd;5614}56155616sub git_log_generic {5617my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;56185619my$head= git_get_head_hash($project);5620if(!defined$base) {5621$base=$head;5622}5623if(!defined$page) {5624$page=0;5625}5626my$refs= git_get_references();56275628my$commit_hash=$base;5629if(defined$parent) {5630$commit_hash="$parent..$base";5631}5632my@commitlist=5633 parse_commits($commit_hash,101, (100*$page),5634defined$file_name? ($file_name,"--full-history") : ());56355636my$ftype;5637if(!defined$file_hash&&defined$file_name) {5638# some commits could have deleted file in question,5639# and not have it in tree, but one of them has to have it5640for(my$i=0;$i<@commitlist;$i++) {5641$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5642last ifdefined$file_hash;5643}5644}5645if(defined$file_hash) {5646$ftype= git_get_type($file_hash);5647}5648if(defined$file_name&& !defined$ftype) {5649 die_error(500,"Unknown type of object");5650}5651my%co;5652if(defined$file_name) {5653%co= parse_commit($base)5654or die_error(404,"Unknown commit object");5655}565656575658my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5659my$next_link='';5660if($#commitlist>=100) {5661$next_link=5662$cgi->a({-href => href(-replay=>1, page=>$page+1),5663-accesskey =>"n", -title =>"Alt-n"},"next");5664}5665my$patch_max= gitweb_get_feature('patches');5666if($patch_max&& !defined$file_name) {5667if($patch_max<0||@commitlist<=$patch_max) {5668$paging_nav.=" ⋅ ".5669$cgi->a({-href => href(action=>"patches", -replay=>1)},5670"patches");5671}5672}56735674 git_header_html();5675 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5676if(defined$file_name) {5677 git_print_header_div('commit', esc_html($co{'title'}),$base);5678}else{5679 git_print_header_div('summary',$project)5680}5681 git_print_page_path($file_name,$ftype,$hash_base)5682if(defined$file_name);56835684$body_subr->(\@commitlist,0,99,$refs,$next_link,5685$file_name,$file_hash,$ftype);56865687 git_footer_html();5688}56895690sub git_log {5691 git_log_generic('log', \&git_log_body,5692$hash,$hash_parent);5693}56945695sub git_commit {5696$hash||=$hash_base||"HEAD";5697my%co= parse_commit($hash)5698or die_error(404,"Unknown commit object");56995700my$parent=$co{'parent'};5701my$parents=$co{'parents'};# listref57025703# we need to prepare $formats_nav before any parameter munging5704my$formats_nav;5705if(!defined$parent) {5706# --root commitdiff5707$formats_nav.='(initial)';5708}elsif(@$parents==1) {5709# single parent commit5710$formats_nav.=5711'(parent: '.5712$cgi->a({-href => href(action=>"commit",5713 hash=>$parent)},5714 esc_html(substr($parent,0,7))) .5715')';5716}else{5717# merge commit5718$formats_nav.=5719'(merge: '.5720join(' ',map{5721$cgi->a({-href => href(action=>"commit",5722 hash=>$_)},5723 esc_html(substr($_,0,7)));5724}@$parents) .5725')';5726}5727if(gitweb_check_feature('patches') &&@$parents<=1) {5728$formats_nav.=" | ".5729$cgi->a({-href => href(action=>"patch", -replay=>1)},5730"patch");5731}57325733if(!defined$parent) {5734$parent="--root";5735}5736my@difftree;5737open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5738@diff_opts,5739(@$parents<=1?$parent:'-c'),5740$hash,"--"5741or die_error(500,"Open git-diff-tree failed");5742@difftree=map{chomp;$_} <$fd>;5743close$fdor die_error(404,"Reading git-diff-tree failed");57445745# non-textual hash id's can be cached5746my$expires;5747if($hash=~m/^[0-9a-fA-F]{40}$/) {5748$expires="+1d";5749}5750my$refs= git_get_references();5751my$ref= format_ref_marker($refs,$co{'id'});57525753 git_header_html(undef,$expires);5754 git_print_page_nav('commit','',5755$hash,$co{'tree'},$hash,5756$formats_nav);57575758if(defined$co{'parent'}) {5759 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5760}else{5761 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5762}5763print"<div class=\"title_text\">\n".5764"<table class=\"object_header\">\n";5765 git_print_authorship_rows(\%co);5766print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5767print"<tr>".5768"<td>tree</td>".5769"<td class=\"sha1\">".5770$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5771class=>"list"},$co{'tree'}) .5772"</td>".5773"<td class=\"link\">".5774$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5775"tree");5776my$snapshot_links= format_snapshot_links($hash);5777if(defined$snapshot_links) {5778print" | ".$snapshot_links;5779}5780print"</td>".5781"</tr>\n";57825783foreachmy$par(@$parents) {5784print"<tr>".5785"<td>parent</td>".5786"<td class=\"sha1\">".5787$cgi->a({-href => href(action=>"commit", hash=>$par),5788class=>"list"},$par) .5789"</td>".5790"<td class=\"link\">".5791$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5792" | ".5793$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5794"</td>".5795"</tr>\n";5796}5797print"</table>".5798"</div>\n";57995800print"<div class=\"page_body\">\n";5801 git_print_log($co{'comment'});5802print"</div>\n";58035804 git_difftree_body(\@difftree,$hash,@$parents);58055806 git_footer_html();5807}58085809sub git_object {5810# object is defined by:5811# - hash or hash_base alone5812# - hash_base and file_name5813my$type;58145815# - hash or hash_base alone5816if($hash|| ($hash_base&& !defined$file_name)) {5817my$object_id=$hash||$hash_base;58185819open my$fd,"-|", quote_command(5820 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5821or die_error(404,"Object does not exist");5822$type= <$fd>;5823chomp$type;5824close$fd5825or die_error(404,"Object does not exist");58265827# - hash_base and file_name5828}elsif($hash_base&&defined$file_name) {5829$file_name=~ s,/+$,,;58305831system(git_cmd(),"cat-file",'-e',$hash_base) ==05832or die_error(404,"Base object does not exist");58335834# here errors should not hapen5835open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5836or die_error(500,"Open git-ls-tree failed");5837my$line= <$fd>;5838close$fd;58395840#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5841unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5842 die_error(404,"File or directory for given base does not exist");5843}5844$type=$2;5845$hash=$3;5846}else{5847 die_error(400,"Not enough information to find object");5848}58495850print$cgi->redirect(-uri => href(action=>$type, -full=>1,5851 hash=>$hash, hash_base=>$hash_base,5852 file_name=>$file_name),5853-status =>'302 Found');5854}58555856sub git_blobdiff {5857my$format=shift||'html';58585859my$fd;5860my@difftree;5861my%diffinfo;5862my$expires;58635864# preparing $fd and %diffinfo for git_patchset_body5865# new style URI5866if(defined$hash_base&&defined$hash_parent_base) {5867if(defined$file_name) {5868# read raw output5869open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5870$hash_parent_base,$hash_base,5871"--", (defined$file_parent?$file_parent: ()),$file_name5872or die_error(500,"Open git-diff-tree failed");5873@difftree=map{chomp;$_} <$fd>;5874close$fd5875or die_error(404,"Reading git-diff-tree failed");5876@difftree5877or die_error(404,"Blob diff not found");58785879}elsif(defined$hash&&5880$hash=~/[0-9a-fA-F]{40}/) {5881# try to find filename from $hash58825883# read filtered raw output5884open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5885$hash_parent_base,$hash_base,"--"5886or die_error(500,"Open git-diff-tree failed");5887@difftree=5888# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5889# $hash == to_id5890grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5891map{chomp;$_} <$fd>;5892close$fd5893or die_error(404,"Reading git-diff-tree failed");5894@difftree5895or die_error(404,"Blob diff not found");58965897}else{5898 die_error(400,"Missing one of the blob diff parameters");5899}59005901if(@difftree>1) {5902 die_error(400,"Ambiguous blob diff specification");5903}59045905%diffinfo= parse_difftree_raw_line($difftree[0]);5906$file_parent||=$diffinfo{'from_file'} ||$file_name;5907$file_name||=$diffinfo{'to_file'};59085909$hash_parent||=$diffinfo{'from_id'};5910$hash||=$diffinfo{'to_id'};59115912# non-textual hash id's can be cached5913if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5914$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5915$expires='+1d';5916}59175918# open patch output5919open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5920'-p', ($formateq'html'?"--full-index": ()),5921$hash_parent_base,$hash_base,5922"--", (defined$file_parent?$file_parent: ()),$file_name5923or die_error(500,"Open git-diff-tree failed");5924}59255926# old/legacy style URI -- not generated anymore since 1.4.3.5927if(!%diffinfo) {5928 die_error('404 Not Found',"Missing one of the blob diff parameters")5929}59305931# header5932if($formateq'html') {5933my$formats_nav=5934$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5935"raw");5936 git_header_html(undef,$expires);5937if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5938 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5939 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5940}else{5941print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5942print"<div class=\"title\">$hashvs$hash_parent</div>\n";5943}5944if(defined$file_name) {5945 git_print_page_path($file_name,"blob",$hash_base);5946}else{5947print"<div class=\"page_path\"></div>\n";5948}59495950}elsif($formateq'plain') {5951print$cgi->header(5952-type =>'text/plain',5953-charset =>'utf-8',5954-expires =>$expires,5955-content_disposition =>'inline; filename="'."$file_name".'.patch"');59565957print"X-Git-Url: ".$cgi->self_url() ."\n\n";59585959}else{5960 die_error(400,"Unknown blobdiff format");5961}59625963# patch5964if($formateq'html') {5965print"<div class=\"page_body\">\n";59665967 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5968close$fd;59695970print"</div>\n";# class="page_body"5971 git_footer_html();59725973}else{5974while(my$line= <$fd>) {5975$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5976$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;59775978print$line;59795980last if$line=~m!^\+\+\+!;5981}5982local$/=undef;5983print<$fd>;5984close$fd;5985}5986}59875988sub git_blobdiff_plain {5989 git_blobdiff('plain');5990}59915992sub git_commitdiff {5993my%params=@_;5994my$format=$params{-format} ||'html';59955996my($patch_max) = gitweb_get_feature('patches');5997if($formateq'patch') {5998 die_error(403,"Patch view not allowed")unless$patch_max;5999}60006001$hash||=$hash_base||"HEAD";6002my%co= parse_commit($hash)6003or die_error(404,"Unknown commit object");60046005# choose format for commitdiff for merge6006if(!defined$hash_parent&& @{$co{'parents'}} >1) {6007$hash_parent='--cc';6008}6009# we need to prepare $formats_nav before almost any parameter munging6010my$formats_nav;6011if($formateq'html') {6012$formats_nav=6013$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6014"raw");6015if($patch_max&& @{$co{'parents'}} <=1) {6016$formats_nav.=" | ".6017$cgi->a({-href => href(action=>"patch", -replay=>1)},6018"patch");6019}60206021if(defined$hash_parent&&6022$hash_parentne'-c'&&$hash_parentne'--cc') {6023# commitdiff with two commits given6024my$hash_parent_short=$hash_parent;6025if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6026$hash_parent_short=substr($hash_parent,0,7);6027}6028$formats_nav.=6029' (from';6030for(my$i=0;$i< @{$co{'parents'}};$i++) {6031if($co{'parents'}[$i]eq$hash_parent) {6032$formats_nav.=' parent '. ($i+1);6033last;6034}6035}6036$formats_nav.=': '.6037$cgi->a({-href => href(action=>"commitdiff",6038 hash=>$hash_parent)},6039 esc_html($hash_parent_short)) .6040')';6041}elsif(!$co{'parent'}) {6042# --root commitdiff6043$formats_nav.=' (initial)';6044}elsif(scalar@{$co{'parents'}} ==1) {6045# single parent commit6046$formats_nav.=6047' (parent: '.6048$cgi->a({-href => href(action=>"commitdiff",6049 hash=>$co{'parent'})},6050 esc_html(substr($co{'parent'},0,7))) .6051')';6052}else{6053# merge commit6054if($hash_parenteq'--cc') {6055$formats_nav.=' | '.6056$cgi->a({-href => href(action=>"commitdiff",6057 hash=>$hash, hash_parent=>'-c')},6058'combined');6059}else{# $hash_parent eq '-c'6060$formats_nav.=' | '.6061$cgi->a({-href => href(action=>"commitdiff",6062 hash=>$hash, hash_parent=>'--cc')},6063'compact');6064}6065$formats_nav.=6066' (merge: '.6067join(' ',map{6068$cgi->a({-href => href(action=>"commitdiff",6069 hash=>$_)},6070 esc_html(substr($_,0,7)));6071} @{$co{'parents'}} ) .6072')';6073}6074}60756076my$hash_parent_param=$hash_parent;6077if(!defined$hash_parent_param) {6078# --cc for multiple parents, --root for parentless6079$hash_parent_param=6080@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6081}60826083# read commitdiff6084my$fd;6085my@difftree;6086if($formateq'html') {6087open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6088"--no-commit-id","--patch-with-raw","--full-index",6089$hash_parent_param,$hash,"--"6090or die_error(500,"Open git-diff-tree failed");60916092while(my$line= <$fd>) {6093chomp$line;6094# empty line ends raw part of diff-tree output6095last unless$line;6096push@difftree,scalar parse_difftree_raw_line($line);6097}60986099}elsif($formateq'plain') {6100open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6101'-p',$hash_parent_param,$hash,"--"6102or die_error(500,"Open git-diff-tree failed");6103}elsif($formateq'patch') {6104# For commit ranges, we limit the output to the number of6105# patches specified in the 'patches' feature.6106# For single commits, we limit the output to a single patch,6107# diverging from the git-format-patch default.6108my@commit_spec= ();6109if($hash_parent) {6110if($patch_max>0) {6111push@commit_spec,"-$patch_max";6112}6113push@commit_spec,'-n',"$hash_parent..$hash";6114}else{6115if($params{-single}) {6116push@commit_spec,'-1';6117}else{6118if($patch_max>0) {6119push@commit_spec,"-$patch_max";6120}6121push@commit_spec,"-n";6122}6123push@commit_spec,'--root',$hash;6124}6125open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',6126'--stdout',@commit_spec6127or die_error(500,"Open git-format-patch failed");6128}else{6129 die_error(400,"Unknown commitdiff format");6130}61316132# non-textual hash id's can be cached6133my$expires;6134if($hash=~m/^[0-9a-fA-F]{40}$/) {6135$expires="+1d";6136}61376138# write commit message6139if($formateq'html') {6140my$refs= git_get_references();6141my$ref= format_ref_marker($refs,$co{'id'});61426143 git_header_html(undef,$expires);6144 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6145 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6146print"<div class=\"title_text\">\n".6147"<table class=\"object_header\">\n";6148 git_print_authorship_rows(\%co);6149print"</table>".6150"</div>\n";6151print"<div class=\"page_body\">\n";6152if(@{$co{'comment'}} >1) {6153print"<div class=\"log\">\n";6154 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6155print"</div>\n";# class="log"6156}61576158}elsif($formateq'plain') {6159my$refs= git_get_references("tags");6160my$tagname= git_get_rev_name_tags($hash);6161my$filename= basename($project) ."-$hash.patch";61626163print$cgi->header(6164-type =>'text/plain',6165-charset =>'utf-8',6166-expires =>$expires,6167-content_disposition =>'inline; filename="'."$filename".'"');6168my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6169print"From: ". to_utf8($co{'author'}) ."\n";6170print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6171print"Subject: ". to_utf8($co{'title'}) ."\n";61726173print"X-Git-Tag:$tagname\n"if$tagname;6174print"X-Git-Url: ".$cgi->self_url() ."\n\n";61756176foreachmy$line(@{$co{'comment'}}) {6177print to_utf8($line) ."\n";6178}6179print"---\n\n";6180}elsif($formateq'patch') {6181my$filename= basename($project) ."-$hash.patch";61826183print$cgi->header(6184-type =>'text/plain',6185-charset =>'utf-8',6186-expires =>$expires,6187-content_disposition =>'inline; filename="'."$filename".'"');6188}61896190# write patch6191if($formateq'html') {6192my$use_parents= !defined$hash_parent||6193$hash_parenteq'-c'||$hash_parenteq'--cc';6194 git_difftree_body(\@difftree,$hash,6195$use_parents? @{$co{'parents'}} :$hash_parent);6196print"<br/>\n";61976198 git_patchset_body($fd, \@difftree,$hash,6199$use_parents? @{$co{'parents'}} :$hash_parent);6200close$fd;6201print"</div>\n";# class="page_body"6202 git_footer_html();62036204}elsif($formateq'plain') {6205local$/=undef;6206print<$fd>;6207close$fd6208or print"Reading git-diff-tree failed\n";6209}elsif($formateq'patch') {6210local$/=undef;6211print<$fd>;6212close$fd6213or print"Reading git-format-patch failed\n";6214}6215}62166217sub git_commitdiff_plain {6218 git_commitdiff(-format =>'plain');6219}62206221# format-patch-style patches6222sub git_patch {6223 git_commitdiff(-format =>'patch', -single =>1);6224}62256226sub git_patches {6227 git_commitdiff(-format =>'patch');6228}62296230sub git_history {6231 git_log_generic('history', \&git_history_body,6232$hash_base,$hash_parent_base,6233$file_name,$hash);6234}62356236sub git_search {6237 gitweb_check_feature('search')or die_error(403,"Search is disabled");6238if(!defined$searchtext) {6239 die_error(400,"Text field is empty");6240}6241if(!defined$hash) {6242$hash= git_get_head_hash($project);6243}6244my%co= parse_commit($hash);6245if(!%co) {6246 die_error(404,"Unknown commit object");6247}6248if(!defined$page) {6249$page=0;6250}62516252$searchtype||='commit';6253if($searchtypeeq'pickaxe') {6254# pickaxe may take all resources of your box and run for several minutes6255# with every query - so decide by yourself how public you make this feature6256 gitweb_check_feature('pickaxe')6257or die_error(403,"Pickaxe is disabled");6258}6259if($searchtypeeq'grep') {6260 gitweb_check_feature('grep')6261or die_error(403,"Grep is disabled");6262}62636264 git_header_html();62656266if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6267my$greptype;6268if($searchtypeeq'commit') {6269$greptype="--grep=";6270}elsif($searchtypeeq'author') {6271$greptype="--author=";6272}elsif($searchtypeeq'committer') {6273$greptype="--committer=";6274}6275$greptype.=$searchtext;6276my@commitlist= parse_commits($hash,101, (100*$page),undef,6277$greptype,'--regexp-ignore-case',6278$search_use_regexp?'--extended-regexp':'--fixed-strings');62796280my$paging_nav='';6281if($page>0) {6282$paging_nav.=6283$cgi->a({-href => href(action=>"search", hash=>$hash,6284 searchtext=>$searchtext,6285 searchtype=>$searchtype)},6286"first");6287$paging_nav.=" ⋅ ".6288$cgi->a({-href => href(-replay=>1, page=>$page-1),6289-accesskey =>"p", -title =>"Alt-p"},"prev");6290}else{6291$paging_nav.="first";6292$paging_nav.=" ⋅ prev";6293}6294my$next_link='';6295if($#commitlist>=100) {6296$next_link=6297$cgi->a({-href => href(-replay=>1, page=>$page+1),6298-accesskey =>"n", -title =>"Alt-n"},"next");6299$paging_nav.=" ⋅$next_link";6300}else{6301$paging_nav.=" ⋅ next";6302}63036304if($#commitlist>=100) {6305}63066307 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6308 git_print_header_div('commit', esc_html($co{'title'}),$hash);6309 git_search_grep_body(\@commitlist,0,99,$next_link);6310}63116312if($searchtypeeq'pickaxe') {6313 git_print_page_nav('','',$hash,$co{'tree'},$hash);6314 git_print_header_div('commit', esc_html($co{'title'}),$hash);63156316print"<table class=\"pickaxe search\">\n";6317my$alternate=1;6318local$/="\n";6319open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6320'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6321($search_use_regexp?'--pickaxe-regex': ());6322undef%co;6323my@files;6324while(my$line= <$fd>) {6325chomp$line;6326next unless$line;63276328my%set= parse_difftree_raw_line($line);6329if(defined$set{'commit'}) {6330# finish previous commit6331if(%co) {6332print"</td>\n".6333"<td class=\"link\">".6334$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6335" | ".6336$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6337print"</td>\n".6338"</tr>\n";6339}63406341if($alternate) {6342print"<tr class=\"dark\">\n";6343}else{6344print"<tr class=\"light\">\n";6345}6346$alternate^=1;6347%co= parse_commit($set{'commit'});6348my$author= chop_and_escape_str($co{'author_name'},15,5);6349print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6350"<td><i>$author</i></td>\n".6351"<td>".6352$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6353-class=>"list subject"},6354 chop_and_escape_str($co{'title'},50) ."<br/>");6355}elsif(defined$set{'to_id'}) {6356next if($set{'to_id'} =~m/^0{40}$/);63576358print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6359 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6360-class=>"list"},6361"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6362"<br/>\n";6363}6364}6365close$fd;63666367# finish last commit (warning: repetition!)6368if(%co) {6369print"</td>\n".6370"<td class=\"link\">".6371$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6372" | ".6373$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6374print"</td>\n".6375"</tr>\n";6376}63776378print"</table>\n";6379}63806381if($searchtypeeq'grep') {6382 git_print_page_nav('','',$hash,$co{'tree'},$hash);6383 git_print_header_div('commit', esc_html($co{'title'}),$hash);63846385print"<table class=\"grep_search\">\n";6386my$alternate=1;6387my$matches=0;6388local$/="\n";6389open my$fd,"-|", git_cmd(),'grep','-n',6390$search_use_regexp? ('-E','-i') :'-F',6391$searchtext,$co{'tree'};6392my$lastfile='';6393while(my$line= <$fd>) {6394chomp$line;6395my($file,$lno,$ltext,$binary);6396last if($matches++>1000);6397if($line=~/^Binary file (.+) matches$/) {6398$file=$1;6399$binary=1;6400}else{6401(undef,$file,$lno,$ltext) =split(/:/,$line,4);6402}6403if($filene$lastfile) {6404$lastfileand print"</td></tr>\n";6405if($alternate++) {6406print"<tr class=\"dark\">\n";6407}else{6408print"<tr class=\"light\">\n";6409}6410print"<td class=\"list\">".6411$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6412 file_name=>"$file"),6413-class=>"list"}, esc_path($file));6414print"</td><td>\n";6415$lastfile=$file;6416}6417if($binary) {6418print"<div class=\"binary\">Binary file</div>\n";6419}else{6420$ltext= untabify($ltext);6421if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6422$ltext= esc_html($1, -nbsp=>1);6423$ltext.='<span class="match">';6424$ltext.= esc_html($2, -nbsp=>1);6425$ltext.='</span>';6426$ltext.= esc_html($3, -nbsp=>1);6427}else{6428$ltext= esc_html($ltext, -nbsp=>1);6429}6430print"<div class=\"pre\">".6431$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6432 file_name=>"$file").'#l'.$lno,6433-class=>"linenr"},sprintf('%4i',$lno))6434.' '.$ltext."</div>\n";6435}6436}6437if($lastfile) {6438print"</td></tr>\n";6439if($matches>1000) {6440print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6441}6442}else{6443print"<div class=\"diff nodifferences\">No matches found</div>\n";6444}6445close$fd;64466447print"</table>\n";6448}6449 git_footer_html();6450}64516452sub git_search_help {6453 git_header_html();6454 git_print_page_nav('','',$hash,$hash,$hash);6455print<<EOT;6456<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6457regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6458the pattern entered is recognized as the POSIX extended6459<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6460insensitive).</p>6461<dl>6462<dt><b>commit</b></dt>6463<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6464EOT6465my$have_grep= gitweb_check_feature('grep');6466if($have_grep) {6467print<<EOT;6468<dt><b>grep</b></dt>6469<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6470 a different one) are searched for the given pattern. On large trees, this search can take6471a while and put some strain on the server, so please use it with some consideration. Note that6472due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6473case-sensitive.</dd>6474EOT6475}6476print<<EOT;6477<dt><b>author</b></dt>6478<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6479<dt><b>committer</b></dt>6480<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6481EOT6482my$have_pickaxe= gitweb_check_feature('pickaxe');6483if($have_pickaxe) {6484print<<EOT;6485<dt><b>pickaxe</b></dt>6486<dd>All commits that caused the string to appear or disappear from any file (changes that6487added, removed or "modified" the string) will be listed. This search can take a while and6488takes a lot of strain on the server, so please use it wisely. Note that since you may be6489interested even in changes just changing the case as well, this search is case sensitive.</dd>6490EOT6491}6492print"</dl>\n";6493 git_footer_html();6494}64956496sub git_shortlog {6497 git_log_generic('shortlog', \&git_shortlog_body,6498$hash,$hash_parent);6499}65006501## ......................................................................6502## feeds (RSS, Atom; OPML)65036504sub git_feed {6505my$format=shift||'atom';6506my$have_blame= gitweb_check_feature('blame');65076508# Atom: http://www.atomenabled.org/developers/syndication/6509# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6510if($formatne'rss'&&$formatne'atom') {6511 die_error(400,"Unknown web feed format");6512}65136514# log/feed of current (HEAD) branch, log of given branch, history of file/directory6515my$head=$hash||'HEAD';6516my@commitlist= parse_commits($head,150,0,$file_name);65176518my%latest_commit;6519my%latest_date;6520my$content_type="application/$format+xml";6521if(defined$cgi->http('HTTP_ACCEPT') &&6522$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6523# browser (feed reader) prefers text/xml6524$content_type='text/xml';6525}6526if(defined($commitlist[0])) {6527%latest_commit= %{$commitlist[0]};6528my$latest_epoch=$latest_commit{'committer_epoch'};6529%latest_date= parse_date($latest_epoch);6530my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6531if(defined$if_modified) {6532my$since;6533if(eval{require HTTP::Date;1; }) {6534$since= HTTP::Date::str2time($if_modified);6535}elsif(eval{require Time::ParseDate;1; }) {6536$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6537}6538if(defined$since&&$latest_epoch<=$since) {6539print$cgi->header(6540-type =>$content_type,6541-charset =>'utf-8',6542-last_modified =>$latest_date{'rfc2822'},6543-status =>'304 Not Modified');6544return;6545}6546}6547print$cgi->header(6548-type =>$content_type,6549-charset =>'utf-8',6550-last_modified =>$latest_date{'rfc2822'});6551}else{6552print$cgi->header(6553-type =>$content_type,6554-charset =>'utf-8');6555}65566557# Optimization: skip generating the body if client asks only6558# for Last-Modified date.6559return if($cgi->request_method()eq'HEAD');65606561# header variables6562my$title="$site_name-$project/$action";6563my$feed_type='log';6564if(defined$hash) {6565$title.=" - '$hash'";6566$feed_type='branch log';6567if(defined$file_name) {6568$title.=" ::$file_name";6569$feed_type='history';6570}6571}elsif(defined$file_name) {6572$title.=" -$file_name";6573$feed_type='history';6574}6575$title.="$feed_type";6576my$descr= git_get_project_description($project);6577if(defined$descr) {6578$descr= esc_html($descr);6579}else{6580$descr="$project".6581($formateq'rss'?'RSS':'Atom') .6582" feed";6583}6584my$owner= git_get_project_owner($project);6585$owner= esc_html($owner);65866587#header6588my$alt_url;6589if(defined$file_name) {6590$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6591}elsif(defined$hash) {6592$alt_url= href(-full=>1, action=>"log", hash=>$hash);6593}else{6594$alt_url= href(-full=>1, action=>"summary");6595}6596print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6597if($formateq'rss') {6598print<<XML;6599<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6600<channel>6601XML6602print"<title>$title</title>\n".6603"<link>$alt_url</link>\n".6604"<description>$descr</description>\n".6605"<language>en</language>\n".6606# project owner is responsible for 'editorial' content6607"<managingEditor>$owner</managingEditor>\n";6608if(defined$logo||defined$favicon) {6609# prefer the logo to the favicon, since RSS6610# doesn't allow both6611my$img= esc_url($logo||$favicon);6612print"<image>\n".6613"<url>$img</url>\n".6614"<title>$title</title>\n".6615"<link>$alt_url</link>\n".6616"</image>\n";6617}6618if(%latest_date) {6619print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6620print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6621}6622print"<generator>gitweb v.$version/$git_version</generator>\n";6623}elsif($formateq'atom') {6624print<<XML;6625<feed xmlns="http://www.w3.org/2005/Atom">6626XML6627print"<title>$title</title>\n".6628"<subtitle>$descr</subtitle>\n".6629'<link rel="alternate" type="text/html" href="'.6630$alt_url.'" />'."\n".6631'<link rel="self" type="'.$content_type.'" href="'.6632$cgi->self_url() .'" />'."\n".6633"<id>". href(-full=>1) ."</id>\n".6634# use project owner for feed author6635"<author><name>$owner</name></author>\n";6636if(defined$favicon) {6637print"<icon>". esc_url($favicon) ."</icon>\n";6638}6639if(defined$logo_url) {6640# not twice as wide as tall: 72 x 27 pixels6641print"<logo>". esc_url($logo) ."</logo>\n";6642}6643if(!%latest_date) {6644# dummy date to keep the feed valid until commits trickle in:6645print"<updated>1970-01-01T00:00:00Z</updated>\n";6646}else{6647print"<updated>$latest_date{'iso-8601'}</updated>\n";6648}6649print"<generator version='$version/$git_version'>gitweb</generator>\n";6650}66516652# contents6653for(my$i=0;$i<=$#commitlist;$i++) {6654my%co= %{$commitlist[$i]};6655my$commit=$co{'id'};6656# we read 150, we always show 30 and the ones more recent than 48 hours6657if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6658last;6659}6660my%cd= parse_date($co{'author_epoch'});66616662# get list of changed files6663open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6664$co{'parent'} ||"--root",6665$co{'id'},"--", (defined$file_name?$file_name: ())6666ornext;6667my@difftree=map{chomp;$_} <$fd>;6668close$fd6669ornext;66706671# print element (entry, item)6672my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6673if($formateq'rss') {6674print"<item>\n".6675"<title>". esc_html($co{'title'}) ."</title>\n".6676"<author>". esc_html($co{'author'}) ."</author>\n".6677"<pubDate>$cd{'rfc2822'}</pubDate>\n".6678"<guid isPermaLink=\"true\">$co_url</guid>\n".6679"<link>$co_url</link>\n".6680"<description>". esc_html($co{'title'}) ."</description>\n".6681"<content:encoded>".6682"<![CDATA[\n";6683}elsif($formateq'atom') {6684print"<entry>\n".6685"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6686"<updated>$cd{'iso-8601'}</updated>\n".6687"<author>\n".6688" <name>". esc_html($co{'author_name'}) ."</name>\n";6689if($co{'author_email'}) {6690print" <email>". esc_html($co{'author_email'}) ."</email>\n";6691}6692print"</author>\n".6693# use committer for contributor6694"<contributor>\n".6695" <name>". esc_html($co{'committer_name'}) ."</name>\n";6696if($co{'committer_email'}) {6697print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6698}6699print"</contributor>\n".6700"<published>$cd{'iso-8601'}</published>\n".6701"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6702"<id>$co_url</id>\n".6703"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6704"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6705}6706my$comment=$co{'comment'};6707print"<pre>\n";6708foreachmy$line(@$comment) {6709$line= esc_html($line);6710print"$line\n";6711}6712print"</pre><ul>\n";6713foreachmy$difftree_line(@difftree) {6714my%difftree= parse_difftree_raw_line($difftree_line);6715next if!$difftree{'from_id'};67166717my$file=$difftree{'file'} ||$difftree{'to_file'};67186719print"<li>".6720"[".6721$cgi->a({-href => href(-full=>1, action=>"blobdiff",6722 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6723 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6724 file_name=>$file, file_parent=>$difftree{'from_file'}),6725-title =>"diff"},'D');6726if($have_blame) {6727print$cgi->a({-href => href(-full=>1, action=>"blame",6728 file_name=>$file, hash_base=>$commit),6729-title =>"blame"},'B');6730}6731# if this is not a feed of a file history6732if(!defined$file_name||$file_namene$file) {6733print$cgi->a({-href => href(-full=>1, action=>"history",6734 file_name=>$file, hash=>$commit),6735-title =>"history"},'H');6736}6737$file= esc_path($file);6738print"] ".6739"$file</li>\n";6740}6741if($formateq'rss') {6742print"</ul>]]>\n".6743"</content:encoded>\n".6744"</item>\n";6745}elsif($formateq'atom') {6746print"</ul>\n</div>\n".6747"</content>\n".6748"</entry>\n";6749}6750}67516752# end of feed6753if($formateq'rss') {6754print"</channel>\n</rss>\n";6755}elsif($formateq'atom') {6756print"</feed>\n";6757}6758}67596760sub git_rss {6761 git_feed('rss');6762}67636764sub git_atom {6765 git_feed('atom');6766}67676768sub git_opml {6769my@list= git_get_projects_list();67706771print$cgi->header(6772-type =>'text/xml',6773-charset =>'utf-8',6774-content_disposition =>'inline; filename="opml.xml"');67756776print<<XML;6777<?xml version="1.0" encoding="utf-8"?>6778<opml version="1.0">6779<head>6780 <title>$site_nameOPML Export</title>6781</head>6782<body>6783<outline text="git RSS feeds">6784XML67856786foreachmy$pr(@list) {6787my%proj=%$pr;6788my$head= git_get_head_hash($proj{'path'});6789if(!defined$head) {6790next;6791}6792$git_dir="$projectroot/$proj{'path'}";6793my%co= parse_commit($head);6794if(!%co) {6795next;6796}67976798my$path= esc_html(chop_str($proj{'path'},25,5));6799my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6800my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6801print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6802}6803print<<XML;6804</outline>6805</body>6806</opml>6807XML6808}