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# try to put as many parameters as possible in PATH_INFO: 735# - project name 736# - action 737# - hash or hash_base:/filename 738 739# When the script is the root DirectoryIndex for the domain, 740# $href here would be something like http://gitweb.example.com/ 741# Thus, we strip any trailing / from $href, to spare us double 742# slashes in the final URL 743$href=~ s,/$,,; 744 745# Then add the project name, if present 746$href.="/".esc_url($params{'project'})ifdefined$params{'project'}; 747delete$params{'project'}; 748 749# Summary just uses the project path URL, any other action is 750# added to the URL 751if(defined$params{'action'}) { 752$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 753delete$params{'action'}; 754} 755 756# Finally, we put either hash_base:/file_name or hash 757if(defined$params{'hash_base'}) { 758$href.="/".esc_url($params{'hash_base'}); 759if(defined$params{'file_name'}) { 760$href.=":/".esc_url($params{'file_name'}); 761delete$params{'file_name'}; 762} 763delete$params{'hash'}; 764delete$params{'hash_base'}; 765}elsif(defined$params{'hash'}) { 766$href.="/".esc_url($params{'hash'}); 767delete$params{'hash'}; 768} 769} 770 771# now encode the parameters explicitly 772my@result= (); 773for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 774my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 775if(defined$params{$name}) { 776if(ref($params{$name})eq"ARRAY") { 777foreachmy$par(@{$params{$name}}) { 778push@result,$symbol."=". esc_param($par); 779} 780}else{ 781push@result,$symbol."=". esc_param($params{$name}); 782} 783} 784} 785$href.="?".join(';',@result)ifscalar@result; 786 787return$href; 788} 789 790 791## ====================================================================== 792## validation, quoting/unquoting and escaping 793 794sub validate_action { 795my$input=shift||returnundef; 796returnundefunlessexists$actions{$input}; 797return$input; 798} 799 800sub validate_project { 801my$input=shift||returnundef; 802if(!validate_pathname($input) || 803!(-d "$projectroot/$input") || 804!check_head_link("$projectroot/$input") || 805($export_ok&& !(-e "$projectroot/$input/$export_ok")) || 806($strict_export&& !project_in_list($input))) { 807returnundef; 808}else{ 809return$input; 810} 811} 812 813sub validate_pathname { 814my$input=shift||returnundef; 815 816# no '.' or '..' as elements of path, i.e. no '.' nor '..' 817# at the beginning, at the end, and between slashes. 818# also this catches doubled slashes 819if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 820returnundef; 821} 822# no null characters 823if($input=~m!\0!) { 824returnundef; 825} 826return$input; 827} 828 829sub validate_refname { 830my$input=shift||returnundef; 831 832# textual hashes are O.K. 833if($input=~m/^[0-9a-fA-F]{40}$/) { 834return$input; 835} 836# it must be correct pathname 837$input= validate_pathname($input) 838orreturnundef; 839# restrictions on ref name according to git-check-ref-format 840if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { 841returnundef; 842} 843return$input; 844} 845 846# decode sequences of octets in utf8 into Perl's internal form, 847# which is utf-8 with utf8 flag set if needed. gitweb writes out 848# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning 849sub to_utf8 { 850my$str=shift; 851if(utf8::valid($str)) { 852 utf8::decode($str); 853return$str; 854}else{ 855return decode($fallback_encoding,$str, Encode::FB_DEFAULT); 856} 857} 858 859# quote unsafe chars, but keep the slash, even when it's not 860# correct, but quoted slashes look too horrible in bookmarks 861sub esc_param { 862my$str=shift; 863$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg; 864$str=~s/\+/%2B/g; 865$str=~s/ /\+/g; 866return$str; 867} 868 869# quote unsafe chars in whole URL, so some charactrs cannot be quoted 870sub esc_url { 871my$str=shift; 872$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg; 873$str=~s/\+/%2B/g; 874$str=~s/ /\+/g; 875return$str; 876} 877 878# replace invalid utf8 character with SUBSTITUTION sequence 879sub esc_html ($;%) { 880my$str=shift; 881my%opts=@_; 882 883$str= to_utf8($str); 884$str=$cgi->escapeHTML($str); 885if($opts{'-nbsp'}) { 886$str=~s/ / /g; 887} 888$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg; 889return$str; 890} 891 892# quote control characters and escape filename to HTML 893sub esc_path { 894my$str=shift; 895my%opts=@_; 896 897$str= to_utf8($str); 898$str=$cgi->escapeHTML($str); 899if($opts{'-nbsp'}) { 900$str=~s/ / /g; 901} 902$str=~ s|([[:cntrl:]])|quot_cec($1)|eg; 903return$str; 904} 905 906# Make control characters "printable", using character escape codes (CEC) 907sub quot_cec { 908my$cntrl=shift; 909my%opts=@_; 910my%es= (# character escape codes, aka escape sequences 911"\t"=>'\t',# tab (HT) 912"\n"=>'\n',# line feed (LF) 913"\r"=>'\r',# carrige return (CR) 914"\f"=>'\f',# form feed (FF) 915"\b"=>'\b',# backspace (BS) 916"\a"=>'\a',# alarm (bell) (BEL) 917"\e"=>'\e',# escape (ESC) 918"\013"=>'\v',# vertical tab (VT) 919"\000"=>'\0',# nul character (NUL) 920); 921my$chr= ( (exists$es{$cntrl}) 922?$es{$cntrl} 923:sprintf('\%2x',ord($cntrl)) ); 924if($opts{-nohtml}) { 925return$chr; 926}else{ 927return"<span class=\"cntrl\">$chr</span>"; 928} 929} 930 931# Alternatively use unicode control pictures codepoints, 932# Unicode "printable representation" (PR) 933sub quot_upr { 934my$cntrl=shift; 935my%opts=@_; 936 937my$chr=sprintf('&#%04d;',0x2400+ord($cntrl)); 938if($opts{-nohtml}) { 939return$chr; 940}else{ 941return"<span class=\"cntrl\">$chr</span>"; 942} 943} 944 945# git may return quoted and escaped filenames 946sub unquote { 947my$str=shift; 948 949sub unq { 950my$seq=shift; 951my%es= (# character escape codes, aka escape sequences 952't'=>"\t",# tab (HT, TAB) 953'n'=>"\n",# newline (NL) 954'r'=>"\r",# return (CR) 955'f'=>"\f",# form feed (FF) 956'b'=>"\b",# backspace (BS) 957'a'=>"\a",# alarm (bell) (BEL) 958'e'=>"\e",# escape (ESC) 959'v'=>"\013",# vertical tab (VT) 960); 961 962if($seq=~m/^[0-7]{1,3}$/) { 963# octal char sequence 964returnchr(oct($seq)); 965}elsif(exists$es{$seq}) { 966# C escape sequence, aka character escape code 967return$es{$seq}; 968} 969# quoted ordinary character 970return$seq; 971} 972 973if($str=~m/^"(.*)"$/) { 974# needs unquoting 975$str=$1; 976$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg; 977} 978return$str; 979} 980 981# escape tabs (convert tabs to spaces) 982sub untabify { 983my$line=shift; 984 985while((my$pos=index($line,"\t")) != -1) { 986if(my$count= (8- ($pos%8))) { 987my$spaces=' ' x $count; 988$line=~s/\t/$spaces/; 989} 990} 991 992return$line; 993} 994 995sub project_in_list { 996my$project=shift; 997my@list= git_get_projects_list(); 998return@list&&scalar(grep{$_->{'path'}eq$project}@list); 999}10001001## ----------------------------------------------------------------------1002## HTML aware string manipulation10031004# Try to chop given string on a word boundary between position1005# $len and $len+$add_len. If there is no word boundary there,1006# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1007# (marking chopped part) would be longer than given string.1008sub chop_str {1009my$str=shift;1010my$len=shift;1011my$add_len=shift||10;1012my$where=shift||'right';# 'left' | 'center' | 'right'10131014# Make sure perl knows it is utf8 encoded so we don't1015# cut in the middle of a utf8 multibyte char.1016$str= to_utf8($str);10171018# allow only $len chars, but don't cut a word if it would fit in $add_len1019# if it doesn't fit, cut it if it's still longer than the dots we would add1020# remove chopped character entities entirely10211022# when chopping in the middle, distribute $len into left and right part1023# return early if chopping wouldn't make string shorter1024if($whereeq'center') {1025return$strif($len+5>=length($str));# filler is length 51026$len=int($len/2);1027}else{1028return$strif($len+4>=length($str));# filler is length 41029}10301031# regexps: ending and beginning with word part up to $add_len1032my$endre=qr/.{$len}\w{0,$add_len}/;1033my$begre=qr/\w{0,$add_len}.{$len}/;10341035if($whereeq'left') {1036$str=~m/^(.*?)($begre)$/;1037my($lead,$body) = ($1,$2);1038if(length($lead) >4) {1039$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1040$lead=" ...";1041}1042return"$lead$body";10431044}elsif($whereeq'center') {1045$str=~m/^($endre)(.*)$/;1046my($left,$str) = ($1,$2);1047$str=~m/^(.*?)($begre)$/;1048my($mid,$right) = ($1,$2);1049if(length($mid) >5) {1050$left=~s/&[^;]*$//;1051$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1052$mid=" ... ";1053}1054return"$left$mid$right";10551056}else{1057$str=~m/^($endre)(.*)$/;1058my$body=$1;1059my$tail=$2;1060if(length($tail) >4) {1061$body=~s/&[^;]*$//;1062$tail="... ";1063}1064return"$body$tail";1065}1066}10671068# takes the same arguments as chop_str, but also wraps a <span> around the1069# result with a title attribute if it does get chopped. Additionally, the1070# string is HTML-escaped.1071sub chop_and_escape_str {1072my($str) =@_;10731074my$chopped= chop_str(@_);1075if($choppedeq$str) {1076return esc_html($chopped);1077}else{1078$str=~s/([[:cntrl:]])/?/g;1079return$cgi->span({-title=>$str}, esc_html($chopped));1080}1081}10821083## ----------------------------------------------------------------------1084## functions returning short strings10851086# CSS class for given age value (in seconds)1087sub age_class {1088my$age=shift;10891090if(!defined$age) {1091return"noage";1092}elsif($age<60*60*2) {1093return"age0";1094}elsif($age<60*60*24*2) {1095return"age1";1096}else{1097return"age2";1098}1099}11001101# convert age in seconds to "nn units ago" string1102sub age_string {1103my$age=shift;1104my$age_str;11051106if($age>60*60*24*365*2) {1107$age_str= (int$age/60/60/24/365);1108$age_str.=" years ago";1109}elsif($age>60*60*24*(365/12)*2) {1110$age_str=int$age/60/60/24/(365/12);1111$age_str.=" months ago";1112}elsif($age>60*60*24*7*2) {1113$age_str=int$age/60/60/24/7;1114$age_str.=" weeks ago";1115}elsif($age>60*60*24*2) {1116$age_str=int$age/60/60/24;1117$age_str.=" days ago";1118}elsif($age>60*60*2) {1119$age_str=int$age/60/60;1120$age_str.=" hours ago";1121}elsif($age>60*2) {1122$age_str=int$age/60;1123$age_str.=" min ago";1124}elsif($age>2) {1125$age_str=int$age;1126$age_str.=" sec ago";1127}else{1128$age_str.=" right now";1129}1130return$age_str;1131}11321133useconstant{1134 S_IFINVALID =>0030000,1135 S_IFGITLINK =>0160000,1136};11371138# submodule/subproject, a commit object reference1139sub S_ISGITLINK($) {1140my$mode=shift;11411142return(($mode& S_IFMT) == S_IFGITLINK)1143}11441145# convert file mode in octal to symbolic file mode string1146sub mode_str {1147my$mode=oct shift;11481149if(S_ISGITLINK($mode)) {1150return'm---------';1151}elsif(S_ISDIR($mode& S_IFMT)) {1152return'drwxr-xr-x';1153}elsif(S_ISLNK($mode)) {1154return'lrwxrwxrwx';1155}elsif(S_ISREG($mode)) {1156# git cares only about the executable bit1157if($mode& S_IXUSR) {1158return'-rwxr-xr-x';1159}else{1160return'-rw-r--r--';1161};1162}else{1163return'----------';1164}1165}11661167# convert file mode in octal to file type string1168sub file_type {1169my$mode=shift;11701171if($mode!~m/^[0-7]+$/) {1172return$mode;1173}else{1174$mode=oct$mode;1175}11761177if(S_ISGITLINK($mode)) {1178return"submodule";1179}elsif(S_ISDIR($mode& S_IFMT)) {1180return"directory";1181}elsif(S_ISLNK($mode)) {1182return"symlink";1183}elsif(S_ISREG($mode)) {1184return"file";1185}else{1186return"unknown";1187}1188}11891190# convert file mode in octal to file type description string1191sub file_type_long {1192my$mode=shift;11931194if($mode!~m/^[0-7]+$/) {1195return$mode;1196}else{1197$mode=oct$mode;1198}11991200if(S_ISGITLINK($mode)) {1201return"submodule";1202}elsif(S_ISDIR($mode& S_IFMT)) {1203return"directory";1204}elsif(S_ISLNK($mode)) {1205return"symlink";1206}elsif(S_ISREG($mode)) {1207if($mode& S_IXUSR) {1208return"executable";1209}else{1210return"file";1211};1212}else{1213return"unknown";1214}1215}121612171218## ----------------------------------------------------------------------1219## functions returning short HTML fragments, or transforming HTML fragments1220## which don't belong to other sections12211222# format line of commit message.1223sub format_log_line_html {1224my$line=shift;12251226$line= esc_html($line, -nbsp=>1);1227if($line=~m/([0-9a-fA-F]{8,40})/) {1228my$hash_text=$1;1229my$link=1230$cgi->a({-href => href(action=>"object", hash=>$hash_text),1231-class=>"text"},$hash_text);1232$line=~s/$hash_text/$link/;1233}1234return$line;1235}12361237# format marker of refs pointing to given object12381239# the destination action is chosen based on object type and current context:1240# - for annotated tags, we choose the tag view unless it's the current view1241# already, in which case we go to shortlog view1242# - for other refs, we keep the current view if we're in history, shortlog or1243# log view, and select shortlog otherwise1244sub format_ref_marker {1245my($refs,$id) =@_;1246my$markers='';12471248if(defined$refs->{$id}) {1249foreachmy$ref(@{$refs->{$id}}) {1250# this code exploits the fact that non-lightweight tags are the1251# only indirect objects, and that they are the only objects for which1252# we want to use tag instead of shortlog as action1253my($type,$name) =qw();1254my$indirect= ($ref=~s/\^\{\}$//);1255# e.g. tags/v2.6.11 or heads/next1256if($ref=~m!^(.*?)s?/(.*)$!) {1257$type=$1;1258$name=$2;1259}else{1260$type="ref";1261$name=$ref;1262}12631264my$class=$type;1265$class.=" indirect"if$indirect;12661267my$dest_action="shortlog";12681269if($indirect) {1270$dest_action="tag"unless$actioneq"tag";1271}elsif($action=~/^(history|(short)?log)$/) {1272$dest_action=$action;1273}12741275my$dest="";1276$dest.="refs/"unless$ref=~ m!^refs/!;1277$dest.=$ref;12781279my$link=$cgi->a({1280-href => href(1281 action=>$dest_action,1282 hash=>$dest1283)},$name);12841285$markers.=" <span class=\"$class\"title=\"$ref\">".1286$link."</span>";1287}1288}12891290if($markers) {1291return' <span class="refs">'.$markers.'</span>';1292}else{1293return"";1294}1295}12961297# format, perhaps shortened and with markers, title line1298sub format_subject_html {1299my($long,$short,$href,$extra) =@_;1300$extra=''unlessdefined($extra);13011302if(length($short) <length($long)) {1303return$cgi->a({-href =>$href, -class=>"list subject",1304-title => to_utf8($long)},1305 esc_html($short) .$extra);1306}else{1307return$cgi->a({-href =>$href, -class=>"list subject"},1308 esc_html($long) .$extra);1309}1310}13111312# format git diff header line, i.e. "diff --(git|combined|cc) ..."1313sub format_git_diff_header_line {1314my$line=shift;1315my$diffinfo=shift;1316my($from,$to) =@_;13171318if($diffinfo->{'nparents'}) {1319# combined diff1320$line=~s!^(diff (.*?) )"?.*$!$1!;1321if($to->{'href'}) {1322$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1323 esc_path($to->{'file'}));1324}else{# file was deleted (no href)1325$line.= esc_path($to->{'file'});1326}1327}else{1328# "ordinary" diff1329$line=~s!^(diff (.*?) )"?a/.*$!$1!;1330if($from->{'href'}) {1331$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1332'a/'. esc_path($from->{'file'}));1333}else{# file was added (no href)1334$line.='a/'. esc_path($from->{'file'});1335}1336$line.=' ';1337if($to->{'href'}) {1338$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1339'b/'. esc_path($to->{'file'}));1340}else{# file was deleted1341$line.='b/'. esc_path($to->{'file'});1342}1343}13441345return"<div class=\"diff header\">$line</div>\n";1346}13471348# format extended diff header line, before patch itself1349sub format_extended_diff_header_line {1350my$line=shift;1351my$diffinfo=shift;1352my($from,$to) =@_;13531354# match <path>1355if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1356$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1357 esc_path($from->{'file'}));1358}1359if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1360$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1361 esc_path($to->{'file'}));1362}1363# match single <mode>1364if($line=~m/\s(\d{6})$/) {1365$line.='<span class="info"> ('.1366 file_type_long($1) .1367')</span>';1368}1369# match <hash>1370if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1371# can match only for combined diff1372$line='index ';1373for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1374if($from->{'href'}[$i]) {1375$line.=$cgi->a({-href=>$from->{'href'}[$i],1376-class=>"hash"},1377substr($diffinfo->{'from_id'}[$i],0,7));1378}else{1379$line.='0' x 7;1380}1381# separator1382$line.=','if($i<$diffinfo->{'nparents'} -1);1383}1384$line.='..';1385if($to->{'href'}) {1386$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1387substr($diffinfo->{'to_id'},0,7));1388}else{1389$line.='0' x 7;1390}13911392}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1393# can match only for ordinary diff1394my($from_link,$to_link);1395if($from->{'href'}) {1396$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1397substr($diffinfo->{'from_id'},0,7));1398}else{1399$from_link='0' x 7;1400}1401if($to->{'href'}) {1402$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1403substr($diffinfo->{'to_id'},0,7));1404}else{1405$to_link='0' x 7;1406}1407my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1408$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1409}14101411return$line."<br/>\n";1412}14131414# format from-file/to-file diff header1415sub format_diff_from_to_header {1416my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1417my$line;1418my$result='';14191420$line=$from_line;1421#assert($line =~ m/^---/) if DEBUG;1422# no extra formatting for "^--- /dev/null"1423if(!$diffinfo->{'nparents'}) {1424# ordinary (single parent) diff1425if($line=~m!^--- "?a/!) {1426if($from->{'href'}) {1427$line='--- a/'.1428$cgi->a({-href=>$from->{'href'}, -class=>"path"},1429 esc_path($from->{'file'}));1430}else{1431$line='--- a/'.1432 esc_path($from->{'file'});1433}1434}1435$result.= qq!<div class="diff from_file">$line</div>\n!;14361437}else{1438# combined diff (merge commit)1439for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1440if($from->{'href'}[$i]) {1441$line='--- '.1442$cgi->a({-href=>href(action=>"blobdiff",1443 hash_parent=>$diffinfo->{'from_id'}[$i],1444 hash_parent_base=>$parents[$i],1445 file_parent=>$from->{'file'}[$i],1446 hash=>$diffinfo->{'to_id'},1447 hash_base=>$hash,1448 file_name=>$to->{'file'}),1449-class=>"path",1450-title=>"diff". ($i+1)},1451$i+1) .1452'/'.1453$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1454 esc_path($from->{'file'}[$i]));1455}else{1456$line='--- /dev/null';1457}1458$result.= qq!<div class="diff from_file">$line</div>\n!;1459}1460}14611462$line=$to_line;1463#assert($line =~ m/^\+\+\+/) if DEBUG;1464# no extra formatting for "^+++ /dev/null"1465if($line=~m!^\+\+\+ "?b/!) {1466if($to->{'href'}) {1467$line='+++ b/'.1468$cgi->a({-href=>$to->{'href'}, -class=>"path"},1469 esc_path($to->{'file'}));1470}else{1471$line='+++ b/'.1472 esc_path($to->{'file'});1473}1474}1475$result.= qq!<div class="diff to_file">$line</div>\n!;14761477return$result;1478}14791480# create note for patch simplified by combined diff1481sub format_diff_cc_simplified {1482my($diffinfo,@parents) =@_;1483my$result='';14841485$result.="<div class=\"diff header\">".1486"diff --cc ";1487if(!is_deleted($diffinfo)) {1488$result.=$cgi->a({-href => href(action=>"blob",1489 hash_base=>$hash,1490 hash=>$diffinfo->{'to_id'},1491 file_name=>$diffinfo->{'to_file'}),1492-class=>"path"},1493 esc_path($diffinfo->{'to_file'}));1494}else{1495$result.= esc_path($diffinfo->{'to_file'});1496}1497$result.="</div>\n".# class="diff header"1498"<div class=\"diff nodifferences\">".1499"Simple merge".1500"</div>\n";# class="diff nodifferences"15011502return$result;1503}15041505# format patch (diff) line (not to be used for diff headers)1506sub format_diff_line {1507my$line=shift;1508my($from,$to) =@_;1509my$diff_class="";15101511chomp$line;15121513if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1514# combined diff1515my$prefix=substr($line,0,scalar@{$from->{'href'}});1516if($line=~m/^\@{3}/) {1517$diff_class=" chunk_header";1518}elsif($line=~m/^\\/) {1519$diff_class=" incomplete";1520}elsif($prefix=~tr/+/+/) {1521$diff_class=" add";1522}elsif($prefix=~tr/-/-/) {1523$diff_class=" rem";1524}1525}else{1526# assume ordinary diff1527my$char=substr($line,0,1);1528if($chareq'+') {1529$diff_class=" add";1530}elsif($chareq'-') {1531$diff_class=" rem";1532}elsif($chareq'@') {1533$diff_class=" chunk_header";1534}elsif($chareq"\\") {1535$diff_class=" incomplete";1536}1537}1538$line= untabify($line);1539if($from&&$to&&$line=~m/^\@{2} /) {1540my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1541$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;15421543$from_lines=0unlessdefined$from_lines;1544$to_lines=0unlessdefined$to_lines;15451546if($from->{'href'}) {1547$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1548-class=>"list"},$from_text);1549}1550if($to->{'href'}) {1551$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1552-class=>"list"},$to_text);1553}1554$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1555"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1556return"<div class=\"diff$diff_class\">$line</div>\n";1557}elsif($from&&$to&&$line=~m/^\@{3}/) {1558my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1559my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);15601561@from_text=split(' ',$ranges);1562for(my$i=0;$i<@from_text; ++$i) {1563($from_start[$i],$from_nlines[$i]) =1564(split(',',substr($from_text[$i],1)),0);1565}15661567$to_text=pop@from_text;1568$to_start=pop@from_start;1569$to_nlines=pop@from_nlines;15701571$line="<span class=\"chunk_info\">$prefix";1572for(my$i=0;$i<@from_text; ++$i) {1573if($from->{'href'}[$i]) {1574$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1575-class=>"list"},$from_text[$i]);1576}else{1577$line.=$from_text[$i];1578}1579$line.=" ";1580}1581if($to->{'href'}) {1582$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1583-class=>"list"},$to_text);1584}else{1585$line.=$to_text;1586}1587$line.="$prefix</span>".1588"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1589return"<div class=\"diff$diff_class\">$line</div>\n";1590}1591return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1592}15931594# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1595# linked. Pass the hash of the tree/commit to snapshot.1596sub format_snapshot_links {1597my($hash) =@_;1598my@snapshot_fmts= gitweb_check_feature('snapshot');1599@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);1600my$num_fmts=@snapshot_fmts;1601if($num_fmts>1) {1602# A parenthesized list of links bearing format names.1603# e.g. "snapshot (_tar.gz_ _zip_)"1604return"snapshot (".join(' ',map1605$cgi->a({1606-href => href(1607 action=>"snapshot",1608 hash=>$hash,1609 snapshot_format=>$_1610)1611},$known_snapshot_formats{$_}{'display'})1612,@snapshot_fmts) .")";1613}elsif($num_fmts==1) {1614# A single "snapshot" link whose tooltip bears the format name.1615# i.e. "_snapshot_"1616my($fmt) =@snapshot_fmts;1617return1618$cgi->a({1619-href => href(1620 action=>"snapshot",1621 hash=>$hash,1622 snapshot_format=>$fmt1623),1624-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1625},"snapshot");1626}else{# $num_fmts == 01627returnundef;1628}1629}16301631## ......................................................................1632## functions returning values to be passed, perhaps after some1633## transformation, to other functions; e.g. returning arguments to href()16341635# returns hash to be passed to href to generate gitweb URL1636# in -title key it returns description of link1637sub get_feed_info {1638my$format=shift||'Atom';1639my%res= (action =>lc($format));16401641# feed links are possible only for project views1642return unless(defined$project);1643# some views should link to OPML, or to generic project feed,1644# or don't have specific feed yet (so they should use generic)1645return if($action=~/^(?:tags|heads|forks|tag|search)$/x);16461647my$branch;1648# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1649# from tag links; this also makes possible to detect branch links1650if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1651(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1652$branch=$1;1653}1654# find log type for feed description (title)1655my$type='log';1656if(defined$file_name) {1657$type="history of$file_name";1658$type.="/"if($actioneq'tree');1659$type.=" on '$branch'"if(defined$branch);1660}else{1661$type="log of$branch"if(defined$branch);1662}16631664$res{-title} =$type;1665$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1666$res{'file_name'} =$file_name;16671668return%res;1669}16701671## ----------------------------------------------------------------------1672## git utility subroutines, invoking git commands16731674# returns path to the core git executable and the --git-dir parameter as list1675sub git_cmd {1676return$GIT,'--git-dir='.$git_dir;1677}16781679# quote the given arguments for passing them to the shell1680# quote_command("command", "arg 1", "arg with ' and ! characters")1681# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1682# Try to avoid using this function wherever possible.1683sub quote_command {1684returnjoin(' ',1685map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1686}16871688# get HEAD ref of given project as hash1689sub git_get_head_hash {1690my$project=shift;1691my$o_git_dir=$git_dir;1692my$retval=undef;1693$git_dir="$projectroot/$project";1694if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1695my$head= <$fd>;1696close$fd;1697if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1698$retval=$1;1699}1700}1701if(defined$o_git_dir) {1702$git_dir=$o_git_dir;1703}1704return$retval;1705}17061707# get type of given object1708sub git_get_type {1709my$hash=shift;17101711open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1712my$type= <$fd>;1713close$fdorreturn;1714chomp$type;1715return$type;1716}17171718# repository configuration1719our$config_file='';1720our%config;17211722# store multiple values for single key as anonymous array reference1723# single values stored directly in the hash, not as [ <value> ]1724sub hash_set_multi {1725my($hash,$key,$value) =@_;17261727if(!exists$hash->{$key}) {1728$hash->{$key} =$value;1729}elsif(!ref$hash->{$key}) {1730$hash->{$key} = [$hash->{$key},$value];1731}else{1732push@{$hash->{$key}},$value;1733}1734}17351736# return hash of git project configuration1737# optionally limited to some section, e.g. 'gitweb'1738sub git_parse_project_config {1739my$section_regexp=shift;1740my%config;17411742local$/="\0";17431744open my$fh,"-|", git_cmd(),"config",'-z','-l',1745orreturn;17461747while(my$keyval= <$fh>) {1748chomp$keyval;1749my($key,$value) =split(/\n/,$keyval,2);17501751 hash_set_multi(\%config,$key,$value)1752if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1753}1754close$fh;17551756return%config;1757}17581759# convert config value to boolean, 'true' or 'false'1760# no value, number > 0, 'true' and 'yes' values are true1761# rest of values are treated as false (never as error)1762sub config_to_bool {1763my$val=shift;17641765# strip leading and trailing whitespace1766$val=~s/^\s+//;1767$val=~s/\s+$//;17681769return(!defined$val||# section.key1770($val=~/^\d+$/&&$val) ||# section.key = 11771($val=~/^(?:true|yes)$/i));# section.key = true1772}17731774# convert config value to simple decimal number1775# an optional value suffix of 'k', 'm', or 'g' will cause the value1776# to be multiplied by 1024, 1048576, or 10737418241777sub config_to_int {1778my$val=shift;17791780# strip leading and trailing whitespace1781$val=~s/^\s+//;1782$val=~s/\s+$//;17831784if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1785$unit=lc($unit);1786# unknown unit is treated as 11787return$num* ($uniteq'g'?1073741824:1788$uniteq'm'?1048576:1789$uniteq'k'?1024:1);1790}1791return$val;1792}17931794# convert config value to array reference, if needed1795sub config_to_multi {1796my$val=shift;17971798returnref($val) ?$val: (defined($val) ? [$val] : []);1799}18001801sub git_get_project_config {1802my($key,$type) =@_;18031804# key sanity check1805return unless($key);1806$key=~s/^gitweb\.//;1807return if($key=~m/\W/);18081809# type sanity check1810if(defined$type) {1811$type=~s/^--//;1812$type=undef1813unless($typeeq'bool'||$typeeq'int');1814}18151816# get config1817if(!defined$config_file||1818$config_filene"$git_dir/config") {1819%config= git_parse_project_config('gitweb');1820$config_file="$git_dir/config";1821}18221823# ensure given type1824if(!defined$type) {1825return$config{"gitweb.$key"};1826}elsif($typeeq'bool') {1827# backward compatibility: 'git config --bool' returns true/false1828return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1829}elsif($typeeq'int') {1830return config_to_int($config{"gitweb.$key"});1831}1832return$config{"gitweb.$key"};1833}18341835# get hash of given path at given ref1836sub git_get_hash_by_path {1837my$base=shift;1838my$path=shift||returnundef;1839my$type=shift;18401841$path=~ s,/+$,,;18421843open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path1844or die_error(500,"Open git-ls-tree failed");1845my$line= <$fd>;1846close$fdorreturnundef;18471848if(!defined$line) {1849# there is no tree or hash given by $path at $base1850returnundef;1851}18521853#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'1854$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;1855if(defined$type&&$typene$2) {1856# type doesn't match1857returnundef;1858}1859return$3;1860}18611862# get path of entry with given hash at given tree-ish (ref)1863# used to get 'from' filename for combined diff (merge commit) for renames1864sub git_get_path_by_hash {1865my$base=shift||return;1866my$hash=shift||return;18671868local$/="\0";18691870open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base1871orreturnundef;1872while(my$line= <$fd>) {1873chomp$line;18741875#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'1876#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'1877if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {1878close$fd;1879return$1;1880}1881}1882close$fd;1883returnundef;1884}18851886## ......................................................................1887## git utility functions, directly accessing git repository18881889sub git_get_project_description {1890my$path=shift;18911892$git_dir="$projectroot/$path";1893open my$fd,"$git_dir/description"1894orreturn git_get_project_config('description');1895my$descr= <$fd>;1896close$fd;1897if(defined$descr) {1898chomp$descr;1899}1900return$descr;1901}19021903sub git_get_project_ctags {1904my$path=shift;1905my$ctags= {};19061907$git_dir="$projectroot/$path";1908foreach(<$git_dir/ctags/*>) {1909open CT,$_ornext;1910my$val= <CT>;1911chomp$val;1912close CT;1913my$ctag=$_;$ctag=~ s#.*/##;1914$ctags->{$ctag} =$val;1915}1916$ctags;1917}19181919sub git_populate_project_tagcloud {1920my$ctags=shift;19211922# First, merge different-cased tags; tags vote on casing1923my%ctags_lc;1924foreach(keys%$ctags) {1925$ctags_lc{lc$_}->{count} +=$ctags->{$_};1926if(not$ctags_lc{lc$_}->{topcount}1927or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {1928$ctags_lc{lc$_}->{topcount} =$ctags->{$_};1929$ctags_lc{lc$_}->{topname} =$_;1930}1931}19321933my$cloud;1934if(eval{require HTML::TagCloud;1; }) {1935$cloud= HTML::TagCloud->new;1936foreach(sort keys%ctags_lc) {1937# Pad the title with spaces so that the cloud looks1938# less crammed.1939my$title=$ctags_lc{$_}->{topname};1940$title=~s/ / /g;1941$title=~s/^/ /g;1942$title=~s/$/ /g;1943$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});1944}1945}else{1946$cloud= \%ctags_lc;1947}1948$cloud;1949}19501951sub git_show_project_tagcloud {1952my($cloud,$count) =@_;1953print STDERR ref($cloud)."..\n";1954if(ref$cloudeq'HTML::TagCloud') {1955return$cloud->html_and_css($count);1956}else{1957my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;1958return'<p align="center">'.join(', ',map{1959"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"1960}splice(@tags,0,$count)) .'</p>';1961}1962}19631964sub git_get_project_url_list {1965my$path=shift;19661967$git_dir="$projectroot/$path";1968open my$fd,"$git_dir/cloneurl"1969orreturnwantarray?1970@{ config_to_multi(git_get_project_config('url')) } :1971 config_to_multi(git_get_project_config('url'));1972my@git_project_url_list=map{chomp;$_} <$fd>;1973close$fd;19741975returnwantarray?@git_project_url_list: \@git_project_url_list;1976}19771978sub git_get_projects_list {1979my($filter) =@_;1980my@list;19811982$filter||='';1983$filter=~s/\.git$//;19841985my($check_forks) = gitweb_check_feature('forks');19861987if(-d $projects_list) {1988# search in directory1989my$dir=$projects_list. ($filter?"/$filter":'');1990# remove the trailing "/"1991$dir=~s!/+$!!;1992my$pfxlen=length("$dir");1993my$pfxdepth= ($dir=~tr!/!!);19941995 File::Find::find({1996 follow_fast =>1,# follow symbolic links1997 follow_skip =>2,# ignore duplicates1998 dangling_symlinks =>0,# ignore dangling symlinks, silently1999 wanted =>sub{2000# skip project-list toplevel, if we get it.2001return if(m!^[/.]$!);2002# only directories can be git repositories2003return unless(-d $_);2004# don't traverse too deep (Find is super slow on os x)2005if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2006$File::Find::prune =1;2007return;2008}20092010my$subdir=substr($File::Find::name,$pfxlen+1);2011# we check related file in $projectroot2012if(check_export_ok("$projectroot/$filter/$subdir")) {2013push@list, { path => ($filter?"$filter/":'') .$subdir};2014$File::Find::prune =1;2015}2016},2017},"$dir");20182019}elsif(-f $projects_list) {2020# read from file(url-encoded):2021# 'git%2Fgit.git Linus+Torvalds'2022# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2023# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2024my%paths;2025open my($fd),$projects_listorreturn;2026 PROJECT:2027while(my$line= <$fd>) {2028chomp$line;2029my($path,$owner) =split' ',$line;2030$path= unescape($path);2031$owner= unescape($owner);2032if(!defined$path) {2033next;2034}2035if($filterne'') {2036# looking for forks;2037my$pfx=substr($path,0,length($filter));2038if($pfxne$filter) {2039next PROJECT;2040}2041my$sfx=substr($path,length($filter));2042if($sfx!~/^\/.*\.git$/) {2043next PROJECT;2044}2045}elsif($check_forks) {2046 PATH:2047foreachmy$filter(keys%paths) {2048# looking for forks;2049my$pfx=substr($path,0,length($filter));2050if($pfxne$filter) {2051next PATH;2052}2053my$sfx=substr($path,length($filter));2054if($sfx!~/^\/.*\.git$/) {2055next PATH;2056}2057# is a fork, don't include it in2058# the list2059next PROJECT;2060}2061}2062if(check_export_ok("$projectroot/$path")) {2063my$pr= {2064 path =>$path,2065 owner => to_utf8($owner),2066};2067push@list,$pr;2068(my$forks_path=$path) =~s/\.git$//;2069$paths{$forks_path}++;2070}2071}2072close$fd;2073}2074return@list;2075}20762077our$gitweb_project_owner=undef;2078sub git_get_project_list_from_file {20792080return if(defined$gitweb_project_owner);20812082$gitweb_project_owner= {};2083# read from file (url-encoded):2084# 'git%2Fgit.git Linus+Torvalds'2085# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2086# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2087if(-f $projects_list) {2088open(my$fd,$projects_list);2089while(my$line= <$fd>) {2090chomp$line;2091my($pr,$ow) =split' ',$line;2092$pr= unescape($pr);2093$ow= unescape($ow);2094$gitweb_project_owner->{$pr} = to_utf8($ow);2095}2096close$fd;2097}2098}20992100sub git_get_project_owner {2101my$project=shift;2102my$owner;21032104returnundefunless$project;2105$git_dir="$projectroot/$project";21062107if(!defined$gitweb_project_owner) {2108 git_get_project_list_from_file();2109}21102111if(exists$gitweb_project_owner->{$project}) {2112$owner=$gitweb_project_owner->{$project};2113}2114if(!defined$owner){2115$owner= git_get_project_config('owner');2116}2117if(!defined$owner) {2118$owner= get_file_owner("$git_dir");2119}21202121return$owner;2122}21232124sub git_get_last_activity {2125my($path) =@_;2126my$fd;21272128$git_dir="$projectroot/$path";2129open($fd,"-|", git_cmd(),'for-each-ref',2130'--format=%(committer)',2131'--sort=-committerdate',2132'--count=1',2133'refs/heads')orreturn;2134my$most_recent= <$fd>;2135close$fdorreturn;2136if(defined$most_recent&&2137$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2138my$timestamp=$1;2139my$age=time-$timestamp;2140return($age, age_string($age));2141}2142return(undef,undef);2143}21442145sub git_get_references {2146my$type=shift||"";2147my%refs;2148# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112149# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2150open my$fd,"-|", git_cmd(),"show-ref","--dereference",2151($type? ("--","refs/$type") : ())# use -- <pattern> if $type2152orreturn;21532154while(my$line= <$fd>) {2155chomp$line;2156if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2157if(defined$refs{$1}) {2158push@{$refs{$1}},$2;2159}else{2160$refs{$1} = [$2];2161}2162}2163}2164close$fdorreturn;2165return \%refs;2166}21672168sub git_get_rev_name_tags {2169my$hash=shift||returnundef;21702171open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2172orreturn;2173my$name_rev= <$fd>;2174close$fd;21752176if($name_rev=~ m|^$hash tags/(.*)$|) {2177return$1;2178}else{2179# catches also '$hash undefined' output2180returnundef;2181}2182}21832184## ----------------------------------------------------------------------2185## parse to hash functions21862187sub parse_date {2188my$epoch=shift;2189my$tz=shift||"-0000";21902191my%date;2192my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2193my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2194my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2195$date{'hour'} =$hour;2196$date{'minute'} =$min;2197$date{'mday'} =$mday;2198$date{'day'} =$days[$wday];2199$date{'month'} =$months[$mon];2200$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2201$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2202$date{'mday-time'} =sprintf"%d%s%02d:%02d",2203$mday,$months[$mon],$hour,$min;2204$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",22051900+$year,1+$mon,$mday,$hour,$min,$sec;22062207$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2208my$local=$epoch+ ((int$1+ ($2/60)) *3600);2209($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2210$date{'hour_local'} =$hour;2211$date{'minute_local'} =$min;2212$date{'tz_local'} =$tz;2213$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",22141900+$year,$mon+1,$mday,2215$hour,$min,$sec,$tz);2216return%date;2217}22182219sub parse_tag {2220my$tag_id=shift;2221my%tag;2222my@comment;22232224open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2225$tag{'id'} =$tag_id;2226while(my$line= <$fd>) {2227chomp$line;2228if($line=~m/^object ([0-9a-fA-F]{40})$/) {2229$tag{'object'} =$1;2230}elsif($line=~m/^type (.+)$/) {2231$tag{'type'} =$1;2232}elsif($line=~m/^tag (.+)$/) {2233$tag{'name'} =$1;2234}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2235$tag{'author'} =$1;2236$tag{'epoch'} =$2;2237$tag{'tz'} =$3;2238}elsif($line=~m/--BEGIN/) {2239push@comment,$line;2240last;2241}elsif($lineeq"") {2242last;2243}2244}2245push@comment, <$fd>;2246$tag{'comment'} = \@comment;2247close$fdorreturn;2248if(!defined$tag{'name'}) {2249return2250};2251return%tag2252}22532254sub parse_commit_text {2255my($commit_text,$withparents) =@_;2256my@commit_lines=split'\n',$commit_text;2257my%co;22582259pop@commit_lines;# Remove '\0'22602261if(!@commit_lines) {2262return;2263}22642265my$header=shift@commit_lines;2266if($header!~m/^[0-9a-fA-F]{40}/) {2267return;2268}2269($co{'id'},my@parents) =split' ',$header;2270while(my$line=shift@commit_lines) {2271last if$lineeq"\n";2272if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2273$co{'tree'} =$1;2274}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2275push@parents,$1;2276}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2277$co{'author'} =$1;2278$co{'author_epoch'} =$2;2279$co{'author_tz'} =$3;2280if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2281$co{'author_name'} =$1;2282$co{'author_email'} =$2;2283}else{2284$co{'author_name'} =$co{'author'};2285}2286}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2287$co{'committer'} =$1;2288$co{'committer_epoch'} =$2;2289$co{'committer_tz'} =$3;2290$co{'committer_name'} =$co{'committer'};2291if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2292$co{'committer_name'} =$1;2293$co{'committer_email'} =$2;2294}else{2295$co{'committer_name'} =$co{'committer'};2296}2297}2298}2299if(!defined$co{'tree'}) {2300return;2301};2302$co{'parents'} = \@parents;2303$co{'parent'} =$parents[0];23042305foreachmy$title(@commit_lines) {2306$title=~s/^ //;2307if($titlene"") {2308$co{'title'} = chop_str($title,80,5);2309# remove leading stuff of merges to make the interesting part visible2310if(length($title) >50) {2311$title=~s/^Automatic //;2312$title=~s/^merge (of|with) /Merge ... /i;2313if(length($title) >50) {2314$title=~s/(http|rsync):\/\///;2315}2316if(length($title) >50) {2317$title=~s/(master|www|rsync)\.//;2318}2319if(length($title) >50) {2320$title=~s/kernel.org:?//;2321}2322if(length($title) >50) {2323$title=~s/\/pub\/scm//;2324}2325}2326$co{'title_short'} = chop_str($title,50,5);2327last;2328}2329}2330if(!defined$co{'title'} ||$co{'title'}eq"") {2331$co{'title'} =$co{'title_short'} ='(no commit message)';2332}2333# remove added spaces2334foreachmy$line(@commit_lines) {2335$line=~s/^ //;2336}2337$co{'comment'} = \@commit_lines;23382339my$age=time-$co{'committer_epoch'};2340$co{'age'} =$age;2341$co{'age_string'} = age_string($age);2342my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2343if($age>60*60*24*7*2) {2344$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2345$co{'age_string_age'} =$co{'age_string'};2346}else{2347$co{'age_string_date'} =$co{'age_string'};2348$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2349}2350return%co;2351}23522353sub parse_commit {2354my($commit_id) =@_;2355my%co;23562357local$/="\0";23582359open my$fd,"-|", git_cmd(),"rev-list",2360"--parents",2361"--header",2362"--max-count=1",2363$commit_id,2364"--",2365or die_error(500,"Open git-rev-list failed");2366%co= parse_commit_text(<$fd>,1);2367close$fd;23682369return%co;2370}23712372sub parse_commits {2373my($commit_id,$maxcount,$skip,$filename,@args) =@_;2374my@cos;23752376$maxcount||=1;2377$skip||=0;23782379local$/="\0";23802381open my$fd,"-|", git_cmd(),"rev-list",2382"--header",2383@args,2384("--max-count=".$maxcount),2385("--skip=".$skip),2386@extra_options,2387$commit_id,2388"--",2389($filename? ($filename) : ())2390or die_error(500,"Open git-rev-list failed");2391while(my$line= <$fd>) {2392my%co= parse_commit_text($line);2393push@cos, \%co;2394}2395close$fd;23962397returnwantarray?@cos: \@cos;2398}23992400# parse line of git-diff-tree "raw" output2401sub parse_difftree_raw_line {2402my$line=shift;2403my%res;24042405# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2406# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2407if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2408$res{'from_mode'} =$1;2409$res{'to_mode'} =$2;2410$res{'from_id'} =$3;2411$res{'to_id'} =$4;2412$res{'status'} =$5;2413$res{'similarity'} =$6;2414if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2415($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2416}else{2417$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2418}2419}2420# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2421# combined diff (for merge commit)2422elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2423$res{'nparents'} =length($1);2424$res{'from_mode'} = [split(' ',$2) ];2425$res{'to_mode'} =pop@{$res{'from_mode'}};2426$res{'from_id'} = [split(' ',$3) ];2427$res{'to_id'} =pop@{$res{'from_id'}};2428$res{'status'} = [split('',$4) ];2429$res{'to_file'} = unquote($5);2430}2431# 'c512b523472485aef4fff9e57b229d9d243c967f'2432elsif($line=~m/^([0-9a-fA-F]{40})$/) {2433$res{'commit'} =$1;2434}24352436returnwantarray?%res: \%res;2437}24382439# wrapper: return parsed line of git-diff-tree "raw" output2440# (the argument might be raw line, or parsed info)2441sub parsed_difftree_line {2442my$line_or_ref=shift;24432444if(ref($line_or_ref)eq"HASH") {2445# pre-parsed (or generated by hand)2446return$line_or_ref;2447}else{2448return parse_difftree_raw_line($line_or_ref);2449}2450}24512452# parse line of git-ls-tree output2453sub parse_ls_tree_line ($;%) {2454my$line=shift;2455my%opts=@_;2456my%res;24572458#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2459$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;24602461$res{'mode'} =$1;2462$res{'type'} =$2;2463$res{'hash'} =$3;2464if($opts{'-z'}) {2465$res{'name'} =$4;2466}else{2467$res{'name'} = unquote($4);2468}24692470returnwantarray?%res: \%res;2471}24722473# generates _two_ hashes, references to which are passed as 2 and 3 argument2474sub parse_from_to_diffinfo {2475my($diffinfo,$from,$to,@parents) =@_;24762477if($diffinfo->{'nparents'}) {2478# combined diff2479$from->{'file'} = [];2480$from->{'href'} = [];2481 fill_from_file_info($diffinfo,@parents)2482unlessexists$diffinfo->{'from_file'};2483for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2484$from->{'file'}[$i] =2485defined$diffinfo->{'from_file'}[$i] ?2486$diffinfo->{'from_file'}[$i] :2487$diffinfo->{'to_file'};2488if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2489$from->{'href'}[$i] = href(action=>"blob",2490 hash_base=>$parents[$i],2491 hash=>$diffinfo->{'from_id'}[$i],2492 file_name=>$from->{'file'}[$i]);2493}else{2494$from->{'href'}[$i] =undef;2495}2496}2497}else{2498# ordinary (not combined) diff2499$from->{'file'} =$diffinfo->{'from_file'};2500if($diffinfo->{'status'}ne"A") {# not new (added) file2501$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2502 hash=>$diffinfo->{'from_id'},2503 file_name=>$from->{'file'});2504}else{2505delete$from->{'href'};2506}2507}25082509$to->{'file'} =$diffinfo->{'to_file'};2510if(!is_deleted($diffinfo)) {# file exists in result2511$to->{'href'} = href(action=>"blob", hash_base=>$hash,2512 hash=>$diffinfo->{'to_id'},2513 file_name=>$to->{'file'});2514}else{2515delete$to->{'href'};2516}2517}25182519## ......................................................................2520## parse to array of hashes functions25212522sub git_get_heads_list {2523my$limit=shift;2524my@headslist;25252526open my$fd,'-|', git_cmd(),'for-each-ref',2527($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2528'--format=%(objectname) %(refname) %(subject)%00%(committer)',2529'refs/heads'2530orreturn;2531while(my$line= <$fd>) {2532my%ref_item;25332534chomp$line;2535my($refinfo,$committerinfo) =split(/\0/,$line);2536my($hash,$name,$title) =split(' ',$refinfo,3);2537my($committer,$epoch,$tz) =2538($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2539$ref_item{'fullname'} =$name;2540$name=~s!^refs/heads/!!;25412542$ref_item{'name'} =$name;2543$ref_item{'id'} =$hash;2544$ref_item{'title'} =$title||'(no commit message)';2545$ref_item{'epoch'} =$epoch;2546if($epoch) {2547$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2548}else{2549$ref_item{'age'} ="unknown";2550}25512552push@headslist, \%ref_item;2553}2554close$fd;25552556returnwantarray?@headslist: \@headslist;2557}25582559sub git_get_tags_list {2560my$limit=shift;2561my@tagslist;25622563open my$fd,'-|', git_cmd(),'for-each-ref',2564($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2565'--format=%(objectname) %(objecttype) %(refname) '.2566'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2567'refs/tags'2568orreturn;2569while(my$line= <$fd>) {2570my%ref_item;25712572chomp$line;2573my($refinfo,$creatorinfo) =split(/\0/,$line);2574my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2575my($creator,$epoch,$tz) =2576($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2577$ref_item{'fullname'} =$name;2578$name=~s!^refs/tags/!!;25792580$ref_item{'type'} =$type;2581$ref_item{'id'} =$id;2582$ref_item{'name'} =$name;2583if($typeeq"tag") {2584$ref_item{'subject'} =$title;2585$ref_item{'reftype'} =$reftype;2586$ref_item{'refid'} =$refid;2587}else{2588$ref_item{'reftype'} =$type;2589$ref_item{'refid'} =$id;2590}25912592if($typeeq"tag"||$typeeq"commit") {2593$ref_item{'epoch'} =$epoch;2594if($epoch) {2595$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2596}else{2597$ref_item{'age'} ="unknown";2598}2599}26002601push@tagslist, \%ref_item;2602}2603close$fd;26042605returnwantarray?@tagslist: \@tagslist;2606}26072608## ----------------------------------------------------------------------2609## filesystem-related functions26102611sub get_file_owner {2612my$path=shift;26132614my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2615my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2616if(!defined$gcos) {2617returnundef;2618}2619my$owner=$gcos;2620$owner=~s/[,;].*$//;2621return to_utf8($owner);2622}26232624## ......................................................................2625## mimetype related functions26262627sub mimetype_guess_file {2628my$filename=shift;2629my$mimemap=shift;2630-r $mimemaporreturnundef;26312632my%mimemap;2633open(MIME,$mimemap)orreturnundef;2634while(<MIME>) {2635next ifm/^#/;# skip comments2636my($mime,$exts) =split(/\t+/);2637if(defined$exts) {2638my@exts=split(/\s+/,$exts);2639foreachmy$ext(@exts) {2640$mimemap{$ext} =$mime;2641}2642}2643}2644close(MIME);26452646$filename=~/\.([^.]*)$/;2647return$mimemap{$1};2648}26492650sub mimetype_guess {2651my$filename=shift;2652my$mime;2653$filename=~/\./orreturnundef;26542655if($mimetypes_file) {2656my$file=$mimetypes_file;2657if($file!~m!^/!) {# if it is relative path2658# it is relative to project2659$file="$projectroot/$project/$file";2660}2661$mime= mimetype_guess_file($filename,$file);2662}2663$mime||= mimetype_guess_file($filename,'/etc/mime.types');2664return$mime;2665}26662667sub blob_mimetype {2668my$fd=shift;2669my$filename=shift;26702671if($filename) {2672my$mime= mimetype_guess($filename);2673$mimeandreturn$mime;2674}26752676# just in case2677return$default_blob_plain_mimetypeunless$fd;26782679if(-T $fd) {2680return'text/plain';2681}elsif(!$filename) {2682return'application/octet-stream';2683}elsif($filename=~m/\.png$/i) {2684return'image/png';2685}elsif($filename=~m/\.gif$/i) {2686return'image/gif';2687}elsif($filename=~m/\.jpe?g$/i) {2688return'image/jpeg';2689}else{2690return'application/octet-stream';2691}2692}26932694sub blob_contenttype {2695my($fd,$file_name,$type) =@_;26962697$type||= blob_mimetype($fd,$file_name);2698if($typeeq'text/plain'&&defined$default_text_plain_charset) {2699$type.="; charset=$default_text_plain_charset";2700}27012702return$type;2703}27042705## ======================================================================2706## functions printing HTML: header, footer, error page27072708sub git_header_html {2709my$status=shift||"200 OK";2710my$expires=shift;27112712my$title="$site_name";2713if(defined$project) {2714$title.=" - ". to_utf8($project);2715if(defined$action) {2716$title.="/$action";2717if(defined$file_name) {2718$title.=" - ". esc_path($file_name);2719if($actioneq"tree"&&$file_name!~ m|/$|) {2720$title.="/";2721}2722}2723}2724}2725my$content_type;2726# require explicit support from the UA if we are to send the page as2727# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2728# we have to do this because MSIE sometimes globs '*/*', pretending to2729# support xhtml+xml but choking when it gets what it asked for.2730if(defined$cgi->http('HTTP_ACCEPT') &&2731$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2732$cgi->Accept('application/xhtml+xml') !=0) {2733$content_type='application/xhtml+xml';2734}else{2735$content_type='text/html';2736}2737print$cgi->header(-type=>$content_type, -charset =>'utf-8',2738-status=>$status, -expires =>$expires);2739my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2740print<<EOF;2741<?xml version="1.0" encoding="utf-8"?>2742<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2743<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2744<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2745<!-- git core binaries version$git_version-->2746<head>2747<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2748<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2749<meta name="robots" content="index, nofollow"/>2750<title>$title</title>2751EOF2752# print out each stylesheet that exist2753if(defined$stylesheet) {2754#provides backwards capability for those people who define style sheet in a config file2755print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2756}else{2757foreachmy$stylesheet(@stylesheets) {2758next unless$stylesheet;2759print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2760}2761}2762if(defined$project) {2763my%href_params= get_feed_info();2764if(!exists$href_params{'-title'}) {2765$href_params{'-title'} ='log';2766}27672768foreachmy$formatqw(RSS Atom){2769my$type=lc($format);2770my%link_attr= (2771'-rel'=>'alternate',2772'-title'=>"$project-$href_params{'-title'} -$formatfeed",2773'-type'=>"application/$type+xml"2774);27752776$href_params{'action'} =$type;2777$link_attr{'-href'} = href(%href_params);2778print"<link ".2779"rel=\"$link_attr{'-rel'}\"".2780"title=\"$link_attr{'-title'}\"".2781"href=\"$link_attr{'-href'}\"".2782"type=\"$link_attr{'-type'}\"".2783"/>\n";27842785$href_params{'extra_options'} ='--no-merges';2786$link_attr{'-href'} = href(%href_params);2787$link_attr{'-title'} .=' (no merges)';2788print"<link ".2789"rel=\"$link_attr{'-rel'}\"".2790"title=\"$link_attr{'-title'}\"".2791"href=\"$link_attr{'-href'}\"".2792"type=\"$link_attr{'-type'}\"".2793"/>\n";2794}27952796}else{2797printf('<link rel="alternate" title="%sprojects list" '.2798'href="%s" type="text/plain; charset=utf-8" />'."\n",2799$site_name, href(project=>undef, action=>"project_index"));2800printf('<link rel="alternate" title="%sprojects feeds" '.2801'href="%s" type="text/x-opml" />'."\n",2802$site_name, href(project=>undef, action=>"opml"));2803}2804if(defined$favicon) {2805printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2806}28072808print"</head>\n".2809"<body>\n";28102811if(-f $site_header) {2812open(my$fd,$site_header);2813print<$fd>;2814close$fd;2815}28162817print"<div class=\"page_header\">\n".2818$cgi->a({-href => esc_url($logo_url),2819-title =>$logo_label},2820qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2821print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2822if(defined$project) {2823print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2824if(defined$action) {2825print" /$action";2826}2827print"\n";2828}2829print"</div>\n";28302831my($have_search) = gitweb_check_feature('search');2832if(defined$project&&$have_search) {2833if(!defined$searchtext) {2834$searchtext="";2835}2836my$search_hash;2837if(defined$hash_base) {2838$search_hash=$hash_base;2839}elsif(defined$hash) {2840$search_hash=$hash;2841}else{2842$search_hash="HEAD";2843}2844my$action=$my_uri;2845my($use_pathinfo) = gitweb_check_feature('pathinfo');2846if($use_pathinfo) {2847$action.="/".esc_url($project);2848}2849print$cgi->startform(-method=>"get", -action =>$action) .2850"<div class=\"search\">\n".2851(!$use_pathinfo&&2852$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .2853$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".2854$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".2855$cgi->popup_menu(-name =>'st', -default=>'commit',2856-values=> ['commit','grep','author','committer','pickaxe']) .2857$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .2858" search:\n",2859$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".2860"<span title=\"Extended regular expression\">".2861$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',2862-checked =>$search_use_regexp) .2863"</span>".2864"</div>".2865$cgi->end_form() ."\n";2866}2867}28682869sub git_footer_html {2870my$feed_class='rss_logo';28712872print"<div class=\"page_footer\">\n";2873if(defined$project) {2874my$descr= git_get_project_description($project);2875if(defined$descr) {2876print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";2877}28782879my%href_params= get_feed_info();2880if(!%href_params) {2881$feed_class.=' generic';2882}2883$href_params{'-title'} ||='log';28842885foreachmy$formatqw(RSS Atom){2886$href_params{'action'} =lc($format);2887print$cgi->a({-href => href(%href_params),2888-title =>"$href_params{'-title'}$formatfeed",2889-class=>$feed_class},$format)."\n";2890}28912892}else{2893print$cgi->a({-href => href(project=>undef, action=>"opml"),2894-class=>$feed_class},"OPML") ." ";2895print$cgi->a({-href => href(project=>undef, action=>"project_index"),2896-class=>$feed_class},"TXT") ."\n";2897}2898print"</div>\n";# class="page_footer"28992900if(-f $site_footer) {2901open(my$fd,$site_footer);2902print<$fd>;2903close$fd;2904}29052906print"</body>\n".2907"</html>";2908}29092910# die_error(<http_status_code>, <error_message>)2911# Example: die_error(404, 'Hash not found')2912# By convention, use the following status codes (as defined in RFC 2616):2913# 400: Invalid or missing CGI parameters, or2914# requested object exists but has wrong type.2915# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on2916# this server or project.2917# 404: Requested object/revision/project doesn't exist.2918# 500: The server isn't configured properly, or2919# an internal error occurred (e.g. failed assertions caused by bugs), or2920# an unknown error occurred (e.g. the git binary died unexpectedly).2921sub die_error {2922my$status=shift||500;2923my$error=shift||"Internal server error";29242925my%http_responses= (400=>'400 Bad Request',2926403=>'403 Forbidden',2927404=>'404 Not Found',2928500=>'500 Internal Server Error');2929 git_header_html($http_responses{$status});2930print<<EOF;2931<div class="page_body">2932<br /><br />2933$status-$error2934<br />2935</div>2936EOF2937 git_footer_html();2938exit;2939}29402941## ----------------------------------------------------------------------2942## functions printing or outputting HTML: navigation29432944sub git_print_page_nav {2945my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;2946$extra=''if!defined$extra;# pager or formats29472948my@navs=qw(summary shortlog log commit commitdiff tree);2949if($suppress) {2950@navs=grep{$_ne$suppress}@navs;2951}29522953my%arg=map{$_=> {action=>$_} }@navs;2954if(defined$head) {2955for(qw(commit commitdiff)) {2956$arg{$_}{'hash'} =$head;2957}2958if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {2959for(qw(shortlog log)) {2960$arg{$_}{'hash'} =$head;2961}2962}2963}29642965$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;2966$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;29672968my@actions= gitweb_check_feature('actions');2969while(@actions) {2970my($label,$link,$pos) = (shift(@actions),shift(@actions),shift(@actions));2971@navs=map{$_eq$pos? ($_,$label) :$_}@navs;2972# munch munch2973$link=~ s#%n#$project#g;2974$link=~ s#%f#$git_dir#g;2975$treehead?$link=~ s#%h#$treehead#g : $link =~ s#%h##g;2976$treebase?$link=~ s#%b#$treebase#g : $link =~ s#%b##g;2977$arg{$label}{'_href'} =$link;2978}29792980print"<div class=\"page_nav\">\n".2981(join" | ",2982map{$_eq$current?2983$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")2984}@navs);2985print"<br/>\n$extra<br/>\n".2986"</div>\n";2987}29882989sub format_paging_nav {2990my($action,$hash,$head,$page,$has_next_link) =@_;2991my$paging_nav;299229932994if($hashne$head||$page) {2995$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");2996}else{2997$paging_nav.="HEAD";2998}29993000if($page>0) {3001$paging_nav.=" ⋅ ".3002$cgi->a({-href => href(-replay=>1, page=>$page-1),3003-accesskey =>"p", -title =>"Alt-p"},"prev");3004}else{3005$paging_nav.=" ⋅ prev";3006}30073008if($has_next_link) {3009$paging_nav.=" ⋅ ".3010$cgi->a({-href => href(-replay=>1, page=>$page+1),3011-accesskey =>"n", -title =>"Alt-n"},"next");3012}else{3013$paging_nav.=" ⋅ next";3014}30153016return$paging_nav;3017}30183019## ......................................................................3020## functions printing or outputting HTML: div30213022sub git_print_header_div {3023my($action,$title,$hash,$hash_base) =@_;3024my%args= ();30253026$args{'action'} =$action;3027$args{'hash'} =$hashif$hash;3028$args{'hash_base'} =$hash_baseif$hash_base;30293030print"<div class=\"header\">\n".3031$cgi->a({-href => href(%args), -class=>"title"},3032$title?$title:$action) .3033"\n</div>\n";3034}30353036#sub git_print_authorship (\%) {3037sub git_print_authorship {3038my$co=shift;30393040my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3041print"<div class=\"author_date\">".3042 esc_html($co->{'author_name'}) .3043" [$ad{'rfc2822'}";3044if($ad{'hour_local'} <6) {3045printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3046$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3047}else{3048printf(" (%02d:%02d%s)",3049$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3050}3051print"]</div>\n";3052}30533054sub git_print_page_path {3055my$name=shift;3056my$type=shift;3057my$hb=shift;305830593060print"<div class=\"page_path\">";3061print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3062-title =>'tree root'}, to_utf8("[$project]"));3063print" / ";3064if(defined$name) {3065my@dirname=split'/',$name;3066my$basename=pop@dirname;3067my$fullname='';30683069foreachmy$dir(@dirname) {3070$fullname.= ($fullname?'/':'') .$dir;3071print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3072 hash_base=>$hb),3073-title =>$fullname}, esc_path($dir));3074print" / ";3075}3076if(defined$type&&$typeeq'blob') {3077print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3078 hash_base=>$hb),3079-title =>$name}, esc_path($basename));3080}elsif(defined$type&&$typeeq'tree') {3081print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3082 hash_base=>$hb),3083-title =>$name}, esc_path($basename));3084print" / ";3085}else{3086print esc_path($basename);3087}3088}3089print"<br/></div>\n";3090}30913092# sub git_print_log (\@;%) {3093sub git_print_log ($;%) {3094my$log=shift;3095my%opts=@_;30963097if($opts{'-remove_title'}) {3098# remove title, i.e. first line of log3099shift@$log;3100}3101# remove leading empty lines3102while(defined$log->[0] &&$log->[0]eq"") {3103shift@$log;3104}31053106# print log3107my$signoff=0;3108my$empty=0;3109foreachmy$line(@$log) {3110if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3111$signoff=1;3112$empty=0;3113if(!$opts{'-remove_signoff'}) {3114print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3115next;3116}else{3117# remove signoff lines3118next;3119}3120}else{3121$signoff=0;3122}31233124# print only one empty line3125# do not print empty line after signoff3126if($lineeq"") {3127next if($empty||$signoff);3128$empty=1;3129}else{3130$empty=0;3131}31323133print format_log_line_html($line) ."<br/>\n";3134}31353136if($opts{'-final_empty_line'}) {3137# end with single empty line3138print"<br/>\n"unless$empty;3139}3140}31413142# return link target (what link points to)3143sub git_get_link_target {3144my$hash=shift;3145my$link_target;31463147# read link3148open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3149orreturn;3150{3151local$/;3152$link_target= <$fd>;3153}3154close$fd3155orreturn;31563157return$link_target;3158}31593160# given link target, and the directory (basedir) the link is in,3161# return target of link relative to top directory (top tree);3162# return undef if it is not possible (including absolute links).3163sub normalize_link_target {3164my($link_target,$basedir,$hash_base) =@_;31653166# we can normalize symlink target only if $hash_base is provided3167return unless$hash_base;31683169# absolute symlinks (beginning with '/') cannot be normalized3170return if(substr($link_target,0,1)eq'/');31713172# normalize link target to path from top (root) tree (dir)3173my$path;3174if($basedir) {3175$path=$basedir.'/'.$link_target;3176}else{3177# we are in top (root) tree (dir)3178$path=$link_target;3179}31803181# remove //, /./, and /../3182my@path_parts;3183foreachmy$part(split('/',$path)) {3184# discard '.' and ''3185next if(!$part||$parteq'.');3186# handle '..'3187if($parteq'..') {3188if(@path_parts) {3189pop@path_parts;3190}else{3191# link leads outside repository (outside top dir)3192return;3193}3194}else{3195push@path_parts,$part;3196}3197}3198$path=join('/',@path_parts);31993200return$path;3201}32023203# print tree entry (row of git_tree), but without encompassing <tr> element3204sub git_print_tree_entry {3205my($t,$basedir,$hash_base,$have_blame) =@_;32063207my%base_key= ();3208$base_key{'hash_base'} =$hash_baseifdefined$hash_base;32093210# The format of a table row is: mode list link. Where mode is3211# the mode of the entry, list is the name of the entry, an href,3212# and link is the action links of the entry.32133214print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3215if($t->{'type'}eq"blob") {3216print"<td class=\"list\">".3217$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3218 file_name=>"$basedir$t->{'name'}",%base_key),3219-class=>"list"}, esc_path($t->{'name'}));3220if(S_ISLNK(oct$t->{'mode'})) {3221my$link_target= git_get_link_target($t->{'hash'});3222if($link_target) {3223my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3224if(defined$norm_target) {3225print" -> ".3226$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3227 file_name=>$norm_target),3228-title =>$norm_target}, esc_path($link_target));3229}else{3230print" -> ". esc_path($link_target);3231}3232}3233}3234print"</td>\n";3235print"<td class=\"link\">";3236print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3237 file_name=>"$basedir$t->{'name'}",%base_key)},3238"blob");3239if($have_blame) {3240print" | ".3241$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3242 file_name=>"$basedir$t->{'name'}",%base_key)},3243"blame");3244}3245if(defined$hash_base) {3246print" | ".3247$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3248 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3249"history");3250}3251print" | ".3252$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3253 file_name=>"$basedir$t->{'name'}")},3254"raw");3255print"</td>\n";32563257}elsif($t->{'type'}eq"tree") {3258print"<td class=\"list\">";3259print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3260 file_name=>"$basedir$t->{'name'}",%base_key)},3261 esc_path($t->{'name'}));3262print"</td>\n";3263print"<td class=\"link\">";3264print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3265 file_name=>"$basedir$t->{'name'}",%base_key)},3266"tree");3267if(defined$hash_base) {3268print" | ".3269$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3270 file_name=>"$basedir$t->{'name'}")},3271"history");3272}3273print"</td>\n";3274}else{3275# unknown object: we can only present history for it3276# (this includes 'commit' object, i.e. submodule support)3277print"<td class=\"list\">".3278 esc_path($t->{'name'}) .3279"</td>\n";3280print"<td class=\"link\">";3281if(defined$hash_base) {3282print$cgi->a({-href => href(action=>"history",3283 hash_base=>$hash_base,3284 file_name=>"$basedir$t->{'name'}")},3285"history");3286}3287print"</td>\n";3288}3289}32903291## ......................................................................3292## functions printing large fragments of HTML32933294# get pre-image filenames for merge (combined) diff3295sub fill_from_file_info {3296my($diff,@parents) =@_;32973298$diff->{'from_file'} = [ ];3299$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3300for(my$i=0;$i<$diff->{'nparents'};$i++) {3301if($diff->{'status'}[$i]eq'R'||3302$diff->{'status'}[$i]eq'C') {3303$diff->{'from_file'}[$i] =3304 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3305}3306}33073308return$diff;3309}33103311# is current raw difftree line of file deletion3312sub is_deleted {3313my$diffinfo=shift;33143315return$diffinfo->{'to_id'}eq('0' x 40);3316}33173318# does patch correspond to [previous] difftree raw line3319# $diffinfo - hashref of parsed raw diff format3320# $patchinfo - hashref of parsed patch diff format3321# (the same keys as in $diffinfo)3322sub is_patch_split {3323my($diffinfo,$patchinfo) =@_;33243325returndefined$diffinfo&&defined$patchinfo3326&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3327}332833293330sub git_difftree_body {3331my($difftree,$hash,@parents) =@_;3332my($parent) =$parents[0];3333my($have_blame) = gitweb_check_feature('blame');3334print"<div class=\"list_head\">\n";3335if($#{$difftree} >10) {3336print(($#{$difftree} +1) ." files changed:\n");3337}3338print"</div>\n";33393340print"<table class=\"".3341(@parents>1?"combined ":"") .3342"diff_tree\">\n";33433344# header only for combined diff in 'commitdiff' view3345my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3346if($has_header) {3347# table header3348print"<thead><tr>\n".3349"<th></th><th></th>\n";# filename, patchN link3350for(my$i=0;$i<@parents;$i++) {3351my$par=$parents[$i];3352print"<th>".3353$cgi->a({-href => href(action=>"commitdiff",3354 hash=>$hash, hash_parent=>$par),3355-title =>'commitdiff to parent number '.3356($i+1) .': '.substr($par,0,7)},3357$i+1) .3358" </th>\n";3359}3360print"</tr></thead>\n<tbody>\n";3361}33623363my$alternate=1;3364my$patchno=0;3365foreachmy$line(@{$difftree}) {3366my$diff= parsed_difftree_line($line);33673368if($alternate) {3369print"<tr class=\"dark\">\n";3370}else{3371print"<tr class=\"light\">\n";3372}3373$alternate^=1;33743375if(exists$diff->{'nparents'}) {# combined diff33763377 fill_from_file_info($diff,@parents)3378unlessexists$diff->{'from_file'};33793380if(!is_deleted($diff)) {3381# file exists in the result (child) commit3382print"<td>".3383$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3384 file_name=>$diff->{'to_file'},3385 hash_base=>$hash),3386-class=>"list"}, esc_path($diff->{'to_file'})) .3387"</td>\n";3388}else{3389print"<td>".3390 esc_path($diff->{'to_file'}) .3391"</td>\n";3392}33933394if($actioneq'commitdiff') {3395# link to patch3396$patchno++;3397print"<td class=\"link\">".3398$cgi->a({-href =>"#patch$patchno"},"patch") .3399" | ".3400"</td>\n";3401}34023403my$has_history=0;3404my$not_deleted=0;3405for(my$i=0;$i<$diff->{'nparents'};$i++) {3406my$hash_parent=$parents[$i];3407my$from_hash=$diff->{'from_id'}[$i];3408my$from_path=$diff->{'from_file'}[$i];3409my$status=$diff->{'status'}[$i];34103411$has_history||= ($statusne'A');3412$not_deleted||= ($statusne'D');34133414if($statuseq'A') {3415print"<td class=\"link\"align=\"right\"> | </td>\n";3416}elsif($statuseq'D') {3417print"<td class=\"link\">".3418$cgi->a({-href => href(action=>"blob",3419 hash_base=>$hash,3420 hash=>$from_hash,3421 file_name=>$from_path)},3422"blob". ($i+1)) .3423" | </td>\n";3424}else{3425if($diff->{'to_id'}eq$from_hash) {3426print"<td class=\"link nochange\">";3427}else{3428print"<td class=\"link\">";3429}3430print$cgi->a({-href => href(action=>"blobdiff",3431 hash=>$diff->{'to_id'},3432 hash_parent=>$from_hash,3433 hash_base=>$hash,3434 hash_parent_base=>$hash_parent,3435 file_name=>$diff->{'to_file'},3436 file_parent=>$from_path)},3437"diff". ($i+1)) .3438" | </td>\n";3439}3440}34413442print"<td class=\"link\">";3443if($not_deleted) {3444print$cgi->a({-href => href(action=>"blob",3445 hash=>$diff->{'to_id'},3446 file_name=>$diff->{'to_file'},3447 hash_base=>$hash)},3448"blob");3449print" | "if($has_history);3450}3451if($has_history) {3452print$cgi->a({-href => href(action=>"history",3453 file_name=>$diff->{'to_file'},3454 hash_base=>$hash)},3455"history");3456}3457print"</td>\n";34583459print"</tr>\n";3460next;# instead of 'else' clause, to avoid extra indent3461}3462# else ordinary diff34633464my($to_mode_oct,$to_mode_str,$to_file_type);3465my($from_mode_oct,$from_mode_str,$from_file_type);3466if($diff->{'to_mode'}ne('0' x 6)) {3467$to_mode_oct=oct$diff->{'to_mode'};3468if(S_ISREG($to_mode_oct)) {# only for regular file3469$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3470}3471$to_file_type= file_type($diff->{'to_mode'});3472}3473if($diff->{'from_mode'}ne('0' x 6)) {3474$from_mode_oct=oct$diff->{'from_mode'};3475if(S_ISREG($to_mode_oct)) {# only for regular file3476$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3477}3478$from_file_type= file_type($diff->{'from_mode'});3479}34803481if($diff->{'status'}eq"A") {# created3482my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3483$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3484$mode_chng.="]</span>";3485print"<td>";3486print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3487 hash_base=>$hash, file_name=>$diff->{'file'}),3488-class=>"list"}, esc_path($diff->{'file'}));3489print"</td>\n";3490print"<td>$mode_chng</td>\n";3491print"<td class=\"link\">";3492if($actioneq'commitdiff') {3493# link to patch3494$patchno++;3495print$cgi->a({-href =>"#patch$patchno"},"patch");3496print" | ";3497}3498print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3499 hash_base=>$hash, file_name=>$diff->{'file'})},3500"blob");3501print"</td>\n";35023503}elsif($diff->{'status'}eq"D") {# deleted3504my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3505print"<td>";3506print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3507 hash_base=>$parent, file_name=>$diff->{'file'}),3508-class=>"list"}, esc_path($diff->{'file'}));3509print"</td>\n";3510print"<td>$mode_chng</td>\n";3511print"<td class=\"link\">";3512if($actioneq'commitdiff') {3513# link to patch3514$patchno++;3515print$cgi->a({-href =>"#patch$patchno"},"patch");3516print" | ";3517}3518print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3519 hash_base=>$parent, file_name=>$diff->{'file'})},3520"blob") ." | ";3521if($have_blame) {3522print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3523 file_name=>$diff->{'file'})},3524"blame") ." | ";3525}3526print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3527 file_name=>$diff->{'file'})},3528"history");3529print"</td>\n";35303531}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3532my$mode_chnge="";3533if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3534$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3535if($from_file_typene$to_file_type) {3536$mode_chnge.=" from$from_file_typeto$to_file_type";3537}3538if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3539if($from_mode_str&&$to_mode_str) {3540$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3541}elsif($to_mode_str) {3542$mode_chnge.=" mode:$to_mode_str";3543}3544}3545$mode_chnge.="]</span>\n";3546}3547print"<td>";3548print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3549 hash_base=>$hash, file_name=>$diff->{'file'}),3550-class=>"list"}, esc_path($diff->{'file'}));3551print"</td>\n";3552print"<td>$mode_chnge</td>\n";3553print"<td class=\"link\">";3554if($actioneq'commitdiff') {3555# link to patch3556$patchno++;3557print$cgi->a({-href =>"#patch$patchno"},"patch") .3558" | ";3559}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3560# "commit" view and modified file (not onlu mode changed)3561print$cgi->a({-href => href(action=>"blobdiff",3562 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3563 hash_base=>$hash, hash_parent_base=>$parent,3564 file_name=>$diff->{'file'})},3565"diff") .3566" | ";3567}3568print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3569 hash_base=>$hash, file_name=>$diff->{'file'})},3570"blob") ." | ";3571if($have_blame) {3572print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3573 file_name=>$diff->{'file'})},3574"blame") ." | ";3575}3576print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3577 file_name=>$diff->{'file'})},3578"history");3579print"</td>\n";35803581}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3582my%status_name= ('R'=>'moved','C'=>'copied');3583my$nstatus=$status_name{$diff->{'status'}};3584my$mode_chng="";3585if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3586# mode also for directories, so we cannot use $to_mode_str3587$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3588}3589print"<td>".3590$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3591 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3592-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3593"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3594$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3595 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3596-class=>"list"}, esc_path($diff->{'from_file'})) .3597" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3598"<td class=\"link\">";3599if($actioneq'commitdiff') {3600# link to patch3601$patchno++;3602print$cgi->a({-href =>"#patch$patchno"},"patch") .3603" | ";3604}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3605# "commit" view and modified file (not only pure rename or copy)3606print$cgi->a({-href => href(action=>"blobdiff",3607 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3608 hash_base=>$hash, hash_parent_base=>$parent,3609 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3610"diff") .3611" | ";3612}3613print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3614 hash_base=>$parent, file_name=>$diff->{'to_file'})},3615"blob") ." | ";3616if($have_blame) {3617print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3618 file_name=>$diff->{'to_file'})},3619"blame") ." | ";3620}3621print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3622 file_name=>$diff->{'to_file'})},3623"history");3624print"</td>\n";36253626}# we should not encounter Unmerged (U) or Unknown (X) status3627print"</tr>\n";3628}3629print"</tbody>"if$has_header;3630print"</table>\n";3631}36323633sub git_patchset_body {3634my($fd,$difftree,$hash,@hash_parents) =@_;3635my($hash_parent) =$hash_parents[0];36363637my$is_combined= (@hash_parents>1);3638my$patch_idx=0;3639my$patch_number=0;3640my$patch_line;3641my$diffinfo;3642my$to_name;3643my(%from,%to);36443645print"<div class=\"patchset\">\n";36463647# skip to first patch3648while($patch_line= <$fd>) {3649chomp$patch_line;36503651last if($patch_line=~m/^diff /);3652}36533654 PATCH:3655while($patch_line) {36563657# parse "git diff" header line3658if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3659# $1 is from_name, which we do not use3660$to_name= unquote($2);3661$to_name=~s!^b/!!;3662}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3663# $1 is 'cc' or 'combined', which we do not use3664$to_name= unquote($2);3665}else{3666$to_name=undef;3667}36683669# check if current patch belong to current raw line3670# and parse raw git-diff line if needed3671if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3672# this is continuation of a split patch3673print"<div class=\"patch cont\">\n";3674}else{3675# advance raw git-diff output if needed3676$patch_idx++ifdefined$diffinfo;36773678# read and prepare patch information3679$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);36803681# compact combined diff output can have some patches skipped3682# find which patch (using pathname of result) we are at now;3683if($is_combined) {3684while($to_namene$diffinfo->{'to_file'}) {3685print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3686 format_diff_cc_simplified($diffinfo,@hash_parents) .3687"</div>\n";# class="patch"36883689$patch_idx++;3690$patch_number++;36913692last if$patch_idx>$#$difftree;3693$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3694}3695}36963697# modifies %from, %to hashes3698 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);36993700# this is first patch for raw difftree line with $patch_idx index3701# we index @$difftree array from 0, but number patches from 13702print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3703}37043705# git diff header3706#assert($patch_line =~ m/^diff /) if DEBUG;3707#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3708$patch_number++;3709# print "git diff" header3710print format_git_diff_header_line($patch_line,$diffinfo,3711 \%from, \%to);37123713# print extended diff header3714print"<div class=\"diff extended_header\">\n";3715 EXTENDED_HEADER:3716while($patch_line= <$fd>) {3717chomp$patch_line;37183719last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);37203721print format_extended_diff_header_line($patch_line,$diffinfo,3722 \%from, \%to);3723}3724print"</div>\n";# class="diff extended_header"37253726# from-file/to-file diff header3727if(!$patch_line) {3728print"</div>\n";# class="patch"3729last PATCH;3730}3731next PATCH if($patch_line=~m/^diff /);3732#assert($patch_line =~ m/^---/) if DEBUG;37333734my$last_patch_line=$patch_line;3735$patch_line= <$fd>;3736chomp$patch_line;3737#assert($patch_line =~ m/^\+\+\+/) if DEBUG;37383739print format_diff_from_to_header($last_patch_line,$patch_line,3740$diffinfo, \%from, \%to,3741@hash_parents);37423743# the patch itself3744 LINE:3745while($patch_line= <$fd>) {3746chomp$patch_line;37473748next PATCH if($patch_line=~m/^diff /);37493750print format_diff_line($patch_line, \%from, \%to);3751}37523753}continue{3754print"</div>\n";# class="patch"3755}37563757# for compact combined (--cc) format, with chunk and patch simpliciaction3758# patchset might be empty, but there might be unprocessed raw lines3759for(++$patch_idxif$patch_number>0;3760$patch_idx<@$difftree;3761++$patch_idx) {3762# read and prepare patch information3763$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);37643765# generate anchor for "patch" links in difftree / whatchanged part3766print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3767 format_diff_cc_simplified($diffinfo,@hash_parents) .3768"</div>\n";# class="patch"37693770$patch_number++;3771}37723773if($patch_number==0) {3774if(@hash_parents>1) {3775print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3776}else{3777print"<div class=\"diff nodifferences\">No differences found</div>\n";3778}3779}37803781print"</div>\n";# class="patchset"3782}37833784# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37853786# fills project list info (age, description, owner, forks) for each3787# project in the list, removing invalid projects from returned list3788# NOTE: modifies $projlist, but does not remove entries from it3789sub fill_project_list_info {3790my($projlist,$check_forks) =@_;3791my@projects;37923793my$show_ctags= gitweb_check_feature('ctags');3794 PROJECT:3795foreachmy$pr(@$projlist) {3796my(@activity) = git_get_last_activity($pr->{'path'});3797unless(@activity) {3798next PROJECT;3799}3800($pr->{'age'},$pr->{'age_string'}) =@activity;3801if(!defined$pr->{'descr'}) {3802my$descr= git_get_project_description($pr->{'path'}) ||"";3803$descr= to_utf8($descr);3804$pr->{'descr_long'} =$descr;3805$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3806}3807if(!defined$pr->{'owner'}) {3808$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3809}3810if($check_forks) {3811my$pname=$pr->{'path'};3812if(($pname=~s/\.git$//) &&3813($pname!~/\/$/) &&3814(-d "$projectroot/$pname")) {3815$pr->{'forks'} ="-d$projectroot/$pname";3816}else{3817$pr->{'forks'} =0;3818}3819}3820$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3821push@projects,$pr;3822}38233824return@projects;3825}38263827# print 'sort by' <th> element, generating 'sort by $name' replay link3828# if that order is not selected3829sub print_sort_th {3830my($name,$order,$header) =@_;3831$header||=ucfirst($name);38323833if($ordereq$name) {3834print"<th>$header</th>\n";3835}else{3836print"<th>".3837$cgi->a({-href => href(-replay=>1, order=>$name),3838-class=>"header"},$header) .3839"</th>\n";3840}3841}38423843sub git_project_list_body {3844# actually uses global variable $project3845my($projlist,$order,$from,$to,$extra,$no_header) =@_;38463847my($check_forks) = gitweb_check_feature('forks');3848my@projects= fill_project_list_info($projlist,$check_forks);38493850$order||=$default_projects_order;3851$from=0unlessdefined$from;3852$to=$#projectsif(!defined$to||$#projects<$to);38533854my%order_info= (3855 project => { key =>'path', type =>'str'},3856 descr => { key =>'descr_long', type =>'str'},3857 owner => { key =>'owner', type =>'str'},3858 age => { key =>'age', type =>'num'}3859);3860my$oi=$order_info{$order};3861if($oi->{'type'}eq'str') {3862@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;3863}else{3864@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;3865}38663867my$show_ctags= gitweb_check_feature('ctags');3868if($show_ctags) {3869my%ctags;3870foreachmy$p(@projects) {3871foreachmy$ct(keys%{$p->{'ctags'}}) {3872$ctags{$ct} +=$p->{'ctags'}->{$ct};3873}3874}3875my$cloud= git_populate_project_tagcloud(\%ctags);3876print git_show_project_tagcloud($cloud,64);3877}38783879print"<table class=\"project_list\">\n";3880unless($no_header) {3881print"<tr>\n";3882if($check_forks) {3883print"<th></th>\n";3884}3885 print_sort_th('project',$order,'Project');3886 print_sort_th('descr',$order,'Description');3887 print_sort_th('owner',$order,'Owner');3888 print_sort_th('age',$order,'Last Change');3889print"<th></th>\n".# for links3890"</tr>\n";3891}3892my$alternate=1;3893my$tagfilter=$cgi->param('by_tag');3894for(my$i=$from;$i<=$to;$i++) {3895my$pr=$projects[$i];38963897next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};3898next if$searchtextand not$pr->{'path'} =~/$searchtext/3899and not$pr->{'descr_long'} =~/$searchtext/;3900# Weed out forks or non-matching entries of search3901if($check_forks) {3902my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;3903$forkbase="^$forkbase"if$forkbase;3904next ifnot$searchtextand not$tagfilterand$show_ctags3905and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe3906}39073908if($alternate) {3909print"<tr class=\"dark\">\n";3910}else{3911print"<tr class=\"light\">\n";3912}3913$alternate^=1;3914if($check_forks) {3915print"<td>";3916if($pr->{'forks'}) {3917print"<!--$pr->{'forks'} -->\n";3918print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");3919}3920print"</td>\n";3921}3922print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3923-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".3924"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3925-class=>"list", -title =>$pr->{'descr_long'}},3926 esc_html($pr->{'descr'})) ."</td>\n".3927"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";3928print"<td class=\"". age_class($pr->{'age'}) ."\">".3929(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".3930"<td class=\"link\">".3931$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".3932$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".3933$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".3934$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .3935($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .3936"</td>\n".3937"</tr>\n";3938}3939if(defined$extra) {3940print"<tr>\n";3941if($check_forks) {3942print"<td></td>\n";3943}3944print"<td colspan=\"5\">$extra</td>\n".3945"</tr>\n";3946}3947print"</table>\n";3948}39493950sub git_shortlog_body {3951# uses global variable $project3952my($commitlist,$from,$to,$refs,$extra) =@_;39533954$from=0unlessdefined$from;3955$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);39563957print"<table class=\"shortlog\">\n";3958my$alternate=1;3959for(my$i=$from;$i<=$to;$i++) {3960my%co= %{$commitlist->[$i]};3961my$commit=$co{'id'};3962my$ref= format_ref_marker($refs,$commit);3963if($alternate) {3964print"<tr class=\"dark\">\n";3965}else{3966print"<tr class=\"light\">\n";3967}3968$alternate^=1;3969my$author= chop_and_escape_str($co{'author_name'},10);3970# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .3971print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".3972"<td><i>".$author."</i></td>\n".3973"<td>";3974print format_subject_html($co{'title'},$co{'title_short'},3975 href(action=>"commit", hash=>$commit),$ref);3976print"</td>\n".3977"<td class=\"link\">".3978$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".3979$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".3980$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");3981my$snapshot_links= format_snapshot_links($commit);3982if(defined$snapshot_links) {3983print" | ".$snapshot_links;3984}3985print"</td>\n".3986"</tr>\n";3987}3988if(defined$extra) {3989print"<tr>\n".3990"<td colspan=\"4\">$extra</td>\n".3991"</tr>\n";3992}3993print"</table>\n";3994}39953996sub git_history_body {3997# Warning: assumes constant type (blob or tree) during history3998my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;39994000$from=0unlessdefined$from;4001$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});40024003print"<table class=\"history\">\n";4004my$alternate=1;4005for(my$i=$from;$i<=$to;$i++) {4006my%co= %{$commitlist->[$i]};4007if(!%co) {4008next;4009}4010my$commit=$co{'id'};40114012my$ref= format_ref_marker($refs,$commit);40134014if($alternate) {4015print"<tr class=\"dark\">\n";4016}else{4017print"<tr class=\"light\">\n";4018}4019$alternate^=1;4020# shortlog uses chop_str($co{'author_name'}, 10)4021my$author= chop_and_escape_str($co{'author_name'},15,3);4022print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4023"<td><i>".$author."</i></td>\n".4024"<td>";4025# originally git_history used chop_str($co{'title'}, 50)4026print format_subject_html($co{'title'},$co{'title_short'},4027 href(action=>"commit", hash=>$commit),$ref);4028print"</td>\n".4029"<td class=\"link\">".4030$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4031$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");40324033if($ftypeeq'blob') {4034my$blob_current= git_get_hash_by_path($hash_base,$file_name);4035my$blob_parent= git_get_hash_by_path($commit,$file_name);4036if(defined$blob_current&&defined$blob_parent&&4037$blob_currentne$blob_parent) {4038print" | ".4039$cgi->a({-href => href(action=>"blobdiff",4040 hash=>$blob_current, hash_parent=>$blob_parent,4041 hash_base=>$hash_base, hash_parent_base=>$commit,4042 file_name=>$file_name)},4043"diff to current");4044}4045}4046print"</td>\n".4047"</tr>\n";4048}4049if(defined$extra) {4050print"<tr>\n".4051"<td colspan=\"4\">$extra</td>\n".4052"</tr>\n";4053}4054print"</table>\n";4055}40564057sub git_tags_body {4058# uses global variable $project4059my($taglist,$from,$to,$extra) =@_;4060$from=0unlessdefined$from;4061$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);40624063print"<table class=\"tags\">\n";4064my$alternate=1;4065for(my$i=$from;$i<=$to;$i++) {4066my$entry=$taglist->[$i];4067my%tag=%$entry;4068my$comment=$tag{'subject'};4069my$comment_short;4070if(defined$comment) {4071$comment_short= chop_str($comment,30,5);4072}4073if($alternate) {4074print"<tr class=\"dark\">\n";4075}else{4076print"<tr class=\"light\">\n";4077}4078$alternate^=1;4079if(defined$tag{'age'}) {4080print"<td><i>$tag{'age'}</i></td>\n";4081}else{4082print"<td></td>\n";4083}4084print"<td>".4085$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4086-class=>"list name"}, esc_html($tag{'name'})) .4087"</td>\n".4088"<td>";4089if(defined$comment) {4090print format_subject_html($comment,$comment_short,4091 href(action=>"tag", hash=>$tag{'id'}));4092}4093print"</td>\n".4094"<td class=\"selflink\">";4095if($tag{'type'}eq"tag") {4096print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4097}else{4098print" ";4099}4100print"</td>\n".4101"<td class=\"link\">"." | ".4102$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4103if($tag{'reftype'}eq"commit") {4104print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4105" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4106}elsif($tag{'reftype'}eq"blob") {4107print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4108}4109print"</td>\n".4110"</tr>";4111}4112if(defined$extra) {4113print"<tr>\n".4114"<td colspan=\"5\">$extra</td>\n".4115"</tr>\n";4116}4117print"</table>\n";4118}41194120sub git_heads_body {4121# uses global variable $project4122my($headlist,$head,$from,$to,$extra) =@_;4123$from=0unlessdefined$from;4124$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);41254126print"<table class=\"heads\">\n";4127my$alternate=1;4128for(my$i=$from;$i<=$to;$i++) {4129my$entry=$headlist->[$i];4130my%ref=%$entry;4131my$curr=$ref{'id'}eq$head;4132if($alternate) {4133print"<tr class=\"dark\">\n";4134}else{4135print"<tr class=\"light\">\n";4136}4137$alternate^=1;4138print"<td><i>$ref{'age'}</i></td>\n".4139($curr?"<td class=\"current_head\">":"<td>") .4140$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4141-class=>"list name"},esc_html($ref{'name'})) .4142"</td>\n".4143"<td class=\"link\">".4144$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4145$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4146$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4147"</td>\n".4148"</tr>";4149}4150if(defined$extra) {4151print"<tr>\n".4152"<td colspan=\"3\">$extra</td>\n".4153"</tr>\n";4154}4155print"</table>\n";4156}41574158sub git_search_grep_body {4159my($commitlist,$from,$to,$extra) =@_;4160$from=0unlessdefined$from;4161$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41624163print"<table class=\"commit_search\">\n";4164my$alternate=1;4165for(my$i=$from;$i<=$to;$i++) {4166my%co= %{$commitlist->[$i]};4167if(!%co) {4168next;4169}4170my$commit=$co{'id'};4171if($alternate) {4172print"<tr class=\"dark\">\n";4173}else{4174print"<tr class=\"light\">\n";4175}4176$alternate^=1;4177my$author= chop_and_escape_str($co{'author_name'},15,5);4178print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4179"<td><i>".$author."</i></td>\n".4180"<td>".4181$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4182-class=>"list subject"},4183 chop_and_escape_str($co{'title'},50) ."<br/>");4184my$comment=$co{'comment'};4185foreachmy$line(@$comment) {4186if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4187my($lead,$match,$trail) = ($1,$2,$3);4188$match= chop_str($match,70,5,'center');4189my$contextlen=int((80-length($match))/2);4190$contextlen=30if($contextlen>30);4191$lead= chop_str($lead,$contextlen,10,'left');4192$trail= chop_str($trail,$contextlen,10,'right');41934194$lead= esc_html($lead);4195$match= esc_html($match);4196$trail= esc_html($trail);41974198print"$lead<span class=\"match\">$match</span>$trail<br />";4199}4200}4201print"</td>\n".4202"<td class=\"link\">".4203$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4204" | ".4205$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4206" | ".4207$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4208print"</td>\n".4209"</tr>\n";4210}4211if(defined$extra) {4212print"<tr>\n".4213"<td colspan=\"3\">$extra</td>\n".4214"</tr>\n";4215}4216print"</table>\n";4217}42184219## ======================================================================4220## ======================================================================4221## actions42224223sub git_project_list {4224my$order=$input_params{'order'};4225if(defined$order&&$order!~m/none|project|descr|owner|age/) {4226 die_error(400,"Unknown order parameter");4227}42284229my@list= git_get_projects_list();4230if(!@list) {4231 die_error(404,"No projects found");4232}42334234 git_header_html();4235if(-f $home_text) {4236print"<div class=\"index_include\">\n";4237open(my$fd,$home_text);4238print<$fd>;4239close$fd;4240print"</div>\n";4241}4242print$cgi->startform(-method=>"get") .4243"<p class=\"projsearch\">Search:\n".4244$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4245"</p>".4246$cgi->end_form() ."\n";4247 git_project_list_body(\@list,$order);4248 git_footer_html();4249}42504251sub git_forks {4252my$order=$input_params{'order'};4253if(defined$order&&$order!~m/none|project|descr|owner|age/) {4254 die_error(400,"Unknown order parameter");4255}42564257my@list= git_get_projects_list($project);4258if(!@list) {4259 die_error(404,"No forks found");4260}42614262 git_header_html();4263 git_print_page_nav('','');4264 git_print_header_div('summary',"$projectforks");4265 git_project_list_body(\@list,$order);4266 git_footer_html();4267}42684269sub git_project_index {4270my@projects= git_get_projects_list($project);42714272print$cgi->header(4273-type =>'text/plain',4274-charset =>'utf-8',4275-content_disposition =>'inline; filename="index.aux"');42764277foreachmy$pr(@projects) {4278if(!exists$pr->{'owner'}) {4279$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4280}42814282my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4283# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4284$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4285$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4286$path=~s/ /\+/g;4287$owner=~s/ /\+/g;42884289print"$path$owner\n";4290}4291}42924293sub git_summary {4294my$descr= git_get_project_description($project) ||"none";4295my%co= parse_commit("HEAD");4296my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4297my$head=$co{'id'};42984299my$owner= git_get_project_owner($project);43004301my$refs= git_get_references();4302# These get_*_list functions return one more to allow us to see if4303# there are more ...4304my@taglist= git_get_tags_list(16);4305my@headlist= git_get_heads_list(16);4306my@forklist;4307my($check_forks) = gitweb_check_feature('forks');43084309if($check_forks) {4310@forklist= git_get_projects_list($project);4311}43124313 git_header_html();4314 git_print_page_nav('summary','',$head);43154316print"<div class=\"title\"> </div>\n";4317print"<table class=\"projects_list\">\n".4318"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4319"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4320if(defined$cd{'rfc2822'}) {4321print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4322}43234324# use per project git URL list in $projectroot/$project/cloneurl4325# or make project git URL from git base URL and project name4326my$url_tag="URL";4327my@url_list= git_get_project_url_list($project);4328@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4329foreachmy$git_url(@url_list) {4330next unless$git_url;4331print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4332$url_tag="";4333}43344335# Tag cloud4336my$show_ctags= (gitweb_check_feature('ctags'))[0];4337if($show_ctags) {4338my$ctags= git_get_project_ctags($project);4339my$cloud= git_populate_project_tagcloud($ctags);4340print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4341print"</td>\n<td>"unless%$ctags;4342print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4343print"</td>\n<td>"if%$ctags;4344print git_show_project_tagcloud($cloud,48);4345print"</td></tr>";4346}43474348print"</table>\n";43494350if(-s "$projectroot/$project/README.html") {4351if(open my$fd,"$projectroot/$project/README.html") {4352print"<div class=\"title\">readme</div>\n".4353"<div class=\"readme\">\n";4354print$_while(<$fd>);4355print"\n</div>\n";# class="readme"4356close$fd;4357}4358}43594360# we need to request one more than 16 (0..15) to check if4361# those 16 are all4362my@commitlist=$head? parse_commits($head,17) : ();4363if(@commitlist) {4364 git_print_header_div('shortlog');4365 git_shortlog_body(\@commitlist,0,15,$refs,4366$#commitlist<=15?undef:4367$cgi->a({-href => href(action=>"shortlog")},"..."));4368}43694370if(@taglist) {4371 git_print_header_div('tags');4372 git_tags_body(\@taglist,0,15,4373$#taglist<=15?undef:4374$cgi->a({-href => href(action=>"tags")},"..."));4375}43764377if(@headlist) {4378 git_print_header_div('heads');4379 git_heads_body(\@headlist,$head,0,15,4380$#headlist<=15?undef:4381$cgi->a({-href => href(action=>"heads")},"..."));4382}43834384if(@forklist) {4385 git_print_header_div('forks');4386 git_project_list_body(\@forklist,'age',0,15,4387$#forklist<=15?undef:4388$cgi->a({-href => href(action=>"forks")},"..."),4389'no_header');4390}43914392 git_footer_html();4393}43944395sub git_tag {4396my$head= git_get_head_hash($project);4397 git_header_html();4398 git_print_page_nav('','',$head,undef,$head);4399my%tag= parse_tag($hash);44004401if(!%tag) {4402 die_error(404,"Unknown tag object");4403}44044405 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4406print"<div class=\"title_text\">\n".4407"<table class=\"object_header\">\n".4408"<tr>\n".4409"<td>object</td>\n".4410"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4411$tag{'object'}) ."</td>\n".4412"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4413$tag{'type'}) ."</td>\n".4414"</tr>\n";4415if(defined($tag{'author'})) {4416my%ad= parse_date($tag{'epoch'},$tag{'tz'});4417print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4418print"<tr><td></td><td>".$ad{'rfc2822'} .4419sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4420"</td></tr>\n";4421}4422print"</table>\n\n".4423"</div>\n";4424print"<div class=\"page_body\">";4425my$comment=$tag{'comment'};4426foreachmy$line(@$comment) {4427chomp$line;4428print esc_html($line, -nbsp=>1) ."<br/>\n";4429}4430print"</div>\n";4431 git_footer_html();4432}44334434sub git_blame {4435my$fd;4436my$ftype;44374438 gitweb_check_feature('blame')4439or die_error(403,"Blame view not allowed");44404441 die_error(400,"No file name given")unless$file_name;4442$hash_base||= git_get_head_hash($project);4443 die_error(404,"Couldn't find base commit")unless($hash_base);4444my%co= parse_commit($hash_base)4445or die_error(404,"Commit not found");4446if(!defined$hash) {4447$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4448or die_error(404,"Error looking up file");4449}4450$ftype= git_get_type($hash);4451if($ftype!~"blob") {4452 die_error(400,"Object is not a blob");4453}4454open($fd,"-|", git_cmd(),"blame",'-p','--',4455$file_name,$hash_base)4456or die_error(500,"Open git-blame failed");4457 git_header_html();4458my$formats_nav=4459$cgi->a({-href => href(action=>"blob", -replay=>1)},4460"blob") .4461" | ".4462$cgi->a({-href => href(action=>"history", -replay=>1)},4463"history") .4464" | ".4465$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4466"HEAD");4467 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4468 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4469 git_print_page_path($file_name,$ftype,$hash_base);4470my@rev_color= (qw(light2 dark2));4471my$num_colors=scalar(@rev_color);4472my$current_color=0;4473my$last_rev;4474print<<HTML;4475<div class="page_body">4476<table class="blame">4477<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4478HTML4479my%metainfo= ();4480while(1) {4481$_= <$fd>;4482last unlessdefined$_;4483my($full_rev,$orig_lineno,$lineno,$group_size) =4484/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;4485if(!exists$metainfo{$full_rev}) {4486$metainfo{$full_rev} = {};4487}4488my$meta=$metainfo{$full_rev};4489while(<$fd>) {4490last if(s/^\t//);4491if(/^(\S+) (.*)$/) {4492$meta->{$1} =$2;4493}4494}4495my$data=$_;4496chomp$data;4497my$rev=substr($full_rev,0,8);4498my$author=$meta->{'author'};4499my%date= parse_date($meta->{'author-time'},4500$meta->{'author-tz'});4501my$date=$date{'iso-tz'};4502if($group_size) {4503$current_color= ++$current_color%$num_colors;4504}4505print"<tr class=\"$rev_color[$current_color]\">\n";4506if($group_size) {4507print"<td class=\"sha1\"";4508print" title=\"". esc_html($author) .",$date\"";4509print" rowspan=\"$group_size\""if($group_size>1);4510print">";4511print$cgi->a({-href => href(action=>"commit",4512 hash=>$full_rev,4513 file_name=>$file_name)},4514 esc_html($rev));4515print"</td>\n";4516}4517open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4518or die_error(500,"Open git-rev-parse failed");4519my$parent_commit= <$dd>;4520close$dd;4521chomp($parent_commit);4522my$blamed= href(action =>'blame',4523 file_name =>$meta->{'filename'},4524 hash_base =>$parent_commit);4525print"<td class=\"linenr\">";4526print$cgi->a({ -href =>"$blamed#l$orig_lineno",4527-id =>"l$lineno",4528-class=>"linenr"},4529 esc_html($lineno));4530print"</td>";4531print"<td class=\"pre\">". esc_html($data) ."</td>\n";4532print"</tr>\n";4533}4534print"</table>\n";4535print"</div>";4536close$fd4537or print"Reading blob failed\n";4538 git_footer_html();4539}45404541sub git_tags {4542my$head= git_get_head_hash($project);4543 git_header_html();4544 git_print_page_nav('','',$head,undef,$head);4545 git_print_header_div('summary',$project);45464547my@tagslist= git_get_tags_list();4548if(@tagslist) {4549 git_tags_body(\@tagslist);4550}4551 git_footer_html();4552}45534554sub git_heads {4555my$head= git_get_head_hash($project);4556 git_header_html();4557 git_print_page_nav('','',$head,undef,$head);4558 git_print_header_div('summary',$project);45594560my@headslist= git_get_heads_list();4561if(@headslist) {4562 git_heads_body(\@headslist,$head);4563}4564 git_footer_html();4565}45664567sub git_blob_plain {4568my$type=shift;4569my$expires;45704571if(!defined$hash) {4572if(defined$file_name) {4573my$base=$hash_base|| git_get_head_hash($project);4574$hash= git_get_hash_by_path($base,$file_name,"blob")4575or die_error(404,"Cannot find file");4576}else{4577 die_error(400,"No file name defined");4578}4579}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4580# blobs defined by non-textual hash id's can be cached4581$expires="+1d";4582}45834584open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4585or die_error(500,"Open git-cat-file blob '$hash' failed");45864587# content-type (can include charset)4588$type= blob_contenttype($fd,$file_name,$type);45894590# "save as" filename, even when no $file_name is given4591my$save_as="$hash";4592if(defined$file_name) {4593$save_as=$file_name;4594}elsif($type=~m/^text\//) {4595$save_as.='.txt';4596}45974598print$cgi->header(4599-type =>$type,4600-expires =>$expires,4601-content_disposition =>'inline; filename="'.$save_as.'"');4602undef$/;4603binmode STDOUT,':raw';4604print<$fd>;4605binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4606$/="\n";4607close$fd;4608}46094610sub git_blob {4611my$expires;46124613if(!defined$hash) {4614if(defined$file_name) {4615my$base=$hash_base|| git_get_head_hash($project);4616$hash= git_get_hash_by_path($base,$file_name,"blob")4617or die_error(404,"Cannot find file");4618}else{4619 die_error(400,"No file name defined");4620}4621}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4622# blobs defined by non-textual hash id's can be cached4623$expires="+1d";4624}46254626my($have_blame) = gitweb_check_feature('blame');4627open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4628or die_error(500,"Couldn't cat$file_name,$hash");4629my$mimetype= blob_mimetype($fd,$file_name);4630if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4631close$fd;4632return git_blob_plain($mimetype);4633}4634# we can have blame only for text/* mimetype4635$have_blame&&= ($mimetype=~m!^text/!);46364637 git_header_html(undef,$expires);4638my$formats_nav='';4639if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4640if(defined$file_name) {4641if($have_blame) {4642$formats_nav.=4643$cgi->a({-href => href(action=>"blame", -replay=>1)},4644"blame") .4645" | ";4646}4647$formats_nav.=4648$cgi->a({-href => href(action=>"history", -replay=>1)},4649"history") .4650" | ".4651$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4652"raw") .4653" | ".4654$cgi->a({-href => href(action=>"blob",4655 hash_base=>"HEAD", file_name=>$file_name)},4656"HEAD");4657}else{4658$formats_nav.=4659$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4660"raw");4661}4662 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4663 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4664}else{4665print"<div class=\"page_nav\">\n".4666"<br/><br/></div>\n".4667"<div class=\"title\">$hash</div>\n";4668}4669 git_print_page_path($file_name,"blob",$hash_base);4670print"<div class=\"page_body\">\n";4671if($mimetype=~m!^image/!) {4672print qq!<img type="$mimetype"!;4673if($file_name) {4674print qq! alt="$file_name" title="$file_name"!;4675}4676print qq! src="! .4677 href(action=>"blob_plain", hash=>$hash,4678 hash_base=>$hash_base, file_name=>$file_name) .4679 qq!"/>\n!;4680}else{4681my$nr;4682while(my$line= <$fd>) {4683chomp$line;4684$nr++;4685$line= untabify($line);4686printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4687$nr,$nr,$nr, esc_html($line, -nbsp=>1);4688}4689}4690close$fd4691or print"Reading blob failed.\n";4692print"</div>";4693 git_footer_html();4694}46954696sub git_tree {4697if(!defined$hash_base) {4698$hash_base="HEAD";4699}4700if(!defined$hash) {4701if(defined$file_name) {4702$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4703}else{4704$hash=$hash_base;4705}4706}4707 die_error(404,"No such tree")unlessdefined($hash);4708$/="\0";4709open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4710or die_error(500,"Open git-ls-tree failed");4711my@entries=map{chomp;$_} <$fd>;4712close$fdor die_error(404,"Reading tree failed");4713$/="\n";47144715my$refs= git_get_references();4716my$ref= format_ref_marker($refs,$hash_base);4717 git_header_html();4718my$basedir='';4719my($have_blame) = gitweb_check_feature('blame');4720if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4721my@views_nav= ();4722if(defined$file_name) {4723push@views_nav,4724$cgi->a({-href => href(action=>"history", -replay=>1)},4725"history"),4726$cgi->a({-href => href(action=>"tree",4727 hash_base=>"HEAD", file_name=>$file_name)},4728"HEAD"),4729}4730my$snapshot_links= format_snapshot_links($hash);4731if(defined$snapshot_links) {4732# FIXME: Should be available when we have no hash base as well.4733push@views_nav,$snapshot_links;4734}4735 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4736 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4737}else{4738undef$hash_base;4739print"<div class=\"page_nav\">\n";4740print"<br/><br/></div>\n";4741print"<div class=\"title\">$hash</div>\n";4742}4743if(defined$file_name) {4744$basedir=$file_name;4745if($basedirne''&&substr($basedir, -1)ne'/') {4746$basedir.='/';4747}4748 git_print_page_path($file_name,'tree',$hash_base);4749}4750print"<div class=\"page_body\">\n";4751print"<table class=\"tree\">\n";4752my$alternate=1;4753# '..' (top directory) link if possible4754if(defined$hash_base&&4755defined$file_name&&$file_name=~m![^/]+$!) {4756if($alternate) {4757print"<tr class=\"dark\">\n";4758}else{4759print"<tr class=\"light\">\n";4760}4761$alternate^=1;47624763my$up=$file_name;4764$up=~s!/?[^/]+$!!;4765undef$upunless$up;4766# based on git_print_tree_entry4767print'<td class="mode">'. mode_str('040000') ."</td>\n";4768print'<td class="list">';4769print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4770 file_name=>$up)},4771"..");4772print"</td>\n";4773print"<td class=\"link\"></td>\n";47744775print"</tr>\n";4776}4777foreachmy$line(@entries) {4778my%t= parse_ls_tree_line($line, -z =>1);47794780if($alternate) {4781print"<tr class=\"dark\">\n";4782}else{4783print"<tr class=\"light\">\n";4784}4785$alternate^=1;47864787 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);47884789print"</tr>\n";4790}4791print"</table>\n".4792"</div>";4793 git_footer_html();4794}47954796sub git_snapshot {4797my@supported_fmts= gitweb_check_feature('snapshot');4798@supported_fmts= filter_snapshot_fmts(@supported_fmts);47994800my$format=$input_params{'snapshot_format'};4801if(!@supported_fmts) {4802 die_error(403,"Snapshots not allowed");4803}4804# default to first supported snapshot format4805$format||=$supported_fmts[0];4806if($format!~m/^[a-z0-9]+$/) {4807 die_error(400,"Invalid snapshot format parameter");4808}elsif(!exists($known_snapshot_formats{$format})) {4809 die_error(400,"Unknown snapshot format");4810}elsif(!grep($_eq$format,@supported_fmts)) {4811 die_error(403,"Unsupported snapshot format");4812}48134814if(!defined$hash) {4815$hash= git_get_head_hash($project);4816}48174818my$name=$project;4819$name=~ s,([^/])/*\.git$,$1,;4820$name= basename($name);4821my$filename= to_utf8($name);4822$name=~s/\047/\047\\\047\047/g;4823my$cmd;4824$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";4825$cmd= quote_command(4826 git_cmd(),'archive',4827"--format=$known_snapshot_formats{$format}{'format'}",4828"--prefix=$name/",$hash);4829if(exists$known_snapshot_formats{$format}{'compressor'}) {4830$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});4831}48324833print$cgi->header(4834-type =>$known_snapshot_formats{$format}{'type'},4835-content_disposition =>'inline; filename="'."$filename".'"',4836-status =>'200 OK');48374838open my$fd,"-|",$cmd4839or die_error(500,"Execute git-archive failed");4840binmode STDOUT,':raw';4841print<$fd>;4842binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4843close$fd;4844}48454846sub git_log {4847my$head= git_get_head_hash($project);4848if(!defined$hash) {4849$hash=$head;4850}4851if(!defined$page) {4852$page=0;4853}4854my$refs= git_get_references();48554856my@commitlist= parse_commits($hash,101, (100*$page));48574858my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);48594860 git_header_html();4861 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);48624863if(!@commitlist) {4864my%co= parse_commit($hash);48654866 git_print_header_div('summary',$project);4867print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";4868}4869my$to= ($#commitlist>=99) ? (99) : ($#commitlist);4870for(my$i=0;$i<=$to;$i++) {4871my%co= %{$commitlist[$i]};4872next if!%co;4873my$commit=$co{'id'};4874my$ref= format_ref_marker($refs,$commit);4875my%ad= parse_date($co{'author_epoch'});4876 git_print_header_div('commit',4877"<span class=\"age\">$co{'age_string'}</span>".4878 esc_html($co{'title'}) .$ref,4879$commit);4880print"<div class=\"title_text\">\n".4881"<div class=\"log_link\">\n".4882$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4883" | ".4884$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4885" | ".4886$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4887"<br/>\n".4888"</div>\n".4889"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".4890"</div>\n";48914892print"<div class=\"log_body\">\n";4893 git_print_log($co{'comment'}, -final_empty_line=>1);4894print"</div>\n";4895}4896if($#commitlist>=100) {4897print"<div class=\"page_nav\">\n";4898print$cgi->a({-href => href(-replay=>1, page=>$page+1),4899-accesskey =>"n", -title =>"Alt-n"},"next");4900print"</div>\n";4901}4902 git_footer_html();4903}49044905sub git_commit {4906$hash||=$hash_base||"HEAD";4907my%co= parse_commit($hash)4908or die_error(404,"Unknown commit object");4909my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});4910my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});49114912my$parent=$co{'parent'};4913my$parents=$co{'parents'};# listref49144915# we need to prepare $formats_nav before any parameter munging4916my$formats_nav;4917if(!defined$parent) {4918# --root commitdiff4919$formats_nav.='(initial)';4920}elsif(@$parents==1) {4921# single parent commit4922$formats_nav.=4923'(parent: '.4924$cgi->a({-href => href(action=>"commit",4925 hash=>$parent)},4926 esc_html(substr($parent,0,7))) .4927')';4928}else{4929# merge commit4930$formats_nav.=4931'(merge: '.4932join(' ',map{4933$cgi->a({-href => href(action=>"commit",4934 hash=>$_)},4935 esc_html(substr($_,0,7)));4936}@$parents) .4937')';4938}49394940if(!defined$parent) {4941$parent="--root";4942}4943my@difftree;4944open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",4945@diff_opts,4946(@$parents<=1?$parent:'-c'),4947$hash,"--"4948or die_error(500,"Open git-diff-tree failed");4949@difftree=map{chomp;$_} <$fd>;4950close$fdor die_error(404,"Reading git-diff-tree failed");49514952# non-textual hash id's can be cached4953my$expires;4954if($hash=~m/^[0-9a-fA-F]{40}$/) {4955$expires="+1d";4956}4957my$refs= git_get_references();4958my$ref= format_ref_marker($refs,$co{'id'});49594960 git_header_html(undef,$expires);4961 git_print_page_nav('commit','',4962$hash,$co{'tree'},$hash,4963$formats_nav);49644965if(defined$co{'parent'}) {4966 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);4967}else{4968 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);4969}4970print"<div class=\"title_text\">\n".4971"<table class=\"object_header\">\n";4972print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".4973"<tr>".4974"<td></td><td>$ad{'rfc2822'}";4975if($ad{'hour_local'} <6) {4976printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",4977$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});4978}else{4979printf(" (%02d:%02d%s)",4980$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});4981}4982print"</td>".4983"</tr>\n";4984print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";4985print"<tr><td></td><td>$cd{'rfc2822'}".4986sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .4987"</td></tr>\n";4988print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";4989print"<tr>".4990"<td>tree</td>".4991"<td class=\"sha1\">".4992$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),4993class=>"list"},$co{'tree'}) .4994"</td>".4995"<td class=\"link\">".4996$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},4997"tree");4998my$snapshot_links= format_snapshot_links($hash);4999if(defined$snapshot_links) {5000print" | ".$snapshot_links;5001}5002print"</td>".5003"</tr>\n";50045005foreachmy$par(@$parents) {5006print"<tr>".5007"<td>parent</td>".5008"<td class=\"sha1\">".5009$cgi->a({-href => href(action=>"commit", hash=>$par),5010class=>"list"},$par) .5011"</td>".5012"<td class=\"link\">".5013$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5014" | ".5015$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5016"</td>".5017"</tr>\n";5018}5019print"</table>".5020"</div>\n";50215022print"<div class=\"page_body\">\n";5023 git_print_log($co{'comment'});5024print"</div>\n";50255026 git_difftree_body(\@difftree,$hash,@$parents);50275028 git_footer_html();5029}50305031sub git_object {5032# object is defined by:5033# - hash or hash_base alone5034# - hash_base and file_name5035my$type;50365037# - hash or hash_base alone5038if($hash|| ($hash_base&& !defined$file_name)) {5039my$object_id=$hash||$hash_base;50405041open my$fd,"-|", quote_command(5042 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5043or die_error(404,"Object does not exist");5044$type= <$fd>;5045chomp$type;5046close$fd5047or die_error(404,"Object does not exist");50485049# - hash_base and file_name5050}elsif($hash_base&&defined$file_name) {5051$file_name=~ s,/+$,,;50525053system(git_cmd(),"cat-file",'-e',$hash_base) ==05054or die_error(404,"Base object does not exist");50555056# here errors should not hapen5057open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5058or die_error(500,"Open git-ls-tree failed");5059my$line= <$fd>;5060close$fd;50615062#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5063unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5064 die_error(404,"File or directory for given base does not exist");5065}5066$type=$2;5067$hash=$3;5068}else{5069 die_error(400,"Not enough information to find object");5070}50715072print$cgi->redirect(-uri => href(action=>$type, -full=>1,5073 hash=>$hash, hash_base=>$hash_base,5074 file_name=>$file_name),5075-status =>'302 Found');5076}50775078sub git_blobdiff {5079my$format=shift||'html';50805081my$fd;5082my@difftree;5083my%diffinfo;5084my$expires;50855086# preparing $fd and %diffinfo for git_patchset_body5087# new style URI5088if(defined$hash_base&&defined$hash_parent_base) {5089if(defined$file_name) {5090# read raw output5091open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5092$hash_parent_base,$hash_base,5093"--", (defined$file_parent?$file_parent: ()),$file_name5094or die_error(500,"Open git-diff-tree failed");5095@difftree=map{chomp;$_} <$fd>;5096close$fd5097or die_error(404,"Reading git-diff-tree failed");5098@difftree5099or die_error(404,"Blob diff not found");51005101}elsif(defined$hash&&5102$hash=~/[0-9a-fA-F]{40}/) {5103# try to find filename from $hash51045105# read filtered raw output5106open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5107$hash_parent_base,$hash_base,"--"5108or die_error(500,"Open git-diff-tree failed");5109@difftree=5110# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5111# $hash == to_id5112grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5113map{chomp;$_} <$fd>;5114close$fd5115or die_error(404,"Reading git-diff-tree failed");5116@difftree5117or die_error(404,"Blob diff not found");51185119}else{5120 die_error(400,"Missing one of the blob diff parameters");5121}51225123if(@difftree>1) {5124 die_error(400,"Ambiguous blob diff specification");5125}51265127%diffinfo= parse_difftree_raw_line($difftree[0]);5128$file_parent||=$diffinfo{'from_file'} ||$file_name;5129$file_name||=$diffinfo{'to_file'};51305131$hash_parent||=$diffinfo{'from_id'};5132$hash||=$diffinfo{'to_id'};51335134# non-textual hash id's can be cached5135if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5136$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5137$expires='+1d';5138}51395140# open patch output5141open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5142'-p', ($formateq'html'?"--full-index": ()),5143$hash_parent_base,$hash_base,5144"--", (defined$file_parent?$file_parent: ()),$file_name5145or die_error(500,"Open git-diff-tree failed");5146}51475148# old/legacy style URI5149if(!%diffinfo&&# if new style URI failed5150defined$hash&&defined$hash_parent) {5151# fake git-diff-tree raw output5152$diffinfo{'from_mode'} =$diffinfo{'to_mode'} ="blob";5153$diffinfo{'from_id'} =$hash_parent;5154$diffinfo{'to_id'} =$hash;5155if(defined$file_name) {5156if(defined$file_parent) {5157$diffinfo{'status'} ='2';5158$diffinfo{'from_file'} =$file_parent;5159$diffinfo{'to_file'} =$file_name;5160}else{# assume not renamed5161$diffinfo{'status'} ='1';5162$diffinfo{'from_file'} =$file_name;5163$diffinfo{'to_file'} =$file_name;5164}5165}else{# no filename given5166$diffinfo{'status'} ='2';5167$diffinfo{'from_file'} =$hash_parent;5168$diffinfo{'to_file'} =$hash;5169}51705171# non-textual hash id's can be cached5172if($hash=~m/^[0-9a-fA-F]{40}$/&&5173$hash_parent=~m/^[0-9a-fA-F]{40}$/) {5174$expires='+1d';5175}51765177# open patch output5178open$fd,"-|", git_cmd(),"diff",@diff_opts,5179'-p', ($formateq'html'?"--full-index": ()),5180$hash_parent,$hash,"--"5181or die_error(500,"Open git-diff failed");5182}else{5183 die_error(400,"Missing one of the blob diff parameters")5184unless%diffinfo;5185}51865187# header5188if($formateq'html') {5189my$formats_nav=5190$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5191"raw");5192 git_header_html(undef,$expires);5193if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5194 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5195 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5196}else{5197print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5198print"<div class=\"title\">$hashvs$hash_parent</div>\n";5199}5200if(defined$file_name) {5201 git_print_page_path($file_name,"blob",$hash_base);5202}else{5203print"<div class=\"page_path\"></div>\n";5204}52055206}elsif($formateq'plain') {5207print$cgi->header(5208-type =>'text/plain',5209-charset =>'utf-8',5210-expires =>$expires,5211-content_disposition =>'inline; filename="'."$file_name".'.patch"');52125213print"X-Git-Url: ".$cgi->self_url() ."\n\n";52145215}else{5216 die_error(400,"Unknown blobdiff format");5217}52185219# patch5220if($formateq'html') {5221print"<div class=\"page_body\">\n";52225223 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5224close$fd;52255226print"</div>\n";# class="page_body"5227 git_footer_html();52285229}else{5230while(my$line= <$fd>) {5231$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5232$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;52335234print$line;52355236last if$line=~m!^\+\+\+!;5237}5238local$/=undef;5239print<$fd>;5240close$fd;5241}5242}52435244sub git_blobdiff_plain {5245 git_blobdiff('plain');5246}52475248sub git_commitdiff {5249my$format=shift||'html';5250$hash||=$hash_base||"HEAD";5251my%co= parse_commit($hash)5252or die_error(404,"Unknown commit object");52535254# choose format for commitdiff for merge5255if(!defined$hash_parent&& @{$co{'parents'}} >1) {5256$hash_parent='--cc';5257}5258# we need to prepare $formats_nav before almost any parameter munging5259my$formats_nav;5260if($formateq'html') {5261$formats_nav=5262$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5263"raw");52645265if(defined$hash_parent&&5266$hash_parentne'-c'&&$hash_parentne'--cc') {5267# commitdiff with two commits given5268my$hash_parent_short=$hash_parent;5269if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5270$hash_parent_short=substr($hash_parent,0,7);5271}5272$formats_nav.=5273' (from';5274for(my$i=0;$i< @{$co{'parents'}};$i++) {5275if($co{'parents'}[$i]eq$hash_parent) {5276$formats_nav.=' parent '. ($i+1);5277last;5278}5279}5280$formats_nav.=': '.5281$cgi->a({-href => href(action=>"commitdiff",5282 hash=>$hash_parent)},5283 esc_html($hash_parent_short)) .5284')';5285}elsif(!$co{'parent'}) {5286# --root commitdiff5287$formats_nav.=' (initial)';5288}elsif(scalar@{$co{'parents'}} ==1) {5289# single parent commit5290$formats_nav.=5291' (parent: '.5292$cgi->a({-href => href(action=>"commitdiff",5293 hash=>$co{'parent'})},5294 esc_html(substr($co{'parent'},0,7))) .5295')';5296}else{5297# merge commit5298if($hash_parenteq'--cc') {5299$formats_nav.=' | '.5300$cgi->a({-href => href(action=>"commitdiff",5301 hash=>$hash, hash_parent=>'-c')},5302'combined');5303}else{# $hash_parent eq '-c'5304$formats_nav.=' | '.5305$cgi->a({-href => href(action=>"commitdiff",5306 hash=>$hash, hash_parent=>'--cc')},5307'compact');5308}5309$formats_nav.=5310' (merge: '.5311join(' ',map{5312$cgi->a({-href => href(action=>"commitdiff",5313 hash=>$_)},5314 esc_html(substr($_,0,7)));5315} @{$co{'parents'}} ) .5316')';5317}5318}53195320my$hash_parent_param=$hash_parent;5321if(!defined$hash_parent_param) {5322# --cc for multiple parents, --root for parentless5323$hash_parent_param=5324@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5325}53265327# read commitdiff5328my$fd;5329my@difftree;5330if($formateq'html') {5331open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5332"--no-commit-id","--patch-with-raw","--full-index",5333$hash_parent_param,$hash,"--"5334or die_error(500,"Open git-diff-tree failed");53355336while(my$line= <$fd>) {5337chomp$line;5338# empty line ends raw part of diff-tree output5339last unless$line;5340push@difftree,scalar parse_difftree_raw_line($line);5341}53425343}elsif($formateq'plain') {5344open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5345'-p',$hash_parent_param,$hash,"--"5346or die_error(500,"Open git-diff-tree failed");53475348}else{5349 die_error(400,"Unknown commitdiff format");5350}53515352# non-textual hash id's can be cached5353my$expires;5354if($hash=~m/^[0-9a-fA-F]{40}$/) {5355$expires="+1d";5356}53575358# write commit message5359if($formateq'html') {5360my$refs= git_get_references();5361my$ref= format_ref_marker($refs,$co{'id'});53625363 git_header_html(undef,$expires);5364 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5365 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5366 git_print_authorship(\%co);5367print"<div class=\"page_body\">\n";5368if(@{$co{'comment'}} >1) {5369print"<div class=\"log\">\n";5370 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5371print"</div>\n";# class="log"5372}53735374}elsif($formateq'plain') {5375my$refs= git_get_references("tags");5376my$tagname= git_get_rev_name_tags($hash);5377my$filename= basename($project) ."-$hash.patch";53785379print$cgi->header(5380-type =>'text/plain',5381-charset =>'utf-8',5382-expires =>$expires,5383-content_disposition =>'inline; filename="'."$filename".'"');5384my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5385print"From: ". to_utf8($co{'author'}) ."\n";5386print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5387print"Subject: ". to_utf8($co{'title'}) ."\n";53885389print"X-Git-Tag:$tagname\n"if$tagname;5390print"X-Git-Url: ".$cgi->self_url() ."\n\n";53915392foreachmy$line(@{$co{'comment'}}) {5393print to_utf8($line) ."\n";5394}5395print"---\n\n";5396}53975398# write patch5399if($formateq'html') {5400my$use_parents= !defined$hash_parent||5401$hash_parenteq'-c'||$hash_parenteq'--cc';5402 git_difftree_body(\@difftree,$hash,5403$use_parents? @{$co{'parents'}} :$hash_parent);5404print"<br/>\n";54055406 git_patchset_body($fd, \@difftree,$hash,5407$use_parents? @{$co{'parents'}} :$hash_parent);5408close$fd;5409print"</div>\n";# class="page_body"5410 git_footer_html();54115412}elsif($formateq'plain') {5413local$/=undef;5414print<$fd>;5415close$fd5416or print"Reading git-diff-tree failed\n";5417}5418}54195420sub git_commitdiff_plain {5421 git_commitdiff('plain');5422}54235424sub git_history {5425if(!defined$hash_base) {5426$hash_base= git_get_head_hash($project);5427}5428if(!defined$page) {5429$page=0;5430}5431my$ftype;5432my%co= parse_commit($hash_base)5433or die_error(404,"Unknown commit object");54345435my$refs= git_get_references();5436my$limit=sprintf("--max-count=%i", (100* ($page+1)));54375438my@commitlist= parse_commits($hash_base,101, (100*$page),5439$file_name,"--full-history")5440or die_error(404,"No such file or directory on given branch");54415442if(!defined$hash&&defined$file_name) {5443# some commits could have deleted file in question,5444# and not have it in tree, but one of them has to have it5445for(my$i=0;$i<=@commitlist;$i++) {5446$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5447last ifdefined$hash;5448}5449}5450if(defined$hash) {5451$ftype= git_get_type($hash);5452}5453if(!defined$ftype) {5454 die_error(500,"Unknown type of object");5455}54565457my$paging_nav='';5458if($page>0) {5459$paging_nav.=5460$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5461 file_name=>$file_name)},5462"first");5463$paging_nav.=" ⋅ ".5464$cgi->a({-href => href(-replay=>1, page=>$page-1),5465-accesskey =>"p", -title =>"Alt-p"},"prev");5466}else{5467$paging_nav.="first";5468$paging_nav.=" ⋅ prev";5469}5470my$next_link='';5471if($#commitlist>=100) {5472$next_link=5473$cgi->a({-href => href(-replay=>1, page=>$page+1),5474-accesskey =>"n", -title =>"Alt-n"},"next");5475$paging_nav.=" ⋅$next_link";5476}else{5477$paging_nav.=" ⋅ next";5478}54795480 git_header_html();5481 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5482 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5483 git_print_page_path($file_name,$ftype,$hash_base);54845485 git_history_body(\@commitlist,0,99,5486$refs,$hash_base,$ftype,$next_link);54875488 git_footer_html();5489}54905491sub git_search {5492 gitweb_check_feature('search')or die_error(403,"Search is disabled");5493if(!defined$searchtext) {5494 die_error(400,"Text field is empty");5495}5496if(!defined$hash) {5497$hash= git_get_head_hash($project);5498}5499my%co= parse_commit($hash);5500if(!%co) {5501 die_error(404,"Unknown commit object");5502}5503if(!defined$page) {5504$page=0;5505}55065507$searchtype||='commit';5508if($searchtypeeq'pickaxe') {5509# pickaxe may take all resources of your box and run for several minutes5510# with every query - so decide by yourself how public you make this feature5511 gitweb_check_feature('pickaxe')5512or die_error(403,"Pickaxe is disabled");5513}5514if($searchtypeeq'grep') {5515 gitweb_check_feature('grep')5516or die_error(403,"Grep is disabled");5517}55185519 git_header_html();55205521if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5522my$greptype;5523if($searchtypeeq'commit') {5524$greptype="--grep=";5525}elsif($searchtypeeq'author') {5526$greptype="--author=";5527}elsif($searchtypeeq'committer') {5528$greptype="--committer=";5529}5530$greptype.=$searchtext;5531my@commitlist= parse_commits($hash,101, (100*$page),undef,5532$greptype,'--regexp-ignore-case',5533$search_use_regexp?'--extended-regexp':'--fixed-strings');55345535my$paging_nav='';5536if($page>0) {5537$paging_nav.=5538$cgi->a({-href => href(action=>"search", hash=>$hash,5539 searchtext=>$searchtext,5540 searchtype=>$searchtype)},5541"first");5542$paging_nav.=" ⋅ ".5543$cgi->a({-href => href(-replay=>1, page=>$page-1),5544-accesskey =>"p", -title =>"Alt-p"},"prev");5545}else{5546$paging_nav.="first";5547$paging_nav.=" ⋅ prev";5548}5549my$next_link='';5550if($#commitlist>=100) {5551$next_link=5552$cgi->a({-href => href(-replay=>1, page=>$page+1),5553-accesskey =>"n", -title =>"Alt-n"},"next");5554$paging_nav.=" ⋅$next_link";5555}else{5556$paging_nav.=" ⋅ next";5557}55585559if($#commitlist>=100) {5560}55615562 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5563 git_print_header_div('commit', esc_html($co{'title'}),$hash);5564 git_search_grep_body(\@commitlist,0,99,$next_link);5565}55665567if($searchtypeeq'pickaxe') {5568 git_print_page_nav('','',$hash,$co{'tree'},$hash);5569 git_print_header_div('commit', esc_html($co{'title'}),$hash);55705571print"<table class=\"pickaxe search\">\n";5572my$alternate=1;5573$/="\n";5574open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5575'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5576($search_use_regexp?'--pickaxe-regex': ());5577undef%co;5578my@files;5579while(my$line= <$fd>) {5580chomp$line;5581next unless$line;55825583my%set= parse_difftree_raw_line($line);5584if(defined$set{'commit'}) {5585# finish previous commit5586if(%co) {5587print"</td>\n".5588"<td class=\"link\">".5589$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5590" | ".5591$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5592print"</td>\n".5593"</tr>\n";5594}55955596if($alternate) {5597print"<tr class=\"dark\">\n";5598}else{5599print"<tr class=\"light\">\n";5600}5601$alternate^=1;5602%co= parse_commit($set{'commit'});5603my$author= chop_and_escape_str($co{'author_name'},15,5);5604print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5605"<td><i>$author</i></td>\n".5606"<td>".5607$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5608-class=>"list subject"},5609 chop_and_escape_str($co{'title'},50) ."<br/>");5610}elsif(defined$set{'to_id'}) {5611next if($set{'to_id'} =~m/^0{40}$/);56125613print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5614 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5615-class=>"list"},5616"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5617"<br/>\n";5618}5619}5620close$fd;56215622# finish last commit (warning: repetition!)5623if(%co) {5624print"</td>\n".5625"<td class=\"link\">".5626$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5627" | ".5628$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5629print"</td>\n".5630"</tr>\n";5631}56325633print"</table>\n";5634}56355636if($searchtypeeq'grep') {5637 git_print_page_nav('','',$hash,$co{'tree'},$hash);5638 git_print_header_div('commit', esc_html($co{'title'}),$hash);56395640print"<table class=\"grep_search\">\n";5641my$alternate=1;5642my$matches=0;5643$/="\n";5644open my$fd,"-|", git_cmd(),'grep','-n',5645$search_use_regexp? ('-E','-i') :'-F',5646$searchtext,$co{'tree'};5647my$lastfile='';5648while(my$line= <$fd>) {5649chomp$line;5650my($file,$lno,$ltext,$binary);5651last if($matches++>1000);5652if($line=~/^Binary file (.+) matches$/) {5653$file=$1;5654$binary=1;5655}else{5656(undef,$file,$lno,$ltext) =split(/:/,$line,4);5657}5658if($filene$lastfile) {5659$lastfileand print"</td></tr>\n";5660if($alternate++) {5661print"<tr class=\"dark\">\n";5662}else{5663print"<tr class=\"light\">\n";5664}5665print"<td class=\"list\">".5666$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5667 file_name=>"$file"),5668-class=>"list"}, esc_path($file));5669print"</td><td>\n";5670$lastfile=$file;5671}5672if($binary) {5673print"<div class=\"binary\">Binary file</div>\n";5674}else{5675$ltext= untabify($ltext);5676if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5677$ltext= esc_html($1, -nbsp=>1);5678$ltext.='<span class="match">';5679$ltext.= esc_html($2, -nbsp=>1);5680$ltext.='</span>';5681$ltext.= esc_html($3, -nbsp=>1);5682}else{5683$ltext= esc_html($ltext, -nbsp=>1);5684}5685print"<div class=\"pre\">".5686$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5687 file_name=>"$file").'#l'.$lno,5688-class=>"linenr"},sprintf('%4i',$lno))5689.' '.$ltext."</div>\n";5690}5691}5692if($lastfile) {5693print"</td></tr>\n";5694if($matches>1000) {5695print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5696}5697}else{5698print"<div class=\"diff nodifferences\">No matches found</div>\n";5699}5700close$fd;57015702print"</table>\n";5703}5704 git_footer_html();5705}57065707sub git_search_help {5708 git_header_html();5709 git_print_page_nav('','',$hash,$hash,$hash);5710print<<EOT;5711<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5712regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5713the pattern entered is recognized as the POSIX extended5714<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5715insensitive).</p>5716<dl>5717<dt><b>commit</b></dt>5718<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5719EOT5720my($have_grep) = gitweb_check_feature('grep');5721if($have_grep) {5722print<<EOT;5723<dt><b>grep</b></dt>5724<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5725 a different one) are searched for the given pattern. On large trees, this search can take5726a while and put some strain on the server, so please use it with some consideration. Note that5727due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5728case-sensitive.</dd>5729EOT5730}5731print<<EOT;5732<dt><b>author</b></dt>5733<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5734<dt><b>committer</b></dt>5735<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5736EOT5737my($have_pickaxe) = gitweb_check_feature('pickaxe');5738if($have_pickaxe) {5739print<<EOT;5740<dt><b>pickaxe</b></dt>5741<dd>All commits that caused the string to appear or disappear from any file (changes that5742added, removed or "modified" the string) will be listed. This search can take a while and5743takes a lot of strain on the server, so please use it wisely. Note that since you may be5744interested even in changes just changing the case as well, this search is case sensitive.</dd>5745EOT5746}5747print"</dl>\n";5748 git_footer_html();5749}57505751sub git_shortlog {5752my$head= git_get_head_hash($project);5753if(!defined$hash) {5754$hash=$head;5755}5756if(!defined$page) {5757$page=0;5758}5759my$refs= git_get_references();57605761my$commit_hash=$hash;5762if(defined$hash_parent) {5763$commit_hash="$hash_parent..$hash";5764}5765my@commitlist= parse_commits($commit_hash,101, (100*$page));57665767my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5768my$next_link='';5769if($#commitlist>=100) {5770$next_link=5771$cgi->a({-href => href(-replay=>1, page=>$page+1),5772-accesskey =>"n", -title =>"Alt-n"},"next");5773}57745775 git_header_html();5776 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);5777 git_print_header_div('summary',$project);57785779 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);57805781 git_footer_html();5782}57835784## ......................................................................5785## feeds (RSS, Atom; OPML)57865787sub git_feed {5788my$format=shift||'atom';5789my($have_blame) = gitweb_check_feature('blame');57905791# Atom: http://www.atomenabled.org/developers/syndication/5792# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ5793if($formatne'rss'&&$formatne'atom') {5794 die_error(400,"Unknown web feed format");5795}57965797# log/feed of current (HEAD) branch, log of given branch, history of file/directory5798my$head=$hash||'HEAD';5799my@commitlist= parse_commits($head,150,0,$file_name);58005801my%latest_commit;5802my%latest_date;5803my$content_type="application/$format+xml";5804if(defined$cgi->http('HTTP_ACCEPT') &&5805$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {5806# browser (feed reader) prefers text/xml5807$content_type='text/xml';5808}5809if(defined($commitlist[0])) {5810%latest_commit= %{$commitlist[0]};5811%latest_date= parse_date($latest_commit{'author_epoch'});5812print$cgi->header(5813-type =>$content_type,5814-charset =>'utf-8',5815-last_modified =>$latest_date{'rfc2822'});5816}else{5817print$cgi->header(5818-type =>$content_type,5819-charset =>'utf-8');5820}58215822# Optimization: skip generating the body if client asks only5823# for Last-Modified date.5824return if($cgi->request_method()eq'HEAD');58255826# header variables5827my$title="$site_name-$project/$action";5828my$feed_type='log';5829if(defined$hash) {5830$title.=" - '$hash'";5831$feed_type='branch log';5832if(defined$file_name) {5833$title.=" ::$file_name";5834$feed_type='history';5835}5836}elsif(defined$file_name) {5837$title.=" -$file_name";5838$feed_type='history';5839}5840$title.="$feed_type";5841my$descr= git_get_project_description($project);5842if(defined$descr) {5843$descr= esc_html($descr);5844}else{5845$descr="$project".5846($formateq'rss'?'RSS':'Atom') .5847" feed";5848}5849my$owner= git_get_project_owner($project);5850$owner= esc_html($owner);58515852#header5853my$alt_url;5854if(defined$file_name) {5855$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);5856}elsif(defined$hash) {5857$alt_url= href(-full=>1, action=>"log", hash=>$hash);5858}else{5859$alt_url= href(-full=>1, action=>"summary");5860}5861print qq!<?xml version="1.0" encoding="utf-8"?>\n!;5862if($formateq'rss') {5863print<<XML;5864<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">5865<channel>5866XML5867print"<title>$title</title>\n".5868"<link>$alt_url</link>\n".5869"<description>$descr</description>\n".5870"<language>en</language>\n";5871}elsif($formateq'atom') {5872print<<XML;5873<feed xmlns="http://www.w3.org/2005/Atom">5874XML5875print"<title>$title</title>\n".5876"<subtitle>$descr</subtitle>\n".5877'<link rel="alternate" type="text/html" href="'.5878$alt_url.'" />'."\n".5879'<link rel="self" type="'.$content_type.'" href="'.5880$cgi->self_url() .'" />'."\n".5881"<id>". href(-full=>1) ."</id>\n".5882# use project owner for feed author5883"<author><name>$owner</name></author>\n";5884if(defined$favicon) {5885print"<icon>". esc_url($favicon) ."</icon>\n";5886}5887if(defined$logo_url) {5888# not twice as wide as tall: 72 x 27 pixels5889print"<logo>". esc_url($logo) ."</logo>\n";5890}5891if(!%latest_date) {5892# dummy date to keep the feed valid until commits trickle in:5893print"<updated>1970-01-01T00:00:00Z</updated>\n";5894}else{5895print"<updated>$latest_date{'iso-8601'}</updated>\n";5896}5897}58985899# contents5900for(my$i=0;$i<=$#commitlist;$i++) {5901my%co= %{$commitlist[$i]};5902my$commit=$co{'id'};5903# we read 150, we always show 30 and the ones more recent than 48 hours5904if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {5905last;5906}5907my%cd= parse_date($co{'author_epoch'});59085909# get list of changed files5910open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5911$co{'parent'} ||"--root",5912$co{'id'},"--", (defined$file_name?$file_name: ())5913ornext;5914my@difftree=map{chomp;$_} <$fd>;5915close$fd5916ornext;59175918# print element (entry, item)5919my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);5920if($formateq'rss') {5921print"<item>\n".5922"<title>". esc_html($co{'title'}) ."</title>\n".5923"<author>". esc_html($co{'author'}) ."</author>\n".5924"<pubDate>$cd{'rfc2822'}</pubDate>\n".5925"<guid isPermaLink=\"true\">$co_url</guid>\n".5926"<link>$co_url</link>\n".5927"<description>". esc_html($co{'title'}) ."</description>\n".5928"<content:encoded>".5929"<![CDATA[\n";5930}elsif($formateq'atom') {5931print"<entry>\n".5932"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".5933"<updated>$cd{'iso-8601'}</updated>\n".5934"<author>\n".5935" <name>". esc_html($co{'author_name'}) ."</name>\n";5936if($co{'author_email'}) {5937print" <email>". esc_html($co{'author_email'}) ."</email>\n";5938}5939print"</author>\n".5940# use committer for contributor5941"<contributor>\n".5942" <name>". esc_html($co{'committer_name'}) ."</name>\n";5943if($co{'committer_email'}) {5944print" <email>". esc_html($co{'committer_email'}) ."</email>\n";5945}5946print"</contributor>\n".5947"<published>$cd{'iso-8601'}</published>\n".5948"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".5949"<id>$co_url</id>\n".5950"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".5951"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";5952}5953my$comment=$co{'comment'};5954print"<pre>\n";5955foreachmy$line(@$comment) {5956$line= esc_html($line);5957print"$line\n";5958}5959print"</pre><ul>\n";5960foreachmy$difftree_line(@difftree) {5961my%difftree= parse_difftree_raw_line($difftree_line);5962next if!$difftree{'from_id'};59635964my$file=$difftree{'file'} ||$difftree{'to_file'};59655966print"<li>".5967"[".5968$cgi->a({-href => href(-full=>1, action=>"blobdiff",5969 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},5970 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},5971 file_name=>$file, file_parent=>$difftree{'from_file'}),5972-title =>"diff"},'D');5973if($have_blame) {5974print$cgi->a({-href => href(-full=>1, action=>"blame",5975 file_name=>$file, hash_base=>$commit),5976-title =>"blame"},'B');5977}5978# if this is not a feed of a file history5979if(!defined$file_name||$file_namene$file) {5980print$cgi->a({-href => href(-full=>1, action=>"history",5981 file_name=>$file, hash=>$commit),5982-title =>"history"},'H');5983}5984$file= esc_path($file);5985print"] ".5986"$file</li>\n";5987}5988if($formateq'rss') {5989print"</ul>]]>\n".5990"</content:encoded>\n".5991"</item>\n";5992}elsif($formateq'atom') {5993print"</ul>\n</div>\n".5994"</content>\n".5995"</entry>\n";5996}5997}59985999# end of feed6000if($formateq'rss') {6001print"</channel>\n</rss>\n";6002}elsif($formateq'atom') {6003print"</feed>\n";6004}6005}60066007sub git_rss {6008 git_feed('rss');6009}60106011sub git_atom {6012 git_feed('atom');6013}60146015sub git_opml {6016my@list= git_get_projects_list();60176018print$cgi->header(-type =>'text/xml', -charset =>'utf-8');6019print<<XML;6020<?xml version="1.0" encoding="utf-8"?>6021<opml version="1.0">6022<head>6023 <title>$site_nameOPML Export</title>6024</head>6025<body>6026<outline text="git RSS feeds">6027XML60286029foreachmy$pr(@list) {6030my%proj=%$pr;6031my$head= git_get_head_hash($proj{'path'});6032if(!defined$head) {6033next;6034}6035$git_dir="$projectroot/$proj{'path'}";6036my%co= parse_commit($head);6037if(!%co) {6038next;6039}60406041my$path= esc_html(chop_str($proj{'path'},25,5));6042my$rss="$my_url?p=$proj{'path'};a=rss";6043my$html="$my_url?p=$proj{'path'};a=summary";6044print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6045}6046print<<XML;6047</outline>6048</body>6049</opml>6050XML6051}