1#!/usr/bin/perl 2 3# gitweb - simple web interface to track changes in git repositories 4# 5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org> 6# (C) 2005, Christian Gierke 7# 8# This program is licensed under the GPLv2 9 10use strict; 11use warnings; 12use CGI qw(:standard :escapeHTML -nosticky); 13use CGI::Util qw(unescape); 14use CGI::Carp qw(fatalsToBrowser); 15use Encode; 16use Fcntl ':mode'; 17use File::Find qw(); 18use File::Basename qw(basename); 19binmode STDOUT,':utf8'; 20 21our$t0; 22if(eval{require Time::HiRes;1; }) { 23$t0= [Time::HiRes::gettimeofday()]; 24} 25our$number_of_git_cmds=0; 26 27BEGIN{ 28 CGI->compile()if$ENV{'MOD_PERL'}; 29} 30 31our$cgi= new CGI; 32our$version="++GIT_VERSION++"; 33our$my_url=$cgi->url(); 34our$my_uri=$cgi->url(-absolute =>1); 35 36# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 37# needed and used only for URLs with nonempty PATH_INFO 38our$base_url=$my_url; 39 40# When the script is used as DirectoryIndex, the URL does not contain the name 41# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 42# have to do it ourselves. We make $path_info global because it's also used 43# later on. 44# 45# Another issue with the script being the DirectoryIndex is that the resulting 46# $my_url data is not the full script URL: this is good, because we want 47# generated links to keep implying the script name if it wasn't explicitly 48# indicated in the URL we're handling, but it means that $my_url cannot be used 49# as base URL. 50# Therefore, if we needed to strip PATH_INFO, then we know that we have 51# to build the base URL ourselves: 52our$path_info=$ENV{"PATH_INFO"}; 53if($path_info) { 54if($my_url=~ s,\Q$path_info\E$,, && 55$my_uri=~ s,\Q$path_info\E$,, && 56defined$ENV{'SCRIPT_NAME'}) { 57$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 58} 59} 60 61# core git executable to use 62# this can just be "git" if your webserver has a sensible PATH 63our$GIT="++GIT_BINDIR++/git"; 64 65# absolute fs-path which will be prepended to the project path 66#our $projectroot = "/pub/scm"; 67our$projectroot="++GITWEB_PROJECTROOT++"; 68 69# fs traversing limit for getting project list 70# the number is relative to the projectroot 71our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 72 73# target of the home link on top of all pages 74our$home_link=$my_uri||"/"; 75 76# string of the home link on top of all pages 77our$home_link_str="++GITWEB_HOME_LINK_STR++"; 78 79# name of your site or organization to appear in page titles 80# replace this with something more descriptive for clearer bookmarks 81our$site_name="++GITWEB_SITENAME++" 82|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 83 84# filename of html text to include at top of each page 85our$site_header="++GITWEB_SITE_HEADER++"; 86# html text to include at home page 87our$home_text="++GITWEB_HOMETEXT++"; 88# filename of html text to include at bottom of each page 89our$site_footer="++GITWEB_SITE_FOOTER++"; 90 91# URI of stylesheets 92our@stylesheets= ("++GITWEB_CSS++"); 93# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 94our$stylesheet=undef; 95# URI of GIT logo (72x27 size) 96our$logo="++GITWEB_LOGO++"; 97# URI of GIT favicon, assumed to be image/png type 98our$favicon="++GITWEB_FAVICON++"; 99 100# URI and label (title) of GIT logo link 101#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 102#our $logo_label = "git documentation"; 103our$logo_url="http://git-scm.com/"; 104our$logo_label="git homepage"; 105 106# source of projects list 107our$projects_list="++GITWEB_LIST++"; 108 109# the width (in characters) of the projects list "Description" column 110our$projects_list_description_width=25; 111 112# default order of projects list 113# valid values are none, project, descr, owner, and age 114our$default_projects_order="project"; 115 116# show repository only if this file exists 117# (only effective if this variable evaluates to true) 118our$export_ok="++GITWEB_EXPORT_OK++"; 119 120# show repository only if this subroutine returns true 121# when given the path to the project, for example: 122# sub { return -e "$_[0]/git-daemon-export-ok"; } 123our$export_auth_hook=undef; 124 125# only allow viewing of repositories also shown on the overview page 126our$strict_export="++GITWEB_STRICT_EXPORT++"; 127 128# list of git base URLs used for URL to where fetch project from, 129# i.e. full URL is "$git_base_url/$project" 130our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 131 132# default blob_plain mimetype and default charset for text/plain blob 133our$default_blob_plain_mimetype='text/plain'; 134our$default_text_plain_charset=undef; 135 136# file to use for guessing MIME types before trying /etc/mime.types 137# (relative to the current git repository) 138our$mimetypes_file=undef; 139 140# assume this charset if line contains non-UTF-8 characters; 141# it should be valid encoding (see Encoding::Supported(3pm) for list), 142# for which encoding all byte sequences are valid, for example 143# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 144# could be even 'utf-8' for the old behavior) 145our$fallback_encoding='latin1'; 146 147# rename detection options for git-diff and git-diff-tree 148# - default is '-M', with the cost proportional to 149# (number of removed files) * (number of new files). 150# - more costly is '-C' (which implies '-M'), with the cost proportional to 151# (number of changed files + number of removed files) * (number of new files) 152# - even more costly is '-C', '--find-copies-harder' with cost 153# (number of files in the original tree) * (number of new files) 154# - one might want to include '-B' option, e.g. '-B', '-M' 155our@diff_opts= ('-M');# taken from git_commit 156 157# Disables features that would allow repository owners to inject script into 158# the gitweb domain. 159our$prevent_xss=0; 160 161# information about snapshot formats that gitweb is capable of serving 162our%known_snapshot_formats= ( 163# name => { 164# 'display' => display name, 165# 'type' => mime type, 166# 'suffix' => filename suffix, 167# 'format' => --format for git-archive, 168# 'compressor' => [compressor command and arguments] 169# (array reference, optional)} 170# 171'tgz'=> { 172'display'=>'tar.gz', 173'type'=>'application/x-gzip', 174'suffix'=>'.tar.gz', 175'format'=>'tar', 176'compressor'=> ['gzip']}, 177 178'tbz2'=> { 179'display'=>'tar.bz2', 180'type'=>'application/x-bzip2', 181'suffix'=>'.tar.bz2', 182'format'=>'tar', 183'compressor'=> ['bzip2']}, 184 185'zip'=> { 186'display'=>'zip', 187'type'=>'application/x-zip', 188'suffix'=>'.zip', 189'format'=>'zip'}, 190); 191 192# Aliases so we understand old gitweb.snapshot values in repository 193# configuration. 194our%known_snapshot_format_aliases= ( 195'gzip'=>'tgz', 196'bzip2'=>'tbz2', 197 198# backward compatibility: legacy gitweb config support 199'x-gzip'=>undef,'gz'=>undef, 200'x-bzip2'=>undef,'bz2'=>undef, 201'x-zip'=>undef,''=>undef, 202); 203 204# Pixel sizes for icons and avatars. If the default font sizes or lineheights 205# are changed, it may be appropriate to change these values too via 206# $GITWEB_CONFIG. 207our%avatar_size= ( 208'default'=>16, 209'double'=>32 210); 211 212# You define site-wide feature defaults here; override them with 213# $GITWEB_CONFIG as necessary. 214our%feature= ( 215# feature => { 216# 'sub' => feature-sub (subroutine), 217# 'override' => allow-override (boolean), 218# 'default' => [ default options...] (array reference)} 219# 220# if feature is overridable (it means that allow-override has true value), 221# then feature-sub will be called with default options as parameters; 222# return value of feature-sub indicates if to enable specified feature 223# 224# if there is no 'sub' key (no feature-sub), then feature cannot be 225# overriden 226# 227# use gitweb_get_feature(<feature>) to retrieve the <feature> value 228# (an array) or gitweb_check_feature(<feature>) to check if <feature> 229# is enabled 230 231# Enable the 'blame' blob view, showing the last commit that modified 232# each line in the file. This can be very CPU-intensive. 233 234# To enable system wide have in $GITWEB_CONFIG 235# $feature{'blame'}{'default'} = [1]; 236# To have project specific config enable override in $GITWEB_CONFIG 237# $feature{'blame'}{'override'} = 1; 238# and in project config gitweb.blame = 0|1; 239'blame'=> { 240'sub'=>sub{ feature_bool('blame',@_) }, 241'override'=>0, 242'default'=> [0]}, 243 244# Enable the 'snapshot' link, providing a compressed archive of any 245# tree. This can potentially generate high traffic if you have large 246# project. 247 248# Value is a list of formats defined in %known_snapshot_formats that 249# you wish to offer. 250# To disable system wide have in $GITWEB_CONFIG 251# $feature{'snapshot'}{'default'} = []; 252# To have project specific config enable override in $GITWEB_CONFIG 253# $feature{'snapshot'}{'override'} = 1; 254# and in project config, a comma-separated list of formats or "none" 255# to disable. Example: gitweb.snapshot = tbz2,zip; 256'snapshot'=> { 257'sub'=> \&feature_snapshot, 258'override'=>0, 259'default'=> ['tgz']}, 260 261# Enable text search, which will list the commits which match author, 262# committer or commit text to a given string. Enabled by default. 263# Project specific override is not supported. 264'search'=> { 265'override'=>0, 266'default'=> [1]}, 267 268# Enable grep search, which will list the files in currently selected 269# tree containing the given string. Enabled by default. This can be 270# potentially CPU-intensive, of course. 271 272# To enable system wide have in $GITWEB_CONFIG 273# $feature{'grep'}{'default'} = [1]; 274# To have project specific config enable override in $GITWEB_CONFIG 275# $feature{'grep'}{'override'} = 1; 276# and in project config gitweb.grep = 0|1; 277'grep'=> { 278'sub'=>sub{ feature_bool('grep',@_) }, 279'override'=>0, 280'default'=> [1]}, 281 282# Enable the pickaxe search, which will list the commits that modified 283# a given string in a file. This can be practical and quite faster 284# alternative to 'blame', but still potentially CPU-intensive. 285 286# To enable system wide have in $GITWEB_CONFIG 287# $feature{'pickaxe'}{'default'} = [1]; 288# To have project specific config enable override in $GITWEB_CONFIG 289# $feature{'pickaxe'}{'override'} = 1; 290# and in project config gitweb.pickaxe = 0|1; 291'pickaxe'=> { 292'sub'=>sub{ feature_bool('pickaxe',@_) }, 293'override'=>0, 294'default'=> [1]}, 295 296# Make gitweb use an alternative format of the URLs which can be 297# more readable and natural-looking: project name is embedded 298# directly in the path and the query string contains other 299# auxiliary information. All gitweb installations recognize 300# URL in either format; this configures in which formats gitweb 301# generates links. 302 303# To enable system wide have in $GITWEB_CONFIG 304# $feature{'pathinfo'}{'default'} = [1]; 305# Project specific override is not supported. 306 307# Note that you will need to change the default location of CSS, 308# favicon, logo and possibly other files to an absolute URL. Also, 309# if gitweb.cgi serves as your indexfile, you will need to force 310# $my_uri to contain the script name in your $GITWEB_CONFIG. 311'pathinfo'=> { 312'override'=>0, 313'default'=> [0]}, 314 315# Make gitweb consider projects in project root subdirectories 316# to be forks of existing projects. Given project $projname.git, 317# projects matching $projname/*.git will not be shown in the main 318# projects list, instead a '+' mark will be added to $projname 319# there and a 'forks' view will be enabled for the project, listing 320# all the forks. If project list is taken from a file, forks have 321# to be listed after the main project. 322 323# To enable system wide have in $GITWEB_CONFIG 324# $feature{'forks'}{'default'} = [1]; 325# Project specific override is not supported. 326'forks'=> { 327'override'=>0, 328'default'=> [0]}, 329 330# Insert custom links to the action bar of all project pages. 331# This enables you mainly to link to third-party scripts integrating 332# into gitweb; e.g. git-browser for graphical history representation 333# or custom web-based repository administration interface. 334 335# The 'default' value consists of a list of triplets in the form 336# (label, link, position) where position is the label after which 337# to insert the link and link is a format string where %n expands 338# to the project name, %f to the project path within the filesystem, 339# %h to the current hash (h gitweb parameter) and %b to the current 340# hash base (hb gitweb parameter); %% expands to %. 341 342# To enable system wide have in $GITWEB_CONFIG e.g. 343# $feature{'actions'}{'default'} = [('graphiclog', 344# '/git-browser/by-commit.html?r=%n', 'summary')]; 345# Project specific override is not supported. 346'actions'=> { 347'override'=>0, 348'default'=> []}, 349 350# Allow gitweb scan project content tags described in ctags/ 351# of project repository, and display the popular Web 2.0-ish 352# "tag cloud" near the project list. Note that this is something 353# COMPLETELY different from the normal Git tags. 354 355# gitweb by itself can show existing tags, but it does not handle 356# tagging itself; you need an external application for that. 357# For an example script, check Girocco's cgi/tagproj.cgi. 358# You may want to install the HTML::TagCloud Perl module to get 359# a pretty tag cloud instead of just a list of tags. 360 361# To enable system wide have in $GITWEB_CONFIG 362# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 363# Project specific override is not supported. 364'ctags'=> { 365'override'=>0, 366'default'=> [0]}, 367 368# The maximum number of patches in a patchset generated in patch 369# view. Set this to 0 or undef to disable patch view, or to a 370# negative number to remove any limit. 371 372# To disable system wide have in $GITWEB_CONFIG 373# $feature{'patches'}{'default'} = [0]; 374# To have project specific config enable override in $GITWEB_CONFIG 375# $feature{'patches'}{'override'} = 1; 376# and in project config gitweb.patches = 0|n; 377# where n is the maximum number of patches allowed in a patchset. 378'patches'=> { 379'sub'=> \&feature_patches, 380'override'=>0, 381'default'=> [16]}, 382 383# Avatar support. When this feature is enabled, views such as 384# shortlog or commit will display an avatar associated with 385# the email of the committer(s) and/or author(s). 386 387# Currently available providers are gravatar and picon. 388# If an unknown provider is specified, the feature is disabled. 389 390# Gravatar depends on Digest::MD5. 391# Picon currently relies on the indiana.edu database. 392 393# To enable system wide have in $GITWEB_CONFIG 394# $feature{'avatar'}{'default'} = ['<provider>']; 395# where <provider> is either gravatar or picon. 396# To have project specific config enable override in $GITWEB_CONFIG 397# $feature{'avatar'}{'override'} = 1; 398# and in project config gitweb.avatar = <provider>; 399'avatar'=> { 400'sub'=> \&feature_avatar, 401'override'=>0, 402'default'=> ['']}, 403 404# Enable displaying how much time and how many git commands 405# it took to generate and display page. Disabled by default. 406# Project specific override is not supported. 407'timed'=> { 408'override'=>0, 409'default'=> [0]}, 410); 411 412sub gitweb_get_feature { 413my($name) =@_; 414return unlessexists$feature{$name}; 415my($sub,$override,@defaults) = ( 416$feature{$name}{'sub'}, 417$feature{$name}{'override'}, 418@{$feature{$name}{'default'}}); 419if(!$override) {return@defaults; } 420if(!defined$sub) { 421warn"feature$nameis not overrideable"; 422return@defaults; 423} 424return$sub->(@defaults); 425} 426 427# A wrapper to check if a given feature is enabled. 428# With this, you can say 429# 430# my $bool_feat = gitweb_check_feature('bool_feat'); 431# gitweb_check_feature('bool_feat') or somecode; 432# 433# instead of 434# 435# my ($bool_feat) = gitweb_get_feature('bool_feat'); 436# (gitweb_get_feature('bool_feat'))[0] or somecode; 437# 438sub gitweb_check_feature { 439return(gitweb_get_feature(@_))[0]; 440} 441 442 443sub feature_bool { 444my$key=shift; 445my($val) = git_get_project_config($key,'--bool'); 446 447if(!defined$val) { 448return($_[0]); 449}elsif($valeq'true') { 450return(1); 451}elsif($valeq'false') { 452return(0); 453} 454} 455 456sub feature_snapshot { 457my(@fmts) =@_; 458 459my($val) = git_get_project_config('snapshot'); 460 461if($val) { 462@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 463} 464 465return@fmts; 466} 467 468sub feature_patches { 469my@val= (git_get_project_config('patches','--int')); 470 471if(@val) { 472return@val; 473} 474 475return($_[0]); 476} 477 478sub feature_avatar { 479my@val= (git_get_project_config('avatar')); 480 481return@val?@val:@_; 482} 483 484# checking HEAD file with -e is fragile if the repository was 485# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 486# and then pruned. 487sub check_head_link { 488my($dir) =@_; 489my$headfile="$dir/HEAD"; 490return((-e $headfile) || 491(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 492} 493 494sub check_export_ok { 495my($dir) =@_; 496return(check_head_link($dir) && 497(!$export_ok|| -e "$dir/$export_ok") && 498(!$export_auth_hook||$export_auth_hook->($dir))); 499} 500 501# process alternate names for backward compatibility 502# filter out unsupported (unknown) snapshot formats 503sub filter_snapshot_fmts { 504my@fmts=@_; 505 506@fmts=map{ 507exists$known_snapshot_format_aliases{$_} ? 508$known_snapshot_format_aliases{$_} :$_}@fmts; 509@fmts=grep{ 510exists$known_snapshot_formats{$_} }@fmts; 511} 512 513our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 514if(-e $GITWEB_CONFIG) { 515do$GITWEB_CONFIG; 516}else{ 517our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 518do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 519} 520 521# version of the core git binary 522our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 523$number_of_git_cmds++; 524 525$projects_list||=$projectroot; 526 527# ====================================================================== 528# input validation and dispatch 529 530# input parameters can be collected from a variety of sources (presently, CGI 531# and PATH_INFO), so we define an %input_params hash that collects them all 532# together during validation: this allows subsequent uses (e.g. href()) to be 533# agnostic of the parameter origin 534 535our%input_params= (); 536 537# input parameters are stored with the long parameter name as key. This will 538# also be used in the href subroutine to convert parameters to their CGI 539# equivalent, and since the href() usage is the most frequent one, we store 540# the name -> CGI key mapping here, instead of the reverse. 541# 542# XXX: Warning: If you touch this, check the search form for updating, 543# too. 544 545our@cgi_param_mapping= ( 546 project =>"p", 547 action =>"a", 548 file_name =>"f", 549 file_parent =>"fp", 550 hash =>"h", 551 hash_parent =>"hp", 552 hash_base =>"hb", 553 hash_parent_base =>"hpb", 554 page =>"pg", 555 order =>"o", 556 searchtext =>"s", 557 searchtype =>"st", 558 snapshot_format =>"sf", 559 extra_options =>"opt", 560 search_use_regexp =>"sr", 561); 562our%cgi_param_mapping=@cgi_param_mapping; 563 564# we will also need to know the possible actions, for validation 565our%actions= ( 566"blame"=> \&git_blame, 567"blobdiff"=> \&git_blobdiff, 568"blobdiff_plain"=> \&git_blobdiff_plain, 569"blob"=> \&git_blob, 570"blob_plain"=> \&git_blob_plain, 571"commitdiff"=> \&git_commitdiff, 572"commitdiff_plain"=> \&git_commitdiff_plain, 573"commit"=> \&git_commit, 574"forks"=> \&git_forks, 575"heads"=> \&git_heads, 576"history"=> \&git_history, 577"log"=> \&git_log, 578"patch"=> \&git_patch, 579"patches"=> \&git_patches, 580"rss"=> \&git_rss, 581"atom"=> \&git_atom, 582"search"=> \&git_search, 583"search_help"=> \&git_search_help, 584"shortlog"=> \&git_shortlog, 585"summary"=> \&git_summary, 586"tag"=> \&git_tag, 587"tags"=> \&git_tags, 588"tree"=> \&git_tree, 589"snapshot"=> \&git_snapshot, 590"object"=> \&git_object, 591# those below don't need $project 592"opml"=> \&git_opml, 593"project_list"=> \&git_project_list, 594"project_index"=> \&git_project_index, 595); 596 597# finally, we have the hash of allowed extra_options for the commands that 598# allow them 599our%allowed_options= ( 600"--no-merges"=> [qw(rss atom log shortlog history)], 601); 602 603# fill %input_params with the CGI parameters. All values except for 'opt' 604# should be single values, but opt can be an array. We should probably 605# build an array of parameters that can be multi-valued, but since for the time 606# being it's only this one, we just single it out 607while(my($name,$symbol) =each%cgi_param_mapping) { 608if($symboleq'opt') { 609$input_params{$name} = [$cgi->param($symbol) ]; 610}else{ 611$input_params{$name} =$cgi->param($symbol); 612} 613} 614 615# now read PATH_INFO and update the parameter list for missing parameters 616sub evaluate_path_info { 617return ifdefined$input_params{'project'}; 618return if!$path_info; 619$path_info=~ s,^/+,,; 620return if!$path_info; 621 622# find which part of PATH_INFO is project 623my$project=$path_info; 624$project=~ s,/+$,,; 625while($project&& !check_head_link("$projectroot/$project")) { 626$project=~ s,/*[^/]*$,,; 627} 628return unless$project; 629$input_params{'project'} =$project; 630 631# do not change any parameters if an action is given using the query string 632return if$input_params{'action'}; 633$path_info=~ s,^\Q$project\E/*,,; 634 635# next, check if we have an action 636my$action=$path_info; 637$action=~ s,/.*$,,; 638if(exists$actions{$action}) { 639$path_info=~ s,^$action/*,,; 640$input_params{'action'} =$action; 641} 642 643# list of actions that want hash_base instead of hash, but can have no 644# pathname (f) parameter 645my@wants_base= ( 646'tree', 647'history', 648); 649 650# we want to catch 651# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 652my($parentrefname,$parentpathname,$refname,$pathname) = 653($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 654 655# first, analyze the 'current' part 656if(defined$pathname) { 657# we got "branch:filename" or "branch:dir/" 658# we could use git_get_type(branch:pathname), but: 659# - it needs $git_dir 660# - it does a git() call 661# - the convention of terminating directories with a slash 662# makes it superfluous 663# - embedding the action in the PATH_INFO would make it even 664# more superfluous 665$pathname=~ s,^/+,,; 666if(!$pathname||substr($pathname, -1)eq"/") { 667$input_params{'action'} ||="tree"; 668$pathname=~ s,/$,,; 669}else{ 670# the default action depends on whether we had parent info 671# or not 672if($parentrefname) { 673$input_params{'action'} ||="blobdiff_plain"; 674}else{ 675$input_params{'action'} ||="blob_plain"; 676} 677} 678$input_params{'hash_base'} ||=$refname; 679$input_params{'file_name'} ||=$pathname; 680}elsif(defined$refname) { 681# we got "branch". In this case we have to choose if we have to 682# set hash or hash_base. 683# 684# Most of the actions without a pathname only want hash to be 685# set, except for the ones specified in @wants_base that want 686# hash_base instead. It should also be noted that hand-crafted 687# links having 'history' as an action and no pathname or hash 688# set will fail, but that happens regardless of PATH_INFO. 689$input_params{'action'} ||="shortlog"; 690if(grep{$_eq$input_params{'action'} }@wants_base) { 691$input_params{'hash_base'} ||=$refname; 692}else{ 693$input_params{'hash'} ||=$refname; 694} 695} 696 697# next, handle the 'parent' part, if present 698if(defined$parentrefname) { 699# a missing pathspec defaults to the 'current' filename, allowing e.g. 700# someproject/blobdiff/oldrev..newrev:/filename 701if($parentpathname) { 702$parentpathname=~ s,^/+,,; 703$parentpathname=~ s,/$,,; 704$input_params{'file_parent'} ||=$parentpathname; 705}else{ 706$input_params{'file_parent'} ||=$input_params{'file_name'}; 707} 708# we assume that hash_parent_base is wanted if a path was specified, 709# or if the action wants hash_base instead of hash 710if(defined$input_params{'file_parent'} || 711grep{$_eq$input_params{'action'} }@wants_base) { 712$input_params{'hash_parent_base'} ||=$parentrefname; 713}else{ 714$input_params{'hash_parent'} ||=$parentrefname; 715} 716} 717 718# for the snapshot action, we allow URLs in the form 719# $project/snapshot/$hash.ext 720# where .ext determines the snapshot and gets removed from the 721# passed $refname to provide the $hash. 722# 723# To be able to tell that $refname includes the format extension, we 724# require the following two conditions to be satisfied: 725# - the hash input parameter MUST have been set from the $refname part 726# of the URL (i.e. they must be equal) 727# - the snapshot format MUST NOT have been defined already (e.g. from 728# CGI parameter sf) 729# It's also useless to try any matching unless $refname has a dot, 730# so we check for that too 731if(defined$input_params{'action'} && 732$input_params{'action'}eq'snapshot'&& 733defined$refname&&index($refname,'.') != -1&& 734$refnameeq$input_params{'hash'} && 735!defined$input_params{'snapshot_format'}) { 736# We loop over the known snapshot formats, checking for 737# extensions. Allowed extensions are both the defined suffix 738# (which includes the initial dot already) and the snapshot 739# format key itself, with a prepended dot 740while(my($fmt,$opt) =each%known_snapshot_formats) { 741my$hash=$refname; 742unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 743next; 744} 745my$sfx=$1; 746# a valid suffix was found, so set the snapshot format 747# and reset the hash parameter 748$input_params{'snapshot_format'} =$fmt; 749$input_params{'hash'} =$hash; 750# we also set the format suffix to the one requested 751# in the URL: this way a request for e.g. .tgz returns 752# a .tgz instead of a .tar.gz 753$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 754last; 755} 756} 757} 758evaluate_path_info(); 759 760our$action=$input_params{'action'}; 761if(defined$action) { 762if(!validate_action($action)) { 763 die_error(400,"Invalid action parameter"); 764} 765} 766 767# parameters which are pathnames 768our$project=$input_params{'project'}; 769if(defined$project) { 770if(!validate_project($project)) { 771undef$project; 772 die_error(404,"No such project"); 773} 774} 775 776our$file_name=$input_params{'file_name'}; 777if(defined$file_name) { 778if(!validate_pathname($file_name)) { 779 die_error(400,"Invalid file parameter"); 780} 781} 782 783our$file_parent=$input_params{'file_parent'}; 784if(defined$file_parent) { 785if(!validate_pathname($file_parent)) { 786 die_error(400,"Invalid file parent parameter"); 787} 788} 789 790# parameters which are refnames 791our$hash=$input_params{'hash'}; 792if(defined$hash) { 793if(!validate_refname($hash)) { 794 die_error(400,"Invalid hash parameter"); 795} 796} 797 798our$hash_parent=$input_params{'hash_parent'}; 799if(defined$hash_parent) { 800if(!validate_refname($hash_parent)) { 801 die_error(400,"Invalid hash parent parameter"); 802} 803} 804 805our$hash_base=$input_params{'hash_base'}; 806if(defined$hash_base) { 807if(!validate_refname($hash_base)) { 808 die_error(400,"Invalid hash base parameter"); 809} 810} 811 812our@extra_options= @{$input_params{'extra_options'}}; 813# @extra_options is always defined, since it can only be (currently) set from 814# CGI, and $cgi->param() returns the empty array in array context if the param 815# is not set 816foreachmy$opt(@extra_options) { 817if(not exists$allowed_options{$opt}) { 818 die_error(400,"Invalid option parameter"); 819} 820if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 821 die_error(400,"Invalid option parameter for this action"); 822} 823} 824 825our$hash_parent_base=$input_params{'hash_parent_base'}; 826if(defined$hash_parent_base) { 827if(!validate_refname($hash_parent_base)) { 828 die_error(400,"Invalid hash parent base parameter"); 829} 830} 831 832# other parameters 833our$page=$input_params{'page'}; 834if(defined$page) { 835if($page=~m/[^0-9]/) { 836 die_error(400,"Invalid page parameter"); 837} 838} 839 840our$searchtype=$input_params{'searchtype'}; 841if(defined$searchtype) { 842if($searchtype=~m/[^a-z]/) { 843 die_error(400,"Invalid searchtype parameter"); 844} 845} 846 847our$search_use_regexp=$input_params{'search_use_regexp'}; 848 849our$searchtext=$input_params{'searchtext'}; 850our$search_regexp; 851if(defined$searchtext) { 852if(length($searchtext) <2) { 853 die_error(403,"At least two characters are required for search parameter"); 854} 855$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 856} 857 858# path to the current git repository 859our$git_dir; 860$git_dir="$projectroot/$project"if$project; 861 862# list of supported snapshot formats 863our@snapshot_fmts= gitweb_get_feature('snapshot'); 864@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 865 866# check that the avatar feature is set to a known provider name, 867# and for each provider check if the dependencies are satisfied. 868# if the provider name is invalid or the dependencies are not met, 869# reset $git_avatar to the empty string. 870our($git_avatar) = gitweb_get_feature('avatar'); 871if($git_avatareq'gravatar') { 872$git_avatar=''unless(eval{require Digest::MD5;1; }); 873}elsif($git_avatareq'picon') { 874# no dependencies 875}else{ 876$git_avatar=''; 877} 878 879# dispatch 880if(!defined$action) { 881if(defined$hash) { 882$action= git_get_type($hash); 883}elsif(defined$hash_base&&defined$file_name) { 884$action= git_get_type("$hash_base:$file_name"); 885}elsif(defined$project) { 886$action='summary'; 887}else{ 888$action='project_list'; 889} 890} 891if(!defined($actions{$action})) { 892 die_error(400,"Unknown action"); 893} 894if($action!~m/^(?:opml|project_list|project_index)$/&& 895!$project) { 896 die_error(400,"Project needed"); 897} 898$actions{$action}->(); 899exit; 900 901## ====================================================================== 902## action links 903 904sub href { 905my%params=@_; 906# default is to use -absolute url() i.e. $my_uri 907my$href=$params{-full} ?$my_url:$my_uri; 908 909$params{'project'} =$projectunlessexists$params{'project'}; 910 911if($params{-replay}) { 912while(my($name,$symbol) =each%cgi_param_mapping) { 913if(!exists$params{$name}) { 914$params{$name} =$input_params{$name}; 915} 916} 917} 918 919my$use_pathinfo= gitweb_check_feature('pathinfo'); 920if($use_pathinfoand defined$params{'project'}) { 921# try to put as many parameters as possible in PATH_INFO: 922# - project name 923# - action 924# - hash_parent or hash_parent_base:/file_parent 925# - hash or hash_base:/filename 926# - the snapshot_format as an appropriate suffix 927 928# When the script is the root DirectoryIndex for the domain, 929# $href here would be something like http://gitweb.example.com/ 930# Thus, we strip any trailing / from $href, to spare us double 931# slashes in the final URL 932$href=~ s,/$,,; 933 934# Then add the project name, if present 935$href.="/".esc_url($params{'project'}); 936delete$params{'project'}; 937 938# since we destructively absorb parameters, we keep this 939# boolean that remembers if we're handling a snapshot 940my$is_snapshot=$params{'action'}eq'snapshot'; 941 942# Summary just uses the project path URL, any other action is 943# added to the URL 944if(defined$params{'action'}) { 945$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 946delete$params{'action'}; 947} 948 949# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 950# stripping nonexistent or useless pieces 951$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 952||$params{'hash_parent'} ||$params{'hash'}); 953if(defined$params{'hash_base'}) { 954if(defined$params{'hash_parent_base'}) { 955$href.= esc_url($params{'hash_parent_base'}); 956# skip the file_parent if it's the same as the file_name 957delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 958if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 959$href.=":/".esc_url($params{'file_parent'}); 960delete$params{'file_parent'}; 961} 962$href.=".."; 963delete$params{'hash_parent'}; 964delete$params{'hash_parent_base'}; 965}elsif(defined$params{'hash_parent'}) { 966$href.= esc_url($params{'hash_parent'}).".."; 967delete$params{'hash_parent'}; 968} 969 970$href.= esc_url($params{'hash_base'}); 971if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 972$href.=":/".esc_url($params{'file_name'}); 973delete$params{'file_name'}; 974} 975delete$params{'hash'}; 976delete$params{'hash_base'}; 977}elsif(defined$params{'hash'}) { 978$href.= esc_url($params{'hash'}); 979delete$params{'hash'}; 980} 981 982# If the action was a snapshot, we can absorb the 983# snapshot_format parameter too 984if($is_snapshot) { 985my$fmt=$params{'snapshot_format'}; 986# snapshot_format should always be defined when href() 987# is called, but just in case some code forgets, we 988# fall back to the default 989$fmt||=$snapshot_fmts[0]; 990$href.=$known_snapshot_formats{$fmt}{'suffix'}; 991delete$params{'snapshot_format'}; 992} 993} 994 995# now encode the parameters explicitly 996my@result= (); 997for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 998my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 999if(defined$params{$name}) {1000if(ref($params{$name})eq"ARRAY") {1001foreachmy$par(@{$params{$name}}) {1002push@result,$symbol."=". esc_param($par);1003}1004}else{1005push@result,$symbol."=". esc_param($params{$name});1006}1007}1008}1009$href.="?".join(';',@result)ifscalar@result;10101011return$href;1012}101310141015## ======================================================================1016## validation, quoting/unquoting and escaping10171018sub validate_action {1019my$input=shift||returnundef;1020returnundefunlessexists$actions{$input};1021return$input;1022}10231024sub validate_project {1025my$input=shift||returnundef;1026if(!validate_pathname($input) ||1027!(-d "$projectroot/$input") ||1028!check_export_ok("$projectroot/$input") ||1029($strict_export&& !project_in_list($input))) {1030returnundef;1031}else{1032return$input;1033}1034}10351036sub validate_pathname {1037my$input=shift||returnundef;10381039# no '.' or '..' as elements of path, i.e. no '.' nor '..'1040# at the beginning, at the end, and between slashes.1041# also this catches doubled slashes1042if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1043returnundef;1044}1045# no null characters1046if($input=~m!\0!) {1047returnundef;1048}1049return$input;1050}10511052sub validate_refname {1053my$input=shift||returnundef;10541055# textual hashes are O.K.1056if($input=~m/^[0-9a-fA-F]{40}$/) {1057return$input;1058}1059# it must be correct pathname1060$input= validate_pathname($input)1061orreturnundef;1062# restrictions on ref name according to git-check-ref-format1063if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1064returnundef;1065}1066return$input;1067}10681069# decode sequences of octets in utf8 into Perl's internal form,1070# which is utf-8 with utf8 flag set if needed. gitweb writes out1071# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1072sub to_utf8 {1073my$str=shift;1074if(utf8::valid($str)) {1075 utf8::decode($str);1076return$str;1077}else{1078return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1079}1080}10811082# quote unsafe chars, but keep the slash, even when it's not1083# correct, but quoted slashes look too horrible in bookmarks1084sub esc_param {1085my$str=shift;1086$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg;1087$str=~s/\+/%2B/g;1088$str=~s/ /\+/g;1089return$str;1090}10911092# quote unsafe chars in whole URL, so some charactrs cannot be quoted1093sub esc_url {1094my$str=shift;1095$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1096$str=~s/\+/%2B/g;1097$str=~s/ /\+/g;1098return$str;1099}11001101# replace invalid utf8 character with SUBSTITUTION sequence1102sub esc_html {1103my$str=shift;1104my%opts=@_;11051106$str= to_utf8($str);1107$str=$cgi->escapeHTML($str);1108if($opts{'-nbsp'}) {1109$str=~s/ / /g;1110}1111$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1112return$str;1113}11141115# quote control characters and escape filename to HTML1116sub esc_path {1117my$str=shift;1118my%opts=@_;11191120$str= to_utf8($str);1121$str=$cgi->escapeHTML($str);1122if($opts{'-nbsp'}) {1123$str=~s/ / /g;1124}1125$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1126return$str;1127}11281129# Make control characters "printable", using character escape codes (CEC)1130sub quot_cec {1131my$cntrl=shift;1132my%opts=@_;1133my%es= (# character escape codes, aka escape sequences1134"\t"=>'\t',# tab (HT)1135"\n"=>'\n',# line feed (LF)1136"\r"=>'\r',# carrige return (CR)1137"\f"=>'\f',# form feed (FF)1138"\b"=>'\b',# backspace (BS)1139"\a"=>'\a',# alarm (bell) (BEL)1140"\e"=>'\e',# escape (ESC)1141"\013"=>'\v',# vertical tab (VT)1142"\000"=>'\0',# nul character (NUL)1143);1144my$chr= ( (exists$es{$cntrl})1145?$es{$cntrl}1146:sprintf('\%2x',ord($cntrl)) );1147if($opts{-nohtml}) {1148return$chr;1149}else{1150return"<span class=\"cntrl\">$chr</span>";1151}1152}11531154# Alternatively use unicode control pictures codepoints,1155# Unicode "printable representation" (PR)1156sub quot_upr {1157my$cntrl=shift;1158my%opts=@_;11591160my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1161if($opts{-nohtml}) {1162return$chr;1163}else{1164return"<span class=\"cntrl\">$chr</span>";1165}1166}11671168# git may return quoted and escaped filenames1169sub unquote {1170my$str=shift;11711172sub unq {1173my$seq=shift;1174my%es= (# character escape codes, aka escape sequences1175't'=>"\t",# tab (HT, TAB)1176'n'=>"\n",# newline (NL)1177'r'=>"\r",# return (CR)1178'f'=>"\f",# form feed (FF)1179'b'=>"\b",# backspace (BS)1180'a'=>"\a",# alarm (bell) (BEL)1181'e'=>"\e",# escape (ESC)1182'v'=>"\013",# vertical tab (VT)1183);11841185if($seq=~m/^[0-7]{1,3}$/) {1186# octal char sequence1187returnchr(oct($seq));1188}elsif(exists$es{$seq}) {1189# C escape sequence, aka character escape code1190return$es{$seq};1191}1192# quoted ordinary character1193return$seq;1194}11951196if($str=~m/^"(.*)"$/) {1197# needs unquoting1198$str=$1;1199$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1200}1201return$str;1202}12031204# escape tabs (convert tabs to spaces)1205sub untabify {1206my$line=shift;12071208while((my$pos=index($line,"\t")) != -1) {1209if(my$count= (8- ($pos%8))) {1210my$spaces=' ' x $count;1211$line=~s/\t/$spaces/;1212}1213}12141215return$line;1216}12171218sub project_in_list {1219my$project=shift;1220my@list= git_get_projects_list();1221return@list&&scalar(grep{$_->{'path'}eq$project}@list);1222}12231224## ----------------------------------------------------------------------1225## HTML aware string manipulation12261227# Try to chop given string on a word boundary between position1228# $len and $len+$add_len. If there is no word boundary there,1229# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1230# (marking chopped part) would be longer than given string.1231sub chop_str {1232my$str=shift;1233my$len=shift;1234my$add_len=shift||10;1235my$where=shift||'right';# 'left' | 'center' | 'right'12361237# Make sure perl knows it is utf8 encoded so we don't1238# cut in the middle of a utf8 multibyte char.1239$str= to_utf8($str);12401241# allow only $len chars, but don't cut a word if it would fit in $add_len1242# if it doesn't fit, cut it if it's still longer than the dots we would add1243# remove chopped character entities entirely12441245# when chopping in the middle, distribute $len into left and right part1246# return early if chopping wouldn't make string shorter1247if($whereeq'center') {1248return$strif($len+5>=length($str));# filler is length 51249$len=int($len/2);1250}else{1251return$strif($len+4>=length($str));# filler is length 41252}12531254# regexps: ending and beginning with word part up to $add_len1255my$endre=qr/.{$len}\w{0,$add_len}/;1256my$begre=qr/\w{0,$add_len}.{$len}/;12571258if($whereeq'left') {1259$str=~m/^(.*?)($begre)$/;1260my($lead,$body) = ($1,$2);1261if(length($lead) >4) {1262$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1263$lead=" ...";1264}1265return"$lead$body";12661267}elsif($whereeq'center') {1268$str=~m/^($endre)(.*)$/;1269my($left,$str) = ($1,$2);1270$str=~m/^(.*?)($begre)$/;1271my($mid,$right) = ($1,$2);1272if(length($mid) >5) {1273$left=~s/&[^;]*$//;1274$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1275$mid=" ... ";1276}1277return"$left$mid$right";12781279}else{1280$str=~m/^($endre)(.*)$/;1281my$body=$1;1282my$tail=$2;1283if(length($tail) >4) {1284$body=~s/&[^;]*$//;1285$tail="... ";1286}1287return"$body$tail";1288}1289}12901291# takes the same arguments as chop_str, but also wraps a <span> around the1292# result with a title attribute if it does get chopped. Additionally, the1293# string is HTML-escaped.1294sub chop_and_escape_str {1295my($str) =@_;12961297my$chopped= chop_str(@_);1298if($choppedeq$str) {1299return esc_html($chopped);1300}else{1301$str=~s/[[:cntrl:]]/?/g;1302return$cgi->span({-title=>$str}, esc_html($chopped));1303}1304}13051306## ----------------------------------------------------------------------1307## functions returning short strings13081309# CSS class for given age value (in seconds)1310sub age_class {1311my$age=shift;13121313if(!defined$age) {1314return"noage";1315}elsif($age<60*60*2) {1316return"age0";1317}elsif($age<60*60*24*2) {1318return"age1";1319}else{1320return"age2";1321}1322}13231324# convert age in seconds to "nn units ago" string1325sub age_string {1326my$age=shift;1327my$age_str;13281329if($age>60*60*24*365*2) {1330$age_str= (int$age/60/60/24/365);1331$age_str.=" years ago";1332}elsif($age>60*60*24*(365/12)*2) {1333$age_str=int$age/60/60/24/(365/12);1334$age_str.=" months ago";1335}elsif($age>60*60*24*7*2) {1336$age_str=int$age/60/60/24/7;1337$age_str.=" weeks ago";1338}elsif($age>60*60*24*2) {1339$age_str=int$age/60/60/24;1340$age_str.=" days ago";1341}elsif($age>60*60*2) {1342$age_str=int$age/60/60;1343$age_str.=" hours ago";1344}elsif($age>60*2) {1345$age_str=int$age/60;1346$age_str.=" min ago";1347}elsif($age>2) {1348$age_str=int$age;1349$age_str.=" sec ago";1350}else{1351$age_str.=" right now";1352}1353return$age_str;1354}13551356useconstant{1357 S_IFINVALID =>0030000,1358 S_IFGITLINK =>0160000,1359};13601361# submodule/subproject, a commit object reference1362sub S_ISGITLINK {1363my$mode=shift;13641365return(($mode& S_IFMT) == S_IFGITLINK)1366}13671368# convert file mode in octal to symbolic file mode string1369sub mode_str {1370my$mode=oct shift;13711372if(S_ISGITLINK($mode)) {1373return'm---------';1374}elsif(S_ISDIR($mode& S_IFMT)) {1375return'drwxr-xr-x';1376}elsif(S_ISLNK($mode)) {1377return'lrwxrwxrwx';1378}elsif(S_ISREG($mode)) {1379# git cares only about the executable bit1380if($mode& S_IXUSR) {1381return'-rwxr-xr-x';1382}else{1383return'-rw-r--r--';1384};1385}else{1386return'----------';1387}1388}13891390# convert file mode in octal to file type string1391sub file_type {1392my$mode=shift;13931394if($mode!~m/^[0-7]+$/) {1395return$mode;1396}else{1397$mode=oct$mode;1398}13991400if(S_ISGITLINK($mode)) {1401return"submodule";1402}elsif(S_ISDIR($mode& S_IFMT)) {1403return"directory";1404}elsif(S_ISLNK($mode)) {1405return"symlink";1406}elsif(S_ISREG($mode)) {1407return"file";1408}else{1409return"unknown";1410}1411}14121413# convert file mode in octal to file type description string1414sub file_type_long {1415my$mode=shift;14161417if($mode!~m/^[0-7]+$/) {1418return$mode;1419}else{1420$mode=oct$mode;1421}14221423if(S_ISGITLINK($mode)) {1424return"submodule";1425}elsif(S_ISDIR($mode& S_IFMT)) {1426return"directory";1427}elsif(S_ISLNK($mode)) {1428return"symlink";1429}elsif(S_ISREG($mode)) {1430if($mode& S_IXUSR) {1431return"executable";1432}else{1433return"file";1434};1435}else{1436return"unknown";1437}1438}143914401441## ----------------------------------------------------------------------1442## functions returning short HTML fragments, or transforming HTML fragments1443## which don't belong to other sections14441445# format line of commit message.1446sub format_log_line_html {1447my$line=shift;14481449$line= esc_html($line, -nbsp=>1);1450$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1451$cgi->a({-href => href(action=>"object", hash=>$1),1452-class=>"text"},$1);1453}eg;14541455return$line;1456}14571458# format marker of refs pointing to given object14591460# the destination action is chosen based on object type and current context:1461# - for annotated tags, we choose the tag view unless it's the current view1462# already, in which case we go to shortlog view1463# - for other refs, we keep the current view if we're in history, shortlog or1464# log view, and select shortlog otherwise1465sub format_ref_marker {1466my($refs,$id) =@_;1467my$markers='';14681469if(defined$refs->{$id}) {1470foreachmy$ref(@{$refs->{$id}}) {1471# this code exploits the fact that non-lightweight tags are the1472# only indirect objects, and that they are the only objects for which1473# we want to use tag instead of shortlog as action1474my($type,$name) =qw();1475my$indirect= ($ref=~s/\^\{\}$//);1476# e.g. tags/v2.6.11 or heads/next1477if($ref=~m!^(.*?)s?/(.*)$!) {1478$type=$1;1479$name=$2;1480}else{1481$type="ref";1482$name=$ref;1483}14841485my$class=$type;1486$class.=" indirect"if$indirect;14871488my$dest_action="shortlog";14891490if($indirect) {1491$dest_action="tag"unless$actioneq"tag";1492}elsif($action=~/^(history|(short)?log)$/) {1493$dest_action=$action;1494}14951496my$dest="";1497$dest.="refs/"unless$ref=~ m!^refs/!;1498$dest.=$ref;14991500my$link=$cgi->a({1501-href => href(1502 action=>$dest_action,1503 hash=>$dest1504)},$name);15051506$markers.=" <span class=\"$class\"title=\"$ref\">".1507$link."</span>";1508}1509}15101511if($markers) {1512return' <span class="refs">'.$markers.'</span>';1513}else{1514return"";1515}1516}15171518# format, perhaps shortened and with markers, title line1519sub format_subject_html {1520my($long,$short,$href,$extra) =@_;1521$extra=''unlessdefined($extra);15221523if(length($short) <length($long)) {1524$long=~s/[[:cntrl:]]/?/g;1525return$cgi->a({-href =>$href, -class=>"list subject",1526-title => to_utf8($long)},1527 esc_html($short) .$extra);1528}else{1529return$cgi->a({-href =>$href, -class=>"list subject"},1530 esc_html($long) .$extra);1531}1532}15331534# Rather than recomputing the url for an email multiple times, we cache it1535# after the first hit. This gives a visible benefit in views where the avatar1536# for the same email is used repeatedly (e.g. shortlog).1537# The cache is shared by all avatar engines (currently gravatar only), which1538# are free to use it as preferred. Since only one avatar engine is used for any1539# given page, there's no risk for cache conflicts.1540our%avatar_cache= ();15411542# Compute the picon url for a given email, by using the picon search service over at1543# http://www.cs.indiana.edu/picons/search.html1544sub picon_url {1545my$email=lc shift;1546if(!$avatar_cache{$email}) {1547my($user,$domain) =split('@',$email);1548$avatar_cache{$email} =1549"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1550"$domain/$user/".1551"users+domains+unknown/up/single";1552}1553return$avatar_cache{$email};1554}15551556# Compute the gravatar url for a given email, if it's not in the cache already.1557# Gravatar stores only the part of the URL before the size, since that's the1558# one computationally more expensive. This also allows reuse of the cache for1559# different sizes (for this particular engine).1560sub gravatar_url {1561my$email=lc shift;1562my$size=shift;1563$avatar_cache{$email} ||=1564"http://www.gravatar.com/avatar/".1565 Digest::MD5::md5_hex($email) ."?s=";1566return$avatar_cache{$email} .$size;1567}15681569# Insert an avatar for the given $email at the given $size if the feature1570# is enabled.1571sub git_get_avatar {1572my($email,%opts) =@_;1573my$pre_white= ($opts{-pad_before} ?" ":"");1574my$post_white= ($opts{-pad_after} ?" ":"");1575$opts{-size} ||='default';1576my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1577my$url="";1578if($git_avatareq'gravatar') {1579$url= gravatar_url($email,$size);1580}elsif($git_avatareq'picon') {1581$url= picon_url($email);1582}1583# Other providers can be added by extending the if chain, defining $url1584# as needed. If no variant puts something in $url, we assume avatars1585# are completely disabled/unavailable.1586if($url) {1587return$pre_white.1588"<img width=\"$size\"".1589"class=\"avatar\"".1590"src=\"$url\"".1591"alt=\"\"".1592"/>".$post_white;1593}else{1594return"";1595}1596}15971598# format the author name of the given commit with the given tag1599# the author name is chopped and escaped according to the other1600# optional parameters (see chop_str).1601sub format_author_html {1602my$tag=shift;1603my$co=shift;1604my$author= chop_and_escape_str($co->{'author_name'},@_);1605return"<$tagclass=\"author\">".1606 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1607$author."</$tag>";1608}16091610# format git diff header line, i.e. "diff --(git|combined|cc) ..."1611sub format_git_diff_header_line {1612my$line=shift;1613my$diffinfo=shift;1614my($from,$to) =@_;16151616if($diffinfo->{'nparents'}) {1617# combined diff1618$line=~s!^(diff (.*?) )"?.*$!$1!;1619if($to->{'href'}) {1620$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1621 esc_path($to->{'file'}));1622}else{# file was deleted (no href)1623$line.= esc_path($to->{'file'});1624}1625}else{1626# "ordinary" diff1627$line=~s!^(diff (.*?) )"?a/.*$!$1!;1628if($from->{'href'}) {1629$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1630'a/'. esc_path($from->{'file'}));1631}else{# file was added (no href)1632$line.='a/'. esc_path($from->{'file'});1633}1634$line.=' ';1635if($to->{'href'}) {1636$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1637'b/'. esc_path($to->{'file'}));1638}else{# file was deleted1639$line.='b/'. esc_path($to->{'file'});1640}1641}16421643return"<div class=\"diff header\">$line</div>\n";1644}16451646# format extended diff header line, before patch itself1647sub format_extended_diff_header_line {1648my$line=shift;1649my$diffinfo=shift;1650my($from,$to) =@_;16511652# match <path>1653if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1654$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1655 esc_path($from->{'file'}));1656}1657if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1658$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1659 esc_path($to->{'file'}));1660}1661# match single <mode>1662if($line=~m/\s(\d{6})$/) {1663$line.='<span class="info"> ('.1664 file_type_long($1) .1665')</span>';1666}1667# match <hash>1668if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1669# can match only for combined diff1670$line='index ';1671for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1672if($from->{'href'}[$i]) {1673$line.=$cgi->a({-href=>$from->{'href'}[$i],1674-class=>"hash"},1675substr($diffinfo->{'from_id'}[$i],0,7));1676}else{1677$line.='0' x 7;1678}1679# separator1680$line.=','if($i<$diffinfo->{'nparents'} -1);1681}1682$line.='..';1683if($to->{'href'}) {1684$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1685substr($diffinfo->{'to_id'},0,7));1686}else{1687$line.='0' x 7;1688}16891690}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1691# can match only for ordinary diff1692my($from_link,$to_link);1693if($from->{'href'}) {1694$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1695substr($diffinfo->{'from_id'},0,7));1696}else{1697$from_link='0' x 7;1698}1699if($to->{'href'}) {1700$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1701substr($diffinfo->{'to_id'},0,7));1702}else{1703$to_link='0' x 7;1704}1705my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1706$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1707}17081709return$line."<br/>\n";1710}17111712# format from-file/to-file diff header1713sub format_diff_from_to_header {1714my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1715my$line;1716my$result='';17171718$line=$from_line;1719#assert($line =~ m/^---/) if DEBUG;1720# no extra formatting for "^--- /dev/null"1721if(!$diffinfo->{'nparents'}) {1722# ordinary (single parent) diff1723if($line=~m!^--- "?a/!) {1724if($from->{'href'}) {1725$line='--- a/'.1726$cgi->a({-href=>$from->{'href'}, -class=>"path"},1727 esc_path($from->{'file'}));1728}else{1729$line='--- a/'.1730 esc_path($from->{'file'});1731}1732}1733$result.= qq!<div class="diff from_file">$line</div>\n!;17341735}else{1736# combined diff (merge commit)1737for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1738if($from->{'href'}[$i]) {1739$line='--- '.1740$cgi->a({-href=>href(action=>"blobdiff",1741 hash_parent=>$diffinfo->{'from_id'}[$i],1742 hash_parent_base=>$parents[$i],1743 file_parent=>$from->{'file'}[$i],1744 hash=>$diffinfo->{'to_id'},1745 hash_base=>$hash,1746 file_name=>$to->{'file'}),1747-class=>"path",1748-title=>"diff". ($i+1)},1749$i+1) .1750'/'.1751$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1752 esc_path($from->{'file'}[$i]));1753}else{1754$line='--- /dev/null';1755}1756$result.= qq!<div class="diff from_file">$line</div>\n!;1757}1758}17591760$line=$to_line;1761#assert($line =~ m/^\+\+\+/) if DEBUG;1762# no extra formatting for "^+++ /dev/null"1763if($line=~m!^\+\+\+ "?b/!) {1764if($to->{'href'}) {1765$line='+++ b/'.1766$cgi->a({-href=>$to->{'href'}, -class=>"path"},1767 esc_path($to->{'file'}));1768}else{1769$line='+++ b/'.1770 esc_path($to->{'file'});1771}1772}1773$result.= qq!<div class="diff to_file">$line</div>\n!;17741775return$result;1776}17771778# create note for patch simplified by combined diff1779sub format_diff_cc_simplified {1780my($diffinfo,@parents) =@_;1781my$result='';17821783$result.="<div class=\"diff header\">".1784"diff --cc ";1785if(!is_deleted($diffinfo)) {1786$result.=$cgi->a({-href => href(action=>"blob",1787 hash_base=>$hash,1788 hash=>$diffinfo->{'to_id'},1789 file_name=>$diffinfo->{'to_file'}),1790-class=>"path"},1791 esc_path($diffinfo->{'to_file'}));1792}else{1793$result.= esc_path($diffinfo->{'to_file'});1794}1795$result.="</div>\n".# class="diff header"1796"<div class=\"diff nodifferences\">".1797"Simple merge".1798"</div>\n";# class="diff nodifferences"17991800return$result;1801}18021803# format patch (diff) line (not to be used for diff headers)1804sub format_diff_line {1805my$line=shift;1806my($from,$to) =@_;1807my$diff_class="";18081809chomp$line;18101811if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1812# combined diff1813my$prefix=substr($line,0,scalar@{$from->{'href'}});1814if($line=~m/^\@{3}/) {1815$diff_class=" chunk_header";1816}elsif($line=~m/^\\/) {1817$diff_class=" incomplete";1818}elsif($prefix=~tr/+/+/) {1819$diff_class=" add";1820}elsif($prefix=~tr/-/-/) {1821$diff_class=" rem";1822}1823}else{1824# assume ordinary diff1825my$char=substr($line,0,1);1826if($chareq'+') {1827$diff_class=" add";1828}elsif($chareq'-') {1829$diff_class=" rem";1830}elsif($chareq'@') {1831$diff_class=" chunk_header";1832}elsif($chareq"\\") {1833$diff_class=" incomplete";1834}1835}1836$line= untabify($line);1837if($from&&$to&&$line=~m/^\@{2} /) {1838my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1839$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;18401841$from_lines=0unlessdefined$from_lines;1842$to_lines=0unlessdefined$to_lines;18431844if($from->{'href'}) {1845$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1846-class=>"list"},$from_text);1847}1848if($to->{'href'}) {1849$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1850-class=>"list"},$to_text);1851}1852$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1853"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1854return"<div class=\"diff$diff_class\">$line</div>\n";1855}elsif($from&&$to&&$line=~m/^\@{3}/) {1856my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1857my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);18581859@from_text=split(' ',$ranges);1860for(my$i=0;$i<@from_text; ++$i) {1861($from_start[$i],$from_nlines[$i]) =1862(split(',',substr($from_text[$i],1)),0);1863}18641865$to_text=pop@from_text;1866$to_start=pop@from_start;1867$to_nlines=pop@from_nlines;18681869$line="<span class=\"chunk_info\">$prefix";1870for(my$i=0;$i<@from_text; ++$i) {1871if($from->{'href'}[$i]) {1872$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1873-class=>"list"},$from_text[$i]);1874}else{1875$line.=$from_text[$i];1876}1877$line.=" ";1878}1879if($to->{'href'}) {1880$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1881-class=>"list"},$to_text);1882}else{1883$line.=$to_text;1884}1885$line.="$prefix</span>".1886"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1887return"<div class=\"diff$diff_class\">$line</div>\n";1888}1889return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1890}18911892# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1893# linked. Pass the hash of the tree/commit to snapshot.1894sub format_snapshot_links {1895my($hash) =@_;1896my$num_fmts=@snapshot_fmts;1897if($num_fmts>1) {1898# A parenthesized list of links bearing format names.1899# e.g. "snapshot (_tar.gz_ _zip_)"1900return"snapshot (".join(' ',map1901$cgi->a({1902-href => href(1903 action=>"snapshot",1904 hash=>$hash,1905 snapshot_format=>$_1906)1907},$known_snapshot_formats{$_}{'display'})1908,@snapshot_fmts) .")";1909}elsif($num_fmts==1) {1910# A single "snapshot" link whose tooltip bears the format name.1911# i.e. "_snapshot_"1912my($fmt) =@snapshot_fmts;1913return1914$cgi->a({1915-href => href(1916 action=>"snapshot",1917 hash=>$hash,1918 snapshot_format=>$fmt1919),1920-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1921},"snapshot");1922}else{# $num_fmts == 01923returnundef;1924}1925}19261927## ......................................................................1928## functions returning values to be passed, perhaps after some1929## transformation, to other functions; e.g. returning arguments to href()19301931# returns hash to be passed to href to generate gitweb URL1932# in -title key it returns description of link1933sub get_feed_info {1934my$format=shift||'Atom';1935my%res= (action =>lc($format));19361937# feed links are possible only for project views1938return unless(defined$project);1939# some views should link to OPML, or to generic project feed,1940# or don't have specific feed yet (so they should use generic)1941return if($action=~/^(?:tags|heads|forks|tag|search)$/x);19421943my$branch;1944# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1945# from tag links; this also makes possible to detect branch links1946if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1947(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1948$branch=$1;1949}1950# find log type for feed description (title)1951my$type='log';1952if(defined$file_name) {1953$type="history of$file_name";1954$type.="/"if($actioneq'tree');1955$type.=" on '$branch'"if(defined$branch);1956}else{1957$type="log of$branch"if(defined$branch);1958}19591960$res{-title} =$type;1961$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1962$res{'file_name'} =$file_name;19631964return%res;1965}19661967## ----------------------------------------------------------------------1968## git utility subroutines, invoking git commands19691970# returns path to the core git executable and the --git-dir parameter as list1971sub git_cmd {1972$number_of_git_cmds++;1973return$GIT,'--git-dir='.$git_dir;1974}19751976# quote the given arguments for passing them to the shell1977# quote_command("command", "arg 1", "arg with ' and ! characters")1978# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1979# Try to avoid using this function wherever possible.1980sub quote_command {1981returnjoin(' ',1982map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);1983}19841985# get HEAD ref of given project as hash1986sub git_get_head_hash {1987my$project=shift;1988my$o_git_dir=$git_dir;1989my$retval=undef;1990$git_dir="$projectroot/$project";1991if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1992my$head= <$fd>;1993close$fd;1994if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1995$retval=$1;1996}1997}1998if(defined$o_git_dir) {1999$git_dir=$o_git_dir;2000}2001return$retval;2002}20032004# get type of given object2005sub git_get_type {2006my$hash=shift;20072008open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2009my$type= <$fd>;2010close$fdorreturn;2011chomp$type;2012return$type;2013}20142015# repository configuration2016our$config_file='';2017our%config;20182019# store multiple values for single key as anonymous array reference2020# single values stored directly in the hash, not as [ <value> ]2021sub hash_set_multi {2022my($hash,$key,$value) =@_;20232024if(!exists$hash->{$key}) {2025$hash->{$key} =$value;2026}elsif(!ref$hash->{$key}) {2027$hash->{$key} = [$hash->{$key},$value];2028}else{2029push@{$hash->{$key}},$value;2030}2031}20322033# return hash of git project configuration2034# optionally limited to some section, e.g. 'gitweb'2035sub git_parse_project_config {2036my$section_regexp=shift;2037my%config;20382039local$/="\0";20402041open my$fh,"-|", git_cmd(),"config",'-z','-l',2042orreturn;20432044while(my$keyval= <$fh>) {2045chomp$keyval;2046my($key,$value) =split(/\n/,$keyval,2);20472048 hash_set_multi(\%config,$key,$value)2049if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2050}2051close$fh;20522053return%config;2054}20552056# convert config value to boolean: 'true' or 'false'2057# no value, number > 0, 'true' and 'yes' values are true2058# rest of values are treated as false (never as error)2059sub config_to_bool {2060my$val=shift;20612062return1if!defined$val;# section.key20632064# strip leading and trailing whitespace2065$val=~s/^\s+//;2066$val=~s/\s+$//;20672068return(($val=~/^\d+$/&&$val) ||# section.key = 12069($val=~/^(?:true|yes)$/i));# section.key = true2070}20712072# convert config value to simple decimal number2073# an optional value suffix of 'k', 'm', or 'g' will cause the value2074# to be multiplied by 1024, 1048576, or 10737418242075sub config_to_int {2076my$val=shift;20772078# strip leading and trailing whitespace2079$val=~s/^\s+//;2080$val=~s/\s+$//;20812082if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2083$unit=lc($unit);2084# unknown unit is treated as 12085return$num* ($uniteq'g'?1073741824:2086$uniteq'm'?1048576:2087$uniteq'k'?1024:1);2088}2089return$val;2090}20912092# convert config value to array reference, if needed2093sub config_to_multi {2094my$val=shift;20952096returnref($val) ?$val: (defined($val) ? [$val] : []);2097}20982099sub git_get_project_config {2100my($key,$type) =@_;21012102# key sanity check2103return unless($key);2104$key=~s/^gitweb\.//;2105return if($key=~m/\W/);21062107# type sanity check2108if(defined$type) {2109$type=~s/^--//;2110$type=undef2111unless($typeeq'bool'||$typeeq'int');2112}21132114# get config2115if(!defined$config_file||2116$config_filene"$git_dir/config") {2117%config= git_parse_project_config('gitweb');2118$config_file="$git_dir/config";2119}21202121# check if config variable (key) exists2122return unlessexists$config{"gitweb.$key"};21232124# ensure given type2125if(!defined$type) {2126return$config{"gitweb.$key"};2127}elsif($typeeq'bool') {2128# backward compatibility: 'git config --bool' returns true/false2129return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2130}elsif($typeeq'int') {2131return config_to_int($config{"gitweb.$key"});2132}2133return$config{"gitweb.$key"};2134}21352136# get hash of given path at given ref2137sub git_get_hash_by_path {2138my$base=shift;2139my$path=shift||returnundef;2140my$type=shift;21412142$path=~ s,/+$,,;21432144open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2145or die_error(500,"Open git-ls-tree failed");2146my$line= <$fd>;2147close$fdorreturnundef;21482149if(!defined$line) {2150# there is no tree or hash given by $path at $base2151returnundef;2152}21532154#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2155$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2156if(defined$type&&$typene$2) {2157# type doesn't match2158returnundef;2159}2160return$3;2161}21622163# get path of entry with given hash at given tree-ish (ref)2164# used to get 'from' filename for combined diff (merge commit) for renames2165sub git_get_path_by_hash {2166my$base=shift||return;2167my$hash=shift||return;21682169local$/="\0";21702171open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2172orreturnundef;2173while(my$line= <$fd>) {2174chomp$line;21752176#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2177#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2178if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2179close$fd;2180return$1;2181}2182}2183close$fd;2184returnundef;2185}21862187## ......................................................................2188## git utility functions, directly accessing git repository21892190sub git_get_project_description {2191my$path=shift;21922193$git_dir="$projectroot/$path";2194open my$fd,'<',"$git_dir/description"2195orreturn git_get_project_config('description');2196my$descr= <$fd>;2197close$fd;2198if(defined$descr) {2199chomp$descr;2200}2201return$descr;2202}22032204sub git_get_project_ctags {2205my$path=shift;2206my$ctags= {};22072208$git_dir="$projectroot/$path";2209opendir my$dh,"$git_dir/ctags"2210orreturn$ctags;2211foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2212open my$ct,'<',$_ornext;2213my$val= <$ct>;2214chomp$val;2215close$ct;2216my$ctag=$_;$ctag=~ s#.*/##;2217$ctags->{$ctag} =$val;2218}2219closedir$dh;2220$ctags;2221}22222223sub git_populate_project_tagcloud {2224my$ctags=shift;22252226# First, merge different-cased tags; tags vote on casing2227my%ctags_lc;2228foreach(keys%$ctags) {2229$ctags_lc{lc$_}->{count} +=$ctags->{$_};2230if(not$ctags_lc{lc$_}->{topcount}2231or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2232$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2233$ctags_lc{lc$_}->{topname} =$_;2234}2235}22362237my$cloud;2238if(eval{require HTML::TagCloud;1; }) {2239$cloud= HTML::TagCloud->new;2240foreach(sort keys%ctags_lc) {2241# Pad the title with spaces so that the cloud looks2242# less crammed.2243my$title=$ctags_lc{$_}->{topname};2244$title=~s/ / /g;2245$title=~s/^/ /g;2246$title=~s/$/ /g;2247$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2248}2249}else{2250$cloud= \%ctags_lc;2251}2252$cloud;2253}22542255sub git_show_project_tagcloud {2256my($cloud,$count) =@_;2257print STDERR ref($cloud)."..\n";2258if(ref$cloudeq'HTML::TagCloud') {2259return$cloud->html_and_css($count);2260}else{2261my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2262return'<p align="center">'.join(', ',map{2263"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2264}splice(@tags,0,$count)) .'</p>';2265}2266}22672268sub git_get_project_url_list {2269my$path=shift;22702271$git_dir="$projectroot/$path";2272open my$fd,'<',"$git_dir/cloneurl"2273orreturnwantarray?2274@{ config_to_multi(git_get_project_config('url')) } :2275 config_to_multi(git_get_project_config('url'));2276my@git_project_url_list=map{chomp;$_} <$fd>;2277close$fd;22782279returnwantarray?@git_project_url_list: \@git_project_url_list;2280}22812282sub git_get_projects_list {2283my($filter) =@_;2284my@list;22852286$filter||='';2287$filter=~s/\.git$//;22882289my$check_forks= gitweb_check_feature('forks');22902291if(-d $projects_list) {2292# search in directory2293my$dir=$projects_list. ($filter?"/$filter":'');2294# remove the trailing "/"2295$dir=~s!/+$!!;2296my$pfxlen=length("$dir");2297my$pfxdepth= ($dir=~tr!/!!);22982299 File::Find::find({2300 follow_fast =>1,# follow symbolic links2301 follow_skip =>2,# ignore duplicates2302 dangling_symlinks =>0,# ignore dangling symlinks, silently2303 wanted =>sub{2304# skip project-list toplevel, if we get it.2305return if(m!^[/.]$!);2306# only directories can be git repositories2307return unless(-d $_);2308# don't traverse too deep (Find is super slow on os x)2309if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2310$File::Find::prune =1;2311return;2312}23132314my$subdir=substr($File::Find::name,$pfxlen+1);2315# we check related file in $projectroot2316my$path= ($filter?"$filter/":'') .$subdir;2317if(check_export_ok("$projectroot/$path")) {2318push@list, { path =>$path};2319$File::Find::prune =1;2320}2321},2322},"$dir");23232324}elsif(-f $projects_list) {2325# read from file(url-encoded):2326# 'git%2Fgit.git Linus+Torvalds'2327# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2328# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2329my%paths;2330open my$fd,'<',$projects_listorreturn;2331 PROJECT:2332while(my$line= <$fd>) {2333chomp$line;2334my($path,$owner) =split' ',$line;2335$path= unescape($path);2336$owner= unescape($owner);2337if(!defined$path) {2338next;2339}2340if($filterne'') {2341# looking for forks;2342my$pfx=substr($path,0,length($filter));2343if($pfxne$filter) {2344next PROJECT;2345}2346my$sfx=substr($path,length($filter));2347if($sfx!~/^\/.*\.git$/) {2348next PROJECT;2349}2350}elsif($check_forks) {2351 PATH:2352foreachmy$filter(keys%paths) {2353# looking for forks;2354my$pfx=substr($path,0,length($filter));2355if($pfxne$filter) {2356next PATH;2357}2358my$sfx=substr($path,length($filter));2359if($sfx!~/^\/.*\.git$/) {2360next PATH;2361}2362# is a fork, don't include it in2363# the list2364next PROJECT;2365}2366}2367if(check_export_ok("$projectroot/$path")) {2368my$pr= {2369 path =>$path,2370 owner => to_utf8($owner),2371};2372push@list,$pr;2373(my$forks_path=$path) =~s/\.git$//;2374$paths{$forks_path}++;2375}2376}2377close$fd;2378}2379return@list;2380}23812382our$gitweb_project_owner=undef;2383sub git_get_project_list_from_file {23842385return if(defined$gitweb_project_owner);23862387$gitweb_project_owner= {};2388# read from file (url-encoded):2389# 'git%2Fgit.git Linus+Torvalds'2390# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2391# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2392if(-f $projects_list) {2393open(my$fd,'<',$projects_list);2394while(my$line= <$fd>) {2395chomp$line;2396my($pr,$ow) =split' ',$line;2397$pr= unescape($pr);2398$ow= unescape($ow);2399$gitweb_project_owner->{$pr} = to_utf8($ow);2400}2401close$fd;2402}2403}24042405sub git_get_project_owner {2406my$project=shift;2407my$owner;24082409returnundefunless$project;2410$git_dir="$projectroot/$project";24112412if(!defined$gitweb_project_owner) {2413 git_get_project_list_from_file();2414}24152416if(exists$gitweb_project_owner->{$project}) {2417$owner=$gitweb_project_owner->{$project};2418}2419if(!defined$owner){2420$owner= git_get_project_config('owner');2421}2422if(!defined$owner) {2423$owner= get_file_owner("$git_dir");2424}24252426return$owner;2427}24282429sub git_get_last_activity {2430my($path) =@_;2431my$fd;24322433$git_dir="$projectroot/$path";2434open($fd,"-|", git_cmd(),'for-each-ref',2435'--format=%(committer)',2436'--sort=-committerdate',2437'--count=1',2438'refs/heads')orreturn;2439my$most_recent= <$fd>;2440close$fdorreturn;2441if(defined$most_recent&&2442$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2443my$timestamp=$1;2444my$age=time-$timestamp;2445return($age, age_string($age));2446}2447return(undef,undef);2448}24492450sub git_get_references {2451my$type=shift||"";2452my%refs;2453# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112454# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2455open my$fd,"-|", git_cmd(),"show-ref","--dereference",2456($type? ("--","refs/$type") : ())# use -- <pattern> if $type2457orreturn;24582459while(my$line= <$fd>) {2460chomp$line;2461if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2462if(defined$refs{$1}) {2463push@{$refs{$1}},$2;2464}else{2465$refs{$1} = [$2];2466}2467}2468}2469close$fdorreturn;2470return \%refs;2471}24722473sub git_get_rev_name_tags {2474my$hash=shift||returnundef;24752476open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2477orreturn;2478my$name_rev= <$fd>;2479close$fd;24802481if($name_rev=~ m|^$hash tags/(.*)$|) {2482return$1;2483}else{2484# catches also '$hash undefined' output2485returnundef;2486}2487}24882489## ----------------------------------------------------------------------2490## parse to hash functions24912492sub parse_date {2493my$epoch=shift;2494my$tz=shift||"-0000";24952496my%date;2497my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2498my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2499my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2500$date{'hour'} =$hour;2501$date{'minute'} =$min;2502$date{'mday'} =$mday;2503$date{'day'} =$days[$wday];2504$date{'month'} =$months[$mon];2505$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2506$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2507$date{'mday-time'} =sprintf"%d%s%02d:%02d",2508$mday,$months[$mon],$hour,$min;2509$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",25101900+$year,1+$mon,$mday,$hour,$min,$sec;25112512$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2513my$local=$epoch+ ((int$1+ ($2/60)) *3600);2514($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2515$date{'hour_local'} =$hour;2516$date{'minute_local'} =$min;2517$date{'tz_local'} =$tz;2518$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",25191900+$year,$mon+1,$mday,2520$hour,$min,$sec,$tz);2521return%date;2522}25232524sub parse_tag {2525my$tag_id=shift;2526my%tag;2527my@comment;25282529open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2530$tag{'id'} =$tag_id;2531while(my$line= <$fd>) {2532chomp$line;2533if($line=~m/^object ([0-9a-fA-F]{40})$/) {2534$tag{'object'} =$1;2535}elsif($line=~m/^type (.+)$/) {2536$tag{'type'} =$1;2537}elsif($line=~m/^tag (.+)$/) {2538$tag{'name'} =$1;2539}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2540$tag{'author'} =$1;2541$tag{'author_epoch'} =$2;2542$tag{'author_tz'} =$3;2543if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2544$tag{'author_name'} =$1;2545$tag{'author_email'} =$2;2546}else{2547$tag{'author_name'} =$tag{'author'};2548}2549}elsif($line=~m/--BEGIN/) {2550push@comment,$line;2551last;2552}elsif($lineeq"") {2553last;2554}2555}2556push@comment, <$fd>;2557$tag{'comment'} = \@comment;2558close$fdorreturn;2559if(!defined$tag{'name'}) {2560return2561};2562return%tag2563}25642565sub parse_commit_text {2566my($commit_text,$withparents) =@_;2567my@commit_lines=split'\n',$commit_text;2568my%co;25692570pop@commit_lines;# Remove '\0'25712572if(!@commit_lines) {2573return;2574}25752576my$header=shift@commit_lines;2577if($header!~m/^[0-9a-fA-F]{40}/) {2578return;2579}2580($co{'id'},my@parents) =split' ',$header;2581while(my$line=shift@commit_lines) {2582last if$lineeq"\n";2583if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2584$co{'tree'} =$1;2585}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2586push@parents,$1;2587}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2588$co{'author'} =$1;2589$co{'author_epoch'} =$2;2590$co{'author_tz'} =$3;2591if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2592$co{'author_name'} =$1;2593$co{'author_email'} =$2;2594}else{2595$co{'author_name'} =$co{'author'};2596}2597}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2598$co{'committer'} =$1;2599$co{'committer_epoch'} =$2;2600$co{'committer_tz'} =$3;2601$co{'committer_name'} =$co{'committer'};2602if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2603$co{'committer_name'} =$1;2604$co{'committer_email'} =$2;2605}else{2606$co{'committer_name'} =$co{'committer'};2607}2608}2609}2610if(!defined$co{'tree'}) {2611return;2612};2613$co{'parents'} = \@parents;2614$co{'parent'} =$parents[0];26152616foreachmy$title(@commit_lines) {2617$title=~s/^ //;2618if($titlene"") {2619$co{'title'} = chop_str($title,80,5);2620# remove leading stuff of merges to make the interesting part visible2621if(length($title) >50) {2622$title=~s/^Automatic //;2623$title=~s/^merge (of|with) /Merge ... /i;2624if(length($title) >50) {2625$title=~s/(http|rsync):\/\///;2626}2627if(length($title) >50) {2628$title=~s/(master|www|rsync)\.//;2629}2630if(length($title) >50) {2631$title=~s/kernel.org:?//;2632}2633if(length($title) >50) {2634$title=~s/\/pub\/scm//;2635}2636}2637$co{'title_short'} = chop_str($title,50,5);2638last;2639}2640}2641if(!defined$co{'title'} ||$co{'title'}eq"") {2642$co{'title'} =$co{'title_short'} ='(no commit message)';2643}2644# remove added spaces2645foreachmy$line(@commit_lines) {2646$line=~s/^ //;2647}2648$co{'comment'} = \@commit_lines;26492650my$age=time-$co{'committer_epoch'};2651$co{'age'} =$age;2652$co{'age_string'} = age_string($age);2653my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2654if($age>60*60*24*7*2) {2655$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2656$co{'age_string_age'} =$co{'age_string'};2657}else{2658$co{'age_string_date'} =$co{'age_string'};2659$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2660}2661return%co;2662}26632664sub parse_commit {2665my($commit_id) =@_;2666my%co;26672668local$/="\0";26692670open my$fd,"-|", git_cmd(),"rev-list",2671"--parents",2672"--header",2673"--max-count=1",2674$commit_id,2675"--",2676or die_error(500,"Open git-rev-list failed");2677%co= parse_commit_text(<$fd>,1);2678close$fd;26792680return%co;2681}26822683sub parse_commits {2684my($commit_id,$maxcount,$skip,$filename,@args) =@_;2685my@cos;26862687$maxcount||=1;2688$skip||=0;26892690local$/="\0";26912692open my$fd,"-|", git_cmd(),"rev-list",2693"--header",2694@args,2695("--max-count=".$maxcount),2696("--skip=".$skip),2697@extra_options,2698$commit_id,2699"--",2700($filename? ($filename) : ())2701or die_error(500,"Open git-rev-list failed");2702while(my$line= <$fd>) {2703my%co= parse_commit_text($line);2704push@cos, \%co;2705}2706close$fd;27072708returnwantarray?@cos: \@cos;2709}27102711# parse line of git-diff-tree "raw" output2712sub parse_difftree_raw_line {2713my$line=shift;2714my%res;27152716# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2717# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2718if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2719$res{'from_mode'} =$1;2720$res{'to_mode'} =$2;2721$res{'from_id'} =$3;2722$res{'to_id'} =$4;2723$res{'status'} =$5;2724$res{'similarity'} =$6;2725if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2726($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2727}else{2728$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2729}2730}2731# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2732# combined diff (for merge commit)2733elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2734$res{'nparents'} =length($1);2735$res{'from_mode'} = [split(' ',$2) ];2736$res{'to_mode'} =pop@{$res{'from_mode'}};2737$res{'from_id'} = [split(' ',$3) ];2738$res{'to_id'} =pop@{$res{'from_id'}};2739$res{'status'} = [split('',$4) ];2740$res{'to_file'} = unquote($5);2741}2742# 'c512b523472485aef4fff9e57b229d9d243c967f'2743elsif($line=~m/^([0-9a-fA-F]{40})$/) {2744$res{'commit'} =$1;2745}27462747returnwantarray?%res: \%res;2748}27492750# wrapper: return parsed line of git-diff-tree "raw" output2751# (the argument might be raw line, or parsed info)2752sub parsed_difftree_line {2753my$line_or_ref=shift;27542755if(ref($line_or_ref)eq"HASH") {2756# pre-parsed (or generated by hand)2757return$line_or_ref;2758}else{2759return parse_difftree_raw_line($line_or_ref);2760}2761}27622763# parse line of git-ls-tree output2764sub parse_ls_tree_line {2765my$line=shift;2766my%opts=@_;2767my%res;27682769#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2770$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;27712772$res{'mode'} =$1;2773$res{'type'} =$2;2774$res{'hash'} =$3;2775if($opts{'-z'}) {2776$res{'name'} =$4;2777}else{2778$res{'name'} = unquote($4);2779}27802781returnwantarray?%res: \%res;2782}27832784# generates _two_ hashes, references to which are passed as 2 and 3 argument2785sub parse_from_to_diffinfo {2786my($diffinfo,$from,$to,@parents) =@_;27872788if($diffinfo->{'nparents'}) {2789# combined diff2790$from->{'file'} = [];2791$from->{'href'} = [];2792 fill_from_file_info($diffinfo,@parents)2793unlessexists$diffinfo->{'from_file'};2794for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2795$from->{'file'}[$i] =2796defined$diffinfo->{'from_file'}[$i] ?2797$diffinfo->{'from_file'}[$i] :2798$diffinfo->{'to_file'};2799if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2800$from->{'href'}[$i] = href(action=>"blob",2801 hash_base=>$parents[$i],2802 hash=>$diffinfo->{'from_id'}[$i],2803 file_name=>$from->{'file'}[$i]);2804}else{2805$from->{'href'}[$i] =undef;2806}2807}2808}else{2809# ordinary (not combined) diff2810$from->{'file'} =$diffinfo->{'from_file'};2811if($diffinfo->{'status'}ne"A") {# not new (added) file2812$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2813 hash=>$diffinfo->{'from_id'},2814 file_name=>$from->{'file'});2815}else{2816delete$from->{'href'};2817}2818}28192820$to->{'file'} =$diffinfo->{'to_file'};2821if(!is_deleted($diffinfo)) {# file exists in result2822$to->{'href'} = href(action=>"blob", hash_base=>$hash,2823 hash=>$diffinfo->{'to_id'},2824 file_name=>$to->{'file'});2825}else{2826delete$to->{'href'};2827}2828}28292830## ......................................................................2831## parse to array of hashes functions28322833sub git_get_heads_list {2834my$limit=shift;2835my@headslist;28362837open my$fd,'-|', git_cmd(),'for-each-ref',2838($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2839'--format=%(objectname) %(refname) %(subject)%00%(committer)',2840'refs/heads'2841orreturn;2842while(my$line= <$fd>) {2843my%ref_item;28442845chomp$line;2846my($refinfo,$committerinfo) =split(/\0/,$line);2847my($hash,$name,$title) =split(' ',$refinfo,3);2848my($committer,$epoch,$tz) =2849($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2850$ref_item{'fullname'} =$name;2851$name=~s!^refs/heads/!!;28522853$ref_item{'name'} =$name;2854$ref_item{'id'} =$hash;2855$ref_item{'title'} =$title||'(no commit message)';2856$ref_item{'epoch'} =$epoch;2857if($epoch) {2858$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2859}else{2860$ref_item{'age'} ="unknown";2861}28622863push@headslist, \%ref_item;2864}2865close$fd;28662867returnwantarray?@headslist: \@headslist;2868}28692870sub git_get_tags_list {2871my$limit=shift;2872my@tagslist;28732874open my$fd,'-|', git_cmd(),'for-each-ref',2875($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2876'--format=%(objectname) %(objecttype) %(refname) '.2877'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2878'refs/tags'2879orreturn;2880while(my$line= <$fd>) {2881my%ref_item;28822883chomp$line;2884my($refinfo,$creatorinfo) =split(/\0/,$line);2885my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2886my($creator,$epoch,$tz) =2887($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2888$ref_item{'fullname'} =$name;2889$name=~s!^refs/tags/!!;28902891$ref_item{'type'} =$type;2892$ref_item{'id'} =$id;2893$ref_item{'name'} =$name;2894if($typeeq"tag") {2895$ref_item{'subject'} =$title;2896$ref_item{'reftype'} =$reftype;2897$ref_item{'refid'} =$refid;2898}else{2899$ref_item{'reftype'} =$type;2900$ref_item{'refid'} =$id;2901}29022903if($typeeq"tag"||$typeeq"commit") {2904$ref_item{'epoch'} =$epoch;2905if($epoch) {2906$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2907}else{2908$ref_item{'age'} ="unknown";2909}2910}29112912push@tagslist, \%ref_item;2913}2914close$fd;29152916returnwantarray?@tagslist: \@tagslist;2917}29182919## ----------------------------------------------------------------------2920## filesystem-related functions29212922sub get_file_owner {2923my$path=shift;29242925my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2926my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2927if(!defined$gcos) {2928returnundef;2929}2930my$owner=$gcos;2931$owner=~s/[,;].*$//;2932return to_utf8($owner);2933}29342935# assume that file exists2936sub insert_file {2937my$filename=shift;29382939open my$fd,'<',$filename;2940print map{ to_utf8($_) } <$fd>;2941close$fd;2942}29432944## ......................................................................2945## mimetype related functions29462947sub mimetype_guess_file {2948my$filename=shift;2949my$mimemap=shift;2950-r $mimemaporreturnundef;29512952my%mimemap;2953open(my$mh,'<',$mimemap)orreturnundef;2954while(<$mh>) {2955next ifm/^#/;# skip comments2956my($mimetype,$exts) =split(/\t+/);2957if(defined$exts) {2958my@exts=split(/\s+/,$exts);2959foreachmy$ext(@exts) {2960$mimemap{$ext} =$mimetype;2961}2962}2963}2964close($mh);29652966$filename=~/\.([^.]*)$/;2967return$mimemap{$1};2968}29692970sub mimetype_guess {2971my$filename=shift;2972my$mime;2973$filename=~/\./orreturnundef;29742975if($mimetypes_file) {2976my$file=$mimetypes_file;2977if($file!~m!^/!) {# if it is relative path2978# it is relative to project2979$file="$projectroot/$project/$file";2980}2981$mime= mimetype_guess_file($filename,$file);2982}2983$mime||= mimetype_guess_file($filename,'/etc/mime.types');2984return$mime;2985}29862987sub blob_mimetype {2988my$fd=shift;2989my$filename=shift;29902991if($filename) {2992my$mime= mimetype_guess($filename);2993$mimeandreturn$mime;2994}29952996# just in case2997return$default_blob_plain_mimetypeunless$fd;29982999if(-T $fd) {3000return'text/plain';3001}elsif(!$filename) {3002return'application/octet-stream';3003}elsif($filename=~m/\.png$/i) {3004return'image/png';3005}elsif($filename=~m/\.gif$/i) {3006return'image/gif';3007}elsif($filename=~m/\.jpe?g$/i) {3008return'image/jpeg';3009}else{3010return'application/octet-stream';3011}3012}30133014sub blob_contenttype {3015my($fd,$file_name,$type) =@_;30163017$type||= blob_mimetype($fd,$file_name);3018if($typeeq'text/plain'&&defined$default_text_plain_charset) {3019$type.="; charset=$default_text_plain_charset";3020}30213022return$type;3023}30243025## ======================================================================3026## functions printing HTML: header, footer, error page30273028sub git_header_html {3029my$status=shift||"200 OK";3030my$expires=shift;30313032my$title="$site_name";3033if(defined$project) {3034$title.=" - ". to_utf8($project);3035if(defined$action) {3036$title.="/$action";3037if(defined$file_name) {3038$title.=" - ". esc_path($file_name);3039if($actioneq"tree"&&$file_name!~ m|/$|) {3040$title.="/";3041}3042}3043}3044}3045my$content_type;3046# require explicit support from the UA if we are to send the page as3047# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3048# we have to do this because MSIE sometimes globs '*/*', pretending to3049# support xhtml+xml but choking when it gets what it asked for.3050if(defined$cgi->http('HTTP_ACCEPT') &&3051$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3052$cgi->Accept('application/xhtml+xml') !=0) {3053$content_type='application/xhtml+xml';3054}else{3055$content_type='text/html';3056}3057print$cgi->header(-type=>$content_type, -charset =>'utf-8',3058-status=>$status, -expires =>$expires);3059my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3060print<<EOF;3061<?xml version="1.0" encoding="utf-8"?>3062<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3063<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3064<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3065<!-- git core binaries version$git_version-->3066<head>3067<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3068<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3069<meta name="robots" content="index, nofollow"/>3070<title>$title</title>3071EOF3072# the stylesheet, favicon etc urls won't work correctly with path_info3073# unless we set the appropriate base URL3074if($ENV{'PATH_INFO'}) {3075print"<base href=\"".esc_url($base_url)."\"/>\n";3076}3077# print out each stylesheet that exist, providing backwards capability3078# for those people who defined $stylesheet in a config file3079if(defined$stylesheet) {3080print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3081}else{3082foreachmy$stylesheet(@stylesheets) {3083next unless$stylesheet;3084print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3085}3086}3087if(defined$project) {3088my%href_params= get_feed_info();3089if(!exists$href_params{'-title'}) {3090$href_params{'-title'} ='log';3091}30923093foreachmy$formatqw(RSS Atom){3094my$type=lc($format);3095my%link_attr= (3096'-rel'=>'alternate',3097'-title'=>"$project-$href_params{'-title'} -$formatfeed",3098'-type'=>"application/$type+xml"3099);31003101$href_params{'action'} =$type;3102$link_attr{'-href'} = href(%href_params);3103print"<link ".3104"rel=\"$link_attr{'-rel'}\"".3105"title=\"$link_attr{'-title'}\"".3106"href=\"$link_attr{'-href'}\"".3107"type=\"$link_attr{'-type'}\"".3108"/>\n";31093110$href_params{'extra_options'} ='--no-merges';3111$link_attr{'-href'} = href(%href_params);3112$link_attr{'-title'} .=' (no merges)';3113print"<link ".3114"rel=\"$link_attr{'-rel'}\"".3115"title=\"$link_attr{'-title'}\"".3116"href=\"$link_attr{'-href'}\"".3117"type=\"$link_attr{'-type'}\"".3118"/>\n";3119}31203121}else{3122printf('<link rel="alternate" title="%sprojects list" '.3123'href="%s" type="text/plain; charset=utf-8" />'."\n",3124$site_name, href(project=>undef, action=>"project_index"));3125printf('<link rel="alternate" title="%sprojects feeds" '.3126'href="%s" type="text/x-opml" />'."\n",3127$site_name, href(project=>undef, action=>"opml"));3128}3129if(defined$favicon) {3130printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3131}31323133print"</head>\n".3134"<body>\n";31353136if(-f $site_header) {3137 insert_file($site_header);3138}31393140print"<div class=\"page_header\">\n".3141$cgi->a({-href => esc_url($logo_url),3142-title =>$logo_label},3143qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3144print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3145if(defined$project) {3146print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3147if(defined$action) {3148print" /$action";3149}3150print"\n";3151}3152print"</div>\n";31533154my$have_search= gitweb_check_feature('search');3155if(defined$project&&$have_search) {3156if(!defined$searchtext) {3157$searchtext="";3158}3159my$search_hash;3160if(defined$hash_base) {3161$search_hash=$hash_base;3162}elsif(defined$hash) {3163$search_hash=$hash;3164}else{3165$search_hash="HEAD";3166}3167my$action=$my_uri;3168my$use_pathinfo= gitweb_check_feature('pathinfo');3169if($use_pathinfo) {3170$action.="/".esc_url($project);3171}3172print$cgi->startform(-method=>"get", -action =>$action) .3173"<div class=\"search\">\n".3174(!$use_pathinfo&&3175$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3176$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3177$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3178$cgi->popup_menu(-name =>'st', -default=>'commit',3179-values=> ['commit','grep','author','committer','pickaxe']) .3180$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3181" search:\n",3182$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3183"<span title=\"Extended regular expression\">".3184$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3185-checked =>$search_use_regexp) .3186"</span>".3187"</div>".3188$cgi->end_form() ."\n";3189}3190}31913192sub git_footer_html {3193my$feed_class='rss_logo';31943195print"<div class=\"page_footer\">\n";3196if(defined$project) {3197my$descr= git_get_project_description($project);3198if(defined$descr) {3199print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3200}32013202my%href_params= get_feed_info();3203if(!%href_params) {3204$feed_class.=' generic';3205}3206$href_params{'-title'} ||='log';32073208foreachmy$formatqw(RSS Atom){3209$href_params{'action'} =lc($format);3210print$cgi->a({-href => href(%href_params),3211-title =>"$href_params{'-title'}$formatfeed",3212-class=>$feed_class},$format)."\n";3213}32143215}else{3216print$cgi->a({-href => href(project=>undef, action=>"opml"),3217-class=>$feed_class},"OPML") ." ";3218print$cgi->a({-href => href(project=>undef, action=>"project_index"),3219-class=>$feed_class},"TXT") ."\n";3220}3221print"</div>\n";# class="page_footer"32223223if(defined$t0&& gitweb_check_feature('timed')) {3224print"<div id=\"generating_info\">\n";3225print'This page took '.3226'<span id="generating_time" class="time_span">'.3227 Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]).3228' seconds </span>'.3229' and '.3230'<span id="generating_cmd">'.3231$number_of_git_cmds.3232'</span> git commands '.3233" to generate.\n";3234print"</div>\n";# class="page_footer"3235}32363237if(-f $site_footer) {3238 insert_file($site_footer);3239}32403241print"</body>\n".3242"</html>";3243}32443245# die_error(<http_status_code>, <error_message>)3246# Example: die_error(404, 'Hash not found')3247# By convention, use the following status codes (as defined in RFC 2616):3248# 400: Invalid or missing CGI parameters, or3249# requested object exists but has wrong type.3250# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3251# this server or project.3252# 404: Requested object/revision/project doesn't exist.3253# 500: The server isn't configured properly, or3254# an internal error occurred (e.g. failed assertions caused by bugs), or3255# an unknown error occurred (e.g. the git binary died unexpectedly).3256sub die_error {3257my$status=shift||500;3258my$error=shift||"Internal server error";32593260my%http_responses= (400=>'400 Bad Request',3261403=>'403 Forbidden',3262404=>'404 Not Found',3263500=>'500 Internal Server Error');3264 git_header_html($http_responses{$status});3265print<<EOF;3266<div class="page_body">3267<br /><br />3268$status-$error3269<br />3270</div>3271EOF3272 git_footer_html();3273exit;3274}32753276## ----------------------------------------------------------------------3277## functions printing or outputting HTML: navigation32783279sub git_print_page_nav {3280my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3281$extra=''if!defined$extra;# pager or formats32823283my@navs=qw(summary shortlog log commit commitdiff tree);3284if($suppress) {3285@navs=grep{$_ne$suppress}@navs;3286}32873288my%arg=map{$_=> {action=>$_} }@navs;3289if(defined$head) {3290for(qw(commit commitdiff)) {3291$arg{$_}{'hash'} =$head;3292}3293if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3294for(qw(shortlog log)) {3295$arg{$_}{'hash'} =$head;3296}3297}3298}32993300$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3301$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;33023303my@actions= gitweb_get_feature('actions');3304my%repl= (3305'%'=>'%',3306'n'=>$project,# project name3307'f'=>$git_dir,# project path within filesystem3308'h'=>$treehead||'',# current hash ('h' parameter)3309'b'=>$treebase||'',# hash base ('hb' parameter)3310);3311while(@actions) {3312my($label,$link,$pos) =splice(@actions,0,3);3313# insert3314@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3315# munch munch3316$link=~s/%([%nfhb])/$repl{$1}/g;3317$arg{$label}{'_href'} =$link;3318}33193320print"<div class=\"page_nav\">\n".3321(join" | ",3322map{$_eq$current?3323$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3324}@navs);3325print"<br/>\n$extra<br/>\n".3326"</div>\n";3327}33283329sub format_paging_nav {3330my($action,$hash,$head,$page,$has_next_link) =@_;3331my$paging_nav;333233333334if($hashne$head||$page) {3335$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3336}else{3337$paging_nav.="HEAD";3338}33393340if($page>0) {3341$paging_nav.=" ⋅ ".3342$cgi->a({-href => href(-replay=>1, page=>$page-1),3343-accesskey =>"p", -title =>"Alt-p"},"prev");3344}else{3345$paging_nav.=" ⋅ prev";3346}33473348if($has_next_link) {3349$paging_nav.=" ⋅ ".3350$cgi->a({-href => href(-replay=>1, page=>$page+1),3351-accesskey =>"n", -title =>"Alt-n"},"next");3352}else{3353$paging_nav.=" ⋅ next";3354}33553356return$paging_nav;3357}33583359## ......................................................................3360## functions printing or outputting HTML: div33613362sub git_print_header_div {3363my($action,$title,$hash,$hash_base) =@_;3364my%args= ();33653366$args{'action'} =$action;3367$args{'hash'} =$hashif$hash;3368$args{'hash_base'} =$hash_baseif$hash_base;33693370print"<div class=\"header\">\n".3371$cgi->a({-href => href(%args), -class=>"title"},3372$title?$title:$action) .3373"\n</div>\n";3374}33753376sub print_local_time {3377my%date=@_;3378if($date{'hour_local'} <6) {3379printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3380$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3381}else{3382printf(" (%02d:%02d%s)",3383$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3384}3385}33863387# Outputs the author name and date in long form3388sub git_print_authorship {3389my$co=shift;3390my%opts=@_;3391my$tag=$opts{-tag} ||'div';33923393my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3394print"<$tagclass=\"author_date\">".3395 esc_html($co->{'author_name'}) .3396" [$ad{'rfc2822'}";3397 print_local_time(%ad)if($opts{-localtime});3398print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3399."</$tag>\n";3400}34013402# Outputs table rows containing the full author or committer information,3403# in the format expected for 'commit' view (& similia).3404# Parameters are a commit hash reference, followed by the list of people3405# to output information for. If the list is empty it defalts to both3406# author and committer.3407sub git_print_authorship_rows {3408my$co=shift;3409# too bad we can't use @people = @_ || ('author', 'committer')3410my@people=@_;3411@people= ('author','committer')unless@people;3412foreachmy$who(@people) {3413my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3414print"<tr><td>$who</td><td>". esc_html($co->{$who}) ."</td>".3415"<td rowspan=\"2\">".3416 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3417"</td></tr>\n".3418"<tr>".3419"<td></td><td>$wd{'rfc2822'}";3420 print_local_time(%wd);3421print"</td>".3422"</tr>\n";3423}3424}34253426sub git_print_page_path {3427my$name=shift;3428my$type=shift;3429my$hb=shift;343034313432print"<div class=\"page_path\">";3433print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3434-title =>'tree root'}, to_utf8("[$project]"));3435print" / ";3436if(defined$name) {3437my@dirname=split'/',$name;3438my$basename=pop@dirname;3439my$fullname='';34403441foreachmy$dir(@dirname) {3442$fullname.= ($fullname?'/':'') .$dir;3443print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3444 hash_base=>$hb),3445-title =>$fullname}, esc_path($dir));3446print" / ";3447}3448if(defined$type&&$typeeq'blob') {3449print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3450 hash_base=>$hb),3451-title =>$name}, esc_path($basename));3452}elsif(defined$type&&$typeeq'tree') {3453print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3454 hash_base=>$hb),3455-title =>$name}, esc_path($basename));3456print" / ";3457}else{3458print esc_path($basename);3459}3460}3461print"<br/></div>\n";3462}34633464sub git_print_log {3465my$log=shift;3466my%opts=@_;34673468if($opts{'-remove_title'}) {3469# remove title, i.e. first line of log3470shift@$log;3471}3472# remove leading empty lines3473while(defined$log->[0] &&$log->[0]eq"") {3474shift@$log;3475}34763477# print log3478my$signoff=0;3479my$empty=0;3480foreachmy$line(@$log) {3481if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3482$signoff=1;3483$empty=0;3484if(!$opts{'-remove_signoff'}) {3485print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3486next;3487}else{3488# remove signoff lines3489next;3490}3491}else{3492$signoff=0;3493}34943495# print only one empty line3496# do not print empty line after signoff3497if($lineeq"") {3498next if($empty||$signoff);3499$empty=1;3500}else{3501$empty=0;3502}35033504print format_log_line_html($line) ."<br/>\n";3505}35063507if($opts{'-final_empty_line'}) {3508# end with single empty line3509print"<br/>\n"unless$empty;3510}3511}35123513# return link target (what link points to)3514sub git_get_link_target {3515my$hash=shift;3516my$link_target;35173518# read link3519open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3520orreturn;3521{3522local$/=undef;3523$link_target= <$fd>;3524}3525close$fd3526orreturn;35273528return$link_target;3529}35303531# given link target, and the directory (basedir) the link is in,3532# return target of link relative to top directory (top tree);3533# return undef if it is not possible (including absolute links).3534sub normalize_link_target {3535my($link_target,$basedir) =@_;35363537# absolute symlinks (beginning with '/') cannot be normalized3538return if(substr($link_target,0,1)eq'/');35393540# normalize link target to path from top (root) tree (dir)3541my$path;3542if($basedir) {3543$path=$basedir.'/'.$link_target;3544}else{3545# we are in top (root) tree (dir)3546$path=$link_target;3547}35483549# remove //, /./, and /../3550my@path_parts;3551foreachmy$part(split('/',$path)) {3552# discard '.' and ''3553next if(!$part||$parteq'.');3554# handle '..'3555if($parteq'..') {3556if(@path_parts) {3557pop@path_parts;3558}else{3559# link leads outside repository (outside top dir)3560return;3561}3562}else{3563push@path_parts,$part;3564}3565}3566$path=join('/',@path_parts);35673568return$path;3569}35703571# print tree entry (row of git_tree), but without encompassing <tr> element3572sub git_print_tree_entry {3573my($t,$basedir,$hash_base,$have_blame) =@_;35743575my%base_key= ();3576$base_key{'hash_base'} =$hash_baseifdefined$hash_base;35773578# The format of a table row is: mode list link. Where mode is3579# the mode of the entry, list is the name of the entry, an href,3580# and link is the action links of the entry.35813582print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3583if($t->{'type'}eq"blob") {3584print"<td class=\"list\">".3585$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3586 file_name=>"$basedir$t->{'name'}",%base_key),3587-class=>"list"}, esc_path($t->{'name'}));3588if(S_ISLNK(oct$t->{'mode'})) {3589my$link_target= git_get_link_target($t->{'hash'});3590if($link_target) {3591my$norm_target= normalize_link_target($link_target,$basedir);3592if(defined$norm_target) {3593print" -> ".3594$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3595 file_name=>$norm_target),3596-title =>$norm_target}, esc_path($link_target));3597}else{3598print" -> ". esc_path($link_target);3599}3600}3601}3602print"</td>\n";3603print"<td class=\"link\">";3604print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3605 file_name=>"$basedir$t->{'name'}",%base_key)},3606"blob");3607if($have_blame) {3608print" | ".3609$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3610 file_name=>"$basedir$t->{'name'}",%base_key)},3611"blame");3612}3613if(defined$hash_base) {3614print" | ".3615$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3616 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3617"history");3618}3619print" | ".3620$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3621 file_name=>"$basedir$t->{'name'}")},3622"raw");3623print"</td>\n";36243625}elsif($t->{'type'}eq"tree") {3626print"<td class=\"list\">";3627print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3628 file_name=>"$basedir$t->{'name'}",%base_key)},3629 esc_path($t->{'name'}));3630print"</td>\n";3631print"<td class=\"link\">";3632print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3633 file_name=>"$basedir$t->{'name'}",%base_key)},3634"tree");3635if(defined$hash_base) {3636print" | ".3637$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3638 file_name=>"$basedir$t->{'name'}")},3639"history");3640}3641print"</td>\n";3642}else{3643# unknown object: we can only present history for it3644# (this includes 'commit' object, i.e. submodule support)3645print"<td class=\"list\">".3646 esc_path($t->{'name'}) .3647"</td>\n";3648print"<td class=\"link\">";3649if(defined$hash_base) {3650print$cgi->a({-href => href(action=>"history",3651 hash_base=>$hash_base,3652 file_name=>"$basedir$t->{'name'}")},3653"history");3654}3655print"</td>\n";3656}3657}36583659## ......................................................................3660## functions printing large fragments of HTML36613662# get pre-image filenames for merge (combined) diff3663sub fill_from_file_info {3664my($diff,@parents) =@_;36653666$diff->{'from_file'} = [ ];3667$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3668for(my$i=0;$i<$diff->{'nparents'};$i++) {3669if($diff->{'status'}[$i]eq'R'||3670$diff->{'status'}[$i]eq'C') {3671$diff->{'from_file'}[$i] =3672 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3673}3674}36753676return$diff;3677}36783679# is current raw difftree line of file deletion3680sub is_deleted {3681my$diffinfo=shift;36823683return$diffinfo->{'to_id'}eq('0' x 40);3684}36853686# does patch correspond to [previous] difftree raw line3687# $diffinfo - hashref of parsed raw diff format3688# $patchinfo - hashref of parsed patch diff format3689# (the same keys as in $diffinfo)3690sub is_patch_split {3691my($diffinfo,$patchinfo) =@_;36923693returndefined$diffinfo&&defined$patchinfo3694&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3695}369636973698sub git_difftree_body {3699my($difftree,$hash,@parents) =@_;3700my($parent) =$parents[0];3701my$have_blame= gitweb_check_feature('blame');3702print"<div class=\"list_head\">\n";3703if($#{$difftree} >10) {3704print(($#{$difftree} +1) ." files changed:\n");3705}3706print"</div>\n";37073708print"<table class=\"".3709(@parents>1?"combined ":"") .3710"diff_tree\">\n";37113712# header only for combined diff in 'commitdiff' view3713my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3714if($has_header) {3715# table header3716print"<thead><tr>\n".3717"<th></th><th></th>\n";# filename, patchN link3718for(my$i=0;$i<@parents;$i++) {3719my$par=$parents[$i];3720print"<th>".3721$cgi->a({-href => href(action=>"commitdiff",3722 hash=>$hash, hash_parent=>$par),3723-title =>'commitdiff to parent number '.3724($i+1) .': '.substr($par,0,7)},3725$i+1) .3726" </th>\n";3727}3728print"</tr></thead>\n<tbody>\n";3729}37303731my$alternate=1;3732my$patchno=0;3733foreachmy$line(@{$difftree}) {3734my$diff= parsed_difftree_line($line);37353736if($alternate) {3737print"<tr class=\"dark\">\n";3738}else{3739print"<tr class=\"light\">\n";3740}3741$alternate^=1;37423743if(exists$diff->{'nparents'}) {# combined diff37443745 fill_from_file_info($diff,@parents)3746unlessexists$diff->{'from_file'};37473748if(!is_deleted($diff)) {3749# file exists in the result (child) commit3750print"<td>".3751$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3752 file_name=>$diff->{'to_file'},3753 hash_base=>$hash),3754-class=>"list"}, esc_path($diff->{'to_file'})) .3755"</td>\n";3756}else{3757print"<td>".3758 esc_path($diff->{'to_file'}) .3759"</td>\n";3760}37613762if($actioneq'commitdiff') {3763# link to patch3764$patchno++;3765print"<td class=\"link\">".3766$cgi->a({-href =>"#patch$patchno"},"patch") .3767" | ".3768"</td>\n";3769}37703771my$has_history=0;3772my$not_deleted=0;3773for(my$i=0;$i<$diff->{'nparents'};$i++) {3774my$hash_parent=$parents[$i];3775my$from_hash=$diff->{'from_id'}[$i];3776my$from_path=$diff->{'from_file'}[$i];3777my$status=$diff->{'status'}[$i];37783779$has_history||= ($statusne'A');3780$not_deleted||= ($statusne'D');37813782if($statuseq'A') {3783print"<td class=\"link\"align=\"right\"> | </td>\n";3784}elsif($statuseq'D') {3785print"<td class=\"link\">".3786$cgi->a({-href => href(action=>"blob",3787 hash_base=>$hash,3788 hash=>$from_hash,3789 file_name=>$from_path)},3790"blob". ($i+1)) .3791" | </td>\n";3792}else{3793if($diff->{'to_id'}eq$from_hash) {3794print"<td class=\"link nochange\">";3795}else{3796print"<td class=\"link\">";3797}3798print$cgi->a({-href => href(action=>"blobdiff",3799 hash=>$diff->{'to_id'},3800 hash_parent=>$from_hash,3801 hash_base=>$hash,3802 hash_parent_base=>$hash_parent,3803 file_name=>$diff->{'to_file'},3804 file_parent=>$from_path)},3805"diff". ($i+1)) .3806" | </td>\n";3807}3808}38093810print"<td class=\"link\">";3811if($not_deleted) {3812print$cgi->a({-href => href(action=>"blob",3813 hash=>$diff->{'to_id'},3814 file_name=>$diff->{'to_file'},3815 hash_base=>$hash)},3816"blob");3817print" | "if($has_history);3818}3819if($has_history) {3820print$cgi->a({-href => href(action=>"history",3821 file_name=>$diff->{'to_file'},3822 hash_base=>$hash)},3823"history");3824}3825print"</td>\n";38263827print"</tr>\n";3828next;# instead of 'else' clause, to avoid extra indent3829}3830# else ordinary diff38313832my($to_mode_oct,$to_mode_str,$to_file_type);3833my($from_mode_oct,$from_mode_str,$from_file_type);3834if($diff->{'to_mode'}ne('0' x 6)) {3835$to_mode_oct=oct$diff->{'to_mode'};3836if(S_ISREG($to_mode_oct)) {# only for regular file3837$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3838}3839$to_file_type= file_type($diff->{'to_mode'});3840}3841if($diff->{'from_mode'}ne('0' x 6)) {3842$from_mode_oct=oct$diff->{'from_mode'};3843if(S_ISREG($to_mode_oct)) {# only for regular file3844$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3845}3846$from_file_type= file_type($diff->{'from_mode'});3847}38483849if($diff->{'status'}eq"A") {# created3850my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3851$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3852$mode_chng.="]</span>";3853print"<td>";3854print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3855 hash_base=>$hash, file_name=>$diff->{'file'}),3856-class=>"list"}, esc_path($diff->{'file'}));3857print"</td>\n";3858print"<td>$mode_chng</td>\n";3859print"<td class=\"link\">";3860if($actioneq'commitdiff') {3861# link to patch3862$patchno++;3863print$cgi->a({-href =>"#patch$patchno"},"patch");3864print" | ";3865}3866print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3867 hash_base=>$hash, file_name=>$diff->{'file'})},3868"blob");3869print"</td>\n";38703871}elsif($diff->{'status'}eq"D") {# deleted3872my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3873print"<td>";3874print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3875 hash_base=>$parent, file_name=>$diff->{'file'}),3876-class=>"list"}, esc_path($diff->{'file'}));3877print"</td>\n";3878print"<td>$mode_chng</td>\n";3879print"<td class=\"link\">";3880if($actioneq'commitdiff') {3881# link to patch3882$patchno++;3883print$cgi->a({-href =>"#patch$patchno"},"patch");3884print" | ";3885}3886print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3887 hash_base=>$parent, file_name=>$diff->{'file'})},3888"blob") ." | ";3889if($have_blame) {3890print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3891 file_name=>$diff->{'file'})},3892"blame") ." | ";3893}3894print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3895 file_name=>$diff->{'file'})},3896"history");3897print"</td>\n";38983899}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3900my$mode_chnge="";3901if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3902$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3903if($from_file_typene$to_file_type) {3904$mode_chnge.=" from$from_file_typeto$to_file_type";3905}3906if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3907if($from_mode_str&&$to_mode_str) {3908$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3909}elsif($to_mode_str) {3910$mode_chnge.=" mode:$to_mode_str";3911}3912}3913$mode_chnge.="]</span>\n";3914}3915print"<td>";3916print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3917 hash_base=>$hash, file_name=>$diff->{'file'}),3918-class=>"list"}, esc_path($diff->{'file'}));3919print"</td>\n";3920print"<td>$mode_chnge</td>\n";3921print"<td class=\"link\">";3922if($actioneq'commitdiff') {3923# link to patch3924$patchno++;3925print$cgi->a({-href =>"#patch$patchno"},"patch") .3926" | ";3927}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3928# "commit" view and modified file (not onlu mode changed)3929print$cgi->a({-href => href(action=>"blobdiff",3930 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3931 hash_base=>$hash, hash_parent_base=>$parent,3932 file_name=>$diff->{'file'})},3933"diff") .3934" | ";3935}3936print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3937 hash_base=>$hash, file_name=>$diff->{'file'})},3938"blob") ." | ";3939if($have_blame) {3940print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3941 file_name=>$diff->{'file'})},3942"blame") ." | ";3943}3944print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3945 file_name=>$diff->{'file'})},3946"history");3947print"</td>\n";39483949}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3950my%status_name= ('R'=>'moved','C'=>'copied');3951my$nstatus=$status_name{$diff->{'status'}};3952my$mode_chng="";3953if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3954# mode also for directories, so we cannot use $to_mode_str3955$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3956}3957print"<td>".3958$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3959 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3960-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3961"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3962$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3963 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3964-class=>"list"}, esc_path($diff->{'from_file'})) .3965" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3966"<td class=\"link\">";3967if($actioneq'commitdiff') {3968# link to patch3969$patchno++;3970print$cgi->a({-href =>"#patch$patchno"},"patch") .3971" | ";3972}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3973# "commit" view and modified file (not only pure rename or copy)3974print$cgi->a({-href => href(action=>"blobdiff",3975 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3976 hash_base=>$hash, hash_parent_base=>$parent,3977 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3978"diff") .3979" | ";3980}3981print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3982 hash_base=>$parent, file_name=>$diff->{'to_file'})},3983"blob") ." | ";3984if($have_blame) {3985print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3986 file_name=>$diff->{'to_file'})},3987"blame") ." | ";3988}3989print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3990 file_name=>$diff->{'to_file'})},3991"history");3992print"</td>\n";39933994}# we should not encounter Unmerged (U) or Unknown (X) status3995print"</tr>\n";3996}3997print"</tbody>"if$has_header;3998print"</table>\n";3999}40004001sub git_patchset_body {4002my($fd,$difftree,$hash,@hash_parents) =@_;4003my($hash_parent) =$hash_parents[0];40044005my$is_combined= (@hash_parents>1);4006my$patch_idx=0;4007my$patch_number=0;4008my$patch_line;4009my$diffinfo;4010my$to_name;4011my(%from,%to);40124013print"<div class=\"patchset\">\n";40144015# skip to first patch4016while($patch_line= <$fd>) {4017chomp$patch_line;40184019last if($patch_line=~m/^diff /);4020}40214022 PATCH:4023while($patch_line) {40244025# parse "git diff" header line4026if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4027# $1 is from_name, which we do not use4028$to_name= unquote($2);4029$to_name=~s!^b/!!;4030}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4031# $1 is 'cc' or 'combined', which we do not use4032$to_name= unquote($2);4033}else{4034$to_name=undef;4035}40364037# check if current patch belong to current raw line4038# and parse raw git-diff line if needed4039if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4040# this is continuation of a split patch4041print"<div class=\"patch cont\">\n";4042}else{4043# advance raw git-diff output if needed4044$patch_idx++ifdefined$diffinfo;40454046# read and prepare patch information4047$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);40484049# compact combined diff output can have some patches skipped4050# find which patch (using pathname of result) we are at now;4051if($is_combined) {4052while($to_namene$diffinfo->{'to_file'}) {4053print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4054 format_diff_cc_simplified($diffinfo,@hash_parents) .4055"</div>\n";# class="patch"40564057$patch_idx++;4058$patch_number++;40594060last if$patch_idx>$#$difftree;4061$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4062}4063}40644065# modifies %from, %to hashes4066 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);40674068# this is first patch for raw difftree line with $patch_idx index4069# we index @$difftree array from 0, but number patches from 14070print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4071}40724073# git diff header4074#assert($patch_line =~ m/^diff /) if DEBUG;4075#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4076$patch_number++;4077# print "git diff" header4078print format_git_diff_header_line($patch_line,$diffinfo,4079 \%from, \%to);40804081# print extended diff header4082print"<div class=\"diff extended_header\">\n";4083 EXTENDED_HEADER:4084while($patch_line= <$fd>) {4085chomp$patch_line;40864087last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);40884089print format_extended_diff_header_line($patch_line,$diffinfo,4090 \%from, \%to);4091}4092print"</div>\n";# class="diff extended_header"40934094# from-file/to-file diff header4095if(!$patch_line) {4096print"</div>\n";# class="patch"4097last PATCH;4098}4099next PATCH if($patch_line=~m/^diff /);4100#assert($patch_line =~ m/^---/) if DEBUG;41014102my$last_patch_line=$patch_line;4103$patch_line= <$fd>;4104chomp$patch_line;4105#assert($patch_line =~ m/^\+\+\+/) if DEBUG;41064107print format_diff_from_to_header($last_patch_line,$patch_line,4108$diffinfo, \%from, \%to,4109@hash_parents);41104111# the patch itself4112 LINE:4113while($patch_line= <$fd>) {4114chomp$patch_line;41154116next PATCH if($patch_line=~m/^diff /);41174118print format_diff_line($patch_line, \%from, \%to);4119}41204121}continue{4122print"</div>\n";# class="patch"4123}41244125# for compact combined (--cc) format, with chunk and patch simpliciaction4126# patchset might be empty, but there might be unprocessed raw lines4127for(++$patch_idxif$patch_number>0;4128$patch_idx<@$difftree;4129++$patch_idx) {4130# read and prepare patch information4131$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);41324133# generate anchor for "patch" links in difftree / whatchanged part4134print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4135 format_diff_cc_simplified($diffinfo,@hash_parents) .4136"</div>\n";# class="patch"41374138$patch_number++;4139}41404141if($patch_number==0) {4142if(@hash_parents>1) {4143print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4144}else{4145print"<div class=\"diff nodifferences\">No differences found</div>\n";4146}4147}41484149print"</div>\n";# class="patchset"4150}41514152# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41534154# fills project list info (age, description, owner, forks) for each4155# project in the list, removing invalid projects from returned list4156# NOTE: modifies $projlist, but does not remove entries from it4157sub fill_project_list_info {4158my($projlist,$check_forks) =@_;4159my@projects;41604161my$show_ctags= gitweb_check_feature('ctags');4162 PROJECT:4163foreachmy$pr(@$projlist) {4164my(@activity) = git_get_last_activity($pr->{'path'});4165unless(@activity) {4166next PROJECT;4167}4168($pr->{'age'},$pr->{'age_string'}) =@activity;4169if(!defined$pr->{'descr'}) {4170my$descr= git_get_project_description($pr->{'path'}) ||"";4171$descr= to_utf8($descr);4172$pr->{'descr_long'} =$descr;4173$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4174}4175if(!defined$pr->{'owner'}) {4176$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4177}4178if($check_forks) {4179my$pname=$pr->{'path'};4180if(($pname=~s/\.git$//) &&4181($pname!~/\/$/) &&4182(-d "$projectroot/$pname")) {4183$pr->{'forks'} ="-d$projectroot/$pname";4184}else{4185$pr->{'forks'} =0;4186}4187}4188$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4189push@projects,$pr;4190}41914192return@projects;4193}41944195# print 'sort by' <th> element, generating 'sort by $name' replay link4196# if that order is not selected4197sub print_sort_th {4198my($name,$order,$header) =@_;4199$header||=ucfirst($name);42004201if($ordereq$name) {4202print"<th>$header</th>\n";4203}else{4204print"<th>".4205$cgi->a({-href => href(-replay=>1, order=>$name),4206-class=>"header"},$header) .4207"</th>\n";4208}4209}42104211sub git_project_list_body {4212# actually uses global variable $project4213my($projlist,$order,$from,$to,$extra,$no_header) =@_;42144215my$check_forks= gitweb_check_feature('forks');4216my@projects= fill_project_list_info($projlist,$check_forks);42174218$order||=$default_projects_order;4219$from=0unlessdefined$from;4220$to=$#projectsif(!defined$to||$#projects<$to);42214222my%order_info= (4223 project => { key =>'path', type =>'str'},4224 descr => { key =>'descr_long', type =>'str'},4225 owner => { key =>'owner', type =>'str'},4226 age => { key =>'age', type =>'num'}4227);4228my$oi=$order_info{$order};4229if($oi->{'type'}eq'str') {4230@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4231}else{4232@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4233}42344235my$show_ctags= gitweb_check_feature('ctags');4236if($show_ctags) {4237my%ctags;4238foreachmy$p(@projects) {4239foreachmy$ct(keys%{$p->{'ctags'}}) {4240$ctags{$ct} +=$p->{'ctags'}->{$ct};4241}4242}4243my$cloud= git_populate_project_tagcloud(\%ctags);4244print git_show_project_tagcloud($cloud,64);4245}42464247print"<table class=\"project_list\">\n";4248unless($no_header) {4249print"<tr>\n";4250if($check_forks) {4251print"<th></th>\n";4252}4253 print_sort_th('project',$order,'Project');4254 print_sort_th('descr',$order,'Description');4255 print_sort_th('owner',$order,'Owner');4256 print_sort_th('age',$order,'Last Change');4257print"<th></th>\n".# for links4258"</tr>\n";4259}4260my$alternate=1;4261my$tagfilter=$cgi->param('by_tag');4262for(my$i=$from;$i<=$to;$i++) {4263my$pr=$projects[$i];42644265next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4266next if$searchtextand not$pr->{'path'} =~/$searchtext/4267and not$pr->{'descr_long'} =~/$searchtext/;4268# Weed out forks or non-matching entries of search4269if($check_forks) {4270my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4271$forkbase="^$forkbase"if$forkbase;4272next ifnot$searchtextand not$tagfilterand$show_ctags4273and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4274}42754276if($alternate) {4277print"<tr class=\"dark\">\n";4278}else{4279print"<tr class=\"light\">\n";4280}4281$alternate^=1;4282if($check_forks) {4283print"<td>";4284if($pr->{'forks'}) {4285print"<!--$pr->{'forks'} -->\n";4286print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4287}4288print"</td>\n";4289}4290print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4291-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4292"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4293-class=>"list", -title =>$pr->{'descr_long'}},4294 esc_html($pr->{'descr'})) ."</td>\n".4295"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4296print"<td class=\"". age_class($pr->{'age'}) ."\">".4297(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4298"<td class=\"link\">".4299$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4300$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4301$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4302$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4303($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4304"</td>\n".4305"</tr>\n";4306}4307if(defined$extra) {4308print"<tr>\n";4309if($check_forks) {4310print"<td></td>\n";4311}4312print"<td colspan=\"5\">$extra</td>\n".4313"</tr>\n";4314}4315print"</table>\n";4316}43174318sub git_shortlog_body {4319# uses global variable $project4320my($commitlist,$from,$to,$refs,$extra) =@_;43214322$from=0unlessdefined$from;4323$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43244325print"<table class=\"shortlog\">\n";4326my$alternate=1;4327for(my$i=$from;$i<=$to;$i++) {4328my%co= %{$commitlist->[$i]};4329my$commit=$co{'id'};4330my$ref= format_ref_marker($refs,$commit);4331if($alternate) {4332print"<tr class=\"dark\">\n";4333}else{4334print"<tr class=\"light\">\n";4335}4336$alternate^=1;4337# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4338print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4339 format_author_html('td', \%co,10) ."<td>";4340print format_subject_html($co{'title'},$co{'title_short'},4341 href(action=>"commit", hash=>$commit),$ref);4342print"</td>\n".4343"<td class=\"link\">".4344$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4345$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4346$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4347my$snapshot_links= format_snapshot_links($commit);4348if(defined$snapshot_links) {4349print" | ".$snapshot_links;4350}4351print"</td>\n".4352"</tr>\n";4353}4354if(defined$extra) {4355print"<tr>\n".4356"<td colspan=\"4\">$extra</td>\n".4357"</tr>\n";4358}4359print"</table>\n";4360}43614362sub git_history_body {4363# Warning: assumes constant type (blob or tree) during history4364my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;43654366$from=0unlessdefined$from;4367$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});43684369print"<table class=\"history\">\n";4370my$alternate=1;4371for(my$i=$from;$i<=$to;$i++) {4372my%co= %{$commitlist->[$i]};4373if(!%co) {4374next;4375}4376my$commit=$co{'id'};43774378my$ref= format_ref_marker($refs,$commit);43794380if($alternate) {4381print"<tr class=\"dark\">\n";4382}else{4383print"<tr class=\"light\">\n";4384}4385$alternate^=1;4386print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4387# shortlog: format_author_html('td', \%co, 10)4388 format_author_html('td', \%co,15,3) ."<td>";4389# originally git_history used chop_str($co{'title'}, 50)4390print format_subject_html($co{'title'},$co{'title_short'},4391 href(action=>"commit", hash=>$commit),$ref);4392print"</td>\n".4393"<td class=\"link\">".4394$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4395$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");43964397if($ftypeeq'blob') {4398my$blob_current= git_get_hash_by_path($hash_base,$file_name);4399my$blob_parent= git_get_hash_by_path($commit,$file_name);4400if(defined$blob_current&&defined$blob_parent&&4401$blob_currentne$blob_parent) {4402print" | ".4403$cgi->a({-href => href(action=>"blobdiff",4404 hash=>$blob_current, hash_parent=>$blob_parent,4405 hash_base=>$hash_base, hash_parent_base=>$commit,4406 file_name=>$file_name)},4407"diff to current");4408}4409}4410print"</td>\n".4411"</tr>\n";4412}4413if(defined$extra) {4414print"<tr>\n".4415"<td colspan=\"4\">$extra</td>\n".4416"</tr>\n";4417}4418print"</table>\n";4419}44204421sub git_tags_body {4422# uses global variable $project4423my($taglist,$from,$to,$extra) =@_;4424$from=0unlessdefined$from;4425$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);44264427print"<table class=\"tags\">\n";4428my$alternate=1;4429for(my$i=$from;$i<=$to;$i++) {4430my$entry=$taglist->[$i];4431my%tag=%$entry;4432my$comment=$tag{'subject'};4433my$comment_short;4434if(defined$comment) {4435$comment_short= chop_str($comment,30,5);4436}4437if($alternate) {4438print"<tr class=\"dark\">\n";4439}else{4440print"<tr class=\"light\">\n";4441}4442$alternate^=1;4443if(defined$tag{'age'}) {4444print"<td><i>$tag{'age'}</i></td>\n";4445}else{4446print"<td></td>\n";4447}4448print"<td>".4449$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4450-class=>"list name"}, esc_html($tag{'name'})) .4451"</td>\n".4452"<td>";4453if(defined$comment) {4454print format_subject_html($comment,$comment_short,4455 href(action=>"tag", hash=>$tag{'id'}));4456}4457print"</td>\n".4458"<td class=\"selflink\">";4459if($tag{'type'}eq"tag") {4460print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4461}else{4462print" ";4463}4464print"</td>\n".4465"<td class=\"link\">"." | ".4466$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4467if($tag{'reftype'}eq"commit") {4468print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4469" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4470}elsif($tag{'reftype'}eq"blob") {4471print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4472}4473print"</td>\n".4474"</tr>";4475}4476if(defined$extra) {4477print"<tr>\n".4478"<td colspan=\"5\">$extra</td>\n".4479"</tr>\n";4480}4481print"</table>\n";4482}44834484sub git_heads_body {4485# uses global variable $project4486my($headlist,$head,$from,$to,$extra) =@_;4487$from=0unlessdefined$from;4488$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);44894490print"<table class=\"heads\">\n";4491my$alternate=1;4492for(my$i=$from;$i<=$to;$i++) {4493my$entry=$headlist->[$i];4494my%ref=%$entry;4495my$curr=$ref{'id'}eq$head;4496if($alternate) {4497print"<tr class=\"dark\">\n";4498}else{4499print"<tr class=\"light\">\n";4500}4501$alternate^=1;4502print"<td><i>$ref{'age'}</i></td>\n".4503($curr?"<td class=\"current_head\">":"<td>") .4504$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4505-class=>"list name"},esc_html($ref{'name'})) .4506"</td>\n".4507"<td class=\"link\">".4508$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4509$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4510$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4511"</td>\n".4512"</tr>";4513}4514if(defined$extra) {4515print"<tr>\n".4516"<td colspan=\"3\">$extra</td>\n".4517"</tr>\n";4518}4519print"</table>\n";4520}45214522sub git_search_grep_body {4523my($commitlist,$from,$to,$extra) =@_;4524$from=0unlessdefined$from;4525$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45264527print"<table class=\"commit_search\">\n";4528my$alternate=1;4529for(my$i=$from;$i<=$to;$i++) {4530my%co= %{$commitlist->[$i]};4531if(!%co) {4532next;4533}4534my$commit=$co{'id'};4535if($alternate) {4536print"<tr class=\"dark\">\n";4537}else{4538print"<tr class=\"light\">\n";4539}4540$alternate^=1;4541print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4542 format_author_html('td', \%co,15,5) .4543"<td>".4544$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4545-class=>"list subject"},4546 chop_and_escape_str($co{'title'},50) ."<br/>");4547my$comment=$co{'comment'};4548foreachmy$line(@$comment) {4549if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4550my($lead,$match,$trail) = ($1,$2,$3);4551$match= chop_str($match,70,5,'center');4552my$contextlen=int((80-length($match))/2);4553$contextlen=30if($contextlen>30);4554$lead= chop_str($lead,$contextlen,10,'left');4555$trail= chop_str($trail,$contextlen,10,'right');45564557$lead= esc_html($lead);4558$match= esc_html($match);4559$trail= esc_html($trail);45604561print"$lead<span class=\"match\">$match</span>$trail<br />";4562}4563}4564print"</td>\n".4565"<td class=\"link\">".4566$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4567" | ".4568$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4569" | ".4570$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4571print"</td>\n".4572"</tr>\n";4573}4574if(defined$extra) {4575print"<tr>\n".4576"<td colspan=\"3\">$extra</td>\n".4577"</tr>\n";4578}4579print"</table>\n";4580}45814582## ======================================================================4583## ======================================================================4584## actions45854586sub git_project_list {4587my$order=$input_params{'order'};4588if(defined$order&&$order!~m/none|project|descr|owner|age/) {4589 die_error(400,"Unknown order parameter");4590}45914592my@list= git_get_projects_list();4593if(!@list) {4594 die_error(404,"No projects found");4595}45964597 git_header_html();4598if(-f $home_text) {4599print"<div class=\"index_include\">\n";4600 insert_file($home_text);4601print"</div>\n";4602}4603print$cgi->startform(-method=>"get") .4604"<p class=\"projsearch\">Search:\n".4605$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4606"</p>".4607$cgi->end_form() ."\n";4608 git_project_list_body(\@list,$order);4609 git_footer_html();4610}46114612sub git_forks {4613my$order=$input_params{'order'};4614if(defined$order&&$order!~m/none|project|descr|owner|age/) {4615 die_error(400,"Unknown order parameter");4616}46174618my@list= git_get_projects_list($project);4619if(!@list) {4620 die_error(404,"No forks found");4621}46224623 git_header_html();4624 git_print_page_nav('','');4625 git_print_header_div('summary',"$projectforks");4626 git_project_list_body(\@list,$order);4627 git_footer_html();4628}46294630sub git_project_index {4631my@projects= git_get_projects_list($project);46324633print$cgi->header(4634-type =>'text/plain',4635-charset =>'utf-8',4636-content_disposition =>'inline; filename="index.aux"');46374638foreachmy$pr(@projects) {4639if(!exists$pr->{'owner'}) {4640$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4641}46424643my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4644# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4645$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4646$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4647$path=~s/ /\+/g;4648$owner=~s/ /\+/g;46494650print"$path$owner\n";4651}4652}46534654sub git_summary {4655my$descr= git_get_project_description($project) ||"none";4656my%co= parse_commit("HEAD");4657my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4658my$head=$co{'id'};46594660my$owner= git_get_project_owner($project);46614662my$refs= git_get_references();4663# These get_*_list functions return one more to allow us to see if4664# there are more ...4665my@taglist= git_get_tags_list(16);4666my@headlist= git_get_heads_list(16);4667my@forklist;4668my$check_forks= gitweb_check_feature('forks');46694670if($check_forks) {4671@forklist= git_get_projects_list($project);4672}46734674 git_header_html();4675 git_print_page_nav('summary','',$head);46764677print"<div class=\"title\"> </div>\n";4678print"<table class=\"projects_list\">\n".4679"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4680"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4681if(defined$cd{'rfc2822'}) {4682print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4683}46844685# use per project git URL list in $projectroot/$project/cloneurl4686# or make project git URL from git base URL and project name4687my$url_tag="URL";4688my@url_list= git_get_project_url_list($project);4689@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4690foreachmy$git_url(@url_list) {4691next unless$git_url;4692print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4693$url_tag="";4694}46954696# Tag cloud4697my$show_ctags= gitweb_check_feature('ctags');4698if($show_ctags) {4699my$ctags= git_get_project_ctags($project);4700my$cloud= git_populate_project_tagcloud($ctags);4701print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4702print"</td>\n<td>"unless%$ctags;4703print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4704print"</td>\n<td>"if%$ctags;4705print git_show_project_tagcloud($cloud,48);4706print"</td></tr>";4707}47084709print"</table>\n";47104711# If XSS prevention is on, we don't include README.html.4712# TODO: Allow a readme in some safe format.4713if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4714print"<div class=\"title\">readme</div>\n".4715"<div class=\"readme\">\n";4716 insert_file("$projectroot/$project/README.html");4717print"\n</div>\n";# class="readme"4718}47194720# we need to request one more than 16 (0..15) to check if4721# those 16 are all4722my@commitlist=$head? parse_commits($head,17) : ();4723if(@commitlist) {4724 git_print_header_div('shortlog');4725 git_shortlog_body(\@commitlist,0,15,$refs,4726$#commitlist<=15?undef:4727$cgi->a({-href => href(action=>"shortlog")},"..."));4728}47294730if(@taglist) {4731 git_print_header_div('tags');4732 git_tags_body(\@taglist,0,15,4733$#taglist<=15?undef:4734$cgi->a({-href => href(action=>"tags")},"..."));4735}47364737if(@headlist) {4738 git_print_header_div('heads');4739 git_heads_body(\@headlist,$head,0,15,4740$#headlist<=15?undef:4741$cgi->a({-href => href(action=>"heads")},"..."));4742}47434744if(@forklist) {4745 git_print_header_div('forks');4746 git_project_list_body(\@forklist,'age',0,15,4747$#forklist<=15?undef:4748$cgi->a({-href => href(action=>"forks")},"..."),4749'no_header');4750}47514752 git_footer_html();4753}47544755sub git_tag {4756my$head= git_get_head_hash($project);4757 git_header_html();4758 git_print_page_nav('','',$head,undef,$head);4759my%tag= parse_tag($hash);47604761if(!%tag) {4762 die_error(404,"Unknown tag object");4763}47644765 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4766print"<div class=\"title_text\">\n".4767"<table class=\"object_header\">\n".4768"<tr>\n".4769"<td>object</td>\n".4770"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4771$tag{'object'}) ."</td>\n".4772"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4773$tag{'type'}) ."</td>\n".4774"</tr>\n";4775if(defined($tag{'author'})) {4776 git_print_authorship_rows(\%tag,'author');4777}4778print"</table>\n\n".4779"</div>\n";4780print"<div class=\"page_body\">";4781my$comment=$tag{'comment'};4782foreachmy$line(@$comment) {4783chomp$line;4784print esc_html($line, -nbsp=>1) ."<br/>\n";4785}4786print"</div>\n";4787 git_footer_html();4788}47894790sub git_blame {4791# permissions4792 gitweb_check_feature('blame')4793or die_error(403,"Blame view not allowed");47944795# error checking4796 die_error(400,"No file name given")unless$file_name;4797$hash_base||= git_get_head_hash($project);4798 die_error(404,"Couldn't find base commit")unless$hash_base;4799my%co= parse_commit($hash_base)4800or die_error(404,"Commit not found");4801my$ftype="blob";4802if(!defined$hash) {4803$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4804or die_error(404,"Error looking up file");4805}else{4806$ftype= git_get_type($hash);4807if($ftype!~"blob") {4808 die_error(400,"Object is not a blob");4809}4810}48114812# run git-blame --porcelain4813open my$fd,"-|", git_cmd(),"blame",'-p',4814$hash_base,'--',$file_name4815or die_error(500,"Open git-blame failed");48164817# page header4818 git_header_html();4819my$formats_nav=4820$cgi->a({-href => href(action=>"blob", -replay=>1)},4821"blob") .4822" | ".4823$cgi->a({-href => href(action=>"history", -replay=>1)},4824"history") .4825" | ".4826$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4827"HEAD");4828 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4829 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4830 git_print_page_path($file_name,$ftype,$hash_base);48314832# page body4833my@rev_color=qw(light dark);4834my$num_colors=scalar(@rev_color);4835my$current_color=0;4836my%metainfo= ();48374838print<<HTML;4839<div class="page_body">4840<table class="blame">4841<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4842HTML4843 LINE:4844while(my$line= <$fd>) {4845chomp$line;4846# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4847# no <lines in group> for subsequent lines in group of lines4848my($full_rev,$orig_lineno,$lineno,$group_size) =4849($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4850if(!exists$metainfo{$full_rev}) {4851$metainfo{$full_rev} = {'nprevious'=>0};4852}4853my$meta=$metainfo{$full_rev};4854my$data;4855while($data= <$fd>) {4856chomp$data;4857last if($data=~s/^\t//);# contents of line4858if($data=~/^(\S+)(?: (.*))?$/) {4859$meta->{$1} =$2unlessexists$meta->{$1};4860}4861if($data=~/^previous /) {4862$meta->{'nprevious'}++;4863}4864}4865my$short_rev=substr($full_rev,0,8);4866my$author=$meta->{'author'};4867my%date=4868 parse_date($meta->{'author-time'},$meta->{'author-tz'});4869my$date=$date{'iso-tz'};4870if($group_size) {4871$current_color= ($current_color+1) %$num_colors;4872}4873my$tr_class=$rev_color[$current_color];4874$tr_class.=' boundary'if(exists$meta->{'boundary'});4875$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);4876$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);4877print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";4878if($group_size) {4879print"<td class=\"sha1\"";4880print" title=\"". esc_html($author) .",$date\"";4881print" rowspan=\"$group_size\""if($group_size>1);4882print">";4883print$cgi->a({-href => href(action=>"commit",4884 hash=>$full_rev,4885 file_name=>$file_name)},4886 esc_html($short_rev));4887if($group_size>=2) {4888my@author_initials= ($author=~/\b([[:upper:]])\B/g);4889if(@author_initials) {4890print"<br />".4891 esc_html(join('',@author_initials));4892# or join('.', ...)4893}4894}4895print"</td>\n";4896}4897# 'previous' <sha1 of parent commit> <filename at commit>4898if(exists$meta->{'previous'} &&4899$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {4900$meta->{'parent'} =$1;4901$meta->{'file_parent'} = unquote($2);4902}4903my$linenr_commit=4904exists($meta->{'parent'}) ?4905$meta->{'parent'} :$full_rev;4906my$linenr_filename=4907exists($meta->{'file_parent'}) ?4908$meta->{'file_parent'} : unquote($meta->{'filename'});4909my$blamed= href(action =>'blame',4910 file_name =>$linenr_filename,4911 hash_base =>$linenr_commit);4912print"<td class=\"linenr\">";4913print$cgi->a({ -href =>"$blamed#l$orig_lineno",4914-class=>"linenr"},4915 esc_html($lineno));4916print"</td>";4917print"<td class=\"pre\">". esc_html($data) ."</td>\n";4918print"</tr>\n";4919}4920print"</table>\n";4921print"</div>";4922close$fd4923or print"Reading blob failed\n";49244925# page footer4926 git_footer_html();4927}49284929sub git_tags {4930my$head= git_get_head_hash($project);4931 git_header_html();4932 git_print_page_nav('','',$head,undef,$head);4933 git_print_header_div('summary',$project);49344935my@tagslist= git_get_tags_list();4936if(@tagslist) {4937 git_tags_body(\@tagslist);4938}4939 git_footer_html();4940}49414942sub git_heads {4943my$head= git_get_head_hash($project);4944 git_header_html();4945 git_print_page_nav('','',$head,undef,$head);4946 git_print_header_div('summary',$project);49474948my@headslist= git_get_heads_list();4949if(@headslist) {4950 git_heads_body(\@headslist,$head);4951}4952 git_footer_html();4953}49544955sub git_blob_plain {4956my$type=shift;4957my$expires;49584959if(!defined$hash) {4960if(defined$file_name) {4961my$base=$hash_base|| git_get_head_hash($project);4962$hash= git_get_hash_by_path($base,$file_name,"blob")4963or die_error(404,"Cannot find file");4964}else{4965 die_error(400,"No file name defined");4966}4967}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4968# blobs defined by non-textual hash id's can be cached4969$expires="+1d";4970}49714972open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4973or die_error(500,"Open git-cat-file blob '$hash' failed");49744975# content-type (can include charset)4976$type= blob_contenttype($fd,$file_name,$type);49774978# "save as" filename, even when no $file_name is given4979my$save_as="$hash";4980if(defined$file_name) {4981$save_as=$file_name;4982}elsif($type=~m/^text\//) {4983$save_as.='.txt';4984}49854986# With XSS prevention on, blobs of all types except a few known safe4987# ones are served with "Content-Disposition: attachment" to make sure4988# they don't run in our security domain. For certain image types,4989# blob view writes an <img> tag referring to blob_plain view, and we4990# want to be sure not to break that by serving the image as an4991# attachment (though Firefox 3 doesn't seem to care).4992my$sandbox=$prevent_xss&&4993$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;49944995print$cgi->header(4996-type =>$type,4997-expires =>$expires,4998-content_disposition =>4999($sandbox?'attachment':'inline')5000.'; filename="'.$save_as.'"');5001local$/=undef;5002binmode STDOUT,':raw';5003print<$fd>;5004binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5005close$fd;5006}50075008sub git_blob {5009my$expires;50105011if(!defined$hash) {5012if(defined$file_name) {5013my$base=$hash_base|| git_get_head_hash($project);5014$hash= git_get_hash_by_path($base,$file_name,"blob")5015or die_error(404,"Cannot find file");5016}else{5017 die_error(400,"No file name defined");5018}5019}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5020# blobs defined by non-textual hash id's can be cached5021$expires="+1d";5022}50235024my$have_blame= gitweb_check_feature('blame');5025open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5026or die_error(500,"Couldn't cat$file_name,$hash");5027my$mimetype= blob_mimetype($fd,$file_name);5028if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5029close$fd;5030return git_blob_plain($mimetype);5031}5032# we can have blame only for text/* mimetype5033$have_blame&&= ($mimetype=~m!^text/!);50345035 git_header_html(undef,$expires);5036my$formats_nav='';5037if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5038if(defined$file_name) {5039if($have_blame) {5040$formats_nav.=5041$cgi->a({-href => href(action=>"blame", -replay=>1)},5042"blame") .5043" | ";5044}5045$formats_nav.=5046$cgi->a({-href => href(action=>"history", -replay=>1)},5047"history") .5048" | ".5049$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5050"raw") .5051" | ".5052$cgi->a({-href => href(action=>"blob",5053 hash_base=>"HEAD", file_name=>$file_name)},5054"HEAD");5055}else{5056$formats_nav.=5057$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5058"raw");5059}5060 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5061 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5062}else{5063print"<div class=\"page_nav\">\n".5064"<br/><br/></div>\n".5065"<div class=\"title\">$hash</div>\n";5066}5067 git_print_page_path($file_name,"blob",$hash_base);5068print"<div class=\"page_body\">\n";5069if($mimetype=~m!^image/!) {5070print qq!<img type="$mimetype"!;5071if($file_name) {5072print qq! alt="$file_name" title="$file_name"!;5073}5074print qq! src="! .5075 href(action=>"blob_plain", hash=>$hash,5076 hash_base=>$hash_base, file_name=>$file_name) .5077 qq!"/>\n!;5078}else{5079my$nr;5080while(my$line= <$fd>) {5081chomp$line;5082$nr++;5083$line= untabify($line);5084printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5085$nr,$nr,$nr, esc_html($line, -nbsp=>1);5086}5087}5088close$fd5089or print"Reading blob failed.\n";5090print"</div>";5091 git_footer_html();5092}50935094sub git_tree {5095if(!defined$hash_base) {5096$hash_base="HEAD";5097}5098if(!defined$hash) {5099if(defined$file_name) {5100$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5101}else{5102$hash=$hash_base;5103}5104}5105 die_error(404,"No such tree")unlessdefined($hash);51065107my@entries= ();5108{5109local$/="\0";5110open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash5111or die_error(500,"Open git-ls-tree failed");5112@entries=map{chomp;$_} <$fd>;5113close$fd5114or die_error(404,"Reading tree failed");5115}51165117my$refs= git_get_references();5118my$ref= format_ref_marker($refs,$hash_base);5119 git_header_html();5120my$basedir='';5121my$have_blame= gitweb_check_feature('blame');5122if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5123my@views_nav= ();5124if(defined$file_name) {5125push@views_nav,5126$cgi->a({-href => href(action=>"history", -replay=>1)},5127"history"),5128$cgi->a({-href => href(action=>"tree",5129 hash_base=>"HEAD", file_name=>$file_name)},5130"HEAD"),5131}5132my$snapshot_links= format_snapshot_links($hash);5133if(defined$snapshot_links) {5134# FIXME: Should be available when we have no hash base as well.5135push@views_nav,$snapshot_links;5136}5137 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));5138 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5139}else{5140undef$hash_base;5141print"<div class=\"page_nav\">\n";5142print"<br/><br/></div>\n";5143print"<div class=\"title\">$hash</div>\n";5144}5145if(defined$file_name) {5146$basedir=$file_name;5147if($basedirne''&&substr($basedir, -1)ne'/') {5148$basedir.='/';5149}5150 git_print_page_path($file_name,'tree',$hash_base);5151}5152print"<div class=\"page_body\">\n";5153print"<table class=\"tree\">\n";5154my$alternate=1;5155# '..' (top directory) link if possible5156if(defined$hash_base&&5157defined$file_name&&$file_name=~m![^/]+$!) {5158if($alternate) {5159print"<tr class=\"dark\">\n";5160}else{5161print"<tr class=\"light\">\n";5162}5163$alternate^=1;51645165my$up=$file_name;5166$up=~s!/?[^/]+$!!;5167undef$upunless$up;5168# based on git_print_tree_entry5169print'<td class="mode">'. mode_str('040000') ."</td>\n";5170print'<td class="list">';5171print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,5172 file_name=>$up)},5173"..");5174print"</td>\n";5175print"<td class=\"link\"></td>\n";51765177print"</tr>\n";5178}5179foreachmy$line(@entries) {5180my%t= parse_ls_tree_line($line, -z =>1);51815182if($alternate) {5183print"<tr class=\"dark\">\n";5184}else{5185print"<tr class=\"light\">\n";5186}5187$alternate^=1;51885189 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);51905191print"</tr>\n";5192}5193print"</table>\n".5194"</div>";5195 git_footer_html();5196}51975198sub git_snapshot {5199my$format=$input_params{'snapshot_format'};5200if(!@snapshot_fmts) {5201 die_error(403,"Snapshots not allowed");5202}5203# default to first supported snapshot format5204$format||=$snapshot_fmts[0];5205if($format!~m/^[a-z0-9]+$/) {5206 die_error(400,"Invalid snapshot format parameter");5207}elsif(!exists($known_snapshot_formats{$format})) {5208 die_error(400,"Unknown snapshot format");5209}elsif(!grep($_eq$format,@snapshot_fmts)) {5210 die_error(403,"Unsupported snapshot format");5211}52125213if(!defined$hash) {5214$hash= git_get_head_hash($project);5215}52165217my$name=$project;5218$name=~ s,([^/])/*\.git$,$1,;5219$name= basename($name);5220my$filename= to_utf8($name);5221$name=~s/\047/\047\\\047\047/g;5222my$cmd;5223$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5224$cmd= quote_command(5225 git_cmd(),'archive',5226"--format=$known_snapshot_formats{$format}{'format'}",5227"--prefix=$name/",$hash);5228if(exists$known_snapshot_formats{$format}{'compressor'}) {5229$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5230}52315232print$cgi->header(5233-type =>$known_snapshot_formats{$format}{'type'},5234-content_disposition =>'inline; filename="'."$filename".'"',5235-status =>'200 OK');52365237open my$fd,"-|",$cmd5238or die_error(500,"Execute git-archive failed");5239binmode STDOUT,':raw';5240print<$fd>;5241binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5242close$fd;5243}52445245sub git_log {5246my$head= git_get_head_hash($project);5247if(!defined$hash) {5248$hash=$head;5249}5250if(!defined$page) {5251$page=0;5252}5253my$refs= git_get_references();52545255my@commitlist= parse_commits($hash,101, (100*$page));52565257my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);52585259my($patch_max) = gitweb_get_feature('patches');5260if($patch_max) {5261if($patch_max<0||@commitlist<=$patch_max) {5262$paging_nav.=" ⋅ ".5263$cgi->a({-href => href(action=>"patches", -replay=>1)},5264"patches");5265}5266}52675268 git_header_html();5269 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);52705271if(!@commitlist) {5272my%co= parse_commit($hash);52735274 git_print_header_div('summary',$project);5275print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5276}5277my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5278for(my$i=0;$i<=$to;$i++) {5279my%co= %{$commitlist[$i]};5280next if!%co;5281my$commit=$co{'id'};5282my$ref= format_ref_marker($refs,$commit);5283my%ad= parse_date($co{'author_epoch'});5284 git_print_header_div('commit',5285"<span class=\"age\">$co{'age_string'}</span>".5286 esc_html($co{'title'}) .$ref,5287$commit);5288print"<div class=\"title_text\">\n".5289"<div class=\"log_link\">\n".5290$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5291" | ".5292$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5293" | ".5294$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5295"<br/>\n".5296"</div>\n";5297 git_print_authorship(\%co, -tag =>'span');5298print"<br/>\n</div>\n";52995300print"<div class=\"log_body\">\n";5301 git_print_log($co{'comment'}, -final_empty_line=>1);5302print"</div>\n";5303}5304if($#commitlist>=100) {5305print"<div class=\"page_nav\">\n";5306print$cgi->a({-href => href(-replay=>1, page=>$page+1),5307-accesskey =>"n", -title =>"Alt-n"},"next");5308print"</div>\n";5309}5310 git_footer_html();5311}53125313sub git_commit {5314$hash||=$hash_base||"HEAD";5315my%co= parse_commit($hash)5316or die_error(404,"Unknown commit object");53175318my$parent=$co{'parent'};5319my$parents=$co{'parents'};# listref53205321# we need to prepare $formats_nav before any parameter munging5322my$formats_nav;5323if(!defined$parent) {5324# --root commitdiff5325$formats_nav.='(initial)';5326}elsif(@$parents==1) {5327# single parent commit5328$formats_nav.=5329'(parent: '.5330$cgi->a({-href => href(action=>"commit",5331 hash=>$parent)},5332 esc_html(substr($parent,0,7))) .5333')';5334}else{5335# merge commit5336$formats_nav.=5337'(merge: '.5338join(' ',map{5339$cgi->a({-href => href(action=>"commit",5340 hash=>$_)},5341 esc_html(substr($_,0,7)));5342}@$parents) .5343')';5344}5345if(gitweb_check_feature('patches')) {5346$formats_nav.=" | ".5347$cgi->a({-href => href(action=>"patch", -replay=>1)},5348"patch");5349}53505351if(!defined$parent) {5352$parent="--root";5353}5354my@difftree;5355open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5356@diff_opts,5357(@$parents<=1?$parent:'-c'),5358$hash,"--"5359or die_error(500,"Open git-diff-tree failed");5360@difftree=map{chomp;$_} <$fd>;5361close$fdor die_error(404,"Reading git-diff-tree failed");53625363# non-textual hash id's can be cached5364my$expires;5365if($hash=~m/^[0-9a-fA-F]{40}$/) {5366$expires="+1d";5367}5368my$refs= git_get_references();5369my$ref= format_ref_marker($refs,$co{'id'});53705371 git_header_html(undef,$expires);5372 git_print_page_nav('commit','',5373$hash,$co{'tree'},$hash,5374$formats_nav);53755376if(defined$co{'parent'}) {5377 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5378}else{5379 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5380}5381print"<div class=\"title_text\">\n".5382"<table class=\"object_header\">\n";5383 git_print_authorship_rows(\%co);5384print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5385print"<tr>".5386"<td>tree</td>".5387"<td class=\"sha1\">".5388$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5389class=>"list"},$co{'tree'}) .5390"</td>".5391"<td class=\"link\">".5392$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5393"tree");5394my$snapshot_links= format_snapshot_links($hash);5395if(defined$snapshot_links) {5396print" | ".$snapshot_links;5397}5398print"</td>".5399"</tr>\n";54005401foreachmy$par(@$parents) {5402print"<tr>".5403"<td>parent</td>".5404"<td class=\"sha1\">".5405$cgi->a({-href => href(action=>"commit", hash=>$par),5406class=>"list"},$par) .5407"</td>".5408"<td class=\"link\">".5409$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5410" | ".5411$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5412"</td>".5413"</tr>\n";5414}5415print"</table>".5416"</div>\n";54175418print"<div class=\"page_body\">\n";5419 git_print_log($co{'comment'});5420print"</div>\n";54215422 git_difftree_body(\@difftree,$hash,@$parents);54235424 git_footer_html();5425}54265427sub git_object {5428# object is defined by:5429# - hash or hash_base alone5430# - hash_base and file_name5431my$type;54325433# - hash or hash_base alone5434if($hash|| ($hash_base&& !defined$file_name)) {5435my$object_id=$hash||$hash_base;54365437open my$fd,"-|", quote_command(5438 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5439or die_error(404,"Object does not exist");5440$type= <$fd>;5441chomp$type;5442close$fd5443or die_error(404,"Object does not exist");54445445# - hash_base and file_name5446}elsif($hash_base&&defined$file_name) {5447$file_name=~ s,/+$,,;54485449system(git_cmd(),"cat-file",'-e',$hash_base) ==05450or die_error(404,"Base object does not exist");54515452# here errors should not hapen5453open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5454or die_error(500,"Open git-ls-tree failed");5455my$line= <$fd>;5456close$fd;54575458#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5459unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5460 die_error(404,"File or directory for given base does not exist");5461}5462$type=$2;5463$hash=$3;5464}else{5465 die_error(400,"Not enough information to find object");5466}54675468print$cgi->redirect(-uri => href(action=>$type, -full=>1,5469 hash=>$hash, hash_base=>$hash_base,5470 file_name=>$file_name),5471-status =>'302 Found');5472}54735474sub git_blobdiff {5475my$format=shift||'html';54765477my$fd;5478my@difftree;5479my%diffinfo;5480my$expires;54815482# preparing $fd and %diffinfo for git_patchset_body5483# new style URI5484if(defined$hash_base&&defined$hash_parent_base) {5485if(defined$file_name) {5486# read raw output5487open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5488$hash_parent_base,$hash_base,5489"--", (defined$file_parent?$file_parent: ()),$file_name5490or die_error(500,"Open git-diff-tree failed");5491@difftree=map{chomp;$_} <$fd>;5492close$fd5493or die_error(404,"Reading git-diff-tree failed");5494@difftree5495or die_error(404,"Blob diff not found");54965497}elsif(defined$hash&&5498$hash=~/[0-9a-fA-F]{40}/) {5499# try to find filename from $hash55005501# read filtered raw output5502open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5503$hash_parent_base,$hash_base,"--"5504or die_error(500,"Open git-diff-tree failed");5505@difftree=5506# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5507# $hash == to_id5508grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5509map{chomp;$_} <$fd>;5510close$fd5511or die_error(404,"Reading git-diff-tree failed");5512@difftree5513or die_error(404,"Blob diff not found");55145515}else{5516 die_error(400,"Missing one of the blob diff parameters");5517}55185519if(@difftree>1) {5520 die_error(400,"Ambiguous blob diff specification");5521}55225523%diffinfo= parse_difftree_raw_line($difftree[0]);5524$file_parent||=$diffinfo{'from_file'} ||$file_name;5525$file_name||=$diffinfo{'to_file'};55265527$hash_parent||=$diffinfo{'from_id'};5528$hash||=$diffinfo{'to_id'};55295530# non-textual hash id's can be cached5531if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5532$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5533$expires='+1d';5534}55355536# open patch output5537open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5538'-p', ($formateq'html'?"--full-index": ()),5539$hash_parent_base,$hash_base,5540"--", (defined$file_parent?$file_parent: ()),$file_name5541or die_error(500,"Open git-diff-tree failed");5542}55435544# old/legacy style URI -- not generated anymore since 1.4.3.5545if(!%diffinfo) {5546 die_error('404 Not Found',"Missing one of the blob diff parameters")5547}55485549# header5550if($formateq'html') {5551my$formats_nav=5552$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5553"raw");5554 git_header_html(undef,$expires);5555if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5556 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5557 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5558}else{5559print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5560print"<div class=\"title\">$hashvs$hash_parent</div>\n";5561}5562if(defined$file_name) {5563 git_print_page_path($file_name,"blob",$hash_base);5564}else{5565print"<div class=\"page_path\"></div>\n";5566}55675568}elsif($formateq'plain') {5569print$cgi->header(5570-type =>'text/plain',5571-charset =>'utf-8',5572-expires =>$expires,5573-content_disposition =>'inline; filename="'."$file_name".'.patch"');55745575print"X-Git-Url: ".$cgi->self_url() ."\n\n";55765577}else{5578 die_error(400,"Unknown blobdiff format");5579}55805581# patch5582if($formateq'html') {5583print"<div class=\"page_body\">\n";55845585 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5586close$fd;55875588print"</div>\n";# class="page_body"5589 git_footer_html();55905591}else{5592while(my$line= <$fd>) {5593$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5594$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;55955596print$line;55975598last if$line=~m!^\+\+\+!;5599}5600local$/=undef;5601print<$fd>;5602close$fd;5603}5604}56055606sub git_blobdiff_plain {5607 git_blobdiff('plain');5608}56095610sub git_commitdiff {5611my%params=@_;5612my$format=$params{-format} ||'html';56135614my($patch_max) = gitweb_get_feature('patches');5615if($formateq'patch') {5616 die_error(403,"Patch view not allowed")unless$patch_max;5617}56185619$hash||=$hash_base||"HEAD";5620my%co= parse_commit($hash)5621or die_error(404,"Unknown commit object");56225623# choose format for commitdiff for merge5624if(!defined$hash_parent&& @{$co{'parents'}} >1) {5625$hash_parent='--cc';5626}5627# we need to prepare $formats_nav before almost any parameter munging5628my$formats_nav;5629if($formateq'html') {5630$formats_nav=5631$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5632"raw");5633if($patch_max) {5634$formats_nav.=" | ".5635$cgi->a({-href => href(action=>"patch", -replay=>1)},5636"patch");5637}56385639if(defined$hash_parent&&5640$hash_parentne'-c'&&$hash_parentne'--cc') {5641# commitdiff with two commits given5642my$hash_parent_short=$hash_parent;5643if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5644$hash_parent_short=substr($hash_parent,0,7);5645}5646$formats_nav.=5647' (from';5648for(my$i=0;$i< @{$co{'parents'}};$i++) {5649if($co{'parents'}[$i]eq$hash_parent) {5650$formats_nav.=' parent '. ($i+1);5651last;5652}5653}5654$formats_nav.=': '.5655$cgi->a({-href => href(action=>"commitdiff",5656 hash=>$hash_parent)},5657 esc_html($hash_parent_short)) .5658')';5659}elsif(!$co{'parent'}) {5660# --root commitdiff5661$formats_nav.=' (initial)';5662}elsif(scalar@{$co{'parents'}} ==1) {5663# single parent commit5664$formats_nav.=5665' (parent: '.5666$cgi->a({-href => href(action=>"commitdiff",5667 hash=>$co{'parent'})},5668 esc_html(substr($co{'parent'},0,7))) .5669')';5670}else{5671# merge commit5672if($hash_parenteq'--cc') {5673$formats_nav.=' | '.5674$cgi->a({-href => href(action=>"commitdiff",5675 hash=>$hash, hash_parent=>'-c')},5676'combined');5677}else{# $hash_parent eq '-c'5678$formats_nav.=' | '.5679$cgi->a({-href => href(action=>"commitdiff",5680 hash=>$hash, hash_parent=>'--cc')},5681'compact');5682}5683$formats_nav.=5684' (merge: '.5685join(' ',map{5686$cgi->a({-href => href(action=>"commitdiff",5687 hash=>$_)},5688 esc_html(substr($_,0,7)));5689} @{$co{'parents'}} ) .5690')';5691}5692}56935694my$hash_parent_param=$hash_parent;5695if(!defined$hash_parent_param) {5696# --cc for multiple parents, --root for parentless5697$hash_parent_param=5698@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5699}57005701# read commitdiff5702my$fd;5703my@difftree;5704if($formateq'html') {5705open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5706"--no-commit-id","--patch-with-raw","--full-index",5707$hash_parent_param,$hash,"--"5708or die_error(500,"Open git-diff-tree failed");57095710while(my$line= <$fd>) {5711chomp$line;5712# empty line ends raw part of diff-tree output5713last unless$line;5714push@difftree,scalar parse_difftree_raw_line($line);5715}57165717}elsif($formateq'plain') {5718open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5719'-p',$hash_parent_param,$hash,"--"5720or die_error(500,"Open git-diff-tree failed");5721}elsif($formateq'patch') {5722# For commit ranges, we limit the output to the number of5723# patches specified in the 'patches' feature.5724# For single commits, we limit the output to a single patch,5725# diverging from the git-format-patch default.5726my@commit_spec= ();5727if($hash_parent) {5728if($patch_max>0) {5729push@commit_spec,"-$patch_max";5730}5731push@commit_spec,'-n',"$hash_parent..$hash";5732}else{5733if($params{-single}) {5734push@commit_spec,'-1';5735}else{5736if($patch_max>0) {5737push@commit_spec,"-$patch_max";5738}5739push@commit_spec,"-n";5740}5741push@commit_spec,'--root',$hash;5742}5743open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5744'--stdout',@commit_spec5745or die_error(500,"Open git-format-patch failed");5746}else{5747 die_error(400,"Unknown commitdiff format");5748}57495750# non-textual hash id's can be cached5751my$expires;5752if($hash=~m/^[0-9a-fA-F]{40}$/) {5753$expires="+1d";5754}57555756# write commit message5757if($formateq'html') {5758my$refs= git_get_references();5759my$ref= format_ref_marker($refs,$co{'id'});57605761 git_header_html(undef,$expires);5762 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5763 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5764print"<div class=\"title_text\">\n".5765"<table class=\"object_header\">\n";5766 git_print_authorship_rows(\%co);5767print"</table>".5768"</div>\n";5769print"<div class=\"page_body\">\n";5770if(@{$co{'comment'}} >1) {5771print"<div class=\"log\">\n";5772 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5773print"</div>\n";# class="log"5774}57755776}elsif($formateq'plain') {5777my$refs= git_get_references("tags");5778my$tagname= git_get_rev_name_tags($hash);5779my$filename= basename($project) ."-$hash.patch";57805781print$cgi->header(5782-type =>'text/plain',5783-charset =>'utf-8',5784-expires =>$expires,5785-content_disposition =>'inline; filename="'."$filename".'"');5786my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5787print"From: ". to_utf8($co{'author'}) ."\n";5788print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5789print"Subject: ". to_utf8($co{'title'}) ."\n";57905791print"X-Git-Tag:$tagname\n"if$tagname;5792print"X-Git-Url: ".$cgi->self_url() ."\n\n";57935794foreachmy$line(@{$co{'comment'}}) {5795print to_utf8($line) ."\n";5796}5797print"---\n\n";5798}elsif($formateq'patch') {5799my$filename= basename($project) ."-$hash.patch";58005801print$cgi->header(5802-type =>'text/plain',5803-charset =>'utf-8',5804-expires =>$expires,5805-content_disposition =>'inline; filename="'."$filename".'"');5806}58075808# write patch5809if($formateq'html') {5810my$use_parents= !defined$hash_parent||5811$hash_parenteq'-c'||$hash_parenteq'--cc';5812 git_difftree_body(\@difftree,$hash,5813$use_parents? @{$co{'parents'}} :$hash_parent);5814print"<br/>\n";58155816 git_patchset_body($fd, \@difftree,$hash,5817$use_parents? @{$co{'parents'}} :$hash_parent);5818close$fd;5819print"</div>\n";# class="page_body"5820 git_footer_html();58215822}elsif($formateq'plain') {5823local$/=undef;5824print<$fd>;5825close$fd5826or print"Reading git-diff-tree failed\n";5827}elsif($formateq'patch') {5828local$/=undef;5829print<$fd>;5830close$fd5831or print"Reading git-format-patch failed\n";5832}5833}58345835sub git_commitdiff_plain {5836 git_commitdiff(-format =>'plain');5837}58385839# format-patch-style patches5840sub git_patch {5841 git_commitdiff(-format =>'patch', -single=>1);5842}58435844sub git_patches {5845 git_commitdiff(-format =>'patch');5846}58475848sub git_history {5849if(!defined$hash_base) {5850$hash_base= git_get_head_hash($project);5851}5852if(!defined$page) {5853$page=0;5854}5855my$ftype;5856my%co= parse_commit($hash_base)5857or die_error(404,"Unknown commit object");58585859my$refs= git_get_references();5860my$limit=sprintf("--max-count=%i", (100* ($page+1)));58615862my@commitlist= parse_commits($hash_base,101, (100*$page),5863$file_name,"--full-history")5864or die_error(404,"No such file or directory on given branch");58655866if(!defined$hash&&defined$file_name) {5867# some commits could have deleted file in question,5868# and not have it in tree, but one of them has to have it5869for(my$i=0;$i<=@commitlist;$i++) {5870$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5871last ifdefined$hash;5872}5873}5874if(defined$hash) {5875$ftype= git_get_type($hash);5876}5877if(!defined$ftype) {5878 die_error(500,"Unknown type of object");5879}58805881my$paging_nav='';5882if($page>0) {5883$paging_nav.=5884$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5885 file_name=>$file_name)},5886"first");5887$paging_nav.=" ⋅ ".5888$cgi->a({-href => href(-replay=>1, page=>$page-1),5889-accesskey =>"p", -title =>"Alt-p"},"prev");5890}else{5891$paging_nav.="first";5892$paging_nav.=" ⋅ prev";5893}5894my$next_link='';5895if($#commitlist>=100) {5896$next_link=5897$cgi->a({-href => href(-replay=>1, page=>$page+1),5898-accesskey =>"n", -title =>"Alt-n"},"next");5899$paging_nav.=" ⋅$next_link";5900}else{5901$paging_nav.=" ⋅ next";5902}59035904 git_header_html();5905 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5906 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5907 git_print_page_path($file_name,$ftype,$hash_base);59085909 git_history_body(\@commitlist,0,99,5910$refs,$hash_base,$ftype,$next_link);59115912 git_footer_html();5913}59145915sub git_search {5916 gitweb_check_feature('search')or die_error(403,"Search is disabled");5917if(!defined$searchtext) {5918 die_error(400,"Text field is empty");5919}5920if(!defined$hash) {5921$hash= git_get_head_hash($project);5922}5923my%co= parse_commit($hash);5924if(!%co) {5925 die_error(404,"Unknown commit object");5926}5927if(!defined$page) {5928$page=0;5929}59305931$searchtype||='commit';5932if($searchtypeeq'pickaxe') {5933# pickaxe may take all resources of your box and run for several minutes5934# with every query - so decide by yourself how public you make this feature5935 gitweb_check_feature('pickaxe')5936or die_error(403,"Pickaxe is disabled");5937}5938if($searchtypeeq'grep') {5939 gitweb_check_feature('grep')5940or die_error(403,"Grep is disabled");5941}59425943 git_header_html();59445945if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5946my$greptype;5947if($searchtypeeq'commit') {5948$greptype="--grep=";5949}elsif($searchtypeeq'author') {5950$greptype="--author=";5951}elsif($searchtypeeq'committer') {5952$greptype="--committer=";5953}5954$greptype.=$searchtext;5955my@commitlist= parse_commits($hash,101, (100*$page),undef,5956$greptype,'--regexp-ignore-case',5957$search_use_regexp?'--extended-regexp':'--fixed-strings');59585959my$paging_nav='';5960if($page>0) {5961$paging_nav.=5962$cgi->a({-href => href(action=>"search", hash=>$hash,5963 searchtext=>$searchtext,5964 searchtype=>$searchtype)},5965"first");5966$paging_nav.=" ⋅ ".5967$cgi->a({-href => href(-replay=>1, page=>$page-1),5968-accesskey =>"p", -title =>"Alt-p"},"prev");5969}else{5970$paging_nav.="first";5971$paging_nav.=" ⋅ prev";5972}5973my$next_link='';5974if($#commitlist>=100) {5975$next_link=5976$cgi->a({-href => href(-replay=>1, page=>$page+1),5977-accesskey =>"n", -title =>"Alt-n"},"next");5978$paging_nav.=" ⋅$next_link";5979}else{5980$paging_nav.=" ⋅ next";5981}59825983if($#commitlist>=100) {5984}59855986 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5987 git_print_header_div('commit', esc_html($co{'title'}),$hash);5988 git_search_grep_body(\@commitlist,0,99,$next_link);5989}59905991if($searchtypeeq'pickaxe') {5992 git_print_page_nav('','',$hash,$co{'tree'},$hash);5993 git_print_header_div('commit', esc_html($co{'title'}),$hash);59945995print"<table class=\"pickaxe search\">\n";5996my$alternate=1;5997local$/="\n";5998open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5999'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6000($search_use_regexp?'--pickaxe-regex': ());6001undef%co;6002my@files;6003while(my$line= <$fd>) {6004chomp$line;6005next unless$line;60066007my%set= parse_difftree_raw_line($line);6008if(defined$set{'commit'}) {6009# finish previous commit6010if(%co) {6011print"</td>\n".6012"<td class=\"link\">".6013$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6014" | ".6015$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6016print"</td>\n".6017"</tr>\n";6018}60196020if($alternate) {6021print"<tr class=\"dark\">\n";6022}else{6023print"<tr class=\"light\">\n";6024}6025$alternate^=1;6026%co= parse_commit($set{'commit'});6027my$author= chop_and_escape_str($co{'author_name'},15,5);6028print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6029"<td><i>$author</i></td>\n".6030"<td>".6031$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6032-class=>"list subject"},6033 chop_and_escape_str($co{'title'},50) ."<br/>");6034}elsif(defined$set{'to_id'}) {6035next if($set{'to_id'} =~m/^0{40}$/);60366037print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6038 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6039-class=>"list"},6040"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6041"<br/>\n";6042}6043}6044close$fd;60456046# finish last commit (warning: repetition!)6047if(%co) {6048print"</td>\n".6049"<td class=\"link\">".6050$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6051" | ".6052$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6053print"</td>\n".6054"</tr>\n";6055}60566057print"</table>\n";6058}60596060if($searchtypeeq'grep') {6061 git_print_page_nav('','',$hash,$co{'tree'},$hash);6062 git_print_header_div('commit', esc_html($co{'title'}),$hash);60636064print"<table class=\"grep_search\">\n";6065my$alternate=1;6066my$matches=0;6067local$/="\n";6068open my$fd,"-|", git_cmd(),'grep','-n',6069$search_use_regexp? ('-E','-i') :'-F',6070$searchtext,$co{'tree'};6071my$lastfile='';6072while(my$line= <$fd>) {6073chomp$line;6074my($file,$lno,$ltext,$binary);6075last if($matches++>1000);6076if($line=~/^Binary file (.+) matches$/) {6077$file=$1;6078$binary=1;6079}else{6080(undef,$file,$lno,$ltext) =split(/:/,$line,4);6081}6082if($filene$lastfile) {6083$lastfileand print"</td></tr>\n";6084if($alternate++) {6085print"<tr class=\"dark\">\n";6086}else{6087print"<tr class=\"light\">\n";6088}6089print"<td class=\"list\">".6090$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6091 file_name=>"$file"),6092-class=>"list"}, esc_path($file));6093print"</td><td>\n";6094$lastfile=$file;6095}6096if($binary) {6097print"<div class=\"binary\">Binary file</div>\n";6098}else{6099$ltext= untabify($ltext);6100if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6101$ltext= esc_html($1, -nbsp=>1);6102$ltext.='<span class="match">';6103$ltext.= esc_html($2, -nbsp=>1);6104$ltext.='</span>';6105$ltext.= esc_html($3, -nbsp=>1);6106}else{6107$ltext= esc_html($ltext, -nbsp=>1);6108}6109print"<div class=\"pre\">".6110$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6111 file_name=>"$file").'#l'.$lno,6112-class=>"linenr"},sprintf('%4i',$lno))6113.' '.$ltext."</div>\n";6114}6115}6116if($lastfile) {6117print"</td></tr>\n";6118if($matches>1000) {6119print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6120}6121}else{6122print"<div class=\"diff nodifferences\">No matches found</div>\n";6123}6124close$fd;61256126print"</table>\n";6127}6128 git_footer_html();6129}61306131sub git_search_help {6132 git_header_html();6133 git_print_page_nav('','',$hash,$hash,$hash);6134print<<EOT;6135<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6136regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6137the pattern entered is recognized as the POSIX extended6138<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6139insensitive).</p>6140<dl>6141<dt><b>commit</b></dt>6142<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6143EOT6144my$have_grep= gitweb_check_feature('grep');6145if($have_grep) {6146print<<EOT;6147<dt><b>grep</b></dt>6148<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6149 a different one) are searched for the given pattern. On large trees, this search can take6150a while and put some strain on the server, so please use it with some consideration. Note that6151due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6152case-sensitive.</dd>6153EOT6154}6155print<<EOT;6156<dt><b>author</b></dt>6157<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6158<dt><b>committer</b></dt>6159<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6160EOT6161my$have_pickaxe= gitweb_check_feature('pickaxe');6162if($have_pickaxe) {6163print<<EOT;6164<dt><b>pickaxe</b></dt>6165<dd>All commits that caused the string to appear or disappear from any file (changes that6166added, removed or "modified" the string) will be listed. This search can take a while and6167takes a lot of strain on the server, so please use it wisely. Note that since you may be6168interested even in changes just changing the case as well, this search is case sensitive.</dd>6169EOT6170}6171print"</dl>\n";6172 git_footer_html();6173}61746175sub git_shortlog {6176my$head= git_get_head_hash($project);6177if(!defined$hash) {6178$hash=$head;6179}6180if(!defined$page) {6181$page=0;6182}6183my$refs= git_get_references();61846185my$commit_hash=$hash;6186if(defined$hash_parent) {6187$commit_hash="$hash_parent..$hash";6188}6189my@commitlist= parse_commits($commit_hash,101, (100*$page));61906191my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);6192my$next_link='';6193if($#commitlist>=100) {6194$next_link=6195$cgi->a({-href => href(-replay=>1, page=>$page+1),6196-accesskey =>"n", -title =>"Alt-n"},"next");6197}6198my$patch_max= gitweb_check_feature('patches');6199if($patch_max) {6200if($patch_max<0||@commitlist<=$patch_max) {6201$paging_nav.=" ⋅ ".6202$cgi->a({-href => href(action=>"patches", -replay=>1)},6203"patches");6204}6205}62066207 git_header_html();6208 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6209 git_print_header_div('summary',$project);62106211 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);62126213 git_footer_html();6214}62156216## ......................................................................6217## feeds (RSS, Atom; OPML)62186219sub git_feed {6220my$format=shift||'atom';6221my$have_blame= gitweb_check_feature('blame');62226223# Atom: http://www.atomenabled.org/developers/syndication/6224# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6225if($formatne'rss'&&$formatne'atom') {6226 die_error(400,"Unknown web feed format");6227}62286229# log/feed of current (HEAD) branch, log of given branch, history of file/directory6230my$head=$hash||'HEAD';6231my@commitlist= parse_commits($head,150,0,$file_name);62326233my%latest_commit;6234my%latest_date;6235my$content_type="application/$format+xml";6236if(defined$cgi->http('HTTP_ACCEPT') &&6237$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6238# browser (feed reader) prefers text/xml6239$content_type='text/xml';6240}6241if(defined($commitlist[0])) {6242%latest_commit= %{$commitlist[0]};6243my$latest_epoch=$latest_commit{'committer_epoch'};6244%latest_date= parse_date($latest_epoch);6245my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6246if(defined$if_modified) {6247my$since;6248if(eval{require HTTP::Date;1; }) {6249$since= HTTP::Date::str2time($if_modified);6250}elsif(eval{require Time::ParseDate;1; }) {6251$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6252}6253if(defined$since&&$latest_epoch<=$since) {6254print$cgi->header(6255-type =>$content_type,6256-charset =>'utf-8',6257-last_modified =>$latest_date{'rfc2822'},6258-status =>'304 Not Modified');6259return;6260}6261}6262print$cgi->header(6263-type =>$content_type,6264-charset =>'utf-8',6265-last_modified =>$latest_date{'rfc2822'});6266}else{6267print$cgi->header(6268-type =>$content_type,6269-charset =>'utf-8');6270}62716272# Optimization: skip generating the body if client asks only6273# for Last-Modified date.6274return if($cgi->request_method()eq'HEAD');62756276# header variables6277my$title="$site_name-$project/$action";6278my$feed_type='log';6279if(defined$hash) {6280$title.=" - '$hash'";6281$feed_type='branch log';6282if(defined$file_name) {6283$title.=" ::$file_name";6284$feed_type='history';6285}6286}elsif(defined$file_name) {6287$title.=" -$file_name";6288$feed_type='history';6289}6290$title.="$feed_type";6291my$descr= git_get_project_description($project);6292if(defined$descr) {6293$descr= esc_html($descr);6294}else{6295$descr="$project".6296($formateq'rss'?'RSS':'Atom') .6297" feed";6298}6299my$owner= git_get_project_owner($project);6300$owner= esc_html($owner);63016302#header6303my$alt_url;6304if(defined$file_name) {6305$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6306}elsif(defined$hash) {6307$alt_url= href(-full=>1, action=>"log", hash=>$hash);6308}else{6309$alt_url= href(-full=>1, action=>"summary");6310}6311print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6312if($formateq'rss') {6313print<<XML;6314<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6315<channel>6316XML6317print"<title>$title</title>\n".6318"<link>$alt_url</link>\n".6319"<description>$descr</description>\n".6320"<language>en</language>\n".6321# project owner is responsible for 'editorial' content6322"<managingEditor>$owner</managingEditor>\n";6323if(defined$logo||defined$favicon) {6324# prefer the logo to the favicon, since RSS6325# doesn't allow both6326my$img= esc_url($logo||$favicon);6327print"<image>\n".6328"<url>$img</url>\n".6329"<title>$title</title>\n".6330"<link>$alt_url</link>\n".6331"</image>\n";6332}6333if(%latest_date) {6334print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6335print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6336}6337print"<generator>gitweb v.$version/$git_version</generator>\n";6338}elsif($formateq'atom') {6339print<<XML;6340<feed xmlns="http://www.w3.org/2005/Atom">6341XML6342print"<title>$title</title>\n".6343"<subtitle>$descr</subtitle>\n".6344'<link rel="alternate" type="text/html" href="'.6345$alt_url.'" />'."\n".6346'<link rel="self" type="'.$content_type.'" href="'.6347$cgi->self_url() .'" />'."\n".6348"<id>". href(-full=>1) ."</id>\n".6349# use project owner for feed author6350"<author><name>$owner</name></author>\n";6351if(defined$favicon) {6352print"<icon>". esc_url($favicon) ."</icon>\n";6353}6354if(defined$logo_url) {6355# not twice as wide as tall: 72 x 27 pixels6356print"<logo>". esc_url($logo) ."</logo>\n";6357}6358if(!%latest_date) {6359# dummy date to keep the feed valid until commits trickle in:6360print"<updated>1970-01-01T00:00:00Z</updated>\n";6361}else{6362print"<updated>$latest_date{'iso-8601'}</updated>\n";6363}6364print"<generator version='$version/$git_version'>gitweb</generator>\n";6365}63666367# contents6368for(my$i=0;$i<=$#commitlist;$i++) {6369my%co= %{$commitlist[$i]};6370my$commit=$co{'id'};6371# we read 150, we always show 30 and the ones more recent than 48 hours6372if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6373last;6374}6375my%cd= parse_date($co{'author_epoch'});63766377# get list of changed files6378open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6379$co{'parent'} ||"--root",6380$co{'id'},"--", (defined$file_name?$file_name: ())6381ornext;6382my@difftree=map{chomp;$_} <$fd>;6383close$fd6384ornext;63856386# print element (entry, item)6387my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6388if($formateq'rss') {6389print"<item>\n".6390"<title>". esc_html($co{'title'}) ."</title>\n".6391"<author>". esc_html($co{'author'}) ."</author>\n".6392"<pubDate>$cd{'rfc2822'}</pubDate>\n".6393"<guid isPermaLink=\"true\">$co_url</guid>\n".6394"<link>$co_url</link>\n".6395"<description>". esc_html($co{'title'}) ."</description>\n".6396"<content:encoded>".6397"<![CDATA[\n";6398}elsif($formateq'atom') {6399print"<entry>\n".6400"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6401"<updated>$cd{'iso-8601'}</updated>\n".6402"<author>\n".6403" <name>". esc_html($co{'author_name'}) ."</name>\n";6404if($co{'author_email'}) {6405print" <email>". esc_html($co{'author_email'}) ."</email>\n";6406}6407print"</author>\n".6408# use committer for contributor6409"<contributor>\n".6410" <name>". esc_html($co{'committer_name'}) ."</name>\n";6411if($co{'committer_email'}) {6412print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6413}6414print"</contributor>\n".6415"<published>$cd{'iso-8601'}</published>\n".6416"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6417"<id>$co_url</id>\n".6418"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6419"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6420}6421my$comment=$co{'comment'};6422print"<pre>\n";6423foreachmy$line(@$comment) {6424$line= esc_html($line);6425print"$line\n";6426}6427print"</pre><ul>\n";6428foreachmy$difftree_line(@difftree) {6429my%difftree= parse_difftree_raw_line($difftree_line);6430next if!$difftree{'from_id'};64316432my$file=$difftree{'file'} ||$difftree{'to_file'};64336434print"<li>".6435"[".6436$cgi->a({-href => href(-full=>1, action=>"blobdiff",6437 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6438 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6439 file_name=>$file, file_parent=>$difftree{'from_file'}),6440-title =>"diff"},'D');6441if($have_blame) {6442print$cgi->a({-href => href(-full=>1, action=>"blame",6443 file_name=>$file, hash_base=>$commit),6444-title =>"blame"},'B');6445}6446# if this is not a feed of a file history6447if(!defined$file_name||$file_namene$file) {6448print$cgi->a({-href => href(-full=>1, action=>"history",6449 file_name=>$file, hash=>$commit),6450-title =>"history"},'H');6451}6452$file= esc_path($file);6453print"] ".6454"$file</li>\n";6455}6456if($formateq'rss') {6457print"</ul>]]>\n".6458"</content:encoded>\n".6459"</item>\n";6460}elsif($formateq'atom') {6461print"</ul>\n</div>\n".6462"</content>\n".6463"</entry>\n";6464}6465}64666467# end of feed6468if($formateq'rss') {6469print"</channel>\n</rss>\n";6470}elsif($formateq'atom') {6471print"</feed>\n";6472}6473}64746475sub git_rss {6476 git_feed('rss');6477}64786479sub git_atom {6480 git_feed('atom');6481}64826483sub git_opml {6484my@list= git_get_projects_list();64856486print$cgi->header(6487-type =>'text/xml',6488-charset =>'utf-8',6489-content_disposition =>'inline; filename="opml.xml"');64906491print<<XML;6492<?xml version="1.0" encoding="utf-8"?>6493<opml version="1.0">6494<head>6495 <title>$site_nameOPML Export</title>6496</head>6497<body>6498<outline text="git RSS feeds">6499XML65006501foreachmy$pr(@list) {6502my%proj=%$pr;6503my$head= git_get_head_hash($proj{'path'});6504if(!defined$head) {6505next;6506}6507$git_dir="$projectroot/$proj{'path'}";6508my%co= parse_commit($head);6509if(!%co) {6510next;6511}65126513my$path= esc_html(chop_str($proj{'path'},25,5));6514my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6515my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6516print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6517}6518print<<XML;6519</outline>6520</body>6521</opml>6522XML6523}