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}->(); 975DONE_GITWEB: 9761; 977 978## ====================================================================== 979## action links 980 981# possible values of extra options 982# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base) 983# -replay => 1 - start from a current view (replay with modifications) 984# -path_info => 0|1 - don't use/use path_info URL (if possible) 985sub href { 986my%params=@_; 987# default is to use -absolute url() i.e. $my_uri 988my$href=$params{-full} ?$my_url:$my_uri; 989 990$params{'project'} =$projectunlessexists$params{'project'}; 991 992if($params{-replay}) { 993while(my($name,$symbol) =each%cgi_param_mapping) { 994if(!exists$params{$name}) { 995$params{$name} =$input_params{$name}; 996} 997} 998} 9991000my$use_pathinfo= gitweb_check_feature('pathinfo');1001if(defined$params{'project'} &&1002(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1003# try to put as many parameters as possible in PATH_INFO:1004# - project name1005# - action1006# - hash_parent or hash_parent_base:/file_parent1007# - hash or hash_base:/filename1008# - the snapshot_format as an appropriate suffix10091010# When the script is the root DirectoryIndex for the domain,1011# $href here would be something like http://gitweb.example.com/1012# Thus, we strip any trailing / from $href, to spare us double1013# slashes in the final URL1014$href=~ s,/$,,;10151016# Then add the project name, if present1017$href.="/".esc_url($params{'project'});1018delete$params{'project'};10191020# since we destructively absorb parameters, we keep this1021# boolean that remembers if we're handling a snapshot1022my$is_snapshot=$params{'action'}eq'snapshot';10231024# Summary just uses the project path URL, any other action is1025# added to the URL1026if(defined$params{'action'}) {1027$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1028delete$params{'action'};1029}10301031# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1032# stripping nonexistent or useless pieces1033$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1034||$params{'hash_parent'} ||$params{'hash'});1035if(defined$params{'hash_base'}) {1036if(defined$params{'hash_parent_base'}) {1037$href.= esc_url($params{'hash_parent_base'});1038# skip the file_parent if it's the same as the file_name1039if(defined$params{'file_parent'}) {1040if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1041delete$params{'file_parent'};1042}elsif($params{'file_parent'} !~/\.\./) {1043$href.=":/".esc_url($params{'file_parent'});1044delete$params{'file_parent'};1045}1046}1047$href.="..";1048delete$params{'hash_parent'};1049delete$params{'hash_parent_base'};1050}elsif(defined$params{'hash_parent'}) {1051$href.= esc_url($params{'hash_parent'})."..";1052delete$params{'hash_parent'};1053}10541055$href.= esc_url($params{'hash_base'});1056if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1057$href.=":/".esc_url($params{'file_name'});1058delete$params{'file_name'};1059}1060delete$params{'hash'};1061delete$params{'hash_base'};1062}elsif(defined$params{'hash'}) {1063$href.= esc_url($params{'hash'});1064delete$params{'hash'};1065}10661067# If the action was a snapshot, we can absorb the1068# snapshot_format parameter too1069if($is_snapshot) {1070my$fmt=$params{'snapshot_format'};1071# snapshot_format should always be defined when href()1072# is called, but just in case some code forgets, we1073# fall back to the default1074$fmt||=$snapshot_fmts[0];1075$href.=$known_snapshot_formats{$fmt}{'suffix'};1076delete$params{'snapshot_format'};1077}1078}10791080# now encode the parameters explicitly1081my@result= ();1082for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1083my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1084if(defined$params{$name}) {1085if(ref($params{$name})eq"ARRAY") {1086foreachmy$par(@{$params{$name}}) {1087push@result,$symbol."=". esc_param($par);1088}1089}else{1090push@result,$symbol."=". esc_param($params{$name});1091}1092}1093}1094$href.="?".join(';',@result)ifscalar@result;10951096return$href;1097}109810991100## ======================================================================1101## validation, quoting/unquoting and escaping11021103sub validate_action {1104my$input=shift||returnundef;1105returnundefunlessexists$actions{$input};1106return$input;1107}11081109sub validate_project {1110my$input=shift||returnundef;1111if(!validate_pathname($input) ||1112!(-d "$projectroot/$input") ||1113!check_export_ok("$projectroot/$input") ||1114($strict_export&& !project_in_list($input))) {1115returnundef;1116}else{1117return$input;1118}1119}11201121sub validate_pathname {1122my$input=shift||returnundef;11231124# no '.' or '..' as elements of path, i.e. no '.' nor '..'1125# at the beginning, at the end, and between slashes.1126# also this catches doubled slashes1127if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1128returnundef;1129}1130# no null characters1131if($input=~m!\0!) {1132returnundef;1133}1134return$input;1135}11361137sub validate_refname {1138my$input=shift||returnundef;11391140# textual hashes are O.K.1141if($input=~m/^[0-9a-fA-F]{40}$/) {1142return$input;1143}1144# it must be correct pathname1145$input= validate_pathname($input)1146orreturnundef;1147# restrictions on ref name according to git-check-ref-format1148if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1149returnundef;1150}1151return$input;1152}11531154# decode sequences of octets in utf8 into Perl's internal form,1155# which is utf-8 with utf8 flag set if needed. gitweb writes out1156# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1157sub to_utf8 {1158my$str=shift;1159returnundefunlessdefined$str;1160if(utf8::valid($str)) {1161 utf8::decode($str);1162return$str;1163}else{1164return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1165}1166}11671168# quote unsafe chars, but keep the slash, even when it's not1169# correct, but quoted slashes look too horrible in bookmarks1170sub esc_param {1171my$str=shift;1172returnundefunlessdefined$str;1173$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1174$str=~s/ /\+/g;1175return$str;1176}11771178# quote unsafe chars in whole URL, so some charactrs cannot be quoted1179sub esc_url {1180my$str=shift;1181returnundefunlessdefined$str;1182$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1183$str=~s/\+/%2B/g;1184$str=~s/ /\+/g;1185return$str;1186}11871188# replace invalid utf8 character with SUBSTITUTION sequence1189sub esc_html {1190my$str=shift;1191my%opts=@_;11921193returnundefunlessdefined$str;11941195$str= to_utf8($str);1196$str=$cgi->escapeHTML($str);1197if($opts{'-nbsp'}) {1198$str=~s/ / /g;1199}1200$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1201return$str;1202}12031204# quote control characters and escape filename to HTML1205sub esc_path {1206my$str=shift;1207my%opts=@_;12081209returnundefunlessdefined$str;12101211$str= to_utf8($str);1212$str=$cgi->escapeHTML($str);1213if($opts{'-nbsp'}) {1214$str=~s/ / /g;1215}1216$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1217return$str;1218}12191220# Make control characters "printable", using character escape codes (CEC)1221sub quot_cec {1222my$cntrl=shift;1223my%opts=@_;1224my%es= (# character escape codes, aka escape sequences1225"\t"=>'\t',# tab (HT)1226"\n"=>'\n',# line feed (LF)1227"\r"=>'\r',# carrige return (CR)1228"\f"=>'\f',# form feed (FF)1229"\b"=>'\b',# backspace (BS)1230"\a"=>'\a',# alarm (bell) (BEL)1231"\e"=>'\e',# escape (ESC)1232"\013"=>'\v',# vertical tab (VT)1233"\000"=>'\0',# nul character (NUL)1234);1235my$chr= ( (exists$es{$cntrl})1236?$es{$cntrl}1237:sprintf('\%2x',ord($cntrl)) );1238if($opts{-nohtml}) {1239return$chr;1240}else{1241return"<span class=\"cntrl\">$chr</span>";1242}1243}12441245# Alternatively use unicode control pictures codepoints,1246# Unicode "printable representation" (PR)1247sub quot_upr {1248my$cntrl=shift;1249my%opts=@_;12501251my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1252if($opts{-nohtml}) {1253return$chr;1254}else{1255return"<span class=\"cntrl\">$chr</span>";1256}1257}12581259# git may return quoted and escaped filenames1260sub unquote {1261my$str=shift;12621263sub unq {1264my$seq=shift;1265my%es= (# character escape codes, aka escape sequences1266't'=>"\t",# tab (HT, TAB)1267'n'=>"\n",# newline (NL)1268'r'=>"\r",# return (CR)1269'f'=>"\f",# form feed (FF)1270'b'=>"\b",# backspace (BS)1271'a'=>"\a",# alarm (bell) (BEL)1272'e'=>"\e",# escape (ESC)1273'v'=>"\013",# vertical tab (VT)1274);12751276if($seq=~m/^[0-7]{1,3}$/) {1277# octal char sequence1278returnchr(oct($seq));1279}elsif(exists$es{$seq}) {1280# C escape sequence, aka character escape code1281return$es{$seq};1282}1283# quoted ordinary character1284return$seq;1285}12861287if($str=~m/^"(.*)"$/) {1288# needs unquoting1289$str=$1;1290$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1291}1292return$str;1293}12941295# escape tabs (convert tabs to spaces)1296sub untabify {1297my$line=shift;12981299while((my$pos=index($line,"\t")) != -1) {1300if(my$count= (8- ($pos%8))) {1301my$spaces=' ' x $count;1302$line=~s/\t/$spaces/;1303}1304}13051306return$line;1307}13081309sub project_in_list {1310my$project=shift;1311my@list= git_get_projects_list();1312return@list&&scalar(grep{$_->{'path'}eq$project}@list);1313}13141315## ----------------------------------------------------------------------1316## HTML aware string manipulation13171318# Try to chop given string on a word boundary between position1319# $len and $len+$add_len. If there is no word boundary there,1320# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1321# (marking chopped part) would be longer than given string.1322sub chop_str {1323my$str=shift;1324my$len=shift;1325my$add_len=shift||10;1326my$where=shift||'right';# 'left' | 'center' | 'right'13271328# Make sure perl knows it is utf8 encoded so we don't1329# cut in the middle of a utf8 multibyte char.1330$str= to_utf8($str);13311332# allow only $len chars, but don't cut a word if it would fit in $add_len1333# if it doesn't fit, cut it if it's still longer than the dots we would add1334# remove chopped character entities entirely13351336# when chopping in the middle, distribute $len into left and right part1337# return early if chopping wouldn't make string shorter1338if($whereeq'center') {1339return$strif($len+5>=length($str));# filler is length 51340$len=int($len/2);1341}else{1342return$strif($len+4>=length($str));# filler is length 41343}13441345# regexps: ending and beginning with word part up to $add_len1346my$endre=qr/.{$len}\w{0,$add_len}/;1347my$begre=qr/\w{0,$add_len}.{$len}/;13481349if($whereeq'left') {1350$str=~m/^(.*?)($begre)$/;1351my($lead,$body) = ($1,$2);1352if(length($lead) >4) {1353$lead=" ...";1354}1355return"$lead$body";13561357}elsif($whereeq'center') {1358$str=~m/^($endre)(.*)$/;1359my($left,$str) = ($1,$2);1360$str=~m/^(.*?)($begre)$/;1361my($mid,$right) = ($1,$2);1362if(length($mid) >5) {1363$mid=" ... ";1364}1365return"$left$mid$right";13661367}else{1368$str=~m/^($endre)(.*)$/;1369my$body=$1;1370my$tail=$2;1371if(length($tail) >4) {1372$tail="... ";1373}1374return"$body$tail";1375}1376}13771378# takes the same arguments as chop_str, but also wraps a <span> around the1379# result with a title attribute if it does get chopped. Additionally, the1380# string is HTML-escaped.1381sub chop_and_escape_str {1382my($str) =@_;13831384my$chopped= chop_str(@_);1385if($choppedeq$str) {1386return esc_html($chopped);1387}else{1388$str=~s/[[:cntrl:]]/?/g;1389return$cgi->span({-title=>$str}, esc_html($chopped));1390}1391}13921393## ----------------------------------------------------------------------1394## functions returning short strings13951396# CSS class for given age value (in seconds)1397sub age_class {1398my$age=shift;13991400if(!defined$age) {1401return"noage";1402}elsif($age<60*60*2) {1403return"age0";1404}elsif($age<60*60*24*2) {1405return"age1";1406}else{1407return"age2";1408}1409}14101411# convert age in seconds to "nn units ago" string1412sub age_string {1413my$age=shift;1414my$age_str;14151416if($age>60*60*24*365*2) {1417$age_str= (int$age/60/60/24/365);1418$age_str.=" years ago";1419}elsif($age>60*60*24*(365/12)*2) {1420$age_str=int$age/60/60/24/(365/12);1421$age_str.=" months ago";1422}elsif($age>60*60*24*7*2) {1423$age_str=int$age/60/60/24/7;1424$age_str.=" weeks ago";1425}elsif($age>60*60*24*2) {1426$age_str=int$age/60/60/24;1427$age_str.=" days ago";1428}elsif($age>60*60*2) {1429$age_str=int$age/60/60;1430$age_str.=" hours ago";1431}elsif($age>60*2) {1432$age_str=int$age/60;1433$age_str.=" min ago";1434}elsif($age>2) {1435$age_str=int$age;1436$age_str.=" sec ago";1437}else{1438$age_str.=" right now";1439}1440return$age_str;1441}14421443useconstant{1444 S_IFINVALID =>0030000,1445 S_IFGITLINK =>0160000,1446};14471448# submodule/subproject, a commit object reference1449sub S_ISGITLINK {1450my$mode=shift;14511452return(($mode& S_IFMT) == S_IFGITLINK)1453}14541455# convert file mode in octal to symbolic file mode string1456sub mode_str {1457my$mode=oct shift;14581459if(S_ISGITLINK($mode)) {1460return'm---------';1461}elsif(S_ISDIR($mode& S_IFMT)) {1462return'drwxr-xr-x';1463}elsif(S_ISLNK($mode)) {1464return'lrwxrwxrwx';1465}elsif(S_ISREG($mode)) {1466# git cares only about the executable bit1467if($mode& S_IXUSR) {1468return'-rwxr-xr-x';1469}else{1470return'-rw-r--r--';1471};1472}else{1473return'----------';1474}1475}14761477# convert file mode in octal to file type string1478sub file_type {1479my$mode=shift;14801481if($mode!~m/^[0-7]+$/) {1482return$mode;1483}else{1484$mode=oct$mode;1485}14861487if(S_ISGITLINK($mode)) {1488return"submodule";1489}elsif(S_ISDIR($mode& S_IFMT)) {1490return"directory";1491}elsif(S_ISLNK($mode)) {1492return"symlink";1493}elsif(S_ISREG($mode)) {1494return"file";1495}else{1496return"unknown";1497}1498}14991500# convert file mode in octal to file type description string1501sub file_type_long {1502my$mode=shift;15031504if($mode!~m/^[0-7]+$/) {1505return$mode;1506}else{1507$mode=oct$mode;1508}15091510if(S_ISGITLINK($mode)) {1511return"submodule";1512}elsif(S_ISDIR($mode& S_IFMT)) {1513return"directory";1514}elsif(S_ISLNK($mode)) {1515return"symlink";1516}elsif(S_ISREG($mode)) {1517if($mode& S_IXUSR) {1518return"executable";1519}else{1520return"file";1521};1522}else{1523return"unknown";1524}1525}152615271528## ----------------------------------------------------------------------1529## functions returning short HTML fragments, or transforming HTML fragments1530## which don't belong to other sections15311532# format line of commit message.1533sub format_log_line_html {1534my$line=shift;15351536$line= esc_html($line, -nbsp=>1);1537$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1538$cgi->a({-href => href(action=>"object", hash=>$1),1539-class=>"text"},$1);1540}eg;15411542return$line;1543}15441545# format marker of refs pointing to given object15461547# the destination action is chosen based on object type and current context:1548# - for annotated tags, we choose the tag view unless it's the current view1549# already, in which case we go to shortlog view1550# - for other refs, we keep the current view if we're in history, shortlog or1551# log view, and select shortlog otherwise1552sub format_ref_marker {1553my($refs,$id) =@_;1554my$markers='';15551556if(defined$refs->{$id}) {1557foreachmy$ref(@{$refs->{$id}}) {1558# this code exploits the fact that non-lightweight tags are the1559# only indirect objects, and that they are the only objects for which1560# we want to use tag instead of shortlog as action1561my($type,$name) =qw();1562my$indirect= ($ref=~s/\^\{\}$//);1563# e.g. tags/v2.6.11 or heads/next1564if($ref=~m!^(.*?)s?/(.*)$!) {1565$type=$1;1566$name=$2;1567}else{1568$type="ref";1569$name=$ref;1570}15711572my$class=$type;1573$class.=" indirect"if$indirect;15741575my$dest_action="shortlog";15761577if($indirect) {1578$dest_action="tag"unless$actioneq"tag";1579}elsif($action=~/^(history|(short)?log)$/) {1580$dest_action=$action;1581}15821583my$dest="";1584$dest.="refs/"unless$ref=~ m!^refs/!;1585$dest.=$ref;15861587my$link=$cgi->a({1588-href => href(1589 action=>$dest_action,1590 hash=>$dest1591)},$name);15921593$markers.=" <span class=\"$class\"title=\"$ref\">".1594$link."</span>";1595}1596}15971598if($markers) {1599return' <span class="refs">'.$markers.'</span>';1600}else{1601return"";1602}1603}16041605# format, perhaps shortened and with markers, title line1606sub format_subject_html {1607my($long,$short,$href,$extra) =@_;1608$extra=''unlessdefined($extra);16091610if(length($short) <length($long)) {1611$long=~s/[[:cntrl:]]/?/g;1612return$cgi->a({-href =>$href, -class=>"list subject",1613-title => to_utf8($long)},1614 esc_html($short)) .$extra;1615}else{1616return$cgi->a({-href =>$href, -class=>"list subject"},1617 esc_html($long)) .$extra;1618}1619}16201621# Rather than recomputing the url for an email multiple times, we cache it1622# after the first hit. This gives a visible benefit in views where the avatar1623# for the same email is used repeatedly (e.g. shortlog).1624# The cache is shared by all avatar engines (currently gravatar only), which1625# are free to use it as preferred. Since only one avatar engine is used for any1626# given page, there's no risk for cache conflicts.1627our%avatar_cache= ();16281629# Compute the picon url for a given email, by using the picon search service over at1630# http://www.cs.indiana.edu/picons/search.html1631sub picon_url {1632my$email=lc shift;1633if(!$avatar_cache{$email}) {1634my($user,$domain) =split('@',$email);1635$avatar_cache{$email} =1636"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1637"$domain/$user/".1638"users+domains+unknown/up/single";1639}1640return$avatar_cache{$email};1641}16421643# Compute the gravatar url for a given email, if it's not in the cache already.1644# Gravatar stores only the part of the URL before the size, since that's the1645# one computationally more expensive. This also allows reuse of the cache for1646# different sizes (for this particular engine).1647sub gravatar_url {1648my$email=lc shift;1649my$size=shift;1650$avatar_cache{$email} ||=1651"http://www.gravatar.com/avatar/".1652 Digest::MD5::md5_hex($email) ."?s=";1653return$avatar_cache{$email} .$size;1654}16551656# Insert an avatar for the given $email at the given $size if the feature1657# is enabled.1658sub git_get_avatar {1659my($email,%opts) =@_;1660my$pre_white= ($opts{-pad_before} ?" ":"");1661my$post_white= ($opts{-pad_after} ?" ":"");1662$opts{-size} ||='default';1663my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1664my$url="";1665if($git_avatareq'gravatar') {1666$url= gravatar_url($email,$size);1667}elsif($git_avatareq'picon') {1668$url= picon_url($email);1669}1670# Other providers can be added by extending the if chain, defining $url1671# as needed. If no variant puts something in $url, we assume avatars1672# are completely disabled/unavailable.1673if($url) {1674return$pre_white.1675"<img width=\"$size\"".1676"class=\"avatar\"".1677"src=\"$url\"".1678"alt=\"\"".1679"/>".$post_white;1680}else{1681return"";1682}1683}16841685sub format_search_author {1686my($author,$searchtype,$displaytext) =@_;1687my$have_search= gitweb_check_feature('search');16881689if($have_search) {1690my$performed="";1691if($searchtypeeq'author') {1692$performed="authored";1693}elsif($searchtypeeq'committer') {1694$performed="committed";1695}16961697return$cgi->a({-href => href(action=>"search", hash=>$hash,1698 searchtext=>$author,1699 searchtype=>$searchtype),class=>"list",1700 title=>"Search for commits$performedby$author"},1701$displaytext);17021703}else{1704return$displaytext;1705}1706}17071708# format the author name of the given commit with the given tag1709# the author name is chopped and escaped according to the other1710# optional parameters (see chop_str).1711sub format_author_html {1712my$tag=shift;1713my$co=shift;1714my$author= chop_and_escape_str($co->{'author_name'},@_);1715return"<$tagclass=\"author\">".1716 format_search_author($co->{'author_name'},"author",1717 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1718$author) .1719"</$tag>";1720}17211722# format git diff header line, i.e. "diff --(git|combined|cc) ..."1723sub format_git_diff_header_line {1724my$line=shift;1725my$diffinfo=shift;1726my($from,$to) =@_;17271728if($diffinfo->{'nparents'}) {1729# combined diff1730$line=~s!^(diff (.*?) )"?.*$!$1!;1731if($to->{'href'}) {1732$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1733 esc_path($to->{'file'}));1734}else{# file was deleted (no href)1735$line.= esc_path($to->{'file'});1736}1737}else{1738# "ordinary" diff1739$line=~s!^(diff (.*?) )"?a/.*$!$1!;1740if($from->{'href'}) {1741$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1742'a/'. esc_path($from->{'file'}));1743}else{# file was added (no href)1744$line.='a/'. esc_path($from->{'file'});1745}1746$line.=' ';1747if($to->{'href'}) {1748$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1749'b/'. esc_path($to->{'file'}));1750}else{# file was deleted1751$line.='b/'. esc_path($to->{'file'});1752}1753}17541755return"<div class=\"diff header\">$line</div>\n";1756}17571758# format extended diff header line, before patch itself1759sub format_extended_diff_header_line {1760my$line=shift;1761my$diffinfo=shift;1762my($from,$to) =@_;17631764# match <path>1765if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1766$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1767 esc_path($from->{'file'}));1768}1769if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1770$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1771 esc_path($to->{'file'}));1772}1773# match single <mode>1774if($line=~m/\s(\d{6})$/) {1775$line.='<span class="info"> ('.1776 file_type_long($1) .1777')</span>';1778}1779# match <hash>1780if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1781# can match only for combined diff1782$line='index ';1783for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1784if($from->{'href'}[$i]) {1785$line.=$cgi->a({-href=>$from->{'href'}[$i],1786-class=>"hash"},1787substr($diffinfo->{'from_id'}[$i],0,7));1788}else{1789$line.='0' x 7;1790}1791# separator1792$line.=','if($i<$diffinfo->{'nparents'} -1);1793}1794$line.='..';1795if($to->{'href'}) {1796$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1797substr($diffinfo->{'to_id'},0,7));1798}else{1799$line.='0' x 7;1800}18011802}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1803# can match only for ordinary diff1804my($from_link,$to_link);1805if($from->{'href'}) {1806$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1807substr($diffinfo->{'from_id'},0,7));1808}else{1809$from_link='0' x 7;1810}1811if($to->{'href'}) {1812$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1813substr($diffinfo->{'to_id'},0,7));1814}else{1815$to_link='0' x 7;1816}1817my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1818$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1819}18201821return$line."<br/>\n";1822}18231824# format from-file/to-file diff header1825sub format_diff_from_to_header {1826my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1827my$line;1828my$result='';18291830$line=$from_line;1831#assert($line =~ m/^---/) if DEBUG;1832# no extra formatting for "^--- /dev/null"1833if(!$diffinfo->{'nparents'}) {1834# ordinary (single parent) diff1835if($line=~m!^--- "?a/!) {1836if($from->{'href'}) {1837$line='--- a/'.1838$cgi->a({-href=>$from->{'href'}, -class=>"path"},1839 esc_path($from->{'file'}));1840}else{1841$line='--- a/'.1842 esc_path($from->{'file'});1843}1844}1845$result.= qq!<div class="diff from_file">$line</div>\n!;18461847}else{1848# combined diff (merge commit)1849for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1850if($from->{'href'}[$i]) {1851$line='--- '.1852$cgi->a({-href=>href(action=>"blobdiff",1853 hash_parent=>$diffinfo->{'from_id'}[$i],1854 hash_parent_base=>$parents[$i],1855 file_parent=>$from->{'file'}[$i],1856 hash=>$diffinfo->{'to_id'},1857 hash_base=>$hash,1858 file_name=>$to->{'file'}),1859-class=>"path",1860-title=>"diff". ($i+1)},1861$i+1) .1862'/'.1863$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1864 esc_path($from->{'file'}[$i]));1865}else{1866$line='--- /dev/null';1867}1868$result.= qq!<div class="diff from_file">$line</div>\n!;1869}1870}18711872$line=$to_line;1873#assert($line =~ m/^\+\+\+/) if DEBUG;1874# no extra formatting for "^+++ /dev/null"1875if($line=~m!^\+\+\+ "?b/!) {1876if($to->{'href'}) {1877$line='+++ b/'.1878$cgi->a({-href=>$to->{'href'}, -class=>"path"},1879 esc_path($to->{'file'}));1880}else{1881$line='+++ b/'.1882 esc_path($to->{'file'});1883}1884}1885$result.= qq!<div class="diff to_file">$line</div>\n!;18861887return$result;1888}18891890# create note for patch simplified by combined diff1891sub format_diff_cc_simplified {1892my($diffinfo,@parents) =@_;1893my$result='';18941895$result.="<div class=\"diff header\">".1896"diff --cc ";1897if(!is_deleted($diffinfo)) {1898$result.=$cgi->a({-href => href(action=>"blob",1899 hash_base=>$hash,1900 hash=>$diffinfo->{'to_id'},1901 file_name=>$diffinfo->{'to_file'}),1902-class=>"path"},1903 esc_path($diffinfo->{'to_file'}));1904}else{1905$result.= esc_path($diffinfo->{'to_file'});1906}1907$result.="</div>\n".# class="diff header"1908"<div class=\"diff nodifferences\">".1909"Simple merge".1910"</div>\n";# class="diff nodifferences"19111912return$result;1913}19141915# format patch (diff) line (not to be used for diff headers)1916sub format_diff_line {1917my$line=shift;1918my($from,$to) =@_;1919my$diff_class="";19201921chomp$line;19221923if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1924# combined diff1925my$prefix=substr($line,0,scalar@{$from->{'href'}});1926if($line=~m/^\@{3}/) {1927$diff_class=" chunk_header";1928}elsif($line=~m/^\\/) {1929$diff_class=" incomplete";1930}elsif($prefix=~tr/+/+/) {1931$diff_class=" add";1932}elsif($prefix=~tr/-/-/) {1933$diff_class=" rem";1934}1935}else{1936# assume ordinary diff1937my$char=substr($line,0,1);1938if($chareq'+') {1939$diff_class=" add";1940}elsif($chareq'-') {1941$diff_class=" rem";1942}elsif($chareq'@') {1943$diff_class=" chunk_header";1944}elsif($chareq"\\") {1945$diff_class=" incomplete";1946}1947}1948$line= untabify($line);1949if($from&&$to&&$line=~m/^\@{2} /) {1950my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1951$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;19521953$from_lines=0unlessdefined$from_lines;1954$to_lines=0unlessdefined$to_lines;19551956if($from->{'href'}) {1957$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1958-class=>"list"},$from_text);1959}1960if($to->{'href'}) {1961$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1962-class=>"list"},$to_text);1963}1964$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1965"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1966return"<div class=\"diff$diff_class\">$line</div>\n";1967}elsif($from&&$to&&$line=~m/^\@{3}/) {1968my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1969my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);19701971@from_text=split(' ',$ranges);1972for(my$i=0;$i<@from_text; ++$i) {1973($from_start[$i],$from_nlines[$i]) =1974(split(',',substr($from_text[$i],1)),0);1975}19761977$to_text=pop@from_text;1978$to_start=pop@from_start;1979$to_nlines=pop@from_nlines;19801981$line="<span class=\"chunk_info\">$prefix";1982for(my$i=0;$i<@from_text; ++$i) {1983if($from->{'href'}[$i]) {1984$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1985-class=>"list"},$from_text[$i]);1986}else{1987$line.=$from_text[$i];1988}1989$line.=" ";1990}1991if($to->{'href'}) {1992$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1993-class=>"list"},$to_text);1994}else{1995$line.=$to_text;1996}1997$line.="$prefix</span>".1998"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1999return"<div class=\"diff$diff_class\">$line</div>\n";2000}2001return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2002}20032004# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2005# linked. Pass the hash of the tree/commit to snapshot.2006sub format_snapshot_links {2007my($hash) =@_;2008my$num_fmts=@snapshot_fmts;2009if($num_fmts>1) {2010# A parenthesized list of links bearing format names.2011# e.g. "snapshot (_tar.gz_ _zip_)"2012return"snapshot (".join(' ',map2013$cgi->a({2014-href => href(2015 action=>"snapshot",2016 hash=>$hash,2017 snapshot_format=>$_2018)2019},$known_snapshot_formats{$_}{'display'})2020,@snapshot_fmts) .")";2021}elsif($num_fmts==1) {2022# A single "snapshot" link whose tooltip bears the format name.2023# i.e. "_snapshot_"2024my($fmt) =@snapshot_fmts;2025return2026$cgi->a({2027-href => href(2028 action=>"snapshot",2029 hash=>$hash,2030 snapshot_format=>$fmt2031),2032-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2033},"snapshot");2034}else{# $num_fmts == 02035returnundef;2036}2037}20382039## ......................................................................2040## functions returning values to be passed, perhaps after some2041## transformation, to other functions; e.g. returning arguments to href()20422043# returns hash to be passed to href to generate gitweb URL2044# in -title key it returns description of link2045sub get_feed_info {2046my$format=shift||'Atom';2047my%res= (action =>lc($format));20482049# feed links are possible only for project views2050return unless(defined$project);2051# some views should link to OPML, or to generic project feed,2052# or don't have specific feed yet (so they should use generic)2053return if($action=~/^(?:tags|heads|forks|tag|search)$/x);20542055my$branch;2056# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2057# from tag links; this also makes possible to detect branch links2058if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2059(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2060$branch=$1;2061}2062# find log type for feed description (title)2063my$type='log';2064if(defined$file_name) {2065$type="history of$file_name";2066$type.="/"if($actioneq'tree');2067$type.=" on '$branch'"if(defined$branch);2068}else{2069$type="log of$branch"if(defined$branch);2070}20712072$res{-title} =$type;2073$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2074$res{'file_name'} =$file_name;20752076return%res;2077}20782079## ----------------------------------------------------------------------2080## git utility subroutines, invoking git commands20812082# returns path to the core git executable and the --git-dir parameter as list2083sub git_cmd {2084$number_of_git_cmds++;2085return$GIT,'--git-dir='.$git_dir;2086}20872088# quote the given arguments for passing them to the shell2089# quote_command("command", "arg 1", "arg with ' and ! characters")2090# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2091# Try to avoid using this function wherever possible.2092sub quote_command {2093returnjoin(' ',2094map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2095}20962097# get HEAD ref of given project as hash2098sub git_get_head_hash {2099return git_get_full_hash(shift,'HEAD');2100}21012102sub git_get_full_hash {2103return git_get_hash(@_);2104}21052106sub git_get_short_hash {2107return git_get_hash(@_,'--short=7');2108}21092110sub git_get_hash {2111my($project,$hash,@options) =@_;2112my$o_git_dir=$git_dir;2113my$retval=undef;2114$git_dir="$projectroot/$project";2115if(open my$fd,'-|', git_cmd(),'rev-parse',2116'--verify','-q',@options,$hash) {2117$retval= <$fd>;2118chomp$retvalifdefined$retval;2119close$fd;2120}2121if(defined$o_git_dir) {2122$git_dir=$o_git_dir;2123}2124return$retval;2125}21262127# get type of given object2128sub git_get_type {2129my$hash=shift;21302131open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2132my$type= <$fd>;2133close$fdorreturn;2134chomp$type;2135return$type;2136}21372138# repository configuration2139our$config_file='';2140our%config;21412142# store multiple values for single key as anonymous array reference2143# single values stored directly in the hash, not as [ <value> ]2144sub hash_set_multi {2145my($hash,$key,$value) =@_;21462147if(!exists$hash->{$key}) {2148$hash->{$key} =$value;2149}elsif(!ref$hash->{$key}) {2150$hash->{$key} = [$hash->{$key},$value];2151}else{2152push@{$hash->{$key}},$value;2153}2154}21552156# return hash of git project configuration2157# optionally limited to some section, e.g. 'gitweb'2158sub git_parse_project_config {2159my$section_regexp=shift;2160my%config;21612162local$/="\0";21632164open my$fh,"-|", git_cmd(),"config",'-z','-l',2165orreturn;21662167while(my$keyval= <$fh>) {2168chomp$keyval;2169my($key,$value) =split(/\n/,$keyval,2);21702171 hash_set_multi(\%config,$key,$value)2172if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2173}2174close$fh;21752176return%config;2177}21782179# convert config value to boolean: 'true' or 'false'2180# no value, number > 0, 'true' and 'yes' values are true2181# rest of values are treated as false (never as error)2182sub config_to_bool {2183my$val=shift;21842185return1if!defined$val;# section.key21862187# strip leading and trailing whitespace2188$val=~s/^\s+//;2189$val=~s/\s+$//;21902191return(($val=~/^\d+$/&&$val) ||# section.key = 12192($val=~/^(?:true|yes)$/i));# section.key = true2193}21942195# convert config value to simple decimal number2196# an optional value suffix of 'k', 'm', or 'g' will cause the value2197# to be multiplied by 1024, 1048576, or 10737418242198sub config_to_int {2199my$val=shift;22002201# strip leading and trailing whitespace2202$val=~s/^\s+//;2203$val=~s/\s+$//;22042205if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2206$unit=lc($unit);2207# unknown unit is treated as 12208return$num* ($uniteq'g'?1073741824:2209$uniteq'm'?1048576:2210$uniteq'k'?1024:1);2211}2212return$val;2213}22142215# convert config value to array reference, if needed2216sub config_to_multi {2217my$val=shift;22182219returnref($val) ?$val: (defined($val) ? [$val] : []);2220}22212222sub git_get_project_config {2223my($key,$type) =@_;22242225return unlessdefined$git_dir;22262227# key sanity check2228return unless($key);2229$key=~s/^gitweb\.//;2230return if($key=~m/\W/);22312232# type sanity check2233if(defined$type) {2234$type=~s/^--//;2235$type=undef2236unless($typeeq'bool'||$typeeq'int');2237}22382239# get config2240if(!defined$config_file||2241$config_filene"$git_dir/config") {2242%config= git_parse_project_config('gitweb');2243$config_file="$git_dir/config";2244}22452246# check if config variable (key) exists2247return unlessexists$config{"gitweb.$key"};22482249# ensure given type2250if(!defined$type) {2251return$config{"gitweb.$key"};2252}elsif($typeeq'bool') {2253# backward compatibility: 'git config --bool' returns true/false2254return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2255}elsif($typeeq'int') {2256return config_to_int($config{"gitweb.$key"});2257}2258return$config{"gitweb.$key"};2259}22602261# get hash of given path at given ref2262sub git_get_hash_by_path {2263my$base=shift;2264my$path=shift||returnundef;2265my$type=shift;22662267$path=~ s,/+$,,;22682269open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2270or die_error(500,"Open git-ls-tree failed");2271my$line= <$fd>;2272close$fdorreturnundef;22732274if(!defined$line) {2275# there is no tree or hash given by $path at $base2276returnundef;2277}22782279#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2280$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2281if(defined$type&&$typene$2) {2282# type doesn't match2283returnundef;2284}2285return$3;2286}22872288# get path of entry with given hash at given tree-ish (ref)2289# used to get 'from' filename for combined diff (merge commit) for renames2290sub git_get_path_by_hash {2291my$base=shift||return;2292my$hash=shift||return;22932294local$/="\0";22952296open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2297orreturnundef;2298while(my$line= <$fd>) {2299chomp$line;23002301#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2302#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2303if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2304close$fd;2305return$1;2306}2307}2308close$fd;2309returnundef;2310}23112312## ......................................................................2313## git utility functions, directly accessing git repository23142315sub git_get_project_description {2316my$path=shift;23172318$git_dir="$projectroot/$path";2319open my$fd,'<',"$git_dir/description"2320orreturn git_get_project_config('description');2321my$descr= <$fd>;2322close$fd;2323if(defined$descr) {2324chomp$descr;2325}2326return$descr;2327}23282329sub git_get_project_ctags {2330my$path=shift;2331my$ctags= {};23322333$git_dir="$projectroot/$path";2334opendir my$dh,"$git_dir/ctags"2335orreturn$ctags;2336foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2337open my$ct,'<',$_ornext;2338my$val= <$ct>;2339chomp$val;2340close$ct;2341my$ctag=$_;$ctag=~ s#.*/##;2342$ctags->{$ctag} =$val;2343}2344closedir$dh;2345$ctags;2346}23472348sub git_populate_project_tagcloud {2349my$ctags=shift;23502351# First, merge different-cased tags; tags vote on casing2352my%ctags_lc;2353foreach(keys%$ctags) {2354$ctags_lc{lc$_}->{count} +=$ctags->{$_};2355if(not$ctags_lc{lc$_}->{topcount}2356or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2357$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2358$ctags_lc{lc$_}->{topname} =$_;2359}2360}23612362my$cloud;2363if(eval{require HTML::TagCloud;1; }) {2364$cloud= HTML::TagCloud->new;2365foreach(sort keys%ctags_lc) {2366# Pad the title with spaces so that the cloud looks2367# less crammed.2368my$title=$ctags_lc{$_}->{topname};2369$title=~s/ / /g;2370$title=~s/^/ /g;2371$title=~s/$/ /g;2372$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2373}2374}else{2375$cloud= \%ctags_lc;2376}2377$cloud;2378}23792380sub git_show_project_tagcloud {2381my($cloud,$count) =@_;2382print STDERR ref($cloud)."..\n";2383if(ref$cloudeq'HTML::TagCloud') {2384return$cloud->html_and_css($count);2385}else{2386my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2387return'<p align="center">'.join(', ',map{2388"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2389}splice(@tags,0,$count)) .'</p>';2390}2391}23922393sub git_get_project_url_list {2394my$path=shift;23952396$git_dir="$projectroot/$path";2397open my$fd,'<',"$git_dir/cloneurl"2398orreturnwantarray?2399@{ config_to_multi(git_get_project_config('url')) } :2400 config_to_multi(git_get_project_config('url'));2401my@git_project_url_list=map{chomp;$_} <$fd>;2402close$fd;24032404returnwantarray?@git_project_url_list: \@git_project_url_list;2405}24062407sub git_get_projects_list {2408my($filter) =@_;2409my@list;24102411$filter||='';2412$filter=~s/\.git$//;24132414my$check_forks= gitweb_check_feature('forks');24152416if(-d $projects_list) {2417# search in directory2418my$dir=$projects_list. ($filter?"/$filter":'');2419# remove the trailing "/"2420$dir=~s!/+$!!;2421my$pfxlen=length("$dir");2422my$pfxdepth= ($dir=~tr!/!!);24232424 File::Find::find({2425 follow_fast =>1,# follow symbolic links2426 follow_skip =>2,# ignore duplicates2427 dangling_symlinks =>0,# ignore dangling symlinks, silently2428 wanted =>sub{2429# skip project-list toplevel, if we get it.2430return if(m!^[/.]$!);2431# only directories can be git repositories2432return unless(-d $_);2433# don't traverse too deep (Find is super slow on os x)2434if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2435$File::Find::prune =1;2436return;2437}24382439my$subdir=substr($File::Find::name,$pfxlen+1);2440# we check related file in $projectroot2441my$path= ($filter?"$filter/":'') .$subdir;2442if(check_export_ok("$projectroot/$path")) {2443push@list, { path =>$path};2444$File::Find::prune =1;2445}2446},2447},"$dir");24482449}elsif(-f $projects_list) {2450# read from file(url-encoded):2451# 'git%2Fgit.git Linus+Torvalds'2452# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2453# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2454my%paths;2455open my$fd,'<',$projects_listorreturn;2456 PROJECT:2457while(my$line= <$fd>) {2458chomp$line;2459my($path,$owner) =split' ',$line;2460$path= unescape($path);2461$owner= unescape($owner);2462if(!defined$path) {2463next;2464}2465if($filterne'') {2466# looking for forks;2467my$pfx=substr($path,0,length($filter));2468if($pfxne$filter) {2469next PROJECT;2470}2471my$sfx=substr($path,length($filter));2472if($sfx!~/^\/.*\.git$/) {2473next PROJECT;2474}2475}elsif($check_forks) {2476 PATH:2477foreachmy$filter(keys%paths) {2478# looking for forks;2479my$pfx=substr($path,0,length($filter));2480if($pfxne$filter) {2481next PATH;2482}2483my$sfx=substr($path,length($filter));2484if($sfx!~/^\/.*\.git$/) {2485next PATH;2486}2487# is a fork, don't include it in2488# the list2489next PROJECT;2490}2491}2492if(check_export_ok("$projectroot/$path")) {2493my$pr= {2494 path =>$path,2495 owner => to_utf8($owner),2496};2497push@list,$pr;2498(my$forks_path=$path) =~s/\.git$//;2499$paths{$forks_path}++;2500}2501}2502close$fd;2503}2504return@list;2505}25062507our$gitweb_project_owner=undef;2508sub git_get_project_list_from_file {25092510return if(defined$gitweb_project_owner);25112512$gitweb_project_owner= {};2513# read from file (url-encoded):2514# 'git%2Fgit.git Linus+Torvalds'2515# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2516# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2517if(-f $projects_list) {2518open(my$fd,'<',$projects_list);2519while(my$line= <$fd>) {2520chomp$line;2521my($pr,$ow) =split' ',$line;2522$pr= unescape($pr);2523$ow= unescape($ow);2524$gitweb_project_owner->{$pr} = to_utf8($ow);2525}2526close$fd;2527}2528}25292530sub git_get_project_owner {2531my$project=shift;2532my$owner;25332534returnundefunless$project;2535$git_dir="$projectroot/$project";25362537if(!defined$gitweb_project_owner) {2538 git_get_project_list_from_file();2539}25402541if(exists$gitweb_project_owner->{$project}) {2542$owner=$gitweb_project_owner->{$project};2543}2544if(!defined$owner){2545$owner= git_get_project_config('owner');2546}2547if(!defined$owner) {2548$owner= get_file_owner("$git_dir");2549}25502551return$owner;2552}25532554sub git_get_last_activity {2555my($path) =@_;2556my$fd;25572558$git_dir="$projectroot/$path";2559open($fd,"-|", git_cmd(),'for-each-ref',2560'--format=%(committer)',2561'--sort=-committerdate',2562'--count=1',2563'refs/heads')orreturn;2564my$most_recent= <$fd>;2565close$fdorreturn;2566if(defined$most_recent&&2567$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2568my$timestamp=$1;2569my$age=time-$timestamp;2570return($age, age_string($age));2571}2572return(undef,undef);2573}25742575sub git_get_references {2576my$type=shift||"";2577my%refs;2578# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112579# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2580open my$fd,"-|", git_cmd(),"show-ref","--dereference",2581($type? ("--","refs/$type") : ())# use -- <pattern> if $type2582orreturn;25832584while(my$line= <$fd>) {2585chomp$line;2586if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2587if(defined$refs{$1}) {2588push@{$refs{$1}},$2;2589}else{2590$refs{$1} = [$2];2591}2592}2593}2594close$fdorreturn;2595return \%refs;2596}25972598sub git_get_rev_name_tags {2599my$hash=shift||returnundef;26002601open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2602orreturn;2603my$name_rev= <$fd>;2604close$fd;26052606if($name_rev=~ m|^$hash tags/(.*)$|) {2607return$1;2608}else{2609# catches also '$hash undefined' output2610returnundef;2611}2612}26132614## ----------------------------------------------------------------------2615## parse to hash functions26162617sub parse_date {2618my$epoch=shift;2619my$tz=shift||"-0000";26202621my%date;2622my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2623my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2624my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2625$date{'hour'} =$hour;2626$date{'minute'} =$min;2627$date{'mday'} =$mday;2628$date{'day'} =$days[$wday];2629$date{'month'} =$months[$mon];2630$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2631$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2632$date{'mday-time'} =sprintf"%d%s%02d:%02d",2633$mday,$months[$mon],$hour,$min;2634$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",26351900+$year,1+$mon,$mday,$hour,$min,$sec;26362637$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2638my$local=$epoch+ ((int$1+ ($2/60)) *3600);2639($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2640$date{'hour_local'} =$hour;2641$date{'minute_local'} =$min;2642$date{'tz_local'} =$tz;2643$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",26441900+$year,$mon+1,$mday,2645$hour,$min,$sec,$tz);2646return%date;2647}26482649sub parse_tag {2650my$tag_id=shift;2651my%tag;2652my@comment;26532654open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2655$tag{'id'} =$tag_id;2656while(my$line= <$fd>) {2657chomp$line;2658if($line=~m/^object ([0-9a-fA-F]{40})$/) {2659$tag{'object'} =$1;2660}elsif($line=~m/^type (.+)$/) {2661$tag{'type'} =$1;2662}elsif($line=~m/^tag (.+)$/) {2663$tag{'name'} =$1;2664}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2665$tag{'author'} =$1;2666$tag{'author_epoch'} =$2;2667$tag{'author_tz'} =$3;2668if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2669$tag{'author_name'} =$1;2670$tag{'author_email'} =$2;2671}else{2672$tag{'author_name'} =$tag{'author'};2673}2674}elsif($line=~m/--BEGIN/) {2675push@comment,$line;2676last;2677}elsif($lineeq"") {2678last;2679}2680}2681push@comment, <$fd>;2682$tag{'comment'} = \@comment;2683close$fdorreturn;2684if(!defined$tag{'name'}) {2685return2686};2687return%tag2688}26892690sub parse_commit_text {2691my($commit_text,$withparents) =@_;2692my@commit_lines=split'\n',$commit_text;2693my%co;26942695pop@commit_lines;# Remove '\0'26962697if(!@commit_lines) {2698return;2699}27002701my$header=shift@commit_lines;2702if($header!~m/^[0-9a-fA-F]{40}/) {2703return;2704}2705($co{'id'},my@parents) =split' ',$header;2706while(my$line=shift@commit_lines) {2707last if$lineeq"\n";2708if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2709$co{'tree'} =$1;2710}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2711push@parents,$1;2712}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2713$co{'author'} = to_utf8($1);2714$co{'author_epoch'} =$2;2715$co{'author_tz'} =$3;2716if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2717$co{'author_name'} =$1;2718$co{'author_email'} =$2;2719}else{2720$co{'author_name'} =$co{'author'};2721}2722}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2723$co{'committer'} = to_utf8($1);2724$co{'committer_epoch'} =$2;2725$co{'committer_tz'} =$3;2726if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2727$co{'committer_name'} =$1;2728$co{'committer_email'} =$2;2729}else{2730$co{'committer_name'} =$co{'committer'};2731}2732}2733}2734if(!defined$co{'tree'}) {2735return;2736};2737$co{'parents'} = \@parents;2738$co{'parent'} =$parents[0];27392740foreachmy$title(@commit_lines) {2741$title=~s/^ //;2742if($titlene"") {2743$co{'title'} = chop_str($title,80,5);2744# remove leading stuff of merges to make the interesting part visible2745if(length($title) >50) {2746$title=~s/^Automatic //;2747$title=~s/^merge (of|with) /Merge ... /i;2748if(length($title) >50) {2749$title=~s/(http|rsync):\/\///;2750}2751if(length($title) >50) {2752$title=~s/(master|www|rsync)\.//;2753}2754if(length($title) >50) {2755$title=~s/kernel.org:?//;2756}2757if(length($title) >50) {2758$title=~s/\/pub\/scm//;2759}2760}2761$co{'title_short'} = chop_str($title,50,5);2762last;2763}2764}2765if(!defined$co{'title'} ||$co{'title'}eq"") {2766$co{'title'} =$co{'title_short'} ='(no commit message)';2767}2768# remove added spaces2769foreachmy$line(@commit_lines) {2770$line=~s/^ //;2771}2772$co{'comment'} = \@commit_lines;27732774my$age=time-$co{'committer_epoch'};2775$co{'age'} =$age;2776$co{'age_string'} = age_string($age);2777my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2778if($age>60*60*24*7*2) {2779$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2780$co{'age_string_age'} =$co{'age_string'};2781}else{2782$co{'age_string_date'} =$co{'age_string'};2783$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2784}2785return%co;2786}27872788sub parse_commit {2789my($commit_id) =@_;2790my%co;27912792local$/="\0";27932794open my$fd,"-|", git_cmd(),"rev-list",2795"--parents",2796"--header",2797"--max-count=1",2798$commit_id,2799"--",2800or die_error(500,"Open git-rev-list failed");2801%co= parse_commit_text(<$fd>,1);2802close$fd;28032804return%co;2805}28062807sub parse_commits {2808my($commit_id,$maxcount,$skip,$filename,@args) =@_;2809my@cos;28102811$maxcount||=1;2812$skip||=0;28132814local$/="\0";28152816open my$fd,"-|", git_cmd(),"rev-list",2817"--header",2818@args,2819("--max-count=".$maxcount),2820("--skip=".$skip),2821@extra_options,2822$commit_id,2823"--",2824($filename? ($filename) : ())2825or die_error(500,"Open git-rev-list failed");2826while(my$line= <$fd>) {2827my%co= parse_commit_text($line);2828push@cos, \%co;2829}2830close$fd;28312832returnwantarray?@cos: \@cos;2833}28342835# parse line of git-diff-tree "raw" output2836sub parse_difftree_raw_line {2837my$line=shift;2838my%res;28392840# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2841# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2842if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2843$res{'from_mode'} =$1;2844$res{'to_mode'} =$2;2845$res{'from_id'} =$3;2846$res{'to_id'} =$4;2847$res{'status'} =$5;2848$res{'similarity'} =$6;2849if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2850($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2851}else{2852$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2853}2854}2855# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2856# combined diff (for merge commit)2857elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2858$res{'nparents'} =length($1);2859$res{'from_mode'} = [split(' ',$2) ];2860$res{'to_mode'} =pop@{$res{'from_mode'}};2861$res{'from_id'} = [split(' ',$3) ];2862$res{'to_id'} =pop@{$res{'from_id'}};2863$res{'status'} = [split('',$4) ];2864$res{'to_file'} = unquote($5);2865}2866# 'c512b523472485aef4fff9e57b229d9d243c967f'2867elsif($line=~m/^([0-9a-fA-F]{40})$/) {2868$res{'commit'} =$1;2869}28702871returnwantarray?%res: \%res;2872}28732874# wrapper: return parsed line of git-diff-tree "raw" output2875# (the argument might be raw line, or parsed info)2876sub parsed_difftree_line {2877my$line_or_ref=shift;28782879if(ref($line_or_ref)eq"HASH") {2880# pre-parsed (or generated by hand)2881return$line_or_ref;2882}else{2883return parse_difftree_raw_line($line_or_ref);2884}2885}28862887# parse line of git-ls-tree output2888sub parse_ls_tree_line {2889my$line=shift;2890my%opts=@_;2891my%res;28922893if($opts{'-l'}) {2894#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'2895$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;28962897$res{'mode'} =$1;2898$res{'type'} =$2;2899$res{'hash'} =$3;2900$res{'size'} =$4;2901if($opts{'-z'}) {2902$res{'name'} =$5;2903}else{2904$res{'name'} = unquote($5);2905}2906}else{2907#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2908$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;29092910$res{'mode'} =$1;2911$res{'type'} =$2;2912$res{'hash'} =$3;2913if($opts{'-z'}) {2914$res{'name'} =$4;2915}else{2916$res{'name'} = unquote($4);2917}2918}29192920returnwantarray?%res: \%res;2921}29222923# generates _two_ hashes, references to which are passed as 2 and 3 argument2924sub parse_from_to_diffinfo {2925my($diffinfo,$from,$to,@parents) =@_;29262927if($diffinfo->{'nparents'}) {2928# combined diff2929$from->{'file'} = [];2930$from->{'href'} = [];2931 fill_from_file_info($diffinfo,@parents)2932unlessexists$diffinfo->{'from_file'};2933for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2934$from->{'file'}[$i] =2935defined$diffinfo->{'from_file'}[$i] ?2936$diffinfo->{'from_file'}[$i] :2937$diffinfo->{'to_file'};2938if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2939$from->{'href'}[$i] = href(action=>"blob",2940 hash_base=>$parents[$i],2941 hash=>$diffinfo->{'from_id'}[$i],2942 file_name=>$from->{'file'}[$i]);2943}else{2944$from->{'href'}[$i] =undef;2945}2946}2947}else{2948# ordinary (not combined) diff2949$from->{'file'} =$diffinfo->{'from_file'};2950if($diffinfo->{'status'}ne"A") {# not new (added) file2951$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2952 hash=>$diffinfo->{'from_id'},2953 file_name=>$from->{'file'});2954}else{2955delete$from->{'href'};2956}2957}29582959$to->{'file'} =$diffinfo->{'to_file'};2960if(!is_deleted($diffinfo)) {# file exists in result2961$to->{'href'} = href(action=>"blob", hash_base=>$hash,2962 hash=>$diffinfo->{'to_id'},2963 file_name=>$to->{'file'});2964}else{2965delete$to->{'href'};2966}2967}29682969## ......................................................................2970## parse to array of hashes functions29712972sub git_get_heads_list {2973my$limit=shift;2974my@headslist;29752976open my$fd,'-|', git_cmd(),'for-each-ref',2977($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2978'--format=%(objectname) %(refname) %(subject)%00%(committer)',2979'refs/heads'2980orreturn;2981while(my$line= <$fd>) {2982my%ref_item;29832984chomp$line;2985my($refinfo,$committerinfo) =split(/\0/,$line);2986my($hash,$name,$title) =split(' ',$refinfo,3);2987my($committer,$epoch,$tz) =2988($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2989$ref_item{'fullname'} =$name;2990$name=~s!^refs/heads/!!;29912992$ref_item{'name'} =$name;2993$ref_item{'id'} =$hash;2994$ref_item{'title'} =$title||'(no commit message)';2995$ref_item{'epoch'} =$epoch;2996if($epoch) {2997$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2998}else{2999$ref_item{'age'} ="unknown";3000}30013002push@headslist, \%ref_item;3003}3004close$fd;30053006returnwantarray?@headslist: \@headslist;3007}30083009sub git_get_tags_list {3010my$limit=shift;3011my@tagslist;30123013open my$fd,'-|', git_cmd(),'for-each-ref',3014($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3015'--format=%(objectname) %(objecttype) %(refname) '.3016'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3017'refs/tags'3018orreturn;3019while(my$line= <$fd>) {3020my%ref_item;30213022chomp$line;3023my($refinfo,$creatorinfo) =split(/\0/,$line);3024my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3025my($creator,$epoch,$tz) =3026($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3027$ref_item{'fullname'} =$name;3028$name=~s!^refs/tags/!!;30293030$ref_item{'type'} =$type;3031$ref_item{'id'} =$id;3032$ref_item{'name'} =$name;3033if($typeeq"tag") {3034$ref_item{'subject'} =$title;3035$ref_item{'reftype'} =$reftype;3036$ref_item{'refid'} =$refid;3037}else{3038$ref_item{'reftype'} =$type;3039$ref_item{'refid'} =$id;3040}30413042if($typeeq"tag"||$typeeq"commit") {3043$ref_item{'epoch'} =$epoch;3044if($epoch) {3045$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3046}else{3047$ref_item{'age'} ="unknown";3048}3049}30503051push@tagslist, \%ref_item;3052}3053close$fd;30543055returnwantarray?@tagslist: \@tagslist;3056}30573058## ----------------------------------------------------------------------3059## filesystem-related functions30603061sub get_file_owner {3062my$path=shift;30633064my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3065my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3066if(!defined$gcos) {3067returnundef;3068}3069my$owner=$gcos;3070$owner=~s/[,;].*$//;3071return to_utf8($owner);3072}30733074# assume that file exists3075sub insert_file {3076my$filename=shift;30773078open my$fd,'<',$filename;3079print map{ to_utf8($_) } <$fd>;3080close$fd;3081}30823083## ......................................................................3084## mimetype related functions30853086sub mimetype_guess_file {3087my$filename=shift;3088my$mimemap=shift;3089-r $mimemaporreturnundef;30903091my%mimemap;3092open(my$mh,'<',$mimemap)orreturnundef;3093while(<$mh>) {3094next ifm/^#/;# skip comments3095my($mimetype,$exts) =split(/\t+/);3096if(defined$exts) {3097my@exts=split(/\s+/,$exts);3098foreachmy$ext(@exts) {3099$mimemap{$ext} =$mimetype;3100}3101}3102}3103close($mh);31043105$filename=~/\.([^.]*)$/;3106return$mimemap{$1};3107}31083109sub mimetype_guess {3110my$filename=shift;3111my$mime;3112$filename=~/\./orreturnundef;31133114if($mimetypes_file) {3115my$file=$mimetypes_file;3116if($file!~m!^/!) {# if it is relative path3117# it is relative to project3118$file="$projectroot/$project/$file";3119}3120$mime= mimetype_guess_file($filename,$file);3121}3122$mime||= mimetype_guess_file($filename,'/etc/mime.types');3123return$mime;3124}31253126sub blob_mimetype {3127my$fd=shift;3128my$filename=shift;31293130if($filename) {3131my$mime= mimetype_guess($filename);3132$mimeandreturn$mime;3133}31343135# just in case3136return$default_blob_plain_mimetypeunless$fd;31373138if(-T $fd) {3139return'text/plain';3140}elsif(!$filename) {3141return'application/octet-stream';3142}elsif($filename=~m/\.png$/i) {3143return'image/png';3144}elsif($filename=~m/\.gif$/i) {3145return'image/gif';3146}elsif($filename=~m/\.jpe?g$/i) {3147return'image/jpeg';3148}else{3149return'application/octet-stream';3150}3151}31523153sub blob_contenttype {3154my($fd,$file_name,$type) =@_;31553156$type||= blob_mimetype($fd,$file_name);3157if($typeeq'text/plain'&&defined$default_text_plain_charset) {3158$type.="; charset=$default_text_plain_charset";3159}31603161return$type;3162}31633164## ======================================================================3165## functions printing HTML: header, footer, error page31663167sub git_header_html {3168my$status=shift||"200 OK";3169my$expires=shift;31703171my$title="$site_name";3172if(defined$project) {3173$title.=" - ". to_utf8($project);3174if(defined$action) {3175$title.="/$action";3176if(defined$file_name) {3177$title.=" - ". esc_path($file_name);3178if($actioneq"tree"&&$file_name!~ m|/$|) {3179$title.="/";3180}3181}3182}3183}3184my$content_type;3185# require explicit support from the UA if we are to send the page as3186# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3187# we have to do this because MSIE sometimes globs '*/*', pretending to3188# support xhtml+xml but choking when it gets what it asked for.3189if(defined$cgi->http('HTTP_ACCEPT') &&3190$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3191$cgi->Accept('application/xhtml+xml') !=0) {3192$content_type='application/xhtml+xml';3193}else{3194$content_type='text/html';3195}3196print$cgi->header(-type=>$content_type, -charset =>'utf-8',3197-status=>$status, -expires =>$expires);3198my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3199print<<EOF;3200<?xml version="1.0" encoding="utf-8"?>3201<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3202<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3203<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3204<!-- git core binaries version$git_version-->3205<head>3206<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3207<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3208<meta name="robots" content="index, nofollow"/>3209<title>$title</title>3210EOF3211# the stylesheet, favicon etc urls won't work correctly with path_info3212# unless we set the appropriate base URL3213if($ENV{'PATH_INFO'}) {3214print"<base href=\"".esc_url($base_url)."\"/>\n";3215}3216# print out each stylesheet that exist, providing backwards capability3217# for those people who defined $stylesheet in a config file3218if(defined$stylesheet) {3219print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3220}else{3221foreachmy$stylesheet(@stylesheets) {3222next unless$stylesheet;3223print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3224}3225}3226if(defined$project) {3227my%href_params= get_feed_info();3228if(!exists$href_params{'-title'}) {3229$href_params{'-title'} ='log';3230}32313232foreachmy$formatqw(RSS Atom){3233my$type=lc($format);3234my%link_attr= (3235'-rel'=>'alternate',3236'-title'=>"$project-$href_params{'-title'} -$formatfeed",3237'-type'=>"application/$type+xml"3238);32393240$href_params{'action'} =$type;3241$link_attr{'-href'} = href(%href_params);3242print"<link ".3243"rel=\"$link_attr{'-rel'}\"".3244"title=\"$link_attr{'-title'}\"".3245"href=\"$link_attr{'-href'}\"".3246"type=\"$link_attr{'-type'}\"".3247"/>\n";32483249$href_params{'extra_options'} ='--no-merges';3250$link_attr{'-href'} = href(%href_params);3251$link_attr{'-title'} .=' (no merges)';3252print"<link ".3253"rel=\"$link_attr{'-rel'}\"".3254"title=\"$link_attr{'-title'}\"".3255"href=\"$link_attr{'-href'}\"".3256"type=\"$link_attr{'-type'}\"".3257"/>\n";3258}32593260}else{3261printf('<link rel="alternate" title="%sprojects list" '.3262'href="%s" type="text/plain; charset=utf-8" />'."\n",3263$site_name, href(project=>undef, action=>"project_index"));3264printf('<link rel="alternate" title="%sprojects feeds" '.3265'href="%s" type="text/x-opml" />'."\n",3266$site_name, href(project=>undef, action=>"opml"));3267}3268if(defined$favicon) {3269printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3270}32713272print"</head>\n".3273"<body>\n";32743275if(defined$site_header&& -f $site_header) {3276 insert_file($site_header);3277}32783279print"<div class=\"page_header\">\n".3280$cgi->a({-href => esc_url($logo_url),3281-title =>$logo_label},3282qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3283print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3284if(defined$project) {3285print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3286if(defined$action) {3287print" /$action";3288}3289print"\n";3290}3291print"</div>\n";32923293my$have_search= gitweb_check_feature('search');3294if(defined$project&&$have_search) {3295if(!defined$searchtext) {3296$searchtext="";3297}3298my$search_hash;3299if(defined$hash_base) {3300$search_hash=$hash_base;3301}elsif(defined$hash) {3302$search_hash=$hash;3303}else{3304$search_hash="HEAD";3305}3306my$action=$my_uri;3307my$use_pathinfo= gitweb_check_feature('pathinfo');3308if($use_pathinfo) {3309$action.="/".esc_url($project);3310}3311print$cgi->startform(-method=>"get", -action =>$action) .3312"<div class=\"search\">\n".3313(!$use_pathinfo&&3314$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3315$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3316$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3317$cgi->popup_menu(-name =>'st', -default=>'commit',3318-values=> ['commit','grep','author','committer','pickaxe']) .3319$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3320" search:\n",3321$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3322"<span title=\"Extended regular expression\">".3323$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3324-checked =>$search_use_regexp) .3325"</span>".3326"</div>".3327$cgi->end_form() ."\n";3328}3329}33303331sub git_footer_html {3332my$feed_class='rss_logo';33333334print"<div class=\"page_footer\">\n";3335if(defined$project) {3336my$descr= git_get_project_description($project);3337if(defined$descr) {3338print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3339}33403341my%href_params= get_feed_info();3342if(!%href_params) {3343$feed_class.=' generic';3344}3345$href_params{'-title'} ||='log';33463347foreachmy$formatqw(RSS Atom){3348$href_params{'action'} =lc($format);3349print$cgi->a({-href => href(%href_params),3350-title =>"$href_params{'-title'}$formatfeed",3351-class=>$feed_class},$format)."\n";3352}33533354}else{3355print$cgi->a({-href => href(project=>undef, action=>"opml"),3356-class=>$feed_class},"OPML") ." ";3357print$cgi->a({-href => href(project=>undef, action=>"project_index"),3358-class=>$feed_class},"TXT") ."\n";3359}3360print"</div>\n";# class="page_footer"33613362if(defined$t0&& gitweb_check_feature('timed')) {3363print"<div id=\"generating_info\">\n";3364print'This page took '.3365'<span id="generating_time" class="time_span">'.3366 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3367' seconds </span>'.3368' and '.3369'<span id="generating_cmd">'.3370$number_of_git_cmds.3371'</span> git commands '.3372" to generate.\n";3373print"</div>\n";# class="page_footer"3374}33753376if(defined$site_footer&& -f $site_footer) {3377 insert_file($site_footer);3378}33793380print qq!<script type="text/javascript" src="$javascript"></script>\n!;3381if(defined$action&&3382$actioneq'blame_incremental') {3383print qq!<script type="text/javascript">\n!.3384 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3385 qq!"!. href() .qq!");\n!.3386 qq!</script>\n!;3387}elsif(gitweb_check_feature('javascript-actions')) {3388print qq!<script type="text/javascript">\n!.3389 qq!window.onload = fixLinks;\n!.3390 qq!</script>\n!;3391}33923393print"</body>\n".3394"</html>";3395}33963397# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3398# Example: die_error(404, 'Hash not found')3399# By convention, use the following status codes (as defined in RFC 2616):3400# 400: Invalid or missing CGI parameters, or3401# requested object exists but has wrong type.3402# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3403# this server or project.3404# 404: Requested object/revision/project doesn't exist.3405# 500: The server isn't configured properly, or3406# an internal error occurred (e.g. failed assertions caused by bugs), or3407# an unknown error occurred (e.g. the git binary died unexpectedly).3408# 503: The server is currently unavailable (because it is overloaded,3409# or down for maintenance). Generally, this is a temporary state.3410sub die_error {3411my$status=shift||500;3412my$error= esc_html(shift) ||"Internal Server Error";3413my$extra=shift;34143415my%http_responses= (3416400=>'400 Bad Request',3417403=>'403 Forbidden',3418404=>'404 Not Found',3419500=>'500 Internal Server Error',3420503=>'503 Service Unavailable',3421);3422 git_header_html($http_responses{$status});3423print<<EOF;3424<div class="page_body">3425<br /><br />3426$status-$error3427<br />3428EOF3429if(defined$extra) {3430print"<hr />\n".3431"$extra\n";3432}3433print"</div>\n";34343435 git_footer_html();3436goto DONE_GITWEB;3437}34383439## ----------------------------------------------------------------------3440## functions printing or outputting HTML: navigation34413442sub git_print_page_nav {3443my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3444$extra=''if!defined$extra;# pager or formats34453446my@navs=qw(summary shortlog log commit commitdiff tree);3447if($suppress) {3448@navs=grep{$_ne$suppress}@navs;3449}34503451my%arg=map{$_=> {action=>$_} }@navs;3452if(defined$head) {3453for(qw(commit commitdiff)) {3454$arg{$_}{'hash'} =$head;3455}3456if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3457for(qw(shortlog log)) {3458$arg{$_}{'hash'} =$head;3459}3460}3461}34623463$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3464$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;34653466my@actions= gitweb_get_feature('actions');3467my%repl= (3468'%'=>'%',3469'n'=>$project,# project name3470'f'=>$git_dir,# project path within filesystem3471'h'=>$treehead||'',# current hash ('h' parameter)3472'b'=>$treebase||'',# hash base ('hb' parameter)3473);3474while(@actions) {3475my($label,$link,$pos) =splice(@actions,0,3);3476# insert3477@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3478# munch munch3479$link=~s/%([%nfhb])/$repl{$1}/g;3480$arg{$label}{'_href'} =$link;3481}34823483print"<div class=\"page_nav\">\n".3484(join" | ",3485map{$_eq$current?3486$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3487}@navs);3488print"<br/>\n$extra<br/>\n".3489"</div>\n";3490}34913492sub format_paging_nav {3493my($action,$page,$has_next_link) =@_;3494my$paging_nav;349534963497if($page>0) {3498$paging_nav.=3499$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3500" ⋅ ".3501$cgi->a({-href => href(-replay=>1, page=>$page-1),3502-accesskey =>"p", -title =>"Alt-p"},"prev");3503}else{3504$paging_nav.="first ⋅ prev";3505}35063507if($has_next_link) {3508$paging_nav.=" ⋅ ".3509$cgi->a({-href => href(-replay=>1, page=>$page+1),3510-accesskey =>"n", -title =>"Alt-n"},"next");3511}else{3512$paging_nav.=" ⋅ next";3513}35143515return$paging_nav;3516}35173518## ......................................................................3519## functions printing or outputting HTML: div35203521sub git_print_header_div {3522my($action,$title,$hash,$hash_base) =@_;3523my%args= ();35243525$args{'action'} =$action;3526$args{'hash'} =$hashif$hash;3527$args{'hash_base'} =$hash_baseif$hash_base;35283529print"<div class=\"header\">\n".3530$cgi->a({-href => href(%args), -class=>"title"},3531$title?$title:$action) .3532"\n</div>\n";3533}35343535sub print_local_time {3536print format_local_time(@_);3537}35383539sub format_local_time {3540my$localtime='';3541my%date=@_;3542if($date{'hour_local'} <6) {3543$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3544$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3545}else{3546$localtime.=sprintf(" (%02d:%02d%s)",3547$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3548}35493550return$localtime;3551}35523553# Outputs the author name and date in long form3554sub git_print_authorship {3555my$co=shift;3556my%opts=@_;3557my$tag=$opts{-tag} ||'div';3558my$author=$co->{'author_name'};35593560my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3561print"<$tagclass=\"author_date\">".3562 format_search_author($author,"author", esc_html($author)) .3563" [$ad{'rfc2822'}";3564 print_local_time(%ad)if($opts{-localtime});3565print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3566."</$tag>\n";3567}35683569# Outputs table rows containing the full author or committer information,3570# in the format expected for 'commit' view (& similia).3571# Parameters are a commit hash reference, followed by the list of people3572# to output information for. If the list is empty it defalts to both3573# author and committer.3574sub git_print_authorship_rows {3575my$co=shift;3576# too bad we can't use @people = @_ || ('author', 'committer')3577my@people=@_;3578@people= ('author','committer')unless@people;3579foreachmy$who(@people) {3580my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3581print"<tr><td>$who</td><td>".3582 format_search_author($co->{"${who}_name"},$who,3583 esc_html($co->{"${who}_name"})) ." ".3584 format_search_author($co->{"${who}_email"},$who,3585 esc_html("<".$co->{"${who}_email"} .">")) .3586"</td><td rowspan=\"2\">".3587 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3588"</td></tr>\n".3589"<tr>".3590"<td></td><td>$wd{'rfc2822'}";3591 print_local_time(%wd);3592print"</td>".3593"</tr>\n";3594}3595}35963597sub git_print_page_path {3598my$name=shift;3599my$type=shift;3600my$hb=shift;360136023603print"<div class=\"page_path\">";3604print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3605-title =>'tree root'}, to_utf8("[$project]"));3606print" / ";3607if(defined$name) {3608my@dirname=split'/',$name;3609my$basename=pop@dirname;3610my$fullname='';36113612foreachmy$dir(@dirname) {3613$fullname.= ($fullname?'/':'') .$dir;3614print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3615 hash_base=>$hb),3616-title =>$fullname}, esc_path($dir));3617print" / ";3618}3619if(defined$type&&$typeeq'blob') {3620print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3621 hash_base=>$hb),3622-title =>$name}, esc_path($basename));3623}elsif(defined$type&&$typeeq'tree') {3624print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3625 hash_base=>$hb),3626-title =>$name}, esc_path($basename));3627print" / ";3628}else{3629print esc_path($basename);3630}3631}3632print"<br/></div>\n";3633}36343635sub git_print_log {3636my$log=shift;3637my%opts=@_;36383639if($opts{'-remove_title'}) {3640# remove title, i.e. first line of log3641shift@$log;3642}3643# remove leading empty lines3644while(defined$log->[0] &&$log->[0]eq"") {3645shift@$log;3646}36473648# print log3649my$signoff=0;3650my$empty=0;3651foreachmy$line(@$log) {3652if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3653$signoff=1;3654$empty=0;3655if(!$opts{'-remove_signoff'}) {3656print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3657next;3658}else{3659# remove signoff lines3660next;3661}3662}else{3663$signoff=0;3664}36653666# print only one empty line3667# do not print empty line after signoff3668if($lineeq"") {3669next if($empty||$signoff);3670$empty=1;3671}else{3672$empty=0;3673}36743675print format_log_line_html($line) ."<br/>\n";3676}36773678if($opts{'-final_empty_line'}) {3679# end with single empty line3680print"<br/>\n"unless$empty;3681}3682}36833684# return link target (what link points to)3685sub git_get_link_target {3686my$hash=shift;3687my$link_target;36883689# read link3690open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3691orreturn;3692{3693local$/=undef;3694$link_target= <$fd>;3695}3696close$fd3697orreturn;36983699return$link_target;3700}37013702# given link target, and the directory (basedir) the link is in,3703# return target of link relative to top directory (top tree);3704# return undef if it is not possible (including absolute links).3705sub normalize_link_target {3706my($link_target,$basedir) =@_;37073708# absolute symlinks (beginning with '/') cannot be normalized3709return if(substr($link_target,0,1)eq'/');37103711# normalize link target to path from top (root) tree (dir)3712my$path;3713if($basedir) {3714$path=$basedir.'/'.$link_target;3715}else{3716# we are in top (root) tree (dir)3717$path=$link_target;3718}37193720# remove //, /./, and /../3721my@path_parts;3722foreachmy$part(split('/',$path)) {3723# discard '.' and ''3724next if(!$part||$parteq'.');3725# handle '..'3726if($parteq'..') {3727if(@path_parts) {3728pop@path_parts;3729}else{3730# link leads outside repository (outside top dir)3731return;3732}3733}else{3734push@path_parts,$part;3735}3736}3737$path=join('/',@path_parts);37383739return$path;3740}37413742# print tree entry (row of git_tree), but without encompassing <tr> element3743sub git_print_tree_entry {3744my($t,$basedir,$hash_base,$have_blame) =@_;37453746my%base_key= ();3747$base_key{'hash_base'} =$hash_baseifdefined$hash_base;37483749# The format of a table row is: mode list link. Where mode is3750# the mode of the entry, list is the name of the entry, an href,3751# and link is the action links of the entry.37523753print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3754if(exists$t->{'size'}) {3755print"<td class=\"size\">$t->{'size'}</td>\n";3756}3757if($t->{'type'}eq"blob") {3758print"<td class=\"list\">".3759$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3760 file_name=>"$basedir$t->{'name'}",%base_key),3761-class=>"list"}, esc_path($t->{'name'}));3762if(S_ISLNK(oct$t->{'mode'})) {3763my$link_target= git_get_link_target($t->{'hash'});3764if($link_target) {3765my$norm_target= normalize_link_target($link_target,$basedir);3766if(defined$norm_target) {3767print" -> ".3768$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3769 file_name=>$norm_target),3770-title =>$norm_target}, esc_path($link_target));3771}else{3772print" -> ". esc_path($link_target);3773}3774}3775}3776print"</td>\n";3777print"<td class=\"link\">";3778print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3779 file_name=>"$basedir$t->{'name'}",%base_key)},3780"blob");3781if($have_blame) {3782print" | ".3783$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3784 file_name=>"$basedir$t->{'name'}",%base_key)},3785"blame");3786}3787if(defined$hash_base) {3788print" | ".3789$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3790 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3791"history");3792}3793print" | ".3794$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3795 file_name=>"$basedir$t->{'name'}")},3796"raw");3797print"</td>\n";37983799}elsif($t->{'type'}eq"tree") {3800print"<td class=\"list\">";3801print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3802 file_name=>"$basedir$t->{'name'}",3803%base_key)},3804 esc_path($t->{'name'}));3805print"</td>\n";3806print"<td class=\"link\">";3807print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3808 file_name=>"$basedir$t->{'name'}",3809%base_key)},3810"tree");3811if(defined$hash_base) {3812print" | ".3813$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3814 file_name=>"$basedir$t->{'name'}")},3815"history");3816}3817print"</td>\n";3818}else{3819# unknown object: we can only present history for it3820# (this includes 'commit' object, i.e. submodule support)3821print"<td class=\"list\">".3822 esc_path($t->{'name'}) .3823"</td>\n";3824print"<td class=\"link\">";3825if(defined$hash_base) {3826print$cgi->a({-href => href(action=>"history",3827 hash_base=>$hash_base,3828 file_name=>"$basedir$t->{'name'}")},3829"history");3830}3831print"</td>\n";3832}3833}38343835## ......................................................................3836## functions printing large fragments of HTML38373838# get pre-image filenames for merge (combined) diff3839sub fill_from_file_info {3840my($diff,@parents) =@_;38413842$diff->{'from_file'} = [ ];3843$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3844for(my$i=0;$i<$diff->{'nparents'};$i++) {3845if($diff->{'status'}[$i]eq'R'||3846$diff->{'status'}[$i]eq'C') {3847$diff->{'from_file'}[$i] =3848 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3849}3850}38513852return$diff;3853}38543855# is current raw difftree line of file deletion3856sub is_deleted {3857my$diffinfo=shift;38583859return$diffinfo->{'to_id'}eq('0' x 40);3860}38613862# does patch correspond to [previous] difftree raw line3863# $diffinfo - hashref of parsed raw diff format3864# $patchinfo - hashref of parsed patch diff format3865# (the same keys as in $diffinfo)3866sub is_patch_split {3867my($diffinfo,$patchinfo) =@_;38683869returndefined$diffinfo&&defined$patchinfo3870&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3871}387238733874sub git_difftree_body {3875my($difftree,$hash,@parents) =@_;3876my($parent) =$parents[0];3877my$have_blame= gitweb_check_feature('blame');3878print"<div class=\"list_head\">\n";3879if($#{$difftree} >10) {3880print(($#{$difftree} +1) ." files changed:\n");3881}3882print"</div>\n";38833884print"<table class=\"".3885(@parents>1?"combined ":"") .3886"diff_tree\">\n";38873888# header only for combined diff in 'commitdiff' view3889my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3890if($has_header) {3891# table header3892print"<thead><tr>\n".3893"<th></th><th></th>\n";# filename, patchN link3894for(my$i=0;$i<@parents;$i++) {3895my$par=$parents[$i];3896print"<th>".3897$cgi->a({-href => href(action=>"commitdiff",3898 hash=>$hash, hash_parent=>$par),3899-title =>'commitdiff to parent number '.3900($i+1) .': '.substr($par,0,7)},3901$i+1) .3902" </th>\n";3903}3904print"</tr></thead>\n<tbody>\n";3905}39063907my$alternate=1;3908my$patchno=0;3909foreachmy$line(@{$difftree}) {3910my$diff= parsed_difftree_line($line);39113912if($alternate) {3913print"<tr class=\"dark\">\n";3914}else{3915print"<tr class=\"light\">\n";3916}3917$alternate^=1;39183919if(exists$diff->{'nparents'}) {# combined diff39203921 fill_from_file_info($diff,@parents)3922unlessexists$diff->{'from_file'};39233924if(!is_deleted($diff)) {3925# file exists in the result (child) commit3926print"<td>".3927$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3928 file_name=>$diff->{'to_file'},3929 hash_base=>$hash),3930-class=>"list"}, esc_path($diff->{'to_file'})) .3931"</td>\n";3932}else{3933print"<td>".3934 esc_path($diff->{'to_file'}) .3935"</td>\n";3936}39373938if($actioneq'commitdiff') {3939# link to patch3940$patchno++;3941print"<td class=\"link\">".3942$cgi->a({-href =>"#patch$patchno"},"patch") .3943" | ".3944"</td>\n";3945}39463947my$has_history=0;3948my$not_deleted=0;3949for(my$i=0;$i<$diff->{'nparents'};$i++) {3950my$hash_parent=$parents[$i];3951my$from_hash=$diff->{'from_id'}[$i];3952my$from_path=$diff->{'from_file'}[$i];3953my$status=$diff->{'status'}[$i];39543955$has_history||= ($statusne'A');3956$not_deleted||= ($statusne'D');39573958if($statuseq'A') {3959print"<td class=\"link\"align=\"right\"> | </td>\n";3960}elsif($statuseq'D') {3961print"<td class=\"link\">".3962$cgi->a({-href => href(action=>"blob",3963 hash_base=>$hash,3964 hash=>$from_hash,3965 file_name=>$from_path)},3966"blob". ($i+1)) .3967" | </td>\n";3968}else{3969if($diff->{'to_id'}eq$from_hash) {3970print"<td class=\"link nochange\">";3971}else{3972print"<td class=\"link\">";3973}3974print$cgi->a({-href => href(action=>"blobdiff",3975 hash=>$diff->{'to_id'},3976 hash_parent=>$from_hash,3977 hash_base=>$hash,3978 hash_parent_base=>$hash_parent,3979 file_name=>$diff->{'to_file'},3980 file_parent=>$from_path)},3981"diff". ($i+1)) .3982" | </td>\n";3983}3984}39853986print"<td class=\"link\">";3987if($not_deleted) {3988print$cgi->a({-href => href(action=>"blob",3989 hash=>$diff->{'to_id'},3990 file_name=>$diff->{'to_file'},3991 hash_base=>$hash)},3992"blob");3993print" | "if($has_history);3994}3995if($has_history) {3996print$cgi->a({-href => href(action=>"history",3997 file_name=>$diff->{'to_file'},3998 hash_base=>$hash)},3999"history");4000}4001print"</td>\n";40024003print"</tr>\n";4004next;# instead of 'else' clause, to avoid extra indent4005}4006# else ordinary diff40074008my($to_mode_oct,$to_mode_str,$to_file_type);4009my($from_mode_oct,$from_mode_str,$from_file_type);4010if($diff->{'to_mode'}ne('0' x 6)) {4011$to_mode_oct=oct$diff->{'to_mode'};4012if(S_ISREG($to_mode_oct)) {# only for regular file4013$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4014}4015$to_file_type= file_type($diff->{'to_mode'});4016}4017if($diff->{'from_mode'}ne('0' x 6)) {4018$from_mode_oct=oct$diff->{'from_mode'};4019if(S_ISREG($to_mode_oct)) {# only for regular file4020$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4021}4022$from_file_type= file_type($diff->{'from_mode'});4023}40244025if($diff->{'status'}eq"A") {# created4026my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4027$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4028$mode_chng.="]</span>";4029print"<td>";4030print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4031 hash_base=>$hash, file_name=>$diff->{'file'}),4032-class=>"list"}, esc_path($diff->{'file'}));4033print"</td>\n";4034print"<td>$mode_chng</td>\n";4035print"<td class=\"link\">";4036if($actioneq'commitdiff') {4037# link to patch4038$patchno++;4039print$cgi->a({-href =>"#patch$patchno"},"patch");4040print" | ";4041}4042print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4043 hash_base=>$hash, file_name=>$diff->{'file'})},4044"blob");4045print"</td>\n";40464047}elsif($diff->{'status'}eq"D") {# deleted4048my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4049print"<td>";4050print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4051 hash_base=>$parent, file_name=>$diff->{'file'}),4052-class=>"list"}, esc_path($diff->{'file'}));4053print"</td>\n";4054print"<td>$mode_chng</td>\n";4055print"<td class=\"link\">";4056if($actioneq'commitdiff') {4057# link to patch4058$patchno++;4059print$cgi->a({-href =>"#patch$patchno"},"patch");4060print" | ";4061}4062print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4063 hash_base=>$parent, file_name=>$diff->{'file'})},4064"blob") ." | ";4065if($have_blame) {4066print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4067 file_name=>$diff->{'file'})},4068"blame") ." | ";4069}4070print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4071 file_name=>$diff->{'file'})},4072"history");4073print"</td>\n";40744075}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4076my$mode_chnge="";4077if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4078$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4079if($from_file_typene$to_file_type) {4080$mode_chnge.=" from$from_file_typeto$to_file_type";4081}4082if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4083if($from_mode_str&&$to_mode_str) {4084$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4085}elsif($to_mode_str) {4086$mode_chnge.=" mode:$to_mode_str";4087}4088}4089$mode_chnge.="]</span>\n";4090}4091print"<td>";4092print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4093 hash_base=>$hash, file_name=>$diff->{'file'}),4094-class=>"list"}, esc_path($diff->{'file'}));4095print"</td>\n";4096print"<td>$mode_chnge</td>\n";4097print"<td class=\"link\">";4098if($actioneq'commitdiff') {4099# link to patch4100$patchno++;4101print$cgi->a({-href =>"#patch$patchno"},"patch") .4102" | ";4103}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4104# "commit" view and modified file (not onlu mode changed)4105print$cgi->a({-href => href(action=>"blobdiff",4106 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4107 hash_base=>$hash, hash_parent_base=>$parent,4108 file_name=>$diff->{'file'})},4109"diff") .4110" | ";4111}4112print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4113 hash_base=>$hash, file_name=>$diff->{'file'})},4114"blob") ." | ";4115if($have_blame) {4116print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4117 file_name=>$diff->{'file'})},4118"blame") ." | ";4119}4120print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4121 file_name=>$diff->{'file'})},4122"history");4123print"</td>\n";41244125}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4126my%status_name= ('R'=>'moved','C'=>'copied');4127my$nstatus=$status_name{$diff->{'status'}};4128my$mode_chng="";4129if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4130# mode also for directories, so we cannot use $to_mode_str4131$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4132}4133print"<td>".4134$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4135 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4136-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4137"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4138$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4139 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4140-class=>"list"}, esc_path($diff->{'from_file'})) .4141" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4142"<td class=\"link\">";4143if($actioneq'commitdiff') {4144# link to patch4145$patchno++;4146print$cgi->a({-href =>"#patch$patchno"},"patch") .4147" | ";4148}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4149# "commit" view and modified file (not only pure rename or copy)4150print$cgi->a({-href => href(action=>"blobdiff",4151 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4152 hash_base=>$hash, hash_parent_base=>$parent,4153 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4154"diff") .4155" | ";4156}4157print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4158 hash_base=>$parent, file_name=>$diff->{'to_file'})},4159"blob") ." | ";4160if($have_blame) {4161print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4162 file_name=>$diff->{'to_file'})},4163"blame") ." | ";4164}4165print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4166 file_name=>$diff->{'to_file'})},4167"history");4168print"</td>\n";41694170}# we should not encounter Unmerged (U) or Unknown (X) status4171print"</tr>\n";4172}4173print"</tbody>"if$has_header;4174print"</table>\n";4175}41764177sub git_patchset_body {4178my($fd,$difftree,$hash,@hash_parents) =@_;4179my($hash_parent) =$hash_parents[0];41804181my$is_combined= (@hash_parents>1);4182my$patch_idx=0;4183my$patch_number=0;4184my$patch_line;4185my$diffinfo;4186my$to_name;4187my(%from,%to);41884189print"<div class=\"patchset\">\n";41904191# skip to first patch4192while($patch_line= <$fd>) {4193chomp$patch_line;41944195last if($patch_line=~m/^diff /);4196}41974198 PATCH:4199while($patch_line) {42004201# parse "git diff" header line4202if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4203# $1 is from_name, which we do not use4204$to_name= unquote($2);4205$to_name=~s!^b/!!;4206}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4207# $1 is 'cc' or 'combined', which we do not use4208$to_name= unquote($2);4209}else{4210$to_name=undef;4211}42124213# check if current patch belong to current raw line4214# and parse raw git-diff line if needed4215if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4216# this is continuation of a split patch4217print"<div class=\"patch cont\">\n";4218}else{4219# advance raw git-diff output if needed4220$patch_idx++ifdefined$diffinfo;42214222# read and prepare patch information4223$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42244225# compact combined diff output can have some patches skipped4226# find which patch (using pathname of result) we are at now;4227if($is_combined) {4228while($to_namene$diffinfo->{'to_file'}) {4229print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4230 format_diff_cc_simplified($diffinfo,@hash_parents) .4231"</div>\n";# class="patch"42324233$patch_idx++;4234$patch_number++;42354236last if$patch_idx>$#$difftree;4237$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4238}4239}42404241# modifies %from, %to hashes4242 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);42434244# this is first patch for raw difftree line with $patch_idx index4245# we index @$difftree array from 0, but number patches from 14246print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4247}42484249# git diff header4250#assert($patch_line =~ m/^diff /) if DEBUG;4251#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4252$patch_number++;4253# print "git diff" header4254print format_git_diff_header_line($patch_line,$diffinfo,4255 \%from, \%to);42564257# print extended diff header4258print"<div class=\"diff extended_header\">\n";4259 EXTENDED_HEADER:4260while($patch_line= <$fd>) {4261chomp$patch_line;42624263last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);42644265print format_extended_diff_header_line($patch_line,$diffinfo,4266 \%from, \%to);4267}4268print"</div>\n";# class="diff extended_header"42694270# from-file/to-file diff header4271if(!$patch_line) {4272print"</div>\n";# class="patch"4273last PATCH;4274}4275next PATCH if($patch_line=~m/^diff /);4276#assert($patch_line =~ m/^---/) if DEBUG;42774278my$last_patch_line=$patch_line;4279$patch_line= <$fd>;4280chomp$patch_line;4281#assert($patch_line =~ m/^\+\+\+/) if DEBUG;42824283print format_diff_from_to_header($last_patch_line,$patch_line,4284$diffinfo, \%from, \%to,4285@hash_parents);42864287# the patch itself4288 LINE:4289while($patch_line= <$fd>) {4290chomp$patch_line;42914292next PATCH if($patch_line=~m/^diff /);42934294print format_diff_line($patch_line, \%from, \%to);4295}42964297}continue{4298print"</div>\n";# class="patch"4299}43004301# for compact combined (--cc) format, with chunk and patch simpliciaction4302# patchset might be empty, but there might be unprocessed raw lines4303for(++$patch_idxif$patch_number>0;4304$patch_idx<@$difftree;4305++$patch_idx) {4306# read and prepare patch information4307$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);43084309# generate anchor for "patch" links in difftree / whatchanged part4310print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4311 format_diff_cc_simplified($diffinfo,@hash_parents) .4312"</div>\n";# class="patch"43134314$patch_number++;4315}43164317if($patch_number==0) {4318if(@hash_parents>1) {4319print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4320}else{4321print"<div class=\"diff nodifferences\">No differences found</div>\n";4322}4323}43244325print"</div>\n";# class="patchset"4326}43274328# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43294330# fills project list info (age, description, owner, forks) for each4331# project in the list, removing invalid projects from returned list4332# NOTE: modifies $projlist, but does not remove entries from it4333sub fill_project_list_info {4334my($projlist,$check_forks) =@_;4335my@projects;43364337my$show_ctags= gitweb_check_feature('ctags');4338 PROJECT:4339foreachmy$pr(@$projlist) {4340my(@activity) = git_get_last_activity($pr->{'path'});4341unless(@activity) {4342next PROJECT;4343}4344($pr->{'age'},$pr->{'age_string'}) =@activity;4345if(!defined$pr->{'descr'}) {4346my$descr= git_get_project_description($pr->{'path'}) ||"";4347$descr= to_utf8($descr);4348$pr->{'descr_long'} =$descr;4349$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4350}4351if(!defined$pr->{'owner'}) {4352$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4353}4354if($check_forks) {4355my$pname=$pr->{'path'};4356if(($pname=~s/\.git$//) &&4357($pname!~/\/$/) &&4358(-d "$projectroot/$pname")) {4359$pr->{'forks'} ="-d$projectroot/$pname";4360}else{4361$pr->{'forks'} =0;4362}4363}4364$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4365push@projects,$pr;4366}43674368return@projects;4369}43704371# print 'sort by' <th> element, generating 'sort by $name' replay link4372# if that order is not selected4373sub print_sort_th {4374print format_sort_th(@_);4375}43764377sub format_sort_th {4378my($name,$order,$header) =@_;4379my$sort_th="";4380$header||=ucfirst($name);43814382if($ordereq$name) {4383$sort_th.="<th>$header</th>\n";4384}else{4385$sort_th.="<th>".4386$cgi->a({-href => href(-replay=>1, order=>$name),4387-class=>"header"},$header) .4388"</th>\n";4389}43904391return$sort_th;4392}43934394sub git_project_list_body {4395# actually uses global variable $project4396my($projlist,$order,$from,$to,$extra,$no_header) =@_;43974398my$check_forks= gitweb_check_feature('forks');4399my@projects= fill_project_list_info($projlist,$check_forks);44004401$order||=$default_projects_order;4402$from=0unlessdefined$from;4403$to=$#projectsif(!defined$to||$#projects<$to);44044405my%order_info= (4406 project => { key =>'path', type =>'str'},4407 descr => { key =>'descr_long', type =>'str'},4408 owner => { key =>'owner', type =>'str'},4409 age => { key =>'age', type =>'num'}4410);4411my$oi=$order_info{$order};4412if($oi->{'type'}eq'str') {4413@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4414}else{4415@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4416}44174418my$show_ctags= gitweb_check_feature('ctags');4419if($show_ctags) {4420my%ctags;4421foreachmy$p(@projects) {4422foreachmy$ct(keys%{$p->{'ctags'}}) {4423$ctags{$ct} +=$p->{'ctags'}->{$ct};4424}4425}4426my$cloud= git_populate_project_tagcloud(\%ctags);4427print git_show_project_tagcloud($cloud,64);4428}44294430print"<table class=\"project_list\">\n";4431unless($no_header) {4432print"<tr>\n";4433if($check_forks) {4434print"<th></th>\n";4435}4436 print_sort_th('project',$order,'Project');4437 print_sort_th('descr',$order,'Description');4438 print_sort_th('owner',$order,'Owner');4439 print_sort_th('age',$order,'Last Change');4440print"<th></th>\n".# for links4441"</tr>\n";4442}4443my$alternate=1;4444my$tagfilter=$cgi->param('by_tag');4445for(my$i=$from;$i<=$to;$i++) {4446my$pr=$projects[$i];44474448next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4449next if$searchtextand not$pr->{'path'} =~/$searchtext/4450and not$pr->{'descr_long'} =~/$searchtext/;4451# Weed out forks or non-matching entries of search4452if($check_forks) {4453my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4454$forkbase="^$forkbase"if$forkbase;4455next ifnot$searchtextand not$tagfilterand$show_ctags4456and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4457}44584459if($alternate) {4460print"<tr class=\"dark\">\n";4461}else{4462print"<tr class=\"light\">\n";4463}4464$alternate^=1;4465if($check_forks) {4466print"<td>";4467if($pr->{'forks'}) {4468print"<!--$pr->{'forks'} -->\n";4469print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4470}4471print"</td>\n";4472}4473print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4474-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4475"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4476-class=>"list", -title =>$pr->{'descr_long'}},4477 esc_html($pr->{'descr'})) ."</td>\n".4478"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4479print"<td class=\"". age_class($pr->{'age'}) ."\">".4480(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4481"<td class=\"link\">".4482$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4483$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4484$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4485$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4486($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4487"</td>\n".4488"</tr>\n";4489}4490if(defined$extra) {4491print"<tr>\n";4492if($check_forks) {4493print"<td></td>\n";4494}4495print"<td colspan=\"5\">$extra</td>\n".4496"</tr>\n";4497}4498print"</table>\n";4499}45004501sub git_log_body {4502# uses global variable $project4503my($commitlist,$from,$to,$refs,$extra) =@_;45044505$from=0unlessdefined$from;4506$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45074508for(my$i=0;$i<=$to;$i++) {4509my%co= %{$commitlist->[$i]};4510next if!%co;4511my$commit=$co{'id'};4512my$ref= format_ref_marker($refs,$commit);4513my%ad= parse_date($co{'author_epoch'});4514 git_print_header_div('commit',4515"<span class=\"age\">$co{'age_string'}</span>".4516 esc_html($co{'title'}) .$ref,4517$commit);4518print"<div class=\"title_text\">\n".4519"<div class=\"log_link\">\n".4520$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4521" | ".4522$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4523" | ".4524$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4525"<br/>\n".4526"</div>\n";4527 git_print_authorship(\%co, -tag =>'span');4528print"<br/>\n</div>\n";45294530print"<div class=\"log_body\">\n";4531 git_print_log($co{'comment'}, -final_empty_line=>1);4532print"</div>\n";4533}4534if($extra) {4535print"<div class=\"page_nav\">\n";4536print"$extra\n";4537print"</div>\n";4538}4539}45404541sub git_shortlog_body {4542# uses global variable $project4543my($commitlist,$from,$to,$refs,$extra) =@_;45444545$from=0unlessdefined$from;4546$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45474548print"<table class=\"shortlog\">\n";4549my$alternate=1;4550for(my$i=$from;$i<=$to;$i++) {4551my%co= %{$commitlist->[$i]};4552my$commit=$co{'id'};4553my$ref= format_ref_marker($refs,$commit);4554if($alternate) {4555print"<tr class=\"dark\">\n";4556}else{4557print"<tr class=\"light\">\n";4558}4559$alternate^=1;4560# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4561print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4562 format_author_html('td', \%co,10) ."<td>";4563print format_subject_html($co{'title'},$co{'title_short'},4564 href(action=>"commit", hash=>$commit),$ref);4565print"</td>\n".4566"<td class=\"link\">".4567$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4568$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4569$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4570my$snapshot_links= format_snapshot_links($commit);4571if(defined$snapshot_links) {4572print" | ".$snapshot_links;4573}4574print"</td>\n".4575"</tr>\n";4576}4577if(defined$extra) {4578print"<tr>\n".4579"<td colspan=\"4\">$extra</td>\n".4580"</tr>\n";4581}4582print"</table>\n";4583}45844585sub git_history_body {4586# Warning: assumes constant type (blob or tree) during history4587my($commitlist,$from,$to,$refs,$extra,4588$file_name,$file_hash,$ftype) =@_;45894590$from=0unlessdefined$from;4591$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});45924593print"<table class=\"history\">\n";4594my$alternate=1;4595for(my$i=$from;$i<=$to;$i++) {4596my%co= %{$commitlist->[$i]};4597if(!%co) {4598next;4599}4600my$commit=$co{'id'};46014602my$ref= format_ref_marker($refs,$commit);46034604if($alternate) {4605print"<tr class=\"dark\">\n";4606}else{4607print"<tr class=\"light\">\n";4608}4609$alternate^=1;4610print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4611# shortlog: format_author_html('td', \%co, 10)4612 format_author_html('td', \%co,15,3) ."<td>";4613# originally git_history used chop_str($co{'title'}, 50)4614print format_subject_html($co{'title'},$co{'title_short'},4615 href(action=>"commit", hash=>$commit),$ref);4616print"</td>\n".4617"<td class=\"link\">".4618$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4619$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");46204621if($ftypeeq'blob') {4622my$blob_current=$file_hash;4623my$blob_parent= git_get_hash_by_path($commit,$file_name);4624if(defined$blob_current&&defined$blob_parent&&4625$blob_currentne$blob_parent) {4626print" | ".4627$cgi->a({-href => href(action=>"blobdiff",4628 hash=>$blob_current, hash_parent=>$blob_parent,4629 hash_base=>$hash_base, hash_parent_base=>$commit,4630 file_name=>$file_name)},4631"diff to current");4632}4633}4634print"</td>\n".4635"</tr>\n";4636}4637if(defined$extra) {4638print"<tr>\n".4639"<td colspan=\"4\">$extra</td>\n".4640"</tr>\n";4641}4642print"</table>\n";4643}46444645sub git_tags_body {4646# uses global variable $project4647my($taglist,$from,$to,$extra) =@_;4648$from=0unlessdefined$from;4649$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);46504651print"<table class=\"tags\">\n";4652my$alternate=1;4653for(my$i=$from;$i<=$to;$i++) {4654my$entry=$taglist->[$i];4655my%tag=%$entry;4656my$comment=$tag{'subject'};4657my$comment_short;4658if(defined$comment) {4659$comment_short= chop_str($comment,30,5);4660}4661if($alternate) {4662print"<tr class=\"dark\">\n";4663}else{4664print"<tr class=\"light\">\n";4665}4666$alternate^=1;4667if(defined$tag{'age'}) {4668print"<td><i>$tag{'age'}</i></td>\n";4669}else{4670print"<td></td>\n";4671}4672print"<td>".4673$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4674-class=>"list name"}, esc_html($tag{'name'})) .4675"</td>\n".4676"<td>";4677if(defined$comment) {4678print format_subject_html($comment,$comment_short,4679 href(action=>"tag", hash=>$tag{'id'}));4680}4681print"</td>\n".4682"<td class=\"selflink\">";4683if($tag{'type'}eq"tag") {4684print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4685}else{4686print" ";4687}4688print"</td>\n".4689"<td class=\"link\">"." | ".4690$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4691if($tag{'reftype'}eq"commit") {4692print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4693" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4694}elsif($tag{'reftype'}eq"blob") {4695print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4696}4697print"</td>\n".4698"</tr>";4699}4700if(defined$extra) {4701print"<tr>\n".4702"<td colspan=\"5\">$extra</td>\n".4703"</tr>\n";4704}4705print"</table>\n";4706}47074708sub git_heads_body {4709# uses global variable $project4710my($headlist,$head,$from,$to,$extra) =@_;4711$from=0unlessdefined$from;4712$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);47134714print"<table class=\"heads\">\n";4715my$alternate=1;4716for(my$i=$from;$i<=$to;$i++) {4717my$entry=$headlist->[$i];4718my%ref=%$entry;4719my$curr=$ref{'id'}eq$head;4720if($alternate) {4721print"<tr class=\"dark\">\n";4722}else{4723print"<tr class=\"light\">\n";4724}4725$alternate^=1;4726print"<td><i>$ref{'age'}</i></td>\n".4727($curr?"<td class=\"current_head\">":"<td>") .4728$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4729-class=>"list name"},esc_html($ref{'name'})) .4730"</td>\n".4731"<td class=\"link\">".4732$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4733$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4734$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4735"</td>\n".4736"</tr>";4737}4738if(defined$extra) {4739print"<tr>\n".4740"<td colspan=\"3\">$extra</td>\n".4741"</tr>\n";4742}4743print"</table>\n";4744}47454746sub git_search_grep_body {4747my($commitlist,$from,$to,$extra) =@_;4748$from=0unlessdefined$from;4749$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47504751print"<table class=\"commit_search\">\n";4752my$alternate=1;4753for(my$i=$from;$i<=$to;$i++) {4754my%co= %{$commitlist->[$i]};4755if(!%co) {4756next;4757}4758my$commit=$co{'id'};4759if($alternate) {4760print"<tr class=\"dark\">\n";4761}else{4762print"<tr class=\"light\">\n";4763}4764$alternate^=1;4765print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4766 format_author_html('td', \%co,15,5) .4767"<td>".4768$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4769-class=>"list subject"},4770 chop_and_escape_str($co{'title'},50) ."<br/>");4771my$comment=$co{'comment'};4772foreachmy$line(@$comment) {4773if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4774my($lead,$match,$trail) = ($1,$2,$3);4775$match= chop_str($match,70,5,'center');4776my$contextlen=int((80-length($match))/2);4777$contextlen=30if($contextlen>30);4778$lead= chop_str($lead,$contextlen,10,'left');4779$trail= chop_str($trail,$contextlen,10,'right');47804781$lead= esc_html($lead);4782$match= esc_html($match);4783$trail= esc_html($trail);47844785print"$lead<span class=\"match\">$match</span>$trail<br />";4786}4787}4788print"</td>\n".4789"<td class=\"link\">".4790$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4791" | ".4792$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4793" | ".4794$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4795print"</td>\n".4796"</tr>\n";4797}4798if(defined$extra) {4799print"<tr>\n".4800"<td colspan=\"3\">$extra</td>\n".4801"</tr>\n";4802}4803print"</table>\n";4804}48054806## ======================================================================4807## ======================================================================4808## actions48094810sub git_project_list {4811my$order=$input_params{'order'};4812if(defined$order&&$order!~m/none|project|descr|owner|age/) {4813 die_error(400,"Unknown order parameter");4814}48154816my@list= git_get_projects_list();4817if(!@list) {4818 die_error(404,"No projects found");4819}48204821 git_header_html();4822if(defined$home_text&& -f $home_text) {4823print"<div class=\"index_include\">\n";4824 insert_file($home_text);4825print"</div>\n";4826}4827print$cgi->startform(-method=>"get") .4828"<p class=\"projsearch\">Search:\n".4829$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4830"</p>".4831$cgi->end_form() ."\n";4832 git_project_list_body(\@list,$order);4833 git_footer_html();4834}48354836sub git_forks {4837my$order=$input_params{'order'};4838if(defined$order&&$order!~m/none|project|descr|owner|age/) {4839 die_error(400,"Unknown order parameter");4840}48414842my@list= git_get_projects_list($project);4843if(!@list) {4844 die_error(404,"No forks found");4845}48464847 git_header_html();4848 git_print_page_nav('','');4849 git_print_header_div('summary',"$projectforks");4850 git_project_list_body(\@list,$order);4851 git_footer_html();4852}48534854sub git_project_index {4855my@projects= git_get_projects_list($project);48564857print$cgi->header(4858-type =>'text/plain',4859-charset =>'utf-8',4860-content_disposition =>'inline; filename="index.aux"');48614862foreachmy$pr(@projects) {4863if(!exists$pr->{'owner'}) {4864$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4865}48664867my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4868# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4869$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4870$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4871$path=~s/ /\+/g;4872$owner=~s/ /\+/g;48734874print"$path$owner\n";4875}4876}48774878sub git_summary {4879my$descr= git_get_project_description($project) ||"none";4880my%co= parse_commit("HEAD");4881my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4882my$head=$co{'id'};48834884my$owner= git_get_project_owner($project);48854886my$refs= git_get_references();4887# These get_*_list functions return one more to allow us to see if4888# there are more ...4889my@taglist= git_get_tags_list(16);4890my@headlist= git_get_heads_list(16);4891my@forklist;4892my$check_forks= gitweb_check_feature('forks');48934894if($check_forks) {4895@forklist= git_get_projects_list($project);4896}48974898 git_header_html();4899 git_print_page_nav('summary','',$head);49004901print"<div class=\"title\"> </div>\n";4902print"<table class=\"projects_list\">\n".4903"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4904"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4905if(defined$cd{'rfc2822'}) {4906print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4907}49084909# use per project git URL list in $projectroot/$project/cloneurl4910# or make project git URL from git base URL and project name4911my$url_tag="URL";4912my@url_list= git_get_project_url_list($project);4913@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4914foreachmy$git_url(@url_list) {4915next unless$git_url;4916print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4917$url_tag="";4918}49194920# Tag cloud4921my$show_ctags= gitweb_check_feature('ctags');4922if($show_ctags) {4923my$ctags= git_get_project_ctags($project);4924my$cloud= git_populate_project_tagcloud($ctags);4925print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4926print"</td>\n<td>"unless%$ctags;4927print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4928print"</td>\n<td>"if%$ctags;4929print git_show_project_tagcloud($cloud,48);4930print"</td></tr>";4931}49324933print"</table>\n";49344935# If XSS prevention is on, we don't include README.html.4936# TODO: Allow a readme in some safe format.4937if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4938print"<div class=\"title\">readme</div>\n".4939"<div class=\"readme\">\n";4940 insert_file("$projectroot/$project/README.html");4941print"\n</div>\n";# class="readme"4942}49434944# we need to request one more than 16 (0..15) to check if4945# those 16 are all4946my@commitlist=$head? parse_commits($head,17) : ();4947if(@commitlist) {4948 git_print_header_div('shortlog');4949 git_shortlog_body(\@commitlist,0,15,$refs,4950$#commitlist<=15?undef:4951$cgi->a({-href => href(action=>"shortlog")},"..."));4952}49534954if(@taglist) {4955 git_print_header_div('tags');4956 git_tags_body(\@taglist,0,15,4957$#taglist<=15?undef:4958$cgi->a({-href => href(action=>"tags")},"..."));4959}49604961if(@headlist) {4962 git_print_header_div('heads');4963 git_heads_body(\@headlist,$head,0,15,4964$#headlist<=15?undef:4965$cgi->a({-href => href(action=>"heads")},"..."));4966}49674968if(@forklist) {4969 git_print_header_div('forks');4970 git_project_list_body(\@forklist,'age',0,15,4971$#forklist<=15?undef:4972$cgi->a({-href => href(action=>"forks")},"..."),4973'no_header');4974}49754976 git_footer_html();4977}49784979sub git_tag {4980my$head= git_get_head_hash($project);4981 git_header_html();4982 git_print_page_nav('','',$head,undef,$head);4983my%tag= parse_tag($hash);49844985if(!%tag) {4986 die_error(404,"Unknown tag object");4987}49884989 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4990print"<div class=\"title_text\">\n".4991"<table class=\"object_header\">\n".4992"<tr>\n".4993"<td>object</td>\n".4994"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4995$tag{'object'}) ."</td>\n".4996"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4997$tag{'type'}) ."</td>\n".4998"</tr>\n";4999if(defined($tag{'author'})) {5000 git_print_authorship_rows(\%tag,'author');5001}5002print"</table>\n\n".5003"</div>\n";5004print"<div class=\"page_body\">";5005my$comment=$tag{'comment'};5006foreachmy$line(@$comment) {5007chomp$line;5008print esc_html($line, -nbsp=>1) ."<br/>\n";5009}5010print"</div>\n";5011 git_footer_html();5012}50135014sub git_blame_common {5015my$format=shift||'porcelain';5016if($formateq'porcelain'&&$cgi->param('js')) {5017$format='incremental';5018$action='blame_incremental';# for page title etc5019}50205021# permissions5022 gitweb_check_feature('blame')5023or die_error(403,"Blame view not allowed");50245025# error checking5026 die_error(400,"No file name given")unless$file_name;5027$hash_base||= git_get_head_hash($project);5028 die_error(404,"Couldn't find base commit")unless$hash_base;5029my%co= parse_commit($hash_base)5030or die_error(404,"Commit not found");5031my$ftype="blob";5032if(!defined$hash) {5033$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5034or die_error(404,"Error looking up file");5035}else{5036$ftype= git_get_type($hash);5037if($ftype!~"blob") {5038 die_error(400,"Object is not a blob");5039}5040}50415042my$fd;5043if($formateq'incremental') {5044# get file contents (as base)5045open$fd,"-|", git_cmd(),'cat-file','blob',$hash5046or die_error(500,"Open git-cat-file failed");5047}elsif($formateq'data') {5048# run git-blame --incremental5049open$fd,"-|", git_cmd(),"blame","--incremental",5050$hash_base,"--",$file_name5051or die_error(500,"Open git-blame --incremental failed");5052}else{5053# run git-blame --porcelain5054open$fd,"-|", git_cmd(),"blame",'-p',5055$hash_base,'--',$file_name5056or die_error(500,"Open git-blame --porcelain failed");5057}50585059# incremental blame data returns early5060if($formateq'data') {5061print$cgi->header(5062-type=>"text/plain", -charset =>"utf-8",5063-status=>"200 OK");5064local$| =1;# output autoflush5065printwhile<$fd>;5066close$fd5067or print"ERROR$!\n";50685069print'END';5070if(defined$t0&& gitweb_check_feature('timed')) {5071print' '.5072 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5073' '.$number_of_git_cmds;5074}5075print"\n";50765077return;5078}50795080# page header5081 git_header_html();5082my$formats_nav=5083$cgi->a({-href => href(action=>"blob", -replay=>1)},5084"blob") .5085" | ";5086if($formateq'incremental') {5087$formats_nav.=5088$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5089"blame") ." (non-incremental)";5090}else{5091$formats_nav.=5092$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5093"blame") ." (incremental)";5094}5095$formats_nav.=5096" | ".5097$cgi->a({-href => href(action=>"history", -replay=>1)},5098"history") .5099" | ".5100$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5101"HEAD");5102 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5103 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5104 git_print_page_path($file_name,$ftype,$hash_base);51055106# page body5107if($formateq'incremental') {5108print"<noscript>\n<div class=\"error\"><center><b>\n".5109"This page requires JavaScript to run.\nUse ".5110$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5111'this page').5112" instead.\n".5113"</b></center></div>\n</noscript>\n";51145115print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5116}51175118print qq!<div class="page_body">\n!;5119print qq!<div id="progress_info">.../ ...</div>\n!5120if($formateq'incremental');5121print qq!<table id="blame_table"class="blame" width="100%">\n!.5122#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5123 qq!<thead>\n!.5124 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5125 qq!</thead>\n!.5126 qq!<tbody>\n!;51275128my@rev_color=qw(light dark);5129my$num_colors=scalar(@rev_color);5130my$current_color=0;51315132if($formateq'incremental') {5133my$color_class=$rev_color[$current_color];51345135#contents of a file5136my$linenr=0;5137 LINE:5138while(my$line= <$fd>) {5139chomp$line;5140$linenr++;51415142print qq!<tr id="l$linenr"class="$color_class">!.5143 qq!<td class="sha1"><a href=""> </a></td>!.5144 qq!<td class="linenr">!.5145 qq!<a class="linenr" href="">$linenr</a></td>!;5146print qq!<td class="pre">! . esc_html($line) ."</td>\n";5147print qq!</tr>\n!;5148}51495150}else{# porcelain, i.e. ordinary blame5151my%metainfo= ();# saves information about commits51525153# blame data5154 LINE:5155while(my$line= <$fd>) {5156chomp$line;5157# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5158# no <lines in group> for subsequent lines in group of lines5159my($full_rev,$orig_lineno,$lineno,$group_size) =5160($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5161if(!exists$metainfo{$full_rev}) {5162$metainfo{$full_rev} = {'nprevious'=>0};5163}5164my$meta=$metainfo{$full_rev};5165my$data;5166while($data= <$fd>) {5167chomp$data;5168last if($data=~s/^\t//);# contents of line5169if($data=~/^(\S+)(?: (.*))?$/) {5170$meta->{$1} =$2unlessexists$meta->{$1};5171}5172if($data=~/^previous /) {5173$meta->{'nprevious'}++;5174}5175}5176my$short_rev=substr($full_rev,0,8);5177my$author=$meta->{'author'};5178my%date=5179 parse_date($meta->{'author-time'},$meta->{'author-tz'});5180my$date=$date{'iso-tz'};5181if($group_size) {5182$current_color= ($current_color+1) %$num_colors;5183}5184my$tr_class=$rev_color[$current_color];5185$tr_class.=' boundary'if(exists$meta->{'boundary'});5186$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5187$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5188print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5189if($group_size) {5190print"<td class=\"sha1\"";5191print" title=\"". esc_html($author) .",$date\"";5192print" rowspan=\"$group_size\""if($group_size>1);5193print">";5194print$cgi->a({-href => href(action=>"commit",5195 hash=>$full_rev,5196 file_name=>$file_name)},5197 esc_html($short_rev));5198if($group_size>=2) {5199my@author_initials= ($author=~/\b([[:upper:]])\B/g);5200if(@author_initials) {5201print"<br />".5202 esc_html(join('',@author_initials));5203# or join('.', ...)5204}5205}5206print"</td>\n";5207}5208# 'previous' <sha1 of parent commit> <filename at commit>5209if(exists$meta->{'previous'} &&5210$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5211$meta->{'parent'} =$1;5212$meta->{'file_parent'} = unquote($2);5213}5214my$linenr_commit=5215exists($meta->{'parent'}) ?5216$meta->{'parent'} :$full_rev;5217my$linenr_filename=5218exists($meta->{'file_parent'}) ?5219$meta->{'file_parent'} : unquote($meta->{'filename'});5220my$blamed= href(action =>'blame',5221 file_name =>$linenr_filename,5222 hash_base =>$linenr_commit);5223print"<td class=\"linenr\">";5224print$cgi->a({ -href =>"$blamed#l$orig_lineno",5225-class=>"linenr"},5226 esc_html($lineno));5227print"</td>";5228print"<td class=\"pre\">". esc_html($data) ."</td>\n";5229print"</tr>\n";5230}# end while52315232}52335234# footer5235print"</tbody>\n".5236"</table>\n";# class="blame"5237print"</div>\n";# class="blame_body"5238close$fd5239or print"Reading blob failed\n";52405241 git_footer_html();5242}52435244sub git_blame {5245 git_blame_common();5246}52475248sub git_blame_incremental {5249 git_blame_common('incremental');5250}52515252sub git_blame_data {5253 git_blame_common('data');5254}52555256sub git_tags {5257my$head= git_get_head_hash($project);5258 git_header_html();5259 git_print_page_nav('','',$head,undef,$head);5260 git_print_header_div('summary',$project);52615262my@tagslist= git_get_tags_list();5263if(@tagslist) {5264 git_tags_body(\@tagslist);5265}5266 git_footer_html();5267}52685269sub git_heads {5270my$head= git_get_head_hash($project);5271 git_header_html();5272 git_print_page_nav('','',$head,undef,$head);5273 git_print_header_div('summary',$project);52745275my@headslist= git_get_heads_list();5276if(@headslist) {5277 git_heads_body(\@headslist,$head);5278}5279 git_footer_html();5280}52815282sub git_blob_plain {5283my$type=shift;5284my$expires;52855286if(!defined$hash) {5287if(defined$file_name) {5288my$base=$hash_base|| git_get_head_hash($project);5289$hash= git_get_hash_by_path($base,$file_name,"blob")5290or die_error(404,"Cannot find file");5291}else{5292 die_error(400,"No file name defined");5293}5294}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5295# blobs defined by non-textual hash id's can be cached5296$expires="+1d";5297}52985299open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5300or die_error(500,"Open git-cat-file blob '$hash' failed");53015302# content-type (can include charset)5303$type= blob_contenttype($fd,$file_name,$type);53045305# "save as" filename, even when no $file_name is given5306my$save_as="$hash";5307if(defined$file_name) {5308$save_as=$file_name;5309}elsif($type=~m/^text\//) {5310$save_as.='.txt';5311}53125313# With XSS prevention on, blobs of all types except a few known safe5314# ones are served with "Content-Disposition: attachment" to make sure5315# they don't run in our security domain. For certain image types,5316# blob view writes an <img> tag referring to blob_plain view, and we5317# want to be sure not to break that by serving the image as an5318# attachment (though Firefox 3 doesn't seem to care).5319my$sandbox=$prevent_xss&&5320$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;53215322print$cgi->header(5323-type =>$type,5324-expires =>$expires,5325-content_disposition =>5326($sandbox?'attachment':'inline')5327.'; filename="'.$save_as.'"');5328local$/=undef;5329binmode STDOUT,':raw';5330print<$fd>;5331binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5332close$fd;5333}53345335sub git_blob {5336my$expires;53375338if(!defined$hash) {5339if(defined$file_name) {5340my$base=$hash_base|| git_get_head_hash($project);5341$hash= git_get_hash_by_path($base,$file_name,"blob")5342or die_error(404,"Cannot find file");5343}else{5344 die_error(400,"No file name defined");5345}5346}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5347# blobs defined by non-textual hash id's can be cached5348$expires="+1d";5349}53505351my$have_blame= gitweb_check_feature('blame');5352open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5353or die_error(500,"Couldn't cat$file_name,$hash");5354my$mimetype= blob_mimetype($fd,$file_name);5355if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5356close$fd;5357return git_blob_plain($mimetype);5358}5359# we can have blame only for text/* mimetype5360$have_blame&&= ($mimetype=~m!^text/!);53615362 git_header_html(undef,$expires);5363my$formats_nav='';5364if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5365if(defined$file_name) {5366if($have_blame) {5367$formats_nav.=5368$cgi->a({-href => href(action=>"blame", -replay=>1)},5369"blame") .5370" | ";5371}5372$formats_nav.=5373$cgi->a({-href => href(action=>"history", -replay=>1)},5374"history") .5375" | ".5376$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5377"raw") .5378" | ".5379$cgi->a({-href => href(action=>"blob",5380 hash_base=>"HEAD", file_name=>$file_name)},5381"HEAD");5382}else{5383$formats_nav.=5384$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5385"raw");5386}5387 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5388 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5389}else{5390print"<div class=\"page_nav\">\n".5391"<br/><br/></div>\n".5392"<div class=\"title\">$hash</div>\n";5393}5394 git_print_page_path($file_name,"blob",$hash_base);5395print"<div class=\"page_body\">\n";5396if($mimetype=~m!^image/!) {5397print qq!<img type="$mimetype"!;5398if($file_name) {5399print qq! alt="$file_name" title="$file_name"!;5400}5401print qq! src="! .5402 href(action=>"blob_plain", hash=>$hash,5403 hash_base=>$hash_base, file_name=>$file_name) .5404 qq!"/>\n!;5405}else{5406my$nr;5407while(my$line= <$fd>) {5408chomp$line;5409$nr++;5410$line= untabify($line);5411printf"<div class=\"pre\"><a id=\"l%i\"href=\"". href(-replay =>1)5412."#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5413$nr,$nr,$nr, esc_html($line, -nbsp=>1);5414}5415}5416close$fd5417or print"Reading blob failed.\n";5418print"</div>";5419 git_footer_html();5420}54215422sub git_tree {5423if(!defined$hash_base) {5424$hash_base="HEAD";5425}5426if(!defined$hash) {5427if(defined$file_name) {5428$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5429}else{5430$hash=$hash_base;5431}5432}5433 die_error(404,"No such tree")unlessdefined($hash);54345435my$show_sizes= gitweb_check_feature('show-sizes');5436my$have_blame= gitweb_check_feature('blame');54375438my@entries= ();5439{5440local$/="\0";5441open my$fd,"-|", git_cmd(),"ls-tree",'-z',5442($show_sizes?'-l': ()),@extra_options,$hash5443or die_error(500,"Open git-ls-tree failed");5444@entries=map{chomp;$_} <$fd>;5445close$fd5446or die_error(404,"Reading tree failed");5447}54485449my$refs= git_get_references();5450my$ref= format_ref_marker($refs,$hash_base);5451 git_header_html();5452my$basedir='';5453if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5454my@views_nav= ();5455if(defined$file_name) {5456push@views_nav,5457$cgi->a({-href => href(action=>"history", -replay=>1)},5458"history"),5459$cgi->a({-href => href(action=>"tree",5460 hash_base=>"HEAD", file_name=>$file_name)},5461"HEAD"),5462}5463my$snapshot_links= format_snapshot_links($hash);5464if(defined$snapshot_links) {5465# FIXME: Should be available when we have no hash base as well.5466push@views_nav,$snapshot_links;5467}5468 git_print_page_nav('tree','',$hash_base,undef,undef,5469join(' | ',@views_nav));5470 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5471}else{5472undef$hash_base;5473print"<div class=\"page_nav\">\n";5474print"<br/><br/></div>\n";5475print"<div class=\"title\">$hash</div>\n";5476}5477if(defined$file_name) {5478$basedir=$file_name;5479if($basedirne''&&substr($basedir, -1)ne'/') {5480$basedir.='/';5481}5482 git_print_page_path($file_name,'tree',$hash_base);5483}5484print"<div class=\"page_body\">\n";5485print"<table class=\"tree\">\n";5486my$alternate=1;5487# '..' (top directory) link if possible5488if(defined$hash_base&&5489defined$file_name&&$file_name=~m![^/]+$!) {5490if($alternate) {5491print"<tr class=\"dark\">\n";5492}else{5493print"<tr class=\"light\">\n";5494}5495$alternate^=1;54965497my$up=$file_name;5498$up=~s!/?[^/]+$!!;5499undef$upunless$up;5500# based on git_print_tree_entry5501print'<td class="mode">'. mode_str('040000') ."</td>\n";5502print'<td class="size"> </td>'."\n"if$show_sizes;5503print'<td class="list">';5504print$cgi->a({-href => href(action=>"tree",5505 hash_base=>$hash_base,5506 file_name=>$up)},5507"..");5508print"</td>\n";5509print"<td class=\"link\"></td>\n";55105511print"</tr>\n";5512}5513foreachmy$line(@entries) {5514my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);55155516if($alternate) {5517print"<tr class=\"dark\">\n";5518}else{5519print"<tr class=\"light\">\n";5520}5521$alternate^=1;55225523 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);55245525print"</tr>\n";5526}5527print"</table>\n".5528"</div>";5529 git_footer_html();5530}55315532sub snapshot_name {5533my($project,$hash) =@_;55345535# path/to/project.git -> project5536# path/to/project/.git -> project5537my$name= to_utf8($project);5538$name=~ s,([^/])/*\.git$,$1,;5539$name= basename($name);5540# sanitize name5541$name=~s/[[:cntrl:]]/?/g;55425543my$ver=$hash;5544if($hash=~/^[0-9a-fA-F]+$/) {5545# shorten SHA-1 hash5546my$full_hash= git_get_full_hash($project,$hash);5547if($full_hash=~/^$hash/&&length($hash) >7) {5548$ver= git_get_short_hash($project,$hash);5549}5550}elsif($hash=~m!^refs/tags/(.*)$!) {5551# tags don't need shortened SHA-1 hash5552$ver=$1;5553}else{5554# branches and other need shortened SHA-1 hash5555if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5556$ver=$1;5557}5558$ver.='-'. git_get_short_hash($project,$hash);5559}5560# in case of hierarchical branch names5561$ver=~s!/!.!g;55625563# name = project-version_string5564$name="$name-$ver";55655566returnwantarray? ($name,$name) :$name;5567}55685569sub git_snapshot {5570my$format=$input_params{'snapshot_format'};5571if(!@snapshot_fmts) {5572 die_error(403,"Snapshots not allowed");5573}5574# default to first supported snapshot format5575$format||=$snapshot_fmts[0];5576if($format!~m/^[a-z0-9]+$/) {5577 die_error(400,"Invalid snapshot format parameter");5578}elsif(!exists($known_snapshot_formats{$format})) {5579 die_error(400,"Unknown snapshot format");5580}elsif($known_snapshot_formats{$format}{'disabled'}) {5581 die_error(403,"Snapshot format not allowed");5582}elsif(!grep($_eq$format,@snapshot_fmts)) {5583 die_error(403,"Unsupported snapshot format");5584}55855586my$type= git_get_type("$hash^{}");5587if(!$type) {5588 die_error(404,'Object does not exist');5589}elsif($typeeq'blob') {5590 die_error(400,'Object is not a tree-ish');5591}55925593my($name,$prefix) = snapshot_name($project,$hash);5594my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5595my$cmd= quote_command(5596 git_cmd(),'archive',5597"--format=$known_snapshot_formats{$format}{'format'}",5598"--prefix=$prefix/",$hash);5599if(exists$known_snapshot_formats{$format}{'compressor'}) {5600$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5601}56025603$filename=~s/(["\\])/\\$1/g;5604print$cgi->header(5605-type =>$known_snapshot_formats{$format}{'type'},5606-content_disposition =>'inline; filename="'.$filename.'"',5607-status =>'200 OK');56085609open my$fd,"-|",$cmd5610or die_error(500,"Execute git-archive failed");5611binmode STDOUT,':raw';5612print<$fd>;5613binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5614close$fd;5615}56165617sub git_log_generic {5618my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;56195620my$head= git_get_head_hash($project);5621if(!defined$base) {5622$base=$head;5623}5624if(!defined$page) {5625$page=0;5626}5627my$refs= git_get_references();56285629my$commit_hash=$base;5630if(defined$parent) {5631$commit_hash="$parent..$base";5632}5633my@commitlist=5634 parse_commits($commit_hash,101, (100*$page),5635defined$file_name? ($file_name,"--full-history") : ());56365637my$ftype;5638if(!defined$file_hash&&defined$file_name) {5639# some commits could have deleted file in question,5640# and not have it in tree, but one of them has to have it5641for(my$i=0;$i<@commitlist;$i++) {5642$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5643last ifdefined$file_hash;5644}5645}5646if(defined$file_hash) {5647$ftype= git_get_type($file_hash);5648}5649if(defined$file_name&& !defined$ftype) {5650 die_error(500,"Unknown type of object");5651}5652my%co;5653if(defined$file_name) {5654%co= parse_commit($base)5655or die_error(404,"Unknown commit object");5656}565756585659my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5660my$next_link='';5661if($#commitlist>=100) {5662$next_link=5663$cgi->a({-href => href(-replay=>1, page=>$page+1),5664-accesskey =>"n", -title =>"Alt-n"},"next");5665}5666my$patch_max= gitweb_get_feature('patches');5667if($patch_max&& !defined$file_name) {5668if($patch_max<0||@commitlist<=$patch_max) {5669$paging_nav.=" ⋅ ".5670$cgi->a({-href => href(action=>"patches", -replay=>1)},5671"patches");5672}5673}56745675 git_header_html();5676 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5677if(defined$file_name) {5678 git_print_header_div('commit', esc_html($co{'title'}),$base);5679}else{5680 git_print_header_div('summary',$project)5681}5682 git_print_page_path($file_name,$ftype,$hash_base)5683if(defined$file_name);56845685$body_subr->(\@commitlist,0,99,$refs,$next_link,5686$file_name,$file_hash,$ftype);56875688 git_footer_html();5689}56905691sub git_log {5692 git_log_generic('log', \&git_log_body,5693$hash,$hash_parent);5694}56955696sub git_commit {5697$hash||=$hash_base||"HEAD";5698my%co= parse_commit($hash)5699or die_error(404,"Unknown commit object");57005701my$parent=$co{'parent'};5702my$parents=$co{'parents'};# listref57035704# we need to prepare $formats_nav before any parameter munging5705my$formats_nav;5706if(!defined$parent) {5707# --root commitdiff5708$formats_nav.='(initial)';5709}elsif(@$parents==1) {5710# single parent commit5711$formats_nav.=5712'(parent: '.5713$cgi->a({-href => href(action=>"commit",5714 hash=>$parent)},5715 esc_html(substr($parent,0,7))) .5716')';5717}else{5718# merge commit5719$formats_nav.=5720'(merge: '.5721join(' ',map{5722$cgi->a({-href => href(action=>"commit",5723 hash=>$_)},5724 esc_html(substr($_,0,7)));5725}@$parents) .5726')';5727}5728if(gitweb_check_feature('patches') &&@$parents<=1) {5729$formats_nav.=" | ".5730$cgi->a({-href => href(action=>"patch", -replay=>1)},5731"patch");5732}57335734if(!defined$parent) {5735$parent="--root";5736}5737my@difftree;5738open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5739@diff_opts,5740(@$parents<=1?$parent:'-c'),5741$hash,"--"5742or die_error(500,"Open git-diff-tree failed");5743@difftree=map{chomp;$_} <$fd>;5744close$fdor die_error(404,"Reading git-diff-tree failed");57455746# non-textual hash id's can be cached5747my$expires;5748if($hash=~m/^[0-9a-fA-F]{40}$/) {5749$expires="+1d";5750}5751my$refs= git_get_references();5752my$ref= format_ref_marker($refs,$co{'id'});57535754 git_header_html(undef,$expires);5755 git_print_page_nav('commit','',5756$hash,$co{'tree'},$hash,5757$formats_nav);57585759if(defined$co{'parent'}) {5760 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5761}else{5762 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5763}5764print"<div class=\"title_text\">\n".5765"<table class=\"object_header\">\n";5766 git_print_authorship_rows(\%co);5767print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5768print"<tr>".5769"<td>tree</td>".5770"<td class=\"sha1\">".5771$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5772class=>"list"},$co{'tree'}) .5773"</td>".5774"<td class=\"link\">".5775$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5776"tree");5777my$snapshot_links= format_snapshot_links($hash);5778if(defined$snapshot_links) {5779print" | ".$snapshot_links;5780}5781print"</td>".5782"</tr>\n";57835784foreachmy$par(@$parents) {5785print"<tr>".5786"<td>parent</td>".5787"<td class=\"sha1\">".5788$cgi->a({-href => href(action=>"commit", hash=>$par),5789class=>"list"},$par) .5790"</td>".5791"<td class=\"link\">".5792$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5793" | ".5794$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5795"</td>".5796"</tr>\n";5797}5798print"</table>".5799"</div>\n";58005801print"<div class=\"page_body\">\n";5802 git_print_log($co{'comment'});5803print"</div>\n";58045805 git_difftree_body(\@difftree,$hash,@$parents);58065807 git_footer_html();5808}58095810sub git_object {5811# object is defined by:5812# - hash or hash_base alone5813# - hash_base and file_name5814my$type;58155816# - hash or hash_base alone5817if($hash|| ($hash_base&& !defined$file_name)) {5818my$object_id=$hash||$hash_base;58195820open my$fd,"-|", quote_command(5821 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5822or die_error(404,"Object does not exist");5823$type= <$fd>;5824chomp$type;5825close$fd5826or die_error(404,"Object does not exist");58275828# - hash_base and file_name5829}elsif($hash_base&&defined$file_name) {5830$file_name=~ s,/+$,,;58315832system(git_cmd(),"cat-file",'-e',$hash_base) ==05833or die_error(404,"Base object does not exist");58345835# here errors should not hapen5836open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5837or die_error(500,"Open git-ls-tree failed");5838my$line= <$fd>;5839close$fd;58405841#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5842unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5843 die_error(404,"File or directory for given base does not exist");5844}5845$type=$2;5846$hash=$3;5847}else{5848 die_error(400,"Not enough information to find object");5849}58505851print$cgi->redirect(-uri => href(action=>$type, -full=>1,5852 hash=>$hash, hash_base=>$hash_base,5853 file_name=>$file_name),5854-status =>'302 Found');5855}58565857sub git_blobdiff {5858my$format=shift||'html';58595860my$fd;5861my@difftree;5862my%diffinfo;5863my$expires;58645865# preparing $fd and %diffinfo for git_patchset_body5866# new style URI5867if(defined$hash_base&&defined$hash_parent_base) {5868if(defined$file_name) {5869# read raw output5870open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5871$hash_parent_base,$hash_base,5872"--", (defined$file_parent?$file_parent: ()),$file_name5873or die_error(500,"Open git-diff-tree failed");5874@difftree=map{chomp;$_} <$fd>;5875close$fd5876or die_error(404,"Reading git-diff-tree failed");5877@difftree5878or die_error(404,"Blob diff not found");58795880}elsif(defined$hash&&5881$hash=~/[0-9a-fA-F]{40}/) {5882# try to find filename from $hash58835884# read filtered raw output5885open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5886$hash_parent_base,$hash_base,"--"5887or die_error(500,"Open git-diff-tree failed");5888@difftree=5889# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5890# $hash == to_id5891grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5892map{chomp;$_} <$fd>;5893close$fd5894or die_error(404,"Reading git-diff-tree failed");5895@difftree5896or die_error(404,"Blob diff not found");58975898}else{5899 die_error(400,"Missing one of the blob diff parameters");5900}59015902if(@difftree>1) {5903 die_error(400,"Ambiguous blob diff specification");5904}59055906%diffinfo= parse_difftree_raw_line($difftree[0]);5907$file_parent||=$diffinfo{'from_file'} ||$file_name;5908$file_name||=$diffinfo{'to_file'};59095910$hash_parent||=$diffinfo{'from_id'};5911$hash||=$diffinfo{'to_id'};59125913# non-textual hash id's can be cached5914if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5915$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5916$expires='+1d';5917}59185919# open patch output5920open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5921'-p', ($formateq'html'?"--full-index": ()),5922$hash_parent_base,$hash_base,5923"--", (defined$file_parent?$file_parent: ()),$file_name5924or die_error(500,"Open git-diff-tree failed");5925}59265927# old/legacy style URI -- not generated anymore since 1.4.3.5928if(!%diffinfo) {5929 die_error('404 Not Found',"Missing one of the blob diff parameters")5930}59315932# header5933if($formateq'html') {5934my$formats_nav=5935$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5936"raw");5937 git_header_html(undef,$expires);5938if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5939 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5940 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5941}else{5942print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5943print"<div class=\"title\">$hashvs$hash_parent</div>\n";5944}5945if(defined$file_name) {5946 git_print_page_path($file_name,"blob",$hash_base);5947}else{5948print"<div class=\"page_path\"></div>\n";5949}59505951}elsif($formateq'plain') {5952print$cgi->header(5953-type =>'text/plain',5954-charset =>'utf-8',5955-expires =>$expires,5956-content_disposition =>'inline; filename="'."$file_name".'.patch"');59575958print"X-Git-Url: ".$cgi->self_url() ."\n\n";59595960}else{5961 die_error(400,"Unknown blobdiff format");5962}59635964# patch5965if($formateq'html') {5966print"<div class=\"page_body\">\n";59675968 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5969close$fd;59705971print"</div>\n";# class="page_body"5972 git_footer_html();59735974}else{5975while(my$line= <$fd>) {5976$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5977$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;59785979print$line;59805981last if$line=~m!^\+\+\+!;5982}5983local$/=undef;5984print<$fd>;5985close$fd;5986}5987}59885989sub git_blobdiff_plain {5990 git_blobdiff('plain');5991}59925993sub git_commitdiff {5994my%params=@_;5995my$format=$params{-format} ||'html';59965997my($patch_max) = gitweb_get_feature('patches');5998if($formateq'patch') {5999 die_error(403,"Patch view not allowed")unless$patch_max;6000}60016002$hash||=$hash_base||"HEAD";6003my%co= parse_commit($hash)6004or die_error(404,"Unknown commit object");60056006# choose format for commitdiff for merge6007if(!defined$hash_parent&& @{$co{'parents'}} >1) {6008$hash_parent='--cc';6009}6010# we need to prepare $formats_nav before almost any parameter munging6011my$formats_nav;6012if($formateq'html') {6013$formats_nav=6014$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6015"raw");6016if($patch_max&& @{$co{'parents'}} <=1) {6017$formats_nav.=" | ".6018$cgi->a({-href => href(action=>"patch", -replay=>1)},6019"patch");6020}60216022if(defined$hash_parent&&6023$hash_parentne'-c'&&$hash_parentne'--cc') {6024# commitdiff with two commits given6025my$hash_parent_short=$hash_parent;6026if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6027$hash_parent_short=substr($hash_parent,0,7);6028}6029$formats_nav.=6030' (from';6031for(my$i=0;$i< @{$co{'parents'}};$i++) {6032if($co{'parents'}[$i]eq$hash_parent) {6033$formats_nav.=' parent '. ($i+1);6034last;6035}6036}6037$formats_nav.=': '.6038$cgi->a({-href => href(action=>"commitdiff",6039 hash=>$hash_parent)},6040 esc_html($hash_parent_short)) .6041')';6042}elsif(!$co{'parent'}) {6043# --root commitdiff6044$formats_nav.=' (initial)';6045}elsif(scalar@{$co{'parents'}} ==1) {6046# single parent commit6047$formats_nav.=6048' (parent: '.6049$cgi->a({-href => href(action=>"commitdiff",6050 hash=>$co{'parent'})},6051 esc_html(substr($co{'parent'},0,7))) .6052')';6053}else{6054# merge commit6055if($hash_parenteq'--cc') {6056$formats_nav.=' | '.6057$cgi->a({-href => href(action=>"commitdiff",6058 hash=>$hash, hash_parent=>'-c')},6059'combined');6060}else{# $hash_parent eq '-c'6061$formats_nav.=' | '.6062$cgi->a({-href => href(action=>"commitdiff",6063 hash=>$hash, hash_parent=>'--cc')},6064'compact');6065}6066$formats_nav.=6067' (merge: '.6068join(' ',map{6069$cgi->a({-href => href(action=>"commitdiff",6070 hash=>$_)},6071 esc_html(substr($_,0,7)));6072} @{$co{'parents'}} ) .6073')';6074}6075}60766077my$hash_parent_param=$hash_parent;6078if(!defined$hash_parent_param) {6079# --cc for multiple parents, --root for parentless6080$hash_parent_param=6081@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6082}60836084# read commitdiff6085my$fd;6086my@difftree;6087if($formateq'html') {6088open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6089"--no-commit-id","--patch-with-raw","--full-index",6090$hash_parent_param,$hash,"--"6091or die_error(500,"Open git-diff-tree failed");60926093while(my$line= <$fd>) {6094chomp$line;6095# empty line ends raw part of diff-tree output6096last unless$line;6097push@difftree,scalar parse_difftree_raw_line($line);6098}60996100}elsif($formateq'plain') {6101open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6102'-p',$hash_parent_param,$hash,"--"6103or die_error(500,"Open git-diff-tree failed");6104}elsif($formateq'patch') {6105# For commit ranges, we limit the output to the number of6106# patches specified in the 'patches' feature.6107# For single commits, we limit the output to a single patch,6108# diverging from the git-format-patch default.6109my@commit_spec= ();6110if($hash_parent) {6111if($patch_max>0) {6112push@commit_spec,"-$patch_max";6113}6114push@commit_spec,'-n',"$hash_parent..$hash";6115}else{6116if($params{-single}) {6117push@commit_spec,'-1';6118}else{6119if($patch_max>0) {6120push@commit_spec,"-$patch_max";6121}6122push@commit_spec,"-n";6123}6124push@commit_spec,'--root',$hash;6125}6126open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',6127'--stdout',@commit_spec6128or die_error(500,"Open git-format-patch failed");6129}else{6130 die_error(400,"Unknown commitdiff format");6131}61326133# non-textual hash id's can be cached6134my$expires;6135if($hash=~m/^[0-9a-fA-F]{40}$/) {6136$expires="+1d";6137}61386139# write commit message6140if($formateq'html') {6141my$refs= git_get_references();6142my$ref= format_ref_marker($refs,$co{'id'});61436144 git_header_html(undef,$expires);6145 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6146 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6147print"<div class=\"title_text\">\n".6148"<table class=\"object_header\">\n";6149 git_print_authorship_rows(\%co);6150print"</table>".6151"</div>\n";6152print"<div class=\"page_body\">\n";6153if(@{$co{'comment'}} >1) {6154print"<div class=\"log\">\n";6155 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6156print"</div>\n";# class="log"6157}61586159}elsif($formateq'plain') {6160my$refs= git_get_references("tags");6161my$tagname= git_get_rev_name_tags($hash);6162my$filename= basename($project) ."-$hash.patch";61636164print$cgi->header(6165-type =>'text/plain',6166-charset =>'utf-8',6167-expires =>$expires,6168-content_disposition =>'inline; filename="'."$filename".'"');6169my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6170print"From: ". to_utf8($co{'author'}) ."\n";6171print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6172print"Subject: ". to_utf8($co{'title'}) ."\n";61736174print"X-Git-Tag:$tagname\n"if$tagname;6175print"X-Git-Url: ".$cgi->self_url() ."\n\n";61766177foreachmy$line(@{$co{'comment'}}) {6178print to_utf8($line) ."\n";6179}6180print"---\n\n";6181}elsif($formateq'patch') {6182my$filename= basename($project) ."-$hash.patch";61836184print$cgi->header(6185-type =>'text/plain',6186-charset =>'utf-8',6187-expires =>$expires,6188-content_disposition =>'inline; filename="'."$filename".'"');6189}61906191# write patch6192if($formateq'html') {6193my$use_parents= !defined$hash_parent||6194$hash_parenteq'-c'||$hash_parenteq'--cc';6195 git_difftree_body(\@difftree,$hash,6196$use_parents? @{$co{'parents'}} :$hash_parent);6197print"<br/>\n";61986199 git_patchset_body($fd, \@difftree,$hash,6200$use_parents? @{$co{'parents'}} :$hash_parent);6201close$fd;6202print"</div>\n";# class="page_body"6203 git_footer_html();62046205}elsif($formateq'plain') {6206local$/=undef;6207print<$fd>;6208close$fd6209or print"Reading git-diff-tree failed\n";6210}elsif($formateq'patch') {6211local$/=undef;6212print<$fd>;6213close$fd6214or print"Reading git-format-patch failed\n";6215}6216}62176218sub git_commitdiff_plain {6219 git_commitdiff(-format =>'plain');6220}62216222# format-patch-style patches6223sub git_patch {6224 git_commitdiff(-format =>'patch', -single =>1);6225}62266227sub git_patches {6228 git_commitdiff(-format =>'patch');6229}62306231sub git_history {6232 git_log_generic('history', \&git_history_body,6233$hash_base,$hash_parent_base,6234$file_name,$hash);6235}62366237sub git_search {6238 gitweb_check_feature('search')or die_error(403,"Search is disabled");6239if(!defined$searchtext) {6240 die_error(400,"Text field is empty");6241}6242if(!defined$hash) {6243$hash= git_get_head_hash($project);6244}6245my%co= parse_commit($hash);6246if(!%co) {6247 die_error(404,"Unknown commit object");6248}6249if(!defined$page) {6250$page=0;6251}62526253$searchtype||='commit';6254if($searchtypeeq'pickaxe') {6255# pickaxe may take all resources of your box and run for several minutes6256# with every query - so decide by yourself how public you make this feature6257 gitweb_check_feature('pickaxe')6258or die_error(403,"Pickaxe is disabled");6259}6260if($searchtypeeq'grep') {6261 gitweb_check_feature('grep')6262or die_error(403,"Grep is disabled");6263}62646265 git_header_html();62666267if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6268my$greptype;6269if($searchtypeeq'commit') {6270$greptype="--grep=";6271}elsif($searchtypeeq'author') {6272$greptype="--author=";6273}elsif($searchtypeeq'committer') {6274$greptype="--committer=";6275}6276$greptype.=$searchtext;6277my@commitlist= parse_commits($hash,101, (100*$page),undef,6278$greptype,'--regexp-ignore-case',6279$search_use_regexp?'--extended-regexp':'--fixed-strings');62806281my$paging_nav='';6282if($page>0) {6283$paging_nav.=6284$cgi->a({-href => href(action=>"search", hash=>$hash,6285 searchtext=>$searchtext,6286 searchtype=>$searchtype)},6287"first");6288$paging_nav.=" ⋅ ".6289$cgi->a({-href => href(-replay=>1, page=>$page-1),6290-accesskey =>"p", -title =>"Alt-p"},"prev");6291}else{6292$paging_nav.="first";6293$paging_nav.=" ⋅ prev";6294}6295my$next_link='';6296if($#commitlist>=100) {6297$next_link=6298$cgi->a({-href => href(-replay=>1, page=>$page+1),6299-accesskey =>"n", -title =>"Alt-n"},"next");6300$paging_nav.=" ⋅$next_link";6301}else{6302$paging_nav.=" ⋅ next";6303}63046305if($#commitlist>=100) {6306}63076308 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6309 git_print_header_div('commit', esc_html($co{'title'}),$hash);6310 git_search_grep_body(\@commitlist,0,99,$next_link);6311}63126313if($searchtypeeq'pickaxe') {6314 git_print_page_nav('','',$hash,$co{'tree'},$hash);6315 git_print_header_div('commit', esc_html($co{'title'}),$hash);63166317print"<table class=\"pickaxe search\">\n";6318my$alternate=1;6319local$/="\n";6320open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6321'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6322($search_use_regexp?'--pickaxe-regex': ());6323undef%co;6324my@files;6325while(my$line= <$fd>) {6326chomp$line;6327next unless$line;63286329my%set= parse_difftree_raw_line($line);6330if(defined$set{'commit'}) {6331# finish previous commit6332if(%co) {6333print"</td>\n".6334"<td class=\"link\">".6335$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6336" | ".6337$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6338print"</td>\n".6339"</tr>\n";6340}63416342if($alternate) {6343print"<tr class=\"dark\">\n";6344}else{6345print"<tr class=\"light\">\n";6346}6347$alternate^=1;6348%co= parse_commit($set{'commit'});6349my$author= chop_and_escape_str($co{'author_name'},15,5);6350print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6351"<td><i>$author</i></td>\n".6352"<td>".6353$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6354-class=>"list subject"},6355 chop_and_escape_str($co{'title'},50) ."<br/>");6356}elsif(defined$set{'to_id'}) {6357next if($set{'to_id'} =~m/^0{40}$/);63586359print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6360 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6361-class=>"list"},6362"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6363"<br/>\n";6364}6365}6366close$fd;63676368# finish last commit (warning: repetition!)6369if(%co) {6370print"</td>\n".6371"<td class=\"link\">".6372$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6373" | ".6374$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6375print"</td>\n".6376"</tr>\n";6377}63786379print"</table>\n";6380}63816382if($searchtypeeq'grep') {6383 git_print_page_nav('','',$hash,$co{'tree'},$hash);6384 git_print_header_div('commit', esc_html($co{'title'}),$hash);63856386print"<table class=\"grep_search\">\n";6387my$alternate=1;6388my$matches=0;6389local$/="\n";6390open my$fd,"-|", git_cmd(),'grep','-n',6391$search_use_regexp? ('-E','-i') :'-F',6392$searchtext,$co{'tree'};6393my$lastfile='';6394while(my$line= <$fd>) {6395chomp$line;6396my($file,$lno,$ltext,$binary);6397last if($matches++>1000);6398if($line=~/^Binary file (.+) matches$/) {6399$file=$1;6400$binary=1;6401}else{6402(undef,$file,$lno,$ltext) =split(/:/,$line,4);6403}6404if($filene$lastfile) {6405$lastfileand print"</td></tr>\n";6406if($alternate++) {6407print"<tr class=\"dark\">\n";6408}else{6409print"<tr class=\"light\">\n";6410}6411print"<td class=\"list\">".6412$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6413 file_name=>"$file"),6414-class=>"list"}, esc_path($file));6415print"</td><td>\n";6416$lastfile=$file;6417}6418if($binary) {6419print"<div class=\"binary\">Binary file</div>\n";6420}else{6421$ltext= untabify($ltext);6422if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6423$ltext= esc_html($1, -nbsp=>1);6424$ltext.='<span class="match">';6425$ltext.= esc_html($2, -nbsp=>1);6426$ltext.='</span>';6427$ltext.= esc_html($3, -nbsp=>1);6428}else{6429$ltext= esc_html($ltext, -nbsp=>1);6430}6431print"<div class=\"pre\">".6432$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6433 file_name=>"$file").'#l'.$lno,6434-class=>"linenr"},sprintf('%4i',$lno))6435.' '.$ltext."</div>\n";6436}6437}6438if($lastfile) {6439print"</td></tr>\n";6440if($matches>1000) {6441print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6442}6443}else{6444print"<div class=\"diff nodifferences\">No matches found</div>\n";6445}6446close$fd;64476448print"</table>\n";6449}6450 git_footer_html();6451}64526453sub git_search_help {6454 git_header_html();6455 git_print_page_nav('','',$hash,$hash,$hash);6456print<<EOT;6457<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6458regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6459the pattern entered is recognized as the POSIX extended6460<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6461insensitive).</p>6462<dl>6463<dt><b>commit</b></dt>6464<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6465EOT6466my$have_grep= gitweb_check_feature('grep');6467if($have_grep) {6468print<<EOT;6469<dt><b>grep</b></dt>6470<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6471 a different one) are searched for the given pattern. On large trees, this search can take6472a while and put some strain on the server, so please use it with some consideration. Note that6473due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6474case-sensitive.</dd>6475EOT6476}6477print<<EOT;6478<dt><b>author</b></dt>6479<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6480<dt><b>committer</b></dt>6481<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6482EOT6483my$have_pickaxe= gitweb_check_feature('pickaxe');6484if($have_pickaxe) {6485print<<EOT;6486<dt><b>pickaxe</b></dt>6487<dd>All commits that caused the string to appear or disappear from any file (changes that6488added, removed or "modified" the string) will be listed. This search can take a while and6489takes a lot of strain on the server, so please use it wisely. Note that since you may be6490interested even in changes just changing the case as well, this search is case sensitive.</dd>6491EOT6492}6493print"</dl>\n";6494 git_footer_html();6495}64966497sub git_shortlog {6498 git_log_generic('shortlog', \&git_shortlog_body,6499$hash,$hash_parent);6500}65016502## ......................................................................6503## feeds (RSS, Atom; OPML)65046505sub git_feed {6506my$format=shift||'atom';6507my$have_blame= gitweb_check_feature('blame');65086509# Atom: http://www.atomenabled.org/developers/syndication/6510# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6511if($formatne'rss'&&$formatne'atom') {6512 die_error(400,"Unknown web feed format");6513}65146515# log/feed of current (HEAD) branch, log of given branch, history of file/directory6516my$head=$hash||'HEAD';6517my@commitlist= parse_commits($head,150,0,$file_name);65186519my%latest_commit;6520my%latest_date;6521my$content_type="application/$format+xml";6522if(defined$cgi->http('HTTP_ACCEPT') &&6523$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6524# browser (feed reader) prefers text/xml6525$content_type='text/xml';6526}6527if(defined($commitlist[0])) {6528%latest_commit= %{$commitlist[0]};6529my$latest_epoch=$latest_commit{'committer_epoch'};6530%latest_date= parse_date($latest_epoch);6531my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6532if(defined$if_modified) {6533my$since;6534if(eval{require HTTP::Date;1; }) {6535$since= HTTP::Date::str2time($if_modified);6536}elsif(eval{require Time::ParseDate;1; }) {6537$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6538}6539if(defined$since&&$latest_epoch<=$since) {6540print$cgi->header(6541-type =>$content_type,6542-charset =>'utf-8',6543-last_modified =>$latest_date{'rfc2822'},6544-status =>'304 Not Modified');6545return;6546}6547}6548print$cgi->header(6549-type =>$content_type,6550-charset =>'utf-8',6551-last_modified =>$latest_date{'rfc2822'});6552}else{6553print$cgi->header(6554-type =>$content_type,6555-charset =>'utf-8');6556}65576558# Optimization: skip generating the body if client asks only6559# for Last-Modified date.6560return if($cgi->request_method()eq'HEAD');65616562# header variables6563my$title="$site_name-$project/$action";6564my$feed_type='log';6565if(defined$hash) {6566$title.=" - '$hash'";6567$feed_type='branch log';6568if(defined$file_name) {6569$title.=" ::$file_name";6570$feed_type='history';6571}6572}elsif(defined$file_name) {6573$title.=" -$file_name";6574$feed_type='history';6575}6576$title.="$feed_type";6577my$descr= git_get_project_description($project);6578if(defined$descr) {6579$descr= esc_html($descr);6580}else{6581$descr="$project".6582($formateq'rss'?'RSS':'Atom') .6583" feed";6584}6585my$owner= git_get_project_owner($project);6586$owner= esc_html($owner);65876588#header6589my$alt_url;6590if(defined$file_name) {6591$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6592}elsif(defined$hash) {6593$alt_url= href(-full=>1, action=>"log", hash=>$hash);6594}else{6595$alt_url= href(-full=>1, action=>"summary");6596}6597print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6598if($formateq'rss') {6599print<<XML;6600<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6601<channel>6602XML6603print"<title>$title</title>\n".6604"<link>$alt_url</link>\n".6605"<description>$descr</description>\n".6606"<language>en</language>\n".6607# project owner is responsible for 'editorial' content6608"<managingEditor>$owner</managingEditor>\n";6609if(defined$logo||defined$favicon) {6610# prefer the logo to the favicon, since RSS6611# doesn't allow both6612my$img= esc_url($logo||$favicon);6613print"<image>\n".6614"<url>$img</url>\n".6615"<title>$title</title>\n".6616"<link>$alt_url</link>\n".6617"</image>\n";6618}6619if(%latest_date) {6620print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6621print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6622}6623print"<generator>gitweb v.$version/$git_version</generator>\n";6624}elsif($formateq'atom') {6625print<<XML;6626<feed xmlns="http://www.w3.org/2005/Atom">6627XML6628print"<title>$title</title>\n".6629"<subtitle>$descr</subtitle>\n".6630'<link rel="alternate" type="text/html" href="'.6631$alt_url.'" />'."\n".6632'<link rel="self" type="'.$content_type.'" href="'.6633$cgi->self_url() .'" />'."\n".6634"<id>". href(-full=>1) ."</id>\n".6635# use project owner for feed author6636"<author><name>$owner</name></author>\n";6637if(defined$favicon) {6638print"<icon>". esc_url($favicon) ."</icon>\n";6639}6640if(defined$logo_url) {6641# not twice as wide as tall: 72 x 27 pixels6642print"<logo>". esc_url($logo) ."</logo>\n";6643}6644if(!%latest_date) {6645# dummy date to keep the feed valid until commits trickle in:6646print"<updated>1970-01-01T00:00:00Z</updated>\n";6647}else{6648print"<updated>$latest_date{'iso-8601'}</updated>\n";6649}6650print"<generator version='$version/$git_version'>gitweb</generator>\n";6651}66526653# contents6654for(my$i=0;$i<=$#commitlist;$i++) {6655my%co= %{$commitlist[$i]};6656my$commit=$co{'id'};6657# we read 150, we always show 30 and the ones more recent than 48 hours6658if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6659last;6660}6661my%cd= parse_date($co{'author_epoch'});66626663# get list of changed files6664open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6665$co{'parent'} ||"--root",6666$co{'id'},"--", (defined$file_name?$file_name: ())6667ornext;6668my@difftree=map{chomp;$_} <$fd>;6669close$fd6670ornext;66716672# print element (entry, item)6673my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6674if($formateq'rss') {6675print"<item>\n".6676"<title>". esc_html($co{'title'}) ."</title>\n".6677"<author>". esc_html($co{'author'}) ."</author>\n".6678"<pubDate>$cd{'rfc2822'}</pubDate>\n".6679"<guid isPermaLink=\"true\">$co_url</guid>\n".6680"<link>$co_url</link>\n".6681"<description>". esc_html($co{'title'}) ."</description>\n".6682"<content:encoded>".6683"<![CDATA[\n";6684}elsif($formateq'atom') {6685print"<entry>\n".6686"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6687"<updated>$cd{'iso-8601'}</updated>\n".6688"<author>\n".6689" <name>". esc_html($co{'author_name'}) ."</name>\n";6690if($co{'author_email'}) {6691print" <email>". esc_html($co{'author_email'}) ."</email>\n";6692}6693print"</author>\n".6694# use committer for contributor6695"<contributor>\n".6696" <name>". esc_html($co{'committer_name'}) ."</name>\n";6697if($co{'committer_email'}) {6698print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6699}6700print"</contributor>\n".6701"<published>$cd{'iso-8601'}</published>\n".6702"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6703"<id>$co_url</id>\n".6704"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6705"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6706}6707my$comment=$co{'comment'};6708print"<pre>\n";6709foreachmy$line(@$comment) {6710$line= esc_html($line);6711print"$line\n";6712}6713print"</pre><ul>\n";6714foreachmy$difftree_line(@difftree) {6715my%difftree= parse_difftree_raw_line($difftree_line);6716next if!$difftree{'from_id'};67176718my$file=$difftree{'file'} ||$difftree{'to_file'};67196720print"<li>".6721"[".6722$cgi->a({-href => href(-full=>1, action=>"blobdiff",6723 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6724 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6725 file_name=>$file, file_parent=>$difftree{'from_file'}),6726-title =>"diff"},'D');6727if($have_blame) {6728print$cgi->a({-href => href(-full=>1, action=>"blame",6729 file_name=>$file, hash_base=>$commit),6730-title =>"blame"},'B');6731}6732# if this is not a feed of a file history6733if(!defined$file_name||$file_namene$file) {6734print$cgi->a({-href => href(-full=>1, action=>"history",6735 file_name=>$file, hash=>$commit),6736-title =>"history"},'H');6737}6738$file= esc_path($file);6739print"] ".6740"$file</li>\n";6741}6742if($formateq'rss') {6743print"</ul>]]>\n".6744"</content:encoded>\n".6745"</item>\n";6746}elsif($formateq'atom') {6747print"</ul>\n</div>\n".6748"</content>\n".6749"</entry>\n";6750}6751}67526753# end of feed6754if($formateq'rss') {6755print"</channel>\n</rss>\n";6756}elsif($formateq'atom') {6757print"</feed>\n";6758}6759}67606761sub git_rss {6762 git_feed('rss');6763}67646765sub git_atom {6766 git_feed('atom');6767}67686769sub git_opml {6770my@list= git_get_projects_list();67716772print$cgi->header(6773-type =>'text/xml',6774-charset =>'utf-8',6775-content_disposition =>'inline; filename="opml.xml"');67766777print<<XML;6778<?xml version="1.0" encoding="utf-8"?>6779<opml version="1.0">6780<head>6781 <title>$site_nameOPML Export</title>6782</head>6783<body>6784<outline text="git RSS feeds">6785XML67866787foreachmy$pr(@list) {6788my%proj=%$pr;6789my$head= git_get_head_hash($proj{'path'});6790if(!defined$head) {6791next;6792}6793$git_dir="$projectroot/$proj{'path'}";6794my%co= parse_commit($head);6795if(!%co) {6796next;6797}67986799my$path= esc_html(chop_str($proj{'path'},25,5));6800my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6801my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6802print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6803}6804print<<XML;6805</outline>6806</body>6807</opml>6808XML6809}