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# information about snapshot formats that gitweb is capable of serving 136our%known_snapshot_formats= ( 137# name => { 138# 'display' => display name, 139# 'type' => mime type, 140# 'suffix' => filename suffix, 141# 'format' => --format for git-archive, 142# 'compressor' => [compressor command and arguments] 143# (array reference, optional)} 144# 145'tgz'=> { 146'display'=>'tar.gz', 147'type'=>'application/x-gzip', 148'suffix'=>'.tar.gz', 149'format'=>'tar', 150'compressor'=> ['gzip']}, 151 152'tbz2'=> { 153'display'=>'tar.bz2', 154'type'=>'application/x-bzip2', 155'suffix'=>'.tar.bz2', 156'format'=>'tar', 157'compressor'=> ['bzip2']}, 158 159'zip'=> { 160'display'=>'zip', 161'type'=>'application/x-zip', 162'suffix'=>'.zip', 163'format'=>'zip'}, 164); 165 166# Aliases so we understand old gitweb.snapshot values in repository 167# configuration. 168our%known_snapshot_format_aliases= ( 169'gzip'=>'tgz', 170'bzip2'=>'tbz2', 171 172# backward compatibility: legacy gitweb config support 173'x-gzip'=>undef,'gz'=>undef, 174'x-bzip2'=>undef,'bz2'=>undef, 175'x-zip'=>undef,''=>undef, 176); 177 178# You define site-wide feature defaults here; override them with 179# $GITWEB_CONFIG as necessary. 180our%feature= ( 181# feature => { 182# 'sub' => feature-sub (subroutine), 183# 'override' => allow-override (boolean), 184# 'default' => [ default options...] (array reference)} 185# 186# if feature is overridable (it means that allow-override has true value), 187# then feature-sub will be called with default options as parameters; 188# return value of feature-sub indicates if to enable specified feature 189# 190# if there is no 'sub' key (no feature-sub), then feature cannot be 191# overriden 192# 193# use gitweb_get_feature(<feature>) to retrieve the <feature> value 194# (an array) or gitweb_check_feature(<feature>) to check if <feature> 195# is enabled 196 197# Enable the 'blame' blob view, showing the last commit that modified 198# each line in the file. This can be very CPU-intensive. 199 200# To enable system wide have in $GITWEB_CONFIG 201# $feature{'blame'}{'default'} = [1]; 202# To have project specific config enable override in $GITWEB_CONFIG 203# $feature{'blame'}{'override'} = 1; 204# and in project config gitweb.blame = 0|1; 205'blame'=> { 206'sub'=>sub{ feature_bool('blame',@_) }, 207'override'=>0, 208'default'=> [0]}, 209 210# Enable the 'snapshot' link, providing a compressed archive of any 211# tree. This can potentially generate high traffic if you have large 212# project. 213 214# Value is a list of formats defined in %known_snapshot_formats that 215# you wish to offer. 216# To disable system wide have in $GITWEB_CONFIG 217# $feature{'snapshot'}{'default'} = []; 218# To have project specific config enable override in $GITWEB_CONFIG 219# $feature{'snapshot'}{'override'} = 1; 220# and in project config, a comma-separated list of formats or "none" 221# to disable. Example: gitweb.snapshot = tbz2,zip; 222'snapshot'=> { 223'sub'=> \&feature_snapshot, 224'override'=>0, 225'default'=> ['tgz']}, 226 227# Enable text search, which will list the commits which match author, 228# committer or commit text to a given string. Enabled by default. 229# Project specific override is not supported. 230'search'=> { 231'override'=>0, 232'default'=> [1]}, 233 234# Enable grep search, which will list the files in currently selected 235# tree containing the given string. Enabled by default. This can be 236# potentially CPU-intensive, of course. 237 238# To enable system wide have in $GITWEB_CONFIG 239# $feature{'grep'}{'default'} = [1]; 240# To have project specific config enable override in $GITWEB_CONFIG 241# $feature{'grep'}{'override'} = 1; 242# and in project config gitweb.grep = 0|1; 243'grep'=> { 244'sub'=>sub{ feature_bool('grep',@_) }, 245'override'=>0, 246'default'=> [1]}, 247 248# Enable the pickaxe search, which will list the commits that modified 249# a given string in a file. This can be practical and quite faster 250# alternative to 'blame', but still potentially CPU-intensive. 251 252# To enable system wide have in $GITWEB_CONFIG 253# $feature{'pickaxe'}{'default'} = [1]; 254# To have project specific config enable override in $GITWEB_CONFIG 255# $feature{'pickaxe'}{'override'} = 1; 256# and in project config gitweb.pickaxe = 0|1; 257'pickaxe'=> { 258'sub'=>sub{ feature_bool('pickaxe',@_) }, 259'override'=>0, 260'default'=> [1]}, 261 262# Make gitweb use an alternative format of the URLs which can be 263# more readable and natural-looking: project name is embedded 264# directly in the path and the query string contains other 265# auxiliary information. All gitweb installations recognize 266# URL in either format; this configures in which formats gitweb 267# generates links. 268 269# To enable system wide have in $GITWEB_CONFIG 270# $feature{'pathinfo'}{'default'} = [1]; 271# Project specific override is not supported. 272 273# Note that you will need to change the default location of CSS, 274# favicon, logo and possibly other files to an absolute URL. Also, 275# if gitweb.cgi serves as your indexfile, you will need to force 276# $my_uri to contain the script name in your $GITWEB_CONFIG. 277'pathinfo'=> { 278'override'=>0, 279'default'=> [0]}, 280 281# Make gitweb consider projects in project root subdirectories 282# to be forks of existing projects. Given project $projname.git, 283# projects matching $projname/*.git will not be shown in the main 284# projects list, instead a '+' mark will be added to $projname 285# there and a 'forks' view will be enabled for the project, listing 286# all the forks. If project list is taken from a file, forks have 287# to be listed after the main project. 288 289# To enable system wide have in $GITWEB_CONFIG 290# $feature{'forks'}{'default'} = [1]; 291# Project specific override is not supported. 292'forks'=> { 293'override'=>0, 294'default'=> [0]}, 295 296# Insert custom links to the action bar of all project pages. 297# This enables you mainly to link to third-party scripts integrating 298# into gitweb; e.g. git-browser for graphical history representation 299# or custom web-based repository administration interface. 300 301# The 'default' value consists of a list of triplets in the form 302# (label, link, position) where position is the label after which 303# to insert the link and link is a format string where %n expands 304# to the project name, %f to the project path within the filesystem, 305# %h to the current hash (h gitweb parameter) and %b to the current 306# hash base (hb gitweb parameter); %% expands to %. 307 308# To enable system wide have in $GITWEB_CONFIG e.g. 309# $feature{'actions'}{'default'} = [('graphiclog', 310# '/git-browser/by-commit.html?r=%n', 'summary')]; 311# Project specific override is not supported. 312'actions'=> { 313'override'=>0, 314'default'=> []}, 315 316# Allow gitweb scan project content tags described in ctags/ 317# of project repository, and display the popular Web 2.0-ish 318# "tag cloud" near the project list. Note that this is something 319# COMPLETELY different from the normal Git tags. 320 321# gitweb by itself can show existing tags, but it does not handle 322# tagging itself; you need an external application for that. 323# For an example script, check Girocco's cgi/tagproj.cgi. 324# You may want to install the HTML::TagCloud Perl module to get 325# a pretty tag cloud instead of just a list of tags. 326 327# To enable system wide have in $GITWEB_CONFIG 328# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 329# Project specific override is not supported. 330'ctags'=> { 331'override'=>0, 332'default'=> [0]}, 333 334# The maximum number of patches in a patchset generated in patch 335# view. Set this to 0 or undef to disable patch view, or to a 336# negative number to remove any limit. 337 338# To disable system wide have in $GITWEB_CONFIG 339# $feature{'patches'}{'default'} = [0]; 340# To have project specific config enable override in $GITWEB_CONFIG 341# $feature{'patches'}{'override'} = 1; 342# and in project config gitweb.patches = 0|n; 343# where n is the maximum number of patches allowed in a patchset. 344'patches'=> { 345'sub'=> \&feature_patches, 346'override'=>0, 347'default'=> [16]}, 348); 349 350sub gitweb_get_feature { 351my($name) =@_; 352return unlessexists$feature{$name}; 353my($sub,$override,@defaults) = ( 354$feature{$name}{'sub'}, 355$feature{$name}{'override'}, 356@{$feature{$name}{'default'}}); 357if(!$override) {return@defaults; } 358if(!defined$sub) { 359warn"feature$nameis not overrideable"; 360return@defaults; 361} 362return$sub->(@defaults); 363} 364 365# A wrapper to check if a given feature is enabled. 366# With this, you can say 367# 368# my $bool_feat = gitweb_check_feature('bool_feat'); 369# gitweb_check_feature('bool_feat') or somecode; 370# 371# instead of 372# 373# my ($bool_feat) = gitweb_get_feature('bool_feat'); 374# (gitweb_get_feature('bool_feat'))[0] or somecode; 375# 376sub gitweb_check_feature { 377return(gitweb_get_feature(@_))[0]; 378} 379 380 381sub feature_bool { 382my$key=shift; 383my($val) = git_get_project_config($key,'--bool'); 384 385if($valeq'true') { 386return(1); 387}elsif($valeq'false') { 388return(0); 389} 390 391return($_[0]); 392} 393 394sub feature_snapshot { 395my(@fmts) =@_; 396 397my($val) = git_get_project_config('snapshot'); 398 399if($val) { 400@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 401} 402 403return@fmts; 404} 405 406sub feature_patches { 407my@val= (git_get_project_config('patches','--int')); 408 409if(@val) { 410return@val; 411} 412 413return($_[0]); 414} 415 416# checking HEAD file with -e is fragile if the repository was 417# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 418# and then pruned. 419sub check_head_link { 420my($dir) =@_; 421my$headfile="$dir/HEAD"; 422return((-e $headfile) || 423(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 424} 425 426sub check_export_ok { 427my($dir) =@_; 428return(check_head_link($dir) && 429(!$export_ok|| -e "$dir/$export_ok") && 430(!$export_auth_hook||$export_auth_hook->($dir))); 431} 432 433# process alternate names for backward compatibility 434# filter out unsupported (unknown) snapshot formats 435sub filter_snapshot_fmts { 436my@fmts=@_; 437 438@fmts=map{ 439exists$known_snapshot_format_aliases{$_} ? 440$known_snapshot_format_aliases{$_} :$_}@fmts; 441@fmts=grep(exists$known_snapshot_formats{$_},@fmts); 442 443} 444 445our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 446if(-e $GITWEB_CONFIG) { 447do$GITWEB_CONFIG; 448}else{ 449our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 450do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 451} 452 453# version of the core git binary 454our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 455 456$projects_list||=$projectroot; 457 458# ====================================================================== 459# input validation and dispatch 460 461# input parameters can be collected from a variety of sources (presently, CGI 462# and PATH_INFO), so we define an %input_params hash that collects them all 463# together during validation: this allows subsequent uses (e.g. href()) to be 464# agnostic of the parameter origin 465 466our%input_params= (); 467 468# input parameters are stored with the long parameter name as key. This will 469# also be used in the href subroutine to convert parameters to their CGI 470# equivalent, and since the href() usage is the most frequent one, we store 471# the name -> CGI key mapping here, instead of the reverse. 472# 473# XXX: Warning: If you touch this, check the search form for updating, 474# too. 475 476our@cgi_param_mapping= ( 477 project =>"p", 478 action =>"a", 479 file_name =>"f", 480 file_parent =>"fp", 481 hash =>"h", 482 hash_parent =>"hp", 483 hash_base =>"hb", 484 hash_parent_base =>"hpb", 485 page =>"pg", 486 order =>"o", 487 searchtext =>"s", 488 searchtype =>"st", 489 snapshot_format =>"sf", 490 extra_options =>"opt", 491 search_use_regexp =>"sr", 492); 493our%cgi_param_mapping=@cgi_param_mapping; 494 495# we will also need to know the possible actions, for validation 496our%actions= ( 497"blame"=> \&git_blame, 498"blobdiff"=> \&git_blobdiff, 499"blobdiff_plain"=> \&git_blobdiff_plain, 500"blob"=> \&git_blob, 501"blob_plain"=> \&git_blob_plain, 502"commitdiff"=> \&git_commitdiff, 503"commitdiff_plain"=> \&git_commitdiff_plain, 504"commit"=> \&git_commit, 505"forks"=> \&git_forks, 506"heads"=> \&git_heads, 507"history"=> \&git_history, 508"log"=> \&git_log, 509"patch"=> \&git_patch, 510"patches"=> \&git_patches, 511"rss"=> \&git_rss, 512"atom"=> \&git_atom, 513"search"=> \&git_search, 514"search_help"=> \&git_search_help, 515"shortlog"=> \&git_shortlog, 516"summary"=> \&git_summary, 517"tag"=> \&git_tag, 518"tags"=> \&git_tags, 519"tree"=> \&git_tree, 520"snapshot"=> \&git_snapshot, 521"object"=> \&git_object, 522# those below don't need $project 523"opml"=> \&git_opml, 524"project_list"=> \&git_project_list, 525"project_index"=> \&git_project_index, 526); 527 528# finally, we have the hash of allowed extra_options for the commands that 529# allow them 530our%allowed_options= ( 531"--no-merges"=> [qw(rss atom log shortlog history)], 532); 533 534# fill %input_params with the CGI parameters. All values except for 'opt' 535# should be single values, but opt can be an array. We should probably 536# build an array of parameters that can be multi-valued, but since for the time 537# being it's only this one, we just single it out 538while(my($name,$symbol) =each%cgi_param_mapping) { 539if($symboleq'opt') { 540$input_params{$name} = [$cgi->param($symbol) ]; 541}else{ 542$input_params{$name} =$cgi->param($symbol); 543} 544} 545 546# now read PATH_INFO and update the parameter list for missing parameters 547sub evaluate_path_info { 548return ifdefined$input_params{'project'}; 549return if!$path_info; 550$path_info=~ s,^/+,,; 551return if!$path_info; 552 553# find which part of PATH_INFO is project 554my$project=$path_info; 555$project=~ s,/+$,,; 556while($project&& !check_head_link("$projectroot/$project")) { 557$project=~ s,/*[^/]*$,,; 558} 559return unless$project; 560$input_params{'project'} =$project; 561 562# do not change any parameters if an action is given using the query string 563return if$input_params{'action'}; 564$path_info=~ s,^\Q$project\E/*,,; 565 566# next, check if we have an action 567my$action=$path_info; 568$action=~ s,/.*$,,; 569if(exists$actions{$action}) { 570$path_info=~ s,^$action/*,,; 571$input_params{'action'} =$action; 572} 573 574# list of actions that want hash_base instead of hash, but can have no 575# pathname (f) parameter 576my@wants_base= ( 577'tree', 578'history', 579); 580 581# we want to catch 582# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 583my($parentrefname,$parentpathname,$refname,$pathname) = 584($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 585 586# first, analyze the 'current' part 587if(defined$pathname) { 588# we got "branch:filename" or "branch:dir/" 589# we could use git_get_type(branch:pathname), but: 590# - it needs $git_dir 591# - it does a git() call 592# - the convention of terminating directories with a slash 593# makes it superfluous 594# - embedding the action in the PATH_INFO would make it even 595# more superfluous 596$pathname=~ s,^/+,,; 597if(!$pathname||substr($pathname, -1)eq"/") { 598$input_params{'action'} ||="tree"; 599$pathname=~ s,/$,,; 600}else{ 601# the default action depends on whether we had parent info 602# or not 603if($parentrefname) { 604$input_params{'action'} ||="blobdiff_plain"; 605}else{ 606$input_params{'action'} ||="blob_plain"; 607} 608} 609$input_params{'hash_base'} ||=$refname; 610$input_params{'file_name'} ||=$pathname; 611}elsif(defined$refname) { 612# we got "branch". In this case we have to choose if we have to 613# set hash or hash_base. 614# 615# Most of the actions without a pathname only want hash to be 616# set, except for the ones specified in @wants_base that want 617# hash_base instead. It should also be noted that hand-crafted 618# links having 'history' as an action and no pathname or hash 619# set will fail, but that happens regardless of PATH_INFO. 620$input_params{'action'} ||="shortlog"; 621if(grep{$_eq$input_params{'action'} }@wants_base) { 622$input_params{'hash_base'} ||=$refname; 623}else{ 624$input_params{'hash'} ||=$refname; 625} 626} 627 628# next, handle the 'parent' part, if present 629if(defined$parentrefname) { 630# a missing pathspec defaults to the 'current' filename, allowing e.g. 631# someproject/blobdiff/oldrev..newrev:/filename 632if($parentpathname) { 633$parentpathname=~ s,^/+,,; 634$parentpathname=~ s,/$,,; 635$input_params{'file_parent'} ||=$parentpathname; 636}else{ 637$input_params{'file_parent'} ||=$input_params{'file_name'}; 638} 639# we assume that hash_parent_base is wanted if a path was specified, 640# or if the action wants hash_base instead of hash 641if(defined$input_params{'file_parent'} || 642grep{$_eq$input_params{'action'} }@wants_base) { 643$input_params{'hash_parent_base'} ||=$parentrefname; 644}else{ 645$input_params{'hash_parent'} ||=$parentrefname; 646} 647} 648 649# for the snapshot action, we allow URLs in the form 650# $project/snapshot/$hash.ext 651# where .ext determines the snapshot and gets removed from the 652# passed $refname to provide the $hash. 653# 654# To be able to tell that $refname includes the format extension, we 655# require the following two conditions to be satisfied: 656# - the hash input parameter MUST have been set from the $refname part 657# of the URL (i.e. they must be equal) 658# - the snapshot format MUST NOT have been defined already (e.g. from 659# CGI parameter sf) 660# It's also useless to try any matching unless $refname has a dot, 661# so we check for that too 662if(defined$input_params{'action'} && 663$input_params{'action'}eq'snapshot'&& 664defined$refname&&index($refname,'.') != -1&& 665$refnameeq$input_params{'hash'} && 666!defined$input_params{'snapshot_format'}) { 667# We loop over the known snapshot formats, checking for 668# extensions. Allowed extensions are both the defined suffix 669# (which includes the initial dot already) and the snapshot 670# format key itself, with a prepended dot 671while(my($fmt,%opt) =each%known_snapshot_formats) { 672my$hash=$refname; 673my$sfx; 674$hash=~s/(\Q$opt{'suffix'}\E|\Q.$fmt\E)$//; 675next unless$sfx=$1; 676# a valid suffix was found, so set the snapshot format 677# and reset the hash parameter 678$input_params{'snapshot_format'} =$fmt; 679$input_params{'hash'} =$hash; 680# we also set the format suffix to the one requested 681# in the URL: this way a request for e.g. .tgz returns 682# a .tgz instead of a .tar.gz 683$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 684last; 685} 686} 687} 688evaluate_path_info(); 689 690our$action=$input_params{'action'}; 691if(defined$action) { 692if(!validate_action($action)) { 693 die_error(400,"Invalid action parameter"); 694} 695} 696 697# parameters which are pathnames 698our$project=$input_params{'project'}; 699if(defined$project) { 700if(!validate_project($project)) { 701undef$project; 702 die_error(404,"No such project"); 703} 704} 705 706our$file_name=$input_params{'file_name'}; 707if(defined$file_name) { 708if(!validate_pathname($file_name)) { 709 die_error(400,"Invalid file parameter"); 710} 711} 712 713our$file_parent=$input_params{'file_parent'}; 714if(defined$file_parent) { 715if(!validate_pathname($file_parent)) { 716 die_error(400,"Invalid file parent parameter"); 717} 718} 719 720# parameters which are refnames 721our$hash=$input_params{'hash'}; 722if(defined$hash) { 723if(!validate_refname($hash)) { 724 die_error(400,"Invalid hash parameter"); 725} 726} 727 728our$hash_parent=$input_params{'hash_parent'}; 729if(defined$hash_parent) { 730if(!validate_refname($hash_parent)) { 731 die_error(400,"Invalid hash parent parameter"); 732} 733} 734 735our$hash_base=$input_params{'hash_base'}; 736if(defined$hash_base) { 737if(!validate_refname($hash_base)) { 738 die_error(400,"Invalid hash base parameter"); 739} 740} 741 742our@extra_options= @{$input_params{'extra_options'}}; 743# @extra_options is always defined, since it can only be (currently) set from 744# CGI, and $cgi->param() returns the empty array in array context if the param 745# is not set 746foreachmy$opt(@extra_options) { 747if(not exists$allowed_options{$opt}) { 748 die_error(400,"Invalid option parameter"); 749} 750if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 751 die_error(400,"Invalid option parameter for this action"); 752} 753} 754 755our$hash_parent_base=$input_params{'hash_parent_base'}; 756if(defined$hash_parent_base) { 757if(!validate_refname($hash_parent_base)) { 758 die_error(400,"Invalid hash parent base parameter"); 759} 760} 761 762# other parameters 763our$page=$input_params{'page'}; 764if(defined$page) { 765if($page=~m/[^0-9]/) { 766 die_error(400,"Invalid page parameter"); 767} 768} 769 770our$searchtype=$input_params{'searchtype'}; 771if(defined$searchtype) { 772if($searchtype=~m/[^a-z]/) { 773 die_error(400,"Invalid searchtype parameter"); 774} 775} 776 777our$search_use_regexp=$input_params{'search_use_regexp'}; 778 779our$searchtext=$input_params{'searchtext'}; 780our$search_regexp; 781if(defined$searchtext) { 782if(length($searchtext) <2) { 783 die_error(403,"At least two characters are required for search parameter"); 784} 785$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 786} 787 788# path to the current git repository 789our$git_dir; 790$git_dir="$projectroot/$project"if$project; 791 792# list of supported snapshot formats 793our@snapshot_fmts= gitweb_get_feature('snapshot'); 794@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 795 796# dispatch 797if(!defined$action) { 798if(defined$hash) { 799$action= git_get_type($hash); 800}elsif(defined$hash_base&&defined$file_name) { 801$action= git_get_type("$hash_base:$file_name"); 802}elsif(defined$project) { 803$action='summary'; 804}else{ 805$action='project_list'; 806} 807} 808if(!defined($actions{$action})) { 809 die_error(400,"Unknown action"); 810} 811if($action!~m/^(opml|project_list|project_index)$/&& 812!$project) { 813 die_error(400,"Project needed"); 814} 815$actions{$action}->(); 816exit; 817 818## ====================================================================== 819## action links 820 821sub href (%) { 822my%params=@_; 823# default is to use -absolute url() i.e. $my_uri 824my$href=$params{-full} ?$my_url:$my_uri; 825 826$params{'project'} =$projectunlessexists$params{'project'}; 827 828if($params{-replay}) { 829while(my($name,$symbol) =each%cgi_param_mapping) { 830if(!exists$params{$name}) { 831$params{$name} =$input_params{$name}; 832} 833} 834} 835 836my$use_pathinfo= gitweb_check_feature('pathinfo'); 837if($use_pathinfoand defined$params{'project'}) { 838# try to put as many parameters as possible in PATH_INFO: 839# - project name 840# - action 841# - hash_parent or hash_parent_base:/file_parent 842# - hash or hash_base:/filename 843# - the snapshot_format as an appropriate suffix 844 845# When the script is the root DirectoryIndex for the domain, 846# $href here would be something like http://gitweb.example.com/ 847# Thus, we strip any trailing / from $href, to spare us double 848# slashes in the final URL 849$href=~ s,/$,,; 850 851# Then add the project name, if present 852$href.="/".esc_url($params{'project'}); 853delete$params{'project'}; 854 855# since we destructively absorb parameters, we keep this 856# boolean that remembers if we're handling a snapshot 857my$is_snapshot=$params{'action'}eq'snapshot'; 858 859# Summary just uses the project path URL, any other action is 860# added to the URL 861if(defined$params{'action'}) { 862$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 863delete$params{'action'}; 864} 865 866# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 867# stripping nonexistent or useless pieces 868$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 869||$params{'hash_parent'} ||$params{'hash'}); 870if(defined$params{'hash_base'}) { 871if(defined$params{'hash_parent_base'}) { 872$href.= esc_url($params{'hash_parent_base'}); 873# skip the file_parent if it's the same as the file_name 874delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 875if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 876$href.=":/".esc_url($params{'file_parent'}); 877delete$params{'file_parent'}; 878} 879$href.=".."; 880delete$params{'hash_parent'}; 881delete$params{'hash_parent_base'}; 882}elsif(defined$params{'hash_parent'}) { 883$href.= esc_url($params{'hash_parent'}).".."; 884delete$params{'hash_parent'}; 885} 886 887$href.= esc_url($params{'hash_base'}); 888if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 889$href.=":/".esc_url($params{'file_name'}); 890delete$params{'file_name'}; 891} 892delete$params{'hash'}; 893delete$params{'hash_base'}; 894}elsif(defined$params{'hash'}) { 895$href.= esc_url($params{'hash'}); 896delete$params{'hash'}; 897} 898 899# If the action was a snapshot, we can absorb the 900# snapshot_format parameter too 901if($is_snapshot) { 902my$fmt=$params{'snapshot_format'}; 903# snapshot_format should always be defined when href() 904# is called, but just in case some code forgets, we 905# fall back to the default 906$fmt||=$snapshot_fmts[0]; 907$href.=$known_snapshot_formats{$fmt}{'suffix'}; 908delete$params{'snapshot_format'}; 909} 910} 911 912# now encode the parameters explicitly 913my@result= (); 914for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 915my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 916if(defined$params{$name}) { 917if(ref($params{$name})eq"ARRAY") { 918foreachmy$par(@{$params{$name}}) { 919push@result,$symbol."=". esc_param($par); 920} 921}else{ 922push@result,$symbol."=". esc_param($params{$name}); 923} 924} 925} 926$href.="?".join(';',@result)ifscalar@result; 927 928return$href; 929} 930 931 932## ====================================================================== 933## validation, quoting/unquoting and escaping 934 935sub validate_action { 936my$input=shift||returnundef; 937returnundefunlessexists$actions{$input}; 938return$input; 939} 940 941sub validate_project { 942my$input=shift||returnundef; 943if(!validate_pathname($input) || 944!(-d "$projectroot/$input") || 945!check_export_ok("$projectroot/$input") || 946($strict_export&& !project_in_list($input))) { 947returnundef; 948}else{ 949return$input; 950} 951} 952 953sub validate_pathname { 954my$input=shift||returnundef; 955 956# no '.' or '..' as elements of path, i.e. no '.' nor '..' 957# at the beginning, at the end, and between slashes. 958# also this catches doubled slashes 959if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 960returnundef; 961} 962# no null characters 963if($input=~m!\0!) { 964returnundef; 965} 966return$input; 967} 968 969sub validate_refname { 970my$input=shift||returnundef; 971 972# textual hashes are O.K. 973if($input=~m/^[0-9a-fA-F]{40}$/) { 974return$input; 975} 976# it must be correct pathname 977$input= validate_pathname($input) 978orreturnundef; 979# restrictions on ref name according to git-check-ref-format 980if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { 981returnundef; 982} 983return$input; 984} 985 986# decode sequences of octets in utf8 into Perl's internal form, 987# which is utf-8 with utf8 flag set if needed. gitweb writes out 988# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning 989sub to_utf8 { 990my$str=shift; 991if(utf8::valid($str)) { 992 utf8::decode($str); 993return$str; 994}else{ 995return decode($fallback_encoding,$str, Encode::FB_DEFAULT); 996} 997} 998 999# quote unsafe chars, but keep the slash, even when it's not1000# correct, but quoted slashes look too horrible in bookmarks1001sub esc_param {1002my$str=shift;1003$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg;1004$str=~s/\+/%2B/g;1005$str=~s/ /\+/g;1006return$str;1007}10081009# quote unsafe chars in whole URL, so some charactrs cannot be quoted1010sub esc_url {1011my$str=shift;1012$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1013$str=~s/\+/%2B/g;1014$str=~s/ /\+/g;1015return$str;1016}10171018# replace invalid utf8 character with SUBSTITUTION sequence1019sub esc_html ($;%) {1020my$str=shift;1021my%opts=@_;10221023$str= to_utf8($str);1024$str=$cgi->escapeHTML($str);1025if($opts{'-nbsp'}) {1026$str=~s/ / /g;1027}1028$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1029return$str;1030}10311032# quote control characters and escape filename to HTML1033sub esc_path {1034my$str=shift;1035my%opts=@_;10361037$str= to_utf8($str);1038$str=$cgi->escapeHTML($str);1039if($opts{'-nbsp'}) {1040$str=~s/ / /g;1041}1042$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1043return$str;1044}10451046# Make control characters "printable", using character escape codes (CEC)1047sub quot_cec {1048my$cntrl=shift;1049my%opts=@_;1050my%es= (# character escape codes, aka escape sequences1051"\t"=>'\t',# tab (HT)1052"\n"=>'\n',# line feed (LF)1053"\r"=>'\r',# carrige return (CR)1054"\f"=>'\f',# form feed (FF)1055"\b"=>'\b',# backspace (BS)1056"\a"=>'\a',# alarm (bell) (BEL)1057"\e"=>'\e',# escape (ESC)1058"\013"=>'\v',# vertical tab (VT)1059"\000"=>'\0',# nul character (NUL)1060);1061my$chr= ( (exists$es{$cntrl})1062?$es{$cntrl}1063:sprintf('\%2x',ord($cntrl)) );1064if($opts{-nohtml}) {1065return$chr;1066}else{1067return"<span class=\"cntrl\">$chr</span>";1068}1069}10701071# Alternatively use unicode control pictures codepoints,1072# Unicode "printable representation" (PR)1073sub quot_upr {1074my$cntrl=shift;1075my%opts=@_;10761077my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1078if($opts{-nohtml}) {1079return$chr;1080}else{1081return"<span class=\"cntrl\">$chr</span>";1082}1083}10841085# git may return quoted and escaped filenames1086sub unquote {1087my$str=shift;10881089sub unq {1090my$seq=shift;1091my%es= (# character escape codes, aka escape sequences1092't'=>"\t",# tab (HT, TAB)1093'n'=>"\n",# newline (NL)1094'r'=>"\r",# return (CR)1095'f'=>"\f",# form feed (FF)1096'b'=>"\b",# backspace (BS)1097'a'=>"\a",# alarm (bell) (BEL)1098'e'=>"\e",# escape (ESC)1099'v'=>"\013",# vertical tab (VT)1100);11011102if($seq=~m/^[0-7]{1,3}$/) {1103# octal char sequence1104returnchr(oct($seq));1105}elsif(exists$es{$seq}) {1106# C escape sequence, aka character escape code1107return$es{$seq};1108}1109# quoted ordinary character1110return$seq;1111}11121113if($str=~m/^"(.*)"$/) {1114# needs unquoting1115$str=$1;1116$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1117}1118return$str;1119}11201121# escape tabs (convert tabs to spaces)1122sub untabify {1123my$line=shift;11241125while((my$pos=index($line,"\t")) != -1) {1126if(my$count= (8- ($pos%8))) {1127my$spaces=' ' x $count;1128$line=~s/\t/$spaces/;1129}1130}11311132return$line;1133}11341135sub project_in_list {1136my$project=shift;1137my@list= git_get_projects_list();1138return@list&&scalar(grep{$_->{'path'}eq$project}@list);1139}11401141## ----------------------------------------------------------------------1142## HTML aware string manipulation11431144# Try to chop given string on a word boundary between position1145# $len and $len+$add_len. If there is no word boundary there,1146# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1147# (marking chopped part) would be longer than given string.1148sub chop_str {1149my$str=shift;1150my$len=shift;1151my$add_len=shift||10;1152my$where=shift||'right';# 'left' | 'center' | 'right'11531154# Make sure perl knows it is utf8 encoded so we don't1155# cut in the middle of a utf8 multibyte char.1156$str= to_utf8($str);11571158# allow only $len chars, but don't cut a word if it would fit in $add_len1159# if it doesn't fit, cut it if it's still longer than the dots we would add1160# remove chopped character entities entirely11611162# when chopping in the middle, distribute $len into left and right part1163# return early if chopping wouldn't make string shorter1164if($whereeq'center') {1165return$strif($len+5>=length($str));# filler is length 51166$len=int($len/2);1167}else{1168return$strif($len+4>=length($str));# filler is length 41169}11701171# regexps: ending and beginning with word part up to $add_len1172my$endre=qr/.{$len}\w{0,$add_len}/;1173my$begre=qr/\w{0,$add_len}.{$len}/;11741175if($whereeq'left') {1176$str=~m/^(.*?)($begre)$/;1177my($lead,$body) = ($1,$2);1178if(length($lead) >4) {1179$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1180$lead=" ...";1181}1182return"$lead$body";11831184}elsif($whereeq'center') {1185$str=~m/^($endre)(.*)$/;1186my($left,$str) = ($1,$2);1187$str=~m/^(.*?)($begre)$/;1188my($mid,$right) = ($1,$2);1189if(length($mid) >5) {1190$left=~s/&[^;]*$//;1191$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1192$mid=" ... ";1193}1194return"$left$mid$right";11951196}else{1197$str=~m/^($endre)(.*)$/;1198my$body=$1;1199my$tail=$2;1200if(length($tail) >4) {1201$body=~s/&[^;]*$//;1202$tail="... ";1203}1204return"$body$tail";1205}1206}12071208# takes the same arguments as chop_str, but also wraps a <span> around the1209# result with a title attribute if it does get chopped. Additionally, the1210# string is HTML-escaped.1211sub chop_and_escape_str {1212my($str) =@_;12131214my$chopped= chop_str(@_);1215if($choppedeq$str) {1216return esc_html($chopped);1217}else{1218$str=~s/([[:cntrl:]])/?/g;1219return$cgi->span({-title=>$str}, esc_html($chopped));1220}1221}12221223## ----------------------------------------------------------------------1224## functions returning short strings12251226# CSS class for given age value (in seconds)1227sub age_class {1228my$age=shift;12291230if(!defined$age) {1231return"noage";1232}elsif($age<60*60*2) {1233return"age0";1234}elsif($age<60*60*24*2) {1235return"age1";1236}else{1237return"age2";1238}1239}12401241# convert age in seconds to "nn units ago" string1242sub age_string {1243my$age=shift;1244my$age_str;12451246if($age>60*60*24*365*2) {1247$age_str= (int$age/60/60/24/365);1248$age_str.=" years ago";1249}elsif($age>60*60*24*(365/12)*2) {1250$age_str=int$age/60/60/24/(365/12);1251$age_str.=" months ago";1252}elsif($age>60*60*24*7*2) {1253$age_str=int$age/60/60/24/7;1254$age_str.=" weeks ago";1255}elsif($age>60*60*24*2) {1256$age_str=int$age/60/60/24;1257$age_str.=" days ago";1258}elsif($age>60*60*2) {1259$age_str=int$age/60/60;1260$age_str.=" hours ago";1261}elsif($age>60*2) {1262$age_str=int$age/60;1263$age_str.=" min ago";1264}elsif($age>2) {1265$age_str=int$age;1266$age_str.=" sec ago";1267}else{1268$age_str.=" right now";1269}1270return$age_str;1271}12721273useconstant{1274 S_IFINVALID =>0030000,1275 S_IFGITLINK =>0160000,1276};12771278# submodule/subproject, a commit object reference1279sub S_ISGITLINK($) {1280my$mode=shift;12811282return(($mode& S_IFMT) == S_IFGITLINK)1283}12841285# convert file mode in octal to symbolic file mode string1286sub mode_str {1287my$mode=oct shift;12881289if(S_ISGITLINK($mode)) {1290return'm---------';1291}elsif(S_ISDIR($mode& S_IFMT)) {1292return'drwxr-xr-x';1293}elsif(S_ISLNK($mode)) {1294return'lrwxrwxrwx';1295}elsif(S_ISREG($mode)) {1296# git cares only about the executable bit1297if($mode& S_IXUSR) {1298return'-rwxr-xr-x';1299}else{1300return'-rw-r--r--';1301};1302}else{1303return'----------';1304}1305}13061307# convert file mode in octal to file type string1308sub file_type {1309my$mode=shift;13101311if($mode!~m/^[0-7]+$/) {1312return$mode;1313}else{1314$mode=oct$mode;1315}13161317if(S_ISGITLINK($mode)) {1318return"submodule";1319}elsif(S_ISDIR($mode& S_IFMT)) {1320return"directory";1321}elsif(S_ISLNK($mode)) {1322return"symlink";1323}elsif(S_ISREG($mode)) {1324return"file";1325}else{1326return"unknown";1327}1328}13291330# convert file mode in octal to file type description string1331sub file_type_long {1332my$mode=shift;13331334if($mode!~m/^[0-7]+$/) {1335return$mode;1336}else{1337$mode=oct$mode;1338}13391340if(S_ISGITLINK($mode)) {1341return"submodule";1342}elsif(S_ISDIR($mode& S_IFMT)) {1343return"directory";1344}elsif(S_ISLNK($mode)) {1345return"symlink";1346}elsif(S_ISREG($mode)) {1347if($mode& S_IXUSR) {1348return"executable";1349}else{1350return"file";1351};1352}else{1353return"unknown";1354}1355}135613571358## ----------------------------------------------------------------------1359## functions returning short HTML fragments, or transforming HTML fragments1360## which don't belong to other sections13611362# format line of commit message.1363sub format_log_line_html {1364my$line=shift;13651366$line= esc_html($line, -nbsp=>1);1367if($line=~m/([0-9a-fA-F]{8,40})/) {1368my$hash_text=$1;1369my$link=1370$cgi->a({-href => href(action=>"object", hash=>$hash_text),1371-class=>"text"},$hash_text);1372$line=~s/$hash_text/$link/;1373}1374return$line;1375}13761377# format marker of refs pointing to given object13781379# the destination action is chosen based on object type and current context:1380# - for annotated tags, we choose the tag view unless it's the current view1381# already, in which case we go to shortlog view1382# - for other refs, we keep the current view if we're in history, shortlog or1383# log view, and select shortlog otherwise1384sub format_ref_marker {1385my($refs,$id) =@_;1386my$markers='';13871388if(defined$refs->{$id}) {1389foreachmy$ref(@{$refs->{$id}}) {1390# this code exploits the fact that non-lightweight tags are the1391# only indirect objects, and that they are the only objects for which1392# we want to use tag instead of shortlog as action1393my($type,$name) =qw();1394my$indirect= ($ref=~s/\^\{\}$//);1395# e.g. tags/v2.6.11 or heads/next1396if($ref=~m!^(.*?)s?/(.*)$!) {1397$type=$1;1398$name=$2;1399}else{1400$type="ref";1401$name=$ref;1402}14031404my$class=$type;1405$class.=" indirect"if$indirect;14061407my$dest_action="shortlog";14081409if($indirect) {1410$dest_action="tag"unless$actioneq"tag";1411}elsif($action=~/^(history|(short)?log)$/) {1412$dest_action=$action;1413}14141415my$dest="";1416$dest.="refs/"unless$ref=~ m!^refs/!;1417$dest.=$ref;14181419my$link=$cgi->a({1420-href => href(1421 action=>$dest_action,1422 hash=>$dest1423)},$name);14241425$markers.=" <span class=\"$class\"title=\"$ref\">".1426$link."</span>";1427}1428}14291430if($markers) {1431return' <span class="refs">'.$markers.'</span>';1432}else{1433return"";1434}1435}14361437# format, perhaps shortened and with markers, title line1438sub format_subject_html {1439my($long,$short,$href,$extra) =@_;1440$extra=''unlessdefined($extra);14411442if(length($short) <length($long)) {1443return$cgi->a({-href =>$href, -class=>"list subject",1444-title => to_utf8($long)},1445 esc_html($short) .$extra);1446}else{1447return$cgi->a({-href =>$href, -class=>"list subject"},1448 esc_html($long) .$extra);1449}1450}14511452# format git diff header line, i.e. "diff --(git|combined|cc) ..."1453sub format_git_diff_header_line {1454my$line=shift;1455my$diffinfo=shift;1456my($from,$to) =@_;14571458if($diffinfo->{'nparents'}) {1459# combined diff1460$line=~s!^(diff (.*?) )"?.*$!$1!;1461if($to->{'href'}) {1462$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1463 esc_path($to->{'file'}));1464}else{# file was deleted (no href)1465$line.= esc_path($to->{'file'});1466}1467}else{1468# "ordinary" diff1469$line=~s!^(diff (.*?) )"?a/.*$!$1!;1470if($from->{'href'}) {1471$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1472'a/'. esc_path($from->{'file'}));1473}else{# file was added (no href)1474$line.='a/'. esc_path($from->{'file'});1475}1476$line.=' ';1477if($to->{'href'}) {1478$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1479'b/'. esc_path($to->{'file'}));1480}else{# file was deleted1481$line.='b/'. esc_path($to->{'file'});1482}1483}14841485return"<div class=\"diff header\">$line</div>\n";1486}14871488# format extended diff header line, before patch itself1489sub format_extended_diff_header_line {1490my$line=shift;1491my$diffinfo=shift;1492my($from,$to) =@_;14931494# match <path>1495if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1496$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1497 esc_path($from->{'file'}));1498}1499if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1500$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1501 esc_path($to->{'file'}));1502}1503# match single <mode>1504if($line=~m/\s(\d{6})$/) {1505$line.='<span class="info"> ('.1506 file_type_long($1) .1507')</span>';1508}1509# match <hash>1510if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1511# can match only for combined diff1512$line='index ';1513for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1514if($from->{'href'}[$i]) {1515$line.=$cgi->a({-href=>$from->{'href'}[$i],1516-class=>"hash"},1517substr($diffinfo->{'from_id'}[$i],0,7));1518}else{1519$line.='0' x 7;1520}1521# separator1522$line.=','if($i<$diffinfo->{'nparents'} -1);1523}1524$line.='..';1525if($to->{'href'}) {1526$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1527substr($diffinfo->{'to_id'},0,7));1528}else{1529$line.='0' x 7;1530}15311532}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1533# can match only for ordinary diff1534my($from_link,$to_link);1535if($from->{'href'}) {1536$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1537substr($diffinfo->{'from_id'},0,7));1538}else{1539$from_link='0' x 7;1540}1541if($to->{'href'}) {1542$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1543substr($diffinfo->{'to_id'},0,7));1544}else{1545$to_link='0' x 7;1546}1547my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1548$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1549}15501551return$line."<br/>\n";1552}15531554# format from-file/to-file diff header1555sub format_diff_from_to_header {1556my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1557my$line;1558my$result='';15591560$line=$from_line;1561#assert($line =~ m/^---/) if DEBUG;1562# no extra formatting for "^--- /dev/null"1563if(!$diffinfo->{'nparents'}) {1564# ordinary (single parent) diff1565if($line=~m!^--- "?a/!) {1566if($from->{'href'}) {1567$line='--- a/'.1568$cgi->a({-href=>$from->{'href'}, -class=>"path"},1569 esc_path($from->{'file'}));1570}else{1571$line='--- a/'.1572 esc_path($from->{'file'});1573}1574}1575$result.= qq!<div class="diff from_file">$line</div>\n!;15761577}else{1578# combined diff (merge commit)1579for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1580if($from->{'href'}[$i]) {1581$line='--- '.1582$cgi->a({-href=>href(action=>"blobdiff",1583 hash_parent=>$diffinfo->{'from_id'}[$i],1584 hash_parent_base=>$parents[$i],1585 file_parent=>$from->{'file'}[$i],1586 hash=>$diffinfo->{'to_id'},1587 hash_base=>$hash,1588 file_name=>$to->{'file'}),1589-class=>"path",1590-title=>"diff". ($i+1)},1591$i+1) .1592'/'.1593$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1594 esc_path($from->{'file'}[$i]));1595}else{1596$line='--- /dev/null';1597}1598$result.= qq!<div class="diff from_file">$line</div>\n!;1599}1600}16011602$line=$to_line;1603#assert($line =~ m/^\+\+\+/) if DEBUG;1604# no extra formatting for "^+++ /dev/null"1605if($line=~m!^\+\+\+ "?b/!) {1606if($to->{'href'}) {1607$line='+++ b/'.1608$cgi->a({-href=>$to->{'href'}, -class=>"path"},1609 esc_path($to->{'file'}));1610}else{1611$line='+++ b/'.1612 esc_path($to->{'file'});1613}1614}1615$result.= qq!<div class="diff to_file">$line</div>\n!;16161617return$result;1618}16191620# create note for patch simplified by combined diff1621sub format_diff_cc_simplified {1622my($diffinfo,@parents) =@_;1623my$result='';16241625$result.="<div class=\"diff header\">".1626"diff --cc ";1627if(!is_deleted($diffinfo)) {1628$result.=$cgi->a({-href => href(action=>"blob",1629 hash_base=>$hash,1630 hash=>$diffinfo->{'to_id'},1631 file_name=>$diffinfo->{'to_file'}),1632-class=>"path"},1633 esc_path($diffinfo->{'to_file'}));1634}else{1635$result.= esc_path($diffinfo->{'to_file'});1636}1637$result.="</div>\n".# class="diff header"1638"<div class=\"diff nodifferences\">".1639"Simple merge".1640"</div>\n";# class="diff nodifferences"16411642return$result;1643}16441645# format patch (diff) line (not to be used for diff headers)1646sub format_diff_line {1647my$line=shift;1648my($from,$to) =@_;1649my$diff_class="";16501651chomp$line;16521653if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1654# combined diff1655my$prefix=substr($line,0,scalar@{$from->{'href'}});1656if($line=~m/^\@{3}/) {1657$diff_class=" chunk_header";1658}elsif($line=~m/^\\/) {1659$diff_class=" incomplete";1660}elsif($prefix=~tr/+/+/) {1661$diff_class=" add";1662}elsif($prefix=~tr/-/-/) {1663$diff_class=" rem";1664}1665}else{1666# assume ordinary diff1667my$char=substr($line,0,1);1668if($chareq'+') {1669$diff_class=" add";1670}elsif($chareq'-') {1671$diff_class=" rem";1672}elsif($chareq'@') {1673$diff_class=" chunk_header";1674}elsif($chareq"\\") {1675$diff_class=" incomplete";1676}1677}1678$line= untabify($line);1679if($from&&$to&&$line=~m/^\@{2} /) {1680my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1681$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;16821683$from_lines=0unlessdefined$from_lines;1684$to_lines=0unlessdefined$to_lines;16851686if($from->{'href'}) {1687$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1688-class=>"list"},$from_text);1689}1690if($to->{'href'}) {1691$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1692-class=>"list"},$to_text);1693}1694$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1695"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1696return"<div class=\"diff$diff_class\">$line</div>\n";1697}elsif($from&&$to&&$line=~m/^\@{3}/) {1698my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1699my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);17001701@from_text=split(' ',$ranges);1702for(my$i=0;$i<@from_text; ++$i) {1703($from_start[$i],$from_nlines[$i]) =1704(split(',',substr($from_text[$i],1)),0);1705}17061707$to_text=pop@from_text;1708$to_start=pop@from_start;1709$to_nlines=pop@from_nlines;17101711$line="<span class=\"chunk_info\">$prefix";1712for(my$i=0;$i<@from_text; ++$i) {1713if($from->{'href'}[$i]) {1714$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1715-class=>"list"},$from_text[$i]);1716}else{1717$line.=$from_text[$i];1718}1719$line.=" ";1720}1721if($to->{'href'}) {1722$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1723-class=>"list"},$to_text);1724}else{1725$line.=$to_text;1726}1727$line.="$prefix</span>".1728"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1729return"<div class=\"diff$diff_class\">$line</div>\n";1730}1731return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1732}17331734# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1735# linked. Pass the hash of the tree/commit to snapshot.1736sub format_snapshot_links {1737my($hash) =@_;1738my$num_fmts=@snapshot_fmts;1739if($num_fmts>1) {1740# A parenthesized list of links bearing format names.1741# e.g. "snapshot (_tar.gz_ _zip_)"1742return"snapshot (".join(' ',map1743$cgi->a({1744-href => href(1745 action=>"snapshot",1746 hash=>$hash,1747 snapshot_format=>$_1748)1749},$known_snapshot_formats{$_}{'display'})1750,@snapshot_fmts) .")";1751}elsif($num_fmts==1) {1752# A single "snapshot" link whose tooltip bears the format name.1753# i.e. "_snapshot_"1754my($fmt) =@snapshot_fmts;1755return1756$cgi->a({1757-href => href(1758 action=>"snapshot",1759 hash=>$hash,1760 snapshot_format=>$fmt1761),1762-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1763},"snapshot");1764}else{# $num_fmts == 01765returnundef;1766}1767}17681769## ......................................................................1770## functions returning values to be passed, perhaps after some1771## transformation, to other functions; e.g. returning arguments to href()17721773# returns hash to be passed to href to generate gitweb URL1774# in -title key it returns description of link1775sub get_feed_info {1776my$format=shift||'Atom';1777my%res= (action =>lc($format));17781779# feed links are possible only for project views1780return unless(defined$project);1781# some views should link to OPML, or to generic project feed,1782# or don't have specific feed yet (so they should use generic)1783return if($action=~/^(?:tags|heads|forks|tag|search)$/x);17841785my$branch;1786# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1787# from tag links; this also makes possible to detect branch links1788if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1789(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1790$branch=$1;1791}1792# find log type for feed description (title)1793my$type='log';1794if(defined$file_name) {1795$type="history of$file_name";1796$type.="/"if($actioneq'tree');1797$type.=" on '$branch'"if(defined$branch);1798}else{1799$type="log of$branch"if(defined$branch);1800}18011802$res{-title} =$type;1803$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1804$res{'file_name'} =$file_name;18051806return%res;1807}18081809## ----------------------------------------------------------------------1810## git utility subroutines, invoking git commands18111812# returns path to the core git executable and the --git-dir parameter as list1813sub git_cmd {1814return$GIT,'--git-dir='.$git_dir;1815}18161817# quote the given arguments for passing them to the shell1818# quote_command("command", "arg 1", "arg with ' and ! characters")1819# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1820# Try to avoid using this function wherever possible.1821sub quote_command {1822returnjoin(' ',1823map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1824}18251826# get HEAD ref of given project as hash1827sub git_get_head_hash {1828my$project=shift;1829my$o_git_dir=$git_dir;1830my$retval=undef;1831$git_dir="$projectroot/$project";1832if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1833my$head= <$fd>;1834close$fd;1835if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1836$retval=$1;1837}1838}1839if(defined$o_git_dir) {1840$git_dir=$o_git_dir;1841}1842return$retval;1843}18441845# get type of given object1846sub git_get_type {1847my$hash=shift;18481849open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1850my$type= <$fd>;1851close$fdorreturn;1852chomp$type;1853return$type;1854}18551856# repository configuration1857our$config_file='';1858our%config;18591860# store multiple values for single key as anonymous array reference1861# single values stored directly in the hash, not as [ <value> ]1862sub hash_set_multi {1863my($hash,$key,$value) =@_;18641865if(!exists$hash->{$key}) {1866$hash->{$key} =$value;1867}elsif(!ref$hash->{$key}) {1868$hash->{$key} = [$hash->{$key},$value];1869}else{1870push@{$hash->{$key}},$value;1871}1872}18731874# return hash of git project configuration1875# optionally limited to some section, e.g. 'gitweb'1876sub git_parse_project_config {1877my$section_regexp=shift;1878my%config;18791880local$/="\0";18811882open my$fh,"-|", git_cmd(),"config",'-z','-l',1883orreturn;18841885while(my$keyval= <$fh>) {1886chomp$keyval;1887my($key,$value) =split(/\n/,$keyval,2);18881889 hash_set_multi(\%config,$key,$value)1890if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1891}1892close$fh;18931894return%config;1895}18961897# convert config value to boolean, 'true' or 'false'1898# no value, number > 0, 'true' and 'yes' values are true1899# rest of values are treated as false (never as error)1900sub config_to_bool {1901my$val=shift;19021903# strip leading and trailing whitespace1904$val=~s/^\s+//;1905$val=~s/\s+$//;19061907return(!defined$val||# section.key1908($val=~/^\d+$/&&$val) ||# section.key = 11909($val=~/^(?:true|yes)$/i));# section.key = true1910}19111912# convert config value to simple decimal number1913# an optional value suffix of 'k', 'm', or 'g' will cause the value1914# to be multiplied by 1024, 1048576, or 10737418241915sub config_to_int {1916my$val=shift;19171918# strip leading and trailing whitespace1919$val=~s/^\s+//;1920$val=~s/\s+$//;19211922if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1923$unit=lc($unit);1924# unknown unit is treated as 11925return$num* ($uniteq'g'?1073741824:1926$uniteq'm'?1048576:1927$uniteq'k'?1024:1);1928}1929return$val;1930}19311932# convert config value to array reference, if needed1933sub config_to_multi {1934my$val=shift;19351936returnref($val) ?$val: (defined($val) ? [$val] : []);1937}19381939sub git_get_project_config {1940my($key,$type) =@_;19411942# key sanity check1943return unless($key);1944$key=~s/^gitweb\.//;1945return if($key=~m/\W/);19461947# type sanity check1948if(defined$type) {1949$type=~s/^--//;1950$type=undef1951unless($typeeq'bool'||$typeeq'int');1952}19531954# get config1955if(!defined$config_file||1956$config_filene"$git_dir/config") {1957%config= git_parse_project_config('gitweb');1958$config_file="$git_dir/config";1959}19601961# ensure given type1962if(!defined$type) {1963return$config{"gitweb.$key"};1964}elsif($typeeq'bool') {1965# backward compatibility: 'git config --bool' returns true/false1966return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1967}elsif($typeeq'int') {1968return config_to_int($config{"gitweb.$key"});1969}1970return$config{"gitweb.$key"};1971}19721973# get hash of given path at given ref1974sub git_get_hash_by_path {1975my$base=shift;1976my$path=shift||returnundef;1977my$type=shift;19781979$path=~ s,/+$,,;19801981open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path1982or die_error(500,"Open git-ls-tree failed");1983my$line= <$fd>;1984close$fdorreturnundef;19851986if(!defined$line) {1987# there is no tree or hash given by $path at $base1988returnundef;1989}19901991#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'1992$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;1993if(defined$type&&$typene$2) {1994# type doesn't match1995returnundef;1996}1997return$3;1998}19992000# get path of entry with given hash at given tree-ish (ref)2001# used to get 'from' filename for combined diff (merge commit) for renames2002sub git_get_path_by_hash {2003my$base=shift||return;2004my$hash=shift||return;20052006local$/="\0";20072008open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2009orreturnundef;2010while(my$line= <$fd>) {2011chomp$line;20122013#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2014#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2015if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2016close$fd;2017return$1;2018}2019}2020close$fd;2021returnundef;2022}20232024## ......................................................................2025## git utility functions, directly accessing git repository20262027sub git_get_project_description {2028my$path=shift;20292030$git_dir="$projectroot/$path";2031open my$fd,"$git_dir/description"2032orreturn git_get_project_config('description');2033my$descr= <$fd>;2034close$fd;2035if(defined$descr) {2036chomp$descr;2037}2038return$descr;2039}20402041sub git_get_project_ctags {2042my$path=shift;2043my$ctags= {};20442045$git_dir="$projectroot/$path";2046unless(opendir D,"$git_dir/ctags") {2047return$ctags;2048}2049foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir(D)) {2050open CT,$_ornext;2051my$val= <CT>;2052chomp$val;2053close CT;2054my$ctag=$_;$ctag=~ s#.*/##;2055$ctags->{$ctag} =$val;2056}2057closedir D;2058$ctags;2059}20602061sub git_populate_project_tagcloud {2062my$ctags=shift;20632064# First, merge different-cased tags; tags vote on casing2065my%ctags_lc;2066foreach(keys%$ctags) {2067$ctags_lc{lc$_}->{count} +=$ctags->{$_};2068if(not$ctags_lc{lc$_}->{topcount}2069or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2070$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2071$ctags_lc{lc$_}->{topname} =$_;2072}2073}20742075my$cloud;2076if(eval{require HTML::TagCloud;1; }) {2077$cloud= HTML::TagCloud->new;2078foreach(sort keys%ctags_lc) {2079# Pad the title with spaces so that the cloud looks2080# less crammed.2081my$title=$ctags_lc{$_}->{topname};2082$title=~s/ / /g;2083$title=~s/^/ /g;2084$title=~s/$/ /g;2085$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2086}2087}else{2088$cloud= \%ctags_lc;2089}2090$cloud;2091}20922093sub git_show_project_tagcloud {2094my($cloud,$count) =@_;2095print STDERR ref($cloud)."..\n";2096if(ref$cloudeq'HTML::TagCloud') {2097return$cloud->html_and_css($count);2098}else{2099my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2100return'<p align="center">'.join(', ',map{2101"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2102}splice(@tags,0,$count)) .'</p>';2103}2104}21052106sub git_get_project_url_list {2107my$path=shift;21082109$git_dir="$projectroot/$path";2110open my$fd,"$git_dir/cloneurl"2111orreturnwantarray?2112@{ config_to_multi(git_get_project_config('url')) } :2113 config_to_multi(git_get_project_config('url'));2114my@git_project_url_list=map{chomp;$_} <$fd>;2115close$fd;21162117returnwantarray?@git_project_url_list: \@git_project_url_list;2118}21192120sub git_get_projects_list {2121my($filter) =@_;2122my@list;21232124$filter||='';2125$filter=~s/\.git$//;21262127my$check_forks= gitweb_check_feature('forks');21282129if(-d $projects_list) {2130# search in directory2131my$dir=$projects_list. ($filter?"/$filter":'');2132# remove the trailing "/"2133$dir=~s!/+$!!;2134my$pfxlen=length("$dir");2135my$pfxdepth= ($dir=~tr!/!!);21362137 File::Find::find({2138 follow_fast =>1,# follow symbolic links2139 follow_skip =>2,# ignore duplicates2140 dangling_symlinks =>0,# ignore dangling symlinks, silently2141 wanted =>sub{2142# skip project-list toplevel, if we get it.2143return if(m!^[/.]$!);2144# only directories can be git repositories2145return unless(-d $_);2146# don't traverse too deep (Find is super slow on os x)2147if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2148$File::Find::prune =1;2149return;2150}21512152my$subdir=substr($File::Find::name,$pfxlen+1);2153# we check related file in $projectroot2154my$path= ($filter?"$filter/":'') .$subdir;2155if(check_export_ok("$projectroot/$path")) {2156push@list, { path =>$path};2157$File::Find::prune =1;2158}2159},2160},"$dir");21612162}elsif(-f $projects_list) {2163# read from file(url-encoded):2164# 'git%2Fgit.git Linus+Torvalds'2165# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2166# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2167my%paths;2168open my($fd),$projects_listorreturn;2169 PROJECT:2170while(my$line= <$fd>) {2171chomp$line;2172my($path,$owner) =split' ',$line;2173$path= unescape($path);2174$owner= unescape($owner);2175if(!defined$path) {2176next;2177}2178if($filterne'') {2179# looking for forks;2180my$pfx=substr($path,0,length($filter));2181if($pfxne$filter) {2182next PROJECT;2183}2184my$sfx=substr($path,length($filter));2185if($sfx!~/^\/.*\.git$/) {2186next PROJECT;2187}2188}elsif($check_forks) {2189 PATH:2190foreachmy$filter(keys%paths) {2191# looking for forks;2192my$pfx=substr($path,0,length($filter));2193if($pfxne$filter) {2194next PATH;2195}2196my$sfx=substr($path,length($filter));2197if($sfx!~/^\/.*\.git$/) {2198next PATH;2199}2200# is a fork, don't include it in2201# the list2202next PROJECT;2203}2204}2205if(check_export_ok("$projectroot/$path")) {2206my$pr= {2207 path =>$path,2208 owner => to_utf8($owner),2209};2210push@list,$pr;2211(my$forks_path=$path) =~s/\.git$//;2212$paths{$forks_path}++;2213}2214}2215close$fd;2216}2217return@list;2218}22192220our$gitweb_project_owner=undef;2221sub git_get_project_list_from_file {22222223return if(defined$gitweb_project_owner);22242225$gitweb_project_owner= {};2226# read from file (url-encoded):2227# 'git%2Fgit.git Linus+Torvalds'2228# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2229# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2230if(-f $projects_list) {2231open(my$fd,$projects_list);2232while(my$line= <$fd>) {2233chomp$line;2234my($pr,$ow) =split' ',$line;2235$pr= unescape($pr);2236$ow= unescape($ow);2237$gitweb_project_owner->{$pr} = to_utf8($ow);2238}2239close$fd;2240}2241}22422243sub git_get_project_owner {2244my$project=shift;2245my$owner;22462247returnundefunless$project;2248$git_dir="$projectroot/$project";22492250if(!defined$gitweb_project_owner) {2251 git_get_project_list_from_file();2252}22532254if(exists$gitweb_project_owner->{$project}) {2255$owner=$gitweb_project_owner->{$project};2256}2257if(!defined$owner){2258$owner= git_get_project_config('owner');2259}2260if(!defined$owner) {2261$owner= get_file_owner("$git_dir");2262}22632264return$owner;2265}22662267sub git_get_last_activity {2268my($path) =@_;2269my$fd;22702271$git_dir="$projectroot/$path";2272open($fd,"-|", git_cmd(),'for-each-ref',2273'--format=%(committer)',2274'--sort=-committerdate',2275'--count=1',2276'refs/heads')orreturn;2277my$most_recent= <$fd>;2278close$fdorreturn;2279if(defined$most_recent&&2280$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2281my$timestamp=$1;2282my$age=time-$timestamp;2283return($age, age_string($age));2284}2285return(undef,undef);2286}22872288sub git_get_references {2289my$type=shift||"";2290my%refs;2291# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112292# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2293open my$fd,"-|", git_cmd(),"show-ref","--dereference",2294($type? ("--","refs/$type") : ())# use -- <pattern> if $type2295orreturn;22962297while(my$line= <$fd>) {2298chomp$line;2299if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2300if(defined$refs{$1}) {2301push@{$refs{$1}},$2;2302}else{2303$refs{$1} = [$2];2304}2305}2306}2307close$fdorreturn;2308return \%refs;2309}23102311sub git_get_rev_name_tags {2312my$hash=shift||returnundef;23132314open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2315orreturn;2316my$name_rev= <$fd>;2317close$fd;23182319if($name_rev=~ m|^$hash tags/(.*)$|) {2320return$1;2321}else{2322# catches also '$hash undefined' output2323returnundef;2324}2325}23262327## ----------------------------------------------------------------------2328## parse to hash functions23292330sub parse_date {2331my$epoch=shift;2332my$tz=shift||"-0000";23332334my%date;2335my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2336my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2337my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2338$date{'hour'} =$hour;2339$date{'minute'} =$min;2340$date{'mday'} =$mday;2341$date{'day'} =$days[$wday];2342$date{'month'} =$months[$mon];2343$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2344$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2345$date{'mday-time'} =sprintf"%d%s%02d:%02d",2346$mday,$months[$mon],$hour,$min;2347$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",23481900+$year,1+$mon,$mday,$hour,$min,$sec;23492350$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2351my$local=$epoch+ ((int$1+ ($2/60)) *3600);2352($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2353$date{'hour_local'} =$hour;2354$date{'minute_local'} =$min;2355$date{'tz_local'} =$tz;2356$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",23571900+$year,$mon+1,$mday,2358$hour,$min,$sec,$tz);2359return%date;2360}23612362sub parse_tag {2363my$tag_id=shift;2364my%tag;2365my@comment;23662367open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2368$tag{'id'} =$tag_id;2369while(my$line= <$fd>) {2370chomp$line;2371if($line=~m/^object ([0-9a-fA-F]{40})$/) {2372$tag{'object'} =$1;2373}elsif($line=~m/^type (.+)$/) {2374$tag{'type'} =$1;2375}elsif($line=~m/^tag (.+)$/) {2376$tag{'name'} =$1;2377}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2378$tag{'author'} =$1;2379$tag{'epoch'} =$2;2380$tag{'tz'} =$3;2381}elsif($line=~m/--BEGIN/) {2382push@comment,$line;2383last;2384}elsif($lineeq"") {2385last;2386}2387}2388push@comment, <$fd>;2389$tag{'comment'} = \@comment;2390close$fdorreturn;2391if(!defined$tag{'name'}) {2392return2393};2394return%tag2395}23962397sub parse_commit_text {2398my($commit_text,$withparents) =@_;2399my@commit_lines=split'\n',$commit_text;2400my%co;24012402pop@commit_lines;# Remove '\0'24032404if(!@commit_lines) {2405return;2406}24072408my$header=shift@commit_lines;2409if($header!~m/^[0-9a-fA-F]{40}/) {2410return;2411}2412($co{'id'},my@parents) =split' ',$header;2413while(my$line=shift@commit_lines) {2414last if$lineeq"\n";2415if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2416$co{'tree'} =$1;2417}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2418push@parents,$1;2419}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2420$co{'author'} =$1;2421$co{'author_epoch'} =$2;2422$co{'author_tz'} =$3;2423if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2424$co{'author_name'} =$1;2425$co{'author_email'} =$2;2426}else{2427$co{'author_name'} =$co{'author'};2428}2429}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2430$co{'committer'} =$1;2431$co{'committer_epoch'} =$2;2432$co{'committer_tz'} =$3;2433$co{'committer_name'} =$co{'committer'};2434if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2435$co{'committer_name'} =$1;2436$co{'committer_email'} =$2;2437}else{2438$co{'committer_name'} =$co{'committer'};2439}2440}2441}2442if(!defined$co{'tree'}) {2443return;2444};2445$co{'parents'} = \@parents;2446$co{'parent'} =$parents[0];24472448foreachmy$title(@commit_lines) {2449$title=~s/^ //;2450if($titlene"") {2451$co{'title'} = chop_str($title,80,5);2452# remove leading stuff of merges to make the interesting part visible2453if(length($title) >50) {2454$title=~s/^Automatic //;2455$title=~s/^merge (of|with) /Merge ... /i;2456if(length($title) >50) {2457$title=~s/(http|rsync):\/\///;2458}2459if(length($title) >50) {2460$title=~s/(master|www|rsync)\.//;2461}2462if(length($title) >50) {2463$title=~s/kernel.org:?//;2464}2465if(length($title) >50) {2466$title=~s/\/pub\/scm//;2467}2468}2469$co{'title_short'} = chop_str($title,50,5);2470last;2471}2472}2473if(!defined$co{'title'} ||$co{'title'}eq"") {2474$co{'title'} =$co{'title_short'} ='(no commit message)';2475}2476# remove added spaces2477foreachmy$line(@commit_lines) {2478$line=~s/^ //;2479}2480$co{'comment'} = \@commit_lines;24812482my$age=time-$co{'committer_epoch'};2483$co{'age'} =$age;2484$co{'age_string'} = age_string($age);2485my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2486if($age>60*60*24*7*2) {2487$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2488$co{'age_string_age'} =$co{'age_string'};2489}else{2490$co{'age_string_date'} =$co{'age_string'};2491$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2492}2493return%co;2494}24952496sub parse_commit {2497my($commit_id) =@_;2498my%co;24992500local$/="\0";25012502open my$fd,"-|", git_cmd(),"rev-list",2503"--parents",2504"--header",2505"--max-count=1",2506$commit_id,2507"--",2508or die_error(500,"Open git-rev-list failed");2509%co= parse_commit_text(<$fd>,1);2510close$fd;25112512return%co;2513}25142515sub parse_commits {2516my($commit_id,$maxcount,$skip,$filename,@args) =@_;2517my@cos;25182519$maxcount||=1;2520$skip||=0;25212522local$/="\0";25232524open my$fd,"-|", git_cmd(),"rev-list",2525"--header",2526@args,2527("--max-count=".$maxcount),2528("--skip=".$skip),2529@extra_options,2530$commit_id,2531"--",2532($filename? ($filename) : ())2533or die_error(500,"Open git-rev-list failed");2534while(my$line= <$fd>) {2535my%co= parse_commit_text($line);2536push@cos, \%co;2537}2538close$fd;25392540returnwantarray?@cos: \@cos;2541}25422543# parse line of git-diff-tree "raw" output2544sub parse_difftree_raw_line {2545my$line=shift;2546my%res;25472548# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2549# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2550if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2551$res{'from_mode'} =$1;2552$res{'to_mode'} =$2;2553$res{'from_id'} =$3;2554$res{'to_id'} =$4;2555$res{'status'} =$5;2556$res{'similarity'} =$6;2557if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2558($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2559}else{2560$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2561}2562}2563# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2564# combined diff (for merge commit)2565elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2566$res{'nparents'} =length($1);2567$res{'from_mode'} = [split(' ',$2) ];2568$res{'to_mode'} =pop@{$res{'from_mode'}};2569$res{'from_id'} = [split(' ',$3) ];2570$res{'to_id'} =pop@{$res{'from_id'}};2571$res{'status'} = [split('',$4) ];2572$res{'to_file'} = unquote($5);2573}2574# 'c512b523472485aef4fff9e57b229d9d243c967f'2575elsif($line=~m/^([0-9a-fA-F]{40})$/) {2576$res{'commit'} =$1;2577}25782579returnwantarray?%res: \%res;2580}25812582# wrapper: return parsed line of git-diff-tree "raw" output2583# (the argument might be raw line, or parsed info)2584sub parsed_difftree_line {2585my$line_or_ref=shift;25862587if(ref($line_or_ref)eq"HASH") {2588# pre-parsed (or generated by hand)2589return$line_or_ref;2590}else{2591return parse_difftree_raw_line($line_or_ref);2592}2593}25942595# parse line of git-ls-tree output2596sub parse_ls_tree_line ($;%) {2597my$line=shift;2598my%opts=@_;2599my%res;26002601#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2602$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;26032604$res{'mode'} =$1;2605$res{'type'} =$2;2606$res{'hash'} =$3;2607if($opts{'-z'}) {2608$res{'name'} =$4;2609}else{2610$res{'name'} = unquote($4);2611}26122613returnwantarray?%res: \%res;2614}26152616# generates _two_ hashes, references to which are passed as 2 and 3 argument2617sub parse_from_to_diffinfo {2618my($diffinfo,$from,$to,@parents) =@_;26192620if($diffinfo->{'nparents'}) {2621# combined diff2622$from->{'file'} = [];2623$from->{'href'} = [];2624 fill_from_file_info($diffinfo,@parents)2625unlessexists$diffinfo->{'from_file'};2626for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2627$from->{'file'}[$i] =2628defined$diffinfo->{'from_file'}[$i] ?2629$diffinfo->{'from_file'}[$i] :2630$diffinfo->{'to_file'};2631if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2632$from->{'href'}[$i] = href(action=>"blob",2633 hash_base=>$parents[$i],2634 hash=>$diffinfo->{'from_id'}[$i],2635 file_name=>$from->{'file'}[$i]);2636}else{2637$from->{'href'}[$i] =undef;2638}2639}2640}else{2641# ordinary (not combined) diff2642$from->{'file'} =$diffinfo->{'from_file'};2643if($diffinfo->{'status'}ne"A") {# not new (added) file2644$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2645 hash=>$diffinfo->{'from_id'},2646 file_name=>$from->{'file'});2647}else{2648delete$from->{'href'};2649}2650}26512652$to->{'file'} =$diffinfo->{'to_file'};2653if(!is_deleted($diffinfo)) {# file exists in result2654$to->{'href'} = href(action=>"blob", hash_base=>$hash,2655 hash=>$diffinfo->{'to_id'},2656 file_name=>$to->{'file'});2657}else{2658delete$to->{'href'};2659}2660}26612662## ......................................................................2663## parse to array of hashes functions26642665sub git_get_heads_list {2666my$limit=shift;2667my@headslist;26682669open my$fd,'-|', git_cmd(),'for-each-ref',2670($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2671'--format=%(objectname) %(refname) %(subject)%00%(committer)',2672'refs/heads'2673orreturn;2674while(my$line= <$fd>) {2675my%ref_item;26762677chomp$line;2678my($refinfo,$committerinfo) =split(/\0/,$line);2679my($hash,$name,$title) =split(' ',$refinfo,3);2680my($committer,$epoch,$tz) =2681($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2682$ref_item{'fullname'} =$name;2683$name=~s!^refs/heads/!!;26842685$ref_item{'name'} =$name;2686$ref_item{'id'} =$hash;2687$ref_item{'title'} =$title||'(no commit message)';2688$ref_item{'epoch'} =$epoch;2689if($epoch) {2690$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2691}else{2692$ref_item{'age'} ="unknown";2693}26942695push@headslist, \%ref_item;2696}2697close$fd;26982699returnwantarray?@headslist: \@headslist;2700}27012702sub git_get_tags_list {2703my$limit=shift;2704my@tagslist;27052706open my$fd,'-|', git_cmd(),'for-each-ref',2707($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2708'--format=%(objectname) %(objecttype) %(refname) '.2709'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2710'refs/tags'2711orreturn;2712while(my$line= <$fd>) {2713my%ref_item;27142715chomp$line;2716my($refinfo,$creatorinfo) =split(/\0/,$line);2717my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2718my($creator,$epoch,$tz) =2719($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2720$ref_item{'fullname'} =$name;2721$name=~s!^refs/tags/!!;27222723$ref_item{'type'} =$type;2724$ref_item{'id'} =$id;2725$ref_item{'name'} =$name;2726if($typeeq"tag") {2727$ref_item{'subject'} =$title;2728$ref_item{'reftype'} =$reftype;2729$ref_item{'refid'} =$refid;2730}else{2731$ref_item{'reftype'} =$type;2732$ref_item{'refid'} =$id;2733}27342735if($typeeq"tag"||$typeeq"commit") {2736$ref_item{'epoch'} =$epoch;2737if($epoch) {2738$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2739}else{2740$ref_item{'age'} ="unknown";2741}2742}27432744push@tagslist, \%ref_item;2745}2746close$fd;27472748returnwantarray?@tagslist: \@tagslist;2749}27502751## ----------------------------------------------------------------------2752## filesystem-related functions27532754sub get_file_owner {2755my$path=shift;27562757my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2758my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2759if(!defined$gcos) {2760returnundef;2761}2762my$owner=$gcos;2763$owner=~s/[,;].*$//;2764return to_utf8($owner);2765}27662767# assume that file exists2768sub insert_file {2769my$filename=shift;27702771open my$fd,'<',$filename;2772print map{ to_utf8($_) } <$fd>;2773close$fd;2774}27752776## ......................................................................2777## mimetype related functions27782779sub mimetype_guess_file {2780my$filename=shift;2781my$mimemap=shift;2782-r $mimemaporreturnundef;27832784my%mimemap;2785open(MIME,$mimemap)orreturnundef;2786while(<MIME>) {2787next ifm/^#/;# skip comments2788my($mime,$exts) =split(/\t+/);2789if(defined$exts) {2790my@exts=split(/\s+/,$exts);2791foreachmy$ext(@exts) {2792$mimemap{$ext} =$mime;2793}2794}2795}2796close(MIME);27972798$filename=~/\.([^.]*)$/;2799return$mimemap{$1};2800}28012802sub mimetype_guess {2803my$filename=shift;2804my$mime;2805$filename=~/\./orreturnundef;28062807if($mimetypes_file) {2808my$file=$mimetypes_file;2809if($file!~m!^/!) {# if it is relative path2810# it is relative to project2811$file="$projectroot/$project/$file";2812}2813$mime= mimetype_guess_file($filename,$file);2814}2815$mime||= mimetype_guess_file($filename,'/etc/mime.types');2816return$mime;2817}28182819sub blob_mimetype {2820my$fd=shift;2821my$filename=shift;28222823if($filename) {2824my$mime= mimetype_guess($filename);2825$mimeandreturn$mime;2826}28272828# just in case2829return$default_blob_plain_mimetypeunless$fd;28302831if(-T $fd) {2832return'text/plain';2833}elsif(!$filename) {2834return'application/octet-stream';2835}elsif($filename=~m/\.png$/i) {2836return'image/png';2837}elsif($filename=~m/\.gif$/i) {2838return'image/gif';2839}elsif($filename=~m/\.jpe?g$/i) {2840return'image/jpeg';2841}else{2842return'application/octet-stream';2843}2844}28452846sub blob_contenttype {2847my($fd,$file_name,$type) =@_;28482849$type||= blob_mimetype($fd,$file_name);2850if($typeeq'text/plain'&&defined$default_text_plain_charset) {2851$type.="; charset=$default_text_plain_charset";2852}28532854return$type;2855}28562857## ======================================================================2858## functions printing HTML: header, footer, error page28592860sub git_header_html {2861my$status=shift||"200 OK";2862my$expires=shift;28632864my$title="$site_name";2865if(defined$project) {2866$title.=" - ". to_utf8($project);2867if(defined$action) {2868$title.="/$action";2869if(defined$file_name) {2870$title.=" - ". esc_path($file_name);2871if($actioneq"tree"&&$file_name!~ m|/$|) {2872$title.="/";2873}2874}2875}2876}2877my$content_type;2878# require explicit support from the UA if we are to send the page as2879# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2880# we have to do this because MSIE sometimes globs '*/*', pretending to2881# support xhtml+xml but choking when it gets what it asked for.2882if(defined$cgi->http('HTTP_ACCEPT') &&2883$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2884$cgi->Accept('application/xhtml+xml') !=0) {2885$content_type='application/xhtml+xml';2886}else{2887$content_type='text/html';2888}2889print$cgi->header(-type=>$content_type, -charset =>'utf-8',2890-status=>$status, -expires =>$expires);2891my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2892print<<EOF;2893<?xml version="1.0" encoding="utf-8"?>2894<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2895<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2896<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2897<!-- git core binaries version$git_version-->2898<head>2899<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2900<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2901<meta name="robots" content="index, nofollow"/>2902<title>$title</title>2903EOF2904# the stylesheet, favicon etc urls won't work correctly with path_info2905# unless we set the appropriate base URL2906if($ENV{'PATH_INFO'}) {2907print'<base href="'.esc_url($my_url).'" />\n';2908}2909# print out each stylesheet that exist, providing backwards capability2910# for those people who defined $stylesheet in a config file2911if(defined$stylesheet) {2912print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2913}else{2914foreachmy$stylesheet(@stylesheets) {2915next unless$stylesheet;2916print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2917}2918}2919if(defined$project) {2920my%href_params= get_feed_info();2921if(!exists$href_params{'-title'}) {2922$href_params{'-title'} ='log';2923}29242925foreachmy$formatqw(RSS Atom){2926my$type=lc($format);2927my%link_attr= (2928'-rel'=>'alternate',2929'-title'=>"$project-$href_params{'-title'} -$formatfeed",2930'-type'=>"application/$type+xml"2931);29322933$href_params{'action'} =$type;2934$link_attr{'-href'} = href(%href_params);2935print"<link ".2936"rel=\"$link_attr{'-rel'}\"".2937"title=\"$link_attr{'-title'}\"".2938"href=\"$link_attr{'-href'}\"".2939"type=\"$link_attr{'-type'}\"".2940"/>\n";29412942$href_params{'extra_options'} ='--no-merges';2943$link_attr{'-href'} = href(%href_params);2944$link_attr{'-title'} .=' (no merges)';2945print"<link ".2946"rel=\"$link_attr{'-rel'}\"".2947"title=\"$link_attr{'-title'}\"".2948"href=\"$link_attr{'-href'}\"".2949"type=\"$link_attr{'-type'}\"".2950"/>\n";2951}29522953}else{2954printf('<link rel="alternate" title="%sprojects list" '.2955'href="%s" type="text/plain; charset=utf-8" />'."\n",2956$site_name, href(project=>undef, action=>"project_index"));2957printf('<link rel="alternate" title="%sprojects feeds" '.2958'href="%s" type="text/x-opml" />'."\n",2959$site_name, href(project=>undef, action=>"opml"));2960}2961if(defined$favicon) {2962printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2963}29642965print"</head>\n".2966"<body>\n";29672968if(-f $site_header) {2969 insert_file($site_header);2970}29712972print"<div class=\"page_header\">\n".2973$cgi->a({-href => esc_url($logo_url),2974-title =>$logo_label},2975qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2976print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2977if(defined$project) {2978print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2979if(defined$action) {2980print" /$action";2981}2982print"\n";2983}2984print"</div>\n";29852986my$have_search= gitweb_check_feature('search');2987if(defined$project&&$have_search) {2988if(!defined$searchtext) {2989$searchtext="";2990}2991my$search_hash;2992if(defined$hash_base) {2993$search_hash=$hash_base;2994}elsif(defined$hash) {2995$search_hash=$hash;2996}else{2997$search_hash="HEAD";2998}2999my$action=$my_uri;3000my$use_pathinfo= gitweb_check_feature('pathinfo');3001if($use_pathinfo) {3002$action.="/".esc_url($project);3003}3004print$cgi->startform(-method=>"get", -action =>$action) .3005"<div class=\"search\">\n".3006(!$use_pathinfo&&3007$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3008$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3009$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3010$cgi->popup_menu(-name =>'st', -default=>'commit',3011-values=> ['commit','grep','author','committer','pickaxe']) .3012$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3013" search:\n",3014$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3015"<span title=\"Extended regular expression\">".3016$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3017-checked =>$search_use_regexp) .3018"</span>".3019"</div>".3020$cgi->end_form() ."\n";3021}3022}30233024sub git_footer_html {3025my$feed_class='rss_logo';30263027print"<div class=\"page_footer\">\n";3028if(defined$project) {3029my$descr= git_get_project_description($project);3030if(defined$descr) {3031print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3032}30333034my%href_params= get_feed_info();3035if(!%href_params) {3036$feed_class.=' generic';3037}3038$href_params{'-title'} ||='log';30393040foreachmy$formatqw(RSS Atom){3041$href_params{'action'} =lc($format);3042print$cgi->a({-href => href(%href_params),3043-title =>"$href_params{'-title'}$formatfeed",3044-class=>$feed_class},$format)."\n";3045}30463047}else{3048print$cgi->a({-href => href(project=>undef, action=>"opml"),3049-class=>$feed_class},"OPML") ." ";3050print$cgi->a({-href => href(project=>undef, action=>"project_index"),3051-class=>$feed_class},"TXT") ."\n";3052}3053print"</div>\n";# class="page_footer"30543055if(-f $site_footer) {3056 insert_file($site_footer);3057}30583059print"</body>\n".3060"</html>";3061}30623063# die_error(<http_status_code>, <error_message>)3064# Example: die_error(404, 'Hash not found')3065# By convention, use the following status codes (as defined in RFC 2616):3066# 400: Invalid or missing CGI parameters, or3067# requested object exists but has wrong type.3068# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3069# this server or project.3070# 404: Requested object/revision/project doesn't exist.3071# 500: The server isn't configured properly, or3072# an internal error occurred (e.g. failed assertions caused by bugs), or3073# an unknown error occurred (e.g. the git binary died unexpectedly).3074sub die_error {3075my$status=shift||500;3076my$error=shift||"Internal server error";30773078my%http_responses= (400=>'400 Bad Request',3079403=>'403 Forbidden',3080404=>'404 Not Found',3081500=>'500 Internal Server Error');3082 git_header_html($http_responses{$status});3083print<<EOF;3084<div class="page_body">3085<br /><br />3086$status-$error3087<br />3088</div>3089EOF3090 git_footer_html();3091exit;3092}30933094## ----------------------------------------------------------------------3095## functions printing or outputting HTML: navigation30963097sub git_print_page_nav {3098my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3099$extra=''if!defined$extra;# pager or formats31003101my@navs=qw(summary shortlog log commit commitdiff tree);3102if($suppress) {3103@navs=grep{$_ne$suppress}@navs;3104}31053106my%arg=map{$_=> {action=>$_} }@navs;3107if(defined$head) {3108for(qw(commit commitdiff)) {3109$arg{$_}{'hash'} =$head;3110}3111if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3112for(qw(shortlog log)) {3113$arg{$_}{'hash'} =$head;3114}3115}3116}31173118$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3119$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;31203121my@actions= gitweb_get_feature('actions');3122my%repl= (3123'%'=>'%',3124'n'=>$project,# project name3125'f'=>$git_dir,# project path within filesystem3126'h'=>$treehead||'',# current hash ('h' parameter)3127'b'=>$treebase||'',# hash base ('hb' parameter)3128);3129while(@actions) {3130my($label,$link,$pos) =splice(@actions,0,3);3131# insert3132@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3133# munch munch3134$link=~s/%([%nfhb])/$repl{$1}/g;3135$arg{$label}{'_href'} =$link;3136}31373138print"<div class=\"page_nav\">\n".3139(join" | ",3140map{$_eq$current?3141$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3142}@navs);3143print"<br/>\n$extra<br/>\n".3144"</div>\n";3145}31463147sub format_paging_nav {3148my($action,$hash,$head,$page,$has_next_link) =@_;3149my$paging_nav;315031513152if($hashne$head||$page) {3153$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3154}else{3155$paging_nav.="HEAD";3156}31573158if($page>0) {3159$paging_nav.=" ⋅ ".3160$cgi->a({-href => href(-replay=>1, page=>$page-1),3161-accesskey =>"p", -title =>"Alt-p"},"prev");3162}else{3163$paging_nav.=" ⋅ prev";3164}31653166if($has_next_link) {3167$paging_nav.=" ⋅ ".3168$cgi->a({-href => href(-replay=>1, page=>$page+1),3169-accesskey =>"n", -title =>"Alt-n"},"next");3170}else{3171$paging_nav.=" ⋅ next";3172}31733174return$paging_nav;3175}31763177## ......................................................................3178## functions printing or outputting HTML: div31793180sub git_print_header_div {3181my($action,$title,$hash,$hash_base) =@_;3182my%args= ();31833184$args{'action'} =$action;3185$args{'hash'} =$hashif$hash;3186$args{'hash_base'} =$hash_baseif$hash_base;31873188print"<div class=\"header\">\n".3189$cgi->a({-href => href(%args), -class=>"title"},3190$title?$title:$action) .3191"\n</div>\n";3192}31933194#sub git_print_authorship (\%) {3195sub git_print_authorship {3196my$co=shift;31973198my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3199print"<div class=\"author_date\">".3200 esc_html($co->{'author_name'}) .3201" [$ad{'rfc2822'}";3202if($ad{'hour_local'} <6) {3203printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3204$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3205}else{3206printf(" (%02d:%02d%s)",3207$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3208}3209print"]</div>\n";3210}32113212sub git_print_page_path {3213my$name=shift;3214my$type=shift;3215my$hb=shift;321632173218print"<div class=\"page_path\">";3219print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3220-title =>'tree root'}, to_utf8("[$project]"));3221print" / ";3222if(defined$name) {3223my@dirname=split'/',$name;3224my$basename=pop@dirname;3225my$fullname='';32263227foreachmy$dir(@dirname) {3228$fullname.= ($fullname?'/':'') .$dir;3229print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3230 hash_base=>$hb),3231-title =>$fullname}, esc_path($dir));3232print" / ";3233}3234if(defined$type&&$typeeq'blob') {3235print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3236 hash_base=>$hb),3237-title =>$name}, esc_path($basename));3238}elsif(defined$type&&$typeeq'tree') {3239print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3240 hash_base=>$hb),3241-title =>$name}, esc_path($basename));3242print" / ";3243}else{3244print esc_path($basename);3245}3246}3247print"<br/></div>\n";3248}32493250# sub git_print_log (\@;%) {3251sub git_print_log ($;%) {3252my$log=shift;3253my%opts=@_;32543255if($opts{'-remove_title'}) {3256# remove title, i.e. first line of log3257shift@$log;3258}3259# remove leading empty lines3260while(defined$log->[0] &&$log->[0]eq"") {3261shift@$log;3262}32633264# print log3265my$signoff=0;3266my$empty=0;3267foreachmy$line(@$log) {3268if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3269$signoff=1;3270$empty=0;3271if(!$opts{'-remove_signoff'}) {3272print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3273next;3274}else{3275# remove signoff lines3276next;3277}3278}else{3279$signoff=0;3280}32813282# print only one empty line3283# do not print empty line after signoff3284if($lineeq"") {3285next if($empty||$signoff);3286$empty=1;3287}else{3288$empty=0;3289}32903291print format_log_line_html($line) ."<br/>\n";3292}32933294if($opts{'-final_empty_line'}) {3295# end with single empty line3296print"<br/>\n"unless$empty;3297}3298}32993300# return link target (what link points to)3301sub git_get_link_target {3302my$hash=shift;3303my$link_target;33043305# read link3306open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3307orreturn;3308{3309local$/;3310$link_target= <$fd>;3311}3312close$fd3313orreturn;33143315return$link_target;3316}33173318# given link target, and the directory (basedir) the link is in,3319# return target of link relative to top directory (top tree);3320# return undef if it is not possible (including absolute links).3321sub normalize_link_target {3322my($link_target,$basedir,$hash_base) =@_;33233324# we can normalize symlink target only if $hash_base is provided3325return unless$hash_base;33263327# absolute symlinks (beginning with '/') cannot be normalized3328return if(substr($link_target,0,1)eq'/');33293330# normalize link target to path from top (root) tree (dir)3331my$path;3332if($basedir) {3333$path=$basedir.'/'.$link_target;3334}else{3335# we are in top (root) tree (dir)3336$path=$link_target;3337}33383339# remove //, /./, and /../3340my@path_parts;3341foreachmy$part(split('/',$path)) {3342# discard '.' and ''3343next if(!$part||$parteq'.');3344# handle '..'3345if($parteq'..') {3346if(@path_parts) {3347pop@path_parts;3348}else{3349# link leads outside repository (outside top dir)3350return;3351}3352}else{3353push@path_parts,$part;3354}3355}3356$path=join('/',@path_parts);33573358return$path;3359}33603361# print tree entry (row of git_tree), but without encompassing <tr> element3362sub git_print_tree_entry {3363my($t,$basedir,$hash_base,$have_blame) =@_;33643365my%base_key= ();3366$base_key{'hash_base'} =$hash_baseifdefined$hash_base;33673368# The format of a table row is: mode list link. Where mode is3369# the mode of the entry, list is the name of the entry, an href,3370# and link is the action links of the entry.33713372print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3373if($t->{'type'}eq"blob") {3374print"<td class=\"list\">".3375$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3376 file_name=>"$basedir$t->{'name'}",%base_key),3377-class=>"list"}, esc_path($t->{'name'}));3378if(S_ISLNK(oct$t->{'mode'})) {3379my$link_target= git_get_link_target($t->{'hash'});3380if($link_target) {3381my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3382if(defined$norm_target) {3383print" -> ".3384$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3385 file_name=>$norm_target),3386-title =>$norm_target}, esc_path($link_target));3387}else{3388print" -> ". esc_path($link_target);3389}3390}3391}3392print"</td>\n";3393print"<td class=\"link\">";3394print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3395 file_name=>"$basedir$t->{'name'}",%base_key)},3396"blob");3397if($have_blame) {3398print" | ".3399$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3400 file_name=>"$basedir$t->{'name'}",%base_key)},3401"blame");3402}3403if(defined$hash_base) {3404print" | ".3405$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3406 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3407"history");3408}3409print" | ".3410$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3411 file_name=>"$basedir$t->{'name'}")},3412"raw");3413print"</td>\n";34143415}elsif($t->{'type'}eq"tree") {3416print"<td class=\"list\">";3417print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3418 file_name=>"$basedir$t->{'name'}",%base_key)},3419 esc_path($t->{'name'}));3420print"</td>\n";3421print"<td class=\"link\">";3422print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3423 file_name=>"$basedir$t->{'name'}",%base_key)},3424"tree");3425if(defined$hash_base) {3426print" | ".3427$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3428 file_name=>"$basedir$t->{'name'}")},3429"history");3430}3431print"</td>\n";3432}else{3433# unknown object: we can only present history for it3434# (this includes 'commit' object, i.e. submodule support)3435print"<td class=\"list\">".3436 esc_path($t->{'name'}) .3437"</td>\n";3438print"<td class=\"link\">";3439if(defined$hash_base) {3440print$cgi->a({-href => href(action=>"history",3441 hash_base=>$hash_base,3442 file_name=>"$basedir$t->{'name'}")},3443"history");3444}3445print"</td>\n";3446}3447}34483449## ......................................................................3450## functions printing large fragments of HTML34513452# get pre-image filenames for merge (combined) diff3453sub fill_from_file_info {3454my($diff,@parents) =@_;34553456$diff->{'from_file'} = [ ];3457$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3458for(my$i=0;$i<$diff->{'nparents'};$i++) {3459if($diff->{'status'}[$i]eq'R'||3460$diff->{'status'}[$i]eq'C') {3461$diff->{'from_file'}[$i] =3462 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3463}3464}34653466return$diff;3467}34683469# is current raw difftree line of file deletion3470sub is_deleted {3471my$diffinfo=shift;34723473return$diffinfo->{'to_id'}eq('0' x 40);3474}34753476# does patch correspond to [previous] difftree raw line3477# $diffinfo - hashref of parsed raw diff format3478# $patchinfo - hashref of parsed patch diff format3479# (the same keys as in $diffinfo)3480sub is_patch_split {3481my($diffinfo,$patchinfo) =@_;34823483returndefined$diffinfo&&defined$patchinfo3484&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3485}348634873488sub git_difftree_body {3489my($difftree,$hash,@parents) =@_;3490my($parent) =$parents[0];3491my$have_blame= gitweb_check_feature('blame');3492print"<div class=\"list_head\">\n";3493if($#{$difftree} >10) {3494print(($#{$difftree} +1) ." files changed:\n");3495}3496print"</div>\n";34973498print"<table class=\"".3499(@parents>1?"combined ":"") .3500"diff_tree\">\n";35013502# header only for combined diff in 'commitdiff' view3503my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3504if($has_header) {3505# table header3506print"<thead><tr>\n".3507"<th></th><th></th>\n";# filename, patchN link3508for(my$i=0;$i<@parents;$i++) {3509my$par=$parents[$i];3510print"<th>".3511$cgi->a({-href => href(action=>"commitdiff",3512 hash=>$hash, hash_parent=>$par),3513-title =>'commitdiff to parent number '.3514($i+1) .': '.substr($par,0,7)},3515$i+1) .3516" </th>\n";3517}3518print"</tr></thead>\n<tbody>\n";3519}35203521my$alternate=1;3522my$patchno=0;3523foreachmy$line(@{$difftree}) {3524my$diff= parsed_difftree_line($line);35253526if($alternate) {3527print"<tr class=\"dark\">\n";3528}else{3529print"<tr class=\"light\">\n";3530}3531$alternate^=1;35323533if(exists$diff->{'nparents'}) {# combined diff35343535 fill_from_file_info($diff,@parents)3536unlessexists$diff->{'from_file'};35373538if(!is_deleted($diff)) {3539# file exists in the result (child) commit3540print"<td>".3541$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3542 file_name=>$diff->{'to_file'},3543 hash_base=>$hash),3544-class=>"list"}, esc_path($diff->{'to_file'})) .3545"</td>\n";3546}else{3547print"<td>".3548 esc_path($diff->{'to_file'}) .3549"</td>\n";3550}35513552if($actioneq'commitdiff') {3553# link to patch3554$patchno++;3555print"<td class=\"link\">".3556$cgi->a({-href =>"#patch$patchno"},"patch") .3557" | ".3558"</td>\n";3559}35603561my$has_history=0;3562my$not_deleted=0;3563for(my$i=0;$i<$diff->{'nparents'};$i++) {3564my$hash_parent=$parents[$i];3565my$from_hash=$diff->{'from_id'}[$i];3566my$from_path=$diff->{'from_file'}[$i];3567my$status=$diff->{'status'}[$i];35683569$has_history||= ($statusne'A');3570$not_deleted||= ($statusne'D');35713572if($statuseq'A') {3573print"<td class=\"link\"align=\"right\"> | </td>\n";3574}elsif($statuseq'D') {3575print"<td class=\"link\">".3576$cgi->a({-href => href(action=>"blob",3577 hash_base=>$hash,3578 hash=>$from_hash,3579 file_name=>$from_path)},3580"blob". ($i+1)) .3581" | </td>\n";3582}else{3583if($diff->{'to_id'}eq$from_hash) {3584print"<td class=\"link nochange\">";3585}else{3586print"<td class=\"link\">";3587}3588print$cgi->a({-href => href(action=>"blobdiff",3589 hash=>$diff->{'to_id'},3590 hash_parent=>$from_hash,3591 hash_base=>$hash,3592 hash_parent_base=>$hash_parent,3593 file_name=>$diff->{'to_file'},3594 file_parent=>$from_path)},3595"diff". ($i+1)) .3596" | </td>\n";3597}3598}35993600print"<td class=\"link\">";3601if($not_deleted) {3602print$cgi->a({-href => href(action=>"blob",3603 hash=>$diff->{'to_id'},3604 file_name=>$diff->{'to_file'},3605 hash_base=>$hash)},3606"blob");3607print" | "if($has_history);3608}3609if($has_history) {3610print$cgi->a({-href => href(action=>"history",3611 file_name=>$diff->{'to_file'},3612 hash_base=>$hash)},3613"history");3614}3615print"</td>\n";36163617print"</tr>\n";3618next;# instead of 'else' clause, to avoid extra indent3619}3620# else ordinary diff36213622my($to_mode_oct,$to_mode_str,$to_file_type);3623my($from_mode_oct,$from_mode_str,$from_file_type);3624if($diff->{'to_mode'}ne('0' x 6)) {3625$to_mode_oct=oct$diff->{'to_mode'};3626if(S_ISREG($to_mode_oct)) {# only for regular file3627$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3628}3629$to_file_type= file_type($diff->{'to_mode'});3630}3631if($diff->{'from_mode'}ne('0' x 6)) {3632$from_mode_oct=oct$diff->{'from_mode'};3633if(S_ISREG($to_mode_oct)) {# only for regular file3634$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3635}3636$from_file_type= file_type($diff->{'from_mode'});3637}36383639if($diff->{'status'}eq"A") {# created3640my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3641$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3642$mode_chng.="]</span>";3643print"<td>";3644print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3645 hash_base=>$hash, file_name=>$diff->{'file'}),3646-class=>"list"}, esc_path($diff->{'file'}));3647print"</td>\n";3648print"<td>$mode_chng</td>\n";3649print"<td class=\"link\">";3650if($actioneq'commitdiff') {3651# link to patch3652$patchno++;3653print$cgi->a({-href =>"#patch$patchno"},"patch");3654print" | ";3655}3656print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3657 hash_base=>$hash, file_name=>$diff->{'file'})},3658"blob");3659print"</td>\n";36603661}elsif($diff->{'status'}eq"D") {# deleted3662my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3663print"<td>";3664print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3665 hash_base=>$parent, file_name=>$diff->{'file'}),3666-class=>"list"}, esc_path($diff->{'file'}));3667print"</td>\n";3668print"<td>$mode_chng</td>\n";3669print"<td class=\"link\">";3670if($actioneq'commitdiff') {3671# link to patch3672$patchno++;3673print$cgi->a({-href =>"#patch$patchno"},"patch");3674print" | ";3675}3676print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3677 hash_base=>$parent, file_name=>$diff->{'file'})},3678"blob") ." | ";3679if($have_blame) {3680print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3681 file_name=>$diff->{'file'})},3682"blame") ." | ";3683}3684print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3685 file_name=>$diff->{'file'})},3686"history");3687print"</td>\n";36883689}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3690my$mode_chnge="";3691if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3692$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3693if($from_file_typene$to_file_type) {3694$mode_chnge.=" from$from_file_typeto$to_file_type";3695}3696if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3697if($from_mode_str&&$to_mode_str) {3698$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3699}elsif($to_mode_str) {3700$mode_chnge.=" mode:$to_mode_str";3701}3702}3703$mode_chnge.="]</span>\n";3704}3705print"<td>";3706print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3707 hash_base=>$hash, file_name=>$diff->{'file'}),3708-class=>"list"}, esc_path($diff->{'file'}));3709print"</td>\n";3710print"<td>$mode_chnge</td>\n";3711print"<td class=\"link\">";3712if($actioneq'commitdiff') {3713# link to patch3714$patchno++;3715print$cgi->a({-href =>"#patch$patchno"},"patch") .3716" | ";3717}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3718# "commit" view and modified file (not onlu mode changed)3719print$cgi->a({-href => href(action=>"blobdiff",3720 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3721 hash_base=>$hash, hash_parent_base=>$parent,3722 file_name=>$diff->{'file'})},3723"diff") .3724" | ";3725}3726print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3727 hash_base=>$hash, file_name=>$diff->{'file'})},3728"blob") ." | ";3729if($have_blame) {3730print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3731 file_name=>$diff->{'file'})},3732"blame") ." | ";3733}3734print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3735 file_name=>$diff->{'file'})},3736"history");3737print"</td>\n";37383739}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3740my%status_name= ('R'=>'moved','C'=>'copied');3741my$nstatus=$status_name{$diff->{'status'}};3742my$mode_chng="";3743if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3744# mode also for directories, so we cannot use $to_mode_str3745$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3746}3747print"<td>".3748$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3749 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3750-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3751"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3752$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3753 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3754-class=>"list"}, esc_path($diff->{'from_file'})) .3755" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3756"<td class=\"link\">";3757if($actioneq'commitdiff') {3758# link to patch3759$patchno++;3760print$cgi->a({-href =>"#patch$patchno"},"patch") .3761" | ";3762}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3763# "commit" view and modified file (not only pure rename or copy)3764print$cgi->a({-href => href(action=>"blobdiff",3765 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3766 hash_base=>$hash, hash_parent_base=>$parent,3767 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3768"diff") .3769" | ";3770}3771print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3772 hash_base=>$parent, file_name=>$diff->{'to_file'})},3773"blob") ." | ";3774if($have_blame) {3775print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3776 file_name=>$diff->{'to_file'})},3777"blame") ." | ";3778}3779print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3780 file_name=>$diff->{'to_file'})},3781"history");3782print"</td>\n";37833784}# we should not encounter Unmerged (U) or Unknown (X) status3785print"</tr>\n";3786}3787print"</tbody>"if$has_header;3788print"</table>\n";3789}37903791sub git_patchset_body {3792my($fd,$difftree,$hash,@hash_parents) =@_;3793my($hash_parent) =$hash_parents[0];37943795my$is_combined= (@hash_parents>1);3796my$patch_idx=0;3797my$patch_number=0;3798my$patch_line;3799my$diffinfo;3800my$to_name;3801my(%from,%to);38023803print"<div class=\"patchset\">\n";38043805# skip to first patch3806while($patch_line= <$fd>) {3807chomp$patch_line;38083809last if($patch_line=~m/^diff /);3810}38113812 PATCH:3813while($patch_line) {38143815# parse "git diff" header line3816if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3817# $1 is from_name, which we do not use3818$to_name= unquote($2);3819$to_name=~s!^b/!!;3820}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3821# $1 is 'cc' or 'combined', which we do not use3822$to_name= unquote($2);3823}else{3824$to_name=undef;3825}38263827# check if current patch belong to current raw line3828# and parse raw git-diff line if needed3829if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3830# this is continuation of a split patch3831print"<div class=\"patch cont\">\n";3832}else{3833# advance raw git-diff output if needed3834$patch_idx++ifdefined$diffinfo;38353836# read and prepare patch information3837$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38383839# compact combined diff output can have some patches skipped3840# find which patch (using pathname of result) we are at now;3841if($is_combined) {3842while($to_namene$diffinfo->{'to_file'}) {3843print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3844 format_diff_cc_simplified($diffinfo,@hash_parents) .3845"</div>\n";# class="patch"38463847$patch_idx++;3848$patch_number++;38493850last if$patch_idx>$#$difftree;3851$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3852}3853}38543855# modifies %from, %to hashes3856 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);38573858# this is first patch for raw difftree line with $patch_idx index3859# we index @$difftree array from 0, but number patches from 13860print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3861}38623863# git diff header3864#assert($patch_line =~ m/^diff /) if DEBUG;3865#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3866$patch_number++;3867# print "git diff" header3868print format_git_diff_header_line($patch_line,$diffinfo,3869 \%from, \%to);38703871# print extended diff header3872print"<div class=\"diff extended_header\">\n";3873 EXTENDED_HEADER:3874while($patch_line= <$fd>) {3875chomp$patch_line;38763877last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);38783879print format_extended_diff_header_line($patch_line,$diffinfo,3880 \%from, \%to);3881}3882print"</div>\n";# class="diff extended_header"38833884# from-file/to-file diff header3885if(!$patch_line) {3886print"</div>\n";# class="patch"3887last PATCH;3888}3889next PATCH if($patch_line=~m/^diff /);3890#assert($patch_line =~ m/^---/) if DEBUG;38913892my$last_patch_line=$patch_line;3893$patch_line= <$fd>;3894chomp$patch_line;3895#assert($patch_line =~ m/^\+\+\+/) if DEBUG;38963897print format_diff_from_to_header($last_patch_line,$patch_line,3898$diffinfo, \%from, \%to,3899@hash_parents);39003901# the patch itself3902 LINE:3903while($patch_line= <$fd>) {3904chomp$patch_line;39053906next PATCH if($patch_line=~m/^diff /);39073908print format_diff_line($patch_line, \%from, \%to);3909}39103911}continue{3912print"</div>\n";# class="patch"3913}39143915# for compact combined (--cc) format, with chunk and patch simpliciaction3916# patchset might be empty, but there might be unprocessed raw lines3917for(++$patch_idxif$patch_number>0;3918$patch_idx<@$difftree;3919++$patch_idx) {3920# read and prepare patch information3921$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);39223923# generate anchor for "patch" links in difftree / whatchanged part3924print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3925 format_diff_cc_simplified($diffinfo,@hash_parents) .3926"</div>\n";# class="patch"39273928$patch_number++;3929}39303931if($patch_number==0) {3932if(@hash_parents>1) {3933print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3934}else{3935print"<div class=\"diff nodifferences\">No differences found</div>\n";3936}3937}39383939print"</div>\n";# class="patchset"3940}39413942# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39433944# fills project list info (age, description, owner, forks) for each3945# project in the list, removing invalid projects from returned list3946# NOTE: modifies $projlist, but does not remove entries from it3947sub fill_project_list_info {3948my($projlist,$check_forks) =@_;3949my@projects;39503951my$show_ctags= gitweb_check_feature('ctags');3952 PROJECT:3953foreachmy$pr(@$projlist) {3954my(@activity) = git_get_last_activity($pr->{'path'});3955unless(@activity) {3956next PROJECT;3957}3958($pr->{'age'},$pr->{'age_string'}) =@activity;3959if(!defined$pr->{'descr'}) {3960my$descr= git_get_project_description($pr->{'path'}) ||"";3961$descr= to_utf8($descr);3962$pr->{'descr_long'} =$descr;3963$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3964}3965if(!defined$pr->{'owner'}) {3966$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3967}3968if($check_forks) {3969my$pname=$pr->{'path'};3970if(($pname=~s/\.git$//) &&3971($pname!~/\/$/) &&3972(-d "$projectroot/$pname")) {3973$pr->{'forks'} ="-d$projectroot/$pname";3974}else{3975$pr->{'forks'} =0;3976}3977}3978$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3979push@projects,$pr;3980}39813982return@projects;3983}39843985# print 'sort by' <th> element, generating 'sort by $name' replay link3986# if that order is not selected3987sub print_sort_th {3988my($name,$order,$header) =@_;3989$header||=ucfirst($name);39903991if($ordereq$name) {3992print"<th>$header</th>\n";3993}else{3994print"<th>".3995$cgi->a({-href => href(-replay=>1, order=>$name),3996-class=>"header"},$header) .3997"</th>\n";3998}3999}40004001sub git_project_list_body {4002# actually uses global variable $project4003my($projlist,$order,$from,$to,$extra,$no_header) =@_;40044005my$check_forks= gitweb_check_feature('forks');4006my@projects= fill_project_list_info($projlist,$check_forks);40074008$order||=$default_projects_order;4009$from=0unlessdefined$from;4010$to=$#projectsif(!defined$to||$#projects<$to);40114012my%order_info= (4013 project => { key =>'path', type =>'str'},4014 descr => { key =>'descr_long', type =>'str'},4015 owner => { key =>'owner', type =>'str'},4016 age => { key =>'age', type =>'num'}4017);4018my$oi=$order_info{$order};4019if($oi->{'type'}eq'str') {4020@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4021}else{4022@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4023}40244025my$show_ctags= gitweb_check_feature('ctags');4026if($show_ctags) {4027my%ctags;4028foreachmy$p(@projects) {4029foreachmy$ct(keys%{$p->{'ctags'}}) {4030$ctags{$ct} +=$p->{'ctags'}->{$ct};4031}4032}4033my$cloud= git_populate_project_tagcloud(\%ctags);4034print git_show_project_tagcloud($cloud,64);4035}40364037print"<table class=\"project_list\">\n";4038unless($no_header) {4039print"<tr>\n";4040if($check_forks) {4041print"<th></th>\n";4042}4043 print_sort_th('project',$order,'Project');4044 print_sort_th('descr',$order,'Description');4045 print_sort_th('owner',$order,'Owner');4046 print_sort_th('age',$order,'Last Change');4047print"<th></th>\n".# for links4048"</tr>\n";4049}4050my$alternate=1;4051my$tagfilter=$cgi->param('by_tag');4052for(my$i=$from;$i<=$to;$i++) {4053my$pr=$projects[$i];40544055next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4056next if$searchtextand not$pr->{'path'} =~/$searchtext/4057and not$pr->{'descr_long'} =~/$searchtext/;4058# Weed out forks or non-matching entries of search4059if($check_forks) {4060my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4061$forkbase="^$forkbase"if$forkbase;4062next ifnot$searchtextand not$tagfilterand$show_ctags4063and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4064}40654066if($alternate) {4067print"<tr class=\"dark\">\n";4068}else{4069print"<tr class=\"light\">\n";4070}4071$alternate^=1;4072if($check_forks) {4073print"<td>";4074if($pr->{'forks'}) {4075print"<!--$pr->{'forks'} -->\n";4076print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4077}4078print"</td>\n";4079}4080print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4081-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4082"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4083-class=>"list", -title =>$pr->{'descr_long'}},4084 esc_html($pr->{'descr'})) ."</td>\n".4085"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4086print"<td class=\"". age_class($pr->{'age'}) ."\">".4087(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4088"<td class=\"link\">".4089$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4090$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4091$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4092$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4093($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4094"</td>\n".4095"</tr>\n";4096}4097if(defined$extra) {4098print"<tr>\n";4099if($check_forks) {4100print"<td></td>\n";4101}4102print"<td colspan=\"5\">$extra</td>\n".4103"</tr>\n";4104}4105print"</table>\n";4106}41074108sub git_shortlog_body {4109# uses global variable $project4110my($commitlist,$from,$to,$refs,$extra) =@_;41114112$from=0unlessdefined$from;4113$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41144115print"<table class=\"shortlog\">\n";4116my$alternate=1;4117for(my$i=$from;$i<=$to;$i++) {4118my%co= %{$commitlist->[$i]};4119my$commit=$co{'id'};4120my$ref= format_ref_marker($refs,$commit);4121if($alternate) {4122print"<tr class=\"dark\">\n";4123}else{4124print"<tr class=\"light\">\n";4125}4126$alternate^=1;4127my$author= chop_and_escape_str($co{'author_name'},10);4128# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4129print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4130"<td><i>".$author."</i></td>\n".4131"<td>";4132print format_subject_html($co{'title'},$co{'title_short'},4133 href(action=>"commit", hash=>$commit),$ref);4134print"</td>\n".4135"<td class=\"link\">".4136$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4137$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4138$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4139my$snapshot_links= format_snapshot_links($commit);4140if(defined$snapshot_links) {4141print" | ".$snapshot_links;4142}4143print"</td>\n".4144"</tr>\n";4145}4146if(defined$extra) {4147print"<tr>\n".4148"<td colspan=\"4\">$extra</td>\n".4149"</tr>\n";4150}4151print"</table>\n";4152}41534154sub git_history_body {4155# Warning: assumes constant type (blob or tree) during history4156my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;41574158$from=0unlessdefined$from;4159$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});41604161print"<table class=\"history\">\n";4162my$alternate=1;4163for(my$i=$from;$i<=$to;$i++) {4164my%co= %{$commitlist->[$i]};4165if(!%co) {4166next;4167}4168my$commit=$co{'id'};41694170my$ref= format_ref_marker($refs,$commit);41714172if($alternate) {4173print"<tr class=\"dark\">\n";4174}else{4175print"<tr class=\"light\">\n";4176}4177$alternate^=1;4178# shortlog uses chop_str($co{'author_name'}, 10)4179my$author= chop_and_escape_str($co{'author_name'},15,3);4180print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4181"<td><i>".$author."</i></td>\n".4182"<td>";4183# originally git_history used chop_str($co{'title'}, 50)4184print format_subject_html($co{'title'},$co{'title_short'},4185 href(action=>"commit", hash=>$commit),$ref);4186print"</td>\n".4187"<td class=\"link\">".4188$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4189$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");41904191if($ftypeeq'blob') {4192my$blob_current= git_get_hash_by_path($hash_base,$file_name);4193my$blob_parent= git_get_hash_by_path($commit,$file_name);4194if(defined$blob_current&&defined$blob_parent&&4195$blob_currentne$blob_parent) {4196print" | ".4197$cgi->a({-href => href(action=>"blobdiff",4198 hash=>$blob_current, hash_parent=>$blob_parent,4199 hash_base=>$hash_base, hash_parent_base=>$commit,4200 file_name=>$file_name)},4201"diff to current");4202}4203}4204print"</td>\n".4205"</tr>\n";4206}4207if(defined$extra) {4208print"<tr>\n".4209"<td colspan=\"4\">$extra</td>\n".4210"</tr>\n";4211}4212print"</table>\n";4213}42144215sub git_tags_body {4216# uses global variable $project4217my($taglist,$from,$to,$extra) =@_;4218$from=0unlessdefined$from;4219$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);42204221print"<table class=\"tags\">\n";4222my$alternate=1;4223for(my$i=$from;$i<=$to;$i++) {4224my$entry=$taglist->[$i];4225my%tag=%$entry;4226my$comment=$tag{'subject'};4227my$comment_short;4228if(defined$comment) {4229$comment_short= chop_str($comment,30,5);4230}4231if($alternate) {4232print"<tr class=\"dark\">\n";4233}else{4234print"<tr class=\"light\">\n";4235}4236$alternate^=1;4237if(defined$tag{'age'}) {4238print"<td><i>$tag{'age'}</i></td>\n";4239}else{4240print"<td></td>\n";4241}4242print"<td>".4243$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4244-class=>"list name"}, esc_html($tag{'name'})) .4245"</td>\n".4246"<td>";4247if(defined$comment) {4248print format_subject_html($comment,$comment_short,4249 href(action=>"tag", hash=>$tag{'id'}));4250}4251print"</td>\n".4252"<td class=\"selflink\">";4253if($tag{'type'}eq"tag") {4254print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4255}else{4256print" ";4257}4258print"</td>\n".4259"<td class=\"link\">"." | ".4260$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4261if($tag{'reftype'}eq"commit") {4262print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4263" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4264}elsif($tag{'reftype'}eq"blob") {4265print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4266}4267print"</td>\n".4268"</tr>";4269}4270if(defined$extra) {4271print"<tr>\n".4272"<td colspan=\"5\">$extra</td>\n".4273"</tr>\n";4274}4275print"</table>\n";4276}42774278sub git_heads_body {4279# uses global variable $project4280my($headlist,$head,$from,$to,$extra) =@_;4281$from=0unlessdefined$from;4282$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);42834284print"<table class=\"heads\">\n";4285my$alternate=1;4286for(my$i=$from;$i<=$to;$i++) {4287my$entry=$headlist->[$i];4288my%ref=%$entry;4289my$curr=$ref{'id'}eq$head;4290if($alternate) {4291print"<tr class=\"dark\">\n";4292}else{4293print"<tr class=\"light\">\n";4294}4295$alternate^=1;4296print"<td><i>$ref{'age'}</i></td>\n".4297($curr?"<td class=\"current_head\">":"<td>") .4298$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4299-class=>"list name"},esc_html($ref{'name'})) .4300"</td>\n".4301"<td class=\"link\">".4302$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4303$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4304$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4305"</td>\n".4306"</tr>";4307}4308if(defined$extra) {4309print"<tr>\n".4310"<td colspan=\"3\">$extra</td>\n".4311"</tr>\n";4312}4313print"</table>\n";4314}43154316sub git_search_grep_body {4317my($commitlist,$from,$to,$extra) =@_;4318$from=0unlessdefined$from;4319$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43204321print"<table class=\"commit_search\">\n";4322my$alternate=1;4323for(my$i=$from;$i<=$to;$i++) {4324my%co= %{$commitlist->[$i]};4325if(!%co) {4326next;4327}4328my$commit=$co{'id'};4329if($alternate) {4330print"<tr class=\"dark\">\n";4331}else{4332print"<tr class=\"light\">\n";4333}4334$alternate^=1;4335my$author= chop_and_escape_str($co{'author_name'},15,5);4336print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4337"<td><i>".$author."</i></td>\n".4338"<td>".4339$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4340-class=>"list subject"},4341 chop_and_escape_str($co{'title'},50) ."<br/>");4342my$comment=$co{'comment'};4343foreachmy$line(@$comment) {4344if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4345my($lead,$match,$trail) = ($1,$2,$3);4346$match= chop_str($match,70,5,'center');4347my$contextlen=int((80-length($match))/2);4348$contextlen=30if($contextlen>30);4349$lead= chop_str($lead,$contextlen,10,'left');4350$trail= chop_str($trail,$contextlen,10,'right');43514352$lead= esc_html($lead);4353$match= esc_html($match);4354$trail= esc_html($trail);43554356print"$lead<span class=\"match\">$match</span>$trail<br />";4357}4358}4359print"</td>\n".4360"<td class=\"link\">".4361$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4362" | ".4363$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4364" | ".4365$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4366print"</td>\n".4367"</tr>\n";4368}4369if(defined$extra) {4370print"<tr>\n".4371"<td colspan=\"3\">$extra</td>\n".4372"</tr>\n";4373}4374print"</table>\n";4375}43764377## ======================================================================4378## ======================================================================4379## actions43804381sub git_project_list {4382my$order=$input_params{'order'};4383if(defined$order&&$order!~m/none|project|descr|owner|age/) {4384 die_error(400,"Unknown order parameter");4385}43864387my@list= git_get_projects_list();4388if(!@list) {4389 die_error(404,"No projects found");4390}43914392 git_header_html();4393if(-f $home_text) {4394print"<div class=\"index_include\">\n";4395 insert_file($home_text);4396print"</div>\n";4397}4398print$cgi->startform(-method=>"get") .4399"<p class=\"projsearch\">Search:\n".4400$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4401"</p>".4402$cgi->end_form() ."\n";4403 git_project_list_body(\@list,$order);4404 git_footer_html();4405}44064407sub git_forks {4408my$order=$input_params{'order'};4409if(defined$order&&$order!~m/none|project|descr|owner|age/) {4410 die_error(400,"Unknown order parameter");4411}44124413my@list= git_get_projects_list($project);4414if(!@list) {4415 die_error(404,"No forks found");4416}44174418 git_header_html();4419 git_print_page_nav('','');4420 git_print_header_div('summary',"$projectforks");4421 git_project_list_body(\@list,$order);4422 git_footer_html();4423}44244425sub git_project_index {4426my@projects= git_get_projects_list($project);44274428print$cgi->header(4429-type =>'text/plain',4430-charset =>'utf-8',4431-content_disposition =>'inline; filename="index.aux"');44324433foreachmy$pr(@projects) {4434if(!exists$pr->{'owner'}) {4435$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4436}44374438my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4439# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4440$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4441$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4442$path=~s/ /\+/g;4443$owner=~s/ /\+/g;44444445print"$path$owner\n";4446}4447}44484449sub git_summary {4450my$descr= git_get_project_description($project) ||"none";4451my%co= parse_commit("HEAD");4452my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4453my$head=$co{'id'};44544455my$owner= git_get_project_owner($project);44564457my$refs= git_get_references();4458# These get_*_list functions return one more to allow us to see if4459# there are more ...4460my@taglist= git_get_tags_list(16);4461my@headlist= git_get_heads_list(16);4462my@forklist;4463my$check_forks= gitweb_check_feature('forks');44644465if($check_forks) {4466@forklist= git_get_projects_list($project);4467}44684469 git_header_html();4470 git_print_page_nav('summary','',$head);44714472print"<div class=\"title\"> </div>\n";4473print"<table class=\"projects_list\">\n".4474"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4475"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4476if(defined$cd{'rfc2822'}) {4477print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4478}44794480# use per project git URL list in $projectroot/$project/cloneurl4481# or make project git URL from git base URL and project name4482my$url_tag="URL";4483my@url_list= git_get_project_url_list($project);4484@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4485foreachmy$git_url(@url_list) {4486next unless$git_url;4487print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4488$url_tag="";4489}44904491# Tag cloud4492my$show_ctags= gitweb_check_feature('ctags');4493if($show_ctags) {4494my$ctags= git_get_project_ctags($project);4495my$cloud= git_populate_project_tagcloud($ctags);4496print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4497print"</td>\n<td>"unless%$ctags;4498print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4499print"</td>\n<td>"if%$ctags;4500print git_show_project_tagcloud($cloud,48);4501print"</td></tr>";4502}45034504print"</table>\n";45054506if(-s "$projectroot/$project/README.html") {4507print"<div class=\"title\">readme</div>\n".4508"<div class=\"readme\">\n";4509 insert_file("$projectroot/$project/README.html");4510print"\n</div>\n";# class="readme"4511}45124513# we need to request one more than 16 (0..15) to check if4514# those 16 are all4515my@commitlist=$head? parse_commits($head,17) : ();4516if(@commitlist) {4517 git_print_header_div('shortlog');4518 git_shortlog_body(\@commitlist,0,15,$refs,4519$#commitlist<=15?undef:4520$cgi->a({-href => href(action=>"shortlog")},"..."));4521}45224523if(@taglist) {4524 git_print_header_div('tags');4525 git_tags_body(\@taglist,0,15,4526$#taglist<=15?undef:4527$cgi->a({-href => href(action=>"tags")},"..."));4528}45294530if(@headlist) {4531 git_print_header_div('heads');4532 git_heads_body(\@headlist,$head,0,15,4533$#headlist<=15?undef:4534$cgi->a({-href => href(action=>"heads")},"..."));4535}45364537if(@forklist) {4538 git_print_header_div('forks');4539 git_project_list_body(\@forklist,'age',0,15,4540$#forklist<=15?undef:4541$cgi->a({-href => href(action=>"forks")},"..."),4542'no_header');4543}45444545 git_footer_html();4546}45474548sub git_tag {4549my$head= git_get_head_hash($project);4550 git_header_html();4551 git_print_page_nav('','',$head,undef,$head);4552my%tag= parse_tag($hash);45534554if(!%tag) {4555 die_error(404,"Unknown tag object");4556}45574558 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4559print"<div class=\"title_text\">\n".4560"<table class=\"object_header\">\n".4561"<tr>\n".4562"<td>object</td>\n".4563"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4564$tag{'object'}) ."</td>\n".4565"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4566$tag{'type'}) ."</td>\n".4567"</tr>\n";4568if(defined($tag{'author'})) {4569my%ad= parse_date($tag{'epoch'},$tag{'tz'});4570print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4571print"<tr><td></td><td>".$ad{'rfc2822'} .4572sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4573"</td></tr>\n";4574}4575print"</table>\n\n".4576"</div>\n";4577print"<div class=\"page_body\">";4578my$comment=$tag{'comment'};4579foreachmy$line(@$comment) {4580chomp$line;4581print esc_html($line, -nbsp=>1) ."<br/>\n";4582}4583print"</div>\n";4584 git_footer_html();4585}45864587sub git_blame {4588# permissions4589 gitweb_check_feature('blame')4590or die_error(403,"Blame view not allowed");45914592# error checking4593 die_error(400,"No file name given")unless$file_name;4594$hash_base||= git_get_head_hash($project);4595 die_error(404,"Couldn't find base commit")unless$hash_base;4596my%co= parse_commit($hash_base)4597or die_error(404,"Commit not found");4598my$ftype="blob";4599if(!defined$hash) {4600$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4601or die_error(404,"Error looking up file");4602}else{4603$ftype= git_get_type($hash);4604if($ftype!~"blob") {4605 die_error(400,"Object is not a blob");4606}4607}46084609# run git-blame --porcelain4610open my$fd,"-|", git_cmd(),"blame",'-p',4611$hash_base,'--',$file_name4612or die_error(500,"Open git-blame failed");46134614# page header4615 git_header_html();4616my$formats_nav=4617$cgi->a({-href => href(action=>"blob", -replay=>1)},4618"blob") .4619" | ".4620$cgi->a({-href => href(action=>"history", -replay=>1)},4621"history") .4622" | ".4623$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4624"HEAD");4625 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4626 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4627 git_print_page_path($file_name,$ftype,$hash_base);46284629# page body4630my@rev_color=qw(light2 dark2);4631my$num_colors=scalar(@rev_color);4632my$current_color=0;4633my%metainfo= ();46344635print<<HTML;4636<div class="page_body">4637<table class="blame">4638<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4639HTML4640 LINE:4641while(my$line= <$fd>) {4642chomp$line;4643# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4644# no <lines in group> for subsequent lines in group of lines4645my($full_rev,$orig_lineno,$lineno,$group_size) =4646($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4647if(!exists$metainfo{$full_rev}) {4648$metainfo{$full_rev} = {};4649}4650my$meta=$metainfo{$full_rev};4651my$data;4652while($data= <$fd>) {4653chomp$data;4654last if($data=~s/^\t//);# contents of line4655if($data=~/^(\S+) (.*)$/) {4656$meta->{$1} =$2;4657}4658}4659my$short_rev=substr($full_rev,0,8);4660my$author=$meta->{'author'};4661my%date=4662 parse_date($meta->{'author-time'},$meta->{'author-tz'});4663my$date=$date{'iso-tz'};4664if($group_size) {4665$current_color= ($current_color+1) %$num_colors;4666}4667print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4668if($group_size) {4669print"<td class=\"sha1\"";4670print" title=\"". esc_html($author) .",$date\"";4671print" rowspan=\"$group_size\""if($group_size>1);4672print">";4673print$cgi->a({-href => href(action=>"commit",4674 hash=>$full_rev,4675 file_name=>$file_name)},4676 esc_html($short_rev));4677print"</td>\n";4678}4679my$parent_commit;4680if(!exists$meta->{'parent'}) {4681open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4682or die_error(500,"Open git-rev-parse failed");4683$parent_commit= <$dd>;4684close$dd;4685chomp($parent_commit);4686$meta->{'parent'} =$parent_commit;4687}else{4688$parent_commit=$meta->{'parent'};4689}4690my$blamed= href(action =>'blame',4691 file_name =>$meta->{'filename'},4692 hash_base =>$parent_commit);4693print"<td class=\"linenr\">";4694print$cgi->a({ -href =>"$blamed#l$orig_lineno",4695-class=>"linenr"},4696 esc_html($lineno));4697print"</td>";4698print"<td class=\"pre\">". esc_html($data) ."</td>\n";4699print"</tr>\n";4700}4701print"</table>\n";4702print"</div>";4703close$fd4704or print"Reading blob failed\n";47054706# page footer4707 git_footer_html();4708}47094710sub git_tags {4711my$head= git_get_head_hash($project);4712 git_header_html();4713 git_print_page_nav('','',$head,undef,$head);4714 git_print_header_div('summary',$project);47154716my@tagslist= git_get_tags_list();4717if(@tagslist) {4718 git_tags_body(\@tagslist);4719}4720 git_footer_html();4721}47224723sub git_heads {4724my$head= git_get_head_hash($project);4725 git_header_html();4726 git_print_page_nav('','',$head,undef,$head);4727 git_print_header_div('summary',$project);47284729my@headslist= git_get_heads_list();4730if(@headslist) {4731 git_heads_body(\@headslist,$head);4732}4733 git_footer_html();4734}47354736sub git_blob_plain {4737my$type=shift;4738my$expires;47394740if(!defined$hash) {4741if(defined$file_name) {4742my$base=$hash_base|| git_get_head_hash($project);4743$hash= git_get_hash_by_path($base,$file_name,"blob")4744or die_error(404,"Cannot find file");4745}else{4746 die_error(400,"No file name defined");4747}4748}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4749# blobs defined by non-textual hash id's can be cached4750$expires="+1d";4751}47524753open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4754or die_error(500,"Open git-cat-file blob '$hash' failed");47554756# content-type (can include charset)4757$type= blob_contenttype($fd,$file_name,$type);47584759# "save as" filename, even when no $file_name is given4760my$save_as="$hash";4761if(defined$file_name) {4762$save_as=$file_name;4763}elsif($type=~m/^text\//) {4764$save_as.='.txt';4765}47664767print$cgi->header(4768-type =>$type,4769-expires =>$expires,4770-content_disposition =>'inline; filename="'.$save_as.'"');4771undef$/;4772binmode STDOUT,':raw';4773print<$fd>;4774binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4775$/="\n";4776close$fd;4777}47784779sub git_blob {4780my$expires;47814782if(!defined$hash) {4783if(defined$file_name) {4784my$base=$hash_base|| git_get_head_hash($project);4785$hash= git_get_hash_by_path($base,$file_name,"blob")4786or die_error(404,"Cannot find file");4787}else{4788 die_error(400,"No file name defined");4789}4790}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4791# blobs defined by non-textual hash id's can be cached4792$expires="+1d";4793}47944795my$have_blame= gitweb_check_feature('blame');4796open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4797or die_error(500,"Couldn't cat$file_name,$hash");4798my$mimetype= blob_mimetype($fd,$file_name);4799if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4800close$fd;4801return git_blob_plain($mimetype);4802}4803# we can have blame only for text/* mimetype4804$have_blame&&= ($mimetype=~m!^text/!);48054806 git_header_html(undef,$expires);4807my$formats_nav='';4808if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4809if(defined$file_name) {4810if($have_blame) {4811$formats_nav.=4812$cgi->a({-href => href(action=>"blame", -replay=>1)},4813"blame") .4814" | ";4815}4816$formats_nav.=4817$cgi->a({-href => href(action=>"history", -replay=>1)},4818"history") .4819" | ".4820$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4821"raw") .4822" | ".4823$cgi->a({-href => href(action=>"blob",4824 hash_base=>"HEAD", file_name=>$file_name)},4825"HEAD");4826}else{4827$formats_nav.=4828$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4829"raw");4830}4831 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4832 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4833}else{4834print"<div class=\"page_nav\">\n".4835"<br/><br/></div>\n".4836"<div class=\"title\">$hash</div>\n";4837}4838 git_print_page_path($file_name,"blob",$hash_base);4839print"<div class=\"page_body\">\n";4840if($mimetype=~m!^image/!) {4841print qq!<img type="$mimetype"!;4842if($file_name) {4843print qq! alt="$file_name" title="$file_name"!;4844}4845print qq! src="! .4846 href(action=>"blob_plain", hash=>$hash,4847 hash_base=>$hash_base, file_name=>$file_name) .4848 qq!"/>\n!;4849}else{4850my$nr;4851while(my$line= <$fd>) {4852chomp$line;4853$nr++;4854$line= untabify($line);4855printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4856$nr,$nr,$nr, esc_html($line, -nbsp=>1);4857}4858}4859close$fd4860or print"Reading blob failed.\n";4861print"</div>";4862 git_footer_html();4863}48644865sub git_tree {4866if(!defined$hash_base) {4867$hash_base="HEAD";4868}4869if(!defined$hash) {4870if(defined$file_name) {4871$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4872}else{4873$hash=$hash_base;4874}4875}4876 die_error(404,"No such tree")unlessdefined($hash);4877$/="\0";4878open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4879or die_error(500,"Open git-ls-tree failed");4880my@entries=map{chomp;$_} <$fd>;4881close$fdor die_error(404,"Reading tree failed");4882$/="\n";48834884my$refs= git_get_references();4885my$ref= format_ref_marker($refs,$hash_base);4886 git_header_html();4887my$basedir='';4888my$have_blame= gitweb_check_feature('blame');4889if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4890my@views_nav= ();4891if(defined$file_name) {4892push@views_nav,4893$cgi->a({-href => href(action=>"history", -replay=>1)},4894"history"),4895$cgi->a({-href => href(action=>"tree",4896 hash_base=>"HEAD", file_name=>$file_name)},4897"HEAD"),4898}4899my$snapshot_links= format_snapshot_links($hash);4900if(defined$snapshot_links) {4901# FIXME: Should be available when we have no hash base as well.4902push@views_nav,$snapshot_links;4903}4904 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4905 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4906}else{4907undef$hash_base;4908print"<div class=\"page_nav\">\n";4909print"<br/><br/></div>\n";4910print"<div class=\"title\">$hash</div>\n";4911}4912if(defined$file_name) {4913$basedir=$file_name;4914if($basedirne''&&substr($basedir, -1)ne'/') {4915$basedir.='/';4916}4917 git_print_page_path($file_name,'tree',$hash_base);4918}4919print"<div class=\"page_body\">\n";4920print"<table class=\"tree\">\n";4921my$alternate=1;4922# '..' (top directory) link if possible4923if(defined$hash_base&&4924defined$file_name&&$file_name=~m![^/]+$!) {4925if($alternate) {4926print"<tr class=\"dark\">\n";4927}else{4928print"<tr class=\"light\">\n";4929}4930$alternate^=1;49314932my$up=$file_name;4933$up=~s!/?[^/]+$!!;4934undef$upunless$up;4935# based on git_print_tree_entry4936print'<td class="mode">'. mode_str('040000') ."</td>\n";4937print'<td class="list">';4938print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4939 file_name=>$up)},4940"..");4941print"</td>\n";4942print"<td class=\"link\"></td>\n";49434944print"</tr>\n";4945}4946foreachmy$line(@entries) {4947my%t= parse_ls_tree_line($line, -z =>1);49484949if($alternate) {4950print"<tr class=\"dark\">\n";4951}else{4952print"<tr class=\"light\">\n";4953}4954$alternate^=1;49554956 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);49574958print"</tr>\n";4959}4960print"</table>\n".4961"</div>";4962 git_footer_html();4963}49644965sub git_snapshot {4966my$format=$input_params{'snapshot_format'};4967if(!@snapshot_fmts) {4968 die_error(403,"Snapshots not allowed");4969}4970# default to first supported snapshot format4971$format||=$snapshot_fmts[0];4972if($format!~m/^[a-z0-9]+$/) {4973 die_error(400,"Invalid snapshot format parameter");4974}elsif(!exists($known_snapshot_formats{$format})) {4975 die_error(400,"Unknown snapshot format");4976}elsif(!grep($_eq$format,@snapshot_fmts)) {4977 die_error(403,"Unsupported snapshot format");4978}49794980if(!defined$hash) {4981$hash= git_get_head_hash($project);4982}49834984my$name=$project;4985$name=~ s,([^/])/*\.git$,$1,;4986$name= basename($name);4987my$filename= to_utf8($name);4988$name=~s/\047/\047\\\047\047/g;4989my$cmd;4990$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";4991$cmd= quote_command(4992 git_cmd(),'archive',4993"--format=$known_snapshot_formats{$format}{'format'}",4994"--prefix=$name/",$hash);4995if(exists$known_snapshot_formats{$format}{'compressor'}) {4996$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});4997}49984999print$cgi->header(5000-type =>$known_snapshot_formats{$format}{'type'},5001-content_disposition =>'inline; filename="'."$filename".'"',5002-status =>'200 OK');50035004open my$fd,"-|",$cmd5005or die_error(500,"Execute git-archive failed");5006binmode STDOUT,':raw';5007print<$fd>;5008binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5009close$fd;5010}50115012sub git_log {5013my$head= git_get_head_hash($project);5014if(!defined$hash) {5015$hash=$head;5016}5017if(!defined$page) {5018$page=0;5019}5020my$refs= git_get_references();50215022my@commitlist= parse_commits($hash,101, (100*$page));50235024my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);50255026my($patch_max) = gitweb_get_feature('patches');5027if($patch_max) {5028if($patch_max<0||@commitlist<=$patch_max) {5029$paging_nav.=" ⋅ ".5030$cgi->a({-href => href(action=>"patches", -replay=>1)},5031"patches");5032}5033}50345035 git_header_html();5036 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);50375038if(!@commitlist) {5039my%co= parse_commit($hash);50405041 git_print_header_div('summary',$project);5042print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5043}5044my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5045for(my$i=0;$i<=$to;$i++) {5046my%co= %{$commitlist[$i]};5047next if!%co;5048my$commit=$co{'id'};5049my$ref= format_ref_marker($refs,$commit);5050my%ad= parse_date($co{'author_epoch'});5051 git_print_header_div('commit',5052"<span class=\"age\">$co{'age_string'}</span>".5053 esc_html($co{'title'}) .$ref,5054$commit);5055print"<div class=\"title_text\">\n".5056"<div class=\"log_link\">\n".5057$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5058" | ".5059$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5060" | ".5061$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5062"<br/>\n".5063"</div>\n".5064"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".5065"</div>\n";50665067print"<div class=\"log_body\">\n";5068 git_print_log($co{'comment'}, -final_empty_line=>1);5069print"</div>\n";5070}5071if($#commitlist>=100) {5072print"<div class=\"page_nav\">\n";5073print$cgi->a({-href => href(-replay=>1, page=>$page+1),5074-accesskey =>"n", -title =>"Alt-n"},"next");5075print"</div>\n";5076}5077 git_footer_html();5078}50795080sub git_commit {5081$hash||=$hash_base||"HEAD";5082my%co= parse_commit($hash)5083or die_error(404,"Unknown commit object");5084my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5085my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});50865087my$parent=$co{'parent'};5088my$parents=$co{'parents'};# listref50895090# we need to prepare $formats_nav before any parameter munging5091my$formats_nav;5092if(!defined$parent) {5093# --root commitdiff5094$formats_nav.='(initial)';5095}elsif(@$parents==1) {5096# single parent commit5097$formats_nav.=5098'(parent: '.5099$cgi->a({-href => href(action=>"commit",5100 hash=>$parent)},5101 esc_html(substr($parent,0,7))) .5102')';5103}else{5104# merge commit5105$formats_nav.=5106'(merge: '.5107join(' ',map{5108$cgi->a({-href => href(action=>"commit",5109 hash=>$_)},5110 esc_html(substr($_,0,7)));5111}@$parents) .5112')';5113}5114if(gitweb_check_feature('patches')) {5115$formats_nav.=" | ".5116$cgi->a({-href => href(action=>"patch", -replay=>1)},5117"patch");5118}51195120if(!defined$parent) {5121$parent="--root";5122}5123my@difftree;5124open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5125@diff_opts,5126(@$parents<=1?$parent:'-c'),5127$hash,"--"5128or die_error(500,"Open git-diff-tree failed");5129@difftree=map{chomp;$_} <$fd>;5130close$fdor die_error(404,"Reading git-diff-tree failed");51315132# non-textual hash id's can be cached5133my$expires;5134if($hash=~m/^[0-9a-fA-F]{40}$/) {5135$expires="+1d";5136}5137my$refs= git_get_references();5138my$ref= format_ref_marker($refs,$co{'id'});51395140 git_header_html(undef,$expires);5141 git_print_page_nav('commit','',5142$hash,$co{'tree'},$hash,5143$formats_nav);51445145if(defined$co{'parent'}) {5146 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5147}else{5148 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5149}5150print"<div class=\"title_text\">\n".5151"<table class=\"object_header\">\n";5152print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5153"<tr>".5154"<td></td><td>$ad{'rfc2822'}";5155if($ad{'hour_local'} <6) {5156printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5157$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5158}else{5159printf(" (%02d:%02d%s)",5160$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5161}5162print"</td>".5163"</tr>\n";5164print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5165print"<tr><td></td><td>$cd{'rfc2822'}".5166sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5167"</td></tr>\n";5168print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5169print"<tr>".5170"<td>tree</td>".5171"<td class=\"sha1\">".5172$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5173class=>"list"},$co{'tree'}) .5174"</td>".5175"<td class=\"link\">".5176$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5177"tree");5178my$snapshot_links= format_snapshot_links($hash);5179if(defined$snapshot_links) {5180print" | ".$snapshot_links;5181}5182print"</td>".5183"</tr>\n";51845185foreachmy$par(@$parents) {5186print"<tr>".5187"<td>parent</td>".5188"<td class=\"sha1\">".5189$cgi->a({-href => href(action=>"commit", hash=>$par),5190class=>"list"},$par) .5191"</td>".5192"<td class=\"link\">".5193$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5194" | ".5195$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5196"</td>".5197"</tr>\n";5198}5199print"</table>".5200"</div>\n";52015202print"<div class=\"page_body\">\n";5203 git_print_log($co{'comment'});5204print"</div>\n";52055206 git_difftree_body(\@difftree,$hash,@$parents);52075208 git_footer_html();5209}52105211sub git_object {5212# object is defined by:5213# - hash or hash_base alone5214# - hash_base and file_name5215my$type;52165217# - hash or hash_base alone5218if($hash|| ($hash_base&& !defined$file_name)) {5219my$object_id=$hash||$hash_base;52205221open my$fd,"-|", quote_command(5222 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5223or die_error(404,"Object does not exist");5224$type= <$fd>;5225chomp$type;5226close$fd5227or die_error(404,"Object does not exist");52285229# - hash_base and file_name5230}elsif($hash_base&&defined$file_name) {5231$file_name=~ s,/+$,,;52325233system(git_cmd(),"cat-file",'-e',$hash_base) ==05234or die_error(404,"Base object does not exist");52355236# here errors should not hapen5237open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5238or die_error(500,"Open git-ls-tree failed");5239my$line= <$fd>;5240close$fd;52415242#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5243unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5244 die_error(404,"File or directory for given base does not exist");5245}5246$type=$2;5247$hash=$3;5248}else{5249 die_error(400,"Not enough information to find object");5250}52515252print$cgi->redirect(-uri => href(action=>$type, -full=>1,5253 hash=>$hash, hash_base=>$hash_base,5254 file_name=>$file_name),5255-status =>'302 Found');5256}52575258sub git_blobdiff {5259my$format=shift||'html';52605261my$fd;5262my@difftree;5263my%diffinfo;5264my$expires;52655266# preparing $fd and %diffinfo for git_patchset_body5267# new style URI5268if(defined$hash_base&&defined$hash_parent_base) {5269if(defined$file_name) {5270# read raw output5271open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5272$hash_parent_base,$hash_base,5273"--", (defined$file_parent?$file_parent: ()),$file_name5274or die_error(500,"Open git-diff-tree failed");5275@difftree=map{chomp;$_} <$fd>;5276close$fd5277or die_error(404,"Reading git-diff-tree failed");5278@difftree5279or die_error(404,"Blob diff not found");52805281}elsif(defined$hash&&5282$hash=~/[0-9a-fA-F]{40}/) {5283# try to find filename from $hash52845285# read filtered raw output5286open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5287$hash_parent_base,$hash_base,"--"5288or die_error(500,"Open git-diff-tree failed");5289@difftree=5290# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5291# $hash == to_id5292grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5293map{chomp;$_} <$fd>;5294close$fd5295or die_error(404,"Reading git-diff-tree failed");5296@difftree5297or die_error(404,"Blob diff not found");52985299}else{5300 die_error(400,"Missing one of the blob diff parameters");5301}53025303if(@difftree>1) {5304 die_error(400,"Ambiguous blob diff specification");5305}53065307%diffinfo= parse_difftree_raw_line($difftree[0]);5308$file_parent||=$diffinfo{'from_file'} ||$file_name;5309$file_name||=$diffinfo{'to_file'};53105311$hash_parent||=$diffinfo{'from_id'};5312$hash||=$diffinfo{'to_id'};53135314# non-textual hash id's can be cached5315if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5316$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5317$expires='+1d';5318}53195320# open patch output5321open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5322'-p', ($formateq'html'?"--full-index": ()),5323$hash_parent_base,$hash_base,5324"--", (defined$file_parent?$file_parent: ()),$file_name5325or die_error(500,"Open git-diff-tree failed");5326}53275328# old/legacy style URI -- not generated anymore since 1.4.3.5329if(!%diffinfo) {5330 die_error('404 Not Found',"Missing one of the blob diff parameters")5331}53325333# header5334if($formateq'html') {5335my$formats_nav=5336$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5337"raw");5338 git_header_html(undef,$expires);5339if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5340 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5341 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5342}else{5343print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5344print"<div class=\"title\">$hashvs$hash_parent</div>\n";5345}5346if(defined$file_name) {5347 git_print_page_path($file_name,"blob",$hash_base);5348}else{5349print"<div class=\"page_path\"></div>\n";5350}53515352}elsif($formateq'plain') {5353print$cgi->header(5354-type =>'text/plain',5355-charset =>'utf-8',5356-expires =>$expires,5357-content_disposition =>'inline; filename="'."$file_name".'.patch"');53585359print"X-Git-Url: ".$cgi->self_url() ."\n\n";53605361}else{5362 die_error(400,"Unknown blobdiff format");5363}53645365# patch5366if($formateq'html') {5367print"<div class=\"page_body\">\n";53685369 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5370close$fd;53715372print"</div>\n";# class="page_body"5373 git_footer_html();53745375}else{5376while(my$line= <$fd>) {5377$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5378$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;53795380print$line;53815382last if$line=~m!^\+\+\+!;5383}5384local$/=undef;5385print<$fd>;5386close$fd;5387}5388}53895390sub git_blobdiff_plain {5391 git_blobdiff('plain');5392}53935394sub git_commitdiff {5395my%params=@_;5396my$format=$params{-format} ||'html';53975398my($patch_max) = gitweb_get_feature('patches');5399if($formateq'patch') {5400 die_error(403,"Patch view not allowed")unless$patch_max;5401}54025403$hash||=$hash_base||"HEAD";5404my%co= parse_commit($hash)5405or die_error(404,"Unknown commit object");54065407# choose format for commitdiff for merge5408if(!defined$hash_parent&& @{$co{'parents'}} >1) {5409$hash_parent='--cc';5410}5411# we need to prepare $formats_nav before almost any parameter munging5412my$formats_nav;5413if($formateq'html') {5414$formats_nav=5415$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5416"raw");5417if($patch_max) {5418$formats_nav.=" | ".5419$cgi->a({-href => href(action=>"patch", -replay=>1)},5420"patch");5421}54225423if(defined$hash_parent&&5424$hash_parentne'-c'&&$hash_parentne'--cc') {5425# commitdiff with two commits given5426my$hash_parent_short=$hash_parent;5427if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5428$hash_parent_short=substr($hash_parent,0,7);5429}5430$formats_nav.=5431' (from';5432for(my$i=0;$i< @{$co{'parents'}};$i++) {5433if($co{'parents'}[$i]eq$hash_parent) {5434$formats_nav.=' parent '. ($i+1);5435last;5436}5437}5438$formats_nav.=': '.5439$cgi->a({-href => href(action=>"commitdiff",5440 hash=>$hash_parent)},5441 esc_html($hash_parent_short)) .5442')';5443}elsif(!$co{'parent'}) {5444# --root commitdiff5445$formats_nav.=' (initial)';5446}elsif(scalar@{$co{'parents'}} ==1) {5447# single parent commit5448$formats_nav.=5449' (parent: '.5450$cgi->a({-href => href(action=>"commitdiff",5451 hash=>$co{'parent'})},5452 esc_html(substr($co{'parent'},0,7))) .5453')';5454}else{5455# merge commit5456if($hash_parenteq'--cc') {5457$formats_nav.=' | '.5458$cgi->a({-href => href(action=>"commitdiff",5459 hash=>$hash, hash_parent=>'-c')},5460'combined');5461}else{# $hash_parent eq '-c'5462$formats_nav.=' | '.5463$cgi->a({-href => href(action=>"commitdiff",5464 hash=>$hash, hash_parent=>'--cc')},5465'compact');5466}5467$formats_nav.=5468' (merge: '.5469join(' ',map{5470$cgi->a({-href => href(action=>"commitdiff",5471 hash=>$_)},5472 esc_html(substr($_,0,7)));5473} @{$co{'parents'}} ) .5474')';5475}5476}54775478my$hash_parent_param=$hash_parent;5479if(!defined$hash_parent_param) {5480# --cc for multiple parents, --root for parentless5481$hash_parent_param=5482@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5483}54845485# read commitdiff5486my$fd;5487my@difftree;5488if($formateq'html') {5489open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5490"--no-commit-id","--patch-with-raw","--full-index",5491$hash_parent_param,$hash,"--"5492or die_error(500,"Open git-diff-tree failed");54935494while(my$line= <$fd>) {5495chomp$line;5496# empty line ends raw part of diff-tree output5497last unless$line;5498push@difftree,scalar parse_difftree_raw_line($line);5499}55005501}elsif($formateq'plain') {5502open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5503'-p',$hash_parent_param,$hash,"--"5504or die_error(500,"Open git-diff-tree failed");5505}elsif($formateq'patch') {5506# For commit ranges, we limit the output to the number of5507# patches specified in the 'patches' feature.5508# For single commits, we limit the output to a single patch,5509# diverging from the git-format-patch default.5510my@commit_spec= ();5511if($hash_parent) {5512if($patch_max>0) {5513push@commit_spec,"-$patch_max";5514}5515push@commit_spec,'-n',"$hash_parent..$hash";5516}else{5517if($params{-single}) {5518push@commit_spec,'-1';5519}else{5520if($patch_max>0) {5521push@commit_spec,"-$patch_max";5522}5523push@commit_spec,"-n";5524}5525push@commit_spec,'--root',$hash;5526}5527open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5528'--stdout',@commit_spec5529or die_error(500,"Open git-format-patch failed");5530}else{5531 die_error(400,"Unknown commitdiff format");5532}55335534# non-textual hash id's can be cached5535my$expires;5536if($hash=~m/^[0-9a-fA-F]{40}$/) {5537$expires="+1d";5538}55395540# write commit message5541if($formateq'html') {5542my$refs= git_get_references();5543my$ref= format_ref_marker($refs,$co{'id'});55445545 git_header_html(undef,$expires);5546 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5547 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5548 git_print_authorship(\%co);5549print"<div class=\"page_body\">\n";5550if(@{$co{'comment'}} >1) {5551print"<div class=\"log\">\n";5552 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5553print"</div>\n";# class="log"5554}55555556}elsif($formateq'plain') {5557my$refs= git_get_references("tags");5558my$tagname= git_get_rev_name_tags($hash);5559my$filename= basename($project) ."-$hash.patch";55605561print$cgi->header(5562-type =>'text/plain',5563-charset =>'utf-8',5564-expires =>$expires,5565-content_disposition =>'inline; filename="'."$filename".'"');5566my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5567print"From: ". to_utf8($co{'author'}) ."\n";5568print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5569print"Subject: ". to_utf8($co{'title'}) ."\n";55705571print"X-Git-Tag:$tagname\n"if$tagname;5572print"X-Git-Url: ".$cgi->self_url() ."\n\n";55735574foreachmy$line(@{$co{'comment'}}) {5575print to_utf8($line) ."\n";5576}5577print"---\n\n";5578}elsif($formateq'patch') {5579my$filename= basename($project) ."-$hash.patch";55805581print$cgi->header(5582-type =>'text/plain',5583-charset =>'utf-8',5584-expires =>$expires,5585-content_disposition =>'inline; filename="'."$filename".'"');5586}55875588# write patch5589if($formateq'html') {5590my$use_parents= !defined$hash_parent||5591$hash_parenteq'-c'||$hash_parenteq'--cc';5592 git_difftree_body(\@difftree,$hash,5593$use_parents? @{$co{'parents'}} :$hash_parent);5594print"<br/>\n";55955596 git_patchset_body($fd, \@difftree,$hash,5597$use_parents? @{$co{'parents'}} :$hash_parent);5598close$fd;5599print"</div>\n";# class="page_body"5600 git_footer_html();56015602}elsif($formateq'plain') {5603local$/=undef;5604print<$fd>;5605close$fd5606or print"Reading git-diff-tree failed\n";5607}elsif($formateq'patch') {5608local$/=undef;5609print<$fd>;5610close$fd5611or print"Reading git-format-patch failed\n";5612}5613}56145615sub git_commitdiff_plain {5616 git_commitdiff(-format =>'plain');5617}56185619# format-patch-style patches5620sub git_patch {5621 git_commitdiff(-format =>'patch', -single=>1);5622}56235624sub git_patches {5625 git_commitdiff(-format =>'patch');5626}56275628sub git_history {5629if(!defined$hash_base) {5630$hash_base= git_get_head_hash($project);5631}5632if(!defined$page) {5633$page=0;5634}5635my$ftype;5636my%co= parse_commit($hash_base)5637or die_error(404,"Unknown commit object");56385639my$refs= git_get_references();5640my$limit=sprintf("--max-count=%i", (100* ($page+1)));56415642my@commitlist= parse_commits($hash_base,101, (100*$page),5643$file_name,"--full-history")5644or die_error(404,"No such file or directory on given branch");56455646if(!defined$hash&&defined$file_name) {5647# some commits could have deleted file in question,5648# and not have it in tree, but one of them has to have it5649for(my$i=0;$i<=@commitlist;$i++) {5650$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5651last ifdefined$hash;5652}5653}5654if(defined$hash) {5655$ftype= git_get_type($hash);5656}5657if(!defined$ftype) {5658 die_error(500,"Unknown type of object");5659}56605661my$paging_nav='';5662if($page>0) {5663$paging_nav.=5664$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5665 file_name=>$file_name)},5666"first");5667$paging_nav.=" ⋅ ".5668$cgi->a({-href => href(-replay=>1, page=>$page-1),5669-accesskey =>"p", -title =>"Alt-p"},"prev");5670}else{5671$paging_nav.="first";5672$paging_nav.=" ⋅ prev";5673}5674my$next_link='';5675if($#commitlist>=100) {5676$next_link=5677$cgi->a({-href => href(-replay=>1, page=>$page+1),5678-accesskey =>"n", -title =>"Alt-n"},"next");5679$paging_nav.=" ⋅$next_link";5680}else{5681$paging_nav.=" ⋅ next";5682}56835684 git_header_html();5685 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5686 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5687 git_print_page_path($file_name,$ftype,$hash_base);56885689 git_history_body(\@commitlist,0,99,5690$refs,$hash_base,$ftype,$next_link);56915692 git_footer_html();5693}56945695sub git_search {5696 gitweb_check_feature('search')or die_error(403,"Search is disabled");5697if(!defined$searchtext) {5698 die_error(400,"Text field is empty");5699}5700if(!defined$hash) {5701$hash= git_get_head_hash($project);5702}5703my%co= parse_commit($hash);5704if(!%co) {5705 die_error(404,"Unknown commit object");5706}5707if(!defined$page) {5708$page=0;5709}57105711$searchtype||='commit';5712if($searchtypeeq'pickaxe') {5713# pickaxe may take all resources of your box and run for several minutes5714# with every query - so decide by yourself how public you make this feature5715 gitweb_check_feature('pickaxe')5716or die_error(403,"Pickaxe is disabled");5717}5718if($searchtypeeq'grep') {5719 gitweb_check_feature('grep')5720or die_error(403,"Grep is disabled");5721}57225723 git_header_html();57245725if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5726my$greptype;5727if($searchtypeeq'commit') {5728$greptype="--grep=";5729}elsif($searchtypeeq'author') {5730$greptype="--author=";5731}elsif($searchtypeeq'committer') {5732$greptype="--committer=";5733}5734$greptype.=$searchtext;5735my@commitlist= parse_commits($hash,101, (100*$page),undef,5736$greptype,'--regexp-ignore-case',5737$search_use_regexp?'--extended-regexp':'--fixed-strings');57385739my$paging_nav='';5740if($page>0) {5741$paging_nav.=5742$cgi->a({-href => href(action=>"search", hash=>$hash,5743 searchtext=>$searchtext,5744 searchtype=>$searchtype)},5745"first");5746$paging_nav.=" ⋅ ".5747$cgi->a({-href => href(-replay=>1, page=>$page-1),5748-accesskey =>"p", -title =>"Alt-p"},"prev");5749}else{5750$paging_nav.="first";5751$paging_nav.=" ⋅ prev";5752}5753my$next_link='';5754if($#commitlist>=100) {5755$next_link=5756$cgi->a({-href => href(-replay=>1, page=>$page+1),5757-accesskey =>"n", -title =>"Alt-n"},"next");5758$paging_nav.=" ⋅$next_link";5759}else{5760$paging_nav.=" ⋅ next";5761}57625763if($#commitlist>=100) {5764}57655766 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5767 git_print_header_div('commit', esc_html($co{'title'}),$hash);5768 git_search_grep_body(\@commitlist,0,99,$next_link);5769}57705771if($searchtypeeq'pickaxe') {5772 git_print_page_nav('','',$hash,$co{'tree'},$hash);5773 git_print_header_div('commit', esc_html($co{'title'}),$hash);57745775print"<table class=\"pickaxe search\">\n";5776my$alternate=1;5777$/="\n";5778open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5779'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5780($search_use_regexp?'--pickaxe-regex': ());5781undef%co;5782my@files;5783while(my$line= <$fd>) {5784chomp$line;5785next unless$line;57865787my%set= parse_difftree_raw_line($line);5788if(defined$set{'commit'}) {5789# finish previous commit5790if(%co) {5791print"</td>\n".5792"<td class=\"link\">".5793$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5794" | ".5795$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5796print"</td>\n".5797"</tr>\n";5798}57995800if($alternate) {5801print"<tr class=\"dark\">\n";5802}else{5803print"<tr class=\"light\">\n";5804}5805$alternate^=1;5806%co= parse_commit($set{'commit'});5807my$author= chop_and_escape_str($co{'author_name'},15,5);5808print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5809"<td><i>$author</i></td>\n".5810"<td>".5811$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5812-class=>"list subject"},5813 chop_and_escape_str($co{'title'},50) ."<br/>");5814}elsif(defined$set{'to_id'}) {5815next if($set{'to_id'} =~m/^0{40}$/);58165817print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5818 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5819-class=>"list"},5820"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5821"<br/>\n";5822}5823}5824close$fd;58255826# finish last commit (warning: repetition!)5827if(%co) {5828print"</td>\n".5829"<td class=\"link\">".5830$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5831" | ".5832$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5833print"</td>\n".5834"</tr>\n";5835}58365837print"</table>\n";5838}58395840if($searchtypeeq'grep') {5841 git_print_page_nav('','',$hash,$co{'tree'},$hash);5842 git_print_header_div('commit', esc_html($co{'title'}),$hash);58435844print"<table class=\"grep_search\">\n";5845my$alternate=1;5846my$matches=0;5847$/="\n";5848open my$fd,"-|", git_cmd(),'grep','-n',5849$search_use_regexp? ('-E','-i') :'-F',5850$searchtext,$co{'tree'};5851my$lastfile='';5852while(my$line= <$fd>) {5853chomp$line;5854my($file,$lno,$ltext,$binary);5855last if($matches++>1000);5856if($line=~/^Binary file (.+) matches$/) {5857$file=$1;5858$binary=1;5859}else{5860(undef,$file,$lno,$ltext) =split(/:/,$line,4);5861}5862if($filene$lastfile) {5863$lastfileand print"</td></tr>\n";5864if($alternate++) {5865print"<tr class=\"dark\">\n";5866}else{5867print"<tr class=\"light\">\n";5868}5869print"<td class=\"list\">".5870$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5871 file_name=>"$file"),5872-class=>"list"}, esc_path($file));5873print"</td><td>\n";5874$lastfile=$file;5875}5876if($binary) {5877print"<div class=\"binary\">Binary file</div>\n";5878}else{5879$ltext= untabify($ltext);5880if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5881$ltext= esc_html($1, -nbsp=>1);5882$ltext.='<span class="match">';5883$ltext.= esc_html($2, -nbsp=>1);5884$ltext.='</span>';5885$ltext.= esc_html($3, -nbsp=>1);5886}else{5887$ltext= esc_html($ltext, -nbsp=>1);5888}5889print"<div class=\"pre\">".5890$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5891 file_name=>"$file").'#l'.$lno,5892-class=>"linenr"},sprintf('%4i',$lno))5893.' '.$ltext."</div>\n";5894}5895}5896if($lastfile) {5897print"</td></tr>\n";5898if($matches>1000) {5899print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5900}5901}else{5902print"<div class=\"diff nodifferences\">No matches found</div>\n";5903}5904close$fd;59055906print"</table>\n";5907}5908 git_footer_html();5909}59105911sub git_search_help {5912 git_header_html();5913 git_print_page_nav('','',$hash,$hash,$hash);5914print<<EOT;5915<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5916regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5917the pattern entered is recognized as the POSIX extended5918<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5919insensitive).</p>5920<dl>5921<dt><b>commit</b></dt>5922<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5923EOT5924my$have_grep= gitweb_check_feature('grep');5925if($have_grep) {5926print<<EOT;5927<dt><b>grep</b></dt>5928<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5929 a different one) are searched for the given pattern. On large trees, this search can take5930a while and put some strain on the server, so please use it with some consideration. Note that5931due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5932case-sensitive.</dd>5933EOT5934}5935print<<EOT;5936<dt><b>author</b></dt>5937<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5938<dt><b>committer</b></dt>5939<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5940EOT5941my$have_pickaxe= gitweb_check_feature('pickaxe');5942if($have_pickaxe) {5943print<<EOT;5944<dt><b>pickaxe</b></dt>5945<dd>All commits that caused the string to appear or disappear from any file (changes that5946added, removed or "modified" the string) will be listed. This search can take a while and5947takes a lot of strain on the server, so please use it wisely. Note that since you may be5948interested even in changes just changing the case as well, this search is case sensitive.</dd>5949EOT5950}5951print"</dl>\n";5952 git_footer_html();5953}59545955sub git_shortlog {5956my$head= git_get_head_hash($project);5957if(!defined$hash) {5958$hash=$head;5959}5960if(!defined$page) {5961$page=0;5962}5963my$refs= git_get_references();59645965my$commit_hash=$hash;5966if(defined$hash_parent) {5967$commit_hash="$hash_parent..$hash";5968}5969my@commitlist= parse_commits($commit_hash,101, (100*$page));59705971my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5972my$next_link='';5973if($#commitlist>=100) {5974$next_link=5975$cgi->a({-href => href(-replay=>1, page=>$page+1),5976-accesskey =>"n", -title =>"Alt-n"},"next");5977}5978my$patch_max= gitweb_check_feature('patches');5979if($patch_max) {5980if($patch_max<0||@commitlist<=$patch_max) {5981$paging_nav.=" ⋅ ".5982$cgi->a({-href => href(action=>"patches", -replay=>1)},5983"patches");5984}5985}59865987 git_header_html();5988 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);5989 git_print_header_div('summary',$project);59905991 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);59925993 git_footer_html();5994}59955996## ......................................................................5997## feeds (RSS, Atom; OPML)59985999sub git_feed {6000my$format=shift||'atom';6001my$have_blame= gitweb_check_feature('blame');60026003# Atom: http://www.atomenabled.org/developers/syndication/6004# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6005if($formatne'rss'&&$formatne'atom') {6006 die_error(400,"Unknown web feed format");6007}60086009# log/feed of current (HEAD) branch, log of given branch, history of file/directory6010my$head=$hash||'HEAD';6011my@commitlist= parse_commits($head,150,0,$file_name);60126013my%latest_commit;6014my%latest_date;6015my$content_type="application/$format+xml";6016if(defined$cgi->http('HTTP_ACCEPT') &&6017$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6018# browser (feed reader) prefers text/xml6019$content_type='text/xml';6020}6021if(defined($commitlist[0])) {6022%latest_commit= %{$commitlist[0]};6023my$latest_epoch=$latest_commit{'committer_epoch'};6024%latest_date= parse_date($latest_epoch);6025my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6026if(defined$if_modified) {6027my$since;6028if(eval{require HTTP::Date;1; }) {6029$since= HTTP::Date::str2time($if_modified);6030}elsif(eval{require Time::ParseDate;1; }) {6031$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6032}6033if(defined$since&&$latest_epoch<=$since) {6034print$cgi->header(6035-type =>$content_type,6036-charset =>'utf-8',6037-last_modified =>$latest_date{'rfc2822'},6038-status =>'304 Not Modified');6039return;6040}6041}6042print$cgi->header(6043-type =>$content_type,6044-charset =>'utf-8',6045-last_modified =>$latest_date{'rfc2822'});6046}else{6047print$cgi->header(6048-type =>$content_type,6049-charset =>'utf-8');6050}60516052# Optimization: skip generating the body if client asks only6053# for Last-Modified date.6054return if($cgi->request_method()eq'HEAD');60556056# header variables6057my$title="$site_name-$project/$action";6058my$feed_type='log';6059if(defined$hash) {6060$title.=" - '$hash'";6061$feed_type='branch log';6062if(defined$file_name) {6063$title.=" ::$file_name";6064$feed_type='history';6065}6066}elsif(defined$file_name) {6067$title.=" -$file_name";6068$feed_type='history';6069}6070$title.="$feed_type";6071my$descr= git_get_project_description($project);6072if(defined$descr) {6073$descr= esc_html($descr);6074}else{6075$descr="$project".6076($formateq'rss'?'RSS':'Atom') .6077" feed";6078}6079my$owner= git_get_project_owner($project);6080$owner= esc_html($owner);60816082#header6083my$alt_url;6084if(defined$file_name) {6085$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6086}elsif(defined$hash) {6087$alt_url= href(-full=>1, action=>"log", hash=>$hash);6088}else{6089$alt_url= href(-full=>1, action=>"summary");6090}6091print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6092if($formateq'rss') {6093print<<XML;6094<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6095<channel>6096XML6097print"<title>$title</title>\n".6098"<link>$alt_url</link>\n".6099"<description>$descr</description>\n".6100"<language>en</language>\n".6101# project owner is responsible for 'editorial' content6102"<managingEditor>$owner</managingEditor>\n";6103if(defined$logo||defined$favicon) {6104# prefer the logo to the favicon, since RSS6105# doesn't allow both6106my$img= esc_url($logo||$favicon);6107print"<image>\n".6108"<url>$img</url>\n".6109"<title>$title</title>\n".6110"<link>$alt_url</link>\n".6111"</image>\n";6112}6113if(%latest_date) {6114print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6115print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6116}6117print"<generator>gitweb v.$version/$git_version</generator>\n";6118}elsif($formateq'atom') {6119print<<XML;6120<feed xmlns="http://www.w3.org/2005/Atom">6121XML6122print"<title>$title</title>\n".6123"<subtitle>$descr</subtitle>\n".6124'<link rel="alternate" type="text/html" href="'.6125$alt_url.'" />'."\n".6126'<link rel="self" type="'.$content_type.'" href="'.6127$cgi->self_url() .'" />'."\n".6128"<id>". href(-full=>1) ."</id>\n".6129# use project owner for feed author6130"<author><name>$owner</name></author>\n";6131if(defined$favicon) {6132print"<icon>". esc_url($favicon) ."</icon>\n";6133}6134if(defined$logo_url) {6135# not twice as wide as tall: 72 x 27 pixels6136print"<logo>". esc_url($logo) ."</logo>\n";6137}6138if(!%latest_date) {6139# dummy date to keep the feed valid until commits trickle in:6140print"<updated>1970-01-01T00:00:00Z</updated>\n";6141}else{6142print"<updated>$latest_date{'iso-8601'}</updated>\n";6143}6144print"<generator version='$version/$git_version'>gitweb</generator>\n";6145}61466147# contents6148for(my$i=0;$i<=$#commitlist;$i++) {6149my%co= %{$commitlist[$i]};6150my$commit=$co{'id'};6151# we read 150, we always show 30 and the ones more recent than 48 hours6152if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6153last;6154}6155my%cd= parse_date($co{'author_epoch'});61566157# get list of changed files6158open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6159$co{'parent'} ||"--root",6160$co{'id'},"--", (defined$file_name?$file_name: ())6161ornext;6162my@difftree=map{chomp;$_} <$fd>;6163close$fd6164ornext;61656166# print element (entry, item)6167my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6168if($formateq'rss') {6169print"<item>\n".6170"<title>". esc_html($co{'title'}) ."</title>\n".6171"<author>". esc_html($co{'author'}) ."</author>\n".6172"<pubDate>$cd{'rfc2822'}</pubDate>\n".6173"<guid isPermaLink=\"true\">$co_url</guid>\n".6174"<link>$co_url</link>\n".6175"<description>". esc_html($co{'title'}) ."</description>\n".6176"<content:encoded>".6177"<![CDATA[\n";6178}elsif($formateq'atom') {6179print"<entry>\n".6180"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6181"<updated>$cd{'iso-8601'}</updated>\n".6182"<author>\n".6183" <name>". esc_html($co{'author_name'}) ."</name>\n";6184if($co{'author_email'}) {6185print" <email>". esc_html($co{'author_email'}) ."</email>\n";6186}6187print"</author>\n".6188# use committer for contributor6189"<contributor>\n".6190" <name>". esc_html($co{'committer_name'}) ."</name>\n";6191if($co{'committer_email'}) {6192print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6193}6194print"</contributor>\n".6195"<published>$cd{'iso-8601'}</published>\n".6196"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6197"<id>$co_url</id>\n".6198"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6199"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6200}6201my$comment=$co{'comment'};6202print"<pre>\n";6203foreachmy$line(@$comment) {6204$line= esc_html($line);6205print"$line\n";6206}6207print"</pre><ul>\n";6208foreachmy$difftree_line(@difftree) {6209my%difftree= parse_difftree_raw_line($difftree_line);6210next if!$difftree{'from_id'};62116212my$file=$difftree{'file'} ||$difftree{'to_file'};62136214print"<li>".6215"[".6216$cgi->a({-href => href(-full=>1, action=>"blobdiff",6217 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6218 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6219 file_name=>$file, file_parent=>$difftree{'from_file'}),6220-title =>"diff"},'D');6221if($have_blame) {6222print$cgi->a({-href => href(-full=>1, action=>"blame",6223 file_name=>$file, hash_base=>$commit),6224-title =>"blame"},'B');6225}6226# if this is not a feed of a file history6227if(!defined$file_name||$file_namene$file) {6228print$cgi->a({-href => href(-full=>1, action=>"history",6229 file_name=>$file, hash=>$commit),6230-title =>"history"},'H');6231}6232$file= esc_path($file);6233print"] ".6234"$file</li>\n";6235}6236if($formateq'rss') {6237print"</ul>]]>\n".6238"</content:encoded>\n".6239"</item>\n";6240}elsif($formateq'atom') {6241print"</ul>\n</div>\n".6242"</content>\n".6243"</entry>\n";6244}6245}62466247# end of feed6248if($formateq'rss') {6249print"</channel>\n</rss>\n";6250}elsif($formateq'atom') {6251print"</feed>\n";6252}6253}62546255sub git_rss {6256 git_feed('rss');6257}62586259sub git_atom {6260 git_feed('atom');6261}62626263sub git_opml {6264my@list= git_get_projects_list();62656266print$cgi->header(6267-type =>'text/xml',6268-charset =>'utf-8',6269-content_disposition =>'inline; filename="opml.xml"');62706271print<<XML;6272<?xml version="1.0" encoding="utf-8"?>6273<opml version="1.0">6274<head>6275 <title>$site_nameOPML Export</title>6276</head>6277<body>6278<outline text="git RSS feeds">6279XML62806281foreachmy$pr(@list) {6282my%proj=%$pr;6283my$head= git_get_head_hash($proj{'path'});6284if(!defined$head) {6285next;6286}6287$git_dir="$projectroot/$proj{'path'}";6288my%co= parse_commit($head);6289if(!%co) {6290next;6291}62926293my$path= esc_html(chop_str($proj{'path'},25,5));6294my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6295my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6296print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6297}6298print<<XML;6299</outline>6300</body>6301</opml>6302XML6303}