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} 620evaluate_path_info(); 621 622our$action=$input_params{'action'}; 623if(defined$action) { 624if(!validate_action($action)) { 625 die_error(400,"Invalid action parameter"); 626} 627} 628 629# parameters which are pathnames 630our$project=$input_params{'project'}; 631if(defined$project) { 632if(!validate_project($project)) { 633undef$project; 634 die_error(404,"No such project"); 635} 636} 637 638our$file_name=$input_params{'file_name'}; 639if(defined$file_name) { 640if(!validate_pathname($file_name)) { 641 die_error(400,"Invalid file parameter"); 642} 643} 644 645our$file_parent=$input_params{'file_parent'}; 646if(defined$file_parent) { 647if(!validate_pathname($file_parent)) { 648 die_error(400,"Invalid file parent parameter"); 649} 650} 651 652# parameters which are refnames 653our$hash=$input_params{'hash'}; 654if(defined$hash) { 655if(!validate_refname($hash)) { 656 die_error(400,"Invalid hash parameter"); 657} 658} 659 660our$hash_parent=$input_params{'hash_parent'}; 661if(defined$hash_parent) { 662if(!validate_refname($hash_parent)) { 663 die_error(400,"Invalid hash parent parameter"); 664} 665} 666 667our$hash_base=$input_params{'hash_base'}; 668if(defined$hash_base) { 669if(!validate_refname($hash_base)) { 670 die_error(400,"Invalid hash base parameter"); 671} 672} 673 674our@extra_options= @{$input_params{'extra_options'}}; 675# @extra_options is always defined, since it can only be (currently) set from 676# CGI, and $cgi->param() returns the empty array in array context if the param 677# is not set 678foreachmy$opt(@extra_options) { 679if(not exists$allowed_options{$opt}) { 680 die_error(400,"Invalid option parameter"); 681} 682if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 683 die_error(400,"Invalid option parameter for this action"); 684} 685} 686 687our$hash_parent_base=$input_params{'hash_parent_base'}; 688if(defined$hash_parent_base) { 689if(!validate_refname($hash_parent_base)) { 690 die_error(400,"Invalid hash parent base parameter"); 691} 692} 693 694# other parameters 695our$page=$input_params{'page'}; 696if(defined$page) { 697if($page=~m/[^0-9]/) { 698 die_error(400,"Invalid page parameter"); 699} 700} 701 702our$searchtype=$input_params{'searchtype'}; 703if(defined$searchtype) { 704if($searchtype=~m/[^a-z]/) { 705 die_error(400,"Invalid searchtype parameter"); 706} 707} 708 709our$search_use_regexp=$input_params{'search_use_regexp'}; 710 711our$searchtext=$input_params{'searchtext'}; 712our$search_regexp; 713if(defined$searchtext) { 714if(length($searchtext) <2) { 715 die_error(403,"At least two characters are required for search parameter"); 716} 717$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 718} 719 720# path to the current git repository 721our$git_dir; 722$git_dir="$projectroot/$project"if$project; 723 724# list of supported snapshot formats 725our@snapshot_fmts= gitweb_check_feature('snapshot'); 726@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 727 728# dispatch 729if(!defined$action) { 730if(defined$hash) { 731$action= git_get_type($hash); 732}elsif(defined$hash_base&&defined$file_name) { 733$action= git_get_type("$hash_base:$file_name"); 734}elsif(defined$project) { 735$action='summary'; 736}else{ 737$action='project_list'; 738} 739} 740if(!defined($actions{$action})) { 741 die_error(400,"Unknown action"); 742} 743if($action!~m/^(opml|project_list|project_index)$/&& 744!$project) { 745 die_error(400,"Project needed"); 746} 747$actions{$action}->(); 748exit; 749 750## ====================================================================== 751## action links 752 753sub href (%) { 754my%params=@_; 755# default is to use -absolute url() i.e. $my_uri 756my$href=$params{-full} ?$my_url:$my_uri; 757 758$params{'project'} =$projectunlessexists$params{'project'}; 759 760if($params{-replay}) { 761while(my($name,$symbol) =each%cgi_param_mapping) { 762if(!exists$params{$name}) { 763$params{$name} =$input_params{$name}; 764} 765} 766} 767 768my($use_pathinfo) = gitweb_check_feature('pathinfo'); 769if($use_pathinfo) { 770# try to put as many parameters as possible in PATH_INFO: 771# - project name 772# - action 773# - hash_parent or hash_parent_base:/file_parent 774# - hash or hash_base:/filename 775 776# When the script is the root DirectoryIndex for the domain, 777# $href here would be something like http://gitweb.example.com/ 778# Thus, we strip any trailing / from $href, to spare us double 779# slashes in the final URL 780$href=~ s,/$,,; 781 782# Then add the project name, if present 783$href.="/".esc_url($params{'project'})ifdefined$params{'project'}; 784delete$params{'project'}; 785 786# Summary just uses the project path URL, any other action is 787# added to the URL 788if(defined$params{'action'}) { 789$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 790delete$params{'action'}; 791} 792 793# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 794# stripping nonexistent or useless pieces 795$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 796||$params{'hash_parent'} ||$params{'hash'}); 797if(defined$params{'hash_base'}) { 798if(defined$params{'hash_parent_base'}) { 799$href.= esc_url($params{'hash_parent_base'}); 800# skip the file_parent if it's the same as the file_name 801delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 802if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 803$href.=":/".esc_url($params{'file_parent'}); 804delete$params{'file_parent'}; 805} 806$href.=".."; 807delete$params{'hash_parent'}; 808delete$params{'hash_parent_base'}; 809}elsif(defined$params{'hash_parent'}) { 810$href.= esc_url($params{'hash_parent'}).".."; 811delete$params{'hash_parent'}; 812} 813 814$href.= esc_url($params{'hash_base'}); 815if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 816$href.=":/".esc_url($params{'file_name'}); 817delete$params{'file_name'}; 818} 819delete$params{'hash'}; 820delete$params{'hash_base'}; 821}elsif(defined$params{'hash'}) { 822$href.= esc_url($params{'hash'}); 823delete$params{'hash'}; 824} 825} 826 827# now encode the parameters explicitly 828my@result= (); 829for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 830my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 831if(defined$params{$name}) { 832if(ref($params{$name})eq"ARRAY") { 833foreachmy$par(@{$params{$name}}) { 834push@result,$symbol."=". esc_param($par); 835} 836}else{ 837push@result,$symbol."=". esc_param($params{$name}); 838} 839} 840} 841$href.="?".join(';',@result)ifscalar@result; 842 843return$href; 844} 845 846 847## ====================================================================== 848## validation, quoting/unquoting and escaping 849 850sub validate_action { 851my$input=shift||returnundef; 852returnundefunlessexists$actions{$input}; 853return$input; 854} 855 856sub validate_project { 857my$input=shift||returnundef; 858if(!validate_pathname($input) || 859!(-d "$projectroot/$input") || 860!check_head_link("$projectroot/$input") || 861($export_ok&& !(-e "$projectroot/$input/$export_ok")) || 862($strict_export&& !project_in_list($input))) { 863returnundef; 864}else{ 865return$input; 866} 867} 868 869sub validate_pathname { 870my$input=shift||returnundef; 871 872# no '.' or '..' as elements of path, i.e. no '.' nor '..' 873# at the beginning, at the end, and between slashes. 874# also this catches doubled slashes 875if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 876returnundef; 877} 878# no null characters 879if($input=~m!\0!) { 880returnundef; 881} 882return$input; 883} 884 885sub validate_refname { 886my$input=shift||returnundef; 887 888# textual hashes are O.K. 889if($input=~m/^[0-9a-fA-F]{40}$/) { 890return$input; 891} 892# it must be correct pathname 893$input= validate_pathname($input) 894orreturnundef; 895# restrictions on ref name according to git-check-ref-format 896if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { 897returnundef; 898} 899return$input; 900} 901 902# decode sequences of octets in utf8 into Perl's internal form, 903# which is utf-8 with utf8 flag set if needed. gitweb writes out 904# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning 905sub to_utf8 { 906my$str=shift; 907if(utf8::valid($str)) { 908 utf8::decode($str); 909return$str; 910}else{ 911return decode($fallback_encoding,$str, Encode::FB_DEFAULT); 912} 913} 914 915# quote unsafe chars, but keep the slash, even when it's not 916# correct, but quoted slashes look too horrible in bookmarks 917sub esc_param { 918my$str=shift; 919$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg; 920$str=~s/\+/%2B/g; 921$str=~s/ /\+/g; 922return$str; 923} 924 925# quote unsafe chars in whole URL, so some charactrs cannot be quoted 926sub esc_url { 927my$str=shift; 928$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg; 929$str=~s/\+/%2B/g; 930$str=~s/ /\+/g; 931return$str; 932} 933 934# replace invalid utf8 character with SUBSTITUTION sequence 935sub esc_html ($;%) { 936my$str=shift; 937my%opts=@_; 938 939$str= to_utf8($str); 940$str=$cgi->escapeHTML($str); 941if($opts{'-nbsp'}) { 942$str=~s/ / /g; 943} 944$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg; 945return$str; 946} 947 948# quote control characters and escape filename to HTML 949sub esc_path { 950my$str=shift; 951my%opts=@_; 952 953$str= to_utf8($str); 954$str=$cgi->escapeHTML($str); 955if($opts{'-nbsp'}) { 956$str=~s/ / /g; 957} 958$str=~ s|([[:cntrl:]])|quot_cec($1)|eg; 959return$str; 960} 961 962# Make control characters "printable", using character escape codes (CEC) 963sub quot_cec { 964my$cntrl=shift; 965my%opts=@_; 966my%es= (# character escape codes, aka escape sequences 967"\t"=>'\t',# tab (HT) 968"\n"=>'\n',# line feed (LF) 969"\r"=>'\r',# carrige return (CR) 970"\f"=>'\f',# form feed (FF) 971"\b"=>'\b',# backspace (BS) 972"\a"=>'\a',# alarm (bell) (BEL) 973"\e"=>'\e',# escape (ESC) 974"\013"=>'\v',# vertical tab (VT) 975"\000"=>'\0',# nul character (NUL) 976); 977my$chr= ( (exists$es{$cntrl}) 978?$es{$cntrl} 979:sprintf('\%2x',ord($cntrl)) ); 980if($opts{-nohtml}) { 981return$chr; 982}else{ 983return"<span class=\"cntrl\">$chr</span>"; 984} 985} 986 987# Alternatively use unicode control pictures codepoints, 988# Unicode "printable representation" (PR) 989sub quot_upr { 990my$cntrl=shift; 991my%opts=@_; 992 993my$chr=sprintf('&#%04d;',0x2400+ord($cntrl)); 994if($opts{-nohtml}) { 995return$chr; 996}else{ 997return"<span class=\"cntrl\">$chr</span>"; 998} 999}10001001# git may return quoted and escaped filenames1002sub unquote {1003my$str=shift;10041005sub unq {1006my$seq=shift;1007my%es= (# character escape codes, aka escape sequences1008't'=>"\t",# tab (HT, TAB)1009'n'=>"\n",# newline (NL)1010'r'=>"\r",# return (CR)1011'f'=>"\f",# form feed (FF)1012'b'=>"\b",# backspace (BS)1013'a'=>"\a",# alarm (bell) (BEL)1014'e'=>"\e",# escape (ESC)1015'v'=>"\013",# vertical tab (VT)1016);10171018if($seq=~m/^[0-7]{1,3}$/) {1019# octal char sequence1020returnchr(oct($seq));1021}elsif(exists$es{$seq}) {1022# C escape sequence, aka character escape code1023return$es{$seq};1024}1025# quoted ordinary character1026return$seq;1027}10281029if($str=~m/^"(.*)"$/) {1030# needs unquoting1031$str=$1;1032$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1033}1034return$str;1035}10361037# escape tabs (convert tabs to spaces)1038sub untabify {1039my$line=shift;10401041while((my$pos=index($line,"\t")) != -1) {1042if(my$count= (8- ($pos%8))) {1043my$spaces=' ' x $count;1044$line=~s/\t/$spaces/;1045}1046}10471048return$line;1049}10501051sub project_in_list {1052my$project=shift;1053my@list= git_get_projects_list();1054return@list&&scalar(grep{$_->{'path'}eq$project}@list);1055}10561057## ----------------------------------------------------------------------1058## HTML aware string manipulation10591060# Try to chop given string on a word boundary between position1061# $len and $len+$add_len. If there is no word boundary there,1062# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1063# (marking chopped part) would be longer than given string.1064sub chop_str {1065my$str=shift;1066my$len=shift;1067my$add_len=shift||10;1068my$where=shift||'right';# 'left' | 'center' | 'right'10691070# Make sure perl knows it is utf8 encoded so we don't1071# cut in the middle of a utf8 multibyte char.1072$str= to_utf8($str);10731074# allow only $len chars, but don't cut a word if it would fit in $add_len1075# if it doesn't fit, cut it if it's still longer than the dots we would add1076# remove chopped character entities entirely10771078# when chopping in the middle, distribute $len into left and right part1079# return early if chopping wouldn't make string shorter1080if($whereeq'center') {1081return$strif($len+5>=length($str));# filler is length 51082$len=int($len/2);1083}else{1084return$strif($len+4>=length($str));# filler is length 41085}10861087# regexps: ending and beginning with word part up to $add_len1088my$endre=qr/.{$len}\w{0,$add_len}/;1089my$begre=qr/\w{0,$add_len}.{$len}/;10901091if($whereeq'left') {1092$str=~m/^(.*?)($begre)$/;1093my($lead,$body) = ($1,$2);1094if(length($lead) >4) {1095$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1096$lead=" ...";1097}1098return"$lead$body";10991100}elsif($whereeq'center') {1101$str=~m/^($endre)(.*)$/;1102my($left,$str) = ($1,$2);1103$str=~m/^(.*?)($begre)$/;1104my($mid,$right) = ($1,$2);1105if(length($mid) >5) {1106$left=~s/&[^;]*$//;1107$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1108$mid=" ... ";1109}1110return"$left$mid$right";11111112}else{1113$str=~m/^($endre)(.*)$/;1114my$body=$1;1115my$tail=$2;1116if(length($tail) >4) {1117$body=~s/&[^;]*$//;1118$tail="... ";1119}1120return"$body$tail";1121}1122}11231124# takes the same arguments as chop_str, but also wraps a <span> around the1125# result with a title attribute if it does get chopped. Additionally, the1126# string is HTML-escaped.1127sub chop_and_escape_str {1128my($str) =@_;11291130my$chopped= chop_str(@_);1131if($choppedeq$str) {1132return esc_html($chopped);1133}else{1134$str=~s/([[:cntrl:]])/?/g;1135return$cgi->span({-title=>$str}, esc_html($chopped));1136}1137}11381139## ----------------------------------------------------------------------1140## functions returning short strings11411142# CSS class for given age value (in seconds)1143sub age_class {1144my$age=shift;11451146if(!defined$age) {1147return"noage";1148}elsif($age<60*60*2) {1149return"age0";1150}elsif($age<60*60*24*2) {1151return"age1";1152}else{1153return"age2";1154}1155}11561157# convert age in seconds to "nn units ago" string1158sub age_string {1159my$age=shift;1160my$age_str;11611162if($age>60*60*24*365*2) {1163$age_str= (int$age/60/60/24/365);1164$age_str.=" years ago";1165}elsif($age>60*60*24*(365/12)*2) {1166$age_str=int$age/60/60/24/(365/12);1167$age_str.=" months ago";1168}elsif($age>60*60*24*7*2) {1169$age_str=int$age/60/60/24/7;1170$age_str.=" weeks ago";1171}elsif($age>60*60*24*2) {1172$age_str=int$age/60/60/24;1173$age_str.=" days ago";1174}elsif($age>60*60*2) {1175$age_str=int$age/60/60;1176$age_str.=" hours ago";1177}elsif($age>60*2) {1178$age_str=int$age/60;1179$age_str.=" min ago";1180}elsif($age>2) {1181$age_str=int$age;1182$age_str.=" sec ago";1183}else{1184$age_str.=" right now";1185}1186return$age_str;1187}11881189useconstant{1190 S_IFINVALID =>0030000,1191 S_IFGITLINK =>0160000,1192};11931194# submodule/subproject, a commit object reference1195sub S_ISGITLINK($) {1196my$mode=shift;11971198return(($mode& S_IFMT) == S_IFGITLINK)1199}12001201# convert file mode in octal to symbolic file mode string1202sub mode_str {1203my$mode=oct shift;12041205if(S_ISGITLINK($mode)) {1206return'm---------';1207}elsif(S_ISDIR($mode& S_IFMT)) {1208return'drwxr-xr-x';1209}elsif(S_ISLNK($mode)) {1210return'lrwxrwxrwx';1211}elsif(S_ISREG($mode)) {1212# git cares only about the executable bit1213if($mode& S_IXUSR) {1214return'-rwxr-xr-x';1215}else{1216return'-rw-r--r--';1217};1218}else{1219return'----------';1220}1221}12221223# convert file mode in octal to file type string1224sub file_type {1225my$mode=shift;12261227if($mode!~m/^[0-7]+$/) {1228return$mode;1229}else{1230$mode=oct$mode;1231}12321233if(S_ISGITLINK($mode)) {1234return"submodule";1235}elsif(S_ISDIR($mode& S_IFMT)) {1236return"directory";1237}elsif(S_ISLNK($mode)) {1238return"symlink";1239}elsif(S_ISREG($mode)) {1240return"file";1241}else{1242return"unknown";1243}1244}12451246# convert file mode in octal to file type description string1247sub file_type_long {1248my$mode=shift;12491250if($mode!~m/^[0-7]+$/) {1251return$mode;1252}else{1253$mode=oct$mode;1254}12551256if(S_ISGITLINK($mode)) {1257return"submodule";1258}elsif(S_ISDIR($mode& S_IFMT)) {1259return"directory";1260}elsif(S_ISLNK($mode)) {1261return"symlink";1262}elsif(S_ISREG($mode)) {1263if($mode& S_IXUSR) {1264return"executable";1265}else{1266return"file";1267};1268}else{1269return"unknown";1270}1271}127212731274## ----------------------------------------------------------------------1275## functions returning short HTML fragments, or transforming HTML fragments1276## which don't belong to other sections12771278# format line of commit message.1279sub format_log_line_html {1280my$line=shift;12811282$line= esc_html($line, -nbsp=>1);1283if($line=~m/([0-9a-fA-F]{8,40})/) {1284my$hash_text=$1;1285my$link=1286$cgi->a({-href => href(action=>"object", hash=>$hash_text),1287-class=>"text"},$hash_text);1288$line=~s/$hash_text/$link/;1289}1290return$line;1291}12921293# format marker of refs pointing to given object12941295# the destination action is chosen based on object type and current context:1296# - for annotated tags, we choose the tag view unless it's the current view1297# already, in which case we go to shortlog view1298# - for other refs, we keep the current view if we're in history, shortlog or1299# log view, and select shortlog otherwise1300sub format_ref_marker {1301my($refs,$id) =@_;1302my$markers='';13031304if(defined$refs->{$id}) {1305foreachmy$ref(@{$refs->{$id}}) {1306# this code exploits the fact that non-lightweight tags are the1307# only indirect objects, and that they are the only objects for which1308# we want to use tag instead of shortlog as action1309my($type,$name) =qw();1310my$indirect= ($ref=~s/\^\{\}$//);1311# e.g. tags/v2.6.11 or heads/next1312if($ref=~m!^(.*?)s?/(.*)$!) {1313$type=$1;1314$name=$2;1315}else{1316$type="ref";1317$name=$ref;1318}13191320my$class=$type;1321$class.=" indirect"if$indirect;13221323my$dest_action="shortlog";13241325if($indirect) {1326$dest_action="tag"unless$actioneq"tag";1327}elsif($action=~/^(history|(short)?log)$/) {1328$dest_action=$action;1329}13301331my$dest="";1332$dest.="refs/"unless$ref=~ m!^refs/!;1333$dest.=$ref;13341335my$link=$cgi->a({1336-href => href(1337 action=>$dest_action,1338 hash=>$dest1339)},$name);13401341$markers.=" <span class=\"$class\"title=\"$ref\">".1342$link."</span>";1343}1344}13451346if($markers) {1347return' <span class="refs">'.$markers.'</span>';1348}else{1349return"";1350}1351}13521353# format, perhaps shortened and with markers, title line1354sub format_subject_html {1355my($long,$short,$href,$extra) =@_;1356$extra=''unlessdefined($extra);13571358if(length($short) <length($long)) {1359return$cgi->a({-href =>$href, -class=>"list subject",1360-title => to_utf8($long)},1361 esc_html($short) .$extra);1362}else{1363return$cgi->a({-href =>$href, -class=>"list subject"},1364 esc_html($long) .$extra);1365}1366}13671368# format git diff header line, i.e. "diff --(git|combined|cc) ..."1369sub format_git_diff_header_line {1370my$line=shift;1371my$diffinfo=shift;1372my($from,$to) =@_;13731374if($diffinfo->{'nparents'}) {1375# combined diff1376$line=~s!^(diff (.*?) )"?.*$!$1!;1377if($to->{'href'}) {1378$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1379 esc_path($to->{'file'}));1380}else{# file was deleted (no href)1381$line.= esc_path($to->{'file'});1382}1383}else{1384# "ordinary" diff1385$line=~s!^(diff (.*?) )"?a/.*$!$1!;1386if($from->{'href'}) {1387$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1388'a/'. esc_path($from->{'file'}));1389}else{# file was added (no href)1390$line.='a/'. esc_path($from->{'file'});1391}1392$line.=' ';1393if($to->{'href'}) {1394$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1395'b/'. esc_path($to->{'file'}));1396}else{# file was deleted1397$line.='b/'. esc_path($to->{'file'});1398}1399}14001401return"<div class=\"diff header\">$line</div>\n";1402}14031404# format extended diff header line, before patch itself1405sub format_extended_diff_header_line {1406my$line=shift;1407my$diffinfo=shift;1408my($from,$to) =@_;14091410# match <path>1411if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1412$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1413 esc_path($from->{'file'}));1414}1415if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1416$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1417 esc_path($to->{'file'}));1418}1419# match single <mode>1420if($line=~m/\s(\d{6})$/) {1421$line.='<span class="info"> ('.1422 file_type_long($1) .1423')</span>';1424}1425# match <hash>1426if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1427# can match only for combined diff1428$line='index ';1429for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1430if($from->{'href'}[$i]) {1431$line.=$cgi->a({-href=>$from->{'href'}[$i],1432-class=>"hash"},1433substr($diffinfo->{'from_id'}[$i],0,7));1434}else{1435$line.='0' x 7;1436}1437# separator1438$line.=','if($i<$diffinfo->{'nparents'} -1);1439}1440$line.='..';1441if($to->{'href'}) {1442$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1443substr($diffinfo->{'to_id'},0,7));1444}else{1445$line.='0' x 7;1446}14471448}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1449# can match only for ordinary diff1450my($from_link,$to_link);1451if($from->{'href'}) {1452$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1453substr($diffinfo->{'from_id'},0,7));1454}else{1455$from_link='0' x 7;1456}1457if($to->{'href'}) {1458$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1459substr($diffinfo->{'to_id'},0,7));1460}else{1461$to_link='0' x 7;1462}1463my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1464$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1465}14661467return$line."<br/>\n";1468}14691470# format from-file/to-file diff header1471sub format_diff_from_to_header {1472my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1473my$line;1474my$result='';14751476$line=$from_line;1477#assert($line =~ m/^---/) if DEBUG;1478# no extra formatting for "^--- /dev/null"1479if(!$diffinfo->{'nparents'}) {1480# ordinary (single parent) diff1481if($line=~m!^--- "?a/!) {1482if($from->{'href'}) {1483$line='--- a/'.1484$cgi->a({-href=>$from->{'href'}, -class=>"path"},1485 esc_path($from->{'file'}));1486}else{1487$line='--- a/'.1488 esc_path($from->{'file'});1489}1490}1491$result.= qq!<div class="diff from_file">$line</div>\n!;14921493}else{1494# combined diff (merge commit)1495for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1496if($from->{'href'}[$i]) {1497$line='--- '.1498$cgi->a({-href=>href(action=>"blobdiff",1499 hash_parent=>$diffinfo->{'from_id'}[$i],1500 hash_parent_base=>$parents[$i],1501 file_parent=>$from->{'file'}[$i],1502 hash=>$diffinfo->{'to_id'},1503 hash_base=>$hash,1504 file_name=>$to->{'file'}),1505-class=>"path",1506-title=>"diff". ($i+1)},1507$i+1) .1508'/'.1509$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1510 esc_path($from->{'file'}[$i]));1511}else{1512$line='--- /dev/null';1513}1514$result.= qq!<div class="diff from_file">$line</div>\n!;1515}1516}15171518$line=$to_line;1519#assert($line =~ m/^\+\+\+/) if DEBUG;1520# no extra formatting for "^+++ /dev/null"1521if($line=~m!^\+\+\+ "?b/!) {1522if($to->{'href'}) {1523$line='+++ b/'.1524$cgi->a({-href=>$to->{'href'}, -class=>"path"},1525 esc_path($to->{'file'}));1526}else{1527$line='+++ b/'.1528 esc_path($to->{'file'});1529}1530}1531$result.= qq!<div class="diff to_file">$line</div>\n!;15321533return$result;1534}15351536# create note for patch simplified by combined diff1537sub format_diff_cc_simplified {1538my($diffinfo,@parents) =@_;1539my$result='';15401541$result.="<div class=\"diff header\">".1542"diff --cc ";1543if(!is_deleted($diffinfo)) {1544$result.=$cgi->a({-href => href(action=>"blob",1545 hash_base=>$hash,1546 hash=>$diffinfo->{'to_id'},1547 file_name=>$diffinfo->{'to_file'}),1548-class=>"path"},1549 esc_path($diffinfo->{'to_file'}));1550}else{1551$result.= esc_path($diffinfo->{'to_file'});1552}1553$result.="</div>\n".# class="diff header"1554"<div class=\"diff nodifferences\">".1555"Simple merge".1556"</div>\n";# class="diff nodifferences"15571558return$result;1559}15601561# format patch (diff) line (not to be used for diff headers)1562sub format_diff_line {1563my$line=shift;1564my($from,$to) =@_;1565my$diff_class="";15661567chomp$line;15681569if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1570# combined diff1571my$prefix=substr($line,0,scalar@{$from->{'href'}});1572if($line=~m/^\@{3}/) {1573$diff_class=" chunk_header";1574}elsif($line=~m/^\\/) {1575$diff_class=" incomplete";1576}elsif($prefix=~tr/+/+/) {1577$diff_class=" add";1578}elsif($prefix=~tr/-/-/) {1579$diff_class=" rem";1580}1581}else{1582# assume ordinary diff1583my$char=substr($line,0,1);1584if($chareq'+') {1585$diff_class=" add";1586}elsif($chareq'-') {1587$diff_class=" rem";1588}elsif($chareq'@') {1589$diff_class=" chunk_header";1590}elsif($chareq"\\") {1591$diff_class=" incomplete";1592}1593}1594$line= untabify($line);1595if($from&&$to&&$line=~m/^\@{2} /) {1596my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1597$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;15981599$from_lines=0unlessdefined$from_lines;1600$to_lines=0unlessdefined$to_lines;16011602if($from->{'href'}) {1603$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1604-class=>"list"},$from_text);1605}1606if($to->{'href'}) {1607$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1608-class=>"list"},$to_text);1609}1610$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1611"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1612return"<div class=\"diff$diff_class\">$line</div>\n";1613}elsif($from&&$to&&$line=~m/^\@{3}/) {1614my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1615my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);16161617@from_text=split(' ',$ranges);1618for(my$i=0;$i<@from_text; ++$i) {1619($from_start[$i],$from_nlines[$i]) =1620(split(',',substr($from_text[$i],1)),0);1621}16221623$to_text=pop@from_text;1624$to_start=pop@from_start;1625$to_nlines=pop@from_nlines;16261627$line="<span class=\"chunk_info\">$prefix";1628for(my$i=0;$i<@from_text; ++$i) {1629if($from->{'href'}[$i]) {1630$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1631-class=>"list"},$from_text[$i]);1632}else{1633$line.=$from_text[$i];1634}1635$line.=" ";1636}1637if($to->{'href'}) {1638$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1639-class=>"list"},$to_text);1640}else{1641$line.=$to_text;1642}1643$line.="$prefix</span>".1644"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1645return"<div class=\"diff$diff_class\">$line</div>\n";1646}1647return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1648}16491650# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1651# linked. Pass the hash of the tree/commit to snapshot.1652sub format_snapshot_links {1653my($hash) =@_;1654my$num_fmts=@snapshot_fmts;1655if($num_fmts>1) {1656# A parenthesized list of links bearing format names.1657# e.g. "snapshot (_tar.gz_ _zip_)"1658return"snapshot (".join(' ',map1659$cgi->a({1660-href => href(1661 action=>"snapshot",1662 hash=>$hash,1663 snapshot_format=>$_1664)1665},$known_snapshot_formats{$_}{'display'})1666,@snapshot_fmts) .")";1667}elsif($num_fmts==1) {1668# A single "snapshot" link whose tooltip bears the format name.1669# i.e. "_snapshot_"1670my($fmt) =@snapshot_fmts;1671return1672$cgi->a({1673-href => href(1674 action=>"snapshot",1675 hash=>$hash,1676 snapshot_format=>$fmt1677),1678-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1679},"snapshot");1680}else{# $num_fmts == 01681returnundef;1682}1683}16841685## ......................................................................1686## functions returning values to be passed, perhaps after some1687## transformation, to other functions; e.g. returning arguments to href()16881689# returns hash to be passed to href to generate gitweb URL1690# in -title key it returns description of link1691sub get_feed_info {1692my$format=shift||'Atom';1693my%res= (action =>lc($format));16941695# feed links are possible only for project views1696return unless(defined$project);1697# some views should link to OPML, or to generic project feed,1698# or don't have specific feed yet (so they should use generic)1699return if($action=~/^(?:tags|heads|forks|tag|search)$/x);17001701my$branch;1702# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1703# from tag links; this also makes possible to detect branch links1704if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1705(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1706$branch=$1;1707}1708# find log type for feed description (title)1709my$type='log';1710if(defined$file_name) {1711$type="history of$file_name";1712$type.="/"if($actioneq'tree');1713$type.=" on '$branch'"if(defined$branch);1714}else{1715$type="log of$branch"if(defined$branch);1716}17171718$res{-title} =$type;1719$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1720$res{'file_name'} =$file_name;17211722return%res;1723}17241725## ----------------------------------------------------------------------1726## git utility subroutines, invoking git commands17271728# returns path to the core git executable and the --git-dir parameter as list1729sub git_cmd {1730return$GIT,'--git-dir='.$git_dir;1731}17321733# quote the given arguments for passing them to the shell1734# quote_command("command", "arg 1", "arg with ' and ! characters")1735# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1736# Try to avoid using this function wherever possible.1737sub quote_command {1738returnjoin(' ',1739map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1740}17411742# get HEAD ref of given project as hash1743sub git_get_head_hash {1744my$project=shift;1745my$o_git_dir=$git_dir;1746my$retval=undef;1747$git_dir="$projectroot/$project";1748if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1749my$head= <$fd>;1750close$fd;1751if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1752$retval=$1;1753}1754}1755if(defined$o_git_dir) {1756$git_dir=$o_git_dir;1757}1758return$retval;1759}17601761# get type of given object1762sub git_get_type {1763my$hash=shift;17641765open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1766my$type= <$fd>;1767close$fdorreturn;1768chomp$type;1769return$type;1770}17711772# repository configuration1773our$config_file='';1774our%config;17751776# store multiple values for single key as anonymous array reference1777# single values stored directly in the hash, not as [ <value> ]1778sub hash_set_multi {1779my($hash,$key,$value) =@_;17801781if(!exists$hash->{$key}) {1782$hash->{$key} =$value;1783}elsif(!ref$hash->{$key}) {1784$hash->{$key} = [$hash->{$key},$value];1785}else{1786push@{$hash->{$key}},$value;1787}1788}17891790# return hash of git project configuration1791# optionally limited to some section, e.g. 'gitweb'1792sub git_parse_project_config {1793my$section_regexp=shift;1794my%config;17951796local$/="\0";17971798open my$fh,"-|", git_cmd(),"config",'-z','-l',1799orreturn;18001801while(my$keyval= <$fh>) {1802chomp$keyval;1803my($key,$value) =split(/\n/,$keyval,2);18041805 hash_set_multi(\%config,$key,$value)1806if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1807}1808close$fh;18091810return%config;1811}18121813# convert config value to boolean, 'true' or 'false'1814# no value, number > 0, 'true' and 'yes' values are true1815# rest of values are treated as false (never as error)1816sub config_to_bool {1817my$val=shift;18181819# strip leading and trailing whitespace1820$val=~s/^\s+//;1821$val=~s/\s+$//;18221823return(!defined$val||# section.key1824($val=~/^\d+$/&&$val) ||# section.key = 11825($val=~/^(?:true|yes)$/i));# section.key = true1826}18271828# convert config value to simple decimal number1829# an optional value suffix of 'k', 'm', or 'g' will cause the value1830# to be multiplied by 1024, 1048576, or 10737418241831sub config_to_int {1832my$val=shift;18331834# strip leading and trailing whitespace1835$val=~s/^\s+//;1836$val=~s/\s+$//;18371838if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1839$unit=lc($unit);1840# unknown unit is treated as 11841return$num* ($uniteq'g'?1073741824:1842$uniteq'm'?1048576:1843$uniteq'k'?1024:1);1844}1845return$val;1846}18471848# convert config value to array reference, if needed1849sub config_to_multi {1850my$val=shift;18511852returnref($val) ?$val: (defined($val) ? [$val] : []);1853}18541855sub git_get_project_config {1856my($key,$type) =@_;18571858# key sanity check1859return unless($key);1860$key=~s/^gitweb\.//;1861return if($key=~m/\W/);18621863# type sanity check1864if(defined$type) {1865$type=~s/^--//;1866$type=undef1867unless($typeeq'bool'||$typeeq'int');1868}18691870# get config1871if(!defined$config_file||1872$config_filene"$git_dir/config") {1873%config= git_parse_project_config('gitweb');1874$config_file="$git_dir/config";1875}18761877# ensure given type1878if(!defined$type) {1879return$config{"gitweb.$key"};1880}elsif($typeeq'bool') {1881# backward compatibility: 'git config --bool' returns true/false1882return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1883}elsif($typeeq'int') {1884return config_to_int($config{"gitweb.$key"});1885}1886return$config{"gitweb.$key"};1887}18881889# get hash of given path at given ref1890sub git_get_hash_by_path {1891my$base=shift;1892my$path=shift||returnundef;1893my$type=shift;18941895$path=~ s,/+$,,;18961897open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path1898or die_error(500,"Open git-ls-tree failed");1899my$line= <$fd>;1900close$fdorreturnundef;19011902if(!defined$line) {1903# there is no tree or hash given by $path at $base1904returnundef;1905}19061907#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'1908$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;1909if(defined$type&&$typene$2) {1910# type doesn't match1911returnundef;1912}1913return$3;1914}19151916# get path of entry with given hash at given tree-ish (ref)1917# used to get 'from' filename for combined diff (merge commit) for renames1918sub git_get_path_by_hash {1919my$base=shift||return;1920my$hash=shift||return;19211922local$/="\0";19231924open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base1925orreturnundef;1926while(my$line= <$fd>) {1927chomp$line;19281929#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'1930#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'1931if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {1932close$fd;1933return$1;1934}1935}1936close$fd;1937returnundef;1938}19391940## ......................................................................1941## git utility functions, directly accessing git repository19421943sub git_get_project_description {1944my$path=shift;19451946$git_dir="$projectroot/$path";1947open my$fd,"$git_dir/description"1948orreturn git_get_project_config('description');1949my$descr= <$fd>;1950close$fd;1951if(defined$descr) {1952chomp$descr;1953}1954return$descr;1955}19561957sub git_get_project_ctags {1958my$path=shift;1959my$ctags= {};19601961$git_dir="$projectroot/$path";1962foreach(<$git_dir/ctags/*>) {1963open CT,$_ornext;1964my$val= <CT>;1965chomp$val;1966close CT;1967my$ctag=$_;$ctag=~ s#.*/##;1968$ctags->{$ctag} =$val;1969}1970$ctags;1971}19721973sub git_populate_project_tagcloud {1974my$ctags=shift;19751976# First, merge different-cased tags; tags vote on casing1977my%ctags_lc;1978foreach(keys%$ctags) {1979$ctags_lc{lc$_}->{count} +=$ctags->{$_};1980if(not$ctags_lc{lc$_}->{topcount}1981or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {1982$ctags_lc{lc$_}->{topcount} =$ctags->{$_};1983$ctags_lc{lc$_}->{topname} =$_;1984}1985}19861987my$cloud;1988if(eval{require HTML::TagCloud;1; }) {1989$cloud= HTML::TagCloud->new;1990foreach(sort keys%ctags_lc) {1991# Pad the title with spaces so that the cloud looks1992# less crammed.1993my$title=$ctags_lc{$_}->{topname};1994$title=~s/ / /g;1995$title=~s/^/ /g;1996$title=~s/$/ /g;1997$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});1998}1999}else{2000$cloud= \%ctags_lc;2001}2002$cloud;2003}20042005sub git_show_project_tagcloud {2006my($cloud,$count) =@_;2007print STDERR ref($cloud)."..\n";2008if(ref$cloudeq'HTML::TagCloud') {2009return$cloud->html_and_css($count);2010}else{2011my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2012return'<p align="center">'.join(', ',map{2013"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2014}splice(@tags,0,$count)) .'</p>';2015}2016}20172018sub git_get_project_url_list {2019my$path=shift;20202021$git_dir="$projectroot/$path";2022open my$fd,"$git_dir/cloneurl"2023orreturnwantarray?2024@{ config_to_multi(git_get_project_config('url')) } :2025 config_to_multi(git_get_project_config('url'));2026my@git_project_url_list=map{chomp;$_} <$fd>;2027close$fd;20282029returnwantarray?@git_project_url_list: \@git_project_url_list;2030}20312032sub git_get_projects_list {2033my($filter) =@_;2034my@list;20352036$filter||='';2037$filter=~s/\.git$//;20382039my($check_forks) = gitweb_check_feature('forks');20402041if(-d $projects_list) {2042# search in directory2043my$dir=$projects_list. ($filter?"/$filter":'');2044# remove the trailing "/"2045$dir=~s!/+$!!;2046my$pfxlen=length("$dir");2047my$pfxdepth= ($dir=~tr!/!!);20482049 File::Find::find({2050 follow_fast =>1,# follow symbolic links2051 follow_skip =>2,# ignore duplicates2052 dangling_symlinks =>0,# ignore dangling symlinks, silently2053 wanted =>sub{2054# skip project-list toplevel, if we get it.2055return if(m!^[/.]$!);2056# only directories can be git repositories2057return unless(-d $_);2058# don't traverse too deep (Find is super slow on os x)2059if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2060$File::Find::prune =1;2061return;2062}20632064my$subdir=substr($File::Find::name,$pfxlen+1);2065# we check related file in $projectroot2066if(check_export_ok("$projectroot/$filter/$subdir")) {2067push@list, { path => ($filter?"$filter/":'') .$subdir};2068$File::Find::prune =1;2069}2070},2071},"$dir");20722073}elsif(-f $projects_list) {2074# read from file(url-encoded):2075# 'git%2Fgit.git Linus+Torvalds'2076# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2077# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2078my%paths;2079open my($fd),$projects_listorreturn;2080 PROJECT:2081while(my$line= <$fd>) {2082chomp$line;2083my($path,$owner) =split' ',$line;2084$path= unescape($path);2085$owner= unescape($owner);2086if(!defined$path) {2087next;2088}2089if($filterne'') {2090# looking for forks;2091my$pfx=substr($path,0,length($filter));2092if($pfxne$filter) {2093next PROJECT;2094}2095my$sfx=substr($path,length($filter));2096if($sfx!~/^\/.*\.git$/) {2097next PROJECT;2098}2099}elsif($check_forks) {2100 PATH:2101foreachmy$filter(keys%paths) {2102# looking for forks;2103my$pfx=substr($path,0,length($filter));2104if($pfxne$filter) {2105next PATH;2106}2107my$sfx=substr($path,length($filter));2108if($sfx!~/^\/.*\.git$/) {2109next PATH;2110}2111# is a fork, don't include it in2112# the list2113next PROJECT;2114}2115}2116if(check_export_ok("$projectroot/$path")) {2117my$pr= {2118 path =>$path,2119 owner => to_utf8($owner),2120};2121push@list,$pr;2122(my$forks_path=$path) =~s/\.git$//;2123$paths{$forks_path}++;2124}2125}2126close$fd;2127}2128return@list;2129}21302131our$gitweb_project_owner=undef;2132sub git_get_project_list_from_file {21332134return if(defined$gitweb_project_owner);21352136$gitweb_project_owner= {};2137# read from file (url-encoded):2138# 'git%2Fgit.git Linus+Torvalds'2139# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2140# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2141if(-f $projects_list) {2142open(my$fd,$projects_list);2143while(my$line= <$fd>) {2144chomp$line;2145my($pr,$ow) =split' ',$line;2146$pr= unescape($pr);2147$ow= unescape($ow);2148$gitweb_project_owner->{$pr} = to_utf8($ow);2149}2150close$fd;2151}2152}21532154sub git_get_project_owner {2155my$project=shift;2156my$owner;21572158returnundefunless$project;2159$git_dir="$projectroot/$project";21602161if(!defined$gitweb_project_owner) {2162 git_get_project_list_from_file();2163}21642165if(exists$gitweb_project_owner->{$project}) {2166$owner=$gitweb_project_owner->{$project};2167}2168if(!defined$owner){2169$owner= git_get_project_config('owner');2170}2171if(!defined$owner) {2172$owner= get_file_owner("$git_dir");2173}21742175return$owner;2176}21772178sub git_get_last_activity {2179my($path) =@_;2180my$fd;21812182$git_dir="$projectroot/$path";2183open($fd,"-|", git_cmd(),'for-each-ref',2184'--format=%(committer)',2185'--sort=-committerdate',2186'--count=1',2187'refs/heads')orreturn;2188my$most_recent= <$fd>;2189close$fdorreturn;2190if(defined$most_recent&&2191$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2192my$timestamp=$1;2193my$age=time-$timestamp;2194return($age, age_string($age));2195}2196return(undef,undef);2197}21982199sub git_get_references {2200my$type=shift||"";2201my%refs;2202# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112203# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2204open my$fd,"-|", git_cmd(),"show-ref","--dereference",2205($type? ("--","refs/$type") : ())# use -- <pattern> if $type2206orreturn;22072208while(my$line= <$fd>) {2209chomp$line;2210if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2211if(defined$refs{$1}) {2212push@{$refs{$1}},$2;2213}else{2214$refs{$1} = [$2];2215}2216}2217}2218close$fdorreturn;2219return \%refs;2220}22212222sub git_get_rev_name_tags {2223my$hash=shift||returnundef;22242225open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2226orreturn;2227my$name_rev= <$fd>;2228close$fd;22292230if($name_rev=~ m|^$hash tags/(.*)$|) {2231return$1;2232}else{2233# catches also '$hash undefined' output2234returnundef;2235}2236}22372238## ----------------------------------------------------------------------2239## parse to hash functions22402241sub parse_date {2242my$epoch=shift;2243my$tz=shift||"-0000";22442245my%date;2246my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2247my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2248my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2249$date{'hour'} =$hour;2250$date{'minute'} =$min;2251$date{'mday'} =$mday;2252$date{'day'} =$days[$wday];2253$date{'month'} =$months[$mon];2254$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2255$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2256$date{'mday-time'} =sprintf"%d%s%02d:%02d",2257$mday,$months[$mon],$hour,$min;2258$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",22591900+$year,1+$mon,$mday,$hour,$min,$sec;22602261$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2262my$local=$epoch+ ((int$1+ ($2/60)) *3600);2263($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2264$date{'hour_local'} =$hour;2265$date{'minute_local'} =$min;2266$date{'tz_local'} =$tz;2267$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",22681900+$year,$mon+1,$mday,2269$hour,$min,$sec,$tz);2270return%date;2271}22722273sub parse_tag {2274my$tag_id=shift;2275my%tag;2276my@comment;22772278open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2279$tag{'id'} =$tag_id;2280while(my$line= <$fd>) {2281chomp$line;2282if($line=~m/^object ([0-9a-fA-F]{40})$/) {2283$tag{'object'} =$1;2284}elsif($line=~m/^type (.+)$/) {2285$tag{'type'} =$1;2286}elsif($line=~m/^tag (.+)$/) {2287$tag{'name'} =$1;2288}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2289$tag{'author'} =$1;2290$tag{'epoch'} =$2;2291$tag{'tz'} =$3;2292}elsif($line=~m/--BEGIN/) {2293push@comment,$line;2294last;2295}elsif($lineeq"") {2296last;2297}2298}2299push@comment, <$fd>;2300$tag{'comment'} = \@comment;2301close$fdorreturn;2302if(!defined$tag{'name'}) {2303return2304};2305return%tag2306}23072308sub parse_commit_text {2309my($commit_text,$withparents) =@_;2310my@commit_lines=split'\n',$commit_text;2311my%co;23122313pop@commit_lines;# Remove '\0'23142315if(!@commit_lines) {2316return;2317}23182319my$header=shift@commit_lines;2320if($header!~m/^[0-9a-fA-F]{40}/) {2321return;2322}2323($co{'id'},my@parents) =split' ',$header;2324while(my$line=shift@commit_lines) {2325last if$lineeq"\n";2326if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2327$co{'tree'} =$1;2328}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2329push@parents,$1;2330}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2331$co{'author'} =$1;2332$co{'author_epoch'} =$2;2333$co{'author_tz'} =$3;2334if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2335$co{'author_name'} =$1;2336$co{'author_email'} =$2;2337}else{2338$co{'author_name'} =$co{'author'};2339}2340}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2341$co{'committer'} =$1;2342$co{'committer_epoch'} =$2;2343$co{'committer_tz'} =$3;2344$co{'committer_name'} =$co{'committer'};2345if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2346$co{'committer_name'} =$1;2347$co{'committer_email'} =$2;2348}else{2349$co{'committer_name'} =$co{'committer'};2350}2351}2352}2353if(!defined$co{'tree'}) {2354return;2355};2356$co{'parents'} = \@parents;2357$co{'parent'} =$parents[0];23582359foreachmy$title(@commit_lines) {2360$title=~s/^ //;2361if($titlene"") {2362$co{'title'} = chop_str($title,80,5);2363# remove leading stuff of merges to make the interesting part visible2364if(length($title) >50) {2365$title=~s/^Automatic //;2366$title=~s/^merge (of|with) /Merge ... /i;2367if(length($title) >50) {2368$title=~s/(http|rsync):\/\///;2369}2370if(length($title) >50) {2371$title=~s/(master|www|rsync)\.//;2372}2373if(length($title) >50) {2374$title=~s/kernel.org:?//;2375}2376if(length($title) >50) {2377$title=~s/\/pub\/scm//;2378}2379}2380$co{'title_short'} = chop_str($title,50,5);2381last;2382}2383}2384if(!defined$co{'title'} ||$co{'title'}eq"") {2385$co{'title'} =$co{'title_short'} ='(no commit message)';2386}2387# remove added spaces2388foreachmy$line(@commit_lines) {2389$line=~s/^ //;2390}2391$co{'comment'} = \@commit_lines;23922393my$age=time-$co{'committer_epoch'};2394$co{'age'} =$age;2395$co{'age_string'} = age_string($age);2396my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2397if($age>60*60*24*7*2) {2398$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2399$co{'age_string_age'} =$co{'age_string'};2400}else{2401$co{'age_string_date'} =$co{'age_string'};2402$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2403}2404return%co;2405}24062407sub parse_commit {2408my($commit_id) =@_;2409my%co;24102411local$/="\0";24122413open my$fd,"-|", git_cmd(),"rev-list",2414"--parents",2415"--header",2416"--max-count=1",2417$commit_id,2418"--",2419or die_error(500,"Open git-rev-list failed");2420%co= parse_commit_text(<$fd>,1);2421close$fd;24222423return%co;2424}24252426sub parse_commits {2427my($commit_id,$maxcount,$skip,$filename,@args) =@_;2428my@cos;24292430$maxcount||=1;2431$skip||=0;24322433local$/="\0";24342435open my$fd,"-|", git_cmd(),"rev-list",2436"--header",2437@args,2438("--max-count=".$maxcount),2439("--skip=".$skip),2440@extra_options,2441$commit_id,2442"--",2443($filename? ($filename) : ())2444or die_error(500,"Open git-rev-list failed");2445while(my$line= <$fd>) {2446my%co= parse_commit_text($line);2447push@cos, \%co;2448}2449close$fd;24502451returnwantarray?@cos: \@cos;2452}24532454# parse line of git-diff-tree "raw" output2455sub parse_difftree_raw_line {2456my$line=shift;2457my%res;24582459# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2460# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2461if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2462$res{'from_mode'} =$1;2463$res{'to_mode'} =$2;2464$res{'from_id'} =$3;2465$res{'to_id'} =$4;2466$res{'status'} =$5;2467$res{'similarity'} =$6;2468if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2469($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2470}else{2471$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2472}2473}2474# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2475# combined diff (for merge commit)2476elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2477$res{'nparents'} =length($1);2478$res{'from_mode'} = [split(' ',$2) ];2479$res{'to_mode'} =pop@{$res{'from_mode'}};2480$res{'from_id'} = [split(' ',$3) ];2481$res{'to_id'} =pop@{$res{'from_id'}};2482$res{'status'} = [split('',$4) ];2483$res{'to_file'} = unquote($5);2484}2485# 'c512b523472485aef4fff9e57b229d9d243c967f'2486elsif($line=~m/^([0-9a-fA-F]{40})$/) {2487$res{'commit'} =$1;2488}24892490returnwantarray?%res: \%res;2491}24922493# wrapper: return parsed line of git-diff-tree "raw" output2494# (the argument might be raw line, or parsed info)2495sub parsed_difftree_line {2496my$line_or_ref=shift;24972498if(ref($line_or_ref)eq"HASH") {2499# pre-parsed (or generated by hand)2500return$line_or_ref;2501}else{2502return parse_difftree_raw_line($line_or_ref);2503}2504}25052506# parse line of git-ls-tree output2507sub parse_ls_tree_line ($;%) {2508my$line=shift;2509my%opts=@_;2510my%res;25112512#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2513$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;25142515$res{'mode'} =$1;2516$res{'type'} =$2;2517$res{'hash'} =$3;2518if($opts{'-z'}) {2519$res{'name'} =$4;2520}else{2521$res{'name'} = unquote($4);2522}25232524returnwantarray?%res: \%res;2525}25262527# generates _two_ hashes, references to which are passed as 2 and 3 argument2528sub parse_from_to_diffinfo {2529my($diffinfo,$from,$to,@parents) =@_;25302531if($diffinfo->{'nparents'}) {2532# combined diff2533$from->{'file'} = [];2534$from->{'href'} = [];2535 fill_from_file_info($diffinfo,@parents)2536unlessexists$diffinfo->{'from_file'};2537for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2538$from->{'file'}[$i] =2539defined$diffinfo->{'from_file'}[$i] ?2540$diffinfo->{'from_file'}[$i] :2541$diffinfo->{'to_file'};2542if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2543$from->{'href'}[$i] = href(action=>"blob",2544 hash_base=>$parents[$i],2545 hash=>$diffinfo->{'from_id'}[$i],2546 file_name=>$from->{'file'}[$i]);2547}else{2548$from->{'href'}[$i] =undef;2549}2550}2551}else{2552# ordinary (not combined) diff2553$from->{'file'} =$diffinfo->{'from_file'};2554if($diffinfo->{'status'}ne"A") {# not new (added) file2555$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2556 hash=>$diffinfo->{'from_id'},2557 file_name=>$from->{'file'});2558}else{2559delete$from->{'href'};2560}2561}25622563$to->{'file'} =$diffinfo->{'to_file'};2564if(!is_deleted($diffinfo)) {# file exists in result2565$to->{'href'} = href(action=>"blob", hash_base=>$hash,2566 hash=>$diffinfo->{'to_id'},2567 file_name=>$to->{'file'});2568}else{2569delete$to->{'href'};2570}2571}25722573## ......................................................................2574## parse to array of hashes functions25752576sub git_get_heads_list {2577my$limit=shift;2578my@headslist;25792580open my$fd,'-|', git_cmd(),'for-each-ref',2581($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2582'--format=%(objectname) %(refname) %(subject)%00%(committer)',2583'refs/heads'2584orreturn;2585while(my$line= <$fd>) {2586my%ref_item;25872588chomp$line;2589my($refinfo,$committerinfo) =split(/\0/,$line);2590my($hash,$name,$title) =split(' ',$refinfo,3);2591my($committer,$epoch,$tz) =2592($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2593$ref_item{'fullname'} =$name;2594$name=~s!^refs/heads/!!;25952596$ref_item{'name'} =$name;2597$ref_item{'id'} =$hash;2598$ref_item{'title'} =$title||'(no commit message)';2599$ref_item{'epoch'} =$epoch;2600if($epoch) {2601$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2602}else{2603$ref_item{'age'} ="unknown";2604}26052606push@headslist, \%ref_item;2607}2608close$fd;26092610returnwantarray?@headslist: \@headslist;2611}26122613sub git_get_tags_list {2614my$limit=shift;2615my@tagslist;26162617open my$fd,'-|', git_cmd(),'for-each-ref',2618($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2619'--format=%(objectname) %(objecttype) %(refname) '.2620'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2621'refs/tags'2622orreturn;2623while(my$line= <$fd>) {2624my%ref_item;26252626chomp$line;2627my($refinfo,$creatorinfo) =split(/\0/,$line);2628my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2629my($creator,$epoch,$tz) =2630($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2631$ref_item{'fullname'} =$name;2632$name=~s!^refs/tags/!!;26332634$ref_item{'type'} =$type;2635$ref_item{'id'} =$id;2636$ref_item{'name'} =$name;2637if($typeeq"tag") {2638$ref_item{'subject'} =$title;2639$ref_item{'reftype'} =$reftype;2640$ref_item{'refid'} =$refid;2641}else{2642$ref_item{'reftype'} =$type;2643$ref_item{'refid'} =$id;2644}26452646if($typeeq"tag"||$typeeq"commit") {2647$ref_item{'epoch'} =$epoch;2648if($epoch) {2649$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2650}else{2651$ref_item{'age'} ="unknown";2652}2653}26542655push@tagslist, \%ref_item;2656}2657close$fd;26582659returnwantarray?@tagslist: \@tagslist;2660}26612662## ----------------------------------------------------------------------2663## filesystem-related functions26642665sub get_file_owner {2666my$path=shift;26672668my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2669my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2670if(!defined$gcos) {2671returnundef;2672}2673my$owner=$gcos;2674$owner=~s/[,;].*$//;2675return to_utf8($owner);2676}26772678## ......................................................................2679## mimetype related functions26802681sub mimetype_guess_file {2682my$filename=shift;2683my$mimemap=shift;2684-r $mimemaporreturnundef;26852686my%mimemap;2687open(MIME,$mimemap)orreturnundef;2688while(<MIME>) {2689next ifm/^#/;# skip comments2690my($mime,$exts) =split(/\t+/);2691if(defined$exts) {2692my@exts=split(/\s+/,$exts);2693foreachmy$ext(@exts) {2694$mimemap{$ext} =$mime;2695}2696}2697}2698close(MIME);26992700$filename=~/\.([^.]*)$/;2701return$mimemap{$1};2702}27032704sub mimetype_guess {2705my$filename=shift;2706my$mime;2707$filename=~/\./orreturnundef;27082709if($mimetypes_file) {2710my$file=$mimetypes_file;2711if($file!~m!^/!) {# if it is relative path2712# it is relative to project2713$file="$projectroot/$project/$file";2714}2715$mime= mimetype_guess_file($filename,$file);2716}2717$mime||= mimetype_guess_file($filename,'/etc/mime.types');2718return$mime;2719}27202721sub blob_mimetype {2722my$fd=shift;2723my$filename=shift;27242725if($filename) {2726my$mime= mimetype_guess($filename);2727$mimeandreturn$mime;2728}27292730# just in case2731return$default_blob_plain_mimetypeunless$fd;27322733if(-T $fd) {2734return'text/plain';2735}elsif(!$filename) {2736return'application/octet-stream';2737}elsif($filename=~m/\.png$/i) {2738return'image/png';2739}elsif($filename=~m/\.gif$/i) {2740return'image/gif';2741}elsif($filename=~m/\.jpe?g$/i) {2742return'image/jpeg';2743}else{2744return'application/octet-stream';2745}2746}27472748sub blob_contenttype {2749my($fd,$file_name,$type) =@_;27502751$type||= blob_mimetype($fd,$file_name);2752if($typeeq'text/plain'&&defined$default_text_plain_charset) {2753$type.="; charset=$default_text_plain_charset";2754}27552756return$type;2757}27582759## ======================================================================2760## functions printing HTML: header, footer, error page27612762sub git_header_html {2763my$status=shift||"200 OK";2764my$expires=shift;27652766my$title="$site_name";2767if(defined$project) {2768$title.=" - ". to_utf8($project);2769if(defined$action) {2770$title.="/$action";2771if(defined$file_name) {2772$title.=" - ". esc_path($file_name);2773if($actioneq"tree"&&$file_name!~ m|/$|) {2774$title.="/";2775}2776}2777}2778}2779my$content_type;2780# require explicit support from the UA if we are to send the page as2781# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2782# we have to do this because MSIE sometimes globs '*/*', pretending to2783# support xhtml+xml but choking when it gets what it asked for.2784if(defined$cgi->http('HTTP_ACCEPT') &&2785$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2786$cgi->Accept('application/xhtml+xml') !=0) {2787$content_type='application/xhtml+xml';2788}else{2789$content_type='text/html';2790}2791print$cgi->header(-type=>$content_type, -charset =>'utf-8',2792-status=>$status, -expires =>$expires);2793my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2794print<<EOF;2795<?xml version="1.0" encoding="utf-8"?>2796<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2797<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2798<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2799<!-- git core binaries version$git_version-->2800<head>2801<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2802<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2803<meta name="robots" content="index, nofollow"/>2804<title>$title</title>2805EOF2806# print out each stylesheet that exist2807if(defined$stylesheet) {2808#provides backwards capability for those people who define style sheet in a config file2809print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2810}else{2811foreachmy$stylesheet(@stylesheets) {2812next unless$stylesheet;2813print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2814}2815}2816if(defined$project) {2817my%href_params= get_feed_info();2818if(!exists$href_params{'-title'}) {2819$href_params{'-title'} ='log';2820}28212822foreachmy$formatqw(RSS Atom){2823my$type=lc($format);2824my%link_attr= (2825'-rel'=>'alternate',2826'-title'=>"$project-$href_params{'-title'} -$formatfeed",2827'-type'=>"application/$type+xml"2828);28292830$href_params{'action'} =$type;2831$link_attr{'-href'} = href(%href_params);2832print"<link ".2833"rel=\"$link_attr{'-rel'}\"".2834"title=\"$link_attr{'-title'}\"".2835"href=\"$link_attr{'-href'}\"".2836"type=\"$link_attr{'-type'}\"".2837"/>\n";28382839$href_params{'extra_options'} ='--no-merges';2840$link_attr{'-href'} = href(%href_params);2841$link_attr{'-title'} .=' (no merges)';2842print"<link ".2843"rel=\"$link_attr{'-rel'}\"".2844"title=\"$link_attr{'-title'}\"".2845"href=\"$link_attr{'-href'}\"".2846"type=\"$link_attr{'-type'}\"".2847"/>\n";2848}28492850}else{2851printf('<link rel="alternate" title="%sprojects list" '.2852'href="%s" type="text/plain; charset=utf-8" />'."\n",2853$site_name, href(project=>undef, action=>"project_index"));2854printf('<link rel="alternate" title="%sprojects feeds" '.2855'href="%s" type="text/x-opml" />'."\n",2856$site_name, href(project=>undef, action=>"opml"));2857}2858if(defined$favicon) {2859printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2860}28612862print"</head>\n".2863"<body>\n";28642865if(-f $site_header) {2866open(my$fd,$site_header);2867print<$fd>;2868close$fd;2869}28702871print"<div class=\"page_header\">\n".2872$cgi->a({-href => esc_url($logo_url),2873-title =>$logo_label},2874qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2875print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2876if(defined$project) {2877print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2878if(defined$action) {2879print" /$action";2880}2881print"\n";2882}2883print"</div>\n";28842885my($have_search) = gitweb_check_feature('search');2886if(defined$project&&$have_search) {2887if(!defined$searchtext) {2888$searchtext="";2889}2890my$search_hash;2891if(defined$hash_base) {2892$search_hash=$hash_base;2893}elsif(defined$hash) {2894$search_hash=$hash;2895}else{2896$search_hash="HEAD";2897}2898my$action=$my_uri;2899my($use_pathinfo) = gitweb_check_feature('pathinfo');2900if($use_pathinfo) {2901$action.="/".esc_url($project);2902}2903print$cgi->startform(-method=>"get", -action =>$action) .2904"<div class=\"search\">\n".2905(!$use_pathinfo&&2906$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .2907$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".2908$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".2909$cgi->popup_menu(-name =>'st', -default=>'commit',2910-values=> ['commit','grep','author','committer','pickaxe']) .2911$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .2912" search:\n",2913$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".2914"<span title=\"Extended regular expression\">".2915$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',2916-checked =>$search_use_regexp) .2917"</span>".2918"</div>".2919$cgi->end_form() ."\n";2920}2921}29222923sub git_footer_html {2924my$feed_class='rss_logo';29252926print"<div class=\"page_footer\">\n";2927if(defined$project) {2928my$descr= git_get_project_description($project);2929if(defined$descr) {2930print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";2931}29322933my%href_params= get_feed_info();2934if(!%href_params) {2935$feed_class.=' generic';2936}2937$href_params{'-title'} ||='log';29382939foreachmy$formatqw(RSS Atom){2940$href_params{'action'} =lc($format);2941print$cgi->a({-href => href(%href_params),2942-title =>"$href_params{'-title'}$formatfeed",2943-class=>$feed_class},$format)."\n";2944}29452946}else{2947print$cgi->a({-href => href(project=>undef, action=>"opml"),2948-class=>$feed_class},"OPML") ." ";2949print$cgi->a({-href => href(project=>undef, action=>"project_index"),2950-class=>$feed_class},"TXT") ."\n";2951}2952print"</div>\n";# class="page_footer"29532954if(-f $site_footer) {2955open(my$fd,$site_footer);2956print<$fd>;2957close$fd;2958}29592960print"</body>\n".2961"</html>";2962}29632964# die_error(<http_status_code>, <error_message>)2965# Example: die_error(404, 'Hash not found')2966# By convention, use the following status codes (as defined in RFC 2616):2967# 400: Invalid or missing CGI parameters, or2968# requested object exists but has wrong type.2969# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on2970# this server or project.2971# 404: Requested object/revision/project doesn't exist.2972# 500: The server isn't configured properly, or2973# an internal error occurred (e.g. failed assertions caused by bugs), or2974# an unknown error occurred (e.g. the git binary died unexpectedly).2975sub die_error {2976my$status=shift||500;2977my$error=shift||"Internal server error";29782979my%http_responses= (400=>'400 Bad Request',2980403=>'403 Forbidden',2981404=>'404 Not Found',2982500=>'500 Internal Server Error');2983 git_header_html($http_responses{$status});2984print<<EOF;2985<div class="page_body">2986<br /><br />2987$status-$error2988<br />2989</div>2990EOF2991 git_footer_html();2992exit;2993}29942995## ----------------------------------------------------------------------2996## functions printing or outputting HTML: navigation29972998sub git_print_page_nav {2999my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3000$extra=''if!defined$extra;# pager or formats30013002my@navs=qw(summary shortlog log commit commitdiff tree);3003if($suppress) {3004@navs=grep{$_ne$suppress}@navs;3005}30063007my%arg=map{$_=> {action=>$_} }@navs;3008if(defined$head) {3009for(qw(commit commitdiff)) {3010$arg{$_}{'hash'} =$head;3011}3012if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3013for(qw(shortlog log)) {3014$arg{$_}{'hash'} =$head;3015}3016}3017}30183019$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3020$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;30213022my@actions= gitweb_check_feature('actions');3023while(@actions) {3024my($label,$link,$pos) = (shift(@actions),shift(@actions),shift(@actions));3025@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3026# munch munch3027$link=~ s#%n#$project#g;3028$link=~ s#%f#$git_dir#g;3029$treehead?$link=~ s#%h#$treehead#g : $link =~ s#%h##g;3030$treebase?$link=~ s#%b#$treebase#g : $link =~ s#%b##g;3031$arg{$label}{'_href'} =$link;3032}30333034print"<div class=\"page_nav\">\n".3035(join" | ",3036map{$_eq$current?3037$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3038}@navs);3039print"<br/>\n$extra<br/>\n".3040"</div>\n";3041}30423043sub format_paging_nav {3044my($action,$hash,$head,$page,$has_next_link) =@_;3045my$paging_nav;304630473048if($hashne$head||$page) {3049$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3050}else{3051$paging_nav.="HEAD";3052}30533054if($page>0) {3055$paging_nav.=" ⋅ ".3056$cgi->a({-href => href(-replay=>1, page=>$page-1),3057-accesskey =>"p", -title =>"Alt-p"},"prev");3058}else{3059$paging_nav.=" ⋅ prev";3060}30613062if($has_next_link) {3063$paging_nav.=" ⋅ ".3064$cgi->a({-href => href(-replay=>1, page=>$page+1),3065-accesskey =>"n", -title =>"Alt-n"},"next");3066}else{3067$paging_nav.=" ⋅ next";3068}30693070return$paging_nav;3071}30723073## ......................................................................3074## functions printing or outputting HTML: div30753076sub git_print_header_div {3077my($action,$title,$hash,$hash_base) =@_;3078my%args= ();30793080$args{'action'} =$action;3081$args{'hash'} =$hashif$hash;3082$args{'hash_base'} =$hash_baseif$hash_base;30833084print"<div class=\"header\">\n".3085$cgi->a({-href => href(%args), -class=>"title"},3086$title?$title:$action) .3087"\n</div>\n";3088}30893090#sub git_print_authorship (\%) {3091sub git_print_authorship {3092my$co=shift;30933094my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3095print"<div class=\"author_date\">".3096 esc_html($co->{'author_name'}) .3097" [$ad{'rfc2822'}";3098if($ad{'hour_local'} <6) {3099printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3100$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3101}else{3102printf(" (%02d:%02d%s)",3103$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3104}3105print"]</div>\n";3106}31073108sub git_print_page_path {3109my$name=shift;3110my$type=shift;3111my$hb=shift;311231133114print"<div class=\"page_path\">";3115print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3116-title =>'tree root'}, to_utf8("[$project]"));3117print" / ";3118if(defined$name) {3119my@dirname=split'/',$name;3120my$basename=pop@dirname;3121my$fullname='';31223123foreachmy$dir(@dirname) {3124$fullname.= ($fullname?'/':'') .$dir;3125print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3126 hash_base=>$hb),3127-title =>$fullname}, esc_path($dir));3128print" / ";3129}3130if(defined$type&&$typeeq'blob') {3131print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3132 hash_base=>$hb),3133-title =>$name}, esc_path($basename));3134}elsif(defined$type&&$typeeq'tree') {3135print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3136 hash_base=>$hb),3137-title =>$name}, esc_path($basename));3138print" / ";3139}else{3140print esc_path($basename);3141}3142}3143print"<br/></div>\n";3144}31453146# sub git_print_log (\@;%) {3147sub git_print_log ($;%) {3148my$log=shift;3149my%opts=@_;31503151if($opts{'-remove_title'}) {3152# remove title, i.e. first line of log3153shift@$log;3154}3155# remove leading empty lines3156while(defined$log->[0] &&$log->[0]eq"") {3157shift@$log;3158}31593160# print log3161my$signoff=0;3162my$empty=0;3163foreachmy$line(@$log) {3164if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3165$signoff=1;3166$empty=0;3167if(!$opts{'-remove_signoff'}) {3168print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3169next;3170}else{3171# remove signoff lines3172next;3173}3174}else{3175$signoff=0;3176}31773178# print only one empty line3179# do not print empty line after signoff3180if($lineeq"") {3181next if($empty||$signoff);3182$empty=1;3183}else{3184$empty=0;3185}31863187print format_log_line_html($line) ."<br/>\n";3188}31893190if($opts{'-final_empty_line'}) {3191# end with single empty line3192print"<br/>\n"unless$empty;3193}3194}31953196# return link target (what link points to)3197sub git_get_link_target {3198my$hash=shift;3199my$link_target;32003201# read link3202open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3203orreturn;3204{3205local$/;3206$link_target= <$fd>;3207}3208close$fd3209orreturn;32103211return$link_target;3212}32133214# given link target, and the directory (basedir) the link is in,3215# return target of link relative to top directory (top tree);3216# return undef if it is not possible (including absolute links).3217sub normalize_link_target {3218my($link_target,$basedir,$hash_base) =@_;32193220# we can normalize symlink target only if $hash_base is provided3221return unless$hash_base;32223223# absolute symlinks (beginning with '/') cannot be normalized3224return if(substr($link_target,0,1)eq'/');32253226# normalize link target to path from top (root) tree (dir)3227my$path;3228if($basedir) {3229$path=$basedir.'/'.$link_target;3230}else{3231# we are in top (root) tree (dir)3232$path=$link_target;3233}32343235# remove //, /./, and /../3236my@path_parts;3237foreachmy$part(split('/',$path)) {3238# discard '.' and ''3239next if(!$part||$parteq'.');3240# handle '..'3241if($parteq'..') {3242if(@path_parts) {3243pop@path_parts;3244}else{3245# link leads outside repository (outside top dir)3246return;3247}3248}else{3249push@path_parts,$part;3250}3251}3252$path=join('/',@path_parts);32533254return$path;3255}32563257# print tree entry (row of git_tree), but without encompassing <tr> element3258sub git_print_tree_entry {3259my($t,$basedir,$hash_base,$have_blame) =@_;32603261my%base_key= ();3262$base_key{'hash_base'} =$hash_baseifdefined$hash_base;32633264# The format of a table row is: mode list link. Where mode is3265# the mode of the entry, list is the name of the entry, an href,3266# and link is the action links of the entry.32673268print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3269if($t->{'type'}eq"blob") {3270print"<td class=\"list\">".3271$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3272 file_name=>"$basedir$t->{'name'}",%base_key),3273-class=>"list"}, esc_path($t->{'name'}));3274if(S_ISLNK(oct$t->{'mode'})) {3275my$link_target= git_get_link_target($t->{'hash'});3276if($link_target) {3277my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3278if(defined$norm_target) {3279print" -> ".3280$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3281 file_name=>$norm_target),3282-title =>$norm_target}, esc_path($link_target));3283}else{3284print" -> ". esc_path($link_target);3285}3286}3287}3288print"</td>\n";3289print"<td class=\"link\">";3290print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3291 file_name=>"$basedir$t->{'name'}",%base_key)},3292"blob");3293if($have_blame) {3294print" | ".3295$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3296 file_name=>"$basedir$t->{'name'}",%base_key)},3297"blame");3298}3299if(defined$hash_base) {3300print" | ".3301$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3302 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3303"history");3304}3305print" | ".3306$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3307 file_name=>"$basedir$t->{'name'}")},3308"raw");3309print"</td>\n";33103311}elsif($t->{'type'}eq"tree") {3312print"<td class=\"list\">";3313print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3314 file_name=>"$basedir$t->{'name'}",%base_key)},3315 esc_path($t->{'name'}));3316print"</td>\n";3317print"<td class=\"link\">";3318print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3319 file_name=>"$basedir$t->{'name'}",%base_key)},3320"tree");3321if(defined$hash_base) {3322print" | ".3323$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3324 file_name=>"$basedir$t->{'name'}")},3325"history");3326}3327print"</td>\n";3328}else{3329# unknown object: we can only present history for it3330# (this includes 'commit' object, i.e. submodule support)3331print"<td class=\"list\">".3332 esc_path($t->{'name'}) .3333"</td>\n";3334print"<td class=\"link\">";3335if(defined$hash_base) {3336print$cgi->a({-href => href(action=>"history",3337 hash_base=>$hash_base,3338 file_name=>"$basedir$t->{'name'}")},3339"history");3340}3341print"</td>\n";3342}3343}33443345## ......................................................................3346## functions printing large fragments of HTML33473348# get pre-image filenames for merge (combined) diff3349sub fill_from_file_info {3350my($diff,@parents) =@_;33513352$diff->{'from_file'} = [ ];3353$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3354for(my$i=0;$i<$diff->{'nparents'};$i++) {3355if($diff->{'status'}[$i]eq'R'||3356$diff->{'status'}[$i]eq'C') {3357$diff->{'from_file'}[$i] =3358 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3359}3360}33613362return$diff;3363}33643365# is current raw difftree line of file deletion3366sub is_deleted {3367my$diffinfo=shift;33683369return$diffinfo->{'to_id'}eq('0' x 40);3370}33713372# does patch correspond to [previous] difftree raw line3373# $diffinfo - hashref of parsed raw diff format3374# $patchinfo - hashref of parsed patch diff format3375# (the same keys as in $diffinfo)3376sub is_patch_split {3377my($diffinfo,$patchinfo) =@_;33783379returndefined$diffinfo&&defined$patchinfo3380&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3381}338233833384sub git_difftree_body {3385my($difftree,$hash,@parents) =@_;3386my($parent) =$parents[0];3387my($have_blame) = gitweb_check_feature('blame');3388print"<div class=\"list_head\">\n";3389if($#{$difftree} >10) {3390print(($#{$difftree} +1) ." files changed:\n");3391}3392print"</div>\n";33933394print"<table class=\"".3395(@parents>1?"combined ":"") .3396"diff_tree\">\n";33973398# header only for combined diff in 'commitdiff' view3399my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3400if($has_header) {3401# table header3402print"<thead><tr>\n".3403"<th></th><th></th>\n";# filename, patchN link3404for(my$i=0;$i<@parents;$i++) {3405my$par=$parents[$i];3406print"<th>".3407$cgi->a({-href => href(action=>"commitdiff",3408 hash=>$hash, hash_parent=>$par),3409-title =>'commitdiff to parent number '.3410($i+1) .': '.substr($par,0,7)},3411$i+1) .3412" </th>\n";3413}3414print"</tr></thead>\n<tbody>\n";3415}34163417my$alternate=1;3418my$patchno=0;3419foreachmy$line(@{$difftree}) {3420my$diff= parsed_difftree_line($line);34213422if($alternate) {3423print"<tr class=\"dark\">\n";3424}else{3425print"<tr class=\"light\">\n";3426}3427$alternate^=1;34283429if(exists$diff->{'nparents'}) {# combined diff34303431 fill_from_file_info($diff,@parents)3432unlessexists$diff->{'from_file'};34333434if(!is_deleted($diff)) {3435# file exists in the result (child) commit3436print"<td>".3437$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3438 file_name=>$diff->{'to_file'},3439 hash_base=>$hash),3440-class=>"list"}, esc_path($diff->{'to_file'})) .3441"</td>\n";3442}else{3443print"<td>".3444 esc_path($diff->{'to_file'}) .3445"</td>\n";3446}34473448if($actioneq'commitdiff') {3449# link to patch3450$patchno++;3451print"<td class=\"link\">".3452$cgi->a({-href =>"#patch$patchno"},"patch") .3453" | ".3454"</td>\n";3455}34563457my$has_history=0;3458my$not_deleted=0;3459for(my$i=0;$i<$diff->{'nparents'};$i++) {3460my$hash_parent=$parents[$i];3461my$from_hash=$diff->{'from_id'}[$i];3462my$from_path=$diff->{'from_file'}[$i];3463my$status=$diff->{'status'}[$i];34643465$has_history||= ($statusne'A');3466$not_deleted||= ($statusne'D');34673468if($statuseq'A') {3469print"<td class=\"link\"align=\"right\"> | </td>\n";3470}elsif($statuseq'D') {3471print"<td class=\"link\">".3472$cgi->a({-href => href(action=>"blob",3473 hash_base=>$hash,3474 hash=>$from_hash,3475 file_name=>$from_path)},3476"blob". ($i+1)) .3477" | </td>\n";3478}else{3479if($diff->{'to_id'}eq$from_hash) {3480print"<td class=\"link nochange\">";3481}else{3482print"<td class=\"link\">";3483}3484print$cgi->a({-href => href(action=>"blobdiff",3485 hash=>$diff->{'to_id'},3486 hash_parent=>$from_hash,3487 hash_base=>$hash,3488 hash_parent_base=>$hash_parent,3489 file_name=>$diff->{'to_file'},3490 file_parent=>$from_path)},3491"diff". ($i+1)) .3492" | </td>\n";3493}3494}34953496print"<td class=\"link\">";3497if($not_deleted) {3498print$cgi->a({-href => href(action=>"blob",3499 hash=>$diff->{'to_id'},3500 file_name=>$diff->{'to_file'},3501 hash_base=>$hash)},3502"blob");3503print" | "if($has_history);3504}3505if($has_history) {3506print$cgi->a({-href => href(action=>"history",3507 file_name=>$diff->{'to_file'},3508 hash_base=>$hash)},3509"history");3510}3511print"</td>\n";35123513print"</tr>\n";3514next;# instead of 'else' clause, to avoid extra indent3515}3516# else ordinary diff35173518my($to_mode_oct,$to_mode_str,$to_file_type);3519my($from_mode_oct,$from_mode_str,$from_file_type);3520if($diff->{'to_mode'}ne('0' x 6)) {3521$to_mode_oct=oct$diff->{'to_mode'};3522if(S_ISREG($to_mode_oct)) {# only for regular file3523$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3524}3525$to_file_type= file_type($diff->{'to_mode'});3526}3527if($diff->{'from_mode'}ne('0' x 6)) {3528$from_mode_oct=oct$diff->{'from_mode'};3529if(S_ISREG($to_mode_oct)) {# only for regular file3530$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3531}3532$from_file_type= file_type($diff->{'from_mode'});3533}35343535if($diff->{'status'}eq"A") {# created3536my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3537$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3538$mode_chng.="]</span>";3539print"<td>";3540print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3541 hash_base=>$hash, file_name=>$diff->{'file'}),3542-class=>"list"}, esc_path($diff->{'file'}));3543print"</td>\n";3544print"<td>$mode_chng</td>\n";3545print"<td class=\"link\">";3546if($actioneq'commitdiff') {3547# link to patch3548$patchno++;3549print$cgi->a({-href =>"#patch$patchno"},"patch");3550print" | ";3551}3552print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3553 hash_base=>$hash, file_name=>$diff->{'file'})},3554"blob");3555print"</td>\n";35563557}elsif($diff->{'status'}eq"D") {# deleted3558my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3559print"<td>";3560print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3561 hash_base=>$parent, file_name=>$diff->{'file'}),3562-class=>"list"}, esc_path($diff->{'file'}));3563print"</td>\n";3564print"<td>$mode_chng</td>\n";3565print"<td class=\"link\">";3566if($actioneq'commitdiff') {3567# link to patch3568$patchno++;3569print$cgi->a({-href =>"#patch$patchno"},"patch");3570print" | ";3571}3572print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3573 hash_base=>$parent, file_name=>$diff->{'file'})},3574"blob") ." | ";3575if($have_blame) {3576print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3577 file_name=>$diff->{'file'})},3578"blame") ." | ";3579}3580print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3581 file_name=>$diff->{'file'})},3582"history");3583print"</td>\n";35843585}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3586my$mode_chnge="";3587if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3588$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3589if($from_file_typene$to_file_type) {3590$mode_chnge.=" from$from_file_typeto$to_file_type";3591}3592if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3593if($from_mode_str&&$to_mode_str) {3594$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3595}elsif($to_mode_str) {3596$mode_chnge.=" mode:$to_mode_str";3597}3598}3599$mode_chnge.="]</span>\n";3600}3601print"<td>";3602print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3603 hash_base=>$hash, file_name=>$diff->{'file'}),3604-class=>"list"}, esc_path($diff->{'file'}));3605print"</td>\n";3606print"<td>$mode_chnge</td>\n";3607print"<td class=\"link\">";3608if($actioneq'commitdiff') {3609# link to patch3610$patchno++;3611print$cgi->a({-href =>"#patch$patchno"},"patch") .3612" | ";3613}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3614# "commit" view and modified file (not onlu mode changed)3615print$cgi->a({-href => href(action=>"blobdiff",3616 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3617 hash_base=>$hash, hash_parent_base=>$parent,3618 file_name=>$diff->{'file'})},3619"diff") .3620" | ";3621}3622print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3623 hash_base=>$hash, file_name=>$diff->{'file'})},3624"blob") ." | ";3625if($have_blame) {3626print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3627 file_name=>$diff->{'file'})},3628"blame") ." | ";3629}3630print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3631 file_name=>$diff->{'file'})},3632"history");3633print"</td>\n";36343635}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3636my%status_name= ('R'=>'moved','C'=>'copied');3637my$nstatus=$status_name{$diff->{'status'}};3638my$mode_chng="";3639if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3640# mode also for directories, so we cannot use $to_mode_str3641$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3642}3643print"<td>".3644$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3645 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3646-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3647"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3648$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3649 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3650-class=>"list"}, esc_path($diff->{'from_file'})) .3651" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3652"<td class=\"link\">";3653if($actioneq'commitdiff') {3654# link to patch3655$patchno++;3656print$cgi->a({-href =>"#patch$patchno"},"patch") .3657" | ";3658}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3659# "commit" view and modified file (not only pure rename or copy)3660print$cgi->a({-href => href(action=>"blobdiff",3661 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3662 hash_base=>$hash, hash_parent_base=>$parent,3663 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3664"diff") .3665" | ";3666}3667print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3668 hash_base=>$parent, file_name=>$diff->{'to_file'})},3669"blob") ." | ";3670if($have_blame) {3671print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3672 file_name=>$diff->{'to_file'})},3673"blame") ." | ";3674}3675print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3676 file_name=>$diff->{'to_file'})},3677"history");3678print"</td>\n";36793680}# we should not encounter Unmerged (U) or Unknown (X) status3681print"</tr>\n";3682}3683print"</tbody>"if$has_header;3684print"</table>\n";3685}36863687sub git_patchset_body {3688my($fd,$difftree,$hash,@hash_parents) =@_;3689my($hash_parent) =$hash_parents[0];36903691my$is_combined= (@hash_parents>1);3692my$patch_idx=0;3693my$patch_number=0;3694my$patch_line;3695my$diffinfo;3696my$to_name;3697my(%from,%to);36983699print"<div class=\"patchset\">\n";37003701# skip to first patch3702while($patch_line= <$fd>) {3703chomp$patch_line;37043705last if($patch_line=~m/^diff /);3706}37073708 PATCH:3709while($patch_line) {37103711# parse "git diff" header line3712if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3713# $1 is from_name, which we do not use3714$to_name= unquote($2);3715$to_name=~s!^b/!!;3716}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3717# $1 is 'cc' or 'combined', which we do not use3718$to_name= unquote($2);3719}else{3720$to_name=undef;3721}37223723# check if current patch belong to current raw line3724# and parse raw git-diff line if needed3725if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3726# this is continuation of a split patch3727print"<div class=\"patch cont\">\n";3728}else{3729# advance raw git-diff output if needed3730$patch_idx++ifdefined$diffinfo;37313732# read and prepare patch information3733$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);37343735# compact combined diff output can have some patches skipped3736# find which patch (using pathname of result) we are at now;3737if($is_combined) {3738while($to_namene$diffinfo->{'to_file'}) {3739print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3740 format_diff_cc_simplified($diffinfo,@hash_parents) .3741"</div>\n";# class="patch"37423743$patch_idx++;3744$patch_number++;37453746last if$patch_idx>$#$difftree;3747$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3748}3749}37503751# modifies %from, %to hashes3752 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);37533754# this is first patch for raw difftree line with $patch_idx index3755# we index @$difftree array from 0, but number patches from 13756print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3757}37583759# git diff header3760#assert($patch_line =~ m/^diff /) if DEBUG;3761#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3762$patch_number++;3763# print "git diff" header3764print format_git_diff_header_line($patch_line,$diffinfo,3765 \%from, \%to);37663767# print extended diff header3768print"<div class=\"diff extended_header\">\n";3769 EXTENDED_HEADER:3770while($patch_line= <$fd>) {3771chomp$patch_line;37723773last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);37743775print format_extended_diff_header_line($patch_line,$diffinfo,3776 \%from, \%to);3777}3778print"</div>\n";# class="diff extended_header"37793780# from-file/to-file diff header3781if(!$patch_line) {3782print"</div>\n";# class="patch"3783last PATCH;3784}3785next PATCH if($patch_line=~m/^diff /);3786#assert($patch_line =~ m/^---/) if DEBUG;37873788my$last_patch_line=$patch_line;3789$patch_line= <$fd>;3790chomp$patch_line;3791#assert($patch_line =~ m/^\+\+\+/) if DEBUG;37923793print format_diff_from_to_header($last_patch_line,$patch_line,3794$diffinfo, \%from, \%to,3795@hash_parents);37963797# the patch itself3798 LINE:3799while($patch_line= <$fd>) {3800chomp$patch_line;38013802next PATCH if($patch_line=~m/^diff /);38033804print format_diff_line($patch_line, \%from, \%to);3805}38063807}continue{3808print"</div>\n";# class="patch"3809}38103811# for compact combined (--cc) format, with chunk and patch simpliciaction3812# patchset might be empty, but there might be unprocessed raw lines3813for(++$patch_idxif$patch_number>0;3814$patch_idx<@$difftree;3815++$patch_idx) {3816# read and prepare patch information3817$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38183819# generate anchor for "patch" links in difftree / whatchanged part3820print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3821 format_diff_cc_simplified($diffinfo,@hash_parents) .3822"</div>\n";# class="patch"38233824$patch_number++;3825}38263827if($patch_number==0) {3828if(@hash_parents>1) {3829print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3830}else{3831print"<div class=\"diff nodifferences\">No differences found</div>\n";3832}3833}38343835print"</div>\n";# class="patchset"3836}38373838# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38393840# fills project list info (age, description, owner, forks) for each3841# project in the list, removing invalid projects from returned list3842# NOTE: modifies $projlist, but does not remove entries from it3843sub fill_project_list_info {3844my($projlist,$check_forks) =@_;3845my@projects;38463847my$show_ctags= gitweb_check_feature('ctags');3848 PROJECT:3849foreachmy$pr(@$projlist) {3850my(@activity) = git_get_last_activity($pr->{'path'});3851unless(@activity) {3852next PROJECT;3853}3854($pr->{'age'},$pr->{'age_string'}) =@activity;3855if(!defined$pr->{'descr'}) {3856my$descr= git_get_project_description($pr->{'path'}) ||"";3857$descr= to_utf8($descr);3858$pr->{'descr_long'} =$descr;3859$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3860}3861if(!defined$pr->{'owner'}) {3862$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3863}3864if($check_forks) {3865my$pname=$pr->{'path'};3866if(($pname=~s/\.git$//) &&3867($pname!~/\/$/) &&3868(-d "$projectroot/$pname")) {3869$pr->{'forks'} ="-d$projectroot/$pname";3870}else{3871$pr->{'forks'} =0;3872}3873}3874$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3875push@projects,$pr;3876}38773878return@projects;3879}38803881# print 'sort by' <th> element, generating 'sort by $name' replay link3882# if that order is not selected3883sub print_sort_th {3884my($name,$order,$header) =@_;3885$header||=ucfirst($name);38863887if($ordereq$name) {3888print"<th>$header</th>\n";3889}else{3890print"<th>".3891$cgi->a({-href => href(-replay=>1, order=>$name),3892-class=>"header"},$header) .3893"</th>\n";3894}3895}38963897sub git_project_list_body {3898# actually uses global variable $project3899my($projlist,$order,$from,$to,$extra,$no_header) =@_;39003901my($check_forks) = gitweb_check_feature('forks');3902my@projects= fill_project_list_info($projlist,$check_forks);39033904$order||=$default_projects_order;3905$from=0unlessdefined$from;3906$to=$#projectsif(!defined$to||$#projects<$to);39073908my%order_info= (3909 project => { key =>'path', type =>'str'},3910 descr => { key =>'descr_long', type =>'str'},3911 owner => { key =>'owner', type =>'str'},3912 age => { key =>'age', type =>'num'}3913);3914my$oi=$order_info{$order};3915if($oi->{'type'}eq'str') {3916@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;3917}else{3918@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;3919}39203921my$show_ctags= gitweb_check_feature('ctags');3922if($show_ctags) {3923my%ctags;3924foreachmy$p(@projects) {3925foreachmy$ct(keys%{$p->{'ctags'}}) {3926$ctags{$ct} +=$p->{'ctags'}->{$ct};3927}3928}3929my$cloud= git_populate_project_tagcloud(\%ctags);3930print git_show_project_tagcloud($cloud,64);3931}39323933print"<table class=\"project_list\">\n";3934unless($no_header) {3935print"<tr>\n";3936if($check_forks) {3937print"<th></th>\n";3938}3939 print_sort_th('project',$order,'Project');3940 print_sort_th('descr',$order,'Description');3941 print_sort_th('owner',$order,'Owner');3942 print_sort_th('age',$order,'Last Change');3943print"<th></th>\n".# for links3944"</tr>\n";3945}3946my$alternate=1;3947my$tagfilter=$cgi->param('by_tag');3948for(my$i=$from;$i<=$to;$i++) {3949my$pr=$projects[$i];39503951next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};3952next if$searchtextand not$pr->{'path'} =~/$searchtext/3953and not$pr->{'descr_long'} =~/$searchtext/;3954# Weed out forks or non-matching entries of search3955if($check_forks) {3956my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;3957$forkbase="^$forkbase"if$forkbase;3958next ifnot$searchtextand not$tagfilterand$show_ctags3959and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe3960}39613962if($alternate) {3963print"<tr class=\"dark\">\n";3964}else{3965print"<tr class=\"light\">\n";3966}3967$alternate^=1;3968if($check_forks) {3969print"<td>";3970if($pr->{'forks'}) {3971print"<!--$pr->{'forks'} -->\n";3972print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");3973}3974print"</td>\n";3975}3976print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3977-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".3978"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3979-class=>"list", -title =>$pr->{'descr_long'}},3980 esc_html($pr->{'descr'})) ."</td>\n".3981"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";3982print"<td class=\"". age_class($pr->{'age'}) ."\">".3983(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".3984"<td class=\"link\">".3985$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".3986$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".3987$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".3988$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .3989($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .3990"</td>\n".3991"</tr>\n";3992}3993if(defined$extra) {3994print"<tr>\n";3995if($check_forks) {3996print"<td></td>\n";3997}3998print"<td colspan=\"5\">$extra</td>\n".3999"</tr>\n";4000}4001print"</table>\n";4002}40034004sub git_shortlog_body {4005# uses global variable $project4006my($commitlist,$from,$to,$refs,$extra) =@_;40074008$from=0unlessdefined$from;4009$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);40104011print"<table class=\"shortlog\">\n";4012my$alternate=1;4013for(my$i=$from;$i<=$to;$i++) {4014my%co= %{$commitlist->[$i]};4015my$commit=$co{'id'};4016my$ref= format_ref_marker($refs,$commit);4017if($alternate) {4018print"<tr class=\"dark\">\n";4019}else{4020print"<tr class=\"light\">\n";4021}4022$alternate^=1;4023my$author= chop_and_escape_str($co{'author_name'},10);4024# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4025print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4026"<td><i>".$author."</i></td>\n".4027"<td>";4028print format_subject_html($co{'title'},$co{'title_short'},4029 href(action=>"commit", hash=>$commit),$ref);4030print"</td>\n".4031"<td class=\"link\">".4032$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4033$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4034$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4035my$snapshot_links= format_snapshot_links($commit);4036if(defined$snapshot_links) {4037print" | ".$snapshot_links;4038}4039print"</td>\n".4040"</tr>\n";4041}4042if(defined$extra) {4043print"<tr>\n".4044"<td colspan=\"4\">$extra</td>\n".4045"</tr>\n";4046}4047print"</table>\n";4048}40494050sub git_history_body {4051# Warning: assumes constant type (blob or tree) during history4052my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;40534054$from=0unlessdefined$from;4055$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});40564057print"<table class=\"history\">\n";4058my$alternate=1;4059for(my$i=$from;$i<=$to;$i++) {4060my%co= %{$commitlist->[$i]};4061if(!%co) {4062next;4063}4064my$commit=$co{'id'};40654066my$ref= format_ref_marker($refs,$commit);40674068if($alternate) {4069print"<tr class=\"dark\">\n";4070}else{4071print"<tr class=\"light\">\n";4072}4073$alternate^=1;4074# shortlog uses chop_str($co{'author_name'}, 10)4075my$author= chop_and_escape_str($co{'author_name'},15,3);4076print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4077"<td><i>".$author."</i></td>\n".4078"<td>";4079# originally git_history used chop_str($co{'title'}, 50)4080print format_subject_html($co{'title'},$co{'title_short'},4081 href(action=>"commit", hash=>$commit),$ref);4082print"</td>\n".4083"<td class=\"link\">".4084$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4085$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");40864087if($ftypeeq'blob') {4088my$blob_current= git_get_hash_by_path($hash_base,$file_name);4089my$blob_parent= git_get_hash_by_path($commit,$file_name);4090if(defined$blob_current&&defined$blob_parent&&4091$blob_currentne$blob_parent) {4092print" | ".4093$cgi->a({-href => href(action=>"blobdiff",4094 hash=>$blob_current, hash_parent=>$blob_parent,4095 hash_base=>$hash_base, hash_parent_base=>$commit,4096 file_name=>$file_name)},4097"diff to current");4098}4099}4100print"</td>\n".4101"</tr>\n";4102}4103if(defined$extra) {4104print"<tr>\n".4105"<td colspan=\"4\">$extra</td>\n".4106"</tr>\n";4107}4108print"</table>\n";4109}41104111sub git_tags_body {4112# uses global variable $project4113my($taglist,$from,$to,$extra) =@_;4114$from=0unlessdefined$from;4115$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);41164117print"<table class=\"tags\">\n";4118my$alternate=1;4119for(my$i=$from;$i<=$to;$i++) {4120my$entry=$taglist->[$i];4121my%tag=%$entry;4122my$comment=$tag{'subject'};4123my$comment_short;4124if(defined$comment) {4125$comment_short= chop_str($comment,30,5);4126}4127if($alternate) {4128print"<tr class=\"dark\">\n";4129}else{4130print"<tr class=\"light\">\n";4131}4132$alternate^=1;4133if(defined$tag{'age'}) {4134print"<td><i>$tag{'age'}</i></td>\n";4135}else{4136print"<td></td>\n";4137}4138print"<td>".4139$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4140-class=>"list name"}, esc_html($tag{'name'})) .4141"</td>\n".4142"<td>";4143if(defined$comment) {4144print format_subject_html($comment,$comment_short,4145 href(action=>"tag", hash=>$tag{'id'}));4146}4147print"</td>\n".4148"<td class=\"selflink\">";4149if($tag{'type'}eq"tag") {4150print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4151}else{4152print" ";4153}4154print"</td>\n".4155"<td class=\"link\">"." | ".4156$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4157if($tag{'reftype'}eq"commit") {4158print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4159" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4160}elsif($tag{'reftype'}eq"blob") {4161print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4162}4163print"</td>\n".4164"</tr>";4165}4166if(defined$extra) {4167print"<tr>\n".4168"<td colspan=\"5\">$extra</td>\n".4169"</tr>\n";4170}4171print"</table>\n";4172}41734174sub git_heads_body {4175# uses global variable $project4176my($headlist,$head,$from,$to,$extra) =@_;4177$from=0unlessdefined$from;4178$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);41794180print"<table class=\"heads\">\n";4181my$alternate=1;4182for(my$i=$from;$i<=$to;$i++) {4183my$entry=$headlist->[$i];4184my%ref=%$entry;4185my$curr=$ref{'id'}eq$head;4186if($alternate) {4187print"<tr class=\"dark\">\n";4188}else{4189print"<tr class=\"light\">\n";4190}4191$alternate^=1;4192print"<td><i>$ref{'age'}</i></td>\n".4193($curr?"<td class=\"current_head\">":"<td>") .4194$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4195-class=>"list name"},esc_html($ref{'name'})) .4196"</td>\n".4197"<td class=\"link\">".4198$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4199$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4200$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4201"</td>\n".4202"</tr>";4203}4204if(defined$extra) {4205print"<tr>\n".4206"<td colspan=\"3\">$extra</td>\n".4207"</tr>\n";4208}4209print"</table>\n";4210}42114212sub git_search_grep_body {4213my($commitlist,$from,$to,$extra) =@_;4214$from=0unlessdefined$from;4215$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);42164217print"<table class=\"commit_search\">\n";4218my$alternate=1;4219for(my$i=$from;$i<=$to;$i++) {4220my%co= %{$commitlist->[$i]};4221if(!%co) {4222next;4223}4224my$commit=$co{'id'};4225if($alternate) {4226print"<tr class=\"dark\">\n";4227}else{4228print"<tr class=\"light\">\n";4229}4230$alternate^=1;4231my$author= chop_and_escape_str($co{'author_name'},15,5);4232print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4233"<td><i>".$author."</i></td>\n".4234"<td>".4235$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4236-class=>"list subject"},4237 chop_and_escape_str($co{'title'},50) ."<br/>");4238my$comment=$co{'comment'};4239foreachmy$line(@$comment) {4240if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4241my($lead,$match,$trail) = ($1,$2,$3);4242$match= chop_str($match,70,5,'center');4243my$contextlen=int((80-length($match))/2);4244$contextlen=30if($contextlen>30);4245$lead= chop_str($lead,$contextlen,10,'left');4246$trail= chop_str($trail,$contextlen,10,'right');42474248$lead= esc_html($lead);4249$match= esc_html($match);4250$trail= esc_html($trail);42514252print"$lead<span class=\"match\">$match</span>$trail<br />";4253}4254}4255print"</td>\n".4256"<td class=\"link\">".4257$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4258" | ".4259$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4260" | ".4261$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4262print"</td>\n".4263"</tr>\n";4264}4265if(defined$extra) {4266print"<tr>\n".4267"<td colspan=\"3\">$extra</td>\n".4268"</tr>\n";4269}4270print"</table>\n";4271}42724273## ======================================================================4274## ======================================================================4275## actions42764277sub git_project_list {4278my$order=$input_params{'order'};4279if(defined$order&&$order!~m/none|project|descr|owner|age/) {4280 die_error(400,"Unknown order parameter");4281}42824283my@list= git_get_projects_list();4284if(!@list) {4285 die_error(404,"No projects found");4286}42874288 git_header_html();4289if(-f $home_text) {4290print"<div class=\"index_include\">\n";4291open(my$fd,$home_text);4292print<$fd>;4293close$fd;4294print"</div>\n";4295}4296print$cgi->startform(-method=>"get") .4297"<p class=\"projsearch\">Search:\n".4298$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4299"</p>".4300$cgi->end_form() ."\n";4301 git_project_list_body(\@list,$order);4302 git_footer_html();4303}43044305sub git_forks {4306my$order=$input_params{'order'};4307if(defined$order&&$order!~m/none|project|descr|owner|age/) {4308 die_error(400,"Unknown order parameter");4309}43104311my@list= git_get_projects_list($project);4312if(!@list) {4313 die_error(404,"No forks found");4314}43154316 git_header_html();4317 git_print_page_nav('','');4318 git_print_header_div('summary',"$projectforks");4319 git_project_list_body(\@list,$order);4320 git_footer_html();4321}43224323sub git_project_index {4324my@projects= git_get_projects_list($project);43254326print$cgi->header(4327-type =>'text/plain',4328-charset =>'utf-8',4329-content_disposition =>'inline; filename="index.aux"');43304331foreachmy$pr(@projects) {4332if(!exists$pr->{'owner'}) {4333$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4334}43354336my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4337# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4338$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4339$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4340$path=~s/ /\+/g;4341$owner=~s/ /\+/g;43424343print"$path$owner\n";4344}4345}43464347sub git_summary {4348my$descr= git_get_project_description($project) ||"none";4349my%co= parse_commit("HEAD");4350my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4351my$head=$co{'id'};43524353my$owner= git_get_project_owner($project);43544355my$refs= git_get_references();4356# These get_*_list functions return one more to allow us to see if4357# there are more ...4358my@taglist= git_get_tags_list(16);4359my@headlist= git_get_heads_list(16);4360my@forklist;4361my($check_forks) = gitweb_check_feature('forks');43624363if($check_forks) {4364@forklist= git_get_projects_list($project);4365}43664367 git_header_html();4368 git_print_page_nav('summary','',$head);43694370print"<div class=\"title\"> </div>\n";4371print"<table class=\"projects_list\">\n".4372"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4373"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4374if(defined$cd{'rfc2822'}) {4375print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4376}43774378# use per project git URL list in $projectroot/$project/cloneurl4379# or make project git URL from git base URL and project name4380my$url_tag="URL";4381my@url_list= git_get_project_url_list($project);4382@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4383foreachmy$git_url(@url_list) {4384next unless$git_url;4385print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4386$url_tag="";4387}43884389# Tag cloud4390my$show_ctags= (gitweb_check_feature('ctags'))[0];4391if($show_ctags) {4392my$ctags= git_get_project_ctags($project);4393my$cloud= git_populate_project_tagcloud($ctags);4394print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4395print"</td>\n<td>"unless%$ctags;4396print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4397print"</td>\n<td>"if%$ctags;4398print git_show_project_tagcloud($cloud,48);4399print"</td></tr>";4400}44014402print"</table>\n";44034404if(-s "$projectroot/$project/README.html") {4405if(open my$fd,"$projectroot/$project/README.html") {4406print"<div class=\"title\">readme</div>\n".4407"<div class=\"readme\">\n";4408print$_while(<$fd>);4409print"\n</div>\n";# class="readme"4410close$fd;4411}4412}44134414# we need to request one more than 16 (0..15) to check if4415# those 16 are all4416my@commitlist=$head? parse_commits($head,17) : ();4417if(@commitlist) {4418 git_print_header_div('shortlog');4419 git_shortlog_body(\@commitlist,0,15,$refs,4420$#commitlist<=15?undef:4421$cgi->a({-href => href(action=>"shortlog")},"..."));4422}44234424if(@taglist) {4425 git_print_header_div('tags');4426 git_tags_body(\@taglist,0,15,4427$#taglist<=15?undef:4428$cgi->a({-href => href(action=>"tags")},"..."));4429}44304431if(@headlist) {4432 git_print_header_div('heads');4433 git_heads_body(\@headlist,$head,0,15,4434$#headlist<=15?undef:4435$cgi->a({-href => href(action=>"heads")},"..."));4436}44374438if(@forklist) {4439 git_print_header_div('forks');4440 git_project_list_body(\@forklist,'age',0,15,4441$#forklist<=15?undef:4442$cgi->a({-href => href(action=>"forks")},"..."),4443'no_header');4444}44454446 git_footer_html();4447}44484449sub git_tag {4450my$head= git_get_head_hash($project);4451 git_header_html();4452 git_print_page_nav('','',$head,undef,$head);4453my%tag= parse_tag($hash);44544455if(!%tag) {4456 die_error(404,"Unknown tag object");4457}44584459 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4460print"<div class=\"title_text\">\n".4461"<table class=\"object_header\">\n".4462"<tr>\n".4463"<td>object</td>\n".4464"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4465$tag{'object'}) ."</td>\n".4466"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4467$tag{'type'}) ."</td>\n".4468"</tr>\n";4469if(defined($tag{'author'})) {4470my%ad= parse_date($tag{'epoch'},$tag{'tz'});4471print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4472print"<tr><td></td><td>".$ad{'rfc2822'} .4473sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4474"</td></tr>\n";4475}4476print"</table>\n\n".4477"</div>\n";4478print"<div class=\"page_body\">";4479my$comment=$tag{'comment'};4480foreachmy$line(@$comment) {4481chomp$line;4482print esc_html($line, -nbsp=>1) ."<br/>\n";4483}4484print"</div>\n";4485 git_footer_html();4486}44874488sub git_blame {4489my$fd;4490my$ftype;44914492 gitweb_check_feature('blame')4493or die_error(403,"Blame view not allowed");44944495 die_error(400,"No file name given")unless$file_name;4496$hash_base||= git_get_head_hash($project);4497 die_error(404,"Couldn't find base commit")unless($hash_base);4498my%co= parse_commit($hash_base)4499or die_error(404,"Commit not found");4500if(!defined$hash) {4501$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4502or die_error(404,"Error looking up file");4503}4504$ftype= git_get_type($hash);4505if($ftype!~"blob") {4506 die_error(400,"Object is not a blob");4507}4508open($fd,"-|", git_cmd(),"blame",'-p','--',4509$file_name,$hash_base)4510or die_error(500,"Open git-blame failed");4511 git_header_html();4512my$formats_nav=4513$cgi->a({-href => href(action=>"blob", -replay=>1)},4514"blob") .4515" | ".4516$cgi->a({-href => href(action=>"history", -replay=>1)},4517"history") .4518" | ".4519$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4520"HEAD");4521 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4522 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4523 git_print_page_path($file_name,$ftype,$hash_base);4524my@rev_color= (qw(light2 dark2));4525my$num_colors=scalar(@rev_color);4526my$current_color=0;4527my$last_rev;4528print<<HTML;4529<div class="page_body">4530<table class="blame">4531<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4532HTML4533my%metainfo= ();4534while(1) {4535$_= <$fd>;4536last unlessdefined$_;4537my($full_rev,$orig_lineno,$lineno,$group_size) =4538/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;4539if(!exists$metainfo{$full_rev}) {4540$metainfo{$full_rev} = {};4541}4542my$meta=$metainfo{$full_rev};4543while(<$fd>) {4544last if(s/^\t//);4545if(/^(\S+) (.*)$/) {4546$meta->{$1} =$2;4547}4548}4549my$data=$_;4550chomp$data;4551my$rev=substr($full_rev,0,8);4552my$author=$meta->{'author'};4553my%date= parse_date($meta->{'author-time'},4554$meta->{'author-tz'});4555my$date=$date{'iso-tz'};4556if($group_size) {4557$current_color= ++$current_color%$num_colors;4558}4559print"<tr class=\"$rev_color[$current_color]\">\n";4560if($group_size) {4561print"<td class=\"sha1\"";4562print" title=\"". esc_html($author) .",$date\"";4563print" rowspan=\"$group_size\""if($group_size>1);4564print">";4565print$cgi->a({-href => href(action=>"commit",4566 hash=>$full_rev,4567 file_name=>$file_name)},4568 esc_html($rev));4569print"</td>\n";4570}4571open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4572or die_error(500,"Open git-rev-parse failed");4573my$parent_commit= <$dd>;4574close$dd;4575chomp($parent_commit);4576my$blamed= href(action =>'blame',4577 file_name =>$meta->{'filename'},4578 hash_base =>$parent_commit);4579print"<td class=\"linenr\">";4580print$cgi->a({ -href =>"$blamed#l$orig_lineno",4581-id =>"l$lineno",4582-class=>"linenr"},4583 esc_html($lineno));4584print"</td>";4585print"<td class=\"pre\">". esc_html($data) ."</td>\n";4586print"</tr>\n";4587}4588print"</table>\n";4589print"</div>";4590close$fd4591or print"Reading blob failed\n";4592 git_footer_html();4593}45944595sub git_tags {4596my$head= git_get_head_hash($project);4597 git_header_html();4598 git_print_page_nav('','',$head,undef,$head);4599 git_print_header_div('summary',$project);46004601my@tagslist= git_get_tags_list();4602if(@tagslist) {4603 git_tags_body(\@tagslist);4604}4605 git_footer_html();4606}46074608sub git_heads {4609my$head= git_get_head_hash($project);4610 git_header_html();4611 git_print_page_nav('','',$head,undef,$head);4612 git_print_header_div('summary',$project);46134614my@headslist= git_get_heads_list();4615if(@headslist) {4616 git_heads_body(\@headslist,$head);4617}4618 git_footer_html();4619}46204621sub git_blob_plain {4622my$type=shift;4623my$expires;46244625if(!defined$hash) {4626if(defined$file_name) {4627my$base=$hash_base|| git_get_head_hash($project);4628$hash= git_get_hash_by_path($base,$file_name,"blob")4629or die_error(404,"Cannot find file");4630}else{4631 die_error(400,"No file name defined");4632}4633}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4634# blobs defined by non-textual hash id's can be cached4635$expires="+1d";4636}46374638open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4639or die_error(500,"Open git-cat-file blob '$hash' failed");46404641# content-type (can include charset)4642$type= blob_contenttype($fd,$file_name,$type);46434644# "save as" filename, even when no $file_name is given4645my$save_as="$hash";4646if(defined$file_name) {4647$save_as=$file_name;4648}elsif($type=~m/^text\//) {4649$save_as.='.txt';4650}46514652print$cgi->header(4653-type =>$type,4654-expires =>$expires,4655-content_disposition =>'inline; filename="'.$save_as.'"');4656undef$/;4657binmode STDOUT,':raw';4658print<$fd>;4659binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4660$/="\n";4661close$fd;4662}46634664sub git_blob {4665my$expires;46664667if(!defined$hash) {4668if(defined$file_name) {4669my$base=$hash_base|| git_get_head_hash($project);4670$hash= git_get_hash_by_path($base,$file_name,"blob")4671or die_error(404,"Cannot find file");4672}else{4673 die_error(400,"No file name defined");4674}4675}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4676# blobs defined by non-textual hash id's can be cached4677$expires="+1d";4678}46794680my($have_blame) = gitweb_check_feature('blame');4681open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4682or die_error(500,"Couldn't cat$file_name,$hash");4683my$mimetype= blob_mimetype($fd,$file_name);4684if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4685close$fd;4686return git_blob_plain($mimetype);4687}4688# we can have blame only for text/* mimetype4689$have_blame&&= ($mimetype=~m!^text/!);46904691 git_header_html(undef,$expires);4692my$formats_nav='';4693if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4694if(defined$file_name) {4695if($have_blame) {4696$formats_nav.=4697$cgi->a({-href => href(action=>"blame", -replay=>1)},4698"blame") .4699" | ";4700}4701$formats_nav.=4702$cgi->a({-href => href(action=>"history", -replay=>1)},4703"history") .4704" | ".4705$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4706"raw") .4707" | ".4708$cgi->a({-href => href(action=>"blob",4709 hash_base=>"HEAD", file_name=>$file_name)},4710"HEAD");4711}else{4712$formats_nav.=4713$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4714"raw");4715}4716 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4717 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4718}else{4719print"<div class=\"page_nav\">\n".4720"<br/><br/></div>\n".4721"<div class=\"title\">$hash</div>\n";4722}4723 git_print_page_path($file_name,"blob",$hash_base);4724print"<div class=\"page_body\">\n";4725if($mimetype=~m!^image/!) {4726print qq!<img type="$mimetype"!;4727if($file_name) {4728print qq! alt="$file_name" title="$file_name"!;4729}4730print qq! src="! .4731 href(action=>"blob_plain", hash=>$hash,4732 hash_base=>$hash_base, file_name=>$file_name) .4733 qq!"/>\n!;4734}else{4735my$nr;4736while(my$line= <$fd>) {4737chomp$line;4738$nr++;4739$line= untabify($line);4740printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4741$nr,$nr,$nr, esc_html($line, -nbsp=>1);4742}4743}4744close$fd4745or print"Reading blob failed.\n";4746print"</div>";4747 git_footer_html();4748}47494750sub git_tree {4751if(!defined$hash_base) {4752$hash_base="HEAD";4753}4754if(!defined$hash) {4755if(defined$file_name) {4756$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4757}else{4758$hash=$hash_base;4759}4760}4761 die_error(404,"No such tree")unlessdefined($hash);4762$/="\0";4763open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4764or die_error(500,"Open git-ls-tree failed");4765my@entries=map{chomp;$_} <$fd>;4766close$fdor die_error(404,"Reading tree failed");4767$/="\n";47684769my$refs= git_get_references();4770my$ref= format_ref_marker($refs,$hash_base);4771 git_header_html();4772my$basedir='';4773my($have_blame) = gitweb_check_feature('blame');4774if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4775my@views_nav= ();4776if(defined$file_name) {4777push@views_nav,4778$cgi->a({-href => href(action=>"history", -replay=>1)},4779"history"),4780$cgi->a({-href => href(action=>"tree",4781 hash_base=>"HEAD", file_name=>$file_name)},4782"HEAD"),4783}4784my$snapshot_links= format_snapshot_links($hash);4785if(defined$snapshot_links) {4786# FIXME: Should be available when we have no hash base as well.4787push@views_nav,$snapshot_links;4788}4789 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4790 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4791}else{4792undef$hash_base;4793print"<div class=\"page_nav\">\n";4794print"<br/><br/></div>\n";4795print"<div class=\"title\">$hash</div>\n";4796}4797if(defined$file_name) {4798$basedir=$file_name;4799if($basedirne''&&substr($basedir, -1)ne'/') {4800$basedir.='/';4801}4802 git_print_page_path($file_name,'tree',$hash_base);4803}4804print"<div class=\"page_body\">\n";4805print"<table class=\"tree\">\n";4806my$alternate=1;4807# '..' (top directory) link if possible4808if(defined$hash_base&&4809defined$file_name&&$file_name=~m![^/]+$!) {4810if($alternate) {4811print"<tr class=\"dark\">\n";4812}else{4813print"<tr class=\"light\">\n";4814}4815$alternate^=1;48164817my$up=$file_name;4818$up=~s!/?[^/]+$!!;4819undef$upunless$up;4820# based on git_print_tree_entry4821print'<td class="mode">'. mode_str('040000') ."</td>\n";4822print'<td class="list">';4823print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4824 file_name=>$up)},4825"..");4826print"</td>\n";4827print"<td class=\"link\"></td>\n";48284829print"</tr>\n";4830}4831foreachmy$line(@entries) {4832my%t= parse_ls_tree_line($line, -z =>1);48334834if($alternate) {4835print"<tr class=\"dark\">\n";4836}else{4837print"<tr class=\"light\">\n";4838}4839$alternate^=1;48404841 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);48424843print"</tr>\n";4844}4845print"</table>\n".4846"</div>";4847 git_footer_html();4848}48494850sub git_snapshot {4851my$format=$input_params{'snapshot_format'};4852if(!@snapshot_fmts) {4853 die_error(403,"Snapshots not allowed");4854}4855# default to first supported snapshot format4856$format||=$snapshot_fmts[0];4857if($format!~m/^[a-z0-9]+$/) {4858 die_error(400,"Invalid snapshot format parameter");4859}elsif(!exists($known_snapshot_formats{$format})) {4860 die_error(400,"Unknown snapshot format");4861}elsif(!grep($_eq$format,@snapshot_fmts)) {4862 die_error(403,"Unsupported snapshot format");4863}48644865if(!defined$hash) {4866$hash= git_get_head_hash($project);4867}48684869my$name=$project;4870$name=~ s,([^/])/*\.git$,$1,;4871$name= basename($name);4872my$filename= to_utf8($name);4873$name=~s/\047/\047\\\047\047/g;4874my$cmd;4875$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";4876$cmd= quote_command(4877 git_cmd(),'archive',4878"--format=$known_snapshot_formats{$format}{'format'}",4879"--prefix=$name/",$hash);4880if(exists$known_snapshot_formats{$format}{'compressor'}) {4881$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});4882}48834884print$cgi->header(4885-type =>$known_snapshot_formats{$format}{'type'},4886-content_disposition =>'inline; filename="'."$filename".'"',4887-status =>'200 OK');48884889open my$fd,"-|",$cmd4890or die_error(500,"Execute git-archive failed");4891binmode STDOUT,':raw';4892print<$fd>;4893binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4894close$fd;4895}48964897sub git_log {4898my$head= git_get_head_hash($project);4899if(!defined$hash) {4900$hash=$head;4901}4902if(!defined$page) {4903$page=0;4904}4905my$refs= git_get_references();49064907my@commitlist= parse_commits($hash,101, (100*$page));49084909my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);49104911 git_header_html();4912 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);49134914if(!@commitlist) {4915my%co= parse_commit($hash);49164917 git_print_header_div('summary',$project);4918print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";4919}4920my$to= ($#commitlist>=99) ? (99) : ($#commitlist);4921for(my$i=0;$i<=$to;$i++) {4922my%co= %{$commitlist[$i]};4923next if!%co;4924my$commit=$co{'id'};4925my$ref= format_ref_marker($refs,$commit);4926my%ad= parse_date($co{'author_epoch'});4927 git_print_header_div('commit',4928"<span class=\"age\">$co{'age_string'}</span>".4929 esc_html($co{'title'}) .$ref,4930$commit);4931print"<div class=\"title_text\">\n".4932"<div class=\"log_link\">\n".4933$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4934" | ".4935$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4936" | ".4937$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4938"<br/>\n".4939"</div>\n".4940"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".4941"</div>\n";49424943print"<div class=\"log_body\">\n";4944 git_print_log($co{'comment'}, -final_empty_line=>1);4945print"</div>\n";4946}4947if($#commitlist>=100) {4948print"<div class=\"page_nav\">\n";4949print$cgi->a({-href => href(-replay=>1, page=>$page+1),4950-accesskey =>"n", -title =>"Alt-n"},"next");4951print"</div>\n";4952}4953 git_footer_html();4954}49554956sub git_commit {4957$hash||=$hash_base||"HEAD";4958my%co= parse_commit($hash)4959or die_error(404,"Unknown commit object");4960my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});4961my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});49624963my$parent=$co{'parent'};4964my$parents=$co{'parents'};# listref49654966# we need to prepare $formats_nav before any parameter munging4967my$formats_nav;4968if(!defined$parent) {4969# --root commitdiff4970$formats_nav.='(initial)';4971}elsif(@$parents==1) {4972# single parent commit4973$formats_nav.=4974'(parent: '.4975$cgi->a({-href => href(action=>"commit",4976 hash=>$parent)},4977 esc_html(substr($parent,0,7))) .4978')';4979}else{4980# merge commit4981$formats_nav.=4982'(merge: '.4983join(' ',map{4984$cgi->a({-href => href(action=>"commit",4985 hash=>$_)},4986 esc_html(substr($_,0,7)));4987}@$parents) .4988')';4989}49904991if(!defined$parent) {4992$parent="--root";4993}4994my@difftree;4995open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",4996@diff_opts,4997(@$parents<=1?$parent:'-c'),4998$hash,"--"4999or die_error(500,"Open git-diff-tree failed");5000@difftree=map{chomp;$_} <$fd>;5001close$fdor die_error(404,"Reading git-diff-tree failed");50025003# non-textual hash id's can be cached5004my$expires;5005if($hash=~m/^[0-9a-fA-F]{40}$/) {5006$expires="+1d";5007}5008my$refs= git_get_references();5009my$ref= format_ref_marker($refs,$co{'id'});50105011 git_header_html(undef,$expires);5012 git_print_page_nav('commit','',5013$hash,$co{'tree'},$hash,5014$formats_nav);50155016if(defined$co{'parent'}) {5017 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5018}else{5019 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5020}5021print"<div class=\"title_text\">\n".5022"<table class=\"object_header\">\n";5023print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5024"<tr>".5025"<td></td><td>$ad{'rfc2822'}";5026if($ad{'hour_local'} <6) {5027printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5028$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5029}else{5030printf(" (%02d:%02d%s)",5031$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5032}5033print"</td>".5034"</tr>\n";5035print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5036print"<tr><td></td><td>$cd{'rfc2822'}".5037sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5038"</td></tr>\n";5039print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5040print"<tr>".5041"<td>tree</td>".5042"<td class=\"sha1\">".5043$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5044class=>"list"},$co{'tree'}) .5045"</td>".5046"<td class=\"link\">".5047$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5048"tree");5049my$snapshot_links= format_snapshot_links($hash);5050if(defined$snapshot_links) {5051print" | ".$snapshot_links;5052}5053print"</td>".5054"</tr>\n";50555056foreachmy$par(@$parents) {5057print"<tr>".5058"<td>parent</td>".5059"<td class=\"sha1\">".5060$cgi->a({-href => href(action=>"commit", hash=>$par),5061class=>"list"},$par) .5062"</td>".5063"<td class=\"link\">".5064$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5065" | ".5066$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5067"</td>".5068"</tr>\n";5069}5070print"</table>".5071"</div>\n";50725073print"<div class=\"page_body\">\n";5074 git_print_log($co{'comment'});5075print"</div>\n";50765077 git_difftree_body(\@difftree,$hash,@$parents);50785079 git_footer_html();5080}50815082sub git_object {5083# object is defined by:5084# - hash or hash_base alone5085# - hash_base and file_name5086my$type;50875088# - hash or hash_base alone5089if($hash|| ($hash_base&& !defined$file_name)) {5090my$object_id=$hash||$hash_base;50915092open my$fd,"-|", quote_command(5093 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5094or die_error(404,"Object does not exist");5095$type= <$fd>;5096chomp$type;5097close$fd5098or die_error(404,"Object does not exist");50995100# - hash_base and file_name5101}elsif($hash_base&&defined$file_name) {5102$file_name=~ s,/+$,,;51035104system(git_cmd(),"cat-file",'-e',$hash_base) ==05105or die_error(404,"Base object does not exist");51065107# here errors should not hapen5108open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5109or die_error(500,"Open git-ls-tree failed");5110my$line= <$fd>;5111close$fd;51125113#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5114unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5115 die_error(404,"File or directory for given base does not exist");5116}5117$type=$2;5118$hash=$3;5119}else{5120 die_error(400,"Not enough information to find object");5121}51225123print$cgi->redirect(-uri => href(action=>$type, -full=>1,5124 hash=>$hash, hash_base=>$hash_base,5125 file_name=>$file_name),5126-status =>'302 Found');5127}51285129sub git_blobdiff {5130my$format=shift||'html';51315132my$fd;5133my@difftree;5134my%diffinfo;5135my$expires;51365137# preparing $fd and %diffinfo for git_patchset_body5138# new style URI5139if(defined$hash_base&&defined$hash_parent_base) {5140if(defined$file_name) {5141# read raw output5142open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5143$hash_parent_base,$hash_base,5144"--", (defined$file_parent?$file_parent: ()),$file_name5145or die_error(500,"Open git-diff-tree failed");5146@difftree=map{chomp;$_} <$fd>;5147close$fd5148or die_error(404,"Reading git-diff-tree failed");5149@difftree5150or die_error(404,"Blob diff not found");51515152}elsif(defined$hash&&5153$hash=~/[0-9a-fA-F]{40}/) {5154# try to find filename from $hash51555156# read filtered raw output5157open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5158$hash_parent_base,$hash_base,"--"5159or die_error(500,"Open git-diff-tree failed");5160@difftree=5161# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5162# $hash == to_id5163grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5164map{chomp;$_} <$fd>;5165close$fd5166or die_error(404,"Reading git-diff-tree failed");5167@difftree5168or die_error(404,"Blob diff not found");51695170}else{5171 die_error(400,"Missing one of the blob diff parameters");5172}51735174if(@difftree>1) {5175 die_error(400,"Ambiguous blob diff specification");5176}51775178%diffinfo= parse_difftree_raw_line($difftree[0]);5179$file_parent||=$diffinfo{'from_file'} ||$file_name;5180$file_name||=$diffinfo{'to_file'};51815182$hash_parent||=$diffinfo{'from_id'};5183$hash||=$diffinfo{'to_id'};51845185# non-textual hash id's can be cached5186if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5187$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5188$expires='+1d';5189}51905191# open patch output5192open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5193'-p', ($formateq'html'?"--full-index": ()),5194$hash_parent_base,$hash_base,5195"--", (defined$file_parent?$file_parent: ()),$file_name5196or die_error(500,"Open git-diff-tree failed");5197}51985199# old/legacy style URI5200if(!%diffinfo&&# if new style URI failed5201defined$hash&&defined$hash_parent) {5202# fake git-diff-tree raw output5203$diffinfo{'from_mode'} =$diffinfo{'to_mode'} ="blob";5204$diffinfo{'from_id'} =$hash_parent;5205$diffinfo{'to_id'} =$hash;5206if(defined$file_name) {5207if(defined$file_parent) {5208$diffinfo{'status'} ='2';5209$diffinfo{'from_file'} =$file_parent;5210$diffinfo{'to_file'} =$file_name;5211}else{# assume not renamed5212$diffinfo{'status'} ='1';5213$diffinfo{'from_file'} =$file_name;5214$diffinfo{'to_file'} =$file_name;5215}5216}else{# no filename given5217$diffinfo{'status'} ='2';5218$diffinfo{'from_file'} =$hash_parent;5219$diffinfo{'to_file'} =$hash;5220}52215222# non-textual hash id's can be cached5223if($hash=~m/^[0-9a-fA-F]{40}$/&&5224$hash_parent=~m/^[0-9a-fA-F]{40}$/) {5225$expires='+1d';5226}52275228# open patch output5229open$fd,"-|", git_cmd(),"diff",@diff_opts,5230'-p', ($formateq'html'?"--full-index": ()),5231$hash_parent,$hash,"--"5232or die_error(500,"Open git-diff failed");5233}else{5234 die_error(400,"Missing one of the blob diff parameters")5235unless%diffinfo;5236}52375238# header5239if($formateq'html') {5240my$formats_nav=5241$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5242"raw");5243 git_header_html(undef,$expires);5244if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5245 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5246 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5247}else{5248print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5249print"<div class=\"title\">$hashvs$hash_parent</div>\n";5250}5251if(defined$file_name) {5252 git_print_page_path($file_name,"blob",$hash_base);5253}else{5254print"<div class=\"page_path\"></div>\n";5255}52565257}elsif($formateq'plain') {5258print$cgi->header(5259-type =>'text/plain',5260-charset =>'utf-8',5261-expires =>$expires,5262-content_disposition =>'inline; filename="'."$file_name".'.patch"');52635264print"X-Git-Url: ".$cgi->self_url() ."\n\n";52655266}else{5267 die_error(400,"Unknown blobdiff format");5268}52695270# patch5271if($formateq'html') {5272print"<div class=\"page_body\">\n";52735274 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5275close$fd;52765277print"</div>\n";# class="page_body"5278 git_footer_html();52795280}else{5281while(my$line= <$fd>) {5282$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5283$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;52845285print$line;52865287last if$line=~m!^\+\+\+!;5288}5289local$/=undef;5290print<$fd>;5291close$fd;5292}5293}52945295sub git_blobdiff_plain {5296 git_blobdiff('plain');5297}52985299sub git_commitdiff {5300my$format=shift||'html';5301$hash||=$hash_base||"HEAD";5302my%co= parse_commit($hash)5303or die_error(404,"Unknown commit object");53045305# choose format for commitdiff for merge5306if(!defined$hash_parent&& @{$co{'parents'}} >1) {5307$hash_parent='--cc';5308}5309# we need to prepare $formats_nav before almost any parameter munging5310my$formats_nav;5311if($formateq'html') {5312$formats_nav=5313$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5314"raw");53155316if(defined$hash_parent&&5317$hash_parentne'-c'&&$hash_parentne'--cc') {5318# commitdiff with two commits given5319my$hash_parent_short=$hash_parent;5320if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5321$hash_parent_short=substr($hash_parent,0,7);5322}5323$formats_nav.=5324' (from';5325for(my$i=0;$i< @{$co{'parents'}};$i++) {5326if($co{'parents'}[$i]eq$hash_parent) {5327$formats_nav.=' parent '. ($i+1);5328last;5329}5330}5331$formats_nav.=': '.5332$cgi->a({-href => href(action=>"commitdiff",5333 hash=>$hash_parent)},5334 esc_html($hash_parent_short)) .5335')';5336}elsif(!$co{'parent'}) {5337# --root commitdiff5338$formats_nav.=' (initial)';5339}elsif(scalar@{$co{'parents'}} ==1) {5340# single parent commit5341$formats_nav.=5342' (parent: '.5343$cgi->a({-href => href(action=>"commitdiff",5344 hash=>$co{'parent'})},5345 esc_html(substr($co{'parent'},0,7))) .5346')';5347}else{5348# merge commit5349if($hash_parenteq'--cc') {5350$formats_nav.=' | '.5351$cgi->a({-href => href(action=>"commitdiff",5352 hash=>$hash, hash_parent=>'-c')},5353'combined');5354}else{# $hash_parent eq '-c'5355$formats_nav.=' | '.5356$cgi->a({-href => href(action=>"commitdiff",5357 hash=>$hash, hash_parent=>'--cc')},5358'compact');5359}5360$formats_nav.=5361' (merge: '.5362join(' ',map{5363$cgi->a({-href => href(action=>"commitdiff",5364 hash=>$_)},5365 esc_html(substr($_,0,7)));5366} @{$co{'parents'}} ) .5367')';5368}5369}53705371my$hash_parent_param=$hash_parent;5372if(!defined$hash_parent_param) {5373# --cc for multiple parents, --root for parentless5374$hash_parent_param=5375@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5376}53775378# read commitdiff5379my$fd;5380my@difftree;5381if($formateq'html') {5382open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5383"--no-commit-id","--patch-with-raw","--full-index",5384$hash_parent_param,$hash,"--"5385or die_error(500,"Open git-diff-tree failed");53865387while(my$line= <$fd>) {5388chomp$line;5389# empty line ends raw part of diff-tree output5390last unless$line;5391push@difftree,scalar parse_difftree_raw_line($line);5392}53935394}elsif($formateq'plain') {5395open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5396'-p',$hash_parent_param,$hash,"--"5397or die_error(500,"Open git-diff-tree failed");53985399}else{5400 die_error(400,"Unknown commitdiff format");5401}54025403# non-textual hash id's can be cached5404my$expires;5405if($hash=~m/^[0-9a-fA-F]{40}$/) {5406$expires="+1d";5407}54085409# write commit message5410if($formateq'html') {5411my$refs= git_get_references();5412my$ref= format_ref_marker($refs,$co{'id'});54135414 git_header_html(undef,$expires);5415 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5416 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5417 git_print_authorship(\%co);5418print"<div class=\"page_body\">\n";5419if(@{$co{'comment'}} >1) {5420print"<div class=\"log\">\n";5421 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5422print"</div>\n";# class="log"5423}54245425}elsif($formateq'plain') {5426my$refs= git_get_references("tags");5427my$tagname= git_get_rev_name_tags($hash);5428my$filename= basename($project) ."-$hash.patch";54295430print$cgi->header(5431-type =>'text/plain',5432-charset =>'utf-8',5433-expires =>$expires,5434-content_disposition =>'inline; filename="'."$filename".'"');5435my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5436print"From: ". to_utf8($co{'author'}) ."\n";5437print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5438print"Subject: ". to_utf8($co{'title'}) ."\n";54395440print"X-Git-Tag:$tagname\n"if$tagname;5441print"X-Git-Url: ".$cgi->self_url() ."\n\n";54425443foreachmy$line(@{$co{'comment'}}) {5444print to_utf8($line) ."\n";5445}5446print"---\n\n";5447}54485449# write patch5450if($formateq'html') {5451my$use_parents= !defined$hash_parent||5452$hash_parenteq'-c'||$hash_parenteq'--cc';5453 git_difftree_body(\@difftree,$hash,5454$use_parents? @{$co{'parents'}} :$hash_parent);5455print"<br/>\n";54565457 git_patchset_body($fd, \@difftree,$hash,5458$use_parents? @{$co{'parents'}} :$hash_parent);5459close$fd;5460print"</div>\n";# class="page_body"5461 git_footer_html();54625463}elsif($formateq'plain') {5464local$/=undef;5465print<$fd>;5466close$fd5467or print"Reading git-diff-tree failed\n";5468}5469}54705471sub git_commitdiff_plain {5472 git_commitdiff('plain');5473}54745475sub git_history {5476if(!defined$hash_base) {5477$hash_base= git_get_head_hash($project);5478}5479if(!defined$page) {5480$page=0;5481}5482my$ftype;5483my%co= parse_commit($hash_base)5484or die_error(404,"Unknown commit object");54855486my$refs= git_get_references();5487my$limit=sprintf("--max-count=%i", (100* ($page+1)));54885489my@commitlist= parse_commits($hash_base,101, (100*$page),5490$file_name,"--full-history")5491or die_error(404,"No such file or directory on given branch");54925493if(!defined$hash&&defined$file_name) {5494# some commits could have deleted file in question,5495# and not have it in tree, but one of them has to have it5496for(my$i=0;$i<=@commitlist;$i++) {5497$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5498last ifdefined$hash;5499}5500}5501if(defined$hash) {5502$ftype= git_get_type($hash);5503}5504if(!defined$ftype) {5505 die_error(500,"Unknown type of object");5506}55075508my$paging_nav='';5509if($page>0) {5510$paging_nav.=5511$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5512 file_name=>$file_name)},5513"first");5514$paging_nav.=" ⋅ ".5515$cgi->a({-href => href(-replay=>1, page=>$page-1),5516-accesskey =>"p", -title =>"Alt-p"},"prev");5517}else{5518$paging_nav.="first";5519$paging_nav.=" ⋅ prev";5520}5521my$next_link='';5522if($#commitlist>=100) {5523$next_link=5524$cgi->a({-href => href(-replay=>1, page=>$page+1),5525-accesskey =>"n", -title =>"Alt-n"},"next");5526$paging_nav.=" ⋅$next_link";5527}else{5528$paging_nav.=" ⋅ next";5529}55305531 git_header_html();5532 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5533 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5534 git_print_page_path($file_name,$ftype,$hash_base);55355536 git_history_body(\@commitlist,0,99,5537$refs,$hash_base,$ftype,$next_link);55385539 git_footer_html();5540}55415542sub git_search {5543 gitweb_check_feature('search')or die_error(403,"Search is disabled");5544if(!defined$searchtext) {5545 die_error(400,"Text field is empty");5546}5547if(!defined$hash) {5548$hash= git_get_head_hash($project);5549}5550my%co= parse_commit($hash);5551if(!%co) {5552 die_error(404,"Unknown commit object");5553}5554if(!defined$page) {5555$page=0;5556}55575558$searchtype||='commit';5559if($searchtypeeq'pickaxe') {5560# pickaxe may take all resources of your box and run for several minutes5561# with every query - so decide by yourself how public you make this feature5562 gitweb_check_feature('pickaxe')5563or die_error(403,"Pickaxe is disabled");5564}5565if($searchtypeeq'grep') {5566 gitweb_check_feature('grep')5567or die_error(403,"Grep is disabled");5568}55695570 git_header_html();55715572if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5573my$greptype;5574if($searchtypeeq'commit') {5575$greptype="--grep=";5576}elsif($searchtypeeq'author') {5577$greptype="--author=";5578}elsif($searchtypeeq'committer') {5579$greptype="--committer=";5580}5581$greptype.=$searchtext;5582my@commitlist= parse_commits($hash,101, (100*$page),undef,5583$greptype,'--regexp-ignore-case',5584$search_use_regexp?'--extended-regexp':'--fixed-strings');55855586my$paging_nav='';5587if($page>0) {5588$paging_nav.=5589$cgi->a({-href => href(action=>"search", hash=>$hash,5590 searchtext=>$searchtext,5591 searchtype=>$searchtype)},5592"first");5593$paging_nav.=" ⋅ ".5594$cgi->a({-href => href(-replay=>1, page=>$page-1),5595-accesskey =>"p", -title =>"Alt-p"},"prev");5596}else{5597$paging_nav.="first";5598$paging_nav.=" ⋅ prev";5599}5600my$next_link='';5601if($#commitlist>=100) {5602$next_link=5603$cgi->a({-href => href(-replay=>1, page=>$page+1),5604-accesskey =>"n", -title =>"Alt-n"},"next");5605$paging_nav.=" ⋅$next_link";5606}else{5607$paging_nav.=" ⋅ next";5608}56095610if($#commitlist>=100) {5611}56125613 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5614 git_print_header_div('commit', esc_html($co{'title'}),$hash);5615 git_search_grep_body(\@commitlist,0,99,$next_link);5616}56175618if($searchtypeeq'pickaxe') {5619 git_print_page_nav('','',$hash,$co{'tree'},$hash);5620 git_print_header_div('commit', esc_html($co{'title'}),$hash);56215622print"<table class=\"pickaxe search\">\n";5623my$alternate=1;5624$/="\n";5625open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5626'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5627($search_use_regexp?'--pickaxe-regex': ());5628undef%co;5629my@files;5630while(my$line= <$fd>) {5631chomp$line;5632next unless$line;56335634my%set= parse_difftree_raw_line($line);5635if(defined$set{'commit'}) {5636# finish previous commit5637if(%co) {5638print"</td>\n".5639"<td class=\"link\">".5640$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5641" | ".5642$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5643print"</td>\n".5644"</tr>\n";5645}56465647if($alternate) {5648print"<tr class=\"dark\">\n";5649}else{5650print"<tr class=\"light\">\n";5651}5652$alternate^=1;5653%co= parse_commit($set{'commit'});5654my$author= chop_and_escape_str($co{'author_name'},15,5);5655print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5656"<td><i>$author</i></td>\n".5657"<td>".5658$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5659-class=>"list subject"},5660 chop_and_escape_str($co{'title'},50) ."<br/>");5661}elsif(defined$set{'to_id'}) {5662next if($set{'to_id'} =~m/^0{40}$/);56635664print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5665 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5666-class=>"list"},5667"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5668"<br/>\n";5669}5670}5671close$fd;56725673# finish last commit (warning: repetition!)5674if(%co) {5675print"</td>\n".5676"<td class=\"link\">".5677$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5678" | ".5679$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5680print"</td>\n".5681"</tr>\n";5682}56835684print"</table>\n";5685}56865687if($searchtypeeq'grep') {5688 git_print_page_nav('','',$hash,$co{'tree'},$hash);5689 git_print_header_div('commit', esc_html($co{'title'}),$hash);56905691print"<table class=\"grep_search\">\n";5692my$alternate=1;5693my$matches=0;5694$/="\n";5695open my$fd,"-|", git_cmd(),'grep','-n',5696$search_use_regexp? ('-E','-i') :'-F',5697$searchtext,$co{'tree'};5698my$lastfile='';5699while(my$line= <$fd>) {5700chomp$line;5701my($file,$lno,$ltext,$binary);5702last if($matches++>1000);5703if($line=~/^Binary file (.+) matches$/) {5704$file=$1;5705$binary=1;5706}else{5707(undef,$file,$lno,$ltext) =split(/:/,$line,4);5708}5709if($filene$lastfile) {5710$lastfileand print"</td></tr>\n";5711if($alternate++) {5712print"<tr class=\"dark\">\n";5713}else{5714print"<tr class=\"light\">\n";5715}5716print"<td class=\"list\">".5717$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5718 file_name=>"$file"),5719-class=>"list"}, esc_path($file));5720print"</td><td>\n";5721$lastfile=$file;5722}5723if($binary) {5724print"<div class=\"binary\">Binary file</div>\n";5725}else{5726$ltext= untabify($ltext);5727if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5728$ltext= esc_html($1, -nbsp=>1);5729$ltext.='<span class="match">';5730$ltext.= esc_html($2, -nbsp=>1);5731$ltext.='</span>';5732$ltext.= esc_html($3, -nbsp=>1);5733}else{5734$ltext= esc_html($ltext, -nbsp=>1);5735}5736print"<div class=\"pre\">".5737$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5738 file_name=>"$file").'#l'.$lno,5739-class=>"linenr"},sprintf('%4i',$lno))5740.' '.$ltext."</div>\n";5741}5742}5743if($lastfile) {5744print"</td></tr>\n";5745if($matches>1000) {5746print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5747}5748}else{5749print"<div class=\"diff nodifferences\">No matches found</div>\n";5750}5751close$fd;57525753print"</table>\n";5754}5755 git_footer_html();5756}57575758sub git_search_help {5759 git_header_html();5760 git_print_page_nav('','',$hash,$hash,$hash);5761print<<EOT;5762<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5763regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5764the pattern entered is recognized as the POSIX extended5765<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5766insensitive).</p>5767<dl>5768<dt><b>commit</b></dt>5769<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5770EOT5771my($have_grep) = gitweb_check_feature('grep');5772if($have_grep) {5773print<<EOT;5774<dt><b>grep</b></dt>5775<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5776 a different one) are searched for the given pattern. On large trees, this search can take5777a while and put some strain on the server, so please use it with some consideration. Note that5778due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5779case-sensitive.</dd>5780EOT5781}5782print<<EOT;5783<dt><b>author</b></dt>5784<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5785<dt><b>committer</b></dt>5786<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5787EOT5788my($have_pickaxe) = gitweb_check_feature('pickaxe');5789if($have_pickaxe) {5790print<<EOT;5791<dt><b>pickaxe</b></dt>5792<dd>All commits that caused the string to appear or disappear from any file (changes that5793added, removed or "modified" the string) will be listed. This search can take a while and5794takes a lot of strain on the server, so please use it wisely. Note that since you may be5795interested even in changes just changing the case as well, this search is case sensitive.</dd>5796EOT5797}5798print"</dl>\n";5799 git_footer_html();5800}58015802sub git_shortlog {5803my$head= git_get_head_hash($project);5804if(!defined$hash) {5805$hash=$head;5806}5807if(!defined$page) {5808$page=0;5809}5810my$refs= git_get_references();58115812my$commit_hash=$hash;5813if(defined$hash_parent) {5814$commit_hash="$hash_parent..$hash";5815}5816my@commitlist= parse_commits($commit_hash,101, (100*$page));58175818my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5819my$next_link='';5820if($#commitlist>=100) {5821$next_link=5822$cgi->a({-href => href(-replay=>1, page=>$page+1),5823-accesskey =>"n", -title =>"Alt-n"},"next");5824}58255826 git_header_html();5827 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);5828 git_print_header_div('summary',$project);58295830 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);58315832 git_footer_html();5833}58345835## ......................................................................5836## feeds (RSS, Atom; OPML)58375838sub git_feed {5839my$format=shift||'atom';5840my($have_blame) = gitweb_check_feature('blame');58415842# Atom: http://www.atomenabled.org/developers/syndication/5843# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ5844if($formatne'rss'&&$formatne'atom') {5845 die_error(400,"Unknown web feed format");5846}58475848# log/feed of current (HEAD) branch, log of given branch, history of file/directory5849my$head=$hash||'HEAD';5850my@commitlist= parse_commits($head,150,0,$file_name);58515852my%latest_commit;5853my%latest_date;5854my$content_type="application/$format+xml";5855if(defined$cgi->http('HTTP_ACCEPT') &&5856$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {5857# browser (feed reader) prefers text/xml5858$content_type='text/xml';5859}5860if(defined($commitlist[0])) {5861%latest_commit= %{$commitlist[0]};5862%latest_date= parse_date($latest_commit{'author_epoch'});5863print$cgi->header(5864-type =>$content_type,5865-charset =>'utf-8',5866-last_modified =>$latest_date{'rfc2822'});5867}else{5868print$cgi->header(5869-type =>$content_type,5870-charset =>'utf-8');5871}58725873# Optimization: skip generating the body if client asks only5874# for Last-Modified date.5875return if($cgi->request_method()eq'HEAD');58765877# header variables5878my$title="$site_name-$project/$action";5879my$feed_type='log';5880if(defined$hash) {5881$title.=" - '$hash'";5882$feed_type='branch log';5883if(defined$file_name) {5884$title.=" ::$file_name";5885$feed_type='history';5886}5887}elsif(defined$file_name) {5888$title.=" -$file_name";5889$feed_type='history';5890}5891$title.="$feed_type";5892my$descr= git_get_project_description($project);5893if(defined$descr) {5894$descr= esc_html($descr);5895}else{5896$descr="$project".5897($formateq'rss'?'RSS':'Atom') .5898" feed";5899}5900my$owner= git_get_project_owner($project);5901$owner= esc_html($owner);59025903#header5904my$alt_url;5905if(defined$file_name) {5906$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);5907}elsif(defined$hash) {5908$alt_url= href(-full=>1, action=>"log", hash=>$hash);5909}else{5910$alt_url= href(-full=>1, action=>"summary");5911}5912print qq!<?xml version="1.0" encoding="utf-8"?>\n!;5913if($formateq'rss') {5914print<<XML;5915<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">5916<channel>5917XML5918print"<title>$title</title>\n".5919"<link>$alt_url</link>\n".5920"<description>$descr</description>\n".5921"<language>en</language>\n";5922}elsif($formateq'atom') {5923print<<XML;5924<feed xmlns="http://www.w3.org/2005/Atom">5925XML5926print"<title>$title</title>\n".5927"<subtitle>$descr</subtitle>\n".5928'<link rel="alternate" type="text/html" href="'.5929$alt_url.'" />'."\n".5930'<link rel="self" type="'.$content_type.'" href="'.5931$cgi->self_url() .'" />'."\n".5932"<id>". href(-full=>1) ."</id>\n".5933# use project owner for feed author5934"<author><name>$owner</name></author>\n";5935if(defined$favicon) {5936print"<icon>". esc_url($favicon) ."</icon>\n";5937}5938if(defined$logo_url) {5939# not twice as wide as tall: 72 x 27 pixels5940print"<logo>". esc_url($logo) ."</logo>\n";5941}5942if(!%latest_date) {5943# dummy date to keep the feed valid until commits trickle in:5944print"<updated>1970-01-01T00:00:00Z</updated>\n";5945}else{5946print"<updated>$latest_date{'iso-8601'}</updated>\n";5947}5948}59495950# contents5951for(my$i=0;$i<=$#commitlist;$i++) {5952my%co= %{$commitlist[$i]};5953my$commit=$co{'id'};5954# we read 150, we always show 30 and the ones more recent than 48 hours5955if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {5956last;5957}5958my%cd= parse_date($co{'author_epoch'});59595960# get list of changed files5961open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5962$co{'parent'} ||"--root",5963$co{'id'},"--", (defined$file_name?$file_name: ())5964ornext;5965my@difftree=map{chomp;$_} <$fd>;5966close$fd5967ornext;59685969# print element (entry, item)5970my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);5971if($formateq'rss') {5972print"<item>\n".5973"<title>". esc_html($co{'title'}) ."</title>\n".5974"<author>". esc_html($co{'author'}) ."</author>\n".5975"<pubDate>$cd{'rfc2822'}</pubDate>\n".5976"<guid isPermaLink=\"true\">$co_url</guid>\n".5977"<link>$co_url</link>\n".5978"<description>". esc_html($co{'title'}) ."</description>\n".5979"<content:encoded>".5980"<![CDATA[\n";5981}elsif($formateq'atom') {5982print"<entry>\n".5983"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".5984"<updated>$cd{'iso-8601'}</updated>\n".5985"<author>\n".5986" <name>". esc_html($co{'author_name'}) ."</name>\n";5987if($co{'author_email'}) {5988print" <email>". esc_html($co{'author_email'}) ."</email>\n";5989}5990print"</author>\n".5991# use committer for contributor5992"<contributor>\n".5993" <name>". esc_html($co{'committer_name'}) ."</name>\n";5994if($co{'committer_email'}) {5995print" <email>". esc_html($co{'committer_email'}) ."</email>\n";5996}5997print"</contributor>\n".5998"<published>$cd{'iso-8601'}</published>\n".5999"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6000"<id>$co_url</id>\n".6001"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6002"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6003}6004my$comment=$co{'comment'};6005print"<pre>\n";6006foreachmy$line(@$comment) {6007$line= esc_html($line);6008print"$line\n";6009}6010print"</pre><ul>\n";6011foreachmy$difftree_line(@difftree) {6012my%difftree= parse_difftree_raw_line($difftree_line);6013next if!$difftree{'from_id'};60146015my$file=$difftree{'file'} ||$difftree{'to_file'};60166017print"<li>".6018"[".6019$cgi->a({-href => href(-full=>1, action=>"blobdiff",6020 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6021 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6022 file_name=>$file, file_parent=>$difftree{'from_file'}),6023-title =>"diff"},'D');6024if($have_blame) {6025print$cgi->a({-href => href(-full=>1, action=>"blame",6026 file_name=>$file, hash_base=>$commit),6027-title =>"blame"},'B');6028}6029# if this is not a feed of a file history6030if(!defined$file_name||$file_namene$file) {6031print$cgi->a({-href => href(-full=>1, action=>"history",6032 file_name=>$file, hash=>$commit),6033-title =>"history"},'H');6034}6035$file= esc_path($file);6036print"] ".6037"$file</li>\n";6038}6039if($formateq'rss') {6040print"</ul>]]>\n".6041"</content:encoded>\n".6042"</item>\n";6043}elsif($formateq'atom') {6044print"</ul>\n</div>\n".6045"</content>\n".6046"</entry>\n";6047}6048}60496050# end of feed6051if($formateq'rss') {6052print"</channel>\n</rss>\n";6053}elsif($formateq'atom') {6054print"</feed>\n";6055}6056}60576058sub git_rss {6059 git_feed('rss');6060}60616062sub git_atom {6063 git_feed('atom');6064}60656066sub git_opml {6067my@list= git_get_projects_list();60686069print$cgi->header(-type =>'text/xml', -charset =>'utf-8');6070print<<XML;6071<?xml version="1.0" encoding="utf-8"?>6072<opml version="1.0">6073<head>6074 <title>$site_nameOPML Export</title>6075</head>6076<body>6077<outline text="git RSS feeds">6078XML60796080foreachmy$pr(@list) {6081my%proj=%$pr;6082my$head= git_get_head_hash($proj{'path'});6083if(!defined$head) {6084next;6085}6086$git_dir="$projectroot/$proj{'path'}";6087my%co= parse_commit($head);6088if(!%co) {6089next;6090}60916092my$path= esc_html(chop_str($proj{'path'},25,5));6093my$rss="$my_url?p=$proj{'path'};a=rss";6094my$html="$my_url?p=$proj{'path'};a=summary";6095print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6096}6097print<<XML;6098</outline>6099</body>6100</opml>6101XML6102}