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 21BEGIN{ 22 CGI->compile()if$ENV{'MOD_PERL'}; 23} 24 25our$cgi= new CGI; 26our$version="++GIT_VERSION++"; 27our$my_url=$cgi->url(); 28our$my_uri=$cgi->url(-absolute =>1); 29 30# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 31# needed and used only for URLs with nonempty PATH_INFO 32our$base_url=$my_url; 33 34# When the script is used as DirectoryIndex, the URL does not contain the name 35# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 36# have to do it ourselves. We make $path_info global because it's also used 37# later on. 38# 39# Another issue with the script being the DirectoryIndex is that the resulting 40# $my_url data is not the full script URL: this is good, because we want 41# generated links to keep implying the script name if it wasn't explicitly 42# indicated in the URL we're handling, but it means that $my_url cannot be used 43# as base URL. 44# Therefore, if we needed to strip PATH_INFO, then we know that we have 45# to build the base URL ourselves: 46our$path_info=$ENV{"PATH_INFO"}; 47if($path_info) { 48if($my_url=~ s,\Q$path_info\E$,, && 49$my_uri=~ s,\Q$path_info\E$,, && 50defined$ENV{'SCRIPT_NAME'}) { 51$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 52} 53} 54 55# core git executable to use 56# this can just be "git" if your webserver has a sensible PATH 57our$GIT="++GIT_BINDIR++/git"; 58 59# absolute fs-path which will be prepended to the project path 60#our $projectroot = "/pub/scm"; 61our$projectroot="++GITWEB_PROJECTROOT++"; 62 63# fs traversing limit for getting project list 64# the number is relative to the projectroot 65our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 66 67# target of the home link on top of all pages 68our$home_link=$my_uri||"/"; 69 70# string of the home link on top of all pages 71our$home_link_str="++GITWEB_HOME_LINK_STR++"; 72 73# name of your site or organization to appear in page titles 74# replace this with something more descriptive for clearer bookmarks 75our$site_name="++GITWEB_SITENAME++" 76|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 77 78# filename of html text to include at top of each page 79our$site_header="++GITWEB_SITE_HEADER++"; 80# html text to include at home page 81our$home_text="++GITWEB_HOMETEXT++"; 82# filename of html text to include at bottom of each page 83our$site_footer="++GITWEB_SITE_FOOTER++"; 84 85# URI of stylesheets 86our@stylesheets= ("++GITWEB_CSS++"); 87# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 88our$stylesheet=undef; 89# URI of GIT logo (72x27 size) 90our$logo="++GITWEB_LOGO++"; 91# URI of GIT favicon, assumed to be image/png type 92our$favicon="++GITWEB_FAVICON++"; 93 94# URI and label (title) of GIT logo link 95#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 96#our $logo_label = "git documentation"; 97our$logo_url="http://git-scm.com/"; 98our$logo_label="git homepage"; 99 100# source of projects list 101our$projects_list="++GITWEB_LIST++"; 102 103# the width (in characters) of the projects list "Description" column 104our$projects_list_description_width=25; 105 106# default order of projects list 107# valid values are none, project, descr, owner, and age 108our$default_projects_order="project"; 109 110# show repository only if this file exists 111# (only effective if this variable evaluates to true) 112our$export_ok="++GITWEB_EXPORT_OK++"; 113 114# show repository only if this subroutine returns true 115# when given the path to the project, for example: 116# sub { return -e "$_[0]/git-daemon-export-ok"; } 117our$export_auth_hook=undef; 118 119# only allow viewing of repositories also shown on the overview page 120our$strict_export="++GITWEB_STRICT_EXPORT++"; 121 122# list of git base URLs used for URL to where fetch project from, 123# i.e. full URL is "$git_base_url/$project" 124our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 125 126# default blob_plain mimetype and default charset for text/plain blob 127our$default_blob_plain_mimetype='text/plain'; 128our$default_text_plain_charset=undef; 129 130# file to use for guessing MIME types before trying /etc/mime.types 131# (relative to the current git repository) 132our$mimetypes_file=undef; 133 134# assume this charset if line contains non-UTF-8 characters; 135# it should be valid encoding (see Encoding::Supported(3pm) for list), 136# for which encoding all byte sequences are valid, for example 137# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 138# could be even 'utf-8' for the old behavior) 139our$fallback_encoding='latin1'; 140 141# rename detection options for git-diff and git-diff-tree 142# - default is '-M', with the cost proportional to 143# (number of removed files) * (number of new files). 144# - more costly is '-C' (which implies '-M'), with the cost proportional to 145# (number of changed files + number of removed files) * (number of new files) 146# - even more costly is '-C', '--find-copies-harder' with cost 147# (number of files in the original tree) * (number of new files) 148# - one might want to include '-B' option, e.g. '-B', '-M' 149our@diff_opts= ('-M');# taken from git_commit 150 151# Disables features that would allow repository owners to inject script into 152# the gitweb domain. 153our$prevent_xss=0; 154 155# information about snapshot formats that gitweb is capable of serving 156our%known_snapshot_formats= ( 157# name => { 158# 'display' => display name, 159# 'type' => mime type, 160# 'suffix' => filename suffix, 161# 'format' => --format for git-archive, 162# 'compressor' => [compressor command and arguments] 163# (array reference, optional) 164# 'disabled' => boolean (optional)} 165# 166'tgz'=> { 167'display'=>'tar.gz', 168'type'=>'application/x-gzip', 169'suffix'=>'.tar.gz', 170'format'=>'tar', 171'compressor'=> ['gzip']}, 172 173'tbz2'=> { 174'display'=>'tar.bz2', 175'type'=>'application/x-bzip2', 176'suffix'=>'.tar.bz2', 177'format'=>'tar', 178'compressor'=> ['bzip2']}, 179 180'txz'=> { 181'display'=>'tar.xz', 182'type'=>'application/x-xz', 183'suffix'=>'.tar.xz', 184'format'=>'tar', 185'compressor'=> ['xz'], 186'disabled'=>1}, 187 188'zip'=> { 189'display'=>'zip', 190'type'=>'application/x-zip', 191'suffix'=>'.zip', 192'format'=>'zip'}, 193); 194 195# Aliases so we understand old gitweb.snapshot values in repository 196# configuration. 197our%known_snapshot_format_aliases= ( 198'gzip'=>'tgz', 199'bzip2'=>'tbz2', 200'xz'=>'txz', 201 202# backward compatibility: legacy gitweb config support 203'x-gzip'=>undef,'gz'=>undef, 204'x-bzip2'=>undef,'bz2'=>undef, 205'x-zip'=>undef,''=>undef, 206); 207 208# Pixel sizes for icons and avatars. If the default font sizes or lineheights 209# are changed, it may be appropriate to change these values too via 210# $GITWEB_CONFIG. 211our%avatar_size= ( 212'default'=>16, 213'double'=>32 214); 215 216# You define site-wide feature defaults here; override them with 217# $GITWEB_CONFIG as necessary. 218our%feature= ( 219# feature => { 220# 'sub' => feature-sub (subroutine), 221# 'override' => allow-override (boolean), 222# 'default' => [ default options...] (array reference)} 223# 224# if feature is overridable (it means that allow-override has true value), 225# then feature-sub will be called with default options as parameters; 226# return value of feature-sub indicates if to enable specified feature 227# 228# if there is no 'sub' key (no feature-sub), then feature cannot be 229# overriden 230# 231# use gitweb_get_feature(<feature>) to retrieve the <feature> value 232# (an array) or gitweb_check_feature(<feature>) to check if <feature> 233# is enabled 234 235# Enable the 'blame' blob view, showing the last commit that modified 236# each line in the file. This can be very CPU-intensive. 237 238# To enable system wide have in $GITWEB_CONFIG 239# $feature{'blame'}{'default'} = [1]; 240# To have project specific config enable override in $GITWEB_CONFIG 241# $feature{'blame'}{'override'} = 1; 242# and in project config gitweb.blame = 0|1; 243'blame'=> { 244'sub'=>sub{ feature_bool('blame',@_) }, 245'override'=>0, 246'default'=> [0]}, 247 248# Enable the 'snapshot' link, providing a compressed archive of any 249# tree. This can potentially generate high traffic if you have large 250# project. 251 252# Value is a list of formats defined in %known_snapshot_formats that 253# you wish to offer. 254# To disable system wide have in $GITWEB_CONFIG 255# $feature{'snapshot'}{'default'} = []; 256# To have project specific config enable override in $GITWEB_CONFIG 257# $feature{'snapshot'}{'override'} = 1; 258# and in project config, a comma-separated list of formats or "none" 259# to disable. Example: gitweb.snapshot = tbz2,zip; 260'snapshot'=> { 261'sub'=> \&feature_snapshot, 262'override'=>0, 263'default'=> ['tgz']}, 264 265# Enable text search, which will list the commits which match author, 266# committer or commit text to a given string. Enabled by default. 267# Project specific override is not supported. 268'search'=> { 269'override'=>0, 270'default'=> [1]}, 271 272# Enable grep search, which will list the files in currently selected 273# tree containing the given string. Enabled by default. This can be 274# potentially CPU-intensive, of course. 275 276# To enable system wide have in $GITWEB_CONFIG 277# $feature{'grep'}{'default'} = [1]; 278# To have project specific config enable override in $GITWEB_CONFIG 279# $feature{'grep'}{'override'} = 1; 280# and in project config gitweb.grep = 0|1; 281'grep'=> { 282'sub'=>sub{ feature_bool('grep',@_) }, 283'override'=>0, 284'default'=> [1]}, 285 286# Enable the pickaxe search, which will list the commits that modified 287# a given string in a file. This can be practical and quite faster 288# alternative to 'blame', but still potentially CPU-intensive. 289 290# To enable system wide have in $GITWEB_CONFIG 291# $feature{'pickaxe'}{'default'} = [1]; 292# To have project specific config enable override in $GITWEB_CONFIG 293# $feature{'pickaxe'}{'override'} = 1; 294# and in project config gitweb.pickaxe = 0|1; 295'pickaxe'=> { 296'sub'=>sub{ feature_bool('pickaxe',@_) }, 297'override'=>0, 298'default'=> [1]}, 299 300# Make gitweb use an alternative format of the URLs which can be 301# more readable and natural-looking: project name is embedded 302# directly in the path and the query string contains other 303# auxiliary information. All gitweb installations recognize 304# URL in either format; this configures in which formats gitweb 305# generates links. 306 307# To enable system wide have in $GITWEB_CONFIG 308# $feature{'pathinfo'}{'default'} = [1]; 309# Project specific override is not supported. 310 311# Note that you will need to change the default location of CSS, 312# favicon, logo and possibly other files to an absolute URL. Also, 313# if gitweb.cgi serves as your indexfile, you will need to force 314# $my_uri to contain the script name in your $GITWEB_CONFIG. 315'pathinfo'=> { 316'override'=>0, 317'default'=> [0]}, 318 319# Make gitweb consider projects in project root subdirectories 320# to be forks of existing projects. Given project $projname.git, 321# projects matching $projname/*.git will not be shown in the main 322# projects list, instead a '+' mark will be added to $projname 323# there and a 'forks' view will be enabled for the project, listing 324# all the forks. If project list is taken from a file, forks have 325# to be listed after the main project. 326 327# To enable system wide have in $GITWEB_CONFIG 328# $feature{'forks'}{'default'} = [1]; 329# Project specific override is not supported. 330'forks'=> { 331'override'=>0, 332'default'=> [0]}, 333 334# Insert custom links to the action bar of all project pages. 335# This enables you mainly to link to third-party scripts integrating 336# into gitweb; e.g. git-browser for graphical history representation 337# or custom web-based repository administration interface. 338 339# The 'default' value consists of a list of triplets in the form 340# (label, link, position) where position is the label after which 341# to insert the link and link is a format string where %n expands 342# to the project name, %f to the project path within the filesystem, 343# %h to the current hash (h gitweb parameter) and %b to the current 344# hash base (hb gitweb parameter); %% expands to %. 345 346# To enable system wide have in $GITWEB_CONFIG e.g. 347# $feature{'actions'}{'default'} = [('graphiclog', 348# '/git-browser/by-commit.html?r=%n', 'summary')]; 349# Project specific override is not supported. 350'actions'=> { 351'override'=>0, 352'default'=> []}, 353 354# Allow gitweb scan project content tags described in ctags/ 355# of project repository, and display the popular Web 2.0-ish 356# "tag cloud" near the project list. Note that this is something 357# COMPLETELY different from the normal Git tags. 358 359# gitweb by itself can show existing tags, but it does not handle 360# tagging itself; you need an external application for that. 361# For an example script, check Girocco's cgi/tagproj.cgi. 362# You may want to install the HTML::TagCloud Perl module to get 363# a pretty tag cloud instead of just a list of tags. 364 365# To enable system wide have in $GITWEB_CONFIG 366# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 367# Project specific override is not supported. 368'ctags'=> { 369'override'=>0, 370'default'=> [0]}, 371 372# The maximum number of patches in a patchset generated in patch 373# view. Set this to 0 or undef to disable patch view, or to a 374# negative number to remove any limit. 375 376# To disable system wide have in $GITWEB_CONFIG 377# $feature{'patches'}{'default'} = [0]; 378# To have project specific config enable override in $GITWEB_CONFIG 379# $feature{'patches'}{'override'} = 1; 380# and in project config gitweb.patches = 0|n; 381# where n is the maximum number of patches allowed in a patchset. 382'patches'=> { 383'sub'=> \&feature_patches, 384'override'=>0, 385'default'=> [16]}, 386 387# Avatar support. When this feature is enabled, views such as 388# shortlog or commit will display an avatar associated with 389# the email of the committer(s) and/or author(s). 390 391# Currently available providers are gravatar and picon. 392# If an unknown provider is specified, the feature is disabled. 393 394# Gravatar depends on Digest::MD5. 395# Picon currently relies on the indiana.edu database. 396 397# To enable system wide have in $GITWEB_CONFIG 398# $feature{'avatar'}{'default'} = ['<provider>']; 399# where <provider> is either gravatar or picon. 400# To have project specific config enable override in $GITWEB_CONFIG 401# $feature{'avatar'}{'override'} = 1; 402# and in project config gitweb.avatar = <provider>; 403'avatar'=> { 404'sub'=> \&feature_avatar, 405'override'=>0, 406'default'=> ['']}, 407); 408 409sub gitweb_get_feature { 410my($name) =@_; 411return unlessexists$feature{$name}; 412my($sub,$override,@defaults) = ( 413$feature{$name}{'sub'}, 414$feature{$name}{'override'}, 415@{$feature{$name}{'default'}}); 416if(!$override) {return@defaults; } 417if(!defined$sub) { 418warn"feature$nameis not overridable"; 419return@defaults; 420} 421return$sub->(@defaults); 422} 423 424# A wrapper to check if a given feature is enabled. 425# With this, you can say 426# 427# my $bool_feat = gitweb_check_feature('bool_feat'); 428# gitweb_check_feature('bool_feat') or somecode; 429# 430# instead of 431# 432# my ($bool_feat) = gitweb_get_feature('bool_feat'); 433# (gitweb_get_feature('bool_feat'))[0] or somecode; 434# 435sub gitweb_check_feature { 436return(gitweb_get_feature(@_))[0]; 437} 438 439 440sub feature_bool { 441my$key=shift; 442my($val) = git_get_project_config($key,'--bool'); 443 444if(!defined$val) { 445return($_[0]); 446}elsif($valeq'true') { 447return(1); 448}elsif($valeq'false') { 449return(0); 450} 451} 452 453sub feature_snapshot { 454my(@fmts) =@_; 455 456my($val) = git_get_project_config('snapshot'); 457 458if($val) { 459@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 460} 461 462return@fmts; 463} 464 465sub feature_patches { 466my@val= (git_get_project_config('patches','--int')); 467 468if(@val) { 469return@val; 470} 471 472return($_[0]); 473} 474 475sub feature_avatar { 476my@val= (git_get_project_config('avatar')); 477 478return@val?@val:@_; 479} 480 481# checking HEAD file with -e is fragile if the repository was 482# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 483# and then pruned. 484sub check_head_link { 485my($dir) =@_; 486my$headfile="$dir/HEAD"; 487return((-e $headfile) || 488(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 489} 490 491sub check_export_ok { 492my($dir) =@_; 493return(check_head_link($dir) && 494(!$export_ok|| -e "$dir/$export_ok") && 495(!$export_auth_hook||$export_auth_hook->($dir))); 496} 497 498# process alternate names for backward compatibility 499# filter out unsupported (unknown) snapshot formats 500sub filter_snapshot_fmts { 501my@fmts=@_; 502 503@fmts=map{ 504exists$known_snapshot_format_aliases{$_} ? 505$known_snapshot_format_aliases{$_} :$_}@fmts; 506@fmts=grep{ 507exists$known_snapshot_formats{$_} && 508!$known_snapshot_formats{$_}{'disabled'}}@fmts; 509} 510 511our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 512if(-e $GITWEB_CONFIG) { 513do$GITWEB_CONFIG; 514}else{ 515our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 516do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 517} 518 519# version of the core git binary 520our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 521 522$projects_list||=$projectroot; 523 524# ====================================================================== 525# input validation and dispatch 526 527# input parameters can be collected from a variety of sources (presently, CGI 528# and PATH_INFO), so we define an %input_params hash that collects them all 529# together during validation: this allows subsequent uses (e.g. href()) to be 530# agnostic of the parameter origin 531 532our%input_params= (); 533 534# input parameters are stored with the long parameter name as key. This will 535# also be used in the href subroutine to convert parameters to their CGI 536# equivalent, and since the href() usage is the most frequent one, we store 537# the name -> CGI key mapping here, instead of the reverse. 538# 539# XXX: Warning: If you touch this, check the search form for updating, 540# too. 541 542our@cgi_param_mapping= ( 543 project =>"p", 544 action =>"a", 545 file_name =>"f", 546 file_parent =>"fp", 547 hash =>"h", 548 hash_parent =>"hp", 549 hash_base =>"hb", 550 hash_parent_base =>"hpb", 551 page =>"pg", 552 order =>"o", 553 searchtext =>"s", 554 searchtype =>"st", 555 snapshot_format =>"sf", 556 extra_options =>"opt", 557 search_use_regexp =>"sr", 558); 559our%cgi_param_mapping=@cgi_param_mapping; 560 561# we will also need to know the possible actions, for validation 562our%actions= ( 563"blame"=> \&git_blame, 564"blobdiff"=> \&git_blobdiff, 565"blobdiff_plain"=> \&git_blobdiff_plain, 566"blob"=> \&git_blob, 567"blob_plain"=> \&git_blob_plain, 568"commitdiff"=> \&git_commitdiff, 569"commitdiff_plain"=> \&git_commitdiff_plain, 570"commit"=> \&git_commit, 571"forks"=> \&git_forks, 572"heads"=> \&git_heads, 573"history"=> \&git_history, 574"log"=> \&git_log, 575"patch"=> \&git_patch, 576"patches"=> \&git_patches, 577"rss"=> \&git_rss, 578"atom"=> \&git_atom, 579"search"=> \&git_search, 580"search_help"=> \&git_search_help, 581"shortlog"=> \&git_shortlog, 582"summary"=> \&git_summary, 583"tag"=> \&git_tag, 584"tags"=> \&git_tags, 585"tree"=> \&git_tree, 586"snapshot"=> \&git_snapshot, 587"object"=> \&git_object, 588# those below don't need $project 589"opml"=> \&git_opml, 590"project_list"=> \&git_project_list, 591"project_index"=> \&git_project_index, 592); 593 594# finally, we have the hash of allowed extra_options for the commands that 595# allow them 596our%allowed_options= ( 597"--no-merges"=> [qw(rss atom log shortlog history)], 598); 599 600# fill %input_params with the CGI parameters. All values except for 'opt' 601# should be single values, but opt can be an array. We should probably 602# build an array of parameters that can be multi-valued, but since for the time 603# being it's only this one, we just single it out 604while(my($name,$symbol) =each%cgi_param_mapping) { 605if($symboleq'opt') { 606$input_params{$name} = [$cgi->param($symbol) ]; 607}else{ 608$input_params{$name} =$cgi->param($symbol); 609} 610} 611 612# now read PATH_INFO and update the parameter list for missing parameters 613sub evaluate_path_info { 614return ifdefined$input_params{'project'}; 615return if!$path_info; 616$path_info=~ s,^/+,,; 617return if!$path_info; 618 619# find which part of PATH_INFO is project 620my$project=$path_info; 621$project=~ s,/+$,,; 622while($project&& !check_head_link("$projectroot/$project")) { 623$project=~ s,/*[^/]*$,,; 624} 625return unless$project; 626$input_params{'project'} =$project; 627 628# do not change any parameters if an action is given using the query string 629return if$input_params{'action'}; 630$path_info=~ s,^\Q$project\E/*,,; 631 632# next, check if we have an action 633my$action=$path_info; 634$action=~ s,/.*$,,; 635if(exists$actions{$action}) { 636$path_info=~ s,^$action/*,,; 637$input_params{'action'} =$action; 638} 639 640# list of actions that want hash_base instead of hash, but can have no 641# pathname (f) parameter 642my@wants_base= ( 643'tree', 644'history', 645); 646 647# we want to catch 648# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 649my($parentrefname,$parentpathname,$refname,$pathname) = 650($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 651 652# first, analyze the 'current' part 653if(defined$pathname) { 654# we got "branch:filename" or "branch:dir/" 655# we could use git_get_type(branch:pathname), but: 656# - it needs $git_dir 657# - it does a git() call 658# - the convention of terminating directories with a slash 659# makes it superfluous 660# - embedding the action in the PATH_INFO would make it even 661# more superfluous 662$pathname=~ s,^/+,,; 663if(!$pathname||substr($pathname, -1)eq"/") { 664$input_params{'action'} ||="tree"; 665$pathname=~ s,/$,,; 666}else{ 667# the default action depends on whether we had parent info 668# or not 669if($parentrefname) { 670$input_params{'action'} ||="blobdiff_plain"; 671}else{ 672$input_params{'action'} ||="blob_plain"; 673} 674} 675$input_params{'hash_base'} ||=$refname; 676$input_params{'file_name'} ||=$pathname; 677}elsif(defined$refname) { 678# we got "branch". In this case we have to choose if we have to 679# set hash or hash_base. 680# 681# Most of the actions without a pathname only want hash to be 682# set, except for the ones specified in @wants_base that want 683# hash_base instead. It should also be noted that hand-crafted 684# links having 'history' as an action and no pathname or hash 685# set will fail, but that happens regardless of PATH_INFO. 686$input_params{'action'} ||="shortlog"; 687if(grep{$_eq$input_params{'action'} }@wants_base) { 688$input_params{'hash_base'} ||=$refname; 689}else{ 690$input_params{'hash'} ||=$refname; 691} 692} 693 694# next, handle the 'parent' part, if present 695if(defined$parentrefname) { 696# a missing pathspec defaults to the 'current' filename, allowing e.g. 697# someproject/blobdiff/oldrev..newrev:/filename 698if($parentpathname) { 699$parentpathname=~ s,^/+,,; 700$parentpathname=~ s,/$,,; 701$input_params{'file_parent'} ||=$parentpathname; 702}else{ 703$input_params{'file_parent'} ||=$input_params{'file_name'}; 704} 705# we assume that hash_parent_base is wanted if a path was specified, 706# or if the action wants hash_base instead of hash 707if(defined$input_params{'file_parent'} || 708grep{$_eq$input_params{'action'} }@wants_base) { 709$input_params{'hash_parent_base'} ||=$parentrefname; 710}else{ 711$input_params{'hash_parent'} ||=$parentrefname; 712} 713} 714 715# for the snapshot action, we allow URLs in the form 716# $project/snapshot/$hash.ext 717# where .ext determines the snapshot and gets removed from the 718# passed $refname to provide the $hash. 719# 720# To be able to tell that $refname includes the format extension, we 721# require the following two conditions to be satisfied: 722# - the hash input parameter MUST have been set from the $refname part 723# of the URL (i.e. they must be equal) 724# - the snapshot format MUST NOT have been defined already (e.g. from 725# CGI parameter sf) 726# It's also useless to try any matching unless $refname has a dot, 727# so we check for that too 728if(defined$input_params{'action'} && 729$input_params{'action'}eq'snapshot'&& 730defined$refname&&index($refname,'.') != -1&& 731$refnameeq$input_params{'hash'} && 732!defined$input_params{'snapshot_format'}) { 733# We loop over the known snapshot formats, checking for 734# extensions. Allowed extensions are both the defined suffix 735# (which includes the initial dot already) and the snapshot 736# format key itself, with a prepended dot 737while(my($fmt,$opt) =each%known_snapshot_formats) { 738my$hash=$refname; 739unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 740next; 741} 742my$sfx=$1; 743# a valid suffix was found, so set the snapshot format 744# and reset the hash parameter 745$input_params{'snapshot_format'} =$fmt; 746$input_params{'hash'} =$hash; 747# we also set the format suffix to the one requested 748# in the URL: this way a request for e.g. .tgz returns 749# a .tgz instead of a .tar.gz 750$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 751last; 752} 753} 754} 755evaluate_path_info(); 756 757our$action=$input_params{'action'}; 758if(defined$action) { 759if(!validate_action($action)) { 760 die_error(400,"Invalid action parameter"); 761} 762} 763 764# parameters which are pathnames 765our$project=$input_params{'project'}; 766if(defined$project) { 767if(!validate_project($project)) { 768undef$project; 769 die_error(404,"No such project"); 770} 771} 772 773our$file_name=$input_params{'file_name'}; 774if(defined$file_name) { 775if(!validate_pathname($file_name)) { 776 die_error(400,"Invalid file parameter"); 777} 778} 779 780our$file_parent=$input_params{'file_parent'}; 781if(defined$file_parent) { 782if(!validate_pathname($file_parent)) { 783 die_error(400,"Invalid file parent parameter"); 784} 785} 786 787# parameters which are refnames 788our$hash=$input_params{'hash'}; 789if(defined$hash) { 790if(!validate_refname($hash)) { 791 die_error(400,"Invalid hash parameter"); 792} 793} 794 795our$hash_parent=$input_params{'hash_parent'}; 796if(defined$hash_parent) { 797if(!validate_refname($hash_parent)) { 798 die_error(400,"Invalid hash parent parameter"); 799} 800} 801 802our$hash_base=$input_params{'hash_base'}; 803if(defined$hash_base) { 804if(!validate_refname($hash_base)) { 805 die_error(400,"Invalid hash base parameter"); 806} 807} 808 809our@extra_options= @{$input_params{'extra_options'}}; 810# @extra_options is always defined, since it can only be (currently) set from 811# CGI, and $cgi->param() returns the empty array in array context if the param 812# is not set 813foreachmy$opt(@extra_options) { 814if(not exists$allowed_options{$opt}) { 815 die_error(400,"Invalid option parameter"); 816} 817if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 818 die_error(400,"Invalid option parameter for this action"); 819} 820} 821 822our$hash_parent_base=$input_params{'hash_parent_base'}; 823if(defined$hash_parent_base) { 824if(!validate_refname($hash_parent_base)) { 825 die_error(400,"Invalid hash parent base parameter"); 826} 827} 828 829# other parameters 830our$page=$input_params{'page'}; 831if(defined$page) { 832if($page=~m/[^0-9]/) { 833 die_error(400,"Invalid page parameter"); 834} 835} 836 837our$searchtype=$input_params{'searchtype'}; 838if(defined$searchtype) { 839if($searchtype=~m/[^a-z]/) { 840 die_error(400,"Invalid searchtype parameter"); 841} 842} 843 844our$search_use_regexp=$input_params{'search_use_regexp'}; 845 846our$searchtext=$input_params{'searchtext'}; 847our$search_regexp; 848if(defined$searchtext) { 849if(length($searchtext) <2) { 850 die_error(403,"At least two characters are required for search parameter"); 851} 852$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 853} 854 855# path to the current git repository 856our$git_dir; 857$git_dir="$projectroot/$project"if$project; 858 859# list of supported snapshot formats 860our@snapshot_fmts= gitweb_get_feature('snapshot'); 861@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 862 863# check that the avatar feature is set to a known provider name, 864# and for each provider check if the dependencies are satisfied. 865# if the provider name is invalid or the dependencies are not met, 866# reset $git_avatar to the empty string. 867our($git_avatar) = gitweb_get_feature('avatar'); 868if($git_avatareq'gravatar') { 869$git_avatar=''unless(eval{require Digest::MD5;1; }); 870}elsif($git_avatareq'picon') { 871# no dependencies 872}else{ 873$git_avatar=''; 874} 875 876# dispatch 877if(!defined$action) { 878if(defined$hash) { 879$action= git_get_type($hash); 880}elsif(defined$hash_base&&defined$file_name) { 881$action= git_get_type("$hash_base:$file_name"); 882}elsif(defined$project) { 883$action='summary'; 884}else{ 885$action='project_list'; 886} 887} 888if(!defined($actions{$action})) { 889 die_error(400,"Unknown action"); 890} 891if($action!~m/^(?:opml|project_list|project_index)$/&& 892!$project) { 893 die_error(400,"Project needed"); 894} 895$actions{$action}->(); 896exit; 897 898## ====================================================================== 899## action links 900 901sub href { 902my%params=@_; 903# default is to use -absolute url() i.e. $my_uri 904my$href=$params{-full} ?$my_url:$my_uri; 905 906$params{'project'} =$projectunlessexists$params{'project'}; 907 908if($params{-replay}) { 909while(my($name,$symbol) =each%cgi_param_mapping) { 910if(!exists$params{$name}) { 911$params{$name} =$input_params{$name}; 912} 913} 914} 915 916my$use_pathinfo= gitweb_check_feature('pathinfo'); 917if($use_pathinfoand defined$params{'project'}) { 918# try to put as many parameters as possible in PATH_INFO: 919# - project name 920# - action 921# - hash_parent or hash_parent_base:/file_parent 922# - hash or hash_base:/filename 923# - the snapshot_format as an appropriate suffix 924 925# When the script is the root DirectoryIndex for the domain, 926# $href here would be something like http://gitweb.example.com/ 927# Thus, we strip any trailing / from $href, to spare us double 928# slashes in the final URL 929$href=~ s,/$,,; 930 931# Then add the project name, if present 932$href.="/".esc_url($params{'project'}); 933delete$params{'project'}; 934 935# since we destructively absorb parameters, we keep this 936# boolean that remembers if we're handling a snapshot 937my$is_snapshot=$params{'action'}eq'snapshot'; 938 939# Summary just uses the project path URL, any other action is 940# added to the URL 941if(defined$params{'action'}) { 942$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 943delete$params{'action'}; 944} 945 946# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 947# stripping nonexistent or useless pieces 948$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 949||$params{'hash_parent'} ||$params{'hash'}); 950if(defined$params{'hash_base'}) { 951if(defined$params{'hash_parent_base'}) { 952$href.= esc_url($params{'hash_parent_base'}); 953# skip the file_parent if it's the same as the file_name 954if(defined$params{'file_parent'}) { 955if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) { 956delete$params{'file_parent'}; 957}elsif($params{'file_parent'} !~/\.\./) { 958$href.=":/".esc_url($params{'file_parent'}); 959delete$params{'file_parent'}; 960} 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 {1972return$GIT,'--git-dir='.$git_dir;1973}19741975# quote the given arguments for passing them to the shell1976# quote_command("command", "arg 1", "arg with ' and ! characters")1977# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1978# Try to avoid using this function wherever possible.1979sub quote_command {1980returnjoin(' ',1981map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);1982}19831984# get HEAD ref of given project as hash1985sub git_get_head_hash {1986my$project=shift;1987my$o_git_dir=$git_dir;1988my$retval=undef;1989$git_dir="$projectroot/$project";1990if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1991my$head= <$fd>;1992close$fd;1993if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1994$retval=$1;1995}1996}1997if(defined$o_git_dir) {1998$git_dir=$o_git_dir;1999}2000return$retval;2001}20022003# get type of given object2004sub git_get_type {2005my$hash=shift;20062007open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2008my$type= <$fd>;2009close$fdorreturn;2010chomp$type;2011return$type;2012}20132014# repository configuration2015our$config_file='';2016our%config;20172018# store multiple values for single key as anonymous array reference2019# single values stored directly in the hash, not as [ <value> ]2020sub hash_set_multi {2021my($hash,$key,$value) =@_;20222023if(!exists$hash->{$key}) {2024$hash->{$key} =$value;2025}elsif(!ref$hash->{$key}) {2026$hash->{$key} = [$hash->{$key},$value];2027}else{2028push@{$hash->{$key}},$value;2029}2030}20312032# return hash of git project configuration2033# optionally limited to some section, e.g. 'gitweb'2034sub git_parse_project_config {2035my$section_regexp=shift;2036my%config;20372038local$/="\0";20392040open my$fh,"-|", git_cmd(),"config",'-z','-l',2041orreturn;20422043while(my$keyval= <$fh>) {2044chomp$keyval;2045my($key,$value) =split(/\n/,$keyval,2);20462047 hash_set_multi(\%config,$key,$value)2048if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2049}2050close$fh;20512052return%config;2053}20542055# convert config value to boolean: 'true' or 'false'2056# no value, number > 0, 'true' and 'yes' values are true2057# rest of values are treated as false (never as error)2058sub config_to_bool {2059my$val=shift;20602061return1if!defined$val;# section.key20622063# strip leading and trailing whitespace2064$val=~s/^\s+//;2065$val=~s/\s+$//;20662067return(($val=~/^\d+$/&&$val) ||# section.key = 12068($val=~/^(?:true|yes)$/i));# section.key = true2069}20702071# convert config value to simple decimal number2072# an optional value suffix of 'k', 'm', or 'g' will cause the value2073# to be multiplied by 1024, 1048576, or 10737418242074sub config_to_int {2075my$val=shift;20762077# strip leading and trailing whitespace2078$val=~s/^\s+//;2079$val=~s/\s+$//;20802081if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2082$unit=lc($unit);2083# unknown unit is treated as 12084return$num* ($uniteq'g'?1073741824:2085$uniteq'm'?1048576:2086$uniteq'k'?1024:1);2087}2088return$val;2089}20902091# convert config value to array reference, if needed2092sub config_to_multi {2093my$val=shift;20942095returnref($val) ?$val: (defined($val) ? [$val] : []);2096}20972098sub git_get_project_config {2099my($key,$type) =@_;21002101# key sanity check2102return unless($key);2103$key=~s/^gitweb\.//;2104return if($key=~m/\W/);21052106# type sanity check2107if(defined$type) {2108$type=~s/^--//;2109$type=undef2110unless($typeeq'bool'||$typeeq'int');2111}21122113# get config2114if(!defined$config_file||2115$config_filene"$git_dir/config") {2116%config= git_parse_project_config('gitweb');2117$config_file="$git_dir/config";2118}21192120# check if config variable (key) exists2121return unlessexists$config{"gitweb.$key"};21222123# ensure given type2124if(!defined$type) {2125return$config{"gitweb.$key"};2126}elsif($typeeq'bool') {2127# backward compatibility: 'git config --bool' returns true/false2128return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2129}elsif($typeeq'int') {2130return config_to_int($config{"gitweb.$key"});2131}2132return$config{"gitweb.$key"};2133}21342135# get hash of given path at given ref2136sub git_get_hash_by_path {2137my$base=shift;2138my$path=shift||returnundef;2139my$type=shift;21402141$path=~ s,/+$,,;21422143open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2144or die_error(500,"Open git-ls-tree failed");2145my$line= <$fd>;2146close$fdorreturnundef;21472148if(!defined$line) {2149# there is no tree or hash given by $path at $base2150returnundef;2151}21522153#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2154$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2155if(defined$type&&$typene$2) {2156# type doesn't match2157returnundef;2158}2159return$3;2160}21612162# get path of entry with given hash at given tree-ish (ref)2163# used to get 'from' filename for combined diff (merge commit) for renames2164sub git_get_path_by_hash {2165my$base=shift||return;2166my$hash=shift||return;21672168local$/="\0";21692170open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2171orreturnundef;2172while(my$line= <$fd>) {2173chomp$line;21742175#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2176#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2177if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2178close$fd;2179return$1;2180}2181}2182close$fd;2183returnundef;2184}21852186## ......................................................................2187## git utility functions, directly accessing git repository21882189sub git_get_project_description {2190my$path=shift;21912192$git_dir="$projectroot/$path";2193open my$fd,'<',"$git_dir/description"2194orreturn git_get_project_config('description');2195my$descr= <$fd>;2196close$fd;2197if(defined$descr) {2198chomp$descr;2199}2200return$descr;2201}22022203sub git_get_project_ctags {2204my$path=shift;2205my$ctags= {};22062207$git_dir="$projectroot/$path";2208opendir my$dh,"$git_dir/ctags"2209orreturn$ctags;2210foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2211open my$ct,'<',$_ornext;2212my$val= <$ct>;2213chomp$val;2214close$ct;2215my$ctag=$_;$ctag=~ s#.*/##;2216$ctags->{$ctag} =$val;2217}2218closedir$dh;2219$ctags;2220}22212222sub git_populate_project_tagcloud {2223my$ctags=shift;22242225# First, merge different-cased tags; tags vote on casing2226my%ctags_lc;2227foreach(keys%$ctags) {2228$ctags_lc{lc$_}->{count} +=$ctags->{$_};2229if(not$ctags_lc{lc$_}->{topcount}2230or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2231$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2232$ctags_lc{lc$_}->{topname} =$_;2233}2234}22352236my$cloud;2237if(eval{require HTML::TagCloud;1; }) {2238$cloud= HTML::TagCloud->new;2239foreach(sort keys%ctags_lc) {2240# Pad the title with spaces so that the cloud looks2241# less crammed.2242my$title=$ctags_lc{$_}->{topname};2243$title=~s/ / /g;2244$title=~s/^/ /g;2245$title=~s/$/ /g;2246$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2247}2248}else{2249$cloud= \%ctags_lc;2250}2251$cloud;2252}22532254sub git_show_project_tagcloud {2255my($cloud,$count) =@_;2256print STDERR ref($cloud)."..\n";2257if(ref$cloudeq'HTML::TagCloud') {2258return$cloud->html_and_css($count);2259}else{2260my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2261return'<p align="center">'.join(', ',map{2262"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2263}splice(@tags,0,$count)) .'</p>';2264}2265}22662267sub git_get_project_url_list {2268my$path=shift;22692270$git_dir="$projectroot/$path";2271open my$fd,'<',"$git_dir/cloneurl"2272orreturnwantarray?2273@{ config_to_multi(git_get_project_config('url')) } :2274 config_to_multi(git_get_project_config('url'));2275my@git_project_url_list=map{chomp;$_} <$fd>;2276close$fd;22772278returnwantarray?@git_project_url_list: \@git_project_url_list;2279}22802281sub git_get_projects_list {2282my($filter) =@_;2283my@list;22842285$filter||='';2286$filter=~s/\.git$//;22872288my$check_forks= gitweb_check_feature('forks');22892290if(-d $projects_list) {2291# search in directory2292my$dir=$projects_list. ($filter?"/$filter":'');2293# remove the trailing "/"2294$dir=~s!/+$!!;2295my$pfxlen=length("$dir");2296my$pfxdepth= ($dir=~tr!/!!);22972298 File::Find::find({2299 follow_fast =>1,# follow symbolic links2300 follow_skip =>2,# ignore duplicates2301 dangling_symlinks =>0,# ignore dangling symlinks, silently2302 wanted =>sub{2303# skip project-list toplevel, if we get it.2304return if(m!^[/.]$!);2305# only directories can be git repositories2306return unless(-d $_);2307# don't traverse too deep (Find is super slow on os x)2308if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2309$File::Find::prune =1;2310return;2311}23122313my$subdir=substr($File::Find::name,$pfxlen+1);2314# we check related file in $projectroot2315my$path= ($filter?"$filter/":'') .$subdir;2316if(check_export_ok("$projectroot/$path")) {2317push@list, { path =>$path};2318$File::Find::prune =1;2319}2320},2321},"$dir");23222323}elsif(-f $projects_list) {2324# read from file(url-encoded):2325# 'git%2Fgit.git Linus+Torvalds'2326# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2327# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2328my%paths;2329open my$fd,'<',$projects_listorreturn;2330 PROJECT:2331while(my$line= <$fd>) {2332chomp$line;2333my($path,$owner) =split' ',$line;2334$path= unescape($path);2335$owner= unescape($owner);2336if(!defined$path) {2337next;2338}2339if($filterne'') {2340# looking for forks;2341my$pfx=substr($path,0,length($filter));2342if($pfxne$filter) {2343next PROJECT;2344}2345my$sfx=substr($path,length($filter));2346if($sfx!~/^\/.*\.git$/) {2347next PROJECT;2348}2349}elsif($check_forks) {2350 PATH:2351foreachmy$filter(keys%paths) {2352# looking for forks;2353my$pfx=substr($path,0,length($filter));2354if($pfxne$filter) {2355next PATH;2356}2357my$sfx=substr($path,length($filter));2358if($sfx!~/^\/.*\.git$/) {2359next PATH;2360}2361# is a fork, don't include it in2362# the list2363next PROJECT;2364}2365}2366if(check_export_ok("$projectroot/$path")) {2367my$pr= {2368 path =>$path,2369 owner => to_utf8($owner),2370};2371push@list,$pr;2372(my$forks_path=$path) =~s/\.git$//;2373$paths{$forks_path}++;2374}2375}2376close$fd;2377}2378return@list;2379}23802381our$gitweb_project_owner=undef;2382sub git_get_project_list_from_file {23832384return if(defined$gitweb_project_owner);23852386$gitweb_project_owner= {};2387# read from file (url-encoded):2388# 'git%2Fgit.git Linus+Torvalds'2389# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2390# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2391if(-f $projects_list) {2392open(my$fd,'<',$projects_list);2393while(my$line= <$fd>) {2394chomp$line;2395my($pr,$ow) =split' ',$line;2396$pr= unescape($pr);2397$ow= unescape($ow);2398$gitweb_project_owner->{$pr} = to_utf8($ow);2399}2400close$fd;2401}2402}24032404sub git_get_project_owner {2405my$project=shift;2406my$owner;24072408returnundefunless$project;2409$git_dir="$projectroot/$project";24102411if(!defined$gitweb_project_owner) {2412 git_get_project_list_from_file();2413}24142415if(exists$gitweb_project_owner->{$project}) {2416$owner=$gitweb_project_owner->{$project};2417}2418if(!defined$owner){2419$owner= git_get_project_config('owner');2420}2421if(!defined$owner) {2422$owner= get_file_owner("$git_dir");2423}24242425return$owner;2426}24272428sub git_get_last_activity {2429my($path) =@_;2430my$fd;24312432$git_dir="$projectroot/$path";2433open($fd,"-|", git_cmd(),'for-each-ref',2434'--format=%(committer)',2435'--sort=-committerdate',2436'--count=1',2437'refs/heads')orreturn;2438my$most_recent= <$fd>;2439close$fdorreturn;2440if(defined$most_recent&&2441$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2442my$timestamp=$1;2443my$age=time-$timestamp;2444return($age, age_string($age));2445}2446return(undef,undef);2447}24482449sub git_get_references {2450my$type=shift||"";2451my%refs;2452# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112453# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2454open my$fd,"-|", git_cmd(),"show-ref","--dereference",2455($type? ("--","refs/$type") : ())# use -- <pattern> if $type2456orreturn;24572458while(my$line= <$fd>) {2459chomp$line;2460if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2461if(defined$refs{$1}) {2462push@{$refs{$1}},$2;2463}else{2464$refs{$1} = [$2];2465}2466}2467}2468close$fdorreturn;2469return \%refs;2470}24712472sub git_get_rev_name_tags {2473my$hash=shift||returnundef;24742475open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2476orreturn;2477my$name_rev= <$fd>;2478close$fd;24792480if($name_rev=~ m|^$hash tags/(.*)$|) {2481return$1;2482}else{2483# catches also '$hash undefined' output2484returnundef;2485}2486}24872488## ----------------------------------------------------------------------2489## parse to hash functions24902491sub parse_date {2492my$epoch=shift;2493my$tz=shift||"-0000";24942495my%date;2496my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2497my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2498my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2499$date{'hour'} =$hour;2500$date{'minute'} =$min;2501$date{'mday'} =$mday;2502$date{'day'} =$days[$wday];2503$date{'month'} =$months[$mon];2504$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2505$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2506$date{'mday-time'} =sprintf"%d%s%02d:%02d",2507$mday,$months[$mon],$hour,$min;2508$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",25091900+$year,1+$mon,$mday,$hour,$min,$sec;25102511$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2512my$local=$epoch+ ((int$1+ ($2/60)) *3600);2513($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2514$date{'hour_local'} =$hour;2515$date{'minute_local'} =$min;2516$date{'tz_local'} =$tz;2517$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",25181900+$year,$mon+1,$mday,2519$hour,$min,$sec,$tz);2520return%date;2521}25222523sub parse_tag {2524my$tag_id=shift;2525my%tag;2526my@comment;25272528open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2529$tag{'id'} =$tag_id;2530while(my$line= <$fd>) {2531chomp$line;2532if($line=~m/^object ([0-9a-fA-F]{40})$/) {2533$tag{'object'} =$1;2534}elsif($line=~m/^type (.+)$/) {2535$tag{'type'} =$1;2536}elsif($line=~m/^tag (.+)$/) {2537$tag{'name'} =$1;2538}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2539$tag{'author'} =$1;2540$tag{'author_epoch'} =$2;2541$tag{'author_tz'} =$3;2542if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2543$tag{'author_name'} =$1;2544$tag{'author_email'} =$2;2545}else{2546$tag{'author_name'} =$tag{'author'};2547}2548}elsif($line=~m/--BEGIN/) {2549push@comment,$line;2550last;2551}elsif($lineeq"") {2552last;2553}2554}2555push@comment, <$fd>;2556$tag{'comment'} = \@comment;2557close$fdorreturn;2558if(!defined$tag{'name'}) {2559return2560};2561return%tag2562}25632564sub parse_commit_text {2565my($commit_text,$withparents) =@_;2566my@commit_lines=split'\n',$commit_text;2567my%co;25682569pop@commit_lines;# Remove '\0'25702571if(!@commit_lines) {2572return;2573}25742575my$header=shift@commit_lines;2576if($header!~m/^[0-9a-fA-F]{40}/) {2577return;2578}2579($co{'id'},my@parents) =split' ',$header;2580while(my$line=shift@commit_lines) {2581last if$lineeq"\n";2582if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2583$co{'tree'} =$1;2584}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2585push@parents,$1;2586}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2587$co{'author'} = to_utf8($1);2588$co{'author_epoch'} =$2;2589$co{'author_tz'} =$3;2590if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2591$co{'author_name'} =$1;2592$co{'author_email'} =$2;2593}else{2594$co{'author_name'} =$co{'author'};2595}2596}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2597$co{'committer'} = to_utf8($1);2598$co{'committer_epoch'} =$2;2599$co{'committer_tz'} =$3;2600if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2601$co{'committer_name'} =$1;2602$co{'committer_email'} =$2;2603}else{2604$co{'committer_name'} =$co{'committer'};2605}2606}2607}2608if(!defined$co{'tree'}) {2609return;2610};2611$co{'parents'} = \@parents;2612$co{'parent'} =$parents[0];26132614foreachmy$title(@commit_lines) {2615$title=~s/^ //;2616if($titlene"") {2617$co{'title'} = chop_str($title,80,5);2618# remove leading stuff of merges to make the interesting part visible2619if(length($title) >50) {2620$title=~s/^Automatic //;2621$title=~s/^merge (of|with) /Merge ... /i;2622if(length($title) >50) {2623$title=~s/(http|rsync):\/\///;2624}2625if(length($title) >50) {2626$title=~s/(master|www|rsync)\.//;2627}2628if(length($title) >50) {2629$title=~s/kernel.org:?//;2630}2631if(length($title) >50) {2632$title=~s/\/pub\/scm//;2633}2634}2635$co{'title_short'} = chop_str($title,50,5);2636last;2637}2638}2639if(!defined$co{'title'} ||$co{'title'}eq"") {2640$co{'title'} =$co{'title_short'} ='(no commit message)';2641}2642# remove added spaces2643foreachmy$line(@commit_lines) {2644$line=~s/^ //;2645}2646$co{'comment'} = \@commit_lines;26472648my$age=time-$co{'committer_epoch'};2649$co{'age'} =$age;2650$co{'age_string'} = age_string($age);2651my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2652if($age>60*60*24*7*2) {2653$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2654$co{'age_string_age'} =$co{'age_string'};2655}else{2656$co{'age_string_date'} =$co{'age_string'};2657$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2658}2659return%co;2660}26612662sub parse_commit {2663my($commit_id) =@_;2664my%co;26652666local$/="\0";26672668open my$fd,"-|", git_cmd(),"rev-list",2669"--parents",2670"--header",2671"--max-count=1",2672$commit_id,2673"--",2674or die_error(500,"Open git-rev-list failed");2675%co= parse_commit_text(<$fd>,1);2676close$fd;26772678return%co;2679}26802681sub parse_commits {2682my($commit_id,$maxcount,$skip,$filename,@args) =@_;2683my@cos;26842685$maxcount||=1;2686$skip||=0;26872688local$/="\0";26892690open my$fd,"-|", git_cmd(),"rev-list",2691"--header",2692@args,2693("--max-count=".$maxcount),2694("--skip=".$skip),2695@extra_options,2696$commit_id,2697"--",2698($filename? ($filename) : ())2699or die_error(500,"Open git-rev-list failed");2700while(my$line= <$fd>) {2701my%co= parse_commit_text($line);2702push@cos, \%co;2703}2704close$fd;27052706returnwantarray?@cos: \@cos;2707}27082709# parse line of git-diff-tree "raw" output2710sub parse_difftree_raw_line {2711my$line=shift;2712my%res;27132714# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2715# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2716if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2717$res{'from_mode'} =$1;2718$res{'to_mode'} =$2;2719$res{'from_id'} =$3;2720$res{'to_id'} =$4;2721$res{'status'} =$5;2722$res{'similarity'} =$6;2723if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2724($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2725}else{2726$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2727}2728}2729# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2730# combined diff (for merge commit)2731elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2732$res{'nparents'} =length($1);2733$res{'from_mode'} = [split(' ',$2) ];2734$res{'to_mode'} =pop@{$res{'from_mode'}};2735$res{'from_id'} = [split(' ',$3) ];2736$res{'to_id'} =pop@{$res{'from_id'}};2737$res{'status'} = [split('',$4) ];2738$res{'to_file'} = unquote($5);2739}2740# 'c512b523472485aef4fff9e57b229d9d243c967f'2741elsif($line=~m/^([0-9a-fA-F]{40})$/) {2742$res{'commit'} =$1;2743}27442745returnwantarray?%res: \%res;2746}27472748# wrapper: return parsed line of git-diff-tree "raw" output2749# (the argument might be raw line, or parsed info)2750sub parsed_difftree_line {2751my$line_or_ref=shift;27522753if(ref($line_or_ref)eq"HASH") {2754# pre-parsed (or generated by hand)2755return$line_or_ref;2756}else{2757return parse_difftree_raw_line($line_or_ref);2758}2759}27602761# parse line of git-ls-tree output2762sub parse_ls_tree_line {2763my$line=shift;2764my%opts=@_;2765my%res;27662767#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2768$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;27692770$res{'mode'} =$1;2771$res{'type'} =$2;2772$res{'hash'} =$3;2773if($opts{'-z'}) {2774$res{'name'} =$4;2775}else{2776$res{'name'} = unquote($4);2777}27782779returnwantarray?%res: \%res;2780}27812782# generates _two_ hashes, references to which are passed as 2 and 3 argument2783sub parse_from_to_diffinfo {2784my($diffinfo,$from,$to,@parents) =@_;27852786if($diffinfo->{'nparents'}) {2787# combined diff2788$from->{'file'} = [];2789$from->{'href'} = [];2790 fill_from_file_info($diffinfo,@parents)2791unlessexists$diffinfo->{'from_file'};2792for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2793$from->{'file'}[$i] =2794defined$diffinfo->{'from_file'}[$i] ?2795$diffinfo->{'from_file'}[$i] :2796$diffinfo->{'to_file'};2797if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2798$from->{'href'}[$i] = href(action=>"blob",2799 hash_base=>$parents[$i],2800 hash=>$diffinfo->{'from_id'}[$i],2801 file_name=>$from->{'file'}[$i]);2802}else{2803$from->{'href'}[$i] =undef;2804}2805}2806}else{2807# ordinary (not combined) diff2808$from->{'file'} =$diffinfo->{'from_file'};2809if($diffinfo->{'status'}ne"A") {# not new (added) file2810$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2811 hash=>$diffinfo->{'from_id'},2812 file_name=>$from->{'file'});2813}else{2814delete$from->{'href'};2815}2816}28172818$to->{'file'} =$diffinfo->{'to_file'};2819if(!is_deleted($diffinfo)) {# file exists in result2820$to->{'href'} = href(action=>"blob", hash_base=>$hash,2821 hash=>$diffinfo->{'to_id'},2822 file_name=>$to->{'file'});2823}else{2824delete$to->{'href'};2825}2826}28272828## ......................................................................2829## parse to array of hashes functions28302831sub git_get_heads_list {2832my$limit=shift;2833my@headslist;28342835open my$fd,'-|', git_cmd(),'for-each-ref',2836($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2837'--format=%(objectname) %(refname) %(subject)%00%(committer)',2838'refs/heads'2839orreturn;2840while(my$line= <$fd>) {2841my%ref_item;28422843chomp$line;2844my($refinfo,$committerinfo) =split(/\0/,$line);2845my($hash,$name,$title) =split(' ',$refinfo,3);2846my($committer,$epoch,$tz) =2847($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2848$ref_item{'fullname'} =$name;2849$name=~s!^refs/heads/!!;28502851$ref_item{'name'} =$name;2852$ref_item{'id'} =$hash;2853$ref_item{'title'} =$title||'(no commit message)';2854$ref_item{'epoch'} =$epoch;2855if($epoch) {2856$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2857}else{2858$ref_item{'age'} ="unknown";2859}28602861push@headslist, \%ref_item;2862}2863close$fd;28642865returnwantarray?@headslist: \@headslist;2866}28672868sub git_get_tags_list {2869my$limit=shift;2870my@tagslist;28712872open my$fd,'-|', git_cmd(),'for-each-ref',2873($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2874'--format=%(objectname) %(objecttype) %(refname) '.2875'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2876'refs/tags'2877orreturn;2878while(my$line= <$fd>) {2879my%ref_item;28802881chomp$line;2882my($refinfo,$creatorinfo) =split(/\0/,$line);2883my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2884my($creator,$epoch,$tz) =2885($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2886$ref_item{'fullname'} =$name;2887$name=~s!^refs/tags/!!;28882889$ref_item{'type'} =$type;2890$ref_item{'id'} =$id;2891$ref_item{'name'} =$name;2892if($typeeq"tag") {2893$ref_item{'subject'} =$title;2894$ref_item{'reftype'} =$reftype;2895$ref_item{'refid'} =$refid;2896}else{2897$ref_item{'reftype'} =$type;2898$ref_item{'refid'} =$id;2899}29002901if($typeeq"tag"||$typeeq"commit") {2902$ref_item{'epoch'} =$epoch;2903if($epoch) {2904$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2905}else{2906$ref_item{'age'} ="unknown";2907}2908}29092910push@tagslist, \%ref_item;2911}2912close$fd;29132914returnwantarray?@tagslist: \@tagslist;2915}29162917## ----------------------------------------------------------------------2918## filesystem-related functions29192920sub get_file_owner {2921my$path=shift;29222923my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2924my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2925if(!defined$gcos) {2926returnundef;2927}2928my$owner=$gcos;2929$owner=~s/[,;].*$//;2930return to_utf8($owner);2931}29322933# assume that file exists2934sub insert_file {2935my$filename=shift;29362937open my$fd,'<',$filename;2938print map{ to_utf8($_) } <$fd>;2939close$fd;2940}29412942## ......................................................................2943## mimetype related functions29442945sub mimetype_guess_file {2946my$filename=shift;2947my$mimemap=shift;2948-r $mimemaporreturnundef;29492950my%mimemap;2951open(my$mh,'<',$mimemap)orreturnundef;2952while(<$mh>) {2953next ifm/^#/;# skip comments2954my($mimetype,$exts) =split(/\t+/);2955if(defined$exts) {2956my@exts=split(/\s+/,$exts);2957foreachmy$ext(@exts) {2958$mimemap{$ext} =$mimetype;2959}2960}2961}2962close($mh);29632964$filename=~/\.([^.]*)$/;2965return$mimemap{$1};2966}29672968sub mimetype_guess {2969my$filename=shift;2970my$mime;2971$filename=~/\./orreturnundef;29722973if($mimetypes_file) {2974my$file=$mimetypes_file;2975if($file!~m!^/!) {# if it is relative path2976# it is relative to project2977$file="$projectroot/$project/$file";2978}2979$mime= mimetype_guess_file($filename,$file);2980}2981$mime||= mimetype_guess_file($filename,'/etc/mime.types');2982return$mime;2983}29842985sub blob_mimetype {2986my$fd=shift;2987my$filename=shift;29882989if($filename) {2990my$mime= mimetype_guess($filename);2991$mimeandreturn$mime;2992}29932994# just in case2995return$default_blob_plain_mimetypeunless$fd;29962997if(-T $fd) {2998return'text/plain';2999}elsif(!$filename) {3000return'application/octet-stream';3001}elsif($filename=~m/\.png$/i) {3002return'image/png';3003}elsif($filename=~m/\.gif$/i) {3004return'image/gif';3005}elsif($filename=~m/\.jpe?g$/i) {3006return'image/jpeg';3007}else{3008return'application/octet-stream';3009}3010}30113012sub blob_contenttype {3013my($fd,$file_name,$type) =@_;30143015$type||= blob_mimetype($fd,$file_name);3016if($typeeq'text/plain'&&defined$default_text_plain_charset) {3017$type.="; charset=$default_text_plain_charset";3018}30193020return$type;3021}30223023## ======================================================================3024## functions printing HTML: header, footer, error page30253026sub git_header_html {3027my$status=shift||"200 OK";3028my$expires=shift;30293030my$title="$site_name";3031if(defined$project) {3032$title.=" - ". to_utf8($project);3033if(defined$action) {3034$title.="/$action";3035if(defined$file_name) {3036$title.=" - ". esc_path($file_name);3037if($actioneq"tree"&&$file_name!~ m|/$|) {3038$title.="/";3039}3040}3041}3042}3043my$content_type;3044# require explicit support from the UA if we are to send the page as3045# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3046# we have to do this because MSIE sometimes globs '*/*', pretending to3047# support xhtml+xml but choking when it gets what it asked for.3048if(defined$cgi->http('HTTP_ACCEPT') &&3049$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3050$cgi->Accept('application/xhtml+xml') !=0) {3051$content_type='application/xhtml+xml';3052}else{3053$content_type='text/html';3054}3055print$cgi->header(-type=>$content_type, -charset =>'utf-8',3056-status=>$status, -expires =>$expires);3057my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3058print<<EOF;3059<?xml version="1.0" encoding="utf-8"?>3060<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3061<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3062<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3063<!-- git core binaries version$git_version-->3064<head>3065<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3066<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3067<meta name="robots" content="index, nofollow"/>3068<title>$title</title>3069EOF3070# the stylesheet, favicon etc urls won't work correctly with path_info3071# unless we set the appropriate base URL3072if($ENV{'PATH_INFO'}) {3073print"<base href=\"".esc_url($base_url)."\"/>\n";3074}3075# print out each stylesheet that exist, providing backwards capability3076# for those people who defined $stylesheet in a config file3077if(defined$stylesheet) {3078print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3079}else{3080foreachmy$stylesheet(@stylesheets) {3081next unless$stylesheet;3082print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";3083}3084}3085if(defined$project) {3086my%href_params= get_feed_info();3087if(!exists$href_params{'-title'}) {3088$href_params{'-title'} ='log';3089}30903091foreachmy$formatqw(RSS Atom){3092my$type=lc($format);3093my%link_attr= (3094'-rel'=>'alternate',3095'-title'=>"$project-$href_params{'-title'} -$formatfeed",3096'-type'=>"application/$type+xml"3097);30983099$href_params{'action'} =$type;3100$link_attr{'-href'} = href(%href_params);3101print"<link ".3102"rel=\"$link_attr{'-rel'}\"".3103"title=\"$link_attr{'-title'}\"".3104"href=\"$link_attr{'-href'}\"".3105"type=\"$link_attr{'-type'}\"".3106"/>\n";31073108$href_params{'extra_options'} ='--no-merges';3109$link_attr{'-href'} = href(%href_params);3110$link_attr{'-title'} .=' (no merges)';3111print"<link ".3112"rel=\"$link_attr{'-rel'}\"".3113"title=\"$link_attr{'-title'}\"".3114"href=\"$link_attr{'-href'}\"".3115"type=\"$link_attr{'-type'}\"".3116"/>\n";3117}31183119}else{3120printf('<link rel="alternate" title="%sprojects list" '.3121'href="%s" type="text/plain; charset=utf-8" />'."\n",3122$site_name, href(project=>undef, action=>"project_index"));3123printf('<link rel="alternate" title="%sprojects feeds" '.3124'href="%s" type="text/x-opml" />'."\n",3125$site_name, href(project=>undef, action=>"opml"));3126}3127if(defined$favicon) {3128printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);3129}31303131print"</head>\n".3132"<body>\n";31333134if(-f $site_header) {3135 insert_file($site_header);3136}31373138print"<div class=\"page_header\">\n".3139$cgi->a({-href => esc_url($logo_url),3140-title =>$logo_label},3141qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));3142print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3143if(defined$project) {3144print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3145if(defined$action) {3146print" /$action";3147}3148print"\n";3149}3150print"</div>\n";31513152my$have_search= gitweb_check_feature('search');3153if(defined$project&&$have_search) {3154if(!defined$searchtext) {3155$searchtext="";3156}3157my$search_hash;3158if(defined$hash_base) {3159$search_hash=$hash_base;3160}elsif(defined$hash) {3161$search_hash=$hash;3162}else{3163$search_hash="HEAD";3164}3165my$action=$my_uri;3166my$use_pathinfo= gitweb_check_feature('pathinfo');3167if($use_pathinfo) {3168$action.="/".esc_url($project);3169}3170print$cgi->startform(-method=>"get", -action =>$action) .3171"<div class=\"search\">\n".3172(!$use_pathinfo&&3173$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3174$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3175$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3176$cgi->popup_menu(-name =>'st', -default=>'commit',3177-values=> ['commit','grep','author','committer','pickaxe']) .3178$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3179" search:\n",3180$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3181"<span title=\"Extended regular expression\">".3182$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3183-checked =>$search_use_regexp) .3184"</span>".3185"</div>".3186$cgi->end_form() ."\n";3187}3188}31893190sub git_footer_html {3191my$feed_class='rss_logo';31923193print"<div class=\"page_footer\">\n";3194if(defined$project) {3195my$descr= git_get_project_description($project);3196if(defined$descr) {3197print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3198}31993200my%href_params= get_feed_info();3201if(!%href_params) {3202$feed_class.=' generic';3203}3204$href_params{'-title'} ||='log';32053206foreachmy$formatqw(RSS Atom){3207$href_params{'action'} =lc($format);3208print$cgi->a({-href => href(%href_params),3209-title =>"$href_params{'-title'}$formatfeed",3210-class=>$feed_class},$format)."\n";3211}32123213}else{3214print$cgi->a({-href => href(project=>undef, action=>"opml"),3215-class=>$feed_class},"OPML") ." ";3216print$cgi->a({-href => href(project=>undef, action=>"project_index"),3217-class=>$feed_class},"TXT") ."\n";3218}3219print"</div>\n";# class="page_footer"32203221if(-f $site_footer) {3222 insert_file($site_footer);3223}32243225print"</body>\n".3226"</html>";3227}32283229# die_error(<http_status_code>, <error_message>)3230# Example: die_error(404, 'Hash not found')3231# By convention, use the following status codes (as defined in RFC 2616):3232# 400: Invalid or missing CGI parameters, or3233# requested object exists but has wrong type.3234# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3235# this server or project.3236# 404: Requested object/revision/project doesn't exist.3237# 500: The server isn't configured properly, or3238# an internal error occurred (e.g. failed assertions caused by bugs), or3239# an unknown error occurred (e.g. the git binary died unexpectedly).3240sub die_error {3241my$status=shift||500;3242my$error=shift||"Internal server error";32433244my%http_responses= (400=>'400 Bad Request',3245403=>'403 Forbidden',3246404=>'404 Not Found',3247500=>'500 Internal Server Error');3248 git_header_html($http_responses{$status});3249print<<EOF;3250<div class="page_body">3251<br /><br />3252$status-$error3253<br />3254</div>3255EOF3256 git_footer_html();3257exit;3258}32593260## ----------------------------------------------------------------------3261## functions printing or outputting HTML: navigation32623263sub git_print_page_nav {3264my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3265$extra=''if!defined$extra;# pager or formats32663267my@navs=qw(summary shortlog log commit commitdiff tree);3268if($suppress) {3269@navs=grep{$_ne$suppress}@navs;3270}32713272my%arg=map{$_=> {action=>$_} }@navs;3273if(defined$head) {3274for(qw(commit commitdiff)) {3275$arg{$_}{'hash'} =$head;3276}3277if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3278for(qw(shortlog log)) {3279$arg{$_}{'hash'} =$head;3280}3281}3282}32833284$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3285$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;32863287my@actions= gitweb_get_feature('actions');3288my%repl= (3289'%'=>'%',3290'n'=>$project,# project name3291'f'=>$git_dir,# project path within filesystem3292'h'=>$treehead||'',# current hash ('h' parameter)3293'b'=>$treebase||'',# hash base ('hb' parameter)3294);3295while(@actions) {3296my($label,$link,$pos) =splice(@actions,0,3);3297# insert3298@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3299# munch munch3300$link=~s/%([%nfhb])/$repl{$1}/g;3301$arg{$label}{'_href'} =$link;3302}33033304print"<div class=\"page_nav\">\n".3305(join" | ",3306map{$_eq$current?3307$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3308}@navs);3309print"<br/>\n$extra<br/>\n".3310"</div>\n";3311}33123313sub format_paging_nav {3314my($action,$hash,$head,$page,$has_next_link) =@_;3315my$paging_nav;331633173318if($hashne$head||$page) {3319$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3320}else{3321$paging_nav.="HEAD";3322}33233324if($page>0) {3325$paging_nav.=" ⋅ ".3326$cgi->a({-href => href(-replay=>1, page=>$page-1),3327-accesskey =>"p", -title =>"Alt-p"},"prev");3328}else{3329$paging_nav.=" ⋅ prev";3330}33313332if($has_next_link) {3333$paging_nav.=" ⋅ ".3334$cgi->a({-href => href(-replay=>1, page=>$page+1),3335-accesskey =>"n", -title =>"Alt-n"},"next");3336}else{3337$paging_nav.=" ⋅ next";3338}33393340return$paging_nav;3341}33423343## ......................................................................3344## functions printing or outputting HTML: div33453346sub git_print_header_div {3347my($action,$title,$hash,$hash_base) =@_;3348my%args= ();33493350$args{'action'} =$action;3351$args{'hash'} =$hashif$hash;3352$args{'hash_base'} =$hash_baseif$hash_base;33533354print"<div class=\"header\">\n".3355$cgi->a({-href => href(%args), -class=>"title"},3356$title?$title:$action) .3357"\n</div>\n";3358}33593360sub print_local_time {3361my%date=@_;3362if($date{'hour_local'} <6) {3363printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3364$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3365}else{3366printf(" (%02d:%02d%s)",3367$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3368}3369}33703371# Outputs the author name and date in long form3372sub git_print_authorship {3373my$co=shift;3374my%opts=@_;3375my$tag=$opts{-tag} ||'div';33763377my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3378print"<$tagclass=\"author_date\">".3379 esc_html($co->{'author_name'}) .3380" [$ad{'rfc2822'}";3381 print_local_time(%ad)if($opts{-localtime});3382print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3383."</$tag>\n";3384}33853386# Outputs table rows containing the full author or committer information,3387# in the format expected for 'commit' view (& similia).3388# Parameters are a commit hash reference, followed by the list of people3389# to output information for. If the list is empty it defalts to both3390# author and committer.3391sub git_print_authorship_rows {3392my$co=shift;3393# too bad we can't use @people = @_ || ('author', 'committer')3394my@people=@_;3395@people= ('author','committer')unless@people;3396foreachmy$who(@people) {3397my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3398print"<tr><td>$who</td><td>". esc_html($co->{$who}) ."</td>".3399"<td rowspan=\"2\">".3400 git_get_avatar($co->{"${who}_email"}, -size =>'double') .3401"</td></tr>\n".3402"<tr>".3403"<td></td><td>$wd{'rfc2822'}";3404 print_local_time(%wd);3405print"</td>".3406"</tr>\n";3407}3408}34093410sub git_print_page_path {3411my$name=shift;3412my$type=shift;3413my$hb=shift;341434153416print"<div class=\"page_path\">";3417print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3418-title =>'tree root'}, to_utf8("[$project]"));3419print" / ";3420if(defined$name) {3421my@dirname=split'/',$name;3422my$basename=pop@dirname;3423my$fullname='';34243425foreachmy$dir(@dirname) {3426$fullname.= ($fullname?'/':'') .$dir;3427print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3428 hash_base=>$hb),3429-title =>$fullname}, esc_path($dir));3430print" / ";3431}3432if(defined$type&&$typeeq'blob') {3433print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3434 hash_base=>$hb),3435-title =>$name}, esc_path($basename));3436}elsif(defined$type&&$typeeq'tree') {3437print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3438 hash_base=>$hb),3439-title =>$name}, esc_path($basename));3440print" / ";3441}else{3442print esc_path($basename);3443}3444}3445print"<br/></div>\n";3446}34473448sub git_print_log {3449my$log=shift;3450my%opts=@_;34513452if($opts{'-remove_title'}) {3453# remove title, i.e. first line of log3454shift@$log;3455}3456# remove leading empty lines3457while(defined$log->[0] &&$log->[0]eq"") {3458shift@$log;3459}34603461# print log3462my$signoff=0;3463my$empty=0;3464foreachmy$line(@$log) {3465if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3466$signoff=1;3467$empty=0;3468if(!$opts{'-remove_signoff'}) {3469print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3470next;3471}else{3472# remove signoff lines3473next;3474}3475}else{3476$signoff=0;3477}34783479# print only one empty line3480# do not print empty line after signoff3481if($lineeq"") {3482next if($empty||$signoff);3483$empty=1;3484}else{3485$empty=0;3486}34873488print format_log_line_html($line) ."<br/>\n";3489}34903491if($opts{'-final_empty_line'}) {3492# end with single empty line3493print"<br/>\n"unless$empty;3494}3495}34963497# return link target (what link points to)3498sub git_get_link_target {3499my$hash=shift;3500my$link_target;35013502# read link3503open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3504orreturn;3505{3506local$/=undef;3507$link_target= <$fd>;3508}3509close$fd3510orreturn;35113512return$link_target;3513}35143515# given link target, and the directory (basedir) the link is in,3516# return target of link relative to top directory (top tree);3517# return undef if it is not possible (including absolute links).3518sub normalize_link_target {3519my($link_target,$basedir) =@_;35203521# absolute symlinks (beginning with '/') cannot be normalized3522return if(substr($link_target,0,1)eq'/');35233524# normalize link target to path from top (root) tree (dir)3525my$path;3526if($basedir) {3527$path=$basedir.'/'.$link_target;3528}else{3529# we are in top (root) tree (dir)3530$path=$link_target;3531}35323533# remove //, /./, and /../3534my@path_parts;3535foreachmy$part(split('/',$path)) {3536# discard '.' and ''3537next if(!$part||$parteq'.');3538# handle '..'3539if($parteq'..') {3540if(@path_parts) {3541pop@path_parts;3542}else{3543# link leads outside repository (outside top dir)3544return;3545}3546}else{3547push@path_parts,$part;3548}3549}3550$path=join('/',@path_parts);35513552return$path;3553}35543555# print tree entry (row of git_tree), but without encompassing <tr> element3556sub git_print_tree_entry {3557my($t,$basedir,$hash_base,$have_blame) =@_;35583559my%base_key= ();3560$base_key{'hash_base'} =$hash_baseifdefined$hash_base;35613562# The format of a table row is: mode list link. Where mode is3563# the mode of the entry, list is the name of the entry, an href,3564# and link is the action links of the entry.35653566print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3567if($t->{'type'}eq"blob") {3568print"<td class=\"list\">".3569$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3570 file_name=>"$basedir$t->{'name'}",%base_key),3571-class=>"list"}, esc_path($t->{'name'}));3572if(S_ISLNK(oct$t->{'mode'})) {3573my$link_target= git_get_link_target($t->{'hash'});3574if($link_target) {3575my$norm_target= normalize_link_target($link_target,$basedir);3576if(defined$norm_target) {3577print" -> ".3578$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3579 file_name=>$norm_target),3580-title =>$norm_target}, esc_path($link_target));3581}else{3582print" -> ". esc_path($link_target);3583}3584}3585}3586print"</td>\n";3587print"<td class=\"link\">";3588print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3589 file_name=>"$basedir$t->{'name'}",%base_key)},3590"blob");3591if($have_blame) {3592print" | ".3593$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3594 file_name=>"$basedir$t->{'name'}",%base_key)},3595"blame");3596}3597if(defined$hash_base) {3598print" | ".3599$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3600 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3601"history");3602}3603print" | ".3604$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3605 file_name=>"$basedir$t->{'name'}")},3606"raw");3607print"</td>\n";36083609}elsif($t->{'type'}eq"tree") {3610print"<td class=\"list\">";3611print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3612 file_name=>"$basedir$t->{'name'}",%base_key)},3613 esc_path($t->{'name'}));3614print"</td>\n";3615print"<td class=\"link\">";3616print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3617 file_name=>"$basedir$t->{'name'}",%base_key)},3618"tree");3619if(defined$hash_base) {3620print" | ".3621$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3622 file_name=>"$basedir$t->{'name'}")},3623"history");3624}3625print"</td>\n";3626}else{3627# unknown object: we can only present history for it3628# (this includes 'commit' object, i.e. submodule support)3629print"<td class=\"list\">".3630 esc_path($t->{'name'}) .3631"</td>\n";3632print"<td class=\"link\">";3633if(defined$hash_base) {3634print$cgi->a({-href => href(action=>"history",3635 hash_base=>$hash_base,3636 file_name=>"$basedir$t->{'name'}")},3637"history");3638}3639print"</td>\n";3640}3641}36423643## ......................................................................3644## functions printing large fragments of HTML36453646# get pre-image filenames for merge (combined) diff3647sub fill_from_file_info {3648my($diff,@parents) =@_;36493650$diff->{'from_file'} = [ ];3651$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3652for(my$i=0;$i<$diff->{'nparents'};$i++) {3653if($diff->{'status'}[$i]eq'R'||3654$diff->{'status'}[$i]eq'C') {3655$diff->{'from_file'}[$i] =3656 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3657}3658}36593660return$diff;3661}36623663# is current raw difftree line of file deletion3664sub is_deleted {3665my$diffinfo=shift;36663667return$diffinfo->{'to_id'}eq('0' x 40);3668}36693670# does patch correspond to [previous] difftree raw line3671# $diffinfo - hashref of parsed raw diff format3672# $patchinfo - hashref of parsed patch diff format3673# (the same keys as in $diffinfo)3674sub is_patch_split {3675my($diffinfo,$patchinfo) =@_;36763677returndefined$diffinfo&&defined$patchinfo3678&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3679}368036813682sub git_difftree_body {3683my($difftree,$hash,@parents) =@_;3684my($parent) =$parents[0];3685my$have_blame= gitweb_check_feature('blame');3686print"<div class=\"list_head\">\n";3687if($#{$difftree} >10) {3688print(($#{$difftree} +1) ." files changed:\n");3689}3690print"</div>\n";36913692print"<table class=\"".3693(@parents>1?"combined ":"") .3694"diff_tree\">\n";36953696# header only for combined diff in 'commitdiff' view3697my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3698if($has_header) {3699# table header3700print"<thead><tr>\n".3701"<th></th><th></th>\n";# filename, patchN link3702for(my$i=0;$i<@parents;$i++) {3703my$par=$parents[$i];3704print"<th>".3705$cgi->a({-href => href(action=>"commitdiff",3706 hash=>$hash, hash_parent=>$par),3707-title =>'commitdiff to parent number '.3708($i+1) .': '.substr($par,0,7)},3709$i+1) .3710" </th>\n";3711}3712print"</tr></thead>\n<tbody>\n";3713}37143715my$alternate=1;3716my$patchno=0;3717foreachmy$line(@{$difftree}) {3718my$diff= parsed_difftree_line($line);37193720if($alternate) {3721print"<tr class=\"dark\">\n";3722}else{3723print"<tr class=\"light\">\n";3724}3725$alternate^=1;37263727if(exists$diff->{'nparents'}) {# combined diff37283729 fill_from_file_info($diff,@parents)3730unlessexists$diff->{'from_file'};37313732if(!is_deleted($diff)) {3733# file exists in the result (child) commit3734print"<td>".3735$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3736 file_name=>$diff->{'to_file'},3737 hash_base=>$hash),3738-class=>"list"}, esc_path($diff->{'to_file'})) .3739"</td>\n";3740}else{3741print"<td>".3742 esc_path($diff->{'to_file'}) .3743"</td>\n";3744}37453746if($actioneq'commitdiff') {3747# link to patch3748$patchno++;3749print"<td class=\"link\">".3750$cgi->a({-href =>"#patch$patchno"},"patch") .3751" | ".3752"</td>\n";3753}37543755my$has_history=0;3756my$not_deleted=0;3757for(my$i=0;$i<$diff->{'nparents'};$i++) {3758my$hash_parent=$parents[$i];3759my$from_hash=$diff->{'from_id'}[$i];3760my$from_path=$diff->{'from_file'}[$i];3761my$status=$diff->{'status'}[$i];37623763$has_history||= ($statusne'A');3764$not_deleted||= ($statusne'D');37653766if($statuseq'A') {3767print"<td class=\"link\"align=\"right\"> | </td>\n";3768}elsif($statuseq'D') {3769print"<td class=\"link\">".3770$cgi->a({-href => href(action=>"blob",3771 hash_base=>$hash,3772 hash=>$from_hash,3773 file_name=>$from_path)},3774"blob". ($i+1)) .3775" | </td>\n";3776}else{3777if($diff->{'to_id'}eq$from_hash) {3778print"<td class=\"link nochange\">";3779}else{3780print"<td class=\"link\">";3781}3782print$cgi->a({-href => href(action=>"blobdiff",3783 hash=>$diff->{'to_id'},3784 hash_parent=>$from_hash,3785 hash_base=>$hash,3786 hash_parent_base=>$hash_parent,3787 file_name=>$diff->{'to_file'},3788 file_parent=>$from_path)},3789"diff". ($i+1)) .3790" | </td>\n";3791}3792}37933794print"<td class=\"link\">";3795if($not_deleted) {3796print$cgi->a({-href => href(action=>"blob",3797 hash=>$diff->{'to_id'},3798 file_name=>$diff->{'to_file'},3799 hash_base=>$hash)},3800"blob");3801print" | "if($has_history);3802}3803if($has_history) {3804print$cgi->a({-href => href(action=>"history",3805 file_name=>$diff->{'to_file'},3806 hash_base=>$hash)},3807"history");3808}3809print"</td>\n";38103811print"</tr>\n";3812next;# instead of 'else' clause, to avoid extra indent3813}3814# else ordinary diff38153816my($to_mode_oct,$to_mode_str,$to_file_type);3817my($from_mode_oct,$from_mode_str,$from_file_type);3818if($diff->{'to_mode'}ne('0' x 6)) {3819$to_mode_oct=oct$diff->{'to_mode'};3820if(S_ISREG($to_mode_oct)) {# only for regular file3821$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3822}3823$to_file_type= file_type($diff->{'to_mode'});3824}3825if($diff->{'from_mode'}ne('0' x 6)) {3826$from_mode_oct=oct$diff->{'from_mode'};3827if(S_ISREG($to_mode_oct)) {# only for regular file3828$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3829}3830$from_file_type= file_type($diff->{'from_mode'});3831}38323833if($diff->{'status'}eq"A") {# created3834my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3835$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3836$mode_chng.="]</span>";3837print"<td>";3838print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3839 hash_base=>$hash, file_name=>$diff->{'file'}),3840-class=>"list"}, esc_path($diff->{'file'}));3841print"</td>\n";3842print"<td>$mode_chng</td>\n";3843print"<td class=\"link\">";3844if($actioneq'commitdiff') {3845# link to patch3846$patchno++;3847print$cgi->a({-href =>"#patch$patchno"},"patch");3848print" | ";3849}3850print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3851 hash_base=>$hash, file_name=>$diff->{'file'})},3852"blob");3853print"</td>\n";38543855}elsif($diff->{'status'}eq"D") {# deleted3856my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3857print"<td>";3858print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3859 hash_base=>$parent, file_name=>$diff->{'file'}),3860-class=>"list"}, esc_path($diff->{'file'}));3861print"</td>\n";3862print"<td>$mode_chng</td>\n";3863print"<td class=\"link\">";3864if($actioneq'commitdiff') {3865# link to patch3866$patchno++;3867print$cgi->a({-href =>"#patch$patchno"},"patch");3868print" | ";3869}3870print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3871 hash_base=>$parent, file_name=>$diff->{'file'})},3872"blob") ." | ";3873if($have_blame) {3874print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3875 file_name=>$diff->{'file'})},3876"blame") ." | ";3877}3878print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3879 file_name=>$diff->{'file'})},3880"history");3881print"</td>\n";38823883}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3884my$mode_chnge="";3885if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3886$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3887if($from_file_typene$to_file_type) {3888$mode_chnge.=" from$from_file_typeto$to_file_type";3889}3890if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3891if($from_mode_str&&$to_mode_str) {3892$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3893}elsif($to_mode_str) {3894$mode_chnge.=" mode:$to_mode_str";3895}3896}3897$mode_chnge.="]</span>\n";3898}3899print"<td>";3900print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3901 hash_base=>$hash, file_name=>$diff->{'file'}),3902-class=>"list"}, esc_path($diff->{'file'}));3903print"</td>\n";3904print"<td>$mode_chnge</td>\n";3905print"<td class=\"link\">";3906if($actioneq'commitdiff') {3907# link to patch3908$patchno++;3909print$cgi->a({-href =>"#patch$patchno"},"patch") .3910" | ";3911}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3912# "commit" view and modified file (not onlu mode changed)3913print$cgi->a({-href => href(action=>"blobdiff",3914 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3915 hash_base=>$hash, hash_parent_base=>$parent,3916 file_name=>$diff->{'file'})},3917"diff") .3918" | ";3919}3920print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3921 hash_base=>$hash, file_name=>$diff->{'file'})},3922"blob") ." | ";3923if($have_blame) {3924print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3925 file_name=>$diff->{'file'})},3926"blame") ." | ";3927}3928print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3929 file_name=>$diff->{'file'})},3930"history");3931print"</td>\n";39323933}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3934my%status_name= ('R'=>'moved','C'=>'copied');3935my$nstatus=$status_name{$diff->{'status'}};3936my$mode_chng="";3937if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3938# mode also for directories, so we cannot use $to_mode_str3939$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3940}3941print"<td>".3942$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3943 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3944-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3945"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3946$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3947 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3948-class=>"list"}, esc_path($diff->{'from_file'})) .3949" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3950"<td class=\"link\">";3951if($actioneq'commitdiff') {3952# link to patch3953$patchno++;3954print$cgi->a({-href =>"#patch$patchno"},"patch") .3955" | ";3956}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3957# "commit" view and modified file (not only pure rename or copy)3958print$cgi->a({-href => href(action=>"blobdiff",3959 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3960 hash_base=>$hash, hash_parent_base=>$parent,3961 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3962"diff") .3963" | ";3964}3965print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3966 hash_base=>$parent, file_name=>$diff->{'to_file'})},3967"blob") ." | ";3968if($have_blame) {3969print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3970 file_name=>$diff->{'to_file'})},3971"blame") ." | ";3972}3973print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3974 file_name=>$diff->{'to_file'})},3975"history");3976print"</td>\n";39773978}# we should not encounter Unmerged (U) or Unknown (X) status3979print"</tr>\n";3980}3981print"</tbody>"if$has_header;3982print"</table>\n";3983}39843985sub git_patchset_body {3986my($fd,$difftree,$hash,@hash_parents) =@_;3987my($hash_parent) =$hash_parents[0];39883989my$is_combined= (@hash_parents>1);3990my$patch_idx=0;3991my$patch_number=0;3992my$patch_line;3993my$diffinfo;3994my$to_name;3995my(%from,%to);39963997print"<div class=\"patchset\">\n";39983999# skip to first patch4000while($patch_line= <$fd>) {4001chomp$patch_line;40024003last if($patch_line=~m/^diff /);4004}40054006 PATCH:4007while($patch_line) {40084009# parse "git diff" header line4010if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4011# $1 is from_name, which we do not use4012$to_name= unquote($2);4013$to_name=~s!^b/!!;4014}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4015# $1 is 'cc' or 'combined', which we do not use4016$to_name= unquote($2);4017}else{4018$to_name=undef;4019}40204021# check if current patch belong to current raw line4022# and parse raw git-diff line if needed4023if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4024# this is continuation of a split patch4025print"<div class=\"patch cont\">\n";4026}else{4027# advance raw git-diff output if needed4028$patch_idx++ifdefined$diffinfo;40294030# read and prepare patch information4031$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);40324033# compact combined diff output can have some patches skipped4034# find which patch (using pathname of result) we are at now;4035if($is_combined) {4036while($to_namene$diffinfo->{'to_file'}) {4037print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4038 format_diff_cc_simplified($diffinfo,@hash_parents) .4039"</div>\n";# class="patch"40404041$patch_idx++;4042$patch_number++;40434044last if$patch_idx>$#$difftree;4045$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4046}4047}40484049# modifies %from, %to hashes4050 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);40514052# this is first patch for raw difftree line with $patch_idx index4053# we index @$difftree array from 0, but number patches from 14054print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4055}40564057# git diff header4058#assert($patch_line =~ m/^diff /) if DEBUG;4059#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4060$patch_number++;4061# print "git diff" header4062print format_git_diff_header_line($patch_line,$diffinfo,4063 \%from, \%to);40644065# print extended diff header4066print"<div class=\"diff extended_header\">\n";4067 EXTENDED_HEADER:4068while($patch_line= <$fd>) {4069chomp$patch_line;40704071last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);40724073print format_extended_diff_header_line($patch_line,$diffinfo,4074 \%from, \%to);4075}4076print"</div>\n";# class="diff extended_header"40774078# from-file/to-file diff header4079if(!$patch_line) {4080print"</div>\n";# class="patch"4081last PATCH;4082}4083next PATCH if($patch_line=~m/^diff /);4084#assert($patch_line =~ m/^---/) if DEBUG;40854086my$last_patch_line=$patch_line;4087$patch_line= <$fd>;4088chomp$patch_line;4089#assert($patch_line =~ m/^\+\+\+/) if DEBUG;40904091print format_diff_from_to_header($last_patch_line,$patch_line,4092$diffinfo, \%from, \%to,4093@hash_parents);40944095# the patch itself4096 LINE:4097while($patch_line= <$fd>) {4098chomp$patch_line;40994100next PATCH if($patch_line=~m/^diff /);41014102print format_diff_line($patch_line, \%from, \%to);4103}41044105}continue{4106print"</div>\n";# class="patch"4107}41084109# for compact combined (--cc) format, with chunk and patch simpliciaction4110# patchset might be empty, but there might be unprocessed raw lines4111for(++$patch_idxif$patch_number>0;4112$patch_idx<@$difftree;4113++$patch_idx) {4114# read and prepare patch information4115$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);41164117# generate anchor for "patch" links in difftree / whatchanged part4118print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4119 format_diff_cc_simplified($diffinfo,@hash_parents) .4120"</div>\n";# class="patch"41214122$patch_number++;4123}41244125if($patch_number==0) {4126if(@hash_parents>1) {4127print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4128}else{4129print"<div class=\"diff nodifferences\">No differences found</div>\n";4130}4131}41324133print"</div>\n";# class="patchset"4134}41354136# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41374138# fills project list info (age, description, owner, forks) for each4139# project in the list, removing invalid projects from returned list4140# NOTE: modifies $projlist, but does not remove entries from it4141sub fill_project_list_info {4142my($projlist,$check_forks) =@_;4143my@projects;41444145my$show_ctags= gitweb_check_feature('ctags');4146 PROJECT:4147foreachmy$pr(@$projlist) {4148my(@activity) = git_get_last_activity($pr->{'path'});4149unless(@activity) {4150next PROJECT;4151}4152($pr->{'age'},$pr->{'age_string'}) =@activity;4153if(!defined$pr->{'descr'}) {4154my$descr= git_get_project_description($pr->{'path'}) ||"";4155$descr= to_utf8($descr);4156$pr->{'descr_long'} =$descr;4157$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4158}4159if(!defined$pr->{'owner'}) {4160$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4161}4162if($check_forks) {4163my$pname=$pr->{'path'};4164if(($pname=~s/\.git$//) &&4165($pname!~/\/$/) &&4166(-d "$projectroot/$pname")) {4167$pr->{'forks'} ="-d$projectroot/$pname";4168}else{4169$pr->{'forks'} =0;4170}4171}4172$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4173push@projects,$pr;4174}41754176return@projects;4177}41784179# print 'sort by' <th> element, generating 'sort by $name' replay link4180# if that order is not selected4181sub print_sort_th {4182my($name,$order,$header) =@_;4183$header||=ucfirst($name);41844185if($ordereq$name) {4186print"<th>$header</th>\n";4187}else{4188print"<th>".4189$cgi->a({-href => href(-replay=>1, order=>$name),4190-class=>"header"},$header) .4191"</th>\n";4192}4193}41944195sub git_project_list_body {4196# actually uses global variable $project4197my($projlist,$order,$from,$to,$extra,$no_header) =@_;41984199my$check_forks= gitweb_check_feature('forks');4200my@projects= fill_project_list_info($projlist,$check_forks);42014202$order||=$default_projects_order;4203$from=0unlessdefined$from;4204$to=$#projectsif(!defined$to||$#projects<$to);42054206my%order_info= (4207 project => { key =>'path', type =>'str'},4208 descr => { key =>'descr_long', type =>'str'},4209 owner => { key =>'owner', type =>'str'},4210 age => { key =>'age', type =>'num'}4211);4212my$oi=$order_info{$order};4213if($oi->{'type'}eq'str') {4214@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4215}else{4216@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4217}42184219my$show_ctags= gitweb_check_feature('ctags');4220if($show_ctags) {4221my%ctags;4222foreachmy$p(@projects) {4223foreachmy$ct(keys%{$p->{'ctags'}}) {4224$ctags{$ct} +=$p->{'ctags'}->{$ct};4225}4226}4227my$cloud= git_populate_project_tagcloud(\%ctags);4228print git_show_project_tagcloud($cloud,64);4229}42304231print"<table class=\"project_list\">\n";4232unless($no_header) {4233print"<tr>\n";4234if($check_forks) {4235print"<th></th>\n";4236}4237 print_sort_th('project',$order,'Project');4238 print_sort_th('descr',$order,'Description');4239 print_sort_th('owner',$order,'Owner');4240 print_sort_th('age',$order,'Last Change');4241print"<th></th>\n".# for links4242"</tr>\n";4243}4244my$alternate=1;4245my$tagfilter=$cgi->param('by_tag');4246for(my$i=$from;$i<=$to;$i++) {4247my$pr=$projects[$i];42484249next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4250next if$searchtextand not$pr->{'path'} =~/$searchtext/4251and not$pr->{'descr_long'} =~/$searchtext/;4252# Weed out forks or non-matching entries of search4253if($check_forks) {4254my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4255$forkbase="^$forkbase"if$forkbase;4256next ifnot$searchtextand not$tagfilterand$show_ctags4257and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4258}42594260if($alternate) {4261print"<tr class=\"dark\">\n";4262}else{4263print"<tr class=\"light\">\n";4264}4265$alternate^=1;4266if($check_forks) {4267print"<td>";4268if($pr->{'forks'}) {4269print"<!--$pr->{'forks'} -->\n";4270print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4271}4272print"</td>\n";4273}4274print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4275-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4276"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4277-class=>"list", -title =>$pr->{'descr_long'}},4278 esc_html($pr->{'descr'})) ."</td>\n".4279"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4280print"<td class=\"". age_class($pr->{'age'}) ."\">".4281(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4282"<td class=\"link\">".4283$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4284$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4285$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4286$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4287($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4288"</td>\n".4289"</tr>\n";4290}4291if(defined$extra) {4292print"<tr>\n";4293if($check_forks) {4294print"<td></td>\n";4295}4296print"<td colspan=\"5\">$extra</td>\n".4297"</tr>\n";4298}4299print"</table>\n";4300}43014302sub git_shortlog_body {4303# uses global variable $project4304my($commitlist,$from,$to,$refs,$extra) =@_;43054306$from=0unlessdefined$from;4307$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43084309print"<table class=\"shortlog\">\n";4310my$alternate=1;4311for(my$i=$from;$i<=$to;$i++) {4312my%co= %{$commitlist->[$i]};4313my$commit=$co{'id'};4314my$ref= format_ref_marker($refs,$commit);4315if($alternate) {4316print"<tr class=\"dark\">\n";4317}else{4318print"<tr class=\"light\">\n";4319}4320$alternate^=1;4321# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4322print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4323 format_author_html('td', \%co,10) ."<td>";4324print format_subject_html($co{'title'},$co{'title_short'},4325 href(action=>"commit", hash=>$commit),$ref);4326print"</td>\n".4327"<td class=\"link\">".4328$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4329$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4330$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4331my$snapshot_links= format_snapshot_links($commit);4332if(defined$snapshot_links) {4333print" | ".$snapshot_links;4334}4335print"</td>\n".4336"</tr>\n";4337}4338if(defined$extra) {4339print"<tr>\n".4340"<td colspan=\"4\">$extra</td>\n".4341"</tr>\n";4342}4343print"</table>\n";4344}43454346sub git_history_body {4347# Warning: assumes constant type (blob or tree) during history4348my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;43494350$from=0unlessdefined$from;4351$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});43524353print"<table class=\"history\">\n";4354my$alternate=1;4355for(my$i=$from;$i<=$to;$i++) {4356my%co= %{$commitlist->[$i]};4357if(!%co) {4358next;4359}4360my$commit=$co{'id'};43614362my$ref= format_ref_marker($refs,$commit);43634364if($alternate) {4365print"<tr class=\"dark\">\n";4366}else{4367print"<tr class=\"light\">\n";4368}4369$alternate^=1;4370print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4371# shortlog: format_author_html('td', \%co, 10)4372 format_author_html('td', \%co,15,3) ."<td>";4373# originally git_history used chop_str($co{'title'}, 50)4374print format_subject_html($co{'title'},$co{'title_short'},4375 href(action=>"commit", hash=>$commit),$ref);4376print"</td>\n".4377"<td class=\"link\">".4378$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4379$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");43804381if($ftypeeq'blob') {4382my$blob_current= git_get_hash_by_path($hash_base,$file_name);4383my$blob_parent= git_get_hash_by_path($commit,$file_name);4384if(defined$blob_current&&defined$blob_parent&&4385$blob_currentne$blob_parent) {4386print" | ".4387$cgi->a({-href => href(action=>"blobdiff",4388 hash=>$blob_current, hash_parent=>$blob_parent,4389 hash_base=>$hash_base, hash_parent_base=>$commit,4390 file_name=>$file_name)},4391"diff to current");4392}4393}4394print"</td>\n".4395"</tr>\n";4396}4397if(defined$extra) {4398print"<tr>\n".4399"<td colspan=\"4\">$extra</td>\n".4400"</tr>\n";4401}4402print"</table>\n";4403}44044405sub git_tags_body {4406# uses global variable $project4407my($taglist,$from,$to,$extra) =@_;4408$from=0unlessdefined$from;4409$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);44104411print"<table class=\"tags\">\n";4412my$alternate=1;4413for(my$i=$from;$i<=$to;$i++) {4414my$entry=$taglist->[$i];4415my%tag=%$entry;4416my$comment=$tag{'subject'};4417my$comment_short;4418if(defined$comment) {4419$comment_short= chop_str($comment,30,5);4420}4421if($alternate) {4422print"<tr class=\"dark\">\n";4423}else{4424print"<tr class=\"light\">\n";4425}4426$alternate^=1;4427if(defined$tag{'age'}) {4428print"<td><i>$tag{'age'}</i></td>\n";4429}else{4430print"<td></td>\n";4431}4432print"<td>".4433$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4434-class=>"list name"}, esc_html($tag{'name'})) .4435"</td>\n".4436"<td>";4437if(defined$comment) {4438print format_subject_html($comment,$comment_short,4439 href(action=>"tag", hash=>$tag{'id'}));4440}4441print"</td>\n".4442"<td class=\"selflink\">";4443if($tag{'type'}eq"tag") {4444print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4445}else{4446print" ";4447}4448print"</td>\n".4449"<td class=\"link\">"." | ".4450$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4451if($tag{'reftype'}eq"commit") {4452print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4453" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4454}elsif($tag{'reftype'}eq"blob") {4455print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4456}4457print"</td>\n".4458"</tr>";4459}4460if(defined$extra) {4461print"<tr>\n".4462"<td colspan=\"5\">$extra</td>\n".4463"</tr>\n";4464}4465print"</table>\n";4466}44674468sub git_heads_body {4469# uses global variable $project4470my($headlist,$head,$from,$to,$extra) =@_;4471$from=0unlessdefined$from;4472$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);44734474print"<table class=\"heads\">\n";4475my$alternate=1;4476for(my$i=$from;$i<=$to;$i++) {4477my$entry=$headlist->[$i];4478my%ref=%$entry;4479my$curr=$ref{'id'}eq$head;4480if($alternate) {4481print"<tr class=\"dark\">\n";4482}else{4483print"<tr class=\"light\">\n";4484}4485$alternate^=1;4486print"<td><i>$ref{'age'}</i></td>\n".4487($curr?"<td class=\"current_head\">":"<td>") .4488$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4489-class=>"list name"},esc_html($ref{'name'})) .4490"</td>\n".4491"<td class=\"link\">".4492$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4493$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4494$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4495"</td>\n".4496"</tr>";4497}4498if(defined$extra) {4499print"<tr>\n".4500"<td colspan=\"3\">$extra</td>\n".4501"</tr>\n";4502}4503print"</table>\n";4504}45054506sub git_search_grep_body {4507my($commitlist,$from,$to,$extra) =@_;4508$from=0unlessdefined$from;4509$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);45104511print"<table class=\"commit_search\">\n";4512my$alternate=1;4513for(my$i=$from;$i<=$to;$i++) {4514my%co= %{$commitlist->[$i]};4515if(!%co) {4516next;4517}4518my$commit=$co{'id'};4519if($alternate) {4520print"<tr class=\"dark\">\n";4521}else{4522print"<tr class=\"light\">\n";4523}4524$alternate^=1;4525print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4526 format_author_html('td', \%co,15,5) .4527"<td>".4528$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4529-class=>"list subject"},4530 chop_and_escape_str($co{'title'},50) ."<br/>");4531my$comment=$co{'comment'};4532foreachmy$line(@$comment) {4533if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4534my($lead,$match,$trail) = ($1,$2,$3);4535$match= chop_str($match,70,5,'center');4536my$contextlen=int((80-length($match))/2);4537$contextlen=30if($contextlen>30);4538$lead= chop_str($lead,$contextlen,10,'left');4539$trail= chop_str($trail,$contextlen,10,'right');45404541$lead= esc_html($lead);4542$match= esc_html($match);4543$trail= esc_html($trail);45444545print"$lead<span class=\"match\">$match</span>$trail<br />";4546}4547}4548print"</td>\n".4549"<td class=\"link\">".4550$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4551" | ".4552$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4553" | ".4554$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4555print"</td>\n".4556"</tr>\n";4557}4558if(defined$extra) {4559print"<tr>\n".4560"<td colspan=\"3\">$extra</td>\n".4561"</tr>\n";4562}4563print"</table>\n";4564}45654566## ======================================================================4567## ======================================================================4568## actions45694570sub git_project_list {4571my$order=$input_params{'order'};4572if(defined$order&&$order!~m/none|project|descr|owner|age/) {4573 die_error(400,"Unknown order parameter");4574}45754576my@list= git_get_projects_list();4577if(!@list) {4578 die_error(404,"No projects found");4579}45804581 git_header_html();4582if(-f $home_text) {4583print"<div class=\"index_include\">\n";4584 insert_file($home_text);4585print"</div>\n";4586}4587print$cgi->startform(-method=>"get") .4588"<p class=\"projsearch\">Search:\n".4589$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4590"</p>".4591$cgi->end_form() ."\n";4592 git_project_list_body(\@list,$order);4593 git_footer_html();4594}45954596sub git_forks {4597my$order=$input_params{'order'};4598if(defined$order&&$order!~m/none|project|descr|owner|age/) {4599 die_error(400,"Unknown order parameter");4600}46014602my@list= git_get_projects_list($project);4603if(!@list) {4604 die_error(404,"No forks found");4605}46064607 git_header_html();4608 git_print_page_nav('','');4609 git_print_header_div('summary',"$projectforks");4610 git_project_list_body(\@list,$order);4611 git_footer_html();4612}46134614sub git_project_index {4615my@projects= git_get_projects_list($project);46164617print$cgi->header(4618-type =>'text/plain',4619-charset =>'utf-8',4620-content_disposition =>'inline; filename="index.aux"');46214622foreachmy$pr(@projects) {4623if(!exists$pr->{'owner'}) {4624$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4625}46264627my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4628# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4629$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4630$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4631$path=~s/ /\+/g;4632$owner=~s/ /\+/g;46334634print"$path$owner\n";4635}4636}46374638sub git_summary {4639my$descr= git_get_project_description($project) ||"none";4640my%co= parse_commit("HEAD");4641my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4642my$head=$co{'id'};46434644my$owner= git_get_project_owner($project);46454646my$refs= git_get_references();4647# These get_*_list functions return one more to allow us to see if4648# there are more ...4649my@taglist= git_get_tags_list(16);4650my@headlist= git_get_heads_list(16);4651my@forklist;4652my$check_forks= gitweb_check_feature('forks');46534654if($check_forks) {4655@forklist= git_get_projects_list($project);4656}46574658 git_header_html();4659 git_print_page_nav('summary','',$head);46604661print"<div class=\"title\"> </div>\n";4662print"<table class=\"projects_list\">\n".4663"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4664"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4665if(defined$cd{'rfc2822'}) {4666print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4667}46684669# use per project git URL list in $projectroot/$project/cloneurl4670# or make project git URL from git base URL and project name4671my$url_tag="URL";4672my@url_list= git_get_project_url_list($project);4673@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4674foreachmy$git_url(@url_list) {4675next unless$git_url;4676print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4677$url_tag="";4678}46794680# Tag cloud4681my$show_ctags= gitweb_check_feature('ctags');4682if($show_ctags) {4683my$ctags= git_get_project_ctags($project);4684my$cloud= git_populate_project_tagcloud($ctags);4685print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4686print"</td>\n<td>"unless%$ctags;4687print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4688print"</td>\n<td>"if%$ctags;4689print git_show_project_tagcloud($cloud,48);4690print"</td></tr>";4691}46924693print"</table>\n";46944695# If XSS prevention is on, we don't include README.html.4696# TODO: Allow a readme in some safe format.4697if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4698print"<div class=\"title\">readme</div>\n".4699"<div class=\"readme\">\n";4700 insert_file("$projectroot/$project/README.html");4701print"\n</div>\n";# class="readme"4702}47034704# we need to request one more than 16 (0..15) to check if4705# those 16 are all4706my@commitlist=$head? parse_commits($head,17) : ();4707if(@commitlist) {4708 git_print_header_div('shortlog');4709 git_shortlog_body(\@commitlist,0,15,$refs,4710$#commitlist<=15?undef:4711$cgi->a({-href => href(action=>"shortlog")},"..."));4712}47134714if(@taglist) {4715 git_print_header_div('tags');4716 git_tags_body(\@taglist,0,15,4717$#taglist<=15?undef:4718$cgi->a({-href => href(action=>"tags")},"..."));4719}47204721if(@headlist) {4722 git_print_header_div('heads');4723 git_heads_body(\@headlist,$head,0,15,4724$#headlist<=15?undef:4725$cgi->a({-href => href(action=>"heads")},"..."));4726}47274728if(@forklist) {4729 git_print_header_div('forks');4730 git_project_list_body(\@forklist,'age',0,15,4731$#forklist<=15?undef:4732$cgi->a({-href => href(action=>"forks")},"..."),4733'no_header');4734}47354736 git_footer_html();4737}47384739sub git_tag {4740my$head= git_get_head_hash($project);4741 git_header_html();4742 git_print_page_nav('','',$head,undef,$head);4743my%tag= parse_tag($hash);47444745if(!%tag) {4746 die_error(404,"Unknown tag object");4747}47484749 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4750print"<div class=\"title_text\">\n".4751"<table class=\"object_header\">\n".4752"<tr>\n".4753"<td>object</td>\n".4754"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4755$tag{'object'}) ."</td>\n".4756"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4757$tag{'type'}) ."</td>\n".4758"</tr>\n";4759if(defined($tag{'author'})) {4760 git_print_authorship_rows(\%tag,'author');4761}4762print"</table>\n\n".4763"</div>\n";4764print"<div class=\"page_body\">";4765my$comment=$tag{'comment'};4766foreachmy$line(@$comment) {4767chomp$line;4768print esc_html($line, -nbsp=>1) ."<br/>\n";4769}4770print"</div>\n";4771 git_footer_html();4772}47734774sub git_blame {4775# permissions4776 gitweb_check_feature('blame')4777or die_error(403,"Blame view not allowed");47784779# error checking4780 die_error(400,"No file name given")unless$file_name;4781$hash_base||= git_get_head_hash($project);4782 die_error(404,"Couldn't find base commit")unless$hash_base;4783my%co= parse_commit($hash_base)4784or die_error(404,"Commit not found");4785my$ftype="blob";4786if(!defined$hash) {4787$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4788or die_error(404,"Error looking up file");4789}else{4790$ftype= git_get_type($hash);4791if($ftype!~"blob") {4792 die_error(400,"Object is not a blob");4793}4794}47954796# run git-blame --porcelain4797open my$fd,"-|", git_cmd(),"blame",'-p',4798$hash_base,'--',$file_name4799or die_error(500,"Open git-blame failed");48004801# page header4802 git_header_html();4803my$formats_nav=4804$cgi->a({-href => href(action=>"blob", -replay=>1)},4805"blob") .4806" | ".4807$cgi->a({-href => href(action=>"history", -replay=>1)},4808"history") .4809" | ".4810$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4811"HEAD");4812 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4813 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4814 git_print_page_path($file_name,$ftype,$hash_base);48154816# page body4817my@rev_color=qw(light dark);4818my$num_colors=scalar(@rev_color);4819my$current_color=0;4820my%metainfo= ();48214822print<<HTML;4823<div class="page_body">4824<table class="blame">4825<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4826HTML4827 LINE:4828while(my$line= <$fd>) {4829chomp$line;4830# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4831# no <lines in group> for subsequent lines in group of lines4832my($full_rev,$orig_lineno,$lineno,$group_size) =4833($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4834if(!exists$metainfo{$full_rev}) {4835$metainfo{$full_rev} = {'nprevious'=>0};4836}4837my$meta=$metainfo{$full_rev};4838my$data;4839while($data= <$fd>) {4840chomp$data;4841last if($data=~s/^\t//);# contents of line4842if($data=~/^(\S+)(?: (.*))?$/) {4843$meta->{$1} =$2unlessexists$meta->{$1};4844}4845if($data=~/^previous /) {4846$meta->{'nprevious'}++;4847}4848}4849my$short_rev=substr($full_rev,0,8);4850my$author=$meta->{'author'};4851my%date=4852 parse_date($meta->{'author-time'},$meta->{'author-tz'});4853my$date=$date{'iso-tz'};4854if($group_size) {4855$current_color= ($current_color+1) %$num_colors;4856}4857my$tr_class=$rev_color[$current_color];4858$tr_class.=' boundary'if(exists$meta->{'boundary'});4859$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);4860$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);4861print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";4862if($group_size) {4863print"<td class=\"sha1\"";4864print" title=\"". esc_html($author) .",$date\"";4865print" rowspan=\"$group_size\""if($group_size>1);4866print">";4867print$cgi->a({-href => href(action=>"commit",4868 hash=>$full_rev,4869 file_name=>$file_name)},4870 esc_html($short_rev));4871if($group_size>=2) {4872my@author_initials= ($author=~/\b([[:upper:]])\B/g);4873if(@author_initials) {4874print"<br />".4875 esc_html(join('',@author_initials));4876# or join('.', ...)4877}4878}4879print"</td>\n";4880}4881# 'previous' <sha1 of parent commit> <filename at commit>4882if(exists$meta->{'previous'} &&4883$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {4884$meta->{'parent'} =$1;4885$meta->{'file_parent'} = unquote($2);4886}4887my$linenr_commit=4888exists($meta->{'parent'}) ?4889$meta->{'parent'} :$full_rev;4890my$linenr_filename=4891exists($meta->{'file_parent'}) ?4892$meta->{'file_parent'} : unquote($meta->{'filename'});4893my$blamed= href(action =>'blame',4894 file_name =>$linenr_filename,4895 hash_base =>$linenr_commit);4896print"<td class=\"linenr\">";4897print$cgi->a({ -href =>"$blamed#l$orig_lineno",4898-class=>"linenr"},4899 esc_html($lineno));4900print"</td>";4901print"<td class=\"pre\">". esc_html($data) ."</td>\n";4902print"</tr>\n";4903}4904print"</table>\n";4905print"</div>";4906close$fd4907or print"Reading blob failed\n";49084909# page footer4910 git_footer_html();4911}49124913sub git_tags {4914my$head= git_get_head_hash($project);4915 git_header_html();4916 git_print_page_nav('','',$head,undef,$head);4917 git_print_header_div('summary',$project);49184919my@tagslist= git_get_tags_list();4920if(@tagslist) {4921 git_tags_body(\@tagslist);4922}4923 git_footer_html();4924}49254926sub git_heads {4927my$head= git_get_head_hash($project);4928 git_header_html();4929 git_print_page_nav('','',$head,undef,$head);4930 git_print_header_div('summary',$project);49314932my@headslist= git_get_heads_list();4933if(@headslist) {4934 git_heads_body(\@headslist,$head);4935}4936 git_footer_html();4937}49384939sub git_blob_plain {4940my$type=shift;4941my$expires;49424943if(!defined$hash) {4944if(defined$file_name) {4945my$base=$hash_base|| git_get_head_hash($project);4946$hash= git_get_hash_by_path($base,$file_name,"blob")4947or die_error(404,"Cannot find file");4948}else{4949 die_error(400,"No file name defined");4950}4951}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4952# blobs defined by non-textual hash id's can be cached4953$expires="+1d";4954}49554956open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4957or die_error(500,"Open git-cat-file blob '$hash' failed");49584959# content-type (can include charset)4960$type= blob_contenttype($fd,$file_name,$type);49614962# "save as" filename, even when no $file_name is given4963my$save_as="$hash";4964if(defined$file_name) {4965$save_as=$file_name;4966}elsif($type=~m/^text\//) {4967$save_as.='.txt';4968}49694970# With XSS prevention on, blobs of all types except a few known safe4971# ones are served with "Content-Disposition: attachment" to make sure4972# they don't run in our security domain. For certain image types,4973# blob view writes an <img> tag referring to blob_plain view, and we4974# want to be sure not to break that by serving the image as an4975# attachment (though Firefox 3 doesn't seem to care).4976my$sandbox=$prevent_xss&&4977$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;49784979print$cgi->header(4980-type =>$type,4981-expires =>$expires,4982-content_disposition =>4983($sandbox?'attachment':'inline')4984.'; filename="'.$save_as.'"');4985local$/=undef;4986binmode STDOUT,':raw';4987print<$fd>;4988binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4989close$fd;4990}49914992sub git_blob {4993my$expires;49944995if(!defined$hash) {4996if(defined$file_name) {4997my$base=$hash_base|| git_get_head_hash($project);4998$hash= git_get_hash_by_path($base,$file_name,"blob")4999or die_error(404,"Cannot find file");5000}else{5001 die_error(400,"No file name defined");5002}5003}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5004# blobs defined by non-textual hash id's can be cached5005$expires="+1d";5006}50075008my$have_blame= gitweb_check_feature('blame');5009open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5010or die_error(500,"Couldn't cat$file_name,$hash");5011my$mimetype= blob_mimetype($fd,$file_name);5012if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5013close$fd;5014return git_blob_plain($mimetype);5015}5016# we can have blame only for text/* mimetype5017$have_blame&&= ($mimetype=~m!^text/!);50185019 git_header_html(undef,$expires);5020my$formats_nav='';5021if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5022if(defined$file_name) {5023if($have_blame) {5024$formats_nav.=5025$cgi->a({-href => href(action=>"blame", -replay=>1)},5026"blame") .5027" | ";5028}5029$formats_nav.=5030$cgi->a({-href => href(action=>"history", -replay=>1)},5031"history") .5032" | ".5033$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5034"raw") .5035" | ".5036$cgi->a({-href => href(action=>"blob",5037 hash_base=>"HEAD", file_name=>$file_name)},5038"HEAD");5039}else{5040$formats_nav.=5041$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5042"raw");5043}5044 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5045 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5046}else{5047print"<div class=\"page_nav\">\n".5048"<br/><br/></div>\n".5049"<div class=\"title\">$hash</div>\n";5050}5051 git_print_page_path($file_name,"blob",$hash_base);5052print"<div class=\"page_body\">\n";5053if($mimetype=~m!^image/!) {5054print qq!<img type="$mimetype"!;5055if($file_name) {5056print qq! alt="$file_name" title="$file_name"!;5057}5058print qq! src="! .5059 href(action=>"blob_plain", hash=>$hash,5060 hash_base=>$hash_base, file_name=>$file_name) .5061 qq!"/>\n!;5062}else{5063my$nr;5064while(my$line= <$fd>) {5065chomp$line;5066$nr++;5067$line= untabify($line);5068printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",5069$nr,$nr,$nr, esc_html($line, -nbsp=>1);5070}5071}5072close$fd5073or print"Reading blob failed.\n";5074print"</div>";5075 git_footer_html();5076}50775078sub git_tree {5079if(!defined$hash_base) {5080$hash_base="HEAD";5081}5082if(!defined$hash) {5083if(defined$file_name) {5084$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5085}else{5086$hash=$hash_base;5087}5088}5089 die_error(404,"No such tree")unlessdefined($hash);50905091my@entries= ();5092{5093local$/="\0";5094open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash5095or die_error(500,"Open git-ls-tree failed");5096@entries=map{chomp;$_} <$fd>;5097close$fd5098or die_error(404,"Reading tree failed");5099}51005101my$refs= git_get_references();5102my$ref= format_ref_marker($refs,$hash_base);5103 git_header_html();5104my$basedir='';5105my$have_blame= gitweb_check_feature('blame');5106if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5107my@views_nav= ();5108if(defined$file_name) {5109push@views_nav,5110$cgi->a({-href => href(action=>"history", -replay=>1)},5111"history"),5112$cgi->a({-href => href(action=>"tree",5113 hash_base=>"HEAD", file_name=>$file_name)},5114"HEAD"),5115}5116my$snapshot_links= format_snapshot_links($hash);5117if(defined$snapshot_links) {5118# FIXME: Should be available when we have no hash base as well.5119push@views_nav,$snapshot_links;5120}5121 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));5122 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);5123}else{5124undef$hash_base;5125print"<div class=\"page_nav\">\n";5126print"<br/><br/></div>\n";5127print"<div class=\"title\">$hash</div>\n";5128}5129if(defined$file_name) {5130$basedir=$file_name;5131if($basedirne''&&substr($basedir, -1)ne'/') {5132$basedir.='/';5133}5134 git_print_page_path($file_name,'tree',$hash_base);5135}5136print"<div class=\"page_body\">\n";5137print"<table class=\"tree\">\n";5138my$alternate=1;5139# '..' (top directory) link if possible5140if(defined$hash_base&&5141defined$file_name&&$file_name=~m![^/]+$!) {5142if($alternate) {5143print"<tr class=\"dark\">\n";5144}else{5145print"<tr class=\"light\">\n";5146}5147$alternate^=1;51485149my$up=$file_name;5150$up=~s!/?[^/]+$!!;5151undef$upunless$up;5152# based on git_print_tree_entry5153print'<td class="mode">'. mode_str('040000') ."</td>\n";5154print'<td class="list">';5155print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,5156 file_name=>$up)},5157"..");5158print"</td>\n";5159print"<td class=\"link\"></td>\n";51605161print"</tr>\n";5162}5163foreachmy$line(@entries) {5164my%t= parse_ls_tree_line($line, -z =>1);51655166if($alternate) {5167print"<tr class=\"dark\">\n";5168}else{5169print"<tr class=\"light\">\n";5170}5171$alternate^=1;51725173 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);51745175print"</tr>\n";5176}5177print"</table>\n".5178"</div>";5179 git_footer_html();5180}51815182sub git_snapshot {5183my$format=$input_params{'snapshot_format'};5184if(!@snapshot_fmts) {5185 die_error(403,"Snapshots not allowed");5186}5187# default to first supported snapshot format5188$format||=$snapshot_fmts[0];5189if($format!~m/^[a-z0-9]+$/) {5190 die_error(400,"Invalid snapshot format parameter");5191}elsif(!exists($known_snapshot_formats{$format})) {5192 die_error(400,"Unknown snapshot format");5193}elsif($known_snapshot_formats{$format}{'disabled'}) {5194 die_error(403,"Snapshot format not allowed");5195}elsif(!grep($_eq$format,@snapshot_fmts)) {5196 die_error(403,"Unsupported snapshot format");5197}51985199my$type= git_get_type("$hash^{}");5200if(!$type) {5201 die_error(404,'Object does not exist');5202}elsif($typeeq'blob') {5203 die_error(400,'Object is not a tree-ish');5204}52055206my$name=$project;5207$name=~ s,([^/])/*\.git$,$1,;5208$name= basename($name);5209my$filename= to_utf8($name);5210$name=~s/\047/\047\\\047\047/g;5211my$cmd;5212$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5213$cmd= quote_command(5214 git_cmd(),'archive',5215"--format=$known_snapshot_formats{$format}{'format'}",5216"--prefix=$name/",$hash);5217if(exists$known_snapshot_formats{$format}{'compressor'}) {5218$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5219}52205221print$cgi->header(5222-type =>$known_snapshot_formats{$format}{'type'},5223-content_disposition =>'inline; filename="'."$filename".'"',5224-status =>'200 OK');52255226open my$fd,"-|",$cmd5227or die_error(500,"Execute git-archive failed");5228binmode STDOUT,':raw';5229print<$fd>;5230binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5231close$fd;5232}52335234sub git_log {5235my$head= git_get_head_hash($project);5236if(!defined$hash) {5237$hash=$head;5238}5239if(!defined$page) {5240$page=0;5241}5242my$refs= git_get_references();52435244my@commitlist= parse_commits($hash,101, (100*$page));52455246my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);52475248my($patch_max) = gitweb_get_feature('patches');5249if($patch_max) {5250if($patch_max<0||@commitlist<=$patch_max) {5251$paging_nav.=" ⋅ ".5252$cgi->a({-href => href(action=>"patches", -replay=>1)},5253"patches");5254}5255}52565257 git_header_html();5258 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);52595260if(!@commitlist) {5261my%co= parse_commit($hash);52625263 git_print_header_div('summary',$project);5264print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5265}5266my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5267for(my$i=0;$i<=$to;$i++) {5268my%co= %{$commitlist[$i]};5269next if!%co;5270my$commit=$co{'id'};5271my$ref= format_ref_marker($refs,$commit);5272my%ad= parse_date($co{'author_epoch'});5273 git_print_header_div('commit',5274"<span class=\"age\">$co{'age_string'}</span>".5275 esc_html($co{'title'}) .$ref,5276$commit);5277print"<div class=\"title_text\">\n".5278"<div class=\"log_link\">\n".5279$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5280" | ".5281$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5282" | ".5283$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5284"<br/>\n".5285"</div>\n";5286 git_print_authorship(\%co, -tag =>'span');5287print"<br/>\n</div>\n";52885289print"<div class=\"log_body\">\n";5290 git_print_log($co{'comment'}, -final_empty_line=>1);5291print"</div>\n";5292}5293if($#commitlist>=100) {5294print"<div class=\"page_nav\">\n";5295print$cgi->a({-href => href(-replay=>1, page=>$page+1),5296-accesskey =>"n", -title =>"Alt-n"},"next");5297print"</div>\n";5298}5299 git_footer_html();5300}53015302sub git_commit {5303$hash||=$hash_base||"HEAD";5304my%co= parse_commit($hash)5305or die_error(404,"Unknown commit object");53065307my$parent=$co{'parent'};5308my$parents=$co{'parents'};# listref53095310# we need to prepare $formats_nav before any parameter munging5311my$formats_nav;5312if(!defined$parent) {5313# --root commitdiff5314$formats_nav.='(initial)';5315}elsif(@$parents==1) {5316# single parent commit5317$formats_nav.=5318'(parent: '.5319$cgi->a({-href => href(action=>"commit",5320 hash=>$parent)},5321 esc_html(substr($parent,0,7))) .5322')';5323}else{5324# merge commit5325$formats_nav.=5326'(merge: '.5327join(' ',map{5328$cgi->a({-href => href(action=>"commit",5329 hash=>$_)},5330 esc_html(substr($_,0,7)));5331}@$parents) .5332')';5333}5334if(gitweb_check_feature('patches')) {5335$formats_nav.=" | ".5336$cgi->a({-href => href(action=>"patch", -replay=>1)},5337"patch");5338}53395340if(!defined$parent) {5341$parent="--root";5342}5343my@difftree;5344open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5345@diff_opts,5346(@$parents<=1?$parent:'-c'),5347$hash,"--"5348or die_error(500,"Open git-diff-tree failed");5349@difftree=map{chomp;$_} <$fd>;5350close$fdor die_error(404,"Reading git-diff-tree failed");53515352# non-textual hash id's can be cached5353my$expires;5354if($hash=~m/^[0-9a-fA-F]{40}$/) {5355$expires="+1d";5356}5357my$refs= git_get_references();5358my$ref= format_ref_marker($refs,$co{'id'});53595360 git_header_html(undef,$expires);5361 git_print_page_nav('commit','',5362$hash,$co{'tree'},$hash,5363$formats_nav);53645365if(defined$co{'parent'}) {5366 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5367}else{5368 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5369}5370print"<div class=\"title_text\">\n".5371"<table class=\"object_header\">\n";5372 git_print_authorship_rows(\%co);5373print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5374print"<tr>".5375"<td>tree</td>".5376"<td class=\"sha1\">".5377$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5378class=>"list"},$co{'tree'}) .5379"</td>".5380"<td class=\"link\">".5381$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5382"tree");5383my$snapshot_links= format_snapshot_links($hash);5384if(defined$snapshot_links) {5385print" | ".$snapshot_links;5386}5387print"</td>".5388"</tr>\n";53895390foreachmy$par(@$parents) {5391print"<tr>".5392"<td>parent</td>".5393"<td class=\"sha1\">".5394$cgi->a({-href => href(action=>"commit", hash=>$par),5395class=>"list"},$par) .5396"</td>".5397"<td class=\"link\">".5398$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5399" | ".5400$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5401"</td>".5402"</tr>\n";5403}5404print"</table>".5405"</div>\n";54065407print"<div class=\"page_body\">\n";5408 git_print_log($co{'comment'});5409print"</div>\n";54105411 git_difftree_body(\@difftree,$hash,@$parents);54125413 git_footer_html();5414}54155416sub git_object {5417# object is defined by:5418# - hash or hash_base alone5419# - hash_base and file_name5420my$type;54215422# - hash or hash_base alone5423if($hash|| ($hash_base&& !defined$file_name)) {5424my$object_id=$hash||$hash_base;54255426open my$fd,"-|", quote_command(5427 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5428or die_error(404,"Object does not exist");5429$type= <$fd>;5430chomp$type;5431close$fd5432or die_error(404,"Object does not exist");54335434# - hash_base and file_name5435}elsif($hash_base&&defined$file_name) {5436$file_name=~ s,/+$,,;54375438system(git_cmd(),"cat-file",'-e',$hash_base) ==05439or die_error(404,"Base object does not exist");54405441# here errors should not hapen5442open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5443or die_error(500,"Open git-ls-tree failed");5444my$line= <$fd>;5445close$fd;54465447#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5448unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5449 die_error(404,"File or directory for given base does not exist");5450}5451$type=$2;5452$hash=$3;5453}else{5454 die_error(400,"Not enough information to find object");5455}54565457print$cgi->redirect(-uri => href(action=>$type, -full=>1,5458 hash=>$hash, hash_base=>$hash_base,5459 file_name=>$file_name),5460-status =>'302 Found');5461}54625463sub git_blobdiff {5464my$format=shift||'html';54655466my$fd;5467my@difftree;5468my%diffinfo;5469my$expires;54705471# preparing $fd and %diffinfo for git_patchset_body5472# new style URI5473if(defined$hash_base&&defined$hash_parent_base) {5474if(defined$file_name) {5475# read raw output5476open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5477$hash_parent_base,$hash_base,5478"--", (defined$file_parent?$file_parent: ()),$file_name5479or die_error(500,"Open git-diff-tree failed");5480@difftree=map{chomp;$_} <$fd>;5481close$fd5482or die_error(404,"Reading git-diff-tree failed");5483@difftree5484or die_error(404,"Blob diff not found");54855486}elsif(defined$hash&&5487$hash=~/[0-9a-fA-F]{40}/) {5488# try to find filename from $hash54895490# read filtered raw output5491open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5492$hash_parent_base,$hash_base,"--"5493or die_error(500,"Open git-diff-tree failed");5494@difftree=5495# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5496# $hash == to_id5497grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5498map{chomp;$_} <$fd>;5499close$fd5500or die_error(404,"Reading git-diff-tree failed");5501@difftree5502or die_error(404,"Blob diff not found");55035504}else{5505 die_error(400,"Missing one of the blob diff parameters");5506}55075508if(@difftree>1) {5509 die_error(400,"Ambiguous blob diff specification");5510}55115512%diffinfo= parse_difftree_raw_line($difftree[0]);5513$file_parent||=$diffinfo{'from_file'} ||$file_name;5514$file_name||=$diffinfo{'to_file'};55155516$hash_parent||=$diffinfo{'from_id'};5517$hash||=$diffinfo{'to_id'};55185519# non-textual hash id's can be cached5520if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5521$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5522$expires='+1d';5523}55245525# open patch output5526open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5527'-p', ($formateq'html'?"--full-index": ()),5528$hash_parent_base,$hash_base,5529"--", (defined$file_parent?$file_parent: ()),$file_name5530or die_error(500,"Open git-diff-tree failed");5531}55325533# old/legacy style URI -- not generated anymore since 1.4.3.5534if(!%diffinfo) {5535 die_error('404 Not Found',"Missing one of the blob diff parameters")5536}55375538# header5539if($formateq'html') {5540my$formats_nav=5541$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5542"raw");5543 git_header_html(undef,$expires);5544if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5545 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5546 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5547}else{5548print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5549print"<div class=\"title\">$hashvs$hash_parent</div>\n";5550}5551if(defined$file_name) {5552 git_print_page_path($file_name,"blob",$hash_base);5553}else{5554print"<div class=\"page_path\"></div>\n";5555}55565557}elsif($formateq'plain') {5558print$cgi->header(5559-type =>'text/plain',5560-charset =>'utf-8',5561-expires =>$expires,5562-content_disposition =>'inline; filename="'."$file_name".'.patch"');55635564print"X-Git-Url: ".$cgi->self_url() ."\n\n";55655566}else{5567 die_error(400,"Unknown blobdiff format");5568}55695570# patch5571if($formateq'html') {5572print"<div class=\"page_body\">\n";55735574 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5575close$fd;55765577print"</div>\n";# class="page_body"5578 git_footer_html();55795580}else{5581while(my$line= <$fd>) {5582$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5583$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;55845585print$line;55865587last if$line=~m!^\+\+\+!;5588}5589local$/=undef;5590print<$fd>;5591close$fd;5592}5593}55945595sub git_blobdiff_plain {5596 git_blobdiff('plain');5597}55985599sub git_commitdiff {5600my%params=@_;5601my$format=$params{-format} ||'html';56025603my($patch_max) = gitweb_get_feature('patches');5604if($formateq'patch') {5605 die_error(403,"Patch view not allowed")unless$patch_max;5606}56075608$hash||=$hash_base||"HEAD";5609my%co= parse_commit($hash)5610or die_error(404,"Unknown commit object");56115612# choose format for commitdiff for merge5613if(!defined$hash_parent&& @{$co{'parents'}} >1) {5614$hash_parent='--cc';5615}5616# we need to prepare $formats_nav before almost any parameter munging5617my$formats_nav;5618if($formateq'html') {5619$formats_nav=5620$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5621"raw");5622if($patch_max) {5623$formats_nav.=" | ".5624$cgi->a({-href => href(action=>"patch", -replay=>1)},5625"patch");5626}56275628if(defined$hash_parent&&5629$hash_parentne'-c'&&$hash_parentne'--cc') {5630# commitdiff with two commits given5631my$hash_parent_short=$hash_parent;5632if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5633$hash_parent_short=substr($hash_parent,0,7);5634}5635$formats_nav.=5636' (from';5637for(my$i=0;$i< @{$co{'parents'}};$i++) {5638if($co{'parents'}[$i]eq$hash_parent) {5639$formats_nav.=' parent '. ($i+1);5640last;5641}5642}5643$formats_nav.=': '.5644$cgi->a({-href => href(action=>"commitdiff",5645 hash=>$hash_parent)},5646 esc_html($hash_parent_short)) .5647')';5648}elsif(!$co{'parent'}) {5649# --root commitdiff5650$formats_nav.=' (initial)';5651}elsif(scalar@{$co{'parents'}} ==1) {5652# single parent commit5653$formats_nav.=5654' (parent: '.5655$cgi->a({-href => href(action=>"commitdiff",5656 hash=>$co{'parent'})},5657 esc_html(substr($co{'parent'},0,7))) .5658')';5659}else{5660# merge commit5661if($hash_parenteq'--cc') {5662$formats_nav.=' | '.5663$cgi->a({-href => href(action=>"commitdiff",5664 hash=>$hash, hash_parent=>'-c')},5665'combined');5666}else{# $hash_parent eq '-c'5667$formats_nav.=' | '.5668$cgi->a({-href => href(action=>"commitdiff",5669 hash=>$hash, hash_parent=>'--cc')},5670'compact');5671}5672$formats_nav.=5673' (merge: '.5674join(' ',map{5675$cgi->a({-href => href(action=>"commitdiff",5676 hash=>$_)},5677 esc_html(substr($_,0,7)));5678} @{$co{'parents'}} ) .5679')';5680}5681}56825683my$hash_parent_param=$hash_parent;5684if(!defined$hash_parent_param) {5685# --cc for multiple parents, --root for parentless5686$hash_parent_param=5687@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5688}56895690# read commitdiff5691my$fd;5692my@difftree;5693if($formateq'html') {5694open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5695"--no-commit-id","--patch-with-raw","--full-index",5696$hash_parent_param,$hash,"--"5697or die_error(500,"Open git-diff-tree failed");56985699while(my$line= <$fd>) {5700chomp$line;5701# empty line ends raw part of diff-tree output5702last unless$line;5703push@difftree,scalar parse_difftree_raw_line($line);5704}57055706}elsif($formateq'plain') {5707open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5708'-p',$hash_parent_param,$hash,"--"5709or die_error(500,"Open git-diff-tree failed");5710}elsif($formateq'patch') {5711# For commit ranges, we limit the output to the number of5712# patches specified in the 'patches' feature.5713# For single commits, we limit the output to a single patch,5714# diverging from the git-format-patch default.5715my@commit_spec= ();5716if($hash_parent) {5717if($patch_max>0) {5718push@commit_spec,"-$patch_max";5719}5720push@commit_spec,'-n',"$hash_parent..$hash";5721}else{5722if($params{-single}) {5723push@commit_spec,'-1';5724}else{5725if($patch_max>0) {5726push@commit_spec,"-$patch_max";5727}5728push@commit_spec,"-n";5729}5730push@commit_spec,'--root',$hash;5731}5732open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5733'--stdout',@commit_spec5734or die_error(500,"Open git-format-patch failed");5735}else{5736 die_error(400,"Unknown commitdiff format");5737}57385739# non-textual hash id's can be cached5740my$expires;5741if($hash=~m/^[0-9a-fA-F]{40}$/) {5742$expires="+1d";5743}57445745# write commit message5746if($formateq'html') {5747my$refs= git_get_references();5748my$ref= format_ref_marker($refs,$co{'id'});57495750 git_header_html(undef,$expires);5751 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5752 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5753print"<div class=\"title_text\">\n".5754"<table class=\"object_header\">\n";5755 git_print_authorship_rows(\%co);5756print"</table>".5757"</div>\n";5758print"<div class=\"page_body\">\n";5759if(@{$co{'comment'}} >1) {5760print"<div class=\"log\">\n";5761 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5762print"</div>\n";# class="log"5763}57645765}elsif($formateq'plain') {5766my$refs= git_get_references("tags");5767my$tagname= git_get_rev_name_tags($hash);5768my$filename= basename($project) ."-$hash.patch";57695770print$cgi->header(5771-type =>'text/plain',5772-charset =>'utf-8',5773-expires =>$expires,5774-content_disposition =>'inline; filename="'."$filename".'"');5775my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5776print"From: ". to_utf8($co{'author'}) ."\n";5777print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5778print"Subject: ". to_utf8($co{'title'}) ."\n";57795780print"X-Git-Tag:$tagname\n"if$tagname;5781print"X-Git-Url: ".$cgi->self_url() ."\n\n";57825783foreachmy$line(@{$co{'comment'}}) {5784print to_utf8($line) ."\n";5785}5786print"---\n\n";5787}elsif($formateq'patch') {5788my$filename= basename($project) ."-$hash.patch";57895790print$cgi->header(5791-type =>'text/plain',5792-charset =>'utf-8',5793-expires =>$expires,5794-content_disposition =>'inline; filename="'."$filename".'"');5795}57965797# write patch5798if($formateq'html') {5799my$use_parents= !defined$hash_parent||5800$hash_parenteq'-c'||$hash_parenteq'--cc';5801 git_difftree_body(\@difftree,$hash,5802$use_parents? @{$co{'parents'}} :$hash_parent);5803print"<br/>\n";58045805 git_patchset_body($fd, \@difftree,$hash,5806$use_parents? @{$co{'parents'}} :$hash_parent);5807close$fd;5808print"</div>\n";# class="page_body"5809 git_footer_html();58105811}elsif($formateq'plain') {5812local$/=undef;5813print<$fd>;5814close$fd5815or print"Reading git-diff-tree failed\n";5816}elsif($formateq'patch') {5817local$/=undef;5818print<$fd>;5819close$fd5820or print"Reading git-format-patch failed\n";5821}5822}58235824sub git_commitdiff_plain {5825 git_commitdiff(-format =>'plain');5826}58275828# format-patch-style patches5829sub git_patch {5830 git_commitdiff(-format =>'patch', -single=>1);5831}58325833sub git_patches {5834 git_commitdiff(-format =>'patch');5835}58365837sub git_history {5838if(!defined$hash_base) {5839$hash_base= git_get_head_hash($project);5840}5841if(!defined$page) {5842$page=0;5843}5844my$ftype;5845my%co= parse_commit($hash_base)5846or die_error(404,"Unknown commit object");58475848my$refs= git_get_references();5849my$limit=sprintf("--max-count=%i", (100* ($page+1)));58505851my@commitlist= parse_commits($hash_base,101, (100*$page),5852$file_name,"--full-history")5853or die_error(404,"No such file or directory on given branch");58545855if(!defined$hash&&defined$file_name) {5856# some commits could have deleted file in question,5857# and not have it in tree, but one of them has to have it5858for(my$i=0;$i<=@commitlist;$i++) {5859$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5860last ifdefined$hash;5861}5862}5863if(defined$hash) {5864$ftype= git_get_type($hash);5865}5866if(!defined$ftype) {5867 die_error(500,"Unknown type of object");5868}58695870my$paging_nav='';5871if($page>0) {5872$paging_nav.=5873$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5874 file_name=>$file_name)},5875"first");5876$paging_nav.=" ⋅ ".5877$cgi->a({-href => href(-replay=>1, page=>$page-1),5878-accesskey =>"p", -title =>"Alt-p"},"prev");5879}else{5880$paging_nav.="first";5881$paging_nav.=" ⋅ prev";5882}5883my$next_link='';5884if($#commitlist>=100) {5885$next_link=5886$cgi->a({-href => href(-replay=>1, page=>$page+1),5887-accesskey =>"n", -title =>"Alt-n"},"next");5888$paging_nav.=" ⋅$next_link";5889}else{5890$paging_nav.=" ⋅ next";5891}58925893 git_header_html();5894 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5895 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5896 git_print_page_path($file_name,$ftype,$hash_base);58975898 git_history_body(\@commitlist,0,99,5899$refs,$hash_base,$ftype,$next_link);59005901 git_footer_html();5902}59035904sub git_search {5905 gitweb_check_feature('search')or die_error(403,"Search is disabled");5906if(!defined$searchtext) {5907 die_error(400,"Text field is empty");5908}5909if(!defined$hash) {5910$hash= git_get_head_hash($project);5911}5912my%co= parse_commit($hash);5913if(!%co) {5914 die_error(404,"Unknown commit object");5915}5916if(!defined$page) {5917$page=0;5918}59195920$searchtype||='commit';5921if($searchtypeeq'pickaxe') {5922# pickaxe may take all resources of your box and run for several minutes5923# with every query - so decide by yourself how public you make this feature5924 gitweb_check_feature('pickaxe')5925or die_error(403,"Pickaxe is disabled");5926}5927if($searchtypeeq'grep') {5928 gitweb_check_feature('grep')5929or die_error(403,"Grep is disabled");5930}59315932 git_header_html();59335934if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5935my$greptype;5936if($searchtypeeq'commit') {5937$greptype="--grep=";5938}elsif($searchtypeeq'author') {5939$greptype="--author=";5940}elsif($searchtypeeq'committer') {5941$greptype="--committer=";5942}5943$greptype.=$searchtext;5944my@commitlist= parse_commits($hash,101, (100*$page),undef,5945$greptype,'--regexp-ignore-case',5946$search_use_regexp?'--extended-regexp':'--fixed-strings');59475948my$paging_nav='';5949if($page>0) {5950$paging_nav.=5951$cgi->a({-href => href(action=>"search", hash=>$hash,5952 searchtext=>$searchtext,5953 searchtype=>$searchtype)},5954"first");5955$paging_nav.=" ⋅ ".5956$cgi->a({-href => href(-replay=>1, page=>$page-1),5957-accesskey =>"p", -title =>"Alt-p"},"prev");5958}else{5959$paging_nav.="first";5960$paging_nav.=" ⋅ prev";5961}5962my$next_link='';5963if($#commitlist>=100) {5964$next_link=5965$cgi->a({-href => href(-replay=>1, page=>$page+1),5966-accesskey =>"n", -title =>"Alt-n"},"next");5967$paging_nav.=" ⋅$next_link";5968}else{5969$paging_nav.=" ⋅ next";5970}59715972if($#commitlist>=100) {5973}59745975 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5976 git_print_header_div('commit', esc_html($co{'title'}),$hash);5977 git_search_grep_body(\@commitlist,0,99,$next_link);5978}59795980if($searchtypeeq'pickaxe') {5981 git_print_page_nav('','',$hash,$co{'tree'},$hash);5982 git_print_header_div('commit', esc_html($co{'title'}),$hash);59835984print"<table class=\"pickaxe search\">\n";5985my$alternate=1;5986local$/="\n";5987open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5988'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5989($search_use_regexp?'--pickaxe-regex': ());5990undef%co;5991my@files;5992while(my$line= <$fd>) {5993chomp$line;5994next unless$line;59955996my%set= parse_difftree_raw_line($line);5997if(defined$set{'commit'}) {5998# finish previous commit5999if(%co) {6000print"</td>\n".6001"<td class=\"link\">".6002$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6003" | ".6004$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6005print"</td>\n".6006"</tr>\n";6007}60086009if($alternate) {6010print"<tr class=\"dark\">\n";6011}else{6012print"<tr class=\"light\">\n";6013}6014$alternate^=1;6015%co= parse_commit($set{'commit'});6016my$author= chop_and_escape_str($co{'author_name'},15,5);6017print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6018"<td><i>$author</i></td>\n".6019"<td>".6020$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6021-class=>"list subject"},6022 chop_and_escape_str($co{'title'},50) ."<br/>");6023}elsif(defined$set{'to_id'}) {6024next if($set{'to_id'} =~m/^0{40}$/);60256026print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6027 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6028-class=>"list"},6029"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6030"<br/>\n";6031}6032}6033close$fd;60346035# finish last commit (warning: repetition!)6036if(%co) {6037print"</td>\n".6038"<td class=\"link\">".6039$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6040" | ".6041$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6042print"</td>\n".6043"</tr>\n";6044}60456046print"</table>\n";6047}60486049if($searchtypeeq'grep') {6050 git_print_page_nav('','',$hash,$co{'tree'},$hash);6051 git_print_header_div('commit', esc_html($co{'title'}),$hash);60526053print"<table class=\"grep_search\">\n";6054my$alternate=1;6055my$matches=0;6056local$/="\n";6057open my$fd,"-|", git_cmd(),'grep','-n',6058$search_use_regexp? ('-E','-i') :'-F',6059$searchtext,$co{'tree'};6060my$lastfile='';6061while(my$line= <$fd>) {6062chomp$line;6063my($file,$lno,$ltext,$binary);6064last if($matches++>1000);6065if($line=~/^Binary file (.+) matches$/) {6066$file=$1;6067$binary=1;6068}else{6069(undef,$file,$lno,$ltext) =split(/:/,$line,4);6070}6071if($filene$lastfile) {6072$lastfileand print"</td></tr>\n";6073if($alternate++) {6074print"<tr class=\"dark\">\n";6075}else{6076print"<tr class=\"light\">\n";6077}6078print"<td class=\"list\">".6079$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6080 file_name=>"$file"),6081-class=>"list"}, esc_path($file));6082print"</td><td>\n";6083$lastfile=$file;6084}6085if($binary) {6086print"<div class=\"binary\">Binary file</div>\n";6087}else{6088$ltext= untabify($ltext);6089if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6090$ltext= esc_html($1, -nbsp=>1);6091$ltext.='<span class="match">';6092$ltext.= esc_html($2, -nbsp=>1);6093$ltext.='</span>';6094$ltext.= esc_html($3, -nbsp=>1);6095}else{6096$ltext= esc_html($ltext, -nbsp=>1);6097}6098print"<div class=\"pre\">".6099$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6100 file_name=>"$file").'#l'.$lno,6101-class=>"linenr"},sprintf('%4i',$lno))6102.' '.$ltext."</div>\n";6103}6104}6105if($lastfile) {6106print"</td></tr>\n";6107if($matches>1000) {6108print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";6109}6110}else{6111print"<div class=\"diff nodifferences\">No matches found</div>\n";6112}6113close$fd;61146115print"</table>\n";6116}6117 git_footer_html();6118}61196120sub git_search_help {6121 git_header_html();6122 git_print_page_nav('','',$hash,$hash,$hash);6123print<<EOT;6124<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without6125regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,6126the pattern entered is recognized as the POSIX extended6127<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case6128insensitive).</p>6129<dl>6130<dt><b>commit</b></dt>6131<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>6132EOT6133my$have_grep= gitweb_check_feature('grep');6134if($have_grep) {6135print<<EOT;6136<dt><b>grep</b></dt>6137<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing6138 a different one) are searched for the given pattern. On large trees, this search can take6139a while and put some strain on the server, so please use it with some consideration. Note that6140due to git-grep peculiarity, currently if regexp mode is turned off, the matches are6141case-sensitive.</dd>6142EOT6143}6144print<<EOT;6145<dt><b>author</b></dt>6146<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>6147<dt><b>committer</b></dt>6148<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>6149EOT6150my$have_pickaxe= gitweb_check_feature('pickaxe');6151if($have_pickaxe) {6152print<<EOT;6153<dt><b>pickaxe</b></dt>6154<dd>All commits that caused the string to appear or disappear from any file (changes that6155added, removed or "modified" the string) will be listed. This search can take a while and6156takes a lot of strain on the server, so please use it wisely. Note that since you may be6157interested even in changes just changing the case as well, this search is case sensitive.</dd>6158EOT6159}6160print"</dl>\n";6161 git_footer_html();6162}61636164sub git_shortlog {6165my$head= git_get_head_hash($project);6166if(!defined$hash) {6167$hash=$head;6168}6169if(!defined$page) {6170$page=0;6171}6172my$refs= git_get_references();61736174my$commit_hash=$hash;6175if(defined$hash_parent) {6176$commit_hash="$hash_parent..$hash";6177}6178my@commitlist= parse_commits($commit_hash,101, (100*$page));61796180my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);6181my$next_link='';6182if($#commitlist>=100) {6183$next_link=6184$cgi->a({-href => href(-replay=>1, page=>$page+1),6185-accesskey =>"n", -title =>"Alt-n"},"next");6186}6187my$patch_max= gitweb_check_feature('patches');6188if($patch_max) {6189if($patch_max<0||@commitlist<=$patch_max) {6190$paging_nav.=" ⋅ ".6191$cgi->a({-href => href(action=>"patches", -replay=>1)},6192"patches");6193}6194}61956196 git_header_html();6197 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6198 git_print_header_div('summary',$project);61996200 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);62016202 git_footer_html();6203}62046205## ......................................................................6206## feeds (RSS, Atom; OPML)62076208sub git_feed {6209my$format=shift||'atom';6210my$have_blame= gitweb_check_feature('blame');62116212# Atom: http://www.atomenabled.org/developers/syndication/6213# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6214if($formatne'rss'&&$formatne'atom') {6215 die_error(400,"Unknown web feed format");6216}62176218# log/feed of current (HEAD) branch, log of given branch, history of file/directory6219my$head=$hash||'HEAD';6220my@commitlist= parse_commits($head,150,0,$file_name);62216222my%latest_commit;6223my%latest_date;6224my$content_type="application/$format+xml";6225if(defined$cgi->http('HTTP_ACCEPT') &&6226$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6227# browser (feed reader) prefers text/xml6228$content_type='text/xml';6229}6230if(defined($commitlist[0])) {6231%latest_commit= %{$commitlist[0]};6232my$latest_epoch=$latest_commit{'committer_epoch'};6233%latest_date= parse_date($latest_epoch);6234my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6235if(defined$if_modified) {6236my$since;6237if(eval{require HTTP::Date;1; }) {6238$since= HTTP::Date::str2time($if_modified);6239}elsif(eval{require Time::ParseDate;1; }) {6240$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6241}6242if(defined$since&&$latest_epoch<=$since) {6243print$cgi->header(6244-type =>$content_type,6245-charset =>'utf-8',6246-last_modified =>$latest_date{'rfc2822'},6247-status =>'304 Not Modified');6248return;6249}6250}6251print$cgi->header(6252-type =>$content_type,6253-charset =>'utf-8',6254-last_modified =>$latest_date{'rfc2822'});6255}else{6256print$cgi->header(6257-type =>$content_type,6258-charset =>'utf-8');6259}62606261# Optimization: skip generating the body if client asks only6262# for Last-Modified date.6263return if($cgi->request_method()eq'HEAD');62646265# header variables6266my$title="$site_name-$project/$action";6267my$feed_type='log';6268if(defined$hash) {6269$title.=" - '$hash'";6270$feed_type='branch log';6271if(defined$file_name) {6272$title.=" ::$file_name";6273$feed_type='history';6274}6275}elsif(defined$file_name) {6276$title.=" -$file_name";6277$feed_type='history';6278}6279$title.="$feed_type";6280my$descr= git_get_project_description($project);6281if(defined$descr) {6282$descr= esc_html($descr);6283}else{6284$descr="$project".6285($formateq'rss'?'RSS':'Atom') .6286" feed";6287}6288my$owner= git_get_project_owner($project);6289$owner= esc_html($owner);62906291#header6292my$alt_url;6293if(defined$file_name) {6294$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6295}elsif(defined$hash) {6296$alt_url= href(-full=>1, action=>"log", hash=>$hash);6297}else{6298$alt_url= href(-full=>1, action=>"summary");6299}6300print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6301if($formateq'rss') {6302print<<XML;6303<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6304<channel>6305XML6306print"<title>$title</title>\n".6307"<link>$alt_url</link>\n".6308"<description>$descr</description>\n".6309"<language>en</language>\n".6310# project owner is responsible for 'editorial' content6311"<managingEditor>$owner</managingEditor>\n";6312if(defined$logo||defined$favicon) {6313# prefer the logo to the favicon, since RSS6314# doesn't allow both6315my$img= esc_url($logo||$favicon);6316print"<image>\n".6317"<url>$img</url>\n".6318"<title>$title</title>\n".6319"<link>$alt_url</link>\n".6320"</image>\n";6321}6322if(%latest_date) {6323print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6324print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6325}6326print"<generator>gitweb v.$version/$git_version</generator>\n";6327}elsif($formateq'atom') {6328print<<XML;6329<feed xmlns="http://www.w3.org/2005/Atom">6330XML6331print"<title>$title</title>\n".6332"<subtitle>$descr</subtitle>\n".6333'<link rel="alternate" type="text/html" href="'.6334$alt_url.'" />'."\n".6335'<link rel="self" type="'.$content_type.'" href="'.6336$cgi->self_url() .'" />'."\n".6337"<id>". href(-full=>1) ."</id>\n".6338# use project owner for feed author6339"<author><name>$owner</name></author>\n";6340if(defined$favicon) {6341print"<icon>". esc_url($favicon) ."</icon>\n";6342}6343if(defined$logo_url) {6344# not twice as wide as tall: 72 x 27 pixels6345print"<logo>". esc_url($logo) ."</logo>\n";6346}6347if(!%latest_date) {6348# dummy date to keep the feed valid until commits trickle in:6349print"<updated>1970-01-01T00:00:00Z</updated>\n";6350}else{6351print"<updated>$latest_date{'iso-8601'}</updated>\n";6352}6353print"<generator version='$version/$git_version'>gitweb</generator>\n";6354}63556356# contents6357for(my$i=0;$i<=$#commitlist;$i++) {6358my%co= %{$commitlist[$i]};6359my$commit=$co{'id'};6360# we read 150, we always show 30 and the ones more recent than 48 hours6361if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6362last;6363}6364my%cd= parse_date($co{'author_epoch'});63656366# get list of changed files6367open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6368$co{'parent'} ||"--root",6369$co{'id'},"--", (defined$file_name?$file_name: ())6370ornext;6371my@difftree=map{chomp;$_} <$fd>;6372close$fd6373ornext;63746375# print element (entry, item)6376my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6377if($formateq'rss') {6378print"<item>\n".6379"<title>". esc_html($co{'title'}) ."</title>\n".6380"<author>". esc_html($co{'author'}) ."</author>\n".6381"<pubDate>$cd{'rfc2822'}</pubDate>\n".6382"<guid isPermaLink=\"true\">$co_url</guid>\n".6383"<link>$co_url</link>\n".6384"<description>". esc_html($co{'title'}) ."</description>\n".6385"<content:encoded>".6386"<![CDATA[\n";6387}elsif($formateq'atom') {6388print"<entry>\n".6389"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6390"<updated>$cd{'iso-8601'}</updated>\n".6391"<author>\n".6392" <name>". esc_html($co{'author_name'}) ."</name>\n";6393if($co{'author_email'}) {6394print" <email>". esc_html($co{'author_email'}) ."</email>\n";6395}6396print"</author>\n".6397# use committer for contributor6398"<contributor>\n".6399" <name>". esc_html($co{'committer_name'}) ."</name>\n";6400if($co{'committer_email'}) {6401print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6402}6403print"</contributor>\n".6404"<published>$cd{'iso-8601'}</published>\n".6405"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6406"<id>$co_url</id>\n".6407"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6408"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6409}6410my$comment=$co{'comment'};6411print"<pre>\n";6412foreachmy$line(@$comment) {6413$line= esc_html($line);6414print"$line\n";6415}6416print"</pre><ul>\n";6417foreachmy$difftree_line(@difftree) {6418my%difftree= parse_difftree_raw_line($difftree_line);6419next if!$difftree{'from_id'};64206421my$file=$difftree{'file'} ||$difftree{'to_file'};64226423print"<li>".6424"[".6425$cgi->a({-href => href(-full=>1, action=>"blobdiff",6426 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6427 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6428 file_name=>$file, file_parent=>$difftree{'from_file'}),6429-title =>"diff"},'D');6430if($have_blame) {6431print$cgi->a({-href => href(-full=>1, action=>"blame",6432 file_name=>$file, hash_base=>$commit),6433-title =>"blame"},'B');6434}6435# if this is not a feed of a file history6436if(!defined$file_name||$file_namene$file) {6437print$cgi->a({-href => href(-full=>1, action=>"history",6438 file_name=>$file, hash=>$commit),6439-title =>"history"},'H');6440}6441$file= esc_path($file);6442print"] ".6443"$file</li>\n";6444}6445if($formateq'rss') {6446print"</ul>]]>\n".6447"</content:encoded>\n".6448"</item>\n";6449}elsif($formateq'atom') {6450print"</ul>\n</div>\n".6451"</content>\n".6452"</entry>\n";6453}6454}64556456# end of feed6457if($formateq'rss') {6458print"</channel>\n</rss>\n";6459}elsif($formateq'atom') {6460print"</feed>\n";6461}6462}64636464sub git_rss {6465 git_feed('rss');6466}64676468sub git_atom {6469 git_feed('atom');6470}64716472sub git_opml {6473my@list= git_get_projects_list();64746475print$cgi->header(6476-type =>'text/xml',6477-charset =>'utf-8',6478-content_disposition =>'inline; filename="opml.xml"');64796480print<<XML;6481<?xml version="1.0" encoding="utf-8"?>6482<opml version="1.0">6483<head>6484 <title>$site_nameOPML Export</title>6485</head>6486<body>6487<outline text="git RSS feeds">6488XML64896490foreachmy$pr(@list) {6491my%proj=%$pr;6492my$head= git_get_head_hash($proj{'path'});6493if(!defined$head) {6494next;6495}6496$git_dir="$projectroot/$proj{'path'}";6497my%co= parse_commit($head);6498if(!%co) {6499next;6500}65016502my$path= esc_html(chop_str($proj{'path'},25,5));6503my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6504my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6505print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6506}6507print<<XML;6508</outline>6509</body>6510</opml>6511XML6512}