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 set_message); 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# custom error handler: 'die <message>' is Internal Server Error 969sub handle_errors_html { 970my$msg=shift;# it is already HTML escaped 971 972# to avoid infinite loop where error occurs in die_error, 973# change handler to default handler, disabling handle_errors_html 974 set_message("Error occured when inside die_error:\n$msg"); 975 976# you cannot jump out of die_error when called as error handler; 977# the subroutine set via CGI::Carp::set_message is called _after_ 978# HTTP headers are already written, so it cannot write them itself 979 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1); 980} 981set_message(\&handle_errors_html); 982 983# dispatch 984if(!defined$action) { 985if(defined$hash) { 986$action= git_get_type($hash); 987}elsif(defined$hash_base&&defined$file_name) { 988$action= git_get_type("$hash_base:$file_name"); 989}elsif(defined$project) { 990$action='summary'; 991}else{ 992$action='project_list'; 993} 994} 995if(!defined($actions{$action})) { 996 die_error(400,"Unknown action"); 997} 998if($action!~m/^(?:opml|project_list|project_index)$/&& 999!$project) {1000 die_error(400,"Project needed");1001}1002$actions{$action}->();10031004DONE_GITWEB:1005if(defined caller) {1006# wrapped in a subroutine processing requests,1007# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1008return;1009}else{1010# pure CGI script, serving single request1011exit;1012}10131014## ======================================================================1015## action links10161017# possible values of extra options1018# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1019# -replay => 1 - start from a current view (replay with modifications)1020# -path_info => 0|1 - don't use/use path_info URL (if possible)1021sub href {1022my%params=@_;1023# default is to use -absolute url() i.e. $my_uri1024my$href=$params{-full} ?$my_url:$my_uri;10251026$params{'project'} =$projectunlessexists$params{'project'};10271028if($params{-replay}) {1029while(my($name,$symbol) =each%cgi_param_mapping) {1030if(!exists$params{$name}) {1031$params{$name} =$input_params{$name};1032}1033}1034}10351036my$use_pathinfo= gitweb_check_feature('pathinfo');1037if(defined$params{'project'} &&1038(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1039# try to put as many parameters as possible in PATH_INFO:1040# - project name1041# - action1042# - hash_parent or hash_parent_base:/file_parent1043# - hash or hash_base:/filename1044# - the snapshot_format as an appropriate suffix10451046# When the script is the root DirectoryIndex for the domain,1047# $href here would be something like http://gitweb.example.com/1048# Thus, we strip any trailing / from $href, to spare us double1049# slashes in the final URL1050$href=~ s,/$,,;10511052# Then add the project name, if present1053$href.="/".esc_url($params{'project'});1054delete$params{'project'};10551056# since we destructively absorb parameters, we keep this1057# boolean that remembers if we're handling a snapshot1058my$is_snapshot=$params{'action'}eq'snapshot';10591060# Summary just uses the project path URL, any other action is1061# added to the URL1062if(defined$params{'action'}) {1063$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary';1064delete$params{'action'};1065}10661067# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1068# stripping nonexistent or useless pieces1069$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1070||$params{'hash_parent'} ||$params{'hash'});1071if(defined$params{'hash_base'}) {1072if(defined$params{'hash_parent_base'}) {1073$href.= esc_url($params{'hash_parent_base'});1074# skip the file_parent if it's the same as the file_name1075if(defined$params{'file_parent'}) {1076if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1077delete$params{'file_parent'};1078}elsif($params{'file_parent'} !~/\.\./) {1079$href.=":/".esc_url($params{'file_parent'});1080delete$params{'file_parent'};1081}1082}1083$href.="..";1084delete$params{'hash_parent'};1085delete$params{'hash_parent_base'};1086}elsif(defined$params{'hash_parent'}) {1087$href.= esc_url($params{'hash_parent'})."..";1088delete$params{'hash_parent'};1089}10901091$href.= esc_url($params{'hash_base'});1092if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1093$href.=":/".esc_url($params{'file_name'});1094delete$params{'file_name'};1095}1096delete$params{'hash'};1097delete$params{'hash_base'};1098}elsif(defined$params{'hash'}) {1099$href.= esc_url($params{'hash'});1100delete$params{'hash'};1101}11021103# If the action was a snapshot, we can absorb the1104# snapshot_format parameter too1105if($is_snapshot) {1106my$fmt=$params{'snapshot_format'};1107# snapshot_format should always be defined when href()1108# is called, but just in case some code forgets, we1109# fall back to the default1110$fmt||=$snapshot_fmts[0];1111$href.=$known_snapshot_formats{$fmt}{'suffix'};1112delete$params{'snapshot_format'};1113}1114}11151116# now encode the parameters explicitly1117my@result= ();1118for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1119my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1120if(defined$params{$name}) {1121if(ref($params{$name})eq"ARRAY") {1122foreachmy$par(@{$params{$name}}) {1123push@result,$symbol."=". esc_param($par);1124}1125}else{1126push@result,$symbol."=". esc_param($params{$name});1127}1128}1129}1130$href.="?".join(';',@result)ifscalar@result;11311132return$href;1133}113411351136## ======================================================================1137## validation, quoting/unquoting and escaping11381139sub validate_action {1140my$input=shift||returnundef;1141returnundefunlessexists$actions{$input};1142return$input;1143}11441145sub validate_project {1146my$input=shift||returnundef;1147if(!validate_pathname($input) ||1148!(-d "$projectroot/$input") ||1149!check_export_ok("$projectroot/$input") ||1150($strict_export&& !project_in_list($input))) {1151returnundef;1152}else{1153return$input;1154}1155}11561157sub validate_pathname {1158my$input=shift||returnundef;11591160# no '.' or '..' as elements of path, i.e. no '.' nor '..'1161# at the beginning, at the end, and between slashes.1162# also this catches doubled slashes1163if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1164returnundef;1165}1166# no null characters1167if($input=~m!\0!) {1168returnundef;1169}1170return$input;1171}11721173sub validate_refname {1174my$input=shift||returnundef;11751176# textual hashes are O.K.1177if($input=~m/^[0-9a-fA-F]{40}$/) {1178return$input;1179}1180# it must be correct pathname1181$input= validate_pathname($input)1182orreturnundef;1183# restrictions on ref name according to git-check-ref-format1184if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1185returnundef;1186}1187return$input;1188}11891190# decode sequences of octets in utf8 into Perl's internal form,1191# which is utf-8 with utf8 flag set if needed. gitweb writes out1192# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1193sub to_utf8 {1194my$str=shift;1195returnundefunlessdefined$str;1196if(utf8::valid($str)) {1197 utf8::decode($str);1198return$str;1199}else{1200return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1201}1202}12031204# quote unsafe chars, but keep the slash, even when it's not1205# correct, but quoted slashes look too horrible in bookmarks1206sub esc_param {1207my$str=shift;1208returnundefunlessdefined$str;1209$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1210$str=~s/ /\+/g;1211return$str;1212}12131214# quote unsafe chars in whole URL, so some charactrs cannot be quoted1215sub esc_url {1216my$str=shift;1217returnundefunlessdefined$str;1218$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1219$str=~s/\+/%2B/g;1220$str=~s/ /\+/g;1221return$str;1222}12231224# replace invalid utf8 character with SUBSTITUTION sequence1225sub esc_html {1226my$str=shift;1227my%opts=@_;12281229returnundefunlessdefined$str;12301231$str= to_utf8($str);1232$str=$cgi->escapeHTML($str);1233if($opts{'-nbsp'}) {1234$str=~s/ / /g;1235}1236$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1237return$str;1238}12391240# quote control characters and escape filename to HTML1241sub esc_path {1242my$str=shift;1243my%opts=@_;12441245returnundefunlessdefined$str;12461247$str= to_utf8($str);1248$str=$cgi->escapeHTML($str);1249if($opts{'-nbsp'}) {1250$str=~s/ / /g;1251}1252$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1253return$str;1254}12551256# Make control characters "printable", using character escape codes (CEC)1257sub quot_cec {1258my$cntrl=shift;1259my%opts=@_;1260my%es= (# character escape codes, aka escape sequences1261"\t"=>'\t',# tab (HT)1262"\n"=>'\n',# line feed (LF)1263"\r"=>'\r',# carrige return (CR)1264"\f"=>'\f',# form feed (FF)1265"\b"=>'\b',# backspace (BS)1266"\a"=>'\a',# alarm (bell) (BEL)1267"\e"=>'\e',# escape (ESC)1268"\013"=>'\v',# vertical tab (VT)1269"\000"=>'\0',# nul character (NUL)1270);1271my$chr= ( (exists$es{$cntrl})1272?$es{$cntrl}1273:sprintf('\%2x',ord($cntrl)) );1274if($opts{-nohtml}) {1275return$chr;1276}else{1277return"<span class=\"cntrl\">$chr</span>";1278}1279}12801281# Alternatively use unicode control pictures codepoints,1282# Unicode "printable representation" (PR)1283sub quot_upr {1284my$cntrl=shift;1285my%opts=@_;12861287my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1288if($opts{-nohtml}) {1289return$chr;1290}else{1291return"<span class=\"cntrl\">$chr</span>";1292}1293}12941295# git may return quoted and escaped filenames1296sub unquote {1297my$str=shift;12981299sub unq {1300my$seq=shift;1301my%es= (# character escape codes, aka escape sequences1302't'=>"\t",# tab (HT, TAB)1303'n'=>"\n",# newline (NL)1304'r'=>"\r",# return (CR)1305'f'=>"\f",# form feed (FF)1306'b'=>"\b",# backspace (BS)1307'a'=>"\a",# alarm (bell) (BEL)1308'e'=>"\e",# escape (ESC)1309'v'=>"\013",# vertical tab (VT)1310);13111312if($seq=~m/^[0-7]{1,3}$/) {1313# octal char sequence1314returnchr(oct($seq));1315}elsif(exists$es{$seq}) {1316# C escape sequence, aka character escape code1317return$es{$seq};1318}1319# quoted ordinary character1320return$seq;1321}13221323if($str=~m/^"(.*)"$/) {1324# needs unquoting1325$str=$1;1326$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1327}1328return$str;1329}13301331# escape tabs (convert tabs to spaces)1332sub untabify {1333my$line=shift;13341335while((my$pos=index($line,"\t")) != -1) {1336if(my$count= (8- ($pos%8))) {1337my$spaces=' ' x $count;1338$line=~s/\t/$spaces/;1339}1340}13411342return$line;1343}13441345sub project_in_list {1346my$project=shift;1347my@list= git_get_projects_list();1348return@list&&scalar(grep{$_->{'path'}eq$project}@list);1349}13501351## ----------------------------------------------------------------------1352## HTML aware string manipulation13531354# Try to chop given string on a word boundary between position1355# $len and $len+$add_len. If there is no word boundary there,1356# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1357# (marking chopped part) would be longer than given string.1358sub chop_str {1359my$str=shift;1360my$len=shift;1361my$add_len=shift||10;1362my$where=shift||'right';# 'left' | 'center' | 'right'13631364# Make sure perl knows it is utf8 encoded so we don't1365# cut in the middle of a utf8 multibyte char.1366$str= to_utf8($str);13671368# allow only $len chars, but don't cut a word if it would fit in $add_len1369# if it doesn't fit, cut it if it's still longer than the dots we would add1370# remove chopped character entities entirely13711372# when chopping in the middle, distribute $len into left and right part1373# return early if chopping wouldn't make string shorter1374if($whereeq'center') {1375return$strif($len+5>=length($str));# filler is length 51376$len=int($len/2);1377}else{1378return$strif($len+4>=length($str));# filler is length 41379}13801381# regexps: ending and beginning with word part up to $add_len1382my$endre=qr/.{$len}\w{0,$add_len}/;1383my$begre=qr/\w{0,$add_len}.{$len}/;13841385if($whereeq'left') {1386$str=~m/^(.*?)($begre)$/;1387my($lead,$body) = ($1,$2);1388if(length($lead) >4) {1389$lead=" ...";1390}1391return"$lead$body";13921393}elsif($whereeq'center') {1394$str=~m/^($endre)(.*)$/;1395my($left,$str) = ($1,$2);1396$str=~m/^(.*?)($begre)$/;1397my($mid,$right) = ($1,$2);1398if(length($mid) >5) {1399$mid=" ... ";1400}1401return"$left$mid$right";14021403}else{1404$str=~m/^($endre)(.*)$/;1405my$body=$1;1406my$tail=$2;1407if(length($tail) >4) {1408$tail="... ";1409}1410return"$body$tail";1411}1412}14131414# takes the same arguments as chop_str, but also wraps a <span> around the1415# result with a title attribute if it does get chopped. Additionally, the1416# string is HTML-escaped.1417sub chop_and_escape_str {1418my($str) =@_;14191420my$chopped= chop_str(@_);1421if($choppedeq$str) {1422return esc_html($chopped);1423}else{1424$str=~s/[[:cntrl:]]/?/g;1425return$cgi->span({-title=>$str}, esc_html($chopped));1426}1427}14281429## ----------------------------------------------------------------------1430## functions returning short strings14311432# CSS class for given age value (in seconds)1433sub age_class {1434my$age=shift;14351436if(!defined$age) {1437return"noage";1438}elsif($age<60*60*2) {1439return"age0";1440}elsif($age<60*60*24*2) {1441return"age1";1442}else{1443return"age2";1444}1445}14461447# convert age in seconds to "nn units ago" string1448sub age_string {1449my$age=shift;1450my$age_str;14511452if($age>60*60*24*365*2) {1453$age_str= (int$age/60/60/24/365);1454$age_str.=" years ago";1455}elsif($age>60*60*24*(365/12)*2) {1456$age_str=int$age/60/60/24/(365/12);1457$age_str.=" months ago";1458}elsif($age>60*60*24*7*2) {1459$age_str=int$age/60/60/24/7;1460$age_str.=" weeks ago";1461}elsif($age>60*60*24*2) {1462$age_str=int$age/60/60/24;1463$age_str.=" days ago";1464}elsif($age>60*60*2) {1465$age_str=int$age/60/60;1466$age_str.=" hours ago";1467}elsif($age>60*2) {1468$age_str=int$age/60;1469$age_str.=" min ago";1470}elsif($age>2) {1471$age_str=int$age;1472$age_str.=" sec ago";1473}else{1474$age_str.=" right now";1475}1476return$age_str;1477}14781479useconstant{1480 S_IFINVALID =>0030000,1481 S_IFGITLINK =>0160000,1482};14831484# submodule/subproject, a commit object reference1485sub S_ISGITLINK {1486my$mode=shift;14871488return(($mode& S_IFMT) == S_IFGITLINK)1489}14901491# convert file mode in octal to symbolic file mode string1492sub mode_str {1493my$mode=oct shift;14941495if(S_ISGITLINK($mode)) {1496return'm---------';1497}elsif(S_ISDIR($mode& S_IFMT)) {1498return'drwxr-xr-x';1499}elsif(S_ISLNK($mode)) {1500return'lrwxrwxrwx';1501}elsif(S_ISREG($mode)) {1502# git cares only about the executable bit1503if($mode& S_IXUSR) {1504return'-rwxr-xr-x';1505}else{1506return'-rw-r--r--';1507};1508}else{1509return'----------';1510}1511}15121513# convert file mode in octal to file type string1514sub file_type {1515my$mode=shift;15161517if($mode!~m/^[0-7]+$/) {1518return$mode;1519}else{1520$mode=oct$mode;1521}15221523if(S_ISGITLINK($mode)) {1524return"submodule";1525}elsif(S_ISDIR($mode& S_IFMT)) {1526return"directory";1527}elsif(S_ISLNK($mode)) {1528return"symlink";1529}elsif(S_ISREG($mode)) {1530return"file";1531}else{1532return"unknown";1533}1534}15351536# convert file mode in octal to file type description string1537sub file_type_long {1538my$mode=shift;15391540if($mode!~m/^[0-7]+$/) {1541return$mode;1542}else{1543$mode=oct$mode;1544}15451546if(S_ISGITLINK($mode)) {1547return"submodule";1548}elsif(S_ISDIR($mode& S_IFMT)) {1549return"directory";1550}elsif(S_ISLNK($mode)) {1551return"symlink";1552}elsif(S_ISREG($mode)) {1553if($mode& S_IXUSR) {1554return"executable";1555}else{1556return"file";1557};1558}else{1559return"unknown";1560}1561}156215631564## ----------------------------------------------------------------------1565## functions returning short HTML fragments, or transforming HTML fragments1566## which don't belong to other sections15671568# format line of commit message.1569sub format_log_line_html {1570my$line=shift;15711572$line= esc_html($line, -nbsp=>1);1573$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1574$cgi->a({-href => href(action=>"object", hash=>$1),1575-class=>"text"},$1);1576}eg;15771578return$line;1579}15801581# format marker of refs pointing to given object15821583# the destination action is chosen based on object type and current context:1584# - for annotated tags, we choose the tag view unless it's the current view1585# already, in which case we go to shortlog view1586# - for other refs, we keep the current view if we're in history, shortlog or1587# log view, and select shortlog otherwise1588sub format_ref_marker {1589my($refs,$id) =@_;1590my$markers='';15911592if(defined$refs->{$id}) {1593foreachmy$ref(@{$refs->{$id}}) {1594# this code exploits the fact that non-lightweight tags are the1595# only indirect objects, and that they are the only objects for which1596# we want to use tag instead of shortlog as action1597my($type,$name) =qw();1598my$indirect= ($ref=~s/\^\{\}$//);1599# e.g. tags/v2.6.11 or heads/next1600if($ref=~m!^(.*?)s?/(.*)$!) {1601$type=$1;1602$name=$2;1603}else{1604$type="ref";1605$name=$ref;1606}16071608my$class=$type;1609$class.=" indirect"if$indirect;16101611my$dest_action="shortlog";16121613if($indirect) {1614$dest_action="tag"unless$actioneq"tag";1615}elsif($action=~/^(history|(short)?log)$/) {1616$dest_action=$action;1617}16181619my$dest="";1620$dest.="refs/"unless$ref=~ m!^refs/!;1621$dest.=$ref;16221623my$link=$cgi->a({1624-href => href(1625 action=>$dest_action,1626 hash=>$dest1627)},$name);16281629$markers.=" <span class=\"$class\"title=\"$ref\">".1630$link."</span>";1631}1632}16331634if($markers) {1635return' <span class="refs">'.$markers.'</span>';1636}else{1637return"";1638}1639}16401641# format, perhaps shortened and with markers, title line1642sub format_subject_html {1643my($long,$short,$href,$extra) =@_;1644$extra=''unlessdefined($extra);16451646if(length($short) <length($long)) {1647$long=~s/[[:cntrl:]]/?/g;1648return$cgi->a({-href =>$href, -class=>"list subject",1649-title => to_utf8($long)},1650 esc_html($short)) .$extra;1651}else{1652return$cgi->a({-href =>$href, -class=>"list subject"},1653 esc_html($long)) .$extra;1654}1655}16561657# Rather than recomputing the url for an email multiple times, we cache it1658# after the first hit. This gives a visible benefit in views where the avatar1659# for the same email is used repeatedly (e.g. shortlog).1660# The cache is shared by all avatar engines (currently gravatar only), which1661# are free to use it as preferred. Since only one avatar engine is used for any1662# given page, there's no risk for cache conflicts.1663our%avatar_cache= ();16641665# Compute the picon url for a given email, by using the picon search service over at1666# http://www.cs.indiana.edu/picons/search.html1667sub picon_url {1668my$email=lc shift;1669if(!$avatar_cache{$email}) {1670my($user,$domain) =split('@',$email);1671$avatar_cache{$email} =1672"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1673"$domain/$user/".1674"users+domains+unknown/up/single";1675}1676return$avatar_cache{$email};1677}16781679# Compute the gravatar url for a given email, if it's not in the cache already.1680# Gravatar stores only the part of the URL before the size, since that's the1681# one computationally more expensive. This also allows reuse of the cache for1682# different sizes (for this particular engine).1683sub gravatar_url {1684my$email=lc shift;1685my$size=shift;1686$avatar_cache{$email} ||=1687"http://www.gravatar.com/avatar/".1688 Digest::MD5::md5_hex($email) ."?s=";1689return$avatar_cache{$email} .$size;1690}16911692# Insert an avatar for the given $email at the given $size if the feature1693# is enabled.1694sub git_get_avatar {1695my($email,%opts) =@_;1696my$pre_white= ($opts{-pad_before} ?" ":"");1697my$post_white= ($opts{-pad_after} ?" ":"");1698$opts{-size} ||='default';1699my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1700my$url="";1701if($git_avatareq'gravatar') {1702$url= gravatar_url($email,$size);1703}elsif($git_avatareq'picon') {1704$url= picon_url($email);1705}1706# Other providers can be added by extending the if chain, defining $url1707# as needed. If no variant puts something in $url, we assume avatars1708# are completely disabled/unavailable.1709if($url) {1710return$pre_white.1711"<img width=\"$size\"".1712"class=\"avatar\"".1713"src=\"$url\"".1714"alt=\"\"".1715"/>".$post_white;1716}else{1717return"";1718}1719}17201721sub format_search_author {1722my($author,$searchtype,$displaytext) =@_;1723my$have_search= gitweb_check_feature('search');17241725if($have_search) {1726my$performed="";1727if($searchtypeeq'author') {1728$performed="authored";1729}elsif($searchtypeeq'committer') {1730$performed="committed";1731}17321733return$cgi->a({-href => href(action=>"search", hash=>$hash,1734 searchtext=>$author,1735 searchtype=>$searchtype),class=>"list",1736 title=>"Search for commits$performedby$author"},1737$displaytext);17381739}else{1740return$displaytext;1741}1742}17431744# format the author name of the given commit with the given tag1745# the author name is chopped and escaped according to the other1746# optional parameters (see chop_str).1747sub format_author_html {1748my$tag=shift;1749my$co=shift;1750my$author= chop_and_escape_str($co->{'author_name'},@_);1751return"<$tagclass=\"author\">".1752 format_search_author($co->{'author_name'},"author",1753 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1754$author) .1755"</$tag>";1756}17571758# format git diff header line, i.e. "diff --(git|combined|cc) ..."1759sub format_git_diff_header_line {1760my$line=shift;1761my$diffinfo=shift;1762my($from,$to) =@_;17631764if($diffinfo->{'nparents'}) {1765# combined diff1766$line=~s!^(diff (.*?) )"?.*$!$1!;1767if($to->{'href'}) {1768$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1769 esc_path($to->{'file'}));1770}else{# file was deleted (no href)1771$line.= esc_path($to->{'file'});1772}1773}else{1774# "ordinary" diff1775$line=~s!^(diff (.*?) )"?a/.*$!$1!;1776if($from->{'href'}) {1777$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1778'a/'. esc_path($from->{'file'}));1779}else{# file was added (no href)1780$line.='a/'. esc_path($from->{'file'});1781}1782$line.=' ';1783if($to->{'href'}) {1784$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1785'b/'. esc_path($to->{'file'}));1786}else{# file was deleted1787$line.='b/'. esc_path($to->{'file'});1788}1789}17901791return"<div class=\"diff header\">$line</div>\n";1792}17931794# format extended diff header line, before patch itself1795sub format_extended_diff_header_line {1796my$line=shift;1797my$diffinfo=shift;1798my($from,$to) =@_;17991800# match <path>1801if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1802$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1803 esc_path($from->{'file'}));1804}1805if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1806$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1807 esc_path($to->{'file'}));1808}1809# match single <mode>1810if($line=~m/\s(\d{6})$/) {1811$line.='<span class="info"> ('.1812 file_type_long($1) .1813')</span>';1814}1815# match <hash>1816if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1817# can match only for combined diff1818$line='index ';1819for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1820if($from->{'href'}[$i]) {1821$line.=$cgi->a({-href=>$from->{'href'}[$i],1822-class=>"hash"},1823substr($diffinfo->{'from_id'}[$i],0,7));1824}else{1825$line.='0' x 7;1826}1827# separator1828$line.=','if($i<$diffinfo->{'nparents'} -1);1829}1830$line.='..';1831if($to->{'href'}) {1832$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1833substr($diffinfo->{'to_id'},0,7));1834}else{1835$line.='0' x 7;1836}18371838}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1839# can match only for ordinary diff1840my($from_link,$to_link);1841if($from->{'href'}) {1842$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1843substr($diffinfo->{'from_id'},0,7));1844}else{1845$from_link='0' x 7;1846}1847if($to->{'href'}) {1848$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1849substr($diffinfo->{'to_id'},0,7));1850}else{1851$to_link='0' x 7;1852}1853my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1854$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1855}18561857return$line."<br/>\n";1858}18591860# format from-file/to-file diff header1861sub format_diff_from_to_header {1862my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1863my$line;1864my$result='';18651866$line=$from_line;1867#assert($line =~ m/^---/) if DEBUG;1868# no extra formatting for "^--- /dev/null"1869if(!$diffinfo->{'nparents'}) {1870# ordinary (single parent) diff1871if($line=~m!^--- "?a/!) {1872if($from->{'href'}) {1873$line='--- a/'.1874$cgi->a({-href=>$from->{'href'}, -class=>"path"},1875 esc_path($from->{'file'}));1876}else{1877$line='--- a/'.1878 esc_path($from->{'file'});1879}1880}1881$result.= qq!<div class="diff from_file">$line</div>\n!;18821883}else{1884# combined diff (merge commit)1885for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1886if($from->{'href'}[$i]) {1887$line='--- '.1888$cgi->a({-href=>href(action=>"blobdiff",1889 hash_parent=>$diffinfo->{'from_id'}[$i],1890 hash_parent_base=>$parents[$i],1891 file_parent=>$from->{'file'}[$i],1892 hash=>$diffinfo->{'to_id'},1893 hash_base=>$hash,1894 file_name=>$to->{'file'}),1895-class=>"path",1896-title=>"diff". ($i+1)},1897$i+1) .1898'/'.1899$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1900 esc_path($from->{'file'}[$i]));1901}else{1902$line='--- /dev/null';1903}1904$result.= qq!<div class="diff from_file">$line</div>\n!;1905}1906}19071908$line=$to_line;1909#assert($line =~ m/^\+\+\+/) if DEBUG;1910# no extra formatting for "^+++ /dev/null"1911if($line=~m!^\+\+\+ "?b/!) {1912if($to->{'href'}) {1913$line='+++ b/'.1914$cgi->a({-href=>$to->{'href'}, -class=>"path"},1915 esc_path($to->{'file'}));1916}else{1917$line='+++ b/'.1918 esc_path($to->{'file'});1919}1920}1921$result.= qq!<div class="diff to_file">$line</div>\n!;19221923return$result;1924}19251926# create note for patch simplified by combined diff1927sub format_diff_cc_simplified {1928my($diffinfo,@parents) =@_;1929my$result='';19301931$result.="<div class=\"diff header\">".1932"diff --cc ";1933if(!is_deleted($diffinfo)) {1934$result.=$cgi->a({-href => href(action=>"blob",1935 hash_base=>$hash,1936 hash=>$diffinfo->{'to_id'},1937 file_name=>$diffinfo->{'to_file'}),1938-class=>"path"},1939 esc_path($diffinfo->{'to_file'}));1940}else{1941$result.= esc_path($diffinfo->{'to_file'});1942}1943$result.="</div>\n".# class="diff header"1944"<div class=\"diff nodifferences\">".1945"Simple merge".1946"</div>\n";# class="diff nodifferences"19471948return$result;1949}19501951# format patch (diff) line (not to be used for diff headers)1952sub format_diff_line {1953my$line=shift;1954my($from,$to) =@_;1955my$diff_class="";19561957chomp$line;19581959if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1960# combined diff1961my$prefix=substr($line,0,scalar@{$from->{'href'}});1962if($line=~m/^\@{3}/) {1963$diff_class=" chunk_header";1964}elsif($line=~m/^\\/) {1965$diff_class=" incomplete";1966}elsif($prefix=~tr/+/+/) {1967$diff_class=" add";1968}elsif($prefix=~tr/-/-/) {1969$diff_class=" rem";1970}1971}else{1972# assume ordinary diff1973my$char=substr($line,0,1);1974if($chareq'+') {1975$diff_class=" add";1976}elsif($chareq'-') {1977$diff_class=" rem";1978}elsif($chareq'@') {1979$diff_class=" chunk_header";1980}elsif($chareq"\\") {1981$diff_class=" incomplete";1982}1983}1984$line= untabify($line);1985if($from&&$to&&$line=~m/^\@{2} /) {1986my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1987$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;19881989$from_lines=0unlessdefined$from_lines;1990$to_lines=0unlessdefined$to_lines;19911992if($from->{'href'}) {1993$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1994-class=>"list"},$from_text);1995}1996if($to->{'href'}) {1997$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1998-class=>"list"},$to_text);1999}2000$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2001"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2002return"<div class=\"diff$diff_class\">$line</div>\n";2003}elsif($from&&$to&&$line=~m/^\@{3}/) {2004my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2005my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);20062007@from_text=split(' ',$ranges);2008for(my$i=0;$i<@from_text; ++$i) {2009($from_start[$i],$from_nlines[$i]) =2010(split(',',substr($from_text[$i],1)),0);2011}20122013$to_text=pop@from_text;2014$to_start=pop@from_start;2015$to_nlines=pop@from_nlines;20162017$line="<span class=\"chunk_info\">$prefix";2018for(my$i=0;$i<@from_text; ++$i) {2019if($from->{'href'}[$i]) {2020$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2021-class=>"list"},$from_text[$i]);2022}else{2023$line.=$from_text[$i];2024}2025$line.=" ";2026}2027if($to->{'href'}) {2028$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2029-class=>"list"},$to_text);2030}else{2031$line.=$to_text;2032}2033$line.="$prefix</span>".2034"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2035return"<div class=\"diff$diff_class\">$line</div>\n";2036}2037return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2038}20392040# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2041# linked. Pass the hash of the tree/commit to snapshot.2042sub format_snapshot_links {2043my($hash) =@_;2044my$num_fmts=@snapshot_fmts;2045if($num_fmts>1) {2046# A parenthesized list of links bearing format names.2047# e.g. "snapshot (_tar.gz_ _zip_)"2048return"snapshot (".join(' ',map2049$cgi->a({2050-href => href(2051 action=>"snapshot",2052 hash=>$hash,2053 snapshot_format=>$_2054)2055},$known_snapshot_formats{$_}{'display'})2056,@snapshot_fmts) .")";2057}elsif($num_fmts==1) {2058# A single "snapshot" link whose tooltip bears the format name.2059# i.e. "_snapshot_"2060my($fmt) =@snapshot_fmts;2061return2062$cgi->a({2063-href => href(2064 action=>"snapshot",2065 hash=>$hash,2066 snapshot_format=>$fmt2067),2068-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2069},"snapshot");2070}else{# $num_fmts == 02071returnundef;2072}2073}20742075## ......................................................................2076## functions returning values to be passed, perhaps after some2077## transformation, to other functions; e.g. returning arguments to href()20782079# returns hash to be passed to href to generate gitweb URL2080# in -title key it returns description of link2081sub get_feed_info {2082my$format=shift||'Atom';2083my%res= (action =>lc($format));20842085# feed links are possible only for project views2086return unless(defined$project);2087# some views should link to OPML, or to generic project feed,2088# or don't have specific feed yet (so they should use generic)2089return if($action=~/^(?:tags|heads|forks|tag|search)$/x);20902091my$branch;2092# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2093# from tag links; this also makes possible to detect branch links2094if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2095(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2096$branch=$1;2097}2098# find log type for feed description (title)2099my$type='log';2100if(defined$file_name) {2101$type="history of$file_name";2102$type.="/"if($actioneq'tree');2103$type.=" on '$branch'"if(defined$branch);2104}else{2105$type="log of$branch"if(defined$branch);2106}21072108$res{-title} =$type;2109$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2110$res{'file_name'} =$file_name;21112112return%res;2113}21142115## ----------------------------------------------------------------------2116## git utility subroutines, invoking git commands21172118# returns path to the core git executable and the --git-dir parameter as list2119sub git_cmd {2120$number_of_git_cmds++;2121return$GIT,'--git-dir='.$git_dir;2122}21232124# quote the given arguments for passing them to the shell2125# quote_command("command", "arg 1", "arg with ' and ! characters")2126# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2127# Try to avoid using this function wherever possible.2128sub quote_command {2129returnjoin(' ',2130map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2131}21322133# get HEAD ref of given project as hash2134sub git_get_head_hash {2135return git_get_full_hash(shift,'HEAD');2136}21372138sub git_get_full_hash {2139return git_get_hash(@_);2140}21412142sub git_get_short_hash {2143return git_get_hash(@_,'--short=7');2144}21452146sub git_get_hash {2147my($project,$hash,@options) =@_;2148my$o_git_dir=$git_dir;2149my$retval=undef;2150$git_dir="$projectroot/$project";2151if(open my$fd,'-|', git_cmd(),'rev-parse',2152'--verify','-q',@options,$hash) {2153$retval= <$fd>;2154chomp$retvalifdefined$retval;2155close$fd;2156}2157if(defined$o_git_dir) {2158$git_dir=$o_git_dir;2159}2160return$retval;2161}21622163# get type of given object2164sub git_get_type {2165my$hash=shift;21662167open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2168my$type= <$fd>;2169close$fdorreturn;2170chomp$type;2171return$type;2172}21732174# repository configuration2175our$config_file='';2176our%config;21772178# store multiple values for single key as anonymous array reference2179# single values stored directly in the hash, not as [ <value> ]2180sub hash_set_multi {2181my($hash,$key,$value) =@_;21822183if(!exists$hash->{$key}) {2184$hash->{$key} =$value;2185}elsif(!ref$hash->{$key}) {2186$hash->{$key} = [$hash->{$key},$value];2187}else{2188push@{$hash->{$key}},$value;2189}2190}21912192# return hash of git project configuration2193# optionally limited to some section, e.g. 'gitweb'2194sub git_parse_project_config {2195my$section_regexp=shift;2196my%config;21972198local$/="\0";21992200open my$fh,"-|", git_cmd(),"config",'-z','-l',2201orreturn;22022203while(my$keyval= <$fh>) {2204chomp$keyval;2205my($key,$value) =split(/\n/,$keyval,2);22062207 hash_set_multi(\%config,$key,$value)2208if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2209}2210close$fh;22112212return%config;2213}22142215# convert config value to boolean: 'true' or 'false'2216# no value, number > 0, 'true' and 'yes' values are true2217# rest of values are treated as false (never as error)2218sub config_to_bool {2219my$val=shift;22202221return1if!defined$val;# section.key22222223# strip leading and trailing whitespace2224$val=~s/^\s+//;2225$val=~s/\s+$//;22262227return(($val=~/^\d+$/&&$val) ||# section.key = 12228($val=~/^(?:true|yes)$/i));# section.key = true2229}22302231# convert config value to simple decimal number2232# an optional value suffix of 'k', 'm', or 'g' will cause the value2233# to be multiplied by 1024, 1048576, or 10737418242234sub config_to_int {2235my$val=shift;22362237# strip leading and trailing whitespace2238$val=~s/^\s+//;2239$val=~s/\s+$//;22402241if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2242$unit=lc($unit);2243# unknown unit is treated as 12244return$num* ($uniteq'g'?1073741824:2245$uniteq'm'?1048576:2246$uniteq'k'?1024:1);2247}2248return$val;2249}22502251# convert config value to array reference, if needed2252sub config_to_multi {2253my$val=shift;22542255returnref($val) ?$val: (defined($val) ? [$val] : []);2256}22572258sub git_get_project_config {2259my($key,$type) =@_;22602261return unlessdefined$git_dir;22622263# key sanity check2264return unless($key);2265$key=~s/^gitweb\.//;2266return if($key=~m/\W/);22672268# type sanity check2269if(defined$type) {2270$type=~s/^--//;2271$type=undef2272unless($typeeq'bool'||$typeeq'int');2273}22742275# get config2276if(!defined$config_file||2277$config_filene"$git_dir/config") {2278%config= git_parse_project_config('gitweb');2279$config_file="$git_dir/config";2280}22812282# check if config variable (key) exists2283return unlessexists$config{"gitweb.$key"};22842285# ensure given type2286if(!defined$type) {2287return$config{"gitweb.$key"};2288}elsif($typeeq'bool') {2289# backward compatibility: 'git config --bool' returns true/false2290return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2291}elsif($typeeq'int') {2292return config_to_int($config{"gitweb.$key"});2293}2294return$config{"gitweb.$key"};2295}22962297# get hash of given path at given ref2298sub git_get_hash_by_path {2299my$base=shift;2300my$path=shift||returnundef;2301my$type=shift;23022303$path=~ s,/+$,,;23042305open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2306or die_error(500,"Open git-ls-tree failed");2307my$line= <$fd>;2308close$fdorreturnundef;23092310if(!defined$line) {2311# there is no tree or hash given by $path at $base2312returnundef;2313}23142315#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2316$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2317if(defined$type&&$typene$2) {2318# type doesn't match2319returnundef;2320}2321return$3;2322}23232324# get path of entry with given hash at given tree-ish (ref)2325# used to get 'from' filename for combined diff (merge commit) for renames2326sub git_get_path_by_hash {2327my$base=shift||return;2328my$hash=shift||return;23292330local$/="\0";23312332open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2333orreturnundef;2334while(my$line= <$fd>) {2335chomp$line;23362337#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2338#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2339if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2340close$fd;2341return$1;2342}2343}2344close$fd;2345returnundef;2346}23472348## ......................................................................2349## git utility functions, directly accessing git repository23502351sub git_get_project_description {2352my$path=shift;23532354$git_dir="$projectroot/$path";2355open my$fd,'<',"$git_dir/description"2356orreturn git_get_project_config('description');2357my$descr= <$fd>;2358close$fd;2359if(defined$descr) {2360chomp$descr;2361}2362return$descr;2363}23642365sub git_get_project_ctags {2366my$path=shift;2367my$ctags= {};23682369$git_dir="$projectroot/$path";2370opendir my$dh,"$git_dir/ctags"2371orreturn$ctags;2372foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2373open my$ct,'<',$_ornext;2374my$val= <$ct>;2375chomp$val;2376close$ct;2377my$ctag=$_;$ctag=~ s#.*/##;2378$ctags->{$ctag} =$val;2379}2380closedir$dh;2381$ctags;2382}23832384sub git_populate_project_tagcloud {2385my$ctags=shift;23862387# First, merge different-cased tags; tags vote on casing2388my%ctags_lc;2389foreach(keys%$ctags) {2390$ctags_lc{lc$_}->{count} +=$ctags->{$_};2391if(not$ctags_lc{lc$_}->{topcount}2392or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2393$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2394$ctags_lc{lc$_}->{topname} =$_;2395}2396}23972398my$cloud;2399if(eval{require HTML::TagCloud;1; }) {2400$cloud= HTML::TagCloud->new;2401foreach(sort keys%ctags_lc) {2402# Pad the title with spaces so that the cloud looks2403# less crammed.2404my$title=$ctags_lc{$_}->{topname};2405$title=~s/ / /g;2406$title=~s/^/ /g;2407$title=~s/$/ /g;2408$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2409}2410}else{2411$cloud= \%ctags_lc;2412}2413$cloud;2414}24152416sub git_show_project_tagcloud {2417my($cloud,$count) =@_;2418print STDERR ref($cloud)."..\n";2419if(ref$cloudeq'HTML::TagCloud') {2420return$cloud->html_and_css($count);2421}else{2422my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2423return'<p align="center">'.join(', ',map{2424"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2425}splice(@tags,0,$count)) .'</p>';2426}2427}24282429sub git_get_project_url_list {2430my$path=shift;24312432$git_dir="$projectroot/$path";2433open my$fd,'<',"$git_dir/cloneurl"2434orreturnwantarray?2435@{ config_to_multi(git_get_project_config('url')) } :2436 config_to_multi(git_get_project_config('url'));2437my@git_project_url_list=map{chomp;$_} <$fd>;2438close$fd;24392440returnwantarray?@git_project_url_list: \@git_project_url_list;2441}24422443sub git_get_projects_list {2444my($filter) =@_;2445my@list;24462447$filter||='';2448$filter=~s/\.git$//;24492450my$check_forks= gitweb_check_feature('forks');24512452if(-d $projects_list) {2453# search in directory2454my$dir=$projects_list. ($filter?"/$filter":'');2455# remove the trailing "/"2456$dir=~s!/+$!!;2457my$pfxlen=length("$dir");2458my$pfxdepth= ($dir=~tr!/!!);24592460 File::Find::find({2461 follow_fast =>1,# follow symbolic links2462 follow_skip =>2,# ignore duplicates2463 dangling_symlinks =>0,# ignore dangling symlinks, silently2464 wanted =>sub{2465# global variables2466our$project_maxdepth;2467our$projectroot;2468# skip project-list toplevel, if we get it.2469return if(m!^[/.]$!);2470# only directories can be git repositories2471return unless(-d $_);2472# don't traverse too deep (Find is super slow on os x)2473if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2474$File::Find::prune =1;2475return;2476}24772478my$subdir=substr($File::Find::name,$pfxlen+1);2479# we check related file in $projectroot2480my$path= ($filter?"$filter/":'') .$subdir;2481if(check_export_ok("$projectroot/$path")) {2482push@list, { path =>$path};2483$File::Find::prune =1;2484}2485},2486},"$dir");24872488}elsif(-f $projects_list) {2489# read from file(url-encoded):2490# 'git%2Fgit.git Linus+Torvalds'2491# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2492# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2493my%paths;2494open my$fd,'<',$projects_listorreturn;2495 PROJECT:2496while(my$line= <$fd>) {2497chomp$line;2498my($path,$owner) =split' ',$line;2499$path= unescape($path);2500$owner= unescape($owner);2501if(!defined$path) {2502next;2503}2504if($filterne'') {2505# looking for forks;2506my$pfx=substr($path,0,length($filter));2507if($pfxne$filter) {2508next PROJECT;2509}2510my$sfx=substr($path,length($filter));2511if($sfx!~/^\/.*\.git$/) {2512next PROJECT;2513}2514}elsif($check_forks) {2515 PATH:2516foreachmy$filter(keys%paths) {2517# looking for forks;2518my$pfx=substr($path,0,length($filter));2519if($pfxne$filter) {2520next PATH;2521}2522my$sfx=substr($path,length($filter));2523if($sfx!~/^\/.*\.git$/) {2524next PATH;2525}2526# is a fork, don't include it in2527# the list2528next PROJECT;2529}2530}2531if(check_export_ok("$projectroot/$path")) {2532my$pr= {2533 path =>$path,2534 owner => to_utf8($owner),2535};2536push@list,$pr;2537(my$forks_path=$path) =~s/\.git$//;2538$paths{$forks_path}++;2539}2540}2541close$fd;2542}2543return@list;2544}25452546our$gitweb_project_owner=undef;2547sub git_get_project_list_from_file {25482549return if(defined$gitweb_project_owner);25502551$gitweb_project_owner= {};2552# read from file (url-encoded):2553# 'git%2Fgit.git Linus+Torvalds'2554# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2555# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2556if(-f $projects_list) {2557open(my$fd,'<',$projects_list);2558while(my$line= <$fd>) {2559chomp$line;2560my($pr,$ow) =split' ',$line;2561$pr= unescape($pr);2562$ow= unescape($ow);2563$gitweb_project_owner->{$pr} = to_utf8($ow);2564}2565close$fd;2566}2567}25682569sub git_get_project_owner {2570my$project=shift;2571my$owner;25722573returnundefunless$project;2574$git_dir="$projectroot/$project";25752576if(!defined$gitweb_project_owner) {2577 git_get_project_list_from_file();2578}25792580if(exists$gitweb_project_owner->{$project}) {2581$owner=$gitweb_project_owner->{$project};2582}2583if(!defined$owner){2584$owner= git_get_project_config('owner');2585}2586if(!defined$owner) {2587$owner= get_file_owner("$git_dir");2588}25892590return$owner;2591}25922593sub git_get_last_activity {2594my($path) =@_;2595my$fd;25962597$git_dir="$projectroot/$path";2598open($fd,"-|", git_cmd(),'for-each-ref',2599'--format=%(committer)',2600'--sort=-committerdate',2601'--count=1',2602'refs/heads')orreturn;2603my$most_recent= <$fd>;2604close$fdorreturn;2605if(defined$most_recent&&2606$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2607my$timestamp=$1;2608my$age=time-$timestamp;2609return($age, age_string($age));2610}2611return(undef,undef);2612}26132614sub git_get_references {2615my$type=shift||"";2616my%refs;2617# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112618# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2619open my$fd,"-|", git_cmd(),"show-ref","--dereference",2620($type? ("--","refs/$type") : ())# use -- <pattern> if $type2621orreturn;26222623while(my$line= <$fd>) {2624chomp$line;2625if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2626if(defined$refs{$1}) {2627push@{$refs{$1}},$2;2628}else{2629$refs{$1} = [$2];2630}2631}2632}2633close$fdorreturn;2634return \%refs;2635}26362637sub git_get_rev_name_tags {2638my$hash=shift||returnundef;26392640open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2641orreturn;2642my$name_rev= <$fd>;2643close$fd;26442645if($name_rev=~ m|^$hash tags/(.*)$|) {2646return$1;2647}else{2648# catches also '$hash undefined' output2649returnundef;2650}2651}26522653## ----------------------------------------------------------------------2654## parse to hash functions26552656sub parse_date {2657my$epoch=shift;2658my$tz=shift||"-0000";26592660my%date;2661my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2662my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2663my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2664$date{'hour'} =$hour;2665$date{'minute'} =$min;2666$date{'mday'} =$mday;2667$date{'day'} =$days[$wday];2668$date{'month'} =$months[$mon];2669$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2670$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2671$date{'mday-time'} =sprintf"%d%s%02d:%02d",2672$mday,$months[$mon],$hour,$min;2673$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",26741900+$year,1+$mon,$mday,$hour,$min,$sec;26752676$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2677my$local=$epoch+ ((int$1+ ($2/60)) *3600);2678($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2679$date{'hour_local'} =$hour;2680$date{'minute_local'} =$min;2681$date{'tz_local'} =$tz;2682$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",26831900+$year,$mon+1,$mday,2684$hour,$min,$sec,$tz);2685return%date;2686}26872688sub parse_tag {2689my$tag_id=shift;2690my%tag;2691my@comment;26922693open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2694$tag{'id'} =$tag_id;2695while(my$line= <$fd>) {2696chomp$line;2697if($line=~m/^object ([0-9a-fA-F]{40})$/) {2698$tag{'object'} =$1;2699}elsif($line=~m/^type (.+)$/) {2700$tag{'type'} =$1;2701}elsif($line=~m/^tag (.+)$/) {2702$tag{'name'} =$1;2703}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2704$tag{'author'} =$1;2705$tag{'author_epoch'} =$2;2706$tag{'author_tz'} =$3;2707if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2708$tag{'author_name'} =$1;2709$tag{'author_email'} =$2;2710}else{2711$tag{'author_name'} =$tag{'author'};2712}2713}elsif($line=~m/--BEGIN/) {2714push@comment,$line;2715last;2716}elsif($lineeq"") {2717last;2718}2719}2720push@comment, <$fd>;2721$tag{'comment'} = \@comment;2722close$fdorreturn;2723if(!defined$tag{'name'}) {2724return2725};2726return%tag2727}27282729sub parse_commit_text {2730my($commit_text,$withparents) =@_;2731my@commit_lines=split'\n',$commit_text;2732my%co;27332734pop@commit_lines;# Remove '\0'27352736if(!@commit_lines) {2737return;2738}27392740my$header=shift@commit_lines;2741if($header!~m/^[0-9a-fA-F]{40}/) {2742return;2743}2744($co{'id'},my@parents) =split' ',$header;2745while(my$line=shift@commit_lines) {2746last if$lineeq"\n";2747if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2748$co{'tree'} =$1;2749}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2750push@parents,$1;2751}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2752$co{'author'} = to_utf8($1);2753$co{'author_epoch'} =$2;2754$co{'author_tz'} =$3;2755if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2756$co{'author_name'} =$1;2757$co{'author_email'} =$2;2758}else{2759$co{'author_name'} =$co{'author'};2760}2761}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2762$co{'committer'} = to_utf8($1);2763$co{'committer_epoch'} =$2;2764$co{'committer_tz'} =$3;2765if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2766$co{'committer_name'} =$1;2767$co{'committer_email'} =$2;2768}else{2769$co{'committer_name'} =$co{'committer'};2770}2771}2772}2773if(!defined$co{'tree'}) {2774return;2775};2776$co{'parents'} = \@parents;2777$co{'parent'} =$parents[0];27782779foreachmy$title(@commit_lines) {2780$title=~s/^ //;2781if($titlene"") {2782$co{'title'} = chop_str($title,80,5);2783# remove leading stuff of merges to make the interesting part visible2784if(length($title) >50) {2785$title=~s/^Automatic //;2786$title=~s/^merge (of|with) /Merge ... /i;2787if(length($title) >50) {2788$title=~s/(http|rsync):\/\///;2789}2790if(length($title) >50) {2791$title=~s/(master|www|rsync)\.//;2792}2793if(length($title) >50) {2794$title=~s/kernel.org:?//;2795}2796if(length($title) >50) {2797$title=~s/\/pub\/scm//;2798}2799}2800$co{'title_short'} = chop_str($title,50,5);2801last;2802}2803}2804if(!defined$co{'title'} ||$co{'title'}eq"") {2805$co{'title'} =$co{'title_short'} ='(no commit message)';2806}2807# remove added spaces2808foreachmy$line(@commit_lines) {2809$line=~s/^ //;2810}2811$co{'comment'} = \@commit_lines;28122813my$age=time-$co{'committer_epoch'};2814$co{'age'} =$age;2815$co{'age_string'} = age_string($age);2816my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2817if($age>60*60*24*7*2) {2818$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2819$co{'age_string_age'} =$co{'age_string'};2820}else{2821$co{'age_string_date'} =$co{'age_string'};2822$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2823}2824return%co;2825}28262827sub parse_commit {2828my($commit_id) =@_;2829my%co;28302831local$/="\0";28322833open my$fd,"-|", git_cmd(),"rev-list",2834"--parents",2835"--header",2836"--max-count=1",2837$commit_id,2838"--",2839or die_error(500,"Open git-rev-list failed");2840%co= parse_commit_text(<$fd>,1);2841close$fd;28422843return%co;2844}28452846sub parse_commits {2847my($commit_id,$maxcount,$skip,$filename,@args) =@_;2848my@cos;28492850$maxcount||=1;2851$skip||=0;28522853local$/="\0";28542855open my$fd,"-|", git_cmd(),"rev-list",2856"--header",2857@args,2858("--max-count=".$maxcount),2859("--skip=".$skip),2860@extra_options,2861$commit_id,2862"--",2863($filename? ($filename) : ())2864or die_error(500,"Open git-rev-list failed");2865while(my$line= <$fd>) {2866my%co= parse_commit_text($line);2867push@cos, \%co;2868}2869close$fd;28702871returnwantarray?@cos: \@cos;2872}28732874# parse line of git-diff-tree "raw" output2875sub parse_difftree_raw_line {2876my$line=shift;2877my%res;28782879# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2880# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2881if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2882$res{'from_mode'} =$1;2883$res{'to_mode'} =$2;2884$res{'from_id'} =$3;2885$res{'to_id'} =$4;2886$res{'status'} =$5;2887$res{'similarity'} =$6;2888if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2889($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2890}else{2891$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2892}2893}2894# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2895# combined diff (for merge commit)2896elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2897$res{'nparents'} =length($1);2898$res{'from_mode'} = [split(' ',$2) ];2899$res{'to_mode'} =pop@{$res{'from_mode'}};2900$res{'from_id'} = [split(' ',$3) ];2901$res{'to_id'} =pop@{$res{'from_id'}};2902$res{'status'} = [split('',$4) ];2903$res{'to_file'} = unquote($5);2904}2905# 'c512b523472485aef4fff9e57b229d9d243c967f'2906elsif($line=~m/^([0-9a-fA-F]{40})$/) {2907$res{'commit'} =$1;2908}29092910returnwantarray?%res: \%res;2911}29122913# wrapper: return parsed line of git-diff-tree "raw" output2914# (the argument might be raw line, or parsed info)2915sub parsed_difftree_line {2916my$line_or_ref=shift;29172918if(ref($line_or_ref)eq"HASH") {2919# pre-parsed (or generated by hand)2920return$line_or_ref;2921}else{2922return parse_difftree_raw_line($line_or_ref);2923}2924}29252926# parse line of git-ls-tree output2927sub parse_ls_tree_line {2928my$line=shift;2929my%opts=@_;2930my%res;29312932if($opts{'-l'}) {2933#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'2934$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;29352936$res{'mode'} =$1;2937$res{'type'} =$2;2938$res{'hash'} =$3;2939$res{'size'} =$4;2940if($opts{'-z'}) {2941$res{'name'} =$5;2942}else{2943$res{'name'} = unquote($5);2944}2945}else{2946#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2947$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;29482949$res{'mode'} =$1;2950$res{'type'} =$2;2951$res{'hash'} =$3;2952if($opts{'-z'}) {2953$res{'name'} =$4;2954}else{2955$res{'name'} = unquote($4);2956}2957}29582959returnwantarray?%res: \%res;2960}29612962# generates _two_ hashes, references to which are passed as 2 and 3 argument2963sub parse_from_to_diffinfo {2964my($diffinfo,$from,$to,@parents) =@_;29652966if($diffinfo->{'nparents'}) {2967# combined diff2968$from->{'file'} = [];2969$from->{'href'} = [];2970 fill_from_file_info($diffinfo,@parents)2971unlessexists$diffinfo->{'from_file'};2972for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2973$from->{'file'}[$i] =2974defined$diffinfo->{'from_file'}[$i] ?2975$diffinfo->{'from_file'}[$i] :2976$diffinfo->{'to_file'};2977if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2978$from->{'href'}[$i] = href(action=>"blob",2979 hash_base=>$parents[$i],2980 hash=>$diffinfo->{'from_id'}[$i],2981 file_name=>$from->{'file'}[$i]);2982}else{2983$from->{'href'}[$i] =undef;2984}2985}2986}else{2987# ordinary (not combined) diff2988$from->{'file'} =$diffinfo->{'from_file'};2989if($diffinfo->{'status'}ne"A") {# not new (added) file2990$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2991 hash=>$diffinfo->{'from_id'},2992 file_name=>$from->{'file'});2993}else{2994delete$from->{'href'};2995}2996}29972998$to->{'file'} =$diffinfo->{'to_file'};2999if(!is_deleted($diffinfo)) {# file exists in result3000$to->{'href'} = href(action=>"blob", hash_base=>$hash,3001 hash=>$diffinfo->{'to_id'},3002 file_name=>$to->{'file'});3003}else{3004delete$to->{'href'};3005}3006}30073008## ......................................................................3009## parse to array of hashes functions30103011sub git_get_heads_list {3012my$limit=shift;3013my@headslist;30143015open my$fd,'-|', git_cmd(),'for-each-ref',3016($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3017'--format=%(objectname) %(refname) %(subject)%00%(committer)',3018'refs/heads'3019orreturn;3020while(my$line= <$fd>) {3021my%ref_item;30223023chomp$line;3024my($refinfo,$committerinfo) =split(/\0/,$line);3025my($hash,$name,$title) =split(' ',$refinfo,3);3026my($committer,$epoch,$tz) =3027($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3028$ref_item{'fullname'} =$name;3029$name=~s!^refs/heads/!!;30303031$ref_item{'name'} =$name;3032$ref_item{'id'} =$hash;3033$ref_item{'title'} =$title||'(no commit message)';3034$ref_item{'epoch'} =$epoch;3035if($epoch) {3036$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3037}else{3038$ref_item{'age'} ="unknown";3039}30403041push@headslist, \%ref_item;3042}3043close$fd;30443045returnwantarray?@headslist: \@headslist;3046}30473048sub git_get_tags_list {3049my$limit=shift;3050my@tagslist;30513052open my$fd,'-|', git_cmd(),'for-each-ref',3053($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3054'--format=%(objectname) %(objecttype) %(refname) '.3055'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3056'refs/tags'3057orreturn;3058while(my$line= <$fd>) {3059my%ref_item;30603061chomp$line;3062my($refinfo,$creatorinfo) =split(/\0/,$line);3063my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3064my($creator,$epoch,$tz) =3065($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3066$ref_item{'fullname'} =$name;3067$name=~s!^refs/tags/!!;30683069$ref_item{'type'} =$type;3070$ref_item{'id'} =$id;3071$ref_item{'name'} =$name;3072if($typeeq"tag") {3073$ref_item{'subject'} =$title;3074$ref_item{'reftype'} =$reftype;3075$ref_item{'refid'} =$refid;3076}else{3077$ref_item{'reftype'} =$type;3078$ref_item{'refid'} =$id;3079}30803081if($typeeq"tag"||$typeeq"commit") {3082$ref_item{'epoch'} =$epoch;3083if($epoch) {3084$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3085}else{3086$ref_item{'age'} ="unknown";3087}3088}30893090push@tagslist, \%ref_item;3091}3092close$fd;30933094returnwantarray?@tagslist: \@tagslist;3095}30963097## ----------------------------------------------------------------------3098## filesystem-related functions30993100sub get_file_owner {3101my$path=shift;31023103my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3104my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3105if(!defined$gcos) {3106returnundef;3107}3108my$owner=$gcos;3109$owner=~s/[,;].*$//;3110return to_utf8($owner);3111}31123113# assume that file exists3114sub insert_file {3115my$filename=shift;31163117open my$fd,'<',$filename;3118print map{ to_utf8($_) } <$fd>;3119close$fd;3120}31213122## ......................................................................3123## mimetype related functions31243125sub mimetype_guess_file {3126my$filename=shift;3127my$mimemap=shift;3128-r $mimemaporreturnundef;31293130my%mimemap;3131open(my$mh,'<',$mimemap)orreturnundef;3132while(<$mh>) {3133next ifm/^#/;# skip comments3134my($mimetype,$exts) =split(/\t+/);3135if(defined$exts) {3136my@exts=split(/\s+/,$exts);3137foreachmy$ext(@exts) {3138$mimemap{$ext} =$mimetype;3139}3140}3141}3142close($mh);31433144$filename=~/\.([^.]*)$/;3145return$mimemap{$1};3146}31473148sub mimetype_guess {3149my$filename=shift;3150my$mime;3151$filename=~/\./orreturnundef;31523153if($mimetypes_file) {3154my$file=$mimetypes_file;3155if($file!~m!^/!) {# if it is relative path3156# it is relative to project3157$file="$projectroot/$project/$file";3158}3159$mime= mimetype_guess_file($filename,$file);3160}3161$mime||= mimetype_guess_file($filename,'/etc/mime.types');3162return$mime;3163}31643165sub blob_mimetype {3166my$fd=shift;3167my$filename=shift;31683169if($filename) {3170my$mime= mimetype_guess($filename);3171$mimeandreturn$mime;3172}31733174# just in case3175return$default_blob_plain_mimetypeunless$fd;31763177if(-T $fd) {3178return'text/plain';3179}elsif(!$filename) {3180return'application/octet-stream';3181}elsif($filename=~m/\.png$/i) {3182return'image/png';3183}elsif($filename=~m/\.gif$/i) {3184return'image/gif';3185}elsif($filename=~m/\.jpe?g$/i) {3186return'image/jpeg';3187}else{3188return'application/octet-stream';3189}3190}31913192sub blob_contenttype {3193my($fd,$file_name,$type) =@_;31943195$type||= blob_mimetype($fd,$file_name);3196if($typeeq'text/plain'&&defined$default_text_plain_charset) {3197$type.="; charset=$default_text_plain_charset";3198}31993200return$type;3201}32023203# guess file syntax for syntax highlighting; return undef if no highlighting3204# the name of syntax can (in the future) depend on syntax highlighter used3205sub guess_file_syntax {3206my($highlight,$mimetype,$file_name) =@_;3207returnundefunless($highlight&&defined$file_name);32083209# configuration for 'highlight' (http://www.andre-simon.de/)3210# match by basename3211my%highlight_basename= (3212#'Program' => 'py',3213#'Library' => 'py',3214'SConstruct'=>'py',# SCons equivalent of Makefile3215'Makefile'=>'make',3216);3217# match by extension3218my%highlight_ext= (3219# main extensions, defining name of syntax;3220# see files in /usr/share/highlight/langDefs/ directory3221map{$_=>$_}3222qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl),3223# alternate extensions, see /etc/highlight/filetypes.conf3224'h'=>'c',3225map{$_=>'cpp'}qw(cxx c++ cc),3226map{$_=>'php'}qw(php3 php4),3227map{$_=>'pl'}qw(perl pm),# perhaps also 'cgi'3228'mak'=>'make',3229map{$_=>'xml'}qw(xhtml html htm),3230);32313232my$basename= basename($file_name,'.in');3233return$highlight_basename{$basename}3234ifexists$highlight_basename{$basename};32353236$basename=~/\.([^.]*)$/;3237my$ext=$1orreturnundef;3238return$highlight_ext{$ext}3239ifexists$highlight_ext{$ext};32403241returnundef;3242}32433244# run highlighter and return FD of its output,3245# or return original FD if no highlighting3246sub run_highlighter {3247my($fd,$highlight,$syntax) =@_;3248return$fdunless($highlight&&defined$syntax);32493250close$fd3251or die_error(404,"Reading blob failed");3252open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3253"highlight --xhtml --fragment --syntax$syntax|"3254or die_error(500,"Couldn't open file or run syntax highlighter");3255return$fd;3256}32573258## ======================================================================3259## functions printing HTML: header, footer, error page32603261sub get_page_title {3262my$title= to_utf8($site_name);32633264return$titleunless(defined$project);3265$title.=" - ". to_utf8($project);32663267return$titleunless(defined$action);3268$title.="/$action";# $action is US-ASCII (7bit ASCII)32693270return$titleunless(defined$file_name);3271$title.=" - ". esc_path($file_name);3272if($actioneq"tree"&&$file_name!~ m|/$|) {3273$title.="/";3274}32753276return$title;3277}32783279sub git_header_html {3280my$status=shift||"200 OK";3281my$expires=shift;3282my%opts=@_;32833284my$title= get_page_title();3285my$content_type;3286# require explicit support from the UA if we are to send the page as3287# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3288# we have to do this because MSIE sometimes globs '*/*', pretending to3289# support xhtml+xml but choking when it gets what it asked for.3290if(defined$cgi->http('HTTP_ACCEPT') &&3291$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3292$cgi->Accept('application/xhtml+xml') !=0) {3293$content_type='application/xhtml+xml';3294}else{3295$content_type='text/html';3296}3297print$cgi->header(-type=>$content_type, -charset =>'utf-8',3298-status=>$status, -expires =>$expires)3299unless($opts{'-no_http_header'});3300my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3301print<<EOF;3302<?xml version="1.0" encoding="utf-8"?>3303<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3304<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3305<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3306<!-- git core binaries version$git_version-->3307<head>3308<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3309<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3310<meta name="robots" content="index, nofollow"/>3311<title>$title</title>3312EOF3313# the stylesheet, favicon etc urls won't work correctly with path_info3314# unless we set the appropriate base URL3315if($ENV{'PATH_INFO'}) {3316print"<base href=\"".esc_url($base_url)."\"/>\n";3317}3318# print out each stylesheet that exist, providing backwards capability3319# for those people who defined $stylesheet in a config file3320if(defined$stylesheet) {3321print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3322}else{3323foreachmy$stylesheet(@stylesheets) {3324next unless$stylesheet;3325print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3326}3327}3328if(defined$project) {3329my%href_params= get_feed_info();3330if(!exists$href_params{'-title'}) {3331$href_params{'-title'} ='log';3332}33333334foreachmy$formatqw(RSS Atom){3335my$type=lc($format);3336my%link_attr= (3337'-rel'=>'alternate',3338'-title'=>"$project-$href_params{'-title'} -$formatfeed",3339'-type'=>"application/$type+xml"3340);33413342$href_params{'action'} =$type;3343$link_attr{'-href'} = href(%href_params);3344print"<link ".3345"rel=\"$link_attr{'-rel'}\"".3346"title=\"$link_attr{'-title'}\"".3347"href=\"$link_attr{'-href'}\"".3348"type=\"$link_attr{'-type'}\"".3349"/>\n";33503351$href_params{'extra_options'} ='--no-merges';3352$link_attr{'-href'} = href(%href_params);3353$link_attr{'-title'} .=' (no merges)';3354print"<link ".3355"rel=\"$link_attr{'-rel'}\"".3356"title=\"$link_attr{'-title'}\"".3357"href=\"$link_attr{'-href'}\"".3358"type=\"$link_attr{'-type'}\"".3359"/>\n";3360}33613362}else{3363printf('<link rel="alternate" title="%sprojects list" '.3364'href="%s" type="text/plain; charset=utf-8" />'."\n",3365$site_name, href(project=>undef, action=>"project_index"));3366printf('<link rel="alternate" title="%sprojects feeds" '.3367'href="%s" type="text/x-opml" />'."\n",3368$site_name, href(project=>undef, action=>"opml"));3369}3370if(defined$favicon) {3371printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3372}33733374print"</head>\n".3375"<body>\n";33763377if(defined$site_header&& -f $site_header) {3378 insert_file($site_header);3379}33803381print"<div class=\"page_header\">\n".3382$cgi->a({-href => esc_url($logo_url),3383-title =>$logo_label},3384qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3385print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3386if(defined$project) {3387print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3388if(defined$action) {3389print" /$action";3390}3391print"\n";3392}3393print"</div>\n";33943395my$have_search= gitweb_check_feature('search');3396if(defined$project&&$have_search) {3397if(!defined$searchtext) {3398$searchtext="";3399}3400my$search_hash;3401if(defined$hash_base) {3402$search_hash=$hash_base;3403}elsif(defined$hash) {3404$search_hash=$hash;3405}else{3406$search_hash="HEAD";3407}3408my$action=$my_uri;3409my$use_pathinfo= gitweb_check_feature('pathinfo');3410if($use_pathinfo) {3411$action.="/".esc_url($project);3412}3413print$cgi->startform(-method=>"get", -action =>$action) .3414"<div class=\"search\">\n".3415(!$use_pathinfo&&3416$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3417$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3418$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3419$cgi->popup_menu(-name =>'st', -default=>'commit',3420-values=> ['commit','grep','author','committer','pickaxe']) .3421$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3422" search:\n",3423$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3424"<span title=\"Extended regular expression\">".3425$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3426-checked =>$search_use_regexp) .3427"</span>".3428"</div>".3429$cgi->end_form() ."\n";3430}3431}34323433sub git_footer_html {3434my$feed_class='rss_logo';34353436print"<div class=\"page_footer\">\n";3437if(defined$project) {3438my$descr= git_get_project_description($project);3439if(defined$descr) {3440print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3441}34423443my%href_params= get_feed_info();3444if(!%href_params) {3445$feed_class.=' generic';3446}3447$href_params{'-title'} ||='log';34483449foreachmy$formatqw(RSS Atom){3450$href_params{'action'} =lc($format);3451print$cgi->a({-href => href(%href_params),3452-title =>"$href_params{'-title'}$formatfeed",3453-class=>$feed_class},$format)."\n";3454}34553456}else{3457print$cgi->a({-href => href(project=>undef, action=>"opml"),3458-class=>$feed_class},"OPML") ." ";3459print$cgi->a({-href => href(project=>undef, action=>"project_index"),3460-class=>$feed_class},"TXT") ."\n";3461}3462print"</div>\n";# class="page_footer"34633464if(defined$t0&& gitweb_check_feature('timed')) {3465print"<div id=\"generating_info\">\n";3466print'This page took '.3467'<span id="generating_time" class="time_span">'.3468 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3469' seconds </span>'.3470' and '.3471'<span id="generating_cmd">'.3472$number_of_git_cmds.3473'</span> git commands '.3474" to generate.\n";3475print"</div>\n";# class="page_footer"3476}34773478if(defined$site_footer&& -f $site_footer) {3479 insert_file($site_footer);3480}34813482print qq!<script type="text/javascript" src="$javascript"></script>\n!;3483if(defined$action&&3484$actioneq'blame_incremental') {3485print qq!<script type="text/javascript">\n!.3486 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3487 qq!"!. href() .qq!");\n!.3488 qq!</script>\n!;3489}elsif(gitweb_check_feature('javascript-actions')) {3490print qq!<script type="text/javascript">\n!.3491 qq!window.onload = fixLinks;\n!.3492 qq!</script>\n!;3493}34943495print"</body>\n".3496"</html>";3497}34983499# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3500# Example: die_error(404, 'Hash not found')3501# By convention, use the following status codes (as defined in RFC 2616):3502# 400: Invalid or missing CGI parameters, or3503# requested object exists but has wrong type.3504# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3505# this server or project.3506# 404: Requested object/revision/project doesn't exist.3507# 500: The server isn't configured properly, or3508# an internal error occurred (e.g. failed assertions caused by bugs), or3509# an unknown error occurred (e.g. the git binary died unexpectedly).3510# 503: The server is currently unavailable (because it is overloaded,3511# or down for maintenance). Generally, this is a temporary state.3512sub die_error {3513my$status=shift||500;3514my$error= esc_html(shift) ||"Internal Server Error";3515my$extra=shift;3516my%opts=@_;35173518my%http_responses= (3519400=>'400 Bad Request',3520403=>'403 Forbidden',3521404=>'404 Not Found',3522500=>'500 Internal Server Error',3523503=>'503 Service Unavailable',3524);3525 git_header_html($http_responses{$status},undef,%opts);3526print<<EOF;3527<div class="page_body">3528<br /><br />3529$status-$error3530<br />3531EOF3532if(defined$extra) {3533print"<hr />\n".3534"$extra\n";3535}3536print"</div>\n";35373538 git_footer_html();3539goto DONE_GITWEB3540unless($opts{'-error_handler'});3541}35423543## ----------------------------------------------------------------------3544## functions printing or outputting HTML: navigation35453546sub git_print_page_nav {3547my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3548$extra=''if!defined$extra;# pager or formats35493550my@navs=qw(summary shortlog log commit commitdiff tree);3551if($suppress) {3552@navs=grep{$_ne$suppress}@navs;3553}35543555my%arg=map{$_=> {action=>$_} }@navs;3556if(defined$head) {3557for(qw(commit commitdiff)) {3558$arg{$_}{'hash'} =$head;3559}3560if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3561for(qw(shortlog log)) {3562$arg{$_}{'hash'} =$head;3563}3564}3565}35663567$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3568$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;35693570my@actions= gitweb_get_feature('actions');3571my%repl= (3572'%'=>'%',3573'n'=>$project,# project name3574'f'=>$git_dir,# project path within filesystem3575'h'=>$treehead||'',# current hash ('h' parameter)3576'b'=>$treebase||'',# hash base ('hb' parameter)3577);3578while(@actions) {3579my($label,$link,$pos) =splice(@actions,0,3);3580# insert3581@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3582# munch munch3583$link=~s/%([%nfhb])/$repl{$1}/g;3584$arg{$label}{'_href'} =$link;3585}35863587print"<div class=\"page_nav\">\n".3588(join" | ",3589map{$_eq$current?3590$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3591}@navs);3592print"<br/>\n$extra<br/>\n".3593"</div>\n";3594}35953596sub format_paging_nav {3597my($action,$page,$has_next_link) =@_;3598my$paging_nav;359936003601if($page>0) {3602$paging_nav.=3603$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3604" ⋅ ".3605$cgi->a({-href => href(-replay=>1, page=>$page-1),3606-accesskey =>"p", -title =>"Alt-p"},"prev");3607}else{3608$paging_nav.="first ⋅ prev";3609}36103611if($has_next_link) {3612$paging_nav.=" ⋅ ".3613$cgi->a({-href => href(-replay=>1, page=>$page+1),3614-accesskey =>"n", -title =>"Alt-n"},"next");3615}else{3616$paging_nav.=" ⋅ next";3617}36183619return$paging_nav;3620}36213622## ......................................................................3623## functions printing or outputting HTML: div36243625sub git_print_header_div {3626my($action,$title,$hash,$hash_base) =@_;3627my%args= ();36283629$args{'action'} =$action;3630$args{'hash'} =$hashif$hash;3631$args{'hash_base'} =$hash_baseif$hash_base;36323633print"<div class=\"header\">\n".3634$cgi->a({-href => href(%args), -class=>"title"},3635$title?$title:$action) .3636"\n</div>\n";3637}36383639sub print_local_time {3640print format_local_time(@_);3641}36423643sub format_local_time {3644my$localtime='';3645my%date=@_;3646if($date{'hour_local'} <6) {3647$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3648$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3649}else{3650$localtime.=sprintf(" (%02d:%02d%s)",3651$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3652}36533654return$localtime;3655}36563657# Outputs the author name and date in long form3658sub git_print_authorship {3659my$co=shift;3660my%opts=@_;3661my$tag=$opts{-tag} ||'div';3662my$author=$co->{'author_name'};36633664my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3665print"<$tagclass=\"author_date\">".3666 format_search_author($author,"author", esc_html($author)) .3667" [$ad{'rfc2822'}";3668 print_local_time(%ad)if($opts{-localtime});3669print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3670."</$tag>\n";3671}36723673# Outputs table rows containing the full author or committer information,3674# in the format expected for 'commit' view (& similia).3675# Parameters are a commit hash reference, followed by the list of people3676# to output information for. If the list is empty it defalts to both3677# author and committer.3678sub git_print_authorship_rows {3679my$co=shift;3680# too bad we can't use @people = @_ || ('author', 'committer')3681my@people=@_;3682@people= ('author','committer')unless@people;3683foreachmy$who(@people) {3684my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3685print"<tr><td>$who</td><td>".3686 format_search_author($co->{"${who}_name"},$who,3687 esc_html($co->{"${who}_name"})) ." ".3688 format_search_author($co->{"${who}_email"},$who,3689 esc_html("<".$co->{"${who}_email"} .">")) .3690"</td><td rowspan=\"2\">".3691 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3692"</td></tr>\n".3693"<tr>".3694"<td></td><td>$wd{'rfc2822'}";3695 print_local_time(%wd);3696print"</td>".3697"</tr>\n";3698}3699}37003701sub git_print_page_path {3702my$name=shift;3703my$type=shift;3704my$hb=shift;370537063707print"<div class=\"page_path\">";3708print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3709-title =>'tree root'}, to_utf8("[$project]"));3710print" / ";3711if(defined$name) {3712my@dirname=split'/',$name;3713my$basename=pop@dirname;3714my$fullname='';37153716foreachmy$dir(@dirname) {3717$fullname.= ($fullname?'/':'') .$dir;3718print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3719 hash_base=>$hb),3720-title =>$fullname}, esc_path($dir));3721print" / ";3722}3723if(defined$type&&$typeeq'blob') {3724print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3725 hash_base=>$hb),3726-title =>$name}, esc_path($basename));3727}elsif(defined$type&&$typeeq'tree') {3728print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3729 hash_base=>$hb),3730-title =>$name}, esc_path($basename));3731print" / ";3732}else{3733print esc_path($basename);3734}3735}3736print"<br/></div>\n";3737}37383739sub git_print_log {3740my$log=shift;3741my%opts=@_;37423743if($opts{'-remove_title'}) {3744# remove title, i.e. first line of log3745shift@$log;3746}3747# remove leading empty lines3748while(defined$log->[0] &&$log->[0]eq"") {3749shift@$log;3750}37513752# print log3753my$signoff=0;3754my$empty=0;3755foreachmy$line(@$log) {3756if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3757$signoff=1;3758$empty=0;3759if(!$opts{'-remove_signoff'}) {3760print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3761next;3762}else{3763# remove signoff lines3764next;3765}3766}else{3767$signoff=0;3768}37693770# print only one empty line3771# do not print empty line after signoff3772if($lineeq"") {3773next if($empty||$signoff);3774$empty=1;3775}else{3776$empty=0;3777}37783779print format_log_line_html($line) ."<br/>\n";3780}37813782if($opts{'-final_empty_line'}) {3783# end with single empty line3784print"<br/>\n"unless$empty;3785}3786}37873788# return link target (what link points to)3789sub git_get_link_target {3790my$hash=shift;3791my$link_target;37923793# read link3794open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3795orreturn;3796{3797local$/=undef;3798$link_target= <$fd>;3799}3800close$fd3801orreturn;38023803return$link_target;3804}38053806# given link target, and the directory (basedir) the link is in,3807# return target of link relative to top directory (top tree);3808# return undef if it is not possible (including absolute links).3809sub normalize_link_target {3810my($link_target,$basedir) =@_;38113812# absolute symlinks (beginning with '/') cannot be normalized3813return if(substr($link_target,0,1)eq'/');38143815# normalize link target to path from top (root) tree (dir)3816my$path;3817if($basedir) {3818$path=$basedir.'/'.$link_target;3819}else{3820# we are in top (root) tree (dir)3821$path=$link_target;3822}38233824# remove //, /./, and /../3825my@path_parts;3826foreachmy$part(split('/',$path)) {3827# discard '.' and ''3828next if(!$part||$parteq'.');3829# handle '..'3830if($parteq'..') {3831if(@path_parts) {3832pop@path_parts;3833}else{3834# link leads outside repository (outside top dir)3835return;3836}3837}else{3838push@path_parts,$part;3839}3840}3841$path=join('/',@path_parts);38423843return$path;3844}38453846# print tree entry (row of git_tree), but without encompassing <tr> element3847sub git_print_tree_entry {3848my($t,$basedir,$hash_base,$have_blame) =@_;38493850my%base_key= ();3851$base_key{'hash_base'} =$hash_baseifdefined$hash_base;38523853# The format of a table row is: mode list link. Where mode is3854# the mode of the entry, list is the name of the entry, an href,3855# and link is the action links of the entry.38563857print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3858if(exists$t->{'size'}) {3859print"<td class=\"size\">$t->{'size'}</td>\n";3860}3861if($t->{'type'}eq"blob") {3862print"<td class=\"list\">".3863$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3864 file_name=>"$basedir$t->{'name'}",%base_key),3865-class=>"list"}, esc_path($t->{'name'}));3866if(S_ISLNK(oct$t->{'mode'})) {3867my$link_target= git_get_link_target($t->{'hash'});3868if($link_target) {3869my$norm_target= normalize_link_target($link_target,$basedir);3870if(defined$norm_target) {3871print" -> ".3872$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3873 file_name=>$norm_target),3874-title =>$norm_target}, esc_path($link_target));3875}else{3876print" -> ". esc_path($link_target);3877}3878}3879}3880print"</td>\n";3881print"<td class=\"link\">";3882print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3883 file_name=>"$basedir$t->{'name'}",%base_key)},3884"blob");3885if($have_blame) {3886print" | ".3887$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3888 file_name=>"$basedir$t->{'name'}",%base_key)},3889"blame");3890}3891if(defined$hash_base) {3892print" | ".3893$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3894 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3895"history");3896}3897print" | ".3898$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3899 file_name=>"$basedir$t->{'name'}")},3900"raw");3901print"</td>\n";39023903}elsif($t->{'type'}eq"tree") {3904print"<td class=\"list\">";3905print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3906 file_name=>"$basedir$t->{'name'}",3907%base_key)},3908 esc_path($t->{'name'}));3909print"</td>\n";3910print"<td class=\"link\">";3911print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3912 file_name=>"$basedir$t->{'name'}",3913%base_key)},3914"tree");3915if(defined$hash_base) {3916print" | ".3917$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3918 file_name=>"$basedir$t->{'name'}")},3919"history");3920}3921print"</td>\n";3922}else{3923# unknown object: we can only present history for it3924# (this includes 'commit' object, i.e. submodule support)3925print"<td class=\"list\">".3926 esc_path($t->{'name'}) .3927"</td>\n";3928print"<td class=\"link\">";3929if(defined$hash_base) {3930print$cgi->a({-href => href(action=>"history",3931 hash_base=>$hash_base,3932 file_name=>"$basedir$t->{'name'}")},3933"history");3934}3935print"</td>\n";3936}3937}39383939## ......................................................................3940## functions printing large fragments of HTML39413942# get pre-image filenames for merge (combined) diff3943sub fill_from_file_info {3944my($diff,@parents) =@_;39453946$diff->{'from_file'} = [ ];3947$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3948for(my$i=0;$i<$diff->{'nparents'};$i++) {3949if($diff->{'status'}[$i]eq'R'||3950$diff->{'status'}[$i]eq'C') {3951$diff->{'from_file'}[$i] =3952 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3953}3954}39553956return$diff;3957}39583959# is current raw difftree line of file deletion3960sub is_deleted {3961my$diffinfo=shift;39623963return$diffinfo->{'to_id'}eq('0' x 40);3964}39653966# does patch correspond to [previous] difftree raw line3967# $diffinfo - hashref of parsed raw diff format3968# $patchinfo - hashref of parsed patch diff format3969# (the same keys as in $diffinfo)3970sub is_patch_split {3971my($diffinfo,$patchinfo) =@_;39723973returndefined$diffinfo&&defined$patchinfo3974&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3975}397639773978sub git_difftree_body {3979my($difftree,$hash,@parents) =@_;3980my($parent) =$parents[0];3981my$have_blame= gitweb_check_feature('blame');3982print"<div class=\"list_head\">\n";3983if($#{$difftree} >10) {3984print(($#{$difftree} +1) ." files changed:\n");3985}3986print"</div>\n";39873988print"<table class=\"".3989(@parents>1?"combined ":"") .3990"diff_tree\">\n";39913992# header only for combined diff in 'commitdiff' view3993my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3994if($has_header) {3995# table header3996print"<thead><tr>\n".3997"<th></th><th></th>\n";# filename, patchN link3998for(my$i=0;$i<@parents;$i++) {3999my$par=$parents[$i];4000print"<th>".4001$cgi->a({-href => href(action=>"commitdiff",4002 hash=>$hash, hash_parent=>$par),4003-title =>'commitdiff to parent number '.4004($i+1) .': '.substr($par,0,7)},4005$i+1) .4006" </th>\n";4007}4008print"</tr></thead>\n<tbody>\n";4009}40104011my$alternate=1;4012my$patchno=0;4013foreachmy$line(@{$difftree}) {4014my$diff= parsed_difftree_line($line);40154016if($alternate) {4017print"<tr class=\"dark\">\n";4018}else{4019print"<tr class=\"light\">\n";4020}4021$alternate^=1;40224023if(exists$diff->{'nparents'}) {# combined diff40244025 fill_from_file_info($diff,@parents)4026unlessexists$diff->{'from_file'};40274028if(!is_deleted($diff)) {4029# file exists in the result (child) commit4030print"<td>".4031$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4032 file_name=>$diff->{'to_file'},4033 hash_base=>$hash),4034-class=>"list"}, esc_path($diff->{'to_file'})) .4035"</td>\n";4036}else{4037print"<td>".4038 esc_path($diff->{'to_file'}) .4039"</td>\n";4040}40414042if($actioneq'commitdiff') {4043# link to patch4044$patchno++;4045print"<td class=\"link\">".4046$cgi->a({-href =>"#patch$patchno"},"patch") .4047" | ".4048"</td>\n";4049}40504051my$has_history=0;4052my$not_deleted=0;4053for(my$i=0;$i<$diff->{'nparents'};$i++) {4054my$hash_parent=$parents[$i];4055my$from_hash=$diff->{'from_id'}[$i];4056my$from_path=$diff->{'from_file'}[$i];4057my$status=$diff->{'status'}[$i];40584059$has_history||= ($statusne'A');4060$not_deleted||= ($statusne'D');40614062if($statuseq'A') {4063print"<td class=\"link\"align=\"right\"> | </td>\n";4064}elsif($statuseq'D') {4065print"<td class=\"link\">".4066$cgi->a({-href => href(action=>"blob",4067 hash_base=>$hash,4068 hash=>$from_hash,4069 file_name=>$from_path)},4070"blob". ($i+1)) .4071" | </td>\n";4072}else{4073if($diff->{'to_id'}eq$from_hash) {4074print"<td class=\"link nochange\">";4075}else{4076print"<td class=\"link\">";4077}4078print$cgi->a({-href => href(action=>"blobdiff",4079 hash=>$diff->{'to_id'},4080 hash_parent=>$from_hash,4081 hash_base=>$hash,4082 hash_parent_base=>$hash_parent,4083 file_name=>$diff->{'to_file'},4084 file_parent=>$from_path)},4085"diff". ($i+1)) .4086" | </td>\n";4087}4088}40894090print"<td class=\"link\">";4091if($not_deleted) {4092print$cgi->a({-href => href(action=>"blob",4093 hash=>$diff->{'to_id'},4094 file_name=>$diff->{'to_file'},4095 hash_base=>$hash)},4096"blob");4097print" | "if($has_history);4098}4099if($has_history) {4100print$cgi->a({-href => href(action=>"history",4101 file_name=>$diff->{'to_file'},4102 hash_base=>$hash)},4103"history");4104}4105print"</td>\n";41064107print"</tr>\n";4108next;# instead of 'else' clause, to avoid extra indent4109}4110# else ordinary diff41114112my($to_mode_oct,$to_mode_str,$to_file_type);4113my($from_mode_oct,$from_mode_str,$from_file_type);4114if($diff->{'to_mode'}ne('0' x 6)) {4115$to_mode_oct=oct$diff->{'to_mode'};4116if(S_ISREG($to_mode_oct)) {# only for regular file4117$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4118}4119$to_file_type= file_type($diff->{'to_mode'});4120}4121if($diff->{'from_mode'}ne('0' x 6)) {4122$from_mode_oct=oct$diff->{'from_mode'};4123if(S_ISREG($to_mode_oct)) {# only for regular file4124$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4125}4126$from_file_type= file_type($diff->{'from_mode'});4127}41284129if($diff->{'status'}eq"A") {# created4130my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4131$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4132$mode_chng.="]</span>";4133print"<td>";4134print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4135 hash_base=>$hash, file_name=>$diff->{'file'}),4136-class=>"list"}, esc_path($diff->{'file'}));4137print"</td>\n";4138print"<td>$mode_chng</td>\n";4139print"<td class=\"link\">";4140if($actioneq'commitdiff') {4141# link to patch4142$patchno++;4143print$cgi->a({-href =>"#patch$patchno"},"patch");4144print" | ";4145}4146print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4147 hash_base=>$hash, file_name=>$diff->{'file'})},4148"blob");4149print"</td>\n";41504151}elsif($diff->{'status'}eq"D") {# deleted4152my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4153print"<td>";4154print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4155 hash_base=>$parent, file_name=>$diff->{'file'}),4156-class=>"list"}, esc_path($diff->{'file'}));4157print"</td>\n";4158print"<td>$mode_chng</td>\n";4159print"<td class=\"link\">";4160if($actioneq'commitdiff') {4161# link to patch4162$patchno++;4163print$cgi->a({-href =>"#patch$patchno"},"patch");4164print" | ";4165}4166print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4167 hash_base=>$parent, file_name=>$diff->{'file'})},4168"blob") ." | ";4169if($have_blame) {4170print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4171 file_name=>$diff->{'file'})},4172"blame") ." | ";4173}4174print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4175 file_name=>$diff->{'file'})},4176"history");4177print"</td>\n";41784179}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4180my$mode_chnge="";4181if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4182$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4183if($from_file_typene$to_file_type) {4184$mode_chnge.=" from$from_file_typeto$to_file_type";4185}4186if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4187if($from_mode_str&&$to_mode_str) {4188$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4189}elsif($to_mode_str) {4190$mode_chnge.=" mode:$to_mode_str";4191}4192}4193$mode_chnge.="]</span>\n";4194}4195print"<td>";4196print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4197 hash_base=>$hash, file_name=>$diff->{'file'}),4198-class=>"list"}, esc_path($diff->{'file'}));4199print"</td>\n";4200print"<td>$mode_chnge</td>\n";4201print"<td class=\"link\">";4202if($actioneq'commitdiff') {4203# link to patch4204$patchno++;4205print$cgi->a({-href =>"#patch$patchno"},"patch") .4206" | ";4207}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4208# "commit" view and modified file (not onlu mode changed)4209print$cgi->a({-href => href(action=>"blobdiff",4210 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4211 hash_base=>$hash, hash_parent_base=>$parent,4212 file_name=>$diff->{'file'})},4213"diff") .4214" | ";4215}4216print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4217 hash_base=>$hash, file_name=>$diff->{'file'})},4218"blob") ." | ";4219if($have_blame) {4220print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4221 file_name=>$diff->{'file'})},4222"blame") ." | ";4223}4224print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4225 file_name=>$diff->{'file'})},4226"history");4227print"</td>\n";42284229}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4230my%status_name= ('R'=>'moved','C'=>'copied');4231my$nstatus=$status_name{$diff->{'status'}};4232my$mode_chng="";4233if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4234# mode also for directories, so we cannot use $to_mode_str4235$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4236}4237print"<td>".4238$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4239 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4240-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4241"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4242$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4243 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4244-class=>"list"}, esc_path($diff->{'from_file'})) .4245" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4246"<td class=\"link\">";4247if($actioneq'commitdiff') {4248# link to patch4249$patchno++;4250print$cgi->a({-href =>"#patch$patchno"},"patch") .4251" | ";4252}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4253# "commit" view and modified file (not only pure rename or copy)4254print$cgi->a({-href => href(action=>"blobdiff",4255 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4256 hash_base=>$hash, hash_parent_base=>$parent,4257 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4258"diff") .4259" | ";4260}4261print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4262 hash_base=>$parent, file_name=>$diff->{'to_file'})},4263"blob") ." | ";4264if($have_blame) {4265print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4266 file_name=>$diff->{'to_file'})},4267"blame") ." | ";4268}4269print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4270 file_name=>$diff->{'to_file'})},4271"history");4272print"</td>\n";42734274}# we should not encounter Unmerged (U) or Unknown (X) status4275print"</tr>\n";4276}4277print"</tbody>"if$has_header;4278print"</table>\n";4279}42804281sub git_patchset_body {4282my($fd,$difftree,$hash,@hash_parents) =@_;4283my($hash_parent) =$hash_parents[0];42844285my$is_combined= (@hash_parents>1);4286my$patch_idx=0;4287my$patch_number=0;4288my$patch_line;4289my$diffinfo;4290my$to_name;4291my(%from,%to);42924293print"<div class=\"patchset\">\n";42944295# skip to first patch4296while($patch_line= <$fd>) {4297chomp$patch_line;42984299last if($patch_line=~m/^diff /);4300}43014302 PATCH:4303while($patch_line) {43044305# parse "git diff" header line4306if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4307# $1 is from_name, which we do not use4308$to_name= unquote($2);4309$to_name=~s!^b/!!;4310}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4311# $1 is 'cc' or 'combined', which we do not use4312$to_name= unquote($2);4313}else{4314$to_name=undef;4315}43164317# check if current patch belong to current raw line4318# and parse raw git-diff line if needed4319if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4320# this is continuation of a split patch4321print"<div class=\"patch cont\">\n";4322}else{4323# advance raw git-diff output if needed4324$patch_idx++ifdefined$diffinfo;43254326# read and prepare patch information4327$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);43284329# compact combined diff output can have some patches skipped4330# find which patch (using pathname of result) we are at now;4331if($is_combined) {4332while($to_namene$diffinfo->{'to_file'}) {4333print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4334 format_diff_cc_simplified($diffinfo,@hash_parents) .4335"</div>\n";# class="patch"43364337$patch_idx++;4338$patch_number++;43394340last if$patch_idx>$#$difftree;4341$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4342}4343}43444345# modifies %from, %to hashes4346 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);43474348# this is first patch for raw difftree line with $patch_idx index4349# we index @$difftree array from 0, but number patches from 14350print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4351}43524353# git diff header4354#assert($patch_line =~ m/^diff /) if DEBUG;4355#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4356$patch_number++;4357# print "git diff" header4358print format_git_diff_header_line($patch_line,$diffinfo,4359 \%from, \%to);43604361# print extended diff header4362print"<div class=\"diff extended_header\">\n";4363 EXTENDED_HEADER:4364while($patch_line= <$fd>) {4365chomp$patch_line;43664367last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);43684369print format_extended_diff_header_line($patch_line,$diffinfo,4370 \%from, \%to);4371}4372print"</div>\n";# class="diff extended_header"43734374# from-file/to-file diff header4375if(!$patch_line) {4376print"</div>\n";# class="patch"4377last PATCH;4378}4379next PATCH if($patch_line=~m/^diff /);4380#assert($patch_line =~ m/^---/) if DEBUG;43814382my$last_patch_line=$patch_line;4383$patch_line= <$fd>;4384chomp$patch_line;4385#assert($patch_line =~ m/^\+\+\+/) if DEBUG;43864387print format_diff_from_to_header($last_patch_line,$patch_line,4388$diffinfo, \%from, \%to,4389@hash_parents);43904391# the patch itself4392 LINE:4393while($patch_line= <$fd>) {4394chomp$patch_line;43954396next PATCH if($patch_line=~m/^diff /);43974398print format_diff_line($patch_line, \%from, \%to);4399}44004401}continue{4402print"</div>\n";# class="patch"4403}44044405# for compact combined (--cc) format, with chunk and patch simpliciaction4406# patchset might be empty, but there might be unprocessed raw lines4407for(++$patch_idxif$patch_number>0;4408$patch_idx<@$difftree;4409++$patch_idx) {4410# read and prepare patch information4411$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);44124413# generate anchor for "patch" links in difftree / whatchanged part4414print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4415 format_diff_cc_simplified($diffinfo,@hash_parents) .4416"</div>\n";# class="patch"44174418$patch_number++;4419}44204421if($patch_number==0) {4422if(@hash_parents>1) {4423print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4424}else{4425print"<div class=\"diff nodifferences\">No differences found</div>\n";4426}4427}44284429print"</div>\n";# class="patchset"4430}44314432# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44334434# fills project list info (age, description, owner, forks) for each4435# project in the list, removing invalid projects from returned list4436# NOTE: modifies $projlist, but does not remove entries from it4437sub fill_project_list_info {4438my($projlist,$check_forks) =@_;4439my@projects;44404441my$show_ctags= gitweb_check_feature('ctags');4442 PROJECT:4443foreachmy$pr(@$projlist) {4444my(@activity) = git_get_last_activity($pr->{'path'});4445unless(@activity) {4446next PROJECT;4447}4448($pr->{'age'},$pr->{'age_string'}) =@activity;4449if(!defined$pr->{'descr'}) {4450my$descr= git_get_project_description($pr->{'path'}) ||"";4451$descr= to_utf8($descr);4452$pr->{'descr_long'} =$descr;4453$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4454}4455if(!defined$pr->{'owner'}) {4456$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4457}4458if($check_forks) {4459my$pname=$pr->{'path'};4460if(($pname=~s/\.git$//) &&4461($pname!~/\/$/) &&4462(-d "$projectroot/$pname")) {4463$pr->{'forks'} ="-d$projectroot/$pname";4464}else{4465$pr->{'forks'} =0;4466}4467}4468$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4469push@projects,$pr;4470}44714472return@projects;4473}44744475# print 'sort by' <th> element, generating 'sort by $name' replay link4476# if that order is not selected4477sub print_sort_th {4478print format_sort_th(@_);4479}44804481sub format_sort_th {4482my($name,$order,$header) =@_;4483my$sort_th="";4484$header||=ucfirst($name);44854486if($ordereq$name) {4487$sort_th.="<th>$header</th>\n";4488}else{4489$sort_th.="<th>".4490$cgi->a({-href => href(-replay=>1, order=>$name),4491-class=>"header"},$header) .4492"</th>\n";4493}44944495return$sort_th;4496}44974498sub git_project_list_body {4499# actually uses global variable $project4500my($projlist,$order,$from,$to,$extra,$no_header) =@_;45014502my$check_forks= gitweb_check_feature('forks');4503my@projects= fill_project_list_info($projlist,$check_forks);45044505$order||=$default_projects_order;4506$from=0unlessdefined$from;4507$to=$#projectsif(!defined$to||$#projects<$to);45084509my%order_info= (4510 project => { key =>'path', type =>'str'},4511 descr => { key =>'descr_long', type =>'str'},4512 owner => { key =>'owner', type =>'str'},4513 age => { key =>'age', type =>'num'}4514);4515my$oi=$order_info{$order};4516if($oi->{'type'}eq'str') {4517@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4518}else{4519@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4520}45214522my$show_ctags= gitweb_check_feature('ctags');4523if($show_ctags) {4524my%ctags;4525foreachmy$p(@projects) {4526foreachmy$ct(keys%{$p->{'ctags'}}) {4527$ctags{$ct} +=$p->{'ctags'}->{$ct};4528}4529}4530my$cloud= git_populate_project_tagcloud(\%ctags);4531print git_show_project_tagcloud($cloud,64);4532}45334534print"<table class=\"project_list\">\n";4535unless($no_header) {4536print"<tr>\n";4537if($check_forks) {4538print"<th></th>\n";4539}4540 print_sort_th('project',$order,'Project');4541 print_sort_th('descr',$order,'Description');4542 print_sort_th('owner',$order,'Owner');4543 print_sort_th('age',$order,'Last Change');4544print"<th></th>\n".# for links4545"</tr>\n";4546}4547my$alternate=1;4548my$tagfilter=$cgi->param('by_tag');4549for(my$i=$from;$i<=$to;$i++) {4550my$pr=$projects[$i];45514552next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4553next if$searchtextand not$pr->{'path'} =~/$searchtext/4554and not$pr->{'descr_long'} =~/$searchtext/;4555# Weed out forks or non-matching entries of search4556if($check_forks) {4557my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4558$forkbase="^$forkbase"if$forkbase;4559next ifnot$searchtextand not$tagfilterand$show_ctags4560and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4561}45624563if($alternate) {4564print"<tr class=\"dark\">\n";4565}else{4566print"<tr class=\"light\">\n";4567}4568$alternate^=1;4569if($check_forks) {4570print"<td>";4571if($pr->{'forks'}) {4572print"<!--$pr->{'forks'} -->\n";4573print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4574}4575print"</td>\n";4576}4577print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4578-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4579"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4580-class=>"list", -title =>$pr->{'descr_long'}},4581 esc_html($pr->{'descr'})) ."</td>\n".4582"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4583print"<td class=\"". age_class($pr->{'age'}) ."\">".4584(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4585"<td class=\"link\">".4586$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4587$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4588$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4589$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4590($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4591"</td>\n".4592"</tr>\n";4593}4594if(defined$extra) {4595print"<tr>\n";4596if($check_forks) {4597print"<td></td>\n";4598}4599print"<td colspan=\"5\">$extra</td>\n".4600"</tr>\n";4601}4602print"</table>\n";4603}46044605sub git_log_body {4606# uses global variable $project4607my($commitlist,$from,$to,$refs,$extra) =@_;46084609$from=0unlessdefined$from;4610$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);46114612for(my$i=0;$i<=$to;$i++) {4613my%co= %{$commitlist->[$i]};4614next if!%co;4615my$commit=$co{'id'};4616my$ref= format_ref_marker($refs,$commit);4617my%ad= parse_date($co{'author_epoch'});4618 git_print_header_div('commit',4619"<span class=\"age\">$co{'age_string'}</span>".4620 esc_html($co{'title'}) .$ref,4621$commit);4622print"<div class=\"title_text\">\n".4623"<div class=\"log_link\">\n".4624$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4625" | ".4626$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4627" | ".4628$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4629"<br/>\n".4630"</div>\n";4631 git_print_authorship(\%co, -tag =>'span');4632print"<br/>\n</div>\n";46334634print"<div class=\"log_body\">\n";4635 git_print_log($co{'comment'}, -final_empty_line=>1);4636print"</div>\n";4637}4638if($extra) {4639print"<div class=\"page_nav\">\n";4640print"$extra\n";4641print"</div>\n";4642}4643}46444645sub git_shortlog_body {4646# uses global variable $project4647my($commitlist,$from,$to,$refs,$extra) =@_;46484649$from=0unlessdefined$from;4650$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);46514652print"<table class=\"shortlog\">\n";4653my$alternate=1;4654for(my$i=$from;$i<=$to;$i++) {4655my%co= %{$commitlist->[$i]};4656my$commit=$co{'id'};4657my$ref= format_ref_marker($refs,$commit);4658if($alternate) {4659print"<tr class=\"dark\">\n";4660}else{4661print"<tr class=\"light\">\n";4662}4663$alternate^=1;4664# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4665print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4666 format_author_html('td', \%co,10) ."<td>";4667print format_subject_html($co{'title'},$co{'title_short'},4668 href(action=>"commit", hash=>$commit),$ref);4669print"</td>\n".4670"<td class=\"link\">".4671$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4672$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4673$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4674my$snapshot_links= format_snapshot_links($commit);4675if(defined$snapshot_links) {4676print" | ".$snapshot_links;4677}4678print"</td>\n".4679"</tr>\n";4680}4681if(defined$extra) {4682print"<tr>\n".4683"<td colspan=\"4\">$extra</td>\n".4684"</tr>\n";4685}4686print"</table>\n";4687}46884689sub git_history_body {4690# Warning: assumes constant type (blob or tree) during history4691my($commitlist,$from,$to,$refs,$extra,4692$file_name,$file_hash,$ftype) =@_;46934694$from=0unlessdefined$from;4695$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});46964697print"<table class=\"history\">\n";4698my$alternate=1;4699for(my$i=$from;$i<=$to;$i++) {4700my%co= %{$commitlist->[$i]};4701if(!%co) {4702next;4703}4704my$commit=$co{'id'};47054706my$ref= format_ref_marker($refs,$commit);47074708if($alternate) {4709print"<tr class=\"dark\">\n";4710}else{4711print"<tr class=\"light\">\n";4712}4713$alternate^=1;4714print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4715# shortlog: format_author_html('td', \%co, 10)4716 format_author_html('td', \%co,15,3) ."<td>";4717# originally git_history used chop_str($co{'title'}, 50)4718print format_subject_html($co{'title'},$co{'title_short'},4719 href(action=>"commit", hash=>$commit),$ref);4720print"</td>\n".4721"<td class=\"link\">".4722$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4723$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");47244725if($ftypeeq'blob') {4726my$blob_current=$file_hash;4727my$blob_parent= git_get_hash_by_path($commit,$file_name);4728if(defined$blob_current&&defined$blob_parent&&4729$blob_currentne$blob_parent) {4730print" | ".4731$cgi->a({-href => href(action=>"blobdiff",4732 hash=>$blob_current, hash_parent=>$blob_parent,4733 hash_base=>$hash_base, hash_parent_base=>$commit,4734 file_name=>$file_name)},4735"diff to current");4736}4737}4738print"</td>\n".4739"</tr>\n";4740}4741if(defined$extra) {4742print"<tr>\n".4743"<td colspan=\"4\">$extra</td>\n".4744"</tr>\n";4745}4746print"</table>\n";4747}47484749sub git_tags_body {4750# uses global variable $project4751my($taglist,$from,$to,$extra) =@_;4752$from=0unlessdefined$from;4753$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);47544755print"<table class=\"tags\">\n";4756my$alternate=1;4757for(my$i=$from;$i<=$to;$i++) {4758my$entry=$taglist->[$i];4759my%tag=%$entry;4760my$comment=$tag{'subject'};4761my$comment_short;4762if(defined$comment) {4763$comment_short= chop_str($comment,30,5);4764}4765if($alternate) {4766print"<tr class=\"dark\">\n";4767}else{4768print"<tr class=\"light\">\n";4769}4770$alternate^=1;4771if(defined$tag{'age'}) {4772print"<td><i>$tag{'age'}</i></td>\n";4773}else{4774print"<td></td>\n";4775}4776print"<td>".4777$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4778-class=>"list name"}, esc_html($tag{'name'})) .4779"</td>\n".4780"<td>";4781if(defined$comment) {4782print format_subject_html($comment,$comment_short,4783 href(action=>"tag", hash=>$tag{'id'}));4784}4785print"</td>\n".4786"<td class=\"selflink\">";4787if($tag{'type'}eq"tag") {4788print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4789}else{4790print" ";4791}4792print"</td>\n".4793"<td class=\"link\">"." | ".4794$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4795if($tag{'reftype'}eq"commit") {4796print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4797" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4798}elsif($tag{'reftype'}eq"blob") {4799print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4800}4801print"</td>\n".4802"</tr>";4803}4804if(defined$extra) {4805print"<tr>\n".4806"<td colspan=\"5\">$extra</td>\n".4807"</tr>\n";4808}4809print"</table>\n";4810}48114812sub git_heads_body {4813# uses global variable $project4814my($headlist,$head,$from,$to,$extra) =@_;4815$from=0unlessdefined$from;4816$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);48174818print"<table class=\"heads\">\n";4819my$alternate=1;4820for(my$i=$from;$i<=$to;$i++) {4821my$entry=$headlist->[$i];4822my%ref=%$entry;4823my$curr=$ref{'id'}eq$head;4824if($alternate) {4825print"<tr class=\"dark\">\n";4826}else{4827print"<tr class=\"light\">\n";4828}4829$alternate^=1;4830print"<td><i>$ref{'age'}</i></td>\n".4831($curr?"<td class=\"current_head\">":"<td>") .4832$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4833-class=>"list name"},esc_html($ref{'name'})) .4834"</td>\n".4835"<td class=\"link\">".4836$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4837$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4838$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4839"</td>\n".4840"</tr>";4841}4842if(defined$extra) {4843print"<tr>\n".4844"<td colspan=\"3\">$extra</td>\n".4845"</tr>\n";4846}4847print"</table>\n";4848}48494850sub git_search_grep_body {4851my($commitlist,$from,$to,$extra) =@_;4852$from=0unlessdefined$from;4853$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);48544855print"<table class=\"commit_search\">\n";4856my$alternate=1;4857for(my$i=$from;$i<=$to;$i++) {4858my%co= %{$commitlist->[$i]};4859if(!%co) {4860next;4861}4862my$commit=$co{'id'};4863if($alternate) {4864print"<tr class=\"dark\">\n";4865}else{4866print"<tr class=\"light\">\n";4867}4868$alternate^=1;4869print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4870 format_author_html('td', \%co,15,5) .4871"<td>".4872$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4873-class=>"list subject"},4874 chop_and_escape_str($co{'title'},50) ."<br/>");4875my$comment=$co{'comment'};4876foreachmy$line(@$comment) {4877if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4878my($lead,$match,$trail) = ($1,$2,$3);4879$match= chop_str($match,70,5,'center');4880my$contextlen=int((80-length($match))/2);4881$contextlen=30if($contextlen>30);4882$lead= chop_str($lead,$contextlen,10,'left');4883$trail= chop_str($trail,$contextlen,10,'right');48844885$lead= esc_html($lead);4886$match= esc_html($match);4887$trail= esc_html($trail);48884889print"$lead<span class=\"match\">$match</span>$trail<br />";4890}4891}4892print"</td>\n".4893"<td class=\"link\">".4894$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4895" | ".4896$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4897" | ".4898$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4899print"</td>\n".4900"</tr>\n";4901}4902if(defined$extra) {4903print"<tr>\n".4904"<td colspan=\"3\">$extra</td>\n".4905"</tr>\n";4906}4907print"</table>\n";4908}49094910## ======================================================================4911## ======================================================================4912## actions49134914sub git_project_list {4915my$order=$input_params{'order'};4916if(defined$order&&$order!~m/none|project|descr|owner|age/) {4917 die_error(400,"Unknown order parameter");4918}49194920my@list= git_get_projects_list();4921if(!@list) {4922 die_error(404,"No projects found");4923}49244925 git_header_html();4926if(defined$home_text&& -f $home_text) {4927print"<div class=\"index_include\">\n";4928 insert_file($home_text);4929print"</div>\n";4930}4931print$cgi->startform(-method=>"get") .4932"<p class=\"projsearch\">Search:\n".4933$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4934"</p>".4935$cgi->end_form() ."\n";4936 git_project_list_body(\@list,$order);4937 git_footer_html();4938}49394940sub git_forks {4941my$order=$input_params{'order'};4942if(defined$order&&$order!~m/none|project|descr|owner|age/) {4943 die_error(400,"Unknown order parameter");4944}49454946my@list= git_get_projects_list($project);4947if(!@list) {4948 die_error(404,"No forks found");4949}49504951 git_header_html();4952 git_print_page_nav('','');4953 git_print_header_div('summary',"$projectforks");4954 git_project_list_body(\@list,$order);4955 git_footer_html();4956}49574958sub git_project_index {4959my@projects= git_get_projects_list($project);49604961print$cgi->header(4962-type =>'text/plain',4963-charset =>'utf-8',4964-content_disposition =>'inline; filename="index.aux"');49654966foreachmy$pr(@projects) {4967if(!exists$pr->{'owner'}) {4968$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4969}49704971my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4972# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4973$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4974$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4975$path=~s/ /\+/g;4976$owner=~s/ /\+/g;49774978print"$path$owner\n";4979}4980}49814982sub git_summary {4983my$descr= git_get_project_description($project) ||"none";4984my%co= parse_commit("HEAD");4985my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4986my$head=$co{'id'};49874988my$owner= git_get_project_owner($project);49894990my$refs= git_get_references();4991# These get_*_list functions return one more to allow us to see if4992# there are more ...4993my@taglist= git_get_tags_list(16);4994my@headlist= git_get_heads_list(16);4995my@forklist;4996my$check_forks= gitweb_check_feature('forks');49974998if($check_forks) {4999@forklist= git_get_projects_list($project);5000}50015002 git_header_html();5003 git_print_page_nav('summary','',$head);50045005print"<div class=\"title\"> </div>\n";5006print"<table class=\"projects_list\">\n".5007"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".5008"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";5009if(defined$cd{'rfc2822'}) {5010print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";5011}50125013# use per project git URL list in $projectroot/$project/cloneurl5014# or make project git URL from git base URL and project name5015my$url_tag="URL";5016my@url_list= git_get_project_url_list($project);5017@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;5018foreachmy$git_url(@url_list) {5019next unless$git_url;5020print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";5021$url_tag="";5022}50235024# Tag cloud5025my$show_ctags= gitweb_check_feature('ctags');5026if($show_ctags) {5027my$ctags= git_get_project_ctags($project);5028my$cloud= git_populate_project_tagcloud($ctags);5029print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";5030print"</td>\n<td>"unless%$ctags;5031print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";5032print"</td>\n<td>"if%$ctags;5033print git_show_project_tagcloud($cloud,48);5034print"</td></tr>";5035}50365037print"</table>\n";50385039# If XSS prevention is on, we don't include README.html.5040# TODO: Allow a readme in some safe format.5041if(!$prevent_xss&& -s "$projectroot/$project/README.html") {5042print"<div class=\"title\">readme</div>\n".5043"<div class=\"readme\">\n";5044 insert_file("$projectroot/$project/README.html");5045print"\n</div>\n";# class="readme"5046}50475048# we need to request one more than 16 (0..15) to check if5049# those 16 are all5050my@commitlist=$head? parse_commits($head,17) : ();5051if(@commitlist) {5052 git_print_header_div('shortlog');5053 git_shortlog_body(\@commitlist,0,15,$refs,5054$#commitlist<=15?undef:5055$cgi->a({-href => href(action=>"shortlog")},"..."));5056}50575058if(@taglist) {5059 git_print_header_div('tags');5060 git_tags_body(\@taglist,0,15,5061$#taglist<=15?undef:5062$cgi->a({-href => href(action=>"tags")},"..."));5063}50645065if(@headlist) {5066 git_print_header_div('heads');5067 git_heads_body(\@headlist,$head,0,15,5068$#headlist<=15?undef:5069$cgi->a({-href => href(action=>"heads")},"..."));5070}50715072if(@forklist) {5073 git_print_header_div('forks');5074 git_project_list_body(\@forklist,'age',0,15,5075$#forklist<=15?undef:5076$cgi->a({-href => href(action=>"forks")},"..."),5077'no_header');5078}50795080 git_footer_html();5081}50825083sub git_tag {5084my$head= git_get_head_hash($project);5085 git_header_html();5086 git_print_page_nav('','',$head,undef,$head);5087my%tag= parse_tag($hash);50885089if(!%tag) {5090 die_error(404,"Unknown tag object");5091}50925093 git_print_header_div('commit', esc_html($tag{'name'}),$hash);5094print"<div class=\"title_text\">\n".5095"<table class=\"object_header\">\n".5096"<tr>\n".5097"<td>object</td>\n".5098"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5099$tag{'object'}) ."</td>\n".5100"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5101$tag{'type'}) ."</td>\n".5102"</tr>\n";5103if(defined($tag{'author'})) {5104 git_print_authorship_rows(\%tag,'author');5105}5106print"</table>\n\n".5107"</div>\n";5108print"<div class=\"page_body\">";5109my$comment=$tag{'comment'};5110foreachmy$line(@$comment) {5111chomp$line;5112print esc_html($line, -nbsp=>1) ."<br/>\n";5113}5114print"</div>\n";5115 git_footer_html();5116}51175118sub git_blame_common {5119my$format=shift||'porcelain';5120if($formateq'porcelain'&&$cgi->param('js')) {5121$format='incremental';5122$action='blame_incremental';# for page title etc5123}51245125# permissions5126 gitweb_check_feature('blame')5127or die_error(403,"Blame view not allowed");51285129# error checking5130 die_error(400,"No file name given")unless$file_name;5131$hash_base||= git_get_head_hash($project);5132 die_error(404,"Couldn't find base commit")unless$hash_base;5133my%co= parse_commit($hash_base)5134or die_error(404,"Commit not found");5135my$ftype="blob";5136if(!defined$hash) {5137$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5138or die_error(404,"Error looking up file");5139}else{5140$ftype= git_get_type($hash);5141if($ftype!~"blob") {5142 die_error(400,"Object is not a blob");5143}5144}51455146my$fd;5147if($formateq'incremental') {5148# get file contents (as base)5149open$fd,"-|", git_cmd(),'cat-file','blob',$hash5150or die_error(500,"Open git-cat-file failed");5151}elsif($formateq'data') {5152# run git-blame --incremental5153open$fd,"-|", git_cmd(),"blame","--incremental",5154$hash_base,"--",$file_name5155or die_error(500,"Open git-blame --incremental failed");5156}else{5157# run git-blame --porcelain5158open$fd,"-|", git_cmd(),"blame",'-p',5159$hash_base,'--',$file_name5160or die_error(500,"Open git-blame --porcelain failed");5161}51625163# incremental blame data returns early5164if($formateq'data') {5165print$cgi->header(5166-type=>"text/plain", -charset =>"utf-8",5167-status=>"200 OK");5168local$| =1;# output autoflush5169printwhile<$fd>;5170close$fd5171or print"ERROR$!\n";51725173print'END';5174if(defined$t0&& gitweb_check_feature('timed')) {5175print' '.5176 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).5177' '.$number_of_git_cmds;5178}5179print"\n";51805181return;5182}51835184# page header5185 git_header_html();5186my$formats_nav=5187$cgi->a({-href => href(action=>"blob", -replay=>1)},5188"blob") .5189" | ";5190if($formateq'incremental') {5191$formats_nav.=5192$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5193"blame") ." (non-incremental)";5194}else{5195$formats_nav.=5196$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5197"blame") ." (incremental)";5198}5199$formats_nav.=5200" | ".5201$cgi->a({-href => href(action=>"history", -replay=>1)},5202"history") .5203" | ".5204$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5205"HEAD");5206 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5207 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5208 git_print_page_path($file_name,$ftype,$hash_base);52095210# page body5211if($formateq'incremental') {5212print"<noscript>\n<div class=\"error\"><center><b>\n".5213"This page requires JavaScript to run.\nUse ".5214$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5215'this page').5216" instead.\n".5217"</b></center></div>\n</noscript>\n";52185219print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5220}52215222print qq!<div class="page_body">\n!;5223print qq!<div id="progress_info">.../ ...</div>\n!5224if($formateq'incremental');5225print qq!<table id="blame_table"class="blame" width="100%">\n!.5226#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5227 qq!<thead>\n!.5228 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5229 qq!</thead>\n!.5230 qq!<tbody>\n!;52315232my@rev_color=qw(light dark);5233my$num_colors=scalar(@rev_color);5234my$current_color=0;52355236if($formateq'incremental') {5237my$color_class=$rev_color[$current_color];52385239#contents of a file5240my$linenr=0;5241 LINE:5242while(my$line= <$fd>) {5243chomp$line;5244$linenr++;52455246print qq!<tr id="l$linenr"class="$color_class">!.5247 qq!<td class="sha1"><a href=""> </a></td>!.5248 qq!<td class="linenr">!.5249 qq!<a class="linenr" href="">$linenr</a></td>!;5250print qq!<td class="pre">! . esc_html($line) ."</td>\n";5251print qq!</tr>\n!;5252}52535254}else{# porcelain, i.e. ordinary blame5255my%metainfo= ();# saves information about commits52565257# blame data5258 LINE:5259while(my$line= <$fd>) {5260chomp$line;5261# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5262# no <lines in group> for subsequent lines in group of lines5263my($full_rev,$orig_lineno,$lineno,$group_size) =5264($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5265if(!exists$metainfo{$full_rev}) {5266$metainfo{$full_rev} = {'nprevious'=>0};5267}5268my$meta=$metainfo{$full_rev};5269my$data;5270while($data= <$fd>) {5271chomp$data;5272last if($data=~s/^\t//);# contents of line5273if($data=~/^(\S+)(?: (.*))?$/) {5274$meta->{$1} =$2unlessexists$meta->{$1};5275}5276if($data=~/^previous /) {5277$meta->{'nprevious'}++;5278}5279}5280my$short_rev=substr($full_rev,0,8);5281my$author=$meta->{'author'};5282my%date=5283 parse_date($meta->{'author-time'},$meta->{'author-tz'});5284my$date=$date{'iso-tz'};5285if($group_size) {5286$current_color= ($current_color+1) %$num_colors;5287}5288my$tr_class=$rev_color[$current_color];5289$tr_class.=' boundary'if(exists$meta->{'boundary'});5290$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5291$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5292print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5293if($group_size) {5294print"<td class=\"sha1\"";5295print" title=\"". esc_html($author) .",$date\"";5296print" rowspan=\"$group_size\""if($group_size>1);5297print">";5298print$cgi->a({-href => href(action=>"commit",5299 hash=>$full_rev,5300 file_name=>$file_name)},5301 esc_html($short_rev));5302if($group_size>=2) {5303my@author_initials= ($author=~/\b([[:upper:]])\B/g);5304if(@author_initials) {5305print"<br />".5306 esc_html(join('',@author_initials));5307# or join('.', ...)5308}5309}5310print"</td>\n";5311}5312# 'previous' <sha1 of parent commit> <filename at commit>5313if(exists$meta->{'previous'} &&5314$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5315$meta->{'parent'} =$1;5316$meta->{'file_parent'} = unquote($2);5317}5318my$linenr_commit=5319exists($meta->{'parent'}) ?5320$meta->{'parent'} :$full_rev;5321my$linenr_filename=5322exists($meta->{'file_parent'}) ?5323$meta->{'file_parent'} : unquote($meta->{'filename'});5324my$blamed= href(action =>'blame',5325 file_name =>$linenr_filename,5326 hash_base =>$linenr_commit);5327print"<td class=\"linenr\">";5328print$cgi->a({ -href =>"$blamed#l$orig_lineno",5329-class=>"linenr"},5330 esc_html($lineno));5331print"</td>";5332print"<td class=\"pre\">". esc_html($data) ."</td>\n";5333print"</tr>\n";5334}# end while53355336}53375338# footer5339print"</tbody>\n".5340"</table>\n";# class="blame"5341print"</div>\n";# class="blame_body"5342close$fd5343or print"Reading blob failed\n";53445345 git_footer_html();5346}53475348sub git_blame {5349 git_blame_common();5350}53515352sub git_blame_incremental {5353 git_blame_common('incremental');5354}53555356sub git_blame_data {5357 git_blame_common('data');5358}53595360sub git_tags {5361my$head= git_get_head_hash($project);5362 git_header_html();5363 git_print_page_nav('','',$head,undef,$head);5364 git_print_header_div('summary',$project);53655366my@tagslist= git_get_tags_list();5367if(@tagslist) {5368 git_tags_body(\@tagslist);5369}5370 git_footer_html();5371}53725373sub git_heads {5374my$head= git_get_head_hash($project);5375 git_header_html();5376 git_print_page_nav('','',$head,undef,$head);5377 git_print_header_div('summary',$project);53785379my@headslist= git_get_heads_list();5380if(@headslist) {5381 git_heads_body(\@headslist,$head);5382}5383 git_footer_html();5384}53855386sub git_blob_plain {5387my$type=shift;5388my$expires;53895390if(!defined$hash) {5391if(defined$file_name) {5392my$base=$hash_base|| git_get_head_hash($project);5393$hash= git_get_hash_by_path($base,$file_name,"blob")5394or die_error(404,"Cannot find file");5395}else{5396 die_error(400,"No file name defined");5397}5398}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5399# blobs defined by non-textual hash id's can be cached5400$expires="+1d";5401}54025403open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5404or die_error(500,"Open git-cat-file blob '$hash' failed");54055406# content-type (can include charset)5407$type= blob_contenttype($fd,$file_name,$type);54085409# "save as" filename, even when no $file_name is given5410my$save_as="$hash";5411if(defined$file_name) {5412$save_as=$file_name;5413}elsif($type=~m/^text\//) {5414$save_as.='.txt';5415}54165417# With XSS prevention on, blobs of all types except a few known safe5418# ones are served with "Content-Disposition: attachment" to make sure5419# they don't run in our security domain. For certain image types,5420# blob view writes an <img> tag referring to blob_plain view, and we5421# want to be sure not to break that by serving the image as an5422# attachment (though Firefox 3 doesn't seem to care).5423my$sandbox=$prevent_xss&&5424$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;54255426print$cgi->header(5427-type =>$type,5428-expires =>$expires,5429-content_disposition =>5430($sandbox?'attachment':'inline')5431.'; filename="'.$save_as.'"');5432local$/=undef;5433binmode STDOUT,':raw';5434print<$fd>;5435binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5436close$fd;5437}54385439sub git_blob {5440my$expires;54415442if(!defined$hash) {5443if(defined$file_name) {5444my$base=$hash_base|| git_get_head_hash($project);5445$hash= git_get_hash_by_path($base,$file_name,"blob")5446or die_error(404,"Cannot find file");5447}else{5448 die_error(400,"No file name defined");5449}5450}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5451# blobs defined by non-textual hash id's can be cached5452$expires="+1d";5453}54545455my$have_blame= gitweb_check_feature('blame');5456open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5457or die_error(500,"Couldn't cat$file_name,$hash");5458my$mimetype= blob_mimetype($fd,$file_name);5459# use 'blob_plain' (aka 'raw') view for files that cannot be displayed5460if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5461close$fd;5462return git_blob_plain($mimetype);5463}5464# we can have blame only for text/* mimetype5465$have_blame&&= ($mimetype=~m!^text/!);54665467my$highlight= gitweb_check_feature('highlight');5468my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);5469$fd= run_highlighter($fd,$highlight,$syntax)5470if$syntax;54715472 git_header_html(undef,$expires);5473my$formats_nav='';5474if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5475if(defined$file_name) {5476if($have_blame) {5477$formats_nav.=5478$cgi->a({-href => href(action=>"blame", -replay=>1)},5479"blame") .5480" | ";5481}5482$formats_nav.=5483$cgi->a({-href => href(action=>"history", -replay=>1)},5484"history") .5485" | ".5486$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5487"raw") .5488" | ".5489$cgi->a({-href => href(action=>"blob",5490 hash_base=>"HEAD", file_name=>$file_name)},5491"HEAD");5492}else{5493$formats_nav.=5494$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5495"raw");5496}5497 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5498 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5499}else{5500print"<div class=\"page_nav\">\n".5501"<br/><br/></div>\n".5502"<div class=\"title\">$hash</div>\n";5503}5504 git_print_page_path($file_name,"blob",$hash_base);5505print"<div class=\"page_body\">\n";5506if($mimetype=~m!^image/!) {5507print qq!<img type="$mimetype"!;5508if($file_name) {5509print qq! alt="$file_name" title="$file_name"!;5510}5511print qq! src="! .5512 href(action=>"blob_plain", hash=>$hash,5513 hash_base=>$hash_base, file_name=>$file_name) .5514 qq!"/>\n!;5515}else{5516my$nr;5517while(my$line= <$fd>) {5518chomp$line;5519$nr++;5520$line= untabify($line);5521printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,5522$nr, href(-replay =>1),$nr,$nr,$syntax?$line: esc_html($line, -nbsp=>1);5523}5524}5525close$fd5526or print"Reading blob failed.\n";5527print"</div>";5528 git_footer_html();5529}55305531sub git_tree {5532if(!defined$hash_base) {5533$hash_base="HEAD";5534}5535if(!defined$hash) {5536if(defined$file_name) {5537$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5538}else{5539$hash=$hash_base;5540}5541}5542 die_error(404,"No such tree")unlessdefined($hash);55435544my$show_sizes= gitweb_check_feature('show-sizes');5545my$have_blame= gitweb_check_feature('blame');55465547my@entries= ();5548{5549local$/="\0";5550open my$fd,"-|", git_cmd(),"ls-tree",'-z',5551($show_sizes?'-l': ()),@extra_options,$hash5552or die_error(500,"Open git-ls-tree failed");5553@entries=map{chomp;$_} <$fd>;5554close$fd5555or die_error(404,"Reading tree failed");5556}55575558my$refs= git_get_references();5559my$ref= format_ref_marker($refs,$hash_base);5560 git_header_html();5561my$basedir='';5562if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5563my@views_nav= ();5564if(defined$file_name) {5565push@views_nav,5566$cgi->a({-href => href(action=>"history", -replay=>1)},5567"history"),5568$cgi->a({-href => href(action=>"tree",5569 hash_base=>"HEAD", file_name=>$file_name)},5570"HEAD"),5571}5572my$snapshot_links= format_snapshot_links($hash);5573if(defined$snapshot_links) {5574# FIXME: Should be available when we have no hash base as well.5575push@views_nav,$snapshot_links;5576}5577 git_print_page_nav('tree','',$hash_base,undef,undef,5578join(' | ',@views_nav));5579 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5580}else{5581undef$hash_base;5582print"<div class=\"page_nav\">\n";5583print"<br/><br/></div>\n";5584print"<div class=\"title\">$hash</div>\n";5585}5586if(defined$file_name) {5587$basedir=$file_name;5588if($basedirne''&&substr($basedir, -1)ne'/') {5589$basedir.='/';5590}5591 git_print_page_path($file_name,'tree',$hash_base);5592}5593print"<div class=\"page_body\">\n";5594print"<table class=\"tree\">\n";5595my$alternate=1;5596# '..' (top directory) link if possible5597if(defined$hash_base&&5598defined$file_name&&$file_name=~m![^/]+$!) {5599if($alternate) {5600print"<tr class=\"dark\">\n";5601}else{5602print"<tr class=\"light\">\n";5603}5604$alternate^=1;56055606my$up=$file_name;5607$up=~s!/?[^/]+$!!;5608undef$upunless$up;5609# based on git_print_tree_entry5610print'<td class="mode">'. mode_str('040000') ."</td>\n";5611print'<td class="size"> </td>'."\n"if$show_sizes;5612print'<td class="list">';5613print$cgi->a({-href => href(action=>"tree",5614 hash_base=>$hash_base,5615 file_name=>$up)},5616"..");5617print"</td>\n";5618print"<td class=\"link\"></td>\n";56195620print"</tr>\n";5621}5622foreachmy$line(@entries) {5623my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);56245625if($alternate) {5626print"<tr class=\"dark\">\n";5627}else{5628print"<tr class=\"light\">\n";5629}5630$alternate^=1;56315632 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);56335634print"</tr>\n";5635}5636print"</table>\n".5637"</div>";5638 git_footer_html();5639}56405641sub snapshot_name {5642my($project,$hash) =@_;56435644# path/to/project.git -> project5645# path/to/project/.git -> project5646my$name= to_utf8($project);5647$name=~ s,([^/])/*\.git$,$1,;5648$name= basename($name);5649# sanitize name5650$name=~s/[[:cntrl:]]/?/g;56515652my$ver=$hash;5653if($hash=~/^[0-9a-fA-F]+$/) {5654# shorten SHA-1 hash5655my$full_hash= git_get_full_hash($project,$hash);5656if($full_hash=~/^$hash/&&length($hash) >7) {5657$ver= git_get_short_hash($project,$hash);5658}5659}elsif($hash=~m!^refs/tags/(.*)$!) {5660# tags don't need shortened SHA-1 hash5661$ver=$1;5662}else{5663# branches and other need shortened SHA-1 hash5664if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {5665$ver=$1;5666}5667$ver.='-'. git_get_short_hash($project,$hash);5668}5669# in case of hierarchical branch names5670$ver=~s!/!.!g;56715672# name = project-version_string5673$name="$name-$ver";56745675returnwantarray? ($name,$name) :$name;5676}56775678sub git_snapshot {5679my$format=$input_params{'snapshot_format'};5680if(!@snapshot_fmts) {5681 die_error(403,"Snapshots not allowed");5682}5683# default to first supported snapshot format5684$format||=$snapshot_fmts[0];5685if($format!~m/^[a-z0-9]+$/) {5686 die_error(400,"Invalid snapshot format parameter");5687}elsif(!exists($known_snapshot_formats{$format})) {5688 die_error(400,"Unknown snapshot format");5689}elsif($known_snapshot_formats{$format}{'disabled'}) {5690 die_error(403,"Snapshot format not allowed");5691}elsif(!grep($_eq$format,@snapshot_fmts)) {5692 die_error(403,"Unsupported snapshot format");5693}56945695my$type= git_get_type("$hash^{}");5696if(!$type) {5697 die_error(404,'Object does not exist');5698}elsif($typeeq'blob') {5699 die_error(400,'Object is not a tree-ish');5700}57015702my($name,$prefix) = snapshot_name($project,$hash);5703my$filename="$name$known_snapshot_formats{$format}{'suffix'}";5704my$cmd= quote_command(5705 git_cmd(),'archive',5706"--format=$known_snapshot_formats{$format}{'format'}",5707"--prefix=$prefix/",$hash);5708if(exists$known_snapshot_formats{$format}{'compressor'}) {5709$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5710}57115712$filename=~s/(["\\])/\\$1/g;5713print$cgi->header(5714-type =>$known_snapshot_formats{$format}{'type'},5715-content_disposition =>'inline; filename="'.$filename.'"',5716-status =>'200 OK');57175718open my$fd,"-|",$cmd5719or die_error(500,"Execute git-archive failed");5720binmode STDOUT,':raw';5721print<$fd>;5722binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5723close$fd;5724}57255726sub git_log_generic {5727my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;57285729my$head= git_get_head_hash($project);5730if(!defined$base) {5731$base=$head;5732}5733if(!defined$page) {5734$page=0;5735}5736my$refs= git_get_references();57375738my$commit_hash=$base;5739if(defined$parent) {5740$commit_hash="$parent..$base";5741}5742my@commitlist=5743 parse_commits($commit_hash,101, (100*$page),5744defined$file_name? ($file_name,"--full-history") : ());57455746my$ftype;5747if(!defined$file_hash&&defined$file_name) {5748# some commits could have deleted file in question,5749# and not have it in tree, but one of them has to have it5750for(my$i=0;$i<@commitlist;$i++) {5751$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5752last ifdefined$file_hash;5753}5754}5755if(defined$file_hash) {5756$ftype= git_get_type($file_hash);5757}5758if(defined$file_name&& !defined$ftype) {5759 die_error(500,"Unknown type of object");5760}5761my%co;5762if(defined$file_name) {5763%co= parse_commit($base)5764or die_error(404,"Unknown commit object");5765}576657675768my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);5769my$next_link='';5770if($#commitlist>=100) {5771$next_link=5772$cgi->a({-href => href(-replay=>1, page=>$page+1),5773-accesskey =>"n", -title =>"Alt-n"},"next");5774}5775my$patch_max= gitweb_get_feature('patches');5776if($patch_max&& !defined$file_name) {5777if($patch_max<0||@commitlist<=$patch_max) {5778$paging_nav.=" ⋅ ".5779$cgi->a({-href => href(action=>"patches", -replay=>1)},5780"patches");5781}5782}57835784 git_header_html();5785 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);5786if(defined$file_name) {5787 git_print_header_div('commit', esc_html($co{'title'}),$base);5788}else{5789 git_print_header_div('summary',$project)5790}5791 git_print_page_path($file_name,$ftype,$hash_base)5792if(defined$file_name);57935794$body_subr->(\@commitlist,0,99,$refs,$next_link,5795$file_name,$file_hash,$ftype);57965797 git_footer_html();5798}57995800sub git_log {5801 git_log_generic('log', \&git_log_body,5802$hash,$hash_parent);5803}58045805sub git_commit {5806$hash||=$hash_base||"HEAD";5807my%co= parse_commit($hash)5808or die_error(404,"Unknown commit object");58095810my$parent=$co{'parent'};5811my$parents=$co{'parents'};# listref58125813# we need to prepare $formats_nav before any parameter munging5814my$formats_nav;5815if(!defined$parent) {5816# --root commitdiff5817$formats_nav.='(initial)';5818}elsif(@$parents==1) {5819# single parent commit5820$formats_nav.=5821'(parent: '.5822$cgi->a({-href => href(action=>"commit",5823 hash=>$parent)},5824 esc_html(substr($parent,0,7))) .5825')';5826}else{5827# merge commit5828$formats_nav.=5829'(merge: '.5830join(' ',map{5831$cgi->a({-href => href(action=>"commit",5832 hash=>$_)},5833 esc_html(substr($_,0,7)));5834}@$parents) .5835')';5836}5837if(gitweb_check_feature('patches') &&@$parents<=1) {5838$formats_nav.=" | ".5839$cgi->a({-href => href(action=>"patch", -replay=>1)},5840"patch");5841}58425843if(!defined$parent) {5844$parent="--root";5845}5846my@difftree;5847open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5848@diff_opts,5849(@$parents<=1?$parent:'-c'),5850$hash,"--"5851or die_error(500,"Open git-diff-tree failed");5852@difftree=map{chomp;$_} <$fd>;5853close$fdor die_error(404,"Reading git-diff-tree failed");58545855# non-textual hash id's can be cached5856my$expires;5857if($hash=~m/^[0-9a-fA-F]{40}$/) {5858$expires="+1d";5859}5860my$refs= git_get_references();5861my$ref= format_ref_marker($refs,$co{'id'});58625863 git_header_html(undef,$expires);5864 git_print_page_nav('commit','',5865$hash,$co{'tree'},$hash,5866$formats_nav);58675868if(defined$co{'parent'}) {5869 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5870}else{5871 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5872}5873print"<div class=\"title_text\">\n".5874"<table class=\"object_header\">\n";5875 git_print_authorship_rows(\%co);5876print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5877print"<tr>".5878"<td>tree</td>".5879"<td class=\"sha1\">".5880$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5881class=>"list"},$co{'tree'}) .5882"</td>".5883"<td class=\"link\">".5884$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5885"tree");5886my$snapshot_links= format_snapshot_links($hash);5887if(defined$snapshot_links) {5888print" | ".$snapshot_links;5889}5890print"</td>".5891"</tr>\n";58925893foreachmy$par(@$parents) {5894print"<tr>".5895"<td>parent</td>".5896"<td class=\"sha1\">".5897$cgi->a({-href => href(action=>"commit", hash=>$par),5898class=>"list"},$par) .5899"</td>".5900"<td class=\"link\">".5901$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5902" | ".5903$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5904"</td>".5905"</tr>\n";5906}5907print"</table>".5908"</div>\n";59095910print"<div class=\"page_body\">\n";5911 git_print_log($co{'comment'});5912print"</div>\n";59135914 git_difftree_body(\@difftree,$hash,@$parents);59155916 git_footer_html();5917}59185919sub git_object {5920# object is defined by:5921# - hash or hash_base alone5922# - hash_base and file_name5923my$type;59245925# - hash or hash_base alone5926if($hash|| ($hash_base&& !defined$file_name)) {5927my$object_id=$hash||$hash_base;59285929open my$fd,"-|", quote_command(5930 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5931or die_error(404,"Object does not exist");5932$type= <$fd>;5933chomp$type;5934close$fd5935or die_error(404,"Object does not exist");59365937# - hash_base and file_name5938}elsif($hash_base&&defined$file_name) {5939$file_name=~ s,/+$,,;59405941system(git_cmd(),"cat-file",'-e',$hash_base) ==05942or die_error(404,"Base object does not exist");59435944# here errors should not hapen5945open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5946or die_error(500,"Open git-ls-tree failed");5947my$line= <$fd>;5948close$fd;59495950#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5951unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5952 die_error(404,"File or directory for given base does not exist");5953}5954$type=$2;5955$hash=$3;5956}else{5957 die_error(400,"Not enough information to find object");5958}59595960print$cgi->redirect(-uri => href(action=>$type, -full=>1,5961 hash=>$hash, hash_base=>$hash_base,5962 file_name=>$file_name),5963-status =>'302 Found');5964}59655966sub git_blobdiff {5967my$format=shift||'html';59685969my$fd;5970my@difftree;5971my%diffinfo;5972my$expires;59735974# preparing $fd and %diffinfo for git_patchset_body5975# new style URI5976if(defined$hash_base&&defined$hash_parent_base) {5977if(defined$file_name) {5978# read raw output5979open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5980$hash_parent_base,$hash_base,5981"--", (defined$file_parent?$file_parent: ()),$file_name5982or die_error(500,"Open git-diff-tree failed");5983@difftree=map{chomp;$_} <$fd>;5984close$fd5985or die_error(404,"Reading git-diff-tree failed");5986@difftree5987or die_error(404,"Blob diff not found");59885989}elsif(defined$hash&&5990$hash=~/[0-9a-fA-F]{40}/) {5991# try to find filename from $hash59925993# read filtered raw output5994open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5995$hash_parent_base,$hash_base,"--"5996or die_error(500,"Open git-diff-tree failed");5997@difftree=5998# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5999# $hash == to_id6000grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}6001map{chomp;$_} <$fd>;6002close$fd6003or die_error(404,"Reading git-diff-tree failed");6004@difftree6005or die_error(404,"Blob diff not found");60066007}else{6008 die_error(400,"Missing one of the blob diff parameters");6009}60106011if(@difftree>1) {6012 die_error(400,"Ambiguous blob diff specification");6013}60146015%diffinfo= parse_difftree_raw_line($difftree[0]);6016$file_parent||=$diffinfo{'from_file'} ||$file_name;6017$file_name||=$diffinfo{'to_file'};60186019$hash_parent||=$diffinfo{'from_id'};6020$hash||=$diffinfo{'to_id'};60216022# non-textual hash id's can be cached6023if($hash_base=~m/^[0-9a-fA-F]{40}$/&&6024$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {6025$expires='+1d';6026}60276028# open patch output6029open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6030'-p', ($formateq'html'?"--full-index": ()),6031$hash_parent_base,$hash_base,6032"--", (defined$file_parent?$file_parent: ()),$file_name6033or die_error(500,"Open git-diff-tree failed");6034}60356036# old/legacy style URI -- not generated anymore since 1.4.3.6037if(!%diffinfo) {6038 die_error('404 Not Found',"Missing one of the blob diff parameters")6039}60406041# header6042if($formateq'html') {6043my$formats_nav=6044$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},6045"raw");6046 git_header_html(undef,$expires);6047if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6048 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6049 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6050}else{6051print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";6052print"<div class=\"title\">$hashvs$hash_parent</div>\n";6053}6054if(defined$file_name) {6055 git_print_page_path($file_name,"blob",$hash_base);6056}else{6057print"<div class=\"page_path\"></div>\n";6058}60596060}elsif($formateq'plain') {6061print$cgi->header(6062-type =>'text/plain',6063-charset =>'utf-8',6064-expires =>$expires,6065-content_disposition =>'inline; filename="'."$file_name".'.patch"');60666067print"X-Git-Url: ".$cgi->self_url() ."\n\n";60686069}else{6070 die_error(400,"Unknown blobdiff format");6071}60726073# patch6074if($formateq'html') {6075print"<div class=\"page_body\">\n";60766077 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);6078close$fd;60796080print"</div>\n";# class="page_body"6081 git_footer_html();60826083}else{6084while(my$line= <$fd>) {6085$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;6086$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;60876088print$line;60896090last if$line=~m!^\+\+\+!;6091}6092local$/=undef;6093print<$fd>;6094close$fd;6095}6096}60976098sub git_blobdiff_plain {6099 git_blobdiff('plain');6100}61016102sub git_commitdiff {6103my%params=@_;6104my$format=$params{-format} ||'html';61056106my($patch_max) = gitweb_get_feature('patches');6107if($formateq'patch') {6108 die_error(403,"Patch view not allowed")unless$patch_max;6109}61106111$hash||=$hash_base||"HEAD";6112my%co= parse_commit($hash)6113or die_error(404,"Unknown commit object");61146115# choose format for commitdiff for merge6116if(!defined$hash_parent&& @{$co{'parents'}} >1) {6117$hash_parent='--cc';6118}6119# we need to prepare $formats_nav before almost any parameter munging6120my$formats_nav;6121if($formateq'html') {6122$formats_nav=6123$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6124"raw");6125if($patch_max&& @{$co{'parents'}} <=1) {6126$formats_nav.=" | ".6127$cgi->a({-href => href(action=>"patch", -replay=>1)},6128"patch");6129}61306131if(defined$hash_parent&&6132$hash_parentne'-c'&&$hash_parentne'--cc') {6133# commitdiff with two commits given6134my$hash_parent_short=$hash_parent;6135if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6136$hash_parent_short=substr($hash_parent,0,7);6137}6138$formats_nav.=6139' (from';6140for(my$i=0;$i< @{$co{'parents'}};$i++) {6141if($co{'parents'}[$i]eq$hash_parent) {6142$formats_nav.=' parent '. ($i+1);6143last;6144}6145}6146$formats_nav.=': '.6147$cgi->a({-href => href(action=>"commitdiff",6148 hash=>$hash_parent)},6149 esc_html($hash_parent_short)) .6150')';6151}elsif(!$co{'parent'}) {6152# --root commitdiff6153$formats_nav.=' (initial)';6154}elsif(scalar@{$co{'parents'}} ==1) {6155# single parent commit6156$formats_nav.=6157' (parent: '.6158$cgi->a({-href => href(action=>"commitdiff",6159 hash=>$co{'parent'})},6160 esc_html(substr($co{'parent'},0,7))) .6161')';6162}else{6163# merge commit6164if($hash_parenteq'--cc') {6165$formats_nav.=' | '.6166$cgi->a({-href => href(action=>"commitdiff",6167 hash=>$hash, hash_parent=>'-c')},6168'combined');6169}else{# $hash_parent eq '-c'6170$formats_nav.=' | '.6171$cgi->a({-href => href(action=>"commitdiff",6172 hash=>$hash, hash_parent=>'--cc')},6173'compact');6174}6175$formats_nav.=6176' (merge: '.6177join(' ',map{6178$cgi->a({-href => href(action=>"commitdiff",6179 hash=>$_)},6180 esc_html(substr($_,0,7)));6181} @{$co{'parents'}} ) .6182')';6183}6184}61856186my$hash_parent_param=$hash_parent;6187if(!defined$hash_parent_param) {6188# --cc for multiple parents, --root for parentless6189$hash_parent_param=6190@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6191}61926193# read commitdiff6194my$fd;6195my@difftree;6196if($formateq'html') {6197open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6198"--no-commit-id","--patch-with-raw","--full-index",6199$hash_parent_param,$hash,"--"6200or die_error(500,"Open git-diff-tree failed");62016202while(my$line= <$fd>) {6203chomp$line;6204# empty line ends raw part of diff-tree output6205last unless$line;6206push@difftree,scalar parse_difftree_raw_line($line);6207}62086209}elsif($formateq'plain') {6210open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6211'-p',$hash_parent_param,$hash,"--"6212or die_error(500,"Open git-diff-tree failed");6213}elsif($formateq'patch') {6214# For commit ranges, we limit the output to the number of6215# patches specified in the 'patches' feature.6216# For single commits, we limit the output to a single patch,6217# diverging from the git-format-patch default.6218my@commit_spec= ();6219if($hash_parent) {6220if($patch_max>0) {6221push@commit_spec,"-$patch_max";6222}6223push@commit_spec,'-n',"$hash_parent..$hash";6224}else{6225if($params{-single}) {6226push@commit_spec,'-1';6227}else{6228if($patch_max>0) {6229push@commit_spec,"-$patch_max";6230}6231push@commit_spec,"-n";6232}6233push@commit_spec,'--root',$hash;6234}6235open$fd,"-|", git_cmd(),"format-patch",@diff_opts,6236'--encoding=utf8','--stdout',@commit_spec6237or die_error(500,"Open git-format-patch failed");6238}else{6239 die_error(400,"Unknown commitdiff format");6240}62416242# non-textual hash id's can be cached6243my$expires;6244if($hash=~m/^[0-9a-fA-F]{40}$/) {6245$expires="+1d";6246}62476248# write commit message6249if($formateq'html') {6250my$refs= git_get_references();6251my$ref= format_ref_marker($refs,$co{'id'});62526253 git_header_html(undef,$expires);6254 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6255 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6256print"<div class=\"title_text\">\n".6257"<table class=\"object_header\">\n";6258 git_print_authorship_rows(\%co);6259print"</table>".6260"</div>\n";6261print"<div class=\"page_body\">\n";6262if(@{$co{'comment'}} >1) {6263print"<div class=\"log\">\n";6264 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6265print"</div>\n";# class="log"6266}62676268}elsif($formateq'plain') {6269my$refs= git_get_references("tags");6270my$tagname= git_get_rev_name_tags($hash);6271my$filename= basename($project) ."-$hash.patch";62726273print$cgi->header(6274-type =>'text/plain',6275-charset =>'utf-8',6276-expires =>$expires,6277-content_disposition =>'inline; filename="'."$filename".'"');6278my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6279print"From: ". to_utf8($co{'author'}) ."\n";6280print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6281print"Subject: ". to_utf8($co{'title'}) ."\n";62826283print"X-Git-Tag:$tagname\n"if$tagname;6284print"X-Git-Url: ".$cgi->self_url() ."\n\n";62856286foreachmy$line(@{$co{'comment'}}) {6287print to_utf8($line) ."\n";6288}6289print"---\n\n";6290}elsif($formateq'patch') {6291my$filename= basename($project) ."-$hash.patch";62926293print$cgi->header(6294-type =>'text/plain',6295-charset =>'utf-8',6296-expires =>$expires,6297-content_disposition =>'inline; filename="'."$filename".'"');6298}62996300# write patch6301if($formateq'html') {6302my$use_parents= !defined$hash_parent||6303$hash_parenteq'-c'||$hash_parenteq'--cc';6304 git_difftree_body(\@difftree,$hash,6305$use_parents? @{$co{'parents'}} :$hash_parent);6306print"<br/>\n";63076308 git_patchset_body($fd, \@difftree,$hash,6309$use_parents? @{$co{'parents'}} :$hash_parent);6310close$fd;6311print"</div>\n";# class="page_body"6312 git_footer_html();63136314}elsif($formateq'plain') {6315local$/=undef;6316print<$fd>;6317close$fd6318or print"Reading git-diff-tree failed\n";6319}elsif($formateq'patch') {6320local$/=undef;6321print<$fd>;6322close$fd6323or print"Reading git-format-patch failed\n";6324}6325}63266327sub git_commitdiff_plain {6328 git_commitdiff(-format =>'plain');6329}63306331# format-patch-style patches6332sub git_patch {6333 git_commitdiff(-format =>'patch', -single =>1);6334}63356336sub git_patches {6337 git_commitdiff(-format =>'patch');6338}63396340sub git_history {6341 git_log_generic('history', \&git_history_body,6342$hash_base,$hash_parent_base,6343$file_name,$hash);6344}63456346sub git_search {6347 gitweb_check_feature('search')or die_error(403,"Search is disabled");6348if(!defined$searchtext) {6349 die_error(400,"Text field is empty");6350}6351if(!defined$hash) {6352$hash= git_get_head_hash($project);6353}6354my%co= parse_commit($hash);6355if(!%co) {6356 die_error(404,"Unknown commit object");6357}6358if(!defined$page) {6359$page=0;6360}63616362$searchtype||='commit';6363if($searchtypeeq'pickaxe') {6364# pickaxe may take all resources of your box and run for several minutes6365# with every query - so decide by yourself how public you make this feature6366 gitweb_check_feature('pickaxe')6367or die_error(403,"Pickaxe is disabled");6368}6369if($searchtypeeq'grep') {6370 gitweb_check_feature('grep')6371or die_error(403,"Grep is disabled");6372}63736374 git_header_html();63756376if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6377my$greptype;6378if($searchtypeeq'commit') {6379$greptype="--grep=";6380}elsif($searchtypeeq'author') {6381$greptype="--author=";6382}elsif($searchtypeeq'committer') {6383$greptype="--committer=";6384}6385$greptype.=$searchtext;6386my@commitlist= parse_commits($hash,101, (100*$page),undef,6387$greptype,'--regexp-ignore-case',6388$search_use_regexp?'--extended-regexp':'--fixed-strings');63896390my$paging_nav='';6391if($page>0) {6392$paging_nav.=6393$cgi->a({-href => href(action=>"search", hash=>$hash,6394 searchtext=>$searchtext,6395 searchtype=>$searchtype)},6396"first");6397$paging_nav.=" ⋅ ".6398$cgi->a({-href => href(-replay=>1, page=>$page-1),6399-accesskey =>"p", -title =>"Alt-p"},"prev");6400}else{6401$paging_nav.="first";6402$paging_nav.=" ⋅ prev";6403}6404my$next_link='';6405if($#commitlist>=100) {6406$next_link=6407$cgi->a({-href => href(-replay=>1, page=>$page+1),6408-accesskey =>"n", -title =>"Alt-n"},"next");6409$paging_nav.=" ⋅$next_link";6410}else{6411$paging_nav.=" ⋅ next";6412}64136414if($#commitlist>=100) {6415}64166417 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6418 git_print_header_div('commit', esc_html($co{'title'}),$hash);6419 git_search_grep_body(\@commitlist,0,99,$next_link);6420}64216422if($searchtypeeq'pickaxe') {6423 git_print_page_nav('','',$hash,$co{'tree'},$hash);6424 git_print_header_div('commit', esc_html($co{'title'}),$hash);64256426print"<table class=\"pickaxe search\">\n";6427my$alternate=1;6428local$/="\n";6429open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6430'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6431($search_use_regexp?'--pickaxe-regex': ());6432undef%co;6433my@files;6434while(my$line= <$fd>) {6435chomp$line;6436next unless$line;64376438my%set= parse_difftree_raw_line($line);6439if(defined$set{'commit'}) {6440# finish previous commit6441if(%co) {6442print"</td>\n".6443"<td class=\"link\">".6444$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6445" | ".6446$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6447print"</td>\n".6448"</tr>\n";6449}64506451if($alternate) {6452print"<tr class=\"dark\">\n";6453}else{6454print"<tr class=\"light\">\n";6455}6456$alternate^=1;6457%co= parse_commit($set{'commit'});6458my$author= chop_and_escape_str($co{'author_name'},15,5);6459print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6460"<td><i>$author</i></td>\n".6461"<td>".6462$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6463-class=>"list subject"},6464 chop_and_escape_str($co{'title'},50) ."<br/>");6465}elsif(defined$set{'to_id'}) {6466next if($set{'to_id'} =~m/^0{40}$/);64676468print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6469 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6470-class=>"list"},6471"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6472"<br/>\n";6473}6474}6475close$fd;64766477# finish last commit (warning: repetition!)6478if(%co) {6479print"</td>\n".6480"<td class=\"link\">".6481$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6482" | ".6483$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6484print"</td>\n".6485"</tr>\n";6486}64876488print"</table>\n";6489}64906491if($searchtypeeq'grep') {6492 git_print_page_nav('','',$hash,$co{'tree'},$hash);6493 git_print_header_div('commit', esc_html($co{'title'}),$hash);64946495print"<table class=\"grep_search\">\n";6496my$alternate=1;6497my$matches=0;6498local$/="\n";6499open my$fd,"-|", git_cmd(),'grep','-n',6500$search_use_regexp? ('-E','-i') :'-F',6501$searchtext,$co{'tree'};6502my$lastfile='';6503while(my$line= <$fd>) {6504chomp$line;6505my($file,$lno,$ltext,$binary);6506last if($matches++>1000);6507if($line=~/^Binary file (.+) matches$/) {6508$file=$1;6509$binary=1;6510}else{6511(undef,$file,$lno,$ltext) =split(/:/,$line,4);6512}6513if($filene$lastfile) {6514$lastfileand print"</td></tr>\n";6515if($alternate++) {6516print"<tr class=\"dark\">\n";6517}else{6518print"<tr class=\"light\">\n";6519}6520print"<td class=\"list\">".6521$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6522 file_name=>"$file"),6523-class=>"list"}, esc_path($file));6524print"</td><td>\n";6525$lastfile=$file;6526}6527if($binary) {6528print"<div class=\"binary\">Binary file</div>\n";6529}else{6530$ltext= untabify($ltext);6531if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6532$ltext= esc_html($1, -nbsp=>1);6533$ltext.='<span class="match">';6534$ltext.= esc_html($2, -nbsp=>1);6535$ltext.='</span>';6536$ltext.= esc_html($3, -nbsp=>1);6537}else{6538$ltext= esc_html($ltext, -nbsp=>1);6539}6540print"<div class=\"pre\">".6541$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6542 file_name=>"$file").'#l'.$lno,6543-class=>"linenr"},sprintf('%4i',$lno))6544.' '.$ltext."</div>\n";6545}6546}6547if($lastfile) {6548print"</td></tr>\n";6549if($matches>1000) {6550print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6551}6552}else{6553print"<div class=\"diff nodifferences\">No matches found</div>\n";6554}6555close$fd;65566557print"</table>\n";6558}6559 git_footer_html();6560}65616562sub git_search_help {6563 git_header_html();6564 git_print_page_nav('','',$hash,$hash,$hash);6565print<<EOT;6566<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6567regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6568the pattern entered is recognized as the POSIX extended6569<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6570insensitive).</p>6571<dl>6572<dt><b>commit</b></dt>6573<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6574EOT6575my$have_grep= gitweb_check_feature('grep');6576if($have_grep) {6577print<<EOT;6578<dt><b>grep</b></dt>6579<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6580 a different one) are searched for the given pattern. On large trees, this search can take6581a while and put some strain on the server, so please use it with some consideration. Note that6582due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6583case-sensitive.</dd>6584EOT6585}6586print<<EOT;6587<dt><b>author</b></dt>6588<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6589<dt><b>committer</b></dt>6590<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6591EOT6592my$have_pickaxe= gitweb_check_feature('pickaxe');6593if($have_pickaxe) {6594print<<EOT;6595<dt><b>pickaxe</b></dt>6596<dd>All commits that caused the string to appear or disappear from any file (changes that6597added, removed or "modified" the string) will be listed. This search can take a while and6598takes a lot of strain on the server, so please use it wisely. Note that since you may be6599interested even in changes just changing the case as well, this search is case sensitive.</dd>6600EOT6601}6602print"</dl>\n";6603 git_footer_html();6604}66056606sub git_shortlog {6607 git_log_generic('shortlog', \&git_shortlog_body,6608$hash,$hash_parent);6609}66106611## ......................................................................6612## feeds (RSS, Atom; OPML)66136614sub git_feed {6615my$format=shift||'atom';6616my$have_blame= gitweb_check_feature('blame');66176618# Atom: http://www.atomenabled.org/developers/syndication/6619# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6620if($formatne'rss'&&$formatne'atom') {6621 die_error(400,"Unknown web feed format");6622}66236624# log/feed of current (HEAD) branch, log of given branch, history of file/directory6625my$head=$hash||'HEAD';6626my@commitlist= parse_commits($head,150,0,$file_name);66276628my%latest_commit;6629my%latest_date;6630my$content_type="application/$format+xml";6631if(defined$cgi->http('HTTP_ACCEPT') &&6632$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6633# browser (feed reader) prefers text/xml6634$content_type='text/xml';6635}6636if(defined($commitlist[0])) {6637%latest_commit= %{$commitlist[0]};6638my$latest_epoch=$latest_commit{'committer_epoch'};6639%latest_date= parse_date($latest_epoch);6640my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6641if(defined$if_modified) {6642my$since;6643if(eval{require HTTP::Date;1; }) {6644$since= HTTP::Date::str2time($if_modified);6645}elsif(eval{require Time::ParseDate;1; }) {6646$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6647}6648if(defined$since&&$latest_epoch<=$since) {6649print$cgi->header(6650-type =>$content_type,6651-charset =>'utf-8',6652-last_modified =>$latest_date{'rfc2822'},6653-status =>'304 Not Modified');6654return;6655}6656}6657print$cgi->header(6658-type =>$content_type,6659-charset =>'utf-8',6660-last_modified =>$latest_date{'rfc2822'});6661}else{6662print$cgi->header(6663-type =>$content_type,6664-charset =>'utf-8');6665}66666667# Optimization: skip generating the body if client asks only6668# for Last-Modified date.6669return if($cgi->request_method()eq'HEAD');66706671# header variables6672my$title="$site_name-$project/$action";6673my$feed_type='log';6674if(defined$hash) {6675$title.=" - '$hash'";6676$feed_type='branch log';6677if(defined$file_name) {6678$title.=" ::$file_name";6679$feed_type='history';6680}6681}elsif(defined$file_name) {6682$title.=" -$file_name";6683$feed_type='history';6684}6685$title.="$feed_type";6686my$descr= git_get_project_description($project);6687if(defined$descr) {6688$descr= esc_html($descr);6689}else{6690$descr="$project".6691($formateq'rss'?'RSS':'Atom') .6692" feed";6693}6694my$owner= git_get_project_owner($project);6695$owner= esc_html($owner);66966697#header6698my$alt_url;6699if(defined$file_name) {6700$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6701}elsif(defined$hash) {6702$alt_url= href(-full=>1, action=>"log", hash=>$hash);6703}else{6704$alt_url= href(-full=>1, action=>"summary");6705}6706print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6707if($formateq'rss') {6708print<<XML;6709<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6710<channel>6711XML6712print"<title>$title</title>\n".6713"<link>$alt_url</link>\n".6714"<description>$descr</description>\n".6715"<language>en</language>\n".6716# project owner is responsible for 'editorial' content6717"<managingEditor>$owner</managingEditor>\n";6718if(defined$logo||defined$favicon) {6719# prefer the logo to the favicon, since RSS6720# doesn't allow both6721my$img= esc_url($logo||$favicon);6722print"<image>\n".6723"<url>$img</url>\n".6724"<title>$title</title>\n".6725"<link>$alt_url</link>\n".6726"</image>\n";6727}6728if(%latest_date) {6729print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6730print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6731}6732print"<generator>gitweb v.$version/$git_version</generator>\n";6733}elsif($formateq'atom') {6734print<<XML;6735<feed xmlns="http://www.w3.org/2005/Atom">6736XML6737print"<title>$title</title>\n".6738"<subtitle>$descr</subtitle>\n".6739'<link rel="alternate" type="text/html" href="'.6740$alt_url.'" />'."\n".6741'<link rel="self" type="'.$content_type.'" href="'.6742$cgi->self_url() .'" />'."\n".6743"<id>". href(-full=>1) ."</id>\n".6744# use project owner for feed author6745"<author><name>$owner</name></author>\n";6746if(defined$favicon) {6747print"<icon>". esc_url($favicon) ."</icon>\n";6748}6749if(defined$logo_url) {6750# not twice as wide as tall: 72 x 27 pixels6751print"<logo>". esc_url($logo) ."</logo>\n";6752}6753if(!%latest_date) {6754# dummy date to keep the feed valid until commits trickle in:6755print"<updated>1970-01-01T00:00:00Z</updated>\n";6756}else{6757print"<updated>$latest_date{'iso-8601'}</updated>\n";6758}6759print"<generator version='$version/$git_version'>gitweb</generator>\n";6760}67616762# contents6763for(my$i=0;$i<=$#commitlist;$i++) {6764my%co= %{$commitlist[$i]};6765my$commit=$co{'id'};6766# we read 150, we always show 30 and the ones more recent than 48 hours6767if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6768last;6769}6770my%cd= parse_date($co{'author_epoch'});67716772# get list of changed files6773open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6774$co{'parent'} ||"--root",6775$co{'id'},"--", (defined$file_name?$file_name: ())6776ornext;6777my@difftree=map{chomp;$_} <$fd>;6778close$fd6779ornext;67806781# print element (entry, item)6782my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6783if($formateq'rss') {6784print"<item>\n".6785"<title>". esc_html($co{'title'}) ."</title>\n".6786"<author>". esc_html($co{'author'}) ."</author>\n".6787"<pubDate>$cd{'rfc2822'}</pubDate>\n".6788"<guid isPermaLink=\"true\">$co_url</guid>\n".6789"<link>$co_url</link>\n".6790"<description>". esc_html($co{'title'}) ."</description>\n".6791"<content:encoded>".6792"<![CDATA[\n";6793}elsif($formateq'atom') {6794print"<entry>\n".6795"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6796"<updated>$cd{'iso-8601'}</updated>\n".6797"<author>\n".6798" <name>". esc_html($co{'author_name'}) ."</name>\n";6799if($co{'author_email'}) {6800print" <email>". esc_html($co{'author_email'}) ."</email>\n";6801}6802print"</author>\n".6803# use committer for contributor6804"<contributor>\n".6805" <name>". esc_html($co{'committer_name'}) ."</name>\n";6806if($co{'committer_email'}) {6807print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6808}6809print"</contributor>\n".6810"<published>$cd{'iso-8601'}</published>\n".6811"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6812"<id>$co_url</id>\n".6813"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6814"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6815}6816my$comment=$co{'comment'};6817print"<pre>\n";6818foreachmy$line(@$comment) {6819$line= esc_html($line);6820print"$line\n";6821}6822print"</pre><ul>\n";6823foreachmy$difftree_line(@difftree) {6824my%difftree= parse_difftree_raw_line($difftree_line);6825next if!$difftree{'from_id'};68266827my$file=$difftree{'file'} ||$difftree{'to_file'};68286829print"<li>".6830"[".6831$cgi->a({-href => href(-full=>1, action=>"blobdiff",6832 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6833 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6834 file_name=>$file, file_parent=>$difftree{'from_file'}),6835-title =>"diff"},'D');6836if($have_blame) {6837print$cgi->a({-href => href(-full=>1, action=>"blame",6838 file_name=>$file, hash_base=>$commit),6839-title =>"blame"},'B');6840}6841# if this is not a feed of a file history6842if(!defined$file_name||$file_namene$file) {6843print$cgi->a({-href => href(-full=>1, action=>"history",6844 file_name=>$file, hash=>$commit),6845-title =>"history"},'H');6846}6847$file= esc_path($file);6848print"] ".6849"$file</li>\n";6850}6851if($formateq'rss') {6852print"</ul>]]>\n".6853"</content:encoded>\n".6854"</item>\n";6855}elsif($formateq'atom') {6856print"</ul>\n</div>\n".6857"</content>\n".6858"</entry>\n";6859}6860}68616862# end of feed6863if($formateq'rss') {6864print"</channel>\n</rss>\n";6865}elsif($formateq'atom') {6866print"</feed>\n";6867}6868}68696870sub git_rss {6871 git_feed('rss');6872}68736874sub git_atom {6875 git_feed('atom');6876}68776878sub git_opml {6879my@list= git_get_projects_list();68806881print$cgi->header(6882-type =>'text/xml',6883-charset =>'utf-8',6884-content_disposition =>'inline; filename="opml.xml"');68856886print<<XML;6887<?xml version="1.0" encoding="utf-8"?>6888<opml version="1.0">6889<head>6890 <title>$site_nameOPML Export</title>6891</head>6892<body>6893<outline text="git RSS feeds">6894XML68956896foreachmy$pr(@list) {6897my%proj=%$pr;6898my$head= git_get_head_hash($proj{'path'});6899if(!defined$head) {6900next;6901}6902$git_dir="$projectroot/$proj{'path'}";6903my%co= parse_commit($head);6904if(!%co) {6905next;6906}69076908my$path= esc_html(chop_str($proj{'path'},25,5));6909my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6910my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6911print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6912}6913print<<XML;6914</outline>6915</body>6916</opml>6917XML6918}