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 10use5.008; 11use strict; 12use warnings; 13use CGI qw(:standard :escapeHTML -nosticky); 14use CGI::Util qw(unescape); 15use CGI::Carp qw(fatalsToBrowser set_message); 16use Encode; 17use Fcntl ':mode'; 18use File::Find qw(); 19use File::Basename qw(basename); 20use Time::HiRes qw(gettimeofday tv_interval); 21binmode STDOUT,':utf8'; 22 23our$t0= [ gettimeofday() ]; 24our$number_of_git_cmds=0; 25 26BEGIN{ 27 CGI->compile()if$ENV{'MOD_PERL'}; 28} 29 30our$version="++GIT_VERSION++"; 31 32our($my_url,$my_uri,$base_url,$path_info,$home_link); 33sub evaluate_uri { 34our$cgi; 35 36our$my_url=$cgi->url(); 37our$my_uri=$cgi->url(-absolute =>1); 38 39# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 40# needed and used only for URLs with nonempty PATH_INFO 41our$base_url=$my_url; 42 43# When the script is used as DirectoryIndex, the URL does not contain the name 44# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 45# have to do it ourselves. We make $path_info global because it's also used 46# later on. 47# 48# Another issue with the script being the DirectoryIndex is that the resulting 49# $my_url data is not the full script URL: this is good, because we want 50# generated links to keep implying the script name if it wasn't explicitly 51# indicated in the URL we're handling, but it means that $my_url cannot be used 52# as base URL. 53# Therefore, if we needed to strip PATH_INFO, then we know that we have 54# to build the base URL ourselves: 55our$path_info=$ENV{"PATH_INFO"}; 56if($path_info) { 57if($my_url=~ s,\Q$path_info\E$,, && 58$my_uri=~ s,\Q$path_info\E$,, && 59defined$ENV{'SCRIPT_NAME'}) { 60$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 61} 62} 63 64# target of the home link on top of all pages 65our$home_link=$my_uri||"/"; 66} 67 68# core git executable to use 69# this can just be "git" if your webserver has a sensible PATH 70our$GIT="++GIT_BINDIR++/git"; 71 72# absolute fs-path which will be prepended to the project path 73#our $projectroot = "/pub/scm"; 74our$projectroot="++GITWEB_PROJECTROOT++"; 75 76# fs traversing limit for getting project list 77# the number is relative to the projectroot 78our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 79 80# string of the home link on top of all pages 81our$home_link_str="++GITWEB_HOME_LINK_STR++"; 82 83# name of your site or organization to appear in page titles 84# replace this with something more descriptive for clearer bookmarks 85our$site_name="++GITWEB_SITENAME++" 86|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 87 88# filename of html text to include at top of each page 89our$site_header="++GITWEB_SITE_HEADER++"; 90# html text to include at home page 91our$home_text="++GITWEB_HOMETEXT++"; 92# filename of html text to include at bottom of each page 93our$site_footer="++GITWEB_SITE_FOOTER++"; 94 95# URI of stylesheets 96our@stylesheets= ("++GITWEB_CSS++"); 97# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 98our$stylesheet=undef; 99# URI of GIT logo (72x27 size) 100our$logo="++GITWEB_LOGO++"; 101# URI of GIT favicon, assumed to be image/png type 102our$favicon="++GITWEB_FAVICON++"; 103# URI of gitweb.js (JavaScript code for gitweb) 104our$javascript="++GITWEB_JS++"; 105 106# URI and label (title) of GIT logo link 107#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 108#our $logo_label = "git documentation"; 109our$logo_url="http://git-scm.com/"; 110our$logo_label="git homepage"; 111 112# source of projects list 113our$projects_list="++GITWEB_LIST++"; 114 115# the width (in characters) of the projects list "Description" column 116our$projects_list_description_width=25; 117 118# default order of projects list 119# valid values are none, project, descr, owner, and age 120our$default_projects_order="project"; 121 122# show repository only if this file exists 123# (only effective if this variable evaluates to true) 124our$export_ok="++GITWEB_EXPORT_OK++"; 125 126# show repository only if this subroutine returns true 127# when given the path to the project, for example: 128# sub { return -e "$_[0]/git-daemon-export-ok"; } 129our$export_auth_hook=undef; 130 131# only allow viewing of repositories also shown on the overview page 132our$strict_export="++GITWEB_STRICT_EXPORT++"; 133 134# list of git base URLs used for URL to where fetch project from, 135# i.e. full URL is "$git_base_url/$project" 136our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 137 138# default blob_plain mimetype and default charset for text/plain blob 139our$default_blob_plain_mimetype='text/plain'; 140our$default_text_plain_charset=undef; 141 142# file to use for guessing MIME types before trying /etc/mime.types 143# (relative to the current git repository) 144our$mimetypes_file=undef; 145 146# assume this charset if line contains non-UTF-8 characters; 147# it should be valid encoding (see Encoding::Supported(3pm) for list), 148# for which encoding all byte sequences are valid, for example 149# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 150# could be even 'utf-8' for the old behavior) 151our$fallback_encoding='latin1'; 152 153# rename detection options for git-diff and git-diff-tree 154# - default is '-M', with the cost proportional to 155# (number of removed files) * (number of new files). 156# - more costly is '-C' (which implies '-M'), with the cost proportional to 157# (number of changed files + number of removed files) * (number of new files) 158# - even more costly is '-C', '--find-copies-harder' with cost 159# (number of files in the original tree) * (number of new files) 160# - one might want to include '-B' option, e.g. '-B', '-M' 161our@diff_opts= ('-M');# taken from git_commit 162 163# Disables features that would allow repository owners to inject script into 164# the gitweb domain. 165our$prevent_xss=0; 166 167# Path to the highlight executable to use (must be the one from 168# http://www.andre-simon.de due to assumptions about parameters and output). 169# Useful if highlight is not installed on your webserver's PATH. 170# [Default: highlight] 171our$highlight_bin="++HIGHLIGHT_BIN++"; 172 173# information about snapshot formats that gitweb is capable of serving 174our%known_snapshot_formats= ( 175# name => { 176# 'display' => display name, 177# 'type' => mime type, 178# 'suffix' => filename suffix, 179# 'format' => --format for git-archive, 180# 'compressor' => [compressor command and arguments] 181# (array reference, optional) 182# 'disabled' => boolean (optional)} 183# 184'tgz'=> { 185'display'=>'tar.gz', 186'type'=>'application/x-gzip', 187'suffix'=>'.tar.gz', 188'format'=>'tar', 189'compressor'=> ['gzip','-n']}, 190 191'tbz2'=> { 192'display'=>'tar.bz2', 193'type'=>'application/x-bzip2', 194'suffix'=>'.tar.bz2', 195'format'=>'tar', 196'compressor'=> ['bzip2']}, 197 198'txz'=> { 199'display'=>'tar.xz', 200'type'=>'application/x-xz', 201'suffix'=>'.tar.xz', 202'format'=>'tar', 203'compressor'=> ['xz'], 204'disabled'=>1}, 205 206'zip'=> { 207'display'=>'zip', 208'type'=>'application/x-zip', 209'suffix'=>'.zip', 210'format'=>'zip'}, 211); 212 213# Aliases so we understand old gitweb.snapshot values in repository 214# configuration. 215our%known_snapshot_format_aliases= ( 216'gzip'=>'tgz', 217'bzip2'=>'tbz2', 218'xz'=>'txz', 219 220# backward compatibility: legacy gitweb config support 221'x-gzip'=>undef,'gz'=>undef, 222'x-bzip2'=>undef,'bz2'=>undef, 223'x-zip'=>undef,''=>undef, 224); 225 226# Pixel sizes for icons and avatars. If the default font sizes or lineheights 227# are changed, it may be appropriate to change these values too via 228# $GITWEB_CONFIG. 229our%avatar_size= ( 230'default'=>16, 231'double'=>32 232); 233 234# Used to set the maximum load that we will still respond to gitweb queries. 235# If server load exceed this value then return "503 server busy" error. 236# If gitweb cannot determined server load, it is taken to be 0. 237# Leave it undefined (or set to 'undef') to turn off load checking. 238our$maxload=300; 239 240# configuration for 'highlight' (http://www.andre-simon.de/) 241# match by basename 242our%highlight_basename= ( 243#'Program' => 'py', 244#'Library' => 'py', 245'SConstruct'=>'py',# SCons equivalent of Makefile 246'Makefile'=>'make', 247); 248# match by extension 249our%highlight_ext= ( 250# main extensions, defining name of syntax; 251# see files in /usr/share/highlight/langDefs/ directory 252map{$_=>$_} 253qw(py c cpp rb java css php sh pl js tex bib xml awk bat ini spec tcl sql make), 254# alternate extensions, see /etc/highlight/filetypes.conf 255'h'=>'c', 256map{$_=>'sh'}qw(bash zsh ksh), 257map{$_=>'cpp'}qw(cxx c++ cc), 258map{$_=>'php'}qw(php3 php4 php5 phps), 259map{$_=>'pl'}qw(perl pm),# perhaps also 'cgi' 260map{$_=>'make'}qw(mak mk), 261map{$_=>'xml'}qw(xhtml html htm), 262); 263 264# You define site-wide feature defaults here; override them with 265# $GITWEB_CONFIG as necessary. 266our%feature= ( 267# feature => { 268# 'sub' => feature-sub (subroutine), 269# 'override' => allow-override (boolean), 270# 'default' => [ default options...] (array reference)} 271# 272# if feature is overridable (it means that allow-override has true value), 273# then feature-sub will be called with default options as parameters; 274# return value of feature-sub indicates if to enable specified feature 275# 276# if there is no 'sub' key (no feature-sub), then feature cannot be 277# overridden 278# 279# use gitweb_get_feature(<feature>) to retrieve the <feature> value 280# (an array) or gitweb_check_feature(<feature>) to check if <feature> 281# is enabled 282 283# Enable the 'blame' blob view, showing the last commit that modified 284# each line in the file. This can be very CPU-intensive. 285 286# To enable system wide have in $GITWEB_CONFIG 287# $feature{'blame'}{'default'} = [1]; 288# To have project specific config enable override in $GITWEB_CONFIG 289# $feature{'blame'}{'override'} = 1; 290# and in project config gitweb.blame = 0|1; 291'blame'=> { 292'sub'=>sub{ feature_bool('blame',@_) }, 293'override'=>0, 294'default'=> [0]}, 295 296# Enable the 'snapshot' link, providing a compressed archive of any 297# tree. This can potentially generate high traffic if you have large 298# project. 299 300# Value is a list of formats defined in %known_snapshot_formats that 301# you wish to offer. 302# To disable system wide have in $GITWEB_CONFIG 303# $feature{'snapshot'}{'default'} = []; 304# To have project specific config enable override in $GITWEB_CONFIG 305# $feature{'snapshot'}{'override'} = 1; 306# and in project config, a comma-separated list of formats or "none" 307# to disable. Example: gitweb.snapshot = tbz2,zip; 308'snapshot'=> { 309'sub'=> \&feature_snapshot, 310'override'=>0, 311'default'=> ['tgz']}, 312 313# Enable text search, which will list the commits which match author, 314# committer or commit text to a given string. Enabled by default. 315# Project specific override is not supported. 316# 317# Note that this controls all search features, which means that if 318# it is disabled, then 'grep' and 'pickaxe' search would also be 319# disabled. 320'search'=> { 321'override'=>0, 322'default'=> [1]}, 323 324# Enable grep search, which will list the files in currently selected 325# tree containing the given string. Enabled by default. This can be 326# potentially CPU-intensive, of course. 327# Note that you need to have 'search' feature enabled too. 328 329# To enable system wide have in $GITWEB_CONFIG 330# $feature{'grep'}{'default'} = [1]; 331# To have project specific config enable override in $GITWEB_CONFIG 332# $feature{'grep'}{'override'} = 1; 333# and in project config gitweb.grep = 0|1; 334'grep'=> { 335'sub'=>sub{ feature_bool('grep',@_) }, 336'override'=>0, 337'default'=> [1]}, 338 339# Enable the pickaxe search, which will list the commits that modified 340# a given string in a file. This can be practical and quite faster 341# alternative to 'blame', but still potentially CPU-intensive. 342# Note that you need to have 'search' feature enabled too. 343 344# To enable system wide have in $GITWEB_CONFIG 345# $feature{'pickaxe'}{'default'} = [1]; 346# To have project specific config enable override in $GITWEB_CONFIG 347# $feature{'pickaxe'}{'override'} = 1; 348# and in project config gitweb.pickaxe = 0|1; 349'pickaxe'=> { 350'sub'=>sub{ feature_bool('pickaxe',@_) }, 351'override'=>0, 352'default'=> [1]}, 353 354# Enable showing size of blobs in a 'tree' view, in a separate 355# column, similar to what 'ls -l' does. This cost a bit of IO. 356 357# To disable system wide have in $GITWEB_CONFIG 358# $feature{'show-sizes'}{'default'} = [0]; 359# To have project specific config enable override in $GITWEB_CONFIG 360# $feature{'show-sizes'}{'override'} = 1; 361# and in project config gitweb.showsizes = 0|1; 362'show-sizes'=> { 363'sub'=>sub{ feature_bool('showsizes',@_) }, 364'override'=>0, 365'default'=> [1]}, 366 367# Make gitweb use an alternative format of the URLs which can be 368# more readable and natural-looking: project name is embedded 369# directly in the path and the query string contains other 370# auxiliary information. All gitweb installations recognize 371# URL in either format; this configures in which formats gitweb 372# generates links. 373 374# To enable system wide have in $GITWEB_CONFIG 375# $feature{'pathinfo'}{'default'} = [1]; 376# Project specific override is not supported. 377 378# Note that you will need to change the default location of CSS, 379# favicon, logo and possibly other files to an absolute URL. Also, 380# if gitweb.cgi serves as your indexfile, you will need to force 381# $my_uri to contain the script name in your $GITWEB_CONFIG. 382'pathinfo'=> { 383'override'=>0, 384'default'=> [0]}, 385 386# Make gitweb consider projects in project root subdirectories 387# to be forks of existing projects. Given project $projname.git, 388# projects matching $projname/*.git will not be shown in the main 389# projects list, instead a '+' mark will be added to $projname 390# there and a 'forks' view will be enabled for the project, listing 391# all the forks. If project list is taken from a file, forks have 392# to be listed after the main project. 393 394# To enable system wide have in $GITWEB_CONFIG 395# $feature{'forks'}{'default'} = [1]; 396# Project specific override is not supported. 397'forks'=> { 398'override'=>0, 399'default'=> [0]}, 400 401# Insert custom links to the action bar of all project pages. 402# This enables you mainly to link to third-party scripts integrating 403# into gitweb; e.g. git-browser for graphical history representation 404# or custom web-based repository administration interface. 405 406# The 'default' value consists of a list of triplets in the form 407# (label, link, position) where position is the label after which 408# to insert the link and link is a format string where %n expands 409# to the project name, %f to the project path within the filesystem, 410# %h to the current hash (h gitweb parameter) and %b to the current 411# hash base (hb gitweb parameter); %% expands to %. 412 413# To enable system wide have in $GITWEB_CONFIG e.g. 414# $feature{'actions'}{'default'} = [('graphiclog', 415# '/git-browser/by-commit.html?r=%n', 'summary')]; 416# Project specific override is not supported. 417'actions'=> { 418'override'=>0, 419'default'=> []}, 420 421# Allow gitweb scan project content tags described in ctags/ 422# of project repository, and display the popular Web 2.0-ish 423# "tag cloud" near the project list. Note that this is something 424# COMPLETELY different from the normal Git tags. 425 426# gitweb by itself can show existing tags, but it does not handle 427# tagging itself; you need an external application for that. 428# For an example script, check Girocco's cgi/tagproj.cgi. 429# You may want to install the HTML::TagCloud Perl module to get 430# a pretty tag cloud instead of just a list of tags. 431 432# To enable system wide have in $GITWEB_CONFIG 433# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 434# Project specific override is not supported. 435'ctags'=> { 436'override'=>0, 437'default'=> [0]}, 438 439# The maximum number of patches in a patchset generated in patch 440# view. Set this to 0 or undef to disable patch view, or to a 441# negative number to remove any limit. 442 443# To disable system wide have in $GITWEB_CONFIG 444# $feature{'patches'}{'default'} = [0]; 445# To have project specific config enable override in $GITWEB_CONFIG 446# $feature{'patches'}{'override'} = 1; 447# and in project config gitweb.patches = 0|n; 448# where n is the maximum number of patches allowed in a patchset. 449'patches'=> { 450'sub'=> \&feature_patches, 451'override'=>0, 452'default'=> [16]}, 453 454# Avatar support. When this feature is enabled, views such as 455# shortlog or commit will display an avatar associated with 456# the email of the committer(s) and/or author(s). 457 458# Currently available providers are gravatar and picon. 459# If an unknown provider is specified, the feature is disabled. 460 461# Gravatar depends on Digest::MD5. 462# Picon currently relies on the indiana.edu database. 463 464# To enable system wide have in $GITWEB_CONFIG 465# $feature{'avatar'}{'default'} = ['<provider>']; 466# where <provider> is either gravatar or picon. 467# To have project specific config enable override in $GITWEB_CONFIG 468# $feature{'avatar'}{'override'} = 1; 469# and in project config gitweb.avatar = <provider>; 470'avatar'=> { 471'sub'=> \&feature_avatar, 472'override'=>0, 473'default'=> ['']}, 474 475# Enable displaying how much time and how many git commands 476# it took to generate and display page. Disabled by default. 477# Project specific override is not supported. 478'timed'=> { 479'override'=>0, 480'default'=> [0]}, 481 482# Enable turning some links into links to actions which require 483# JavaScript to run (like 'blame_incremental'). Not enabled by 484# default. Project specific override is currently not supported. 485'javascript-actions'=> { 486'override'=>0, 487'default'=> [0]}, 488 489# Syntax highlighting support. This is based on Daniel Svensson's 490# and Sham Chukoury's work in gitweb-xmms2.git. 491# It requires the 'highlight' program present in $PATH, 492# and therefore is disabled by default. 493 494# To enable system wide have in $GITWEB_CONFIG 495# $feature{'highlight'}{'default'} = [1]; 496 497'highlight'=> { 498'sub'=>sub{ feature_bool('highlight',@_) }, 499'override'=>0, 500'default'=> [0]}, 501 502# Enable displaying of remote heads in the heads list 503 504# To enable system wide have in $GITWEB_CONFIG 505# $feature{'remote_heads'}{'default'} = [1]; 506# To have project specific config enable override in $GITWEB_CONFIG 507# $feature{'remote_heads'}{'override'} = 1; 508# and in project config gitweb.remote_heads = 0|1; 509'remote_heads'=> { 510'sub'=>sub{ feature_bool('remote_heads',@_) }, 511'override'=>0, 512'default'=> [0]}, 513); 514 515sub gitweb_get_feature { 516my($name) =@_; 517return unlessexists$feature{$name}; 518my($sub,$override,@defaults) = ( 519$feature{$name}{'sub'}, 520$feature{$name}{'override'}, 521@{$feature{$name}{'default'}}); 522# project specific override is possible only if we have project 523our$git_dir;# global variable, declared later 524if(!$override|| !defined$git_dir) { 525return@defaults; 526} 527if(!defined$sub) { 528warn"feature$nameis not overridable"; 529return@defaults; 530} 531return$sub->(@defaults); 532} 533 534# A wrapper to check if a given feature is enabled. 535# With this, you can say 536# 537# my $bool_feat = gitweb_check_feature('bool_feat'); 538# gitweb_check_feature('bool_feat') or somecode; 539# 540# instead of 541# 542# my ($bool_feat) = gitweb_get_feature('bool_feat'); 543# (gitweb_get_feature('bool_feat'))[0] or somecode; 544# 545sub gitweb_check_feature { 546return(gitweb_get_feature(@_))[0]; 547} 548 549 550sub feature_bool { 551my$key=shift; 552my($val) = git_get_project_config($key,'--bool'); 553 554if(!defined$val) { 555return($_[0]); 556}elsif($valeq'true') { 557return(1); 558}elsif($valeq'false') { 559return(0); 560} 561} 562 563sub feature_snapshot { 564my(@fmts) =@_; 565 566my($val) = git_get_project_config('snapshot'); 567 568if($val) { 569@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 570} 571 572return@fmts; 573} 574 575sub feature_patches { 576my@val= (git_get_project_config('patches','--int')); 577 578if(@val) { 579return@val; 580} 581 582return($_[0]); 583} 584 585sub feature_avatar { 586my@val= (git_get_project_config('avatar')); 587 588return@val?@val:@_; 589} 590 591# checking HEAD file with -e is fragile if the repository was 592# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 593# and then pruned. 594sub check_head_link { 595my($dir) =@_; 596my$headfile="$dir/HEAD"; 597return((-e $headfile) || 598(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 599} 600 601sub check_export_ok { 602my($dir) =@_; 603return(check_head_link($dir) && 604(!$export_ok|| -e "$dir/$export_ok") && 605(!$export_auth_hook||$export_auth_hook->($dir))); 606} 607 608# process alternate names for backward compatibility 609# filter out unsupported (unknown) snapshot formats 610sub filter_snapshot_fmts { 611my@fmts=@_; 612 613@fmts=map{ 614exists$known_snapshot_format_aliases{$_} ? 615$known_snapshot_format_aliases{$_} :$_}@fmts; 616@fmts=grep{ 617exists$known_snapshot_formats{$_} && 618!$known_snapshot_formats{$_}{'disabled'}}@fmts; 619} 620 621# If it is set to code reference, it is code that it is to be run once per 622# request, allowing updating configurations that change with each request, 623# while running other code in config file only once. 624# 625# Otherwise, if it is false then gitweb would process config file only once; 626# if it is true then gitweb config would be run for each request. 627our$per_request_config=1; 628 629our($GITWEB_CONFIG,$GITWEB_CONFIG_SYSTEM); 630sub evaluate_gitweb_config { 631our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 632our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 633# die if there are errors parsing config file 634if(-e $GITWEB_CONFIG) { 635do$GITWEB_CONFIG; 636die$@if$@; 637}elsif(-e $GITWEB_CONFIG_SYSTEM) { 638do$GITWEB_CONFIG_SYSTEM; 639die$@if$@; 640} 641} 642 643# Get loadavg of system, to compare against $maxload. 644# Currently it requires '/proc/loadavg' present to get loadavg; 645# if it is not present it returns 0, which means no load checking. 646sub get_loadavg { 647if( -e '/proc/loadavg'){ 648open my$fd,'<','/proc/loadavg' 649orreturn0; 650my@load=split(/\s+/,scalar<$fd>); 651close$fd; 652 653# The first three columns measure CPU and IO utilization of the last one, 654# five, and 10 minute periods. The fourth column shows the number of 655# currently running processes and the total number of processes in the m/n 656# format. The last column displays the last process ID used. 657return$load[0] ||0; 658} 659# additional checks for load average should go here for things that don't export 660# /proc/loadavg 661 662return0; 663} 664 665# version of the core git binary 666our$git_version; 667sub evaluate_git_version { 668our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 669$number_of_git_cmds++; 670} 671 672sub check_loadavg { 673if(defined$maxload&& get_loadavg() >$maxload) { 674 die_error(503,"The load average on the server is too high"); 675} 676} 677 678# ====================================================================== 679# input validation and dispatch 680 681# input parameters can be collected from a variety of sources (presently, CGI 682# and PATH_INFO), so we define an %input_params hash that collects them all 683# together during validation: this allows subsequent uses (e.g. href()) to be 684# agnostic of the parameter origin 685 686our%input_params= (); 687 688# input parameters are stored with the long parameter name as key. This will 689# also be used in the href subroutine to convert parameters to their CGI 690# equivalent, and since the href() usage is the most frequent one, we store 691# the name -> CGI key mapping here, instead of the reverse. 692# 693# XXX: Warning: If you touch this, check the search form for updating, 694# too. 695 696our@cgi_param_mapping= ( 697 project =>"p", 698 action =>"a", 699 file_name =>"f", 700 file_parent =>"fp", 701 hash =>"h", 702 hash_parent =>"hp", 703 hash_base =>"hb", 704 hash_parent_base =>"hpb", 705 page =>"pg", 706 order =>"o", 707 searchtext =>"s", 708 searchtype =>"st", 709 snapshot_format =>"sf", 710 extra_options =>"opt", 711 search_use_regexp =>"sr", 712# this must be last entry (for manipulation from JavaScript) 713 javascript =>"js" 714); 715our%cgi_param_mapping=@cgi_param_mapping; 716 717# we will also need to know the possible actions, for validation 718our%actions= ( 719"blame"=> \&git_blame, 720"blame_incremental"=> \&git_blame_incremental, 721"blame_data"=> \&git_blame_data, 722"blobdiff"=> \&git_blobdiff, 723"blobdiff_plain"=> \&git_blobdiff_plain, 724"blob"=> \&git_blob, 725"blob_plain"=> \&git_blob_plain, 726"commitdiff"=> \&git_commitdiff, 727"commitdiff_plain"=> \&git_commitdiff_plain, 728"commit"=> \&git_commit, 729"forks"=> \&git_forks, 730"heads"=> \&git_heads, 731"history"=> \&git_history, 732"log"=> \&git_log, 733"patch"=> \&git_patch, 734"patches"=> \&git_patches, 735"remotes"=> \&git_remotes, 736"rss"=> \&git_rss, 737"atom"=> \&git_atom, 738"search"=> \&git_search, 739"search_help"=> \&git_search_help, 740"shortlog"=> \&git_shortlog, 741"summary"=> \&git_summary, 742"tag"=> \&git_tag, 743"tags"=> \&git_tags, 744"tree"=> \&git_tree, 745"snapshot"=> \&git_snapshot, 746"object"=> \&git_object, 747# those below don't need $project 748"opml"=> \&git_opml, 749"project_list"=> \&git_project_list, 750"project_index"=> \&git_project_index, 751); 752 753# finally, we have the hash of allowed extra_options for the commands that 754# allow them 755our%allowed_options= ( 756"--no-merges"=> [qw(rss atom log shortlog history)], 757); 758 759# fill %input_params with the CGI parameters. All values except for 'opt' 760# should be single values, but opt can be an array. We should probably 761# build an array of parameters that can be multi-valued, but since for the time 762# being it's only this one, we just single it out 763sub evaluate_query_params { 764our$cgi; 765 766while(my($name,$symbol) =each%cgi_param_mapping) { 767if($symboleq'opt') { 768$input_params{$name} = [$cgi->param($symbol) ]; 769}else{ 770$input_params{$name} =$cgi->param($symbol); 771} 772} 773} 774 775# now read PATH_INFO and update the parameter list for missing parameters 776sub evaluate_path_info { 777return ifdefined$input_params{'project'}; 778return if!$path_info; 779$path_info=~ s,^/+,,; 780return if!$path_info; 781 782# find which part of PATH_INFO is project 783my$project=$path_info; 784$project=~ s,/+$,,; 785while($project&& !check_head_link("$projectroot/$project")) { 786$project=~ s,/*[^/]*$,,; 787} 788return unless$project; 789$input_params{'project'} =$project; 790 791# do not change any parameters if an action is given using the query string 792return if$input_params{'action'}; 793$path_info=~ s,^\Q$project\E/*,,; 794 795# next, check if we have an action 796my$action=$path_info; 797$action=~ s,/.*$,,; 798if(exists$actions{$action}) { 799$path_info=~ s,^$action/*,,; 800$input_params{'action'} =$action; 801} 802 803# list of actions that want hash_base instead of hash, but can have no 804# pathname (f) parameter 805my@wants_base= ( 806'tree', 807'history', 808); 809 810# we want to catch, among others 811# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 812my($parentrefname,$parentpathname,$refname,$pathname) = 813($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?([^:]+?)?(?::(.+))?$/); 814 815# first, analyze the 'current' part 816if(defined$pathname) { 817# we got "branch:filename" or "branch:dir/" 818# we could use git_get_type(branch:pathname), but: 819# - it needs $git_dir 820# - it does a git() call 821# - the convention of terminating directories with a slash 822# makes it superfluous 823# - embedding the action in the PATH_INFO would make it even 824# more superfluous 825$pathname=~ s,^/+,,; 826if(!$pathname||substr($pathname, -1)eq"/") { 827$input_params{'action'} ||="tree"; 828$pathname=~ s,/$,,; 829}else{ 830# the default action depends on whether we had parent info 831# or not 832if($parentrefname) { 833$input_params{'action'} ||="blobdiff_plain"; 834}else{ 835$input_params{'action'} ||="blob_plain"; 836} 837} 838$input_params{'hash_base'} ||=$refname; 839$input_params{'file_name'} ||=$pathname; 840}elsif(defined$refname) { 841# we got "branch". In this case we have to choose if we have to 842# set hash or hash_base. 843# 844# Most of the actions without a pathname only want hash to be 845# set, except for the ones specified in @wants_base that want 846# hash_base instead. It should also be noted that hand-crafted 847# links having 'history' as an action and no pathname or hash 848# set will fail, but that happens regardless of PATH_INFO. 849if(defined$parentrefname) { 850# if there is parent let the default be 'shortlog' action 851# (for http://git.example.com/repo.git/A..B links); if there 852# is no parent, dispatch will detect type of object and set 853# action appropriately if required (if action is not set) 854$input_params{'action'} ||="shortlog"; 855} 856if($input_params{'action'} && 857grep{$_eq$input_params{'action'} }@wants_base) { 858$input_params{'hash_base'} ||=$refname; 859}else{ 860$input_params{'hash'} ||=$refname; 861} 862} 863 864# next, handle the 'parent' part, if present 865if(defined$parentrefname) { 866# a missing pathspec defaults to the 'current' filename, allowing e.g. 867# someproject/blobdiff/oldrev..newrev:/filename 868if($parentpathname) { 869$parentpathname=~ s,^/+,,; 870$parentpathname=~ s,/$,,; 871$input_params{'file_parent'} ||=$parentpathname; 872}else{ 873$input_params{'file_parent'} ||=$input_params{'file_name'}; 874} 875# we assume that hash_parent_base is wanted if a path was specified, 876# or if the action wants hash_base instead of hash 877if(defined$input_params{'file_parent'} || 878grep{$_eq$input_params{'action'} }@wants_base) { 879$input_params{'hash_parent_base'} ||=$parentrefname; 880}else{ 881$input_params{'hash_parent'} ||=$parentrefname; 882} 883} 884 885# for the snapshot action, we allow URLs in the form 886# $project/snapshot/$hash.ext 887# where .ext determines the snapshot and gets removed from the 888# passed $refname to provide the $hash. 889# 890# To be able to tell that $refname includes the format extension, we 891# require the following two conditions to be satisfied: 892# - the hash input parameter MUST have been set from the $refname part 893# of the URL (i.e. they must be equal) 894# - the snapshot format MUST NOT have been defined already (e.g. from 895# CGI parameter sf) 896# It's also useless to try any matching unless $refname has a dot, 897# so we check for that too 898if(defined$input_params{'action'} && 899$input_params{'action'}eq'snapshot'&& 900defined$refname&&index($refname,'.') != -1&& 901$refnameeq$input_params{'hash'} && 902!defined$input_params{'snapshot_format'}) { 903# We loop over the known snapshot formats, checking for 904# extensions. Allowed extensions are both the defined suffix 905# (which includes the initial dot already) and the snapshot 906# format key itself, with a prepended dot 907while(my($fmt,$opt) =each%known_snapshot_formats) { 908my$hash=$refname; 909unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 910next; 911} 912my$sfx=$1; 913# a valid suffix was found, so set the snapshot format 914# and reset the hash parameter 915$input_params{'snapshot_format'} =$fmt; 916$input_params{'hash'} =$hash; 917# we also set the format suffix to the one requested 918# in the URL: this way a request for e.g. .tgz returns 919# a .tgz instead of a .tar.gz 920$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 921last; 922} 923} 924} 925 926our($action,$project,$file_name,$file_parent,$hash,$hash_parent,$hash_base, 927$hash_parent_base,@extra_options,$page,$searchtype,$search_use_regexp, 928$searchtext,$search_regexp); 929sub evaluate_and_validate_params { 930our$action=$input_params{'action'}; 931if(defined$action) { 932if(!validate_action($action)) { 933 die_error(400,"Invalid action parameter"); 934} 935} 936 937# parameters which are pathnames 938our$project=$input_params{'project'}; 939if(defined$project) { 940if(!validate_project($project)) { 941undef$project; 942 die_error(404,"No such project"); 943} 944} 945 946our$file_name=$input_params{'file_name'}; 947if(defined$file_name) { 948if(!validate_pathname($file_name)) { 949 die_error(400,"Invalid file parameter"); 950} 951} 952 953our$file_parent=$input_params{'file_parent'}; 954if(defined$file_parent) { 955if(!validate_pathname($file_parent)) { 956 die_error(400,"Invalid file parent parameter"); 957} 958} 959 960# parameters which are refnames 961our$hash=$input_params{'hash'}; 962if(defined$hash) { 963if(!validate_refname($hash)) { 964 die_error(400,"Invalid hash parameter"); 965} 966} 967 968our$hash_parent=$input_params{'hash_parent'}; 969if(defined$hash_parent) { 970if(!validate_refname($hash_parent)) { 971 die_error(400,"Invalid hash parent parameter"); 972} 973} 974 975our$hash_base=$input_params{'hash_base'}; 976if(defined$hash_base) { 977if(!validate_refname($hash_base)) { 978 die_error(400,"Invalid hash base parameter"); 979} 980} 981 982our@extra_options= @{$input_params{'extra_options'}}; 983# @extra_options is always defined, since it can only be (currently) set from 984# CGI, and $cgi->param() returns the empty array in array context if the param 985# is not set 986foreachmy$opt(@extra_options) { 987if(not exists$allowed_options{$opt}) { 988 die_error(400,"Invalid option parameter"); 989} 990if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 991 die_error(400,"Invalid option parameter for this action"); 992} 993} 994 995our$hash_parent_base=$input_params{'hash_parent_base'}; 996if(defined$hash_parent_base) { 997if(!validate_refname($hash_parent_base)) { 998 die_error(400,"Invalid hash parent base parameter"); 999}1000}10011002# other parameters1003our$page=$input_params{'page'};1004if(defined$page) {1005if($page=~m/[^0-9]/) {1006 die_error(400,"Invalid page parameter");1007}1008}10091010our$searchtype=$input_params{'searchtype'};1011if(defined$searchtype) {1012if($searchtype=~m/[^a-z]/) {1013 die_error(400,"Invalid searchtype parameter");1014}1015}10161017our$search_use_regexp=$input_params{'search_use_regexp'};10181019our$searchtext=$input_params{'searchtext'};1020our$search_regexp;1021if(defined$searchtext) {1022if(length($searchtext) <2) {1023 die_error(403,"At least two characters are required for search parameter");1024}1025$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext;1026}1027}10281029# path to the current git repository1030our$git_dir;1031sub evaluate_git_dir {1032our$git_dir="$projectroot/$project"if$project;1033}10341035our(@snapshot_fmts,$git_avatar);1036sub configure_gitweb_features {1037# list of supported snapshot formats1038our@snapshot_fmts= gitweb_get_feature('snapshot');1039@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts);10401041# check that the avatar feature is set to a known provider name,1042# and for each provider check if the dependencies are satisfied.1043# if the provider name is invalid or the dependencies are not met,1044# reset $git_avatar to the empty string.1045our($git_avatar) = gitweb_get_feature('avatar');1046if($git_avatareq'gravatar') {1047$git_avatar=''unless(eval{require Digest::MD5;1; });1048}elsif($git_avatareq'picon') {1049# no dependencies1050}else{1051$git_avatar='';1052}1053}10541055# custom error handler: 'die <message>' is Internal Server Error1056sub handle_errors_html {1057my$msg=shift;# it is already HTML escaped10581059# to avoid infinite loop where error occurs in die_error,1060# change handler to default handler, disabling handle_errors_html1061 set_message("Error occured when inside die_error:\n$msg");10621063# you cannot jump out of die_error when called as error handler;1064# the subroutine set via CGI::Carp::set_message is called _after_1065# HTTP headers are already written, so it cannot write them itself1066 die_error(undef,undef,$msg, -error_handler =>1, -no_http_header =>1);1067}1068set_message(\&handle_errors_html);10691070# dispatch1071sub dispatch {1072if(!defined$action) {1073if(defined$hash) {1074$action= git_get_type($hash);1075}elsif(defined$hash_base&&defined$file_name) {1076$action= git_get_type("$hash_base:$file_name");1077}elsif(defined$project) {1078$action='summary';1079}else{1080$action='project_list';1081}1082}1083if(!defined($actions{$action})) {1084 die_error(400,"Unknown action");1085}1086if($action!~m/^(?:opml|project_list|project_index)$/&&1087!$project) {1088 die_error(400,"Project needed");1089}1090$actions{$action}->();1091}10921093sub reset_timer {1094our$t0= [ gettimeofday() ]1095ifdefined$t0;1096our$number_of_git_cmds=0;1097}10981099our$first_request=1;1100sub run_request {1101 reset_timer();11021103 evaluate_uri();1104if($first_request) {1105 evaluate_gitweb_config();1106 evaluate_git_version();1107}1108if($per_request_config) {1109if(ref($per_request_config)eq'CODE') {1110$per_request_config->();1111}elsif(!$first_request) {1112 evaluate_gitweb_config();1113}1114}1115 check_loadavg();11161117# $projectroot and $projects_list might be set in gitweb config file1118$projects_list||=$projectroot;11191120 evaluate_query_params();1121 evaluate_path_info();1122 evaluate_and_validate_params();1123 evaluate_git_dir();11241125 configure_gitweb_features();11261127 dispatch();1128}11291130our$is_last_request=sub{1};1131our($pre_dispatch_hook,$post_dispatch_hook,$pre_listen_hook);1132our$CGI='CGI';1133our$cgi;1134sub configure_as_fcgi {1135require CGI::Fast;1136our$CGI='CGI::Fast';11371138my$request_number=0;1139# let each child service 100 requests1140our$is_last_request=sub{ ++$request_number>100};1141}1142sub evaluate_argv {1143my$script_name=$ENV{'SCRIPT_NAME'} ||$ENV{'SCRIPT_FILENAME'} || __FILE__;1144 configure_as_fcgi()1145if$script_name=~/\.fcgi$/;11461147return unless(@ARGV);11481149require Getopt::Long;1150 Getopt::Long::GetOptions(1151'fastcgi|fcgi|f'=> \&configure_as_fcgi,1152'nproc|n=i'=>sub{1153my($arg,$val) =@_;1154return unlesseval{require FCGI::ProcManager;1; };1155my$proc_manager= FCGI::ProcManager->new({1156 n_processes =>$val,1157});1158our$pre_listen_hook=sub{$proc_manager->pm_manage() };1159our$pre_dispatch_hook=sub{$proc_manager->pm_pre_dispatch() };1160our$post_dispatch_hook=sub{$proc_manager->pm_post_dispatch() };1161},1162);1163}11641165sub run {1166 evaluate_argv();11671168$first_request=1;1169$pre_listen_hook->()1170if$pre_listen_hook;11711172 REQUEST:1173while($cgi=$CGI->new()) {1174$pre_dispatch_hook->()1175if$pre_dispatch_hook;11761177 run_request();11781179$post_dispatch_hook->()1180if$post_dispatch_hook;1181$first_request=0;11821183last REQUEST if($is_last_request->());1184}11851186 DONE_GITWEB:11871;1188}11891190run();11911192if(defined caller) {1193# wrapped in a subroutine processing requests,1194# e.g. mod_perl with ModPerl::Registry, or PSGI with Plack::App::WrapCGI1195return;1196}else{1197# pure CGI script, serving single request1198exit;1199}12001201## ======================================================================1202## action links12031204# possible values of extra options1205# -full => 0|1 - use absolute/full URL ($my_uri/$my_url as base)1206# -replay => 1 - start from a current view (replay with modifications)1207# -path_info => 0|1 - don't use/use path_info URL (if possible)1208# -anchor => ANCHOR - add #ANCHOR to end of URL, implies -replay if used alone1209sub href {1210my%params=@_;1211# default is to use -absolute url() i.e. $my_uri1212my$href=$params{-full} ?$my_url:$my_uri;12131214# implicit -replay, must be first of implicit params1215$params{-replay} =1if(keys%params==1&&$params{-anchor});12161217$params{'project'} =$projectunlessexists$params{'project'};12181219if($params{-replay}) {1220while(my($name,$symbol) =each%cgi_param_mapping) {1221if(!exists$params{$name}) {1222$params{$name} =$input_params{$name};1223}1224}1225}12261227my$use_pathinfo= gitweb_check_feature('pathinfo');1228if(defined$params{'project'} &&1229(exists$params{-path_info} ?$params{-path_info} :$use_pathinfo)) {1230# try to put as many parameters as possible in PATH_INFO:1231# - project name1232# - action1233# - hash_parent or hash_parent_base:/file_parent1234# - hash or hash_base:/filename1235# - the snapshot_format as an appropriate suffix12361237# When the script is the root DirectoryIndex for the domain,1238# $href here would be something like http://gitweb.example.com/1239# Thus, we strip any trailing / from $href, to spare us double1240# slashes in the final URL1241$href=~ s,/$,,;12421243# Then add the project name, if present1244$href.="/".esc_path_info($params{'project'});1245delete$params{'project'};12461247# since we destructively absorb parameters, we keep this1248# boolean that remembers if we're handling a snapshot1249my$is_snapshot=$params{'action'}eq'snapshot';12501251# Summary just uses the project path URL, any other action is1252# added to the URL1253if(defined$params{'action'}) {1254$href.="/".esc_path_info($params{'action'})1255unless$params{'action'}eq'summary';1256delete$params{'action'};1257}12581259# Next, we put hash_parent_base:/file_parent..hash_base:/file_name,1260# stripping nonexistent or useless pieces1261$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'}1262||$params{'hash_parent'} ||$params{'hash'});1263if(defined$params{'hash_base'}) {1264if(defined$params{'hash_parent_base'}) {1265$href.= esc_path_info($params{'hash_parent_base'});1266# skip the file_parent if it's the same as the file_name1267if(defined$params{'file_parent'}) {1268if(defined$params{'file_name'} &&$params{'file_parent'}eq$params{'file_name'}) {1269delete$params{'file_parent'};1270}elsif($params{'file_parent'} !~/\.\./) {1271$href.=":/".esc_path_info($params{'file_parent'});1272delete$params{'file_parent'};1273}1274}1275$href.="..";1276delete$params{'hash_parent'};1277delete$params{'hash_parent_base'};1278}elsif(defined$params{'hash_parent'}) {1279$href.= esc_path_info($params{'hash_parent'})."..";1280delete$params{'hash_parent'};1281}12821283$href.= esc_path_info($params{'hash_base'});1284if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) {1285$href.=":/".esc_path_info($params{'file_name'});1286delete$params{'file_name'};1287}1288delete$params{'hash'};1289delete$params{'hash_base'};1290}elsif(defined$params{'hash'}) {1291$href.= esc_path_info($params{'hash'});1292delete$params{'hash'};1293}12941295# If the action was a snapshot, we can absorb the1296# snapshot_format parameter too1297if($is_snapshot) {1298my$fmt=$params{'snapshot_format'};1299# snapshot_format should always be defined when href()1300# is called, but just in case some code forgets, we1301# fall back to the default1302$fmt||=$snapshot_fmts[0];1303$href.=$known_snapshot_formats{$fmt}{'suffix'};1304delete$params{'snapshot_format'};1305}1306}13071308# now encode the parameters explicitly1309my@result= ();1310for(my$i=0;$i<@cgi_param_mapping;$i+=2) {1311my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]);1312if(defined$params{$name}) {1313if(ref($params{$name})eq"ARRAY") {1314foreachmy$par(@{$params{$name}}) {1315push@result,$symbol."=". esc_param($par);1316}1317}else{1318push@result,$symbol."=". esc_param($params{$name});1319}1320}1321}1322$href.="?".join(';',@result)ifscalar@result;13231324# final transformation: trailing spaces must be escaped (URI-encoded)1325$href=~s/(\s+)$/CGI::escape($1)/e;13261327if($params{-anchor}) {1328$href.="#".esc_param($params{-anchor});1329}13301331return$href;1332}133313341335## ======================================================================1336## validation, quoting/unquoting and escaping13371338sub validate_action {1339my$input=shift||returnundef;1340returnundefunlessexists$actions{$input};1341return$input;1342}13431344sub validate_project {1345my$input=shift||returnundef;1346if(!validate_pathname($input) ||1347!(-d "$projectroot/$input") ||1348!check_export_ok("$projectroot/$input") ||1349($strict_export&& !project_in_list($input))) {1350returnundef;1351}else{1352return$input;1353}1354}13551356sub validate_pathname {1357my$input=shift||returnundef;13581359# no '.' or '..' as elements of path, i.e. no '.' nor '..'1360# at the beginning, at the end, and between slashes.1361# also this catches doubled slashes1362if($input=~m!(^|/)(|\.|\.\.)(/|$)!) {1363returnundef;1364}1365# no null characters1366if($input=~m!\0!) {1367returnundef;1368}1369return$input;1370}13711372sub validate_refname {1373my$input=shift||returnundef;13741375# textual hashes are O.K.1376if($input=~m/^[0-9a-fA-F]{40}$/) {1377return$input;1378}1379# it must be correct pathname1380$input= validate_pathname($input)1381orreturnundef;1382# restrictions on ref name according to git-check-ref-format1383if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1384returnundef;1385}1386return$input;1387}13881389# decode sequences of octets in utf8 into Perl's internal form,1390# which is utf-8 with utf8 flag set if needed. gitweb writes out1391# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1392sub to_utf8 {1393my$str=shift;1394returnundefunlessdefined$str;1395if(utf8::valid($str)) {1396 utf8::decode($str);1397return$str;1398}else{1399return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1400}1401}14021403# quote unsafe chars, but keep the slash, even when it's not1404# correct, but quoted slashes look too horrible in bookmarks1405sub esc_param {1406my$str=shift;1407returnundefunlessdefined$str;1408$str=~s/([^A-Za-z0-9\-_.~()\/:@ ]+)/CGI::escape($1)/eg;1409$str=~s/ /\+/g;1410return$str;1411}14121413# the quoting rules for path_info fragment are slightly different1414sub esc_path_info {1415my$str=shift;1416returnundefunlessdefined$str;14171418# path_info doesn't treat '+' as space (specially), but '?' must be escaped1419$str=~s/([^A-Za-z0-9\-_.~();\/;:@&= +]+)/CGI::escape($1)/eg;14201421return$str;1422}14231424# quote unsafe chars in whole URL, so some characters cannot be quoted1425sub esc_url {1426my$str=shift;1427returnundefunlessdefined$str;1428$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&= ]+)/CGI::escape($1)/eg;1429$str=~s/ /\+/g;1430return$str;1431}14321433# quote unsafe characters in HTML attributes1434sub esc_attr {14351436# for XHTML conformance escaping '"' to '"' is not enough1437return esc_html(@_);1438}14391440# replace invalid utf8 character with SUBSTITUTION sequence1441sub esc_html {1442my$str=shift;1443my%opts=@_;14441445returnundefunlessdefined$str;14461447$str= to_utf8($str);1448$str=$cgi->escapeHTML($str);1449if($opts{'-nbsp'}) {1450$str=~s/ / /g;1451}1452$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1453return$str;1454}14551456# quote control characters and escape filename to HTML1457sub esc_path {1458my$str=shift;1459my%opts=@_;14601461returnundefunlessdefined$str;14621463$str= to_utf8($str);1464$str=$cgi->escapeHTML($str);1465if($opts{'-nbsp'}) {1466$str=~s/ / /g;1467}1468$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1469return$str;1470}14711472# Make control characters "printable", using character escape codes (CEC)1473sub quot_cec {1474my$cntrl=shift;1475my%opts=@_;1476my%es= (# character escape codes, aka escape sequences1477"\t"=>'\t',# tab (HT)1478"\n"=>'\n',# line feed (LF)1479"\r"=>'\r',# carrige return (CR)1480"\f"=>'\f',# form feed (FF)1481"\b"=>'\b',# backspace (BS)1482"\a"=>'\a',# alarm (bell) (BEL)1483"\e"=>'\e',# escape (ESC)1484"\013"=>'\v',# vertical tab (VT)1485"\000"=>'\0',# nul character (NUL)1486);1487my$chr= ( (exists$es{$cntrl})1488?$es{$cntrl}1489:sprintf('\%2x',ord($cntrl)) );1490if($opts{-nohtml}) {1491return$chr;1492}else{1493return"<span class=\"cntrl\">$chr</span>";1494}1495}14961497# Alternatively use unicode control pictures codepoints,1498# Unicode "printable representation" (PR)1499sub quot_upr {1500my$cntrl=shift;1501my%opts=@_;15021503my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1504if($opts{-nohtml}) {1505return$chr;1506}else{1507return"<span class=\"cntrl\">$chr</span>";1508}1509}15101511# git may return quoted and escaped filenames1512sub unquote {1513my$str=shift;15141515sub unq {1516my$seq=shift;1517my%es= (# character escape codes, aka escape sequences1518't'=>"\t",# tab (HT, TAB)1519'n'=>"\n",# newline (NL)1520'r'=>"\r",# return (CR)1521'f'=>"\f",# form feed (FF)1522'b'=>"\b",# backspace (BS)1523'a'=>"\a",# alarm (bell) (BEL)1524'e'=>"\e",# escape (ESC)1525'v'=>"\013",# vertical tab (VT)1526);15271528if($seq=~m/^[0-7]{1,3}$/) {1529# octal char sequence1530returnchr(oct($seq));1531}elsif(exists$es{$seq}) {1532# C escape sequence, aka character escape code1533return$es{$seq};1534}1535# quoted ordinary character1536return$seq;1537}15381539if($str=~m/^"(.*)"$/) {1540# needs unquoting1541$str=$1;1542$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1543}1544return$str;1545}15461547# escape tabs (convert tabs to spaces)1548sub untabify {1549my$line=shift;15501551while((my$pos=index($line,"\t")) != -1) {1552if(my$count= (8- ($pos%8))) {1553my$spaces=' ' x $count;1554$line=~s/\t/$spaces/;1555}1556}15571558return$line;1559}15601561sub project_in_list {1562my$project=shift;1563my@list= git_get_projects_list();1564return@list&&scalar(grep{$_->{'path'}eq$project}@list);1565}15661567## ----------------------------------------------------------------------1568## HTML aware string manipulation15691570# Try to chop given string on a word boundary between position1571# $len and $len+$add_len. If there is no word boundary there,1572# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1573# (marking chopped part) would be longer than given string.1574sub chop_str {1575my$str=shift;1576my$len=shift;1577my$add_len=shift||10;1578my$where=shift||'right';# 'left' | 'center' | 'right'15791580# Make sure perl knows it is utf8 encoded so we don't1581# cut in the middle of a utf8 multibyte char.1582$str= to_utf8($str);15831584# allow only $len chars, but don't cut a word if it would fit in $add_len1585# if it doesn't fit, cut it if it's still longer than the dots we would add1586# remove chopped character entities entirely15871588# when chopping in the middle, distribute $len into left and right part1589# return early if chopping wouldn't make string shorter1590if($whereeq'center') {1591return$strif($len+5>=length($str));# filler is length 51592$len=int($len/2);1593}else{1594return$strif($len+4>=length($str));# filler is length 41595}15961597# regexps: ending and beginning with word part up to $add_len1598my$endre=qr/.{$len}\w{0,$add_len}/;1599my$begre=qr/\w{0,$add_len}.{$len}/;16001601if($whereeq'left') {1602$str=~m/^(.*?)($begre)$/;1603my($lead,$body) = ($1,$2);1604if(length($lead) >4) {1605$lead=" ...";1606}1607return"$lead$body";16081609}elsif($whereeq'center') {1610$str=~m/^($endre)(.*)$/;1611my($left,$str) = ($1,$2);1612$str=~m/^(.*?)($begre)$/;1613my($mid,$right) = ($1,$2);1614if(length($mid) >5) {1615$mid=" ... ";1616}1617return"$left$mid$right";16181619}else{1620$str=~m/^($endre)(.*)$/;1621my$body=$1;1622my$tail=$2;1623if(length($tail) >4) {1624$tail="... ";1625}1626return"$body$tail";1627}1628}16291630# takes the same arguments as chop_str, but also wraps a <span> around the1631# result with a title attribute if it does get chopped. Additionally, the1632# string is HTML-escaped.1633sub chop_and_escape_str {1634my($str) =@_;16351636my$chopped= chop_str(@_);1637if($choppedeq$str) {1638return esc_html($chopped);1639}else{1640$str=~s/[[:cntrl:]]/?/g;1641return$cgi->span({-title=>$str}, esc_html($chopped));1642}1643}16441645## ----------------------------------------------------------------------1646## functions returning short strings16471648# CSS class for given age value (in seconds)1649sub age_class {1650my$age=shift;16511652if(!defined$age) {1653return"noage";1654}elsif($age<60*60*2) {1655return"age0";1656}elsif($age<60*60*24*2) {1657return"age1";1658}else{1659return"age2";1660}1661}16621663# convert age in seconds to "nn units ago" string1664sub age_string {1665my$age=shift;1666my$age_str;16671668if($age>60*60*24*365*2) {1669$age_str= (int$age/60/60/24/365);1670$age_str.=" years ago";1671}elsif($age>60*60*24*(365/12)*2) {1672$age_str=int$age/60/60/24/(365/12);1673$age_str.=" months ago";1674}elsif($age>60*60*24*7*2) {1675$age_str=int$age/60/60/24/7;1676$age_str.=" weeks ago";1677}elsif($age>60*60*24*2) {1678$age_str=int$age/60/60/24;1679$age_str.=" days ago";1680}elsif($age>60*60*2) {1681$age_str=int$age/60/60;1682$age_str.=" hours ago";1683}elsif($age>60*2) {1684$age_str=int$age/60;1685$age_str.=" min ago";1686}elsif($age>2) {1687$age_str=int$age;1688$age_str.=" sec ago";1689}else{1690$age_str.=" right now";1691}1692return$age_str;1693}16941695useconstant{1696 S_IFINVALID =>0030000,1697 S_IFGITLINK =>0160000,1698};16991700# submodule/subproject, a commit object reference1701sub S_ISGITLINK {1702my$mode=shift;17031704return(($mode& S_IFMT) == S_IFGITLINK)1705}17061707# convert file mode in octal to symbolic file mode string1708sub mode_str {1709my$mode=oct shift;17101711if(S_ISGITLINK($mode)) {1712return'm---------';1713}elsif(S_ISDIR($mode& S_IFMT)) {1714return'drwxr-xr-x';1715}elsif(S_ISLNK($mode)) {1716return'lrwxrwxrwx';1717}elsif(S_ISREG($mode)) {1718# git cares only about the executable bit1719if($mode& S_IXUSR) {1720return'-rwxr-xr-x';1721}else{1722return'-rw-r--r--';1723};1724}else{1725return'----------';1726}1727}17281729# convert file mode in octal to file type string1730sub file_type {1731my$mode=shift;17321733if($mode!~m/^[0-7]+$/) {1734return$mode;1735}else{1736$mode=oct$mode;1737}17381739if(S_ISGITLINK($mode)) {1740return"submodule";1741}elsif(S_ISDIR($mode& S_IFMT)) {1742return"directory";1743}elsif(S_ISLNK($mode)) {1744return"symlink";1745}elsif(S_ISREG($mode)) {1746return"file";1747}else{1748return"unknown";1749}1750}17511752# convert file mode in octal to file type description string1753sub file_type_long {1754my$mode=shift;17551756if($mode!~m/^[0-7]+$/) {1757return$mode;1758}else{1759$mode=oct$mode;1760}17611762if(S_ISGITLINK($mode)) {1763return"submodule";1764}elsif(S_ISDIR($mode& S_IFMT)) {1765return"directory";1766}elsif(S_ISLNK($mode)) {1767return"symlink";1768}elsif(S_ISREG($mode)) {1769if($mode& S_IXUSR) {1770return"executable";1771}else{1772return"file";1773};1774}else{1775return"unknown";1776}1777}177817791780## ----------------------------------------------------------------------1781## functions returning short HTML fragments, or transforming HTML fragments1782## which don't belong to other sections17831784# format line of commit message.1785sub format_log_line_html {1786my$line=shift;17871788$line= esc_html($line, -nbsp=>1);1789$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1790$cgi->a({-href => href(action=>"object", hash=>$1),1791-class=>"text"},$1);1792}eg;17931794return$line;1795}17961797# format marker of refs pointing to given object17981799# the destination action is chosen based on object type and current context:1800# - for annotated tags, we choose the tag view unless it's the current view1801# already, in which case we go to shortlog view1802# - for other refs, we keep the current view if we're in history, shortlog or1803# log view, and select shortlog otherwise1804sub format_ref_marker {1805my($refs,$id) =@_;1806my$markers='';18071808if(defined$refs->{$id}) {1809foreachmy$ref(@{$refs->{$id}}) {1810# this code exploits the fact that non-lightweight tags are the1811# only indirect objects, and that they are the only objects for which1812# we want to use tag instead of shortlog as action1813my($type,$name) =qw();1814my$indirect= ($ref=~s/\^\{\}$//);1815# e.g. tags/v2.6.11 or heads/next1816if($ref=~m!^(.*?)s?/(.*)$!) {1817$type=$1;1818$name=$2;1819}else{1820$type="ref";1821$name=$ref;1822}18231824my$class=$type;1825$class.=" indirect"if$indirect;18261827my$dest_action="shortlog";18281829if($indirect) {1830$dest_action="tag"unless$actioneq"tag";1831}elsif($action=~/^(history|(short)?log)$/) {1832$dest_action=$action;1833}18341835my$dest="";1836$dest.="refs/"unless$ref=~ m!^refs/!;1837$dest.=$ref;18381839my$link=$cgi->a({1840-href => href(1841 action=>$dest_action,1842 hash=>$dest1843)},$name);18441845$markers.=" <span class=\"".esc_attr($class)."\"title=\"".esc_attr($ref)."\">".1846$link."</span>";1847}1848}18491850if($markers) {1851return' <span class="refs">'.$markers.'</span>';1852}else{1853return"";1854}1855}18561857# format, perhaps shortened and with markers, title line1858sub format_subject_html {1859my($long,$short,$href,$extra) =@_;1860$extra=''unlessdefined($extra);18611862if(length($short) <length($long)) {1863$long=~s/[[:cntrl:]]/?/g;1864return$cgi->a({-href =>$href, -class=>"list subject",1865-title => to_utf8($long)},1866 esc_html($short)) .$extra;1867}else{1868return$cgi->a({-href =>$href, -class=>"list subject"},1869 esc_html($long)) .$extra;1870}1871}18721873# Rather than recomputing the url for an email multiple times, we cache it1874# after the first hit. This gives a visible benefit in views where the avatar1875# for the same email is used repeatedly (e.g. shortlog).1876# The cache is shared by all avatar engines (currently gravatar only), which1877# are free to use it as preferred. Since only one avatar engine is used for any1878# given page, there's no risk for cache conflicts.1879our%avatar_cache= ();18801881# Compute the picon url for a given email, by using the picon search service over at1882# http://www.cs.indiana.edu/picons/search.html1883sub picon_url {1884my$email=lc shift;1885if(!$avatar_cache{$email}) {1886my($user,$domain) =split('@',$email);1887$avatar_cache{$email} =1888"http://www.cs.indiana.edu/cgi-pub/kinzler/piconsearch.cgi/".1889"$domain/$user/".1890"users+domains+unknown/up/single";1891}1892return$avatar_cache{$email};1893}18941895# Compute the gravatar url for a given email, if it's not in the cache already.1896# Gravatar stores only the part of the URL before the size, since that's the1897# one computationally more expensive. This also allows reuse of the cache for1898# different sizes (for this particular engine).1899sub gravatar_url {1900my$email=lc shift;1901my$size=shift;1902$avatar_cache{$email} ||=1903"http://www.gravatar.com/avatar/".1904 Digest::MD5::md5_hex($email) ."?s=";1905return$avatar_cache{$email} .$size;1906}19071908# Insert an avatar for the given $email at the given $size if the feature1909# is enabled.1910sub git_get_avatar {1911my($email,%opts) =@_;1912my$pre_white= ($opts{-pad_before} ?" ":"");1913my$post_white= ($opts{-pad_after} ?" ":"");1914$opts{-size} ||='default';1915my$size=$avatar_size{$opts{-size}} ||$avatar_size{'default'};1916my$url="";1917if($git_avatareq'gravatar') {1918$url= gravatar_url($email,$size);1919}elsif($git_avatareq'picon') {1920$url= picon_url($email);1921}1922# Other providers can be added by extending the if chain, defining $url1923# as needed. If no variant puts something in $url, we assume avatars1924# are completely disabled/unavailable.1925if($url) {1926return$pre_white.1927"<img width=\"$size\"".1928"class=\"avatar\"".1929"src=\"".esc_url($url)."\"".1930"alt=\"\"".1931"/>".$post_white;1932}else{1933return"";1934}1935}19361937sub format_search_author {1938my($author,$searchtype,$displaytext) =@_;1939my$have_search= gitweb_check_feature('search');19401941if($have_search) {1942my$performed="";1943if($searchtypeeq'author') {1944$performed="authored";1945}elsif($searchtypeeq'committer') {1946$performed="committed";1947}19481949return$cgi->a({-href => href(action=>"search", hash=>$hash,1950 searchtext=>$author,1951 searchtype=>$searchtype),class=>"list",1952 title=>"Search for commits$performedby$author"},1953$displaytext);19541955}else{1956return$displaytext;1957}1958}19591960# format the author name of the given commit with the given tag1961# the author name is chopped and escaped according to the other1962# optional parameters (see chop_str).1963sub format_author_html {1964my$tag=shift;1965my$co=shift;1966my$author= chop_and_escape_str($co->{'author_name'},@_);1967return"<$tagclass=\"author\">".1968 format_search_author($co->{'author_name'},"author",1969 git_get_avatar($co->{'author_email'}, -pad_after =>1) .1970$author) .1971"</$tag>";1972}19731974# format git diff header line, i.e. "diff --(git|combined|cc) ..."1975sub format_git_diff_header_line {1976my$line=shift;1977my$diffinfo=shift;1978my($from,$to) =@_;19791980if($diffinfo->{'nparents'}) {1981# combined diff1982$line=~s!^(diff (.*?) )"?.*$!$1!;1983if($to->{'href'}) {1984$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1985 esc_path($to->{'file'}));1986}else{# file was deleted (no href)1987$line.= esc_path($to->{'file'});1988}1989}else{1990# "ordinary" diff1991$line=~s!^(diff (.*?) )"?a/.*$!$1!;1992if($from->{'href'}) {1993$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1994'a/'. esc_path($from->{'file'}));1995}else{# file was added (no href)1996$line.='a/'. esc_path($from->{'file'});1997}1998$line.=' ';1999if($to->{'href'}) {2000$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},2001'b/'. esc_path($to->{'file'}));2002}else{# file was deleted2003$line.='b/'. esc_path($to->{'file'});2004}2005}20062007return"<div class=\"diff header\">$line</div>\n";2008}20092010# format extended diff header line, before patch itself2011sub format_extended_diff_header_line {2012my$line=shift;2013my$diffinfo=shift;2014my($from,$to) =@_;20152016# match <path>2017if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {2018$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},2019 esc_path($from->{'file'}));2020}2021if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {2022$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},2023 esc_path($to->{'file'}));2024}2025# match single <mode>2026if($line=~m/\s(\d{6})$/) {2027$line.='<span class="info"> ('.2028 file_type_long($1) .2029')</span>';2030}2031# match <hash>2032if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {2033# can match only for combined diff2034$line='index ';2035for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2036if($from->{'href'}[$i]) {2037$line.=$cgi->a({-href=>$from->{'href'}[$i],2038-class=>"hash"},2039substr($diffinfo->{'from_id'}[$i],0,7));2040}else{2041$line.='0' x 7;2042}2043# separator2044$line.=','if($i<$diffinfo->{'nparents'} -1);2045}2046$line.='..';2047if($to->{'href'}) {2048$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2049substr($diffinfo->{'to_id'},0,7));2050}else{2051$line.='0' x 7;2052}20532054}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {2055# can match only for ordinary diff2056my($from_link,$to_link);2057if($from->{'href'}) {2058$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},2059substr($diffinfo->{'from_id'},0,7));2060}else{2061$from_link='0' x 7;2062}2063if($to->{'href'}) {2064$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},2065substr($diffinfo->{'to_id'},0,7));2066}else{2067$to_link='0' x 7;2068}2069my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});2070$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;2071}20722073return$line."<br/>\n";2074}20752076# format from-file/to-file diff header2077sub format_diff_from_to_header {2078my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;2079my$line;2080my$result='';20812082$line=$from_line;2083#assert($line =~ m/^---/) if DEBUG;2084# no extra formatting for "^--- /dev/null"2085if(!$diffinfo->{'nparents'}) {2086# ordinary (single parent) diff2087if($line=~m!^--- "?a/!) {2088if($from->{'href'}) {2089$line='--- a/'.2090$cgi->a({-href=>$from->{'href'}, -class=>"path"},2091 esc_path($from->{'file'}));2092}else{2093$line='--- a/'.2094 esc_path($from->{'file'});2095}2096}2097$result.= qq!<div class="diff from_file">$line</div>\n!;20982099}else{2100# combined diff (merge commit)2101for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2102if($from->{'href'}[$i]) {2103$line='--- '.2104$cgi->a({-href=>href(action=>"blobdiff",2105 hash_parent=>$diffinfo->{'from_id'}[$i],2106 hash_parent_base=>$parents[$i],2107 file_parent=>$from->{'file'}[$i],2108 hash=>$diffinfo->{'to_id'},2109 hash_base=>$hash,2110 file_name=>$to->{'file'}),2111-class=>"path",2112-title=>"diff". ($i+1)},2113$i+1) .2114'/'.2115$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},2116 esc_path($from->{'file'}[$i]));2117}else{2118$line='--- /dev/null';2119}2120$result.= qq!<div class="diff from_file">$line</div>\n!;2121}2122}21232124$line=$to_line;2125#assert($line =~ m/^\+\+\+/) if DEBUG;2126# no extra formatting for "^+++ /dev/null"2127if($line=~m!^\+\+\+ "?b/!) {2128if($to->{'href'}) {2129$line='+++ b/'.2130$cgi->a({-href=>$to->{'href'}, -class=>"path"},2131 esc_path($to->{'file'}));2132}else{2133$line='+++ b/'.2134 esc_path($to->{'file'});2135}2136}2137$result.= qq!<div class="diff to_file">$line</div>\n!;21382139return$result;2140}21412142# create note for patch simplified by combined diff2143sub format_diff_cc_simplified {2144my($diffinfo,@parents) =@_;2145my$result='';21462147$result.="<div class=\"diff header\">".2148"diff --cc ";2149if(!is_deleted($diffinfo)) {2150$result.=$cgi->a({-href => href(action=>"blob",2151 hash_base=>$hash,2152 hash=>$diffinfo->{'to_id'},2153 file_name=>$diffinfo->{'to_file'}),2154-class=>"path"},2155 esc_path($diffinfo->{'to_file'}));2156}else{2157$result.= esc_path($diffinfo->{'to_file'});2158}2159$result.="</div>\n".# class="diff header"2160"<div class=\"diff nodifferences\">".2161"Simple merge".2162"</div>\n";# class="diff nodifferences"21632164return$result;2165}21662167# format patch (diff) line (not to be used for diff headers)2168sub format_diff_line {2169my$line=shift;2170my($from,$to) =@_;2171my$diff_class="";21722173chomp$line;21742175if($from&&$to&&ref($from->{'href'})eq"ARRAY") {2176# combined diff2177my$prefix=substr($line,0,scalar@{$from->{'href'}});2178if($line=~m/^\@{3}/) {2179$diff_class=" chunk_header";2180}elsif($line=~m/^\\/) {2181$diff_class=" incomplete";2182}elsif($prefix=~tr/+/+/) {2183$diff_class=" add";2184}elsif($prefix=~tr/-/-/) {2185$diff_class=" rem";2186}2187}else{2188# assume ordinary diff2189my$char=substr($line,0,1);2190if($chareq'+') {2191$diff_class=" add";2192}elsif($chareq'-') {2193$diff_class=" rem";2194}elsif($chareq'@') {2195$diff_class=" chunk_header";2196}elsif($chareq"\\") {2197$diff_class=" incomplete";2198}2199}2200$line= untabify($line);2201if($from&&$to&&$line=~m/^\@{2} /) {2202my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =2203$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;22042205$from_lines=0unlessdefined$from_lines;2206$to_lines=0unlessdefined$to_lines;22072208if($from->{'href'}) {2209$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",2210-class=>"list"},$from_text);2211}2212if($to->{'href'}) {2213$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",2214-class=>"list"},$to_text);2215}2216$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".2217"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2218return"<div class=\"diff$diff_class\">$line</div>\n";2219}elsif($from&&$to&&$line=~m/^\@{3}/) {2220my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;2221my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);22222223@from_text=split(' ',$ranges);2224for(my$i=0;$i<@from_text; ++$i) {2225($from_start[$i],$from_nlines[$i]) =2226(split(',',substr($from_text[$i],1)),0);2227}22282229$to_text=pop@from_text;2230$to_start=pop@from_start;2231$to_nlines=pop@from_nlines;22322233$line="<span class=\"chunk_info\">$prefix";2234for(my$i=0;$i<@from_text; ++$i) {2235if($from->{'href'}[$i]) {2236$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",2237-class=>"list"},$from_text[$i]);2238}else{2239$line.=$from_text[$i];2240}2241$line.=" ";2242}2243if($to->{'href'}) {2244$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",2245-class=>"list"},$to_text);2246}else{2247$line.=$to_text;2248}2249$line.="$prefix</span>".2250"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";2251return"<div class=\"diff$diff_class\">$line</div>\n";2252}2253return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";2254}22552256# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",2257# linked. Pass the hash of the tree/commit to snapshot.2258sub format_snapshot_links {2259my($hash) =@_;2260my$num_fmts=@snapshot_fmts;2261if($num_fmts>1) {2262# A parenthesized list of links bearing format names.2263# e.g. "snapshot (_tar.gz_ _zip_)"2264return"snapshot (".join(' ',map2265$cgi->a({2266-href => href(2267 action=>"snapshot",2268 hash=>$hash,2269 snapshot_format=>$_2270)2271},$known_snapshot_formats{$_}{'display'})2272,@snapshot_fmts) .")";2273}elsif($num_fmts==1) {2274# A single "snapshot" link whose tooltip bears the format name.2275# i.e. "_snapshot_"2276my($fmt) =@snapshot_fmts;2277return2278$cgi->a({2279-href => href(2280 action=>"snapshot",2281 hash=>$hash,2282 snapshot_format=>$fmt2283),2284-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"2285},"snapshot");2286}else{# $num_fmts == 02287returnundef;2288}2289}22902291## ......................................................................2292## functions returning values to be passed, perhaps after some2293## transformation, to other functions; e.g. returning arguments to href()22942295# returns hash to be passed to href to generate gitweb URL2296# in -title key it returns description of link2297sub get_feed_info {2298my$format=shift||'Atom';2299my%res= (action =>lc($format));23002301# feed links are possible only for project views2302return unless(defined$project);2303# some views should link to OPML, or to generic project feed,2304# or don't have specific feed yet (so they should use generic)2305return if($action=~/^(?:tags|heads|forks|tag|search)$/x);23062307my$branch;2308# branches refs uses 'refs/heads/' prefix (fullname) to differentiate2309# from tag links; this also makes possible to detect branch links2310if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||2311(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {2312$branch=$1;2313}2314# find log type for feed description (title)2315my$type='log';2316if(defined$file_name) {2317$type="history of$file_name";2318$type.="/"if($actioneq'tree');2319$type.=" on '$branch'"if(defined$branch);2320}else{2321$type="log of$branch"if(defined$branch);2322}23232324$res{-title} =$type;2325$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);2326$res{'file_name'} =$file_name;23272328return%res;2329}23302331## ----------------------------------------------------------------------2332## git utility subroutines, invoking git commands23332334# returns path to the core git executable and the --git-dir parameter as list2335sub git_cmd {2336$number_of_git_cmds++;2337return$GIT,'--git-dir='.$git_dir;2338}23392340# quote the given arguments for passing them to the shell2341# quote_command("command", "arg 1", "arg with ' and ! characters")2342# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"2343# Try to avoid using this function wherever possible.2344sub quote_command {2345returnjoin(' ',2346map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);2347}23482349# get HEAD ref of given project as hash2350sub git_get_head_hash {2351return git_get_full_hash(shift,'HEAD');2352}23532354sub git_get_full_hash {2355return git_get_hash(@_);2356}23572358sub git_get_short_hash {2359return git_get_hash(@_,'--short=7');2360}23612362sub git_get_hash {2363my($project,$hash,@options) =@_;2364my$o_git_dir=$git_dir;2365my$retval=undef;2366$git_dir="$projectroot/$project";2367if(open my$fd,'-|', git_cmd(),'rev-parse',2368'--verify','-q',@options,$hash) {2369$retval= <$fd>;2370chomp$retvalifdefined$retval;2371close$fd;2372}2373if(defined$o_git_dir) {2374$git_dir=$o_git_dir;2375}2376return$retval;2377}23782379# get type of given object2380sub git_get_type {2381my$hash=shift;23822383open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;2384my$type= <$fd>;2385close$fdorreturn;2386chomp$type;2387return$type;2388}23892390# repository configuration2391our$config_file='';2392our%config;23932394# store multiple values for single key as anonymous array reference2395# single values stored directly in the hash, not as [ <value> ]2396sub hash_set_multi {2397my($hash,$key,$value) =@_;23982399if(!exists$hash->{$key}) {2400$hash->{$key} =$value;2401}elsif(!ref$hash->{$key}) {2402$hash->{$key} = [$hash->{$key},$value];2403}else{2404push@{$hash->{$key}},$value;2405}2406}24072408# return hash of git project configuration2409# optionally limited to some section, e.g. 'gitweb'2410sub git_parse_project_config {2411my$section_regexp=shift;2412my%config;24132414local$/="\0";24152416open my$fh,"-|", git_cmd(),"config",'-z','-l',2417orreturn;24182419while(my$keyval= <$fh>) {2420chomp$keyval;2421my($key,$value) =split(/\n/,$keyval,2);24222423 hash_set_multi(\%config,$key,$value)2424if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);2425}2426close$fh;24272428return%config;2429}24302431# convert config value to boolean: 'true' or 'false'2432# no value, number > 0, 'true' and 'yes' values are true2433# rest of values are treated as false (never as error)2434sub config_to_bool {2435my$val=shift;24362437return1if!defined$val;# section.key24382439# strip leading and trailing whitespace2440$val=~s/^\s+//;2441$val=~s/\s+$//;24422443return(($val=~/^\d+$/&&$val) ||# section.key = 12444($val=~/^(?:true|yes)$/i));# section.key = true2445}24462447# convert config value to simple decimal number2448# an optional value suffix of 'k', 'm', or 'g' will cause the value2449# to be multiplied by 1024, 1048576, or 10737418242450sub config_to_int {2451my$val=shift;24522453# strip leading and trailing whitespace2454$val=~s/^\s+//;2455$val=~s/\s+$//;24562457if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {2458$unit=lc($unit);2459# unknown unit is treated as 12460return$num* ($uniteq'g'?1073741824:2461$uniteq'm'?1048576:2462$uniteq'k'?1024:1);2463}2464return$val;2465}24662467# convert config value to array reference, if needed2468sub config_to_multi {2469my$val=shift;24702471returnref($val) ?$val: (defined($val) ? [$val] : []);2472}24732474sub git_get_project_config {2475my($key,$type) =@_;24762477return unlessdefined$git_dir;24782479# key sanity check2480return unless($key);2481$key=~s/^gitweb\.//;2482return if($key=~m/\W/);24832484# type sanity check2485if(defined$type) {2486$type=~s/^--//;2487$type=undef2488unless($typeeq'bool'||$typeeq'int');2489}24902491# get config2492if(!defined$config_file||2493$config_filene"$git_dir/config") {2494%config= git_parse_project_config('gitweb');2495$config_file="$git_dir/config";2496}24972498# check if config variable (key) exists2499return unlessexists$config{"gitweb.$key"};25002501# ensure given type2502if(!defined$type) {2503return$config{"gitweb.$key"};2504}elsif($typeeq'bool') {2505# backward compatibility: 'git config --bool' returns true/false2506return config_to_bool($config{"gitweb.$key"}) ?'true':'false';2507}elsif($typeeq'int') {2508return config_to_int($config{"gitweb.$key"});2509}2510return$config{"gitweb.$key"};2511}25122513# get hash of given path at given ref2514sub git_get_hash_by_path {2515my$base=shift;2516my$path=shift||returnundef;2517my$type=shift;25182519$path=~ s,/+$,,;25202521open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2522or die_error(500,"Open git-ls-tree failed");2523my$line= <$fd>;2524close$fdorreturnundef;25252526if(!defined$line) {2527# there is no tree or hash given by $path at $base2528returnundef;2529}25302531#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2532$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2533if(defined$type&&$typene$2) {2534# type doesn't match2535returnundef;2536}2537return$3;2538}25392540# get path of entry with given hash at given tree-ish (ref)2541# used to get 'from' filename for combined diff (merge commit) for renames2542sub git_get_path_by_hash {2543my$base=shift||return;2544my$hash=shift||return;25452546local$/="\0";25472548open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2549orreturnundef;2550while(my$line= <$fd>) {2551chomp$line;25522553#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2554#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2555if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2556close$fd;2557return$1;2558}2559}2560close$fd;2561returnundef;2562}25632564## ......................................................................2565## git utility functions, directly accessing git repository25662567sub git_get_project_description {2568my$path=shift;25692570$git_dir="$projectroot/$path";2571open my$fd,'<',"$git_dir/description"2572orreturn git_get_project_config('description');2573my$descr= <$fd>;2574close$fd;2575if(defined$descr) {2576chomp$descr;2577}2578return$descr;2579}25802581sub git_get_project_ctags {2582my$path=shift;2583my$ctags= {};25842585$git_dir="$projectroot/$path";2586opendir my$dh,"$git_dir/ctags"2587orreturn$ctags;2588foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2589open my$ct,'<',$_ornext;2590my$val= <$ct>;2591chomp$val;2592close$ct;2593my$ctag=$_;$ctag=~ s#.*/##;2594$ctags->{$ctag} =$val;2595}2596closedir$dh;2597$ctags;2598}25992600sub git_populate_project_tagcloud {2601my$ctags=shift;26022603# First, merge different-cased tags; tags vote on casing2604my%ctags_lc;2605foreach(keys%$ctags) {2606$ctags_lc{lc$_}->{count} +=$ctags->{$_};2607if(not$ctags_lc{lc$_}->{topcount}2608or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2609$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2610$ctags_lc{lc$_}->{topname} =$_;2611}2612}26132614my$cloud;2615if(eval{require HTML::TagCloud;1; }) {2616$cloud= HTML::TagCloud->new;2617foreach(sort keys%ctags_lc) {2618# Pad the title with spaces so that the cloud looks2619# less crammed.2620my$title=$ctags_lc{$_}->{topname};2621$title=~s/ / /g;2622$title=~s/^/ /g;2623$title=~s/$/ /g;2624$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2625}2626}else{2627$cloud= \%ctags_lc;2628}2629$cloud;2630}26312632sub git_show_project_tagcloud {2633my($cloud,$count) =@_;2634print STDERR ref($cloud)."..\n";2635if(ref$cloudeq'HTML::TagCloud') {2636return$cloud->html_and_css($count);2637}else{2638my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2639return'<p align="center">'.join(', ',map{2640$cgi->a({-href=>"$home_link?by_tag=$_"},$cloud->{$_}->{topname})2641}splice(@tags,0,$count)) .'</p>';2642}2643}26442645sub git_get_project_url_list {2646my$path=shift;26472648$git_dir="$projectroot/$path";2649open my$fd,'<',"$git_dir/cloneurl"2650orreturnwantarray?2651@{ config_to_multi(git_get_project_config('url')) } :2652 config_to_multi(git_get_project_config('url'));2653my@git_project_url_list=map{chomp;$_} <$fd>;2654close$fd;26552656returnwantarray?@git_project_url_list: \@git_project_url_list;2657}26582659sub git_get_projects_list {2660my($filter) =@_;2661my@list;26622663$filter||='';2664$filter=~s/\.git$//;26652666my$check_forks= gitweb_check_feature('forks');26672668if(-d $projects_list) {2669# search in directory2670my$dir=$projects_list. ($filter?"/$filter":'');2671# remove the trailing "/"2672$dir=~s!/+$!!;2673my$pfxlen=length("$dir");2674my$pfxdepth= ($dir=~tr!/!!);26752676 File::Find::find({2677 follow_fast =>1,# follow symbolic links2678 follow_skip =>2,# ignore duplicates2679 dangling_symlinks =>0,# ignore dangling symlinks, silently2680 wanted =>sub{2681# global variables2682our$project_maxdepth;2683our$projectroot;2684# skip project-list toplevel, if we get it.2685return if(m!^[/.]$!);2686# only directories can be git repositories2687return unless(-d $_);2688# don't traverse too deep (Find is super slow on os x)2689if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2690$File::Find::prune =1;2691return;2692}26932694my$subdir=substr($File::Find::name,$pfxlen+1);2695# we check related file in $projectroot2696my$path= ($filter?"$filter/":'') .$subdir;2697if(check_export_ok("$projectroot/$path")) {2698push@list, { path =>$path};2699$File::Find::prune =1;2700}2701},2702},"$dir");27032704}elsif(-f $projects_list) {2705# read from file(url-encoded):2706# 'git%2Fgit.git Linus+Torvalds'2707# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2708# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2709my%paths;2710open my$fd,'<',$projects_listorreturn;2711 PROJECT:2712while(my$line= <$fd>) {2713chomp$line;2714my($path,$owner) =split' ',$line;2715$path= unescape($path);2716$owner= unescape($owner);2717if(!defined$path) {2718next;2719}2720if($filterne'') {2721# looking for forks;2722my$pfx=substr($path,0,length($filter));2723if($pfxne$filter) {2724next PROJECT;2725}2726my$sfx=substr($path,length($filter));2727if($sfx!~/^\/.*\.git$/) {2728next PROJECT;2729}2730}elsif($check_forks) {2731 PATH:2732foreachmy$filter(keys%paths) {2733# looking for forks;2734my$pfx=substr($path,0,length($filter));2735if($pfxne$filter) {2736next PATH;2737}2738my$sfx=substr($path,length($filter));2739if($sfx!~/^\/.*\.git$/) {2740next PATH;2741}2742# is a fork, don't include it in2743# the list2744next PROJECT;2745}2746}2747if(check_export_ok("$projectroot/$path")) {2748my$pr= {2749 path =>$path,2750 owner => to_utf8($owner),2751};2752push@list,$pr;2753(my$forks_path=$path) =~s/\.git$//;2754$paths{$forks_path}++;2755}2756}2757close$fd;2758}2759return@list;2760}27612762our$gitweb_project_owner=undef;2763sub git_get_project_list_from_file {27642765return if(defined$gitweb_project_owner);27662767$gitweb_project_owner= {};2768# read from file (url-encoded):2769# 'git%2Fgit.git Linus+Torvalds'2770# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2771# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2772if(-f $projects_list) {2773open(my$fd,'<',$projects_list);2774while(my$line= <$fd>) {2775chomp$line;2776my($pr,$ow) =split' ',$line;2777$pr= unescape($pr);2778$ow= unescape($ow);2779$gitweb_project_owner->{$pr} = to_utf8($ow);2780}2781close$fd;2782}2783}27842785sub git_get_project_owner {2786my$project=shift;2787my$owner;27882789returnundefunless$project;2790$git_dir="$projectroot/$project";27912792if(!defined$gitweb_project_owner) {2793 git_get_project_list_from_file();2794}27952796if(exists$gitweb_project_owner->{$project}) {2797$owner=$gitweb_project_owner->{$project};2798}2799if(!defined$owner){2800$owner= git_get_project_config('owner');2801}2802if(!defined$owner) {2803$owner= get_file_owner("$git_dir");2804}28052806return$owner;2807}28082809sub git_get_last_activity {2810my($path) =@_;2811my$fd;28122813$git_dir="$projectroot/$path";2814open($fd,"-|", git_cmd(),'for-each-ref',2815'--format=%(committer)',2816'--sort=-committerdate',2817'--count=1',2818'refs/heads')orreturn;2819my$most_recent= <$fd>;2820close$fdorreturn;2821if(defined$most_recent&&2822$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2823my$timestamp=$1;2824my$age=time-$timestamp;2825return($age, age_string($age));2826}2827return(undef,undef);2828}28292830# Implementation note: when a single remote is wanted, we cannot use 'git2831# remote show -n' because that command always work (assuming it's a remote URL2832# if it's not defined), and we cannot use 'git remote show' because that would2833# try to make a network roundtrip. So the only way to find if that particular2834# remote is defined is to walk the list provided by 'git remote -v' and stop if2835# and when we find what we want.2836sub git_get_remotes_list {2837my$wanted=shift;2838my%remotes= ();28392840open my$fd,'-|', git_cmd(),'remote','-v';2841return unless$fd;2842while(my$remote= <$fd>) {2843chomp$remote;2844$remote=~s!\t(.*?)\s+\((\w+)\)$!!;2845next if$wantedand not$remoteeq$wanted;2846my($url,$key) = ($1,$2);28472848$remotes{$remote} ||= {'heads'=> () };2849$remotes{$remote}{$key} =$url;2850}2851close$fdorreturn;2852returnwantarray?%remotes: \%remotes;2853}28542855# Takes a hash of remotes as first parameter and fills it by adding the2856# available remote heads for each of the indicated remotes.2857sub fill_remote_heads {2858my$remotes=shift;2859my@heads=map{"remotes/$_"}keys%$remotes;2860my@remoteheads= git_get_heads_list(undef,@heads);2861foreachmy$remote(keys%$remotes) {2862$remotes->{$remote}{'heads'} = [grep{2863$_->{'name'} =~s!^$remote/!!2864}@remoteheads];2865}2866}28672868sub git_get_references {2869my$type=shift||"";2870my%refs;2871# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112872# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2873open my$fd,"-|", git_cmd(),"show-ref","--dereference",2874($type? ("--","refs/$type") : ())# use -- <pattern> if $type2875orreturn;28762877while(my$line= <$fd>) {2878chomp$line;2879if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2880if(defined$refs{$1}) {2881push@{$refs{$1}},$2;2882}else{2883$refs{$1} = [$2];2884}2885}2886}2887close$fdorreturn;2888return \%refs;2889}28902891sub git_get_rev_name_tags {2892my$hash=shift||returnundef;28932894open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2895orreturn;2896my$name_rev= <$fd>;2897close$fd;28982899if($name_rev=~ m|^$hash tags/(.*)$|) {2900return$1;2901}else{2902# catches also '$hash undefined' output2903returnundef;2904}2905}29062907## ----------------------------------------------------------------------2908## parse to hash functions29092910sub parse_date {2911my$epoch=shift;2912my$tz=shift||"-0000";29132914my%date;2915my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2916my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2917my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2918$date{'hour'} =$hour;2919$date{'minute'} =$min;2920$date{'mday'} =$mday;2921$date{'day'} =$days[$wday];2922$date{'month'} =$months[$mon];2923$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2924$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2925$date{'mday-time'} =sprintf"%d%s%02d:%02d",2926$mday,$months[$mon],$hour,$min;2927$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",29281900+$year,1+$mon,$mday,$hour,$min,$sec;29292930my($tz_sign,$tz_hour,$tz_min) =2931($tz=~m/^([-+])(\d\d)(\d\d)$/);2932$tz_sign= ($tz_signeq'-'? -1: +1);2933my$local=$epoch+$tz_sign*((($tz_hour*60) +$tz_min)*60);2934($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2935$date{'hour_local'} =$hour;2936$date{'minute_local'} =$min;2937$date{'tz_local'} =$tz;2938$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",29391900+$year,$mon+1,$mday,2940$hour,$min,$sec,$tz);2941return%date;2942}29432944sub parse_tag {2945my$tag_id=shift;2946my%tag;2947my@comment;29482949open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2950$tag{'id'} =$tag_id;2951while(my$line= <$fd>) {2952chomp$line;2953if($line=~m/^object ([0-9a-fA-F]{40})$/) {2954$tag{'object'} =$1;2955}elsif($line=~m/^type (.+)$/) {2956$tag{'type'} =$1;2957}elsif($line=~m/^tag (.+)$/) {2958$tag{'name'} =$1;2959}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2960$tag{'author'} =$1;2961$tag{'author_epoch'} =$2;2962$tag{'author_tz'} =$3;2963if($tag{'author'} =~m/^([^<]+) <([^>]*)>/) {2964$tag{'author_name'} =$1;2965$tag{'author_email'} =$2;2966}else{2967$tag{'author_name'} =$tag{'author'};2968}2969}elsif($line=~m/--BEGIN/) {2970push@comment,$line;2971last;2972}elsif($lineeq"") {2973last;2974}2975}2976push@comment, <$fd>;2977$tag{'comment'} = \@comment;2978close$fdorreturn;2979if(!defined$tag{'name'}) {2980return2981};2982return%tag2983}29842985sub parse_commit_text {2986my($commit_text,$withparents) =@_;2987my@commit_lines=split'\n',$commit_text;2988my%co;29892990pop@commit_lines;# Remove '\0'29912992if(!@commit_lines) {2993return;2994}29952996my$header=shift@commit_lines;2997if($header!~m/^[0-9a-fA-F]{40}/) {2998return;2999}3000($co{'id'},my@parents) =split' ',$header;3001while(my$line=shift@commit_lines) {3002last if$lineeq"\n";3003if($line=~m/^tree ([0-9a-fA-F]{40})$/) {3004$co{'tree'} =$1;3005}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {3006push@parents,$1;3007}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {3008$co{'author'} = to_utf8($1);3009$co{'author_epoch'} =$2;3010$co{'author_tz'} =$3;3011if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {3012$co{'author_name'} =$1;3013$co{'author_email'} =$2;3014}else{3015$co{'author_name'} =$co{'author'};3016}3017}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {3018$co{'committer'} = to_utf8($1);3019$co{'committer_epoch'} =$2;3020$co{'committer_tz'} =$3;3021if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {3022$co{'committer_name'} =$1;3023$co{'committer_email'} =$2;3024}else{3025$co{'committer_name'} =$co{'committer'};3026}3027}3028}3029if(!defined$co{'tree'}) {3030return;3031};3032$co{'parents'} = \@parents;3033$co{'parent'} =$parents[0];30343035foreachmy$title(@commit_lines) {3036$title=~s/^ //;3037if($titlene"") {3038$co{'title'} = chop_str($title,80,5);3039# remove leading stuff of merges to make the interesting part visible3040if(length($title) >50) {3041$title=~s/^Automatic //;3042$title=~s/^merge (of|with) /Merge ... /i;3043if(length($title) >50) {3044$title=~s/(http|rsync):\/\///;3045}3046if(length($title) >50) {3047$title=~s/(master|www|rsync)\.//;3048}3049if(length($title) >50) {3050$title=~s/kernel.org:?//;3051}3052if(length($title) >50) {3053$title=~s/\/pub\/scm//;3054}3055}3056$co{'title_short'} = chop_str($title,50,5);3057last;3058}3059}3060if(!defined$co{'title'} ||$co{'title'}eq"") {3061$co{'title'} =$co{'title_short'} ='(no commit message)';3062}3063# remove added spaces3064foreachmy$line(@commit_lines) {3065$line=~s/^ //;3066}3067$co{'comment'} = \@commit_lines;30683069my$age=time-$co{'committer_epoch'};3070$co{'age'} =$age;3071$co{'age_string'} = age_string($age);3072my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});3073if($age>60*60*24*7*2) {3074$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3075$co{'age_string_age'} =$co{'age_string'};3076}else{3077$co{'age_string_date'} =$co{'age_string'};3078$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;3079}3080return%co;3081}30823083sub parse_commit {3084my($commit_id) =@_;3085my%co;30863087local$/="\0";30883089open my$fd,"-|", git_cmd(),"rev-list",3090"--parents",3091"--header",3092"--max-count=1",3093$commit_id,3094"--",3095or die_error(500,"Open git-rev-list failed");3096%co= parse_commit_text(<$fd>,1);3097close$fd;30983099return%co;3100}31013102sub parse_commits {3103my($commit_id,$maxcount,$skip,$filename,@args) =@_;3104my@cos;31053106$maxcount||=1;3107$skip||=0;31083109local$/="\0";31103111open my$fd,"-|", git_cmd(),"rev-list",3112"--header",3113@args,3114("--max-count=".$maxcount),3115("--skip=".$skip),3116@extra_options,3117$commit_id,3118"--",3119($filename? ($filename) : ())3120or die_error(500,"Open git-rev-list failed");3121while(my$line= <$fd>) {3122my%co= parse_commit_text($line);3123push@cos, \%co;3124}3125close$fd;31263127returnwantarray?@cos: \@cos;3128}31293130# parse line of git-diff-tree "raw" output3131sub parse_difftree_raw_line {3132my$line=shift;3133my%res;31343135# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'3136# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'3137if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {3138$res{'from_mode'} =$1;3139$res{'to_mode'} =$2;3140$res{'from_id'} =$3;3141$res{'to_id'} =$4;3142$res{'status'} =$5;3143$res{'similarity'} =$6;3144if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied3145($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);3146}else{3147$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);3148}3149}3150# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'3151# combined diff (for merge commit)3152elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {3153$res{'nparents'} =length($1);3154$res{'from_mode'} = [split(' ',$2) ];3155$res{'to_mode'} =pop@{$res{'from_mode'}};3156$res{'from_id'} = [split(' ',$3) ];3157$res{'to_id'} =pop@{$res{'from_id'}};3158$res{'status'} = [split('',$4) ];3159$res{'to_file'} = unquote($5);3160}3161# 'c512b523472485aef4fff9e57b229d9d243c967f'3162elsif($line=~m/^([0-9a-fA-F]{40})$/) {3163$res{'commit'} =$1;3164}31653166returnwantarray?%res: \%res;3167}31683169# wrapper: return parsed line of git-diff-tree "raw" output3170# (the argument might be raw line, or parsed info)3171sub parsed_difftree_line {3172my$line_or_ref=shift;31733174if(ref($line_or_ref)eq"HASH") {3175# pre-parsed (or generated by hand)3176return$line_or_ref;3177}else{3178return parse_difftree_raw_line($line_or_ref);3179}3180}31813182# parse line of git-ls-tree output3183sub parse_ls_tree_line {3184my$line=shift;3185my%opts=@_;3186my%res;31873188if($opts{'-l'}) {3189#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa 16717 panic.c'3190$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;31913192$res{'mode'} =$1;3193$res{'type'} =$2;3194$res{'hash'} =$3;3195$res{'size'} =$4;3196if($opts{'-z'}) {3197$res{'name'} =$5;3198}else{3199$res{'name'} = unquote($5);3200}3201}else{3202#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'3203$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;32043205$res{'mode'} =$1;3206$res{'type'} =$2;3207$res{'hash'} =$3;3208if($opts{'-z'}) {3209$res{'name'} =$4;3210}else{3211$res{'name'} = unquote($4);3212}3213}32143215returnwantarray?%res: \%res;3216}32173218# generates _two_ hashes, references to which are passed as 2 and 3 argument3219sub parse_from_to_diffinfo {3220my($diffinfo,$from,$to,@parents) =@_;32213222if($diffinfo->{'nparents'}) {3223# combined diff3224$from->{'file'} = [];3225$from->{'href'} = [];3226 fill_from_file_info($diffinfo,@parents)3227unlessexists$diffinfo->{'from_file'};3228for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {3229$from->{'file'}[$i] =3230defined$diffinfo->{'from_file'}[$i] ?3231$diffinfo->{'from_file'}[$i] :3232$diffinfo->{'to_file'};3233if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file3234$from->{'href'}[$i] = href(action=>"blob",3235 hash_base=>$parents[$i],3236 hash=>$diffinfo->{'from_id'}[$i],3237 file_name=>$from->{'file'}[$i]);3238}else{3239$from->{'href'}[$i] =undef;3240}3241}3242}else{3243# ordinary (not combined) diff3244$from->{'file'} =$diffinfo->{'from_file'};3245if($diffinfo->{'status'}ne"A") {# not new (added) file3246$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,3247 hash=>$diffinfo->{'from_id'},3248 file_name=>$from->{'file'});3249}else{3250delete$from->{'href'};3251}3252}32533254$to->{'file'} =$diffinfo->{'to_file'};3255if(!is_deleted($diffinfo)) {# file exists in result3256$to->{'href'} = href(action=>"blob", hash_base=>$hash,3257 hash=>$diffinfo->{'to_id'},3258 file_name=>$to->{'file'});3259}else{3260delete$to->{'href'};3261}3262}32633264## ......................................................................3265## parse to array of hashes functions32663267sub git_get_heads_list {3268my($limit,@classes) =@_;3269@classes= ('heads')unless@classes;3270my@patterns=map{"refs/$_"}@classes;3271my@headslist;32723273open my$fd,'-|', git_cmd(),'for-each-ref',3274($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',3275'--format=%(objectname) %(refname) %(subject)%00%(committer)',3276@patterns3277orreturn;3278while(my$line= <$fd>) {3279my%ref_item;32803281chomp$line;3282my($refinfo,$committerinfo) =split(/\0/,$line);3283my($hash,$name,$title) =split(' ',$refinfo,3);3284my($committer,$epoch,$tz) =3285($committerinfo=~/^(.*) ([0-9]+) (.*)$/);3286$ref_item{'fullname'} =$name;3287$name=~s!^refs/(?:head|remote)s/!!;32883289$ref_item{'name'} =$name;3290$ref_item{'id'} =$hash;3291$ref_item{'title'} =$title||'(no commit message)';3292$ref_item{'epoch'} =$epoch;3293if($epoch) {3294$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3295}else{3296$ref_item{'age'} ="unknown";3297}32983299push@headslist, \%ref_item;3300}3301close$fd;33023303returnwantarray?@headslist: \@headslist;3304}33053306sub git_get_tags_list {3307my$limit=shift;3308my@tagslist;33093310open my$fd,'-|', git_cmd(),'for-each-ref',3311($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',3312'--format=%(objectname) %(objecttype) %(refname) '.3313'%(*objectname) %(*objecttype) %(subject)%00%(creator)',3314'refs/tags'3315orreturn;3316while(my$line= <$fd>) {3317my%ref_item;33183319chomp$line;3320my($refinfo,$creatorinfo) =split(/\0/,$line);3321my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);3322my($creator,$epoch,$tz) =3323($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);3324$ref_item{'fullname'} =$name;3325$name=~s!^refs/tags/!!;33263327$ref_item{'type'} =$type;3328$ref_item{'id'} =$id;3329$ref_item{'name'} =$name;3330if($typeeq"tag") {3331$ref_item{'subject'} =$title;3332$ref_item{'reftype'} =$reftype;3333$ref_item{'refid'} =$refid;3334}else{3335$ref_item{'reftype'} =$type;3336$ref_item{'refid'} =$id;3337}33383339if($typeeq"tag"||$typeeq"commit") {3340$ref_item{'epoch'} =$epoch;3341if($epoch) {3342$ref_item{'age'} = age_string(time-$ref_item{'epoch'});3343}else{3344$ref_item{'age'} ="unknown";3345}3346}33473348push@tagslist, \%ref_item;3349}3350close$fd;33513352returnwantarray?@tagslist: \@tagslist;3353}33543355## ----------------------------------------------------------------------3356## filesystem-related functions33573358sub get_file_owner {3359my$path=shift;33603361my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);3362my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);3363if(!defined$gcos) {3364returnundef;3365}3366my$owner=$gcos;3367$owner=~s/[,;].*$//;3368return to_utf8($owner);3369}33703371# assume that file exists3372sub insert_file {3373my$filename=shift;33743375open my$fd,'<',$filename;3376print map{ to_utf8($_) } <$fd>;3377close$fd;3378}33793380## ......................................................................3381## mimetype related functions33823383sub mimetype_guess_file {3384my$filename=shift;3385my$mimemap=shift;3386-r $mimemaporreturnundef;33873388my%mimemap;3389open(my$mh,'<',$mimemap)orreturnundef;3390while(<$mh>) {3391next ifm/^#/;# skip comments3392my($mimetype,$exts) =split(/\t+/);3393if(defined$exts) {3394my@exts=split(/\s+/,$exts);3395foreachmy$ext(@exts) {3396$mimemap{$ext} =$mimetype;3397}3398}3399}3400close($mh);34013402$filename=~/\.([^.]*)$/;3403return$mimemap{$1};3404}34053406sub mimetype_guess {3407my$filename=shift;3408my$mime;3409$filename=~/\./orreturnundef;34103411if($mimetypes_file) {3412my$file=$mimetypes_file;3413if($file!~m!^/!) {# if it is relative path3414# it is relative to project3415$file="$projectroot/$project/$file";3416}3417$mime= mimetype_guess_file($filename,$file);3418}3419$mime||= mimetype_guess_file($filename,'/etc/mime.types');3420return$mime;3421}34223423sub blob_mimetype {3424my$fd=shift;3425my$filename=shift;34263427if($filename) {3428my$mime= mimetype_guess($filename);3429$mimeandreturn$mime;3430}34313432# just in case3433return$default_blob_plain_mimetypeunless$fd;34343435if(-T $fd) {3436return'text/plain';3437}elsif(!$filename) {3438return'application/octet-stream';3439}elsif($filename=~m/\.png$/i) {3440return'image/png';3441}elsif($filename=~m/\.gif$/i) {3442return'image/gif';3443}elsif($filename=~m/\.jpe?g$/i) {3444return'image/jpeg';3445}else{3446return'application/octet-stream';3447}3448}34493450sub blob_contenttype {3451my($fd,$file_name,$type) =@_;34523453$type||= blob_mimetype($fd,$file_name);3454if($typeeq'text/plain'&&defined$default_text_plain_charset) {3455$type.="; charset=$default_text_plain_charset";3456}34573458return$type;3459}34603461# guess file syntax for syntax highlighting; return undef if no highlighting3462# the name of syntax can (in the future) depend on syntax highlighter used3463sub guess_file_syntax {3464my($highlight,$mimetype,$file_name) =@_;3465returnundefunless($highlight&&defined$file_name);3466my$basename= basename($file_name,'.in');3467return$highlight_basename{$basename}3468ifexists$highlight_basename{$basename};34693470$basename=~/\.([^.]*)$/;3471my$ext=$1orreturnundef;3472return$highlight_ext{$ext}3473ifexists$highlight_ext{$ext};34743475returnundef;3476}34773478# run highlighter and return FD of its output,3479# or return original FD if no highlighting3480sub run_highlighter {3481my($fd,$highlight,$syntax) =@_;3482return$fdunless($highlight&&defined$syntax);34833484close$fd;3485open$fd, quote_command(git_cmd(),"cat-file","blob",$hash)." | ".3486 quote_command($highlight_bin).3487" --replace-tabs=8 --fragment --syntax$syntax|"3488or die_error(500,"Couldn't open file or run syntax highlighter");3489return$fd;3490}34913492## ======================================================================3493## functions printing HTML: header, footer, error page34943495sub get_page_title {3496my$title= to_utf8($site_name);34973498return$titleunless(defined$project);3499$title.=" - ". to_utf8($project);35003501return$titleunless(defined$action);3502$title.="/$action";# $action is US-ASCII (7bit ASCII)35033504return$titleunless(defined$file_name);3505$title.=" - ". esc_path($file_name);3506if($actioneq"tree"&&$file_name!~ m|/$|) {3507$title.="/";3508}35093510return$title;3511}35123513sub print_feed_meta {3514if(defined$project) {3515my%href_params= get_feed_info();3516if(!exists$href_params{'-title'}) {3517$href_params{'-title'} ='log';3518}35193520foreachmy$format(qw(RSS Atom)) {3521my$type=lc($format);3522my%link_attr= (3523'-rel'=>'alternate',3524'-title'=> esc_attr("$project-$href_params{'-title'} -$formatfeed"),3525'-type'=>"application/$type+xml"3526);35273528$href_params{'action'} =$type;3529$link_attr{'-href'} = href(%href_params);3530print"<link ".3531"rel=\"$link_attr{'-rel'}\"".3532"title=\"$link_attr{'-title'}\"".3533"href=\"$link_attr{'-href'}\"".3534"type=\"$link_attr{'-type'}\"".3535"/>\n";35363537$href_params{'extra_options'} ='--no-merges';3538$link_attr{'-href'} = href(%href_params);3539$link_attr{'-title'} .=' (no merges)';3540print"<link ".3541"rel=\"$link_attr{'-rel'}\"".3542"title=\"$link_attr{'-title'}\"".3543"href=\"$link_attr{'-href'}\"".3544"type=\"$link_attr{'-type'}\"".3545"/>\n";3546}35473548}else{3549printf('<link rel="alternate" title="%sprojects list" '.3550'href="%s" type="text/plain; charset=utf-8" />'."\n",3551 esc_attr($site_name), href(project=>undef, action=>"project_index"));3552printf('<link rel="alternate" title="%sprojects feeds" '.3553'href="%s" type="text/x-opml" />'."\n",3554 esc_attr($site_name), href(project=>undef, action=>"opml"));3555}3556}35573558sub git_header_html {3559my$status=shift||"200 OK";3560my$expires=shift;3561my%opts=@_;35623563my$title= get_page_title();3564my$content_type;3565# require explicit support from the UA if we are to send the page as3566# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.3567# we have to do this because MSIE sometimes globs '*/*', pretending to3568# support xhtml+xml but choking when it gets what it asked for.3569if(defined$cgi->http('HTTP_ACCEPT') &&3570$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&3571$cgi->Accept('application/xhtml+xml') !=0) {3572$content_type='application/xhtml+xml';3573}else{3574$content_type='text/html';3575}3576print$cgi->header(-type=>$content_type, -charset =>'utf-8',3577-status=>$status, -expires =>$expires)3578unless($opts{'-no_http_header'});3579my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';3580print<<EOF;3581<?xml version="1.0" encoding="utf-8"?>3582<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">3583<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">3584<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->3585<!-- git core binaries version$git_version-->3586<head>3587<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>3588<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>3589<meta name="robots" content="index, nofollow"/>3590<title>$title</title>3591EOF3592# the stylesheet, favicon etc urls won't work correctly with path_info3593# unless we set the appropriate base URL3594if($ENV{'PATH_INFO'}) {3595print"<base href=\"".esc_url($base_url)."\"/>\n";3596}3597# print out each stylesheet that exist, providing backwards capability3598# for those people who defined $stylesheet in a config file3599if(defined$stylesheet) {3600print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3601}else{3602foreachmy$stylesheet(@stylesheets) {3603next unless$stylesheet;3604print'<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";3605}3606}3607 print_feed_meta()3608if($statuseq'200 OK');3609if(defined$favicon) {3610printqq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);3611}36123613print"</head>\n".3614"<body>\n";36153616if(defined$site_header&& -f $site_header) {3617 insert_file($site_header);3618}36193620print"<div class=\"page_header\">\n";3621if(defined$logo) {3622print$cgi->a({-href => esc_url($logo_url),3623-title =>$logo_label},3624$cgi->img({-src => esc_url($logo),3625-width =>72, -height =>27,3626-alt =>"git",3627-class=>"logo"}));3628}3629print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";3630if(defined$project) {3631print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3632if(defined$action) {3633my$action_print=$action;3634if(defined$opts{-action_extra}) {3635$action_print=$cgi->a({-href => href(action=>$action)},3636$action);3637}3638print" /$action_print";3639}3640if(defined$opts{-action_extra}) {3641print" /$opts{-action_extra}";3642}3643print"\n";3644}3645print"</div>\n";36463647my$have_search= gitweb_check_feature('search');3648if(defined$project&&$have_search) {3649if(!defined$searchtext) {3650$searchtext="";3651}3652my$search_hash;3653if(defined$hash_base) {3654$search_hash=$hash_base;3655}elsif(defined$hash) {3656$search_hash=$hash;3657}else{3658$search_hash="HEAD";3659}3660my$action=$my_uri;3661my$use_pathinfo= gitweb_check_feature('pathinfo');3662if($use_pathinfo) {3663$action.="/".esc_url($project);3664}3665print$cgi->startform(-method=>"get", -action =>$action) .3666"<div class=\"search\">\n".3667(!$use_pathinfo&&3668$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3669$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3670$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3671$cgi->popup_menu(-name =>'st', -default=>'commit',3672-values=> ['commit','grep','author','committer','pickaxe']) .3673$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3674" search:\n",3675$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3676"<span title=\"Extended regular expression\">".3677$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3678-checked =>$search_use_regexp) .3679"</span>".3680"</div>".3681$cgi->end_form() ."\n";3682}3683}36843685sub git_footer_html {3686my$feed_class='rss_logo';36873688print"<div class=\"page_footer\">\n";3689if(defined$project) {3690my$descr= git_get_project_description($project);3691if(defined$descr) {3692print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3693}36943695my%href_params= get_feed_info();3696if(!%href_params) {3697$feed_class.=' generic';3698}3699$href_params{'-title'} ||='log';37003701foreachmy$format(qw(RSS Atom)) {3702$href_params{'action'} =lc($format);3703print$cgi->a({-href => href(%href_params),3704-title =>"$href_params{'-title'}$formatfeed",3705-class=>$feed_class},$format)."\n";3706}37073708}else{3709print$cgi->a({-href => href(project=>undef, action=>"opml"),3710-class=>$feed_class},"OPML") ." ";3711print$cgi->a({-href => href(project=>undef, action=>"project_index"),3712-class=>$feed_class},"TXT") ."\n";3713}3714print"</div>\n";# class="page_footer"37153716if(defined$t0&& gitweb_check_feature('timed')) {3717print"<div id=\"generating_info\">\n";3718print'This page took '.3719'<span id="generating_time" class="time_span">'.3720 tv_interval($t0, [ gettimeofday() ]).3721' seconds </span>'.3722' and '.3723'<span id="generating_cmd">'.3724$number_of_git_cmds.3725'</span> git commands '.3726" to generate.\n";3727print"</div>\n";# class="page_footer"3728}37293730if(defined$site_footer&& -f $site_footer) {3731 insert_file($site_footer);3732}37333734print qq!<script type="text/javascript" src="!.esc_url($javascript).qq!"></script>\n!;3735if(defined$action&&3736$actioneq'blame_incremental') {3737print qq!<script type="text/javascript">\n!.3738 qq!startBlame("!. href(action=>"blame_data", -replay=>1) .qq!",\n!.3739 qq!"!. href() .qq!");\n!.3740 qq!</script>\n!;3741}elsif(gitweb_check_feature('javascript-actions')) {3742print qq!<script type="text/javascript">\n!.3743 qq!window.onload = fixLinks;\n!.3744 qq!</script>\n!;3745}37463747print"</body>\n".3748"</html>";3749}37503751# die_error(<http_status_code>, <error_message>[, <detailed_html_description>])3752# Example: die_error(404, 'Hash not found')3753# By convention, use the following status codes (as defined in RFC 2616):3754# 400: Invalid or missing CGI parameters, or3755# requested object exists but has wrong type.3756# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3757# this server or project.3758# 404: Requested object/revision/project doesn't exist.3759# 500: The server isn't configured properly, or3760# an internal error occurred (e.g. failed assertions caused by bugs), or3761# an unknown error occurred (e.g. the git binary died unexpectedly).3762# 503: The server is currently unavailable (because it is overloaded,3763# or down for maintenance). Generally, this is a temporary state.3764sub die_error {3765my$status=shift||500;3766my$error= esc_html(shift) ||"Internal Server Error";3767my$extra=shift;3768my%opts=@_;37693770my%http_responses= (3771400=>'400 Bad Request',3772403=>'403 Forbidden',3773404=>'404 Not Found',3774500=>'500 Internal Server Error',3775503=>'503 Service Unavailable',3776);3777 git_header_html($http_responses{$status},undef,%opts);3778print<<EOF;3779<div class="page_body">3780<br /><br />3781$status-$error3782<br />3783EOF3784if(defined$extra) {3785print"<hr />\n".3786"$extra\n";3787}3788print"</div>\n";37893790 git_footer_html();3791goto DONE_GITWEB3792unless($opts{'-error_handler'});3793}37943795## ----------------------------------------------------------------------3796## functions printing or outputting HTML: navigation37973798sub git_print_page_nav {3799my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3800$extra=''if!defined$extra;# pager or formats38013802my@navs=qw(summary shortlog log commit commitdiff tree);3803if($suppress) {3804@navs=grep{$_ne$suppress}@navs;3805}38063807my%arg=map{$_=> {action=>$_} }@navs;3808if(defined$head) {3809for(qw(commit commitdiff)) {3810$arg{$_}{'hash'} =$head;3811}3812if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3813for(qw(shortlog log)) {3814$arg{$_}{'hash'} =$head;3815}3816}3817}38183819$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3820$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;38213822my@actions= gitweb_get_feature('actions');3823my%repl= (3824'%'=>'%',3825'n'=>$project,# project name3826'f'=>$git_dir,# project path within filesystem3827'h'=>$treehead||'',# current hash ('h' parameter)3828'b'=>$treebase||'',# hash base ('hb' parameter)3829);3830while(@actions) {3831my($label,$link,$pos) =splice(@actions,0,3);3832# insert3833@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3834# munch munch3835$link=~s/%([%nfhb])/$repl{$1}/g;3836$arg{$label}{'_href'} =$link;3837}38383839print"<div class=\"page_nav\">\n".3840(join" | ",3841map{$_eq$current?3842$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3843}@navs);3844print"<br/>\n$extra<br/>\n".3845"</div>\n";3846}38473848# returns a submenu for the nagivation of the refs views (tags, heads,3849# remotes) with the current view disabled and the remotes view only3850# available if the feature is enabled3851sub format_ref_views {3852my($current) =@_;3853my@ref_views=qw{tags heads};3854push@ref_views,'remotes'if gitweb_check_feature('remote_heads');3855returnjoin" | ",map{3856$_eq$current?$_:3857$cgi->a({-href => href(action=>$_)},$_)3858}@ref_views3859}38603861sub format_paging_nav {3862my($action,$page,$has_next_link) =@_;3863my$paging_nav;386438653866if($page>0) {3867$paging_nav.=3868$cgi->a({-href => href(-replay=>1, page=>undef)},"first") .3869" ⋅ ".3870$cgi->a({-href => href(-replay=>1, page=>$page-1),3871-accesskey =>"p", -title =>"Alt-p"},"prev");3872}else{3873$paging_nav.="first ⋅ prev";3874}38753876if($has_next_link) {3877$paging_nav.=" ⋅ ".3878$cgi->a({-href => href(-replay=>1, page=>$page+1),3879-accesskey =>"n", -title =>"Alt-n"},"next");3880}else{3881$paging_nav.=" ⋅ next";3882}38833884return$paging_nav;3885}38863887## ......................................................................3888## functions printing or outputting HTML: div38893890sub git_print_header_div {3891my($action,$title,$hash,$hash_base) =@_;3892my%args= ();38933894$args{'action'} =$action;3895$args{'hash'} =$hashif$hash;3896$args{'hash_base'} =$hash_baseif$hash_base;38973898print"<div class=\"header\">\n".3899$cgi->a({-href => href(%args), -class=>"title"},3900$title?$title:$action) .3901"\n</div>\n";3902}39033904sub format_repo_url {3905my($name,$url) =@_;3906return"<tr class=\"metadata_url\"><td>$name</td><td>$url</td></tr>\n";3907}39083909# Group output by placing it in a DIV element and adding a header.3910# Options for start_div() can be provided by passing a hash reference as the3911# first parameter to the function.3912# Options to git_print_header_div() can be provided by passing an array3913# reference. This must follow the options to start_div if they are present.3914# The content can be a scalar, which is output as-is, a scalar reference, which3915# is output after html escaping, an IO handle passed either as *handle or3916# *handle{IO}, or a function reference. In the latter case all following3917# parameters will be taken as argument to the content function call.3918sub git_print_section {3919my($div_args,$header_args,$content);3920my$arg=shift;3921if(ref($arg)eq'HASH') {3922$div_args=$arg;3923$arg=shift;3924}3925if(ref($arg)eq'ARRAY') {3926$header_args=$arg;3927$arg=shift;3928}3929$content=$arg;39303931print$cgi->start_div($div_args);3932 git_print_header_div(@$header_args);39333934if(ref($content)eq'CODE') {3935$content->(@_);3936}elsif(ref($content)eq'SCALAR') {3937print esc_html($$content);3938}elsif(ref($content)eq'GLOB'or ref($content)eq'IO::Handle') {3939print<$content>;3940}elsif(!ref($content) &&defined($content)) {3941print$content;3942}39433944print$cgi->end_div;3945}39463947sub print_local_time {3948print format_local_time(@_);3949}39503951sub format_local_time {3952my$localtime='';3953my%date=@_;3954if($date{'hour_local'} <6) {3955$localtime.=sprintf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3956$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3957}else{3958$localtime.=sprintf(" (%02d:%02d%s)",3959$date{'hour_local'},$date{'minute_local'},$date{'tz_local'});3960}39613962return$localtime;3963}39643965# Outputs the author name and date in long form3966sub git_print_authorship {3967my$co=shift;3968my%opts=@_;3969my$tag=$opts{-tag} ||'div';3970my$author=$co->{'author_name'};39713972my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3973print"<$tagclass=\"author_date\">".3974 format_search_author($author,"author", esc_html($author)) .3975" [$ad{'rfc2822'}";3976 print_local_time(%ad)if($opts{-localtime});3977print"]". git_get_avatar($co->{'author_email'}, -pad_before =>1)3978."</$tag>\n";3979}39803981# Outputs table rows containing the full author or committer information,3982# in the format expected for 'commit' view (& similar).3983# Parameters are a commit hash reference, followed by the list of people3984# to output information for. If the list is empty it defaults to both3985# author and committer.3986sub git_print_authorship_rows {3987my$co=shift;3988# too bad we can't use @people = @_ || ('author', 'committer')3989my@people=@_;3990@people= ('author','committer')unless@people;3991foreachmy$who(@people) {3992my%wd= parse_date($co->{"${who}_epoch"},$co->{"${who}_tz"});3993print"<tr><td>$who</td><td>".3994 format_search_author($co->{"${who}_name"},$who,3995 esc_html($co->{"${who}_name"})) ." ".3996 format_search_author($co->{"${who}_email"},$who,3997 esc_html("<".$co->{"${who}_email"} .">")) .3998"</td><td rowspan=\"2\">".3999 git_get_avatar($co->{"${who}_email"}, -size =>'double') .4000"</td></tr>\n".4001"<tr>".4002"<td></td><td>$wd{'rfc2822'}";4003 print_local_time(%wd);4004print"</td>".4005"</tr>\n";4006}4007}40084009sub git_print_page_path {4010my$name=shift;4011my$type=shift;4012my$hb=shift;401340144015print"<div class=\"page_path\">";4016print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),4017-title =>'tree root'}, to_utf8("[$project]"));4018print" / ";4019if(defined$name) {4020my@dirname=split'/',$name;4021my$basename=pop@dirname;4022my$fullname='';40234024foreachmy$dir(@dirname) {4025$fullname.= ($fullname?'/':'') .$dir;4026print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,4027 hash_base=>$hb),4028-title =>$fullname}, esc_path($dir));4029print" / ";4030}4031if(defined$type&&$typeeq'blob') {4032print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,4033 hash_base=>$hb),4034-title =>$name}, esc_path($basename));4035}elsif(defined$type&&$typeeq'tree') {4036print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,4037 hash_base=>$hb),4038-title =>$name}, esc_path($basename));4039print" / ";4040}else{4041print esc_path($basename);4042}4043}4044print"<br/></div>\n";4045}40464047sub git_print_log {4048my$log=shift;4049my%opts=@_;40504051if($opts{'-remove_title'}) {4052# remove title, i.e. first line of log4053shift@$log;4054}4055# remove leading empty lines4056while(defined$log->[0] &&$log->[0]eq"") {4057shift@$log;4058}40594060# print log4061my$signoff=0;4062my$empty=0;4063foreachmy$line(@$log) {4064if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {4065$signoff=1;4066$empty=0;4067if(!$opts{'-remove_signoff'}) {4068print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";4069next;4070}else{4071# remove signoff lines4072next;4073}4074}else{4075$signoff=0;4076}40774078# print only one empty line4079# do not print empty line after signoff4080if($lineeq"") {4081next if($empty||$signoff);4082$empty=1;4083}else{4084$empty=0;4085}40864087print format_log_line_html($line) ."<br/>\n";4088}40894090if($opts{'-final_empty_line'}) {4091# end with single empty line4092print"<br/>\n"unless$empty;4093}4094}40954096# return link target (what link points to)4097sub git_get_link_target {4098my$hash=shift;4099my$link_target;41004101# read link4102open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4103orreturn;4104{4105local$/=undef;4106$link_target= <$fd>;4107}4108close$fd4109orreturn;41104111return$link_target;4112}41134114# given link target, and the directory (basedir) the link is in,4115# return target of link relative to top directory (top tree);4116# return undef if it is not possible (including absolute links).4117sub normalize_link_target {4118my($link_target,$basedir) =@_;41194120# absolute symlinks (beginning with '/') cannot be normalized4121return if(substr($link_target,0,1)eq'/');41224123# normalize link target to path from top (root) tree (dir)4124my$path;4125if($basedir) {4126$path=$basedir.'/'.$link_target;4127}else{4128# we are in top (root) tree (dir)4129$path=$link_target;4130}41314132# remove //, /./, and /../4133my@path_parts;4134foreachmy$part(split('/',$path)) {4135# discard '.' and ''4136next if(!$part||$parteq'.');4137# handle '..'4138if($parteq'..') {4139if(@path_parts) {4140pop@path_parts;4141}else{4142# link leads outside repository (outside top dir)4143return;4144}4145}else{4146push@path_parts,$part;4147}4148}4149$path=join('/',@path_parts);41504151return$path;4152}41534154# print tree entry (row of git_tree), but without encompassing <tr> element4155sub git_print_tree_entry {4156my($t,$basedir,$hash_base,$have_blame) =@_;41574158my%base_key= ();4159$base_key{'hash_base'} =$hash_baseifdefined$hash_base;41604161# The format of a table row is: mode list link. Where mode is4162# the mode of the entry, list is the name of the entry, an href,4163# and link is the action links of the entry.41644165print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";4166if(exists$t->{'size'}) {4167print"<td class=\"size\">$t->{'size'}</td>\n";4168}4169if($t->{'type'}eq"blob") {4170print"<td class=\"list\">".4171$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4172 file_name=>"$basedir$t->{'name'}",%base_key),4173-class=>"list"}, esc_path($t->{'name'}));4174if(S_ISLNK(oct$t->{'mode'})) {4175my$link_target= git_get_link_target($t->{'hash'});4176if($link_target) {4177my$norm_target= normalize_link_target($link_target,$basedir);4178if(defined$norm_target) {4179print" -> ".4180$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,4181 file_name=>$norm_target),4182-title =>$norm_target}, esc_path($link_target));4183}else{4184print" -> ". esc_path($link_target);4185}4186}4187}4188print"</td>\n";4189print"<td class=\"link\">";4190print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},4191 file_name=>"$basedir$t->{'name'}",%base_key)},4192"blob");4193if($have_blame) {4194print" | ".4195$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},4196 file_name=>"$basedir$t->{'name'}",%base_key)},4197"blame");4198}4199if(defined$hash_base) {4200print" | ".4201$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4202 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},4203"history");4204}4205print" | ".4206$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,4207 file_name=>"$basedir$t->{'name'}")},4208"raw");4209print"</td>\n";42104211}elsif($t->{'type'}eq"tree") {4212print"<td class=\"list\">";4213print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4214 file_name=>"$basedir$t->{'name'}",4215%base_key)},4216 esc_path($t->{'name'}));4217print"</td>\n";4218print"<td class=\"link\">";4219print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},4220 file_name=>"$basedir$t->{'name'}",4221%base_key)},4222"tree");4223if(defined$hash_base) {4224print" | ".4225$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,4226 file_name=>"$basedir$t->{'name'}")},4227"history");4228}4229print"</td>\n";4230}else{4231# unknown object: we can only present history for it4232# (this includes 'commit' object, i.e. submodule support)4233print"<td class=\"list\">".4234 esc_path($t->{'name'}) .4235"</td>\n";4236print"<td class=\"link\">";4237if(defined$hash_base) {4238print$cgi->a({-href => href(action=>"history",4239 hash_base=>$hash_base,4240 file_name=>"$basedir$t->{'name'}")},4241"history");4242}4243print"</td>\n";4244}4245}42464247## ......................................................................4248## functions printing large fragments of HTML42494250# get pre-image filenames for merge (combined) diff4251sub fill_from_file_info {4252my($diff,@parents) =@_;42534254$diff->{'from_file'} = [ ];4255$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;4256for(my$i=0;$i<$diff->{'nparents'};$i++) {4257if($diff->{'status'}[$i]eq'R'||4258$diff->{'status'}[$i]eq'C') {4259$diff->{'from_file'}[$i] =4260 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);4261}4262}42634264return$diff;4265}42664267# is current raw difftree line of file deletion4268sub is_deleted {4269my$diffinfo=shift;42704271return$diffinfo->{'to_id'}eq('0' x 40);4272}42734274# does patch correspond to [previous] difftree raw line4275# $diffinfo - hashref of parsed raw diff format4276# $patchinfo - hashref of parsed patch diff format4277# (the same keys as in $diffinfo)4278sub is_patch_split {4279my($diffinfo,$patchinfo) =@_;42804281returndefined$diffinfo&&defined$patchinfo4282&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};4283}428442854286sub git_difftree_body {4287my($difftree,$hash,@parents) =@_;4288my($parent) =$parents[0];4289my$have_blame= gitweb_check_feature('blame');4290print"<div class=\"list_head\">\n";4291if($#{$difftree} >10) {4292print(($#{$difftree} +1) ." files changed:\n");4293}4294print"</div>\n";42954296print"<table class=\"".4297(@parents>1?"combined ":"") .4298"diff_tree\">\n";42994300# header only for combined diff in 'commitdiff' view4301my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';4302if($has_header) {4303# table header4304print"<thead><tr>\n".4305"<th></th><th></th>\n";# filename, patchN link4306for(my$i=0;$i<@parents;$i++) {4307my$par=$parents[$i];4308print"<th>".4309$cgi->a({-href => href(action=>"commitdiff",4310 hash=>$hash, hash_parent=>$par),4311-title =>'commitdiff to parent number '.4312($i+1) .': '.substr($par,0,7)},4313$i+1) .4314" </th>\n";4315}4316print"</tr></thead>\n<tbody>\n";4317}43184319my$alternate=1;4320my$patchno=0;4321foreachmy$line(@{$difftree}) {4322my$diff= parsed_difftree_line($line);43234324if($alternate) {4325print"<tr class=\"dark\">\n";4326}else{4327print"<tr class=\"light\">\n";4328}4329$alternate^=1;43304331if(exists$diff->{'nparents'}) {# combined diff43324333 fill_from_file_info($diff,@parents)4334unlessexists$diff->{'from_file'};43354336if(!is_deleted($diff)) {4337# file exists in the result (child) commit4338print"<td>".4339$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4340 file_name=>$diff->{'to_file'},4341 hash_base=>$hash),4342-class=>"list"}, esc_path($diff->{'to_file'})) .4343"</td>\n";4344}else{4345print"<td>".4346 esc_path($diff->{'to_file'}) .4347"</td>\n";4348}43494350if($actioneq'commitdiff') {4351# link to patch4352$patchno++;4353print"<td class=\"link\">".4354$cgi->a({-href => href(-anchor=>"patch$patchno")},4355"patch") .4356" | ".4357"</td>\n";4358}43594360my$has_history=0;4361my$not_deleted=0;4362for(my$i=0;$i<$diff->{'nparents'};$i++) {4363my$hash_parent=$parents[$i];4364my$from_hash=$diff->{'from_id'}[$i];4365my$from_path=$diff->{'from_file'}[$i];4366my$status=$diff->{'status'}[$i];43674368$has_history||= ($statusne'A');4369$not_deleted||= ($statusne'D');43704371if($statuseq'A') {4372print"<td class=\"link\"align=\"right\"> | </td>\n";4373}elsif($statuseq'D') {4374print"<td class=\"link\">".4375$cgi->a({-href => href(action=>"blob",4376 hash_base=>$hash,4377 hash=>$from_hash,4378 file_name=>$from_path)},4379"blob". ($i+1)) .4380" | </td>\n";4381}else{4382if($diff->{'to_id'}eq$from_hash) {4383print"<td class=\"link nochange\">";4384}else{4385print"<td class=\"link\">";4386}4387print$cgi->a({-href => href(action=>"blobdiff",4388 hash=>$diff->{'to_id'},4389 hash_parent=>$from_hash,4390 hash_base=>$hash,4391 hash_parent_base=>$hash_parent,4392 file_name=>$diff->{'to_file'},4393 file_parent=>$from_path)},4394"diff". ($i+1)) .4395" | </td>\n";4396}4397}43984399print"<td class=\"link\">";4400if($not_deleted) {4401print$cgi->a({-href => href(action=>"blob",4402 hash=>$diff->{'to_id'},4403 file_name=>$diff->{'to_file'},4404 hash_base=>$hash)},4405"blob");4406print" | "if($has_history);4407}4408if($has_history) {4409print$cgi->a({-href => href(action=>"history",4410 file_name=>$diff->{'to_file'},4411 hash_base=>$hash)},4412"history");4413}4414print"</td>\n";44154416print"</tr>\n";4417next;# instead of 'else' clause, to avoid extra indent4418}4419# else ordinary diff44204421my($to_mode_oct,$to_mode_str,$to_file_type);4422my($from_mode_oct,$from_mode_str,$from_file_type);4423if($diff->{'to_mode'}ne('0' x 6)) {4424$to_mode_oct=oct$diff->{'to_mode'};4425if(S_ISREG($to_mode_oct)) {# only for regular file4426$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits4427}4428$to_file_type= file_type($diff->{'to_mode'});4429}4430if($diff->{'from_mode'}ne('0' x 6)) {4431$from_mode_oct=oct$diff->{'from_mode'};4432if(S_ISREG($from_mode_oct)) {# only for regular file4433$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits4434}4435$from_file_type= file_type($diff->{'from_mode'});4436}44374438if($diff->{'status'}eq"A") {# created4439my$mode_chng="<span class=\"file_status new\">[new$to_file_type";4440$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;4441$mode_chng.="]</span>";4442print"<td>";4443print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4444 hash_base=>$hash, file_name=>$diff->{'file'}),4445-class=>"list"}, esc_path($diff->{'file'}));4446print"</td>\n";4447print"<td>$mode_chng</td>\n";4448print"<td class=\"link\">";4449if($actioneq'commitdiff') {4450# link to patch4451$patchno++;4452print$cgi->a({-href => href(-anchor=>"patch$patchno")},4453"patch") .4454" | ";4455}4456print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4457 hash_base=>$hash, file_name=>$diff->{'file'})},4458"blob");4459print"</td>\n";44604461}elsif($diff->{'status'}eq"D") {# deleted4462my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";4463print"<td>";4464print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4465 hash_base=>$parent, file_name=>$diff->{'file'}),4466-class=>"list"}, esc_path($diff->{'file'}));4467print"</td>\n";4468print"<td>$mode_chng</td>\n";4469print"<td class=\"link\">";4470if($actioneq'commitdiff') {4471# link to patch4472$patchno++;4473print$cgi->a({-href => href(-anchor=>"patch$patchno")},4474"patch") .4475" | ";4476}4477print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},4478 hash_base=>$parent, file_name=>$diff->{'file'})},4479"blob") ." | ";4480if($have_blame) {4481print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,4482 file_name=>$diff->{'file'})},4483"blame") ." | ";4484}4485print$cgi->a({-href => href(action=>"history", hash_base=>$parent,4486 file_name=>$diff->{'file'})},4487"history");4488print"</td>\n";44894490}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed4491my$mode_chnge="";4492if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4493$mode_chnge="<span class=\"file_status mode_chnge\">[changed";4494if($from_file_typene$to_file_type) {4495$mode_chnge.=" from$from_file_typeto$to_file_type";4496}4497if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {4498if($from_mode_str&&$to_mode_str) {4499$mode_chnge.=" mode:$from_mode_str->$to_mode_str";4500}elsif($to_mode_str) {4501$mode_chnge.=" mode:$to_mode_str";4502}4503}4504$mode_chnge.="]</span>\n";4505}4506print"<td>";4507print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4508 hash_base=>$hash, file_name=>$diff->{'file'}),4509-class=>"list"}, esc_path($diff->{'file'}));4510print"</td>\n";4511print"<td>$mode_chnge</td>\n";4512print"<td class=\"link\">";4513if($actioneq'commitdiff') {4514# link to patch4515$patchno++;4516print$cgi->a({-href => href(-anchor=>"patch$patchno")},4517"patch") .4518" | ";4519}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4520# "commit" view and modified file (not onlu mode changed)4521print$cgi->a({-href => href(action=>"blobdiff",4522 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4523 hash_base=>$hash, hash_parent_base=>$parent,4524 file_name=>$diff->{'file'})},4525"diff") .4526" | ";4527}4528print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4529 hash_base=>$hash, file_name=>$diff->{'file'})},4530"blob") ." | ";4531if($have_blame) {4532print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4533 file_name=>$diff->{'file'})},4534"blame") ." | ";4535}4536print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4537 file_name=>$diff->{'file'})},4538"history");4539print"</td>\n";45404541}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied4542my%status_name= ('R'=>'moved','C'=>'copied');4543my$nstatus=$status_name{$diff->{'status'}};4544my$mode_chng="";4545if($diff->{'from_mode'} !=$diff->{'to_mode'}) {4546# mode also for directories, so we cannot use $to_mode_str4547$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);4548}4549print"<td>".4550$cgi->a({-href => href(action=>"blob", hash_base=>$hash,4551 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),4552-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".4553"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".4554$cgi->a({-href => href(action=>"blob", hash_base=>$parent,4555 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),4556-class=>"list"}, esc_path($diff->{'from_file'})) .4557" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".4558"<td class=\"link\">";4559if($actioneq'commitdiff') {4560# link to patch4561$patchno++;4562print$cgi->a({-href => href(-anchor=>"patch$patchno")},4563"patch") .4564" | ";4565}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {4566# "commit" view and modified file (not only pure rename or copy)4567print$cgi->a({-href => href(action=>"blobdiff",4568 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},4569 hash_base=>$hash, hash_parent_base=>$parent,4570 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},4571"diff") .4572" | ";4573}4574print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},4575 hash_base=>$parent, file_name=>$diff->{'to_file'})},4576"blob") ." | ";4577if($have_blame) {4578print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,4579 file_name=>$diff->{'to_file'})},4580"blame") ." | ";4581}4582print$cgi->a({-href => href(action=>"history", hash_base=>$hash,4583 file_name=>$diff->{'to_file'})},4584"history");4585print"</td>\n";45864587}# we should not encounter Unmerged (U) or Unknown (X) status4588print"</tr>\n";4589}4590print"</tbody>"if$has_header;4591print"</table>\n";4592}45934594sub git_patchset_body {4595my($fd,$difftree,$hash,@hash_parents) =@_;4596my($hash_parent) =$hash_parents[0];45974598my$is_combined= (@hash_parents>1);4599my$patch_idx=0;4600my$patch_number=0;4601my$patch_line;4602my$diffinfo;4603my$to_name;4604my(%from,%to);46054606print"<div class=\"patchset\">\n";46074608# skip to first patch4609while($patch_line= <$fd>) {4610chomp$patch_line;46114612last if($patch_line=~m/^diff /);4613}46144615 PATCH:4616while($patch_line) {46174618# parse "git diff" header line4619if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {4620# $1 is from_name, which we do not use4621$to_name= unquote($2);4622$to_name=~s!^b/!!;4623}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {4624# $1 is 'cc' or 'combined', which we do not use4625$to_name= unquote($2);4626}else{4627$to_name=undef;4628}46294630# check if current patch belong to current raw line4631# and parse raw git-diff line if needed4632if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {4633# this is continuation of a split patch4634print"<div class=\"patch cont\">\n";4635}else{4636# advance raw git-diff output if needed4637$patch_idx++ifdefined$diffinfo;46384639# read and prepare patch information4640$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);46414642# compact combined diff output can have some patches skipped4643# find which patch (using pathname of result) we are at now;4644if($is_combined) {4645while($to_namene$diffinfo->{'to_file'}) {4646print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4647 format_diff_cc_simplified($diffinfo,@hash_parents) .4648"</div>\n";# class="patch"46494650$patch_idx++;4651$patch_number++;46524653last if$patch_idx>$#$difftree;4654$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);4655}4656}46574658# modifies %from, %to hashes4659 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);46604661# this is first patch for raw difftree line with $patch_idx index4662# we index @$difftree array from 0, but number patches from 14663print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";4664}46654666# git diff header4667#assert($patch_line =~ m/^diff /) if DEBUG;4668#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed4669$patch_number++;4670# print "git diff" header4671print format_git_diff_header_line($patch_line,$diffinfo,4672 \%from, \%to);46734674# print extended diff header4675print"<div class=\"diff extended_header\">\n";4676 EXTENDED_HEADER:4677while($patch_line= <$fd>) {4678chomp$patch_line;46794680last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);46814682print format_extended_diff_header_line($patch_line,$diffinfo,4683 \%from, \%to);4684}4685print"</div>\n";# class="diff extended_header"46864687# from-file/to-file diff header4688if(!$patch_line) {4689print"</div>\n";# class="patch"4690last PATCH;4691}4692next PATCH if($patch_line=~m/^diff /);4693#assert($patch_line =~ m/^---/) if DEBUG;46944695my$last_patch_line=$patch_line;4696$patch_line= <$fd>;4697chomp$patch_line;4698#assert($patch_line =~ m/^\+\+\+/) if DEBUG;46994700print format_diff_from_to_header($last_patch_line,$patch_line,4701$diffinfo, \%from, \%to,4702@hash_parents);47034704# the patch itself4705 LINE:4706while($patch_line= <$fd>) {4707chomp$patch_line;47084709next PATCH if($patch_line=~m/^diff /);47104711print format_diff_line($patch_line, \%from, \%to);4712}47134714}continue{4715print"</div>\n";# class="patch"4716}47174718# for compact combined (--cc) format, with chunk and patch simplification4719# the patchset might be empty, but there might be unprocessed raw lines4720for(++$patch_idxif$patch_number>0;4721$patch_idx<@$difftree;4722++$patch_idx) {4723# read and prepare patch information4724$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);47254726# generate anchor for "patch" links in difftree / whatchanged part4727print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".4728 format_diff_cc_simplified($diffinfo,@hash_parents) .4729"</div>\n";# class="patch"47304731$patch_number++;4732}47334734if($patch_number==0) {4735if(@hash_parents>1) {4736print"<div class=\"diff nodifferences\">Trivial merge</div>\n";4737}else{4738print"<div class=\"diff nodifferences\">No differences found</div>\n";4739}4740}47414742print"</div>\n";# class="patchset"4743}47444745# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47464747# fills project list info (age, description, owner, forks) for each4748# project in the list, removing invalid projects from returned list4749# NOTE: modifies $projlist, but does not remove entries from it4750sub fill_project_list_info {4751my($projlist,$check_forks) =@_;4752my@projects;47534754my$show_ctags= gitweb_check_feature('ctags');4755 PROJECT:4756foreachmy$pr(@$projlist) {4757my(@activity) = git_get_last_activity($pr->{'path'});4758unless(@activity) {4759next PROJECT;4760}4761($pr->{'age'},$pr->{'age_string'}) =@activity;4762if(!defined$pr->{'descr'}) {4763my$descr= git_get_project_description($pr->{'path'}) ||"";4764$descr= to_utf8($descr);4765$pr->{'descr_long'} =$descr;4766$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);4767}4768if(!defined$pr->{'owner'}) {4769$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";4770}4771if($check_forks) {4772my$pname=$pr->{'path'};4773if(($pname=~s/\.git$//) &&4774($pname!~/\/$/) &&4775(-d "$projectroot/$pname")) {4776$pr->{'forks'} ="-d$projectroot/$pname";4777}else{4778$pr->{'forks'} =0;4779}4780}4781$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4782push@projects,$pr;4783}47844785return@projects;4786}47874788# print 'sort by' <th> element, generating 'sort by $name' replay link4789# if that order is not selected4790sub print_sort_th {4791print format_sort_th(@_);4792}47934794sub format_sort_th {4795my($name,$order,$header) =@_;4796my$sort_th="";4797$header||=ucfirst($name);47984799if($ordereq$name) {4800$sort_th.="<th>$header</th>\n";4801}else{4802$sort_th.="<th>".4803$cgi->a({-href => href(-replay=>1, order=>$name),4804-class=>"header"},$header) .4805"</th>\n";4806}48074808return$sort_th;4809}48104811sub git_project_list_body {4812# actually uses global variable $project4813my($projlist,$order,$from,$to,$extra,$no_header) =@_;48144815my$check_forks= gitweb_check_feature('forks');4816my@projects= fill_project_list_info($projlist,$check_forks);48174818$order||=$default_projects_order;4819$from=0unlessdefined$from;4820$to=$#projectsif(!defined$to||$#projects<$to);48214822my%order_info= (4823 project => { key =>'path', type =>'str'},4824 descr => { key =>'descr_long', type =>'str'},4825 owner => { key =>'owner', type =>'str'},4826 age => { key =>'age', type =>'num'}4827);4828my$oi=$order_info{$order};4829if($oi->{'type'}eq'str') {4830@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4831}else{4832@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4833}48344835my$show_ctags= gitweb_check_feature('ctags');4836if($show_ctags) {4837my%ctags;4838foreachmy$p(@projects) {4839foreachmy$ct(keys%{$p->{'ctags'}}) {4840$ctags{$ct} +=$p->{'ctags'}->{$ct};4841}4842}4843my$cloud= git_populate_project_tagcloud(\%ctags);4844print git_show_project_tagcloud($cloud,64);4845}48464847print"<table class=\"project_list\">\n";4848unless($no_header) {4849print"<tr>\n";4850if($check_forks) {4851print"<th></th>\n";4852}4853 print_sort_th('project',$order,'Project');4854 print_sort_th('descr',$order,'Description');4855 print_sort_th('owner',$order,'Owner');4856 print_sort_th('age',$order,'Last Change');4857print"<th></th>\n".# for links4858"</tr>\n";4859}4860my$alternate=1;4861my$tagfilter=$cgi->param('by_tag');4862for(my$i=$from;$i<=$to;$i++) {4863my$pr=$projects[$i];48644865next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4866next if$searchtextand not$pr->{'path'} =~/$searchtext/4867and not$pr->{'descr_long'} =~/$searchtext/;4868# Weed out forks or non-matching entries of search4869if($check_forks) {4870my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4871$forkbase="^$forkbase"if$forkbase;4872next ifnot$searchtextand not$tagfilterand$show_ctags4873and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4874}48754876if($alternate) {4877print"<tr class=\"dark\">\n";4878}else{4879print"<tr class=\"light\">\n";4880}4881$alternate^=1;4882if($check_forks) {4883print"<td>";4884if($pr->{'forks'}) {4885print"<!--$pr->{'forks'} -->\n";4886print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4887}4888print"</td>\n";4889}4890print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4891-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4892"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4893-class=>"list", -title =>$pr->{'descr_long'}},4894 esc_html($pr->{'descr'})) ."</td>\n".4895"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4896print"<td class=\"". age_class($pr->{'age'}) ."\">".4897(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4898"<td class=\"link\">".4899$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4900$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4901$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4902$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4903($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4904"</td>\n".4905"</tr>\n";4906}4907if(defined$extra) {4908print"<tr>\n";4909if($check_forks) {4910print"<td></td>\n";4911}4912print"<td colspan=\"5\">$extra</td>\n".4913"</tr>\n";4914}4915print"</table>\n";4916}49174918sub git_log_body {4919# uses global variable $project4920my($commitlist,$from,$to,$refs,$extra) =@_;49214922$from=0unlessdefined$from;4923$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);49244925for(my$i=0;$i<=$to;$i++) {4926my%co= %{$commitlist->[$i]};4927next if!%co;4928my$commit=$co{'id'};4929my$ref= format_ref_marker($refs,$commit);4930 git_print_header_div('commit',4931"<span class=\"age\">$co{'age_string'}</span>".4932 esc_html($co{'title'}) .$ref,4933$commit);4934print"<div class=\"title_text\">\n".4935"<div class=\"log_link\">\n".4936$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .4937" | ".4938$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .4939" | ".4940$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .4941"<br/>\n".4942"</div>\n";4943 git_print_authorship(\%co, -tag =>'span');4944print"<br/>\n</div>\n";49454946print"<div class=\"log_body\">\n";4947 git_print_log($co{'comment'}, -final_empty_line=>1);4948print"</div>\n";4949}4950if($extra) {4951print"<div class=\"page_nav\">\n";4952print"$extra\n";4953print"</div>\n";4954}4955}49564957sub git_shortlog_body {4958# uses global variable $project4959my($commitlist,$from,$to,$refs,$extra) =@_;49604961$from=0unlessdefined$from;4962$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);49634964print"<table class=\"shortlog\">\n";4965my$alternate=1;4966for(my$i=$from;$i<=$to;$i++) {4967my%co= %{$commitlist->[$i]};4968my$commit=$co{'id'};4969my$ref= format_ref_marker($refs,$commit);4970if($alternate) {4971print"<tr class=\"dark\">\n";4972}else{4973print"<tr class=\"light\">\n";4974}4975$alternate^=1;4976# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4977print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4978 format_author_html('td', \%co,10) ."<td>";4979print format_subject_html($co{'title'},$co{'title_short'},4980 href(action=>"commit", hash=>$commit),$ref);4981print"</td>\n".4982"<td class=\"link\">".4983$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4984$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4985$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4986my$snapshot_links= format_snapshot_links($commit);4987if(defined$snapshot_links) {4988print" | ".$snapshot_links;4989}4990print"</td>\n".4991"</tr>\n";4992}4993if(defined$extra) {4994print"<tr>\n".4995"<td colspan=\"4\">$extra</td>\n".4996"</tr>\n";4997}4998print"</table>\n";4999}50005001sub git_history_body {5002# Warning: assumes constant type (blob or tree) during history5003my($commitlist,$from,$to,$refs,$extra,5004$file_name,$file_hash,$ftype) =@_;50055006$from=0unlessdefined$from;5007$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});50085009print"<table class=\"history\">\n";5010my$alternate=1;5011for(my$i=$from;$i<=$to;$i++) {5012my%co= %{$commitlist->[$i]};5013if(!%co) {5014next;5015}5016my$commit=$co{'id'};50175018my$ref= format_ref_marker($refs,$commit);50195020if($alternate) {5021print"<tr class=\"dark\">\n";5022}else{5023print"<tr class=\"light\">\n";5024}5025$alternate^=1;5026print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5027# shortlog: format_author_html('td', \%co, 10)5028 format_author_html('td', \%co,15,3) ."<td>";5029# originally git_history used chop_str($co{'title'}, 50)5030print format_subject_html($co{'title'},$co{'title_short'},5031 href(action=>"commit", hash=>$commit),$ref);5032print"</td>\n".5033"<td class=\"link\">".5034$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".5035$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");50365037if($ftypeeq'blob') {5038my$blob_current=$file_hash;5039my$blob_parent= git_get_hash_by_path($commit,$file_name);5040if(defined$blob_current&&defined$blob_parent&&5041$blob_currentne$blob_parent) {5042print" | ".5043$cgi->a({-href => href(action=>"blobdiff",5044 hash=>$blob_current, hash_parent=>$blob_parent,5045 hash_base=>$hash_base, hash_parent_base=>$commit,5046 file_name=>$file_name)},5047"diff to current");5048}5049}5050print"</td>\n".5051"</tr>\n";5052}5053if(defined$extra) {5054print"<tr>\n".5055"<td colspan=\"4\">$extra</td>\n".5056"</tr>\n";5057}5058print"</table>\n";5059}50605061sub git_tags_body {5062# uses global variable $project5063my($taglist,$from,$to,$extra) =@_;5064$from=0unlessdefined$from;5065$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);50665067print"<table class=\"tags\">\n";5068my$alternate=1;5069for(my$i=$from;$i<=$to;$i++) {5070my$entry=$taglist->[$i];5071my%tag=%$entry;5072my$comment=$tag{'subject'};5073my$comment_short;5074if(defined$comment) {5075$comment_short= chop_str($comment,30,5);5076}5077if($alternate) {5078print"<tr class=\"dark\">\n";5079}else{5080print"<tr class=\"light\">\n";5081}5082$alternate^=1;5083if(defined$tag{'age'}) {5084print"<td><i>$tag{'age'}</i></td>\n";5085}else{5086print"<td></td>\n";5087}5088print"<td>".5089$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),5090-class=>"list name"}, esc_html($tag{'name'})) .5091"</td>\n".5092"<td>";5093if(defined$comment) {5094print format_subject_html($comment,$comment_short,5095 href(action=>"tag", hash=>$tag{'id'}));5096}5097print"</td>\n".5098"<td class=\"selflink\">";5099if($tag{'type'}eq"tag") {5100print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");5101}else{5102print" ";5103}5104print"</td>\n".5105"<td class=\"link\">"." | ".5106$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});5107if($tag{'reftype'}eq"commit") {5108print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .5109" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");5110}elsif($tag{'reftype'}eq"blob") {5111print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");5112}5113print"</td>\n".5114"</tr>";5115}5116if(defined$extra) {5117print"<tr>\n".5118"<td colspan=\"5\">$extra</td>\n".5119"</tr>\n";5120}5121print"</table>\n";5122}51235124sub git_heads_body {5125# uses global variable $project5126my($headlist,$head,$from,$to,$extra) =@_;5127$from=0unlessdefined$from;5128$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);51295130print"<table class=\"heads\">\n";5131my$alternate=1;5132for(my$i=$from;$i<=$to;$i++) {5133my$entry=$headlist->[$i];5134my%ref=%$entry;5135my$curr=$ref{'id'}eq$head;5136if($alternate) {5137print"<tr class=\"dark\">\n";5138}else{5139print"<tr class=\"light\">\n";5140}5141$alternate^=1;5142print"<td><i>$ref{'age'}</i></td>\n".5143($curr?"<td class=\"current_head\">":"<td>") .5144$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),5145-class=>"list name"},esc_html($ref{'name'})) .5146"</td>\n".5147"<td class=\"link\">".5148$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".5149$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".5150$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'fullname'})},"tree") .5151"</td>\n".5152"</tr>";5153}5154if(defined$extra) {5155print"<tr>\n".5156"<td colspan=\"3\">$extra</td>\n".5157"</tr>\n";5158}5159print"</table>\n";5160}51615162# Display a single remote block5163sub git_remote_block {5164my($remote,$rdata,$limit,$head) =@_;51655166my$heads=$rdata->{'heads'};5167my$fetch=$rdata->{'fetch'};5168my$push=$rdata->{'push'};51695170my$urls_table="<table class=\"projects_list\">\n";51715172if(defined$fetch) {5173if($fetcheq$push) {5174$urls_table.= format_repo_url("URL",$fetch);5175}else{5176$urls_table.= format_repo_url("Fetch URL",$fetch);5177$urls_table.= format_repo_url("Push URL",$push)ifdefined$push;5178}5179}elsif(defined$push) {5180$urls_table.= format_repo_url("Push URL",$push);5181}else{5182$urls_table.= format_repo_url("","No remote URL");5183}51845185$urls_table.="</table>\n";51865187my$dots;5188if(defined$limit&&$limit<@$heads) {5189$dots=$cgi->a({-href => href(action=>"remotes", hash=>$remote)},"...");5190}51915192print$urls_table;5193 git_heads_body($heads,$head,0,$limit,$dots);5194}51955196# Display a list of remote names with the respective fetch and push URLs5197sub git_remotes_list {5198my($remotedata,$limit) =@_;5199print"<table class=\"heads\">\n";5200my$alternate=1;5201my@remotes=sort keys%$remotedata;52025203my$limited=$limit&&$limit<@remotes;52045205$#remotes=$limit-1if$limited;52065207while(my$remote=shift@remotes) {5208my$rdata=$remotedata->{$remote};5209my$fetch=$rdata->{'fetch'};5210my$push=$rdata->{'push'};5211if($alternate) {5212print"<tr class=\"dark\">\n";5213}else{5214print"<tr class=\"light\">\n";5215}5216$alternate^=1;5217print"<td>".5218$cgi->a({-href=> href(action=>'remotes', hash=>$remote),5219-class=>"list name"},esc_html($remote)) .5220"</td>";5221print"<td class=\"link\">".5222(defined$fetch?$cgi->a({-href=>$fetch},"fetch") :"fetch") .5223" | ".5224(defined$push?$cgi->a({-href=>$push},"push") :"push") .5225"</td>";52265227print"</tr>\n";5228}52295230if($limited) {5231print"<tr>\n".5232"<td colspan=\"3\">".5233$cgi->a({-href => href(action=>"remotes")},"...") .5234"</td>\n"."</tr>\n";5235}52365237print"</table>";5238}52395240# Display remote heads grouped by remote, unless there are too many5241# remotes, in which case we only display the remote names5242sub git_remotes_body {5243my($remotedata,$limit,$head) =@_;5244if($limitand$limit<keys%$remotedata) {5245 git_remotes_list($remotedata,$limit);5246}else{5247 fill_remote_heads($remotedata);5248while(my($remote,$rdata) =each%$remotedata) {5249 git_print_section({-class=>"remote", -id=>$remote},5250["remotes",$remote,$remote],sub{5251 git_remote_block($remote,$rdata,$limit,$head);5252});5253}5254}5255}52565257sub git_search_grep_body {5258my($commitlist,$from,$to,$extra) =@_;5259$from=0unlessdefined$from;5260$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);52615262print"<table class=\"commit_search\">\n";5263my$alternate=1;5264for(my$i=$from;$i<=$to;$i++) {5265my%co= %{$commitlist->[$i]};5266if(!%co) {5267next;5268}5269my$commit=$co{'id'};5270if($alternate) {5271print"<tr class=\"dark\">\n";5272}else{5273print"<tr class=\"light\">\n";5274}5275$alternate^=1;5276print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5277 format_author_html('td', \%co,15,5) .5278"<td>".5279$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5280-class=>"list subject"},5281 chop_and_escape_str($co{'title'},50) ."<br/>");5282my$comment=$co{'comment'};5283foreachmy$line(@$comment) {5284if($line=~m/^(.*?)($search_regexp)(.*)$/i) {5285my($lead,$match,$trail) = ($1,$2,$3);5286$match= chop_str($match,70,5,'center');5287my$contextlen=int((80-length($match))/2);5288$contextlen=30if($contextlen>30);5289$lead= chop_str($lead,$contextlen,10,'left');5290$trail= chop_str($trail,$contextlen,10,'right');52915292$lead= esc_html($lead);5293$match= esc_html($match);5294$trail= esc_html($trail);52955296print"$lead<span class=\"match\">$match</span>$trail<br />";5297}5298}5299print"</td>\n".5300"<td class=\"link\">".5301$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5302" | ".5303$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .5304" | ".5305$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5306print"</td>\n".5307"</tr>\n";5308}5309if(defined$extra) {5310print"<tr>\n".5311"<td colspan=\"3\">$extra</td>\n".5312"</tr>\n";5313}5314print"</table>\n";5315}53165317## ======================================================================5318## ======================================================================5319## actions53205321sub git_project_list {5322my$order=$input_params{'order'};5323if(defined$order&&$order!~m/none|project|descr|owner|age/) {5324 die_error(400,"Unknown order parameter");5325}53265327my@list= git_get_projects_list();5328if(!@list) {5329 die_error(404,"No projects found");5330}53315332 git_header_html();5333if(defined$home_text&& -f $home_text) {5334print"<div class=\"index_include\">\n";5335 insert_file($home_text);5336print"</div>\n";5337}5338print$cgi->startform(-method=>"get") .5339"<p class=\"projsearch\">Search:\n".5340$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".5341"</p>".5342$cgi->end_form() ."\n";5343 git_project_list_body(\@list,$order);5344 git_footer_html();5345}53465347sub git_forks {5348my$order=$input_params{'order'};5349if(defined$order&&$order!~m/none|project|descr|owner|age/) {5350 die_error(400,"Unknown order parameter");5351}53525353my@list= git_get_projects_list($project);5354if(!@list) {5355 die_error(404,"No forks found");5356}53575358 git_header_html();5359 git_print_page_nav('','');5360 git_print_header_div('summary',"$projectforks");5361 git_project_list_body(\@list,$order);5362 git_footer_html();5363}53645365sub git_project_index {5366my@projects= git_get_projects_list($project);53675368print$cgi->header(5369-type =>'text/plain',5370-charset =>'utf-8',5371-content_disposition =>'inline; filename="index.aux"');53725373foreachmy$pr(@projects) {5374if(!exists$pr->{'owner'}) {5375$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");5376}53775378my($path,$owner) = ($pr->{'path'},$pr->{'owner'});5379# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '5380$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5381$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;5382$path=~s/ /\+/g;5383$owner=~s/ /\+/g;53845385print"$path$owner\n";5386}5387}53885389sub git_summary {5390my$descr= git_get_project_description($project) ||"none";5391my%co= parse_commit("HEAD");5392my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();5393my$head=$co{'id'};5394my$remote_heads= gitweb_check_feature('remote_heads');53955396my$owner= git_get_project_owner($project);53975398my$refs= git_get_references();5399# These get_*_list functions return one more to allow us to see if5400# there are more ...5401my@taglist= git_get_tags_list(16);5402my@headlist= git_get_heads_list(16);5403my%remotedata=$remote_heads? git_get_remotes_list() : ();5404my@forklist;5405my$check_forks= gitweb_check_feature('forks');54065407if($check_forks) {5408@forklist= git_get_projects_list($project);5409}54105411 git_header_html();5412 git_print_page_nav('summary','',$head);54135414print"<div class=\"title\"> </div>\n";5415print"<table class=\"projects_list\">\n".5416"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".5417"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";5418if(defined$cd{'rfc2822'}) {5419print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";5420}54215422# use per project git URL list in $projectroot/$project/cloneurl5423# or make project git URL from git base URL and project name5424my$url_tag="URL";5425my@url_list= git_get_project_url_list($project);5426@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;5427foreachmy$git_url(@url_list) {5428next unless$git_url;5429print format_repo_url($url_tag,$git_url);5430$url_tag="";5431}54325433# Tag cloud5434my$show_ctags= gitweb_check_feature('ctags');5435if($show_ctags) {5436my$ctags= git_get_project_ctags($project);5437my$cloud= git_populate_project_tagcloud($ctags);5438print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";5439print"</td>\n<td>"unless%$ctags;5440print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";5441print"</td>\n<td>"if%$ctags;5442print git_show_project_tagcloud($cloud,48);5443print"</td></tr>";5444}54455446print"</table>\n";54475448# If XSS prevention is on, we don't include README.html.5449# TODO: Allow a readme in some safe format.5450if(!$prevent_xss&& -s "$projectroot/$project/README.html") {5451print"<div class=\"title\">readme</div>\n".5452"<div class=\"readme\">\n";5453 insert_file("$projectroot/$project/README.html");5454print"\n</div>\n";# class="readme"5455}54565457# we need to request one more than 16 (0..15) to check if5458# those 16 are all5459my@commitlist=$head? parse_commits($head,17) : ();5460if(@commitlist) {5461 git_print_header_div('shortlog');5462 git_shortlog_body(\@commitlist,0,15,$refs,5463$#commitlist<=15?undef:5464$cgi->a({-href => href(action=>"shortlog")},"..."));5465}54665467if(@taglist) {5468 git_print_header_div('tags');5469 git_tags_body(\@taglist,0,15,5470$#taglist<=15?undef:5471$cgi->a({-href => href(action=>"tags")},"..."));5472}54735474if(@headlist) {5475 git_print_header_div('heads');5476 git_heads_body(\@headlist,$head,0,15,5477$#headlist<=15?undef:5478$cgi->a({-href => href(action=>"heads")},"..."));5479}54805481if(%remotedata) {5482 git_print_header_div('remotes');5483 git_remotes_body(\%remotedata,15,$head);5484}54855486if(@forklist) {5487 git_print_header_div('forks');5488 git_project_list_body(\@forklist,'age',0,15,5489$#forklist<=15?undef:5490$cgi->a({-href => href(action=>"forks")},"..."),5491'no_header');5492}54935494 git_footer_html();5495}54965497sub git_tag {5498my%tag= parse_tag($hash);54995500if(!%tag) {5501 die_error(404,"Unknown tag object");5502}55035504my$head= git_get_head_hash($project);5505 git_header_html();5506 git_print_page_nav('','',$head,undef,$head);5507 git_print_header_div('commit', esc_html($tag{'name'}),$hash);5508print"<div class=\"title_text\">\n".5509"<table class=\"object_header\">\n".5510"<tr>\n".5511"<td>object</td>\n".5512"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5513$tag{'object'}) ."</td>\n".5514"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},5515$tag{'type'}) ."</td>\n".5516"</tr>\n";5517if(defined($tag{'author'})) {5518 git_print_authorship_rows(\%tag,'author');5519}5520print"</table>\n\n".5521"</div>\n";5522print"<div class=\"page_body\">";5523my$comment=$tag{'comment'};5524foreachmy$line(@$comment) {5525chomp$line;5526print esc_html($line, -nbsp=>1) ."<br/>\n";5527}5528print"</div>\n";5529 git_footer_html();5530}55315532sub git_blame_common {5533my$format=shift||'porcelain';5534if($formateq'porcelain'&&$cgi->param('js')) {5535$format='incremental';5536$action='blame_incremental';# for page title etc5537}55385539# permissions5540 gitweb_check_feature('blame')5541or die_error(403,"Blame view not allowed");55425543# error checking5544 die_error(400,"No file name given")unless$file_name;5545$hash_base||= git_get_head_hash($project);5546 die_error(404,"Couldn't find base commit")unless$hash_base;5547my%co= parse_commit($hash_base)5548or die_error(404,"Commit not found");5549my$ftype="blob";5550if(!defined$hash) {5551$hash= git_get_hash_by_path($hash_base,$file_name,"blob")5552or die_error(404,"Error looking up file");5553}else{5554$ftype= git_get_type($hash);5555if($ftype!~"blob") {5556 die_error(400,"Object is not a blob");5557}5558}55595560my$fd;5561if($formateq'incremental') {5562# get file contents (as base)5563open$fd,"-|", git_cmd(),'cat-file','blob',$hash5564or die_error(500,"Open git-cat-file failed");5565}elsif($formateq'data') {5566# run git-blame --incremental5567open$fd,"-|", git_cmd(),"blame","--incremental",5568$hash_base,"--",$file_name5569or die_error(500,"Open git-blame --incremental failed");5570}else{5571# run git-blame --porcelain5572open$fd,"-|", git_cmd(),"blame",'-p',5573$hash_base,'--',$file_name5574or die_error(500,"Open git-blame --porcelain failed");5575}55765577# incremental blame data returns early5578if($formateq'data') {5579print$cgi->header(5580-type=>"text/plain", -charset =>"utf-8",5581-status=>"200 OK");5582local$| =1;# output autoflush5583printwhile<$fd>;5584close$fd5585or print"ERROR$!\n";55865587print'END';5588if(defined$t0&& gitweb_check_feature('timed')) {5589print' '.5590 tv_interval($t0, [ gettimeofday() ]).5591' '.$number_of_git_cmds;5592}5593print"\n";55945595return;5596}55975598# page header5599 git_header_html();5600my$formats_nav=5601$cgi->a({-href => href(action=>"blob", -replay=>1)},5602"blob") .5603" | ";5604if($formateq'incremental') {5605$formats_nav.=5606$cgi->a({-href => href(action=>"blame", javascript=>0, -replay=>1)},5607"blame") ." (non-incremental)";5608}else{5609$formats_nav.=5610$cgi->a({-href => href(action=>"blame_incremental", -replay=>1)},5611"blame") ." (incremental)";5612}5613$formats_nav.=5614" | ".5615$cgi->a({-href => href(action=>"history", -replay=>1)},5616"history") .5617" | ".5618$cgi->a({-href => href(action=>$action, file_name=>$file_name)},5619"HEAD");5620 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5621 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5622 git_print_page_path($file_name,$ftype,$hash_base);56235624# page body5625if($formateq'incremental') {5626print"<noscript>\n<div class=\"error\"><center><b>\n".5627"This page requires JavaScript to run.\nUse ".5628$cgi->a({-href => href(action=>'blame',javascript=>0,-replay=>1)},5629'this page').5630" instead.\n".5631"</b></center></div>\n</noscript>\n";56325633print qq!<div id="progress_bar" style="width: 100%; background-color: yellow"></div>\n!;5634}56355636print qq!<div class="page_body">\n!;5637print qq!<div id="progress_info">.../ ...</div>\n!5638if($formateq'incremental');5639print qq!<table id="blame_table"class="blame" width="100%">\n!.5640#qq!<col width="5.5em" /><col width="2.5em" /><col width="*" />\n!.5641 qq!<thead>\n!.5642 qq!<tr><th>Commit</th><th>Line</th><th>Data</th></tr>\n!.5643 qq!</thead>\n!.5644 qq!<tbody>\n!;56455646my@rev_color=qw(light dark);5647my$num_colors=scalar(@rev_color);5648my$current_color=0;56495650if($formateq'incremental') {5651my$color_class=$rev_color[$current_color];56525653#contents of a file5654my$linenr=0;5655 LINE:5656while(my$line= <$fd>) {5657chomp$line;5658$linenr++;56595660print qq!<tr id="l$linenr"class="$color_class">!.5661 qq!<td class="sha1"><a href=""> </a></td>!.5662 qq!<td class="linenr">!.5663 qq!<a class="linenr" href="">$linenr</a></td>!;5664print qq!<td class="pre">! . esc_html($line) ."</td>\n";5665print qq!</tr>\n!;5666}56675668}else{# porcelain, i.e. ordinary blame5669my%metainfo= ();# saves information about commits56705671# blame data5672 LINE:5673while(my$line= <$fd>) {5674chomp$line;5675# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]5676# no <lines in group> for subsequent lines in group of lines5677my($full_rev,$orig_lineno,$lineno,$group_size) =5678($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);5679if(!exists$metainfo{$full_rev}) {5680$metainfo{$full_rev} = {'nprevious'=>0};5681}5682my$meta=$metainfo{$full_rev};5683my$data;5684while($data= <$fd>) {5685chomp$data;5686last if($data=~s/^\t//);# contents of line5687if($data=~/^(\S+)(?: (.*))?$/) {5688$meta->{$1} =$2unlessexists$meta->{$1};5689}5690if($data=~/^previous /) {5691$meta->{'nprevious'}++;5692}5693}5694my$short_rev=substr($full_rev,0,8);5695my$author=$meta->{'author'};5696my%date=5697 parse_date($meta->{'author-time'},$meta->{'author-tz'});5698my$date=$date{'iso-tz'};5699if($group_size) {5700$current_color= ($current_color+1) %$num_colors;5701}5702my$tr_class=$rev_color[$current_color];5703$tr_class.=' boundary'if(exists$meta->{'boundary'});5704$tr_class.=' no-previous'if($meta->{'nprevious'} ==0);5705$tr_class.=' multiple-previous'if($meta->{'nprevious'} >1);5706print"<tr id=\"l$lineno\"class=\"$tr_class\">\n";5707if($group_size) {5708print"<td class=\"sha1\"";5709print" title=\"". esc_html($author) .",$date\"";5710print" rowspan=\"$group_size\""if($group_size>1);5711print">";5712print$cgi->a({-href => href(action=>"commit",5713 hash=>$full_rev,5714 file_name=>$file_name)},5715 esc_html($short_rev));5716if($group_size>=2) {5717my@author_initials= ($author=~/\b([[:upper:]])\B/g);5718if(@author_initials) {5719print"<br />".5720 esc_html(join('',@author_initials));5721# or join('.', ...)5722}5723}5724print"</td>\n";5725}5726# 'previous' <sha1 of parent commit> <filename at commit>5727if(exists$meta->{'previous'} &&5728$meta->{'previous'} =~/^([a-fA-F0-9]{40}) (.*)$/) {5729$meta->{'parent'} =$1;5730$meta->{'file_parent'} = unquote($2);5731}5732my$linenr_commit=5733exists($meta->{'parent'}) ?5734$meta->{'parent'} :$full_rev;5735my$linenr_filename=5736exists($meta->{'file_parent'}) ?5737$meta->{'file_parent'} : unquote($meta->{'filename'});5738my$blamed= href(action =>'blame',5739 file_name =>$linenr_filename,5740 hash_base =>$linenr_commit);5741print"<td class=\"linenr\">";5742print$cgi->a({ -href =>"$blamed#l$orig_lineno",5743-class=>"linenr"},5744 esc_html($lineno));5745print"</td>";5746print"<td class=\"pre\">". esc_html($data) ."</td>\n";5747print"</tr>\n";5748}# end while57495750}57515752# footer5753print"</tbody>\n".5754"</table>\n";# class="blame"5755print"</div>\n";# class="blame_body"5756close$fd5757or print"Reading blob failed\n";57585759 git_footer_html();5760}57615762sub git_blame {5763 git_blame_common();5764}57655766sub git_blame_incremental {5767 git_blame_common('incremental');5768}57695770sub git_blame_data {5771 git_blame_common('data');5772}57735774sub git_tags {5775my$head= git_get_head_hash($project);5776 git_header_html();5777 git_print_page_nav('','',$head,undef,$head,format_ref_views('tags'));5778 git_print_header_div('summary',$project);57795780my@tagslist= git_get_tags_list();5781if(@tagslist) {5782 git_tags_body(\@tagslist);5783}5784 git_footer_html();5785}57865787sub git_heads {5788my$head= git_get_head_hash($project);5789 git_header_html();5790 git_print_page_nav('','',$head,undef,$head,format_ref_views('heads'));5791 git_print_header_div('summary',$project);57925793my@headslist= git_get_heads_list();5794if(@headslist) {5795 git_heads_body(\@headslist,$head);5796}5797 git_footer_html();5798}57995800# used both for single remote view and for list of all the remotes5801sub git_remotes {5802 gitweb_check_feature('remote_heads')5803or die_error(403,"Remote heads view is disabled");58045805my$head= git_get_head_hash($project);5806my$remote=$input_params{'hash'};58075808my$remotedata= git_get_remotes_list($remote);5809 die_error(500,"Unable to get remote information")unlessdefined$remotedata;58105811unless(%$remotedata) {5812 die_error(404,defined$remote?5813"Remote$remotenot found":5814"No remotes found");5815}58165817 git_header_html(undef,undef, -action_extra =>$remote);5818 git_print_page_nav('','',$head,undef,$head,5819 format_ref_views($remote?'':'remotes'));58205821 fill_remote_heads($remotedata);5822if(defined$remote) {5823 git_print_header_div('remotes',"$remoteremote for$project");5824 git_remote_block($remote,$remotedata->{$remote},undef,$head);5825}else{5826 git_print_header_div('summary',"$projectremotes");5827 git_remotes_body($remotedata,undef,$head);5828}58295830 git_footer_html();5831}58325833sub git_blob_plain {5834my$type=shift;5835my$expires;58365837if(!defined$hash) {5838if(defined$file_name) {5839my$base=$hash_base|| git_get_head_hash($project);5840$hash= git_get_hash_by_path($base,$file_name,"blob")5841or die_error(404,"Cannot find file");5842}else{5843 die_error(400,"No file name defined");5844}5845}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5846# blobs defined by non-textual hash id's can be cached5847$expires="+1d";5848}58495850open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5851or die_error(500,"Open git-cat-file blob '$hash' failed");58525853# content-type (can include charset)5854$type= blob_contenttype($fd,$file_name,$type);58555856# "save as" filename, even when no $file_name is given5857my$save_as="$hash";5858if(defined$file_name) {5859$save_as=$file_name;5860}elsif($type=~m/^text\//) {5861$save_as.='.txt';5862}58635864# With XSS prevention on, blobs of all types except a few known safe5865# ones are served with "Content-Disposition: attachment" to make sure5866# they don't run in our security domain. For certain image types,5867# blob view writes an <img> tag referring to blob_plain view, and we5868# want to be sure not to break that by serving the image as an5869# attachment (though Firefox 3 doesn't seem to care).5870my$sandbox=$prevent_xss&&5871$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;58725873print$cgi->header(5874-type =>$type,5875-expires =>$expires,5876-content_disposition =>5877($sandbox?'attachment':'inline')5878.'; filename="'.$save_as.'"');5879local$/=undef;5880binmode STDOUT,':raw';5881print<$fd>;5882binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5883close$fd;5884}58855886sub git_blob {5887my$expires;58885889if(!defined$hash) {5890if(defined$file_name) {5891my$base=$hash_base|| git_get_head_hash($project);5892$hash= git_get_hash_by_path($base,$file_name,"blob")5893or die_error(404,"Cannot find file");5894}else{5895 die_error(400,"No file name defined");5896}5897}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {5898# blobs defined by non-textual hash id's can be cached5899$expires="+1d";5900}59015902my$have_blame= gitweb_check_feature('blame');5903open my$fd,"-|", git_cmd(),"cat-file","blob",$hash5904or die_error(500,"Couldn't cat$file_name,$hash");5905my$mimetype= blob_mimetype($fd,$file_name);5906# use 'blob_plain' (aka 'raw') view for files that cannot be displayed5907if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {5908close$fd;5909return git_blob_plain($mimetype);5910}5911# we can have blame only for text/* mimetype5912$have_blame&&= ($mimetype=~m!^text/!);59135914my$highlight= gitweb_check_feature('highlight');5915my$syntax= guess_file_syntax($highlight,$mimetype,$file_name);5916$fd= run_highlighter($fd,$highlight,$syntax)5917if$syntax;59185919 git_header_html(undef,$expires);5920my$formats_nav='';5921if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5922if(defined$file_name) {5923if($have_blame) {5924$formats_nav.=5925$cgi->a({-href => href(action=>"blame", -replay=>1)},5926"blame") .5927" | ";5928}5929$formats_nav.=5930$cgi->a({-href => href(action=>"history", -replay=>1)},5931"history") .5932" | ".5933$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5934"raw") .5935" | ".5936$cgi->a({-href => href(action=>"blob",5937 hash_base=>"HEAD", file_name=>$file_name)},5938"HEAD");5939}else{5940$formats_nav.=5941$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},5942"raw");5943}5944 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5945 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5946}else{5947print"<div class=\"page_nav\">\n".5948"<br/><br/></div>\n".5949"<div class=\"title\">".esc_html($hash)."</div>\n";5950}5951 git_print_page_path($file_name,"blob",$hash_base);5952print"<div class=\"page_body\">\n";5953if($mimetype=~m!^image/!) {5954print qq!<img type="!.esc_attr($mimetype).qq!"!;5955if($file_name) {5956print qq! alt="!.esc_attr($file_name).qq!" title="!.esc_attr($file_name).qq!"!;5957}5958print qq! src="! .5959 href(action=>"blob_plain", hash=>$hash,5960 hash_base=>$hash_base, file_name=>$file_name) .5961 qq!"/>\n!;5962}else{5963my$nr;5964while(my$line= <$fd>) {5965chomp$line;5966$nr++;5967$line= untabify($line);5968printf qq!<div class="pre"><a id="l%i" href="%s#l%i"class="linenr">%4i</a> %s</div>\n!,5969$nr, esc_attr(href(-replay =>1)),$nr,$nr,$syntax?$line: esc_html($line, -nbsp=>1);5970}5971}5972close$fd5973or print"Reading blob failed.\n";5974print"</div>";5975 git_footer_html();5976}59775978sub git_tree {5979if(!defined$hash_base) {5980$hash_base="HEAD";5981}5982if(!defined$hash) {5983if(defined$file_name) {5984$hash= git_get_hash_by_path($hash_base,$file_name,"tree");5985}else{5986$hash=$hash_base;5987}5988}5989 die_error(404,"No such tree")unlessdefined($hash);59905991my$show_sizes= gitweb_check_feature('show-sizes');5992my$have_blame= gitweb_check_feature('blame');59935994my@entries= ();5995{5996local$/="\0";5997open my$fd,"-|", git_cmd(),"ls-tree",'-z',5998($show_sizes?'-l': ()),@extra_options,$hash5999or die_error(500,"Open git-ls-tree failed");6000@entries=map{chomp;$_} <$fd>;6001close$fd6002or die_error(404,"Reading tree failed");6003}60046005my$refs= git_get_references();6006my$ref= format_ref_marker($refs,$hash_base);6007 git_header_html();6008my$basedir='';6009if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6010my@views_nav= ();6011if(defined$file_name) {6012push@views_nav,6013$cgi->a({-href => href(action=>"history", -replay=>1)},6014"history"),6015$cgi->a({-href => href(action=>"tree",6016 hash_base=>"HEAD", file_name=>$file_name)},6017"HEAD"),6018}6019my$snapshot_links= format_snapshot_links($hash);6020if(defined$snapshot_links) {6021# FIXME: Should be available when we have no hash base as well.6022push@views_nav,$snapshot_links;6023}6024 git_print_page_nav('tree','',$hash_base,undef,undef,6025join(' | ',@views_nav));6026 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);6027}else{6028undef$hash_base;6029print"<div class=\"page_nav\">\n";6030print"<br/><br/></div>\n";6031print"<div class=\"title\">".esc_html($hash)."</div>\n";6032}6033if(defined$file_name) {6034$basedir=$file_name;6035if($basedirne''&&substr($basedir, -1)ne'/') {6036$basedir.='/';6037}6038 git_print_page_path($file_name,'tree',$hash_base);6039}6040print"<div class=\"page_body\">\n";6041print"<table class=\"tree\">\n";6042my$alternate=1;6043# '..' (top directory) link if possible6044if(defined$hash_base&&6045defined$file_name&&$file_name=~m![^/]+$!) {6046if($alternate) {6047print"<tr class=\"dark\">\n";6048}else{6049print"<tr class=\"light\">\n";6050}6051$alternate^=1;60526053my$up=$file_name;6054$up=~s!/?[^/]+$!!;6055undef$upunless$up;6056# based on git_print_tree_entry6057print'<td class="mode">'. mode_str('040000') ."</td>\n";6058print'<td class="size"> </td>'."\n"if$show_sizes;6059print'<td class="list">';6060print$cgi->a({-href => href(action=>"tree",6061 hash_base=>$hash_base,6062 file_name=>$up)},6063"..");6064print"</td>\n";6065print"<td class=\"link\"></td>\n";60666067print"</tr>\n";6068}6069foreachmy$line(@entries) {6070my%t= parse_ls_tree_line($line, -z =>1, -l =>$show_sizes);60716072if($alternate) {6073print"<tr class=\"dark\">\n";6074}else{6075print"<tr class=\"light\">\n";6076}6077$alternate^=1;60786079 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);60806081print"</tr>\n";6082}6083print"</table>\n".6084"</div>";6085 git_footer_html();6086}60876088sub snapshot_name {6089my($project,$hash) =@_;60906091# path/to/project.git -> project6092# path/to/project/.git -> project6093my$name= to_utf8($project);6094$name=~ s,([^/])/*\.git$,$1,;6095$name= basename($name);6096# sanitize name6097$name=~s/[[:cntrl:]]/?/g;60986099my$ver=$hash;6100if($hash=~/^[0-9a-fA-F]+$/) {6101# shorten SHA-1 hash6102my$full_hash= git_get_full_hash($project,$hash);6103if($full_hash=~/^$hash/&&length($hash) >7) {6104$ver= git_get_short_hash($project,$hash);6105}6106}elsif($hash=~m!^refs/tags/(.*)$!) {6107# tags don't need shortened SHA-1 hash6108$ver=$1;6109}else{6110# branches and other need shortened SHA-1 hash6111if($hash=~m!^refs/(?:heads|remotes)/(.*)$!) {6112$ver=$1;6113}6114$ver.='-'. git_get_short_hash($project,$hash);6115}6116# in case of hierarchical branch names6117$ver=~s!/!.!g;61186119# name = project-version_string6120$name="$name-$ver";61216122returnwantarray? ($name,$name) :$name;6123}61246125sub git_snapshot {6126my$format=$input_params{'snapshot_format'};6127if(!@snapshot_fmts) {6128 die_error(403,"Snapshots not allowed");6129}6130# default to first supported snapshot format6131$format||=$snapshot_fmts[0];6132if($format!~m/^[a-z0-9]+$/) {6133 die_error(400,"Invalid snapshot format parameter");6134}elsif(!exists($known_snapshot_formats{$format})) {6135 die_error(400,"Unknown snapshot format");6136}elsif($known_snapshot_formats{$format}{'disabled'}) {6137 die_error(403,"Snapshot format not allowed");6138}elsif(!grep($_eq$format,@snapshot_fmts)) {6139 die_error(403,"Unsupported snapshot format");6140}61416142my$type= git_get_type("$hash^{}");6143if(!$type) {6144 die_error(404,'Object does not exist');6145}elsif($typeeq'blob') {6146 die_error(400,'Object is not a tree-ish');6147}61486149my($name,$prefix) = snapshot_name($project,$hash);6150my$filename="$name$known_snapshot_formats{$format}{'suffix'}";6151my$cmd= quote_command(6152 git_cmd(),'archive',6153"--format=$known_snapshot_formats{$format}{'format'}",6154"--prefix=$prefix/",$hash);6155if(exists$known_snapshot_formats{$format}{'compressor'}) {6156$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});6157}61586159$filename=~s/(["\\])/\\$1/g;6160print$cgi->header(6161-type =>$known_snapshot_formats{$format}{'type'},6162-content_disposition =>'inline; filename="'.$filename.'"',6163-status =>'200 OK');61646165open my$fd,"-|",$cmd6166or die_error(500,"Execute git-archive failed");6167binmode STDOUT,':raw';6168print<$fd>;6169binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi6170close$fd;6171}61726173sub git_log_generic {6174my($fmt_name,$body_subr,$base,$parent,$file_name,$file_hash) =@_;61756176my$head= git_get_head_hash($project);6177if(!defined$base) {6178$base=$head;6179}6180if(!defined$page) {6181$page=0;6182}6183my$refs= git_get_references();61846185my$commit_hash=$base;6186if(defined$parent) {6187$commit_hash="$parent..$base";6188}6189my@commitlist=6190 parse_commits($commit_hash,101, (100*$page),6191defined$file_name? ($file_name,"--full-history") : ());61926193my$ftype;6194if(!defined$file_hash&&defined$file_name) {6195# some commits could have deleted file in question,6196# and not have it in tree, but one of them has to have it6197for(my$i=0;$i<@commitlist;$i++) {6198$file_hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);6199last ifdefined$file_hash;6200}6201}6202if(defined$file_hash) {6203$ftype= git_get_type($file_hash);6204}6205if(defined$file_name&& !defined$ftype) {6206 die_error(500,"Unknown type of object");6207}6208my%co;6209if(defined$file_name) {6210%co= parse_commit($base)6211or die_error(404,"Unknown commit object");6212}621362146215my$paging_nav= format_paging_nav($fmt_name,$page,$#commitlist>=100);6216my$next_link='';6217if($#commitlist>=100) {6218$next_link=6219$cgi->a({-href => href(-replay=>1, page=>$page+1),6220-accesskey =>"n", -title =>"Alt-n"},"next");6221}6222my$patch_max= gitweb_get_feature('patches');6223if($patch_max&& !defined$file_name) {6224if($patch_max<0||@commitlist<=$patch_max) {6225$paging_nav.=" ⋅ ".6226$cgi->a({-href => href(action=>"patches", -replay=>1)},6227"patches");6228}6229}62306231 git_header_html();6232 git_print_page_nav($fmt_name,'',$hash,$hash,$hash,$paging_nav);6233if(defined$file_name) {6234 git_print_header_div('commit', esc_html($co{'title'}),$base);6235}else{6236 git_print_header_div('summary',$project)6237}6238 git_print_page_path($file_name,$ftype,$hash_base)6239if(defined$file_name);62406241$body_subr->(\@commitlist,0,99,$refs,$next_link,6242$file_name,$file_hash,$ftype);62436244 git_footer_html();6245}62466247sub git_log {6248 git_log_generic('log', \&git_log_body,6249$hash,$hash_parent);6250}62516252sub git_commit {6253$hash||=$hash_base||"HEAD";6254my%co= parse_commit($hash)6255or die_error(404,"Unknown commit object");62566257my$parent=$co{'parent'};6258my$parents=$co{'parents'};# listref62596260# we need to prepare $formats_nav before any parameter munging6261my$formats_nav;6262if(!defined$parent) {6263# --root commitdiff6264$formats_nav.='(initial)';6265}elsif(@$parents==1) {6266# single parent commit6267$formats_nav.=6268'(parent: '.6269$cgi->a({-href => href(action=>"commit",6270 hash=>$parent)},6271 esc_html(substr($parent,0,7))) .6272')';6273}else{6274# merge commit6275$formats_nav.=6276'(merge: '.6277join(' ',map{6278$cgi->a({-href => href(action=>"commit",6279 hash=>$_)},6280 esc_html(substr($_,0,7)));6281}@$parents) .6282')';6283}6284if(gitweb_check_feature('patches') &&@$parents<=1) {6285$formats_nav.=" | ".6286$cgi->a({-href => href(action=>"patch", -replay=>1)},6287"patch");6288}62896290if(!defined$parent) {6291$parent="--root";6292}6293my@difftree;6294open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",6295@diff_opts,6296(@$parents<=1?$parent:'-c'),6297$hash,"--"6298or die_error(500,"Open git-diff-tree failed");6299@difftree=map{chomp;$_} <$fd>;6300close$fdor die_error(404,"Reading git-diff-tree failed");63016302# non-textual hash id's can be cached6303my$expires;6304if($hash=~m/^[0-9a-fA-F]{40}$/) {6305$expires="+1d";6306}6307my$refs= git_get_references();6308my$ref= format_ref_marker($refs,$co{'id'});63096310 git_header_html(undef,$expires);6311 git_print_page_nav('commit','',6312$hash,$co{'tree'},$hash,6313$formats_nav);63146315if(defined$co{'parent'}) {6316 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);6317}else{6318 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);6319}6320print"<div class=\"title_text\">\n".6321"<table class=\"object_header\">\n";6322 git_print_authorship_rows(\%co);6323print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";6324print"<tr>".6325"<td>tree</td>".6326"<td class=\"sha1\">".6327$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),6328class=>"list"},$co{'tree'}) .6329"</td>".6330"<td class=\"link\">".6331$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},6332"tree");6333my$snapshot_links= format_snapshot_links($hash);6334if(defined$snapshot_links) {6335print" | ".$snapshot_links;6336}6337print"</td>".6338"</tr>\n";63396340foreachmy$par(@$parents) {6341print"<tr>".6342"<td>parent</td>".6343"<td class=\"sha1\">".6344$cgi->a({-href => href(action=>"commit", hash=>$par),6345class=>"list"},$par) .6346"</td>".6347"<td class=\"link\">".6348$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .6349" | ".6350$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .6351"</td>".6352"</tr>\n";6353}6354print"</table>".6355"</div>\n";63566357print"<div class=\"page_body\">\n";6358 git_print_log($co{'comment'});6359print"</div>\n";63606361 git_difftree_body(\@difftree,$hash,@$parents);63626363 git_footer_html();6364}63656366sub git_object {6367# object is defined by:6368# - hash or hash_base alone6369# - hash_base and file_name6370my$type;63716372# - hash or hash_base alone6373if($hash|| ($hash_base&& !defined$file_name)) {6374my$object_id=$hash||$hash_base;63756376open my$fd,"-|", quote_command(6377 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'6378or die_error(404,"Object does not exist");6379$type= <$fd>;6380chomp$type;6381close$fd6382or die_error(404,"Object does not exist");63836384# - hash_base and file_name6385}elsif($hash_base&&defined$file_name) {6386$file_name=~ s,/+$,,;63876388system(git_cmd(),"cat-file",'-e',$hash_base) ==06389or die_error(404,"Base object does not exist");63906391# here errors should not hapen6392open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name6393or die_error(500,"Open git-ls-tree failed");6394my$line= <$fd>;6395close$fd;63966397#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'6398unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {6399 die_error(404,"File or directory for given base does not exist");6400}6401$type=$2;6402$hash=$3;6403}else{6404 die_error(400,"Not enough information to find object");6405}64066407print$cgi->redirect(-uri => href(action=>$type, -full=>1,6408 hash=>$hash, hash_base=>$hash_base,6409 file_name=>$file_name),6410-status =>'302 Found');6411}64126413sub git_blobdiff {6414my$format=shift||'html';64156416my$fd;6417my@difftree;6418my%diffinfo;6419my$expires;64206421# preparing $fd and %diffinfo for git_patchset_body6422# new style URI6423if(defined$hash_base&&defined$hash_parent_base) {6424if(defined$file_name) {6425# read raw output6426open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6427$hash_parent_base,$hash_base,6428"--", (defined$file_parent?$file_parent: ()),$file_name6429or die_error(500,"Open git-diff-tree failed");6430@difftree=map{chomp;$_} <$fd>;6431close$fd6432or die_error(404,"Reading git-diff-tree failed");6433@difftree6434or die_error(404,"Blob diff not found");64356436}elsif(defined$hash&&6437$hash=~/[0-9a-fA-F]{40}/) {6438# try to find filename from $hash64396440# read filtered raw output6441open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6442$hash_parent_base,$hash_base,"--"6443or die_error(500,"Open git-diff-tree failed");6444@difftree=6445# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'6446# $hash == to_id6447grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}6448map{chomp;$_} <$fd>;6449close$fd6450or die_error(404,"Reading git-diff-tree failed");6451@difftree6452or die_error(404,"Blob diff not found");64536454}else{6455 die_error(400,"Missing one of the blob diff parameters");6456}64576458if(@difftree>1) {6459 die_error(400,"Ambiguous blob diff specification");6460}64616462%diffinfo= parse_difftree_raw_line($difftree[0]);6463$file_parent||=$diffinfo{'from_file'} ||$file_name;6464$file_name||=$diffinfo{'to_file'};64656466$hash_parent||=$diffinfo{'from_id'};6467$hash||=$diffinfo{'to_id'};64686469# non-textual hash id's can be cached6470if($hash_base=~m/^[0-9a-fA-F]{40}$/&&6471$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {6472$expires='+1d';6473}64746475# open patch output6476open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6477'-p', ($formateq'html'?"--full-index": ()),6478$hash_parent_base,$hash_base,6479"--", (defined$file_parent?$file_parent: ()),$file_name6480or die_error(500,"Open git-diff-tree failed");6481}64826483# old/legacy style URI -- not generated anymore since 1.4.3.6484if(!%diffinfo) {6485 die_error('404 Not Found',"Missing one of the blob diff parameters")6486}64876488# header6489if($formateq'html') {6490my$formats_nav=6491$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},6492"raw");6493 git_header_html(undef,$expires);6494if(defined$hash_base&& (my%co= parse_commit($hash_base))) {6495 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);6496 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);6497}else{6498print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";6499print"<div class=\"title\">".esc_html("$hashvs$hash_parent")."</div>\n";6500}6501if(defined$file_name) {6502 git_print_page_path($file_name,"blob",$hash_base);6503}else{6504print"<div class=\"page_path\"></div>\n";6505}65066507}elsif($formateq'plain') {6508print$cgi->header(6509-type =>'text/plain',6510-charset =>'utf-8',6511-expires =>$expires,6512-content_disposition =>'inline; filename="'."$file_name".'.patch"');65136514print"X-Git-Url: ".$cgi->self_url() ."\n\n";65156516}else{6517 die_error(400,"Unknown blobdiff format");6518}65196520# patch6521if($formateq'html') {6522print"<div class=\"page_body\">\n";65236524 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);6525close$fd;65266527print"</div>\n";# class="page_body"6528 git_footer_html();65296530}else{6531while(my$line= <$fd>) {6532$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;6533$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;65346535print$line;65366537last if$line=~m!^\+\+\+!;6538}6539local$/=undef;6540print<$fd>;6541close$fd;6542}6543}65446545sub git_blobdiff_plain {6546 git_blobdiff('plain');6547}65486549sub git_commitdiff {6550my%params=@_;6551my$format=$params{-format} ||'html';65526553my($patch_max) = gitweb_get_feature('patches');6554if($formateq'patch') {6555 die_error(403,"Patch view not allowed")unless$patch_max;6556}65576558$hash||=$hash_base||"HEAD";6559my%co= parse_commit($hash)6560or die_error(404,"Unknown commit object");65616562# choose format for commitdiff for merge6563if(!defined$hash_parent&& @{$co{'parents'}} >1) {6564$hash_parent='--cc';6565}6566# we need to prepare $formats_nav before almost any parameter munging6567my$formats_nav;6568if($formateq'html') {6569$formats_nav=6570$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},6571"raw");6572if($patch_max&& @{$co{'parents'}} <=1) {6573$formats_nav.=" | ".6574$cgi->a({-href => href(action=>"patch", -replay=>1)},6575"patch");6576}65776578if(defined$hash_parent&&6579$hash_parentne'-c'&&$hash_parentne'--cc') {6580# commitdiff with two commits given6581my$hash_parent_short=$hash_parent;6582if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {6583$hash_parent_short=substr($hash_parent,0,7);6584}6585$formats_nav.=6586' (from';6587for(my$i=0;$i< @{$co{'parents'}};$i++) {6588if($co{'parents'}[$i]eq$hash_parent) {6589$formats_nav.=' parent '. ($i+1);6590last;6591}6592}6593$formats_nav.=': '.6594$cgi->a({-href => href(action=>"commitdiff",6595 hash=>$hash_parent)},6596 esc_html($hash_parent_short)) .6597')';6598}elsif(!$co{'parent'}) {6599# --root commitdiff6600$formats_nav.=' (initial)';6601}elsif(scalar@{$co{'parents'}} ==1) {6602# single parent commit6603$formats_nav.=6604' (parent: '.6605$cgi->a({-href => href(action=>"commitdiff",6606 hash=>$co{'parent'})},6607 esc_html(substr($co{'parent'},0,7))) .6608')';6609}else{6610# merge commit6611if($hash_parenteq'--cc') {6612$formats_nav.=' | '.6613$cgi->a({-href => href(action=>"commitdiff",6614 hash=>$hash, hash_parent=>'-c')},6615'combined');6616}else{# $hash_parent eq '-c'6617$formats_nav.=' | '.6618$cgi->a({-href => href(action=>"commitdiff",6619 hash=>$hash, hash_parent=>'--cc')},6620'compact');6621}6622$formats_nav.=6623' (merge: '.6624join(' ',map{6625$cgi->a({-href => href(action=>"commitdiff",6626 hash=>$_)},6627 esc_html(substr($_,0,7)));6628} @{$co{'parents'}} ) .6629')';6630}6631}66326633my$hash_parent_param=$hash_parent;6634if(!defined$hash_parent_param) {6635# --cc for multiple parents, --root for parentless6636$hash_parent_param=6637@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';6638}66396640# read commitdiff6641my$fd;6642my@difftree;6643if($formateq'html') {6644open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6645"--no-commit-id","--patch-with-raw","--full-index",6646$hash_parent_param,$hash,"--"6647or die_error(500,"Open git-diff-tree failed");66486649while(my$line= <$fd>) {6650chomp$line;6651# empty line ends raw part of diff-tree output6652last unless$line;6653push@difftree,scalar parse_difftree_raw_line($line);6654}66556656}elsif($formateq'plain') {6657open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6658'-p',$hash_parent_param,$hash,"--"6659or die_error(500,"Open git-diff-tree failed");6660}elsif($formateq'patch') {6661# For commit ranges, we limit the output to the number of6662# patches specified in the 'patches' feature.6663# For single commits, we limit the output to a single patch,6664# diverging from the git-format-patch default.6665my@commit_spec= ();6666if($hash_parent) {6667if($patch_max>0) {6668push@commit_spec,"-$patch_max";6669}6670push@commit_spec,'-n',"$hash_parent..$hash";6671}else{6672if($params{-single}) {6673push@commit_spec,'-1';6674}else{6675if($patch_max>0) {6676push@commit_spec,"-$patch_max";6677}6678push@commit_spec,"-n";6679}6680push@commit_spec,'--root',$hash;6681}6682open$fd,"-|", git_cmd(),"format-patch",@diff_opts,6683'--encoding=utf8','--stdout',@commit_spec6684or die_error(500,"Open git-format-patch failed");6685}else{6686 die_error(400,"Unknown commitdiff format");6687}66886689# non-textual hash id's can be cached6690my$expires;6691if($hash=~m/^[0-9a-fA-F]{40}$/) {6692$expires="+1d";6693}66946695# write commit message6696if($formateq'html') {6697my$refs= git_get_references();6698my$ref= format_ref_marker($refs,$co{'id'});66996700 git_header_html(undef,$expires);6701 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);6702 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);6703print"<div class=\"title_text\">\n".6704"<table class=\"object_header\">\n";6705 git_print_authorship_rows(\%co);6706print"</table>".6707"</div>\n";6708print"<div class=\"page_body\">\n";6709if(@{$co{'comment'}} >1) {6710print"<div class=\"log\">\n";6711 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);6712print"</div>\n";# class="log"6713}67146715}elsif($formateq'plain') {6716my$refs= git_get_references("tags");6717my$tagname= git_get_rev_name_tags($hash);6718my$filename= basename($project) ."-$hash.patch";67196720print$cgi->header(6721-type =>'text/plain',6722-charset =>'utf-8',6723-expires =>$expires,6724-content_disposition =>'inline; filename="'."$filename".'"');6725my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});6726print"From: ". to_utf8($co{'author'}) ."\n";6727print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";6728print"Subject: ". to_utf8($co{'title'}) ."\n";67296730print"X-Git-Tag:$tagname\n"if$tagname;6731print"X-Git-Url: ".$cgi->self_url() ."\n\n";67326733foreachmy$line(@{$co{'comment'}}) {6734print to_utf8($line) ."\n";6735}6736print"---\n\n";6737}elsif($formateq'patch') {6738my$filename= basename($project) ."-$hash.patch";67396740print$cgi->header(6741-type =>'text/plain',6742-charset =>'utf-8',6743-expires =>$expires,6744-content_disposition =>'inline; filename="'."$filename".'"');6745}67466747# write patch6748if($formateq'html') {6749my$use_parents= !defined$hash_parent||6750$hash_parenteq'-c'||$hash_parenteq'--cc';6751 git_difftree_body(\@difftree,$hash,6752$use_parents? @{$co{'parents'}} :$hash_parent);6753print"<br/>\n";67546755 git_patchset_body($fd, \@difftree,$hash,6756$use_parents? @{$co{'parents'}} :$hash_parent);6757close$fd;6758print"</div>\n";# class="page_body"6759 git_footer_html();67606761}elsif($formateq'plain') {6762local$/=undef;6763print<$fd>;6764close$fd6765or print"Reading git-diff-tree failed\n";6766}elsif($formateq'patch') {6767local$/=undef;6768print<$fd>;6769close$fd6770or print"Reading git-format-patch failed\n";6771}6772}67736774sub git_commitdiff_plain {6775 git_commitdiff(-format =>'plain');6776}67776778# format-patch-style patches6779sub git_patch {6780 git_commitdiff(-format =>'patch', -single =>1);6781}67826783sub git_patches {6784 git_commitdiff(-format =>'patch');6785}67866787sub git_history {6788 git_log_generic('history', \&git_history_body,6789$hash_base,$hash_parent_base,6790$file_name,$hash);6791}67926793sub git_search {6794$searchtype||='commit';67956796# check if appropriate features are enabled6797 gitweb_check_feature('search')6798or die_error(403,"Search is disabled");6799if($searchtypeeq'pickaxe') {6800# pickaxe may take all resources of your box and run for several minutes6801# with every query - so decide by yourself how public you make this feature6802 gitweb_check_feature('pickaxe')6803or die_error(403,"Pickaxe search is disabled");6804}6805if($searchtypeeq'grep') {6806# grep search might be potentially CPU-intensive, too6807 gitweb_check_feature('grep')6808or die_error(403,"Grep search is disabled");6809}68106811if(!defined$searchtext) {6812 die_error(400,"Text field is empty");6813}6814if(!defined$hash) {6815$hash= git_get_head_hash($project);6816}6817my%co= parse_commit($hash);6818if(!%co) {6819 die_error(404,"Unknown commit object");6820}6821if(!defined$page) {6822$page=0;6823}68246825 git_header_html();68266827if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {6828my$greptype;6829if($searchtypeeq'commit') {6830$greptype="--grep=";6831}elsif($searchtypeeq'author') {6832$greptype="--author=";6833}elsif($searchtypeeq'committer') {6834$greptype="--committer=";6835}6836$greptype.=$searchtext;6837my@commitlist= parse_commits($hash,101, (100*$page),undef,6838$greptype,'--regexp-ignore-case',6839$search_use_regexp?'--extended-regexp':'--fixed-strings');68406841my$paging_nav='';6842if($page>0) {6843$paging_nav.=6844$cgi->a({-href => href(action=>"search", hash=>$hash,6845 searchtext=>$searchtext,6846 searchtype=>$searchtype)},6847"first");6848$paging_nav.=" ⋅ ".6849$cgi->a({-href => href(-replay=>1, page=>$page-1),6850-accesskey =>"p", -title =>"Alt-p"},"prev");6851}else{6852$paging_nav.="first";6853$paging_nav.=" ⋅ prev";6854}6855my$next_link='';6856if($#commitlist>=100) {6857$next_link=6858$cgi->a({-href => href(-replay=>1, page=>$page+1),6859-accesskey =>"n", -title =>"Alt-n"},"next");6860$paging_nav.=" ⋅$next_link";6861}else{6862$paging_nav.=" ⋅ next";6863}68646865 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);6866 git_print_header_div('commit', esc_html($co{'title'}),$hash);6867if($page==0&& !@commitlist) {6868print"<p>No match.</p>\n";6869}else{6870 git_search_grep_body(\@commitlist,0,99,$next_link);6871}6872}68736874if($searchtypeeq'pickaxe') {6875 git_print_page_nav('','',$hash,$co{'tree'},$hash);6876 git_print_header_div('commit', esc_html($co{'title'}),$hash);68776878print"<table class=\"pickaxe search\">\n";6879my$alternate=1;6880local$/="\n";6881open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,6882'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",6883($search_use_regexp?'--pickaxe-regex': ());6884undef%co;6885my@files;6886while(my$line= <$fd>) {6887chomp$line;6888next unless$line;68896890my%set= parse_difftree_raw_line($line);6891if(defined$set{'commit'}) {6892# finish previous commit6893if(%co) {6894print"</td>\n".6895"<td class=\"link\">".6896$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6897" | ".6898$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6899print"</td>\n".6900"</tr>\n";6901}69026903if($alternate) {6904print"<tr class=\"dark\">\n";6905}else{6906print"<tr class=\"light\">\n";6907}6908$alternate^=1;6909%co= parse_commit($set{'commit'});6910my$author= chop_and_escape_str($co{'author_name'},15,5);6911print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".6912"<td><i>$author</i></td>\n".6913"<td>".6914$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),6915-class=>"list subject"},6916 chop_and_escape_str($co{'title'},50) ."<br/>");6917}elsif(defined$set{'to_id'}) {6918next if($set{'to_id'} =~m/^0{40}$/);69196920print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},6921 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),6922-class=>"list"},6923"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .6924"<br/>\n";6925}6926}6927close$fd;69286929# finish last commit (warning: repetition!)6930if(%co) {6931print"</td>\n".6932"<td class=\"link\">".6933$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .6934" | ".6935$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");6936print"</td>\n".6937"</tr>\n";6938}69396940print"</table>\n";6941}69426943if($searchtypeeq'grep') {6944 git_print_page_nav('','',$hash,$co{'tree'},$hash);6945 git_print_header_div('commit', esc_html($co{'title'}),$hash);69466947print"<table class=\"grep_search\">\n";6948my$alternate=1;6949my$matches=0;6950local$/="\n";6951open my$fd,"-|", git_cmd(),'grep','-n',6952$search_use_regexp? ('-E','-i') :'-F',6953$searchtext,$co{'tree'};6954my$lastfile='';6955while(my$line= <$fd>) {6956chomp$line;6957my($file,$lno,$ltext,$binary);6958last if($matches++>1000);6959if($line=~/^Binary file (.+) matches$/) {6960$file=$1;6961$binary=1;6962}else{6963(undef,$file,$lno,$ltext) =split(/:/,$line,4);6964}6965if($filene$lastfile) {6966$lastfileand print"</td></tr>\n";6967if($alternate++) {6968print"<tr class=\"dark\">\n";6969}else{6970print"<tr class=\"light\">\n";6971}6972print"<td class=\"list\">".6973$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6974 file_name=>"$file"),6975-class=>"list"}, esc_path($file));6976print"</td><td>\n";6977$lastfile=$file;6978}6979if($binary) {6980print"<div class=\"binary\">Binary file</div>\n";6981}else{6982$ltext= untabify($ltext);6983if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {6984$ltext= esc_html($1, -nbsp=>1);6985$ltext.='<span class="match">';6986$ltext.= esc_html($2, -nbsp=>1);6987$ltext.='</span>';6988$ltext.= esc_html($3, -nbsp=>1);6989}else{6990$ltext= esc_html($ltext, -nbsp=>1);6991}6992print"<div class=\"pre\">".6993$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},6994 file_name=>"$file").'#l'.$lno,6995-class=>"linenr"},sprintf('%4i',$lno))6996.' '.$ltext."</div>\n";6997}6998}6999if($lastfile) {7000print"</td></tr>\n";7001if($matches>1000) {7002print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";7003}7004}else{7005print"<div class=\"diff nodifferences\">No matches found</div>\n";7006}7007close$fd;70087009print"</table>\n";7010}7011 git_footer_html();7012}70137014sub git_search_help {7015 git_header_html();7016 git_print_page_nav('','',$hash,$hash,$hash);7017print<<EOT;7018<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without7019regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,7020the pattern entered is recognized as the POSIX extended7021<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case7022insensitive).</p>7023<dl>7024<dt><b>commit</b></dt>7025<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>7026EOT7027my$have_grep= gitweb_check_feature('grep');7028if($have_grep) {7029print<<EOT;7030<dt><b>grep</b></dt>7031<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing7032 a different one) are searched for the given pattern. On large trees, this search can take7033a while and put some strain on the server, so please use it with some consideration. Note that7034due to git-grep peculiarity, currently if regexp mode is turned off, the matches are7035case-sensitive.</dd>7036EOT7037}7038print<<EOT;7039<dt><b>author</b></dt>7040<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>7041<dt><b>committer</b></dt>7042<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>7043EOT7044my$have_pickaxe= gitweb_check_feature('pickaxe');7045if($have_pickaxe) {7046print<<EOT;7047<dt><b>pickaxe</b></dt>7048<dd>All commits that caused the string to appear or disappear from any file (changes that7049added, removed or "modified" the string) will be listed. This search can take a while and7050takes a lot of strain on the server, so please use it wisely. Note that since you may be7051interested even in changes just changing the case as well, this search is case sensitive.</dd>7052EOT7053}7054print"</dl>\n";7055 git_footer_html();7056}70577058sub git_shortlog {7059 git_log_generic('shortlog', \&git_shortlog_body,7060$hash,$hash_parent);7061}70627063## ......................................................................7064## feeds (RSS, Atom; OPML)70657066sub git_feed {7067my$format=shift||'atom';7068my$have_blame= gitweb_check_feature('blame');70697070# Atom: http://www.atomenabled.org/developers/syndication/7071# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ7072if($formatne'rss'&&$formatne'atom') {7073 die_error(400,"Unknown web feed format");7074}70757076# log/feed of current (HEAD) branch, log of given branch, history of file/directory7077my$head=$hash||'HEAD';7078my@commitlist= parse_commits($head,150,0,$file_name);70797080my%latest_commit;7081my%latest_date;7082my$content_type="application/$format+xml";7083if(defined$cgi->http('HTTP_ACCEPT') &&7084$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {7085# browser (feed reader) prefers text/xml7086$content_type='text/xml';7087}7088if(defined($commitlist[0])) {7089%latest_commit= %{$commitlist[0]};7090my$latest_epoch=$latest_commit{'committer_epoch'};7091%latest_date= parse_date($latest_epoch,$latest_commit{'comitter_tz'});7092my$if_modified=$cgi->http('IF_MODIFIED_SINCE');7093if(defined$if_modified) {7094my$since;7095if(eval{require HTTP::Date;1; }) {7096$since= HTTP::Date::str2time($if_modified);7097}elsif(eval{require Time::ParseDate;1; }) {7098$since= Time::ParseDate::parsedate($if_modified, GMT =>1);7099}7100if(defined$since&&$latest_epoch<=$since) {7101print$cgi->header(7102-type =>$content_type,7103-charset =>'utf-8',7104-last_modified =>$latest_date{'rfc2822'},7105-status =>'304 Not Modified');7106return;7107}7108}7109print$cgi->header(7110-type =>$content_type,7111-charset =>'utf-8',7112-last_modified =>$latest_date{'rfc2822'});7113}else{7114print$cgi->header(7115-type =>$content_type,7116-charset =>'utf-8');7117}71187119# Optimization: skip generating the body if client asks only7120# for Last-Modified date.7121return if($cgi->request_method()eq'HEAD');71227123# header variables7124my$title="$site_name-$project/$action";7125my$feed_type='log';7126if(defined$hash) {7127$title.=" - '$hash'";7128$feed_type='branch log';7129if(defined$file_name) {7130$title.=" ::$file_name";7131$feed_type='history';7132}7133}elsif(defined$file_name) {7134$title.=" -$file_name";7135$feed_type='history';7136}7137$title.="$feed_type";7138my$descr= git_get_project_description($project);7139if(defined$descr) {7140$descr= esc_html($descr);7141}else{7142$descr="$project".7143($formateq'rss'?'RSS':'Atom') .7144" feed";7145}7146my$owner= git_get_project_owner($project);7147$owner= esc_html($owner);71487149#header7150my$alt_url;7151if(defined$file_name) {7152$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);7153}elsif(defined$hash) {7154$alt_url= href(-full=>1, action=>"log", hash=>$hash);7155}else{7156$alt_url= href(-full=>1, action=>"summary");7157}7158print qq!<?xml version="1.0" encoding="utf-8"?>\n!;7159if($formateq'rss') {7160print<<XML;7161<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">7162<channel>7163XML7164print"<title>$title</title>\n".7165"<link>$alt_url</link>\n".7166"<description>$descr</description>\n".7167"<language>en</language>\n".7168# project owner is responsible for 'editorial' content7169"<managingEditor>$owner</managingEditor>\n";7170if(defined$logo||defined$favicon) {7171# prefer the logo to the favicon, since RSS7172# doesn't allow both7173my$img= esc_url($logo||$favicon);7174print"<image>\n".7175"<url>$img</url>\n".7176"<title>$title</title>\n".7177"<link>$alt_url</link>\n".7178"</image>\n";7179}7180if(%latest_date) {7181print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";7182print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";7183}7184print"<generator>gitweb v.$version/$git_version</generator>\n";7185}elsif($formateq'atom') {7186print<<XML;7187<feed xmlns="http://www.w3.org/2005/Atom">7188XML7189print"<title>$title</title>\n".7190"<subtitle>$descr</subtitle>\n".7191'<link rel="alternate" type="text/html" href="'.7192$alt_url.'" />'."\n".7193'<link rel="self" type="'.$content_type.'" href="'.7194$cgi->self_url() .'" />'."\n".7195"<id>". href(-full=>1) ."</id>\n".7196# use project owner for feed author7197"<author><name>$owner</name></author>\n";7198if(defined$favicon) {7199print"<icon>". esc_url($favicon) ."</icon>\n";7200}7201if(defined$logo) {7202# not twice as wide as tall: 72 x 27 pixels7203print"<logo>". esc_url($logo) ."</logo>\n";7204}7205if(!%latest_date) {7206# dummy date to keep the feed valid until commits trickle in:7207print"<updated>1970-01-01T00:00:00Z</updated>\n";7208}else{7209print"<updated>$latest_date{'iso-8601'}</updated>\n";7210}7211print"<generator version='$version/$git_version'>gitweb</generator>\n";7212}72137214# contents7215for(my$i=0;$i<=$#commitlist;$i++) {7216my%co= %{$commitlist[$i]};7217my$commit=$co{'id'};7218# we read 150, we always show 30 and the ones more recent than 48 hours7219if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {7220last;7221}7222my%cd= parse_date($co{'author_epoch'},$co{'author_tz'});72237224# get list of changed files7225open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,7226$co{'parent'} ||"--root",7227$co{'id'},"--", (defined$file_name?$file_name: ())7228ornext;7229my@difftree=map{chomp;$_} <$fd>;7230close$fd7231ornext;72327233# print element (entry, item)7234my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);7235if($formateq'rss') {7236print"<item>\n".7237"<title>". esc_html($co{'title'}) ."</title>\n".7238"<author>". esc_html($co{'author'}) ."</author>\n".7239"<pubDate>$cd{'rfc2822'}</pubDate>\n".7240"<guid isPermaLink=\"true\">$co_url</guid>\n".7241"<link>$co_url</link>\n".7242"<description>". esc_html($co{'title'}) ."</description>\n".7243"<content:encoded>".7244"<![CDATA[\n";7245}elsif($formateq'atom') {7246print"<entry>\n".7247"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".7248"<updated>$cd{'iso-8601'}</updated>\n".7249"<author>\n".7250" <name>". esc_html($co{'author_name'}) ."</name>\n";7251if($co{'author_email'}) {7252print" <email>". esc_html($co{'author_email'}) ."</email>\n";7253}7254print"</author>\n".7255# use committer for contributor7256"<contributor>\n".7257" <name>". esc_html($co{'committer_name'}) ."</name>\n";7258if($co{'committer_email'}) {7259print" <email>". esc_html($co{'committer_email'}) ."</email>\n";7260}7261print"</contributor>\n".7262"<published>$cd{'iso-8601'}</published>\n".7263"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".7264"<id>$co_url</id>\n".7265"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".7266"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";7267}7268my$comment=$co{'comment'};7269print"<pre>\n";7270foreachmy$line(@$comment) {7271$line= esc_html($line);7272print"$line\n";7273}7274print"</pre><ul>\n";7275foreachmy$difftree_line(@difftree) {7276my%difftree= parse_difftree_raw_line($difftree_line);7277next if!$difftree{'from_id'};72787279my$file=$difftree{'file'} ||$difftree{'to_file'};72807281print"<li>".7282"[".7283$cgi->a({-href => href(-full=>1, action=>"blobdiff",7284 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},7285 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},7286 file_name=>$file, file_parent=>$difftree{'from_file'}),7287-title =>"diff"},'D');7288if($have_blame) {7289print$cgi->a({-href => href(-full=>1, action=>"blame",7290 file_name=>$file, hash_base=>$commit),7291-title =>"blame"},'B');7292}7293# if this is not a feed of a file history7294if(!defined$file_name||$file_namene$file) {7295print$cgi->a({-href => href(-full=>1, action=>"history",7296 file_name=>$file, hash=>$commit),7297-title =>"history"},'H');7298}7299$file= esc_path($file);7300print"] ".7301"$file</li>\n";7302}7303if($formateq'rss') {7304print"</ul>]]>\n".7305"</content:encoded>\n".7306"</item>\n";7307}elsif($formateq'atom') {7308print"</ul>\n</div>\n".7309"</content>\n".7310"</entry>\n";7311}7312}73137314# end of feed7315if($formateq'rss') {7316print"</channel>\n</rss>\n";7317}elsif($formateq'atom') {7318print"</feed>\n";7319}7320}73217322sub git_rss {7323 git_feed('rss');7324}73257326sub git_atom {7327 git_feed('atom');7328}73297330sub git_opml {7331my@list= git_get_projects_list();73327333print$cgi->header(7334-type =>'text/xml',7335-charset =>'utf-8',7336-content_disposition =>'inline; filename="opml.xml"');73377338print<<XML;7339<?xml version="1.0" encoding="utf-8"?>7340<opml version="1.0">7341<head>7342 <title>$site_nameOPML Export</title>7343</head>7344<body>7345<outline text="git RSS feeds">7346XML73477348foreachmy$pr(@list) {7349my%proj=%$pr;7350my$head= git_get_head_hash($proj{'path'});7351if(!defined$head) {7352next;7353}7354$git_dir="$projectroot/$proj{'path'}";7355my%co= parse_commit($head);7356if(!%co) {7357next;7358}73597360my$path= esc_html(chop_str($proj{'path'},25,5));7361my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);7362my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);7363print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";7364}7365print<<XML;7366</outline>7367</body>7368</opml>7369XML7370}