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# Syntax highlighting support. This is based on Daniel Svensson's 450# and Sham Chukoury's work in gitweb-xmms2.git. 451# It requires the 'highlight' program present in $PATH, 452# and therefore is disabled by default. 453 454# To enable system wide have in $GITWEB_CONFIG 455# $feature{'highlight'}{'default'} = [1]; 456 457'highlight'=> { 458'sub'=>sub{ feature_bool('highlight',@_) }, 459'override'=>0, 460'default'=> [0]}, 461); 462 463sub gitweb_get_feature { 464my($name) =@_; 465return unlessexists$feature{$name}; 466my($sub,$override,@defaults) = ( 467$feature{$name}{'sub'}, 468$feature{$name}{'override'}, 469@{$feature{$name}{'default'}}); 470# project specific override is possible only if we have project 471our$git_dir;# global variable, declared later 472if(!$override|| !defined$git_dir) { 473return@defaults; 474} 475if(!defined$sub) { 476warn"feature$nameis not overridable"; 477return@defaults; 478} 479return$sub->(@defaults); 480} 481 482# A wrapper to check if a given feature is enabled. 483# With this, you can say 484# 485# my $bool_feat = gitweb_check_feature('bool_feat'); 486# gitweb_check_feature('bool_feat') or somecode; 487# 488# instead of 489# 490# my ($bool_feat) = gitweb_get_feature('bool_feat'); 491# (gitweb_get_feature('bool_feat'))[0] or somecode; 492# 493sub gitweb_check_feature { 494return(gitweb_get_feature(@_))[0]; 495} 496 497 498sub feature_bool { 499my$key=shift; 500my($val) = git_get_project_config($key,'--bool'); 501 502if(!defined$val) { 503return($_[0]); 504}elsif($valeq'true') { 505return(1); 506}elsif($valeq'false') { 507return(0); 508} 509} 510 511sub feature_snapshot { 512my(@fmts) =@_; 513 514my($val) = git_get_project_config('snapshot'); 515 516if($val) { 517@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 518} 519 520return@fmts; 521} 522 523sub feature_patches { 524my@val= (git_get_project_config('patches','--int')); 525 526if(@val) { 527return@val; 528} 529 530return($_[0]); 531} 532 533sub feature_avatar { 534my@val= (git_get_project_config('avatar')); 535 536return@val?@val:@_; 537} 538 539# checking HEAD file with -e is fragile if the repository was 540# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 541# and then pruned. 542sub check_head_link { 543my($dir) =@_; 544my$headfile="$dir/HEAD"; 545return((-e $headfile) || 546(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 547} 548 549sub check_export_ok { 550my($dir) =@_; 551return(check_head_link($dir) && 552(!$export_ok|| -e "$dir/$export_ok") && 553(!$export_auth_hook||$export_auth_hook->($dir))); 554} 555 556# process alternate names for backward compatibility 557# filter out unsupported (unknown) snapshot formats 558sub filter_snapshot_fmts { 559my@fmts=@_; 560 561@fmts=map{ 562exists$known_snapshot_format_aliases{$_} ? 563$known_snapshot_format_aliases{$_} :$_}@fmts; 564@fmts=grep{ 565exists$known_snapshot_formats{$_} && 566!$known_snapshot_formats{$_}{'disabled'}}@fmts; 567} 568 569our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 570our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 571# die if there are errors parsing config file 572if(-e $GITWEB_CONFIG) { 573do$GITWEB_CONFIG; 574die$@if$@; 575}elsif(-e $GITWEB_CONFIG_SYSTEM) { 576do$GITWEB_CONFIG_SYSTEM; 577die$@if$@; 578} 579 580# Get loadavg of system, to compare against $maxload. 581# Currently it requires '/proc/loadavg' present to get loadavg; 582# if it is not present it returns 0, which means no load checking. 583sub get_loadavg { 584if( -e '/proc/loadavg'){ 585open my$fd,'<','/proc/loadavg' 586orreturn0; 587my@load=split(/\s+/,scalar<$fd>); 588close$fd; 589 590# The first three columns measure CPU and IO utilization of the last one, 591# five, and 10 minute periods. The fourth column shows the number of 592# currently running processes and the total number of processes in the m/n 593# format. The last column displays the last process ID used. 594return$load[0] ||0; 595} 596# additional checks for load average should go here for things that don't export 597# /proc/loadavg 598 599return0; 600} 601 602# version of the core git binary 603our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 604$number_of_git_cmds++; 605 606$projects_list||=$projectroot; 607 608if(defined$maxload&& get_loadavg() >$maxload) { 609 die_error(503,"The load average on the server is too high"); 610} 611 612# ====================================================================== 613# input validation and dispatch 614 615# input parameters can be collected from a variety of sources (presently, CGI 616# and PATH_INFO), so we define an %input_params hash that collects them all 617# together during validation: this allows subsequent uses (e.g. href()) to be 618# agnostic of the parameter origin 619 620our%input_params= (); 621 622# input parameters are stored with the long parameter name as key. This will 623# also be used in the href subroutine to convert parameters to their CGI 624# equivalent, and since the href() usage is the most frequent one, we store 625# the name -> CGI key mapping here, instead of the reverse. 626# 627# XXX: Warning: If you touch this, check the search form for updating, 628# too. 629 630our@cgi_param_mapping= ( 631 project =>"p", 632 action =>"a", 633 file_name =>"f", 634 file_parent =>"fp", 635 hash =>"h", 636 hash_parent =>"hp", 637 hash_base =>"hb", 638 hash_parent_base =>"hpb", 639 page =>"pg", 640 order =>"o", 641 searchtext =>"s", 642 searchtype =>"st", 643 snapshot_format =>"sf", 644 extra_options =>"opt", 645 search_use_regexp =>"sr", 646# this must be last entry (for manipulation from JavaScript) 647 javascript =>"js" 648); 649our%cgi_param_mapping=@cgi_param_mapping; 650 651# we will also need to know the possible actions, for validation 652our%actions= ( 653"blame"=> \&git_blame, 654"blame_incremental"=> \&git_blame_incremental, 655"blame_data"=> \&git_blame_data, 656"blobdiff"=> \&git_blobdiff, 657"blobdiff_plain"=> \&git_blobdiff_plain, 658"blob"=> \&git_blob, 659"blob_plain"=> \&git_blob_plain, 660"commitdiff"=> \&git_commitdiff, 661"commitdiff_plain"=> \&git_commitdiff_plain, 662"commit"=> \&git_commit, 663"forks"=> \&git_forks, 664"heads"=> \&git_heads, 665"history"=> \&git_history, 666"log"=> \&git_log, 667"patch"=> \&git_patch, 668"patches"=> \&git_patches, 669"rss"=> \&git_rss, 670"atom"=> \&git_atom, 671"search"=> \&git_search, 672"search_help"=> \&git_search_help, 673"shortlog"=> \&git_shortlog, 674"summary"=> \&git_summary, 675"tag"=> \&git_tag, 676"tags"=> \&git_tags, 677"tree"=> \&git_tree, 678"snapshot"=> \&git_snapshot, 679"object"=> \&git_object, 680# those below don't need $project 681"opml"=> \&git_opml, 682"project_list"=> \&git_project_list, 683"project_index"=> \&git_project_index, 684); 685 686# finally, we have the hash of allowed extra_options for the commands that 687# allow them 688our%allowed_options= ( 689"--no-merges"=> [qw(rss atom log shortlog history)], 690); 691 692# fill %input_params with the CGI parameters. All values except for 'opt' 693# should be single values, but opt can be an array. We should probably 694# build an array of parameters that can be multi-valued, but since for the time 695# being it's only this one, we just single it out 696while(my($name,$symbol) =each%cgi_param_mapping) { 697if($symboleq'opt') { 698$input_params{$name} = [$cgi->param($symbol) ]; 699}else{ 700$input_params{$name} =$cgi->param($symbol); 701} 702} 703 704# now read PATH_INFO and update the parameter list for missing parameters 705sub evaluate_path_info { 706return ifdefined$input_params{'project'}; 707return if!$path_info; 708$path_info=~ s,^/+,,; 709return if!$path_info; 710 711# find which part of PATH_INFO is project 712my$project=$path_info; 713$project=~ s,/+$,,; 714while($project&& !check_head_link("$projectroot/$project")) { 715$project=~ s,/*[^/]*$,,; 716} 717return unless$project; 718$input_params{'project'} =$project; 719 720# do not change any parameters if an action is given using the query string 721return if$input_params{'action'}; 722$path_info=~ s,^\Q$project\E/*,,; 723 724# next, check if we have an action 725my$action=$path_info; 726$action=~ s,/.*$,,; 727if(exists$actions{$action}) { 728$path_info=~ s,^$action/*,,; 729$input_params{'action'} =$action; 730} 731 732# list of actions that want hash_base instead of hash, but can have no 733# pathname (f) parameter 734my@wants_base= ( 735'tree', 736'history', 737); 738 739# we want to catch 740# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 741my($parentrefname,$parentpathname,$refname,$pathname) = 742($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 743 744# first, analyze the 'current' part 745if(defined$pathname) { 746# we got "branch:filename" or "branch:dir/" 747# we could use git_get_type(branch:pathname), but: 748# - it needs $git_dir 749# - it does a git() call 750# - the convention of terminating directories with a slash 751# makes it superfluous 752# - embedding the action in the PATH_INFO would make it even 753# more superfluous 754$pathname=~ s,^/+,,; 755if(!$pathname||substr($pathname, -1)eq"/") { 756$input_params{'action'} ||="tree"; 757$pathname=~ s,/$,,; 758}else{ 759# the default action depends on whether we had parent info 760# or not 761if($parentrefname) { 762$input_params{'action'} ||="blobdiff_plain"; 763}else{ 764$input_params{'action'} ||="blob_plain"; 765} 766} 767$input_params{'hash_base'} ||=$refname; 768$input_params{'file_name'} ||=$pathname; 769}elsif(defined$refname) { 770# we got "branch". In this case we have to choose if we have to 771# set hash or hash_base. 772# 773# Most of the actions without a pathname only want hash to be 774# set, except for the ones specified in @wants_base that want 775# hash_base instead. It should also be noted that hand-crafted 776# links having 'history' as an action and no pathname or hash 777# set will fail, but that happens regardless of PATH_INFO. 778$input_params{'action'} ||="shortlog"; 779if(grep{$_eq$input_params{'action'} }@wants_base) { 780$input_params{'hash_base'} ||=$refname; 781}else{ 782$input_params{'hash'} ||=$refname; 783} 784} 785 786# next, handle the 'parent' part, if present 787if(defined$parentrefname) { 788# a missing pathspec defaults to the 'current' filename, allowing e.g. 789# someproject/blobdiff/oldrev..newrev:/filename 790if($parentpathname) { 791$parentpathname=~ s,^/+,,; 792$parentpathname=~ s,/$,,; 793$input_params{'file_parent'} ||=$parentpathname; 794}else{ 795$input_params{'file_parent'} ||=$input_params{'file_name'}; 796} 797# we assume that hash_parent_base is wanted if a path was specified, 798# or if the action wants hash_base instead of hash 799if(defined$input_params{'file_parent'} || 800grep{$_eq$input_params{'action'} }@wants_base) { 801$input_params{'hash_parent_base'} ||=$parentrefname; 802}else{ 803$input_params{'hash_parent'} ||=$parentrefname; 804} 805} 806 807# for the snapshot action, we allow URLs in the form 808# $project/snapshot/$hash.ext 809# where .ext determines the snapshot and gets removed from the 810# passed $refname to provide the $hash. 811# 812# To be able to tell that $refname includes the format extension, we 813# require the following two conditions to be satisfied: 814# - the hash input parameter MUST have been set from the $refname part 815# of the URL (i.e. they must be equal) 816# - the snapshot format MUST NOT have been defined already (e.g. from 817# CGI parameter sf) 818# It's also useless to try any matching unless $refname has a dot, 819# so we check for that too 820if(defined$input_params{'action'} && 821$input_params{'action'}eq'snapshot'&& 822defined$refname&&index($refname,'.') != -1&& 823$refnameeq$input_params{'hash'} && 824!defined$input_params{'snapshot_format'}) { 825# We loop over the known snapshot formats, checking for 826# extensions. Allowed extensions are both the defined suffix 827# (which includes the initial dot already) and the snapshot 828# format key itself, with a prepended dot 829while(my($fmt,$opt) =each%known_snapshot_formats) { 830my$hash=$refname; 831unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 832next; 833} 834my$sfx=$1; 835# a valid suffix was found, so set the snapshot format 836# and reset the hash parameter 837$input_params{'snapshot_format'} =$fmt; 838$input_params{'hash'} =$hash; 839# we also set the format suffix to the one requested 840# in the URL: this way a request for e.g. .tgz returns 841# a .tgz instead of a .tar.gz 842$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 843last; 844} 845} 846} 847evaluate_path_info(); 848 849our$action=$input_params{'action'}; 850if(defined$action) { 851if(!validate_action($action)) { 852 die_error(400,"Invalid action parameter"); 853} 854} 855 856# parameters which are pathnames 857our$project=$input_params{'project'}; 858if(defined$project) { 859if(!validate_project($project)) { 860undef$project; 861 die_error(404,"No such project"); 862} 863} 864 865our$file_name=$input_params{'file_name'}; 866if(defined$file_name) { 867if(!validate_pathname($file_name)) { 868 die_error(400,"Invalid file parameter"); 869} 870} 871 872our$file_parent=$input_params{'file_parent'}; 873if(defined$file_parent) { 874if(!validate_pathname($file_parent)) { 875 die_error(400,"Invalid file parent parameter"); 876} 877} 878 879# parameters which are refnames 880our$hash=$input_params{'hash'}; 881if(defined$hash) { 882if(!validate_refname($hash)) { 883 die_error(400,"Invalid hash parameter"); 884} 885} 886 887our$hash_parent=$input_params{'hash_parent'}; 888if(defined$hash_parent) { 889if(!validate_refname($hash_parent)) { 890 die_error(400,"Invalid hash parent parameter"); 891} 892} 893 894our$hash_base=$input_params{'hash_base'}; 895if(defined$hash_base) { 896if(!validate_refname($hash_base)) { 897 die_error(400,"Invalid hash base parameter"); 898} 899} 900 901our@extra_options= @{$input_params{'extra_options'}}; 902# @extra_options is always defined, since it can only be (currently) set from 903# CGI, and $cgi->param() returns the empty array in array context if the param 904# is not set 905foreachmy$opt(@extra_options) { 906if(not exists$allowed_options{$opt}) { 907 die_error(400,"Invalid option parameter"); 908} 909if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 910 die_error(400,"Invalid option parameter for this action"); 911} 912} 913 914our$hash_parent_base=$input_params{'hash_parent_base'}; 915if(defined$hash_parent_base) { 916if(!validate_refname($hash_parent_base)) { 917 die_error(400,"Invalid hash parent base parameter"); 918} 919} 920 921# other parameters 922our$page=$input_params{'page'}; 923if(defined$page) { 924if($page=~m/[^0-9]/) { 925 die_error(400,"Invalid page parameter"); 926} 927} 928 929our$searchtype=$input_params{'searchtype'}; 930if(defined$searchtype) { 931if($searchtype=~m/[^a-z]/) { 932 die_error(400,"Invalid searchtype parameter"); 933} 934} 935 936our$search_use_regexp=$input_params{'search_use_regexp'}; 937 938our$searchtext=$input_params{'searchtext'}; 939our$search_regexp; 940if(defined$searchtext) { 941if(length($searchtext) <2) { 942 die_error(403,"At least two characters are required for search parameter"); 943} 944$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 945} 946 947# path to the current git repository 948our$git_dir; 949$git_dir="$projectroot/$project"if$project; 950 951# list of supported snapshot formats 952our@snapshot_fmts= gitweb_get_feature('snapshot'); 953@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 954 955# check that the avatar feature is set to a known provider name, 956# and for each provider check if the dependencies are satisfied. 957# if the provider name is invalid or the dependencies are not met, 958# reset $git_avatar to the empty string. 959our($git_avatar) = gitweb_get_feature('avatar'); 960if($git_avatareq'gravatar') { 961$git_avatar=''unless(eval{require Digest::MD5;1; }); 962}elsif($git_avatareq'picon') { 963# no dependencies 964}else{ 965$git_avatar=''; 966} 967 968# dispatch 969if(!defined$action) { 970if(defined$hash) { 971$action= git_get_type($hash); 972}elsif(defined$hash_base&&defined$file_name) { 973$action= git_get_type("$hash_base:$file_name"); 974}elsif(defined$project) { 975$action='summary'; 976}else{ 977$action='project_list'; 978} 979} 980if(!defined($actions{$action})) { 981 die_error(400,"Unknown action"); 982} 983if($action!~m/^(?:opml|project_list|project_index)$/&& 984!$project) { 985 die_error(400,"Project needed"); 986} 987$actions{$action}->(); 988exit; 989 990## ====================================================================== 991## action links 992 993sub href { 994my%params=@_; 995# default is to use -absolute url() i.e. $my_uri 996my$href=$params{-full} ?$my_url:$my_uri; 997 998$params{'project'} =$projectunlessexists$params{'project'}; 9991000if($params{-replay}) {1001while(my($name,$symbol) =each%cgi_param_mapping) {1002if(!exists$params{$name}) {1003$params{$name} =$input_params{$name};1004}1005}1006}10071008my$use_pathinfo= gitweb_check_feature('pathinfo');1009if($use_pathinfoand defined$params{'project'}) {1010# try to put as many parameters as possible in PATH_INFO:1011# - project name1012# - action1013# - hash_parent or hash_parent_base:/file_parent1014# - hash or hash_base:/filename1015# - the snapshot_format as an appropriate suffix10161017# When the script is the root DirectoryIndex for the domain,1018# $href here would be something like http://gitweb.example.com/1019# Thus, we strip any trailing / from $href, to spare us double1020# slashes in the final URL1021$href=~ s,/$,,;10221023# Then add the project name, if present1024$href.="/".esc_url($params{'project'});1025delete$params{'project'};10261027# since we destructively absorb parameters, we keep this1028# boolean that remembers if we're handling a snapshot1029my$is_snapshot=$params{'action'}eq'snapshot';10301031# Summary just uses the project path URL, any other action is1032# added to the URL1033if(defined$params{'action'}) {1034$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1035delete$params{'action'};1036}10371038# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1039# stripping nonexistent or useless pieces1040$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1041||$params{'hash_parent'} ||$params{'hash'});1042if(defined$params{'hash_base'}) {1043if(defined$params{'hash_parent_base'}) {1044$href.= esc_url($params{'hash_parent_base'});1045# skip the file_parent if it's the same as the file_name1046if(defined$params{'file_parent'}) {1047if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1048delete$params{'file_parent'};1049}elsif($params{'file_parent'} !~/\.\./) {1050$href.=":/".esc_url($params{'file_parent'});1051delete$params{'file_parent'};1052}1053}1054$href.="..";1055delete$params{'hash_parent'};1056delete$params{'hash_parent_base'};1057}elsif(defined$params{'hash_parent'}) {1058$href.= esc_url($params{'hash_parent'})."..";1059delete$params{'hash_parent'};1060}10611062$href.= esc_url($params{'hash_base'});1063if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1064$href.=":/".esc_url($params{'file_name'});1065delete$params{'file_name'};1066}1067delete$params{'hash'};1068delete$params{'hash_base'};1069}elsif(defined$params{'hash'}) {1070$href.= esc_url($params{'hash'});1071delete$params{'hash'};1072}10731074# If the action was a snapshot, we can absorb the1075# snapshot_format parameter too1076if($is_snapshot) {1077my$fmt=$params{'snapshot_format'};1078# snapshot_format should always be defined when href()1079# is called, but just in case some code forgets, we1080# fall back to the default1081$fmt||=$snapshot_fmts[0];1082$href.=$known_snapshot_formats{$fmt}{'suffix'};1083delete$params{'snapshot_format'};1084}1085}10861087# now encode the parameters explicitly1088my@result= ();1089for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1090my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1091if(defined$params{$name}) {1092if(ref($params{$name})eq"ARRAY") {1093foreachmy$par(@{$params{$name}}) {1094push@result,$symbol."=". esc_param($par);1095}1096}else{1097push@result,$symbol."=". esc_param($params{$name});1098}1099}1100}1101$href.="?".join(';',@result)ifscalar@result;11021103return$href;1104}110511061107## ======================================================================1108## validation, quoting/unquoting and escaping11091110sub validate_action {1111my$input=shift||returnundef;1112returnundefunlessexists$actions{$input};1113return$input;1114}11151116sub validate_project {1117my$input=shift||returnundef;1118if(!validate_pathname($input) ||1119!(-d "$projectroot/$input") ||1120!check_export_ok("$projectroot/$input") ||1121($strict_export&& !project_in_list($input))) {1122returnundef;1123}else{1124return$input;1125}1126}11271128sub validate_pathname {1129my$input=shift||returnundef;11301131# no '.' or '..' as elements of path, i.e. no '.' nor '..'1132# at the beginning, at the end, and between slashes.1133# also this catches doubled slashes1134if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1135returnundef;1136}1137# no null characters1138if($input=~m!\0!) {1139returnundef;1140}1141return$input;1142}11431144sub validate_refname {1145my$input=shift||returnundef;11461147# textual hashes are O.K.1148if($input=~m/^[0-9a-fA-F]{40}$/) {1149return$input;1150}1151# it must be correct pathname1152$input= validate_pathname($input)1153orreturnundef;1154# restrictions on ref name according to git-check-ref-format1155if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1156returnundef;1157}1158return$input;1159}11601161# decode sequences of octets in utf8 into Perl's internal form,1162# which is utf-8 with utf8 flag set if needed. gitweb writes out1163# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1164sub to_utf8 {1165my$str=shift;1166returnundefunlessdefined$str;1167if(utf8::valid($str)) {1168 utf8::decode($str);1169return$str;1170}else{1171return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1172}1173}11741175# quote unsafe chars, but keep the slash, even when it's not1176# correct, but quoted slashes look too horrible in bookmarks1177sub esc_param {1178my$str=shift;1179returnundefunlessdefined$str;1180$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1181$str=~s/ /\+/g;1182return$str;1183}11841185# quote unsafe chars in whole URL, so some charactrs cannot be quoted1186sub esc_url {1187my$str=shift;1188returnundefunlessdefined$str;1189$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1190$str=~s/\+/%2B/g;1191$str=~s/ /\+/g;1192return$str;1193}11941195# replace invalid utf8 character with SUBSTITUTION sequence1196sub esc_html {1197my$str=shift;1198my%opts=@_;11991200returnundefunlessdefined$str;12011202$str= to_utf8($str);1203$str=$cgi->escapeHTML($str);1204if($opts{'-nbsp'}) {1205$str=~s/ / /g;1206}1207$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1208return$str;1209}12101211# quote control characters and escape filename to HTML1212sub esc_path {1213my$str=shift;1214my%opts=@_;12151216returnundefunlessdefined$str;12171218$str= to_utf8($str);1219$str=$cgi->escapeHTML($str);1220if($opts{'-nbsp'}) {1221$str=~s/ / /g;1222}1223$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1224return$str;1225}12261227# Make control characters "printable", using character escape codes (CEC)1228sub quot_cec {1229my$cntrl=shift;1230my%opts=@_;1231my%es= (# character escape codes, aka escape sequences1232"\t"=>'\t',# tab (HT)1233"\n"=>'\n',# line feed (LF)1234"\r"=>'\r',# carrige return (CR)1235"\f"=>'\f',# form feed (FF)1236"\b"=>'\b',# backspace (BS)1237"\a"=>'\a',# alarm (bell) (BEL)1238"\e"=>'\e',# escape (ESC)1239"\013"=>'\v',# vertical tab (VT)1240"\000"=>'\0',# nul character (NUL)1241);1242my$chr= ( (exists$es{$cntrl})1243?$es{$cntrl}1244:sprintf('\%2x',ord($cntrl)) );1245if($opts{-nohtml}) {1246return$chr;1247}else{1248return"<span class=\"cntrl\">$chr</span>";1249}1250}12511252# Alternatively use unicode control pictures codepoints,1253# Unicode "printable representation" (PR)1254sub quot_upr {1255my$cntrl=shift;1256my%opts=@_;12571258my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1259if($opts{-nohtml}) {1260return$chr;1261}else{1262return"<span class=\"cntrl\">$chr</span>";1263}1264}12651266# git may return quoted and escaped filenames1267sub unquote {1268my$str=shift;12691270sub unq {1271my$seq=shift;1272my%es= (# character escape codes, aka escape sequences1273't'=>"\t",# tab (HT, TAB)1274'n'=>"\n",# newline (NL)1275'r'=>"\r",# return (CR)1276'f'=>"\f",# form feed (FF)1277'b'=>"\b",# backspace (BS)1278'a'=>"\a",# alarm (bell) (BEL)1279'e'=>"\e",# escape (ESC)1280'v'=>"\013",# vertical tab (VT)1281);12821283if($seq=~m/^[0-7]{1,3}$/) {1284# octal char sequence1285returnchr(oct($seq));1286}elsif(exists$es{$seq}) {1287# C escape sequence, aka character escape code1288return$es{$seq};1289}1290# quoted ordinary character1291return$seq;1292}12931294if($str=~m/^"(.*)"$/) {1295# needs unquoting1296$str=$1;1297$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1298}1299return$str;1300}13011302# escape tabs (convert tabs to spaces)1303sub untabify {1304my$line=shift;13051306while((my$pos=index($line,"\t")) != -1) {1307if(my$count= (8- ($pos%8))) {1308my$spaces=' ' x $count;1309$line=~s/\t/$spaces/;1310}1311}13121313return$line;1314}13151316sub project_in_list {1317my$project=shift;1318my@list= git_get_projects_list();1319return@list&&scalar(grep{$_->{'path'}eq$project}@list);1320}13211322## ----------------------------------------------------------------------1323## HTML aware string manipulation13241325# Try to chop given string on a word boundary between position1326# $len and $len+$add_len. If there is no word boundary there,1327# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1328# (marking chopped part) would be longer than given string.1329sub chop_str {1330my$str=shift;1331my$len=shift;1332my$add_len=shift||10;1333my$where=shift||'right';# 'left' | 'center' | 'right'13341335# Make sure perl knows it is utf8 encoded so we don't1336# cut in the middle of a utf8 multibyte char.1337$str= to_utf8($str);13381339# allow only $len chars, but don't cut a word if it would fit in $add_len1340# if it doesn't fit, cut it if it's still longer than the dots we would add1341# remove chopped character entities entirely13421343# when chopping in the middle, distribute $len into left and right part1344# return early if chopping wouldn't make string shorter1345if($whereeq'center') {1346return$strif($len+5>=length($str));# filler is length 51347$len=int($len/2);1348}else{1349return$strif($len+4>=length($str));# filler is length 41350}13511352# regexps: ending and beginning with word part up to $add_len1353my$endre=qr/.{$len}\w{0,$add_len}/;1354my$begre=qr/\w{0,$add_len}.{$len}/;13551356if($whereeq'left') {1357$str=~m/^(.*?)($begre)$/;1358my($lead,$body) = ($1,$2);1359if(length($lead) >4) {1360$lead=" ...";1361}1362return"$lead$body";13631364}elsif($whereeq'center') {1365$str=~m/^($endre)(.*)$/;1366my($left,$str) = ($1,$2);1367$str=~m/^(.*?)($begre)$/;1368my($mid,$right) = ($1,$2);1369if(length($mid) >5) {1370$mid=" ... ";1371}1372return"$left$mid$right";13731374}else{1375$str=~m/^($endre)(.*)$/;1376my$body=$1;1377my$tail=$2;1378if(length($tail) >4) {1379$tail="... ";1380}1381return"$body$tail";1382}1383}13841385# takes the same arguments as chop_str, but also wraps a <span> around the1386# result with a title attribute if it does get chopped. Additionally, the1387# string is HTML-escaped.1388sub chop_and_escape_str {1389my($str) =@_;13901391my$chopped= chop_str(@_);1392if($choppedeq$str) {1393return esc_html($chopped);1394}else{1395$str=~s/[[:cntrl:]]/?/g;1396return$cgi->span({-title=>$str}, esc_html($chopped));1397}1398}13991400## ----------------------------------------------------------------------1401## functions returning short strings14021403# CSS class for given age value (in seconds)1404sub age_class {1405my$age=shift;14061407if(!defined$age) {1408return"noage";1409}elsif($age<60*60*2) {1410return"age0";1411}elsif($age<60*60*24*2) {1412return"age1";1413}else{1414return"age2";1415}1416}14171418# convert age in seconds to "nn units ago" string1419sub age_string {1420my$age=shift;1421my$age_str;14221423if($age>60*60*24*365*2) {1424$age_str= (int$age/60/60/24/365);1425$age_str.=" years ago";1426}elsif($age>60*60*24*(365/12)*2) {1427$age_str=int$age/60/60/24/(365/12);1428$age_str.=" months ago";1429}elsif($age>60*60*24*7*2) {1430$age_str=int$age/60/60/24/7;1431$age_str.=" weeks ago";1432}elsif($age>60*60*24*2) {1433$age_str=int$age/60/60/24;1434$age_str.=" days ago";1435}elsif($age>60*60*2) {1436$age_str=int$age/60/60;1437$age_str.=" hours ago";1438}elsif($age>60*2) {1439$age_str=int$age/60;1440$age_str.=" min ago";1441}elsif($age>2) {1442$age_str=int$age;1443$age_str.=" sec ago";1444}else{1445$age_str.=" right now";1446}1447return$age_str;1448}14491450useconstant{1451 S_IFINVALID =>0030000,1452 S_IFGITLINK =>0160000,1453};14541455# submodule/subproject, a commit object reference1456sub S_ISGITLINK {1457my$mode=shift;14581459return(($mode& S_IFMT) == S_IFGITLINK)1460}14611462# convert file mode in octal to symbolic file mode string1463sub mode_str {1464my$mode=oct shift;14651466if(S_ISGITLINK($mode)) {1467return'm---------';1468}elsif(S_ISDIR($mode& S_IFMT)) {1469return'drwxr-xr-x';1470}elsif(S_ISLNK($mode)) {1471return'lrwxrwxrwx';1472}elsif(S_ISREG($mode)) {1473# git cares only about the executable bit1474if($mode& S_IXUSR) {1475return'-rwxr-xr-x';1476}else{1477return'-rw-r--r--';1478};1479}else{1480return'----------';1481}1482}14831484# convert file mode in octal to file type string1485sub file_type {1486my$mode=shift;14871488if($mode!~m/^[0-7]+$/) {1489return$mode;1490}else{1491$mode=oct$mode;1492}14931494if(S_ISGITLINK($mode)) {1495return"submodule";1496}elsif(S_ISDIR($mode& S_IFMT)) {1497return"directory";1498}elsif(S_ISLNK($mode)) {1499return"symlink";1500}elsif(S_ISREG($mode)) {1501return"file";1502}else{1503return"unknown";1504}1505}15061507# convert file mode in octal to file type description string1508sub file_type_long {1509my$mode=shift;15101511if($mode!~m/^[0-7]+$/) {1512return$mode;1513}else{1514$mode=oct$mode;1515}15161517if(S_ISGITLINK($mode)) {1518return"submodule";1519}elsif(S_ISDIR($mode& S_IFMT)) {1520return"directory";1521}elsif(S_ISLNK($mode)) {1522return"symlink";1523}elsif(S_ISREG($mode)) {1524if($mode& S_IXUSR) {1525return"executable";1526}else{1527return"file";1528};1529}else{1530return"unknown";1531}1532}153315341535## ----------------------------------------------------------------------1536## functions returning short HTML fragments, or transforming HTML fragments1537## which don't belong to other sections15381539# format line of commit message.1540sub format_log_line_html {1541my$line=shift;15421543$line= esc_html($line, -nbsp=>1);1544$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1545$cgi->a({-href => href(action=>"object", hash=>$1),1546-class=>"text"},$1);1547}eg;15481549return$line;1550}15511552# format marker of refs pointing to given object15531554# the destination action is chosen based on object type and current context:1555# - for annotated tags, we choose the tag view unless it's the current view1556# already, in which case we go to shortlog view1557# - for other refs, we keep the current view if we're in history, shortlog or1558# log view, and select shortlog otherwise1559sub format_ref_marker {1560my($refs,$id) =@_;1561my$markers='';15621563if(defined$refs->{$id}) {1564foreachmy$ref(@{$refs->{$id}}) {1565# this code exploits the fact that non-lightweight tags are the1566# only indirect objects, and that they are the only objects for which1567# we want to use tag instead of shortlog as action1568my($type,$name) =qw();1569my$indirect= ($ref=~s/\^\{\}$//);1570# e.g. tags/v2.6.11 or heads/next1571if($ref=~m!^(.*?)s?/(.*)$!) {1572$type=$1;1573$name=$2;1574}else{1575$type="ref";1576$name=$ref;1577}15781579my$class=$type;1580$class.=" indirect"if$indirect;15811582my$dest_action="shortlog";15831584if($indirect) {1585$dest_action="tag"unless$actioneq"tag";1586}elsif($action=~/^(history|(short)?log)$/) {1587$dest_action=$action;1588}15891590my$dest="";1591$dest.="refs/"unless$ref=~ m!^refs/!;1592$dest.=$ref;15931594my$link=$cgi->a({1595-href => href(1596 action=>$dest_action,1597 hash=>$dest1598)},$name);15991600$markers.=" <span class=\"$class\"title=\"$ref\">".1601$link."</span>";1602}1603}16041605if($markers) {1606return' <span class="refs">'.$markers.'</span>';1607}else{1608return"";1609}1610}16111612# format, perhaps shortened and with markers, title line1613sub format_subject_html {1614my($long,$short,$href,$extra) =@_;1615$extra=''unlessdefined($extra);16161617if(length($short) <length($long)) {1618$long=~s/[[:cntrl:]]/?/g;1619return$cgi->a({-href =>$href, -class=>"list subject",1620-title => to_utf8($long)},1621 esc_html($short)) .$extra;1622}else{1623return$cgi->a({-href =>$href, -class=>"list subject"},1624 esc_html($long)) .$extra;1625}1626}16271628# Rather than recomputing the url for an email multiple times, we cache it1629# after the first hit. This gives a visible benefit in views where the avatar1630# for the same email is used repeatedly (e.g. shortlog).1631# The cache is shared by all avatar engines (currently gravatar only), which1632# are free to use it as preferred. Since only one avatar engine is used for any1633# given page, there's no risk for cache conflicts.1634our%avatar_cache= ();16351636# Compute the picon url for a given email, by using the picon search service over at1637# http://www.cs.indiana.edu/picons/search.html1638sub picon_url {1639my$email=lc shift;1640if(!$avatar_cache{$email}) {1641my($user,$domain) =split('@',$email);1642$avatar_cache{$email} =1643"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1644"$domain/$user/".1645"users+domains+unknown/up/single";1646}1647return$avatar_cache{$email};1648}16491650# Compute the gravatar url for a given email, if it's not in the cache already.1651# Gravatar stores only the part of the URL before the size, since that's the1652# one computationally more expensive. This also allows reuse of the cache for1653# different sizes (for this particular engine).1654sub gravatar_url {1655my$email=lc shift;1656my$size=shift;1657$avatar_cache{$email} ||=1658"http://www.gravatar.com/avatar/".1659 Digest::MD5::md5_hex($email) ."?s=";1660return$avatar_cache{$email} .$size;1661}16621663# Insert an avatar for the given $email at the given $size if the feature1664# is enabled.1665sub git_get_avatar {1666my($email,%opts) =@_;1667my$pre_white= ($opts{-pad_before} ?" ":"");1668my$post_white= ($opts{-pad_after} ?" ":"");1669$opts{-size} ||='default';1670my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1671my$url="";1672if($git_avatareq'gravatar') {1673$url= gravatar_url($email,$size);1674}elsif($git_avatareq'picon') {1675$url= picon_url($email);1676}1677# Other providers can be added by extending the if chain, defining $url1678# as needed. If no variant puts something in $url, we assume avatars1679# are completely disabled/unavailable.1680if($url) {1681return$pre_white.1682"<img width=\"$size\"".1683"class=\"avatar\"".1684"src=\"$url\"".1685"alt=\"\"".1686"/>".$post_white;1687}else{1688return"";1689}1690}16911692sub format_search_author {1693my($author,$searchtype,$displaytext) =@_;1694my$have_search= gitweb_check_feature('search');16951696if($have_search) {1697my$performed="";1698if($searchtypeeq'author') {1699$performed="authored";1700}elsif($searchtypeeq'committer') {1701$performed="committed";1702}17031704return$cgi->a({-href => href(action=>"search", hash=>$hash,1705 searchtext=>$author,1706 searchtype=>$searchtype),class=>"list",1707 title=>"Search for commits$performedby$author"},1708$displaytext);17091710}else{1711return$displaytext;1712}1713}17141715# format the author name of the given commit with the given tag1716# the author name is chopped and escaped according to the other1717# optional parameters (see chop_str).1718sub format_author_html {1719my$tag=shift;1720my$co=shift;1721my$author= chop_and_escape_str($co->{'author_name'},@_);1722return"<$tagclass=\"author\">".1723 format_search_author($co->{'author_name'},"author",1724 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1725$author) .1726"</$tag>";1727}17281729# format git diff header line, i.e. "diff --(git|combined|cc) ..."1730sub format_git_diff_header_line {1731my$line=shift;1732my$diffinfo=shift;1733my($from,$to) =@_;17341735if($diffinfo->{'nparents'}) {1736# combined diff1737$line=~s!^(diff (.*?) )"?.*$!$1!;1738if($to->{'href'}) {1739$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1740 esc_path($to->{'file'}));1741}else{# file was deleted (no href)1742$line.= esc_path($to->{'file'});1743}1744}else{1745# "ordinary" diff1746$line=~s!^(diff (.*?) )"?a/.*$!$1!;1747if($from->{'href'}) {1748$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1749'a/'. esc_path($from->{'file'}));1750}else{# file was added (no href)1751$line.='a/'. esc_path($from->{'file'});1752}1753$line.=' ';1754if($to->{'href'}) {1755$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1756'b/'. esc_path($to->{'file'}));1757}else{# file was deleted1758$line.='b/'. esc_path($to->{'file'});1759}1760}17611762return"<div class=\"diff header\">$line</div>\n";1763}17641765# format extended diff header line, before patch itself1766sub format_extended_diff_header_line {1767my$line=shift;1768my$diffinfo=shift;1769my($from,$to) =@_;17701771# match <path>1772if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1773$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1774 esc_path($from->{'file'}));1775}1776if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1777$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1778 esc_path($to->{'file'}));1779}1780# match single <mode>1781if($line=~m/\s(\d{6})$/) {1782$line.='<span class="info"> ('.1783 file_type_long($1) .1784')</span>';1785}1786# match <hash>1787if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1788# can match only for combined diff1789$line='index ';1790for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1791if($from->{'href'}[$i]) {1792$line.=$cgi->a({-href=>$from->{'href'}[$i],1793-class=>"hash"},1794substr($diffinfo->{'from_id'}[$i],0,7));1795}else{1796$line.='0' x 7;1797}1798# separator1799$line.=','if($i<$diffinfo->{'nparents'} -1);1800}1801$line.='..';1802if($to->{'href'}) {1803$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1804substr($diffinfo->{'to_id'},0,7));1805}else{1806$line.='0' x 7;1807}18081809}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1810# can match only for ordinary diff1811my($from_link,$to_link);1812if($from->{'href'}) {1813$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1814substr($diffinfo->{'from_id'},0,7));1815}else{1816$from_link='0' x 7;1817}1818if($to->{'href'}) {1819$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1820substr($diffinfo->{'to_id'},0,7));1821}else{1822$to_link='0' x 7;1823}1824my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1825$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1826}18271828return$line."<br/>\n";1829}18301831# format from-file/to-file diff header1832sub format_diff_from_to_header {1833my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1834my$line;1835my$result='';18361837$line=$from_line;1838#assert($line =~ m/^---/) if DEBUG;1839# no extra formatting for "^--- /dev/null"1840if(!$diffinfo->{'nparents'}) {1841# ordinary (single parent) diff1842if($line=~m!^--- "?a/!) {1843if($from->{'href'}) {1844$line='--- a/'.1845$cgi->a({-href=>$from->{'href'}, -class=>"path"},1846 esc_path($from->{'file'}));1847}else{1848$line='--- a/'.1849 esc_path($from->{'file'});1850}1851}1852$result.= qq!<div class="diff from_file">$line</div>\n!;18531854}else{1855# combined diff (merge commit)1856for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1857if($from->{'href'}[$i]) {1858$line='--- '.1859$cgi->a({-href=>href(action=>"blobdiff",1860 hash_parent=>$diffinfo->{'from_id'}[$i],1861 hash_parent_base=>$parents[$i],1862 file_parent=>$from->{'file'}[$i],1863 hash=>$diffinfo->{'to_id'},1864 hash_base=>$hash,1865 file_name=>$to->{'file'}),1866-class=>"path",1867-title=>"diff". ($i+1)},1868$i+1) .1869'/'.1870$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1871 esc_path($from->{'file'}[$i]));1872}else{1873$line='--- /dev/null';1874}1875$result.= qq!<div class="diff from_file">$line</div>\n!;1876}1877}18781879$line=$to_line;1880#assert($line =~ m/^\+\+\+/) if DEBUG;1881# no extra formatting for "^+++ /dev/null"1882if($line=~m!^\+\+\+ "?b/!) {1883if($to->{'href'}) {1884$line='+++ b/'.1885$cgi->a({-href=>$to->{'href'}, -class=>"path"},1886 esc_path($to->{'file'}));1887}else{1888$line='+++ b/'.1889 esc_path($to->{'file'});1890}1891}1892$result.= qq!<div class="diff to_file">$line</div>\n!;18931894return$result;1895}18961897# create note for patch simplified by combined diff1898sub format_diff_cc_simplified {1899my($diffinfo,@parents) =@_;1900my$result='';19011902$result.="<div class=\"diff header\">".1903"diff --cc ";1904if(!is_deleted($diffinfo)) {1905$result.=$cgi->a({-href => href(action=>"blob",1906 hash_base=>$hash,1907 hash=>$diffinfo->{'to_id'},1908 file_name=>$diffinfo->{'to_file'}),1909-class=>"path"},1910 esc_path($diffinfo->{'to_file'}));1911}else{1912$result.= esc_path($diffinfo->{'to_file'});1913}1914$result.="</div>\n".# class="diff header"1915"<div class=\"diff nodifferences\">".1916"Simple merge".1917"</div>\n";# class="diff nodifferences"19181919return$result;1920}19211922# format patch (diff) line (not to be used for diff headers)1923sub format_diff_line {1924my$line=shift;1925my($from,$to) =@_;1926my$diff_class="";19271928chomp$line;19291930if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1931# combined diff1932my$prefix=substr($line,0,scalar@{$from->{'href'}});1933if($line=~m/^\@{3}/) {1934$diff_class=" chunk_header";1935}elsif($line=~m/^\\/) {1936$diff_class=" incomplete";1937}elsif($prefix=~tr/+/+/) {1938$diff_class=" add";1939}elsif($prefix=~tr/-/-/) {1940$diff_class=" rem";1941}1942}else{1943# assume ordinary diff1944my$char=substr($line,0,1);1945if($chareq'+') {1946$diff_class=" add";1947}elsif($chareq'-') {1948$diff_class=" rem";1949}elsif($chareq'@') {1950$diff_class=" chunk_header";1951}elsif($chareq"\\") {1952$diff_class=" incomplete";1953}1954}1955$line= untabify($line);1956if($from&&$to&&$line=~m/^\@{2} /) {1957my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1958$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;19591960$from_lines=0unlessdefined$from_lines;1961$to_lines=0unlessdefined$to_lines;19621963if($from->{'href'}) {1964$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1965-class=>"list"},$from_text);1966}1967if($to->{'href'}) {1968$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1969-class=>"list"},$to_text);1970}1971$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1972"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1973return"<div class=\"diff$diff_class\">$line</div>\n";1974}elsif($from&&$to&&$line=~m/^\@{3}/) {1975my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1976my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);19771978@from_text=split(' ',$ranges);1979for(my$i=0;$i<@from_text; ++$i) {1980($from_start[$i],$from_nlines[$i]) =1981(split(',',substr($from_text[$i],1)),0);1982}19831984$to_text=pop@from_text;1985$to_start=pop@from_start;1986$to_nlines=pop@from_nlines;19871988$line="<span class=\"chunk_info\">$prefix";1989for(my$i=0;$i<@from_text; ++$i) {1990if($from->{'href'}[$i]) {1991$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1992-class=>"list"},$from_text[$i]);1993}else{1994$line.=$from_text[$i];1995}1996$line.=" ";1997}1998if($to->{'href'}) {1999$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2000-class=>"list"},$to_text);2001}else{2002$line.=$to_text;2003}2004$line.="$prefix</span>".2005"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2006return"<div class=\"diff$diff_class\">$line</div>\n";2007}2008return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2009}20102011# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2012# linked. Pass the hash of the tree/commit to snapshot.2013sub format_snapshot_links {2014my($hash) =@_;2015my$num_fmts=@snapshot_fmts;2016if($num_fmts>1) {2017# A parenthesized list of links bearing format names.2018# e.g. "snapshot (_tar.gz_ _zip_)"2019return"snapshot (".join(' ',map2020$cgi->a({2021-href => href(2022 action=>"snapshot",2023 hash=>$hash,2024 snapshot_format=>$_2025)2026},$known_snapshot_formats{$_}{'display'})2027,@snapshot_fmts) .")";2028}elsif($num_fmts==1) {2029# A single "snapshot" link whose tooltip bears the format name.2030# i.e. "_snapshot_"2031my($fmt) =@snapshot_fmts;2032return2033$cgi->a({2034-href => href(2035 action=>"snapshot",2036 hash=>$hash,2037 snapshot_format=>$fmt2038),2039-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2040},"snapshot");2041}else{# $num_fmts == 02042returnundef;2043}2044}20452046## ......................................................................2047## functions returning values to be passed, perhaps after some2048## transformation, to other functions; e.g. returning arguments to href()20492050# returns hash to be passed to href to generate gitweb URL2051# in -title key it returns description of link2052sub get_feed_info {2053my$format=shift||'Atom';2054my%res= (action =>lc($format));20552056# feed links are possible only for project views2057return unless(defined$project);2058# some views should link to OPML, or to generic project feed,2059# or don't have specific feed yet (so they should use generic)2060return if($action=~/^(?:tags|heads|forks|tag|search)$/x);20612062my$branch;2063# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2064# from tag links; this also makes possible to detect branch links2065if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2066(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2067$branch=$1;2068}2069# find log type for feed description (title)2070my$type='log';2071if(defined$file_name) {2072$type="history of$file_name";2073$type.="/"if($actioneq'tree');2074$type.=" on '$branch'"if(defined$branch);2075}else{2076$type="log of$branch"if(defined$branch);2077}20782079$res{-title} =$type;2080$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2081$res{'file_name'} =$file_name;20822083return%res;2084}20852086## ----------------------------------------------------------------------2087## git utility subroutines, invoking git commands20882089# returns path to the core git executable and the --git-dir parameter as list2090sub git_cmd {2091$number_of_git_cmds++;2092return$GIT,'--git-dir='.$git_dir;2093}20942095# quote the given arguments for passing them to the shell2096# quote_command("command", "arg 1", "arg with ' and ! characters")2097# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2098# Try to avoid using this function wherever possible.2099sub quote_command {2100returnjoin(' ',2101map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2102}21032104# get HEAD ref of given project as hash2105sub git_get_head_hash {2106return git_get_full_hash(shift,'HEAD');2107}21082109sub git_get_full_hash {2110return git_get_hash(@_);2111}21122113sub git_get_short_hash {2114return git_get_hash(@_,'--short=7');2115}21162117sub git_get_hash {2118my($project,$hash,@options) =@_;2119my$o_git_dir=$git_dir;2120my$retval=undef;2121$git_dir="$projectroot/$project";2122if(open my$fd,'-|', git_cmd(),'rev-parse',2123'--verify','-q',@options,$hash) {2124$retval= <$fd>;2125chomp$retvalifdefined$retval;2126close$fd;2127}2128if(defined$o_git_dir) {2129$git_dir=$o_git_dir;2130}2131return$retval;2132}21332134# get type of given object2135sub git_get_type {2136my$hash=shift;21372138open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2139my$type= <$fd>;2140close$fdorreturn;2141chomp$type;2142return$type;2143}21442145# repository configuration2146our$config_file='';2147our%config;21482149# store multiple values for single key as anonymous array reference2150# single values stored directly in the hash, not as [ <value> ]2151sub hash_set_multi {2152my($hash,$key,$value) =@_;21532154if(!exists$hash->{$key}) {2155$hash->{$key} =$value;2156}elsif(!ref$hash->{$key}) {2157$hash->{$key} = [$hash->{$key},$value];2158}else{2159push@{$hash->{$key}},$value;2160}2161}21622163# return hash of git project configuration2164# optionally limited to some section, e.g. 'gitweb'2165sub git_parse_project_config {2166my$section_regexp=shift;2167my%config;21682169local$/="\0";21702171open my$fh,"-|", git_cmd(),"config",'-z','-l',2172orreturn;21732174while(my$keyval= <$fh>) {2175chomp$keyval;2176my($key,$value) =split(/\n/,$keyval,2);21772178 hash_set_multi(\%config,$key,$value)2179if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2180}2181close$fh;21822183return%config;2184}21852186# convert config value to boolean: 'true' or 'false'2187# no value, number > 0, 'true' and 'yes' values are true2188# rest of values are treated as false (never as error)2189sub config_to_bool {2190my$val=shift;21912192return1if!defined$val;# section.key21932194# strip leading and trailing whitespace2195$val=~s/^\s+//;2196$val=~s/\s+$//;21972198return(($val=~/^\d+$/&&$val) ||# section.key = 12199($val=~/^(?:true|yes)$/i));# section.key = true2200}22012202# convert config value to simple decimal number2203# an optional value suffix of 'k', 'm', or 'g' will cause the value2204# to be multiplied by 1024, 1048576, or 10737418242205sub config_to_int {2206my$val=shift;22072208# strip leading and trailing whitespace2209$val=~s/^\s+//;2210$val=~s/\s+$//;22112212if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2213$unit=lc($unit);2214# unknown unit is treated as 12215return$num* ($uniteq'g'?1073741824:2216$uniteq'm'?1048576:2217$uniteq'k'?1024:1);2218}2219return$val;2220}22212222# convert config value to array reference, if needed2223sub config_to_multi {2224my$val=shift;22252226returnref($val) ?$val: (defined($val) ? [$val] : []);2227}22282229sub git_get_project_config {2230my($key,$type) =@_;22312232return unlessdefined$git_dir;22332234# key sanity check2235return unless($key);2236$key=~s/^gitweb\.//;2237return if($key=~m/\W/);22382239# type sanity check2240if(defined$type) {2241$type=~s/^--//;2242$type=undef2243unless($typeeq'bool'||$typeeq'int');2244}22452246# get config2247if(!defined$config_file||2248$config_filene"$git_dir/config") {2249%config= git_parse_project_config('gitweb');2250$config_file="$git_dir/config";2251}22522253# check if config variable (key) exists2254return unlessexists$config{"gitweb.$key"};22552256# ensure given type2257if(!defined$type) {2258return$config{"gitweb.$key"};2259}elsif($typeeq'bool') {2260# backward compatibility: 'git config --bool' returns true/false2261return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2262}elsif($typeeq'int') {2263return config_to_int($config{"gitweb.$key"});2264}2265return$config{"gitweb.$key"};2266}22672268# get hash of given path at given ref2269sub git_get_hash_by_path {2270my$base=shift;2271my$path=shift||returnundef;2272my$type=shift;22732274$path=~ s,/+$,,;22752276open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2277or die_error(500,"Open git-ls-tree failed");2278my$line= <$fd>;2279close$fdorreturnundef;22802281if(!defined$line) {2282# there is no tree or hash given by $path at $base2283returnundef;2284}22852286#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2287$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2288if(defined$type&&$typene$2) {2289# type doesn't match2290returnundef;2291}2292return$3;2293}22942295# get path of entry with given hash at given tree-ish (ref)2296# used to get 'from' filename for combined diff (merge commit) for renames2297sub git_get_path_by_hash {2298my$base=shift||return;2299my$hash=shift||return;23002301local$/="\0";23022303open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2304orreturnundef;2305while(my$line= <$fd>) {2306chomp$line;23072308#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2309#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2310if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2311close$fd;2312return$1;2313}2314}2315close$fd;2316returnundef;2317}23182319## ......................................................................2320## git utility functions, directly accessing git repository23212322sub git_get_project_description {2323my$path=shift;23242325$git_dir="$projectroot/$path";2326open my$fd,'<',"$git_dir/description"2327orreturn git_get_project_config('description');2328my$descr= <$fd>;2329close$fd;2330if(defined$descr) {2331chomp$descr;2332}2333return$descr;2334}23352336sub git_get_project_ctags {2337my$path=shift;2338my$ctags= {};23392340$git_dir="$projectroot/$path";2341opendir my$dh,"$git_dir/ctags"2342orreturn$ctags;2343foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2344open my$ct,'<',$_ornext;2345my$val= <$ct>;2346chomp$val;2347close$ct;2348my$ctag=$_;$ctag=~ s#.*/##;2349$ctags->{$ctag} =$val;2350}2351closedir$dh;2352$ctags;2353}23542355sub git_populate_project_tagcloud {2356my$ctags=shift;23572358# First, merge different-cased tags; tags vote on casing2359my%ctags_lc;2360foreach(keys%$ctags) {2361$ctags_lc{lc$_}->{count} +=$ctags->{$_};2362if(not$ctags_lc{lc$_}->{topcount}2363or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2364$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2365$ctags_lc{lc$_}->{topname} =$_;2366}2367}23682369my$cloud;2370if(eval{require HTML::TagCloud;1; }) {2371$cloud= HTML::TagCloud->new;2372foreach(sort keys%ctags_lc) {2373# Pad the title with spaces so that the cloud looks2374# less crammed.2375my$title=$ctags_lc{$_}->{topname};2376$title=~s/ / /g;2377$title=~s/^/ /g;2378$title=~s/$/ /g;2379$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2380}2381}else{2382$cloud= \%ctags_lc;2383}2384$cloud;2385}23862387sub git_show_project_tagcloud {2388my($cloud,$count) =@_;2389print STDERR ref($cloud)."..\n";2390if(ref$cloudeq'HTML::TagCloud') {2391return$cloud->html_and_css($count);2392}else{2393my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2394return'<p align="center">'.join(', ',map{2395"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2396}splice(@tags,0,$count)) .'</p>';2397}2398}23992400sub git_get_project_url_list {2401my$path=shift;24022403$git_dir="$projectroot/$path";2404open my$fd,'<',"$git_dir/cloneurl"2405orreturnwantarray?2406@{ config_to_multi(git_get_project_config('url')) } :2407 config_to_multi(git_get_project_config('url'));2408my@git_project_url_list=map{chomp;$_} <$fd>;2409close$fd;24102411returnwantarray?@git_project_url_list: \@git_project_url_list;2412}24132414sub git_get_projects_list {2415my($filter) =@_;2416my@list;24172418$filter||='';2419$filter=~s/\.git$//;24202421my$check_forks= gitweb_check_feature('forks');24222423if(-d $projects_list) {2424# search in directory2425my$dir=$projects_list. ($filter?"/$filter":'');2426# remove the trailing "/"2427$dir=~s!/+$!!;2428my$pfxlen=length("$dir");2429my$pfxdepth= ($dir=~tr!/!!);24302431 File::Find::find({2432 follow_fast =>1,# follow symbolic links2433 follow_skip =>2,# ignore duplicates2434 dangling_symlinks =>0,# ignore dangling symlinks, silently2435 wanted =>sub{2436# skip project-list toplevel, if we get it.2437return if(m!^[/.]$!);2438# only directories can be git repositories2439return unless(-d $_);2440# don't traverse too deep (Find is super slow on os x)2441if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2442$File::Find::prune =1;2443return;2444}24452446my$subdir=substr($File::Find::name,$pfxlen+1);2447# we check related file in $projectroot2448my$path= ($filter?"$filter/":'') .$subdir;2449if(check_export_ok("$projectroot/$path")) {2450push@list, { path =>$path};2451$File::Find::prune =1;2452}2453},2454},"$dir");24552456}elsif(-f $projects_list) {2457# read from file(url-encoded):2458# 'git%2Fgit.git Linus+Torvalds'2459# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2460# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2461my%paths;2462open my$fd,'<',$projects_listorreturn;2463 PROJECT:2464while(my$line= <$fd>) {2465chomp$line;2466my($path,$owner) =split' ',$line;2467$path= unescape($path);2468$owner= unescape($owner);2469if(!defined$path) {2470next;2471}2472if($filterne'') {2473# looking for forks;2474my$pfx=substr($path,0,length($filter));2475if($pfxne$filter) {2476next PROJECT;2477}2478my$sfx=substr($path,length($filter));2479if($sfx!~/^\/.*\.git$/) {2480next PROJECT;2481}2482}elsif($check_forks) {2483 PATH:2484foreachmy$filter(keys%paths) {2485# looking for forks;2486my$pfx=substr($path,0,length($filter));2487if($pfxne$filter) {2488next PATH;2489}2490my$sfx=substr($path,length($filter));2491if($sfx!~/^\/.*\.git$/) {2492next PATH;2493}2494# is a fork, don't include it in2495# the list2496next PROJECT;2497}2498}2499if(check_export_ok("$projectroot/$path")) {2500my$pr= {2501 path =>$path,2502 owner => to_utf8($owner),2503};2504push@list,$pr;2505(my$forks_path=$path) =~s/\.git$//;2506$paths{$forks_path}++;2507}2508}2509close$fd;2510}2511return@list;2512}25132514our$gitweb_project_owner=undef;2515sub git_get_project_list_from_file {25162517return if(defined$gitweb_project_owner);25182519$gitweb_project_owner= {};2520# read from file (url-encoded):2521# 'git%2Fgit.git Linus+Torvalds'2522# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2523# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2524if(-f $projects_list) {2525open(my$fd,'<',$projects_list);2526while(my$line= <$fd>) {2527chomp$line;2528my($pr,$ow) =split' ',$line;2529$pr= unescape($pr);2530$ow= unescape($ow);2531$gitweb_project_owner->{$pr} = to_utf8($ow);2532}2533close$fd;2534}2535}25362537sub git_get_project_owner {2538my$project=shift;2539my$owner;25402541returnundefunless$project;2542$git_dir="$projectroot/$project";25432544if(!defined$gitweb_project_owner) {2545 git_get_project_list_from_file();2546}25472548if(exists$gitweb_project_owner->{$project}) {2549$owner=$gitweb_project_owner->{$project};2550}2551if(!defined$owner){2552$owner= git_get_project_config('owner');2553}2554if(!defined$owner) {2555$owner= get_file_owner("$git_dir");2556}25572558return$owner;2559}25602561sub git_get_last_activity {2562my($path) =@_;2563my$fd;25642565$git_dir="$projectroot/$path";2566open($fd,"-|", git_cmd(),'for-each-ref',2567'--format=%(committer)',2568'--sort=-committerdate',2569'--count=1',2570'refs/heads')orreturn;2571my$most_recent= <$fd>;2572close$fdorreturn;2573if(defined$most_recent&&2574$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2575my$timestamp=$1;2576my$age=time-$timestamp;2577return($age, age_string($age));2578}2579return(undef,undef);2580}25812582sub git_get_references {2583my$type=shift||"";2584my%refs;2585# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112586# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2587open my$fd,"-|", git_cmd(),"show-ref","--dereference",2588($type? ("--","refs/$type") : ())# use -- <pattern> if $type2589orreturn;25902591while(my$line= <$fd>) {2592chomp$line;2593if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2594if(defined$refs{$1}) {2595push@{$refs{$1}},$2;2596}else{2597$refs{$1} = [$2];2598}2599}2600}2601close$fdorreturn;2602return \%refs;2603}26042605sub git_get_rev_name_tags {2606my$hash=shift||returnundef;26072608open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2609orreturn;2610my$name_rev= <$fd>;2611close$fd;26122613if($name_rev=~ m|^$hash tags/(.*)$|) {2614return$1;2615}else{2616# catches also '$hash undefined' output2617returnundef;2618}2619}26202621## ----------------------------------------------------------------------2622## parse to hash functions26232624sub parse_date {2625my$epoch=shift;2626my$tz=shift||"-0000";26272628my%date;2629my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2630my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2631my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2632$date{'hour'} =$hour;2633$date{'minute'} =$min;2634$date{'mday'} =$mday;2635$date{'day'} =$days[$wday];2636$date{'month'} =$months[$mon];2637$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2638$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2639$date{'mday-time'} =sprintf"%d%s%02d:%02d",2640$mday,$months[$mon],$hour,$min;2641$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",26421900+$year,1+$mon,$mday,$hour,$min,$sec;26432644$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2645my$local=$epoch+ ((int$1+ ($2/60)) *3600);2646($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2647$date{'hour_local'} =$hour;2648$date{'minute_local'} =$min;2649$date{'tz_local'} =$tz;2650$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",26511900+$year,$mon+1,$mday,2652$hour,$min,$sec,$tz);2653return%date;2654}26552656sub parse_tag {2657my$tag_id=shift;2658my%tag;2659my@comment;26602661open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2662$tag{'id'} =$tag_id;2663while(my$line= <$fd>) {2664chomp$line;2665if($line=~m/^object ([0-9a-fA-F]{40})$/) {2666$tag{'object'} =$1;2667}elsif($line=~m/^type (.+)$/) {2668$tag{'type'} =$1;2669}elsif($line=~m/^tag (.+)$/) {2670$tag{'name'} =$1;2671}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2672$tag{'author'} =$1;2673$tag{'author_epoch'} =$2;2674$tag{'author_tz'} =$3;2675if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2676$tag{'author_name'} =$1;2677$tag{'author_email'} =$2;2678}else{2679$tag{'author_name'} =$tag{'author'};2680}2681}elsif($line=~m/--BEGIN/) {2682push@comment,$line;2683last;2684}elsif($lineeq"") {2685last;2686}2687}2688push@comment, <$fd>;2689$tag{'comment'} = \@comment;2690close$fdorreturn;2691if(!defined$tag{'name'}) {2692return2693};2694return%tag2695}26962697sub parse_commit_text {2698my($commit_text,$withparents) =@_;2699my@commit_lines=split'\n',$commit_text;2700my%co;27012702pop@commit_lines;# Remove '\0'27032704if(!@commit_lines) {2705return;2706}27072708my$header=shift@commit_lines;2709if($header!~m/^[0-9a-fA-F]{40}/) {2710return;2711}2712($co{'id'},my@parents) =split' ',$header;2713while(my$line=shift@commit_lines) {2714last if$lineeq"\n";2715if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2716$co{'tree'} =$1;2717}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2718push@parents,$1;2719}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2720$co{'author'} = to_utf8($1);2721$co{'author_epoch'} =$2;2722$co{'author_tz'} =$3;2723if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2724$co{'author_name'} =$1;2725$co{'author_email'} =$2;2726}else{2727$co{'author_name'} =$co{'author'};2728}2729}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2730$co{'committer'} = to_utf8($1);2731$co{'committer_epoch'} =$2;2732$co{'committer_tz'} =$3;2733if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2734$co{'committer_name'} =$1;2735$co{'committer_email'} =$2;2736}else{2737$co{'committer_name'} =$co{'committer'};2738}2739}2740}2741if(!defined$co{'tree'}) {2742return;2743};2744$co{'parents'} = \@parents;2745$co{'parent'} =$parents[0];27462747foreachmy$title(@commit_lines) {2748$title=~s/^ //;2749if($titlene"") {2750$co{'title'} = chop_str($title,80,5);2751# remove leading stuff of merges to make the interesting part visible2752if(length($title) >50) {2753$title=~s/^Automatic //;2754$title=~s/^merge (of|with) /Merge ... /i;2755if(length($title) >50) {2756$title=~s/(http|rsync):\/\///;2757}2758if(length($title) >50) {2759$title=~s/(master|www|rsync)\.//;2760}2761if(length($title) >50) {2762$title=~s/kernel.org:?//;2763}2764if(length($title) >50) {2765$title=~s/\/pub\/scm//;2766}2767}2768$co{'title_short'} = chop_str($title,50,5);2769last;2770}2771}2772if(!defined$co{'title'} ||$co{'title'}eq"") {2773$co{'title'} =$co{'title_short'} ='(no commit message)';2774}2775# remove added spaces2776foreachmy$line(@commit_lines) {2777$line=~s/^ //;2778}2779$co{'comment'} = \@commit_lines;27802781my$age=time-$co{'committer_epoch'};2782$co{'age'} =$age;2783$co{'age_string'} = age_string($age);2784my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2785if($age>60*60*24*7*2) {2786$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2787$co{'age_string_age'} =$co{'age_string'};2788}else{2789$co{'age_string_date'} =$co{'age_string'};2790$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2791}2792return%co;2793}27942795sub parse_commit {2796my($commit_id) =@_;2797my%co;27982799local$/="\0";28002801open my$fd,"-|", git_cmd(),"rev-list",2802"--parents",2803"--header",2804"--max-count=1",2805$commit_id,2806"--",2807or die_error(500,"Open git-rev-list failed");2808%co= parse_commit_text(<$fd>,1);2809close$fd;28102811return%co;2812}28132814sub parse_commits {2815my($commit_id,$maxcount,$skip,$filename,@args) =@_;2816my@cos;28172818$maxcount||=1;2819$skip||=0;28202821local$/="\0";28222823open my$fd,"-|", git_cmd(),"rev-list",2824"--header",2825@args,2826("--max-count=".$maxcount),2827("--skip=".$skip),2828@extra_options,2829$commit_id,2830"--",2831($filename? ($filename) : ())2832or die_error(500,"Open git-rev-list failed");2833while(my$line= <$fd>) {2834my%co= parse_commit_text($line);2835push@cos, \%co;2836}2837close$fd;28382839returnwantarray?@cos: \@cos;2840}28412842# parse line of git-diff-tree "raw" output2843sub parse_difftree_raw_line {2844my$line=shift;2845my%res;28462847# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2848# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2849if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2850$res{'from_mode'} =$1;2851$res{'to_mode'} =$2;2852$res{'from_id'} =$3;2853$res{'to_id'} =$4;2854$res{'status'} =$5;2855$res{'similarity'} =$6;2856if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2857($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2858}else{2859$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2860}2861}2862# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2863# combined diff (for merge commit)2864elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2865$res{'nparents'} =length($1);2866$res{'from_mode'} = [split(' ',$2) ];2867$res{'to_mode'} =pop@{$res{'from_mode'}};2868$res{'from_id'} = [split(' ',$3) ];2869$res{'to_id'} =pop@{$res{'from_id'}};2870$res{'status'} = [split('',$4) ];2871$res{'to_file'} = unquote($5);2872}2873# 'c512b523472485aef4fff9e57b229d9d243c967f'2874elsif($line=~m/^([0-9a-fA-F]{40})$/) {2875$res{'commit'} =$1;2876}28772878returnwantarray?%res: \%res;2879}28802881# wrapper: return parsed line of git-diff-tree "raw" output2882# (the argument might be raw line, or parsed info)2883sub parsed_difftree_line {2884my$line_or_ref=shift;28852886if(ref($line_or_ref)eq"HASH") {2887# pre-parsed (or generated by hand)2888return$line_or_ref;2889}else{2890return parse_difftree_raw_line($line_or_ref);2891}2892}28932894# parse line of git-ls-tree output2895sub parse_ls_tree_line {2896my$line=shift;2897my%opts=@_;2898my%res;28992900if($opts{'-l'}) {2901#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'2902$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;29032904$res{'mode'} =$1;2905$res{'type'} =$2;2906$res{'hash'} =$3;2907$res{'size'} =$4;2908if($opts{'-z'}) {2909$res{'name'} =$5;2910}else{2911$res{'name'} = unquote($5);2912}2913}else{2914#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2915$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;29162917$res{'mode'} =$1;2918$res{'type'} =$2;2919$res{'hash'} =$3;2920if($opts{'-z'}) {2921$res{'name'} =$4;2922}else{2923$res{'name'} = unquote($4);2924}2925}29262927returnwantarray?%res: \%res;2928}29292930# generates _two_ hashes, references to which are passed as 2 and 3 argument2931sub parse_from_to_diffinfo {2932my($diffinfo,$from,$to,@parents) =@_;29332934if($diffinfo->{'nparents'}) {2935# combined diff2936$from->{'file'} = [];2937$from->{'href'} = [];2938 fill_from_file_info($diffinfo,@parents)2939unlessexists$diffinfo->{'from_file'};2940for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2941$from->{'file'}[$i] =2942defined$diffinfo->{'from_file'}[$i] ?2943$diffinfo->{'from_file'}[$i] :2944$diffinfo->{'to_file'};2945if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2946$from->{'href'}[$i] = href(action=>"blob",2947 hash_base=>$parents[$i],2948 hash=>$diffinfo->{'from_id'}[$i],2949 file_name=>$from->{'file'}[$i]);2950}else{2951$from->{'href'}[$i] =undef;2952}2953}2954}else{2955# ordinary (not combined) diff2956$from->{'file'} =$diffinfo->{'from_file'};2957if($diffinfo->{'status'}ne"A") {# not new (added) file2958$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2959 hash=>$diffinfo->{'from_id'},2960 file_name=>$from->{'file'});2961}else{2962delete$from->{'href'};2963}2964}29652966$to->{'file'} =$diffinfo->{'to_file'};2967if(!is_deleted($diffinfo)) {# file exists in result2968$to->{'href'} = href(action=>"blob", hash_base=>$hash,2969 hash=>$diffinfo->{'to_id'},2970 file_name=>$to->{'file'});2971}else{2972delete$to->{'href'};2973}2974}29752976## ......................................................................2977## parse to array of hashes functions29782979sub git_get_heads_list {2980my$limit=shift;2981my@headslist;29822983open my$fd,'-|', git_cmd(),'for-each-ref',2984($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2985'--format=%(objectname) %(refname) %(subject)%00%(committer)',2986'refs/heads'2987orreturn;2988while(my$line= <$fd>) {2989my%ref_item;29902991chomp$line;2992my($refinfo,$committerinfo) =split(/\0/,$line);2993my($hash,$name,$title) =split(' ',$refinfo,3);2994my($committer,$epoch,$tz) =2995($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2996$ref_item{'fullname'} =$name;2997$name=~s!^refs/heads/!!;29982999$ref_item{'name'} =$name;3000$ref_item{'id'} =$hash;3001$ref_item{'title'} =$title||'(no commit message)';3002$ref_item{'epoch'} =$epoch;3003if($epoch) {3004$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3005}else{3006$ref_item{'age'} ="unknown";3007}30083009push@headslist, \%ref_item;3010}3011close$fd;30123013returnwantarray?@headslist: \@headslist;3014}30153016sub git_get_tags_list {3017my$limit=shift;3018my@tagslist;30193020open my$fd,'-|', git_cmd(),'for-each-ref',3021($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3022'--format=%(objectname) %(objecttype) %(refname) '.3023'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3024'refs/tags'3025orreturn;3026while(my$line= <$fd>) {3027my%ref_item;30283029chomp$line;3030my($refinfo,$creatorinfo) =split(/\0/,$line);3031my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3032my($creator,$epoch,$tz) =3033($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3034$ref_item{'fullname'} =$name;3035$name=~s!^refs/tags/!!;30363037$ref_item{'type'} =$type;3038$ref_item{'id'} =$id;3039$ref_item{'name'} =$name;3040if($typeeq"tag") {3041$ref_item{'subject'} =$title;3042$ref_item{'reftype'} =$reftype;3043$ref_item{'refid'} =$refid;3044}else{3045$ref_item{'reftype'} =$type;3046$ref_item{'refid'} =$id;3047}30483049if($typeeq"tag"||$typeeq"commit") {3050$ref_item{'epoch'} =$epoch;3051if($epoch) {3052$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3053}else{3054$ref_item{'age'} ="unknown";3055}3056}30573058push@tagslist, \%ref_item;3059}3060close$fd;30613062returnwantarray?@tagslist: \@tagslist;3063}30643065## ----------------------------------------------------------------------3066## filesystem-related functions30673068sub get_file_owner {3069my$path=shift;30703071my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3072my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3073if(!defined$gcos) {3074returnundef;3075}3076my$owner=$gcos;3077$owner=~s/[,;].*$//;3078return to_utf8($owner);3079}30803081# assume that file exists3082sub insert_file {3083my$filename=shift;30843085open my$fd,'<',$filename;3086print map{ to_utf8($_) } <$fd>;3087close$fd;3088}30893090## ......................................................................3091## mimetype related functions30923093sub mimetype_guess_file {3094my$filename=shift;3095my$mimemap=shift;3096-r $mimemaporreturnundef;30973098my%mimemap;3099open(my$mh,'<',$mimemap)orreturnundef;3100while(<$mh>) {3101next ifm/^#/;# skip comments3102my($mimetype,$exts) =split(/\t+/);3103if(defined$exts) {3104my@exts=split(/\s+/,$exts);3105foreachmy$ext(@exts) {3106$mimemap{$ext} =$mimetype;3107}3108}3109}3110close($mh);31113112$filename=~/\.([^.]*)$/;3113return$mimemap{$1};3114}31153116sub mimetype_guess {3117my$filename=shift;3118my$mime;3119$filename=~/\./orreturnundef;31203121if($mimetypes_file) {3122my$file=$mimetypes_file;3123if($file!~m!^/!) {# if it is relative path3124# it is relative to project3125$file="$projectroot/$project/$file";3126}3127$mime= mimetype_guess_file($filename,$file);3128}3129$mime||= mimetype_guess_file($filename,'/etc/mime.types');3130return$mime;3131}31323133sub blob_mimetype {3134my$fd=shift;3135my$filename=shift;31363137if($filename) {3138my$mime= mimetype_guess($filename);3139$mimeandreturn$mime;3140}31413142# just in case3143return$default_blob_plain_mimetypeunless$fd;31443145if(-T $fd) {3146return'text/plain';3147}elsif(!$filename) {3148return'application/octet-stream';3149}elsif($filename=~m/\.png$/i) {3150return'image/png';3151}elsif($filename=~m/\.gif$/i) {3152return'image/gif';3153}elsif($filename=~m/\.jpe?g$/i) {3154return'image/jpeg';3155}else{3156return'application/octet-stream';3157}3158}31593160sub blob_contenttype {3161my($fd,$file_name,$type) =@_;31623163$type||= blob_mimetype($fd,$file_name);3164if($typeeq'text/plain'&&defined$default_text_plain_charset) {3165$type.="; charset=$default_text_plain_charset";3166}31673168return$type;3169}31703171# guess file syntax for syntax highlighting; return undef if no highlighting3172# the name of syntax can (in the future) depend on syntax highlighter used3173sub guess_file_syntax {3174my($highlight,$mimetype,$file_name) =@_;3175returnundefunless($highlight&&defined$file_name);31763177# configuration for 'highlight' (http://www.andre-simon.de/)3178# match by basename3179my%highlight_basename= (3180#'Program' => 'py',3181#'Library' => 'py',3182'SConstruct'=>'py',# SCons equivalent of Makefile3183'Makefile'=>'make',3184);3185# match by extension3186my%highlight_ext= (3187# main extensions, defining name of syntax;3188# see files in /usr/share/highlight/langDefs/ directory3189map{$_=>$_}3190qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),3191# alternate extensions, see /etc/highlight/filetypes.conf3192'h'=>'c',3193map{$_=>'cpp'}qw(cxx c++ cc),3194map{$_=>'php'}qw(php3 php4),3195map{$_=>'pl'}qw(perl pm),# perhaps also 'cgi'3196'mak'=>'make',3197map{$_=>'xml'}qw(xhtml html htm),3198);31993200my$basename= basename($file_name,'.in');3201return$highlight_basename{$basename}3202ifexists$highlight_basename{$basename};32033204$basename=~/\.([^.]*)$/;3205my$ext=$1orreturnundef;3206return$highlight_ext{$ext}3207ifexists$highlight_ext{$ext};32083209returnundef;3210}32113212# run highlighter and return FD of its output,3213# or return original FD if no highlighting3214sub run_highlighter {3215my($fd,$highlight,$syntax) =@_;3216return$fdunless($highlight&&defined$syntax);32173218close$fd3219or die_error(404,"Reading blob failed");3220open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3221"highlight --xhtml --fragment --syntax$syntax|"3222or die_error(500,"Couldn't open file or run syntax highlighter");3223return$fd;3224}32253226## ======================================================================3227## functions printing HTML: header, footer, error page32283229sub git_header_html {3230my$status=shift||"200 OK";3231my$expires=shift;32323233my$title="$site_name";3234if(defined$project) {3235$title.=" - ". to_utf8($project);3236if(defined$action) {3237$title.="/$action";3238if(defined$file_name) {3239$title.=" - ". esc_path($file_name);3240if($actioneq"tree"&&$file_name!~ m|/$|) {3241$title.="/";3242}3243}3244}3245}3246my$content_type;3247# require explicit support from the UA if we are to send the page as3248# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3249# we have to do this because MSIE sometimes globs '*/*', pretending to3250# support xhtml+xml but choking when it gets what it asked for.3251if(defined$cgi->http('HTTP_ACCEPT') &&3252$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3253$cgi->Accept('application/xhtml+xml') !=0) {3254$content_type='application/xhtml+xml';3255}else{3256$content_type='text/html';3257}3258print$cgi->header(-type=>$content_type, -charset =>'utf-8',3259-status=>$status, -expires =>$expires);3260my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3261print<<EOF;3262<?xml version="1.0" encoding="utf-8"?>3263<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3264<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3265<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3266<!-- git core binaries version$git_version-->3267<head>3268<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3269<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3270<meta name="robots" content="index, nofollow"/>3271<title>$title</title>3272EOF3273# the stylesheet, favicon etc urls won't work correctly with path_info3274# unless we set the appropriate base URL3275if($ENV{'PATH_INFO'}) {3276print"<base href=\"".esc_url($base_url)."\"/>\n";3277}3278# print out each stylesheet that exist, providing backwards capability3279# for those people who defined $stylesheet in a config file3280if(defined$stylesheet) {3281print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3282}else{3283foreachmy$stylesheet(@stylesheets) {3284next unless$stylesheet;3285print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3286}3287}3288if(defined$project) {3289my%href_params= get_feed_info();3290if(!exists$href_params{'-title'}) {3291$href_params{'-title'} ='log';3292}32933294foreachmy$formatqw(RSS Atom){3295my$type=lc($format);3296my%link_attr= (3297'-rel'=>'alternate',3298'-title'=>"$project-$href_params{'-title'} -$formatfeed",3299'-type'=>"application/$type+xml"3300);33013302$href_params{'action'} =$type;3303$link_attr{'-href'} = href(%href_params);3304print"<link ".3305"rel=\"$link_attr{'-rel'}\"".3306"title=\"$link_attr{'-title'}\"".3307"href=\"$link_attr{'-href'}\"".3308"type=\"$link_attr{'-type'}\"".3309"/>\n";33103311$href_params{'extra_options'} ='--no-merges';3312$link_attr{'-href'} = href(%href_params);3313$link_attr{'-title'} .=' (no merges)';3314print"<link ".3315"rel=\"$link_attr{'-rel'}\"".3316"title=\"$link_attr{'-title'}\"".3317"href=\"$link_attr{'-href'}\"".3318"type=\"$link_attr{'-type'}\"".3319"/>\n";3320}33213322}else{3323printf('<link rel="alternate" title="%sprojects list" '.3324'href="%s" type="text/plain; charset=utf-8" />'."\n",3325$site_name, href(project=>undef, action=>"project_index"));3326printf('<link rel="alternate" title="%sprojects feeds" '.3327'href="%s" type="text/x-opml" />'."\n",3328$site_name, href(project=>undef, action=>"opml"));3329}3330if(defined$favicon) {3331printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3332}33333334print"</head>\n".3335"<body>\n";33363337if(defined$site_header&& -f $site_header) {3338 insert_file($site_header);3339}33403341print"<div class=\"page_header\">\n".3342$cgi->a({-href => esc_url($logo_url),3343-title =>$logo_label},3344qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3345print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3346if(defined$project) {3347print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3348if(defined$action) {3349print" /$action";3350}3351print"\n";3352}3353print"</div>\n";33543355my$have_search= gitweb_check_feature('search');3356if(defined$project&&$have_search) {3357if(!defined$searchtext) {3358$searchtext="";3359}3360my$search_hash;3361if(defined$hash_base) {3362$search_hash=$hash_base;3363}elsif(defined$hash) {3364$search_hash=$hash;3365}else{3366$search_hash="HEAD";3367}3368my$action=$my_uri;3369my$use_pathinfo= gitweb_check_feature('pathinfo');3370if($use_pathinfo) {3371$action.="/".esc_url($project);3372}3373print$cgi->startform(-method=>"get", -action =>$action) .3374"<div class=\"search\">\n".3375(!$use_pathinfo&&3376$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3377$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3378$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3379$cgi->popup_menu(-name =>'st', -default=>'commit',3380-values=> ['commit','grep','author','committer','pickaxe']) .3381$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3382" search:\n",3383$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3384"<span title=\"Extended regular expression\">".3385$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3386-checked =>$search_use_regexp) .3387"</span>".3388"</div>".3389$cgi->end_form() ."\n";3390}3391}33923393sub git_footer_html {3394my$feed_class='rss_logo';33953396print"<div class=\"page_footer\">\n";3397if(defined$project) {3398my$descr= git_get_project_description($project);3399if(defined$descr) {3400print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3401}34023403my%href_params= get_feed_info();3404if(!%href_params) {3405$feed_class.=' generic';3406}3407$href_params{'-title'} ||='log';34083409foreachmy$formatqw(RSS Atom){3410$href_params{'action'} =lc($format);3411print$cgi->a({-href => href(%href_params),3412-title =>"$href_params{'-title'}$formatfeed",3413-class=>$feed_class},$format)."\n";3414}34153416}else{3417print$cgi->a({-href => href(project=>undef, action=>"opml"),3418-class=>$feed_class},"OPML") ." ";3419print$cgi->a({-href => href(project=>undef, action=>"project_index"),3420-class=>$feed_class},"TXT") ."\n";3421}3422print"</div>\n";# class="page_footer"34233424if(defined$t0&& gitweb_check_feature('timed')) {3425print"<div id=\"generating_info\">\n";3426print'This page took '.3427'<span id="generating_time" class="time_span">'.3428 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3429' seconds </span>'.3430' and '.3431'<span id="generating_cmd">'.3432$number_of_git_cmds.3433'</span> git commands '.3434" to generate.\n";3435print"</div>\n";# class="page_footer"3436}34373438if(defined$site_footer&& -f $site_footer) {3439 insert_file($site_footer);3440}34413442print qq!<script type="text/javascript" src="$javascript"></script>\n!;3443if(defined$action&&3444$actioneq'blame_incremental') {3445print qq!<script type="text/javascript">\n!.3446 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3447 qq!"!. href() .qq!");\n!.3448 qq!</script>\n!;3449}elsif(gitweb_check_feature('javascript-actions')) {3450print qq!<script type="text/javascript">\n!.3451 qq!window.onload = fixLinks;\n!.3452 qq!</script>\n!;3453}34543455print"</body>\n".3456"</html>";3457}34583459# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3460# Example: die_error(404, 'Hash not found')3461# By convention, use the following status codes (as defined in RFC 2616):3462# 400: Invalid or missing CGI parameters, or3463# requested object exists but has wrong type.3464# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3465# this server or project.3466# 404: Requested object/revision/project doesn't exist.3467# 500: The server isn't configured properly, or3468# an internal error occurred (e.g. failed assertions caused by bugs), or3469# an unknown error occurred (e.g. the git binary died unexpectedly).3470# 503: The server is currently unavailable (because it is overloaded,3471# or down for maintenance). Generally, this is a temporary state.3472sub die_error {3473my$status=shift||500;3474my$error= esc_html(shift) ||"Internal Server Error";3475my$extra=shift;34763477my%http_responses= (3478400=>'400 Bad Request',3479403=>'403 Forbidden',3480404=>'404 Not Found',3481500=>'500 Internal Server Error',3482503=>'503 Service Unavailable',3483);3484 git_header_html($http_responses{$status});3485print<<EOF;3486<div class="page_body">3487<br /><br />3488$status-$error3489<br />3490EOF3491if(defined$extra) {3492print"<hr />\n".3493"$extra\n";3494}3495print"</div>\n";34963497 git_footer_html();3498exit;3499}35003501## ----------------------------------------------------------------------3502## functions printing or outputting HTML: navigation35033504sub git_print_page_nav {3505my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3506$extra=''if!defined$extra;# pager or formats35073508my@navs=qw(summary shortlog log commit commitdiff tree);3509if($suppress) {3510@navs=grep{$_ne$suppress}@navs;3511}35123513my%arg=map{$_=> {action=>$_} }@navs;3514if(defined$head) {3515for(qw(commit commitdiff)) {3516$arg{$_}{'hash'} =$head;3517}3518if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3519for(qw(shortlog log)) {3520$arg{$_}{'hash'} =$head;3521}3522}3523}35243525$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3526$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;35273528my@actions= gitweb_get_feature('actions');3529my%repl= (3530'%'=>'%',3531'n'=>$project,# project name3532'f'=>$git_dir,# project path within filesystem3533'h'=>$treehead||'',# current hash ('h' parameter)3534'b'=>$treebase||'',# hash base ('hb' parameter)3535);3536while(@actions) {3537my($label,$link,$pos) =splice(@actions,0,3);3538# insert3539@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3540# munch munch3541$link=~s/%([%nfhb])/$repl{$1}/g;3542$arg{$label}{'_href'} =$link;3543}35443545print"<div class=\"page_nav\">\n".3546(join" | ",3547map{$_eq$current?3548$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3549}@navs);3550print"<br/>\n$extra<br/>\n".3551"</div>\n";3552}35533554sub format_paging_nav {3555my($action,$page,$has_next_link) =@_;3556my$paging_nav;355735583559if($page>0) {3560$paging_nav.=3561$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3562" ⋅ ".3563$cgi->a({-href => href(-replay=>1, page=>$page-1),3564-accesskey =>"p", -title =>"Alt-p"},"prev");3565}else{3566$paging_nav.="first ⋅ prev";3567}35683569if($has_next_link) {3570$paging_nav.=" ⋅ ".3571$cgi->a({-href => href(-replay=>1, page=>$page+1),3572-accesskey =>"n", -title =>"Alt-n"},"next");3573}else{3574$paging_nav.=" ⋅ next";3575}35763577return$paging_nav;3578}35793580## ......................................................................3581## functions printing or outputting HTML: div35823583sub git_print_header_div {3584my($action,$title,$hash,$hash_base) =@_;3585my%args= ();35863587$args{'action'} =$action;3588$args{'hash'} =$hashif$hash;3589$args{'hash_base'} =$hash_baseif$hash_base;35903591print"<div class=\"header\">\n".3592$cgi->a({-href => href(%args), -class=>"title"},3593$title?$title:$action) .3594"\n</div>\n";3595}35963597sub print_local_time {3598print format_local_time(@_);3599}36003601sub format_local_time {3602my$localtime='';3603my%date=@_;3604if($date{'hour_local'} <6) {3605$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3606$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3607}else{3608$localtime.=sprintf(" (%02d:%02d%s)",3609$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3610}36113612return$localtime;3613}36143615# Outputs the author name and date in long form3616sub git_print_authorship {3617my$co=shift;3618my%opts=@_;3619my$tag=$opts{-tag} ||'div';3620my$author=$co->{'author_name'};36213622my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3623print"<$tagclass=\"author_date\">".3624 format_search_author($author,"author", esc_html($author)) .3625" [$ad{'rfc2822'}";3626 print_local_time(%ad)if($opts{-localtime});3627print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3628."</$tag>\n";3629}36303631# Outputs table rows containing the full author or committer information,3632# in the format expected for 'commit' view (& similia).3633# Parameters are a commit hash reference, followed by the list of people3634# to output information for. If the list is empty it defalts to both3635# author and committer.3636sub git_print_authorship_rows {3637my$co=shift;3638# too bad we can't use @people = @_ || ('author', 'committer')3639my@people=@_;3640@people= ('author','committer')unless@people;3641foreachmy$who(@people) {3642my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3643print"<tr><td>$who</td><td>".3644 format_search_author($co->{"${who}_name"},$who,3645 esc_html($co->{"${who}_name"})) ." ".3646 format_search_author($co->{"${who}_email"},$who,3647 esc_html("<".$co->{"${who}_email"} .">")) .3648"</td><td rowspan=\"2\">".3649 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3650"</td></tr>\n".3651"<tr>".3652"<td></td><td>$wd{'rfc2822'}";3653 print_local_time(%wd);3654print"</td>".3655"</tr>\n";3656}3657}36583659sub git_print_page_path {3660my$name=shift;3661my$type=shift;3662my$hb=shift;366336643665print"<div class=\"page_path\">";3666print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3667-title =>'tree root'}, to_utf8("[$project]"));3668print" / ";3669if(defined$name) {3670my@dirname=split'/',$name;3671my$basename=pop@dirname;3672my$fullname='';36733674foreachmy$dir(@dirname) {3675$fullname.= ($fullname?'/':'') .$dir;3676print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3677 hash_base=>$hb),3678-title =>$fullname}, esc_path($dir));3679print" / ";3680}3681if(defined$type&&$typeeq'blob') {3682print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3683 hash_base=>$hb),3684-title =>$name}, esc_path($basename));3685}elsif(defined$type&&$typeeq'tree') {3686print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3687 hash_base=>$hb),3688-title =>$name}, esc_path($basename));3689print" / ";3690}else{3691print esc_path($basename);3692}3693}3694print"<br/></div>\n";3695}36963697sub git_print_log {3698my$log=shift;3699my%opts=@_;37003701if($opts{'-remove_title'}) {3702# remove title, i.e. first line of log3703shift@$log;3704}3705# remove leading empty lines3706while(defined$log->[0] &&$log->[0]eq"") {3707shift@$log;3708}37093710# print log3711my$signoff=0;3712my$empty=0;3713foreachmy$line(@$log) {3714if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3715$signoff=1;3716$empty=0;3717if(!$opts{'-remove_signoff'}) {3718print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3719next;3720}else{3721# remove signoff lines3722next;3723}3724}else{3725$signoff=0;3726}37273728# print only one empty line3729# do not print empty line after signoff3730if($lineeq"") {3731next if($empty||$signoff);3732$empty=1;3733}else{3734$empty=0;3735}37363737print format_log_line_html($line) ."<br/>\n";3738}37393740if($opts{'-final_empty_line'}) {3741# end with single empty line3742print"<br/>\n"unless$empty;3743}3744}37453746# return link target (what link points to)3747sub git_get_link_target {3748my$hash=shift;3749my$link_target;37503751# read link3752open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3753orreturn;3754{3755local$/=undef;3756$link_target= <$fd>;3757}3758close$fd3759orreturn;37603761return$link_target;3762}37633764# given link target, and the directory (basedir) the link is in,3765# return target of link relative to top directory (top tree);3766# return undef if it is not possible (including absolute links).3767sub normalize_link_target {3768my($link_target,$basedir) =@_;37693770# absolute symlinks (beginning with '/') cannot be normalized3771return if(substr($link_target,0,1)eq'/');37723773# normalize link target to path from top (root) tree (dir)3774my$path;3775if($basedir) {3776$path=$basedir.'/'.$link_target;3777}else{3778# we are in top (root) tree (dir)3779$path=$link_target;3780}37813782# remove //, /./, and /../3783my@path_parts;3784foreachmy$part(split('/',$path)) {3785# discard '.' and ''3786next if(!$part||$parteq'.');3787# handle '..'3788if($parteq'..') {3789if(@path_parts) {3790pop@path_parts;3791}else{3792# link leads outside repository (outside top dir)3793return;3794}3795}else{3796push@path_parts,$part;3797}3798}3799$path=join('/',@path_parts);38003801return$path;3802}38033804# print tree entry (row of git_tree), but without encompassing <tr> element3805sub git_print_tree_entry {3806my($t,$basedir,$hash_base,$have_blame) =@_;38073808my%base_key= ();3809$base_key{'hash_base'} =$hash_baseifdefined$hash_base;38103811# The format of a table row is: mode list link. Where mode is3812# the mode of the entry, list is the name of the entry, an href,3813# and link is the action links of the entry.38143815print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3816if(exists$t->{'size'}) {3817print"<td class=\"size\">$t->{'size'}</td>\n";3818}3819if($t->{'type'}eq"blob") {3820print"<td class=\"list\">".3821$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3822 file_name=>"$basedir$t->{'name'}",%base_key),3823-class=>"list"}, esc_path($t->{'name'}));3824if(S_ISLNK(oct$t->{'mode'})) {3825my$link_target= git_get_link_target($t->{'hash'});3826if($link_target) {3827my$norm_target= normalize_link_target($link_target,$basedir);3828if(defined$norm_target) {3829print" -> ".3830$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3831 file_name=>$norm_target),3832-title =>$norm_target}, esc_path($link_target));3833}else{3834print" -> ". esc_path($link_target);3835}3836}3837}3838print"</td>\n";3839print"<td class=\"link\">";3840print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3841 file_name=>"$basedir$t->{'name'}",%base_key)},3842"blob");3843if($have_blame) {3844print" | ".3845$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3846 file_name=>"$basedir$t->{'name'}",%base_key)},3847"blame");3848}3849if(defined$hash_base) {3850print" | ".3851$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3852 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3853"history");3854}3855print" | ".3856$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3857 file_name=>"$basedir$t->{'name'}")},3858"raw");3859print"</td>\n";38603861}elsif($t->{'type'}eq"tree") {3862print"<td class=\"list\">";3863print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3864 file_name=>"$basedir$t->{'name'}",3865%base_key)},3866 esc_path($t->{'name'}));3867print"</td>\n";3868print"<td class=\"link\">";3869print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3870 file_name=>"$basedir$t->{'name'}",3871%base_key)},3872"tree");3873if(defined$hash_base) {3874print" | ".3875$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3876 file_name=>"$basedir$t->{'name'}")},3877"history");3878}3879print"</td>\n";3880}else{3881# unknown object: we can only present history for it3882# (this includes 'commit' object, i.e. submodule support)3883print"<td class=\"list\">".3884 esc_path($t->{'name'}) .3885"</td>\n";3886print"<td class=\"link\">";3887if(defined$hash_base) {3888print$cgi->a({-href => href(action=>"history",3889 hash_base=>$hash_base,3890 file_name=>"$basedir$t->{'name'}")},3891"history");3892}3893print"</td>\n";3894}3895}38963897## ......................................................................3898## functions printing large fragments of HTML38993900# get pre-image filenames for merge (combined) diff3901sub fill_from_file_info {3902my($diff,@parents) =@_;39033904$diff->{'from_file'} = [ ];3905$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3906for(my$i=0;$i<$diff->{'nparents'};$i++) {3907if($diff->{'status'}[$i]eq'R'||3908$diff->{'status'}[$i]eq'C') {3909$diff->{'from_file'}[$i] =3910 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3911}3912}39133914return$diff;3915}39163917# is current raw difftree line of file deletion3918sub is_deleted {3919my$diffinfo=shift;39203921return$diffinfo->{'to_id'}eq('0' x 40);3922}39233924# does patch correspond to [previous] difftree raw line3925# $diffinfo - hashref of parsed raw diff format3926# $patchinfo - hashref of parsed patch diff format3927# (the same keys as in $diffinfo)3928sub is_patch_split {3929my($diffinfo,$patchinfo) =@_;39303931returndefined$diffinfo&&defined$patchinfo3932&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3933}393439353936sub git_difftree_body {3937my($difftree,$hash,@parents) =@_;3938my($parent) =$parents[0];3939my$have_blame= gitweb_check_feature('blame');3940print"<div class=\"list_head\">\n";3941if($#{$difftree} >10) {3942print(($#{$difftree} +1) ." files changed:\n");3943}3944print"</div>\n";39453946print"<table class=\"".3947(@parents>1?"combined ":"") .3948"diff_tree\">\n";39493950# header only for combined diff in 'commitdiff' view3951my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3952if($has_header) {3953# table header3954print"<thead><tr>\n".3955"<th></th><th></th>\n";# filename, patchN link3956for(my$i=0;$i<@parents;$i++) {3957my$par=$parents[$i];3958print"<th>".3959$cgi->a({-href => href(action=>"commitdiff",3960 hash=>$hash, hash_parent=>$par),3961-title =>'commitdiff to parent number '.3962($i+1) .': '.substr($par,0,7)},3963$i+1) .3964" </th>\n";3965}3966print"</tr></thead>\n<tbody>\n";3967}39683969my$alternate=1;3970my$patchno=0;3971foreachmy$line(@{$difftree}) {3972my$diff= parsed_difftree_line($line);39733974if($alternate) {3975print"<tr class=\"dark\">\n";3976}else{3977print"<tr class=\"light\">\n";3978}3979$alternate^=1;39803981if(exists$diff->{'nparents'}) {# combined diff39823983 fill_from_file_info($diff,@parents)3984unlessexists$diff->{'from_file'};39853986if(!is_deleted($diff)) {3987# file exists in the result (child) commit3988print"<td>".3989$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3990 file_name=>$diff->{'to_file'},3991 hash_base=>$hash),3992-class=>"list"}, esc_path($diff->{'to_file'})) .3993"</td>\n";3994}else{3995print"<td>".3996 esc_path($diff->{'to_file'}) .3997"</td>\n";3998}39994000if($actioneq'commitdiff') {4001# link to patch4002$patchno++;4003print"<td class=\"link\">".4004$cgi->a({-href =>"#patch$patchno"},"patch") .4005" | ".4006"</td>\n";4007}40084009my$has_history=0;4010my$not_deleted=0;4011for(my$i=0;$i<$diff->{'nparents'};$i++) {4012my$hash_parent=$parents[$i];4013my$from_hash=$diff->{'from_id'}[$i];4014my$from_path=$diff->{'from_file'}[$i];4015my$status=$diff->{'status'}[$i];40164017$has_history||= ($statusne'A');4018$not_deleted||= ($statusne'D');40194020if($statuseq'A') {4021print"<td class=\"link\"align=\"right\"> | </td>\n";4022}elsif($statuseq'D') {4023print"<td class=\"link\">".4024$cgi->a({-href => href(action=>"blob",4025 hash_base=>$hash,4026 hash=>$from_hash,4027 file_name=>$from_path)},4028"blob". ($i+1)) .4029" | </td>\n";4030}else{4031if($diff->{'to_id'}eq$from_hash) {4032print"<td class=\"link nochange\">";4033}else{4034print"<td class=\"link\">";4035}4036print$cgi->a({-href => href(action=>"blobdiff",4037 hash=>$diff->{'to_id'},4038 hash_parent=>$from_hash,4039 hash_base=>$hash,4040 hash_parent_base=>$hash_parent,4041 file_name=>$diff->{'to_file'},4042 file_parent=>$from_path)},4043"diff". ($i+1)) .4044" | </td>\n";4045}4046}40474048print"<td class=\"link\">";4049if($not_deleted) {4050print$cgi->a({-href => href(action=>"blob",4051 hash=>$diff->{'to_id'},4052 file_name=>$diff->{'to_file'},4053 hash_base=>$hash)},4054"blob");4055print" | "if($has_history);4056}4057if($has_history) {4058print$cgi->a({-href => href(action=>"history",4059 file_name=>$diff->{'to_file'},4060 hash_base=>$hash)},4061"history");4062}4063print"</td>\n";40644065print"</tr>\n";4066next;# instead of 'else' clause, to avoid extra indent4067}4068# else ordinary diff40694070my($to_mode_oct,$to_mode_str,$to_file_type);4071my($from_mode_oct,$from_mode_str,$from_file_type);4072if($diff->{'to_mode'}ne('0' x 6)) {4073$to_mode_oct=oct$diff->{'to_mode'};4074if(S_ISREG($to_mode_oct)) {# only for regular file4075$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4076}4077$to_file_type= file_type($diff->{'to_mode'});4078}4079if($diff->{'from_mode'}ne('0' x 6)) {4080$from_mode_oct=oct$diff->{'from_mode'};4081if(S_ISREG($to_mode_oct)) {# only for regular file4082$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4083}4084$from_file_type= file_type($diff->{'from_mode'});4085}40864087if($diff->{'status'}eq"A") {# created4088my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4089$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4090$mode_chng.="]</span>";4091print"<td>";4092print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4093 hash_base=>$hash, file_name=>$diff->{'file'}),4094-class=>"list"}, esc_path($diff->{'file'}));4095print"</td>\n";4096print"<td>$mode_chng</td>\n";4097print"<td class=\"link\">";4098if($actioneq'commitdiff') {4099# link to patch4100$patchno++;4101print$cgi->a({-href =>"#patch$patchno"},"patch");4102print" | ";4103}4104print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4105 hash_base=>$hash, file_name=>$diff->{'file'})},4106"blob");4107print"</td>\n";41084109}elsif($diff->{'status'}eq"D") {# deleted4110my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4111print"<td>";4112print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4113 hash_base=>$parent, file_name=>$diff->{'file'}),4114-class=>"list"}, esc_path($diff->{'file'}));4115print"</td>\n";4116print"<td>$mode_chng</td>\n";4117print"<td class=\"link\">";4118if($actioneq'commitdiff') {4119# link to patch4120$patchno++;4121print$cgi->a({-href =>"#patch$patchno"},"patch");4122print" | ";4123}4124print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4125 hash_base=>$parent, file_name=>$diff->{'file'})},4126"blob") ." | ";4127if($have_blame) {4128print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4129 file_name=>$diff->{'file'})},4130"blame") ." | ";4131}4132print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4133 file_name=>$diff->{'file'})},4134"history");4135print"</td>\n";41364137}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4138my$mode_chnge="";4139if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4140$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4141if($from_file_typene$to_file_type) {4142$mode_chnge.=" from$from_file_typeto$to_file_type";4143}4144if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4145if($from_mode_str&&$to_mode_str) {4146$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4147}elsif($to_mode_str) {4148$mode_chnge.=" mode:$to_mode_str";4149}4150}4151$mode_chnge.="]</span>\n";4152}4153print"<td>";4154print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4155 hash_base=>$hash, file_name=>$diff->{'file'}),4156-class=>"list"}, esc_path($diff->{'file'}));4157print"</td>\n";4158print"<td>$mode_chnge</td>\n";4159print"<td class=\"link\">";4160if($actioneq'commitdiff') {4161# link to patch4162$patchno++;4163print$cgi->a({-href =>"#patch$patchno"},"patch") .4164" | ";4165}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4166# "commit" view and modified file (not onlu mode changed)4167print$cgi->a({-href => href(action=>"blobdiff",4168 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4169 hash_base=>$hash, hash_parent_base=>$parent,4170 file_name=>$diff->{'file'})},4171"diff") .4172" | ";4173}4174print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4175 hash_base=>$hash, file_name=>$diff->{'file'})},4176"blob") ." | ";4177if($have_blame) {4178print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4179 file_name=>$diff->{'file'})},4180"blame") ." | ";4181}4182print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4183 file_name=>$diff->{'file'})},4184"history");4185print"</td>\n";41864187}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4188my%status_name= ('R'=>'moved','C'=>'copied');4189my$nstatus=$status_name{$diff->{'status'}};4190my$mode_chng="";4191if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4192# mode also for directories, so we cannot use $to_mode_str4193$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4194}4195print"<td>".4196$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4197 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4198-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4199"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4200$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4201 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4202-class=>"list"}, esc_path($diff->{'from_file'})) .4203" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4204"<td class=\"link\">";4205if($actioneq'commitdiff') {4206# link to patch4207$patchno++;4208print$cgi->a({-href =>"#patch$patchno"},"patch") .4209" | ";4210}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4211# "commit" view and modified file (not only pure rename or copy)4212print$cgi->a({-href => href(action=>"blobdiff",4213 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4214 hash_base=>$hash, hash_parent_base=>$parent,4215 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4216"diff") .4217" | ";4218}4219print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4220 hash_base=>$parent, file_name=>$diff->{'to_file'})},4221"blob") ." | ";4222if($have_blame) {4223print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4224 file_name=>$diff->{'to_file'})},4225"blame") ." | ";4226}4227print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4228 file_name=>$diff->{'to_file'})},4229"history");4230print"</td>\n";42314232}# we should not encounter Unmerged (U) or Unknown (X) status4233print"</tr>\n";4234}4235print"</tbody>"if$has_header;4236print"</table>\n";4237}42384239sub git_patchset_body {4240my($fd,$difftree,$hash,@hash_parents) =@_;4241my($hash_parent) =$hash_parents[0];42424243my$is_combined= (@hash_parents>1);4244my$patch_idx=0;4245my$patch_number=0;4246my$patch_line;4247my$diffinfo;4248my$to_name;4249my(%from,%to);42504251print"<div class=\"patchset\">\n";42524253# skip to first patch4254while($patch_line= <$fd>) {4255chomp$patch_line;42564257last if($patch_line=~m/^diff /);4258}42594260 PATCH:4261while($patch_line) {42624263# parse "git diff" header line4264if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4265# $1 is from_name, which we do not use4266$to_name= unquote($2);4267$to_name=~s!^b/!!;4268}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4269# $1 is 'cc' or 'combined', which we do not use4270$to_name= unquote($2);4271}else{4272$to_name=undef;4273}42744275# check if current patch belong to current raw line4276# and parse raw git-diff line if needed4277if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4278# this is continuation of a split patch4279print"<div class=\"patch cont\">\n";4280}else{4281# advance raw git-diff output if needed4282$patch_idx++ifdefined$diffinfo;42834284# read and prepare patch information4285$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);42864287# compact combined diff output can have some patches skipped4288# find which patch (using pathname of result) we are at now;4289if($is_combined) {4290while($to_namene$diffinfo->{'to_file'}) {4291print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4292 format_diff_cc_simplified($diffinfo,@hash_parents) .4293"</div>\n";# class="patch"42944295$patch_idx++;4296$patch_number++;42974298last if$patch_idx>$#$difftree;4299$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4300}4301}43024303# modifies %from, %to hashes4304 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);43054306# this is first patch for raw difftree line with $patch_idx index4307# we index @$difftree array from 0, but number patches from 14308print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4309}43104311# git diff header4312#assert($patch_line =~ m/^diff /) if DEBUG;4313#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4314$patch_number++;4315# print "git diff" header4316print format_git_diff_header_line($patch_line,$diffinfo,4317 \%from, \%to);43184319# print extended diff header4320print"<div class=\"diff extended_header\">\n";4321 EXTENDED_HEADER:4322while($patch_line= <$fd>) {4323chomp$patch_line;43244325last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);43264327print format_extended_diff_header_line($patch_line,$diffinfo,4328 \%from, \%to);4329}4330print"</div>\n";# class="diff extended_header"43314332# from-file/to-file diff header4333if(!$patch_line) {4334print"</div>\n";# class="patch"4335last PATCH;4336}4337next PATCH if($patch_line=~m/^diff /);4338#assert($patch_line =~ m/^---/) if DEBUG;43394340my$last_patch_line=$patch_line;4341$patch_line= <$fd>;4342chomp$patch_line;4343#assert($patch_line =~ m/^\+\+\+/) if DEBUG;43444345print format_diff_from_to_header($last_patch_line,$patch_line,4346$diffinfo, \%from, \%to,4347@hash_parents);43484349# the patch itself4350 LINE:4351while($patch_line= <$fd>) {4352chomp$patch_line;43534354next PATCH if($patch_line=~m/^diff /);43554356print format_diff_line($patch_line, \%from, \%to);4357}43584359}continue{4360print"</div>\n";# class="patch"4361}43624363# for compact combined (--cc) format, with chunk and patch simpliciaction4364# patchset might be empty, but there might be unprocessed raw lines4365for(++$patch_idxif$patch_number>0;4366$patch_idx<@$difftree;4367++$patch_idx) {4368# read and prepare patch information4369$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);43704371# generate anchor for "patch" links in difftree / whatchanged part4372print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4373 format_diff_cc_simplified($diffinfo,@hash_parents) .4374"</div>\n";# class="patch"43754376$patch_number++;4377}43784379if($patch_number==0) {4380if(@hash_parents>1) {4381print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4382}else{4383print"<div class=\"diff nodifferences\">No differences found</div>\n";4384}4385}43864387print"</div>\n";# class="patchset"4388}43894390# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43914392# fills project list info (age, description, owner, forks) for each4393# project in the list, removing invalid projects from returned list4394# NOTE: modifies $projlist, but does not remove entries from it4395sub fill_project_list_info {4396my($projlist,$check_forks) =@_;4397my@projects;43984399my$show_ctags= gitweb_check_feature('ctags');4400 PROJECT:4401foreachmy$pr(@$projlist) {4402my(@activity) = git_get_last_activity($pr->{'path'});4403unless(@activity) {4404next PROJECT;4405}4406($pr->{'age'},$pr->{'age_string'}) =@activity;4407if(!defined$pr->{'descr'}) {4408my$descr= git_get_project_description($pr->{'path'}) ||"";4409$descr= to_utf8($descr);4410$pr->{'descr_long'} =$descr;4411$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4412}4413if(!defined$pr->{'owner'}) {4414$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4415}4416if($check_forks) {4417my$pname=$pr->{'path'};4418if(($pname=~s/\.git$//) &&4419($pname!~/\/$/) &&4420(-d "$projectroot/$pname")) {4421$pr->{'forks'} ="-d$projectroot/$pname";4422}else{4423$pr->{'forks'} =0;4424}4425}4426$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4427push@projects,$pr;4428}44294430return@projects;4431}44324433# print 'sort by' <th> element, generating 'sort by $name' replay link4434# if that order is not selected4435sub print_sort_th {4436print format_sort_th(@_);4437}44384439sub format_sort_th {4440my($name,$order,$header) =@_;4441my$sort_th="";4442$header||=ucfirst($name);44434444if($ordereq$name) {4445$sort_th.="<th>$header</th>\n";4446}else{4447$sort_th.="<th>".4448$cgi->a({-href => href(-replay=>1, order=>$name),4449-class=>"header"},$header) .4450"</th>\n";4451}44524453return$sort_th;4454}44554456sub git_project_list_body {4457# actually uses global variable $project4458my($projlist,$order,$from,$to,$extra,$no_header) =@_;44594460my$check_forks= gitweb_check_feature('forks');4461my@projects= fill_project_list_info($projlist,$check_forks);44624463$order||=$default_projects_order;4464$from=0unlessdefined$from;4465$to=$#projectsif(!defined$to||$#projects<$to);44664467my%order_info= (4468 project => { key =>'path', type =>'str'},4469 descr => { key =>'descr_long', type =>'str'},4470 owner => { key =>'owner', type =>'str'},4471 age => { key =>'age', type =>'num'}4472);4473my$oi=$order_info{$order};4474if($oi->{'type'}eq'str') {4475@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4476}else{4477@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4478}44794480my$show_ctags= gitweb_check_feature('ctags');4481if($show_ctags) {4482my%ctags;4483foreachmy$p(@projects) {4484foreachmy$ct(keys%{$p->{'ctags'}}) {4485$ctags{$ct} +=$p->{'ctags'}->{$ct};4486}4487}4488my$cloud= git_populate_project_tagcloud(\%ctags);4489print git_show_project_tagcloud($cloud,64);4490}44914492print"<table class=\"project_list\">\n";4493unless($no_header) {4494print"<tr>\n";4495if($check_forks) {4496print"<th></th>\n";4497}4498 print_sort_th('project',$order,'Project');4499 print_sort_th('descr',$order,'Description');4500 print_sort_th('owner',$order,'Owner');4501 print_sort_th('age',$order,'Last Change');4502print"<th></th>\n".# for links4503"</tr>\n";4504}4505my$alternate=1;4506my$tagfilter=$cgi->param('by_tag');4507for(my$i=$from;$i<=$to;$i++) {4508my$pr=$projects[$i];45094510next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4511next if$searchtextand not$pr->{'path'} =~/$searchtext/4512and not$pr->{'descr_long'} =~/$searchtext/;4513# Weed out forks or non-matching entries of search4514if($check_forks) {4515my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4516$forkbase="^$forkbase"if$forkbase;4517next ifnot$searchtextand not$tagfilterand$show_ctags4518and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4519}45204521if($alternate) {4522print"<tr class=\"dark\">\n";4523}else{4524print"<tr class=\"light\">\n";4525}4526$alternate^=1;4527if($check_forks) {4528print"<td>";4529if($pr->{'forks'}) {4530print"<!--$pr->{'forks'} -->\n";4531print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4532}4533print"</td>\n";4534}4535print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4536-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4537"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4538-class=>"list", -title =>$pr->{'descr_long'}},4539 esc_html($pr->{'descr'})) ."</td>\n".4540"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4541print"<td class=\"". age_class($pr->{'age'}) ."\">".4542(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4543"<td class=\"link\">".4544$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4545$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4546$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4547$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4548($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4549"</td>\n".4550"</tr>\n";4551}4552if(defined$extra) {4553print"<tr>\n";4554if($check_forks) {4555print"<td></td>\n";4556}4557print"<td colspan=\"5\">$extra</td>\n".4558"</tr>\n";4559}4560print"</table>\n";4561}45624563sub git_log_body {4564# uses global variable $project4565my($commitlist,$from,$to,$refs,$extra) =@_;45664567$from=0unlessdefined$from;4568$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45694570for(my$i=0;$i<=$to;$i++) {4571my%co= %{$commitlist->[$i]};4572next if!%co;4573my$commit=$co{'id'};4574my$ref= format_ref_marker($refs,$commit);4575my%ad= parse_date($co{'author_epoch'});4576 git_print_header_div('commit',4577"<span class=\"age\">$co{'age_string'}</span>".4578 esc_html($co{'title'}) .$ref,4579$commit);4580print"<div class=\"title_text\">\n".4581"<div class=\"log_link\">\n".4582$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4583" | ".4584$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4585" | ".4586$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4587"<br/>\n".4588"</div>\n";4589 git_print_authorship(\%co, -tag =>'span');4590print"<br/>\n</div>\n";45914592print"<div class=\"log_body\">\n";4593 git_print_log($co{'comment'}, -final_empty_line=>1);4594print"</div>\n";4595}4596if($extra) {4597print"<div class=\"page_nav\">\n";4598print"$extra\n";4599print"</div>\n";4600}4601}46024603sub git_shortlog_body {4604# uses global variable $project4605my($commitlist,$from,$to,$refs,$extra) =@_;46064607$from=0unlessdefined$from;4608$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);46094610print"<table class=\"shortlog\">\n";4611my$alternate=1;4612for(my$i=$from;$i<=$to;$i++) {4613my%co= %{$commitlist->[$i]};4614my$commit=$co{'id'};4615my$ref= format_ref_marker($refs,$commit);4616if($alternate) {4617print"<tr class=\"dark\">\n";4618}else{4619print"<tr class=\"light\">\n";4620}4621$alternate^=1;4622# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4623print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4624 format_author_html('td', \%co,10) ."<td>";4625print format_subject_html($co{'title'},$co{'title_short'},4626 href(action=>"commit", hash=>$commit),$ref);4627print"</td>\n".4628"<td class=\"link\">".4629$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4630$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4631$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4632my$snapshot_links= format_snapshot_links($commit);4633if(defined$snapshot_links) {4634print" | ".$snapshot_links;4635}4636print"</td>\n".4637"</tr>\n";4638}4639if(defined$extra) {4640print"<tr>\n".4641"<td colspan=\"4\">$extra</td>\n".4642"</tr>\n";4643}4644print"</table>\n";4645}46464647sub git_history_body {4648# Warning: assumes constant type (blob or tree) during history4649my($commitlist,$from,$to,$refs,$extra,4650$file_name,$file_hash,$ftype) =@_;46514652$from=0unlessdefined$from;4653$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});46544655print"<table class=\"history\">\n";4656my$alternate=1;4657for(my$i=$from;$i<=$to;$i++) {4658my%co= %{$commitlist->[$i]};4659if(!%co) {4660next;4661}4662my$commit=$co{'id'};46634664my$ref= format_ref_marker($refs,$commit);46654666if($alternate) {4667print"<tr class=\"dark\">\n";4668}else{4669print"<tr class=\"light\">\n";4670}4671$alternate^=1;4672print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4673# shortlog: format_author_html('td', \%co, 10)4674 format_author_html('td', \%co,15,3) ."<td>";4675# originally git_history used chop_str($co{'title'}, 50)4676print format_subject_html($co{'title'},$co{'title_short'},4677 href(action=>"commit", hash=>$commit),$ref);4678print"</td>\n".4679"<td class=\"link\">".4680$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4681$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");46824683if($ftypeeq'blob') {4684my$blob_current=$file_hash;4685my$blob_parent= git_get_hash_by_path($commit,$file_name);4686if(defined$blob_current&&defined$blob_parent&&4687$blob_currentne$blob_parent) {4688print" | ".4689$cgi->a({-href => href(action=>"blobdiff",4690 hash=>$blob_current, hash_parent=>$blob_parent,4691 hash_base=>$hash_base, hash_parent_base=>$commit,4692 file_name=>$file_name)},4693"diff to current");4694}4695}4696print"</td>\n".4697"</tr>\n";4698}4699if(defined$extra) {4700print"<tr>\n".4701"<td colspan=\"4\">$extra</td>\n".4702"</tr>\n";4703}4704print"</table>\n";4705}47064707sub git_tags_body {4708# uses global variable $project4709my($taglist,$from,$to,$extra) =@_;4710$from=0unlessdefined$from;4711$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);47124713print"<table class=\"tags\">\n";4714my$alternate=1;4715for(my$i=$from;$i<=$to;$i++) {4716my$entry=$taglist->[$i];4717my%tag=%$entry;4718my$comment=$tag{'subject'};4719my$comment_short;4720if(defined$comment) {4721$comment_short= chop_str($comment,30,5);4722}4723if($alternate) {4724print"<tr class=\"dark\">\n";4725}else{4726print"<tr class=\"light\">\n";4727}4728$alternate^=1;4729if(defined$tag{'age'}) {4730print"<td><i>$tag{'age'}</i></td>\n";4731}else{4732print"<td></td>\n";4733}4734print"<td>".4735$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4736-class=>"list name"}, esc_html($tag{'name'})) .4737"</td>\n".4738"<td>";4739if(defined$comment) {4740print format_subject_html($comment,$comment_short,4741 href(action=>"tag", hash=>$tag{'id'}));4742}4743print"</td>\n".4744"<td class=\"selflink\">";4745if($tag{'type'}eq"tag") {4746print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4747}else{4748print" ";4749}4750print"</td>\n".4751"<td class=\"link\">"." | ".4752$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4753if($tag{'reftype'}eq"commit") {4754print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4755" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4756}elsif($tag{'reftype'}eq"blob") {4757print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4758}4759print"</td>\n".4760"</tr>";4761}4762if(defined$extra) {4763print"<tr>\n".4764"<td colspan=\"5\">$extra</td>\n".4765"</tr>\n";4766}4767print"</table>\n";4768}47694770sub git_heads_body {4771# uses global variable $project4772my($headlist,$head,$from,$to,$extra) =@_;4773$from=0unlessdefined$from;4774$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);47754776print"<table class=\"heads\">\n";4777my$alternate=1;4778for(my$i=$from;$i<=$to;$i++) {4779my$entry=$headlist->[$i];4780my%ref=%$entry;4781my$curr=$ref{'id'}eq$head;4782if($alternate) {4783print"<tr class=\"dark\">\n";4784}else{4785print"<tr class=\"light\">\n";4786}4787$alternate^=1;4788print"<td><i>$ref{'age'}</i></td>\n".4789($curr?"<td class=\"current_head\">":"<td>") .4790$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4791-class=>"list name"},esc_html($ref{'name'})) .4792"</td>\n".4793"<td class=\"link\">".4794$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4795$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4796$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4797"</td>\n".4798"</tr>";4799}4800if(defined$extra) {4801print"<tr>\n".4802"<td colspan=\"3\">$extra</td>\n".4803"</tr>\n";4804}4805print"</table>\n";4806}48074808sub git_search_grep_body {4809my($commitlist,$from,$to,$extra) =@_;4810$from=0unlessdefined$from;4811$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);48124813print"<table class=\"commit_search\">\n";4814my$alternate=1;4815for(my$i=$from;$i<=$to;$i++) {4816my%co= %{$commitlist->[$i]};4817if(!%co) {4818next;4819}4820my$commit=$co{'id'};4821if($alternate) {4822print"<tr class=\"dark\">\n";4823}else{4824print"<tr class=\"light\">\n";4825}4826$alternate^=1;4827print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4828 format_author_html('td', \%co,15,5) .4829"<td>".4830$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4831-class=>"list subject"},4832 chop_and_escape_str($co{'title'},50) ."<br/>");4833my$comment=$co{'comment'};4834foreachmy$line(@$comment) {4835if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4836my($lead,$match,$trail) = ($1,$2,$3);4837$match= chop_str($match,70,5,'center');4838my$contextlen=int((80-length($match))/2);4839$contextlen=30if($contextlen>30);4840$lead= chop_str($lead,$contextlen,10,'left');4841$trail= chop_str($trail,$contextlen,10,'right');48424843$lead= esc_html($lead);4844$match= esc_html($match);4845$trail= esc_html($trail);48464847print"$lead<span class=\"match\">$match</span>$trail<br />";4848}4849}4850print"</td>\n".4851"<td class=\"link\">".4852$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4853" | ".4854$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4855" | ".4856$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4857print"</td>\n".4858"</tr>\n";4859}4860if(defined$extra) {4861print"<tr>\n".4862"<td colspan=\"3\">$extra</td>\n".4863"</tr>\n";4864}4865print"</table>\n";4866}48674868## ======================================================================4869## ======================================================================4870## actions48714872sub git_project_list {4873my$order=$input_params{'order'};4874if(defined$order&&$order!~m/none|project|descr|owner|age/) {4875 die_error(400,"Unknown order parameter");4876}48774878my@list= git_get_projects_list();4879if(!@list) {4880 die_error(404,"No projects found");4881}48824883 git_header_html();4884if(defined$home_text&& -f $home_text) {4885print"<div class=\"index_include\">\n";4886 insert_file($home_text);4887print"</div>\n";4888}4889print$cgi->startform(-method=>"get") .4890"<p class=\"projsearch\">Search:\n".4891$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4892"</p>".4893$cgi->end_form() ."\n";4894 git_project_list_body(\@list,$order);4895 git_footer_html();4896}48974898sub git_forks {4899my$order=$input_params{'order'};4900if(defined$order&&$order!~m/none|project|descr|owner|age/) {4901 die_error(400,"Unknown order parameter");4902}49034904my@list= git_get_projects_list($project);4905if(!@list) {4906 die_error(404,"No forks found");4907}49084909 git_header_html();4910 git_print_page_nav('','');4911 git_print_header_div('summary',"$projectforks");4912 git_project_list_body(\@list,$order);4913 git_footer_html();4914}49154916sub git_project_index {4917my@projects= git_get_projects_list($project);49184919print$cgi->header(4920-type =>'text/plain',4921-charset =>'utf-8',4922-content_disposition =>'inline; filename="index.aux"');49234924foreachmy$pr(@projects) {4925if(!exists$pr->{'owner'}) {4926$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4927}49284929my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4930# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4931$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4932$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4933$path=~s/ /\+/g;4934$owner=~s/ /\+/g;49354936print"$path$owner\n";4937}4938}49394940sub git_summary {4941my$descr= git_get_project_description($project) ||"none";4942my%co= parse_commit("HEAD");4943my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4944my$head=$co{'id'};49454946my$owner= git_get_project_owner($project);49474948my$refs= git_get_references();4949# These get_*_list functions return one more to allow us to see if4950# there are more ...4951my@taglist= git_get_tags_list(16);4952my@headlist= git_get_heads_list(16);4953my@forklist;4954my$check_forks= gitweb_check_feature('forks');49554956if($check_forks) {4957@forklist= git_get_projects_list($project);4958}49594960 git_header_html();4961 git_print_page_nav('summary','',$head);49624963print"<div class=\"title\"> </div>\n";4964print"<table class=\"projects_list\">\n".4965"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4966"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4967if(defined$cd{'rfc2822'}) {4968print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4969}49704971# use per project git URL list in $projectroot/$project/cloneurl4972# or make project git URL from git base URL and project name4973my$url_tag="URL";4974my@url_list= git_get_project_url_list($project);4975@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4976foreachmy$git_url(@url_list) {4977next unless$git_url;4978print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4979$url_tag="";4980}49814982# Tag cloud4983my$show_ctags= gitweb_check_feature('ctags');4984if($show_ctags) {4985my$ctags= git_get_project_ctags($project);4986my$cloud= git_populate_project_tagcloud($ctags);4987print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4988print"</td>\n<td>"unless%$ctags;4989print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4990print"</td>\n<td>"if%$ctags;4991print git_show_project_tagcloud($cloud,48);4992print"</td></tr>";4993}49944995print"</table>\n";49964997# If XSS prevention is on, we don't include README.html.4998# TODO: Allow a readme in some safe format.4999if(!$prevent_xss&& -s "$projectroot/$project/README.html") {5000print"<div class=\"title\">readme</div>\n".5001"<div class=\"readme\">\n";5002 insert_file("$projectroot/$project/README.html");5003print"\n</div>\n";# class="readme"5004}50055006# we need to request one more than 16 (0..15) to check if5007# those 16 are all5008my@commitlist=$head? parse_commits($head,17) : ();5009if(@commitlist) {5010 git_print_header_div('shortlog');5011 git_shortlog_body(\@commitlist,0,15,$refs,5012$#commitlist<=15?undef:5013$cgi->a({-href => href(action=>"shortlog")},"..."));5014}50155016if(@taglist) {5017 git_print_header_div('tags');5018 git_tags_body(\@taglist,0,15,5019$#taglist<=15?undef:5020$cgi->a({-href => href(action=>"tags")},"..."));5021}50225023if(@headlist) {5024 git_print_header_div('heads');5025 git_heads_body(\@headlist,$head,0,15,5026$#headlist<=15?undef:5027$cgi->a({-href => href(action=>"heads")},"..."));5028}50295030if(@forklist) {5031 git_print_header_div('forks');5032 git_project_list_body(\@forklist,'age',0,15,5033$#forklist<=15?undef:5034$cgi->a({-href => href(action=>"forks")},"..."),5035'no_header');5036}50375038 git_footer_html();5039}50405041sub git_tag {5042my$head= git_get_head_hash($project);5043 git_header_html();5044 git_print_page_nav('','',$head,undef,$head);5045my%tag= parse_tag($hash);50465047if(!%tag) {5048 die_error(404,"Unknown tag object");5049}50505051 git_print_header_div('commit', esc_html($tag{'name'}),$hash);5052print"<div class=\"title_text\">\n".5053"<table class=\"object_header\">\n".5054"<tr>\n".5055"<td>object</td>\n".5056"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5057$tag{'object'}) ."</td>\n".5058"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5059$tag{'type'}) ."</td>\n".5060"</tr>\n";5061if(defined($tag{'author'})) {5062 git_print_authorship_rows(\%tag,'author');5063}5064print"</table>\n\n".5065"</div>\n";5066print"<div class=\"page_body\">";5067my$comment=$tag{'comment'};5068foreachmy$line(@$comment) {5069chomp$line;5070print esc_html($line, -nbsp=>1) ."<br/>\n";5071}5072print"</div>\n";5073 git_footer_html();5074}50755076sub git_blame_common {5077my$format=shift||'porcelain';5078if($formateq'porcelain'&&$cgi->param('js')) {5079$format='incremental';5080$action='blame_incremental';# for page title etc5081}50825083# permissions5084 gitweb_check_feature('blame')5085or die_error(403,"Blame view not allowed");50865087# error checking5088 die_error(400,"No file name given")unless$file_name;5089$hash_base||= git_get_head_hash($project);5090 die_error(404,"Couldn't find base commit")unless$hash_base;5091my%co= parse_commit($hash_base)5092or die_error(404,"Commit not found");5093my$ftype="blob";5094if(!defined$hash) {5095$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5096or die_error(404,"Error looking up file");5097}else{5098$ftype= git_get_type($hash);5099if($ftype!~"blob") {5100 die_error(400,"Object is not a blob");5101}5102}51035104my$fd;5105if($formateq'incremental') {5106# get file contents (as base)5107open$fd,"-|", git_cmd(),'cat-file','blob',$hash5108or die_error(500,"Open git-cat-file failed");5109}elsif($formateq'data') {5110# run git-blame --incremental5111open$fd,"-|", git_cmd(),"blame","--incremental",5112$hash_base,"--",$file_name5113or die_error(500,"Open git-blame --incremental failed");5114}else{5115# run git-blame --porcelain5116open$fd,"-|", git_cmd(),"blame",'-p',5117$hash_base,'--',$file_name5118or die_error(500,"Open git-blame --porcelain failed");5119}51205121# incremental blame data returns early5122if($formateq'data') {5123print$cgi->header(5124-type=>"text/plain", -charset =>"utf-8",5125-status=>"200 OK");5126local$| =1;# output autoflush5127printwhile<$fd>;5128close$fd5129or print"ERROR$!\n";51305131print'END';5132if(defined$t0&& gitweb_check_feature('timed')) {5133print' '.5134 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5135' '.$number_of_git_cmds;5136}5137print"\n";51385139return;5140}51415142# page header5143 git_header_html();5144my$formats_nav=5145$cgi->a({-href => href(action=>"blob", -replay=>1)},5146"blob") .5147" | ";5148if($formateq'incremental') {5149$formats_nav.=5150$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5151"blame") ." (non-incremental)";5152}else{5153$formats_nav.=5154$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5155"blame") ." (incremental)";5156}5157$formats_nav.=5158" | ".5159$cgi->a({-href => href(action=>"history", -replay=>1)},5160"history") .5161" | ".5162$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5163"HEAD");5164 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5165 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5166 git_print_page_path($file_name,$ftype,$hash_base);51675168# page body5169if($formateq'incremental') {5170print"<noscript>\n<div class=\"error\"><center><b>\n".5171"This page requires JavaScript to run.\nUse ".5172$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5173'this page').5174" instead.\n".5175"</b></center></div>\n</noscript>\n";51765177print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5178}51795180print qq!<div class="page_body">\n!;5181print qq!<div id="progress_info">.../ ...</div>\n!5182if($formateq'incremental');5183print qq!<table id="blame_table"class="blame" width="100%">\n!.5184#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5185 qq!<thead>\n!.5186 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5187 qq!</thead>\n!.5188 qq!<tbody>\n!;51895190my@rev_color=qw(light dark);5191my$num_colors=scalar(@rev_color);5192my$current_color=0;51935194if($formateq'incremental') {5195my$color_class=$rev_color[$current_color];51965197#contents of a file5198my$linenr=0;5199 LINE:5200while(my$line= <$fd>) {5201chomp$line;5202$linenr++;52035204print qq!<tr id="l$linenr"class="$color_class">!.5205 qq!<td class="sha1"><a href=""> </a></td>!.5206 qq!<td class="linenr">!.5207 qq!<a class="linenr" href="">$linenr</a></td>!;5208print qq!<td class="pre">! . esc_html($line) ."</td>\n";5209print qq!</tr>\n!;5210}52115212}else{# porcelain, i.e. ordinary blame5213my%metainfo= ();# saves information about commits52145215# blame data5216 LINE:5217while(my$line= <$fd>) {5218chomp$line;5219# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5220# no <lines in group> for subsequent lines in group of lines5221my($full_rev,$orig_lineno,$lineno,$group_size) =5222($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5223if(!exists$metainfo{$full_rev}) {5224$metainfo{$full_rev} = {'nprevious'=>0};5225}5226my$meta=$metainfo{$full_rev};5227my$data;5228while($data= <$fd>) {5229chomp$data;5230last if($data=~s/^\t//);# contents of line5231if($data=~/^(\S+)(?: (.*))?$/) {5232$meta->{$1} =$2unlessexists$meta->{$1};5233}5234if($data=~/^previous /) {5235$meta->{'nprevious'}++;5236}5237}5238my$short_rev=substr($full_rev,0,8);5239my$author=$meta->{'author'};5240my%date=5241 parse_date($meta->{'author-time'},$meta->{'author-tz'});5242my$date=$date{'iso-tz'};5243if($group_size) {5244$current_color= ($current_color+1) %$num_colors;5245}5246my$tr_class=$rev_color[$current_color];5247$tr_class.=' boundary'if(exists$meta->{'boundary'});5248$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5249$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5250print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5251if($group_size) {5252print"<td class=\"sha1\"";5253print" title=\"". esc_html($author) .",$date\"";5254print" rowspan=\"$group_size\""if($group_size>1);5255print">";5256print$cgi->a({-href => href(action=>"commit",5257 hash=>$full_rev,5258 file_name=>$file_name)},5259 esc_html($short_rev));5260if($group_size>=2) {5261my@author_initials= ($author=~/\b([[:upper:]])\B/g);5262if(@author_initials) {5263print"<br />".5264 esc_html(join('',@author_initials));5265# or join('.', ...)5266}5267}5268print"</td>\n";5269}5270# 'previous' <sha1 of parent commit> <filename at commit>5271if(exists$meta->{'previous'} &&5272$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5273$meta->{'parent'} =$1;5274$meta->{'file_parent'} = unquote($2);5275}5276my$linenr_commit=5277exists($meta->{'parent'}) ?5278$meta->{'parent'} :$full_rev;5279my$linenr_filename=5280exists($meta->{'file_parent'}) ?5281$meta->{'file_parent'} : unquote($meta->{'filename'});5282my$blamed= href(action =>'blame',5283 file_name =>$linenr_filename,5284 hash_base =>$linenr_commit);5285print"<td class=\"linenr\">";5286print$cgi->a({ -href =>"$blamed#l$orig_lineno",5287-class=>"linenr"},5288 esc_html($lineno));5289print"</td>";5290print"<td class=\"pre\">". esc_html($data) ."</td>\n";5291print"</tr>\n";5292}# end while52935294}52955296# footer5297print"</tbody>\n".5298"</table>\n";# class="blame"5299print"</div>\n";# class="blame_body"5300close$fd5301or print"Reading blob failed\n";53025303 git_footer_html();5304}53055306sub git_blame {5307 git_blame_common();5308}53095310sub git_blame_incremental {5311 git_blame_common('incremental');5312}53135314sub git_blame_data {5315 git_blame_common('data');5316}53175318sub git_tags {5319my$head= git_get_head_hash($project);5320 git_header_html();5321 git_print_page_nav('','',$head,undef,$head);5322 git_print_header_div('summary',$project);53235324my@tagslist= git_get_tags_list();5325if(@tagslist) {5326 git_tags_body(\@tagslist);5327}5328 git_footer_html();5329}53305331sub git_heads {5332my$head= git_get_head_hash($project);5333 git_header_html();5334 git_print_page_nav('','',$head,undef,$head);5335 git_print_header_div('summary',$project);53365337my@headslist= git_get_heads_list();5338if(@headslist) {5339 git_heads_body(\@headslist,$head);5340}5341 git_footer_html();5342}53435344sub git_blob_plain {5345my$type=shift;5346my$expires;53475348if(!defined$hash) {5349if(defined$file_name) {5350my$base=$hash_base|| git_get_head_hash($project);5351$hash= git_get_hash_by_path($base,$file_name,"blob")5352or die_error(404,"Cannot find file");5353}else{5354 die_error(400,"No file name defined");5355}5356}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5357# blobs defined by non-textual hash id's can be cached5358$expires="+1d";5359}53605361open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5362or die_error(500,"Open git-cat-file blob '$hash' failed");53635364# content-type (can include charset)5365$type= blob_contenttype($fd,$file_name,$type);53665367# "save as" filename, even when no $file_name is given5368my$save_as="$hash";5369if(defined$file_name) {5370$save_as=$file_name;5371}elsif($type=~m/^text\//) {5372$save_as.='.txt';5373}53745375# With XSS prevention on, blobs of all types except a few known safe5376# ones are served with "Content-Disposition: attachment" to make sure5377# they don't run in our security domain. For certain image types,5378# blob view writes an <img> tag referring to blob_plain view, and we5379# want to be sure not to break that by serving the image as an5380# attachment (though Firefox 3 doesn't seem to care).5381my$sandbox=$prevent_xss&&5382$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;53835384print$cgi->header(5385-type =>$type,5386-expires =>$expires,5387-content_disposition =>5388($sandbox?'attachment':'inline')5389.'; filename="'.$save_as.'"');5390local$/=undef;5391binmode STDOUT,':raw';5392print<$fd>;5393binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5394close$fd;5395}53965397sub git_blob {5398my$expires;53995400if(!defined$hash) {5401if(defined$file_name) {5402my$base=$hash_base|| git_get_head_hash($project);5403$hash= git_get_hash_by_path($base,$file_name,"blob")5404or die_error(404,"Cannot find file");5405}else{5406 die_error(400,"No file name defined");5407}5408}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5409# blobs defined by non-textual hash id's can be cached5410$expires="+1d";5411}54125413my$have_blame= gitweb_check_feature('blame');5414open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5415or die_error(500,"Couldn't cat$file_name,$hash");5416my$mimetype= blob_mimetype($fd,$file_name);5417# use 'blob_plain' (aka 'raw') view for files that cannot be displayed5418if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5419close$fd;5420return git_blob_plain($mimetype);5421}5422# we can have blame only for text/* mimetype5423$have_blame&&= ($mimetype=~m!^text/!);54245425my$highlight= gitweb_check_feature('highlight');5426my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);5427$fd= run_highlighter($fd,$highlight,$syntax)5428if$syntax;54295430 git_header_html(undef,$expires);5431my$formats_nav='';5432if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5433if(defined$file_name) {5434if($have_blame) {5435$formats_nav.=5436$cgi->a({-href => href(action=>"blame", -replay=>1)},5437"blame") .5438" | ";5439}5440$formats_nav.=5441$cgi->a({-href => href(action=>"history", -replay=>1)},5442"history") .5443" | ".5444$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5445"raw") .5446" | ".5447$cgi->a({-href => href(action=>"blob",5448 hash_base=>"HEAD", file_name=>$file_name)},5449"HEAD");5450}else{5451$formats_nav.=5452$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5453"raw");5454}5455 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5456 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5457}else{5458print"<div class=\"page_nav\">\n".5459"<br/><br/></div>\n".5460"<div class=\"title\">$hash</div>\n";5461}5462 git_print_page_path($file_name,"blob",$hash_base);5463print"<div class=\"page_body\">\n";5464if($mimetype=~m!^image/!) {5465print qq!<img type="$mimetype"!;5466if($file_name) {5467print qq! alt="$file_name" title="$file_name"!;5468}5469print qq! src="! .5470 href(action=>"blob_plain", hash=>$hash,5471 hash_base=>$hash_base, file_name=>$file_name) .5472 qq!"/>\n!;5473}else{5474my$nr;5475while(my$line= <$fd>) {5476chomp$line;5477$nr++;5478$line= untabify($line);5479printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,5480$nr, href(-replay =>1),$nr,$nr,$syntax?$line: esc_html($line, -nbsp=>1);5481}5482}5483close$fd5484or print"Reading blob failed.\n";5485print"</div>";5486 git_footer_html();5487}54885489sub git_tree {5490if(!defined$hash_base) {5491$hash_base="HEAD";5492}5493if(!defined$hash) {5494if(defined$file_name) {5495$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5496}else{5497$hash=$hash_base;5498}5499}5500 die_error(404,"No such tree")unlessdefined($hash);55015502my$show_sizes= gitweb_check_feature('show-sizes');5503my$have_blame= gitweb_check_feature('blame');55045505my@entries= ();5506{5507local$/="\0";5508open my$fd,"-|", git_cmd(),"ls-tree",'-z',5509($show_sizes?'-l': ()),@extra_options,$hash5510or die_error(500,"Open git-ls-tree failed");5511@entries=map{chomp;$_} <$fd>;5512close$fd5513or die_error(404,"Reading tree failed");5514}55155516my$refs= git_get_references();5517my$ref= format_ref_marker($refs,$hash_base);5518 git_header_html();5519my$basedir='';5520if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5521my@views_nav= ();5522if(defined$file_name) {5523push@views_nav,5524$cgi->a({-href => href(action=>"history", -replay=>1)},5525"history"),5526$cgi->a({-href => href(action=>"tree",5527 hash_base=>"HEAD", file_name=>$file_name)},5528"HEAD"),5529}5530my$snapshot_links= format_snapshot_links($hash);5531if(defined$snapshot_links) {5532# FIXME: Should be available when we have no hash base as well.5533push@views_nav,$snapshot_links;5534}5535 git_print_page_nav('tree','',$hash_base,undef,undef,5536join(' | ',@views_nav));5537 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5538}else{5539undef$hash_base;5540print"<div class=\"page_nav\">\n";5541print"<br/><br/></div>\n";5542print"<div class=\"title\">$hash</div>\n";5543}5544if(defined$file_name) {5545$basedir=$file_name;5546if($basedirne''&&substr($basedir, -1)ne'/') {5547$basedir.='/';5548}5549 git_print_page_path($file_name,'tree',$hash_base);5550}5551print"<div class=\"page_body\">\n";5552print"<table class=\"tree\">\n";5553my$alternate=1;5554# '..' (top directory) link if possible5555if(defined$hash_base&&5556defined$file_name&&$file_name=~m![^/]+$!) {5557if($alternate) {5558print"<tr class=\"dark\">\n";5559}else{5560print"<tr class=\"light\">\n";5561}5562$alternate^=1;55635564my$up=$file_name;5565$up=~s!/?[^/]+$!!;5566undef$upunless$up;5567# based on git_print_tree_entry5568print'<td class="mode">'. mode_str('040000') ."</td>\n";5569print'<td class="size"> </td>'."\n"if$show_sizes;5570print'<td class="list">';5571print$cgi->a({-href => href(action=>"tree",5572 hash_base=>$hash_base,5573 file_name=>$up)},5574"..");5575print"</td>\n";5576print"<td class=\"link\"></td>\n";55775578print"</tr>\n";5579}5580foreachmy$line(@entries) {5581my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);55825583if($alternate) {5584print"<tr class=\"dark\">\n";5585}else{5586print"<tr class=\"light\">\n";5587}5588$alternate^=1;55895590 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);55915592print"</tr>\n";5593}5594print"</table>\n".5595"</div>";5596 git_footer_html();5597}55985599sub snapshot_name {5600my($project,$hash) =@_;56015602# path/to/project.git -> project5603# path/to/project/.git -> project5604my$name= to_utf8($project);5605$name=~ s,([^/])/*\.git$,$1,;5606$name= basename($name);5607# sanitize name5608$name=~s/[[:cntrl:]]/?/g;56095610my$ver=$hash;5611if($hash=~/^[0-9a-fA-F]+$/) {5612# shorten SHA-1 hash5613my$full_hash= git_get_full_hash($project,$hash);5614if($full_hash=~/^$hash/&&length($hash) >7) {5615$ver= git_get_short_hash($project,$hash);5616}5617}elsif($hash=~m!^refs/tags/(.*)$!) {5618# tags don't need shortened SHA-1 hash5619$ver=$1;5620}else{5621# branches and other need shortened SHA-1 hash5622if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5623$ver=$1;5624}5625$ver.='-'. git_get_short_hash($project,$hash);5626}5627# in case of hierarchical branch names5628$ver=~s!/!.!g;56295630# name = project-version_string5631$name="$name-$ver";56325633returnwantarray? ($name,$name) :$name;5634}56355636sub git_snapshot {5637my$format=$input_params{'snapshot_format'};5638if(!@snapshot_fmts) {5639 die_error(403,"Snapshots not allowed");5640}5641# default to first supported snapshot format5642$format||=$snapshot_fmts[0];5643if($format!~m/^[a-z0-9]+$/) {5644 die_error(400,"Invalid snapshot format parameter");5645}elsif(!exists($known_snapshot_formats{$format})) {5646 die_error(400,"Unknown snapshot format");5647}elsif($known_snapshot_formats{$format}{'disabled'}) {5648 die_error(403,"Snapshot format not allowed");5649}elsif(!grep($_eq$format,@snapshot_fmts)) {5650 die_error(403,"Unsupported snapshot format");5651}56525653my$type= git_get_type("$hash^{}");5654if(!$type) {5655 die_error(404,'Object does not exist');5656}elsif($typeeq'blob') {5657 die_error(400,'Object is not a tree-ish');5658}56595660my($name,$prefix) = snapshot_name($project,$hash);5661my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5662my$cmd= quote_command(5663 git_cmd(),'archive',5664"--format=$known_snapshot_formats{$format}{'format'}",5665"--prefix=$prefix/",$hash);5666if(exists$known_snapshot_formats{$format}{'compressor'}) {5667$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5668}56695670$filename=~s/(["\\])/\\$1/g;5671print$cgi->header(5672-type =>$known_snapshot_formats{$format}{'type'},5673-content_disposition =>'inline; filename="'.$filename.'"',5674-status =>'200 OK');56755676open my$fd,"-|",$cmd5677or die_error(500,"Execute git-archive failed");5678binmode STDOUT,':raw';5679print<$fd>;5680binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5681close$fd;5682}56835684sub git_log_generic {5685my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;56865687my$head= git_get_head_hash($project);5688if(!defined$base) {5689$base=$head;5690}5691if(!defined$page) {5692$page=0;5693}5694my$refs= git_get_references();56955696my$commit_hash=$base;5697if(defined$parent) {5698$commit_hash="$parent..$base";5699}5700my@commitlist=5701 parse_commits($commit_hash,101, (100*$page),5702defined$file_name? ($file_name,"--full-history") : ());57035704my$ftype;5705if(!defined$file_hash&&defined$file_name) {5706# some commits could have deleted file in question,5707# and not have it in tree, but one of them has to have it5708for(my$i=0;$i<@commitlist;$i++) {5709$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5710last ifdefined$file_hash;5711}5712}5713if(defined$file_hash) {5714$ftype= git_get_type($file_hash);5715}5716if(defined$file_name&& !defined$ftype) {5717 die_error(500,"Unknown type of object");5718}5719my%co;5720if(defined$file_name) {5721%co= parse_commit($base)5722or die_error(404,"Unknown commit object");5723}572457255726my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5727my$next_link='';5728if($#commitlist>=100) {5729$next_link=5730$cgi->a({-href => href(-replay=>1, page=>$page+1),5731-accesskey =>"n", -title =>"Alt-n"},"next");5732}5733my$patch_max= gitweb_get_feature('patches');5734if($patch_max&& !defined$file_name) {5735if($patch_max<0||@commitlist<=$patch_max) {5736$paging_nav.=" ⋅ ".5737$cgi->a({-href => href(action=>"patches", -replay=>1)},5738"patches");5739}5740}57415742 git_header_html();5743 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5744if(defined$file_name) {5745 git_print_header_div('commit', esc_html($co{'title'}),$base);5746}else{5747 git_print_header_div('summary',$project)5748}5749 git_print_page_path($file_name,$ftype,$hash_base)5750if(defined$file_name);57515752$body_subr->(\@commitlist,0,99,$refs,$next_link,5753$file_name,$file_hash,$ftype);57545755 git_footer_html();5756}57575758sub git_log {5759 git_log_generic('log', \&git_log_body,5760$hash,$hash_parent);5761}57625763sub git_commit {5764$hash||=$hash_base||"HEAD";5765my%co= parse_commit($hash)5766or die_error(404,"Unknown commit object");57675768my$parent=$co{'parent'};5769my$parents=$co{'parents'};# listref57705771# we need to prepare $formats_nav before any parameter munging5772my$formats_nav;5773if(!defined$parent) {5774# --root commitdiff5775$formats_nav.='(initial)';5776}elsif(@$parents==1) {5777# single parent commit5778$formats_nav.=5779'(parent: '.5780$cgi->a({-href => href(action=>"commit",5781 hash=>$parent)},5782 esc_html(substr($parent,0,7))) .5783')';5784}else{5785# merge commit5786$formats_nav.=5787'(merge: '.5788join(' ',map{5789$cgi->a({-href => href(action=>"commit",5790 hash=>$_)},5791 esc_html(substr($_,0,7)));5792}@$parents) .5793')';5794}5795if(gitweb_check_feature('patches') &&@$parents<=1) {5796$formats_nav.=" | ".5797$cgi->a({-href => href(action=>"patch", -replay=>1)},5798"patch");5799}58005801if(!defined$parent) {5802$parent="--root";5803}5804my@difftree;5805open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5806@diff_opts,5807(@$parents<=1?$parent:'-c'),5808$hash,"--"5809or die_error(500,"Open git-diff-tree failed");5810@difftree=map{chomp;$_} <$fd>;5811close$fdor die_error(404,"Reading git-diff-tree failed");58125813# non-textual hash id's can be cached5814my$expires;5815if($hash=~m/^[0-9a-fA-F]{40}$/) {5816$expires="+1d";5817}5818my$refs= git_get_references();5819my$ref= format_ref_marker($refs,$co{'id'});58205821 git_header_html(undef,$expires);5822 git_print_page_nav('commit','',5823$hash,$co{'tree'},$hash,5824$formats_nav);58255826if(defined$co{'parent'}) {5827 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5828}else{5829 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5830}5831print"<div class=\"title_text\">\n".5832"<table class=\"object_header\">\n";5833 git_print_authorship_rows(\%co);5834print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5835print"<tr>".5836"<td>tree</td>".5837"<td class=\"sha1\">".5838$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5839class=>"list"},$co{'tree'}) .5840"</td>".5841"<td class=\"link\">".5842$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5843"tree");5844my$snapshot_links= format_snapshot_links($hash);5845if(defined$snapshot_links) {5846print" | ".$snapshot_links;5847}5848print"</td>".5849"</tr>\n";58505851foreachmy$par(@$parents) {5852print"<tr>".5853"<td>parent</td>".5854"<td class=\"sha1\">".5855$cgi->a({-href => href(action=>"commit", hash=>$par),5856class=>"list"},$par) .5857"</td>".5858"<td class=\"link\">".5859$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5860" | ".5861$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5862"</td>".5863"</tr>\n";5864}5865print"</table>".5866"</div>\n";58675868print"<div class=\"page_body\">\n";5869 git_print_log($co{'comment'});5870print"</div>\n";58715872 git_difftree_body(\@difftree,$hash,@$parents);58735874 git_footer_html();5875}58765877sub git_object {5878# object is defined by:5879# - hash or hash_base alone5880# - hash_base and file_name5881my$type;58825883# - hash or hash_base alone5884if($hash|| ($hash_base&& !defined$file_name)) {5885my$object_id=$hash||$hash_base;58865887open my$fd,"-|", quote_command(5888 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5889or die_error(404,"Object does not exist");5890$type= <$fd>;5891chomp$type;5892close$fd5893or die_error(404,"Object does not exist");58945895# - hash_base and file_name5896}elsif($hash_base&&defined$file_name) {5897$file_name=~ s,/+$,,;58985899system(git_cmd(),"cat-file",'-e',$hash_base) ==05900or die_error(404,"Base object does not exist");59015902# here errors should not hapen5903open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5904or die_error(500,"Open git-ls-tree failed");5905my$line= <$fd>;5906close$fd;59075908#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5909unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5910 die_error(404,"File or directory for given base does not exist");5911}5912$type=$2;5913$hash=$3;5914}else{5915 die_error(400,"Not enough information to find object");5916}59175918print$cgi->redirect(-uri => href(action=>$type, -full=>1,5919 hash=>$hash, hash_base=>$hash_base,5920 file_name=>$file_name),5921-status =>'302 Found');5922}59235924sub git_blobdiff {5925my$format=shift||'html';59265927my$fd;5928my@difftree;5929my%diffinfo;5930my$expires;59315932# preparing $fd and %diffinfo for git_patchset_body5933# new style URI5934if(defined$hash_base&&defined$hash_parent_base) {5935if(defined$file_name) {5936# read raw output5937open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5938$hash_parent_base,$hash_base,5939"--", (defined$file_parent?$file_parent: ()),$file_name5940or die_error(500,"Open git-diff-tree failed");5941@difftree=map{chomp;$_} <$fd>;5942close$fd5943or die_error(404,"Reading git-diff-tree failed");5944@difftree5945or die_error(404,"Blob diff not found");59465947}elsif(defined$hash&&5948$hash=~/[0-9a-fA-F]{40}/) {5949# try to find filename from $hash59505951# read filtered raw output5952open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5953$hash_parent_base,$hash_base,"--"5954or die_error(500,"Open git-diff-tree failed");5955@difftree=5956# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5957# $hash == to_id5958grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5959map{chomp;$_} <$fd>;5960close$fd5961or die_error(404,"Reading git-diff-tree failed");5962@difftree5963or die_error(404,"Blob diff not found");59645965}else{5966 die_error(400,"Missing one of the blob diff parameters");5967}59685969if(@difftree>1) {5970 die_error(400,"Ambiguous blob diff specification");5971}59725973%diffinfo= parse_difftree_raw_line($difftree[0]);5974$file_parent||=$diffinfo{'from_file'} ||$file_name;5975$file_name||=$diffinfo{'to_file'};59765977$hash_parent||=$diffinfo{'from_id'};5978$hash||=$diffinfo{'to_id'};59795980# non-textual hash id's can be cached5981if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5982$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5983$expires='+1d';5984}59855986# open patch output5987open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5988'-p', ($formateq'html'?"--full-index": ()),5989$hash_parent_base,$hash_base,5990"--", (defined$file_parent?$file_parent: ()),$file_name5991or die_error(500,"Open git-diff-tree failed");5992}59935994# old/legacy style URI -- not generated anymore since 1.4.3.5995if(!%diffinfo) {5996 die_error('404 Not Found',"Missing one of the blob diff parameters")5997}59985999# header6000if($formateq'html') {6001my$formats_nav=6002$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},6003"raw");6004 git_header_html(undef,$expires);6005if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6006 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6007 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6008}else{6009print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";6010print"<div class=\"title\">$hashvs$hash_parent</div>\n";6011}6012if(defined$file_name) {6013 git_print_page_path($file_name,"blob",$hash_base);6014}else{6015print"<div class=\"page_path\"></div>\n";6016}60176018}elsif($formateq'plain') {6019print$cgi->header(6020-type =>'text/plain',6021-charset =>'utf-8',6022-expires =>$expires,6023-content_disposition =>'inline; filename="'."$file_name".'.patch"');60246025print"X-Git-Url: ".$cgi->self_url() ."\n\n";60266027}else{6028 die_error(400,"Unknown blobdiff format");6029}60306031# patch6032if($formateq'html') {6033print"<div class=\"page_body\">\n";60346035 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);6036close$fd;60376038print"</div>\n";# class="page_body"6039 git_footer_html();60406041}else{6042while(my$line= <$fd>) {6043$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;6044$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;60456046print$line;60476048last if$line=~m!^\+\+\+!;6049}6050local$/=undef;6051print<$fd>;6052close$fd;6053}6054}60556056sub git_blobdiff_plain {6057 git_blobdiff('plain');6058}60596060sub git_commitdiff {6061my%params=@_;6062my$format=$params{-format} ||'html';60636064my($patch_max) = gitweb_get_feature('patches');6065if($formateq'patch') {6066 die_error(403,"Patch view not allowed")unless$patch_max;6067}60686069$hash||=$hash_base||"HEAD";6070my%co= parse_commit($hash)6071or die_error(404,"Unknown commit object");60726073# choose format for commitdiff for merge6074if(!defined$hash_parent&& @{$co{'parents'}} >1) {6075$hash_parent='--cc';6076}6077# we need to prepare $formats_nav before almost any parameter munging6078my$formats_nav;6079if($formateq'html') {6080$formats_nav=6081$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6082"raw");6083if($patch_max&& @{$co{'parents'}} <=1) {6084$formats_nav.=" | ".6085$cgi->a({-href => href(action=>"patch", -replay=>1)},6086"patch");6087}60886089if(defined$hash_parent&&6090$hash_parentne'-c'&&$hash_parentne'--cc') {6091# commitdiff with two commits given6092my$hash_parent_short=$hash_parent;6093if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6094$hash_parent_short=substr($hash_parent,0,7);6095}6096$formats_nav.=6097' (from';6098for(my$i=0;$i< @{$co{'parents'}};$i++) {6099if($co{'parents'}[$i]eq$hash_parent) {6100$formats_nav.=' parent '. ($i+1);6101last;6102}6103}6104$formats_nav.=': '.6105$cgi->a({-href => href(action=>"commitdiff",6106 hash=>$hash_parent)},6107 esc_html($hash_parent_short)) .6108')';6109}elsif(!$co{'parent'}) {6110# --root commitdiff6111$formats_nav.=' (initial)';6112}elsif(scalar@{$co{'parents'}} ==1) {6113# single parent commit6114$formats_nav.=6115' (parent: '.6116$cgi->a({-href => href(action=>"commitdiff",6117 hash=>$co{'parent'})},6118 esc_html(substr($co{'parent'},0,7))) .6119')';6120}else{6121# merge commit6122if($hash_parenteq'--cc') {6123$formats_nav.=' | '.6124$cgi->a({-href => href(action=>"commitdiff",6125 hash=>$hash, hash_parent=>'-c')},6126'combined');6127}else{# $hash_parent eq '-c'6128$formats_nav.=' | '.6129$cgi->a({-href => href(action=>"commitdiff",6130 hash=>$hash, hash_parent=>'--cc')},6131'compact');6132}6133$formats_nav.=6134' (merge: '.6135join(' ',map{6136$cgi->a({-href => href(action=>"commitdiff",6137 hash=>$_)},6138 esc_html(substr($_,0,7)));6139} @{$co{'parents'}} ) .6140')';6141}6142}61436144my$hash_parent_param=$hash_parent;6145if(!defined$hash_parent_param) {6146# --cc for multiple parents, --root for parentless6147$hash_parent_param=6148@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6149}61506151# read commitdiff6152my$fd;6153my@difftree;6154if($formateq'html') {6155open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6156"--no-commit-id","--patch-with-raw","--full-index",6157$hash_parent_param,$hash,"--"6158or die_error(500,"Open git-diff-tree failed");61596160while(my$line= <$fd>) {6161chomp$line;6162# empty line ends raw part of diff-tree output6163last unless$line;6164push@difftree,scalar parse_difftree_raw_line($line);6165}61666167}elsif($formateq'plain') {6168open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6169'-p',$hash_parent_param,$hash,"--"6170or die_error(500,"Open git-diff-tree failed");6171}elsif($formateq'patch') {6172# For commit ranges, we limit the output to the number of6173# patches specified in the 'patches' feature.6174# For single commits, we limit the output to a single patch,6175# diverging from the git-format-patch default.6176my@commit_spec= ();6177if($hash_parent) {6178if($patch_max>0) {6179push@commit_spec,"-$patch_max";6180}6181push@commit_spec,'-n',"$hash_parent..$hash";6182}else{6183if($params{-single}) {6184push@commit_spec,'-1';6185}else{6186if($patch_max>0) {6187push@commit_spec,"-$patch_max";6188}6189push@commit_spec,"-n";6190}6191push@commit_spec,'--root',$hash;6192}6193open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',6194'--stdout',@commit_spec6195or die_error(500,"Open git-format-patch failed");6196}else{6197 die_error(400,"Unknown commitdiff format");6198}61996200# non-textual hash id's can be cached6201my$expires;6202if($hash=~m/^[0-9a-fA-F]{40}$/) {6203$expires="+1d";6204}62056206# write commit message6207if($formateq'html') {6208my$refs= git_get_references();6209my$ref= format_ref_marker($refs,$co{'id'});62106211 git_header_html(undef,$expires);6212 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6213 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6214print"<div class=\"title_text\">\n".6215"<table class=\"object_header\">\n";6216 git_print_authorship_rows(\%co);6217print"</table>".6218"</div>\n";6219print"<div class=\"page_body\">\n";6220if(@{$co{'comment'}} >1) {6221print"<div class=\"log\">\n";6222 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6223print"</div>\n";# class="log"6224}62256226}elsif($formateq'plain') {6227my$refs= git_get_references("tags");6228my$tagname= git_get_rev_name_tags($hash);6229my$filename= basename($project) ."-$hash.patch";62306231print$cgi->header(6232-type =>'text/plain',6233-charset =>'utf-8',6234-expires =>$expires,6235-content_disposition =>'inline; filename="'."$filename".'"');6236my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6237print"From: ". to_utf8($co{'author'}) ."\n";6238print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6239print"Subject: ". to_utf8($co{'title'}) ."\n";62406241print"X-Git-Tag:$tagname\n"if$tagname;6242print"X-Git-Url: ".$cgi->self_url() ."\n\n";62436244foreachmy$line(@{$co{'comment'}}) {6245print to_utf8($line) ."\n";6246}6247print"---\n\n";6248}elsif($formateq'patch') {6249my$filename= basename($project) ."-$hash.patch";62506251print$cgi->header(6252-type =>'text/plain',6253-charset =>'utf-8',6254-expires =>$expires,6255-content_disposition =>'inline; filename="'."$filename".'"');6256}62576258# write patch6259if($formateq'html') {6260my$use_parents= !defined$hash_parent||6261$hash_parenteq'-c'||$hash_parenteq'--cc';6262 git_difftree_body(\@difftree,$hash,6263$use_parents? @{$co{'parents'}} :$hash_parent);6264print"<br/>\n";62656266 git_patchset_body($fd, \@difftree,$hash,6267$use_parents? @{$co{'parents'}} :$hash_parent);6268close$fd;6269print"</div>\n";# class="page_body"6270 git_footer_html();62716272}elsif($formateq'plain') {6273local$/=undef;6274print<$fd>;6275close$fd6276or print"Reading git-diff-tree failed\n";6277}elsif($formateq'patch') {6278local$/=undef;6279print<$fd>;6280close$fd6281or print"Reading git-format-patch failed\n";6282}6283}62846285sub git_commitdiff_plain {6286 git_commitdiff(-format =>'plain');6287}62886289# format-patch-style patches6290sub git_patch {6291 git_commitdiff(-format =>'patch', -single =>1);6292}62936294sub git_patches {6295 git_commitdiff(-format =>'patch');6296}62976298sub git_history {6299 git_log_generic('history', \&git_history_body,6300$hash_base,$hash_parent_base,6301$file_name,$hash);6302}63036304sub git_search {6305 gitweb_check_feature('search')or die_error(403,"Search is disabled");6306if(!defined$searchtext) {6307 die_error(400,"Text field is empty");6308}6309if(!defined$hash) {6310$hash= git_get_head_hash($project);6311}6312my%co= parse_commit($hash);6313if(!%co) {6314 die_error(404,"Unknown commit object");6315}6316if(!defined$page) {6317$page=0;6318}63196320$searchtype||='commit';6321if($searchtypeeq'pickaxe') {6322# pickaxe may take all resources of your box and run for several minutes6323# with every query - so decide by yourself how public you make this feature6324 gitweb_check_feature('pickaxe')6325or die_error(403,"Pickaxe is disabled");6326}6327if($searchtypeeq'grep') {6328 gitweb_check_feature('grep')6329or die_error(403,"Grep is disabled");6330}63316332 git_header_html();63336334if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6335my$greptype;6336if($searchtypeeq'commit') {6337$greptype="--grep=";6338}elsif($searchtypeeq'author') {6339$greptype="--author=";6340}elsif($searchtypeeq'committer') {6341$greptype="--committer=";6342}6343$greptype.=$searchtext;6344my@commitlist= parse_commits($hash,101, (100*$page),undef,6345$greptype,'--regexp-ignore-case',6346$search_use_regexp?'--extended-regexp':'--fixed-strings');63476348my$paging_nav='';6349if($page>0) {6350$paging_nav.=6351$cgi->a({-href => href(action=>"search", hash=>$hash,6352 searchtext=>$searchtext,6353 searchtype=>$searchtype)},6354"first");6355$paging_nav.=" ⋅ ".6356$cgi->a({-href => href(-replay=>1, page=>$page-1),6357-accesskey =>"p", -title =>"Alt-p"},"prev");6358}else{6359$paging_nav.="first";6360$paging_nav.=" ⋅ prev";6361}6362my$next_link='';6363if($#commitlist>=100) {6364$next_link=6365$cgi->a({-href => href(-replay=>1, page=>$page+1),6366-accesskey =>"n", -title =>"Alt-n"},"next");6367$paging_nav.=" ⋅$next_link";6368}else{6369$paging_nav.=" ⋅ next";6370}63716372if($#commitlist>=100) {6373}63746375 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6376 git_print_header_div('commit', esc_html($co{'title'}),$hash);6377 git_search_grep_body(\@commitlist,0,99,$next_link);6378}63796380if($searchtypeeq'pickaxe') {6381 git_print_page_nav('','',$hash,$co{'tree'},$hash);6382 git_print_header_div('commit', esc_html($co{'title'}),$hash);63836384print"<table class=\"pickaxe search\">\n";6385my$alternate=1;6386local$/="\n";6387open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6388'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6389($search_use_regexp?'--pickaxe-regex': ());6390undef%co;6391my@files;6392while(my$line= <$fd>) {6393chomp$line;6394next unless$line;63956396my%set= parse_difftree_raw_line($line);6397if(defined$set{'commit'}) {6398# finish previous commit6399if(%co) {6400print"</td>\n".6401"<td class=\"link\">".6402$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6403" | ".6404$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6405print"</td>\n".6406"</tr>\n";6407}64086409if($alternate) {6410print"<tr class=\"dark\">\n";6411}else{6412print"<tr class=\"light\">\n";6413}6414$alternate^=1;6415%co= parse_commit($set{'commit'});6416my$author= chop_and_escape_str($co{'author_name'},15,5);6417print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6418"<td><i>$author</i></td>\n".6419"<td>".6420$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6421-class=>"list subject"},6422 chop_and_escape_str($co{'title'},50) ."<br/>");6423}elsif(defined$set{'to_id'}) {6424next if($set{'to_id'} =~m/^0{40}$/);64256426print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6427 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6428-class=>"list"},6429"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6430"<br/>\n";6431}6432}6433close$fd;64346435# finish last commit (warning: repetition!)6436if(%co) {6437print"</td>\n".6438"<td class=\"link\">".6439$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6440" | ".6441$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6442print"</td>\n".6443"</tr>\n";6444}64456446print"</table>\n";6447}64486449if($searchtypeeq'grep') {6450 git_print_page_nav('','',$hash,$co{'tree'},$hash);6451 git_print_header_div('commit', esc_html($co{'title'}),$hash);64526453print"<table class=\"grep_search\">\n";6454my$alternate=1;6455my$matches=0;6456local$/="\n";6457open my$fd,"-|", git_cmd(),'grep','-n',6458$search_use_regexp? ('-E','-i') :'-F',6459$searchtext,$co{'tree'};6460my$lastfile='';6461while(my$line= <$fd>) {6462chomp$line;6463my($file,$lno,$ltext,$binary);6464last if($matches++>1000);6465if($line=~/^Binary file (.+) matches$/) {6466$file=$1;6467$binary=1;6468}else{6469(undef,$file,$lno,$ltext) =split(/:/,$line,4);6470}6471if($filene$lastfile) {6472$lastfileand print"</td></tr>\n";6473if($alternate++) {6474print"<tr class=\"dark\">\n";6475}else{6476print"<tr class=\"light\">\n";6477}6478print"<td class=\"list\">".6479$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6480 file_name=>"$file"),6481-class=>"list"}, esc_path($file));6482print"</td><td>\n";6483$lastfile=$file;6484}6485if($binary) {6486print"<div class=\"binary\">Binary file</div>\n";6487}else{6488$ltext= untabify($ltext);6489if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6490$ltext= esc_html($1, -nbsp=>1);6491$ltext.='<span class="match">';6492$ltext.= esc_html($2, -nbsp=>1);6493$ltext.='</span>';6494$ltext.= esc_html($3, -nbsp=>1);6495}else{6496$ltext= esc_html($ltext, -nbsp=>1);6497}6498print"<div class=\"pre\">".6499$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6500 file_name=>"$file").'#l'.$lno,6501-class=>"linenr"},sprintf('%4i',$lno))6502.' '.$ltext."</div>\n";6503}6504}6505if($lastfile) {6506print"</td></tr>\n";6507if($matches>1000) {6508print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6509}6510}else{6511print"<div class=\"diff nodifferences\">No matches found</div>\n";6512}6513close$fd;65146515print"</table>\n";6516}6517 git_footer_html();6518}65196520sub git_search_help {6521 git_header_html();6522 git_print_page_nav('','',$hash,$hash,$hash);6523print<<EOT;6524<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6525regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6526the pattern entered is recognized as the POSIX extended6527<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6528insensitive).</p>6529<dl>6530<dt><b>commit</b></dt>6531<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6532EOT6533my$have_grep= gitweb_check_feature('grep');6534if($have_grep) {6535print<<EOT;6536<dt><b>grep</b></dt>6537<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6538 a different one) are searched for the given pattern. On large trees, this search can take6539a while and put some strain on the server, so please use it with some consideration. Note that6540due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6541case-sensitive.</dd>6542EOT6543}6544print<<EOT;6545<dt><b>author</b></dt>6546<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6547<dt><b>committer</b></dt>6548<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6549EOT6550my$have_pickaxe= gitweb_check_feature('pickaxe');6551if($have_pickaxe) {6552print<<EOT;6553<dt><b>pickaxe</b></dt>6554<dd>All commits that caused the string to appear or disappear from any file (changes that6555added, removed or "modified" the string) will be listed. This search can take a while and6556takes a lot of strain on the server, so please use it wisely. Note that since you may be6557interested even in changes just changing the case as well, this search is case sensitive.</dd>6558EOT6559}6560print"</dl>\n";6561 git_footer_html();6562}65636564sub git_shortlog {6565 git_log_generic('shortlog', \&git_shortlog_body,6566$hash,$hash_parent);6567}65686569## ......................................................................6570## feeds (RSS, Atom; OPML)65716572sub git_feed {6573my$format=shift||'atom';6574my$have_blame= gitweb_check_feature('blame');65756576# Atom: http://www.atomenabled.org/developers/syndication/6577# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6578if($formatne'rss'&&$formatne'atom') {6579 die_error(400,"Unknown web feed format");6580}65816582# log/feed of current (HEAD) branch, log of given branch, history of file/directory6583my$head=$hash||'HEAD';6584my@commitlist= parse_commits($head,150,0,$file_name);65856586my%latest_commit;6587my%latest_date;6588my$content_type="application/$format+xml";6589if(defined$cgi->http('HTTP_ACCEPT') &&6590$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6591# browser (feed reader) prefers text/xml6592$content_type='text/xml';6593}6594if(defined($commitlist[0])) {6595%latest_commit= %{$commitlist[0]};6596my$latest_epoch=$latest_commit{'committer_epoch'};6597%latest_date= parse_date($latest_epoch);6598my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6599if(defined$if_modified) {6600my$since;6601if(eval{require HTTP::Date;1; }) {6602$since= HTTP::Date::str2time($if_modified);6603}elsif(eval{require Time::ParseDate;1; }) {6604$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6605}6606if(defined$since&&$latest_epoch<=$since) {6607print$cgi->header(6608-type =>$content_type,6609-charset =>'utf-8',6610-last_modified =>$latest_date{'rfc2822'},6611-status =>'304 Not Modified');6612return;6613}6614}6615print$cgi->header(6616-type =>$content_type,6617-charset =>'utf-8',6618-last_modified =>$latest_date{'rfc2822'});6619}else{6620print$cgi->header(6621-type =>$content_type,6622-charset =>'utf-8');6623}66246625# Optimization: skip generating the body if client asks only6626# for Last-Modified date.6627return if($cgi->request_method()eq'HEAD');66286629# header variables6630my$title="$site_name-$project/$action";6631my$feed_type='log';6632if(defined$hash) {6633$title.=" - '$hash'";6634$feed_type='branch log';6635if(defined$file_name) {6636$title.=" ::$file_name";6637$feed_type='history';6638}6639}elsif(defined$file_name) {6640$title.=" -$file_name";6641$feed_type='history';6642}6643$title.="$feed_type";6644my$descr= git_get_project_description($project);6645if(defined$descr) {6646$descr= esc_html($descr);6647}else{6648$descr="$project".6649($formateq'rss'?'RSS':'Atom') .6650" feed";6651}6652my$owner= git_get_project_owner($project);6653$owner= esc_html($owner);66546655#header6656my$alt_url;6657if(defined$file_name) {6658$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6659}elsif(defined$hash) {6660$alt_url= href(-full=>1, action=>"log", hash=>$hash);6661}else{6662$alt_url= href(-full=>1, action=>"summary");6663}6664print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6665if($formateq'rss') {6666print<<XML;6667<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6668<channel>6669XML6670print"<title>$title</title>\n".6671"<link>$alt_url</link>\n".6672"<description>$descr</description>\n".6673"<language>en</language>\n".6674# project owner is responsible for 'editorial' content6675"<managingEditor>$owner</managingEditor>\n";6676if(defined$logo||defined$favicon) {6677# prefer the logo to the favicon, since RSS6678# doesn't allow both6679my$img= esc_url($logo||$favicon);6680print"<image>\n".6681"<url>$img</url>\n".6682"<title>$title</title>\n".6683"<link>$alt_url</link>\n".6684"</image>\n";6685}6686if(%latest_date) {6687print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6688print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6689}6690print"<generator>gitweb v.$version/$git_version</generator>\n";6691}elsif($formateq'atom') {6692print<<XML;6693<feed xmlns="http://www.w3.org/2005/Atom">6694XML6695print"<title>$title</title>\n".6696"<subtitle>$descr</subtitle>\n".6697'<link rel="alternate" type="text/html" href="'.6698$alt_url.'" />'."\n".6699'<link rel="self" type="'.$content_type.'" href="'.6700$cgi->self_url() .'" />'."\n".6701"<id>". href(-full=>1) ."</id>\n".6702# use project owner for feed author6703"<author><name>$owner</name></author>\n";6704if(defined$favicon) {6705print"<icon>". esc_url($favicon) ."</icon>\n";6706}6707if(defined$logo_url) {6708# not twice as wide as tall: 72 x 27 pixels6709print"<logo>". esc_url($logo) ."</logo>\n";6710}6711if(!%latest_date) {6712# dummy date to keep the feed valid until commits trickle in:6713print"<updated>1970-01-01T00:00:00Z</updated>\n";6714}else{6715print"<updated>$latest_date{'iso-8601'}</updated>\n";6716}6717print"<generator version='$version/$git_version'>gitweb</generator>\n";6718}67196720# contents6721for(my$i=0;$i<=$#commitlist;$i++) {6722my%co= %{$commitlist[$i]};6723my$commit=$co{'id'};6724# we read 150, we always show 30 and the ones more recent than 48 hours6725if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6726last;6727}6728my%cd= parse_date($co{'author_epoch'});67296730# get list of changed files6731open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6732$co{'parent'} ||"--root",6733$co{'id'},"--", (defined$file_name?$file_name: ())6734ornext;6735my@difftree=map{chomp;$_} <$fd>;6736close$fd6737ornext;67386739# print element (entry, item)6740my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6741if($formateq'rss') {6742print"<item>\n".6743"<title>". esc_html($co{'title'}) ."</title>\n".6744"<author>". esc_html($co{'author'}) ."</author>\n".6745"<pubDate>$cd{'rfc2822'}</pubDate>\n".6746"<guid isPermaLink=\"true\">$co_url</guid>\n".6747"<link>$co_url</link>\n".6748"<description>". esc_html($co{'title'}) ."</description>\n".6749"<content:encoded>".6750"<![CDATA[\n";6751}elsif($formateq'atom') {6752print"<entry>\n".6753"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6754"<updated>$cd{'iso-8601'}</updated>\n".6755"<author>\n".6756" <name>". esc_html($co{'author_name'}) ."</name>\n";6757if($co{'author_email'}) {6758print" <email>". esc_html($co{'author_email'}) ."</email>\n";6759}6760print"</author>\n".6761# use committer for contributor6762"<contributor>\n".6763" <name>". esc_html($co{'committer_name'}) ."</name>\n";6764if($co{'committer_email'}) {6765print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6766}6767print"</contributor>\n".6768"<published>$cd{'iso-8601'}</published>\n".6769"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6770"<id>$co_url</id>\n".6771"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6772"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6773}6774my$comment=$co{'comment'};6775print"<pre>\n";6776foreachmy$line(@$comment) {6777$line= esc_html($line);6778print"$line\n";6779}6780print"</pre><ul>\n";6781foreachmy$difftree_line(@difftree) {6782my%difftree= parse_difftree_raw_line($difftree_line);6783next if!$difftree{'from_id'};67846785my$file=$difftree{'file'} ||$difftree{'to_file'};67866787print"<li>".6788"[".6789$cgi->a({-href => href(-full=>1, action=>"blobdiff",6790 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6791 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6792 file_name=>$file, file_parent=>$difftree{'from_file'}),6793-title =>"diff"},'D');6794if($have_blame) {6795print$cgi->a({-href => href(-full=>1, action=>"blame",6796 file_name=>$file, hash_base=>$commit),6797-title =>"blame"},'B');6798}6799# if this is not a feed of a file history6800if(!defined$file_name||$file_namene$file) {6801print$cgi->a({-href => href(-full=>1, action=>"history",6802 file_name=>$file, hash=>$commit),6803-title =>"history"},'H');6804}6805$file= esc_path($file);6806print"] ".6807"$file</li>\n";6808}6809if($formateq'rss') {6810print"</ul>]]>\n".6811"</content:encoded>\n".6812"</item>\n";6813}elsif($formateq'atom') {6814print"</ul>\n</div>\n".6815"</content>\n".6816"</entry>\n";6817}6818}68196820# end of feed6821if($formateq'rss') {6822print"</channel>\n</rss>\n";6823}elsif($formateq'atom') {6824print"</feed>\n";6825}6826}68276828sub git_rss {6829 git_feed('rss');6830}68316832sub git_atom {6833 git_feed('atom');6834}68356836sub git_opml {6837my@list= git_get_projects_list();68386839print$cgi->header(6840-type =>'text/xml',6841-charset =>'utf-8',6842-content_disposition =>'inline; filename="opml.xml"');68436844print<<XML;6845<?xml version="1.0" encoding="utf-8"?>6846<opml version="1.0">6847<head>6848 <title>$site_nameOPML Export</title>6849</head>6850<body>6851<outline text="git RSS feeds">6852XML68536854foreachmy$pr(@list) {6855my%proj=%$pr;6856my$head= git_get_head_hash($proj{'path'});6857if(!defined$head) {6858next;6859}6860$git_dir="$projectroot/$proj{'path'}";6861my%co= parse_commit($head);6862if(!%co) {6863next;6864}68656866my$path= esc_html(chop_str($proj{'path'},25,5));6867my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6868my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6869print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6870}6871print<<XML;6872</outline>6873</body>6874</opml>6875XML6876}