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) =@_;22112212# do we have project2213return unless(defined$project&&defined$git_dir);22142215# key sanity check2216return unless($key);2217$key=~s/^gitweb\.//;2218return if($key=~m/\W/);22192220# type sanity check2221if(defined$type) {2222$type=~s/^--//;2223$type=undef2224unless($typeeq'bool'||$typeeq'int');2225}22262227# get config2228if(!defined$config_file||2229$config_filene"$git_dir/config") {2230%config= git_parse_project_config('gitweb');2231$config_file="$git_dir/config";2232}22332234# check if config variable (key) exists2235return unlessexists$config{"gitweb.$key"};22362237# ensure given type2238if(!defined$type) {2239return$config{"gitweb.$key"};2240}elsif($typeeq'bool') {2241# backward compatibility: 'git config --bool' returns true/false2242return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2243}elsif($typeeq'int') {2244return config_to_int($config{"gitweb.$key"});2245}2246return$config{"gitweb.$key"};2247}22482249# get hash of given path at given ref2250sub git_get_hash_by_path {2251my$base=shift;2252my$path=shift||returnundef;2253my$type=shift;22542255$path=~ s,/+$,,;22562257open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2258or die_error(500,"Open git-ls-tree failed");2259my$line= <$fd>;2260close$fdorreturnundef;22612262if(!defined$line) {2263# there is no tree or hash given by $path at $base2264returnundef;2265}22662267#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2268$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2269if(defined$type&&$typene$2) {2270# type doesn't match2271returnundef;2272}2273return$3;2274}22752276# get path of entry with given hash at given tree-ish (ref)2277# used to get 'from' filename for combined diff (merge commit) for renames2278sub git_get_path_by_hash {2279my$base=shift||return;2280my$hash=shift||return;22812282local$/="\0";22832284open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2285orreturnundef;2286while(my$line= <$fd>) {2287chomp$line;22882289#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2290#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2291if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2292close$fd;2293return$1;2294}2295}2296close$fd;2297returnundef;2298}22992300## ......................................................................2301## git utility functions, directly accessing git repository23022303sub git_get_project_description {2304my$path=shift;23052306$git_dir="$projectroot/$path";2307open my$fd,'<',"$git_dir/description"2308orreturn git_get_project_config('description');2309my$descr= <$fd>;2310close$fd;2311if(defined$descr) {2312chomp$descr;2313}2314return$descr;2315}23162317sub git_get_project_ctags {2318my$path=shift;2319my$ctags= {};23202321$git_dir="$projectroot/$path";2322opendir my$dh,"$git_dir/ctags"2323orreturn$ctags;2324foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2325open my$ct,'<',$_ornext;2326my$val= <$ct>;2327chomp$val;2328close$ct;2329my$ctag=$_;$ctag=~ s#.*/##;2330$ctags->{$ctag} =$val;2331}2332closedir$dh;2333$ctags;2334}23352336sub git_populate_project_tagcloud {2337my$ctags=shift;23382339# First, merge different-cased tags; tags vote on casing2340my%ctags_lc;2341foreach(keys%$ctags) {2342$ctags_lc{lc$_}->{count} +=$ctags->{$_};2343if(not$ctags_lc{lc$_}->{topcount}2344or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2345$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2346$ctags_lc{lc$_}->{topname} =$_;2347}2348}23492350my$cloud;2351if(eval{require HTML::TagCloud;1; }) {2352$cloud= HTML::TagCloud->new;2353foreach(sort keys%ctags_lc) {2354# Pad the title with spaces so that the cloud looks2355# less crammed.2356my$title=$ctags_lc{$_}->{topname};2357$title=~s/ / /g;2358$title=~s/^/ /g;2359$title=~s/$/ /g;2360$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2361}2362}else{2363$cloud= \%ctags_lc;2364}2365$cloud;2366}23672368sub git_show_project_tagcloud {2369my($cloud,$count) =@_;2370print STDERR ref($cloud)."..\n";2371if(ref$cloudeq'HTML::TagCloud') {2372return$cloud->html_and_css($count);2373}else{2374my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2375return'<p align="center">'.join(', ',map{2376"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2377}splice(@tags,0,$count)) .'</p>';2378}2379}23802381sub git_get_project_url_list {2382my$path=shift;23832384$git_dir="$projectroot/$path";2385open my$fd,'<',"$git_dir/cloneurl"2386orreturnwantarray?2387@{ config_to_multi(git_get_project_config('url')) } :2388 config_to_multi(git_get_project_config('url'));2389my@git_project_url_list=map{chomp;$_} <$fd>;2390close$fd;23912392returnwantarray?@git_project_url_list: \@git_project_url_list;2393}23942395sub git_get_projects_list {2396my($filter) =@_;2397my@list;23982399$filter||='';2400$filter=~s/\.git$//;24012402my$check_forks= gitweb_check_feature('forks');24032404if(-d $projects_list) {2405# search in directory2406my$dir=$projects_list. ($filter?"/$filter":'');2407# remove the trailing "/"2408$dir=~s!/+$!!;2409my$pfxlen=length("$dir");2410my$pfxdepth= ($dir=~tr!/!!);24112412 File::Find::find({2413 follow_fast =>1,# follow symbolic links2414 follow_skip =>2,# ignore duplicates2415 dangling_symlinks =>0,# ignore dangling symlinks, silently2416 wanted =>sub{2417# skip project-list toplevel, if we get it.2418return if(m!^[/.]$!);2419# only directories can be git repositories2420return unless(-d $_);2421# don't traverse too deep (Find is super slow on os x)2422if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2423$File::Find::prune =1;2424return;2425}24262427my$subdir=substr($File::Find::name,$pfxlen+1);2428# we check related file in $projectroot2429my$path= ($filter?"$filter/":'') .$subdir;2430if(check_export_ok("$projectroot/$path")) {2431push@list, { path =>$path};2432$File::Find::prune =1;2433}2434},2435},"$dir");24362437}elsif(-f $projects_list) {2438# read from file(url-encoded):2439# 'git%2Fgit.git Linus+Torvalds'2440# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2441# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2442my%paths;2443open my$fd,'<',$projects_listorreturn;2444 PROJECT:2445while(my$line= <$fd>) {2446chomp$line;2447my($path,$owner) =split' ',$line;2448$path= unescape($path);2449$owner= unescape($owner);2450if(!defined$path) {2451next;2452}2453if($filterne'') {2454# looking for forks;2455my$pfx=substr($path,0,length($filter));2456if($pfxne$filter) {2457next PROJECT;2458}2459my$sfx=substr($path,length($filter));2460if($sfx!~/^\/.*\.git$/) {2461next PROJECT;2462}2463}elsif($check_forks) {2464 PATH:2465foreachmy$filter(keys%paths) {2466# looking for forks;2467my$pfx=substr($path,0,length($filter));2468if($pfxne$filter) {2469next PATH;2470}2471my$sfx=substr($path,length($filter));2472if($sfx!~/^\/.*\.git$/) {2473next PATH;2474}2475# is a fork, don't include it in2476# the list2477next PROJECT;2478}2479}2480if(check_export_ok("$projectroot/$path")) {2481my$pr= {2482 path =>$path,2483 owner => to_utf8($owner),2484};2485push@list,$pr;2486(my$forks_path=$path) =~s/\.git$//;2487$paths{$forks_path}++;2488}2489}2490close$fd;2491}2492return@list;2493}24942495our$gitweb_project_owner=undef;2496sub git_get_project_list_from_file {24972498return if(defined$gitweb_project_owner);24992500$gitweb_project_owner= {};2501# read from file (url-encoded):2502# 'git%2Fgit.git Linus+Torvalds'2503# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2504# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2505if(-f $projects_list) {2506open(my$fd,'<',$projects_list);2507while(my$line= <$fd>) {2508chomp$line;2509my($pr,$ow) =split' ',$line;2510$pr= unescape($pr);2511$ow= unescape($ow);2512$gitweb_project_owner->{$pr} = to_utf8($ow);2513}2514close$fd;2515}2516}25172518sub git_get_project_owner {2519my$project=shift;2520my$owner;25212522returnundefunless$project;2523$git_dir="$projectroot/$project";25242525if(!defined$gitweb_project_owner) {2526 git_get_project_list_from_file();2527}25282529if(exists$gitweb_project_owner->{$project}) {2530$owner=$gitweb_project_owner->{$project};2531}2532if(!defined$owner){2533$owner= git_get_project_config('owner');2534}2535if(!defined$owner) {2536$owner= get_file_owner("$git_dir");2537}25382539return$owner;2540}25412542sub git_get_last_activity {2543my($path) =@_;2544my$fd;25452546$git_dir="$projectroot/$path";2547open($fd,"-|", git_cmd(),'for-each-ref',2548'--format=%(committer)',2549'--sort=-committerdate',2550'--count=1',2551'refs/heads')orreturn;2552my$most_recent= <$fd>;2553close$fdorreturn;2554if(defined$most_recent&&2555$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2556my$timestamp=$1;2557my$age=time-$timestamp;2558return($age, age_string($age));2559}2560return(undef,undef);2561}25622563sub git_get_references {2564my$type=shift||"";2565my%refs;2566# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112567# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2568open my$fd,"-|", git_cmd(),"show-ref","--dereference",2569($type? ("--","refs/$type") : ())# use -- <pattern> if $type2570orreturn;25712572while(my$line= <$fd>) {2573chomp$line;2574if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2575if(defined$refs{$1}) {2576push@{$refs{$1}},$2;2577}else{2578$refs{$1} = [$2];2579}2580}2581}2582close$fdorreturn;2583return \%refs;2584}25852586sub git_get_rev_name_tags {2587my$hash=shift||returnundef;25882589open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2590orreturn;2591my$name_rev= <$fd>;2592close$fd;25932594if($name_rev=~ m|^$hash tags/(.*)$|) {2595return$1;2596}else{2597# catches also '$hash undefined' output2598returnundef;2599}2600}26012602## ----------------------------------------------------------------------2603## parse to hash functions26042605sub parse_date {2606my$epoch=shift;2607my$tz=shift||"-0000";26082609my%date;2610my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2611my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2612my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2613$date{'hour'} =$hour;2614$date{'minute'} =$min;2615$date{'mday'} =$mday;2616$date{'day'} =$days[$wday];2617$date{'month'} =$months[$mon];2618$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2619$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2620$date{'mday-time'} =sprintf"%d%s%02d:%02d",2621$mday,$months[$mon],$hour,$min;2622$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",26231900+$year,1+$mon,$mday,$hour,$min,$sec;26242625$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2626my$local=$epoch+ ((int$1+ ($2/60)) *3600);2627($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2628$date{'hour_local'} =$hour;2629$date{'minute_local'} =$min;2630$date{'tz_local'} =$tz;2631$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",26321900+$year,$mon+1,$mday,2633$hour,$min,$sec,$tz);2634return%date;2635}26362637sub parse_tag {2638my$tag_id=shift;2639my%tag;2640my@comment;26412642open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2643$tag{'id'} =$tag_id;2644while(my$line= <$fd>) {2645chomp$line;2646if($line=~m/^object ([0-9a-fA-F]{40})$/) {2647$tag{'object'} =$1;2648}elsif($line=~m/^type (.+)$/) {2649$tag{'type'} =$1;2650}elsif($line=~m/^tag (.+)$/) {2651$tag{'name'} =$1;2652}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2653$tag{'author'} =$1;2654$tag{'author_epoch'} =$2;2655$tag{'author_tz'} =$3;2656if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2657$tag{'author_name'} =$1;2658$tag{'author_email'} =$2;2659}else{2660$tag{'author_name'} =$tag{'author'};2661}2662}elsif($line=~m/--BEGIN/) {2663push@comment,$line;2664last;2665}elsif($lineeq"") {2666last;2667}2668}2669push@comment, <$fd>;2670$tag{'comment'} = \@comment;2671close$fdorreturn;2672if(!defined$tag{'name'}) {2673return2674};2675return%tag2676}26772678sub parse_commit_text {2679my($commit_text,$withparents) =@_;2680my@commit_lines=split'\n',$commit_text;2681my%co;26822683pop@commit_lines;# Remove '\0'26842685if(!@commit_lines) {2686return;2687}26882689my$header=shift@commit_lines;2690if($header!~m/^[0-9a-fA-F]{40}/) {2691return;2692}2693($co{'id'},my@parents) =split' ',$header;2694while(my$line=shift@commit_lines) {2695last if$lineeq"\n";2696if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2697$co{'tree'} =$1;2698}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2699push@parents,$1;2700}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2701$co{'author'} = to_utf8($1);2702$co{'author_epoch'} =$2;2703$co{'author_tz'} =$3;2704if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2705$co{'author_name'} =$1;2706$co{'author_email'} =$2;2707}else{2708$co{'author_name'} =$co{'author'};2709}2710}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2711$co{'committer'} = to_utf8($1);2712$co{'committer_epoch'} =$2;2713$co{'committer_tz'} =$3;2714if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2715$co{'committer_name'} =$1;2716$co{'committer_email'} =$2;2717}else{2718$co{'committer_name'} =$co{'committer'};2719}2720}2721}2722if(!defined$co{'tree'}) {2723return;2724};2725$co{'parents'} = \@parents;2726$co{'parent'} =$parents[0];27272728foreachmy$title(@commit_lines) {2729$title=~s/^ //;2730if($titlene"") {2731$co{'title'} = chop_str($title,80,5);2732# remove leading stuff of merges to make the interesting part visible2733if(length($title) >50) {2734$title=~s/^Automatic //;2735$title=~s/^merge (of|with) /Merge ... /i;2736if(length($title) >50) {2737$title=~s/(http|rsync):\/\///;2738}2739if(length($title) >50) {2740$title=~s/(master|www|rsync)\.//;2741}2742if(length($title) >50) {2743$title=~s/kernel.org:?//;2744}2745if(length($title) >50) {2746$title=~s/\/pub\/scm//;2747}2748}2749$co{'title_short'} = chop_str($title,50,5);2750last;2751}2752}2753if(!defined$co{'title'} ||$co{'title'}eq"") {2754$co{'title'} =$co{'title_short'} ='(no commit message)';2755}2756# remove added spaces2757foreachmy$line(@commit_lines) {2758$line=~s/^ //;2759}2760$co{'comment'} = \@commit_lines;27612762my$age=time-$co{'committer_epoch'};2763$co{'age'} =$age;2764$co{'age_string'} = age_string($age);2765my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2766if($age>60*60*24*7*2) {2767$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2768$co{'age_string_age'} =$co{'age_string'};2769}else{2770$co{'age_string_date'} =$co{'age_string'};2771$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2772}2773return%co;2774}27752776sub parse_commit {2777my($commit_id) =@_;2778my%co;27792780local$/="\0";27812782open my$fd,"-|", git_cmd(),"rev-list",2783"--parents",2784"--header",2785"--max-count=1",2786$commit_id,2787"--",2788or die_error(500,"Open git-rev-list failed");2789%co= parse_commit_text(<$fd>,1);2790close$fd;27912792return%co;2793}27942795sub parse_commits {2796my($commit_id,$maxcount,$skip,$filename,@args) =@_;2797my@cos;27982799$maxcount||=1;2800$skip||=0;28012802local$/="\0";28032804open my$fd,"-|", git_cmd(),"rev-list",2805"--header",2806@args,2807("--max-count=".$maxcount),2808("--skip=".$skip),2809@extra_options,2810$commit_id,2811"--",2812($filename? ($filename) : ())2813or die_error(500,"Open git-rev-list failed");2814while(my$line= <$fd>) {2815my%co= parse_commit_text($line);2816push@cos, \%co;2817}2818close$fd;28192820returnwantarray?@cos: \@cos;2821}28222823# parse line of git-diff-tree "raw" output2824sub parse_difftree_raw_line {2825my$line=shift;2826my%res;28272828# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2829# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2830if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2831$res{'from_mode'} =$1;2832$res{'to_mode'} =$2;2833$res{'from_id'} =$3;2834$res{'to_id'} =$4;2835$res{'status'} =$5;2836$res{'similarity'} =$6;2837if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2838($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2839}else{2840$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2841}2842}2843# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2844# combined diff (for merge commit)2845elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2846$res{'nparents'} =length($1);2847$res{'from_mode'} = [split(' ',$2) ];2848$res{'to_mode'} =pop@{$res{'from_mode'}};2849$res{'from_id'} = [split(' ',$3) ];2850$res{'to_id'} =pop@{$res{'from_id'}};2851$res{'status'} = [split('',$4) ];2852$res{'to_file'} = unquote($5);2853}2854# 'c512b523472485aef4fff9e57b229d9d243c967f'2855elsif($line=~m/^([0-9a-fA-F]{40})$/) {2856$res{'commit'} =$1;2857}28582859returnwantarray?%res: \%res;2860}28612862# wrapper: return parsed line of git-diff-tree "raw" output2863# (the argument might be raw line, or parsed info)2864sub parsed_difftree_line {2865my$line_or_ref=shift;28662867if(ref($line_or_ref)eq"HASH") {2868# pre-parsed (or generated by hand)2869return$line_or_ref;2870}else{2871return parse_difftree_raw_line($line_or_ref);2872}2873}28742875# parse line of git-ls-tree output2876sub parse_ls_tree_line {2877my$line=shift;2878my%opts=@_;2879my%res;28802881if($opts{'-l'}) {2882#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'2883$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;28842885$res{'mode'} =$1;2886$res{'type'} =$2;2887$res{'hash'} =$3;2888$res{'size'} =$4;2889if($opts{'-z'}) {2890$res{'name'} =$5;2891}else{2892$res{'name'} = unquote($5);2893}2894}else{2895#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2896$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;28972898$res{'mode'} =$1;2899$res{'type'} =$2;2900$res{'hash'} =$3;2901if($opts{'-z'}) {2902$res{'name'} =$4;2903}else{2904$res{'name'} = unquote($4);2905}2906}29072908returnwantarray?%res: \%res;2909}29102911# generates _two_ hashes, references to which are passed as 2 and 3 argument2912sub parse_from_to_diffinfo {2913my($diffinfo,$from,$to,@parents) =@_;29142915if($diffinfo->{'nparents'}) {2916# combined diff2917$from->{'file'} = [];2918$from->{'href'} = [];2919 fill_from_file_info($diffinfo,@parents)2920unlessexists$diffinfo->{'from_file'};2921for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2922$from->{'file'}[$i] =2923defined$diffinfo->{'from_file'}[$i] ?2924$diffinfo->{'from_file'}[$i] :2925$diffinfo->{'to_file'};2926if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2927$from->{'href'}[$i] = href(action=>"blob",2928 hash_base=>$parents[$i],2929 hash=>$diffinfo->{'from_id'}[$i],2930 file_name=>$from->{'file'}[$i]);2931}else{2932$from->{'href'}[$i] =undef;2933}2934}2935}else{2936# ordinary (not combined) diff2937$from->{'file'} =$diffinfo->{'from_file'};2938if($diffinfo->{'status'}ne"A") {# not new (added) file2939$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2940 hash=>$diffinfo->{'from_id'},2941 file_name=>$from->{'file'});2942}else{2943delete$from->{'href'};2944}2945}29462947$to->{'file'} =$diffinfo->{'to_file'};2948if(!is_deleted($diffinfo)) {# file exists in result2949$to->{'href'} = href(action=>"blob", hash_base=>$hash,2950 hash=>$diffinfo->{'to_id'},2951 file_name=>$to->{'file'});2952}else{2953delete$to->{'href'};2954}2955}29562957## ......................................................................2958## parse to array of hashes functions29592960sub git_get_heads_list {2961my$limit=shift;2962my@headslist;29632964open my$fd,'-|', git_cmd(),'for-each-ref',2965($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2966'--format=%(objectname) %(refname) %(subject)%00%(committer)',2967'refs/heads'2968orreturn;2969while(my$line= <$fd>) {2970my%ref_item;29712972chomp$line;2973my($refinfo,$committerinfo) =split(/\0/,$line);2974my($hash,$name,$title) =split(' ',$refinfo,3);2975my($committer,$epoch,$tz) =2976($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2977$ref_item{'fullname'} =$name;2978$name=~s!^refs/heads/!!;29792980$ref_item{'name'} =$name;2981$ref_item{'id'} =$hash;2982$ref_item{'title'} =$title||'(no commit message)';2983$ref_item{'epoch'} =$epoch;2984if($epoch) {2985$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2986}else{2987$ref_item{'age'} ="unknown";2988}29892990push@headslist, \%ref_item;2991}2992close$fd;29932994returnwantarray?@headslist: \@headslist;2995}29962997sub git_get_tags_list {2998my$limit=shift;2999my@tagslist;30003001open my$fd,'-|', git_cmd(),'for-each-ref',3002($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3003'--format=%(objectname) %(objecttype) %(refname) '.3004'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3005'refs/tags'3006orreturn;3007while(my$line= <$fd>) {3008my%ref_item;30093010chomp$line;3011my($refinfo,$creatorinfo) =split(/\0/,$line);3012my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3013my($creator,$epoch,$tz) =3014($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3015$ref_item{'fullname'} =$name;3016$name=~s!^refs/tags/!!;30173018$ref_item{'type'} =$type;3019$ref_item{'id'} =$id;3020$ref_item{'name'} =$name;3021if($typeeq"tag") {3022$ref_item{'subject'} =$title;3023$ref_item{'reftype'} =$reftype;3024$ref_item{'refid'} =$refid;3025}else{3026$ref_item{'reftype'} =$type;3027$ref_item{'refid'} =$id;3028}30293030if($typeeq"tag"||$typeeq"commit") {3031$ref_item{'epoch'} =$epoch;3032if($epoch) {3033$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3034}else{3035$ref_item{'age'} ="unknown";3036}3037}30383039push@tagslist, \%ref_item;3040}3041close$fd;30423043returnwantarray?@tagslist: \@tagslist;3044}30453046## ----------------------------------------------------------------------3047## filesystem-related functions30483049sub get_file_owner {3050my$path=shift;30513052my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3053my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3054if(!defined$gcos) {3055returnundef;3056}3057my$owner=$gcos;3058$owner=~s/[,;].*$//;3059return to_utf8($owner);3060}30613062# assume that file exists3063sub insert_file {3064my$filename=shift;30653066open my$fd,'<',$filename;3067print map{ to_utf8($_) } <$fd>;3068close$fd;3069}30703071## ......................................................................3072## mimetype related functions30733074sub mimetype_guess_file {3075my$filename=shift;3076my$mimemap=shift;3077-r $mimemaporreturnundef;30783079my%mimemap;3080open(my$mh,'<',$mimemap)orreturnundef;3081while(<$mh>) {3082next ifm/^#/;# skip comments3083my($mimetype,$exts) =split(/\t+/);3084if(defined$exts) {3085my@exts=split(/\s+/,$exts);3086foreachmy$ext(@exts) {3087$mimemap{$ext} =$mimetype;3088}3089}3090}3091close($mh);30923093$filename=~/\.([^.]*)$/;3094return$mimemap{$1};3095}30963097sub mimetype_guess {3098my$filename=shift;3099my$mime;3100$filename=~/\./orreturnundef;31013102if($mimetypes_file) {3103my$file=$mimetypes_file;3104if($file!~m!^/!) {# if it is relative path3105# it is relative to project3106$file="$projectroot/$project/$file";3107}3108$mime= mimetype_guess_file($filename,$file);3109}3110$mime||= mimetype_guess_file($filename,'/etc/mime.types');3111return$mime;3112}31133114sub blob_mimetype {3115my$fd=shift;3116my$filename=shift;31173118if($filename) {3119my$mime= mimetype_guess($filename);3120$mimeandreturn$mime;3121}31223123# just in case3124return$default_blob_plain_mimetypeunless$fd;31253126if(-T $fd) {3127return'text/plain';3128}elsif(!$filename) {3129return'application/octet-stream';3130}elsif($filename=~m/\.png$/i) {3131return'image/png';3132}elsif($filename=~m/\.gif$/i) {3133return'image/gif';3134}elsif($filename=~m/\.jpe?g$/i) {3135return'image/jpeg';3136}else{3137return'application/octet-stream';3138}3139}31403141sub blob_contenttype {3142my($fd,$file_name,$type) =@_;31433144$type||= blob_mimetype($fd,$file_name);3145if($typeeq'text/plain'&&defined$default_text_plain_charset) {3146$type.="; charset=$default_text_plain_charset";3147}31483149return$type;3150}31513152## ======================================================================3153## functions printing HTML: header, footer, error page31543155sub git_header_html {3156my$status=shift||"200 OK";3157my$expires=shift;31583159my$title="$site_name";3160if(defined$project) {3161$title.=" - ". to_utf8($project);3162if(defined$action) {3163$title.="/$action";3164if(defined$file_name) {3165$title.=" - ". esc_path($file_name);3166if($actioneq"tree"&&$file_name!~ m|/$|) {3167$title.="/";3168}3169}3170}3171}3172my$content_type;3173# require explicit support from the UA if we are to send the page as3174# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3175# we have to do this because MSIE sometimes globs '*/*', pretending to3176# support xhtml+xml but choking when it gets what it asked for.3177if(defined$cgi->http('HTTP_ACCEPT') &&3178$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3179$cgi->Accept('application/xhtml+xml') !=0) {3180$content_type='application/xhtml+xml';3181}else{3182$content_type='text/html';3183}3184print$cgi->header(-type=>$content_type, -charset =>'utf-8',3185-status=>$status, -expires =>$expires);3186my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3187print<<EOF;3188<?xml version="1.0" encoding="utf-8"?>3189<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3190<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3191<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3192<!-- git core binaries version$git_version-->3193<head>3194<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3195<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3196<meta name="robots" content="index, nofollow"/>3197<title>$title</title>3198EOF3199# the stylesheet, favicon etc urls won't work correctly with path_info3200# unless we set the appropriate base URL3201if($ENV{'PATH_INFO'}) {3202print"<base href=\"".esc_url($base_url)."\"/>\n";3203}3204# print out each stylesheet that exist, providing backwards capability3205# for those people who defined $stylesheet in a config file3206if(defined$stylesheet) {3207print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3208}else{3209foreachmy$stylesheet(@stylesheets) {3210next unless$stylesheet;3211print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3212}3213}3214if(defined$project) {3215my%href_params= get_feed_info();3216if(!exists$href_params{'-title'}) {3217$href_params{'-title'} ='log';3218}32193220foreachmy$formatqw(RSS Atom){3221my$type=lc($format);3222my%link_attr= (3223'-rel'=>'alternate',3224'-title'=>"$project-$href_params{'-title'} -$formatfeed",3225'-type'=>"application/$type+xml"3226);32273228$href_params{'action'} =$type;3229$link_attr{'-href'} = href(%href_params);3230print"<link ".3231"rel=\"$link_attr{'-rel'}\"".3232"title=\"$link_attr{'-title'}\"".3233"href=\"$link_attr{'-href'}\"".3234"type=\"$link_attr{'-type'}\"".3235"/>\n";32363237$href_params{'extra_options'} ='--no-merges';3238$link_attr{'-href'} = href(%href_params);3239$link_attr{'-title'} .=' (no merges)';3240print"<link ".3241"rel=\"$link_attr{'-rel'}\"".3242"title=\"$link_attr{'-title'}\"".3243"href=\"$link_attr{'-href'}\"".3244"type=\"$link_attr{'-type'}\"".3245"/>\n";3246}32473248}else{3249printf('<link rel="alternate" title="%sprojects list" '.3250'href="%s" type="text/plain; charset=utf-8" />'."\n",3251$site_name, href(project=>undef, action=>"project_index"));3252printf('<link rel="alternate" title="%sprojects feeds" '.3253'href="%s" type="text/x-opml" />'."\n",3254$site_name, href(project=>undef, action=>"opml"));3255}3256if(defined$favicon) {3257printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3258}32593260print"</head>\n".3261"<body>\n";32623263if(defined$site_header&& -f $site_header) {3264 insert_file($site_header);3265}32663267print"<div class=\"page_header\">\n".3268$cgi->a({-href => esc_url($logo_url),3269-title =>$logo_label},3270qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3271print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3272if(defined$project) {3273print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3274if(defined$action) {3275print" /$action";3276}3277print"\n";3278}3279print"</div>\n";32803281my$have_search= gitweb_check_feature('search');3282if(defined$project&&$have_search) {3283if(!defined$searchtext) {3284$searchtext="";3285}3286my$search_hash;3287if(defined$hash_base) {3288$search_hash=$hash_base;3289}elsif(defined$hash) {3290$search_hash=$hash;3291}else{3292$search_hash="HEAD";3293}3294my$action=$my_uri;3295my$use_pathinfo= gitweb_check_feature('pathinfo');3296if($use_pathinfo) {3297$action.="/".esc_url($project);3298}3299print$cgi->startform(-method=>"get", -action =>$action) .3300"<div class=\"search\">\n".3301(!$use_pathinfo&&3302$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3303$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3304$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3305$cgi->popup_menu(-name =>'st', -default=>'commit',3306-values=> ['commit','grep','author','committer','pickaxe']) .3307$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3308" search:\n",3309$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3310"<span title=\"Extended regular expression\">".3311$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3312-checked =>$search_use_regexp) .3313"</span>".3314"</div>".3315$cgi->end_form() ."\n";3316}3317}33183319sub git_footer_html {3320my$feed_class='rss_logo';33213322print"<div class=\"page_footer\">\n";3323if(defined$project) {3324my$descr= git_get_project_description($project);3325if(defined$descr) {3326print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3327}33283329my%href_params= get_feed_info();3330if(!%href_params) {3331$feed_class.=' generic';3332}3333$href_params{'-title'} ||='log';33343335foreachmy$formatqw(RSS Atom){3336$href_params{'action'} =lc($format);3337print$cgi->a({-href => href(%href_params),3338-title =>"$href_params{'-title'}$formatfeed",3339-class=>$feed_class},$format)."\n";3340}33413342}else{3343print$cgi->a({-href => href(project=>undef, action=>"opml"),3344-class=>$feed_class},"OPML") ." ";3345print$cgi->a({-href => href(project=>undef, action=>"project_index"),3346-class=>$feed_class},"TXT") ."\n";3347}3348print"</div>\n";# class="page_footer"33493350if(defined$t0&& gitweb_check_feature('timed')) {3351print"<div id=\"generating_info\">\n";3352print'This page took '.3353'<span id="generating_time" class="time_span">'.3354 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3355' seconds </span>'.3356' and '.3357'<span id="generating_cmd">'.3358$number_of_git_cmds.3359'</span> git commands '.3360" to generate.\n";3361print"</div>\n";# class="page_footer"3362}33633364if(defined$site_footer&& -f $site_footer) {3365 insert_file($site_footer);3366}33673368print qq!<script type="text/javascript" src="$javascript"></script>\n!;3369if(defined$action&&3370$actioneq'blame_incremental') {3371print qq!<script type="text/javascript">\n!.3372 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3373 qq!"!. href() .qq!");\n!.3374 qq!</script>\n!;3375}elsif(gitweb_check_feature('javascript-actions')) {3376print qq!<script type="text/javascript">\n!.3377 qq!window.onload = fixLinks;\n!.3378 qq!</script>\n!;3379}33803381print"</body>\n".3382"</html>";3383}33843385# die_error(<http_status_code>, <error_message>)3386# Example: die_error(404, 'Hash not found')3387# By convention, use the following status codes (as defined in RFC 2616):3388# 400: Invalid or missing CGI parameters, or3389# requested object exists but has wrong type.3390# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3391# this server or project.3392# 404: Requested object/revision/project doesn't exist.3393# 500: The server isn't configured properly, or3394# an internal error occurred (e.g. failed assertions caused by bugs), or3395# an unknown error occurred (e.g. the git binary died unexpectedly).3396# 503: The server is currently unavailable (because it is overloaded,3397# or down for maintenance). Generally, this is a temporary state.3398sub die_error {3399my$status=shift||500;3400my$error=shift||"Internal server error";3401my$extra=shift;34023403my%http_responses= (3404400=>'400 Bad Request',3405403=>'403 Forbidden',3406404=>'404 Not Found',3407500=>'500 Internal Server Error',3408503=>'503 Service Unavailable',3409);3410 git_header_html($http_responses{$status});3411print<<EOF;3412<div class="page_body">3413<br /><br />3414$status-$error3415<br />3416EOF3417if(defined$extra) {3418print"<hr />\n".3419"$extra\n";3420}3421print"</div>\n";34223423 git_footer_html();3424exit;3425}34263427## ----------------------------------------------------------------------3428## functions printing or outputting HTML: navigation34293430sub git_print_page_nav {3431my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3432$extra=''if!defined$extra;# pager or formats34333434my@navs=qw(summary shortlog log commit commitdiff tree);3435if($suppress) {3436@navs=grep{$_ne$suppress}@navs;3437}34383439my%arg=map{$_=> {action=>$_} }@navs;3440if(defined$head) {3441for(qw(commit commitdiff)) {3442$arg{$_}{'hash'} =$head;3443}3444if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3445for(qw(shortlog log)) {3446$arg{$_}{'hash'} =$head;3447}3448}3449}34503451$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3452$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;34533454my@actions= gitweb_get_feature('actions');3455my%repl= (3456'%'=>'%',3457'n'=>$project,# project name3458'f'=>$git_dir,# project path within filesystem3459'h'=>$treehead||'',# current hash ('h' parameter)3460'b'=>$treebase||'',# hash base ('hb' parameter)3461);3462while(@actions) {3463my($label,$link,$pos) =splice(@actions,0,3);3464# insert3465@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3466# munch munch3467$link=~s/%([%nfhb])/$repl{$1}/g;3468$arg{$label}{'_href'} =$link;3469}34703471print"<div class=\"page_nav\">\n".3472(join" | ",3473map{$_eq$current?3474$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3475}@navs);3476print"<br/>\n$extra<br/>\n".3477"</div>\n";3478}34793480sub format_paging_nav {3481my($action,$page,$has_next_link) =@_;3482my$paging_nav;348334843485if($page>0) {3486$paging_nav.=3487$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3488" ⋅ ".3489$cgi->a({-href => href(-replay=>1, page=>$page-1),3490-accesskey =>"p", -title =>"Alt-p"},"prev");3491}else{3492$paging_nav.="first ⋅ prev";3493}34943495if($has_next_link) {3496$paging_nav.=" ⋅ ".3497$cgi->a({-href => href(-replay=>1, page=>$page+1),3498-accesskey =>"n", -title =>"Alt-n"},"next");3499}else{3500$paging_nav.=" ⋅ next";3501}35023503return$paging_nav;3504}35053506## ......................................................................3507## functions printing or outputting HTML: div35083509sub git_print_header_div {3510my($action,$title,$hash,$hash_base) =@_;3511my%args= ();35123513$args{'action'} =$action;3514$args{'hash'} =$hashif$hash;3515$args{'hash_base'} =$hash_baseif$hash_base;35163517print"<div class=\"header\">\n".3518$cgi->a({-href => href(%args), -class=>"title"},3519$title?$title:$action) .3520"\n</div>\n";3521}35223523sub print_local_time {3524print format_local_time(@_);3525}35263527sub format_local_time {3528my$localtime='';3529my%date=@_;3530if($date{'hour_local'} <6) {3531$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3532$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3533}else{3534$localtime.=sprintf(" (%02d:%02d%s)",3535$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3536}35373538return$localtime;3539}35403541# Outputs the author name and date in long form3542sub git_print_authorship {3543my$co=shift;3544my%opts=@_;3545my$tag=$opts{-tag} ||'div';3546my$author=$co->{'author_name'};35473548my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3549print"<$tagclass=\"author_date\">".3550 format_search_author($author,"author", esc_html($author)) .3551" [$ad{'rfc2822'}";3552 print_local_time(%ad)if($opts{-localtime});3553print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3554."</$tag>\n";3555}35563557# Outputs table rows containing the full author or committer information,3558# in the format expected for 'commit' view (& similia).3559# Parameters are a commit hash reference, followed by the list of people3560# to output information for. If the list is empty it defalts to both3561# author and committer.3562sub git_print_authorship_rows {3563my$co=shift;3564# too bad we can't use @people = @_ || ('author', 'committer')3565my@people=@_;3566@people= ('author','committer')unless@people;3567foreachmy$who(@people) {3568my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3569print"<tr><td>$who</td><td>".3570 format_search_author($co->{"${who}_name"},$who,3571 esc_html($co->{"${who}_name"})) ." ".3572 format_search_author($co->{"${who}_email"},$who,3573 esc_html("<".$co->{"${who}_email"} .">")) .3574"</td><td rowspan=\"2\">".3575 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3576"</td></tr>\n".3577"<tr>".3578"<td></td><td>$wd{'rfc2822'}";3579 print_local_time(%wd);3580print"</td>".3581"</tr>\n";3582}3583}35843585sub git_print_page_path {3586my$name=shift;3587my$type=shift;3588my$hb=shift;358935903591print"<div class=\"page_path\">";3592print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3593-title =>'tree root'}, to_utf8("[$project]"));3594print" / ";3595if(defined$name) {3596my@dirname=split'/',$name;3597my$basename=pop@dirname;3598my$fullname='';35993600foreachmy$dir(@dirname) {3601$fullname.= ($fullname?'/':'') .$dir;3602print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3603 hash_base=>$hb),3604-title =>$fullname}, esc_path($dir));3605print" / ";3606}3607if(defined$type&&$typeeq'blob') {3608print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3609 hash_base=>$hb),3610-title =>$name}, esc_path($basename));3611}elsif(defined$type&&$typeeq'tree') {3612print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3613 hash_base=>$hb),3614-title =>$name}, esc_path($basename));3615print" / ";3616}else{3617print esc_path($basename);3618}3619}3620print"<br/></div>\n";3621}36223623sub git_print_log {3624my$log=shift;3625my%opts=@_;36263627if($opts{'-remove_title'}) {3628# remove title, i.e. first line of log3629shift@$log;3630}3631# remove leading empty lines3632while(defined$log->[0] &&$log->[0]eq"") {3633shift@$log;3634}36353636# print log3637my$signoff=0;3638my$empty=0;3639foreachmy$line(@$log) {3640if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3641$signoff=1;3642$empty=0;3643if(!$opts{'-remove_signoff'}) {3644print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3645next;3646}else{3647# remove signoff lines3648next;3649}3650}else{3651$signoff=0;3652}36533654# print only one empty line3655# do not print empty line after signoff3656if($lineeq"") {3657next if($empty||$signoff);3658$empty=1;3659}else{3660$empty=0;3661}36623663print format_log_line_html($line) ."<br/>\n";3664}36653666if($opts{'-final_empty_line'}) {3667# end with single empty line3668print"<br/>\n"unless$empty;3669}3670}36713672# return link target (what link points to)3673sub git_get_link_target {3674my$hash=shift;3675my$link_target;36763677# read link3678open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3679orreturn;3680{3681local$/=undef;3682$link_target= <$fd>;3683}3684close$fd3685orreturn;36863687return$link_target;3688}36893690# given link target, and the directory (basedir) the link is in,3691# return target of link relative to top directory (top tree);3692# return undef if it is not possible (including absolute links).3693sub normalize_link_target {3694my($link_target,$basedir) =@_;36953696# absolute symlinks (beginning with '/') cannot be normalized3697return if(substr($link_target,0,1)eq'/');36983699# normalize link target to path from top (root) tree (dir)3700my$path;3701if($basedir) {3702$path=$basedir.'/'.$link_target;3703}else{3704# we are in top (root) tree (dir)3705$path=$link_target;3706}37073708# remove //, /./, and /../3709my@path_parts;3710foreachmy$part(split('/',$path)) {3711# discard '.' and ''3712next if(!$part||$parteq'.');3713# handle '..'3714if($parteq'..') {3715if(@path_parts) {3716pop@path_parts;3717}else{3718# link leads outside repository (outside top dir)3719return;3720}3721}else{3722push@path_parts,$part;3723}3724}3725$path=join('/',@path_parts);37263727return$path;3728}37293730# print tree entry (row of git_tree), but without encompassing <tr> element3731sub git_print_tree_entry {3732my($t,$basedir,$hash_base,$have_blame) =@_;37333734my%base_key= ();3735$base_key{'hash_base'} =$hash_baseifdefined$hash_base;37363737# The format of a table row is: mode list link. Where mode is3738# the mode of the entry, list is the name of the entry, an href,3739# and link is the action links of the entry.37403741print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3742if(exists$t->{'size'}) {3743print"<td class=\"size\">$t->{'size'}</td>\n";3744}3745if($t->{'type'}eq"blob") {3746print"<td class=\"list\">".3747$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3748 file_name=>"$basedir$t->{'name'}",%base_key),3749-class=>"list"}, esc_path($t->{'name'}));3750if(S_ISLNK(oct$t->{'mode'})) {3751my$link_target= git_get_link_target($t->{'hash'});3752if($link_target) {3753my$norm_target= normalize_link_target($link_target,$basedir);3754if(defined$norm_target) {3755print" -> ".3756$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3757 file_name=>$norm_target),3758-title =>$norm_target}, esc_path($link_target));3759}else{3760print" -> ". esc_path($link_target);3761}3762}3763}3764print"</td>\n";3765print"<td class=\"link\">";3766print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3767 file_name=>"$basedir$t->{'name'}",%base_key)},3768"blob");3769if($have_blame) {3770print" | ".3771$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3772 file_name=>"$basedir$t->{'name'}",%base_key)},3773"blame");3774}3775if(defined$hash_base) {3776print" | ".3777$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3778 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3779"history");3780}3781print" | ".3782$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3783 file_name=>"$basedir$t->{'name'}")},3784"raw");3785print"</td>\n";37863787}elsif($t->{'type'}eq"tree") {3788print"<td class=\"list\">";3789print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3790 file_name=>"$basedir$t->{'name'}",3791%base_key)},3792 esc_path($t->{'name'}));3793print"</td>\n";3794print"<td class=\"link\">";3795print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3796 file_name=>"$basedir$t->{'name'}",3797%base_key)},3798"tree");3799if(defined$hash_base) {3800print" | ".3801$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3802 file_name=>"$basedir$t->{'name'}")},3803"history");3804}3805print"</td>\n";3806}else{3807# unknown object: we can only present history for it3808# (this includes 'commit' object, i.e. submodule support)3809print"<td class=\"list\">".3810 esc_path($t->{'name'}) .3811"</td>\n";3812print"<td class=\"link\">";3813if(defined$hash_base) {3814print$cgi->a({-href => href(action=>"history",3815 hash_base=>$hash_base,3816 file_name=>"$basedir$t->{'name'}")},3817"history");3818}3819print"</td>\n";3820}3821}38223823## ......................................................................3824## functions printing large fragments of HTML38253826# get pre-image filenames for merge (combined) diff3827sub fill_from_file_info {3828my($diff,@parents) =@_;38293830$diff->{'from_file'} = [ ];3831$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3832for(my$i=0;$i<$diff->{'nparents'};$i++) {3833if($diff->{'status'}[$i]eq'R'||3834$diff->{'status'}[$i]eq'C') {3835$diff->{'from_file'}[$i] =3836 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3837}3838}38393840return$diff;3841}38423843# is current raw difftree line of file deletion3844sub is_deleted {3845my$diffinfo=shift;38463847return$diffinfo->{'to_id'}eq('0' x 40);3848}38493850# does patch correspond to [previous] difftree raw line3851# $diffinfo - hashref of parsed raw diff format3852# $patchinfo - hashref of parsed patch diff format3853# (the same keys as in $diffinfo)3854sub is_patch_split {3855my($diffinfo,$patchinfo) =@_;38563857returndefined$diffinfo&&defined$patchinfo3858&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3859}386038613862sub git_difftree_body {3863my($difftree,$hash,@parents) =@_;3864my($parent) =$parents[0];3865my$have_blame= gitweb_check_feature('blame');3866print"<div class=\"list_head\">\n";3867if($#{$difftree} >10) {3868print(($#{$difftree} +1) ." files changed:\n");3869}3870print"</div>\n";38713872print"<table class=\"".3873(@parents>1?"combined ":"") .3874"diff_tree\">\n";38753876# header only for combined diff in 'commitdiff' view3877my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3878if($has_header) {3879# table header3880print"<thead><tr>\n".3881"<th></th><th></th>\n";# filename, patchN link3882for(my$i=0;$i<@parents;$i++) {3883my$par=$parents[$i];3884print"<th>".3885$cgi->a({-href => href(action=>"commitdiff",3886 hash=>$hash, hash_parent=>$par),3887-title =>'commitdiff to parent number '.3888($i+1) .': '.substr($par,0,7)},3889$i+1) .3890" </th>\n";3891}3892print"</tr></thead>\n<tbody>\n";3893}38943895my$alternate=1;3896my$patchno=0;3897foreachmy$line(@{$difftree}) {3898my$diff= parsed_difftree_line($line);38993900if($alternate) {3901print"<tr class=\"dark\">\n";3902}else{3903print"<tr class=\"light\">\n";3904}3905$alternate^=1;39063907if(exists$diff->{'nparents'}) {# combined diff39083909 fill_from_file_info($diff,@parents)3910unlessexists$diff->{'from_file'};39113912if(!is_deleted($diff)) {3913# file exists in the result (child) commit3914print"<td>".3915$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3916 file_name=>$diff->{'to_file'},3917 hash_base=>$hash),3918-class=>"list"}, esc_path($diff->{'to_file'})) .3919"</td>\n";3920}else{3921print"<td>".3922 esc_path($diff->{'to_file'}) .3923"</td>\n";3924}39253926if($actioneq'commitdiff') {3927# link to patch3928$patchno++;3929print"<td class=\"link\">".3930$cgi->a({-href =>"#patch$patchno"},"patch") .3931" | ".3932"</td>\n";3933}39343935my$has_history=0;3936my$not_deleted=0;3937for(my$i=0;$i<$diff->{'nparents'};$i++) {3938my$hash_parent=$parents[$i];3939my$from_hash=$diff->{'from_id'}[$i];3940my$from_path=$diff->{'from_file'}[$i];3941my$status=$diff->{'status'}[$i];39423943$has_history||= ($statusne'A');3944$not_deleted||= ($statusne'D');39453946if($statuseq'A') {3947print"<td class=\"link\"align=\"right\"> | </td>\n";3948}elsif($statuseq'D') {3949print"<td class=\"link\">".3950$cgi->a({-href => href(action=>"blob",3951 hash_base=>$hash,3952 hash=>$from_hash,3953 file_name=>$from_path)},3954"blob". ($i+1)) .3955" | </td>\n";3956}else{3957if($diff->{'to_id'}eq$from_hash) {3958print"<td class=\"link nochange\">";3959}else{3960print"<td class=\"link\">";3961}3962print$cgi->a({-href => href(action=>"blobdiff",3963 hash=>$diff->{'to_id'},3964 hash_parent=>$from_hash,3965 hash_base=>$hash,3966 hash_parent_base=>$hash_parent,3967 file_name=>$diff->{'to_file'},3968 file_parent=>$from_path)},3969"diff". ($i+1)) .3970" | </td>\n";3971}3972}39733974print"<td class=\"link\">";3975if($not_deleted) {3976print$cgi->a({-href => href(action=>"blob",3977 hash=>$diff->{'to_id'},3978 file_name=>$diff->{'to_file'},3979 hash_base=>$hash)},3980"blob");3981print" | "if($has_history);3982}3983if($has_history) {3984print$cgi->a({-href => href(action=>"history",3985 file_name=>$diff->{'to_file'},3986 hash_base=>$hash)},3987"history");3988}3989print"</td>\n";39903991print"</tr>\n";3992next;# instead of 'else' clause, to avoid extra indent3993}3994# else ordinary diff39953996my($to_mode_oct,$to_mode_str,$to_file_type);3997my($from_mode_oct,$from_mode_str,$from_file_type);3998if($diff->{'to_mode'}ne('0' x 6)) {3999$to_mode_oct=oct$diff->{'to_mode'};4000if(S_ISREG($to_mode_oct)) {# only for regular file4001$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4002}4003$to_file_type= file_type($diff->{'to_mode'});4004}4005if($diff->{'from_mode'}ne('0' x 6)) {4006$from_mode_oct=oct$diff->{'from_mode'};4007if(S_ISREG($to_mode_oct)) {# only for regular file4008$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4009}4010$from_file_type= file_type($diff->{'from_mode'});4011}40124013if($diff->{'status'}eq"A") {# created4014my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4015$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4016$mode_chng.="]</span>";4017print"<td>";4018print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4019 hash_base=>$hash, file_name=>$diff->{'file'}),4020-class=>"list"}, esc_path($diff->{'file'}));4021print"</td>\n";4022print"<td>$mode_chng</td>\n";4023print"<td class=\"link\">";4024if($actioneq'commitdiff') {4025# link to patch4026$patchno++;4027print$cgi->a({-href =>"#patch$patchno"},"patch");4028print" | ";4029}4030print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4031 hash_base=>$hash, file_name=>$diff->{'file'})},4032"blob");4033print"</td>\n";40344035}elsif($diff->{'status'}eq"D") {# deleted4036my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4037print"<td>";4038print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4039 hash_base=>$parent, file_name=>$diff->{'file'}),4040-class=>"list"}, esc_path($diff->{'file'}));4041print"</td>\n";4042print"<td>$mode_chng</td>\n";4043print"<td class=\"link\">";4044if($actioneq'commitdiff') {4045# link to patch4046$patchno++;4047print$cgi->a({-href =>"#patch$patchno"},"patch");4048print" | ";4049}4050print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4051 hash_base=>$parent, file_name=>$diff->{'file'})},4052"blob") ." | ";4053if($have_blame) {4054print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4055 file_name=>$diff->{'file'})},4056"blame") ." | ";4057}4058print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4059 file_name=>$diff->{'file'})},4060"history");4061print"</td>\n";40624063}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4064my$mode_chnge="";4065if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4066$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4067if($from_file_typene$to_file_type) {4068$mode_chnge.=" from$from_file_typeto$to_file_type";4069}4070if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4071if($from_mode_str&&$to_mode_str) {4072$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4073}elsif($to_mode_str) {4074$mode_chnge.=" mode:$to_mode_str";4075}4076}4077$mode_chnge.="]</span>\n";4078}4079print"<td>";4080print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4081 hash_base=>$hash, file_name=>$diff->{'file'}),4082-class=>"list"}, esc_path($diff->{'file'}));4083print"</td>\n";4084print"<td>$mode_chnge</td>\n";4085print"<td class=\"link\">";4086if($actioneq'commitdiff') {4087# link to patch4088$patchno++;4089print$cgi->a({-href =>"#patch$patchno"},"patch") .4090" | ";4091}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4092# "commit" view and modified file (not onlu mode changed)4093print$cgi->a({-href => href(action=>"blobdiff",4094 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4095 hash_base=>$hash, hash_parent_base=>$parent,4096 file_name=>$diff->{'file'})},4097"diff") .4098" | ";4099}4100print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4101 hash_base=>$hash, file_name=>$diff->{'file'})},4102"blob") ." | ";4103if($have_blame) {4104print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4105 file_name=>$diff->{'file'})},4106"blame") ." | ";4107}4108print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4109 file_name=>$diff->{'file'})},4110"history");4111print"</td>\n";41124113}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4114my%status_name= ('R'=>'moved','C'=>'copied');4115my$nstatus=$status_name{$diff->{'status'}};4116my$mode_chng="";4117if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4118# mode also for directories, so we cannot use $to_mode_str4119$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4120}4121print"<td>".4122$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4123 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4124-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4125"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4126$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4127 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4128-class=>"list"}, esc_path($diff->{'from_file'})) .4129" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4130"<td class=\"link\">";4131if($actioneq'commitdiff') {4132# link to patch4133$patchno++;4134print$cgi->a({-href =>"#patch$patchno"},"patch") .4135" | ";4136}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4137# "commit" view and modified file (not only pure rename or copy)4138print$cgi->a({-href => href(action=>"blobdiff",4139 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4140 hash_base=>$hash, hash_parent_base=>$parent,4141 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4142"diff") .4143" | ";4144}4145print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4146 hash_base=>$parent, file_name=>$diff->{'to_file'})},4147"blob") ." | ";4148if($have_blame) {4149print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4150 file_name=>$diff->{'to_file'})},4151"blame") ." | ";4152}4153print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4154 file_name=>$diff->{'to_file'})},4155"history");4156print"</td>\n";41574158}# we should not encounter Unmerged (U) or Unknown (X) status4159print"</tr>\n";4160}4161print"</tbody>"if$has_header;4162print"</table>\n";4163}41644165sub git_patchset_body {4166my($fd,$difftree,$hash,@hash_parents) =@_;4167my($hash_parent) =$hash_parents[0];41684169my$is_combined= (@hash_parents>1);4170my$patch_idx=0;4171my$patch_number=0;4172my$patch_line;4173my$diffinfo;4174my$to_name;4175my(%from,%to);41764177print"<div class=\"patchset\">\n";41784179# skip to first patch4180while($patch_line= <$fd>) {4181chomp$patch_line;41824183last if($patch_line=~m/^diff /);4184}41854186 PATCH:4187while($patch_line) {41884189# parse "git diff" header line4190if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4191# $1 is from_name, which we do not use4192$to_name= unquote($2);4193$to_name=~s!^b/!!;4194}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4195# $1 is 'cc' or 'combined', which we do not use4196$to_name= unquote($2);4197}else{4198$to_name=undef;4199}42004201# check if current patch belong to current raw line4202# and parse raw git-diff line if needed4203if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4204# this is continuation of a split patch4205print"<div class=\"patch cont\">\n";4206}else{4207# advance raw git-diff output if needed4208$patch_idx++ifdefined$diffinfo;42094210# read and prepare patch information4211$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42124213# compact combined diff output can have some patches skipped4214# find which patch (using pathname of result) we are at now;4215if($is_combined) {4216while($to_namene$diffinfo->{'to_file'}) {4217print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4218 format_diff_cc_simplified($diffinfo,@hash_parents) .4219"</div>\n";# class="patch"42204221$patch_idx++;4222$patch_number++;42234224last if$patch_idx>$#$difftree;4225$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4226}4227}42284229# modifies %from, %to hashes4230 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);42314232# this is first patch for raw difftree line with $patch_idx index4233# we index @$difftree array from 0, but number patches from 14234print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4235}42364237# git diff header4238#assert($patch_line =~ m/^diff /) if DEBUG;4239#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4240$patch_number++;4241# print "git diff" header4242print format_git_diff_header_line($patch_line,$diffinfo,4243 \%from, \%to);42444245# print extended diff header4246print"<div class=\"diff extended_header\">\n";4247 EXTENDED_HEADER:4248while($patch_line= <$fd>) {4249chomp$patch_line;42504251last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);42524253print format_extended_diff_header_line($patch_line,$diffinfo,4254 \%from, \%to);4255}4256print"</div>\n";# class="diff extended_header"42574258# from-file/to-file diff header4259if(!$patch_line) {4260print"</div>\n";# class="patch"4261last PATCH;4262}4263next PATCH if($patch_line=~m/^diff /);4264#assert($patch_line =~ m/^---/) if DEBUG;42654266my$last_patch_line=$patch_line;4267$patch_line= <$fd>;4268chomp$patch_line;4269#assert($patch_line =~ m/^\+\+\+/) if DEBUG;42704271print format_diff_from_to_header($last_patch_line,$patch_line,4272$diffinfo, \%from, \%to,4273@hash_parents);42744275# the patch itself4276 LINE:4277while($patch_line= <$fd>) {4278chomp$patch_line;42794280next PATCH if($patch_line=~m/^diff /);42814282print format_diff_line($patch_line, \%from, \%to);4283}42844285}continue{4286print"</div>\n";# class="patch"4287}42884289# for compact combined (--cc) format, with chunk and patch simpliciaction4290# patchset might be empty, but there might be unprocessed raw lines4291for(++$patch_idxif$patch_number>0;4292$patch_idx<@$difftree;4293++$patch_idx) {4294# read and prepare patch information4295$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42964297# generate anchor for "patch" links in difftree / whatchanged part4298print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4299 format_diff_cc_simplified($diffinfo,@hash_parents) .4300"</div>\n";# class="patch"43014302$patch_number++;4303}43044305if($patch_number==0) {4306if(@hash_parents>1) {4307print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4308}else{4309print"<div class=\"diff nodifferences\">No differences found</div>\n";4310}4311}43124313print"</div>\n";# class="patchset"4314}43154316# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43174318# fills project list info (age, description, owner, forks) for each4319# project in the list, removing invalid projects from returned list4320# NOTE: modifies $projlist, but does not remove entries from it4321sub fill_project_list_info {4322my($projlist,$check_forks) =@_;4323my@projects;43244325my$show_ctags= gitweb_check_feature('ctags');4326 PROJECT:4327foreachmy$pr(@$projlist) {4328my(@activity) = git_get_last_activity($pr->{'path'});4329unless(@activity) {4330next PROJECT;4331}4332($pr->{'age'},$pr->{'age_string'}) =@activity;4333if(!defined$pr->{'descr'}) {4334my$descr= git_get_project_description($pr->{'path'}) ||"";4335$descr= to_utf8($descr);4336$pr->{'descr_long'} =$descr;4337$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4338}4339if(!defined$pr->{'owner'}) {4340$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4341}4342if($check_forks) {4343my$pname=$pr->{'path'};4344if(($pname=~s/\.git$//) &&4345($pname!~/\/$/) &&4346(-d "$projectroot/$pname")) {4347$pr->{'forks'} ="-d$projectroot/$pname";4348}else{4349$pr->{'forks'} =0;4350}4351}4352$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4353push@projects,$pr;4354}43554356return@projects;4357}43584359# print 'sort by' <th> element, generating 'sort by $name' replay link4360# if that order is not selected4361sub print_sort_th {4362print format_sort_th(@_);4363}43644365sub format_sort_th {4366my($name,$order,$header) =@_;4367my$sort_th="";4368$header||=ucfirst($name);43694370if($ordereq$name) {4371$sort_th.="<th>$header</th>\n";4372}else{4373$sort_th.="<th>".4374$cgi->a({-href => href(-replay=>1, order=>$name),4375-class=>"header"},$header) .4376"</th>\n";4377}43784379return$sort_th;4380}43814382sub git_project_list_body {4383# actually uses global variable $project4384my($projlist,$order,$from,$to,$extra,$no_header) =@_;43854386my$check_forks= gitweb_check_feature('forks');4387my@projects= fill_project_list_info($projlist,$check_forks);43884389$order||=$default_projects_order;4390$from=0unlessdefined$from;4391$to=$#projectsif(!defined$to||$#projects<$to);43924393my%order_info= (4394 project => { key =>'path', type =>'str'},4395 descr => { key =>'descr_long', type =>'str'},4396 owner => { key =>'owner', type =>'str'},4397 age => { key =>'age', type =>'num'}4398);4399my$oi=$order_info{$order};4400if($oi->{'type'}eq'str') {4401@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4402}else{4403@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4404}44054406my$show_ctags= gitweb_check_feature('ctags');4407if($show_ctags) {4408my%ctags;4409foreachmy$p(@projects) {4410foreachmy$ct(keys%{$p->{'ctags'}}) {4411$ctags{$ct} +=$p->{'ctags'}->{$ct};4412}4413}4414my$cloud= git_populate_project_tagcloud(\%ctags);4415print git_show_project_tagcloud($cloud,64);4416}44174418print"<table class=\"project_list\">\n";4419unless($no_header) {4420print"<tr>\n";4421if($check_forks) {4422print"<th></th>\n";4423}4424 print_sort_th('project',$order,'Project');4425 print_sort_th('descr',$order,'Description');4426 print_sort_th('owner',$order,'Owner');4427 print_sort_th('age',$order,'Last Change');4428print"<th></th>\n".# for links4429"</tr>\n";4430}4431my$alternate=1;4432my$tagfilter=$cgi->param('by_tag');4433for(my$i=$from;$i<=$to;$i++) {4434my$pr=$projects[$i];44354436next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4437next if$searchtextand not$pr->{'path'} =~/$searchtext/4438and not$pr->{'descr_long'} =~/$searchtext/;4439# Weed out forks or non-matching entries of search4440if($check_forks) {4441my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4442$forkbase="^$forkbase"if$forkbase;4443next ifnot$searchtextand not$tagfilterand$show_ctags4444and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4445}44464447if($alternate) {4448print"<tr class=\"dark\">\n";4449}else{4450print"<tr class=\"light\">\n";4451}4452$alternate^=1;4453if($check_forks) {4454print"<td>";4455if($pr->{'forks'}) {4456print"<!--$pr->{'forks'} -->\n";4457print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4458}4459print"</td>\n";4460}4461print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4462-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4463"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4464-class=>"list", -title =>$pr->{'descr_long'}},4465 esc_html($pr->{'descr'})) ."</td>\n".4466"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4467print"<td class=\"". age_class($pr->{'age'}) ."\">".4468(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4469"<td class=\"link\">".4470$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4471$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4472$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4473$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4474($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4475"</td>\n".4476"</tr>\n";4477}4478if(defined$extra) {4479print"<tr>\n";4480if($check_forks) {4481print"<td></td>\n";4482}4483print"<td colspan=\"5\">$extra</td>\n".4484"</tr>\n";4485}4486print"</table>\n";4487}44884489sub git_log_body {4490# uses global variable $project4491my($commitlist,$from,$to,$refs,$extra) =@_;44924493$from=0unlessdefined$from;4494$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);44954496for(my$i=0;$i<=$to;$i++) {4497my%co= %{$commitlist->[$i]};4498next if!%co;4499my$commit=$co{'id'};4500my$ref= format_ref_marker($refs,$commit);4501my%ad= parse_date($co{'author_epoch'});4502 git_print_header_div('commit',4503"<span class=\"age\">$co{'age_string'}</span>".4504 esc_html($co{'title'}) .$ref,4505$commit);4506print"<div class=\"title_text\">\n".4507"<div class=\"log_link\">\n".4508$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4509" | ".4510$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4511" | ".4512$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4513"<br/>\n".4514"</div>\n";4515 git_print_authorship(\%co, -tag =>'span');4516print"<br/>\n</div>\n";45174518print"<div class=\"log_body\">\n";4519 git_print_log($co{'comment'}, -final_empty_line=>1);4520print"</div>\n";4521}4522if($extra) {4523print"<div class=\"page_nav\">\n";4524print"$extra\n";4525print"</div>\n";4526}4527}45284529sub git_shortlog_body {4530# uses global variable $project4531my($commitlist,$from,$to,$refs,$extra) =@_;45324533$from=0unlessdefined$from;4534$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45354536print"<table class=\"shortlog\">\n";4537my$alternate=1;4538for(my$i=$from;$i<=$to;$i++) {4539my%co= %{$commitlist->[$i]};4540my$commit=$co{'id'};4541my$ref= format_ref_marker($refs,$commit);4542if($alternate) {4543print"<tr class=\"dark\">\n";4544}else{4545print"<tr class=\"light\">\n";4546}4547$alternate^=1;4548# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4549print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4550 format_author_html('td', \%co,10) ."<td>";4551print format_subject_html($co{'title'},$co{'title_short'},4552 href(action=>"commit", hash=>$commit),$ref);4553print"</td>\n".4554"<td class=\"link\">".4555$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4556$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4557$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4558my$snapshot_links= format_snapshot_links($commit);4559if(defined$snapshot_links) {4560print" | ".$snapshot_links;4561}4562print"</td>\n".4563"</tr>\n";4564}4565if(defined$extra) {4566print"<tr>\n".4567"<td colspan=\"4\">$extra</td>\n".4568"</tr>\n";4569}4570print"</table>\n";4571}45724573sub git_history_body {4574# Warning: assumes constant type (blob or tree) during history4575my($commitlist,$from,$to,$refs,$extra,4576$file_name,$file_hash,$ftype) =@_;45774578$from=0unlessdefined$from;4579$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});45804581print"<table class=\"history\">\n";4582my$alternate=1;4583for(my$i=$from;$i<=$to;$i++) {4584my%co= %{$commitlist->[$i]};4585if(!%co) {4586next;4587}4588my$commit=$co{'id'};45894590my$ref= format_ref_marker($refs,$commit);45914592if($alternate) {4593print"<tr class=\"dark\">\n";4594}else{4595print"<tr class=\"light\">\n";4596}4597$alternate^=1;4598print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4599# shortlog: format_author_html('td', \%co, 10)4600 format_author_html('td', \%co,15,3) ."<td>";4601# originally git_history used chop_str($co{'title'}, 50)4602print format_subject_html($co{'title'},$co{'title_short'},4603 href(action=>"commit", hash=>$commit),$ref);4604print"</td>\n".4605"<td class=\"link\">".4606$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4607$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");46084609if($ftypeeq'blob') {4610my$blob_current=$file_hash;4611my$blob_parent= git_get_hash_by_path($commit,$file_name);4612if(defined$blob_current&&defined$blob_parent&&4613$blob_currentne$blob_parent) {4614print" | ".4615$cgi->a({-href => href(action=>"blobdiff",4616 hash=>$blob_current, hash_parent=>$blob_parent,4617 hash_base=>$hash_base, hash_parent_base=>$commit,4618 file_name=>$file_name)},4619"diff to current");4620}4621}4622print"</td>\n".4623"</tr>\n";4624}4625if(defined$extra) {4626print"<tr>\n".4627"<td colspan=\"4\">$extra</td>\n".4628"</tr>\n";4629}4630print"</table>\n";4631}46324633sub git_tags_body {4634# uses global variable $project4635my($taglist,$from,$to,$extra) =@_;4636$from=0unlessdefined$from;4637$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);46384639print"<table class=\"tags\">\n";4640my$alternate=1;4641for(my$i=$from;$i<=$to;$i++) {4642my$entry=$taglist->[$i];4643my%tag=%$entry;4644my$comment=$tag{'subject'};4645my$comment_short;4646if(defined$comment) {4647$comment_short= chop_str($comment,30,5);4648}4649if($alternate) {4650print"<tr class=\"dark\">\n";4651}else{4652print"<tr class=\"light\">\n";4653}4654$alternate^=1;4655if(defined$tag{'age'}) {4656print"<td><i>$tag{'age'}</i></td>\n";4657}else{4658print"<td></td>\n";4659}4660print"<td>".4661$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4662-class=>"list name"}, esc_html($tag{'name'})) .4663"</td>\n".4664"<td>";4665if(defined$comment) {4666print format_subject_html($comment,$comment_short,4667 href(action=>"tag", hash=>$tag{'id'}));4668}4669print"</td>\n".4670"<td class=\"selflink\">";4671if($tag{'type'}eq"tag") {4672print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4673}else{4674print" ";4675}4676print"</td>\n".4677"<td class=\"link\">"." | ".4678$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4679if($tag{'reftype'}eq"commit") {4680print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4681" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4682}elsif($tag{'reftype'}eq"blob") {4683print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4684}4685print"</td>\n".4686"</tr>";4687}4688if(defined$extra) {4689print"<tr>\n".4690"<td colspan=\"5\">$extra</td>\n".4691"</tr>\n";4692}4693print"</table>\n";4694}46954696sub git_heads_body {4697# uses global variable $project4698my($headlist,$head,$from,$to,$extra) =@_;4699$from=0unlessdefined$from;4700$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);47014702print"<table class=\"heads\">\n";4703my$alternate=1;4704for(my$i=$from;$i<=$to;$i++) {4705my$entry=$headlist->[$i];4706my%ref=%$entry;4707my$curr=$ref{'id'}eq$head;4708if($alternate) {4709print"<tr class=\"dark\">\n";4710}else{4711print"<tr class=\"light\">\n";4712}4713$alternate^=1;4714print"<td><i>$ref{'age'}</i></td>\n".4715($curr?"<td class=\"current_head\">":"<td>") .4716$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4717-class=>"list name"},esc_html($ref{'name'})) .4718"</td>\n".4719"<td class=\"link\">".4720$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4721$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4722$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4723"</td>\n".4724"</tr>";4725}4726if(defined$extra) {4727print"<tr>\n".4728"<td colspan=\"3\">$extra</td>\n".4729"</tr>\n";4730}4731print"</table>\n";4732}47334734sub git_search_grep_body {4735my($commitlist,$from,$to,$extra) =@_;4736$from=0unlessdefined$from;4737$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);47384739print"<table class=\"commit_search\">\n";4740my$alternate=1;4741for(my$i=$from;$i<=$to;$i++) {4742my%co= %{$commitlist->[$i]};4743if(!%co) {4744next;4745}4746my$commit=$co{'id'};4747if($alternate) {4748print"<tr class=\"dark\">\n";4749}else{4750print"<tr class=\"light\">\n";4751}4752$alternate^=1;4753print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4754 format_author_html('td', \%co,15,5) .4755"<td>".4756$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4757-class=>"list subject"},4758 chop_and_escape_str($co{'title'},50) ."<br/>");4759my$comment=$co{'comment'};4760foreachmy$line(@$comment) {4761if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4762my($lead,$match,$trail) = ($1,$2,$3);4763$match= chop_str($match,70,5,'center');4764my$contextlen=int((80-length($match))/2);4765$contextlen=30if($contextlen>30);4766$lead= chop_str($lead,$contextlen,10,'left');4767$trail= chop_str($trail,$contextlen,10,'right');47684769$lead= esc_html($lead);4770$match= esc_html($match);4771$trail= esc_html($trail);47724773print"$lead<span class=\"match\">$match</span>$trail<br />";4774}4775}4776print"</td>\n".4777"<td class=\"link\">".4778$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4779" | ".4780$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4781" | ".4782$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4783print"</td>\n".4784"</tr>\n";4785}4786if(defined$extra) {4787print"<tr>\n".4788"<td colspan=\"3\">$extra</td>\n".4789"</tr>\n";4790}4791print"</table>\n";4792}47934794## ======================================================================4795## ======================================================================4796## actions47974798sub git_project_list {4799my$order=$input_params{'order'};4800if(defined$order&&$order!~m/none|project|descr|owner|age/) {4801 die_error(400,"Unknown order parameter");4802}48034804my@list= git_get_projects_list();4805if(!@list) {4806 die_error(404,"No projects found");4807}48084809 git_header_html();4810if(defined$home_text&& -f $home_text) {4811print"<div class=\"index_include\">\n";4812 insert_file($home_text);4813print"</div>\n";4814}4815print$cgi->startform(-method=>"get") .4816"<p class=\"projsearch\">Search:\n".4817$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4818"</p>".4819$cgi->end_form() ."\n";4820 git_project_list_body(\@list,$order);4821 git_footer_html();4822}48234824sub git_forks {4825my$order=$input_params{'order'};4826if(defined$order&&$order!~m/none|project|descr|owner|age/) {4827 die_error(400,"Unknown order parameter");4828}48294830my@list= git_get_projects_list($project);4831if(!@list) {4832 die_error(404,"No forks found");4833}48344835 git_header_html();4836 git_print_page_nav('','');4837 git_print_header_div('summary',"$projectforks");4838 git_project_list_body(\@list,$order);4839 git_footer_html();4840}48414842sub git_project_index {4843my@projects= git_get_projects_list($project);48444845print$cgi->header(4846-type =>'text/plain',4847-charset =>'utf-8',4848-content_disposition =>'inline; filename="index.aux"');48494850foreachmy$pr(@projects) {4851if(!exists$pr->{'owner'}) {4852$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4853}48544855my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4856# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4857$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4858$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4859$path=~s/ /\+/g;4860$owner=~s/ /\+/g;48614862print"$path$owner\n";4863}4864}48654866sub git_summary {4867my$descr= git_get_project_description($project) ||"none";4868my%co= parse_commit("HEAD");4869my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4870my$head=$co{'id'};48714872my$owner= git_get_project_owner($project);48734874my$refs= git_get_references();4875# These get_*_list functions return one more to allow us to see if4876# there are more ...4877my@taglist= git_get_tags_list(16);4878my@headlist= git_get_heads_list(16);4879my@forklist;4880my$check_forks= gitweb_check_feature('forks');48814882if($check_forks) {4883@forklist= git_get_projects_list($project);4884}48854886 git_header_html();4887 git_print_page_nav('summary','',$head);48884889print"<div class=\"title\"> </div>\n";4890print"<table class=\"projects_list\">\n".4891"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4892"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4893if(defined$cd{'rfc2822'}) {4894print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4895}48964897# use per project git URL list in $projectroot/$project/cloneurl4898# or make project git URL from git base URL and project name4899my$url_tag="URL";4900my@url_list= git_get_project_url_list($project);4901@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4902foreachmy$git_url(@url_list) {4903next unless$git_url;4904print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4905$url_tag="";4906}49074908# Tag cloud4909my$show_ctags= gitweb_check_feature('ctags');4910if($show_ctags) {4911my$ctags= git_get_project_ctags($project);4912my$cloud= git_populate_project_tagcloud($ctags);4913print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4914print"</td>\n<td>"unless%$ctags;4915print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4916print"</td>\n<td>"if%$ctags;4917print git_show_project_tagcloud($cloud,48);4918print"</td></tr>";4919}49204921print"</table>\n";49224923# If XSS prevention is on, we don't include README.html.4924# TODO: Allow a readme in some safe format.4925if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4926print"<div class=\"title\">readme</div>\n".4927"<div class=\"readme\">\n";4928 insert_file("$projectroot/$project/README.html");4929print"\n</div>\n";# class="readme"4930}49314932# we need to request one more than 16 (0..15) to check if4933# those 16 are all4934my@commitlist=$head? parse_commits($head,17) : ();4935if(@commitlist) {4936 git_print_header_div('shortlog');4937 git_shortlog_body(\@commitlist,0,15,$refs,4938$#commitlist<=15?undef:4939$cgi->a({-href => href(action=>"shortlog")},"..."));4940}49414942if(@taglist) {4943 git_print_header_div('tags');4944 git_tags_body(\@taglist,0,15,4945$#taglist<=15?undef:4946$cgi->a({-href => href(action=>"tags")},"..."));4947}49484949if(@headlist) {4950 git_print_header_div('heads');4951 git_heads_body(\@headlist,$head,0,15,4952$#headlist<=15?undef:4953$cgi->a({-href => href(action=>"heads")},"..."));4954}49554956if(@forklist) {4957 git_print_header_div('forks');4958 git_project_list_body(\@forklist,'age',0,15,4959$#forklist<=15?undef:4960$cgi->a({-href => href(action=>"forks")},"..."),4961'no_header');4962}49634964 git_footer_html();4965}49664967sub git_tag {4968my$head= git_get_head_hash($project);4969 git_header_html();4970 git_print_page_nav('','',$head,undef,$head);4971my%tag= parse_tag($hash);49724973if(!%tag) {4974 die_error(404,"Unknown tag object");4975}49764977 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4978print"<div class=\"title_text\">\n".4979"<table class=\"object_header\">\n".4980"<tr>\n".4981"<td>object</td>\n".4982"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4983$tag{'object'}) ."</td>\n".4984"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4985$tag{'type'}) ."</td>\n".4986"</tr>\n";4987if(defined($tag{'author'})) {4988 git_print_authorship_rows(\%tag,'author');4989}4990print"</table>\n\n".4991"</div>\n";4992print"<div class=\"page_body\">";4993my$comment=$tag{'comment'};4994foreachmy$line(@$comment) {4995chomp$line;4996print esc_html($line, -nbsp=>1) ."<br/>\n";4997}4998print"</div>\n";4999 git_footer_html();5000}50015002sub git_blame_common {5003my$format=shift||'porcelain';5004if($formateq'porcelain'&&$cgi->param('js')) {5005$format='incremental';5006$action='blame_incremental';# for page title etc5007}50085009# permissions5010 gitweb_check_feature('blame')5011or die_error(403,"Blame view not allowed");50125013# error checking5014 die_error(400,"No file name given")unless$file_name;5015$hash_base||= git_get_head_hash($project);5016 die_error(404,"Couldn't find base commit")unless$hash_base;5017my%co= parse_commit($hash_base)5018or die_error(404,"Commit not found");5019my$ftype="blob";5020if(!defined$hash) {5021$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5022or die_error(404,"Error looking up file");5023}else{5024$ftype= git_get_type($hash);5025if($ftype!~"blob") {5026 die_error(400,"Object is not a blob");5027}5028}50295030my$fd;5031if($formateq'incremental') {5032# get file contents (as base)5033open$fd,"-|", git_cmd(),'cat-file','blob',$hash5034or die_error(500,"Open git-cat-file failed");5035}elsif($formateq'data') {5036# run git-blame --incremental5037open$fd,"-|", git_cmd(),"blame","--incremental",5038$hash_base,"--",$file_name5039or die_error(500,"Open git-blame --incremental failed");5040}else{5041# run git-blame --porcelain5042open$fd,"-|", git_cmd(),"blame",'-p',5043$hash_base,'--',$file_name5044or die_error(500,"Open git-blame --porcelain failed");5045}50465047# incremental blame data returns early5048if($formateq'data') {5049print$cgi->header(5050-type=>"text/plain", -charset =>"utf-8",5051-status=>"200 OK");5052local$| =1;# output autoflush5053printwhile<$fd>;5054close$fd5055or print"ERROR$!\n";50565057print'END';5058if(defined$t0&& gitweb_check_feature('timed')) {5059print' '.5060 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5061' '.$number_of_git_cmds;5062}5063print"\n";50645065return;5066}50675068# page header5069 git_header_html();5070my$formats_nav=5071$cgi->a({-href => href(action=>"blob", -replay=>1)},5072"blob") .5073" | ";5074if($formateq'incremental') {5075$formats_nav.=5076$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5077"blame") ." (non-incremental)";5078}else{5079$formats_nav.=5080$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5081"blame") ." (incremental)";5082}5083$formats_nav.=5084" | ".5085$cgi->a({-href => href(action=>"history", -replay=>1)},5086"history") .5087" | ".5088$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5089"HEAD");5090 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5091 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5092 git_print_page_path($file_name,$ftype,$hash_base);50935094# page body5095if($formateq'incremental') {5096print"<noscript>\n<div class=\"error\"><center><b>\n".5097"This page requires JavaScript to run.\nUse ".5098$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5099'this page').5100" instead.\n".5101"</b></center></div>\n</noscript>\n";51025103print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5104}51055106print qq!<div class="page_body">\n!;5107print qq!<div id="progress_info">.../ ...</div>\n!5108if($formateq'incremental');5109print qq!<table id="blame_table"class="blame" width="100%">\n!.5110#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5111 qq!<thead>\n!.5112 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5113 qq!</thead>\n!.5114 qq!<tbody>\n!;51155116my@rev_color=qw(light dark);5117my$num_colors=scalar(@rev_color);5118my$current_color=0;51195120if($formateq'incremental') {5121my$color_class=$rev_color[$current_color];51225123#contents of a file5124my$linenr=0;5125 LINE:5126while(my$line= <$fd>) {5127chomp$line;5128$linenr++;51295130print qq!<tr id="l$linenr"class="$color_class">!.5131 qq!<td class="sha1"><a href=""> </a></td>!.5132 qq!<td class="linenr">!.5133 qq!<a class="linenr" href="">$linenr</a></td>!;5134print qq!<td class="pre">! . esc_html($line) ."</td>\n";5135print qq!</tr>\n!;5136}51375138}else{# porcelain, i.e. ordinary blame5139my%metainfo= ();# saves information about commits51405141# blame data5142 LINE:5143while(my$line= <$fd>) {5144chomp$line;5145# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5146# no <lines in group> for subsequent lines in group of lines5147my($full_rev,$orig_lineno,$lineno,$group_size) =5148($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5149if(!exists$metainfo{$full_rev}) {5150$metainfo{$full_rev} = {'nprevious'=>0};5151}5152my$meta=$metainfo{$full_rev};5153my$data;5154while($data= <$fd>) {5155chomp$data;5156last if($data=~s/^\t//);# contents of line5157if($data=~/^(\S+)(?: (.*))?$/) {5158$meta->{$1} =$2unlessexists$meta->{$1};5159}5160if($data=~/^previous /) {5161$meta->{'nprevious'}++;5162}5163}5164my$short_rev=substr($full_rev,0,8);5165my$author=$meta->{'author'};5166my%date=5167 parse_date($meta->{'author-time'},$meta->{'author-tz'});5168my$date=$date{'iso-tz'};5169if($group_size) {5170$current_color= ($current_color+1) %$num_colors;5171}5172my$tr_class=$rev_color[$current_color];5173$tr_class.=' boundary'if(exists$meta->{'boundary'});5174$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5175$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5176print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5177if($group_size) {5178print"<td class=\"sha1\"";5179print" title=\"". esc_html($author) .",$date\"";5180print" rowspan=\"$group_size\""if($group_size>1);5181print">";5182print$cgi->a({-href => href(action=>"commit",5183 hash=>$full_rev,5184 file_name=>$file_name)},5185 esc_html($short_rev));5186if($group_size>=2) {5187my@author_initials= ($author=~/\b([[:upper:]])\B/g);5188if(@author_initials) {5189print"<br />".5190 esc_html(join('',@author_initials));5191# or join('.', ...)5192}5193}5194print"</td>\n";5195}5196# 'previous' <sha1 of parent commit> <filename at commit>5197if(exists$meta->{'previous'} &&5198$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5199$meta->{'parent'} =$1;5200$meta->{'file_parent'} = unquote($2);5201}5202my$linenr_commit=5203exists($meta->{'parent'}) ?5204$meta->{'parent'} :$full_rev;5205my$linenr_filename=5206exists($meta->{'file_parent'}) ?5207$meta->{'file_parent'} : unquote($meta->{'filename'});5208my$blamed= href(action =>'blame',5209 file_name =>$linenr_filename,5210 hash_base =>$linenr_commit);5211print"<td class=\"linenr\">";5212print$cgi->a({ -href =>"$blamed#l$orig_lineno",5213-class=>"linenr"},5214 esc_html($lineno));5215print"</td>";5216print"<td class=\"pre\">". esc_html($data) ."</td>\n";5217print"</tr>\n";5218}# end while52195220}52215222# footer5223print"</tbody>\n".5224"</table>\n";# class="blame"5225print"</div>\n";# class="blame_body"5226close$fd5227or print"Reading blob failed\n";52285229 git_footer_html();5230}52315232sub git_blame {5233 git_blame_common();5234}52355236sub git_blame_incremental {5237 git_blame_common('incremental');5238}52395240sub git_blame_data {5241 git_blame_common('data');5242}52435244sub git_tags {5245my$head= git_get_head_hash($project);5246 git_header_html();5247 git_print_page_nav('','',$head,undef,$head);5248 git_print_header_div('summary',$project);52495250my@tagslist= git_get_tags_list();5251if(@tagslist) {5252 git_tags_body(\@tagslist);5253}5254 git_footer_html();5255}52565257sub git_heads {5258my$head= git_get_head_hash($project);5259 git_header_html();5260 git_print_page_nav('','',$head,undef,$head);5261 git_print_header_div('summary',$project);52625263my@headslist= git_get_heads_list();5264if(@headslist) {5265 git_heads_body(\@headslist,$head);5266}5267 git_footer_html();5268}52695270sub git_blob_plain {5271my$type=shift;5272my$expires;52735274if(!defined$hash) {5275if(defined$file_name) {5276my$base=$hash_base|| git_get_head_hash($project);5277$hash= git_get_hash_by_path($base,$file_name,"blob")5278or die_error(404,"Cannot find file");5279}else{5280 die_error(400,"No file name defined");5281}5282}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5283# blobs defined by non-textual hash id's can be cached5284$expires="+1d";5285}52865287open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5288or die_error(500,"Open git-cat-file blob '$hash' failed");52895290# content-type (can include charset)5291$type= blob_contenttype($fd,$file_name,$type);52925293# "save as" filename, even when no $file_name is given5294my$save_as="$hash";5295if(defined$file_name) {5296$save_as=$file_name;5297}elsif($type=~m/^text\//) {5298$save_as.='.txt';5299}53005301# With XSS prevention on, blobs of all types except a few known safe5302# ones are served with "Content-Disposition: attachment" to make sure5303# they don't run in our security domain. For certain image types,5304# blob view writes an <img> tag referring to blob_plain view, and we5305# want to be sure not to break that by serving the image as an5306# attachment (though Firefox 3 doesn't seem to care).5307my$sandbox=$prevent_xss&&5308$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;53095310print$cgi->header(5311-type =>$type,5312-expires =>$expires,5313-content_disposition =>5314($sandbox?'attachment':'inline')5315.'; filename="'.$save_as.'"');5316local$/=undef;5317binmode STDOUT,':raw';5318print<$fd>;5319binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5320close$fd;5321}53225323sub git_blob {5324my$expires;53255326if(!defined$hash) {5327if(defined$file_name) {5328my$base=$hash_base|| git_get_head_hash($project);5329$hash= git_get_hash_by_path($base,$file_name,"blob")5330or die_error(404,"Cannot find file");5331}else{5332 die_error(400,"No file name defined");5333}5334}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5335# blobs defined by non-textual hash id's can be cached5336$expires="+1d";5337}53385339my$have_blame= gitweb_check_feature('blame');5340open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5341or die_error(500,"Couldn't cat$file_name,$hash");5342my$mimetype= blob_mimetype($fd,$file_name);5343if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5344close$fd;5345return git_blob_plain($mimetype);5346}5347# we can have blame only for text/* mimetype5348$have_blame&&= ($mimetype=~m!^text/!);53495350 git_header_html(undef,$expires);5351my$formats_nav='';5352if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5353if(defined$file_name) {5354if($have_blame) {5355$formats_nav.=5356$cgi->a({-href => href(action=>"blame", -replay=>1)},5357"blame") .5358" | ";5359}5360$formats_nav.=5361$cgi->a({-href => href(action=>"history", -replay=>1)},5362"history") .5363" | ".5364$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5365"raw") .5366" | ".5367$cgi->a({-href => href(action=>"blob",5368 hash_base=>"HEAD", file_name=>$file_name)},5369"HEAD");5370}else{5371$formats_nav.=5372$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5373"raw");5374}5375 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5376 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5377}else{5378print"<div class=\"page_nav\">\n".5379"<br/><br/></div>\n".5380"<div class=\"title\">$hash</div>\n";5381}5382 git_print_page_path($file_name,"blob",$hash_base);5383print"<div class=\"page_body\">\n";5384if($mimetype=~m!^image/!) {5385print qq!<img type="$mimetype"!;5386if($file_name) {5387print qq! alt="$file_name" title="$file_name"!;5388}5389print qq! src="! .5390 href(action=>"blob_plain", hash=>$hash,5391 hash_base=>$hash_base, file_name=>$file_name) .5392 qq!"/>\n!;5393}else{5394my$nr;5395while(my$line= <$fd>) {5396chomp$line;5397$nr++;5398$line= untabify($line);5399printf"<div class=\"pre\"><a id=\"l%i\"href=\"". href(-replay =>1)5400."#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5401$nr,$nr,$nr, esc_html($line, -nbsp=>1);5402}5403}5404close$fd5405or print"Reading blob failed.\n";5406print"</div>";5407 git_footer_html();5408}54095410sub git_tree {5411if(!defined$hash_base) {5412$hash_base="HEAD";5413}5414if(!defined$hash) {5415if(defined$file_name) {5416$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5417}else{5418$hash=$hash_base;5419}5420}5421 die_error(404,"No such tree")unlessdefined($hash);54225423my$show_sizes= gitweb_check_feature('show-sizes');5424my$have_blame= gitweb_check_feature('blame');54255426my@entries= ();5427{5428local$/="\0";5429open my$fd,"-|", git_cmd(),"ls-tree",'-z',5430($show_sizes?'-l': ()),@extra_options,$hash5431or die_error(500,"Open git-ls-tree failed");5432@entries=map{chomp;$_} <$fd>;5433close$fd5434or die_error(404,"Reading tree failed");5435}54365437my$refs= git_get_references();5438my$ref= format_ref_marker($refs,$hash_base);5439 git_header_html();5440my$basedir='';5441if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5442my@views_nav= ();5443if(defined$file_name) {5444push@views_nav,5445$cgi->a({-href => href(action=>"history", -replay=>1)},5446"history"),5447$cgi->a({-href => href(action=>"tree",5448 hash_base=>"HEAD", file_name=>$file_name)},5449"HEAD"),5450}5451my$snapshot_links= format_snapshot_links($hash);5452if(defined$snapshot_links) {5453# FIXME: Should be available when we have no hash base as well.5454push@views_nav,$snapshot_links;5455}5456 git_print_page_nav('tree','',$hash_base,undef,undef,5457join(' | ',@views_nav));5458 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5459}else{5460undef$hash_base;5461print"<div class=\"page_nav\">\n";5462print"<br/><br/></div>\n";5463print"<div class=\"title\">$hash</div>\n";5464}5465if(defined$file_name) {5466$basedir=$file_name;5467if($basedirne''&&substr($basedir, -1)ne'/') {5468$basedir.='/';5469}5470 git_print_page_path($file_name,'tree',$hash_base);5471}5472print"<div class=\"page_body\">\n";5473print"<table class=\"tree\">\n";5474my$alternate=1;5475# '..' (top directory) link if possible5476if(defined$hash_base&&5477defined$file_name&&$file_name=~m![^/]+$!) {5478if($alternate) {5479print"<tr class=\"dark\">\n";5480}else{5481print"<tr class=\"light\">\n";5482}5483$alternate^=1;54845485my$up=$file_name;5486$up=~s!/?[^/]+$!!;5487undef$upunless$up;5488# based on git_print_tree_entry5489print'<td class="mode">'. mode_str('040000') ."</td>\n";5490print'<td class="size"> </td>'."\n"if$show_sizes;5491print'<td class="list">';5492print$cgi->a({-href => href(action=>"tree",5493 hash_base=>$hash_base,5494 file_name=>$up)},5495"..");5496print"</td>\n";5497print"<td class=\"link\"></td>\n";54985499print"</tr>\n";5500}5501foreachmy$line(@entries) {5502my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);55035504if($alternate) {5505print"<tr class=\"dark\">\n";5506}else{5507print"<tr class=\"light\">\n";5508}5509$alternate^=1;55105511 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);55125513print"</tr>\n";5514}5515print"</table>\n".5516"</div>";5517 git_footer_html();5518}55195520sub snapshot_name {5521my($project,$hash) =@_;55225523# path/to/project.git -> project5524# path/to/project/.git -> project5525my$name= to_utf8($project);5526$name=~ s,([^/])/*\.git$,$1,;5527$name= basename($name);5528# sanitize name5529$name=~s/[[:cntrl:]]/?/g;55305531my$ver=$hash;5532if($hash=~/^[0-9a-fA-F]+$/) {5533# shorten SHA-1 hash5534my$full_hash= git_get_full_hash($project,$hash);5535if($full_hash=~/^$hash/&&length($hash) >7) {5536$ver= git_get_short_hash($project,$hash);5537}5538}elsif($hash=~m!^refs/tags/(.*)$!) {5539# tags don't need shortened SHA-1 hash5540$ver=$1;5541}else{5542# branches and other need shortened SHA-1 hash5543if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5544$ver=$1;5545}5546$ver.='-'. git_get_short_hash($project,$hash);5547}5548# in case of hierarchical branch names5549$ver=~s!/!.!g;55505551# name = project-version_string5552$name="$name-$ver";55535554returnwantarray? ($name,$name) :$name;5555}55565557sub git_snapshot {5558my$format=$input_params{'snapshot_format'};5559if(!@snapshot_fmts) {5560 die_error(403,"Snapshots not allowed");5561}5562# default to first supported snapshot format5563$format||=$snapshot_fmts[0];5564if($format!~m/^[a-z0-9]+$/) {5565 die_error(400,"Invalid snapshot format parameter");5566}elsif(!exists($known_snapshot_formats{$format})) {5567 die_error(400,"Unknown snapshot format");5568}elsif($known_snapshot_formats{$format}{'disabled'}) {5569 die_error(403,"Snapshot format not allowed");5570}elsif(!grep($_eq$format,@snapshot_fmts)) {5571 die_error(403,"Unsupported snapshot format");5572}55735574my$type= git_get_type("$hash^{}");5575if(!$type) {5576 die_error(404,'Object does not exist');5577}elsif($typeeq'blob') {5578 die_error(400,'Object is not a tree-ish');5579}55805581my($name,$prefix) = snapshot_name($project,$hash);5582my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5583my$cmd= quote_command(5584 git_cmd(),'archive',5585"--format=$known_snapshot_formats{$format}{'format'}",5586"--prefix=$prefix/",$hash);5587if(exists$known_snapshot_formats{$format}{'compressor'}) {5588$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5589}55905591$filename=~s/(["\\])/\\$1/g;5592print$cgi->header(5593-type =>$known_snapshot_formats{$format}{'type'},5594-content_disposition =>'inline; filename="'.$filename.'"',5595-status =>'200 OK');55965597open my$fd,"-|",$cmd5598or die_error(500,"Execute git-archive failed");5599binmode STDOUT,':raw';5600print<$fd>;5601binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5602close$fd;5603}56045605sub git_log_generic {5606my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;56075608my$head= git_get_head_hash($project);5609if(!defined$base) {5610$base=$head;5611}5612if(!defined$page) {5613$page=0;5614}5615my$refs= git_get_references();56165617my$commit_hash=$base;5618if(defined$parent) {5619$commit_hash="$parent..$base";5620}5621my@commitlist=5622 parse_commits($commit_hash,101, (100*$page),5623defined$file_name? ($file_name,"--full-history") : ());56245625my$ftype;5626if(!defined$file_hash&&defined$file_name) {5627# some commits could have deleted file in question,5628# and not have it in tree, but one of them has to have it5629for(my$i=0;$i<@commitlist;$i++) {5630$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5631last ifdefined$file_hash;5632}5633}5634if(defined$file_hash) {5635$ftype= git_get_type($file_hash);5636}5637if(defined$file_name&& !defined$ftype) {5638 die_error(500,"Unknown type of object");5639}5640my%co;5641if(defined$file_name) {5642%co= parse_commit($base)5643or die_error(404,"Unknown commit object");5644}564556465647my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5648my$next_link='';5649if($#commitlist>=100) {5650$next_link=5651$cgi->a({-href => href(-replay=>1, page=>$page+1),5652-accesskey =>"n", -title =>"Alt-n"},"next");5653}5654my$patch_max= gitweb_get_feature('patches');5655if($patch_max&& !defined$file_name) {5656if($patch_max<0||@commitlist<=$patch_max) {5657$paging_nav.=" ⋅ ".5658$cgi->a({-href => href(action=>"patches", -replay=>1)},5659"patches");5660}5661}56625663 git_header_html();5664 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5665if(defined$file_name) {5666 git_print_header_div('commit', esc_html($co{'title'}),$base);5667}else{5668 git_print_header_div('summary',$project)5669}5670 git_print_page_path($file_name,$ftype,$hash_base)5671if(defined$file_name);56725673$body_subr->(\@commitlist,0,99,$refs,$next_link,5674$file_name,$file_hash,$ftype);56755676 git_footer_html();5677}56785679sub git_log {5680 git_log_generic('log', \&git_log_body,5681$hash,$hash_parent);5682}56835684sub git_commit {5685$hash||=$hash_base||"HEAD";5686my%co= parse_commit($hash)5687or die_error(404,"Unknown commit object");56885689my$parent=$co{'parent'};5690my$parents=$co{'parents'};# listref56915692# we need to prepare $formats_nav before any parameter munging5693my$formats_nav;5694if(!defined$parent) {5695# --root commitdiff5696$formats_nav.='(initial)';5697}elsif(@$parents==1) {5698# single parent commit5699$formats_nav.=5700'(parent: '.5701$cgi->a({-href => href(action=>"commit",5702 hash=>$parent)},5703 esc_html(substr($parent,0,7))) .5704')';5705}else{5706# merge commit5707$formats_nav.=5708'(merge: '.5709join(' ',map{5710$cgi->a({-href => href(action=>"commit",5711 hash=>$_)},5712 esc_html(substr($_,0,7)));5713}@$parents) .5714')';5715}5716if(gitweb_check_feature('patches') &&@$parents<=1) {5717$formats_nav.=" | ".5718$cgi->a({-href => href(action=>"patch", -replay=>1)},5719"patch");5720}57215722if(!defined$parent) {5723$parent="--root";5724}5725my@difftree;5726open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5727@diff_opts,5728(@$parents<=1?$parent:'-c'),5729$hash,"--"5730or die_error(500,"Open git-diff-tree failed");5731@difftree=map{chomp;$_} <$fd>;5732close$fdor die_error(404,"Reading git-diff-tree failed");57335734# non-textual hash id's can be cached5735my$expires;5736if($hash=~m/^[0-9a-fA-F]{40}$/) {5737$expires="+1d";5738}5739my$refs= git_get_references();5740my$ref= format_ref_marker($refs,$co{'id'});57415742 git_header_html(undef,$expires);5743 git_print_page_nav('commit','',5744$hash,$co{'tree'},$hash,5745$formats_nav);57465747if(defined$co{'parent'}) {5748 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5749}else{5750 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5751}5752print"<div class=\"title_text\">\n".5753"<table class=\"object_header\">\n";5754 git_print_authorship_rows(\%co);5755print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5756print"<tr>".5757"<td>tree</td>".5758"<td class=\"sha1\">".5759$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5760class=>"list"},$co{'tree'}) .5761"</td>".5762"<td class=\"link\">".5763$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5764"tree");5765my$snapshot_links= format_snapshot_links($hash);5766if(defined$snapshot_links) {5767print" | ".$snapshot_links;5768}5769print"</td>".5770"</tr>\n";57715772foreachmy$par(@$parents) {5773print"<tr>".5774"<td>parent</td>".5775"<td class=\"sha1\">".5776$cgi->a({-href => href(action=>"commit", hash=>$par),5777class=>"list"},$par) .5778"</td>".5779"<td class=\"link\">".5780$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5781" | ".5782$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5783"</td>".5784"</tr>\n";5785}5786print"</table>".5787"</div>\n";57885789print"<div class=\"page_body\">\n";5790 git_print_log($co{'comment'});5791print"</div>\n";57925793 git_difftree_body(\@difftree,$hash,@$parents);57945795 git_footer_html();5796}57975798sub git_object {5799# object is defined by:5800# - hash or hash_base alone5801# - hash_base and file_name5802my$type;58035804# - hash or hash_base alone5805if($hash|| ($hash_base&& !defined$file_name)) {5806my$object_id=$hash||$hash_base;58075808open my$fd,"-|", quote_command(5809 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5810or die_error(404,"Object does not exist");5811$type= <$fd>;5812chomp$type;5813close$fd5814or die_error(404,"Object does not exist");58155816# - hash_base and file_name5817}elsif($hash_base&&defined$file_name) {5818$file_name=~ s,/+$,,;58195820system(git_cmd(),"cat-file",'-e',$hash_base) ==05821or die_error(404,"Base object does not exist");58225823# here errors should not hapen5824open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5825or die_error(500,"Open git-ls-tree failed");5826my$line= <$fd>;5827close$fd;58285829#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5830unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5831 die_error(404,"File or directory for given base does not exist");5832}5833$type=$2;5834$hash=$3;5835}else{5836 die_error(400,"Not enough information to find object");5837}58385839print$cgi->redirect(-uri => href(action=>$type, -full=>1,5840 hash=>$hash, hash_base=>$hash_base,5841 file_name=>$file_name),5842-status =>'302 Found');5843}58445845sub git_blobdiff {5846my$format=shift||'html';58475848my$fd;5849my@difftree;5850my%diffinfo;5851my$expires;58525853# preparing $fd and %diffinfo for git_patchset_body5854# new style URI5855if(defined$hash_base&&defined$hash_parent_base) {5856if(defined$file_name) {5857# read raw output5858open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5859$hash_parent_base,$hash_base,5860"--", (defined$file_parent?$file_parent: ()),$file_name5861or die_error(500,"Open git-diff-tree failed");5862@difftree=map{chomp;$_} <$fd>;5863close$fd5864or die_error(404,"Reading git-diff-tree failed");5865@difftree5866or die_error(404,"Blob diff not found");58675868}elsif(defined$hash&&5869$hash=~/[0-9a-fA-F]{40}/) {5870# try to find filename from $hash58715872# read filtered raw output5873open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5874$hash_parent_base,$hash_base,"--"5875or die_error(500,"Open git-diff-tree failed");5876@difftree=5877# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5878# $hash == to_id5879grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5880map{chomp;$_} <$fd>;5881close$fd5882or die_error(404,"Reading git-diff-tree failed");5883@difftree5884or die_error(404,"Blob diff not found");58855886}else{5887 die_error(400,"Missing one of the blob diff parameters");5888}58895890if(@difftree>1) {5891 die_error(400,"Ambiguous blob diff specification");5892}58935894%diffinfo= parse_difftree_raw_line($difftree[0]);5895$file_parent||=$diffinfo{'from_file'} ||$file_name;5896$file_name||=$diffinfo{'to_file'};58975898$hash_parent||=$diffinfo{'from_id'};5899$hash||=$diffinfo{'to_id'};59005901# non-textual hash id's can be cached5902if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5903$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5904$expires='+1d';5905}59065907# open patch output5908open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5909'-p', ($formateq'html'?"--full-index": ()),5910$hash_parent_base,$hash_base,5911"--", (defined$file_parent?$file_parent: ()),$file_name5912or die_error(500,"Open git-diff-tree failed");5913}59145915# old/legacy style URI -- not generated anymore since 1.4.3.5916if(!%diffinfo) {5917 die_error('404 Not Found',"Missing one of the blob diff parameters")5918}59195920# header5921if($formateq'html') {5922my$formats_nav=5923$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5924"raw");5925 git_header_html(undef,$expires);5926if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5927 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5928 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5929}else{5930print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5931print"<div class=\"title\">$hashvs$hash_parent</div>\n";5932}5933if(defined$file_name) {5934 git_print_page_path($file_name,"blob",$hash_base);5935}else{5936print"<div class=\"page_path\"></div>\n";5937}59385939}elsif($formateq'plain') {5940print$cgi->header(5941-type =>'text/plain',5942-charset =>'utf-8',5943-expires =>$expires,5944-content_disposition =>'inline; filename="'."$file_name".'.patch"');59455946print"X-Git-Url: ".$cgi->self_url() ."\n\n";59475948}else{5949 die_error(400,"Unknown blobdiff format");5950}59515952# patch5953if($formateq'html') {5954print"<div class=\"page_body\">\n";59555956 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5957close$fd;59585959print"</div>\n";# class="page_body"5960 git_footer_html();59615962}else{5963while(my$line= <$fd>) {5964$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5965$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;59665967print$line;59685969last if$line=~m!^\+\+\+!;5970}5971local$/=undef;5972print<$fd>;5973close$fd;5974}5975}59765977sub git_blobdiff_plain {5978 git_blobdiff('plain');5979}59805981sub git_commitdiff {5982my%params=@_;5983my$format=$params{-format} ||'html';59845985my($patch_max) = gitweb_get_feature('patches');5986if($formateq'patch') {5987 die_error(403,"Patch view not allowed")unless$patch_max;5988}59895990$hash||=$hash_base||"HEAD";5991my%co= parse_commit($hash)5992or die_error(404,"Unknown commit object");59935994# choose format for commitdiff for merge5995if(!defined$hash_parent&& @{$co{'parents'}} >1) {5996$hash_parent='--cc';5997}5998# we need to prepare $formats_nav before almost any parameter munging5999my$formats_nav;6000if($formateq'html') {6001$formats_nav=6002$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6003"raw");6004if($patch_max&& @{$co{'parents'}} <=1) {6005$formats_nav.=" | ".6006$cgi->a({-href => href(action=>"patch", -replay=>1)},6007"patch");6008}60096010if(defined$hash_parent&&6011$hash_parentne'-c'&&$hash_parentne'--cc') {6012# commitdiff with two commits given6013my$hash_parent_short=$hash_parent;6014if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6015$hash_parent_short=substr($hash_parent,0,7);6016}6017$formats_nav.=6018' (from';6019for(my$i=0;$i< @{$co{'parents'}};$i++) {6020if($co{'parents'}[$i]eq$hash_parent) {6021$formats_nav.=' parent '. ($i+1);6022last;6023}6024}6025$formats_nav.=': '.6026$cgi->a({-href => href(action=>"commitdiff",6027 hash=>$hash_parent)},6028 esc_html($hash_parent_short)) .6029')';6030}elsif(!$co{'parent'}) {6031# --root commitdiff6032$formats_nav.=' (initial)';6033}elsif(scalar@{$co{'parents'}} ==1) {6034# single parent commit6035$formats_nav.=6036' (parent: '.6037$cgi->a({-href => href(action=>"commitdiff",6038 hash=>$co{'parent'})},6039 esc_html(substr($co{'parent'},0,7))) .6040')';6041}else{6042# merge commit6043if($hash_parenteq'--cc') {6044$formats_nav.=' | '.6045$cgi->a({-href => href(action=>"commitdiff",6046 hash=>$hash, hash_parent=>'-c')},6047'combined');6048}else{# $hash_parent eq '-c'6049$formats_nav.=' | '.6050$cgi->a({-href => href(action=>"commitdiff",6051 hash=>$hash, hash_parent=>'--cc')},6052'compact');6053}6054$formats_nav.=6055' (merge: '.6056join(' ',map{6057$cgi->a({-href => href(action=>"commitdiff",6058 hash=>$_)},6059 esc_html(substr($_,0,7)));6060} @{$co{'parents'}} ) .6061')';6062}6063}60646065my$hash_parent_param=$hash_parent;6066if(!defined$hash_parent_param) {6067# --cc for multiple parents, --root for parentless6068$hash_parent_param=6069@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6070}60716072# read commitdiff6073my$fd;6074my@difftree;6075if($formateq'html') {6076open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6077"--no-commit-id","--patch-with-raw","--full-index",6078$hash_parent_param,$hash,"--"6079or die_error(500,"Open git-diff-tree failed");60806081while(my$line= <$fd>) {6082chomp$line;6083# empty line ends raw part of diff-tree output6084last unless$line;6085push@difftree,scalar parse_difftree_raw_line($line);6086}60876088}elsif($formateq'plain') {6089open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6090'-p',$hash_parent_param,$hash,"--"6091or die_error(500,"Open git-diff-tree failed");6092}elsif($formateq'patch') {6093# For commit ranges, we limit the output to the number of6094# patches specified in the 'patches' feature.6095# For single commits, we limit the output to a single patch,6096# diverging from the git-format-patch default.6097my@commit_spec= ();6098if($hash_parent) {6099if($patch_max>0) {6100push@commit_spec,"-$patch_max";6101}6102push@commit_spec,'-n',"$hash_parent..$hash";6103}else{6104if($params{-single}) {6105push@commit_spec,'-1';6106}else{6107if($patch_max>0) {6108push@commit_spec,"-$patch_max";6109}6110push@commit_spec,"-n";6111}6112push@commit_spec,'--root',$hash;6113}6114open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',6115'--stdout',@commit_spec6116or die_error(500,"Open git-format-patch failed");6117}else{6118 die_error(400,"Unknown commitdiff format");6119}61206121# non-textual hash id's can be cached6122my$expires;6123if($hash=~m/^[0-9a-fA-F]{40}$/) {6124$expires="+1d";6125}61266127# write commit message6128if($formateq'html') {6129my$refs= git_get_references();6130my$ref= format_ref_marker($refs,$co{'id'});61316132 git_header_html(undef,$expires);6133 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6134 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6135print"<div class=\"title_text\">\n".6136"<table class=\"object_header\">\n";6137 git_print_authorship_rows(\%co);6138print"</table>".6139"</div>\n";6140print"<div class=\"page_body\">\n";6141if(@{$co{'comment'}} >1) {6142print"<div class=\"log\">\n";6143 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6144print"</div>\n";# class="log"6145}61466147}elsif($formateq'plain') {6148my$refs= git_get_references("tags");6149my$tagname= git_get_rev_name_tags($hash);6150my$filename= basename($project) ."-$hash.patch";61516152print$cgi->header(6153-type =>'text/plain',6154-charset =>'utf-8',6155-expires =>$expires,6156-content_disposition =>'inline; filename="'."$filename".'"');6157my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6158print"From: ". to_utf8($co{'author'}) ."\n";6159print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6160print"Subject: ". to_utf8($co{'title'}) ."\n";61616162print"X-Git-Tag:$tagname\n"if$tagname;6163print"X-Git-Url: ".$cgi->self_url() ."\n\n";61646165foreachmy$line(@{$co{'comment'}}) {6166print to_utf8($line) ."\n";6167}6168print"---\n\n";6169}elsif($formateq'patch') {6170my$filename= basename($project) ."-$hash.patch";61716172print$cgi->header(6173-type =>'text/plain',6174-charset =>'utf-8',6175-expires =>$expires,6176-content_disposition =>'inline; filename="'."$filename".'"');6177}61786179# write patch6180if($formateq'html') {6181my$use_parents= !defined$hash_parent||6182$hash_parenteq'-c'||$hash_parenteq'--cc';6183 git_difftree_body(\@difftree,$hash,6184$use_parents? @{$co{'parents'}} :$hash_parent);6185print"<br/>\n";61866187 git_patchset_body($fd, \@difftree,$hash,6188$use_parents? @{$co{'parents'}} :$hash_parent);6189close$fd;6190print"</div>\n";# class="page_body"6191 git_footer_html();61926193}elsif($formateq'plain') {6194local$/=undef;6195print<$fd>;6196close$fd6197or print"Reading git-diff-tree failed\n";6198}elsif($formateq'patch') {6199local$/=undef;6200print<$fd>;6201close$fd6202or print"Reading git-format-patch failed\n";6203}6204}62056206sub git_commitdiff_plain {6207 git_commitdiff(-format =>'plain');6208}62096210# format-patch-style patches6211sub git_patch {6212 git_commitdiff(-format =>'patch', -single =>1);6213}62146215sub git_patches {6216 git_commitdiff(-format =>'patch');6217}62186219sub git_history {6220 git_log_generic('history', \&git_history_body,6221$hash_base,$hash_parent_base,6222$file_name,$hash);6223}62246225sub git_search {6226 gitweb_check_feature('search')or die_error(403,"Search is disabled");6227if(!defined$searchtext) {6228 die_error(400,"Text field is empty");6229}6230if(!defined$hash) {6231$hash= git_get_head_hash($project);6232}6233my%co= parse_commit($hash);6234if(!%co) {6235 die_error(404,"Unknown commit object");6236}6237if(!defined$page) {6238$page=0;6239}62406241$searchtype||='commit';6242if($searchtypeeq'pickaxe') {6243# pickaxe may take all resources of your box and run for several minutes6244# with every query - so decide by yourself how public you make this feature6245 gitweb_check_feature('pickaxe')6246or die_error(403,"Pickaxe is disabled");6247}6248if($searchtypeeq'grep') {6249 gitweb_check_feature('grep')6250or die_error(403,"Grep is disabled");6251}62526253 git_header_html();62546255if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6256my$greptype;6257if($searchtypeeq'commit') {6258$greptype="--grep=";6259}elsif($searchtypeeq'author') {6260$greptype="--author=";6261}elsif($searchtypeeq'committer') {6262$greptype="--committer=";6263}6264$greptype.=$searchtext;6265my@commitlist= parse_commits($hash,101, (100*$page),undef,6266$greptype,'--regexp-ignore-case',6267$search_use_regexp?'--extended-regexp':'--fixed-strings');62686269my$paging_nav='';6270if($page>0) {6271$paging_nav.=6272$cgi->a({-href => href(action=>"search", hash=>$hash,6273 searchtext=>$searchtext,6274 searchtype=>$searchtype)},6275"first");6276$paging_nav.=" ⋅ ".6277$cgi->a({-href => href(-replay=>1, page=>$page-1),6278-accesskey =>"p", -title =>"Alt-p"},"prev");6279}else{6280$paging_nav.="first";6281$paging_nav.=" ⋅ prev";6282}6283my$next_link='';6284if($#commitlist>=100) {6285$next_link=6286$cgi->a({-href => href(-replay=>1, page=>$page+1),6287-accesskey =>"n", -title =>"Alt-n"},"next");6288$paging_nav.=" ⋅$next_link";6289}else{6290$paging_nav.=" ⋅ next";6291}62926293if($#commitlist>=100) {6294}62956296 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6297 git_print_header_div('commit', esc_html($co{'title'}),$hash);6298 git_search_grep_body(\@commitlist,0,99,$next_link);6299}63006301if($searchtypeeq'pickaxe') {6302 git_print_page_nav('','',$hash,$co{'tree'},$hash);6303 git_print_header_div('commit', esc_html($co{'title'}),$hash);63046305print"<table class=\"pickaxe search\">\n";6306my$alternate=1;6307local$/="\n";6308open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6309'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6310($search_use_regexp?'--pickaxe-regex': ());6311undef%co;6312my@files;6313while(my$line= <$fd>) {6314chomp$line;6315next unless$line;63166317my%set= parse_difftree_raw_line($line);6318if(defined$set{'commit'}) {6319# finish previous commit6320if(%co) {6321print"</td>\n".6322"<td class=\"link\">".6323$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6324" | ".6325$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6326print"</td>\n".6327"</tr>\n";6328}63296330if($alternate) {6331print"<tr class=\"dark\">\n";6332}else{6333print"<tr class=\"light\">\n";6334}6335$alternate^=1;6336%co= parse_commit($set{'commit'});6337my$author= chop_and_escape_str($co{'author_name'},15,5);6338print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6339"<td><i>$author</i></td>\n".6340"<td>".6341$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6342-class=>"list subject"},6343 chop_and_escape_str($co{'title'},50) ."<br/>");6344}elsif(defined$set{'to_id'}) {6345next if($set{'to_id'} =~m/^0{40}$/);63466347print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6348 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6349-class=>"list"},6350"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6351"<br/>\n";6352}6353}6354close$fd;63556356# finish last commit (warning: repetition!)6357if(%co) {6358print"</td>\n".6359"<td class=\"link\">".6360$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6361" | ".6362$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6363print"</td>\n".6364"</tr>\n";6365}63666367print"</table>\n";6368}63696370if($searchtypeeq'grep') {6371 git_print_page_nav('','',$hash,$co{'tree'},$hash);6372 git_print_header_div('commit', esc_html($co{'title'}),$hash);63736374print"<table class=\"grep_search\">\n";6375my$alternate=1;6376my$matches=0;6377local$/="\n";6378open my$fd,"-|", git_cmd(),'grep','-n',6379$search_use_regexp? ('-E','-i') :'-F',6380$searchtext,$co{'tree'};6381my$lastfile='';6382while(my$line= <$fd>) {6383chomp$line;6384my($file,$lno,$ltext,$binary);6385last if($matches++>1000);6386if($line=~/^Binary file (.+) matches$/) {6387$file=$1;6388$binary=1;6389}else{6390(undef,$file,$lno,$ltext) =split(/:/,$line,4);6391}6392if($filene$lastfile) {6393$lastfileand print"</td></tr>\n";6394if($alternate++) {6395print"<tr class=\"dark\">\n";6396}else{6397print"<tr class=\"light\">\n";6398}6399print"<td class=\"list\">".6400$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6401 file_name=>"$file"),6402-class=>"list"}, esc_path($file));6403print"</td><td>\n";6404$lastfile=$file;6405}6406if($binary) {6407print"<div class=\"binary\">Binary file</div>\n";6408}else{6409$ltext= untabify($ltext);6410if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6411$ltext= esc_html($1, -nbsp=>1);6412$ltext.='<span class="match">';6413$ltext.= esc_html($2, -nbsp=>1);6414$ltext.='</span>';6415$ltext.= esc_html($3, -nbsp=>1);6416}else{6417$ltext= esc_html($ltext, -nbsp=>1);6418}6419print"<div class=\"pre\">".6420$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6421 file_name=>"$file").'#l'.$lno,6422-class=>"linenr"},sprintf('%4i',$lno))6423.' '.$ltext."</div>\n";6424}6425}6426if($lastfile) {6427print"</td></tr>\n";6428if($matches>1000) {6429print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6430}6431}else{6432print"<div class=\"diff nodifferences\">No matches found</div>\n";6433}6434close$fd;64356436print"</table>\n";6437}6438 git_footer_html();6439}64406441sub git_search_help {6442 git_header_html();6443 git_print_page_nav('','',$hash,$hash,$hash);6444print<<EOT;6445<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6446regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6447the pattern entered is recognized as the POSIX extended6448<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6449insensitive).</p>6450<dl>6451<dt><b>commit</b></dt>6452<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6453EOT6454my$have_grep= gitweb_check_feature('grep');6455if($have_grep) {6456print<<EOT;6457<dt><b>grep</b></dt>6458<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6459 a different one) are searched for the given pattern. On large trees, this search can take6460a while and put some strain on the server, so please use it with some consideration. Note that6461due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6462case-sensitive.</dd>6463EOT6464}6465print<<EOT;6466<dt><b>author</b></dt>6467<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6468<dt><b>committer</b></dt>6469<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6470EOT6471my$have_pickaxe= gitweb_check_feature('pickaxe');6472if($have_pickaxe) {6473print<<EOT;6474<dt><b>pickaxe</b></dt>6475<dd>All commits that caused the string to appear or disappear from any file (changes that6476added, removed or "modified" the string) will be listed. This search can take a while and6477takes a lot of strain on the server, so please use it wisely. Note that since you may be6478interested even in changes just changing the case as well, this search is case sensitive.</dd>6479EOT6480}6481print"</dl>\n";6482 git_footer_html();6483}64846485sub git_shortlog {6486 git_log_generic('shortlog', \&git_shortlog_body,6487$hash,$hash_parent);6488}64896490## ......................................................................6491## feeds (RSS, Atom; OPML)64926493sub git_feed {6494my$format=shift||'atom';6495my$have_blame= gitweb_check_feature('blame');64966497# Atom: http://www.atomenabled.org/developers/syndication/6498# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6499if($formatne'rss'&&$formatne'atom') {6500 die_error(400,"Unknown web feed format");6501}65026503# log/feed of current (HEAD) branch, log of given branch, history of file/directory6504my$head=$hash||'HEAD';6505my@commitlist= parse_commits($head,150,0,$file_name);65066507my%latest_commit;6508my%latest_date;6509my$content_type="application/$format+xml";6510if(defined$cgi->http('HTTP_ACCEPT') &&6511$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6512# browser (feed reader) prefers text/xml6513$content_type='text/xml';6514}6515if(defined($commitlist[0])) {6516%latest_commit= %{$commitlist[0]};6517my$latest_epoch=$latest_commit{'committer_epoch'};6518%latest_date= parse_date($latest_epoch);6519my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6520if(defined$if_modified) {6521my$since;6522if(eval{require HTTP::Date;1; }) {6523$since= HTTP::Date::str2time($if_modified);6524}elsif(eval{require Time::ParseDate;1; }) {6525$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6526}6527if(defined$since&&$latest_epoch<=$since) {6528print$cgi->header(6529-type =>$content_type,6530-charset =>'utf-8',6531-last_modified =>$latest_date{'rfc2822'},6532-status =>'304 Not Modified');6533return;6534}6535}6536print$cgi->header(6537-type =>$content_type,6538-charset =>'utf-8',6539-last_modified =>$latest_date{'rfc2822'});6540}else{6541print$cgi->header(6542-type =>$content_type,6543-charset =>'utf-8');6544}65456546# Optimization: skip generating the body if client asks only6547# for Last-Modified date.6548return if($cgi->request_method()eq'HEAD');65496550# header variables6551my$title="$site_name-$project/$action";6552my$feed_type='log';6553if(defined$hash) {6554$title.=" - '$hash'";6555$feed_type='branch log';6556if(defined$file_name) {6557$title.=" ::$file_name";6558$feed_type='history';6559}6560}elsif(defined$file_name) {6561$title.=" -$file_name";6562$feed_type='history';6563}6564$title.="$feed_type";6565my$descr= git_get_project_description($project);6566if(defined$descr) {6567$descr= esc_html($descr);6568}else{6569$descr="$project".6570($formateq'rss'?'RSS':'Atom') .6571" feed";6572}6573my$owner= git_get_project_owner($project);6574$owner= esc_html($owner);65756576#header6577my$alt_url;6578if(defined$file_name) {6579$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6580}elsif(defined$hash) {6581$alt_url= href(-full=>1, action=>"log", hash=>$hash);6582}else{6583$alt_url= href(-full=>1, action=>"summary");6584}6585print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6586if($formateq'rss') {6587print<<XML;6588<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6589<channel>6590XML6591print"<title>$title</title>\n".6592"<link>$alt_url</link>\n".6593"<description>$descr</description>\n".6594"<language>en</language>\n".6595# project owner is responsible for 'editorial' content6596"<managingEditor>$owner</managingEditor>\n";6597if(defined$logo||defined$favicon) {6598# prefer the logo to the favicon, since RSS6599# doesn't allow both6600my$img= esc_url($logo||$favicon);6601print"<image>\n".6602"<url>$img</url>\n".6603"<title>$title</title>\n".6604"<link>$alt_url</link>\n".6605"</image>\n";6606}6607if(%latest_date) {6608print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6609print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6610}6611print"<generator>gitweb v.$version/$git_version</generator>\n";6612}elsif($formateq'atom') {6613print<<XML;6614<feed xmlns="http://www.w3.org/2005/Atom">6615XML6616print"<title>$title</title>\n".6617"<subtitle>$descr</subtitle>\n".6618'<link rel="alternate" type="text/html" href="'.6619$alt_url.'" />'."\n".6620'<link rel="self" type="'.$content_type.'" href="'.6621$cgi->self_url() .'" />'."\n".6622"<id>". href(-full=>1) ."</id>\n".6623# use project owner for feed author6624"<author><name>$owner</name></author>\n";6625if(defined$favicon) {6626print"<icon>". esc_url($favicon) ."</icon>\n";6627}6628if(defined$logo_url) {6629# not twice as wide as tall: 72 x 27 pixels6630print"<logo>". esc_url($logo) ."</logo>\n";6631}6632if(!%latest_date) {6633# dummy date to keep the feed valid until commits trickle in:6634print"<updated>1970-01-01T00:00:00Z</updated>\n";6635}else{6636print"<updated>$latest_date{'iso-8601'}</updated>\n";6637}6638print"<generator version='$version/$git_version'>gitweb</generator>\n";6639}66406641# contents6642for(my$i=0;$i<=$#commitlist;$i++) {6643my%co= %{$commitlist[$i]};6644my$commit=$co{'id'};6645# we read 150, we always show 30 and the ones more recent than 48 hours6646if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6647last;6648}6649my%cd= parse_date($co{'author_epoch'});66506651# get list of changed files6652open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6653$co{'parent'} ||"--root",6654$co{'id'},"--", (defined$file_name?$file_name: ())6655ornext;6656my@difftree=map{chomp;$_} <$fd>;6657close$fd6658ornext;66596660# print element (entry, item)6661my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6662if($formateq'rss') {6663print"<item>\n".6664"<title>". esc_html($co{'title'}) ."</title>\n".6665"<author>". esc_html($co{'author'}) ."</author>\n".6666"<pubDate>$cd{'rfc2822'}</pubDate>\n".6667"<guid isPermaLink=\"true\">$co_url</guid>\n".6668"<link>$co_url</link>\n".6669"<description>". esc_html($co{'title'}) ."</description>\n".6670"<content:encoded>".6671"<![CDATA[\n";6672}elsif($formateq'atom') {6673print"<entry>\n".6674"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6675"<updated>$cd{'iso-8601'}</updated>\n".6676"<author>\n".6677" <name>". esc_html($co{'author_name'}) ."</name>\n";6678if($co{'author_email'}) {6679print" <email>". esc_html($co{'author_email'}) ."</email>\n";6680}6681print"</author>\n".6682# use committer for contributor6683"<contributor>\n".6684" <name>". esc_html($co{'committer_name'}) ."</name>\n";6685if($co{'committer_email'}) {6686print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6687}6688print"</contributor>\n".6689"<published>$cd{'iso-8601'}</published>\n".6690"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6691"<id>$co_url</id>\n".6692"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6693"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6694}6695my$comment=$co{'comment'};6696print"<pre>\n";6697foreachmy$line(@$comment) {6698$line= esc_html($line);6699print"$line\n";6700}6701print"</pre><ul>\n";6702foreachmy$difftree_line(@difftree) {6703my%difftree= parse_difftree_raw_line($difftree_line);6704next if!$difftree{'from_id'};67056706my$file=$difftree{'file'} ||$difftree{'to_file'};67076708print"<li>".6709"[".6710$cgi->a({-href => href(-full=>1, action=>"blobdiff",6711 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6712 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6713 file_name=>$file, file_parent=>$difftree{'from_file'}),6714-title =>"diff"},'D');6715if($have_blame) {6716print$cgi->a({-href => href(-full=>1, action=>"blame",6717 file_name=>$file, hash_base=>$commit),6718-title =>"blame"},'B');6719}6720# if this is not a feed of a file history6721if(!defined$file_name||$file_namene$file) {6722print$cgi->a({-href => href(-full=>1, action=>"history",6723 file_name=>$file, hash=>$commit),6724-title =>"history"},'H');6725}6726$file= esc_path($file);6727print"] ".6728"$file</li>\n";6729}6730if($formateq'rss') {6731print"</ul>]]>\n".6732"</content:encoded>\n".6733"</item>\n";6734}elsif($formateq'atom') {6735print"</ul>\n</div>\n".6736"</content>\n".6737"</entry>\n";6738}6739}67406741# end of feed6742if($formateq'rss') {6743print"</channel>\n</rss>\n";6744}elsif($formateq'atom') {6745print"</feed>\n";6746}6747}67486749sub git_rss {6750 git_feed('rss');6751}67526753sub git_atom {6754 git_feed('atom');6755}67566757sub git_opml {6758my@list= git_get_projects_list();67596760print$cgi->header(6761-type =>'text/xml',6762-charset =>'utf-8',6763-content_disposition =>'inline; filename="opml.xml"');67646765print<<XML;6766<?xml version="1.0" encoding="utf-8"?>6767<opml version="1.0">6768<head>6769 <title>$site_nameOPML Export</title>6770</head>6771<body>6772<outline text="git RSS feeds">6773XML67746775foreachmy$pr(@list) {6776my%proj=%$pr;6777my$head= git_get_head_hash($proj{'path'});6778if(!defined$head) {6779next;6780}6781$git_dir="$projectroot/$proj{'path'}";6782my%co= parse_commit($head);6783if(!%co) {6784next;6785}67866787my$path= esc_html(chop_str($proj{'path'},25,5));6788my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6789my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6790print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6791}6792print<<XML;6793</outline>6794</body>6795</opml>6796XML6797}