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# dispatch 725if(!defined$action) { 726if(defined$hash) { 727$action= git_get_type($hash); 728}elsif(defined$hash_base&&defined$file_name) { 729$action= git_get_type("$hash_base:$file_name"); 730}elsif(defined$project) { 731$action='summary'; 732}else{ 733$action='project_list'; 734} 735} 736if(!defined($actions{$action})) { 737 die_error(400,"Unknown action"); 738} 739if($action!~m/^(opml|project_list|project_index)$/&& 740!$project) { 741 die_error(400,"Project needed"); 742} 743$actions{$action}->(); 744exit; 745 746## ====================================================================== 747## action links 748 749sub href (%) { 750my%params=@_; 751# default is to use -absolute url() i.e. $my_uri 752my$href=$params{-full} ?$my_url:$my_uri; 753 754$params{'project'} =$projectunlessexists$params{'project'}; 755 756if($params{-replay}) { 757while(my($name,$symbol) =each%cgi_param_mapping) { 758if(!exists$params{$name}) { 759$params{$name} =$input_params{$name}; 760} 761} 762} 763 764my($use_pathinfo) = gitweb_check_feature('pathinfo'); 765if($use_pathinfo) { 766# try to put as many parameters as possible in PATH_INFO: 767# - project name 768# - action 769# - hash_parent or hash_parent_base:/file_parent 770# - hash or hash_base:/filename 771 772# When the script is the root DirectoryIndex for the domain, 773# $href here would be something like http://gitweb.example.com/ 774# Thus, we strip any trailing / from $href, to spare us double 775# slashes in the final URL 776$href=~ s,/$,,; 777 778# Then add the project name, if present 779$href.="/".esc_url($params{'project'})ifdefined$params{'project'}; 780delete$params{'project'}; 781 782# Summary just uses the project path URL, any other action is 783# added to the URL 784if(defined$params{'action'}) { 785$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 786delete$params{'action'}; 787} 788 789# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 790# stripping nonexistent or useless pieces 791$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 792||$params{'hash_parent'} ||$params{'hash'}); 793if(defined$params{'hash_base'}) { 794if(defined$params{'hash_parent_base'}) { 795$href.= esc_url($params{'hash_parent_base'}); 796# skip the file_parent if it's the same as the file_name 797delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 798if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 799$href.=":/".esc_url($params{'file_parent'}); 800delete$params{'file_parent'}; 801} 802$href.=".."; 803delete$params{'hash_parent'}; 804delete$params{'hash_parent_base'}; 805}elsif(defined$params{'hash_parent'}) { 806$href.= esc_url($params{'hash_parent'}).".."; 807delete$params{'hash_parent'}; 808} 809 810$href.= esc_url($params{'hash_base'}); 811if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 812$href.=":/".esc_url($params{'file_name'}); 813delete$params{'file_name'}; 814} 815delete$params{'hash'}; 816delete$params{'hash_base'}; 817}elsif(defined$params{'hash'}) { 818$href.= esc_url($params{'hash'}); 819delete$params{'hash'}; 820} 821} 822 823# now encode the parameters explicitly 824my@result= (); 825for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 826my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 827if(defined$params{$name}) { 828if(ref($params{$name})eq"ARRAY") { 829foreachmy$par(@{$params{$name}}) { 830push@result,$symbol."=". esc_param($par); 831} 832}else{ 833push@result,$symbol."=". esc_param($params{$name}); 834} 835} 836} 837$href.="?".join(';',@result)ifscalar@result; 838 839return$href; 840} 841 842 843## ====================================================================== 844## validation, quoting/unquoting and escaping 845 846sub validate_action { 847my$input=shift||returnundef; 848returnundefunlessexists$actions{$input}; 849return$input; 850} 851 852sub validate_project { 853my$input=shift||returnundef; 854if(!validate_pathname($input) || 855!(-d "$projectroot/$input") || 856!check_export_ok("$projectroot/$input") || 857($strict_export&& !project_in_list($input))) { 858returnundef; 859}else{ 860return$input; 861} 862} 863 864sub validate_pathname { 865my$input=shift||returnundef; 866 867# no '.' or '..' as elements of path, i.e. no '.' nor '..' 868# at the beginning, at the end, and between slashes. 869# also this catches doubled slashes 870if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 871returnundef; 872} 873# no null characters 874if($input=~m!\0!) { 875returnundef; 876} 877return$input; 878} 879 880sub validate_refname { 881my$input=shift||returnundef; 882 883# textual hashes are O.K. 884if($input=~m/^[0-9a-fA-F]{40}$/) { 885return$input; 886} 887# it must be correct pathname 888$input= validate_pathname($input) 889orreturnundef; 890# restrictions on ref name according to git-check-ref-format 891if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { 892returnundef; 893} 894return$input; 895} 896 897# decode sequences of octets in utf8 into Perl's internal form, 898# which is utf-8 with utf8 flag set if needed. gitweb writes out 899# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning 900sub to_utf8 { 901my$str=shift; 902if(utf8::valid($str)) { 903 utf8::decode($str); 904return$str; 905}else{ 906return decode($fallback_encoding,$str, Encode::FB_DEFAULT); 907} 908} 909 910# quote unsafe chars, but keep the slash, even when it's not 911# correct, but quoted slashes look too horrible in bookmarks 912sub esc_param { 913my$str=shift; 914$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg; 915$str=~s/\+/%2B/g; 916$str=~s/ /\+/g; 917return$str; 918} 919 920# quote unsafe chars in whole URL, so some charactrs cannot be quoted 921sub esc_url { 922my$str=shift; 923$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg; 924$str=~s/\+/%2B/g; 925$str=~s/ /\+/g; 926return$str; 927} 928 929# replace invalid utf8 character with SUBSTITUTION sequence 930sub esc_html ($;%) { 931my$str=shift; 932my%opts=@_; 933 934$str= to_utf8($str); 935$str=$cgi->escapeHTML($str); 936if($opts{'-nbsp'}) { 937$str=~s/ / /g; 938} 939$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg; 940return$str; 941} 942 943# quote control characters and escape filename to HTML 944sub esc_path { 945my$str=shift; 946my%opts=@_; 947 948$str= to_utf8($str); 949$str=$cgi->escapeHTML($str); 950if($opts{'-nbsp'}) { 951$str=~s/ / /g; 952} 953$str=~ s|([[:cntrl:]])|quot_cec($1)|eg; 954return$str; 955} 956 957# Make control characters "printable", using character escape codes (CEC) 958sub quot_cec { 959my$cntrl=shift; 960my%opts=@_; 961my%es= (# character escape codes, aka escape sequences 962"\t"=>'\t',# tab (HT) 963"\n"=>'\n',# line feed (LF) 964"\r"=>'\r',# carrige return (CR) 965"\f"=>'\f',# form feed (FF) 966"\b"=>'\b',# backspace (BS) 967"\a"=>'\a',# alarm (bell) (BEL) 968"\e"=>'\e',# escape (ESC) 969"\013"=>'\v',# vertical tab (VT) 970"\000"=>'\0',# nul character (NUL) 971); 972my$chr= ( (exists$es{$cntrl}) 973?$es{$cntrl} 974:sprintf('\%2x',ord($cntrl)) ); 975if($opts{-nohtml}) { 976return$chr; 977}else{ 978return"<span class=\"cntrl\">$chr</span>"; 979} 980} 981 982# Alternatively use unicode control pictures codepoints, 983# Unicode "printable representation" (PR) 984sub quot_upr { 985my$cntrl=shift; 986my%opts=@_; 987 988my$chr=sprintf('&#%04d;',0x2400+ord($cntrl)); 989if($opts{-nohtml}) { 990return$chr; 991}else{ 992return"<span class=\"cntrl\">$chr</span>"; 993} 994} 995 996# git may return quoted and escaped filenames 997sub unquote { 998my$str=shift; 9991000sub unq {1001my$seq=shift;1002my%es= (# character escape codes, aka escape sequences1003't'=>"\t",# tab (HT, TAB)1004'n'=>"\n",# newline (NL)1005'r'=>"\r",# return (CR)1006'f'=>"\f",# form feed (FF)1007'b'=>"\b",# backspace (BS)1008'a'=>"\a",# alarm (bell) (BEL)1009'e'=>"\e",# escape (ESC)1010'v'=>"\013",# vertical tab (VT)1011);10121013if($seq=~m/^[0-7]{1,3}$/) {1014# octal char sequence1015returnchr(oct($seq));1016}elsif(exists$es{$seq}) {1017# C escape sequence, aka character escape code1018return$es{$seq};1019}1020# quoted ordinary character1021return$seq;1022}10231024if($str=~m/^"(.*)"$/) {1025# needs unquoting1026$str=$1;1027$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1028}1029return$str;1030}10311032# escape tabs (convert tabs to spaces)1033sub untabify {1034my$line=shift;10351036while((my$pos=index($line,"\t")) != -1) {1037if(my$count= (8- ($pos%8))) {1038my$spaces=' ' x $count;1039$line=~s/\t/$spaces/;1040}1041}10421043return$line;1044}10451046sub project_in_list {1047my$project=shift;1048my@list= git_get_projects_list();1049return@list&&scalar(grep{$_->{'path'}eq$project}@list);1050}10511052## ----------------------------------------------------------------------1053## HTML aware string manipulation10541055# Try to chop given string on a word boundary between position1056# $len and $len+$add_len. If there is no word boundary there,1057# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1058# (marking chopped part) would be longer than given string.1059sub chop_str {1060my$str=shift;1061my$len=shift;1062my$add_len=shift||10;1063my$where=shift||'right';# 'left' | 'center' | 'right'10641065# Make sure perl knows it is utf8 encoded so we don't1066# cut in the middle of a utf8 multibyte char.1067$str= to_utf8($str);10681069# allow only $len chars, but don't cut a word if it would fit in $add_len1070# if it doesn't fit, cut it if it's still longer than the dots we would add1071# remove chopped character entities entirely10721073# when chopping in the middle, distribute $len into left and right part1074# return early if chopping wouldn't make string shorter1075if($whereeq'center') {1076return$strif($len+5>=length($str));# filler is length 51077$len=int($len/2);1078}else{1079return$strif($len+4>=length($str));# filler is length 41080}10811082# regexps: ending and beginning with word part up to $add_len1083my$endre=qr/.{$len}\w{0,$add_len}/;1084my$begre=qr/\w{0,$add_len}.{$len}/;10851086if($whereeq'left') {1087$str=~m/^(.*?)($begre)$/;1088my($lead,$body) = ($1,$2);1089if(length($lead) >4) {1090$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1091$lead=" ...";1092}1093return"$lead$body";10941095}elsif($whereeq'center') {1096$str=~m/^($endre)(.*)$/;1097my($left,$str) = ($1,$2);1098$str=~m/^(.*?)($begre)$/;1099my($mid,$right) = ($1,$2);1100if(length($mid) >5) {1101$left=~s/&[^;]*$//;1102$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1103$mid=" ... ";1104}1105return"$left$mid$right";11061107}else{1108$str=~m/^($endre)(.*)$/;1109my$body=$1;1110my$tail=$2;1111if(length($tail) >4) {1112$body=~s/&[^;]*$//;1113$tail="... ";1114}1115return"$body$tail";1116}1117}11181119# takes the same arguments as chop_str, but also wraps a <span> around the1120# result with a title attribute if it does get chopped. Additionally, the1121# string is HTML-escaped.1122sub chop_and_escape_str {1123my($str) =@_;11241125my$chopped= chop_str(@_);1126if($choppedeq$str) {1127return esc_html($chopped);1128}else{1129$str=~s/([[:cntrl:]])/?/g;1130return$cgi->span({-title=>$str}, esc_html($chopped));1131}1132}11331134## ----------------------------------------------------------------------1135## functions returning short strings11361137# CSS class for given age value (in seconds)1138sub age_class {1139my$age=shift;11401141if(!defined$age) {1142return"noage";1143}elsif($age<60*60*2) {1144return"age0";1145}elsif($age<60*60*24*2) {1146return"age1";1147}else{1148return"age2";1149}1150}11511152# convert age in seconds to "nn units ago" string1153sub age_string {1154my$age=shift;1155my$age_str;11561157if($age>60*60*24*365*2) {1158$age_str= (int$age/60/60/24/365);1159$age_str.=" years ago";1160}elsif($age>60*60*24*(365/12)*2) {1161$age_str=int$age/60/60/24/(365/12);1162$age_str.=" months ago";1163}elsif($age>60*60*24*7*2) {1164$age_str=int$age/60/60/24/7;1165$age_str.=" weeks ago";1166}elsif($age>60*60*24*2) {1167$age_str=int$age/60/60/24;1168$age_str.=" days ago";1169}elsif($age>60*60*2) {1170$age_str=int$age/60/60;1171$age_str.=" hours ago";1172}elsif($age>60*2) {1173$age_str=int$age/60;1174$age_str.=" min ago";1175}elsif($age>2) {1176$age_str=int$age;1177$age_str.=" sec ago";1178}else{1179$age_str.=" right now";1180}1181return$age_str;1182}11831184useconstant{1185 S_IFINVALID =>0030000,1186 S_IFGITLINK =>0160000,1187};11881189# submodule/subproject, a commit object reference1190sub S_ISGITLINK($) {1191my$mode=shift;11921193return(($mode& S_IFMT) == S_IFGITLINK)1194}11951196# convert file mode in octal to symbolic file mode string1197sub mode_str {1198my$mode=oct shift;11991200if(S_ISGITLINK($mode)) {1201return'm---------';1202}elsif(S_ISDIR($mode& S_IFMT)) {1203return'drwxr-xr-x';1204}elsif(S_ISLNK($mode)) {1205return'lrwxrwxrwx';1206}elsif(S_ISREG($mode)) {1207# git cares only about the executable bit1208if($mode& S_IXUSR) {1209return'-rwxr-xr-x';1210}else{1211return'-rw-r--r--';1212};1213}else{1214return'----------';1215}1216}12171218# convert file mode in octal to file type string1219sub file_type {1220my$mode=shift;12211222if($mode!~m/^[0-7]+$/) {1223return$mode;1224}else{1225$mode=oct$mode;1226}12271228if(S_ISGITLINK($mode)) {1229return"submodule";1230}elsif(S_ISDIR($mode& S_IFMT)) {1231return"directory";1232}elsif(S_ISLNK($mode)) {1233return"symlink";1234}elsif(S_ISREG($mode)) {1235return"file";1236}else{1237return"unknown";1238}1239}12401241# convert file mode in octal to file type description string1242sub file_type_long {1243my$mode=shift;12441245if($mode!~m/^[0-7]+$/) {1246return$mode;1247}else{1248$mode=oct$mode;1249}12501251if(S_ISGITLINK($mode)) {1252return"submodule";1253}elsif(S_ISDIR($mode& S_IFMT)) {1254return"directory";1255}elsif(S_ISLNK($mode)) {1256return"symlink";1257}elsif(S_ISREG($mode)) {1258if($mode& S_IXUSR) {1259return"executable";1260}else{1261return"file";1262};1263}else{1264return"unknown";1265}1266}126712681269## ----------------------------------------------------------------------1270## functions returning short HTML fragments, or transforming HTML fragments1271## which don't belong to other sections12721273# format line of commit message.1274sub format_log_line_html {1275my$line=shift;12761277$line= esc_html($line, -nbsp=>1);1278if($line=~m/([0-9a-fA-F]{8,40})/) {1279my$hash_text=$1;1280my$link=1281$cgi->a({-href => href(action=>"object", hash=>$hash_text),1282-class=>"text"},$hash_text);1283$line=~s/$hash_text/$link/;1284}1285return$line;1286}12871288# format marker of refs pointing to given object12891290# the destination action is chosen based on object type and current context:1291# - for annotated tags, we choose the tag view unless it's the current view1292# already, in which case we go to shortlog view1293# - for other refs, we keep the current view if we're in history, shortlog or1294# log view, and select shortlog otherwise1295sub format_ref_marker {1296my($refs,$id) =@_;1297my$markers='';12981299if(defined$refs->{$id}) {1300foreachmy$ref(@{$refs->{$id}}) {1301# this code exploits the fact that non-lightweight tags are the1302# only indirect objects, and that they are the only objects for which1303# we want to use tag instead of shortlog as action1304my($type,$name) =qw();1305my$indirect= ($ref=~s/\^\{\}$//);1306# e.g. tags/v2.6.11 or heads/next1307if($ref=~m!^(.*?)s?/(.*)$!) {1308$type=$1;1309$name=$2;1310}else{1311$type="ref";1312$name=$ref;1313}13141315my$class=$type;1316$class.=" indirect"if$indirect;13171318my$dest_action="shortlog";13191320if($indirect) {1321$dest_action="tag"unless$actioneq"tag";1322}elsif($action=~/^(history|(short)?log)$/) {1323$dest_action=$action;1324}13251326my$dest="";1327$dest.="refs/"unless$ref=~ m!^refs/!;1328$dest.=$ref;13291330my$link=$cgi->a({1331-href => href(1332 action=>$dest_action,1333 hash=>$dest1334)},$name);13351336$markers.=" <span class=\"$class\"title=\"$ref\">".1337$link."</span>";1338}1339}13401341if($markers) {1342return' <span class="refs">'.$markers.'</span>';1343}else{1344return"";1345}1346}13471348# format, perhaps shortened and with markers, title line1349sub format_subject_html {1350my($long,$short,$href,$extra) =@_;1351$extra=''unlessdefined($extra);13521353if(length($short) <length($long)) {1354return$cgi->a({-href =>$href, -class=>"list subject",1355-title => to_utf8($long)},1356 esc_html($short) .$extra);1357}else{1358return$cgi->a({-href =>$href, -class=>"list subject"},1359 esc_html($long) .$extra);1360}1361}13621363# format git diff header line, i.e. "diff --(git|combined|cc) ..."1364sub format_git_diff_header_line {1365my$line=shift;1366my$diffinfo=shift;1367my($from,$to) =@_;13681369if($diffinfo->{'nparents'}) {1370# combined diff1371$line=~s!^(diff (.*?) )"?.*$!$1!;1372if($to->{'href'}) {1373$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1374 esc_path($to->{'file'}));1375}else{# file was deleted (no href)1376$line.= esc_path($to->{'file'});1377}1378}else{1379# "ordinary" diff1380$line=~s!^(diff (.*?) )"?a/.*$!$1!;1381if($from->{'href'}) {1382$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1383'a/'. esc_path($from->{'file'}));1384}else{# file was added (no href)1385$line.='a/'. esc_path($from->{'file'});1386}1387$line.=' ';1388if($to->{'href'}) {1389$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1390'b/'. esc_path($to->{'file'}));1391}else{# file was deleted1392$line.='b/'. esc_path($to->{'file'});1393}1394}13951396return"<div class=\"diff header\">$line</div>\n";1397}13981399# format extended diff header line, before patch itself1400sub format_extended_diff_header_line {1401my$line=shift;1402my$diffinfo=shift;1403my($from,$to) =@_;14041405# match <path>1406if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1407$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1408 esc_path($from->{'file'}));1409}1410if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1411$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1412 esc_path($to->{'file'}));1413}1414# match single <mode>1415if($line=~m/\s(\d{6})$/) {1416$line.='<span class="info"> ('.1417 file_type_long($1) .1418')</span>';1419}1420# match <hash>1421if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1422# can match only for combined diff1423$line='index ';1424for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1425if($from->{'href'}[$i]) {1426$line.=$cgi->a({-href=>$from->{'href'}[$i],1427-class=>"hash"},1428substr($diffinfo->{'from_id'}[$i],0,7));1429}else{1430$line.='0' x 7;1431}1432# separator1433$line.=','if($i<$diffinfo->{'nparents'} -1);1434}1435$line.='..';1436if($to->{'href'}) {1437$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1438substr($diffinfo->{'to_id'},0,7));1439}else{1440$line.='0' x 7;1441}14421443}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1444# can match only for ordinary diff1445my($from_link,$to_link);1446if($from->{'href'}) {1447$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1448substr($diffinfo->{'from_id'},0,7));1449}else{1450$from_link='0' x 7;1451}1452if($to->{'href'}) {1453$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1454substr($diffinfo->{'to_id'},0,7));1455}else{1456$to_link='0' x 7;1457}1458my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1459$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1460}14611462return$line."<br/>\n";1463}14641465# format from-file/to-file diff header1466sub format_diff_from_to_header {1467my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1468my$line;1469my$result='';14701471$line=$from_line;1472#assert($line =~ m/^---/) if DEBUG;1473# no extra formatting for "^--- /dev/null"1474if(!$diffinfo->{'nparents'}) {1475# ordinary (single parent) diff1476if($line=~m!^--- "?a/!) {1477if($from->{'href'}) {1478$line='--- a/'.1479$cgi->a({-href=>$from->{'href'}, -class=>"path"},1480 esc_path($from->{'file'}));1481}else{1482$line='--- a/'.1483 esc_path($from->{'file'});1484}1485}1486$result.= qq!<div class="diff from_file">$line</div>\n!;14871488}else{1489# combined diff (merge commit)1490for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1491if($from->{'href'}[$i]) {1492$line='--- '.1493$cgi->a({-href=>href(action=>"blobdiff",1494 hash_parent=>$diffinfo->{'from_id'}[$i],1495 hash_parent_base=>$parents[$i],1496 file_parent=>$from->{'file'}[$i],1497 hash=>$diffinfo->{'to_id'},1498 hash_base=>$hash,1499 file_name=>$to->{'file'}),1500-class=>"path",1501-title=>"diff". ($i+1)},1502$i+1) .1503'/'.1504$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1505 esc_path($from->{'file'}[$i]));1506}else{1507$line='--- /dev/null';1508}1509$result.= qq!<div class="diff from_file">$line</div>\n!;1510}1511}15121513$line=$to_line;1514#assert($line =~ m/^\+\+\+/) if DEBUG;1515# no extra formatting for "^+++ /dev/null"1516if($line=~m!^\+\+\+ "?b/!) {1517if($to->{'href'}) {1518$line='+++ b/'.1519$cgi->a({-href=>$to->{'href'}, -class=>"path"},1520 esc_path($to->{'file'}));1521}else{1522$line='+++ b/'.1523 esc_path($to->{'file'});1524}1525}1526$result.= qq!<div class="diff to_file">$line</div>\n!;15271528return$result;1529}15301531# create note for patch simplified by combined diff1532sub format_diff_cc_simplified {1533my($diffinfo,@parents) =@_;1534my$result='';15351536$result.="<div class=\"diff header\">".1537"diff --cc ";1538if(!is_deleted($diffinfo)) {1539$result.=$cgi->a({-href => href(action=>"blob",1540 hash_base=>$hash,1541 hash=>$diffinfo->{'to_id'},1542 file_name=>$diffinfo->{'to_file'}),1543-class=>"path"},1544 esc_path($diffinfo->{'to_file'}));1545}else{1546$result.= esc_path($diffinfo->{'to_file'});1547}1548$result.="</div>\n".# class="diff header"1549"<div class=\"diff nodifferences\">".1550"Simple merge".1551"</div>\n";# class="diff nodifferences"15521553return$result;1554}15551556# format patch (diff) line (not to be used for diff headers)1557sub format_diff_line {1558my$line=shift;1559my($from,$to) =@_;1560my$diff_class="";15611562chomp$line;15631564if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1565# combined diff1566my$prefix=substr($line,0,scalar@{$from->{'href'}});1567if($line=~m/^\@{3}/) {1568$diff_class=" chunk_header";1569}elsif($line=~m/^\\/) {1570$diff_class=" incomplete";1571}elsif($prefix=~tr/+/+/) {1572$diff_class=" add";1573}elsif($prefix=~tr/-/-/) {1574$diff_class=" rem";1575}1576}else{1577# assume ordinary diff1578my$char=substr($line,0,1);1579if($chareq'+') {1580$diff_class=" add";1581}elsif($chareq'-') {1582$diff_class=" rem";1583}elsif($chareq'@') {1584$diff_class=" chunk_header";1585}elsif($chareq"\\") {1586$diff_class=" incomplete";1587}1588}1589$line= untabify($line);1590if($from&&$to&&$line=~m/^\@{2} /) {1591my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1592$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;15931594$from_lines=0unlessdefined$from_lines;1595$to_lines=0unlessdefined$to_lines;15961597if($from->{'href'}) {1598$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1599-class=>"list"},$from_text);1600}1601if($to->{'href'}) {1602$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1603-class=>"list"},$to_text);1604}1605$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1606"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1607return"<div class=\"diff$diff_class\">$line</div>\n";1608}elsif($from&&$to&&$line=~m/^\@{3}/) {1609my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1610my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);16111612@from_text=split(' ',$ranges);1613for(my$i=0;$i<@from_text; ++$i) {1614($from_start[$i],$from_nlines[$i]) =1615(split(',',substr($from_text[$i],1)),0);1616}16171618$to_text=pop@from_text;1619$to_start=pop@from_start;1620$to_nlines=pop@from_nlines;16211622$line="<span class=\"chunk_info\">$prefix";1623for(my$i=0;$i<@from_text; ++$i) {1624if($from->{'href'}[$i]) {1625$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1626-class=>"list"},$from_text[$i]);1627}else{1628$line.=$from_text[$i];1629}1630$line.=" ";1631}1632if($to->{'href'}) {1633$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1634-class=>"list"},$to_text);1635}else{1636$line.=$to_text;1637}1638$line.="$prefix</span>".1639"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1640return"<div class=\"diff$diff_class\">$line</div>\n";1641}1642return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1643}16441645# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1646# linked. Pass the hash of the tree/commit to snapshot.1647sub format_snapshot_links {1648my($hash) =@_;1649my@snapshot_fmts= gitweb_check_feature('snapshot');1650@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);1651my$num_fmts=@snapshot_fmts;1652if($num_fmts>1) {1653# A parenthesized list of links bearing format names.1654# e.g. "snapshot (_tar.gz_ _zip_)"1655return"snapshot (".join(' ',map1656$cgi->a({1657-href => href(1658 action=>"snapshot",1659 hash=>$hash,1660 snapshot_format=>$_1661)1662},$known_snapshot_formats{$_}{'display'})1663,@snapshot_fmts) .")";1664}elsif($num_fmts==1) {1665# A single "snapshot" link whose tooltip bears the format name.1666# i.e. "_snapshot_"1667my($fmt) =@snapshot_fmts;1668return1669$cgi->a({1670-href => href(1671 action=>"snapshot",1672 hash=>$hash,1673 snapshot_format=>$fmt1674),1675-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1676},"snapshot");1677}else{# $num_fmts == 01678returnundef;1679}1680}16811682## ......................................................................1683## functions returning values to be passed, perhaps after some1684## transformation, to other functions; e.g. returning arguments to href()16851686# returns hash to be passed to href to generate gitweb URL1687# in -title key it returns description of link1688sub get_feed_info {1689my$format=shift||'Atom';1690my%res= (action =>lc($format));16911692# feed links are possible only for project views1693return unless(defined$project);1694# some views should link to OPML, or to generic project feed,1695# or don't have specific feed yet (so they should use generic)1696return if($action=~/^(?:tags|heads|forks|tag|search)$/x);16971698my$branch;1699# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1700# from tag links; this also makes possible to detect branch links1701if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1702(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1703$branch=$1;1704}1705# find log type for feed description (title)1706my$type='log';1707if(defined$file_name) {1708$type="history of$file_name";1709$type.="/"if($actioneq'tree');1710$type.=" on '$branch'"if(defined$branch);1711}else{1712$type="log of$branch"if(defined$branch);1713}17141715$res{-title} =$type;1716$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1717$res{'file_name'} =$file_name;17181719return%res;1720}17211722## ----------------------------------------------------------------------1723## git utility subroutines, invoking git commands17241725# returns path to the core git executable and the --git-dir parameter as list1726sub git_cmd {1727return$GIT,'--git-dir='.$git_dir;1728}17291730# quote the given arguments for passing them to the shell1731# quote_command("command", "arg 1", "arg with ' and ! characters")1732# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1733# Try to avoid using this function wherever possible.1734sub quote_command {1735returnjoin(' ',1736map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1737}17381739# get HEAD ref of given project as hash1740sub git_get_head_hash {1741my$project=shift;1742my$o_git_dir=$git_dir;1743my$retval=undef;1744$git_dir="$projectroot/$project";1745if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1746my$head= <$fd>;1747close$fd;1748if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1749$retval=$1;1750}1751}1752if(defined$o_git_dir) {1753$git_dir=$o_git_dir;1754}1755return$retval;1756}17571758# get type of given object1759sub git_get_type {1760my$hash=shift;17611762open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1763my$type= <$fd>;1764close$fdorreturn;1765chomp$type;1766return$type;1767}17681769# repository configuration1770our$config_file='';1771our%config;17721773# store multiple values for single key as anonymous array reference1774# single values stored directly in the hash, not as [ <value> ]1775sub hash_set_multi {1776my($hash,$key,$value) =@_;17771778if(!exists$hash->{$key}) {1779$hash->{$key} =$value;1780}elsif(!ref$hash->{$key}) {1781$hash->{$key} = [$hash->{$key},$value];1782}else{1783push@{$hash->{$key}},$value;1784}1785}17861787# return hash of git project configuration1788# optionally limited to some section, e.g. 'gitweb'1789sub git_parse_project_config {1790my$section_regexp=shift;1791my%config;17921793local$/="\0";17941795open my$fh,"-|", git_cmd(),"config",'-z','-l',1796orreturn;17971798while(my$keyval= <$fh>) {1799chomp$keyval;1800my($key,$value) =split(/\n/,$keyval,2);18011802 hash_set_multi(\%config,$key,$value)1803if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1804}1805close$fh;18061807return%config;1808}18091810# convert config value to boolean, 'true' or 'false'1811# no value, number > 0, 'true' and 'yes' values are true1812# rest of values are treated as false (never as error)1813sub config_to_bool {1814my$val=shift;18151816# strip leading and trailing whitespace1817$val=~s/^\s+//;1818$val=~s/\s+$//;18191820return(!defined$val||# section.key1821($val=~/^\d+$/&&$val) ||# section.key = 11822($val=~/^(?:true|yes)$/i));# section.key = true1823}18241825# convert config value to simple decimal number1826# an optional value suffix of 'k', 'm', or 'g' will cause the value1827# to be multiplied by 1024, 1048576, or 10737418241828sub config_to_int {1829my$val=shift;18301831# strip leading and trailing whitespace1832$val=~s/^\s+//;1833$val=~s/\s+$//;18341835if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1836$unit=lc($unit);1837# unknown unit is treated as 11838return$num* ($uniteq'g'?1073741824:1839$uniteq'm'?1048576:1840$uniteq'k'?1024:1);1841}1842return$val;1843}18441845# convert config value to array reference, if needed1846sub config_to_multi {1847my$val=shift;18481849returnref($val) ?$val: (defined($val) ? [$val] : []);1850}18511852sub git_get_project_config {1853my($key,$type) =@_;18541855# key sanity check1856return unless($key);1857$key=~s/^gitweb\.//;1858return if($key=~m/\W/);18591860# type sanity check1861if(defined$type) {1862$type=~s/^--//;1863$type=undef1864unless($typeeq'bool'||$typeeq'int');1865}18661867# get config1868if(!defined$config_file||1869$config_filene"$git_dir/config") {1870%config= git_parse_project_config('gitweb');1871$config_file="$git_dir/config";1872}18731874# ensure given type1875if(!defined$type) {1876return$config{"gitweb.$key"};1877}elsif($typeeq'bool') {1878# backward compatibility: 'git config --bool' returns true/false1879return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1880}elsif($typeeq'int') {1881return config_to_int($config{"gitweb.$key"});1882}1883return$config{"gitweb.$key"};1884}18851886# get hash of given path at given ref1887sub git_get_hash_by_path {1888my$base=shift;1889my$path=shift||returnundef;1890my$type=shift;18911892$path=~ s,/+$,,;18931894open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path1895or die_error(500,"Open git-ls-tree failed");1896my$line= <$fd>;1897close$fdorreturnundef;18981899if(!defined$line) {1900# there is no tree or hash given by $path at $base1901returnundef;1902}19031904#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'1905$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;1906if(defined$type&&$typene$2) {1907# type doesn't match1908returnundef;1909}1910return$3;1911}19121913# get path of entry with given hash at given tree-ish (ref)1914# used to get 'from' filename for combined diff (merge commit) for renames1915sub git_get_path_by_hash {1916my$base=shift||return;1917my$hash=shift||return;19181919local$/="\0";19201921open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base1922orreturnundef;1923while(my$line= <$fd>) {1924chomp$line;19251926#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'1927#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'1928if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {1929close$fd;1930return$1;1931}1932}1933close$fd;1934returnundef;1935}19361937## ......................................................................1938## git utility functions, directly accessing git repository19391940sub git_get_project_description {1941my$path=shift;19421943$git_dir="$projectroot/$path";1944open my$fd,"$git_dir/description"1945orreturn git_get_project_config('description');1946my$descr= <$fd>;1947close$fd;1948if(defined$descr) {1949chomp$descr;1950}1951return$descr;1952}19531954sub git_get_project_ctags {1955my$path=shift;1956my$ctags= {};19571958$git_dir="$projectroot/$path";1959unless(opendir D,"$git_dir/ctags") {1960return$ctags;1961}1962foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir(D)) {1963open CT,$_ornext;1964my$val= <CT>;1965chomp$val;1966close CT;1967my$ctag=$_;$ctag=~ s#.*/##;1968$ctags->{$ctag} =$val;1969}1970closedir D;1971$ctags;1972}19731974sub git_populate_project_tagcloud {1975my$ctags=shift;19761977# First, merge different-cased tags; tags vote on casing1978my%ctags_lc;1979foreach(keys%$ctags) {1980$ctags_lc{lc$_}->{count} +=$ctags->{$_};1981if(not$ctags_lc{lc$_}->{topcount}1982or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {1983$ctags_lc{lc$_}->{topcount} =$ctags->{$_};1984$ctags_lc{lc$_}->{topname} =$_;1985}1986}19871988my$cloud;1989if(eval{require HTML::TagCloud;1; }) {1990$cloud= HTML::TagCloud->new;1991foreach(sort keys%ctags_lc) {1992# Pad the title with spaces so that the cloud looks1993# less crammed.1994my$title=$ctags_lc{$_}->{topname};1995$title=~s/ / /g;1996$title=~s/^/ /g;1997$title=~s/$/ /g;1998$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});1999}2000}else{2001$cloud= \%ctags_lc;2002}2003$cloud;2004}20052006sub git_show_project_tagcloud {2007my($cloud,$count) =@_;2008print STDERR ref($cloud)."..\n";2009if(ref$cloudeq'HTML::TagCloud') {2010return$cloud->html_and_css($count);2011}else{2012my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2013return'<p align="center">'.join(', ',map{2014"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2015}splice(@tags,0,$count)) .'</p>';2016}2017}20182019sub git_get_project_url_list {2020my$path=shift;20212022$git_dir="$projectroot/$path";2023open my$fd,"$git_dir/cloneurl"2024orreturnwantarray?2025@{ config_to_multi(git_get_project_config('url')) } :2026 config_to_multi(git_get_project_config('url'));2027my@git_project_url_list=map{chomp;$_} <$fd>;2028close$fd;20292030returnwantarray?@git_project_url_list: \@git_project_url_list;2031}20322033sub git_get_projects_list {2034my($filter) =@_;2035my@list;20362037$filter||='';2038$filter=~s/\.git$//;20392040my($check_forks) = gitweb_check_feature('forks');20412042if(-d $projects_list) {2043# search in directory2044my$dir=$projects_list. ($filter?"/$filter":'');2045# remove the trailing "/"2046$dir=~s!/+$!!;2047my$pfxlen=length("$dir");2048my$pfxdepth= ($dir=~tr!/!!);20492050 File::Find::find({2051 follow_fast =>1,# follow symbolic links2052 follow_skip =>2,# ignore duplicates2053 dangling_symlinks =>0,# ignore dangling symlinks, silently2054 wanted =>sub{2055# skip project-list toplevel, if we get it.2056return if(m!^[/.]$!);2057# only directories can be git repositories2058return unless(-d $_);2059# don't traverse too deep (Find is super slow on os x)2060if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2061$File::Find::prune =1;2062return;2063}20642065my$subdir=substr($File::Find::name,$pfxlen+1);2066# we check related file in $projectroot2067if(check_export_ok("$projectroot/$filter/$subdir")) {2068push@list, { path => ($filter?"$filter/":'') .$subdir};2069$File::Find::prune =1;2070}2071},2072},"$dir");20732074}elsif(-f $projects_list) {2075# read from file(url-encoded):2076# 'git%2Fgit.git Linus+Torvalds'2077# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2078# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2079my%paths;2080open my($fd),$projects_listorreturn;2081 PROJECT:2082while(my$line= <$fd>) {2083chomp$line;2084my($path,$owner) =split' ',$line;2085$path= unescape($path);2086$owner= unescape($owner);2087if(!defined$path) {2088next;2089}2090if($filterne'') {2091# looking for forks;2092my$pfx=substr($path,0,length($filter));2093if($pfxne$filter) {2094next PROJECT;2095}2096my$sfx=substr($path,length($filter));2097if($sfx!~/^\/.*\.git$/) {2098next PROJECT;2099}2100}elsif($check_forks) {2101 PATH:2102foreachmy$filter(keys%paths) {2103# looking for forks;2104my$pfx=substr($path,0,length($filter));2105if($pfxne$filter) {2106next PATH;2107}2108my$sfx=substr($path,length($filter));2109if($sfx!~/^\/.*\.git$/) {2110next PATH;2111}2112# is a fork, don't include it in2113# the list2114next PROJECT;2115}2116}2117if(check_export_ok("$projectroot/$path")) {2118my$pr= {2119 path =>$path,2120 owner => to_utf8($owner),2121};2122push@list,$pr;2123(my$forks_path=$path) =~s/\.git$//;2124$paths{$forks_path}++;2125}2126}2127close$fd;2128}2129return@list;2130}21312132our$gitweb_project_owner=undef;2133sub git_get_project_list_from_file {21342135return if(defined$gitweb_project_owner);21362137$gitweb_project_owner= {};2138# read from file (url-encoded):2139# 'git%2Fgit.git Linus+Torvalds'2140# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2141# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2142if(-f $projects_list) {2143open(my$fd,$projects_list);2144while(my$line= <$fd>) {2145chomp$line;2146my($pr,$ow) =split' ',$line;2147$pr= unescape($pr);2148$ow= unescape($ow);2149$gitweb_project_owner->{$pr} = to_utf8($ow);2150}2151close$fd;2152}2153}21542155sub git_get_project_owner {2156my$project=shift;2157my$owner;21582159returnundefunless$project;2160$git_dir="$projectroot/$project";21612162if(!defined$gitweb_project_owner) {2163 git_get_project_list_from_file();2164}21652166if(exists$gitweb_project_owner->{$project}) {2167$owner=$gitweb_project_owner->{$project};2168}2169if(!defined$owner){2170$owner= git_get_project_config('owner');2171}2172if(!defined$owner) {2173$owner= get_file_owner("$git_dir");2174}21752176return$owner;2177}21782179sub git_get_last_activity {2180my($path) =@_;2181my$fd;21822183$git_dir="$projectroot/$path";2184open($fd,"-|", git_cmd(),'for-each-ref',2185'--format=%(committer)',2186'--sort=-committerdate',2187'--count=1',2188'refs/heads')orreturn;2189my$most_recent= <$fd>;2190close$fdorreturn;2191if(defined$most_recent&&2192$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2193my$timestamp=$1;2194my$age=time-$timestamp;2195return($age, age_string($age));2196}2197return(undef,undef);2198}21992200sub git_get_references {2201my$type=shift||"";2202my%refs;2203# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112204# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2205open my$fd,"-|", git_cmd(),"show-ref","--dereference",2206($type? ("--","refs/$type") : ())# use -- <pattern> if $type2207orreturn;22082209while(my$line= <$fd>) {2210chomp$line;2211if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2212if(defined$refs{$1}) {2213push@{$refs{$1}},$2;2214}else{2215$refs{$1} = [$2];2216}2217}2218}2219close$fdorreturn;2220return \%refs;2221}22222223sub git_get_rev_name_tags {2224my$hash=shift||returnundef;22252226open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2227orreturn;2228my$name_rev= <$fd>;2229close$fd;22302231if($name_rev=~ m|^$hash tags/(.*)$|) {2232return$1;2233}else{2234# catches also '$hash undefined' output2235returnundef;2236}2237}22382239## ----------------------------------------------------------------------2240## parse to hash functions22412242sub parse_date {2243my$epoch=shift;2244my$tz=shift||"-0000";22452246my%date;2247my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2248my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2249my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2250$date{'hour'} =$hour;2251$date{'minute'} =$min;2252$date{'mday'} =$mday;2253$date{'day'} =$days[$wday];2254$date{'month'} =$months[$mon];2255$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2256$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2257$date{'mday-time'} =sprintf"%d%s%02d:%02d",2258$mday,$months[$mon],$hour,$min;2259$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",22601900+$year,1+$mon,$mday,$hour,$min,$sec;22612262$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2263my$local=$epoch+ ((int$1+ ($2/60)) *3600);2264($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2265$date{'hour_local'} =$hour;2266$date{'minute_local'} =$min;2267$date{'tz_local'} =$tz;2268$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",22691900+$year,$mon+1,$mday,2270$hour,$min,$sec,$tz);2271return%date;2272}22732274sub parse_tag {2275my$tag_id=shift;2276my%tag;2277my@comment;22782279open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2280$tag{'id'} =$tag_id;2281while(my$line= <$fd>) {2282chomp$line;2283if($line=~m/^object ([0-9a-fA-F]{40})$/) {2284$tag{'object'} =$1;2285}elsif($line=~m/^type (.+)$/) {2286$tag{'type'} =$1;2287}elsif($line=~m/^tag (.+)$/) {2288$tag{'name'} =$1;2289}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2290$tag{'author'} =$1;2291$tag{'epoch'} =$2;2292$tag{'tz'} =$3;2293}elsif($line=~m/--BEGIN/) {2294push@comment,$line;2295last;2296}elsif($lineeq"") {2297last;2298}2299}2300push@comment, <$fd>;2301$tag{'comment'} = \@comment;2302close$fdorreturn;2303if(!defined$tag{'name'}) {2304return2305};2306return%tag2307}23082309sub parse_commit_text {2310my($commit_text,$withparents) =@_;2311my@commit_lines=split'\n',$commit_text;2312my%co;23132314pop@commit_lines;# Remove '\0'23152316if(!@commit_lines) {2317return;2318}23192320my$header=shift@commit_lines;2321if($header!~m/^[0-9a-fA-F]{40}/) {2322return;2323}2324($co{'id'},my@parents) =split' ',$header;2325while(my$line=shift@commit_lines) {2326last if$lineeq"\n";2327if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2328$co{'tree'} =$1;2329}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2330push@parents,$1;2331}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2332$co{'author'} =$1;2333$co{'author_epoch'} =$2;2334$co{'author_tz'} =$3;2335if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2336$co{'author_name'} =$1;2337$co{'author_email'} =$2;2338}else{2339$co{'author_name'} =$co{'author'};2340}2341}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2342$co{'committer'} =$1;2343$co{'committer_epoch'} =$2;2344$co{'committer_tz'} =$3;2345$co{'committer_name'} =$co{'committer'};2346if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2347$co{'committer_name'} =$1;2348$co{'committer_email'} =$2;2349}else{2350$co{'committer_name'} =$co{'committer'};2351}2352}2353}2354if(!defined$co{'tree'}) {2355return;2356};2357$co{'parents'} = \@parents;2358$co{'parent'} =$parents[0];23592360foreachmy$title(@commit_lines) {2361$title=~s/^ //;2362if($titlene"") {2363$co{'title'} = chop_str($title,80,5);2364# remove leading stuff of merges to make the interesting part visible2365if(length($title) >50) {2366$title=~s/^Automatic //;2367$title=~s/^merge (of|with) /Merge ... /i;2368if(length($title) >50) {2369$title=~s/(http|rsync):\/\///;2370}2371if(length($title) >50) {2372$title=~s/(master|www|rsync)\.//;2373}2374if(length($title) >50) {2375$title=~s/kernel.org:?//;2376}2377if(length($title) >50) {2378$title=~s/\/pub\/scm//;2379}2380}2381$co{'title_short'} = chop_str($title,50,5);2382last;2383}2384}2385if(!defined$co{'title'} ||$co{'title'}eq"") {2386$co{'title'} =$co{'title_short'} ='(no commit message)';2387}2388# remove added spaces2389foreachmy$line(@commit_lines) {2390$line=~s/^ //;2391}2392$co{'comment'} = \@commit_lines;23932394my$age=time-$co{'committer_epoch'};2395$co{'age'} =$age;2396$co{'age_string'} = age_string($age);2397my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2398if($age>60*60*24*7*2) {2399$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2400$co{'age_string_age'} =$co{'age_string'};2401}else{2402$co{'age_string_date'} =$co{'age_string'};2403$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2404}2405return%co;2406}24072408sub parse_commit {2409my($commit_id) =@_;2410my%co;24112412local$/="\0";24132414open my$fd,"-|", git_cmd(),"rev-list",2415"--parents",2416"--header",2417"--max-count=1",2418$commit_id,2419"--",2420or die_error(500,"Open git-rev-list failed");2421%co= parse_commit_text(<$fd>,1);2422close$fd;24232424return%co;2425}24262427sub parse_commits {2428my($commit_id,$maxcount,$skip,$filename,@args) =@_;2429my@cos;24302431$maxcount||=1;2432$skip||=0;24332434local$/="\0";24352436open my$fd,"-|", git_cmd(),"rev-list",2437"--header",2438@args,2439("--max-count=".$maxcount),2440("--skip=".$skip),2441@extra_options,2442$commit_id,2443"--",2444($filename? ($filename) : ())2445or die_error(500,"Open git-rev-list failed");2446while(my$line= <$fd>) {2447my%co= parse_commit_text($line);2448push@cos, \%co;2449}2450close$fd;24512452returnwantarray?@cos: \@cos;2453}24542455# parse line of git-diff-tree "raw" output2456sub parse_difftree_raw_line {2457my$line=shift;2458my%res;24592460# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2461# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2462if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2463$res{'from_mode'} =$1;2464$res{'to_mode'} =$2;2465$res{'from_id'} =$3;2466$res{'to_id'} =$4;2467$res{'status'} =$5;2468$res{'similarity'} =$6;2469if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2470($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2471}else{2472$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2473}2474}2475# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2476# combined diff (for merge commit)2477elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2478$res{'nparents'} =length($1);2479$res{'from_mode'} = [split(' ',$2) ];2480$res{'to_mode'} =pop@{$res{'from_mode'}};2481$res{'from_id'} = [split(' ',$3) ];2482$res{'to_id'} =pop@{$res{'from_id'}};2483$res{'status'} = [split('',$4) ];2484$res{'to_file'} = unquote($5);2485}2486# 'c512b523472485aef4fff9e57b229d9d243c967f'2487elsif($line=~m/^([0-9a-fA-F]{40})$/) {2488$res{'commit'} =$1;2489}24902491returnwantarray?%res: \%res;2492}24932494# wrapper: return parsed line of git-diff-tree "raw" output2495# (the argument might be raw line, or parsed info)2496sub parsed_difftree_line {2497my$line_or_ref=shift;24982499if(ref($line_or_ref)eq"HASH") {2500# pre-parsed (or generated by hand)2501return$line_or_ref;2502}else{2503return parse_difftree_raw_line($line_or_ref);2504}2505}25062507# parse line of git-ls-tree output2508sub parse_ls_tree_line ($;%) {2509my$line=shift;2510my%opts=@_;2511my%res;25122513#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2514$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;25152516$res{'mode'} =$1;2517$res{'type'} =$2;2518$res{'hash'} =$3;2519if($opts{'-z'}) {2520$res{'name'} =$4;2521}else{2522$res{'name'} = unquote($4);2523}25242525returnwantarray?%res: \%res;2526}25272528# generates _two_ hashes, references to which are passed as 2 and 3 argument2529sub parse_from_to_diffinfo {2530my($diffinfo,$from,$to,@parents) =@_;25312532if($diffinfo->{'nparents'}) {2533# combined diff2534$from->{'file'} = [];2535$from->{'href'} = [];2536 fill_from_file_info($diffinfo,@parents)2537unlessexists$diffinfo->{'from_file'};2538for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2539$from->{'file'}[$i] =2540defined$diffinfo->{'from_file'}[$i] ?2541$diffinfo->{'from_file'}[$i] :2542$diffinfo->{'to_file'};2543if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2544$from->{'href'}[$i] = href(action=>"blob",2545 hash_base=>$parents[$i],2546 hash=>$diffinfo->{'from_id'}[$i],2547 file_name=>$from->{'file'}[$i]);2548}else{2549$from->{'href'}[$i] =undef;2550}2551}2552}else{2553# ordinary (not combined) diff2554$from->{'file'} =$diffinfo->{'from_file'};2555if($diffinfo->{'status'}ne"A") {# not new (added) file2556$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2557 hash=>$diffinfo->{'from_id'},2558 file_name=>$from->{'file'});2559}else{2560delete$from->{'href'};2561}2562}25632564$to->{'file'} =$diffinfo->{'to_file'};2565if(!is_deleted($diffinfo)) {# file exists in result2566$to->{'href'} = href(action=>"blob", hash_base=>$hash,2567 hash=>$diffinfo->{'to_id'},2568 file_name=>$to->{'file'});2569}else{2570delete$to->{'href'};2571}2572}25732574## ......................................................................2575## parse to array of hashes functions25762577sub git_get_heads_list {2578my$limit=shift;2579my@headslist;25802581open my$fd,'-|', git_cmd(),'for-each-ref',2582($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2583'--format=%(objectname) %(refname) %(subject)%00%(committer)',2584'refs/heads'2585orreturn;2586while(my$line= <$fd>) {2587my%ref_item;25882589chomp$line;2590my($refinfo,$committerinfo) =split(/\0/,$line);2591my($hash,$name,$title) =split(' ',$refinfo,3);2592my($committer,$epoch,$tz) =2593($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2594$ref_item{'fullname'} =$name;2595$name=~s!^refs/heads/!!;25962597$ref_item{'name'} =$name;2598$ref_item{'id'} =$hash;2599$ref_item{'title'} =$title||'(no commit message)';2600$ref_item{'epoch'} =$epoch;2601if($epoch) {2602$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2603}else{2604$ref_item{'age'} ="unknown";2605}26062607push@headslist, \%ref_item;2608}2609close$fd;26102611returnwantarray?@headslist: \@headslist;2612}26132614sub git_get_tags_list {2615my$limit=shift;2616my@tagslist;26172618open my$fd,'-|', git_cmd(),'for-each-ref',2619($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2620'--format=%(objectname) %(objecttype) %(refname) '.2621'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2622'refs/tags'2623orreturn;2624while(my$line= <$fd>) {2625my%ref_item;26262627chomp$line;2628my($refinfo,$creatorinfo) =split(/\0/,$line);2629my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2630my($creator,$epoch,$tz) =2631($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2632$ref_item{'fullname'} =$name;2633$name=~s!^refs/tags/!!;26342635$ref_item{'type'} =$type;2636$ref_item{'id'} =$id;2637$ref_item{'name'} =$name;2638if($typeeq"tag") {2639$ref_item{'subject'} =$title;2640$ref_item{'reftype'} =$reftype;2641$ref_item{'refid'} =$refid;2642}else{2643$ref_item{'reftype'} =$type;2644$ref_item{'refid'} =$id;2645}26462647if($typeeq"tag"||$typeeq"commit") {2648$ref_item{'epoch'} =$epoch;2649if($epoch) {2650$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2651}else{2652$ref_item{'age'} ="unknown";2653}2654}26552656push@tagslist, \%ref_item;2657}2658close$fd;26592660returnwantarray?@tagslist: \@tagslist;2661}26622663## ----------------------------------------------------------------------2664## filesystem-related functions26652666sub get_file_owner {2667my$path=shift;26682669my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2670my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2671if(!defined$gcos) {2672returnundef;2673}2674my$owner=$gcos;2675$owner=~s/[,;].*$//;2676return to_utf8($owner);2677}26782679## ......................................................................2680## mimetype related functions26812682sub mimetype_guess_file {2683my$filename=shift;2684my$mimemap=shift;2685-r $mimemaporreturnundef;26862687my%mimemap;2688open(MIME,$mimemap)orreturnundef;2689while(<MIME>) {2690next ifm/^#/;# skip comments2691my($mime,$exts) =split(/\t+/);2692if(defined$exts) {2693my@exts=split(/\s+/,$exts);2694foreachmy$ext(@exts) {2695$mimemap{$ext} =$mime;2696}2697}2698}2699close(MIME);27002701$filename=~/\.([^.]*)$/;2702return$mimemap{$1};2703}27042705sub mimetype_guess {2706my$filename=shift;2707my$mime;2708$filename=~/\./orreturnundef;27092710if($mimetypes_file) {2711my$file=$mimetypes_file;2712if($file!~m!^/!) {# if it is relative path2713# it is relative to project2714$file="$projectroot/$project/$file";2715}2716$mime= mimetype_guess_file($filename,$file);2717}2718$mime||= mimetype_guess_file($filename,'/etc/mime.types');2719return$mime;2720}27212722sub blob_mimetype {2723my$fd=shift;2724my$filename=shift;27252726if($filename) {2727my$mime= mimetype_guess($filename);2728$mimeandreturn$mime;2729}27302731# just in case2732return$default_blob_plain_mimetypeunless$fd;27332734if(-T $fd) {2735return'text/plain';2736}elsif(!$filename) {2737return'application/octet-stream';2738}elsif($filename=~m/\.png$/i) {2739return'image/png';2740}elsif($filename=~m/\.gif$/i) {2741return'image/gif';2742}elsif($filename=~m/\.jpe?g$/i) {2743return'image/jpeg';2744}else{2745return'application/octet-stream';2746}2747}27482749sub blob_contenttype {2750my($fd,$file_name,$type) =@_;27512752$type||= blob_mimetype($fd,$file_name);2753if($typeeq'text/plain'&&defined$default_text_plain_charset) {2754$type.="; charset=$default_text_plain_charset";2755}27562757return$type;2758}27592760## ======================================================================2761## functions printing HTML: header, footer, error page27622763sub git_header_html {2764my$status=shift||"200 OK";2765my$expires=shift;27662767my$title="$site_name";2768if(defined$project) {2769$title.=" - ". to_utf8($project);2770if(defined$action) {2771$title.="/$action";2772if(defined$file_name) {2773$title.=" - ". esc_path($file_name);2774if($actioneq"tree"&&$file_name!~ m|/$|) {2775$title.="/";2776}2777}2778}2779}2780my$content_type;2781# require explicit support from the UA if we are to send the page as2782# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2783# we have to do this because MSIE sometimes globs '*/*', pretending to2784# support xhtml+xml but choking when it gets what it asked for.2785if(defined$cgi->http('HTTP_ACCEPT') &&2786$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2787$cgi->Accept('application/xhtml+xml') !=0) {2788$content_type='application/xhtml+xml';2789}else{2790$content_type='text/html';2791}2792print$cgi->header(-type=>$content_type, -charset =>'utf-8',2793-status=>$status, -expires =>$expires);2794my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2795print<<EOF;2796<?xml version="1.0" encoding="utf-8"?>2797<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2798<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2799<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2800<!-- git core binaries version$git_version-->2801<head>2802<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2803<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2804<meta name="robots" content="index, nofollow"/>2805<title>$title</title>2806EOF2807# print out each stylesheet that exist2808if(defined$stylesheet) {2809#provides backwards capability for those people who define style sheet in a config file2810print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2811}else{2812foreachmy$stylesheet(@stylesheets) {2813next unless$stylesheet;2814print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2815}2816}2817if(defined$project) {2818my%href_params= get_feed_info();2819if(!exists$href_params{'-title'}) {2820$href_params{'-title'} ='log';2821}28222823foreachmy$formatqw(RSS Atom){2824my$type=lc($format);2825my%link_attr= (2826'-rel'=>'alternate',2827'-title'=>"$project-$href_params{'-title'} -$formatfeed",2828'-type'=>"application/$type+xml"2829);28302831$href_params{'action'} =$type;2832$link_attr{'-href'} = href(%href_params);2833print"<link ".2834"rel=\"$link_attr{'-rel'}\"".2835"title=\"$link_attr{'-title'}\"".2836"href=\"$link_attr{'-href'}\"".2837"type=\"$link_attr{'-type'}\"".2838"/>\n";28392840$href_params{'extra_options'} ='--no-merges';2841$link_attr{'-href'} = href(%href_params);2842$link_attr{'-title'} .=' (no merges)';2843print"<link ".2844"rel=\"$link_attr{'-rel'}\"".2845"title=\"$link_attr{'-title'}\"".2846"href=\"$link_attr{'-href'}\"".2847"type=\"$link_attr{'-type'}\"".2848"/>\n";2849}28502851}else{2852printf('<link rel="alternate" title="%sprojects list" '.2853'href="%s" type="text/plain; charset=utf-8" />'."\n",2854$site_name, href(project=>undef, action=>"project_index"));2855printf('<link rel="alternate" title="%sprojects feeds" '.2856'href="%s" type="text/x-opml" />'."\n",2857$site_name, href(project=>undef, action=>"opml"));2858}2859if(defined$favicon) {2860printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2861}28622863print"</head>\n".2864"<body>\n";28652866if(-f $site_header) {2867open(my$fd,$site_header);2868print<$fd>;2869close$fd;2870}28712872print"<div class=\"page_header\">\n".2873$cgi->a({-href => esc_url($logo_url),2874-title =>$logo_label},2875qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2876print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2877if(defined$project) {2878print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2879if(defined$action) {2880print" /$action";2881}2882print"\n";2883}2884print"</div>\n";28852886my($have_search) = gitweb_check_feature('search');2887if(defined$project&&$have_search) {2888if(!defined$searchtext) {2889$searchtext="";2890}2891my$search_hash;2892if(defined$hash_base) {2893$search_hash=$hash_base;2894}elsif(defined$hash) {2895$search_hash=$hash;2896}else{2897$search_hash="HEAD";2898}2899my$action=$my_uri;2900my($use_pathinfo) = gitweb_check_feature('pathinfo');2901if($use_pathinfo) {2902$action.="/".esc_url($project);2903}2904print$cgi->startform(-method=>"get", -action =>$action) .2905"<div class=\"search\">\n".2906(!$use_pathinfo&&2907$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .2908$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".2909$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".2910$cgi->popup_menu(-name =>'st', -default=>'commit',2911-values=> ['commit','grep','author','committer','pickaxe']) .2912$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .2913" search:\n",2914$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".2915"<span title=\"Extended regular expression\">".2916$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',2917-checked =>$search_use_regexp) .2918"</span>".2919"</div>".2920$cgi->end_form() ."\n";2921}2922}29232924sub git_footer_html {2925my$feed_class='rss_logo';29262927print"<div class=\"page_footer\">\n";2928if(defined$project) {2929my$descr= git_get_project_description($project);2930if(defined$descr) {2931print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";2932}29332934my%href_params= get_feed_info();2935if(!%href_params) {2936$feed_class.=' generic';2937}2938$href_params{'-title'} ||='log';29392940foreachmy$formatqw(RSS Atom){2941$href_params{'action'} =lc($format);2942print$cgi->a({-href => href(%href_params),2943-title =>"$href_params{'-title'}$formatfeed",2944-class=>$feed_class},$format)."\n";2945}29462947}else{2948print$cgi->a({-href => href(project=>undef, action=>"opml"),2949-class=>$feed_class},"OPML") ." ";2950print$cgi->a({-href => href(project=>undef, action=>"project_index"),2951-class=>$feed_class},"TXT") ."\n";2952}2953print"</div>\n";# class="page_footer"29542955if(-f $site_footer) {2956open(my$fd,$site_footer);2957print<$fd>;2958close$fd;2959}29602961print"</body>\n".2962"</html>";2963}29642965# die_error(<http_status_code>, <error_message>)2966# Example: die_error(404, 'Hash not found')2967# By convention, use the following status codes (as defined in RFC 2616):2968# 400: Invalid or missing CGI parameters, or2969# requested object exists but has wrong type.2970# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on2971# this server or project.2972# 404: Requested object/revision/project doesn't exist.2973# 500: The server isn't configured properly, or2974# an internal error occurred (e.g. failed assertions caused by bugs), or2975# an unknown error occurred (e.g. the git binary died unexpectedly).2976sub die_error {2977my$status=shift||500;2978my$error=shift||"Internal server error";29792980my%http_responses= (400=>'400 Bad Request',2981403=>'403 Forbidden',2982404=>'404 Not Found',2983500=>'500 Internal Server Error');2984 git_header_html($http_responses{$status});2985print<<EOF;2986<div class="page_body">2987<br /><br />2988$status-$error2989<br />2990</div>2991EOF2992 git_footer_html();2993exit;2994}29952996## ----------------------------------------------------------------------2997## functions printing or outputting HTML: navigation29982999sub git_print_page_nav {3000my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3001$extra=''if!defined$extra;# pager or formats30023003my@navs=qw(summary shortlog log commit commitdiff tree);3004if($suppress) {3005@navs=grep{$_ne$suppress}@navs;3006}30073008my%arg=map{$_=> {action=>$_} }@navs;3009if(defined$head) {3010for(qw(commit commitdiff)) {3011$arg{$_}{'hash'} =$head;3012}3013if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3014for(qw(shortlog log)) {3015$arg{$_}{'hash'} =$head;3016}3017}3018}30193020$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3021$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;30223023my@actions= gitweb_check_feature('actions');3024while(@actions) {3025my($label,$link,$pos) = (shift(@actions),shift(@actions),shift(@actions));3026@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3027# munch munch3028$link=~ s#%n#$project#g;3029$link=~ s#%f#$git_dir#g;3030$treehead?$link=~ s#%h#$treehead#g : $link =~ s#%h##g;3031$treebase?$link=~ s#%b#$treebase#g : $link =~ s#%b##g;3032$arg{$label}{'_href'} =$link;3033}30343035print"<div class=\"page_nav\">\n".3036(join" | ",3037map{$_eq$current?3038$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3039}@navs);3040print"<br/>\n$extra<br/>\n".3041"</div>\n";3042}30433044sub format_paging_nav {3045my($action,$hash,$head,$page,$has_next_link) =@_;3046my$paging_nav;304730483049if($hashne$head||$page) {3050$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3051}else{3052$paging_nav.="HEAD";3053}30543055if($page>0) {3056$paging_nav.=" ⋅ ".3057$cgi->a({-href => href(-replay=>1, page=>$page-1),3058-accesskey =>"p", -title =>"Alt-p"},"prev");3059}else{3060$paging_nav.=" ⋅ prev";3061}30623063if($has_next_link) {3064$paging_nav.=" ⋅ ".3065$cgi->a({-href => href(-replay=>1, page=>$page+1),3066-accesskey =>"n", -title =>"Alt-n"},"next");3067}else{3068$paging_nav.=" ⋅ next";3069}30703071return$paging_nav;3072}30733074## ......................................................................3075## functions printing or outputting HTML: div30763077sub git_print_header_div {3078my($action,$title,$hash,$hash_base) =@_;3079my%args= ();30803081$args{'action'} =$action;3082$args{'hash'} =$hashif$hash;3083$args{'hash_base'} =$hash_baseif$hash_base;30843085print"<div class=\"header\">\n".3086$cgi->a({-href => href(%args), -class=>"title"},3087$title?$title:$action) .3088"\n</div>\n";3089}30903091#sub git_print_authorship (\%) {3092sub git_print_authorship {3093my$co=shift;30943095my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3096print"<div class=\"author_date\">".3097 esc_html($co->{'author_name'}) .3098" [$ad{'rfc2822'}";3099if($ad{'hour_local'} <6) {3100printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3101$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3102}else{3103printf(" (%02d:%02d%s)",3104$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3105}3106print"]</div>\n";3107}31083109sub git_print_page_path {3110my$name=shift;3111my$type=shift;3112my$hb=shift;311331143115print"<div class=\"page_path\">";3116print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3117-title =>'tree root'}, to_utf8("[$project]"));3118print" / ";3119if(defined$name) {3120my@dirname=split'/',$name;3121my$basename=pop@dirname;3122my$fullname='';31233124foreachmy$dir(@dirname) {3125$fullname.= ($fullname?'/':'') .$dir;3126print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3127 hash_base=>$hb),3128-title =>$fullname}, esc_path($dir));3129print" / ";3130}3131if(defined$type&&$typeeq'blob') {3132print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3133 hash_base=>$hb),3134-title =>$name}, esc_path($basename));3135}elsif(defined$type&&$typeeq'tree') {3136print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3137 hash_base=>$hb),3138-title =>$name}, esc_path($basename));3139print" / ";3140}else{3141print esc_path($basename);3142}3143}3144print"<br/></div>\n";3145}31463147# sub git_print_log (\@;%) {3148sub git_print_log ($;%) {3149my$log=shift;3150my%opts=@_;31513152if($opts{'-remove_title'}) {3153# remove title, i.e. first line of log3154shift@$log;3155}3156# remove leading empty lines3157while(defined$log->[0] &&$log->[0]eq"") {3158shift@$log;3159}31603161# print log3162my$signoff=0;3163my$empty=0;3164foreachmy$line(@$log) {3165if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3166$signoff=1;3167$empty=0;3168if(!$opts{'-remove_signoff'}) {3169print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3170next;3171}else{3172# remove signoff lines3173next;3174}3175}else{3176$signoff=0;3177}31783179# print only one empty line3180# do not print empty line after signoff3181if($lineeq"") {3182next if($empty||$signoff);3183$empty=1;3184}else{3185$empty=0;3186}31873188print format_log_line_html($line) ."<br/>\n";3189}31903191if($opts{'-final_empty_line'}) {3192# end with single empty line3193print"<br/>\n"unless$empty;3194}3195}31963197# return link target (what link points to)3198sub git_get_link_target {3199my$hash=shift;3200my$link_target;32013202# read link3203open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3204orreturn;3205{3206local$/;3207$link_target= <$fd>;3208}3209close$fd3210orreturn;32113212return$link_target;3213}32143215# given link target, and the directory (basedir) the link is in,3216# return target of link relative to top directory (top tree);3217# return undef if it is not possible (including absolute links).3218sub normalize_link_target {3219my($link_target,$basedir,$hash_base) =@_;32203221# we can normalize symlink target only if $hash_base is provided3222return unless$hash_base;32233224# absolute symlinks (beginning with '/') cannot be normalized3225return if(substr($link_target,0,1)eq'/');32263227# normalize link target to path from top (root) tree (dir)3228my$path;3229if($basedir) {3230$path=$basedir.'/'.$link_target;3231}else{3232# we are in top (root) tree (dir)3233$path=$link_target;3234}32353236# remove //, /./, and /../3237my@path_parts;3238foreachmy$part(split('/',$path)) {3239# discard '.' and ''3240next if(!$part||$parteq'.');3241# handle '..'3242if($parteq'..') {3243if(@path_parts) {3244pop@path_parts;3245}else{3246# link leads outside repository (outside top dir)3247return;3248}3249}else{3250push@path_parts,$part;3251}3252}3253$path=join('/',@path_parts);32543255return$path;3256}32573258# print tree entry (row of git_tree), but without encompassing <tr> element3259sub git_print_tree_entry {3260my($t,$basedir,$hash_base,$have_blame) =@_;32613262my%base_key= ();3263$base_key{'hash_base'} =$hash_baseifdefined$hash_base;32643265# The format of a table row is: mode list link. Where mode is3266# the mode of the entry, list is the name of the entry, an href,3267# and link is the action links of the entry.32683269print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3270if($t->{'type'}eq"blob") {3271print"<td class=\"list\">".3272$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3273 file_name=>"$basedir$t->{'name'}",%base_key),3274-class=>"list"}, esc_path($t->{'name'}));3275if(S_ISLNK(oct$t->{'mode'})) {3276my$link_target= git_get_link_target($t->{'hash'});3277if($link_target) {3278my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3279if(defined$norm_target) {3280print" -> ".3281$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3282 file_name=>$norm_target),3283-title =>$norm_target}, esc_path($link_target));3284}else{3285print" -> ". esc_path($link_target);3286}3287}3288}3289print"</td>\n";3290print"<td class=\"link\">";3291print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3292 file_name=>"$basedir$t->{'name'}",%base_key)},3293"blob");3294if($have_blame) {3295print" | ".3296$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3297 file_name=>"$basedir$t->{'name'}",%base_key)},3298"blame");3299}3300if(defined$hash_base) {3301print" | ".3302$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3303 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3304"history");3305}3306print" | ".3307$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3308 file_name=>"$basedir$t->{'name'}")},3309"raw");3310print"</td>\n";33113312}elsif($t->{'type'}eq"tree") {3313print"<td class=\"list\">";3314print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3315 file_name=>"$basedir$t->{'name'}",%base_key)},3316 esc_path($t->{'name'}));3317print"</td>\n";3318print"<td class=\"link\">";3319print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3320 file_name=>"$basedir$t->{'name'}",%base_key)},3321"tree");3322if(defined$hash_base) {3323print" | ".3324$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3325 file_name=>"$basedir$t->{'name'}")},3326"history");3327}3328print"</td>\n";3329}else{3330# unknown object: we can only present history for it3331# (this includes 'commit' object, i.e. submodule support)3332print"<td class=\"list\">".3333 esc_path($t->{'name'}) .3334"</td>\n";3335print"<td class=\"link\">";3336if(defined$hash_base) {3337print$cgi->a({-href => href(action=>"history",3338 hash_base=>$hash_base,3339 file_name=>"$basedir$t->{'name'}")},3340"history");3341}3342print"</td>\n";3343}3344}33453346## ......................................................................3347## functions printing large fragments of HTML33483349# get pre-image filenames for merge (combined) diff3350sub fill_from_file_info {3351my($diff,@parents) =@_;33523353$diff->{'from_file'} = [ ];3354$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3355for(my$i=0;$i<$diff->{'nparents'};$i++) {3356if($diff->{'status'}[$i]eq'R'||3357$diff->{'status'}[$i]eq'C') {3358$diff->{'from_file'}[$i] =3359 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3360}3361}33623363return$diff;3364}33653366# is current raw difftree line of file deletion3367sub is_deleted {3368my$diffinfo=shift;33693370return$diffinfo->{'to_id'}eq('0' x 40);3371}33723373# does patch correspond to [previous] difftree raw line3374# $diffinfo - hashref of parsed raw diff format3375# $patchinfo - hashref of parsed patch diff format3376# (the same keys as in $diffinfo)3377sub is_patch_split {3378my($diffinfo,$patchinfo) =@_;33793380returndefined$diffinfo&&defined$patchinfo3381&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3382}338333843385sub git_difftree_body {3386my($difftree,$hash,@parents) =@_;3387my($parent) =$parents[0];3388my($have_blame) = gitweb_check_feature('blame');3389print"<div class=\"list_head\">\n";3390if($#{$difftree} >10) {3391print(($#{$difftree} +1) ." files changed:\n");3392}3393print"</div>\n";33943395print"<table class=\"".3396(@parents>1?"combined ":"") .3397"diff_tree\">\n";33983399# header only for combined diff in 'commitdiff' view3400my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3401if($has_header) {3402# table header3403print"<thead><tr>\n".3404"<th></th><th></th>\n";# filename, patchN link3405for(my$i=0;$i<@parents;$i++) {3406my$par=$parents[$i];3407print"<th>".3408$cgi->a({-href => href(action=>"commitdiff",3409 hash=>$hash, hash_parent=>$par),3410-title =>'commitdiff to parent number '.3411($i+1) .': '.substr($par,0,7)},3412$i+1) .3413" </th>\n";3414}3415print"</tr></thead>\n<tbody>\n";3416}34173418my$alternate=1;3419my$patchno=0;3420foreachmy$line(@{$difftree}) {3421my$diff= parsed_difftree_line($line);34223423if($alternate) {3424print"<tr class=\"dark\">\n";3425}else{3426print"<tr class=\"light\">\n";3427}3428$alternate^=1;34293430if(exists$diff->{'nparents'}) {# combined diff34313432 fill_from_file_info($diff,@parents)3433unlessexists$diff->{'from_file'};34343435if(!is_deleted($diff)) {3436# file exists in the result (child) commit3437print"<td>".3438$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3439 file_name=>$diff->{'to_file'},3440 hash_base=>$hash),3441-class=>"list"}, esc_path($diff->{'to_file'})) .3442"</td>\n";3443}else{3444print"<td>".3445 esc_path($diff->{'to_file'}) .3446"</td>\n";3447}34483449if($actioneq'commitdiff') {3450# link to patch3451$patchno++;3452print"<td class=\"link\">".3453$cgi->a({-href =>"#patch$patchno"},"patch") .3454" | ".3455"</td>\n";3456}34573458my$has_history=0;3459my$not_deleted=0;3460for(my$i=0;$i<$diff->{'nparents'};$i++) {3461my$hash_parent=$parents[$i];3462my$from_hash=$diff->{'from_id'}[$i];3463my$from_path=$diff->{'from_file'}[$i];3464my$status=$diff->{'status'}[$i];34653466$has_history||= ($statusne'A');3467$not_deleted||= ($statusne'D');34683469if($statuseq'A') {3470print"<td class=\"link\"align=\"right\"> | </td>\n";3471}elsif($statuseq'D') {3472print"<td class=\"link\">".3473$cgi->a({-href => href(action=>"blob",3474 hash_base=>$hash,3475 hash=>$from_hash,3476 file_name=>$from_path)},3477"blob". ($i+1)) .3478" | </td>\n";3479}else{3480if($diff->{'to_id'}eq$from_hash) {3481print"<td class=\"link nochange\">";3482}else{3483print"<td class=\"link\">";3484}3485print$cgi->a({-href => href(action=>"blobdiff",3486 hash=>$diff->{'to_id'},3487 hash_parent=>$from_hash,3488 hash_base=>$hash,3489 hash_parent_base=>$hash_parent,3490 file_name=>$diff->{'to_file'},3491 file_parent=>$from_path)},3492"diff". ($i+1)) .3493" | </td>\n";3494}3495}34963497print"<td class=\"link\">";3498if($not_deleted) {3499print$cgi->a({-href => href(action=>"blob",3500 hash=>$diff->{'to_id'},3501 file_name=>$diff->{'to_file'},3502 hash_base=>$hash)},3503"blob");3504print" | "if($has_history);3505}3506if($has_history) {3507print$cgi->a({-href => href(action=>"history",3508 file_name=>$diff->{'to_file'},3509 hash_base=>$hash)},3510"history");3511}3512print"</td>\n";35133514print"</tr>\n";3515next;# instead of 'else' clause, to avoid extra indent3516}3517# else ordinary diff35183519my($to_mode_oct,$to_mode_str,$to_file_type);3520my($from_mode_oct,$from_mode_str,$from_file_type);3521if($diff->{'to_mode'}ne('0' x 6)) {3522$to_mode_oct=oct$diff->{'to_mode'};3523if(S_ISREG($to_mode_oct)) {# only for regular file3524$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3525}3526$to_file_type= file_type($diff->{'to_mode'});3527}3528if($diff->{'from_mode'}ne('0' x 6)) {3529$from_mode_oct=oct$diff->{'from_mode'};3530if(S_ISREG($to_mode_oct)) {# only for regular file3531$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3532}3533$from_file_type= file_type($diff->{'from_mode'});3534}35353536if($diff->{'status'}eq"A") {# created3537my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3538$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3539$mode_chng.="]</span>";3540print"<td>";3541print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3542 hash_base=>$hash, file_name=>$diff->{'file'}),3543-class=>"list"}, esc_path($diff->{'file'}));3544print"</td>\n";3545print"<td>$mode_chng</td>\n";3546print"<td class=\"link\">";3547if($actioneq'commitdiff') {3548# link to patch3549$patchno++;3550print$cgi->a({-href =>"#patch$patchno"},"patch");3551print" | ";3552}3553print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3554 hash_base=>$hash, file_name=>$diff->{'file'})},3555"blob");3556print"</td>\n";35573558}elsif($diff->{'status'}eq"D") {# deleted3559my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3560print"<td>";3561print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3562 hash_base=>$parent, file_name=>$diff->{'file'}),3563-class=>"list"}, esc_path($diff->{'file'}));3564print"</td>\n";3565print"<td>$mode_chng</td>\n";3566print"<td class=\"link\">";3567if($actioneq'commitdiff') {3568# link to patch3569$patchno++;3570print$cgi->a({-href =>"#patch$patchno"},"patch");3571print" | ";3572}3573print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3574 hash_base=>$parent, file_name=>$diff->{'file'})},3575"blob") ." | ";3576if($have_blame) {3577print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3578 file_name=>$diff->{'file'})},3579"blame") ." | ";3580}3581print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3582 file_name=>$diff->{'file'})},3583"history");3584print"</td>\n";35853586}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3587my$mode_chnge="";3588if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3589$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3590if($from_file_typene$to_file_type) {3591$mode_chnge.=" from$from_file_typeto$to_file_type";3592}3593if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3594if($from_mode_str&&$to_mode_str) {3595$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3596}elsif($to_mode_str) {3597$mode_chnge.=" mode:$to_mode_str";3598}3599}3600$mode_chnge.="]</span>\n";3601}3602print"<td>";3603print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3604 hash_base=>$hash, file_name=>$diff->{'file'}),3605-class=>"list"}, esc_path($diff->{'file'}));3606print"</td>\n";3607print"<td>$mode_chnge</td>\n";3608print"<td class=\"link\">";3609if($actioneq'commitdiff') {3610# link to patch3611$patchno++;3612print$cgi->a({-href =>"#patch$patchno"},"patch") .3613" | ";3614}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3615# "commit" view and modified file (not onlu mode changed)3616print$cgi->a({-href => href(action=>"blobdiff",3617 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3618 hash_base=>$hash, hash_parent_base=>$parent,3619 file_name=>$diff->{'file'})},3620"diff") .3621" | ";3622}3623print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3624 hash_base=>$hash, file_name=>$diff->{'file'})},3625"blob") ." | ";3626if($have_blame) {3627print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3628 file_name=>$diff->{'file'})},3629"blame") ." | ";3630}3631print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3632 file_name=>$diff->{'file'})},3633"history");3634print"</td>\n";36353636}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3637my%status_name= ('R'=>'moved','C'=>'copied');3638my$nstatus=$status_name{$diff->{'status'}};3639my$mode_chng="";3640if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3641# mode also for directories, so we cannot use $to_mode_str3642$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3643}3644print"<td>".3645$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3646 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3647-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3648"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3649$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3650 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3651-class=>"list"}, esc_path($diff->{'from_file'})) .3652" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3653"<td class=\"link\">";3654if($actioneq'commitdiff') {3655# link to patch3656$patchno++;3657print$cgi->a({-href =>"#patch$patchno"},"patch") .3658" | ";3659}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3660# "commit" view and modified file (not only pure rename or copy)3661print$cgi->a({-href => href(action=>"blobdiff",3662 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3663 hash_base=>$hash, hash_parent_base=>$parent,3664 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3665"diff") .3666" | ";3667}3668print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3669 hash_base=>$parent, file_name=>$diff->{'to_file'})},3670"blob") ." | ";3671if($have_blame) {3672print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3673 file_name=>$diff->{'to_file'})},3674"blame") ." | ";3675}3676print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3677 file_name=>$diff->{'to_file'})},3678"history");3679print"</td>\n";36803681}# we should not encounter Unmerged (U) or Unknown (X) status3682print"</tr>\n";3683}3684print"</tbody>"if$has_header;3685print"</table>\n";3686}36873688sub git_patchset_body {3689my($fd,$difftree,$hash,@hash_parents) =@_;3690my($hash_parent) =$hash_parents[0];36913692my$is_combined= (@hash_parents>1);3693my$patch_idx=0;3694my$patch_number=0;3695my$patch_line;3696my$diffinfo;3697my$to_name;3698my(%from,%to);36993700print"<div class=\"patchset\">\n";37013702# skip to first patch3703while($patch_line= <$fd>) {3704chomp$patch_line;37053706last if($patch_line=~m/^diff /);3707}37083709 PATCH:3710while($patch_line) {37113712# parse "git diff" header line3713if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3714# $1 is from_name, which we do not use3715$to_name= unquote($2);3716$to_name=~s!^b/!!;3717}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3718# $1 is 'cc' or 'combined', which we do not use3719$to_name= unquote($2);3720}else{3721$to_name=undef;3722}37233724# check if current patch belong to current raw line3725# and parse raw git-diff line if needed3726if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3727# this is continuation of a split patch3728print"<div class=\"patch cont\">\n";3729}else{3730# advance raw git-diff output if needed3731$patch_idx++ifdefined$diffinfo;37323733# read and prepare patch information3734$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);37353736# compact combined diff output can have some patches skipped3737# find which patch (using pathname of result) we are at now;3738if($is_combined) {3739while($to_namene$diffinfo->{'to_file'}) {3740print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3741 format_diff_cc_simplified($diffinfo,@hash_parents) .3742"</div>\n";# class="patch"37433744$patch_idx++;3745$patch_number++;37463747last if$patch_idx>$#$difftree;3748$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3749}3750}37513752# modifies %from, %to hashes3753 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);37543755# this is first patch for raw difftree line with $patch_idx index3756# we index @$difftree array from 0, but number patches from 13757print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3758}37593760# git diff header3761#assert($patch_line =~ m/^diff /) if DEBUG;3762#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3763$patch_number++;3764# print "git diff" header3765print format_git_diff_header_line($patch_line,$diffinfo,3766 \%from, \%to);37673768# print extended diff header3769print"<div class=\"diff extended_header\">\n";3770 EXTENDED_HEADER:3771while($patch_line= <$fd>) {3772chomp$patch_line;37733774last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);37753776print format_extended_diff_header_line($patch_line,$diffinfo,3777 \%from, \%to);3778}3779print"</div>\n";# class="diff extended_header"37803781# from-file/to-file diff header3782if(!$patch_line) {3783print"</div>\n";# class="patch"3784last PATCH;3785}3786next PATCH if($patch_line=~m/^diff /);3787#assert($patch_line =~ m/^---/) if DEBUG;37883789my$last_patch_line=$patch_line;3790$patch_line= <$fd>;3791chomp$patch_line;3792#assert($patch_line =~ m/^\+\+\+/) if DEBUG;37933794print format_diff_from_to_header($last_patch_line,$patch_line,3795$diffinfo, \%from, \%to,3796@hash_parents);37973798# the patch itself3799 LINE:3800while($patch_line= <$fd>) {3801chomp$patch_line;38023803next PATCH if($patch_line=~m/^diff /);38043805print format_diff_line($patch_line, \%from, \%to);3806}38073808}continue{3809print"</div>\n";# class="patch"3810}38113812# for compact combined (--cc) format, with chunk and patch simpliciaction3813# patchset might be empty, but there might be unprocessed raw lines3814for(++$patch_idxif$patch_number>0;3815$patch_idx<@$difftree;3816++$patch_idx) {3817# read and prepare patch information3818$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38193820# generate anchor for "patch" links in difftree / whatchanged part3821print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3822 format_diff_cc_simplified($diffinfo,@hash_parents) .3823"</div>\n";# class="patch"38243825$patch_number++;3826}38273828if($patch_number==0) {3829if(@hash_parents>1) {3830print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3831}else{3832print"<div class=\"diff nodifferences\">No differences found</div>\n";3833}3834}38353836print"</div>\n";# class="patchset"3837}38383839# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38403841# fills project list info (age, description, owner, forks) for each3842# project in the list, removing invalid projects from returned list3843# NOTE: modifies $projlist, but does not remove entries from it3844sub fill_project_list_info {3845my($projlist,$check_forks) =@_;3846my@projects;38473848my$show_ctags= gitweb_check_feature('ctags');3849 PROJECT:3850foreachmy$pr(@$projlist) {3851my(@activity) = git_get_last_activity($pr->{'path'});3852unless(@activity) {3853next PROJECT;3854}3855($pr->{'age'},$pr->{'age_string'}) =@activity;3856if(!defined$pr->{'descr'}) {3857my$descr= git_get_project_description($pr->{'path'}) ||"";3858$descr= to_utf8($descr);3859$pr->{'descr_long'} =$descr;3860$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3861}3862if(!defined$pr->{'owner'}) {3863$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3864}3865if($check_forks) {3866my$pname=$pr->{'path'};3867if(($pname=~s/\.git$//) &&3868($pname!~/\/$/) &&3869(-d "$projectroot/$pname")) {3870$pr->{'forks'} ="-d$projectroot/$pname";3871}else{3872$pr->{'forks'} =0;3873}3874}3875$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3876push@projects,$pr;3877}38783879return@projects;3880}38813882# print 'sort by' <th> element, generating 'sort by $name' replay link3883# if that order is not selected3884sub print_sort_th {3885my($name,$order,$header) =@_;3886$header||=ucfirst($name);38873888if($ordereq$name) {3889print"<th>$header</th>\n";3890}else{3891print"<th>".3892$cgi->a({-href => href(-replay=>1, order=>$name),3893-class=>"header"},$header) .3894"</th>\n";3895}3896}38973898sub git_project_list_body {3899# actually uses global variable $project3900my($projlist,$order,$from,$to,$extra,$no_header) =@_;39013902my($check_forks) = gitweb_check_feature('forks');3903my@projects= fill_project_list_info($projlist,$check_forks);39043905$order||=$default_projects_order;3906$from=0unlessdefined$from;3907$to=$#projectsif(!defined$to||$#projects<$to);39083909my%order_info= (3910 project => { key =>'path', type =>'str'},3911 descr => { key =>'descr_long', type =>'str'},3912 owner => { key =>'owner', type =>'str'},3913 age => { key =>'age', type =>'num'}3914);3915my$oi=$order_info{$order};3916if($oi->{'type'}eq'str') {3917@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;3918}else{3919@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;3920}39213922my$show_ctags= gitweb_check_feature('ctags');3923if($show_ctags) {3924my%ctags;3925foreachmy$p(@projects) {3926foreachmy$ct(keys%{$p->{'ctags'}}) {3927$ctags{$ct} +=$p->{'ctags'}->{$ct};3928}3929}3930my$cloud= git_populate_project_tagcloud(\%ctags);3931print git_show_project_tagcloud($cloud,64);3932}39333934print"<table class=\"project_list\">\n";3935unless($no_header) {3936print"<tr>\n";3937if($check_forks) {3938print"<th></th>\n";3939}3940 print_sort_th('project',$order,'Project');3941 print_sort_th('descr',$order,'Description');3942 print_sort_th('owner',$order,'Owner');3943 print_sort_th('age',$order,'Last Change');3944print"<th></th>\n".# for links3945"</tr>\n";3946}3947my$alternate=1;3948my$tagfilter=$cgi->param('by_tag');3949for(my$i=$from;$i<=$to;$i++) {3950my$pr=$projects[$i];39513952next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};3953next if$searchtextand not$pr->{'path'} =~/$searchtext/3954and not$pr->{'descr_long'} =~/$searchtext/;3955# Weed out forks or non-matching entries of search3956if($check_forks) {3957my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;3958$forkbase="^$forkbase"if$forkbase;3959next ifnot$searchtextand not$tagfilterand$show_ctags3960and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe3961}39623963if($alternate) {3964print"<tr class=\"dark\">\n";3965}else{3966print"<tr class=\"light\">\n";3967}3968$alternate^=1;3969if($check_forks) {3970print"<td>";3971if($pr->{'forks'}) {3972print"<!--$pr->{'forks'} -->\n";3973print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");3974}3975print"</td>\n";3976}3977print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3978-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".3979"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3980-class=>"list", -title =>$pr->{'descr_long'}},3981 esc_html($pr->{'descr'})) ."</td>\n".3982"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";3983print"<td class=\"". age_class($pr->{'age'}) ."\">".3984(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".3985"<td class=\"link\">".3986$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".3987$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".3988$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".3989$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .3990($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .3991"</td>\n".3992"</tr>\n";3993}3994if(defined$extra) {3995print"<tr>\n";3996if($check_forks) {3997print"<td></td>\n";3998}3999print"<td colspan=\"5\">$extra</td>\n".4000"</tr>\n";4001}4002print"</table>\n";4003}40044005sub git_shortlog_body {4006# uses global variable $project4007my($commitlist,$from,$to,$refs,$extra) =@_;40084009$from=0unlessdefined$from;4010$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);40114012print"<table class=\"shortlog\">\n";4013my$alternate=1;4014for(my$i=$from;$i<=$to;$i++) {4015my%co= %{$commitlist->[$i]};4016my$commit=$co{'id'};4017my$ref= format_ref_marker($refs,$commit);4018if($alternate) {4019print"<tr class=\"dark\">\n";4020}else{4021print"<tr class=\"light\">\n";4022}4023$alternate^=1;4024my$author= chop_and_escape_str($co{'author_name'},10);4025# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4026print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4027"<td><i>".$author."</i></td>\n".4028"<td>";4029print format_subject_html($co{'title'},$co{'title_short'},4030 href(action=>"commit", hash=>$commit),$ref);4031print"</td>\n".4032"<td class=\"link\">".4033$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4034$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4035$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4036my$snapshot_links= format_snapshot_links($commit);4037if(defined$snapshot_links) {4038print" | ".$snapshot_links;4039}4040print"</td>\n".4041"</tr>\n";4042}4043if(defined$extra) {4044print"<tr>\n".4045"<td colspan=\"4\">$extra</td>\n".4046"</tr>\n";4047}4048print"</table>\n";4049}40504051sub git_history_body {4052# Warning: assumes constant type (blob or tree) during history4053my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;40544055$from=0unlessdefined$from;4056$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});40574058print"<table class=\"history\">\n";4059my$alternate=1;4060for(my$i=$from;$i<=$to;$i++) {4061my%co= %{$commitlist->[$i]};4062if(!%co) {4063next;4064}4065my$commit=$co{'id'};40664067my$ref= format_ref_marker($refs,$commit);40684069if($alternate) {4070print"<tr class=\"dark\">\n";4071}else{4072print"<tr class=\"light\">\n";4073}4074$alternate^=1;4075# shortlog uses chop_str($co{'author_name'}, 10)4076my$author= chop_and_escape_str($co{'author_name'},15,3);4077print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4078"<td><i>".$author."</i></td>\n".4079"<td>";4080# originally git_history used chop_str($co{'title'}, 50)4081print format_subject_html($co{'title'},$co{'title_short'},4082 href(action=>"commit", hash=>$commit),$ref);4083print"</td>\n".4084"<td class=\"link\">".4085$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4086$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");40874088if($ftypeeq'blob') {4089my$blob_current= git_get_hash_by_path($hash_base,$file_name);4090my$blob_parent= git_get_hash_by_path($commit,$file_name);4091if(defined$blob_current&&defined$blob_parent&&4092$blob_currentne$blob_parent) {4093print" | ".4094$cgi->a({-href => href(action=>"blobdiff",4095 hash=>$blob_current, hash_parent=>$blob_parent,4096 hash_base=>$hash_base, hash_parent_base=>$commit,4097 file_name=>$file_name)},4098"diff to current");4099}4100}4101print"</td>\n".4102"</tr>\n";4103}4104if(defined$extra) {4105print"<tr>\n".4106"<td colspan=\"4\">$extra</td>\n".4107"</tr>\n";4108}4109print"</table>\n";4110}41114112sub git_tags_body {4113# uses global variable $project4114my($taglist,$from,$to,$extra) =@_;4115$from=0unlessdefined$from;4116$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);41174118print"<table class=\"tags\">\n";4119my$alternate=1;4120for(my$i=$from;$i<=$to;$i++) {4121my$entry=$taglist->[$i];4122my%tag=%$entry;4123my$comment=$tag{'subject'};4124my$comment_short;4125if(defined$comment) {4126$comment_short= chop_str($comment,30,5);4127}4128if($alternate) {4129print"<tr class=\"dark\">\n";4130}else{4131print"<tr class=\"light\">\n";4132}4133$alternate^=1;4134if(defined$tag{'age'}) {4135print"<td><i>$tag{'age'}</i></td>\n";4136}else{4137print"<td></td>\n";4138}4139print"<td>".4140$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4141-class=>"list name"}, esc_html($tag{'name'})) .4142"</td>\n".4143"<td>";4144if(defined$comment) {4145print format_subject_html($comment,$comment_short,4146 href(action=>"tag", hash=>$tag{'id'}));4147}4148print"</td>\n".4149"<td class=\"selflink\">";4150if($tag{'type'}eq"tag") {4151print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4152}else{4153print" ";4154}4155print"</td>\n".4156"<td class=\"link\">"." | ".4157$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4158if($tag{'reftype'}eq"commit") {4159print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4160" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4161}elsif($tag{'reftype'}eq"blob") {4162print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4163}4164print"</td>\n".4165"</tr>";4166}4167if(defined$extra) {4168print"<tr>\n".4169"<td colspan=\"5\">$extra</td>\n".4170"</tr>\n";4171}4172print"</table>\n";4173}41744175sub git_heads_body {4176# uses global variable $project4177my($headlist,$head,$from,$to,$extra) =@_;4178$from=0unlessdefined$from;4179$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);41804181print"<table class=\"heads\">\n";4182my$alternate=1;4183for(my$i=$from;$i<=$to;$i++) {4184my$entry=$headlist->[$i];4185my%ref=%$entry;4186my$curr=$ref{'id'}eq$head;4187if($alternate) {4188print"<tr class=\"dark\">\n";4189}else{4190print"<tr class=\"light\">\n";4191}4192$alternate^=1;4193print"<td><i>$ref{'age'}</i></td>\n".4194($curr?"<td class=\"current_head\">":"<td>") .4195$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4196-class=>"list name"},esc_html($ref{'name'})) .4197"</td>\n".4198"<td class=\"link\">".4199$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4200$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4201$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4202"</td>\n".4203"</tr>";4204}4205if(defined$extra) {4206print"<tr>\n".4207"<td colspan=\"3\">$extra</td>\n".4208"</tr>\n";4209}4210print"</table>\n";4211}42124213sub git_search_grep_body {4214my($commitlist,$from,$to,$extra) =@_;4215$from=0unlessdefined$from;4216$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);42174218print"<table class=\"commit_search\">\n";4219my$alternate=1;4220for(my$i=$from;$i<=$to;$i++) {4221my%co= %{$commitlist->[$i]};4222if(!%co) {4223next;4224}4225my$commit=$co{'id'};4226if($alternate) {4227print"<tr class=\"dark\">\n";4228}else{4229print"<tr class=\"light\">\n";4230}4231$alternate^=1;4232my$author= chop_and_escape_str($co{'author_name'},15,5);4233print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4234"<td><i>".$author."</i></td>\n".4235"<td>".4236$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4237-class=>"list subject"},4238 chop_and_escape_str($co{'title'},50) ."<br/>");4239my$comment=$co{'comment'};4240foreachmy$line(@$comment) {4241if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4242my($lead,$match,$trail) = ($1,$2,$3);4243$match= chop_str($match,70,5,'center');4244my$contextlen=int((80-length($match))/2);4245$contextlen=30if($contextlen>30);4246$lead= chop_str($lead,$contextlen,10,'left');4247$trail= chop_str($trail,$contextlen,10,'right');42484249$lead= esc_html($lead);4250$match= esc_html($match);4251$trail= esc_html($trail);42524253print"$lead<span class=\"match\">$match</span>$trail<br />";4254}4255}4256print"</td>\n".4257"<td class=\"link\">".4258$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4259" | ".4260$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4261" | ".4262$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4263print"</td>\n".4264"</tr>\n";4265}4266if(defined$extra) {4267print"<tr>\n".4268"<td colspan=\"3\">$extra</td>\n".4269"</tr>\n";4270}4271print"</table>\n";4272}42734274## ======================================================================4275## ======================================================================4276## actions42774278sub git_project_list {4279my$order=$input_params{'order'};4280if(defined$order&&$order!~m/none|project|descr|owner|age/) {4281 die_error(400,"Unknown order parameter");4282}42834284my@list= git_get_projects_list();4285if(!@list) {4286 die_error(404,"No projects found");4287}42884289 git_header_html();4290if(-f $home_text) {4291print"<div class=\"index_include\">\n";4292open(my$fd,$home_text);4293print<$fd>;4294close$fd;4295print"</div>\n";4296}4297print$cgi->startform(-method=>"get") .4298"<p class=\"projsearch\">Search:\n".4299$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4300"</p>".4301$cgi->end_form() ."\n";4302 git_project_list_body(\@list,$order);4303 git_footer_html();4304}43054306sub git_forks {4307my$order=$input_params{'order'};4308if(defined$order&&$order!~m/none|project|descr|owner|age/) {4309 die_error(400,"Unknown order parameter");4310}43114312my@list= git_get_projects_list($project);4313if(!@list) {4314 die_error(404,"No forks found");4315}43164317 git_header_html();4318 git_print_page_nav('','');4319 git_print_header_div('summary',"$projectforks");4320 git_project_list_body(\@list,$order);4321 git_footer_html();4322}43234324sub git_project_index {4325my@projects= git_get_projects_list($project);43264327print$cgi->header(4328-type =>'text/plain',4329-charset =>'utf-8',4330-content_disposition =>'inline; filename="index.aux"');43314332foreachmy$pr(@projects) {4333if(!exists$pr->{'owner'}) {4334$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4335}43364337my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4338# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4339$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4340$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4341$path=~s/ /\+/g;4342$owner=~s/ /\+/g;43434344print"$path$owner\n";4345}4346}43474348sub git_summary {4349my$descr= git_get_project_description($project) ||"none";4350my%co= parse_commit("HEAD");4351my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4352my$head=$co{'id'};43534354my$owner= git_get_project_owner($project);43554356my$refs= git_get_references();4357# These get_*_list functions return one more to allow us to see if4358# there are more ...4359my@taglist= git_get_tags_list(16);4360my@headlist= git_get_heads_list(16);4361my@forklist;4362my($check_forks) = gitweb_check_feature('forks');43634364if($check_forks) {4365@forklist= git_get_projects_list($project);4366}43674368 git_header_html();4369 git_print_page_nav('summary','',$head);43704371print"<div class=\"title\"> </div>\n";4372print"<table class=\"projects_list\">\n".4373"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4374"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4375if(defined$cd{'rfc2822'}) {4376print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4377}43784379# use per project git URL list in $projectroot/$project/cloneurl4380# or make project git URL from git base URL and project name4381my$url_tag="URL";4382my@url_list= git_get_project_url_list($project);4383@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4384foreachmy$git_url(@url_list) {4385next unless$git_url;4386print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4387$url_tag="";4388}43894390# Tag cloud4391my$show_ctags= (gitweb_check_feature('ctags'))[0];4392if($show_ctags) {4393my$ctags= git_get_project_ctags($project);4394my$cloud= git_populate_project_tagcloud($ctags);4395print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4396print"</td>\n<td>"unless%$ctags;4397print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4398print"</td>\n<td>"if%$ctags;4399print git_show_project_tagcloud($cloud,48);4400print"</td></tr>";4401}44024403print"</table>\n";44044405if(-s "$projectroot/$project/README.html") {4406if(open my$fd,"$projectroot/$project/README.html") {4407print"<div class=\"title\">readme</div>\n".4408"<div class=\"readme\">\n";4409print$_while(<$fd>);4410print"\n</div>\n";# class="readme"4411close$fd;4412}4413}44144415# we need to request one more than 16 (0..15) to check if4416# those 16 are all4417my@commitlist=$head? parse_commits($head,17) : ();4418if(@commitlist) {4419 git_print_header_div('shortlog');4420 git_shortlog_body(\@commitlist,0,15,$refs,4421$#commitlist<=15?undef:4422$cgi->a({-href => href(action=>"shortlog")},"..."));4423}44244425if(@taglist) {4426 git_print_header_div('tags');4427 git_tags_body(\@taglist,0,15,4428$#taglist<=15?undef:4429$cgi->a({-href => href(action=>"tags")},"..."));4430}44314432if(@headlist) {4433 git_print_header_div('heads');4434 git_heads_body(\@headlist,$head,0,15,4435$#headlist<=15?undef:4436$cgi->a({-href => href(action=>"heads")},"..."));4437}44384439if(@forklist) {4440 git_print_header_div('forks');4441 git_project_list_body(\@forklist,'age',0,15,4442$#forklist<=15?undef:4443$cgi->a({-href => href(action=>"forks")},"..."),4444'no_header');4445}44464447 git_footer_html();4448}44494450sub git_tag {4451my$head= git_get_head_hash($project);4452 git_header_html();4453 git_print_page_nav('','',$head,undef,$head);4454my%tag= parse_tag($hash);44554456if(!%tag) {4457 die_error(404,"Unknown tag object");4458}44594460 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4461print"<div class=\"title_text\">\n".4462"<table class=\"object_header\">\n".4463"<tr>\n".4464"<td>object</td>\n".4465"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4466$tag{'object'}) ."</td>\n".4467"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4468$tag{'type'}) ."</td>\n".4469"</tr>\n";4470if(defined($tag{'author'})) {4471my%ad= parse_date($tag{'epoch'},$tag{'tz'});4472print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4473print"<tr><td></td><td>".$ad{'rfc2822'} .4474sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4475"</td></tr>\n";4476}4477print"</table>\n\n".4478"</div>\n";4479print"<div class=\"page_body\">";4480my$comment=$tag{'comment'};4481foreachmy$line(@$comment) {4482chomp$line;4483print esc_html($line, -nbsp=>1) ."<br/>\n";4484}4485print"</div>\n";4486 git_footer_html();4487}44884489sub git_blame {4490my$fd;4491my$ftype;44924493 gitweb_check_feature('blame')4494or die_error(403,"Blame view not allowed");44954496 die_error(400,"No file name given")unless$file_name;4497$hash_base||= git_get_head_hash($project);4498 die_error(404,"Couldn't find base commit")unless($hash_base);4499my%co= parse_commit($hash_base)4500or die_error(404,"Commit not found");4501if(!defined$hash) {4502$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4503or die_error(404,"Error looking up file");4504}4505$ftype= git_get_type($hash);4506if($ftype!~"blob") {4507 die_error(400,"Object is not a blob");4508}4509open($fd,"-|", git_cmd(),"blame",'-p','--',4510$file_name,$hash_base)4511or die_error(500,"Open git-blame failed");4512 git_header_html();4513my$formats_nav=4514$cgi->a({-href => href(action=>"blob", -replay=>1)},4515"blob") .4516" | ".4517$cgi->a({-href => href(action=>"history", -replay=>1)},4518"history") .4519" | ".4520$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4521"HEAD");4522 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4523 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4524 git_print_page_path($file_name,$ftype,$hash_base);4525my@rev_color= (qw(light2 dark2));4526my$num_colors=scalar(@rev_color);4527my$current_color=0;4528my$last_rev;4529print<<HTML;4530<div class="page_body">4531<table class="blame">4532<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4533HTML4534my%metainfo= ();4535while(1) {4536$_= <$fd>;4537last unlessdefined$_;4538my($full_rev,$orig_lineno,$lineno,$group_size) =4539/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;4540if(!exists$metainfo{$full_rev}) {4541$metainfo{$full_rev} = {};4542}4543my$meta=$metainfo{$full_rev};4544while(<$fd>) {4545last if(s/^\t//);4546if(/^(\S+) (.*)$/) {4547$meta->{$1} =$2;4548}4549}4550my$data=$_;4551chomp$data;4552my$rev=substr($full_rev,0,8);4553my$author=$meta->{'author'};4554my%date= parse_date($meta->{'author-time'},4555$meta->{'author-tz'});4556my$date=$date{'iso-tz'};4557if($group_size) {4558$current_color= ++$current_color%$num_colors;4559}4560print"<tr class=\"$rev_color[$current_color]\">\n";4561if($group_size) {4562print"<td class=\"sha1\"";4563print" title=\"". esc_html($author) .",$date\"";4564print" rowspan=\"$group_size\""if($group_size>1);4565print">";4566print$cgi->a({-href => href(action=>"commit",4567 hash=>$full_rev,4568 file_name=>$file_name)},4569 esc_html($rev));4570print"</td>\n";4571}4572open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4573or die_error(500,"Open git-rev-parse failed");4574my$parent_commit= <$dd>;4575close$dd;4576chomp($parent_commit);4577my$blamed= href(action =>'blame',4578 file_name =>$meta->{'filename'},4579 hash_base =>$parent_commit);4580print"<td class=\"linenr\">";4581print$cgi->a({ -href =>"$blamed#l$orig_lineno",4582-id =>"l$lineno",4583-class=>"linenr"},4584 esc_html($lineno));4585print"</td>";4586print"<td class=\"pre\">". esc_html($data) ."</td>\n";4587print"</tr>\n";4588}4589print"</table>\n";4590print"</div>";4591close$fd4592or print"Reading blob failed\n";4593 git_footer_html();4594}45954596sub git_tags {4597my$head= git_get_head_hash($project);4598 git_header_html();4599 git_print_page_nav('','',$head,undef,$head);4600 git_print_header_div('summary',$project);46014602my@tagslist= git_get_tags_list();4603if(@tagslist) {4604 git_tags_body(\@tagslist);4605}4606 git_footer_html();4607}46084609sub git_heads {4610my$head= git_get_head_hash($project);4611 git_header_html();4612 git_print_page_nav('','',$head,undef,$head);4613 git_print_header_div('summary',$project);46144615my@headslist= git_get_heads_list();4616if(@headslist) {4617 git_heads_body(\@headslist,$head);4618}4619 git_footer_html();4620}46214622sub git_blob_plain {4623my$type=shift;4624my$expires;46254626if(!defined$hash) {4627if(defined$file_name) {4628my$base=$hash_base|| git_get_head_hash($project);4629$hash= git_get_hash_by_path($base,$file_name,"blob")4630or die_error(404,"Cannot find file");4631}else{4632 die_error(400,"No file name defined");4633}4634}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4635# blobs defined by non-textual hash id's can be cached4636$expires="+1d";4637}46384639open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4640or die_error(500,"Open git-cat-file blob '$hash' failed");46414642# content-type (can include charset)4643$type= blob_contenttype($fd,$file_name,$type);46444645# "save as" filename, even when no $file_name is given4646my$save_as="$hash";4647if(defined$file_name) {4648$save_as=$file_name;4649}elsif($type=~m/^text\//) {4650$save_as.='.txt';4651}46524653print$cgi->header(4654-type =>$type,4655-expires =>$expires,4656-content_disposition =>'inline; filename="'.$save_as.'"');4657undef$/;4658binmode STDOUT,':raw';4659print<$fd>;4660binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4661$/="\n";4662close$fd;4663}46644665sub git_blob {4666my$expires;46674668if(!defined$hash) {4669if(defined$file_name) {4670my$base=$hash_base|| git_get_head_hash($project);4671$hash= git_get_hash_by_path($base,$file_name,"blob")4672or die_error(404,"Cannot find file");4673}else{4674 die_error(400,"No file name defined");4675}4676}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4677# blobs defined by non-textual hash id's can be cached4678$expires="+1d";4679}46804681my($have_blame) = gitweb_check_feature('blame');4682open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4683or die_error(500,"Couldn't cat$file_name,$hash");4684my$mimetype= blob_mimetype($fd,$file_name);4685if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4686close$fd;4687return git_blob_plain($mimetype);4688}4689# we can have blame only for text/* mimetype4690$have_blame&&= ($mimetype=~m!^text/!);46914692 git_header_html(undef,$expires);4693my$formats_nav='';4694if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4695if(defined$file_name) {4696if($have_blame) {4697$formats_nav.=4698$cgi->a({-href => href(action=>"blame", -replay=>1)},4699"blame") .4700" | ";4701}4702$formats_nav.=4703$cgi->a({-href => href(action=>"history", -replay=>1)},4704"history") .4705" | ".4706$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4707"raw") .4708" | ".4709$cgi->a({-href => href(action=>"blob",4710 hash_base=>"HEAD", file_name=>$file_name)},4711"HEAD");4712}else{4713$formats_nav.=4714$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4715"raw");4716}4717 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4718 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4719}else{4720print"<div class=\"page_nav\">\n".4721"<br/><br/></div>\n".4722"<div class=\"title\">$hash</div>\n";4723}4724 git_print_page_path($file_name,"blob",$hash_base);4725print"<div class=\"page_body\">\n";4726if($mimetype=~m!^image/!) {4727print qq!<img type="$mimetype"!;4728if($file_name) {4729print qq! alt="$file_name" title="$file_name"!;4730}4731print qq! src="! .4732 href(action=>"blob_plain", hash=>$hash,4733 hash_base=>$hash_base, file_name=>$file_name) .4734 qq!"/>\n!;4735}else{4736my$nr;4737while(my$line= <$fd>) {4738chomp$line;4739$nr++;4740$line= untabify($line);4741printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4742$nr,$nr,$nr, esc_html($line, -nbsp=>1);4743}4744}4745close$fd4746or print"Reading blob failed.\n";4747print"</div>";4748 git_footer_html();4749}47504751sub git_tree {4752if(!defined$hash_base) {4753$hash_base="HEAD";4754}4755if(!defined$hash) {4756if(defined$file_name) {4757$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4758}else{4759$hash=$hash_base;4760}4761}4762 die_error(404,"No such tree")unlessdefined($hash);4763$/="\0";4764open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4765or die_error(500,"Open git-ls-tree failed");4766my@entries=map{chomp;$_} <$fd>;4767close$fdor die_error(404,"Reading tree failed");4768$/="\n";47694770my$refs= git_get_references();4771my$ref= format_ref_marker($refs,$hash_base);4772 git_header_html();4773my$basedir='';4774my($have_blame) = gitweb_check_feature('blame');4775if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4776my@views_nav= ();4777if(defined$file_name) {4778push@views_nav,4779$cgi->a({-href => href(action=>"history", -replay=>1)},4780"history"),4781$cgi->a({-href => href(action=>"tree",4782 hash_base=>"HEAD", file_name=>$file_name)},4783"HEAD"),4784}4785my$snapshot_links= format_snapshot_links($hash);4786if(defined$snapshot_links) {4787# FIXME: Should be available when we have no hash base as well.4788push@views_nav,$snapshot_links;4789}4790 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4791 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4792}else{4793undef$hash_base;4794print"<div class=\"page_nav\">\n";4795print"<br/><br/></div>\n";4796print"<div class=\"title\">$hash</div>\n";4797}4798if(defined$file_name) {4799$basedir=$file_name;4800if($basedirne''&&substr($basedir, -1)ne'/') {4801$basedir.='/';4802}4803 git_print_page_path($file_name,'tree',$hash_base);4804}4805print"<div class=\"page_body\">\n";4806print"<table class=\"tree\">\n";4807my$alternate=1;4808# '..' (top directory) link if possible4809if(defined$hash_base&&4810defined$file_name&&$file_name=~m![^/]+$!) {4811if($alternate) {4812print"<tr class=\"dark\">\n";4813}else{4814print"<tr class=\"light\">\n";4815}4816$alternate^=1;48174818my$up=$file_name;4819$up=~s!/?[^/]+$!!;4820undef$upunless$up;4821# based on git_print_tree_entry4822print'<td class="mode">'. mode_str('040000') ."</td>\n";4823print'<td class="list">';4824print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4825 file_name=>$up)},4826"..");4827print"</td>\n";4828print"<td class=\"link\"></td>\n";48294830print"</tr>\n";4831}4832foreachmy$line(@entries) {4833my%t= parse_ls_tree_line($line, -z =>1);48344835if($alternate) {4836print"<tr class=\"dark\">\n";4837}else{4838print"<tr class=\"light\">\n";4839}4840$alternate^=1;48414842 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);48434844print"</tr>\n";4845}4846print"</table>\n".4847"</div>";4848 git_footer_html();4849}48504851sub git_snapshot {4852my@supported_fmts= gitweb_check_feature('snapshot');4853@supported_fmts= filter_snapshot_fmts(@supported_fmts);48544855my$format=$input_params{'snapshot_format'};4856if(!@supported_fmts) {4857 die_error(403,"Snapshots not allowed");4858}4859# default to first supported snapshot format4860$format||=$supported_fmts[0];4861if($format!~m/^[a-z0-9]+$/) {4862 die_error(400,"Invalid snapshot format parameter");4863}elsif(!exists($known_snapshot_formats{$format})) {4864 die_error(400,"Unknown snapshot format");4865}elsif(!grep($_eq$format,@supported_fmts)) {4866 die_error(403,"Unsupported snapshot format");4867}48684869if(!defined$hash) {4870$hash= git_get_head_hash($project);4871}48724873my$name=$project;4874$name=~ s,([^/])/*\.git$,$1,;4875$name= basename($name);4876my$filename= to_utf8($name);4877$name=~s/\047/\047\\\047\047/g;4878my$cmd;4879$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";4880$cmd= quote_command(4881 git_cmd(),'archive',4882"--format=$known_snapshot_formats{$format}{'format'}",4883"--prefix=$name/",$hash);4884if(exists$known_snapshot_formats{$format}{'compressor'}) {4885$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});4886}48874888print$cgi->header(4889-type =>$known_snapshot_formats{$format}{'type'},4890-content_disposition =>'inline; filename="'."$filename".'"',4891-status =>'200 OK');48924893open my$fd,"-|",$cmd4894or die_error(500,"Execute git-archive failed");4895binmode STDOUT,':raw';4896print<$fd>;4897binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4898close$fd;4899}49004901sub git_log {4902my$head= git_get_head_hash($project);4903if(!defined$hash) {4904$hash=$head;4905}4906if(!defined$page) {4907$page=0;4908}4909my$refs= git_get_references();49104911my@commitlist= parse_commits($hash,101, (100*$page));49124913my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);49144915 git_header_html();4916 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);49174918if(!@commitlist) {4919my%co= parse_commit($hash);49204921 git_print_header_div('summary',$project);4922print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";4923}4924my$to= ($#commitlist>=99) ? (99) : ($#commitlist);4925for(my$i=0;$i<=$to;$i++) {4926my%co= %{$commitlist[$i]};4927next if!%co;4928my$commit=$co{'id'};4929my$ref= format_ref_marker($refs,$commit);4930my%ad= parse_date($co{'author_epoch'});4931 git_print_header_div('commit',4932"<span class=\"age\">$co{'age_string'}</span>".4933 esc_html($co{'title'}) .$ref,4934$commit);4935print"<div class=\"title_text\">\n".4936"<div class=\"log_link\">\n".4937$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4938" | ".4939$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4940" | ".4941$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4942"<br/>\n".4943"</div>\n".4944"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".4945"</div>\n";49464947print"<div class=\"log_body\">\n";4948 git_print_log($co{'comment'}, -final_empty_line=>1);4949print"</div>\n";4950}4951if($#commitlist>=100) {4952print"<div class=\"page_nav\">\n";4953print$cgi->a({-href => href(-replay=>1, page=>$page+1),4954-accesskey =>"n", -title =>"Alt-n"},"next");4955print"</div>\n";4956}4957 git_footer_html();4958}49594960sub git_commit {4961$hash||=$hash_base||"HEAD";4962my%co= parse_commit($hash)4963or die_error(404,"Unknown commit object");4964my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});4965my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});49664967my$parent=$co{'parent'};4968my$parents=$co{'parents'};# listref49694970# we need to prepare $formats_nav before any parameter munging4971my$formats_nav;4972if(!defined$parent) {4973# --root commitdiff4974$formats_nav.='(initial)';4975}elsif(@$parents==1) {4976# single parent commit4977$formats_nav.=4978'(parent: '.4979$cgi->a({-href => href(action=>"commit",4980 hash=>$parent)},4981 esc_html(substr($parent,0,7))) .4982')';4983}else{4984# merge commit4985$formats_nav.=4986'(merge: '.4987join(' ',map{4988$cgi->a({-href => href(action=>"commit",4989 hash=>$_)},4990 esc_html(substr($_,0,7)));4991}@$parents) .4992')';4993}49944995if(!defined$parent) {4996$parent="--root";4997}4998my@difftree;4999open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5000@diff_opts,5001(@$parents<=1?$parent:'-c'),5002$hash,"--"5003or die_error(500,"Open git-diff-tree failed");5004@difftree=map{chomp;$_} <$fd>;5005close$fdor die_error(404,"Reading git-diff-tree failed");50065007# non-textual hash id's can be cached5008my$expires;5009if($hash=~m/^[0-9a-fA-F]{40}$/) {5010$expires="+1d";5011}5012my$refs= git_get_references();5013my$ref= format_ref_marker($refs,$co{'id'});50145015 git_header_html(undef,$expires);5016 git_print_page_nav('commit','',5017$hash,$co{'tree'},$hash,5018$formats_nav);50195020if(defined$co{'parent'}) {5021 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5022}else{5023 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5024}5025print"<div class=\"title_text\">\n".5026"<table class=\"object_header\">\n";5027print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5028"<tr>".5029"<td></td><td>$ad{'rfc2822'}";5030if($ad{'hour_local'} <6) {5031printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5032$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5033}else{5034printf(" (%02d:%02d%s)",5035$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5036}5037print"</td>".5038"</tr>\n";5039print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5040print"<tr><td></td><td>$cd{'rfc2822'}".5041sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5042"</td></tr>\n";5043print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5044print"<tr>".5045"<td>tree</td>".5046"<td class=\"sha1\">".5047$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5048class=>"list"},$co{'tree'}) .5049"</td>".5050"<td class=\"link\">".5051$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5052"tree");5053my$snapshot_links= format_snapshot_links($hash);5054if(defined$snapshot_links) {5055print" | ".$snapshot_links;5056}5057print"</td>".5058"</tr>\n";50595060foreachmy$par(@$parents) {5061print"<tr>".5062"<td>parent</td>".5063"<td class=\"sha1\">".5064$cgi->a({-href => href(action=>"commit", hash=>$par),5065class=>"list"},$par) .5066"</td>".5067"<td class=\"link\">".5068$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5069" | ".5070$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5071"</td>".5072"</tr>\n";5073}5074print"</table>".5075"</div>\n";50765077print"<div class=\"page_body\">\n";5078 git_print_log($co{'comment'});5079print"</div>\n";50805081 git_difftree_body(\@difftree,$hash,@$parents);50825083 git_footer_html();5084}50855086sub git_object {5087# object is defined by:5088# - hash or hash_base alone5089# - hash_base and file_name5090my$type;50915092# - hash or hash_base alone5093if($hash|| ($hash_base&& !defined$file_name)) {5094my$object_id=$hash||$hash_base;50955096open my$fd,"-|", quote_command(5097 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5098or die_error(404,"Object does not exist");5099$type= <$fd>;5100chomp$type;5101close$fd5102or die_error(404,"Object does not exist");51035104# - hash_base and file_name5105}elsif($hash_base&&defined$file_name) {5106$file_name=~ s,/+$,,;51075108system(git_cmd(),"cat-file",'-e',$hash_base) ==05109or die_error(404,"Base object does not exist");51105111# here errors should not hapen5112open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5113or die_error(500,"Open git-ls-tree failed");5114my$line= <$fd>;5115close$fd;51165117#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5118unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5119 die_error(404,"File or directory for given base does not exist");5120}5121$type=$2;5122$hash=$3;5123}else{5124 die_error(400,"Not enough information to find object");5125}51265127print$cgi->redirect(-uri => href(action=>$type, -full=>1,5128 hash=>$hash, hash_base=>$hash_base,5129 file_name=>$file_name),5130-status =>'302 Found');5131}51325133sub git_blobdiff {5134my$format=shift||'html';51355136my$fd;5137my@difftree;5138my%diffinfo;5139my$expires;51405141# preparing $fd and %diffinfo for git_patchset_body5142# new style URI5143if(defined$hash_base&&defined$hash_parent_base) {5144if(defined$file_name) {5145# read raw output5146open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5147$hash_parent_base,$hash_base,5148"--", (defined$file_parent?$file_parent: ()),$file_name5149or die_error(500,"Open git-diff-tree failed");5150@difftree=map{chomp;$_} <$fd>;5151close$fd5152or die_error(404,"Reading git-diff-tree failed");5153@difftree5154or die_error(404,"Blob diff not found");51555156}elsif(defined$hash&&5157$hash=~/[0-9a-fA-F]{40}/) {5158# try to find filename from $hash51595160# read filtered raw output5161open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5162$hash_parent_base,$hash_base,"--"5163or die_error(500,"Open git-diff-tree failed");5164@difftree=5165# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5166# $hash == to_id5167grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5168map{chomp;$_} <$fd>;5169close$fd5170or die_error(404,"Reading git-diff-tree failed");5171@difftree5172or die_error(404,"Blob diff not found");51735174}else{5175 die_error(400,"Missing one of the blob diff parameters");5176}51775178if(@difftree>1) {5179 die_error(400,"Ambiguous blob diff specification");5180}51815182%diffinfo= parse_difftree_raw_line($difftree[0]);5183$file_parent||=$diffinfo{'from_file'} ||$file_name;5184$file_name||=$diffinfo{'to_file'};51855186$hash_parent||=$diffinfo{'from_id'};5187$hash||=$diffinfo{'to_id'};51885189# non-textual hash id's can be cached5190if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5191$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5192$expires='+1d';5193}51945195# open patch output5196open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5197'-p', ($formateq'html'?"--full-index": ()),5198$hash_parent_base,$hash_base,5199"--", (defined$file_parent?$file_parent: ()),$file_name5200or die_error(500,"Open git-diff-tree failed");5201}52025203# old/legacy style URI5204if(!%diffinfo&&# if new style URI failed5205defined$hash&&defined$hash_parent) {5206# fake git-diff-tree raw output5207$diffinfo{'from_mode'} =$diffinfo{'to_mode'} ="blob";5208$diffinfo{'from_id'} =$hash_parent;5209$diffinfo{'to_id'} =$hash;5210if(defined$file_name) {5211if(defined$file_parent) {5212$diffinfo{'status'} ='2';5213$diffinfo{'from_file'} =$file_parent;5214$diffinfo{'to_file'} =$file_name;5215}else{# assume not renamed5216$diffinfo{'status'} ='1';5217$diffinfo{'from_file'} =$file_name;5218$diffinfo{'to_file'} =$file_name;5219}5220}else{# no filename given5221$diffinfo{'status'} ='2';5222$diffinfo{'from_file'} =$hash_parent;5223$diffinfo{'to_file'} =$hash;5224}52255226# non-textual hash id's can be cached5227if($hash=~m/^[0-9a-fA-F]{40}$/&&5228$hash_parent=~m/^[0-9a-fA-F]{40}$/) {5229$expires='+1d';5230}52315232# open patch output5233open$fd,"-|", git_cmd(),"diff",@diff_opts,5234'-p', ($formateq'html'?"--full-index": ()),5235$hash_parent,$hash,"--"5236or die_error(500,"Open git-diff failed");5237}else{5238 die_error(400,"Missing one of the blob diff parameters")5239unless%diffinfo;5240}52415242# header5243if($formateq'html') {5244my$formats_nav=5245$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5246"raw");5247 git_header_html(undef,$expires);5248if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5249 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5250 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5251}else{5252print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5253print"<div class=\"title\">$hashvs$hash_parent</div>\n";5254}5255if(defined$file_name) {5256 git_print_page_path($file_name,"blob",$hash_base);5257}else{5258print"<div class=\"page_path\"></div>\n";5259}52605261}elsif($formateq'plain') {5262print$cgi->header(5263-type =>'text/plain',5264-charset =>'utf-8',5265-expires =>$expires,5266-content_disposition =>'inline; filename="'."$file_name".'.patch"');52675268print"X-Git-Url: ".$cgi->self_url() ."\n\n";52695270}else{5271 die_error(400,"Unknown blobdiff format");5272}52735274# patch5275if($formateq'html') {5276print"<div class=\"page_body\">\n";52775278 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5279close$fd;52805281print"</div>\n";# class="page_body"5282 git_footer_html();52835284}else{5285while(my$line= <$fd>) {5286$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5287$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;52885289print$line;52905291last if$line=~m!^\+\+\+!;5292}5293local$/=undef;5294print<$fd>;5295close$fd;5296}5297}52985299sub git_blobdiff_plain {5300 git_blobdiff('plain');5301}53025303sub git_commitdiff {5304my$format=shift||'html';5305$hash||=$hash_base||"HEAD";5306my%co= parse_commit($hash)5307or die_error(404,"Unknown commit object");53085309# choose format for commitdiff for merge5310if(!defined$hash_parent&& @{$co{'parents'}} >1) {5311$hash_parent='--cc';5312}5313# we need to prepare $formats_nav before almost any parameter munging5314my$formats_nav;5315if($formateq'html') {5316$formats_nav=5317$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5318"raw");53195320if(defined$hash_parent&&5321$hash_parentne'-c'&&$hash_parentne'--cc') {5322# commitdiff with two commits given5323my$hash_parent_short=$hash_parent;5324if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5325$hash_parent_short=substr($hash_parent,0,7);5326}5327$formats_nav.=5328' (from';5329for(my$i=0;$i< @{$co{'parents'}};$i++) {5330if($co{'parents'}[$i]eq$hash_parent) {5331$formats_nav.=' parent '. ($i+1);5332last;5333}5334}5335$formats_nav.=': '.5336$cgi->a({-href => href(action=>"commitdiff",5337 hash=>$hash_parent)},5338 esc_html($hash_parent_short)) .5339')';5340}elsif(!$co{'parent'}) {5341# --root commitdiff5342$formats_nav.=' (initial)';5343}elsif(scalar@{$co{'parents'}} ==1) {5344# single parent commit5345$formats_nav.=5346' (parent: '.5347$cgi->a({-href => href(action=>"commitdiff",5348 hash=>$co{'parent'})},5349 esc_html(substr($co{'parent'},0,7))) .5350')';5351}else{5352# merge commit5353if($hash_parenteq'--cc') {5354$formats_nav.=' | '.5355$cgi->a({-href => href(action=>"commitdiff",5356 hash=>$hash, hash_parent=>'-c')},5357'combined');5358}else{# $hash_parent eq '-c'5359$formats_nav.=' | '.5360$cgi->a({-href => href(action=>"commitdiff",5361 hash=>$hash, hash_parent=>'--cc')},5362'compact');5363}5364$formats_nav.=5365' (merge: '.5366join(' ',map{5367$cgi->a({-href => href(action=>"commitdiff",5368 hash=>$_)},5369 esc_html(substr($_,0,7)));5370} @{$co{'parents'}} ) .5371')';5372}5373}53745375my$hash_parent_param=$hash_parent;5376if(!defined$hash_parent_param) {5377# --cc for multiple parents, --root for parentless5378$hash_parent_param=5379@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5380}53815382# read commitdiff5383my$fd;5384my@difftree;5385if($formateq'html') {5386open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5387"--no-commit-id","--patch-with-raw","--full-index",5388$hash_parent_param,$hash,"--"5389or die_error(500,"Open git-diff-tree failed");53905391while(my$line= <$fd>) {5392chomp$line;5393# empty line ends raw part of diff-tree output5394last unless$line;5395push@difftree,scalar parse_difftree_raw_line($line);5396}53975398}elsif($formateq'plain') {5399open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5400'-p',$hash_parent_param,$hash,"--"5401or die_error(500,"Open git-diff-tree failed");54025403}else{5404 die_error(400,"Unknown commitdiff format");5405}54065407# non-textual hash id's can be cached5408my$expires;5409if($hash=~m/^[0-9a-fA-F]{40}$/) {5410$expires="+1d";5411}54125413# write commit message5414if($formateq'html') {5415my$refs= git_get_references();5416my$ref= format_ref_marker($refs,$co{'id'});54175418 git_header_html(undef,$expires);5419 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5420 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5421 git_print_authorship(\%co);5422print"<div class=\"page_body\">\n";5423if(@{$co{'comment'}} >1) {5424print"<div class=\"log\">\n";5425 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5426print"</div>\n";# class="log"5427}54285429}elsif($formateq'plain') {5430my$refs= git_get_references("tags");5431my$tagname= git_get_rev_name_tags($hash);5432my$filename= basename($project) ."-$hash.patch";54335434print$cgi->header(5435-type =>'text/plain',5436-charset =>'utf-8',5437-expires =>$expires,5438-content_disposition =>'inline; filename="'."$filename".'"');5439my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5440print"From: ". to_utf8($co{'author'}) ."\n";5441print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5442print"Subject: ". to_utf8($co{'title'}) ."\n";54435444print"X-Git-Tag:$tagname\n"if$tagname;5445print"X-Git-Url: ".$cgi->self_url() ."\n\n";54465447foreachmy$line(@{$co{'comment'}}) {5448print to_utf8($line) ."\n";5449}5450print"---\n\n";5451}54525453# write patch5454if($formateq'html') {5455my$use_parents= !defined$hash_parent||5456$hash_parenteq'-c'||$hash_parenteq'--cc';5457 git_difftree_body(\@difftree,$hash,5458$use_parents? @{$co{'parents'}} :$hash_parent);5459print"<br/>\n";54605461 git_patchset_body($fd, \@difftree,$hash,5462$use_parents? @{$co{'parents'}} :$hash_parent);5463close$fd;5464print"</div>\n";# class="page_body"5465 git_footer_html();54665467}elsif($formateq'plain') {5468local$/=undef;5469print<$fd>;5470close$fd5471or print"Reading git-diff-tree failed\n";5472}5473}54745475sub git_commitdiff_plain {5476 git_commitdiff('plain');5477}54785479sub git_history {5480if(!defined$hash_base) {5481$hash_base= git_get_head_hash($project);5482}5483if(!defined$page) {5484$page=0;5485}5486my$ftype;5487my%co= parse_commit($hash_base)5488or die_error(404,"Unknown commit object");54895490my$refs= git_get_references();5491my$limit=sprintf("--max-count=%i", (100* ($page+1)));54925493my@commitlist= parse_commits($hash_base,101, (100*$page),5494$file_name,"--full-history")5495or die_error(404,"No such file or directory on given branch");54965497if(!defined$hash&&defined$file_name) {5498# some commits could have deleted file in question,5499# and not have it in tree, but one of them has to have it5500for(my$i=0;$i<=@commitlist;$i++) {5501$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5502last ifdefined$hash;5503}5504}5505if(defined$hash) {5506$ftype= git_get_type($hash);5507}5508if(!defined$ftype) {5509 die_error(500,"Unknown type of object");5510}55115512my$paging_nav='';5513if($page>0) {5514$paging_nav.=5515$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5516 file_name=>$file_name)},5517"first");5518$paging_nav.=" ⋅ ".5519$cgi->a({-href => href(-replay=>1, page=>$page-1),5520-accesskey =>"p", -title =>"Alt-p"},"prev");5521}else{5522$paging_nav.="first";5523$paging_nav.=" ⋅ prev";5524}5525my$next_link='';5526if($#commitlist>=100) {5527$next_link=5528$cgi->a({-href => href(-replay=>1, page=>$page+1),5529-accesskey =>"n", -title =>"Alt-n"},"next");5530$paging_nav.=" ⋅$next_link";5531}else{5532$paging_nav.=" ⋅ next";5533}55345535 git_header_html();5536 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5537 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5538 git_print_page_path($file_name,$ftype,$hash_base);55395540 git_history_body(\@commitlist,0,99,5541$refs,$hash_base,$ftype,$next_link);55425543 git_footer_html();5544}55455546sub git_search {5547 gitweb_check_feature('search')or die_error(403,"Search is disabled");5548if(!defined$searchtext) {5549 die_error(400,"Text field is empty");5550}5551if(!defined$hash) {5552$hash= git_get_head_hash($project);5553}5554my%co= parse_commit($hash);5555if(!%co) {5556 die_error(404,"Unknown commit object");5557}5558if(!defined$page) {5559$page=0;5560}55615562$searchtype||='commit';5563if($searchtypeeq'pickaxe') {5564# pickaxe may take all resources of your box and run for several minutes5565# with every query - so decide by yourself how public you make this feature5566 gitweb_check_feature('pickaxe')5567or die_error(403,"Pickaxe is disabled");5568}5569if($searchtypeeq'grep') {5570 gitweb_check_feature('grep')5571or die_error(403,"Grep is disabled");5572}55735574 git_header_html();55755576if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5577my$greptype;5578if($searchtypeeq'commit') {5579$greptype="--grep=";5580}elsif($searchtypeeq'author') {5581$greptype="--author=";5582}elsif($searchtypeeq'committer') {5583$greptype="--committer=";5584}5585$greptype.=$searchtext;5586my@commitlist= parse_commits($hash,101, (100*$page),undef,5587$greptype,'--regexp-ignore-case',5588$search_use_regexp?'--extended-regexp':'--fixed-strings');55895590my$paging_nav='';5591if($page>0) {5592$paging_nav.=5593$cgi->a({-href => href(action=>"search", hash=>$hash,5594 searchtext=>$searchtext,5595 searchtype=>$searchtype)},5596"first");5597$paging_nav.=" ⋅ ".5598$cgi->a({-href => href(-replay=>1, page=>$page-1),5599-accesskey =>"p", -title =>"Alt-p"},"prev");5600}else{5601$paging_nav.="first";5602$paging_nav.=" ⋅ prev";5603}5604my$next_link='';5605if($#commitlist>=100) {5606$next_link=5607$cgi->a({-href => href(-replay=>1, page=>$page+1),5608-accesskey =>"n", -title =>"Alt-n"},"next");5609$paging_nav.=" ⋅$next_link";5610}else{5611$paging_nav.=" ⋅ next";5612}56135614if($#commitlist>=100) {5615}56165617 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5618 git_print_header_div('commit', esc_html($co{'title'}),$hash);5619 git_search_grep_body(\@commitlist,0,99,$next_link);5620}56215622if($searchtypeeq'pickaxe') {5623 git_print_page_nav('','',$hash,$co{'tree'},$hash);5624 git_print_header_div('commit', esc_html($co{'title'}),$hash);56255626print"<table class=\"pickaxe search\">\n";5627my$alternate=1;5628$/="\n";5629open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5630'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5631($search_use_regexp?'--pickaxe-regex': ());5632undef%co;5633my@files;5634while(my$line= <$fd>) {5635chomp$line;5636next unless$line;56375638my%set= parse_difftree_raw_line($line);5639if(defined$set{'commit'}) {5640# finish previous commit5641if(%co) {5642print"</td>\n".5643"<td class=\"link\">".5644$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5645" | ".5646$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5647print"</td>\n".5648"</tr>\n";5649}56505651if($alternate) {5652print"<tr class=\"dark\">\n";5653}else{5654print"<tr class=\"light\">\n";5655}5656$alternate^=1;5657%co= parse_commit($set{'commit'});5658my$author= chop_and_escape_str($co{'author_name'},15,5);5659print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5660"<td><i>$author</i></td>\n".5661"<td>".5662$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5663-class=>"list subject"},5664 chop_and_escape_str($co{'title'},50) ."<br/>");5665}elsif(defined$set{'to_id'}) {5666next if($set{'to_id'} =~m/^0{40}$/);56675668print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5669 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5670-class=>"list"},5671"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5672"<br/>\n";5673}5674}5675close$fd;56765677# finish last commit (warning: repetition!)5678if(%co) {5679print"</td>\n".5680"<td class=\"link\">".5681$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5682" | ".5683$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5684print"</td>\n".5685"</tr>\n";5686}56875688print"</table>\n";5689}56905691if($searchtypeeq'grep') {5692 git_print_page_nav('','',$hash,$co{'tree'},$hash);5693 git_print_header_div('commit', esc_html($co{'title'}),$hash);56945695print"<table class=\"grep_search\">\n";5696my$alternate=1;5697my$matches=0;5698$/="\n";5699open my$fd,"-|", git_cmd(),'grep','-n',5700$search_use_regexp? ('-E','-i') :'-F',5701$searchtext,$co{'tree'};5702my$lastfile='';5703while(my$line= <$fd>) {5704chomp$line;5705my($file,$lno,$ltext,$binary);5706last if($matches++>1000);5707if($line=~/^Binary file (.+) matches$/) {5708$file=$1;5709$binary=1;5710}else{5711(undef,$file,$lno,$ltext) =split(/:/,$line,4);5712}5713if($filene$lastfile) {5714$lastfileand print"</td></tr>\n";5715if($alternate++) {5716print"<tr class=\"dark\">\n";5717}else{5718print"<tr class=\"light\">\n";5719}5720print"<td class=\"list\">".5721$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5722 file_name=>"$file"),5723-class=>"list"}, esc_path($file));5724print"</td><td>\n";5725$lastfile=$file;5726}5727if($binary) {5728print"<div class=\"binary\">Binary file</div>\n";5729}else{5730$ltext= untabify($ltext);5731if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5732$ltext= esc_html($1, -nbsp=>1);5733$ltext.='<span class="match">';5734$ltext.= esc_html($2, -nbsp=>1);5735$ltext.='</span>';5736$ltext.= esc_html($3, -nbsp=>1);5737}else{5738$ltext= esc_html($ltext, -nbsp=>1);5739}5740print"<div class=\"pre\">".5741$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5742 file_name=>"$file").'#l'.$lno,5743-class=>"linenr"},sprintf('%4i',$lno))5744.' '.$ltext."</div>\n";5745}5746}5747if($lastfile) {5748print"</td></tr>\n";5749if($matches>1000) {5750print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5751}5752}else{5753print"<div class=\"diff nodifferences\">No matches found</div>\n";5754}5755close$fd;57565757print"</table>\n";5758}5759 git_footer_html();5760}57615762sub git_search_help {5763 git_header_html();5764 git_print_page_nav('','',$hash,$hash,$hash);5765print<<EOT;5766<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5767regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5768the pattern entered is recognized as the POSIX extended5769<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5770insensitive).</p>5771<dl>5772<dt><b>commit</b></dt>5773<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5774EOT5775my($have_grep) = gitweb_check_feature('grep');5776if($have_grep) {5777print<<EOT;5778<dt><b>grep</b></dt>5779<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5780 a different one) are searched for the given pattern. On large trees, this search can take5781a while and put some strain on the server, so please use it with some consideration. Note that5782due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5783case-sensitive.</dd>5784EOT5785}5786print<<EOT;5787<dt><b>author</b></dt>5788<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5789<dt><b>committer</b></dt>5790<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5791EOT5792my($have_pickaxe) = gitweb_check_feature('pickaxe');5793if($have_pickaxe) {5794print<<EOT;5795<dt><b>pickaxe</b></dt>5796<dd>All commits that caused the string to appear or disappear from any file (changes that5797added, removed or "modified" the string) will be listed. This search can take a while and5798takes a lot of strain on the server, so please use it wisely. Note that since you may be5799interested even in changes just changing the case as well, this search is case sensitive.</dd>5800EOT5801}5802print"</dl>\n";5803 git_footer_html();5804}58055806sub git_shortlog {5807my$head= git_get_head_hash($project);5808if(!defined$hash) {5809$hash=$head;5810}5811if(!defined$page) {5812$page=0;5813}5814my$refs= git_get_references();58155816my$commit_hash=$hash;5817if(defined$hash_parent) {5818$commit_hash="$hash_parent..$hash";5819}5820my@commitlist= parse_commits($commit_hash,101, (100*$page));58215822my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5823my$next_link='';5824if($#commitlist>=100) {5825$next_link=5826$cgi->a({-href => href(-replay=>1, page=>$page+1),5827-accesskey =>"n", -title =>"Alt-n"},"next");5828}58295830 git_header_html();5831 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);5832 git_print_header_div('summary',$project);58335834 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);58355836 git_footer_html();5837}58385839## ......................................................................5840## feeds (RSS, Atom; OPML)58415842sub git_feed {5843my$format=shift||'atom';5844my($have_blame) = gitweb_check_feature('blame');58455846# Atom: http://www.atomenabled.org/developers/syndication/5847# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ5848if($formatne'rss'&&$formatne'atom') {5849 die_error(400,"Unknown web feed format");5850}58515852# log/feed of current (HEAD) branch, log of given branch, history of file/directory5853my$head=$hash||'HEAD';5854my@commitlist= parse_commits($head,150,0,$file_name);58555856my%latest_commit;5857my%latest_date;5858my$content_type="application/$format+xml";5859if(defined$cgi->http('HTTP_ACCEPT') &&5860$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {5861# browser (feed reader) prefers text/xml5862$content_type='text/xml';5863}5864if(defined($commitlist[0])) {5865%latest_commit= %{$commitlist[0]};5866%latest_date= parse_date($latest_commit{'author_epoch'});5867print$cgi->header(5868-type =>$content_type,5869-charset =>'utf-8',5870-last_modified =>$latest_date{'rfc2822'});5871}else{5872print$cgi->header(5873-type =>$content_type,5874-charset =>'utf-8');5875}58765877# Optimization: skip generating the body if client asks only5878# for Last-Modified date.5879return if($cgi->request_method()eq'HEAD');58805881# header variables5882my$title="$site_name-$project/$action";5883my$feed_type='log';5884if(defined$hash) {5885$title.=" - '$hash'";5886$feed_type='branch log';5887if(defined$file_name) {5888$title.=" ::$file_name";5889$feed_type='history';5890}5891}elsif(defined$file_name) {5892$title.=" -$file_name";5893$feed_type='history';5894}5895$title.="$feed_type";5896my$descr= git_get_project_description($project);5897if(defined$descr) {5898$descr= esc_html($descr);5899}else{5900$descr="$project".5901($formateq'rss'?'RSS':'Atom') .5902" feed";5903}5904my$owner= git_get_project_owner($project);5905$owner= esc_html($owner);59065907#header5908my$alt_url;5909if(defined$file_name) {5910$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);5911}elsif(defined$hash) {5912$alt_url= href(-full=>1, action=>"log", hash=>$hash);5913}else{5914$alt_url= href(-full=>1, action=>"summary");5915}5916print qq!<?xml version="1.0" encoding="utf-8"?>\n!;5917if($formateq'rss') {5918print<<XML;5919<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">5920<channel>5921XML5922print"<title>$title</title>\n".5923"<link>$alt_url</link>\n".5924"<description>$descr</description>\n".5925"<language>en</language>\n";5926}elsif($formateq'atom') {5927print<<XML;5928<feed xmlns="http://www.w3.org/2005/Atom">5929XML5930print"<title>$title</title>\n".5931"<subtitle>$descr</subtitle>\n".5932'<link rel="alternate" type="text/html" href="'.5933$alt_url.'" />'."\n".5934'<link rel="self" type="'.$content_type.'" href="'.5935$cgi->self_url() .'" />'."\n".5936"<id>". href(-full=>1) ."</id>\n".5937# use project owner for feed author5938"<author><name>$owner</name></author>\n";5939if(defined$favicon) {5940print"<icon>". esc_url($favicon) ."</icon>\n";5941}5942if(defined$logo_url) {5943# not twice as wide as tall: 72 x 27 pixels5944print"<logo>". esc_url($logo) ."</logo>\n";5945}5946if(!%latest_date) {5947# dummy date to keep the feed valid until commits trickle in:5948print"<updated>1970-01-01T00:00:00Z</updated>\n";5949}else{5950print"<updated>$latest_date{'iso-8601'}</updated>\n";5951}5952}59535954# contents5955for(my$i=0;$i<=$#commitlist;$i++) {5956my%co= %{$commitlist[$i]};5957my$commit=$co{'id'};5958# we read 150, we always show 30 and the ones more recent than 48 hours5959if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {5960last;5961}5962my%cd= parse_date($co{'author_epoch'});59635964# get list of changed files5965open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5966$co{'parent'} ||"--root",5967$co{'id'},"--", (defined$file_name?$file_name: ())5968ornext;5969my@difftree=map{chomp;$_} <$fd>;5970close$fd5971ornext;59725973# print element (entry, item)5974my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);5975if($formateq'rss') {5976print"<item>\n".5977"<title>". esc_html($co{'title'}) ."</title>\n".5978"<author>". esc_html($co{'author'}) ."</author>\n".5979"<pubDate>$cd{'rfc2822'}</pubDate>\n".5980"<guid isPermaLink=\"true\">$co_url</guid>\n".5981"<link>$co_url</link>\n".5982"<description>". esc_html($co{'title'}) ."</description>\n".5983"<content:encoded>".5984"<![CDATA[\n";5985}elsif($formateq'atom') {5986print"<entry>\n".5987"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".5988"<updated>$cd{'iso-8601'}</updated>\n".5989"<author>\n".5990" <name>". esc_html($co{'author_name'}) ."</name>\n";5991if($co{'author_email'}) {5992print" <email>". esc_html($co{'author_email'}) ."</email>\n";5993}5994print"</author>\n".5995# use committer for contributor5996"<contributor>\n".5997" <name>". esc_html($co{'committer_name'}) ."</name>\n";5998if($co{'committer_email'}) {5999print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6000}6001print"</contributor>\n".6002"<published>$cd{'iso-8601'}</published>\n".6003"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6004"<id>$co_url</id>\n".6005"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6006"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6007}6008my$comment=$co{'comment'};6009print"<pre>\n";6010foreachmy$line(@$comment) {6011$line= esc_html($line);6012print"$line\n";6013}6014print"</pre><ul>\n";6015foreachmy$difftree_line(@difftree) {6016my%difftree= parse_difftree_raw_line($difftree_line);6017next if!$difftree{'from_id'};60186019my$file=$difftree{'file'} ||$difftree{'to_file'};60206021print"<li>".6022"[".6023$cgi->a({-href => href(-full=>1, action=>"blobdiff",6024 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6025 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6026 file_name=>$file, file_parent=>$difftree{'from_file'}),6027-title =>"diff"},'D');6028if($have_blame) {6029print$cgi->a({-href => href(-full=>1, action=>"blame",6030 file_name=>$file, hash_base=>$commit),6031-title =>"blame"},'B');6032}6033# if this is not a feed of a file history6034if(!defined$file_name||$file_namene$file) {6035print$cgi->a({-href => href(-full=>1, action=>"history",6036 file_name=>$file, hash=>$commit),6037-title =>"history"},'H');6038}6039$file= esc_path($file);6040print"] ".6041"$file</li>\n";6042}6043if($formateq'rss') {6044print"</ul>]]>\n".6045"</content:encoded>\n".6046"</item>\n";6047}elsif($formateq'atom') {6048print"</ul>\n</div>\n".6049"</content>\n".6050"</entry>\n";6051}6052}60536054# end of feed6055if($formateq'rss') {6056print"</channel>\n</rss>\n";6057}elsif($formateq'atom') {6058print"</feed>\n";6059}6060}60616062sub git_rss {6063 git_feed('rss');6064}60656066sub git_atom {6067 git_feed('atom');6068}60696070sub git_opml {6071my@list= git_get_projects_list();60726073print$cgi->header(-type =>'text/xml', -charset =>'utf-8');6074print<<XML;6075<?xml version="1.0" encoding="utf-8"?>6076<opml version="1.0">6077<head>6078 <title>$site_nameOPML Export</title>6079</head>6080<body>6081<outline text="git RSS feeds">6082XML60836084foreachmy$pr(@list) {6085my%proj=%$pr;6086my$head= git_get_head_hash($proj{'path'});6087if(!defined$head) {6088next;6089}6090$git_dir="$projectroot/$proj{'path'}";6091my%co= parse_commit($head);6092if(!%co) {6093next;6094}60956096my$path= esc_html(chop_str($proj{'path'},25,5));6097my$rss="$my_url?p=$proj{'path'};a=rss";6098my$html="$my_url?p=$proj{'path'};a=summary";6099print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6100}6101print<<XML;6102</outline>6103</body>6104</opml>6105XML6106}