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 33my$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# only allow viewing of repositories also shown on the overview page 99our$strict_export="++GITWEB_STRICT_EXPORT++"; 100 101# list of git base URLs used for URL to where fetch project from, 102# i.e. full URL is "$git_base_url/$project" 103our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 104 105# default blob_plain mimetype and default charset for text/plain blob 106our$default_blob_plain_mimetype='text/plain'; 107our$default_text_plain_charset=undef; 108 109# file to use for guessing MIME types before trying /etc/mime.types 110# (relative to the current git repository) 111our$mimetypes_file=undef; 112 113# assume this charset if line contains non-UTF-8 characters; 114# it should be valid encoding (see Encoding::Supported(3pm) for list), 115# for which encoding all byte sequences are valid, for example 116# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 117# could be even 'utf-8' for the old behavior) 118our$fallback_encoding='latin1'; 119 120# rename detection options for git-diff and git-diff-tree 121# - default is '-M', with the cost proportional to 122# (number of removed files) * (number of new files). 123# - more costly is '-C' (which implies '-M'), with the cost proportional to 124# (number of changed files + number of removed files) * (number of new files) 125# - even more costly is '-C', '--find-copies-harder' with cost 126# (number of files in the original tree) * (number of new files) 127# - one might want to include '-B' option, e.g. '-B', '-M' 128our@diff_opts= ('-M');# taken from git_commit 129 130# information about snapshot formats that gitweb is capable of serving 131our%known_snapshot_formats= ( 132# name => { 133# 'display' => display name, 134# 'type' => mime type, 135# 'suffix' => filename suffix, 136# 'format' => --format for git-archive, 137# 'compressor' => [compressor command and arguments] 138# (array reference, optional)} 139# 140'tgz'=> { 141'display'=>'tar.gz', 142'type'=>'application/x-gzip', 143'suffix'=>'.tar.gz', 144'format'=>'tar', 145'compressor'=> ['gzip']}, 146 147'tbz2'=> { 148'display'=>'tar.bz2', 149'type'=>'application/x-bzip2', 150'suffix'=>'.tar.bz2', 151'format'=>'tar', 152'compressor'=> ['bzip2']}, 153 154'zip'=> { 155'display'=>'zip', 156'type'=>'application/x-zip', 157'suffix'=>'.zip', 158'format'=>'zip'}, 159); 160 161# Aliases so we understand old gitweb.snapshot values in repository 162# configuration. 163our%known_snapshot_format_aliases= ( 164'gzip'=>'tgz', 165'bzip2'=>'tbz2', 166 167# backward compatibility: legacy gitweb config support 168'x-gzip'=>undef,'gz'=>undef, 169'x-bzip2'=>undef,'bz2'=>undef, 170'x-zip'=>undef,''=>undef, 171); 172 173# You define site-wide feature defaults here; override them with 174# $GITWEB_CONFIG as necessary. 175our%feature= ( 176# feature => { 177# 'sub' => feature-sub (subroutine), 178# 'override' => allow-override (boolean), 179# 'default' => [ default options...] (array reference)} 180# 181# if feature is overridable (it means that allow-override has true value), 182# then feature-sub will be called with default options as parameters; 183# return value of feature-sub indicates if to enable specified feature 184# 185# if there is no 'sub' key (no feature-sub), then feature cannot be 186# overriden 187# 188# use gitweb_check_feature(<feature>) to check if <feature> is enabled 189 190# Enable the 'blame' blob view, showing the last commit that modified 191# each line in the file. This can be very CPU-intensive. 192 193# To enable system wide have in $GITWEB_CONFIG 194# $feature{'blame'}{'default'} = [1]; 195# To have project specific config enable override in $GITWEB_CONFIG 196# $feature{'blame'}{'override'} = 1; 197# and in project config gitweb.blame = 0|1; 198'blame'=> { 199'sub'=> \&feature_blame, 200'override'=>0, 201'default'=> [0]}, 202 203# Enable the 'snapshot' link, providing a compressed archive of any 204# tree. This can potentially generate high traffic if you have large 205# project. 206 207# Value is a list of formats defined in %known_snapshot_formats that 208# you wish to offer. 209# To disable system wide have in $GITWEB_CONFIG 210# $feature{'snapshot'}{'default'} = []; 211# To have project specific config enable override in $GITWEB_CONFIG 212# $feature{'snapshot'}{'override'} = 1; 213# and in project config, a comma-separated list of formats or "none" 214# to disable. Example: gitweb.snapshot = tbz2,zip; 215'snapshot'=> { 216'sub'=> \&feature_snapshot, 217'override'=>0, 218'default'=> ['tgz']}, 219 220# Enable text search, which will list the commits which match author, 221# committer or commit text to a given string. Enabled by default. 222# Project specific override is not supported. 223'search'=> { 224'override'=>0, 225'default'=> [1]}, 226 227# Enable grep search, which will list the files in currently selected 228# tree containing the given string. Enabled by default. This can be 229# potentially CPU-intensive, of course. 230 231# To enable system wide have in $GITWEB_CONFIG 232# $feature{'grep'}{'default'} = [1]; 233# To have project specific config enable override in $GITWEB_CONFIG 234# $feature{'grep'}{'override'} = 1; 235# and in project config gitweb.grep = 0|1; 236'grep'=> { 237'override'=>0, 238'default'=> [1]}, 239 240# Enable the pickaxe search, which will list the commits that modified 241# a given string in a file. This can be practical and quite faster 242# alternative to 'blame', but still potentially CPU-intensive. 243 244# To enable system wide have in $GITWEB_CONFIG 245# $feature{'pickaxe'}{'default'} = [1]; 246# To have project specific config enable override in $GITWEB_CONFIG 247# $feature{'pickaxe'}{'override'} = 1; 248# and in project config gitweb.pickaxe = 0|1; 249'pickaxe'=> { 250'sub'=> \&feature_pickaxe, 251'override'=>0, 252'default'=> [1]}, 253 254# Make gitweb use an alternative format of the URLs which can be 255# more readable and natural-looking: project name is embedded 256# directly in the path and the query string contains other 257# auxiliary information. All gitweb installations recognize 258# URL in either format; this configures in which formats gitweb 259# generates links. 260 261# To enable system wide have in $GITWEB_CONFIG 262# $feature{'pathinfo'}{'default'} = [1]; 263# Project specific override is not supported. 264 265# Note that you will need to change the default location of CSS, 266# favicon, logo and possibly other files to an absolute URL. Also, 267# if gitweb.cgi serves as your indexfile, you will need to force 268# $my_uri to contain the script name in your $GITWEB_CONFIG. 269'pathinfo'=> { 270'override'=>0, 271'default'=> [0]}, 272 273# Make gitweb consider projects in project root subdirectories 274# to be forks of existing projects. Given project $projname.git, 275# projects matching $projname/*.git will not be shown in the main 276# projects list, instead a '+' mark will be added to $projname 277# there and a 'forks' view will be enabled for the project, listing 278# all the forks. If project list is taken from a file, forks have 279# to be listed after the main project. 280 281# To enable system wide have in $GITWEB_CONFIG 282# $feature{'forks'}{'default'} = [1]; 283# Project specific override is not supported. 284'forks'=> { 285'override'=>0, 286'default'=> [0]}, 287 288# Insert custom links to the action bar of all project pages. 289# This enables you mainly to link to third-party scripts integrating 290# into gitweb; e.g. git-browser for graphical history representation 291# or custom web-based repository administration interface. 292 293# The 'default' value consists of a list of triplets in the form 294# (label, link, position) where position is the label after which 295# to inster the link and link is a format string where %n expands 296# to the project name, %f to the project path within the filesystem, 297# %h to the current hash (h gitweb parameter) and %b to the current 298# hash base (hb gitweb parameter). 299 300# To enable system wide have in $GITWEB_CONFIG e.g. 301# $feature{'actions'}{'default'} = [('graphiclog', 302# '/git-browser/by-commit.html?r=%n', 'summary')]; 303# Project specific override is not supported. 304'actions'=> { 305'override'=>0, 306'default'=> []}, 307 308# Allow gitweb scan project content tags described in ctags/ 309# of project repository, and display the popular Web 2.0-ish 310# "tag cloud" near the project list. Note that this is something 311# COMPLETELY different from the normal Git tags. 312 313# gitweb by itself can show existing tags, but it does not handle 314# tagging itself; you need an external application for that. 315# For an example script, check Girocco's cgi/tagproj.cgi. 316# You may want to install the HTML::TagCloud Perl module to get 317# a pretty tag cloud instead of just a list of tags. 318 319# To enable system wide have in $GITWEB_CONFIG 320# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 321# Project specific override is not supported. 322'ctags'=> { 323'override'=>0, 324'default'=> [0]}, 325); 326 327sub gitweb_check_feature { 328my($name) =@_; 329return unlessexists$feature{$name}; 330my($sub,$override,@defaults) = ( 331$feature{$name}{'sub'}, 332$feature{$name}{'override'}, 333@{$feature{$name}{'default'}}); 334if(!$override) {return@defaults; } 335if(!defined$sub) { 336warn"feature$nameis not overrideable"; 337return@defaults; 338} 339return$sub->(@defaults); 340} 341 342sub feature_blame { 343my($val) = git_get_project_config('blame','--bool'); 344 345if($valeq'true') { 346return1; 347}elsif($valeq'false') { 348return0; 349} 350 351return$_[0]; 352} 353 354sub feature_snapshot { 355my(@fmts) =@_; 356 357my($val) = git_get_project_config('snapshot'); 358 359if($val) { 360@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 361} 362 363return@fmts; 364} 365 366sub feature_grep { 367my($val) = git_get_project_config('grep','--bool'); 368 369if($valeq'true') { 370return(1); 371}elsif($valeq'false') { 372return(0); 373} 374 375return($_[0]); 376} 377 378sub feature_pickaxe { 379my($val) = git_get_project_config('pickaxe','--bool'); 380 381if($valeq'true') { 382return(1); 383}elsif($valeq'false') { 384return(0); 385} 386 387return($_[0]); 388} 389 390# checking HEAD file with -e is fragile if the repository was 391# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 392# and then pruned. 393sub check_head_link { 394my($dir) =@_; 395my$headfile="$dir/HEAD"; 396return((-e $headfile) || 397(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 398} 399 400sub check_export_ok { 401my($dir) =@_; 402return(check_head_link($dir) && 403(!$export_ok|| -e "$dir/$export_ok")); 404} 405 406# process alternate names for backward compatibility 407# filter out unsupported (unknown) snapshot formats 408sub filter_snapshot_fmts { 409my@fmts=@_; 410 411@fmts=map{ 412exists$known_snapshot_format_aliases{$_} ? 413$known_snapshot_format_aliases{$_} :$_}@fmts; 414@fmts=grep(exists$known_snapshot_formats{$_},@fmts); 415 416} 417 418our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 419if(-e $GITWEB_CONFIG) { 420do$GITWEB_CONFIG; 421}else{ 422our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 423do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 424} 425 426# version of the core git binary 427our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 428 429$projects_list||=$projectroot; 430 431# ====================================================================== 432# input validation and dispatch 433 434# input parameters can be collected from a variety of sources (presently, CGI 435# and PATH_INFO), so we define an %input_params hash that collects them all 436# together during validation: this allows subsequent uses (e.g. href()) to be 437# agnostic of the parameter origin 438 439my%input_params= (); 440 441# input parameters are stored with the long parameter name as key. This will 442# also be used in the href subroutine to convert parameters to their CGI 443# equivalent, and since the href() usage is the most frequent one, we store 444# the name -> CGI key mapping here, instead of the reverse. 445# 446# XXX: Warning: If you touch this, check the search form for updating, 447# too. 448 449my@cgi_param_mapping= ( 450 project =>"p", 451 action =>"a", 452 file_name =>"f", 453 file_parent =>"fp", 454 hash =>"h", 455 hash_parent =>"hp", 456 hash_base =>"hb", 457 hash_parent_base =>"hpb", 458 page =>"pg", 459 order =>"o", 460 searchtext =>"s", 461 searchtype =>"st", 462 snapshot_format =>"sf", 463 extra_options =>"opt", 464 search_use_regexp =>"sr", 465); 466my%cgi_param_mapping=@cgi_param_mapping; 467 468# we will also need to know the possible actions, for validation 469my%actions= ( 470"blame"=> \&git_blame, 471"blobdiff"=> \&git_blobdiff, 472"blobdiff_plain"=> \&git_blobdiff_plain, 473"blob"=> \&git_blob, 474"blob_plain"=> \&git_blob_plain, 475"commitdiff"=> \&git_commitdiff, 476"commitdiff_plain"=> \&git_commitdiff_plain, 477"commit"=> \&git_commit, 478"forks"=> \&git_forks, 479"heads"=> \&git_heads, 480"history"=> \&git_history, 481"log"=> \&git_log, 482"rss"=> \&git_rss, 483"atom"=> \&git_atom, 484"search"=> \&git_search, 485"search_help"=> \&git_search_help, 486"shortlog"=> \&git_shortlog, 487"summary"=> \&git_summary, 488"tag"=> \&git_tag, 489"tags"=> \&git_tags, 490"tree"=> \&git_tree, 491"snapshot"=> \&git_snapshot, 492"object"=> \&git_object, 493# those below don't need $project 494"opml"=> \&git_opml, 495"project_list"=> \&git_project_list, 496"project_index"=> \&git_project_index, 497); 498 499# finally, we have the hash of allowed extra_options for the commands that 500# allow them 501my%allowed_options= ( 502"--no-merges"=> [qw(rss atom log shortlog history)], 503); 504 505# fill %input_params with the CGI parameters. All values except for 'opt' 506# should be single values, but opt can be an array. We should probably 507# build an array of parameters that can be multi-valued, but since for the time 508# being it's only this one, we just single it out 509while(my($name,$symbol) =each%cgi_param_mapping) { 510if($symboleq'opt') { 511$input_params{$name} = [$cgi->param($symbol) ]; 512}else{ 513$input_params{$name} =$cgi->param($symbol); 514} 515} 516 517# now read PATH_INFO and update the parameter list for missing parameters 518sub evaluate_path_info { 519return ifdefined$input_params{'project'}; 520return if!$path_info; 521$path_info=~ s,^/+,,; 522return if!$path_info; 523 524# find which part of PATH_INFO is project 525my$project=$path_info; 526$project=~ s,/+$,,; 527while($project&& !check_head_link("$projectroot/$project")) { 528$project=~ s,/*[^/]*$,,; 529} 530return unless$project; 531$input_params{'project'} =$project; 532 533# do not change any parameters if an action is given using the query string 534return if$input_params{'action'}; 535$path_info=~ s,^\Q$project\E/*,,; 536 537# next, check if we have an action 538my$action=$path_info; 539$action=~ s,/.*$,,; 540if(exists$actions{$action}) { 541$path_info=~ s,^$action/*,,; 542$input_params{'action'} =$action; 543} 544 545# list of actions that want hash_base instead of hash, but can have no 546# pathname (f) parameter 547my@wants_base= ( 548'tree', 549'history', 550); 551 552# we want to catch 553# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 554my($parentrefname,$parentpathname,$refname,$pathname) = 555($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 556 557# first, analyze the 'current' part 558if(defined$pathname) { 559# we got "branch:filename" or "branch:dir/" 560# we could use git_get_type(branch:pathname), but: 561# - it needs $git_dir 562# - it does a git() call 563# - the convention of terminating directories with a slash 564# makes it superfluous 565# - embedding the action in the PATH_INFO would make it even 566# more superfluous 567$pathname=~ s,^/+,,; 568if(!$pathname||substr($pathname, -1)eq"/") { 569$input_params{'action'} ||="tree"; 570$pathname=~ s,/$,,; 571}else{ 572# the default action depends on whether we had parent info 573# or not 574if($parentrefname) { 575$input_params{'action'} ||="blobdiff_plain"; 576}else{ 577$input_params{'action'} ||="blob_plain"; 578} 579} 580$input_params{'hash_base'} ||=$refname; 581$input_params{'file_name'} ||=$pathname; 582}elsif(defined$refname) { 583# we got "branch". In this case we have to choose if we have to 584# set hash or hash_base. 585# 586# Most of the actions without a pathname only want hash to be 587# set, except for the ones specified in @wants_base that want 588# hash_base instead. It should also be noted that hand-crafted 589# links having 'history' as an action and no pathname or hash 590# set will fail, but that happens regardless of PATH_INFO. 591$input_params{'action'} ||="shortlog"; 592if(grep{$_eq$input_params{'action'} }@wants_base) { 593$input_params{'hash_base'} ||=$refname; 594}else{ 595$input_params{'hash'} ||=$refname; 596} 597} 598 599# next, handle the 'parent' part, if present 600if(defined$parentrefname) { 601# a missing pathspec defaults to the 'current' filename, allowing e.g. 602# someproject/blobdiff/oldrev..newrev:/filename 603if($parentpathname) { 604$parentpathname=~ s,^/+,,; 605$parentpathname=~ s,/$,,; 606$input_params{'file_parent'} ||=$parentpathname; 607}else{ 608$input_params{'file_parent'} ||=$input_params{'file_name'}; 609} 610# we assume that hash_parent_base is wanted if a path was specified, 611# or if the action wants hash_base instead of hash 612if(defined$input_params{'file_parent'} || 613grep{$_eq$input_params{'action'} }@wants_base) { 614$input_params{'hash_parent_base'} ||=$parentrefname; 615}else{ 616$input_params{'hash_parent'} ||=$parentrefname; 617} 618} 619 620# for the snapshot action, we allow URLs in the form 621# $project/snapshot/$hash.ext 622# where .ext determines the snapshot and gets removed from the 623# passed $refname to provide the $hash. 624# 625# To be able to tell that $refname includes the format extension, we 626# require the following two conditions to be satisfied: 627# - the hash input parameter MUST have been set from the $refname part 628# of the URL (i.e. they must be equal) 629# - the snapshot format MUST NOT have been defined already (e.g. from 630# CGI parameter sf) 631# It's also useless to try any matching unless $refname has a dot, 632# so we check for that too 633if(defined$input_params{'action'} && 634$input_params{'action'}eq'snapshot'&& 635defined$refname&&index($refname,'.') != -1&& 636$refnameeq$input_params{'hash'} && 637!defined$input_params{'snapshot_format'}) { 638# We loop over the known snapshot formats, checking for 639# extensions. Allowed extensions are both the defined suffix 640# (which includes the initial dot already) and the snapshot 641# format key itself, with a prepended dot 642while(my($fmt,%opt) =each%known_snapshot_formats) { 643my$hash=$refname; 644my$sfx; 645$hash=~s/(\Q$opt{'suffix'}\E|\Q.$fmt\E)$//; 646next unless$sfx=$1; 647# a valid suffix was found, so set the snapshot format 648# and reset the hash parameter 649$input_params{'snapshot_format'} =$fmt; 650$input_params{'hash'} =$hash; 651# we also set the format suffix to the one requested 652# in the URL: this way a request for e.g. .tgz returns 653# a .tgz instead of a .tar.gz 654$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 655last; 656} 657} 658} 659evaluate_path_info(); 660 661our$action=$input_params{'action'}; 662if(defined$action) { 663if(!validate_action($action)) { 664 die_error(400,"Invalid action parameter"); 665} 666} 667 668# parameters which are pathnames 669our$project=$input_params{'project'}; 670if(defined$project) { 671if(!validate_project($project)) { 672undef$project; 673 die_error(404,"No such project"); 674} 675} 676 677our$file_name=$input_params{'file_name'}; 678if(defined$file_name) { 679if(!validate_pathname($file_name)) { 680 die_error(400,"Invalid file parameter"); 681} 682} 683 684our$file_parent=$input_params{'file_parent'}; 685if(defined$file_parent) { 686if(!validate_pathname($file_parent)) { 687 die_error(400,"Invalid file parent parameter"); 688} 689} 690 691# parameters which are refnames 692our$hash=$input_params{'hash'}; 693if(defined$hash) { 694if(!validate_refname($hash)) { 695 die_error(400,"Invalid hash parameter"); 696} 697} 698 699our$hash_parent=$input_params{'hash_parent'}; 700if(defined$hash_parent) { 701if(!validate_refname($hash_parent)) { 702 die_error(400,"Invalid hash parent parameter"); 703} 704} 705 706our$hash_base=$input_params{'hash_base'}; 707if(defined$hash_base) { 708if(!validate_refname($hash_base)) { 709 die_error(400,"Invalid hash base parameter"); 710} 711} 712 713our@extra_options= @{$input_params{'extra_options'}}; 714# @extra_options is always defined, since it can only be (currently) set from 715# CGI, and $cgi->param() returns the empty array in array context if the param 716# is not set 717foreachmy$opt(@extra_options) { 718if(not exists$allowed_options{$opt}) { 719 die_error(400,"Invalid option parameter"); 720} 721if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 722 die_error(400,"Invalid option parameter for this action"); 723} 724} 725 726our$hash_parent_base=$input_params{'hash_parent_base'}; 727if(defined$hash_parent_base) { 728if(!validate_refname($hash_parent_base)) { 729 die_error(400,"Invalid hash parent base parameter"); 730} 731} 732 733# other parameters 734our$page=$input_params{'page'}; 735if(defined$page) { 736if($page=~m/[^0-9]/) { 737 die_error(400,"Invalid page parameter"); 738} 739} 740 741our$searchtype=$input_params{'searchtype'}; 742if(defined$searchtype) { 743if($searchtype=~m/[^a-z]/) { 744 die_error(400,"Invalid searchtype parameter"); 745} 746} 747 748our$search_use_regexp=$input_params{'search_use_regexp'}; 749 750our$searchtext=$input_params{'searchtext'}; 751our$search_regexp; 752if(defined$searchtext) { 753if(length($searchtext) <2) { 754 die_error(403,"At least two characters are required for search parameter"); 755} 756$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 757} 758 759# path to the current git repository 760our$git_dir; 761$git_dir="$projectroot/$project"if$project; 762 763# list of supported snapshot formats 764our@snapshot_fmts= gitweb_check_feature('snapshot'); 765@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 766 767# dispatch 768if(!defined$action) { 769if(defined$hash) { 770$action= git_get_type($hash); 771}elsif(defined$hash_base&&defined$file_name) { 772$action= git_get_type("$hash_base:$file_name"); 773}elsif(defined$project) { 774$action='summary'; 775}else{ 776$action='project_list'; 777} 778} 779if(!defined($actions{$action})) { 780 die_error(400,"Unknown action"); 781} 782if($action!~m/^(opml|project_list|project_index)$/&& 783!$project) { 784 die_error(400,"Project needed"); 785} 786$actions{$action}->(); 787exit; 788 789## ====================================================================== 790## action links 791 792sub href (%) { 793my%params=@_; 794# default is to use -absolute url() i.e. $my_uri 795my$href=$params{-full} ?$my_url:$my_uri; 796 797$params{'project'} =$projectunlessexists$params{'project'}; 798 799if($params{-replay}) { 800while(my($name,$symbol) =each%cgi_param_mapping) { 801if(!exists$params{$name}) { 802$params{$name} =$input_params{$name}; 803} 804} 805} 806 807my($use_pathinfo) = gitweb_check_feature('pathinfo'); 808if($use_pathinfo) { 809# try to put as many parameters as possible in PATH_INFO: 810# - project name 811# - action 812# - hash_parent or hash_parent_base:/file_parent 813# - hash or hash_base:/filename 814 815# When the script is the root DirectoryIndex for the domain, 816# $href here would be something like http://gitweb.example.com/ 817# Thus, we strip any trailing / from $href, to spare us double 818# slashes in the final URL 819$href=~ s,/$,,; 820 821# Then add the project name, if present 822$href.="/".esc_url($params{'project'})ifdefined$params{'project'}; 823delete$params{'project'}; 824 825# Summary just uses the project path URL, any other action is 826# added to the URL 827if(defined$params{'action'}) { 828$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 829delete$params{'action'}; 830} 831 832# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 833# stripping nonexistent or useless pieces 834$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 835||$params{'hash_parent'} ||$params{'hash'}); 836if(defined$params{'hash_base'}) { 837if(defined$params{'hash_parent_base'}) { 838$href.= esc_url($params{'hash_parent_base'}); 839# skip the file_parent if it's the same as the file_name 840delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 841if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 842$href.=":/".esc_url($params{'file_parent'}); 843delete$params{'file_parent'}; 844} 845$href.=".."; 846delete$params{'hash_parent'}; 847delete$params{'hash_parent_base'}; 848}elsif(defined$params{'hash_parent'}) { 849$href.= esc_url($params{'hash_parent'}).".."; 850delete$params{'hash_parent'}; 851} 852 853$href.= esc_url($params{'hash_base'}); 854if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 855$href.=":/".esc_url($params{'file_name'}); 856delete$params{'file_name'}; 857} 858delete$params{'hash'}; 859delete$params{'hash_base'}; 860}elsif(defined$params{'hash'}) { 861$href.= esc_url($params{'hash'}); 862delete$params{'hash'}; 863} 864} 865 866# now encode the parameters explicitly 867my@result= (); 868for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 869my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 870if(defined$params{$name}) { 871if(ref($params{$name})eq"ARRAY") { 872foreachmy$par(@{$params{$name}}) { 873push@result,$symbol."=". esc_param($par); 874} 875}else{ 876push@result,$symbol."=". esc_param($params{$name}); 877} 878} 879} 880$href.="?".join(';',@result)ifscalar@result; 881 882return$href; 883} 884 885 886## ====================================================================== 887## validation, quoting/unquoting and escaping 888 889sub validate_action { 890my$input=shift||returnundef; 891returnundefunlessexists$actions{$input}; 892return$input; 893} 894 895sub validate_project { 896my$input=shift||returnundef; 897if(!validate_pathname($input) || 898!(-d "$projectroot/$input") || 899!check_head_link("$projectroot/$input") || 900($export_ok&& !(-e "$projectroot/$input/$export_ok")) || 901($strict_export&& !project_in_list($input))) { 902returnundef; 903}else{ 904return$input; 905} 906} 907 908sub validate_pathname { 909my$input=shift||returnundef; 910 911# no '.' or '..' as elements of path, i.e. no '.' nor '..' 912# at the beginning, at the end, and between slashes. 913# also this catches doubled slashes 914if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 915returnundef; 916} 917# no null characters 918if($input=~m!\0!) { 919returnundef; 920} 921return$input; 922} 923 924sub validate_refname { 925my$input=shift||returnundef; 926 927# textual hashes are O.K. 928if($input=~m/^[0-9a-fA-F]{40}$/) { 929return$input; 930} 931# it must be correct pathname 932$input= validate_pathname($input) 933orreturnundef; 934# restrictions on ref name according to git-check-ref-format 935if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { 936returnundef; 937} 938return$input; 939} 940 941# decode sequences of octets in utf8 into Perl's internal form, 942# which is utf-8 with utf8 flag set if needed. gitweb writes out 943# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning 944sub to_utf8 { 945my$str=shift; 946if(utf8::valid($str)) { 947 utf8::decode($str); 948return$str; 949}else{ 950return decode($fallback_encoding,$str, Encode::FB_DEFAULT); 951} 952} 953 954# quote unsafe chars, but keep the slash, even when it's not 955# correct, but quoted slashes look too horrible in bookmarks 956sub esc_param { 957my$str=shift; 958$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg; 959$str=~s/\+/%2B/g; 960$str=~s/ /\+/g; 961return$str; 962} 963 964# quote unsafe chars in whole URL, so some charactrs cannot be quoted 965sub esc_url { 966my$str=shift; 967$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg; 968$str=~s/\+/%2B/g; 969$str=~s/ /\+/g; 970return$str; 971} 972 973# replace invalid utf8 character with SUBSTITUTION sequence 974sub esc_html ($;%) { 975my$str=shift; 976my%opts=@_; 977 978$str= to_utf8($str); 979$str=$cgi->escapeHTML($str); 980if($opts{'-nbsp'}) { 981$str=~s/ / /g; 982} 983$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg; 984return$str; 985} 986 987# quote control characters and escape filename to HTML 988sub esc_path { 989my$str=shift; 990my%opts=@_; 991 992$str= to_utf8($str); 993$str=$cgi->escapeHTML($str); 994if($opts{'-nbsp'}) { 995$str=~s/ / /g; 996} 997$str=~ s|([[:cntrl:]])|quot_cec($1)|eg; 998return$str; 999}10001001# Make control characters "printable", using character escape codes (CEC)1002sub quot_cec {1003my$cntrl=shift;1004my%opts=@_;1005my%es= (# character escape codes, aka escape sequences1006"\t"=>'\t',# tab (HT)1007"\n"=>'\n',# line feed (LF)1008"\r"=>'\r',# carrige return (CR)1009"\f"=>'\f',# form feed (FF)1010"\b"=>'\b',# backspace (BS)1011"\a"=>'\a',# alarm (bell) (BEL)1012"\e"=>'\e',# escape (ESC)1013"\013"=>'\v',# vertical tab (VT)1014"\000"=>'\0',# nul character (NUL)1015);1016my$chr= ( (exists$es{$cntrl})1017?$es{$cntrl}1018:sprintf('\%2x',ord($cntrl)) );1019if($opts{-nohtml}) {1020return$chr;1021}else{1022return"<span class=\"cntrl\">$chr</span>";1023}1024}10251026# Alternatively use unicode control pictures codepoints,1027# Unicode "printable representation" (PR)1028sub quot_upr {1029my$cntrl=shift;1030my%opts=@_;10311032my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1033if($opts{-nohtml}) {1034return$chr;1035}else{1036return"<span class=\"cntrl\">$chr</span>";1037}1038}10391040# git may return quoted and escaped filenames1041sub unquote {1042my$str=shift;10431044sub unq {1045my$seq=shift;1046my%es= (# character escape codes, aka escape sequences1047't'=>"\t",# tab (HT, TAB)1048'n'=>"\n",# newline (NL)1049'r'=>"\r",# return (CR)1050'f'=>"\f",# form feed (FF)1051'b'=>"\b",# backspace (BS)1052'a'=>"\a",# alarm (bell) (BEL)1053'e'=>"\e",# escape (ESC)1054'v'=>"\013",# vertical tab (VT)1055);10561057if($seq=~m/^[0-7]{1,3}$/) {1058# octal char sequence1059returnchr(oct($seq));1060}elsif(exists$es{$seq}) {1061# C escape sequence, aka character escape code1062return$es{$seq};1063}1064# quoted ordinary character1065return$seq;1066}10671068if($str=~m/^"(.*)"$/) {1069# needs unquoting1070$str=$1;1071$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1072}1073return$str;1074}10751076# escape tabs (convert tabs to spaces)1077sub untabify {1078my$line=shift;10791080while((my$pos=index($line,"\t")) != -1) {1081if(my$count= (8- ($pos%8))) {1082my$spaces=' ' x $count;1083$line=~s/\t/$spaces/;1084}1085}10861087return$line;1088}10891090sub project_in_list {1091my$project=shift;1092my@list= git_get_projects_list();1093return@list&&scalar(grep{$_->{'path'}eq$project}@list);1094}10951096## ----------------------------------------------------------------------1097## HTML aware string manipulation10981099# Try to chop given string on a word boundary between position1100# $len and $len+$add_len. If there is no word boundary there,1101# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1102# (marking chopped part) would be longer than given string.1103sub chop_str {1104my$str=shift;1105my$len=shift;1106my$add_len=shift||10;1107my$where=shift||'right';# 'left' | 'center' | 'right'11081109# Make sure perl knows it is utf8 encoded so we don't1110# cut in the middle of a utf8 multibyte char.1111$str= to_utf8($str);11121113# allow only $len chars, but don't cut a word if it would fit in $add_len1114# if it doesn't fit, cut it if it's still longer than the dots we would add1115# remove chopped character entities entirely11161117# when chopping in the middle, distribute $len into left and right part1118# return early if chopping wouldn't make string shorter1119if($whereeq'center') {1120return$strif($len+5>=length($str));# filler is length 51121$len=int($len/2);1122}else{1123return$strif($len+4>=length($str));# filler is length 41124}11251126# regexps: ending and beginning with word part up to $add_len1127my$endre=qr/.{$len}\w{0,$add_len}/;1128my$begre=qr/\w{0,$add_len}.{$len}/;11291130if($whereeq'left') {1131$str=~m/^(.*?)($begre)$/;1132my($lead,$body) = ($1,$2);1133if(length($lead) >4) {1134$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1135$lead=" ...";1136}1137return"$lead$body";11381139}elsif($whereeq'center') {1140$str=~m/^($endre)(.*)$/;1141my($left,$str) = ($1,$2);1142$str=~m/^(.*?)($begre)$/;1143my($mid,$right) = ($1,$2);1144if(length($mid) >5) {1145$left=~s/&[^;]*$//;1146$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1147$mid=" ... ";1148}1149return"$left$mid$right";11501151}else{1152$str=~m/^($endre)(.*)$/;1153my$body=$1;1154my$tail=$2;1155if(length($tail) >4) {1156$body=~s/&[^;]*$//;1157$tail="... ";1158}1159return"$body$tail";1160}1161}11621163# takes the same arguments as chop_str, but also wraps a <span> around the1164# result with a title attribute if it does get chopped. Additionally, the1165# string is HTML-escaped.1166sub chop_and_escape_str {1167my($str) =@_;11681169my$chopped= chop_str(@_);1170if($choppedeq$str) {1171return esc_html($chopped);1172}else{1173$str=~s/([[:cntrl:]])/?/g;1174return$cgi->span({-title=>$str}, esc_html($chopped));1175}1176}11771178## ----------------------------------------------------------------------1179## functions returning short strings11801181# CSS class for given age value (in seconds)1182sub age_class {1183my$age=shift;11841185if(!defined$age) {1186return"noage";1187}elsif($age<60*60*2) {1188return"age0";1189}elsif($age<60*60*24*2) {1190return"age1";1191}else{1192return"age2";1193}1194}11951196# convert age in seconds to "nn units ago" string1197sub age_string {1198my$age=shift;1199my$age_str;12001201if($age>60*60*24*365*2) {1202$age_str= (int$age/60/60/24/365);1203$age_str.=" years ago";1204}elsif($age>60*60*24*(365/12)*2) {1205$age_str=int$age/60/60/24/(365/12);1206$age_str.=" months ago";1207}elsif($age>60*60*24*7*2) {1208$age_str=int$age/60/60/24/7;1209$age_str.=" weeks ago";1210}elsif($age>60*60*24*2) {1211$age_str=int$age/60/60/24;1212$age_str.=" days ago";1213}elsif($age>60*60*2) {1214$age_str=int$age/60/60;1215$age_str.=" hours ago";1216}elsif($age>60*2) {1217$age_str=int$age/60;1218$age_str.=" min ago";1219}elsif($age>2) {1220$age_str=int$age;1221$age_str.=" sec ago";1222}else{1223$age_str.=" right now";1224}1225return$age_str;1226}12271228useconstant{1229 S_IFINVALID =>0030000,1230 S_IFGITLINK =>0160000,1231};12321233# submodule/subproject, a commit object reference1234sub S_ISGITLINK($) {1235my$mode=shift;12361237return(($mode& S_IFMT) == S_IFGITLINK)1238}12391240# convert file mode in octal to symbolic file mode string1241sub mode_str {1242my$mode=oct shift;12431244if(S_ISGITLINK($mode)) {1245return'm---------';1246}elsif(S_ISDIR($mode& S_IFMT)) {1247return'drwxr-xr-x';1248}elsif(S_ISLNK($mode)) {1249return'lrwxrwxrwx';1250}elsif(S_ISREG($mode)) {1251# git cares only about the executable bit1252if($mode& S_IXUSR) {1253return'-rwxr-xr-x';1254}else{1255return'-rw-r--r--';1256};1257}else{1258return'----------';1259}1260}12611262# convert file mode in octal to file type string1263sub file_type {1264my$mode=shift;12651266if($mode!~m/^[0-7]+$/) {1267return$mode;1268}else{1269$mode=oct$mode;1270}12711272if(S_ISGITLINK($mode)) {1273return"submodule";1274}elsif(S_ISDIR($mode& S_IFMT)) {1275return"directory";1276}elsif(S_ISLNK($mode)) {1277return"symlink";1278}elsif(S_ISREG($mode)) {1279return"file";1280}else{1281return"unknown";1282}1283}12841285# convert file mode in octal to file type description string1286sub file_type_long {1287my$mode=shift;12881289if($mode!~m/^[0-7]+$/) {1290return$mode;1291}else{1292$mode=oct$mode;1293}12941295if(S_ISGITLINK($mode)) {1296return"submodule";1297}elsif(S_ISDIR($mode& S_IFMT)) {1298return"directory";1299}elsif(S_ISLNK($mode)) {1300return"symlink";1301}elsif(S_ISREG($mode)) {1302if($mode& S_IXUSR) {1303return"executable";1304}else{1305return"file";1306};1307}else{1308return"unknown";1309}1310}131113121313## ----------------------------------------------------------------------1314## functions returning short HTML fragments, or transforming HTML fragments1315## which don't belong to other sections13161317# format line of commit message.1318sub format_log_line_html {1319my$line=shift;13201321$line= esc_html($line, -nbsp=>1);1322if($line=~m/([0-9a-fA-F]{8,40})/) {1323my$hash_text=$1;1324my$link=1325$cgi->a({-href => href(action=>"object", hash=>$hash_text),1326-class=>"text"},$hash_text);1327$line=~s/$hash_text/$link/;1328}1329return$line;1330}13311332# format marker of refs pointing to given object13331334# the destination action is chosen based on object type and current context:1335# - for annotated tags, we choose the tag view unless it's the current view1336# already, in which case we go to shortlog view1337# - for other refs, we keep the current view if we're in history, shortlog or1338# log view, and select shortlog otherwise1339sub format_ref_marker {1340my($refs,$id) =@_;1341my$markers='';13421343if(defined$refs->{$id}) {1344foreachmy$ref(@{$refs->{$id}}) {1345# this code exploits the fact that non-lightweight tags are the1346# only indirect objects, and that they are the only objects for which1347# we want to use tag instead of shortlog as action1348my($type,$name) =qw();1349my$indirect= ($ref=~s/\^\{\}$//);1350# e.g. tags/v2.6.11 or heads/next1351if($ref=~m!^(.*?)s?/(.*)$!) {1352$type=$1;1353$name=$2;1354}else{1355$type="ref";1356$name=$ref;1357}13581359my$class=$type;1360$class.=" indirect"if$indirect;13611362my$dest_action="shortlog";13631364if($indirect) {1365$dest_action="tag"unless$actioneq"tag";1366}elsif($action=~/^(history|(short)?log)$/) {1367$dest_action=$action;1368}13691370my$dest="";1371$dest.="refs/"unless$ref=~ m!^refs/!;1372$dest.=$ref;13731374my$link=$cgi->a({1375-href => href(1376 action=>$dest_action,1377 hash=>$dest1378)},$name);13791380$markers.=" <span class=\"$class\"title=\"$ref\">".1381$link."</span>";1382}1383}13841385if($markers) {1386return' <span class="refs">'.$markers.'</span>';1387}else{1388return"";1389}1390}13911392# format, perhaps shortened and with markers, title line1393sub format_subject_html {1394my($long,$short,$href,$extra) =@_;1395$extra=''unlessdefined($extra);13961397if(length($short) <length($long)) {1398return$cgi->a({-href =>$href, -class=>"list subject",1399-title => to_utf8($long)},1400 esc_html($short) .$extra);1401}else{1402return$cgi->a({-href =>$href, -class=>"list subject"},1403 esc_html($long) .$extra);1404}1405}14061407# format git diff header line, i.e. "diff --(git|combined|cc) ..."1408sub format_git_diff_header_line {1409my$line=shift;1410my$diffinfo=shift;1411my($from,$to) =@_;14121413if($diffinfo->{'nparents'}) {1414# combined diff1415$line=~s!^(diff (.*?) )"?.*$!$1!;1416if($to->{'href'}) {1417$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1418 esc_path($to->{'file'}));1419}else{# file was deleted (no href)1420$line.= esc_path($to->{'file'});1421}1422}else{1423# "ordinary" diff1424$line=~s!^(diff (.*?) )"?a/.*$!$1!;1425if($from->{'href'}) {1426$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1427'a/'. esc_path($from->{'file'}));1428}else{# file was added (no href)1429$line.='a/'. esc_path($from->{'file'});1430}1431$line.=' ';1432if($to->{'href'}) {1433$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1434'b/'. esc_path($to->{'file'}));1435}else{# file was deleted1436$line.='b/'. esc_path($to->{'file'});1437}1438}14391440return"<div class=\"diff header\">$line</div>\n";1441}14421443# format extended diff header line, before patch itself1444sub format_extended_diff_header_line {1445my$line=shift;1446my$diffinfo=shift;1447my($from,$to) =@_;14481449# match <path>1450if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1451$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1452 esc_path($from->{'file'}));1453}1454if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1455$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1456 esc_path($to->{'file'}));1457}1458# match single <mode>1459if($line=~m/\s(\d{6})$/) {1460$line.='<span class="info"> ('.1461 file_type_long($1) .1462')</span>';1463}1464# match <hash>1465if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1466# can match only for combined diff1467$line='index ';1468for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1469if($from->{'href'}[$i]) {1470$line.=$cgi->a({-href=>$from->{'href'}[$i],1471-class=>"hash"},1472substr($diffinfo->{'from_id'}[$i],0,7));1473}else{1474$line.='0' x 7;1475}1476# separator1477$line.=','if($i<$diffinfo->{'nparents'} -1);1478}1479$line.='..';1480if($to->{'href'}) {1481$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1482substr($diffinfo->{'to_id'},0,7));1483}else{1484$line.='0' x 7;1485}14861487}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1488# can match only for ordinary diff1489my($from_link,$to_link);1490if($from->{'href'}) {1491$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1492substr($diffinfo->{'from_id'},0,7));1493}else{1494$from_link='0' x 7;1495}1496if($to->{'href'}) {1497$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1498substr($diffinfo->{'to_id'},0,7));1499}else{1500$to_link='0' x 7;1501}1502my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1503$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1504}15051506return$line."<br/>\n";1507}15081509# format from-file/to-file diff header1510sub format_diff_from_to_header {1511my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1512my$line;1513my$result='';15141515$line=$from_line;1516#assert($line =~ m/^---/) if DEBUG;1517# no extra formatting for "^--- /dev/null"1518if(!$diffinfo->{'nparents'}) {1519# ordinary (single parent) diff1520if($line=~m!^--- "?a/!) {1521if($from->{'href'}) {1522$line='--- a/'.1523$cgi->a({-href=>$from->{'href'}, -class=>"path"},1524 esc_path($from->{'file'}));1525}else{1526$line='--- a/'.1527 esc_path($from->{'file'});1528}1529}1530$result.= qq!<div class="diff from_file">$line</div>\n!;15311532}else{1533# combined diff (merge commit)1534for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1535if($from->{'href'}[$i]) {1536$line='--- '.1537$cgi->a({-href=>href(action=>"blobdiff",1538 hash_parent=>$diffinfo->{'from_id'}[$i],1539 hash_parent_base=>$parents[$i],1540 file_parent=>$from->{'file'}[$i],1541 hash=>$diffinfo->{'to_id'},1542 hash_base=>$hash,1543 file_name=>$to->{'file'}),1544-class=>"path",1545-title=>"diff". ($i+1)},1546$i+1) .1547'/'.1548$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1549 esc_path($from->{'file'}[$i]));1550}else{1551$line='--- /dev/null';1552}1553$result.= qq!<div class="diff from_file">$line</div>\n!;1554}1555}15561557$line=$to_line;1558#assert($line =~ m/^\+\+\+/) if DEBUG;1559# no extra formatting for "^+++ /dev/null"1560if($line=~m!^\+\+\+ "?b/!) {1561if($to->{'href'}) {1562$line='+++ b/'.1563$cgi->a({-href=>$to->{'href'}, -class=>"path"},1564 esc_path($to->{'file'}));1565}else{1566$line='+++ b/'.1567 esc_path($to->{'file'});1568}1569}1570$result.= qq!<div class="diff to_file">$line</div>\n!;15711572return$result;1573}15741575# create note for patch simplified by combined diff1576sub format_diff_cc_simplified {1577my($diffinfo,@parents) =@_;1578my$result='';15791580$result.="<div class=\"diff header\">".1581"diff --cc ";1582if(!is_deleted($diffinfo)) {1583$result.=$cgi->a({-href => href(action=>"blob",1584 hash_base=>$hash,1585 hash=>$diffinfo->{'to_id'},1586 file_name=>$diffinfo->{'to_file'}),1587-class=>"path"},1588 esc_path($diffinfo->{'to_file'}));1589}else{1590$result.= esc_path($diffinfo->{'to_file'});1591}1592$result.="</div>\n".# class="diff header"1593"<div class=\"diff nodifferences\">".1594"Simple merge".1595"</div>\n";# class="diff nodifferences"15961597return$result;1598}15991600# format patch (diff) line (not to be used for diff headers)1601sub format_diff_line {1602my$line=shift;1603my($from,$to) =@_;1604my$diff_class="";16051606chomp$line;16071608if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1609# combined diff1610my$prefix=substr($line,0,scalar@{$from->{'href'}});1611if($line=~m/^\@{3}/) {1612$diff_class=" chunk_header";1613}elsif($line=~m/^\\/) {1614$diff_class=" incomplete";1615}elsif($prefix=~tr/+/+/) {1616$diff_class=" add";1617}elsif($prefix=~tr/-/-/) {1618$diff_class=" rem";1619}1620}else{1621# assume ordinary diff1622my$char=substr($line,0,1);1623if($chareq'+') {1624$diff_class=" add";1625}elsif($chareq'-') {1626$diff_class=" rem";1627}elsif($chareq'@') {1628$diff_class=" chunk_header";1629}elsif($chareq"\\") {1630$diff_class=" incomplete";1631}1632}1633$line= untabify($line);1634if($from&&$to&&$line=~m/^\@{2} /) {1635my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1636$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;16371638$from_lines=0unlessdefined$from_lines;1639$to_lines=0unlessdefined$to_lines;16401641if($from->{'href'}) {1642$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1643-class=>"list"},$from_text);1644}1645if($to->{'href'}) {1646$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1647-class=>"list"},$to_text);1648}1649$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1650"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1651return"<div class=\"diff$diff_class\">$line</div>\n";1652}elsif($from&&$to&&$line=~m/^\@{3}/) {1653my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1654my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);16551656@from_text=split(' ',$ranges);1657for(my$i=0;$i<@from_text; ++$i) {1658($from_start[$i],$from_nlines[$i]) =1659(split(',',substr($from_text[$i],1)),0);1660}16611662$to_text=pop@from_text;1663$to_start=pop@from_start;1664$to_nlines=pop@from_nlines;16651666$line="<span class=\"chunk_info\">$prefix";1667for(my$i=0;$i<@from_text; ++$i) {1668if($from->{'href'}[$i]) {1669$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1670-class=>"list"},$from_text[$i]);1671}else{1672$line.=$from_text[$i];1673}1674$line.=" ";1675}1676if($to->{'href'}) {1677$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1678-class=>"list"},$to_text);1679}else{1680$line.=$to_text;1681}1682$line.="$prefix</span>".1683"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1684return"<div class=\"diff$diff_class\">$line</div>\n";1685}1686return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1687}16881689# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1690# linked. Pass the hash of the tree/commit to snapshot.1691sub format_snapshot_links {1692my($hash) =@_;1693my$num_fmts=@snapshot_fmts;1694if($num_fmts>1) {1695# A parenthesized list of links bearing format names.1696# e.g. "snapshot (_tar.gz_ _zip_)"1697return"snapshot (".join(' ',map1698$cgi->a({1699-href => href(1700 action=>"snapshot",1701 hash=>$hash,1702 snapshot_format=>$_1703)1704},$known_snapshot_formats{$_}{'display'})1705,@snapshot_fmts) .")";1706}elsif($num_fmts==1) {1707# A single "snapshot" link whose tooltip bears the format name.1708# i.e. "_snapshot_"1709my($fmt) =@snapshot_fmts;1710return1711$cgi->a({1712-href => href(1713 action=>"snapshot",1714 hash=>$hash,1715 snapshot_format=>$fmt1716),1717-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1718},"snapshot");1719}else{# $num_fmts == 01720returnundef;1721}1722}17231724## ......................................................................1725## functions returning values to be passed, perhaps after some1726## transformation, to other functions; e.g. returning arguments to href()17271728# returns hash to be passed to href to generate gitweb URL1729# in -title key it returns description of link1730sub get_feed_info {1731my$format=shift||'Atom';1732my%res= (action =>lc($format));17331734# feed links are possible only for project views1735return unless(defined$project);1736# some views should link to OPML, or to generic project feed,1737# or don't have specific feed yet (so they should use generic)1738return if($action=~/^(?:tags|heads|forks|tag|search)$/x);17391740my$branch;1741# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1742# from tag links; this also makes possible to detect branch links1743if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1744(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1745$branch=$1;1746}1747# find log type for feed description (title)1748my$type='log';1749if(defined$file_name) {1750$type="history of$file_name";1751$type.="/"if($actioneq'tree');1752$type.=" on '$branch'"if(defined$branch);1753}else{1754$type="log of$branch"if(defined$branch);1755}17561757$res{-title} =$type;1758$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1759$res{'file_name'} =$file_name;17601761return%res;1762}17631764## ----------------------------------------------------------------------1765## git utility subroutines, invoking git commands17661767# returns path to the core git executable and the --git-dir parameter as list1768sub git_cmd {1769return$GIT,'--git-dir='.$git_dir;1770}17711772# quote the given arguments for passing them to the shell1773# quote_command("command", "arg 1", "arg with ' and ! characters")1774# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1775# Try to avoid using this function wherever possible.1776sub quote_command {1777returnjoin(' ',1778map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1779}17801781# get HEAD ref of given project as hash1782sub git_get_head_hash {1783my$project=shift;1784my$o_git_dir=$git_dir;1785my$retval=undef;1786$git_dir="$projectroot/$project";1787if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1788my$head= <$fd>;1789close$fd;1790if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1791$retval=$1;1792}1793}1794if(defined$o_git_dir) {1795$git_dir=$o_git_dir;1796}1797return$retval;1798}17991800# get type of given object1801sub git_get_type {1802my$hash=shift;18031804open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1805my$type= <$fd>;1806close$fdorreturn;1807chomp$type;1808return$type;1809}18101811# repository configuration1812our$config_file='';1813our%config;18141815# store multiple values for single key as anonymous array reference1816# single values stored directly in the hash, not as [ <value> ]1817sub hash_set_multi {1818my($hash,$key,$value) =@_;18191820if(!exists$hash->{$key}) {1821$hash->{$key} =$value;1822}elsif(!ref$hash->{$key}) {1823$hash->{$key} = [$hash->{$key},$value];1824}else{1825push@{$hash->{$key}},$value;1826}1827}18281829# return hash of git project configuration1830# optionally limited to some section, e.g. 'gitweb'1831sub git_parse_project_config {1832my$section_regexp=shift;1833my%config;18341835local$/="\0";18361837open my$fh,"-|", git_cmd(),"config",'-z','-l',1838orreturn;18391840while(my$keyval= <$fh>) {1841chomp$keyval;1842my($key,$value) =split(/\n/,$keyval,2);18431844 hash_set_multi(\%config,$key,$value)1845if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1846}1847close$fh;18481849return%config;1850}18511852# convert config value to boolean, 'true' or 'false'1853# no value, number > 0, 'true' and 'yes' values are true1854# rest of values are treated as false (never as error)1855sub config_to_bool {1856my$val=shift;18571858# strip leading and trailing whitespace1859$val=~s/^\s+//;1860$val=~s/\s+$//;18611862return(!defined$val||# section.key1863($val=~/^\d+$/&&$val) ||# section.key = 11864($val=~/^(?:true|yes)$/i));# section.key = true1865}18661867# convert config value to simple decimal number1868# an optional value suffix of 'k', 'm', or 'g' will cause the value1869# to be multiplied by 1024, 1048576, or 10737418241870sub config_to_int {1871my$val=shift;18721873# strip leading and trailing whitespace1874$val=~s/^\s+//;1875$val=~s/\s+$//;18761877if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1878$unit=lc($unit);1879# unknown unit is treated as 11880return$num* ($uniteq'g'?1073741824:1881$uniteq'm'?1048576:1882$uniteq'k'?1024:1);1883}1884return$val;1885}18861887# convert config value to array reference, if needed1888sub config_to_multi {1889my$val=shift;18901891returnref($val) ?$val: (defined($val) ? [$val] : []);1892}18931894sub git_get_project_config {1895my($key,$type) =@_;18961897# key sanity check1898return unless($key);1899$key=~s/^gitweb\.//;1900return if($key=~m/\W/);19011902# type sanity check1903if(defined$type) {1904$type=~s/^--//;1905$type=undef1906unless($typeeq'bool'||$typeeq'int');1907}19081909# get config1910if(!defined$config_file||1911$config_filene"$git_dir/config") {1912%config= git_parse_project_config('gitweb');1913$config_file="$git_dir/config";1914}19151916# ensure given type1917if(!defined$type) {1918return$config{"gitweb.$key"};1919}elsif($typeeq'bool') {1920# backward compatibility: 'git config --bool' returns true/false1921return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1922}elsif($typeeq'int') {1923return config_to_int($config{"gitweb.$key"});1924}1925return$config{"gitweb.$key"};1926}19271928# get hash of given path at given ref1929sub git_get_hash_by_path {1930my$base=shift;1931my$path=shift||returnundef;1932my$type=shift;19331934$path=~ s,/+$,,;19351936open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path1937or die_error(500,"Open git-ls-tree failed");1938my$line= <$fd>;1939close$fdorreturnundef;19401941if(!defined$line) {1942# there is no tree or hash given by $path at $base1943returnundef;1944}19451946#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'1947$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;1948if(defined$type&&$typene$2) {1949# type doesn't match1950returnundef;1951}1952return$3;1953}19541955# get path of entry with given hash at given tree-ish (ref)1956# used to get 'from' filename for combined diff (merge commit) for renames1957sub git_get_path_by_hash {1958my$base=shift||return;1959my$hash=shift||return;19601961local$/="\0";19621963open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base1964orreturnundef;1965while(my$line= <$fd>) {1966chomp$line;19671968#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'1969#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'1970if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {1971close$fd;1972return$1;1973}1974}1975close$fd;1976returnundef;1977}19781979## ......................................................................1980## git utility functions, directly accessing git repository19811982sub git_get_project_description {1983my$path=shift;19841985$git_dir="$projectroot/$path";1986open my$fd,"$git_dir/description"1987orreturn git_get_project_config('description');1988my$descr= <$fd>;1989close$fd;1990if(defined$descr) {1991chomp$descr;1992}1993return$descr;1994}19951996sub git_get_project_ctags {1997my$path=shift;1998my$ctags= {};19992000$git_dir="$projectroot/$path";2001foreach(<$git_dir/ctags/*>) {2002open CT,$_ornext;2003my$val= <CT>;2004chomp$val;2005close CT;2006my$ctag=$_;$ctag=~ s#.*/##;2007$ctags->{$ctag} =$val;2008}2009$ctags;2010}20112012sub git_populate_project_tagcloud {2013my$ctags=shift;20142015# First, merge different-cased tags; tags vote on casing2016my%ctags_lc;2017foreach(keys%$ctags) {2018$ctags_lc{lc$_}->{count} +=$ctags->{$_};2019if(not$ctags_lc{lc$_}->{topcount}2020or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2021$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2022$ctags_lc{lc$_}->{topname} =$_;2023}2024}20252026my$cloud;2027if(eval{require HTML::TagCloud;1; }) {2028$cloud= HTML::TagCloud->new;2029foreach(sort keys%ctags_lc) {2030# Pad the title with spaces so that the cloud looks2031# less crammed.2032my$title=$ctags_lc{$_}->{topname};2033$title=~s/ / /g;2034$title=~s/^/ /g;2035$title=~s/$/ /g;2036$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2037}2038}else{2039$cloud= \%ctags_lc;2040}2041$cloud;2042}20432044sub git_show_project_tagcloud {2045my($cloud,$count) =@_;2046print STDERR ref($cloud)."..\n";2047if(ref$cloudeq'HTML::TagCloud') {2048return$cloud->html_and_css($count);2049}else{2050my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2051return'<p align="center">'.join(', ',map{2052"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2053}splice(@tags,0,$count)) .'</p>';2054}2055}20562057sub git_get_project_url_list {2058my$path=shift;20592060$git_dir="$projectroot/$path";2061open my$fd,"$git_dir/cloneurl"2062orreturnwantarray?2063@{ config_to_multi(git_get_project_config('url')) } :2064 config_to_multi(git_get_project_config('url'));2065my@git_project_url_list=map{chomp;$_} <$fd>;2066close$fd;20672068returnwantarray?@git_project_url_list: \@git_project_url_list;2069}20702071sub git_get_projects_list {2072my($filter) =@_;2073my@list;20742075$filter||='';2076$filter=~s/\.git$//;20772078my($check_forks) = gitweb_check_feature('forks');20792080if(-d $projects_list) {2081# search in directory2082my$dir=$projects_list. ($filter?"/$filter":'');2083# remove the trailing "/"2084$dir=~s!/+$!!;2085my$pfxlen=length("$dir");2086my$pfxdepth= ($dir=~tr!/!!);20872088 File::Find::find({2089 follow_fast =>1,# follow symbolic links2090 follow_skip =>2,# ignore duplicates2091 dangling_symlinks =>0,# ignore dangling symlinks, silently2092 wanted =>sub{2093# skip project-list toplevel, if we get it.2094return if(m!^[/.]$!);2095# only directories can be git repositories2096return unless(-d $_);2097# don't traverse too deep (Find is super slow on os x)2098if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2099$File::Find::prune =1;2100return;2101}21022103my$subdir=substr($File::Find::name,$pfxlen+1);2104# we check related file in $projectroot2105if(check_export_ok("$projectroot/$filter/$subdir")) {2106push@list, { path => ($filter?"$filter/":'') .$subdir};2107$File::Find::prune =1;2108}2109},2110},"$dir");21112112}elsif(-f $projects_list) {2113# read from file(url-encoded):2114# 'git%2Fgit.git Linus+Torvalds'2115# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2116# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2117my%paths;2118open my($fd),$projects_listorreturn;2119 PROJECT:2120while(my$line= <$fd>) {2121chomp$line;2122my($path,$owner) =split' ',$line;2123$path= unescape($path);2124$owner= unescape($owner);2125if(!defined$path) {2126next;2127}2128if($filterne'') {2129# looking for forks;2130my$pfx=substr($path,0,length($filter));2131if($pfxne$filter) {2132next PROJECT;2133}2134my$sfx=substr($path,length($filter));2135if($sfx!~/^\/.*\.git$/) {2136next PROJECT;2137}2138}elsif($check_forks) {2139 PATH:2140foreachmy$filter(keys%paths) {2141# looking for forks;2142my$pfx=substr($path,0,length($filter));2143if($pfxne$filter) {2144next PATH;2145}2146my$sfx=substr($path,length($filter));2147if($sfx!~/^\/.*\.git$/) {2148next PATH;2149}2150# is a fork, don't include it in2151# the list2152next PROJECT;2153}2154}2155if(check_export_ok("$projectroot/$path")) {2156my$pr= {2157 path =>$path,2158 owner => to_utf8($owner),2159};2160push@list,$pr;2161(my$forks_path=$path) =~s/\.git$//;2162$paths{$forks_path}++;2163}2164}2165close$fd;2166}2167return@list;2168}21692170our$gitweb_project_owner=undef;2171sub git_get_project_list_from_file {21722173return if(defined$gitweb_project_owner);21742175$gitweb_project_owner= {};2176# read from file (url-encoded):2177# 'git%2Fgit.git Linus+Torvalds'2178# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2179# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2180if(-f $projects_list) {2181open(my$fd,$projects_list);2182while(my$line= <$fd>) {2183chomp$line;2184my($pr,$ow) =split' ',$line;2185$pr= unescape($pr);2186$ow= unescape($ow);2187$gitweb_project_owner->{$pr} = to_utf8($ow);2188}2189close$fd;2190}2191}21922193sub git_get_project_owner {2194my$project=shift;2195my$owner;21962197returnundefunless$project;2198$git_dir="$projectroot/$project";21992200if(!defined$gitweb_project_owner) {2201 git_get_project_list_from_file();2202}22032204if(exists$gitweb_project_owner->{$project}) {2205$owner=$gitweb_project_owner->{$project};2206}2207if(!defined$owner){2208$owner= git_get_project_config('owner');2209}2210if(!defined$owner) {2211$owner= get_file_owner("$git_dir");2212}22132214return$owner;2215}22162217sub git_get_last_activity {2218my($path) =@_;2219my$fd;22202221$git_dir="$projectroot/$path";2222open($fd,"-|", git_cmd(),'for-each-ref',2223'--format=%(committer)',2224'--sort=-committerdate',2225'--count=1',2226'refs/heads')orreturn;2227my$most_recent= <$fd>;2228close$fdorreturn;2229if(defined$most_recent&&2230$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2231my$timestamp=$1;2232my$age=time-$timestamp;2233return($age, age_string($age));2234}2235return(undef,undef);2236}22372238sub git_get_references {2239my$type=shift||"";2240my%refs;2241# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112242# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2243open my$fd,"-|", git_cmd(),"show-ref","--dereference",2244($type? ("--","refs/$type") : ())# use -- <pattern> if $type2245orreturn;22462247while(my$line= <$fd>) {2248chomp$line;2249if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2250if(defined$refs{$1}) {2251push@{$refs{$1}},$2;2252}else{2253$refs{$1} = [$2];2254}2255}2256}2257close$fdorreturn;2258return \%refs;2259}22602261sub git_get_rev_name_tags {2262my$hash=shift||returnundef;22632264open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2265orreturn;2266my$name_rev= <$fd>;2267close$fd;22682269if($name_rev=~ m|^$hash tags/(.*)$|) {2270return$1;2271}else{2272# catches also '$hash undefined' output2273returnundef;2274}2275}22762277## ----------------------------------------------------------------------2278## parse to hash functions22792280sub parse_date {2281my$epoch=shift;2282my$tz=shift||"-0000";22832284my%date;2285my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2286my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2287my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2288$date{'hour'} =$hour;2289$date{'minute'} =$min;2290$date{'mday'} =$mday;2291$date{'day'} =$days[$wday];2292$date{'month'} =$months[$mon];2293$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2294$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2295$date{'mday-time'} =sprintf"%d%s%02d:%02d",2296$mday,$months[$mon],$hour,$min;2297$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",22981900+$year,1+$mon,$mday,$hour,$min,$sec;22992300$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2301my$local=$epoch+ ((int$1+ ($2/60)) *3600);2302($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2303$date{'hour_local'} =$hour;2304$date{'minute_local'} =$min;2305$date{'tz_local'} =$tz;2306$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",23071900+$year,$mon+1,$mday,2308$hour,$min,$sec,$tz);2309return%date;2310}23112312sub parse_tag {2313my$tag_id=shift;2314my%tag;2315my@comment;23162317open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2318$tag{'id'} =$tag_id;2319while(my$line= <$fd>) {2320chomp$line;2321if($line=~m/^object ([0-9a-fA-F]{40})$/) {2322$tag{'object'} =$1;2323}elsif($line=~m/^type (.+)$/) {2324$tag{'type'} =$1;2325}elsif($line=~m/^tag (.+)$/) {2326$tag{'name'} =$1;2327}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2328$tag{'author'} =$1;2329$tag{'epoch'} =$2;2330$tag{'tz'} =$3;2331}elsif($line=~m/--BEGIN/) {2332push@comment,$line;2333last;2334}elsif($lineeq"") {2335last;2336}2337}2338push@comment, <$fd>;2339$tag{'comment'} = \@comment;2340close$fdorreturn;2341if(!defined$tag{'name'}) {2342return2343};2344return%tag2345}23462347sub parse_commit_text {2348my($commit_text,$withparents) =@_;2349my@commit_lines=split'\n',$commit_text;2350my%co;23512352pop@commit_lines;# Remove '\0'23532354if(!@commit_lines) {2355return;2356}23572358my$header=shift@commit_lines;2359if($header!~m/^[0-9a-fA-F]{40}/) {2360return;2361}2362($co{'id'},my@parents) =split' ',$header;2363while(my$line=shift@commit_lines) {2364last if$lineeq"\n";2365if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2366$co{'tree'} =$1;2367}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2368push@parents,$1;2369}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2370$co{'author'} =$1;2371$co{'author_epoch'} =$2;2372$co{'author_tz'} =$3;2373if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2374$co{'author_name'} =$1;2375$co{'author_email'} =$2;2376}else{2377$co{'author_name'} =$co{'author'};2378}2379}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2380$co{'committer'} =$1;2381$co{'committer_epoch'} =$2;2382$co{'committer_tz'} =$3;2383$co{'committer_name'} =$co{'committer'};2384if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2385$co{'committer_name'} =$1;2386$co{'committer_email'} =$2;2387}else{2388$co{'committer_name'} =$co{'committer'};2389}2390}2391}2392if(!defined$co{'tree'}) {2393return;2394};2395$co{'parents'} = \@parents;2396$co{'parent'} =$parents[0];23972398foreachmy$title(@commit_lines) {2399$title=~s/^ //;2400if($titlene"") {2401$co{'title'} = chop_str($title,80,5);2402# remove leading stuff of merges to make the interesting part visible2403if(length($title) >50) {2404$title=~s/^Automatic //;2405$title=~s/^merge (of|with) /Merge ... /i;2406if(length($title) >50) {2407$title=~s/(http|rsync):\/\///;2408}2409if(length($title) >50) {2410$title=~s/(master|www|rsync)\.//;2411}2412if(length($title) >50) {2413$title=~s/kernel.org:?//;2414}2415if(length($title) >50) {2416$title=~s/\/pub\/scm//;2417}2418}2419$co{'title_short'} = chop_str($title,50,5);2420last;2421}2422}2423if(!defined$co{'title'} ||$co{'title'}eq"") {2424$co{'title'} =$co{'title_short'} ='(no commit message)';2425}2426# remove added spaces2427foreachmy$line(@commit_lines) {2428$line=~s/^ //;2429}2430$co{'comment'} = \@commit_lines;24312432my$age=time-$co{'committer_epoch'};2433$co{'age'} =$age;2434$co{'age_string'} = age_string($age);2435my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2436if($age>60*60*24*7*2) {2437$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2438$co{'age_string_age'} =$co{'age_string'};2439}else{2440$co{'age_string_date'} =$co{'age_string'};2441$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2442}2443return%co;2444}24452446sub parse_commit {2447my($commit_id) =@_;2448my%co;24492450local$/="\0";24512452open my$fd,"-|", git_cmd(),"rev-list",2453"--parents",2454"--header",2455"--max-count=1",2456$commit_id,2457"--",2458or die_error(500,"Open git-rev-list failed");2459%co= parse_commit_text(<$fd>,1);2460close$fd;24612462return%co;2463}24642465sub parse_commits {2466my($commit_id,$maxcount,$skip,$filename,@args) =@_;2467my@cos;24682469$maxcount||=1;2470$skip||=0;24712472local$/="\0";24732474open my$fd,"-|", git_cmd(),"rev-list",2475"--header",2476@args,2477("--max-count=".$maxcount),2478("--skip=".$skip),2479@extra_options,2480$commit_id,2481"--",2482($filename? ($filename) : ())2483or die_error(500,"Open git-rev-list failed");2484while(my$line= <$fd>) {2485my%co= parse_commit_text($line);2486push@cos, \%co;2487}2488close$fd;24892490returnwantarray?@cos: \@cos;2491}24922493# parse line of git-diff-tree "raw" output2494sub parse_difftree_raw_line {2495my$line=shift;2496my%res;24972498# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2499# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2500if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2501$res{'from_mode'} =$1;2502$res{'to_mode'} =$2;2503$res{'from_id'} =$3;2504$res{'to_id'} =$4;2505$res{'status'} =$5;2506$res{'similarity'} =$6;2507if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2508($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2509}else{2510$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2511}2512}2513# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2514# combined diff (for merge commit)2515elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2516$res{'nparents'} =length($1);2517$res{'from_mode'} = [split(' ',$2) ];2518$res{'to_mode'} =pop@{$res{'from_mode'}};2519$res{'from_id'} = [split(' ',$3) ];2520$res{'to_id'} =pop@{$res{'from_id'}};2521$res{'status'} = [split('',$4) ];2522$res{'to_file'} = unquote($5);2523}2524# 'c512b523472485aef4fff9e57b229d9d243c967f'2525elsif($line=~m/^([0-9a-fA-F]{40})$/) {2526$res{'commit'} =$1;2527}25282529returnwantarray?%res: \%res;2530}25312532# wrapper: return parsed line of git-diff-tree "raw" output2533# (the argument might be raw line, or parsed info)2534sub parsed_difftree_line {2535my$line_or_ref=shift;25362537if(ref($line_or_ref)eq"HASH") {2538# pre-parsed (or generated by hand)2539return$line_or_ref;2540}else{2541return parse_difftree_raw_line($line_or_ref);2542}2543}25442545# parse line of git-ls-tree output2546sub parse_ls_tree_line ($;%) {2547my$line=shift;2548my%opts=@_;2549my%res;25502551#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2552$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;25532554$res{'mode'} =$1;2555$res{'type'} =$2;2556$res{'hash'} =$3;2557if($opts{'-z'}) {2558$res{'name'} =$4;2559}else{2560$res{'name'} = unquote($4);2561}25622563returnwantarray?%res: \%res;2564}25652566# generates _two_ hashes, references to which are passed as 2 and 3 argument2567sub parse_from_to_diffinfo {2568my($diffinfo,$from,$to,@parents) =@_;25692570if($diffinfo->{'nparents'}) {2571# combined diff2572$from->{'file'} = [];2573$from->{'href'} = [];2574 fill_from_file_info($diffinfo,@parents)2575unlessexists$diffinfo->{'from_file'};2576for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2577$from->{'file'}[$i] =2578defined$diffinfo->{'from_file'}[$i] ?2579$diffinfo->{'from_file'}[$i] :2580$diffinfo->{'to_file'};2581if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2582$from->{'href'}[$i] = href(action=>"blob",2583 hash_base=>$parents[$i],2584 hash=>$diffinfo->{'from_id'}[$i],2585 file_name=>$from->{'file'}[$i]);2586}else{2587$from->{'href'}[$i] =undef;2588}2589}2590}else{2591# ordinary (not combined) diff2592$from->{'file'} =$diffinfo->{'from_file'};2593if($diffinfo->{'status'}ne"A") {# not new (added) file2594$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2595 hash=>$diffinfo->{'from_id'},2596 file_name=>$from->{'file'});2597}else{2598delete$from->{'href'};2599}2600}26012602$to->{'file'} =$diffinfo->{'to_file'};2603if(!is_deleted($diffinfo)) {# file exists in result2604$to->{'href'} = href(action=>"blob", hash_base=>$hash,2605 hash=>$diffinfo->{'to_id'},2606 file_name=>$to->{'file'});2607}else{2608delete$to->{'href'};2609}2610}26112612## ......................................................................2613## parse to array of hashes functions26142615sub git_get_heads_list {2616my$limit=shift;2617my@headslist;26182619open my$fd,'-|', git_cmd(),'for-each-ref',2620($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2621'--format=%(objectname) %(refname) %(subject)%00%(committer)',2622'refs/heads'2623orreturn;2624while(my$line= <$fd>) {2625my%ref_item;26262627chomp$line;2628my($refinfo,$committerinfo) =split(/\0/,$line);2629my($hash,$name,$title) =split(' ',$refinfo,3);2630my($committer,$epoch,$tz) =2631($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2632$ref_item{'fullname'} =$name;2633$name=~s!^refs/heads/!!;26342635$ref_item{'name'} =$name;2636$ref_item{'id'} =$hash;2637$ref_item{'title'} =$title||'(no commit message)';2638$ref_item{'epoch'} =$epoch;2639if($epoch) {2640$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2641}else{2642$ref_item{'age'} ="unknown";2643}26442645push@headslist, \%ref_item;2646}2647close$fd;26482649returnwantarray?@headslist: \@headslist;2650}26512652sub git_get_tags_list {2653my$limit=shift;2654my@tagslist;26552656open my$fd,'-|', git_cmd(),'for-each-ref',2657($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2658'--format=%(objectname) %(objecttype) %(refname) '.2659'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2660'refs/tags'2661orreturn;2662while(my$line= <$fd>) {2663my%ref_item;26642665chomp$line;2666my($refinfo,$creatorinfo) =split(/\0/,$line);2667my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2668my($creator,$epoch,$tz) =2669($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2670$ref_item{'fullname'} =$name;2671$name=~s!^refs/tags/!!;26722673$ref_item{'type'} =$type;2674$ref_item{'id'} =$id;2675$ref_item{'name'} =$name;2676if($typeeq"tag") {2677$ref_item{'subject'} =$title;2678$ref_item{'reftype'} =$reftype;2679$ref_item{'refid'} =$refid;2680}else{2681$ref_item{'reftype'} =$type;2682$ref_item{'refid'} =$id;2683}26842685if($typeeq"tag"||$typeeq"commit") {2686$ref_item{'epoch'} =$epoch;2687if($epoch) {2688$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2689}else{2690$ref_item{'age'} ="unknown";2691}2692}26932694push@tagslist, \%ref_item;2695}2696close$fd;26972698returnwantarray?@tagslist: \@tagslist;2699}27002701## ----------------------------------------------------------------------2702## filesystem-related functions27032704sub get_file_owner {2705my$path=shift;27062707my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2708my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2709if(!defined$gcos) {2710returnundef;2711}2712my$owner=$gcos;2713$owner=~s/[,;].*$//;2714return to_utf8($owner);2715}27162717## ......................................................................2718## mimetype related functions27192720sub mimetype_guess_file {2721my$filename=shift;2722my$mimemap=shift;2723-r $mimemaporreturnundef;27242725my%mimemap;2726open(MIME,$mimemap)orreturnundef;2727while(<MIME>) {2728next ifm/^#/;# skip comments2729my($mime,$exts) =split(/\t+/);2730if(defined$exts) {2731my@exts=split(/\s+/,$exts);2732foreachmy$ext(@exts) {2733$mimemap{$ext} =$mime;2734}2735}2736}2737close(MIME);27382739$filename=~/\.([^.]*)$/;2740return$mimemap{$1};2741}27422743sub mimetype_guess {2744my$filename=shift;2745my$mime;2746$filename=~/\./orreturnundef;27472748if($mimetypes_file) {2749my$file=$mimetypes_file;2750if($file!~m!^/!) {# if it is relative path2751# it is relative to project2752$file="$projectroot/$project/$file";2753}2754$mime= mimetype_guess_file($filename,$file);2755}2756$mime||= mimetype_guess_file($filename,'/etc/mime.types');2757return$mime;2758}27592760sub blob_mimetype {2761my$fd=shift;2762my$filename=shift;27632764if($filename) {2765my$mime= mimetype_guess($filename);2766$mimeandreturn$mime;2767}27682769# just in case2770return$default_blob_plain_mimetypeunless$fd;27712772if(-T $fd) {2773return'text/plain';2774}elsif(!$filename) {2775return'application/octet-stream';2776}elsif($filename=~m/\.png$/i) {2777return'image/png';2778}elsif($filename=~m/\.gif$/i) {2779return'image/gif';2780}elsif($filename=~m/\.jpe?g$/i) {2781return'image/jpeg';2782}else{2783return'application/octet-stream';2784}2785}27862787sub blob_contenttype {2788my($fd,$file_name,$type) =@_;27892790$type||= blob_mimetype($fd,$file_name);2791if($typeeq'text/plain'&&defined$default_text_plain_charset) {2792$type.="; charset=$default_text_plain_charset";2793}27942795return$type;2796}27972798## ======================================================================2799## functions printing HTML: header, footer, error page28002801sub git_header_html {2802my$status=shift||"200 OK";2803my$expires=shift;28042805my$title="$site_name";2806if(defined$project) {2807$title.=" - ". to_utf8($project);2808if(defined$action) {2809$title.="/$action";2810if(defined$file_name) {2811$title.=" - ". esc_path($file_name);2812if($actioneq"tree"&&$file_name!~ m|/$|) {2813$title.="/";2814}2815}2816}2817}2818my$content_type;2819# require explicit support from the UA if we are to send the page as2820# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2821# we have to do this because MSIE sometimes globs '*/*', pretending to2822# support xhtml+xml but choking when it gets what it asked for.2823if(defined$cgi->http('HTTP_ACCEPT') &&2824$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2825$cgi->Accept('application/xhtml+xml') !=0) {2826$content_type='application/xhtml+xml';2827}else{2828$content_type='text/html';2829}2830print$cgi->header(-type=>$content_type, -charset =>'utf-8',2831-status=>$status, -expires =>$expires);2832my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2833print<<EOF;2834<?xml version="1.0" encoding="utf-8"?>2835<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2836<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2837<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2838<!-- git core binaries version$git_version-->2839<head>2840<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2841<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2842<meta name="robots" content="index, nofollow"/>2843<title>$title</title>2844EOF2845# print out each stylesheet that exist2846if(defined$stylesheet) {2847#provides backwards capability for those people who define style sheet in a config file2848print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2849}else{2850foreachmy$stylesheet(@stylesheets) {2851next unless$stylesheet;2852print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2853}2854}2855if(defined$project) {2856my%href_params= get_feed_info();2857if(!exists$href_params{'-title'}) {2858$href_params{'-title'} ='log';2859}28602861foreachmy$formatqw(RSS Atom){2862my$type=lc($format);2863my%link_attr= (2864'-rel'=>'alternate',2865'-title'=>"$project-$href_params{'-title'} -$formatfeed",2866'-type'=>"application/$type+xml"2867);28682869$href_params{'action'} =$type;2870$link_attr{'-href'} = href(%href_params);2871print"<link ".2872"rel=\"$link_attr{'-rel'}\"".2873"title=\"$link_attr{'-title'}\"".2874"href=\"$link_attr{'-href'}\"".2875"type=\"$link_attr{'-type'}\"".2876"/>\n";28772878$href_params{'extra_options'} ='--no-merges';2879$link_attr{'-href'} = href(%href_params);2880$link_attr{'-title'} .=' (no merges)';2881print"<link ".2882"rel=\"$link_attr{'-rel'}\"".2883"title=\"$link_attr{'-title'}\"".2884"href=\"$link_attr{'-href'}\"".2885"type=\"$link_attr{'-type'}\"".2886"/>\n";2887}28882889}else{2890printf('<link rel="alternate" title="%sprojects list" '.2891'href="%s" type="text/plain; charset=utf-8" />'."\n",2892$site_name, href(project=>undef, action=>"project_index"));2893printf('<link rel="alternate" title="%sprojects feeds" '.2894'href="%s" type="text/x-opml" />'."\n",2895$site_name, href(project=>undef, action=>"opml"));2896}2897if(defined$favicon) {2898printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2899}29002901print"</head>\n".2902"<body>\n";29032904if(-f $site_header) {2905open(my$fd,$site_header);2906print<$fd>;2907close$fd;2908}29092910print"<div class=\"page_header\">\n".2911$cgi->a({-href => esc_url($logo_url),2912-title =>$logo_label},2913qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2914print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2915if(defined$project) {2916print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2917if(defined$action) {2918print" /$action";2919}2920print"\n";2921}2922print"</div>\n";29232924my($have_search) = gitweb_check_feature('search');2925if(defined$project&&$have_search) {2926if(!defined$searchtext) {2927$searchtext="";2928}2929my$search_hash;2930if(defined$hash_base) {2931$search_hash=$hash_base;2932}elsif(defined$hash) {2933$search_hash=$hash;2934}else{2935$search_hash="HEAD";2936}2937my$action=$my_uri;2938my($use_pathinfo) = gitweb_check_feature('pathinfo');2939if($use_pathinfo) {2940$action.="/".esc_url($project);2941}2942print$cgi->startform(-method=>"get", -action =>$action) .2943"<div class=\"search\">\n".2944(!$use_pathinfo&&2945$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .2946$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".2947$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".2948$cgi->popup_menu(-name =>'st', -default=>'commit',2949-values=> ['commit','grep','author','committer','pickaxe']) .2950$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .2951" search:\n",2952$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".2953"<span title=\"Extended regular expression\">".2954$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',2955-checked =>$search_use_regexp) .2956"</span>".2957"</div>".2958$cgi->end_form() ."\n";2959}2960}29612962sub git_footer_html {2963my$feed_class='rss_logo';29642965print"<div class=\"page_footer\">\n";2966if(defined$project) {2967my$descr= git_get_project_description($project);2968if(defined$descr) {2969print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";2970}29712972my%href_params= get_feed_info();2973if(!%href_params) {2974$feed_class.=' generic';2975}2976$href_params{'-title'} ||='log';29772978foreachmy$formatqw(RSS Atom){2979$href_params{'action'} =lc($format);2980print$cgi->a({-href => href(%href_params),2981-title =>"$href_params{'-title'}$formatfeed",2982-class=>$feed_class},$format)."\n";2983}29842985}else{2986print$cgi->a({-href => href(project=>undef, action=>"opml"),2987-class=>$feed_class},"OPML") ." ";2988print$cgi->a({-href => href(project=>undef, action=>"project_index"),2989-class=>$feed_class},"TXT") ."\n";2990}2991print"</div>\n";# class="page_footer"29922993if(-f $site_footer) {2994open(my$fd,$site_footer);2995print<$fd>;2996close$fd;2997}29982999print"</body>\n".3000"</html>";3001}30023003# die_error(<http_status_code>, <error_message>)3004# Example: die_error(404, 'Hash not found')3005# By convention, use the following status codes (as defined in RFC 2616):3006# 400: Invalid or missing CGI parameters, or3007# requested object exists but has wrong type.3008# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3009# this server or project.3010# 404: Requested object/revision/project doesn't exist.3011# 500: The server isn't configured properly, or3012# an internal error occurred (e.g. failed assertions caused by bugs), or3013# an unknown error occurred (e.g. the git binary died unexpectedly).3014sub die_error {3015my$status=shift||500;3016my$error=shift||"Internal server error";30173018my%http_responses= (400=>'400 Bad Request',3019403=>'403 Forbidden',3020404=>'404 Not Found',3021500=>'500 Internal Server Error');3022 git_header_html($http_responses{$status});3023print<<EOF;3024<div class="page_body">3025<br /><br />3026$status-$error3027<br />3028</div>3029EOF3030 git_footer_html();3031exit;3032}30333034## ----------------------------------------------------------------------3035## functions printing or outputting HTML: navigation30363037sub git_print_page_nav {3038my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3039$extra=''if!defined$extra;# pager or formats30403041my@navs=qw(summary shortlog log commit commitdiff tree);3042if($suppress) {3043@navs=grep{$_ne$suppress}@navs;3044}30453046my%arg=map{$_=> {action=>$_} }@navs;3047if(defined$head) {3048for(qw(commit commitdiff)) {3049$arg{$_}{'hash'} =$head;3050}3051if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3052for(qw(shortlog log)) {3053$arg{$_}{'hash'} =$head;3054}3055}3056}30573058$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3059$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;30603061my@actions= gitweb_check_feature('actions');3062while(@actions) {3063my($label,$link,$pos) = (shift(@actions),shift(@actions),shift(@actions));3064@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3065# munch munch3066$link=~ s#%n#$project#g;3067$link=~ s#%f#$git_dir#g;3068$treehead?$link=~ s#%h#$treehead#g : $link =~ s#%h##g;3069$treebase?$link=~ s#%b#$treebase#g : $link =~ s#%b##g;3070$arg{$label}{'_href'} =$link;3071}30723073print"<div class=\"page_nav\">\n".3074(join" | ",3075map{$_eq$current?3076$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3077}@navs);3078print"<br/>\n$extra<br/>\n".3079"</div>\n";3080}30813082sub format_paging_nav {3083my($action,$hash,$head,$page,$has_next_link) =@_;3084my$paging_nav;308530863087if($hashne$head||$page) {3088$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3089}else{3090$paging_nav.="HEAD";3091}30923093if($page>0) {3094$paging_nav.=" ⋅ ".3095$cgi->a({-href => href(-replay=>1, page=>$page-1),3096-accesskey =>"p", -title =>"Alt-p"},"prev");3097}else{3098$paging_nav.=" ⋅ prev";3099}31003101if($has_next_link) {3102$paging_nav.=" ⋅ ".3103$cgi->a({-href => href(-replay=>1, page=>$page+1),3104-accesskey =>"n", -title =>"Alt-n"},"next");3105}else{3106$paging_nav.=" ⋅ next";3107}31083109return$paging_nav;3110}31113112## ......................................................................3113## functions printing or outputting HTML: div31143115sub git_print_header_div {3116my($action,$title,$hash,$hash_base) =@_;3117my%args= ();31183119$args{'action'} =$action;3120$args{'hash'} =$hashif$hash;3121$args{'hash_base'} =$hash_baseif$hash_base;31223123print"<div class=\"header\">\n".3124$cgi->a({-href => href(%args), -class=>"title"},3125$title?$title:$action) .3126"\n</div>\n";3127}31283129#sub git_print_authorship (\%) {3130sub git_print_authorship {3131my$co=shift;31323133my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3134print"<div class=\"author_date\">".3135 esc_html($co->{'author_name'}) .3136" [$ad{'rfc2822'}";3137if($ad{'hour_local'} <6) {3138printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3139$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3140}else{3141printf(" (%02d:%02d%s)",3142$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3143}3144print"]</div>\n";3145}31463147sub git_print_page_path {3148my$name=shift;3149my$type=shift;3150my$hb=shift;315131523153print"<div class=\"page_path\">";3154print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3155-title =>'tree root'}, to_utf8("[$project]"));3156print" / ";3157if(defined$name) {3158my@dirname=split'/',$name;3159my$basename=pop@dirname;3160my$fullname='';31613162foreachmy$dir(@dirname) {3163$fullname.= ($fullname?'/':'') .$dir;3164print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3165 hash_base=>$hb),3166-title =>$fullname}, esc_path($dir));3167print" / ";3168}3169if(defined$type&&$typeeq'blob') {3170print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3171 hash_base=>$hb),3172-title =>$name}, esc_path($basename));3173}elsif(defined$type&&$typeeq'tree') {3174print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3175 hash_base=>$hb),3176-title =>$name}, esc_path($basename));3177print" / ";3178}else{3179print esc_path($basename);3180}3181}3182print"<br/></div>\n";3183}31843185# sub git_print_log (\@;%) {3186sub git_print_log ($;%) {3187my$log=shift;3188my%opts=@_;31893190if($opts{'-remove_title'}) {3191# remove title, i.e. first line of log3192shift@$log;3193}3194# remove leading empty lines3195while(defined$log->[0] &&$log->[0]eq"") {3196shift@$log;3197}31983199# print log3200my$signoff=0;3201my$empty=0;3202foreachmy$line(@$log) {3203if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3204$signoff=1;3205$empty=0;3206if(!$opts{'-remove_signoff'}) {3207print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3208next;3209}else{3210# remove signoff lines3211next;3212}3213}else{3214$signoff=0;3215}32163217# print only one empty line3218# do not print empty line after signoff3219if($lineeq"") {3220next if($empty||$signoff);3221$empty=1;3222}else{3223$empty=0;3224}32253226print format_log_line_html($line) ."<br/>\n";3227}32283229if($opts{'-final_empty_line'}) {3230# end with single empty line3231print"<br/>\n"unless$empty;3232}3233}32343235# return link target (what link points to)3236sub git_get_link_target {3237my$hash=shift;3238my$link_target;32393240# read link3241open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3242orreturn;3243{3244local$/;3245$link_target= <$fd>;3246}3247close$fd3248orreturn;32493250return$link_target;3251}32523253# given link target, and the directory (basedir) the link is in,3254# return target of link relative to top directory (top tree);3255# return undef if it is not possible (including absolute links).3256sub normalize_link_target {3257my($link_target,$basedir,$hash_base) =@_;32583259# we can normalize symlink target only if $hash_base is provided3260return unless$hash_base;32613262# absolute symlinks (beginning with '/') cannot be normalized3263return if(substr($link_target,0,1)eq'/');32643265# normalize link target to path from top (root) tree (dir)3266my$path;3267if($basedir) {3268$path=$basedir.'/'.$link_target;3269}else{3270# we are in top (root) tree (dir)3271$path=$link_target;3272}32733274# remove //, /./, and /../3275my@path_parts;3276foreachmy$part(split('/',$path)) {3277# discard '.' and ''3278next if(!$part||$parteq'.');3279# handle '..'3280if($parteq'..') {3281if(@path_parts) {3282pop@path_parts;3283}else{3284# link leads outside repository (outside top dir)3285return;3286}3287}else{3288push@path_parts,$part;3289}3290}3291$path=join('/',@path_parts);32923293return$path;3294}32953296# print tree entry (row of git_tree), but without encompassing <tr> element3297sub git_print_tree_entry {3298my($t,$basedir,$hash_base,$have_blame) =@_;32993300my%base_key= ();3301$base_key{'hash_base'} =$hash_baseifdefined$hash_base;33023303# The format of a table row is: mode list link. Where mode is3304# the mode of the entry, list is the name of the entry, an href,3305# and link is the action links of the entry.33063307print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3308if($t->{'type'}eq"blob") {3309print"<td class=\"list\">".3310$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3311 file_name=>"$basedir$t->{'name'}",%base_key),3312-class=>"list"}, esc_path($t->{'name'}));3313if(S_ISLNK(oct$t->{'mode'})) {3314my$link_target= git_get_link_target($t->{'hash'});3315if($link_target) {3316my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3317if(defined$norm_target) {3318print" -> ".3319$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3320 file_name=>$norm_target),3321-title =>$norm_target}, esc_path($link_target));3322}else{3323print" -> ". esc_path($link_target);3324}3325}3326}3327print"</td>\n";3328print"<td class=\"link\">";3329print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3330 file_name=>"$basedir$t->{'name'}",%base_key)},3331"blob");3332if($have_blame) {3333print" | ".3334$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3335 file_name=>"$basedir$t->{'name'}",%base_key)},3336"blame");3337}3338if(defined$hash_base) {3339print" | ".3340$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3341 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3342"history");3343}3344print" | ".3345$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3346 file_name=>"$basedir$t->{'name'}")},3347"raw");3348print"</td>\n";33493350}elsif($t->{'type'}eq"tree") {3351print"<td class=\"list\">";3352print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3353 file_name=>"$basedir$t->{'name'}",%base_key)},3354 esc_path($t->{'name'}));3355print"</td>\n";3356print"<td class=\"link\">";3357print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3358 file_name=>"$basedir$t->{'name'}",%base_key)},3359"tree");3360if(defined$hash_base) {3361print" | ".3362$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3363 file_name=>"$basedir$t->{'name'}")},3364"history");3365}3366print"</td>\n";3367}else{3368# unknown object: we can only present history for it3369# (this includes 'commit' object, i.e. submodule support)3370print"<td class=\"list\">".3371 esc_path($t->{'name'}) .3372"</td>\n";3373print"<td class=\"link\">";3374if(defined$hash_base) {3375print$cgi->a({-href => href(action=>"history",3376 hash_base=>$hash_base,3377 file_name=>"$basedir$t->{'name'}")},3378"history");3379}3380print"</td>\n";3381}3382}33833384## ......................................................................3385## functions printing large fragments of HTML33863387# get pre-image filenames for merge (combined) diff3388sub fill_from_file_info {3389my($diff,@parents) =@_;33903391$diff->{'from_file'} = [ ];3392$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3393for(my$i=0;$i<$diff->{'nparents'};$i++) {3394if($diff->{'status'}[$i]eq'R'||3395$diff->{'status'}[$i]eq'C') {3396$diff->{'from_file'}[$i] =3397 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3398}3399}34003401return$diff;3402}34033404# is current raw difftree line of file deletion3405sub is_deleted {3406my$diffinfo=shift;34073408return$diffinfo->{'to_id'}eq('0' x 40);3409}34103411# does patch correspond to [previous] difftree raw line3412# $diffinfo - hashref of parsed raw diff format3413# $patchinfo - hashref of parsed patch diff format3414# (the same keys as in $diffinfo)3415sub is_patch_split {3416my($diffinfo,$patchinfo) =@_;34173418returndefined$diffinfo&&defined$patchinfo3419&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3420}342134223423sub git_difftree_body {3424my($difftree,$hash,@parents) =@_;3425my($parent) =$parents[0];3426my($have_blame) = gitweb_check_feature('blame');3427print"<div class=\"list_head\">\n";3428if($#{$difftree} >10) {3429print(($#{$difftree} +1) ." files changed:\n");3430}3431print"</div>\n";34323433print"<table class=\"".3434(@parents>1?"combined ":"") .3435"diff_tree\">\n";34363437# header only for combined diff in 'commitdiff' view3438my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3439if($has_header) {3440# table header3441print"<thead><tr>\n".3442"<th></th><th></th>\n";# filename, patchN link3443for(my$i=0;$i<@parents;$i++) {3444my$par=$parents[$i];3445print"<th>".3446$cgi->a({-href => href(action=>"commitdiff",3447 hash=>$hash, hash_parent=>$par),3448-title =>'commitdiff to parent number '.3449($i+1) .': '.substr($par,0,7)},3450$i+1) .3451" </th>\n";3452}3453print"</tr></thead>\n<tbody>\n";3454}34553456my$alternate=1;3457my$patchno=0;3458foreachmy$line(@{$difftree}) {3459my$diff= parsed_difftree_line($line);34603461if($alternate) {3462print"<tr class=\"dark\">\n";3463}else{3464print"<tr class=\"light\">\n";3465}3466$alternate^=1;34673468if(exists$diff->{'nparents'}) {# combined diff34693470 fill_from_file_info($diff,@parents)3471unlessexists$diff->{'from_file'};34723473if(!is_deleted($diff)) {3474# file exists in the result (child) commit3475print"<td>".3476$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3477 file_name=>$diff->{'to_file'},3478 hash_base=>$hash),3479-class=>"list"}, esc_path($diff->{'to_file'})) .3480"</td>\n";3481}else{3482print"<td>".3483 esc_path($diff->{'to_file'}) .3484"</td>\n";3485}34863487if($actioneq'commitdiff') {3488# link to patch3489$patchno++;3490print"<td class=\"link\">".3491$cgi->a({-href =>"#patch$patchno"},"patch") .3492" | ".3493"</td>\n";3494}34953496my$has_history=0;3497my$not_deleted=0;3498for(my$i=0;$i<$diff->{'nparents'};$i++) {3499my$hash_parent=$parents[$i];3500my$from_hash=$diff->{'from_id'}[$i];3501my$from_path=$diff->{'from_file'}[$i];3502my$status=$diff->{'status'}[$i];35033504$has_history||= ($statusne'A');3505$not_deleted||= ($statusne'D');35063507if($statuseq'A') {3508print"<td class=\"link\"align=\"right\"> | </td>\n";3509}elsif($statuseq'D') {3510print"<td class=\"link\">".3511$cgi->a({-href => href(action=>"blob",3512 hash_base=>$hash,3513 hash=>$from_hash,3514 file_name=>$from_path)},3515"blob". ($i+1)) .3516" | </td>\n";3517}else{3518if($diff->{'to_id'}eq$from_hash) {3519print"<td class=\"link nochange\">";3520}else{3521print"<td class=\"link\">";3522}3523print$cgi->a({-href => href(action=>"blobdiff",3524 hash=>$diff->{'to_id'},3525 hash_parent=>$from_hash,3526 hash_base=>$hash,3527 hash_parent_base=>$hash_parent,3528 file_name=>$diff->{'to_file'},3529 file_parent=>$from_path)},3530"diff". ($i+1)) .3531" | </td>\n";3532}3533}35343535print"<td class=\"link\">";3536if($not_deleted) {3537print$cgi->a({-href => href(action=>"blob",3538 hash=>$diff->{'to_id'},3539 file_name=>$diff->{'to_file'},3540 hash_base=>$hash)},3541"blob");3542print" | "if($has_history);3543}3544if($has_history) {3545print$cgi->a({-href => href(action=>"history",3546 file_name=>$diff->{'to_file'},3547 hash_base=>$hash)},3548"history");3549}3550print"</td>\n";35513552print"</tr>\n";3553next;# instead of 'else' clause, to avoid extra indent3554}3555# else ordinary diff35563557my($to_mode_oct,$to_mode_str,$to_file_type);3558my($from_mode_oct,$from_mode_str,$from_file_type);3559if($diff->{'to_mode'}ne('0' x 6)) {3560$to_mode_oct=oct$diff->{'to_mode'};3561if(S_ISREG($to_mode_oct)) {# only for regular file3562$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3563}3564$to_file_type= file_type($diff->{'to_mode'});3565}3566if($diff->{'from_mode'}ne('0' x 6)) {3567$from_mode_oct=oct$diff->{'from_mode'};3568if(S_ISREG($to_mode_oct)) {# only for regular file3569$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3570}3571$from_file_type= file_type($diff->{'from_mode'});3572}35733574if($diff->{'status'}eq"A") {# created3575my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3576$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3577$mode_chng.="]</span>";3578print"<td>";3579print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3580 hash_base=>$hash, file_name=>$diff->{'file'}),3581-class=>"list"}, esc_path($diff->{'file'}));3582print"</td>\n";3583print"<td>$mode_chng</td>\n";3584print"<td class=\"link\">";3585if($actioneq'commitdiff') {3586# link to patch3587$patchno++;3588print$cgi->a({-href =>"#patch$patchno"},"patch");3589print" | ";3590}3591print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3592 hash_base=>$hash, file_name=>$diff->{'file'})},3593"blob");3594print"</td>\n";35953596}elsif($diff->{'status'}eq"D") {# deleted3597my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3598print"<td>";3599print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3600 hash_base=>$parent, file_name=>$diff->{'file'}),3601-class=>"list"}, esc_path($diff->{'file'}));3602print"</td>\n";3603print"<td>$mode_chng</td>\n";3604print"<td class=\"link\">";3605if($actioneq'commitdiff') {3606# link to patch3607$patchno++;3608print$cgi->a({-href =>"#patch$patchno"},"patch");3609print" | ";3610}3611print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3612 hash_base=>$parent, file_name=>$diff->{'file'})},3613"blob") ." | ";3614if($have_blame) {3615print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3616 file_name=>$diff->{'file'})},3617"blame") ." | ";3618}3619print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3620 file_name=>$diff->{'file'})},3621"history");3622print"</td>\n";36233624}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3625my$mode_chnge="";3626if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3627$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3628if($from_file_typene$to_file_type) {3629$mode_chnge.=" from$from_file_typeto$to_file_type";3630}3631if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3632if($from_mode_str&&$to_mode_str) {3633$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3634}elsif($to_mode_str) {3635$mode_chnge.=" mode:$to_mode_str";3636}3637}3638$mode_chnge.="]</span>\n";3639}3640print"<td>";3641print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3642 hash_base=>$hash, file_name=>$diff->{'file'}),3643-class=>"list"}, esc_path($diff->{'file'}));3644print"</td>\n";3645print"<td>$mode_chnge</td>\n";3646print"<td class=\"link\">";3647if($actioneq'commitdiff') {3648# link to patch3649$patchno++;3650print$cgi->a({-href =>"#patch$patchno"},"patch") .3651" | ";3652}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3653# "commit" view and modified file (not onlu mode changed)3654print$cgi->a({-href => href(action=>"blobdiff",3655 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3656 hash_base=>$hash, hash_parent_base=>$parent,3657 file_name=>$diff->{'file'})},3658"diff") .3659" | ";3660}3661print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3662 hash_base=>$hash, file_name=>$diff->{'file'})},3663"blob") ." | ";3664if($have_blame) {3665print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3666 file_name=>$diff->{'file'})},3667"blame") ." | ";3668}3669print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3670 file_name=>$diff->{'file'})},3671"history");3672print"</td>\n";36733674}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3675my%status_name= ('R'=>'moved','C'=>'copied');3676my$nstatus=$status_name{$diff->{'status'}};3677my$mode_chng="";3678if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3679# mode also for directories, so we cannot use $to_mode_str3680$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3681}3682print"<td>".3683$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3684 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3685-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3686"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3687$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3688 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3689-class=>"list"}, esc_path($diff->{'from_file'})) .3690" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3691"<td class=\"link\">";3692if($actioneq'commitdiff') {3693# link to patch3694$patchno++;3695print$cgi->a({-href =>"#patch$patchno"},"patch") .3696" | ";3697}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3698# "commit" view and modified file (not only pure rename or copy)3699print$cgi->a({-href => href(action=>"blobdiff",3700 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3701 hash_base=>$hash, hash_parent_base=>$parent,3702 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3703"diff") .3704" | ";3705}3706print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3707 hash_base=>$parent, file_name=>$diff->{'to_file'})},3708"blob") ." | ";3709if($have_blame) {3710print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3711 file_name=>$diff->{'to_file'})},3712"blame") ." | ";3713}3714print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3715 file_name=>$diff->{'to_file'})},3716"history");3717print"</td>\n";37183719}# we should not encounter Unmerged (U) or Unknown (X) status3720print"</tr>\n";3721}3722print"</tbody>"if$has_header;3723print"</table>\n";3724}37253726sub git_patchset_body {3727my($fd,$difftree,$hash,@hash_parents) =@_;3728my($hash_parent) =$hash_parents[0];37293730my$is_combined= (@hash_parents>1);3731my$patch_idx=0;3732my$patch_number=0;3733my$patch_line;3734my$diffinfo;3735my$to_name;3736my(%from,%to);37373738print"<div class=\"patchset\">\n";37393740# skip to first patch3741while($patch_line= <$fd>) {3742chomp$patch_line;37433744last if($patch_line=~m/^diff /);3745}37463747 PATCH:3748while($patch_line) {37493750# parse "git diff" header line3751if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3752# $1 is from_name, which we do not use3753$to_name= unquote($2);3754$to_name=~s!^b/!!;3755}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3756# $1 is 'cc' or 'combined', which we do not use3757$to_name= unquote($2);3758}else{3759$to_name=undef;3760}37613762# check if current patch belong to current raw line3763# and parse raw git-diff line if needed3764if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3765# this is continuation of a split patch3766print"<div class=\"patch cont\">\n";3767}else{3768# advance raw git-diff output if needed3769$patch_idx++ifdefined$diffinfo;37703771# read and prepare patch information3772$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);37733774# compact combined diff output can have some patches skipped3775# find which patch (using pathname of result) we are at now;3776if($is_combined) {3777while($to_namene$diffinfo->{'to_file'}) {3778print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3779 format_diff_cc_simplified($diffinfo,@hash_parents) .3780"</div>\n";# class="patch"37813782$patch_idx++;3783$patch_number++;37843785last if$patch_idx>$#$difftree;3786$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3787}3788}37893790# modifies %from, %to hashes3791 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);37923793# this is first patch for raw difftree line with $patch_idx index3794# we index @$difftree array from 0, but number patches from 13795print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3796}37973798# git diff header3799#assert($patch_line =~ m/^diff /) if DEBUG;3800#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3801$patch_number++;3802# print "git diff" header3803print format_git_diff_header_line($patch_line,$diffinfo,3804 \%from, \%to);38053806# print extended diff header3807print"<div class=\"diff extended_header\">\n";3808 EXTENDED_HEADER:3809while($patch_line= <$fd>) {3810chomp$patch_line;38113812last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);38133814print format_extended_diff_header_line($patch_line,$diffinfo,3815 \%from, \%to);3816}3817print"</div>\n";# class="diff extended_header"38183819# from-file/to-file diff header3820if(!$patch_line) {3821print"</div>\n";# class="patch"3822last PATCH;3823}3824next PATCH if($patch_line=~m/^diff /);3825#assert($patch_line =~ m/^---/) if DEBUG;38263827my$last_patch_line=$patch_line;3828$patch_line= <$fd>;3829chomp$patch_line;3830#assert($patch_line =~ m/^\+\+\+/) if DEBUG;38313832print format_diff_from_to_header($last_patch_line,$patch_line,3833$diffinfo, \%from, \%to,3834@hash_parents);38353836# the patch itself3837 LINE:3838while($patch_line= <$fd>) {3839chomp$patch_line;38403841next PATCH if($patch_line=~m/^diff /);38423843print format_diff_line($patch_line, \%from, \%to);3844}38453846}continue{3847print"</div>\n";# class="patch"3848}38493850# for compact combined (--cc) format, with chunk and patch simpliciaction3851# patchset might be empty, but there might be unprocessed raw lines3852for(++$patch_idxif$patch_number>0;3853$patch_idx<@$difftree;3854++$patch_idx) {3855# read and prepare patch information3856$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38573858# generate anchor for "patch" links in difftree / whatchanged part3859print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3860 format_diff_cc_simplified($diffinfo,@hash_parents) .3861"</div>\n";# class="patch"38623863$patch_number++;3864}38653866if($patch_number==0) {3867if(@hash_parents>1) {3868print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3869}else{3870print"<div class=\"diff nodifferences\">No differences found</div>\n";3871}3872}38733874print"</div>\n";# class="patchset"3875}38763877# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38783879# fills project list info (age, description, owner, forks) for each3880# project in the list, removing invalid projects from returned list3881# NOTE: modifies $projlist, but does not remove entries from it3882sub fill_project_list_info {3883my($projlist,$check_forks) =@_;3884my@projects;38853886my$show_ctags= gitweb_check_feature('ctags');3887 PROJECT:3888foreachmy$pr(@$projlist) {3889my(@activity) = git_get_last_activity($pr->{'path'});3890unless(@activity) {3891next PROJECT;3892}3893($pr->{'age'},$pr->{'age_string'}) =@activity;3894if(!defined$pr->{'descr'}) {3895my$descr= git_get_project_description($pr->{'path'}) ||"";3896$descr= to_utf8($descr);3897$pr->{'descr_long'} =$descr;3898$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3899}3900if(!defined$pr->{'owner'}) {3901$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3902}3903if($check_forks) {3904my$pname=$pr->{'path'};3905if(($pname=~s/\.git$//) &&3906($pname!~/\/$/) &&3907(-d "$projectroot/$pname")) {3908$pr->{'forks'} ="-d$projectroot/$pname";3909}else{3910$pr->{'forks'} =0;3911}3912}3913$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3914push@projects,$pr;3915}39163917return@projects;3918}39193920# print 'sort by' <th> element, generating 'sort by $name' replay link3921# if that order is not selected3922sub print_sort_th {3923my($name,$order,$header) =@_;3924$header||=ucfirst($name);39253926if($ordereq$name) {3927print"<th>$header</th>\n";3928}else{3929print"<th>".3930$cgi->a({-href => href(-replay=>1, order=>$name),3931-class=>"header"},$header) .3932"</th>\n";3933}3934}39353936sub git_project_list_body {3937# actually uses global variable $project3938my($projlist,$order,$from,$to,$extra,$no_header) =@_;39393940my($check_forks) = gitweb_check_feature('forks');3941my@projects= fill_project_list_info($projlist,$check_forks);39423943$order||=$default_projects_order;3944$from=0unlessdefined$from;3945$to=$#projectsif(!defined$to||$#projects<$to);39463947my%order_info= (3948 project => { key =>'path', type =>'str'},3949 descr => { key =>'descr_long', type =>'str'},3950 owner => { key =>'owner', type =>'str'},3951 age => { key =>'age', type =>'num'}3952);3953my$oi=$order_info{$order};3954if($oi->{'type'}eq'str') {3955@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;3956}else{3957@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;3958}39593960my$show_ctags= gitweb_check_feature('ctags');3961if($show_ctags) {3962my%ctags;3963foreachmy$p(@projects) {3964foreachmy$ct(keys%{$p->{'ctags'}}) {3965$ctags{$ct} +=$p->{'ctags'}->{$ct};3966}3967}3968my$cloud= git_populate_project_tagcloud(\%ctags);3969print git_show_project_tagcloud($cloud,64);3970}39713972print"<table class=\"project_list\">\n";3973unless($no_header) {3974print"<tr>\n";3975if($check_forks) {3976print"<th></th>\n";3977}3978 print_sort_th('project',$order,'Project');3979 print_sort_th('descr',$order,'Description');3980 print_sort_th('owner',$order,'Owner');3981 print_sort_th('age',$order,'Last Change');3982print"<th></th>\n".# for links3983"</tr>\n";3984}3985my$alternate=1;3986my$tagfilter=$cgi->param('by_tag');3987for(my$i=$from;$i<=$to;$i++) {3988my$pr=$projects[$i];39893990next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};3991next if$searchtextand not$pr->{'path'} =~/$searchtext/3992and not$pr->{'descr_long'} =~/$searchtext/;3993# Weed out forks or non-matching entries of search3994if($check_forks) {3995my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;3996$forkbase="^$forkbase"if$forkbase;3997next ifnot$searchtextand not$tagfilterand$show_ctags3998and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe3999}40004001if($alternate) {4002print"<tr class=\"dark\">\n";4003}else{4004print"<tr class=\"light\">\n";4005}4006$alternate^=1;4007if($check_forks) {4008print"<td>";4009if($pr->{'forks'}) {4010print"<!--$pr->{'forks'} -->\n";4011print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4012}4013print"</td>\n";4014}4015print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4016-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4017"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4018-class=>"list", -title =>$pr->{'descr_long'}},4019 esc_html($pr->{'descr'})) ."</td>\n".4020"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4021print"<td class=\"". age_class($pr->{'age'}) ."\">".4022(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4023"<td class=\"link\">".4024$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4025$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4026$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4027$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4028($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4029"</td>\n".4030"</tr>\n";4031}4032if(defined$extra) {4033print"<tr>\n";4034if($check_forks) {4035print"<td></td>\n";4036}4037print"<td colspan=\"5\">$extra</td>\n".4038"</tr>\n";4039}4040print"</table>\n";4041}40424043sub git_shortlog_body {4044# uses global variable $project4045my($commitlist,$from,$to,$refs,$extra) =@_;40464047$from=0unlessdefined$from;4048$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);40494050print"<table class=\"shortlog\">\n";4051my$alternate=1;4052for(my$i=$from;$i<=$to;$i++) {4053my%co= %{$commitlist->[$i]};4054my$commit=$co{'id'};4055my$ref= format_ref_marker($refs,$commit);4056if($alternate) {4057print"<tr class=\"dark\">\n";4058}else{4059print"<tr class=\"light\">\n";4060}4061$alternate^=1;4062my$author= chop_and_escape_str($co{'author_name'},10);4063# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4064print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4065"<td><i>".$author."</i></td>\n".4066"<td>";4067print format_subject_html($co{'title'},$co{'title_short'},4068 href(action=>"commit", hash=>$commit),$ref);4069print"</td>\n".4070"<td class=\"link\">".4071$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4072$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4073$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4074my$snapshot_links= format_snapshot_links($commit);4075if(defined$snapshot_links) {4076print" | ".$snapshot_links;4077}4078print"</td>\n".4079"</tr>\n";4080}4081if(defined$extra) {4082print"<tr>\n".4083"<td colspan=\"4\">$extra</td>\n".4084"</tr>\n";4085}4086print"</table>\n";4087}40884089sub git_history_body {4090# Warning: assumes constant type (blob or tree) during history4091my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;40924093$from=0unlessdefined$from;4094$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});40954096print"<table class=\"history\">\n";4097my$alternate=1;4098for(my$i=$from;$i<=$to;$i++) {4099my%co= %{$commitlist->[$i]};4100if(!%co) {4101next;4102}4103my$commit=$co{'id'};41044105my$ref= format_ref_marker($refs,$commit);41064107if($alternate) {4108print"<tr class=\"dark\">\n";4109}else{4110print"<tr class=\"light\">\n";4111}4112$alternate^=1;4113# shortlog uses chop_str($co{'author_name'}, 10)4114my$author= chop_and_escape_str($co{'author_name'},15,3);4115print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4116"<td><i>".$author."</i></td>\n".4117"<td>";4118# originally git_history used chop_str($co{'title'}, 50)4119print format_subject_html($co{'title'},$co{'title_short'},4120 href(action=>"commit", hash=>$commit),$ref);4121print"</td>\n".4122"<td class=\"link\">".4123$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4124$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");41254126if($ftypeeq'blob') {4127my$blob_current= git_get_hash_by_path($hash_base,$file_name);4128my$blob_parent= git_get_hash_by_path($commit,$file_name);4129if(defined$blob_current&&defined$blob_parent&&4130$blob_currentne$blob_parent) {4131print" | ".4132$cgi->a({-href => href(action=>"blobdiff",4133 hash=>$blob_current, hash_parent=>$blob_parent,4134 hash_base=>$hash_base, hash_parent_base=>$commit,4135 file_name=>$file_name)},4136"diff to current");4137}4138}4139print"</td>\n".4140"</tr>\n";4141}4142if(defined$extra) {4143print"<tr>\n".4144"<td colspan=\"4\">$extra</td>\n".4145"</tr>\n";4146}4147print"</table>\n";4148}41494150sub git_tags_body {4151# uses global variable $project4152my($taglist,$from,$to,$extra) =@_;4153$from=0unlessdefined$from;4154$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);41554156print"<table class=\"tags\">\n";4157my$alternate=1;4158for(my$i=$from;$i<=$to;$i++) {4159my$entry=$taglist->[$i];4160my%tag=%$entry;4161my$comment=$tag{'subject'};4162my$comment_short;4163if(defined$comment) {4164$comment_short= chop_str($comment,30,5);4165}4166if($alternate) {4167print"<tr class=\"dark\">\n";4168}else{4169print"<tr class=\"light\">\n";4170}4171$alternate^=1;4172if(defined$tag{'age'}) {4173print"<td><i>$tag{'age'}</i></td>\n";4174}else{4175print"<td></td>\n";4176}4177print"<td>".4178$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4179-class=>"list name"}, esc_html($tag{'name'})) .4180"</td>\n".4181"<td>";4182if(defined$comment) {4183print format_subject_html($comment,$comment_short,4184 href(action=>"tag", hash=>$tag{'id'}));4185}4186print"</td>\n".4187"<td class=\"selflink\">";4188if($tag{'type'}eq"tag") {4189print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4190}else{4191print" ";4192}4193print"</td>\n".4194"<td class=\"link\">"." | ".4195$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4196if($tag{'reftype'}eq"commit") {4197print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4198" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4199}elsif($tag{'reftype'}eq"blob") {4200print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4201}4202print"</td>\n".4203"</tr>";4204}4205if(defined$extra) {4206print"<tr>\n".4207"<td colspan=\"5\">$extra</td>\n".4208"</tr>\n";4209}4210print"</table>\n";4211}42124213sub git_heads_body {4214# uses global variable $project4215my($headlist,$head,$from,$to,$extra) =@_;4216$from=0unlessdefined$from;4217$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);42184219print"<table class=\"heads\">\n";4220my$alternate=1;4221for(my$i=$from;$i<=$to;$i++) {4222my$entry=$headlist->[$i];4223my%ref=%$entry;4224my$curr=$ref{'id'}eq$head;4225if($alternate) {4226print"<tr class=\"dark\">\n";4227}else{4228print"<tr class=\"light\">\n";4229}4230$alternate^=1;4231print"<td><i>$ref{'age'}</i></td>\n".4232($curr?"<td class=\"current_head\">":"<td>") .4233$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4234-class=>"list name"},esc_html($ref{'name'})) .4235"</td>\n".4236"<td class=\"link\">".4237$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4238$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4239$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4240"</td>\n".4241"</tr>";4242}4243if(defined$extra) {4244print"<tr>\n".4245"<td colspan=\"3\">$extra</td>\n".4246"</tr>\n";4247}4248print"</table>\n";4249}42504251sub git_search_grep_body {4252my($commitlist,$from,$to,$extra) =@_;4253$from=0unlessdefined$from;4254$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);42554256print"<table class=\"commit_search\">\n";4257my$alternate=1;4258for(my$i=$from;$i<=$to;$i++) {4259my%co= %{$commitlist->[$i]};4260if(!%co) {4261next;4262}4263my$commit=$co{'id'};4264if($alternate) {4265print"<tr class=\"dark\">\n";4266}else{4267print"<tr class=\"light\">\n";4268}4269$alternate^=1;4270my$author= chop_and_escape_str($co{'author_name'},15,5);4271print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4272"<td><i>".$author."</i></td>\n".4273"<td>".4274$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4275-class=>"list subject"},4276 chop_and_escape_str($co{'title'},50) ."<br/>");4277my$comment=$co{'comment'};4278foreachmy$line(@$comment) {4279if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4280my($lead,$match,$trail) = ($1,$2,$3);4281$match= chop_str($match,70,5,'center');4282my$contextlen=int((80-length($match))/2);4283$contextlen=30if($contextlen>30);4284$lead= chop_str($lead,$contextlen,10,'left');4285$trail= chop_str($trail,$contextlen,10,'right');42864287$lead= esc_html($lead);4288$match= esc_html($match);4289$trail= esc_html($trail);42904291print"$lead<span class=\"match\">$match</span>$trail<br />";4292}4293}4294print"</td>\n".4295"<td class=\"link\">".4296$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4297" | ".4298$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4299" | ".4300$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4301print"</td>\n".4302"</tr>\n";4303}4304if(defined$extra) {4305print"<tr>\n".4306"<td colspan=\"3\">$extra</td>\n".4307"</tr>\n";4308}4309print"</table>\n";4310}43114312## ======================================================================4313## ======================================================================4314## actions43154316sub git_project_list {4317my$order=$input_params{'order'};4318if(defined$order&&$order!~m/none|project|descr|owner|age/) {4319 die_error(400,"Unknown order parameter");4320}43214322my@list= git_get_projects_list();4323if(!@list) {4324 die_error(404,"No projects found");4325}43264327 git_header_html();4328if(-f $home_text) {4329print"<div class=\"index_include\">\n";4330open(my$fd,$home_text);4331print<$fd>;4332close$fd;4333print"</div>\n";4334}4335print$cgi->startform(-method=>"get") .4336"<p class=\"projsearch\">Search:\n".4337$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4338"</p>".4339$cgi->end_form() ."\n";4340 git_project_list_body(\@list,$order);4341 git_footer_html();4342}43434344sub git_forks {4345my$order=$input_params{'order'};4346if(defined$order&&$order!~m/none|project|descr|owner|age/) {4347 die_error(400,"Unknown order parameter");4348}43494350my@list= git_get_projects_list($project);4351if(!@list) {4352 die_error(404,"No forks found");4353}43544355 git_header_html();4356 git_print_page_nav('','');4357 git_print_header_div('summary',"$projectforks");4358 git_project_list_body(\@list,$order);4359 git_footer_html();4360}43614362sub git_project_index {4363my@projects= git_get_projects_list($project);43644365print$cgi->header(4366-type =>'text/plain',4367-charset =>'utf-8',4368-content_disposition =>'inline; filename="index.aux"');43694370foreachmy$pr(@projects) {4371if(!exists$pr->{'owner'}) {4372$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4373}43744375my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4376# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4377$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4378$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4379$path=~s/ /\+/g;4380$owner=~s/ /\+/g;43814382print"$path$owner\n";4383}4384}43854386sub git_summary {4387my$descr= git_get_project_description($project) ||"none";4388my%co= parse_commit("HEAD");4389my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4390my$head=$co{'id'};43914392my$owner= git_get_project_owner($project);43934394my$refs= git_get_references();4395# These get_*_list functions return one more to allow us to see if4396# there are more ...4397my@taglist= git_get_tags_list(16);4398my@headlist= git_get_heads_list(16);4399my@forklist;4400my($check_forks) = gitweb_check_feature('forks');44014402if($check_forks) {4403@forklist= git_get_projects_list($project);4404}44054406 git_header_html();4407 git_print_page_nav('summary','',$head);44084409print"<div class=\"title\"> </div>\n";4410print"<table class=\"projects_list\">\n".4411"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4412"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4413if(defined$cd{'rfc2822'}) {4414print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4415}44164417# use per project git URL list in $projectroot/$project/cloneurl4418# or make project git URL from git base URL and project name4419my$url_tag="URL";4420my@url_list= git_get_project_url_list($project);4421@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4422foreachmy$git_url(@url_list) {4423next unless$git_url;4424print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4425$url_tag="";4426}44274428# Tag cloud4429my$show_ctags= (gitweb_check_feature('ctags'))[0];4430if($show_ctags) {4431my$ctags= git_get_project_ctags($project);4432my$cloud= git_populate_project_tagcloud($ctags);4433print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4434print"</td>\n<td>"unless%$ctags;4435print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4436print"</td>\n<td>"if%$ctags;4437print git_show_project_tagcloud($cloud,48);4438print"</td></tr>";4439}44404441print"</table>\n";44424443if(-s "$projectroot/$project/README.html") {4444if(open my$fd,"$projectroot/$project/README.html") {4445print"<div class=\"title\">readme</div>\n".4446"<div class=\"readme\">\n";4447print$_while(<$fd>);4448print"\n</div>\n";# class="readme"4449close$fd;4450}4451}44524453# we need to request one more than 16 (0..15) to check if4454# those 16 are all4455my@commitlist=$head? parse_commits($head,17) : ();4456if(@commitlist) {4457 git_print_header_div('shortlog');4458 git_shortlog_body(\@commitlist,0,15,$refs,4459$#commitlist<=15?undef:4460$cgi->a({-href => href(action=>"shortlog")},"..."));4461}44624463if(@taglist) {4464 git_print_header_div('tags');4465 git_tags_body(\@taglist,0,15,4466$#taglist<=15?undef:4467$cgi->a({-href => href(action=>"tags")},"..."));4468}44694470if(@headlist) {4471 git_print_header_div('heads');4472 git_heads_body(\@headlist,$head,0,15,4473$#headlist<=15?undef:4474$cgi->a({-href => href(action=>"heads")},"..."));4475}44764477if(@forklist) {4478 git_print_header_div('forks');4479 git_project_list_body(\@forklist,'age',0,15,4480$#forklist<=15?undef:4481$cgi->a({-href => href(action=>"forks")},"..."),4482'no_header');4483}44844485 git_footer_html();4486}44874488sub git_tag {4489my$head= git_get_head_hash($project);4490 git_header_html();4491 git_print_page_nav('','',$head,undef,$head);4492my%tag= parse_tag($hash);44934494if(!%tag) {4495 die_error(404,"Unknown tag object");4496}44974498 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4499print"<div class=\"title_text\">\n".4500"<table class=\"object_header\">\n".4501"<tr>\n".4502"<td>object</td>\n".4503"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4504$tag{'object'}) ."</td>\n".4505"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4506$tag{'type'}) ."</td>\n".4507"</tr>\n";4508if(defined($tag{'author'})) {4509my%ad= parse_date($tag{'epoch'},$tag{'tz'});4510print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4511print"<tr><td></td><td>".$ad{'rfc2822'} .4512sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4513"</td></tr>\n";4514}4515print"</table>\n\n".4516"</div>\n";4517print"<div class=\"page_body\">";4518my$comment=$tag{'comment'};4519foreachmy$line(@$comment) {4520chomp$line;4521print esc_html($line, -nbsp=>1) ."<br/>\n";4522}4523print"</div>\n";4524 git_footer_html();4525}45264527sub git_blame {4528my$fd;4529my$ftype;45304531 gitweb_check_feature('blame')4532or die_error(403,"Blame view not allowed");45334534 die_error(400,"No file name given")unless$file_name;4535$hash_base||= git_get_head_hash($project);4536 die_error(404,"Couldn't find base commit")unless($hash_base);4537my%co= parse_commit($hash_base)4538or die_error(404,"Commit not found");4539if(!defined$hash) {4540$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4541or die_error(404,"Error looking up file");4542}4543$ftype= git_get_type($hash);4544if($ftype!~"blob") {4545 die_error(400,"Object is not a blob");4546}4547open($fd,"-|", git_cmd(),"blame",'-p','--',4548$file_name,$hash_base)4549or die_error(500,"Open git-blame failed");4550 git_header_html();4551my$formats_nav=4552$cgi->a({-href => href(action=>"blob", -replay=>1)},4553"blob") .4554" | ".4555$cgi->a({-href => href(action=>"history", -replay=>1)},4556"history") .4557" | ".4558$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4559"HEAD");4560 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4561 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4562 git_print_page_path($file_name,$ftype,$hash_base);4563my@rev_color= (qw(light2 dark2));4564my$num_colors=scalar(@rev_color);4565my$current_color=0;4566my$last_rev;4567print<<HTML;4568<div class="page_body">4569<table class="blame">4570<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4571HTML4572my%metainfo= ();4573while(1) {4574$_= <$fd>;4575last unlessdefined$_;4576my($full_rev,$orig_lineno,$lineno,$group_size) =4577/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;4578if(!exists$metainfo{$full_rev}) {4579$metainfo{$full_rev} = {};4580}4581my$meta=$metainfo{$full_rev};4582while(<$fd>) {4583last if(s/^\t//);4584if(/^(\S+) (.*)$/) {4585$meta->{$1} =$2;4586}4587}4588my$data=$_;4589chomp$data;4590my$rev=substr($full_rev,0,8);4591my$author=$meta->{'author'};4592my%date= parse_date($meta->{'author-time'},4593$meta->{'author-tz'});4594my$date=$date{'iso-tz'};4595if($group_size) {4596$current_color= ++$current_color%$num_colors;4597}4598print"<tr class=\"$rev_color[$current_color]\">\n";4599if($group_size) {4600print"<td class=\"sha1\"";4601print" title=\"". esc_html($author) .",$date\"";4602print" rowspan=\"$group_size\""if($group_size>1);4603print">";4604print$cgi->a({-href => href(action=>"commit",4605 hash=>$full_rev,4606 file_name=>$file_name)},4607 esc_html($rev));4608print"</td>\n";4609}4610open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4611or die_error(500,"Open git-rev-parse failed");4612my$parent_commit= <$dd>;4613close$dd;4614chomp($parent_commit);4615my$blamed= href(action =>'blame',4616 file_name =>$meta->{'filename'},4617 hash_base =>$parent_commit);4618print"<td class=\"linenr\">";4619print$cgi->a({ -href =>"$blamed#l$orig_lineno",4620-id =>"l$lineno",4621-class=>"linenr"},4622 esc_html($lineno));4623print"</td>";4624print"<td class=\"pre\">". esc_html($data) ."</td>\n";4625print"</tr>\n";4626}4627print"</table>\n";4628print"</div>";4629close$fd4630or print"Reading blob failed\n";4631 git_footer_html();4632}46334634sub git_tags {4635my$head= git_get_head_hash($project);4636 git_header_html();4637 git_print_page_nav('','',$head,undef,$head);4638 git_print_header_div('summary',$project);46394640my@tagslist= git_get_tags_list();4641if(@tagslist) {4642 git_tags_body(\@tagslist);4643}4644 git_footer_html();4645}46464647sub git_heads {4648my$head= git_get_head_hash($project);4649 git_header_html();4650 git_print_page_nav('','',$head,undef,$head);4651 git_print_header_div('summary',$project);46524653my@headslist= git_get_heads_list();4654if(@headslist) {4655 git_heads_body(\@headslist,$head);4656}4657 git_footer_html();4658}46594660sub git_blob_plain {4661my$type=shift;4662my$expires;46634664if(!defined$hash) {4665if(defined$file_name) {4666my$base=$hash_base|| git_get_head_hash($project);4667$hash= git_get_hash_by_path($base,$file_name,"blob")4668or die_error(404,"Cannot find file");4669}else{4670 die_error(400,"No file name defined");4671}4672}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4673# blobs defined by non-textual hash id's can be cached4674$expires="+1d";4675}46764677open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4678or die_error(500,"Open git-cat-file blob '$hash' failed");46794680# content-type (can include charset)4681$type= blob_contenttype($fd,$file_name,$type);46824683# "save as" filename, even when no $file_name is given4684my$save_as="$hash";4685if(defined$file_name) {4686$save_as=$file_name;4687}elsif($type=~m/^text\//) {4688$save_as.='.txt';4689}46904691print$cgi->header(4692-type =>$type,4693-expires =>$expires,4694-content_disposition =>'inline; filename="'.$save_as.'"');4695undef$/;4696binmode STDOUT,':raw';4697print<$fd>;4698binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4699$/="\n";4700close$fd;4701}47024703sub git_blob {4704my$expires;47054706if(!defined$hash) {4707if(defined$file_name) {4708my$base=$hash_base|| git_get_head_hash($project);4709$hash= git_get_hash_by_path($base,$file_name,"blob")4710or die_error(404,"Cannot find file");4711}else{4712 die_error(400,"No file name defined");4713}4714}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4715# blobs defined by non-textual hash id's can be cached4716$expires="+1d";4717}47184719my($have_blame) = gitweb_check_feature('blame');4720open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4721or die_error(500,"Couldn't cat$file_name,$hash");4722my$mimetype= blob_mimetype($fd,$file_name);4723if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4724close$fd;4725return git_blob_plain($mimetype);4726}4727# we can have blame only for text/* mimetype4728$have_blame&&= ($mimetype=~m!^text/!);47294730 git_header_html(undef,$expires);4731my$formats_nav='';4732if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4733if(defined$file_name) {4734if($have_blame) {4735$formats_nav.=4736$cgi->a({-href => href(action=>"blame", -replay=>1)},4737"blame") .4738" | ";4739}4740$formats_nav.=4741$cgi->a({-href => href(action=>"history", -replay=>1)},4742"history") .4743" | ".4744$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4745"raw") .4746" | ".4747$cgi->a({-href => href(action=>"blob",4748 hash_base=>"HEAD", file_name=>$file_name)},4749"HEAD");4750}else{4751$formats_nav.=4752$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4753"raw");4754}4755 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4756 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4757}else{4758print"<div class=\"page_nav\">\n".4759"<br/><br/></div>\n".4760"<div class=\"title\">$hash</div>\n";4761}4762 git_print_page_path($file_name,"blob",$hash_base);4763print"<div class=\"page_body\">\n";4764if($mimetype=~m!^image/!) {4765print qq!<img type="$mimetype"!;4766if($file_name) {4767print qq! alt="$file_name" title="$file_name"!;4768}4769print qq! src="! .4770 href(action=>"blob_plain", hash=>$hash,4771 hash_base=>$hash_base, file_name=>$file_name) .4772 qq!"/>\n!;4773}else{4774my$nr;4775while(my$line= <$fd>) {4776chomp$line;4777$nr++;4778$line= untabify($line);4779printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4780$nr,$nr,$nr, esc_html($line, -nbsp=>1);4781}4782}4783close$fd4784or print"Reading blob failed.\n";4785print"</div>";4786 git_footer_html();4787}47884789sub git_tree {4790if(!defined$hash_base) {4791$hash_base="HEAD";4792}4793if(!defined$hash) {4794if(defined$file_name) {4795$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4796}else{4797$hash=$hash_base;4798}4799}4800 die_error(404,"No such tree")unlessdefined($hash);4801$/="\0";4802open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4803or die_error(500,"Open git-ls-tree failed");4804my@entries=map{chomp;$_} <$fd>;4805close$fdor die_error(404,"Reading tree failed");4806$/="\n";48074808my$refs= git_get_references();4809my$ref= format_ref_marker($refs,$hash_base);4810 git_header_html();4811my$basedir='';4812my($have_blame) = gitweb_check_feature('blame');4813if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4814my@views_nav= ();4815if(defined$file_name) {4816push@views_nav,4817$cgi->a({-href => href(action=>"history", -replay=>1)},4818"history"),4819$cgi->a({-href => href(action=>"tree",4820 hash_base=>"HEAD", file_name=>$file_name)},4821"HEAD"),4822}4823my$snapshot_links= format_snapshot_links($hash);4824if(defined$snapshot_links) {4825# FIXME: Should be available when we have no hash base as well.4826push@views_nav,$snapshot_links;4827}4828 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4829 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4830}else{4831undef$hash_base;4832print"<div class=\"page_nav\">\n";4833print"<br/><br/></div>\n";4834print"<div class=\"title\">$hash</div>\n";4835}4836if(defined$file_name) {4837$basedir=$file_name;4838if($basedirne''&&substr($basedir, -1)ne'/') {4839$basedir.='/';4840}4841 git_print_page_path($file_name,'tree',$hash_base);4842}4843print"<div class=\"page_body\">\n";4844print"<table class=\"tree\">\n";4845my$alternate=1;4846# '..' (top directory) link if possible4847if(defined$hash_base&&4848defined$file_name&&$file_name=~m![^/]+$!) {4849if($alternate) {4850print"<tr class=\"dark\">\n";4851}else{4852print"<tr class=\"light\">\n";4853}4854$alternate^=1;48554856my$up=$file_name;4857$up=~s!/?[^/]+$!!;4858undef$upunless$up;4859# based on git_print_tree_entry4860print'<td class="mode">'. mode_str('040000') ."</td>\n";4861print'<td class="list">';4862print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4863 file_name=>$up)},4864"..");4865print"</td>\n";4866print"<td class=\"link\"></td>\n";48674868print"</tr>\n";4869}4870foreachmy$line(@entries) {4871my%t= parse_ls_tree_line($line, -z =>1);48724873if($alternate) {4874print"<tr class=\"dark\">\n";4875}else{4876print"<tr class=\"light\">\n";4877}4878$alternate^=1;48794880 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);48814882print"</tr>\n";4883}4884print"</table>\n".4885"</div>";4886 git_footer_html();4887}48884889sub git_snapshot {4890my$format=$input_params{'snapshot_format'};4891if(!@snapshot_fmts) {4892 die_error(403,"Snapshots not allowed");4893}4894# default to first supported snapshot format4895$format||=$snapshot_fmts[0];4896if($format!~m/^[a-z0-9]+$/) {4897 die_error(400,"Invalid snapshot format parameter");4898}elsif(!exists($known_snapshot_formats{$format})) {4899 die_error(400,"Unknown snapshot format");4900}elsif(!grep($_eq$format,@snapshot_fmts)) {4901 die_error(403,"Unsupported snapshot format");4902}49034904if(!defined$hash) {4905$hash= git_get_head_hash($project);4906}49074908my$name=$project;4909$name=~ s,([^/])/*\.git$,$1,;4910$name= basename($name);4911my$filename= to_utf8($name);4912$name=~s/\047/\047\\\047\047/g;4913my$cmd;4914$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";4915$cmd= quote_command(4916 git_cmd(),'archive',4917"--format=$known_snapshot_formats{$format}{'format'}",4918"--prefix=$name/",$hash);4919if(exists$known_snapshot_formats{$format}{'compressor'}) {4920$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});4921}49224923print$cgi->header(4924-type =>$known_snapshot_formats{$format}{'type'},4925-content_disposition =>'inline; filename="'."$filename".'"',4926-status =>'200 OK');49274928open my$fd,"-|",$cmd4929or die_error(500,"Execute git-archive failed");4930binmode STDOUT,':raw';4931print<$fd>;4932binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4933close$fd;4934}49354936sub git_log {4937my$head= git_get_head_hash($project);4938if(!defined$hash) {4939$hash=$head;4940}4941if(!defined$page) {4942$page=0;4943}4944my$refs= git_get_references();49454946my@commitlist= parse_commits($hash,101, (100*$page));49474948my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);49494950 git_header_html();4951 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);49524953if(!@commitlist) {4954my%co= parse_commit($hash);49554956 git_print_header_div('summary',$project);4957print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";4958}4959my$to= ($#commitlist>=99) ? (99) : ($#commitlist);4960for(my$i=0;$i<=$to;$i++) {4961my%co= %{$commitlist[$i]};4962next if!%co;4963my$commit=$co{'id'};4964my$ref= format_ref_marker($refs,$commit);4965my%ad= parse_date($co{'author_epoch'});4966 git_print_header_div('commit',4967"<span class=\"age\">$co{'age_string'}</span>".4968 esc_html($co{'title'}) .$ref,4969$commit);4970print"<div class=\"title_text\">\n".4971"<div class=\"log_link\">\n".4972$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4973" | ".4974$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4975" | ".4976$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4977"<br/>\n".4978"</div>\n".4979"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".4980"</div>\n";49814982print"<div class=\"log_body\">\n";4983 git_print_log($co{'comment'}, -final_empty_line=>1);4984print"</div>\n";4985}4986if($#commitlist>=100) {4987print"<div class=\"page_nav\">\n";4988print$cgi->a({-href => href(-replay=>1, page=>$page+1),4989-accesskey =>"n", -title =>"Alt-n"},"next");4990print"</div>\n";4991}4992 git_footer_html();4993}49944995sub git_commit {4996$hash||=$hash_base||"HEAD";4997my%co= parse_commit($hash)4998or die_error(404,"Unknown commit object");4999my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5000my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});50015002my$parent=$co{'parent'};5003my$parents=$co{'parents'};# listref50045005# we need to prepare $formats_nav before any parameter munging5006my$formats_nav;5007if(!defined$parent) {5008# --root commitdiff5009$formats_nav.='(initial)';5010}elsif(@$parents==1) {5011# single parent commit5012$formats_nav.=5013'(parent: '.5014$cgi->a({-href => href(action=>"commit",5015 hash=>$parent)},5016 esc_html(substr($parent,0,7))) .5017')';5018}else{5019# merge commit5020$formats_nav.=5021'(merge: '.5022join(' ',map{5023$cgi->a({-href => href(action=>"commit",5024 hash=>$_)},5025 esc_html(substr($_,0,7)));5026}@$parents) .5027')';5028}50295030if(!defined$parent) {5031$parent="--root";5032}5033my@difftree;5034open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5035@diff_opts,5036(@$parents<=1?$parent:'-c'),5037$hash,"--"5038or die_error(500,"Open git-diff-tree failed");5039@difftree=map{chomp;$_} <$fd>;5040close$fdor die_error(404,"Reading git-diff-tree failed");50415042# non-textual hash id's can be cached5043my$expires;5044if($hash=~m/^[0-9a-fA-F]{40}$/) {5045$expires="+1d";5046}5047my$refs= git_get_references();5048my$ref= format_ref_marker($refs,$co{'id'});50495050 git_header_html(undef,$expires);5051 git_print_page_nav('commit','',5052$hash,$co{'tree'},$hash,5053$formats_nav);50545055if(defined$co{'parent'}) {5056 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5057}else{5058 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5059}5060print"<div class=\"title_text\">\n".5061"<table class=\"object_header\">\n";5062print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5063"<tr>".5064"<td></td><td>$ad{'rfc2822'}";5065if($ad{'hour_local'} <6) {5066printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5067$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5068}else{5069printf(" (%02d:%02d%s)",5070$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5071}5072print"</td>".5073"</tr>\n";5074print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5075print"<tr><td></td><td>$cd{'rfc2822'}".5076sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5077"</td></tr>\n";5078print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5079print"<tr>".5080"<td>tree</td>".5081"<td class=\"sha1\">".5082$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5083class=>"list"},$co{'tree'}) .5084"</td>".5085"<td class=\"link\">".5086$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5087"tree");5088my$snapshot_links= format_snapshot_links($hash);5089if(defined$snapshot_links) {5090print" | ".$snapshot_links;5091}5092print"</td>".5093"</tr>\n";50945095foreachmy$par(@$parents) {5096print"<tr>".5097"<td>parent</td>".5098"<td class=\"sha1\">".5099$cgi->a({-href => href(action=>"commit", hash=>$par),5100class=>"list"},$par) .5101"</td>".5102"<td class=\"link\">".5103$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5104" | ".5105$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5106"</td>".5107"</tr>\n";5108}5109print"</table>".5110"</div>\n";51115112print"<div class=\"page_body\">\n";5113 git_print_log($co{'comment'});5114print"</div>\n";51155116 git_difftree_body(\@difftree,$hash,@$parents);51175118 git_footer_html();5119}51205121sub git_object {5122# object is defined by:5123# - hash or hash_base alone5124# - hash_base and file_name5125my$type;51265127# - hash or hash_base alone5128if($hash|| ($hash_base&& !defined$file_name)) {5129my$object_id=$hash||$hash_base;51305131open my$fd,"-|", quote_command(5132 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5133or die_error(404,"Object does not exist");5134$type= <$fd>;5135chomp$type;5136close$fd5137or die_error(404,"Object does not exist");51385139# - hash_base and file_name5140}elsif($hash_base&&defined$file_name) {5141$file_name=~ s,/+$,,;51425143system(git_cmd(),"cat-file",'-e',$hash_base) ==05144or die_error(404,"Base object does not exist");51455146# here errors should not hapen5147open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5148or die_error(500,"Open git-ls-tree failed");5149my$line= <$fd>;5150close$fd;51515152#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5153unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5154 die_error(404,"File or directory for given base does not exist");5155}5156$type=$2;5157$hash=$3;5158}else{5159 die_error(400,"Not enough information to find object");5160}51615162print$cgi->redirect(-uri => href(action=>$type, -full=>1,5163 hash=>$hash, hash_base=>$hash_base,5164 file_name=>$file_name),5165-status =>'302 Found');5166}51675168sub git_blobdiff {5169my$format=shift||'html';51705171my$fd;5172my@difftree;5173my%diffinfo;5174my$expires;51755176# preparing $fd and %diffinfo for git_patchset_body5177# new style URI5178if(defined$hash_base&&defined$hash_parent_base) {5179if(defined$file_name) {5180# read raw output5181open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5182$hash_parent_base,$hash_base,5183"--", (defined$file_parent?$file_parent: ()),$file_name5184or die_error(500,"Open git-diff-tree failed");5185@difftree=map{chomp;$_} <$fd>;5186close$fd5187or die_error(404,"Reading git-diff-tree failed");5188@difftree5189or die_error(404,"Blob diff not found");51905191}elsif(defined$hash&&5192$hash=~/[0-9a-fA-F]{40}/) {5193# try to find filename from $hash51945195# read filtered raw output5196open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5197$hash_parent_base,$hash_base,"--"5198or die_error(500,"Open git-diff-tree failed");5199@difftree=5200# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5201# $hash == to_id5202grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5203map{chomp;$_} <$fd>;5204close$fd5205or die_error(404,"Reading git-diff-tree failed");5206@difftree5207or die_error(404,"Blob diff not found");52085209}else{5210 die_error(400,"Missing one of the blob diff parameters");5211}52125213if(@difftree>1) {5214 die_error(400,"Ambiguous blob diff specification");5215}52165217%diffinfo= parse_difftree_raw_line($difftree[0]);5218$file_parent||=$diffinfo{'from_file'} ||$file_name;5219$file_name||=$diffinfo{'to_file'};52205221$hash_parent||=$diffinfo{'from_id'};5222$hash||=$diffinfo{'to_id'};52235224# non-textual hash id's can be cached5225if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5226$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5227$expires='+1d';5228}52295230# open patch output5231open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5232'-p', ($formateq'html'?"--full-index": ()),5233$hash_parent_base,$hash_base,5234"--", (defined$file_parent?$file_parent: ()),$file_name5235or die_error(500,"Open git-diff-tree failed");5236}52375238# old/legacy style URI5239if(!%diffinfo&&# if new style URI failed5240defined$hash&&defined$hash_parent) {5241# fake git-diff-tree raw output5242$diffinfo{'from_mode'} =$diffinfo{'to_mode'} ="blob";5243$diffinfo{'from_id'} =$hash_parent;5244$diffinfo{'to_id'} =$hash;5245if(defined$file_name) {5246if(defined$file_parent) {5247$diffinfo{'status'} ='2';5248$diffinfo{'from_file'} =$file_parent;5249$diffinfo{'to_file'} =$file_name;5250}else{# assume not renamed5251$diffinfo{'status'} ='1';5252$diffinfo{'from_file'} =$file_name;5253$diffinfo{'to_file'} =$file_name;5254}5255}else{# no filename given5256$diffinfo{'status'} ='2';5257$diffinfo{'from_file'} =$hash_parent;5258$diffinfo{'to_file'} =$hash;5259}52605261# non-textual hash id's can be cached5262if($hash=~m/^[0-9a-fA-F]{40}$/&&5263$hash_parent=~m/^[0-9a-fA-F]{40}$/) {5264$expires='+1d';5265}52665267# open patch output5268open$fd,"-|", git_cmd(),"diff",@diff_opts,5269'-p', ($formateq'html'?"--full-index": ()),5270$hash_parent,$hash,"--"5271or die_error(500,"Open git-diff failed");5272}else{5273 die_error(400,"Missing one of the blob diff parameters")5274unless%diffinfo;5275}52765277# header5278if($formateq'html') {5279my$formats_nav=5280$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5281"raw");5282 git_header_html(undef,$expires);5283if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5284 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5285 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5286}else{5287print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5288print"<div class=\"title\">$hashvs$hash_parent</div>\n";5289}5290if(defined$file_name) {5291 git_print_page_path($file_name,"blob",$hash_base);5292}else{5293print"<div class=\"page_path\"></div>\n";5294}52955296}elsif($formateq'plain') {5297print$cgi->header(5298-type =>'text/plain',5299-charset =>'utf-8',5300-expires =>$expires,5301-content_disposition =>'inline; filename="'."$file_name".'.patch"');53025303print"X-Git-Url: ".$cgi->self_url() ."\n\n";53045305}else{5306 die_error(400,"Unknown blobdiff format");5307}53085309# patch5310if($formateq'html') {5311print"<div class=\"page_body\">\n";53125313 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5314close$fd;53155316print"</div>\n";# class="page_body"5317 git_footer_html();53185319}else{5320while(my$line= <$fd>) {5321$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5322$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;53235324print$line;53255326last if$line=~m!^\+\+\+!;5327}5328local$/=undef;5329print<$fd>;5330close$fd;5331}5332}53335334sub git_blobdiff_plain {5335 git_blobdiff('plain');5336}53375338sub git_commitdiff {5339my$format=shift||'html';5340$hash||=$hash_base||"HEAD";5341my%co= parse_commit($hash)5342or die_error(404,"Unknown commit object");53435344# choose format for commitdiff for merge5345if(!defined$hash_parent&& @{$co{'parents'}} >1) {5346$hash_parent='--cc';5347}5348# we need to prepare $formats_nav before almost any parameter munging5349my$formats_nav;5350if($formateq'html') {5351$formats_nav=5352$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5353"raw");53545355if(defined$hash_parent&&5356$hash_parentne'-c'&&$hash_parentne'--cc') {5357# commitdiff with two commits given5358my$hash_parent_short=$hash_parent;5359if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5360$hash_parent_short=substr($hash_parent,0,7);5361}5362$formats_nav.=5363' (from';5364for(my$i=0;$i< @{$co{'parents'}};$i++) {5365if($co{'parents'}[$i]eq$hash_parent) {5366$formats_nav.=' parent '. ($i+1);5367last;5368}5369}5370$formats_nav.=': '.5371$cgi->a({-href => href(action=>"commitdiff",5372 hash=>$hash_parent)},5373 esc_html($hash_parent_short)) .5374')';5375}elsif(!$co{'parent'}) {5376# --root commitdiff5377$formats_nav.=' (initial)';5378}elsif(scalar@{$co{'parents'}} ==1) {5379# single parent commit5380$formats_nav.=5381' (parent: '.5382$cgi->a({-href => href(action=>"commitdiff",5383 hash=>$co{'parent'})},5384 esc_html(substr($co{'parent'},0,7))) .5385')';5386}else{5387# merge commit5388if($hash_parenteq'--cc') {5389$formats_nav.=' | '.5390$cgi->a({-href => href(action=>"commitdiff",5391 hash=>$hash, hash_parent=>'-c')},5392'combined');5393}else{# $hash_parent eq '-c'5394$formats_nav.=' | '.5395$cgi->a({-href => href(action=>"commitdiff",5396 hash=>$hash, hash_parent=>'--cc')},5397'compact');5398}5399$formats_nav.=5400' (merge: '.5401join(' ',map{5402$cgi->a({-href => href(action=>"commitdiff",5403 hash=>$_)},5404 esc_html(substr($_,0,7)));5405} @{$co{'parents'}} ) .5406')';5407}5408}54095410my$hash_parent_param=$hash_parent;5411if(!defined$hash_parent_param) {5412# --cc for multiple parents, --root for parentless5413$hash_parent_param=5414@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5415}54165417# read commitdiff5418my$fd;5419my@difftree;5420if($formateq'html') {5421open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5422"--no-commit-id","--patch-with-raw","--full-index",5423$hash_parent_param,$hash,"--"5424or die_error(500,"Open git-diff-tree failed");54255426while(my$line= <$fd>) {5427chomp$line;5428# empty line ends raw part of diff-tree output5429last unless$line;5430push@difftree,scalar parse_difftree_raw_line($line);5431}54325433}elsif($formateq'plain') {5434open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5435'-p',$hash_parent_param,$hash,"--"5436or die_error(500,"Open git-diff-tree failed");54375438}else{5439 die_error(400,"Unknown commitdiff format");5440}54415442# non-textual hash id's can be cached5443my$expires;5444if($hash=~m/^[0-9a-fA-F]{40}$/) {5445$expires="+1d";5446}54475448# write commit message5449if($formateq'html') {5450my$refs= git_get_references();5451my$ref= format_ref_marker($refs,$co{'id'});54525453 git_header_html(undef,$expires);5454 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5455 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5456 git_print_authorship(\%co);5457print"<div class=\"page_body\">\n";5458if(@{$co{'comment'}} >1) {5459print"<div class=\"log\">\n";5460 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5461print"</div>\n";# class="log"5462}54635464}elsif($formateq'plain') {5465my$refs= git_get_references("tags");5466my$tagname= git_get_rev_name_tags($hash);5467my$filename= basename($project) ."-$hash.patch";54685469print$cgi->header(5470-type =>'text/plain',5471-charset =>'utf-8',5472-expires =>$expires,5473-content_disposition =>'inline; filename="'."$filename".'"');5474my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5475print"From: ". to_utf8($co{'author'}) ."\n";5476print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5477print"Subject: ". to_utf8($co{'title'}) ."\n";54785479print"X-Git-Tag:$tagname\n"if$tagname;5480print"X-Git-Url: ".$cgi->self_url() ."\n\n";54815482foreachmy$line(@{$co{'comment'}}) {5483print to_utf8($line) ."\n";5484}5485print"---\n\n";5486}54875488# write patch5489if($formateq'html') {5490my$use_parents= !defined$hash_parent||5491$hash_parenteq'-c'||$hash_parenteq'--cc';5492 git_difftree_body(\@difftree,$hash,5493$use_parents? @{$co{'parents'}} :$hash_parent);5494print"<br/>\n";54955496 git_patchset_body($fd, \@difftree,$hash,5497$use_parents? @{$co{'parents'}} :$hash_parent);5498close$fd;5499print"</div>\n";# class="page_body"5500 git_footer_html();55015502}elsif($formateq'plain') {5503local$/=undef;5504print<$fd>;5505close$fd5506or print"Reading git-diff-tree failed\n";5507}5508}55095510sub git_commitdiff_plain {5511 git_commitdiff('plain');5512}55135514sub git_history {5515if(!defined$hash_base) {5516$hash_base= git_get_head_hash($project);5517}5518if(!defined$page) {5519$page=0;5520}5521my$ftype;5522my%co= parse_commit($hash_base)5523or die_error(404,"Unknown commit object");55245525my$refs= git_get_references();5526my$limit=sprintf("--max-count=%i", (100* ($page+1)));55275528my@commitlist= parse_commits($hash_base,101, (100*$page),5529$file_name,"--full-history")5530or die_error(404,"No such file or directory on given branch");55315532if(!defined$hash&&defined$file_name) {5533# some commits could have deleted file in question,5534# and not have it in tree, but one of them has to have it5535for(my$i=0;$i<=@commitlist;$i++) {5536$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5537last ifdefined$hash;5538}5539}5540if(defined$hash) {5541$ftype= git_get_type($hash);5542}5543if(!defined$ftype) {5544 die_error(500,"Unknown type of object");5545}55465547my$paging_nav='';5548if($page>0) {5549$paging_nav.=5550$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5551 file_name=>$file_name)},5552"first");5553$paging_nav.=" ⋅ ".5554$cgi->a({-href => href(-replay=>1, page=>$page-1),5555-accesskey =>"p", -title =>"Alt-p"},"prev");5556}else{5557$paging_nav.="first";5558$paging_nav.=" ⋅ prev";5559}5560my$next_link='';5561if($#commitlist>=100) {5562$next_link=5563$cgi->a({-href => href(-replay=>1, page=>$page+1),5564-accesskey =>"n", -title =>"Alt-n"},"next");5565$paging_nav.=" ⋅$next_link";5566}else{5567$paging_nav.=" ⋅ next";5568}55695570 git_header_html();5571 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5572 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5573 git_print_page_path($file_name,$ftype,$hash_base);55745575 git_history_body(\@commitlist,0,99,5576$refs,$hash_base,$ftype,$next_link);55775578 git_footer_html();5579}55805581sub git_search {5582 gitweb_check_feature('search')or die_error(403,"Search is disabled");5583if(!defined$searchtext) {5584 die_error(400,"Text field is empty");5585}5586if(!defined$hash) {5587$hash= git_get_head_hash($project);5588}5589my%co= parse_commit($hash);5590if(!%co) {5591 die_error(404,"Unknown commit object");5592}5593if(!defined$page) {5594$page=0;5595}55965597$searchtype||='commit';5598if($searchtypeeq'pickaxe') {5599# pickaxe may take all resources of your box and run for several minutes5600# with every query - so decide by yourself how public you make this feature5601 gitweb_check_feature('pickaxe')5602or die_error(403,"Pickaxe is disabled");5603}5604if($searchtypeeq'grep') {5605 gitweb_check_feature('grep')5606or die_error(403,"Grep is disabled");5607}56085609 git_header_html();56105611if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5612my$greptype;5613if($searchtypeeq'commit') {5614$greptype="--grep=";5615}elsif($searchtypeeq'author') {5616$greptype="--author=";5617}elsif($searchtypeeq'committer') {5618$greptype="--committer=";5619}5620$greptype.=$searchtext;5621my@commitlist= parse_commits($hash,101, (100*$page),undef,5622$greptype,'--regexp-ignore-case',5623$search_use_regexp?'--extended-regexp':'--fixed-strings');56245625my$paging_nav='';5626if($page>0) {5627$paging_nav.=5628$cgi->a({-href => href(action=>"search", hash=>$hash,5629 searchtext=>$searchtext,5630 searchtype=>$searchtype)},5631"first");5632$paging_nav.=" ⋅ ".5633$cgi->a({-href => href(-replay=>1, page=>$page-1),5634-accesskey =>"p", -title =>"Alt-p"},"prev");5635}else{5636$paging_nav.="first";5637$paging_nav.=" ⋅ prev";5638}5639my$next_link='';5640if($#commitlist>=100) {5641$next_link=5642$cgi->a({-href => href(-replay=>1, page=>$page+1),5643-accesskey =>"n", -title =>"Alt-n"},"next");5644$paging_nav.=" ⋅$next_link";5645}else{5646$paging_nav.=" ⋅ next";5647}56485649if($#commitlist>=100) {5650}56515652 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5653 git_print_header_div('commit', esc_html($co{'title'}),$hash);5654 git_search_grep_body(\@commitlist,0,99,$next_link);5655}56565657if($searchtypeeq'pickaxe') {5658 git_print_page_nav('','',$hash,$co{'tree'},$hash);5659 git_print_header_div('commit', esc_html($co{'title'}),$hash);56605661print"<table class=\"pickaxe search\">\n";5662my$alternate=1;5663$/="\n";5664open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5665'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5666($search_use_regexp?'--pickaxe-regex': ());5667undef%co;5668my@files;5669while(my$line= <$fd>) {5670chomp$line;5671next unless$line;56725673my%set= parse_difftree_raw_line($line);5674if(defined$set{'commit'}) {5675# finish previous commit5676if(%co) {5677print"</td>\n".5678"<td class=\"link\">".5679$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5680" | ".5681$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5682print"</td>\n".5683"</tr>\n";5684}56855686if($alternate) {5687print"<tr class=\"dark\">\n";5688}else{5689print"<tr class=\"light\">\n";5690}5691$alternate^=1;5692%co= parse_commit($set{'commit'});5693my$author= chop_and_escape_str($co{'author_name'},15,5);5694print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5695"<td><i>$author</i></td>\n".5696"<td>".5697$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5698-class=>"list subject"},5699 chop_and_escape_str($co{'title'},50) ."<br/>");5700}elsif(defined$set{'to_id'}) {5701next if($set{'to_id'} =~m/^0{40}$/);57025703print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5704 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5705-class=>"list"},5706"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5707"<br/>\n";5708}5709}5710close$fd;57115712# finish last commit (warning: repetition!)5713if(%co) {5714print"</td>\n".5715"<td class=\"link\">".5716$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5717" | ".5718$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5719print"</td>\n".5720"</tr>\n";5721}57225723print"</table>\n";5724}57255726if($searchtypeeq'grep') {5727 git_print_page_nav('','',$hash,$co{'tree'},$hash);5728 git_print_header_div('commit', esc_html($co{'title'}),$hash);57295730print"<table class=\"grep_search\">\n";5731my$alternate=1;5732my$matches=0;5733$/="\n";5734open my$fd,"-|", git_cmd(),'grep','-n',5735$search_use_regexp? ('-E','-i') :'-F',5736$searchtext,$co{'tree'};5737my$lastfile='';5738while(my$line= <$fd>) {5739chomp$line;5740my($file,$lno,$ltext,$binary);5741last if($matches++>1000);5742if($line=~/^Binary file (.+) matches$/) {5743$file=$1;5744$binary=1;5745}else{5746(undef,$file,$lno,$ltext) =split(/:/,$line,4);5747}5748if($filene$lastfile) {5749$lastfileand print"</td></tr>\n";5750if($alternate++) {5751print"<tr class=\"dark\">\n";5752}else{5753print"<tr class=\"light\">\n";5754}5755print"<td class=\"list\">".5756$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5757 file_name=>"$file"),5758-class=>"list"}, esc_path($file));5759print"</td><td>\n";5760$lastfile=$file;5761}5762if($binary) {5763print"<div class=\"binary\">Binary file</div>\n";5764}else{5765$ltext= untabify($ltext);5766if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5767$ltext= esc_html($1, -nbsp=>1);5768$ltext.='<span class="match">';5769$ltext.= esc_html($2, -nbsp=>1);5770$ltext.='</span>';5771$ltext.= esc_html($3, -nbsp=>1);5772}else{5773$ltext= esc_html($ltext, -nbsp=>1);5774}5775print"<div class=\"pre\">".5776$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5777 file_name=>"$file").'#l'.$lno,5778-class=>"linenr"},sprintf('%4i',$lno))5779.' '.$ltext."</div>\n";5780}5781}5782if($lastfile) {5783print"</td></tr>\n";5784if($matches>1000) {5785print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5786}5787}else{5788print"<div class=\"diff nodifferences\">No matches found</div>\n";5789}5790close$fd;57915792print"</table>\n";5793}5794 git_footer_html();5795}57965797sub git_search_help {5798 git_header_html();5799 git_print_page_nav('','',$hash,$hash,$hash);5800print<<EOT;5801<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5802regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5803the pattern entered is recognized as the POSIX extended5804<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5805insensitive).</p>5806<dl>5807<dt><b>commit</b></dt>5808<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5809EOT5810my($have_grep) = gitweb_check_feature('grep');5811if($have_grep) {5812print<<EOT;5813<dt><b>grep</b></dt>5814<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5815 a different one) are searched for the given pattern. On large trees, this search can take5816a while and put some strain on the server, so please use it with some consideration. Note that5817due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5818case-sensitive.</dd>5819EOT5820}5821print<<EOT;5822<dt><b>author</b></dt>5823<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5824<dt><b>committer</b></dt>5825<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5826EOT5827my($have_pickaxe) = gitweb_check_feature('pickaxe');5828if($have_pickaxe) {5829print<<EOT;5830<dt><b>pickaxe</b></dt>5831<dd>All commits that caused the string to appear or disappear from any file (changes that5832added, removed or "modified" the string) will be listed. This search can take a while and5833takes a lot of strain on the server, so please use it wisely. Note that since you may be5834interested even in changes just changing the case as well, this search is case sensitive.</dd>5835EOT5836}5837print"</dl>\n";5838 git_footer_html();5839}58405841sub git_shortlog {5842my$head= git_get_head_hash($project);5843if(!defined$hash) {5844$hash=$head;5845}5846if(!defined$page) {5847$page=0;5848}5849my$refs= git_get_references();58505851my$commit_hash=$hash;5852if(defined$hash_parent) {5853$commit_hash="$hash_parent..$hash";5854}5855my@commitlist= parse_commits($commit_hash,101, (100*$page));58565857my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5858my$next_link='';5859if($#commitlist>=100) {5860$next_link=5861$cgi->a({-href => href(-replay=>1, page=>$page+1),5862-accesskey =>"n", -title =>"Alt-n"},"next");5863}58645865 git_header_html();5866 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);5867 git_print_header_div('summary',$project);58685869 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);58705871 git_footer_html();5872}58735874## ......................................................................5875## feeds (RSS, Atom; OPML)58765877sub git_feed {5878my$format=shift||'atom';5879my($have_blame) = gitweb_check_feature('blame');58805881# Atom: http://www.atomenabled.org/developers/syndication/5882# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ5883if($formatne'rss'&&$formatne'atom') {5884 die_error(400,"Unknown web feed format");5885}58865887# log/feed of current (HEAD) branch, log of given branch, history of file/directory5888my$head=$hash||'HEAD';5889my@commitlist= parse_commits($head,150,0,$file_name);58905891my%latest_commit;5892my%latest_date;5893my$content_type="application/$format+xml";5894if(defined$cgi->http('HTTP_ACCEPT') &&5895$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {5896# browser (feed reader) prefers text/xml5897$content_type='text/xml';5898}5899if(defined($commitlist[0])) {5900%latest_commit= %{$commitlist[0]};5901%latest_date= parse_date($latest_commit{'author_epoch'});5902print$cgi->header(5903-type =>$content_type,5904-charset =>'utf-8',5905-last_modified =>$latest_date{'rfc2822'});5906}else{5907print$cgi->header(5908-type =>$content_type,5909-charset =>'utf-8');5910}59115912# Optimization: skip generating the body if client asks only5913# for Last-Modified date.5914return if($cgi->request_method()eq'HEAD');59155916# header variables5917my$title="$site_name-$project/$action";5918my$feed_type='log';5919if(defined$hash) {5920$title.=" - '$hash'";5921$feed_type='branch log';5922if(defined$file_name) {5923$title.=" ::$file_name";5924$feed_type='history';5925}5926}elsif(defined$file_name) {5927$title.=" -$file_name";5928$feed_type='history';5929}5930$title.="$feed_type";5931my$descr= git_get_project_description($project);5932if(defined$descr) {5933$descr= esc_html($descr);5934}else{5935$descr="$project".5936($formateq'rss'?'RSS':'Atom') .5937" feed";5938}5939my$owner= git_get_project_owner($project);5940$owner= esc_html($owner);59415942#header5943my$alt_url;5944if(defined$file_name) {5945$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);5946}elsif(defined$hash) {5947$alt_url= href(-full=>1, action=>"log", hash=>$hash);5948}else{5949$alt_url= href(-full=>1, action=>"summary");5950}5951print qq!<?xml version="1.0" encoding="utf-8"?>\n!;5952if($formateq'rss') {5953print<<XML;5954<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">5955<channel>5956XML5957print"<title>$title</title>\n".5958"<link>$alt_url</link>\n".5959"<description>$descr</description>\n".5960"<language>en</language>\n";5961}elsif($formateq'atom') {5962print<<XML;5963<feed xmlns="http://www.w3.org/2005/Atom">5964XML5965print"<title>$title</title>\n".5966"<subtitle>$descr</subtitle>\n".5967'<link rel="alternate" type="text/html" href="'.5968$alt_url.'" />'."\n".5969'<link rel="self" type="'.$content_type.'" href="'.5970$cgi->self_url() .'" />'."\n".5971"<id>". href(-full=>1) ."</id>\n".5972# use project owner for feed author5973"<author><name>$owner</name></author>\n";5974if(defined$favicon) {5975print"<icon>". esc_url($favicon) ."</icon>\n";5976}5977if(defined$logo_url) {5978# not twice as wide as tall: 72 x 27 pixels5979print"<logo>". esc_url($logo) ."</logo>\n";5980}5981if(!%latest_date) {5982# dummy date to keep the feed valid until commits trickle in:5983print"<updated>1970-01-01T00:00:00Z</updated>\n";5984}else{5985print"<updated>$latest_date{'iso-8601'}</updated>\n";5986}5987}59885989# contents5990for(my$i=0;$i<=$#commitlist;$i++) {5991my%co= %{$commitlist[$i]};5992my$commit=$co{'id'};5993# we read 150, we always show 30 and the ones more recent than 48 hours5994if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {5995last;5996}5997my%cd= parse_date($co{'author_epoch'});59985999# get list of changed files6000open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6001$co{'parent'} ||"--root",6002$co{'id'},"--", (defined$file_name?$file_name: ())6003ornext;6004my@difftree=map{chomp;$_} <$fd>;6005close$fd6006ornext;60076008# print element (entry, item)6009my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6010if($formateq'rss') {6011print"<item>\n".6012"<title>". esc_html($co{'title'}) ."</title>\n".6013"<author>". esc_html($co{'author'}) ."</author>\n".6014"<pubDate>$cd{'rfc2822'}</pubDate>\n".6015"<guid isPermaLink=\"true\">$co_url</guid>\n".6016"<link>$co_url</link>\n".6017"<description>". esc_html($co{'title'}) ."</description>\n".6018"<content:encoded>".6019"<![CDATA[\n";6020}elsif($formateq'atom') {6021print"<entry>\n".6022"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6023"<updated>$cd{'iso-8601'}</updated>\n".6024"<author>\n".6025" <name>". esc_html($co{'author_name'}) ."</name>\n";6026if($co{'author_email'}) {6027print" <email>". esc_html($co{'author_email'}) ."</email>\n";6028}6029print"</author>\n".6030# use committer for contributor6031"<contributor>\n".6032" <name>". esc_html($co{'committer_name'}) ."</name>\n";6033if($co{'committer_email'}) {6034print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6035}6036print"</contributor>\n".6037"<published>$cd{'iso-8601'}</published>\n".6038"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6039"<id>$co_url</id>\n".6040"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6041"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6042}6043my$comment=$co{'comment'};6044print"<pre>\n";6045foreachmy$line(@$comment) {6046$line= esc_html($line);6047print"$line\n";6048}6049print"</pre><ul>\n";6050foreachmy$difftree_line(@difftree) {6051my%difftree= parse_difftree_raw_line($difftree_line);6052next if!$difftree{'from_id'};60536054my$file=$difftree{'file'} ||$difftree{'to_file'};60556056print"<li>".6057"[".6058$cgi->a({-href => href(-full=>1, action=>"blobdiff",6059 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6060 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6061 file_name=>$file, file_parent=>$difftree{'from_file'}),6062-title =>"diff"},'D');6063if($have_blame) {6064print$cgi->a({-href => href(-full=>1, action=>"blame",6065 file_name=>$file, hash_base=>$commit),6066-title =>"blame"},'B');6067}6068# if this is not a feed of a file history6069if(!defined$file_name||$file_namene$file) {6070print$cgi->a({-href => href(-full=>1, action=>"history",6071 file_name=>$file, hash=>$commit),6072-title =>"history"},'H');6073}6074$file= esc_path($file);6075print"] ".6076"$file</li>\n";6077}6078if($formateq'rss') {6079print"</ul>]]>\n".6080"</content:encoded>\n".6081"</item>\n";6082}elsif($formateq'atom') {6083print"</ul>\n</div>\n".6084"</content>\n".6085"</entry>\n";6086}6087}60886089# end of feed6090if($formateq'rss') {6091print"</channel>\n</rss>\n";6092}elsif($formateq'atom') {6093print"</feed>\n";6094}6095}60966097sub git_rss {6098 git_feed('rss');6099}61006101sub git_atom {6102 git_feed('atom');6103}61046105sub git_opml {6106my@list= git_get_projects_list();61076108print$cgi->header(-type =>'text/xml', -charset =>'utf-8');6109print<<XML;6110<?xml version="1.0" encoding="utf-8"?>6111<opml version="1.0">6112<head>6113 <title>$site_nameOPML Export</title>6114</head>6115<body>6116<outline text="git RSS feeds">6117XML61186119foreachmy$pr(@list) {6120my%proj=%$pr;6121my$head= git_get_head_hash($proj{'path'});6122if(!defined$head) {6123next;6124}6125$git_dir="$projectroot/$proj{'path'}";6126my%co= parse_commit($head);6127if(!%co) {6128next;6129}61306131my$path= esc_html(chop_str($proj{'path'},25,5));6132my$rss="$my_url?p=$proj{'path'};a=rss";6133my$html="$my_url?p=$proj{'path'};a=summary";6134print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6135}6136print<<XML;6137</outline>6138</body>6139</opml>6140XML6141}