1#!/usr/bin/perl 2 3# gitweb - simple web interface to track changes in git repositories 4# 5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org> 6# (C) 2005, Christian Gierke 7# 8# This program is licensed under the GPLv2 9 10use strict; 11use warnings; 12use CGI qw(:standard :escapeHTML -nosticky); 13use CGI::Util qw(unescape); 14use CGI::Carp qw(fatalsToBrowser); 15use Encode; 16use Fcntl ':mode'; 17use File::Find qw(); 18use File::Basename qw(basename); 19binmode STDOUT,':utf8'; 20 21BEGIN{ 22 CGI->compile()if$ENV{'MOD_PERL'}; 23} 24 25our$cgi= new CGI; 26our$version="++GIT_VERSION++"; 27our$my_url=$cgi->url(); 28our$my_uri=$cgi->url(-absolute =>1); 29 30# if we're called with PATH_INFO, we have to strip that 31# from the URL to find our real URL 32# we make $path_info global because it's also used later on 33my$path_info=$ENV{"PATH_INFO"}; 34if($path_info) { 35$my_url=~ s,\Q$path_info\E$,,; 36$my_uri=~ s,\Q$path_info\E$,,; 37} 38 39# core git executable to use 40# this can just be "git" if your webserver has a sensible PATH 41our$GIT="++GIT_BINDIR++/git"; 42 43# absolute fs-path which will be prepended to the project path 44#our $projectroot = "/pub/scm"; 45our$projectroot="++GITWEB_PROJECTROOT++"; 46 47# fs traversing limit for getting project list 48# the number is relative to the projectroot 49our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 50 51# target of the home link on top of all pages 52our$home_link=$my_uri||"/"; 53 54# string of the home link on top of all pages 55our$home_link_str="++GITWEB_HOME_LINK_STR++"; 56 57# name of your site or organization to appear in page titles 58# replace this with something more descriptive for clearer bookmarks 59our$site_name="++GITWEB_SITENAME++" 60|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 61 62# filename of html text to include at top of each page 63our$site_header="++GITWEB_SITE_HEADER++"; 64# html text to include at home page 65our$home_text="++GITWEB_HOMETEXT++"; 66# filename of html text to include at bottom of each page 67our$site_footer="++GITWEB_SITE_FOOTER++"; 68 69# URI of stylesheets 70our@stylesheets= ("++GITWEB_CSS++"); 71# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 72our$stylesheet=undef; 73# URI of GIT logo (72x27 size) 74our$logo="++GITWEB_LOGO++"; 75# URI of GIT favicon, assumed to be image/png type 76our$favicon="++GITWEB_FAVICON++"; 77 78# URI and label (title) of GIT logo link 79#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 80#our $logo_label = "git documentation"; 81our$logo_url="http://git.or.cz/"; 82our$logo_label="git homepage"; 83 84# source of projects list 85our$projects_list="++GITWEB_LIST++"; 86 87# the width (in characters) of the projects list "Description" column 88our$projects_list_description_width=25; 89 90# default order of projects list 91# valid values are none, project, descr, owner, and age 92our$default_projects_order="project"; 93 94# show repository only if this file exists 95# (only effective if this variable evaluates to true) 96our$export_ok="++GITWEB_EXPORT_OK++"; 97 98# only allow viewing of repositories also shown on the overview page 99our$strict_export="++GITWEB_STRICT_EXPORT++"; 100 101# list of git base URLs used for URL to where fetch project from, 102# i.e. full URL is "$git_base_url/$project" 103our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 104 105# default blob_plain mimetype and default charset for text/plain blob 106our$default_blob_plain_mimetype='text/plain'; 107our$default_text_plain_charset=undef; 108 109# file to use for guessing MIME types before trying /etc/mime.types 110# (relative to the current git repository) 111our$mimetypes_file=undef; 112 113# assume this charset if line contains non-UTF-8 characters; 114# it should be valid encoding (see Encoding::Supported(3pm) for list), 115# for which encoding all byte sequences are valid, for example 116# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 117# could be even 'utf-8' for the old behavior) 118our$fallback_encoding='latin1'; 119 120# rename detection options for git-diff and git-diff-tree 121# - default is '-M', with the cost proportional to 122# (number of removed files) * (number of new files). 123# - more costly is '-C' (which implies '-M'), with the cost proportional to 124# (number of changed files + number of removed files) * (number of new files) 125# - even more costly is '-C', '--find-copies-harder' with cost 126# (number of files in the original tree) * (number of new files) 127# - one might want to include '-B' option, e.g. '-B', '-M' 128our@diff_opts= ('-M');# taken from git_commit 129 130# information about snapshot formats that gitweb is capable of serving 131our%known_snapshot_formats= ( 132# name => { 133# 'display' => display name, 134# 'type' => mime type, 135# 'suffix' => filename suffix, 136# 'format' => --format for git-archive, 137# 'compressor' => [compressor command and arguments] 138# (array reference, optional)} 139# 140'tgz'=> { 141'display'=>'tar.gz', 142'type'=>'application/x-gzip', 143'suffix'=>'.tar.gz', 144'format'=>'tar', 145'compressor'=> ['gzip']}, 146 147'tbz2'=> { 148'display'=>'tar.bz2', 149'type'=>'application/x-bzip2', 150'suffix'=>'.tar.bz2', 151'format'=>'tar', 152'compressor'=> ['bzip2']}, 153 154'zip'=> { 155'display'=>'zip', 156'type'=>'application/x-zip', 157'suffix'=>'.zip', 158'format'=>'zip'}, 159); 160 161# Aliases so we understand old gitweb.snapshot values in repository 162# configuration. 163our%known_snapshot_format_aliases= ( 164'gzip'=>'tgz', 165'bzip2'=>'tbz2', 166 167# backward compatibility: legacy gitweb config support 168'x-gzip'=>undef,'gz'=>undef, 169'x-bzip2'=>undef,'bz2'=>undef, 170'x-zip'=>undef,''=>undef, 171); 172 173# You define site-wide feature defaults here; override them with 174# $GITWEB_CONFIG as necessary. 175our%feature= ( 176# feature => { 177# 'sub' => feature-sub (subroutine), 178# 'override' => allow-override (boolean), 179# 'default' => [ default options...] (array reference)} 180# 181# if feature is overridable (it means that allow-override has true value), 182# then feature-sub will be called with default options as parameters; 183# return value of feature-sub indicates if to enable specified feature 184# 185# if there is no 'sub' key (no feature-sub), then feature cannot be 186# overriden 187# 188# use gitweb_check_feature(<feature>) to check if <feature> is enabled 189 190# Enable the 'blame' blob view, showing the last commit that modified 191# each line in the file. This can be very CPU-intensive. 192 193# To enable system wide have in $GITWEB_CONFIG 194# $feature{'blame'}{'default'} = [1]; 195# To have project specific config enable override in $GITWEB_CONFIG 196# $feature{'blame'}{'override'} = 1; 197# and in project config gitweb.blame = 0|1; 198'blame'=> { 199'sub'=> \&feature_blame, 200'override'=>0, 201'default'=> [0]}, 202 203# Enable the 'snapshot' link, providing a compressed archive of any 204# tree. This can potentially generate high traffic if you have large 205# project. 206 207# Value is a list of formats defined in %known_snapshot_formats that 208# you wish to offer. 209# To disable system wide have in $GITWEB_CONFIG 210# $feature{'snapshot'}{'default'} = []; 211# To have project specific config enable override in $GITWEB_CONFIG 212# $feature{'snapshot'}{'override'} = 1; 213# and in project config, a comma-separated list of formats or "none" 214# to disable. Example: gitweb.snapshot = tbz2,zip; 215'snapshot'=> { 216'sub'=> \&feature_snapshot, 217'override'=>0, 218'default'=> ['tgz']}, 219 220# Enable text search, which will list the commits which match author, 221# committer or commit text to a given string. Enabled by default. 222# Project specific override is not supported. 223'search'=> { 224'override'=>0, 225'default'=> [1]}, 226 227# Enable grep search, which will list the files in currently selected 228# tree containing the given string. Enabled by default. This can be 229# potentially CPU-intensive, of course. 230 231# To enable system wide have in $GITWEB_CONFIG 232# $feature{'grep'}{'default'} = [1]; 233# To have project specific config enable override in $GITWEB_CONFIG 234# $feature{'grep'}{'override'} = 1; 235# and in project config gitweb.grep = 0|1; 236'grep'=> { 237'override'=>0, 238'default'=> [1]}, 239 240# Enable the pickaxe search, which will list the commits that modified 241# a given string in a file. This can be practical and quite faster 242# alternative to 'blame', but still potentially CPU-intensive. 243 244# To enable system wide have in $GITWEB_CONFIG 245# $feature{'pickaxe'}{'default'} = [1]; 246# To have project specific config enable override in $GITWEB_CONFIG 247# $feature{'pickaxe'}{'override'} = 1; 248# and in project config gitweb.pickaxe = 0|1; 249'pickaxe'=> { 250'sub'=> \&feature_pickaxe, 251'override'=>0, 252'default'=> [1]}, 253 254# Make gitweb use an alternative format of the URLs which can be 255# more readable and natural-looking: project name is embedded 256# directly in the path and the query string contains other 257# auxiliary information. All gitweb installations recognize 258# URL in either format; this configures in which formats gitweb 259# generates links. 260 261# To enable system wide have in $GITWEB_CONFIG 262# $feature{'pathinfo'}{'default'} = [1]; 263# Project specific override is not supported. 264 265# Note that you will need to change the default location of CSS, 266# favicon, logo and possibly other files to an absolute URL. Also, 267# if gitweb.cgi serves as your indexfile, you will need to force 268# $my_uri to contain the script name in your $GITWEB_CONFIG. 269'pathinfo'=> { 270'override'=>0, 271'default'=> [0]}, 272 273# Make gitweb consider projects in project root subdirectories 274# to be forks of existing projects. Given project $projname.git, 275# projects matching $projname/*.git will not be shown in the main 276# projects list, instead a '+' mark will be added to $projname 277# there and a 'forks' view will be enabled for the project, listing 278# all the forks. If project list is taken from a file, forks have 279# to be listed after the main project. 280 281# To enable system wide have in $GITWEB_CONFIG 282# $feature{'forks'}{'default'} = [1]; 283# Project specific override is not supported. 284'forks'=> { 285'override'=>0, 286'default'=> [0]}, 287 288# Insert custom links to the action bar of all project pages. 289# This enables you mainly to link to third-party scripts integrating 290# into gitweb; e.g. git-browser for graphical history representation 291# or custom web-based repository administration interface. 292 293# The 'default' value consists of a list of triplets in the form 294# (label, link, position) where position is the label after which 295# to inster the link and link is a format string where %n expands 296# to the project name, %f to the project path within the filesystem, 297# %h to the current hash (h gitweb parameter) and %b to the current 298# hash base (hb gitweb parameter). 299 300# To enable system wide have in $GITWEB_CONFIG e.g. 301# $feature{'actions'}{'default'} = [('graphiclog', 302# '/git-browser/by-commit.html?r=%n', 'summary')]; 303# Project specific override is not supported. 304'actions'=> { 305'override'=>0, 306'default'=> []}, 307 308# Allow gitweb scan project content tags described in ctags/ 309# of project repository, and display the popular Web 2.0-ish 310# "tag cloud" near the project list. Note that this is something 311# COMPLETELY different from the normal Git tags. 312 313# gitweb by itself can show existing tags, but it does not handle 314# tagging itself; you need an external application for that. 315# For an example script, check Girocco's cgi/tagproj.cgi. 316# You may want to install the HTML::TagCloud Perl module to get 317# a pretty tag cloud instead of just a list of tags. 318 319# To enable system wide have in $GITWEB_CONFIG 320# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 321# Project specific override is not supported. 322'ctags'=> { 323'override'=>0, 324'default'=> [0]}, 325); 326 327sub gitweb_check_feature { 328my($name) =@_; 329return unlessexists$feature{$name}; 330my($sub,$override,@defaults) = ( 331$feature{$name}{'sub'}, 332$feature{$name}{'override'}, 333@{$feature{$name}{'default'}}); 334if(!$override) {return@defaults; } 335if(!defined$sub) { 336warn"feature$nameis not overrideable"; 337return@defaults; 338} 339return$sub->(@defaults); 340} 341 342sub feature_blame { 343my($val) = git_get_project_config('blame','--bool'); 344 345if($valeq'true') { 346return1; 347}elsif($valeq'false') { 348return0; 349} 350 351return$_[0]; 352} 353 354sub feature_snapshot { 355my(@fmts) =@_; 356 357my($val) = git_get_project_config('snapshot'); 358 359if($val) { 360@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 361} 362 363return@fmts; 364} 365 366sub feature_grep { 367my($val) = git_get_project_config('grep','--bool'); 368 369if($valeq'true') { 370return(1); 371}elsif($valeq'false') { 372return(0); 373} 374 375return($_[0]); 376} 377 378sub feature_pickaxe { 379my($val) = git_get_project_config('pickaxe','--bool'); 380 381if($valeq'true') { 382return(1); 383}elsif($valeq'false') { 384return(0); 385} 386 387return($_[0]); 388} 389 390# checking HEAD file with -e is fragile if the repository was 391# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 392# and then pruned. 393sub check_head_link { 394my($dir) =@_; 395my$headfile="$dir/HEAD"; 396return((-e $headfile) || 397(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 398} 399 400sub check_export_ok { 401my($dir) =@_; 402return(check_head_link($dir) && 403(!$export_ok|| -e "$dir/$export_ok")); 404} 405 406# process alternate names for backward compatibility 407# filter out unsupported (unknown) snapshot formats 408sub filter_snapshot_fmts { 409my@fmts=@_; 410 411@fmts=map{ 412exists$known_snapshot_format_aliases{$_} ? 413$known_snapshot_format_aliases{$_} :$_}@fmts; 414@fmts=grep(exists$known_snapshot_formats{$_},@fmts); 415 416} 417 418our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 419if(-e $GITWEB_CONFIG) { 420do$GITWEB_CONFIG; 421}else{ 422our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 423do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 424} 425 426# version of the core git binary 427our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 428 429$projects_list||=$projectroot; 430 431# ====================================================================== 432# input validation and dispatch 433 434# input parameters can be collected from a variety of sources (presently, CGI 435# and PATH_INFO), so we define an %input_params hash that collects them all 436# together during validation: this allows subsequent uses (e.g. href()) to be 437# agnostic of the parameter origin 438 439my%input_params= (); 440 441# input parameters are stored with the long parameter name as key. This will 442# also be used in the href subroutine to convert parameters to their CGI 443# equivalent, and since the href() usage is the most frequent one, we store 444# the name -> CGI key mapping here, instead of the reverse. 445# 446# XXX: Warning: If you touch this, check the search form for updating, 447# too. 448 449my@cgi_param_mapping= ( 450 project =>"p", 451 action =>"a", 452 file_name =>"f", 453 file_parent =>"fp", 454 hash =>"h", 455 hash_parent =>"hp", 456 hash_base =>"hb", 457 hash_parent_base =>"hpb", 458 page =>"pg", 459 order =>"o", 460 searchtext =>"s", 461 searchtype =>"st", 462 snapshot_format =>"sf", 463 extra_options =>"opt", 464 search_use_regexp =>"sr", 465); 466my%cgi_param_mapping=@cgi_param_mapping; 467 468# we will also need to know the possible actions, for validation 469my%actions= ( 470"blame"=> \&git_blame, 471"blobdiff"=> \&git_blobdiff, 472"blobdiff_plain"=> \&git_blobdiff_plain, 473"blob"=> \&git_blob, 474"blob_plain"=> \&git_blob_plain, 475"commitdiff"=> \&git_commitdiff, 476"commitdiff_plain"=> \&git_commitdiff_plain, 477"commit"=> \&git_commit, 478"forks"=> \&git_forks, 479"heads"=> \&git_heads, 480"history"=> \&git_history, 481"log"=> \&git_log, 482"rss"=> \&git_rss, 483"atom"=> \&git_atom, 484"search"=> \&git_search, 485"search_help"=> \&git_search_help, 486"shortlog"=> \&git_shortlog, 487"summary"=> \&git_summary, 488"tag"=> \&git_tag, 489"tags"=> \&git_tags, 490"tree"=> \&git_tree, 491"snapshot"=> \&git_snapshot, 492"object"=> \&git_object, 493# those below don't need $project 494"opml"=> \&git_opml, 495"project_list"=> \&git_project_list, 496"project_index"=> \&git_project_index, 497); 498 499# finally, we have the hash of allowed extra_options for the commands that 500# allow them 501my%allowed_options= ( 502"--no-merges"=> [qw(rss atom log shortlog history)], 503); 504 505# fill %input_params with the CGI parameters. All values except for 'opt' 506# should be single values, but opt can be an array. We should probably 507# build an array of parameters that can be multi-valued, but since for the time 508# being it's only this one, we just single it out 509while(my($name,$symbol) =each%cgi_param_mapping) { 510if($symboleq'opt') { 511$input_params{$name} = [$cgi->param($symbol) ]; 512}else{ 513$input_params{$name} =$cgi->param($symbol); 514} 515} 516 517# now read PATH_INFO and update the parameter list for missing parameters 518sub evaluate_path_info { 519return ifdefined$input_params{'project'}; 520return if!$path_info; 521$path_info=~ s,^/+,,; 522return if!$path_info; 523 524# find which part of PATH_INFO is project 525my$project=$path_info; 526$project=~ s,/+$,,; 527while($project&& !check_head_link("$projectroot/$project")) { 528$project=~ s,/*[^/]*$,,; 529} 530return unless$project; 531$input_params{'project'} =$project; 532 533# do not change any parameters if an action is given using the query string 534return if$input_params{'action'}; 535$path_info=~ s,^\Q$project\E/*,,; 536 537# next, check if we have an action 538my$action=$path_info; 539$action=~ s,/.*$,,; 540if(exists$actions{$action}) { 541$path_info=~ s,^$action/*,,; 542$input_params{'action'} =$action; 543} 544 545# list of actions that want hash_base instead of hash, but can have no 546# pathname (f) parameter 547my@wants_base= ( 548'tree', 549'history', 550); 551 552# we want to catch 553# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 554my($parentrefname,$parentpathname,$refname,$pathname) = 555($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 556 557# first, analyze the 'current' part 558if(defined$pathname) { 559# we got "branch:filename" or "branch:dir/" 560# we could use git_get_type(branch:pathname), but: 561# - it needs $git_dir 562# - it does a git() call 563# - the convention of terminating directories with a slash 564# makes it superfluous 565# - embedding the action in the PATH_INFO would make it even 566# more superfluous 567$pathname=~ s,^/+,,; 568if(!$pathname||substr($pathname, -1)eq"/") { 569$input_params{'action'} ||="tree"; 570$pathname=~ s,/$,,; 571}else{ 572# the default action depends on whether we had parent info 573# or not 574if($parentrefname) { 575$input_params{'action'} ||="blobdiff_plain"; 576}else{ 577$input_params{'action'} ||="blob_plain"; 578} 579} 580$input_params{'hash_base'} ||=$refname; 581$input_params{'file_name'} ||=$pathname; 582}elsif(defined$refname) { 583# we got "branch". In this case we have to choose if we have to 584# set hash or hash_base. 585# 586# Most of the actions without a pathname only want hash to be 587# set, except for the ones specified in @wants_base that want 588# hash_base instead. It should also be noted that hand-crafted 589# links having 'history' as an action and no pathname or hash 590# set will fail, but that happens regardless of PATH_INFO. 591$input_params{'action'} ||="shortlog"; 592if(grep{$_eq$input_params{'action'} }@wants_base) { 593$input_params{'hash_base'} ||=$refname; 594}else{ 595$input_params{'hash'} ||=$refname; 596} 597} 598 599# next, handle the 'parent' part, if present 600if(defined$parentrefname) { 601# a missing pathspec defaults to the 'current' filename, allowing e.g. 602# someproject/blobdiff/oldrev..newrev:/filename 603if($parentpathname) { 604$parentpathname=~ s,^/+,,; 605$parentpathname=~ s,/$,,; 606$input_params{'file_parent'} ||=$parentpathname; 607}else{ 608$input_params{'file_parent'} ||=$input_params{'file_name'}; 609} 610# we assume that hash_parent_base is wanted if a path was specified, 611# or if the action wants hash_base instead of hash 612if(defined$input_params{'file_parent'} || 613grep{$_eq$input_params{'action'} }@wants_base) { 614$input_params{'hash_parent_base'} ||=$parentrefname; 615}else{ 616$input_params{'hash_parent'} ||=$parentrefname; 617} 618} 619} 620evaluate_path_info(); 621 622our$action=$input_params{'action'}; 623if(defined$action) { 624if(!validate_action($action)) { 625 die_error(400,"Invalid action parameter"); 626} 627} 628 629# parameters which are pathnames 630our$project=$input_params{'project'}; 631if(defined$project) { 632if(!validate_project($project)) { 633undef$project; 634 die_error(404,"No such project"); 635} 636} 637 638our$file_name=$input_params{'file_name'}; 639if(defined$file_name) { 640if(!validate_pathname($file_name)) { 641 die_error(400,"Invalid file parameter"); 642} 643} 644 645our$file_parent=$input_params{'file_parent'}; 646if(defined$file_parent) { 647if(!validate_pathname($file_parent)) { 648 die_error(400,"Invalid file parent parameter"); 649} 650} 651 652# parameters which are refnames 653our$hash=$input_params{'hash'}; 654if(defined$hash) { 655if(!validate_refname($hash)) { 656 die_error(400,"Invalid hash parameter"); 657} 658} 659 660our$hash_parent=$input_params{'hash_parent'}; 661if(defined$hash_parent) { 662if(!validate_refname($hash_parent)) { 663 die_error(400,"Invalid hash parent parameter"); 664} 665} 666 667our$hash_base=$input_params{'hash_base'}; 668if(defined$hash_base) { 669if(!validate_refname($hash_base)) { 670 die_error(400,"Invalid hash base parameter"); 671} 672} 673 674our@extra_options= @{$input_params{'extra_options'}}; 675# @extra_options is always defined, since it can only be (currently) set from 676# CGI, and $cgi->param() returns the empty array in array context if the param 677# is not set 678foreachmy$opt(@extra_options) { 679if(not exists$allowed_options{$opt}) { 680 die_error(400,"Invalid option parameter"); 681} 682if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 683 die_error(400,"Invalid option parameter for this action"); 684} 685} 686 687our$hash_parent_base=$input_params{'hash_parent_base'}; 688if(defined$hash_parent_base) { 689if(!validate_refname($hash_parent_base)) { 690 die_error(400,"Invalid hash parent base parameter"); 691} 692} 693 694# other parameters 695our$page=$input_params{'page'}; 696if(defined$page) { 697if($page=~m/[^0-9]/) { 698 die_error(400,"Invalid page parameter"); 699} 700} 701 702our$searchtype=$input_params{'searchtype'}; 703if(defined$searchtype) { 704if($searchtype=~m/[^a-z]/) { 705 die_error(400,"Invalid searchtype parameter"); 706} 707} 708 709our$search_use_regexp=$input_params{'search_use_regexp'}; 710 711our$searchtext=$input_params{'searchtext'}; 712our$search_regexp; 713if(defined$searchtext) { 714if(length($searchtext) <2) { 715 die_error(403,"At least two characters are required for search parameter"); 716} 717$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 718} 719 720# path to the current git repository 721our$git_dir; 722$git_dir="$projectroot/$project"if$project; 723 724# dispatch 725if(!defined$action) { 726if(defined$hash) { 727$action= git_get_type($hash); 728}elsif(defined$hash_base&&defined$file_name) { 729$action= git_get_type("$hash_base:$file_name"); 730}elsif(defined$project) { 731$action='summary'; 732}else{ 733$action='project_list'; 734} 735} 736if(!defined($actions{$action})) { 737 die_error(400,"Unknown action"); 738} 739if($action!~m/^(opml|project_list|project_index)$/&& 740!$project) { 741 die_error(400,"Project needed"); 742} 743$actions{$action}->(); 744exit; 745 746## ====================================================================== 747## action links 748 749sub href (%) { 750my%params=@_; 751# default is to use -absolute url() i.e. $my_uri 752my$href=$params{-full} ?$my_url:$my_uri; 753 754$params{'project'} =$projectunlessexists$params{'project'}; 755 756if($params{-replay}) { 757while(my($name,$symbol) =each%cgi_param_mapping) { 758if(!exists$params{$name}) { 759$params{$name} =$input_params{$name}; 760} 761} 762} 763 764my($use_pathinfo) = gitweb_check_feature('pathinfo'); 765if($use_pathinfo) { 766# try to put as many parameters as possible in PATH_INFO: 767# - project name 768# - action 769# - hash or hash_base:/filename 770 771# When the script is the root DirectoryIndex for the domain, 772# $href here would be something like http://gitweb.example.com/ 773# Thus, we strip any trailing / from $href, to spare us double 774# slashes in the final URL 775$href=~ s,/$,,; 776 777# Then add the project name, if present 778$href.="/".esc_url($params{'project'})ifdefined$params{'project'}; 779delete$params{'project'}; 780 781# Summary just uses the project path URL, any other action is 782# added to the URL 783if(defined$params{'action'}) { 784$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 785delete$params{'action'}; 786} 787 788# Finally, we put either hash_base:/file_name or hash 789if(defined$params{'hash_base'}) { 790$href.="/".esc_url($params{'hash_base'}); 791if(defined$params{'file_name'}) { 792$href.=":/".esc_url($params{'file_name'}); 793delete$params{'file_name'}; 794} 795delete$params{'hash'}; 796delete$params{'hash_base'}; 797}elsif(defined$params{'hash'}) { 798$href.="/".esc_url($params{'hash'}); 799delete$params{'hash'}; 800} 801} 802 803# now encode the parameters explicitly 804my@result= (); 805for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 806my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 807if(defined$params{$name}) { 808if(ref($params{$name})eq"ARRAY") { 809foreachmy$par(@{$params{$name}}) { 810push@result,$symbol."=". esc_param($par); 811} 812}else{ 813push@result,$symbol."=". esc_param($params{$name}); 814} 815} 816} 817$href.="?".join(';',@result)ifscalar@result; 818 819return$href; 820} 821 822 823## ====================================================================== 824## validation, quoting/unquoting and escaping 825 826sub validate_action { 827my$input=shift||returnundef; 828returnundefunlessexists$actions{$input}; 829return$input; 830} 831 832sub validate_project { 833my$input=shift||returnundef; 834if(!validate_pathname($input) || 835!(-d "$projectroot/$input") || 836!check_head_link("$projectroot/$input") || 837($export_ok&& !(-e "$projectroot/$input/$export_ok")) || 838($strict_export&& !project_in_list($input))) { 839returnundef; 840}else{ 841return$input; 842} 843} 844 845sub validate_pathname { 846my$input=shift||returnundef; 847 848# no '.' or '..' as elements of path, i.e. no '.' nor '..' 849# at the beginning, at the end, and between slashes. 850# also this catches doubled slashes 851if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 852returnundef; 853} 854# no null characters 855if($input=~m!\0!) { 856returnundef; 857} 858return$input; 859} 860 861sub validate_refname { 862my$input=shift||returnundef; 863 864# textual hashes are O.K. 865if($input=~m/^[0-9a-fA-F]{40}$/) { 866return$input; 867} 868# it must be correct pathname 869$input= validate_pathname($input) 870orreturnundef; 871# restrictions on ref name according to git-check-ref-format 872if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) { 873returnundef; 874} 875return$input; 876} 877 878# decode sequences of octets in utf8 into Perl's internal form, 879# which is utf-8 with utf8 flag set if needed. gitweb writes out 880# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning 881sub to_utf8 { 882my$str=shift; 883if(utf8::valid($str)) { 884 utf8::decode($str); 885return$str; 886}else{ 887return decode($fallback_encoding,$str, Encode::FB_DEFAULT); 888} 889} 890 891# quote unsafe chars, but keep the slash, even when it's not 892# correct, but quoted slashes look too horrible in bookmarks 893sub esc_param { 894my$str=shift; 895$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg; 896$str=~s/\+/%2B/g; 897$str=~s/ /\+/g; 898return$str; 899} 900 901# quote unsafe chars in whole URL, so some charactrs cannot be quoted 902sub esc_url { 903my$str=shift; 904$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg; 905$str=~s/\+/%2B/g; 906$str=~s/ /\+/g; 907return$str; 908} 909 910# replace invalid utf8 character with SUBSTITUTION sequence 911sub esc_html ($;%) { 912my$str=shift; 913my%opts=@_; 914 915$str= to_utf8($str); 916$str=$cgi->escapeHTML($str); 917if($opts{'-nbsp'}) { 918$str=~s/ / /g; 919} 920$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg; 921return$str; 922} 923 924# quote control characters and escape filename to HTML 925sub esc_path { 926my$str=shift; 927my%opts=@_; 928 929$str= to_utf8($str); 930$str=$cgi->escapeHTML($str); 931if($opts{'-nbsp'}) { 932$str=~s/ / /g; 933} 934$str=~ s|([[:cntrl:]])|quot_cec($1)|eg; 935return$str; 936} 937 938# Make control characters "printable", using character escape codes (CEC) 939sub quot_cec { 940my$cntrl=shift; 941my%opts=@_; 942my%es= (# character escape codes, aka escape sequences 943"\t"=>'\t',# tab (HT) 944"\n"=>'\n',# line feed (LF) 945"\r"=>'\r',# carrige return (CR) 946"\f"=>'\f',# form feed (FF) 947"\b"=>'\b',# backspace (BS) 948"\a"=>'\a',# alarm (bell) (BEL) 949"\e"=>'\e',# escape (ESC) 950"\013"=>'\v',# vertical tab (VT) 951"\000"=>'\0',# nul character (NUL) 952); 953my$chr= ( (exists$es{$cntrl}) 954?$es{$cntrl} 955:sprintf('\%2x',ord($cntrl)) ); 956if($opts{-nohtml}) { 957return$chr; 958}else{ 959return"<span class=\"cntrl\">$chr</span>"; 960} 961} 962 963# Alternatively use unicode control pictures codepoints, 964# Unicode "printable representation" (PR) 965sub quot_upr { 966my$cntrl=shift; 967my%opts=@_; 968 969my$chr=sprintf('&#%04d;',0x2400+ord($cntrl)); 970if($opts{-nohtml}) { 971return$chr; 972}else{ 973return"<span class=\"cntrl\">$chr</span>"; 974} 975} 976 977# git may return quoted and escaped filenames 978sub unquote { 979my$str=shift; 980 981sub unq { 982my$seq=shift; 983my%es= (# character escape codes, aka escape sequences 984't'=>"\t",# tab (HT, TAB) 985'n'=>"\n",# newline (NL) 986'r'=>"\r",# return (CR) 987'f'=>"\f",# form feed (FF) 988'b'=>"\b",# backspace (BS) 989'a'=>"\a",# alarm (bell) (BEL) 990'e'=>"\e",# escape (ESC) 991'v'=>"\013",# vertical tab (VT) 992); 993 994if($seq=~m/^[0-7]{1,3}$/) { 995# octal char sequence 996returnchr(oct($seq)); 997}elsif(exists$es{$seq}) { 998# C escape sequence, aka character escape code 999return$es{$seq};1000}1001# quoted ordinary character1002return$seq;1003}10041005if($str=~m/^"(.*)"$/) {1006# needs unquoting1007$str=$1;1008$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1009}1010return$str;1011}10121013# escape tabs (convert tabs to spaces)1014sub untabify {1015my$line=shift;10161017while((my$pos=index($line,"\t")) != -1) {1018if(my$count= (8- ($pos%8))) {1019my$spaces=' ' x $count;1020$line=~s/\t/$spaces/;1021}1022}10231024return$line;1025}10261027sub project_in_list {1028my$project=shift;1029my@list= git_get_projects_list();1030return@list&&scalar(grep{$_->{'path'}eq$project}@list);1031}10321033## ----------------------------------------------------------------------1034## HTML aware string manipulation10351036# Try to chop given string on a word boundary between position1037# $len and $len+$add_len. If there is no word boundary there,1038# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1039# (marking chopped part) would be longer than given string.1040sub chop_str {1041my$str=shift;1042my$len=shift;1043my$add_len=shift||10;1044my$where=shift||'right';# 'left' | 'center' | 'right'10451046# Make sure perl knows it is utf8 encoded so we don't1047# cut in the middle of a utf8 multibyte char.1048$str= to_utf8($str);10491050# allow only $len chars, but don't cut a word if it would fit in $add_len1051# if it doesn't fit, cut it if it's still longer than the dots we would add1052# remove chopped character entities entirely10531054# when chopping in the middle, distribute $len into left and right part1055# return early if chopping wouldn't make string shorter1056if($whereeq'center') {1057return$strif($len+5>=length($str));# filler is length 51058$len=int($len/2);1059}else{1060return$strif($len+4>=length($str));# filler is length 41061}10621063# regexps: ending and beginning with word part up to $add_len1064my$endre=qr/.{$len}\w{0,$add_len}/;1065my$begre=qr/\w{0,$add_len}.{$len}/;10661067if($whereeq'left') {1068$str=~m/^(.*?)($begre)$/;1069my($lead,$body) = ($1,$2);1070if(length($lead) >4) {1071$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1072$lead=" ...";1073}1074return"$lead$body";10751076}elsif($whereeq'center') {1077$str=~m/^($endre)(.*)$/;1078my($left,$str) = ($1,$2);1079$str=~m/^(.*?)($begre)$/;1080my($mid,$right) = ($1,$2);1081if(length($mid) >5) {1082$left=~s/&[^;]*$//;1083$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1084$mid=" ... ";1085}1086return"$left$mid$right";10871088}else{1089$str=~m/^($endre)(.*)$/;1090my$body=$1;1091my$tail=$2;1092if(length($tail) >4) {1093$body=~s/&[^;]*$//;1094$tail="... ";1095}1096return"$body$tail";1097}1098}10991100# takes the same arguments as chop_str, but also wraps a <span> around the1101# result with a title attribute if it does get chopped. Additionally, the1102# string is HTML-escaped.1103sub chop_and_escape_str {1104my($str) =@_;11051106my$chopped= chop_str(@_);1107if($choppedeq$str) {1108return esc_html($chopped);1109}else{1110$str=~s/([[:cntrl:]])/?/g;1111return$cgi->span({-title=>$str}, esc_html($chopped));1112}1113}11141115## ----------------------------------------------------------------------1116## functions returning short strings11171118# CSS class for given age value (in seconds)1119sub age_class {1120my$age=shift;11211122if(!defined$age) {1123return"noage";1124}elsif($age<60*60*2) {1125return"age0";1126}elsif($age<60*60*24*2) {1127return"age1";1128}else{1129return"age2";1130}1131}11321133# convert age in seconds to "nn units ago" string1134sub age_string {1135my$age=shift;1136my$age_str;11371138if($age>60*60*24*365*2) {1139$age_str= (int$age/60/60/24/365);1140$age_str.=" years ago";1141}elsif($age>60*60*24*(365/12)*2) {1142$age_str=int$age/60/60/24/(365/12);1143$age_str.=" months ago";1144}elsif($age>60*60*24*7*2) {1145$age_str=int$age/60/60/24/7;1146$age_str.=" weeks ago";1147}elsif($age>60*60*24*2) {1148$age_str=int$age/60/60/24;1149$age_str.=" days ago";1150}elsif($age>60*60*2) {1151$age_str=int$age/60/60;1152$age_str.=" hours ago";1153}elsif($age>60*2) {1154$age_str=int$age/60;1155$age_str.=" min ago";1156}elsif($age>2) {1157$age_str=int$age;1158$age_str.=" sec ago";1159}else{1160$age_str.=" right now";1161}1162return$age_str;1163}11641165useconstant{1166 S_IFINVALID =>0030000,1167 S_IFGITLINK =>0160000,1168};11691170# submodule/subproject, a commit object reference1171sub S_ISGITLINK($) {1172my$mode=shift;11731174return(($mode& S_IFMT) == S_IFGITLINK)1175}11761177# convert file mode in octal to symbolic file mode string1178sub mode_str {1179my$mode=oct shift;11801181if(S_ISGITLINK($mode)) {1182return'm---------';1183}elsif(S_ISDIR($mode& S_IFMT)) {1184return'drwxr-xr-x';1185}elsif(S_ISLNK($mode)) {1186return'lrwxrwxrwx';1187}elsif(S_ISREG($mode)) {1188# git cares only about the executable bit1189if($mode& S_IXUSR) {1190return'-rwxr-xr-x';1191}else{1192return'-rw-r--r--';1193};1194}else{1195return'----------';1196}1197}11981199# convert file mode in octal to file type string1200sub file_type {1201my$mode=shift;12021203if($mode!~m/^[0-7]+$/) {1204return$mode;1205}else{1206$mode=oct$mode;1207}12081209if(S_ISGITLINK($mode)) {1210return"submodule";1211}elsif(S_ISDIR($mode& S_IFMT)) {1212return"directory";1213}elsif(S_ISLNK($mode)) {1214return"symlink";1215}elsif(S_ISREG($mode)) {1216return"file";1217}else{1218return"unknown";1219}1220}12211222# convert file mode in octal to file type description string1223sub file_type_long {1224my$mode=shift;12251226if($mode!~m/^[0-7]+$/) {1227return$mode;1228}else{1229$mode=oct$mode;1230}12311232if(S_ISGITLINK($mode)) {1233return"submodule";1234}elsif(S_ISDIR($mode& S_IFMT)) {1235return"directory";1236}elsif(S_ISLNK($mode)) {1237return"symlink";1238}elsif(S_ISREG($mode)) {1239if($mode& S_IXUSR) {1240return"executable";1241}else{1242return"file";1243};1244}else{1245return"unknown";1246}1247}124812491250## ----------------------------------------------------------------------1251## functions returning short HTML fragments, or transforming HTML fragments1252## which don't belong to other sections12531254# format line of commit message.1255sub format_log_line_html {1256my$line=shift;12571258$line= esc_html($line, -nbsp=>1);1259if($line=~m/([0-9a-fA-F]{8,40})/) {1260my$hash_text=$1;1261my$link=1262$cgi->a({-href => href(action=>"object", hash=>$hash_text),1263-class=>"text"},$hash_text);1264$line=~s/$hash_text/$link/;1265}1266return$line;1267}12681269# format marker of refs pointing to given object12701271# the destination action is chosen based on object type and current context:1272# - for annotated tags, we choose the tag view unless it's the current view1273# already, in which case we go to shortlog view1274# - for other refs, we keep the current view if we're in history, shortlog or1275# log view, and select shortlog otherwise1276sub format_ref_marker {1277my($refs,$id) =@_;1278my$markers='';12791280if(defined$refs->{$id}) {1281foreachmy$ref(@{$refs->{$id}}) {1282# this code exploits the fact that non-lightweight tags are the1283# only indirect objects, and that they are the only objects for which1284# we want to use tag instead of shortlog as action1285my($type,$name) =qw();1286my$indirect= ($ref=~s/\^\{\}$//);1287# e.g. tags/v2.6.11 or heads/next1288if($ref=~m!^(.*?)s?/(.*)$!) {1289$type=$1;1290$name=$2;1291}else{1292$type="ref";1293$name=$ref;1294}12951296my$class=$type;1297$class.=" indirect"if$indirect;12981299my$dest_action="shortlog";13001301if($indirect) {1302$dest_action="tag"unless$actioneq"tag";1303}elsif($action=~/^(history|(short)?log)$/) {1304$dest_action=$action;1305}13061307my$dest="";1308$dest.="refs/"unless$ref=~ m!^refs/!;1309$dest.=$ref;13101311my$link=$cgi->a({1312-href => href(1313 action=>$dest_action,1314 hash=>$dest1315)},$name);13161317$markers.=" <span class=\"$class\"title=\"$ref\">".1318$link."</span>";1319}1320}13211322if($markers) {1323return' <span class="refs">'.$markers.'</span>';1324}else{1325return"";1326}1327}13281329# format, perhaps shortened and with markers, title line1330sub format_subject_html {1331my($long,$short,$href,$extra) =@_;1332$extra=''unlessdefined($extra);13331334if(length($short) <length($long)) {1335return$cgi->a({-href =>$href, -class=>"list subject",1336-title => to_utf8($long)},1337 esc_html($short) .$extra);1338}else{1339return$cgi->a({-href =>$href, -class=>"list subject"},1340 esc_html($long) .$extra);1341}1342}13431344# format git diff header line, i.e. "diff --(git|combined|cc) ..."1345sub format_git_diff_header_line {1346my$line=shift;1347my$diffinfo=shift;1348my($from,$to) =@_;13491350if($diffinfo->{'nparents'}) {1351# combined diff1352$line=~s!^(diff (.*?) )"?.*$!$1!;1353if($to->{'href'}) {1354$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1355 esc_path($to->{'file'}));1356}else{# file was deleted (no href)1357$line.= esc_path($to->{'file'});1358}1359}else{1360# "ordinary" diff1361$line=~s!^(diff (.*?) )"?a/.*$!$1!;1362if($from->{'href'}) {1363$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1364'a/'. esc_path($from->{'file'}));1365}else{# file was added (no href)1366$line.='a/'. esc_path($from->{'file'});1367}1368$line.=' ';1369if($to->{'href'}) {1370$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1371'b/'. esc_path($to->{'file'}));1372}else{# file was deleted1373$line.='b/'. esc_path($to->{'file'});1374}1375}13761377return"<div class=\"diff header\">$line</div>\n";1378}13791380# format extended diff header line, before patch itself1381sub format_extended_diff_header_line {1382my$line=shift;1383my$diffinfo=shift;1384my($from,$to) =@_;13851386# match <path>1387if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1388$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1389 esc_path($from->{'file'}));1390}1391if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1392$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1393 esc_path($to->{'file'}));1394}1395# match single <mode>1396if($line=~m/\s(\d{6})$/) {1397$line.='<span class="info"> ('.1398 file_type_long($1) .1399')</span>';1400}1401# match <hash>1402if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1403# can match only for combined diff1404$line='index ';1405for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1406if($from->{'href'}[$i]) {1407$line.=$cgi->a({-href=>$from->{'href'}[$i],1408-class=>"hash"},1409substr($diffinfo->{'from_id'}[$i],0,7));1410}else{1411$line.='0' x 7;1412}1413# separator1414$line.=','if($i<$diffinfo->{'nparents'} -1);1415}1416$line.='..';1417if($to->{'href'}) {1418$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1419substr($diffinfo->{'to_id'},0,7));1420}else{1421$line.='0' x 7;1422}14231424}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1425# can match only for ordinary diff1426my($from_link,$to_link);1427if($from->{'href'}) {1428$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1429substr($diffinfo->{'from_id'},0,7));1430}else{1431$from_link='0' x 7;1432}1433if($to->{'href'}) {1434$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1435substr($diffinfo->{'to_id'},0,7));1436}else{1437$to_link='0' x 7;1438}1439my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1440$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1441}14421443return$line."<br/>\n";1444}14451446# format from-file/to-file diff header1447sub format_diff_from_to_header {1448my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1449my$line;1450my$result='';14511452$line=$from_line;1453#assert($line =~ m/^---/) if DEBUG;1454# no extra formatting for "^--- /dev/null"1455if(!$diffinfo->{'nparents'}) {1456# ordinary (single parent) diff1457if($line=~m!^--- "?a/!) {1458if($from->{'href'}) {1459$line='--- a/'.1460$cgi->a({-href=>$from->{'href'}, -class=>"path"},1461 esc_path($from->{'file'}));1462}else{1463$line='--- a/'.1464 esc_path($from->{'file'});1465}1466}1467$result.= qq!<div class="diff from_file">$line</div>\n!;14681469}else{1470# combined diff (merge commit)1471for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1472if($from->{'href'}[$i]) {1473$line='--- '.1474$cgi->a({-href=>href(action=>"blobdiff",1475 hash_parent=>$diffinfo->{'from_id'}[$i],1476 hash_parent_base=>$parents[$i],1477 file_parent=>$from->{'file'}[$i],1478 hash=>$diffinfo->{'to_id'},1479 hash_base=>$hash,1480 file_name=>$to->{'file'}),1481-class=>"path",1482-title=>"diff". ($i+1)},1483$i+1) .1484'/'.1485$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1486 esc_path($from->{'file'}[$i]));1487}else{1488$line='--- /dev/null';1489}1490$result.= qq!<div class="diff from_file">$line</div>\n!;1491}1492}14931494$line=$to_line;1495#assert($line =~ m/^\+\+\+/) if DEBUG;1496# no extra formatting for "^+++ /dev/null"1497if($line=~m!^\+\+\+ "?b/!) {1498if($to->{'href'}) {1499$line='+++ b/'.1500$cgi->a({-href=>$to->{'href'}, -class=>"path"},1501 esc_path($to->{'file'}));1502}else{1503$line='+++ b/'.1504 esc_path($to->{'file'});1505}1506}1507$result.= qq!<div class="diff to_file">$line</div>\n!;15081509return$result;1510}15111512# create note for patch simplified by combined diff1513sub format_diff_cc_simplified {1514my($diffinfo,@parents) =@_;1515my$result='';15161517$result.="<div class=\"diff header\">".1518"diff --cc ";1519if(!is_deleted($diffinfo)) {1520$result.=$cgi->a({-href => href(action=>"blob",1521 hash_base=>$hash,1522 hash=>$diffinfo->{'to_id'},1523 file_name=>$diffinfo->{'to_file'}),1524-class=>"path"},1525 esc_path($diffinfo->{'to_file'}));1526}else{1527$result.= esc_path($diffinfo->{'to_file'});1528}1529$result.="</div>\n".# class="diff header"1530"<div class=\"diff nodifferences\">".1531"Simple merge".1532"</div>\n";# class="diff nodifferences"15331534return$result;1535}15361537# format patch (diff) line (not to be used for diff headers)1538sub format_diff_line {1539my$line=shift;1540my($from,$to) =@_;1541my$diff_class="";15421543chomp$line;15441545if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1546# combined diff1547my$prefix=substr($line,0,scalar@{$from->{'href'}});1548if($line=~m/^\@{3}/) {1549$diff_class=" chunk_header";1550}elsif($line=~m/^\\/) {1551$diff_class=" incomplete";1552}elsif($prefix=~tr/+/+/) {1553$diff_class=" add";1554}elsif($prefix=~tr/-/-/) {1555$diff_class=" rem";1556}1557}else{1558# assume ordinary diff1559my$char=substr($line,0,1);1560if($chareq'+') {1561$diff_class=" add";1562}elsif($chareq'-') {1563$diff_class=" rem";1564}elsif($chareq'@') {1565$diff_class=" chunk_header";1566}elsif($chareq"\\") {1567$diff_class=" incomplete";1568}1569}1570$line= untabify($line);1571if($from&&$to&&$line=~m/^\@{2} /) {1572my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1573$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;15741575$from_lines=0unlessdefined$from_lines;1576$to_lines=0unlessdefined$to_lines;15771578if($from->{'href'}) {1579$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1580-class=>"list"},$from_text);1581}1582if($to->{'href'}) {1583$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1584-class=>"list"},$to_text);1585}1586$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1587"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1588return"<div class=\"diff$diff_class\">$line</div>\n";1589}elsif($from&&$to&&$line=~m/^\@{3}/) {1590my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1591my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);15921593@from_text=split(' ',$ranges);1594for(my$i=0;$i<@from_text; ++$i) {1595($from_start[$i],$from_nlines[$i]) =1596(split(',',substr($from_text[$i],1)),0);1597}15981599$to_text=pop@from_text;1600$to_start=pop@from_start;1601$to_nlines=pop@from_nlines;16021603$line="<span class=\"chunk_info\">$prefix";1604for(my$i=0;$i<@from_text; ++$i) {1605if($from->{'href'}[$i]) {1606$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1607-class=>"list"},$from_text[$i]);1608}else{1609$line.=$from_text[$i];1610}1611$line.=" ";1612}1613if($to->{'href'}) {1614$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1615-class=>"list"},$to_text);1616}else{1617$line.=$to_text;1618}1619$line.="$prefix</span>".1620"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1621return"<div class=\"diff$diff_class\">$line</div>\n";1622}1623return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1624}16251626# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1627# linked. Pass the hash of the tree/commit to snapshot.1628sub format_snapshot_links {1629my($hash) =@_;1630my@snapshot_fmts= gitweb_check_feature('snapshot');1631@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);1632my$num_fmts=@snapshot_fmts;1633if($num_fmts>1) {1634# A parenthesized list of links bearing format names.1635# e.g. "snapshot (_tar.gz_ _zip_)"1636return"snapshot (".join(' ',map1637$cgi->a({1638-href => href(1639 action=>"snapshot",1640 hash=>$hash,1641 snapshot_format=>$_1642)1643},$known_snapshot_formats{$_}{'display'})1644,@snapshot_fmts) .")";1645}elsif($num_fmts==1) {1646# A single "snapshot" link whose tooltip bears the format name.1647# i.e. "_snapshot_"1648my($fmt) =@snapshot_fmts;1649return1650$cgi->a({1651-href => href(1652 action=>"snapshot",1653 hash=>$hash,1654 snapshot_format=>$fmt1655),1656-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1657},"snapshot");1658}else{# $num_fmts == 01659returnundef;1660}1661}16621663## ......................................................................1664## functions returning values to be passed, perhaps after some1665## transformation, to other functions; e.g. returning arguments to href()16661667# returns hash to be passed to href to generate gitweb URL1668# in -title key it returns description of link1669sub get_feed_info {1670my$format=shift||'Atom';1671my%res= (action =>lc($format));16721673# feed links are possible only for project views1674return unless(defined$project);1675# some views should link to OPML, or to generic project feed,1676# or don't have specific feed yet (so they should use generic)1677return if($action=~/^(?:tags|heads|forks|tag|search)$/x);16781679my$branch;1680# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1681# from tag links; this also makes possible to detect branch links1682if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1683(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1684$branch=$1;1685}1686# find log type for feed description (title)1687my$type='log';1688if(defined$file_name) {1689$type="history of$file_name";1690$type.="/"if($actioneq'tree');1691$type.=" on '$branch'"if(defined$branch);1692}else{1693$type="log of$branch"if(defined$branch);1694}16951696$res{-title} =$type;1697$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1698$res{'file_name'} =$file_name;16991700return%res;1701}17021703## ----------------------------------------------------------------------1704## git utility subroutines, invoking git commands17051706# returns path to the core git executable and the --git-dir parameter as list1707sub git_cmd {1708return$GIT,'--git-dir='.$git_dir;1709}17101711# quote the given arguments for passing them to the shell1712# quote_command("command", "arg 1", "arg with ' and ! characters")1713# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1714# Try to avoid using this function wherever possible.1715sub quote_command {1716returnjoin(' ',1717map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1718}17191720# get HEAD ref of given project as hash1721sub git_get_head_hash {1722my$project=shift;1723my$o_git_dir=$git_dir;1724my$retval=undef;1725$git_dir="$projectroot/$project";1726if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1727my$head= <$fd>;1728close$fd;1729if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1730$retval=$1;1731}1732}1733if(defined$o_git_dir) {1734$git_dir=$o_git_dir;1735}1736return$retval;1737}17381739# get type of given object1740sub git_get_type {1741my$hash=shift;17421743open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1744my$type= <$fd>;1745close$fdorreturn;1746chomp$type;1747return$type;1748}17491750# repository configuration1751our$config_file='';1752our%config;17531754# store multiple values for single key as anonymous array reference1755# single values stored directly in the hash, not as [ <value> ]1756sub hash_set_multi {1757my($hash,$key,$value) =@_;17581759if(!exists$hash->{$key}) {1760$hash->{$key} =$value;1761}elsif(!ref$hash->{$key}) {1762$hash->{$key} = [$hash->{$key},$value];1763}else{1764push@{$hash->{$key}},$value;1765}1766}17671768# return hash of git project configuration1769# optionally limited to some section, e.g. 'gitweb'1770sub git_parse_project_config {1771my$section_regexp=shift;1772my%config;17731774local$/="\0";17751776open my$fh,"-|", git_cmd(),"config",'-z','-l',1777orreturn;17781779while(my$keyval= <$fh>) {1780chomp$keyval;1781my($key,$value) =split(/\n/,$keyval,2);17821783 hash_set_multi(\%config,$key,$value)1784if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1785}1786close$fh;17871788return%config;1789}17901791# convert config value to boolean, 'true' or 'false'1792# no value, number > 0, 'true' and 'yes' values are true1793# rest of values are treated as false (never as error)1794sub config_to_bool {1795my$val=shift;17961797# strip leading and trailing whitespace1798$val=~s/^\s+//;1799$val=~s/\s+$//;18001801return(!defined$val||# section.key1802($val=~/^\d+$/&&$val) ||# section.key = 11803($val=~/^(?:true|yes)$/i));# section.key = true1804}18051806# convert config value to simple decimal number1807# an optional value suffix of 'k', 'm', or 'g' will cause the value1808# to be multiplied by 1024, 1048576, or 10737418241809sub config_to_int {1810my$val=shift;18111812# strip leading and trailing whitespace1813$val=~s/^\s+//;1814$val=~s/\s+$//;18151816if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1817$unit=lc($unit);1818# unknown unit is treated as 11819return$num* ($uniteq'g'?1073741824:1820$uniteq'm'?1048576:1821$uniteq'k'?1024:1);1822}1823return$val;1824}18251826# convert config value to array reference, if needed1827sub config_to_multi {1828my$val=shift;18291830returnref($val) ?$val: (defined($val) ? [$val] : []);1831}18321833sub git_get_project_config {1834my($key,$type) =@_;18351836# key sanity check1837return unless($key);1838$key=~s/^gitweb\.//;1839return if($key=~m/\W/);18401841# type sanity check1842if(defined$type) {1843$type=~s/^--//;1844$type=undef1845unless($typeeq'bool'||$typeeq'int');1846}18471848# get config1849if(!defined$config_file||1850$config_filene"$git_dir/config") {1851%config= git_parse_project_config('gitweb');1852$config_file="$git_dir/config";1853}18541855# ensure given type1856if(!defined$type) {1857return$config{"gitweb.$key"};1858}elsif($typeeq'bool') {1859# backward compatibility: 'git config --bool' returns true/false1860return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1861}elsif($typeeq'int') {1862return config_to_int($config{"gitweb.$key"});1863}1864return$config{"gitweb.$key"};1865}18661867# get hash of given path at given ref1868sub git_get_hash_by_path {1869my$base=shift;1870my$path=shift||returnundef;1871my$type=shift;18721873$path=~ s,/+$,,;18741875open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path1876or die_error(500,"Open git-ls-tree failed");1877my$line= <$fd>;1878close$fdorreturnundef;18791880if(!defined$line) {1881# there is no tree or hash given by $path at $base1882returnundef;1883}18841885#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'1886$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;1887if(defined$type&&$typene$2) {1888# type doesn't match1889returnundef;1890}1891return$3;1892}18931894# get path of entry with given hash at given tree-ish (ref)1895# used to get 'from' filename for combined diff (merge commit) for renames1896sub git_get_path_by_hash {1897my$base=shift||return;1898my$hash=shift||return;18991900local$/="\0";19011902open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base1903orreturnundef;1904while(my$line= <$fd>) {1905chomp$line;19061907#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'1908#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'1909if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {1910close$fd;1911return$1;1912}1913}1914close$fd;1915returnundef;1916}19171918## ......................................................................1919## git utility functions, directly accessing git repository19201921sub git_get_project_description {1922my$path=shift;19231924$git_dir="$projectroot/$path";1925open my$fd,"$git_dir/description"1926orreturn git_get_project_config('description');1927my$descr= <$fd>;1928close$fd;1929if(defined$descr) {1930chomp$descr;1931}1932return$descr;1933}19341935sub git_get_project_ctags {1936my$path=shift;1937my$ctags= {};19381939$git_dir="$projectroot/$path";1940foreach(<$git_dir/ctags/*>) {1941open CT,$_ornext;1942my$val= <CT>;1943chomp$val;1944close CT;1945my$ctag=$_;$ctag=~ s#.*/##;1946$ctags->{$ctag} =$val;1947}1948$ctags;1949}19501951sub git_populate_project_tagcloud {1952my$ctags=shift;19531954# First, merge different-cased tags; tags vote on casing1955my%ctags_lc;1956foreach(keys%$ctags) {1957$ctags_lc{lc$_}->{count} +=$ctags->{$_};1958if(not$ctags_lc{lc$_}->{topcount}1959or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {1960$ctags_lc{lc$_}->{topcount} =$ctags->{$_};1961$ctags_lc{lc$_}->{topname} =$_;1962}1963}19641965my$cloud;1966if(eval{require HTML::TagCloud;1; }) {1967$cloud= HTML::TagCloud->new;1968foreach(sort keys%ctags_lc) {1969# Pad the title with spaces so that the cloud looks1970# less crammed.1971my$title=$ctags_lc{$_}->{topname};1972$title=~s/ / /g;1973$title=~s/^/ /g;1974$title=~s/$/ /g;1975$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});1976}1977}else{1978$cloud= \%ctags_lc;1979}1980$cloud;1981}19821983sub git_show_project_tagcloud {1984my($cloud,$count) =@_;1985print STDERR ref($cloud)."..\n";1986if(ref$cloudeq'HTML::TagCloud') {1987return$cloud->html_and_css($count);1988}else{1989my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;1990return'<p align="center">'.join(', ',map{1991"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"1992}splice(@tags,0,$count)) .'</p>';1993}1994}19951996sub git_get_project_url_list {1997my$path=shift;19981999$git_dir="$projectroot/$path";2000open my$fd,"$git_dir/cloneurl"2001orreturnwantarray?2002@{ config_to_multi(git_get_project_config('url')) } :2003 config_to_multi(git_get_project_config('url'));2004my@git_project_url_list=map{chomp;$_} <$fd>;2005close$fd;20062007returnwantarray?@git_project_url_list: \@git_project_url_list;2008}20092010sub git_get_projects_list {2011my($filter) =@_;2012my@list;20132014$filter||='';2015$filter=~s/\.git$//;20162017my($check_forks) = gitweb_check_feature('forks');20182019if(-d $projects_list) {2020# search in directory2021my$dir=$projects_list. ($filter?"/$filter":'');2022# remove the trailing "/"2023$dir=~s!/+$!!;2024my$pfxlen=length("$dir");2025my$pfxdepth= ($dir=~tr!/!!);20262027 File::Find::find({2028 follow_fast =>1,# follow symbolic links2029 follow_skip =>2,# ignore duplicates2030 dangling_symlinks =>0,# ignore dangling symlinks, silently2031 wanted =>sub{2032# skip project-list toplevel, if we get it.2033return if(m!^[/.]$!);2034# only directories can be git repositories2035return unless(-d $_);2036# don't traverse too deep (Find is super slow on os x)2037if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2038$File::Find::prune =1;2039return;2040}20412042my$subdir=substr($File::Find::name,$pfxlen+1);2043# we check related file in $projectroot2044if(check_export_ok("$projectroot/$filter/$subdir")) {2045push@list, { path => ($filter?"$filter/":'') .$subdir};2046$File::Find::prune =1;2047}2048},2049},"$dir");20502051}elsif(-f $projects_list) {2052# read from file(url-encoded):2053# 'git%2Fgit.git Linus+Torvalds'2054# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2055# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2056my%paths;2057open my($fd),$projects_listorreturn;2058 PROJECT:2059while(my$line= <$fd>) {2060chomp$line;2061my($path,$owner) =split' ',$line;2062$path= unescape($path);2063$owner= unescape($owner);2064if(!defined$path) {2065next;2066}2067if($filterne'') {2068# looking for forks;2069my$pfx=substr($path,0,length($filter));2070if($pfxne$filter) {2071next PROJECT;2072}2073my$sfx=substr($path,length($filter));2074if($sfx!~/^\/.*\.git$/) {2075next PROJECT;2076}2077}elsif($check_forks) {2078 PATH:2079foreachmy$filter(keys%paths) {2080# looking for forks;2081my$pfx=substr($path,0,length($filter));2082if($pfxne$filter) {2083next PATH;2084}2085my$sfx=substr($path,length($filter));2086if($sfx!~/^\/.*\.git$/) {2087next PATH;2088}2089# is a fork, don't include it in2090# the list2091next PROJECT;2092}2093}2094if(check_export_ok("$projectroot/$path")) {2095my$pr= {2096 path =>$path,2097 owner => to_utf8($owner),2098};2099push@list,$pr;2100(my$forks_path=$path) =~s/\.git$//;2101$paths{$forks_path}++;2102}2103}2104close$fd;2105}2106return@list;2107}21082109our$gitweb_project_owner=undef;2110sub git_get_project_list_from_file {21112112return if(defined$gitweb_project_owner);21132114$gitweb_project_owner= {};2115# read from file (url-encoded):2116# 'git%2Fgit.git Linus+Torvalds'2117# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2118# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2119if(-f $projects_list) {2120open(my$fd,$projects_list);2121while(my$line= <$fd>) {2122chomp$line;2123my($pr,$ow) =split' ',$line;2124$pr= unescape($pr);2125$ow= unescape($ow);2126$gitweb_project_owner->{$pr} = to_utf8($ow);2127}2128close$fd;2129}2130}21312132sub git_get_project_owner {2133my$project=shift;2134my$owner;21352136returnundefunless$project;2137$git_dir="$projectroot/$project";21382139if(!defined$gitweb_project_owner) {2140 git_get_project_list_from_file();2141}21422143if(exists$gitweb_project_owner->{$project}) {2144$owner=$gitweb_project_owner->{$project};2145}2146if(!defined$owner){2147$owner= git_get_project_config('owner');2148}2149if(!defined$owner) {2150$owner= get_file_owner("$git_dir");2151}21522153return$owner;2154}21552156sub git_get_last_activity {2157my($path) =@_;2158my$fd;21592160$git_dir="$projectroot/$path";2161open($fd,"-|", git_cmd(),'for-each-ref',2162'--format=%(committer)',2163'--sort=-committerdate',2164'--count=1',2165'refs/heads')orreturn;2166my$most_recent= <$fd>;2167close$fdorreturn;2168if(defined$most_recent&&2169$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2170my$timestamp=$1;2171my$age=time-$timestamp;2172return($age, age_string($age));2173}2174return(undef,undef);2175}21762177sub git_get_references {2178my$type=shift||"";2179my%refs;2180# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112181# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2182open my$fd,"-|", git_cmd(),"show-ref","--dereference",2183($type? ("--","refs/$type") : ())# use -- <pattern> if $type2184orreturn;21852186while(my$line= <$fd>) {2187chomp$line;2188if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2189if(defined$refs{$1}) {2190push@{$refs{$1}},$2;2191}else{2192$refs{$1} = [$2];2193}2194}2195}2196close$fdorreturn;2197return \%refs;2198}21992200sub git_get_rev_name_tags {2201my$hash=shift||returnundef;22022203open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2204orreturn;2205my$name_rev= <$fd>;2206close$fd;22072208if($name_rev=~ m|^$hash tags/(.*)$|) {2209return$1;2210}else{2211# catches also '$hash undefined' output2212returnundef;2213}2214}22152216## ----------------------------------------------------------------------2217## parse to hash functions22182219sub parse_date {2220my$epoch=shift;2221my$tz=shift||"-0000";22222223my%date;2224my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2225my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2226my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2227$date{'hour'} =$hour;2228$date{'minute'} =$min;2229$date{'mday'} =$mday;2230$date{'day'} =$days[$wday];2231$date{'month'} =$months[$mon];2232$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2233$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2234$date{'mday-time'} =sprintf"%d%s%02d:%02d",2235$mday,$months[$mon],$hour,$min;2236$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",22371900+$year,1+$mon,$mday,$hour,$min,$sec;22382239$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2240my$local=$epoch+ ((int$1+ ($2/60)) *3600);2241($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2242$date{'hour_local'} =$hour;2243$date{'minute_local'} =$min;2244$date{'tz_local'} =$tz;2245$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",22461900+$year,$mon+1,$mday,2247$hour,$min,$sec,$tz);2248return%date;2249}22502251sub parse_tag {2252my$tag_id=shift;2253my%tag;2254my@comment;22552256open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2257$tag{'id'} =$tag_id;2258while(my$line= <$fd>) {2259chomp$line;2260if($line=~m/^object ([0-9a-fA-F]{40})$/) {2261$tag{'object'} =$1;2262}elsif($line=~m/^type (.+)$/) {2263$tag{'type'} =$1;2264}elsif($line=~m/^tag (.+)$/) {2265$tag{'name'} =$1;2266}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2267$tag{'author'} =$1;2268$tag{'epoch'} =$2;2269$tag{'tz'} =$3;2270}elsif($line=~m/--BEGIN/) {2271push@comment,$line;2272last;2273}elsif($lineeq"") {2274last;2275}2276}2277push@comment, <$fd>;2278$tag{'comment'} = \@comment;2279close$fdorreturn;2280if(!defined$tag{'name'}) {2281return2282};2283return%tag2284}22852286sub parse_commit_text {2287my($commit_text,$withparents) =@_;2288my@commit_lines=split'\n',$commit_text;2289my%co;22902291pop@commit_lines;# Remove '\0'22922293if(!@commit_lines) {2294return;2295}22962297my$header=shift@commit_lines;2298if($header!~m/^[0-9a-fA-F]{40}/) {2299return;2300}2301($co{'id'},my@parents) =split' ',$header;2302while(my$line=shift@commit_lines) {2303last if$lineeq"\n";2304if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2305$co{'tree'} =$1;2306}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2307push@parents,$1;2308}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2309$co{'author'} =$1;2310$co{'author_epoch'} =$2;2311$co{'author_tz'} =$3;2312if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2313$co{'author_name'} =$1;2314$co{'author_email'} =$2;2315}else{2316$co{'author_name'} =$co{'author'};2317}2318}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2319$co{'committer'} =$1;2320$co{'committer_epoch'} =$2;2321$co{'committer_tz'} =$3;2322$co{'committer_name'} =$co{'committer'};2323if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2324$co{'committer_name'} =$1;2325$co{'committer_email'} =$2;2326}else{2327$co{'committer_name'} =$co{'committer'};2328}2329}2330}2331if(!defined$co{'tree'}) {2332return;2333};2334$co{'parents'} = \@parents;2335$co{'parent'} =$parents[0];23362337foreachmy$title(@commit_lines) {2338$title=~s/^ //;2339if($titlene"") {2340$co{'title'} = chop_str($title,80,5);2341# remove leading stuff of merges to make the interesting part visible2342if(length($title) >50) {2343$title=~s/^Automatic //;2344$title=~s/^merge (of|with) /Merge ... /i;2345if(length($title) >50) {2346$title=~s/(http|rsync):\/\///;2347}2348if(length($title) >50) {2349$title=~s/(master|www|rsync)\.//;2350}2351if(length($title) >50) {2352$title=~s/kernel.org:?//;2353}2354if(length($title) >50) {2355$title=~s/\/pub\/scm//;2356}2357}2358$co{'title_short'} = chop_str($title,50,5);2359last;2360}2361}2362if(!defined$co{'title'} ||$co{'title'}eq"") {2363$co{'title'} =$co{'title_short'} ='(no commit message)';2364}2365# remove added spaces2366foreachmy$line(@commit_lines) {2367$line=~s/^ //;2368}2369$co{'comment'} = \@commit_lines;23702371my$age=time-$co{'committer_epoch'};2372$co{'age'} =$age;2373$co{'age_string'} = age_string($age);2374my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2375if($age>60*60*24*7*2) {2376$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2377$co{'age_string_age'} =$co{'age_string'};2378}else{2379$co{'age_string_date'} =$co{'age_string'};2380$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2381}2382return%co;2383}23842385sub parse_commit {2386my($commit_id) =@_;2387my%co;23882389local$/="\0";23902391open my$fd,"-|", git_cmd(),"rev-list",2392"--parents",2393"--header",2394"--max-count=1",2395$commit_id,2396"--",2397or die_error(500,"Open git-rev-list failed");2398%co= parse_commit_text(<$fd>,1);2399close$fd;24002401return%co;2402}24032404sub parse_commits {2405my($commit_id,$maxcount,$skip,$filename,@args) =@_;2406my@cos;24072408$maxcount||=1;2409$skip||=0;24102411local$/="\0";24122413open my$fd,"-|", git_cmd(),"rev-list",2414"--header",2415@args,2416("--max-count=".$maxcount),2417("--skip=".$skip),2418@extra_options,2419$commit_id,2420"--",2421($filename? ($filename) : ())2422or die_error(500,"Open git-rev-list failed");2423while(my$line= <$fd>) {2424my%co= parse_commit_text($line);2425push@cos, \%co;2426}2427close$fd;24282429returnwantarray?@cos: \@cos;2430}24312432# parse line of git-diff-tree "raw" output2433sub parse_difftree_raw_line {2434my$line=shift;2435my%res;24362437# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2438# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2439if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2440$res{'from_mode'} =$1;2441$res{'to_mode'} =$2;2442$res{'from_id'} =$3;2443$res{'to_id'} =$4;2444$res{'status'} =$5;2445$res{'similarity'} =$6;2446if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2447($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2448}else{2449$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2450}2451}2452# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2453# combined diff (for merge commit)2454elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2455$res{'nparents'} =length($1);2456$res{'from_mode'} = [split(' ',$2) ];2457$res{'to_mode'} =pop@{$res{'from_mode'}};2458$res{'from_id'} = [split(' ',$3) ];2459$res{'to_id'} =pop@{$res{'from_id'}};2460$res{'status'} = [split('',$4) ];2461$res{'to_file'} = unquote($5);2462}2463# 'c512b523472485aef4fff9e57b229d9d243c967f'2464elsif($line=~m/^([0-9a-fA-F]{40})$/) {2465$res{'commit'} =$1;2466}24672468returnwantarray?%res: \%res;2469}24702471# wrapper: return parsed line of git-diff-tree "raw" output2472# (the argument might be raw line, or parsed info)2473sub parsed_difftree_line {2474my$line_or_ref=shift;24752476if(ref($line_or_ref)eq"HASH") {2477# pre-parsed (or generated by hand)2478return$line_or_ref;2479}else{2480return parse_difftree_raw_line($line_or_ref);2481}2482}24832484# parse line of git-ls-tree output2485sub parse_ls_tree_line ($;%) {2486my$line=shift;2487my%opts=@_;2488my%res;24892490#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2491$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;24922493$res{'mode'} =$1;2494$res{'type'} =$2;2495$res{'hash'} =$3;2496if($opts{'-z'}) {2497$res{'name'} =$4;2498}else{2499$res{'name'} = unquote($4);2500}25012502returnwantarray?%res: \%res;2503}25042505# generates _two_ hashes, references to which are passed as 2 and 3 argument2506sub parse_from_to_diffinfo {2507my($diffinfo,$from,$to,@parents) =@_;25082509if($diffinfo->{'nparents'}) {2510# combined diff2511$from->{'file'} = [];2512$from->{'href'} = [];2513 fill_from_file_info($diffinfo,@parents)2514unlessexists$diffinfo->{'from_file'};2515for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2516$from->{'file'}[$i] =2517defined$diffinfo->{'from_file'}[$i] ?2518$diffinfo->{'from_file'}[$i] :2519$diffinfo->{'to_file'};2520if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2521$from->{'href'}[$i] = href(action=>"blob",2522 hash_base=>$parents[$i],2523 hash=>$diffinfo->{'from_id'}[$i],2524 file_name=>$from->{'file'}[$i]);2525}else{2526$from->{'href'}[$i] =undef;2527}2528}2529}else{2530# ordinary (not combined) diff2531$from->{'file'} =$diffinfo->{'from_file'};2532if($diffinfo->{'status'}ne"A") {# not new (added) file2533$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2534 hash=>$diffinfo->{'from_id'},2535 file_name=>$from->{'file'});2536}else{2537delete$from->{'href'};2538}2539}25402541$to->{'file'} =$diffinfo->{'to_file'};2542if(!is_deleted($diffinfo)) {# file exists in result2543$to->{'href'} = href(action=>"blob", hash_base=>$hash,2544 hash=>$diffinfo->{'to_id'},2545 file_name=>$to->{'file'});2546}else{2547delete$to->{'href'};2548}2549}25502551## ......................................................................2552## parse to array of hashes functions25532554sub git_get_heads_list {2555my$limit=shift;2556my@headslist;25572558open my$fd,'-|', git_cmd(),'for-each-ref',2559($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2560'--format=%(objectname) %(refname) %(subject)%00%(committer)',2561'refs/heads'2562orreturn;2563while(my$line= <$fd>) {2564my%ref_item;25652566chomp$line;2567my($refinfo,$committerinfo) =split(/\0/,$line);2568my($hash,$name,$title) =split(' ',$refinfo,3);2569my($committer,$epoch,$tz) =2570($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2571$ref_item{'fullname'} =$name;2572$name=~s!^refs/heads/!!;25732574$ref_item{'name'} =$name;2575$ref_item{'id'} =$hash;2576$ref_item{'title'} =$title||'(no commit message)';2577$ref_item{'epoch'} =$epoch;2578if($epoch) {2579$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2580}else{2581$ref_item{'age'} ="unknown";2582}25832584push@headslist, \%ref_item;2585}2586close$fd;25872588returnwantarray?@headslist: \@headslist;2589}25902591sub git_get_tags_list {2592my$limit=shift;2593my@tagslist;25942595open my$fd,'-|', git_cmd(),'for-each-ref',2596($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2597'--format=%(objectname) %(objecttype) %(refname) '.2598'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2599'refs/tags'2600orreturn;2601while(my$line= <$fd>) {2602my%ref_item;26032604chomp$line;2605my($refinfo,$creatorinfo) =split(/\0/,$line);2606my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2607my($creator,$epoch,$tz) =2608($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2609$ref_item{'fullname'} =$name;2610$name=~s!^refs/tags/!!;26112612$ref_item{'type'} =$type;2613$ref_item{'id'} =$id;2614$ref_item{'name'} =$name;2615if($typeeq"tag") {2616$ref_item{'subject'} =$title;2617$ref_item{'reftype'} =$reftype;2618$ref_item{'refid'} =$refid;2619}else{2620$ref_item{'reftype'} =$type;2621$ref_item{'refid'} =$id;2622}26232624if($typeeq"tag"||$typeeq"commit") {2625$ref_item{'epoch'} =$epoch;2626if($epoch) {2627$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2628}else{2629$ref_item{'age'} ="unknown";2630}2631}26322633push@tagslist, \%ref_item;2634}2635close$fd;26362637returnwantarray?@tagslist: \@tagslist;2638}26392640## ----------------------------------------------------------------------2641## filesystem-related functions26422643sub get_file_owner {2644my$path=shift;26452646my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2647my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2648if(!defined$gcos) {2649returnundef;2650}2651my$owner=$gcos;2652$owner=~s/[,;].*$//;2653return to_utf8($owner);2654}26552656## ......................................................................2657## mimetype related functions26582659sub mimetype_guess_file {2660my$filename=shift;2661my$mimemap=shift;2662-r $mimemaporreturnundef;26632664my%mimemap;2665open(MIME,$mimemap)orreturnundef;2666while(<MIME>) {2667next ifm/^#/;# skip comments2668my($mime,$exts) =split(/\t+/);2669if(defined$exts) {2670my@exts=split(/\s+/,$exts);2671foreachmy$ext(@exts) {2672$mimemap{$ext} =$mime;2673}2674}2675}2676close(MIME);26772678$filename=~/\.([^.]*)$/;2679return$mimemap{$1};2680}26812682sub mimetype_guess {2683my$filename=shift;2684my$mime;2685$filename=~/\./orreturnundef;26862687if($mimetypes_file) {2688my$file=$mimetypes_file;2689if($file!~m!^/!) {# if it is relative path2690# it is relative to project2691$file="$projectroot/$project/$file";2692}2693$mime= mimetype_guess_file($filename,$file);2694}2695$mime||= mimetype_guess_file($filename,'/etc/mime.types');2696return$mime;2697}26982699sub blob_mimetype {2700my$fd=shift;2701my$filename=shift;27022703if($filename) {2704my$mime= mimetype_guess($filename);2705$mimeandreturn$mime;2706}27072708# just in case2709return$default_blob_plain_mimetypeunless$fd;27102711if(-T $fd) {2712return'text/plain';2713}elsif(!$filename) {2714return'application/octet-stream';2715}elsif($filename=~m/\.png$/i) {2716return'image/png';2717}elsif($filename=~m/\.gif$/i) {2718return'image/gif';2719}elsif($filename=~m/\.jpe?g$/i) {2720return'image/jpeg';2721}else{2722return'application/octet-stream';2723}2724}27252726sub blob_contenttype {2727my($fd,$file_name,$type) =@_;27282729$type||= blob_mimetype($fd,$file_name);2730if($typeeq'text/plain'&&defined$default_text_plain_charset) {2731$type.="; charset=$default_text_plain_charset";2732}27332734return$type;2735}27362737## ======================================================================2738## functions printing HTML: header, footer, error page27392740sub git_header_html {2741my$status=shift||"200 OK";2742my$expires=shift;27432744my$title="$site_name";2745if(defined$project) {2746$title.=" - ". to_utf8($project);2747if(defined$action) {2748$title.="/$action";2749if(defined$file_name) {2750$title.=" - ". esc_path($file_name);2751if($actioneq"tree"&&$file_name!~ m|/$|) {2752$title.="/";2753}2754}2755}2756}2757my$content_type;2758# require explicit support from the UA if we are to send the page as2759# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2760# we have to do this because MSIE sometimes globs '*/*', pretending to2761# support xhtml+xml but choking when it gets what it asked for.2762if(defined$cgi->http('HTTP_ACCEPT') &&2763$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2764$cgi->Accept('application/xhtml+xml') !=0) {2765$content_type='application/xhtml+xml';2766}else{2767$content_type='text/html';2768}2769print$cgi->header(-type=>$content_type, -charset =>'utf-8',2770-status=>$status, -expires =>$expires);2771my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2772print<<EOF;2773<?xml version="1.0" encoding="utf-8"?>2774<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2775<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2776<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2777<!-- git core binaries version$git_version-->2778<head>2779<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2780<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2781<meta name="robots" content="index, nofollow"/>2782<title>$title</title>2783EOF2784# print out each stylesheet that exist2785if(defined$stylesheet) {2786#provides backwards capability for those people who define style sheet in a config file2787print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2788}else{2789foreachmy$stylesheet(@stylesheets) {2790next unless$stylesheet;2791print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2792}2793}2794if(defined$project) {2795my%href_params= get_feed_info();2796if(!exists$href_params{'-title'}) {2797$href_params{'-title'} ='log';2798}27992800foreachmy$formatqw(RSS Atom){2801my$type=lc($format);2802my%link_attr= (2803'-rel'=>'alternate',2804'-title'=>"$project-$href_params{'-title'} -$formatfeed",2805'-type'=>"application/$type+xml"2806);28072808$href_params{'action'} =$type;2809$link_attr{'-href'} = href(%href_params);2810print"<link ".2811"rel=\"$link_attr{'-rel'}\"".2812"title=\"$link_attr{'-title'}\"".2813"href=\"$link_attr{'-href'}\"".2814"type=\"$link_attr{'-type'}\"".2815"/>\n";28162817$href_params{'extra_options'} ='--no-merges';2818$link_attr{'-href'} = href(%href_params);2819$link_attr{'-title'} .=' (no merges)';2820print"<link ".2821"rel=\"$link_attr{'-rel'}\"".2822"title=\"$link_attr{'-title'}\"".2823"href=\"$link_attr{'-href'}\"".2824"type=\"$link_attr{'-type'}\"".2825"/>\n";2826}28272828}else{2829printf('<link rel="alternate" title="%sprojects list" '.2830'href="%s" type="text/plain; charset=utf-8" />'."\n",2831$site_name, href(project=>undef, action=>"project_index"));2832printf('<link rel="alternate" title="%sprojects feeds" '.2833'href="%s" type="text/x-opml" />'."\n",2834$site_name, href(project=>undef, action=>"opml"));2835}2836if(defined$favicon) {2837printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2838}28392840print"</head>\n".2841"<body>\n";28422843if(-f $site_header) {2844open(my$fd,$site_header);2845print<$fd>;2846close$fd;2847}28482849print"<div class=\"page_header\">\n".2850$cgi->a({-href => esc_url($logo_url),2851-title =>$logo_label},2852qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2853print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2854if(defined$project) {2855print$cgi->a({-href => href(action=>"summary")}, esc_html($project));2856if(defined$action) {2857print" /$action";2858}2859print"\n";2860}2861print"</div>\n";28622863my($have_search) = gitweb_check_feature('search');2864if(defined$project&&$have_search) {2865if(!defined$searchtext) {2866$searchtext="";2867}2868my$search_hash;2869if(defined$hash_base) {2870$search_hash=$hash_base;2871}elsif(defined$hash) {2872$search_hash=$hash;2873}else{2874$search_hash="HEAD";2875}2876my$action=$my_uri;2877my($use_pathinfo) = gitweb_check_feature('pathinfo');2878if($use_pathinfo) {2879$action.="/".esc_url($project);2880}2881print$cgi->startform(-method=>"get", -action =>$action) .2882"<div class=\"search\">\n".2883(!$use_pathinfo&&2884$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .2885$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".2886$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".2887$cgi->popup_menu(-name =>'st', -default=>'commit',2888-values=> ['commit','grep','author','committer','pickaxe']) .2889$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .2890" search:\n",2891$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".2892"<span title=\"Extended regular expression\">".2893$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',2894-checked =>$search_use_regexp) .2895"</span>".2896"</div>".2897$cgi->end_form() ."\n";2898}2899}29002901sub git_footer_html {2902my$feed_class='rss_logo';29032904print"<div class=\"page_footer\">\n";2905if(defined$project) {2906my$descr= git_get_project_description($project);2907if(defined$descr) {2908print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";2909}29102911my%href_params= get_feed_info();2912if(!%href_params) {2913$feed_class.=' generic';2914}2915$href_params{'-title'} ||='log';29162917foreachmy$formatqw(RSS Atom){2918$href_params{'action'} =lc($format);2919print$cgi->a({-href => href(%href_params),2920-title =>"$href_params{'-title'}$formatfeed",2921-class=>$feed_class},$format)."\n";2922}29232924}else{2925print$cgi->a({-href => href(project=>undef, action=>"opml"),2926-class=>$feed_class},"OPML") ." ";2927print$cgi->a({-href => href(project=>undef, action=>"project_index"),2928-class=>$feed_class},"TXT") ."\n";2929}2930print"</div>\n";# class="page_footer"29312932if(-f $site_footer) {2933open(my$fd,$site_footer);2934print<$fd>;2935close$fd;2936}29372938print"</body>\n".2939"</html>";2940}29412942# die_error(<http_status_code>, <error_message>)2943# Example: die_error(404, 'Hash not found')2944# By convention, use the following status codes (as defined in RFC 2616):2945# 400: Invalid or missing CGI parameters, or2946# requested object exists but has wrong type.2947# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on2948# this server or project.2949# 404: Requested object/revision/project doesn't exist.2950# 500: The server isn't configured properly, or2951# an internal error occurred (e.g. failed assertions caused by bugs), or2952# an unknown error occurred (e.g. the git binary died unexpectedly).2953sub die_error {2954my$status=shift||500;2955my$error=shift||"Internal server error";29562957my%http_responses= (400=>'400 Bad Request',2958403=>'403 Forbidden',2959404=>'404 Not Found',2960500=>'500 Internal Server Error');2961 git_header_html($http_responses{$status});2962print<<EOF;2963<div class="page_body">2964<br /><br />2965$status-$error2966<br />2967</div>2968EOF2969 git_footer_html();2970exit;2971}29722973## ----------------------------------------------------------------------2974## functions printing or outputting HTML: navigation29752976sub git_print_page_nav {2977my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;2978$extra=''if!defined$extra;# pager or formats29792980my@navs=qw(summary shortlog log commit commitdiff tree);2981if($suppress) {2982@navs=grep{$_ne$suppress}@navs;2983}29842985my%arg=map{$_=> {action=>$_} }@navs;2986if(defined$head) {2987for(qw(commit commitdiff)) {2988$arg{$_}{'hash'} =$head;2989}2990if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {2991for(qw(shortlog log)) {2992$arg{$_}{'hash'} =$head;2993}2994}2995}29962997$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;2998$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;29993000my@actions= gitweb_check_feature('actions');3001while(@actions) {3002my($label,$link,$pos) = (shift(@actions),shift(@actions),shift(@actions));3003@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3004# munch munch3005$link=~ s#%n#$project#g;3006$link=~ s#%f#$git_dir#g;3007$treehead?$link=~ s#%h#$treehead#g : $link =~ s#%h##g;3008$treebase?$link=~ s#%b#$treebase#g : $link =~ s#%b##g;3009$arg{$label}{'_href'} =$link;3010}30113012print"<div class=\"page_nav\">\n".3013(join" | ",3014map{$_eq$current?3015$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3016}@navs);3017print"<br/>\n$extra<br/>\n".3018"</div>\n";3019}30203021sub format_paging_nav {3022my($action,$hash,$head,$page,$has_next_link) =@_;3023my$paging_nav;302430253026if($hashne$head||$page) {3027$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3028}else{3029$paging_nav.="HEAD";3030}30313032if($page>0) {3033$paging_nav.=" ⋅ ".3034$cgi->a({-href => href(-replay=>1, page=>$page-1),3035-accesskey =>"p", -title =>"Alt-p"},"prev");3036}else{3037$paging_nav.=" ⋅ prev";3038}30393040if($has_next_link) {3041$paging_nav.=" ⋅ ".3042$cgi->a({-href => href(-replay=>1, page=>$page+1),3043-accesskey =>"n", -title =>"Alt-n"},"next");3044}else{3045$paging_nav.=" ⋅ next";3046}30473048return$paging_nav;3049}30503051## ......................................................................3052## functions printing or outputting HTML: div30533054sub git_print_header_div {3055my($action,$title,$hash,$hash_base) =@_;3056my%args= ();30573058$args{'action'} =$action;3059$args{'hash'} =$hashif$hash;3060$args{'hash_base'} =$hash_baseif$hash_base;30613062print"<div class=\"header\">\n".3063$cgi->a({-href => href(%args), -class=>"title"},3064$title?$title:$action) .3065"\n</div>\n";3066}30673068#sub git_print_authorship (\%) {3069sub git_print_authorship {3070my$co=shift;30713072my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3073print"<div class=\"author_date\">".3074 esc_html($co->{'author_name'}) .3075" [$ad{'rfc2822'}";3076if($ad{'hour_local'} <6) {3077printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3078$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3079}else{3080printf(" (%02d:%02d%s)",3081$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3082}3083print"]</div>\n";3084}30853086sub git_print_page_path {3087my$name=shift;3088my$type=shift;3089my$hb=shift;309030913092print"<div class=\"page_path\">";3093print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3094-title =>'tree root'}, to_utf8("[$project]"));3095print" / ";3096if(defined$name) {3097my@dirname=split'/',$name;3098my$basename=pop@dirname;3099my$fullname='';31003101foreachmy$dir(@dirname) {3102$fullname.= ($fullname?'/':'') .$dir;3103print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3104 hash_base=>$hb),3105-title =>$fullname}, esc_path($dir));3106print" / ";3107}3108if(defined$type&&$typeeq'blob') {3109print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3110 hash_base=>$hb),3111-title =>$name}, esc_path($basename));3112}elsif(defined$type&&$typeeq'tree') {3113print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3114 hash_base=>$hb),3115-title =>$name}, esc_path($basename));3116print" / ";3117}else{3118print esc_path($basename);3119}3120}3121print"<br/></div>\n";3122}31233124# sub git_print_log (\@;%) {3125sub git_print_log ($;%) {3126my$log=shift;3127my%opts=@_;31283129if($opts{'-remove_title'}) {3130# remove title, i.e. first line of log3131shift@$log;3132}3133# remove leading empty lines3134while(defined$log->[0] &&$log->[0]eq"") {3135shift@$log;3136}31373138# print log3139my$signoff=0;3140my$empty=0;3141foreachmy$line(@$log) {3142if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3143$signoff=1;3144$empty=0;3145if(!$opts{'-remove_signoff'}) {3146print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3147next;3148}else{3149# remove signoff lines3150next;3151}3152}else{3153$signoff=0;3154}31553156# print only one empty line3157# do not print empty line after signoff3158if($lineeq"") {3159next if($empty||$signoff);3160$empty=1;3161}else{3162$empty=0;3163}31643165print format_log_line_html($line) ."<br/>\n";3166}31673168if($opts{'-final_empty_line'}) {3169# end with single empty line3170print"<br/>\n"unless$empty;3171}3172}31733174# return link target (what link points to)3175sub git_get_link_target {3176my$hash=shift;3177my$link_target;31783179# read link3180open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3181orreturn;3182{3183local$/;3184$link_target= <$fd>;3185}3186close$fd3187orreturn;31883189return$link_target;3190}31913192# given link target, and the directory (basedir) the link is in,3193# return target of link relative to top directory (top tree);3194# return undef if it is not possible (including absolute links).3195sub normalize_link_target {3196my($link_target,$basedir,$hash_base) =@_;31973198# we can normalize symlink target only if $hash_base is provided3199return unless$hash_base;32003201# absolute symlinks (beginning with '/') cannot be normalized3202return if(substr($link_target,0,1)eq'/');32033204# normalize link target to path from top (root) tree (dir)3205my$path;3206if($basedir) {3207$path=$basedir.'/'.$link_target;3208}else{3209# we are in top (root) tree (dir)3210$path=$link_target;3211}32123213# remove //, /./, and /../3214my@path_parts;3215foreachmy$part(split('/',$path)) {3216# discard '.' and ''3217next if(!$part||$parteq'.');3218# handle '..'3219if($parteq'..') {3220if(@path_parts) {3221pop@path_parts;3222}else{3223# link leads outside repository (outside top dir)3224return;3225}3226}else{3227push@path_parts,$part;3228}3229}3230$path=join('/',@path_parts);32313232return$path;3233}32343235# print tree entry (row of git_tree), but without encompassing <tr> element3236sub git_print_tree_entry {3237my($t,$basedir,$hash_base,$have_blame) =@_;32383239my%base_key= ();3240$base_key{'hash_base'} =$hash_baseifdefined$hash_base;32413242# The format of a table row is: mode list link. Where mode is3243# the mode of the entry, list is the name of the entry, an href,3244# and link is the action links of the entry.32453246print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3247if($t->{'type'}eq"blob") {3248print"<td class=\"list\">".3249$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3250 file_name=>"$basedir$t->{'name'}",%base_key),3251-class=>"list"}, esc_path($t->{'name'}));3252if(S_ISLNK(oct$t->{'mode'})) {3253my$link_target= git_get_link_target($t->{'hash'});3254if($link_target) {3255my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3256if(defined$norm_target) {3257print" -> ".3258$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3259 file_name=>$norm_target),3260-title =>$norm_target}, esc_path($link_target));3261}else{3262print" -> ". esc_path($link_target);3263}3264}3265}3266print"</td>\n";3267print"<td class=\"link\">";3268print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3269 file_name=>"$basedir$t->{'name'}",%base_key)},3270"blob");3271if($have_blame) {3272print" | ".3273$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3274 file_name=>"$basedir$t->{'name'}",%base_key)},3275"blame");3276}3277if(defined$hash_base) {3278print" | ".3279$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3280 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3281"history");3282}3283print" | ".3284$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3285 file_name=>"$basedir$t->{'name'}")},3286"raw");3287print"</td>\n";32883289}elsif($t->{'type'}eq"tree") {3290print"<td class=\"list\">";3291print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3292 file_name=>"$basedir$t->{'name'}",%base_key)},3293 esc_path($t->{'name'}));3294print"</td>\n";3295print"<td class=\"link\">";3296print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3297 file_name=>"$basedir$t->{'name'}",%base_key)},3298"tree");3299if(defined$hash_base) {3300print" | ".3301$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3302 file_name=>"$basedir$t->{'name'}")},3303"history");3304}3305print"</td>\n";3306}else{3307# unknown object: we can only present history for it3308# (this includes 'commit' object, i.e. submodule support)3309print"<td class=\"list\">".3310 esc_path($t->{'name'}) .3311"</td>\n";3312print"<td class=\"link\">";3313if(defined$hash_base) {3314print$cgi->a({-href => href(action=>"history",3315 hash_base=>$hash_base,3316 file_name=>"$basedir$t->{'name'}")},3317"history");3318}3319print"</td>\n";3320}3321}33223323## ......................................................................3324## functions printing large fragments of HTML33253326# get pre-image filenames for merge (combined) diff3327sub fill_from_file_info {3328my($diff,@parents) =@_;33293330$diff->{'from_file'} = [ ];3331$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3332for(my$i=0;$i<$diff->{'nparents'};$i++) {3333if($diff->{'status'}[$i]eq'R'||3334$diff->{'status'}[$i]eq'C') {3335$diff->{'from_file'}[$i] =3336 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3337}3338}33393340return$diff;3341}33423343# is current raw difftree line of file deletion3344sub is_deleted {3345my$diffinfo=shift;33463347return$diffinfo->{'to_id'}eq('0' x 40);3348}33493350# does patch correspond to [previous] difftree raw line3351# $diffinfo - hashref of parsed raw diff format3352# $patchinfo - hashref of parsed patch diff format3353# (the same keys as in $diffinfo)3354sub is_patch_split {3355my($diffinfo,$patchinfo) =@_;33563357returndefined$diffinfo&&defined$patchinfo3358&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3359}336033613362sub git_difftree_body {3363my($difftree,$hash,@parents) =@_;3364my($parent) =$parents[0];3365my($have_blame) = gitweb_check_feature('blame');3366print"<div class=\"list_head\">\n";3367if($#{$difftree} >10) {3368print(($#{$difftree} +1) ." files changed:\n");3369}3370print"</div>\n";33713372print"<table class=\"".3373(@parents>1?"combined ":"") .3374"diff_tree\">\n";33753376# header only for combined diff in 'commitdiff' view3377my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3378if($has_header) {3379# table header3380print"<thead><tr>\n".3381"<th></th><th></th>\n";# filename, patchN link3382for(my$i=0;$i<@parents;$i++) {3383my$par=$parents[$i];3384print"<th>".3385$cgi->a({-href => href(action=>"commitdiff",3386 hash=>$hash, hash_parent=>$par),3387-title =>'commitdiff to parent number '.3388($i+1) .': '.substr($par,0,7)},3389$i+1) .3390" </th>\n";3391}3392print"</tr></thead>\n<tbody>\n";3393}33943395my$alternate=1;3396my$patchno=0;3397foreachmy$line(@{$difftree}) {3398my$diff= parsed_difftree_line($line);33993400if($alternate) {3401print"<tr class=\"dark\">\n";3402}else{3403print"<tr class=\"light\">\n";3404}3405$alternate^=1;34063407if(exists$diff->{'nparents'}) {# combined diff34083409 fill_from_file_info($diff,@parents)3410unlessexists$diff->{'from_file'};34113412if(!is_deleted($diff)) {3413# file exists in the result (child) commit3414print"<td>".3415$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3416 file_name=>$diff->{'to_file'},3417 hash_base=>$hash),3418-class=>"list"}, esc_path($diff->{'to_file'})) .3419"</td>\n";3420}else{3421print"<td>".3422 esc_path($diff->{'to_file'}) .3423"</td>\n";3424}34253426if($actioneq'commitdiff') {3427# link to patch3428$patchno++;3429print"<td class=\"link\">".3430$cgi->a({-href =>"#patch$patchno"},"patch") .3431" | ".3432"</td>\n";3433}34343435my$has_history=0;3436my$not_deleted=0;3437for(my$i=0;$i<$diff->{'nparents'};$i++) {3438my$hash_parent=$parents[$i];3439my$from_hash=$diff->{'from_id'}[$i];3440my$from_path=$diff->{'from_file'}[$i];3441my$status=$diff->{'status'}[$i];34423443$has_history||= ($statusne'A');3444$not_deleted||= ($statusne'D');34453446if($statuseq'A') {3447print"<td class=\"link\"align=\"right\"> | </td>\n";3448}elsif($statuseq'D') {3449print"<td class=\"link\">".3450$cgi->a({-href => href(action=>"blob",3451 hash_base=>$hash,3452 hash=>$from_hash,3453 file_name=>$from_path)},3454"blob". ($i+1)) .3455" | </td>\n";3456}else{3457if($diff->{'to_id'}eq$from_hash) {3458print"<td class=\"link nochange\">";3459}else{3460print"<td class=\"link\">";3461}3462print$cgi->a({-href => href(action=>"blobdiff",3463 hash=>$diff->{'to_id'},3464 hash_parent=>$from_hash,3465 hash_base=>$hash,3466 hash_parent_base=>$hash_parent,3467 file_name=>$diff->{'to_file'},3468 file_parent=>$from_path)},3469"diff". ($i+1)) .3470" | </td>\n";3471}3472}34733474print"<td class=\"link\">";3475if($not_deleted) {3476print$cgi->a({-href => href(action=>"blob",3477 hash=>$diff->{'to_id'},3478 file_name=>$diff->{'to_file'},3479 hash_base=>$hash)},3480"blob");3481print" | "if($has_history);3482}3483if($has_history) {3484print$cgi->a({-href => href(action=>"history",3485 file_name=>$diff->{'to_file'},3486 hash_base=>$hash)},3487"history");3488}3489print"</td>\n";34903491print"</tr>\n";3492next;# instead of 'else' clause, to avoid extra indent3493}3494# else ordinary diff34953496my($to_mode_oct,$to_mode_str,$to_file_type);3497my($from_mode_oct,$from_mode_str,$from_file_type);3498if($diff->{'to_mode'}ne('0' x 6)) {3499$to_mode_oct=oct$diff->{'to_mode'};3500if(S_ISREG($to_mode_oct)) {# only for regular file3501$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3502}3503$to_file_type= file_type($diff->{'to_mode'});3504}3505if($diff->{'from_mode'}ne('0' x 6)) {3506$from_mode_oct=oct$diff->{'from_mode'};3507if(S_ISREG($to_mode_oct)) {# only for regular file3508$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3509}3510$from_file_type= file_type($diff->{'from_mode'});3511}35123513if($diff->{'status'}eq"A") {# created3514my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3515$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3516$mode_chng.="]</span>";3517print"<td>";3518print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3519 hash_base=>$hash, file_name=>$diff->{'file'}),3520-class=>"list"}, esc_path($diff->{'file'}));3521print"</td>\n";3522print"<td>$mode_chng</td>\n";3523print"<td class=\"link\">";3524if($actioneq'commitdiff') {3525# link to patch3526$patchno++;3527print$cgi->a({-href =>"#patch$patchno"},"patch");3528print" | ";3529}3530print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3531 hash_base=>$hash, file_name=>$diff->{'file'})},3532"blob");3533print"</td>\n";35343535}elsif($diff->{'status'}eq"D") {# deleted3536my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3537print"<td>";3538print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3539 hash_base=>$parent, file_name=>$diff->{'file'}),3540-class=>"list"}, esc_path($diff->{'file'}));3541print"</td>\n";3542print"<td>$mode_chng</td>\n";3543print"<td class=\"link\">";3544if($actioneq'commitdiff') {3545# link to patch3546$patchno++;3547print$cgi->a({-href =>"#patch$patchno"},"patch");3548print" | ";3549}3550print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3551 hash_base=>$parent, file_name=>$diff->{'file'})},3552"blob") ." | ";3553if($have_blame) {3554print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3555 file_name=>$diff->{'file'})},3556"blame") ." | ";3557}3558print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3559 file_name=>$diff->{'file'})},3560"history");3561print"</td>\n";35623563}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3564my$mode_chnge="";3565if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3566$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3567if($from_file_typene$to_file_type) {3568$mode_chnge.=" from$from_file_typeto$to_file_type";3569}3570if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3571if($from_mode_str&&$to_mode_str) {3572$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3573}elsif($to_mode_str) {3574$mode_chnge.=" mode:$to_mode_str";3575}3576}3577$mode_chnge.="]</span>\n";3578}3579print"<td>";3580print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3581 hash_base=>$hash, file_name=>$diff->{'file'}),3582-class=>"list"}, esc_path($diff->{'file'}));3583print"</td>\n";3584print"<td>$mode_chnge</td>\n";3585print"<td class=\"link\">";3586if($actioneq'commitdiff') {3587# link to patch3588$patchno++;3589print$cgi->a({-href =>"#patch$patchno"},"patch") .3590" | ";3591}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3592# "commit" view and modified file (not onlu mode changed)3593print$cgi->a({-href => href(action=>"blobdiff",3594 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3595 hash_base=>$hash, hash_parent_base=>$parent,3596 file_name=>$diff->{'file'})},3597"diff") .3598" | ";3599}3600print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3601 hash_base=>$hash, file_name=>$diff->{'file'})},3602"blob") ." | ";3603if($have_blame) {3604print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3605 file_name=>$diff->{'file'})},3606"blame") ." | ";3607}3608print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3609 file_name=>$diff->{'file'})},3610"history");3611print"</td>\n";36123613}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3614my%status_name= ('R'=>'moved','C'=>'copied');3615my$nstatus=$status_name{$diff->{'status'}};3616my$mode_chng="";3617if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3618# mode also for directories, so we cannot use $to_mode_str3619$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3620}3621print"<td>".3622$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3623 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3624-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3625"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3626$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3627 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3628-class=>"list"}, esc_path($diff->{'from_file'})) .3629" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3630"<td class=\"link\">";3631if($actioneq'commitdiff') {3632# link to patch3633$patchno++;3634print$cgi->a({-href =>"#patch$patchno"},"patch") .3635" | ";3636}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3637# "commit" view and modified file (not only pure rename or copy)3638print$cgi->a({-href => href(action=>"blobdiff",3639 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3640 hash_base=>$hash, hash_parent_base=>$parent,3641 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3642"diff") .3643" | ";3644}3645print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3646 hash_base=>$parent, file_name=>$diff->{'to_file'})},3647"blob") ." | ";3648if($have_blame) {3649print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3650 file_name=>$diff->{'to_file'})},3651"blame") ." | ";3652}3653print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3654 file_name=>$diff->{'to_file'})},3655"history");3656print"</td>\n";36573658}# we should not encounter Unmerged (U) or Unknown (X) status3659print"</tr>\n";3660}3661print"</tbody>"if$has_header;3662print"</table>\n";3663}36643665sub git_patchset_body {3666my($fd,$difftree,$hash,@hash_parents) =@_;3667my($hash_parent) =$hash_parents[0];36683669my$is_combined= (@hash_parents>1);3670my$patch_idx=0;3671my$patch_number=0;3672my$patch_line;3673my$diffinfo;3674my$to_name;3675my(%from,%to);36763677print"<div class=\"patchset\">\n";36783679# skip to first patch3680while($patch_line= <$fd>) {3681chomp$patch_line;36823683last if($patch_line=~m/^diff /);3684}36853686 PATCH:3687while($patch_line) {36883689# parse "git diff" header line3690if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3691# $1 is from_name, which we do not use3692$to_name= unquote($2);3693$to_name=~s!^b/!!;3694}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3695# $1 is 'cc' or 'combined', which we do not use3696$to_name= unquote($2);3697}else{3698$to_name=undef;3699}37003701# check if current patch belong to current raw line3702# and parse raw git-diff line if needed3703if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3704# this is continuation of a split patch3705print"<div class=\"patch cont\">\n";3706}else{3707# advance raw git-diff output if needed3708$patch_idx++ifdefined$diffinfo;37093710# read and prepare patch information3711$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);37123713# compact combined diff output can have some patches skipped3714# find which patch (using pathname of result) we are at now;3715if($is_combined) {3716while($to_namene$diffinfo->{'to_file'}) {3717print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3718 format_diff_cc_simplified($diffinfo,@hash_parents) .3719"</div>\n";# class="patch"37203721$patch_idx++;3722$patch_number++;37233724last if$patch_idx>$#$difftree;3725$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3726}3727}37283729# modifies %from, %to hashes3730 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);37313732# this is first patch for raw difftree line with $patch_idx index3733# we index @$difftree array from 0, but number patches from 13734print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3735}37363737# git diff header3738#assert($patch_line =~ m/^diff /) if DEBUG;3739#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3740$patch_number++;3741# print "git diff" header3742print format_git_diff_header_line($patch_line,$diffinfo,3743 \%from, \%to);37443745# print extended diff header3746print"<div class=\"diff extended_header\">\n";3747 EXTENDED_HEADER:3748while($patch_line= <$fd>) {3749chomp$patch_line;37503751last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);37523753print format_extended_diff_header_line($patch_line,$diffinfo,3754 \%from, \%to);3755}3756print"</div>\n";# class="diff extended_header"37573758# from-file/to-file diff header3759if(!$patch_line) {3760print"</div>\n";# class="patch"3761last PATCH;3762}3763next PATCH if($patch_line=~m/^diff /);3764#assert($patch_line =~ m/^---/) if DEBUG;37653766my$last_patch_line=$patch_line;3767$patch_line= <$fd>;3768chomp$patch_line;3769#assert($patch_line =~ m/^\+\+\+/) if DEBUG;37703771print format_diff_from_to_header($last_patch_line,$patch_line,3772$diffinfo, \%from, \%to,3773@hash_parents);37743775# the patch itself3776 LINE:3777while($patch_line= <$fd>) {3778chomp$patch_line;37793780next PATCH if($patch_line=~m/^diff /);37813782print format_diff_line($patch_line, \%from, \%to);3783}37843785}continue{3786print"</div>\n";# class="patch"3787}37883789# for compact combined (--cc) format, with chunk and patch simpliciaction3790# patchset might be empty, but there might be unprocessed raw lines3791for(++$patch_idxif$patch_number>0;3792$patch_idx<@$difftree;3793++$patch_idx) {3794# read and prepare patch information3795$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);37963797# generate anchor for "patch" links in difftree / whatchanged part3798print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3799 format_diff_cc_simplified($diffinfo,@hash_parents) .3800"</div>\n";# class="patch"38013802$patch_number++;3803}38043805if($patch_number==0) {3806if(@hash_parents>1) {3807print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3808}else{3809print"<div class=\"diff nodifferences\">No differences found</div>\n";3810}3811}38123813print"</div>\n";# class="patchset"3814}38153816# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38173818# fills project list info (age, description, owner, forks) for each3819# project in the list, removing invalid projects from returned list3820# NOTE: modifies $projlist, but does not remove entries from it3821sub fill_project_list_info {3822my($projlist,$check_forks) =@_;3823my@projects;38243825my$show_ctags= gitweb_check_feature('ctags');3826 PROJECT:3827foreachmy$pr(@$projlist) {3828my(@activity) = git_get_last_activity($pr->{'path'});3829unless(@activity) {3830next PROJECT;3831}3832($pr->{'age'},$pr->{'age_string'}) =@activity;3833if(!defined$pr->{'descr'}) {3834my$descr= git_get_project_description($pr->{'path'}) ||"";3835$descr= to_utf8($descr);3836$pr->{'descr_long'} =$descr;3837$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3838}3839if(!defined$pr->{'owner'}) {3840$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3841}3842if($check_forks) {3843my$pname=$pr->{'path'};3844if(($pname=~s/\.git$//) &&3845($pname!~/\/$/) &&3846(-d "$projectroot/$pname")) {3847$pr->{'forks'} ="-d$projectroot/$pname";3848}else{3849$pr->{'forks'} =0;3850}3851}3852$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3853push@projects,$pr;3854}38553856return@projects;3857}38583859# print 'sort by' <th> element, generating 'sort by $name' replay link3860# if that order is not selected3861sub print_sort_th {3862my($name,$order,$header) =@_;3863$header||=ucfirst($name);38643865if($ordereq$name) {3866print"<th>$header</th>\n";3867}else{3868print"<th>".3869$cgi->a({-href => href(-replay=>1, order=>$name),3870-class=>"header"},$header) .3871"</th>\n";3872}3873}38743875sub git_project_list_body {3876# actually uses global variable $project3877my($projlist,$order,$from,$to,$extra,$no_header) =@_;38783879my($check_forks) = gitweb_check_feature('forks');3880my@projects= fill_project_list_info($projlist,$check_forks);38813882$order||=$default_projects_order;3883$from=0unlessdefined$from;3884$to=$#projectsif(!defined$to||$#projects<$to);38853886my%order_info= (3887 project => { key =>'path', type =>'str'},3888 descr => { key =>'descr_long', type =>'str'},3889 owner => { key =>'owner', type =>'str'},3890 age => { key =>'age', type =>'num'}3891);3892my$oi=$order_info{$order};3893if($oi->{'type'}eq'str') {3894@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;3895}else{3896@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;3897}38983899my$show_ctags= gitweb_check_feature('ctags');3900if($show_ctags) {3901my%ctags;3902foreachmy$p(@projects) {3903foreachmy$ct(keys%{$p->{'ctags'}}) {3904$ctags{$ct} +=$p->{'ctags'}->{$ct};3905}3906}3907my$cloud= git_populate_project_tagcloud(\%ctags);3908print git_show_project_tagcloud($cloud,64);3909}39103911print"<table class=\"project_list\">\n";3912unless($no_header) {3913print"<tr>\n";3914if($check_forks) {3915print"<th></th>\n";3916}3917 print_sort_th('project',$order,'Project');3918 print_sort_th('descr',$order,'Description');3919 print_sort_th('owner',$order,'Owner');3920 print_sort_th('age',$order,'Last Change');3921print"<th></th>\n".# for links3922"</tr>\n";3923}3924my$alternate=1;3925my$tagfilter=$cgi->param('by_tag');3926for(my$i=$from;$i<=$to;$i++) {3927my$pr=$projects[$i];39283929next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};3930next if$searchtextand not$pr->{'path'} =~/$searchtext/3931and not$pr->{'descr_long'} =~/$searchtext/;3932# Weed out forks or non-matching entries of search3933if($check_forks) {3934my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;3935$forkbase="^$forkbase"if$forkbase;3936next ifnot$searchtextand not$tagfilterand$show_ctags3937and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe3938}39393940if($alternate) {3941print"<tr class=\"dark\">\n";3942}else{3943print"<tr class=\"light\">\n";3944}3945$alternate^=1;3946if($check_forks) {3947print"<td>";3948if($pr->{'forks'}) {3949print"<!--$pr->{'forks'} -->\n";3950print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");3951}3952print"</td>\n";3953}3954print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3955-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".3956"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),3957-class=>"list", -title =>$pr->{'descr_long'}},3958 esc_html($pr->{'descr'})) ."</td>\n".3959"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";3960print"<td class=\"". age_class($pr->{'age'}) ."\">".3961(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".3962"<td class=\"link\">".3963$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".3964$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".3965$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".3966$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .3967($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .3968"</td>\n".3969"</tr>\n";3970}3971if(defined$extra) {3972print"<tr>\n";3973if($check_forks) {3974print"<td></td>\n";3975}3976print"<td colspan=\"5\">$extra</td>\n".3977"</tr>\n";3978}3979print"</table>\n";3980}39813982sub git_shortlog_body {3983# uses global variable $project3984my($commitlist,$from,$to,$refs,$extra) =@_;39853986$from=0unlessdefined$from;3987$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);39883989print"<table class=\"shortlog\">\n";3990my$alternate=1;3991for(my$i=$from;$i<=$to;$i++) {3992my%co= %{$commitlist->[$i]};3993my$commit=$co{'id'};3994my$ref= format_ref_marker($refs,$commit);3995if($alternate) {3996print"<tr class=\"dark\">\n";3997}else{3998print"<tr class=\"light\">\n";3999}4000$alternate^=1;4001my$author= chop_and_escape_str($co{'author_name'},10);4002# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4003print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4004"<td><i>".$author."</i></td>\n".4005"<td>";4006print format_subject_html($co{'title'},$co{'title_short'},4007 href(action=>"commit", hash=>$commit),$ref);4008print"</td>\n".4009"<td class=\"link\">".4010$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4011$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4012$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4013my$snapshot_links= format_snapshot_links($commit);4014if(defined$snapshot_links) {4015print" | ".$snapshot_links;4016}4017print"</td>\n".4018"</tr>\n";4019}4020if(defined$extra) {4021print"<tr>\n".4022"<td colspan=\"4\">$extra</td>\n".4023"</tr>\n";4024}4025print"</table>\n";4026}40274028sub git_history_body {4029# Warning: assumes constant type (blob or tree) during history4030my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;40314032$from=0unlessdefined$from;4033$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});40344035print"<table class=\"history\">\n";4036my$alternate=1;4037for(my$i=$from;$i<=$to;$i++) {4038my%co= %{$commitlist->[$i]};4039if(!%co) {4040next;4041}4042my$commit=$co{'id'};40434044my$ref= format_ref_marker($refs,$commit);40454046if($alternate) {4047print"<tr class=\"dark\">\n";4048}else{4049print"<tr class=\"light\">\n";4050}4051$alternate^=1;4052# shortlog uses chop_str($co{'author_name'}, 10)4053my$author= chop_and_escape_str($co{'author_name'},15,3);4054print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4055"<td><i>".$author."</i></td>\n".4056"<td>";4057# originally git_history used chop_str($co{'title'}, 50)4058print format_subject_html($co{'title'},$co{'title_short'},4059 href(action=>"commit", hash=>$commit),$ref);4060print"</td>\n".4061"<td class=\"link\">".4062$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4063$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");40644065if($ftypeeq'blob') {4066my$blob_current= git_get_hash_by_path($hash_base,$file_name);4067my$blob_parent= git_get_hash_by_path($commit,$file_name);4068if(defined$blob_current&&defined$blob_parent&&4069$blob_currentne$blob_parent) {4070print" | ".4071$cgi->a({-href => href(action=>"blobdiff",4072 hash=>$blob_current, hash_parent=>$blob_parent,4073 hash_base=>$hash_base, hash_parent_base=>$commit,4074 file_name=>$file_name)},4075"diff to current");4076}4077}4078print"</td>\n".4079"</tr>\n";4080}4081if(defined$extra) {4082print"<tr>\n".4083"<td colspan=\"4\">$extra</td>\n".4084"</tr>\n";4085}4086print"</table>\n";4087}40884089sub git_tags_body {4090# uses global variable $project4091my($taglist,$from,$to,$extra) =@_;4092$from=0unlessdefined$from;4093$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);40944095print"<table class=\"tags\">\n";4096my$alternate=1;4097for(my$i=$from;$i<=$to;$i++) {4098my$entry=$taglist->[$i];4099my%tag=%$entry;4100my$comment=$tag{'subject'};4101my$comment_short;4102if(defined$comment) {4103$comment_short= chop_str($comment,30,5);4104}4105if($alternate) {4106print"<tr class=\"dark\">\n";4107}else{4108print"<tr class=\"light\">\n";4109}4110$alternate^=1;4111if(defined$tag{'age'}) {4112print"<td><i>$tag{'age'}</i></td>\n";4113}else{4114print"<td></td>\n";4115}4116print"<td>".4117$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4118-class=>"list name"}, esc_html($tag{'name'})) .4119"</td>\n".4120"<td>";4121if(defined$comment) {4122print format_subject_html($comment,$comment_short,4123 href(action=>"tag", hash=>$tag{'id'}));4124}4125print"</td>\n".4126"<td class=\"selflink\">";4127if($tag{'type'}eq"tag") {4128print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4129}else{4130print" ";4131}4132print"</td>\n".4133"<td class=\"link\">"." | ".4134$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4135if($tag{'reftype'}eq"commit") {4136print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4137" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4138}elsif($tag{'reftype'}eq"blob") {4139print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4140}4141print"</td>\n".4142"</tr>";4143}4144if(defined$extra) {4145print"<tr>\n".4146"<td colspan=\"5\">$extra</td>\n".4147"</tr>\n";4148}4149print"</table>\n";4150}41514152sub git_heads_body {4153# uses global variable $project4154my($headlist,$head,$from,$to,$extra) =@_;4155$from=0unlessdefined$from;4156$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);41574158print"<table class=\"heads\">\n";4159my$alternate=1;4160for(my$i=$from;$i<=$to;$i++) {4161my$entry=$headlist->[$i];4162my%ref=%$entry;4163my$curr=$ref{'id'}eq$head;4164if($alternate) {4165print"<tr class=\"dark\">\n";4166}else{4167print"<tr class=\"light\">\n";4168}4169$alternate^=1;4170print"<td><i>$ref{'age'}</i></td>\n".4171($curr?"<td class=\"current_head\">":"<td>") .4172$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4173-class=>"list name"},esc_html($ref{'name'})) .4174"</td>\n".4175"<td class=\"link\">".4176$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4177$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4178$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4179"</td>\n".4180"</tr>";4181}4182if(defined$extra) {4183print"<tr>\n".4184"<td colspan=\"3\">$extra</td>\n".4185"</tr>\n";4186}4187print"</table>\n";4188}41894190sub git_search_grep_body {4191my($commitlist,$from,$to,$extra) =@_;4192$from=0unlessdefined$from;4193$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41944195print"<table class=\"commit_search\">\n";4196my$alternate=1;4197for(my$i=$from;$i<=$to;$i++) {4198my%co= %{$commitlist->[$i]};4199if(!%co) {4200next;4201}4202my$commit=$co{'id'};4203if($alternate) {4204print"<tr class=\"dark\">\n";4205}else{4206print"<tr class=\"light\">\n";4207}4208$alternate^=1;4209my$author= chop_and_escape_str($co{'author_name'},15,5);4210print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4211"<td><i>".$author."</i></td>\n".4212"<td>".4213$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4214-class=>"list subject"},4215 chop_and_escape_str($co{'title'},50) ."<br/>");4216my$comment=$co{'comment'};4217foreachmy$line(@$comment) {4218if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4219my($lead,$match,$trail) = ($1,$2,$3);4220$match= chop_str($match,70,5,'center');4221my$contextlen=int((80-length($match))/2);4222$contextlen=30if($contextlen>30);4223$lead= chop_str($lead,$contextlen,10,'left');4224$trail= chop_str($trail,$contextlen,10,'right');42254226$lead= esc_html($lead);4227$match= esc_html($match);4228$trail= esc_html($trail);42294230print"$lead<span class=\"match\">$match</span>$trail<br />";4231}4232}4233print"</td>\n".4234"<td class=\"link\">".4235$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4236" | ".4237$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4238" | ".4239$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4240print"</td>\n".4241"</tr>\n";4242}4243if(defined$extra) {4244print"<tr>\n".4245"<td colspan=\"3\">$extra</td>\n".4246"</tr>\n";4247}4248print"</table>\n";4249}42504251## ======================================================================4252## ======================================================================4253## actions42544255sub git_project_list {4256my$order=$input_params{'order'};4257if(defined$order&&$order!~m/none|project|descr|owner|age/) {4258 die_error(400,"Unknown order parameter");4259}42604261my@list= git_get_projects_list();4262if(!@list) {4263 die_error(404,"No projects found");4264}42654266 git_header_html();4267if(-f $home_text) {4268print"<div class=\"index_include\">\n";4269open(my$fd,$home_text);4270print<$fd>;4271close$fd;4272print"</div>\n";4273}4274print$cgi->startform(-method=>"get") .4275"<p class=\"projsearch\">Search:\n".4276$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4277"</p>".4278$cgi->end_form() ."\n";4279 git_project_list_body(\@list,$order);4280 git_footer_html();4281}42824283sub git_forks {4284my$order=$input_params{'order'};4285if(defined$order&&$order!~m/none|project|descr|owner|age/) {4286 die_error(400,"Unknown order parameter");4287}42884289my@list= git_get_projects_list($project);4290if(!@list) {4291 die_error(404,"No forks found");4292}42934294 git_header_html();4295 git_print_page_nav('','');4296 git_print_header_div('summary',"$projectforks");4297 git_project_list_body(\@list,$order);4298 git_footer_html();4299}43004301sub git_project_index {4302my@projects= git_get_projects_list($project);43034304print$cgi->header(4305-type =>'text/plain',4306-charset =>'utf-8',4307-content_disposition =>'inline; filename="index.aux"');43084309foreachmy$pr(@projects) {4310if(!exists$pr->{'owner'}) {4311$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4312}43134314my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4315# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4316$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4317$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4318$path=~s/ /\+/g;4319$owner=~s/ /\+/g;43204321print"$path$owner\n";4322}4323}43244325sub git_summary {4326my$descr= git_get_project_description($project) ||"none";4327my%co= parse_commit("HEAD");4328my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4329my$head=$co{'id'};43304331my$owner= git_get_project_owner($project);43324333my$refs= git_get_references();4334# These get_*_list functions return one more to allow us to see if4335# there are more ...4336my@taglist= git_get_tags_list(16);4337my@headlist= git_get_heads_list(16);4338my@forklist;4339my($check_forks) = gitweb_check_feature('forks');43404341if($check_forks) {4342@forklist= git_get_projects_list($project);4343}43444345 git_header_html();4346 git_print_page_nav('summary','',$head);43474348print"<div class=\"title\"> </div>\n";4349print"<table class=\"projects_list\">\n".4350"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4351"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4352if(defined$cd{'rfc2822'}) {4353print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4354}43554356# use per project git URL list in $projectroot/$project/cloneurl4357# or make project git URL from git base URL and project name4358my$url_tag="URL";4359my@url_list= git_get_project_url_list($project);4360@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4361foreachmy$git_url(@url_list) {4362next unless$git_url;4363print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4364$url_tag="";4365}43664367# Tag cloud4368my$show_ctags= (gitweb_check_feature('ctags'))[0];4369if($show_ctags) {4370my$ctags= git_get_project_ctags($project);4371my$cloud= git_populate_project_tagcloud($ctags);4372print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4373print"</td>\n<td>"unless%$ctags;4374print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4375print"</td>\n<td>"if%$ctags;4376print git_show_project_tagcloud($cloud,48);4377print"</td></tr>";4378}43794380print"</table>\n";43814382if(-s "$projectroot/$project/README.html") {4383if(open my$fd,"$projectroot/$project/README.html") {4384print"<div class=\"title\">readme</div>\n".4385"<div class=\"readme\">\n";4386print$_while(<$fd>);4387print"\n</div>\n";# class="readme"4388close$fd;4389}4390}43914392# we need to request one more than 16 (0..15) to check if4393# those 16 are all4394my@commitlist=$head? parse_commits($head,17) : ();4395if(@commitlist) {4396 git_print_header_div('shortlog');4397 git_shortlog_body(\@commitlist,0,15,$refs,4398$#commitlist<=15?undef:4399$cgi->a({-href => href(action=>"shortlog")},"..."));4400}44014402if(@taglist) {4403 git_print_header_div('tags');4404 git_tags_body(\@taglist,0,15,4405$#taglist<=15?undef:4406$cgi->a({-href => href(action=>"tags")},"..."));4407}44084409if(@headlist) {4410 git_print_header_div('heads');4411 git_heads_body(\@headlist,$head,0,15,4412$#headlist<=15?undef:4413$cgi->a({-href => href(action=>"heads")},"..."));4414}44154416if(@forklist) {4417 git_print_header_div('forks');4418 git_project_list_body(\@forklist,'age',0,15,4419$#forklist<=15?undef:4420$cgi->a({-href => href(action=>"forks")},"..."),4421'no_header');4422}44234424 git_footer_html();4425}44264427sub git_tag {4428my$head= git_get_head_hash($project);4429 git_header_html();4430 git_print_page_nav('','',$head,undef,$head);4431my%tag= parse_tag($hash);44324433if(!%tag) {4434 die_error(404,"Unknown tag object");4435}44364437 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4438print"<div class=\"title_text\">\n".4439"<table class=\"object_header\">\n".4440"<tr>\n".4441"<td>object</td>\n".4442"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4443$tag{'object'}) ."</td>\n".4444"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4445$tag{'type'}) ."</td>\n".4446"</tr>\n";4447if(defined($tag{'author'})) {4448my%ad= parse_date($tag{'epoch'},$tag{'tz'});4449print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4450print"<tr><td></td><td>".$ad{'rfc2822'} .4451sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4452"</td></tr>\n";4453}4454print"</table>\n\n".4455"</div>\n";4456print"<div class=\"page_body\">";4457my$comment=$tag{'comment'};4458foreachmy$line(@$comment) {4459chomp$line;4460print esc_html($line, -nbsp=>1) ."<br/>\n";4461}4462print"</div>\n";4463 git_footer_html();4464}44654466sub git_blame {4467my$fd;4468my$ftype;44694470 gitweb_check_feature('blame')4471or die_error(403,"Blame view not allowed");44724473 die_error(400,"No file name given")unless$file_name;4474$hash_base||= git_get_head_hash($project);4475 die_error(404,"Couldn't find base commit")unless($hash_base);4476my%co= parse_commit($hash_base)4477or die_error(404,"Commit not found");4478if(!defined$hash) {4479$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4480or die_error(404,"Error looking up file");4481}4482$ftype= git_get_type($hash);4483if($ftype!~"blob") {4484 die_error(400,"Object is not a blob");4485}4486open($fd,"-|", git_cmd(),"blame",'-p','--',4487$file_name,$hash_base)4488or die_error(500,"Open git-blame failed");4489 git_header_html();4490my$formats_nav=4491$cgi->a({-href => href(action=>"blob", -replay=>1)},4492"blob") .4493" | ".4494$cgi->a({-href => href(action=>"history", -replay=>1)},4495"history") .4496" | ".4497$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4498"HEAD");4499 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4500 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4501 git_print_page_path($file_name,$ftype,$hash_base);4502my@rev_color= (qw(light2 dark2));4503my$num_colors=scalar(@rev_color);4504my$current_color=0;4505my$last_rev;4506print<<HTML;4507<div class="page_body">4508<table class="blame">4509<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4510HTML4511my%metainfo= ();4512while(1) {4513$_= <$fd>;4514last unlessdefined$_;4515my($full_rev,$orig_lineno,$lineno,$group_size) =4516/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/;4517if(!exists$metainfo{$full_rev}) {4518$metainfo{$full_rev} = {};4519}4520my$meta=$metainfo{$full_rev};4521while(<$fd>) {4522last if(s/^\t//);4523if(/^(\S+) (.*)$/) {4524$meta->{$1} =$2;4525}4526}4527my$data=$_;4528chomp$data;4529my$rev=substr($full_rev,0,8);4530my$author=$meta->{'author'};4531my%date= parse_date($meta->{'author-time'},4532$meta->{'author-tz'});4533my$date=$date{'iso-tz'};4534if($group_size) {4535$current_color= ++$current_color%$num_colors;4536}4537print"<tr class=\"$rev_color[$current_color]\">\n";4538if($group_size) {4539print"<td class=\"sha1\"";4540print" title=\"". esc_html($author) .",$date\"";4541print" rowspan=\"$group_size\""if($group_size>1);4542print">";4543print$cgi->a({-href => href(action=>"commit",4544 hash=>$full_rev,4545 file_name=>$file_name)},4546 esc_html($rev));4547print"</td>\n";4548}4549open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4550or die_error(500,"Open git-rev-parse failed");4551my$parent_commit= <$dd>;4552close$dd;4553chomp($parent_commit);4554my$blamed= href(action =>'blame',4555 file_name =>$meta->{'filename'},4556 hash_base =>$parent_commit);4557print"<td class=\"linenr\">";4558print$cgi->a({ -href =>"$blamed#l$orig_lineno",4559-id =>"l$lineno",4560-class=>"linenr"},4561 esc_html($lineno));4562print"</td>";4563print"<td class=\"pre\">". esc_html($data) ."</td>\n";4564print"</tr>\n";4565}4566print"</table>\n";4567print"</div>";4568close$fd4569or print"Reading blob failed\n";4570 git_footer_html();4571}45724573sub git_tags {4574my$head= git_get_head_hash($project);4575 git_header_html();4576 git_print_page_nav('','',$head,undef,$head);4577 git_print_header_div('summary',$project);45784579my@tagslist= git_get_tags_list();4580if(@tagslist) {4581 git_tags_body(\@tagslist);4582}4583 git_footer_html();4584}45854586sub git_heads {4587my$head= git_get_head_hash($project);4588 git_header_html();4589 git_print_page_nav('','',$head,undef,$head);4590 git_print_header_div('summary',$project);45914592my@headslist= git_get_heads_list();4593if(@headslist) {4594 git_heads_body(\@headslist,$head);4595}4596 git_footer_html();4597}45984599sub git_blob_plain {4600my$type=shift;4601my$expires;46024603if(!defined$hash) {4604if(defined$file_name) {4605my$base=$hash_base|| git_get_head_hash($project);4606$hash= git_get_hash_by_path($base,$file_name,"blob")4607or die_error(404,"Cannot find file");4608}else{4609 die_error(400,"No file name defined");4610}4611}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4612# blobs defined by non-textual hash id's can be cached4613$expires="+1d";4614}46154616open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4617or die_error(500,"Open git-cat-file blob '$hash' failed");46184619# content-type (can include charset)4620$type= blob_contenttype($fd,$file_name,$type);46214622# "save as" filename, even when no $file_name is given4623my$save_as="$hash";4624if(defined$file_name) {4625$save_as=$file_name;4626}elsif($type=~m/^text\//) {4627$save_as.='.txt';4628}46294630print$cgi->header(4631-type =>$type,4632-expires =>$expires,4633-content_disposition =>'inline; filename="'.$save_as.'"');4634undef$/;4635binmode STDOUT,':raw';4636print<$fd>;4637binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4638$/="\n";4639close$fd;4640}46414642sub git_blob {4643my$expires;46444645if(!defined$hash) {4646if(defined$file_name) {4647my$base=$hash_base|| git_get_head_hash($project);4648$hash= git_get_hash_by_path($base,$file_name,"blob")4649or die_error(404,"Cannot find file");4650}else{4651 die_error(400,"No file name defined");4652}4653}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4654# blobs defined by non-textual hash id's can be cached4655$expires="+1d";4656}46574658my($have_blame) = gitweb_check_feature('blame');4659open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4660or die_error(500,"Couldn't cat$file_name,$hash");4661my$mimetype= blob_mimetype($fd,$file_name);4662if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4663close$fd;4664return git_blob_plain($mimetype);4665}4666# we can have blame only for text/* mimetype4667$have_blame&&= ($mimetype=~m!^text/!);46684669 git_header_html(undef,$expires);4670my$formats_nav='';4671if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4672if(defined$file_name) {4673if($have_blame) {4674$formats_nav.=4675$cgi->a({-href => href(action=>"blame", -replay=>1)},4676"blame") .4677" | ";4678}4679$formats_nav.=4680$cgi->a({-href => href(action=>"history", -replay=>1)},4681"history") .4682" | ".4683$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4684"raw") .4685" | ".4686$cgi->a({-href => href(action=>"blob",4687 hash_base=>"HEAD", file_name=>$file_name)},4688"HEAD");4689}else{4690$formats_nav.=4691$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4692"raw");4693}4694 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4695 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4696}else{4697print"<div class=\"page_nav\">\n".4698"<br/><br/></div>\n".4699"<div class=\"title\">$hash</div>\n";4700}4701 git_print_page_path($file_name,"blob",$hash_base);4702print"<div class=\"page_body\">\n";4703if($mimetype=~m!^image/!) {4704print qq!<img type="$mimetype"!;4705if($file_name) {4706print qq! alt="$file_name" title="$file_name"!;4707}4708print qq! src="! .4709 href(action=>"blob_plain", hash=>$hash,4710 hash_base=>$hash_base, file_name=>$file_name) .4711 qq!"/>\n!;4712}else{4713my$nr;4714while(my$line= <$fd>) {4715chomp$line;4716$nr++;4717$line= untabify($line);4718printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4719$nr,$nr,$nr, esc_html($line, -nbsp=>1);4720}4721}4722close$fd4723or print"Reading blob failed.\n";4724print"</div>";4725 git_footer_html();4726}47274728sub git_tree {4729if(!defined$hash_base) {4730$hash_base="HEAD";4731}4732if(!defined$hash) {4733if(defined$file_name) {4734$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4735}else{4736$hash=$hash_base;4737}4738}4739 die_error(404,"No such tree")unlessdefined($hash);4740$/="\0";4741open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4742or die_error(500,"Open git-ls-tree failed");4743my@entries=map{chomp;$_} <$fd>;4744close$fdor die_error(404,"Reading tree failed");4745$/="\n";47464747my$refs= git_get_references();4748my$ref= format_ref_marker($refs,$hash_base);4749 git_header_html();4750my$basedir='';4751my($have_blame) = gitweb_check_feature('blame');4752if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4753my@views_nav= ();4754if(defined$file_name) {4755push@views_nav,4756$cgi->a({-href => href(action=>"history", -replay=>1)},4757"history"),4758$cgi->a({-href => href(action=>"tree",4759 hash_base=>"HEAD", file_name=>$file_name)},4760"HEAD"),4761}4762my$snapshot_links= format_snapshot_links($hash);4763if(defined$snapshot_links) {4764# FIXME: Should be available when we have no hash base as well.4765push@views_nav,$snapshot_links;4766}4767 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4768 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4769}else{4770undef$hash_base;4771print"<div class=\"page_nav\">\n";4772print"<br/><br/></div>\n";4773print"<div class=\"title\">$hash</div>\n";4774}4775if(defined$file_name) {4776$basedir=$file_name;4777if($basedirne''&&substr($basedir, -1)ne'/') {4778$basedir.='/';4779}4780 git_print_page_path($file_name,'tree',$hash_base);4781}4782print"<div class=\"page_body\">\n";4783print"<table class=\"tree\">\n";4784my$alternate=1;4785# '..' (top directory) link if possible4786if(defined$hash_base&&4787defined$file_name&&$file_name=~m![^/]+$!) {4788if($alternate) {4789print"<tr class=\"dark\">\n";4790}else{4791print"<tr class=\"light\">\n";4792}4793$alternate^=1;47944795my$up=$file_name;4796$up=~s!/?[^/]+$!!;4797undef$upunless$up;4798# based on git_print_tree_entry4799print'<td class="mode">'. mode_str('040000') ."</td>\n";4800print'<td class="list">';4801print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4802 file_name=>$up)},4803"..");4804print"</td>\n";4805print"<td class=\"link\"></td>\n";48064807print"</tr>\n";4808}4809foreachmy$line(@entries) {4810my%t= parse_ls_tree_line($line, -z =>1);48114812if($alternate) {4813print"<tr class=\"dark\">\n";4814}else{4815print"<tr class=\"light\">\n";4816}4817$alternate^=1;48184819 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);48204821print"</tr>\n";4822}4823print"</table>\n".4824"</div>";4825 git_footer_html();4826}48274828sub git_snapshot {4829my@supported_fmts= gitweb_check_feature('snapshot');4830@supported_fmts= filter_snapshot_fmts(@supported_fmts);48314832my$format=$input_params{'snapshot_format'};4833if(!@supported_fmts) {4834 die_error(403,"Snapshots not allowed");4835}4836# default to first supported snapshot format4837$format||=$supported_fmts[0];4838if($format!~m/^[a-z0-9]+$/) {4839 die_error(400,"Invalid snapshot format parameter");4840}elsif(!exists($known_snapshot_formats{$format})) {4841 die_error(400,"Unknown snapshot format");4842}elsif(!grep($_eq$format,@supported_fmts)) {4843 die_error(403,"Unsupported snapshot format");4844}48454846if(!defined$hash) {4847$hash= git_get_head_hash($project);4848}48494850my$name=$project;4851$name=~ s,([^/])/*\.git$,$1,;4852$name= basename($name);4853my$filename= to_utf8($name);4854$name=~s/\047/\047\\\047\047/g;4855my$cmd;4856$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";4857$cmd= quote_command(4858 git_cmd(),'archive',4859"--format=$known_snapshot_formats{$format}{'format'}",4860"--prefix=$name/",$hash);4861if(exists$known_snapshot_formats{$format}{'compressor'}) {4862$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});4863}48644865print$cgi->header(4866-type =>$known_snapshot_formats{$format}{'type'},4867-content_disposition =>'inline; filename="'."$filename".'"',4868-status =>'200 OK');48694870open my$fd,"-|",$cmd4871or die_error(500,"Execute git-archive failed");4872binmode STDOUT,':raw';4873print<$fd>;4874binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4875close$fd;4876}48774878sub git_log {4879my$head= git_get_head_hash($project);4880if(!defined$hash) {4881$hash=$head;4882}4883if(!defined$page) {4884$page=0;4885}4886my$refs= git_get_references();48874888my@commitlist= parse_commits($hash,101, (100*$page));48894890my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);48914892 git_header_html();4893 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);48944895if(!@commitlist) {4896my%co= parse_commit($hash);48974898 git_print_header_div('summary',$project);4899print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";4900}4901my$to= ($#commitlist>=99) ? (99) : ($#commitlist);4902for(my$i=0;$i<=$to;$i++) {4903my%co= %{$commitlist[$i]};4904next if!%co;4905my$commit=$co{'id'};4906my$ref= format_ref_marker($refs,$commit);4907my%ad= parse_date($co{'author_epoch'});4908 git_print_header_div('commit',4909"<span class=\"age\">$co{'age_string'}</span>".4910 esc_html($co{'title'}) .$ref,4911$commit);4912print"<div class=\"title_text\">\n".4913"<div class=\"log_link\">\n".4914$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4915" | ".4916$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4917" | ".4918$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4919"<br/>\n".4920"</div>\n".4921"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".4922"</div>\n";49234924print"<div class=\"log_body\">\n";4925 git_print_log($co{'comment'}, -final_empty_line=>1);4926print"</div>\n";4927}4928if($#commitlist>=100) {4929print"<div class=\"page_nav\">\n";4930print$cgi->a({-href => href(-replay=>1, page=>$page+1),4931-accesskey =>"n", -title =>"Alt-n"},"next");4932print"</div>\n";4933}4934 git_footer_html();4935}49364937sub git_commit {4938$hash||=$hash_base||"HEAD";4939my%co= parse_commit($hash)4940or die_error(404,"Unknown commit object");4941my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});4942my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});49434944my$parent=$co{'parent'};4945my$parents=$co{'parents'};# listref49464947# we need to prepare $formats_nav before any parameter munging4948my$formats_nav;4949if(!defined$parent) {4950# --root commitdiff4951$formats_nav.='(initial)';4952}elsif(@$parents==1) {4953# single parent commit4954$formats_nav.=4955'(parent: '.4956$cgi->a({-href => href(action=>"commit",4957 hash=>$parent)},4958 esc_html(substr($parent,0,7))) .4959')';4960}else{4961# merge commit4962$formats_nav.=4963'(merge: '.4964join(' ',map{4965$cgi->a({-href => href(action=>"commit",4966 hash=>$_)},4967 esc_html(substr($_,0,7)));4968}@$parents) .4969')';4970}49714972if(!defined$parent) {4973$parent="--root";4974}4975my@difftree;4976open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",4977@diff_opts,4978(@$parents<=1?$parent:'-c'),4979$hash,"--"4980or die_error(500,"Open git-diff-tree failed");4981@difftree=map{chomp;$_} <$fd>;4982close$fdor die_error(404,"Reading git-diff-tree failed");49834984# non-textual hash id's can be cached4985my$expires;4986if($hash=~m/^[0-9a-fA-F]{40}$/) {4987$expires="+1d";4988}4989my$refs= git_get_references();4990my$ref= format_ref_marker($refs,$co{'id'});49914992 git_header_html(undef,$expires);4993 git_print_page_nav('commit','',4994$hash,$co{'tree'},$hash,4995$formats_nav);49964997if(defined$co{'parent'}) {4998 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);4999}else{5000 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5001}5002print"<div class=\"title_text\">\n".5003"<table class=\"object_header\">\n";5004print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5005"<tr>".5006"<td></td><td>$ad{'rfc2822'}";5007if($ad{'hour_local'} <6) {5008printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5009$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5010}else{5011printf(" (%02d:%02d%s)",5012$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5013}5014print"</td>".5015"</tr>\n";5016print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5017print"<tr><td></td><td>$cd{'rfc2822'}".5018sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5019"</td></tr>\n";5020print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5021print"<tr>".5022"<td>tree</td>".5023"<td class=\"sha1\">".5024$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5025class=>"list"},$co{'tree'}) .5026"</td>".5027"<td class=\"link\">".5028$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5029"tree");5030my$snapshot_links= format_snapshot_links($hash);5031if(defined$snapshot_links) {5032print" | ".$snapshot_links;5033}5034print"</td>".5035"</tr>\n";50365037foreachmy$par(@$parents) {5038print"<tr>".5039"<td>parent</td>".5040"<td class=\"sha1\">".5041$cgi->a({-href => href(action=>"commit", hash=>$par),5042class=>"list"},$par) .5043"</td>".5044"<td class=\"link\">".5045$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5046" | ".5047$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5048"</td>".5049"</tr>\n";5050}5051print"</table>".5052"</div>\n";50535054print"<div class=\"page_body\">\n";5055 git_print_log($co{'comment'});5056print"</div>\n";50575058 git_difftree_body(\@difftree,$hash,@$parents);50595060 git_footer_html();5061}50625063sub git_object {5064# object is defined by:5065# - hash or hash_base alone5066# - hash_base and file_name5067my$type;50685069# - hash or hash_base alone5070if($hash|| ($hash_base&& !defined$file_name)) {5071my$object_id=$hash||$hash_base;50725073open my$fd,"-|", quote_command(5074 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5075or die_error(404,"Object does not exist");5076$type= <$fd>;5077chomp$type;5078close$fd5079or die_error(404,"Object does not exist");50805081# - hash_base and file_name5082}elsif($hash_base&&defined$file_name) {5083$file_name=~ s,/+$,,;50845085system(git_cmd(),"cat-file",'-e',$hash_base) ==05086or die_error(404,"Base object does not exist");50875088# here errors should not hapen5089open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5090or die_error(500,"Open git-ls-tree failed");5091my$line= <$fd>;5092close$fd;50935094#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5095unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5096 die_error(404,"File or directory for given base does not exist");5097}5098$type=$2;5099$hash=$3;5100}else{5101 die_error(400,"Not enough information to find object");5102}51035104print$cgi->redirect(-uri => href(action=>$type, -full=>1,5105 hash=>$hash, hash_base=>$hash_base,5106 file_name=>$file_name),5107-status =>'302 Found');5108}51095110sub git_blobdiff {5111my$format=shift||'html';51125113my$fd;5114my@difftree;5115my%diffinfo;5116my$expires;51175118# preparing $fd and %diffinfo for git_patchset_body5119# new style URI5120if(defined$hash_base&&defined$hash_parent_base) {5121if(defined$file_name) {5122# read raw output5123open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5124$hash_parent_base,$hash_base,5125"--", (defined$file_parent?$file_parent: ()),$file_name5126or die_error(500,"Open git-diff-tree failed");5127@difftree=map{chomp;$_} <$fd>;5128close$fd5129or die_error(404,"Reading git-diff-tree failed");5130@difftree5131or die_error(404,"Blob diff not found");51325133}elsif(defined$hash&&5134$hash=~/[0-9a-fA-F]{40}/) {5135# try to find filename from $hash51365137# read filtered raw output5138open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5139$hash_parent_base,$hash_base,"--"5140or die_error(500,"Open git-diff-tree failed");5141@difftree=5142# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5143# $hash == to_id5144grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5145map{chomp;$_} <$fd>;5146close$fd5147or die_error(404,"Reading git-diff-tree failed");5148@difftree5149or die_error(404,"Blob diff not found");51505151}else{5152 die_error(400,"Missing one of the blob diff parameters");5153}51545155if(@difftree>1) {5156 die_error(400,"Ambiguous blob diff specification");5157}51585159%diffinfo= parse_difftree_raw_line($difftree[0]);5160$file_parent||=$diffinfo{'from_file'} ||$file_name;5161$file_name||=$diffinfo{'to_file'};51625163$hash_parent||=$diffinfo{'from_id'};5164$hash||=$diffinfo{'to_id'};51655166# non-textual hash id's can be cached5167if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5168$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5169$expires='+1d';5170}51715172# open patch output5173open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5174'-p', ($formateq'html'?"--full-index": ()),5175$hash_parent_base,$hash_base,5176"--", (defined$file_parent?$file_parent: ()),$file_name5177or die_error(500,"Open git-diff-tree failed");5178}51795180# old/legacy style URI5181if(!%diffinfo&&# if new style URI failed5182defined$hash&&defined$hash_parent) {5183# fake git-diff-tree raw output5184$diffinfo{'from_mode'} =$diffinfo{'to_mode'} ="blob";5185$diffinfo{'from_id'} =$hash_parent;5186$diffinfo{'to_id'} =$hash;5187if(defined$file_name) {5188if(defined$file_parent) {5189$diffinfo{'status'} ='2';5190$diffinfo{'from_file'} =$file_parent;5191$diffinfo{'to_file'} =$file_name;5192}else{# assume not renamed5193$diffinfo{'status'} ='1';5194$diffinfo{'from_file'} =$file_name;5195$diffinfo{'to_file'} =$file_name;5196}5197}else{# no filename given5198$diffinfo{'status'} ='2';5199$diffinfo{'from_file'} =$hash_parent;5200$diffinfo{'to_file'} =$hash;5201}52025203# non-textual hash id's can be cached5204if($hash=~m/^[0-9a-fA-F]{40}$/&&5205$hash_parent=~m/^[0-9a-fA-F]{40}$/) {5206$expires='+1d';5207}52085209# open patch output5210open$fd,"-|", git_cmd(),"diff",@diff_opts,5211'-p', ($formateq'html'?"--full-index": ()),5212$hash_parent,$hash,"--"5213or die_error(500,"Open git-diff failed");5214}else{5215 die_error(400,"Missing one of the blob diff parameters")5216unless%diffinfo;5217}52185219# header5220if($formateq'html') {5221my$formats_nav=5222$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5223"raw");5224 git_header_html(undef,$expires);5225if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5226 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5227 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5228}else{5229print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5230print"<div class=\"title\">$hashvs$hash_parent</div>\n";5231}5232if(defined$file_name) {5233 git_print_page_path($file_name,"blob",$hash_base);5234}else{5235print"<div class=\"page_path\"></div>\n";5236}52375238}elsif($formateq'plain') {5239print$cgi->header(5240-type =>'text/plain',5241-charset =>'utf-8',5242-expires =>$expires,5243-content_disposition =>'inline; filename="'."$file_name".'.patch"');52445245print"X-Git-Url: ".$cgi->self_url() ."\n\n";52465247}else{5248 die_error(400,"Unknown blobdiff format");5249}52505251# patch5252if($formateq'html') {5253print"<div class=\"page_body\">\n";52545255 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5256close$fd;52575258print"</div>\n";# class="page_body"5259 git_footer_html();52605261}else{5262while(my$line= <$fd>) {5263$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5264$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;52655266print$line;52675268last if$line=~m!^\+\+\+!;5269}5270local$/=undef;5271print<$fd>;5272close$fd;5273}5274}52755276sub git_blobdiff_plain {5277 git_blobdiff('plain');5278}52795280sub git_commitdiff {5281my$format=shift||'html';5282$hash||=$hash_base||"HEAD";5283my%co= parse_commit($hash)5284or die_error(404,"Unknown commit object");52855286# choose format for commitdiff for merge5287if(!defined$hash_parent&& @{$co{'parents'}} >1) {5288$hash_parent='--cc';5289}5290# we need to prepare $formats_nav before almost any parameter munging5291my$formats_nav;5292if($formateq'html') {5293$formats_nav=5294$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5295"raw");52965297if(defined$hash_parent&&5298$hash_parentne'-c'&&$hash_parentne'--cc') {5299# commitdiff with two commits given5300my$hash_parent_short=$hash_parent;5301if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5302$hash_parent_short=substr($hash_parent,0,7);5303}5304$formats_nav.=5305' (from';5306for(my$i=0;$i< @{$co{'parents'}};$i++) {5307if($co{'parents'}[$i]eq$hash_parent) {5308$formats_nav.=' parent '. ($i+1);5309last;5310}5311}5312$formats_nav.=': '.5313$cgi->a({-href => href(action=>"commitdiff",5314 hash=>$hash_parent)},5315 esc_html($hash_parent_short)) .5316')';5317}elsif(!$co{'parent'}) {5318# --root commitdiff5319$formats_nav.=' (initial)';5320}elsif(scalar@{$co{'parents'}} ==1) {5321# single parent commit5322$formats_nav.=5323' (parent: '.5324$cgi->a({-href => href(action=>"commitdiff",5325 hash=>$co{'parent'})},5326 esc_html(substr($co{'parent'},0,7))) .5327')';5328}else{5329# merge commit5330if($hash_parenteq'--cc') {5331$formats_nav.=' | '.5332$cgi->a({-href => href(action=>"commitdiff",5333 hash=>$hash, hash_parent=>'-c')},5334'combined');5335}else{# $hash_parent eq '-c'5336$formats_nav.=' | '.5337$cgi->a({-href => href(action=>"commitdiff",5338 hash=>$hash, hash_parent=>'--cc')},5339'compact');5340}5341$formats_nav.=5342' (merge: '.5343join(' ',map{5344$cgi->a({-href => href(action=>"commitdiff",5345 hash=>$_)},5346 esc_html(substr($_,0,7)));5347} @{$co{'parents'}} ) .5348')';5349}5350}53515352my$hash_parent_param=$hash_parent;5353if(!defined$hash_parent_param) {5354# --cc for multiple parents, --root for parentless5355$hash_parent_param=5356@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5357}53585359# read commitdiff5360my$fd;5361my@difftree;5362if($formateq'html') {5363open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5364"--no-commit-id","--patch-with-raw","--full-index",5365$hash_parent_param,$hash,"--"5366or die_error(500,"Open git-diff-tree failed");53675368while(my$line= <$fd>) {5369chomp$line;5370# empty line ends raw part of diff-tree output5371last unless$line;5372push@difftree,scalar parse_difftree_raw_line($line);5373}53745375}elsif($formateq'plain') {5376open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5377'-p',$hash_parent_param,$hash,"--"5378or die_error(500,"Open git-diff-tree failed");53795380}else{5381 die_error(400,"Unknown commitdiff format");5382}53835384# non-textual hash id's can be cached5385my$expires;5386if($hash=~m/^[0-9a-fA-F]{40}$/) {5387$expires="+1d";5388}53895390# write commit message5391if($formateq'html') {5392my$refs= git_get_references();5393my$ref= format_ref_marker($refs,$co{'id'});53945395 git_header_html(undef,$expires);5396 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5397 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5398 git_print_authorship(\%co);5399print"<div class=\"page_body\">\n";5400if(@{$co{'comment'}} >1) {5401print"<div class=\"log\">\n";5402 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5403print"</div>\n";# class="log"5404}54055406}elsif($formateq'plain') {5407my$refs= git_get_references("tags");5408my$tagname= git_get_rev_name_tags($hash);5409my$filename= basename($project) ."-$hash.patch";54105411print$cgi->header(5412-type =>'text/plain',5413-charset =>'utf-8',5414-expires =>$expires,5415-content_disposition =>'inline; filename="'."$filename".'"');5416my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5417print"From: ". to_utf8($co{'author'}) ."\n";5418print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5419print"Subject: ". to_utf8($co{'title'}) ."\n";54205421print"X-Git-Tag:$tagname\n"if$tagname;5422print"X-Git-Url: ".$cgi->self_url() ."\n\n";54235424foreachmy$line(@{$co{'comment'}}) {5425print to_utf8($line) ."\n";5426}5427print"---\n\n";5428}54295430# write patch5431if($formateq'html') {5432my$use_parents= !defined$hash_parent||5433$hash_parenteq'-c'||$hash_parenteq'--cc';5434 git_difftree_body(\@difftree,$hash,5435$use_parents? @{$co{'parents'}} :$hash_parent);5436print"<br/>\n";54375438 git_patchset_body($fd, \@difftree,$hash,5439$use_parents? @{$co{'parents'}} :$hash_parent);5440close$fd;5441print"</div>\n";# class="page_body"5442 git_footer_html();54435444}elsif($formateq'plain') {5445local$/=undef;5446print<$fd>;5447close$fd5448or print"Reading git-diff-tree failed\n";5449}5450}54515452sub git_commitdiff_plain {5453 git_commitdiff('plain');5454}54555456sub git_history {5457if(!defined$hash_base) {5458$hash_base= git_get_head_hash($project);5459}5460if(!defined$page) {5461$page=0;5462}5463my$ftype;5464my%co= parse_commit($hash_base)5465or die_error(404,"Unknown commit object");54665467my$refs= git_get_references();5468my$limit=sprintf("--max-count=%i", (100* ($page+1)));54695470my@commitlist= parse_commits($hash_base,101, (100*$page),5471$file_name,"--full-history")5472or die_error(404,"No such file or directory on given branch");54735474if(!defined$hash&&defined$file_name) {5475# some commits could have deleted file in question,5476# and not have it in tree, but one of them has to have it5477for(my$i=0;$i<=@commitlist;$i++) {5478$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5479last ifdefined$hash;5480}5481}5482if(defined$hash) {5483$ftype= git_get_type($hash);5484}5485if(!defined$ftype) {5486 die_error(500,"Unknown type of object");5487}54885489my$paging_nav='';5490if($page>0) {5491$paging_nav.=5492$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5493 file_name=>$file_name)},5494"first");5495$paging_nav.=" ⋅ ".5496$cgi->a({-href => href(-replay=>1, page=>$page-1),5497-accesskey =>"p", -title =>"Alt-p"},"prev");5498}else{5499$paging_nav.="first";5500$paging_nav.=" ⋅ prev";5501}5502my$next_link='';5503if($#commitlist>=100) {5504$next_link=5505$cgi->a({-href => href(-replay=>1, page=>$page+1),5506-accesskey =>"n", -title =>"Alt-n"},"next");5507$paging_nav.=" ⋅$next_link";5508}else{5509$paging_nav.=" ⋅ next";5510}55115512 git_header_html();5513 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5514 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5515 git_print_page_path($file_name,$ftype,$hash_base);55165517 git_history_body(\@commitlist,0,99,5518$refs,$hash_base,$ftype,$next_link);55195520 git_footer_html();5521}55225523sub git_search {5524 gitweb_check_feature('search')or die_error(403,"Search is disabled");5525if(!defined$searchtext) {5526 die_error(400,"Text field is empty");5527}5528if(!defined$hash) {5529$hash= git_get_head_hash($project);5530}5531my%co= parse_commit($hash);5532if(!%co) {5533 die_error(404,"Unknown commit object");5534}5535if(!defined$page) {5536$page=0;5537}55385539$searchtype||='commit';5540if($searchtypeeq'pickaxe') {5541# pickaxe may take all resources of your box and run for several minutes5542# with every query - so decide by yourself how public you make this feature5543 gitweb_check_feature('pickaxe')5544or die_error(403,"Pickaxe is disabled");5545}5546if($searchtypeeq'grep') {5547 gitweb_check_feature('grep')5548or die_error(403,"Grep is disabled");5549}55505551 git_header_html();55525553if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5554my$greptype;5555if($searchtypeeq'commit') {5556$greptype="--grep=";5557}elsif($searchtypeeq'author') {5558$greptype="--author=";5559}elsif($searchtypeeq'committer') {5560$greptype="--committer=";5561}5562$greptype.=$searchtext;5563my@commitlist= parse_commits($hash,101, (100*$page),undef,5564$greptype,'--regexp-ignore-case',5565$search_use_regexp?'--extended-regexp':'--fixed-strings');55665567my$paging_nav='';5568if($page>0) {5569$paging_nav.=5570$cgi->a({-href => href(action=>"search", hash=>$hash,5571 searchtext=>$searchtext,5572 searchtype=>$searchtype)},5573"first");5574$paging_nav.=" ⋅ ".5575$cgi->a({-href => href(-replay=>1, page=>$page-1),5576-accesskey =>"p", -title =>"Alt-p"},"prev");5577}else{5578$paging_nav.="first";5579$paging_nav.=" ⋅ prev";5580}5581my$next_link='';5582if($#commitlist>=100) {5583$next_link=5584$cgi->a({-href => href(-replay=>1, page=>$page+1),5585-accesskey =>"n", -title =>"Alt-n"},"next");5586$paging_nav.=" ⋅$next_link";5587}else{5588$paging_nav.=" ⋅ next";5589}55905591if($#commitlist>=100) {5592}55935594 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5595 git_print_header_div('commit', esc_html($co{'title'}),$hash);5596 git_search_grep_body(\@commitlist,0,99,$next_link);5597}55985599if($searchtypeeq'pickaxe') {5600 git_print_page_nav('','',$hash,$co{'tree'},$hash);5601 git_print_header_div('commit', esc_html($co{'title'}),$hash);56025603print"<table class=\"pickaxe search\">\n";5604my$alternate=1;5605$/="\n";5606open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5607'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5608($search_use_regexp?'--pickaxe-regex': ());5609undef%co;5610my@files;5611while(my$line= <$fd>) {5612chomp$line;5613next unless$line;56145615my%set= parse_difftree_raw_line($line);5616if(defined$set{'commit'}) {5617# finish previous commit5618if(%co) {5619print"</td>\n".5620"<td class=\"link\">".5621$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5622" | ".5623$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5624print"</td>\n".5625"</tr>\n";5626}56275628if($alternate) {5629print"<tr class=\"dark\">\n";5630}else{5631print"<tr class=\"light\">\n";5632}5633$alternate^=1;5634%co= parse_commit($set{'commit'});5635my$author= chop_and_escape_str($co{'author_name'},15,5);5636print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5637"<td><i>$author</i></td>\n".5638"<td>".5639$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5640-class=>"list subject"},5641 chop_and_escape_str($co{'title'},50) ."<br/>");5642}elsif(defined$set{'to_id'}) {5643next if($set{'to_id'} =~m/^0{40}$/);56445645print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5646 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5647-class=>"list"},5648"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5649"<br/>\n";5650}5651}5652close$fd;56535654# finish last commit (warning: repetition!)5655if(%co) {5656print"</td>\n".5657"<td class=\"link\">".5658$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5659" | ".5660$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5661print"</td>\n".5662"</tr>\n";5663}56645665print"</table>\n";5666}56675668if($searchtypeeq'grep') {5669 git_print_page_nav('','',$hash,$co{'tree'},$hash);5670 git_print_header_div('commit', esc_html($co{'title'}),$hash);56715672print"<table class=\"grep_search\">\n";5673my$alternate=1;5674my$matches=0;5675$/="\n";5676open my$fd,"-|", git_cmd(),'grep','-n',5677$search_use_regexp? ('-E','-i') :'-F',5678$searchtext,$co{'tree'};5679my$lastfile='';5680while(my$line= <$fd>) {5681chomp$line;5682my($file,$lno,$ltext,$binary);5683last if($matches++>1000);5684if($line=~/^Binary file (.+) matches$/) {5685$file=$1;5686$binary=1;5687}else{5688(undef,$file,$lno,$ltext) =split(/:/,$line,4);5689}5690if($filene$lastfile) {5691$lastfileand print"</td></tr>\n";5692if($alternate++) {5693print"<tr class=\"dark\">\n";5694}else{5695print"<tr class=\"light\">\n";5696}5697print"<td class=\"list\">".5698$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5699 file_name=>"$file"),5700-class=>"list"}, esc_path($file));5701print"</td><td>\n";5702$lastfile=$file;5703}5704if($binary) {5705print"<div class=\"binary\">Binary file</div>\n";5706}else{5707$ltext= untabify($ltext);5708if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5709$ltext= esc_html($1, -nbsp=>1);5710$ltext.='<span class="match">';5711$ltext.= esc_html($2, -nbsp=>1);5712$ltext.='</span>';5713$ltext.= esc_html($3, -nbsp=>1);5714}else{5715$ltext= esc_html($ltext, -nbsp=>1);5716}5717print"<div class=\"pre\">".5718$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5719 file_name=>"$file").'#l'.$lno,5720-class=>"linenr"},sprintf('%4i',$lno))5721.' '.$ltext."</div>\n";5722}5723}5724if($lastfile) {5725print"</td></tr>\n";5726if($matches>1000) {5727print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5728}5729}else{5730print"<div class=\"diff nodifferences\">No matches found</div>\n";5731}5732close$fd;57335734print"</table>\n";5735}5736 git_footer_html();5737}57385739sub git_search_help {5740 git_header_html();5741 git_print_page_nav('','',$hash,$hash,$hash);5742print<<EOT;5743<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5744regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5745the pattern entered is recognized as the POSIX extended5746<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5747insensitive).</p>5748<dl>5749<dt><b>commit</b></dt>5750<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5751EOT5752my($have_grep) = gitweb_check_feature('grep');5753if($have_grep) {5754print<<EOT;5755<dt><b>grep</b></dt>5756<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5757 a different one) are searched for the given pattern. On large trees, this search can take5758a while and put some strain on the server, so please use it with some consideration. Note that5759due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5760case-sensitive.</dd>5761EOT5762}5763print<<EOT;5764<dt><b>author</b></dt>5765<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5766<dt><b>committer</b></dt>5767<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5768EOT5769my($have_pickaxe) = gitweb_check_feature('pickaxe');5770if($have_pickaxe) {5771print<<EOT;5772<dt><b>pickaxe</b></dt>5773<dd>All commits that caused the string to appear or disappear from any file (changes that5774added, removed or "modified" the string) will be listed. This search can take a while and5775takes a lot of strain on the server, so please use it wisely. Note that since you may be5776interested even in changes just changing the case as well, this search is case sensitive.</dd>5777EOT5778}5779print"</dl>\n";5780 git_footer_html();5781}57825783sub git_shortlog {5784my$head= git_get_head_hash($project);5785if(!defined$hash) {5786$hash=$head;5787}5788if(!defined$page) {5789$page=0;5790}5791my$refs= git_get_references();57925793my$commit_hash=$hash;5794if(defined$hash_parent) {5795$commit_hash="$hash_parent..$hash";5796}5797my@commitlist= parse_commits($commit_hash,101, (100*$page));57985799my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);5800my$next_link='';5801if($#commitlist>=100) {5802$next_link=5803$cgi->a({-href => href(-replay=>1, page=>$page+1),5804-accesskey =>"n", -title =>"Alt-n"},"next");5805}58065807 git_header_html();5808 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);5809 git_print_header_div('summary',$project);58105811 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);58125813 git_footer_html();5814}58155816## ......................................................................5817## feeds (RSS, Atom; OPML)58185819sub git_feed {5820my$format=shift||'atom';5821my($have_blame) = gitweb_check_feature('blame');58225823# Atom: http://www.atomenabled.org/developers/syndication/5824# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ5825if($formatne'rss'&&$formatne'atom') {5826 die_error(400,"Unknown web feed format");5827}58285829# log/feed of current (HEAD) branch, log of given branch, history of file/directory5830my$head=$hash||'HEAD';5831my@commitlist= parse_commits($head,150,0,$file_name);58325833my%latest_commit;5834my%latest_date;5835my$content_type="application/$format+xml";5836if(defined$cgi->http('HTTP_ACCEPT') &&5837$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {5838# browser (feed reader) prefers text/xml5839$content_type='text/xml';5840}5841if(defined($commitlist[0])) {5842%latest_commit= %{$commitlist[0]};5843%latest_date= parse_date($latest_commit{'author_epoch'});5844print$cgi->header(5845-type =>$content_type,5846-charset =>'utf-8',5847-last_modified =>$latest_date{'rfc2822'});5848}else{5849print$cgi->header(5850-type =>$content_type,5851-charset =>'utf-8');5852}58535854# Optimization: skip generating the body if client asks only5855# for Last-Modified date.5856return if($cgi->request_method()eq'HEAD');58575858# header variables5859my$title="$site_name-$project/$action";5860my$feed_type='log';5861if(defined$hash) {5862$title.=" - '$hash'";5863$feed_type='branch log';5864if(defined$file_name) {5865$title.=" ::$file_name";5866$feed_type='history';5867}5868}elsif(defined$file_name) {5869$title.=" -$file_name";5870$feed_type='history';5871}5872$title.="$feed_type";5873my$descr= git_get_project_description($project);5874if(defined$descr) {5875$descr= esc_html($descr);5876}else{5877$descr="$project".5878($formateq'rss'?'RSS':'Atom') .5879" feed";5880}5881my$owner= git_get_project_owner($project);5882$owner= esc_html($owner);58835884#header5885my$alt_url;5886if(defined$file_name) {5887$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);5888}elsif(defined$hash) {5889$alt_url= href(-full=>1, action=>"log", hash=>$hash);5890}else{5891$alt_url= href(-full=>1, action=>"summary");5892}5893print qq!<?xml version="1.0" encoding="utf-8"?>\n!;5894if($formateq'rss') {5895print<<XML;5896<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">5897<channel>5898XML5899print"<title>$title</title>\n".5900"<link>$alt_url</link>\n".5901"<description>$descr</description>\n".5902"<language>en</language>\n";5903}elsif($formateq'atom') {5904print<<XML;5905<feed xmlns="http://www.w3.org/2005/Atom">5906XML5907print"<title>$title</title>\n".5908"<subtitle>$descr</subtitle>\n".5909'<link rel="alternate" type="text/html" href="'.5910$alt_url.'" />'."\n".5911'<link rel="self" type="'.$content_type.'" href="'.5912$cgi->self_url() .'" />'."\n".5913"<id>". href(-full=>1) ."</id>\n".5914# use project owner for feed author5915"<author><name>$owner</name></author>\n";5916if(defined$favicon) {5917print"<icon>". esc_url($favicon) ."</icon>\n";5918}5919if(defined$logo_url) {5920# not twice as wide as tall: 72 x 27 pixels5921print"<logo>". esc_url($logo) ."</logo>\n";5922}5923if(!%latest_date) {5924# dummy date to keep the feed valid until commits trickle in:5925print"<updated>1970-01-01T00:00:00Z</updated>\n";5926}else{5927print"<updated>$latest_date{'iso-8601'}</updated>\n";5928}5929}59305931# contents5932for(my$i=0;$i<=$#commitlist;$i++) {5933my%co= %{$commitlist[$i]};5934my$commit=$co{'id'};5935# we read 150, we always show 30 and the ones more recent than 48 hours5936if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {5937last;5938}5939my%cd= parse_date($co{'author_epoch'});59405941# get list of changed files5942open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5943$co{'parent'} ||"--root",5944$co{'id'},"--", (defined$file_name?$file_name: ())5945ornext;5946my@difftree=map{chomp;$_} <$fd>;5947close$fd5948ornext;59495950# print element (entry, item)5951my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);5952if($formateq'rss') {5953print"<item>\n".5954"<title>". esc_html($co{'title'}) ."</title>\n".5955"<author>". esc_html($co{'author'}) ."</author>\n".5956"<pubDate>$cd{'rfc2822'}</pubDate>\n".5957"<guid isPermaLink=\"true\">$co_url</guid>\n".5958"<link>$co_url</link>\n".5959"<description>". esc_html($co{'title'}) ."</description>\n".5960"<content:encoded>".5961"<![CDATA[\n";5962}elsif($formateq'atom') {5963print"<entry>\n".5964"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".5965"<updated>$cd{'iso-8601'}</updated>\n".5966"<author>\n".5967" <name>". esc_html($co{'author_name'}) ."</name>\n";5968if($co{'author_email'}) {5969print" <email>". esc_html($co{'author_email'}) ."</email>\n";5970}5971print"</author>\n".5972# use committer for contributor5973"<contributor>\n".5974" <name>". esc_html($co{'committer_name'}) ."</name>\n";5975if($co{'committer_email'}) {5976print" <email>". esc_html($co{'committer_email'}) ."</email>\n";5977}5978print"</contributor>\n".5979"<published>$cd{'iso-8601'}</published>\n".5980"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".5981"<id>$co_url</id>\n".5982"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".5983"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";5984}5985my$comment=$co{'comment'};5986print"<pre>\n";5987foreachmy$line(@$comment) {5988$line= esc_html($line);5989print"$line\n";5990}5991print"</pre><ul>\n";5992foreachmy$difftree_line(@difftree) {5993my%difftree= parse_difftree_raw_line($difftree_line);5994next if!$difftree{'from_id'};59955996my$file=$difftree{'file'} ||$difftree{'to_file'};59975998print"<li>".5999"[".6000$cgi->a({-href => href(-full=>1, action=>"blobdiff",6001 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6002 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6003 file_name=>$file, file_parent=>$difftree{'from_file'}),6004-title =>"diff"},'D');6005if($have_blame) {6006print$cgi->a({-href => href(-full=>1, action=>"blame",6007 file_name=>$file, hash_base=>$commit),6008-title =>"blame"},'B');6009}6010# if this is not a feed of a file history6011if(!defined$file_name||$file_namene$file) {6012print$cgi->a({-href => href(-full=>1, action=>"history",6013 file_name=>$file, hash=>$commit),6014-title =>"history"},'H');6015}6016$file= esc_path($file);6017print"] ".6018"$file</li>\n";6019}6020if($formateq'rss') {6021print"</ul>]]>\n".6022"</content:encoded>\n".6023"</item>\n";6024}elsif($formateq'atom') {6025print"</ul>\n</div>\n".6026"</content>\n".6027"</entry>\n";6028}6029}60306031# end of feed6032if($formateq'rss') {6033print"</channel>\n</rss>\n";6034}elsif($formateq'atom') {6035print"</feed>\n";6036}6037}60386039sub git_rss {6040 git_feed('rss');6041}60426043sub git_atom {6044 git_feed('atom');6045}60466047sub git_opml {6048my@list= git_get_projects_list();60496050print$cgi->header(-type =>'text/xml', -charset =>'utf-8');6051print<<XML;6052<?xml version="1.0" encoding="utf-8"?>6053<opml version="1.0">6054<head>6055 <title>$site_nameOPML Export</title>6056</head>6057<body>6058<outline text="git RSS feeds">6059XML60606061foreachmy$pr(@list) {6062my%proj=%$pr;6063my$head= git_get_head_hash($proj{'path'});6064if(!defined$head) {6065next;6066}6067$git_dir="$projectroot/$proj{'path'}";6068my%co= parse_commit($head);6069if(!%co) {6070next;6071}60726073my$path= esc_html(chop_str($proj{'path'},25,5));6074my$rss="$my_url?p=$proj{'path'};a=rss";6075my$html="$my_url?p=$proj{'path'};a=summary";6076print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6077}6078print<<XML;6079</outline>6080</body>6081</opml>6082XML6083}