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# if we're called with PATH_INFO, we have to strip that 31# from the URL to find our real URL 32# we make $path_info global because it's also used later on 33our$path_info=$ENV{"PATH_INFO"}; 34if($path_info) { 35$my_url=~ s,\Q$path_info\E$,,; 36$my_uri=~ s,\Q$path_info\E$,,; 37} 38 39# core git executable to use 40# this can just be "git" if your webserver has a sensible PATH 41our$GIT="++GIT_BINDIR++/git"; 42 43# absolute fs-path which will be prepended to the project path 44#our $projectroot = "/pub/scm"; 45our$projectroot="++GITWEB_PROJECTROOT++"; 46 47# fs traversing limit for getting project list 48# the number is relative to the projectroot 49our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 50 51# target of the home link on top of all pages 52our$home_link=$my_uri||"/"; 53 54# string of the home link on top of all pages 55our$home_link_str="++GITWEB_HOME_LINK_STR++"; 56 57# name of your site or organization to appear in page titles 58# replace this with something more descriptive for clearer bookmarks 59our$site_name="++GITWEB_SITENAME++" 60|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 61 62# filename of html text to include at top of each page 63our$site_header="++GITWEB_SITE_HEADER++"; 64# html text to include at home page 65our$home_text="++GITWEB_HOMETEXT++"; 66# filename of html text to include at bottom of each page 67our$site_footer="++GITWEB_SITE_FOOTER++"; 68 69# URI of stylesheets 70our@stylesheets= ("++GITWEB_CSS++"); 71# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 72our$stylesheet=undef; 73# URI of GIT logo (72x27 size) 74our$logo="++GITWEB_LOGO++"; 75# URI of GIT favicon, assumed to be image/png type 76our$favicon="++GITWEB_FAVICON++"; 77 78# URI and label (title) of GIT logo link 79#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 80#our $logo_label = "git documentation"; 81our$logo_url="http://git.or.cz/"; 82our$logo_label="git homepage"; 83 84# source of projects list 85our$projects_list="++GITWEB_LIST++"; 86 87# the width (in characters) of the projects list "Description" column 88our$projects_list_description_width=25; 89 90# default order of projects list 91# valid values are none, project, descr, owner, and age 92our$default_projects_order="project"; 93 94# show repository only if this file exists 95# (only effective if this variable evaluates to true) 96our$export_ok="++GITWEB_EXPORT_OK++"; 97 98# show repository only if this subroutine returns true 99# when given the path to the project, for example: 100# sub { return -e "$_[0]/git-daemon-export-ok"; } 101our$export_auth_hook=undef; 102 103# only allow viewing of repositories also shown on the overview page 104our$strict_export="++GITWEB_STRICT_EXPORT++"; 105 106# list of git base URLs used for URL to where fetch project from, 107# i.e. full URL is "$git_base_url/$project" 108our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 109 110# default blob_plain mimetype and default charset for text/plain blob 111our$default_blob_plain_mimetype='text/plain'; 112our$default_text_plain_charset=undef; 113 114# file to use for guessing MIME types before trying /etc/mime.types 115# (relative to the current git repository) 116our$mimetypes_file=undef; 117 118# assume this charset if line contains non-UTF-8 characters; 119# it should be valid encoding (see Encoding::Supported(3pm) for list), 120# for which encoding all byte sequences are valid, for example 121# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 122# could be even 'utf-8' for the old behavior) 123our$fallback_encoding='latin1'; 124 125# rename detection options for git-diff and git-diff-tree 126# - default is '-M', with the cost proportional to 127# (number of removed files) * (number of new files). 128# - more costly is '-C' (which implies '-M'), with the cost proportional to 129# (number of changed files + number of removed files) * (number of new files) 130# - even more costly is '-C', '--find-copies-harder' with cost 131# (number of files in the original tree) * (number of new files) 132# - one might want to include '-B' option, e.g. '-B', '-M' 133our@diff_opts= ('-M');# taken from git_commit 134 135# Disables features that would allow repository owners to inject script into 136# the gitweb domain. 137our$prevent_xss=0; 138 139# information about snapshot formats that gitweb is capable of serving 140our%known_snapshot_formats= ( 141# name => { 142# 'display' => display name, 143# 'type' => mime type, 144# 'suffix' => filename suffix, 145# 'format' => --format for git-archive, 146# 'compressor' => [compressor command and arguments] 147# (array reference, optional)} 148# 149'tgz'=> { 150'display'=>'tar.gz', 151'type'=>'application/x-gzip', 152'suffix'=>'.tar.gz', 153'format'=>'tar', 154'compressor'=> ['gzip']}, 155 156'tbz2'=> { 157'display'=>'tar.bz2', 158'type'=>'application/x-bzip2', 159'suffix'=>'.tar.bz2', 160'format'=>'tar', 161'compressor'=> ['bzip2']}, 162 163'zip'=> { 164'display'=>'zip', 165'type'=>'application/x-zip', 166'suffix'=>'.zip', 167'format'=>'zip'}, 168); 169 170# Aliases so we understand old gitweb.snapshot values in repository 171# configuration. 172our%known_snapshot_format_aliases= ( 173'gzip'=>'tgz', 174'bzip2'=>'tbz2', 175 176# backward compatibility: legacy gitweb config support 177'x-gzip'=>undef,'gz'=>undef, 178'x-bzip2'=>undef,'bz2'=>undef, 179'x-zip'=>undef,''=>undef, 180); 181 182# You define site-wide feature defaults here; override them with 183# $GITWEB_CONFIG as necessary. 184our%feature= ( 185# feature => { 186# 'sub' => feature-sub (subroutine), 187# 'override' => allow-override (boolean), 188# 'default' => [ default options...] (array reference)} 189# 190# if feature is overridable (it means that allow-override has true value), 191# then feature-sub will be called with default options as parameters; 192# return value of feature-sub indicates if to enable specified feature 193# 194# if there is no 'sub' key (no feature-sub), then feature cannot be 195# overriden 196# 197# use gitweb_get_feature(<feature>) to retrieve the <feature> value 198# (an array) or gitweb_check_feature(<feature>) to check if <feature> 199# is enabled 200 201# Enable the 'blame' blob view, showing the last commit that modified 202# each line in the file. This can be very CPU-intensive. 203 204# To enable system wide have in $GITWEB_CONFIG 205# $feature{'blame'}{'default'} = [1]; 206# To have project specific config enable override in $GITWEB_CONFIG 207# $feature{'blame'}{'override'} = 1; 208# and in project config gitweb.blame = 0|1; 209'blame'=> { 210'sub'=>sub{ feature_bool('blame',@_) }, 211'override'=>0, 212'default'=> [0]}, 213 214# Enable the 'snapshot' link, providing a compressed archive of any 215# tree. This can potentially generate high traffic if you have large 216# project. 217 218# Value is a list of formats defined in %known_snapshot_formats that 219# you wish to offer. 220# To disable system wide have in $GITWEB_CONFIG 221# $feature{'snapshot'}{'default'} = []; 222# To have project specific config enable override in $GITWEB_CONFIG 223# $feature{'snapshot'}{'override'} = 1; 224# and in project config, a comma-separated list of formats or "none" 225# to disable. Example: gitweb.snapshot = tbz2,zip; 226'snapshot'=> { 227'sub'=> \&feature_snapshot, 228'override'=>0, 229'default'=> ['tgz']}, 230 231# Enable text search, which will list the commits which match author, 232# committer or commit text to a given string. Enabled by default. 233# Project specific override is not supported. 234'search'=> { 235'override'=>0, 236'default'=> [1]}, 237 238# Enable grep search, which will list the files in currently selected 239# tree containing the given string. Enabled by default. This can be 240# potentially CPU-intensive, of course. 241 242# To enable system wide have in $GITWEB_CONFIG 243# $feature{'grep'}{'default'} = [1]; 244# To have project specific config enable override in $GITWEB_CONFIG 245# $feature{'grep'}{'override'} = 1; 246# and in project config gitweb.grep = 0|1; 247'grep'=> { 248'sub'=>sub{ feature_bool('grep',@_) }, 249'override'=>0, 250'default'=> [1]}, 251 252# Enable the pickaxe search, which will list the commits that modified 253# a given string in a file. This can be practical and quite faster 254# alternative to 'blame', but still potentially CPU-intensive. 255 256# To enable system wide have in $GITWEB_CONFIG 257# $feature{'pickaxe'}{'default'} = [1]; 258# To have project specific config enable override in $GITWEB_CONFIG 259# $feature{'pickaxe'}{'override'} = 1; 260# and in project config gitweb.pickaxe = 0|1; 261'pickaxe'=> { 262'sub'=>sub{ feature_bool('pickaxe',@_) }, 263'override'=>0, 264'default'=> [1]}, 265 266# Make gitweb use an alternative format of the URLs which can be 267# more readable and natural-looking: project name is embedded 268# directly in the path and the query string contains other 269# auxiliary information. All gitweb installations recognize 270# URL in either format; this configures in which formats gitweb 271# generates links. 272 273# To enable system wide have in $GITWEB_CONFIG 274# $feature{'pathinfo'}{'default'} = [1]; 275# Project specific override is not supported. 276 277# Note that you will need to change the default location of CSS, 278# favicon, logo and possibly other files to an absolute URL. Also, 279# if gitweb.cgi serves as your indexfile, you will need to force 280# $my_uri to contain the script name in your $GITWEB_CONFIG. 281'pathinfo'=> { 282'override'=>0, 283'default'=> [0]}, 284 285# Make gitweb consider projects in project root subdirectories 286# to be forks of existing projects. Given project $projname.git, 287# projects matching $projname/*.git will not be shown in the main 288# projects list, instead a '+' mark will be added to $projname 289# there and a 'forks' view will be enabled for the project, listing 290# all the forks. If project list is taken from a file, forks have 291# to be listed after the main project. 292 293# To enable system wide have in $GITWEB_CONFIG 294# $feature{'forks'}{'default'} = [1]; 295# Project specific override is not supported. 296'forks'=> { 297'override'=>0, 298'default'=> [0]}, 299 300# Insert custom links to the action bar of all project pages. 301# This enables you mainly to link to third-party scripts integrating 302# into gitweb; e.g. git-browser for graphical history representation 303# or custom web-based repository administration interface. 304 305# The 'default' value consists of a list of triplets in the form 306# (label, link, position) where position is the label after which 307# to insert the link and link is a format string where %n expands 308# to the project name, %f to the project path within the filesystem, 309# %h to the current hash (h gitweb parameter) and %b to the current 310# hash base (hb gitweb parameter); %% expands to %. 311 312# To enable system wide have in $GITWEB_CONFIG e.g. 313# $feature{'actions'}{'default'} = [('graphiclog', 314# '/git-browser/by-commit.html?r=%n', 'summary')]; 315# Project specific override is not supported. 316'actions'=> { 317'override'=>0, 318'default'=> []}, 319 320# Allow gitweb scan project content tags described in ctags/ 321# of project repository, and display the popular Web 2.0-ish 322# "tag cloud" near the project list. Note that this is something 323# COMPLETELY different from the normal Git tags. 324 325# gitweb by itself can show existing tags, but it does not handle 326# tagging itself; you need an external application for that. 327# For an example script, check Girocco's cgi/tagproj.cgi. 328# You may want to install the HTML::TagCloud Perl module to get 329# a pretty tag cloud instead of just a list of tags. 330 331# To enable system wide have in $GITWEB_CONFIG 332# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 333# Project specific override is not supported. 334'ctags'=> { 335'override'=>0, 336'default'=> [0]}, 337 338# The maximum number of patches in a patchset generated in patch 339# view. Set this to 0 or undef to disable patch view, or to a 340# negative number to remove any limit. 341 342# To disable system wide have in $GITWEB_CONFIG 343# $feature{'patches'}{'default'} = [0]; 344# To have project specific config enable override in $GITWEB_CONFIG 345# $feature{'patches'}{'override'} = 1; 346# and in project config gitweb.patches = 0|n; 347# where n is the maximum number of patches allowed in a patchset. 348'patches'=> { 349'sub'=> \&feature_patches, 350'override'=>0, 351'default'=> [16]}, 352); 353 354sub gitweb_get_feature { 355my($name) =@_; 356return unlessexists$feature{$name}; 357my($sub,$override,@defaults) = ( 358$feature{$name}{'sub'}, 359$feature{$name}{'override'}, 360@{$feature{$name}{'default'}}); 361if(!$override) {return@defaults; } 362if(!defined$sub) { 363warn"feature$nameis not overrideable"; 364return@defaults; 365} 366return$sub->(@defaults); 367} 368 369# A wrapper to check if a given feature is enabled. 370# With this, you can say 371# 372# my $bool_feat = gitweb_check_feature('bool_feat'); 373# gitweb_check_feature('bool_feat') or somecode; 374# 375# instead of 376# 377# my ($bool_feat) = gitweb_get_feature('bool_feat'); 378# (gitweb_get_feature('bool_feat'))[0] or somecode; 379# 380sub gitweb_check_feature { 381return(gitweb_get_feature(@_))[0]; 382} 383 384 385sub feature_bool { 386my$key=shift; 387my($val) = git_get_project_config($key,'--bool'); 388 389if($valeq'true') { 390return(1); 391}elsif($valeq'false') { 392return(0); 393} 394 395return($_[0]); 396} 397 398sub feature_snapshot { 399my(@fmts) =@_; 400 401my($val) = git_get_project_config('snapshot'); 402 403if($val) { 404@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 405} 406 407return@fmts; 408} 409 410sub feature_patches { 411my@val= (git_get_project_config('patches','--int')); 412 413if(@val) { 414return@val; 415} 416 417return($_[0]); 418} 419 420# checking HEAD file with -e is fragile if the repository was 421# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 422# and then pruned. 423sub check_head_link { 424my($dir) =@_; 425my$headfile="$dir/HEAD"; 426return((-e $headfile) || 427(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 428} 429 430sub check_export_ok { 431my($dir) =@_; 432return(check_head_link($dir) && 433(!$export_ok|| -e "$dir/$export_ok") && 434(!$export_auth_hook||$export_auth_hook->($dir))); 435} 436 437# process alternate names for backward compatibility 438# filter out unsupported (unknown) snapshot formats 439sub filter_snapshot_fmts { 440my@fmts=@_; 441 442@fmts=map{ 443exists$known_snapshot_format_aliases{$_} ? 444$known_snapshot_format_aliases{$_} :$_}@fmts; 445@fmts=grep(exists$known_snapshot_formats{$_},@fmts); 446 447} 448 449our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 450if(-e $GITWEB_CONFIG) { 451do$GITWEB_CONFIG; 452}else{ 453our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 454do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 455} 456 457# version of the core git binary 458our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 459 460$projects_list||=$projectroot; 461 462# ====================================================================== 463# input validation and dispatch 464 465# input parameters can be collected from a variety of sources (presently, CGI 466# and PATH_INFO), so we define an %input_params hash that collects them all 467# together during validation: this allows subsequent uses (e.g. href()) to be 468# agnostic of the parameter origin 469 470our%input_params= (); 471 472# input parameters are stored with the long parameter name as key. This will 473# also be used in the href subroutine to convert parameters to their CGI 474# equivalent, and since the href() usage is the most frequent one, we store 475# the name -> CGI key mapping here, instead of the reverse. 476# 477# XXX: Warning: If you touch this, check the search form for updating, 478# too. 479 480our@cgi_param_mapping= ( 481 project =>"p", 482 action =>"a", 483 file_name =>"f", 484 file_parent =>"fp", 485 hash =>"h", 486 hash_parent =>"hp", 487 hash_base =>"hb", 488 hash_parent_base =>"hpb", 489 page =>"pg", 490 order =>"o", 491 searchtext =>"s", 492 searchtype =>"st", 493 snapshot_format =>"sf", 494 extra_options =>"opt", 495 search_use_regexp =>"sr", 496); 497our%cgi_param_mapping=@cgi_param_mapping; 498 499# we will also need to know the possible actions, for validation 500our%actions= ( 501"blame"=> \&git_blame, 502"blobdiff"=> \&git_blobdiff, 503"blobdiff_plain"=> \&git_blobdiff_plain, 504"blob"=> \&git_blob, 505"blob_plain"=> \&git_blob_plain, 506"commitdiff"=> \&git_commitdiff, 507"commitdiff_plain"=> \&git_commitdiff_plain, 508"commit"=> \&git_commit, 509"forks"=> \&git_forks, 510"heads"=> \&git_heads, 511"history"=> \&git_history, 512"log"=> \&git_log, 513"patch"=> \&git_patch, 514"patches"=> \&git_patches, 515"rss"=> \&git_rss, 516"atom"=> \&git_atom, 517"search"=> \&git_search, 518"search_help"=> \&git_search_help, 519"shortlog"=> \&git_shortlog, 520"summary"=> \&git_summary, 521"tag"=> \&git_tag, 522"tags"=> \&git_tags, 523"tree"=> \&git_tree, 524"snapshot"=> \&git_snapshot, 525"object"=> \&git_object, 526# those below don't need $project 527"opml"=> \&git_opml, 528"project_list"=> \&git_project_list, 529"project_index"=> \&git_project_index, 530); 531 532# finally, we have the hash of allowed extra_options for the commands that 533# allow them 534our%allowed_options= ( 535"--no-merges"=> [qw(rss atom log shortlog history)], 536); 537 538# fill %input_params with the CGI parameters. All values except for 'opt' 539# should be single values, but opt can be an array. We should probably 540# build an array of parameters that can be multi-valued, but since for the time 541# being it's only this one, we just single it out 542while(my($name,$symbol) =each%cgi_param_mapping) { 543if($symboleq'opt') { 544$input_params{$name} = [$cgi->param($symbol) ]; 545}else{ 546$input_params{$name} =$cgi->param($symbol); 547} 548} 549 550# now read PATH_INFO and update the parameter list for missing parameters 551sub evaluate_path_info { 552return ifdefined$input_params{'project'}; 553return if!$path_info; 554$path_info=~ s,^/+,,; 555return if!$path_info; 556 557# find which part of PATH_INFO is project 558my$project=$path_info; 559$project=~ s,/+$,,; 560while($project&& !check_head_link("$projectroot/$project")) { 561$project=~ s,/*[^/]*$,,; 562} 563return unless$project; 564$input_params{'project'} =$project; 565 566# do not change any parameters if an action is given using the query string 567return if$input_params{'action'}; 568$path_info=~ s,^\Q$project\E/*,,; 569 570# next, check if we have an action 571my$action=$path_info; 572$action=~ s,/.*$,,; 573if(exists$actions{$action}) { 574$path_info=~ s,^$action/*,,; 575$input_params{'action'} =$action; 576} 577 578# list of actions that want hash_base instead of hash, but can have no 579# pathname (f) parameter 580my@wants_base= ( 581'tree', 582'history', 583); 584 585# we want to catch 586# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 587my($parentrefname,$parentpathname,$refname,$pathname) = 588($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 589 590# first, analyze the 'current' part 591if(defined$pathname) { 592# we got "branch:filename" or "branch:dir/" 593# we could use git_get_type(branch:pathname), but: 594# - it needs $git_dir 595# - it does a git() call 596# - the convention of terminating directories with a slash 597# makes it superfluous 598# - embedding the action in the PATH_INFO would make it even 599# more superfluous 600$pathname=~ s,^/+,,; 601if(!$pathname||substr($pathname, -1)eq"/") { 602$input_params{'action'} ||="tree"; 603$pathname=~ s,/$,,; 604}else{ 605# the default action depends on whether we had parent info 606# or not 607if($parentrefname) { 608$input_params{'action'} ||="blobdiff_plain"; 609}else{ 610$input_params{'action'} ||="blob_plain"; 611} 612} 613$input_params{'hash_base'} ||=$refname; 614$input_params{'file_name'} ||=$pathname; 615}elsif(defined$refname) { 616# we got "branch". In this case we have to choose if we have to 617# set hash or hash_base. 618# 619# Most of the actions without a pathname only want hash to be 620# set, except for the ones specified in @wants_base that want 621# hash_base instead. It should also be noted that hand-crafted 622# links having 'history' as an action and no pathname or hash 623# set will fail, but that happens regardless of PATH_INFO. 624$input_params{'action'} ||="shortlog"; 625if(grep{$_eq$input_params{'action'} }@wants_base) { 626$input_params{'hash_base'} ||=$refname; 627}else{ 628$input_params{'hash'} ||=$refname; 629} 630} 631 632# next, handle the 'parent' part, if present 633if(defined$parentrefname) { 634# a missing pathspec defaults to the 'current' filename, allowing e.g. 635# someproject/blobdiff/oldrev..newrev:/filename 636if($parentpathname) { 637$parentpathname=~ s,^/+,,; 638$parentpathname=~ s,/$,,; 639$input_params{'file_parent'} ||=$parentpathname; 640}else{ 641$input_params{'file_parent'} ||=$input_params{'file_name'}; 642} 643# we assume that hash_parent_base is wanted if a path was specified, 644# or if the action wants hash_base instead of hash 645if(defined$input_params{'file_parent'} || 646grep{$_eq$input_params{'action'} }@wants_base) { 647$input_params{'hash_parent_base'} ||=$parentrefname; 648}else{ 649$input_params{'hash_parent'} ||=$parentrefname; 650} 651} 652 653# for the snapshot action, we allow URLs in the form 654# $project/snapshot/$hash.ext 655# where .ext determines the snapshot and gets removed from the 656# passed $refname to provide the $hash. 657# 658# To be able to tell that $refname includes the format extension, we 659# require the following two conditions to be satisfied: 660# - the hash input parameter MUST have been set from the $refname part 661# of the URL (i.e. they must be equal) 662# - the snapshot format MUST NOT have been defined already (e.g. from 663# CGI parameter sf) 664# It's also useless to try any matching unless $refname has a dot, 665# so we check for that too 666if(defined$input_params{'action'} && 667$input_params{'action'}eq'snapshot'&& 668defined$refname&&index($refname,'.') != -1&& 669$refnameeq$input_params{'hash'} && 670!defined$input_params{'snapshot_format'}) { 671# We loop over the known snapshot formats, checking for 672# extensions. Allowed extensions are both the defined suffix 673# (which includes the initial dot already) and the snapshot 674# format key itself, with a prepended dot 675while(my($fmt,%opt) =each%known_snapshot_formats) { 676my$hash=$refname; 677my$sfx; 678$hash=~s/(\Q$opt{'suffix'}\E|\Q.$fmt\E)$//; 679next unless$sfx=$1; 680# a valid suffix was found, so set the snapshot format 681# and reset the hash parameter 682$input_params{'snapshot_format'} =$fmt; 683$input_params{'hash'} =$hash; 684# we also set the format suffix to the one requested 685# in the URL: this way a request for e.g. .tgz returns 686# a .tgz instead of a .tar.gz 687$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 688last; 689} 690} 691} 692evaluate_path_info(); 693 694our$action=$input_params{'action'}; 695if(defined$action) { 696if(!validate_action($action)) { 697 die_error(400,"Invalid action parameter"); 698} 699} 700 701# parameters which are pathnames 702our$project=$input_params{'project'}; 703if(defined$project) { 704if(!validate_project($project)) { 705undef$project; 706 die_error(404,"No such project"); 707} 708} 709 710our$file_name=$input_params{'file_name'}; 711if(defined$file_name) { 712if(!validate_pathname($file_name)) { 713 die_error(400,"Invalid file parameter"); 714} 715} 716 717our$file_parent=$input_params{'file_parent'}; 718if(defined$file_parent) { 719if(!validate_pathname($file_parent)) { 720 die_error(400,"Invalid file parent parameter"); 721} 722} 723 724# parameters which are refnames 725our$hash=$input_params{'hash'}; 726if(defined$hash) { 727if(!validate_refname($hash)) { 728 die_error(400,"Invalid hash parameter"); 729} 730} 731 732our$hash_parent=$input_params{'hash_parent'}; 733if(defined$hash_parent) { 734if(!validate_refname($hash_parent)) { 735 die_error(400,"Invalid hash parent parameter"); 736} 737} 738 739our$hash_base=$input_params{'hash_base'}; 740if(defined$hash_base) { 741if(!validate_refname($hash_base)) { 742 die_error(400,"Invalid hash base parameter"); 743} 744} 745 746our@extra_options= @{$input_params{'extra_options'}}; 747# @extra_options is always defined, since it can only be (currently) set from 748# CGI, and $cgi->param() returns the empty array in array context if the param 749# is not set 750foreachmy$opt(@extra_options) { 751if(not exists$allowed_options{$opt}) { 752 die_error(400,"Invalid option parameter"); 753} 754if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 755 die_error(400,"Invalid option parameter for this action"); 756} 757} 758 759our$hash_parent_base=$input_params{'hash_parent_base'}; 760if(defined$hash_parent_base) { 761if(!validate_refname($hash_parent_base)) { 762 die_error(400,"Invalid hash parent base parameter"); 763} 764} 765 766# other parameters 767our$page=$input_params{'page'}; 768if(defined$page) { 769if($page=~m/[^0-9]/) { 770 die_error(400,"Invalid page parameter"); 771} 772} 773 774our$searchtype=$input_params{'searchtype'}; 775if(defined$searchtype) { 776if($searchtype=~m/[^a-z]/) { 777 die_error(400,"Invalid searchtype parameter"); 778} 779} 780 781our$search_use_regexp=$input_params{'search_use_regexp'}; 782 783our$searchtext=$input_params{'searchtext'}; 784our$search_regexp; 785if(defined$searchtext) { 786if(length($searchtext) <2) { 787 die_error(403,"At least two characters are required for search parameter"); 788} 789$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 790} 791 792# path to the current git repository 793our$git_dir; 794$git_dir="$projectroot/$project"if$project; 795 796# list of supported snapshot formats 797our@snapshot_fmts= gitweb_get_feature('snapshot'); 798@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 799 800# dispatch 801if(!defined$action) { 802if(defined$hash) { 803$action= git_get_type($hash); 804}elsif(defined$hash_base&&defined$file_name) { 805$action= git_get_type("$hash_base:$file_name"); 806}elsif(defined$project) { 807$action='summary'; 808}else{ 809$action='project_list'; 810} 811} 812if(!defined($actions{$action})) { 813 die_error(400,"Unknown action"); 814} 815if($action!~m/^(opml|project_list|project_index)$/&& 816!$project) { 817 die_error(400,"Project needed"); 818} 819$actions{$action}->(); 820exit; 821 822## ====================================================================== 823## action links 824 825sub href (%) { 826my%params=@_; 827# default is to use -absolute url() i.e. $my_uri 828my$href=$params{-full} ?$my_url:$my_uri; 829 830$params{'project'} =$projectunlessexists$params{'project'}; 831 832if($params{-replay}) { 833while(my($name,$symbol) =each%cgi_param_mapping) { 834if(!exists$params{$name}) { 835$params{$name} =$input_params{$name}; 836} 837} 838} 839 840my$use_pathinfo= gitweb_check_feature('pathinfo'); 841if($use_pathinfoand defined$params{'project'}) { 842# try to put as many parameters as possible in PATH_INFO: 843# - project name 844# - action 845# - hash_parent or hash_parent_base:/file_parent 846# - hash or hash_base:/filename 847# - the snapshot_format as an appropriate suffix 848 849# When the script is the root DirectoryIndex for the domain, 850# $href here would be something like http://gitweb.example.com/ 851# Thus, we strip any trailing / from $href, to spare us double 852# slashes in the final URL 853$href=~ s,/$,,; 854 855# Then add the project name, if present 856$href.="/".esc_url($params{'project'}); 857delete$params{'project'}; 858 859# since we destructively absorb parameters, we keep this 860# boolean that remembers if we're handling a snapshot 861my$is_snapshot=$params{'action'}eq'snapshot'; 862 863# Summary just uses the project path URL, any other action is 864# added to the URL 865if(defined$params{'action'}) { 866$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 867delete$params{'action'}; 868} 869 870# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 871# stripping nonexistent or useless pieces 872$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 873||$params{'hash_parent'} ||$params{'hash'}); 874if(defined$params{'hash_base'}) { 875if(defined$params{'hash_parent_base'}) { 876$href.= esc_url($params{'hash_parent_base'}); 877# skip the file_parent if it's the same as the file_name 878delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 879if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 880$href.=":/".esc_url($params{'file_parent'}); 881delete$params{'file_parent'}; 882} 883$href.=".."; 884delete$params{'hash_parent'}; 885delete$params{'hash_parent_base'}; 886}elsif(defined$params{'hash_parent'}) { 887$href.= esc_url($params{'hash_parent'}).".."; 888delete$params{'hash_parent'}; 889} 890 891$href.= esc_url($params{'hash_base'}); 892if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 893$href.=":/".esc_url($params{'file_name'}); 894delete$params{'file_name'}; 895} 896delete$params{'hash'}; 897delete$params{'hash_base'}; 898}elsif(defined$params{'hash'}) { 899$href.= esc_url($params{'hash'}); 900delete$params{'hash'}; 901} 902 903# If the action was a snapshot, we can absorb the 904# snapshot_format parameter too 905if($is_snapshot) { 906my$fmt=$params{'snapshot_format'}; 907# snapshot_format should always be defined when href() 908# is called, but just in case some code forgets, we 909# fall back to the default 910$fmt||=$snapshot_fmts[0]; 911$href.=$known_snapshot_formats{$fmt}{'suffix'}; 912delete$params{'snapshot_format'}; 913} 914} 915 916# now encode the parameters explicitly 917my@result= (); 918for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 919my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 920if(defined$params{$name}) { 921if(ref($params{$name})eq"ARRAY") { 922foreachmy$par(@{$params{$name}}) { 923push@result,$symbol."=". esc_param($par); 924} 925}else{ 926push@result,$symbol."=". esc_param($params{$name}); 927} 928} 929} 930$href.="?".join(';',@result)ifscalar@result; 931 932return$href; 933} 934 935 936## ====================================================================== 937## validation, quoting/unquoting and escaping 938 939sub validate_action { 940my$input=shift||returnundef; 941returnundefunlessexists$actions{$input}; 942return$input; 943} 944 945sub validate_project { 946my$input=shift||returnundef; 947if(!validate_pathname($input) || 948!(-d "$projectroot/$input") || 949!check_export_ok("$projectroot/$input") || 950($strict_export&& !project_in_list($input))) { 951returnundef; 952}else{ 953return$input; 954} 955} 956 957sub validate_pathname { 958my$input=shift||returnundef; 959 960# no '.' or '..' as elements of path, i.e. no '.' nor '..' 961# at the beginning, at the end, and between slashes. 962# also this catches doubled slashes 963if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 964returnundef; 965} 966# no null characters 967if($input=~m!\0!) { 968returnundef; 969} 970return$input; 971} 972 973sub validate_refname { 974my$input=shift||returnundef; 975 976# textual hashes are O.K. 977if($input=~m/^[0-9a-fA-F]{40}$/) { 978return$input; 979} 980# it must be correct pathname 981$input= validate_pathname($input) 982orreturnundef; 983# restrictions on ref name according to git-check-ref-format 984if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { 985returnundef; 986} 987return$input; 988} 989 990# decode sequences of octets in utf8 into Perl's internal form, 991# which is utf-8 with utf8 flag set if needed. gitweb writes out 992# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning 993sub to_utf8 { 994my$str=shift; 995if(utf8::valid($str)) { 996 utf8::decode($str); 997return$str; 998}else{ 999return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1000}1001}10021003# quote unsafe chars, but keep the slash, even when it's not1004# correct, but quoted slashes look too horrible in bookmarks1005sub esc_param {1006my$str=shift;1007$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg;1008$str=~s/\+/%2B/g;1009$str=~s/ /\+/g;1010return$str;1011}10121013# quote unsafe chars in whole URL, so some charactrs cannot be quoted1014sub esc_url {1015my$str=shift;1016$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1017$str=~s/\+/%2B/g;1018$str=~s/ /\+/g;1019return$str;1020}10211022# replace invalid utf8 character with SUBSTITUTION sequence1023sub esc_html ($;%) {1024my$str=shift;1025my%opts=@_;10261027$str= to_utf8($str);1028$str=$cgi->escapeHTML($str);1029if($opts{'-nbsp'}) {1030$str=~s/ / /g;1031}1032$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1033return$str;1034}10351036# quote control characters and escape filename to HTML1037sub esc_path {1038my$str=shift;1039my%opts=@_;10401041$str= to_utf8($str);1042$str=$cgi->escapeHTML($str);1043if($opts{'-nbsp'}) {1044$str=~s/ / /g;1045}1046$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1047return$str;1048}10491050# Make control characters "printable", using character escape codes (CEC)1051sub quot_cec {1052my$cntrl=shift;1053my%opts=@_;1054my%es= (# character escape codes, aka escape sequences1055"\t"=>'\t',# tab (HT)1056"\n"=>'\n',# line feed (LF)1057"\r"=>'\r',# carrige return (CR)1058"\f"=>'\f',# form feed (FF)1059"\b"=>'\b',# backspace (BS)1060"\a"=>'\a',# alarm (bell) (BEL)1061"\e"=>'\e',# escape (ESC)1062"\013"=>'\v',# vertical tab (VT)1063"\000"=>'\0',# nul character (NUL)1064);1065my$chr= ( (exists$es{$cntrl})1066?$es{$cntrl}1067:sprintf('\%2x',ord($cntrl)) );1068if($opts{-nohtml}) {1069return$chr;1070}else{1071return"<span class=\"cntrl\">$chr</span>";1072}1073}10741075# Alternatively use unicode control pictures codepoints,1076# Unicode "printable representation" (PR)1077sub quot_upr {1078my$cntrl=shift;1079my%opts=@_;10801081my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1082if($opts{-nohtml}) {1083return$chr;1084}else{1085return"<span class=\"cntrl\">$chr</span>";1086}1087}10881089# git may return quoted and escaped filenames1090sub unquote {1091my$str=shift;10921093sub unq {1094my$seq=shift;1095my%es= (# character escape codes, aka escape sequences1096't'=>"\t",# tab (HT, TAB)1097'n'=>"\n",# newline (NL)1098'r'=>"\r",# return (CR)1099'f'=>"\f",# form feed (FF)1100'b'=>"\b",# backspace (BS)1101'a'=>"\a",# alarm (bell) (BEL)1102'e'=>"\e",# escape (ESC)1103'v'=>"\013",# vertical tab (VT)1104);11051106if($seq=~m/^[0-7]{1,3}$/) {1107# octal char sequence1108returnchr(oct($seq));1109}elsif(exists$es{$seq}) {1110# C escape sequence, aka character escape code1111return$es{$seq};1112}1113# quoted ordinary character1114return$seq;1115}11161117if($str=~m/^"(.*)"$/) {1118# needs unquoting1119$str=$1;1120$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1121}1122return$str;1123}11241125# escape tabs (convert tabs to spaces)1126sub untabify {1127my$line=shift;11281129while((my$pos=index($line,"\t")) != -1) {1130if(my$count= (8- ($pos%8))) {1131my$spaces=' ' x $count;1132$line=~s/\t/$spaces/;1133}1134}11351136return$line;1137}11381139sub project_in_list {1140my$project=shift;1141my@list= git_get_projects_list();1142return@list&&scalar(grep{$_->{'path'}eq$project}@list);1143}11441145## ----------------------------------------------------------------------1146## HTML aware string manipulation11471148# Try to chop given string on a word boundary between position1149# $len and $len+$add_len. If there is no word boundary there,1150# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1151# (marking chopped part) would be longer than given string.1152sub chop_str {1153my$str=shift;1154my$len=shift;1155my$add_len=shift||10;1156my$where=shift||'right';# 'left' | 'center' | 'right'11571158# Make sure perl knows it is utf8 encoded so we don't1159# cut in the middle of a utf8 multibyte char.1160$str= to_utf8($str);11611162# allow only $len chars, but don't cut a word if it would fit in $add_len1163# if it doesn't fit, cut it if it's still longer than the dots we would add1164# remove chopped character entities entirely11651166# when chopping in the middle, distribute $len into left and right part1167# return early if chopping wouldn't make string shorter1168if($whereeq'center') {1169return$strif($len+5>=length($str));# filler is length 51170$len=int($len/2);1171}else{1172return$strif($len+4>=length($str));# filler is length 41173}11741175# regexps: ending and beginning with word part up to $add_len1176my$endre=qr/.{$len}\w{0,$add_len}/;1177my$begre=qr/\w{0,$add_len}.{$len}/;11781179if($whereeq'left') {1180$str=~m/^(.*?)($begre)$/;1181my($lead,$body) = ($1,$2);1182if(length($lead) >4) {1183$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1184$lead=" ...";1185}1186return"$lead$body";11871188}elsif($whereeq'center') {1189$str=~m/^($endre)(.*)$/;1190my($left,$str) = ($1,$2);1191$str=~m/^(.*?)($begre)$/;1192my($mid,$right) = ($1,$2);1193if(length($mid) >5) {1194$left=~s/&[^;]*$//;1195$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1196$mid=" ... ";1197}1198return"$left$mid$right";11991200}else{1201$str=~m/^($endre)(.*)$/;1202my$body=$1;1203my$tail=$2;1204if(length($tail) >4) {1205$body=~s/&[^;]*$//;1206$tail="... ";1207}1208return"$body$tail";1209}1210}12111212# takes the same arguments as chop_str, but also wraps a <span> around the1213# result with a title attribute if it does get chopped. Additionally, the1214# string is HTML-escaped.1215sub chop_and_escape_str {1216my($str) =@_;12171218my$chopped= chop_str(@_);1219if($choppedeq$str) {1220return esc_html($chopped);1221}else{1222$str=~s/([[:cntrl:]])/?/g;1223return$cgi->span({-title=>$str}, esc_html($chopped));1224}1225}12261227## ----------------------------------------------------------------------1228## functions returning short strings12291230# CSS class for given age value (in seconds)1231sub age_class {1232my$age=shift;12331234if(!defined$age) {1235return"noage";1236}elsif($age<60*60*2) {1237return"age0";1238}elsif($age<60*60*24*2) {1239return"age1";1240}else{1241return"age2";1242}1243}12441245# convert age in seconds to "nn units ago" string1246sub age_string {1247my$age=shift;1248my$age_str;12491250if($age>60*60*24*365*2) {1251$age_str= (int$age/60/60/24/365);1252$age_str.=" years ago";1253}elsif($age>60*60*24*(365/12)*2) {1254$age_str=int$age/60/60/24/(365/12);1255$age_str.=" months ago";1256}elsif($age>60*60*24*7*2) {1257$age_str=int$age/60/60/24/7;1258$age_str.=" weeks ago";1259}elsif($age>60*60*24*2) {1260$age_str=int$age/60/60/24;1261$age_str.=" days ago";1262}elsif($age>60*60*2) {1263$age_str=int$age/60/60;1264$age_str.=" hours ago";1265}elsif($age>60*2) {1266$age_str=int$age/60;1267$age_str.=" min ago";1268}elsif($age>2) {1269$age_str=int$age;1270$age_str.=" sec ago";1271}else{1272$age_str.=" right now";1273}1274return$age_str;1275}12761277useconstant{1278 S_IFINVALID =>0030000,1279 S_IFGITLINK =>0160000,1280};12811282# submodule/subproject, a commit object reference1283sub S_ISGITLINK($) {1284my$mode=shift;12851286return(($mode& S_IFMT) == S_IFGITLINK)1287}12881289# convert file mode in octal to symbolic file mode string1290sub mode_str {1291my$mode=oct shift;12921293if(S_ISGITLINK($mode)) {1294return'm---------';1295}elsif(S_ISDIR($mode& S_IFMT)) {1296return'drwxr-xr-x';1297}elsif(S_ISLNK($mode)) {1298return'lrwxrwxrwx';1299}elsif(S_ISREG($mode)) {1300# git cares only about the executable bit1301if($mode& S_IXUSR) {1302return'-rwxr-xr-x';1303}else{1304return'-rw-r--r--';1305};1306}else{1307return'----------';1308}1309}13101311# convert file mode in octal to file type string1312sub file_type {1313my$mode=shift;13141315if($mode!~m/^[0-7]+$/) {1316return$mode;1317}else{1318$mode=oct$mode;1319}13201321if(S_ISGITLINK($mode)) {1322return"submodule";1323}elsif(S_ISDIR($mode& S_IFMT)) {1324return"directory";1325}elsif(S_ISLNK($mode)) {1326return"symlink";1327}elsif(S_ISREG($mode)) {1328return"file";1329}else{1330return"unknown";1331}1332}13331334# convert file mode in octal to file type description string1335sub file_type_long {1336my$mode=shift;13371338if($mode!~m/^[0-7]+$/) {1339return$mode;1340}else{1341$mode=oct$mode;1342}13431344if(S_ISGITLINK($mode)) {1345return"submodule";1346}elsif(S_ISDIR($mode& S_IFMT)) {1347return"directory";1348}elsif(S_ISLNK($mode)) {1349return"symlink";1350}elsif(S_ISREG($mode)) {1351if($mode& S_IXUSR) {1352return"executable";1353}else{1354return"file";1355};1356}else{1357return"unknown";1358}1359}136013611362## ----------------------------------------------------------------------1363## functions returning short HTML fragments, or transforming HTML fragments1364## which don't belong to other sections13651366# format line of commit message.1367sub format_log_line_html {1368my$line=shift;13691370$line= esc_html($line, -nbsp=>1);1371if($line=~m/([0-9a-fA-F]{8,40})/) {1372my$hash_text=$1;1373my$link=1374$cgi->a({-href => href(action=>"object", hash=>$hash_text),1375-class=>"text"},$hash_text);1376$line=~s/$hash_text/$link/;1377}1378return$line;1379}13801381# format marker of refs pointing to given object13821383# the destination action is chosen based on object type and current context:1384# - for annotated tags, we choose the tag view unless it's the current view1385# already, in which case we go to shortlog view1386# - for other refs, we keep the current view if we're in history, shortlog or1387# log view, and select shortlog otherwise1388sub format_ref_marker {1389my($refs,$id) =@_;1390my$markers='';13911392if(defined$refs->{$id}) {1393foreachmy$ref(@{$refs->{$id}}) {1394# this code exploits the fact that non-lightweight tags are the1395# only indirect objects, and that they are the only objects for which1396# we want to use tag instead of shortlog as action1397my($type,$name) =qw();1398my$indirect= ($ref=~s/\^\{\}$//);1399# e.g. tags/v2.6.11 or heads/next1400if($ref=~m!^(.*?)s?/(.*)$!) {1401$type=$1;1402$name=$2;1403}else{1404$type="ref";1405$name=$ref;1406}14071408my$class=$type;1409$class.=" indirect"if$indirect;14101411my$dest_action="shortlog";14121413if($indirect) {1414$dest_action="tag"unless$actioneq"tag";1415}elsif($action=~/^(history|(short)?log)$/) {1416$dest_action=$action;1417}14181419my$dest="";1420$dest.="refs/"unless$ref=~ m!^refs/!;1421$dest.=$ref;14221423my$link=$cgi->a({1424-href => href(1425 action=>$dest_action,1426 hash=>$dest1427)},$name);14281429$markers.=" <span class=\"$class\"title=\"$ref\">".1430$link."</span>";1431}1432}14331434if($markers) {1435return' <span class="refs">'.$markers.'</span>';1436}else{1437return"";1438}1439}14401441# format, perhaps shortened and with markers, title line1442sub format_subject_html {1443my($long,$short,$href,$extra) =@_;1444$extra=''unlessdefined($extra);14451446if(length($short) <length($long)) {1447return$cgi->a({-href =>$href, -class=>"list subject",1448-title => to_utf8($long)},1449 esc_html($short) .$extra);1450}else{1451return$cgi->a({-href =>$href, -class=>"list subject"},1452 esc_html($long) .$extra);1453}1454}14551456# format git diff header line, i.e. "diff --(git|combined|cc) ..."1457sub format_git_diff_header_line {1458my$line=shift;1459my$diffinfo=shift;1460my($from,$to) =@_;14611462if($diffinfo->{'nparents'}) {1463# combined diff1464$line=~s!^(diff (.*?) )"?.*$!$1!;1465if($to->{'href'}) {1466$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1467 esc_path($to->{'file'}));1468}else{# file was deleted (no href)1469$line.= esc_path($to->{'file'});1470}1471}else{1472# "ordinary" diff1473$line=~s!^(diff (.*?) )"?a/.*$!$1!;1474if($from->{'href'}) {1475$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1476'a/'. esc_path($from->{'file'}));1477}else{# file was added (no href)1478$line.='a/'. esc_path($from->{'file'});1479}1480$line.=' ';1481if($to->{'href'}) {1482$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1483'b/'. esc_path($to->{'file'}));1484}else{# file was deleted1485$line.='b/'. esc_path($to->{'file'});1486}1487}14881489return"<div class=\"diff header\">$line</div>\n";1490}14911492# format extended diff header line, before patch itself1493sub format_extended_diff_header_line {1494my$line=shift;1495my$diffinfo=shift;1496my($from,$to) =@_;14971498# match <path>1499if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1500$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1501 esc_path($from->{'file'}));1502}1503if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1504$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1505 esc_path($to->{'file'}));1506}1507# match single <mode>1508if($line=~m/\s(\d{6})$/) {1509$line.='<span class="info"> ('.1510 file_type_long($1) .1511')</span>';1512}1513# match <hash>1514if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1515# can match only for combined diff1516$line='index ';1517for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1518if($from->{'href'}[$i]) {1519$line.=$cgi->a({-href=>$from->{'href'}[$i],1520-class=>"hash"},1521substr($diffinfo->{'from_id'}[$i],0,7));1522}else{1523$line.='0' x 7;1524}1525# separator1526$line.=','if($i<$diffinfo->{'nparents'} -1);1527}1528$line.='..';1529if($to->{'href'}) {1530$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1531substr($diffinfo->{'to_id'},0,7));1532}else{1533$line.='0' x 7;1534}15351536}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1537# can match only for ordinary diff1538my($from_link,$to_link);1539if($from->{'href'}) {1540$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1541substr($diffinfo->{'from_id'},0,7));1542}else{1543$from_link='0' x 7;1544}1545if($to->{'href'}) {1546$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1547substr($diffinfo->{'to_id'},0,7));1548}else{1549$to_link='0' x 7;1550}1551my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1552$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1553}15541555return$line."<br/>\n";1556}15571558# format from-file/to-file diff header1559sub format_diff_from_to_header {1560my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1561my$line;1562my$result='';15631564$line=$from_line;1565#assert($line =~ m/^---/) if DEBUG;1566# no extra formatting for "^--- /dev/null"1567if(!$diffinfo->{'nparents'}) {1568# ordinary (single parent) diff1569if($line=~m!^--- "?a/!) {1570if($from->{'href'}) {1571$line='--- a/'.1572$cgi->a({-href=>$from->{'href'}, -class=>"path"},1573 esc_path($from->{'file'}));1574}else{1575$line='--- a/'.1576 esc_path($from->{'file'});1577}1578}1579$result.= qq!<div class="diff from_file">$line</div>\n!;15801581}else{1582# combined diff (merge commit)1583for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1584if($from->{'href'}[$i]) {1585$line='--- '.1586$cgi->a({-href=>href(action=>"blobdiff",1587 hash_parent=>$diffinfo->{'from_id'}[$i],1588 hash_parent_base=>$parents[$i],1589 file_parent=>$from->{'file'}[$i],1590 hash=>$diffinfo->{'to_id'},1591 hash_base=>$hash,1592 file_name=>$to->{'file'}),1593-class=>"path",1594-title=>"diff". ($i+1)},1595$i+1) .1596'/'.1597$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1598 esc_path($from->{'file'}[$i]));1599}else{1600$line='--- /dev/null';1601}1602$result.= qq!<div class="diff from_file">$line</div>\n!;1603}1604}16051606$line=$to_line;1607#assert($line =~ m/^\+\+\+/) if DEBUG;1608# no extra formatting for "^+++ /dev/null"1609if($line=~m!^\+\+\+ "?b/!) {1610if($to->{'href'}) {1611$line='+++ b/'.1612$cgi->a({-href=>$to->{'href'}, -class=>"path"},1613 esc_path($to->{'file'}));1614}else{1615$line='+++ b/'.1616 esc_path($to->{'file'});1617}1618}1619$result.= qq!<div class="diff to_file">$line</div>\n!;16201621return$result;1622}16231624# create note for patch simplified by combined diff1625sub format_diff_cc_simplified {1626my($diffinfo,@parents) =@_;1627my$result='';16281629$result.="<div class=\"diff header\">".1630"diff --cc ";1631if(!is_deleted($diffinfo)) {1632$result.=$cgi->a({-href => href(action=>"blob",1633 hash_base=>$hash,1634 hash=>$diffinfo->{'to_id'},1635 file_name=>$diffinfo->{'to_file'}),1636-class=>"path"},1637 esc_path($diffinfo->{'to_file'}));1638}else{1639$result.= esc_path($diffinfo->{'to_file'});1640}1641$result.="</div>\n".# class="diff header"1642"<div class=\"diff nodifferences\">".1643"Simple merge".1644"</div>\n";# class="diff nodifferences"16451646return$result;1647}16481649# format patch (diff) line (not to be used for diff headers)1650sub format_diff_line {1651my$line=shift;1652my($from,$to) =@_;1653my$diff_class="";16541655chomp$line;16561657if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1658# combined diff1659my$prefix=substr($line,0,scalar@{$from->{'href'}});1660if($line=~m/^\@{3}/) {1661$diff_class=" chunk_header";1662}elsif($line=~m/^\\/) {1663$diff_class=" incomplete";1664}elsif($prefix=~tr/+/+/) {1665$diff_class=" add";1666}elsif($prefix=~tr/-/-/) {1667$diff_class=" rem";1668}1669}else{1670# assume ordinary diff1671my$char=substr($line,0,1);1672if($chareq'+') {1673$diff_class=" add";1674}elsif($chareq'-') {1675$diff_class=" rem";1676}elsif($chareq'@') {1677$diff_class=" chunk_header";1678}elsif($chareq"\\") {1679$diff_class=" incomplete";1680}1681}1682$line= untabify($line);1683if($from&&$to&&$line=~m/^\@{2} /) {1684my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1685$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;16861687$from_lines=0unlessdefined$from_lines;1688$to_lines=0unlessdefined$to_lines;16891690if($from->{'href'}) {1691$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1692-class=>"list"},$from_text);1693}1694if($to->{'href'}) {1695$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1696-class=>"list"},$to_text);1697}1698$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1699"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1700return"<div class=\"diff$diff_class\">$line</div>\n";1701}elsif($from&&$to&&$line=~m/^\@{3}/) {1702my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1703my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);17041705@from_text=split(' ',$ranges);1706for(my$i=0;$i<@from_text; ++$i) {1707($from_start[$i],$from_nlines[$i]) =1708(split(',',substr($from_text[$i],1)),0);1709}17101711$to_text=pop@from_text;1712$to_start=pop@from_start;1713$to_nlines=pop@from_nlines;17141715$line="<span class=\"chunk_info\">$prefix";1716for(my$i=0;$i<@from_text; ++$i) {1717if($from->{'href'}[$i]) {1718$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1719-class=>"list"},$from_text[$i]);1720}else{1721$line.=$from_text[$i];1722}1723$line.=" ";1724}1725if($to->{'href'}) {1726$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1727-class=>"list"},$to_text);1728}else{1729$line.=$to_text;1730}1731$line.="$prefix</span>".1732"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1733return"<div class=\"diff$diff_class\">$line</div>\n";1734}1735return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1736}17371738# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1739# linked. Pass the hash of the tree/commit to snapshot.1740sub format_snapshot_links {1741my($hash) =@_;1742my$num_fmts=@snapshot_fmts;1743if($num_fmts>1) {1744# A parenthesized list of links bearing format names.1745# e.g. "snapshot (_tar.gz_ _zip_)"1746return"snapshot (".join(' ',map1747$cgi->a({1748-href => href(1749 action=>"snapshot",1750 hash=>$hash,1751 snapshot_format=>$_1752)1753},$known_snapshot_formats{$_}{'display'})1754,@snapshot_fmts) .")";1755}elsif($num_fmts==1) {1756# A single "snapshot" link whose tooltip bears the format name.1757# i.e. "_snapshot_"1758my($fmt) =@snapshot_fmts;1759return1760$cgi->a({1761-href => href(1762 action=>"snapshot",1763 hash=>$hash,1764 snapshot_format=>$fmt1765),1766-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1767},"snapshot");1768}else{# $num_fmts == 01769returnundef;1770}1771}17721773## ......................................................................1774## functions returning values to be passed, perhaps after some1775## transformation, to other functions; e.g. returning arguments to href()17761777# returns hash to be passed to href to generate gitweb URL1778# in -title key it returns description of link1779sub get_feed_info {1780my$format=shift||'Atom';1781my%res= (action =>lc($format));17821783# feed links are possible only for project views1784return unless(defined$project);1785# some views should link to OPML, or to generic project feed,1786# or don't have specific feed yet (so they should use generic)1787return if($action=~/^(?:tags|heads|forks|tag|search)$/x);17881789my$branch;1790# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1791# from tag links; this also makes possible to detect branch links1792if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1793(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1794$branch=$1;1795}1796# find log type for feed description (title)1797my$type='log';1798if(defined$file_name) {1799$type="history of$file_name";1800$type.="/"if($actioneq'tree');1801$type.=" on '$branch'"if(defined$branch);1802}else{1803$type="log of$branch"if(defined$branch);1804}18051806$res{-title} =$type;1807$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1808$res{'file_name'} =$file_name;18091810return%res;1811}18121813## ----------------------------------------------------------------------1814## git utility subroutines, invoking git commands18151816# returns path to the core git executable and the --git-dir parameter as list1817sub git_cmd {1818return$GIT,'--git-dir='.$git_dir;1819}18201821# quote the given arguments for passing them to the shell1822# quote_command("command", "arg 1", "arg with ' and ! characters")1823# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1824# Try to avoid using this function wherever possible.1825sub quote_command {1826returnjoin(' ',1827map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1828}18291830# get HEAD ref of given project as hash1831sub git_get_head_hash {1832my$project=shift;1833my$o_git_dir=$git_dir;1834my$retval=undef;1835$git_dir="$projectroot/$project";1836if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1837my$head= <$fd>;1838close$fd;1839if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1840$retval=$1;1841}1842}1843if(defined$o_git_dir) {1844$git_dir=$o_git_dir;1845}1846return$retval;1847}18481849# get type of given object1850sub git_get_type {1851my$hash=shift;18521853open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1854my$type= <$fd>;1855close$fdorreturn;1856chomp$type;1857return$type;1858}18591860# repository configuration1861our$config_file='';1862our%config;18631864# store multiple values for single key as anonymous array reference1865# single values stored directly in the hash, not as [ <value> ]1866sub hash_set_multi {1867my($hash,$key,$value) =@_;18681869if(!exists$hash->{$key}) {1870$hash->{$key} =$value;1871}elsif(!ref$hash->{$key}) {1872$hash->{$key} = [$hash->{$key},$value];1873}else{1874push@{$hash->{$key}},$value;1875}1876}18771878# return hash of git project configuration1879# optionally limited to some section, e.g. 'gitweb'1880sub git_parse_project_config {1881my$section_regexp=shift;1882my%config;18831884local$/="\0";18851886open my$fh,"-|", git_cmd(),"config",'-z','-l',1887orreturn;18881889while(my$keyval= <$fh>) {1890chomp$keyval;1891my($key,$value) =split(/\n/,$keyval,2);18921893 hash_set_multi(\%config,$key,$value)1894if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1895}1896close$fh;18971898return%config;1899}19001901# convert config value to boolean, 'true' or 'false'1902# no value, number > 0, 'true' and 'yes' values are true1903# rest of values are treated as false (never as error)1904sub config_to_bool {1905my$val=shift;19061907# strip leading and trailing whitespace1908$val=~s/^\s+//;1909$val=~s/\s+$//;19101911return(!defined$val||# section.key1912($val=~/^\d+$/&&$val) ||# section.key = 11913($val=~/^(?:true|yes)$/i));# section.key = true1914}19151916# convert config value to simple decimal number1917# an optional value suffix of 'k', 'm', or 'g' will cause the value1918# to be multiplied by 1024, 1048576, or 10737418241919sub config_to_int {1920my$val=shift;19211922# strip leading and trailing whitespace1923$val=~s/^\s+//;1924$val=~s/\s+$//;19251926if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1927$unit=lc($unit);1928# unknown unit is treated as 11929return$num* ($uniteq'g'?1073741824:1930$uniteq'm'?1048576:1931$uniteq'k'?1024:1);1932}1933return$val;1934}19351936# convert config value to array reference, if needed1937sub config_to_multi {1938my$val=shift;19391940returnref($val) ?$val: (defined($val) ? [$val] : []);1941}19421943sub git_get_project_config {1944my($key,$type) =@_;19451946# key sanity check1947return unless($key);1948$key=~s/^gitweb\.//;1949return if($key=~m/\W/);19501951# type sanity check1952if(defined$type) {1953$type=~s/^--//;1954$type=undef1955unless($typeeq'bool'||$typeeq'int');1956}19571958# get config1959if(!defined$config_file||1960$config_filene"$git_dir/config") {1961%config= git_parse_project_config('gitweb');1962$config_file="$git_dir/config";1963}19641965# ensure given type1966if(!defined$type) {1967return$config{"gitweb.$key"};1968}elsif($typeeq'bool') {1969# backward compatibility: 'git config --bool' returns true/false1970return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1971}elsif($typeeq'int') {1972return config_to_int($config{"gitweb.$key"});1973}1974return$config{"gitweb.$key"};1975}19761977# get hash of given path at given ref1978sub git_get_hash_by_path {1979my$base=shift;1980my$path=shift||returnundef;1981my$type=shift;19821983$path=~ s,/+$,,;19841985open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path1986or die_error(500,"Open git-ls-tree failed");1987my$line= <$fd>;1988close$fdorreturnundef;19891990if(!defined$line) {1991# there is no tree or hash given by $path at $base1992returnundef;1993}19941995#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'1996$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;1997if(defined$type&&$typene$2) {1998# type doesn't match1999returnundef;2000}2001return$3;2002}20032004# get path of entry with given hash at given tree-ish (ref)2005# used to get 'from' filename for combined diff (merge commit) for renames2006sub git_get_path_by_hash {2007my$base=shift||return;2008my$hash=shift||return;20092010local$/="\0";20112012open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2013orreturnundef;2014while(my$line= <$fd>) {2015chomp$line;20162017#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2018#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2019if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2020close$fd;2021return$1;2022}2023}2024close$fd;2025returnundef;2026}20272028## ......................................................................2029## git utility functions, directly accessing git repository20302031sub git_get_project_description {2032my$path=shift;20332034$git_dir="$projectroot/$path";2035open my$fd,"$git_dir/description"2036orreturn git_get_project_config('description');2037my$descr= <$fd>;2038close$fd;2039if(defined$descr) {2040chomp$descr;2041}2042return$descr;2043}20442045sub git_get_project_ctags {2046my$path=shift;2047my$ctags= {};20482049$git_dir="$projectroot/$path";2050unless(opendir D,"$git_dir/ctags") {2051return$ctags;2052}2053foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir(D)) {2054open CT,$_ornext;2055my$val= <CT>;2056chomp$val;2057close CT;2058my$ctag=$_;$ctag=~ s#.*/##;2059$ctags->{$ctag} =$val;2060}2061closedir D;2062$ctags;2063}20642065sub git_populate_project_tagcloud {2066my$ctags=shift;20672068# First, merge different-cased tags; tags vote on casing2069my%ctags_lc;2070foreach(keys%$ctags) {2071$ctags_lc{lc$_}->{count} +=$ctags->{$_};2072if(not$ctags_lc{lc$_}->{topcount}2073or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2074$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2075$ctags_lc{lc$_}->{topname} =$_;2076}2077}20782079my$cloud;2080if(eval{require HTML::TagCloud;1; }) {2081$cloud= HTML::TagCloud->new;2082foreach(sort keys%ctags_lc) {2083# Pad the title with spaces so that the cloud looks2084# less crammed.2085my$title=$ctags_lc{$_}->{topname};2086$title=~s/ / /g;2087$title=~s/^/ /g;2088$title=~s/$/ /g;2089$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2090}2091}else{2092$cloud= \%ctags_lc;2093}2094$cloud;2095}20962097sub git_show_project_tagcloud {2098my($cloud,$count) =@_;2099print STDERR ref($cloud)."..\n";2100if(ref$cloudeq'HTML::TagCloud') {2101return$cloud->html_and_css($count);2102}else{2103my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2104return'<p align="center">'.join(', ',map{2105"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2106}splice(@tags,0,$count)) .'</p>';2107}2108}21092110sub git_get_project_url_list {2111my$path=shift;21122113$git_dir="$projectroot/$path";2114open my$fd,"$git_dir/cloneurl"2115orreturnwantarray?2116@{ config_to_multi(git_get_project_config('url')) } :2117 config_to_multi(git_get_project_config('url'));2118my@git_project_url_list=map{chomp;$_} <$fd>;2119close$fd;21202121returnwantarray?@git_project_url_list: \@git_project_url_list;2122}21232124sub git_get_projects_list {2125my($filter) =@_;2126my@list;21272128$filter||='';2129$filter=~s/\.git$//;21302131my$check_forks= gitweb_check_feature('forks');21322133if(-d $projects_list) {2134# search in directory2135my$dir=$projects_list. ($filter?"/$filter":'');2136# remove the trailing "/"2137$dir=~s!/+$!!;2138my$pfxlen=length("$dir");2139my$pfxdepth= ($dir=~tr!/!!);21402141 File::Find::find({2142 follow_fast =>1,# follow symbolic links2143 follow_skip =>2,# ignore duplicates2144 dangling_symlinks =>0,# ignore dangling symlinks, silently2145 wanted =>sub{2146# skip project-list toplevel, if we get it.2147return if(m!^[/.]$!);2148# only directories can be git repositories2149return unless(-d $_);2150# don't traverse too deep (Find is super slow on os x)2151if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2152$File::Find::prune =1;2153return;2154}21552156my$subdir=substr($File::Find::name,$pfxlen+1);2157# we check related file in $projectroot2158my$path= ($filter?"$filter/":'') .$subdir;2159if(check_export_ok("$projectroot/$path")) {2160push@list, { path =>$path};2161$File::Find::prune =1;2162}2163},2164},"$dir");21652166}elsif(-f $projects_list) {2167# read from file(url-encoded):2168# 'git%2Fgit.git Linus+Torvalds'2169# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2170# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2171my%paths;2172open my($fd),$projects_listorreturn;2173 PROJECT:2174while(my$line= <$fd>) {2175chomp$line;2176my($path,$owner) =split' ',$line;2177$path= unescape($path);2178$owner= unescape($owner);2179if(!defined$path) {2180next;2181}2182if($filterne'') {2183# looking for forks;2184my$pfx=substr($path,0,length($filter));2185if($pfxne$filter) {2186next PROJECT;2187}2188my$sfx=substr($path,length($filter));2189if($sfx!~/^\/.*\.git$/) {2190next PROJECT;2191}2192}elsif($check_forks) {2193 PATH:2194foreachmy$filter(keys%paths) {2195# looking for forks;2196my$pfx=substr($path,0,length($filter));2197if($pfxne$filter) {2198next PATH;2199}2200my$sfx=substr($path,length($filter));2201if($sfx!~/^\/.*\.git$/) {2202next PATH;2203}2204# is a fork, don't include it in2205# the list2206next PROJECT;2207}2208}2209if(check_export_ok("$projectroot/$path")) {2210my$pr= {2211 path =>$path,2212 owner => to_utf8($owner),2213};2214push@list,$pr;2215(my$forks_path=$path) =~s/\.git$//;2216$paths{$forks_path}++;2217}2218}2219close$fd;2220}2221return@list;2222}22232224our$gitweb_project_owner=undef;2225sub git_get_project_list_from_file {22262227return if(defined$gitweb_project_owner);22282229$gitweb_project_owner= {};2230# read from file (url-encoded):2231# 'git%2Fgit.git Linus+Torvalds'2232# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2233# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2234if(-f $projects_list) {2235open(my$fd,$projects_list);2236while(my$line= <$fd>) {2237chomp$line;2238my($pr,$ow) =split' ',$line;2239$pr= unescape($pr);2240$ow= unescape($ow);2241$gitweb_project_owner->{$pr} = to_utf8($ow);2242}2243close$fd;2244}2245}22462247sub git_get_project_owner {2248my$project=shift;2249my$owner;22502251returnundefunless$project;2252$git_dir="$projectroot/$project";22532254if(!defined$gitweb_project_owner) {2255 git_get_project_list_from_file();2256}22572258if(exists$gitweb_project_owner->{$project}) {2259$owner=$gitweb_project_owner->{$project};2260}2261if(!defined$owner){2262$owner= git_get_project_config('owner');2263}2264if(!defined$owner) {2265$owner= get_file_owner("$git_dir");2266}22672268return$owner;2269}22702271sub git_get_last_activity {2272my($path) =@_;2273my$fd;22742275$git_dir="$projectroot/$path";2276open($fd,"-|", git_cmd(),'for-each-ref',2277'--format=%(committer)',2278'--sort=-committerdate',2279'--count=1',2280'refs/heads')orreturn;2281my$most_recent= <$fd>;2282close$fdorreturn;2283if(defined$most_recent&&2284$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2285my$timestamp=$1;2286my$age=time-$timestamp;2287return($age, age_string($age));2288}2289return(undef,undef);2290}22912292sub git_get_references {2293my$type=shift||"";2294my%refs;2295# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112296# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2297open my$fd,"-|", git_cmd(),"show-ref","--dereference",2298($type? ("--","refs/$type") : ())# use -- <pattern> if $type2299orreturn;23002301while(my$line= <$fd>) {2302chomp$line;2303if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2304if(defined$refs{$1}) {2305push@{$refs{$1}},$2;2306}else{2307$refs{$1} = [$2];2308}2309}2310}2311close$fdorreturn;2312return \%refs;2313}23142315sub git_get_rev_name_tags {2316my$hash=shift||returnundef;23172318open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2319orreturn;2320my$name_rev= <$fd>;2321close$fd;23222323if($name_rev=~ m|^$hash tags/(.*)$|) {2324return$1;2325}else{2326# catches also '$hash undefined' output2327returnundef;2328}2329}23302331## ----------------------------------------------------------------------2332## parse to hash functions23332334sub parse_date {2335my$epoch=shift;2336my$tz=shift||"-0000";23372338my%date;2339my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2340my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2341my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2342$date{'hour'} =$hour;2343$date{'minute'} =$min;2344$date{'mday'} =$mday;2345$date{'day'} =$days[$wday];2346$date{'month'} =$months[$mon];2347$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2348$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2349$date{'mday-time'} =sprintf"%d%s%02d:%02d",2350$mday,$months[$mon],$hour,$min;2351$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",23521900+$year,1+$mon,$mday,$hour,$min,$sec;23532354$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2355my$local=$epoch+ ((int$1+ ($2/60)) *3600);2356($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2357$date{'hour_local'} =$hour;2358$date{'minute_local'} =$min;2359$date{'tz_local'} =$tz;2360$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",23611900+$year,$mon+1,$mday,2362$hour,$min,$sec,$tz);2363return%date;2364}23652366sub parse_tag {2367my$tag_id=shift;2368my%tag;2369my@comment;23702371open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2372$tag{'id'} =$tag_id;2373while(my$line= <$fd>) {2374chomp$line;2375if($line=~m/^object ([0-9a-fA-F]{40})$/) {2376$tag{'object'} =$1;2377}elsif($line=~m/^type (.+)$/) {2378$tag{'type'} =$1;2379}elsif($line=~m/^tag (.+)$/) {2380$tag{'name'} =$1;2381}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2382$tag{'author'} =$1;2383$tag{'epoch'} =$2;2384$tag{'tz'} =$3;2385}elsif($line=~m/--BEGIN/) {2386push@comment,$line;2387last;2388}elsif($lineeq"") {2389last;2390}2391}2392push@comment, <$fd>;2393$tag{'comment'} = \@comment;2394close$fdorreturn;2395if(!defined$tag{'name'}) {2396return2397};2398return%tag2399}24002401sub parse_commit_text {2402my($commit_text,$withparents) =@_;2403my@commit_lines=split'\n',$commit_text;2404my%co;24052406pop@commit_lines;# Remove '\0'24072408if(!@commit_lines) {2409return;2410}24112412my$header=shift@commit_lines;2413if($header!~m/^[0-9a-fA-F]{40}/) {2414return;2415}2416($co{'id'},my@parents) =split' ',$header;2417while(my$line=shift@commit_lines) {2418last if$lineeq"\n";2419if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2420$co{'tree'} =$1;2421}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2422push@parents,$1;2423}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2424$co{'author'} =$1;2425$co{'author_epoch'} =$2;2426$co{'author_tz'} =$3;2427if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2428$co{'author_name'} =$1;2429$co{'author_email'} =$2;2430}else{2431$co{'author_name'} =$co{'author'};2432}2433}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2434$co{'committer'} =$1;2435$co{'committer_epoch'} =$2;2436$co{'committer_tz'} =$3;2437$co{'committer_name'} =$co{'committer'};2438if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2439$co{'committer_name'} =$1;2440$co{'committer_email'} =$2;2441}else{2442$co{'committer_name'} =$co{'committer'};2443}2444}2445}2446if(!defined$co{'tree'}) {2447return;2448};2449$co{'parents'} = \@parents;2450$co{'parent'} =$parents[0];24512452foreachmy$title(@commit_lines) {2453$title=~s/^ //;2454if($titlene"") {2455$co{'title'} = chop_str($title,80,5);2456# remove leading stuff of merges to make the interesting part visible2457if(length($title) >50) {2458$title=~s/^Automatic //;2459$title=~s/^merge (of|with) /Merge ... /i;2460if(length($title) >50) {2461$title=~s/(http|rsync):\/\///;2462}2463if(length($title) >50) {2464$title=~s/(master|www|rsync)\.//;2465}2466if(length($title) >50) {2467$title=~s/kernel.org:?//;2468}2469if(length($title) >50) {2470$title=~s/\/pub\/scm//;2471}2472}2473$co{'title_short'} = chop_str($title,50,5);2474last;2475}2476}2477if(!defined$co{'title'} ||$co{'title'}eq"") {2478$co{'title'} =$co{'title_short'} ='(no commit message)';2479}2480# remove added spaces2481foreachmy$line(@commit_lines) {2482$line=~s/^ //;2483}2484$co{'comment'} = \@commit_lines;24852486my$age=time-$co{'committer_epoch'};2487$co{'age'} =$age;2488$co{'age_string'} = age_string($age);2489my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2490if($age>60*60*24*7*2) {2491$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2492$co{'age_string_age'} =$co{'age_string'};2493}else{2494$co{'age_string_date'} =$co{'age_string'};2495$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2496}2497return%co;2498}24992500sub parse_commit {2501my($commit_id) =@_;2502my%co;25032504local$/="\0";25052506open my$fd,"-|", git_cmd(),"rev-list",2507"--parents",2508"--header",2509"--max-count=1",2510$commit_id,2511"--",2512or die_error(500,"Open git-rev-list failed");2513%co= parse_commit_text(<$fd>,1);2514close$fd;25152516return%co;2517}25182519sub parse_commits {2520my($commit_id,$maxcount,$skip,$filename,@args) =@_;2521my@cos;25222523$maxcount||=1;2524$skip||=0;25252526local$/="\0";25272528open my$fd,"-|", git_cmd(),"rev-list",2529"--header",2530@args,2531("--max-count=".$maxcount),2532("--skip=".$skip),2533@extra_options,2534$commit_id,2535"--",2536($filename? ($filename) : ())2537or die_error(500,"Open git-rev-list failed");2538while(my$line= <$fd>) {2539my%co= parse_commit_text($line);2540push@cos, \%co;2541}2542close$fd;25432544returnwantarray?@cos: \@cos;2545}25462547# parse line of git-diff-tree "raw" output2548sub parse_difftree_raw_line {2549my$line=shift;2550my%res;25512552# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2553# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2554if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2555$res{'from_mode'} =$1;2556$res{'to_mode'} =$2;2557$res{'from_id'} =$3;2558$res{'to_id'} =$4;2559$res{'status'} =$5;2560$res{'similarity'} =$6;2561if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2562($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2563}else{2564$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2565}2566}2567# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2568# combined diff (for merge commit)2569elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2570$res{'nparents'} =length($1);2571$res{'from_mode'} = [split(' ',$2) ];2572$res{'to_mode'} =pop@{$res{'from_mode'}};2573$res{'from_id'} = [split(' ',$3) ];2574$res{'to_id'} =pop@{$res{'from_id'}};2575$res{'status'} = [split('',$4) ];2576$res{'to_file'} = unquote($5);2577}2578# 'c512b523472485aef4fff9e57b229d9d243c967f'2579elsif($line=~m/^([0-9a-fA-F]{40})$/) {2580$res{'commit'} =$1;2581}25822583returnwantarray?%res: \%res;2584}25852586# wrapper: return parsed line of git-diff-tree "raw" output2587# (the argument might be raw line, or parsed info)2588sub parsed_difftree_line {2589my$line_or_ref=shift;25902591if(ref($line_or_ref)eq"HASH") {2592# pre-parsed (or generated by hand)2593return$line_or_ref;2594}else{2595return parse_difftree_raw_line($line_or_ref);2596}2597}25982599# parse line of git-ls-tree output2600sub parse_ls_tree_line ($;%) {2601my$line=shift;2602my%opts=@_;2603my%res;26042605#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2606$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;26072608$res{'mode'} =$1;2609$res{'type'} =$2;2610$res{'hash'} =$3;2611if($opts{'-z'}) {2612$res{'name'} =$4;2613}else{2614$res{'name'} = unquote($4);2615}26162617returnwantarray?%res: \%res;2618}26192620# generates _two_ hashes, references to which are passed as 2 and 3 argument2621sub parse_from_to_diffinfo {2622my($diffinfo,$from,$to,@parents) =@_;26232624if($diffinfo->{'nparents'}) {2625# combined diff2626$from->{'file'} = [];2627$from->{'href'} = [];2628 fill_from_file_info($diffinfo,@parents)2629unlessexists$diffinfo->{'from_file'};2630for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2631$from->{'file'}[$i] =2632defined$diffinfo->{'from_file'}[$i] ?2633$diffinfo->{'from_file'}[$i] :2634$diffinfo->{'to_file'};2635if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2636$from->{'href'}[$i] = href(action=>"blob",2637 hash_base=>$parents[$i],2638 hash=>$diffinfo->{'from_id'}[$i],2639 file_name=>$from->{'file'}[$i]);2640}else{2641$from->{'href'}[$i] =undef;2642}2643}2644}else{2645# ordinary (not combined) diff2646$from->{'file'} =$diffinfo->{'from_file'};2647if($diffinfo->{'status'}ne"A") {# not new (added) file2648$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2649 hash=>$diffinfo->{'from_id'},2650 file_name=>$from->{'file'});2651}else{2652delete$from->{'href'};2653}2654}26552656$to->{'file'} =$diffinfo->{'to_file'};2657if(!is_deleted($diffinfo)) {# file exists in result2658$to->{'href'} = href(action=>"blob", hash_base=>$hash,2659 hash=>$diffinfo->{'to_id'},2660 file_name=>$to->{'file'});2661}else{2662delete$to->{'href'};2663}2664}26652666## ......................................................................2667## parse to array of hashes functions26682669sub git_get_heads_list {2670my$limit=shift;2671my@headslist;26722673open my$fd,'-|', git_cmd(),'for-each-ref',2674($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2675'--format=%(objectname) %(refname) %(subject)%00%(committer)',2676'refs/heads'2677orreturn;2678while(my$line= <$fd>) {2679my%ref_item;26802681chomp$line;2682my($refinfo,$committerinfo) =split(/\0/,$line);2683my($hash,$name,$title) =split(' ',$refinfo,3);2684my($committer,$epoch,$tz) =2685($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2686$ref_item{'fullname'} =$name;2687$name=~s!^refs/heads/!!;26882689$ref_item{'name'} =$name;2690$ref_item{'id'} =$hash;2691$ref_item{'title'} =$title||'(no commit message)';2692$ref_item{'epoch'} =$epoch;2693if($epoch) {2694$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2695}else{2696$ref_item{'age'} ="unknown";2697}26982699push@headslist, \%ref_item;2700}2701close$fd;27022703returnwantarray?@headslist: \@headslist;2704}27052706sub git_get_tags_list {2707my$limit=shift;2708my@tagslist;27092710open my$fd,'-|', git_cmd(),'for-each-ref',2711($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2712'--format=%(objectname) %(objecttype) %(refname) '.2713'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2714'refs/tags'2715orreturn;2716while(my$line= <$fd>) {2717my%ref_item;27182719chomp$line;2720my($refinfo,$creatorinfo) =split(/\0/,$line);2721my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2722my($creator,$epoch,$tz) =2723($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2724$ref_item{'fullname'} =$name;2725$name=~s!^refs/tags/!!;27262727$ref_item{'type'} =$type;2728$ref_item{'id'} =$id;2729$ref_item{'name'} =$name;2730if($typeeq"tag") {2731$ref_item{'subject'} =$title;2732$ref_item{'reftype'} =$reftype;2733$ref_item{'refid'} =$refid;2734}else{2735$ref_item{'reftype'} =$type;2736$ref_item{'refid'} =$id;2737}27382739if($typeeq"tag"||$typeeq"commit") {2740$ref_item{'epoch'} =$epoch;2741if($epoch) {2742$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2743}else{2744$ref_item{'age'} ="unknown";2745}2746}27472748push@tagslist, \%ref_item;2749}2750close$fd;27512752returnwantarray?@tagslist: \@tagslist;2753}27542755## ----------------------------------------------------------------------2756## filesystem-related functions27572758sub get_file_owner {2759my$path=shift;27602761my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2762my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2763if(!defined$gcos) {2764returnundef;2765}2766my$owner=$gcos;2767$owner=~s/[,;].*$//;2768return to_utf8($owner);2769}27702771# assume that file exists2772sub insert_file {2773my$filename=shift;27742775open my$fd,'<',$filename;2776print map{ to_utf8($_) } <$fd>;2777close$fd;2778}27792780## ......................................................................2781## mimetype related functions27822783sub mimetype_guess_file {2784my$filename=shift;2785my$mimemap=shift;2786-r $mimemaporreturnundef;27872788my%mimemap;2789open(MIME,$mimemap)orreturnundef;2790while(<MIME>) {2791next ifm/^#/;# skip comments2792my($mime,$exts) =split(/\t+/);2793if(defined$exts) {2794my@exts=split(/\s+/,$exts);2795foreachmy$ext(@exts) {2796$mimemap{$ext} =$mime;2797}2798}2799}2800close(MIME);28012802$filename=~/\.([^.]*)$/;2803return$mimemap{$1};2804}28052806sub mimetype_guess {2807my$filename=shift;2808my$mime;2809$filename=~/\./orreturnundef;28102811if($mimetypes_file) {2812my$file=$mimetypes_file;2813if($file!~m!^/!) {# if it is relative path2814# it is relative to project2815$file="$projectroot/$project/$file";2816}2817$mime= mimetype_guess_file($filename,$file);2818}2819$mime||= mimetype_guess_file($filename,'/etc/mime.types');2820return$mime;2821}28222823sub blob_mimetype {2824my$fd=shift;2825my$filename=shift;28262827if($filename) {2828my$mime= mimetype_guess($filename);2829$mimeandreturn$mime;2830}28312832# just in case2833return$default_blob_plain_mimetypeunless$fd;28342835if(-T $fd) {2836return'text/plain';2837}elsif(!$filename) {2838return'application/octet-stream';2839}elsif($filename=~m/\.png$/i) {2840return'image/png';2841}elsif($filename=~m/\.gif$/i) {2842return'image/gif';2843}elsif($filename=~m/\.jpe?g$/i) {2844return'image/jpeg';2845}else{2846return'application/octet-stream';2847}2848}28492850sub blob_contenttype {2851my($fd,$file_name,$type) =@_;28522853$type||= blob_mimetype($fd,$file_name);2854if($typeeq'text/plain'&&defined$default_text_plain_charset) {2855$type.="; charset=$default_text_plain_charset";2856}28572858return$type;2859}28602861## ======================================================================2862## functions printing HTML: header, footer, error page28632864sub git_header_html {2865my$status=shift||"200 OK";2866my$expires=shift;28672868my$title="$site_name";2869if(defined$project) {2870$title.=" - ". to_utf8($project);2871if(defined$action) {2872$title.="/$action";2873if(defined$file_name) {2874$title.=" - ". esc_path($file_name);2875if($actioneq"tree"&&$file_name!~ m|/$|) {2876$title.="/";2877}2878}2879}2880}2881my$content_type;2882# require explicit support from the UA if we are to send the page as2883# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2884# we have to do this because MSIE sometimes globs '*/*', pretending to2885# support xhtml+xml but choking when it gets what it asked for.2886if(defined$cgi->http('HTTP_ACCEPT') &&2887$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2888$cgi->Accept('application/xhtml+xml') !=0) {2889$content_type='application/xhtml+xml';2890}else{2891$content_type='text/html';2892}2893print$cgi->header(-type=>$content_type, -charset =>'utf-8',2894-status=>$status, -expires =>$expires);2895my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2896print<<EOF;2897<?xml version="1.0" encoding="utf-8"?>2898<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2899<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2900<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2901<!-- git core binaries version$git_version-->2902<head>2903<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2904<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2905<meta name="robots" content="index, nofollow"/>2906<title>$title</title>2907EOF2908# the stylesheet, favicon etc urls won't work correctly with path_info2909# unless we set the appropriate base URL2910if($ENV{'PATH_INFO'}) {2911print'<base href="'.esc_url($my_url).'" />\n';2912}2913# print out each stylesheet that exist, providing backwards capability2914# for those people who defined $stylesheet in a config file2915if(defined$stylesheet) {2916print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2917}else{2918foreachmy$stylesheet(@stylesheets) {2919next unless$stylesheet;2920print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2921}2922}2923if(defined$project) {2924my%href_params= get_feed_info();2925if(!exists$href_params{'-title'}) {2926$href_params{'-title'} ='log';2927}29282929foreachmy$formatqw(RSS Atom){2930my$type=lc($format);2931my%link_attr= (2932'-rel'=>'alternate',2933'-title'=>"$project-$href_params{'-title'} -$formatfeed",2934'-type'=>"application/$type+xml"2935);29362937$href_params{'action'} =$type;2938$link_attr{'-href'} = href(%href_params);2939print"<link ".2940"rel=\"$link_attr{'-rel'}\"".2941"title=\"$link_attr{'-title'}\"".2942"href=\"$link_attr{'-href'}\"".2943"type=\"$link_attr{'-type'}\"".2944"/>\n";29452946$href_params{'extra_options'} ='--no-merges';2947$link_attr{'-href'} = href(%href_params);2948$link_attr{'-title'} .=' (no merges)';2949print"<link ".2950"rel=\"$link_attr{'-rel'}\"".2951"title=\"$link_attr{'-title'}\"".2952"href=\"$link_attr{'-href'}\"".2953"type=\"$link_attr{'-type'}\"".2954"/>\n";2955}29562957}else{2958printf('<link rel="alternate" title="%sprojects list" '.2959'href="%s" type="text/plain; charset=utf-8" />'."\n",2960$site_name, href(project=>undef, action=>"project_index"));2961printf('<link rel="alternate" title="%sprojects feeds" '.2962'href="%s" type="text/x-opml" />'."\n",2963$site_name, href(project=>undef, action=>"opml"));2964}2965if(defined$favicon) {2966printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2967}29682969print"</head>\n".2970"<body>\n";29712972if(-f $site_header) {2973 insert_file($site_header);2974}29752976print"<div class=\"page_header\">\n".2977$cgi->a({-href => esc_url($logo_url),2978-title =>$logo_label},2979qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2980print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2981if(defined$project) {2982print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2983if(defined$action) {2984print" /$action";2985}2986print"\n";2987}2988print"</div>\n";29892990my$have_search= gitweb_check_feature('search');2991if(defined$project&&$have_search) {2992if(!defined$searchtext) {2993$searchtext="";2994}2995my$search_hash;2996if(defined$hash_base) {2997$search_hash=$hash_base;2998}elsif(defined$hash) {2999$search_hash=$hash;3000}else{3001$search_hash="HEAD";3002}3003my$action=$my_uri;3004my$use_pathinfo= gitweb_check_feature('pathinfo');3005if($use_pathinfo) {3006$action.="/".esc_url($project);3007}3008print$cgi->startform(-method=>"get", -action =>$action) .3009"<div class=\"search\">\n".3010(!$use_pathinfo&&3011$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3012$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3013$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3014$cgi->popup_menu(-name =>'st', -default=>'commit',3015-values=> ['commit','grep','author','committer','pickaxe']) .3016$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3017" search:\n",3018$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3019"<span title=\"Extended regular expression\">".3020$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3021-checked =>$search_use_regexp) .3022"</span>".3023"</div>".3024$cgi->end_form() ."\n";3025}3026}30273028sub git_footer_html {3029my$feed_class='rss_logo';30303031print"<div class=\"page_footer\">\n";3032if(defined$project) {3033my$descr= git_get_project_description($project);3034if(defined$descr) {3035print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3036}30373038my%href_params= get_feed_info();3039if(!%href_params) {3040$feed_class.=' generic';3041}3042$href_params{'-title'} ||='log';30433044foreachmy$formatqw(RSS Atom){3045$href_params{'action'} =lc($format);3046print$cgi->a({-href => href(%href_params),3047-title =>"$href_params{'-title'}$formatfeed",3048-class=>$feed_class},$format)."\n";3049}30503051}else{3052print$cgi->a({-href => href(project=>undef, action=>"opml"),3053-class=>$feed_class},"OPML") ." ";3054print$cgi->a({-href => href(project=>undef, action=>"project_index"),3055-class=>$feed_class},"TXT") ."\n";3056}3057print"</div>\n";# class="page_footer"30583059if(-f $site_footer) {3060 insert_file($site_footer);3061}30623063print"</body>\n".3064"</html>";3065}30663067# die_error(<http_status_code>, <error_message>)3068# Example: die_error(404, 'Hash not found')3069# By convention, use the following status codes (as defined in RFC 2616):3070# 400: Invalid or missing CGI parameters, or3071# requested object exists but has wrong type.3072# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3073# this server or project.3074# 404: Requested object/revision/project doesn't exist.3075# 500: The server isn't configured properly, or3076# an internal error occurred (e.g. failed assertions caused by bugs), or3077# an unknown error occurred (e.g. the git binary died unexpectedly).3078sub die_error {3079my$status=shift||500;3080my$error=shift||"Internal server error";30813082my%http_responses= (400=>'400 Bad Request',3083403=>'403 Forbidden',3084404=>'404 Not Found',3085500=>'500 Internal Server Error');3086 git_header_html($http_responses{$status});3087print<<EOF;3088<div class="page_body">3089<br /><br />3090$status-$error3091<br />3092</div>3093EOF3094 git_footer_html();3095exit;3096}30973098## ----------------------------------------------------------------------3099## functions printing or outputting HTML: navigation31003101sub git_print_page_nav {3102my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3103$extra=''if!defined$extra;# pager or formats31043105my@navs=qw(summary shortlog log commit commitdiff tree);3106if($suppress) {3107@navs=grep{$_ne$suppress}@navs;3108}31093110my%arg=map{$_=> {action=>$_} }@navs;3111if(defined$head) {3112for(qw(commit commitdiff)) {3113$arg{$_}{'hash'} =$head;3114}3115if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3116for(qw(shortlog log)) {3117$arg{$_}{'hash'} =$head;3118}3119}3120}31213122$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3123$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;31243125my@actions= gitweb_get_feature('actions');3126my%repl= (3127'%'=>'%',3128'n'=>$project,# project name3129'f'=>$git_dir,# project path within filesystem3130'h'=>$treehead||'',# current hash ('h' parameter)3131'b'=>$treebase||'',# hash base ('hb' parameter)3132);3133while(@actions) {3134my($label,$link,$pos) =splice(@actions,0,3);3135# insert3136@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3137# munch munch3138$link=~s/%([%nfhb])/$repl{$1}/g;3139$arg{$label}{'_href'} =$link;3140}31413142print"<div class=\"page_nav\">\n".3143(join" | ",3144map{$_eq$current?3145$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3146}@navs);3147print"<br/>\n$extra<br/>\n".3148"</div>\n";3149}31503151sub format_paging_nav {3152my($action,$hash,$head,$page,$has_next_link) =@_;3153my$paging_nav;315431553156if($hashne$head||$page) {3157$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3158}else{3159$paging_nav.="HEAD";3160}31613162if($page>0) {3163$paging_nav.=" ⋅ ".3164$cgi->a({-href => href(-replay=>1, page=>$page-1),3165-accesskey =>"p", -title =>"Alt-p"},"prev");3166}else{3167$paging_nav.=" ⋅ prev";3168}31693170if($has_next_link) {3171$paging_nav.=" ⋅ ".3172$cgi->a({-href => href(-replay=>1, page=>$page+1),3173-accesskey =>"n", -title =>"Alt-n"},"next");3174}else{3175$paging_nav.=" ⋅ next";3176}31773178return$paging_nav;3179}31803181## ......................................................................3182## functions printing or outputting HTML: div31833184sub git_print_header_div {3185my($action,$title,$hash,$hash_base) =@_;3186my%args= ();31873188$args{'action'} =$action;3189$args{'hash'} =$hashif$hash;3190$args{'hash_base'} =$hash_baseif$hash_base;31913192print"<div class=\"header\">\n".3193$cgi->a({-href => href(%args), -class=>"title"},3194$title?$title:$action) .3195"\n</div>\n";3196}31973198#sub git_print_authorship (\%) {3199sub git_print_authorship {3200my$co=shift;32013202my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3203print"<div class=\"author_date\">".3204 esc_html($co->{'author_name'}) .3205" [$ad{'rfc2822'}";3206if($ad{'hour_local'} <6) {3207printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3208$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3209}else{3210printf(" (%02d:%02d%s)",3211$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3212}3213print"]</div>\n";3214}32153216sub git_print_page_path {3217my$name=shift;3218my$type=shift;3219my$hb=shift;322032213222print"<div class=\"page_path\">";3223print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3224-title =>'tree root'}, to_utf8("[$project]"));3225print" / ";3226if(defined$name) {3227my@dirname=split'/',$name;3228my$basename=pop@dirname;3229my$fullname='';32303231foreachmy$dir(@dirname) {3232$fullname.= ($fullname?'/':'') .$dir;3233print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3234 hash_base=>$hb),3235-title =>$fullname}, esc_path($dir));3236print" / ";3237}3238if(defined$type&&$typeeq'blob') {3239print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3240 hash_base=>$hb),3241-title =>$name}, esc_path($basename));3242}elsif(defined$type&&$typeeq'tree') {3243print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3244 hash_base=>$hb),3245-title =>$name}, esc_path($basename));3246print" / ";3247}else{3248print esc_path($basename);3249}3250}3251print"<br/></div>\n";3252}32533254# sub git_print_log (\@;%) {3255sub git_print_log ($;%) {3256my$log=shift;3257my%opts=@_;32583259if($opts{'-remove_title'}) {3260# remove title, i.e. first line of log3261shift@$log;3262}3263# remove leading empty lines3264while(defined$log->[0] &&$log->[0]eq"") {3265shift@$log;3266}32673268# print log3269my$signoff=0;3270my$empty=0;3271foreachmy$line(@$log) {3272if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3273$signoff=1;3274$empty=0;3275if(!$opts{'-remove_signoff'}) {3276print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3277next;3278}else{3279# remove signoff lines3280next;3281}3282}else{3283$signoff=0;3284}32853286# print only one empty line3287# do not print empty line after signoff3288if($lineeq"") {3289next if($empty||$signoff);3290$empty=1;3291}else{3292$empty=0;3293}32943295print format_log_line_html($line) ."<br/>\n";3296}32973298if($opts{'-final_empty_line'}) {3299# end with single empty line3300print"<br/>\n"unless$empty;3301}3302}33033304# return link target (what link points to)3305sub git_get_link_target {3306my$hash=shift;3307my$link_target;33083309# read link3310open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3311orreturn;3312{3313local$/;3314$link_target= <$fd>;3315}3316close$fd3317orreturn;33183319return$link_target;3320}33213322# given link target, and the directory (basedir) the link is in,3323# return target of link relative to top directory (top tree);3324# return undef if it is not possible (including absolute links).3325sub normalize_link_target {3326my($link_target,$basedir,$hash_base) =@_;33273328# we can normalize symlink target only if $hash_base is provided3329return unless$hash_base;33303331# absolute symlinks (beginning with '/') cannot be normalized3332return if(substr($link_target,0,1)eq'/');33333334# normalize link target to path from top (root) tree (dir)3335my$path;3336if($basedir) {3337$path=$basedir.'/'.$link_target;3338}else{3339# we are in top (root) tree (dir)3340$path=$link_target;3341}33423343# remove //, /./, and /../3344my@path_parts;3345foreachmy$part(split('/',$path)) {3346# discard '.' and ''3347next if(!$part||$parteq'.');3348# handle '..'3349if($parteq'..') {3350if(@path_parts) {3351pop@path_parts;3352}else{3353# link leads outside repository (outside top dir)3354return;3355}3356}else{3357push@path_parts,$part;3358}3359}3360$path=join('/',@path_parts);33613362return$path;3363}33643365# print tree entry (row of git_tree), but without encompassing <tr> element3366sub git_print_tree_entry {3367my($t,$basedir,$hash_base,$have_blame) =@_;33683369my%base_key= ();3370$base_key{'hash_base'} =$hash_baseifdefined$hash_base;33713372# The format of a table row is: mode list link. Where mode is3373# the mode of the entry, list is the name of the entry, an href,3374# and link is the action links of the entry.33753376print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3377if($t->{'type'}eq"blob") {3378print"<td class=\"list\">".3379$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3380 file_name=>"$basedir$t->{'name'}",%base_key),3381-class=>"list"}, esc_path($t->{'name'}));3382if(S_ISLNK(oct$t->{'mode'})) {3383my$link_target= git_get_link_target($t->{'hash'});3384if($link_target) {3385my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3386if(defined$norm_target) {3387print" -> ".3388$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3389 file_name=>$norm_target),3390-title =>$norm_target}, esc_path($link_target));3391}else{3392print" -> ". esc_path($link_target);3393}3394}3395}3396print"</td>\n";3397print"<td class=\"link\">";3398print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3399 file_name=>"$basedir$t->{'name'}",%base_key)},3400"blob");3401if($have_blame) {3402print" | ".3403$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3404 file_name=>"$basedir$t->{'name'}",%base_key)},3405"blame");3406}3407if(defined$hash_base) {3408print" | ".3409$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3410 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3411"history");3412}3413print" | ".3414$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3415 file_name=>"$basedir$t->{'name'}")},3416"raw");3417print"</td>\n";34183419}elsif($t->{'type'}eq"tree") {3420print"<td class=\"list\">";3421print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3422 file_name=>"$basedir$t->{'name'}",%base_key)},3423 esc_path($t->{'name'}));3424print"</td>\n";3425print"<td class=\"link\">";3426print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3427 file_name=>"$basedir$t->{'name'}",%base_key)},3428"tree");3429if(defined$hash_base) {3430print" | ".3431$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3432 file_name=>"$basedir$t->{'name'}")},3433"history");3434}3435print"</td>\n";3436}else{3437# unknown object: we can only present history for it3438# (this includes 'commit' object, i.e. submodule support)3439print"<td class=\"list\">".3440 esc_path($t->{'name'}) .3441"</td>\n";3442print"<td class=\"link\">";3443if(defined$hash_base) {3444print$cgi->a({-href => href(action=>"history",3445 hash_base=>$hash_base,3446 file_name=>"$basedir$t->{'name'}")},3447"history");3448}3449print"</td>\n";3450}3451}34523453## ......................................................................3454## functions printing large fragments of HTML34553456# get pre-image filenames for merge (combined) diff3457sub fill_from_file_info {3458my($diff,@parents) =@_;34593460$diff->{'from_file'} = [ ];3461$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3462for(my$i=0;$i<$diff->{'nparents'};$i++) {3463if($diff->{'status'}[$i]eq'R'||3464$diff->{'status'}[$i]eq'C') {3465$diff->{'from_file'}[$i] =3466 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3467}3468}34693470return$diff;3471}34723473# is current raw difftree line of file deletion3474sub is_deleted {3475my$diffinfo=shift;34763477return$diffinfo->{'to_id'}eq('0' x 40);3478}34793480# does patch correspond to [previous] difftree raw line3481# $diffinfo - hashref of parsed raw diff format3482# $patchinfo - hashref of parsed patch diff format3483# (the same keys as in $diffinfo)3484sub is_patch_split {3485my($diffinfo,$patchinfo) =@_;34863487returndefined$diffinfo&&defined$patchinfo3488&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3489}349034913492sub git_difftree_body {3493my($difftree,$hash,@parents) =@_;3494my($parent) =$parents[0];3495my$have_blame= gitweb_check_feature('blame');3496print"<div class=\"list_head\">\n";3497if($#{$difftree} >10) {3498print(($#{$difftree} +1) ." files changed:\n");3499}3500print"</div>\n";35013502print"<table class=\"".3503(@parents>1?"combined ":"") .3504"diff_tree\">\n";35053506# header only for combined diff in 'commitdiff' view3507my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3508if($has_header) {3509# table header3510print"<thead><tr>\n".3511"<th></th><th></th>\n";# filename, patchN link3512for(my$i=0;$i<@parents;$i++) {3513my$par=$parents[$i];3514print"<th>".3515$cgi->a({-href => href(action=>"commitdiff",3516 hash=>$hash, hash_parent=>$par),3517-title =>'commitdiff to parent number '.3518($i+1) .': '.substr($par,0,7)},3519$i+1) .3520" </th>\n";3521}3522print"</tr></thead>\n<tbody>\n";3523}35243525my$alternate=1;3526my$patchno=0;3527foreachmy$line(@{$difftree}) {3528my$diff= parsed_difftree_line($line);35293530if($alternate) {3531print"<tr class=\"dark\">\n";3532}else{3533print"<tr class=\"light\">\n";3534}3535$alternate^=1;35363537if(exists$diff->{'nparents'}) {# combined diff35383539 fill_from_file_info($diff,@parents)3540unlessexists$diff->{'from_file'};35413542if(!is_deleted($diff)) {3543# file exists in the result (child) commit3544print"<td>".3545$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3546 file_name=>$diff->{'to_file'},3547 hash_base=>$hash),3548-class=>"list"}, esc_path($diff->{'to_file'})) .3549"</td>\n";3550}else{3551print"<td>".3552 esc_path($diff->{'to_file'}) .3553"</td>\n";3554}35553556if($actioneq'commitdiff') {3557# link to patch3558$patchno++;3559print"<td class=\"link\">".3560$cgi->a({-href =>"#patch$patchno"},"patch") .3561" | ".3562"</td>\n";3563}35643565my$has_history=0;3566my$not_deleted=0;3567for(my$i=0;$i<$diff->{'nparents'};$i++) {3568my$hash_parent=$parents[$i];3569my$from_hash=$diff->{'from_id'}[$i];3570my$from_path=$diff->{'from_file'}[$i];3571my$status=$diff->{'status'}[$i];35723573$has_history||= ($statusne'A');3574$not_deleted||= ($statusne'D');35753576if($statuseq'A') {3577print"<td class=\"link\"align=\"right\"> | </td>\n";3578}elsif($statuseq'D') {3579print"<td class=\"link\">".3580$cgi->a({-href => href(action=>"blob",3581 hash_base=>$hash,3582 hash=>$from_hash,3583 file_name=>$from_path)},3584"blob". ($i+1)) .3585" | </td>\n";3586}else{3587if($diff->{'to_id'}eq$from_hash) {3588print"<td class=\"link nochange\">";3589}else{3590print"<td class=\"link\">";3591}3592print$cgi->a({-href => href(action=>"blobdiff",3593 hash=>$diff->{'to_id'},3594 hash_parent=>$from_hash,3595 hash_base=>$hash,3596 hash_parent_base=>$hash_parent,3597 file_name=>$diff->{'to_file'},3598 file_parent=>$from_path)},3599"diff". ($i+1)) .3600" | </td>\n";3601}3602}36033604print"<td class=\"link\">";3605if($not_deleted) {3606print$cgi->a({-href => href(action=>"blob",3607 hash=>$diff->{'to_id'},3608 file_name=>$diff->{'to_file'},3609 hash_base=>$hash)},3610"blob");3611print" | "if($has_history);3612}3613if($has_history) {3614print$cgi->a({-href => href(action=>"history",3615 file_name=>$diff->{'to_file'},3616 hash_base=>$hash)},3617"history");3618}3619print"</td>\n";36203621print"</tr>\n";3622next;# instead of 'else' clause, to avoid extra indent3623}3624# else ordinary diff36253626my($to_mode_oct,$to_mode_str,$to_file_type);3627my($from_mode_oct,$from_mode_str,$from_file_type);3628if($diff->{'to_mode'}ne('0' x 6)) {3629$to_mode_oct=oct$diff->{'to_mode'};3630if(S_ISREG($to_mode_oct)) {# only for regular file3631$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3632}3633$to_file_type= file_type($diff->{'to_mode'});3634}3635if($diff->{'from_mode'}ne('0' x 6)) {3636$from_mode_oct=oct$diff->{'from_mode'};3637if(S_ISREG($to_mode_oct)) {# only for regular file3638$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3639}3640$from_file_type= file_type($diff->{'from_mode'});3641}36423643if($diff->{'status'}eq"A") {# created3644my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3645$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3646$mode_chng.="]</span>";3647print"<td>";3648print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3649 hash_base=>$hash, file_name=>$diff->{'file'}),3650-class=>"list"}, esc_path($diff->{'file'}));3651print"</td>\n";3652print"<td>$mode_chng</td>\n";3653print"<td class=\"link\">";3654if($actioneq'commitdiff') {3655# link to patch3656$patchno++;3657print$cgi->a({-href =>"#patch$patchno"},"patch");3658print" | ";3659}3660print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3661 hash_base=>$hash, file_name=>$diff->{'file'})},3662"blob");3663print"</td>\n";36643665}elsif($diff->{'status'}eq"D") {# deleted3666my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3667print"<td>";3668print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3669 hash_base=>$parent, file_name=>$diff->{'file'}),3670-class=>"list"}, esc_path($diff->{'file'}));3671print"</td>\n";3672print"<td>$mode_chng</td>\n";3673print"<td class=\"link\">";3674if($actioneq'commitdiff') {3675# link to patch3676$patchno++;3677print$cgi->a({-href =>"#patch$patchno"},"patch");3678print" | ";3679}3680print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3681 hash_base=>$parent, file_name=>$diff->{'file'})},3682"blob") ." | ";3683if($have_blame) {3684print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3685 file_name=>$diff->{'file'})},3686"blame") ." | ";3687}3688print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3689 file_name=>$diff->{'file'})},3690"history");3691print"</td>\n";36923693}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3694my$mode_chnge="";3695if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3696$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3697if($from_file_typene$to_file_type) {3698$mode_chnge.=" from$from_file_typeto$to_file_type";3699}3700if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3701if($from_mode_str&&$to_mode_str) {3702$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3703}elsif($to_mode_str) {3704$mode_chnge.=" mode:$to_mode_str";3705}3706}3707$mode_chnge.="]</span>\n";3708}3709print"<td>";3710print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3711 hash_base=>$hash, file_name=>$diff->{'file'}),3712-class=>"list"}, esc_path($diff->{'file'}));3713print"</td>\n";3714print"<td>$mode_chnge</td>\n";3715print"<td class=\"link\">";3716if($actioneq'commitdiff') {3717# link to patch3718$patchno++;3719print$cgi->a({-href =>"#patch$patchno"},"patch") .3720" | ";3721}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3722# "commit" view and modified file (not onlu mode changed)3723print$cgi->a({-href => href(action=>"blobdiff",3724 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3725 hash_base=>$hash, hash_parent_base=>$parent,3726 file_name=>$diff->{'file'})},3727"diff") .3728" | ";3729}3730print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3731 hash_base=>$hash, file_name=>$diff->{'file'})},3732"blob") ." | ";3733if($have_blame) {3734print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3735 file_name=>$diff->{'file'})},3736"blame") ." | ";3737}3738print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3739 file_name=>$diff->{'file'})},3740"history");3741print"</td>\n";37423743}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3744my%status_name= ('R'=>'moved','C'=>'copied');3745my$nstatus=$status_name{$diff->{'status'}};3746my$mode_chng="";3747if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3748# mode also for directories, so we cannot use $to_mode_str3749$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3750}3751print"<td>".3752$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3753 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3754-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3755"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3756$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3757 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3758-class=>"list"}, esc_path($diff->{'from_file'})) .3759" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3760"<td class=\"link\">";3761if($actioneq'commitdiff') {3762# link to patch3763$patchno++;3764print$cgi->a({-href =>"#patch$patchno"},"patch") .3765" | ";3766}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3767# "commit" view and modified file (not only pure rename or copy)3768print$cgi->a({-href => href(action=>"blobdiff",3769 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3770 hash_base=>$hash, hash_parent_base=>$parent,3771 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3772"diff") .3773" | ";3774}3775print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3776 hash_base=>$parent, file_name=>$diff->{'to_file'})},3777"blob") ." | ";3778if($have_blame) {3779print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3780 file_name=>$diff->{'to_file'})},3781"blame") ." | ";3782}3783print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3784 file_name=>$diff->{'to_file'})},3785"history");3786print"</td>\n";37873788}# we should not encounter Unmerged (U) or Unknown (X) status3789print"</tr>\n";3790}3791print"</tbody>"if$has_header;3792print"</table>\n";3793}37943795sub git_patchset_body {3796my($fd,$difftree,$hash,@hash_parents) =@_;3797my($hash_parent) =$hash_parents[0];37983799my$is_combined= (@hash_parents>1);3800my$patch_idx=0;3801my$patch_number=0;3802my$patch_line;3803my$diffinfo;3804my$to_name;3805my(%from,%to);38063807print"<div class=\"patchset\">\n";38083809# skip to first patch3810while($patch_line= <$fd>) {3811chomp$patch_line;38123813last if($patch_line=~m/^diff /);3814}38153816 PATCH:3817while($patch_line) {38183819# parse "git diff" header line3820if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3821# $1 is from_name, which we do not use3822$to_name= unquote($2);3823$to_name=~s!^b/!!;3824}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3825# $1 is 'cc' or 'combined', which we do not use3826$to_name= unquote($2);3827}else{3828$to_name=undef;3829}38303831# check if current patch belong to current raw line3832# and parse raw git-diff line if needed3833if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3834# this is continuation of a split patch3835print"<div class=\"patch cont\">\n";3836}else{3837# advance raw git-diff output if needed3838$patch_idx++ifdefined$diffinfo;38393840# read and prepare patch information3841$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38423843# compact combined diff output can have some patches skipped3844# find which patch (using pathname of result) we are at now;3845if($is_combined) {3846while($to_namene$diffinfo->{'to_file'}) {3847print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3848 format_diff_cc_simplified($diffinfo,@hash_parents) .3849"</div>\n";# class="patch"38503851$patch_idx++;3852$patch_number++;38533854last if$patch_idx>$#$difftree;3855$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3856}3857}38583859# modifies %from, %to hashes3860 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);38613862# this is first patch for raw difftree line with $patch_idx index3863# we index @$difftree array from 0, but number patches from 13864print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3865}38663867# git diff header3868#assert($patch_line =~ m/^diff /) if DEBUG;3869#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3870$patch_number++;3871# print "git diff" header3872print format_git_diff_header_line($patch_line,$diffinfo,3873 \%from, \%to);38743875# print extended diff header3876print"<div class=\"diff extended_header\">\n";3877 EXTENDED_HEADER:3878while($patch_line= <$fd>) {3879chomp$patch_line;38803881last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);38823883print format_extended_diff_header_line($patch_line,$diffinfo,3884 \%from, \%to);3885}3886print"</div>\n";# class="diff extended_header"38873888# from-file/to-file diff header3889if(!$patch_line) {3890print"</div>\n";# class="patch"3891last PATCH;3892}3893next PATCH if($patch_line=~m/^diff /);3894#assert($patch_line =~ m/^---/) if DEBUG;38953896my$last_patch_line=$patch_line;3897$patch_line= <$fd>;3898chomp$patch_line;3899#assert($patch_line =~ m/^\+\+\+/) if DEBUG;39003901print format_diff_from_to_header($last_patch_line,$patch_line,3902$diffinfo, \%from, \%to,3903@hash_parents);39043905# the patch itself3906 LINE:3907while($patch_line= <$fd>) {3908chomp$patch_line;39093910next PATCH if($patch_line=~m/^diff /);39113912print format_diff_line($patch_line, \%from, \%to);3913}39143915}continue{3916print"</div>\n";# class="patch"3917}39183919# for compact combined (--cc) format, with chunk and patch simpliciaction3920# patchset might be empty, but there might be unprocessed raw lines3921for(++$patch_idxif$patch_number>0;3922$patch_idx<@$difftree;3923++$patch_idx) {3924# read and prepare patch information3925$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);39263927# generate anchor for "patch" links in difftree / whatchanged part3928print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3929 format_diff_cc_simplified($diffinfo,@hash_parents) .3930"</div>\n";# class="patch"39313932$patch_number++;3933}39343935if($patch_number==0) {3936if(@hash_parents>1) {3937print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3938}else{3939print"<div class=\"diff nodifferences\">No differences found</div>\n";3940}3941}39423943print"</div>\n";# class="patchset"3944}39453946# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39473948# fills project list info (age, description, owner, forks) for each3949# project in the list, removing invalid projects from returned list3950# NOTE: modifies $projlist, but does not remove entries from it3951sub fill_project_list_info {3952my($projlist,$check_forks) =@_;3953my@projects;39543955my$show_ctags= gitweb_check_feature('ctags');3956 PROJECT:3957foreachmy$pr(@$projlist) {3958my(@activity) = git_get_last_activity($pr->{'path'});3959unless(@activity) {3960next PROJECT;3961}3962($pr->{'age'},$pr->{'age_string'}) =@activity;3963if(!defined$pr->{'descr'}) {3964my$descr= git_get_project_description($pr->{'path'}) ||"";3965$descr= to_utf8($descr);3966$pr->{'descr_long'} =$descr;3967$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3968}3969if(!defined$pr->{'owner'}) {3970$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3971}3972if($check_forks) {3973my$pname=$pr->{'path'};3974if(($pname=~s/\.git$//) &&3975($pname!~/\/$/) &&3976(-d "$projectroot/$pname")) {3977$pr->{'forks'} ="-d$projectroot/$pname";3978}else{3979$pr->{'forks'} =0;3980}3981}3982$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3983push@projects,$pr;3984}39853986return@projects;3987}39883989# print 'sort by' <th> element, generating 'sort by $name' replay link3990# if that order is not selected3991sub print_sort_th {3992my($name,$order,$header) =@_;3993$header||=ucfirst($name);39943995if($ordereq$name) {3996print"<th>$header</th>\n";3997}else{3998print"<th>".3999$cgi->a({-href => href(-replay=>1, order=>$name),4000-class=>"header"},$header) .4001"</th>\n";4002}4003}40044005sub git_project_list_body {4006# actually uses global variable $project4007my($projlist,$order,$from,$to,$extra,$no_header) =@_;40084009my$check_forks= gitweb_check_feature('forks');4010my@projects= fill_project_list_info($projlist,$check_forks);40114012$order||=$default_projects_order;4013$from=0unlessdefined$from;4014$to=$#projectsif(!defined$to||$#projects<$to);40154016my%order_info= (4017 project => { key =>'path', type =>'str'},4018 descr => { key =>'descr_long', type =>'str'},4019 owner => { key =>'owner', type =>'str'},4020 age => { key =>'age', type =>'num'}4021);4022my$oi=$order_info{$order};4023if($oi->{'type'}eq'str') {4024@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4025}else{4026@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4027}40284029my$show_ctags= gitweb_check_feature('ctags');4030if($show_ctags) {4031my%ctags;4032foreachmy$p(@projects) {4033foreachmy$ct(keys%{$p->{'ctags'}}) {4034$ctags{$ct} +=$p->{'ctags'}->{$ct};4035}4036}4037my$cloud= git_populate_project_tagcloud(\%ctags);4038print git_show_project_tagcloud($cloud,64);4039}40404041print"<table class=\"project_list\">\n";4042unless($no_header) {4043print"<tr>\n";4044if($check_forks) {4045print"<th></th>\n";4046}4047 print_sort_th('project',$order,'Project');4048 print_sort_th('descr',$order,'Description');4049 print_sort_th('owner',$order,'Owner');4050 print_sort_th('age',$order,'Last Change');4051print"<th></th>\n".# for links4052"</tr>\n";4053}4054my$alternate=1;4055my$tagfilter=$cgi->param('by_tag');4056for(my$i=$from;$i<=$to;$i++) {4057my$pr=$projects[$i];40584059next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4060next if$searchtextand not$pr->{'path'} =~/$searchtext/4061and not$pr->{'descr_long'} =~/$searchtext/;4062# Weed out forks or non-matching entries of search4063if($check_forks) {4064my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4065$forkbase="^$forkbase"if$forkbase;4066next ifnot$searchtextand not$tagfilterand$show_ctags4067and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4068}40694070if($alternate) {4071print"<tr class=\"dark\">\n";4072}else{4073print"<tr class=\"light\">\n";4074}4075$alternate^=1;4076if($check_forks) {4077print"<td>";4078if($pr->{'forks'}) {4079print"<!--$pr->{'forks'} -->\n";4080print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4081}4082print"</td>\n";4083}4084print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4085-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4086"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4087-class=>"list", -title =>$pr->{'descr_long'}},4088 esc_html($pr->{'descr'})) ."</td>\n".4089"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4090print"<td class=\"". age_class($pr->{'age'}) ."\">".4091(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4092"<td class=\"link\">".4093$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4094$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4095$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4096$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4097($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4098"</td>\n".4099"</tr>\n";4100}4101if(defined$extra) {4102print"<tr>\n";4103if($check_forks) {4104print"<td></td>\n";4105}4106print"<td colspan=\"5\">$extra</td>\n".4107"</tr>\n";4108}4109print"</table>\n";4110}41114112sub git_shortlog_body {4113# uses global variable $project4114my($commitlist,$from,$to,$refs,$extra) =@_;41154116$from=0unlessdefined$from;4117$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41184119print"<table class=\"shortlog\">\n";4120my$alternate=1;4121for(my$i=$from;$i<=$to;$i++) {4122my%co= %{$commitlist->[$i]};4123my$commit=$co{'id'};4124my$ref= format_ref_marker($refs,$commit);4125if($alternate) {4126print"<tr class=\"dark\">\n";4127}else{4128print"<tr class=\"light\">\n";4129}4130$alternate^=1;4131my$author= chop_and_escape_str($co{'author_name'},10);4132# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4133print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4134"<td><i>".$author."</i></td>\n".4135"<td>";4136print format_subject_html($co{'title'},$co{'title_short'},4137 href(action=>"commit", hash=>$commit),$ref);4138print"</td>\n".4139"<td class=\"link\">".4140$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4141$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4142$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4143my$snapshot_links= format_snapshot_links($commit);4144if(defined$snapshot_links) {4145print" | ".$snapshot_links;4146}4147print"</td>\n".4148"</tr>\n";4149}4150if(defined$extra) {4151print"<tr>\n".4152"<td colspan=\"4\">$extra</td>\n".4153"</tr>\n";4154}4155print"</table>\n";4156}41574158sub git_history_body {4159# Warning: assumes constant type (blob or tree) during history4160my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;41614162$from=0unlessdefined$from;4163$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});41644165print"<table class=\"history\">\n";4166my$alternate=1;4167for(my$i=$from;$i<=$to;$i++) {4168my%co= %{$commitlist->[$i]};4169if(!%co) {4170next;4171}4172my$commit=$co{'id'};41734174my$ref= format_ref_marker($refs,$commit);41754176if($alternate) {4177print"<tr class=\"dark\">\n";4178}else{4179print"<tr class=\"light\">\n";4180}4181$alternate^=1;4182# shortlog uses chop_str($co{'author_name'}, 10)4183my$author= chop_and_escape_str($co{'author_name'},15,3);4184print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4185"<td><i>".$author."</i></td>\n".4186"<td>";4187# originally git_history used chop_str($co{'title'}, 50)4188print format_subject_html($co{'title'},$co{'title_short'},4189 href(action=>"commit", hash=>$commit),$ref);4190print"</td>\n".4191"<td class=\"link\">".4192$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4193$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");41944195if($ftypeeq'blob') {4196my$blob_current= git_get_hash_by_path($hash_base,$file_name);4197my$blob_parent= git_get_hash_by_path($commit,$file_name);4198if(defined$blob_current&&defined$blob_parent&&4199$blob_currentne$blob_parent) {4200print" | ".4201$cgi->a({-href => href(action=>"blobdiff",4202 hash=>$blob_current, hash_parent=>$blob_parent,4203 hash_base=>$hash_base, hash_parent_base=>$commit,4204 file_name=>$file_name)},4205"diff to current");4206}4207}4208print"</td>\n".4209"</tr>\n";4210}4211if(defined$extra) {4212print"<tr>\n".4213"<td colspan=\"4\">$extra</td>\n".4214"</tr>\n";4215}4216print"</table>\n";4217}42184219sub git_tags_body {4220# uses global variable $project4221my($taglist,$from,$to,$extra) =@_;4222$from=0unlessdefined$from;4223$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);42244225print"<table class=\"tags\">\n";4226my$alternate=1;4227for(my$i=$from;$i<=$to;$i++) {4228my$entry=$taglist->[$i];4229my%tag=%$entry;4230my$comment=$tag{'subject'};4231my$comment_short;4232if(defined$comment) {4233$comment_short= chop_str($comment,30,5);4234}4235if($alternate) {4236print"<tr class=\"dark\">\n";4237}else{4238print"<tr class=\"light\">\n";4239}4240$alternate^=1;4241if(defined$tag{'age'}) {4242print"<td><i>$tag{'age'}</i></td>\n";4243}else{4244print"<td></td>\n";4245}4246print"<td>".4247$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4248-class=>"list name"}, esc_html($tag{'name'})) .4249"</td>\n".4250"<td>";4251if(defined$comment) {4252print format_subject_html($comment,$comment_short,4253 href(action=>"tag", hash=>$tag{'id'}));4254}4255print"</td>\n".4256"<td class=\"selflink\">";4257if($tag{'type'}eq"tag") {4258print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4259}else{4260print" ";4261}4262print"</td>\n".4263"<td class=\"link\">"." | ".4264$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4265if($tag{'reftype'}eq"commit") {4266print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4267" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4268}elsif($tag{'reftype'}eq"blob") {4269print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4270}4271print"</td>\n".4272"</tr>";4273}4274if(defined$extra) {4275print"<tr>\n".4276"<td colspan=\"5\">$extra</td>\n".4277"</tr>\n";4278}4279print"</table>\n";4280}42814282sub git_heads_body {4283# uses global variable $project4284my($headlist,$head,$from,$to,$extra) =@_;4285$from=0unlessdefined$from;4286$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);42874288print"<table class=\"heads\">\n";4289my$alternate=1;4290for(my$i=$from;$i<=$to;$i++) {4291my$entry=$headlist->[$i];4292my%ref=%$entry;4293my$curr=$ref{'id'}eq$head;4294if($alternate) {4295print"<tr class=\"dark\">\n";4296}else{4297print"<tr class=\"light\">\n";4298}4299$alternate^=1;4300print"<td><i>$ref{'age'}</i></td>\n".4301($curr?"<td class=\"current_head\">":"<td>") .4302$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4303-class=>"list name"},esc_html($ref{'name'})) .4304"</td>\n".4305"<td class=\"link\">".4306$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4307$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4308$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4309"</td>\n".4310"</tr>";4311}4312if(defined$extra) {4313print"<tr>\n".4314"<td colspan=\"3\">$extra</td>\n".4315"</tr>\n";4316}4317print"</table>\n";4318}43194320sub git_search_grep_body {4321my($commitlist,$from,$to,$extra) =@_;4322$from=0unlessdefined$from;4323$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43244325print"<table class=\"commit_search\">\n";4326my$alternate=1;4327for(my$i=$from;$i<=$to;$i++) {4328my%co= %{$commitlist->[$i]};4329if(!%co) {4330next;4331}4332my$commit=$co{'id'};4333if($alternate) {4334print"<tr class=\"dark\">\n";4335}else{4336print"<tr class=\"light\">\n";4337}4338$alternate^=1;4339my$author= chop_and_escape_str($co{'author_name'},15,5);4340print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4341"<td><i>".$author."</i></td>\n".4342"<td>".4343$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4344-class=>"list subject"},4345 chop_and_escape_str($co{'title'},50) ."<br/>");4346my$comment=$co{'comment'};4347foreachmy$line(@$comment) {4348if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4349my($lead,$match,$trail) = ($1,$2,$3);4350$match= chop_str($match,70,5,'center');4351my$contextlen=int((80-length($match))/2);4352$contextlen=30if($contextlen>30);4353$lead= chop_str($lead,$contextlen,10,'left');4354$trail= chop_str($trail,$contextlen,10,'right');43554356$lead= esc_html($lead);4357$match= esc_html($match);4358$trail= esc_html($trail);43594360print"$lead<span class=\"match\">$match</span>$trail<br />";4361}4362}4363print"</td>\n".4364"<td class=\"link\">".4365$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4366" | ".4367$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4368" | ".4369$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4370print"</td>\n".4371"</tr>\n";4372}4373if(defined$extra) {4374print"<tr>\n".4375"<td colspan=\"3\">$extra</td>\n".4376"</tr>\n";4377}4378print"</table>\n";4379}43804381## ======================================================================4382## ======================================================================4383## actions43844385sub git_project_list {4386my$order=$input_params{'order'};4387if(defined$order&&$order!~m/none|project|descr|owner|age/) {4388 die_error(400,"Unknown order parameter");4389}43904391my@list= git_get_projects_list();4392if(!@list) {4393 die_error(404,"No projects found");4394}43954396 git_header_html();4397if(-f $home_text) {4398print"<div class=\"index_include\">\n";4399 insert_file($home_text);4400print"</div>\n";4401}4402print$cgi->startform(-method=>"get") .4403"<p class=\"projsearch\">Search:\n".4404$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4405"</p>".4406$cgi->end_form() ."\n";4407 git_project_list_body(\@list,$order);4408 git_footer_html();4409}44104411sub git_forks {4412my$order=$input_params{'order'};4413if(defined$order&&$order!~m/none|project|descr|owner|age/) {4414 die_error(400,"Unknown order parameter");4415}44164417my@list= git_get_projects_list($project);4418if(!@list) {4419 die_error(404,"No forks found");4420}44214422 git_header_html();4423 git_print_page_nav('','');4424 git_print_header_div('summary',"$projectforks");4425 git_project_list_body(\@list,$order);4426 git_footer_html();4427}44284429sub git_project_index {4430my@projects= git_get_projects_list($project);44314432print$cgi->header(4433-type =>'text/plain',4434-charset =>'utf-8',4435-content_disposition =>'inline; filename="index.aux"');44364437foreachmy$pr(@projects) {4438if(!exists$pr->{'owner'}) {4439$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4440}44414442my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4443# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4444$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4445$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4446$path=~s/ /\+/g;4447$owner=~s/ /\+/g;44484449print"$path$owner\n";4450}4451}44524453sub git_summary {4454my$descr= git_get_project_description($project) ||"none";4455my%co= parse_commit("HEAD");4456my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4457my$head=$co{'id'};44584459my$owner= git_get_project_owner($project);44604461my$refs= git_get_references();4462# These get_*_list functions return one more to allow us to see if4463# there are more ...4464my@taglist= git_get_tags_list(16);4465my@headlist= git_get_heads_list(16);4466my@forklist;4467my$check_forks= gitweb_check_feature('forks');44684469if($check_forks) {4470@forklist= git_get_projects_list($project);4471}44724473 git_header_html();4474 git_print_page_nav('summary','',$head);44754476print"<div class=\"title\"> </div>\n";4477print"<table class=\"projects_list\">\n".4478"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4479"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4480if(defined$cd{'rfc2822'}) {4481print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4482}44834484# use per project git URL list in $projectroot/$project/cloneurl4485# or make project git URL from git base URL and project name4486my$url_tag="URL";4487my@url_list= git_get_project_url_list($project);4488@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4489foreachmy$git_url(@url_list) {4490next unless$git_url;4491print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4492$url_tag="";4493}44944495# Tag cloud4496my$show_ctags= gitweb_check_feature('ctags');4497if($show_ctags) {4498my$ctags= git_get_project_ctags($project);4499my$cloud= git_populate_project_tagcloud($ctags);4500print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4501print"</td>\n<td>"unless%$ctags;4502print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4503print"</td>\n<td>"if%$ctags;4504print git_show_project_tagcloud($cloud,48);4505print"</td></tr>";4506}45074508print"</table>\n";45094510# If XSS prevention is on, we don't include README.html.4511# TODO: Allow a readme in some safe format.4512if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4513print"<div class=\"title\">readme</div>\n".4514"<div class=\"readme\">\n";4515 insert_file("$projectroot/$project/README.html");4516print"\n</div>\n";# class="readme"4517}45184519# we need to request one more than 16 (0..15) to check if4520# those 16 are all4521my@commitlist=$head? parse_commits($head,17) : ();4522if(@commitlist) {4523 git_print_header_div('shortlog');4524 git_shortlog_body(\@commitlist,0,15,$refs,4525$#commitlist<=15?undef:4526$cgi->a({-href => href(action=>"shortlog")},"..."));4527}45284529if(@taglist) {4530 git_print_header_div('tags');4531 git_tags_body(\@taglist,0,15,4532$#taglist<=15?undef:4533$cgi->a({-href => href(action=>"tags")},"..."));4534}45354536if(@headlist) {4537 git_print_header_div('heads');4538 git_heads_body(\@headlist,$head,0,15,4539$#headlist<=15?undef:4540$cgi->a({-href => href(action=>"heads")},"..."));4541}45424543if(@forklist) {4544 git_print_header_div('forks');4545 git_project_list_body(\@forklist,'age',0,15,4546$#forklist<=15?undef:4547$cgi->a({-href => href(action=>"forks")},"..."),4548'no_header');4549}45504551 git_footer_html();4552}45534554sub git_tag {4555my$head= git_get_head_hash($project);4556 git_header_html();4557 git_print_page_nav('','',$head,undef,$head);4558my%tag= parse_tag($hash);45594560if(!%tag) {4561 die_error(404,"Unknown tag object");4562}45634564 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4565print"<div class=\"title_text\">\n".4566"<table class=\"object_header\">\n".4567"<tr>\n".4568"<td>object</td>\n".4569"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4570$tag{'object'}) ."</td>\n".4571"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4572$tag{'type'}) ."</td>\n".4573"</tr>\n";4574if(defined($tag{'author'})) {4575my%ad= parse_date($tag{'epoch'},$tag{'tz'});4576print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4577print"<tr><td></td><td>".$ad{'rfc2822'} .4578sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4579"</td></tr>\n";4580}4581print"</table>\n\n".4582"</div>\n";4583print"<div class=\"page_body\">";4584my$comment=$tag{'comment'};4585foreachmy$line(@$comment) {4586chomp$line;4587print esc_html($line, -nbsp=>1) ."<br/>\n";4588}4589print"</div>\n";4590 git_footer_html();4591}45924593sub git_blame {4594# permissions4595 gitweb_check_feature('blame')4596or die_error(403,"Blame view not allowed");45974598# error checking4599 die_error(400,"No file name given")unless$file_name;4600$hash_base||= git_get_head_hash($project);4601 die_error(404,"Couldn't find base commit")unless$hash_base;4602my%co= parse_commit($hash_base)4603or die_error(404,"Commit not found");4604my$ftype="blob";4605if(!defined$hash) {4606$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4607or die_error(404,"Error looking up file");4608}else{4609$ftype= git_get_type($hash);4610if($ftype!~"blob") {4611 die_error(400,"Object is not a blob");4612}4613}46144615# run git-blame --porcelain4616open my$fd,"-|", git_cmd(),"blame",'-p',4617$hash_base,'--',$file_name4618or die_error(500,"Open git-blame failed");46194620# page header4621 git_header_html();4622my$formats_nav=4623$cgi->a({-href => href(action=>"blob", -replay=>1)},4624"blob") .4625" | ".4626$cgi->a({-href => href(action=>"history", -replay=>1)},4627"history") .4628" | ".4629$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4630"HEAD");4631 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4632 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4633 git_print_page_path($file_name,$ftype,$hash_base);46344635# page body4636my@rev_color=qw(light2 dark2);4637my$num_colors=scalar(@rev_color);4638my$current_color=0;4639my%metainfo= ();46404641print<<HTML;4642<div class="page_body">4643<table class="blame">4644<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4645HTML4646 LINE:4647while(my$line= <$fd>) {4648chomp$line;4649# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4650# no <lines in group> for subsequent lines in group of lines4651my($full_rev,$orig_lineno,$lineno,$group_size) =4652($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4653if(!exists$metainfo{$full_rev}) {4654$metainfo{$full_rev} = {};4655}4656my$meta=$metainfo{$full_rev};4657my$data;4658while($data= <$fd>) {4659chomp$data;4660last if($data=~s/^\t//);# contents of line4661if($data=~/^(\S+) (.*)$/) {4662$meta->{$1} =$2;4663}4664}4665my$short_rev=substr($full_rev,0,8);4666my$author=$meta->{'author'};4667my%date=4668 parse_date($meta->{'author-time'},$meta->{'author-tz'});4669my$date=$date{'iso-tz'};4670if($group_size) {4671$current_color= ($current_color+1) %$num_colors;4672}4673print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4674if($group_size) {4675print"<td class=\"sha1\"";4676print" title=\"". esc_html($author) .",$date\"";4677print" rowspan=\"$group_size\""if($group_size>1);4678print">";4679print$cgi->a({-href => href(action=>"commit",4680 hash=>$full_rev,4681 file_name=>$file_name)},4682 esc_html($short_rev));4683print"</td>\n";4684}4685my$parent_commit;4686if(!exists$meta->{'parent'}) {4687open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4688or die_error(500,"Open git-rev-parse failed");4689$parent_commit= <$dd>;4690close$dd;4691chomp($parent_commit);4692$meta->{'parent'} =$parent_commit;4693}else{4694$parent_commit=$meta->{'parent'};4695}4696my$blamed= href(action =>'blame',4697 file_name =>$meta->{'filename'},4698 hash_base =>$parent_commit);4699print"<td class=\"linenr\">";4700print$cgi->a({ -href =>"$blamed#l$orig_lineno",4701-class=>"linenr"},4702 esc_html($lineno));4703print"</td>";4704print"<td class=\"pre\">". esc_html($data) ."</td>\n";4705print"</tr>\n";4706}4707print"</table>\n";4708print"</div>";4709close$fd4710or print"Reading blob failed\n";47114712# page footer4713 git_footer_html();4714}47154716sub git_tags {4717my$head= git_get_head_hash($project);4718 git_header_html();4719 git_print_page_nav('','',$head,undef,$head);4720 git_print_header_div('summary',$project);47214722my@tagslist= git_get_tags_list();4723if(@tagslist) {4724 git_tags_body(\@tagslist);4725}4726 git_footer_html();4727}47284729sub git_heads {4730my$head= git_get_head_hash($project);4731 git_header_html();4732 git_print_page_nav('','',$head,undef,$head);4733 git_print_header_div('summary',$project);47344735my@headslist= git_get_heads_list();4736if(@headslist) {4737 git_heads_body(\@headslist,$head);4738}4739 git_footer_html();4740}47414742sub git_blob_plain {4743my$type=shift;4744my$expires;47454746if(!defined$hash) {4747if(defined$file_name) {4748my$base=$hash_base|| git_get_head_hash($project);4749$hash= git_get_hash_by_path($base,$file_name,"blob")4750or die_error(404,"Cannot find file");4751}else{4752 die_error(400,"No file name defined");4753}4754}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4755# blobs defined by non-textual hash id's can be cached4756$expires="+1d";4757}47584759open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4760or die_error(500,"Open git-cat-file blob '$hash' failed");47614762# content-type (can include charset)4763$type= blob_contenttype($fd,$file_name,$type);47644765# "save as" filename, even when no $file_name is given4766my$save_as="$hash";4767if(defined$file_name) {4768$save_as=$file_name;4769}elsif($type=~m/^text\//) {4770$save_as.='.txt';4771}47724773# With XSS prevention on, blobs of all types except a few known safe4774# ones are served with "Content-Disposition: attachment" to make sure4775# they don't run in our security domain. For certain image types,4776# blob view writes an <img> tag referring to blob_plain view, and we4777# want to be sure not to break that by serving the image as an4778# attachment (though Firefox 3 doesn't seem to care).4779my$sandbox=$prevent_xss&&4780$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;47814782print$cgi->header(4783-type =>$type,4784-expires =>$expires,4785-content_disposition =>4786($sandbox?'attachment':'inline')4787.'; filename="'.$save_as.'"');4788undef$/;4789binmode STDOUT,':raw';4790print<$fd>;4791binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4792$/="\n";4793close$fd;4794}47954796sub git_blob {4797my$expires;47984799if(!defined$hash) {4800if(defined$file_name) {4801my$base=$hash_base|| git_get_head_hash($project);4802$hash= git_get_hash_by_path($base,$file_name,"blob")4803or die_error(404,"Cannot find file");4804}else{4805 die_error(400,"No file name defined");4806}4807}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4808# blobs defined by non-textual hash id's can be cached4809$expires="+1d";4810}48114812my$have_blame= gitweb_check_feature('blame');4813open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4814or die_error(500,"Couldn't cat$file_name,$hash");4815my$mimetype= blob_mimetype($fd,$file_name);4816if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4817close$fd;4818return git_blob_plain($mimetype);4819}4820# we can have blame only for text/* mimetype4821$have_blame&&= ($mimetype=~m!^text/!);48224823 git_header_html(undef,$expires);4824my$formats_nav='';4825if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4826if(defined$file_name) {4827if($have_blame) {4828$formats_nav.=4829$cgi->a({-href => href(action=>"blame", -replay=>1)},4830"blame") .4831" | ";4832}4833$formats_nav.=4834$cgi->a({-href => href(action=>"history", -replay=>1)},4835"history") .4836" | ".4837$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4838"raw") .4839" | ".4840$cgi->a({-href => href(action=>"blob",4841 hash_base=>"HEAD", file_name=>$file_name)},4842"HEAD");4843}else{4844$formats_nav.=4845$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4846"raw");4847}4848 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4849 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4850}else{4851print"<div class=\"page_nav\">\n".4852"<br/><br/></div>\n".4853"<div class=\"title\">$hash</div>\n";4854}4855 git_print_page_path($file_name,"blob",$hash_base);4856print"<div class=\"page_body\">\n";4857if($mimetype=~m!^image/!) {4858print qq!<img type="$mimetype"!;4859if($file_name) {4860print qq! alt="$file_name" title="$file_name"!;4861}4862print qq! src="! .4863 href(action=>"blob_plain", hash=>$hash,4864 hash_base=>$hash_base, file_name=>$file_name) .4865 qq!"/>\n!;4866}else{4867my$nr;4868while(my$line= <$fd>) {4869chomp$line;4870$nr++;4871$line= untabify($line);4872printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4873$nr,$nr,$nr, esc_html($line, -nbsp=>1);4874}4875}4876close$fd4877or print"Reading blob failed.\n";4878print"</div>";4879 git_footer_html();4880}48814882sub git_tree {4883if(!defined$hash_base) {4884$hash_base="HEAD";4885}4886if(!defined$hash) {4887if(defined$file_name) {4888$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4889}else{4890$hash=$hash_base;4891}4892}4893 die_error(404,"No such tree")unlessdefined($hash);4894$/="\0";4895open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4896or die_error(500,"Open git-ls-tree failed");4897my@entries=map{chomp;$_} <$fd>;4898close$fdor die_error(404,"Reading tree failed");4899$/="\n";49004901my$refs= git_get_references();4902my$ref= format_ref_marker($refs,$hash_base);4903 git_header_html();4904my$basedir='';4905my$have_blame= gitweb_check_feature('blame');4906if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4907my@views_nav= ();4908if(defined$file_name) {4909push@views_nav,4910$cgi->a({-href => href(action=>"history", -replay=>1)},4911"history"),4912$cgi->a({-href => href(action=>"tree",4913 hash_base=>"HEAD", file_name=>$file_name)},4914"HEAD"),4915}4916my$snapshot_links= format_snapshot_links($hash);4917if(defined$snapshot_links) {4918# FIXME: Should be available when we have no hash base as well.4919push@views_nav,$snapshot_links;4920}4921 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4922 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4923}else{4924undef$hash_base;4925print"<div class=\"page_nav\">\n";4926print"<br/><br/></div>\n";4927print"<div class=\"title\">$hash</div>\n";4928}4929if(defined$file_name) {4930$basedir=$file_name;4931if($basedirne''&&substr($basedir, -1)ne'/') {4932$basedir.='/';4933}4934 git_print_page_path($file_name,'tree',$hash_base);4935}4936print"<div class=\"page_body\">\n";4937print"<table class=\"tree\">\n";4938my$alternate=1;4939# '..' (top directory) link if possible4940if(defined$hash_base&&4941defined$file_name&&$file_name=~m![^/]+$!) {4942if($alternate) {4943print"<tr class=\"dark\">\n";4944}else{4945print"<tr class=\"light\">\n";4946}4947$alternate^=1;49484949my$up=$file_name;4950$up=~s!/?[^/]+$!!;4951undef$upunless$up;4952# based on git_print_tree_entry4953print'<td class="mode">'. mode_str('040000') ."</td>\n";4954print'<td class="list">';4955print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4956 file_name=>$up)},4957"..");4958print"</td>\n";4959print"<td class=\"link\"></td>\n";49604961print"</tr>\n";4962}4963foreachmy$line(@entries) {4964my%t= parse_ls_tree_line($line, -z =>1);49654966if($alternate) {4967print"<tr class=\"dark\">\n";4968}else{4969print"<tr class=\"light\">\n";4970}4971$alternate^=1;49724973 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);49744975print"</tr>\n";4976}4977print"</table>\n".4978"</div>";4979 git_footer_html();4980}49814982sub git_snapshot {4983my$format=$input_params{'snapshot_format'};4984if(!@snapshot_fmts) {4985 die_error(403,"Snapshots not allowed");4986}4987# default to first supported snapshot format4988$format||=$snapshot_fmts[0];4989if($format!~m/^[a-z0-9]+$/) {4990 die_error(400,"Invalid snapshot format parameter");4991}elsif(!exists($known_snapshot_formats{$format})) {4992 die_error(400,"Unknown snapshot format");4993}elsif(!grep($_eq$format,@snapshot_fmts)) {4994 die_error(403,"Unsupported snapshot format");4995}49964997if(!defined$hash) {4998$hash= git_get_head_hash($project);4999}50005001my$name=$project;5002$name=~ s,([^/])/*\.git$,$1,;5003$name= basename($name);5004my$filename= to_utf8($name);5005$name=~s/\047/\047\\\047\047/g;5006my$cmd;5007$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5008$cmd= quote_command(5009 git_cmd(),'archive',5010"--format=$known_snapshot_formats{$format}{'format'}",5011"--prefix=$name/",$hash);5012if(exists$known_snapshot_formats{$format}{'compressor'}) {5013$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5014}50155016print$cgi->header(5017-type =>$known_snapshot_formats{$format}{'type'},5018-content_disposition =>'inline; filename="'."$filename".'"',5019-status =>'200 OK');50205021open my$fd,"-|",$cmd5022or die_error(500,"Execute git-archive failed");5023binmode STDOUT,':raw';5024print<$fd>;5025binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5026close$fd;5027}50285029sub git_log {5030my$head= git_get_head_hash($project);5031if(!defined$hash) {5032$hash=$head;5033}5034if(!defined$page) {5035$page=0;5036}5037my$refs= git_get_references();50385039my@commitlist= parse_commits($hash,101, (100*$page));50405041my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);50425043my($patch_max) = gitweb_get_feature('patches');5044if($patch_max) {5045if($patch_max<0||@commitlist<=$patch_max) {5046$paging_nav.=" ⋅ ".5047$cgi->a({-href => href(action=>"patches", -replay=>1)},5048"patches");5049}5050}50515052 git_header_html();5053 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);50545055if(!@commitlist) {5056my%co= parse_commit($hash);50575058 git_print_header_div('summary',$project);5059print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5060}5061my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5062for(my$i=0;$i<=$to;$i++) {5063my%co= %{$commitlist[$i]};5064next if!%co;5065my$commit=$co{'id'};5066my$ref= format_ref_marker($refs,$commit);5067my%ad= parse_date($co{'author_epoch'});5068 git_print_header_div('commit',5069"<span class=\"age\">$co{'age_string'}</span>".5070 esc_html($co{'title'}) .$ref,5071$commit);5072print"<div class=\"title_text\">\n".5073"<div class=\"log_link\">\n".5074$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5075" | ".5076$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5077" | ".5078$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5079"<br/>\n".5080"</div>\n".5081"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".5082"</div>\n";50835084print"<div class=\"log_body\">\n";5085 git_print_log($co{'comment'}, -final_empty_line=>1);5086print"</div>\n";5087}5088if($#commitlist>=100) {5089print"<div class=\"page_nav\">\n";5090print$cgi->a({-href => href(-replay=>1, page=>$page+1),5091-accesskey =>"n", -title =>"Alt-n"},"next");5092print"</div>\n";5093}5094 git_footer_html();5095}50965097sub git_commit {5098$hash||=$hash_base||"HEAD";5099my%co= parse_commit($hash)5100or die_error(404,"Unknown commit object");5101my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5102my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});51035104my$parent=$co{'parent'};5105my$parents=$co{'parents'};# listref51065107# we need to prepare $formats_nav before any parameter munging5108my$formats_nav;5109if(!defined$parent) {5110# --root commitdiff5111$formats_nav.='(initial)';5112}elsif(@$parents==1) {5113# single parent commit5114$formats_nav.=5115'(parent: '.5116$cgi->a({-href => href(action=>"commit",5117 hash=>$parent)},5118 esc_html(substr($parent,0,7))) .5119')';5120}else{5121# merge commit5122$formats_nav.=5123'(merge: '.5124join(' ',map{5125$cgi->a({-href => href(action=>"commit",5126 hash=>$_)},5127 esc_html(substr($_,0,7)));5128}@$parents) .5129')';5130}5131if(gitweb_check_feature('patches')) {5132$formats_nav.=" | ".5133$cgi->a({-href => href(action=>"patch", -replay=>1)},5134"patch");5135}51365137if(!defined$parent) {5138$parent="--root";5139}5140my@difftree;5141open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5142@diff_opts,5143(@$parents<=1?$parent:'-c'),5144$hash,"--"5145or die_error(500,"Open git-diff-tree failed");5146@difftree=map{chomp;$_} <$fd>;5147close$fdor die_error(404,"Reading git-diff-tree failed");51485149# non-textual hash id's can be cached5150my$expires;5151if($hash=~m/^[0-9a-fA-F]{40}$/) {5152$expires="+1d";5153}5154my$refs= git_get_references();5155my$ref= format_ref_marker($refs,$co{'id'});51565157 git_header_html(undef,$expires);5158 git_print_page_nav('commit','',5159$hash,$co{'tree'},$hash,5160$formats_nav);51615162if(defined$co{'parent'}) {5163 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5164}else{5165 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5166}5167print"<div class=\"title_text\">\n".5168"<table class=\"object_header\">\n";5169print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5170"<tr>".5171"<td></td><td>$ad{'rfc2822'}";5172if($ad{'hour_local'} <6) {5173printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5174$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5175}else{5176printf(" (%02d:%02d%s)",5177$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5178}5179print"</td>".5180"</tr>\n";5181print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5182print"<tr><td></td><td>$cd{'rfc2822'}".5183sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5184"</td></tr>\n";5185print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5186print"<tr>".5187"<td>tree</td>".5188"<td class=\"sha1\">".5189$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5190class=>"list"},$co{'tree'}) .5191"</td>".5192"<td class=\"link\">".5193$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5194"tree");5195my$snapshot_links= format_snapshot_links($hash);5196if(defined$snapshot_links) {5197print" | ".$snapshot_links;5198}5199print"</td>".5200"</tr>\n";52015202foreachmy$par(@$parents) {5203print"<tr>".5204"<td>parent</td>".5205"<td class=\"sha1\">".5206$cgi->a({-href => href(action=>"commit", hash=>$par),5207class=>"list"},$par) .5208"</td>".5209"<td class=\"link\">".5210$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5211" | ".5212$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5213"</td>".5214"</tr>\n";5215}5216print"</table>".5217"</div>\n";52185219print"<div class=\"page_body\">\n";5220 git_print_log($co{'comment'});5221print"</div>\n";52225223 git_difftree_body(\@difftree,$hash,@$parents);52245225 git_footer_html();5226}52275228sub git_object {5229# object is defined by:5230# - hash or hash_base alone5231# - hash_base and file_name5232my$type;52335234# - hash or hash_base alone5235if($hash|| ($hash_base&& !defined$file_name)) {5236my$object_id=$hash||$hash_base;52375238open my$fd,"-|", quote_command(5239 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5240or die_error(404,"Object does not exist");5241$type= <$fd>;5242chomp$type;5243close$fd5244or die_error(404,"Object does not exist");52455246# - hash_base and file_name5247}elsif($hash_base&&defined$file_name) {5248$file_name=~ s,/+$,,;52495250system(git_cmd(),"cat-file",'-e',$hash_base) ==05251or die_error(404,"Base object does not exist");52525253# here errors should not hapen5254open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5255or die_error(500,"Open git-ls-tree failed");5256my$line= <$fd>;5257close$fd;52585259#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5260unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5261 die_error(404,"File or directory for given base does not exist");5262}5263$type=$2;5264$hash=$3;5265}else{5266 die_error(400,"Not enough information to find object");5267}52685269print$cgi->redirect(-uri => href(action=>$type, -full=>1,5270 hash=>$hash, hash_base=>$hash_base,5271 file_name=>$file_name),5272-status =>'302 Found');5273}52745275sub git_blobdiff {5276my$format=shift||'html';52775278my$fd;5279my@difftree;5280my%diffinfo;5281my$expires;52825283# preparing $fd and %diffinfo for git_patchset_body5284# new style URI5285if(defined$hash_base&&defined$hash_parent_base) {5286if(defined$file_name) {5287# read raw output5288open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5289$hash_parent_base,$hash_base,5290"--", (defined$file_parent?$file_parent: ()),$file_name5291or die_error(500,"Open git-diff-tree failed");5292@difftree=map{chomp;$_} <$fd>;5293close$fd5294or die_error(404,"Reading git-diff-tree failed");5295@difftree5296or die_error(404,"Blob diff not found");52975298}elsif(defined$hash&&5299$hash=~/[0-9a-fA-F]{40}/) {5300# try to find filename from $hash53015302# read filtered raw output5303open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5304$hash_parent_base,$hash_base,"--"5305or die_error(500,"Open git-diff-tree failed");5306@difftree=5307# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5308# $hash == to_id5309grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5310map{chomp;$_} <$fd>;5311close$fd5312or die_error(404,"Reading git-diff-tree failed");5313@difftree5314or die_error(404,"Blob diff not found");53155316}else{5317 die_error(400,"Missing one of the blob diff parameters");5318}53195320if(@difftree>1) {5321 die_error(400,"Ambiguous blob diff specification");5322}53235324%diffinfo= parse_difftree_raw_line($difftree[0]);5325$file_parent||=$diffinfo{'from_file'} ||$file_name;5326$file_name||=$diffinfo{'to_file'};53275328$hash_parent||=$diffinfo{'from_id'};5329$hash||=$diffinfo{'to_id'};53305331# non-textual hash id's can be cached5332if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5333$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5334$expires='+1d';5335}53365337# open patch output5338open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5339'-p', ($formateq'html'?"--full-index": ()),5340$hash_parent_base,$hash_base,5341"--", (defined$file_parent?$file_parent: ()),$file_name5342or die_error(500,"Open git-diff-tree failed");5343}53445345# old/legacy style URI -- not generated anymore since 1.4.3.5346if(!%diffinfo) {5347 die_error('404 Not Found',"Missing one of the blob diff parameters")5348}53495350# header5351if($formateq'html') {5352my$formats_nav=5353$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5354"raw");5355 git_header_html(undef,$expires);5356if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5357 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5358 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5359}else{5360print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5361print"<div class=\"title\">$hashvs$hash_parent</div>\n";5362}5363if(defined$file_name) {5364 git_print_page_path($file_name,"blob",$hash_base);5365}else{5366print"<div class=\"page_path\"></div>\n";5367}53685369}elsif($formateq'plain') {5370print$cgi->header(5371-type =>'text/plain',5372-charset =>'utf-8',5373-expires =>$expires,5374-content_disposition =>'inline; filename="'."$file_name".'.patch"');53755376print"X-Git-Url: ".$cgi->self_url() ."\n\n";53775378}else{5379 die_error(400,"Unknown blobdiff format");5380}53815382# patch5383if($formateq'html') {5384print"<div class=\"page_body\">\n";53855386 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5387close$fd;53885389print"</div>\n";# class="page_body"5390 git_footer_html();53915392}else{5393while(my$line= <$fd>) {5394$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5395$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;53965397print$line;53985399last if$line=~m!^\+\+\+!;5400}5401local$/=undef;5402print<$fd>;5403close$fd;5404}5405}54065407sub git_blobdiff_plain {5408 git_blobdiff('plain');5409}54105411sub git_commitdiff {5412my%params=@_;5413my$format=$params{-format} ||'html';54145415my($patch_max) = gitweb_get_feature('patches');5416if($formateq'patch') {5417 die_error(403,"Patch view not allowed")unless$patch_max;5418}54195420$hash||=$hash_base||"HEAD";5421my%co= parse_commit($hash)5422or die_error(404,"Unknown commit object");54235424# choose format for commitdiff for merge5425if(!defined$hash_parent&& @{$co{'parents'}} >1) {5426$hash_parent='--cc';5427}5428# we need to prepare $formats_nav before almost any parameter munging5429my$formats_nav;5430if($formateq'html') {5431$formats_nav=5432$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5433"raw");5434if($patch_max) {5435$formats_nav.=" | ".5436$cgi->a({-href => href(action=>"patch", -replay=>1)},5437"patch");5438}54395440if(defined$hash_parent&&5441$hash_parentne'-c'&&$hash_parentne'--cc') {5442# commitdiff with two commits given5443my$hash_parent_short=$hash_parent;5444if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5445$hash_parent_short=substr($hash_parent,0,7);5446}5447$formats_nav.=5448' (from';5449for(my$i=0;$i< @{$co{'parents'}};$i++) {5450if($co{'parents'}[$i]eq$hash_parent) {5451$formats_nav.=' parent '. ($i+1);5452last;5453}5454}5455$formats_nav.=': '.5456$cgi->a({-href => href(action=>"commitdiff",5457 hash=>$hash_parent)},5458 esc_html($hash_parent_short)) .5459')';5460}elsif(!$co{'parent'}) {5461# --root commitdiff5462$formats_nav.=' (initial)';5463}elsif(scalar@{$co{'parents'}} ==1) {5464# single parent commit5465$formats_nav.=5466' (parent: '.5467$cgi->a({-href => href(action=>"commitdiff",5468 hash=>$co{'parent'})},5469 esc_html(substr($co{'parent'},0,7))) .5470')';5471}else{5472# merge commit5473if($hash_parenteq'--cc') {5474$formats_nav.=' | '.5475$cgi->a({-href => href(action=>"commitdiff",5476 hash=>$hash, hash_parent=>'-c')},5477'combined');5478}else{# $hash_parent eq '-c'5479$formats_nav.=' | '.5480$cgi->a({-href => href(action=>"commitdiff",5481 hash=>$hash, hash_parent=>'--cc')},5482'compact');5483}5484$formats_nav.=5485' (merge: '.5486join(' ',map{5487$cgi->a({-href => href(action=>"commitdiff",5488 hash=>$_)},5489 esc_html(substr($_,0,7)));5490} @{$co{'parents'}} ) .5491')';5492}5493}54945495my$hash_parent_param=$hash_parent;5496if(!defined$hash_parent_param) {5497# --cc for multiple parents, --root for parentless5498$hash_parent_param=5499@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5500}55015502# read commitdiff5503my$fd;5504my@difftree;5505if($formateq'html') {5506open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5507"--no-commit-id","--patch-with-raw","--full-index",5508$hash_parent_param,$hash,"--"5509or die_error(500,"Open git-diff-tree failed");55105511while(my$line= <$fd>) {5512chomp$line;5513# empty line ends raw part of diff-tree output5514last unless$line;5515push@difftree,scalar parse_difftree_raw_line($line);5516}55175518}elsif($formateq'plain') {5519open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5520'-p',$hash_parent_param,$hash,"--"5521or die_error(500,"Open git-diff-tree failed");5522}elsif($formateq'patch') {5523# For commit ranges, we limit the output to the number of5524# patches specified in the 'patches' feature.5525# For single commits, we limit the output to a single patch,5526# diverging from the git-format-patch default.5527my@commit_spec= ();5528if($hash_parent) {5529if($patch_max>0) {5530push@commit_spec,"-$patch_max";5531}5532push@commit_spec,'-n',"$hash_parent..$hash";5533}else{5534if($params{-single}) {5535push@commit_spec,'-1';5536}else{5537if($patch_max>0) {5538push@commit_spec,"-$patch_max";5539}5540push@commit_spec,"-n";5541}5542push@commit_spec,'--root',$hash;5543}5544open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5545'--stdout',@commit_spec5546or die_error(500,"Open git-format-patch failed");5547}else{5548 die_error(400,"Unknown commitdiff format");5549}55505551# non-textual hash id's can be cached5552my$expires;5553if($hash=~m/^[0-9a-fA-F]{40}$/) {5554$expires="+1d";5555}55565557# write commit message5558if($formateq'html') {5559my$refs= git_get_references();5560my$ref= format_ref_marker($refs,$co{'id'});55615562 git_header_html(undef,$expires);5563 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5564 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5565 git_print_authorship(\%co);5566print"<div class=\"page_body\">\n";5567if(@{$co{'comment'}} >1) {5568print"<div class=\"log\">\n";5569 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5570print"</div>\n";# class="log"5571}55725573}elsif($formateq'plain') {5574my$refs= git_get_references("tags");5575my$tagname= git_get_rev_name_tags($hash);5576my$filename= basename($project) ."-$hash.patch";55775578print$cgi->header(5579-type =>'text/plain',5580-charset =>'utf-8',5581-expires =>$expires,5582-content_disposition =>'inline; filename="'."$filename".'"');5583my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5584print"From: ". to_utf8($co{'author'}) ."\n";5585print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5586print"Subject: ". to_utf8($co{'title'}) ."\n";55875588print"X-Git-Tag:$tagname\n"if$tagname;5589print"X-Git-Url: ".$cgi->self_url() ."\n\n";55905591foreachmy$line(@{$co{'comment'}}) {5592print to_utf8($line) ."\n";5593}5594print"---\n\n";5595}elsif($formateq'patch') {5596my$filename= basename($project) ."-$hash.patch";55975598print$cgi->header(5599-type =>'text/plain',5600-charset =>'utf-8',5601-expires =>$expires,5602-content_disposition =>'inline; filename="'."$filename".'"');5603}56045605# write patch5606if($formateq'html') {5607my$use_parents= !defined$hash_parent||5608$hash_parenteq'-c'||$hash_parenteq'--cc';5609 git_difftree_body(\@difftree,$hash,5610$use_parents? @{$co{'parents'}} :$hash_parent);5611print"<br/>\n";56125613 git_patchset_body($fd, \@difftree,$hash,5614$use_parents? @{$co{'parents'}} :$hash_parent);5615close$fd;5616print"</div>\n";# class="page_body"5617 git_footer_html();56185619}elsif($formateq'plain') {5620local$/=undef;5621print<$fd>;5622close$fd5623or print"Reading git-diff-tree failed\n";5624}elsif($formateq'patch') {5625local$/=undef;5626print<$fd>;5627close$fd5628or print"Reading git-format-patch failed\n";5629}5630}56315632sub git_commitdiff_plain {5633 git_commitdiff(-format =>'plain');5634}56355636# format-patch-style patches5637sub git_patch {5638 git_commitdiff(-format =>'patch', -single=>1);5639}56405641sub git_patches {5642 git_commitdiff(-format =>'patch');5643}56445645sub git_history {5646if(!defined$hash_base) {5647$hash_base= git_get_head_hash($project);5648}5649if(!defined$page) {5650$page=0;5651}5652my$ftype;5653my%co= parse_commit($hash_base)5654or die_error(404,"Unknown commit object");56555656my$refs= git_get_references();5657my$limit=sprintf("--max-count=%i", (100* ($page+1)));56585659my@commitlist= parse_commits($hash_base,101, (100*$page),5660$file_name,"--full-history")5661or die_error(404,"No such file or directory on given branch");56625663if(!defined$hash&&defined$file_name) {5664# some commits could have deleted file in question,5665# and not have it in tree, but one of them has to have it5666for(my$i=0;$i<=@commitlist;$i++) {5667$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5668last ifdefined$hash;5669}5670}5671if(defined$hash) {5672$ftype= git_get_type($hash);5673}5674if(!defined$ftype) {5675 die_error(500,"Unknown type of object");5676}56775678my$paging_nav='';5679if($page>0) {5680$paging_nav.=5681$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5682 file_name=>$file_name)},5683"first");5684$paging_nav.=" ⋅ ".5685$cgi->a({-href => href(-replay=>1, page=>$page-1),5686-accesskey =>"p", -title =>"Alt-p"},"prev");5687}else{5688$paging_nav.="first";5689$paging_nav.=" ⋅ prev";5690}5691my$next_link='';5692if($#commitlist>=100) {5693$next_link=5694$cgi->a({-href => href(-replay=>1, page=>$page+1),5695-accesskey =>"n", -title =>"Alt-n"},"next");5696$paging_nav.=" ⋅$next_link";5697}else{5698$paging_nav.=" ⋅ next";5699}57005701 git_header_html();5702 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5703 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5704 git_print_page_path($file_name,$ftype,$hash_base);57055706 git_history_body(\@commitlist,0,99,5707$refs,$hash_base,$ftype,$next_link);57085709 git_footer_html();5710}57115712sub git_search {5713 gitweb_check_feature('search')or die_error(403,"Search is disabled");5714if(!defined$searchtext) {5715 die_error(400,"Text field is empty");5716}5717if(!defined$hash) {5718$hash= git_get_head_hash($project);5719}5720my%co= parse_commit($hash);5721if(!%co) {5722 die_error(404,"Unknown commit object");5723}5724if(!defined$page) {5725$page=0;5726}57275728$searchtype||='commit';5729if($searchtypeeq'pickaxe') {5730# pickaxe may take all resources of your box and run for several minutes5731# with every query - so decide by yourself how public you make this feature5732 gitweb_check_feature('pickaxe')5733or die_error(403,"Pickaxe is disabled");5734}5735if($searchtypeeq'grep') {5736 gitweb_check_feature('grep')5737or die_error(403,"Grep is disabled");5738}57395740 git_header_html();57415742if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5743my$greptype;5744if($searchtypeeq'commit') {5745$greptype="--grep=";5746}elsif($searchtypeeq'author') {5747$greptype="--author=";5748}elsif($searchtypeeq'committer') {5749$greptype="--committer=";5750}5751$greptype.=$searchtext;5752my@commitlist= parse_commits($hash,101, (100*$page),undef,5753$greptype,'--regexp-ignore-case',5754$search_use_regexp?'--extended-regexp':'--fixed-strings');57555756my$paging_nav='';5757if($page>0) {5758$paging_nav.=5759$cgi->a({-href => href(action=>"search", hash=>$hash,5760 searchtext=>$searchtext,5761 searchtype=>$searchtype)},5762"first");5763$paging_nav.=" ⋅ ".5764$cgi->a({-href => href(-replay=>1, page=>$page-1),5765-accesskey =>"p", -title =>"Alt-p"},"prev");5766}else{5767$paging_nav.="first";5768$paging_nav.=" ⋅ prev";5769}5770my$next_link='';5771if($#commitlist>=100) {5772$next_link=5773$cgi->a({-href => href(-replay=>1, page=>$page+1),5774-accesskey =>"n", -title =>"Alt-n"},"next");5775$paging_nav.=" ⋅$next_link";5776}else{5777$paging_nav.=" ⋅ next";5778}57795780if($#commitlist>=100) {5781}57825783 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5784 git_print_header_div('commit', esc_html($co{'title'}),$hash);5785 git_search_grep_body(\@commitlist,0,99,$next_link);5786}57875788if($searchtypeeq'pickaxe') {5789 git_print_page_nav('','',$hash,$co{'tree'},$hash);5790 git_print_header_div('commit', esc_html($co{'title'}),$hash);57915792print"<table class=\"pickaxe search\">\n";5793my$alternate=1;5794$/="\n";5795open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5796'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5797($search_use_regexp?'--pickaxe-regex': ());5798undef%co;5799my@files;5800while(my$line= <$fd>) {5801chomp$line;5802next unless$line;58035804my%set= parse_difftree_raw_line($line);5805if(defined$set{'commit'}) {5806# finish previous commit5807if(%co) {5808print"</td>\n".5809"<td class=\"link\">".5810$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5811" | ".5812$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5813print"</td>\n".5814"</tr>\n";5815}58165817if($alternate) {5818print"<tr class=\"dark\">\n";5819}else{5820print"<tr class=\"light\">\n";5821}5822$alternate^=1;5823%co= parse_commit($set{'commit'});5824my$author= chop_and_escape_str($co{'author_name'},15,5);5825print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5826"<td><i>$author</i></td>\n".5827"<td>".5828$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5829-class=>"list subject"},5830 chop_and_escape_str($co{'title'},50) ."<br/>");5831}elsif(defined$set{'to_id'}) {5832next if($set{'to_id'} =~m/^0{40}$/);58335834print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5835 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5836-class=>"list"},5837"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5838"<br/>\n";5839}5840}5841close$fd;58425843# finish last commit (warning: repetition!)5844if(%co) {5845print"</td>\n".5846"<td class=\"link\">".5847$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5848" | ".5849$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5850print"</td>\n".5851"</tr>\n";5852}58535854print"</table>\n";5855}58565857if($searchtypeeq'grep') {5858 git_print_page_nav('','',$hash,$co{'tree'},$hash);5859 git_print_header_div('commit', esc_html($co{'title'}),$hash);58605861print"<table class=\"grep_search\">\n";5862my$alternate=1;5863my$matches=0;5864$/="\n";5865open my$fd,"-|", git_cmd(),'grep','-n',5866$search_use_regexp? ('-E','-i') :'-F',5867$searchtext,$co{'tree'};5868my$lastfile='';5869while(my$line= <$fd>) {5870chomp$line;5871my($file,$lno,$ltext,$binary);5872last if($matches++>1000);5873if($line=~/^Binary file (.+) matches$/) {5874$file=$1;5875$binary=1;5876}else{5877(undef,$file,$lno,$ltext) =split(/:/,$line,4);5878}5879if($filene$lastfile) {5880$lastfileand print"</td></tr>\n";5881if($alternate++) {5882print"<tr class=\"dark\">\n";5883}else{5884print"<tr class=\"light\">\n";5885}5886print"<td class=\"list\">".5887$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5888 file_name=>"$file"),5889-class=>"list"}, esc_path($file));5890print"</td><td>\n";5891$lastfile=$file;5892}5893if($binary) {5894print"<div class=\"binary\">Binary file</div>\n";5895}else{5896$ltext= untabify($ltext);5897if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5898$ltext= esc_html($1, -nbsp=>1);5899$ltext.='<span class="match">';5900$ltext.= esc_html($2, -nbsp=>1);5901$ltext.='</span>';5902$ltext.= esc_html($3, -nbsp=>1);5903}else{5904$ltext= esc_html($ltext, -nbsp=>1);5905}5906print"<div class=\"pre\">".5907$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5908 file_name=>"$file").'#l'.$lno,5909-class=>"linenr"},sprintf('%4i',$lno))5910.' '.$ltext."</div>\n";5911}5912}5913if($lastfile) {5914print"</td></tr>\n";5915if($matches>1000) {5916print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5917}5918}else{5919print"<div class=\"diff nodifferences\">No matches found</div>\n";5920}5921close$fd;59225923print"</table>\n";5924}5925 git_footer_html();5926}59275928sub git_search_help {5929 git_header_html();5930 git_print_page_nav('','',$hash,$hash,$hash);5931print<<EOT;5932<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5933regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5934the pattern entered is recognized as the POSIX extended5935<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5936insensitive).</p>5937<dl>5938<dt><b>commit</b></dt>5939<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5940EOT5941my$have_grep= gitweb_check_feature('grep');5942if($have_grep) {5943print<<EOT;5944<dt><b>grep</b></dt>5945<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5946 a different one) are searched for the given pattern. On large trees, this search can take5947a while and put some strain on the server, so please use it with some consideration. Note that5948due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5949case-sensitive.</dd>5950EOT5951}5952print<<EOT;5953<dt><b>author</b></dt>5954<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5955<dt><b>committer</b></dt>5956<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5957EOT5958my$have_pickaxe= gitweb_check_feature('pickaxe');5959if($have_pickaxe) {5960print<<EOT;5961<dt><b>pickaxe</b></dt>5962<dd>All commits that caused the string to appear or disappear from any file (changes that5963added, removed or "modified" the string) will be listed. This search can take a while and5964takes a lot of strain on the server, so please use it wisely. Note that since you may be5965interested even in changes just changing the case as well, this search is case sensitive.</dd>5966EOT5967}5968print"</dl>\n";5969 git_footer_html();5970}59715972sub git_shortlog {5973my$head= git_get_head_hash($project);5974if(!defined$hash) {5975$hash=$head;5976}5977if(!defined$page) {5978$page=0;5979}5980my$refs= git_get_references();59815982my$commit_hash=$hash;5983if(defined$hash_parent) {5984$commit_hash="$hash_parent..$hash";5985}5986my@commitlist= parse_commits($commit_hash,101, (100*$page));59875988my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5989my$next_link='';5990if($#commitlist>=100) {5991$next_link=5992$cgi->a({-href => href(-replay=>1, page=>$page+1),5993-accesskey =>"n", -title =>"Alt-n"},"next");5994}5995my$patch_max= gitweb_check_feature('patches');5996if($patch_max) {5997if($patch_max<0||@commitlist<=$patch_max) {5998$paging_nav.=" ⋅ ".5999$cgi->a({-href => href(action=>"patches", -replay=>1)},6000"patches");6001}6002}60036004 git_header_html();6005 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6006 git_print_header_div('summary',$project);60076008 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);60096010 git_footer_html();6011}60126013## ......................................................................6014## feeds (RSS, Atom; OPML)60156016sub git_feed {6017my$format=shift||'atom';6018my$have_blame= gitweb_check_feature('blame');60196020# Atom: http://www.atomenabled.org/developers/syndication/6021# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6022if($formatne'rss'&&$formatne'atom') {6023 die_error(400,"Unknown web feed format");6024}60256026# log/feed of current (HEAD) branch, log of given branch, history of file/directory6027my$head=$hash||'HEAD';6028my@commitlist= parse_commits($head,150,0,$file_name);60296030my%latest_commit;6031my%latest_date;6032my$content_type="application/$format+xml";6033if(defined$cgi->http('HTTP_ACCEPT') &&6034$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6035# browser (feed reader) prefers text/xml6036$content_type='text/xml';6037}6038if(defined($commitlist[0])) {6039%latest_commit= %{$commitlist[0]};6040my$latest_epoch=$latest_commit{'committer_epoch'};6041%latest_date= parse_date($latest_epoch);6042my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6043if(defined$if_modified) {6044my$since;6045if(eval{require HTTP::Date;1; }) {6046$since= HTTP::Date::str2time($if_modified);6047}elsif(eval{require Time::ParseDate;1; }) {6048$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6049}6050if(defined$since&&$latest_epoch<=$since) {6051print$cgi->header(6052-type =>$content_type,6053-charset =>'utf-8',6054-last_modified =>$latest_date{'rfc2822'},6055-status =>'304 Not Modified');6056return;6057}6058}6059print$cgi->header(6060-type =>$content_type,6061-charset =>'utf-8',6062-last_modified =>$latest_date{'rfc2822'});6063}else{6064print$cgi->header(6065-type =>$content_type,6066-charset =>'utf-8');6067}60686069# Optimization: skip generating the body if client asks only6070# for Last-Modified date.6071return if($cgi->request_method()eq'HEAD');60726073# header variables6074my$title="$site_name-$project/$action";6075my$feed_type='log';6076if(defined$hash) {6077$title.=" - '$hash'";6078$feed_type='branch log';6079if(defined$file_name) {6080$title.=" ::$file_name";6081$feed_type='history';6082}6083}elsif(defined$file_name) {6084$title.=" -$file_name";6085$feed_type='history';6086}6087$title.="$feed_type";6088my$descr= git_get_project_description($project);6089if(defined$descr) {6090$descr= esc_html($descr);6091}else{6092$descr="$project".6093($formateq'rss'?'RSS':'Atom') .6094" feed";6095}6096my$owner= git_get_project_owner($project);6097$owner= esc_html($owner);60986099#header6100my$alt_url;6101if(defined$file_name) {6102$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6103}elsif(defined$hash) {6104$alt_url= href(-full=>1, action=>"log", hash=>$hash);6105}else{6106$alt_url= href(-full=>1, action=>"summary");6107}6108print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6109if($formateq'rss') {6110print<<XML;6111<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6112<channel>6113XML6114print"<title>$title</title>\n".6115"<link>$alt_url</link>\n".6116"<description>$descr</description>\n".6117"<language>en</language>\n".6118# project owner is responsible for 'editorial' content6119"<managingEditor>$owner</managingEditor>\n";6120if(defined$logo||defined$favicon) {6121# prefer the logo to the favicon, since RSS6122# doesn't allow both6123my$img= esc_url($logo||$favicon);6124print"<image>\n".6125"<url>$img</url>\n".6126"<title>$title</title>\n".6127"<link>$alt_url</link>\n".6128"</image>\n";6129}6130if(%latest_date) {6131print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6132print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6133}6134print"<generator>gitweb v.$version/$git_version</generator>\n";6135}elsif($formateq'atom') {6136print<<XML;6137<feed xmlns="http://www.w3.org/2005/Atom">6138XML6139print"<title>$title</title>\n".6140"<subtitle>$descr</subtitle>\n".6141'<link rel="alternate" type="text/html" href="'.6142$alt_url.'" />'."\n".6143'<link rel="self" type="'.$content_type.'" href="'.6144$cgi->self_url() .'" />'."\n".6145"<id>". href(-full=>1) ."</id>\n".6146# use project owner for feed author6147"<author><name>$owner</name></author>\n";6148if(defined$favicon) {6149print"<icon>". esc_url($favicon) ."</icon>\n";6150}6151if(defined$logo_url) {6152# not twice as wide as tall: 72 x 27 pixels6153print"<logo>". esc_url($logo) ."</logo>\n";6154}6155if(!%latest_date) {6156# dummy date to keep the feed valid until commits trickle in:6157print"<updated>1970-01-01T00:00:00Z</updated>\n";6158}else{6159print"<updated>$latest_date{'iso-8601'}</updated>\n";6160}6161print"<generator version='$version/$git_version'>gitweb</generator>\n";6162}61636164# contents6165for(my$i=0;$i<=$#commitlist;$i++) {6166my%co= %{$commitlist[$i]};6167my$commit=$co{'id'};6168# we read 150, we always show 30 and the ones more recent than 48 hours6169if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6170last;6171}6172my%cd= parse_date($co{'author_epoch'});61736174# get list of changed files6175open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6176$co{'parent'} ||"--root",6177$co{'id'},"--", (defined$file_name?$file_name: ())6178ornext;6179my@difftree=map{chomp;$_} <$fd>;6180close$fd6181ornext;61826183# print element (entry, item)6184my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6185if($formateq'rss') {6186print"<item>\n".6187"<title>". esc_html($co{'title'}) ."</title>\n".6188"<author>". esc_html($co{'author'}) ."</author>\n".6189"<pubDate>$cd{'rfc2822'}</pubDate>\n".6190"<guid isPermaLink=\"true\">$co_url</guid>\n".6191"<link>$co_url</link>\n".6192"<description>". esc_html($co{'title'}) ."</description>\n".6193"<content:encoded>".6194"<![CDATA[\n";6195}elsif($formateq'atom') {6196print"<entry>\n".6197"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6198"<updated>$cd{'iso-8601'}</updated>\n".6199"<author>\n".6200" <name>". esc_html($co{'author_name'}) ."</name>\n";6201if($co{'author_email'}) {6202print" <email>". esc_html($co{'author_email'}) ."</email>\n";6203}6204print"</author>\n".6205# use committer for contributor6206"<contributor>\n".6207" <name>". esc_html($co{'committer_name'}) ."</name>\n";6208if($co{'committer_email'}) {6209print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6210}6211print"</contributor>\n".6212"<published>$cd{'iso-8601'}</published>\n".6213"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6214"<id>$co_url</id>\n".6215"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6216"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6217}6218my$comment=$co{'comment'};6219print"<pre>\n";6220foreachmy$line(@$comment) {6221$line= esc_html($line);6222print"$line\n";6223}6224print"</pre><ul>\n";6225foreachmy$difftree_line(@difftree) {6226my%difftree= parse_difftree_raw_line($difftree_line);6227next if!$difftree{'from_id'};62286229my$file=$difftree{'file'} ||$difftree{'to_file'};62306231print"<li>".6232"[".6233$cgi->a({-href => href(-full=>1, action=>"blobdiff",6234 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6235 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6236 file_name=>$file, file_parent=>$difftree{'from_file'}),6237-title =>"diff"},'D');6238if($have_blame) {6239print$cgi->a({-href => href(-full=>1, action=>"blame",6240 file_name=>$file, hash_base=>$commit),6241-title =>"blame"},'B');6242}6243# if this is not a feed of a file history6244if(!defined$file_name||$file_namene$file) {6245print$cgi->a({-href => href(-full=>1, action=>"history",6246 file_name=>$file, hash=>$commit),6247-title =>"history"},'H');6248}6249$file= esc_path($file);6250print"] ".6251"$file</li>\n";6252}6253if($formateq'rss') {6254print"</ul>]]>\n".6255"</content:encoded>\n".6256"</item>\n";6257}elsif($formateq'atom') {6258print"</ul>\n</div>\n".6259"</content>\n".6260"</entry>\n";6261}6262}62636264# end of feed6265if($formateq'rss') {6266print"</channel>\n</rss>\n";6267}elsif($formateq'atom') {6268print"</feed>\n";6269}6270}62716272sub git_rss {6273 git_feed('rss');6274}62756276sub git_atom {6277 git_feed('atom');6278}62796280sub git_opml {6281my@list= git_get_projects_list();62826283print$cgi->header(6284-type =>'text/xml',6285-charset =>'utf-8',6286-content_disposition =>'inline; filename="opml.xml"');62876288print<<XML;6289<?xml version="1.0" encoding="utf-8"?>6290<opml version="1.0">6291<head>6292 <title>$site_nameOPML Export</title>6293</head>6294<body>6295<outline text="git RSS feeds">6296XML62976298foreachmy$pr(@list) {6299my%proj=%$pr;6300my$head= git_get_head_hash($proj{'path'});6301if(!defined$head) {6302next;6303}6304$git_dir="$projectroot/$proj{'path'}";6305my%co= parse_commit($head);6306if(!%co) {6307next;6308}63096310my$path= esc_html(chop_str($proj{'path'},25,5));6311my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6312my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6313print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6314}6315print<<XML;6316</outline>6317</body>6318</opml>6319XML6320}