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# print out each stylesheet that exist2905if(defined$stylesheet) {2906#provides backwards capability for those people who define style sheet in a config file2907print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2908}else{2909foreachmy$stylesheet(@stylesheets) {2910next unless$stylesheet;2911print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2912}2913}2914if(defined$project) {2915my%href_params= get_feed_info();2916if(!exists$href_params{'-title'}) {2917$href_params{'-title'} ='log';2918}29192920foreachmy$formatqw(RSS Atom){2921my$type=lc($format);2922my%link_attr= (2923'-rel'=>'alternate',2924'-title'=>"$project-$href_params{'-title'} -$formatfeed",2925'-type'=>"application/$type+xml"2926);29272928$href_params{'action'} =$type;2929$link_attr{'-href'} = href(%href_params);2930print"<link ".2931"rel=\"$link_attr{'-rel'}\"".2932"title=\"$link_attr{'-title'}\"".2933"href=\"$link_attr{'-href'}\"".2934"type=\"$link_attr{'-type'}\"".2935"/>\n";29362937$href_params{'extra_options'} ='--no-merges';2938$link_attr{'-href'} = href(%href_params);2939$link_attr{'-title'} .=' (no merges)';2940print"<link ".2941"rel=\"$link_attr{'-rel'}\"".2942"title=\"$link_attr{'-title'}\"".2943"href=\"$link_attr{'-href'}\"".2944"type=\"$link_attr{'-type'}\"".2945"/>\n";2946}29472948}else{2949printf('<link rel="alternate" title="%sprojects list" '.2950'href="%s" type="text/plain; charset=utf-8" />'."\n",2951$site_name, href(project=>undef, action=>"project_index"));2952printf('<link rel="alternate" title="%sprojects feeds" '.2953'href="%s" type="text/x-opml" />'."\n",2954$site_name, href(project=>undef, action=>"opml"));2955}2956if(defined$favicon) {2957printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2958}29592960print"</head>\n".2961"<body>\n";29622963if(-f $site_header) {2964 insert_file($site_header);2965}29662967print"<div class=\"page_header\">\n".2968$cgi->a({-href => esc_url($logo_url),2969-title =>$logo_label},2970qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2971print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2972if(defined$project) {2973print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2974if(defined$action) {2975print" /$action";2976}2977print"\n";2978}2979print"</div>\n";29802981my$have_search= gitweb_check_feature('search');2982if(defined$project&&$have_search) {2983if(!defined$searchtext) {2984$searchtext="";2985}2986my$search_hash;2987if(defined$hash_base) {2988$search_hash=$hash_base;2989}elsif(defined$hash) {2990$search_hash=$hash;2991}else{2992$search_hash="HEAD";2993}2994my$action=$my_uri;2995my$use_pathinfo= gitweb_check_feature('pathinfo');2996if($use_pathinfo) {2997$action.="/".esc_url($project);2998}2999print$cgi->startform(-method=>"get", -action =>$action) .3000"<div class=\"search\">\n".3001(!$use_pathinfo&&3002$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3003$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3004$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3005$cgi->popup_menu(-name =>'st', -default=>'commit',3006-values=> ['commit','grep','author','committer','pickaxe']) .3007$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3008" search:\n",3009$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3010"<span title=\"Extended regular expression\">".3011$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3012-checked =>$search_use_regexp) .3013"</span>".3014"</div>".3015$cgi->end_form() ."\n";3016}3017}30183019sub git_footer_html {3020my$feed_class='rss_logo';30213022print"<div class=\"page_footer\">\n";3023if(defined$project) {3024my$descr= git_get_project_description($project);3025if(defined$descr) {3026print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3027}30283029my%href_params= get_feed_info();3030if(!%href_params) {3031$feed_class.=' generic';3032}3033$href_params{'-title'} ||='log';30343035foreachmy$formatqw(RSS Atom){3036$href_params{'action'} =lc($format);3037print$cgi->a({-href => href(%href_params),3038-title =>"$href_params{'-title'}$formatfeed",3039-class=>$feed_class},$format)."\n";3040}30413042}else{3043print$cgi->a({-href => href(project=>undef, action=>"opml"),3044-class=>$feed_class},"OPML") ." ";3045print$cgi->a({-href => href(project=>undef, action=>"project_index"),3046-class=>$feed_class},"TXT") ."\n";3047}3048print"</div>\n";# class="page_footer"30493050if(-f $site_footer) {3051 insert_file($site_footer);3052}30533054print"</body>\n".3055"</html>";3056}30573058# die_error(<http_status_code>, <error_message>)3059# Example: die_error(404, 'Hash not found')3060# By convention, use the following status codes (as defined in RFC 2616):3061# 400: Invalid or missing CGI parameters, or3062# requested object exists but has wrong type.3063# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3064# this server or project.3065# 404: Requested object/revision/project doesn't exist.3066# 500: The server isn't configured properly, or3067# an internal error occurred (e.g. failed assertions caused by bugs), or3068# an unknown error occurred (e.g. the git binary died unexpectedly).3069sub die_error {3070my$status=shift||500;3071my$error=shift||"Internal server error";30723073my%http_responses= (400=>'400 Bad Request',3074403=>'403 Forbidden',3075404=>'404 Not Found',3076500=>'500 Internal Server Error');3077 git_header_html($http_responses{$status});3078print<<EOF;3079<div class="page_body">3080<br /><br />3081$status-$error3082<br />3083</div>3084EOF3085 git_footer_html();3086exit;3087}30883089## ----------------------------------------------------------------------3090## functions printing or outputting HTML: navigation30913092sub git_print_page_nav {3093my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3094$extra=''if!defined$extra;# pager or formats30953096my@navs=qw(summary shortlog log commit commitdiff tree);3097if($suppress) {3098@navs=grep{$_ne$suppress}@navs;3099}31003101my%arg=map{$_=> {action=>$_} }@navs;3102if(defined$head) {3103for(qw(commit commitdiff)) {3104$arg{$_}{'hash'} =$head;3105}3106if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3107for(qw(shortlog log)) {3108$arg{$_}{'hash'} =$head;3109}3110}3111}31123113$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3114$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;31153116my@actions= gitweb_get_feature('actions');3117my%repl= (3118'%'=>'%',3119'n'=>$project,# project name3120'f'=>$git_dir,# project path within filesystem3121'h'=>$treehead||'',# current hash ('h' parameter)3122'b'=>$treebase||'',# hash base ('hb' parameter)3123);3124while(@actions) {3125my($label,$link,$pos) =splice(@actions,0,3);3126# insert3127@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3128# munch munch3129$link=~s/%([%nfhb])/$repl{$1}/g;3130$arg{$label}{'_href'} =$link;3131}31323133print"<div class=\"page_nav\">\n".3134(join" | ",3135map{$_eq$current?3136$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3137}@navs);3138print"<br/>\n$extra<br/>\n".3139"</div>\n";3140}31413142sub format_paging_nav {3143my($action,$hash,$head,$page,$has_next_link) =@_;3144my$paging_nav;314531463147if($hashne$head||$page) {3148$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3149}else{3150$paging_nav.="HEAD";3151}31523153if($page>0) {3154$paging_nav.=" ⋅ ".3155$cgi->a({-href => href(-replay=>1, page=>$page-1),3156-accesskey =>"p", -title =>"Alt-p"},"prev");3157}else{3158$paging_nav.=" ⋅ prev";3159}31603161if($has_next_link) {3162$paging_nav.=" ⋅ ".3163$cgi->a({-href => href(-replay=>1, page=>$page+1),3164-accesskey =>"n", -title =>"Alt-n"},"next");3165}else{3166$paging_nav.=" ⋅ next";3167}31683169return$paging_nav;3170}31713172## ......................................................................3173## functions printing or outputting HTML: div31743175sub git_print_header_div {3176my($action,$title,$hash,$hash_base) =@_;3177my%args= ();31783179$args{'action'} =$action;3180$args{'hash'} =$hashif$hash;3181$args{'hash_base'} =$hash_baseif$hash_base;31823183print"<div class=\"header\">\n".3184$cgi->a({-href => href(%args), -class=>"title"},3185$title?$title:$action) .3186"\n</div>\n";3187}31883189#sub git_print_authorship (\%) {3190sub git_print_authorship {3191my$co=shift;31923193my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3194print"<div class=\"author_date\">".3195 esc_html($co->{'author_name'}) .3196" [$ad{'rfc2822'}";3197if($ad{'hour_local'} <6) {3198printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3199$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3200}else{3201printf(" (%02d:%02d%s)",3202$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3203}3204print"]</div>\n";3205}32063207sub git_print_page_path {3208my$name=shift;3209my$type=shift;3210my$hb=shift;321132123213print"<div class=\"page_path\">";3214print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3215-title =>'tree root'}, to_utf8("[$project]"));3216print" / ";3217if(defined$name) {3218my@dirname=split'/',$name;3219my$basename=pop@dirname;3220my$fullname='';32213222foreachmy$dir(@dirname) {3223$fullname.= ($fullname?'/':'') .$dir;3224print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3225 hash_base=>$hb),3226-title =>$fullname}, esc_path($dir));3227print" / ";3228}3229if(defined$type&&$typeeq'blob') {3230print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3231 hash_base=>$hb),3232-title =>$name}, esc_path($basename));3233}elsif(defined$type&&$typeeq'tree') {3234print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3235 hash_base=>$hb),3236-title =>$name}, esc_path($basename));3237print" / ";3238}else{3239print esc_path($basename);3240}3241}3242print"<br/></div>\n";3243}32443245# sub git_print_log (\@;%) {3246sub git_print_log ($;%) {3247my$log=shift;3248my%opts=@_;32493250if($opts{'-remove_title'}) {3251# remove title, i.e. first line of log3252shift@$log;3253}3254# remove leading empty lines3255while(defined$log->[0] &&$log->[0]eq"") {3256shift@$log;3257}32583259# print log3260my$signoff=0;3261my$empty=0;3262foreachmy$line(@$log) {3263if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3264$signoff=1;3265$empty=0;3266if(!$opts{'-remove_signoff'}) {3267print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3268next;3269}else{3270# remove signoff lines3271next;3272}3273}else{3274$signoff=0;3275}32763277# print only one empty line3278# do not print empty line after signoff3279if($lineeq"") {3280next if($empty||$signoff);3281$empty=1;3282}else{3283$empty=0;3284}32853286print format_log_line_html($line) ."<br/>\n";3287}32883289if($opts{'-final_empty_line'}) {3290# end with single empty line3291print"<br/>\n"unless$empty;3292}3293}32943295# return link target (what link points to)3296sub git_get_link_target {3297my$hash=shift;3298my$link_target;32993300# read link3301open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3302orreturn;3303{3304local$/;3305$link_target= <$fd>;3306}3307close$fd3308orreturn;33093310return$link_target;3311}33123313# given link target, and the directory (basedir) the link is in,3314# return target of link relative to top directory (top tree);3315# return undef if it is not possible (including absolute links).3316sub normalize_link_target {3317my($link_target,$basedir,$hash_base) =@_;33183319# we can normalize symlink target only if $hash_base is provided3320return unless$hash_base;33213322# absolute symlinks (beginning with '/') cannot be normalized3323return if(substr($link_target,0,1)eq'/');33243325# normalize link target to path from top (root) tree (dir)3326my$path;3327if($basedir) {3328$path=$basedir.'/'.$link_target;3329}else{3330# we are in top (root) tree (dir)3331$path=$link_target;3332}33333334# remove //, /./, and /../3335my@path_parts;3336foreachmy$part(split('/',$path)) {3337# discard '.' and ''3338next if(!$part||$parteq'.');3339# handle '..'3340if($parteq'..') {3341if(@path_parts) {3342pop@path_parts;3343}else{3344# link leads outside repository (outside top dir)3345return;3346}3347}else{3348push@path_parts,$part;3349}3350}3351$path=join('/',@path_parts);33523353return$path;3354}33553356# print tree entry (row of git_tree), but without encompassing <tr> element3357sub git_print_tree_entry {3358my($t,$basedir,$hash_base,$have_blame) =@_;33593360my%base_key= ();3361$base_key{'hash_base'} =$hash_baseifdefined$hash_base;33623363# The format of a table row is: mode list link. Where mode is3364# the mode of the entry, list is the name of the entry, an href,3365# and link is the action links of the entry.33663367print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3368if($t->{'type'}eq"blob") {3369print"<td class=\"list\">".3370$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3371 file_name=>"$basedir$t->{'name'}",%base_key),3372-class=>"list"}, esc_path($t->{'name'}));3373if(S_ISLNK(oct$t->{'mode'})) {3374my$link_target= git_get_link_target($t->{'hash'});3375if($link_target) {3376my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3377if(defined$norm_target) {3378print" -> ".3379$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3380 file_name=>$norm_target),3381-title =>$norm_target}, esc_path($link_target));3382}else{3383print" -> ". esc_path($link_target);3384}3385}3386}3387print"</td>\n";3388print"<td class=\"link\">";3389print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3390 file_name=>"$basedir$t->{'name'}",%base_key)},3391"blob");3392if($have_blame) {3393print" | ".3394$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3395 file_name=>"$basedir$t->{'name'}",%base_key)},3396"blame");3397}3398if(defined$hash_base) {3399print" | ".3400$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3401 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3402"history");3403}3404print" | ".3405$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3406 file_name=>"$basedir$t->{'name'}")},3407"raw");3408print"</td>\n";34093410}elsif($t->{'type'}eq"tree") {3411print"<td class=\"list\">";3412print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3413 file_name=>"$basedir$t->{'name'}",%base_key)},3414 esc_path($t->{'name'}));3415print"</td>\n";3416print"<td class=\"link\">";3417print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3418 file_name=>"$basedir$t->{'name'}",%base_key)},3419"tree");3420if(defined$hash_base) {3421print" | ".3422$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3423 file_name=>"$basedir$t->{'name'}")},3424"history");3425}3426print"</td>\n";3427}else{3428# unknown object: we can only present history for it3429# (this includes 'commit' object, i.e. submodule support)3430print"<td class=\"list\">".3431 esc_path($t->{'name'}) .3432"</td>\n";3433print"<td class=\"link\">";3434if(defined$hash_base) {3435print$cgi->a({-href => href(action=>"history",3436 hash_base=>$hash_base,3437 file_name=>"$basedir$t->{'name'}")},3438"history");3439}3440print"</td>\n";3441}3442}34433444## ......................................................................3445## functions printing large fragments of HTML34463447# get pre-image filenames for merge (combined) diff3448sub fill_from_file_info {3449my($diff,@parents) =@_;34503451$diff->{'from_file'} = [ ];3452$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3453for(my$i=0;$i<$diff->{'nparents'};$i++) {3454if($diff->{'status'}[$i]eq'R'||3455$diff->{'status'}[$i]eq'C') {3456$diff->{'from_file'}[$i] =3457 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3458}3459}34603461return$diff;3462}34633464# is current raw difftree line of file deletion3465sub is_deleted {3466my$diffinfo=shift;34673468return$diffinfo->{'to_id'}eq('0' x 40);3469}34703471# does patch correspond to [previous] difftree raw line3472# $diffinfo - hashref of parsed raw diff format3473# $patchinfo - hashref of parsed patch diff format3474# (the same keys as in $diffinfo)3475sub is_patch_split {3476my($diffinfo,$patchinfo) =@_;34773478returndefined$diffinfo&&defined$patchinfo3479&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3480}348134823483sub git_difftree_body {3484my($difftree,$hash,@parents) =@_;3485my($parent) =$parents[0];3486my$have_blame= gitweb_check_feature('blame');3487print"<div class=\"list_head\">\n";3488if($#{$difftree} >10) {3489print(($#{$difftree} +1) ." files changed:\n");3490}3491print"</div>\n";34923493print"<table class=\"".3494(@parents>1?"combined ":"") .3495"diff_tree\">\n";34963497# header only for combined diff in 'commitdiff' view3498my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3499if($has_header) {3500# table header3501print"<thead><tr>\n".3502"<th></th><th></th>\n";# filename, patchN link3503for(my$i=0;$i<@parents;$i++) {3504my$par=$parents[$i];3505print"<th>".3506$cgi->a({-href => href(action=>"commitdiff",3507 hash=>$hash, hash_parent=>$par),3508-title =>'commitdiff to parent number '.3509($i+1) .': '.substr($par,0,7)},3510$i+1) .3511" </th>\n";3512}3513print"</tr></thead>\n<tbody>\n";3514}35153516my$alternate=1;3517my$patchno=0;3518foreachmy$line(@{$difftree}) {3519my$diff= parsed_difftree_line($line);35203521if($alternate) {3522print"<tr class=\"dark\">\n";3523}else{3524print"<tr class=\"light\">\n";3525}3526$alternate^=1;35273528if(exists$diff->{'nparents'}) {# combined diff35293530 fill_from_file_info($diff,@parents)3531unlessexists$diff->{'from_file'};35323533if(!is_deleted($diff)) {3534# file exists in the result (child) commit3535print"<td>".3536$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3537 file_name=>$diff->{'to_file'},3538 hash_base=>$hash),3539-class=>"list"}, esc_path($diff->{'to_file'})) .3540"</td>\n";3541}else{3542print"<td>".3543 esc_path($diff->{'to_file'}) .3544"</td>\n";3545}35463547if($actioneq'commitdiff') {3548# link to patch3549$patchno++;3550print"<td class=\"link\">".3551$cgi->a({-href =>"#patch$patchno"},"patch") .3552" | ".3553"</td>\n";3554}35553556my$has_history=0;3557my$not_deleted=0;3558for(my$i=0;$i<$diff->{'nparents'};$i++) {3559my$hash_parent=$parents[$i];3560my$from_hash=$diff->{'from_id'}[$i];3561my$from_path=$diff->{'from_file'}[$i];3562my$status=$diff->{'status'}[$i];35633564$has_history||= ($statusne'A');3565$not_deleted||= ($statusne'D');35663567if($statuseq'A') {3568print"<td class=\"link\"align=\"right\"> | </td>\n";3569}elsif($statuseq'D') {3570print"<td class=\"link\">".3571$cgi->a({-href => href(action=>"blob",3572 hash_base=>$hash,3573 hash=>$from_hash,3574 file_name=>$from_path)},3575"blob". ($i+1)) .3576" | </td>\n";3577}else{3578if($diff->{'to_id'}eq$from_hash) {3579print"<td class=\"link nochange\">";3580}else{3581print"<td class=\"link\">";3582}3583print$cgi->a({-href => href(action=>"blobdiff",3584 hash=>$diff->{'to_id'},3585 hash_parent=>$from_hash,3586 hash_base=>$hash,3587 hash_parent_base=>$hash_parent,3588 file_name=>$diff->{'to_file'},3589 file_parent=>$from_path)},3590"diff". ($i+1)) .3591" | </td>\n";3592}3593}35943595print"<td class=\"link\">";3596if($not_deleted) {3597print$cgi->a({-href => href(action=>"blob",3598 hash=>$diff->{'to_id'},3599 file_name=>$diff->{'to_file'},3600 hash_base=>$hash)},3601"blob");3602print" | "if($has_history);3603}3604if($has_history) {3605print$cgi->a({-href => href(action=>"history",3606 file_name=>$diff->{'to_file'},3607 hash_base=>$hash)},3608"history");3609}3610print"</td>\n";36113612print"</tr>\n";3613next;# instead of 'else' clause, to avoid extra indent3614}3615# else ordinary diff36163617my($to_mode_oct,$to_mode_str,$to_file_type);3618my($from_mode_oct,$from_mode_str,$from_file_type);3619if($diff->{'to_mode'}ne('0' x 6)) {3620$to_mode_oct=oct$diff->{'to_mode'};3621if(S_ISREG($to_mode_oct)) {# only for regular file3622$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3623}3624$to_file_type= file_type($diff->{'to_mode'});3625}3626if($diff->{'from_mode'}ne('0' x 6)) {3627$from_mode_oct=oct$diff->{'from_mode'};3628if(S_ISREG($to_mode_oct)) {# only for regular file3629$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3630}3631$from_file_type= file_type($diff->{'from_mode'});3632}36333634if($diff->{'status'}eq"A") {# created3635my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3636$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3637$mode_chng.="]</span>";3638print"<td>";3639print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3640 hash_base=>$hash, file_name=>$diff->{'file'}),3641-class=>"list"}, esc_path($diff->{'file'}));3642print"</td>\n";3643print"<td>$mode_chng</td>\n";3644print"<td class=\"link\">";3645if($actioneq'commitdiff') {3646# link to patch3647$patchno++;3648print$cgi->a({-href =>"#patch$patchno"},"patch");3649print" | ";3650}3651print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3652 hash_base=>$hash, file_name=>$diff->{'file'})},3653"blob");3654print"</td>\n";36553656}elsif($diff->{'status'}eq"D") {# deleted3657my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3658print"<td>";3659print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3660 hash_base=>$parent, file_name=>$diff->{'file'}),3661-class=>"list"}, esc_path($diff->{'file'}));3662print"</td>\n";3663print"<td>$mode_chng</td>\n";3664print"<td class=\"link\">";3665if($actioneq'commitdiff') {3666# link to patch3667$patchno++;3668print$cgi->a({-href =>"#patch$patchno"},"patch");3669print" | ";3670}3671print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3672 hash_base=>$parent, file_name=>$diff->{'file'})},3673"blob") ." | ";3674if($have_blame) {3675print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3676 file_name=>$diff->{'file'})},3677"blame") ." | ";3678}3679print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3680 file_name=>$diff->{'file'})},3681"history");3682print"</td>\n";36833684}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3685my$mode_chnge="";3686if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3687$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3688if($from_file_typene$to_file_type) {3689$mode_chnge.=" from$from_file_typeto$to_file_type";3690}3691if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3692if($from_mode_str&&$to_mode_str) {3693$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3694}elsif($to_mode_str) {3695$mode_chnge.=" mode:$to_mode_str";3696}3697}3698$mode_chnge.="]</span>\n";3699}3700print"<td>";3701print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3702 hash_base=>$hash, file_name=>$diff->{'file'}),3703-class=>"list"}, esc_path($diff->{'file'}));3704print"</td>\n";3705print"<td>$mode_chnge</td>\n";3706print"<td class=\"link\">";3707if($actioneq'commitdiff') {3708# link to patch3709$patchno++;3710print$cgi->a({-href =>"#patch$patchno"},"patch") .3711" | ";3712}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3713# "commit" view and modified file (not onlu mode changed)3714print$cgi->a({-href => href(action=>"blobdiff",3715 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3716 hash_base=>$hash, hash_parent_base=>$parent,3717 file_name=>$diff->{'file'})},3718"diff") .3719" | ";3720}3721print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3722 hash_base=>$hash, file_name=>$diff->{'file'})},3723"blob") ." | ";3724if($have_blame) {3725print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3726 file_name=>$diff->{'file'})},3727"blame") ." | ";3728}3729print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3730 file_name=>$diff->{'file'})},3731"history");3732print"</td>\n";37333734}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3735my%status_name= ('R'=>'moved','C'=>'copied');3736my$nstatus=$status_name{$diff->{'status'}};3737my$mode_chng="";3738if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3739# mode also for directories, so we cannot use $to_mode_str3740$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3741}3742print"<td>".3743$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3744 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3745-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3746"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3747$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3748 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3749-class=>"list"}, esc_path($diff->{'from_file'})) .3750" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3751"<td class=\"link\">";3752if($actioneq'commitdiff') {3753# link to patch3754$patchno++;3755print$cgi->a({-href =>"#patch$patchno"},"patch") .3756" | ";3757}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3758# "commit" view and modified file (not only pure rename or copy)3759print$cgi->a({-href => href(action=>"blobdiff",3760 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3761 hash_base=>$hash, hash_parent_base=>$parent,3762 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3763"diff") .3764" | ";3765}3766print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3767 hash_base=>$parent, file_name=>$diff->{'to_file'})},3768"blob") ." | ";3769if($have_blame) {3770print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3771 file_name=>$diff->{'to_file'})},3772"blame") ." | ";3773}3774print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3775 file_name=>$diff->{'to_file'})},3776"history");3777print"</td>\n";37783779}# we should not encounter Unmerged (U) or Unknown (X) status3780print"</tr>\n";3781}3782print"</tbody>"if$has_header;3783print"</table>\n";3784}37853786sub git_patchset_body {3787my($fd,$difftree,$hash,@hash_parents) =@_;3788my($hash_parent) =$hash_parents[0];37893790my$is_combined= (@hash_parents>1);3791my$patch_idx=0;3792my$patch_number=0;3793my$patch_line;3794my$diffinfo;3795my$to_name;3796my(%from,%to);37973798print"<div class=\"patchset\">\n";37993800# skip to first patch3801while($patch_line= <$fd>) {3802chomp$patch_line;38033804last if($patch_line=~m/^diff /);3805}38063807 PATCH:3808while($patch_line) {38093810# parse "git diff" header line3811if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3812# $1 is from_name, which we do not use3813$to_name= unquote($2);3814$to_name=~s!^b/!!;3815}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3816# $1 is 'cc' or 'combined', which we do not use3817$to_name= unquote($2);3818}else{3819$to_name=undef;3820}38213822# check if current patch belong to current raw line3823# and parse raw git-diff line if needed3824if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3825# this is continuation of a split patch3826print"<div class=\"patch cont\">\n";3827}else{3828# advance raw git-diff output if needed3829$patch_idx++ifdefined$diffinfo;38303831# read and prepare patch information3832$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38333834# compact combined diff output can have some patches skipped3835# find which patch (using pathname of result) we are at now;3836if($is_combined) {3837while($to_namene$diffinfo->{'to_file'}) {3838print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3839 format_diff_cc_simplified($diffinfo,@hash_parents) .3840"</div>\n";# class="patch"38413842$patch_idx++;3843$patch_number++;38443845last if$patch_idx>$#$difftree;3846$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3847}3848}38493850# modifies %from, %to hashes3851 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);38523853# this is first patch for raw difftree line with $patch_idx index3854# we index @$difftree array from 0, but number patches from 13855print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3856}38573858# git diff header3859#assert($patch_line =~ m/^diff /) if DEBUG;3860#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3861$patch_number++;3862# print "git diff" header3863print format_git_diff_header_line($patch_line,$diffinfo,3864 \%from, \%to);38653866# print extended diff header3867print"<div class=\"diff extended_header\">\n";3868 EXTENDED_HEADER:3869while($patch_line= <$fd>) {3870chomp$patch_line;38713872last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);38733874print format_extended_diff_header_line($patch_line,$diffinfo,3875 \%from, \%to);3876}3877print"</div>\n";# class="diff extended_header"38783879# from-file/to-file diff header3880if(!$patch_line) {3881print"</div>\n";# class="patch"3882last PATCH;3883}3884next PATCH if($patch_line=~m/^diff /);3885#assert($patch_line =~ m/^---/) if DEBUG;38863887my$last_patch_line=$patch_line;3888$patch_line= <$fd>;3889chomp$patch_line;3890#assert($patch_line =~ m/^\+\+\+/) if DEBUG;38913892print format_diff_from_to_header($last_patch_line,$patch_line,3893$diffinfo, \%from, \%to,3894@hash_parents);38953896# the patch itself3897 LINE:3898while($patch_line= <$fd>) {3899chomp$patch_line;39003901next PATCH if($patch_line=~m/^diff /);39023903print format_diff_line($patch_line, \%from, \%to);3904}39053906}continue{3907print"</div>\n";# class="patch"3908}39093910# for compact combined (--cc) format, with chunk and patch simpliciaction3911# patchset might be empty, but there might be unprocessed raw lines3912for(++$patch_idxif$patch_number>0;3913$patch_idx<@$difftree;3914++$patch_idx) {3915# read and prepare patch information3916$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);39173918# generate anchor for "patch" links in difftree / whatchanged part3919print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3920 format_diff_cc_simplified($diffinfo,@hash_parents) .3921"</div>\n";# class="patch"39223923$patch_number++;3924}39253926if($patch_number==0) {3927if(@hash_parents>1) {3928print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3929}else{3930print"<div class=\"diff nodifferences\">No differences found</div>\n";3931}3932}39333934print"</div>\n";# class="patchset"3935}39363937# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39383939# fills project list info (age, description, owner, forks) for each3940# project in the list, removing invalid projects from returned list3941# NOTE: modifies $projlist, but does not remove entries from it3942sub fill_project_list_info {3943my($projlist,$check_forks) =@_;3944my@projects;39453946my$show_ctags= gitweb_check_feature('ctags');3947 PROJECT:3948foreachmy$pr(@$projlist) {3949my(@activity) = git_get_last_activity($pr->{'path'});3950unless(@activity) {3951next PROJECT;3952}3953($pr->{'age'},$pr->{'age_string'}) =@activity;3954if(!defined$pr->{'descr'}) {3955my$descr= git_get_project_description($pr->{'path'}) ||"";3956$descr= to_utf8($descr);3957$pr->{'descr_long'} =$descr;3958$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3959}3960if(!defined$pr->{'owner'}) {3961$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3962}3963if($check_forks) {3964my$pname=$pr->{'path'};3965if(($pname=~s/\.git$//) &&3966($pname!~/\/$/) &&3967(-d "$projectroot/$pname")) {3968$pr->{'forks'} ="-d$projectroot/$pname";3969}else{3970$pr->{'forks'} =0;3971}3972}3973$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3974push@projects,$pr;3975}39763977return@projects;3978}39793980# print 'sort by' <th> element, generating 'sort by $name' replay link3981# if that order is not selected3982sub print_sort_th {3983my($name,$order,$header) =@_;3984$header||=ucfirst($name);39853986if($ordereq$name) {3987print"<th>$header</th>\n";3988}else{3989print"<th>".3990$cgi->a({-href => href(-replay=>1, order=>$name),3991-class=>"header"},$header) .3992"</th>\n";3993}3994}39953996sub git_project_list_body {3997# actually uses global variable $project3998my($projlist,$order,$from,$to,$extra,$no_header) =@_;39994000my$check_forks= gitweb_check_feature('forks');4001my@projects= fill_project_list_info($projlist,$check_forks);40024003$order||=$default_projects_order;4004$from=0unlessdefined$from;4005$to=$#projectsif(!defined$to||$#projects<$to);40064007my%order_info= (4008 project => { key =>'path', type =>'str'},4009 descr => { key =>'descr_long', type =>'str'},4010 owner => { key =>'owner', type =>'str'},4011 age => { key =>'age', type =>'num'}4012);4013my$oi=$order_info{$order};4014if($oi->{'type'}eq'str') {4015@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4016}else{4017@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4018}40194020my$show_ctags= gitweb_check_feature('ctags');4021if($show_ctags) {4022my%ctags;4023foreachmy$p(@projects) {4024foreachmy$ct(keys%{$p->{'ctags'}}) {4025$ctags{$ct} +=$p->{'ctags'}->{$ct};4026}4027}4028my$cloud= git_populate_project_tagcloud(\%ctags);4029print git_show_project_tagcloud($cloud,64);4030}40314032print"<table class=\"project_list\">\n";4033unless($no_header) {4034print"<tr>\n";4035if($check_forks) {4036print"<th></th>\n";4037}4038 print_sort_th('project',$order,'Project');4039 print_sort_th('descr',$order,'Description');4040 print_sort_th('owner',$order,'Owner');4041 print_sort_th('age',$order,'Last Change');4042print"<th></th>\n".# for links4043"</tr>\n";4044}4045my$alternate=1;4046my$tagfilter=$cgi->param('by_tag');4047for(my$i=$from;$i<=$to;$i++) {4048my$pr=$projects[$i];40494050next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4051next if$searchtextand not$pr->{'path'} =~/$searchtext/4052and not$pr->{'descr_long'} =~/$searchtext/;4053# Weed out forks or non-matching entries of search4054if($check_forks) {4055my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4056$forkbase="^$forkbase"if$forkbase;4057next ifnot$searchtextand not$tagfilterand$show_ctags4058and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4059}40604061if($alternate) {4062print"<tr class=\"dark\">\n";4063}else{4064print"<tr class=\"light\">\n";4065}4066$alternate^=1;4067if($check_forks) {4068print"<td>";4069if($pr->{'forks'}) {4070print"<!--$pr->{'forks'} -->\n";4071print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4072}4073print"</td>\n";4074}4075print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4076-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4077"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4078-class=>"list", -title =>$pr->{'descr_long'}},4079 esc_html($pr->{'descr'})) ."</td>\n".4080"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4081print"<td class=\"". age_class($pr->{'age'}) ."\">".4082(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4083"<td class=\"link\">".4084$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4085$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4086$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4087$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4088($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4089"</td>\n".4090"</tr>\n";4091}4092if(defined$extra) {4093print"<tr>\n";4094if($check_forks) {4095print"<td></td>\n";4096}4097print"<td colspan=\"5\">$extra</td>\n".4098"</tr>\n";4099}4100print"</table>\n";4101}41024103sub git_shortlog_body {4104# uses global variable $project4105my($commitlist,$from,$to,$refs,$extra) =@_;41064107$from=0unlessdefined$from;4108$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41094110print"<table class=\"shortlog\">\n";4111my$alternate=1;4112for(my$i=$from;$i<=$to;$i++) {4113my%co= %{$commitlist->[$i]};4114my$commit=$co{'id'};4115my$ref= format_ref_marker($refs,$commit);4116if($alternate) {4117print"<tr class=\"dark\">\n";4118}else{4119print"<tr class=\"light\">\n";4120}4121$alternate^=1;4122my$author= chop_and_escape_str($co{'author_name'},10);4123# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4124print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4125"<td><i>".$author."</i></td>\n".4126"<td>";4127print format_subject_html($co{'title'},$co{'title_short'},4128 href(action=>"commit", hash=>$commit),$ref);4129print"</td>\n".4130"<td class=\"link\">".4131$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4132$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4133$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4134my$snapshot_links= format_snapshot_links($commit);4135if(defined$snapshot_links) {4136print" | ".$snapshot_links;4137}4138print"</td>\n".4139"</tr>\n";4140}4141if(defined$extra) {4142print"<tr>\n".4143"<td colspan=\"4\">$extra</td>\n".4144"</tr>\n";4145}4146print"</table>\n";4147}41484149sub git_history_body {4150# Warning: assumes constant type (blob or tree) during history4151my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;41524153$from=0unlessdefined$from;4154$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});41554156print"<table class=\"history\">\n";4157my$alternate=1;4158for(my$i=$from;$i<=$to;$i++) {4159my%co= %{$commitlist->[$i]};4160if(!%co) {4161next;4162}4163my$commit=$co{'id'};41644165my$ref= format_ref_marker($refs,$commit);41664167if($alternate) {4168print"<tr class=\"dark\">\n";4169}else{4170print"<tr class=\"light\">\n";4171}4172$alternate^=1;4173# shortlog uses chop_str($co{'author_name'}, 10)4174my$author= chop_and_escape_str($co{'author_name'},15,3);4175print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4176"<td><i>".$author."</i></td>\n".4177"<td>";4178# originally git_history used chop_str($co{'title'}, 50)4179print format_subject_html($co{'title'},$co{'title_short'},4180 href(action=>"commit", hash=>$commit),$ref);4181print"</td>\n".4182"<td class=\"link\">".4183$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4184$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");41854186if($ftypeeq'blob') {4187my$blob_current= git_get_hash_by_path($hash_base,$file_name);4188my$blob_parent= git_get_hash_by_path($commit,$file_name);4189if(defined$blob_current&&defined$blob_parent&&4190$blob_currentne$blob_parent) {4191print" | ".4192$cgi->a({-href => href(action=>"blobdiff",4193 hash=>$blob_current, hash_parent=>$blob_parent,4194 hash_base=>$hash_base, hash_parent_base=>$commit,4195 file_name=>$file_name)},4196"diff to current");4197}4198}4199print"</td>\n".4200"</tr>\n";4201}4202if(defined$extra) {4203print"<tr>\n".4204"<td colspan=\"4\">$extra</td>\n".4205"</tr>\n";4206}4207print"</table>\n";4208}42094210sub git_tags_body {4211# uses global variable $project4212my($taglist,$from,$to,$extra) =@_;4213$from=0unlessdefined$from;4214$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);42154216print"<table class=\"tags\">\n";4217my$alternate=1;4218for(my$i=$from;$i<=$to;$i++) {4219my$entry=$taglist->[$i];4220my%tag=%$entry;4221my$comment=$tag{'subject'};4222my$comment_short;4223if(defined$comment) {4224$comment_short= chop_str($comment,30,5);4225}4226if($alternate) {4227print"<tr class=\"dark\">\n";4228}else{4229print"<tr class=\"light\">\n";4230}4231$alternate^=1;4232if(defined$tag{'age'}) {4233print"<td><i>$tag{'age'}</i></td>\n";4234}else{4235print"<td></td>\n";4236}4237print"<td>".4238$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4239-class=>"list name"}, esc_html($tag{'name'})) .4240"</td>\n".4241"<td>";4242if(defined$comment) {4243print format_subject_html($comment,$comment_short,4244 href(action=>"tag", hash=>$tag{'id'}));4245}4246print"</td>\n".4247"<td class=\"selflink\">";4248if($tag{'type'}eq"tag") {4249print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4250}else{4251print" ";4252}4253print"</td>\n".4254"<td class=\"link\">"." | ".4255$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4256if($tag{'reftype'}eq"commit") {4257print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4258" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4259}elsif($tag{'reftype'}eq"blob") {4260print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4261}4262print"</td>\n".4263"</tr>";4264}4265if(defined$extra) {4266print"<tr>\n".4267"<td colspan=\"5\">$extra</td>\n".4268"</tr>\n";4269}4270print"</table>\n";4271}42724273sub git_heads_body {4274# uses global variable $project4275my($headlist,$head,$from,$to,$extra) =@_;4276$from=0unlessdefined$from;4277$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);42784279print"<table class=\"heads\">\n";4280my$alternate=1;4281for(my$i=$from;$i<=$to;$i++) {4282my$entry=$headlist->[$i];4283my%ref=%$entry;4284my$curr=$ref{'id'}eq$head;4285if($alternate) {4286print"<tr class=\"dark\">\n";4287}else{4288print"<tr class=\"light\">\n";4289}4290$alternate^=1;4291print"<td><i>$ref{'age'}</i></td>\n".4292($curr?"<td class=\"current_head\">":"<td>") .4293$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4294-class=>"list name"},esc_html($ref{'name'})) .4295"</td>\n".4296"<td class=\"link\">".4297$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4298$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4299$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4300"</td>\n".4301"</tr>";4302}4303if(defined$extra) {4304print"<tr>\n".4305"<td colspan=\"3\">$extra</td>\n".4306"</tr>\n";4307}4308print"</table>\n";4309}43104311sub git_search_grep_body {4312my($commitlist,$from,$to,$extra) =@_;4313$from=0unlessdefined$from;4314$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43154316print"<table class=\"commit_search\">\n";4317my$alternate=1;4318for(my$i=$from;$i<=$to;$i++) {4319my%co= %{$commitlist->[$i]};4320if(!%co) {4321next;4322}4323my$commit=$co{'id'};4324if($alternate) {4325print"<tr class=\"dark\">\n";4326}else{4327print"<tr class=\"light\">\n";4328}4329$alternate^=1;4330my$author= chop_and_escape_str($co{'author_name'},15,5);4331print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4332"<td><i>".$author."</i></td>\n".4333"<td>".4334$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4335-class=>"list subject"},4336 chop_and_escape_str($co{'title'},50) ."<br/>");4337my$comment=$co{'comment'};4338foreachmy$line(@$comment) {4339if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4340my($lead,$match,$trail) = ($1,$2,$3);4341$match= chop_str($match,70,5,'center');4342my$contextlen=int((80-length($match))/2);4343$contextlen=30if($contextlen>30);4344$lead= chop_str($lead,$contextlen,10,'left');4345$trail= chop_str($trail,$contextlen,10,'right');43464347$lead= esc_html($lead);4348$match= esc_html($match);4349$trail= esc_html($trail);43504351print"$lead<span class=\"match\">$match</span>$trail<br />";4352}4353}4354print"</td>\n".4355"<td class=\"link\">".4356$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4357" | ".4358$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4359" | ".4360$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4361print"</td>\n".4362"</tr>\n";4363}4364if(defined$extra) {4365print"<tr>\n".4366"<td colspan=\"3\">$extra</td>\n".4367"</tr>\n";4368}4369print"</table>\n";4370}43714372## ======================================================================4373## ======================================================================4374## actions43754376sub git_project_list {4377my$order=$input_params{'order'};4378if(defined$order&&$order!~m/none|project|descr|owner|age/) {4379 die_error(400,"Unknown order parameter");4380}43814382my@list= git_get_projects_list();4383if(!@list) {4384 die_error(404,"No projects found");4385}43864387 git_header_html();4388if(-f $home_text) {4389print"<div class=\"index_include\">\n";4390 insert_file($home_text);4391print"</div>\n";4392}4393print$cgi->startform(-method=>"get") .4394"<p class=\"projsearch\">Search:\n".4395$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4396"</p>".4397$cgi->end_form() ."\n";4398 git_project_list_body(\@list,$order);4399 git_footer_html();4400}44014402sub git_forks {4403my$order=$input_params{'order'};4404if(defined$order&&$order!~m/none|project|descr|owner|age/) {4405 die_error(400,"Unknown order parameter");4406}44074408my@list= git_get_projects_list($project);4409if(!@list) {4410 die_error(404,"No forks found");4411}44124413 git_header_html();4414 git_print_page_nav('','');4415 git_print_header_div('summary',"$projectforks");4416 git_project_list_body(\@list,$order);4417 git_footer_html();4418}44194420sub git_project_index {4421my@projects= git_get_projects_list($project);44224423print$cgi->header(4424-type =>'text/plain',4425-charset =>'utf-8',4426-content_disposition =>'inline; filename="index.aux"');44274428foreachmy$pr(@projects) {4429if(!exists$pr->{'owner'}) {4430$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4431}44324433my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4434# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4435$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4436$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4437$path=~s/ /\+/g;4438$owner=~s/ /\+/g;44394440print"$path$owner\n";4441}4442}44434444sub git_summary {4445my$descr= git_get_project_description($project) ||"none";4446my%co= parse_commit("HEAD");4447my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4448my$head=$co{'id'};44494450my$owner= git_get_project_owner($project);44514452my$refs= git_get_references();4453# These get_*_list functions return one more to allow us to see if4454# there are more ...4455my@taglist= git_get_tags_list(16);4456my@headlist= git_get_heads_list(16);4457my@forklist;4458my$check_forks= gitweb_check_feature('forks');44594460if($check_forks) {4461@forklist= git_get_projects_list($project);4462}44634464 git_header_html();4465 git_print_page_nav('summary','',$head);44664467print"<div class=\"title\"> </div>\n";4468print"<table class=\"projects_list\">\n".4469"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4470"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4471if(defined$cd{'rfc2822'}) {4472print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4473}44744475# use per project git URL list in $projectroot/$project/cloneurl4476# or make project git URL from git base URL and project name4477my$url_tag="URL";4478my@url_list= git_get_project_url_list($project);4479@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4480foreachmy$git_url(@url_list) {4481next unless$git_url;4482print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4483$url_tag="";4484}44854486# Tag cloud4487my$show_ctags= gitweb_check_feature('ctags');4488if($show_ctags) {4489my$ctags= git_get_project_ctags($project);4490my$cloud= git_populate_project_tagcloud($ctags);4491print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4492print"</td>\n<td>"unless%$ctags;4493print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4494print"</td>\n<td>"if%$ctags;4495print git_show_project_tagcloud($cloud,48);4496print"</td></tr>";4497}44984499print"</table>\n";45004501if(-s "$projectroot/$project/README.html") {4502print"<div class=\"title\">readme</div>\n".4503"<div class=\"readme\">\n";4504 insert_file("$projectroot/$project/README.html");4505print"\n</div>\n";# class="readme"4506}45074508# we need to request one more than 16 (0..15) to check if4509# those 16 are all4510my@commitlist=$head? parse_commits($head,17) : ();4511if(@commitlist) {4512 git_print_header_div('shortlog');4513 git_shortlog_body(\@commitlist,0,15,$refs,4514$#commitlist<=15?undef:4515$cgi->a({-href => href(action=>"shortlog")},"..."));4516}45174518if(@taglist) {4519 git_print_header_div('tags');4520 git_tags_body(\@taglist,0,15,4521$#taglist<=15?undef:4522$cgi->a({-href => href(action=>"tags")},"..."));4523}45244525if(@headlist) {4526 git_print_header_div('heads');4527 git_heads_body(\@headlist,$head,0,15,4528$#headlist<=15?undef:4529$cgi->a({-href => href(action=>"heads")},"..."));4530}45314532if(@forklist) {4533 git_print_header_div('forks');4534 git_project_list_body(\@forklist,'age',0,15,4535$#forklist<=15?undef:4536$cgi->a({-href => href(action=>"forks")},"..."),4537'no_header');4538}45394540 git_footer_html();4541}45424543sub git_tag {4544my$head= git_get_head_hash($project);4545 git_header_html();4546 git_print_page_nav('','',$head,undef,$head);4547my%tag= parse_tag($hash);45484549if(!%tag) {4550 die_error(404,"Unknown tag object");4551}45524553 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4554print"<div class=\"title_text\">\n".4555"<table class=\"object_header\">\n".4556"<tr>\n".4557"<td>object</td>\n".4558"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4559$tag{'object'}) ."</td>\n".4560"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4561$tag{'type'}) ."</td>\n".4562"</tr>\n";4563if(defined($tag{'author'})) {4564my%ad= parse_date($tag{'epoch'},$tag{'tz'});4565print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4566print"<tr><td></td><td>".$ad{'rfc2822'} .4567sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4568"</td></tr>\n";4569}4570print"</table>\n\n".4571"</div>\n";4572print"<div class=\"page_body\">";4573my$comment=$tag{'comment'};4574foreachmy$line(@$comment) {4575chomp$line;4576print esc_html($line, -nbsp=>1) ."<br/>\n";4577}4578print"</div>\n";4579 git_footer_html();4580}45814582sub git_blame {4583# permissions4584 gitweb_check_feature('blame')4585or die_error(403,"Blame view not allowed");45864587# error checking4588 die_error(400,"No file name given")unless$file_name;4589$hash_base||= git_get_head_hash($project);4590 die_error(404,"Couldn't find base commit")unless$hash_base;4591my%co= parse_commit($hash_base)4592or die_error(404,"Commit not found");4593my$ftype="blob";4594if(!defined$hash) {4595$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4596or die_error(404,"Error looking up file");4597}else{4598$ftype= git_get_type($hash);4599if($ftype!~"blob") {4600 die_error(400,"Object is not a blob");4601}4602}46034604# run git-blame --porcelain4605open my$fd,"-|", git_cmd(),"blame",'-p',4606$hash_base,'--',$file_name4607or die_error(500,"Open git-blame failed");46084609# page header4610 git_header_html();4611my$formats_nav=4612$cgi->a({-href => href(action=>"blob", -replay=>1)},4613"blob") .4614" | ".4615$cgi->a({-href => href(action=>"history", -replay=>1)},4616"history") .4617" | ".4618$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4619"HEAD");4620 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4621 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4622 git_print_page_path($file_name,$ftype,$hash_base);46234624# page body4625my@rev_color=qw(light2 dark2);4626my$num_colors=scalar(@rev_color);4627my$current_color=0;4628my%metainfo= ();46294630print<<HTML;4631<div class="page_body">4632<table class="blame">4633<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4634HTML4635 LINE:4636while(my$line= <$fd>) {4637chomp$line;4638# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4639# no <lines in group> for subsequent lines in group of lines4640my($full_rev,$orig_lineno,$lineno,$group_size) =4641($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4642if(!exists$metainfo{$full_rev}) {4643$metainfo{$full_rev} = {};4644}4645my$meta=$metainfo{$full_rev};4646my$data;4647while($data= <$fd>) {4648chomp$data;4649last if($data=~s/^\t//);# contents of line4650if($data=~/^(\S+) (.*)$/) {4651$meta->{$1} =$2;4652}4653}4654my$short_rev=substr($full_rev,0,8);4655my$author=$meta->{'author'};4656my%date=4657 parse_date($meta->{'author-time'},$meta->{'author-tz'});4658my$date=$date{'iso-tz'};4659if($group_size) {4660$current_color= ($current_color+1) %$num_colors;4661}4662print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4663if($group_size) {4664print"<td class=\"sha1\"";4665print" title=\"". esc_html($author) .",$date\"";4666print" rowspan=\"$group_size\""if($group_size>1);4667print">";4668print$cgi->a({-href => href(action=>"commit",4669 hash=>$full_rev,4670 file_name=>$file_name)},4671 esc_html($short_rev));4672print"</td>\n";4673}4674my$parent_commit;4675if(!exists$meta->{'parent'}) {4676open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4677or die_error(500,"Open git-rev-parse failed");4678$parent_commit= <$dd>;4679close$dd;4680chomp($parent_commit);4681$meta->{'parent'} =$parent_commit;4682}else{4683$parent_commit=$meta->{'parent'};4684}4685my$blamed= href(action =>'blame',4686 file_name =>$meta->{'filename'},4687 hash_base =>$parent_commit);4688print"<td class=\"linenr\">";4689print$cgi->a({ -href =>"$blamed#l$orig_lineno",4690-class=>"linenr"},4691 esc_html($lineno));4692print"</td>";4693print"<td class=\"pre\">". esc_html($data) ."</td>\n";4694print"</tr>\n";4695}4696print"</table>\n";4697print"</div>";4698close$fd4699or print"Reading blob failed\n";47004701# page footer4702 git_footer_html();4703}47044705sub git_tags {4706my$head= git_get_head_hash($project);4707 git_header_html();4708 git_print_page_nav('','',$head,undef,$head);4709 git_print_header_div('summary',$project);47104711my@tagslist= git_get_tags_list();4712if(@tagslist) {4713 git_tags_body(\@tagslist);4714}4715 git_footer_html();4716}47174718sub git_heads {4719my$head= git_get_head_hash($project);4720 git_header_html();4721 git_print_page_nav('','',$head,undef,$head);4722 git_print_header_div('summary',$project);47234724my@headslist= git_get_heads_list();4725if(@headslist) {4726 git_heads_body(\@headslist,$head);4727}4728 git_footer_html();4729}47304731sub git_blob_plain {4732my$type=shift;4733my$expires;47344735if(!defined$hash) {4736if(defined$file_name) {4737my$base=$hash_base|| git_get_head_hash($project);4738$hash= git_get_hash_by_path($base,$file_name,"blob")4739or die_error(404,"Cannot find file");4740}else{4741 die_error(400,"No file name defined");4742}4743}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4744# blobs defined by non-textual hash id's can be cached4745$expires="+1d";4746}47474748open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4749or die_error(500,"Open git-cat-file blob '$hash' failed");47504751# content-type (can include charset)4752$type= blob_contenttype($fd,$file_name,$type);47534754# "save as" filename, even when no $file_name is given4755my$save_as="$hash";4756if(defined$file_name) {4757$save_as=$file_name;4758}elsif($type=~m/^text\//) {4759$save_as.='.txt';4760}47614762print$cgi->header(4763-type =>$type,4764-expires =>$expires,4765-content_disposition =>'inline; filename="'.$save_as.'"');4766undef$/;4767binmode STDOUT,':raw';4768print<$fd>;4769binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4770$/="\n";4771close$fd;4772}47734774sub git_blob {4775my$expires;47764777if(!defined$hash) {4778if(defined$file_name) {4779my$base=$hash_base|| git_get_head_hash($project);4780$hash= git_get_hash_by_path($base,$file_name,"blob")4781or die_error(404,"Cannot find file");4782}else{4783 die_error(400,"No file name defined");4784}4785}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4786# blobs defined by non-textual hash id's can be cached4787$expires="+1d";4788}47894790my$have_blame= gitweb_check_feature('blame');4791open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4792or die_error(500,"Couldn't cat$file_name,$hash");4793my$mimetype= blob_mimetype($fd,$file_name);4794if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4795close$fd;4796return git_blob_plain($mimetype);4797}4798# we can have blame only for text/* mimetype4799$have_blame&&= ($mimetype=~m!^text/!);48004801 git_header_html(undef,$expires);4802my$formats_nav='';4803if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4804if(defined$file_name) {4805if($have_blame) {4806$formats_nav.=4807$cgi->a({-href => href(action=>"blame", -replay=>1)},4808"blame") .4809" | ";4810}4811$formats_nav.=4812$cgi->a({-href => href(action=>"history", -replay=>1)},4813"history") .4814" | ".4815$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4816"raw") .4817" | ".4818$cgi->a({-href => href(action=>"blob",4819 hash_base=>"HEAD", file_name=>$file_name)},4820"HEAD");4821}else{4822$formats_nav.=4823$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4824"raw");4825}4826 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4827 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4828}else{4829print"<div class=\"page_nav\">\n".4830"<br/><br/></div>\n".4831"<div class=\"title\">$hash</div>\n";4832}4833 git_print_page_path($file_name,"blob",$hash_base);4834print"<div class=\"page_body\">\n";4835if($mimetype=~m!^image/!) {4836print qq!<img type="$mimetype"!;4837if($file_name) {4838print qq! alt="$file_name" title="$file_name"!;4839}4840print qq! src="! .4841 href(action=>"blob_plain", hash=>$hash,4842 hash_base=>$hash_base, file_name=>$file_name) .4843 qq!"/>\n!;4844}else{4845my$nr;4846while(my$line= <$fd>) {4847chomp$line;4848$nr++;4849$line= untabify($line);4850printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4851$nr,$nr,$nr, esc_html($line, -nbsp=>1);4852}4853}4854close$fd4855or print"Reading blob failed.\n";4856print"</div>";4857 git_footer_html();4858}48594860sub git_tree {4861if(!defined$hash_base) {4862$hash_base="HEAD";4863}4864if(!defined$hash) {4865if(defined$file_name) {4866$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4867}else{4868$hash=$hash_base;4869}4870}4871 die_error(404,"No such tree")unlessdefined($hash);4872$/="\0";4873open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4874or die_error(500,"Open git-ls-tree failed");4875my@entries=map{chomp;$_} <$fd>;4876close$fdor die_error(404,"Reading tree failed");4877$/="\n";48784879my$refs= git_get_references();4880my$ref= format_ref_marker($refs,$hash_base);4881 git_header_html();4882my$basedir='';4883my$have_blame= gitweb_check_feature('blame');4884if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4885my@views_nav= ();4886if(defined$file_name) {4887push@views_nav,4888$cgi->a({-href => href(action=>"history", -replay=>1)},4889"history"),4890$cgi->a({-href => href(action=>"tree",4891 hash_base=>"HEAD", file_name=>$file_name)},4892"HEAD"),4893}4894my$snapshot_links= format_snapshot_links($hash);4895if(defined$snapshot_links) {4896# FIXME: Should be available when we have no hash base as well.4897push@views_nav,$snapshot_links;4898}4899 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4900 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4901}else{4902undef$hash_base;4903print"<div class=\"page_nav\">\n";4904print"<br/><br/></div>\n";4905print"<div class=\"title\">$hash</div>\n";4906}4907if(defined$file_name) {4908$basedir=$file_name;4909if($basedirne''&&substr($basedir, -1)ne'/') {4910$basedir.='/';4911}4912 git_print_page_path($file_name,'tree',$hash_base);4913}4914print"<div class=\"page_body\">\n";4915print"<table class=\"tree\">\n";4916my$alternate=1;4917# '..' (top directory) link if possible4918if(defined$hash_base&&4919defined$file_name&&$file_name=~m![^/]+$!) {4920if($alternate) {4921print"<tr class=\"dark\">\n";4922}else{4923print"<tr class=\"light\">\n";4924}4925$alternate^=1;49264927my$up=$file_name;4928$up=~s!/?[^/]+$!!;4929undef$upunless$up;4930# based on git_print_tree_entry4931print'<td class="mode">'. mode_str('040000') ."</td>\n";4932print'<td class="list">';4933print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4934 file_name=>$up)},4935"..");4936print"</td>\n";4937print"<td class=\"link\"></td>\n";49384939print"</tr>\n";4940}4941foreachmy$line(@entries) {4942my%t= parse_ls_tree_line($line, -z =>1);49434944if($alternate) {4945print"<tr class=\"dark\">\n";4946}else{4947print"<tr class=\"light\">\n";4948}4949$alternate^=1;49504951 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);49524953print"</tr>\n";4954}4955print"</table>\n".4956"</div>";4957 git_footer_html();4958}49594960sub git_snapshot {4961my$format=$input_params{'snapshot_format'};4962if(!@snapshot_fmts) {4963 die_error(403,"Snapshots not allowed");4964}4965# default to first supported snapshot format4966$format||=$snapshot_fmts[0];4967if($format!~m/^[a-z0-9]+$/) {4968 die_error(400,"Invalid snapshot format parameter");4969}elsif(!exists($known_snapshot_formats{$format})) {4970 die_error(400,"Unknown snapshot format");4971}elsif(!grep($_eq$format,@snapshot_fmts)) {4972 die_error(403,"Unsupported snapshot format");4973}49744975if(!defined$hash) {4976$hash= git_get_head_hash($project);4977}49784979my$name=$project;4980$name=~ s,([^/])/*\.git$,$1,;4981$name= basename($name);4982my$filename= to_utf8($name);4983$name=~s/\047/\047\\\047\047/g;4984my$cmd;4985$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";4986$cmd= quote_command(4987 git_cmd(),'archive',4988"--format=$known_snapshot_formats{$format}{'format'}",4989"--prefix=$name/",$hash);4990if(exists$known_snapshot_formats{$format}{'compressor'}) {4991$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});4992}49934994print$cgi->header(4995-type =>$known_snapshot_formats{$format}{'type'},4996-content_disposition =>'inline; filename="'."$filename".'"',4997-status =>'200 OK');49984999open my$fd,"-|",$cmd5000or die_error(500,"Execute git-archive failed");5001binmode STDOUT,':raw';5002print<$fd>;5003binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5004close$fd;5005}50065007sub git_log {5008my$head= git_get_head_hash($project);5009if(!defined$hash) {5010$hash=$head;5011}5012if(!defined$page) {5013$page=0;5014}5015my$refs= git_get_references();50165017my@commitlist= parse_commits($hash,101, (100*$page));50185019my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);50205021my($patch_max) = gitweb_get_feature('patches');5022if($patch_max) {5023if($patch_max<0||@commitlist<=$patch_max) {5024$paging_nav.=" ⋅ ".5025$cgi->a({-href => href(action=>"patches", -replay=>1)},5026"patches");5027}5028}50295030 git_header_html();5031 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);50325033if(!@commitlist) {5034my%co= parse_commit($hash);50355036 git_print_header_div('summary',$project);5037print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5038}5039my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5040for(my$i=0;$i<=$to;$i++) {5041my%co= %{$commitlist[$i]};5042next if!%co;5043my$commit=$co{'id'};5044my$ref= format_ref_marker($refs,$commit);5045my%ad= parse_date($co{'author_epoch'});5046 git_print_header_div('commit',5047"<span class=\"age\">$co{'age_string'}</span>".5048 esc_html($co{'title'}) .$ref,5049$commit);5050print"<div class=\"title_text\">\n".5051"<div class=\"log_link\">\n".5052$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5053" | ".5054$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5055" | ".5056$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5057"<br/>\n".5058"</div>\n".5059"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".5060"</div>\n";50615062print"<div class=\"log_body\">\n";5063 git_print_log($co{'comment'}, -final_empty_line=>1);5064print"</div>\n";5065}5066if($#commitlist>=100) {5067print"<div class=\"page_nav\">\n";5068print$cgi->a({-href => href(-replay=>1, page=>$page+1),5069-accesskey =>"n", -title =>"Alt-n"},"next");5070print"</div>\n";5071}5072 git_footer_html();5073}50745075sub git_commit {5076$hash||=$hash_base||"HEAD";5077my%co= parse_commit($hash)5078or die_error(404,"Unknown commit object");5079my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5080my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});50815082my$parent=$co{'parent'};5083my$parents=$co{'parents'};# listref50845085# we need to prepare $formats_nav before any parameter munging5086my$formats_nav;5087if(!defined$parent) {5088# --root commitdiff5089$formats_nav.='(initial)';5090}elsif(@$parents==1) {5091# single parent commit5092$formats_nav.=5093'(parent: '.5094$cgi->a({-href => href(action=>"commit",5095 hash=>$parent)},5096 esc_html(substr($parent,0,7))) .5097')';5098}else{5099# merge commit5100$formats_nav.=5101'(merge: '.5102join(' ',map{5103$cgi->a({-href => href(action=>"commit",5104 hash=>$_)},5105 esc_html(substr($_,0,7)));5106}@$parents) .5107')';5108}5109if(gitweb_check_feature('patches')) {5110$formats_nav.=" | ".5111$cgi->a({-href => href(action=>"patch", -replay=>1)},5112"patch");5113}51145115if(!defined$parent) {5116$parent="--root";5117}5118my@difftree;5119open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5120@diff_opts,5121(@$parents<=1?$parent:'-c'),5122$hash,"--"5123or die_error(500,"Open git-diff-tree failed");5124@difftree=map{chomp;$_} <$fd>;5125close$fdor die_error(404,"Reading git-diff-tree failed");51265127# non-textual hash id's can be cached5128my$expires;5129if($hash=~m/^[0-9a-fA-F]{40}$/) {5130$expires="+1d";5131}5132my$refs= git_get_references();5133my$ref= format_ref_marker($refs,$co{'id'});51345135 git_header_html(undef,$expires);5136 git_print_page_nav('commit','',5137$hash,$co{'tree'},$hash,5138$formats_nav);51395140if(defined$co{'parent'}) {5141 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5142}else{5143 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5144}5145print"<div class=\"title_text\">\n".5146"<table class=\"object_header\">\n";5147print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5148"<tr>".5149"<td></td><td>$ad{'rfc2822'}";5150if($ad{'hour_local'} <6) {5151printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5152$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5153}else{5154printf(" (%02d:%02d%s)",5155$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5156}5157print"</td>".5158"</tr>\n";5159print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5160print"<tr><td></td><td>$cd{'rfc2822'}".5161sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5162"</td></tr>\n";5163print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5164print"<tr>".5165"<td>tree</td>".5166"<td class=\"sha1\">".5167$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5168class=>"list"},$co{'tree'}) .5169"</td>".5170"<td class=\"link\">".5171$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5172"tree");5173my$snapshot_links= format_snapshot_links($hash);5174if(defined$snapshot_links) {5175print" | ".$snapshot_links;5176}5177print"</td>".5178"</tr>\n";51795180foreachmy$par(@$parents) {5181print"<tr>".5182"<td>parent</td>".5183"<td class=\"sha1\">".5184$cgi->a({-href => href(action=>"commit", hash=>$par),5185class=>"list"},$par) .5186"</td>".5187"<td class=\"link\">".5188$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5189" | ".5190$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5191"</td>".5192"</tr>\n";5193}5194print"</table>".5195"</div>\n";51965197print"<div class=\"page_body\">\n";5198 git_print_log($co{'comment'});5199print"</div>\n";52005201 git_difftree_body(\@difftree,$hash,@$parents);52025203 git_footer_html();5204}52055206sub git_object {5207# object is defined by:5208# - hash or hash_base alone5209# - hash_base and file_name5210my$type;52115212# - hash or hash_base alone5213if($hash|| ($hash_base&& !defined$file_name)) {5214my$object_id=$hash||$hash_base;52155216open my$fd,"-|", quote_command(5217 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5218or die_error(404,"Object does not exist");5219$type= <$fd>;5220chomp$type;5221close$fd5222or die_error(404,"Object does not exist");52235224# - hash_base and file_name5225}elsif($hash_base&&defined$file_name) {5226$file_name=~ s,/+$,,;52275228system(git_cmd(),"cat-file",'-e',$hash_base) ==05229or die_error(404,"Base object does not exist");52305231# here errors should not hapen5232open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5233or die_error(500,"Open git-ls-tree failed");5234my$line= <$fd>;5235close$fd;52365237#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5238unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5239 die_error(404,"File or directory for given base does not exist");5240}5241$type=$2;5242$hash=$3;5243}else{5244 die_error(400,"Not enough information to find object");5245}52465247print$cgi->redirect(-uri => href(action=>$type, -full=>1,5248 hash=>$hash, hash_base=>$hash_base,5249 file_name=>$file_name),5250-status =>'302 Found');5251}52525253sub git_blobdiff {5254my$format=shift||'html';52555256my$fd;5257my@difftree;5258my%diffinfo;5259my$expires;52605261# preparing $fd and %diffinfo for git_patchset_body5262# new style URI5263if(defined$hash_base&&defined$hash_parent_base) {5264if(defined$file_name) {5265# read raw output5266open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5267$hash_parent_base,$hash_base,5268"--", (defined$file_parent?$file_parent: ()),$file_name5269or die_error(500,"Open git-diff-tree failed");5270@difftree=map{chomp;$_} <$fd>;5271close$fd5272or die_error(404,"Reading git-diff-tree failed");5273@difftree5274or die_error(404,"Blob diff not found");52755276}elsif(defined$hash&&5277$hash=~/[0-9a-fA-F]{40}/) {5278# try to find filename from $hash52795280# read filtered raw output5281open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5282$hash_parent_base,$hash_base,"--"5283or die_error(500,"Open git-diff-tree failed");5284@difftree=5285# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5286# $hash == to_id5287grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5288map{chomp;$_} <$fd>;5289close$fd5290or die_error(404,"Reading git-diff-tree failed");5291@difftree5292or die_error(404,"Blob diff not found");52935294}else{5295 die_error(400,"Missing one of the blob diff parameters");5296}52975298if(@difftree>1) {5299 die_error(400,"Ambiguous blob diff specification");5300}53015302%diffinfo= parse_difftree_raw_line($difftree[0]);5303$file_parent||=$diffinfo{'from_file'} ||$file_name;5304$file_name||=$diffinfo{'to_file'};53055306$hash_parent||=$diffinfo{'from_id'};5307$hash||=$diffinfo{'to_id'};53085309# non-textual hash id's can be cached5310if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5311$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5312$expires='+1d';5313}53145315# open patch output5316open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5317'-p', ($formateq'html'?"--full-index": ()),5318$hash_parent_base,$hash_base,5319"--", (defined$file_parent?$file_parent: ()),$file_name5320or die_error(500,"Open git-diff-tree failed");5321}53225323# old/legacy style URI -- not generated anymore since 1.4.3.5324if(!%diffinfo) {5325 die_error('404 Not Found',"Missing one of the blob diff parameters")5326}53275328# header5329if($formateq'html') {5330my$formats_nav=5331$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5332"raw");5333 git_header_html(undef,$expires);5334if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5335 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5336 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5337}else{5338print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5339print"<div class=\"title\">$hashvs$hash_parent</div>\n";5340}5341if(defined$file_name) {5342 git_print_page_path($file_name,"blob",$hash_base);5343}else{5344print"<div class=\"page_path\"></div>\n";5345}53465347}elsif($formateq'plain') {5348print$cgi->header(5349-type =>'text/plain',5350-charset =>'utf-8',5351-expires =>$expires,5352-content_disposition =>'inline; filename="'."$file_name".'.patch"');53535354print"X-Git-Url: ".$cgi->self_url() ."\n\n";53555356}else{5357 die_error(400,"Unknown blobdiff format");5358}53595360# patch5361if($formateq'html') {5362print"<div class=\"page_body\">\n";53635364 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5365close$fd;53665367print"</div>\n";# class="page_body"5368 git_footer_html();53695370}else{5371while(my$line= <$fd>) {5372$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5373$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;53745375print$line;53765377last if$line=~m!^\+\+\+!;5378}5379local$/=undef;5380print<$fd>;5381close$fd;5382}5383}53845385sub git_blobdiff_plain {5386 git_blobdiff('plain');5387}53885389sub git_commitdiff {5390my%params=@_;5391my$format=$params{-format} ||'html';53925393my($patch_max) = gitweb_get_feature('patches');5394if($formateq'patch') {5395 die_error(403,"Patch view not allowed")unless$patch_max;5396}53975398$hash||=$hash_base||"HEAD";5399my%co= parse_commit($hash)5400or die_error(404,"Unknown commit object");54015402# choose format for commitdiff for merge5403if(!defined$hash_parent&& @{$co{'parents'}} >1) {5404$hash_parent='--cc';5405}5406# we need to prepare $formats_nav before almost any parameter munging5407my$formats_nav;5408if($formateq'html') {5409$formats_nav=5410$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5411"raw");5412if($patch_max) {5413$formats_nav.=" | ".5414$cgi->a({-href => href(action=>"patch", -replay=>1)},5415"patch");5416}54175418if(defined$hash_parent&&5419$hash_parentne'-c'&&$hash_parentne'--cc') {5420# commitdiff with two commits given5421my$hash_parent_short=$hash_parent;5422if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5423$hash_parent_short=substr($hash_parent,0,7);5424}5425$formats_nav.=5426' (from';5427for(my$i=0;$i< @{$co{'parents'}};$i++) {5428if($co{'parents'}[$i]eq$hash_parent) {5429$formats_nav.=' parent '. ($i+1);5430last;5431}5432}5433$formats_nav.=': '.5434$cgi->a({-href => href(action=>"commitdiff",5435 hash=>$hash_parent)},5436 esc_html($hash_parent_short)) .5437')';5438}elsif(!$co{'parent'}) {5439# --root commitdiff5440$formats_nav.=' (initial)';5441}elsif(scalar@{$co{'parents'}} ==1) {5442# single parent commit5443$formats_nav.=5444' (parent: '.5445$cgi->a({-href => href(action=>"commitdiff",5446 hash=>$co{'parent'})},5447 esc_html(substr($co{'parent'},0,7))) .5448')';5449}else{5450# merge commit5451if($hash_parenteq'--cc') {5452$formats_nav.=' | '.5453$cgi->a({-href => href(action=>"commitdiff",5454 hash=>$hash, hash_parent=>'-c')},5455'combined');5456}else{# $hash_parent eq '-c'5457$formats_nav.=' | '.5458$cgi->a({-href => href(action=>"commitdiff",5459 hash=>$hash, hash_parent=>'--cc')},5460'compact');5461}5462$formats_nav.=5463' (merge: '.5464join(' ',map{5465$cgi->a({-href => href(action=>"commitdiff",5466 hash=>$_)},5467 esc_html(substr($_,0,7)));5468} @{$co{'parents'}} ) .5469')';5470}5471}54725473my$hash_parent_param=$hash_parent;5474if(!defined$hash_parent_param) {5475# --cc for multiple parents, --root for parentless5476$hash_parent_param=5477@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5478}54795480# read commitdiff5481my$fd;5482my@difftree;5483if($formateq'html') {5484open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5485"--no-commit-id","--patch-with-raw","--full-index",5486$hash_parent_param,$hash,"--"5487or die_error(500,"Open git-diff-tree failed");54885489while(my$line= <$fd>) {5490chomp$line;5491# empty line ends raw part of diff-tree output5492last unless$line;5493push@difftree,scalar parse_difftree_raw_line($line);5494}54955496}elsif($formateq'plain') {5497open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5498'-p',$hash_parent_param,$hash,"--"5499or die_error(500,"Open git-diff-tree failed");5500}elsif($formateq'patch') {5501# For commit ranges, we limit the output to the number of5502# patches specified in the 'patches' feature.5503# For single commits, we limit the output to a single patch,5504# diverging from the git-format-patch default.5505my@commit_spec= ();5506if($hash_parent) {5507if($patch_max>0) {5508push@commit_spec,"-$patch_max";5509}5510push@commit_spec,'-n',"$hash_parent..$hash";5511}else{5512if($params{-single}) {5513push@commit_spec,'-1';5514}else{5515if($patch_max>0) {5516push@commit_spec,"-$patch_max";5517}5518push@commit_spec,"-n";5519}5520push@commit_spec,'--root',$hash;5521}5522open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5523'--stdout',@commit_spec5524or die_error(500,"Open git-format-patch failed");5525}else{5526 die_error(400,"Unknown commitdiff format");5527}55285529# non-textual hash id's can be cached5530my$expires;5531if($hash=~m/^[0-9a-fA-F]{40}$/) {5532$expires="+1d";5533}55345535# write commit message5536if($formateq'html') {5537my$refs= git_get_references();5538my$ref= format_ref_marker($refs,$co{'id'});55395540 git_header_html(undef,$expires);5541 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5542 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5543 git_print_authorship(\%co);5544print"<div class=\"page_body\">\n";5545if(@{$co{'comment'}} >1) {5546print"<div class=\"log\">\n";5547 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5548print"</div>\n";# class="log"5549}55505551}elsif($formateq'plain') {5552my$refs= git_get_references("tags");5553my$tagname= git_get_rev_name_tags($hash);5554my$filename= basename($project) ."-$hash.patch";55555556print$cgi->header(5557-type =>'text/plain',5558-charset =>'utf-8',5559-expires =>$expires,5560-content_disposition =>'inline; filename="'."$filename".'"');5561my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5562print"From: ". to_utf8($co{'author'}) ."\n";5563print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5564print"Subject: ". to_utf8($co{'title'}) ."\n";55655566print"X-Git-Tag:$tagname\n"if$tagname;5567print"X-Git-Url: ".$cgi->self_url() ."\n\n";55685569foreachmy$line(@{$co{'comment'}}) {5570print to_utf8($line) ."\n";5571}5572print"---\n\n";5573}elsif($formateq'patch') {5574my$filename= basename($project) ."-$hash.patch";55755576print$cgi->header(5577-type =>'text/plain',5578-charset =>'utf-8',5579-expires =>$expires,5580-content_disposition =>'inline; filename="'."$filename".'"');5581}55825583# write patch5584if($formateq'html') {5585my$use_parents= !defined$hash_parent||5586$hash_parenteq'-c'||$hash_parenteq'--cc';5587 git_difftree_body(\@difftree,$hash,5588$use_parents? @{$co{'parents'}} :$hash_parent);5589print"<br/>\n";55905591 git_patchset_body($fd, \@difftree,$hash,5592$use_parents? @{$co{'parents'}} :$hash_parent);5593close$fd;5594print"</div>\n";# class="page_body"5595 git_footer_html();55965597}elsif($formateq'plain') {5598local$/=undef;5599print<$fd>;5600close$fd5601or print"Reading git-diff-tree failed\n";5602}elsif($formateq'patch') {5603local$/=undef;5604print<$fd>;5605close$fd5606or print"Reading git-format-patch failed\n";5607}5608}56095610sub git_commitdiff_plain {5611 git_commitdiff(-format =>'plain');5612}56135614# format-patch-style patches5615sub git_patch {5616 git_commitdiff(-format =>'patch', -single=>1);5617}56185619sub git_patches {5620 git_commitdiff(-format =>'patch');5621}56225623sub git_history {5624if(!defined$hash_base) {5625$hash_base= git_get_head_hash($project);5626}5627if(!defined$page) {5628$page=0;5629}5630my$ftype;5631my%co= parse_commit($hash_base)5632or die_error(404,"Unknown commit object");56335634my$refs= git_get_references();5635my$limit=sprintf("--max-count=%i", (100* ($page+1)));56365637my@commitlist= parse_commits($hash_base,101, (100*$page),5638$file_name,"--full-history")5639or die_error(404,"No such file or directory on given branch");56405641if(!defined$hash&&defined$file_name) {5642# some commits could have deleted file in question,5643# and not have it in tree, but one of them has to have it5644for(my$i=0;$i<=@commitlist;$i++) {5645$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5646last ifdefined$hash;5647}5648}5649if(defined$hash) {5650$ftype= git_get_type($hash);5651}5652if(!defined$ftype) {5653 die_error(500,"Unknown type of object");5654}56555656my$paging_nav='';5657if($page>0) {5658$paging_nav.=5659$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5660 file_name=>$file_name)},5661"first");5662$paging_nav.=" ⋅ ".5663$cgi->a({-href => href(-replay=>1, page=>$page-1),5664-accesskey =>"p", -title =>"Alt-p"},"prev");5665}else{5666$paging_nav.="first";5667$paging_nav.=" ⋅ prev";5668}5669my$next_link='';5670if($#commitlist>=100) {5671$next_link=5672$cgi->a({-href => href(-replay=>1, page=>$page+1),5673-accesskey =>"n", -title =>"Alt-n"},"next");5674$paging_nav.=" ⋅$next_link";5675}else{5676$paging_nav.=" ⋅ next";5677}56785679 git_header_html();5680 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5681 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5682 git_print_page_path($file_name,$ftype,$hash_base);56835684 git_history_body(\@commitlist,0,99,5685$refs,$hash_base,$ftype,$next_link);56865687 git_footer_html();5688}56895690sub git_search {5691 gitweb_check_feature('search')or die_error(403,"Search is disabled");5692if(!defined$searchtext) {5693 die_error(400,"Text field is empty");5694}5695if(!defined$hash) {5696$hash= git_get_head_hash($project);5697}5698my%co= parse_commit($hash);5699if(!%co) {5700 die_error(404,"Unknown commit object");5701}5702if(!defined$page) {5703$page=0;5704}57055706$searchtype||='commit';5707if($searchtypeeq'pickaxe') {5708# pickaxe may take all resources of your box and run for several minutes5709# with every query - so decide by yourself how public you make this feature5710 gitweb_check_feature('pickaxe')5711or die_error(403,"Pickaxe is disabled");5712}5713if($searchtypeeq'grep') {5714 gitweb_check_feature('grep')5715or die_error(403,"Grep is disabled");5716}57175718 git_header_html();57195720if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5721my$greptype;5722if($searchtypeeq'commit') {5723$greptype="--grep=";5724}elsif($searchtypeeq'author') {5725$greptype="--author=";5726}elsif($searchtypeeq'committer') {5727$greptype="--committer=";5728}5729$greptype.=$searchtext;5730my@commitlist= parse_commits($hash,101, (100*$page),undef,5731$greptype,'--regexp-ignore-case',5732$search_use_regexp?'--extended-regexp':'--fixed-strings');57335734my$paging_nav='';5735if($page>0) {5736$paging_nav.=5737$cgi->a({-href => href(action=>"search", hash=>$hash,5738 searchtext=>$searchtext,5739 searchtype=>$searchtype)},5740"first");5741$paging_nav.=" ⋅ ".5742$cgi->a({-href => href(-replay=>1, page=>$page-1),5743-accesskey =>"p", -title =>"Alt-p"},"prev");5744}else{5745$paging_nav.="first";5746$paging_nav.=" ⋅ prev";5747}5748my$next_link='';5749if($#commitlist>=100) {5750$next_link=5751$cgi->a({-href => href(-replay=>1, page=>$page+1),5752-accesskey =>"n", -title =>"Alt-n"},"next");5753$paging_nav.=" ⋅$next_link";5754}else{5755$paging_nav.=" ⋅ next";5756}57575758if($#commitlist>=100) {5759}57605761 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5762 git_print_header_div('commit', esc_html($co{'title'}),$hash);5763 git_search_grep_body(\@commitlist,0,99,$next_link);5764}57655766if($searchtypeeq'pickaxe') {5767 git_print_page_nav('','',$hash,$co{'tree'},$hash);5768 git_print_header_div('commit', esc_html($co{'title'}),$hash);57695770print"<table class=\"pickaxe search\">\n";5771my$alternate=1;5772$/="\n";5773open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5774'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5775($search_use_regexp?'--pickaxe-regex': ());5776undef%co;5777my@files;5778while(my$line= <$fd>) {5779chomp$line;5780next unless$line;57815782my%set= parse_difftree_raw_line($line);5783if(defined$set{'commit'}) {5784# finish previous commit5785if(%co) {5786print"</td>\n".5787"<td class=\"link\">".5788$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5789" | ".5790$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5791print"</td>\n".5792"</tr>\n";5793}57945795if($alternate) {5796print"<tr class=\"dark\">\n";5797}else{5798print"<tr class=\"light\">\n";5799}5800$alternate^=1;5801%co= parse_commit($set{'commit'});5802my$author= chop_and_escape_str($co{'author_name'},15,5);5803print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5804"<td><i>$author</i></td>\n".5805"<td>".5806$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5807-class=>"list subject"},5808 chop_and_escape_str($co{'title'},50) ."<br/>");5809}elsif(defined$set{'to_id'}) {5810next if($set{'to_id'} =~m/^0{40}$/);58115812print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5813 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5814-class=>"list"},5815"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5816"<br/>\n";5817}5818}5819close$fd;58205821# finish last commit (warning: repetition!)5822if(%co) {5823print"</td>\n".5824"<td class=\"link\">".5825$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5826" | ".5827$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5828print"</td>\n".5829"</tr>\n";5830}58315832print"</table>\n";5833}58345835if($searchtypeeq'grep') {5836 git_print_page_nav('','',$hash,$co{'tree'},$hash);5837 git_print_header_div('commit', esc_html($co{'title'}),$hash);58385839print"<table class=\"grep_search\">\n";5840my$alternate=1;5841my$matches=0;5842$/="\n";5843open my$fd,"-|", git_cmd(),'grep','-n',5844$search_use_regexp? ('-E','-i') :'-F',5845$searchtext,$co{'tree'};5846my$lastfile='';5847while(my$line= <$fd>) {5848chomp$line;5849my($file,$lno,$ltext,$binary);5850last if($matches++>1000);5851if($line=~/^Binary file (.+) matches$/) {5852$file=$1;5853$binary=1;5854}else{5855(undef,$file,$lno,$ltext) =split(/:/,$line,4);5856}5857if($filene$lastfile) {5858$lastfileand print"</td></tr>\n";5859if($alternate++) {5860print"<tr class=\"dark\">\n";5861}else{5862print"<tr class=\"light\">\n";5863}5864print"<td class=\"list\">".5865$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5866 file_name=>"$file"),5867-class=>"list"}, esc_path($file));5868print"</td><td>\n";5869$lastfile=$file;5870}5871if($binary) {5872print"<div class=\"binary\">Binary file</div>\n";5873}else{5874$ltext= untabify($ltext);5875if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5876$ltext= esc_html($1, -nbsp=>1);5877$ltext.='<span class="match">';5878$ltext.= esc_html($2, -nbsp=>1);5879$ltext.='</span>';5880$ltext.= esc_html($3, -nbsp=>1);5881}else{5882$ltext= esc_html($ltext, -nbsp=>1);5883}5884print"<div class=\"pre\">".5885$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5886 file_name=>"$file").'#l'.$lno,5887-class=>"linenr"},sprintf('%4i',$lno))5888.' '.$ltext."</div>\n";5889}5890}5891if($lastfile) {5892print"</td></tr>\n";5893if($matches>1000) {5894print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5895}5896}else{5897print"<div class=\"diff nodifferences\">No matches found</div>\n";5898}5899close$fd;59005901print"</table>\n";5902}5903 git_footer_html();5904}59055906sub git_search_help {5907 git_header_html();5908 git_print_page_nav('','',$hash,$hash,$hash);5909print<<EOT;5910<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5911regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5912the pattern entered is recognized as the POSIX extended5913<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5914insensitive).</p>5915<dl>5916<dt><b>commit</b></dt>5917<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5918EOT5919my$have_grep= gitweb_check_feature('grep');5920if($have_grep) {5921print<<EOT;5922<dt><b>grep</b></dt>5923<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5924 a different one) are searched for the given pattern. On large trees, this search can take5925a while and put some strain on the server, so please use it with some consideration. Note that5926due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5927case-sensitive.</dd>5928EOT5929}5930print<<EOT;5931<dt><b>author</b></dt>5932<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5933<dt><b>committer</b></dt>5934<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5935EOT5936my$have_pickaxe= gitweb_check_feature('pickaxe');5937if($have_pickaxe) {5938print<<EOT;5939<dt><b>pickaxe</b></dt>5940<dd>All commits that caused the string to appear or disappear from any file (changes that5941added, removed or "modified" the string) will be listed. This search can take a while and5942takes a lot of strain on the server, so please use it wisely. Note that since you may be5943interested even in changes just changing the case as well, this search is case sensitive.</dd>5944EOT5945}5946print"</dl>\n";5947 git_footer_html();5948}59495950sub git_shortlog {5951my$head= git_get_head_hash($project);5952if(!defined$hash) {5953$hash=$head;5954}5955if(!defined$page) {5956$page=0;5957}5958my$refs= git_get_references();59595960my$commit_hash=$hash;5961if(defined$hash_parent) {5962$commit_hash="$hash_parent..$hash";5963}5964my@commitlist= parse_commits($commit_hash,101, (100*$page));59655966my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5967my$next_link='';5968if($#commitlist>=100) {5969$next_link=5970$cgi->a({-href => href(-replay=>1, page=>$page+1),5971-accesskey =>"n", -title =>"Alt-n"},"next");5972}5973my$patch_max= gitweb_check_feature('patches');5974if($patch_max) {5975if($patch_max<0||@commitlist<=$patch_max) {5976$paging_nav.=" ⋅ ".5977$cgi->a({-href => href(action=>"patches", -replay=>1)},5978"patches");5979}5980}59815982 git_header_html();5983 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);5984 git_print_header_div('summary',$project);59855986 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);59875988 git_footer_html();5989}59905991## ......................................................................5992## feeds (RSS, Atom; OPML)59935994sub git_feed {5995my$format=shift||'atom';5996my$have_blame= gitweb_check_feature('blame');59975998# Atom: http://www.atomenabled.org/developers/syndication/5999# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6000if($formatne'rss'&&$formatne'atom') {6001 die_error(400,"Unknown web feed format");6002}60036004# log/feed of current (HEAD) branch, log of given branch, history of file/directory6005my$head=$hash||'HEAD';6006my@commitlist= parse_commits($head,150,0,$file_name);60076008my%latest_commit;6009my%latest_date;6010my$content_type="application/$format+xml";6011if(defined$cgi->http('HTTP_ACCEPT') &&6012$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6013# browser (feed reader) prefers text/xml6014$content_type='text/xml';6015}6016if(defined($commitlist[0])) {6017%latest_commit= %{$commitlist[0]};6018%latest_date= parse_date($latest_commit{'author_epoch'});6019print$cgi->header(6020-type =>$content_type,6021-charset =>'utf-8',6022-last_modified =>$latest_date{'rfc2822'});6023}else{6024print$cgi->header(6025-type =>$content_type,6026-charset =>'utf-8');6027}60286029# Optimization: skip generating the body if client asks only6030# for Last-Modified date.6031return if($cgi->request_method()eq'HEAD');60326033# header variables6034my$title="$site_name-$project/$action";6035my$feed_type='log';6036if(defined$hash) {6037$title.=" - '$hash'";6038$feed_type='branch log';6039if(defined$file_name) {6040$title.=" ::$file_name";6041$feed_type='history';6042}6043}elsif(defined$file_name) {6044$title.=" -$file_name";6045$feed_type='history';6046}6047$title.="$feed_type";6048my$descr= git_get_project_description($project);6049if(defined$descr) {6050$descr= esc_html($descr);6051}else{6052$descr="$project".6053($formateq'rss'?'RSS':'Atom') .6054" feed";6055}6056my$owner= git_get_project_owner($project);6057$owner= esc_html($owner);60586059#header6060my$alt_url;6061if(defined$file_name) {6062$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6063}elsif(defined$hash) {6064$alt_url= href(-full=>1, action=>"log", hash=>$hash);6065}else{6066$alt_url= href(-full=>1, action=>"summary");6067}6068print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6069if($formateq'rss') {6070print<<XML;6071<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6072<channel>6073XML6074print"<title>$title</title>\n".6075"<link>$alt_url</link>\n".6076"<description>$descr</description>\n".6077"<language>en</language>\n";6078}elsif($formateq'atom') {6079print<<XML;6080<feed xmlns="http://www.w3.org/2005/Atom">6081XML6082print"<title>$title</title>\n".6083"<subtitle>$descr</subtitle>\n".6084'<link rel="alternate" type="text/html" href="'.6085$alt_url.'" />'."\n".6086'<link rel="self" type="'.$content_type.'" href="'.6087$cgi->self_url() .'" />'."\n".6088"<id>". href(-full=>1) ."</id>\n".6089# use project owner for feed author6090"<author><name>$owner</name></author>\n";6091if(defined$favicon) {6092print"<icon>". esc_url($favicon) ."</icon>\n";6093}6094if(defined$logo_url) {6095# not twice as wide as tall: 72 x 27 pixels6096print"<logo>". esc_url($logo) ."</logo>\n";6097}6098if(!%latest_date) {6099# dummy date to keep the feed valid until commits trickle in:6100print"<updated>1970-01-01T00:00:00Z</updated>\n";6101}else{6102print"<updated>$latest_date{'iso-8601'}</updated>\n";6103}6104}61056106# contents6107for(my$i=0;$i<=$#commitlist;$i++) {6108my%co= %{$commitlist[$i]};6109my$commit=$co{'id'};6110# we read 150, we always show 30 and the ones more recent than 48 hours6111if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6112last;6113}6114my%cd= parse_date($co{'author_epoch'});61156116# get list of changed files6117open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6118$co{'parent'} ||"--root",6119$co{'id'},"--", (defined$file_name?$file_name: ())6120ornext;6121my@difftree=map{chomp;$_} <$fd>;6122close$fd6123ornext;61246125# print element (entry, item)6126my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6127if($formateq'rss') {6128print"<item>\n".6129"<title>". esc_html($co{'title'}) ."</title>\n".6130"<author>". esc_html($co{'author'}) ."</author>\n".6131"<pubDate>$cd{'rfc2822'}</pubDate>\n".6132"<guid isPermaLink=\"true\">$co_url</guid>\n".6133"<link>$co_url</link>\n".6134"<description>". esc_html($co{'title'}) ."</description>\n".6135"<content:encoded>".6136"<![CDATA[\n";6137}elsif($formateq'atom') {6138print"<entry>\n".6139"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6140"<updated>$cd{'iso-8601'}</updated>\n".6141"<author>\n".6142" <name>". esc_html($co{'author_name'}) ."</name>\n";6143if($co{'author_email'}) {6144print" <email>". esc_html($co{'author_email'}) ."</email>\n";6145}6146print"</author>\n".6147# use committer for contributor6148"<contributor>\n".6149" <name>". esc_html($co{'committer_name'}) ."</name>\n";6150if($co{'committer_email'}) {6151print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6152}6153print"</contributor>\n".6154"<published>$cd{'iso-8601'}</published>\n".6155"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6156"<id>$co_url</id>\n".6157"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6158"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6159}6160my$comment=$co{'comment'};6161print"<pre>\n";6162foreachmy$line(@$comment) {6163$line= esc_html($line);6164print"$line\n";6165}6166print"</pre><ul>\n";6167foreachmy$difftree_line(@difftree) {6168my%difftree= parse_difftree_raw_line($difftree_line);6169next if!$difftree{'from_id'};61706171my$file=$difftree{'file'} ||$difftree{'to_file'};61726173print"<li>".6174"[".6175$cgi->a({-href => href(-full=>1, action=>"blobdiff",6176 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6177 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6178 file_name=>$file, file_parent=>$difftree{'from_file'}),6179-title =>"diff"},'D');6180if($have_blame) {6181print$cgi->a({-href => href(-full=>1, action=>"blame",6182 file_name=>$file, hash_base=>$commit),6183-title =>"blame"},'B');6184}6185# if this is not a feed of a file history6186if(!defined$file_name||$file_namene$file) {6187print$cgi->a({-href => href(-full=>1, action=>"history",6188 file_name=>$file, hash=>$commit),6189-title =>"history"},'H');6190}6191$file= esc_path($file);6192print"] ".6193"$file</li>\n";6194}6195if($formateq'rss') {6196print"</ul>]]>\n".6197"</content:encoded>\n".6198"</item>\n";6199}elsif($formateq'atom') {6200print"</ul>\n</div>\n".6201"</content>\n".6202"</entry>\n";6203}6204}62056206# end of feed6207if($formateq'rss') {6208print"</channel>\n</rss>\n";6209}elsif($formateq'atom') {6210print"</feed>\n";6211}6212}62136214sub git_rss {6215 git_feed('rss');6216}62176218sub git_atom {6219 git_feed('atom');6220}62216222sub git_opml {6223my@list= git_get_projects_list();62246225print$cgi->header(6226-type =>'text/xml',6227-charset =>'utf-8',6228-content_disposition =>'inline; filename="opml.xml"');62296230print<<XML;6231<?xml version="1.0" encoding="utf-8"?>6232<opml version="1.0">6233<head>6234 <title>$site_nameOPML Export</title>6235</head>6236<body>6237<outline text="git RSS feeds">6238XML62396240foreachmy$pr(@list) {6241my%proj=%$pr;6242my$head= git_get_head_hash($proj{'path'});6243if(!defined$head) {6244next;6245}6246$git_dir="$projectroot/$proj{'path'}";6247my%co= parse_commit($head);6248if(!%co) {6249next;6250}62516252my$path= esc_html(chop_str($proj{'path'},25,5));6253my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6254my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6255print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6256}6257print<<XML;6258</outline>6259</body>6260</opml>6261XML6262}