1#!/usr/bin/perl 2 3# gitweb - simple web interface to track changes in git repositories 4# 5# (C) 2005-2006, Kay Sievers <kay.sievers@vrfy.org> 6# (C) 2005, Christian Gierke 7# 8# This program is licensed under the GPLv2 9 10use strict; 11use warnings; 12use CGI qw(:standard :escapeHTML -nosticky); 13use CGI::Util qw(unescape); 14use CGI::Carp qw(fatalsToBrowser); 15use Encode; 16use Fcntl ':mode'; 17use File::Find qw(); 18use File::Basename qw(basename); 19binmode STDOUT,':utf8'; 20 21BEGIN{ 22 CGI->compile()if$ENV{'MOD_PERL'}; 23} 24 25our$cgi= new CGI; 26our$version="++GIT_VERSION++"; 27our$my_url=$cgi->url(); 28our$my_uri=$cgi->url(-absolute =>1); 29 30# Base URL for relative URLs in gitweb ($logo, $favicon, ...), 31# needed and used only for URLs with nonempty PATH_INFO 32our$base_url=$my_url; 33 34# When the script is used as DirectoryIndex, the URL does not contain the name 35# of the script file itself, and $cgi->url() fails to strip PATH_INFO, so we 36# have to do it ourselves. We make $path_info global because it's also used 37# later on. 38# 39# Another issue with the script being the DirectoryIndex is that the resulting 40# $my_url data is not the full script URL: this is good, because we want 41# generated links to keep implying the script name if it wasn't explicitly 42# indicated in the URL we're handling, but it means that $my_url cannot be used 43# as base URL. 44# Therefore, if we needed to strip PATH_INFO, then we know that we have 45# to build the base URL ourselves: 46our$path_info=$ENV{"PATH_INFO"}; 47if($path_info) { 48if($my_url=~ s,\Q$path_info\E$,, && 49$my_uri=~ s,\Q$path_info\E$,, && 50defined$ENV{'SCRIPT_NAME'}) { 51$base_url=$cgi->url(-base =>1) .$ENV{'SCRIPT_NAME'}; 52} 53} 54 55# core git executable to use 56# this can just be "git" if your webserver has a sensible PATH 57our$GIT="++GIT_BINDIR++/git"; 58 59# absolute fs-path which will be prepended to the project path 60#our $projectroot = "/pub/scm"; 61our$projectroot="++GITWEB_PROJECTROOT++"; 62 63# fs traversing limit for getting project list 64# the number is relative to the projectroot 65our$project_maxdepth="++GITWEB_PROJECT_MAXDEPTH++"; 66 67# target of the home link on top of all pages 68our$home_link=$my_uri||"/"; 69 70# string of the home link on top of all pages 71our$home_link_str="++GITWEB_HOME_LINK_STR++"; 72 73# name of your site or organization to appear in page titles 74# replace this with something more descriptive for clearer bookmarks 75our$site_name="++GITWEB_SITENAME++" 76|| ($ENV{'SERVER_NAME'} ||"Untitled") ." Git"; 77 78# filename of html text to include at top of each page 79our$site_header="++GITWEB_SITE_HEADER++"; 80# html text to include at home page 81our$home_text="++GITWEB_HOMETEXT++"; 82# filename of html text to include at bottom of each page 83our$site_footer="++GITWEB_SITE_FOOTER++"; 84 85# URI of stylesheets 86our@stylesheets= ("++GITWEB_CSS++"); 87# URI of a single stylesheet, which can be overridden in GITWEB_CONFIG. 88our$stylesheet=undef; 89# URI of GIT logo (72x27 size) 90our$logo="++GITWEB_LOGO++"; 91# URI of GIT favicon, assumed to be image/png type 92our$favicon="++GITWEB_FAVICON++"; 93 94# URI and label (title) of GIT logo link 95#our $logo_url = "http://www.kernel.org/pub/software/scm/git/docs/"; 96#our $logo_label = "git documentation"; 97our$logo_url="http://git.or.cz/"; 98our$logo_label="git homepage"; 99 100# source of projects list 101our$projects_list="++GITWEB_LIST++"; 102 103# the width (in characters) of the projects list "Description" column 104our$projects_list_description_width=25; 105 106# default order of projects list 107# valid values are none, project, descr, owner, and age 108our$default_projects_order="project"; 109 110# show repository only if this file exists 111# (only effective if this variable evaluates to true) 112our$export_ok="++GITWEB_EXPORT_OK++"; 113 114# show repository only if this subroutine returns true 115# when given the path to the project, for example: 116# sub { return -e "$_[0]/git-daemon-export-ok"; } 117our$export_auth_hook=undef; 118 119# only allow viewing of repositories also shown on the overview page 120our$strict_export="++GITWEB_STRICT_EXPORT++"; 121 122# list of git base URLs used for URL to where fetch project from, 123# i.e. full URL is "$git_base_url/$project" 124our@git_base_url_list=grep{$_ne''} ("++GITWEB_BASE_URL++"); 125 126# default blob_plain mimetype and default charset for text/plain blob 127our$default_blob_plain_mimetype='text/plain'; 128our$default_text_plain_charset=undef; 129 130# file to use for guessing MIME types before trying /etc/mime.types 131# (relative to the current git repository) 132our$mimetypes_file=undef; 133 134# assume this charset if line contains non-UTF-8 characters; 135# it should be valid encoding (see Encoding::Supported(3pm) for list), 136# for which encoding all byte sequences are valid, for example 137# 'iso-8859-1' aka 'latin1' (it is decoded without checking, so it 138# could be even 'utf-8' for the old behavior) 139our$fallback_encoding='latin1'; 140 141# rename detection options for git-diff and git-diff-tree 142# - default is '-M', with the cost proportional to 143# (number of removed files) * (number of new files). 144# - more costly is '-C' (which implies '-M'), with the cost proportional to 145# (number of changed files + number of removed files) * (number of new files) 146# - even more costly is '-C', '--find-copies-harder' with cost 147# (number of files in the original tree) * (number of new files) 148# - one might want to include '-B' option, e.g. '-B', '-M' 149our@diff_opts= ('-M');# taken from git_commit 150 151# Disables features that would allow repository owners to inject script into 152# the gitweb domain. 153our$prevent_xss=0; 154 155# information about snapshot formats that gitweb is capable of serving 156our%known_snapshot_formats= ( 157# name => { 158# 'display' => display name, 159# 'type' => mime type, 160# 'suffix' => filename suffix, 161# 'format' => --format for git-archive, 162# 'compressor' => [compressor command and arguments] 163# (array reference, optional)} 164# 165'tgz'=> { 166'display'=>'tar.gz', 167'type'=>'application/x-gzip', 168'suffix'=>'.tar.gz', 169'format'=>'tar', 170'compressor'=> ['gzip']}, 171 172'tbz2'=> { 173'display'=>'tar.bz2', 174'type'=>'application/x-bzip2', 175'suffix'=>'.tar.bz2', 176'format'=>'tar', 177'compressor'=> ['bzip2']}, 178 179'zip'=> { 180'display'=>'zip', 181'type'=>'application/x-zip', 182'suffix'=>'.zip', 183'format'=>'zip'}, 184); 185 186# Aliases so we understand old gitweb.snapshot values in repository 187# configuration. 188our%known_snapshot_format_aliases= ( 189'gzip'=>'tgz', 190'bzip2'=>'tbz2', 191 192# backward compatibility: legacy gitweb config support 193'x-gzip'=>undef,'gz'=>undef, 194'x-bzip2'=>undef,'bz2'=>undef, 195'x-zip'=>undef,''=>undef, 196); 197 198# You define site-wide feature defaults here; override them with 199# $GITWEB_CONFIG as necessary. 200our%feature= ( 201# feature => { 202# 'sub' => feature-sub (subroutine), 203# 'override' => allow-override (boolean), 204# 'default' => [ default options...] (array reference)} 205# 206# if feature is overridable (it means that allow-override has true value), 207# then feature-sub will be called with default options as parameters; 208# return value of feature-sub indicates if to enable specified feature 209# 210# if there is no 'sub' key (no feature-sub), then feature cannot be 211# overriden 212# 213# use gitweb_get_feature(<feature>) to retrieve the <feature> value 214# (an array) or gitweb_check_feature(<feature>) to check if <feature> 215# is enabled 216 217# Enable the 'blame' blob view, showing the last commit that modified 218# each line in the file. This can be very CPU-intensive. 219 220# To enable system wide have in $GITWEB_CONFIG 221# $feature{'blame'}{'default'} = [1]; 222# To have project specific config enable override in $GITWEB_CONFIG 223# $feature{'blame'}{'override'} = 1; 224# and in project config gitweb.blame = 0|1; 225'blame'=> { 226'sub'=>sub{ feature_bool('blame',@_) }, 227'override'=>0, 228'default'=> [0]}, 229 230# Enable the 'snapshot' link, providing a compressed archive of any 231# tree. This can potentially generate high traffic if you have large 232# project. 233 234# Value is a list of formats defined in %known_snapshot_formats that 235# you wish to offer. 236# To disable system wide have in $GITWEB_CONFIG 237# $feature{'snapshot'}{'default'} = []; 238# To have project specific config enable override in $GITWEB_CONFIG 239# $feature{'snapshot'}{'override'} = 1; 240# and in project config, a comma-separated list of formats or "none" 241# to disable. Example: gitweb.snapshot = tbz2,zip; 242'snapshot'=> { 243'sub'=> \&feature_snapshot, 244'override'=>0, 245'default'=> ['tgz']}, 246 247# Enable text search, which will list the commits which match author, 248# committer or commit text to a given string. Enabled by default. 249# Project specific override is not supported. 250'search'=> { 251'override'=>0, 252'default'=> [1]}, 253 254# Enable grep search, which will list the files in currently selected 255# tree containing the given string. Enabled by default. This can be 256# potentially CPU-intensive, of course. 257 258# To enable system wide have in $GITWEB_CONFIG 259# $feature{'grep'}{'default'} = [1]; 260# To have project specific config enable override in $GITWEB_CONFIG 261# $feature{'grep'}{'override'} = 1; 262# and in project config gitweb.grep = 0|1; 263'grep'=> { 264'sub'=>sub{ feature_bool('grep',@_) }, 265'override'=>0, 266'default'=> [1]}, 267 268# Enable the pickaxe search, which will list the commits that modified 269# a given string in a file. This can be practical and quite faster 270# alternative to 'blame', but still potentially CPU-intensive. 271 272# To enable system wide have in $GITWEB_CONFIG 273# $feature{'pickaxe'}{'default'} = [1]; 274# To have project specific config enable override in $GITWEB_CONFIG 275# $feature{'pickaxe'}{'override'} = 1; 276# and in project config gitweb.pickaxe = 0|1; 277'pickaxe'=> { 278'sub'=>sub{ feature_bool('pickaxe',@_) }, 279'override'=>0, 280'default'=> [1]}, 281 282# Make gitweb use an alternative format of the URLs which can be 283# more readable and natural-looking: project name is embedded 284# directly in the path and the query string contains other 285# auxiliary information. All gitweb installations recognize 286# URL in either format; this configures in which formats gitweb 287# generates links. 288 289# To enable system wide have in $GITWEB_CONFIG 290# $feature{'pathinfo'}{'default'} = [1]; 291# Project specific override is not supported. 292 293# Note that you will need to change the default location of CSS, 294# favicon, logo and possibly other files to an absolute URL. Also, 295# if gitweb.cgi serves as your indexfile, you will need to force 296# $my_uri to contain the script name in your $GITWEB_CONFIG. 297'pathinfo'=> { 298'override'=>0, 299'default'=> [0]}, 300 301# Make gitweb consider projects in project root subdirectories 302# to be forks of existing projects. Given project $projname.git, 303# projects matching $projname/*.git will not be shown in the main 304# projects list, instead a '+' mark will be added to $projname 305# there and a 'forks' view will be enabled for the project, listing 306# all the forks. If project list is taken from a file, forks have 307# to be listed after the main project. 308 309# To enable system wide have in $GITWEB_CONFIG 310# $feature{'forks'}{'default'} = [1]; 311# Project specific override is not supported. 312'forks'=> { 313'override'=>0, 314'default'=> [0]}, 315 316# Insert custom links to the action bar of all project pages. 317# This enables you mainly to link to third-party scripts integrating 318# into gitweb; e.g. git-browser for graphical history representation 319# or custom web-based repository administration interface. 320 321# The 'default' value consists of a list of triplets in the form 322# (label, link, position) where position is the label after which 323# to insert the link and link is a format string where %n expands 324# to the project name, %f to the project path within the filesystem, 325# %h to the current hash (h gitweb parameter) and %b to the current 326# hash base (hb gitweb parameter); %% expands to %. 327 328# To enable system wide have in $GITWEB_CONFIG e.g. 329# $feature{'actions'}{'default'} = [('graphiclog', 330# '/git-browser/by-commit.html?r=%n', 'summary')]; 331# Project specific override is not supported. 332'actions'=> { 333'override'=>0, 334'default'=> []}, 335 336# Allow gitweb scan project content tags described in ctags/ 337# of project repository, and display the popular Web 2.0-ish 338# "tag cloud" near the project list. Note that this is something 339# COMPLETELY different from the normal Git tags. 340 341# gitweb by itself can show existing tags, but it does not handle 342# tagging itself; you need an external application for that. 343# For an example script, check Girocco's cgi/tagproj.cgi. 344# You may want to install the HTML::TagCloud Perl module to get 345# a pretty tag cloud instead of just a list of tags. 346 347# To enable system wide have in $GITWEB_CONFIG 348# $feature{'ctags'}{'default'} = ['path_to_tag_script']; 349# Project specific override is not supported. 350'ctags'=> { 351'override'=>0, 352'default'=> [0]}, 353 354# The maximum number of patches in a patchset generated in patch 355# view. Set this to 0 or undef to disable patch view, or to a 356# negative number to remove any limit. 357 358# To disable system wide have in $GITWEB_CONFIG 359# $feature{'patches'}{'default'} = [0]; 360# To have project specific config enable override in $GITWEB_CONFIG 361# $feature{'patches'}{'override'} = 1; 362# and in project config gitweb.patches = 0|n; 363# where n is the maximum number of patches allowed in a patchset. 364'patches'=> { 365'sub'=> \&feature_patches, 366'override'=>0, 367'default'=> [16]}, 368); 369 370sub gitweb_get_feature { 371my($name) =@_; 372return unlessexists$feature{$name}; 373my($sub,$override,@defaults) = ( 374$feature{$name}{'sub'}, 375$feature{$name}{'override'}, 376@{$feature{$name}{'default'}}); 377if(!$override) {return@defaults; } 378if(!defined$sub) { 379warn"feature$nameis not overrideable"; 380return@defaults; 381} 382return$sub->(@defaults); 383} 384 385# A wrapper to check if a given feature is enabled. 386# With this, you can say 387# 388# my $bool_feat = gitweb_check_feature('bool_feat'); 389# gitweb_check_feature('bool_feat') or somecode; 390# 391# instead of 392# 393# my ($bool_feat) = gitweb_get_feature('bool_feat'); 394# (gitweb_get_feature('bool_feat'))[0] or somecode; 395# 396sub gitweb_check_feature { 397return(gitweb_get_feature(@_))[0]; 398} 399 400 401sub feature_bool { 402my$key=shift; 403my($val) = git_get_project_config($key,'--bool'); 404 405if(!defined$val) { 406return($_[0]); 407}elsif($valeq'true') { 408return(1); 409}elsif($valeq'false') { 410return(0); 411} 412} 413 414sub feature_snapshot { 415my(@fmts) =@_; 416 417my($val) = git_get_project_config('snapshot'); 418 419if($val) { 420@fmts= ($valeq'none'? () :split/\s*[,\s]\s*/,$val); 421} 422 423return@fmts; 424} 425 426sub feature_patches { 427my@val= (git_get_project_config('patches','--int')); 428 429if(@val) { 430return@val; 431} 432 433return($_[0]); 434} 435 436# checking HEAD file with -e is fragile if the repository was 437# initialized long time ago (i.e. symlink HEAD) and was pack-ref'ed 438# and then pruned. 439sub check_head_link { 440my($dir) =@_; 441my$headfile="$dir/HEAD"; 442return((-e $headfile) || 443(-l $headfile&&readlink($headfile) =~/^refs\/heads\//)); 444} 445 446sub check_export_ok { 447my($dir) =@_; 448return(check_head_link($dir) && 449(!$export_ok|| -e "$dir/$export_ok") && 450(!$export_auth_hook||$export_auth_hook->($dir))); 451} 452 453# process alternate names for backward compatibility 454# filter out unsupported (unknown) snapshot formats 455sub filter_snapshot_fmts { 456my@fmts=@_; 457 458@fmts=map{ 459exists$known_snapshot_format_aliases{$_} ? 460$known_snapshot_format_aliases{$_} :$_}@fmts; 461@fmts=grep(exists$known_snapshot_formats{$_},@fmts); 462 463} 464 465our$GITWEB_CONFIG=$ENV{'GITWEB_CONFIG'} ||"++GITWEB_CONFIG++"; 466if(-e $GITWEB_CONFIG) { 467do$GITWEB_CONFIG; 468}else{ 469our$GITWEB_CONFIG_SYSTEM=$ENV{'GITWEB_CONFIG_SYSTEM'} ||"++GITWEB_CONFIG_SYSTEM++"; 470do$GITWEB_CONFIG_SYSTEMif-e $GITWEB_CONFIG_SYSTEM; 471} 472 473# version of the core git binary 474our$git_version=qx("$GIT" --version)=~m/git version (.*)$/?$1:"unknown"; 475 476$projects_list||=$projectroot; 477 478# ====================================================================== 479# input validation and dispatch 480 481# input parameters can be collected from a variety of sources (presently, CGI 482# and PATH_INFO), so we define an %input_params hash that collects them all 483# together during validation: this allows subsequent uses (e.g. href()) to be 484# agnostic of the parameter origin 485 486our%input_params= (); 487 488# input parameters are stored with the long parameter name as key. This will 489# also be used in the href subroutine to convert parameters to their CGI 490# equivalent, and since the href() usage is the most frequent one, we store 491# the name -> CGI key mapping here, instead of the reverse. 492# 493# XXX: Warning: If you touch this, check the search form for updating, 494# too. 495 496our@cgi_param_mapping= ( 497 project =>"p", 498 action =>"a", 499 file_name =>"f", 500 file_parent =>"fp", 501 hash =>"h", 502 hash_parent =>"hp", 503 hash_base =>"hb", 504 hash_parent_base =>"hpb", 505 page =>"pg", 506 order =>"o", 507 searchtext =>"s", 508 searchtype =>"st", 509 snapshot_format =>"sf", 510 extra_options =>"opt", 511 search_use_regexp =>"sr", 512); 513our%cgi_param_mapping=@cgi_param_mapping; 514 515# we will also need to know the possible actions, for validation 516our%actions= ( 517"blame"=> \&git_blame, 518"blobdiff"=> \&git_blobdiff, 519"blobdiff_plain"=> \&git_blobdiff_plain, 520"blob"=> \&git_blob, 521"blob_plain"=> \&git_blob_plain, 522"commitdiff"=> \&git_commitdiff, 523"commitdiff_plain"=> \&git_commitdiff_plain, 524"commit"=> \&git_commit, 525"forks"=> \&git_forks, 526"heads"=> \&git_heads, 527"history"=> \&git_history, 528"log"=> \&git_log, 529"patch"=> \&git_patch, 530"patches"=> \&git_patches, 531"rss"=> \&git_rss, 532"atom"=> \&git_atom, 533"search"=> \&git_search, 534"search_help"=> \&git_search_help, 535"shortlog"=> \&git_shortlog, 536"summary"=> \&git_summary, 537"tag"=> \&git_tag, 538"tags"=> \&git_tags, 539"tree"=> \&git_tree, 540"snapshot"=> \&git_snapshot, 541"object"=> \&git_object, 542# those below don't need $project 543"opml"=> \&git_opml, 544"project_list"=> \&git_project_list, 545"project_index"=> \&git_project_index, 546); 547 548# finally, we have the hash of allowed extra_options for the commands that 549# allow them 550our%allowed_options= ( 551"--no-merges"=> [qw(rss atom log shortlog history)], 552); 553 554# fill %input_params with the CGI parameters. All values except for 'opt' 555# should be single values, but opt can be an array. We should probably 556# build an array of parameters that can be multi-valued, but since for the time 557# being it's only this one, we just single it out 558while(my($name,$symbol) =each%cgi_param_mapping) { 559if($symboleq'opt') { 560$input_params{$name} = [$cgi->param($symbol) ]; 561}else{ 562$input_params{$name} =$cgi->param($symbol); 563} 564} 565 566# now read PATH_INFO and update the parameter list for missing parameters 567sub evaluate_path_info { 568return ifdefined$input_params{'project'}; 569return if!$path_info; 570$path_info=~ s,^/+,,; 571return if!$path_info; 572 573# find which part of PATH_INFO is project 574my$project=$path_info; 575$project=~ s,/+$,,; 576while($project&& !check_head_link("$projectroot/$project")) { 577$project=~ s,/*[^/]*$,,; 578} 579return unless$project; 580$input_params{'project'} =$project; 581 582# do not change any parameters if an action is given using the query string 583return if$input_params{'action'}; 584$path_info=~ s,^\Q$project\E/*,,; 585 586# next, check if we have an action 587my$action=$path_info; 588$action=~ s,/.*$,,; 589if(exists$actions{$action}) { 590$path_info=~ s,^$action/*,,; 591$input_params{'action'} =$action; 592} 593 594# list of actions that want hash_base instead of hash, but can have no 595# pathname (f) parameter 596my@wants_base= ( 597'tree', 598'history', 599); 600 601# we want to catch 602# [$hash_parent_base[:$file_parent]..]$hash_parent[:$file_name] 603my($parentrefname,$parentpathname,$refname,$pathname) = 604($path_info=~/^(?:(.+?)(?::(.+))?\.\.)?(.+?)(?::(.+))?$/); 605 606# first, analyze the 'current' part 607if(defined$pathname) { 608# we got "branch:filename" or "branch:dir/" 609# we could use git_get_type(branch:pathname), but: 610# - it needs $git_dir 611# - it does a git() call 612# - the convention of terminating directories with a slash 613# makes it superfluous 614# - embedding the action in the PATH_INFO would make it even 615# more superfluous 616$pathname=~ s,^/+,,; 617if(!$pathname||substr($pathname, -1)eq"/") { 618$input_params{'action'} ||="tree"; 619$pathname=~ s,/$,,; 620}else{ 621# the default action depends on whether we had parent info 622# or not 623if($parentrefname) { 624$input_params{'action'} ||="blobdiff_plain"; 625}else{ 626$input_params{'action'} ||="blob_plain"; 627} 628} 629$input_params{'hash_base'} ||=$refname; 630$input_params{'file_name'} ||=$pathname; 631}elsif(defined$refname) { 632# we got "branch". In this case we have to choose if we have to 633# set hash or hash_base. 634# 635# Most of the actions without a pathname only want hash to be 636# set, except for the ones specified in @wants_base that want 637# hash_base instead. It should also be noted that hand-crafted 638# links having 'history' as an action and no pathname or hash 639# set will fail, but that happens regardless of PATH_INFO. 640$input_params{'action'} ||="shortlog"; 641if(grep{$_eq$input_params{'action'} }@wants_base) { 642$input_params{'hash_base'} ||=$refname; 643}else{ 644$input_params{'hash'} ||=$refname; 645} 646} 647 648# next, handle the 'parent' part, if present 649if(defined$parentrefname) { 650# a missing pathspec defaults to the 'current' filename, allowing e.g. 651# someproject/blobdiff/oldrev..newrev:/filename 652if($parentpathname) { 653$parentpathname=~ s,^/+,,; 654$parentpathname=~ s,/$,,; 655$input_params{'file_parent'} ||=$parentpathname; 656}else{ 657$input_params{'file_parent'} ||=$input_params{'file_name'}; 658} 659# we assume that hash_parent_base is wanted if a path was specified, 660# or if the action wants hash_base instead of hash 661if(defined$input_params{'file_parent'} || 662grep{$_eq$input_params{'action'} }@wants_base) { 663$input_params{'hash_parent_base'} ||=$parentrefname; 664}else{ 665$input_params{'hash_parent'} ||=$parentrefname; 666} 667} 668 669# for the snapshot action, we allow URLs in the form 670# $project/snapshot/$hash.ext 671# where .ext determines the snapshot and gets removed from the 672# passed $refname to provide the $hash. 673# 674# To be able to tell that $refname includes the format extension, we 675# require the following two conditions to be satisfied: 676# - the hash input parameter MUST have been set from the $refname part 677# of the URL (i.e. they must be equal) 678# - the snapshot format MUST NOT have been defined already (e.g. from 679# CGI parameter sf) 680# It's also useless to try any matching unless $refname has a dot, 681# so we check for that too 682if(defined$input_params{'action'} && 683$input_params{'action'}eq'snapshot'&& 684defined$refname&&index($refname,'.') != -1&& 685$refnameeq$input_params{'hash'} && 686!defined$input_params{'snapshot_format'}) { 687# We loop over the known snapshot formats, checking for 688# extensions. Allowed extensions are both the defined suffix 689# (which includes the initial dot already) and the snapshot 690# format key itself, with a prepended dot 691while(my($fmt,%opt) =each%known_snapshot_formats) { 692my$hash=$refname; 693my$sfx; 694$hash=~s/(\Q$opt{'suffix'}\E|\Q.$fmt\E)$//; 695next unless$sfx=$1; 696# a valid suffix was found, so set the snapshot format 697# and reset the hash parameter 698$input_params{'snapshot_format'} =$fmt; 699$input_params{'hash'} =$hash; 700# we also set the format suffix to the one requested 701# in the URL: this way a request for e.g. .tgz returns 702# a .tgz instead of a .tar.gz 703$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 704last; 705} 706} 707} 708evaluate_path_info(); 709 710our$action=$input_params{'action'}; 711if(defined$action) { 712if(!validate_action($action)) { 713 die_error(400,"Invalid action parameter"); 714} 715} 716 717# parameters which are pathnames 718our$project=$input_params{'project'}; 719if(defined$project) { 720if(!validate_project($project)) { 721undef$project; 722 die_error(404,"No such project"); 723} 724} 725 726our$file_name=$input_params{'file_name'}; 727if(defined$file_name) { 728if(!validate_pathname($file_name)) { 729 die_error(400,"Invalid file parameter"); 730} 731} 732 733our$file_parent=$input_params{'file_parent'}; 734if(defined$file_parent) { 735if(!validate_pathname($file_parent)) { 736 die_error(400,"Invalid file parent parameter"); 737} 738} 739 740# parameters which are refnames 741our$hash=$input_params{'hash'}; 742if(defined$hash) { 743if(!validate_refname($hash)) { 744 die_error(400,"Invalid hash parameter"); 745} 746} 747 748our$hash_parent=$input_params{'hash_parent'}; 749if(defined$hash_parent) { 750if(!validate_refname($hash_parent)) { 751 die_error(400,"Invalid hash parent parameter"); 752} 753} 754 755our$hash_base=$input_params{'hash_base'}; 756if(defined$hash_base) { 757if(!validate_refname($hash_base)) { 758 die_error(400,"Invalid hash base parameter"); 759} 760} 761 762our@extra_options= @{$input_params{'extra_options'}}; 763# @extra_options is always defined, since it can only be (currently) set from 764# CGI, and $cgi->param() returns the empty array in array context if the param 765# is not set 766foreachmy$opt(@extra_options) { 767if(not exists$allowed_options{$opt}) { 768 die_error(400,"Invalid option parameter"); 769} 770if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 771 die_error(400,"Invalid option parameter for this action"); 772} 773} 774 775our$hash_parent_base=$input_params{'hash_parent_base'}; 776if(defined$hash_parent_base) { 777if(!validate_refname($hash_parent_base)) { 778 die_error(400,"Invalid hash parent base parameter"); 779} 780} 781 782# other parameters 783our$page=$input_params{'page'}; 784if(defined$page) { 785if($page=~m/[^0-9]/) { 786 die_error(400,"Invalid page parameter"); 787} 788} 789 790our$searchtype=$input_params{'searchtype'}; 791if(defined$searchtype) { 792if($searchtype=~m/[^a-z]/) { 793 die_error(400,"Invalid searchtype parameter"); 794} 795} 796 797our$search_use_regexp=$input_params{'search_use_regexp'}; 798 799our$searchtext=$input_params{'searchtext'}; 800our$search_regexp; 801if(defined$searchtext) { 802if(length($searchtext) <2) { 803 die_error(403,"At least two characters are required for search parameter"); 804} 805$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 806} 807 808# path to the current git repository 809our$git_dir; 810$git_dir="$projectroot/$project"if$project; 811 812# list of supported snapshot formats 813our@snapshot_fmts= gitweb_get_feature('snapshot'); 814@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 815 816# dispatch 817if(!defined$action) { 818if(defined$hash) { 819$action= git_get_type($hash); 820}elsif(defined$hash_base&&defined$file_name) { 821$action= git_get_type("$hash_base:$file_name"); 822}elsif(defined$project) { 823$action='summary'; 824}else{ 825$action='project_list'; 826} 827} 828if(!defined($actions{$action})) { 829 die_error(400,"Unknown action"); 830} 831if($action!~m/^(opml|project_list|project_index)$/&& 832!$project) { 833 die_error(400,"Project needed"); 834} 835$actions{$action}->(); 836exit; 837 838## ====================================================================== 839## action links 840 841sub href (%) { 842my%params=@_; 843# default is to use -absolute url() i.e. $my_uri 844my$href=$params{-full} ?$my_url:$my_uri; 845 846$params{'project'} =$projectunlessexists$params{'project'}; 847 848if($params{-replay}) { 849while(my($name,$symbol) =each%cgi_param_mapping) { 850if(!exists$params{$name}) { 851$params{$name} =$input_params{$name}; 852} 853} 854} 855 856my$use_pathinfo= gitweb_check_feature('pathinfo'); 857if($use_pathinfoand defined$params{'project'}) { 858# try to put as many parameters as possible in PATH_INFO: 859# - project name 860# - action 861# - hash_parent or hash_parent_base:/file_parent 862# - hash or hash_base:/filename 863# - the snapshot_format as an appropriate suffix 864 865# When the script is the root DirectoryIndex for the domain, 866# $href here would be something like http://gitweb.example.com/ 867# Thus, we strip any trailing / from $href, to spare us double 868# slashes in the final URL 869$href=~ s,/$,,; 870 871# Then add the project name, if present 872$href.="/".esc_url($params{'project'}); 873delete$params{'project'}; 874 875# since we destructively absorb parameters, we keep this 876# boolean that remembers if we're handling a snapshot 877my$is_snapshot=$params{'action'}eq'snapshot'; 878 879# Summary just uses the project path URL, any other action is 880# added to the URL 881if(defined$params{'action'}) { 882$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 883delete$params{'action'}; 884} 885 886# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 887# stripping nonexistent or useless pieces 888$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 889||$params{'hash_parent'} ||$params{'hash'}); 890if(defined$params{'hash_base'}) { 891if(defined$params{'hash_parent_base'}) { 892$href.= esc_url($params{'hash_parent_base'}); 893# skip the file_parent if it's the same as the file_name 894delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 895if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 896$href.=":/".esc_url($params{'file_parent'}); 897delete$params{'file_parent'}; 898} 899$href.=".."; 900delete$params{'hash_parent'}; 901delete$params{'hash_parent_base'}; 902}elsif(defined$params{'hash_parent'}) { 903$href.= esc_url($params{'hash_parent'}).".."; 904delete$params{'hash_parent'}; 905} 906 907$href.= esc_url($params{'hash_base'}); 908if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 909$href.=":/".esc_url($params{'file_name'}); 910delete$params{'file_name'}; 911} 912delete$params{'hash'}; 913delete$params{'hash_base'}; 914}elsif(defined$params{'hash'}) { 915$href.= esc_url($params{'hash'}); 916delete$params{'hash'}; 917} 918 919# If the action was a snapshot, we can absorb the 920# snapshot_format parameter too 921if($is_snapshot) { 922my$fmt=$params{'snapshot_format'}; 923# snapshot_format should always be defined when href() 924# is called, but just in case some code forgets, we 925# fall back to the default 926$fmt||=$snapshot_fmts[0]; 927$href.=$known_snapshot_formats{$fmt}{'suffix'}; 928delete$params{'snapshot_format'}; 929} 930} 931 932# now encode the parameters explicitly 933my@result= (); 934for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 935my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 936if(defined$params{$name}) { 937if(ref($params{$name})eq"ARRAY") { 938foreachmy$par(@{$params{$name}}) { 939push@result,$symbol."=". esc_param($par); 940} 941}else{ 942push@result,$symbol."=". esc_param($params{$name}); 943} 944} 945} 946$href.="?".join(';',@result)ifscalar@result; 947 948return$href; 949} 950 951 952## ====================================================================== 953## validation, quoting/unquoting and escaping 954 955sub validate_action { 956my$input=shift||returnundef; 957returnundefunlessexists$actions{$input}; 958return$input; 959} 960 961sub validate_project { 962my$input=shift||returnundef; 963if(!validate_pathname($input) || 964!(-d "$projectroot/$input") || 965!check_export_ok("$projectroot/$input") || 966($strict_export&& !project_in_list($input))) { 967returnundef; 968}else{ 969return$input; 970} 971} 972 973sub validate_pathname { 974my$input=shift||returnundef; 975 976# no '.' or '..' as elements of path, i.e. no '.' nor '..' 977# at the beginning, at the end, and between slashes. 978# also this catches doubled slashes 979if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 980returnundef; 981} 982# no null characters 983if($input=~m!\0!) { 984returnundef; 985} 986return$input; 987} 988 989sub validate_refname { 990my$input=shift||returnundef; 991 992# textual hashes are O.K. 993if($input=~m/^[0-9a-fA-F]{40}$/) { 994return$input; 995} 996# it must be correct pathname 997$input= validate_pathname($input) 998orreturnundef; 999# restrictions on ref name according to git-check-ref-format1000if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1001returnundef;1002}1003return$input;1004}10051006# decode sequences of octets in utf8 into Perl's internal form,1007# which is utf-8 with utf8 flag set if needed. gitweb writes out1008# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1009sub to_utf8 {1010my$str=shift;1011if(utf8::valid($str)) {1012 utf8::decode($str);1013return$str;1014}else{1015return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1016}1017}10181019# quote unsafe chars, but keep the slash, even when it's not1020# correct, but quoted slashes look too horrible in bookmarks1021sub esc_param {1022my$str=shift;1023$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg;1024$str=~s/\+/%2B/g;1025$str=~s/ /\+/g;1026return$str;1027}10281029# quote unsafe chars in whole URL, so some charactrs cannot be quoted1030sub esc_url {1031my$str=shift;1032$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1033$str=~s/\+/%2B/g;1034$str=~s/ /\+/g;1035return$str;1036}10371038# replace invalid utf8 character with SUBSTITUTION sequence1039sub esc_html ($;%) {1040my$str=shift;1041my%opts=@_;10421043$str= to_utf8($str);1044$str=$cgi->escapeHTML($str);1045if($opts{'-nbsp'}) {1046$str=~s/ / /g;1047}1048$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1049return$str;1050}10511052# quote control characters and escape filename to HTML1053sub esc_path {1054my$str=shift;1055my%opts=@_;10561057$str= to_utf8($str);1058$str=$cgi->escapeHTML($str);1059if($opts{'-nbsp'}) {1060$str=~s/ / /g;1061}1062$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1063return$str;1064}10651066# Make control characters "printable", using character escape codes (CEC)1067sub quot_cec {1068my$cntrl=shift;1069my%opts=@_;1070my%es= (# character escape codes, aka escape sequences1071"\t"=>'\t',# tab (HT)1072"\n"=>'\n',# line feed (LF)1073"\r"=>'\r',# carrige return (CR)1074"\f"=>'\f',# form feed (FF)1075"\b"=>'\b',# backspace (BS)1076"\a"=>'\a',# alarm (bell) (BEL)1077"\e"=>'\e',# escape (ESC)1078"\013"=>'\v',# vertical tab (VT)1079"\000"=>'\0',# nul character (NUL)1080);1081my$chr= ( (exists$es{$cntrl})1082?$es{$cntrl}1083:sprintf('\%2x',ord($cntrl)) );1084if($opts{-nohtml}) {1085return$chr;1086}else{1087return"<span class=\"cntrl\">$chr</span>";1088}1089}10901091# Alternatively use unicode control pictures codepoints,1092# Unicode "printable representation" (PR)1093sub quot_upr {1094my$cntrl=shift;1095my%opts=@_;10961097my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1098if($opts{-nohtml}) {1099return$chr;1100}else{1101return"<span class=\"cntrl\">$chr</span>";1102}1103}11041105# git may return quoted and escaped filenames1106sub unquote {1107my$str=shift;11081109sub unq {1110my$seq=shift;1111my%es= (# character escape codes, aka escape sequences1112't'=>"\t",# tab (HT, TAB)1113'n'=>"\n",# newline (NL)1114'r'=>"\r",# return (CR)1115'f'=>"\f",# form feed (FF)1116'b'=>"\b",# backspace (BS)1117'a'=>"\a",# alarm (bell) (BEL)1118'e'=>"\e",# escape (ESC)1119'v'=>"\013",# vertical tab (VT)1120);11211122if($seq=~m/^[0-7]{1,3}$/) {1123# octal char sequence1124returnchr(oct($seq));1125}elsif(exists$es{$seq}) {1126# C escape sequence, aka character escape code1127return$es{$seq};1128}1129# quoted ordinary character1130return$seq;1131}11321133if($str=~m/^"(.*)"$/) {1134# needs unquoting1135$str=$1;1136$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1137}1138return$str;1139}11401141# escape tabs (convert tabs to spaces)1142sub untabify {1143my$line=shift;11441145while((my$pos=index($line,"\t")) != -1) {1146if(my$count= (8- ($pos%8))) {1147my$spaces=' ' x $count;1148$line=~s/\t/$spaces/;1149}1150}11511152return$line;1153}11541155sub project_in_list {1156my$project=shift;1157my@list= git_get_projects_list();1158return@list&&scalar(grep{$_->{'path'}eq$project}@list);1159}11601161## ----------------------------------------------------------------------1162## HTML aware string manipulation11631164# Try to chop given string on a word boundary between position1165# $len and $len+$add_len. If there is no word boundary there,1166# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1167# (marking chopped part) would be longer than given string.1168sub chop_str {1169my$str=shift;1170my$len=shift;1171my$add_len=shift||10;1172my$where=shift||'right';# 'left' | 'center' | 'right'11731174# Make sure perl knows it is utf8 encoded so we don't1175# cut in the middle of a utf8 multibyte char.1176$str= to_utf8($str);11771178# allow only $len chars, but don't cut a word if it would fit in $add_len1179# if it doesn't fit, cut it if it's still longer than the dots we would add1180# remove chopped character entities entirely11811182# when chopping in the middle, distribute $len into left and right part1183# return early if chopping wouldn't make string shorter1184if($whereeq'center') {1185return$strif($len+5>=length($str));# filler is length 51186$len=int($len/2);1187}else{1188return$strif($len+4>=length($str));# filler is length 41189}11901191# regexps: ending and beginning with word part up to $add_len1192my$endre=qr/.{$len}\w{0,$add_len}/;1193my$begre=qr/\w{0,$add_len}.{$len}/;11941195if($whereeq'left') {1196$str=~m/^(.*?)($begre)$/;1197my($lead,$body) = ($1,$2);1198if(length($lead) >4) {1199$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1200$lead=" ...";1201}1202return"$lead$body";12031204}elsif($whereeq'center') {1205$str=~m/^($endre)(.*)$/;1206my($left,$str) = ($1,$2);1207$str=~m/^(.*?)($begre)$/;1208my($mid,$right) = ($1,$2);1209if(length($mid) >5) {1210$left=~s/&[^;]*$//;1211$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1212$mid=" ... ";1213}1214return"$left$mid$right";12151216}else{1217$str=~m/^($endre)(.*)$/;1218my$body=$1;1219my$tail=$2;1220if(length($tail) >4) {1221$body=~s/&[^;]*$//;1222$tail="... ";1223}1224return"$body$tail";1225}1226}12271228# takes the same arguments as chop_str, but also wraps a <span> around the1229# result with a title attribute if it does get chopped. Additionally, the1230# string is HTML-escaped.1231sub chop_and_escape_str {1232my($str) =@_;12331234my$chopped= chop_str(@_);1235if($choppedeq$str) {1236return esc_html($chopped);1237}else{1238$str=~s/([[:cntrl:]])/?/g;1239return$cgi->span({-title=>$str}, esc_html($chopped));1240}1241}12421243## ----------------------------------------------------------------------1244## functions returning short strings12451246# CSS class for given age value (in seconds)1247sub age_class {1248my$age=shift;12491250if(!defined$age) {1251return"noage";1252}elsif($age<60*60*2) {1253return"age0";1254}elsif($age<60*60*24*2) {1255return"age1";1256}else{1257return"age2";1258}1259}12601261# convert age in seconds to "nn units ago" string1262sub age_string {1263my$age=shift;1264my$age_str;12651266if($age>60*60*24*365*2) {1267$age_str= (int$age/60/60/24/365);1268$age_str.=" years ago";1269}elsif($age>60*60*24*(365/12)*2) {1270$age_str=int$age/60/60/24/(365/12);1271$age_str.=" months ago";1272}elsif($age>60*60*24*7*2) {1273$age_str=int$age/60/60/24/7;1274$age_str.=" weeks ago";1275}elsif($age>60*60*24*2) {1276$age_str=int$age/60/60/24;1277$age_str.=" days ago";1278}elsif($age>60*60*2) {1279$age_str=int$age/60/60;1280$age_str.=" hours ago";1281}elsif($age>60*2) {1282$age_str=int$age/60;1283$age_str.=" min ago";1284}elsif($age>2) {1285$age_str=int$age;1286$age_str.=" sec ago";1287}else{1288$age_str.=" right now";1289}1290return$age_str;1291}12921293useconstant{1294 S_IFINVALID =>0030000,1295 S_IFGITLINK =>0160000,1296};12971298# submodule/subproject, a commit object reference1299sub S_ISGITLINK($) {1300my$mode=shift;13011302return(($mode& S_IFMT) == S_IFGITLINK)1303}13041305# convert file mode in octal to symbolic file mode string1306sub mode_str {1307my$mode=oct shift;13081309if(S_ISGITLINK($mode)) {1310return'm---------';1311}elsif(S_ISDIR($mode& S_IFMT)) {1312return'drwxr-xr-x';1313}elsif(S_ISLNK($mode)) {1314return'lrwxrwxrwx';1315}elsif(S_ISREG($mode)) {1316# git cares only about the executable bit1317if($mode& S_IXUSR) {1318return'-rwxr-xr-x';1319}else{1320return'-rw-r--r--';1321};1322}else{1323return'----------';1324}1325}13261327# convert file mode in octal to file type string1328sub file_type {1329my$mode=shift;13301331if($mode!~m/^[0-7]+$/) {1332return$mode;1333}else{1334$mode=oct$mode;1335}13361337if(S_ISGITLINK($mode)) {1338return"submodule";1339}elsif(S_ISDIR($mode& S_IFMT)) {1340return"directory";1341}elsif(S_ISLNK($mode)) {1342return"symlink";1343}elsif(S_ISREG($mode)) {1344return"file";1345}else{1346return"unknown";1347}1348}13491350# convert file mode in octal to file type description string1351sub file_type_long {1352my$mode=shift;13531354if($mode!~m/^[0-7]+$/) {1355return$mode;1356}else{1357$mode=oct$mode;1358}13591360if(S_ISGITLINK($mode)) {1361return"submodule";1362}elsif(S_ISDIR($mode& S_IFMT)) {1363return"directory";1364}elsif(S_ISLNK($mode)) {1365return"symlink";1366}elsif(S_ISREG($mode)) {1367if($mode& S_IXUSR) {1368return"executable";1369}else{1370return"file";1371};1372}else{1373return"unknown";1374}1375}137613771378## ----------------------------------------------------------------------1379## functions returning short HTML fragments, or transforming HTML fragments1380## which don't belong to other sections13811382# format line of commit message.1383sub format_log_line_html {1384my$line=shift;13851386$line= esc_html($line, -nbsp=>1);1387$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1388$cgi->a({-href => href(action=>"object", hash=>$1),1389-class=>"text"},$1);1390}eg;13911392return$line;1393}13941395# format marker of refs pointing to given object13961397# the destination action is chosen based on object type and current context:1398# - for annotated tags, we choose the tag view unless it's the current view1399# already, in which case we go to shortlog view1400# - for other refs, we keep the current view if we're in history, shortlog or1401# log view, and select shortlog otherwise1402sub format_ref_marker {1403my($refs,$id) =@_;1404my$markers='';14051406if(defined$refs->{$id}) {1407foreachmy$ref(@{$refs->{$id}}) {1408# this code exploits the fact that non-lightweight tags are the1409# only indirect objects, and that they are the only objects for which1410# we want to use tag instead of shortlog as action1411my($type,$name) =qw();1412my$indirect= ($ref=~s/\^\{\}$//);1413# e.g. tags/v2.6.11 or heads/next1414if($ref=~m!^(.*?)s?/(.*)$!) {1415$type=$1;1416$name=$2;1417}else{1418$type="ref";1419$name=$ref;1420}14211422my$class=$type;1423$class.=" indirect"if$indirect;14241425my$dest_action="shortlog";14261427if($indirect) {1428$dest_action="tag"unless$actioneq"tag";1429}elsif($action=~/^(history|(short)?log)$/) {1430$dest_action=$action;1431}14321433my$dest="";1434$dest.="refs/"unless$ref=~ m!^refs/!;1435$dest.=$ref;14361437my$link=$cgi->a({1438-href => href(1439 action=>$dest_action,1440 hash=>$dest1441)},$name);14421443$markers.=" <span class=\"$class\"title=\"$ref\">".1444$link."</span>";1445}1446}14471448if($markers) {1449return' <span class="refs">'.$markers.'</span>';1450}else{1451return"";1452}1453}14541455# format, perhaps shortened and with markers, title line1456sub format_subject_html {1457my($long,$short,$href,$extra) =@_;1458$extra=''unlessdefined($extra);14591460if(length($short) <length($long)) {1461return$cgi->a({-href =>$href, -class=>"list subject",1462-title => to_utf8($long)},1463 esc_html($short) .$extra);1464}else{1465return$cgi->a({-href =>$href, -class=>"list subject"},1466 esc_html($long) .$extra);1467}1468}14691470# format git diff header line, i.e. "diff --(git|combined|cc) ..."1471sub format_git_diff_header_line {1472my$line=shift;1473my$diffinfo=shift;1474my($from,$to) =@_;14751476if($diffinfo->{'nparents'}) {1477# combined diff1478$line=~s!^(diff (.*?) )"?.*$!$1!;1479if($to->{'href'}) {1480$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1481 esc_path($to->{'file'}));1482}else{# file was deleted (no href)1483$line.= esc_path($to->{'file'});1484}1485}else{1486# "ordinary" diff1487$line=~s!^(diff (.*?) )"?a/.*$!$1!;1488if($from->{'href'}) {1489$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1490'a/'. esc_path($from->{'file'}));1491}else{# file was added (no href)1492$line.='a/'. esc_path($from->{'file'});1493}1494$line.=' ';1495if($to->{'href'}) {1496$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1497'b/'. esc_path($to->{'file'}));1498}else{# file was deleted1499$line.='b/'. esc_path($to->{'file'});1500}1501}15021503return"<div class=\"diff header\">$line</div>\n";1504}15051506# format extended diff header line, before patch itself1507sub format_extended_diff_header_line {1508my$line=shift;1509my$diffinfo=shift;1510my($from,$to) =@_;15111512# match <path>1513if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1514$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1515 esc_path($from->{'file'}));1516}1517if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1518$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1519 esc_path($to->{'file'}));1520}1521# match single <mode>1522if($line=~m/\s(\d{6})$/) {1523$line.='<span class="info"> ('.1524 file_type_long($1) .1525')</span>';1526}1527# match <hash>1528if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1529# can match only for combined diff1530$line='index ';1531for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1532if($from->{'href'}[$i]) {1533$line.=$cgi->a({-href=>$from->{'href'}[$i],1534-class=>"hash"},1535substr($diffinfo->{'from_id'}[$i],0,7));1536}else{1537$line.='0' x 7;1538}1539# separator1540$line.=','if($i<$diffinfo->{'nparents'} -1);1541}1542$line.='..';1543if($to->{'href'}) {1544$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1545substr($diffinfo->{'to_id'},0,7));1546}else{1547$line.='0' x 7;1548}15491550}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1551# can match only for ordinary diff1552my($from_link,$to_link);1553if($from->{'href'}) {1554$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1555substr($diffinfo->{'from_id'},0,7));1556}else{1557$from_link='0' x 7;1558}1559if($to->{'href'}) {1560$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1561substr($diffinfo->{'to_id'},0,7));1562}else{1563$to_link='0' x 7;1564}1565my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1566$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1567}15681569return$line."<br/>\n";1570}15711572# format from-file/to-file diff header1573sub format_diff_from_to_header {1574my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1575my$line;1576my$result='';15771578$line=$from_line;1579#assert($line =~ m/^---/) if DEBUG;1580# no extra formatting for "^--- /dev/null"1581if(!$diffinfo->{'nparents'}) {1582# ordinary (single parent) diff1583if($line=~m!^--- "?a/!) {1584if($from->{'href'}) {1585$line='--- a/'.1586$cgi->a({-href=>$from->{'href'}, -class=>"path"},1587 esc_path($from->{'file'}));1588}else{1589$line='--- a/'.1590 esc_path($from->{'file'});1591}1592}1593$result.= qq!<div class="diff from_file">$line</div>\n!;15941595}else{1596# combined diff (merge commit)1597for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1598if($from->{'href'}[$i]) {1599$line='--- '.1600$cgi->a({-href=>href(action=>"blobdiff",1601 hash_parent=>$diffinfo->{'from_id'}[$i],1602 hash_parent_base=>$parents[$i],1603 file_parent=>$from->{'file'}[$i],1604 hash=>$diffinfo->{'to_id'},1605 hash_base=>$hash,1606 file_name=>$to->{'file'}),1607-class=>"path",1608-title=>"diff". ($i+1)},1609$i+1) .1610'/'.1611$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1612 esc_path($from->{'file'}[$i]));1613}else{1614$line='--- /dev/null';1615}1616$result.= qq!<div class="diff from_file">$line</div>\n!;1617}1618}16191620$line=$to_line;1621#assert($line =~ m/^\+\+\+/) if DEBUG;1622# no extra formatting for "^+++ /dev/null"1623if($line=~m!^\+\+\+ "?b/!) {1624if($to->{'href'}) {1625$line='+++ b/'.1626$cgi->a({-href=>$to->{'href'}, -class=>"path"},1627 esc_path($to->{'file'}));1628}else{1629$line='+++ b/'.1630 esc_path($to->{'file'});1631}1632}1633$result.= qq!<div class="diff to_file">$line</div>\n!;16341635return$result;1636}16371638# create note for patch simplified by combined diff1639sub format_diff_cc_simplified {1640my($diffinfo,@parents) =@_;1641my$result='';16421643$result.="<div class=\"diff header\">".1644"diff --cc ";1645if(!is_deleted($diffinfo)) {1646$result.=$cgi->a({-href => href(action=>"blob",1647 hash_base=>$hash,1648 hash=>$diffinfo->{'to_id'},1649 file_name=>$diffinfo->{'to_file'}),1650-class=>"path"},1651 esc_path($diffinfo->{'to_file'}));1652}else{1653$result.= esc_path($diffinfo->{'to_file'});1654}1655$result.="</div>\n".# class="diff header"1656"<div class=\"diff nodifferences\">".1657"Simple merge".1658"</div>\n";# class="diff nodifferences"16591660return$result;1661}16621663# format patch (diff) line (not to be used for diff headers)1664sub format_diff_line {1665my$line=shift;1666my($from,$to) =@_;1667my$diff_class="";16681669chomp$line;16701671if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1672# combined diff1673my$prefix=substr($line,0,scalar@{$from->{'href'}});1674if($line=~m/^\@{3}/) {1675$diff_class=" chunk_header";1676}elsif($line=~m/^\\/) {1677$diff_class=" incomplete";1678}elsif($prefix=~tr/+/+/) {1679$diff_class=" add";1680}elsif($prefix=~tr/-/-/) {1681$diff_class=" rem";1682}1683}else{1684# assume ordinary diff1685my$char=substr($line,0,1);1686if($chareq'+') {1687$diff_class=" add";1688}elsif($chareq'-') {1689$diff_class=" rem";1690}elsif($chareq'@') {1691$diff_class=" chunk_header";1692}elsif($chareq"\\") {1693$diff_class=" incomplete";1694}1695}1696$line= untabify($line);1697if($from&&$to&&$line=~m/^\@{2} /) {1698my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1699$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;17001701$from_lines=0unlessdefined$from_lines;1702$to_lines=0unlessdefined$to_lines;17031704if($from->{'href'}) {1705$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1706-class=>"list"},$from_text);1707}1708if($to->{'href'}) {1709$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1710-class=>"list"},$to_text);1711}1712$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1713"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1714return"<div class=\"diff$diff_class\">$line</div>\n";1715}elsif($from&&$to&&$line=~m/^\@{3}/) {1716my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1717my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);17181719@from_text=split(' ',$ranges);1720for(my$i=0;$i<@from_text; ++$i) {1721($from_start[$i],$from_nlines[$i]) =1722(split(',',substr($from_text[$i],1)),0);1723}17241725$to_text=pop@from_text;1726$to_start=pop@from_start;1727$to_nlines=pop@from_nlines;17281729$line="<span class=\"chunk_info\">$prefix";1730for(my$i=0;$i<@from_text; ++$i) {1731if($from->{'href'}[$i]) {1732$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1733-class=>"list"},$from_text[$i]);1734}else{1735$line.=$from_text[$i];1736}1737$line.=" ";1738}1739if($to->{'href'}) {1740$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1741-class=>"list"},$to_text);1742}else{1743$line.=$to_text;1744}1745$line.="$prefix</span>".1746"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1747return"<div class=\"diff$diff_class\">$line</div>\n";1748}1749return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1750}17511752# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1753# linked. Pass the hash of the tree/commit to snapshot.1754sub format_snapshot_links {1755my($hash) =@_;1756my$num_fmts=@snapshot_fmts;1757if($num_fmts>1) {1758# A parenthesized list of links bearing format names.1759# e.g. "snapshot (_tar.gz_ _zip_)"1760return"snapshot (".join(' ',map1761$cgi->a({1762-href => href(1763 action=>"snapshot",1764 hash=>$hash,1765 snapshot_format=>$_1766)1767},$known_snapshot_formats{$_}{'display'})1768,@snapshot_fmts) .")";1769}elsif($num_fmts==1) {1770# A single "snapshot" link whose tooltip bears the format name.1771# i.e. "_snapshot_"1772my($fmt) =@snapshot_fmts;1773return1774$cgi->a({1775-href => href(1776 action=>"snapshot",1777 hash=>$hash,1778 snapshot_format=>$fmt1779),1780-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1781},"snapshot");1782}else{# $num_fmts == 01783returnundef;1784}1785}17861787## ......................................................................1788## functions returning values to be passed, perhaps after some1789## transformation, to other functions; e.g. returning arguments to href()17901791# returns hash to be passed to href to generate gitweb URL1792# in -title key it returns description of link1793sub get_feed_info {1794my$format=shift||'Atom';1795my%res= (action =>lc($format));17961797# feed links are possible only for project views1798return unless(defined$project);1799# some views should link to OPML, or to generic project feed,1800# or don't have specific feed yet (so they should use generic)1801return if($action=~/^(?:tags|heads|forks|tag|search)$/x);18021803my$branch;1804# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1805# from tag links; this also makes possible to detect branch links1806if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1807(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1808$branch=$1;1809}1810# find log type for feed description (title)1811my$type='log';1812if(defined$file_name) {1813$type="history of$file_name";1814$type.="/"if($actioneq'tree');1815$type.=" on '$branch'"if(defined$branch);1816}else{1817$type="log of$branch"if(defined$branch);1818}18191820$res{-title} =$type;1821$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1822$res{'file_name'} =$file_name;18231824return%res;1825}18261827## ----------------------------------------------------------------------1828## git utility subroutines, invoking git commands18291830# returns path to the core git executable and the --git-dir parameter as list1831sub git_cmd {1832return$GIT,'--git-dir='.$git_dir;1833}18341835# quote the given arguments for passing them to the shell1836# quote_command("command", "arg 1", "arg with ' and ! characters")1837# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1838# Try to avoid using this function wherever possible.1839sub quote_command {1840returnjoin(' ',1841map( {my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_));1842}18431844# get HEAD ref of given project as hash1845sub git_get_head_hash {1846my$project=shift;1847my$o_git_dir=$git_dir;1848my$retval=undef;1849$git_dir="$projectroot/$project";1850if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1851my$head= <$fd>;1852close$fd;1853if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1854$retval=$1;1855}1856}1857if(defined$o_git_dir) {1858$git_dir=$o_git_dir;1859}1860return$retval;1861}18621863# get type of given object1864sub git_get_type {1865my$hash=shift;18661867open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1868my$type= <$fd>;1869close$fdorreturn;1870chomp$type;1871return$type;1872}18731874# repository configuration1875our$config_file='';1876our%config;18771878# store multiple values for single key as anonymous array reference1879# single values stored directly in the hash, not as [ <value> ]1880sub hash_set_multi {1881my($hash,$key,$value) =@_;18821883if(!exists$hash->{$key}) {1884$hash->{$key} =$value;1885}elsif(!ref$hash->{$key}) {1886$hash->{$key} = [$hash->{$key},$value];1887}else{1888push@{$hash->{$key}},$value;1889}1890}18911892# return hash of git project configuration1893# optionally limited to some section, e.g. 'gitweb'1894sub git_parse_project_config {1895my$section_regexp=shift;1896my%config;18971898local$/="\0";18991900open my$fh,"-|", git_cmd(),"config",'-z','-l',1901orreturn;19021903while(my$keyval= <$fh>) {1904chomp$keyval;1905my($key,$value) =split(/\n/,$keyval,2);19061907 hash_set_multi(\%config,$key,$value)1908if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1909}1910close$fh;19111912return%config;1913}19141915# convert config value to boolean: 'true' or 'false'1916# no value, number > 0, 'true' and 'yes' values are true1917# rest of values are treated as false (never as error)1918sub config_to_bool {1919my$val=shift;19201921return1if!defined$val;# section.key19221923# strip leading and trailing whitespace1924$val=~s/^\s+//;1925$val=~s/\s+$//;19261927return(($val=~/^\d+$/&&$val) ||# section.key = 11928($val=~/^(?:true|yes)$/i));# section.key = true1929}19301931# convert config value to simple decimal number1932# an optional value suffix of 'k', 'm', or 'g' will cause the value1933# to be multiplied by 1024, 1048576, or 10737418241934sub config_to_int {1935my$val=shift;19361937# strip leading and trailing whitespace1938$val=~s/^\s+//;1939$val=~s/\s+$//;19401941if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1942$unit=lc($unit);1943# unknown unit is treated as 11944return$num* ($uniteq'g'?1073741824:1945$uniteq'm'?1048576:1946$uniteq'k'?1024:1);1947}1948return$val;1949}19501951# convert config value to array reference, if needed1952sub config_to_multi {1953my$val=shift;19541955returnref($val) ?$val: (defined($val) ? [$val] : []);1956}19571958sub git_get_project_config {1959my($key,$type) =@_;19601961# key sanity check1962return unless($key);1963$key=~s/^gitweb\.//;1964return if($key=~m/\W/);19651966# type sanity check1967if(defined$type) {1968$type=~s/^--//;1969$type=undef1970unless($typeeq'bool'||$typeeq'int');1971}19721973# get config1974if(!defined$config_file||1975$config_filene"$git_dir/config") {1976%config= git_parse_project_config('gitweb');1977$config_file="$git_dir/config";1978}19791980# check if config variable (key) exists1981return unlessexists$config{"gitweb.$key"};19821983# ensure given type1984if(!defined$type) {1985return$config{"gitweb.$key"};1986}elsif($typeeq'bool') {1987# backward compatibility: 'git config --bool' returns true/false1988return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1989}elsif($typeeq'int') {1990return config_to_int($config{"gitweb.$key"});1991}1992return$config{"gitweb.$key"};1993}19941995# get hash of given path at given ref1996sub git_get_hash_by_path {1997my$base=shift;1998my$path=shift||returnundef;1999my$type=shift;20002001$path=~ s,/+$,,;20022003open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2004or die_error(500,"Open git-ls-tree failed");2005my$line= <$fd>;2006close$fdorreturnundef;20072008if(!defined$line) {2009# there is no tree or hash given by $path at $base2010returnundef;2011}20122013#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2014$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2015if(defined$type&&$typene$2) {2016# type doesn't match2017returnundef;2018}2019return$3;2020}20212022# get path of entry with given hash at given tree-ish (ref)2023# used to get 'from' filename for combined diff (merge commit) for renames2024sub git_get_path_by_hash {2025my$base=shift||return;2026my$hash=shift||return;20272028local$/="\0";20292030open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2031orreturnundef;2032while(my$line= <$fd>) {2033chomp$line;20342035#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2036#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2037if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2038close$fd;2039return$1;2040}2041}2042close$fd;2043returnundef;2044}20452046## ......................................................................2047## git utility functions, directly accessing git repository20482049sub git_get_project_description {2050my$path=shift;20512052$git_dir="$projectroot/$path";2053open my$fd,"$git_dir/description"2054orreturn git_get_project_config('description');2055my$descr= <$fd>;2056close$fd;2057if(defined$descr) {2058chomp$descr;2059}2060return$descr;2061}20622063sub git_get_project_ctags {2064my$path=shift;2065my$ctags= {};20662067$git_dir="$projectroot/$path";2068unless(opendir D,"$git_dir/ctags") {2069return$ctags;2070}2071foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir(D)) {2072open CT,$_ornext;2073my$val= <CT>;2074chomp$val;2075close CT;2076my$ctag=$_;$ctag=~ s#.*/##;2077$ctags->{$ctag} =$val;2078}2079closedir D;2080$ctags;2081}20822083sub git_populate_project_tagcloud {2084my$ctags=shift;20852086# First, merge different-cased tags; tags vote on casing2087my%ctags_lc;2088foreach(keys%$ctags) {2089$ctags_lc{lc$_}->{count} +=$ctags->{$_};2090if(not$ctags_lc{lc$_}->{topcount}2091or$ctags_lc{lc$_}->{topcount} <$ctags->{$_}) {2092$ctags_lc{lc$_}->{topcount} =$ctags->{$_};2093$ctags_lc{lc$_}->{topname} =$_;2094}2095}20962097my$cloud;2098if(eval{require HTML::TagCloud;1; }) {2099$cloud= HTML::TagCloud->new;2100foreach(sort keys%ctags_lc) {2101# Pad the title with spaces so that the cloud looks2102# less crammed.2103my$title=$ctags_lc{$_}->{topname};2104$title=~s/ / /g;2105$title=~s/^/ /g;2106$title=~s/$/ /g;2107$cloud->add($title,$home_link."?by_tag=".$_,$ctags_lc{$_}->{count});2108}2109}else{2110$cloud= \%ctags_lc;2111}2112$cloud;2113}21142115sub git_show_project_tagcloud {2116my($cloud,$count) =@_;2117print STDERR ref($cloud)."..\n";2118if(ref$cloudeq'HTML::TagCloud') {2119return$cloud->html_and_css($count);2120}else{2121my@tags=sort{$cloud->{$a}->{count} <=>$cloud->{$b}->{count} }keys%$cloud;2122return'<p align="center">'.join(', ',map{2123"<a href=\"$home_link?by_tag=$_\">$cloud->{$_}->{topname}</a>"2124}splice(@tags,0,$count)) .'</p>';2125}2126}21272128sub git_get_project_url_list {2129my$path=shift;21302131$git_dir="$projectroot/$path";2132open my$fd,"$git_dir/cloneurl"2133orreturnwantarray?2134@{ config_to_multi(git_get_project_config('url')) } :2135 config_to_multi(git_get_project_config('url'));2136my@git_project_url_list=map{chomp;$_} <$fd>;2137close$fd;21382139returnwantarray?@git_project_url_list: \@git_project_url_list;2140}21412142sub git_get_projects_list {2143my($filter) =@_;2144my@list;21452146$filter||='';2147$filter=~s/\.git$//;21482149my$check_forks= gitweb_check_feature('forks');21502151if(-d $projects_list) {2152# search in directory2153my$dir=$projects_list. ($filter?"/$filter":'');2154# remove the trailing "/"2155$dir=~s!/+$!!;2156my$pfxlen=length("$dir");2157my$pfxdepth= ($dir=~tr!/!!);21582159 File::Find::find({2160 follow_fast =>1,# follow symbolic links2161 follow_skip =>2,# ignore duplicates2162 dangling_symlinks =>0,# ignore dangling symlinks, silently2163 wanted =>sub{2164# skip project-list toplevel, if we get it.2165return if(m!^[/.]$!);2166# only directories can be git repositories2167return unless(-d $_);2168# don't traverse too deep (Find is super slow on os x)2169if(($File::Find::name =~tr!/!!) -$pfxdepth>$project_maxdepth) {2170$File::Find::prune =1;2171return;2172}21732174my$subdir=substr($File::Find::name,$pfxlen+1);2175# we check related file in $projectroot2176my$path= ($filter?"$filter/":'') .$subdir;2177if(check_export_ok("$projectroot/$path")) {2178push@list, { path =>$path};2179$File::Find::prune =1;2180}2181},2182},"$dir");21832184}elsif(-f $projects_list) {2185# read from file(url-encoded):2186# 'git%2Fgit.git Linus+Torvalds'2187# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2188# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2189my%paths;2190open my($fd),$projects_listorreturn;2191 PROJECT:2192while(my$line= <$fd>) {2193chomp$line;2194my($path,$owner) =split' ',$line;2195$path= unescape($path);2196$owner= unescape($owner);2197if(!defined$path) {2198next;2199}2200if($filterne'') {2201# looking for forks;2202my$pfx=substr($path,0,length($filter));2203if($pfxne$filter) {2204next PROJECT;2205}2206my$sfx=substr($path,length($filter));2207if($sfx!~/^\/.*\.git$/) {2208next PROJECT;2209}2210}elsif($check_forks) {2211 PATH:2212foreachmy$filter(keys%paths) {2213# looking for forks;2214my$pfx=substr($path,0,length($filter));2215if($pfxne$filter) {2216next PATH;2217}2218my$sfx=substr($path,length($filter));2219if($sfx!~/^\/.*\.git$/) {2220next PATH;2221}2222# is a fork, don't include it in2223# the list2224next PROJECT;2225}2226}2227if(check_export_ok("$projectroot/$path")) {2228my$pr= {2229 path =>$path,2230 owner => to_utf8($owner),2231};2232push@list,$pr;2233(my$forks_path=$path) =~s/\.git$//;2234$paths{$forks_path}++;2235}2236}2237close$fd;2238}2239return@list;2240}22412242our$gitweb_project_owner=undef;2243sub git_get_project_list_from_file {22442245return if(defined$gitweb_project_owner);22462247$gitweb_project_owner= {};2248# read from file (url-encoded):2249# 'git%2Fgit.git Linus+Torvalds'2250# 'libs%2Fklibc%2Fklibc.git H.+Peter+Anvin'2251# 'linux%2Fhotplug%2Fudev.git Greg+Kroah-Hartman'2252if(-f $projects_list) {2253open(my$fd,$projects_list);2254while(my$line= <$fd>) {2255chomp$line;2256my($pr,$ow) =split' ',$line;2257$pr= unescape($pr);2258$ow= unescape($ow);2259$gitweb_project_owner->{$pr} = to_utf8($ow);2260}2261close$fd;2262}2263}22642265sub git_get_project_owner {2266my$project=shift;2267my$owner;22682269returnundefunless$project;2270$git_dir="$projectroot/$project";22712272if(!defined$gitweb_project_owner) {2273 git_get_project_list_from_file();2274}22752276if(exists$gitweb_project_owner->{$project}) {2277$owner=$gitweb_project_owner->{$project};2278}2279if(!defined$owner){2280$owner= git_get_project_config('owner');2281}2282if(!defined$owner) {2283$owner= get_file_owner("$git_dir");2284}22852286return$owner;2287}22882289sub git_get_last_activity {2290my($path) =@_;2291my$fd;22922293$git_dir="$projectroot/$path";2294open($fd,"-|", git_cmd(),'for-each-ref',2295'--format=%(committer)',2296'--sort=-committerdate',2297'--count=1',2298'refs/heads')orreturn;2299my$most_recent= <$fd>;2300close$fdorreturn;2301if(defined$most_recent&&2302$most_recent=~/ (\d+) [-+][01]\d\d\d$/) {2303my$timestamp=$1;2304my$age=time-$timestamp;2305return($age, age_string($age));2306}2307return(undef,undef);2308}23092310sub git_get_references {2311my$type=shift||"";2312my%refs;2313# 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.112314# c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{}2315open my$fd,"-|", git_cmd(),"show-ref","--dereference",2316($type? ("--","refs/$type") : ())# use -- <pattern> if $type2317orreturn;23182319while(my$line= <$fd>) {2320chomp$line;2321if($line=~m!^([0-9a-fA-F]{40})\srefs/($type.*)$!) {2322if(defined$refs{$1}) {2323push@{$refs{$1}},$2;2324}else{2325$refs{$1} = [$2];2326}2327}2328}2329close$fdorreturn;2330return \%refs;2331}23322333sub git_get_rev_name_tags {2334my$hash=shift||returnundef;23352336open my$fd,"-|", git_cmd(),"name-rev","--tags",$hash2337orreturn;2338my$name_rev= <$fd>;2339close$fd;23402341if($name_rev=~ m|^$hash tags/(.*)$|) {2342return$1;2343}else{2344# catches also '$hash undefined' output2345returnundef;2346}2347}23482349## ----------------------------------------------------------------------2350## parse to hash functions23512352sub parse_date {2353my$epoch=shift;2354my$tz=shift||"-0000";23552356my%date;2357my@months= ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");2358my@days= ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");2359my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($epoch);2360$date{'hour'} =$hour;2361$date{'minute'} =$min;2362$date{'mday'} =$mday;2363$date{'day'} =$days[$wday];2364$date{'month'} =$months[$mon];2365$date{'rfc2822'} =sprintf"%s,%d%s%4d%02d:%02d:%02d+0000",2366$days[$wday],$mday,$months[$mon],1900+$year,$hour,$min,$sec;2367$date{'mday-time'} =sprintf"%d%s%02d:%02d",2368$mday,$months[$mon],$hour,$min;2369$date{'iso-8601'} =sprintf"%04d-%02d-%02dT%02d:%02d:%02dZ",23701900+$year,1+$mon,$mday,$hour,$min,$sec;23712372$tz=~m/^([+\-][0-9][0-9])([0-9][0-9])$/;2373my$local=$epoch+ ((int$1+ ($2/60)) *3600);2374($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($local);2375$date{'hour_local'} =$hour;2376$date{'minute_local'} =$min;2377$date{'tz_local'} =$tz;2378$date{'iso-tz'} =sprintf("%04d-%02d-%02d%02d:%02d:%02d%s",23791900+$year,$mon+1,$mday,2380$hour,$min,$sec,$tz);2381return%date;2382}23832384sub parse_tag {2385my$tag_id=shift;2386my%tag;2387my@comment;23882389open my$fd,"-|", git_cmd(),"cat-file","tag",$tag_idorreturn;2390$tag{'id'} =$tag_id;2391while(my$line= <$fd>) {2392chomp$line;2393if($line=~m/^object ([0-9a-fA-F]{40})$/) {2394$tag{'object'} =$1;2395}elsif($line=~m/^type (.+)$/) {2396$tag{'type'} =$1;2397}elsif($line=~m/^tag (.+)$/) {2398$tag{'name'} =$1;2399}elsif($line=~m/^tagger (.*) ([0-9]+) (.*)$/) {2400$tag{'author'} =$1;2401$tag{'epoch'} =$2;2402$tag{'tz'} =$3;2403}elsif($line=~m/--BEGIN/) {2404push@comment,$line;2405last;2406}elsif($lineeq"") {2407last;2408}2409}2410push@comment, <$fd>;2411$tag{'comment'} = \@comment;2412close$fdorreturn;2413if(!defined$tag{'name'}) {2414return2415};2416return%tag2417}24182419sub parse_commit_text {2420my($commit_text,$withparents) =@_;2421my@commit_lines=split'\n',$commit_text;2422my%co;24232424pop@commit_lines;# Remove '\0'24252426if(!@commit_lines) {2427return;2428}24292430my$header=shift@commit_lines;2431if($header!~m/^[0-9a-fA-F]{40}/) {2432return;2433}2434($co{'id'},my@parents) =split' ',$header;2435while(my$line=shift@commit_lines) {2436last if$lineeq"\n";2437if($line=~m/^tree ([0-9a-fA-F]{40})$/) {2438$co{'tree'} =$1;2439}elsif((!defined$withparents) && ($line=~m/^parent ([0-9a-fA-F]{40})$/)) {2440push@parents,$1;2441}elsif($line=~m/^author (.*) ([0-9]+) (.*)$/) {2442$co{'author'} =$1;2443$co{'author_epoch'} =$2;2444$co{'author_tz'} =$3;2445if($co{'author'} =~m/^([^<]+) <([^>]*)>/) {2446$co{'author_name'} =$1;2447$co{'author_email'} =$2;2448}else{2449$co{'author_name'} =$co{'author'};2450}2451}elsif($line=~m/^committer (.*) ([0-9]+) (.*)$/) {2452$co{'committer'} =$1;2453$co{'committer_epoch'} =$2;2454$co{'committer_tz'} =$3;2455$co{'committer_name'} =$co{'committer'};2456if($co{'committer'} =~m/^([^<]+) <([^>]*)>/) {2457$co{'committer_name'} =$1;2458$co{'committer_email'} =$2;2459}else{2460$co{'committer_name'} =$co{'committer'};2461}2462}2463}2464if(!defined$co{'tree'}) {2465return;2466};2467$co{'parents'} = \@parents;2468$co{'parent'} =$parents[0];24692470foreachmy$title(@commit_lines) {2471$title=~s/^ //;2472if($titlene"") {2473$co{'title'} = chop_str($title,80,5);2474# remove leading stuff of merges to make the interesting part visible2475if(length($title) >50) {2476$title=~s/^Automatic //;2477$title=~s/^merge (of|with) /Merge ... /i;2478if(length($title) >50) {2479$title=~s/(http|rsync):\/\///;2480}2481if(length($title) >50) {2482$title=~s/(master|www|rsync)\.//;2483}2484if(length($title) >50) {2485$title=~s/kernel.org:?//;2486}2487if(length($title) >50) {2488$title=~s/\/pub\/scm//;2489}2490}2491$co{'title_short'} = chop_str($title,50,5);2492last;2493}2494}2495if(!defined$co{'title'} ||$co{'title'}eq"") {2496$co{'title'} =$co{'title_short'} ='(no commit message)';2497}2498# remove added spaces2499foreachmy$line(@commit_lines) {2500$line=~s/^ //;2501}2502$co{'comment'} = \@commit_lines;25032504my$age=time-$co{'committer_epoch'};2505$co{'age'} =$age;2506$co{'age_string'} = age_string($age);2507my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) =gmtime($co{'committer_epoch'});2508if($age>60*60*24*7*2) {2509$co{'age_string_date'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2510$co{'age_string_age'} =$co{'age_string'};2511}else{2512$co{'age_string_date'} =$co{'age_string'};2513$co{'age_string_age'} =sprintf"%4i-%02u-%02i",1900+$year,$mon+1,$mday;2514}2515return%co;2516}25172518sub parse_commit {2519my($commit_id) =@_;2520my%co;25212522local$/="\0";25232524open my$fd,"-|", git_cmd(),"rev-list",2525"--parents",2526"--header",2527"--max-count=1",2528$commit_id,2529"--",2530or die_error(500,"Open git-rev-list failed");2531%co= parse_commit_text(<$fd>,1);2532close$fd;25332534return%co;2535}25362537sub parse_commits {2538my($commit_id,$maxcount,$skip,$filename,@args) =@_;2539my@cos;25402541$maxcount||=1;2542$skip||=0;25432544local$/="\0";25452546open my$fd,"-|", git_cmd(),"rev-list",2547"--header",2548@args,2549("--max-count=".$maxcount),2550("--skip=".$skip),2551@extra_options,2552$commit_id,2553"--",2554($filename? ($filename) : ())2555or die_error(500,"Open git-rev-list failed");2556while(my$line= <$fd>) {2557my%co= parse_commit_text($line);2558push@cos, \%co;2559}2560close$fd;25612562returnwantarray?@cos: \@cos;2563}25642565# parse line of git-diff-tree "raw" output2566sub parse_difftree_raw_line {2567my$line=shift;2568my%res;25692570# ':100644 100644 03b218260e99b78c6df0ed378e59ed9205ccc96d 3b93d5e7cc7f7dd4ebed13a5cc1a4ad976fc94d8 M ls-files.c'2571# ':100644 100644 7f9281985086971d3877aca27704f2aaf9c448ce bc190ebc71bbd923f2b728e505408f5e54bd073a M rev-tree.c'2572if($line=~m/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/) {2573$res{'from_mode'} =$1;2574$res{'to_mode'} =$2;2575$res{'from_id'} =$3;2576$res{'to_id'} =$4;2577$res{'status'} =$5;2578$res{'similarity'} =$6;2579if($res{'status'}eq'R'||$res{'status'}eq'C') {# renamed or copied2580($res{'from_file'},$res{'to_file'}) =map{ unquote($_) }split("\t",$7);2581}else{2582$res{'from_file'} =$res{'to_file'} =$res{'file'} = unquote($7);2583}2584}2585# '::100755 100755 100755 60e79ca1b01bc8b057abe17ddab484699a7f5fdb 94067cc5f73388f33722d52ae02f44692bc07490 94067cc5f73388f33722d52ae02f44692bc07490 MR git-gui/git-gui.sh'2586# combined diff (for merge commit)2587elsif($line=~s/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$//) {2588$res{'nparents'} =length($1);2589$res{'from_mode'} = [split(' ',$2) ];2590$res{'to_mode'} =pop@{$res{'from_mode'}};2591$res{'from_id'} = [split(' ',$3) ];2592$res{'to_id'} =pop@{$res{'from_id'}};2593$res{'status'} = [split('',$4) ];2594$res{'to_file'} = unquote($5);2595}2596# 'c512b523472485aef4fff9e57b229d9d243c967f'2597elsif($line=~m/^([0-9a-fA-F]{40})$/) {2598$res{'commit'} =$1;2599}26002601returnwantarray?%res: \%res;2602}26032604# wrapper: return parsed line of git-diff-tree "raw" output2605# (the argument might be raw line, or parsed info)2606sub parsed_difftree_line {2607my$line_or_ref=shift;26082609if(ref($line_or_ref)eq"HASH") {2610# pre-parsed (or generated by hand)2611return$line_or_ref;2612}else{2613return parse_difftree_raw_line($line_or_ref);2614}2615}26162617# parse line of git-ls-tree output2618sub parse_ls_tree_line ($;%) {2619my$line=shift;2620my%opts=@_;2621my%res;26222623#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2624$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;26252626$res{'mode'} =$1;2627$res{'type'} =$2;2628$res{'hash'} =$3;2629if($opts{'-z'}) {2630$res{'name'} =$4;2631}else{2632$res{'name'} = unquote($4);2633}26342635returnwantarray?%res: \%res;2636}26372638# generates _two_ hashes, references to which are passed as 2 and 3 argument2639sub parse_from_to_diffinfo {2640my($diffinfo,$from,$to,@parents) =@_;26412642if($diffinfo->{'nparents'}) {2643# combined diff2644$from->{'file'} = [];2645$from->{'href'} = [];2646 fill_from_file_info($diffinfo,@parents)2647unlessexists$diffinfo->{'from_file'};2648for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {2649$from->{'file'}[$i] =2650defined$diffinfo->{'from_file'}[$i] ?2651$diffinfo->{'from_file'}[$i] :2652$diffinfo->{'to_file'};2653if($diffinfo->{'status'}[$i]ne"A") {# not new (added) file2654$from->{'href'}[$i] = href(action=>"blob",2655 hash_base=>$parents[$i],2656 hash=>$diffinfo->{'from_id'}[$i],2657 file_name=>$from->{'file'}[$i]);2658}else{2659$from->{'href'}[$i] =undef;2660}2661}2662}else{2663# ordinary (not combined) diff2664$from->{'file'} =$diffinfo->{'from_file'};2665if($diffinfo->{'status'}ne"A") {# not new (added) file2666$from->{'href'} = href(action=>"blob", hash_base=>$hash_parent,2667 hash=>$diffinfo->{'from_id'},2668 file_name=>$from->{'file'});2669}else{2670delete$from->{'href'};2671}2672}26732674$to->{'file'} =$diffinfo->{'to_file'};2675if(!is_deleted($diffinfo)) {# file exists in result2676$to->{'href'} = href(action=>"blob", hash_base=>$hash,2677 hash=>$diffinfo->{'to_id'},2678 file_name=>$to->{'file'});2679}else{2680delete$to->{'href'};2681}2682}26832684## ......................................................................2685## parse to array of hashes functions26862687sub git_get_heads_list {2688my$limit=shift;2689my@headslist;26902691open my$fd,'-|', git_cmd(),'for-each-ref',2692($limit?'--count='.($limit+1) : ()),'--sort=-committerdate',2693'--format=%(objectname) %(refname) %(subject)%00%(committer)',2694'refs/heads'2695orreturn;2696while(my$line= <$fd>) {2697my%ref_item;26982699chomp$line;2700my($refinfo,$committerinfo) =split(/\0/,$line);2701my($hash,$name,$title) =split(' ',$refinfo,3);2702my($committer,$epoch,$tz) =2703($committerinfo=~/^(.*) ([0-9]+) (.*)$/);2704$ref_item{'fullname'} =$name;2705$name=~s!^refs/heads/!!;27062707$ref_item{'name'} =$name;2708$ref_item{'id'} =$hash;2709$ref_item{'title'} =$title||'(no commit message)';2710$ref_item{'epoch'} =$epoch;2711if($epoch) {2712$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2713}else{2714$ref_item{'age'} ="unknown";2715}27162717push@headslist, \%ref_item;2718}2719close$fd;27202721returnwantarray?@headslist: \@headslist;2722}27232724sub git_get_tags_list {2725my$limit=shift;2726my@tagslist;27272728open my$fd,'-|', git_cmd(),'for-each-ref',2729($limit?'--count='.($limit+1) : ()),'--sort=-creatordate',2730'--format=%(objectname) %(objecttype) %(refname) '.2731'%(*objectname) %(*objecttype) %(subject)%00%(creator)',2732'refs/tags'2733orreturn;2734while(my$line= <$fd>) {2735my%ref_item;27362737chomp$line;2738my($refinfo,$creatorinfo) =split(/\0/,$line);2739my($id,$type,$name,$refid,$reftype,$title) =split(' ',$refinfo,6);2740my($creator,$epoch,$tz) =2741($creatorinfo=~/^(.*) ([0-9]+) (.*)$/);2742$ref_item{'fullname'} =$name;2743$name=~s!^refs/tags/!!;27442745$ref_item{'type'} =$type;2746$ref_item{'id'} =$id;2747$ref_item{'name'} =$name;2748if($typeeq"tag") {2749$ref_item{'subject'} =$title;2750$ref_item{'reftype'} =$reftype;2751$ref_item{'refid'} =$refid;2752}else{2753$ref_item{'reftype'} =$type;2754$ref_item{'refid'} =$id;2755}27562757if($typeeq"tag"||$typeeq"commit") {2758$ref_item{'epoch'} =$epoch;2759if($epoch) {2760$ref_item{'age'} = age_string(time-$ref_item{'epoch'});2761}else{2762$ref_item{'age'} ="unknown";2763}2764}27652766push@tagslist, \%ref_item;2767}2768close$fd;27692770returnwantarray?@tagslist: \@tagslist;2771}27722773## ----------------------------------------------------------------------2774## filesystem-related functions27752776sub get_file_owner {2777my$path=shift;27782779my($dev,$ino,$mode,$nlink,$st_uid,$st_gid,$rdev,$size) =stat($path);2780my($name,$passwd,$uid,$gid,$quota,$comment,$gcos,$dir,$shell) =getpwuid($st_uid);2781if(!defined$gcos) {2782returnundef;2783}2784my$owner=$gcos;2785$owner=~s/[,;].*$//;2786return to_utf8($owner);2787}27882789# assume that file exists2790sub insert_file {2791my$filename=shift;27922793open my$fd,'<',$filename;2794print map{ to_utf8($_) } <$fd>;2795close$fd;2796}27972798## ......................................................................2799## mimetype related functions28002801sub mimetype_guess_file {2802my$filename=shift;2803my$mimemap=shift;2804-r $mimemaporreturnundef;28052806my%mimemap;2807open(MIME,$mimemap)orreturnundef;2808while(<MIME>) {2809next ifm/^#/;# skip comments2810my($mime,$exts) =split(/\t+/);2811if(defined$exts) {2812my@exts=split(/\s+/,$exts);2813foreachmy$ext(@exts) {2814$mimemap{$ext} =$mime;2815}2816}2817}2818close(MIME);28192820$filename=~/\.([^.]*)$/;2821return$mimemap{$1};2822}28232824sub mimetype_guess {2825my$filename=shift;2826my$mime;2827$filename=~/\./orreturnundef;28282829if($mimetypes_file) {2830my$file=$mimetypes_file;2831if($file!~m!^/!) {# if it is relative path2832# it is relative to project2833$file="$projectroot/$project/$file";2834}2835$mime= mimetype_guess_file($filename,$file);2836}2837$mime||= mimetype_guess_file($filename,'/etc/mime.types');2838return$mime;2839}28402841sub blob_mimetype {2842my$fd=shift;2843my$filename=shift;28442845if($filename) {2846my$mime= mimetype_guess($filename);2847$mimeandreturn$mime;2848}28492850# just in case2851return$default_blob_plain_mimetypeunless$fd;28522853if(-T $fd) {2854return'text/plain';2855}elsif(!$filename) {2856return'application/octet-stream';2857}elsif($filename=~m/\.png$/i) {2858return'image/png';2859}elsif($filename=~m/\.gif$/i) {2860return'image/gif';2861}elsif($filename=~m/\.jpe?g$/i) {2862return'image/jpeg';2863}else{2864return'application/octet-stream';2865}2866}28672868sub blob_contenttype {2869my($fd,$file_name,$type) =@_;28702871$type||= blob_mimetype($fd,$file_name);2872if($typeeq'text/plain'&&defined$default_text_plain_charset) {2873$type.="; charset=$default_text_plain_charset";2874}28752876return$type;2877}28782879## ======================================================================2880## functions printing HTML: header, footer, error page28812882sub git_header_html {2883my$status=shift||"200 OK";2884my$expires=shift;28852886my$title="$site_name";2887if(defined$project) {2888$title.=" - ". to_utf8($project);2889if(defined$action) {2890$title.="/$action";2891if(defined$file_name) {2892$title.=" - ". esc_path($file_name);2893if($actioneq"tree"&&$file_name!~ m|/$|) {2894$title.="/";2895}2896}2897}2898}2899my$content_type;2900# require explicit support from the UA if we are to send the page as2901# 'application/xhtml+xml', otherwise send it as plain old 'text/html'.2902# we have to do this because MSIE sometimes globs '*/*', pretending to2903# support xhtml+xml but choking when it gets what it asked for.2904if(defined$cgi->http('HTTP_ACCEPT') &&2905$cgi->http('HTTP_ACCEPT') =~m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&2906$cgi->Accept('application/xhtml+xml') !=0) {2907$content_type='application/xhtml+xml';2908}else{2909$content_type='text/html';2910}2911print$cgi->header(-type=>$content_type, -charset =>'utf-8',2912-status=>$status, -expires =>$expires);2913my$mod_perl_version=$ENV{'MOD_PERL'} ?"$ENV{'MOD_PERL'}":'';2914print<<EOF;2915<?xml version="1.0" encoding="utf-8"?>2916<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">2917<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">2918<!-- git web interface version$version, (C) 2005-2006, Kay Sievers <kay.sievers\@vrfy.org>, Christian Gierke -->2919<!-- git core binaries version$git_version-->2920<head>2921<meta http-equiv="content-type" content="$content_type; charset=utf-8"/>2922<meta name="generator" content="gitweb/$versiongit/$git_version$mod_perl_version"/>2923<meta name="robots" content="index, nofollow"/>2924<title>$title</title>2925EOF2926# the stylesheet, favicon etc urls won't work correctly with path_info2927# unless we set the appropriate base URL2928if($ENV{'PATH_INFO'}) {2929print"<base href=\"".esc_url($base_url)."\"/>\n";2930}2931# print out each stylesheet that exist, providing backwards capability2932# for those people who defined $stylesheet in a config file2933if(defined$stylesheet) {2934print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2935}else{2936foreachmy$stylesheet(@stylesheets) {2937next unless$stylesheet;2938print'<link rel="stylesheet" type="text/css" href="'.$stylesheet.'"/>'."\n";2939}2940}2941if(defined$project) {2942my%href_params= get_feed_info();2943if(!exists$href_params{'-title'}) {2944$href_params{'-title'} ='log';2945}29462947foreachmy$formatqw(RSS Atom){2948my$type=lc($format);2949my%link_attr= (2950'-rel'=>'alternate',2951'-title'=>"$project-$href_params{'-title'} -$formatfeed",2952'-type'=>"application/$type+xml"2953);29542955$href_params{'action'} =$type;2956$link_attr{'-href'} = href(%href_params);2957print"<link ".2958"rel=\"$link_attr{'-rel'}\"".2959"title=\"$link_attr{'-title'}\"".2960"href=\"$link_attr{'-href'}\"".2961"type=\"$link_attr{'-type'}\"".2962"/>\n";29632964$href_params{'extra_options'} ='--no-merges';2965$link_attr{'-href'} = href(%href_params);2966$link_attr{'-title'} .=' (no merges)';2967print"<link ".2968"rel=\"$link_attr{'-rel'}\"".2969"title=\"$link_attr{'-title'}\"".2970"href=\"$link_attr{'-href'}\"".2971"type=\"$link_attr{'-type'}\"".2972"/>\n";2973}29742975}else{2976printf('<link rel="alternate" title="%sprojects list" '.2977'href="%s" type="text/plain; charset=utf-8" />'."\n",2978$site_name, href(project=>undef, action=>"project_index"));2979printf('<link rel="alternate" title="%sprojects feeds" '.2980'href="%s" type="text/x-opml" />'."\n",2981$site_name, href(project=>undef, action=>"opml"));2982}2983if(defined$favicon) {2984printqq(<link rel="shortcut icon" href="$favicon" type="image/png" />\n);2985}29862987print"</head>\n".2988"<body>\n";29892990if(-f $site_header) {2991 insert_file($site_header);2992}29932994print"<div class=\"page_header\">\n".2995$cgi->a({-href => esc_url($logo_url),2996-title =>$logo_label},2997qq(<img src="$logo" width="72" height="27" alt="git" class="logo"/>));2998print$cgi->a({-href => esc_url($home_link)},$home_link_str) ." / ";2999if(defined$project) {3000print$cgi->a({-href => href(action=>"summary")}, esc_html($project));3001if(defined$action) {3002print" /$action";3003}3004print"\n";3005}3006print"</div>\n";30073008my$have_search= gitweb_check_feature('search');3009if(defined$project&&$have_search) {3010if(!defined$searchtext) {3011$searchtext="";3012}3013my$search_hash;3014if(defined$hash_base) {3015$search_hash=$hash_base;3016}elsif(defined$hash) {3017$search_hash=$hash;3018}else{3019$search_hash="HEAD";3020}3021my$action=$my_uri;3022my$use_pathinfo= gitweb_check_feature('pathinfo');3023if($use_pathinfo) {3024$action.="/".esc_url($project);3025}3026print$cgi->startform(-method=>"get", -action =>$action) .3027"<div class=\"search\">\n".3028(!$use_pathinfo&&3029$cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) ."\n") .3030$cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) ."\n".3031$cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) ."\n".3032$cgi->popup_menu(-name =>'st', -default=>'commit',3033-values=> ['commit','grep','author','committer','pickaxe']) .3034$cgi->sup($cgi->a({-href => href(action=>"search_help")},"?")) .3035" search:\n",3036$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".3037"<span title=\"Extended regular expression\">".3038$cgi->checkbox(-name =>'sr', -value =>1, -label =>'re',3039-checked =>$search_use_regexp) .3040"</span>".3041"</div>".3042$cgi->end_form() ."\n";3043}3044}30453046sub git_footer_html {3047my$feed_class='rss_logo';30483049print"<div class=\"page_footer\">\n";3050if(defined$project) {3051my$descr= git_get_project_description($project);3052if(defined$descr) {3053print"<div class=\"page_footer_text\">". esc_html($descr) ."</div>\n";3054}30553056my%href_params= get_feed_info();3057if(!%href_params) {3058$feed_class.=' generic';3059}3060$href_params{'-title'} ||='log';30613062foreachmy$formatqw(RSS Atom){3063$href_params{'action'} =lc($format);3064print$cgi->a({-href => href(%href_params),3065-title =>"$href_params{'-title'}$formatfeed",3066-class=>$feed_class},$format)."\n";3067}30683069}else{3070print$cgi->a({-href => href(project=>undef, action=>"opml"),3071-class=>$feed_class},"OPML") ." ";3072print$cgi->a({-href => href(project=>undef, action=>"project_index"),3073-class=>$feed_class},"TXT") ."\n";3074}3075print"</div>\n";# class="page_footer"30763077if(-f $site_footer) {3078 insert_file($site_footer);3079}30803081print"</body>\n".3082"</html>";3083}30843085# die_error(<http_status_code>, <error_message>)3086# Example: die_error(404, 'Hash not found')3087# By convention, use the following status codes (as defined in RFC 2616):3088# 400: Invalid or missing CGI parameters, or3089# requested object exists but has wrong type.3090# 403: Requested feature (like "pickaxe" or "snapshot") not enabled on3091# this server or project.3092# 404: Requested object/revision/project doesn't exist.3093# 500: The server isn't configured properly, or3094# an internal error occurred (e.g. failed assertions caused by bugs), or3095# an unknown error occurred (e.g. the git binary died unexpectedly).3096sub die_error {3097my$status=shift||500;3098my$error=shift||"Internal server error";30993100my%http_responses= (400=>'400 Bad Request',3101403=>'403 Forbidden',3102404=>'404 Not Found',3103500=>'500 Internal Server Error');3104 git_header_html($http_responses{$status});3105print<<EOF;3106<div class="page_body">3107<br /><br />3108$status-$error3109<br />3110</div>3111EOF3112 git_footer_html();3113exit;3114}31153116## ----------------------------------------------------------------------3117## functions printing or outputting HTML: navigation31183119sub git_print_page_nav {3120my($current,$suppress,$head,$treehead,$treebase,$extra) =@_;3121$extra=''if!defined$extra;# pager or formats31223123my@navs=qw(summary shortlog log commit commitdiff tree);3124if($suppress) {3125@navs=grep{$_ne$suppress}@navs;3126}31273128my%arg=map{$_=> {action=>$_} }@navs;3129if(defined$head) {3130for(qw(commit commitdiff)) {3131$arg{$_}{'hash'} =$head;3132}3133if($current=~m/^(tree | log | shortlog | commit | commitdiff | search)$/x) {3134for(qw(shortlog log)) {3135$arg{$_}{'hash'} =$head;3136}3137}3138}31393140$arg{'tree'}{'hash'} =$treeheadifdefined$treehead;3141$arg{'tree'}{'hash_base'} =$treebaseifdefined$treebase;31423143my@actions= gitweb_get_feature('actions');3144my%repl= (3145'%'=>'%',3146'n'=>$project,# project name3147'f'=>$git_dir,# project path within filesystem3148'h'=>$treehead||'',# current hash ('h' parameter)3149'b'=>$treebase||'',# hash base ('hb' parameter)3150);3151while(@actions) {3152my($label,$link,$pos) =splice(@actions,0,3);3153# insert3154@navs=map{$_eq$pos? ($_,$label) :$_}@navs;3155# munch munch3156$link=~s/%([%nfhb])/$repl{$1}/g;3157$arg{$label}{'_href'} =$link;3158}31593160print"<div class=\"page_nav\">\n".3161(join" | ",3162map{$_eq$current?3163$_:$cgi->a({-href => ($arg{$_}{_href} ?$arg{$_}{_href} : href(%{$arg{$_}}))},"$_")3164}@navs);3165print"<br/>\n$extra<br/>\n".3166"</div>\n";3167}31683169sub format_paging_nav {3170my($action,$hash,$head,$page,$has_next_link) =@_;3171my$paging_nav;317231733174if($hashne$head||$page) {3175$paging_nav.=$cgi->a({-href => href(action=>$action)},"HEAD");3176}else{3177$paging_nav.="HEAD";3178}31793180if($page>0) {3181$paging_nav.=" ⋅ ".3182$cgi->a({-href => href(-replay=>1, page=>$page-1),3183-accesskey =>"p", -title =>"Alt-p"},"prev");3184}else{3185$paging_nav.=" ⋅ prev";3186}31873188if($has_next_link) {3189$paging_nav.=" ⋅ ".3190$cgi->a({-href => href(-replay=>1, page=>$page+1),3191-accesskey =>"n", -title =>"Alt-n"},"next");3192}else{3193$paging_nav.=" ⋅ next";3194}31953196return$paging_nav;3197}31983199## ......................................................................3200## functions printing or outputting HTML: div32013202sub git_print_header_div {3203my($action,$title,$hash,$hash_base) =@_;3204my%args= ();32053206$args{'action'} =$action;3207$args{'hash'} =$hashif$hash;3208$args{'hash_base'} =$hash_baseif$hash_base;32093210print"<div class=\"header\">\n".3211$cgi->a({-href => href(%args), -class=>"title"},3212$title?$title:$action) .3213"\n</div>\n";3214}32153216#sub git_print_authorship (\%) {3217sub git_print_authorship {3218my$co=shift;32193220my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3221print"<div class=\"author_date\">".3222 esc_html($co->{'author_name'}) .3223" [$ad{'rfc2822'}";3224if($ad{'hour_local'} <6) {3225printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3226$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3227}else{3228printf(" (%02d:%02d%s)",3229$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3230}3231print"]</div>\n";3232}32333234sub git_print_page_path {3235my$name=shift;3236my$type=shift;3237my$hb=shift;323832393240print"<div class=\"page_path\">";3241print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3242-title =>'tree root'}, to_utf8("[$project]"));3243print" / ";3244if(defined$name) {3245my@dirname=split'/',$name;3246my$basename=pop@dirname;3247my$fullname='';32483249foreachmy$dir(@dirname) {3250$fullname.= ($fullname?'/':'') .$dir;3251print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3252 hash_base=>$hb),3253-title =>$fullname}, esc_path($dir));3254print" / ";3255}3256if(defined$type&&$typeeq'blob') {3257print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3258 hash_base=>$hb),3259-title =>$name}, esc_path($basename));3260}elsif(defined$type&&$typeeq'tree') {3261print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3262 hash_base=>$hb),3263-title =>$name}, esc_path($basename));3264print" / ";3265}else{3266print esc_path($basename);3267}3268}3269print"<br/></div>\n";3270}32713272# sub git_print_log (\@;%) {3273sub git_print_log ($;%) {3274my$log=shift;3275my%opts=@_;32763277if($opts{'-remove_title'}) {3278# remove title, i.e. first line of log3279shift@$log;3280}3281# remove leading empty lines3282while(defined$log->[0] &&$log->[0]eq"") {3283shift@$log;3284}32853286# print log3287my$signoff=0;3288my$empty=0;3289foreachmy$line(@$log) {3290if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3291$signoff=1;3292$empty=0;3293if(!$opts{'-remove_signoff'}) {3294print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3295next;3296}else{3297# remove signoff lines3298next;3299}3300}else{3301$signoff=0;3302}33033304# print only one empty line3305# do not print empty line after signoff3306if($lineeq"") {3307next if($empty||$signoff);3308$empty=1;3309}else{3310$empty=0;3311}33123313print format_log_line_html($line) ."<br/>\n";3314}33153316if($opts{'-final_empty_line'}) {3317# end with single empty line3318print"<br/>\n"unless$empty;3319}3320}33213322# return link target (what link points to)3323sub git_get_link_target {3324my$hash=shift;3325my$link_target;33263327# read link3328open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3329orreturn;3330{3331local$/;3332$link_target= <$fd>;3333}3334close$fd3335orreturn;33363337return$link_target;3338}33393340# given link target, and the directory (basedir) the link is in,3341# return target of link relative to top directory (top tree);3342# return undef if it is not possible (including absolute links).3343sub normalize_link_target {3344my($link_target,$basedir,$hash_base) =@_;33453346# we can normalize symlink target only if $hash_base is provided3347return unless$hash_base;33483349# absolute symlinks (beginning with '/') cannot be normalized3350return if(substr($link_target,0,1)eq'/');33513352# normalize link target to path from top (root) tree (dir)3353my$path;3354if($basedir) {3355$path=$basedir.'/'.$link_target;3356}else{3357# we are in top (root) tree (dir)3358$path=$link_target;3359}33603361# remove //, /./, and /../3362my@path_parts;3363foreachmy$part(split('/',$path)) {3364# discard '.' and ''3365next if(!$part||$parteq'.');3366# handle '..'3367if($parteq'..') {3368if(@path_parts) {3369pop@path_parts;3370}else{3371# link leads outside repository (outside top dir)3372return;3373}3374}else{3375push@path_parts,$part;3376}3377}3378$path=join('/',@path_parts);33793380return$path;3381}33823383# print tree entry (row of git_tree), but without encompassing <tr> element3384sub git_print_tree_entry {3385my($t,$basedir,$hash_base,$have_blame) =@_;33863387my%base_key= ();3388$base_key{'hash_base'} =$hash_baseifdefined$hash_base;33893390# The format of a table row is: mode list link. Where mode is3391# the mode of the entry, list is the name of the entry, an href,3392# and link is the action links of the entry.33933394print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3395if($t->{'type'}eq"blob") {3396print"<td class=\"list\">".3397$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3398 file_name=>"$basedir$t->{'name'}",%base_key),3399-class=>"list"}, esc_path($t->{'name'}));3400if(S_ISLNK(oct$t->{'mode'})) {3401my$link_target= git_get_link_target($t->{'hash'});3402if($link_target) {3403my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3404if(defined$norm_target) {3405print" -> ".3406$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3407 file_name=>$norm_target),3408-title =>$norm_target}, esc_path($link_target));3409}else{3410print" -> ". esc_path($link_target);3411}3412}3413}3414print"</td>\n";3415print"<td class=\"link\">";3416print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3417 file_name=>"$basedir$t->{'name'}",%base_key)},3418"blob");3419if($have_blame) {3420print" | ".3421$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3422 file_name=>"$basedir$t->{'name'}",%base_key)},3423"blame");3424}3425if(defined$hash_base) {3426print" | ".3427$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3428 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3429"history");3430}3431print" | ".3432$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3433 file_name=>"$basedir$t->{'name'}")},3434"raw");3435print"</td>\n";34363437}elsif($t->{'type'}eq"tree") {3438print"<td class=\"list\">";3439print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3440 file_name=>"$basedir$t->{'name'}",%base_key)},3441 esc_path($t->{'name'}));3442print"</td>\n";3443print"<td class=\"link\">";3444print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3445 file_name=>"$basedir$t->{'name'}",%base_key)},3446"tree");3447if(defined$hash_base) {3448print" | ".3449$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3450 file_name=>"$basedir$t->{'name'}")},3451"history");3452}3453print"</td>\n";3454}else{3455# unknown object: we can only present history for it3456# (this includes 'commit' object, i.e. submodule support)3457print"<td class=\"list\">".3458 esc_path($t->{'name'}) .3459"</td>\n";3460print"<td class=\"link\">";3461if(defined$hash_base) {3462print$cgi->a({-href => href(action=>"history",3463 hash_base=>$hash_base,3464 file_name=>"$basedir$t->{'name'}")},3465"history");3466}3467print"</td>\n";3468}3469}34703471## ......................................................................3472## functions printing large fragments of HTML34733474# get pre-image filenames for merge (combined) diff3475sub fill_from_file_info {3476my($diff,@parents) =@_;34773478$diff->{'from_file'} = [ ];3479$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3480for(my$i=0;$i<$diff->{'nparents'};$i++) {3481if($diff->{'status'}[$i]eq'R'||3482$diff->{'status'}[$i]eq'C') {3483$diff->{'from_file'}[$i] =3484 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3485}3486}34873488return$diff;3489}34903491# is current raw difftree line of file deletion3492sub is_deleted {3493my$diffinfo=shift;34943495return$diffinfo->{'to_id'}eq('0' x 40);3496}34973498# does patch correspond to [previous] difftree raw line3499# $diffinfo - hashref of parsed raw diff format3500# $patchinfo - hashref of parsed patch diff format3501# (the same keys as in $diffinfo)3502sub is_patch_split {3503my($diffinfo,$patchinfo) =@_;35043505returndefined$diffinfo&&defined$patchinfo3506&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3507}350835093510sub git_difftree_body {3511my($difftree,$hash,@parents) =@_;3512my($parent) =$parents[0];3513my$have_blame= gitweb_check_feature('blame');3514print"<div class=\"list_head\">\n";3515if($#{$difftree} >10) {3516print(($#{$difftree} +1) ." files changed:\n");3517}3518print"</div>\n";35193520print"<table class=\"".3521(@parents>1?"combined ":"") .3522"diff_tree\">\n";35233524# header only for combined diff in 'commitdiff' view3525my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3526if($has_header) {3527# table header3528print"<thead><tr>\n".3529"<th></th><th></th>\n";# filename, patchN link3530for(my$i=0;$i<@parents;$i++) {3531my$par=$parents[$i];3532print"<th>".3533$cgi->a({-href => href(action=>"commitdiff",3534 hash=>$hash, hash_parent=>$par),3535-title =>'commitdiff to parent number '.3536($i+1) .': '.substr($par,0,7)},3537$i+1) .3538" </th>\n";3539}3540print"</tr></thead>\n<tbody>\n";3541}35423543my$alternate=1;3544my$patchno=0;3545foreachmy$line(@{$difftree}) {3546my$diff= parsed_difftree_line($line);35473548if($alternate) {3549print"<tr class=\"dark\">\n";3550}else{3551print"<tr class=\"light\">\n";3552}3553$alternate^=1;35543555if(exists$diff->{'nparents'}) {# combined diff35563557 fill_from_file_info($diff,@parents)3558unlessexists$diff->{'from_file'};35593560if(!is_deleted($diff)) {3561# file exists in the result (child) commit3562print"<td>".3563$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3564 file_name=>$diff->{'to_file'},3565 hash_base=>$hash),3566-class=>"list"}, esc_path($diff->{'to_file'})) .3567"</td>\n";3568}else{3569print"<td>".3570 esc_path($diff->{'to_file'}) .3571"</td>\n";3572}35733574if($actioneq'commitdiff') {3575# link to patch3576$patchno++;3577print"<td class=\"link\">".3578$cgi->a({-href =>"#patch$patchno"},"patch") .3579" | ".3580"</td>\n";3581}35823583my$has_history=0;3584my$not_deleted=0;3585for(my$i=0;$i<$diff->{'nparents'};$i++) {3586my$hash_parent=$parents[$i];3587my$from_hash=$diff->{'from_id'}[$i];3588my$from_path=$diff->{'from_file'}[$i];3589my$status=$diff->{'status'}[$i];35903591$has_history||= ($statusne'A');3592$not_deleted||= ($statusne'D');35933594if($statuseq'A') {3595print"<td class=\"link\"align=\"right\"> | </td>\n";3596}elsif($statuseq'D') {3597print"<td class=\"link\">".3598$cgi->a({-href => href(action=>"blob",3599 hash_base=>$hash,3600 hash=>$from_hash,3601 file_name=>$from_path)},3602"blob". ($i+1)) .3603" | </td>\n";3604}else{3605if($diff->{'to_id'}eq$from_hash) {3606print"<td class=\"link nochange\">";3607}else{3608print"<td class=\"link\">";3609}3610print$cgi->a({-href => href(action=>"blobdiff",3611 hash=>$diff->{'to_id'},3612 hash_parent=>$from_hash,3613 hash_base=>$hash,3614 hash_parent_base=>$hash_parent,3615 file_name=>$diff->{'to_file'},3616 file_parent=>$from_path)},3617"diff". ($i+1)) .3618" | </td>\n";3619}3620}36213622print"<td class=\"link\">";3623if($not_deleted) {3624print$cgi->a({-href => href(action=>"blob",3625 hash=>$diff->{'to_id'},3626 file_name=>$diff->{'to_file'},3627 hash_base=>$hash)},3628"blob");3629print" | "if($has_history);3630}3631if($has_history) {3632print$cgi->a({-href => href(action=>"history",3633 file_name=>$diff->{'to_file'},3634 hash_base=>$hash)},3635"history");3636}3637print"</td>\n";36383639print"</tr>\n";3640next;# instead of 'else' clause, to avoid extra indent3641}3642# else ordinary diff36433644my($to_mode_oct,$to_mode_str,$to_file_type);3645my($from_mode_oct,$from_mode_str,$from_file_type);3646if($diff->{'to_mode'}ne('0' x 6)) {3647$to_mode_oct=oct$diff->{'to_mode'};3648if(S_ISREG($to_mode_oct)) {# only for regular file3649$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3650}3651$to_file_type= file_type($diff->{'to_mode'});3652}3653if($diff->{'from_mode'}ne('0' x 6)) {3654$from_mode_oct=oct$diff->{'from_mode'};3655if(S_ISREG($to_mode_oct)) {# only for regular file3656$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3657}3658$from_file_type= file_type($diff->{'from_mode'});3659}36603661if($diff->{'status'}eq"A") {# created3662my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3663$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3664$mode_chng.="]</span>";3665print"<td>";3666print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3667 hash_base=>$hash, file_name=>$diff->{'file'}),3668-class=>"list"}, esc_path($diff->{'file'}));3669print"</td>\n";3670print"<td>$mode_chng</td>\n";3671print"<td class=\"link\">";3672if($actioneq'commitdiff') {3673# link to patch3674$patchno++;3675print$cgi->a({-href =>"#patch$patchno"},"patch");3676print" | ";3677}3678print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3679 hash_base=>$hash, file_name=>$diff->{'file'})},3680"blob");3681print"</td>\n";36823683}elsif($diff->{'status'}eq"D") {# deleted3684my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3685print"<td>";3686print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3687 hash_base=>$parent, file_name=>$diff->{'file'}),3688-class=>"list"}, esc_path($diff->{'file'}));3689print"</td>\n";3690print"<td>$mode_chng</td>\n";3691print"<td class=\"link\">";3692if($actioneq'commitdiff') {3693# link to patch3694$patchno++;3695print$cgi->a({-href =>"#patch$patchno"},"patch");3696print" | ";3697}3698print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3699 hash_base=>$parent, file_name=>$diff->{'file'})},3700"blob") ." | ";3701if($have_blame) {3702print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3703 file_name=>$diff->{'file'})},3704"blame") ." | ";3705}3706print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3707 file_name=>$diff->{'file'})},3708"history");3709print"</td>\n";37103711}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3712my$mode_chnge="";3713if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3714$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3715if($from_file_typene$to_file_type) {3716$mode_chnge.=" from$from_file_typeto$to_file_type";3717}3718if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3719if($from_mode_str&&$to_mode_str) {3720$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3721}elsif($to_mode_str) {3722$mode_chnge.=" mode:$to_mode_str";3723}3724}3725$mode_chnge.="]</span>\n";3726}3727print"<td>";3728print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3729 hash_base=>$hash, file_name=>$diff->{'file'}),3730-class=>"list"}, esc_path($diff->{'file'}));3731print"</td>\n";3732print"<td>$mode_chnge</td>\n";3733print"<td class=\"link\">";3734if($actioneq'commitdiff') {3735# link to patch3736$patchno++;3737print$cgi->a({-href =>"#patch$patchno"},"patch") .3738" | ";3739}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3740# "commit" view and modified file (not onlu mode changed)3741print$cgi->a({-href => href(action=>"blobdiff",3742 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3743 hash_base=>$hash, hash_parent_base=>$parent,3744 file_name=>$diff->{'file'})},3745"diff") .3746" | ";3747}3748print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3749 hash_base=>$hash, file_name=>$diff->{'file'})},3750"blob") ." | ";3751if($have_blame) {3752print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3753 file_name=>$diff->{'file'})},3754"blame") ." | ";3755}3756print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3757 file_name=>$diff->{'file'})},3758"history");3759print"</td>\n";37603761}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3762my%status_name= ('R'=>'moved','C'=>'copied');3763my$nstatus=$status_name{$diff->{'status'}};3764my$mode_chng="";3765if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3766# mode also for directories, so we cannot use $to_mode_str3767$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3768}3769print"<td>".3770$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3771 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3772-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3773"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3774$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3775 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3776-class=>"list"}, esc_path($diff->{'from_file'})) .3777" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3778"<td class=\"link\">";3779if($actioneq'commitdiff') {3780# link to patch3781$patchno++;3782print$cgi->a({-href =>"#patch$patchno"},"patch") .3783" | ";3784}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3785# "commit" view and modified file (not only pure rename or copy)3786print$cgi->a({-href => href(action=>"blobdiff",3787 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3788 hash_base=>$hash, hash_parent_base=>$parent,3789 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3790"diff") .3791" | ";3792}3793print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3794 hash_base=>$parent, file_name=>$diff->{'to_file'})},3795"blob") ." | ";3796if($have_blame) {3797print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3798 file_name=>$diff->{'to_file'})},3799"blame") ." | ";3800}3801print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3802 file_name=>$diff->{'to_file'})},3803"history");3804print"</td>\n";38053806}# we should not encounter Unmerged (U) or Unknown (X) status3807print"</tr>\n";3808}3809print"</tbody>"if$has_header;3810print"</table>\n";3811}38123813sub git_patchset_body {3814my($fd,$difftree,$hash,@hash_parents) =@_;3815my($hash_parent) =$hash_parents[0];38163817my$is_combined= (@hash_parents>1);3818my$patch_idx=0;3819my$patch_number=0;3820my$patch_line;3821my$diffinfo;3822my$to_name;3823my(%from,%to);38243825print"<div class=\"patchset\">\n";38263827# skip to first patch3828while($patch_line= <$fd>) {3829chomp$patch_line;38303831last if($patch_line=~m/^diff /);3832}38333834 PATCH:3835while($patch_line) {38363837# parse "git diff" header line3838if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3839# $1 is from_name, which we do not use3840$to_name= unquote($2);3841$to_name=~s!^b/!!;3842}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3843# $1 is 'cc' or 'combined', which we do not use3844$to_name= unquote($2);3845}else{3846$to_name=undef;3847}38483849# check if current patch belong to current raw line3850# and parse raw git-diff line if needed3851if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3852# this is continuation of a split patch3853print"<div class=\"patch cont\">\n";3854}else{3855# advance raw git-diff output if needed3856$patch_idx++ifdefined$diffinfo;38573858# read and prepare patch information3859$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38603861# compact combined diff output can have some patches skipped3862# find which patch (using pathname of result) we are at now;3863if($is_combined) {3864while($to_namene$diffinfo->{'to_file'}) {3865print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3866 format_diff_cc_simplified($diffinfo,@hash_parents) .3867"</div>\n";# class="patch"38683869$patch_idx++;3870$patch_number++;38713872last if$patch_idx>$#$difftree;3873$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3874}3875}38763877# modifies %from, %to hashes3878 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);38793880# this is first patch for raw difftree line with $patch_idx index3881# we index @$difftree array from 0, but number patches from 13882print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3883}38843885# git diff header3886#assert($patch_line =~ m/^diff /) if DEBUG;3887#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3888$patch_number++;3889# print "git diff" header3890print format_git_diff_header_line($patch_line,$diffinfo,3891 \%from, \%to);38923893# print extended diff header3894print"<div class=\"diff extended_header\">\n";3895 EXTENDED_HEADER:3896while($patch_line= <$fd>) {3897chomp$patch_line;38983899last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);39003901print format_extended_diff_header_line($patch_line,$diffinfo,3902 \%from, \%to);3903}3904print"</div>\n";# class="diff extended_header"39053906# from-file/to-file diff header3907if(!$patch_line) {3908print"</div>\n";# class="patch"3909last PATCH;3910}3911next PATCH if($patch_line=~m/^diff /);3912#assert($patch_line =~ m/^---/) if DEBUG;39133914my$last_patch_line=$patch_line;3915$patch_line= <$fd>;3916chomp$patch_line;3917#assert($patch_line =~ m/^\+\+\+/) if DEBUG;39183919print format_diff_from_to_header($last_patch_line,$patch_line,3920$diffinfo, \%from, \%to,3921@hash_parents);39223923# the patch itself3924 LINE:3925while($patch_line= <$fd>) {3926chomp$patch_line;39273928next PATCH if($patch_line=~m/^diff /);39293930print format_diff_line($patch_line, \%from, \%to);3931}39323933}continue{3934print"</div>\n";# class="patch"3935}39363937# for compact combined (--cc) format, with chunk and patch simpliciaction3938# patchset might be empty, but there might be unprocessed raw lines3939for(++$patch_idxif$patch_number>0;3940$patch_idx<@$difftree;3941++$patch_idx) {3942# read and prepare patch information3943$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);39443945# generate anchor for "patch" links in difftree / whatchanged part3946print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3947 format_diff_cc_simplified($diffinfo,@hash_parents) .3948"</div>\n";# class="patch"39493950$patch_number++;3951}39523953if($patch_number==0) {3954if(@hash_parents>1) {3955print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3956}else{3957print"<div class=\"diff nodifferences\">No differences found</div>\n";3958}3959}39603961print"</div>\n";# class="patchset"3962}39633964# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39653966# fills project list info (age, description, owner, forks) for each3967# project in the list, removing invalid projects from returned list3968# NOTE: modifies $projlist, but does not remove entries from it3969sub fill_project_list_info {3970my($projlist,$check_forks) =@_;3971my@projects;39723973my$show_ctags= gitweb_check_feature('ctags');3974 PROJECT:3975foreachmy$pr(@$projlist) {3976my(@activity) = git_get_last_activity($pr->{'path'});3977unless(@activity) {3978next PROJECT;3979}3980($pr->{'age'},$pr->{'age_string'}) =@activity;3981if(!defined$pr->{'descr'}) {3982my$descr= git_get_project_description($pr->{'path'}) ||"";3983$descr= to_utf8($descr);3984$pr->{'descr_long'} =$descr;3985$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3986}3987if(!defined$pr->{'owner'}) {3988$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3989}3990if($check_forks) {3991my$pname=$pr->{'path'};3992if(($pname=~s/\.git$//) &&3993($pname!~/\/$/) &&3994(-d "$projectroot/$pname")) {3995$pr->{'forks'} ="-d$projectroot/$pname";3996}else{3997$pr->{'forks'} =0;3998}3999}4000$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});4001push@projects,$pr;4002}40034004return@projects;4005}40064007# print 'sort by' <th> element, generating 'sort by $name' replay link4008# if that order is not selected4009sub print_sort_th {4010my($name,$order,$header) =@_;4011$header||=ucfirst($name);40124013if($ordereq$name) {4014print"<th>$header</th>\n";4015}else{4016print"<th>".4017$cgi->a({-href => href(-replay=>1, order=>$name),4018-class=>"header"},$header) .4019"</th>\n";4020}4021}40224023sub git_project_list_body {4024# actually uses global variable $project4025my($projlist,$order,$from,$to,$extra,$no_header) =@_;40264027my$check_forks= gitweb_check_feature('forks');4028my@projects= fill_project_list_info($projlist,$check_forks);40294030$order||=$default_projects_order;4031$from=0unlessdefined$from;4032$to=$#projectsif(!defined$to||$#projects<$to);40334034my%order_info= (4035 project => { key =>'path', type =>'str'},4036 descr => { key =>'descr_long', type =>'str'},4037 owner => { key =>'owner', type =>'str'},4038 age => { key =>'age', type =>'num'}4039);4040my$oi=$order_info{$order};4041if($oi->{'type'}eq'str') {4042@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4043}else{4044@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4045}40464047my$show_ctags= gitweb_check_feature('ctags');4048if($show_ctags) {4049my%ctags;4050foreachmy$p(@projects) {4051foreachmy$ct(keys%{$p->{'ctags'}}) {4052$ctags{$ct} +=$p->{'ctags'}->{$ct};4053}4054}4055my$cloud= git_populate_project_tagcloud(\%ctags);4056print git_show_project_tagcloud($cloud,64);4057}40584059print"<table class=\"project_list\">\n";4060unless($no_header) {4061print"<tr>\n";4062if($check_forks) {4063print"<th></th>\n";4064}4065 print_sort_th('project',$order,'Project');4066 print_sort_th('descr',$order,'Description');4067 print_sort_th('owner',$order,'Owner');4068 print_sort_th('age',$order,'Last Change');4069print"<th></th>\n".# for links4070"</tr>\n";4071}4072my$alternate=1;4073my$tagfilter=$cgi->param('by_tag');4074for(my$i=$from;$i<=$to;$i++) {4075my$pr=$projects[$i];40764077next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4078next if$searchtextand not$pr->{'path'} =~/$searchtext/4079and not$pr->{'descr_long'} =~/$searchtext/;4080# Weed out forks or non-matching entries of search4081if($check_forks) {4082my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4083$forkbase="^$forkbase"if$forkbase;4084next ifnot$searchtextand not$tagfilterand$show_ctags4085and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4086}40874088if($alternate) {4089print"<tr class=\"dark\">\n";4090}else{4091print"<tr class=\"light\">\n";4092}4093$alternate^=1;4094if($check_forks) {4095print"<td>";4096if($pr->{'forks'}) {4097print"<!--$pr->{'forks'} -->\n";4098print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4099}4100print"</td>\n";4101}4102print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4103-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4104"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4105-class=>"list", -title =>$pr->{'descr_long'}},4106 esc_html($pr->{'descr'})) ."</td>\n".4107"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4108print"<td class=\"". age_class($pr->{'age'}) ."\">".4109(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4110"<td class=\"link\">".4111$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4112$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4113$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4114$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4115($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4116"</td>\n".4117"</tr>\n";4118}4119if(defined$extra) {4120print"<tr>\n";4121if($check_forks) {4122print"<td></td>\n";4123}4124print"<td colspan=\"5\">$extra</td>\n".4125"</tr>\n";4126}4127print"</table>\n";4128}41294130sub git_shortlog_body {4131# uses global variable $project4132my($commitlist,$from,$to,$refs,$extra) =@_;41334134$from=0unlessdefined$from;4135$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41364137print"<table class=\"shortlog\">\n";4138my$alternate=1;4139for(my$i=$from;$i<=$to;$i++) {4140my%co= %{$commitlist->[$i]};4141my$commit=$co{'id'};4142my$ref= format_ref_marker($refs,$commit);4143if($alternate) {4144print"<tr class=\"dark\">\n";4145}else{4146print"<tr class=\"light\">\n";4147}4148$alternate^=1;4149my$author= chop_and_escape_str($co{'author_name'},10);4150# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4151print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4152"<td><i>".$author."</i></td>\n".4153"<td>";4154print format_subject_html($co{'title'},$co{'title_short'},4155 href(action=>"commit", hash=>$commit),$ref);4156print"</td>\n".4157"<td class=\"link\">".4158$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4159$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4160$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4161my$snapshot_links= format_snapshot_links($commit);4162if(defined$snapshot_links) {4163print" | ".$snapshot_links;4164}4165print"</td>\n".4166"</tr>\n";4167}4168if(defined$extra) {4169print"<tr>\n".4170"<td colspan=\"4\">$extra</td>\n".4171"</tr>\n";4172}4173print"</table>\n";4174}41754176sub git_history_body {4177# Warning: assumes constant type (blob or tree) during history4178my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;41794180$from=0unlessdefined$from;4181$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});41824183print"<table class=\"history\">\n";4184my$alternate=1;4185for(my$i=$from;$i<=$to;$i++) {4186my%co= %{$commitlist->[$i]};4187if(!%co) {4188next;4189}4190my$commit=$co{'id'};41914192my$ref= format_ref_marker($refs,$commit);41934194if($alternate) {4195print"<tr class=\"dark\">\n";4196}else{4197print"<tr class=\"light\">\n";4198}4199$alternate^=1;4200# shortlog uses chop_str($co{'author_name'}, 10)4201my$author= chop_and_escape_str($co{'author_name'},15,3);4202print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4203"<td><i>".$author."</i></td>\n".4204"<td>";4205# originally git_history used chop_str($co{'title'}, 50)4206print format_subject_html($co{'title'},$co{'title_short'},4207 href(action=>"commit", hash=>$commit),$ref);4208print"</td>\n".4209"<td class=\"link\">".4210$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4211$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");42124213if($ftypeeq'blob') {4214my$blob_current= git_get_hash_by_path($hash_base,$file_name);4215my$blob_parent= git_get_hash_by_path($commit,$file_name);4216if(defined$blob_current&&defined$blob_parent&&4217$blob_currentne$blob_parent) {4218print" | ".4219$cgi->a({-href => href(action=>"blobdiff",4220 hash=>$blob_current, hash_parent=>$blob_parent,4221 hash_base=>$hash_base, hash_parent_base=>$commit,4222 file_name=>$file_name)},4223"diff to current");4224}4225}4226print"</td>\n".4227"</tr>\n";4228}4229if(defined$extra) {4230print"<tr>\n".4231"<td colspan=\"4\">$extra</td>\n".4232"</tr>\n";4233}4234print"</table>\n";4235}42364237sub git_tags_body {4238# uses global variable $project4239my($taglist,$from,$to,$extra) =@_;4240$from=0unlessdefined$from;4241$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);42424243print"<table class=\"tags\">\n";4244my$alternate=1;4245for(my$i=$from;$i<=$to;$i++) {4246my$entry=$taglist->[$i];4247my%tag=%$entry;4248my$comment=$tag{'subject'};4249my$comment_short;4250if(defined$comment) {4251$comment_short= chop_str($comment,30,5);4252}4253if($alternate) {4254print"<tr class=\"dark\">\n";4255}else{4256print"<tr class=\"light\">\n";4257}4258$alternate^=1;4259if(defined$tag{'age'}) {4260print"<td><i>$tag{'age'}</i></td>\n";4261}else{4262print"<td></td>\n";4263}4264print"<td>".4265$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4266-class=>"list name"}, esc_html($tag{'name'})) .4267"</td>\n".4268"<td>";4269if(defined$comment) {4270print format_subject_html($comment,$comment_short,4271 href(action=>"tag", hash=>$tag{'id'}));4272}4273print"</td>\n".4274"<td class=\"selflink\">";4275if($tag{'type'}eq"tag") {4276print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4277}else{4278print" ";4279}4280print"</td>\n".4281"<td class=\"link\">"." | ".4282$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4283if($tag{'reftype'}eq"commit") {4284print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4285" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4286}elsif($tag{'reftype'}eq"blob") {4287print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4288}4289print"</td>\n".4290"</tr>";4291}4292if(defined$extra) {4293print"<tr>\n".4294"<td colspan=\"5\">$extra</td>\n".4295"</tr>\n";4296}4297print"</table>\n";4298}42994300sub git_heads_body {4301# uses global variable $project4302my($headlist,$head,$from,$to,$extra) =@_;4303$from=0unlessdefined$from;4304$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);43054306print"<table class=\"heads\">\n";4307my$alternate=1;4308for(my$i=$from;$i<=$to;$i++) {4309my$entry=$headlist->[$i];4310my%ref=%$entry;4311my$curr=$ref{'id'}eq$head;4312if($alternate) {4313print"<tr class=\"dark\">\n";4314}else{4315print"<tr class=\"light\">\n";4316}4317$alternate^=1;4318print"<td><i>$ref{'age'}</i></td>\n".4319($curr?"<td class=\"current_head\">":"<td>") .4320$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4321-class=>"list name"},esc_html($ref{'name'})) .4322"</td>\n".4323"<td class=\"link\">".4324$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4325$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4326$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4327"</td>\n".4328"</tr>";4329}4330if(defined$extra) {4331print"<tr>\n".4332"<td colspan=\"3\">$extra</td>\n".4333"</tr>\n";4334}4335print"</table>\n";4336}43374338sub git_search_grep_body {4339my($commitlist,$from,$to,$extra) =@_;4340$from=0unlessdefined$from;4341$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43424343print"<table class=\"commit_search\">\n";4344my$alternate=1;4345for(my$i=$from;$i<=$to;$i++) {4346my%co= %{$commitlist->[$i]};4347if(!%co) {4348next;4349}4350my$commit=$co{'id'};4351if($alternate) {4352print"<tr class=\"dark\">\n";4353}else{4354print"<tr class=\"light\">\n";4355}4356$alternate^=1;4357my$author= chop_and_escape_str($co{'author_name'},15,5);4358print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4359"<td><i>".$author."</i></td>\n".4360"<td>".4361$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4362-class=>"list subject"},4363 chop_and_escape_str($co{'title'},50) ."<br/>");4364my$comment=$co{'comment'};4365foreachmy$line(@$comment) {4366if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4367my($lead,$match,$trail) = ($1,$2,$3);4368$match= chop_str($match,70,5,'center');4369my$contextlen=int((80-length($match))/2);4370$contextlen=30if($contextlen>30);4371$lead= chop_str($lead,$contextlen,10,'left');4372$trail= chop_str($trail,$contextlen,10,'right');43734374$lead= esc_html($lead);4375$match= esc_html($match);4376$trail= esc_html($trail);43774378print"$lead<span class=\"match\">$match</span>$trail<br />";4379}4380}4381print"</td>\n".4382"<td class=\"link\">".4383$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4384" | ".4385$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4386" | ".4387$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4388print"</td>\n".4389"</tr>\n";4390}4391if(defined$extra) {4392print"<tr>\n".4393"<td colspan=\"3\">$extra</td>\n".4394"</tr>\n";4395}4396print"</table>\n";4397}43984399## ======================================================================4400## ======================================================================4401## actions44024403sub git_project_list {4404my$order=$input_params{'order'};4405if(defined$order&&$order!~m/none|project|descr|owner|age/) {4406 die_error(400,"Unknown order parameter");4407}44084409my@list= git_get_projects_list();4410if(!@list) {4411 die_error(404,"No projects found");4412}44134414 git_header_html();4415if(-f $home_text) {4416print"<div class=\"index_include\">\n";4417 insert_file($home_text);4418print"</div>\n";4419}4420print$cgi->startform(-method=>"get") .4421"<p class=\"projsearch\">Search:\n".4422$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4423"</p>".4424$cgi->end_form() ."\n";4425 git_project_list_body(\@list,$order);4426 git_footer_html();4427}44284429sub git_forks {4430my$order=$input_params{'order'};4431if(defined$order&&$order!~m/none|project|descr|owner|age/) {4432 die_error(400,"Unknown order parameter");4433}44344435my@list= git_get_projects_list($project);4436if(!@list) {4437 die_error(404,"No forks found");4438}44394440 git_header_html();4441 git_print_page_nav('','');4442 git_print_header_div('summary',"$projectforks");4443 git_project_list_body(\@list,$order);4444 git_footer_html();4445}44464447sub git_project_index {4448my@projects= git_get_projects_list($project);44494450print$cgi->header(4451-type =>'text/plain',4452-charset =>'utf-8',4453-content_disposition =>'inline; filename="index.aux"');44544455foreachmy$pr(@projects) {4456if(!exists$pr->{'owner'}) {4457$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4458}44594460my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4461# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4462$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4463$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4464$path=~s/ /\+/g;4465$owner=~s/ /\+/g;44664467print"$path$owner\n";4468}4469}44704471sub git_summary {4472my$descr= git_get_project_description($project) ||"none";4473my%co= parse_commit("HEAD");4474my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4475my$head=$co{'id'};44764477my$owner= git_get_project_owner($project);44784479my$refs= git_get_references();4480# These get_*_list functions return one more to allow us to see if4481# there are more ...4482my@taglist= git_get_tags_list(16);4483my@headlist= git_get_heads_list(16);4484my@forklist;4485my$check_forks= gitweb_check_feature('forks');44864487if($check_forks) {4488@forklist= git_get_projects_list($project);4489}44904491 git_header_html();4492 git_print_page_nav('summary','',$head);44934494print"<div class=\"title\"> </div>\n";4495print"<table class=\"projects_list\">\n".4496"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4497"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4498if(defined$cd{'rfc2822'}) {4499print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4500}45014502# use per project git URL list in $projectroot/$project/cloneurl4503# or make project git URL from git base URL and project name4504my$url_tag="URL";4505my@url_list= git_get_project_url_list($project);4506@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4507foreachmy$git_url(@url_list) {4508next unless$git_url;4509print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4510$url_tag="";4511}45124513# Tag cloud4514my$show_ctags= gitweb_check_feature('ctags');4515if($show_ctags) {4516my$ctags= git_get_project_ctags($project);4517my$cloud= git_populate_project_tagcloud($ctags);4518print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4519print"</td>\n<td>"unless%$ctags;4520print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4521print"</td>\n<td>"if%$ctags;4522print git_show_project_tagcloud($cloud,48);4523print"</td></tr>";4524}45254526print"</table>\n";45274528# If XSS prevention is on, we don't include README.html.4529# TODO: Allow a readme in some safe format.4530if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4531print"<div class=\"title\">readme</div>\n".4532"<div class=\"readme\">\n";4533 insert_file("$projectroot/$project/README.html");4534print"\n</div>\n";# class="readme"4535}45364537# we need to request one more than 16 (0..15) to check if4538# those 16 are all4539my@commitlist=$head? parse_commits($head,17) : ();4540if(@commitlist) {4541 git_print_header_div('shortlog');4542 git_shortlog_body(\@commitlist,0,15,$refs,4543$#commitlist<=15?undef:4544$cgi->a({-href => href(action=>"shortlog")},"..."));4545}45464547if(@taglist) {4548 git_print_header_div('tags');4549 git_tags_body(\@taglist,0,15,4550$#taglist<=15?undef:4551$cgi->a({-href => href(action=>"tags")},"..."));4552}45534554if(@headlist) {4555 git_print_header_div('heads');4556 git_heads_body(\@headlist,$head,0,15,4557$#headlist<=15?undef:4558$cgi->a({-href => href(action=>"heads")},"..."));4559}45604561if(@forklist) {4562 git_print_header_div('forks');4563 git_project_list_body(\@forklist,'age',0,15,4564$#forklist<=15?undef:4565$cgi->a({-href => href(action=>"forks")},"..."),4566'no_header');4567}45684569 git_footer_html();4570}45714572sub git_tag {4573my$head= git_get_head_hash($project);4574 git_header_html();4575 git_print_page_nav('','',$head,undef,$head);4576my%tag= parse_tag($hash);45774578if(!%tag) {4579 die_error(404,"Unknown tag object");4580}45814582 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4583print"<div class=\"title_text\">\n".4584"<table class=\"object_header\">\n".4585"<tr>\n".4586"<td>object</td>\n".4587"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4588$tag{'object'}) ."</td>\n".4589"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4590$tag{'type'}) ."</td>\n".4591"</tr>\n";4592if(defined($tag{'author'})) {4593my%ad= parse_date($tag{'epoch'},$tag{'tz'});4594print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4595print"<tr><td></td><td>".$ad{'rfc2822'} .4596sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4597"</td></tr>\n";4598}4599print"</table>\n\n".4600"</div>\n";4601print"<div class=\"page_body\">";4602my$comment=$tag{'comment'};4603foreachmy$line(@$comment) {4604chomp$line;4605print esc_html($line, -nbsp=>1) ."<br/>\n";4606}4607print"</div>\n";4608 git_footer_html();4609}46104611sub git_blame {4612# permissions4613 gitweb_check_feature('blame')4614or die_error(403,"Blame view not allowed");46154616# error checking4617 die_error(400,"No file name given")unless$file_name;4618$hash_base||= git_get_head_hash($project);4619 die_error(404,"Couldn't find base commit")unless$hash_base;4620my%co= parse_commit($hash_base)4621or die_error(404,"Commit not found");4622my$ftype="blob";4623if(!defined$hash) {4624$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4625or die_error(404,"Error looking up file");4626}else{4627$ftype= git_get_type($hash);4628if($ftype!~"blob") {4629 die_error(400,"Object is not a blob");4630}4631}46324633# run git-blame --porcelain4634open my$fd,"-|", git_cmd(),"blame",'-p',4635$hash_base,'--',$file_name4636or die_error(500,"Open git-blame failed");46374638# page header4639 git_header_html();4640my$formats_nav=4641$cgi->a({-href => href(action=>"blob", -replay=>1)},4642"blob") .4643" | ".4644$cgi->a({-href => href(action=>"history", -replay=>1)},4645"history") .4646" | ".4647$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4648"HEAD");4649 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4650 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4651 git_print_page_path($file_name,$ftype,$hash_base);46524653# page body4654my@rev_color=qw(light2 dark2);4655my$num_colors=scalar(@rev_color);4656my$current_color=0;4657my%metainfo= ();46584659print<<HTML;4660<div class="page_body">4661<table class="blame">4662<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4663HTML4664 LINE:4665while(my$line= <$fd>) {4666chomp$line;4667# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4668# no <lines in group> for subsequent lines in group of lines4669my($full_rev,$orig_lineno,$lineno,$group_size) =4670($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4671if(!exists$metainfo{$full_rev}) {4672$metainfo{$full_rev} = {};4673}4674my$meta=$metainfo{$full_rev};4675my$data;4676while($data= <$fd>) {4677chomp$data;4678last if($data=~s/^\t//);# contents of line4679if($data=~/^(\S+) (.*)$/) {4680$meta->{$1} =$2;4681}4682}4683my$short_rev=substr($full_rev,0,8);4684my$author=$meta->{'author'};4685my%date=4686 parse_date($meta->{'author-time'},$meta->{'author-tz'});4687my$date=$date{'iso-tz'};4688if($group_size) {4689$current_color= ($current_color+1) %$num_colors;4690}4691print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4692if($group_size) {4693print"<td class=\"sha1\"";4694print" title=\"". esc_html($author) .",$date\"";4695print" rowspan=\"$group_size\""if($group_size>1);4696print">";4697print$cgi->a({-href => href(action=>"commit",4698 hash=>$full_rev,4699 file_name=>$file_name)},4700 esc_html($short_rev));4701print"</td>\n";4702}4703my$parent_commit;4704if(!exists$meta->{'parent'}) {4705open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4706or die_error(500,"Open git-rev-parse failed");4707$parent_commit= <$dd>;4708close$dd;4709chomp($parent_commit);4710$meta->{'parent'} =$parent_commit;4711}else{4712$parent_commit=$meta->{'parent'};4713}4714my$blamed= href(action =>'blame',4715 file_name =>$meta->{'filename'},4716 hash_base =>$parent_commit);4717print"<td class=\"linenr\">";4718print$cgi->a({ -href =>"$blamed#l$orig_lineno",4719-class=>"linenr"},4720 esc_html($lineno));4721print"</td>";4722print"<td class=\"pre\">". esc_html($data) ."</td>\n";4723print"</tr>\n";4724}4725print"</table>\n";4726print"</div>";4727close$fd4728or print"Reading blob failed\n";47294730# page footer4731 git_footer_html();4732}47334734sub git_tags {4735my$head= git_get_head_hash($project);4736 git_header_html();4737 git_print_page_nav('','',$head,undef,$head);4738 git_print_header_div('summary',$project);47394740my@tagslist= git_get_tags_list();4741if(@tagslist) {4742 git_tags_body(\@tagslist);4743}4744 git_footer_html();4745}47464747sub git_heads {4748my$head= git_get_head_hash($project);4749 git_header_html();4750 git_print_page_nav('','',$head,undef,$head);4751 git_print_header_div('summary',$project);47524753my@headslist= git_get_heads_list();4754if(@headslist) {4755 git_heads_body(\@headslist,$head);4756}4757 git_footer_html();4758}47594760sub git_blob_plain {4761my$type=shift;4762my$expires;47634764if(!defined$hash) {4765if(defined$file_name) {4766my$base=$hash_base|| git_get_head_hash($project);4767$hash= git_get_hash_by_path($base,$file_name,"blob")4768or die_error(404,"Cannot find file");4769}else{4770 die_error(400,"No file name defined");4771}4772}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4773# blobs defined by non-textual hash id's can be cached4774$expires="+1d";4775}47764777open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4778or die_error(500,"Open git-cat-file blob '$hash' failed");47794780# content-type (can include charset)4781$type= blob_contenttype($fd,$file_name,$type);47824783# "save as" filename, even when no $file_name is given4784my$save_as="$hash";4785if(defined$file_name) {4786$save_as=$file_name;4787}elsif($type=~m/^text\//) {4788$save_as.='.txt';4789}47904791# With XSS prevention on, blobs of all types except a few known safe4792# ones are served with "Content-Disposition: attachment" to make sure4793# they don't run in our security domain. For certain image types,4794# blob view writes an <img> tag referring to blob_plain view, and we4795# want to be sure not to break that by serving the image as an4796# attachment (though Firefox 3 doesn't seem to care).4797my$sandbox=$prevent_xss&&4798$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;47994800print$cgi->header(4801-type =>$type,4802-expires =>$expires,4803-content_disposition =>4804($sandbox?'attachment':'inline')4805.'; filename="'.$save_as.'"');4806undef$/;4807binmode STDOUT,':raw';4808print<$fd>;4809binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4810$/="\n";4811close$fd;4812}48134814sub git_blob {4815my$expires;48164817if(!defined$hash) {4818if(defined$file_name) {4819my$base=$hash_base|| git_get_head_hash($project);4820$hash= git_get_hash_by_path($base,$file_name,"blob")4821or die_error(404,"Cannot find file");4822}else{4823 die_error(400,"No file name defined");4824}4825}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4826# blobs defined by non-textual hash id's can be cached4827$expires="+1d";4828}48294830my$have_blame= gitweb_check_feature('blame');4831open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4832or die_error(500,"Couldn't cat$file_name,$hash");4833my$mimetype= blob_mimetype($fd,$file_name);4834if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4835close$fd;4836return git_blob_plain($mimetype);4837}4838# we can have blame only for text/* mimetype4839$have_blame&&= ($mimetype=~m!^text/!);48404841 git_header_html(undef,$expires);4842my$formats_nav='';4843if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4844if(defined$file_name) {4845if($have_blame) {4846$formats_nav.=4847$cgi->a({-href => href(action=>"blame", -replay=>1)},4848"blame") .4849" | ";4850}4851$formats_nav.=4852$cgi->a({-href => href(action=>"history", -replay=>1)},4853"history") .4854" | ".4855$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4856"raw") .4857" | ".4858$cgi->a({-href => href(action=>"blob",4859 hash_base=>"HEAD", file_name=>$file_name)},4860"HEAD");4861}else{4862$formats_nav.=4863$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4864"raw");4865}4866 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4867 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4868}else{4869print"<div class=\"page_nav\">\n".4870"<br/><br/></div>\n".4871"<div class=\"title\">$hash</div>\n";4872}4873 git_print_page_path($file_name,"blob",$hash_base);4874print"<div class=\"page_body\">\n";4875if($mimetype=~m!^image/!) {4876print qq!<img type="$mimetype"!;4877if($file_name) {4878print qq! alt="$file_name" title="$file_name"!;4879}4880print qq! src="! .4881 href(action=>"blob_plain", hash=>$hash,4882 hash_base=>$hash_base, file_name=>$file_name) .4883 qq!"/>\n!;4884}else{4885my$nr;4886while(my$line= <$fd>) {4887chomp$line;4888$nr++;4889$line= untabify($line);4890printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4891$nr,$nr,$nr, esc_html($line, -nbsp=>1);4892}4893}4894close$fd4895or print"Reading blob failed.\n";4896print"</div>";4897 git_footer_html();4898}48994900sub git_tree {4901if(!defined$hash_base) {4902$hash_base="HEAD";4903}4904if(!defined$hash) {4905if(defined$file_name) {4906$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4907}else{4908$hash=$hash_base;4909}4910}4911 die_error(404,"No such tree")unlessdefined($hash);4912$/="\0";4913open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4914or die_error(500,"Open git-ls-tree failed");4915my@entries=map{chomp;$_} <$fd>;4916close$fdor die_error(404,"Reading tree failed");4917$/="\n";49184919my$refs= git_get_references();4920my$ref= format_ref_marker($refs,$hash_base);4921 git_header_html();4922my$basedir='';4923my$have_blame= gitweb_check_feature('blame');4924if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4925my@views_nav= ();4926if(defined$file_name) {4927push@views_nav,4928$cgi->a({-href => href(action=>"history", -replay=>1)},4929"history"),4930$cgi->a({-href => href(action=>"tree",4931 hash_base=>"HEAD", file_name=>$file_name)},4932"HEAD"),4933}4934my$snapshot_links= format_snapshot_links($hash);4935if(defined$snapshot_links) {4936# FIXME: Should be available when we have no hash base as well.4937push@views_nav,$snapshot_links;4938}4939 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4940 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4941}else{4942undef$hash_base;4943print"<div class=\"page_nav\">\n";4944print"<br/><br/></div>\n";4945print"<div class=\"title\">$hash</div>\n";4946}4947if(defined$file_name) {4948$basedir=$file_name;4949if($basedirne''&&substr($basedir, -1)ne'/') {4950$basedir.='/';4951}4952 git_print_page_path($file_name,'tree',$hash_base);4953}4954print"<div class=\"page_body\">\n";4955print"<table class=\"tree\">\n";4956my$alternate=1;4957# '..' (top directory) link if possible4958if(defined$hash_base&&4959defined$file_name&&$file_name=~m![^/]+$!) {4960if($alternate) {4961print"<tr class=\"dark\">\n";4962}else{4963print"<tr class=\"light\">\n";4964}4965$alternate^=1;49664967my$up=$file_name;4968$up=~s!/?[^/]+$!!;4969undef$upunless$up;4970# based on git_print_tree_entry4971print'<td class="mode">'. mode_str('040000') ."</td>\n";4972print'<td class="list">';4973print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4974 file_name=>$up)},4975"..");4976print"</td>\n";4977print"<td class=\"link\"></td>\n";49784979print"</tr>\n";4980}4981foreachmy$line(@entries) {4982my%t= parse_ls_tree_line($line, -z =>1);49834984if($alternate) {4985print"<tr class=\"dark\">\n";4986}else{4987print"<tr class=\"light\">\n";4988}4989$alternate^=1;49904991 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);49924993print"</tr>\n";4994}4995print"</table>\n".4996"</div>";4997 git_footer_html();4998}49995000sub git_snapshot {5001my$format=$input_params{'snapshot_format'};5002if(!@snapshot_fmts) {5003 die_error(403,"Snapshots not allowed");5004}5005# default to first supported snapshot format5006$format||=$snapshot_fmts[0];5007if($format!~m/^[a-z0-9]+$/) {5008 die_error(400,"Invalid snapshot format parameter");5009}elsif(!exists($known_snapshot_formats{$format})) {5010 die_error(400,"Unknown snapshot format");5011}elsif(!grep($_eq$format,@snapshot_fmts)) {5012 die_error(403,"Unsupported snapshot format");5013}50145015if(!defined$hash) {5016$hash= git_get_head_hash($project);5017}50185019my$name=$project;5020$name=~ s,([^/])/*\.git$,$1,;5021$name= basename($name);5022my$filename= to_utf8($name);5023$name=~s/\047/\047\\\047\047/g;5024my$cmd;5025$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5026$cmd= quote_command(5027 git_cmd(),'archive',5028"--format=$known_snapshot_formats{$format}{'format'}",5029"--prefix=$name/",$hash);5030if(exists$known_snapshot_formats{$format}{'compressor'}) {5031$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5032}50335034print$cgi->header(5035-type =>$known_snapshot_formats{$format}{'type'},5036-content_disposition =>'inline; filename="'."$filename".'"',5037-status =>'200 OK');50385039open my$fd,"-|",$cmd5040or die_error(500,"Execute git-archive failed");5041binmode STDOUT,':raw';5042print<$fd>;5043binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5044close$fd;5045}50465047sub git_log {5048my$head= git_get_head_hash($project);5049if(!defined$hash) {5050$hash=$head;5051}5052if(!defined$page) {5053$page=0;5054}5055my$refs= git_get_references();50565057my@commitlist= parse_commits($hash,101, (100*$page));50585059my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);50605061my($patch_max) = gitweb_get_feature('patches');5062if($patch_max) {5063if($patch_max<0||@commitlist<=$patch_max) {5064$paging_nav.=" ⋅ ".5065$cgi->a({-href => href(action=>"patches", -replay=>1)},5066"patches");5067}5068}50695070 git_header_html();5071 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);50725073if(!@commitlist) {5074my%co= parse_commit($hash);50755076 git_print_header_div('summary',$project);5077print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5078}5079my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5080for(my$i=0;$i<=$to;$i++) {5081my%co= %{$commitlist[$i]};5082next if!%co;5083my$commit=$co{'id'};5084my$ref= format_ref_marker($refs,$commit);5085my%ad= parse_date($co{'author_epoch'});5086 git_print_header_div('commit',5087"<span class=\"age\">$co{'age_string'}</span>".5088 esc_html($co{'title'}) .$ref,5089$commit);5090print"<div class=\"title_text\">\n".5091"<div class=\"log_link\">\n".5092$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5093" | ".5094$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5095" | ".5096$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5097"<br/>\n".5098"</div>\n".5099"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".5100"</div>\n";51015102print"<div class=\"log_body\">\n";5103 git_print_log($co{'comment'}, -final_empty_line=>1);5104print"</div>\n";5105}5106if($#commitlist>=100) {5107print"<div class=\"page_nav\">\n";5108print$cgi->a({-href => href(-replay=>1, page=>$page+1),5109-accesskey =>"n", -title =>"Alt-n"},"next");5110print"</div>\n";5111}5112 git_footer_html();5113}51145115sub git_commit {5116$hash||=$hash_base||"HEAD";5117my%co= parse_commit($hash)5118or die_error(404,"Unknown commit object");5119my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5120my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});51215122my$parent=$co{'parent'};5123my$parents=$co{'parents'};# listref51245125# we need to prepare $formats_nav before any parameter munging5126my$formats_nav;5127if(!defined$parent) {5128# --root commitdiff5129$formats_nav.='(initial)';5130}elsif(@$parents==1) {5131# single parent commit5132$formats_nav.=5133'(parent: '.5134$cgi->a({-href => href(action=>"commit",5135 hash=>$parent)},5136 esc_html(substr($parent,0,7))) .5137')';5138}else{5139# merge commit5140$formats_nav.=5141'(merge: '.5142join(' ',map{5143$cgi->a({-href => href(action=>"commit",5144 hash=>$_)},5145 esc_html(substr($_,0,7)));5146}@$parents) .5147')';5148}5149if(gitweb_check_feature('patches')) {5150$formats_nav.=" | ".5151$cgi->a({-href => href(action=>"patch", -replay=>1)},5152"patch");5153}51545155if(!defined$parent) {5156$parent="--root";5157}5158my@difftree;5159open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5160@diff_opts,5161(@$parents<=1?$parent:'-c'),5162$hash,"--"5163or die_error(500,"Open git-diff-tree failed");5164@difftree=map{chomp;$_} <$fd>;5165close$fdor die_error(404,"Reading git-diff-tree failed");51665167# non-textual hash id's can be cached5168my$expires;5169if($hash=~m/^[0-9a-fA-F]{40}$/) {5170$expires="+1d";5171}5172my$refs= git_get_references();5173my$ref= format_ref_marker($refs,$co{'id'});51745175 git_header_html(undef,$expires);5176 git_print_page_nav('commit','',5177$hash,$co{'tree'},$hash,5178$formats_nav);51795180if(defined$co{'parent'}) {5181 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5182}else{5183 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5184}5185print"<div class=\"title_text\">\n".5186"<table class=\"object_header\">\n";5187print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5188"<tr>".5189"<td></td><td>$ad{'rfc2822'}";5190if($ad{'hour_local'} <6) {5191printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5192$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5193}else{5194printf(" (%02d:%02d%s)",5195$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5196}5197print"</td>".5198"</tr>\n";5199print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5200print"<tr><td></td><td>$cd{'rfc2822'}".5201sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5202"</td></tr>\n";5203print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5204print"<tr>".5205"<td>tree</td>".5206"<td class=\"sha1\">".5207$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5208class=>"list"},$co{'tree'}) .5209"</td>".5210"<td class=\"link\">".5211$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5212"tree");5213my$snapshot_links= format_snapshot_links($hash);5214if(defined$snapshot_links) {5215print" | ".$snapshot_links;5216}5217print"</td>".5218"</tr>\n";52195220foreachmy$par(@$parents) {5221print"<tr>".5222"<td>parent</td>".5223"<td class=\"sha1\">".5224$cgi->a({-href => href(action=>"commit", hash=>$par),5225class=>"list"},$par) .5226"</td>".5227"<td class=\"link\">".5228$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5229" | ".5230$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5231"</td>".5232"</tr>\n";5233}5234print"</table>".5235"</div>\n";52365237print"<div class=\"page_body\">\n";5238 git_print_log($co{'comment'});5239print"</div>\n";52405241 git_difftree_body(\@difftree,$hash,@$parents);52425243 git_footer_html();5244}52455246sub git_object {5247# object is defined by:5248# - hash or hash_base alone5249# - hash_base and file_name5250my$type;52515252# - hash or hash_base alone5253if($hash|| ($hash_base&& !defined$file_name)) {5254my$object_id=$hash||$hash_base;52555256open my$fd,"-|", quote_command(5257 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5258or die_error(404,"Object does not exist");5259$type= <$fd>;5260chomp$type;5261close$fd5262or die_error(404,"Object does not exist");52635264# - hash_base and file_name5265}elsif($hash_base&&defined$file_name) {5266$file_name=~ s,/+$,,;52675268system(git_cmd(),"cat-file",'-e',$hash_base) ==05269or die_error(404,"Base object does not exist");52705271# here errors should not hapen5272open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5273or die_error(500,"Open git-ls-tree failed");5274my$line= <$fd>;5275close$fd;52765277#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5278unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5279 die_error(404,"File or directory for given base does not exist");5280}5281$type=$2;5282$hash=$3;5283}else{5284 die_error(400,"Not enough information to find object");5285}52865287print$cgi->redirect(-uri => href(action=>$type, -full=>1,5288 hash=>$hash, hash_base=>$hash_base,5289 file_name=>$file_name),5290-status =>'302 Found');5291}52925293sub git_blobdiff {5294my$format=shift||'html';52955296my$fd;5297my@difftree;5298my%diffinfo;5299my$expires;53005301# preparing $fd and %diffinfo for git_patchset_body5302# new style URI5303if(defined$hash_base&&defined$hash_parent_base) {5304if(defined$file_name) {5305# read raw output5306open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5307$hash_parent_base,$hash_base,5308"--", (defined$file_parent?$file_parent: ()),$file_name5309or die_error(500,"Open git-diff-tree failed");5310@difftree=map{chomp;$_} <$fd>;5311close$fd5312or die_error(404,"Reading git-diff-tree failed");5313@difftree5314or die_error(404,"Blob diff not found");53155316}elsif(defined$hash&&5317$hash=~/[0-9a-fA-F]{40}/) {5318# try to find filename from $hash53195320# read filtered raw output5321open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5322$hash_parent_base,$hash_base,"--"5323or die_error(500,"Open git-diff-tree failed");5324@difftree=5325# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5326# $hash == to_id5327grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5328map{chomp;$_} <$fd>;5329close$fd5330or die_error(404,"Reading git-diff-tree failed");5331@difftree5332or die_error(404,"Blob diff not found");53335334}else{5335 die_error(400,"Missing one of the blob diff parameters");5336}53375338if(@difftree>1) {5339 die_error(400,"Ambiguous blob diff specification");5340}53415342%diffinfo= parse_difftree_raw_line($difftree[0]);5343$file_parent||=$diffinfo{'from_file'} ||$file_name;5344$file_name||=$diffinfo{'to_file'};53455346$hash_parent||=$diffinfo{'from_id'};5347$hash||=$diffinfo{'to_id'};53485349# non-textual hash id's can be cached5350if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5351$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5352$expires='+1d';5353}53545355# open patch output5356open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5357'-p', ($formateq'html'?"--full-index": ()),5358$hash_parent_base,$hash_base,5359"--", (defined$file_parent?$file_parent: ()),$file_name5360or die_error(500,"Open git-diff-tree failed");5361}53625363# old/legacy style URI -- not generated anymore since 1.4.3.5364if(!%diffinfo) {5365 die_error('404 Not Found',"Missing one of the blob diff parameters")5366}53675368# header5369if($formateq'html') {5370my$formats_nav=5371$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5372"raw");5373 git_header_html(undef,$expires);5374if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5375 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5376 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5377}else{5378print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5379print"<div class=\"title\">$hashvs$hash_parent</div>\n";5380}5381if(defined$file_name) {5382 git_print_page_path($file_name,"blob",$hash_base);5383}else{5384print"<div class=\"page_path\"></div>\n";5385}53865387}elsif($formateq'plain') {5388print$cgi->header(5389-type =>'text/plain',5390-charset =>'utf-8',5391-expires =>$expires,5392-content_disposition =>'inline; filename="'."$file_name".'.patch"');53935394print"X-Git-Url: ".$cgi->self_url() ."\n\n";53955396}else{5397 die_error(400,"Unknown blobdiff format");5398}53995400# patch5401if($formateq'html') {5402print"<div class=\"page_body\">\n";54035404 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5405close$fd;54065407print"</div>\n";# class="page_body"5408 git_footer_html();54095410}else{5411while(my$line= <$fd>) {5412$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5413$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;54145415print$line;54165417last if$line=~m!^\+\+\+!;5418}5419local$/=undef;5420print<$fd>;5421close$fd;5422}5423}54245425sub git_blobdiff_plain {5426 git_blobdiff('plain');5427}54285429sub git_commitdiff {5430my%params=@_;5431my$format=$params{-format} ||'html';54325433my($patch_max) = gitweb_get_feature('patches');5434if($formateq'patch') {5435 die_error(403,"Patch view not allowed")unless$patch_max;5436}54375438$hash||=$hash_base||"HEAD";5439my%co= parse_commit($hash)5440or die_error(404,"Unknown commit object");54415442# choose format for commitdiff for merge5443if(!defined$hash_parent&& @{$co{'parents'}} >1) {5444$hash_parent='--cc';5445}5446# we need to prepare $formats_nav before almost any parameter munging5447my$formats_nav;5448if($formateq'html') {5449$formats_nav=5450$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5451"raw");5452if($patch_max) {5453$formats_nav.=" | ".5454$cgi->a({-href => href(action=>"patch", -replay=>1)},5455"patch");5456}54575458if(defined$hash_parent&&5459$hash_parentne'-c'&&$hash_parentne'--cc') {5460# commitdiff with two commits given5461my$hash_parent_short=$hash_parent;5462if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5463$hash_parent_short=substr($hash_parent,0,7);5464}5465$formats_nav.=5466' (from';5467for(my$i=0;$i< @{$co{'parents'}};$i++) {5468if($co{'parents'}[$i]eq$hash_parent) {5469$formats_nav.=' parent '. ($i+1);5470last;5471}5472}5473$formats_nav.=': '.5474$cgi->a({-href => href(action=>"commitdiff",5475 hash=>$hash_parent)},5476 esc_html($hash_parent_short)) .5477')';5478}elsif(!$co{'parent'}) {5479# --root commitdiff5480$formats_nav.=' (initial)';5481}elsif(scalar@{$co{'parents'}} ==1) {5482# single parent commit5483$formats_nav.=5484' (parent: '.5485$cgi->a({-href => href(action=>"commitdiff",5486 hash=>$co{'parent'})},5487 esc_html(substr($co{'parent'},0,7))) .5488')';5489}else{5490# merge commit5491if($hash_parenteq'--cc') {5492$formats_nav.=' | '.5493$cgi->a({-href => href(action=>"commitdiff",5494 hash=>$hash, hash_parent=>'-c')},5495'combined');5496}else{# $hash_parent eq '-c'5497$formats_nav.=' | '.5498$cgi->a({-href => href(action=>"commitdiff",5499 hash=>$hash, hash_parent=>'--cc')},5500'compact');5501}5502$formats_nav.=5503' (merge: '.5504join(' ',map{5505$cgi->a({-href => href(action=>"commitdiff",5506 hash=>$_)},5507 esc_html(substr($_,0,7)));5508} @{$co{'parents'}} ) .5509')';5510}5511}55125513my$hash_parent_param=$hash_parent;5514if(!defined$hash_parent_param) {5515# --cc for multiple parents, --root for parentless5516$hash_parent_param=5517@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5518}55195520# read commitdiff5521my$fd;5522my@difftree;5523if($formateq'html') {5524open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5525"--no-commit-id","--patch-with-raw","--full-index",5526$hash_parent_param,$hash,"--"5527or die_error(500,"Open git-diff-tree failed");55285529while(my$line= <$fd>) {5530chomp$line;5531# empty line ends raw part of diff-tree output5532last unless$line;5533push@difftree,scalar parse_difftree_raw_line($line);5534}55355536}elsif($formateq'plain') {5537open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5538'-p',$hash_parent_param,$hash,"--"5539or die_error(500,"Open git-diff-tree failed");5540}elsif($formateq'patch') {5541# For commit ranges, we limit the output to the number of5542# patches specified in the 'patches' feature.5543# For single commits, we limit the output to a single patch,5544# diverging from the git-format-patch default.5545my@commit_spec= ();5546if($hash_parent) {5547if($patch_max>0) {5548push@commit_spec,"-$patch_max";5549}5550push@commit_spec,'-n',"$hash_parent..$hash";5551}else{5552if($params{-single}) {5553push@commit_spec,'-1';5554}else{5555if($patch_max>0) {5556push@commit_spec,"-$patch_max";5557}5558push@commit_spec,"-n";5559}5560push@commit_spec,'--root',$hash;5561}5562open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5563'--stdout',@commit_spec5564or die_error(500,"Open git-format-patch failed");5565}else{5566 die_error(400,"Unknown commitdiff format");5567}55685569# non-textual hash id's can be cached5570my$expires;5571if($hash=~m/^[0-9a-fA-F]{40}$/) {5572$expires="+1d";5573}55745575# write commit message5576if($formateq'html') {5577my$refs= git_get_references();5578my$ref= format_ref_marker($refs,$co{'id'});55795580 git_header_html(undef,$expires);5581 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5582 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5583 git_print_authorship(\%co);5584print"<div class=\"page_body\">\n";5585if(@{$co{'comment'}} >1) {5586print"<div class=\"log\">\n";5587 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5588print"</div>\n";# class="log"5589}55905591}elsif($formateq'plain') {5592my$refs= git_get_references("tags");5593my$tagname= git_get_rev_name_tags($hash);5594my$filename= basename($project) ."-$hash.patch";55955596print$cgi->header(5597-type =>'text/plain',5598-charset =>'utf-8',5599-expires =>$expires,5600-content_disposition =>'inline; filename="'."$filename".'"');5601my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5602print"From: ". to_utf8($co{'author'}) ."\n";5603print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5604print"Subject: ". to_utf8($co{'title'}) ."\n";56055606print"X-Git-Tag:$tagname\n"if$tagname;5607print"X-Git-Url: ".$cgi->self_url() ."\n\n";56085609foreachmy$line(@{$co{'comment'}}) {5610print to_utf8($line) ."\n";5611}5612print"---\n\n";5613}elsif($formateq'patch') {5614my$filename= basename($project) ."-$hash.patch";56155616print$cgi->header(5617-type =>'text/plain',5618-charset =>'utf-8',5619-expires =>$expires,5620-content_disposition =>'inline; filename="'."$filename".'"');5621}56225623# write patch5624if($formateq'html') {5625my$use_parents= !defined$hash_parent||5626$hash_parenteq'-c'||$hash_parenteq'--cc';5627 git_difftree_body(\@difftree,$hash,5628$use_parents? @{$co{'parents'}} :$hash_parent);5629print"<br/>\n";56305631 git_patchset_body($fd, \@difftree,$hash,5632$use_parents? @{$co{'parents'}} :$hash_parent);5633close$fd;5634print"</div>\n";# class="page_body"5635 git_footer_html();56365637}elsif($formateq'plain') {5638local$/=undef;5639print<$fd>;5640close$fd5641or print"Reading git-diff-tree failed\n";5642}elsif($formateq'patch') {5643local$/=undef;5644print<$fd>;5645close$fd5646or print"Reading git-format-patch failed\n";5647}5648}56495650sub git_commitdiff_plain {5651 git_commitdiff(-format =>'plain');5652}56535654# format-patch-style patches5655sub git_patch {5656 git_commitdiff(-format =>'patch', -single=>1);5657}56585659sub git_patches {5660 git_commitdiff(-format =>'patch');5661}56625663sub git_history {5664if(!defined$hash_base) {5665$hash_base= git_get_head_hash($project);5666}5667if(!defined$page) {5668$page=0;5669}5670my$ftype;5671my%co= parse_commit($hash_base)5672or die_error(404,"Unknown commit object");56735674my$refs= git_get_references();5675my$limit=sprintf("--max-count=%i", (100* ($page+1)));56765677my@commitlist= parse_commits($hash_base,101, (100*$page),5678$file_name,"--full-history")5679or die_error(404,"No such file or directory on given branch");56805681if(!defined$hash&&defined$file_name) {5682# some commits could have deleted file in question,5683# and not have it in tree, but one of them has to have it5684for(my$i=0;$i<=@commitlist;$i++) {5685$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5686last ifdefined$hash;5687}5688}5689if(defined$hash) {5690$ftype= git_get_type($hash);5691}5692if(!defined$ftype) {5693 die_error(500,"Unknown type of object");5694}56955696my$paging_nav='';5697if($page>0) {5698$paging_nav.=5699$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5700 file_name=>$file_name)},5701"first");5702$paging_nav.=" ⋅ ".5703$cgi->a({-href => href(-replay=>1, page=>$page-1),5704-accesskey =>"p", -title =>"Alt-p"},"prev");5705}else{5706$paging_nav.="first";5707$paging_nav.=" ⋅ prev";5708}5709my$next_link='';5710if($#commitlist>=100) {5711$next_link=5712$cgi->a({-href => href(-replay=>1, page=>$page+1),5713-accesskey =>"n", -title =>"Alt-n"},"next");5714$paging_nav.=" ⋅$next_link";5715}else{5716$paging_nav.=" ⋅ next";5717}57185719 git_header_html();5720 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5721 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5722 git_print_page_path($file_name,$ftype,$hash_base);57235724 git_history_body(\@commitlist,0,99,5725$refs,$hash_base,$ftype,$next_link);57265727 git_footer_html();5728}57295730sub git_search {5731 gitweb_check_feature('search')or die_error(403,"Search is disabled");5732if(!defined$searchtext) {5733 die_error(400,"Text field is empty");5734}5735if(!defined$hash) {5736$hash= git_get_head_hash($project);5737}5738my%co= parse_commit($hash);5739if(!%co) {5740 die_error(404,"Unknown commit object");5741}5742if(!defined$page) {5743$page=0;5744}57455746$searchtype||='commit';5747if($searchtypeeq'pickaxe') {5748# pickaxe may take all resources of your box and run for several minutes5749# with every query - so decide by yourself how public you make this feature5750 gitweb_check_feature('pickaxe')5751or die_error(403,"Pickaxe is disabled");5752}5753if($searchtypeeq'grep') {5754 gitweb_check_feature('grep')5755or die_error(403,"Grep is disabled");5756}57575758 git_header_html();57595760if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5761my$greptype;5762if($searchtypeeq'commit') {5763$greptype="--grep=";5764}elsif($searchtypeeq'author') {5765$greptype="--author=";5766}elsif($searchtypeeq'committer') {5767$greptype="--committer=";5768}5769$greptype.=$searchtext;5770my@commitlist= parse_commits($hash,101, (100*$page),undef,5771$greptype,'--regexp-ignore-case',5772$search_use_regexp?'--extended-regexp':'--fixed-strings');57735774my$paging_nav='';5775if($page>0) {5776$paging_nav.=5777$cgi->a({-href => href(action=>"search", hash=>$hash,5778 searchtext=>$searchtext,5779 searchtype=>$searchtype)},5780"first");5781$paging_nav.=" ⋅ ".5782$cgi->a({-href => href(-replay=>1, page=>$page-1),5783-accesskey =>"p", -title =>"Alt-p"},"prev");5784}else{5785$paging_nav.="first";5786$paging_nav.=" ⋅ prev";5787}5788my$next_link='';5789if($#commitlist>=100) {5790$next_link=5791$cgi->a({-href => href(-replay=>1, page=>$page+1),5792-accesskey =>"n", -title =>"Alt-n"},"next");5793$paging_nav.=" ⋅$next_link";5794}else{5795$paging_nav.=" ⋅ next";5796}57975798if($#commitlist>=100) {5799}58005801 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5802 git_print_header_div('commit', esc_html($co{'title'}),$hash);5803 git_search_grep_body(\@commitlist,0,99,$next_link);5804}58055806if($searchtypeeq'pickaxe') {5807 git_print_page_nav('','',$hash,$co{'tree'},$hash);5808 git_print_header_div('commit', esc_html($co{'title'}),$hash);58095810print"<table class=\"pickaxe search\">\n";5811my$alternate=1;5812$/="\n";5813open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5814'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5815($search_use_regexp?'--pickaxe-regex': ());5816undef%co;5817my@files;5818while(my$line= <$fd>) {5819chomp$line;5820next unless$line;58215822my%set= parse_difftree_raw_line($line);5823if(defined$set{'commit'}) {5824# finish previous commit5825if(%co) {5826print"</td>\n".5827"<td class=\"link\">".5828$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5829" | ".5830$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5831print"</td>\n".5832"</tr>\n";5833}58345835if($alternate) {5836print"<tr class=\"dark\">\n";5837}else{5838print"<tr class=\"light\">\n";5839}5840$alternate^=1;5841%co= parse_commit($set{'commit'});5842my$author= chop_and_escape_str($co{'author_name'},15,5);5843print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5844"<td><i>$author</i></td>\n".5845"<td>".5846$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5847-class=>"list subject"},5848 chop_and_escape_str($co{'title'},50) ."<br/>");5849}elsif(defined$set{'to_id'}) {5850next if($set{'to_id'} =~m/^0{40}$/);58515852print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5853 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5854-class=>"list"},5855"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5856"<br/>\n";5857}5858}5859close$fd;58605861# finish last commit (warning: repetition!)5862if(%co) {5863print"</td>\n".5864"<td class=\"link\">".5865$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5866" | ".5867$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5868print"</td>\n".5869"</tr>\n";5870}58715872print"</table>\n";5873}58745875if($searchtypeeq'grep') {5876 git_print_page_nav('','',$hash,$co{'tree'},$hash);5877 git_print_header_div('commit', esc_html($co{'title'}),$hash);58785879print"<table class=\"grep_search\">\n";5880my$alternate=1;5881my$matches=0;5882$/="\n";5883open my$fd,"-|", git_cmd(),'grep','-n',5884$search_use_regexp? ('-E','-i') :'-F',5885$searchtext,$co{'tree'};5886my$lastfile='';5887while(my$line= <$fd>) {5888chomp$line;5889my($file,$lno,$ltext,$binary);5890last if($matches++>1000);5891if($line=~/^Binary file (.+) matches$/) {5892$file=$1;5893$binary=1;5894}else{5895(undef,$file,$lno,$ltext) =split(/:/,$line,4);5896}5897if($filene$lastfile) {5898$lastfileand print"</td></tr>\n";5899if($alternate++) {5900print"<tr class=\"dark\">\n";5901}else{5902print"<tr class=\"light\">\n";5903}5904print"<td class=\"list\">".5905$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5906 file_name=>"$file"),5907-class=>"list"}, esc_path($file));5908print"</td><td>\n";5909$lastfile=$file;5910}5911if($binary) {5912print"<div class=\"binary\">Binary file</div>\n";5913}else{5914$ltext= untabify($ltext);5915if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5916$ltext= esc_html($1, -nbsp=>1);5917$ltext.='<span class="match">';5918$ltext.= esc_html($2, -nbsp=>1);5919$ltext.='</span>';5920$ltext.= esc_html($3, -nbsp=>1);5921}else{5922$ltext= esc_html($ltext, -nbsp=>1);5923}5924print"<div class=\"pre\">".5925$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5926 file_name=>"$file").'#l'.$lno,5927-class=>"linenr"},sprintf('%4i',$lno))5928.' '.$ltext."</div>\n";5929}5930}5931if($lastfile) {5932print"</td></tr>\n";5933if($matches>1000) {5934print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5935}5936}else{5937print"<div class=\"diff nodifferences\">No matches found</div>\n";5938}5939close$fd;59405941print"</table>\n";5942}5943 git_footer_html();5944}59455946sub git_search_help {5947 git_header_html();5948 git_print_page_nav('','',$hash,$hash,$hash);5949print<<EOT;5950<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5951regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5952the pattern entered is recognized as the POSIX extended5953<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5954insensitive).</p>5955<dl>5956<dt><b>commit</b></dt>5957<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5958EOT5959my$have_grep= gitweb_check_feature('grep');5960if($have_grep) {5961print<<EOT;5962<dt><b>grep</b></dt>5963<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5964 a different one) are searched for the given pattern. On large trees, this search can take5965a while and put some strain on the server, so please use it with some consideration. Note that5966due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5967case-sensitive.</dd>5968EOT5969}5970print<<EOT;5971<dt><b>author</b></dt>5972<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5973<dt><b>committer</b></dt>5974<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5975EOT5976my$have_pickaxe= gitweb_check_feature('pickaxe');5977if($have_pickaxe) {5978print<<EOT;5979<dt><b>pickaxe</b></dt>5980<dd>All commits that caused the string to appear or disappear from any file (changes that5981added, removed or "modified" the string) will be listed. This search can take a while and5982takes a lot of strain on the server, so please use it wisely. Note that since you may be5983interested even in changes just changing the case as well, this search is case sensitive.</dd>5984EOT5985}5986print"</dl>\n";5987 git_footer_html();5988}59895990sub git_shortlog {5991my$head= git_get_head_hash($project);5992if(!defined$hash) {5993$hash=$head;5994}5995if(!defined$page) {5996$page=0;5997}5998my$refs= git_get_references();59996000my$commit_hash=$hash;6001if(defined$hash_parent) {6002$commit_hash="$hash_parent..$hash";6003}6004my@commitlist= parse_commits($commit_hash,101, (100*$page));60056006my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);6007my$next_link='';6008if($#commitlist>=100) {6009$next_link=6010$cgi->a({-href => href(-replay=>1, page=>$page+1),6011-accesskey =>"n", -title =>"Alt-n"},"next");6012}6013my$patch_max= gitweb_check_feature('patches');6014if($patch_max) {6015if($patch_max<0||@commitlist<=$patch_max) {6016$paging_nav.=" ⋅ ".6017$cgi->a({-href => href(action=>"patches", -replay=>1)},6018"patches");6019}6020}60216022 git_header_html();6023 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6024 git_print_header_div('summary',$project);60256026 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);60276028 git_footer_html();6029}60306031## ......................................................................6032## feeds (RSS, Atom; OPML)60336034sub git_feed {6035my$format=shift||'atom';6036my$have_blame= gitweb_check_feature('blame');60376038# Atom: http://www.atomenabled.org/developers/syndication/6039# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6040if($formatne'rss'&&$formatne'atom') {6041 die_error(400,"Unknown web feed format");6042}60436044# log/feed of current (HEAD) branch, log of given branch, history of file/directory6045my$head=$hash||'HEAD';6046my@commitlist= parse_commits($head,150,0,$file_name);60476048my%latest_commit;6049my%latest_date;6050my$content_type="application/$format+xml";6051if(defined$cgi->http('HTTP_ACCEPT') &&6052$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6053# browser (feed reader) prefers text/xml6054$content_type='text/xml';6055}6056if(defined($commitlist[0])) {6057%latest_commit= %{$commitlist[0]};6058my$latest_epoch=$latest_commit{'committer_epoch'};6059%latest_date= parse_date($latest_epoch);6060my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6061if(defined$if_modified) {6062my$since;6063if(eval{require HTTP::Date;1; }) {6064$since= HTTP::Date::str2time($if_modified);6065}elsif(eval{require Time::ParseDate;1; }) {6066$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6067}6068if(defined$since&&$latest_epoch<=$since) {6069print$cgi->header(6070-type =>$content_type,6071-charset =>'utf-8',6072-last_modified =>$latest_date{'rfc2822'},6073-status =>'304 Not Modified');6074return;6075}6076}6077print$cgi->header(6078-type =>$content_type,6079-charset =>'utf-8',6080-last_modified =>$latest_date{'rfc2822'});6081}else{6082print$cgi->header(6083-type =>$content_type,6084-charset =>'utf-8');6085}60866087# Optimization: skip generating the body if client asks only6088# for Last-Modified date.6089return if($cgi->request_method()eq'HEAD');60906091# header variables6092my$title="$site_name-$project/$action";6093my$feed_type='log';6094if(defined$hash) {6095$title.=" - '$hash'";6096$feed_type='branch log';6097if(defined$file_name) {6098$title.=" ::$file_name";6099$feed_type='history';6100}6101}elsif(defined$file_name) {6102$title.=" -$file_name";6103$feed_type='history';6104}6105$title.="$feed_type";6106my$descr= git_get_project_description($project);6107if(defined$descr) {6108$descr= esc_html($descr);6109}else{6110$descr="$project".6111($formateq'rss'?'RSS':'Atom') .6112" feed";6113}6114my$owner= git_get_project_owner($project);6115$owner= esc_html($owner);61166117#header6118my$alt_url;6119if(defined$file_name) {6120$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6121}elsif(defined$hash) {6122$alt_url= href(-full=>1, action=>"log", hash=>$hash);6123}else{6124$alt_url= href(-full=>1, action=>"summary");6125}6126print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6127if($formateq'rss') {6128print<<XML;6129<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6130<channel>6131XML6132print"<title>$title</title>\n".6133"<link>$alt_url</link>\n".6134"<description>$descr</description>\n".6135"<language>en</language>\n".6136# project owner is responsible for 'editorial' content6137"<managingEditor>$owner</managingEditor>\n";6138if(defined$logo||defined$favicon) {6139# prefer the logo to the favicon, since RSS6140# doesn't allow both6141my$img= esc_url($logo||$favicon);6142print"<image>\n".6143"<url>$img</url>\n".6144"<title>$title</title>\n".6145"<link>$alt_url</link>\n".6146"</image>\n";6147}6148if(%latest_date) {6149print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6150print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6151}6152print"<generator>gitweb v.$version/$git_version</generator>\n";6153}elsif($formateq'atom') {6154print<<XML;6155<feed xmlns="http://www.w3.org/2005/Atom">6156XML6157print"<title>$title</title>\n".6158"<subtitle>$descr</subtitle>\n".6159'<link rel="alternate" type="text/html" href="'.6160$alt_url.'" />'."\n".6161'<link rel="self" type="'.$content_type.'" href="'.6162$cgi->self_url() .'" />'."\n".6163"<id>". href(-full=>1) ."</id>\n".6164# use project owner for feed author6165"<author><name>$owner</name></author>\n";6166if(defined$favicon) {6167print"<icon>". esc_url($favicon) ."</icon>\n";6168}6169if(defined$logo_url) {6170# not twice as wide as tall: 72 x 27 pixels6171print"<logo>". esc_url($logo) ."</logo>\n";6172}6173if(!%latest_date) {6174# dummy date to keep the feed valid until commits trickle in:6175print"<updated>1970-01-01T00:00:00Z</updated>\n";6176}else{6177print"<updated>$latest_date{'iso-8601'}</updated>\n";6178}6179print"<generator version='$version/$git_version'>gitweb</generator>\n";6180}61816182# contents6183for(my$i=0;$i<=$#commitlist;$i++) {6184my%co= %{$commitlist[$i]};6185my$commit=$co{'id'};6186# we read 150, we always show 30 and the ones more recent than 48 hours6187if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6188last;6189}6190my%cd= parse_date($co{'author_epoch'});61916192# get list of changed files6193open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6194$co{'parent'} ||"--root",6195$co{'id'},"--", (defined$file_name?$file_name: ())6196ornext;6197my@difftree=map{chomp;$_} <$fd>;6198close$fd6199ornext;62006201# print element (entry, item)6202my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6203if($formateq'rss') {6204print"<item>\n".6205"<title>". esc_html($co{'title'}) ."</title>\n".6206"<author>". esc_html($co{'author'}) ."</author>\n".6207"<pubDate>$cd{'rfc2822'}</pubDate>\n".6208"<guid isPermaLink=\"true\">$co_url</guid>\n".6209"<link>$co_url</link>\n".6210"<description>". esc_html($co{'title'}) ."</description>\n".6211"<content:encoded>".6212"<![CDATA[\n";6213}elsif($formateq'atom') {6214print"<entry>\n".6215"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6216"<updated>$cd{'iso-8601'}</updated>\n".6217"<author>\n".6218" <name>". esc_html($co{'author_name'}) ."</name>\n";6219if($co{'author_email'}) {6220print" <email>". esc_html($co{'author_email'}) ."</email>\n";6221}6222print"</author>\n".6223# use committer for contributor6224"<contributor>\n".6225" <name>". esc_html($co{'committer_name'}) ."</name>\n";6226if($co{'committer_email'}) {6227print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6228}6229print"</contributor>\n".6230"<published>$cd{'iso-8601'}</published>\n".6231"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6232"<id>$co_url</id>\n".6233"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6234"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6235}6236my$comment=$co{'comment'};6237print"<pre>\n";6238foreachmy$line(@$comment) {6239$line= esc_html($line);6240print"$line\n";6241}6242print"</pre><ul>\n";6243foreachmy$difftree_line(@difftree) {6244my%difftree= parse_difftree_raw_line($difftree_line);6245next if!$difftree{'from_id'};62466247my$file=$difftree{'file'} ||$difftree{'to_file'};62486249print"<li>".6250"[".6251$cgi->a({-href => href(-full=>1, action=>"blobdiff",6252 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6253 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6254 file_name=>$file, file_parent=>$difftree{'from_file'}),6255-title =>"diff"},'D');6256if($have_blame) {6257print$cgi->a({-href => href(-full=>1, action=>"blame",6258 file_name=>$file, hash_base=>$commit),6259-title =>"blame"},'B');6260}6261# if this is not a feed of a file history6262if(!defined$file_name||$file_namene$file) {6263print$cgi->a({-href => href(-full=>1, action=>"history",6264 file_name=>$file, hash=>$commit),6265-title =>"history"},'H');6266}6267$file= esc_path($file);6268print"] ".6269"$file</li>\n";6270}6271if($formateq'rss') {6272print"</ul>]]>\n".6273"</content:encoded>\n".6274"</item>\n";6275}elsif($formateq'atom') {6276print"</ul>\n</div>\n".6277"</content>\n".6278"</entry>\n";6279}6280}62816282# end of feed6283if($formateq'rss') {6284print"</channel>\n</rss>\n";6285}elsif($formateq'atom') {6286print"</feed>\n";6287}6288}62896290sub git_rss {6291 git_feed('rss');6292}62936294sub git_atom {6295 git_feed('atom');6296}62976298sub git_opml {6299my@list= git_get_projects_list();63006301print$cgi->header(6302-type =>'text/xml',6303-charset =>'utf-8',6304-content_disposition =>'inline; filename="opml.xml"');63056306print<<XML;6307<?xml version="1.0" encoding="utf-8"?>6308<opml version="1.0">6309<head>6310 <title>$site_nameOPML Export</title>6311</head>6312<body>6313<outline text="git RSS feeds">6314XML63156316foreachmy$pr(@list) {6317my%proj=%$pr;6318my$head= git_get_head_hash($proj{'path'});6319if(!defined$head) {6320next;6321}6322$git_dir="$projectroot/$proj{'path'}";6323my%co= parse_commit($head);6324if(!%co) {6325next;6326}63276328my$path= esc_html(chop_str($proj{'path'},25,5));6329my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6330my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6331print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6332}6333print<<XML;6334</outline>6335</body>6336</opml>6337XML6338}