1#!/usr/bin/perl 2 3# gitweb - simple web interface to track changes in git repositories 4# 5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org> 6# (C) 2005, Christian Gierke 7# 8# This program is licensed under the GPLv2 9 10use strict; 11use warnings; 12use CGI qw(:standard :escapeHTML -nosticky); 13use CGI::Util qw(unescape); 14use CGI::Carp qw(fatalsToBrowser); 15use Encode; 16use Fcntl ':mode'; 17use File::Find qw(); 18use File::Basename qw(basename); 19binmode STDOUT,':utf8'; 20 21our$t0; 22if(eval{require Time::HiRes;1; }) { 23$t0= [Time::HiRes::gettimeofday()]; 24} 25our$number_of_git_cmds=0; 26 27BEGIN{ 28 CGI->compile()if$ENV{'MOD_PERL'}; 29} 30 31our$cgi= new CGI; 32our$version="++GIT_VERSION++"; 33our$my_url=$cgi->url(); 34our$my_uri=$cgi->url(-absolute =>1); 35 36# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 37# needed and used only for URLs with nonempty PATH_INFO 38our$base_url=$my_url; 39 40# When the script is used as DirectoryIndex, the URL does not contain the name 41# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 42# have to do it ourselves. We make $path_info global because it's also used 43# later on. 44# 45# Another issue with the script being the DirectoryIndex is that the resulting 46# $my_url data is not the full script URL: this is good, because we want 47# generated links to keep implying the script name if it wasn't explicitly 48# indicated in the URL we're handling, but it means that $my_url cannot be used 49# as base URL. 50# Therefore, if we needed to strip PATH_INFO, then we know that we have 51# to build the base URL ourselves: 52our$path_info=$ENV{"PATH_INFO"}; 53if($path_info) { 54if($my_url=~ s,\Q$path_info\E$,, && 55$my_uri=~ s,\Q$path_info\E$,, && 56defined$ENV{'SCRIPT_NAME'}) { 57$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 58} 59} 60 61# core git executable to use 62# this can just be "git" if your webserver has a sensible PATH 63our$GIT="++GIT_BINDIR++/git"; 64 65# absolute fs-path which will be prepended to the project path 66#our $projectroot = "/pub/scm"; 67our$projectroot="++GITWEB_PROJECTROOT++"; 68 69# fs traversing limit for getting project list 70# the number is relative to the projectroot 71our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 72 73# target of the home link on top of all pages 74our$home_link=$my_uri||"/"; 75 76# string of the home link on top of all pages 77our$home_link_str="++GITWEB_HOME_LINK_STR++"; 78 79# name of your site or organization to appear in page titles 80# replace this with something more descriptive for clearer bookmarks 81our$site_name="++GITWEB_SITENAME++" 82|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 83 84# filename of html text to include at top of each page 85our$site_header="++GITWEB_SITE_HEADER++"; 86# html text to include at home page 87our$home_text="++GITWEB_HOMETEXT++"; 88# filename of html text to include at bottom of each page 89our$site_footer="++GITWEB_SITE_FOOTER++"; 90 91# URI of stylesheets 92our@stylesheets= ("++GITWEB_CSS++"); 93# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 94our$stylesheet=undef; 95# URI of GIT logo (72x27 size) 96our$logo="++GITWEB_LOGO++"; 97# URI of GIT favicon, assumed to be image/png type 98our$favicon="++GITWEB_FAVICON++"; 99# URI of gitweb.js (JavaScript code for gitweb) 100our$javascript="++GITWEB_JS++"; 101 102# URI and label (title) of GIT logo link 103#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 104#our $logo_label = "git documentation"; 105our$logo_url="http://git-scm.com/"; 106our$logo_label="git homepage"; 107 108# source of projects list 109our$projects_list="++GITWEB_LIST++"; 110 111# the width (in characters) of the projects list "Description" column 112our$projects_list_description_width=25; 113 114# default order of projects list 115# valid values are none, project, descr, owner, and age 116our$default_projects_order="project"; 117 118# show repository only if this file exists 119# (only effective if this variable evaluates to true) 120our$export_ok="++GITWEB_EXPORT_OK++"; 121 122# show repository only if this subroutine returns true 123# when given the path to the project, for example: 124# sub { return -e "$_[0]/git-daemon-export-ok"; } 125our$export_auth_hook=undef; 126 127# only allow viewing of repositories also shown on the overview page 128our$strict_export="++GITWEB_STRICT_EXPORT++"; 129 130# list of git base URLs used for URL to where fetch project from, 131# i.e. full URL is "$git_base_url/$project" 132our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 133 134# default blob_plain mimetype and default charset for text/plain blob 135our$default_blob_plain_mimetype='text/plain'; 136our$default_text_plain_charset=undef; 137 138# file to use for guessing MIME types before trying /etc/mime.types 139# (relative to the current git repository) 140our$mimetypes_file=undef; 141 142# assume this charset if line contains non-UTF-8 characters; 143# it should be valid encoding (see Encoding::Supported(3pm) for list), 144# for which encoding all byte sequences are valid, for example 145# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 146# could be even 'utf-8' for the old behavior) 147our$fallback_encoding='latin1'; 148 149# rename detection options for git-diff and git-diff-tree 150# - default is '-M', with the cost proportional to 151# (number of removed files) * (number of new files). 152# - more costly is '-C' (which implies '-M'), with the cost proportional to 153# (number of changed files + number of removed files) * (number of new files) 154# - even more costly is '-C', '--find-copies-harder' with cost 155# (number of files in the original tree) * (number of new files) 156# - one might want to include '-B' option, e.g. '-B', '-M' 157our@diff_opts= ('-M');# taken from git_commit 158 159# Disables features that would allow repository owners to inject script into 160# the gitweb domain. 161our$prevent_xss=0; 162 163# information about snapshot formats that gitweb is capable of serving 164our%known_snapshot_formats= ( 165# name => { 166# 'display' => display name, 167# 'type' => mime type, 168# 'suffix' => filename suffix, 169# 'format' => --format for git-archive, 170# 'compressor' => [compressor command and arguments] 171# (array reference, optional) 172# 'disabled' => boolean (optional)} 173# 174'tgz'=> { 175'display'=>'tar.gz', 176'type'=>'application/x-gzip', 177'suffix'=>'.tar.gz', 178'format'=>'tar', 179'compressor'=> ['gzip']}, 180 181'tbz2'=> { 182'display'=>'tar.bz2', 183'type'=>'application/x-bzip2', 184'suffix'=>'.tar.bz2', 185'format'=>'tar', 186'compressor'=> ['bzip2']}, 187 188'txz'=> { 189'display'=>'tar.xz', 190'type'=>'application/x-xz', 191'suffix'=>'.tar.xz', 192'format'=>'tar', 193'compressor'=> ['xz'], 194'disabled'=>1}, 195 196'zip'=> { 197'display'=>'zip', 198'type'=>'application/x-zip', 199'suffix'=>'.zip', 200'format'=>'zip'}, 201); 202 203# Aliases so we understand old gitweb.snapshot values in repository 204# configuration. 205our%known_snapshot_format_aliases= ( 206'gzip'=>'tgz', 207'bzip2'=>'tbz2', 208'xz'=>'txz', 209 210# backward compatibility: legacy gitweb config support 211'x-gzip'=>undef,'gz'=>undef, 212'x-bzip2'=>undef,'bz2'=>undef, 213'x-zip'=>undef,''=>undef, 214); 215 216# Pixel sizes for icons and avatars. If the default font sizes or lineheights 217# are changed, it may be appropriate to change these values too via 218# $GITWEB_CONFIG. 219our%avatar_size= ( 220'default'=>16, 221'double'=>32 222); 223 224# Used to set the maximum load that we will still respond to gitweb queries. 225# If server load exceed this value then return "503 server busy" error. 226# If gitweb cannot determined server load, it is taken to be 0. 227# Leave it undefined (or set to 'undef') to turn off load checking. 228our$maxload=300; 229 230# You define site-wide feature defaults here; override them with 231# $GITWEB_CONFIG as necessary. 232our%feature= ( 233# feature => { 234# 'sub' => feature-sub (subroutine), 235# 'override' => allow-override (boolean), 236# 'default' => [ default options...] (array reference)} 237# 238# if feature is overridable (it means that allow-override has true value), 239# then feature-sub will be called with default options as parameters; 240# return value of feature-sub indicates if to enable specified feature 241# 242# if there is no 'sub' key (no feature-sub), then feature cannot be 243# overriden 244# 245# use gitweb_get_feature(<feature>) to retrieve the <feature> value 246# (an array) or gitweb_check_feature(<feature>) to check if <feature> 247# is enabled 248 249# Enable the 'blame' blob view, showing the last commit that modified 250# each line in the file. This can be very CPU-intensive. 251 252# To enable system wide have in $GITWEB_CONFIG 253# $feature{'blame'}{'default'} = [1]; 254# To have project specific config enable override in $GITWEB_CONFIG 255# $feature{'blame'}{'override'} = 1; 256# and in project config gitweb.blame = 0|1; 257'blame'=> { 258'sub'=>sub{ feature_bool('blame',@_) }, 259'override'=>0, 260'default'=> [0]}, 261 262# Enable the 'snapshot' link, providing a compressed archive of any 263# tree. This can potentially generate high traffic if you have large 264# project. 265 266# Value is a list of formats defined in %known_snapshot_formats that 267# you wish to offer. 268# To disable system wide have in $GITWEB_CONFIG 269# $feature{'snapshot'}{'default'} = []; 270# To have project specific config enable override in $GITWEB_CONFIG 271# $feature{'snapshot'}{'override'} = 1; 272# and in project config, a comma-separated list of formats or "none" 273# to disable. Example: gitweb.snapshot = tbz2,zip; 274'snapshot'=> { 275'sub'=> \&feature_snapshot, 276'override'=>0, 277'default'=> ['tgz']}, 278 279# Enable text search, which will list the commits which match author, 280# committer or commit text to a given string. Enabled by default. 281# Project specific override is not supported. 282'search'=> { 283'override'=>0, 284'default'=> [1]}, 285 286# Enable grep search, which will list the files in currently selected 287# tree containing the given string. Enabled by default. This can be 288# potentially CPU-intensive, of course. 289 290# To enable system wide have in $GITWEB_CONFIG 291# $feature{'grep'}{'default'} = [1]; 292# To have project specific config enable override in $GITWEB_CONFIG 293# $feature{'grep'}{'override'} = 1; 294# and in project config gitweb.grep = 0|1; 295'grep'=> { 296'sub'=>sub{ feature_bool('grep',@_) }, 297'override'=>0, 298'default'=> [1]}, 299 300# Enable the pickaxe search, which will list the commits that modified 301# a given string in a file. This can be practical and quite faster 302# alternative to 'blame', but still potentially CPU-intensive. 303 304# To enable system wide have in $GITWEB_CONFIG 305# $feature{'pickaxe'}{'default'} = [1]; 306# To have project specific config enable override in $GITWEB_CONFIG 307# $feature{'pickaxe'}{'override'} = 1; 308# and in project config gitweb.pickaxe = 0|1; 309'pickaxe'=> { 310'sub'=>sub{ feature_bool('pickaxe',@_) }, 311'override'=>0, 312'default'=> [1]}, 313 314# Enable showing size of blobs in a 'tree' view, in a separate 315# column, similar to what 'ls -l' does. This cost a bit of IO. 316 317# To disable system wide have in $GITWEB_CONFIG 318# $feature{'show-sizes'}{'default'} = [0]; 319# To have project specific config enable override in $GITWEB_CONFIG 320# $feature{'show-sizes'}{'override'} = 1; 321# and in project config gitweb.showsizes = 0|1; 322'show-sizes'=> { 323'sub'=>sub{ feature_bool('showsizes',@_) }, 324'override'=>0, 325'default'=> [1]}, 326 327# Make gitweb use an alternative format of the URLs which can be 328# more readable and natural-looking: project name is embedded 329# directly in the path and the query string contains other 330# auxiliary information. All gitweb installations recognize 331# URL in either format; this configures in which formats gitweb 332# generates links. 333 334# To enable system wide have in $GITWEB_CONFIG 335# $feature{'pathinfo'}{'default'} = [1]; 336# Project specific override is not supported. 337 338# Note that you will need to change the default location of CSS, 339# favicon, logo and possibly other files to an absolute URL. Also, 340# if gitweb.cgi serves as your indexfile, you will need to force 341# $my_uri to contain the script name in your $GITWEB_CONFIG. 342'pathinfo'=> { 343'override'=>0, 344'default'=> [0]}, 345 346# Make gitweb consider projects in project root subdirectories 347# to be forks of existing projects. Given project $projname.git, 348# projects matching $projname/*.git will not be shown in the main 349# projects list, instead a '+' mark will be added to $projname 350# there and a 'forks' view will be enabled for the project, listing 351# all the forks. If project list is taken from a file, forks have 352# to be listed after the main project. 353 354# To enable system wide have in $GITWEB_CONFIG 355# $feature{'forks'}{'default'} = [1]; 356# Project specific override is not supported. 357'forks'=> { 358'override'=>0, 359'default'=> [0]}, 360 361# Insert custom links to the action bar of all project pages. 362# This enables you mainly to link to third-party scripts integrating 363# into gitweb; e.g. git-browser for graphical history representation 364# or custom web-based repository administration interface. 365 366# The 'default' value consists of a list of triplets in the form 367# (label, link, position) where position is the label after which 368# to insert the link and link is a format string where %n expands 369# to the project name, %f to the project path within the filesystem, 370# %h to the current hash (h gitweb parameter) and %b to the current 371# hash base (hb gitweb parameter); %% expands to %. 372 373# To enable system wide have in $GITWEB_CONFIG e.g. 374# $feature{'actions'}{'default'} = [('graphiclog', 375# '/git-browser/by-commit.html?r=%n', 'summary')]; 376# Project specific override is not supported. 377'actions'=> { 378'override'=>0, 379'default'=> []}, 380 381# Allow gitweb scan project content tags described in ctags/ 382# of project repository, and display the popular Web 2.0-ish 383# "tag cloud" near the project list. Note that this is something 384# COMPLETELY different from the normal Git tags. 385 386# gitweb by itself can show existing tags, but it does not handle 387# tagging itself; you need an external application for that. 388# For an example script, check Girocco's cgi/tagproj.cgi. 389# You may want to install the HTML::TagCloud Perl module to get 390# a pretty tag cloud instead of just a list of tags. 391 392# To enable system wide have in $GITWEB_CONFIG 393# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 394# Project specific override is not supported. 395'ctags'=> { 396'override'=>0, 397'default'=> [0]}, 398 399# The maximum number of patches in a patchset generated in patch 400# view. Set this to 0 or undef to disable patch view, or to a 401# negative number to remove any limit. 402 403# To disable system wide have in $GITWEB_CONFIG 404# $feature{'patches'}{'default'} = [0]; 405# To have project specific config enable override in $GITWEB_CONFIG 406# $feature{'patches'}{'override'} = 1; 407# and in project config gitweb.patches = 0|n; 408# where n is the maximum number of patches allowed in a patchset. 409'patches'=> { 410'sub'=> \&feature_patches, 411'override'=>0, 412'default'=> [16]}, 413 414# Avatar support. When this feature is enabled, views such as 415# shortlog or commit will display an avatar associated with 416# the email of the committer(s) and/or author(s). 417 418# Currently available providers are gravatar and picon. 419# If an unknown provider is specified, the feature is disabled. 420 421# Gravatar depends on Digest::MD5. 422# Picon currently relies on the indiana.edu database. 423 424# To enable system wide have in $GITWEB_CONFIG 425# $feature{'avatar'}{'default'} = ['<provider>']; 426# where <provider> is either gravatar or picon. 427# To have project specific config enable override in $GITWEB_CONFIG 428# $feature{'avatar'}{'override'} = 1; 429# and in project config gitweb.avatar = <provider>; 430'avatar'=> { 431'sub'=> \&feature_avatar, 432'override'=>0, 433'default'=> ['']}, 434 435# Enable displaying how much time and how many git commands 436# it took to generate and display page. Disabled by default. 437# Project specific override is not supported. 438'timed'=> { 439'override'=>0, 440'default'=> [0]}, 441 442# Enable turning some links into links to actions which require 443# JavaScript to run (like 'blame_incremental'). Not enabled by 444# default. Project specific override is currently not supported. 445'javascript-actions'=> { 446'override'=>0, 447'default'=> [0]}, 448); 449 450sub gitweb_get_feature { 451my($name) =@_; 452return unlessexists$feature{$name}; 453my($sub,$override,@defaults) = ( 454$feature{$name}{'sub'}, 455$feature{$name}{'override'}, 456@{$feature{$name}{'default'}}); 457# project specific override is possible only if we have project 458our$git_dir;# global variable, declared later 459if(!$override|| !defined$git_dir) { 460return@defaults; 461} 462if(!defined$sub) { 463warn"feature$nameis not overridable"; 464return@defaults; 465} 466return$sub->(@defaults); 467} 468 469# A wrapper to check if a given feature is enabled. 470# With this, you can say 471# 472# my $bool_feat = gitweb_check_feature('bool_feat'); 473# gitweb_check_feature('bool_feat') or somecode; 474# 475# instead of 476# 477# my ($bool_feat) = gitweb_get_feature('bool_feat'); 478# (gitweb_get_feature('bool_feat'))[0] or somecode; 479# 480sub gitweb_check_feature { 481return(gitweb_get_feature(@_))[0]; 482} 483 484 485sub feature_bool { 486my$key=shift; 487my($val) = git_get_project_config($key,'--bool'); 488 489if(!defined$val) { 490return($_[0]); 491}elsif($valeq'true') { 492return(1); 493}elsif($valeq'false') { 494return(0); 495} 496} 497 498sub feature_snapshot { 499my(@fmts) =@_; 500 501my($val) = git_get_project_config('snapshot'); 502 503if($val) { 504@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 505} 506 507return@fmts; 508} 509 510sub feature_patches { 511my@val= (git_get_project_config('patches','--int')); 512 513if(@val) { 514return@val; 515} 516 517return($_[0]); 518} 519 520sub feature_avatar { 521my@val= (git_get_project_config('avatar')); 522 523return@val?@val:@_; 524} 525 526# checking HEAD file with -e is fragile if the repository was 527# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 528# and then pruned. 529sub check_head_link { 530my($dir) =@_; 531my$headfile="$dir/HEAD"; 532return((-e $headfile) || 533(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 534} 535 536sub check_export_ok { 537my($dir) =@_; 538return(check_head_link($dir) && 539(!$export_ok|| -e "$dir/$export_ok") && 540(!$export_auth_hook||$export_auth_hook->($dir))); 541} 542 543# process alternate names for backward compatibility 544# filter out unsupported (unknown) snapshot formats 545sub filter_snapshot_fmts { 546my@fmts=@_; 547 548@fmts=map{ 549exists$known_snapshot_format_aliases{$_} ? 550$known_snapshot_format_aliases{$_} :$_}@fmts; 551@fmts=grep{ 552exists$known_snapshot_formats{$_} && 553!$known_snapshot_formats{$_}{'disabled'}}@fmts; 554} 555 556our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 557our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 558# die if there are errors parsing config file 559if(-e $GITWEB_CONFIG) { 560do$GITWEB_CONFIG; 561die$@if$@; 562}elsif(-e $GITWEB_CONFIG_SYSTEM) { 563do$GITWEB_CONFIG_SYSTEM; 564die$@if$@; 565} 566 567# Get loadavg of system, to compare against $maxload. 568# Currently it requires '/proc/loadavg' present to get loadavg; 569# if it is not present it returns 0, which means no load checking. 570sub get_loadavg { 571if( -e '/proc/loadavg'){ 572open my$fd,'<','/proc/loadavg' 573orreturn0; 574my@load=split(/\s+/,scalar<$fd>); 575close$fd; 576 577# The first three columns measure CPU and IO utilization of the last one, 578# five, and 10 minute periods. The fourth column shows the number of 579# currently running processes and the total number of processes in the m/n 580# format. The last column displays the last process ID used. 581return$load[0] ||0; 582} 583# additional checks for load average should go here for things that don't export 584# /proc/loadavg 585 586return0; 587} 588 589# version of the core git binary 590our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 591$number_of_git_cmds++; 592 593$projects_list||=$projectroot; 594 595if(defined$maxload&& get_loadavg() >$maxload) { 596 die_error(503,"The load average on the server is too high"); 597} 598 599# ====================================================================== 600# input validation and dispatch 601 602# input parameters can be collected from a variety of sources (presently, CGI 603# and PATH_INFO), so we define an %input_params hash that collects them all 604# together during validation: this allows subsequent uses (e.g. href()) to be 605# agnostic of the parameter origin 606 607our%input_params= (); 608 609# input parameters are stored with the long parameter name as key. This will 610# also be used in the href subroutine to convert parameters to their CGI 611# equivalent, and since the href() usage is the most frequent one, we store 612# the name -> CGI key mapping here, instead of the reverse. 613# 614# XXX: Warning: If you touch this, check the search form for updating, 615# too. 616 617our@cgi_param_mapping= ( 618 project =>"p", 619 action =>"a", 620 file_name =>"f", 621 file_parent =>"fp", 622 hash =>"h", 623 hash_parent =>"hp", 624 hash_base =>"hb", 625 hash_parent_base =>"hpb", 626 page =>"pg", 627 order =>"o", 628 searchtext =>"s", 629 searchtype =>"st", 630 snapshot_format =>"sf", 631 extra_options =>"opt", 632 search_use_regexp =>"sr", 633# this must be last entry (for manipulation from JavaScript) 634 javascript =>"js" 635); 636our%cgi_param_mapping=@cgi_param_mapping; 637 638# we will also need to know the possible actions, for validation 639our%actions= ( 640"blame"=> \&git_blame, 641"blame_incremental"=> \&git_blame_incremental, 642"blame_data"=> \&git_blame_data, 643"blobdiff"=> \&git_blobdiff, 644"blobdiff_plain"=> \&git_blobdiff_plain, 645"blob"=> \&git_blob, 646"blob_plain"=> \&git_blob_plain, 647"commitdiff"=> \&git_commitdiff, 648"commitdiff_plain"=> \&git_commitdiff_plain, 649"commit"=> \&git_commit, 650"forks"=> \&git_forks, 651"heads"=> \&git_heads, 652"history"=> \&git_history, 653"log"=> \&git_log, 654"patch"=> \&git_patch, 655"patches"=> \&git_patches, 656"rss"=> \&git_rss, 657"atom"=> \&git_atom, 658"search"=> \&git_search, 659"search_help"=> \&git_search_help, 660"shortlog"=> \&git_shortlog, 661"summary"=> \&git_summary, 662"tag"=> \&git_tag, 663"tags"=> \&git_tags, 664"tree"=> \&git_tree, 665"snapshot"=> \&git_snapshot, 666"object"=> \&git_object, 667# those below don't need $project 668"opml"=> \&git_opml, 669"project_list"=> \&git_project_list, 670"project_index"=> \&git_project_index, 671); 672 673# finally, we have the hash of allowed extra_options for the commands that 674# allow them 675our%allowed_options= ( 676"--no-merges"=> [qw(rss atom log shortlog history)], 677); 678 679# fill %input_params with the CGI parameters. All values except for 'opt' 680# should be single values, but opt can be an array. We should probably 681# build an array of parameters that can be multi-valued, but since for the time 682# being it's only this one, we just single it out 683while(my($name,$symbol) =each%cgi_param_mapping) { 684if($symboleq'opt') { 685$input_params{$name} = [$cgi->param($symbol) ]; 686}else{ 687$input_params{$name} =$cgi->param($symbol); 688} 689} 690 691# now read PATH_INFO and update the parameter list for missing parameters 692sub evaluate_path_info { 693return ifdefined$input_params{'project'}; 694return if!$path_info; 695$path_info=~ s,^/+,,; 696return if!$path_info; 697 698# find which part of PATH_INFO is project 699my$project=$path_info; 700$project=~ s,/+$,,; 701while($project&& !check_head_link("$projectroot/$project")) { 702$project=~ s,/*[^/]*$,,; 703} 704return unless$project; 705$input_params{'project'} =$project; 706 707# do not change any parameters if an action is given using the query string 708return if$input_params{'action'}; 709$path_info=~ s,^\Q$project\E/*,,; 710 711# next, check if we have an action 712my$action=$path_info; 713$action=~ s,/.*$,,; 714if(exists$actions{$action}) { 715$path_info=~ s,^$action/*,,; 716$input_params{'action'} =$action; 717} 718 719# list of actions that want hash_base instead of hash, but can have no 720# pathname (f) parameter 721my@wants_base= ( 722'tree', 723'history', 724); 725 726# we want to catch 727# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 728my($parentrefname,$parentpathname,$refname,$pathname) = 729($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 730 731# first, analyze the 'current' part 732if(defined$pathname) { 733# we got "branch:filename" or "branch:dir/" 734# we could use git_get_type(branch:pathname), but: 735# - it needs $git_dir 736# - it does a git() call 737# - the convention of terminating directories with a slash 738# makes it superfluous 739# - embedding the action in the PATH_INFO would make it even 740# more superfluous 741$pathname=~ s,^/+,,; 742if(!$pathname||substr($pathname, -1)eq"/") { 743$input_params{'action'} ||="tree"; 744$pathname=~ s,/$,,; 745}else{ 746# the default action depends on whether we had parent info 747# or not 748if($parentrefname) { 749$input_params{'action'} ||="blobdiff_plain"; 750}else{ 751$input_params{'action'} ||="blob_plain"; 752} 753} 754$input_params{'hash_base'} ||=$refname; 755$input_params{'file_name'} ||=$pathname; 756}elsif(defined$refname) { 757# we got "branch". In this case we have to choose if we have to 758# set hash or hash_base. 759# 760# Most of the actions without a pathname only want hash to be 761# set, except for the ones specified in @wants_base that want 762# hash_base instead. It should also be noted that hand-crafted 763# links having 'history' as an action and no pathname or hash 764# set will fail, but that happens regardless of PATH_INFO. 765$input_params{'action'} ||="shortlog"; 766if(grep{$_eq$input_params{'action'} }@wants_base) { 767$input_params{'hash_base'} ||=$refname; 768}else{ 769$input_params{'hash'} ||=$refname; 770} 771} 772 773# next, handle the 'parent' part, if present 774if(defined$parentrefname) { 775# a missing pathspec defaults to the 'current' filename, allowing e.g. 776# someproject/blobdiff/oldrev..newrev:/filename 777if($parentpathname) { 778$parentpathname=~ s,^/+,,; 779$parentpathname=~ s,/$,,; 780$input_params{'file_parent'} ||=$parentpathname; 781}else{ 782$input_params{'file_parent'} ||=$input_params{'file_name'}; 783} 784# we assume that hash_parent_base is wanted if a path was specified, 785# or if the action wants hash_base instead of hash 786if(defined$input_params{'file_parent'} || 787grep{$_eq$input_params{'action'} }@wants_base) { 788$input_params{'hash_parent_base'} ||=$parentrefname; 789}else{ 790$input_params{'hash_parent'} ||=$parentrefname; 791} 792} 793 794# for the snapshot action, we allow URLs in the form 795# $project/snapshot/$hash.ext 796# where .ext determines the snapshot and gets removed from the 797# passed $refname to provide the $hash. 798# 799# To be able to tell that $refname includes the format extension, we 800# require the following two conditions to be satisfied: 801# - the hash input parameter MUST have been set from the $refname part 802# of the URL (i.e. they must be equal) 803# - the snapshot format MUST NOT have been defined already (e.g. from 804# CGI parameter sf) 805# It's also useless to try any matching unless $refname has a dot, 806# so we check for that too 807if(defined$input_params{'action'} && 808$input_params{'action'}eq'snapshot'&& 809defined$refname&&index($refname,'.') != -1&& 810$refnameeq$input_params{'hash'} && 811!defined$input_params{'snapshot_format'}) { 812# We loop over the known snapshot formats, checking for 813# extensions. Allowed extensions are both the defined suffix 814# (which includes the initial dot already) and the snapshot 815# format key itself, with a prepended dot 816while(my($fmt,$opt) =each%known_snapshot_formats) { 817my$hash=$refname; 818unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 819next; 820} 821my$sfx=$1; 822# a valid suffix was found, so set the snapshot format 823# and reset the hash parameter 824$input_params{'snapshot_format'} =$fmt; 825$input_params{'hash'} =$hash; 826# we also set the format suffix to the one requested 827# in the URL: this way a request for e.g. .tgz returns 828# a .tgz instead of a .tar.gz 829$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 830last; 831} 832} 833} 834evaluate_path_info(); 835 836our$action=$input_params{'action'}; 837if(defined$action) { 838if(!validate_action($action)) { 839 die_error(400,"Invalid action parameter"); 840} 841} 842 843# parameters which are pathnames 844our$project=$input_params{'project'}; 845if(defined$project) { 846if(!validate_project($project)) { 847undef$project; 848 die_error(404,"No such project"); 849} 850} 851 852our$file_name=$input_params{'file_name'}; 853if(defined$file_name) { 854if(!validate_pathname($file_name)) { 855 die_error(400,"Invalid file parameter"); 856} 857} 858 859our$file_parent=$input_params{'file_parent'}; 860if(defined$file_parent) { 861if(!validate_pathname($file_parent)) { 862 die_error(400,"Invalid file parent parameter"); 863} 864} 865 866# parameters which are refnames 867our$hash=$input_params{'hash'}; 868if(defined$hash) { 869if(!validate_refname($hash)) { 870 die_error(400,"Invalid hash parameter"); 871} 872} 873 874our$hash_parent=$input_params{'hash_parent'}; 875if(defined$hash_parent) { 876if(!validate_refname($hash_parent)) { 877 die_error(400,"Invalid hash parent parameter"); 878} 879} 880 881our$hash_base=$input_params{'hash_base'}; 882if(defined$hash_base) { 883if(!validate_refname($hash_base)) { 884 die_error(400,"Invalid hash base parameter"); 885} 886} 887 888our@extra_options= @{$input_params{'extra_options'}}; 889# @extra_options is always defined, since it can only be (currently) set from 890# CGI, and $cgi->param() returns the empty array in array context if the param 891# is not set 892foreachmy$opt(@extra_options) { 893if(not exists$allowed_options{$opt}) { 894 die_error(400,"Invalid option parameter"); 895} 896if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 897 die_error(400,"Invalid option parameter for this action"); 898} 899} 900 901our$hash_parent_base=$input_params{'hash_parent_base'}; 902if(defined$hash_parent_base) { 903if(!validate_refname($hash_parent_base)) { 904 die_error(400,"Invalid hash parent base parameter"); 905} 906} 907 908# other parameters 909our$page=$input_params{'page'}; 910if(defined$page) { 911if($page=~m/[^0-9]/) { 912 die_error(400,"Invalid page parameter"); 913} 914} 915 916our$searchtype=$input_params{'searchtype'}; 917if(defined$searchtype) { 918if($searchtype=~m/[^a-z]/) { 919 die_error(400,"Invalid searchtype parameter"); 920} 921} 922 923our$search_use_regexp=$input_params{'search_use_regexp'}; 924 925our$searchtext=$input_params{'searchtext'}; 926our$search_regexp; 927if(defined$searchtext) { 928if(length($searchtext) <2) { 929 die_error(403,"At least two characters are required for search parameter"); 930} 931$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 932} 933 934# path to the current git repository 935our$git_dir; 936$git_dir="$projectroot/$project"if$project; 937 938# list of supported snapshot formats 939our@snapshot_fmts= gitweb_get_feature('snapshot'); 940@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 941 942# check that the avatar feature is set to a known provider name, 943# and for each provider check if the dependencies are satisfied. 944# if the provider name is invalid or the dependencies are not met, 945# reset $git_avatar to the empty string. 946our($git_avatar) = gitweb_get_feature('avatar'); 947if($git_avatareq'gravatar') { 948$git_avatar=''unless(eval{require Digest::MD5;1; }); 949}elsif($git_avatareq'picon') { 950# no dependencies 951}else{ 952$git_avatar=''; 953} 954 955# dispatch 956if(!defined$action) { 957if(defined$hash) { 958$action= git_get_type($hash); 959}elsif(defined$hash_base&&defined$file_name) { 960$action= git_get_type("$hash_base:$file_name"); 961}elsif(defined$project) { 962$action='summary'; 963}else{ 964$action='project_list'; 965} 966} 967if(!defined($actions{$action})) { 968 die_error(400,"Unknown action"); 969} 970if($action!~m/^(?:opml|project_list|project_index)$/&& 971!$project) { 972 die_error(400,"Project needed"); 973} 974$actions{$action}->(); 975exit; 976 977## ====================================================================== 978## action links 979 980sub href { 981my%params=@_; 982# default is to use -absolute url() i.e. $my_uri 983my$href=$params{-full} ?$my_url:$my_uri; 984 985$params{'project'} =$projectunlessexists$params{'project'}; 986 987if($params{-replay}) { 988while(my($name,$symbol) =each%cgi_param_mapping) { 989if(!exists$params{$name}) { 990$params{$name} =$input_params{$name}; 991} 992} 993} 994 995my$use_pathinfo= gitweb_check_feature('pathinfo'); 996if($use_pathinfoand defined$params{'project'}) { 997# try to put as many parameters as possible in PATH_INFO: 998# - project name 999# - action1000# - hash_parent or hash_parent_base:/file_parent1001# - hash or hash_base:/filename1002# - the snapshot_format as an appropriate suffix10031004# When the script is the root DirectoryIndex for the domain,1005# $href here would be something like http://gitweb.example.com/1006# Thus, we strip any trailing / from $href, to spare us double1007# slashes in the final URL1008$href=~ s,/$,,;10091010# Then add the project name, if present1011$href.="/".esc_url($params{'project'});1012delete$params{'project'};10131014# since we destructively absorb parameters, we keep this1015# boolean that remembers if we're handling a snapshot1016my$is_snapshot=$params{'action'}eq'snapshot';10171018# Summary just uses the project path URL, any other action is1019# added to the URL1020if(defined$params{'action'}) {1021$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1022delete$params{'action'};1023}10241025# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1026# stripping nonexistent or useless pieces1027$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1028||$params{'hash_parent'} ||$params{'hash'});1029if(defined$params{'hash_base'}) {1030if(defined$params{'hash_parent_base'}) {1031$href.= esc_url($params{'hash_parent_base'});1032# skip the file_parent if it's the same as the file_name1033if(defined$params{'file_parent'}) {1034if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1035delete$params{'file_parent'};1036}elsif($params{'file_parent'} !~/\.\./) {1037$href.=":/".esc_url($params{'file_parent'});1038delete$params{'file_parent'};1039}1040}1041$href.="..";1042delete$params{'hash_parent'};1043delete$params{'hash_parent_base'};1044}elsif(defined$params{'hash_parent'}) {1045$href.= esc_url($params{'hash_parent'})."..";1046delete$params{'hash_parent'};1047}10481049$href.= esc_url($params{'hash_base'});1050if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1051$href.=":/".esc_url($params{'file_name'});1052delete$params{'file_name'};1053}1054delete$params{'hash'};1055delete$params{'hash_base'};1056}elsif(defined$params{'hash'}) {1057$href.= esc_url($params{'hash'});1058delete$params{'hash'};1059}10601061# If the action was a snapshot, we can absorb the1062# snapshot_format parameter too1063if($is_snapshot) {1064my$fmt=$params{'snapshot_format'};1065# snapshot_format should always be defined when href()1066# is called, but just in case some code forgets, we1067# fall back to the default1068$fmt||=$snapshot_fmts[0];1069$href.=$known_snapshot_formats{$fmt}{'suffix'};1070delete$params{'snapshot_format'};1071}1072}10731074# now encode the parameters explicitly1075my@result= ();1076for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1077my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1078if(defined$params{$name}) {1079if(ref($params{$name})eq"ARRAY") {1080foreachmy$par(@{$params{$name}}) {1081push@result,$symbol."=". esc_param($par);1082}1083}else{1084push@result,$symbol."=". esc_param($params{$name});1085}1086}1087}1088$href.="?".join(';',@result)ifscalar@result;10891090return$href;1091}109210931094## ======================================================================1095## validation, quoting/unquoting and escaping10961097sub validate_action {1098my$input=shift||returnundef;1099returnundefunlessexists$actions{$input};1100return$input;1101}11021103sub validate_project {1104my$input=shift||returnundef;1105if(!validate_pathname($input) ||1106!(-d "$projectroot/$input") ||1107!check_export_ok("$projectroot/$input") ||1108($strict_export&& !project_in_list($input))) {1109returnundef;1110}else{1111return$input;1112}1113}11141115sub validate_pathname {1116my$input=shift||returnundef;11171118# no '.' or '..' as elements of path, i.e. no '.' nor '..'1119# at the beginning, at the end, and between slashes.1120# also this catches doubled slashes1121if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1122returnundef;1123}1124# no null characters1125if($input=~m!\0!) {1126returnundef;1127}1128return$input;1129}11301131sub validate_refname {1132my$input=shift||returnundef;11331134# textual hashes are O.K.1135if($input=~m/^[0-9a-fA-F]{40}$/) {1136return$input;1137}1138# it must be correct pathname1139$input= validate_pathname($input)1140orreturnundef;1141# restrictions on ref name according to git-check-ref-format1142if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1143returnundef;1144}1145return$input;1146}11471148# decode sequences of octets in utf8 into Perl's internal form,1149# which is utf-8 with utf8 flag set if needed. gitweb writes out1150# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1151sub to_utf8 {1152my$str=shift;1153if(utf8::valid($str)) {1154 utf8::decode($str);1155return$str;1156}else{1157return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1158}1159}11601161# quote unsafe chars, but keep the slash, even when it's not1162# correct, but quoted slashes look too horrible in bookmarks1163sub esc_param {1164my$str=shift;1165$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1166$str=~s/ /\+/g;1167return$str;1168}11691170# quote unsafe chars in whole URL, so some charactrs cannot be quoted1171sub esc_url {1172my$str=shift;1173$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1174$str=~s/\+/%2B/g;1175$str=~s/ /\+/g;1176return$str;1177}11781179# replace invalid utf8 character with SUBSTITUTION sequence1180sub esc_html {1181my$str=shift;1182my%opts=@_;11831184$str= to_utf8($str);1185$str=$cgi->escapeHTML($str);1186if($opts{'-nbsp'}) {1187$str=~s/ / /g;1188}1189$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1190return$str;1191}11921193# quote control characters and escape filename to HTML1194sub esc_path {1195my$str=shift;1196my%opts=@_;11971198$str= to_utf8($str);1199$str=$cgi->escapeHTML($str);1200if($opts{'-nbsp'}) {1201$str=~s/ / /g;1202}1203$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1204return$str;1205}12061207# Make control characters "printable", using character escape codes (CEC)1208sub quot_cec {1209my$cntrl=shift;1210my%opts=@_;1211my%es= (# character escape codes, aka escape sequences1212"\t"=>'\t',# tab (HT)1213"\n"=>'\n',# line feed (LF)1214"\r"=>'\r',# carrige return (CR)1215"\f"=>'\f',# form feed (FF)1216"\b"=>'\b',# backspace (BS)1217"\a"=>'\a',# alarm (bell) (BEL)1218"\e"=>'\e',# escape (ESC)1219"\013"=>'\v',# vertical tab (VT)1220"\000"=>'\0',# nul character (NUL)1221);1222my$chr= ( (exists$es{$cntrl})1223?$es{$cntrl}1224:sprintf('\%2x',ord($cntrl)) );1225if($opts{-nohtml}) {1226return$chr;1227}else{1228return"<span class=\"cntrl\">$chr</span>";1229}1230}12311232# Alternatively use unicode control pictures codepoints,1233# Unicode "printable representation" (PR)1234sub quot_upr {1235my$cntrl=shift;1236my%opts=@_;12371238my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1239if($opts{-nohtml}) {1240return$chr;1241}else{1242return"<span class=\"cntrl\">$chr</span>";1243}1244}12451246# git may return quoted and escaped filenames1247sub unquote {1248my$str=shift;12491250sub unq {1251my$seq=shift;1252my%es= (# character escape codes, aka escape sequences1253't'=>"\t",# tab (HT, TAB)1254'n'=>"\n",# newline (NL)1255'r'=>"\r",# return (CR)1256'f'=>"\f",# form feed (FF)1257'b'=>"\b",# backspace (BS)1258'a'=>"\a",# alarm (bell) (BEL)1259'e'=>"\e",# escape (ESC)1260'v'=>"\013",# vertical tab (VT)1261);12621263if($seq=~m/^[0-7]{1,3}$/) {1264# octal char sequence1265returnchr(oct($seq));1266}elsif(exists$es{$seq}) {1267# C escape sequence, aka character escape code1268return$es{$seq};1269}1270# quoted ordinary character1271return$seq;1272}12731274if($str=~m/^"(.*)"$/) {1275# needs unquoting1276$str=$1;1277$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1278}1279return$str;1280}12811282# escape tabs (convert tabs to spaces)1283sub untabify {1284my$line=shift;12851286while((my$pos=index($line,"\t")) != -1) {1287if(my$count= (8- ($pos%8))) {1288my$spaces=' ' x $count;1289$line=~s/\t/$spaces/;1290}1291}12921293return$line;1294}12951296sub project_in_list {1297my$project=shift;1298my@list= git_get_projects_list();1299return@list&&scalar(grep{$_->{'path'}eq$project}@list);1300}13011302## ----------------------------------------------------------------------1303## HTML aware string manipulation13041305# Try to chop given string on a word boundary between position1306# $len and $len+$add_len. If there is no word boundary there,1307# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1308# (marking chopped part) would be longer than given string.1309sub chop_str {1310my$str=shift;1311my$len=shift;1312my$add_len=shift||10;1313my$where=shift||'right';# 'left' | 'center' | 'right'13141315# Make sure perl knows it is utf8 encoded so we don't1316# cut in the middle of a utf8 multibyte char.1317$str= to_utf8($str);13181319# allow only $len chars, but don't cut a word if it would fit in $add_len1320# if it doesn't fit, cut it if it's still longer than the dots we would add1321# remove chopped character entities entirely13221323# when chopping in the middle, distribute $len into left and right part1324# return early if chopping wouldn't make string shorter1325if($whereeq'center') {1326return$strif($len+5>=length($str));# filler is length 51327$len=int($len/2);1328}else{1329return$strif($len+4>=length($str));# filler is length 41330}13311332# regexps: ending and beginning with word part up to $add_len1333my$endre=qr/.{$len}\w{0,$add_len}/;1334my$begre=qr/\w{0,$add_len}.{$len}/;13351336if($whereeq'left') {1337$str=~m/^(.*?)($begre)$/;1338my($lead,$body) = ($1,$2);1339if(length($lead) >4) {1340$lead=" ...";1341}1342return"$lead$body";13431344}elsif($whereeq'center') {1345$str=~m/^($endre)(.*)$/;1346my($left,$str) = ($1,$2);1347$str=~m/^(.*?)($begre)$/;1348my($mid,$right) = ($1,$2);1349if(length($mid) >5) {1350$mid=" ... ";1351}1352return"$left$mid$right";13531354}else{1355$str=~m/^($endre)(.*)$/;1356my$body=$1;1357my$tail=$2;1358if(length($tail) >4) {1359$tail="... ";1360}1361return"$body$tail";1362}1363}13641365# takes the same arguments as chop_str, but also wraps a <span> around the1366# result with a title attribute if it does get chopped. Additionally, the1367# string is HTML-escaped.1368sub chop_and_escape_str {1369my($str) =@_;13701371my$chopped= chop_str(@_);1372if($choppedeq$str) {1373return esc_html($chopped);1374}else{1375$str=~s/[[:cntrl:]]/?/g;1376return$cgi->span({-title=>$str}, esc_html($chopped));1377}1378}13791380## ----------------------------------------------------------------------1381## functions returning short strings13821383# CSS class for given age value (in seconds)1384sub age_class {1385my$age=shift;13861387if(!defined$age) {1388return"noage";1389}elsif($age<60*60*2) {1390return"age0";1391}elsif($age<60*60*24*2) {1392return"age1";1393}else{1394return"age2";1395}1396}13971398# convert age in seconds to "nn units ago" string1399sub age_string {1400my$age=shift;1401my$age_str;14021403if($age>60*60*24*365*2) {1404$age_str= (int$age/60/60/24/365);1405$age_str.=" years ago";1406}elsif($age>60*60*24*(365/12)*2) {1407$age_str=int$age/60/60/24/(365/12);1408$age_str.=" months ago";1409}elsif($age>60*60*24*7*2) {1410$age_str=int$age/60/60/24/7;1411$age_str.=" weeks ago";1412}elsif($age>60*60*24*2) {1413$age_str=int$age/60/60/24;1414$age_str.=" days ago";1415}elsif($age>60*60*2) {1416$age_str=int$age/60/60;1417$age_str.=" hours ago";1418}elsif($age>60*2) {1419$age_str=int$age/60;1420$age_str.=" min ago";1421}elsif($age>2) {1422$age_str=int$age;1423$age_str.=" sec ago";1424}else{1425$age_str.=" right now";1426}1427return$age_str;1428}14291430useconstant{1431 S_IFINVALID =>0030000,1432 S_IFGITLINK =>0160000,1433};14341435# submodule/subproject, a commit object reference1436sub S_ISGITLINK {1437my$mode=shift;14381439return(($mode& S_IFMT) == S_IFGITLINK)1440}14411442# convert file mode in octal to symbolic file mode string1443sub mode_str {1444my$mode=oct shift;14451446if(S_ISGITLINK($mode)) {1447return'm---------';1448}elsif(S_ISDIR($mode& S_IFMT)) {1449return'drwxr-xr-x';1450}elsif(S_ISLNK($mode)) {1451return'lrwxrwxrwx';1452}elsif(S_ISREG($mode)) {1453# git cares only about the executable bit1454if($mode& S_IXUSR) {1455return'-rwxr-xr-x';1456}else{1457return'-rw-r--r--';1458};1459}else{1460return'----------';1461}1462}14631464# convert file mode in octal to file type string1465sub file_type {1466my$mode=shift;14671468if($mode!~m/^[0-7]+$/) {1469return$mode;1470}else{1471$mode=oct$mode;1472}14731474if(S_ISGITLINK($mode)) {1475return"submodule";1476}elsif(S_ISDIR($mode& S_IFMT)) {1477return"directory";1478}elsif(S_ISLNK($mode)) {1479return"symlink";1480}elsif(S_ISREG($mode)) {1481return"file";1482}else{1483return"unknown";1484}1485}14861487# convert file mode in octal to file type description string1488sub file_type_long {1489my$mode=shift;14901491if($mode!~m/^[0-7]+$/) {1492return$mode;1493}else{1494$mode=oct$mode;1495}14961497if(S_ISGITLINK($mode)) {1498return"submodule";1499}elsif(S_ISDIR($mode& S_IFMT)) {1500return"directory";1501}elsif(S_ISLNK($mode)) {1502return"symlink";1503}elsif(S_ISREG($mode)) {1504if($mode& S_IXUSR) {1505return"executable";1506}else{1507return"file";1508};1509}else{1510return"unknown";1511}1512}151315141515## ----------------------------------------------------------------------1516## functions returning short HTML fragments, or transforming HTML fragments1517## which don't belong to other sections15181519# format line of commit message.1520sub format_log_line_html {1521my$line=shift;15221523$line= esc_html($line, -nbsp=>1);1524$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1525$cgi->a({-href => href(action=>"object", hash=>$1),1526-class=>"text"},$1);1527}eg;15281529return$line;1530}15311532# format marker of refs pointing to given object15331534# the destination action is chosen based on object type and current context:1535# - for annotated tags, we choose the tag view unless it's the current view1536# already, in which case we go to shortlog view1537# - for other refs, we keep the current view if we're in history, shortlog or1538# log view, and select shortlog otherwise1539sub format_ref_marker {1540my($refs,$id) =@_;1541my$markers='';15421543if(defined$refs->{$id}) {1544foreachmy$ref(@{$refs->{$id}}) {1545# this code exploits the fact that non-lightweight tags are the1546# only indirect objects, and that they are the only objects for which1547# we want to use tag instead of shortlog as action1548my($type,$name) =qw();1549my$indirect= ($ref=~s/\^\{\}$//);1550# e.g. tags/v2.6.11 or heads/next1551if($ref=~m!^(.*?)s?/(.*)$!) {1552$type=$1;1553$name=$2;1554}else{1555$type="ref";1556$name=$ref;1557}15581559my$class=$type;1560$class.=" indirect"if$indirect;15611562my$dest_action="shortlog";15631564if($indirect) {1565$dest_action="tag"unless$actioneq"tag";1566}elsif($action=~/^(history|(short)?log)$/) {1567$dest_action=$action;1568}15691570my$dest="";1571$dest.="refs/"unless$ref=~ m!^refs/!;1572$dest.=$ref;15731574my$link=$cgi->a({1575-href => href(1576 action=>$dest_action,1577 hash=>$dest1578)},$name);15791580$markers.=" <span class=\"$class\"title=\"$ref\">".1581$link."</span>";1582}1583}15841585if($markers) {1586return' <span class="refs">'.$markers.'</span>';1587}else{1588return"";1589}1590}15911592# format, perhaps shortened and with markers, title line1593sub format_subject_html {1594my($long,$short,$href,$extra) =@_;1595$extra=''unlessdefined($extra);15961597if(length($short) <length($long)) {1598$long=~s/[[:cntrl:]]/?/g;1599return$cgi->a({-href =>$href, -class=>"list subject",1600-title => to_utf8($long)},1601 esc_html($short)) .$extra;1602}else{1603return$cgi->a({-href =>$href, -class=>"list subject"},1604 esc_html($long)) .$extra;1605}1606}16071608# Rather than recomputing the url for an email multiple times, we cache it1609# after the first hit. This gives a visible benefit in views where the avatar1610# for the same email is used repeatedly (e.g. shortlog).1611# The cache is shared by all avatar engines (currently gravatar only), which1612# are free to use it as preferred. Since only one avatar engine is used for any1613# given page, there's no risk for cache conflicts.1614our%avatar_cache= ();16151616# Compute the picon url for a given email, by using the picon search service over at1617# http://www.cs.indiana.edu/picons/search.html1618sub picon_url {1619my$email=lc shift;1620if(!$avatar_cache{$email}) {1621my($user,$domain) =split('@',$email);1622$avatar_cache{$email} =1623"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1624"$domain/$user/".1625"users+domains+unknown/up/single";1626}1627return$avatar_cache{$email};1628}16291630# Compute the gravatar url for a given email, if it's not in the cache already.1631# Gravatar stores only the part of the URL before the size, since that's the1632# one computationally more expensive. This also allows reuse of the cache for1633# different sizes (for this particular engine).1634sub gravatar_url {1635my$email=lc shift;1636my$size=shift;1637$avatar_cache{$email} ||=1638"http://www.gravatar.com/avatar/".1639 Digest::MD5::md5_hex($email) ."?s=";1640return$avatar_cache{$email} .$size;1641}16421643# Insert an avatar for the given $email at the given $size if the feature1644# is enabled.1645sub git_get_avatar {1646my($email,%opts) =@_;1647my$pre_white= ($opts{-pad_before} ?" ":"");1648my$post_white= ($opts{-pad_after} ?" ":"");1649$opts{-size} ||='default';1650my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1651my$url="";1652if($git_avatareq'gravatar') {1653$url= gravatar_url($email,$size);1654}elsif($git_avatareq'picon') {1655$url= picon_url($email);1656}1657# Other providers can be added by extending the if chain, defining $url1658# as needed. If no variant puts something in $url, we assume avatars1659# are completely disabled/unavailable.1660if($url) {1661return$pre_white.1662"<img width=\"$size\"".1663"class=\"avatar\"".1664"src=\"$url\"".1665"alt=\"\"".1666"/>".$post_white;1667}else{1668return"";1669}1670}16711672sub format_search_author {1673my($author,$searchtype,$displaytext) =@_;1674my$have_search= gitweb_check_feature('search');16751676if($have_search) {1677my$performed="";1678if($searchtypeeq'author') {1679$performed="authored";1680}elsif($searchtypeeq'committer') {1681$performed="committed";1682}16831684return$cgi->a({-href => href(action=>"search", hash=>$hash,1685 searchtext=>$author,1686 searchtype=>$searchtype),class=>"list",1687 title=>"Search for commits$performedby$author"},1688$displaytext);16891690}else{1691return$displaytext;1692}1693}16941695# format the author name of the given commit with the given tag1696# the author name is chopped and escaped according to the other1697# optional parameters (see chop_str).1698sub format_author_html {1699my$tag=shift;1700my$co=shift;1701my$author= chop_and_escape_str($co->{'author_name'},@_);1702return"<$tagclass=\"author\">".1703 format_search_author($co->{'author_name'},"author",1704 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1705$author) .1706"</$tag>";1707}17081709# format git diff header line, i.e. "diff --(git|combined|cc) ..."1710sub format_git_diff_header_line {1711my$line=shift;1712my$diffinfo=shift;1713my($from,$to) =@_;17141715if($diffinfo->{'nparents'}) {1716# combined diff1717$line=~s!^(diff (.*?) )"?.*$!$1!;1718if($to->{'href'}) {1719$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1720 esc_path($to->{'file'}));1721}else{# file was deleted (no href)1722$line.= esc_path($to->{'file'});1723}1724}else{1725# "ordinary" diff1726$line=~s!^(diff (.*?) )"?a/.*$!$1!;1727if($from->{'href'}) {1728$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1729'a/'. esc_path($from->{'file'}));1730}else{# file was added (no href)1731$line.='a/'. esc_path($from->{'file'});1732}1733$line.=' ';1734if($to->{'href'}) {1735$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1736'b/'. esc_path($to->{'file'}));1737}else{# file was deleted1738$line.='b/'. esc_path($to->{'file'});1739}1740}17411742return"<div class=\"diff header\">$line</div>\n";1743}17441745# format extended diff header line, before patch itself1746sub format_extended_diff_header_line {1747my$line=shift;1748my$diffinfo=shift;1749my($from,$to) =@_;17501751# match <path>1752if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1753$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1754 esc_path($from->{'file'}));1755}1756if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1757$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1758 esc_path($to->{'file'}));1759}1760# match single <mode>1761if($line=~m/\s(\d{6})$/) {1762$line.='<span class="info"> ('.1763 file_type_long($1) .1764')</span>';1765}1766# match <hash>1767if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1768# can match only for combined diff1769$line='index ';1770for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1771if($from->{'href'}[$i]) {1772$line.=$cgi->a({-href=>$from->{'href'}[$i],1773-class=>"hash"},1774substr($diffinfo->{'from_id'}[$i],0,7));1775}else{1776$line.='0' x 7;1777}1778# separator1779$line.=','if($i<$diffinfo->{'nparents'} -1);1780}1781$line.='..';1782if($to->{'href'}) {1783$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1784substr($diffinfo->{'to_id'},0,7));1785}else{1786$line.='0' x 7;1787}17881789}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1790# can match only for ordinary diff1791my($from_link,$to_link);1792if($from->{'href'}) {1793$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1794substr($diffinfo->{'from_id'},0,7));1795}else{1796$from_link='0' x 7;1797}1798if($to->{'href'}) {1799$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1800substr($diffinfo->{'to_id'},0,7));1801}else{1802$to_link='0' x 7;1803}1804my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1805$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1806}18071808return$line."<br/>\n";1809}18101811# format from-file/to-file diff header1812sub format_diff_from_to_header {1813my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1814my$line;1815my$result='';18161817$line=$from_line;1818#assert($line =~ m/^---/) if DEBUG;1819# no extra formatting for "^--- /dev/null"1820if(!$diffinfo->{'nparents'}) {1821# ordinary (single parent) diff1822if($line=~m!^--- "?a/!) {1823if($from->{'href'}) {1824$line='--- a/'.1825$cgi->a({-href=>$from->{'href'}, -class=>"path"},1826 esc_path($from->{'file'}));1827}else{1828$line='--- a/'.1829 esc_path($from->{'file'});1830}1831}1832$result.= qq!<div class="diff from_file">$line</div>\n!;18331834}else{1835# combined diff (merge commit)1836for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1837if($from->{'href'}[$i]) {1838$line='--- '.1839$cgi->a({-href=>href(action=>"blobdiff",1840 hash_parent=>$diffinfo->{'from_id'}[$i],1841 hash_parent_base=>$parents[$i],1842 file_parent=>$from->{'file'}[$i],1843 hash=>$diffinfo->{'to_id'},1844 hash_base=>$hash,1845 file_name=>$to->{'file'}),1846-class=>"path",1847-title=>"diff". ($i+1)},1848$i+1) .1849'/'.1850$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1851 esc_path($from->{'file'}[$i]));1852}else{1853$line='--- /dev/null';1854}1855$result.= qq!<div class="diff from_file">$line</div>\n!;1856}1857}18581859$line=$to_line;1860#assert($line =~ m/^\+\+\+/) if DEBUG;1861# no extra formatting for "^+++ /dev/null"1862if($line=~m!^\+\+\+ "?b/!) {1863if($to->{'href'}) {1864$line='+++ b/'.1865$cgi->a({-href=>$to->{'href'}, -class=>"path"},1866 esc_path($to->{'file'}));1867}else{1868$line='+++ b/'.1869 esc_path($to->{'file'});1870}1871}1872$result.= qq!<div class="diff to_file">$line</div>\n!;18731874return$result;1875}18761877# create note for patch simplified by combined diff1878sub format_diff_cc_simplified {1879my($diffinfo,@parents) =@_;1880my$result='';18811882$result.="<div class=\"diff header\">".1883"diff --cc ";1884if(!is_deleted($diffinfo)) {1885$result.=$cgi->a({-href => href(action=>"blob",1886 hash_base=>$hash,1887 hash=>$diffinfo->{'to_id'},1888 file_name=>$diffinfo->{'to_file'}),1889-class=>"path"},1890 esc_path($diffinfo->{'to_file'}));1891}else{1892$result.= esc_path($diffinfo->{'to_file'});1893}1894$result.="</div>\n".# class="diff header"1895"<div class=\"diff nodifferences\">".1896"Simple merge".1897"</div>\n";# class="diff nodifferences"18981899return$result;1900}19011902# format patch (diff) line (not to be used for diff headers)1903sub format_diff_line {1904my$line=shift;1905my($from,$to) =@_;1906my$diff_class="";19071908chomp$line;19091910if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1911# combined diff1912my$prefix=substr($line,0,scalar@{$from->{'href'}});1913if($line=~m/^\@{3}/) {1914$diff_class=" chunk_header";1915}elsif($line=~m/^\\/) {1916$diff_class=" incomplete";1917}elsif($prefix=~tr/+/+/) {1918$diff_class=" add";1919}elsif($prefix=~tr/-/-/) {1920$diff_class=" rem";1921}1922}else{1923# assume ordinary diff1924my$char=substr($line,0,1);1925if($chareq'+') {1926$diff_class=" add";1927}elsif($chareq'-') {1928$diff_class=" rem";1929}elsif($chareq'@') {1930$diff_class=" chunk_header";1931}elsif($chareq"\\") {1932$diff_class=" incomplete";1933}1934}1935$line= untabify($line);1936if($from&&$to&&$line=~m/^\@{2} /) {1937my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1938$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;19391940$from_lines=0unlessdefined$from_lines;1941$to_lines=0unlessdefined$to_lines;19421943if($from->{'href'}) {1944$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1945-class=>"list"},$from_text);1946}1947if($to->{'href'}) {1948$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1949-class=>"list"},$to_text);1950}1951$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1952"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1953return"<div class=\"diff$diff_class\">$line</div>\n";1954}elsif($from&&$to&&$line=~m/^\@{3}/) {1955my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1956my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);19571958@from_text=split(' ',$ranges);1959for(my$i=0;$i<@from_text; ++$i) {1960($from_start[$i],$from_nlines[$i]) =1961(split(',',substr($from_text[$i],1)),0);1962}19631964$to_text=pop@from_text;1965$to_start=pop@from_start;1966$to_nlines=pop@from_nlines;19671968$line="<span class=\"chunk_info\">$prefix";1969for(my$i=0;$i<@from_text; ++$i) {1970if($from->{'href'}[$i]) {1971$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1972-class=>"list"},$from_text[$i]);1973}else{1974$line.=$from_text[$i];1975}1976$line.=" ";1977}1978if($to->{'href'}) {1979$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1980-class=>"list"},$to_text);1981}else{1982$line.=$to_text;1983}1984$line.="$prefix</span>".1985"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1986return"<div class=\"diff$diff_class\">$line</div>\n";1987}1988return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1989}19901991# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1992# linked. Pass the hash of the tree/commit to snapshot.1993sub format_snapshot_links {1994my($hash) =@_;1995my$num_fmts=@snapshot_fmts;1996if($num_fmts>1) {1997# A parenthesized list of links bearing format names.1998# e.g. "snapshot (_tar.gz_ _zip_)"1999return"snapshot (".join(' ',map2000$cgi->a({2001-href => href(2002 action=>"snapshot",2003 hash=>$hash,2004 snapshot_format=>$_2005)2006},$known_snapshot_formats{$_}{'display'})2007,@snapshot_fmts) .")";2008}elsif($num_fmts==1) {2009# A single "snapshot" link whose tooltip bears the format name.2010# i.e. "_snapshot_"2011my($fmt) =@snapshot_fmts;2012return2013$cgi->a({2014-href => href(2015 action=>"snapshot",2016 hash=>$hash,2017 snapshot_format=>$fmt2018),2019-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2020},"snapshot");2021}else{# $num_fmts == 02022returnundef;2023}2024}20252026## ......................................................................2027## functions returning values to be passed, perhaps after some2028## transformation, to other functions; e.g. returning arguments to href()20292030# returns hash to be passed to href to generate gitweb URL2031# in -title key it returns description of link2032sub get_feed_info {2033my$format=shift||'Atom';2034my%res= (action =>lc($format));20352036# feed links are possible only for project views2037return unless(defined$project);2038# some views should link to OPML, or to generic project feed,2039# or don't have specific feed yet (so they should use generic)2040return if($action=~/^(?:tags|heads|forks|tag|search)$/x);20412042my$branch;2043# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2044# from tag links; this also makes possible to detect branch links2045if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2046(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2047$branch=$1;2048}2049# find log type for feed description (title)2050my$type='log';2051if(defined$file_name) {2052$type="history of$file_name";2053$type.="/"if($actioneq'tree');2054$type.=" on '$branch'"if(defined$branch);2055}else{2056$type="log of$branch"if(defined$branch);2057}20582059$res{-title} =$type;2060$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2061$res{'file_name'} =$file_name;20622063return%res;2064}20652066## ----------------------------------------------------------------------2067## git utility subroutines, invoking git commands20682069# returns path to the core git executable and the --git-dir parameter as list2070sub git_cmd {2071$number_of_git_cmds++;2072return$GIT,'--git-dir='.$git_dir;2073}20742075# quote the given arguments for passing them to the shell2076# quote_command("command", "arg 1", "arg with ' and ! characters")2077# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2078# Try to avoid using this function wherever possible.2079sub quote_command {2080returnjoin(' ',2081map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2082}20832084# get HEAD ref of given project as hash2085sub git_get_head_hash {2086return git_get_full_hash(shift,'HEAD');2087}20882089sub git_get_full_hash {2090return git_get_hash(@_);2091}20922093sub git_get_short_hash {2094return git_get_hash(@_,'--short=7');2095}20962097sub git_get_hash {2098my($project,$hash,@options) =@_;2099my$o_git_dir=$git_dir;2100my$retval=undef;2101$git_dir="$projectroot/$project";2102if(open my$fd,'-|', git_cmd(),'rev-parse',2103'--verify','-q',@options,$hash) {2104$retval= <$fd>;2105chomp$retvalifdefined$retval;2106close$fd;2107}2108if(defined$o_git_dir) {2109$git_dir=$o_git_dir;2110}2111return$retval;2112}21132114# get type of given object2115sub git_get_type {2116my$hash=shift;21172118open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2119my$type= <$fd>;2120close$fdorreturn;2121chomp$type;2122return$type;2123}21242125# repository configuration2126our$config_file='';2127our%config;21282129# store multiple values for single key as anonymous array reference2130# single values stored directly in the hash, not as [ <value> ]2131sub hash_set_multi {2132my($hash,$key,$value) =@_;21332134if(!exists$hash->{$key}) {2135$hash->{$key} =$value;2136}elsif(!ref$hash->{$key}) {2137$hash->{$key} = [$hash->{$key},$value];2138}else{2139push@{$hash->{$key}},$value;2140}2141}21422143# return hash of git project configuration2144# optionally limited to some section, e.g. 'gitweb'2145sub git_parse_project_config {2146my$section_regexp=shift;2147my%config;21482149local$/="\0";21502151open my$fh,"-|", git_cmd(),"config",'-z','-l',2152orreturn;21532154while(my$keyval= <$fh>) {2155chomp$keyval;2156my($key,$value) =split(/\n/,$keyval,2);21572158 hash_set_multi(\%config,$key,$value)2159if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2160}2161close$fh;21622163return%config;2164}21652166# convert config value to boolean: 'true' or 'false'2167# no value, number > 0, 'true' and 'yes' values are true2168# rest of values are treated as false (never as error)2169sub config_to_bool {2170my$val=shift;21712172return1if!defined$val;# section.key21732174# strip leading and trailing whitespace2175$val=~s/^\s+//;2176$val=~s/\s+$//;21772178return(($val=~/^\d+$/&&$val) ||# section.key = 12179($val=~/^(?:true|yes)$/i));# section.key = true2180}21812182# convert config value to simple decimal number2183# an optional value suffix of 'k', 'm', or 'g' will cause the value2184# to be multiplied by 1024, 1048576, or 10737418242185sub config_to_int {2186my$val=shift;21872188# strip leading and trailing whitespace2189$val=~s/^\s+//;2190$val=~s/\s+$//;21912192if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2193$unit=lc($unit);2194# unknown unit is treated as 12195return$num* ($uniteq'g'?1073741824:2196$uniteq'm'?1048576:2197$uniteq'k'?1024:1);2198}2199return$val;2200}22012202# convert config value to array reference, if needed2203sub config_to_multi {2204my$val=shift;22052206returnref($val) ?$val: (defined($val) ? [$val] : []);2207}22082209sub git_get_project_config {2210my($key,$type) =@_;22112212return unlessdefined$git_dir;22132214# key sanity check2215return unless($key);2216$key=~s/^gitweb\.//;2217return if($key=~m/\W/);22182219# type sanity check2220if(defined$type) {2221$type=~s/^--//;2222$type=undef2223unless($typeeq'bool'||$typeeq'int');2224}22252226# get config2227if(!defined$config_file||2228$config_filene"$git_dir/config") {2229%config= git_parse_project_config('gitweb');2230$config_file="$git_dir/config";2231}22322233# check if config variable (key) exists2234return unlessexists$config{"gitweb.$key"};22352236# ensure given type2237if(!defined$type) {2238return$config{"gitweb.$key"};2239}elsif($typeeq'bool') {2240# backward compatibility: 'git config --bool' returns true/false2241return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2242}elsif($typeeq'int') {2243return config_to_int($config{"gitweb.$key"});2244}2245return$config{"gitweb.$key"};2246}22472248# get hash of given path at given ref2249sub git_get_hash_by_path {2250my$base=shift;2251my$path=shift||returnundef;2252my$type=shift;22532254$path=~ s,/+$,,;22552256open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2257or die_error(500,"Open git-ls-tree failed");2258my$line= <$fd>;2259close$fdorreturnundef;22602261if(!defined$line) {2262# there is no tree or hash given by $path at $base2263returnundef;2264}22652266#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2267$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2268if(defined$type&&$typene$2) {2269# type doesn't match2270returnundef;2271}2272return$3;2273}22742275# get path of entry with given hash at given tree-ish (ref)2276# used to get 'from' filename for combined diff (merge commit) for renames2277sub git_get_path_by_hash {2278my$base=shift||return;2279my$hash=shift||return;22802281local$/="\0";22822283open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2284orreturnundef;2285while(my$line= <$fd>) {2286chomp$line;22872288#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2289#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2290if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2291close$fd;2292return$1;2293}2294}2295close$fd;2296returnundef;2297}22982299## ......................................................................2300## git utility functions, directly accessing git repository23012302sub git_get_project_description {2303my$path=shift;23042305$git_dir="$projectroot/$path";2306open my$fd,'<',"$git_dir/description"2307orreturn git_get_project_config('description');2308my$descr= <$fd>;2309close$fd;2310if(defined$descr) {2311chomp$descr;2312}2313return$descr;2314}23152316sub git_get_project_ctags {2317my$path=shift;2318my$ctags= {};23192320$git_dir="$projectroot/$path";2321opendir my$dh,"$git_dir/ctags"2322orreturn$ctags;2323foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2324open my$ct,'<',$_ornext;2325my$val= <$ct>;2326chomp$val;2327close$ct;2328my$ctag=$_;$ctag=~ s#.*/##;2329$ctags->{$ctag} =$val;2330}2331closedir$dh;2332$ctags;2333}23342335sub git_populate_project_tagcloud {2336my$ctags=shift;23372338# First, merge different-cased tags; tags vote on casing2339my%ctags_lc;2340foreach(keys%$ctags) {2341$ctags_lc{lc$_}->{count} +=$ctags->{$_};2342if(not$ctags_lc{lc$_}->{topcount}2343or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2344$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2345$ctags_lc{lc$_}->{topname} =$_;2346}2347}23482349my$cloud;2350if(eval{require HTML::TagCloud;1; }) {2351$cloud= HTML::TagCloud->new;2352foreach(sort keys%ctags_lc) {2353# Pad the title with spaces so that the cloud looks2354# less crammed.2355my$title=$ctags_lc{$_}->{topname};2356$title=~s/ / /g;2357$title=~s/^/ /g;2358$title=~s/$/ /g;2359$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2360}2361}else{2362$cloud= \%ctags_lc;2363}2364$cloud;2365}23662367sub git_show_project_tagcloud {2368my($cloud,$count) =@_;2369print STDERR ref($cloud)."..\n";2370if(ref$cloudeq'HTML::TagCloud') {2371return$cloud->html_and_css($count);2372}else{2373my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2374return'<p align="center">'.join(', ',map{2375"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2376}splice(@tags,0,$count)) .'</p>';2377}2378}23792380sub git_get_project_url_list {2381my$path=shift;23822383$git_dir="$projectroot/$path";2384open my$fd,'<',"$git_dir/cloneurl"2385orreturnwantarray?2386@{ config_to_multi(git_get_project_config('url')) } :2387 config_to_multi(git_get_project_config('url'));2388my@git_project_url_list=map{chomp;$_} <$fd>;2389close$fd;23902391returnwantarray?@git_project_url_list: \@git_project_url_list;2392}23932394sub git_get_projects_list {2395my($filter) =@_;2396my@list;23972398$filter||='';2399$filter=~s/\.git$//;24002401my$check_forks= gitweb_check_feature('forks');24022403if(-d $projects_list) {2404# search in directory2405my$dir=$projects_list. ($filter?"/$filter":'');2406# remove the trailing "/"2407$dir=~s!/+$!!;2408my$pfxlen=length("$dir");2409my$pfxdepth= ($dir=~tr!/!!);24102411 File::Find::find({2412 follow_fast =>1,# follow symbolic links2413 follow_skip =>2,# ignore duplicates2414 dangling_symlinks =>0,# ignore dangling symlinks, silently2415 wanted =>sub{2416# skip project-list toplevel, if we get it.2417return if(m!^[/.]$!);2418# only directories can be git repositories2419return unless(-d $_);2420# don't traverse too deep (Find is super slow on os x)2421if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2422$File::Find::prune =1;2423return;2424}24252426my$subdir=substr($File::Find::name,$pfxlen+1);2427# we check related file in $projectroot2428my$path= ($filter?"$filter/":'') .$subdir;2429if(check_export_ok("$projectroot/$path")) {2430push@list, { path =>$path};2431$File::Find::prune =1;2432}2433},2434},"$dir");24352436}elsif(-f $projects_list) {2437# read from file(url-encoded):2438# 'git%2Fgit.git Linus+Torvalds'2439# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2440# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2441my%paths;2442open my$fd,'<',$projects_listorreturn;2443 PROJECT:2444while(my$line= <$fd>) {2445chomp$line;2446my($path,$owner) =split' ',$line;2447$path= unescape($path);2448$owner= unescape($owner);2449if(!defined$path) {2450next;2451}2452if($filterne'') {2453# looking for forks;2454my$pfx=substr($path,0,length($filter));2455if($pfxne$filter) {2456next PROJECT;2457}2458my$sfx=substr($path,length($filter));2459if($sfx!~/^\/.*\.git$/) {2460next PROJECT;2461}2462}elsif($check_forks) {2463 PATH:2464foreachmy$filter(keys%paths) {2465# looking for forks;2466my$pfx=substr($path,0,length($filter));2467if($pfxne$filter) {2468next PATH;2469}2470my$sfx=substr($path,length($filter));2471if($sfx!~/^\/.*\.git$/) {2472next PATH;2473}2474# is a fork, don't include it in2475# the list2476next PROJECT;2477}2478}2479if(check_export_ok("$projectroot/$path")) {2480my$pr= {2481 path =>$path,2482 owner => to_utf8($owner),2483};2484push@list,$pr;2485(my$forks_path=$path) =~s/\.git$//;2486$paths{$forks_path}++;2487}2488}2489close$fd;2490}2491return@list;2492}24932494our$gitweb_project_owner=undef;2495sub git_get_project_list_from_file {24962497return if(defined$gitweb_project_owner);24982499$gitweb_project_owner= {};2500# read from file (url-encoded):2501# 'git%2Fgit.git Linus+Torvalds'2502# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2503# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2504if(-f $projects_list) {2505open(my$fd,'<',$projects_list);2506while(my$line= <$fd>) {2507chomp$line;2508my($pr,$ow) =split' ',$line;2509$pr= unescape($pr);2510$ow= unescape($ow);2511$gitweb_project_owner->{$pr} = to_utf8($ow);2512}2513close$fd;2514}2515}25162517sub git_get_project_owner {2518my$project=shift;2519my$owner;25202521returnundefunless$project;2522$git_dir="$projectroot/$project";25232524if(!defined$gitweb_project_owner) {2525 git_get_project_list_from_file();2526}25272528if(exists$gitweb_project_owner->{$project}) {2529$owner=$gitweb_project_owner->{$project};2530}2531if(!defined$owner){2532$owner= git_get_project_config('owner');2533}2534if(!defined$owner) {2535$owner= get_file_owner("$git_dir");2536}25372538return$owner;2539}25402541sub git_get_last_activity {2542my($path) =@_;2543my$fd;25442545$git_dir="$projectroot/$path";2546open($fd,"-|", git_cmd(),'for-each-ref',2547'--format=%(committer)',2548'--sort=-committerdate',2549'--count=1',2550'refs/heads')orreturn;2551my$most_recent= <$fd>;2552close$fdorreturn;2553if(defined$most_recent&&2554$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2555my$timestamp=$1;2556my$age=time-$timestamp;2557return($age, age_string($age));2558}2559return(undef,undef);2560}25612562sub git_get_references {2563my$type=shift||"";2564my%refs;2565# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112566# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2567open my$fd,"-|", git_cmd(),"show-ref","--dereference",2568($type? ("--","refs/$type") : ())# use -- <pattern> if $type2569orreturn;25702571while(my$line= <$fd>) {2572chomp$line;2573if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2574if(defined$refs{$1}) {2575push@{$refs{$1}},$2;2576}else{2577$refs{$1} = [$2];2578}2579}2580}2581close$fdorreturn;2582return \%refs;2583}25842585sub git_get_rev_name_tags {2586my$hash=shift||returnundef;25872588open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2589orreturn;2590my$name_rev= <$fd>;2591close$fd;25922593if($name_rev=~ m|^$hash tags/(.*)$|) {2594return$1;2595}else{2596# catches also '$hash undefined' output2597returnundef;2598}2599}26002601## ----------------------------------------------------------------------2602## parse to hash functions26032604sub parse_date {2605my$epoch=shift;2606my$tz=shift||"-0000";26072608my%date;2609my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2610my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2611my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2612$date{'hour'} =$hour;2613$date{'minute'} =$min;2614$date{'mday'} =$mday;2615$date{'day'} =$days[$wday];2616$date{'month'} =$months[$mon];2617$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2618$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2619$date{'mday-time'} =sprintf"%d%s%02d:%02d",2620$mday,$months[$mon],$hour,$min;2621$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",26221900+$year,1+$mon,$mday,$hour,$min,$sec;26232624$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2625my$local=$epoch+ ((int$1+ ($2/60)) *3600);2626($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2627$date{'hour_local'} =$hour;2628$date{'minute_local'} =$min;2629$date{'tz_local'} =$tz;2630$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",26311900+$year,$mon+1,$mday,2632$hour,$min,$sec,$tz);2633return%date;2634}26352636sub parse_tag {2637my$tag_id=shift;2638my%tag;2639my@comment;26402641open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2642$tag{'id'} =$tag_id;2643while(my$line= <$fd>) {2644chomp$line;2645if($line=~m/^object ([0-9a-fA-F]{40})$/) {2646$tag{'object'} =$1;2647}elsif($line=~m/^type (.+)$/) {2648$tag{'type'} =$1;2649}elsif($line=~m/^tag (.+)$/) {2650$tag{'name'} =$1;2651}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2652$tag{'author'} =$1;2653$tag{'author_epoch'} =$2;2654$tag{'author_tz'} =$3;2655if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2656$tag{'author_name'} =$1;2657$tag{'author_email'} =$2;2658}else{2659$tag{'author_name'} =$tag{'author'};2660}2661}elsif($line=~m/--BEGIN/) {2662push@comment,$line;2663last;2664}elsif($lineeq"") {2665last;2666}2667}2668push@comment, <$fd>;2669$tag{'comment'} = \@comment;2670close$fdorreturn;2671if(!defined$tag{'name'}) {2672return2673};2674return%tag2675}26762677sub parse_commit_text {2678my($commit_text,$withparents) =@_;2679my@commit_lines=split'\n',$commit_text;2680my%co;26812682pop@commit_lines;# Remove '\0'26832684if(!@commit_lines) {2685return;2686}26872688my$header=shift@commit_lines;2689if($header!~m/^[0-9a-fA-F]{40}/) {2690return;2691}2692($co{'id'},my@parents) =split' ',$header;2693while(my$line=shift@commit_lines) {2694last if$lineeq"\n";2695if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2696$co{'tree'} =$1;2697}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2698push@parents,$1;2699}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2700$co{'author'} = to_utf8($1);2701$co{'author_epoch'} =$2;2702$co{'author_tz'} =$3;2703if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2704$co{'author_name'} =$1;2705$co{'author_email'} =$2;2706}else{2707$co{'author_name'} =$co{'author'};2708}2709}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2710$co{'committer'} = to_utf8($1);2711$co{'committer_epoch'} =$2;2712$co{'committer_tz'} =$3;2713if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2714$co{'committer_name'} =$1;2715$co{'committer_email'} =$2;2716}else{2717$co{'committer_name'} =$co{'committer'};2718}2719}2720}2721if(!defined$co{'tree'}) {2722return;2723};2724$co{'parents'} = \@parents;2725$co{'parent'} =$parents[0];27262727foreachmy$title(@commit_lines) {2728$title=~s/^ //;2729if($titlene"") {2730$co{'title'} = chop_str($title,80,5);2731# remove leading stuff of merges to make the interesting part visible2732if(length($title) >50) {2733$title=~s/^Automatic //;2734$title=~s/^merge (of|with) /Merge ... /i;2735if(length($title) >50) {2736$title=~s/(http|rsync):\/\///;2737}2738if(length($title) >50) {2739$title=~s/(master|www|rsync)\.//;2740}2741if(length($title) >50) {2742$title=~s/kernel.org:?//;2743}2744if(length($title) >50) {2745$title=~s/\/pub\/scm//;2746}2747}2748$co{'title_short'} = chop_str($title,50,5);2749last;2750}2751}2752if(!defined$co{'title'} ||$co{'title'}eq"") {2753$co{'title'} =$co{'title_short'} ='(no commit message)';2754}2755# remove added spaces2756foreachmy$line(@commit_lines) {2757$line=~s/^ //;2758}2759$co{'comment'} = \@commit_lines;27602761my$age=time-$co{'committer_epoch'};2762$co{'age'} =$age;2763$co{'age_string'} = age_string($age);2764my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2765if($age>60*60*24*7*2) {2766$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2767$co{'age_string_age'} =$co{'age_string'};2768}else{2769$co{'age_string_date'} =$co{'age_string'};2770$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2771}2772return%co;2773}27742775sub parse_commit {2776my($commit_id) =@_;2777my%co;27782779local$/="\0";27802781open my$fd,"-|", git_cmd(),"rev-list",2782"--parents",2783"--header",2784"--max-count=1",2785$commit_id,2786"--",2787or die_error(500,"Open git-rev-list failed");2788%co= parse_commit_text(<$fd>,1);2789close$fd;27902791return%co;2792}27932794sub parse_commits {2795my($commit_id,$maxcount,$skip,$filename,@args) =@_;2796my@cos;27972798$maxcount||=1;2799$skip||=0;28002801local$/="\0";28022803open my$fd,"-|", git_cmd(),"rev-list",2804"--header",2805@args,2806("--max-count=".$maxcount),2807("--skip=".$skip),2808@extra_options,2809$commit_id,2810"--",2811($filename? ($filename) : ())2812or die_error(500,"Open git-rev-list failed");2813while(my$line= <$fd>) {2814my%co= parse_commit_text($line);2815push@cos, \%co;2816}2817close$fd;28182819returnwantarray?@cos: \@cos;2820}28212822# parse line of git-diff-tree "raw" output2823sub parse_difftree_raw_line {2824my$line=shift;2825my%res;28262827# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2828# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2829if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2830$res{'from_mode'} =$1;2831$res{'to_mode'} =$2;2832$res{'from_id'} =$3;2833$res{'to_id'} =$4;2834$res{'status'} =$5;2835$res{'similarity'} =$6;2836if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2837($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2838}else{2839$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2840}2841}2842# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2843# combined diff (for merge commit)2844elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2845$res{'nparents'} =length($1);2846$res{'from_mode'} = [split(' ',$2) ];2847$res{'to_mode'} =pop@{$res{'from_mode'}};2848$res{'from_id'} = [split(' ',$3) ];2849$res{'to_id'} =pop@{$res{'from_id'}};2850$res{'status'} = [split('',$4) ];2851$res{'to_file'} = unquote($5);2852}2853# 'c512b523472485aef4fff9e57b229d9d243c967f'2854elsif($line=~m/^([0-9a-fA-F]{40})$/) {2855$res{'commit'} =$1;2856}28572858returnwantarray?%res: \%res;2859}28602861# wrapper: return parsed line of git-diff-tree "raw" output2862# (the argument might be raw line, or parsed info)2863sub parsed_difftree_line {2864my$line_or_ref=shift;28652866if(ref($line_or_ref)eq"HASH") {2867# pre-parsed (or generated by hand)2868return$line_or_ref;2869}else{2870return parse_difftree_raw_line($line_or_ref);2871}2872}28732874# parse line of git-ls-tree output2875sub parse_ls_tree_line {2876my$line=shift;2877my%opts=@_;2878my%res;28792880if($opts{'-l'}) {2881#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'2882$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;28832884$res{'mode'} =$1;2885$res{'type'} =$2;2886$res{'hash'} =$3;2887$res{'size'} =$4;2888if($opts{'-z'}) {2889$res{'name'} =$5;2890}else{2891$res{'name'} = unquote($5);2892}2893}else{2894#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2895$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;28962897$res{'mode'} =$1;2898$res{'type'} =$2;2899$res{'hash'} =$3;2900if($opts{'-z'}) {2901$res{'name'} =$4;2902}else{2903$res{'name'} = unquote($4);2904}2905}29062907returnwantarray?%res: \%res;2908}29092910# generates _two_ hashes, references to which are passed as 2 and 3 argument2911sub parse_from_to_diffinfo {2912my($diffinfo,$from,$to,@parents) =@_;29132914if($diffinfo->{'nparents'}) {2915# combined diff2916$from->{'file'} = [];2917$from->{'href'} = [];2918 fill_from_file_info($diffinfo,@parents)2919unlessexists$diffinfo->{'from_file'};2920for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2921$from->{'file'}[$i] =2922defined$diffinfo->{'from_file'}[$i] ?2923$diffinfo->{'from_file'}[$i] :2924$diffinfo->{'to_file'};2925if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2926$from->{'href'}[$i] = href(action=>"blob",2927 hash_base=>$parents[$i],2928 hash=>$diffinfo->{'from_id'}[$i],2929 file_name=>$from->{'file'}[$i]);2930}else{2931$from->{'href'}[$i] =undef;2932}2933}2934}else{2935# ordinary (not combined) diff2936$from->{'file'} =$diffinfo->{'from_file'};2937if($diffinfo->{'status'}ne"A") {# not new (added) file2938$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2939 hash=>$diffinfo->{'from_id'},2940 file_name=>$from->{'file'});2941}else{2942delete$from->{'href'};2943}2944}29452946$to->{'file'} =$diffinfo->{'to_file'};2947if(!is_deleted($diffinfo)) {# file exists in result2948$to->{'href'} = href(action=>"blob", hash_base=>$hash,2949 hash=>$diffinfo->{'to_id'},2950 file_name=>$to->{'file'});2951}else{2952delete$to->{'href'};2953}2954}29552956## ......................................................................2957## parse to array of hashes functions29582959sub git_get_heads_list {2960my$limit=shift;2961my@headslist;29622963open my$fd,'-|', git_cmd(),'for-each-ref',2964($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2965'--format=%(objectname) %(refname) %(subject)%00%(committer)',2966'refs/heads'2967orreturn;2968while(my$line= <$fd>) {2969my%ref_item;29702971chomp$line;2972my($refinfo,$committerinfo) =split(/\0/,$line);2973my($hash,$name,$title) =split(' ',$refinfo,3);2974my($committer,$epoch,$tz) =2975($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2976$ref_item{'fullname'} =$name;2977$name=~s!^refs/heads/!!;29782979$ref_item{'name'} =$name;2980$ref_item{'id'} =$hash;2981$ref_item{'title'} =$title||'(no commit message)';2982$ref_item{'epoch'} =$epoch;2983if($epoch) {2984$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2985}else{2986$ref_item{'age'} ="unknown";2987}29882989push@headslist, \%ref_item;2990}2991close$fd;29922993returnwantarray?@headslist: \@headslist;2994}29952996sub git_get_tags_list {2997my$limit=shift;2998my@tagslist;29993000open my$fd,'-|', git_cmd(),'for-each-ref',3001($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3002'--format=%(objectname) %(objecttype) %(refname) '.3003'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3004'refs/tags'3005orreturn;3006while(my$line= <$fd>) {3007my%ref_item;30083009chomp$line;3010my($refinfo,$creatorinfo) =split(/\0/,$line);3011my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3012my($creator,$epoch,$tz) =3013($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3014$ref_item{'fullname'} =$name;3015$name=~s!^refs/tags/!!;30163017$ref_item{'type'} =$type;3018$ref_item{'id'} =$id;3019$ref_item{'name'} =$name;3020if($typeeq"tag") {3021$ref_item{'subject'} =$title;3022$ref_item{'reftype'} =$reftype;3023$ref_item{'refid'} =$refid;3024}else{3025$ref_item{'reftype'} =$type;3026$ref_item{'refid'} =$id;3027}30283029if($typeeq"tag"||$typeeq"commit") {3030$ref_item{'epoch'} =$epoch;3031if($epoch) {3032$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3033}else{3034$ref_item{'age'} ="unknown";3035}3036}30373038push@tagslist, \%ref_item;3039}3040close$fd;30413042returnwantarray?@tagslist: \@tagslist;3043}30443045## ----------------------------------------------------------------------3046## filesystem-related functions30473048sub get_file_owner {3049my$path=shift;30503051my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3052my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3053if(!defined$gcos) {3054returnundef;3055}3056my$owner=$gcos;3057$owner=~s/[,;].*$//;3058return to_utf8($owner);3059}30603061# assume that file exists3062sub insert_file {3063my$filename=shift;30643065open my$fd,'<',$filename;3066print map{ to_utf8($_) } <$fd>;3067close$fd;3068}30693070## ......................................................................3071## mimetype related functions30723073sub mimetype_guess_file {3074my$filename=shift;3075my$mimemap=shift;3076-r $mimemaporreturnundef;30773078my%mimemap;3079open(my$mh,'<',$mimemap)orreturnundef;3080while(<$mh>) {3081next ifm/^#/;# skip comments3082my($mimetype,$exts) =split(/\t+/);3083if(defined$exts) {3084my@exts=split(/\s+/,$exts);3085foreachmy$ext(@exts) {3086$mimemap{$ext} =$mimetype;3087}3088}3089}3090close($mh);30913092$filename=~/\.([^.]*)$/;3093return$mimemap{$1};3094}30953096sub mimetype_guess {3097my$filename=shift;3098my$mime;3099$filename=~/\./orreturnundef;31003101if($mimetypes_file) {3102my$file=$mimetypes_file;3103if($file!~m!^/!) {# if it is relative path3104# it is relative to project3105$file="$projectroot/$project/$file";3106}3107$mime= mimetype_guess_file($filename,$file);3108}3109$mime||= mimetype_guess_file($filename,'/etc/mime.types');3110return$mime;3111}31123113sub blob_mimetype {3114my$fd=shift;3115my$filename=shift;31163117if($filename) {3118my$mime= mimetype_guess($filename);3119$mimeandreturn$mime;3120}31213122# just in case3123return$default_blob_plain_mimetypeunless$fd;31243125if(-T $fd) {3126return'text/plain';3127}elsif(!$filename) {3128return'application/octet-stream';3129}elsif($filename=~m/\.png$/i) {3130return'image/png';3131}elsif($filename=~m/\.gif$/i) {3132return'image/gif';3133}elsif($filename=~m/\.jpe?g$/i) {3134return'image/jpeg';3135}else{3136return'application/octet-stream';3137}3138}31393140sub blob_contenttype {3141my($fd,$file_name,$type) =@_;31423143$type||= blob_mimetype($fd,$file_name);3144if($typeeq'text/plain'&&defined$default_text_plain_charset) {3145$type.="; charset=$default_text_plain_charset";3146}31473148return$type;3149}31503151## ======================================================================3152## functions printing HTML: header, footer, error page31533154sub git_header_html {3155my$status=shift||"200 OK";3156my$expires=shift;31573158my$title="$site_name";3159if(defined$project) {3160$title.=" - ". to_utf8($project);3161if(defined$action) {3162$title.="/$action";3163if(defined$file_name) {3164$title.=" - ". esc_path($file_name);3165if($actioneq"tree"&&$file_name!~ m|/$|) {3166$title.="/";3167}3168}3169}3170}3171my$content_type;3172# require explicit support from the UA if we are to send the page as3173# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3174# we have to do this because MSIE sometimes globs '*/*', pretending to3175# support xhtml+xml but choking when it gets what it asked for.3176if(defined$cgi->http('HTTP_ACCEPT') &&3177$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3178$cgi->Accept('application/xhtml+xml') !=0) {3179$content_type='application/xhtml+xml';3180}else{3181$content_type='text/html';3182}3183print$cgi->header(-type=>$content_type, -charset =>'utf-8',3184-status=>$status, -expires =>$expires);3185my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3186print<<EOF;3187<?xml version="1.0" encoding="utf-8"?>3188<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3189<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3190<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3191<!-- git core binaries version$git_version-->3192<head>3193<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3194<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3195<meta name="robots" content="index, nofollow"/>3196<title>$title</title>3197EOF3198# the stylesheet, favicon etc urls won't work correctly with path_info3199# unless we set the appropriate base URL3200if($ENV{'PATH_INFO'}) {3201print"<base href=\"".esc_url($base_url)."\"/>\n";3202}3203# print out each stylesheet that exist, providing backwards capability3204# for those people who defined $stylesheet in a config file3205if(defined$stylesheet) {3206print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3207}else{3208foreachmy$stylesheet(@stylesheets) {3209next unless$stylesheet;3210print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3211}3212}3213if(defined$project) {3214my%href_params= get_feed_info();3215if(!exists$href_params{'-title'}) {3216$href_params{'-title'} ='log';3217}32183219foreachmy$formatqw(RSS Atom){3220my$type=lc($format);3221my%link_attr= (3222'-rel'=>'alternate',3223'-title'=>"$project-$href_params{'-title'} -$formatfeed",3224'-type'=>"application/$type+xml"3225);32263227$href_params{'action'} =$type;3228$link_attr{'-href'} = href(%href_params);3229print"<link ".3230"rel=\"$link_attr{'-rel'}\"".3231"title=\"$link_attr{'-title'}\"".3232"href=\"$link_attr{'-href'}\"".3233"type=\"$link_attr{'-type'}\"".3234"/>\n";32353236$href_params{'extra_options'} ='--no-merges';3237$link_attr{'-href'} = href(%href_params);3238$link_attr{'-title'} .=' (no merges)';3239print"<link ".3240"rel=\"$link_attr{'-rel'}\"".3241"title=\"$link_attr{'-title'}\"".3242"href=\"$link_attr{'-href'}\"".3243"type=\"$link_attr{'-type'}\"".3244"/>\n";3245}32463247}else{3248printf('<link rel="alternate" title="%sprojects list" '.3249'href="%s" type="text/plain; charset=utf-8" />'."\n",3250$site_name, href(project=>undef, action=>"project_index"));3251printf('<link rel="alternate" title="%sprojects feeds" '.3252'href="%s" type="text/x-opml" />'."\n",3253$site_name, href(project=>undef, action=>"opml"));3254}3255if(defined$favicon) {3256printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3257}32583259print"</head>\n".3260"<body>\n";32613262if(defined$site_header&& -f $site_header) {3263 insert_file($site_header);3264}32653266print"<div class=\"page_header\">\n".3267$cgi->a({-href => esc_url($logo_url),3268-title =>$logo_label},3269qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3270print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3271if(defined$project) {3272print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3273if(defined$action) {3274print" /$action";3275}3276print"\n";3277}3278print"</div>\n";32793280my$have_search= gitweb_check_feature('search');3281if(defined$project&&$have_search) {3282if(!defined$searchtext) {3283$searchtext="";3284}3285my$search_hash;3286if(defined$hash_base) {3287$search_hash=$hash_base;3288}elsif(defined$hash) {3289$search_hash=$hash;3290}else{3291$search_hash="HEAD";3292}3293my$action=$my_uri;3294my$use_pathinfo= gitweb_check_feature('pathinfo');3295if($use_pathinfo) {3296$action.="/".esc_url($project);3297}3298print$cgi->startform(-method=>"get", -action =>$action) .3299"<div class=\"search\">\n".3300(!$use_pathinfo&&3301$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3302$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3303$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3304$cgi->popup_menu(-name =>'st', -default=>'commit',3305-values=> ['commit','grep','author','committer','pickaxe']) .3306$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3307" search:\n",3308$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3309"<span title=\"Extended regular expression\">".3310$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3311-checked =>$search_use_regexp) .3312"</span>".3313"</div>".3314$cgi->end_form() ."\n";3315}3316}33173318sub git_footer_html {3319my$feed_class='rss_logo';33203321print"<div class=\"page_footer\">\n";3322if(defined$project) {3323my$descr= git_get_project_description($project);3324if(defined$descr) {3325print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3326}33273328my%href_params= get_feed_info();3329if(!%href_params) {3330$feed_class.=' generic';3331}3332$href_params{'-title'} ||='log';33333334foreachmy$formatqw(RSS Atom){3335$href_params{'action'} =lc($format);3336print$cgi->a({-href => href(%href_params),3337-title =>"$href_params{'-title'}$formatfeed",3338-class=>$feed_class},$format)."\n";3339}33403341}else{3342print$cgi->a({-href => href(project=>undef, action=>"opml"),3343-class=>$feed_class},"OPML") ." ";3344print$cgi->a({-href => href(project=>undef, action=>"project_index"),3345-class=>$feed_class},"TXT") ."\n";3346}3347print"</div>\n";# class="page_footer"33483349if(defined$t0&& gitweb_check_feature('timed')) {3350print"<div id=\"generating_info\">\n";3351print'This page took '.3352'<span id="generating_time" class="time_span">'.3353 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3354' seconds </span>'.3355' and '.3356'<span id="generating_cmd">'.3357$number_of_git_cmds.3358'</span> git commands '.3359" to generate.\n";3360print"</div>\n";# class="page_footer"3361}33623363if(defined$site_footer&& -f $site_footer) {3364 insert_file($site_footer);3365}33663367print qq!<script type="text/javascript" src="$javascript"></script>\n!;3368if(defined$action&&3369$actioneq'blame_incremental') {3370print qq!<script type="text/javascript">\n!.3371 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3372 qq!"!. href() .qq!");\n!.3373 qq!</script>\n!;3374}elsif(gitweb_check_feature('javascript-actions')) {3375print qq!<script type="text/javascript">\n!.3376 qq!window.onload = fixLinks;\n!.3377 qq!</script>\n!;3378}33793380print"</body>\n".3381"</html>";3382}33833384# die_error(<http_status_code>, <error_message>)3385# Example: die_error(404, 'Hash not found')3386# By convention, use the following status codes (as defined in RFC 2616):3387# 400: Invalid or missing CGI parameters, or3388# requested object exists but has wrong type.3389# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3390# this server or project.3391# 404: Requested object/revision/project doesn't exist.3392# 500: The server isn't configured properly, or3393# an internal error occurred (e.g. failed assertions caused by bugs), or3394# an unknown error occurred (e.g. the git binary died unexpectedly).3395# 503: The server is currently unavailable (because it is overloaded,3396# or down for maintenance). Generally, this is a temporary state.3397sub die_error {3398my$status=shift||500;3399my$error=shift||"Internal server error";3400my$extra=shift;34013402my%http_responses= (3403400=>'400 Bad Request',3404403=>'403 Forbidden',3405404=>'404 Not Found',3406500=>'500 Internal Server Error',3407503=>'503 Service Unavailable',3408);3409 git_header_html($http_responses{$status});3410print<<EOF;3411<div class="page_body">3412<br /><br />3413$status-$error3414<br />3415EOF3416if(defined$extra) {3417print"<hr />\n".3418"$extra\n";3419}3420print"</div>\n";34213422 git_footer_html();3423exit;3424}34253426## ----------------------------------------------------------------------3427## functions printing or outputting HTML: navigation34283429sub git_print_page_nav {3430my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3431$extra=''if!defined$extra;# pager or formats34323433my@navs=qw(summary shortlog log commit commitdiff tree);3434if($suppress) {3435@navs=grep{$_ne$suppress}@navs;3436}34373438my%arg=map{$_=> {action=>$_} }@navs;3439if(defined$head) {3440for(qw(commit commitdiff)) {3441$arg{$_}{'hash'} =$head;3442}3443if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3444for(qw(shortlog log)) {3445$arg{$_}{'hash'} =$head;3446}3447}3448}34493450$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3451$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;34523453my@actions= gitweb_get_feature('actions');3454my%repl= (3455'%'=>'%',3456'n'=>$project,# project name3457'f'=>$git_dir,# project path within filesystem3458'h'=>$treehead||'',# current hash ('h' parameter)3459'b'=>$treebase||'',# hash base ('hb' parameter)3460);3461while(@actions) {3462my($label,$link,$pos) =splice(@actions,0,3);3463# insert3464@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3465# munch munch3466$link=~s/%([%nfhb])/$repl{$1}/g;3467$arg{$label}{'_href'} =$link;3468}34693470print"<div class=\"page_nav\">\n".3471(join" | ",3472map{$_eq$current?3473$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3474}@navs);3475print"<br/>\n$extra<br/>\n".3476"</div>\n";3477}34783479sub format_paging_nav {3480my($action,$page,$has_next_link) =@_;3481my$paging_nav;348234833484if($page>0) {3485$paging_nav.=3486$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3487" ⋅ ".3488$cgi->a({-href => href(-replay=>1, page=>$page-1),3489-accesskey =>"p", -title =>"Alt-p"},"prev");3490}else{3491$paging_nav.="first ⋅ prev";3492}34933494if($has_next_link) {3495$paging_nav.=" ⋅ ".3496$cgi->a({-href => href(-replay=>1, page=>$page+1),3497-accesskey =>"n", -title =>"Alt-n"},"next");3498}else{3499$paging_nav.=" ⋅ next";3500}35013502return$paging_nav;3503}35043505## ......................................................................3506## functions printing or outputting HTML: div35073508sub git_print_header_div {3509my($action,$title,$hash,$hash_base) =@_;3510my%args= ();35113512$args{'action'} =$action;3513$args{'hash'} =$hashif$hash;3514$args{'hash_base'} =$hash_baseif$hash_base;35153516print"<div class=\"header\">\n".3517$cgi->a({-href => href(%args), -class=>"title"},3518$title?$title:$action) .3519"\n</div>\n";3520}35213522sub print_local_time {3523print format_local_time(@_);3524}35253526sub format_local_time {3527my$localtime='';3528my%date=@_;3529if($date{'hour_local'} <6) {3530$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3531$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3532}else{3533$localtime.=sprintf(" (%02d:%02d%s)",3534$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3535}35363537return$localtime;3538}35393540# Outputs the author name and date in long form3541sub git_print_authorship {3542my$co=shift;3543my%opts=@_;3544my$tag=$opts{-tag} ||'div';3545my$author=$co->{'author_name'};35463547my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3548print"<$tagclass=\"author_date\">".3549 format_search_author($author,"author", esc_html($author)) .3550" [$ad{'rfc2822'}";3551 print_local_time(%ad)if($opts{-localtime});3552print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3553."</$tag>\n";3554}35553556# Outputs table rows containing the full author or committer information,3557# in the format expected for 'commit' view (& similia).3558# Parameters are a commit hash reference, followed by the list of people3559# to output information for. If the list is empty it defalts to both3560# author and committer.3561sub git_print_authorship_rows {3562my$co=shift;3563# too bad we can't use @people = @_ || ('author', 'committer')3564my@people=@_;3565@people= ('author','committer')unless@people;3566foreachmy$who(@people) {3567my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3568print"<tr><td>$who</td><td>".3569 format_search_author($co->{"${who}_name"},$who,3570 esc_html($co->{"${who}_name"})) ." ".3571 format_search_author($co->{"${who}_email"},$who,3572 esc_html("<".$co->{"${who}_email"} .">")) .3573"</td><td rowspan=\"2\">".3574 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3575"</td></tr>\n".3576"<tr>".3577"<td></td><td>$wd{'rfc2822'}";3578 print_local_time(%wd);3579print"</td>".3580"</tr>\n";3581}3582}35833584sub git_print_page_path {3585my$name=shift;3586my$type=shift;3587my$hb=shift;358835893590print"<div class=\"page_path\">";3591print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3592-title =>'tree root'}, to_utf8("[$project]"));3593print" / ";3594if(defined$name) {3595my@dirname=split'/',$name;3596my$basename=pop@dirname;3597my$fullname='';35983599foreachmy$dir(@dirname) {3600$fullname.= ($fullname?'/':'') .$dir;3601print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3602 hash_base=>$hb),3603-title =>$fullname}, esc_path($dir));3604print" / ";3605}3606if(defined$type&&$typeeq'blob') {3607print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3608 hash_base=>$hb),3609-title =>$name}, esc_path($basename));3610}elsif(defined$type&&$typeeq'tree') {3611print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3612 hash_base=>$hb),3613-title =>$name}, esc_path($basename));3614print" / ";3615}else{3616print esc_path($basename);3617}3618}3619print"<br/></div>\n";3620}36213622sub git_print_log {3623my$log=shift;3624my%opts=@_;36253626if($opts{'-remove_title'}) {3627# remove title, i.e. first line of log3628shift@$log;3629}3630# remove leading empty lines3631while(defined$log->[0] &&$log->[0]eq"") {3632shift@$log;3633}36343635# print log3636my$signoff=0;3637my$empty=0;3638foreachmy$line(@$log) {3639if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3640$signoff=1;3641$empty=0;3642if(!$opts{'-remove_signoff'}) {3643print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3644next;3645}else{3646# remove signoff lines3647next;3648}3649}else{3650$signoff=0;3651}36523653# print only one empty line3654# do not print empty line after signoff3655if($lineeq"") {3656next if($empty||$signoff);3657$empty=1;3658}else{3659$empty=0;3660}36613662print format_log_line_html($line) ."<br/>\n";3663}36643665if($opts{'-final_empty_line'}) {3666# end with single empty line3667print"<br/>\n"unless$empty;3668}3669}36703671# return link target (what link points to)3672sub git_get_link_target {3673my$hash=shift;3674my$link_target;36753676# read link3677open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3678orreturn;3679{3680local$/=undef;3681$link_target= <$fd>;3682}3683close$fd3684orreturn;36853686return$link_target;3687}36883689# given link target, and the directory (basedir) the link is in,3690# return target of link relative to top directory (top tree);3691# return undef if it is not possible (including absolute links).3692sub normalize_link_target {3693my($link_target,$basedir) =@_;36943695# absolute symlinks (beginning with '/') cannot be normalized3696return if(substr($link_target,0,1)eq'/');36973698# normalize link target to path from top (root) tree (dir)3699my$path;3700if($basedir) {3701$path=$basedir.'/'.$link_target;3702}else{3703# we are in top (root) tree (dir)3704$path=$link_target;3705}37063707# remove //, /./, and /../3708my@path_parts;3709foreachmy$part(split('/',$path)) {3710# discard '.' and ''3711next if(!$part||$parteq'.');3712# handle '..'3713if($parteq'..') {3714if(@path_parts) {3715pop@path_parts;3716}else{3717# link leads outside repository (outside top dir)3718return;3719}3720}else{3721push@path_parts,$part;3722}3723}3724$path=join('/',@path_parts);37253726return$path;3727}37283729# print tree entry (row of git_tree), but without encompassing <tr> element3730sub git_print_tree_entry {3731my($t,$basedir,$hash_base,$have_blame) =@_;37323733my%base_key= ();3734$base_key{'hash_base'} =$hash_baseifdefined$hash_base;37353736# The format of a table row is: mode list link. Where mode is3737# the mode of the entry, list is the name of the entry, an href,3738# and link is the action links of the entry.37393740print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3741if(exists$t->{'size'}) {3742print"<td class=\"size\">$t->{'size'}</td>\n";3743}3744if($t->{'type'}eq"blob") {3745print"<td class=\"list\">".3746$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3747 file_name=>"$basedir$t->{'name'}",%base_key),3748-class=>"list"}, esc_path($t->{'name'}));3749if(S_ISLNK(oct$t->{'mode'})) {3750my$link_target= git_get_link_target($t->{'hash'});3751if($link_target) {3752my$norm_target= normalize_link_target($link_target,$basedir);3753if(defined$norm_target) {3754print" -> ".3755$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3756 file_name=>$norm_target),3757-title =>$norm_target}, esc_path($link_target));3758}else{3759print" -> ". esc_path($link_target);3760}3761}3762}3763print"</td>\n";3764print"<td class=\"link\">";3765print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3766 file_name=>"$basedir$t->{'name'}",%base_key)},3767"blob");3768if($have_blame) {3769print" | ".3770$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3771 file_name=>"$basedir$t->{'name'}",%base_key)},3772"blame");3773}3774if(defined$hash_base) {3775print" | ".3776$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3777 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3778"history");3779}3780print" | ".3781$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3782 file_name=>"$basedir$t->{'name'}")},3783"raw");3784print"</td>\n";37853786}elsif($t->{'type'}eq"tree") {3787print"<td class=\"list\">";3788print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3789 file_name=>"$basedir$t->{'name'}",3790%base_key)},3791 esc_path($t->{'name'}));3792print"</td>\n";3793print"<td class=\"link\">";3794print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3795 file_name=>"$basedir$t->{'name'}",3796%base_key)},3797"tree");3798if(defined$hash_base) {3799print" | ".3800$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3801 file_name=>"$basedir$t->{'name'}")},3802"history");3803}3804print"</td>\n";3805}else{3806# unknown object: we can only present history for it3807# (this includes 'commit' object, i.e. submodule support)3808print"<td class=\"list\">".3809 esc_path($t->{'name'}) .3810"</td>\n";3811print"<td class=\"link\">";3812if(defined$hash_base) {3813print$cgi->a({-href => href(action=>"history",3814 hash_base=>$hash_base,3815 file_name=>"$basedir$t->{'name'}")},3816"history");3817}3818print"</td>\n";3819}3820}38213822## ......................................................................3823## functions printing large fragments of HTML38243825# get pre-image filenames for merge (combined) diff3826sub fill_from_file_info {3827my($diff,@parents) =@_;38283829$diff->{'from_file'} = [ ];3830$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3831for(my$i=0;$i<$diff->{'nparents'};$i++) {3832if($diff->{'status'}[$i]eq'R'||3833$diff->{'status'}[$i]eq'C') {3834$diff->{'from_file'}[$i] =3835 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3836}3837}38383839return$diff;3840}38413842# is current raw difftree line of file deletion3843sub is_deleted {3844my$diffinfo=shift;38453846return$diffinfo->{'to_id'}eq('0' x 40);3847}38483849# does patch correspond to [previous] difftree raw line3850# $diffinfo - hashref of parsed raw diff format3851# $patchinfo - hashref of parsed patch diff format3852# (the same keys as in $diffinfo)3853sub is_patch_split {3854my($diffinfo,$patchinfo) =@_;38553856returndefined$diffinfo&&defined$patchinfo3857&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3858}385938603861sub git_difftree_body {3862my($difftree,$hash,@parents) =@_;3863my($parent) =$parents[0];3864my$have_blame= gitweb_check_feature('blame');3865print"<div class=\"list_head\">\n";3866if($#{$difftree} >10) {3867print(($#{$difftree} +1) ." files changed:\n");3868}3869print"</div>\n";38703871print"<table class=\"".3872(@parents>1?"combined ":"") .3873"diff_tree\">\n";38743875# header only for combined diff in 'commitdiff' view3876my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3877if($has_header) {3878# table header3879print"<thead><tr>\n".3880"<th></th><th></th>\n";# filename, patchN link3881for(my$i=0;$i<@parents;$i++) {3882my$par=$parents[$i];3883print"<th>".3884$cgi->a({-href => href(action=>"commitdiff",3885 hash=>$hash, hash_parent=>$par),3886-title =>'commitdiff to parent number '.3887($i+1) .': '.substr($par,0,7)},3888$i+1) .3889" </th>\n";3890}3891print"</tr></thead>\n<tbody>\n";3892}38933894my$alternate=1;3895my$patchno=0;3896foreachmy$line(@{$difftree}) {3897my$diff= parsed_difftree_line($line);38983899if($alternate) {3900print"<tr class=\"dark\">\n";3901}else{3902print"<tr class=\"light\">\n";3903}3904$alternate^=1;39053906if(exists$diff->{'nparents'}) {# combined diff39073908 fill_from_file_info($diff,@parents)3909unlessexists$diff->{'from_file'};39103911if(!is_deleted($diff)) {3912# file exists in the result (child) commit3913print"<td>".3914$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3915 file_name=>$diff->{'to_file'},3916 hash_base=>$hash),3917-class=>"list"}, esc_path($diff->{'to_file'})) .3918"</td>\n";3919}else{3920print"<td>".3921 esc_path($diff->{'to_file'}) .3922"</td>\n";3923}39243925if($actioneq'commitdiff') {3926# link to patch3927$patchno++;3928print"<td class=\"link\">".3929$cgi->a({-href =>"#patch$patchno"},"patch") .3930" | ".3931"</td>\n";3932}39333934my$has_history=0;3935my$not_deleted=0;3936for(my$i=0;$i<$diff->{'nparents'};$i++) {3937my$hash_parent=$parents[$i];3938my$from_hash=$diff->{'from_id'}[$i];3939my$from_path=$diff->{'from_file'}[$i];3940my$status=$diff->{'status'}[$i];39413942$has_history||= ($statusne'A');3943$not_deleted||= ($statusne'D');39443945if($statuseq'A') {3946print"<td class=\"link\"align=\"right\"> | </td>\n";3947}elsif($statuseq'D') {3948print"<td class=\"link\">".3949$cgi->a({-href => href(action=>"blob",3950 hash_base=>$hash,3951 hash=>$from_hash,3952 file_name=>$from_path)},3953"blob". ($i+1)) .3954" | </td>\n";3955}else{3956if($diff->{'to_id'}eq$from_hash) {3957print"<td class=\"link nochange\">";3958}else{3959print"<td class=\"link\">";3960}3961print$cgi->a({-href => href(action=>"blobdiff",3962 hash=>$diff->{'to_id'},3963 hash_parent=>$from_hash,3964 hash_base=>$hash,3965 hash_parent_base=>$hash_parent,3966 file_name=>$diff->{'to_file'},3967 file_parent=>$from_path)},3968"diff". ($i+1)) .3969" | </td>\n";3970}3971}39723973print"<td class=\"link\">";3974if($not_deleted) {3975print$cgi->a({-href => href(action=>"blob",3976 hash=>$diff->{'to_id'},3977 file_name=>$diff->{'to_file'},3978 hash_base=>$hash)},3979"blob");3980print" | "if($has_history);3981}3982if($has_history) {3983print$cgi->a({-href => href(action=>"history",3984 file_name=>$diff->{'to_file'},3985 hash_base=>$hash)},3986"history");3987}3988print"</td>\n";39893990print"</tr>\n";3991next;# instead of 'else' clause, to avoid extra indent3992}3993# else ordinary diff39943995my($to_mode_oct,$to_mode_str,$to_file_type);3996my($from_mode_oct,$from_mode_str,$from_file_type);3997if($diff->{'to_mode'}ne('0' x 6)) {3998$to_mode_oct=oct$diff->{'to_mode'};3999if(S_ISREG($to_mode_oct)) {# only for regular file4000$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4001}4002$to_file_type= file_type($diff->{'to_mode'});4003}4004if($diff->{'from_mode'}ne('0' x 6)) {4005$from_mode_oct=oct$diff->{'from_mode'};4006if(S_ISREG($to_mode_oct)) {# only for regular file4007$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4008}4009$from_file_type= file_type($diff->{'from_mode'});4010}40114012if($diff->{'status'}eq"A") {# created4013my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4014$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4015$mode_chng.="]</span>";4016print"<td>";4017print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4018 hash_base=>$hash, file_name=>$diff->{'file'}),4019-class=>"list"}, esc_path($diff->{'file'}));4020print"</td>\n";4021print"<td>$mode_chng</td>\n";4022print"<td class=\"link\">";4023if($actioneq'commitdiff') {4024# link to patch4025$patchno++;4026print$cgi->a({-href =>"#patch$patchno"},"patch");4027print" | ";4028}4029print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4030 hash_base=>$hash, file_name=>$diff->{'file'})},4031"blob");4032print"</td>\n";40334034}elsif($diff->{'status'}eq"D") {# deleted4035my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4036print"<td>";4037print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4038 hash_base=>$parent, file_name=>$diff->{'file'}),4039-class=>"list"}, esc_path($diff->{'file'}));4040print"</td>\n";4041print"<td>$mode_chng</td>\n";4042print"<td class=\"link\">";4043if($actioneq'commitdiff') {4044# link to patch4045$patchno++;4046print$cgi->a({-href =>"#patch$patchno"},"patch");4047print" | ";4048}4049print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4050 hash_base=>$parent, file_name=>$diff->{'file'})},4051"blob") ." | ";4052if($have_blame) {4053print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4054 file_name=>$diff->{'file'})},4055"blame") ." | ";4056}4057print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4058 file_name=>$diff->{'file'})},4059"history");4060print"</td>\n";40614062}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4063my$mode_chnge="";4064if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4065$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4066if($from_file_typene$to_file_type) {4067$mode_chnge.=" from$from_file_typeto$to_file_type";4068}4069if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4070if($from_mode_str&&$to_mode_str) {4071$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4072}elsif($to_mode_str) {4073$mode_chnge.=" mode:$to_mode_str";4074}4075}4076$mode_chnge.="]</span>\n";4077}4078print"<td>";4079print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4080 hash_base=>$hash, file_name=>$diff->{'file'}),4081-class=>"list"}, esc_path($diff->{'file'}));4082print"</td>\n";4083print"<td>$mode_chnge</td>\n";4084print"<td class=\"link\">";4085if($actioneq'commitdiff') {4086# link to patch4087$patchno++;4088print$cgi->a({-href =>"#patch$patchno"},"patch") .4089" | ";4090}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4091# "commit" view and modified file (not onlu mode changed)4092print$cgi->a({-href => href(action=>"blobdiff",4093 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4094 hash_base=>$hash, hash_parent_base=>$parent,4095 file_name=>$diff->{'file'})},4096"diff") .4097" | ";4098}4099print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4100 hash_base=>$hash, file_name=>$diff->{'file'})},4101"blob") ." | ";4102if($have_blame) {4103print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4104 file_name=>$diff->{'file'})},4105"blame") ." | ";4106}4107print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4108 file_name=>$diff->{'file'})},4109"history");4110print"</td>\n";41114112}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4113my%status_name= ('R'=>'moved','C'=>'copied');4114my$nstatus=$status_name{$diff->{'status'}};4115my$mode_chng="";4116if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4117# mode also for directories, so we cannot use $to_mode_str4118$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4119}4120print"<td>".4121$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4122 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4123-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4124"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4125$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4126 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4127-class=>"list"}, esc_path($diff->{'from_file'})) .4128" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4129"<td class=\"link\">";4130if($actioneq'commitdiff') {4131# link to patch4132$patchno++;4133print$cgi->a({-href =>"#patch$patchno"},"patch") .4134" | ";4135}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4136# "commit" view and modified file (not only pure rename or copy)4137print$cgi->a({-href => href(action=>"blobdiff",4138 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4139 hash_base=>$hash, hash_parent_base=>$parent,4140 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4141"diff") .4142" | ";4143}4144print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4145 hash_base=>$parent, file_name=>$diff->{'to_file'})},4146"blob") ." | ";4147if($have_blame) {4148print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4149 file_name=>$diff->{'to_file'})},4150"blame") ." | ";4151}4152print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4153 file_name=>$diff->{'to_file'})},4154"history");4155print"</td>\n";41564157}# we should not encounter Unmerged (U) or Unknown (X) status4158print"</tr>\n";4159}4160print"</tbody>"if$has_header;4161print"</table>\n";4162}41634164sub git_patchset_body {4165my($fd,$difftree,$hash,@hash_parents) =@_;4166my($hash_parent) =$hash_parents[0];41674168my$is_combined= (@hash_parents>1);4169my$patch_idx=0;4170my$patch_number=0;4171my$patch_line;4172my$diffinfo;4173my$to_name;4174my(%from,%to);41754176print"<div class=\"patchset\">\n";41774178# skip to first patch4179while($patch_line= <$fd>) {4180chomp$patch_line;41814182last if($patch_line=~m/^diff /);4183}41844185 PATCH:4186while($patch_line) {41874188# parse "git diff" header line4189if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4190# $1 is from_name, which we do not use4191$to_name= unquote($2);4192$to_name=~s!^b/!!;4193}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4194# $1 is 'cc' or 'combined', which we do not use4195$to_name= unquote($2);4196}else{4197$to_name=undef;4198}41994200# check if current patch belong to current raw line4201# and parse raw git-diff line if needed4202if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4203# this is continuation of a split patch4204print"<div class=\"patch cont\">\n";4205}else{4206# advance raw git-diff output if needed4207$patch_idx++ifdefined$diffinfo;42084209# read and prepare patch information4210$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42114212# compact combined diff output can have some patches skipped4213# find which patch (using pathname of result) we are at now;4214if($is_combined) {4215while($to_namene$diffinfo->{'to_file'}) {4216print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4217 format_diff_cc_simplified($diffinfo,@hash_parents) .4218"</div>\n";# class="patch"42194220$patch_idx++;4221$patch_number++;42224223last if$patch_idx>$#$difftree;4224$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4225}4226}42274228# modifies %from, %to hashes4229 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);42304231# this is first patch for raw difftree line with $patch_idx index4232# we index @$difftree array from 0, but number patches from 14233print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4234}42354236# git diff header4237#assert($patch_line =~ m/^diff /) if DEBUG;4238#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4239$patch_number++;4240# print "git diff" header4241print format_git_diff_header_line($patch_line,$diffinfo,4242 \%from, \%to);42434244# print extended diff header4245print"<div class=\"diff extended_header\">\n";4246 EXTENDED_HEADER:4247while($patch_line= <$fd>) {4248chomp$patch_line;42494250last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);42514252print format_extended_diff_header_line($patch_line,$diffinfo,4253 \%from, \%to);4254}4255print"</div>\n";# class="diff extended_header"42564257# from-file/to-file diff header4258if(!$patch_line) {4259print"</div>\n";# class="patch"4260last PATCH;4261}4262next PATCH if($patch_line=~m/^diff /);4263#assert($patch_line =~ m/^---/) if DEBUG;42644265my$last_patch_line=$patch_line;4266$patch_line= <$fd>;4267chomp$patch_line;4268#assert($patch_line =~ m/^\+\+\+/) if DEBUG;42694270print format_diff_from_to_header($last_patch_line,$patch_line,4271$diffinfo, \%from, \%to,4272@hash_parents);42734274# the patch itself4275 LINE:4276while($patch_line= <$fd>) {4277chomp$patch_line;42784279next PATCH if($patch_line=~m/^diff /);42804281print format_diff_line($patch_line, \%from, \%to);4282}42834284}continue{4285print"</div>\n";# class="patch"4286}42874288# for compact combined (--cc) format, with chunk and patch simpliciaction4289# patchset might be empty, but there might be unprocessed raw lines4290for(++$patch_idxif$patch_number>0;4291$patch_idx<@$difftree;4292++$patch_idx) {4293# read and prepare patch information4294$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42954296# generate anchor for "patch" links in difftree / whatchanged part4297print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4298 format_diff_cc_simplified($diffinfo,@hash_parents) .4299"</div>\n";# class="patch"43004301$patch_number++;4302}43034304if($patch_number==0) {4305if(@hash_parents>1) {4306print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4307}else{4308print"<div class=\"diff nodifferences\">No differences found</div>\n";4309}4310}43114312print"</div>\n";# class="patchset"4313}43144315# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43164317# fills project list info (age, description, owner, forks) for each4318# project in the list, removing invalid projects from returned list4319# NOTE: modifies $projlist, but does not remove entries from it4320sub fill_project_list_info {4321my($projlist,$check_forks) =@_;4322my@projects;43234324my$show_ctags= gitweb_check_feature('ctags');4325 PROJECT:4326foreachmy$pr(@$projlist) {4327my(@activity) = git_get_last_activity($pr->{'path'});4328unless(@activity) {4329next PROJECT;4330}4331($pr->{'age'},$pr->{'age_string'}) =@activity;4332if(!defined$pr->{'descr'}) {4333my$descr= git_get_project_description($pr->{'path'}) ||"";4334$descr= to_utf8($descr);4335$pr->{'descr_long'} =$descr;4336$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4337}4338if(!defined$pr->{'owner'}) {4339$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4340}4341if($check_forks) {4342my$pname=$pr->{'path'};4343if(($pname=~s/\.git$//) &&4344($pname!~/\/$/) &&4345(-d "$projectroot/$pname")) {4346$pr->{'forks'} ="-d$projectroot/$pname";4347}else{4348$pr->{'forks'} =0;4349}4350}4351$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4352push@projects,$pr;4353}43544355return@projects;4356}43574358# print 'sort by' <th> element, generating 'sort by $name' replay link4359# if that order is not selected4360sub print_sort_th {4361print format_sort_th(@_);4362}43634364sub format_sort_th {4365my($name,$order,$header) =@_;4366my$sort_th="";4367$header||=ucfirst($name);43684369if($ordereq$name) {4370$sort_th.="<th>$header</th>\n";4371}else{4372$sort_th.="<th>".4373$cgi->a({-href => href(-replay=>1, order=>$name),4374-class=>"header"},$header) .4375"</th>\n";4376}43774378return$sort_th;4379}43804381sub git_project_list_body {4382# actually uses global variable $project4383my($projlist,$order,$from,$to,$extra,$no_header) =@_;43844385my$check_forks= gitweb_check_feature('forks');4386my@projects= fill_project_list_info($projlist,$check_forks);43874388$order||=$default_projects_order;4389$from=0unlessdefined$from;4390$to=$#projectsif(!defined$to||$#projects<$to);43914392my%order_info= (4393 project => { key =>'path', type =>'str'},4394 descr => { key =>'descr_long', type =>'str'},4395 owner => { key =>'owner', type =>'str'},4396 age => { key =>'age', type =>'num'}4397);4398my$oi=$order_info{$order};4399if($oi->{'type'}eq'str') {4400@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4401}else{4402@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4403}44044405my$show_ctags= gitweb_check_feature('ctags');4406if($show_ctags) {4407my%ctags;4408foreachmy$p(@projects) {4409foreachmy$ct(keys%{$p->{'ctags'}}) {4410$ctags{$ct} +=$p->{'ctags'}->{$ct};4411}4412}4413my$cloud= git_populate_project_tagcloud(\%ctags);4414print git_show_project_tagcloud($cloud,64);4415}44164417print"<table class=\"project_list\">\n";4418unless($no_header) {4419print"<tr>\n";4420if($check_forks) {4421print"<th></th>\n";4422}4423 print_sort_th('project',$order,'Project');4424 print_sort_th('descr',$order,'Description');4425 print_sort_th('owner',$order,'Owner');4426 print_sort_th('age',$order,'Last Change');4427print"<th></th>\n".# for links4428"</tr>\n";4429}4430my$alternate=1;4431my$tagfilter=$cgi->param('by_tag');4432for(my$i=$from;$i<=$to;$i++) {4433my$pr=$projects[$i];44344435next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4436next if$searchtextand not$pr->{'path'} =~/$searchtext/4437and not$pr->{'descr_long'} =~/$searchtext/;4438# Weed out forks or non-matching entries of search4439if($check_forks) {4440my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4441$forkbase="^$forkbase"if$forkbase;4442next ifnot$searchtextand not$tagfilterand$show_ctags4443and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4444}44454446if($alternate) {4447print"<tr class=\"dark\">\n";4448}else{4449print"<tr class=\"light\">\n";4450}4451$alternate^=1;4452if($check_forks) {4453print"<td>";4454if($pr->{'forks'}) {4455print"<!--$pr->{'forks'} -->\n";4456print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4457}4458print"</td>\n";4459}4460print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4461-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4462"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4463-class=>"list", -title =>$pr->{'descr_long'}},4464 esc_html($pr->{'descr'})) ."</td>\n".4465"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4466print"<td class=\"". age_class($pr->{'age'}) ."\">".4467(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4468"<td class=\"link\">".4469$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4470$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4471$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4472$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4473($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4474"</td>\n".4475"</tr>\n";4476}4477if(defined$extra) {4478print"<tr>\n";4479if($check_forks) {4480print"<td></td>\n";4481}4482print"<td colspan=\"5\">$extra</td>\n".4483"</tr>\n";4484}4485print"</table>\n";4486}44874488sub git_log_body {4489# uses global variable $project4490my($commitlist,$from,$to,$refs,$extra) =@_;44914492$from=0unlessdefined$from;4493$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);44944495for(my$i=0;$i<=$to;$i++) {4496my%co= %{$commitlist->[$i]};4497next if!%co;4498my$commit=$co{'id'};4499my$ref= format_ref_marker($refs,$commit);4500my%ad= parse_date($co{'author_epoch'});4501 git_print_header_div('commit',4502"<span class=\"age\">$co{'age_string'}</span>".4503 esc_html($co{'title'}) .$ref,4504$commit);4505print"<div class=\"title_text\">\n".4506"<div class=\"log_link\">\n".4507$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4508" | ".4509$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4510" | ".4511$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4512"<br/>\n".4513"</div>\n";4514 git_print_authorship(\%co, -tag =>'span');4515print"<br/>\n</div>\n";45164517print"<div class=\"log_body\">\n";4518 git_print_log($co{'comment'}, -final_empty_line=>1);4519print"</div>\n";4520}4521if($extra) {4522print"<div class=\"page_nav\">\n";4523print"$extra\n";4524print"</div>\n";4525}4526}45274528sub git_shortlog_body {4529# uses global variable $project4530my($commitlist,$from,$to,$refs,$extra) =@_;45314532$from=0unlessdefined$from;4533$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45344535print"<table class=\"shortlog\">\n";4536my$alternate=1;4537for(my$i=$from;$i<=$to;$i++) {4538my%co= %{$commitlist->[$i]};4539my$commit=$co{'id'};4540my$ref= format_ref_marker($refs,$commit);4541if($alternate) {4542print"<tr class=\"dark\">\n";4543}else{4544print"<tr class=\"light\">\n";4545}4546$alternate^=1;4547# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4548print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4549 format_author_html('td', \%co,10) ."<td>";4550print format_subject_html($co{'title'},$co{'title_short'},4551 href(action=>"commit", hash=>$commit),$ref);4552print"</td>\n".4553"<td class=\"link\">".4554$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4555$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4556$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4557my$snapshot_links= format_snapshot_links($commit);4558if(defined$snapshot_links) {4559print" | ".$snapshot_links;4560}4561print"</td>\n".4562"</tr>\n";4563}4564if(defined$extra) {4565print"<tr>\n".4566"<td colspan=\"4\">$extra</td>\n".4567"</tr>\n";4568}4569print"</table>\n";4570}45714572sub git_history_body {4573# Warning: assumes constant type (blob or tree) during history4574my($commitlist,$from,$to,$refs,$extra,4575$file_name,$file_hash,$ftype) =@_;45764577$from=0unlessdefined$from;4578$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});45794580print"<table class=\"history\">\n";4581my$alternate=1;4582for(my$i=$from;$i<=$to;$i++) {4583my%co= %{$commitlist->[$i]};4584if(!%co) {4585next;4586}4587my$commit=$co{'id'};45884589my$ref= format_ref_marker($refs,$commit);45904591if($alternate) {4592print"<tr class=\"dark\">\n";4593}else{4594print"<tr class=\"light\">\n";4595}4596$alternate^=1;4597print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4598# shortlog: format_author_html('td', \%co, 10)4599 format_author_html('td', \%co,15,3) ."<td>";4600# originally git_history used chop_str($co{'title'}, 50)4601print format_subject_html($co{'title'},$co{'title_short'},4602 href(action=>"commit", hash=>$commit),$ref);4603print"</td>\n".4604"<td class=\"link\">".4605$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4606$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");46074608if($ftypeeq'blob') {4609my$blob_current=$file_hash;4610my$blob_parent= git_get_hash_by_path($commit,$file_name);4611if(defined$blob_current&&defined$blob_parent&&4612$blob_currentne$blob_parent) {4613print" | ".4614$cgi->a({-href => href(action=>"blobdiff",4615 hash=>$blob_current, hash_parent=>$blob_parent,4616 hash_base=>$hash_base, hash_parent_base=>$commit,4617 file_name=>$file_name)},4618"diff to current");4619}4620}4621print"</td>\n".4622"</tr>\n";4623}4624if(defined$extra) {4625print"<tr>\n".4626"<td colspan=\"4\">$extra</td>\n".4627"</tr>\n";4628}4629print"</table>\n";4630}46314632sub git_tags_body {4633# uses global variable $project4634my($taglist,$from,$to,$extra) =@_;4635$from=0unlessdefined$from;4636$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);46374638print"<table class=\"tags\">\n";4639my$alternate=1;4640for(my$i=$from;$i<=$to;$i++) {4641my$entry=$taglist->[$i];4642my%tag=%$entry;4643my$comment=$tag{'subject'};4644my$comment_short;4645if(defined$comment) {4646$comment_short= chop_str($comment,30,5);4647}4648if($alternate) {4649print"<tr class=\"dark\">\n";4650}else{4651print"<tr class=\"light\">\n";4652}4653$alternate^=1;4654if(defined$tag{'age'}) {4655print"<td><i>$tag{'age'}</i></td>\n";4656}else{4657print"<td></td>\n";4658}4659print"<td>".4660$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4661-class=>"list name"}, esc_html($tag{'name'})) .4662"</td>\n".4663"<td>";4664if(defined$comment) {4665print format_subject_html($comment,$comment_short,4666 href(action=>"tag", hash=>$tag{'id'}));4667}4668print"</td>\n".4669"<td class=\"selflink\">";4670if($tag{'type'}eq"tag") {4671print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4672}else{4673print" ";4674}4675print"</td>\n".4676"<td class=\"link\">"." | ".4677$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4678if($tag{'reftype'}eq"commit") {4679print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4680" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4681}elsif($tag{'reftype'}eq"blob") {4682print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4683}4684print"</td>\n".4685"</tr>";4686}4687if(defined$extra) {4688print"<tr>\n".4689"<td colspan=\"5\">$extra</td>\n".4690"</tr>\n";4691}4692print"</table>\n";4693}46944695sub git_heads_body {4696# uses global variable $project4697my($headlist,$head,$from,$to,$extra) =@_;4698$from=0unlessdefined$from;4699$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);47004701print"<table class=\"heads\">\n";4702my$alternate=1;4703for(my$i=$from;$i<=$to;$i++) {4704my$entry=$headlist->[$i];4705my%ref=%$entry;4706my$curr=$ref{'id'}eq$head;4707if($alternate) {4708print"<tr class=\"dark\">\n";4709}else{4710print"<tr class=\"light\">\n";4711}4712$alternate^=1;4713print"<td><i>$ref{'age'}</i></td>\n".4714($curr?"<td class=\"current_head\">":"<td>") .4715$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4716-class=>"list name"},esc_html($ref{'name'})) .4717"</td>\n".4718"<td class=\"link\">".4719$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4720$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4721$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4722"</td>\n".4723"</tr>";4724}4725if(defined$extra) {4726print"<tr>\n".4727"<td colspan=\"3\">$extra</td>\n".4728"</tr>\n";4729}4730print"</table>\n";4731}47324733sub git_search_grep_body {4734my($commitlist,$from,$to,$extra) =@_;4735$from=0unlessdefined$from;4736$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47374738print"<table class=\"commit_search\">\n";4739my$alternate=1;4740for(my$i=$from;$i<=$to;$i++) {4741my%co= %{$commitlist->[$i]};4742if(!%co) {4743next;4744}4745my$commit=$co{'id'};4746if($alternate) {4747print"<tr class=\"dark\">\n";4748}else{4749print"<tr class=\"light\">\n";4750}4751$alternate^=1;4752print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4753 format_author_html('td', \%co,15,5) .4754"<td>".4755$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4756-class=>"list subject"},4757 chop_and_escape_str($co{'title'},50) ."<br/>");4758my$comment=$co{'comment'};4759foreachmy$line(@$comment) {4760if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4761my($lead,$match,$trail) = ($1,$2,$3);4762$match= chop_str($match,70,5,'center');4763my$contextlen=int((80-length($match))/2);4764$contextlen=30if($contextlen>30);4765$lead= chop_str($lead,$contextlen,10,'left');4766$trail= chop_str($trail,$contextlen,10,'right');47674768$lead= esc_html($lead);4769$match= esc_html($match);4770$trail= esc_html($trail);47714772print"$lead<span class=\"match\">$match</span>$trail<br />";4773}4774}4775print"</td>\n".4776"<td class=\"link\">".4777$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4778" | ".4779$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4780" | ".4781$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4782print"</td>\n".4783"</tr>\n";4784}4785if(defined$extra) {4786print"<tr>\n".4787"<td colspan=\"3\">$extra</td>\n".4788"</tr>\n";4789}4790print"</table>\n";4791}47924793## ======================================================================4794## ======================================================================4795## actions47964797sub git_project_list {4798my$order=$input_params{'order'};4799if(defined$order&&$order!~m/none|project|descr|owner|age/) {4800 die_error(400,"Unknown order parameter");4801}48024803my@list= git_get_projects_list();4804if(!@list) {4805 die_error(404,"No projects found");4806}48074808 git_header_html();4809if(defined$home_text&& -f $home_text) {4810print"<div class=\"index_include\">\n";4811 insert_file($home_text);4812print"</div>\n";4813}4814print$cgi->startform(-method=>"get") .4815"<p class=\"projsearch\">Search:\n".4816$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4817"</p>".4818$cgi->end_form() ."\n";4819 git_project_list_body(\@list,$order);4820 git_footer_html();4821}48224823sub git_forks {4824my$order=$input_params{'order'};4825if(defined$order&&$order!~m/none|project|descr|owner|age/) {4826 die_error(400,"Unknown order parameter");4827}48284829my@list= git_get_projects_list($project);4830if(!@list) {4831 die_error(404,"No forks found");4832}48334834 git_header_html();4835 git_print_page_nav('','');4836 git_print_header_div('summary',"$projectforks");4837 git_project_list_body(\@list,$order);4838 git_footer_html();4839}48404841sub git_project_index {4842my@projects= git_get_projects_list($project);48434844print$cgi->header(4845-type =>'text/plain',4846-charset =>'utf-8',4847-content_disposition =>'inline; filename="index.aux"');48484849foreachmy$pr(@projects) {4850if(!exists$pr->{'owner'}) {4851$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4852}48534854my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4855# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4856$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4857$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4858$path=~s/ /\+/g;4859$owner=~s/ /\+/g;48604861print"$path$owner\n";4862}4863}48644865sub git_summary {4866my$descr= git_get_project_description($project) ||"none";4867my%co= parse_commit("HEAD");4868my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4869my$head=$co{'id'};48704871my$owner= git_get_project_owner($project);48724873my$refs= git_get_references();4874# These get_*_list functions return one more to allow us to see if4875# there are more ...4876my@taglist= git_get_tags_list(16);4877my@headlist= git_get_heads_list(16);4878my@forklist;4879my$check_forks= gitweb_check_feature('forks');48804881if($check_forks) {4882@forklist= git_get_projects_list($project);4883}48844885 git_header_html();4886 git_print_page_nav('summary','',$head);48874888print"<div class=\"title\"> </div>\n";4889print"<table class=\"projects_list\">\n".4890"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4891"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4892if(defined$cd{'rfc2822'}) {4893print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4894}48954896# use per project git URL list in $projectroot/$project/cloneurl4897# or make project git URL from git base URL and project name4898my$url_tag="URL";4899my@url_list= git_get_project_url_list($project);4900@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4901foreachmy$git_url(@url_list) {4902next unless$git_url;4903print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4904$url_tag="";4905}49064907# Tag cloud4908my$show_ctags= gitweb_check_feature('ctags');4909if($show_ctags) {4910my$ctags= git_get_project_ctags($project);4911my$cloud= git_populate_project_tagcloud($ctags);4912print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4913print"</td>\n<td>"unless%$ctags;4914print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4915print"</td>\n<td>"if%$ctags;4916print git_show_project_tagcloud($cloud,48);4917print"</td></tr>";4918}49194920print"</table>\n";49214922# If XSS prevention is on, we don't include README.html.4923# TODO: Allow a readme in some safe format.4924if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4925print"<div class=\"title\">readme</div>\n".4926"<div class=\"readme\">\n";4927 insert_file("$projectroot/$project/README.html");4928print"\n</div>\n";# class="readme"4929}49304931# we need to request one more than 16 (0..15) to check if4932# those 16 are all4933my@commitlist=$head? parse_commits($head,17) : ();4934if(@commitlist) {4935 git_print_header_div('shortlog');4936 git_shortlog_body(\@commitlist,0,15,$refs,4937$#commitlist<=15?undef:4938$cgi->a({-href => href(action=>"shortlog")},"..."));4939}49404941if(@taglist) {4942 git_print_header_div('tags');4943 git_tags_body(\@taglist,0,15,4944$#taglist<=15?undef:4945$cgi->a({-href => href(action=>"tags")},"..."));4946}49474948if(@headlist) {4949 git_print_header_div('heads');4950 git_heads_body(\@headlist,$head,0,15,4951$#headlist<=15?undef:4952$cgi->a({-href => href(action=>"heads")},"..."));4953}49544955if(@forklist) {4956 git_print_header_div('forks');4957 git_project_list_body(\@forklist,'age',0,15,4958$#forklist<=15?undef:4959$cgi->a({-href => href(action=>"forks")},"..."),4960'no_header');4961}49624963 git_footer_html();4964}49654966sub git_tag {4967my$head= git_get_head_hash($project);4968 git_header_html();4969 git_print_page_nav('','',$head,undef,$head);4970my%tag= parse_tag($hash);49714972if(!%tag) {4973 die_error(404,"Unknown tag object");4974}49754976 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4977print"<div class=\"title_text\">\n".4978"<table class=\"object_header\">\n".4979"<tr>\n".4980"<td>object</td>\n".4981"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4982$tag{'object'}) ."</td>\n".4983"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4984$tag{'type'}) ."</td>\n".4985"</tr>\n";4986if(defined($tag{'author'})) {4987 git_print_authorship_rows(\%tag,'author');4988}4989print"</table>\n\n".4990"</div>\n";4991print"<div class=\"page_body\">";4992my$comment=$tag{'comment'};4993foreachmy$line(@$comment) {4994chomp$line;4995print esc_html($line, -nbsp=>1) ."<br/>\n";4996}4997print"</div>\n";4998 git_footer_html();4999}50005001sub git_blame_common {5002my$format=shift||'porcelain';5003if($formateq'porcelain'&&$cgi->param('js')) {5004$format='incremental';5005$action='blame_incremental';# for page title etc5006}50075008# permissions5009 gitweb_check_feature('blame')5010or die_error(403,"Blame view not allowed");50115012# error checking5013 die_error(400,"No file name given")unless$file_name;5014$hash_base||= git_get_head_hash($project);5015 die_error(404,"Couldn't find base commit")unless$hash_base;5016my%co= parse_commit($hash_base)5017or die_error(404,"Commit not found");5018my$ftype="blob";5019if(!defined$hash) {5020$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5021or die_error(404,"Error looking up file");5022}else{5023$ftype= git_get_type($hash);5024if($ftype!~"blob") {5025 die_error(400,"Object is not a blob");5026}5027}50285029my$fd;5030if($formateq'incremental') {5031# get file contents (as base)5032open$fd,"-|", git_cmd(),'cat-file','blob',$hash5033or die_error(500,"Open git-cat-file failed");5034}elsif($formateq'data') {5035# run git-blame --incremental5036open$fd,"-|", git_cmd(),"blame","--incremental",5037$hash_base,"--",$file_name5038or die_error(500,"Open git-blame --incremental failed");5039}else{5040# run git-blame --porcelain5041open$fd,"-|", git_cmd(),"blame",'-p',5042$hash_base,'--',$file_name5043or die_error(500,"Open git-blame --porcelain failed");5044}50455046# incremental blame data returns early5047if($formateq'data') {5048print$cgi->header(5049-type=>"text/plain", -charset =>"utf-8",5050-status=>"200 OK");5051local$| =1;# output autoflush5052printwhile<$fd>;5053close$fd5054or print"ERROR$!\n";50555056print'END';5057if(defined$t0&& gitweb_check_feature('timed')) {5058print' '.5059 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5060' '.$number_of_git_cmds;5061}5062print"\n";50635064return;5065}50665067# page header5068 git_header_html();5069my$formats_nav=5070$cgi->a({-href => href(action=>"blob", -replay=>1)},5071"blob") .5072" | ";5073if($formateq'incremental') {5074$formats_nav.=5075$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5076"blame") ." (non-incremental)";5077}else{5078$formats_nav.=5079$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5080"blame") ." (incremental)";5081}5082$formats_nav.=5083" | ".5084$cgi->a({-href => href(action=>"history", -replay=>1)},5085"history") .5086" | ".5087$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5088"HEAD");5089 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5090 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5091 git_print_page_path($file_name,$ftype,$hash_base);50925093# page body5094if($formateq'incremental') {5095print"<noscript>\n<div class=\"error\"><center><b>\n".5096"This page requires JavaScript to run.\nUse ".5097$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5098'this page').5099" instead.\n".5100"</b></center></div>\n</noscript>\n";51015102print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5103}51045105print qq!<div class="page_body">\n!;5106print qq!<div id="progress_info">.../ ...</div>\n!5107if($formateq'incremental');5108print qq!<table id="blame_table"class="blame" width="100%">\n!.5109#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5110 qq!<thead>\n!.5111 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5112 qq!</thead>\n!.5113 qq!<tbody>\n!;51145115my@rev_color=qw(light dark);5116my$num_colors=scalar(@rev_color);5117my$current_color=0;51185119if($formateq'incremental') {5120my$color_class=$rev_color[$current_color];51215122#contents of a file5123my$linenr=0;5124 LINE:5125while(my$line= <$fd>) {5126chomp$line;5127$linenr++;51285129print qq!<tr id="l$linenr"class="$color_class">!.5130 qq!<td class="sha1"><a href=""> </a></td>!.5131 qq!<td class="linenr">!.5132 qq!<a class="linenr" href="">$linenr</a></td>!;5133print qq!<td class="pre">! . esc_html($line) ."</td>\n";5134print qq!</tr>\n!;5135}51365137}else{# porcelain, i.e. ordinary blame5138my%metainfo= ();# saves information about commits51395140# blame data5141 LINE:5142while(my$line= <$fd>) {5143chomp$line;5144# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5145# no <lines in group> for subsequent lines in group of lines5146my($full_rev,$orig_lineno,$lineno,$group_size) =5147($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5148if(!exists$metainfo{$full_rev}) {5149$metainfo{$full_rev} = {'nprevious'=>0};5150}5151my$meta=$metainfo{$full_rev};5152my$data;5153while($data= <$fd>) {5154chomp$data;5155last if($data=~s/^\t//);# contents of line5156if($data=~/^(\S+)(?: (.*))?$/) {5157$meta->{$1} =$2unlessexists$meta->{$1};5158}5159if($data=~/^previous /) {5160$meta->{'nprevious'}++;5161}5162}5163my$short_rev=substr($full_rev,0,8);5164my$author=$meta->{'author'};5165my%date=5166 parse_date($meta->{'author-time'},$meta->{'author-tz'});5167my$date=$date{'iso-tz'};5168if($group_size) {5169$current_color= ($current_color+1) %$num_colors;5170}5171my$tr_class=$rev_color[$current_color];5172$tr_class.=' boundary'if(exists$meta->{'boundary'});5173$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5174$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5175print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5176if($group_size) {5177print"<td class=\"sha1\"";5178print" title=\"". esc_html($author) .",$date\"";5179print" rowspan=\"$group_size\""if($group_size>1);5180print">";5181print$cgi->a({-href => href(action=>"commit",5182 hash=>$full_rev,5183 file_name=>$file_name)},5184 esc_html($short_rev));5185if($group_size>=2) {5186my@author_initials= ($author=~/\b([[:upper:]])\B/g);5187if(@author_initials) {5188print"<br />".5189 esc_html(join('',@author_initials));5190# or join('.', ...)5191}5192}5193print"</td>\n";5194}5195# 'previous' <sha1 of parent commit> <filename at commit>5196if(exists$meta->{'previous'} &&5197$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5198$meta->{'parent'} =$1;5199$meta->{'file_parent'} = unquote($2);5200}5201my$linenr_commit=5202exists($meta->{'parent'}) ?5203$meta->{'parent'} :$full_rev;5204my$linenr_filename=5205exists($meta->{'file_parent'}) ?5206$meta->{'file_parent'} : unquote($meta->{'filename'});5207my$blamed= href(action =>'blame',5208 file_name =>$linenr_filename,5209 hash_base =>$linenr_commit);5210print"<td class=\"linenr\">";5211print$cgi->a({ -href =>"$blamed#l$orig_lineno",5212-class=>"linenr"},5213 esc_html($lineno));5214print"</td>";5215print"<td class=\"pre\">". esc_html($data) ."</td>\n";5216print"</tr>\n";5217}# end while52185219}52205221# footer5222print"</tbody>\n".5223"</table>\n";# class="blame"5224print"</div>\n";# class="blame_body"5225close$fd5226or print"Reading blob failed\n";52275228 git_footer_html();5229}52305231sub git_blame {5232 git_blame_common();5233}52345235sub git_blame_incremental {5236 git_blame_common('incremental');5237}52385239sub git_blame_data {5240 git_blame_common('data');5241}52425243sub git_tags {5244my$head= git_get_head_hash($project);5245 git_header_html();5246 git_print_page_nav('','',$head,undef,$head);5247 git_print_header_div('summary',$project);52485249my@tagslist= git_get_tags_list();5250if(@tagslist) {5251 git_tags_body(\@tagslist);5252}5253 git_footer_html();5254}52555256sub git_heads {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@headslist= git_get_heads_list();5263if(@headslist) {5264 git_heads_body(\@headslist,$head);5265}5266 git_footer_html();5267}52685269sub git_blob_plain {5270my$type=shift;5271my$expires;52725273if(!defined$hash) {5274if(defined$file_name) {5275my$base=$hash_base|| git_get_head_hash($project);5276$hash= git_get_hash_by_path($base,$file_name,"blob")5277or die_error(404,"Cannot find file");5278}else{5279 die_error(400,"No file name defined");5280}5281}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5282# blobs defined by non-textual hash id's can be cached5283$expires="+1d";5284}52855286open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5287or die_error(500,"Open git-cat-file blob '$hash' failed");52885289# content-type (can include charset)5290$type= blob_contenttype($fd,$file_name,$type);52915292# "save as" filename, even when no $file_name is given5293my$save_as="$hash";5294if(defined$file_name) {5295$save_as=$file_name;5296}elsif($type=~m/^text\//) {5297$save_as.='.txt';5298}52995300# With XSS prevention on, blobs of all types except a few known safe5301# ones are served with "Content-Disposition: attachment" to make sure5302# they don't run in our security domain. For certain image types,5303# blob view writes an <img> tag referring to blob_plain view, and we5304# want to be sure not to break that by serving the image as an5305# attachment (though Firefox 3 doesn't seem to care).5306my$sandbox=$prevent_xss&&5307$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;53085309print$cgi->header(5310-type =>$type,5311-expires =>$expires,5312-content_disposition =>5313($sandbox?'attachment':'inline')5314.'; filename="'.$save_as.'"');5315local$/=undef;5316binmode STDOUT,':raw';5317print<$fd>;5318binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5319close$fd;5320}53215322sub git_blob {5323my$expires;53245325if(!defined$hash) {5326if(defined$file_name) {5327my$base=$hash_base|| git_get_head_hash($project);5328$hash= git_get_hash_by_path($base,$file_name,"blob")5329or die_error(404,"Cannot find file");5330}else{5331 die_error(400,"No file name defined");5332}5333}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5334# blobs defined by non-textual hash id's can be cached5335$expires="+1d";5336}53375338my$have_blame= gitweb_check_feature('blame');5339open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5340or die_error(500,"Couldn't cat$file_name,$hash");5341my$mimetype= blob_mimetype($fd,$file_name);5342if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5343close$fd;5344return git_blob_plain($mimetype);5345}5346# we can have blame only for text/* mimetype5347$have_blame&&= ($mimetype=~m!^text/!);53485349 git_header_html(undef,$expires);5350my$formats_nav='';5351if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5352if(defined$file_name) {5353if($have_blame) {5354$formats_nav.=5355$cgi->a({-href => href(action=>"blame", -replay=>1)},5356"blame") .5357" | ";5358}5359$formats_nav.=5360$cgi->a({-href => href(action=>"history", -replay=>1)},5361"history") .5362" | ".5363$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5364"raw") .5365" | ".5366$cgi->a({-href => href(action=>"blob",5367 hash_base=>"HEAD", file_name=>$file_name)},5368"HEAD");5369}else{5370$formats_nav.=5371$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5372"raw");5373}5374 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5375 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5376}else{5377print"<div class=\"page_nav\">\n".5378"<br/><br/></div>\n".5379"<div class=\"title\">$hash</div>\n";5380}5381 git_print_page_path($file_name,"blob",$hash_base);5382print"<div class=\"page_body\">\n";5383if($mimetype=~m!^image/!) {5384print qq!<img type="$mimetype"!;5385if($file_name) {5386print qq! alt="$file_name" title="$file_name"!;5387}5388print qq! src="! .5389 href(action=>"blob_plain", hash=>$hash,5390 hash_base=>$hash_base, file_name=>$file_name) .5391 qq!"/>\n!;5392}else{5393my$nr;5394while(my$line= <$fd>) {5395chomp$line;5396$nr++;5397$line= untabify($line);5398printf"<div class=\"pre\"><a id=\"l%i\"href=\"". href(-replay =>1)5399."#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5400$nr,$nr,$nr, esc_html($line, -nbsp=>1);5401}5402}5403close$fd5404or print"Reading blob failed.\n";5405print"</div>";5406 git_footer_html();5407}54085409sub git_tree {5410if(!defined$hash_base) {5411$hash_base="HEAD";5412}5413if(!defined$hash) {5414if(defined$file_name) {5415$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5416}else{5417$hash=$hash_base;5418}5419}5420 die_error(404,"No such tree")unlessdefined($hash);54215422my$show_sizes= gitweb_check_feature('show-sizes');5423my$have_blame= gitweb_check_feature('blame');54245425my@entries= ();5426{5427local$/="\0";5428open my$fd,"-|", git_cmd(),"ls-tree",'-z',5429($show_sizes?'-l': ()),@extra_options,$hash5430or die_error(500,"Open git-ls-tree failed");5431@entries=map{chomp;$_} <$fd>;5432close$fd5433or die_error(404,"Reading tree failed");5434}54355436my$refs= git_get_references();5437my$ref= format_ref_marker($refs,$hash_base);5438 git_header_html();5439my$basedir='';5440if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5441my@views_nav= ();5442if(defined$file_name) {5443push@views_nav,5444$cgi->a({-href => href(action=>"history", -replay=>1)},5445"history"),5446$cgi->a({-href => href(action=>"tree",5447 hash_base=>"HEAD", file_name=>$file_name)},5448"HEAD"),5449}5450my$snapshot_links= format_snapshot_links($hash);5451if(defined$snapshot_links) {5452# FIXME: Should be available when we have no hash base as well.5453push@views_nav,$snapshot_links;5454}5455 git_print_page_nav('tree','',$hash_base,undef,undef,5456join(' | ',@views_nav));5457 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5458}else{5459undef$hash_base;5460print"<div class=\"page_nav\">\n";5461print"<br/><br/></div>\n";5462print"<div class=\"title\">$hash</div>\n";5463}5464if(defined$file_name) {5465$basedir=$file_name;5466if($basedirne''&&substr($basedir, -1)ne'/') {5467$basedir.='/';5468}5469 git_print_page_path($file_name,'tree',$hash_base);5470}5471print"<div class=\"page_body\">\n";5472print"<table class=\"tree\">\n";5473my$alternate=1;5474# '..' (top directory) link if possible5475if(defined$hash_base&&5476defined$file_name&&$file_name=~m![^/]+$!) {5477if($alternate) {5478print"<tr class=\"dark\">\n";5479}else{5480print"<tr class=\"light\">\n";5481}5482$alternate^=1;54835484my$up=$file_name;5485$up=~s!/?[^/]+$!!;5486undef$upunless$up;5487# based on git_print_tree_entry5488print'<td class="mode">'. mode_str('040000') ."</td>\n";5489print'<td class="size"> </td>'."\n"if$show_sizes;5490print'<td class="list">';5491print$cgi->a({-href => href(action=>"tree",5492 hash_base=>$hash_base,5493 file_name=>$up)},5494"..");5495print"</td>\n";5496print"<td class=\"link\"></td>\n";54975498print"</tr>\n";5499}5500foreachmy$line(@entries) {5501my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);55025503if($alternate) {5504print"<tr class=\"dark\">\n";5505}else{5506print"<tr class=\"light\">\n";5507}5508$alternate^=1;55095510 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);55115512print"</tr>\n";5513}5514print"</table>\n".5515"</div>";5516 git_footer_html();5517}55185519sub snapshot_name {5520my($project,$hash) =@_;55215522# path/to/project.git -> project5523# path/to/project/.git -> project5524my$name= to_utf8($project);5525$name=~ s,([^/])/*\.git$,$1,;5526$name= basename($name);5527# sanitize name5528$name=~s/[[:cntrl:]]/?/g;55295530my$ver=$hash;5531if($hash=~/^[0-9a-fA-F]+$/) {5532# shorten SHA-1 hash5533my$full_hash= git_get_full_hash($project,$hash);5534if($full_hash=~/^$hash/&&length($hash) >7) {5535$ver= git_get_short_hash($project,$hash);5536}5537}elsif($hash=~m!^refs/tags/(.*)$!) {5538# tags don't need shortened SHA-1 hash5539$ver=$1;5540}else{5541# branches and other need shortened SHA-1 hash5542if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5543$ver=$1;5544}5545$ver.='-'. git_get_short_hash($project,$hash);5546}5547# in case of hierarchical branch names5548$ver=~s!/!.!g;55495550# name = project-version_string5551$name="$name-$ver";55525553returnwantarray? ($name,$name) :$name;5554}55555556sub git_snapshot {5557my$format=$input_params{'snapshot_format'};5558if(!@snapshot_fmts) {5559 die_error(403,"Snapshots not allowed");5560}5561# default to first supported snapshot format5562$format||=$snapshot_fmts[0];5563if($format!~m/^[a-z0-9]+$/) {5564 die_error(400,"Invalid snapshot format parameter");5565}elsif(!exists($known_snapshot_formats{$format})) {5566 die_error(400,"Unknown snapshot format");5567}elsif($known_snapshot_formats{$format}{'disabled'}) {5568 die_error(403,"Snapshot format not allowed");5569}elsif(!grep($_eq$format,@snapshot_fmts)) {5570 die_error(403,"Unsupported snapshot format");5571}55725573my$type= git_get_type("$hash^{}");5574if(!$type) {5575 die_error(404,'Object does not exist');5576}elsif($typeeq'blob') {5577 die_error(400,'Object is not a tree-ish');5578}55795580my($name,$prefix) = snapshot_name($project,$hash);5581my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5582my$cmd= quote_command(5583 git_cmd(),'archive',5584"--format=$known_snapshot_formats{$format}{'format'}",5585"--prefix=$prefix/",$hash);5586if(exists$known_snapshot_formats{$format}{'compressor'}) {5587$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5588}55895590$filename=~s/(["\\])/\\$1/g;5591print$cgi->header(5592-type =>$known_snapshot_formats{$format}{'type'},5593-content_disposition =>'inline; filename="'.$filename.'"',5594-status =>'200 OK');55955596open my$fd,"-|",$cmd5597or die_error(500,"Execute git-archive failed");5598binmode STDOUT,':raw';5599print<$fd>;5600binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5601close$fd;5602}56035604sub git_log_generic {5605my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;56065607my$head= git_get_head_hash($project);5608if(!defined$base) {5609$base=$head;5610}5611if(!defined$page) {5612$page=0;5613}5614my$refs= git_get_references();56155616my$commit_hash=$base;5617if(defined$parent) {5618$commit_hash="$parent..$base";5619}5620my@commitlist=5621 parse_commits($commit_hash,101, (100*$page),5622defined$file_name? ($file_name,"--full-history") : ());56235624my$ftype;5625if(!defined$file_hash&&defined$file_name) {5626# some commits could have deleted file in question,5627# and not have it in tree, but one of them has to have it5628for(my$i=0;$i<@commitlist;$i++) {5629$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5630last ifdefined$file_hash;5631}5632}5633if(defined$file_hash) {5634$ftype= git_get_type($file_hash);5635}5636if(defined$file_name&& !defined$ftype) {5637 die_error(500,"Unknown type of object");5638}5639my%co;5640if(defined$file_name) {5641%co= parse_commit($base)5642or die_error(404,"Unknown commit object");5643}564456455646my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5647my$next_link='';5648if($#commitlist>=100) {5649$next_link=5650$cgi->a({-href => href(-replay=>1, page=>$page+1),5651-accesskey =>"n", -title =>"Alt-n"},"next");5652}5653my$patch_max= gitweb_get_feature('patches');5654if($patch_max&& !defined$file_name) {5655if($patch_max<0||@commitlist<=$patch_max) {5656$paging_nav.=" ⋅ ".5657$cgi->a({-href => href(action=>"patches", -replay=>1)},5658"patches");5659}5660}56615662 git_header_html();5663 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5664if(defined$file_name) {5665 git_print_header_div('commit', esc_html($co{'title'}),$base);5666}else{5667 git_print_header_div('summary',$project)5668}5669 git_print_page_path($file_name,$ftype,$hash_base)5670if(defined$file_name);56715672$body_subr->(\@commitlist,0,99,$refs,$next_link,5673$file_name,$file_hash,$ftype);56745675 git_footer_html();5676}56775678sub git_log {5679 git_log_generic('log', \&git_log_body,5680$hash,$hash_parent);5681}56825683sub git_commit {5684$hash||=$hash_base||"HEAD";5685my%co= parse_commit($hash)5686or die_error(404,"Unknown commit object");56875688my$parent=$co{'parent'};5689my$parents=$co{'parents'};# listref56905691# we need to prepare $formats_nav before any parameter munging5692my$formats_nav;5693if(!defined$parent) {5694# --root commitdiff5695$formats_nav.='(initial)';5696}elsif(@$parents==1) {5697# single parent commit5698$formats_nav.=5699'(parent: '.5700$cgi->a({-href => href(action=>"commit",5701 hash=>$parent)},5702 esc_html(substr($parent,0,7))) .5703')';5704}else{5705# merge commit5706$formats_nav.=5707'(merge: '.5708join(' ',map{5709$cgi->a({-href => href(action=>"commit",5710 hash=>$_)},5711 esc_html(substr($_,0,7)));5712}@$parents) .5713')';5714}5715if(gitweb_check_feature('patches') &&@$parents<=1) {5716$formats_nav.=" | ".5717$cgi->a({-href => href(action=>"patch", -replay=>1)},5718"patch");5719}57205721if(!defined$parent) {5722$parent="--root";5723}5724my@difftree;5725open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5726@diff_opts,5727(@$parents<=1?$parent:'-c'),5728$hash,"--"5729or die_error(500,"Open git-diff-tree failed");5730@difftree=map{chomp;$_} <$fd>;5731close$fdor die_error(404,"Reading git-diff-tree failed");57325733# non-textual hash id's can be cached5734my$expires;5735if($hash=~m/^[0-9a-fA-F]{40}$/) {5736$expires="+1d";5737}5738my$refs= git_get_references();5739my$ref= format_ref_marker($refs,$co{'id'});57405741 git_header_html(undef,$expires);5742 git_print_page_nav('commit','',5743$hash,$co{'tree'},$hash,5744$formats_nav);57455746if(defined$co{'parent'}) {5747 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5748}else{5749 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5750}5751print"<div class=\"title_text\">\n".5752"<table class=\"object_header\">\n";5753 git_print_authorship_rows(\%co);5754print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5755print"<tr>".5756"<td>tree</td>".5757"<td class=\"sha1\">".5758$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5759class=>"list"},$co{'tree'}) .5760"</td>".5761"<td class=\"link\">".5762$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5763"tree");5764my$snapshot_links= format_snapshot_links($hash);5765if(defined$snapshot_links) {5766print" | ".$snapshot_links;5767}5768print"</td>".5769"</tr>\n";57705771foreachmy$par(@$parents) {5772print"<tr>".5773"<td>parent</td>".5774"<td class=\"sha1\">".5775$cgi->a({-href => href(action=>"commit", hash=>$par),5776class=>"list"},$par) .5777"</td>".5778"<td class=\"link\">".5779$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5780" | ".5781$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5782"</td>".5783"</tr>\n";5784}5785print"</table>".5786"</div>\n";57875788print"<div class=\"page_body\">\n";5789 git_print_log($co{'comment'});5790print"</div>\n";57915792 git_difftree_body(\@difftree,$hash,@$parents);57935794 git_footer_html();5795}57965797sub git_object {5798# object is defined by:5799# - hash or hash_base alone5800# - hash_base and file_name5801my$type;58025803# - hash or hash_base alone5804if($hash|| ($hash_base&& !defined$file_name)) {5805my$object_id=$hash||$hash_base;58065807open my$fd,"-|", quote_command(5808 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5809or die_error(404,"Object does not exist");5810$type= <$fd>;5811chomp$type;5812close$fd5813or die_error(404,"Object does not exist");58145815# - hash_base and file_name5816}elsif($hash_base&&defined$file_name) {5817$file_name=~ s,/+$,,;58185819system(git_cmd(),"cat-file",'-e',$hash_base) ==05820or die_error(404,"Base object does not exist");58215822# here errors should not hapen5823open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5824or die_error(500,"Open git-ls-tree failed");5825my$line= <$fd>;5826close$fd;58275828#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5829unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5830 die_error(404,"File or directory for given base does not exist");5831}5832$type=$2;5833$hash=$3;5834}else{5835 die_error(400,"Not enough information to find object");5836}58375838print$cgi->redirect(-uri => href(action=>$type, -full=>1,5839 hash=>$hash, hash_base=>$hash_base,5840 file_name=>$file_name),5841-status =>'302 Found');5842}58435844sub git_blobdiff {5845my$format=shift||'html';58465847my$fd;5848my@difftree;5849my%diffinfo;5850my$expires;58515852# preparing $fd and %diffinfo for git_patchset_body5853# new style URI5854if(defined$hash_base&&defined$hash_parent_base) {5855if(defined$file_name) {5856# read raw output5857open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5858$hash_parent_base,$hash_base,5859"--", (defined$file_parent?$file_parent: ()),$file_name5860or die_error(500,"Open git-diff-tree failed");5861@difftree=map{chomp;$_} <$fd>;5862close$fd5863or die_error(404,"Reading git-diff-tree failed");5864@difftree5865or die_error(404,"Blob diff not found");58665867}elsif(defined$hash&&5868$hash=~/[0-9a-fA-F]{40}/) {5869# try to find filename from $hash58705871# read filtered raw output5872open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5873$hash_parent_base,$hash_base,"--"5874or die_error(500,"Open git-diff-tree failed");5875@difftree=5876# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5877# $hash == to_id5878grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5879map{chomp;$_} <$fd>;5880close$fd5881or die_error(404,"Reading git-diff-tree failed");5882@difftree5883or die_error(404,"Blob diff not found");58845885}else{5886 die_error(400,"Missing one of the blob diff parameters");5887}58885889if(@difftree>1) {5890 die_error(400,"Ambiguous blob diff specification");5891}58925893%diffinfo= parse_difftree_raw_line($difftree[0]);5894$file_parent||=$diffinfo{'from_file'} ||$file_name;5895$file_name||=$diffinfo{'to_file'};58965897$hash_parent||=$diffinfo{'from_id'};5898$hash||=$diffinfo{'to_id'};58995900# non-textual hash id's can be cached5901if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5902$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5903$expires='+1d';5904}59055906# open patch output5907open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5908'-p', ($formateq'html'?"--full-index": ()),5909$hash_parent_base,$hash_base,5910"--", (defined$file_parent?$file_parent: ()),$file_name5911or die_error(500,"Open git-diff-tree failed");5912}59135914# old/legacy style URI -- not generated anymore since 1.4.3.5915if(!%diffinfo) {5916 die_error('404 Not Found',"Missing one of the blob diff parameters")5917}59185919# header5920if($formateq'html') {5921my$formats_nav=5922$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5923"raw");5924 git_header_html(undef,$expires);5925if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5926 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5927 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5928}else{5929print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5930print"<div class=\"title\">$hashvs$hash_parent</div>\n";5931}5932if(defined$file_name) {5933 git_print_page_path($file_name,"blob",$hash_base);5934}else{5935print"<div class=\"page_path\"></div>\n";5936}59375938}elsif($formateq'plain') {5939print$cgi->header(5940-type =>'text/plain',5941-charset =>'utf-8',5942-expires =>$expires,5943-content_disposition =>'inline; filename="'."$file_name".'.patch"');59445945print"X-Git-Url: ".$cgi->self_url() ."\n\n";59465947}else{5948 die_error(400,"Unknown blobdiff format");5949}59505951# patch5952if($formateq'html') {5953print"<div class=\"page_body\">\n";59545955 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5956close$fd;59575958print"</div>\n";# class="page_body"5959 git_footer_html();59605961}else{5962while(my$line= <$fd>) {5963$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5964$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;59655966print$line;59675968last if$line=~m!^\+\+\+!;5969}5970local$/=undef;5971print<$fd>;5972close$fd;5973}5974}59755976sub git_blobdiff_plain {5977 git_blobdiff('plain');5978}59795980sub git_commitdiff {5981my%params=@_;5982my$format=$params{-format} ||'html';59835984my($patch_max) = gitweb_get_feature('patches');5985if($formateq'patch') {5986 die_error(403,"Patch view not allowed")unless$patch_max;5987}59885989$hash||=$hash_base||"HEAD";5990my%co= parse_commit($hash)5991or die_error(404,"Unknown commit object");59925993# choose format for commitdiff for merge5994if(!defined$hash_parent&& @{$co{'parents'}} >1) {5995$hash_parent='--cc';5996}5997# we need to prepare $formats_nav before almost any parameter munging5998my$formats_nav;5999if($formateq'html') {6000$formats_nav=6001$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6002"raw");6003if($patch_max&& @{$co{'parents'}} <=1) {6004$formats_nav.=" | ".6005$cgi->a({-href => href(action=>"patch", -replay=>1)},6006"patch");6007}60086009if(defined$hash_parent&&6010$hash_parentne'-c'&&$hash_parentne'--cc') {6011# commitdiff with two commits given6012my$hash_parent_short=$hash_parent;6013if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6014$hash_parent_short=substr($hash_parent,0,7);6015}6016$formats_nav.=6017' (from';6018for(my$i=0;$i< @{$co{'parents'}};$i++) {6019if($co{'parents'}[$i]eq$hash_parent) {6020$formats_nav.=' parent '. ($i+1);6021last;6022}6023}6024$formats_nav.=': '.6025$cgi->a({-href => href(action=>"commitdiff",6026 hash=>$hash_parent)},6027 esc_html($hash_parent_short)) .6028')';6029}elsif(!$co{'parent'}) {6030# --root commitdiff6031$formats_nav.=' (initial)';6032}elsif(scalar@{$co{'parents'}} ==1) {6033# single parent commit6034$formats_nav.=6035' (parent: '.6036$cgi->a({-href => href(action=>"commitdiff",6037 hash=>$co{'parent'})},6038 esc_html(substr($co{'parent'},0,7))) .6039')';6040}else{6041# merge commit6042if($hash_parenteq'--cc') {6043$formats_nav.=' | '.6044$cgi->a({-href => href(action=>"commitdiff",6045 hash=>$hash, hash_parent=>'-c')},6046'combined');6047}else{# $hash_parent eq '-c'6048$formats_nav.=' | '.6049$cgi->a({-href => href(action=>"commitdiff",6050 hash=>$hash, hash_parent=>'--cc')},6051'compact');6052}6053$formats_nav.=6054' (merge: '.6055join(' ',map{6056$cgi->a({-href => href(action=>"commitdiff",6057 hash=>$_)},6058 esc_html(substr($_,0,7)));6059} @{$co{'parents'}} ) .6060')';6061}6062}60636064my$hash_parent_param=$hash_parent;6065if(!defined$hash_parent_param) {6066# --cc for multiple parents, --root for parentless6067$hash_parent_param=6068@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6069}60706071# read commitdiff6072my$fd;6073my@difftree;6074if($formateq'html') {6075open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6076"--no-commit-id","--patch-with-raw","--full-index",6077$hash_parent_param,$hash,"--"6078or die_error(500,"Open git-diff-tree failed");60796080while(my$line= <$fd>) {6081chomp$line;6082# empty line ends raw part of diff-tree output6083last unless$line;6084push@difftree,scalar parse_difftree_raw_line($line);6085}60866087}elsif($formateq'plain') {6088open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6089'-p',$hash_parent_param,$hash,"--"6090or die_error(500,"Open git-diff-tree failed");6091}elsif($formateq'patch') {6092# For commit ranges, we limit the output to the number of6093# patches specified in the 'patches' feature.6094# For single commits, we limit the output to a single patch,6095# diverging from the git-format-patch default.6096my@commit_spec= ();6097if($hash_parent) {6098if($patch_max>0) {6099push@commit_spec,"-$patch_max";6100}6101push@commit_spec,'-n',"$hash_parent..$hash";6102}else{6103if($params{-single}) {6104push@commit_spec,'-1';6105}else{6106if($patch_max>0) {6107push@commit_spec,"-$patch_max";6108}6109push@commit_spec,"-n";6110}6111push@commit_spec,'--root',$hash;6112}6113open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',6114'--stdout',@commit_spec6115or die_error(500,"Open git-format-patch failed");6116}else{6117 die_error(400,"Unknown commitdiff format");6118}61196120# non-textual hash id's can be cached6121my$expires;6122if($hash=~m/^[0-9a-fA-F]{40}$/) {6123$expires="+1d";6124}61256126# write commit message6127if($formateq'html') {6128my$refs= git_get_references();6129my$ref= format_ref_marker($refs,$co{'id'});61306131 git_header_html(undef,$expires);6132 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6133 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6134print"<div class=\"title_text\">\n".6135"<table class=\"object_header\">\n";6136 git_print_authorship_rows(\%co);6137print"</table>".6138"</div>\n";6139print"<div class=\"page_body\">\n";6140if(@{$co{'comment'}} >1) {6141print"<div class=\"log\">\n";6142 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6143print"</div>\n";# class="log"6144}61456146}elsif($formateq'plain') {6147my$refs= git_get_references("tags");6148my$tagname= git_get_rev_name_tags($hash);6149my$filename= basename($project) ."-$hash.patch";61506151print$cgi->header(6152-type =>'text/plain',6153-charset =>'utf-8',6154-expires =>$expires,6155-content_disposition =>'inline; filename="'."$filename".'"');6156my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6157print"From: ". to_utf8($co{'author'}) ."\n";6158print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6159print"Subject: ". to_utf8($co{'title'}) ."\n";61606161print"X-Git-Tag:$tagname\n"if$tagname;6162print"X-Git-Url: ".$cgi->self_url() ."\n\n";61636164foreachmy$line(@{$co{'comment'}}) {6165print to_utf8($line) ."\n";6166}6167print"---\n\n";6168}elsif($formateq'patch') {6169my$filename= basename($project) ."-$hash.patch";61706171print$cgi->header(6172-type =>'text/plain',6173-charset =>'utf-8',6174-expires =>$expires,6175-content_disposition =>'inline; filename="'."$filename".'"');6176}61776178# write patch6179if($formateq'html') {6180my$use_parents= !defined$hash_parent||6181$hash_parenteq'-c'||$hash_parenteq'--cc';6182 git_difftree_body(\@difftree,$hash,6183$use_parents? @{$co{'parents'}} :$hash_parent);6184print"<br/>\n";61856186 git_patchset_body($fd, \@difftree,$hash,6187$use_parents? @{$co{'parents'}} :$hash_parent);6188close$fd;6189print"</div>\n";# class="page_body"6190 git_footer_html();61916192}elsif($formateq'plain') {6193local$/=undef;6194print<$fd>;6195close$fd6196or print"Reading git-diff-tree failed\n";6197}elsif($formateq'patch') {6198local$/=undef;6199print<$fd>;6200close$fd6201or print"Reading git-format-patch failed\n";6202}6203}62046205sub git_commitdiff_plain {6206 git_commitdiff(-format =>'plain');6207}62086209# format-patch-style patches6210sub git_patch {6211 git_commitdiff(-format =>'patch', -single =>1);6212}62136214sub git_patches {6215 git_commitdiff(-format =>'patch');6216}62176218sub git_history {6219 git_log_generic('history', \&git_history_body,6220$hash_base,$hash_parent_base,6221$file_name,$hash);6222}62236224sub git_search {6225 gitweb_check_feature('search')or die_error(403,"Search is disabled");6226if(!defined$searchtext) {6227 die_error(400,"Text field is empty");6228}6229if(!defined$hash) {6230$hash= git_get_head_hash($project);6231}6232my%co= parse_commit($hash);6233if(!%co) {6234 die_error(404,"Unknown commit object");6235}6236if(!defined$page) {6237$page=0;6238}62396240$searchtype||='commit';6241if($searchtypeeq'pickaxe') {6242# pickaxe may take all resources of your box and run for several minutes6243# with every query - so decide by yourself how public you make this feature6244 gitweb_check_feature('pickaxe')6245or die_error(403,"Pickaxe is disabled");6246}6247if($searchtypeeq'grep') {6248 gitweb_check_feature('grep')6249or die_error(403,"Grep is disabled");6250}62516252 git_header_html();62536254if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6255my$greptype;6256if($searchtypeeq'commit') {6257$greptype="--grep=";6258}elsif($searchtypeeq'author') {6259$greptype="--author=";6260}elsif($searchtypeeq'committer') {6261$greptype="--committer=";6262}6263$greptype.=$searchtext;6264my@commitlist= parse_commits($hash,101, (100*$page),undef,6265$greptype,'--regexp-ignore-case',6266$search_use_regexp?'--extended-regexp':'--fixed-strings');62676268my$paging_nav='';6269if($page>0) {6270$paging_nav.=6271$cgi->a({-href => href(action=>"search", hash=>$hash,6272 searchtext=>$searchtext,6273 searchtype=>$searchtype)},6274"first");6275$paging_nav.=" ⋅ ".6276$cgi->a({-href => href(-replay=>1, page=>$page-1),6277-accesskey =>"p", -title =>"Alt-p"},"prev");6278}else{6279$paging_nav.="first";6280$paging_nav.=" ⋅ prev";6281}6282my$next_link='';6283if($#commitlist>=100) {6284$next_link=6285$cgi->a({-href => href(-replay=>1, page=>$page+1),6286-accesskey =>"n", -title =>"Alt-n"},"next");6287$paging_nav.=" ⋅$next_link";6288}else{6289$paging_nav.=" ⋅ next";6290}62916292if($#commitlist>=100) {6293}62946295 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6296 git_print_header_div('commit', esc_html($co{'title'}),$hash);6297 git_search_grep_body(\@commitlist,0,99,$next_link);6298}62996300if($searchtypeeq'pickaxe') {6301 git_print_page_nav('','',$hash,$co{'tree'},$hash);6302 git_print_header_div('commit', esc_html($co{'title'}),$hash);63036304print"<table class=\"pickaxe search\">\n";6305my$alternate=1;6306local$/="\n";6307open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6308'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6309($search_use_regexp?'--pickaxe-regex': ());6310undef%co;6311my@files;6312while(my$line= <$fd>) {6313chomp$line;6314next unless$line;63156316my%set= parse_difftree_raw_line($line);6317if(defined$set{'commit'}) {6318# finish previous commit6319if(%co) {6320print"</td>\n".6321"<td class=\"link\">".6322$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6323" | ".6324$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6325print"</td>\n".6326"</tr>\n";6327}63286329if($alternate) {6330print"<tr class=\"dark\">\n";6331}else{6332print"<tr class=\"light\">\n";6333}6334$alternate^=1;6335%co= parse_commit($set{'commit'});6336my$author= chop_and_escape_str($co{'author_name'},15,5);6337print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6338"<td><i>$author</i></td>\n".6339"<td>".6340$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6341-class=>"list subject"},6342 chop_and_escape_str($co{'title'},50) ."<br/>");6343}elsif(defined$set{'to_id'}) {6344next if($set{'to_id'} =~m/^0{40}$/);63456346print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6347 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6348-class=>"list"},6349"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6350"<br/>\n";6351}6352}6353close$fd;63546355# finish last commit (warning: repetition!)6356if(%co) {6357print"</td>\n".6358"<td class=\"link\">".6359$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6360" | ".6361$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6362print"</td>\n".6363"</tr>\n";6364}63656366print"</table>\n";6367}63686369if($searchtypeeq'grep') {6370 git_print_page_nav('','',$hash,$co{'tree'},$hash);6371 git_print_header_div('commit', esc_html($co{'title'}),$hash);63726373print"<table class=\"grep_search\">\n";6374my$alternate=1;6375my$matches=0;6376local$/="\n";6377open my$fd,"-|", git_cmd(),'grep','-n',6378$search_use_regexp? ('-E','-i') :'-F',6379$searchtext,$co{'tree'};6380my$lastfile='';6381while(my$line= <$fd>) {6382chomp$line;6383my($file,$lno,$ltext,$binary);6384last if($matches++>1000);6385if($line=~/^Binary file (.+) matches$/) {6386$file=$1;6387$binary=1;6388}else{6389(undef,$file,$lno,$ltext) =split(/:/,$line,4);6390}6391if($filene$lastfile) {6392$lastfileand print"</td></tr>\n";6393if($alternate++) {6394print"<tr class=\"dark\">\n";6395}else{6396print"<tr class=\"light\">\n";6397}6398print"<td class=\"list\">".6399$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6400 file_name=>"$file"),6401-class=>"list"}, esc_path($file));6402print"</td><td>\n";6403$lastfile=$file;6404}6405if($binary) {6406print"<div class=\"binary\">Binary file</div>\n";6407}else{6408$ltext= untabify($ltext);6409if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6410$ltext= esc_html($1, -nbsp=>1);6411$ltext.='<span class="match">';6412$ltext.= esc_html($2, -nbsp=>1);6413$ltext.='</span>';6414$ltext.= esc_html($3, -nbsp=>1);6415}else{6416$ltext= esc_html($ltext, -nbsp=>1);6417}6418print"<div class=\"pre\">".6419$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6420 file_name=>"$file").'#l'.$lno,6421-class=>"linenr"},sprintf('%4i',$lno))6422.' '.$ltext."</div>\n";6423}6424}6425if($lastfile) {6426print"</td></tr>\n";6427if($matches>1000) {6428print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6429}6430}else{6431print"<div class=\"diff nodifferences\">No matches found</div>\n";6432}6433close$fd;64346435print"</table>\n";6436}6437 git_footer_html();6438}64396440sub git_search_help {6441 git_header_html();6442 git_print_page_nav('','',$hash,$hash,$hash);6443print<<EOT;6444<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6445regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6446the pattern entered is recognized as the POSIX extended6447<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6448insensitive).</p>6449<dl>6450<dt><b>commit</b></dt>6451<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6452EOT6453my$have_grep= gitweb_check_feature('grep');6454if($have_grep) {6455print<<EOT;6456<dt><b>grep</b></dt>6457<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6458 a different one) are searched for the given pattern. On large trees, this search can take6459a while and put some strain on the server, so please use it with some consideration. Note that6460due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6461case-sensitive.</dd>6462EOT6463}6464print<<EOT;6465<dt><b>author</b></dt>6466<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6467<dt><b>committer</b></dt>6468<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6469EOT6470my$have_pickaxe= gitweb_check_feature('pickaxe');6471if($have_pickaxe) {6472print<<EOT;6473<dt><b>pickaxe</b></dt>6474<dd>All commits that caused the string to appear or disappear from any file (changes that6475added, removed or "modified" the string) will be listed. This search can take a while and6476takes a lot of strain on the server, so please use it wisely. Note that since you may be6477interested even in changes just changing the case as well, this search is case sensitive.</dd>6478EOT6479}6480print"</dl>\n";6481 git_footer_html();6482}64836484sub git_shortlog {6485 git_log_generic('shortlog', \&git_shortlog_body,6486$hash,$hash_parent);6487}64886489## ......................................................................6490## feeds (RSS, Atom; OPML)64916492sub git_feed {6493my$format=shift||'atom';6494my$have_blame= gitweb_check_feature('blame');64956496# Atom: http://www.atomenabled.org/developers/syndication/6497# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6498if($formatne'rss'&&$formatne'atom') {6499 die_error(400,"Unknown web feed format");6500}65016502# log/feed of current (HEAD) branch, log of given branch, history of file/directory6503my$head=$hash||'HEAD';6504my@commitlist= parse_commits($head,150,0,$file_name);65056506my%latest_commit;6507my%latest_date;6508my$content_type="application/$format+xml";6509if(defined$cgi->http('HTTP_ACCEPT') &&6510$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6511# browser (feed reader) prefers text/xml6512$content_type='text/xml';6513}6514if(defined($commitlist[0])) {6515%latest_commit= %{$commitlist[0]};6516my$latest_epoch=$latest_commit{'committer_epoch'};6517%latest_date= parse_date($latest_epoch);6518my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6519if(defined$if_modified) {6520my$since;6521if(eval{require HTTP::Date;1; }) {6522$since= HTTP::Date::str2time($if_modified);6523}elsif(eval{require Time::ParseDate;1; }) {6524$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6525}6526if(defined$since&&$latest_epoch<=$since) {6527print$cgi->header(6528-type =>$content_type,6529-charset =>'utf-8',6530-last_modified =>$latest_date{'rfc2822'},6531-status =>'304 Not Modified');6532return;6533}6534}6535print$cgi->header(6536-type =>$content_type,6537-charset =>'utf-8',6538-last_modified =>$latest_date{'rfc2822'});6539}else{6540print$cgi->header(6541-type =>$content_type,6542-charset =>'utf-8');6543}65446545# Optimization: skip generating the body if client asks only6546# for Last-Modified date.6547return if($cgi->request_method()eq'HEAD');65486549# header variables6550my$title="$site_name-$project/$action";6551my$feed_type='log';6552if(defined$hash) {6553$title.=" - '$hash'";6554$feed_type='branch log';6555if(defined$file_name) {6556$title.=" ::$file_name";6557$feed_type='history';6558}6559}elsif(defined$file_name) {6560$title.=" -$file_name";6561$feed_type='history';6562}6563$title.="$feed_type";6564my$descr= git_get_project_description($project);6565if(defined$descr) {6566$descr= esc_html($descr);6567}else{6568$descr="$project".6569($formateq'rss'?'RSS':'Atom') .6570" feed";6571}6572my$owner= git_get_project_owner($project);6573$owner= esc_html($owner);65746575#header6576my$alt_url;6577if(defined$file_name) {6578$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6579}elsif(defined$hash) {6580$alt_url= href(-full=>1, action=>"log", hash=>$hash);6581}else{6582$alt_url= href(-full=>1, action=>"summary");6583}6584print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6585if($formateq'rss') {6586print<<XML;6587<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6588<channel>6589XML6590print"<title>$title</title>\n".6591"<link>$alt_url</link>\n".6592"<description>$descr</description>\n".6593"<language>en</language>\n".6594# project owner is responsible for 'editorial' content6595"<managingEditor>$owner</managingEditor>\n";6596if(defined$logo||defined$favicon) {6597# prefer the logo to the favicon, since RSS6598# doesn't allow both6599my$img= esc_url($logo||$favicon);6600print"<image>\n".6601"<url>$img</url>\n".6602"<title>$title</title>\n".6603"<link>$alt_url</link>\n".6604"</image>\n";6605}6606if(%latest_date) {6607print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6608print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6609}6610print"<generator>gitweb v.$version/$git_version</generator>\n";6611}elsif($formateq'atom') {6612print<<XML;6613<feed xmlns="http://www.w3.org/2005/Atom">6614XML6615print"<title>$title</title>\n".6616"<subtitle>$descr</subtitle>\n".6617'<link rel="alternate" type="text/html" href="'.6618$alt_url.'" />'."\n".6619'<link rel="self" type="'.$content_type.'" href="'.6620$cgi->self_url() .'" />'."\n".6621"<id>". href(-full=>1) ."</id>\n".6622# use project owner for feed author6623"<author><name>$owner</name></author>\n";6624if(defined$favicon) {6625print"<icon>". esc_url($favicon) ."</icon>\n";6626}6627if(defined$logo_url) {6628# not twice as wide as tall: 72 x 27 pixels6629print"<logo>". esc_url($logo) ."</logo>\n";6630}6631if(!%latest_date) {6632# dummy date to keep the feed valid until commits trickle in:6633print"<updated>1970-01-01T00:00:00Z</updated>\n";6634}else{6635print"<updated>$latest_date{'iso-8601'}</updated>\n";6636}6637print"<generator version='$version/$git_version'>gitweb</generator>\n";6638}66396640# contents6641for(my$i=0;$i<=$#commitlist;$i++) {6642my%co= %{$commitlist[$i]};6643my$commit=$co{'id'};6644# we read 150, we always show 30 and the ones more recent than 48 hours6645if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6646last;6647}6648my%cd= parse_date($co{'author_epoch'});66496650# get list of changed files6651open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6652$co{'parent'} ||"--root",6653$co{'id'},"--", (defined$file_name?$file_name: ())6654ornext;6655my@difftree=map{chomp;$_} <$fd>;6656close$fd6657ornext;66586659# print element (entry, item)6660my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6661if($formateq'rss') {6662print"<item>\n".6663"<title>". esc_html($co{'title'}) ."</title>\n".6664"<author>". esc_html($co{'author'}) ."</author>\n".6665"<pubDate>$cd{'rfc2822'}</pubDate>\n".6666"<guid isPermaLink=\"true\">$co_url</guid>\n".6667"<link>$co_url</link>\n".6668"<description>". esc_html($co{'title'}) ."</description>\n".6669"<content:encoded>".6670"<![CDATA[\n";6671}elsif($formateq'atom') {6672print"<entry>\n".6673"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6674"<updated>$cd{'iso-8601'}</updated>\n".6675"<author>\n".6676" <name>". esc_html($co{'author_name'}) ."</name>\n";6677if($co{'author_email'}) {6678print" <email>". esc_html($co{'author_email'}) ."</email>\n";6679}6680print"</author>\n".6681# use committer for contributor6682"<contributor>\n".6683" <name>". esc_html($co{'committer_name'}) ."</name>\n";6684if($co{'committer_email'}) {6685print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6686}6687print"</contributor>\n".6688"<published>$cd{'iso-8601'}</published>\n".6689"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6690"<id>$co_url</id>\n".6691"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6692"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6693}6694my$comment=$co{'comment'};6695print"<pre>\n";6696foreachmy$line(@$comment) {6697$line= esc_html($line);6698print"$line\n";6699}6700print"</pre><ul>\n";6701foreachmy$difftree_line(@difftree) {6702my%difftree= parse_difftree_raw_line($difftree_line);6703next if!$difftree{'from_id'};67046705my$file=$difftree{'file'} ||$difftree{'to_file'};67066707print"<li>".6708"[".6709$cgi->a({-href => href(-full=>1, action=>"blobdiff",6710 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6711 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6712 file_name=>$file, file_parent=>$difftree{'from_file'}),6713-title =>"diff"},'D');6714if($have_blame) {6715print$cgi->a({-href => href(-full=>1, action=>"blame",6716 file_name=>$file, hash_base=>$commit),6717-title =>"blame"},'B');6718}6719# if this is not a feed of a file history6720if(!defined$file_name||$file_namene$file) {6721print$cgi->a({-href => href(-full=>1, action=>"history",6722 file_name=>$file, hash=>$commit),6723-title =>"history"},'H');6724}6725$file= esc_path($file);6726print"] ".6727"$file</li>\n";6728}6729if($formateq'rss') {6730print"</ul>]]>\n".6731"</content:encoded>\n".6732"</item>\n";6733}elsif($formateq'atom') {6734print"</ul>\n</div>\n".6735"</content>\n".6736"</entry>\n";6737}6738}67396740# end of feed6741if($formateq'rss') {6742print"</channel>\n</rss>\n";6743}elsif($formateq'atom') {6744print"</feed>\n";6745}6746}67476748sub git_rss {6749 git_feed('rss');6750}67516752sub git_atom {6753 git_feed('atom');6754}67556756sub git_opml {6757my@list= git_get_projects_list();67586759print$cgi->header(6760-type =>'text/xml',6761-charset =>'utf-8',6762-content_disposition =>'inline; filename="opml.xml"');67636764print<<XML;6765<?xml version="1.0" encoding="utf-8"?>6766<opml version="1.0">6767<head>6768 <title>$site_nameOPML Export</title>6769</head>6770<body>6771<outline text="git RSS feeds">6772XML67736774foreachmy$pr(@list) {6775my%proj=%$pr;6776my$head= git_get_head_hash($proj{'path'});6777if(!defined$head) {6778next;6779}6780$git_dir="$projectroot/$proj{'path'}";6781my%co= parse_commit($head);6782if(!%co) {6783next;6784}67856786my$path= esc_html(chop_str($proj{'path'},25,5));6787my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6788my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6789print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6790}6791print<<XML;6792</outline>6793</body>6794</opml>6795XML6796}