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_check_feature(<feature>) to check if <feature> is enabled 194 195# Enable the 'blame' blob view, showing the last commit that modified 196# each line in the file. This can be very CPU-intensive. 197 198# To enable system wide have in $GITWEB_CONFIG 199# $feature{'blame'}{'default'} = [1]; 200# To have project specific config enable override in $GITWEB_CONFIG 201# $feature{'blame'}{'override'} = 1; 202# and in project config gitweb.blame = 0|1; 203'blame'=> { 204'sub'=> \&feature_blame, 205'override'=>0, 206'default'=> [0]}, 207 208# Enable the 'snapshot' link, providing a compressed archive of any 209# tree. This can potentially generate high traffic if you have large 210# project. 211 212# Value is a list of formats defined in %known_snapshot_formats that 213# you wish to offer. 214# To disable system wide have in $GITWEB_CONFIG 215# $feature{'snapshot'}{'default'} = []; 216# To have project specific config enable override in $GITWEB_CONFIG 217# $feature{'snapshot'}{'override'} = 1; 218# and in project config, a comma-separated list of formats or "none" 219# to disable. Example: gitweb.snapshot = tbz2,zip; 220'snapshot'=> { 221'sub'=> \&feature_snapshot, 222'override'=>0, 223'default'=> ['tgz']}, 224 225# Enable text search, which will list the commits which match author, 226# committer or commit text to a given string. Enabled by default. 227# Project specific override is not supported. 228'search'=> { 229'override'=>0, 230'default'=> [1]}, 231 232# Enable grep search, which will list the files in currently selected 233# tree containing the given string. Enabled by default. This can be 234# potentially CPU-intensive, of course. 235 236# To enable system wide have in $GITWEB_CONFIG 237# $feature{'grep'}{'default'} = [1]; 238# To have project specific config enable override in $GITWEB_CONFIG 239# $feature{'grep'}{'override'} = 1; 240# and in project config gitweb.grep = 0|1; 241'grep'=> { 242'override'=>0, 243'default'=> [1]}, 244 245# Enable the pickaxe search, which will list the commits that modified 246# a given string in a file. This can be practical and quite faster 247# alternative to 'blame', but still potentially CPU-intensive. 248 249# To enable system wide have in $GITWEB_CONFIG 250# $feature{'pickaxe'}{'default'} = [1]; 251# To have project specific config enable override in $GITWEB_CONFIG 252# $feature{'pickaxe'}{'override'} = 1; 253# and in project config gitweb.pickaxe = 0|1; 254'pickaxe'=> { 255'sub'=> \&feature_pickaxe, 256'override'=>0, 257'default'=> [1]}, 258 259# Make gitweb use an alternative format of the URLs which can be 260# more readable and natural-looking: project name is embedded 261# directly in the path and the query string contains other 262# auxiliary information. All gitweb installations recognize 263# URL in either format; this configures in which formats gitweb 264# generates links. 265 266# To enable system wide have in $GITWEB_CONFIG 267# $feature{'pathinfo'}{'default'} = [1]; 268# Project specific override is not supported. 269 270# Note that you will need to change the default location of CSS, 271# favicon, logo and possibly other files to an absolute URL. Also, 272# if gitweb.cgi serves as your indexfile, you will need to force 273# $my_uri to contain the script name in your $GITWEB_CONFIG. 274'pathinfo'=> { 275'override'=>0, 276'default'=> [0]}, 277 278# Make gitweb consider projects in project root subdirectories 279# to be forks of existing projects. Given project $projname.git, 280# projects matching $projname/*.git will not be shown in the main 281# projects list, instead a '+' mark will be added to $projname 282# there and a 'forks' view will be enabled for the project, listing 283# all the forks. If project list is taken from a file, forks have 284# to be listed after the main project. 285 286# To enable system wide have in $GITWEB_CONFIG 287# $feature{'forks'}{'default'} = [1]; 288# Project specific override is not supported. 289'forks'=> { 290'override'=>0, 291'default'=> [0]}, 292 293# Insert custom links to the action bar of all project pages. 294# This enables you mainly to link to third-party scripts integrating 295# into gitweb; e.g. git-browser for graphical history representation 296# or custom web-based repository administration interface. 297 298# The 'default' value consists of a list of triplets in the form 299# (label, link, position) where position is the label after which 300# to insert the link and link is a format string where %n expands 301# to the project name, %f to the project path within the filesystem, 302# %h to the current hash (h gitweb parameter) and %b to the current 303# hash base (hb gitweb parameter); %% expands to %. 304 305# To enable system wide have in $GITWEB_CONFIG e.g. 306# $feature{'actions'}{'default'} = [('graphiclog', 307# '/git-browser/by-commit.html?r=%n', 'summary')]; 308# Project specific override is not supported. 309'actions'=> { 310'override'=>0, 311'default'=> []}, 312 313# Allow gitweb scan project content tags described in ctags/ 314# of project repository, and display the popular Web 2.0-ish 315# "tag cloud" near the project list. Note that this is something 316# COMPLETELY different from the normal Git tags. 317 318# gitweb by itself can show existing tags, but it does not handle 319# tagging itself; you need an external application for that. 320# For an example script, check Girocco's cgi/tagproj.cgi. 321# You may want to install the HTML::TagCloud Perl module to get 322# a pretty tag cloud instead of just a list of tags. 323 324# To enable system wide have in $GITWEB_CONFIG 325# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 326# Project specific override is not supported. 327'ctags'=> { 328'override'=>0, 329'default'=> [0]}, 330); 331 332sub gitweb_get_feature { 333my($name) =@_; 334return unlessexists$feature{$name}; 335my($sub,$override,@defaults) = ( 336$feature{$name}{'sub'}, 337$feature{$name}{'override'}, 338@{$feature{$name}{'default'}}); 339if(!$override) {return@defaults; } 340if(!defined$sub) { 341warn"feature$nameis not overrideable"; 342return@defaults; 343} 344return$sub->(@defaults); 345} 346 347# A wrapper to check if a given feature is enabled. 348# With this, you can say 349# 350# my $bool_feat = gitweb_check_feature('bool_feat'); 351# gitweb_check_feature('bool_feat') or somecode; 352# 353# instead of 354# 355# my ($bool_feat) = gitweb_get_feature('bool_feat'); 356# (gitweb_get_feature('bool_feat'))[0] or somecode; 357# 358sub gitweb_check_feature { 359return(gitweb_get_feature(@_))[0]; 360} 361 362 363sub feature_blame { 364my($val) = git_get_project_config('blame','--bool'); 365 366if($valeq'true') { 367return1; 368}elsif($valeq'false') { 369return0; 370} 371 372return$_[0]; 373} 374 375sub feature_snapshot { 376my(@fmts) =@_; 377 378my($val) = git_get_project_config('snapshot'); 379 380if($val) { 381@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 382} 383 384return@fmts; 385} 386 387sub feature_grep { 388my($val) = git_get_project_config('grep','--bool'); 389 390if($valeq'true') { 391return(1); 392}elsif($valeq'false') { 393return(0); 394} 395 396return($_[0]); 397} 398 399sub feature_pickaxe { 400my($val) = git_get_project_config('pickaxe','--bool'); 401 402if($valeq'true') { 403return(1); 404}elsif($valeq'false') { 405return(0); 406} 407 408return($_[0]); 409} 410 411# checking HEAD file with -e is fragile if the repository was 412# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 413# and then pruned. 414sub check_head_link { 415my($dir) =@_; 416my$headfile="$dir/HEAD"; 417return((-e $headfile) || 418(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 419} 420 421sub check_export_ok { 422my($dir) =@_; 423return(check_head_link($dir) && 424(!$export_ok|| -e "$dir/$export_ok") && 425(!$export_auth_hook||$export_auth_hook->($dir))); 426} 427 428# process alternate names for backward compatibility 429# filter out unsupported (unknown) snapshot formats 430sub filter_snapshot_fmts { 431my@fmts=@_; 432 433@fmts=map{ 434exists$known_snapshot_format_aliases{$_} ? 435$known_snapshot_format_aliases{$_} :$_}@fmts; 436@fmts=grep(exists$known_snapshot_formats{$_},@fmts); 437 438} 439 440our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 441if(-e $GITWEB_CONFIG) { 442do$GITWEB_CONFIG; 443}else{ 444our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 445do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 446} 447 448# version of the core git binary 449our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 450 451$projects_list||=$projectroot; 452 453# ====================================================================== 454# input validation and dispatch 455 456# input parameters can be collected from a variety of sources (presently, CGI 457# and PATH_INFO), so we define an %input_params hash that collects them all 458# together during validation: this allows subsequent uses (e.g. href()) to be 459# agnostic of the parameter origin 460 461our%input_params= (); 462 463# input parameters are stored with the long parameter name as key. This will 464# also be used in the href subroutine to convert parameters to their CGI 465# equivalent, and since the href() usage is the most frequent one, we store 466# the name -> CGI key mapping here, instead of the reverse. 467# 468# XXX: Warning: If you touch this, check the search form for updating, 469# too. 470 471our@cgi_param_mapping= ( 472 project =>"p", 473 action =>"a", 474 file_name =>"f", 475 file_parent =>"fp", 476 hash =>"h", 477 hash_parent =>"hp", 478 hash_base =>"hb", 479 hash_parent_base =>"hpb", 480 page =>"pg", 481 order =>"o", 482 searchtext =>"s", 483 searchtype =>"st", 484 snapshot_format =>"sf", 485 extra_options =>"opt", 486 search_use_regexp =>"sr", 487); 488our%cgi_param_mapping=@cgi_param_mapping; 489 490# we will also need to know the possible actions, for validation 491our%actions= ( 492"blame"=> \&git_blame, 493"blobdiff"=> \&git_blobdiff, 494"blobdiff_plain"=> \&git_blobdiff_plain, 495"blob"=> \&git_blob, 496"blob_plain"=> \&git_blob_plain, 497"commitdiff"=> \&git_commitdiff, 498"commitdiff_plain"=> \&git_commitdiff_plain, 499"commit"=> \&git_commit, 500"forks"=> \&git_forks, 501"heads"=> \&git_heads, 502"history"=> \&git_history, 503"log"=> \&git_log, 504"rss"=> \&git_rss, 505"atom"=> \&git_atom, 506"search"=> \&git_search, 507"search_help"=> \&git_search_help, 508"shortlog"=> \&git_shortlog, 509"summary"=> \&git_summary, 510"tag"=> \&git_tag, 511"tags"=> \&git_tags, 512"tree"=> \&git_tree, 513"snapshot"=> \&git_snapshot, 514"object"=> \&git_object, 515# those below don't need $project 516"opml"=> \&git_opml, 517"project_list"=> \&git_project_list, 518"project_index"=> \&git_project_index, 519); 520 521# finally, we have the hash of allowed extra_options for the commands that 522# allow them 523our%allowed_options= ( 524"--no-merges"=> [qw(rss atom log shortlog history)], 525); 526 527# fill %input_params with the CGI parameters. All values except for 'opt' 528# should be single values, but opt can be an array. We should probably 529# build an array of parameters that can be multi-valued, but since for the time 530# being it's only this one, we just single it out 531while(my($name,$symbol) =each%cgi_param_mapping) { 532if($symboleq'opt') { 533$input_params{$name} = [$cgi->param($symbol) ]; 534}else{ 535$input_params{$name} =$cgi->param($symbol); 536} 537} 538 539# now read PATH_INFO and update the parameter list for missing parameters 540sub evaluate_path_info { 541return ifdefined$input_params{'project'}; 542return if!$path_info; 543$path_info=~ s,^/+,,; 544return if!$path_info; 545 546# find which part of PATH_INFO is project 547my$project=$path_info; 548$project=~ s,/+$,,; 549while($project&& !check_head_link("$projectroot/$project")) { 550$project=~ s,/*[^/]*$,,; 551} 552return unless$project; 553$input_params{'project'} =$project; 554 555# do not change any parameters if an action is given using the query string 556return if$input_params{'action'}; 557$path_info=~ s,^\Q$project\E/*,,; 558 559# next, check if we have an action 560my$action=$path_info; 561$action=~ s,/.*$,,; 562if(exists$actions{$action}) { 563$path_info=~ s,^$action/*,,; 564$input_params{'action'} =$action; 565} 566 567# list of actions that want hash_base instead of hash, but can have no 568# pathname (f) parameter 569my@wants_base= ( 570'tree', 571'history', 572); 573 574# we want to catch 575# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 576my($parentrefname,$parentpathname,$refname,$pathname) = 577($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 578 579# first, analyze the 'current' part 580if(defined$pathname) { 581# we got "branch:filename" or "branch:dir/" 582# we could use git_get_type(branch:pathname), but: 583# - it needs $git_dir 584# - it does a git() call 585# - the convention of terminating directories with a slash 586# makes it superfluous 587# - embedding the action in the PATH_INFO would make it even 588# more superfluous 589$pathname=~ s,^/+,,; 590if(!$pathname||substr($pathname, -1)eq"/") { 591$input_params{'action'} ||="tree"; 592$pathname=~ s,/$,,; 593}else{ 594# the default action depends on whether we had parent info 595# or not 596if($parentrefname) { 597$input_params{'action'} ||="blobdiff_plain"; 598}else{ 599$input_params{'action'} ||="blob_plain"; 600} 601} 602$input_params{'hash_base'} ||=$refname; 603$input_params{'file_name'} ||=$pathname; 604}elsif(defined$refname) { 605# we got "branch". In this case we have to choose if we have to 606# set hash or hash_base. 607# 608# Most of the actions without a pathname only want hash to be 609# set, except for the ones specified in @wants_base that want 610# hash_base instead. It should also be noted that hand-crafted 611# links having 'history' as an action and no pathname or hash 612# set will fail, but that happens regardless of PATH_INFO. 613$input_params{'action'} ||="shortlog"; 614if(grep{$_eq$input_params{'action'} }@wants_base) { 615$input_params{'hash_base'} ||=$refname; 616}else{ 617$input_params{'hash'} ||=$refname; 618} 619} 620 621# next, handle the 'parent' part, if present 622if(defined$parentrefname) { 623# a missing pathspec defaults to the 'current' filename, allowing e.g. 624# someproject/blobdiff/oldrev..newrev:/filename 625if($parentpathname) { 626$parentpathname=~ s,^/+,,; 627$parentpathname=~ s,/$,,; 628$input_params{'file_parent'} ||=$parentpathname; 629}else{ 630$input_params{'file_parent'} ||=$input_params{'file_name'}; 631} 632# we assume that hash_parent_base is wanted if a path was specified, 633# or if the action wants hash_base instead of hash 634if(defined$input_params{'file_parent'} || 635grep{$_eq$input_params{'action'} }@wants_base) { 636$input_params{'hash_parent_base'} ||=$parentrefname; 637}else{ 638$input_params{'hash_parent'} ||=$parentrefname; 639} 640} 641 642# for the snapshot action, we allow URLs in the form 643# $project/snapshot/$hash.ext 644# where .ext determines the snapshot and gets removed from the 645# passed $refname to provide the $hash. 646# 647# To be able to tell that $refname includes the format extension, we 648# require the following two conditions to be satisfied: 649# - the hash input parameter MUST have been set from the $refname part 650# of the URL (i.e. they must be equal) 651# - the snapshot format MUST NOT have been defined already (e.g. from 652# CGI parameter sf) 653# It's also useless to try any matching unless $refname has a dot, 654# so we check for that too 655if(defined$input_params{'action'} && 656$input_params{'action'}eq'snapshot'&& 657defined$refname&&index($refname,'.') != -1&& 658$refnameeq$input_params{'hash'} && 659!defined$input_params{'snapshot_format'}) { 660# We loop over the known snapshot formats, checking for 661# extensions. Allowed extensions are both the defined suffix 662# (which includes the initial dot already) and the snapshot 663# format key itself, with a prepended dot 664while(my($fmt,%opt) =each%known_snapshot_formats) { 665my$hash=$refname; 666my$sfx; 667$hash=~s/(\Q$opt{'suffix'}\E|\Q.$fmt\E)$//; 668next unless$sfx=$1; 669# a valid suffix was found, so set the snapshot format 670# and reset the hash parameter 671$input_params{'snapshot_format'} =$fmt; 672$input_params{'hash'} =$hash; 673# we also set the format suffix to the one requested 674# in the URL: this way a request for e.g. .tgz returns 675# a .tgz instead of a .tar.gz 676$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 677last; 678} 679} 680} 681evaluate_path_info(); 682 683our$action=$input_params{'action'}; 684if(defined$action) { 685if(!validate_action($action)) { 686 die_error(400,"Invalid action parameter"); 687} 688} 689 690# parameters which are pathnames 691our$project=$input_params{'project'}; 692if(defined$project) { 693if(!validate_project($project)) { 694undef$project; 695 die_error(404,"No such project"); 696} 697} 698 699our$file_name=$input_params{'file_name'}; 700if(defined$file_name) { 701if(!validate_pathname($file_name)) { 702 die_error(400,"Invalid file parameter"); 703} 704} 705 706our$file_parent=$input_params{'file_parent'}; 707if(defined$file_parent) { 708if(!validate_pathname($file_parent)) { 709 die_error(400,"Invalid file parent parameter"); 710} 711} 712 713# parameters which are refnames 714our$hash=$input_params{'hash'}; 715if(defined$hash) { 716if(!validate_refname($hash)) { 717 die_error(400,"Invalid hash parameter"); 718} 719} 720 721our$hash_parent=$input_params{'hash_parent'}; 722if(defined$hash_parent) { 723if(!validate_refname($hash_parent)) { 724 die_error(400,"Invalid hash parent parameter"); 725} 726} 727 728our$hash_base=$input_params{'hash_base'}; 729if(defined$hash_base) { 730if(!validate_refname($hash_base)) { 731 die_error(400,"Invalid hash base parameter"); 732} 733} 734 735our@extra_options= @{$input_params{'extra_options'}}; 736# @extra_options is always defined, since it can only be (currently) set from 737# CGI, and $cgi->param() returns the empty array in array context if the param 738# is not set 739foreachmy$opt(@extra_options) { 740if(not exists$allowed_options{$opt}) { 741 die_error(400,"Invalid option parameter"); 742} 743if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 744 die_error(400,"Invalid option parameter for this action"); 745} 746} 747 748our$hash_parent_base=$input_params{'hash_parent_base'}; 749if(defined$hash_parent_base) { 750if(!validate_refname($hash_parent_base)) { 751 die_error(400,"Invalid hash parent base parameter"); 752} 753} 754 755# other parameters 756our$page=$input_params{'page'}; 757if(defined$page) { 758if($page=~m/[^0-9]/) { 759 die_error(400,"Invalid page parameter"); 760} 761} 762 763our$searchtype=$input_params{'searchtype'}; 764if(defined$searchtype) { 765if($searchtype=~m/[^a-z]/) { 766 die_error(400,"Invalid searchtype parameter"); 767} 768} 769 770our$search_use_regexp=$input_params{'search_use_regexp'}; 771 772our$searchtext=$input_params{'searchtext'}; 773our$search_regexp; 774if(defined$searchtext) { 775if(length($searchtext) <2) { 776 die_error(403,"At least two characters are required for search parameter"); 777} 778$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 779} 780 781# path to the current git repository 782our$git_dir; 783$git_dir="$projectroot/$project"if$project; 784 785# list of supported snapshot formats 786our@snapshot_fmts= gitweb_get_feature('snapshot'); 787@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 788 789# dispatch 790if(!defined$action) { 791if(defined$hash) { 792$action= git_get_type($hash); 793}elsif(defined$hash_base&&defined$file_name) { 794$action= git_get_type("$hash_base:$file_name"); 795}elsif(defined$project) { 796$action='summary'; 797}else{ 798$action='project_list'; 799} 800} 801if(!defined($actions{$action})) { 802 die_error(400,"Unknown action"); 803} 804if($action!~m/^(opml|project_list|project_index)$/&& 805!$project) { 806 die_error(400,"Project needed"); 807} 808$actions{$action}->(); 809exit; 810 811## ====================================================================== 812## action links 813 814sub href (%) { 815my%params=@_; 816# default is to use -absolute url() i.e. $my_uri 817my$href=$params{-full} ?$my_url:$my_uri; 818 819$params{'project'} =$projectunlessexists$params{'project'}; 820 821if($params{-replay}) { 822while(my($name,$symbol) =each%cgi_param_mapping) { 823if(!exists$params{$name}) { 824$params{$name} =$input_params{$name}; 825} 826} 827} 828 829my$use_pathinfo= gitweb_check_feature('pathinfo'); 830if($use_pathinfo) { 831# try to put as many parameters as possible in PATH_INFO: 832# - project name 833# - action 834# - hash_parent or hash_parent_base:/file_parent 835# - hash or hash_base:/filename 836# - the snapshot_format as an appropriate suffix 837 838# When the script is the root DirectoryIndex for the domain, 839# $href here would be something like http://gitweb.example.com/ 840# Thus, we strip any trailing / from $href, to spare us double 841# slashes in the final URL 842$href=~ s,/$,,; 843 844# Then add the project name, if present 845$href.="/".esc_url($params{'project'})ifdefined$params{'project'}; 846delete$params{'project'}; 847 848# since we destructively absorb parameters, we keep this 849# boolean that remembers if we're handling a snapshot 850my$is_snapshot=$params{'action'}eq'snapshot'; 851 852# Summary just uses the project path URL, any other action is 853# added to the URL 854if(defined$params{'action'}) { 855$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 856delete$params{'action'}; 857} 858 859# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 860# stripping nonexistent or useless pieces 861$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 862||$params{'hash_parent'} ||$params{'hash'}); 863if(defined$params{'hash_base'}) { 864if(defined$params{'hash_parent_base'}) { 865$href.= esc_url($params{'hash_parent_base'}); 866# skip the file_parent if it's the same as the file_name 867delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 868if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 869$href.=":/".esc_url($params{'file_parent'}); 870delete$params{'file_parent'}; 871} 872$href.=".."; 873delete$params{'hash_parent'}; 874delete$params{'hash_parent_base'}; 875}elsif(defined$params{'hash_parent'}) { 876$href.= esc_url($params{'hash_parent'}).".."; 877delete$params{'hash_parent'}; 878} 879 880$href.= esc_url($params{'hash_base'}); 881if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 882$href.=":/".esc_url($params{'file_name'}); 883delete$params{'file_name'}; 884} 885delete$params{'hash'}; 886delete$params{'hash_base'}; 887}elsif(defined$params{'hash'}) { 888$href.= esc_url($params{'hash'}); 889delete$params{'hash'}; 890} 891 892# If the action was a snapshot, we can absorb the 893# snapshot_format parameter too 894if($is_snapshot) { 895my$fmt=$params{'snapshot_format'}; 896# snapshot_format should always be defined when href() 897# is called, but just in case some code forgets, we 898# fall back to the default 899$fmt||=$snapshot_fmts[0]; 900$href.=$known_snapshot_formats{$fmt}{'suffix'}; 901delete$params{'snapshot_format'}; 902} 903} 904 905# now encode the parameters explicitly 906my@result= (); 907for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 908my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 909if(defined$params{$name}) { 910if(ref($params{$name})eq"ARRAY") { 911foreachmy$par(@{$params{$name}}) { 912push@result,$symbol."=". esc_param($par); 913} 914}else{ 915push@result,$symbol."=". esc_param($params{$name}); 916} 917} 918} 919$href.="?".join(';',@result)ifscalar@result; 920 921return$href; 922} 923 924 925## ====================================================================== 926## validation, quoting/unquoting and escaping 927 928sub validate_action { 929my$input=shift||returnundef; 930returnundefunlessexists$actions{$input}; 931return$input; 932} 933 934sub validate_project { 935my$input=shift||returnundef; 936if(!validate_pathname($input) || 937!(-d "$projectroot/$input") || 938!check_export_ok("$projectroot/$input") || 939($strict_export&& !project_in_list($input))) { 940returnundef; 941}else{ 942return$input; 943} 944} 945 946sub validate_pathname { 947my$input=shift||returnundef; 948 949# no '.' or '..' as elements of path, i.e. no '.' nor '..' 950# at the beginning, at the end, and between slashes. 951# also this catches doubled slashes 952if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 953returnundef; 954} 955# no null characters 956if($input=~m!\0!) { 957returnundef; 958} 959return$input; 960} 961 962sub validate_refname { 963my$input=shift||returnundef; 964 965# textual hashes are O.K. 966if($input=~m/^[0-9a-fA-F]{40}$/) { 967return$input; 968} 969# it must be correct pathname 970$input= validate_pathname($input) 971orreturnundef; 972# restrictions on ref name according to git-check-ref-format 973if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { 974returnundef; 975} 976return$input; 977} 978 979# decode sequences of octets in utf8 into Perl's internal form, 980# which is utf-8 with utf8 flag set if needed. gitweb writes out 981# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning 982sub to_utf8 { 983my$str=shift; 984if(utf8::valid($str)) { 985 utf8::decode($str); 986return$str; 987}else{ 988return decode($fallback_encoding,$str, Encode::FB_DEFAULT); 989} 990} 991 992# quote unsafe chars, but keep the slash, even when it's not 993# correct, but quoted slashes look too horrible in bookmarks 994sub esc_param { 995my$str=shift; 996$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg; 997$str=~s/\+/%2B/g; 998$str=~s/ /\+/g; 999return$str;1000}10011002# quote unsafe chars in whole URL, so some charactrs cannot be quoted1003sub esc_url {1004my$str=shift;1005$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1006$str=~s/\+/%2B/g;1007$str=~s/ /\+/g;1008return$str;1009}10101011# replace invalid utf8 character with SUBSTITUTION sequence1012sub esc_html ($;%) {1013my$str=shift;1014my%opts=@_;10151016$str= to_utf8($str);1017$str=$cgi->escapeHTML($str);1018if($opts{'-nbsp'}) {1019$str=~s/ / /g;1020}1021$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1022return$str;1023}10241025# quote control characters and escape filename to HTML1026sub esc_path {1027my$str=shift;1028my%opts=@_;10291030$str= to_utf8($str);1031$str=$cgi->escapeHTML($str);1032if($opts{'-nbsp'}) {1033$str=~s/ / /g;1034}1035$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1036return$str;1037}10381039# Make control characters "printable", using character escape codes (CEC)1040sub quot_cec {1041my$cntrl=shift;1042my%opts=@_;1043my%es= (# character escape codes, aka escape sequences1044"\t"=>'\t',# tab (HT)1045"\n"=>'\n',# line feed (LF)1046"\r"=>'\r',# carrige return (CR)1047"\f"=>'\f',# form feed (FF)1048"\b"=>'\b',# backspace (BS)1049"\a"=>'\a',# alarm (bell) (BEL)1050"\e"=>'\e',# escape (ESC)1051"\013"=>'\v',# vertical tab (VT)1052"\000"=>'\0',# nul character (NUL)1053);1054my$chr= ( (exists$es{$cntrl})1055?$es{$cntrl}1056:sprintf('\%2x',ord($cntrl)) );1057if($opts{-nohtml}) {1058return$chr;1059}else{1060return"<span class=\"cntrl\">$chr</span>";1061}1062}10631064# Alternatively use unicode control pictures codepoints,1065# Unicode "printable representation" (PR)1066sub quot_upr {1067my$cntrl=shift;1068my%opts=@_;10691070my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1071if($opts{-nohtml}) {1072return$chr;1073}else{1074return"<span class=\"cntrl\">$chr</span>";1075}1076}10771078# git may return quoted and escaped filenames1079sub unquote {1080my$str=shift;10811082sub unq {1083my$seq=shift;1084my%es= (# character escape codes, aka escape sequences1085't'=>"\t",# tab (HT, TAB)1086'n'=>"\n",# newline (NL)1087'r'=>"\r",# return (CR)1088'f'=>"\f",# form feed (FF)1089'b'=>"\b",# backspace (BS)1090'a'=>"\a",# alarm (bell) (BEL)1091'e'=>"\e",# escape (ESC)1092'v'=>"\013",# vertical tab (VT)1093);10941095if($seq=~m/^[0-7]{1,3}$/) {1096# octal char sequence1097returnchr(oct($seq));1098}elsif(exists$es{$seq}) {1099# C escape sequence, aka character escape code1100return$es{$seq};1101}1102# quoted ordinary character1103return$seq;1104}11051106if($str=~m/^"(.*)"$/) {1107# needs unquoting1108$str=$1;1109$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1110}1111return$str;1112}11131114# escape tabs (convert tabs to spaces)1115sub untabify {1116my$line=shift;11171118while((my$pos=index($line,"\t")) != -1) {1119if(my$count= (8- ($pos%8))) {1120my$spaces=' ' x $count;1121$line=~s/\t/$spaces/;1122}1123}11241125return$line;1126}11271128sub project_in_list {1129my$project=shift;1130my@list= git_get_projects_list();1131return@list&&scalar(grep{$_->{'path'}eq$project}@list);1132}11331134## ----------------------------------------------------------------------1135## HTML aware string manipulation11361137# Try to chop given string on a word boundary between position1138# $len and $len+$add_len. If there is no word boundary there,1139# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1140# (marking chopped part) would be longer than given string.1141sub chop_str {1142my$str=shift;1143my$len=shift;1144my$add_len=shift||10;1145my$where=shift||'right';# 'left' | 'center' | 'right'11461147# Make sure perl knows it is utf8 encoded so we don't1148# cut in the middle of a utf8 multibyte char.1149$str= to_utf8($str);11501151# allow only $len chars, but don't cut a word if it would fit in $add_len1152# if it doesn't fit, cut it if it's still longer than the dots we would add1153# remove chopped character entities entirely11541155# when chopping in the middle, distribute $len into left and right part1156# return early if chopping wouldn't make string shorter1157if($whereeq'center') {1158return$strif($len+5>=length($str));# filler is length 51159$len=int($len/2);1160}else{1161return$strif($len+4>=length($str));# filler is length 41162}11631164# regexps: ending and beginning with word part up to $add_len1165my$endre=qr/.{$len}\w{0,$add_len}/;1166my$begre=qr/\w{0,$add_len}.{$len}/;11671168if($whereeq'left') {1169$str=~m/^(.*?)($begre)$/;1170my($lead,$body) = ($1,$2);1171if(length($lead) >4) {1172$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1173$lead=" ...";1174}1175return"$lead$body";11761177}elsif($whereeq'center') {1178$str=~m/^($endre)(.*)$/;1179my($left,$str) = ($1,$2);1180$str=~m/^(.*?)($begre)$/;1181my($mid,$right) = ($1,$2);1182if(length($mid) >5) {1183$left=~s/&[^;]*$//;1184$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1185$mid=" ... ";1186}1187return"$left$mid$right";11881189}else{1190$str=~m/^($endre)(.*)$/;1191my$body=$1;1192my$tail=$2;1193if(length($tail) >4) {1194$body=~s/&[^;]*$//;1195$tail="... ";1196}1197return"$body$tail";1198}1199}12001201# takes the same arguments as chop_str, but also wraps a <span> around the1202# result with a title attribute if it does get chopped. Additionally, the1203# string is HTML-escaped.1204sub chop_and_escape_str {1205my($str) =@_;12061207my$chopped= chop_str(@_);1208if($choppedeq$str) {1209return esc_html($chopped);1210}else{1211$str=~s/([[:cntrl:]])/?/g;1212return$cgi->span({-title=>$str}, esc_html($chopped));1213}1214}12151216## ----------------------------------------------------------------------1217## functions returning short strings12181219# CSS class for given age value (in seconds)1220sub age_class {1221my$age=shift;12221223if(!defined$age) {1224return"noage";1225}elsif($age<60*60*2) {1226return"age0";1227}elsif($age<60*60*24*2) {1228return"age1";1229}else{1230return"age2";1231}1232}12331234# convert age in seconds to "nn units ago" string1235sub age_string {1236my$age=shift;1237my$age_str;12381239if($age>60*60*24*365*2) {1240$age_str= (int$age/60/60/24/365);1241$age_str.=" years ago";1242}elsif($age>60*60*24*(365/12)*2) {1243$age_str=int$age/60/60/24/(365/12);1244$age_str.=" months ago";1245}elsif($age>60*60*24*7*2) {1246$age_str=int$age/60/60/24/7;1247$age_str.=" weeks ago";1248}elsif($age>60*60*24*2) {1249$age_str=int$age/60/60/24;1250$age_str.=" days ago";1251}elsif($age>60*60*2) {1252$age_str=int$age/60/60;1253$age_str.=" hours ago";1254}elsif($age>60*2) {1255$age_str=int$age/60;1256$age_str.=" min ago";1257}elsif($age>2) {1258$age_str=int$age;1259$age_str.=" sec ago";1260}else{1261$age_str.=" right now";1262}1263return$age_str;1264}12651266useconstant{1267 S_IFINVALID =>0030000,1268 S_IFGITLINK =>0160000,1269};12701271# submodule/subproject, a commit object reference1272sub S_ISGITLINK($) {1273my$mode=shift;12741275return(($mode& S_IFMT) == S_IFGITLINK)1276}12771278# convert file mode in octal to symbolic file mode string1279sub mode_str {1280my$mode=oct shift;12811282if(S_ISGITLINK($mode)) {1283return'm---------';1284}elsif(S_ISDIR($mode& S_IFMT)) {1285return'drwxr-xr-x';1286}elsif(S_ISLNK($mode)) {1287return'lrwxrwxrwx';1288}elsif(S_ISREG($mode)) {1289# git cares only about the executable bit1290if($mode& S_IXUSR) {1291return'-rwxr-xr-x';1292}else{1293return'-rw-r--r--';1294};1295}else{1296return'----------';1297}1298}12991300# convert file mode in octal to file type string1301sub file_type {1302my$mode=shift;13031304if($mode!~m/^[0-7]+$/) {1305return$mode;1306}else{1307$mode=oct$mode;1308}13091310if(S_ISGITLINK($mode)) {1311return"submodule";1312}elsif(S_ISDIR($mode& S_IFMT)) {1313return"directory";1314}elsif(S_ISLNK($mode)) {1315return"symlink";1316}elsif(S_ISREG($mode)) {1317return"file";1318}else{1319return"unknown";1320}1321}13221323# convert file mode in octal to file type description string1324sub file_type_long {1325my$mode=shift;13261327if($mode!~m/^[0-7]+$/) {1328return$mode;1329}else{1330$mode=oct$mode;1331}13321333if(S_ISGITLINK($mode)) {1334return"submodule";1335}elsif(S_ISDIR($mode& S_IFMT)) {1336return"directory";1337}elsif(S_ISLNK($mode)) {1338return"symlink";1339}elsif(S_ISREG($mode)) {1340if($mode& S_IXUSR) {1341return"executable";1342}else{1343return"file";1344};1345}else{1346return"unknown";1347}1348}134913501351## ----------------------------------------------------------------------1352## functions returning short HTML fragments, or transforming HTML fragments1353## which don't belong to other sections13541355# format line of commit message.1356sub format_log_line_html {1357my$line=shift;13581359$line= esc_html($line, -nbsp=>1);1360if($line=~m/([0-9a-fA-F]{8,40})/) {1361my$hash_text=$1;1362my$link=1363$cgi->a({-href => href(action=>"object", hash=>$hash_text),1364-class=>"text"},$hash_text);1365$line=~s/$hash_text/$link/;1366}1367return$line;1368}13691370# format marker of refs pointing to given object13711372# the destination action is chosen based on object type and current context:1373# - for annotated tags, we choose the tag view unless it's the current view1374# already, in which case we go to shortlog view1375# - for other refs, we keep the current view if we're in history, shortlog or1376# log view, and select shortlog otherwise1377sub format_ref_marker {1378my($refs,$id) =@_;1379my$markers='';13801381if(defined$refs->{$id}) {1382foreachmy$ref(@{$refs->{$id}}) {1383# this code exploits the fact that non-lightweight tags are the1384# only indirect objects, and that they are the only objects for which1385# we want to use tag instead of shortlog as action1386my($type,$name) =qw();1387my$indirect= ($ref=~s/\^\{\}$//);1388# e.g. tags/v2.6.11 or heads/next1389if($ref=~m!^(.*?)s?/(.*)$!) {1390$type=$1;1391$name=$2;1392}else{1393$type="ref";1394$name=$ref;1395}13961397my$class=$type;1398$class.=" indirect"if$indirect;13991400my$dest_action="shortlog";14011402if($indirect) {1403$dest_action="tag"unless$actioneq"tag";1404}elsif($action=~/^(history|(short)?log)$/) {1405$dest_action=$action;1406}14071408my$dest="";1409$dest.="refs/"unless$ref=~ m!^refs/!;1410$dest.=$ref;14111412my$link=$cgi->a({1413-href => href(1414 action=>$dest_action,1415 hash=>$dest1416)},$name);14171418$markers.=" <span class=\"$class\"title=\"$ref\">".1419$link."</span>";1420}1421}14221423if($markers) {1424return' <span class="refs">'.$markers.'</span>';1425}else{1426return"";1427}1428}14291430# format, perhaps shortened and with markers, title line1431sub format_subject_html {1432my($long,$short,$href,$extra) =@_;1433$extra=''unlessdefined($extra);14341435if(length($short) <length($long)) {1436return$cgi->a({-href =>$href, -class=>"list subject",1437-title => to_utf8($long)},1438 esc_html($short) .$extra);1439}else{1440return$cgi->a({-href =>$href, -class=>"list subject"},1441 esc_html($long) .$extra);1442}1443}14441445# format git diff header line, i.e. "diff --(git|combined|cc) ..."1446sub format_git_diff_header_line {1447my$line=shift;1448my$diffinfo=shift;1449my($from,$to) =@_;14501451if($diffinfo->{'nparents'}) {1452# combined diff1453$line=~s!^(diff (.*?) )"?.*$!$1!;1454if($to->{'href'}) {1455$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1456 esc_path($to->{'file'}));1457}else{# file was deleted (no href)1458$line.= esc_path($to->{'file'});1459}1460}else{1461# "ordinary" diff1462$line=~s!^(diff (.*?) )"?a/.*$!$1!;1463if($from->{'href'}) {1464$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1465'a/'. esc_path($from->{'file'}));1466}else{# file was added (no href)1467$line.='a/'. esc_path($from->{'file'});1468}1469$line.=' ';1470if($to->{'href'}) {1471$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1472'b/'. esc_path($to->{'file'}));1473}else{# file was deleted1474$line.='b/'. esc_path($to->{'file'});1475}1476}14771478return"<div class=\"diff header\">$line</div>\n";1479}14801481# format extended diff header line, before patch itself1482sub format_extended_diff_header_line {1483my$line=shift;1484my$diffinfo=shift;1485my($from,$to) =@_;14861487# match <path>1488if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1489$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1490 esc_path($from->{'file'}));1491}1492if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1493$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1494 esc_path($to->{'file'}));1495}1496# match single <mode>1497if($line=~m/\s(\d{6})$/) {1498$line.='<span class="info"> ('.1499 file_type_long($1) .1500')</span>';1501}1502# match <hash>1503if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1504# can match only for combined diff1505$line='index ';1506for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1507if($from->{'href'}[$i]) {1508$line.=$cgi->a({-href=>$from->{'href'}[$i],1509-class=>"hash"},1510substr($diffinfo->{'from_id'}[$i],0,7));1511}else{1512$line.='0' x 7;1513}1514# separator1515$line.=','if($i<$diffinfo->{'nparents'} -1);1516}1517$line.='..';1518if($to->{'href'}) {1519$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1520substr($diffinfo->{'to_id'},0,7));1521}else{1522$line.='0' x 7;1523}15241525}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1526# can match only for ordinary diff1527my($from_link,$to_link);1528if($from->{'href'}) {1529$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1530substr($diffinfo->{'from_id'},0,7));1531}else{1532$from_link='0' x 7;1533}1534if($to->{'href'}) {1535$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1536substr($diffinfo->{'to_id'},0,7));1537}else{1538$to_link='0' x 7;1539}1540my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1541$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1542}15431544return$line."<br/>\n";1545}15461547# format from-file/to-file diff header1548sub format_diff_from_to_header {1549my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1550my$line;1551my$result='';15521553$line=$from_line;1554#assert($line =~ m/^---/) if DEBUG;1555# no extra formatting for "^--- /dev/null"1556if(!$diffinfo->{'nparents'}) {1557# ordinary (single parent) diff1558if($line=~m!^--- "?a/!) {1559if($from->{'href'}) {1560$line='--- a/'.1561$cgi->a({-href=>$from->{'href'}, -class=>"path"},1562 esc_path($from->{'file'}));1563}else{1564$line='--- a/'.1565 esc_path($from->{'file'});1566}1567}1568$result.= qq!<div class="diff from_file">$line</div>\n!;15691570}else{1571# combined diff (merge commit)1572for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1573if($from->{'href'}[$i]) {1574$line='--- '.1575$cgi->a({-href=>href(action=>"blobdiff",1576 hash_parent=>$diffinfo->{'from_id'}[$i],1577 hash_parent_base=>$parents[$i],1578 file_parent=>$from->{'file'}[$i],1579 hash=>$diffinfo->{'to_id'},1580 hash_base=>$hash,1581 file_name=>$to->{'file'}),1582-class=>"path",1583-title=>"diff". ($i+1)},1584$i+1) .1585'/'.1586$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1587 esc_path($from->{'file'}[$i]));1588}else{1589$line='--- /dev/null';1590}1591$result.= qq!<div class="diff from_file">$line</div>\n!;1592}1593}15941595$line=$to_line;1596#assert($line =~ m/^\+\+\+/) if DEBUG;1597# no extra formatting for "^+++ /dev/null"1598if($line=~m!^\+\+\+ "?b/!) {1599if($to->{'href'}) {1600$line='+++ b/'.1601$cgi->a({-href=>$to->{'href'}, -class=>"path"},1602 esc_path($to->{'file'}));1603}else{1604$line='+++ b/'.1605 esc_path($to->{'file'});1606}1607}1608$result.= qq!<div class="diff to_file">$line</div>\n!;16091610return$result;1611}16121613# create note for patch simplified by combined diff1614sub format_diff_cc_simplified {1615my($diffinfo,@parents) =@_;1616my$result='';16171618$result.="<div class=\"diff header\">".1619"diff --cc ";1620if(!is_deleted($diffinfo)) {1621$result.=$cgi->a({-href => href(action=>"blob",1622 hash_base=>$hash,1623 hash=>$diffinfo->{'to_id'},1624 file_name=>$diffinfo->{'to_file'}),1625-class=>"path"},1626 esc_path($diffinfo->{'to_file'}));1627}else{1628$result.= esc_path($diffinfo->{'to_file'});1629}1630$result.="</div>\n".# class="diff header"1631"<div class=\"diff nodifferences\">".1632"Simple merge".1633"</div>\n";# class="diff nodifferences"16341635return$result;1636}16371638# format patch (diff) line (not to be used for diff headers)1639sub format_diff_line {1640my$line=shift;1641my($from,$to) =@_;1642my$diff_class="";16431644chomp$line;16451646if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1647# combined diff1648my$prefix=substr($line,0,scalar@{$from->{'href'}});1649if($line=~m/^\@{3}/) {1650$diff_class=" chunk_header";1651}elsif($line=~m/^\\/) {1652$diff_class=" incomplete";1653}elsif($prefix=~tr/+/+/) {1654$diff_class=" add";1655}elsif($prefix=~tr/-/-/) {1656$diff_class=" rem";1657}1658}else{1659# assume ordinary diff1660my$char=substr($line,0,1);1661if($chareq'+') {1662$diff_class=" add";1663}elsif($chareq'-') {1664$diff_class=" rem";1665}elsif($chareq'@') {1666$diff_class=" chunk_header";1667}elsif($chareq"\\") {1668$diff_class=" incomplete";1669}1670}1671$line= untabify($line);1672if($from&&$to&&$line=~m/^\@{2} /) {1673my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1674$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;16751676$from_lines=0unlessdefined$from_lines;1677$to_lines=0unlessdefined$to_lines;16781679if($from->{'href'}) {1680$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1681-class=>"list"},$from_text);1682}1683if($to->{'href'}) {1684$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1685-class=>"list"},$to_text);1686}1687$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1688"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1689return"<div class=\"diff$diff_class\">$line</div>\n";1690}elsif($from&&$to&&$line=~m/^\@{3}/) {1691my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1692my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);16931694@from_text=split(' ',$ranges);1695for(my$i=0;$i<@from_text; ++$i) {1696($from_start[$i],$from_nlines[$i]) =1697(split(',',substr($from_text[$i],1)),0);1698}16991700$to_text=pop@from_text;1701$to_start=pop@from_start;1702$to_nlines=pop@from_nlines;17031704$line="<span class=\"chunk_info\">$prefix";1705for(my$i=0;$i<@from_text; ++$i) {1706if($from->{'href'}[$i]) {1707$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1708-class=>"list"},$from_text[$i]);1709}else{1710$line.=$from_text[$i];1711}1712$line.=" ";1713}1714if($to->{'href'}) {1715$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1716-class=>"list"},$to_text);1717}else{1718$line.=$to_text;1719}1720$line.="$prefix</span>".1721"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1722return"<div class=\"diff$diff_class\">$line</div>\n";1723}1724return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1725}17261727# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1728# linked. Pass the hash of the tree/commit to snapshot.1729sub format_snapshot_links {1730my($hash) =@_;1731my$num_fmts=@snapshot_fmts;1732if($num_fmts>1) {1733# A parenthesized list of links bearing format names.1734# e.g. "snapshot (_tar.gz_ _zip_)"1735return"snapshot (".join(' ',map1736$cgi->a({1737-href => href(1738 action=>"snapshot",1739 hash=>$hash,1740 snapshot_format=>$_1741)1742},$known_snapshot_formats{$_}{'display'})1743,@snapshot_fmts) .")";1744}elsif($num_fmts==1) {1745# A single "snapshot" link whose tooltip bears the format name.1746# i.e. "_snapshot_"1747my($fmt) =@snapshot_fmts;1748return1749$cgi->a({1750-href => href(1751 action=>"snapshot",1752 hash=>$hash,1753 snapshot_format=>$fmt1754),1755-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1756},"snapshot");1757}else{# $num_fmts == 01758returnundef;1759}1760}17611762## ......................................................................1763## functions returning values to be passed, perhaps after some1764## transformation, to other functions; e.g. returning arguments to href()17651766# returns hash to be passed to href to generate gitweb URL1767# in -title key it returns description of link1768sub get_feed_info {1769my$format=shift||'Atom';1770my%res= (action =>lc($format));17711772# feed links are possible only for project views1773return unless(defined$project);1774# some views should link to OPML, or to generic project feed,1775# or don't have specific feed yet (so they should use generic)1776return if($action=~/^(?:tags|heads|forks|tag|search)$/x);17771778my$branch;1779# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1780# from tag links; this also makes possible to detect branch links1781if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1782(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1783$branch=$1;1784}1785# find log type for feed description (title)1786my$type='log';1787if(defined$file_name) {1788$type="history of$file_name";1789$type.="/"if($actioneq'tree');1790$type.=" on '$branch'"if(defined$branch);1791}else{1792$type="log of$branch"if(defined$branch);1793}17941795$res{-title} =$type;1796$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1797$res{'file_name'} =$file_name;17981799return%res;1800}18011802## ----------------------------------------------------------------------1803## git utility subroutines, invoking git commands18041805# returns path to the core git executable and the --git-dir parameter as list1806sub git_cmd {1807return$GIT,'--git-dir='.$git_dir;1808}18091810# quote the given arguments for passing them to the shell1811# quote_command("command", "arg 1", "arg with ' and ! characters")1812# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1813# Try to avoid using this function wherever possible.1814sub quote_command {1815returnjoin(' ',1816map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1817}18181819# get HEAD ref of given project as hash1820sub git_get_head_hash {1821my$project=shift;1822my$o_git_dir=$git_dir;1823my$retval=undef;1824$git_dir="$projectroot/$project";1825if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1826my$head= <$fd>;1827close$fd;1828if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1829$retval=$1;1830}1831}1832if(defined$o_git_dir) {1833$git_dir=$o_git_dir;1834}1835return$retval;1836}18371838# get type of given object1839sub git_get_type {1840my$hash=shift;18411842open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1843my$type= <$fd>;1844close$fdorreturn;1845chomp$type;1846return$type;1847}18481849# repository configuration1850our$config_file='';1851our%config;18521853# store multiple values for single key as anonymous array reference1854# single values stored directly in the hash, not as [ <value> ]1855sub hash_set_multi {1856my($hash,$key,$value) =@_;18571858if(!exists$hash->{$key}) {1859$hash->{$key} =$value;1860}elsif(!ref$hash->{$key}) {1861$hash->{$key} = [$hash->{$key},$value];1862}else{1863push@{$hash->{$key}},$value;1864}1865}18661867# return hash of git project configuration1868# optionally limited to some section, e.g. 'gitweb'1869sub git_parse_project_config {1870my$section_regexp=shift;1871my%config;18721873local$/="\0";18741875open my$fh,"-|", git_cmd(),"config",'-z','-l',1876orreturn;18771878while(my$keyval= <$fh>) {1879chomp$keyval;1880my($key,$value) =split(/\n/,$keyval,2);18811882 hash_set_multi(\%config,$key,$value)1883if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1884}1885close$fh;18861887return%config;1888}18891890# convert config value to boolean, 'true' or 'false'1891# no value, number > 0, 'true' and 'yes' values are true1892# rest of values are treated as false (never as error)1893sub config_to_bool {1894my$val=shift;18951896# strip leading and trailing whitespace1897$val=~s/^\s+//;1898$val=~s/\s+$//;18991900return(!defined$val||# section.key1901($val=~/^\d+$/&&$val) ||# section.key = 11902($val=~/^(?:true|yes)$/i));# section.key = true1903}19041905# convert config value to simple decimal number1906# an optional value suffix of 'k', 'm', or 'g' will cause the value1907# to be multiplied by 1024, 1048576, or 10737418241908sub config_to_int {1909my$val=shift;19101911# strip leading and trailing whitespace1912$val=~s/^\s+//;1913$val=~s/\s+$//;19141915if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1916$unit=lc($unit);1917# unknown unit is treated as 11918return$num* ($uniteq'g'?1073741824:1919$uniteq'm'?1048576:1920$uniteq'k'?1024:1);1921}1922return$val;1923}19241925# convert config value to array reference, if needed1926sub config_to_multi {1927my$val=shift;19281929returnref($val) ?$val: (defined($val) ? [$val] : []);1930}19311932sub git_get_project_config {1933my($key,$type) =@_;19341935# key sanity check1936return unless($key);1937$key=~s/^gitweb\.//;1938return if($key=~m/\W/);19391940# type sanity check1941if(defined$type) {1942$type=~s/^--//;1943$type=undef1944unless($typeeq'bool'||$typeeq'int');1945}19461947# get config1948if(!defined$config_file||1949$config_filene"$git_dir/config") {1950%config= git_parse_project_config('gitweb');1951$config_file="$git_dir/config";1952}19531954# ensure given type1955if(!defined$type) {1956return$config{"gitweb.$key"};1957}elsif($typeeq'bool') {1958# backward compatibility: 'git config --bool' returns true/false1959return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1960}elsif($typeeq'int') {1961return config_to_int($config{"gitweb.$key"});1962}1963return$config{"gitweb.$key"};1964}19651966# get hash of given path at given ref1967sub git_get_hash_by_path {1968my$base=shift;1969my$path=shift||returnundef;1970my$type=shift;19711972$path=~ s,/+$,,;19731974open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path1975or die_error(500,"Open git-ls-tree failed");1976my$line= <$fd>;1977close$fdorreturnundef;19781979if(!defined$line) {1980# there is no tree or hash given by $path at $base1981returnundef;1982}19831984#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'1985$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;1986if(defined$type&&$typene$2) {1987# type doesn't match1988returnundef;1989}1990return$3;1991}19921993# get path of entry with given hash at given tree-ish (ref)1994# used to get 'from' filename for combined diff (merge commit) for renames1995sub git_get_path_by_hash {1996my$base=shift||return;1997my$hash=shift||return;19981999local$/="\0";20002001open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2002orreturnundef;2003while(my$line= <$fd>) {2004chomp$line;20052006#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2007#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2008if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2009close$fd;2010return$1;2011}2012}2013close$fd;2014returnundef;2015}20162017## ......................................................................2018## git utility functions, directly accessing git repository20192020sub git_get_project_description {2021my$path=shift;20222023$git_dir="$projectroot/$path";2024open my$fd,"$git_dir/description"2025orreturn git_get_project_config('description');2026my$descr= <$fd>;2027close$fd;2028if(defined$descr) {2029chomp$descr;2030}2031return$descr;2032}20332034sub git_get_project_ctags {2035my$path=shift;2036my$ctags= {};20372038$git_dir="$projectroot/$path";2039unless(opendir D,"$git_dir/ctags") {2040return$ctags;2041}2042foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir(D)) {2043open CT,$_ornext;2044my$val= <CT>;2045chomp$val;2046close CT;2047my$ctag=$_;$ctag=~ s#.*/##;2048$ctags->{$ctag} =$val;2049}2050closedir D;2051$ctags;2052}20532054sub git_populate_project_tagcloud {2055my$ctags=shift;20562057# First, merge different-cased tags; tags vote on casing2058my%ctags_lc;2059foreach(keys%$ctags) {2060$ctags_lc{lc$_}->{count} +=$ctags->{$_};2061if(not$ctags_lc{lc$_}->{topcount}2062or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2063$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2064$ctags_lc{lc$_}->{topname} =$_;2065}2066}20672068my$cloud;2069if(eval{require HTML::TagCloud;1; }) {2070$cloud= HTML::TagCloud->new;2071foreach(sort keys%ctags_lc) {2072# Pad the title with spaces so that the cloud looks2073# less crammed.2074my$title=$ctags_lc{$_}->{topname};2075$title=~s/ / /g;2076$title=~s/^/ /g;2077$title=~s/$/ /g;2078$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2079}2080}else{2081$cloud= \%ctags_lc;2082}2083$cloud;2084}20852086sub git_show_project_tagcloud {2087my($cloud,$count) =@_;2088print STDERR ref($cloud)."..\n";2089if(ref$cloudeq'HTML::TagCloud') {2090return$cloud->html_and_css($count);2091}else{2092my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2093return'<p align="center">'.join(', ',map{2094"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2095}splice(@tags,0,$count)) .'</p>';2096}2097}20982099sub git_get_project_url_list {2100my$path=shift;21012102$git_dir="$projectroot/$path";2103open my$fd,"$git_dir/cloneurl"2104orreturnwantarray?2105@{ config_to_multi(git_get_project_config('url')) } :2106 config_to_multi(git_get_project_config('url'));2107my@git_project_url_list=map{chomp;$_} <$fd>;2108close$fd;21092110returnwantarray?@git_project_url_list: \@git_project_url_list;2111}21122113sub git_get_projects_list {2114my($filter) =@_;2115my@list;21162117$filter||='';2118$filter=~s/\.git$//;21192120my$check_forks= gitweb_check_feature('forks');21212122if(-d $projects_list) {2123# search in directory2124my$dir=$projects_list. ($filter?"/$filter":'');2125# remove the trailing "/"2126$dir=~s!/+$!!;2127my$pfxlen=length("$dir");2128my$pfxdepth= ($dir=~tr!/!!);21292130 File::Find::find({2131 follow_fast =>1,# follow symbolic links2132 follow_skip =>2,# ignore duplicates2133 dangling_symlinks =>0,# ignore dangling symlinks, silently2134 wanted =>sub{2135# skip project-list toplevel, if we get it.2136return if(m!^[/.]$!);2137# only directories can be git repositories2138return unless(-d $_);2139# don't traverse too deep (Find is super slow on os x)2140if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2141$File::Find::prune =1;2142return;2143}21442145my$subdir=substr($File::Find::name,$pfxlen+1);2146# we check related file in $projectroot2147if(check_export_ok("$projectroot/$filter/$subdir")) {2148push@list, { path => ($filter?"$filter/":'') .$subdir};2149$File::Find::prune =1;2150}2151},2152},"$dir");21532154}elsif(-f $projects_list) {2155# read from file(url-encoded):2156# 'git%2Fgit.git Linus+Torvalds'2157# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2158# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2159my%paths;2160open my($fd),$projects_listorreturn;2161 PROJECT:2162while(my$line= <$fd>) {2163chomp$line;2164my($path,$owner) =split' ',$line;2165$path= unescape($path);2166$owner= unescape($owner);2167if(!defined$path) {2168next;2169}2170if($filterne'') {2171# looking for forks;2172my$pfx=substr($path,0,length($filter));2173if($pfxne$filter) {2174next PROJECT;2175}2176my$sfx=substr($path,length($filter));2177if($sfx!~/^\/.*\.git$/) {2178next PROJECT;2179}2180}elsif($check_forks) {2181 PATH:2182foreachmy$filter(keys%paths) {2183# looking for forks;2184my$pfx=substr($path,0,length($filter));2185if($pfxne$filter) {2186next PATH;2187}2188my$sfx=substr($path,length($filter));2189if($sfx!~/^\/.*\.git$/) {2190next PATH;2191}2192# is a fork, don't include it in2193# the list2194next PROJECT;2195}2196}2197if(check_export_ok("$projectroot/$path")) {2198my$pr= {2199 path =>$path,2200 owner => to_utf8($owner),2201};2202push@list,$pr;2203(my$forks_path=$path) =~s/\.git$//;2204$paths{$forks_path}++;2205}2206}2207close$fd;2208}2209return@list;2210}22112212our$gitweb_project_owner=undef;2213sub git_get_project_list_from_file {22142215return if(defined$gitweb_project_owner);22162217$gitweb_project_owner= {};2218# read from file (url-encoded):2219# 'git%2Fgit.git Linus+Torvalds'2220# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2221# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2222if(-f $projects_list) {2223open(my$fd,$projects_list);2224while(my$line= <$fd>) {2225chomp$line;2226my($pr,$ow) =split' ',$line;2227$pr= unescape($pr);2228$ow= unescape($ow);2229$gitweb_project_owner->{$pr} = to_utf8($ow);2230}2231close$fd;2232}2233}22342235sub git_get_project_owner {2236my$project=shift;2237my$owner;22382239returnundefunless$project;2240$git_dir="$projectroot/$project";22412242if(!defined$gitweb_project_owner) {2243 git_get_project_list_from_file();2244}22452246if(exists$gitweb_project_owner->{$project}) {2247$owner=$gitweb_project_owner->{$project};2248}2249if(!defined$owner){2250$owner= git_get_project_config('owner');2251}2252if(!defined$owner) {2253$owner= get_file_owner("$git_dir");2254}22552256return$owner;2257}22582259sub git_get_last_activity {2260my($path) =@_;2261my$fd;22622263$git_dir="$projectroot/$path";2264open($fd,"-|", git_cmd(),'for-each-ref',2265'--format=%(committer)',2266'--sort=-committerdate',2267'--count=1',2268'refs/heads')orreturn;2269my$most_recent= <$fd>;2270close$fdorreturn;2271if(defined$most_recent&&2272$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2273my$timestamp=$1;2274my$age=time-$timestamp;2275return($age, age_string($age));2276}2277return(undef,undef);2278}22792280sub git_get_references {2281my$type=shift||"";2282my%refs;2283# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112284# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2285open my$fd,"-|", git_cmd(),"show-ref","--dereference",2286($type? ("--","refs/$type") : ())# use -- <pattern> if $type2287orreturn;22882289while(my$line= <$fd>) {2290chomp$line;2291if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2292if(defined$refs{$1}) {2293push@{$refs{$1}},$2;2294}else{2295$refs{$1} = [$2];2296}2297}2298}2299close$fdorreturn;2300return \%refs;2301}23022303sub git_get_rev_name_tags {2304my$hash=shift||returnundef;23052306open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2307orreturn;2308my$name_rev= <$fd>;2309close$fd;23102311if($name_rev=~ m|^$hash tags/(.*)$|) {2312return$1;2313}else{2314# catches also '$hash undefined' output2315returnundef;2316}2317}23182319## ----------------------------------------------------------------------2320## parse to hash functions23212322sub parse_date {2323my$epoch=shift;2324my$tz=shift||"-0000";23252326my%date;2327my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2328my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2329my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2330$date{'hour'} =$hour;2331$date{'minute'} =$min;2332$date{'mday'} =$mday;2333$date{'day'} =$days[$wday];2334$date{'month'} =$months[$mon];2335$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2336$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2337$date{'mday-time'} =sprintf"%d%s%02d:%02d",2338$mday,$months[$mon],$hour,$min;2339$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",23401900+$year,1+$mon,$mday,$hour,$min,$sec;23412342$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2343my$local=$epoch+ ((int$1+ ($2/60)) *3600);2344($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2345$date{'hour_local'} =$hour;2346$date{'minute_local'} =$min;2347$date{'tz_local'} =$tz;2348$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",23491900+$year,$mon+1,$mday,2350$hour,$min,$sec,$tz);2351return%date;2352}23532354sub parse_tag {2355my$tag_id=shift;2356my%tag;2357my@comment;23582359open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2360$tag{'id'} =$tag_id;2361while(my$line= <$fd>) {2362chomp$line;2363if($line=~m/^object ([0-9a-fA-F]{40})$/) {2364$tag{'object'} =$1;2365}elsif($line=~m/^type (.+)$/) {2366$tag{'type'} =$1;2367}elsif($line=~m/^tag (.+)$/) {2368$tag{'name'} =$1;2369}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2370$tag{'author'} =$1;2371$tag{'epoch'} =$2;2372$tag{'tz'} =$3;2373}elsif($line=~m/--BEGIN/) {2374push@comment,$line;2375last;2376}elsif($lineeq"") {2377last;2378}2379}2380push@comment, <$fd>;2381$tag{'comment'} = \@comment;2382close$fdorreturn;2383if(!defined$tag{'name'}) {2384return2385};2386return%tag2387}23882389sub parse_commit_text {2390my($commit_text,$withparents) =@_;2391my@commit_lines=split'\n',$commit_text;2392my%co;23932394pop@commit_lines;# Remove '\0'23952396if(!@commit_lines) {2397return;2398}23992400my$header=shift@commit_lines;2401if($header!~m/^[0-9a-fA-F]{40}/) {2402return;2403}2404($co{'id'},my@parents) =split' ',$header;2405while(my$line=shift@commit_lines) {2406last if$lineeq"\n";2407if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2408$co{'tree'} =$1;2409}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2410push@parents,$1;2411}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2412$co{'author'} =$1;2413$co{'author_epoch'} =$2;2414$co{'author_tz'} =$3;2415if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2416$co{'author_name'} =$1;2417$co{'author_email'} =$2;2418}else{2419$co{'author_name'} =$co{'author'};2420}2421}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2422$co{'committer'} =$1;2423$co{'committer_epoch'} =$2;2424$co{'committer_tz'} =$3;2425$co{'committer_name'} =$co{'committer'};2426if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2427$co{'committer_name'} =$1;2428$co{'committer_email'} =$2;2429}else{2430$co{'committer_name'} =$co{'committer'};2431}2432}2433}2434if(!defined$co{'tree'}) {2435return;2436};2437$co{'parents'} = \@parents;2438$co{'parent'} =$parents[0];24392440foreachmy$title(@commit_lines) {2441$title=~s/^ //;2442if($titlene"") {2443$co{'title'} = chop_str($title,80,5);2444# remove leading stuff of merges to make the interesting part visible2445if(length($title) >50) {2446$title=~s/^Automatic //;2447$title=~s/^merge (of|with) /Merge ... /i;2448if(length($title) >50) {2449$title=~s/(http|rsync):\/\///;2450}2451if(length($title) >50) {2452$title=~s/(master|www|rsync)\.//;2453}2454if(length($title) >50) {2455$title=~s/kernel.org:?//;2456}2457if(length($title) >50) {2458$title=~s/\/pub\/scm//;2459}2460}2461$co{'title_short'} = chop_str($title,50,5);2462last;2463}2464}2465if(!defined$co{'title'} ||$co{'title'}eq"") {2466$co{'title'} =$co{'title_short'} ='(no commit message)';2467}2468# remove added spaces2469foreachmy$line(@commit_lines) {2470$line=~s/^ //;2471}2472$co{'comment'} = \@commit_lines;24732474my$age=time-$co{'committer_epoch'};2475$co{'age'} =$age;2476$co{'age_string'} = age_string($age);2477my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2478if($age>60*60*24*7*2) {2479$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2480$co{'age_string_age'} =$co{'age_string'};2481}else{2482$co{'age_string_date'} =$co{'age_string'};2483$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2484}2485return%co;2486}24872488sub parse_commit {2489my($commit_id) =@_;2490my%co;24912492local$/="\0";24932494open my$fd,"-|", git_cmd(),"rev-list",2495"--parents",2496"--header",2497"--max-count=1",2498$commit_id,2499"--",2500or die_error(500,"Open git-rev-list failed");2501%co= parse_commit_text(<$fd>,1);2502close$fd;25032504return%co;2505}25062507sub parse_commits {2508my($commit_id,$maxcount,$skip,$filename,@args) =@_;2509my@cos;25102511$maxcount||=1;2512$skip||=0;25132514local$/="\0";25152516open my$fd,"-|", git_cmd(),"rev-list",2517"--header",2518@args,2519("--max-count=".$maxcount),2520("--skip=".$skip),2521@extra_options,2522$commit_id,2523"--",2524($filename? ($filename) : ())2525or die_error(500,"Open git-rev-list failed");2526while(my$line= <$fd>) {2527my%co= parse_commit_text($line);2528push@cos, \%co;2529}2530close$fd;25312532returnwantarray?@cos: \@cos;2533}25342535# parse line of git-diff-tree "raw" output2536sub parse_difftree_raw_line {2537my$line=shift;2538my%res;25392540# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2541# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2542if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2543$res{'from_mode'} =$1;2544$res{'to_mode'} =$2;2545$res{'from_id'} =$3;2546$res{'to_id'} =$4;2547$res{'status'} =$5;2548$res{'similarity'} =$6;2549if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2550($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2551}else{2552$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2553}2554}2555# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2556# combined diff (for merge commit)2557elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2558$res{'nparents'} =length($1);2559$res{'from_mode'} = [split(' ',$2) ];2560$res{'to_mode'} =pop@{$res{'from_mode'}};2561$res{'from_id'} = [split(' ',$3) ];2562$res{'to_id'} =pop@{$res{'from_id'}};2563$res{'status'} = [split('',$4) ];2564$res{'to_file'} = unquote($5);2565}2566# 'c512b523472485aef4fff9e57b229d9d243c967f'2567elsif($line=~m/^([0-9a-fA-F]{40})$/) {2568$res{'commit'} =$1;2569}25702571returnwantarray?%res: \%res;2572}25732574# wrapper: return parsed line of git-diff-tree "raw" output2575# (the argument might be raw line, or parsed info)2576sub parsed_difftree_line {2577my$line_or_ref=shift;25782579if(ref($line_or_ref)eq"HASH") {2580# pre-parsed (or generated by hand)2581return$line_or_ref;2582}else{2583return parse_difftree_raw_line($line_or_ref);2584}2585}25862587# parse line of git-ls-tree output2588sub parse_ls_tree_line ($;%) {2589my$line=shift;2590my%opts=@_;2591my%res;25922593#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2594$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;25952596$res{'mode'} =$1;2597$res{'type'} =$2;2598$res{'hash'} =$3;2599if($opts{'-z'}) {2600$res{'name'} =$4;2601}else{2602$res{'name'} = unquote($4);2603}26042605returnwantarray?%res: \%res;2606}26072608# generates _two_ hashes, references to which are passed as 2 and 3 argument2609sub parse_from_to_diffinfo {2610my($diffinfo,$from,$to,@parents) =@_;26112612if($diffinfo->{'nparents'}) {2613# combined diff2614$from->{'file'} = [];2615$from->{'href'} = [];2616 fill_from_file_info($diffinfo,@parents)2617unlessexists$diffinfo->{'from_file'};2618for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2619$from->{'file'}[$i] =2620defined$diffinfo->{'from_file'}[$i] ?2621$diffinfo->{'from_file'}[$i] :2622$diffinfo->{'to_file'};2623if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2624$from->{'href'}[$i] = href(action=>"blob",2625 hash_base=>$parents[$i],2626 hash=>$diffinfo->{'from_id'}[$i],2627 file_name=>$from->{'file'}[$i]);2628}else{2629$from->{'href'}[$i] =undef;2630}2631}2632}else{2633# ordinary (not combined) diff2634$from->{'file'} =$diffinfo->{'from_file'};2635if($diffinfo->{'status'}ne"A") {# not new (added) file2636$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2637 hash=>$diffinfo->{'from_id'},2638 file_name=>$from->{'file'});2639}else{2640delete$from->{'href'};2641}2642}26432644$to->{'file'} =$diffinfo->{'to_file'};2645if(!is_deleted($diffinfo)) {# file exists in result2646$to->{'href'} = href(action=>"blob", hash_base=>$hash,2647 hash=>$diffinfo->{'to_id'},2648 file_name=>$to->{'file'});2649}else{2650delete$to->{'href'};2651}2652}26532654## ......................................................................2655## parse to array of hashes functions26562657sub git_get_heads_list {2658my$limit=shift;2659my@headslist;26602661open my$fd,'-|', git_cmd(),'for-each-ref',2662($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2663'--format=%(objectname) %(refname) %(subject)%00%(committer)',2664'refs/heads'2665orreturn;2666while(my$line= <$fd>) {2667my%ref_item;26682669chomp$line;2670my($refinfo,$committerinfo) =split(/\0/,$line);2671my($hash,$name,$title) =split(' ',$refinfo,3);2672my($committer,$epoch,$tz) =2673($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2674$ref_item{'fullname'} =$name;2675$name=~s!^refs/heads/!!;26762677$ref_item{'name'} =$name;2678$ref_item{'id'} =$hash;2679$ref_item{'title'} =$title||'(no commit message)';2680$ref_item{'epoch'} =$epoch;2681if($epoch) {2682$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2683}else{2684$ref_item{'age'} ="unknown";2685}26862687push@headslist, \%ref_item;2688}2689close$fd;26902691returnwantarray?@headslist: \@headslist;2692}26932694sub git_get_tags_list {2695my$limit=shift;2696my@tagslist;26972698open my$fd,'-|', git_cmd(),'for-each-ref',2699($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2700'--format=%(objectname) %(objecttype) %(refname) '.2701'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2702'refs/tags'2703orreturn;2704while(my$line= <$fd>) {2705my%ref_item;27062707chomp$line;2708my($refinfo,$creatorinfo) =split(/\0/,$line);2709my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2710my($creator,$epoch,$tz) =2711($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2712$ref_item{'fullname'} =$name;2713$name=~s!^refs/tags/!!;27142715$ref_item{'type'} =$type;2716$ref_item{'id'} =$id;2717$ref_item{'name'} =$name;2718if($typeeq"tag") {2719$ref_item{'subject'} =$title;2720$ref_item{'reftype'} =$reftype;2721$ref_item{'refid'} =$refid;2722}else{2723$ref_item{'reftype'} =$type;2724$ref_item{'refid'} =$id;2725}27262727if($typeeq"tag"||$typeeq"commit") {2728$ref_item{'epoch'} =$epoch;2729if($epoch) {2730$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2731}else{2732$ref_item{'age'} ="unknown";2733}2734}27352736push@tagslist, \%ref_item;2737}2738close$fd;27392740returnwantarray?@tagslist: \@tagslist;2741}27422743## ----------------------------------------------------------------------2744## filesystem-related functions27452746sub get_file_owner {2747my$path=shift;27482749my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2750my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2751if(!defined$gcos) {2752returnundef;2753}2754my$owner=$gcos;2755$owner=~s/[,;].*$//;2756return to_utf8($owner);2757}27582759## ......................................................................2760## mimetype related functions27612762sub mimetype_guess_file {2763my$filename=shift;2764my$mimemap=shift;2765-r $mimemaporreturnundef;27662767my%mimemap;2768open(MIME,$mimemap)orreturnundef;2769while(<MIME>) {2770next ifm/^#/;# skip comments2771my($mime,$exts) =split(/\t+/);2772if(defined$exts) {2773my@exts=split(/\s+/,$exts);2774foreachmy$ext(@exts) {2775$mimemap{$ext} =$mime;2776}2777}2778}2779close(MIME);27802781$filename=~/\.([^.]*)$/;2782return$mimemap{$1};2783}27842785sub mimetype_guess {2786my$filename=shift;2787my$mime;2788$filename=~/\./orreturnundef;27892790if($mimetypes_file) {2791my$file=$mimetypes_file;2792if($file!~m!^/!) {# if it is relative path2793# it is relative to project2794$file="$projectroot/$project/$file";2795}2796$mime= mimetype_guess_file($filename,$file);2797}2798$mime||= mimetype_guess_file($filename,'/etc/mime.types');2799return$mime;2800}28012802sub blob_mimetype {2803my$fd=shift;2804my$filename=shift;28052806if($filename) {2807my$mime= mimetype_guess($filename);2808$mimeandreturn$mime;2809}28102811# just in case2812return$default_blob_plain_mimetypeunless$fd;28132814if(-T $fd) {2815return'text/plain';2816}elsif(!$filename) {2817return'application/octet-stream';2818}elsif($filename=~m/\.png$/i) {2819return'image/png';2820}elsif($filename=~m/\.gif$/i) {2821return'image/gif';2822}elsif($filename=~m/\.jpe?g$/i) {2823return'image/jpeg';2824}else{2825return'application/octet-stream';2826}2827}28282829sub blob_contenttype {2830my($fd,$file_name,$type) =@_;28312832$type||= blob_mimetype($fd,$file_name);2833if($typeeq'text/plain'&&defined$default_text_plain_charset) {2834$type.="; charset=$default_text_plain_charset";2835}28362837return$type;2838}28392840## ======================================================================2841## functions printing HTML: header, footer, error page28422843sub git_header_html {2844my$status=shift||"200 OK";2845my$expires=shift;28462847my$title="$site_name";2848if(defined$project) {2849$title.=" - ". to_utf8($project);2850if(defined$action) {2851$title.="/$action";2852if(defined$file_name) {2853$title.=" - ". esc_path($file_name);2854if($actioneq"tree"&&$file_name!~ m|/$|) {2855$title.="/";2856}2857}2858}2859}2860my$content_type;2861# require explicit support from the UA if we are to send the page as2862# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2863# we have to do this because MSIE sometimes globs '*/*', pretending to2864# support xhtml+xml but choking when it gets what it asked for.2865if(defined$cgi->http('HTTP_ACCEPT') &&2866$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2867$cgi->Accept('application/xhtml+xml') !=0) {2868$content_type='application/xhtml+xml';2869}else{2870$content_type='text/html';2871}2872print$cgi->header(-type=>$content_type, -charset =>'utf-8',2873-status=>$status, -expires =>$expires);2874my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2875print<<EOF;2876<?xml version="1.0" encoding="utf-8"?>2877<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2878<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2879<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2880<!-- git core binaries version$git_version-->2881<head>2882<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2883<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2884<meta name="robots" content="index, nofollow"/>2885<title>$title</title>2886EOF2887# print out each stylesheet that exist2888if(defined$stylesheet) {2889#provides backwards capability for those people who define style sheet in a config file2890print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2891}else{2892foreachmy$stylesheet(@stylesheets) {2893next unless$stylesheet;2894print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2895}2896}2897if(defined$project) {2898my%href_params= get_feed_info();2899if(!exists$href_params{'-title'}) {2900$href_params{'-title'} ='log';2901}29022903foreachmy$formatqw(RSS Atom){2904my$type=lc($format);2905my%link_attr= (2906'-rel'=>'alternate',2907'-title'=>"$project-$href_params{'-title'} -$formatfeed",2908'-type'=>"application/$type+xml"2909);29102911$href_params{'action'} =$type;2912$link_attr{'-href'} = href(%href_params);2913print"<link ".2914"rel=\"$link_attr{'-rel'}\"".2915"title=\"$link_attr{'-title'}\"".2916"href=\"$link_attr{'-href'}\"".2917"type=\"$link_attr{'-type'}\"".2918"/>\n";29192920$href_params{'extra_options'} ='--no-merges';2921$link_attr{'-href'} = href(%href_params);2922$link_attr{'-title'} .=' (no merges)';2923print"<link ".2924"rel=\"$link_attr{'-rel'}\"".2925"title=\"$link_attr{'-title'}\"".2926"href=\"$link_attr{'-href'}\"".2927"type=\"$link_attr{'-type'}\"".2928"/>\n";2929}29302931}else{2932printf('<link rel="alternate" title="%sprojects list" '.2933'href="%s" type="text/plain; charset=utf-8" />'."\n",2934$site_name, href(project=>undef, action=>"project_index"));2935printf('<link rel="alternate" title="%sprojects feeds" '.2936'href="%s" type="text/x-opml" />'."\n",2937$site_name, href(project=>undef, action=>"opml"));2938}2939if(defined$favicon) {2940printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2941}29422943print"</head>\n".2944"<body>\n";29452946if(-f $site_header) {2947open(my$fd,$site_header);2948print<$fd>;2949close$fd;2950}29512952print"<div class=\"page_header\">\n".2953$cgi->a({-href => esc_url($logo_url),2954-title =>$logo_label},2955qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2956print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2957if(defined$project) {2958print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2959if(defined$action) {2960print" /$action";2961}2962print"\n";2963}2964print"</div>\n";29652966my$have_search= gitweb_check_feature('search');2967if(defined$project&&$have_search) {2968if(!defined$searchtext) {2969$searchtext="";2970}2971my$search_hash;2972if(defined$hash_base) {2973$search_hash=$hash_base;2974}elsif(defined$hash) {2975$search_hash=$hash;2976}else{2977$search_hash="HEAD";2978}2979my$action=$my_uri;2980my$use_pathinfo= gitweb_check_feature('pathinfo');2981if($use_pathinfo) {2982$action.="/".esc_url($project);2983}2984print$cgi->startform(-method=>"get", -action =>$action) .2985"<div class=\"search\">\n".2986(!$use_pathinfo&&2987$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .2988$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".2989$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".2990$cgi->popup_menu(-name =>'st', -default=>'commit',2991-values=> ['commit','grep','author','committer','pickaxe']) .2992$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .2993" search:\n",2994$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".2995"<span title=\"Extended regular expression\">".2996$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',2997-checked =>$search_use_regexp) .2998"</span>".2999"</div>".3000$cgi->end_form() ."\n";3001}3002}30033004sub git_footer_html {3005my$feed_class='rss_logo';30063007print"<div class=\"page_footer\">\n";3008if(defined$project) {3009my$descr= git_get_project_description($project);3010if(defined$descr) {3011print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3012}30133014my%href_params= get_feed_info();3015if(!%href_params) {3016$feed_class.=' generic';3017}3018$href_params{'-title'} ||='log';30193020foreachmy$formatqw(RSS Atom){3021$href_params{'action'} =lc($format);3022print$cgi->a({-href => href(%href_params),3023-title =>"$href_params{'-title'}$formatfeed",3024-class=>$feed_class},$format)."\n";3025}30263027}else{3028print$cgi->a({-href => href(project=>undef, action=>"opml"),3029-class=>$feed_class},"OPML") ." ";3030print$cgi->a({-href => href(project=>undef, action=>"project_index"),3031-class=>$feed_class},"TXT") ."\n";3032}3033print"</div>\n";# class="page_footer"30343035if(-f $site_footer) {3036open(my$fd,$site_footer);3037print<$fd>;3038close$fd;3039}30403041print"</body>\n".3042"</html>";3043}30443045# die_error(<http_status_code>, <error_message>)3046# Example: die_error(404, 'Hash not found')3047# By convention, use the following status codes (as defined in RFC 2616):3048# 400: Invalid or missing CGI parameters, or3049# requested object exists but has wrong type.3050# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3051# this server or project.3052# 404: Requested object/revision/project doesn't exist.3053# 500: The server isn't configured properly, or3054# an internal error occurred (e.g. failed assertions caused by bugs), or3055# an unknown error occurred (e.g. the git binary died unexpectedly).3056sub die_error {3057my$status=shift||500;3058my$error=shift||"Internal server error";30593060my%http_responses= (400=>'400 Bad Request',3061403=>'403 Forbidden',3062404=>'404 Not Found',3063500=>'500 Internal Server Error');3064 git_header_html($http_responses{$status});3065print<<EOF;3066<div class="page_body">3067<br /><br />3068$status-$error3069<br />3070</div>3071EOF3072 git_footer_html();3073exit;3074}30753076## ----------------------------------------------------------------------3077## functions printing or outputting HTML: navigation30783079sub git_print_page_nav {3080my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3081$extra=''if!defined$extra;# pager or formats30823083my@navs=qw(summary shortlog log commit commitdiff tree);3084if($suppress) {3085@navs=grep{$_ne$suppress}@navs;3086}30873088my%arg=map{$_=> {action=>$_} }@navs;3089if(defined$head) {3090for(qw(commit commitdiff)) {3091$arg{$_}{'hash'} =$head;3092}3093if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3094for(qw(shortlog log)) {3095$arg{$_}{'hash'} =$head;3096}3097}3098}30993100$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3101$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;31023103my@actions= gitweb_get_feature('actions');3104my%repl= (3105'%'=>'%',3106'n'=>$project,# project name3107'f'=>$git_dir,# project path within filesystem3108'h'=>$treehead||'',# current hash ('h' parameter)3109'b'=>$treebase||'',# hash base ('hb' parameter)3110);3111while(@actions) {3112my($label,$link,$pos) =splice(@actions,0,3);3113# insert3114@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3115# munch munch3116$link=~s/%([%nfhb])/$repl{$1}/g;3117$arg{$label}{'_href'} =$link;3118}31193120print"<div class=\"page_nav\">\n".3121(join" | ",3122map{$_eq$current?3123$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3124}@navs);3125print"<br/>\n$extra<br/>\n".3126"</div>\n";3127}31283129sub format_paging_nav {3130my($action,$hash,$head,$page,$has_next_link) =@_;3131my$paging_nav;313231333134if($hashne$head||$page) {3135$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3136}else{3137$paging_nav.="HEAD";3138}31393140if($page>0) {3141$paging_nav.=" ⋅ ".3142$cgi->a({-href => href(-replay=>1, page=>$page-1),3143-accesskey =>"p", -title =>"Alt-p"},"prev");3144}else{3145$paging_nav.=" ⋅ prev";3146}31473148if($has_next_link) {3149$paging_nav.=" ⋅ ".3150$cgi->a({-href => href(-replay=>1, page=>$page+1),3151-accesskey =>"n", -title =>"Alt-n"},"next");3152}else{3153$paging_nav.=" ⋅ next";3154}31553156return$paging_nav;3157}31583159## ......................................................................3160## functions printing or outputting HTML: div31613162sub git_print_header_div {3163my($action,$title,$hash,$hash_base) =@_;3164my%args= ();31653166$args{'action'} =$action;3167$args{'hash'} =$hashif$hash;3168$args{'hash_base'} =$hash_baseif$hash_base;31693170print"<div class=\"header\">\n".3171$cgi->a({-href => href(%args), -class=>"title"},3172$title?$title:$action) .3173"\n</div>\n";3174}31753176#sub git_print_authorship (\%) {3177sub git_print_authorship {3178my$co=shift;31793180my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3181print"<div class=\"author_date\">".3182 esc_html($co->{'author_name'}) .3183" [$ad{'rfc2822'}";3184if($ad{'hour_local'} <6) {3185printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3186$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3187}else{3188printf(" (%02d:%02d%s)",3189$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3190}3191print"]</div>\n";3192}31933194sub git_print_page_path {3195my$name=shift;3196my$type=shift;3197my$hb=shift;319831993200print"<div class=\"page_path\">";3201print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3202-title =>'tree root'}, to_utf8("[$project]"));3203print" / ";3204if(defined$name) {3205my@dirname=split'/',$name;3206my$basename=pop@dirname;3207my$fullname='';32083209foreachmy$dir(@dirname) {3210$fullname.= ($fullname?'/':'') .$dir;3211print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3212 hash_base=>$hb),3213-title =>$fullname}, esc_path($dir));3214print" / ";3215}3216if(defined$type&&$typeeq'blob') {3217print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3218 hash_base=>$hb),3219-title =>$name}, esc_path($basename));3220}elsif(defined$type&&$typeeq'tree') {3221print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3222 hash_base=>$hb),3223-title =>$name}, esc_path($basename));3224print" / ";3225}else{3226print esc_path($basename);3227}3228}3229print"<br/></div>\n";3230}32313232# sub git_print_log (\@;%) {3233sub git_print_log ($;%) {3234my$log=shift;3235my%opts=@_;32363237if($opts{'-remove_title'}) {3238# remove title, i.e. first line of log3239shift@$log;3240}3241# remove leading empty lines3242while(defined$log->[0] &&$log->[0]eq"") {3243shift@$log;3244}32453246# print log3247my$signoff=0;3248my$empty=0;3249foreachmy$line(@$log) {3250if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3251$signoff=1;3252$empty=0;3253if(!$opts{'-remove_signoff'}) {3254print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3255next;3256}else{3257# remove signoff lines3258next;3259}3260}else{3261$signoff=0;3262}32633264# print only one empty line3265# do not print empty line after signoff3266if($lineeq"") {3267next if($empty||$signoff);3268$empty=1;3269}else{3270$empty=0;3271}32723273print format_log_line_html($line) ."<br/>\n";3274}32753276if($opts{'-final_empty_line'}) {3277# end with single empty line3278print"<br/>\n"unless$empty;3279}3280}32813282# return link target (what link points to)3283sub git_get_link_target {3284my$hash=shift;3285my$link_target;32863287# read link3288open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3289orreturn;3290{3291local$/;3292$link_target= <$fd>;3293}3294close$fd3295orreturn;32963297return$link_target;3298}32993300# given link target, and the directory (basedir) the link is in,3301# return target of link relative to top directory (top tree);3302# return undef if it is not possible (including absolute links).3303sub normalize_link_target {3304my($link_target,$basedir,$hash_base) =@_;33053306# we can normalize symlink target only if $hash_base is provided3307return unless$hash_base;33083309# absolute symlinks (beginning with '/') cannot be normalized3310return if(substr($link_target,0,1)eq'/');33113312# normalize link target to path from top (root) tree (dir)3313my$path;3314if($basedir) {3315$path=$basedir.'/'.$link_target;3316}else{3317# we are in top (root) tree (dir)3318$path=$link_target;3319}33203321# remove //, /./, and /../3322my@path_parts;3323foreachmy$part(split('/',$path)) {3324# discard '.' and ''3325next if(!$part||$parteq'.');3326# handle '..'3327if($parteq'..') {3328if(@path_parts) {3329pop@path_parts;3330}else{3331# link leads outside repository (outside top dir)3332return;3333}3334}else{3335push@path_parts,$part;3336}3337}3338$path=join('/',@path_parts);33393340return$path;3341}33423343# print tree entry (row of git_tree), but without encompassing <tr> element3344sub git_print_tree_entry {3345my($t,$basedir,$hash_base,$have_blame) =@_;33463347my%base_key= ();3348$base_key{'hash_base'} =$hash_baseifdefined$hash_base;33493350# The format of a table row is: mode list link. Where mode is3351# the mode of the entry, list is the name of the entry, an href,3352# and link is the action links of the entry.33533354print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3355if($t->{'type'}eq"blob") {3356print"<td class=\"list\">".3357$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3358 file_name=>"$basedir$t->{'name'}",%base_key),3359-class=>"list"}, esc_path($t->{'name'}));3360if(S_ISLNK(oct$t->{'mode'})) {3361my$link_target= git_get_link_target($t->{'hash'});3362if($link_target) {3363my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3364if(defined$norm_target) {3365print" -> ".3366$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3367 file_name=>$norm_target),3368-title =>$norm_target}, esc_path($link_target));3369}else{3370print" -> ". esc_path($link_target);3371}3372}3373}3374print"</td>\n";3375print"<td class=\"link\">";3376print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3377 file_name=>"$basedir$t->{'name'}",%base_key)},3378"blob");3379if($have_blame) {3380print" | ".3381$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3382 file_name=>"$basedir$t->{'name'}",%base_key)},3383"blame");3384}3385if(defined$hash_base) {3386print" | ".3387$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3388 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3389"history");3390}3391print" | ".3392$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3393 file_name=>"$basedir$t->{'name'}")},3394"raw");3395print"</td>\n";33963397}elsif($t->{'type'}eq"tree") {3398print"<td class=\"list\">";3399print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3400 file_name=>"$basedir$t->{'name'}",%base_key)},3401 esc_path($t->{'name'}));3402print"</td>\n";3403print"<td class=\"link\">";3404print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3405 file_name=>"$basedir$t->{'name'}",%base_key)},3406"tree");3407if(defined$hash_base) {3408print" | ".3409$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3410 file_name=>"$basedir$t->{'name'}")},3411"history");3412}3413print"</td>\n";3414}else{3415# unknown object: we can only present history for it3416# (this includes 'commit' object, i.e. submodule support)3417print"<td class=\"list\">".3418 esc_path($t->{'name'}) .3419"</td>\n";3420print"<td class=\"link\">";3421if(defined$hash_base) {3422print$cgi->a({-href => href(action=>"history",3423 hash_base=>$hash_base,3424 file_name=>"$basedir$t->{'name'}")},3425"history");3426}3427print"</td>\n";3428}3429}34303431## ......................................................................3432## functions printing large fragments of HTML34333434# get pre-image filenames for merge (combined) diff3435sub fill_from_file_info {3436my($diff,@parents) =@_;34373438$diff->{'from_file'} = [ ];3439$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3440for(my$i=0;$i<$diff->{'nparents'};$i++) {3441if($diff->{'status'}[$i]eq'R'||3442$diff->{'status'}[$i]eq'C') {3443$diff->{'from_file'}[$i] =3444 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3445}3446}34473448return$diff;3449}34503451# is current raw difftree line of file deletion3452sub is_deleted {3453my$diffinfo=shift;34543455return$diffinfo->{'to_id'}eq('0' x 40);3456}34573458# does patch correspond to [previous] difftree raw line3459# $diffinfo - hashref of parsed raw diff format3460# $patchinfo - hashref of parsed patch diff format3461# (the same keys as in $diffinfo)3462sub is_patch_split {3463my($diffinfo,$patchinfo) =@_;34643465returndefined$diffinfo&&defined$patchinfo3466&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3467}346834693470sub git_difftree_body {3471my($difftree,$hash,@parents) =@_;3472my($parent) =$parents[0];3473my$have_blame= gitweb_check_feature('blame');3474print"<div class=\"list_head\">\n";3475if($#{$difftree} >10) {3476print(($#{$difftree} +1) ." files changed:\n");3477}3478print"</div>\n";34793480print"<table class=\"".3481(@parents>1?"combined ":"") .3482"diff_tree\">\n";34833484# header only for combined diff in 'commitdiff' view3485my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3486if($has_header) {3487# table header3488print"<thead><tr>\n".3489"<th></th><th></th>\n";# filename, patchN link3490for(my$i=0;$i<@parents;$i++) {3491my$par=$parents[$i];3492print"<th>".3493$cgi->a({-href => href(action=>"commitdiff",3494 hash=>$hash, hash_parent=>$par),3495-title =>'commitdiff to parent number '.3496($i+1) .': '.substr($par,0,7)},3497$i+1) .3498" </th>\n";3499}3500print"</tr></thead>\n<tbody>\n";3501}35023503my$alternate=1;3504my$patchno=0;3505foreachmy$line(@{$difftree}) {3506my$diff= parsed_difftree_line($line);35073508if($alternate) {3509print"<tr class=\"dark\">\n";3510}else{3511print"<tr class=\"light\">\n";3512}3513$alternate^=1;35143515if(exists$diff->{'nparents'}) {# combined diff35163517 fill_from_file_info($diff,@parents)3518unlessexists$diff->{'from_file'};35193520if(!is_deleted($diff)) {3521# file exists in the result (child) commit3522print"<td>".3523$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3524 file_name=>$diff->{'to_file'},3525 hash_base=>$hash),3526-class=>"list"}, esc_path($diff->{'to_file'})) .3527"</td>\n";3528}else{3529print"<td>".3530 esc_path($diff->{'to_file'}) .3531"</td>\n";3532}35333534if($actioneq'commitdiff') {3535# link to patch3536$patchno++;3537print"<td class=\"link\">".3538$cgi->a({-href =>"#patch$patchno"},"patch") .3539" | ".3540"</td>\n";3541}35423543my$has_history=0;3544my$not_deleted=0;3545for(my$i=0;$i<$diff->{'nparents'};$i++) {3546my$hash_parent=$parents[$i];3547my$from_hash=$diff->{'from_id'}[$i];3548my$from_path=$diff->{'from_file'}[$i];3549my$status=$diff->{'status'}[$i];35503551$has_history||= ($statusne'A');3552$not_deleted||= ($statusne'D');35533554if($statuseq'A') {3555print"<td class=\"link\"align=\"right\"> | </td>\n";3556}elsif($statuseq'D') {3557print"<td class=\"link\">".3558$cgi->a({-href => href(action=>"blob",3559 hash_base=>$hash,3560 hash=>$from_hash,3561 file_name=>$from_path)},3562"blob". ($i+1)) .3563" | </td>\n";3564}else{3565if($diff->{'to_id'}eq$from_hash) {3566print"<td class=\"link nochange\">";3567}else{3568print"<td class=\"link\">";3569}3570print$cgi->a({-href => href(action=>"blobdiff",3571 hash=>$diff->{'to_id'},3572 hash_parent=>$from_hash,3573 hash_base=>$hash,3574 hash_parent_base=>$hash_parent,3575 file_name=>$diff->{'to_file'},3576 file_parent=>$from_path)},3577"diff". ($i+1)) .3578" | </td>\n";3579}3580}35813582print"<td class=\"link\">";3583if($not_deleted) {3584print$cgi->a({-href => href(action=>"blob",3585 hash=>$diff->{'to_id'},3586 file_name=>$diff->{'to_file'},3587 hash_base=>$hash)},3588"blob");3589print" | "if($has_history);3590}3591if($has_history) {3592print$cgi->a({-href => href(action=>"history",3593 file_name=>$diff->{'to_file'},3594 hash_base=>$hash)},3595"history");3596}3597print"</td>\n";35983599print"</tr>\n";3600next;# instead of 'else' clause, to avoid extra indent3601}3602# else ordinary diff36033604my($to_mode_oct,$to_mode_str,$to_file_type);3605my($from_mode_oct,$from_mode_str,$from_file_type);3606if($diff->{'to_mode'}ne('0' x 6)) {3607$to_mode_oct=oct$diff->{'to_mode'};3608if(S_ISREG($to_mode_oct)) {# only for regular file3609$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3610}3611$to_file_type= file_type($diff->{'to_mode'});3612}3613if($diff->{'from_mode'}ne('0' x 6)) {3614$from_mode_oct=oct$diff->{'from_mode'};3615if(S_ISREG($to_mode_oct)) {# only for regular file3616$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3617}3618$from_file_type= file_type($diff->{'from_mode'});3619}36203621if($diff->{'status'}eq"A") {# created3622my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3623$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3624$mode_chng.="]</span>";3625print"<td>";3626print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3627 hash_base=>$hash, file_name=>$diff->{'file'}),3628-class=>"list"}, esc_path($diff->{'file'}));3629print"</td>\n";3630print"<td>$mode_chng</td>\n";3631print"<td class=\"link\">";3632if($actioneq'commitdiff') {3633# link to patch3634$patchno++;3635print$cgi->a({-href =>"#patch$patchno"},"patch");3636print" | ";3637}3638print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3639 hash_base=>$hash, file_name=>$diff->{'file'})},3640"blob");3641print"</td>\n";36423643}elsif($diff->{'status'}eq"D") {# deleted3644my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3645print"<td>";3646print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3647 hash_base=>$parent, file_name=>$diff->{'file'}),3648-class=>"list"}, esc_path($diff->{'file'}));3649print"</td>\n";3650print"<td>$mode_chng</td>\n";3651print"<td class=\"link\">";3652if($actioneq'commitdiff') {3653# link to patch3654$patchno++;3655print$cgi->a({-href =>"#patch$patchno"},"patch");3656print" | ";3657}3658print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3659 hash_base=>$parent, file_name=>$diff->{'file'})},3660"blob") ." | ";3661if($have_blame) {3662print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3663 file_name=>$diff->{'file'})},3664"blame") ." | ";3665}3666print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3667 file_name=>$diff->{'file'})},3668"history");3669print"</td>\n";36703671}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3672my$mode_chnge="";3673if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3674$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3675if($from_file_typene$to_file_type) {3676$mode_chnge.=" from$from_file_typeto$to_file_type";3677}3678if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3679if($from_mode_str&&$to_mode_str) {3680$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3681}elsif($to_mode_str) {3682$mode_chnge.=" mode:$to_mode_str";3683}3684}3685$mode_chnge.="]</span>\n";3686}3687print"<td>";3688print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3689 hash_base=>$hash, file_name=>$diff->{'file'}),3690-class=>"list"}, esc_path($diff->{'file'}));3691print"</td>\n";3692print"<td>$mode_chnge</td>\n";3693print"<td class=\"link\">";3694if($actioneq'commitdiff') {3695# link to patch3696$patchno++;3697print$cgi->a({-href =>"#patch$patchno"},"patch") .3698" | ";3699}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3700# "commit" view and modified file (not onlu mode changed)3701print$cgi->a({-href => href(action=>"blobdiff",3702 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3703 hash_base=>$hash, hash_parent_base=>$parent,3704 file_name=>$diff->{'file'})},3705"diff") .3706" | ";3707}3708print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3709 hash_base=>$hash, file_name=>$diff->{'file'})},3710"blob") ." | ";3711if($have_blame) {3712print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3713 file_name=>$diff->{'file'})},3714"blame") ." | ";3715}3716print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3717 file_name=>$diff->{'file'})},3718"history");3719print"</td>\n";37203721}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3722my%status_name= ('R'=>'moved','C'=>'copied');3723my$nstatus=$status_name{$diff->{'status'}};3724my$mode_chng="";3725if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3726# mode also for directories, so we cannot use $to_mode_str3727$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3728}3729print"<td>".3730$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3731 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3732-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3733"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3734$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3735 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3736-class=>"list"}, esc_path($diff->{'from_file'})) .3737" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3738"<td class=\"link\">";3739if($actioneq'commitdiff') {3740# link to patch3741$patchno++;3742print$cgi->a({-href =>"#patch$patchno"},"patch") .3743" | ";3744}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3745# "commit" view and modified file (not only pure rename or copy)3746print$cgi->a({-href => href(action=>"blobdiff",3747 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3748 hash_base=>$hash, hash_parent_base=>$parent,3749 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3750"diff") .3751" | ";3752}3753print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3754 hash_base=>$parent, file_name=>$diff->{'to_file'})},3755"blob") ." | ";3756if($have_blame) {3757print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3758 file_name=>$diff->{'to_file'})},3759"blame") ." | ";3760}3761print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3762 file_name=>$diff->{'to_file'})},3763"history");3764print"</td>\n";37653766}# we should not encounter Unmerged (U) or Unknown (X) status3767print"</tr>\n";3768}3769print"</tbody>"if$has_header;3770print"</table>\n";3771}37723773sub git_patchset_body {3774my($fd,$difftree,$hash,@hash_parents) =@_;3775my($hash_parent) =$hash_parents[0];37763777my$is_combined= (@hash_parents>1);3778my$patch_idx=0;3779my$patch_number=0;3780my$patch_line;3781my$diffinfo;3782my$to_name;3783my(%from,%to);37843785print"<div class=\"patchset\">\n";37863787# skip to first patch3788while($patch_line= <$fd>) {3789chomp$patch_line;37903791last if($patch_line=~m/^diff /);3792}37933794 PATCH:3795while($patch_line) {37963797# parse "git diff" header line3798if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3799# $1 is from_name, which we do not use3800$to_name= unquote($2);3801$to_name=~s!^b/!!;3802}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3803# $1 is 'cc' or 'combined', which we do not use3804$to_name= unquote($2);3805}else{3806$to_name=undef;3807}38083809# check if current patch belong to current raw line3810# and parse raw git-diff line if needed3811if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3812# this is continuation of a split patch3813print"<div class=\"patch cont\">\n";3814}else{3815# advance raw git-diff output if needed3816$patch_idx++ifdefined$diffinfo;38173818# read and prepare patch information3819$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38203821# compact combined diff output can have some patches skipped3822# find which patch (using pathname of result) we are at now;3823if($is_combined) {3824while($to_namene$diffinfo->{'to_file'}) {3825print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3826 format_diff_cc_simplified($diffinfo,@hash_parents) .3827"</div>\n";# class="patch"38283829$patch_idx++;3830$patch_number++;38313832last if$patch_idx>$#$difftree;3833$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3834}3835}38363837# modifies %from, %to hashes3838 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);38393840# this is first patch for raw difftree line with $patch_idx index3841# we index @$difftree array from 0, but number patches from 13842print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3843}38443845# git diff header3846#assert($patch_line =~ m/^diff /) if DEBUG;3847#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3848$patch_number++;3849# print "git diff" header3850print format_git_diff_header_line($patch_line,$diffinfo,3851 \%from, \%to);38523853# print extended diff header3854print"<div class=\"diff extended_header\">\n";3855 EXTENDED_HEADER:3856while($patch_line= <$fd>) {3857chomp$patch_line;38583859last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);38603861print format_extended_diff_header_line($patch_line,$diffinfo,3862 \%from, \%to);3863}3864print"</div>\n";# class="diff extended_header"38653866# from-file/to-file diff header3867if(!$patch_line) {3868print"</div>\n";# class="patch"3869last PATCH;3870}3871next PATCH if($patch_line=~m/^diff /);3872#assert($patch_line =~ m/^---/) if DEBUG;38733874my$last_patch_line=$patch_line;3875$patch_line= <$fd>;3876chomp$patch_line;3877#assert($patch_line =~ m/^\+\+\+/) if DEBUG;38783879print format_diff_from_to_header($last_patch_line,$patch_line,3880$diffinfo, \%from, \%to,3881@hash_parents);38823883# the patch itself3884 LINE:3885while($patch_line= <$fd>) {3886chomp$patch_line;38873888next PATCH if($patch_line=~m/^diff /);38893890print format_diff_line($patch_line, \%from, \%to);3891}38923893}continue{3894print"</div>\n";# class="patch"3895}38963897# for compact combined (--cc) format, with chunk and patch simpliciaction3898# patchset might be empty, but there might be unprocessed raw lines3899for(++$patch_idxif$patch_number>0;3900$patch_idx<@$difftree;3901++$patch_idx) {3902# read and prepare patch information3903$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);39043905# generate anchor for "patch" links in difftree / whatchanged part3906print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3907 format_diff_cc_simplified($diffinfo,@hash_parents) .3908"</div>\n";# class="patch"39093910$patch_number++;3911}39123913if($patch_number==0) {3914if(@hash_parents>1) {3915print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3916}else{3917print"<div class=\"diff nodifferences\">No differences found</div>\n";3918}3919}39203921print"</div>\n";# class="patchset"3922}39233924# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39253926# fills project list info (age, description, owner, forks) for each3927# project in the list, removing invalid projects from returned list3928# NOTE: modifies $projlist, but does not remove entries from it3929sub fill_project_list_info {3930my($projlist,$check_forks) =@_;3931my@projects;39323933my$show_ctags= gitweb_check_feature('ctags');3934 PROJECT:3935foreachmy$pr(@$projlist) {3936my(@activity) = git_get_last_activity($pr->{'path'});3937unless(@activity) {3938next PROJECT;3939}3940($pr->{'age'},$pr->{'age_string'}) =@activity;3941if(!defined$pr->{'descr'}) {3942my$descr= git_get_project_description($pr->{'path'}) ||"";3943$descr= to_utf8($descr);3944$pr->{'descr_long'} =$descr;3945$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3946}3947if(!defined$pr->{'owner'}) {3948$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3949}3950if($check_forks) {3951my$pname=$pr->{'path'};3952if(($pname=~s/\.git$//) &&3953($pname!~/\/$/) &&3954(-d "$projectroot/$pname")) {3955$pr->{'forks'} ="-d$projectroot/$pname";3956}else{3957$pr->{'forks'} =0;3958}3959}3960$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3961push@projects,$pr;3962}39633964return@projects;3965}39663967# print 'sort by' <th> element, generating 'sort by $name' replay link3968# if that order is not selected3969sub print_sort_th {3970my($name,$order,$header) =@_;3971$header||=ucfirst($name);39723973if($ordereq$name) {3974print"<th>$header</th>\n";3975}else{3976print"<th>".3977$cgi->a({-href => href(-replay=>1, order=>$name),3978-class=>"header"},$header) .3979"</th>\n";3980}3981}39823983sub git_project_list_body {3984# actually uses global variable $project3985my($projlist,$order,$from,$to,$extra,$no_header) =@_;39863987my$check_forks= gitweb_check_feature('forks');3988my@projects= fill_project_list_info($projlist,$check_forks);39893990$order||=$default_projects_order;3991$from=0unlessdefined$from;3992$to=$#projectsif(!defined$to||$#projects<$to);39933994my%order_info= (3995 project => { key =>'path', type =>'str'},3996 descr => { key =>'descr_long', type =>'str'},3997 owner => { key =>'owner', type =>'str'},3998 age => { key =>'age', type =>'num'}3999);4000my$oi=$order_info{$order};4001if($oi->{'type'}eq'str') {4002@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4003}else{4004@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4005}40064007my$show_ctags= gitweb_check_feature('ctags');4008if($show_ctags) {4009my%ctags;4010foreachmy$p(@projects) {4011foreachmy$ct(keys%{$p->{'ctags'}}) {4012$ctags{$ct} +=$p->{'ctags'}->{$ct};4013}4014}4015my$cloud= git_populate_project_tagcloud(\%ctags);4016print git_show_project_tagcloud($cloud,64);4017}40184019print"<table class=\"project_list\">\n";4020unless($no_header) {4021print"<tr>\n";4022if($check_forks) {4023print"<th></th>\n";4024}4025 print_sort_th('project',$order,'Project');4026 print_sort_th('descr',$order,'Description');4027 print_sort_th('owner',$order,'Owner');4028 print_sort_th('age',$order,'Last Change');4029print"<th></th>\n".# for links4030"</tr>\n";4031}4032my$alternate=1;4033my$tagfilter=$cgi->param('by_tag');4034for(my$i=$from;$i<=$to;$i++) {4035my$pr=$projects[$i];40364037next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4038next if$searchtextand not$pr->{'path'} =~/$searchtext/4039and not$pr->{'descr_long'} =~/$searchtext/;4040# Weed out forks or non-matching entries of search4041if($check_forks) {4042my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4043$forkbase="^$forkbase"if$forkbase;4044next ifnot$searchtextand not$tagfilterand$show_ctags4045and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4046}40474048if($alternate) {4049print"<tr class=\"dark\">\n";4050}else{4051print"<tr class=\"light\">\n";4052}4053$alternate^=1;4054if($check_forks) {4055print"<td>";4056if($pr->{'forks'}) {4057print"<!--$pr->{'forks'} -->\n";4058print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4059}4060print"</td>\n";4061}4062print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4063-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4064"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4065-class=>"list", -title =>$pr->{'descr_long'}},4066 esc_html($pr->{'descr'})) ."</td>\n".4067"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4068print"<td class=\"". age_class($pr->{'age'}) ."\">".4069(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4070"<td class=\"link\">".4071$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4072$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4073$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4074$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4075($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4076"</td>\n".4077"</tr>\n";4078}4079if(defined$extra) {4080print"<tr>\n";4081if($check_forks) {4082print"<td></td>\n";4083}4084print"<td colspan=\"5\">$extra</td>\n".4085"</tr>\n";4086}4087print"</table>\n";4088}40894090sub git_shortlog_body {4091# uses global variable $project4092my($commitlist,$from,$to,$refs,$extra) =@_;40934094$from=0unlessdefined$from;4095$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);40964097print"<table class=\"shortlog\">\n";4098my$alternate=1;4099for(my$i=$from;$i<=$to;$i++) {4100my%co= %{$commitlist->[$i]};4101my$commit=$co{'id'};4102my$ref= format_ref_marker($refs,$commit);4103if($alternate) {4104print"<tr class=\"dark\">\n";4105}else{4106print"<tr class=\"light\">\n";4107}4108$alternate^=1;4109my$author= chop_and_escape_str($co{'author_name'},10);4110# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4111print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4112"<td><i>".$author."</i></td>\n".4113"<td>";4114print format_subject_html($co{'title'},$co{'title_short'},4115 href(action=>"commit", hash=>$commit),$ref);4116print"</td>\n".4117"<td class=\"link\">".4118$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4119$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4120$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4121my$snapshot_links= format_snapshot_links($commit);4122if(defined$snapshot_links) {4123print" | ".$snapshot_links;4124}4125print"</td>\n".4126"</tr>\n";4127}4128if(defined$extra) {4129print"<tr>\n".4130"<td colspan=\"4\">$extra</td>\n".4131"</tr>\n";4132}4133print"</table>\n";4134}41354136sub git_history_body {4137# Warning: assumes constant type (blob or tree) during history4138my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;41394140$from=0unlessdefined$from;4141$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});41424143print"<table class=\"history\">\n";4144my$alternate=1;4145for(my$i=$from;$i<=$to;$i++) {4146my%co= %{$commitlist->[$i]};4147if(!%co) {4148next;4149}4150my$commit=$co{'id'};41514152my$ref= format_ref_marker($refs,$commit);41534154if($alternate) {4155print"<tr class=\"dark\">\n";4156}else{4157print"<tr class=\"light\">\n";4158}4159$alternate^=1;4160# shortlog uses chop_str($co{'author_name'}, 10)4161my$author= chop_and_escape_str($co{'author_name'},15,3);4162print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4163"<td><i>".$author."</i></td>\n".4164"<td>";4165# originally git_history used chop_str($co{'title'}, 50)4166print format_subject_html($co{'title'},$co{'title_short'},4167 href(action=>"commit", hash=>$commit),$ref);4168print"</td>\n".4169"<td class=\"link\">".4170$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4171$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");41724173if($ftypeeq'blob') {4174my$blob_current= git_get_hash_by_path($hash_base,$file_name);4175my$blob_parent= git_get_hash_by_path($commit,$file_name);4176if(defined$blob_current&&defined$blob_parent&&4177$blob_currentne$blob_parent) {4178print" | ".4179$cgi->a({-href => href(action=>"blobdiff",4180 hash=>$blob_current, hash_parent=>$blob_parent,4181 hash_base=>$hash_base, hash_parent_base=>$commit,4182 file_name=>$file_name)},4183"diff to current");4184}4185}4186print"</td>\n".4187"</tr>\n";4188}4189if(defined$extra) {4190print"<tr>\n".4191"<td colspan=\"4\">$extra</td>\n".4192"</tr>\n";4193}4194print"</table>\n";4195}41964197sub git_tags_body {4198# uses global variable $project4199my($taglist,$from,$to,$extra) =@_;4200$from=0unlessdefined$from;4201$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);42024203print"<table class=\"tags\">\n";4204my$alternate=1;4205for(my$i=$from;$i<=$to;$i++) {4206my$entry=$taglist->[$i];4207my%tag=%$entry;4208my$comment=$tag{'subject'};4209my$comment_short;4210if(defined$comment) {4211$comment_short= chop_str($comment,30,5);4212}4213if($alternate) {4214print"<tr class=\"dark\">\n";4215}else{4216print"<tr class=\"light\">\n";4217}4218$alternate^=1;4219if(defined$tag{'age'}) {4220print"<td><i>$tag{'age'}</i></td>\n";4221}else{4222print"<td></td>\n";4223}4224print"<td>".4225$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4226-class=>"list name"}, esc_html($tag{'name'})) .4227"</td>\n".4228"<td>";4229if(defined$comment) {4230print format_subject_html($comment,$comment_short,4231 href(action=>"tag", hash=>$tag{'id'}));4232}4233print"</td>\n".4234"<td class=\"selflink\">";4235if($tag{'type'}eq"tag") {4236print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4237}else{4238print" ";4239}4240print"</td>\n".4241"<td class=\"link\">"." | ".4242$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4243if($tag{'reftype'}eq"commit") {4244print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4245" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4246}elsif($tag{'reftype'}eq"blob") {4247print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4248}4249print"</td>\n".4250"</tr>";4251}4252if(defined$extra) {4253print"<tr>\n".4254"<td colspan=\"5\">$extra</td>\n".4255"</tr>\n";4256}4257print"</table>\n";4258}42594260sub git_heads_body {4261# uses global variable $project4262my($headlist,$head,$from,$to,$extra) =@_;4263$from=0unlessdefined$from;4264$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);42654266print"<table class=\"heads\">\n";4267my$alternate=1;4268for(my$i=$from;$i<=$to;$i++) {4269my$entry=$headlist->[$i];4270my%ref=%$entry;4271my$curr=$ref{'id'}eq$head;4272if($alternate) {4273print"<tr class=\"dark\">\n";4274}else{4275print"<tr class=\"light\">\n";4276}4277$alternate^=1;4278print"<td><i>$ref{'age'}</i></td>\n".4279($curr?"<td class=\"current_head\">":"<td>") .4280$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4281-class=>"list name"},esc_html($ref{'name'})) .4282"</td>\n".4283"<td class=\"link\">".4284$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4285$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4286$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4287"</td>\n".4288"</tr>";4289}4290if(defined$extra) {4291print"<tr>\n".4292"<td colspan=\"3\">$extra</td>\n".4293"</tr>\n";4294}4295print"</table>\n";4296}42974298sub git_search_grep_body {4299my($commitlist,$from,$to,$extra) =@_;4300$from=0unlessdefined$from;4301$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43024303print"<table class=\"commit_search\">\n";4304my$alternate=1;4305for(my$i=$from;$i<=$to;$i++) {4306my%co= %{$commitlist->[$i]};4307if(!%co) {4308next;4309}4310my$commit=$co{'id'};4311if($alternate) {4312print"<tr class=\"dark\">\n";4313}else{4314print"<tr class=\"light\">\n";4315}4316$alternate^=1;4317my$author= chop_and_escape_str($co{'author_name'},15,5);4318print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4319"<td><i>".$author."</i></td>\n".4320"<td>".4321$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4322-class=>"list subject"},4323 chop_and_escape_str($co{'title'},50) ."<br/>");4324my$comment=$co{'comment'};4325foreachmy$line(@$comment) {4326if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4327my($lead,$match,$trail) = ($1,$2,$3);4328$match= chop_str($match,70,5,'center');4329my$contextlen=int((80-length($match))/2);4330$contextlen=30if($contextlen>30);4331$lead= chop_str($lead,$contextlen,10,'left');4332$trail= chop_str($trail,$contextlen,10,'right');43334334$lead= esc_html($lead);4335$match= esc_html($match);4336$trail= esc_html($trail);43374338print"$lead<span class=\"match\">$match</span>$trail<br />";4339}4340}4341print"</td>\n".4342"<td class=\"link\">".4343$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4344" | ".4345$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4346" | ".4347$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4348print"</td>\n".4349"</tr>\n";4350}4351if(defined$extra) {4352print"<tr>\n".4353"<td colspan=\"3\">$extra</td>\n".4354"</tr>\n";4355}4356print"</table>\n";4357}43584359## ======================================================================4360## ======================================================================4361## actions43624363sub git_project_list {4364my$order=$input_params{'order'};4365if(defined$order&&$order!~m/none|project|descr|owner|age/) {4366 die_error(400,"Unknown order parameter");4367}43684369my@list= git_get_projects_list();4370if(!@list) {4371 die_error(404,"No projects found");4372}43734374 git_header_html();4375if(-f $home_text) {4376print"<div class=\"index_include\">\n";4377open(my$fd,$home_text);4378print<$fd>;4379close$fd;4380print"</div>\n";4381}4382print$cgi->startform(-method=>"get") .4383"<p class=\"projsearch\">Search:\n".4384$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4385"</p>".4386$cgi->end_form() ."\n";4387 git_project_list_body(\@list,$order);4388 git_footer_html();4389}43904391sub git_forks {4392my$order=$input_params{'order'};4393if(defined$order&&$order!~m/none|project|descr|owner|age/) {4394 die_error(400,"Unknown order parameter");4395}43964397my@list= git_get_projects_list($project);4398if(!@list) {4399 die_error(404,"No forks found");4400}44014402 git_header_html();4403 git_print_page_nav('','');4404 git_print_header_div('summary',"$projectforks");4405 git_project_list_body(\@list,$order);4406 git_footer_html();4407}44084409sub git_project_index {4410my@projects= git_get_projects_list($project);44114412print$cgi->header(4413-type =>'text/plain',4414-charset =>'utf-8',4415-content_disposition =>'inline; filename="index.aux"');44164417foreachmy$pr(@projects) {4418if(!exists$pr->{'owner'}) {4419$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4420}44214422my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4423# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4424$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4425$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4426$path=~s/ /\+/g;4427$owner=~s/ /\+/g;44284429print"$path$owner\n";4430}4431}44324433sub git_summary {4434my$descr= git_get_project_description($project) ||"none";4435my%co= parse_commit("HEAD");4436my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4437my$head=$co{'id'};44384439my$owner= git_get_project_owner($project);44404441my$refs= git_get_references();4442# These get_*_list functions return one more to allow us to see if4443# there are more ...4444my@taglist= git_get_tags_list(16);4445my@headlist= git_get_heads_list(16);4446my@forklist;4447my$check_forks= gitweb_check_feature('forks');44484449if($check_forks) {4450@forklist= git_get_projects_list($project);4451}44524453 git_header_html();4454 git_print_page_nav('summary','',$head);44554456print"<div class=\"title\"> </div>\n";4457print"<table class=\"projects_list\">\n".4458"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4459"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4460if(defined$cd{'rfc2822'}) {4461print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4462}44634464# use per project git URL list in $projectroot/$project/cloneurl4465# or make project git URL from git base URL and project name4466my$url_tag="URL";4467my@url_list= git_get_project_url_list($project);4468@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4469foreachmy$git_url(@url_list) {4470next unless$git_url;4471print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4472$url_tag="";4473}44744475# Tag cloud4476my$show_ctags= gitweb_check_feature('ctags');4477if($show_ctags) {4478my$ctags= git_get_project_ctags($project);4479my$cloud= git_populate_project_tagcloud($ctags);4480print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4481print"</td>\n<td>"unless%$ctags;4482print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4483print"</td>\n<td>"if%$ctags;4484print git_show_project_tagcloud($cloud,48);4485print"</td></tr>";4486}44874488print"</table>\n";44894490if(-s "$projectroot/$project/README.html") {4491if(open my$fd,"$projectroot/$project/README.html") {4492print"<div class=\"title\">readme</div>\n".4493"<div class=\"readme\">\n";4494print$_while(<$fd>);4495print"\n</div>\n";# class="readme"4496close$fd;4497}4498}44994500# we need to request one more than 16 (0..15) to check if4501# those 16 are all4502my@commitlist=$head? parse_commits($head,17) : ();4503if(@commitlist) {4504 git_print_header_div('shortlog');4505 git_shortlog_body(\@commitlist,0,15,$refs,4506$#commitlist<=15?undef:4507$cgi->a({-href => href(action=>"shortlog")},"..."));4508}45094510if(@taglist) {4511 git_print_header_div('tags');4512 git_tags_body(\@taglist,0,15,4513$#taglist<=15?undef:4514$cgi->a({-href => href(action=>"tags")},"..."));4515}45164517if(@headlist) {4518 git_print_header_div('heads');4519 git_heads_body(\@headlist,$head,0,15,4520$#headlist<=15?undef:4521$cgi->a({-href => href(action=>"heads")},"..."));4522}45234524if(@forklist) {4525 git_print_header_div('forks');4526 git_project_list_body(\@forklist,'age',0,15,4527$#forklist<=15?undef:4528$cgi->a({-href => href(action=>"forks")},"..."),4529'no_header');4530}45314532 git_footer_html();4533}45344535sub git_tag {4536my$head= git_get_head_hash($project);4537 git_header_html();4538 git_print_page_nav('','',$head,undef,$head);4539my%tag= parse_tag($hash);45404541if(!%tag) {4542 die_error(404,"Unknown tag object");4543}45444545 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4546print"<div class=\"title_text\">\n".4547"<table class=\"object_header\">\n".4548"<tr>\n".4549"<td>object</td>\n".4550"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4551$tag{'object'}) ."</td>\n".4552"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4553$tag{'type'}) ."</td>\n".4554"</tr>\n";4555if(defined($tag{'author'})) {4556my%ad= parse_date($tag{'epoch'},$tag{'tz'});4557print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4558print"<tr><td></td><td>".$ad{'rfc2822'} .4559sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4560"</td></tr>\n";4561}4562print"</table>\n\n".4563"</div>\n";4564print"<div class=\"page_body\">";4565my$comment=$tag{'comment'};4566foreachmy$line(@$comment) {4567chomp$line;4568print esc_html($line, -nbsp=>1) ."<br/>\n";4569}4570print"</div>\n";4571 git_footer_html();4572}45734574sub git_blame {4575my$fd;4576my$ftype;45774578 gitweb_check_feature('blame')4579or die_error(403,"Blame view not allowed");45804581 die_error(400,"No file name given")unless$file_name;4582$hash_base||= git_get_head_hash($project);4583 die_error(404,"Couldn't find base commit")unless($hash_base);4584my%co= parse_commit($hash_base)4585or die_error(404,"Commit not found");4586if(!defined$hash) {4587$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4588or die_error(404,"Error looking up file");4589}4590$ftype= git_get_type($hash);4591if($ftype!~"blob") {4592 die_error(400,"Object is not a blob");4593}4594open($fd,"-|", git_cmd(),"blame",'-p','--',4595$file_name,$hash_base)4596or die_error(500,"Open git-blame failed");4597 git_header_html();4598my$formats_nav=4599$cgi->a({-href => href(action=>"blob", -replay=>1)},4600"blob") .4601" | ".4602$cgi->a({-href => href(action=>"history", -replay=>1)},4603"history") .4604" | ".4605$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4606"HEAD");4607 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4608 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4609 git_print_page_path($file_name,$ftype,$hash_base);4610my@rev_color= (qw(light2 dark2));4611my$num_colors=scalar(@rev_color);4612my$current_color=0;4613my$last_rev;4614print<<HTML;4615<div class="page_body">4616<table class="blame">4617<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4618HTML4619my%metainfo= ();4620while(1) {4621$_= <$fd>;4622last unlessdefined$_;4623my($full_rev,$orig_lineno,$lineno,$group_size) =4624/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;4625if(!exists$metainfo{$full_rev}) {4626$metainfo{$full_rev} = {};4627}4628my$meta=$metainfo{$full_rev};4629while(<$fd>) {4630last if(s/^\t//);4631if(/^(\S+) (.*)$/) {4632$meta->{$1} =$2;4633}4634}4635my$data=$_;4636chomp$data;4637my$rev=substr($full_rev,0,8);4638my$author=$meta->{'author'};4639my%date= parse_date($meta->{'author-time'},4640$meta->{'author-tz'});4641my$date=$date{'iso-tz'};4642if($group_size) {4643$current_color= ++$current_color%$num_colors;4644}4645print"<tr class=\"$rev_color[$current_color]\">\n";4646if($group_size) {4647print"<td class=\"sha1\"";4648print" title=\"". esc_html($author) .",$date\"";4649print" rowspan=\"$group_size\""if($group_size>1);4650print">";4651print$cgi->a({-href => href(action=>"commit",4652 hash=>$full_rev,4653 file_name=>$file_name)},4654 esc_html($rev));4655print"</td>\n";4656}4657open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4658or die_error(500,"Open git-rev-parse failed");4659my$parent_commit= <$dd>;4660close$dd;4661chomp($parent_commit);4662my$blamed= href(action =>'blame',4663 file_name =>$meta->{'filename'},4664 hash_base =>$parent_commit);4665print"<td class=\"linenr\">";4666print$cgi->a({ -href =>"$blamed#l$orig_lineno",4667-id =>"l$lineno",4668-class=>"linenr"},4669 esc_html($lineno));4670print"</td>";4671print"<td class=\"pre\">". esc_html($data) ."</td>\n";4672print"</tr>\n";4673}4674print"</table>\n";4675print"</div>";4676close$fd4677or print"Reading blob failed\n";4678 git_footer_html();4679}46804681sub git_tags {4682my$head= git_get_head_hash($project);4683 git_header_html();4684 git_print_page_nav('','',$head,undef,$head);4685 git_print_header_div('summary',$project);46864687my@tagslist= git_get_tags_list();4688if(@tagslist) {4689 git_tags_body(\@tagslist);4690}4691 git_footer_html();4692}46934694sub git_heads {4695my$head= git_get_head_hash($project);4696 git_header_html();4697 git_print_page_nav('','',$head,undef,$head);4698 git_print_header_div('summary',$project);46994700my@headslist= git_get_heads_list();4701if(@headslist) {4702 git_heads_body(\@headslist,$head);4703}4704 git_footer_html();4705}47064707sub git_blob_plain {4708my$type=shift;4709my$expires;47104711if(!defined$hash) {4712if(defined$file_name) {4713my$base=$hash_base|| git_get_head_hash($project);4714$hash= git_get_hash_by_path($base,$file_name,"blob")4715or die_error(404,"Cannot find file");4716}else{4717 die_error(400,"No file name defined");4718}4719}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4720# blobs defined by non-textual hash id's can be cached4721$expires="+1d";4722}47234724open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4725or die_error(500,"Open git-cat-file blob '$hash' failed");47264727# content-type (can include charset)4728$type= blob_contenttype($fd,$file_name,$type);47294730# "save as" filename, even when no $file_name is given4731my$save_as="$hash";4732if(defined$file_name) {4733$save_as=$file_name;4734}elsif($type=~m/^text\//) {4735$save_as.='.txt';4736}47374738print$cgi->header(4739-type =>$type,4740-expires =>$expires,4741-content_disposition =>'inline; filename="'.$save_as.'"');4742undef$/;4743binmode STDOUT,':raw';4744print<$fd>;4745binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4746$/="\n";4747close$fd;4748}47494750sub git_blob {4751my$expires;47524753if(!defined$hash) {4754if(defined$file_name) {4755my$base=$hash_base|| git_get_head_hash($project);4756$hash= git_get_hash_by_path($base,$file_name,"blob")4757or die_error(404,"Cannot find file");4758}else{4759 die_error(400,"No file name defined");4760}4761}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4762# blobs defined by non-textual hash id's can be cached4763$expires="+1d";4764}47654766my$have_blame= gitweb_check_feature('blame');4767open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4768or die_error(500,"Couldn't cat$file_name,$hash");4769my$mimetype= blob_mimetype($fd,$file_name);4770if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4771close$fd;4772return git_blob_plain($mimetype);4773}4774# we can have blame only for text/* mimetype4775$have_blame&&= ($mimetype=~m!^text/!);47764777 git_header_html(undef,$expires);4778my$formats_nav='';4779if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4780if(defined$file_name) {4781if($have_blame) {4782$formats_nav.=4783$cgi->a({-href => href(action=>"blame", -replay=>1)},4784"blame") .4785" | ";4786}4787$formats_nav.=4788$cgi->a({-href => href(action=>"history", -replay=>1)},4789"history") .4790" | ".4791$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4792"raw") .4793" | ".4794$cgi->a({-href => href(action=>"blob",4795 hash_base=>"HEAD", file_name=>$file_name)},4796"HEAD");4797}else{4798$formats_nav.=4799$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4800"raw");4801}4802 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4803 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4804}else{4805print"<div class=\"page_nav\">\n".4806"<br/><br/></div>\n".4807"<div class=\"title\">$hash</div>\n";4808}4809 git_print_page_path($file_name,"blob",$hash_base);4810print"<div class=\"page_body\">\n";4811if($mimetype=~m!^image/!) {4812print qq!<img type="$mimetype"!;4813if($file_name) {4814print qq! alt="$file_name" title="$file_name"!;4815}4816print qq! src="! .4817 href(action=>"blob_plain", hash=>$hash,4818 hash_base=>$hash_base, file_name=>$file_name) .4819 qq!"/>\n!;4820}else{4821my$nr;4822while(my$line= <$fd>) {4823chomp$line;4824$nr++;4825$line= untabify($line);4826printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4827$nr,$nr,$nr, esc_html($line, -nbsp=>1);4828}4829}4830close$fd4831or print"Reading blob failed.\n";4832print"</div>";4833 git_footer_html();4834}48354836sub git_tree {4837if(!defined$hash_base) {4838$hash_base="HEAD";4839}4840if(!defined$hash) {4841if(defined$file_name) {4842$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4843}else{4844$hash=$hash_base;4845}4846}4847 die_error(404,"No such tree")unlessdefined($hash);4848$/="\0";4849open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4850or die_error(500,"Open git-ls-tree failed");4851my@entries=map{chomp;$_} <$fd>;4852close$fdor die_error(404,"Reading tree failed");4853$/="\n";48544855my$refs= git_get_references();4856my$ref= format_ref_marker($refs,$hash_base);4857 git_header_html();4858my$basedir='';4859my$have_blame= gitweb_check_feature('blame');4860if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4861my@views_nav= ();4862if(defined$file_name) {4863push@views_nav,4864$cgi->a({-href => href(action=>"history", -replay=>1)},4865"history"),4866$cgi->a({-href => href(action=>"tree",4867 hash_base=>"HEAD", file_name=>$file_name)},4868"HEAD"),4869}4870my$snapshot_links= format_snapshot_links($hash);4871if(defined$snapshot_links) {4872# FIXME: Should be available when we have no hash base as well.4873push@views_nav,$snapshot_links;4874}4875 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4876 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4877}else{4878undef$hash_base;4879print"<div class=\"page_nav\">\n";4880print"<br/><br/></div>\n";4881print"<div class=\"title\">$hash</div>\n";4882}4883if(defined$file_name) {4884$basedir=$file_name;4885if($basedirne''&&substr($basedir, -1)ne'/') {4886$basedir.='/';4887}4888 git_print_page_path($file_name,'tree',$hash_base);4889}4890print"<div class=\"page_body\">\n";4891print"<table class=\"tree\">\n";4892my$alternate=1;4893# '..' (top directory) link if possible4894if(defined$hash_base&&4895defined$file_name&&$file_name=~m![^/]+$!) {4896if($alternate) {4897print"<tr class=\"dark\">\n";4898}else{4899print"<tr class=\"light\">\n";4900}4901$alternate^=1;49024903my$up=$file_name;4904$up=~s!/?[^/]+$!!;4905undef$upunless$up;4906# based on git_print_tree_entry4907print'<td class="mode">'. mode_str('040000') ."</td>\n";4908print'<td class="list">';4909print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4910 file_name=>$up)},4911"..");4912print"</td>\n";4913print"<td class=\"link\"></td>\n";49144915print"</tr>\n";4916}4917foreachmy$line(@entries) {4918my%t= parse_ls_tree_line($line, -z =>1);49194920if($alternate) {4921print"<tr class=\"dark\">\n";4922}else{4923print"<tr class=\"light\">\n";4924}4925$alternate^=1;49264927 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);49284929print"</tr>\n";4930}4931print"</table>\n".4932"</div>";4933 git_footer_html();4934}49354936sub git_snapshot {4937my$format=$input_params{'snapshot_format'};4938if(!@snapshot_fmts) {4939 die_error(403,"Snapshots not allowed");4940}4941# default to first supported snapshot format4942$format||=$snapshot_fmts[0];4943if($format!~m/^[a-z0-9]+$/) {4944 die_error(400,"Invalid snapshot format parameter");4945}elsif(!exists($known_snapshot_formats{$format})) {4946 die_error(400,"Unknown snapshot format");4947}elsif(!grep($_eq$format,@snapshot_fmts)) {4948 die_error(403,"Unsupported snapshot format");4949}49504951if(!defined$hash) {4952$hash= git_get_head_hash($project);4953}49544955my$name=$project;4956$name=~ s,([^/])/*\.git$,$1,;4957$name= basename($name);4958my$filename= to_utf8($name);4959$name=~s/\047/\047\\\047\047/g;4960my$cmd;4961$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";4962$cmd= quote_command(4963 git_cmd(),'archive',4964"--format=$known_snapshot_formats{$format}{'format'}",4965"--prefix=$name/",$hash);4966if(exists$known_snapshot_formats{$format}{'compressor'}) {4967$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});4968}49694970print$cgi->header(4971-type =>$known_snapshot_formats{$format}{'type'},4972-content_disposition =>'inline; filename="'."$filename".'"',4973-status =>'200 OK');49744975open my$fd,"-|",$cmd4976or die_error(500,"Execute git-archive failed");4977binmode STDOUT,':raw';4978print<$fd>;4979binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4980close$fd;4981}49824983sub git_log {4984my$head= git_get_head_hash($project);4985if(!defined$hash) {4986$hash=$head;4987}4988if(!defined$page) {4989$page=0;4990}4991my$refs= git_get_references();49924993my@commitlist= parse_commits($hash,101, (100*$page));49944995my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);49964997 git_header_html();4998 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);49995000if(!@commitlist) {5001my%co= parse_commit($hash);50025003 git_print_header_div('summary',$project);5004print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5005}5006my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5007for(my$i=0;$i<=$to;$i++) {5008my%co= %{$commitlist[$i]};5009next if!%co;5010my$commit=$co{'id'};5011my$ref= format_ref_marker($refs,$commit);5012my%ad= parse_date($co{'author_epoch'});5013 git_print_header_div('commit',5014"<span class=\"age\">$co{'age_string'}</span>".5015 esc_html($co{'title'}) .$ref,5016$commit);5017print"<div class=\"title_text\">\n".5018"<div class=\"log_link\">\n".5019$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5020" | ".5021$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5022" | ".5023$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5024"<br/>\n".5025"</div>\n".5026"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".5027"</div>\n";50285029print"<div class=\"log_body\">\n";5030 git_print_log($co{'comment'}, -final_empty_line=>1);5031print"</div>\n";5032}5033if($#commitlist>=100) {5034print"<div class=\"page_nav\">\n";5035print$cgi->a({-href => href(-replay=>1, page=>$page+1),5036-accesskey =>"n", -title =>"Alt-n"},"next");5037print"</div>\n";5038}5039 git_footer_html();5040}50415042sub git_commit {5043$hash||=$hash_base||"HEAD";5044my%co= parse_commit($hash)5045or die_error(404,"Unknown commit object");5046my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5047my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});50485049my$parent=$co{'parent'};5050my$parents=$co{'parents'};# listref50515052# we need to prepare $formats_nav before any parameter munging5053my$formats_nav;5054if(!defined$parent) {5055# --root commitdiff5056$formats_nav.='(initial)';5057}elsif(@$parents==1) {5058# single parent commit5059$formats_nav.=5060'(parent: '.5061$cgi->a({-href => href(action=>"commit",5062 hash=>$parent)},5063 esc_html(substr($parent,0,7))) .5064')';5065}else{5066# merge commit5067$formats_nav.=5068'(merge: '.5069join(' ',map{5070$cgi->a({-href => href(action=>"commit",5071 hash=>$_)},5072 esc_html(substr($_,0,7)));5073}@$parents) .5074')';5075}50765077if(!defined$parent) {5078$parent="--root";5079}5080my@difftree;5081open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5082@diff_opts,5083(@$parents<=1?$parent:'-c'),5084$hash,"--"5085or die_error(500,"Open git-diff-tree failed");5086@difftree=map{chomp;$_} <$fd>;5087close$fdor die_error(404,"Reading git-diff-tree failed");50885089# non-textual hash id's can be cached5090my$expires;5091if($hash=~m/^[0-9a-fA-F]{40}$/) {5092$expires="+1d";5093}5094my$refs= git_get_references();5095my$ref= format_ref_marker($refs,$co{'id'});50965097 git_header_html(undef,$expires);5098 git_print_page_nav('commit','',5099$hash,$co{'tree'},$hash,5100$formats_nav);51015102if(defined$co{'parent'}) {5103 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5104}else{5105 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5106}5107print"<div class=\"title_text\">\n".5108"<table class=\"object_header\">\n";5109print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5110"<tr>".5111"<td></td><td>$ad{'rfc2822'}";5112if($ad{'hour_local'} <6) {5113printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5114$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5115}else{5116printf(" (%02d:%02d%s)",5117$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5118}5119print"</td>".5120"</tr>\n";5121print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5122print"<tr><td></td><td>$cd{'rfc2822'}".5123sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5124"</td></tr>\n";5125print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5126print"<tr>".5127"<td>tree</td>".5128"<td class=\"sha1\">".5129$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5130class=>"list"},$co{'tree'}) .5131"</td>".5132"<td class=\"link\">".5133$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5134"tree");5135my$snapshot_links= format_snapshot_links($hash);5136if(defined$snapshot_links) {5137print" | ".$snapshot_links;5138}5139print"</td>".5140"</tr>\n";51415142foreachmy$par(@$parents) {5143print"<tr>".5144"<td>parent</td>".5145"<td class=\"sha1\">".5146$cgi->a({-href => href(action=>"commit", hash=>$par),5147class=>"list"},$par) .5148"</td>".5149"<td class=\"link\">".5150$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5151" | ".5152$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5153"</td>".5154"</tr>\n";5155}5156print"</table>".5157"</div>\n";51585159print"<div class=\"page_body\">\n";5160 git_print_log($co{'comment'});5161print"</div>\n";51625163 git_difftree_body(\@difftree,$hash,@$parents);51645165 git_footer_html();5166}51675168sub git_object {5169# object is defined by:5170# - hash or hash_base alone5171# - hash_base and file_name5172my$type;51735174# - hash or hash_base alone5175if($hash|| ($hash_base&& !defined$file_name)) {5176my$object_id=$hash||$hash_base;51775178open my$fd,"-|", quote_command(5179 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5180or die_error(404,"Object does not exist");5181$type= <$fd>;5182chomp$type;5183close$fd5184or die_error(404,"Object does not exist");51855186# - hash_base and file_name5187}elsif($hash_base&&defined$file_name) {5188$file_name=~ s,/+$,,;51895190system(git_cmd(),"cat-file",'-e',$hash_base) ==05191or die_error(404,"Base object does not exist");51925193# here errors should not hapen5194open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5195or die_error(500,"Open git-ls-tree failed");5196my$line= <$fd>;5197close$fd;51985199#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5200unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5201 die_error(404,"File or directory for given base does not exist");5202}5203$type=$2;5204$hash=$3;5205}else{5206 die_error(400,"Not enough information to find object");5207}52085209print$cgi->redirect(-uri => href(action=>$type, -full=>1,5210 hash=>$hash, hash_base=>$hash_base,5211 file_name=>$file_name),5212-status =>'302 Found');5213}52145215sub git_blobdiff {5216my$format=shift||'html';52175218my$fd;5219my@difftree;5220my%diffinfo;5221my$expires;52225223# preparing $fd and %diffinfo for git_patchset_body5224# new style URI5225if(defined$hash_base&&defined$hash_parent_base) {5226if(defined$file_name) {5227# read raw output5228open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5229$hash_parent_base,$hash_base,5230"--", (defined$file_parent?$file_parent: ()),$file_name5231or die_error(500,"Open git-diff-tree failed");5232@difftree=map{chomp;$_} <$fd>;5233close$fd5234or die_error(404,"Reading git-diff-tree failed");5235@difftree5236or die_error(404,"Blob diff not found");52375238}elsif(defined$hash&&5239$hash=~/[0-9a-fA-F]{40}/) {5240# try to find filename from $hash52415242# read filtered raw output5243open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5244$hash_parent_base,$hash_base,"--"5245or die_error(500,"Open git-diff-tree failed");5246@difftree=5247# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5248# $hash == to_id5249grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5250map{chomp;$_} <$fd>;5251close$fd5252or die_error(404,"Reading git-diff-tree failed");5253@difftree5254or die_error(404,"Blob diff not found");52555256}else{5257 die_error(400,"Missing one of the blob diff parameters");5258}52595260if(@difftree>1) {5261 die_error(400,"Ambiguous blob diff specification");5262}52635264%diffinfo= parse_difftree_raw_line($difftree[0]);5265$file_parent||=$diffinfo{'from_file'} ||$file_name;5266$file_name||=$diffinfo{'to_file'};52675268$hash_parent||=$diffinfo{'from_id'};5269$hash||=$diffinfo{'to_id'};52705271# non-textual hash id's can be cached5272if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5273$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5274$expires='+1d';5275}52765277# open patch output5278open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5279'-p', ($formateq'html'?"--full-index": ()),5280$hash_parent_base,$hash_base,5281"--", (defined$file_parent?$file_parent: ()),$file_name5282or die_error(500,"Open git-diff-tree failed");5283}52845285# old/legacy style URI5286if(!%diffinfo&&# if new style URI failed5287defined$hash&&defined$hash_parent) {5288# fake git-diff-tree raw output5289$diffinfo{'from_mode'} =$diffinfo{'to_mode'} ="blob";5290$diffinfo{'from_id'} =$hash_parent;5291$diffinfo{'to_id'} =$hash;5292if(defined$file_name) {5293if(defined$file_parent) {5294$diffinfo{'status'} ='2';5295$diffinfo{'from_file'} =$file_parent;5296$diffinfo{'to_file'} =$file_name;5297}else{# assume not renamed5298$diffinfo{'status'} ='1';5299$diffinfo{'from_file'} =$file_name;5300$diffinfo{'to_file'} =$file_name;5301}5302}else{# no filename given5303$diffinfo{'status'} ='2';5304$diffinfo{'from_file'} =$hash_parent;5305$diffinfo{'to_file'} =$hash;5306}53075308# non-textual hash id's can be cached5309if($hash=~m/^[0-9a-fA-F]{40}$/&&5310$hash_parent=~m/^[0-9a-fA-F]{40}$/) {5311$expires='+1d';5312}53135314# open patch output5315open$fd,"-|", git_cmd(),"diff",@diff_opts,5316'-p', ($formateq'html'?"--full-index": ()),5317$hash_parent,$hash,"--"5318or die_error(500,"Open git-diff failed");5319}else{5320 die_error(400,"Missing one of the blob diff parameters")5321unless%diffinfo;5322}53235324# header5325if($formateq'html') {5326my$formats_nav=5327$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5328"raw");5329 git_header_html(undef,$expires);5330if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5331 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5332 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5333}else{5334print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5335print"<div class=\"title\">$hashvs$hash_parent</div>\n";5336}5337if(defined$file_name) {5338 git_print_page_path($file_name,"blob",$hash_base);5339}else{5340print"<div class=\"page_path\"></div>\n";5341}53425343}elsif($formateq'plain') {5344print$cgi->header(5345-type =>'text/plain',5346-charset =>'utf-8',5347-expires =>$expires,5348-content_disposition =>'inline; filename="'."$file_name".'.patch"');53495350print"X-Git-Url: ".$cgi->self_url() ."\n\n";53515352}else{5353 die_error(400,"Unknown blobdiff format");5354}53555356# patch5357if($formateq'html') {5358print"<div class=\"page_body\">\n";53595360 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5361close$fd;53625363print"</div>\n";# class="page_body"5364 git_footer_html();53655366}else{5367while(my$line= <$fd>) {5368$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5369$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;53705371print$line;53725373last if$line=~m!^\+\+\+!;5374}5375local$/=undef;5376print<$fd>;5377close$fd;5378}5379}53805381sub git_blobdiff_plain {5382 git_blobdiff('plain');5383}53845385sub git_commitdiff {5386my$format=shift||'html';5387$hash||=$hash_base||"HEAD";5388my%co= parse_commit($hash)5389or die_error(404,"Unknown commit object");53905391# choose format for commitdiff for merge5392if(!defined$hash_parent&& @{$co{'parents'}} >1) {5393$hash_parent='--cc';5394}5395# we need to prepare $formats_nav before almost any parameter munging5396my$formats_nav;5397if($formateq'html') {5398$formats_nav=5399$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5400"raw");54015402if(defined$hash_parent&&5403$hash_parentne'-c'&&$hash_parentne'--cc') {5404# commitdiff with two commits given5405my$hash_parent_short=$hash_parent;5406if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5407$hash_parent_short=substr($hash_parent,0,7);5408}5409$formats_nav.=5410' (from';5411for(my$i=0;$i< @{$co{'parents'}};$i++) {5412if($co{'parents'}[$i]eq$hash_parent) {5413$formats_nav.=' parent '. ($i+1);5414last;5415}5416}5417$formats_nav.=': '.5418$cgi->a({-href => href(action=>"commitdiff",5419 hash=>$hash_parent)},5420 esc_html($hash_parent_short)) .5421')';5422}elsif(!$co{'parent'}) {5423# --root commitdiff5424$formats_nav.=' (initial)';5425}elsif(scalar@{$co{'parents'}} ==1) {5426# single parent commit5427$formats_nav.=5428' (parent: '.5429$cgi->a({-href => href(action=>"commitdiff",5430 hash=>$co{'parent'})},5431 esc_html(substr($co{'parent'},0,7))) .5432')';5433}else{5434# merge commit5435if($hash_parenteq'--cc') {5436$formats_nav.=' | '.5437$cgi->a({-href => href(action=>"commitdiff",5438 hash=>$hash, hash_parent=>'-c')},5439'combined');5440}else{# $hash_parent eq '-c'5441$formats_nav.=' | '.5442$cgi->a({-href => href(action=>"commitdiff",5443 hash=>$hash, hash_parent=>'--cc')},5444'compact');5445}5446$formats_nav.=5447' (merge: '.5448join(' ',map{5449$cgi->a({-href => href(action=>"commitdiff",5450 hash=>$_)},5451 esc_html(substr($_,0,7)));5452} @{$co{'parents'}} ) .5453')';5454}5455}54565457my$hash_parent_param=$hash_parent;5458if(!defined$hash_parent_param) {5459# --cc for multiple parents, --root for parentless5460$hash_parent_param=5461@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5462}54635464# read commitdiff5465my$fd;5466my@difftree;5467if($formateq'html') {5468open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5469"--no-commit-id","--patch-with-raw","--full-index",5470$hash_parent_param,$hash,"--"5471or die_error(500,"Open git-diff-tree failed");54725473while(my$line= <$fd>) {5474chomp$line;5475# empty line ends raw part of diff-tree output5476last unless$line;5477push@difftree,scalar parse_difftree_raw_line($line);5478}54795480}elsif($formateq'plain') {5481open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5482'-p',$hash_parent_param,$hash,"--"5483or die_error(500,"Open git-diff-tree failed");54845485}else{5486 die_error(400,"Unknown commitdiff format");5487}54885489# non-textual hash id's can be cached5490my$expires;5491if($hash=~m/^[0-9a-fA-F]{40}$/) {5492$expires="+1d";5493}54945495# write commit message5496if($formateq'html') {5497my$refs= git_get_references();5498my$ref= format_ref_marker($refs,$co{'id'});54995500 git_header_html(undef,$expires);5501 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5502 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5503 git_print_authorship(\%co);5504print"<div class=\"page_body\">\n";5505if(@{$co{'comment'}} >1) {5506print"<div class=\"log\">\n";5507 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5508print"</div>\n";# class="log"5509}55105511}elsif($formateq'plain') {5512my$refs= git_get_references("tags");5513my$tagname= git_get_rev_name_tags($hash);5514my$filename= basename($project) ."-$hash.patch";55155516print$cgi->header(5517-type =>'text/plain',5518-charset =>'utf-8',5519-expires =>$expires,5520-content_disposition =>'inline; filename="'."$filename".'"');5521my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5522print"From: ". to_utf8($co{'author'}) ."\n";5523print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5524print"Subject: ". to_utf8($co{'title'}) ."\n";55255526print"X-Git-Tag:$tagname\n"if$tagname;5527print"X-Git-Url: ".$cgi->self_url() ."\n\n";55285529foreachmy$line(@{$co{'comment'}}) {5530print to_utf8($line) ."\n";5531}5532print"---\n\n";5533}55345535# write patch5536if($formateq'html') {5537my$use_parents= !defined$hash_parent||5538$hash_parenteq'-c'||$hash_parenteq'--cc';5539 git_difftree_body(\@difftree,$hash,5540$use_parents? @{$co{'parents'}} :$hash_parent);5541print"<br/>\n";55425543 git_patchset_body($fd, \@difftree,$hash,5544$use_parents? @{$co{'parents'}} :$hash_parent);5545close$fd;5546print"</div>\n";# class="page_body"5547 git_footer_html();55485549}elsif($formateq'plain') {5550local$/=undef;5551print<$fd>;5552close$fd5553or print"Reading git-diff-tree failed\n";5554}5555}55565557sub git_commitdiff_plain {5558 git_commitdiff('plain');5559}55605561sub git_history {5562if(!defined$hash_base) {5563$hash_base= git_get_head_hash($project);5564}5565if(!defined$page) {5566$page=0;5567}5568my$ftype;5569my%co= parse_commit($hash_base)5570or die_error(404,"Unknown commit object");55715572my$refs= git_get_references();5573my$limit=sprintf("--max-count=%i", (100* ($page+1)));55745575my@commitlist= parse_commits($hash_base,101, (100*$page),5576$file_name,"--full-history")5577or die_error(404,"No such file or directory on given branch");55785579if(!defined$hash&&defined$file_name) {5580# some commits could have deleted file in question,5581# and not have it in tree, but one of them has to have it5582for(my$i=0;$i<=@commitlist;$i++) {5583$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5584last ifdefined$hash;5585}5586}5587if(defined$hash) {5588$ftype= git_get_type($hash);5589}5590if(!defined$ftype) {5591 die_error(500,"Unknown type of object");5592}55935594my$paging_nav='';5595if($page>0) {5596$paging_nav.=5597$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5598 file_name=>$file_name)},5599"first");5600$paging_nav.=" ⋅ ".5601$cgi->a({-href => href(-replay=>1, page=>$page-1),5602-accesskey =>"p", -title =>"Alt-p"},"prev");5603}else{5604$paging_nav.="first";5605$paging_nav.=" ⋅ prev";5606}5607my$next_link='';5608if($#commitlist>=100) {5609$next_link=5610$cgi->a({-href => href(-replay=>1, page=>$page+1),5611-accesskey =>"n", -title =>"Alt-n"},"next");5612$paging_nav.=" ⋅$next_link";5613}else{5614$paging_nav.=" ⋅ next";5615}56165617 git_header_html();5618 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5619 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5620 git_print_page_path($file_name,$ftype,$hash_base);56215622 git_history_body(\@commitlist,0,99,5623$refs,$hash_base,$ftype,$next_link);56245625 git_footer_html();5626}56275628sub git_search {5629 gitweb_check_feature('search')or die_error(403,"Search is disabled");5630if(!defined$searchtext) {5631 die_error(400,"Text field is empty");5632}5633if(!defined$hash) {5634$hash= git_get_head_hash($project);5635}5636my%co= parse_commit($hash);5637if(!%co) {5638 die_error(404,"Unknown commit object");5639}5640if(!defined$page) {5641$page=0;5642}56435644$searchtype||='commit';5645if($searchtypeeq'pickaxe') {5646# pickaxe may take all resources of your box and run for several minutes5647# with every query - so decide by yourself how public you make this feature5648 gitweb_check_feature('pickaxe')5649or die_error(403,"Pickaxe is disabled");5650}5651if($searchtypeeq'grep') {5652 gitweb_check_feature('grep')5653or die_error(403,"Grep is disabled");5654}56555656 git_header_html();56575658if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5659my$greptype;5660if($searchtypeeq'commit') {5661$greptype="--grep=";5662}elsif($searchtypeeq'author') {5663$greptype="--author=";5664}elsif($searchtypeeq'committer') {5665$greptype="--committer=";5666}5667$greptype.=$searchtext;5668my@commitlist= parse_commits($hash,101, (100*$page),undef,5669$greptype,'--regexp-ignore-case',5670$search_use_regexp?'--extended-regexp':'--fixed-strings');56715672my$paging_nav='';5673if($page>0) {5674$paging_nav.=5675$cgi->a({-href => href(action=>"search", hash=>$hash,5676 searchtext=>$searchtext,5677 searchtype=>$searchtype)},5678"first");5679$paging_nav.=" ⋅ ".5680$cgi->a({-href => href(-replay=>1, page=>$page-1),5681-accesskey =>"p", -title =>"Alt-p"},"prev");5682}else{5683$paging_nav.="first";5684$paging_nav.=" ⋅ prev";5685}5686my$next_link='';5687if($#commitlist>=100) {5688$next_link=5689$cgi->a({-href => href(-replay=>1, page=>$page+1),5690-accesskey =>"n", -title =>"Alt-n"},"next");5691$paging_nav.=" ⋅$next_link";5692}else{5693$paging_nav.=" ⋅ next";5694}56955696if($#commitlist>=100) {5697}56985699 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5700 git_print_header_div('commit', esc_html($co{'title'}),$hash);5701 git_search_grep_body(\@commitlist,0,99,$next_link);5702}57035704if($searchtypeeq'pickaxe') {5705 git_print_page_nav('','',$hash,$co{'tree'},$hash);5706 git_print_header_div('commit', esc_html($co{'title'}),$hash);57075708print"<table class=\"pickaxe search\">\n";5709my$alternate=1;5710$/="\n";5711open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5712'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5713($search_use_regexp?'--pickaxe-regex': ());5714undef%co;5715my@files;5716while(my$line= <$fd>) {5717chomp$line;5718next unless$line;57195720my%set= parse_difftree_raw_line($line);5721if(defined$set{'commit'}) {5722# finish previous commit5723if(%co) {5724print"</td>\n".5725"<td class=\"link\">".5726$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5727" | ".5728$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5729print"</td>\n".5730"</tr>\n";5731}57325733if($alternate) {5734print"<tr class=\"dark\">\n";5735}else{5736print"<tr class=\"light\">\n";5737}5738$alternate^=1;5739%co= parse_commit($set{'commit'});5740my$author= chop_and_escape_str($co{'author_name'},15,5);5741print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5742"<td><i>$author</i></td>\n".5743"<td>".5744$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5745-class=>"list subject"},5746 chop_and_escape_str($co{'title'},50) ."<br/>");5747}elsif(defined$set{'to_id'}) {5748next if($set{'to_id'} =~m/^0{40}$/);57495750print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5751 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5752-class=>"list"},5753"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5754"<br/>\n";5755}5756}5757close$fd;57585759# finish last commit (warning: repetition!)5760if(%co) {5761print"</td>\n".5762"<td class=\"link\">".5763$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5764" | ".5765$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5766print"</td>\n".5767"</tr>\n";5768}57695770print"</table>\n";5771}57725773if($searchtypeeq'grep') {5774 git_print_page_nav('','',$hash,$co{'tree'},$hash);5775 git_print_header_div('commit', esc_html($co{'title'}),$hash);57765777print"<table class=\"grep_search\">\n";5778my$alternate=1;5779my$matches=0;5780$/="\n";5781open my$fd,"-|", git_cmd(),'grep','-n',5782$search_use_regexp? ('-E','-i') :'-F',5783$searchtext,$co{'tree'};5784my$lastfile='';5785while(my$line= <$fd>) {5786chomp$line;5787my($file,$lno,$ltext,$binary);5788last if($matches++>1000);5789if($line=~/^Binary file (.+) matches$/) {5790$file=$1;5791$binary=1;5792}else{5793(undef,$file,$lno,$ltext) =split(/:/,$line,4);5794}5795if($filene$lastfile) {5796$lastfileand print"</td></tr>\n";5797if($alternate++) {5798print"<tr class=\"dark\">\n";5799}else{5800print"<tr class=\"light\">\n";5801}5802print"<td class=\"list\">".5803$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5804 file_name=>"$file"),5805-class=>"list"}, esc_path($file));5806print"</td><td>\n";5807$lastfile=$file;5808}5809if($binary) {5810print"<div class=\"binary\">Binary file</div>\n";5811}else{5812$ltext= untabify($ltext);5813if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5814$ltext= esc_html($1, -nbsp=>1);5815$ltext.='<span class="match">';5816$ltext.= esc_html($2, -nbsp=>1);5817$ltext.='</span>';5818$ltext.= esc_html($3, -nbsp=>1);5819}else{5820$ltext= esc_html($ltext, -nbsp=>1);5821}5822print"<div class=\"pre\">".5823$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5824 file_name=>"$file").'#l'.$lno,5825-class=>"linenr"},sprintf('%4i',$lno))5826.' '.$ltext."</div>\n";5827}5828}5829if($lastfile) {5830print"</td></tr>\n";5831if($matches>1000) {5832print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5833}5834}else{5835print"<div class=\"diff nodifferences\">No matches found</div>\n";5836}5837close$fd;58385839print"</table>\n";5840}5841 git_footer_html();5842}58435844sub git_search_help {5845 git_header_html();5846 git_print_page_nav('','',$hash,$hash,$hash);5847print<<EOT;5848<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5849regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5850the pattern entered is recognized as the POSIX extended5851<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5852insensitive).</p>5853<dl>5854<dt><b>commit</b></dt>5855<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5856EOT5857my$have_grep= gitweb_check_feature('grep');5858if($have_grep) {5859print<<EOT;5860<dt><b>grep</b></dt>5861<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5862 a different one) are searched for the given pattern. On large trees, this search can take5863a while and put some strain on the server, so please use it with some consideration. Note that5864due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5865case-sensitive.</dd>5866EOT5867}5868print<<EOT;5869<dt><b>author</b></dt>5870<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5871<dt><b>committer</b></dt>5872<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5873EOT5874my$have_pickaxe= gitweb_check_feature('pickaxe');5875if($have_pickaxe) {5876print<<EOT;5877<dt><b>pickaxe</b></dt>5878<dd>All commits that caused the string to appear or disappear from any file (changes that5879added, removed or "modified" the string) will be listed. This search can take a while and5880takes a lot of strain on the server, so please use it wisely. Note that since you may be5881interested even in changes just changing the case as well, this search is case sensitive.</dd>5882EOT5883}5884print"</dl>\n";5885 git_footer_html();5886}58875888sub git_shortlog {5889my$head= git_get_head_hash($project);5890if(!defined$hash) {5891$hash=$head;5892}5893if(!defined$page) {5894$page=0;5895}5896my$refs= git_get_references();58975898my$commit_hash=$hash;5899if(defined$hash_parent) {5900$commit_hash="$hash_parent..$hash";5901}5902my@commitlist= parse_commits($commit_hash,101, (100*$page));59035904my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5905my$next_link='';5906if($#commitlist>=100) {5907$next_link=5908$cgi->a({-href => href(-replay=>1, page=>$page+1),5909-accesskey =>"n", -title =>"Alt-n"},"next");5910}59115912 git_header_html();5913 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);5914 git_print_header_div('summary',$project);59155916 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);59175918 git_footer_html();5919}59205921## ......................................................................5922## feeds (RSS, Atom; OPML)59235924sub git_feed {5925my$format=shift||'atom';5926my$have_blame= gitweb_check_feature('blame');59275928# Atom: http://www.atomenabled.org/developers/syndication/5929# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ5930if($formatne'rss'&&$formatne'atom') {5931 die_error(400,"Unknown web feed format");5932}59335934# log/feed of current (HEAD) branch, log of given branch, history of file/directory5935my$head=$hash||'HEAD';5936my@commitlist= parse_commits($head,150,0,$file_name);59375938my%latest_commit;5939my%latest_date;5940my$content_type="application/$format+xml";5941if(defined$cgi->http('HTTP_ACCEPT') &&5942$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {5943# browser (feed reader) prefers text/xml5944$content_type='text/xml';5945}5946if(defined($commitlist[0])) {5947%latest_commit= %{$commitlist[0]};5948%latest_date= parse_date($latest_commit{'author_epoch'});5949print$cgi->header(5950-type =>$content_type,5951-charset =>'utf-8',5952-last_modified =>$latest_date{'rfc2822'});5953}else{5954print$cgi->header(5955-type =>$content_type,5956-charset =>'utf-8');5957}59585959# Optimization: skip generating the body if client asks only5960# for Last-Modified date.5961return if($cgi->request_method()eq'HEAD');59625963# header variables5964my$title="$site_name-$project/$action";5965my$feed_type='log';5966if(defined$hash) {5967$title.=" - '$hash'";5968$feed_type='branch log';5969if(defined$file_name) {5970$title.=" ::$file_name";5971$feed_type='history';5972}5973}elsif(defined$file_name) {5974$title.=" -$file_name";5975$feed_type='history';5976}5977$title.="$feed_type";5978my$descr= git_get_project_description($project);5979if(defined$descr) {5980$descr= esc_html($descr);5981}else{5982$descr="$project".5983($formateq'rss'?'RSS':'Atom') .5984" feed";5985}5986my$owner= git_get_project_owner($project);5987$owner= esc_html($owner);59885989#header5990my$alt_url;5991if(defined$file_name) {5992$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);5993}elsif(defined$hash) {5994$alt_url= href(-full=>1, action=>"log", hash=>$hash);5995}else{5996$alt_url= href(-full=>1, action=>"summary");5997}5998print qq!<?xml version="1.0" encoding="utf-8"?>\n!;5999if($formateq'rss') {6000print<<XML;6001<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6002<channel>6003XML6004print"<title>$title</title>\n".6005"<link>$alt_url</link>\n".6006"<description>$descr</description>\n".6007"<language>en</language>\n";6008}elsif($formateq'atom') {6009print<<XML;6010<feed xmlns="http://www.w3.org/2005/Atom">6011XML6012print"<title>$title</title>\n".6013"<subtitle>$descr</subtitle>\n".6014'<link rel="alternate" type="text/html" href="'.6015$alt_url.'" />'."\n".6016'<link rel="self" type="'.$content_type.'" href="'.6017$cgi->self_url() .'" />'."\n".6018"<id>". href(-full=>1) ."</id>\n".6019# use project owner for feed author6020"<author><name>$owner</name></author>\n";6021if(defined$favicon) {6022print"<icon>". esc_url($favicon) ."</icon>\n";6023}6024if(defined$logo_url) {6025# not twice as wide as tall: 72 x 27 pixels6026print"<logo>". esc_url($logo) ."</logo>\n";6027}6028if(!%latest_date) {6029# dummy date to keep the feed valid until commits trickle in:6030print"<updated>1970-01-01T00:00:00Z</updated>\n";6031}else{6032print"<updated>$latest_date{'iso-8601'}</updated>\n";6033}6034}60356036# contents6037for(my$i=0;$i<=$#commitlist;$i++) {6038my%co= %{$commitlist[$i]};6039my$commit=$co{'id'};6040# we read 150, we always show 30 and the ones more recent than 48 hours6041if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6042last;6043}6044my%cd= parse_date($co{'author_epoch'});60456046# get list of changed files6047open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6048$co{'parent'} ||"--root",6049$co{'id'},"--", (defined$file_name?$file_name: ())6050ornext;6051my@difftree=map{chomp;$_} <$fd>;6052close$fd6053ornext;60546055# print element (entry, item)6056my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6057if($formateq'rss') {6058print"<item>\n".6059"<title>". esc_html($co{'title'}) ."</title>\n".6060"<author>". esc_html($co{'author'}) ."</author>\n".6061"<pubDate>$cd{'rfc2822'}</pubDate>\n".6062"<guid isPermaLink=\"true\">$co_url</guid>\n".6063"<link>$co_url</link>\n".6064"<description>". esc_html($co{'title'}) ."</description>\n".6065"<content:encoded>".6066"<![CDATA[\n";6067}elsif($formateq'atom') {6068print"<entry>\n".6069"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6070"<updated>$cd{'iso-8601'}</updated>\n".6071"<author>\n".6072" <name>". esc_html($co{'author_name'}) ."</name>\n";6073if($co{'author_email'}) {6074print" <email>". esc_html($co{'author_email'}) ."</email>\n";6075}6076print"</author>\n".6077# use committer for contributor6078"<contributor>\n".6079" <name>". esc_html($co{'committer_name'}) ."</name>\n";6080if($co{'committer_email'}) {6081print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6082}6083print"</contributor>\n".6084"<published>$cd{'iso-8601'}</published>\n".6085"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6086"<id>$co_url</id>\n".6087"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6088"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6089}6090my$comment=$co{'comment'};6091print"<pre>\n";6092foreachmy$line(@$comment) {6093$line= esc_html($line);6094print"$line\n";6095}6096print"</pre><ul>\n";6097foreachmy$difftree_line(@difftree) {6098my%difftree= parse_difftree_raw_line($difftree_line);6099next if!$difftree{'from_id'};61006101my$file=$difftree{'file'} ||$difftree{'to_file'};61026103print"<li>".6104"[".6105$cgi->a({-href => href(-full=>1, action=>"blobdiff",6106 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6107 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6108 file_name=>$file, file_parent=>$difftree{'from_file'}),6109-title =>"diff"},'D');6110if($have_blame) {6111print$cgi->a({-href => href(-full=>1, action=>"blame",6112 file_name=>$file, hash_base=>$commit),6113-title =>"blame"},'B');6114}6115# if this is not a feed of a file history6116if(!defined$file_name||$file_namene$file) {6117print$cgi->a({-href => href(-full=>1, action=>"history",6118 file_name=>$file, hash=>$commit),6119-title =>"history"},'H');6120}6121$file= esc_path($file);6122print"] ".6123"$file</li>\n";6124}6125if($formateq'rss') {6126print"</ul>]]>\n".6127"</content:encoded>\n".6128"</item>\n";6129}elsif($formateq'atom') {6130print"</ul>\n</div>\n".6131"</content>\n".6132"</entry>\n";6133}6134}61356136# end of feed6137if($formateq'rss') {6138print"</channel>\n</rss>\n";6139}elsif($formateq'atom') {6140print"</feed>\n";6141}6142}61436144sub git_rss {6145 git_feed('rss');6146}61476148sub git_atom {6149 git_feed('atom');6150}61516152sub git_opml {6153my@list= git_get_projects_list();61546155print$cgi->header(-type =>'text/xml', -charset =>'utf-8');6156print<<XML;6157<?xml version="1.0" encoding="utf-8"?>6158<opml version="1.0">6159<head>6160 <title>$site_nameOPML Export</title>6161</head>6162<body>6163<outline text="git RSS feeds">6164XML61656166foreachmy$pr(@list) {6167my%proj=%$pr;6168my$head= git_get_head_hash($proj{'path'});6169if(!defined$head) {6170next;6171}6172$git_dir="$projectroot/$proj{'path'}";6173my%co= parse_commit($head);6174if(!%co) {6175next;6176}61776178my$path= esc_html(chop_str($proj{'path'},25,5));6179my$rss="$my_url?p=$proj{'path'};a=rss";6180my$html="$my_url?p=$proj{'path'};a=summary";6181print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6182}6183print<<XML;6184</outline>6185</body>6186</opml>6187XML6188}