1#!/usr/bin/perl 2 3# gitweb - simple web interface to track changes in git repositories 4# 5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org> 6# (C) 2005, Christian Gierke 7# 8# This program is licensed under the GPLv2 9 10use strict; 11use warnings; 12use CGI qw(:standard :escapeHTML -nosticky); 13use CGI::Util qw(unescape); 14use CGI::Carp qw(fatalsToBrowser); 15use Encode; 16use Fcntl ':mode'; 17use File::Find qw(); 18use File::Basename qw(basename); 19binmode STDOUT,':utf8'; 20 21BEGIN{ 22 CGI->compile()if$ENV{'MOD_PERL'}; 23} 24 25our$cgi= new CGI; 26our$version="++GIT_VERSION++"; 27our$my_url=$cgi->url(); 28our$my_uri=$cgi->url(-absolute =>1); 29 30# if we're called with PATH_INFO, we have to strip that 31# from the URL to find our real URL 32# we make $path_info global because it's also used later on 33our$path_info=$ENV{"PATH_INFO"}; 34if($path_info) { 35$my_url=~ s,\Q$path_info\E$,,; 36$my_uri=~ s,\Q$path_info\E$,,; 37} 38 39# core git executable to use 40# this can just be "git" if your webserver has a sensible PATH 41our$GIT="++GIT_BINDIR++/git"; 42 43# absolute fs-path which will be prepended to the project path 44#our $projectroot = "/pub/scm"; 45our$projectroot="++GITWEB_PROJECTROOT++"; 46 47# fs traversing limit for getting project list 48# the number is relative to the projectroot 49our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 50 51# target of the home link on top of all pages 52our$home_link=$my_uri||"/"; 53 54# string of the home link on top of all pages 55our$home_link_str="++GITWEB_HOME_LINK_STR++"; 56 57# name of your site or organization to appear in page titles 58# replace this with something more descriptive for clearer bookmarks 59our$site_name="++GITWEB_SITENAME++" 60|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 61 62# filename of html text to include at top of each page 63our$site_header="++GITWEB_SITE_HEADER++"; 64# html text to include at home page 65our$home_text="++GITWEB_HOMETEXT++"; 66# filename of html text to include at bottom of each page 67our$site_footer="++GITWEB_SITE_FOOTER++"; 68 69# URI of stylesheets 70our@stylesheets= ("++GITWEB_CSS++"); 71# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 72our$stylesheet=undef; 73# URI of GIT logo (72x27 size) 74our$logo="++GITWEB_LOGO++"; 75# URI of GIT favicon, assumed to be image/png type 76our$favicon="++GITWEB_FAVICON++"; 77 78# URI and label (title) of GIT logo link 79#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 80#our $logo_label = "git documentation"; 81our$logo_url="http://git.or.cz/"; 82our$logo_label="git homepage"; 83 84# source of projects list 85our$projects_list="++GITWEB_LIST++"; 86 87# the width (in characters) of the projects list "Description" column 88our$projects_list_description_width=25; 89 90# default order of projects list 91# valid values are none, project, descr, owner, and age 92our$default_projects_order="project"; 93 94# show repository only if this file exists 95# (only effective if this variable evaluates to true) 96our$export_ok="++GITWEB_EXPORT_OK++"; 97 98# show repository only if this subroutine returns true 99# when given the path to the project, for example: 100# sub { return -e "$_[0]/git-daemon-export-ok"; } 101our$export_auth_hook=undef; 102 103# only allow viewing of repositories also shown on the overview page 104our$strict_export="++GITWEB_STRICT_EXPORT++"; 105 106# list of git base URLs used for URL to where fetch project from, 107# i.e. full URL is "$git_base_url/$project" 108our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 109 110# default blob_plain mimetype and default charset for text/plain blob 111our$default_blob_plain_mimetype='text/plain'; 112our$default_text_plain_charset=undef; 113 114# file to use for guessing MIME types before trying /etc/mime.types 115# (relative to the current git repository) 116our$mimetypes_file=undef; 117 118# assume this charset if line contains non-UTF-8 characters; 119# it should be valid encoding (see Encoding::Supported(3pm) for list), 120# for which encoding all byte sequences are valid, for example 121# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 122# could be even 'utf-8' for the old behavior) 123our$fallback_encoding='latin1'; 124 125# rename detection options for git-diff and git-diff-tree 126# - default is '-M', with the cost proportional to 127# (number of removed files) * (number of new files). 128# - more costly is '-C' (which implies '-M'), with the cost proportional to 129# (number of changed files + number of removed files) * (number of new files) 130# - even more costly is '-C', '--find-copies-harder' with cost 131# (number of files in the original tree) * (number of new files) 132# - one might want to include '-B' option, e.g. '-B', '-M' 133our@diff_opts= ('-M');# taken from git_commit 134 135# information about snapshot formats that gitweb is capable of serving 136our%known_snapshot_formats= ( 137# name => { 138# 'display' => display name, 139# 'type' => mime type, 140# 'suffix' => filename suffix, 141# 'format' => --format for git-archive, 142# 'compressor' => [compressor command and arguments] 143# (array reference, optional)} 144# 145'tgz'=> { 146'display'=>'tar.gz', 147'type'=>'application/x-gzip', 148'suffix'=>'.tar.gz', 149'format'=>'tar', 150'compressor'=> ['gzip']}, 151 152'tbz2'=> { 153'display'=>'tar.bz2', 154'type'=>'application/x-bzip2', 155'suffix'=>'.tar.bz2', 156'format'=>'tar', 157'compressor'=> ['bzip2']}, 158 159'zip'=> { 160'display'=>'zip', 161'type'=>'application/x-zip', 162'suffix'=>'.zip', 163'format'=>'zip'}, 164); 165 166# Aliases so we understand old gitweb.snapshot values in repository 167# configuration. 168our%known_snapshot_format_aliases= ( 169'gzip'=>'tgz', 170'bzip2'=>'tbz2', 171 172# backward compatibility: legacy gitweb config support 173'x-gzip'=>undef,'gz'=>undef, 174'x-bzip2'=>undef,'bz2'=>undef, 175'x-zip'=>undef,''=>undef, 176); 177 178# You define site-wide feature defaults here; override them with 179# $GITWEB_CONFIG as necessary. 180our%feature= ( 181# feature => { 182# 'sub' => feature-sub (subroutine), 183# 'override' => allow-override (boolean), 184# 'default' => [ default options...] (array reference)} 185# 186# if feature is overridable (it means that allow-override has true value), 187# then feature-sub will be called with default options as parameters; 188# return value of feature-sub indicates if to enable specified feature 189# 190# if there is no 'sub' key (no feature-sub), then feature cannot be 191# overriden 192# 193# use gitweb_get_feature(<feature>) to retrieve the <feature> value 194# (an array) or gitweb_check_feature(<feature>) to check if <feature> 195# is enabled 196 197# Enable the 'blame' blob view, showing the last commit that modified 198# each line in the file. This can be very CPU-intensive. 199 200# To enable system wide have in $GITWEB_CONFIG 201# $feature{'blame'}{'default'} = [1]; 202# To have project specific config enable override in $GITWEB_CONFIG 203# $feature{'blame'}{'override'} = 1; 204# and in project config gitweb.blame = 0|1; 205'blame'=> { 206'sub'=>sub{ feature_bool('blame',@_) }, 207'override'=>0, 208'default'=> [0]}, 209 210# Enable the 'snapshot' link, providing a compressed archive of any 211# tree. This can potentially generate high traffic if you have large 212# project. 213 214# Value is a list of formats defined in %known_snapshot_formats that 215# you wish to offer. 216# To disable system wide have in $GITWEB_CONFIG 217# $feature{'snapshot'}{'default'} = []; 218# To have project specific config enable override in $GITWEB_CONFIG 219# $feature{'snapshot'}{'override'} = 1; 220# and in project config, a comma-separated list of formats or "none" 221# to disable. Example: gitweb.snapshot = tbz2,zip; 222'snapshot'=> { 223'sub'=> \&feature_snapshot, 224'override'=>0, 225'default'=> ['tgz']}, 226 227# Enable text search, which will list the commits which match author, 228# committer or commit text to a given string. Enabled by default. 229# Project specific override is not supported. 230'search'=> { 231'override'=>0, 232'default'=> [1]}, 233 234# Enable grep search, which will list the files in currently selected 235# tree containing the given string. Enabled by default. This can be 236# potentially CPU-intensive, of course. 237 238# To enable system wide have in $GITWEB_CONFIG 239# $feature{'grep'}{'default'} = [1]; 240# To have project specific config enable override in $GITWEB_CONFIG 241# $feature{'grep'}{'override'} = 1; 242# and in project config gitweb.grep = 0|1; 243'grep'=> { 244'sub'=>sub{ feature_bool('grep',@_) }, 245'override'=>0, 246'default'=> [1]}, 247 248# Enable the pickaxe search, which will list the commits that modified 249# a given string in a file. This can be practical and quite faster 250# alternative to 'blame', but still potentially CPU-intensive. 251 252# To enable system wide have in $GITWEB_CONFIG 253# $feature{'pickaxe'}{'default'} = [1]; 254# To have project specific config enable override in $GITWEB_CONFIG 255# $feature{'pickaxe'}{'override'} = 1; 256# and in project config gitweb.pickaxe = 0|1; 257'pickaxe'=> { 258'sub'=>sub{ feature_bool('pickaxe',@_) }, 259'override'=>0, 260'default'=> [1]}, 261 262# Make gitweb use an alternative format of the URLs which can be 263# more readable and natural-looking: project name is embedded 264# directly in the path and the query string contains other 265# auxiliary information. All gitweb installations recognize 266# URL in either format; this configures in which formats gitweb 267# generates links. 268 269# To enable system wide have in $GITWEB_CONFIG 270# $feature{'pathinfo'}{'default'} = [1]; 271# Project specific override is not supported. 272 273# Note that you will need to change the default location of CSS, 274# favicon, logo and possibly other files to an absolute URL. Also, 275# if gitweb.cgi serves as your indexfile, you will need to force 276# $my_uri to contain the script name in your $GITWEB_CONFIG. 277'pathinfo'=> { 278'override'=>0, 279'default'=> [0]}, 280 281# Make gitweb consider projects in project root subdirectories 282# to be forks of existing projects. Given project $projname.git, 283# projects matching $projname/*.git will not be shown in the main 284# projects list, instead a '+' mark will be added to $projname 285# there and a 'forks' view will be enabled for the project, listing 286# all the forks. If project list is taken from a file, forks have 287# to be listed after the main project. 288 289# To enable system wide have in $GITWEB_CONFIG 290# $feature{'forks'}{'default'} = [1]; 291# Project specific override is not supported. 292'forks'=> { 293'override'=>0, 294'default'=> [0]}, 295 296# Insert custom links to the action bar of all project pages. 297# This enables you mainly to link to third-party scripts integrating 298# into gitweb; e.g. git-browser for graphical history representation 299# or custom web-based repository administration interface. 300 301# The 'default' value consists of a list of triplets in the form 302# (label, link, position) where position is the label after which 303# to insert the link and link is a format string where %n expands 304# to the project name, %f to the project path within the filesystem, 305# %h to the current hash (h gitweb parameter) and %b to the current 306# hash base (hb gitweb parameter); %% expands to %. 307 308# To enable system wide have in $GITWEB_CONFIG e.g. 309# $feature{'actions'}{'default'} = [('graphiclog', 310# '/git-browser/by-commit.html?r=%n', 'summary')]; 311# Project specific override is not supported. 312'actions'=> { 313'override'=>0, 314'default'=> []}, 315 316# Allow gitweb scan project content tags described in ctags/ 317# of project repository, and display the popular Web 2.0-ish 318# "tag cloud" near the project list. Note that this is something 319# COMPLETELY different from the normal Git tags. 320 321# gitweb by itself can show existing tags, but it does not handle 322# tagging itself; you need an external application for that. 323# For an example script, check Girocco's cgi/tagproj.cgi. 324# You may want to install the HTML::TagCloud Perl module to get 325# a pretty tag cloud instead of just a list of tags. 326 327# To enable system wide have in $GITWEB_CONFIG 328# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 329# Project specific override is not supported. 330'ctags'=> { 331'override'=>0, 332'default'=> [0]}, 333); 334 335sub gitweb_get_feature { 336my($name) =@_; 337return unlessexists$feature{$name}; 338my($sub,$override,@defaults) = ( 339$feature{$name}{'sub'}, 340$feature{$name}{'override'}, 341@{$feature{$name}{'default'}}); 342if(!$override) {return@defaults; } 343if(!defined$sub) { 344warn"feature$nameis not overrideable"; 345return@defaults; 346} 347return$sub->(@defaults); 348} 349 350# A wrapper to check if a given feature is enabled. 351# With this, you can say 352# 353# my $bool_feat = gitweb_check_feature('bool_feat'); 354# gitweb_check_feature('bool_feat') or somecode; 355# 356# instead of 357# 358# my ($bool_feat) = gitweb_get_feature('bool_feat'); 359# (gitweb_get_feature('bool_feat'))[0] or somecode; 360# 361sub gitweb_check_feature { 362return(gitweb_get_feature(@_))[0]; 363} 364 365 366sub feature_bool { 367my$key=shift; 368my($val) = git_get_project_config($key,'--bool'); 369 370if($valeq'true') { 371return(1); 372}elsif($valeq'false') { 373return(0); 374} 375 376return($_[0]); 377} 378 379sub feature_snapshot { 380my(@fmts) =@_; 381 382my($val) = git_get_project_config('snapshot'); 383 384if($val) { 385@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 386} 387 388return@fmts; 389} 390 391# checking HEAD file with -e is fragile if the repository was 392# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 393# and then pruned. 394sub check_head_link { 395my($dir) =@_; 396my$headfile="$dir/HEAD"; 397return((-e $headfile) || 398(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 399} 400 401sub check_export_ok { 402my($dir) =@_; 403return(check_head_link($dir) && 404(!$export_ok|| -e "$dir/$export_ok") && 405(!$export_auth_hook||$export_auth_hook->($dir))); 406} 407 408# process alternate names for backward compatibility 409# filter out unsupported (unknown) snapshot formats 410sub filter_snapshot_fmts { 411my@fmts=@_; 412 413@fmts=map{ 414exists$known_snapshot_format_aliases{$_} ? 415$known_snapshot_format_aliases{$_} :$_}@fmts; 416@fmts=grep(exists$known_snapshot_formats{$_},@fmts); 417 418} 419 420our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 421if(-e $GITWEB_CONFIG) { 422do$GITWEB_CONFIG; 423}else{ 424our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 425do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 426} 427 428# version of the core git binary 429our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 430 431$projects_list||=$projectroot; 432 433# ====================================================================== 434# input validation and dispatch 435 436# input parameters can be collected from a variety of sources (presently, CGI 437# and PATH_INFO), so we define an %input_params hash that collects them all 438# together during validation: this allows subsequent uses (e.g. href()) to be 439# agnostic of the parameter origin 440 441our%input_params= (); 442 443# input parameters are stored with the long parameter name as key. This will 444# also be used in the href subroutine to convert parameters to their CGI 445# equivalent, and since the href() usage is the most frequent one, we store 446# the name -> CGI key mapping here, instead of the reverse. 447# 448# XXX: Warning: If you touch this, check the search form for updating, 449# too. 450 451our@cgi_param_mapping= ( 452 project =>"p", 453 action =>"a", 454 file_name =>"f", 455 file_parent =>"fp", 456 hash =>"h", 457 hash_parent =>"hp", 458 hash_base =>"hb", 459 hash_parent_base =>"hpb", 460 page =>"pg", 461 order =>"o", 462 searchtext =>"s", 463 searchtype =>"st", 464 snapshot_format =>"sf", 465 extra_options =>"opt", 466 search_use_regexp =>"sr", 467); 468our%cgi_param_mapping=@cgi_param_mapping; 469 470# we will also need to know the possible actions, for validation 471our%actions= ( 472"blame"=> \&git_blame, 473"blobdiff"=> \&git_blobdiff, 474"blobdiff_plain"=> \&git_blobdiff_plain, 475"blob"=> \&git_blob, 476"blob_plain"=> \&git_blob_plain, 477"commitdiff"=> \&git_commitdiff, 478"commitdiff_plain"=> \&git_commitdiff_plain, 479"commit"=> \&git_commit, 480"forks"=> \&git_forks, 481"heads"=> \&git_heads, 482"history"=> \&git_history, 483"log"=> \&git_log, 484"rss"=> \&git_rss, 485"atom"=> \&git_atom, 486"search"=> \&git_search, 487"search_help"=> \&git_search_help, 488"shortlog"=> \&git_shortlog, 489"summary"=> \&git_summary, 490"tag"=> \&git_tag, 491"tags"=> \&git_tags, 492"tree"=> \&git_tree, 493"snapshot"=> \&git_snapshot, 494"object"=> \&git_object, 495# those below don't need $project 496"opml"=> \&git_opml, 497"project_list"=> \&git_project_list, 498"project_index"=> \&git_project_index, 499); 500 501# finally, we have the hash of allowed extra_options for the commands that 502# allow them 503our%allowed_options= ( 504"--no-merges"=> [qw(rss atom log shortlog history)], 505); 506 507# fill %input_params with the CGI parameters. All values except for 'opt' 508# should be single values, but opt can be an array. We should probably 509# build an array of parameters that can be multi-valued, but since for the time 510# being it's only this one, we just single it out 511while(my($name,$symbol) =each%cgi_param_mapping) { 512if($symboleq'opt') { 513$input_params{$name} = [$cgi->param($symbol) ]; 514}else{ 515$input_params{$name} =$cgi->param($symbol); 516} 517} 518 519# now read PATH_INFO and update the parameter list for missing parameters 520sub evaluate_path_info { 521return ifdefined$input_params{'project'}; 522return if!$path_info; 523$path_info=~ s,^/+,,; 524return if!$path_info; 525 526# find which part of PATH_INFO is project 527my$project=$path_info; 528$project=~ s,/+$,,; 529while($project&& !check_head_link("$projectroot/$project")) { 530$project=~ s,/*[^/]*$,,; 531} 532return unless$project; 533$input_params{'project'} =$project; 534 535# do not change any parameters if an action is given using the query string 536return if$input_params{'action'}; 537$path_info=~ s,^\Q$project\E/*,,; 538 539# next, check if we have an action 540my$action=$path_info; 541$action=~ s,/.*$,,; 542if(exists$actions{$action}) { 543$path_info=~ s,^$action/*,,; 544$input_params{'action'} =$action; 545} 546 547# list of actions that want hash_base instead of hash, but can have no 548# pathname (f) parameter 549my@wants_base= ( 550'tree', 551'history', 552); 553 554# we want to catch 555# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 556my($parentrefname,$parentpathname,$refname,$pathname) = 557($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 558 559# first, analyze the 'current' part 560if(defined$pathname) { 561# we got "branch:filename" or "branch:dir/" 562# we could use git_get_type(branch:pathname), but: 563# - it needs $git_dir 564# - it does a git() call 565# - the convention of terminating directories with a slash 566# makes it superfluous 567# - embedding the action in the PATH_INFO would make it even 568# more superfluous 569$pathname=~ s,^/+,,; 570if(!$pathname||substr($pathname, -1)eq"/") { 571$input_params{'action'} ||="tree"; 572$pathname=~ s,/$,,; 573}else{ 574# the default action depends on whether we had parent info 575# or not 576if($parentrefname) { 577$input_params{'action'} ||="blobdiff_plain"; 578}else{ 579$input_params{'action'} ||="blob_plain"; 580} 581} 582$input_params{'hash_base'} ||=$refname; 583$input_params{'file_name'} ||=$pathname; 584}elsif(defined$refname) { 585# we got "branch". In this case we have to choose if we have to 586# set hash or hash_base. 587# 588# Most of the actions without a pathname only want hash to be 589# set, except for the ones specified in @wants_base that want 590# hash_base instead. It should also be noted that hand-crafted 591# links having 'history' as an action and no pathname or hash 592# set will fail, but that happens regardless of PATH_INFO. 593$input_params{'action'} ||="shortlog"; 594if(grep{$_eq$input_params{'action'} }@wants_base) { 595$input_params{'hash_base'} ||=$refname; 596}else{ 597$input_params{'hash'} ||=$refname; 598} 599} 600 601# next, handle the 'parent' part, if present 602if(defined$parentrefname) { 603# a missing pathspec defaults to the 'current' filename, allowing e.g. 604# someproject/blobdiff/oldrev..newrev:/filename 605if($parentpathname) { 606$parentpathname=~ s,^/+,,; 607$parentpathname=~ s,/$,,; 608$input_params{'file_parent'} ||=$parentpathname; 609}else{ 610$input_params{'file_parent'} ||=$input_params{'file_name'}; 611} 612# we assume that hash_parent_base is wanted if a path was specified, 613# or if the action wants hash_base instead of hash 614if(defined$input_params{'file_parent'} || 615grep{$_eq$input_params{'action'} }@wants_base) { 616$input_params{'hash_parent_base'} ||=$parentrefname; 617}else{ 618$input_params{'hash_parent'} ||=$parentrefname; 619} 620} 621 622# for the snapshot action, we allow URLs in the form 623# $project/snapshot/$hash.ext 624# where .ext determines the snapshot and gets removed from the 625# passed $refname to provide the $hash. 626# 627# To be able to tell that $refname includes the format extension, we 628# require the following two conditions to be satisfied: 629# - the hash input parameter MUST have been set from the $refname part 630# of the URL (i.e. they must be equal) 631# - the snapshot format MUST NOT have been defined already (e.g. from 632# CGI parameter sf) 633# It's also useless to try any matching unless $refname has a dot, 634# so we check for that too 635if(defined$input_params{'action'} && 636$input_params{'action'}eq'snapshot'&& 637defined$refname&&index($refname,'.') != -1&& 638$refnameeq$input_params{'hash'} && 639!defined$input_params{'snapshot_format'}) { 640# We loop over the known snapshot formats, checking for 641# extensions. Allowed extensions are both the defined suffix 642# (which includes the initial dot already) and the snapshot 643# format key itself, with a prepended dot 644while(my($fmt,%opt) =each%known_snapshot_formats) { 645my$hash=$refname; 646my$sfx; 647$hash=~s/(\Q$opt{'suffix'}\E|\Q.$fmt\E)$//; 648next unless$sfx=$1; 649# a valid suffix was found, so set the snapshot format 650# and reset the hash parameter 651$input_params{'snapshot_format'} =$fmt; 652$input_params{'hash'} =$hash; 653# we also set the format suffix to the one requested 654# in the URL: this way a request for e.g. .tgz returns 655# a .tgz instead of a .tar.gz 656$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 657last; 658} 659} 660} 661evaluate_path_info(); 662 663our$action=$input_params{'action'}; 664if(defined$action) { 665if(!validate_action($action)) { 666 die_error(400,"Invalid action parameter"); 667} 668} 669 670# parameters which are pathnames 671our$project=$input_params{'project'}; 672if(defined$project) { 673if(!validate_project($project)) { 674undef$project; 675 die_error(404,"No such project"); 676} 677} 678 679our$file_name=$input_params{'file_name'}; 680if(defined$file_name) { 681if(!validate_pathname($file_name)) { 682 die_error(400,"Invalid file parameter"); 683} 684} 685 686our$file_parent=$input_params{'file_parent'}; 687if(defined$file_parent) { 688if(!validate_pathname($file_parent)) { 689 die_error(400,"Invalid file parent parameter"); 690} 691} 692 693# parameters which are refnames 694our$hash=$input_params{'hash'}; 695if(defined$hash) { 696if(!validate_refname($hash)) { 697 die_error(400,"Invalid hash parameter"); 698} 699} 700 701our$hash_parent=$input_params{'hash_parent'}; 702if(defined$hash_parent) { 703if(!validate_refname($hash_parent)) { 704 die_error(400,"Invalid hash parent parameter"); 705} 706} 707 708our$hash_base=$input_params{'hash_base'}; 709if(defined$hash_base) { 710if(!validate_refname($hash_base)) { 711 die_error(400,"Invalid hash base parameter"); 712} 713} 714 715our@extra_options= @{$input_params{'extra_options'}}; 716# @extra_options is always defined, since it can only be (currently) set from 717# CGI, and $cgi->param() returns the empty array in array context if the param 718# is not set 719foreachmy$opt(@extra_options) { 720if(not exists$allowed_options{$opt}) { 721 die_error(400,"Invalid option parameter"); 722} 723if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 724 die_error(400,"Invalid option parameter for this action"); 725} 726} 727 728our$hash_parent_base=$input_params{'hash_parent_base'}; 729if(defined$hash_parent_base) { 730if(!validate_refname($hash_parent_base)) { 731 die_error(400,"Invalid hash parent base parameter"); 732} 733} 734 735# other parameters 736our$page=$input_params{'page'}; 737if(defined$page) { 738if($page=~m/[^0-9]/) { 739 die_error(400,"Invalid page parameter"); 740} 741} 742 743our$searchtype=$input_params{'searchtype'}; 744if(defined$searchtype) { 745if($searchtype=~m/[^a-z]/) { 746 die_error(400,"Invalid searchtype parameter"); 747} 748} 749 750our$search_use_regexp=$input_params{'search_use_regexp'}; 751 752our$searchtext=$input_params{'searchtext'}; 753our$search_regexp; 754if(defined$searchtext) { 755if(length($searchtext) <2) { 756 die_error(403,"At least two characters are required for search parameter"); 757} 758$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 759} 760 761# path to the current git repository 762our$git_dir; 763$git_dir="$projectroot/$project"if$project; 764 765# list of supported snapshot formats 766our@snapshot_fmts= gitweb_get_feature('snapshot'); 767@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 768 769# dispatch 770if(!defined$action) { 771if(defined$hash) { 772$action= git_get_type($hash); 773}elsif(defined$hash_base&&defined$file_name) { 774$action= git_get_type("$hash_base:$file_name"); 775}elsif(defined$project) { 776$action='summary'; 777}else{ 778$action='project_list'; 779} 780} 781if(!defined($actions{$action})) { 782 die_error(400,"Unknown action"); 783} 784if($action!~m/^(opml|project_list|project_index)$/&& 785!$project) { 786 die_error(400,"Project needed"); 787} 788$actions{$action}->(); 789exit; 790 791## ====================================================================== 792## action links 793 794sub href (%) { 795my%params=@_; 796# default is to use -absolute url() i.e. $my_uri 797my$href=$params{-full} ?$my_url:$my_uri; 798 799$params{'project'} =$projectunlessexists$params{'project'}; 800 801if($params{-replay}) { 802while(my($name,$symbol) =each%cgi_param_mapping) { 803if(!exists$params{$name}) { 804$params{$name} =$input_params{$name}; 805} 806} 807} 808 809my$use_pathinfo= gitweb_check_feature('pathinfo'); 810if($use_pathinfo) { 811# try to put as many parameters as possible in PATH_INFO: 812# - project name 813# - action 814# - hash_parent or hash_parent_base:/file_parent 815# - hash or hash_base:/filename 816# - the snapshot_format as an appropriate suffix 817 818# When the script is the root DirectoryIndex for the domain, 819# $href here would be something like http://gitweb.example.com/ 820# Thus, we strip any trailing / from $href, to spare us double 821# slashes in the final URL 822$href=~ s,/$,,; 823 824# Then add the project name, if present 825$href.="/".esc_url($params{'project'})ifdefined$params{'project'}; 826delete$params{'project'}; 827 828# since we destructively absorb parameters, we keep this 829# boolean that remembers if we're handling a snapshot 830my$is_snapshot=$params{'action'}eq'snapshot'; 831 832# Summary just uses the project path URL, any other action is 833# added to the URL 834if(defined$params{'action'}) { 835$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 836delete$params{'action'}; 837} 838 839# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 840# stripping nonexistent or useless pieces 841$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 842||$params{'hash_parent'} ||$params{'hash'}); 843if(defined$params{'hash_base'}) { 844if(defined$params{'hash_parent_base'}) { 845$href.= esc_url($params{'hash_parent_base'}); 846# skip the file_parent if it's the same as the file_name 847delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 848if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 849$href.=":/".esc_url($params{'file_parent'}); 850delete$params{'file_parent'}; 851} 852$href.=".."; 853delete$params{'hash_parent'}; 854delete$params{'hash_parent_base'}; 855}elsif(defined$params{'hash_parent'}) { 856$href.= esc_url($params{'hash_parent'}).".."; 857delete$params{'hash_parent'}; 858} 859 860$href.= esc_url($params{'hash_base'}); 861if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 862$href.=":/".esc_url($params{'file_name'}); 863delete$params{'file_name'}; 864} 865delete$params{'hash'}; 866delete$params{'hash_base'}; 867}elsif(defined$params{'hash'}) { 868$href.= esc_url($params{'hash'}); 869delete$params{'hash'}; 870} 871 872# If the action was a snapshot, we can absorb the 873# snapshot_format parameter too 874if($is_snapshot) { 875my$fmt=$params{'snapshot_format'}; 876# snapshot_format should always be defined when href() 877# is called, but just in case some code forgets, we 878# fall back to the default 879$fmt||=$snapshot_fmts[0]; 880$href.=$known_snapshot_formats{$fmt}{'suffix'}; 881delete$params{'snapshot_format'}; 882} 883} 884 885# now encode the parameters explicitly 886my@result= (); 887for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 888my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 889if(defined$params{$name}) { 890if(ref($params{$name})eq"ARRAY") { 891foreachmy$par(@{$params{$name}}) { 892push@result,$symbol."=". esc_param($par); 893} 894}else{ 895push@result,$symbol."=". esc_param($params{$name}); 896} 897} 898} 899$href.="?".join(';',@result)ifscalar@result; 900 901return$href; 902} 903 904 905## ====================================================================== 906## validation, quoting/unquoting and escaping 907 908sub validate_action { 909my$input=shift||returnundef; 910returnundefunlessexists$actions{$input}; 911return$input; 912} 913 914sub validate_project { 915my$input=shift||returnundef; 916if(!validate_pathname($input) || 917!(-d "$projectroot/$input") || 918!check_export_ok("$projectroot/$input") || 919($strict_export&& !project_in_list($input))) { 920returnundef; 921}else{ 922return$input; 923} 924} 925 926sub validate_pathname { 927my$input=shift||returnundef; 928 929# no '.' or '..' as elements of path, i.e. no '.' nor '..' 930# at the beginning, at the end, and between slashes. 931# also this catches doubled slashes 932if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 933returnundef; 934} 935# no null characters 936if($input=~m!\0!) { 937returnundef; 938} 939return$input; 940} 941 942sub validate_refname { 943my$input=shift||returnundef; 944 945# textual hashes are O.K. 946if($input=~m/^[0-9a-fA-F]{40}$/) { 947return$input; 948} 949# it must be correct pathname 950$input= validate_pathname($input) 951orreturnundef; 952# restrictions on ref name according to git-check-ref-format 953if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { 954returnundef; 955} 956return$input; 957} 958 959# decode sequences of octets in utf8 into Perl's internal form, 960# which is utf-8 with utf8 flag set if needed. gitweb writes out 961# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning 962sub to_utf8 { 963my$str=shift; 964if(utf8::valid($str)) { 965 utf8::decode($str); 966return$str; 967}else{ 968return decode($fallback_encoding,$str, Encode::FB_DEFAULT); 969} 970} 971 972# quote unsafe chars, but keep the slash, even when it's not 973# correct, but quoted slashes look too horrible in bookmarks 974sub esc_param { 975my$str=shift; 976$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg; 977$str=~s/\+/%2B/g; 978$str=~s/ /\+/g; 979return$str; 980} 981 982# quote unsafe chars in whole URL, so some charactrs cannot be quoted 983sub esc_url { 984my$str=shift; 985$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg; 986$str=~s/\+/%2B/g; 987$str=~s/ /\+/g; 988return$str; 989} 990 991# replace invalid utf8 character with SUBSTITUTION sequence 992sub esc_html ($;%) { 993my$str=shift; 994my%opts=@_; 995 996$str= to_utf8($str); 997$str=$cgi->escapeHTML($str); 998if($opts{'-nbsp'}) { 999$str=~s/ / /g;1000}1001$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1002return$str;1003}10041005# quote control characters and escape filename to HTML1006sub esc_path {1007my$str=shift;1008my%opts=@_;10091010$str= to_utf8($str);1011$str=$cgi->escapeHTML($str);1012if($opts{'-nbsp'}) {1013$str=~s/ / /g;1014}1015$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1016return$str;1017}10181019# Make control characters "printable", using character escape codes (CEC)1020sub quot_cec {1021my$cntrl=shift;1022my%opts=@_;1023my%es= (# character escape codes, aka escape sequences1024"\t"=>'\t',# tab (HT)1025"\n"=>'\n',# line feed (LF)1026"\r"=>'\r',# carrige return (CR)1027"\f"=>'\f',# form feed (FF)1028"\b"=>'\b',# backspace (BS)1029"\a"=>'\a',# alarm (bell) (BEL)1030"\e"=>'\e',# escape (ESC)1031"\013"=>'\v',# vertical tab (VT)1032"\000"=>'\0',# nul character (NUL)1033);1034my$chr= ( (exists$es{$cntrl})1035?$es{$cntrl}1036:sprintf('\%2x',ord($cntrl)) );1037if($opts{-nohtml}) {1038return$chr;1039}else{1040return"<span class=\"cntrl\">$chr</span>";1041}1042}10431044# Alternatively use unicode control pictures codepoints,1045# Unicode "printable representation" (PR)1046sub quot_upr {1047my$cntrl=shift;1048my%opts=@_;10491050my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1051if($opts{-nohtml}) {1052return$chr;1053}else{1054return"<span class=\"cntrl\">$chr</span>";1055}1056}10571058# git may return quoted and escaped filenames1059sub unquote {1060my$str=shift;10611062sub unq {1063my$seq=shift;1064my%es= (# character escape codes, aka escape sequences1065't'=>"\t",# tab (HT, TAB)1066'n'=>"\n",# newline (NL)1067'r'=>"\r",# return (CR)1068'f'=>"\f",# form feed (FF)1069'b'=>"\b",# backspace (BS)1070'a'=>"\a",# alarm (bell) (BEL)1071'e'=>"\e",# escape (ESC)1072'v'=>"\013",# vertical tab (VT)1073);10741075if($seq=~m/^[0-7]{1,3}$/) {1076# octal char sequence1077returnchr(oct($seq));1078}elsif(exists$es{$seq}) {1079# C escape sequence, aka character escape code1080return$es{$seq};1081}1082# quoted ordinary character1083return$seq;1084}10851086if($str=~m/^"(.*)"$/) {1087# needs unquoting1088$str=$1;1089$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1090}1091return$str;1092}10931094# escape tabs (convert tabs to spaces)1095sub untabify {1096my$line=shift;10971098while((my$pos=index($line,"\t")) != -1) {1099if(my$count= (8- ($pos%8))) {1100my$spaces=' ' x $count;1101$line=~s/\t/$spaces/;1102}1103}11041105return$line;1106}11071108sub project_in_list {1109my$project=shift;1110my@list= git_get_projects_list();1111return@list&&scalar(grep{$_->{'path'}eq$project}@list);1112}11131114## ----------------------------------------------------------------------1115## HTML aware string manipulation11161117# Try to chop given string on a word boundary between position1118# $len and $len+$add_len. If there is no word boundary there,1119# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1120# (marking chopped part) would be longer than given string.1121sub chop_str {1122my$str=shift;1123my$len=shift;1124my$add_len=shift||10;1125my$where=shift||'right';# 'left' | 'center' | 'right'11261127# Make sure perl knows it is utf8 encoded so we don't1128# cut in the middle of a utf8 multibyte char.1129$str= to_utf8($str);11301131# allow only $len chars, but don't cut a word if it would fit in $add_len1132# if it doesn't fit, cut it if it's still longer than the dots we would add1133# remove chopped character entities entirely11341135# when chopping in the middle, distribute $len into left and right part1136# return early if chopping wouldn't make string shorter1137if($whereeq'center') {1138return$strif($len+5>=length($str));# filler is length 51139$len=int($len/2);1140}else{1141return$strif($len+4>=length($str));# filler is length 41142}11431144# regexps: ending and beginning with word part up to $add_len1145my$endre=qr/.{$len}\w{0,$add_len}/;1146my$begre=qr/\w{0,$add_len}.{$len}/;11471148if($whereeq'left') {1149$str=~m/^(.*?)($begre)$/;1150my($lead,$body) = ($1,$2);1151if(length($lead) >4) {1152$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1153$lead=" ...";1154}1155return"$lead$body";11561157}elsif($whereeq'center') {1158$str=~m/^($endre)(.*)$/;1159my($left,$str) = ($1,$2);1160$str=~m/^(.*?)($begre)$/;1161my($mid,$right) = ($1,$2);1162if(length($mid) >5) {1163$left=~s/&[^;]*$//;1164$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1165$mid=" ... ";1166}1167return"$left$mid$right";11681169}else{1170$str=~m/^($endre)(.*)$/;1171my$body=$1;1172my$tail=$2;1173if(length($tail) >4) {1174$body=~s/&[^;]*$//;1175$tail="... ";1176}1177return"$body$tail";1178}1179}11801181# takes the same arguments as chop_str, but also wraps a <span> around the1182# result with a title attribute if it does get chopped. Additionally, the1183# string is HTML-escaped.1184sub chop_and_escape_str {1185my($str) =@_;11861187my$chopped= chop_str(@_);1188if($choppedeq$str) {1189return esc_html($chopped);1190}else{1191$str=~s/([[:cntrl:]])/?/g;1192return$cgi->span({-title=>$str}, esc_html($chopped));1193}1194}11951196## ----------------------------------------------------------------------1197## functions returning short strings11981199# CSS class for given age value (in seconds)1200sub age_class {1201my$age=shift;12021203if(!defined$age) {1204return"noage";1205}elsif($age<60*60*2) {1206return"age0";1207}elsif($age<60*60*24*2) {1208return"age1";1209}else{1210return"age2";1211}1212}12131214# convert age in seconds to "nn units ago" string1215sub age_string {1216my$age=shift;1217my$age_str;12181219if($age>60*60*24*365*2) {1220$age_str= (int$age/60/60/24/365);1221$age_str.=" years ago";1222}elsif($age>60*60*24*(365/12)*2) {1223$age_str=int$age/60/60/24/(365/12);1224$age_str.=" months ago";1225}elsif($age>60*60*24*7*2) {1226$age_str=int$age/60/60/24/7;1227$age_str.=" weeks ago";1228}elsif($age>60*60*24*2) {1229$age_str=int$age/60/60/24;1230$age_str.=" days ago";1231}elsif($age>60*60*2) {1232$age_str=int$age/60/60;1233$age_str.=" hours ago";1234}elsif($age>60*2) {1235$age_str=int$age/60;1236$age_str.=" min ago";1237}elsif($age>2) {1238$age_str=int$age;1239$age_str.=" sec ago";1240}else{1241$age_str.=" right now";1242}1243return$age_str;1244}12451246useconstant{1247 S_IFINVALID =>0030000,1248 S_IFGITLINK =>0160000,1249};12501251# submodule/subproject, a commit object reference1252sub S_ISGITLINK($) {1253my$mode=shift;12541255return(($mode& S_IFMT) == S_IFGITLINK)1256}12571258# convert file mode in octal to symbolic file mode string1259sub mode_str {1260my$mode=oct shift;12611262if(S_ISGITLINK($mode)) {1263return'm---------';1264}elsif(S_ISDIR($mode& S_IFMT)) {1265return'drwxr-xr-x';1266}elsif(S_ISLNK($mode)) {1267return'lrwxrwxrwx';1268}elsif(S_ISREG($mode)) {1269# git cares only about the executable bit1270if($mode& S_IXUSR) {1271return'-rwxr-xr-x';1272}else{1273return'-rw-r--r--';1274};1275}else{1276return'----------';1277}1278}12791280# convert file mode in octal to file type string1281sub file_type {1282my$mode=shift;12831284if($mode!~m/^[0-7]+$/) {1285return$mode;1286}else{1287$mode=oct$mode;1288}12891290if(S_ISGITLINK($mode)) {1291return"submodule";1292}elsif(S_ISDIR($mode& S_IFMT)) {1293return"directory";1294}elsif(S_ISLNK($mode)) {1295return"symlink";1296}elsif(S_ISREG($mode)) {1297return"file";1298}else{1299return"unknown";1300}1301}13021303# convert file mode in octal to file type description string1304sub file_type_long {1305my$mode=shift;13061307if($mode!~m/^[0-7]+$/) {1308return$mode;1309}else{1310$mode=oct$mode;1311}13121313if(S_ISGITLINK($mode)) {1314return"submodule";1315}elsif(S_ISDIR($mode& S_IFMT)) {1316return"directory";1317}elsif(S_ISLNK($mode)) {1318return"symlink";1319}elsif(S_ISREG($mode)) {1320if($mode& S_IXUSR) {1321return"executable";1322}else{1323return"file";1324};1325}else{1326return"unknown";1327}1328}132913301331## ----------------------------------------------------------------------1332## functions returning short HTML fragments, or transforming HTML fragments1333## which don't belong to other sections13341335# format line of commit message.1336sub format_log_line_html {1337my$line=shift;13381339$line= esc_html($line, -nbsp=>1);1340if($line=~m/([0-9a-fA-F]{8,40})/) {1341my$hash_text=$1;1342my$link=1343$cgi->a({-href => href(action=>"object", hash=>$hash_text),1344-class=>"text"},$hash_text);1345$line=~s/$hash_text/$link/;1346}1347return$line;1348}13491350# format marker of refs pointing to given object13511352# the destination action is chosen based on object type and current context:1353# - for annotated tags, we choose the tag view unless it's the current view1354# already, in which case we go to shortlog view1355# - for other refs, we keep the current view if we're in history, shortlog or1356# log view, and select shortlog otherwise1357sub format_ref_marker {1358my($refs,$id) =@_;1359my$markers='';13601361if(defined$refs->{$id}) {1362foreachmy$ref(@{$refs->{$id}}) {1363# this code exploits the fact that non-lightweight tags are the1364# only indirect objects, and that they are the only objects for which1365# we want to use tag instead of shortlog as action1366my($type,$name) =qw();1367my$indirect= ($ref=~s/\^\{\}$//);1368# e.g. tags/v2.6.11 or heads/next1369if($ref=~m!^(.*?)s?/(.*)$!) {1370$type=$1;1371$name=$2;1372}else{1373$type="ref";1374$name=$ref;1375}13761377my$class=$type;1378$class.=" indirect"if$indirect;13791380my$dest_action="shortlog";13811382if($indirect) {1383$dest_action="tag"unless$actioneq"tag";1384}elsif($action=~/^(history|(short)?log)$/) {1385$dest_action=$action;1386}13871388my$dest="";1389$dest.="refs/"unless$ref=~ m!^refs/!;1390$dest.=$ref;13911392my$link=$cgi->a({1393-href => href(1394 action=>$dest_action,1395 hash=>$dest1396)},$name);13971398$markers.=" <span class=\"$class\"title=\"$ref\">".1399$link."</span>";1400}1401}14021403if($markers) {1404return' <span class="refs">'.$markers.'</span>';1405}else{1406return"";1407}1408}14091410# format, perhaps shortened and with markers, title line1411sub format_subject_html {1412my($long,$short,$href,$extra) =@_;1413$extra=''unlessdefined($extra);14141415if(length($short) <length($long)) {1416return$cgi->a({-href =>$href, -class=>"list subject",1417-title => to_utf8($long)},1418 esc_html($short) .$extra);1419}else{1420return$cgi->a({-href =>$href, -class=>"list subject"},1421 esc_html($long) .$extra);1422}1423}14241425# format git diff header line, i.e. "diff --(git|combined|cc) ..."1426sub format_git_diff_header_line {1427my$line=shift;1428my$diffinfo=shift;1429my($from,$to) =@_;14301431if($diffinfo->{'nparents'}) {1432# combined diff1433$line=~s!^(diff (.*?) )"?.*$!$1!;1434if($to->{'href'}) {1435$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1436 esc_path($to->{'file'}));1437}else{# file was deleted (no href)1438$line.= esc_path($to->{'file'});1439}1440}else{1441# "ordinary" diff1442$line=~s!^(diff (.*?) )"?a/.*$!$1!;1443if($from->{'href'}) {1444$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1445'a/'. esc_path($from->{'file'}));1446}else{# file was added (no href)1447$line.='a/'. esc_path($from->{'file'});1448}1449$line.=' ';1450if($to->{'href'}) {1451$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1452'b/'. esc_path($to->{'file'}));1453}else{# file was deleted1454$line.='b/'. esc_path($to->{'file'});1455}1456}14571458return"<div class=\"diff header\">$line</div>\n";1459}14601461# format extended diff header line, before patch itself1462sub format_extended_diff_header_line {1463my$line=shift;1464my$diffinfo=shift;1465my($from,$to) =@_;14661467# match <path>1468if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1469$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1470 esc_path($from->{'file'}));1471}1472if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1473$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1474 esc_path($to->{'file'}));1475}1476# match single <mode>1477if($line=~m/\s(\d{6})$/) {1478$line.='<span class="info"> ('.1479 file_type_long($1) .1480')</span>';1481}1482# match <hash>1483if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1484# can match only for combined diff1485$line='index ';1486for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1487if($from->{'href'}[$i]) {1488$line.=$cgi->a({-href=>$from->{'href'}[$i],1489-class=>"hash"},1490substr($diffinfo->{'from_id'}[$i],0,7));1491}else{1492$line.='0' x 7;1493}1494# separator1495$line.=','if($i<$diffinfo->{'nparents'} -1);1496}1497$line.='..';1498if($to->{'href'}) {1499$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1500substr($diffinfo->{'to_id'},0,7));1501}else{1502$line.='0' x 7;1503}15041505}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1506# can match only for ordinary diff1507my($from_link,$to_link);1508if($from->{'href'}) {1509$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1510substr($diffinfo->{'from_id'},0,7));1511}else{1512$from_link='0' x 7;1513}1514if($to->{'href'}) {1515$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1516substr($diffinfo->{'to_id'},0,7));1517}else{1518$to_link='0' x 7;1519}1520my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1521$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1522}15231524return$line."<br/>\n";1525}15261527# format from-file/to-file diff header1528sub format_diff_from_to_header {1529my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1530my$line;1531my$result='';15321533$line=$from_line;1534#assert($line =~ m/^---/) if DEBUG;1535# no extra formatting for "^--- /dev/null"1536if(!$diffinfo->{'nparents'}) {1537# ordinary (single parent) diff1538if($line=~m!^--- "?a/!) {1539if($from->{'href'}) {1540$line='--- a/'.1541$cgi->a({-href=>$from->{'href'}, -class=>"path"},1542 esc_path($from->{'file'}));1543}else{1544$line='--- a/'.1545 esc_path($from->{'file'});1546}1547}1548$result.= qq!<div class="diff from_file">$line</div>\n!;15491550}else{1551# combined diff (merge commit)1552for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1553if($from->{'href'}[$i]) {1554$line='--- '.1555$cgi->a({-href=>href(action=>"blobdiff",1556 hash_parent=>$diffinfo->{'from_id'}[$i],1557 hash_parent_base=>$parents[$i],1558 file_parent=>$from->{'file'}[$i],1559 hash=>$diffinfo->{'to_id'},1560 hash_base=>$hash,1561 file_name=>$to->{'file'}),1562-class=>"path",1563-title=>"diff". ($i+1)},1564$i+1) .1565'/'.1566$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1567 esc_path($from->{'file'}[$i]));1568}else{1569$line='--- /dev/null';1570}1571$result.= qq!<div class="diff from_file">$line</div>\n!;1572}1573}15741575$line=$to_line;1576#assert($line =~ m/^\+\+\+/) if DEBUG;1577# no extra formatting for "^+++ /dev/null"1578if($line=~m!^\+\+\+ "?b/!) {1579if($to->{'href'}) {1580$line='+++ b/'.1581$cgi->a({-href=>$to->{'href'}, -class=>"path"},1582 esc_path($to->{'file'}));1583}else{1584$line='+++ b/'.1585 esc_path($to->{'file'});1586}1587}1588$result.= qq!<div class="diff to_file">$line</div>\n!;15891590return$result;1591}15921593# create note for patch simplified by combined diff1594sub format_diff_cc_simplified {1595my($diffinfo,@parents) =@_;1596my$result='';15971598$result.="<div class=\"diff header\">".1599"diff --cc ";1600if(!is_deleted($diffinfo)) {1601$result.=$cgi->a({-href => href(action=>"blob",1602 hash_base=>$hash,1603 hash=>$diffinfo->{'to_id'},1604 file_name=>$diffinfo->{'to_file'}),1605-class=>"path"},1606 esc_path($diffinfo->{'to_file'}));1607}else{1608$result.= esc_path($diffinfo->{'to_file'});1609}1610$result.="</div>\n".# class="diff header"1611"<div class=\"diff nodifferences\">".1612"Simple merge".1613"</div>\n";# class="diff nodifferences"16141615return$result;1616}16171618# format patch (diff) line (not to be used for diff headers)1619sub format_diff_line {1620my$line=shift;1621my($from,$to) =@_;1622my$diff_class="";16231624chomp$line;16251626if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1627# combined diff1628my$prefix=substr($line,0,scalar@{$from->{'href'}});1629if($line=~m/^\@{3}/) {1630$diff_class=" chunk_header";1631}elsif($line=~m/^\\/) {1632$diff_class=" incomplete";1633}elsif($prefix=~tr/+/+/) {1634$diff_class=" add";1635}elsif($prefix=~tr/-/-/) {1636$diff_class=" rem";1637}1638}else{1639# assume ordinary diff1640my$char=substr($line,0,1);1641if($chareq'+') {1642$diff_class=" add";1643}elsif($chareq'-') {1644$diff_class=" rem";1645}elsif($chareq'@') {1646$diff_class=" chunk_header";1647}elsif($chareq"\\") {1648$diff_class=" incomplete";1649}1650}1651$line= untabify($line);1652if($from&&$to&&$line=~m/^\@{2} /) {1653my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1654$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;16551656$from_lines=0unlessdefined$from_lines;1657$to_lines=0unlessdefined$to_lines;16581659if($from->{'href'}) {1660$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1661-class=>"list"},$from_text);1662}1663if($to->{'href'}) {1664$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1665-class=>"list"},$to_text);1666}1667$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1668"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1669return"<div class=\"diff$diff_class\">$line</div>\n";1670}elsif($from&&$to&&$line=~m/^\@{3}/) {1671my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1672my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);16731674@from_text=split(' ',$ranges);1675for(my$i=0;$i<@from_text; ++$i) {1676($from_start[$i],$from_nlines[$i]) =1677(split(',',substr($from_text[$i],1)),0);1678}16791680$to_text=pop@from_text;1681$to_start=pop@from_start;1682$to_nlines=pop@from_nlines;16831684$line="<span class=\"chunk_info\">$prefix";1685for(my$i=0;$i<@from_text; ++$i) {1686if($from->{'href'}[$i]) {1687$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1688-class=>"list"},$from_text[$i]);1689}else{1690$line.=$from_text[$i];1691}1692$line.=" ";1693}1694if($to->{'href'}) {1695$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1696-class=>"list"},$to_text);1697}else{1698$line.=$to_text;1699}1700$line.="$prefix</span>".1701"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1702return"<div class=\"diff$diff_class\">$line</div>\n";1703}1704return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1705}17061707# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1708# linked. Pass the hash of the tree/commit to snapshot.1709sub format_snapshot_links {1710my($hash) =@_;1711my$num_fmts=@snapshot_fmts;1712if($num_fmts>1) {1713# A parenthesized list of links bearing format names.1714# e.g. "snapshot (_tar.gz_ _zip_)"1715return"snapshot (".join(' ',map1716$cgi->a({1717-href => href(1718 action=>"snapshot",1719 hash=>$hash,1720 snapshot_format=>$_1721)1722},$known_snapshot_formats{$_}{'display'})1723,@snapshot_fmts) .")";1724}elsif($num_fmts==1) {1725# A single "snapshot" link whose tooltip bears the format name.1726# i.e. "_snapshot_"1727my($fmt) =@snapshot_fmts;1728return1729$cgi->a({1730-href => href(1731 action=>"snapshot",1732 hash=>$hash,1733 snapshot_format=>$fmt1734),1735-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1736},"snapshot");1737}else{# $num_fmts == 01738returnundef;1739}1740}17411742## ......................................................................1743## functions returning values to be passed, perhaps after some1744## transformation, to other functions; e.g. returning arguments to href()17451746# returns hash to be passed to href to generate gitweb URL1747# in -title key it returns description of link1748sub get_feed_info {1749my$format=shift||'Atom';1750my%res= (action =>lc($format));17511752# feed links are possible only for project views1753return unless(defined$project);1754# some views should link to OPML, or to generic project feed,1755# or don't have specific feed yet (so they should use generic)1756return if($action=~/^(?:tags|heads|forks|tag|search)$/x);17571758my$branch;1759# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1760# from tag links; this also makes possible to detect branch links1761if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1762(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1763$branch=$1;1764}1765# find log type for feed description (title)1766my$type='log';1767if(defined$file_name) {1768$type="history of$file_name";1769$type.="/"if($actioneq'tree');1770$type.=" on '$branch'"if(defined$branch);1771}else{1772$type="log of$branch"if(defined$branch);1773}17741775$res{-title} =$type;1776$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1777$res{'file_name'} =$file_name;17781779return%res;1780}17811782## ----------------------------------------------------------------------1783## git utility subroutines, invoking git commands17841785# returns path to the core git executable and the --git-dir parameter as list1786sub git_cmd {1787return$GIT,'--git-dir='.$git_dir;1788}17891790# quote the given arguments for passing them to the shell1791# quote_command("command", "arg 1", "arg with ' and ! characters")1792# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1793# Try to avoid using this function wherever possible.1794sub quote_command {1795returnjoin(' ',1796map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1797}17981799# get HEAD ref of given project as hash1800sub git_get_head_hash {1801my$project=shift;1802my$o_git_dir=$git_dir;1803my$retval=undef;1804$git_dir="$projectroot/$project";1805if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1806my$head= <$fd>;1807close$fd;1808if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1809$retval=$1;1810}1811}1812if(defined$o_git_dir) {1813$git_dir=$o_git_dir;1814}1815return$retval;1816}18171818# get type of given object1819sub git_get_type {1820my$hash=shift;18211822open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1823my$type= <$fd>;1824close$fdorreturn;1825chomp$type;1826return$type;1827}18281829# repository configuration1830our$config_file='';1831our%config;18321833# store multiple values for single key as anonymous array reference1834# single values stored directly in the hash, not as [ <value> ]1835sub hash_set_multi {1836my($hash,$key,$value) =@_;18371838if(!exists$hash->{$key}) {1839$hash->{$key} =$value;1840}elsif(!ref$hash->{$key}) {1841$hash->{$key} = [$hash->{$key},$value];1842}else{1843push@{$hash->{$key}},$value;1844}1845}18461847# return hash of git project configuration1848# optionally limited to some section, e.g. 'gitweb'1849sub git_parse_project_config {1850my$section_regexp=shift;1851my%config;18521853local$/="\0";18541855open my$fh,"-|", git_cmd(),"config",'-z','-l',1856orreturn;18571858while(my$keyval= <$fh>) {1859chomp$keyval;1860my($key,$value) =split(/\n/,$keyval,2);18611862 hash_set_multi(\%config,$key,$value)1863if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1864}1865close$fh;18661867return%config;1868}18691870# convert config value to boolean, 'true' or 'false'1871# no value, number > 0, 'true' and 'yes' values are true1872# rest of values are treated as false (never as error)1873sub config_to_bool {1874my$val=shift;18751876# strip leading and trailing whitespace1877$val=~s/^\s+//;1878$val=~s/\s+$//;18791880return(!defined$val||# section.key1881($val=~/^\d+$/&&$val) ||# section.key = 11882($val=~/^(?:true|yes)$/i));# section.key = true1883}18841885# convert config value to simple decimal number1886# an optional value suffix of 'k', 'm', or 'g' will cause the value1887# to be multiplied by 1024, 1048576, or 10737418241888sub config_to_int {1889my$val=shift;18901891# strip leading and trailing whitespace1892$val=~s/^\s+//;1893$val=~s/\s+$//;18941895if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1896$unit=lc($unit);1897# unknown unit is treated as 11898return$num* ($uniteq'g'?1073741824:1899$uniteq'm'?1048576:1900$uniteq'k'?1024:1);1901}1902return$val;1903}19041905# convert config value to array reference, if needed1906sub config_to_multi {1907my$val=shift;19081909returnref($val) ?$val: (defined($val) ? [$val] : []);1910}19111912sub git_get_project_config {1913my($key,$type) =@_;19141915# key sanity check1916return unless($key);1917$key=~s/^gitweb\.//;1918return if($key=~m/\W/);19191920# type sanity check1921if(defined$type) {1922$type=~s/^--//;1923$type=undef1924unless($typeeq'bool'||$typeeq'int');1925}19261927# get config1928if(!defined$config_file||1929$config_filene"$git_dir/config") {1930%config= git_parse_project_config('gitweb');1931$config_file="$git_dir/config";1932}19331934# ensure given type1935if(!defined$type) {1936return$config{"gitweb.$key"};1937}elsif($typeeq'bool') {1938# backward compatibility: 'git config --bool' returns true/false1939return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1940}elsif($typeeq'int') {1941return config_to_int($config{"gitweb.$key"});1942}1943return$config{"gitweb.$key"};1944}19451946# get hash of given path at given ref1947sub git_get_hash_by_path {1948my$base=shift;1949my$path=shift||returnundef;1950my$type=shift;19511952$path=~ s,/+$,,;19531954open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path1955or die_error(500,"Open git-ls-tree failed");1956my$line= <$fd>;1957close$fdorreturnundef;19581959if(!defined$line) {1960# there is no tree or hash given by $path at $base1961returnundef;1962}19631964#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'1965$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;1966if(defined$type&&$typene$2) {1967# type doesn't match1968returnundef;1969}1970return$3;1971}19721973# get path of entry with given hash at given tree-ish (ref)1974# used to get 'from' filename for combined diff (merge commit) for renames1975sub git_get_path_by_hash {1976my$base=shift||return;1977my$hash=shift||return;19781979local$/="\0";19801981open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base1982orreturnundef;1983while(my$line= <$fd>) {1984chomp$line;19851986#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'1987#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'1988if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {1989close$fd;1990return$1;1991}1992}1993close$fd;1994returnundef;1995}19961997## ......................................................................1998## git utility functions, directly accessing git repository19992000sub git_get_project_description {2001my$path=shift;20022003$git_dir="$projectroot/$path";2004open my$fd,"$git_dir/description"2005orreturn git_get_project_config('description');2006my$descr= <$fd>;2007close$fd;2008if(defined$descr) {2009chomp$descr;2010}2011return$descr;2012}20132014sub git_get_project_ctags {2015my$path=shift;2016my$ctags= {};20172018$git_dir="$projectroot/$path";2019unless(opendir D,"$git_dir/ctags") {2020return$ctags;2021}2022foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir(D)) {2023open CT,$_ornext;2024my$val= <CT>;2025chomp$val;2026close CT;2027my$ctag=$_;$ctag=~ s#.*/##;2028$ctags->{$ctag} =$val;2029}2030closedir D;2031$ctags;2032}20332034sub git_populate_project_tagcloud {2035my$ctags=shift;20362037# First, merge different-cased tags; tags vote on casing2038my%ctags_lc;2039foreach(keys%$ctags) {2040$ctags_lc{lc$_}->{count} +=$ctags->{$_};2041if(not$ctags_lc{lc$_}->{topcount}2042or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2043$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2044$ctags_lc{lc$_}->{topname} =$_;2045}2046}20472048my$cloud;2049if(eval{require HTML::TagCloud;1; }) {2050$cloud= HTML::TagCloud->new;2051foreach(sort keys%ctags_lc) {2052# Pad the title with spaces so that the cloud looks2053# less crammed.2054my$title=$ctags_lc{$_}->{topname};2055$title=~s/ / /g;2056$title=~s/^/ /g;2057$title=~s/$/ /g;2058$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2059}2060}else{2061$cloud= \%ctags_lc;2062}2063$cloud;2064}20652066sub git_show_project_tagcloud {2067my($cloud,$count) =@_;2068print STDERR ref($cloud)."..\n";2069if(ref$cloudeq'HTML::TagCloud') {2070return$cloud->html_and_css($count);2071}else{2072my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2073return'<p align="center">'.join(', ',map{2074"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2075}splice(@tags,0,$count)) .'</p>';2076}2077}20782079sub git_get_project_url_list {2080my$path=shift;20812082$git_dir="$projectroot/$path";2083open my$fd,"$git_dir/cloneurl"2084orreturnwantarray?2085@{ config_to_multi(git_get_project_config('url')) } :2086 config_to_multi(git_get_project_config('url'));2087my@git_project_url_list=map{chomp;$_} <$fd>;2088close$fd;20892090returnwantarray?@git_project_url_list: \@git_project_url_list;2091}20922093sub git_get_projects_list {2094my($filter) =@_;2095my@list;20962097$filter||='';2098$filter=~s/\.git$//;20992100my$check_forks= gitweb_check_feature('forks');21012102if(-d $projects_list) {2103# search in directory2104my$dir=$projects_list. ($filter?"/$filter":'');2105# remove the trailing "/"2106$dir=~s!/+$!!;2107my$pfxlen=length("$dir");2108my$pfxdepth= ($dir=~tr!/!!);21092110 File::Find::find({2111 follow_fast =>1,# follow symbolic links2112 follow_skip =>2,# ignore duplicates2113 dangling_symlinks =>0,# ignore dangling symlinks, silently2114 wanted =>sub{2115# skip project-list toplevel, if we get it.2116return if(m!^[/.]$!);2117# only directories can be git repositories2118return unless(-d $_);2119# don't traverse too deep (Find is super slow on os x)2120if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2121$File::Find::prune =1;2122return;2123}21242125my$subdir=substr($File::Find::name,$pfxlen+1);2126# we check related file in $projectroot2127my$path= ($filter?"$filter/":'') .$subdir;2128if(check_export_ok("$projectroot/$path")) {2129push@list, { path =>$path};2130$File::Find::prune =1;2131}2132},2133},"$dir");21342135}elsif(-f $projects_list) {2136# read from file(url-encoded):2137# 'git%2Fgit.git Linus+Torvalds'2138# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2139# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2140my%paths;2141open my($fd),$projects_listorreturn;2142 PROJECT:2143while(my$line= <$fd>) {2144chomp$line;2145my($path,$owner) =split' ',$line;2146$path= unescape($path);2147$owner= unescape($owner);2148if(!defined$path) {2149next;2150}2151if($filterne'') {2152# looking for forks;2153my$pfx=substr($path,0,length($filter));2154if($pfxne$filter) {2155next PROJECT;2156}2157my$sfx=substr($path,length($filter));2158if($sfx!~/^\/.*\.git$/) {2159next PROJECT;2160}2161}elsif($check_forks) {2162 PATH:2163foreachmy$filter(keys%paths) {2164# looking for forks;2165my$pfx=substr($path,0,length($filter));2166if($pfxne$filter) {2167next PATH;2168}2169my$sfx=substr($path,length($filter));2170if($sfx!~/^\/.*\.git$/) {2171next PATH;2172}2173# is a fork, don't include it in2174# the list2175next PROJECT;2176}2177}2178if(check_export_ok("$projectroot/$path")) {2179my$pr= {2180 path =>$path,2181 owner => to_utf8($owner),2182};2183push@list,$pr;2184(my$forks_path=$path) =~s/\.git$//;2185$paths{$forks_path}++;2186}2187}2188close$fd;2189}2190return@list;2191}21922193our$gitweb_project_owner=undef;2194sub git_get_project_list_from_file {21952196return if(defined$gitweb_project_owner);21972198$gitweb_project_owner= {};2199# read from file (url-encoded):2200# 'git%2Fgit.git Linus+Torvalds'2201# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2202# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2203if(-f $projects_list) {2204open(my$fd,$projects_list);2205while(my$line= <$fd>) {2206chomp$line;2207my($pr,$ow) =split' ',$line;2208$pr= unescape($pr);2209$ow= unescape($ow);2210$gitweb_project_owner->{$pr} = to_utf8($ow);2211}2212close$fd;2213}2214}22152216sub git_get_project_owner {2217my$project=shift;2218my$owner;22192220returnundefunless$project;2221$git_dir="$projectroot/$project";22222223if(!defined$gitweb_project_owner) {2224 git_get_project_list_from_file();2225}22262227if(exists$gitweb_project_owner->{$project}) {2228$owner=$gitweb_project_owner->{$project};2229}2230if(!defined$owner){2231$owner= git_get_project_config('owner');2232}2233if(!defined$owner) {2234$owner= get_file_owner("$git_dir");2235}22362237return$owner;2238}22392240sub git_get_last_activity {2241my($path) =@_;2242my$fd;22432244$git_dir="$projectroot/$path";2245open($fd,"-|", git_cmd(),'for-each-ref',2246'--format=%(committer)',2247'--sort=-committerdate',2248'--count=1',2249'refs/heads')orreturn;2250my$most_recent= <$fd>;2251close$fdorreturn;2252if(defined$most_recent&&2253$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2254my$timestamp=$1;2255my$age=time-$timestamp;2256return($age, age_string($age));2257}2258return(undef,undef);2259}22602261sub git_get_references {2262my$type=shift||"";2263my%refs;2264# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112265# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2266open my$fd,"-|", git_cmd(),"show-ref","--dereference",2267($type? ("--","refs/$type") : ())# use -- <pattern> if $type2268orreturn;22692270while(my$line= <$fd>) {2271chomp$line;2272if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2273if(defined$refs{$1}) {2274push@{$refs{$1}},$2;2275}else{2276$refs{$1} = [$2];2277}2278}2279}2280close$fdorreturn;2281return \%refs;2282}22832284sub git_get_rev_name_tags {2285my$hash=shift||returnundef;22862287open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2288orreturn;2289my$name_rev= <$fd>;2290close$fd;22912292if($name_rev=~ m|^$hash tags/(.*)$|) {2293return$1;2294}else{2295# catches also '$hash undefined' output2296returnundef;2297}2298}22992300## ----------------------------------------------------------------------2301## parse to hash functions23022303sub parse_date {2304my$epoch=shift;2305my$tz=shift||"-0000";23062307my%date;2308my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2309my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2310my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2311$date{'hour'} =$hour;2312$date{'minute'} =$min;2313$date{'mday'} =$mday;2314$date{'day'} =$days[$wday];2315$date{'month'} =$months[$mon];2316$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2317$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2318$date{'mday-time'} =sprintf"%d%s%02d:%02d",2319$mday,$months[$mon],$hour,$min;2320$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",23211900+$year,1+$mon,$mday,$hour,$min,$sec;23222323$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2324my$local=$epoch+ ((int$1+ ($2/60)) *3600);2325($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2326$date{'hour_local'} =$hour;2327$date{'minute_local'} =$min;2328$date{'tz_local'} =$tz;2329$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",23301900+$year,$mon+1,$mday,2331$hour,$min,$sec,$tz);2332return%date;2333}23342335sub parse_tag {2336my$tag_id=shift;2337my%tag;2338my@comment;23392340open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2341$tag{'id'} =$tag_id;2342while(my$line= <$fd>) {2343chomp$line;2344if($line=~m/^object ([0-9a-fA-F]{40})$/) {2345$tag{'object'} =$1;2346}elsif($line=~m/^type (.+)$/) {2347$tag{'type'} =$1;2348}elsif($line=~m/^tag (.+)$/) {2349$tag{'name'} =$1;2350}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2351$tag{'author'} =$1;2352$tag{'epoch'} =$2;2353$tag{'tz'} =$3;2354}elsif($line=~m/--BEGIN/) {2355push@comment,$line;2356last;2357}elsif($lineeq"") {2358last;2359}2360}2361push@comment, <$fd>;2362$tag{'comment'} = \@comment;2363close$fdorreturn;2364if(!defined$tag{'name'}) {2365return2366};2367return%tag2368}23692370sub parse_commit_text {2371my($commit_text,$withparents) =@_;2372my@commit_lines=split'\n',$commit_text;2373my%co;23742375pop@commit_lines;# Remove '\0'23762377if(!@commit_lines) {2378return;2379}23802381my$header=shift@commit_lines;2382if($header!~m/^[0-9a-fA-F]{40}/) {2383return;2384}2385($co{'id'},my@parents) =split' ',$header;2386while(my$line=shift@commit_lines) {2387last if$lineeq"\n";2388if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2389$co{'tree'} =$1;2390}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2391push@parents,$1;2392}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2393$co{'author'} =$1;2394$co{'author_epoch'} =$2;2395$co{'author_tz'} =$3;2396if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2397$co{'author_name'} =$1;2398$co{'author_email'} =$2;2399}else{2400$co{'author_name'} =$co{'author'};2401}2402}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2403$co{'committer'} =$1;2404$co{'committer_epoch'} =$2;2405$co{'committer_tz'} =$3;2406$co{'committer_name'} =$co{'committer'};2407if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2408$co{'committer_name'} =$1;2409$co{'committer_email'} =$2;2410}else{2411$co{'committer_name'} =$co{'committer'};2412}2413}2414}2415if(!defined$co{'tree'}) {2416return;2417};2418$co{'parents'} = \@parents;2419$co{'parent'} =$parents[0];24202421foreachmy$title(@commit_lines) {2422$title=~s/^ //;2423if($titlene"") {2424$co{'title'} = chop_str($title,80,5);2425# remove leading stuff of merges to make the interesting part visible2426if(length($title) >50) {2427$title=~s/^Automatic //;2428$title=~s/^merge (of|with) /Merge ... /i;2429if(length($title) >50) {2430$title=~s/(http|rsync):\/\///;2431}2432if(length($title) >50) {2433$title=~s/(master|www|rsync)\.//;2434}2435if(length($title) >50) {2436$title=~s/kernel.org:?//;2437}2438if(length($title) >50) {2439$title=~s/\/pub\/scm//;2440}2441}2442$co{'title_short'} = chop_str($title,50,5);2443last;2444}2445}2446if(!defined$co{'title'} ||$co{'title'}eq"") {2447$co{'title'} =$co{'title_short'} ='(no commit message)';2448}2449# remove added spaces2450foreachmy$line(@commit_lines) {2451$line=~s/^ //;2452}2453$co{'comment'} = \@commit_lines;24542455my$age=time-$co{'committer_epoch'};2456$co{'age'} =$age;2457$co{'age_string'} = age_string($age);2458my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2459if($age>60*60*24*7*2) {2460$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2461$co{'age_string_age'} =$co{'age_string'};2462}else{2463$co{'age_string_date'} =$co{'age_string'};2464$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2465}2466return%co;2467}24682469sub parse_commit {2470my($commit_id) =@_;2471my%co;24722473local$/="\0";24742475open my$fd,"-|", git_cmd(),"rev-list",2476"--parents",2477"--header",2478"--max-count=1",2479$commit_id,2480"--",2481or die_error(500,"Open git-rev-list failed");2482%co= parse_commit_text(<$fd>,1);2483close$fd;24842485return%co;2486}24872488sub parse_commits {2489my($commit_id,$maxcount,$skip,$filename,@args) =@_;2490my@cos;24912492$maxcount||=1;2493$skip||=0;24942495local$/="\0";24962497open my$fd,"-|", git_cmd(),"rev-list",2498"--header",2499@args,2500("--max-count=".$maxcount),2501("--skip=".$skip),2502@extra_options,2503$commit_id,2504"--",2505($filename? ($filename) : ())2506or die_error(500,"Open git-rev-list failed");2507while(my$line= <$fd>) {2508my%co= parse_commit_text($line);2509push@cos, \%co;2510}2511close$fd;25122513returnwantarray?@cos: \@cos;2514}25152516# parse line of git-diff-tree "raw" output2517sub parse_difftree_raw_line {2518my$line=shift;2519my%res;25202521# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2522# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2523if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2524$res{'from_mode'} =$1;2525$res{'to_mode'} =$2;2526$res{'from_id'} =$3;2527$res{'to_id'} =$4;2528$res{'status'} =$5;2529$res{'similarity'} =$6;2530if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2531($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2532}else{2533$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2534}2535}2536# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2537# combined diff (for merge commit)2538elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2539$res{'nparents'} =length($1);2540$res{'from_mode'} = [split(' ',$2) ];2541$res{'to_mode'} =pop@{$res{'from_mode'}};2542$res{'from_id'} = [split(' ',$3) ];2543$res{'to_id'} =pop@{$res{'from_id'}};2544$res{'status'} = [split('',$4) ];2545$res{'to_file'} = unquote($5);2546}2547# 'c512b523472485aef4fff9e57b229d9d243c967f'2548elsif($line=~m/^([0-9a-fA-F]{40})$/) {2549$res{'commit'} =$1;2550}25512552returnwantarray?%res: \%res;2553}25542555# wrapper: return parsed line of git-diff-tree "raw" output2556# (the argument might be raw line, or parsed info)2557sub parsed_difftree_line {2558my$line_or_ref=shift;25592560if(ref($line_or_ref)eq"HASH") {2561# pre-parsed (or generated by hand)2562return$line_or_ref;2563}else{2564return parse_difftree_raw_line($line_or_ref);2565}2566}25672568# parse line of git-ls-tree output2569sub parse_ls_tree_line ($;%) {2570my$line=shift;2571my%opts=@_;2572my%res;25732574#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2575$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;25762577$res{'mode'} =$1;2578$res{'type'} =$2;2579$res{'hash'} =$3;2580if($opts{'-z'}) {2581$res{'name'} =$4;2582}else{2583$res{'name'} = unquote($4);2584}25852586returnwantarray?%res: \%res;2587}25882589# generates _two_ hashes, references to which are passed as 2 and 3 argument2590sub parse_from_to_diffinfo {2591my($diffinfo,$from,$to,@parents) =@_;25922593if($diffinfo->{'nparents'}) {2594# combined diff2595$from->{'file'} = [];2596$from->{'href'} = [];2597 fill_from_file_info($diffinfo,@parents)2598unlessexists$diffinfo->{'from_file'};2599for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2600$from->{'file'}[$i] =2601defined$diffinfo->{'from_file'}[$i] ?2602$diffinfo->{'from_file'}[$i] :2603$diffinfo->{'to_file'};2604if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2605$from->{'href'}[$i] = href(action=>"blob",2606 hash_base=>$parents[$i],2607 hash=>$diffinfo->{'from_id'}[$i],2608 file_name=>$from->{'file'}[$i]);2609}else{2610$from->{'href'}[$i] =undef;2611}2612}2613}else{2614# ordinary (not combined) diff2615$from->{'file'} =$diffinfo->{'from_file'};2616if($diffinfo->{'status'}ne"A") {# not new (added) file2617$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2618 hash=>$diffinfo->{'from_id'},2619 file_name=>$from->{'file'});2620}else{2621delete$from->{'href'};2622}2623}26242625$to->{'file'} =$diffinfo->{'to_file'};2626if(!is_deleted($diffinfo)) {# file exists in result2627$to->{'href'} = href(action=>"blob", hash_base=>$hash,2628 hash=>$diffinfo->{'to_id'},2629 file_name=>$to->{'file'});2630}else{2631delete$to->{'href'};2632}2633}26342635## ......................................................................2636## parse to array of hashes functions26372638sub git_get_heads_list {2639my$limit=shift;2640my@headslist;26412642open my$fd,'-|', git_cmd(),'for-each-ref',2643($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2644'--format=%(objectname) %(refname) %(subject)%00%(committer)',2645'refs/heads'2646orreturn;2647while(my$line= <$fd>) {2648my%ref_item;26492650chomp$line;2651my($refinfo,$committerinfo) =split(/\0/,$line);2652my($hash,$name,$title) =split(' ',$refinfo,3);2653my($committer,$epoch,$tz) =2654($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2655$ref_item{'fullname'} =$name;2656$name=~s!^refs/heads/!!;26572658$ref_item{'name'} =$name;2659$ref_item{'id'} =$hash;2660$ref_item{'title'} =$title||'(no commit message)';2661$ref_item{'epoch'} =$epoch;2662if($epoch) {2663$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2664}else{2665$ref_item{'age'} ="unknown";2666}26672668push@headslist, \%ref_item;2669}2670close$fd;26712672returnwantarray?@headslist: \@headslist;2673}26742675sub git_get_tags_list {2676my$limit=shift;2677my@tagslist;26782679open my$fd,'-|', git_cmd(),'for-each-ref',2680($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2681'--format=%(objectname) %(objecttype) %(refname) '.2682'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2683'refs/tags'2684orreturn;2685while(my$line= <$fd>) {2686my%ref_item;26872688chomp$line;2689my($refinfo,$creatorinfo) =split(/\0/,$line);2690my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2691my($creator,$epoch,$tz) =2692($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2693$ref_item{'fullname'} =$name;2694$name=~s!^refs/tags/!!;26952696$ref_item{'type'} =$type;2697$ref_item{'id'} =$id;2698$ref_item{'name'} =$name;2699if($typeeq"tag") {2700$ref_item{'subject'} =$title;2701$ref_item{'reftype'} =$reftype;2702$ref_item{'refid'} =$refid;2703}else{2704$ref_item{'reftype'} =$type;2705$ref_item{'refid'} =$id;2706}27072708if($typeeq"tag"||$typeeq"commit") {2709$ref_item{'epoch'} =$epoch;2710if($epoch) {2711$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2712}else{2713$ref_item{'age'} ="unknown";2714}2715}27162717push@tagslist, \%ref_item;2718}2719close$fd;27202721returnwantarray?@tagslist: \@tagslist;2722}27232724## ----------------------------------------------------------------------2725## filesystem-related functions27262727sub get_file_owner {2728my$path=shift;27292730my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2731my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2732if(!defined$gcos) {2733returnundef;2734}2735my$owner=$gcos;2736$owner=~s/[,;].*$//;2737return to_utf8($owner);2738}27392740# assume that file exists2741sub insert_file {2742my$filename=shift;27432744open my$fd,'<',$filename;2745print map{ to_utf8($_) } <$fd>;2746close$fd;2747}27482749## ......................................................................2750## mimetype related functions27512752sub mimetype_guess_file {2753my$filename=shift;2754my$mimemap=shift;2755-r $mimemaporreturnundef;27562757my%mimemap;2758open(MIME,$mimemap)orreturnundef;2759while(<MIME>) {2760next ifm/^#/;# skip comments2761my($mime,$exts) =split(/\t+/);2762if(defined$exts) {2763my@exts=split(/\s+/,$exts);2764foreachmy$ext(@exts) {2765$mimemap{$ext} =$mime;2766}2767}2768}2769close(MIME);27702771$filename=~/\.([^.]*)$/;2772return$mimemap{$1};2773}27742775sub mimetype_guess {2776my$filename=shift;2777my$mime;2778$filename=~/\./orreturnundef;27792780if($mimetypes_file) {2781my$file=$mimetypes_file;2782if($file!~m!^/!) {# if it is relative path2783# it is relative to project2784$file="$projectroot/$project/$file";2785}2786$mime= mimetype_guess_file($filename,$file);2787}2788$mime||= mimetype_guess_file($filename,'/etc/mime.types');2789return$mime;2790}27912792sub blob_mimetype {2793my$fd=shift;2794my$filename=shift;27952796if($filename) {2797my$mime= mimetype_guess($filename);2798$mimeandreturn$mime;2799}28002801# just in case2802return$default_blob_plain_mimetypeunless$fd;28032804if(-T $fd) {2805return'text/plain';2806}elsif(!$filename) {2807return'application/octet-stream';2808}elsif($filename=~m/\.png$/i) {2809return'image/png';2810}elsif($filename=~m/\.gif$/i) {2811return'image/gif';2812}elsif($filename=~m/\.jpe?g$/i) {2813return'image/jpeg';2814}else{2815return'application/octet-stream';2816}2817}28182819sub blob_contenttype {2820my($fd,$file_name,$type) =@_;28212822$type||= blob_mimetype($fd,$file_name);2823if($typeeq'text/plain'&&defined$default_text_plain_charset) {2824$type.="; charset=$default_text_plain_charset";2825}28262827return$type;2828}28292830## ======================================================================2831## functions printing HTML: header, footer, error page28322833sub git_header_html {2834my$status=shift||"200 OK";2835my$expires=shift;28362837my$title="$site_name";2838if(defined$project) {2839$title.=" - ". to_utf8($project);2840if(defined$action) {2841$title.="/$action";2842if(defined$file_name) {2843$title.=" - ". esc_path($file_name);2844if($actioneq"tree"&&$file_name!~ m|/$|) {2845$title.="/";2846}2847}2848}2849}2850my$content_type;2851# require explicit support from the UA if we are to send the page as2852# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2853# we have to do this because MSIE sometimes globs '*/*', pretending to2854# support xhtml+xml but choking when it gets what it asked for.2855if(defined$cgi->http('HTTP_ACCEPT') &&2856$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2857$cgi->Accept('application/xhtml+xml') !=0) {2858$content_type='application/xhtml+xml';2859}else{2860$content_type='text/html';2861}2862print$cgi->header(-type=>$content_type, -charset =>'utf-8',2863-status=>$status, -expires =>$expires);2864my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2865print<<EOF;2866<?xml version="1.0" encoding="utf-8"?>2867<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2868<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2869<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2870<!-- git core binaries version$git_version-->2871<head>2872<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2873<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2874<meta name="robots" content="index, nofollow"/>2875<title>$title</title>2876EOF2877# print out each stylesheet that exist2878if(defined$stylesheet) {2879#provides backwards capability for those people who define style sheet in a config file2880print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2881}else{2882foreachmy$stylesheet(@stylesheets) {2883next unless$stylesheet;2884print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2885}2886}2887if(defined$project) {2888my%href_params= get_feed_info();2889if(!exists$href_params{'-title'}) {2890$href_params{'-title'} ='log';2891}28922893foreachmy$formatqw(RSS Atom){2894my$type=lc($format);2895my%link_attr= (2896'-rel'=>'alternate',2897'-title'=>"$project-$href_params{'-title'} -$formatfeed",2898'-type'=>"application/$type+xml"2899);29002901$href_params{'action'} =$type;2902$link_attr{'-href'} = href(%href_params);2903print"<link ".2904"rel=\"$link_attr{'-rel'}\"".2905"title=\"$link_attr{'-title'}\"".2906"href=\"$link_attr{'-href'}\"".2907"type=\"$link_attr{'-type'}\"".2908"/>\n";29092910$href_params{'extra_options'} ='--no-merges';2911$link_attr{'-href'} = href(%href_params);2912$link_attr{'-title'} .=' (no merges)';2913print"<link ".2914"rel=\"$link_attr{'-rel'}\"".2915"title=\"$link_attr{'-title'}\"".2916"href=\"$link_attr{'-href'}\"".2917"type=\"$link_attr{'-type'}\"".2918"/>\n";2919}29202921}else{2922printf('<link rel="alternate" title="%sprojects list" '.2923'href="%s" type="text/plain; charset=utf-8" />'."\n",2924$site_name, href(project=>undef, action=>"project_index"));2925printf('<link rel="alternate" title="%sprojects feeds" '.2926'href="%s" type="text/x-opml" />'."\n",2927$site_name, href(project=>undef, action=>"opml"));2928}2929if(defined$favicon) {2930printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2931}29322933print"</head>\n".2934"<body>\n";29352936if(-f $site_header) {2937 insert_file($site_header);2938}29392940print"<div class=\"page_header\">\n".2941$cgi->a({-href => esc_url($logo_url),2942-title =>$logo_label},2943qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2944print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2945if(defined$project) {2946print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2947if(defined$action) {2948print" /$action";2949}2950print"\n";2951}2952print"</div>\n";29532954my$have_search= gitweb_check_feature('search');2955if(defined$project&&$have_search) {2956if(!defined$searchtext) {2957$searchtext="";2958}2959my$search_hash;2960if(defined$hash_base) {2961$search_hash=$hash_base;2962}elsif(defined$hash) {2963$search_hash=$hash;2964}else{2965$search_hash="HEAD";2966}2967my$action=$my_uri;2968my$use_pathinfo= gitweb_check_feature('pathinfo');2969if($use_pathinfo) {2970$action.="/".esc_url($project);2971}2972print$cgi->startform(-method=>"get", -action =>$action) .2973"<div class=\"search\">\n".2974(!$use_pathinfo&&2975$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .2976$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".2977$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".2978$cgi->popup_menu(-name =>'st', -default=>'commit',2979-values=> ['commit','grep','author','committer','pickaxe']) .2980$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .2981" search:\n",2982$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".2983"<span title=\"Extended regular expression\">".2984$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',2985-checked =>$search_use_regexp) .2986"</span>".2987"</div>".2988$cgi->end_form() ."\n";2989}2990}29912992sub git_footer_html {2993my$feed_class='rss_logo';29942995print"<div class=\"page_footer\">\n";2996if(defined$project) {2997my$descr= git_get_project_description($project);2998if(defined$descr) {2999print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3000}30013002my%href_params= get_feed_info();3003if(!%href_params) {3004$feed_class.=' generic';3005}3006$href_params{'-title'} ||='log';30073008foreachmy$formatqw(RSS Atom){3009$href_params{'action'} =lc($format);3010print$cgi->a({-href => href(%href_params),3011-title =>"$href_params{'-title'}$formatfeed",3012-class=>$feed_class},$format)."\n";3013}30143015}else{3016print$cgi->a({-href => href(project=>undef, action=>"opml"),3017-class=>$feed_class},"OPML") ." ";3018print$cgi->a({-href => href(project=>undef, action=>"project_index"),3019-class=>$feed_class},"TXT") ."\n";3020}3021print"</div>\n";# class="page_footer"30223023if(-f $site_footer) {3024 insert_file($site_footer);3025}30263027print"</body>\n".3028"</html>";3029}30303031# die_error(<http_status_code>, <error_message>)3032# Example: die_error(404, 'Hash not found')3033# By convention, use the following status codes (as defined in RFC 2616):3034# 400: Invalid or missing CGI parameters, or3035# requested object exists but has wrong type.3036# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3037# this server or project.3038# 404: Requested object/revision/project doesn't exist.3039# 500: The server isn't configured properly, or3040# an internal error occurred (e.g. failed assertions caused by bugs), or3041# an unknown error occurred (e.g. the git binary died unexpectedly).3042sub die_error {3043my$status=shift||500;3044my$error=shift||"Internal server error";30453046my%http_responses= (400=>'400 Bad Request',3047403=>'403 Forbidden',3048404=>'404 Not Found',3049500=>'500 Internal Server Error');3050 git_header_html($http_responses{$status});3051print<<EOF;3052<div class="page_body">3053<br /><br />3054$status-$error3055<br />3056</div>3057EOF3058 git_footer_html();3059exit;3060}30613062## ----------------------------------------------------------------------3063## functions printing or outputting HTML: navigation30643065sub git_print_page_nav {3066my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3067$extra=''if!defined$extra;# pager or formats30683069my@navs=qw(summary shortlog log commit commitdiff tree);3070if($suppress) {3071@navs=grep{$_ne$suppress}@navs;3072}30733074my%arg=map{$_=> {action=>$_} }@navs;3075if(defined$head) {3076for(qw(commit commitdiff)) {3077$arg{$_}{'hash'} =$head;3078}3079if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3080for(qw(shortlog log)) {3081$arg{$_}{'hash'} =$head;3082}3083}3084}30853086$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3087$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;30883089my@actions= gitweb_get_feature('actions');3090my%repl= (3091'%'=>'%',3092'n'=>$project,# project name3093'f'=>$git_dir,# project path within filesystem3094'h'=>$treehead||'',# current hash ('h' parameter)3095'b'=>$treebase||'',# hash base ('hb' parameter)3096);3097while(@actions) {3098my($label,$link,$pos) =splice(@actions,0,3);3099# insert3100@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3101# munch munch3102$link=~s/%([%nfhb])/$repl{$1}/g;3103$arg{$label}{'_href'} =$link;3104}31053106print"<div class=\"page_nav\">\n".3107(join" | ",3108map{$_eq$current?3109$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3110}@navs);3111print"<br/>\n$extra<br/>\n".3112"</div>\n";3113}31143115sub format_paging_nav {3116my($action,$hash,$head,$page,$has_next_link) =@_;3117my$paging_nav;311831193120if($hashne$head||$page) {3121$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3122}else{3123$paging_nav.="HEAD";3124}31253126if($page>0) {3127$paging_nav.=" ⋅ ".3128$cgi->a({-href => href(-replay=>1, page=>$page-1),3129-accesskey =>"p", -title =>"Alt-p"},"prev");3130}else{3131$paging_nav.=" ⋅ prev";3132}31333134if($has_next_link) {3135$paging_nav.=" ⋅ ".3136$cgi->a({-href => href(-replay=>1, page=>$page+1),3137-accesskey =>"n", -title =>"Alt-n"},"next");3138}else{3139$paging_nav.=" ⋅ next";3140}31413142return$paging_nav;3143}31443145## ......................................................................3146## functions printing or outputting HTML: div31473148sub git_print_header_div {3149my($action,$title,$hash,$hash_base) =@_;3150my%args= ();31513152$args{'action'} =$action;3153$args{'hash'} =$hashif$hash;3154$args{'hash_base'} =$hash_baseif$hash_base;31553156print"<div class=\"header\">\n".3157$cgi->a({-href => href(%args), -class=>"title"},3158$title?$title:$action) .3159"\n</div>\n";3160}31613162#sub git_print_authorship (\%) {3163sub git_print_authorship {3164my$co=shift;31653166my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3167print"<div class=\"author_date\">".3168 esc_html($co->{'author_name'}) .3169" [$ad{'rfc2822'}";3170if($ad{'hour_local'} <6) {3171printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3172$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3173}else{3174printf(" (%02d:%02d%s)",3175$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3176}3177print"]</div>\n";3178}31793180sub git_print_page_path {3181my$name=shift;3182my$type=shift;3183my$hb=shift;318431853186print"<div class=\"page_path\">";3187print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3188-title =>'tree root'}, to_utf8("[$project]"));3189print" / ";3190if(defined$name) {3191my@dirname=split'/',$name;3192my$basename=pop@dirname;3193my$fullname='';31943195foreachmy$dir(@dirname) {3196$fullname.= ($fullname?'/':'') .$dir;3197print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3198 hash_base=>$hb),3199-title =>$fullname}, esc_path($dir));3200print" / ";3201}3202if(defined$type&&$typeeq'blob') {3203print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3204 hash_base=>$hb),3205-title =>$name}, esc_path($basename));3206}elsif(defined$type&&$typeeq'tree') {3207print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3208 hash_base=>$hb),3209-title =>$name}, esc_path($basename));3210print" / ";3211}else{3212print esc_path($basename);3213}3214}3215print"<br/></div>\n";3216}32173218# sub git_print_log (\@;%) {3219sub git_print_log ($;%) {3220my$log=shift;3221my%opts=@_;32223223if($opts{'-remove_title'}) {3224# remove title, i.e. first line of log3225shift@$log;3226}3227# remove leading empty lines3228while(defined$log->[0] &&$log->[0]eq"") {3229shift@$log;3230}32313232# print log3233my$signoff=0;3234my$empty=0;3235foreachmy$line(@$log) {3236if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3237$signoff=1;3238$empty=0;3239if(!$opts{'-remove_signoff'}) {3240print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3241next;3242}else{3243# remove signoff lines3244next;3245}3246}else{3247$signoff=0;3248}32493250# print only one empty line3251# do not print empty line after signoff3252if($lineeq"") {3253next if($empty||$signoff);3254$empty=1;3255}else{3256$empty=0;3257}32583259print format_log_line_html($line) ."<br/>\n";3260}32613262if($opts{'-final_empty_line'}) {3263# end with single empty line3264print"<br/>\n"unless$empty;3265}3266}32673268# return link target (what link points to)3269sub git_get_link_target {3270my$hash=shift;3271my$link_target;32723273# read link3274open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3275orreturn;3276{3277local$/;3278$link_target= <$fd>;3279}3280close$fd3281orreturn;32823283return$link_target;3284}32853286# given link target, and the directory (basedir) the link is in,3287# return target of link relative to top directory (top tree);3288# return undef if it is not possible (including absolute links).3289sub normalize_link_target {3290my($link_target,$basedir,$hash_base) =@_;32913292# we can normalize symlink target only if $hash_base is provided3293return unless$hash_base;32943295# absolute symlinks (beginning with '/') cannot be normalized3296return if(substr($link_target,0,1)eq'/');32973298# normalize link target to path from top (root) tree (dir)3299my$path;3300if($basedir) {3301$path=$basedir.'/'.$link_target;3302}else{3303# we are in top (root) tree (dir)3304$path=$link_target;3305}33063307# remove //, /./, and /../3308my@path_parts;3309foreachmy$part(split('/',$path)) {3310# discard '.' and ''3311next if(!$part||$parteq'.');3312# handle '..'3313if($parteq'..') {3314if(@path_parts) {3315pop@path_parts;3316}else{3317# link leads outside repository (outside top dir)3318return;3319}3320}else{3321push@path_parts,$part;3322}3323}3324$path=join('/',@path_parts);33253326return$path;3327}33283329# print tree entry (row of git_tree), but without encompassing <tr> element3330sub git_print_tree_entry {3331my($t,$basedir,$hash_base,$have_blame) =@_;33323333my%base_key= ();3334$base_key{'hash_base'} =$hash_baseifdefined$hash_base;33353336# The format of a table row is: mode list link. Where mode is3337# the mode of the entry, list is the name of the entry, an href,3338# and link is the action links of the entry.33393340print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3341if($t->{'type'}eq"blob") {3342print"<td class=\"list\">".3343$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3344 file_name=>"$basedir$t->{'name'}",%base_key),3345-class=>"list"}, esc_path($t->{'name'}));3346if(S_ISLNK(oct$t->{'mode'})) {3347my$link_target= git_get_link_target($t->{'hash'});3348if($link_target) {3349my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3350if(defined$norm_target) {3351print" -> ".3352$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3353 file_name=>$norm_target),3354-title =>$norm_target}, esc_path($link_target));3355}else{3356print" -> ". esc_path($link_target);3357}3358}3359}3360print"</td>\n";3361print"<td class=\"link\">";3362print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3363 file_name=>"$basedir$t->{'name'}",%base_key)},3364"blob");3365if($have_blame) {3366print" | ".3367$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3368 file_name=>"$basedir$t->{'name'}",%base_key)},3369"blame");3370}3371if(defined$hash_base) {3372print" | ".3373$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3374 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3375"history");3376}3377print" | ".3378$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3379 file_name=>"$basedir$t->{'name'}")},3380"raw");3381print"</td>\n";33823383}elsif($t->{'type'}eq"tree") {3384print"<td class=\"list\">";3385print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3386 file_name=>"$basedir$t->{'name'}",%base_key)},3387 esc_path($t->{'name'}));3388print"</td>\n";3389print"<td class=\"link\">";3390print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3391 file_name=>"$basedir$t->{'name'}",%base_key)},3392"tree");3393if(defined$hash_base) {3394print" | ".3395$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3396 file_name=>"$basedir$t->{'name'}")},3397"history");3398}3399print"</td>\n";3400}else{3401# unknown object: we can only present history for it3402# (this includes 'commit' object, i.e. submodule support)3403print"<td class=\"list\">".3404 esc_path($t->{'name'}) .3405"</td>\n";3406print"<td class=\"link\">";3407if(defined$hash_base) {3408print$cgi->a({-href => href(action=>"history",3409 hash_base=>$hash_base,3410 file_name=>"$basedir$t->{'name'}")},3411"history");3412}3413print"</td>\n";3414}3415}34163417## ......................................................................3418## functions printing large fragments of HTML34193420# get pre-image filenames for merge (combined) diff3421sub fill_from_file_info {3422my($diff,@parents) =@_;34233424$diff->{'from_file'} = [ ];3425$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3426for(my$i=0;$i<$diff->{'nparents'};$i++) {3427if($diff->{'status'}[$i]eq'R'||3428$diff->{'status'}[$i]eq'C') {3429$diff->{'from_file'}[$i] =3430 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3431}3432}34333434return$diff;3435}34363437# is current raw difftree line of file deletion3438sub is_deleted {3439my$diffinfo=shift;34403441return$diffinfo->{'to_id'}eq('0' x 40);3442}34433444# does patch correspond to [previous] difftree raw line3445# $diffinfo - hashref of parsed raw diff format3446# $patchinfo - hashref of parsed patch diff format3447# (the same keys as in $diffinfo)3448sub is_patch_split {3449my($diffinfo,$patchinfo) =@_;34503451returndefined$diffinfo&&defined$patchinfo3452&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3453}345434553456sub git_difftree_body {3457my($difftree,$hash,@parents) =@_;3458my($parent) =$parents[0];3459my$have_blame= gitweb_check_feature('blame');3460print"<div class=\"list_head\">\n";3461if($#{$difftree} >10) {3462print(($#{$difftree} +1) ." files changed:\n");3463}3464print"</div>\n";34653466print"<table class=\"".3467(@parents>1?"combined ":"") .3468"diff_tree\">\n";34693470# header only for combined diff in 'commitdiff' view3471my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3472if($has_header) {3473# table header3474print"<thead><tr>\n".3475"<th></th><th></th>\n";# filename, patchN link3476for(my$i=0;$i<@parents;$i++) {3477my$par=$parents[$i];3478print"<th>".3479$cgi->a({-href => href(action=>"commitdiff",3480 hash=>$hash, hash_parent=>$par),3481-title =>'commitdiff to parent number '.3482($i+1) .': '.substr($par,0,7)},3483$i+1) .3484" </th>\n";3485}3486print"</tr></thead>\n<tbody>\n";3487}34883489my$alternate=1;3490my$patchno=0;3491foreachmy$line(@{$difftree}) {3492my$diff= parsed_difftree_line($line);34933494if($alternate) {3495print"<tr class=\"dark\">\n";3496}else{3497print"<tr class=\"light\">\n";3498}3499$alternate^=1;35003501if(exists$diff->{'nparents'}) {# combined diff35023503 fill_from_file_info($diff,@parents)3504unlessexists$diff->{'from_file'};35053506if(!is_deleted($diff)) {3507# file exists in the result (child) commit3508print"<td>".3509$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3510 file_name=>$diff->{'to_file'},3511 hash_base=>$hash),3512-class=>"list"}, esc_path($diff->{'to_file'})) .3513"</td>\n";3514}else{3515print"<td>".3516 esc_path($diff->{'to_file'}) .3517"</td>\n";3518}35193520if($actioneq'commitdiff') {3521# link to patch3522$patchno++;3523print"<td class=\"link\">".3524$cgi->a({-href =>"#patch$patchno"},"patch") .3525" | ".3526"</td>\n";3527}35283529my$has_history=0;3530my$not_deleted=0;3531for(my$i=0;$i<$diff->{'nparents'};$i++) {3532my$hash_parent=$parents[$i];3533my$from_hash=$diff->{'from_id'}[$i];3534my$from_path=$diff->{'from_file'}[$i];3535my$status=$diff->{'status'}[$i];35363537$has_history||= ($statusne'A');3538$not_deleted||= ($statusne'D');35393540if($statuseq'A') {3541print"<td class=\"link\"align=\"right\"> | </td>\n";3542}elsif($statuseq'D') {3543print"<td class=\"link\">".3544$cgi->a({-href => href(action=>"blob",3545 hash_base=>$hash,3546 hash=>$from_hash,3547 file_name=>$from_path)},3548"blob". ($i+1)) .3549" | </td>\n";3550}else{3551if($diff->{'to_id'}eq$from_hash) {3552print"<td class=\"link nochange\">";3553}else{3554print"<td class=\"link\">";3555}3556print$cgi->a({-href => href(action=>"blobdiff",3557 hash=>$diff->{'to_id'},3558 hash_parent=>$from_hash,3559 hash_base=>$hash,3560 hash_parent_base=>$hash_parent,3561 file_name=>$diff->{'to_file'},3562 file_parent=>$from_path)},3563"diff". ($i+1)) .3564" | </td>\n";3565}3566}35673568print"<td class=\"link\">";3569if($not_deleted) {3570print$cgi->a({-href => href(action=>"blob",3571 hash=>$diff->{'to_id'},3572 file_name=>$diff->{'to_file'},3573 hash_base=>$hash)},3574"blob");3575print" | "if($has_history);3576}3577if($has_history) {3578print$cgi->a({-href => href(action=>"history",3579 file_name=>$diff->{'to_file'},3580 hash_base=>$hash)},3581"history");3582}3583print"</td>\n";35843585print"</tr>\n";3586next;# instead of 'else' clause, to avoid extra indent3587}3588# else ordinary diff35893590my($to_mode_oct,$to_mode_str,$to_file_type);3591my($from_mode_oct,$from_mode_str,$from_file_type);3592if($diff->{'to_mode'}ne('0' x 6)) {3593$to_mode_oct=oct$diff->{'to_mode'};3594if(S_ISREG($to_mode_oct)) {# only for regular file3595$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3596}3597$to_file_type= file_type($diff->{'to_mode'});3598}3599if($diff->{'from_mode'}ne('0' x 6)) {3600$from_mode_oct=oct$diff->{'from_mode'};3601if(S_ISREG($to_mode_oct)) {# only for regular file3602$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3603}3604$from_file_type= file_type($diff->{'from_mode'});3605}36063607if($diff->{'status'}eq"A") {# created3608my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3609$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3610$mode_chng.="]</span>";3611print"<td>";3612print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3613 hash_base=>$hash, file_name=>$diff->{'file'}),3614-class=>"list"}, esc_path($diff->{'file'}));3615print"</td>\n";3616print"<td>$mode_chng</td>\n";3617print"<td class=\"link\">";3618if($actioneq'commitdiff') {3619# link to patch3620$patchno++;3621print$cgi->a({-href =>"#patch$patchno"},"patch");3622print" | ";3623}3624print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3625 hash_base=>$hash, file_name=>$diff->{'file'})},3626"blob");3627print"</td>\n";36283629}elsif($diff->{'status'}eq"D") {# deleted3630my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3631print"<td>";3632print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3633 hash_base=>$parent, file_name=>$diff->{'file'}),3634-class=>"list"}, esc_path($diff->{'file'}));3635print"</td>\n";3636print"<td>$mode_chng</td>\n";3637print"<td class=\"link\">";3638if($actioneq'commitdiff') {3639# link to patch3640$patchno++;3641print$cgi->a({-href =>"#patch$patchno"},"patch");3642print" | ";3643}3644print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3645 hash_base=>$parent, file_name=>$diff->{'file'})},3646"blob") ." | ";3647if($have_blame) {3648print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3649 file_name=>$diff->{'file'})},3650"blame") ." | ";3651}3652print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3653 file_name=>$diff->{'file'})},3654"history");3655print"</td>\n";36563657}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3658my$mode_chnge="";3659if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3660$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3661if($from_file_typene$to_file_type) {3662$mode_chnge.=" from$from_file_typeto$to_file_type";3663}3664if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3665if($from_mode_str&&$to_mode_str) {3666$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3667}elsif($to_mode_str) {3668$mode_chnge.=" mode:$to_mode_str";3669}3670}3671$mode_chnge.="]</span>\n";3672}3673print"<td>";3674print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3675 hash_base=>$hash, file_name=>$diff->{'file'}),3676-class=>"list"}, esc_path($diff->{'file'}));3677print"</td>\n";3678print"<td>$mode_chnge</td>\n";3679print"<td class=\"link\">";3680if($actioneq'commitdiff') {3681# link to patch3682$patchno++;3683print$cgi->a({-href =>"#patch$patchno"},"patch") .3684" | ";3685}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3686# "commit" view and modified file (not onlu mode changed)3687print$cgi->a({-href => href(action=>"blobdiff",3688 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3689 hash_base=>$hash, hash_parent_base=>$parent,3690 file_name=>$diff->{'file'})},3691"diff") .3692" | ";3693}3694print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3695 hash_base=>$hash, file_name=>$diff->{'file'})},3696"blob") ." | ";3697if($have_blame) {3698print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3699 file_name=>$diff->{'file'})},3700"blame") ." | ";3701}3702print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3703 file_name=>$diff->{'file'})},3704"history");3705print"</td>\n";37063707}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3708my%status_name= ('R'=>'moved','C'=>'copied');3709my$nstatus=$status_name{$diff->{'status'}};3710my$mode_chng="";3711if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3712# mode also for directories, so we cannot use $to_mode_str3713$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3714}3715print"<td>".3716$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3717 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3718-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3719"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3720$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3721 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3722-class=>"list"}, esc_path($diff->{'from_file'})) .3723" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3724"<td class=\"link\">";3725if($actioneq'commitdiff') {3726# link to patch3727$patchno++;3728print$cgi->a({-href =>"#patch$patchno"},"patch") .3729" | ";3730}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3731# "commit" view and modified file (not only pure rename or copy)3732print$cgi->a({-href => href(action=>"blobdiff",3733 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3734 hash_base=>$hash, hash_parent_base=>$parent,3735 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3736"diff") .3737" | ";3738}3739print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3740 hash_base=>$parent, file_name=>$diff->{'to_file'})},3741"blob") ." | ";3742if($have_blame) {3743print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3744 file_name=>$diff->{'to_file'})},3745"blame") ." | ";3746}3747print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3748 file_name=>$diff->{'to_file'})},3749"history");3750print"</td>\n";37513752}# we should not encounter Unmerged (U) or Unknown (X) status3753print"</tr>\n";3754}3755print"</tbody>"if$has_header;3756print"</table>\n";3757}37583759sub git_patchset_body {3760my($fd,$difftree,$hash,@hash_parents) =@_;3761my($hash_parent) =$hash_parents[0];37623763my$is_combined= (@hash_parents>1);3764my$patch_idx=0;3765my$patch_number=0;3766my$patch_line;3767my$diffinfo;3768my$to_name;3769my(%from,%to);37703771print"<div class=\"patchset\">\n";37723773# skip to first patch3774while($patch_line= <$fd>) {3775chomp$patch_line;37763777last if($patch_line=~m/^diff /);3778}37793780 PATCH:3781while($patch_line) {37823783# parse "git diff" header line3784if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3785# $1 is from_name, which we do not use3786$to_name= unquote($2);3787$to_name=~s!^b/!!;3788}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3789# $1 is 'cc' or 'combined', which we do not use3790$to_name= unquote($2);3791}else{3792$to_name=undef;3793}37943795# check if current patch belong to current raw line3796# and parse raw git-diff line if needed3797if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3798# this is continuation of a split patch3799print"<div class=\"patch cont\">\n";3800}else{3801# advance raw git-diff output if needed3802$patch_idx++ifdefined$diffinfo;38033804# read and prepare patch information3805$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38063807# compact combined diff output can have some patches skipped3808# find which patch (using pathname of result) we are at now;3809if($is_combined) {3810while($to_namene$diffinfo->{'to_file'}) {3811print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3812 format_diff_cc_simplified($diffinfo,@hash_parents) .3813"</div>\n";# class="patch"38143815$patch_idx++;3816$patch_number++;38173818last if$patch_idx>$#$difftree;3819$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3820}3821}38223823# modifies %from, %to hashes3824 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);38253826# this is first patch for raw difftree line with $patch_idx index3827# we index @$difftree array from 0, but number patches from 13828print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3829}38303831# git diff header3832#assert($patch_line =~ m/^diff /) if DEBUG;3833#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3834$patch_number++;3835# print "git diff" header3836print format_git_diff_header_line($patch_line,$diffinfo,3837 \%from, \%to);38383839# print extended diff header3840print"<div class=\"diff extended_header\">\n";3841 EXTENDED_HEADER:3842while($patch_line= <$fd>) {3843chomp$patch_line;38443845last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);38463847print format_extended_diff_header_line($patch_line,$diffinfo,3848 \%from, \%to);3849}3850print"</div>\n";# class="diff extended_header"38513852# from-file/to-file diff header3853if(!$patch_line) {3854print"</div>\n";# class="patch"3855last PATCH;3856}3857next PATCH if($patch_line=~m/^diff /);3858#assert($patch_line =~ m/^---/) if DEBUG;38593860my$last_patch_line=$patch_line;3861$patch_line= <$fd>;3862chomp$patch_line;3863#assert($patch_line =~ m/^\+\+\+/) if DEBUG;38643865print format_diff_from_to_header($last_patch_line,$patch_line,3866$diffinfo, \%from, \%to,3867@hash_parents);38683869# the patch itself3870 LINE:3871while($patch_line= <$fd>) {3872chomp$patch_line;38733874next PATCH if($patch_line=~m/^diff /);38753876print format_diff_line($patch_line, \%from, \%to);3877}38783879}continue{3880print"</div>\n";# class="patch"3881}38823883# for compact combined (--cc) format, with chunk and patch simpliciaction3884# patchset might be empty, but there might be unprocessed raw lines3885for(++$patch_idxif$patch_number>0;3886$patch_idx<@$difftree;3887++$patch_idx) {3888# read and prepare patch information3889$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38903891# generate anchor for "patch" links in difftree / whatchanged part3892print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3893 format_diff_cc_simplified($diffinfo,@hash_parents) .3894"</div>\n";# class="patch"38953896$patch_number++;3897}38983899if($patch_number==0) {3900if(@hash_parents>1) {3901print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3902}else{3903print"<div class=\"diff nodifferences\">No differences found</div>\n";3904}3905}39063907print"</div>\n";# class="patchset"3908}39093910# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39113912# fills project list info (age, description, owner, forks) for each3913# project in the list, removing invalid projects from returned list3914# NOTE: modifies $projlist, but does not remove entries from it3915sub fill_project_list_info {3916my($projlist,$check_forks) =@_;3917my@projects;39183919my$show_ctags= gitweb_check_feature('ctags');3920 PROJECT:3921foreachmy$pr(@$projlist) {3922my(@activity) = git_get_last_activity($pr->{'path'});3923unless(@activity) {3924next PROJECT;3925}3926($pr->{'age'},$pr->{'age_string'}) =@activity;3927if(!defined$pr->{'descr'}) {3928my$descr= git_get_project_description($pr->{'path'}) ||"";3929$descr= to_utf8($descr);3930$pr->{'descr_long'} =$descr;3931$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3932}3933if(!defined$pr->{'owner'}) {3934$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3935}3936if($check_forks) {3937my$pname=$pr->{'path'};3938if(($pname=~s/\.git$//) &&3939($pname!~/\/$/) &&3940(-d "$projectroot/$pname")) {3941$pr->{'forks'} ="-d$projectroot/$pname";3942}else{3943$pr->{'forks'} =0;3944}3945}3946$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3947push@projects,$pr;3948}39493950return@projects;3951}39523953# print 'sort by' <th> element, generating 'sort by $name' replay link3954# if that order is not selected3955sub print_sort_th {3956my($name,$order,$header) =@_;3957$header||=ucfirst($name);39583959if($ordereq$name) {3960print"<th>$header</th>\n";3961}else{3962print"<th>".3963$cgi->a({-href => href(-replay=>1, order=>$name),3964-class=>"header"},$header) .3965"</th>\n";3966}3967}39683969sub git_project_list_body {3970# actually uses global variable $project3971my($projlist,$order,$from,$to,$extra,$no_header) =@_;39723973my$check_forks= gitweb_check_feature('forks');3974my@projects= fill_project_list_info($projlist,$check_forks);39753976$order||=$default_projects_order;3977$from=0unlessdefined$from;3978$to=$#projectsif(!defined$to||$#projects<$to);39793980my%order_info= (3981 project => { key =>'path', type =>'str'},3982 descr => { key =>'descr_long', type =>'str'},3983 owner => { key =>'owner', type =>'str'},3984 age => { key =>'age', type =>'num'}3985);3986my$oi=$order_info{$order};3987if($oi->{'type'}eq'str') {3988@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;3989}else{3990@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;3991}39923993my$show_ctags= gitweb_check_feature('ctags');3994if($show_ctags) {3995my%ctags;3996foreachmy$p(@projects) {3997foreachmy$ct(keys%{$p->{'ctags'}}) {3998$ctags{$ct} +=$p->{'ctags'}->{$ct};3999}4000}4001my$cloud= git_populate_project_tagcloud(\%ctags);4002print git_show_project_tagcloud($cloud,64);4003}40044005print"<table class=\"project_list\">\n";4006unless($no_header) {4007print"<tr>\n";4008if($check_forks) {4009print"<th></th>\n";4010}4011 print_sort_th('project',$order,'Project');4012 print_sort_th('descr',$order,'Description');4013 print_sort_th('owner',$order,'Owner');4014 print_sort_th('age',$order,'Last Change');4015print"<th></th>\n".# for links4016"</tr>\n";4017}4018my$alternate=1;4019my$tagfilter=$cgi->param('by_tag');4020for(my$i=$from;$i<=$to;$i++) {4021my$pr=$projects[$i];40224023next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4024next if$searchtextand not$pr->{'path'} =~/$searchtext/4025and not$pr->{'descr_long'} =~/$searchtext/;4026# Weed out forks or non-matching entries of search4027if($check_forks) {4028my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4029$forkbase="^$forkbase"if$forkbase;4030next ifnot$searchtextand not$tagfilterand$show_ctags4031and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4032}40334034if($alternate) {4035print"<tr class=\"dark\">\n";4036}else{4037print"<tr class=\"light\">\n";4038}4039$alternate^=1;4040if($check_forks) {4041print"<td>";4042if($pr->{'forks'}) {4043print"<!--$pr->{'forks'} -->\n";4044print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4045}4046print"</td>\n";4047}4048print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4049-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4050"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4051-class=>"list", -title =>$pr->{'descr_long'}},4052 esc_html($pr->{'descr'})) ."</td>\n".4053"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4054print"<td class=\"". age_class($pr->{'age'}) ."\">".4055(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4056"<td class=\"link\">".4057$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4058$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4059$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4060$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4061($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4062"</td>\n".4063"</tr>\n";4064}4065if(defined$extra) {4066print"<tr>\n";4067if($check_forks) {4068print"<td></td>\n";4069}4070print"<td colspan=\"5\">$extra</td>\n".4071"</tr>\n";4072}4073print"</table>\n";4074}40754076sub git_shortlog_body {4077# uses global variable $project4078my($commitlist,$from,$to,$refs,$extra) =@_;40794080$from=0unlessdefined$from;4081$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);40824083print"<table class=\"shortlog\">\n";4084my$alternate=1;4085for(my$i=$from;$i<=$to;$i++) {4086my%co= %{$commitlist->[$i]};4087my$commit=$co{'id'};4088my$ref= format_ref_marker($refs,$commit);4089if($alternate) {4090print"<tr class=\"dark\">\n";4091}else{4092print"<tr class=\"light\">\n";4093}4094$alternate^=1;4095my$author= chop_and_escape_str($co{'author_name'},10);4096# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4097print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4098"<td><i>".$author."</i></td>\n".4099"<td>";4100print format_subject_html($co{'title'},$co{'title_short'},4101 href(action=>"commit", hash=>$commit),$ref);4102print"</td>\n".4103"<td class=\"link\">".4104$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4105$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4106$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4107my$snapshot_links= format_snapshot_links($commit);4108if(defined$snapshot_links) {4109print" | ".$snapshot_links;4110}4111print"</td>\n".4112"</tr>\n";4113}4114if(defined$extra) {4115print"<tr>\n".4116"<td colspan=\"4\">$extra</td>\n".4117"</tr>\n";4118}4119print"</table>\n";4120}41214122sub git_history_body {4123# Warning: assumes constant type (blob or tree) during history4124my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;41254126$from=0unlessdefined$from;4127$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});41284129print"<table class=\"history\">\n";4130my$alternate=1;4131for(my$i=$from;$i<=$to;$i++) {4132my%co= %{$commitlist->[$i]};4133if(!%co) {4134next;4135}4136my$commit=$co{'id'};41374138my$ref= format_ref_marker($refs,$commit);41394140if($alternate) {4141print"<tr class=\"dark\">\n";4142}else{4143print"<tr class=\"light\">\n";4144}4145$alternate^=1;4146# shortlog uses chop_str($co{'author_name'}, 10)4147my$author= chop_and_escape_str($co{'author_name'},15,3);4148print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4149"<td><i>".$author."</i></td>\n".4150"<td>";4151# originally git_history used chop_str($co{'title'}, 50)4152print format_subject_html($co{'title'},$co{'title_short'},4153 href(action=>"commit", hash=>$commit),$ref);4154print"</td>\n".4155"<td class=\"link\">".4156$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4157$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");41584159if($ftypeeq'blob') {4160my$blob_current= git_get_hash_by_path($hash_base,$file_name);4161my$blob_parent= git_get_hash_by_path($commit,$file_name);4162if(defined$blob_current&&defined$blob_parent&&4163$blob_currentne$blob_parent) {4164print" | ".4165$cgi->a({-href => href(action=>"blobdiff",4166 hash=>$blob_current, hash_parent=>$blob_parent,4167 hash_base=>$hash_base, hash_parent_base=>$commit,4168 file_name=>$file_name)},4169"diff to current");4170}4171}4172print"</td>\n".4173"</tr>\n";4174}4175if(defined$extra) {4176print"<tr>\n".4177"<td colspan=\"4\">$extra</td>\n".4178"</tr>\n";4179}4180print"</table>\n";4181}41824183sub git_tags_body {4184# uses global variable $project4185my($taglist,$from,$to,$extra) =@_;4186$from=0unlessdefined$from;4187$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);41884189print"<table class=\"tags\">\n";4190my$alternate=1;4191for(my$i=$from;$i<=$to;$i++) {4192my$entry=$taglist->[$i];4193my%tag=%$entry;4194my$comment=$tag{'subject'};4195my$comment_short;4196if(defined$comment) {4197$comment_short= chop_str($comment,30,5);4198}4199if($alternate) {4200print"<tr class=\"dark\">\n";4201}else{4202print"<tr class=\"light\">\n";4203}4204$alternate^=1;4205if(defined$tag{'age'}) {4206print"<td><i>$tag{'age'}</i></td>\n";4207}else{4208print"<td></td>\n";4209}4210print"<td>".4211$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4212-class=>"list name"}, esc_html($tag{'name'})) .4213"</td>\n".4214"<td>";4215if(defined$comment) {4216print format_subject_html($comment,$comment_short,4217 href(action=>"tag", hash=>$tag{'id'}));4218}4219print"</td>\n".4220"<td class=\"selflink\">";4221if($tag{'type'}eq"tag") {4222print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4223}else{4224print" ";4225}4226print"</td>\n".4227"<td class=\"link\">"." | ".4228$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4229if($tag{'reftype'}eq"commit") {4230print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4231" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4232}elsif($tag{'reftype'}eq"blob") {4233print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4234}4235print"</td>\n".4236"</tr>";4237}4238if(defined$extra) {4239print"<tr>\n".4240"<td colspan=\"5\">$extra</td>\n".4241"</tr>\n";4242}4243print"</table>\n";4244}42454246sub git_heads_body {4247# uses global variable $project4248my($headlist,$head,$from,$to,$extra) =@_;4249$from=0unlessdefined$from;4250$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);42514252print"<table class=\"heads\">\n";4253my$alternate=1;4254for(my$i=$from;$i<=$to;$i++) {4255my$entry=$headlist->[$i];4256my%ref=%$entry;4257my$curr=$ref{'id'}eq$head;4258if($alternate) {4259print"<tr class=\"dark\">\n";4260}else{4261print"<tr class=\"light\">\n";4262}4263$alternate^=1;4264print"<td><i>$ref{'age'}</i></td>\n".4265($curr?"<td class=\"current_head\">":"<td>") .4266$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4267-class=>"list name"},esc_html($ref{'name'})) .4268"</td>\n".4269"<td class=\"link\">".4270$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4271$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4272$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4273"</td>\n".4274"</tr>";4275}4276if(defined$extra) {4277print"<tr>\n".4278"<td colspan=\"3\">$extra</td>\n".4279"</tr>\n";4280}4281print"</table>\n";4282}42834284sub git_search_grep_body {4285my($commitlist,$from,$to,$extra) =@_;4286$from=0unlessdefined$from;4287$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);42884289print"<table class=\"commit_search\">\n";4290my$alternate=1;4291for(my$i=$from;$i<=$to;$i++) {4292my%co= %{$commitlist->[$i]};4293if(!%co) {4294next;4295}4296my$commit=$co{'id'};4297if($alternate) {4298print"<tr class=\"dark\">\n";4299}else{4300print"<tr class=\"light\">\n";4301}4302$alternate^=1;4303my$author= chop_and_escape_str($co{'author_name'},15,5);4304print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4305"<td><i>".$author."</i></td>\n".4306"<td>".4307$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4308-class=>"list subject"},4309 chop_and_escape_str($co{'title'},50) ."<br/>");4310my$comment=$co{'comment'};4311foreachmy$line(@$comment) {4312if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4313my($lead,$match,$trail) = ($1,$2,$3);4314$match= chop_str($match,70,5,'center');4315my$contextlen=int((80-length($match))/2);4316$contextlen=30if($contextlen>30);4317$lead= chop_str($lead,$contextlen,10,'left');4318$trail= chop_str($trail,$contextlen,10,'right');43194320$lead= esc_html($lead);4321$match= esc_html($match);4322$trail= esc_html($trail);43234324print"$lead<span class=\"match\">$match</span>$trail<br />";4325}4326}4327print"</td>\n".4328"<td class=\"link\">".4329$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4330" | ".4331$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4332" | ".4333$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4334print"</td>\n".4335"</tr>\n";4336}4337if(defined$extra) {4338print"<tr>\n".4339"<td colspan=\"3\">$extra</td>\n".4340"</tr>\n";4341}4342print"</table>\n";4343}43444345## ======================================================================4346## ======================================================================4347## actions43484349sub git_project_list {4350my$order=$input_params{'order'};4351if(defined$order&&$order!~m/none|project|descr|owner|age/) {4352 die_error(400,"Unknown order parameter");4353}43544355my@list= git_get_projects_list();4356if(!@list) {4357 die_error(404,"No projects found");4358}43594360 git_header_html();4361if(-f $home_text) {4362print"<div class=\"index_include\">\n";4363 insert_file($home_text);4364print"</div>\n";4365}4366print$cgi->startform(-method=>"get") .4367"<p class=\"projsearch\">Search:\n".4368$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4369"</p>".4370$cgi->end_form() ."\n";4371 git_project_list_body(\@list,$order);4372 git_footer_html();4373}43744375sub git_forks {4376my$order=$input_params{'order'};4377if(defined$order&&$order!~m/none|project|descr|owner|age/) {4378 die_error(400,"Unknown order parameter");4379}43804381my@list= git_get_projects_list($project);4382if(!@list) {4383 die_error(404,"No forks found");4384}43854386 git_header_html();4387 git_print_page_nav('','');4388 git_print_header_div('summary',"$projectforks");4389 git_project_list_body(\@list,$order);4390 git_footer_html();4391}43924393sub git_project_index {4394my@projects= git_get_projects_list($project);43954396print$cgi->header(4397-type =>'text/plain',4398-charset =>'utf-8',4399-content_disposition =>'inline; filename="index.aux"');44004401foreachmy$pr(@projects) {4402if(!exists$pr->{'owner'}) {4403$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4404}44054406my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4407# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4408$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4409$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4410$path=~s/ /\+/g;4411$owner=~s/ /\+/g;44124413print"$path$owner\n";4414}4415}44164417sub git_summary {4418my$descr= git_get_project_description($project) ||"none";4419my%co= parse_commit("HEAD");4420my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4421my$head=$co{'id'};44224423my$owner= git_get_project_owner($project);44244425my$refs= git_get_references();4426# These get_*_list functions return one more to allow us to see if4427# there are more ...4428my@taglist= git_get_tags_list(16);4429my@headlist= git_get_heads_list(16);4430my@forklist;4431my$check_forks= gitweb_check_feature('forks');44324433if($check_forks) {4434@forklist= git_get_projects_list($project);4435}44364437 git_header_html();4438 git_print_page_nav('summary','',$head);44394440print"<div class=\"title\"> </div>\n";4441print"<table class=\"projects_list\">\n".4442"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4443"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4444if(defined$cd{'rfc2822'}) {4445print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4446}44474448# use per project git URL list in $projectroot/$project/cloneurl4449# or make project git URL from git base URL and project name4450my$url_tag="URL";4451my@url_list= git_get_project_url_list($project);4452@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4453foreachmy$git_url(@url_list) {4454next unless$git_url;4455print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4456$url_tag="";4457}44584459# Tag cloud4460my$show_ctags= gitweb_check_feature('ctags');4461if($show_ctags) {4462my$ctags= git_get_project_ctags($project);4463my$cloud= git_populate_project_tagcloud($ctags);4464print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4465print"</td>\n<td>"unless%$ctags;4466print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4467print"</td>\n<td>"if%$ctags;4468print git_show_project_tagcloud($cloud,48);4469print"</td></tr>";4470}44714472print"</table>\n";44734474if(-s "$projectroot/$project/README.html") {4475print"<div class=\"title\">readme</div>\n".4476"<div class=\"readme\">\n";4477 insert_file("$projectroot/$project/README.html");4478print"\n</div>\n";# class="readme"4479}44804481# we need to request one more than 16 (0..15) to check if4482# those 16 are all4483my@commitlist=$head? parse_commits($head,17) : ();4484if(@commitlist) {4485 git_print_header_div('shortlog');4486 git_shortlog_body(\@commitlist,0,15,$refs,4487$#commitlist<=15?undef:4488$cgi->a({-href => href(action=>"shortlog")},"..."));4489}44904491if(@taglist) {4492 git_print_header_div('tags');4493 git_tags_body(\@taglist,0,15,4494$#taglist<=15?undef:4495$cgi->a({-href => href(action=>"tags")},"..."));4496}44974498if(@headlist) {4499 git_print_header_div('heads');4500 git_heads_body(\@headlist,$head,0,15,4501$#headlist<=15?undef:4502$cgi->a({-href => href(action=>"heads")},"..."));4503}45044505if(@forklist) {4506 git_print_header_div('forks');4507 git_project_list_body(\@forklist,'age',0,15,4508$#forklist<=15?undef:4509$cgi->a({-href => href(action=>"forks")},"..."),4510'no_header');4511}45124513 git_footer_html();4514}45154516sub git_tag {4517my$head= git_get_head_hash($project);4518 git_header_html();4519 git_print_page_nav('','',$head,undef,$head);4520my%tag= parse_tag($hash);45214522if(!%tag) {4523 die_error(404,"Unknown tag object");4524}45254526 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4527print"<div class=\"title_text\">\n".4528"<table class=\"object_header\">\n".4529"<tr>\n".4530"<td>object</td>\n".4531"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4532$tag{'object'}) ."</td>\n".4533"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4534$tag{'type'}) ."</td>\n".4535"</tr>\n";4536if(defined($tag{'author'})) {4537my%ad= parse_date($tag{'epoch'},$tag{'tz'});4538print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4539print"<tr><td></td><td>".$ad{'rfc2822'} .4540sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4541"</td></tr>\n";4542}4543print"</table>\n\n".4544"</div>\n";4545print"<div class=\"page_body\">";4546my$comment=$tag{'comment'};4547foreachmy$line(@$comment) {4548chomp$line;4549print esc_html($line, -nbsp=>1) ."<br/>\n";4550}4551print"</div>\n";4552 git_footer_html();4553}45544555sub git_blame {4556# permissions4557 gitweb_check_feature('blame')4558or die_error(403,"Blame view not allowed");45594560# error checking4561 die_error(400,"No file name given")unless$file_name;4562$hash_base||= git_get_head_hash($project);4563 die_error(404,"Couldn't find base commit")unless$hash_base;4564my%co= parse_commit($hash_base)4565or die_error(404,"Commit not found");4566my$ftype="blob";4567if(!defined$hash) {4568$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4569or die_error(404,"Error looking up file");4570}else{4571$ftype= git_get_type($hash);4572if($ftype!~"blob") {4573 die_error(400,"Object is not a blob");4574}4575}45764577# run git-blame --porcelain4578open my$fd,"-|", git_cmd(),"blame",'-p',4579$hash_base,'--',$file_name4580or die_error(500,"Open git-blame failed");45814582# page header4583 git_header_html();4584my$formats_nav=4585$cgi->a({-href => href(action=>"blob", -replay=>1)},4586"blob") .4587" | ".4588$cgi->a({-href => href(action=>"history", -replay=>1)},4589"history") .4590" | ".4591$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4592"HEAD");4593 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4594 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4595 git_print_page_path($file_name,$ftype,$hash_base);45964597# page body4598my@rev_color=qw(light2 dark2);4599my$num_colors=scalar(@rev_color);4600my$current_color=0;4601my%metainfo= ();46024603print<<HTML;4604<div class="page_body">4605<table class="blame">4606<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4607HTML4608 LINE:4609while(my$line= <$fd>) {4610chomp$line;4611# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4612# no <lines in group> for subsequent lines in group of lines4613my($full_rev,$orig_lineno,$lineno,$group_size) =4614($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4615if(!exists$metainfo{$full_rev}) {4616$metainfo{$full_rev} = {};4617}4618my$meta=$metainfo{$full_rev};4619my$data;4620while($data= <$fd>) {4621chomp$data;4622last if($data=~s/^\t//);# contents of line4623if($data=~/^(\S+) (.*)$/) {4624$meta->{$1} =$2;4625}4626}4627my$short_rev=substr($full_rev,0,8);4628my$author=$meta->{'author'};4629my%date=4630 parse_date($meta->{'author-time'},$meta->{'author-tz'});4631my$date=$date{'iso-tz'};4632if($group_size) {4633$current_color= ($current_color+1) %$num_colors;4634}4635print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4636if($group_size) {4637print"<td class=\"sha1\"";4638print" title=\"". esc_html($author) .",$date\"";4639print" rowspan=\"$group_size\""if($group_size>1);4640print">";4641print$cgi->a({-href => href(action=>"commit",4642 hash=>$full_rev,4643 file_name=>$file_name)},4644 esc_html($short_rev));4645print"</td>\n";4646}4647my$parent_commit;4648if(!exists$meta->{'parent'}) {4649open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4650or die_error(500,"Open git-rev-parse failed");4651$parent_commit= <$dd>;4652close$dd;4653chomp($parent_commit);4654$meta->{'parent'} =$parent_commit;4655}else{4656$parent_commit=$meta->{'parent'};4657}4658my$blamed= href(action =>'blame',4659 file_name =>$meta->{'filename'},4660 hash_base =>$parent_commit);4661print"<td class=\"linenr\">";4662print$cgi->a({ -href =>"$blamed#l$orig_lineno",4663-class=>"linenr"},4664 esc_html($lineno));4665print"</td>";4666print"<td class=\"pre\">". esc_html($data) ."</td>\n";4667print"</tr>\n";4668}4669print"</table>\n";4670print"</div>";4671close$fd4672or print"Reading blob failed\n";46734674# page footer4675 git_footer_html();4676}46774678sub git_tags {4679my$head= git_get_head_hash($project);4680 git_header_html();4681 git_print_page_nav('','',$head,undef,$head);4682 git_print_header_div('summary',$project);46834684my@tagslist= git_get_tags_list();4685if(@tagslist) {4686 git_tags_body(\@tagslist);4687}4688 git_footer_html();4689}46904691sub git_heads {4692my$head= git_get_head_hash($project);4693 git_header_html();4694 git_print_page_nav('','',$head,undef,$head);4695 git_print_header_div('summary',$project);46964697my@headslist= git_get_heads_list();4698if(@headslist) {4699 git_heads_body(\@headslist,$head);4700}4701 git_footer_html();4702}47034704sub git_blob_plain {4705my$type=shift;4706my$expires;47074708if(!defined$hash) {4709if(defined$file_name) {4710my$base=$hash_base|| git_get_head_hash($project);4711$hash= git_get_hash_by_path($base,$file_name,"blob")4712or die_error(404,"Cannot find file");4713}else{4714 die_error(400,"No file name defined");4715}4716}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4717# blobs defined by non-textual hash id's can be cached4718$expires="+1d";4719}47204721open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4722or die_error(500,"Open git-cat-file blob '$hash' failed");47234724# content-type (can include charset)4725$type= blob_contenttype($fd,$file_name,$type);47264727# "save as" filename, even when no $file_name is given4728my$save_as="$hash";4729if(defined$file_name) {4730$save_as=$file_name;4731}elsif($type=~m/^text\//) {4732$save_as.='.txt';4733}47344735print$cgi->header(4736-type =>$type,4737-expires =>$expires,4738-content_disposition =>'inline; filename="'.$save_as.'"');4739undef$/;4740binmode STDOUT,':raw';4741print<$fd>;4742binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4743$/="\n";4744close$fd;4745}47464747sub git_blob {4748my$expires;47494750if(!defined$hash) {4751if(defined$file_name) {4752my$base=$hash_base|| git_get_head_hash($project);4753$hash= git_get_hash_by_path($base,$file_name,"blob")4754or die_error(404,"Cannot find file");4755}else{4756 die_error(400,"No file name defined");4757}4758}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4759# blobs defined by non-textual hash id's can be cached4760$expires="+1d";4761}47624763my$have_blame= gitweb_check_feature('blame');4764open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4765or die_error(500,"Couldn't cat$file_name,$hash");4766my$mimetype= blob_mimetype($fd,$file_name);4767if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4768close$fd;4769return git_blob_plain($mimetype);4770}4771# we can have blame only for text/* mimetype4772$have_blame&&= ($mimetype=~m!^text/!);47734774 git_header_html(undef,$expires);4775my$formats_nav='';4776if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4777if(defined$file_name) {4778if($have_blame) {4779$formats_nav.=4780$cgi->a({-href => href(action=>"blame", -replay=>1)},4781"blame") .4782" | ";4783}4784$formats_nav.=4785$cgi->a({-href => href(action=>"history", -replay=>1)},4786"history") .4787" | ".4788$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4789"raw") .4790" | ".4791$cgi->a({-href => href(action=>"blob",4792 hash_base=>"HEAD", file_name=>$file_name)},4793"HEAD");4794}else{4795$formats_nav.=4796$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4797"raw");4798}4799 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4800 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4801}else{4802print"<div class=\"page_nav\">\n".4803"<br/><br/></div>\n".4804"<div class=\"title\">$hash</div>\n";4805}4806 git_print_page_path($file_name,"blob",$hash_base);4807print"<div class=\"page_body\">\n";4808if($mimetype=~m!^image/!) {4809print qq!<img type="$mimetype"!;4810if($file_name) {4811print qq! alt="$file_name" title="$file_name"!;4812}4813print qq! src="! .4814 href(action=>"blob_plain", hash=>$hash,4815 hash_base=>$hash_base, file_name=>$file_name) .4816 qq!"/>\n!;4817}else{4818my$nr;4819while(my$line= <$fd>) {4820chomp$line;4821$nr++;4822$line= untabify($line);4823printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4824$nr,$nr,$nr, esc_html($line, -nbsp=>1);4825}4826}4827close$fd4828or print"Reading blob failed.\n";4829print"</div>";4830 git_footer_html();4831}48324833sub git_tree {4834if(!defined$hash_base) {4835$hash_base="HEAD";4836}4837if(!defined$hash) {4838if(defined$file_name) {4839$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4840}else{4841$hash=$hash_base;4842}4843}4844 die_error(404,"No such tree")unlessdefined($hash);4845$/="\0";4846open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4847or die_error(500,"Open git-ls-tree failed");4848my@entries=map{chomp;$_} <$fd>;4849close$fdor die_error(404,"Reading tree failed");4850$/="\n";48514852my$refs= git_get_references();4853my$ref= format_ref_marker($refs,$hash_base);4854 git_header_html();4855my$basedir='';4856my$have_blame= gitweb_check_feature('blame');4857if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4858my@views_nav= ();4859if(defined$file_name) {4860push@views_nav,4861$cgi->a({-href => href(action=>"history", -replay=>1)},4862"history"),4863$cgi->a({-href => href(action=>"tree",4864 hash_base=>"HEAD", file_name=>$file_name)},4865"HEAD"),4866}4867my$snapshot_links= format_snapshot_links($hash);4868if(defined$snapshot_links) {4869# FIXME: Should be available when we have no hash base as well.4870push@views_nav,$snapshot_links;4871}4872 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4873 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4874}else{4875undef$hash_base;4876print"<div class=\"page_nav\">\n";4877print"<br/><br/></div>\n";4878print"<div class=\"title\">$hash</div>\n";4879}4880if(defined$file_name) {4881$basedir=$file_name;4882if($basedirne''&&substr($basedir, -1)ne'/') {4883$basedir.='/';4884}4885 git_print_page_path($file_name,'tree',$hash_base);4886}4887print"<div class=\"page_body\">\n";4888print"<table class=\"tree\">\n";4889my$alternate=1;4890# '..' (top directory) link if possible4891if(defined$hash_base&&4892defined$file_name&&$file_name=~m![^/]+$!) {4893if($alternate) {4894print"<tr class=\"dark\">\n";4895}else{4896print"<tr class=\"light\">\n";4897}4898$alternate^=1;48994900my$up=$file_name;4901$up=~s!/?[^/]+$!!;4902undef$upunless$up;4903# based on git_print_tree_entry4904print'<td class="mode">'. mode_str('040000') ."</td>\n";4905print'<td class="list">';4906print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4907 file_name=>$up)},4908"..");4909print"</td>\n";4910print"<td class=\"link\"></td>\n";49114912print"</tr>\n";4913}4914foreachmy$line(@entries) {4915my%t= parse_ls_tree_line($line, -z =>1);49164917if($alternate) {4918print"<tr class=\"dark\">\n";4919}else{4920print"<tr class=\"light\">\n";4921}4922$alternate^=1;49234924 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);49254926print"</tr>\n";4927}4928print"</table>\n".4929"</div>";4930 git_footer_html();4931}49324933sub git_snapshot {4934my$format=$input_params{'snapshot_format'};4935if(!@snapshot_fmts) {4936 die_error(403,"Snapshots not allowed");4937}4938# default to first supported snapshot format4939$format||=$snapshot_fmts[0];4940if($format!~m/^[a-z0-9]+$/) {4941 die_error(400,"Invalid snapshot format parameter");4942}elsif(!exists($known_snapshot_formats{$format})) {4943 die_error(400,"Unknown snapshot format");4944}elsif(!grep($_eq$format,@snapshot_fmts)) {4945 die_error(403,"Unsupported snapshot format");4946}49474948if(!defined$hash) {4949$hash= git_get_head_hash($project);4950}49514952my$name=$project;4953$name=~ s,([^/])/*\.git$,$1,;4954$name= basename($name);4955my$filename= to_utf8($name);4956$name=~s/\047/\047\\\047\047/g;4957my$cmd;4958$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";4959$cmd= quote_command(4960 git_cmd(),'archive',4961"--format=$known_snapshot_formats{$format}{'format'}",4962"--prefix=$name/",$hash);4963if(exists$known_snapshot_formats{$format}{'compressor'}) {4964$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});4965}49664967print$cgi->header(4968-type =>$known_snapshot_formats{$format}{'type'},4969-content_disposition =>'inline; filename="'."$filename".'"',4970-status =>'200 OK');49714972open my$fd,"-|",$cmd4973or die_error(500,"Execute git-archive failed");4974binmode STDOUT,':raw';4975print<$fd>;4976binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4977close$fd;4978}49794980sub git_log {4981my$head= git_get_head_hash($project);4982if(!defined$hash) {4983$hash=$head;4984}4985if(!defined$page) {4986$page=0;4987}4988my$refs= git_get_references();49894990my@commitlist= parse_commits($hash,101, (100*$page));49914992my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);49934994 git_header_html();4995 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);49964997if(!@commitlist) {4998my%co= parse_commit($hash);49995000 git_print_header_div('summary',$project);5001print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5002}5003my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5004for(my$i=0;$i<=$to;$i++) {5005my%co= %{$commitlist[$i]};5006next if!%co;5007my$commit=$co{'id'};5008my$ref= format_ref_marker($refs,$commit);5009my%ad= parse_date($co{'author_epoch'});5010 git_print_header_div('commit',5011"<span class=\"age\">$co{'age_string'}</span>".5012 esc_html($co{'title'}) .$ref,5013$commit);5014print"<div class=\"title_text\">\n".5015"<div class=\"log_link\">\n".5016$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5017" | ".5018$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5019" | ".5020$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5021"<br/>\n".5022"</div>\n".5023"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".5024"</div>\n";50255026print"<div class=\"log_body\">\n";5027 git_print_log($co{'comment'}, -final_empty_line=>1);5028print"</div>\n";5029}5030if($#commitlist>=100) {5031print"<div class=\"page_nav\">\n";5032print$cgi->a({-href => href(-replay=>1, page=>$page+1),5033-accesskey =>"n", -title =>"Alt-n"},"next");5034print"</div>\n";5035}5036 git_footer_html();5037}50385039sub git_commit {5040$hash||=$hash_base||"HEAD";5041my%co= parse_commit($hash)5042or die_error(404,"Unknown commit object");5043my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5044my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});50455046my$parent=$co{'parent'};5047my$parents=$co{'parents'};# listref50485049# we need to prepare $formats_nav before any parameter munging5050my$formats_nav;5051if(!defined$parent) {5052# --root commitdiff5053$formats_nav.='(initial)';5054}elsif(@$parents==1) {5055# single parent commit5056$formats_nav.=5057'(parent: '.5058$cgi->a({-href => href(action=>"commit",5059 hash=>$parent)},5060 esc_html(substr($parent,0,7))) .5061')';5062}else{5063# merge commit5064$formats_nav.=5065'(merge: '.5066join(' ',map{5067$cgi->a({-href => href(action=>"commit",5068 hash=>$_)},5069 esc_html(substr($_,0,7)));5070}@$parents) .5071')';5072}50735074if(!defined$parent) {5075$parent="--root";5076}5077my@difftree;5078open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5079@diff_opts,5080(@$parents<=1?$parent:'-c'),5081$hash,"--"5082or die_error(500,"Open git-diff-tree failed");5083@difftree=map{chomp;$_} <$fd>;5084close$fdor die_error(404,"Reading git-diff-tree failed");50855086# non-textual hash id's can be cached5087my$expires;5088if($hash=~m/^[0-9a-fA-F]{40}$/) {5089$expires="+1d";5090}5091my$refs= git_get_references();5092my$ref= format_ref_marker($refs,$co{'id'});50935094 git_header_html(undef,$expires);5095 git_print_page_nav('commit','',5096$hash,$co{'tree'},$hash,5097$formats_nav);50985099if(defined$co{'parent'}) {5100 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5101}else{5102 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5103}5104print"<div class=\"title_text\">\n".5105"<table class=\"object_header\">\n";5106print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5107"<tr>".5108"<td></td><td>$ad{'rfc2822'}";5109if($ad{'hour_local'} <6) {5110printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5111$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5112}else{5113printf(" (%02d:%02d%s)",5114$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5115}5116print"</td>".5117"</tr>\n";5118print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5119print"<tr><td></td><td>$cd{'rfc2822'}".5120sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5121"</td></tr>\n";5122print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5123print"<tr>".5124"<td>tree</td>".5125"<td class=\"sha1\">".5126$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5127class=>"list"},$co{'tree'}) .5128"</td>".5129"<td class=\"link\">".5130$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5131"tree");5132my$snapshot_links= format_snapshot_links($hash);5133if(defined$snapshot_links) {5134print" | ".$snapshot_links;5135}5136print"</td>".5137"</tr>\n";51385139foreachmy$par(@$parents) {5140print"<tr>".5141"<td>parent</td>".5142"<td class=\"sha1\">".5143$cgi->a({-href => href(action=>"commit", hash=>$par),5144class=>"list"},$par) .5145"</td>".5146"<td class=\"link\">".5147$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5148" | ".5149$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5150"</td>".5151"</tr>\n";5152}5153print"</table>".5154"</div>\n";51555156print"<div class=\"page_body\">\n";5157 git_print_log($co{'comment'});5158print"</div>\n";51595160 git_difftree_body(\@difftree,$hash,@$parents);51615162 git_footer_html();5163}51645165sub git_object {5166# object is defined by:5167# - hash or hash_base alone5168# - hash_base and file_name5169my$type;51705171# - hash or hash_base alone5172if($hash|| ($hash_base&& !defined$file_name)) {5173my$object_id=$hash||$hash_base;51745175open my$fd,"-|", quote_command(5176 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5177or die_error(404,"Object does not exist");5178$type= <$fd>;5179chomp$type;5180close$fd5181or die_error(404,"Object does not exist");51825183# - hash_base and file_name5184}elsif($hash_base&&defined$file_name) {5185$file_name=~ s,/+$,,;51865187system(git_cmd(),"cat-file",'-e',$hash_base) ==05188or die_error(404,"Base object does not exist");51895190# here errors should not hapen5191open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5192or die_error(500,"Open git-ls-tree failed");5193my$line= <$fd>;5194close$fd;51955196#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5197unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5198 die_error(404,"File or directory for given base does not exist");5199}5200$type=$2;5201$hash=$3;5202}else{5203 die_error(400,"Not enough information to find object");5204}52055206print$cgi->redirect(-uri => href(action=>$type, -full=>1,5207 hash=>$hash, hash_base=>$hash_base,5208 file_name=>$file_name),5209-status =>'302 Found');5210}52115212sub git_blobdiff {5213my$format=shift||'html';52145215my$fd;5216my@difftree;5217my%diffinfo;5218my$expires;52195220# preparing $fd and %diffinfo for git_patchset_body5221# new style URI5222if(defined$hash_base&&defined$hash_parent_base) {5223if(defined$file_name) {5224# read raw output5225open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5226$hash_parent_base,$hash_base,5227"--", (defined$file_parent?$file_parent: ()),$file_name5228or die_error(500,"Open git-diff-tree failed");5229@difftree=map{chomp;$_} <$fd>;5230close$fd5231or die_error(404,"Reading git-diff-tree failed");5232@difftree5233or die_error(404,"Blob diff not found");52345235}elsif(defined$hash&&5236$hash=~/[0-9a-fA-F]{40}/) {5237# try to find filename from $hash52385239# read filtered raw output5240open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5241$hash_parent_base,$hash_base,"--"5242or die_error(500,"Open git-diff-tree failed");5243@difftree=5244# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5245# $hash == to_id5246grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5247map{chomp;$_} <$fd>;5248close$fd5249or die_error(404,"Reading git-diff-tree failed");5250@difftree5251or die_error(404,"Blob diff not found");52525253}else{5254 die_error(400,"Missing one of the blob diff parameters");5255}52565257if(@difftree>1) {5258 die_error(400,"Ambiguous blob diff specification");5259}52605261%diffinfo= parse_difftree_raw_line($difftree[0]);5262$file_parent||=$diffinfo{'from_file'} ||$file_name;5263$file_name||=$diffinfo{'to_file'};52645265$hash_parent||=$diffinfo{'from_id'};5266$hash||=$diffinfo{'to_id'};52675268# non-textual hash id's can be cached5269if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5270$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5271$expires='+1d';5272}52735274# open patch output5275open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5276'-p', ($formateq'html'?"--full-index": ()),5277$hash_parent_base,$hash_base,5278"--", (defined$file_parent?$file_parent: ()),$file_name5279or die_error(500,"Open git-diff-tree failed");5280}52815282# old/legacy style URI -- not generated anymore since 1.4.3.5283if(!%diffinfo) {5284 die_error('404 Not Found',"Missing one of the blob diff parameters")5285}52865287# header5288if($formateq'html') {5289my$formats_nav=5290$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5291"raw");5292 git_header_html(undef,$expires);5293if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5294 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5295 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5296}else{5297print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5298print"<div class=\"title\">$hashvs$hash_parent</div>\n";5299}5300if(defined$file_name) {5301 git_print_page_path($file_name,"blob",$hash_base);5302}else{5303print"<div class=\"page_path\"></div>\n";5304}53055306}elsif($formateq'plain') {5307print$cgi->header(5308-type =>'text/plain',5309-charset =>'utf-8',5310-expires =>$expires,5311-content_disposition =>'inline; filename="'."$file_name".'.patch"');53125313print"X-Git-Url: ".$cgi->self_url() ."\n\n";53145315}else{5316 die_error(400,"Unknown blobdiff format");5317}53185319# patch5320if($formateq'html') {5321print"<div class=\"page_body\">\n";53225323 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5324close$fd;53255326print"</div>\n";# class="page_body"5327 git_footer_html();53285329}else{5330while(my$line= <$fd>) {5331$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5332$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;53335334print$line;53355336last if$line=~m!^\+\+\+!;5337}5338local$/=undef;5339print<$fd>;5340close$fd;5341}5342}53435344sub git_blobdiff_plain {5345 git_blobdiff('plain');5346}53475348sub git_commitdiff {5349my$format=shift||'html';5350$hash||=$hash_base||"HEAD";5351my%co= parse_commit($hash)5352or die_error(404,"Unknown commit object");53535354# choose format for commitdiff for merge5355if(!defined$hash_parent&& @{$co{'parents'}} >1) {5356$hash_parent='--cc';5357}5358# we need to prepare $formats_nav before almost any parameter munging5359my$formats_nav;5360if($formateq'html') {5361$formats_nav=5362$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5363"raw");53645365if(defined$hash_parent&&5366$hash_parentne'-c'&&$hash_parentne'--cc') {5367# commitdiff with two commits given5368my$hash_parent_short=$hash_parent;5369if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5370$hash_parent_short=substr($hash_parent,0,7);5371}5372$formats_nav.=5373' (from';5374for(my$i=0;$i< @{$co{'parents'}};$i++) {5375if($co{'parents'}[$i]eq$hash_parent) {5376$formats_nav.=' parent '. ($i+1);5377last;5378}5379}5380$formats_nav.=': '.5381$cgi->a({-href => href(action=>"commitdiff",5382 hash=>$hash_parent)},5383 esc_html($hash_parent_short)) .5384')';5385}elsif(!$co{'parent'}) {5386# --root commitdiff5387$formats_nav.=' (initial)';5388}elsif(scalar@{$co{'parents'}} ==1) {5389# single parent commit5390$formats_nav.=5391' (parent: '.5392$cgi->a({-href => href(action=>"commitdiff",5393 hash=>$co{'parent'})},5394 esc_html(substr($co{'parent'},0,7))) .5395')';5396}else{5397# merge commit5398if($hash_parenteq'--cc') {5399$formats_nav.=' | '.5400$cgi->a({-href => href(action=>"commitdiff",5401 hash=>$hash, hash_parent=>'-c')},5402'combined');5403}else{# $hash_parent eq '-c'5404$formats_nav.=' | '.5405$cgi->a({-href => href(action=>"commitdiff",5406 hash=>$hash, hash_parent=>'--cc')},5407'compact');5408}5409$formats_nav.=5410' (merge: '.5411join(' ',map{5412$cgi->a({-href => href(action=>"commitdiff",5413 hash=>$_)},5414 esc_html(substr($_,0,7)));5415} @{$co{'parents'}} ) .5416')';5417}5418}54195420my$hash_parent_param=$hash_parent;5421if(!defined$hash_parent_param) {5422# --cc for multiple parents, --root for parentless5423$hash_parent_param=5424@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5425}54265427# read commitdiff5428my$fd;5429my@difftree;5430if($formateq'html') {5431open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5432"--no-commit-id","--patch-with-raw","--full-index",5433$hash_parent_param,$hash,"--"5434or die_error(500,"Open git-diff-tree failed");54355436while(my$line= <$fd>) {5437chomp$line;5438# empty line ends raw part of diff-tree output5439last unless$line;5440push@difftree,scalar parse_difftree_raw_line($line);5441}54425443}elsif($formateq'plain') {5444open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5445'-p',$hash_parent_param,$hash,"--"5446or die_error(500,"Open git-diff-tree failed");54475448}else{5449 die_error(400,"Unknown commitdiff format");5450}54515452# non-textual hash id's can be cached5453my$expires;5454if($hash=~m/^[0-9a-fA-F]{40}$/) {5455$expires="+1d";5456}54575458# write commit message5459if($formateq'html') {5460my$refs= git_get_references();5461my$ref= format_ref_marker($refs,$co{'id'});54625463 git_header_html(undef,$expires);5464 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5465 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5466 git_print_authorship(\%co);5467print"<div class=\"page_body\">\n";5468if(@{$co{'comment'}} >1) {5469print"<div class=\"log\">\n";5470 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5471print"</div>\n";# class="log"5472}54735474}elsif($formateq'plain') {5475my$refs= git_get_references("tags");5476my$tagname= git_get_rev_name_tags($hash);5477my$filename= basename($project) ."-$hash.patch";54785479print$cgi->header(5480-type =>'text/plain',5481-charset =>'utf-8',5482-expires =>$expires,5483-content_disposition =>'inline; filename="'."$filename".'"');5484my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5485print"From: ". to_utf8($co{'author'}) ."\n";5486print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5487print"Subject: ". to_utf8($co{'title'}) ."\n";54885489print"X-Git-Tag:$tagname\n"if$tagname;5490print"X-Git-Url: ".$cgi->self_url() ."\n\n";54915492foreachmy$line(@{$co{'comment'}}) {5493print to_utf8($line) ."\n";5494}5495print"---\n\n";5496}54975498# write patch5499if($formateq'html') {5500my$use_parents= !defined$hash_parent||5501$hash_parenteq'-c'||$hash_parenteq'--cc';5502 git_difftree_body(\@difftree,$hash,5503$use_parents? @{$co{'parents'}} :$hash_parent);5504print"<br/>\n";55055506 git_patchset_body($fd, \@difftree,$hash,5507$use_parents? @{$co{'parents'}} :$hash_parent);5508close$fd;5509print"</div>\n";# class="page_body"5510 git_footer_html();55115512}elsif($formateq'plain') {5513local$/=undef;5514print<$fd>;5515close$fd5516or print"Reading git-diff-tree failed\n";5517}5518}55195520sub git_commitdiff_plain {5521 git_commitdiff('plain');5522}55235524sub git_history {5525if(!defined$hash_base) {5526$hash_base= git_get_head_hash($project);5527}5528if(!defined$page) {5529$page=0;5530}5531my$ftype;5532my%co= parse_commit($hash_base)5533or die_error(404,"Unknown commit object");55345535my$refs= git_get_references();5536my$limit=sprintf("--max-count=%i", (100* ($page+1)));55375538my@commitlist= parse_commits($hash_base,101, (100*$page),5539$file_name,"--full-history")5540or die_error(404,"No such file or directory on given branch");55415542if(!defined$hash&&defined$file_name) {5543# some commits could have deleted file in question,5544# and not have it in tree, but one of them has to have it5545for(my$i=0;$i<=@commitlist;$i++) {5546$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5547last ifdefined$hash;5548}5549}5550if(defined$hash) {5551$ftype= git_get_type($hash);5552}5553if(!defined$ftype) {5554 die_error(500,"Unknown type of object");5555}55565557my$paging_nav='';5558if($page>0) {5559$paging_nav.=5560$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5561 file_name=>$file_name)},5562"first");5563$paging_nav.=" ⋅ ".5564$cgi->a({-href => href(-replay=>1, page=>$page-1),5565-accesskey =>"p", -title =>"Alt-p"},"prev");5566}else{5567$paging_nav.="first";5568$paging_nav.=" ⋅ prev";5569}5570my$next_link='';5571if($#commitlist>=100) {5572$next_link=5573$cgi->a({-href => href(-replay=>1, page=>$page+1),5574-accesskey =>"n", -title =>"Alt-n"},"next");5575$paging_nav.=" ⋅$next_link";5576}else{5577$paging_nav.=" ⋅ next";5578}55795580 git_header_html();5581 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5582 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5583 git_print_page_path($file_name,$ftype,$hash_base);55845585 git_history_body(\@commitlist,0,99,5586$refs,$hash_base,$ftype,$next_link);55875588 git_footer_html();5589}55905591sub git_search {5592 gitweb_check_feature('search')or die_error(403,"Search is disabled");5593if(!defined$searchtext) {5594 die_error(400,"Text field is empty");5595}5596if(!defined$hash) {5597$hash= git_get_head_hash($project);5598}5599my%co= parse_commit($hash);5600if(!%co) {5601 die_error(404,"Unknown commit object");5602}5603if(!defined$page) {5604$page=0;5605}56065607$searchtype||='commit';5608if($searchtypeeq'pickaxe') {5609# pickaxe may take all resources of your box and run for several minutes5610# with every query - so decide by yourself how public you make this feature5611 gitweb_check_feature('pickaxe')5612or die_error(403,"Pickaxe is disabled");5613}5614if($searchtypeeq'grep') {5615 gitweb_check_feature('grep')5616or die_error(403,"Grep is disabled");5617}56185619 git_header_html();56205621if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5622my$greptype;5623if($searchtypeeq'commit') {5624$greptype="--grep=";5625}elsif($searchtypeeq'author') {5626$greptype="--author=";5627}elsif($searchtypeeq'committer') {5628$greptype="--committer=";5629}5630$greptype.=$searchtext;5631my@commitlist= parse_commits($hash,101, (100*$page),undef,5632$greptype,'--regexp-ignore-case',5633$search_use_regexp?'--extended-regexp':'--fixed-strings');56345635my$paging_nav='';5636if($page>0) {5637$paging_nav.=5638$cgi->a({-href => href(action=>"search", hash=>$hash,5639 searchtext=>$searchtext,5640 searchtype=>$searchtype)},5641"first");5642$paging_nav.=" ⋅ ".5643$cgi->a({-href => href(-replay=>1, page=>$page-1),5644-accesskey =>"p", -title =>"Alt-p"},"prev");5645}else{5646$paging_nav.="first";5647$paging_nav.=" ⋅ prev";5648}5649my$next_link='';5650if($#commitlist>=100) {5651$next_link=5652$cgi->a({-href => href(-replay=>1, page=>$page+1),5653-accesskey =>"n", -title =>"Alt-n"},"next");5654$paging_nav.=" ⋅$next_link";5655}else{5656$paging_nav.=" ⋅ next";5657}56585659if($#commitlist>=100) {5660}56615662 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5663 git_print_header_div('commit', esc_html($co{'title'}),$hash);5664 git_search_grep_body(\@commitlist,0,99,$next_link);5665}56665667if($searchtypeeq'pickaxe') {5668 git_print_page_nav('','',$hash,$co{'tree'},$hash);5669 git_print_header_div('commit', esc_html($co{'title'}),$hash);56705671print"<table class=\"pickaxe search\">\n";5672my$alternate=1;5673$/="\n";5674open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5675'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5676($search_use_regexp?'--pickaxe-regex': ());5677undef%co;5678my@files;5679while(my$line= <$fd>) {5680chomp$line;5681next unless$line;56825683my%set= parse_difftree_raw_line($line);5684if(defined$set{'commit'}) {5685# finish previous commit5686if(%co) {5687print"</td>\n".5688"<td class=\"link\">".5689$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5690" | ".5691$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5692print"</td>\n".5693"</tr>\n";5694}56955696if($alternate) {5697print"<tr class=\"dark\">\n";5698}else{5699print"<tr class=\"light\">\n";5700}5701$alternate^=1;5702%co= parse_commit($set{'commit'});5703my$author= chop_and_escape_str($co{'author_name'},15,5);5704print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5705"<td><i>$author</i></td>\n".5706"<td>".5707$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5708-class=>"list subject"},5709 chop_and_escape_str($co{'title'},50) ."<br/>");5710}elsif(defined$set{'to_id'}) {5711next if($set{'to_id'} =~m/^0{40}$/);57125713print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5714 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5715-class=>"list"},5716"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5717"<br/>\n";5718}5719}5720close$fd;57215722# finish last commit (warning: repetition!)5723if(%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}57325733print"</table>\n";5734}57355736if($searchtypeeq'grep') {5737 git_print_page_nav('','',$hash,$co{'tree'},$hash);5738 git_print_header_div('commit', esc_html($co{'title'}),$hash);57395740print"<table class=\"grep_search\">\n";5741my$alternate=1;5742my$matches=0;5743$/="\n";5744open my$fd,"-|", git_cmd(),'grep','-n',5745$search_use_regexp? ('-E','-i') :'-F',5746$searchtext,$co{'tree'};5747my$lastfile='';5748while(my$line= <$fd>) {5749chomp$line;5750my($file,$lno,$ltext,$binary);5751last if($matches++>1000);5752if($line=~/^Binary file (.+) matches$/) {5753$file=$1;5754$binary=1;5755}else{5756(undef,$file,$lno,$ltext) =split(/:/,$line,4);5757}5758if($filene$lastfile) {5759$lastfileand print"</td></tr>\n";5760if($alternate++) {5761print"<tr class=\"dark\">\n";5762}else{5763print"<tr class=\"light\">\n";5764}5765print"<td class=\"list\">".5766$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5767 file_name=>"$file"),5768-class=>"list"}, esc_path($file));5769print"</td><td>\n";5770$lastfile=$file;5771}5772if($binary) {5773print"<div class=\"binary\">Binary file</div>\n";5774}else{5775$ltext= untabify($ltext);5776if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5777$ltext= esc_html($1, -nbsp=>1);5778$ltext.='<span class="match">';5779$ltext.= esc_html($2, -nbsp=>1);5780$ltext.='</span>';5781$ltext.= esc_html($3, -nbsp=>1);5782}else{5783$ltext= esc_html($ltext, -nbsp=>1);5784}5785print"<div class=\"pre\">".5786$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5787 file_name=>"$file").'#l'.$lno,5788-class=>"linenr"},sprintf('%4i',$lno))5789.' '.$ltext."</div>\n";5790}5791}5792if($lastfile) {5793print"</td></tr>\n";5794if($matches>1000) {5795print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5796}5797}else{5798print"<div class=\"diff nodifferences\">No matches found</div>\n";5799}5800close$fd;58015802print"</table>\n";5803}5804 git_footer_html();5805}58065807sub git_search_help {5808 git_header_html();5809 git_print_page_nav('','',$hash,$hash,$hash);5810print<<EOT;5811<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5812regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5813the pattern entered is recognized as the POSIX extended5814<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5815insensitive).</p>5816<dl>5817<dt><b>commit</b></dt>5818<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5819EOT5820my$have_grep= gitweb_check_feature('grep');5821if($have_grep) {5822print<<EOT;5823<dt><b>grep</b></dt>5824<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5825 a different one) are searched for the given pattern. On large trees, this search can take5826a while and put some strain on the server, so please use it with some consideration. Note that5827due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5828case-sensitive.</dd>5829EOT5830}5831print<<EOT;5832<dt><b>author</b></dt>5833<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5834<dt><b>committer</b></dt>5835<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5836EOT5837my$have_pickaxe= gitweb_check_feature('pickaxe');5838if($have_pickaxe) {5839print<<EOT;5840<dt><b>pickaxe</b></dt>5841<dd>All commits that caused the string to appear or disappear from any file (changes that5842added, removed or "modified" the string) will be listed. This search can take a while and5843takes a lot of strain on the server, so please use it wisely. Note that since you may be5844interested even in changes just changing the case as well, this search is case sensitive.</dd>5845EOT5846}5847print"</dl>\n";5848 git_footer_html();5849}58505851sub git_shortlog {5852my$head= git_get_head_hash($project);5853if(!defined$hash) {5854$hash=$head;5855}5856if(!defined$page) {5857$page=0;5858}5859my$refs= git_get_references();58605861my$commit_hash=$hash;5862if(defined$hash_parent) {5863$commit_hash="$hash_parent..$hash";5864}5865my@commitlist= parse_commits($commit_hash,101, (100*$page));58665867my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5868my$next_link='';5869if($#commitlist>=100) {5870$next_link=5871$cgi->a({-href => href(-replay=>1, page=>$page+1),5872-accesskey =>"n", -title =>"Alt-n"},"next");5873}58745875 git_header_html();5876 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);5877 git_print_header_div('summary',$project);58785879 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);58805881 git_footer_html();5882}58835884## ......................................................................5885## feeds (RSS, Atom; OPML)58865887sub git_feed {5888my$format=shift||'atom';5889my$have_blame= gitweb_check_feature('blame');58905891# Atom: http://www.atomenabled.org/developers/syndication/5892# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ5893if($formatne'rss'&&$formatne'atom') {5894 die_error(400,"Unknown web feed format");5895}58965897# log/feed of current (HEAD) branch, log of given branch, history of file/directory5898my$head=$hash||'HEAD';5899my@commitlist= parse_commits($head,150,0,$file_name);59005901my%latest_commit;5902my%latest_date;5903my$content_type="application/$format+xml";5904if(defined$cgi->http('HTTP_ACCEPT') &&5905$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {5906# browser (feed reader) prefers text/xml5907$content_type='text/xml';5908}5909if(defined($commitlist[0])) {5910%latest_commit= %{$commitlist[0]};5911%latest_date= parse_date($latest_commit{'author_epoch'});5912print$cgi->header(5913-type =>$content_type,5914-charset =>'utf-8',5915-last_modified =>$latest_date{'rfc2822'});5916}else{5917print$cgi->header(5918-type =>$content_type,5919-charset =>'utf-8');5920}59215922# Optimization: skip generating the body if client asks only5923# for Last-Modified date.5924return if($cgi->request_method()eq'HEAD');59255926# header variables5927my$title="$site_name-$project/$action";5928my$feed_type='log';5929if(defined$hash) {5930$title.=" - '$hash'";5931$feed_type='branch log';5932if(defined$file_name) {5933$title.=" ::$file_name";5934$feed_type='history';5935}5936}elsif(defined$file_name) {5937$title.=" -$file_name";5938$feed_type='history';5939}5940$title.="$feed_type";5941my$descr= git_get_project_description($project);5942if(defined$descr) {5943$descr= esc_html($descr);5944}else{5945$descr="$project".5946($formateq'rss'?'RSS':'Atom') .5947" feed";5948}5949my$owner= git_get_project_owner($project);5950$owner= esc_html($owner);59515952#header5953my$alt_url;5954if(defined$file_name) {5955$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);5956}elsif(defined$hash) {5957$alt_url= href(-full=>1, action=>"log", hash=>$hash);5958}else{5959$alt_url= href(-full=>1, action=>"summary");5960}5961print qq!<?xml version="1.0" encoding="utf-8"?>\n!;5962if($formateq'rss') {5963print<<XML;5964<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">5965<channel>5966XML5967print"<title>$title</title>\n".5968"<link>$alt_url</link>\n".5969"<description>$descr</description>\n".5970"<language>en</language>\n";5971}elsif($formateq'atom') {5972print<<XML;5973<feed xmlns="http://www.w3.org/2005/Atom">5974XML5975print"<title>$title</title>\n".5976"<subtitle>$descr</subtitle>\n".5977'<link rel="alternate" type="text/html" href="'.5978$alt_url.'" />'."\n".5979'<link rel="self" type="'.$content_type.'" href="'.5980$cgi->self_url() .'" />'."\n".5981"<id>". href(-full=>1) ."</id>\n".5982# use project owner for feed author5983"<author><name>$owner</name></author>\n";5984if(defined$favicon) {5985print"<icon>". esc_url($favicon) ."</icon>\n";5986}5987if(defined$logo_url) {5988# not twice as wide as tall: 72 x 27 pixels5989print"<logo>". esc_url($logo) ."</logo>\n";5990}5991if(!%latest_date) {5992# dummy date to keep the feed valid until commits trickle in:5993print"<updated>1970-01-01T00:00:00Z</updated>\n";5994}else{5995print"<updated>$latest_date{'iso-8601'}</updated>\n";5996}5997}59985999# contents6000for(my$i=0;$i<=$#commitlist;$i++) {6001my%co= %{$commitlist[$i]};6002my$commit=$co{'id'};6003# we read 150, we always show 30 and the ones more recent than 48 hours6004if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6005last;6006}6007my%cd= parse_date($co{'author_epoch'});60086009# get list of changed files6010open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6011$co{'parent'} ||"--root",6012$co{'id'},"--", (defined$file_name?$file_name: ())6013ornext;6014my@difftree=map{chomp;$_} <$fd>;6015close$fd6016ornext;60176018# print element (entry, item)6019my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6020if($formateq'rss') {6021print"<item>\n".6022"<title>". esc_html($co{'title'}) ."</title>\n".6023"<author>". esc_html($co{'author'}) ."</author>\n".6024"<pubDate>$cd{'rfc2822'}</pubDate>\n".6025"<guid isPermaLink=\"true\">$co_url</guid>\n".6026"<link>$co_url</link>\n".6027"<description>". esc_html($co{'title'}) ."</description>\n".6028"<content:encoded>".6029"<![CDATA[\n";6030}elsif($formateq'atom') {6031print"<entry>\n".6032"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6033"<updated>$cd{'iso-8601'}</updated>\n".6034"<author>\n".6035" <name>". esc_html($co{'author_name'}) ."</name>\n";6036if($co{'author_email'}) {6037print" <email>". esc_html($co{'author_email'}) ."</email>\n";6038}6039print"</author>\n".6040# use committer for contributor6041"<contributor>\n".6042" <name>". esc_html($co{'committer_name'}) ."</name>\n";6043if($co{'committer_email'}) {6044print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6045}6046print"</contributor>\n".6047"<published>$cd{'iso-8601'}</published>\n".6048"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6049"<id>$co_url</id>\n".6050"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6051"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6052}6053my$comment=$co{'comment'};6054print"<pre>\n";6055foreachmy$line(@$comment) {6056$line= esc_html($line);6057print"$line\n";6058}6059print"</pre><ul>\n";6060foreachmy$difftree_line(@difftree) {6061my%difftree= parse_difftree_raw_line($difftree_line);6062next if!$difftree{'from_id'};60636064my$file=$difftree{'file'} ||$difftree{'to_file'};60656066print"<li>".6067"[".6068$cgi->a({-href => href(-full=>1, action=>"blobdiff",6069 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6070 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6071 file_name=>$file, file_parent=>$difftree{'from_file'}),6072-title =>"diff"},'D');6073if($have_blame) {6074print$cgi->a({-href => href(-full=>1, action=>"blame",6075 file_name=>$file, hash_base=>$commit),6076-title =>"blame"},'B');6077}6078# if this is not a feed of a file history6079if(!defined$file_name||$file_namene$file) {6080print$cgi->a({-href => href(-full=>1, action=>"history",6081 file_name=>$file, hash=>$commit),6082-title =>"history"},'H');6083}6084$file= esc_path($file);6085print"] ".6086"$file</li>\n";6087}6088if($formateq'rss') {6089print"</ul>]]>\n".6090"</content:encoded>\n".6091"</item>\n";6092}elsif($formateq'atom') {6093print"</ul>\n</div>\n".6094"</content>\n".6095"</entry>\n";6096}6097}60986099# end of feed6100if($formateq'rss') {6101print"</channel>\n</rss>\n";6102}elsif($formateq'atom') {6103print"</feed>\n";6104}6105}61066107sub git_rss {6108 git_feed('rss');6109}61106111sub git_atom {6112 git_feed('atom');6113}61146115sub git_opml {6116my@list= git_get_projects_list();61176118print$cgi->header(-type =>'text/xml', -charset =>'utf-8');6119print<<XML;6120<?xml version="1.0" encoding="utf-8"?>6121<opml version="1.0">6122<head>6123 <title>$site_nameOPML Export</title>6124</head>6125<body>6126<outline text="git RSS feeds">6127XML61286129foreachmy$pr(@list) {6130my%proj=%$pr;6131my$head= git_get_head_hash($proj{'path'});6132if(!defined$head) {6133next;6134}6135$git_dir="$projectroot/$proj{'path'}";6136my%co= parse_commit($head);6137if(!%co) {6138next;6139}61406141my$path= esc_html(chop_str($proj{'path'},25,5));6142my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6143my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6144print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6145}6146print<<XML;6147</outline>6148</body>6149</opml>6150XML6151}