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 552my($refname,$pathname) =split(/:/,$path_info,2); 553if(defined$pathname) { 554# we got "branch:filename" or "branch:dir/" 555# we could use git_get_type(branch:pathname), but: 556# - it needs $git_dir 557# - it does a git() call 558# - the convention of terminating directories with a slash 559# makes it superfluous 560# - embedding the action in the PATH_INFO would make it even 561# more superfluous 562$pathname=~ s,^/+,,; 563if(!$pathname||substr($pathname, -1)eq"/") { 564$input_params{'action'} ||="tree"; 565$pathname=~ s,/$,,; 566}else{ 567$input_params{'action'} ||="blob_plain"; 568} 569$input_params{'hash_base'} ||=$refname; 570$input_params{'file_name'} ||=$pathname; 571}elsif(defined$refname) { 572# we got "branch". In this case we have to choose if we have to 573# set hash or hash_base. 574# 575# Most of the actions without a pathname only want hash to be 576# set, except for the ones specified in @wants_base that want 577# hash_base instead. It should also be noted that hand-crafted 578# links having 'history' as an action and no pathname or hash 579# set will fail, but that happens regardless of PATH_INFO. 580$input_params{'action'} ||="shortlog"; 581if(grep{$_eq$input_params{'action'} }@wants_base) { 582$input_params{'hash_base'} ||=$refname; 583}else{ 584$input_params{'hash'} ||=$refname; 585} 586} 587} 588evaluate_path_info(); 589 590our$action=$input_params{'action'}; 591if(defined$action) { 592if(!validate_action($action)) { 593 die_error(400,"Invalid action parameter"); 594} 595} 596 597# parameters which are pathnames 598our$project=$input_params{'project'}; 599if(defined$project) { 600if(!validate_project($project)) { 601undef$project; 602 die_error(404,"No such project"); 603} 604} 605 606our$file_name=$input_params{'file_name'}; 607if(defined$file_name) { 608if(!validate_pathname($file_name)) { 609 die_error(400,"Invalid file parameter"); 610} 611} 612 613our$file_parent=$input_params{'file_parent'}; 614if(defined$file_parent) { 615if(!validate_pathname($file_parent)) { 616 die_error(400,"Invalid file parent parameter"); 617} 618} 619 620# parameters which are refnames 621our$hash=$input_params{'hash'}; 622if(defined$hash) { 623if(!validate_refname($hash)) { 624 die_error(400,"Invalid hash parameter"); 625} 626} 627 628our$hash_parent=$input_params{'hash_parent'}; 629if(defined$hash_parent) { 630if(!validate_refname($hash_parent)) { 631 die_error(400,"Invalid hash parent parameter"); 632} 633} 634 635our$hash_base=$input_params{'hash_base'}; 636if(defined$hash_base) { 637if(!validate_refname($hash_base)) { 638 die_error(400,"Invalid hash base parameter"); 639} 640} 641 642our@extra_options= @{$input_params{'extra_options'}}; 643# @extra_options is always defined, since it can only be (currently) set from 644# CGI, and $cgi->param() returns the empty array in array context if the param 645# is not set 646foreachmy$opt(@extra_options) { 647if(not exists$allowed_options{$opt}) { 648 die_error(400,"Invalid option parameter"); 649} 650if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 651 die_error(400,"Invalid option parameter for this action"); 652} 653} 654 655our$hash_parent_base=$input_params{'hash_parent_base'}; 656if(defined$hash_parent_base) { 657if(!validate_refname($hash_parent_base)) { 658 die_error(400,"Invalid hash parent base parameter"); 659} 660} 661 662# other parameters 663our$page=$input_params{'page'}; 664if(defined$page) { 665if($page=~m/[^0-9]/) { 666 die_error(400,"Invalid page parameter"); 667} 668} 669 670our$searchtype=$input_params{'searchtype'}; 671if(defined$searchtype) { 672if($searchtype=~m/[^a-z]/) { 673 die_error(400,"Invalid searchtype parameter"); 674} 675} 676 677our$search_use_regexp=$input_params{'search_use_regexp'}; 678 679our$searchtext=$input_params{'searchtext'}; 680our$search_regexp; 681if(defined$searchtext) { 682if(length($searchtext) <2) { 683 die_error(403,"At least two characters are required for search parameter"); 684} 685$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 686} 687 688# path to the current git repository 689our$git_dir; 690$git_dir="$projectroot/$project"if$project; 691 692# dispatch 693if(!defined$action) { 694if(defined$hash) { 695$action= git_get_type($hash); 696}elsif(defined$hash_base&&defined$file_name) { 697$action= git_get_type("$hash_base:$file_name"); 698}elsif(defined$project) { 699$action='summary'; 700}else{ 701$action='project_list'; 702} 703} 704if(!defined($actions{$action})) { 705 die_error(400,"Unknown action"); 706} 707if($action!~m/^(opml|project_list|project_index)$/&& 708!$project) { 709 die_error(400,"Project needed"); 710} 711$actions{$action}->(); 712exit; 713 714## ====================================================================== 715## action links 716 717sub href (%) { 718my%params=@_; 719# default is to use -absolute url() i.e. $my_uri 720my$href=$params{-full} ?$my_url:$my_uri; 721 722$params{'project'} =$projectunlessexists$params{'project'}; 723 724if($params{-replay}) { 725while(my($name,$symbol) =each%cgi_param_mapping) { 726if(!exists$params{$name}) { 727$params{$name} =$input_params{$name}; 728} 729} 730} 731 732my($use_pathinfo) = gitweb_check_feature('pathinfo'); 733if($use_pathinfo) { 734# use PATH_INFO for project name 735$href.="/".esc_url($params{'project'})ifdefined$params{'project'}; 736delete$params{'project'}; 737 738# Summary just uses the project path URL 739if(defined$params{'action'} &&$params{'action'}eq'summary') { 740delete$params{'action'}; 741} 742} 743 744# now encode the parameters explicitly 745my@result= (); 746for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 747my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 748if(defined$params{$name}) { 749if(ref($params{$name})eq"ARRAY") { 750foreachmy$par(@{$params{$name}}) { 751push@result,$symbol."=". esc_param($par); 752} 753}else{ 754push@result,$symbol."=". esc_param($params{$name}); 755} 756} 757} 758$href.="?".join(';',@result)ifscalar@result; 759 760return$href; 761} 762 763 764## ====================================================================== 765## validation, quoting/unquoting and escaping 766 767sub validate_action { 768my$input=shift||returnundef; 769returnundefunlessexists$actions{$input}; 770return$input; 771} 772 773sub validate_project { 774my$input=shift||returnundef; 775if(!validate_pathname($input) || 776!(-d "$projectroot/$input") || 777!check_head_link("$projectroot/$input") || 778($export_ok&& !(-e "$projectroot/$input/$export_ok")) || 779($strict_export&& !project_in_list($input))) { 780returnundef; 781}else{ 782return$input; 783} 784} 785 786sub validate_pathname { 787my$input=shift||returnundef; 788 789# no '.' or '..' as elements of path, i.e. no '.' nor '..' 790# at the beginning, at the end, and between slashes. 791# also this catches doubled slashes 792if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 793returnundef; 794} 795# no null characters 796if($input=~m!\0!) { 797returnundef; 798} 799return$input; 800} 801 802sub validate_refname { 803my$input=shift||returnundef; 804 805# textual hashes are O.K. 806if($input=~m/^[0-9a-fA-F]{40}$/) { 807return$input; 808} 809# it must be correct pathname 810$input= validate_pathname($input) 811orreturnundef; 812# restrictions on ref name according to git-check-ref-format 813if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { 814returnundef; 815} 816return$input; 817} 818 819# decode sequences of octets in utf8 into Perl's internal form, 820# which is utf-8 with utf8 flag set if needed. gitweb writes out 821# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning 822sub to_utf8 { 823my$str=shift; 824if(utf8::valid($str)) { 825 utf8::decode($str); 826return$str; 827}else{ 828return decode($fallback_encoding,$str, Encode::FB_DEFAULT); 829} 830} 831 832# quote unsafe chars, but keep the slash, even when it's not 833# correct, but quoted slashes look too horrible in bookmarks 834sub esc_param { 835my$str=shift; 836$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg; 837$str=~s/\+/%2B/g; 838$str=~s/ /\+/g; 839return$str; 840} 841 842# quote unsafe chars in whole URL, so some charactrs cannot be quoted 843sub esc_url { 844my$str=shift; 845$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg; 846$str=~s/\+/%2B/g; 847$str=~s/ /\+/g; 848return$str; 849} 850 851# replace invalid utf8 character with SUBSTITUTION sequence 852sub esc_html ($;%) { 853my$str=shift; 854my%opts=@_; 855 856$str= to_utf8($str); 857$str=$cgi->escapeHTML($str); 858if($opts{'-nbsp'}) { 859$str=~s/ / /g; 860} 861$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg; 862return$str; 863} 864 865# quote control characters and escape filename to HTML 866sub esc_path { 867my$str=shift; 868my%opts=@_; 869 870$str= to_utf8($str); 871$str=$cgi->escapeHTML($str); 872if($opts{'-nbsp'}) { 873$str=~s/ / /g; 874} 875$str=~ s|([[:cntrl:]])|quot_cec($1)|eg; 876return$str; 877} 878 879# Make control characters "printable", using character escape codes (CEC) 880sub quot_cec { 881my$cntrl=shift; 882my%opts=@_; 883my%es= (# character escape codes, aka escape sequences 884"\t"=>'\t',# tab (HT) 885"\n"=>'\n',# line feed (LF) 886"\r"=>'\r',# carrige return (CR) 887"\f"=>'\f',# form feed (FF) 888"\b"=>'\b',# backspace (BS) 889"\a"=>'\a',# alarm (bell) (BEL) 890"\e"=>'\e',# escape (ESC) 891"\013"=>'\v',# vertical tab (VT) 892"\000"=>'\0',# nul character (NUL) 893); 894my$chr= ( (exists$es{$cntrl}) 895?$es{$cntrl} 896:sprintf('\%2x',ord($cntrl)) ); 897if($opts{-nohtml}) { 898return$chr; 899}else{ 900return"<span class=\"cntrl\">$chr</span>"; 901} 902} 903 904# Alternatively use unicode control pictures codepoints, 905# Unicode "printable representation" (PR) 906sub quot_upr { 907my$cntrl=shift; 908my%opts=@_; 909 910my$chr=sprintf('&#%04d;',0x2400+ord($cntrl)); 911if($opts{-nohtml}) { 912return$chr; 913}else{ 914return"<span class=\"cntrl\">$chr</span>"; 915} 916} 917 918# git may return quoted and escaped filenames 919sub unquote { 920my$str=shift; 921 922sub unq { 923my$seq=shift; 924my%es= (# character escape codes, aka escape sequences 925't'=>"\t",# tab (HT, TAB) 926'n'=>"\n",# newline (NL) 927'r'=>"\r",# return (CR) 928'f'=>"\f",# form feed (FF) 929'b'=>"\b",# backspace (BS) 930'a'=>"\a",# alarm (bell) (BEL) 931'e'=>"\e",# escape (ESC) 932'v'=>"\013",# vertical tab (VT) 933); 934 935if($seq=~m/^[0-7]{1,3}$/) { 936# octal char sequence 937returnchr(oct($seq)); 938}elsif(exists$es{$seq}) { 939# C escape sequence, aka character escape code 940return$es{$seq}; 941} 942# quoted ordinary character 943return$seq; 944} 945 946if($str=~m/^"(.*)"$/) { 947# needs unquoting 948$str=$1; 949$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg; 950} 951return$str; 952} 953 954# escape tabs (convert tabs to spaces) 955sub untabify { 956my$line=shift; 957 958while((my$pos=index($line,"\t")) != -1) { 959if(my$count= (8- ($pos%8))) { 960my$spaces=' ' x $count; 961$line=~s/\t/$spaces/; 962} 963} 964 965return$line; 966} 967 968sub project_in_list { 969my$project=shift; 970my@list= git_get_projects_list(); 971return@list&&scalar(grep{$_->{'path'}eq$project}@list); 972} 973 974## ---------------------------------------------------------------------- 975## HTML aware string manipulation 976 977# Try to chop given string on a word boundary between position 978# $len and $len+$add_len. If there is no word boundary there, 979# chop at $len+$add_len. Do not chop if chopped part plus ellipsis 980# (marking chopped part) would be longer than given string. 981sub chop_str { 982my$str=shift; 983my$len=shift; 984my$add_len=shift||10; 985my$where=shift||'right';# 'left' | 'center' | 'right' 986 987# Make sure perl knows it is utf8 encoded so we don't 988# cut in the middle of a utf8 multibyte char. 989$str= to_utf8($str); 990 991# allow only $len chars, but don't cut a word if it would fit in $add_len 992# if it doesn't fit, cut it if it's still longer than the dots we would add 993# remove chopped character entities entirely 994 995# when chopping in the middle, distribute $len into left and right part 996# return early if chopping wouldn't make string shorter 997if($whereeq'center') { 998return$strif($len+5>=length($str));# filler is length 5 999$len=int($len/2);1000}else{1001return$strif($len+4>=length($str));# filler is length 41002}10031004# regexps: ending and beginning with word part up to $add_len1005my$endre=qr/.{$len}\w{0,$add_len}/;1006my$begre=qr/\w{0,$add_len}.{$len}/;10071008if($whereeq'left') {1009$str=~m/^(.*?)($begre)$/;1010my($lead,$body) = ($1,$2);1011if(length($lead) >4) {1012$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1013$lead=" ...";1014}1015return"$lead$body";10161017}elsif($whereeq'center') {1018$str=~m/^($endre)(.*)$/;1019my($left,$str) = ($1,$2);1020$str=~m/^(.*?)($begre)$/;1021my($mid,$right) = ($1,$2);1022if(length($mid) >5) {1023$left=~s/&[^;]*$//;1024$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1025$mid=" ... ";1026}1027return"$left$mid$right";10281029}else{1030$str=~m/^($endre)(.*)$/;1031my$body=$1;1032my$tail=$2;1033if(length($tail) >4) {1034$body=~s/&[^;]*$//;1035$tail="... ";1036}1037return"$body$tail";1038}1039}10401041# takes the same arguments as chop_str, but also wraps a <span> around the1042# result with a title attribute if it does get chopped. Additionally, the1043# string is HTML-escaped.1044sub chop_and_escape_str {1045my($str) =@_;10461047my$chopped= chop_str(@_);1048if($choppedeq$str) {1049return esc_html($chopped);1050}else{1051$str=~s/([[:cntrl:]])/?/g;1052return$cgi->span({-title=>$str}, esc_html($chopped));1053}1054}10551056## ----------------------------------------------------------------------1057## functions returning short strings10581059# CSS class for given age value (in seconds)1060sub age_class {1061my$age=shift;10621063if(!defined$age) {1064return"noage";1065}elsif($age<60*60*2) {1066return"age0";1067}elsif($age<60*60*24*2) {1068return"age1";1069}else{1070return"age2";1071}1072}10731074# convert age in seconds to "nn units ago" string1075sub age_string {1076my$age=shift;1077my$age_str;10781079if($age>60*60*24*365*2) {1080$age_str= (int$age/60/60/24/365);1081$age_str.=" years ago";1082}elsif($age>60*60*24*(365/12)*2) {1083$age_str=int$age/60/60/24/(365/12);1084$age_str.=" months ago";1085}elsif($age>60*60*24*7*2) {1086$age_str=int$age/60/60/24/7;1087$age_str.=" weeks ago";1088}elsif($age>60*60*24*2) {1089$age_str=int$age/60/60/24;1090$age_str.=" days ago";1091}elsif($age>60*60*2) {1092$age_str=int$age/60/60;1093$age_str.=" hours ago";1094}elsif($age>60*2) {1095$age_str=int$age/60;1096$age_str.=" min ago";1097}elsif($age>2) {1098$age_str=int$age;1099$age_str.=" sec ago";1100}else{1101$age_str.=" right now";1102}1103return$age_str;1104}11051106useconstant{1107 S_IFINVALID =>0030000,1108 S_IFGITLINK =>0160000,1109};11101111# submodule/subproject, a commit object reference1112sub S_ISGITLINK($) {1113my$mode=shift;11141115return(($mode& S_IFMT) == S_IFGITLINK)1116}11171118# convert file mode in octal to symbolic file mode string1119sub mode_str {1120my$mode=oct shift;11211122if(S_ISGITLINK($mode)) {1123return'm---------';1124}elsif(S_ISDIR($mode& S_IFMT)) {1125return'drwxr-xr-x';1126}elsif(S_ISLNK($mode)) {1127return'lrwxrwxrwx';1128}elsif(S_ISREG($mode)) {1129# git cares only about the executable bit1130if($mode& S_IXUSR) {1131return'-rwxr-xr-x';1132}else{1133return'-rw-r--r--';1134};1135}else{1136return'----------';1137}1138}11391140# convert file mode in octal to file type string1141sub file_type {1142my$mode=shift;11431144if($mode!~m/^[0-7]+$/) {1145return$mode;1146}else{1147$mode=oct$mode;1148}11491150if(S_ISGITLINK($mode)) {1151return"submodule";1152}elsif(S_ISDIR($mode& S_IFMT)) {1153return"directory";1154}elsif(S_ISLNK($mode)) {1155return"symlink";1156}elsif(S_ISREG($mode)) {1157return"file";1158}else{1159return"unknown";1160}1161}11621163# convert file mode in octal to file type description string1164sub file_type_long {1165my$mode=shift;11661167if($mode!~m/^[0-7]+$/) {1168return$mode;1169}else{1170$mode=oct$mode;1171}11721173if(S_ISGITLINK($mode)) {1174return"submodule";1175}elsif(S_ISDIR($mode& S_IFMT)) {1176return"directory";1177}elsif(S_ISLNK($mode)) {1178return"symlink";1179}elsif(S_ISREG($mode)) {1180if($mode& S_IXUSR) {1181return"executable";1182}else{1183return"file";1184};1185}else{1186return"unknown";1187}1188}118911901191## ----------------------------------------------------------------------1192## functions returning short HTML fragments, or transforming HTML fragments1193## which don't belong to other sections11941195# format line of commit message.1196sub format_log_line_html {1197my$line=shift;11981199$line= esc_html($line, -nbsp=>1);1200if($line=~m/([0-9a-fA-F]{8,40})/) {1201my$hash_text=$1;1202my$link=1203$cgi->a({-href => href(action=>"object", hash=>$hash_text),1204-class=>"text"},$hash_text);1205$line=~s/$hash_text/$link/;1206}1207return$line;1208}12091210# format marker of refs pointing to given object12111212# the destination action is chosen based on object type and current context:1213# - for annotated tags, we choose the tag view unless it's the current view1214# already, in which case we go to shortlog view1215# - for other refs, we keep the current view if we're in history, shortlog or1216# log view, and select shortlog otherwise1217sub format_ref_marker {1218my($refs,$id) =@_;1219my$markers='';12201221if(defined$refs->{$id}) {1222foreachmy$ref(@{$refs->{$id}}) {1223# this code exploits the fact that non-lightweight tags are the1224# only indirect objects, and that they are the only objects for which1225# we want to use tag instead of shortlog as action1226my($type,$name) =qw();1227my$indirect= ($ref=~s/\^\{\}$//);1228# e.g. tags/v2.6.11 or heads/next1229if($ref=~m!^(.*?)s?/(.*)$!) {1230$type=$1;1231$name=$2;1232}else{1233$type="ref";1234$name=$ref;1235}12361237my$class=$type;1238$class.=" indirect"if$indirect;12391240my$dest_action="shortlog";12411242if($indirect) {1243$dest_action="tag"unless$actioneq"tag";1244}elsif($action=~/^(history|(short)?log)$/) {1245$dest_action=$action;1246}12471248my$dest="";1249$dest.="refs/"unless$ref=~ m!^refs/!;1250$dest.=$ref;12511252my$link=$cgi->a({1253-href => href(1254 action=>$dest_action,1255 hash=>$dest1256)},$name);12571258$markers.=" <span class=\"$class\"title=\"$ref\">".1259$link."</span>";1260}1261}12621263if($markers) {1264return' <span class="refs">'.$markers.'</span>';1265}else{1266return"";1267}1268}12691270# format, perhaps shortened and with markers, title line1271sub format_subject_html {1272my($long,$short,$href,$extra) =@_;1273$extra=''unlessdefined($extra);12741275if(length($short) <length($long)) {1276return$cgi->a({-href =>$href, -class=>"list subject",1277-title => to_utf8($long)},1278 esc_html($short) .$extra);1279}else{1280return$cgi->a({-href =>$href, -class=>"list subject"},1281 esc_html($long) .$extra);1282}1283}12841285# format git diff header line, i.e. "diff --(git|combined|cc) ..."1286sub format_git_diff_header_line {1287my$line=shift;1288my$diffinfo=shift;1289my($from,$to) =@_;12901291if($diffinfo->{'nparents'}) {1292# combined diff1293$line=~s!^(diff (.*?) )"?.*$!$1!;1294if($to->{'href'}) {1295$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1296 esc_path($to->{'file'}));1297}else{# file was deleted (no href)1298$line.= esc_path($to->{'file'});1299}1300}else{1301# "ordinary" diff1302$line=~s!^(diff (.*?) )"?a/.*$!$1!;1303if($from->{'href'}) {1304$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1305'a/'. esc_path($from->{'file'}));1306}else{# file was added (no href)1307$line.='a/'. esc_path($from->{'file'});1308}1309$line.=' ';1310if($to->{'href'}) {1311$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1312'b/'. esc_path($to->{'file'}));1313}else{# file was deleted1314$line.='b/'. esc_path($to->{'file'});1315}1316}13171318return"<div class=\"diff header\">$line</div>\n";1319}13201321# format extended diff header line, before patch itself1322sub format_extended_diff_header_line {1323my$line=shift;1324my$diffinfo=shift;1325my($from,$to) =@_;13261327# match <path>1328if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1329$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1330 esc_path($from->{'file'}));1331}1332if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1333$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1334 esc_path($to->{'file'}));1335}1336# match single <mode>1337if($line=~m/\s(\d{6})$/) {1338$line.='<span class="info"> ('.1339 file_type_long($1) .1340')</span>';1341}1342# match <hash>1343if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1344# can match only for combined diff1345$line='index ';1346for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1347if($from->{'href'}[$i]) {1348$line.=$cgi->a({-href=>$from->{'href'}[$i],1349-class=>"hash"},1350substr($diffinfo->{'from_id'}[$i],0,7));1351}else{1352$line.='0' x 7;1353}1354# separator1355$line.=','if($i<$diffinfo->{'nparents'} -1);1356}1357$line.='..';1358if($to->{'href'}) {1359$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1360substr($diffinfo->{'to_id'},0,7));1361}else{1362$line.='0' x 7;1363}13641365}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1366# can match only for ordinary diff1367my($from_link,$to_link);1368if($from->{'href'}) {1369$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1370substr($diffinfo->{'from_id'},0,7));1371}else{1372$from_link='0' x 7;1373}1374if($to->{'href'}) {1375$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1376substr($diffinfo->{'to_id'},0,7));1377}else{1378$to_link='0' x 7;1379}1380my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1381$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1382}13831384return$line."<br/>\n";1385}13861387# format from-file/to-file diff header1388sub format_diff_from_to_header {1389my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1390my$line;1391my$result='';13921393$line=$from_line;1394#assert($line =~ m/^---/) if DEBUG;1395# no extra formatting for "^--- /dev/null"1396if(!$diffinfo->{'nparents'}) {1397# ordinary (single parent) diff1398if($line=~m!^--- "?a/!) {1399if($from->{'href'}) {1400$line='--- a/'.1401$cgi->a({-href=>$from->{'href'}, -class=>"path"},1402 esc_path($from->{'file'}));1403}else{1404$line='--- a/'.1405 esc_path($from->{'file'});1406}1407}1408$result.= qq!<div class="diff from_file">$line</div>\n!;14091410}else{1411# combined diff (merge commit)1412for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1413if($from->{'href'}[$i]) {1414$line='--- '.1415$cgi->a({-href=>href(action=>"blobdiff",1416 hash_parent=>$diffinfo->{'from_id'}[$i],1417 hash_parent_base=>$parents[$i],1418 file_parent=>$from->{'file'}[$i],1419 hash=>$diffinfo->{'to_id'},1420 hash_base=>$hash,1421 file_name=>$to->{'file'}),1422-class=>"path",1423-title=>"diff". ($i+1)},1424$i+1) .1425'/'.1426$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1427 esc_path($from->{'file'}[$i]));1428}else{1429$line='--- /dev/null';1430}1431$result.= qq!<div class="diff from_file">$line</div>\n!;1432}1433}14341435$line=$to_line;1436#assert($line =~ m/^\+\+\+/) if DEBUG;1437# no extra formatting for "^+++ /dev/null"1438if($line=~m!^\+\+\+ "?b/!) {1439if($to->{'href'}) {1440$line='+++ b/'.1441$cgi->a({-href=>$to->{'href'}, -class=>"path"},1442 esc_path($to->{'file'}));1443}else{1444$line='+++ b/'.1445 esc_path($to->{'file'});1446}1447}1448$result.= qq!<div class="diff to_file">$line</div>\n!;14491450return$result;1451}14521453# create note for patch simplified by combined diff1454sub format_diff_cc_simplified {1455my($diffinfo,@parents) =@_;1456my$result='';14571458$result.="<div class=\"diff header\">".1459"diff --cc ";1460if(!is_deleted($diffinfo)) {1461$result.=$cgi->a({-href => href(action=>"blob",1462 hash_base=>$hash,1463 hash=>$diffinfo->{'to_id'},1464 file_name=>$diffinfo->{'to_file'}),1465-class=>"path"},1466 esc_path($diffinfo->{'to_file'}));1467}else{1468$result.= esc_path($diffinfo->{'to_file'});1469}1470$result.="</div>\n".# class="diff header"1471"<div class=\"diff nodifferences\">".1472"Simple merge".1473"</div>\n";# class="diff nodifferences"14741475return$result;1476}14771478# format patch (diff) line (not to be used for diff headers)1479sub format_diff_line {1480my$line=shift;1481my($from,$to) =@_;1482my$diff_class="";14831484chomp$line;14851486if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1487# combined diff1488my$prefix=substr($line,0,scalar@{$from->{'href'}});1489if($line=~m/^\@{3}/) {1490$diff_class=" chunk_header";1491}elsif($line=~m/^\\/) {1492$diff_class=" incomplete";1493}elsif($prefix=~tr/+/+/) {1494$diff_class=" add";1495}elsif($prefix=~tr/-/-/) {1496$diff_class=" rem";1497}1498}else{1499# assume ordinary diff1500my$char=substr($line,0,1);1501if($chareq'+') {1502$diff_class=" add";1503}elsif($chareq'-') {1504$diff_class=" rem";1505}elsif($chareq'@') {1506$diff_class=" chunk_header";1507}elsif($chareq"\\") {1508$diff_class=" incomplete";1509}1510}1511$line= untabify($line);1512if($from&&$to&&$line=~m/^\@{2} /) {1513my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1514$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;15151516$from_lines=0unlessdefined$from_lines;1517$to_lines=0unlessdefined$to_lines;15181519if($from->{'href'}) {1520$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1521-class=>"list"},$from_text);1522}1523if($to->{'href'}) {1524$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1525-class=>"list"},$to_text);1526}1527$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1528"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1529return"<div class=\"diff$diff_class\">$line</div>\n";1530}elsif($from&&$to&&$line=~m/^\@{3}/) {1531my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1532my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);15331534@from_text=split(' ',$ranges);1535for(my$i=0;$i<@from_text; ++$i) {1536($from_start[$i],$from_nlines[$i]) =1537(split(',',substr($from_text[$i],1)),0);1538}15391540$to_text=pop@from_text;1541$to_start=pop@from_start;1542$to_nlines=pop@from_nlines;15431544$line="<span class=\"chunk_info\">$prefix";1545for(my$i=0;$i<@from_text; ++$i) {1546if($from->{'href'}[$i]) {1547$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1548-class=>"list"},$from_text[$i]);1549}else{1550$line.=$from_text[$i];1551}1552$line.=" ";1553}1554if($to->{'href'}) {1555$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1556-class=>"list"},$to_text);1557}else{1558$line.=$to_text;1559}1560$line.="$prefix</span>".1561"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1562return"<div class=\"diff$diff_class\">$line</div>\n";1563}1564return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1565}15661567# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1568# linked. Pass the hash of the tree/commit to snapshot.1569sub format_snapshot_links {1570my($hash) =@_;1571my@snapshot_fmts= gitweb_check_feature('snapshot');1572@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);1573my$num_fmts=@snapshot_fmts;1574if($num_fmts>1) {1575# A parenthesized list of links bearing format names.1576# e.g. "snapshot (_tar.gz_ _zip_)"1577return"snapshot (".join(' ',map1578$cgi->a({1579-href => href(1580 action=>"snapshot",1581 hash=>$hash,1582 snapshot_format=>$_1583)1584},$known_snapshot_formats{$_}{'display'})1585,@snapshot_fmts) .")";1586}elsif($num_fmts==1) {1587# A single "snapshot" link whose tooltip bears the format name.1588# i.e. "_snapshot_"1589my($fmt) =@snapshot_fmts;1590return1591$cgi->a({1592-href => href(1593 action=>"snapshot",1594 hash=>$hash,1595 snapshot_format=>$fmt1596),1597-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1598},"snapshot");1599}else{# $num_fmts == 01600returnundef;1601}1602}16031604## ......................................................................1605## functions returning values to be passed, perhaps after some1606## transformation, to other functions; e.g. returning arguments to href()16071608# returns hash to be passed to href to generate gitweb URL1609# in -title key it returns description of link1610sub get_feed_info {1611my$format=shift||'Atom';1612my%res= (action =>lc($format));16131614# feed links are possible only for project views1615return unless(defined$project);1616# some views should link to OPML, or to generic project feed,1617# or don't have specific feed yet (so they should use generic)1618return if($action=~/^(?:tags|heads|forks|tag|search)$/x);16191620my$branch;1621# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1622# from tag links; this also makes possible to detect branch links1623if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1624(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1625$branch=$1;1626}1627# find log type for feed description (title)1628my$type='log';1629if(defined$file_name) {1630$type="history of$file_name";1631$type.="/"if($actioneq'tree');1632$type.=" on '$branch'"if(defined$branch);1633}else{1634$type="log of$branch"if(defined$branch);1635}16361637$res{-title} =$type;1638$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1639$res{'file_name'} =$file_name;16401641return%res;1642}16431644## ----------------------------------------------------------------------1645## git utility subroutines, invoking git commands16461647# returns path to the core git executable and the --git-dir parameter as list1648sub git_cmd {1649return$GIT,'--git-dir='.$git_dir;1650}16511652# quote the given arguments for passing them to the shell1653# quote_command("command", "arg 1", "arg with ' and ! characters")1654# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1655# Try to avoid using this function wherever possible.1656sub quote_command {1657returnjoin(' ',1658map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1659}16601661# get HEAD ref of given project as hash1662sub git_get_head_hash {1663my$project=shift;1664my$o_git_dir=$git_dir;1665my$retval=undef;1666$git_dir="$projectroot/$project";1667if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1668my$head= <$fd>;1669close$fd;1670if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1671$retval=$1;1672}1673}1674if(defined$o_git_dir) {1675$git_dir=$o_git_dir;1676}1677return$retval;1678}16791680# get type of given object1681sub git_get_type {1682my$hash=shift;16831684open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1685my$type= <$fd>;1686close$fdorreturn;1687chomp$type;1688return$type;1689}16901691# repository configuration1692our$config_file='';1693our%config;16941695# store multiple values for single key as anonymous array reference1696# single values stored directly in the hash, not as [ <value> ]1697sub hash_set_multi {1698my($hash,$key,$value) =@_;16991700if(!exists$hash->{$key}) {1701$hash->{$key} =$value;1702}elsif(!ref$hash->{$key}) {1703$hash->{$key} = [$hash->{$key},$value];1704}else{1705push@{$hash->{$key}},$value;1706}1707}17081709# return hash of git project configuration1710# optionally limited to some section, e.g. 'gitweb'1711sub git_parse_project_config {1712my$section_regexp=shift;1713my%config;17141715local$/="\0";17161717open my$fh,"-|", git_cmd(),"config",'-z','-l',1718orreturn;17191720while(my$keyval= <$fh>) {1721chomp$keyval;1722my($key,$value) =split(/\n/,$keyval,2);17231724 hash_set_multi(\%config,$key,$value)1725if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1726}1727close$fh;17281729return%config;1730}17311732# convert config value to boolean, 'true' or 'false'1733# no value, number > 0, 'true' and 'yes' values are true1734# rest of values are treated as false (never as error)1735sub config_to_bool {1736my$val=shift;17371738# strip leading and trailing whitespace1739$val=~s/^\s+//;1740$val=~s/\s+$//;17411742return(!defined$val||# section.key1743($val=~/^\d+$/&&$val) ||# section.key = 11744($val=~/^(?:true|yes)$/i));# section.key = true1745}17461747# convert config value to simple decimal number1748# an optional value suffix of 'k', 'm', or 'g' will cause the value1749# to be multiplied by 1024, 1048576, or 10737418241750sub config_to_int {1751my$val=shift;17521753# strip leading and trailing whitespace1754$val=~s/^\s+//;1755$val=~s/\s+$//;17561757if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1758$unit=lc($unit);1759# unknown unit is treated as 11760return$num* ($uniteq'g'?1073741824:1761$uniteq'm'?1048576:1762$uniteq'k'?1024:1);1763}1764return$val;1765}17661767# convert config value to array reference, if needed1768sub config_to_multi {1769my$val=shift;17701771returnref($val) ?$val: (defined($val) ? [$val] : []);1772}17731774sub git_get_project_config {1775my($key,$type) =@_;17761777# key sanity check1778return unless($key);1779$key=~s/^gitweb\.//;1780return if($key=~m/\W/);17811782# type sanity check1783if(defined$type) {1784$type=~s/^--//;1785$type=undef1786unless($typeeq'bool'||$typeeq'int');1787}17881789# get config1790if(!defined$config_file||1791$config_filene"$git_dir/config") {1792%config= git_parse_project_config('gitweb');1793$config_file="$git_dir/config";1794}17951796# ensure given type1797if(!defined$type) {1798return$config{"gitweb.$key"};1799}elsif($typeeq'bool') {1800# backward compatibility: 'git config --bool' returns true/false1801return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1802}elsif($typeeq'int') {1803return config_to_int($config{"gitweb.$key"});1804}1805return$config{"gitweb.$key"};1806}18071808# get hash of given path at given ref1809sub git_get_hash_by_path {1810my$base=shift;1811my$path=shift||returnundef;1812my$type=shift;18131814$path=~ s,/+$,,;18151816open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path1817or die_error(500,"Open git-ls-tree failed");1818my$line= <$fd>;1819close$fdorreturnundef;18201821if(!defined$line) {1822# there is no tree or hash given by $path at $base1823returnundef;1824}18251826#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'1827$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;1828if(defined$type&&$typene$2) {1829# type doesn't match1830returnundef;1831}1832return$3;1833}18341835# get path of entry with given hash at given tree-ish (ref)1836# used to get 'from' filename for combined diff (merge commit) for renames1837sub git_get_path_by_hash {1838my$base=shift||return;1839my$hash=shift||return;18401841local$/="\0";18421843open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base1844orreturnundef;1845while(my$line= <$fd>) {1846chomp$line;18471848#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'1849#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'1850if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {1851close$fd;1852return$1;1853}1854}1855close$fd;1856returnundef;1857}18581859## ......................................................................1860## git utility functions, directly accessing git repository18611862sub git_get_project_description {1863my$path=shift;18641865$git_dir="$projectroot/$path";1866open my$fd,"$git_dir/description"1867orreturn git_get_project_config('description');1868my$descr= <$fd>;1869close$fd;1870if(defined$descr) {1871chomp$descr;1872}1873return$descr;1874}18751876sub git_get_project_ctags {1877my$path=shift;1878my$ctags= {};18791880$git_dir="$projectroot/$path";1881foreach(<$git_dir/ctags/*>) {1882open CT,$_ornext;1883my$val= <CT>;1884chomp$val;1885close CT;1886my$ctag=$_;$ctag=~ s#.*/##;1887$ctags->{$ctag} =$val;1888}1889$ctags;1890}18911892sub git_populate_project_tagcloud {1893my$ctags=shift;18941895# First, merge different-cased tags; tags vote on casing1896my%ctags_lc;1897foreach(keys%$ctags) {1898$ctags_lc{lc$_}->{count} +=$ctags->{$_};1899if(not$ctags_lc{lc$_}->{topcount}1900or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {1901$ctags_lc{lc$_}->{topcount} =$ctags->{$_};1902$ctags_lc{lc$_}->{topname} =$_;1903}1904}19051906my$cloud;1907if(eval{require HTML::TagCloud;1; }) {1908$cloud= HTML::TagCloud->new;1909foreach(sort keys%ctags_lc) {1910# Pad the title with spaces so that the cloud looks1911# less crammed.1912my$title=$ctags_lc{$_}->{topname};1913$title=~s/ / /g;1914$title=~s/^/ /g;1915$title=~s/$/ /g;1916$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});1917}1918}else{1919$cloud= \%ctags_lc;1920}1921$cloud;1922}19231924sub git_show_project_tagcloud {1925my($cloud,$count) =@_;1926print STDERR ref($cloud)."..\n";1927if(ref$cloudeq'HTML::TagCloud') {1928return$cloud->html_and_css($count);1929}else{1930my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;1931return'<p align="center">'.join(', ',map{1932"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"1933}splice(@tags,0,$count)) .'</p>';1934}1935}19361937sub git_get_project_url_list {1938my$path=shift;19391940$git_dir="$projectroot/$path";1941open my$fd,"$git_dir/cloneurl"1942orreturnwantarray?1943@{ config_to_multi(git_get_project_config('url')) } :1944 config_to_multi(git_get_project_config('url'));1945my@git_project_url_list=map{chomp;$_} <$fd>;1946close$fd;19471948returnwantarray?@git_project_url_list: \@git_project_url_list;1949}19501951sub git_get_projects_list {1952my($filter) =@_;1953my@list;19541955$filter||='';1956$filter=~s/\.git$//;19571958my($check_forks) = gitweb_check_feature('forks');19591960if(-d $projects_list) {1961# search in directory1962my$dir=$projects_list. ($filter?"/$filter":'');1963# remove the trailing "/"1964$dir=~s!/+$!!;1965my$pfxlen=length("$dir");1966my$pfxdepth= ($dir=~tr!/!!);19671968 File::Find::find({1969 follow_fast =>1,# follow symbolic links1970 follow_skip =>2,# ignore duplicates1971 dangling_symlinks =>0,# ignore dangling symlinks, silently1972 wanted =>sub{1973# skip project-list toplevel, if we get it.1974return if(m!^[/.]$!);1975# only directories can be git repositories1976return unless(-d $_);1977# don't traverse too deep (Find is super slow on os x)1978if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {1979$File::Find::prune =1;1980return;1981}19821983my$subdir=substr($File::Find::name,$pfxlen+1);1984# we check related file in $projectroot1985if(check_export_ok("$projectroot/$filter/$subdir")) {1986push@list, { path => ($filter?"$filter/":'') .$subdir};1987$File::Find::prune =1;1988}1989},1990},"$dir");19911992}elsif(-f $projects_list) {1993# read from file(url-encoded):1994# 'git%2Fgit.git Linus+Torvalds'1995# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'1996# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'1997my%paths;1998open my($fd),$projects_listorreturn;1999 PROJECT:2000while(my$line= <$fd>) {2001chomp$line;2002my($path,$owner) =split' ',$line;2003$path= unescape($path);2004$owner= unescape($owner);2005if(!defined$path) {2006next;2007}2008if($filterne'') {2009# looking for forks;2010my$pfx=substr($path,0,length($filter));2011if($pfxne$filter) {2012next PROJECT;2013}2014my$sfx=substr($path,length($filter));2015if($sfx!~/^\/.*\.git$/) {2016next PROJECT;2017}2018}elsif($check_forks) {2019 PATH:2020foreachmy$filter(keys%paths) {2021# looking for forks;2022my$pfx=substr($path,0,length($filter));2023if($pfxne$filter) {2024next PATH;2025}2026my$sfx=substr($path,length($filter));2027if($sfx!~/^\/.*\.git$/) {2028next PATH;2029}2030# is a fork, don't include it in2031# the list2032next PROJECT;2033}2034}2035if(check_export_ok("$projectroot/$path")) {2036my$pr= {2037 path =>$path,2038 owner => to_utf8($owner),2039};2040push@list,$pr;2041(my$forks_path=$path) =~s/\.git$//;2042$paths{$forks_path}++;2043}2044}2045close$fd;2046}2047return@list;2048}20492050our$gitweb_project_owner=undef;2051sub git_get_project_list_from_file {20522053return if(defined$gitweb_project_owner);20542055$gitweb_project_owner= {};2056# read from file (url-encoded):2057# 'git%2Fgit.git Linus+Torvalds'2058# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2059# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2060if(-f $projects_list) {2061open(my$fd,$projects_list);2062while(my$line= <$fd>) {2063chomp$line;2064my($pr,$ow) =split' ',$line;2065$pr= unescape($pr);2066$ow= unescape($ow);2067$gitweb_project_owner->{$pr} = to_utf8($ow);2068}2069close$fd;2070}2071}20722073sub git_get_project_owner {2074my$project=shift;2075my$owner;20762077returnundefunless$project;2078$git_dir="$projectroot/$project";20792080if(!defined$gitweb_project_owner) {2081 git_get_project_list_from_file();2082}20832084if(exists$gitweb_project_owner->{$project}) {2085$owner=$gitweb_project_owner->{$project};2086}2087if(!defined$owner){2088$owner= git_get_project_config('owner');2089}2090if(!defined$owner) {2091$owner= get_file_owner("$git_dir");2092}20932094return$owner;2095}20962097sub git_get_last_activity {2098my($path) =@_;2099my$fd;21002101$git_dir="$projectroot/$path";2102open($fd,"-|", git_cmd(),'for-each-ref',2103'--format=%(committer)',2104'--sort=-committerdate',2105'--count=1',2106'refs/heads')orreturn;2107my$most_recent= <$fd>;2108close$fdorreturn;2109if(defined$most_recent&&2110$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2111my$timestamp=$1;2112my$age=time-$timestamp;2113return($age, age_string($age));2114}2115return(undef,undef);2116}21172118sub git_get_references {2119my$type=shift||"";2120my%refs;2121# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112122# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2123open my$fd,"-|", git_cmd(),"show-ref","--dereference",2124($type? ("--","refs/$type") : ())# use -- <pattern> if $type2125orreturn;21262127while(my$line= <$fd>) {2128chomp$line;2129if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2130if(defined$refs{$1}) {2131push@{$refs{$1}},$2;2132}else{2133$refs{$1} = [$2];2134}2135}2136}2137close$fdorreturn;2138return \%refs;2139}21402141sub git_get_rev_name_tags {2142my$hash=shift||returnundef;21432144open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2145orreturn;2146my$name_rev= <$fd>;2147close$fd;21482149if($name_rev=~ m|^$hash tags/(.*)$|) {2150return$1;2151}else{2152# catches also '$hash undefined' output2153returnundef;2154}2155}21562157## ----------------------------------------------------------------------2158## parse to hash functions21592160sub parse_date {2161my$epoch=shift;2162my$tz=shift||"-0000";21632164my%date;2165my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2166my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2167my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2168$date{'hour'} =$hour;2169$date{'minute'} =$min;2170$date{'mday'} =$mday;2171$date{'day'} =$days[$wday];2172$date{'month'} =$months[$mon];2173$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2174$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2175$date{'mday-time'} =sprintf"%d%s%02d:%02d",2176$mday,$months[$mon],$hour,$min;2177$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",21781900+$year,1+$mon,$mday,$hour,$min,$sec;21792180$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2181my$local=$epoch+ ((int$1+ ($2/60)) *3600);2182($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2183$date{'hour_local'} =$hour;2184$date{'minute_local'} =$min;2185$date{'tz_local'} =$tz;2186$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",21871900+$year,$mon+1,$mday,2188$hour,$min,$sec,$tz);2189return%date;2190}21912192sub parse_tag {2193my$tag_id=shift;2194my%tag;2195my@comment;21962197open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2198$tag{'id'} =$tag_id;2199while(my$line= <$fd>) {2200chomp$line;2201if($line=~m/^object ([0-9a-fA-F]{40})$/) {2202$tag{'object'} =$1;2203}elsif($line=~m/^type (.+)$/) {2204$tag{'type'} =$1;2205}elsif($line=~m/^tag (.+)$/) {2206$tag{'name'} =$1;2207}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2208$tag{'author'} =$1;2209$tag{'epoch'} =$2;2210$tag{'tz'} =$3;2211}elsif($line=~m/--BEGIN/) {2212push@comment,$line;2213last;2214}elsif($lineeq"") {2215last;2216}2217}2218push@comment, <$fd>;2219$tag{'comment'} = \@comment;2220close$fdorreturn;2221if(!defined$tag{'name'}) {2222return2223};2224return%tag2225}22262227sub parse_commit_text {2228my($commit_text,$withparents) =@_;2229my@commit_lines=split'\n',$commit_text;2230my%co;22312232pop@commit_lines;# Remove '\0'22332234if(!@commit_lines) {2235return;2236}22372238my$header=shift@commit_lines;2239if($header!~m/^[0-9a-fA-F]{40}/) {2240return;2241}2242($co{'id'},my@parents) =split' ',$header;2243while(my$line=shift@commit_lines) {2244last if$lineeq"\n";2245if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2246$co{'tree'} =$1;2247}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2248push@parents,$1;2249}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2250$co{'author'} =$1;2251$co{'author_epoch'} =$2;2252$co{'author_tz'} =$3;2253if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2254$co{'author_name'} =$1;2255$co{'author_email'} =$2;2256}else{2257$co{'author_name'} =$co{'author'};2258}2259}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2260$co{'committer'} =$1;2261$co{'committer_epoch'} =$2;2262$co{'committer_tz'} =$3;2263$co{'committer_name'} =$co{'committer'};2264if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2265$co{'committer_name'} =$1;2266$co{'committer_email'} =$2;2267}else{2268$co{'committer_name'} =$co{'committer'};2269}2270}2271}2272if(!defined$co{'tree'}) {2273return;2274};2275$co{'parents'} = \@parents;2276$co{'parent'} =$parents[0];22772278foreachmy$title(@commit_lines) {2279$title=~s/^ //;2280if($titlene"") {2281$co{'title'} = chop_str($title,80,5);2282# remove leading stuff of merges to make the interesting part visible2283if(length($title) >50) {2284$title=~s/^Automatic //;2285$title=~s/^merge (of|with) /Merge ... /i;2286if(length($title) >50) {2287$title=~s/(http|rsync):\/\///;2288}2289if(length($title) >50) {2290$title=~s/(master|www|rsync)\.//;2291}2292if(length($title) >50) {2293$title=~s/kernel.org:?//;2294}2295if(length($title) >50) {2296$title=~s/\/pub\/scm//;2297}2298}2299$co{'title_short'} = chop_str($title,50,5);2300last;2301}2302}2303if(!defined$co{'title'} ||$co{'title'}eq"") {2304$co{'title'} =$co{'title_short'} ='(no commit message)';2305}2306# remove added spaces2307foreachmy$line(@commit_lines) {2308$line=~s/^ //;2309}2310$co{'comment'} = \@commit_lines;23112312my$age=time-$co{'committer_epoch'};2313$co{'age'} =$age;2314$co{'age_string'} = age_string($age);2315my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2316if($age>60*60*24*7*2) {2317$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2318$co{'age_string_age'} =$co{'age_string'};2319}else{2320$co{'age_string_date'} =$co{'age_string'};2321$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2322}2323return%co;2324}23252326sub parse_commit {2327my($commit_id) =@_;2328my%co;23292330local$/="\0";23312332open my$fd,"-|", git_cmd(),"rev-list",2333"--parents",2334"--header",2335"--max-count=1",2336$commit_id,2337"--",2338or die_error(500,"Open git-rev-list failed");2339%co= parse_commit_text(<$fd>,1);2340close$fd;23412342return%co;2343}23442345sub parse_commits {2346my($commit_id,$maxcount,$skip,$filename,@args) =@_;2347my@cos;23482349$maxcount||=1;2350$skip||=0;23512352local$/="\0";23532354open my$fd,"-|", git_cmd(),"rev-list",2355"--header",2356@args,2357("--max-count=".$maxcount),2358("--skip=".$skip),2359@extra_options,2360$commit_id,2361"--",2362($filename? ($filename) : ())2363or die_error(500,"Open git-rev-list failed");2364while(my$line= <$fd>) {2365my%co= parse_commit_text($line);2366push@cos, \%co;2367}2368close$fd;23692370returnwantarray?@cos: \@cos;2371}23722373# parse line of git-diff-tree "raw" output2374sub parse_difftree_raw_line {2375my$line=shift;2376my%res;23772378# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2379# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2380if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2381$res{'from_mode'} =$1;2382$res{'to_mode'} =$2;2383$res{'from_id'} =$3;2384$res{'to_id'} =$4;2385$res{'status'} =$5;2386$res{'similarity'} =$6;2387if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2388($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2389}else{2390$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2391}2392}2393# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2394# combined diff (for merge commit)2395elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2396$res{'nparents'} =length($1);2397$res{'from_mode'} = [split(' ',$2) ];2398$res{'to_mode'} =pop@{$res{'from_mode'}};2399$res{'from_id'} = [split(' ',$3) ];2400$res{'to_id'} =pop@{$res{'from_id'}};2401$res{'status'} = [split('',$4) ];2402$res{'to_file'} = unquote($5);2403}2404# 'c512b523472485aef4fff9e57b229d9d243c967f'2405elsif($line=~m/^([0-9a-fA-F]{40})$/) {2406$res{'commit'} =$1;2407}24082409returnwantarray?%res: \%res;2410}24112412# wrapper: return parsed line of git-diff-tree "raw" output2413# (the argument might be raw line, or parsed info)2414sub parsed_difftree_line {2415my$line_or_ref=shift;24162417if(ref($line_or_ref)eq"HASH") {2418# pre-parsed (or generated by hand)2419return$line_or_ref;2420}else{2421return parse_difftree_raw_line($line_or_ref);2422}2423}24242425# parse line of git-ls-tree output2426sub parse_ls_tree_line ($;%) {2427my$line=shift;2428my%opts=@_;2429my%res;24302431#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2432$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;24332434$res{'mode'} =$1;2435$res{'type'} =$2;2436$res{'hash'} =$3;2437if($opts{'-z'}) {2438$res{'name'} =$4;2439}else{2440$res{'name'} = unquote($4);2441}24422443returnwantarray?%res: \%res;2444}24452446# generates _two_ hashes, references to which are passed as 2 and 3 argument2447sub parse_from_to_diffinfo {2448my($diffinfo,$from,$to,@parents) =@_;24492450if($diffinfo->{'nparents'}) {2451# combined diff2452$from->{'file'} = [];2453$from->{'href'} = [];2454 fill_from_file_info($diffinfo,@parents)2455unlessexists$diffinfo->{'from_file'};2456for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2457$from->{'file'}[$i] =2458defined$diffinfo->{'from_file'}[$i] ?2459$diffinfo->{'from_file'}[$i] :2460$diffinfo->{'to_file'};2461if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2462$from->{'href'}[$i] = href(action=>"blob",2463 hash_base=>$parents[$i],2464 hash=>$diffinfo->{'from_id'}[$i],2465 file_name=>$from->{'file'}[$i]);2466}else{2467$from->{'href'}[$i] =undef;2468}2469}2470}else{2471# ordinary (not combined) diff2472$from->{'file'} =$diffinfo->{'from_file'};2473if($diffinfo->{'status'}ne"A") {# not new (added) file2474$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2475 hash=>$diffinfo->{'from_id'},2476 file_name=>$from->{'file'});2477}else{2478delete$from->{'href'};2479}2480}24812482$to->{'file'} =$diffinfo->{'to_file'};2483if(!is_deleted($diffinfo)) {# file exists in result2484$to->{'href'} = href(action=>"blob", hash_base=>$hash,2485 hash=>$diffinfo->{'to_id'},2486 file_name=>$to->{'file'});2487}else{2488delete$to->{'href'};2489}2490}24912492## ......................................................................2493## parse to array of hashes functions24942495sub git_get_heads_list {2496my$limit=shift;2497my@headslist;24982499open my$fd,'-|', git_cmd(),'for-each-ref',2500($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2501'--format=%(objectname) %(refname) %(subject)%00%(committer)',2502'refs/heads'2503orreturn;2504while(my$line= <$fd>) {2505my%ref_item;25062507chomp$line;2508my($refinfo,$committerinfo) =split(/\0/,$line);2509my($hash,$name,$title) =split(' ',$refinfo,3);2510my($committer,$epoch,$tz) =2511($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2512$ref_item{'fullname'} =$name;2513$name=~s!^refs/heads/!!;25142515$ref_item{'name'} =$name;2516$ref_item{'id'} =$hash;2517$ref_item{'title'} =$title||'(no commit message)';2518$ref_item{'epoch'} =$epoch;2519if($epoch) {2520$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2521}else{2522$ref_item{'age'} ="unknown";2523}25242525push@headslist, \%ref_item;2526}2527close$fd;25282529returnwantarray?@headslist: \@headslist;2530}25312532sub git_get_tags_list {2533my$limit=shift;2534my@tagslist;25352536open my$fd,'-|', git_cmd(),'for-each-ref',2537($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2538'--format=%(objectname) %(objecttype) %(refname) '.2539'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2540'refs/tags'2541orreturn;2542while(my$line= <$fd>) {2543my%ref_item;25442545chomp$line;2546my($refinfo,$creatorinfo) =split(/\0/,$line);2547my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2548my($creator,$epoch,$tz) =2549($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2550$ref_item{'fullname'} =$name;2551$name=~s!^refs/tags/!!;25522553$ref_item{'type'} =$type;2554$ref_item{'id'} =$id;2555$ref_item{'name'} =$name;2556if($typeeq"tag") {2557$ref_item{'subject'} =$title;2558$ref_item{'reftype'} =$reftype;2559$ref_item{'refid'} =$refid;2560}else{2561$ref_item{'reftype'} =$type;2562$ref_item{'refid'} =$id;2563}25642565if($typeeq"tag"||$typeeq"commit") {2566$ref_item{'epoch'} =$epoch;2567if($epoch) {2568$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2569}else{2570$ref_item{'age'} ="unknown";2571}2572}25732574push@tagslist, \%ref_item;2575}2576close$fd;25772578returnwantarray?@tagslist: \@tagslist;2579}25802581## ----------------------------------------------------------------------2582## filesystem-related functions25832584sub get_file_owner {2585my$path=shift;25862587my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2588my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2589if(!defined$gcos) {2590returnundef;2591}2592my$owner=$gcos;2593$owner=~s/[,;].*$//;2594return to_utf8($owner);2595}25962597## ......................................................................2598## mimetype related functions25992600sub mimetype_guess_file {2601my$filename=shift;2602my$mimemap=shift;2603-r $mimemaporreturnundef;26042605my%mimemap;2606open(MIME,$mimemap)orreturnundef;2607while(<MIME>) {2608next ifm/^#/;# skip comments2609my($mime,$exts) =split(/\t+/);2610if(defined$exts) {2611my@exts=split(/\s+/,$exts);2612foreachmy$ext(@exts) {2613$mimemap{$ext} =$mime;2614}2615}2616}2617close(MIME);26182619$filename=~/\.([^.]*)$/;2620return$mimemap{$1};2621}26222623sub mimetype_guess {2624my$filename=shift;2625my$mime;2626$filename=~/\./orreturnundef;26272628if($mimetypes_file) {2629my$file=$mimetypes_file;2630if($file!~m!^/!) {# if it is relative path2631# it is relative to project2632$file="$projectroot/$project/$file";2633}2634$mime= mimetype_guess_file($filename,$file);2635}2636$mime||= mimetype_guess_file($filename,'/etc/mime.types');2637return$mime;2638}26392640sub blob_mimetype {2641my$fd=shift;2642my$filename=shift;26432644if($filename) {2645my$mime= mimetype_guess($filename);2646$mimeandreturn$mime;2647}26482649# just in case2650return$default_blob_plain_mimetypeunless$fd;26512652if(-T $fd) {2653return'text/plain';2654}elsif(!$filename) {2655return'application/octet-stream';2656}elsif($filename=~m/\.png$/i) {2657return'image/png';2658}elsif($filename=~m/\.gif$/i) {2659return'image/gif';2660}elsif($filename=~m/\.jpe?g$/i) {2661return'image/jpeg';2662}else{2663return'application/octet-stream';2664}2665}26662667sub blob_contenttype {2668my($fd,$file_name,$type) =@_;26692670$type||= blob_mimetype($fd,$file_name);2671if($typeeq'text/plain'&&defined$default_text_plain_charset) {2672$type.="; charset=$default_text_plain_charset";2673}26742675return$type;2676}26772678## ======================================================================2679## functions printing HTML: header, footer, error page26802681sub git_header_html {2682my$status=shift||"200 OK";2683my$expires=shift;26842685my$title="$site_name";2686if(defined$project) {2687$title.=" - ". to_utf8($project);2688if(defined$action) {2689$title.="/$action";2690if(defined$file_name) {2691$title.=" - ". esc_path($file_name);2692if($actioneq"tree"&&$file_name!~ m|/$|) {2693$title.="/";2694}2695}2696}2697}2698my$content_type;2699# require explicit support from the UA if we are to send the page as2700# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2701# we have to do this because MSIE sometimes globs '*/*', pretending to2702# support xhtml+xml but choking when it gets what it asked for.2703if(defined$cgi->http('HTTP_ACCEPT') &&2704$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2705$cgi->Accept('application/xhtml+xml') !=0) {2706$content_type='application/xhtml+xml';2707}else{2708$content_type='text/html';2709}2710print$cgi->header(-type=>$content_type, -charset =>'utf-8',2711-status=>$status, -expires =>$expires);2712my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2713print<<EOF;2714<?xml version="1.0" encoding="utf-8"?>2715<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2716<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2717<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2718<!-- git core binaries version$git_version-->2719<head>2720<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2721<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2722<meta name="robots" content="index, nofollow"/>2723<title>$title</title>2724EOF2725# print out each stylesheet that exist2726if(defined$stylesheet) {2727#provides backwards capability for those people who define style sheet in a config file2728print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2729}else{2730foreachmy$stylesheet(@stylesheets) {2731next unless$stylesheet;2732print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2733}2734}2735if(defined$project) {2736my%href_params= get_feed_info();2737if(!exists$href_params{'-title'}) {2738$href_params{'-title'} ='log';2739}27402741foreachmy$formatqw(RSS Atom){2742my$type=lc($format);2743my%link_attr= (2744'-rel'=>'alternate',2745'-title'=>"$project-$href_params{'-title'} -$formatfeed",2746'-type'=>"application/$type+xml"2747);27482749$href_params{'action'} =$type;2750$link_attr{'-href'} = href(%href_params);2751print"<link ".2752"rel=\"$link_attr{'-rel'}\"".2753"title=\"$link_attr{'-title'}\"".2754"href=\"$link_attr{'-href'}\"".2755"type=\"$link_attr{'-type'}\"".2756"/>\n";27572758$href_params{'extra_options'} ='--no-merges';2759$link_attr{'-href'} = href(%href_params);2760$link_attr{'-title'} .=' (no merges)';2761print"<link ".2762"rel=\"$link_attr{'-rel'}\"".2763"title=\"$link_attr{'-title'}\"".2764"href=\"$link_attr{'-href'}\"".2765"type=\"$link_attr{'-type'}\"".2766"/>\n";2767}27682769}else{2770printf('<link rel="alternate" title="%sprojects list" '.2771'href="%s" type="text/plain; charset=utf-8" />'."\n",2772$site_name, href(project=>undef, action=>"project_index"));2773printf('<link rel="alternate" title="%sprojects feeds" '.2774'href="%s" type="text/x-opml" />'."\n",2775$site_name, href(project=>undef, action=>"opml"));2776}2777if(defined$favicon) {2778printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2779}27802781print"</head>\n".2782"<body>\n";27832784if(-f $site_header) {2785open(my$fd,$site_header);2786print<$fd>;2787close$fd;2788}27892790print"<div class=\"page_header\">\n".2791$cgi->a({-href => esc_url($logo_url),2792-title =>$logo_label},2793qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2794print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2795if(defined$project) {2796print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2797if(defined$action) {2798print" /$action";2799}2800print"\n";2801}2802print"</div>\n";28032804my($have_search) = gitweb_check_feature('search');2805if(defined$project&&$have_search) {2806if(!defined$searchtext) {2807$searchtext="";2808}2809my$search_hash;2810if(defined$hash_base) {2811$search_hash=$hash_base;2812}elsif(defined$hash) {2813$search_hash=$hash;2814}else{2815$search_hash="HEAD";2816}2817my$action=$my_uri;2818my($use_pathinfo) = gitweb_check_feature('pathinfo');2819if($use_pathinfo) {2820$action.="/".esc_url($project);2821}2822print$cgi->startform(-method=>"get", -action =>$action) .2823"<div class=\"search\">\n".2824(!$use_pathinfo&&2825$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .2826$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".2827$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".2828$cgi->popup_menu(-name =>'st', -default=>'commit',2829-values=> ['commit','grep','author','committer','pickaxe']) .2830$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .2831" search:\n",2832$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".2833"<span title=\"Extended regular expression\">".2834$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',2835-checked =>$search_use_regexp) .2836"</span>".2837"</div>".2838$cgi->end_form() ."\n";2839}2840}28412842sub git_footer_html {2843my$feed_class='rss_logo';28442845print"<div class=\"page_footer\">\n";2846if(defined$project) {2847my$descr= git_get_project_description($project);2848if(defined$descr) {2849print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";2850}28512852my%href_params= get_feed_info();2853if(!%href_params) {2854$feed_class.=' generic';2855}2856$href_params{'-title'} ||='log';28572858foreachmy$formatqw(RSS Atom){2859$href_params{'action'} =lc($format);2860print$cgi->a({-href => href(%href_params),2861-title =>"$href_params{'-title'}$formatfeed",2862-class=>$feed_class},$format)."\n";2863}28642865}else{2866print$cgi->a({-href => href(project=>undef, action=>"opml"),2867-class=>$feed_class},"OPML") ." ";2868print$cgi->a({-href => href(project=>undef, action=>"project_index"),2869-class=>$feed_class},"TXT") ."\n";2870}2871print"</div>\n";# class="page_footer"28722873if(-f $site_footer) {2874open(my$fd,$site_footer);2875print<$fd>;2876close$fd;2877}28782879print"</body>\n".2880"</html>";2881}28822883# die_error(<http_status_code>, <error_message>)2884# Example: die_error(404, 'Hash not found')2885# By convention, use the following status codes (as defined in RFC 2616):2886# 400: Invalid or missing CGI parameters, or2887# requested object exists but has wrong type.2888# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on2889# this server or project.2890# 404: Requested object/revision/project doesn't exist.2891# 500: The server isn't configured properly, or2892# an internal error occurred (e.g. failed assertions caused by bugs), or2893# an unknown error occurred (e.g. the git binary died unexpectedly).2894sub die_error {2895my$status=shift||500;2896my$error=shift||"Internal server error";28972898my%http_responses= (400=>'400 Bad Request',2899403=>'403 Forbidden',2900404=>'404 Not Found',2901500=>'500 Internal Server Error');2902 git_header_html($http_responses{$status});2903print<<EOF;2904<div class="page_body">2905<br /><br />2906$status-$error2907<br />2908</div>2909EOF2910 git_footer_html();2911exit;2912}29132914## ----------------------------------------------------------------------2915## functions printing or outputting HTML: navigation29162917sub git_print_page_nav {2918my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;2919$extra=''if!defined$extra;# pager or formats29202921my@navs=qw(summary shortlog log commit commitdiff tree);2922if($suppress) {2923@navs=grep{$_ne$suppress}@navs;2924}29252926my%arg=map{$_=> {action=>$_} }@navs;2927if(defined$head) {2928for(qw(commit commitdiff)) {2929$arg{$_}{'hash'} =$head;2930}2931if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {2932for(qw(shortlog log)) {2933$arg{$_}{'hash'} =$head;2934}2935}2936}29372938$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;2939$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;29402941my@actions= gitweb_check_feature('actions');2942while(@actions) {2943my($label,$link,$pos) = (shift(@actions),shift(@actions),shift(@actions));2944@navs=map{$_eq$pos? ($_,$label) :$_}@navs;2945# munch munch2946$link=~ s#%n#$project#g;2947$link=~ s#%f#$git_dir#g;2948$treehead?$link=~ s#%h#$treehead#g : $link =~ s#%h##g;2949$treebase?$link=~ s#%b#$treebase#g : $link =~ s#%b##g;2950$arg{$label}{'_href'} =$link;2951}29522953print"<div class=\"page_nav\">\n".2954(join" | ",2955map{$_eq$current?2956$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")2957}@navs);2958print"<br/>\n$extra<br/>\n".2959"</div>\n";2960}29612962sub format_paging_nav {2963my($action,$hash,$head,$page,$has_next_link) =@_;2964my$paging_nav;296529662967if($hashne$head||$page) {2968$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");2969}else{2970$paging_nav.="HEAD";2971}29722973if($page>0) {2974$paging_nav.=" ⋅ ".2975$cgi->a({-href => href(-replay=>1, page=>$page-1),2976-accesskey =>"p", -title =>"Alt-p"},"prev");2977}else{2978$paging_nav.=" ⋅ prev";2979}29802981if($has_next_link) {2982$paging_nav.=" ⋅ ".2983$cgi->a({-href => href(-replay=>1, page=>$page+1),2984-accesskey =>"n", -title =>"Alt-n"},"next");2985}else{2986$paging_nav.=" ⋅ next";2987}29882989return$paging_nav;2990}29912992## ......................................................................2993## functions printing or outputting HTML: div29942995sub git_print_header_div {2996my($action,$title,$hash,$hash_base) =@_;2997my%args= ();29982999$args{'action'} =$action;3000$args{'hash'} =$hashif$hash;3001$args{'hash_base'} =$hash_baseif$hash_base;30023003print"<div class=\"header\">\n".3004$cgi->a({-href => href(%args), -class=>"title"},3005$title?$title:$action) .3006"\n</div>\n";3007}30083009#sub git_print_authorship (\%) {3010sub git_print_authorship {3011my$co=shift;30123013my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3014print"<div class=\"author_date\">".3015 esc_html($co->{'author_name'}) .3016" [$ad{'rfc2822'}";3017if($ad{'hour_local'} <6) {3018printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3019$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3020}else{3021printf(" (%02d:%02d%s)",3022$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3023}3024print"]</div>\n";3025}30263027sub git_print_page_path {3028my$name=shift;3029my$type=shift;3030my$hb=shift;303130323033print"<div class=\"page_path\">";3034print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3035-title =>'tree root'}, to_utf8("[$project]"));3036print" / ";3037if(defined$name) {3038my@dirname=split'/',$name;3039my$basename=pop@dirname;3040my$fullname='';30413042foreachmy$dir(@dirname) {3043$fullname.= ($fullname?'/':'') .$dir;3044print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3045 hash_base=>$hb),3046-title =>$fullname}, esc_path($dir));3047print" / ";3048}3049if(defined$type&&$typeeq'blob') {3050print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3051 hash_base=>$hb),3052-title =>$name}, esc_path($basename));3053}elsif(defined$type&&$typeeq'tree') {3054print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3055 hash_base=>$hb),3056-title =>$name}, esc_path($basename));3057print" / ";3058}else{3059print esc_path($basename);3060}3061}3062print"<br/></div>\n";3063}30643065# sub git_print_log (\@;%) {3066sub git_print_log ($;%) {3067my$log=shift;3068my%opts=@_;30693070if($opts{'-remove_title'}) {3071# remove title, i.e. first line of log3072shift@$log;3073}3074# remove leading empty lines3075while(defined$log->[0] &&$log->[0]eq"") {3076shift@$log;3077}30783079# print log3080my$signoff=0;3081my$empty=0;3082foreachmy$line(@$log) {3083if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3084$signoff=1;3085$empty=0;3086if(!$opts{'-remove_signoff'}) {3087print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3088next;3089}else{3090# remove signoff lines3091next;3092}3093}else{3094$signoff=0;3095}30963097# print only one empty line3098# do not print empty line after signoff3099if($lineeq"") {3100next if($empty||$signoff);3101$empty=1;3102}else{3103$empty=0;3104}31053106print format_log_line_html($line) ."<br/>\n";3107}31083109if($opts{'-final_empty_line'}) {3110# end with single empty line3111print"<br/>\n"unless$empty;3112}3113}31143115# return link target (what link points to)3116sub git_get_link_target {3117my$hash=shift;3118my$link_target;31193120# read link3121open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3122orreturn;3123{3124local$/;3125$link_target= <$fd>;3126}3127close$fd3128orreturn;31293130return$link_target;3131}31323133# given link target, and the directory (basedir) the link is in,3134# return target of link relative to top directory (top tree);3135# return undef if it is not possible (including absolute links).3136sub normalize_link_target {3137my($link_target,$basedir,$hash_base) =@_;31383139# we can normalize symlink target only if $hash_base is provided3140return unless$hash_base;31413142# absolute symlinks (beginning with '/') cannot be normalized3143return if(substr($link_target,0,1)eq'/');31443145# normalize link target to path from top (root) tree (dir)3146my$path;3147if($basedir) {3148$path=$basedir.'/'.$link_target;3149}else{3150# we are in top (root) tree (dir)3151$path=$link_target;3152}31533154# remove //, /./, and /../3155my@path_parts;3156foreachmy$part(split('/',$path)) {3157# discard '.' and ''3158next if(!$part||$parteq'.');3159# handle '..'3160if($parteq'..') {3161if(@path_parts) {3162pop@path_parts;3163}else{3164# link leads outside repository (outside top dir)3165return;3166}3167}else{3168push@path_parts,$part;3169}3170}3171$path=join('/',@path_parts);31723173return$path;3174}31753176# print tree entry (row of git_tree), but without encompassing <tr> element3177sub git_print_tree_entry {3178my($t,$basedir,$hash_base,$have_blame) =@_;31793180my%base_key= ();3181$base_key{'hash_base'} =$hash_baseifdefined$hash_base;31823183# The format of a table row is: mode list link. Where mode is3184# the mode of the entry, list is the name of the entry, an href,3185# and link is the action links of the entry.31863187print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3188if($t->{'type'}eq"blob") {3189print"<td class=\"list\">".3190$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3191 file_name=>"$basedir$t->{'name'}",%base_key),3192-class=>"list"}, esc_path($t->{'name'}));3193if(S_ISLNK(oct$t->{'mode'})) {3194my$link_target= git_get_link_target($t->{'hash'});3195if($link_target) {3196my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3197if(defined$norm_target) {3198print" -> ".3199$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3200 file_name=>$norm_target),3201-title =>$norm_target}, esc_path($link_target));3202}else{3203print" -> ". esc_path($link_target);3204}3205}3206}3207print"</td>\n";3208print"<td class=\"link\">";3209print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3210 file_name=>"$basedir$t->{'name'}",%base_key)},3211"blob");3212if($have_blame) {3213print" | ".3214$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3215 file_name=>"$basedir$t->{'name'}",%base_key)},3216"blame");3217}3218if(defined$hash_base) {3219print" | ".3220$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3221 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3222"history");3223}3224print" | ".3225$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3226 file_name=>"$basedir$t->{'name'}")},3227"raw");3228print"</td>\n";32293230}elsif($t->{'type'}eq"tree") {3231print"<td class=\"list\">";3232print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3233 file_name=>"$basedir$t->{'name'}",%base_key)},3234 esc_path($t->{'name'}));3235print"</td>\n";3236print"<td class=\"link\">";3237print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3238 file_name=>"$basedir$t->{'name'}",%base_key)},3239"tree");3240if(defined$hash_base) {3241print" | ".3242$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3243 file_name=>"$basedir$t->{'name'}")},3244"history");3245}3246print"</td>\n";3247}else{3248# unknown object: we can only present history for it3249# (this includes 'commit' object, i.e. submodule support)3250print"<td class=\"list\">".3251 esc_path($t->{'name'}) .3252"</td>\n";3253print"<td class=\"link\">";3254if(defined$hash_base) {3255print$cgi->a({-href => href(action=>"history",3256 hash_base=>$hash_base,3257 file_name=>"$basedir$t->{'name'}")},3258"history");3259}3260print"</td>\n";3261}3262}32633264## ......................................................................3265## functions printing large fragments of HTML32663267# get pre-image filenames for merge (combined) diff3268sub fill_from_file_info {3269my($diff,@parents) =@_;32703271$diff->{'from_file'} = [ ];3272$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3273for(my$i=0;$i<$diff->{'nparents'};$i++) {3274if($diff->{'status'}[$i]eq'R'||3275$diff->{'status'}[$i]eq'C') {3276$diff->{'from_file'}[$i] =3277 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3278}3279}32803281return$diff;3282}32833284# is current raw difftree line of file deletion3285sub is_deleted {3286my$diffinfo=shift;32873288return$diffinfo->{'to_id'}eq('0' x 40);3289}32903291# does patch correspond to [previous] difftree raw line3292# $diffinfo - hashref of parsed raw diff format3293# $patchinfo - hashref of parsed patch diff format3294# (the same keys as in $diffinfo)3295sub is_patch_split {3296my($diffinfo,$patchinfo) =@_;32973298returndefined$diffinfo&&defined$patchinfo3299&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3300}330133023303sub git_difftree_body {3304my($difftree,$hash,@parents) =@_;3305my($parent) =$parents[0];3306my($have_blame) = gitweb_check_feature('blame');3307print"<div class=\"list_head\">\n";3308if($#{$difftree} >10) {3309print(($#{$difftree} +1) ." files changed:\n");3310}3311print"</div>\n";33123313print"<table class=\"".3314(@parents>1?"combined ":"") .3315"diff_tree\">\n";33163317# header only for combined diff in 'commitdiff' view3318my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3319if($has_header) {3320# table header3321print"<thead><tr>\n".3322"<th></th><th></th>\n";# filename, patchN link3323for(my$i=0;$i<@parents;$i++) {3324my$par=$parents[$i];3325print"<th>".3326$cgi->a({-href => href(action=>"commitdiff",3327 hash=>$hash, hash_parent=>$par),3328-title =>'commitdiff to parent number '.3329($i+1) .': '.substr($par,0,7)},3330$i+1) .3331" </th>\n";3332}3333print"</tr></thead>\n<tbody>\n";3334}33353336my$alternate=1;3337my$patchno=0;3338foreachmy$line(@{$difftree}) {3339my$diff= parsed_difftree_line($line);33403341if($alternate) {3342print"<tr class=\"dark\">\n";3343}else{3344print"<tr class=\"light\">\n";3345}3346$alternate^=1;33473348if(exists$diff->{'nparents'}) {# combined diff33493350 fill_from_file_info($diff,@parents)3351unlessexists$diff->{'from_file'};33523353if(!is_deleted($diff)) {3354# file exists in the result (child) commit3355print"<td>".3356$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3357 file_name=>$diff->{'to_file'},3358 hash_base=>$hash),3359-class=>"list"}, esc_path($diff->{'to_file'})) .3360"</td>\n";3361}else{3362print"<td>".3363 esc_path($diff->{'to_file'}) .3364"</td>\n";3365}33663367if($actioneq'commitdiff') {3368# link to patch3369$patchno++;3370print"<td class=\"link\">".3371$cgi->a({-href =>"#patch$patchno"},"patch") .3372" | ".3373"</td>\n";3374}33753376my$has_history=0;3377my$not_deleted=0;3378for(my$i=0;$i<$diff->{'nparents'};$i++) {3379my$hash_parent=$parents[$i];3380my$from_hash=$diff->{'from_id'}[$i];3381my$from_path=$diff->{'from_file'}[$i];3382my$status=$diff->{'status'}[$i];33833384$has_history||= ($statusne'A');3385$not_deleted||= ($statusne'D');33863387if($statuseq'A') {3388print"<td class=\"link\"align=\"right\"> | </td>\n";3389}elsif($statuseq'D') {3390print"<td class=\"link\">".3391$cgi->a({-href => href(action=>"blob",3392 hash_base=>$hash,3393 hash=>$from_hash,3394 file_name=>$from_path)},3395"blob". ($i+1)) .3396" | </td>\n";3397}else{3398if($diff->{'to_id'}eq$from_hash) {3399print"<td class=\"link nochange\">";3400}else{3401print"<td class=\"link\">";3402}3403print$cgi->a({-href => href(action=>"blobdiff",3404 hash=>$diff->{'to_id'},3405 hash_parent=>$from_hash,3406 hash_base=>$hash,3407 hash_parent_base=>$hash_parent,3408 file_name=>$diff->{'to_file'},3409 file_parent=>$from_path)},3410"diff". ($i+1)) .3411" | </td>\n";3412}3413}34143415print"<td class=\"link\">";3416if($not_deleted) {3417print$cgi->a({-href => href(action=>"blob",3418 hash=>$diff->{'to_id'},3419 file_name=>$diff->{'to_file'},3420 hash_base=>$hash)},3421"blob");3422print" | "if($has_history);3423}3424if($has_history) {3425print$cgi->a({-href => href(action=>"history",3426 file_name=>$diff->{'to_file'},3427 hash_base=>$hash)},3428"history");3429}3430print"</td>\n";34313432print"</tr>\n";3433next;# instead of 'else' clause, to avoid extra indent3434}3435# else ordinary diff34363437my($to_mode_oct,$to_mode_str,$to_file_type);3438my($from_mode_oct,$from_mode_str,$from_file_type);3439if($diff->{'to_mode'}ne('0' x 6)) {3440$to_mode_oct=oct$diff->{'to_mode'};3441if(S_ISREG($to_mode_oct)) {# only for regular file3442$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3443}3444$to_file_type= file_type($diff->{'to_mode'});3445}3446if($diff->{'from_mode'}ne('0' x 6)) {3447$from_mode_oct=oct$diff->{'from_mode'};3448if(S_ISREG($to_mode_oct)) {# only for regular file3449$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3450}3451$from_file_type= file_type($diff->{'from_mode'});3452}34533454if($diff->{'status'}eq"A") {# created3455my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3456$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3457$mode_chng.="]</span>";3458print"<td>";3459print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3460 hash_base=>$hash, file_name=>$diff->{'file'}),3461-class=>"list"}, esc_path($diff->{'file'}));3462print"</td>\n";3463print"<td>$mode_chng</td>\n";3464print"<td class=\"link\">";3465if($actioneq'commitdiff') {3466# link to patch3467$patchno++;3468print$cgi->a({-href =>"#patch$patchno"},"patch");3469print" | ";3470}3471print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3472 hash_base=>$hash, file_name=>$diff->{'file'})},3473"blob");3474print"</td>\n";34753476}elsif($diff->{'status'}eq"D") {# deleted3477my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3478print"<td>";3479print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3480 hash_base=>$parent, file_name=>$diff->{'file'}),3481-class=>"list"}, esc_path($diff->{'file'}));3482print"</td>\n";3483print"<td>$mode_chng</td>\n";3484print"<td class=\"link\">";3485if($actioneq'commitdiff') {3486# link to patch3487$patchno++;3488print$cgi->a({-href =>"#patch$patchno"},"patch");3489print" | ";3490}3491print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3492 hash_base=>$parent, file_name=>$diff->{'file'})},3493"blob") ." | ";3494if($have_blame) {3495print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3496 file_name=>$diff->{'file'})},3497"blame") ." | ";3498}3499print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3500 file_name=>$diff->{'file'})},3501"history");3502print"</td>\n";35033504}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3505my$mode_chnge="";3506if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3507$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3508if($from_file_typene$to_file_type) {3509$mode_chnge.=" from$from_file_typeto$to_file_type";3510}3511if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3512if($from_mode_str&&$to_mode_str) {3513$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3514}elsif($to_mode_str) {3515$mode_chnge.=" mode:$to_mode_str";3516}3517}3518$mode_chnge.="]</span>\n";3519}3520print"<td>";3521print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3522 hash_base=>$hash, file_name=>$diff->{'file'}),3523-class=>"list"}, esc_path($diff->{'file'}));3524print"</td>\n";3525print"<td>$mode_chnge</td>\n";3526print"<td class=\"link\">";3527if($actioneq'commitdiff') {3528# link to patch3529$patchno++;3530print$cgi->a({-href =>"#patch$patchno"},"patch") .3531" | ";3532}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3533# "commit" view and modified file (not onlu mode changed)3534print$cgi->a({-href => href(action=>"blobdiff",3535 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3536 hash_base=>$hash, hash_parent_base=>$parent,3537 file_name=>$diff->{'file'})},3538"diff") .3539" | ";3540}3541print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3542 hash_base=>$hash, file_name=>$diff->{'file'})},3543"blob") ." | ";3544if($have_blame) {3545print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3546 file_name=>$diff->{'file'})},3547"blame") ." | ";3548}3549print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3550 file_name=>$diff->{'file'})},3551"history");3552print"</td>\n";35533554}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3555my%status_name= ('R'=>'moved','C'=>'copied');3556my$nstatus=$status_name{$diff->{'status'}};3557my$mode_chng="";3558if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3559# mode also for directories, so we cannot use $to_mode_str3560$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3561}3562print"<td>".3563$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3564 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3565-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3566"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3567$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3568 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3569-class=>"list"}, esc_path($diff->{'from_file'})) .3570" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3571"<td class=\"link\">";3572if($actioneq'commitdiff') {3573# link to patch3574$patchno++;3575print$cgi->a({-href =>"#patch$patchno"},"patch") .3576" | ";3577}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3578# "commit" view and modified file (not only pure rename or copy)3579print$cgi->a({-href => href(action=>"blobdiff",3580 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3581 hash_base=>$hash, hash_parent_base=>$parent,3582 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3583"diff") .3584" | ";3585}3586print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3587 hash_base=>$parent, file_name=>$diff->{'to_file'})},3588"blob") ." | ";3589if($have_blame) {3590print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3591 file_name=>$diff->{'to_file'})},3592"blame") ." | ";3593}3594print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3595 file_name=>$diff->{'to_file'})},3596"history");3597print"</td>\n";35983599}# we should not encounter Unmerged (U) or Unknown (X) status3600print"</tr>\n";3601}3602print"</tbody>"if$has_header;3603print"</table>\n";3604}36053606sub git_patchset_body {3607my($fd,$difftree,$hash,@hash_parents) =@_;3608my($hash_parent) =$hash_parents[0];36093610my$is_combined= (@hash_parents>1);3611my$patch_idx=0;3612my$patch_number=0;3613my$patch_line;3614my$diffinfo;3615my$to_name;3616my(%from,%to);36173618print"<div class=\"patchset\">\n";36193620# skip to first patch3621while($patch_line= <$fd>) {3622chomp$patch_line;36233624last if($patch_line=~m/^diff /);3625}36263627 PATCH:3628while($patch_line) {36293630# parse "git diff" header line3631if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3632# $1 is from_name, which we do not use3633$to_name= unquote($2);3634$to_name=~s!^b/!!;3635}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3636# $1 is 'cc' or 'combined', which we do not use3637$to_name= unquote($2);3638}else{3639$to_name=undef;3640}36413642# check if current patch belong to current raw line3643# and parse raw git-diff line if needed3644if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3645# this is continuation of a split patch3646print"<div class=\"patch cont\">\n";3647}else{3648# advance raw git-diff output if needed3649$patch_idx++ifdefined$diffinfo;36503651# read and prepare patch information3652$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);36533654# compact combined diff output can have some patches skipped3655# find which patch (using pathname of result) we are at now;3656if($is_combined) {3657while($to_namene$diffinfo->{'to_file'}) {3658print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3659 format_diff_cc_simplified($diffinfo,@hash_parents) .3660"</div>\n";# class="patch"36613662$patch_idx++;3663$patch_number++;36643665last if$patch_idx>$#$difftree;3666$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3667}3668}36693670# modifies %from, %to hashes3671 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);36723673# this is first patch for raw difftree line with $patch_idx index3674# we index @$difftree array from 0, but number patches from 13675print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3676}36773678# git diff header3679#assert($patch_line =~ m/^diff /) if DEBUG;3680#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3681$patch_number++;3682# print "git diff" header3683print format_git_diff_header_line($patch_line,$diffinfo,3684 \%from, \%to);36853686# print extended diff header3687print"<div class=\"diff extended_header\">\n";3688 EXTENDED_HEADER:3689while($patch_line= <$fd>) {3690chomp$patch_line;36913692last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);36933694print format_extended_diff_header_line($patch_line,$diffinfo,3695 \%from, \%to);3696}3697print"</div>\n";# class="diff extended_header"36983699# from-file/to-file diff header3700if(!$patch_line) {3701print"</div>\n";# class="patch"3702last PATCH;3703}3704next PATCH if($patch_line=~m/^diff /);3705#assert($patch_line =~ m/^---/) if DEBUG;37063707my$last_patch_line=$patch_line;3708$patch_line= <$fd>;3709chomp$patch_line;3710#assert($patch_line =~ m/^\+\+\+/) if DEBUG;37113712print format_diff_from_to_header($last_patch_line,$patch_line,3713$diffinfo, \%from, \%to,3714@hash_parents);37153716# the patch itself3717 LINE:3718while($patch_line= <$fd>) {3719chomp$patch_line;37203721next PATCH if($patch_line=~m/^diff /);37223723print format_diff_line($patch_line, \%from, \%to);3724}37253726}continue{3727print"</div>\n";# class="patch"3728}37293730# for compact combined (--cc) format, with chunk and patch simpliciaction3731# patchset might be empty, but there might be unprocessed raw lines3732for(++$patch_idxif$patch_number>0;3733$patch_idx<@$difftree;3734++$patch_idx) {3735# read and prepare patch information3736$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);37373738# generate anchor for "patch" links in difftree / whatchanged part3739print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3740 format_diff_cc_simplified($diffinfo,@hash_parents) .3741"</div>\n";# class="patch"37423743$patch_number++;3744}37453746if($patch_number==0) {3747if(@hash_parents>1) {3748print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3749}else{3750print"<div class=\"diff nodifferences\">No differences found</div>\n";3751}3752}37533754print"</div>\n";# class="patchset"3755}37563757# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37583759# fills project list info (age, description, owner, forks) for each3760# project in the list, removing invalid projects from returned list3761# NOTE: modifies $projlist, but does not remove entries from it3762sub fill_project_list_info {3763my($projlist,$check_forks) =@_;3764my@projects;37653766my$show_ctags= gitweb_check_feature('ctags');3767 PROJECT:3768foreachmy$pr(@$projlist) {3769my(@activity) = git_get_last_activity($pr->{'path'});3770unless(@activity) {3771next PROJECT;3772}3773($pr->{'age'},$pr->{'age_string'}) =@activity;3774if(!defined$pr->{'descr'}) {3775my$descr= git_get_project_description($pr->{'path'}) ||"";3776$descr= to_utf8($descr);3777$pr->{'descr_long'} =$descr;3778$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3779}3780if(!defined$pr->{'owner'}) {3781$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3782}3783if($check_forks) {3784my$pname=$pr->{'path'};3785if(($pname=~s/\.git$//) &&3786($pname!~/\/$/) &&3787(-d "$projectroot/$pname")) {3788$pr->{'forks'} ="-d$projectroot/$pname";3789}else{3790$pr->{'forks'} =0;3791}3792}3793$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3794push@projects,$pr;3795}37963797return@projects;3798}37993800# print 'sort by' <th> element, generating 'sort by $name' replay link3801# if that order is not selected3802sub print_sort_th {3803my($name,$order,$header) =@_;3804$header||=ucfirst($name);38053806if($ordereq$name) {3807print"<th>$header</th>\n";3808}else{3809print"<th>".3810$cgi->a({-href => href(-replay=>1, order=>$name),3811-class=>"header"},$header) .3812"</th>\n";3813}3814}38153816sub git_project_list_body {3817# actually uses global variable $project3818my($projlist,$order,$from,$to,$extra,$no_header) =@_;38193820my($check_forks) = gitweb_check_feature('forks');3821my@projects= fill_project_list_info($projlist,$check_forks);38223823$order||=$default_projects_order;3824$from=0unlessdefined$from;3825$to=$#projectsif(!defined$to||$#projects<$to);38263827my%order_info= (3828 project => { key =>'path', type =>'str'},3829 descr => { key =>'descr_long', type =>'str'},3830 owner => { key =>'owner', type =>'str'},3831 age => { key =>'age', type =>'num'}3832);3833my$oi=$order_info{$order};3834if($oi->{'type'}eq'str') {3835@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;3836}else{3837@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;3838}38393840my$show_ctags= gitweb_check_feature('ctags');3841if($show_ctags) {3842my%ctags;3843foreachmy$p(@projects) {3844foreachmy$ct(keys%{$p->{'ctags'}}) {3845$ctags{$ct} +=$p->{'ctags'}->{$ct};3846}3847}3848my$cloud= git_populate_project_tagcloud(\%ctags);3849print git_show_project_tagcloud($cloud,64);3850}38513852print"<table class=\"project_list\">\n";3853unless($no_header) {3854print"<tr>\n";3855if($check_forks) {3856print"<th></th>\n";3857}3858 print_sort_th('project',$order,'Project');3859 print_sort_th('descr',$order,'Description');3860 print_sort_th('owner',$order,'Owner');3861 print_sort_th('age',$order,'Last Change');3862print"<th></th>\n".# for links3863"</tr>\n";3864}3865my$alternate=1;3866my$tagfilter=$cgi->param('by_tag');3867for(my$i=$from;$i<=$to;$i++) {3868my$pr=$projects[$i];38693870next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};3871next if$searchtextand not$pr->{'path'} =~/$searchtext/3872and not$pr->{'descr_long'} =~/$searchtext/;3873# Weed out forks or non-matching entries of search3874if($check_forks) {3875my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;3876$forkbase="^$forkbase"if$forkbase;3877next ifnot$searchtextand not$tagfilterand$show_ctags3878and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe3879}38803881if($alternate) {3882print"<tr class=\"dark\">\n";3883}else{3884print"<tr class=\"light\">\n";3885}3886$alternate^=1;3887if($check_forks) {3888print"<td>";3889if($pr->{'forks'}) {3890print"<!--$pr->{'forks'} -->\n";3891print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");3892}3893print"</td>\n";3894}3895print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3896-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".3897"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3898-class=>"list", -title =>$pr->{'descr_long'}},3899 esc_html($pr->{'descr'})) ."</td>\n".3900"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";3901print"<td class=\"". age_class($pr->{'age'}) ."\">".3902(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".3903"<td class=\"link\">".3904$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".3905$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".3906$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".3907$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .3908($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .3909"</td>\n".3910"</tr>\n";3911}3912if(defined$extra) {3913print"<tr>\n";3914if($check_forks) {3915print"<td></td>\n";3916}3917print"<td colspan=\"5\">$extra</td>\n".3918"</tr>\n";3919}3920print"</table>\n";3921}39223923sub git_shortlog_body {3924# uses global variable $project3925my($commitlist,$from,$to,$refs,$extra) =@_;39263927$from=0unlessdefined$from;3928$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);39293930print"<table class=\"shortlog\">\n";3931my$alternate=1;3932for(my$i=$from;$i<=$to;$i++) {3933my%co= %{$commitlist->[$i]};3934my$commit=$co{'id'};3935my$ref= format_ref_marker($refs,$commit);3936if($alternate) {3937print"<tr class=\"dark\">\n";3938}else{3939print"<tr class=\"light\">\n";3940}3941$alternate^=1;3942my$author= chop_and_escape_str($co{'author_name'},10);3943# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .3944print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".3945"<td><i>".$author."</i></td>\n".3946"<td>";3947print format_subject_html($co{'title'},$co{'title_short'},3948 href(action=>"commit", hash=>$commit),$ref);3949print"</td>\n".3950"<td class=\"link\">".3951$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".3952$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".3953$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");3954my$snapshot_links= format_snapshot_links($commit);3955if(defined$snapshot_links) {3956print" | ".$snapshot_links;3957}3958print"</td>\n".3959"</tr>\n";3960}3961if(defined$extra) {3962print"<tr>\n".3963"<td colspan=\"4\">$extra</td>\n".3964"</tr>\n";3965}3966print"</table>\n";3967}39683969sub git_history_body {3970# Warning: assumes constant type (blob or tree) during history3971my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;39723973$from=0unlessdefined$from;3974$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});39753976print"<table class=\"history\">\n";3977my$alternate=1;3978for(my$i=$from;$i<=$to;$i++) {3979my%co= %{$commitlist->[$i]};3980if(!%co) {3981next;3982}3983my$commit=$co{'id'};39843985my$ref= format_ref_marker($refs,$commit);39863987if($alternate) {3988print"<tr class=\"dark\">\n";3989}else{3990print"<tr class=\"light\">\n";3991}3992$alternate^=1;3993# shortlog uses chop_str($co{'author_name'}, 10)3994my$author= chop_and_escape_str($co{'author_name'},15,3);3995print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".3996"<td><i>".$author."</i></td>\n".3997"<td>";3998# originally git_history used chop_str($co{'title'}, 50)3999print format_subject_html($co{'title'},$co{'title_short'},4000 href(action=>"commit", hash=>$commit),$ref);4001print"</td>\n".4002"<td class=\"link\">".4003$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4004$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");40054006if($ftypeeq'blob') {4007my$blob_current= git_get_hash_by_path($hash_base,$file_name);4008my$blob_parent= git_get_hash_by_path($commit,$file_name);4009if(defined$blob_current&&defined$blob_parent&&4010$blob_currentne$blob_parent) {4011print" | ".4012$cgi->a({-href => href(action=>"blobdiff",4013 hash=>$blob_current, hash_parent=>$blob_parent,4014 hash_base=>$hash_base, hash_parent_base=>$commit,4015 file_name=>$file_name)},4016"diff to current");4017}4018}4019print"</td>\n".4020"</tr>\n";4021}4022if(defined$extra) {4023print"<tr>\n".4024"<td colspan=\"4\">$extra</td>\n".4025"</tr>\n";4026}4027print"</table>\n";4028}40294030sub git_tags_body {4031# uses global variable $project4032my($taglist,$from,$to,$extra) =@_;4033$from=0unlessdefined$from;4034$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);40354036print"<table class=\"tags\">\n";4037my$alternate=1;4038for(my$i=$from;$i<=$to;$i++) {4039my$entry=$taglist->[$i];4040my%tag=%$entry;4041my$comment=$tag{'subject'};4042my$comment_short;4043if(defined$comment) {4044$comment_short= chop_str($comment,30,5);4045}4046if($alternate) {4047print"<tr class=\"dark\">\n";4048}else{4049print"<tr class=\"light\">\n";4050}4051$alternate^=1;4052if(defined$tag{'age'}) {4053print"<td><i>$tag{'age'}</i></td>\n";4054}else{4055print"<td></td>\n";4056}4057print"<td>".4058$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4059-class=>"list name"}, esc_html($tag{'name'})) .4060"</td>\n".4061"<td>";4062if(defined$comment) {4063print format_subject_html($comment,$comment_short,4064 href(action=>"tag", hash=>$tag{'id'}));4065}4066print"</td>\n".4067"<td class=\"selflink\">";4068if($tag{'type'}eq"tag") {4069print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4070}else{4071print" ";4072}4073print"</td>\n".4074"<td class=\"link\">"." | ".4075$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4076if($tag{'reftype'}eq"commit") {4077print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4078" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4079}elsif($tag{'reftype'}eq"blob") {4080print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4081}4082print"</td>\n".4083"</tr>";4084}4085if(defined$extra) {4086print"<tr>\n".4087"<td colspan=\"5\">$extra</td>\n".4088"</tr>\n";4089}4090print"</table>\n";4091}40924093sub git_heads_body {4094# uses global variable $project4095my($headlist,$head,$from,$to,$extra) =@_;4096$from=0unlessdefined$from;4097$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);40984099print"<table class=\"heads\">\n";4100my$alternate=1;4101for(my$i=$from;$i<=$to;$i++) {4102my$entry=$headlist->[$i];4103my%ref=%$entry;4104my$curr=$ref{'id'}eq$head;4105if($alternate) {4106print"<tr class=\"dark\">\n";4107}else{4108print"<tr class=\"light\">\n";4109}4110$alternate^=1;4111print"<td><i>$ref{'age'}</i></td>\n".4112($curr?"<td class=\"current_head\">":"<td>") .4113$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4114-class=>"list name"},esc_html($ref{'name'})) .4115"</td>\n".4116"<td class=\"link\">".4117$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4118$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4119$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4120"</td>\n".4121"</tr>";4122}4123if(defined$extra) {4124print"<tr>\n".4125"<td colspan=\"3\">$extra</td>\n".4126"</tr>\n";4127}4128print"</table>\n";4129}41304131sub git_search_grep_body {4132my($commitlist,$from,$to,$extra) =@_;4133$from=0unlessdefined$from;4134$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41354136print"<table class=\"commit_search\">\n";4137my$alternate=1;4138for(my$i=$from;$i<=$to;$i++) {4139my%co= %{$commitlist->[$i]};4140if(!%co) {4141next;4142}4143my$commit=$co{'id'};4144if($alternate) {4145print"<tr class=\"dark\">\n";4146}else{4147print"<tr class=\"light\">\n";4148}4149$alternate^=1;4150my$author= chop_and_escape_str($co{'author_name'},15,5);4151print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4152"<td><i>".$author."</i></td>\n".4153"<td>".4154$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4155-class=>"list subject"},4156 chop_and_escape_str($co{'title'},50) ."<br/>");4157my$comment=$co{'comment'};4158foreachmy$line(@$comment) {4159if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4160my($lead,$match,$trail) = ($1,$2,$3);4161$match= chop_str($match,70,5,'center');4162my$contextlen=int((80-length($match))/2);4163$contextlen=30if($contextlen>30);4164$lead= chop_str($lead,$contextlen,10,'left');4165$trail= chop_str($trail,$contextlen,10,'right');41664167$lead= esc_html($lead);4168$match= esc_html($match);4169$trail= esc_html($trail);41704171print"$lead<span class=\"match\">$match</span>$trail<br />";4172}4173}4174print"</td>\n".4175"<td class=\"link\">".4176$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4177" | ".4178$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4179" | ".4180$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4181print"</td>\n".4182"</tr>\n";4183}4184if(defined$extra) {4185print"<tr>\n".4186"<td colspan=\"3\">$extra</td>\n".4187"</tr>\n";4188}4189print"</table>\n";4190}41914192## ======================================================================4193## ======================================================================4194## actions41954196sub git_project_list {4197my$order=$input_params{'order'};4198if(defined$order&&$order!~m/none|project|descr|owner|age/) {4199 die_error(400,"Unknown order parameter");4200}42014202my@list= git_get_projects_list();4203if(!@list) {4204 die_error(404,"No projects found");4205}42064207 git_header_html();4208if(-f $home_text) {4209print"<div class=\"index_include\">\n";4210open(my$fd,$home_text);4211print<$fd>;4212close$fd;4213print"</div>\n";4214}4215print$cgi->startform(-method=>"get") .4216"<p class=\"projsearch\">Search:\n".4217$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4218"</p>".4219$cgi->end_form() ."\n";4220 git_project_list_body(\@list,$order);4221 git_footer_html();4222}42234224sub git_forks {4225my$order=$input_params{'order'};4226if(defined$order&&$order!~m/none|project|descr|owner|age/) {4227 die_error(400,"Unknown order parameter");4228}42294230my@list= git_get_projects_list($project);4231if(!@list) {4232 die_error(404,"No forks found");4233}42344235 git_header_html();4236 git_print_page_nav('','');4237 git_print_header_div('summary',"$projectforks");4238 git_project_list_body(\@list,$order);4239 git_footer_html();4240}42414242sub git_project_index {4243my@projects= git_get_projects_list($project);42444245print$cgi->header(4246-type =>'text/plain',4247-charset =>'utf-8',4248-content_disposition =>'inline; filename="index.aux"');42494250foreachmy$pr(@projects) {4251if(!exists$pr->{'owner'}) {4252$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4253}42544255my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4256# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4257$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4258$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4259$path=~s/ /\+/g;4260$owner=~s/ /\+/g;42614262print"$path$owner\n";4263}4264}42654266sub git_summary {4267my$descr= git_get_project_description($project) ||"none";4268my%co= parse_commit("HEAD");4269my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4270my$head=$co{'id'};42714272my$owner= git_get_project_owner($project);42734274my$refs= git_get_references();4275# These get_*_list functions return one more to allow us to see if4276# there are more ...4277my@taglist= git_get_tags_list(16);4278my@headlist= git_get_heads_list(16);4279my@forklist;4280my($check_forks) = gitweb_check_feature('forks');42814282if($check_forks) {4283@forklist= git_get_projects_list($project);4284}42854286 git_header_html();4287 git_print_page_nav('summary','',$head);42884289print"<div class=\"title\"> </div>\n";4290print"<table class=\"projects_list\">\n".4291"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4292"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4293if(defined$cd{'rfc2822'}) {4294print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4295}42964297# use per project git URL list in $projectroot/$project/cloneurl4298# or make project git URL from git base URL and project name4299my$url_tag="URL";4300my@url_list= git_get_project_url_list($project);4301@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4302foreachmy$git_url(@url_list) {4303next unless$git_url;4304print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4305$url_tag="";4306}43074308# Tag cloud4309my$show_ctags= (gitweb_check_feature('ctags'))[0];4310if($show_ctags) {4311my$ctags= git_get_project_ctags($project);4312my$cloud= git_populate_project_tagcloud($ctags);4313print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4314print"</td>\n<td>"unless%$ctags;4315print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4316print"</td>\n<td>"if%$ctags;4317print git_show_project_tagcloud($cloud,48);4318print"</td></tr>";4319}43204321print"</table>\n";43224323if(-s "$projectroot/$project/README.html") {4324if(open my$fd,"$projectroot/$project/README.html") {4325print"<div class=\"title\">readme</div>\n".4326"<div class=\"readme\">\n";4327print$_while(<$fd>);4328print"\n</div>\n";# class="readme"4329close$fd;4330}4331}43324333# we need to request one more than 16 (0..15) to check if4334# those 16 are all4335my@commitlist=$head? parse_commits($head,17) : ();4336if(@commitlist) {4337 git_print_header_div('shortlog');4338 git_shortlog_body(\@commitlist,0,15,$refs,4339$#commitlist<=15?undef:4340$cgi->a({-href => href(action=>"shortlog")},"..."));4341}43424343if(@taglist) {4344 git_print_header_div('tags');4345 git_tags_body(\@taglist,0,15,4346$#taglist<=15?undef:4347$cgi->a({-href => href(action=>"tags")},"..."));4348}43494350if(@headlist) {4351 git_print_header_div('heads');4352 git_heads_body(\@headlist,$head,0,15,4353$#headlist<=15?undef:4354$cgi->a({-href => href(action=>"heads")},"..."));4355}43564357if(@forklist) {4358 git_print_header_div('forks');4359 git_project_list_body(\@forklist,'age',0,15,4360$#forklist<=15?undef:4361$cgi->a({-href => href(action=>"forks")},"..."),4362'no_header');4363}43644365 git_footer_html();4366}43674368sub git_tag {4369my$head= git_get_head_hash($project);4370 git_header_html();4371 git_print_page_nav('','',$head,undef,$head);4372my%tag= parse_tag($hash);43734374if(!%tag) {4375 die_error(404,"Unknown tag object");4376}43774378 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4379print"<div class=\"title_text\">\n".4380"<table class=\"object_header\">\n".4381"<tr>\n".4382"<td>object</td>\n".4383"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4384$tag{'object'}) ."</td>\n".4385"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4386$tag{'type'}) ."</td>\n".4387"</tr>\n";4388if(defined($tag{'author'})) {4389my%ad= parse_date($tag{'epoch'},$tag{'tz'});4390print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4391print"<tr><td></td><td>".$ad{'rfc2822'} .4392sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4393"</td></tr>\n";4394}4395print"</table>\n\n".4396"</div>\n";4397print"<div class=\"page_body\">";4398my$comment=$tag{'comment'};4399foreachmy$line(@$comment) {4400chomp$line;4401print esc_html($line, -nbsp=>1) ."<br/>\n";4402}4403print"</div>\n";4404 git_footer_html();4405}44064407sub git_blame {4408my$fd;4409my$ftype;44104411 gitweb_check_feature('blame')4412or die_error(403,"Blame view not allowed");44134414 die_error(400,"No file name given")unless$file_name;4415$hash_base||= git_get_head_hash($project);4416 die_error(404,"Couldn't find base commit")unless($hash_base);4417my%co= parse_commit($hash_base)4418or die_error(404,"Commit not found");4419if(!defined$hash) {4420$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4421or die_error(404,"Error looking up file");4422}4423$ftype= git_get_type($hash);4424if($ftype!~"blob") {4425 die_error(400,"Object is not a blob");4426}4427open($fd,"-|", git_cmd(),"blame",'-p','--',4428$file_name,$hash_base)4429or die_error(500,"Open git-blame failed");4430 git_header_html();4431my$formats_nav=4432$cgi->a({-href => href(action=>"blob", -replay=>1)},4433"blob") .4434" | ".4435$cgi->a({-href => href(action=>"history", -replay=>1)},4436"history") .4437" | ".4438$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4439"HEAD");4440 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4441 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4442 git_print_page_path($file_name,$ftype,$hash_base);4443my@rev_color= (qw(light2 dark2));4444my$num_colors=scalar(@rev_color);4445my$current_color=0;4446my$last_rev;4447print<<HTML;4448<div class="page_body">4449<table class="blame">4450<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4451HTML4452my%metainfo= ();4453while(1) {4454$_= <$fd>;4455last unlessdefined$_;4456my($full_rev,$orig_lineno,$lineno,$group_size) =4457/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;4458if(!exists$metainfo{$full_rev}) {4459$metainfo{$full_rev} = {};4460}4461my$meta=$metainfo{$full_rev};4462while(<$fd>) {4463last if(s/^\t//);4464if(/^(\S+) (.*)$/) {4465$meta->{$1} =$2;4466}4467}4468my$data=$_;4469chomp$data;4470my$rev=substr($full_rev,0,8);4471my$author=$meta->{'author'};4472my%date= parse_date($meta->{'author-time'},4473$meta->{'author-tz'});4474my$date=$date{'iso-tz'};4475if($group_size) {4476$current_color= ++$current_color%$num_colors;4477}4478print"<tr class=\"$rev_color[$current_color]\">\n";4479if($group_size) {4480print"<td class=\"sha1\"";4481print" title=\"". esc_html($author) .",$date\"";4482print" rowspan=\"$group_size\""if($group_size>1);4483print">";4484print$cgi->a({-href => href(action=>"commit",4485 hash=>$full_rev,4486 file_name=>$file_name)},4487 esc_html($rev));4488print"</td>\n";4489}4490open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4491or die_error(500,"Open git-rev-parse failed");4492my$parent_commit= <$dd>;4493close$dd;4494chomp($parent_commit);4495my$blamed= href(action =>'blame',4496 file_name =>$meta->{'filename'},4497 hash_base =>$parent_commit);4498print"<td class=\"linenr\">";4499print$cgi->a({ -href =>"$blamed#l$orig_lineno",4500-id =>"l$lineno",4501-class=>"linenr"},4502 esc_html($lineno));4503print"</td>";4504print"<td class=\"pre\">". esc_html($data) ."</td>\n";4505print"</tr>\n";4506}4507print"</table>\n";4508print"</div>";4509close$fd4510or print"Reading blob failed\n";4511 git_footer_html();4512}45134514sub git_tags {4515my$head= git_get_head_hash($project);4516 git_header_html();4517 git_print_page_nav('','',$head,undef,$head);4518 git_print_header_div('summary',$project);45194520my@tagslist= git_get_tags_list();4521if(@tagslist) {4522 git_tags_body(\@tagslist);4523}4524 git_footer_html();4525}45264527sub git_heads {4528my$head= git_get_head_hash($project);4529 git_header_html();4530 git_print_page_nav('','',$head,undef,$head);4531 git_print_header_div('summary',$project);45324533my@headslist= git_get_heads_list();4534if(@headslist) {4535 git_heads_body(\@headslist,$head);4536}4537 git_footer_html();4538}45394540sub git_blob_plain {4541my$type=shift;4542my$expires;45434544if(!defined$hash) {4545if(defined$file_name) {4546my$base=$hash_base|| git_get_head_hash($project);4547$hash= git_get_hash_by_path($base,$file_name,"blob")4548or die_error(404,"Cannot find file");4549}else{4550 die_error(400,"No file name defined");4551}4552}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4553# blobs defined by non-textual hash id's can be cached4554$expires="+1d";4555}45564557open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4558or die_error(500,"Open git-cat-file blob '$hash' failed");45594560# content-type (can include charset)4561$type= blob_contenttype($fd,$file_name,$type);45624563# "save as" filename, even when no $file_name is given4564my$save_as="$hash";4565if(defined$file_name) {4566$save_as=$file_name;4567}elsif($type=~m/^text\//) {4568$save_as.='.txt';4569}45704571print$cgi->header(4572-type =>$type,4573-expires =>$expires,4574-content_disposition =>'inline; filename="'.$save_as.'"');4575undef$/;4576binmode STDOUT,':raw';4577print<$fd>;4578binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4579$/="\n";4580close$fd;4581}45824583sub git_blob {4584my$expires;45854586if(!defined$hash) {4587if(defined$file_name) {4588my$base=$hash_base|| git_get_head_hash($project);4589$hash= git_get_hash_by_path($base,$file_name,"blob")4590or die_error(404,"Cannot find file");4591}else{4592 die_error(400,"No file name defined");4593}4594}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4595# blobs defined by non-textual hash id's can be cached4596$expires="+1d";4597}45984599my($have_blame) = gitweb_check_feature('blame');4600open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4601or die_error(500,"Couldn't cat$file_name,$hash");4602my$mimetype= blob_mimetype($fd,$file_name);4603if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4604close$fd;4605return git_blob_plain($mimetype);4606}4607# we can have blame only for text/* mimetype4608$have_blame&&= ($mimetype=~m!^text/!);46094610 git_header_html(undef,$expires);4611my$formats_nav='';4612if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4613if(defined$file_name) {4614if($have_blame) {4615$formats_nav.=4616$cgi->a({-href => href(action=>"blame", -replay=>1)},4617"blame") .4618" | ";4619}4620$formats_nav.=4621$cgi->a({-href => href(action=>"history", -replay=>1)},4622"history") .4623" | ".4624$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4625"raw") .4626" | ".4627$cgi->a({-href => href(action=>"blob",4628 hash_base=>"HEAD", file_name=>$file_name)},4629"HEAD");4630}else{4631$formats_nav.=4632$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4633"raw");4634}4635 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4636 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4637}else{4638print"<div class=\"page_nav\">\n".4639"<br/><br/></div>\n".4640"<div class=\"title\">$hash</div>\n";4641}4642 git_print_page_path($file_name,"blob",$hash_base);4643print"<div class=\"page_body\">\n";4644if($mimetype=~m!^image/!) {4645print qq!<img type="$mimetype"!;4646if($file_name) {4647print qq! alt="$file_name" title="$file_name"!;4648}4649print qq! src="! .4650 href(action=>"blob_plain", hash=>$hash,4651 hash_base=>$hash_base, file_name=>$file_name) .4652 qq!"/>\n!;4653}else{4654my$nr;4655while(my$line= <$fd>) {4656chomp$line;4657$nr++;4658$line= untabify($line);4659printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4660$nr,$nr,$nr, esc_html($line, -nbsp=>1);4661}4662}4663close$fd4664or print"Reading blob failed.\n";4665print"</div>";4666 git_footer_html();4667}46684669sub git_tree {4670if(!defined$hash_base) {4671$hash_base="HEAD";4672}4673if(!defined$hash) {4674if(defined$file_name) {4675$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4676}else{4677$hash=$hash_base;4678}4679}4680 die_error(404,"No such tree")unlessdefined($hash);4681$/="\0";4682open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4683or die_error(500,"Open git-ls-tree failed");4684my@entries=map{chomp;$_} <$fd>;4685close$fdor die_error(404,"Reading tree failed");4686$/="\n";46874688my$refs= git_get_references();4689my$ref= format_ref_marker($refs,$hash_base);4690 git_header_html();4691my$basedir='';4692my($have_blame) = gitweb_check_feature('blame');4693if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4694my@views_nav= ();4695if(defined$file_name) {4696push@views_nav,4697$cgi->a({-href => href(action=>"history", -replay=>1)},4698"history"),4699$cgi->a({-href => href(action=>"tree",4700 hash_base=>"HEAD", file_name=>$file_name)},4701"HEAD"),4702}4703my$snapshot_links= format_snapshot_links($hash);4704if(defined$snapshot_links) {4705# FIXME: Should be available when we have no hash base as well.4706push@views_nav,$snapshot_links;4707}4708 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4709 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4710}else{4711undef$hash_base;4712print"<div class=\"page_nav\">\n";4713print"<br/><br/></div>\n";4714print"<div class=\"title\">$hash</div>\n";4715}4716if(defined$file_name) {4717$basedir=$file_name;4718if($basedirne''&&substr($basedir, -1)ne'/') {4719$basedir.='/';4720}4721 git_print_page_path($file_name,'tree',$hash_base);4722}4723print"<div class=\"page_body\">\n";4724print"<table class=\"tree\">\n";4725my$alternate=1;4726# '..' (top directory) link if possible4727if(defined$hash_base&&4728defined$file_name&&$file_name=~m![^/]+$!) {4729if($alternate) {4730print"<tr class=\"dark\">\n";4731}else{4732print"<tr class=\"light\">\n";4733}4734$alternate^=1;47354736my$up=$file_name;4737$up=~s!/?[^/]+$!!;4738undef$upunless$up;4739# based on git_print_tree_entry4740print'<td class="mode">'. mode_str('040000') ."</td>\n";4741print'<td class="list">';4742print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4743 file_name=>$up)},4744"..");4745print"</td>\n";4746print"<td class=\"link\"></td>\n";47474748print"</tr>\n";4749}4750foreachmy$line(@entries) {4751my%t= parse_ls_tree_line($line, -z =>1);47524753if($alternate) {4754print"<tr class=\"dark\">\n";4755}else{4756print"<tr class=\"light\">\n";4757}4758$alternate^=1;47594760 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);47614762print"</tr>\n";4763}4764print"</table>\n".4765"</div>";4766 git_footer_html();4767}47684769sub git_snapshot {4770my@supported_fmts= gitweb_check_feature('snapshot');4771@supported_fmts= filter_snapshot_fmts(@supported_fmts);47724773my$format=$input_params{'snapshot_format'};4774if(!@supported_fmts) {4775 die_error(403,"Snapshots not allowed");4776}4777# default to first supported snapshot format4778$format||=$supported_fmts[0];4779if($format!~m/^[a-z0-9]+$/) {4780 die_error(400,"Invalid snapshot format parameter");4781}elsif(!exists($known_snapshot_formats{$format})) {4782 die_error(400,"Unknown snapshot format");4783}elsif(!grep($_eq$format,@supported_fmts)) {4784 die_error(403,"Unsupported snapshot format");4785}47864787if(!defined$hash) {4788$hash= git_get_head_hash($project);4789}47904791my$name=$project;4792$name=~ s,([^/])/*\.git$,$1,;4793$name= basename($name);4794my$filename= to_utf8($name);4795$name=~s/\047/\047\\\047\047/g;4796my$cmd;4797$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";4798$cmd= quote_command(4799 git_cmd(),'archive',4800"--format=$known_snapshot_formats{$format}{'format'}",4801"--prefix=$name/",$hash);4802if(exists$known_snapshot_formats{$format}{'compressor'}) {4803$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});4804}48054806print$cgi->header(4807-type =>$known_snapshot_formats{$format}{'type'},4808-content_disposition =>'inline; filename="'."$filename".'"',4809-status =>'200 OK');48104811open my$fd,"-|",$cmd4812or die_error(500,"Execute git-archive failed");4813binmode STDOUT,':raw';4814print<$fd>;4815binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4816close$fd;4817}48184819sub git_log {4820my$head= git_get_head_hash($project);4821if(!defined$hash) {4822$hash=$head;4823}4824if(!defined$page) {4825$page=0;4826}4827my$refs= git_get_references();48284829my@commitlist= parse_commits($hash,101, (100*$page));48304831my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);48324833 git_header_html();4834 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);48354836if(!@commitlist) {4837my%co= parse_commit($hash);48384839 git_print_header_div('summary',$project);4840print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";4841}4842my$to= ($#commitlist>=99) ? (99) : ($#commitlist);4843for(my$i=0;$i<=$to;$i++) {4844my%co= %{$commitlist[$i]};4845next if!%co;4846my$commit=$co{'id'};4847my$ref= format_ref_marker($refs,$commit);4848my%ad= parse_date($co{'author_epoch'});4849 git_print_header_div('commit',4850"<span class=\"age\">$co{'age_string'}</span>".4851 esc_html($co{'title'}) .$ref,4852$commit);4853print"<div class=\"title_text\">\n".4854"<div class=\"log_link\">\n".4855$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4856" | ".4857$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4858" | ".4859$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4860"<br/>\n".4861"</div>\n".4862"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".4863"</div>\n";48644865print"<div class=\"log_body\">\n";4866 git_print_log($co{'comment'}, -final_empty_line=>1);4867print"</div>\n";4868}4869if($#commitlist>=100) {4870print"<div class=\"page_nav\">\n";4871print$cgi->a({-href => href(-replay=>1, page=>$page+1),4872-accesskey =>"n", -title =>"Alt-n"},"next");4873print"</div>\n";4874}4875 git_footer_html();4876}48774878sub git_commit {4879$hash||=$hash_base||"HEAD";4880my%co= parse_commit($hash)4881or die_error(404,"Unknown commit object");4882my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});4883my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});48844885my$parent=$co{'parent'};4886my$parents=$co{'parents'};# listref48874888# we need to prepare $formats_nav before any parameter munging4889my$formats_nav;4890if(!defined$parent) {4891# --root commitdiff4892$formats_nav.='(initial)';4893}elsif(@$parents==1) {4894# single parent commit4895$formats_nav.=4896'(parent: '.4897$cgi->a({-href => href(action=>"commit",4898 hash=>$parent)},4899 esc_html(substr($parent,0,7))) .4900')';4901}else{4902# merge commit4903$formats_nav.=4904'(merge: '.4905join(' ',map{4906$cgi->a({-href => href(action=>"commit",4907 hash=>$_)},4908 esc_html(substr($_,0,7)));4909}@$parents) .4910')';4911}49124913if(!defined$parent) {4914$parent="--root";4915}4916my@difftree;4917open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",4918@diff_opts,4919(@$parents<=1?$parent:'-c'),4920$hash,"--"4921or die_error(500,"Open git-diff-tree failed");4922@difftree=map{chomp;$_} <$fd>;4923close$fdor die_error(404,"Reading git-diff-tree failed");49244925# non-textual hash id's can be cached4926my$expires;4927if($hash=~m/^[0-9a-fA-F]{40}$/) {4928$expires="+1d";4929}4930my$refs= git_get_references();4931my$ref= format_ref_marker($refs,$co{'id'});49324933 git_header_html(undef,$expires);4934 git_print_page_nav('commit','',4935$hash,$co{'tree'},$hash,4936$formats_nav);49374938if(defined$co{'parent'}) {4939 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);4940}else{4941 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);4942}4943print"<div class=\"title_text\">\n".4944"<table class=\"object_header\">\n";4945print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".4946"<tr>".4947"<td></td><td>$ad{'rfc2822'}";4948if($ad{'hour_local'} <6) {4949printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",4950$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});4951}else{4952printf(" (%02d:%02d%s)",4953$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});4954}4955print"</td>".4956"</tr>\n";4957print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";4958print"<tr><td></td><td>$cd{'rfc2822'}".4959sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .4960"</td></tr>\n";4961print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";4962print"<tr>".4963"<td>tree</td>".4964"<td class=\"sha1\">".4965$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),4966class=>"list"},$co{'tree'}) .4967"</td>".4968"<td class=\"link\">".4969$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},4970"tree");4971my$snapshot_links= format_snapshot_links($hash);4972if(defined$snapshot_links) {4973print" | ".$snapshot_links;4974}4975print"</td>".4976"</tr>\n";49774978foreachmy$par(@$parents) {4979print"<tr>".4980"<td>parent</td>".4981"<td class=\"sha1\">".4982$cgi->a({-href => href(action=>"commit", hash=>$par),4983class=>"list"},$par) .4984"</td>".4985"<td class=\"link\">".4986$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .4987" | ".4988$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .4989"</td>".4990"</tr>\n";4991}4992print"</table>".4993"</div>\n";49944995print"<div class=\"page_body\">\n";4996 git_print_log($co{'comment'});4997print"</div>\n";49984999 git_difftree_body(\@difftree,$hash,@$parents);50005001 git_footer_html();5002}50035004sub git_object {5005# object is defined by:5006# - hash or hash_base alone5007# - hash_base and file_name5008my$type;50095010# - hash or hash_base alone5011if($hash|| ($hash_base&& !defined$file_name)) {5012my$object_id=$hash||$hash_base;50135014open my$fd,"-|", quote_command(5015 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5016or die_error(404,"Object does not exist");5017$type= <$fd>;5018chomp$type;5019close$fd5020or die_error(404,"Object does not exist");50215022# - hash_base and file_name5023}elsif($hash_base&&defined$file_name) {5024$file_name=~ s,/+$,,;50255026system(git_cmd(),"cat-file",'-e',$hash_base) ==05027or die_error(404,"Base object does not exist");50285029# here errors should not hapen5030open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5031or die_error(500,"Open git-ls-tree failed");5032my$line= <$fd>;5033close$fd;50345035#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5036unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5037 die_error(404,"File or directory for given base does not exist");5038}5039$type=$2;5040$hash=$3;5041}else{5042 die_error(400,"Not enough information to find object");5043}50445045print$cgi->redirect(-uri => href(action=>$type, -full=>1,5046 hash=>$hash, hash_base=>$hash_base,5047 file_name=>$file_name),5048-status =>'302 Found');5049}50505051sub git_blobdiff {5052my$format=shift||'html';50535054my$fd;5055my@difftree;5056my%diffinfo;5057my$expires;50585059# preparing $fd and %diffinfo for git_patchset_body5060# new style URI5061if(defined$hash_base&&defined$hash_parent_base) {5062if(defined$file_name) {5063# read raw output5064open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5065$hash_parent_base,$hash_base,5066"--", (defined$file_parent?$file_parent: ()),$file_name5067or die_error(500,"Open git-diff-tree failed");5068@difftree=map{chomp;$_} <$fd>;5069close$fd5070or die_error(404,"Reading git-diff-tree failed");5071@difftree5072or die_error(404,"Blob diff not found");50735074}elsif(defined$hash&&5075$hash=~/[0-9a-fA-F]{40}/) {5076# try to find filename from $hash50775078# read filtered raw output5079open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5080$hash_parent_base,$hash_base,"--"5081or die_error(500,"Open git-diff-tree failed");5082@difftree=5083# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5084# $hash == to_id5085grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5086map{chomp;$_} <$fd>;5087close$fd5088or die_error(404,"Reading git-diff-tree failed");5089@difftree5090or die_error(404,"Blob diff not found");50915092}else{5093 die_error(400,"Missing one of the blob diff parameters");5094}50955096if(@difftree>1) {5097 die_error(400,"Ambiguous blob diff specification");5098}50995100%diffinfo= parse_difftree_raw_line($difftree[0]);5101$file_parent||=$diffinfo{'from_file'} ||$file_name;5102$file_name||=$diffinfo{'to_file'};51035104$hash_parent||=$diffinfo{'from_id'};5105$hash||=$diffinfo{'to_id'};51065107# non-textual hash id's can be cached5108if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5109$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5110$expires='+1d';5111}51125113# open patch output5114open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5115'-p', ($formateq'html'?"--full-index": ()),5116$hash_parent_base,$hash_base,5117"--", (defined$file_parent?$file_parent: ()),$file_name5118or die_error(500,"Open git-diff-tree failed");5119}51205121# old/legacy style URI5122if(!%diffinfo&&# if new style URI failed5123defined$hash&&defined$hash_parent) {5124# fake git-diff-tree raw output5125$diffinfo{'from_mode'} =$diffinfo{'to_mode'} ="blob";5126$diffinfo{'from_id'} =$hash_parent;5127$diffinfo{'to_id'} =$hash;5128if(defined$file_name) {5129if(defined$file_parent) {5130$diffinfo{'status'} ='2';5131$diffinfo{'from_file'} =$file_parent;5132$diffinfo{'to_file'} =$file_name;5133}else{# assume not renamed5134$diffinfo{'status'} ='1';5135$diffinfo{'from_file'} =$file_name;5136$diffinfo{'to_file'} =$file_name;5137}5138}else{# no filename given5139$diffinfo{'status'} ='2';5140$diffinfo{'from_file'} =$hash_parent;5141$diffinfo{'to_file'} =$hash;5142}51435144# non-textual hash id's can be cached5145if($hash=~m/^[0-9a-fA-F]{40}$/&&5146$hash_parent=~m/^[0-9a-fA-F]{40}$/) {5147$expires='+1d';5148}51495150# open patch output5151open$fd,"-|", git_cmd(),"diff",@diff_opts,5152'-p', ($formateq'html'?"--full-index": ()),5153$hash_parent,$hash,"--"5154or die_error(500,"Open git-diff failed");5155}else{5156 die_error(400,"Missing one of the blob diff parameters")5157unless%diffinfo;5158}51595160# header5161if($formateq'html') {5162my$formats_nav=5163$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5164"raw");5165 git_header_html(undef,$expires);5166if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5167 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5168 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5169}else{5170print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5171print"<div class=\"title\">$hashvs$hash_parent</div>\n";5172}5173if(defined$file_name) {5174 git_print_page_path($file_name,"blob",$hash_base);5175}else{5176print"<div class=\"page_path\"></div>\n";5177}51785179}elsif($formateq'plain') {5180print$cgi->header(5181-type =>'text/plain',5182-charset =>'utf-8',5183-expires =>$expires,5184-content_disposition =>'inline; filename="'."$file_name".'.patch"');51855186print"X-Git-Url: ".$cgi->self_url() ."\n\n";51875188}else{5189 die_error(400,"Unknown blobdiff format");5190}51915192# patch5193if($formateq'html') {5194print"<div class=\"page_body\">\n";51955196 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5197close$fd;51985199print"</div>\n";# class="page_body"5200 git_footer_html();52015202}else{5203while(my$line= <$fd>) {5204$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5205$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;52065207print$line;52085209last if$line=~m!^\+\+\+!;5210}5211local$/=undef;5212print<$fd>;5213close$fd;5214}5215}52165217sub git_blobdiff_plain {5218 git_blobdiff('plain');5219}52205221sub git_commitdiff {5222my$format=shift||'html';5223$hash||=$hash_base||"HEAD";5224my%co= parse_commit($hash)5225or die_error(404,"Unknown commit object");52265227# choose format for commitdiff for merge5228if(!defined$hash_parent&& @{$co{'parents'}} >1) {5229$hash_parent='--cc';5230}5231# we need to prepare $formats_nav before almost any parameter munging5232my$formats_nav;5233if($formateq'html') {5234$formats_nav=5235$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5236"raw");52375238if(defined$hash_parent&&5239$hash_parentne'-c'&&$hash_parentne'--cc') {5240# commitdiff with two commits given5241my$hash_parent_short=$hash_parent;5242if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5243$hash_parent_short=substr($hash_parent,0,7);5244}5245$formats_nav.=5246' (from';5247for(my$i=0;$i< @{$co{'parents'}};$i++) {5248if($co{'parents'}[$i]eq$hash_parent) {5249$formats_nav.=' parent '. ($i+1);5250last;5251}5252}5253$formats_nav.=': '.5254$cgi->a({-href => href(action=>"commitdiff",5255 hash=>$hash_parent)},5256 esc_html($hash_parent_short)) .5257')';5258}elsif(!$co{'parent'}) {5259# --root commitdiff5260$formats_nav.=' (initial)';5261}elsif(scalar@{$co{'parents'}} ==1) {5262# single parent commit5263$formats_nav.=5264' (parent: '.5265$cgi->a({-href => href(action=>"commitdiff",5266 hash=>$co{'parent'})},5267 esc_html(substr($co{'parent'},0,7))) .5268')';5269}else{5270# merge commit5271if($hash_parenteq'--cc') {5272$formats_nav.=' | '.5273$cgi->a({-href => href(action=>"commitdiff",5274 hash=>$hash, hash_parent=>'-c')},5275'combined');5276}else{# $hash_parent eq '-c'5277$formats_nav.=' | '.5278$cgi->a({-href => href(action=>"commitdiff",5279 hash=>$hash, hash_parent=>'--cc')},5280'compact');5281}5282$formats_nav.=5283' (merge: '.5284join(' ',map{5285$cgi->a({-href => href(action=>"commitdiff",5286 hash=>$_)},5287 esc_html(substr($_,0,7)));5288} @{$co{'parents'}} ) .5289')';5290}5291}52925293my$hash_parent_param=$hash_parent;5294if(!defined$hash_parent_param) {5295# --cc for multiple parents, --root for parentless5296$hash_parent_param=5297@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5298}52995300# read commitdiff5301my$fd;5302my@difftree;5303if($formateq'html') {5304open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5305"--no-commit-id","--patch-with-raw","--full-index",5306$hash_parent_param,$hash,"--"5307or die_error(500,"Open git-diff-tree failed");53085309while(my$line= <$fd>) {5310chomp$line;5311# empty line ends raw part of diff-tree output5312last unless$line;5313push@difftree,scalar parse_difftree_raw_line($line);5314}53155316}elsif($formateq'plain') {5317open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5318'-p',$hash_parent_param,$hash,"--"5319or die_error(500,"Open git-diff-tree failed");53205321}else{5322 die_error(400,"Unknown commitdiff format");5323}53245325# non-textual hash id's can be cached5326my$expires;5327if($hash=~m/^[0-9a-fA-F]{40}$/) {5328$expires="+1d";5329}53305331# write commit message5332if($formateq'html') {5333my$refs= git_get_references();5334my$ref= format_ref_marker($refs,$co{'id'});53355336 git_header_html(undef,$expires);5337 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5338 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5339 git_print_authorship(\%co);5340print"<div class=\"page_body\">\n";5341if(@{$co{'comment'}} >1) {5342print"<div class=\"log\">\n";5343 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5344print"</div>\n";# class="log"5345}53465347}elsif($formateq'plain') {5348my$refs= git_get_references("tags");5349my$tagname= git_get_rev_name_tags($hash);5350my$filename= basename($project) ."-$hash.patch";53515352print$cgi->header(5353-type =>'text/plain',5354-charset =>'utf-8',5355-expires =>$expires,5356-content_disposition =>'inline; filename="'."$filename".'"');5357my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5358print"From: ". to_utf8($co{'author'}) ."\n";5359print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5360print"Subject: ". to_utf8($co{'title'}) ."\n";53615362print"X-Git-Tag:$tagname\n"if$tagname;5363print"X-Git-Url: ".$cgi->self_url() ."\n\n";53645365foreachmy$line(@{$co{'comment'}}) {5366print to_utf8($line) ."\n";5367}5368print"---\n\n";5369}53705371# write patch5372if($formateq'html') {5373my$use_parents= !defined$hash_parent||5374$hash_parenteq'-c'||$hash_parenteq'--cc';5375 git_difftree_body(\@difftree,$hash,5376$use_parents? @{$co{'parents'}} :$hash_parent);5377print"<br/>\n";53785379 git_patchset_body($fd, \@difftree,$hash,5380$use_parents? @{$co{'parents'}} :$hash_parent);5381close$fd;5382print"</div>\n";# class="page_body"5383 git_footer_html();53845385}elsif($formateq'plain') {5386local$/=undef;5387print<$fd>;5388close$fd5389or print"Reading git-diff-tree failed\n";5390}5391}53925393sub git_commitdiff_plain {5394 git_commitdiff('plain');5395}53965397sub git_history {5398if(!defined$hash_base) {5399$hash_base= git_get_head_hash($project);5400}5401if(!defined$page) {5402$page=0;5403}5404my$ftype;5405my%co= parse_commit($hash_base)5406or die_error(404,"Unknown commit object");54075408my$refs= git_get_references();5409my$limit=sprintf("--max-count=%i", (100* ($page+1)));54105411my@commitlist= parse_commits($hash_base,101, (100*$page),5412$file_name,"--full-history")5413or die_error(404,"No such file or directory on given branch");54145415if(!defined$hash&&defined$file_name) {5416# some commits could have deleted file in question,5417# and not have it in tree, but one of them has to have it5418for(my$i=0;$i<=@commitlist;$i++) {5419$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5420last ifdefined$hash;5421}5422}5423if(defined$hash) {5424$ftype= git_get_type($hash);5425}5426if(!defined$ftype) {5427 die_error(500,"Unknown type of object");5428}54295430my$paging_nav='';5431if($page>0) {5432$paging_nav.=5433$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5434 file_name=>$file_name)},5435"first");5436$paging_nav.=" ⋅ ".5437$cgi->a({-href => href(-replay=>1, page=>$page-1),5438-accesskey =>"p", -title =>"Alt-p"},"prev");5439}else{5440$paging_nav.="first";5441$paging_nav.=" ⋅ prev";5442}5443my$next_link='';5444if($#commitlist>=100) {5445$next_link=5446$cgi->a({-href => href(-replay=>1, page=>$page+1),5447-accesskey =>"n", -title =>"Alt-n"},"next");5448$paging_nav.=" ⋅$next_link";5449}else{5450$paging_nav.=" ⋅ next";5451}54525453 git_header_html();5454 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5455 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5456 git_print_page_path($file_name,$ftype,$hash_base);54575458 git_history_body(\@commitlist,0,99,5459$refs,$hash_base,$ftype,$next_link);54605461 git_footer_html();5462}54635464sub git_search {5465 gitweb_check_feature('search')or die_error(403,"Search is disabled");5466if(!defined$searchtext) {5467 die_error(400,"Text field is empty");5468}5469if(!defined$hash) {5470$hash= git_get_head_hash($project);5471}5472my%co= parse_commit($hash);5473if(!%co) {5474 die_error(404,"Unknown commit object");5475}5476if(!defined$page) {5477$page=0;5478}54795480$searchtype||='commit';5481if($searchtypeeq'pickaxe') {5482# pickaxe may take all resources of your box and run for several minutes5483# with every query - so decide by yourself how public you make this feature5484 gitweb_check_feature('pickaxe')5485or die_error(403,"Pickaxe is disabled");5486}5487if($searchtypeeq'grep') {5488 gitweb_check_feature('grep')5489or die_error(403,"Grep is disabled");5490}54915492 git_header_html();54935494if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5495my$greptype;5496if($searchtypeeq'commit') {5497$greptype="--grep=";5498}elsif($searchtypeeq'author') {5499$greptype="--author=";5500}elsif($searchtypeeq'committer') {5501$greptype="--committer=";5502}5503$greptype.=$searchtext;5504my@commitlist= parse_commits($hash,101, (100*$page),undef,5505$greptype,'--regexp-ignore-case',5506$search_use_regexp?'--extended-regexp':'--fixed-strings');55075508my$paging_nav='';5509if($page>0) {5510$paging_nav.=5511$cgi->a({-href => href(action=>"search", hash=>$hash,5512 searchtext=>$searchtext,5513 searchtype=>$searchtype)},5514"first");5515$paging_nav.=" ⋅ ".5516$cgi->a({-href => href(-replay=>1, page=>$page-1),5517-accesskey =>"p", -title =>"Alt-p"},"prev");5518}else{5519$paging_nav.="first";5520$paging_nav.=" ⋅ prev";5521}5522my$next_link='';5523if($#commitlist>=100) {5524$next_link=5525$cgi->a({-href => href(-replay=>1, page=>$page+1),5526-accesskey =>"n", -title =>"Alt-n"},"next");5527$paging_nav.=" ⋅$next_link";5528}else{5529$paging_nav.=" ⋅ next";5530}55315532if($#commitlist>=100) {5533}55345535 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5536 git_print_header_div('commit', esc_html($co{'title'}),$hash);5537 git_search_grep_body(\@commitlist,0,99,$next_link);5538}55395540if($searchtypeeq'pickaxe') {5541 git_print_page_nav('','',$hash,$co{'tree'},$hash);5542 git_print_header_div('commit', esc_html($co{'title'}),$hash);55435544print"<table class=\"pickaxe search\">\n";5545my$alternate=1;5546$/="\n";5547open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5548'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5549($search_use_regexp?'--pickaxe-regex': ());5550undef%co;5551my@files;5552while(my$line= <$fd>) {5553chomp$line;5554next unless$line;55555556my%set= parse_difftree_raw_line($line);5557if(defined$set{'commit'}) {5558# finish previous commit5559if(%co) {5560print"</td>\n".5561"<td class=\"link\">".5562$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5563" | ".5564$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5565print"</td>\n".5566"</tr>\n";5567}55685569if($alternate) {5570print"<tr class=\"dark\">\n";5571}else{5572print"<tr class=\"light\">\n";5573}5574$alternate^=1;5575%co= parse_commit($set{'commit'});5576my$author= chop_and_escape_str($co{'author_name'},15,5);5577print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5578"<td><i>$author</i></td>\n".5579"<td>".5580$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5581-class=>"list subject"},5582 chop_and_escape_str($co{'title'},50) ."<br/>");5583}elsif(defined$set{'to_id'}) {5584next if($set{'to_id'} =~m/^0{40}$/);55855586print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5587 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5588-class=>"list"},5589"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5590"<br/>\n";5591}5592}5593close$fd;55945595# finish last commit (warning: repetition!)5596if(%co) {5597print"</td>\n".5598"<td class=\"link\">".5599$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5600" | ".5601$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5602print"</td>\n".5603"</tr>\n";5604}56055606print"</table>\n";5607}56085609if($searchtypeeq'grep') {5610 git_print_page_nav('','',$hash,$co{'tree'},$hash);5611 git_print_header_div('commit', esc_html($co{'title'}),$hash);56125613print"<table class=\"grep_search\">\n";5614my$alternate=1;5615my$matches=0;5616$/="\n";5617open my$fd,"-|", git_cmd(),'grep','-n',5618$search_use_regexp? ('-E','-i') :'-F',5619$searchtext,$co{'tree'};5620my$lastfile='';5621while(my$line= <$fd>) {5622chomp$line;5623my($file,$lno,$ltext,$binary);5624last if($matches++>1000);5625if($line=~/^Binary file (.+) matches$/) {5626$file=$1;5627$binary=1;5628}else{5629(undef,$file,$lno,$ltext) =split(/:/,$line,4);5630}5631if($filene$lastfile) {5632$lastfileand print"</td></tr>\n";5633if($alternate++) {5634print"<tr class=\"dark\">\n";5635}else{5636print"<tr class=\"light\">\n";5637}5638print"<td class=\"list\">".5639$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5640 file_name=>"$file"),5641-class=>"list"}, esc_path($file));5642print"</td><td>\n";5643$lastfile=$file;5644}5645if($binary) {5646print"<div class=\"binary\">Binary file</div>\n";5647}else{5648$ltext= untabify($ltext);5649if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5650$ltext= esc_html($1, -nbsp=>1);5651$ltext.='<span class="match">';5652$ltext.= esc_html($2, -nbsp=>1);5653$ltext.='</span>';5654$ltext.= esc_html($3, -nbsp=>1);5655}else{5656$ltext= esc_html($ltext, -nbsp=>1);5657}5658print"<div class=\"pre\">".5659$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5660 file_name=>"$file").'#l'.$lno,5661-class=>"linenr"},sprintf('%4i',$lno))5662.' '.$ltext."</div>\n";5663}5664}5665if($lastfile) {5666print"</td></tr>\n";5667if($matches>1000) {5668print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5669}5670}else{5671print"<div class=\"diff nodifferences\">No matches found</div>\n";5672}5673close$fd;56745675print"</table>\n";5676}5677 git_footer_html();5678}56795680sub git_search_help {5681 git_header_html();5682 git_print_page_nav('','',$hash,$hash,$hash);5683print<<EOT;5684<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5685regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5686the pattern entered is recognized as the POSIX extended5687<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5688insensitive).</p>5689<dl>5690<dt><b>commit</b></dt>5691<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5692EOT5693my($have_grep) = gitweb_check_feature('grep');5694if($have_grep) {5695print<<EOT;5696<dt><b>grep</b></dt>5697<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5698 a different one) are searched for the given pattern. On large trees, this search can take5699a while and put some strain on the server, so please use it with some consideration. Note that5700due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5701case-sensitive.</dd>5702EOT5703}5704print<<EOT;5705<dt><b>author</b></dt>5706<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5707<dt><b>committer</b></dt>5708<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5709EOT5710my($have_pickaxe) = gitweb_check_feature('pickaxe');5711if($have_pickaxe) {5712print<<EOT;5713<dt><b>pickaxe</b></dt>5714<dd>All commits that caused the string to appear or disappear from any file (changes that5715added, removed or "modified" the string) will be listed. This search can take a while and5716takes a lot of strain on the server, so please use it wisely. Note that since you may be5717interested even in changes just changing the case as well, this search is case sensitive.</dd>5718EOT5719}5720print"</dl>\n";5721 git_footer_html();5722}57235724sub git_shortlog {5725my$head= git_get_head_hash($project);5726if(!defined$hash) {5727$hash=$head;5728}5729if(!defined$page) {5730$page=0;5731}5732my$refs= git_get_references();57335734my$commit_hash=$hash;5735if(defined$hash_parent) {5736$commit_hash="$hash_parent..$hash";5737}5738my@commitlist= parse_commits($commit_hash,101, (100*$page));57395740my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5741my$next_link='';5742if($#commitlist>=100) {5743$next_link=5744$cgi->a({-href => href(-replay=>1, page=>$page+1),5745-accesskey =>"n", -title =>"Alt-n"},"next");5746}57475748 git_header_html();5749 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);5750 git_print_header_div('summary',$project);57515752 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);57535754 git_footer_html();5755}57565757## ......................................................................5758## feeds (RSS, Atom; OPML)57595760sub git_feed {5761my$format=shift||'atom';5762my($have_blame) = gitweb_check_feature('blame');57635764# Atom: http://www.atomenabled.org/developers/syndication/5765# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ5766if($formatne'rss'&&$formatne'atom') {5767 die_error(400,"Unknown web feed format");5768}57695770# log/feed of current (HEAD) branch, log of given branch, history of file/directory5771my$head=$hash||'HEAD';5772my@commitlist= parse_commits($head,150,0,$file_name);57735774my%latest_commit;5775my%latest_date;5776my$content_type="application/$format+xml";5777if(defined$cgi->http('HTTP_ACCEPT') &&5778$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {5779# browser (feed reader) prefers text/xml5780$content_type='text/xml';5781}5782if(defined($commitlist[0])) {5783%latest_commit= %{$commitlist[0]};5784%latest_date= parse_date($latest_commit{'author_epoch'});5785print$cgi->header(5786-type =>$content_type,5787-charset =>'utf-8',5788-last_modified =>$latest_date{'rfc2822'});5789}else{5790print$cgi->header(5791-type =>$content_type,5792-charset =>'utf-8');5793}57945795# Optimization: skip generating the body if client asks only5796# for Last-Modified date.5797return if($cgi->request_method()eq'HEAD');57985799# header variables5800my$title="$site_name-$project/$action";5801my$feed_type='log';5802if(defined$hash) {5803$title.=" - '$hash'";5804$feed_type='branch log';5805if(defined$file_name) {5806$title.=" ::$file_name";5807$feed_type='history';5808}5809}elsif(defined$file_name) {5810$title.=" -$file_name";5811$feed_type='history';5812}5813$title.="$feed_type";5814my$descr= git_get_project_description($project);5815if(defined$descr) {5816$descr= esc_html($descr);5817}else{5818$descr="$project".5819($formateq'rss'?'RSS':'Atom') .5820" feed";5821}5822my$owner= git_get_project_owner($project);5823$owner= esc_html($owner);58245825#header5826my$alt_url;5827if(defined$file_name) {5828$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);5829}elsif(defined$hash) {5830$alt_url= href(-full=>1, action=>"log", hash=>$hash);5831}else{5832$alt_url= href(-full=>1, action=>"summary");5833}5834print qq!<?xml version="1.0" encoding="utf-8"?>\n!;5835if($formateq'rss') {5836print<<XML;5837<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">5838<channel>5839XML5840print"<title>$title</title>\n".5841"<link>$alt_url</link>\n".5842"<description>$descr</description>\n".5843"<language>en</language>\n";5844}elsif($formateq'atom') {5845print<<XML;5846<feed xmlns="http://www.w3.org/2005/Atom">5847XML5848print"<title>$title</title>\n".5849"<subtitle>$descr</subtitle>\n".5850'<link rel="alternate" type="text/html" href="'.5851$alt_url.'" />'."\n".5852'<link rel="self" type="'.$content_type.'" href="'.5853$cgi->self_url() .'" />'."\n".5854"<id>". href(-full=>1) ."</id>\n".5855# use project owner for feed author5856"<author><name>$owner</name></author>\n";5857if(defined$favicon) {5858print"<icon>". esc_url($favicon) ."</icon>\n";5859}5860if(defined$logo_url) {5861# not twice as wide as tall: 72 x 27 pixels5862print"<logo>". esc_url($logo) ."</logo>\n";5863}5864if(!%latest_date) {5865# dummy date to keep the feed valid until commits trickle in:5866print"<updated>1970-01-01T00:00:00Z</updated>\n";5867}else{5868print"<updated>$latest_date{'iso-8601'}</updated>\n";5869}5870}58715872# contents5873for(my$i=0;$i<=$#commitlist;$i++) {5874my%co= %{$commitlist[$i]};5875my$commit=$co{'id'};5876# we read 150, we always show 30 and the ones more recent than 48 hours5877if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {5878last;5879}5880my%cd= parse_date($co{'author_epoch'});58815882# get list of changed files5883open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5884$co{'parent'} ||"--root",5885$co{'id'},"--", (defined$file_name?$file_name: ())5886ornext;5887my@difftree=map{chomp;$_} <$fd>;5888close$fd5889ornext;58905891# print element (entry, item)5892my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);5893if($formateq'rss') {5894print"<item>\n".5895"<title>". esc_html($co{'title'}) ."</title>\n".5896"<author>". esc_html($co{'author'}) ."</author>\n".5897"<pubDate>$cd{'rfc2822'}</pubDate>\n".5898"<guid isPermaLink=\"true\">$co_url</guid>\n".5899"<link>$co_url</link>\n".5900"<description>". esc_html($co{'title'}) ."</description>\n".5901"<content:encoded>".5902"<![CDATA[\n";5903}elsif($formateq'atom') {5904print"<entry>\n".5905"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".5906"<updated>$cd{'iso-8601'}</updated>\n".5907"<author>\n".5908" <name>". esc_html($co{'author_name'}) ."</name>\n";5909if($co{'author_email'}) {5910print" <email>". esc_html($co{'author_email'}) ."</email>\n";5911}5912print"</author>\n".5913# use committer for contributor5914"<contributor>\n".5915" <name>". esc_html($co{'committer_name'}) ."</name>\n";5916if($co{'committer_email'}) {5917print" <email>". esc_html($co{'committer_email'}) ."</email>\n";5918}5919print"</contributor>\n".5920"<published>$cd{'iso-8601'}</published>\n".5921"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".5922"<id>$co_url</id>\n".5923"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".5924"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";5925}5926my$comment=$co{'comment'};5927print"<pre>\n";5928foreachmy$line(@$comment) {5929$line= esc_html($line);5930print"$line\n";5931}5932print"</pre><ul>\n";5933foreachmy$difftree_line(@difftree) {5934my%difftree= parse_difftree_raw_line($difftree_line);5935next if!$difftree{'from_id'};59365937my$file=$difftree{'file'} ||$difftree{'to_file'};59385939print"<li>".5940"[".5941$cgi->a({-href => href(-full=>1, action=>"blobdiff",5942 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},5943 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},5944 file_name=>$file, file_parent=>$difftree{'from_file'}),5945-title =>"diff"},'D');5946if($have_blame) {5947print$cgi->a({-href => href(-full=>1, action=>"blame",5948 file_name=>$file, hash_base=>$commit),5949-title =>"blame"},'B');5950}5951# if this is not a feed of a file history5952if(!defined$file_name||$file_namene$file) {5953print$cgi->a({-href => href(-full=>1, action=>"history",5954 file_name=>$file, hash=>$commit),5955-title =>"history"},'H');5956}5957$file= esc_path($file);5958print"] ".5959"$file</li>\n";5960}5961if($formateq'rss') {5962print"</ul>]]>\n".5963"</content:encoded>\n".5964"</item>\n";5965}elsif($formateq'atom') {5966print"</ul>\n</div>\n".5967"</content>\n".5968"</entry>\n";5969}5970}59715972# end of feed5973if($formateq'rss') {5974print"</channel>\n</rss>\n";5975}elsif($formateq'atom') {5976print"</feed>\n";5977}5978}59795980sub git_rss {5981 git_feed('rss');5982}59835984sub git_atom {5985 git_feed('atom');5986}59875988sub git_opml {5989my@list= git_get_projects_list();59905991print$cgi->header(-type =>'text/xml', -charset =>'utf-8');5992print<<XML;5993<?xml version="1.0" encoding="utf-8"?>5994<opml version="1.0">5995<head>5996 <title>$site_nameOPML Export</title>5997</head>5998<body>5999<outline text="git RSS feeds">6000XML60016002foreachmy$pr(@list) {6003my%proj=%$pr;6004my$head= git_get_head_hash($proj{'path'});6005if(!defined$head) {6006next;6007}6008$git_dir="$projectroot/$proj{'path'}";6009my%co= parse_commit($head);6010if(!%co) {6011next;6012}60136014my$path= esc_html(chop_str($proj{'path'},25,5));6015my$rss="$my_url?p=$proj{'path'};a=rss";6016my$html="$my_url?p=$proj{'path'};a=summary";6017print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6018}6019print<<XML;6020</outline>6021</body>6022</opml>6023XML6024}