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{ 462exists$known_snapshot_formats{$_} }@fmts; 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; 693unless($hash=~s/(\Q$opt->{'suffix'}\E|\Q.$fmt\E)$//) { 694next; 695} 696my$sfx=$1; 697# a valid suffix was found, so set the snapshot format 698# and reset the hash parameter 699$input_params{'snapshot_format'} =$fmt; 700$input_params{'hash'} =$hash; 701# we also set the format suffix to the one requested 702# in the URL: this way a request for e.g. .tgz returns 703# a .tgz instead of a .tar.gz 704$known_snapshot_formats{$fmt}{'suffix'} =$sfx; 705last; 706} 707} 708} 709evaluate_path_info(); 710 711our$action=$input_params{'action'}; 712if(defined$action) { 713if(!validate_action($action)) { 714 die_error(400,"Invalid action parameter"); 715} 716} 717 718# parameters which are pathnames 719our$project=$input_params{'project'}; 720if(defined$project) { 721if(!validate_project($project)) { 722undef$project; 723 die_error(404,"No such project"); 724} 725} 726 727our$file_name=$input_params{'file_name'}; 728if(defined$file_name) { 729if(!validate_pathname($file_name)) { 730 die_error(400,"Invalid file parameter"); 731} 732} 733 734our$file_parent=$input_params{'file_parent'}; 735if(defined$file_parent) { 736if(!validate_pathname($file_parent)) { 737 die_error(400,"Invalid file parent parameter"); 738} 739} 740 741# parameters which are refnames 742our$hash=$input_params{'hash'}; 743if(defined$hash) { 744if(!validate_refname($hash)) { 745 die_error(400,"Invalid hash parameter"); 746} 747} 748 749our$hash_parent=$input_params{'hash_parent'}; 750if(defined$hash_parent) { 751if(!validate_refname($hash_parent)) { 752 die_error(400,"Invalid hash parent parameter"); 753} 754} 755 756our$hash_base=$input_params{'hash_base'}; 757if(defined$hash_base) { 758if(!validate_refname($hash_base)) { 759 die_error(400,"Invalid hash base parameter"); 760} 761} 762 763our@extra_options= @{$input_params{'extra_options'}}; 764# @extra_options is always defined, since it can only be (currently) set from 765# CGI, and $cgi->param() returns the empty array in array context if the param 766# is not set 767foreachmy$opt(@extra_options) { 768if(not exists$allowed_options{$opt}) { 769 die_error(400,"Invalid option parameter"); 770} 771if(not grep(/^$action$/, @{$allowed_options{$opt}})) { 772 die_error(400,"Invalid option parameter for this action"); 773} 774} 775 776our$hash_parent_base=$input_params{'hash_parent_base'}; 777if(defined$hash_parent_base) { 778if(!validate_refname($hash_parent_base)) { 779 die_error(400,"Invalid hash parent base parameter"); 780} 781} 782 783# other parameters 784our$page=$input_params{'page'}; 785if(defined$page) { 786if($page=~m/[^0-9]/) { 787 die_error(400,"Invalid page parameter"); 788} 789} 790 791our$searchtype=$input_params{'searchtype'}; 792if(defined$searchtype) { 793if($searchtype=~m/[^a-z]/) { 794 die_error(400,"Invalid searchtype parameter"); 795} 796} 797 798our$search_use_regexp=$input_params{'search_use_regexp'}; 799 800our$searchtext=$input_params{'searchtext'}; 801our$search_regexp; 802if(defined$searchtext) { 803if(length($searchtext) <2) { 804 die_error(403,"At least two characters are required for search parameter"); 805} 806$search_regexp=$search_use_regexp?$searchtext:quotemeta$searchtext; 807} 808 809# path to the current git repository 810our$git_dir; 811$git_dir="$projectroot/$project"if$project; 812 813# list of supported snapshot formats 814our@snapshot_fmts= gitweb_get_feature('snapshot'); 815@snapshot_fmts= filter_snapshot_fmts(@snapshot_fmts); 816 817# dispatch 818if(!defined$action) { 819if(defined$hash) { 820$action= git_get_type($hash); 821}elsif(defined$hash_base&&defined$file_name) { 822$action= git_get_type("$hash_base:$file_name"); 823}elsif(defined$project) { 824$action='summary'; 825}else{ 826$action='project_list'; 827} 828} 829if(!defined($actions{$action})) { 830 die_error(400,"Unknown action"); 831} 832if($action!~m/^(?:opml|project_list|project_index)$/&& 833!$project) { 834 die_error(400,"Project needed"); 835} 836$actions{$action}->(); 837exit; 838 839## ====================================================================== 840## action links 841 842sub href { 843my%params=@_; 844# default is to use -absolute url() i.e. $my_uri 845my$href=$params{-full} ?$my_url:$my_uri; 846 847$params{'project'} =$projectunlessexists$params{'project'}; 848 849if($params{-replay}) { 850while(my($name,$symbol) =each%cgi_param_mapping) { 851if(!exists$params{$name}) { 852$params{$name} =$input_params{$name}; 853} 854} 855} 856 857my$use_pathinfo= gitweb_check_feature('pathinfo'); 858if($use_pathinfoand defined$params{'project'}) { 859# try to put as many parameters as possible in PATH_INFO: 860# - project name 861# - action 862# - hash_parent or hash_parent_base:/file_parent 863# - hash or hash_base:/filename 864# - the snapshot_format as an appropriate suffix 865 866# When the script is the root DirectoryIndex for the domain, 867# $href here would be something like http://gitweb.example.com/ 868# Thus, we strip any trailing / from $href, to spare us double 869# slashes in the final URL 870$href=~ s,/$,,; 871 872# Then add the project name, if present 873$href.="/".esc_url($params{'project'}); 874delete$params{'project'}; 875 876# since we destructively absorb parameters, we keep this 877# boolean that remembers if we're handling a snapshot 878my$is_snapshot=$params{'action'}eq'snapshot'; 879 880# Summary just uses the project path URL, any other action is 881# added to the URL 882if(defined$params{'action'}) { 883$href.="/".esc_url($params{'action'})unless$params{'action'}eq'summary'; 884delete$params{'action'}; 885} 886 887# Next, we put hash_parent_base:/file_parent..hash_base:/file_name, 888# stripping nonexistent or useless pieces 889$href.="/"if($params{'hash_base'} ||$params{'hash_parent_base'} 890||$params{'hash_parent'} ||$params{'hash'}); 891if(defined$params{'hash_base'}) { 892if(defined$params{'hash_parent_base'}) { 893$href.= esc_url($params{'hash_parent_base'}); 894# skip the file_parent if it's the same as the file_name 895delete$params{'file_parent'}if$params{'file_parent'}eq$params{'file_name'}; 896if(defined$params{'file_parent'} &&$params{'file_parent'} !~/\.\./) { 897$href.=":/".esc_url($params{'file_parent'}); 898delete$params{'file_parent'}; 899} 900$href.=".."; 901delete$params{'hash_parent'}; 902delete$params{'hash_parent_base'}; 903}elsif(defined$params{'hash_parent'}) { 904$href.= esc_url($params{'hash_parent'}).".."; 905delete$params{'hash_parent'}; 906} 907 908$href.= esc_url($params{'hash_base'}); 909if(defined$params{'file_name'} &&$params{'file_name'} !~/\.\./) { 910$href.=":/".esc_url($params{'file_name'}); 911delete$params{'file_name'}; 912} 913delete$params{'hash'}; 914delete$params{'hash_base'}; 915}elsif(defined$params{'hash'}) { 916$href.= esc_url($params{'hash'}); 917delete$params{'hash'}; 918} 919 920# If the action was a snapshot, we can absorb the 921# snapshot_format parameter too 922if($is_snapshot) { 923my$fmt=$params{'snapshot_format'}; 924# snapshot_format should always be defined when href() 925# is called, but just in case some code forgets, we 926# fall back to the default 927$fmt||=$snapshot_fmts[0]; 928$href.=$known_snapshot_formats{$fmt}{'suffix'}; 929delete$params{'snapshot_format'}; 930} 931} 932 933# now encode the parameters explicitly 934my@result= (); 935for(my$i=0;$i<@cgi_param_mapping;$i+=2) { 936my($name,$symbol) = ($cgi_param_mapping[$i],$cgi_param_mapping[$i+1]); 937if(defined$params{$name}) { 938if(ref($params{$name})eq"ARRAY") { 939foreachmy$par(@{$params{$name}}) { 940push@result,$symbol."=". esc_param($par); 941} 942}else{ 943push@result,$symbol."=". esc_param($params{$name}); 944} 945} 946} 947$href.="?".join(';',@result)ifscalar@result; 948 949return$href; 950} 951 952 953## ====================================================================== 954## validation, quoting/unquoting and escaping 955 956sub validate_action { 957my$input=shift||returnundef; 958returnundefunlessexists$actions{$input}; 959return$input; 960} 961 962sub validate_project { 963my$input=shift||returnundef; 964if(!validate_pathname($input) || 965!(-d "$projectroot/$input") || 966!check_export_ok("$projectroot/$input") || 967($strict_export&& !project_in_list($input))) { 968returnundef; 969}else{ 970return$input; 971} 972} 973 974sub validate_pathname { 975my$input=shift||returnundef; 976 977# no '.' or '..' as elements of path, i.e. no '.' nor '..' 978# at the beginning, at the end, and between slashes. 979# also this catches doubled slashes 980if($input=~m!(^|/)(|\.|\.\.)(/|$)!) { 981returnundef; 982} 983# no null characters 984if($input=~m!\0!) { 985returnundef; 986} 987return$input; 988} 989 990sub validate_refname { 991my$input=shift||returnundef; 992 993# textual hashes are O.K. 994if($input=~m/^[0-9a-fA-F]{40}$/) { 995return$input; 996} 997# it must be correct pathname 998$input= validate_pathname($input) 999orreturnundef;1000# restrictions on ref name according to git-check-ref-format1001if($input=~m!(/\.|\.\.|[\000-\040\177 ~^:?*\[]|/$)!) {1002returnundef;1003}1004return$input;1005}10061007# decode sequences of octets in utf8 into Perl's internal form,1008# which is utf-8 with utf8 flag set if needed. gitweb writes out1009# in utf-8 thanks to "binmode STDOUT, ':utf8'" at beginning1010sub to_utf8 {1011my$str=shift;1012if(utf8::valid($str)) {1013 utf8::decode($str);1014return$str;1015}else{1016return decode($fallback_encoding,$str, Encode::FB_DEFAULT);1017}1018}10191020# quote unsafe chars, but keep the slash, even when it's not1021# correct, but quoted slashes look too horrible in bookmarks1022sub esc_param {1023my$str=shift;1024$str=~s/([^A-Za-z0-9\-_.~()\/:@])/sprintf("%%%02X",ord($1))/eg;1025$str=~s/\+/%2B/g;1026$str=~s/ /\+/g;1027return$str;1028}10291030# quote unsafe chars in whole URL, so some charactrs cannot be quoted1031sub esc_url {1032my$str=shift;1033$str=~s/([^A-Za-z0-9\-_.~();\/;?:@&=])/sprintf("%%%02X",ord($1))/eg;1034$str=~s/\+/%2B/g;1035$str=~s/ /\+/g;1036return$str;1037}10381039# replace invalid utf8 character with SUBSTITUTION sequence1040sub esc_html {1041my$str=shift;1042my%opts=@_;10431044$str= to_utf8($str);1045$str=$cgi->escapeHTML($str);1046if($opts{'-nbsp'}) {1047$str=~s/ / /g;1048}1049$str=~ s|([[:cntrl:]])|(($1ne"\t") ? quot_cec($1) :$1)|eg;1050return$str;1051}10521053# quote control characters and escape filename to HTML1054sub esc_path {1055my$str=shift;1056my%opts=@_;10571058$str= to_utf8($str);1059$str=$cgi->escapeHTML($str);1060if($opts{'-nbsp'}) {1061$str=~s/ / /g;1062}1063$str=~ s|([[:cntrl:]])|quot_cec($1)|eg;1064return$str;1065}10661067# Make control characters "printable", using character escape codes (CEC)1068sub quot_cec {1069my$cntrl=shift;1070my%opts=@_;1071my%es= (# character escape codes, aka escape sequences1072"\t"=>'\t',# tab (HT)1073"\n"=>'\n',# line feed (LF)1074"\r"=>'\r',# carrige return (CR)1075"\f"=>'\f',# form feed (FF)1076"\b"=>'\b',# backspace (BS)1077"\a"=>'\a',# alarm (bell) (BEL)1078"\e"=>'\e',# escape (ESC)1079"\013"=>'\v',# vertical tab (VT)1080"\000"=>'\0',# nul character (NUL)1081);1082my$chr= ( (exists$es{$cntrl})1083?$es{$cntrl}1084:sprintf('\%2x',ord($cntrl)) );1085if($opts{-nohtml}) {1086return$chr;1087}else{1088return"<span class=\"cntrl\">$chr</span>";1089}1090}10911092# Alternatively use unicode control pictures codepoints,1093# Unicode "printable representation" (PR)1094sub quot_upr {1095my$cntrl=shift;1096my%opts=@_;10971098my$chr=sprintf('&#%04d;',0x2400+ord($cntrl));1099if($opts{-nohtml}) {1100return$chr;1101}else{1102return"<span class=\"cntrl\">$chr</span>";1103}1104}11051106# git may return quoted and escaped filenames1107sub unquote {1108my$str=shift;11091110sub unq {1111my$seq=shift;1112my%es= (# character escape codes, aka escape sequences1113't'=>"\t",# tab (HT, TAB)1114'n'=>"\n",# newline (NL)1115'r'=>"\r",# return (CR)1116'f'=>"\f",# form feed (FF)1117'b'=>"\b",# backspace (BS)1118'a'=>"\a",# alarm (bell) (BEL)1119'e'=>"\e",# escape (ESC)1120'v'=>"\013",# vertical tab (VT)1121);11221123if($seq=~m/^[0-7]{1,3}$/) {1124# octal char sequence1125returnchr(oct($seq));1126}elsif(exists$es{$seq}) {1127# C escape sequence, aka character escape code1128return$es{$seq};1129}1130# quoted ordinary character1131return$seq;1132}11331134if($str=~m/^"(.*)"$/) {1135# needs unquoting1136$str=$1;1137$str=~s/\\([^0-7]|[0-7]{1,3})/unq($1)/eg;1138}1139return$str;1140}11411142# escape tabs (convert tabs to spaces)1143sub untabify {1144my$line=shift;11451146while((my$pos=index($line,"\t")) != -1) {1147if(my$count= (8- ($pos%8))) {1148my$spaces=' ' x $count;1149$line=~s/\t/$spaces/;1150}1151}11521153return$line;1154}11551156sub project_in_list {1157my$project=shift;1158my@list= git_get_projects_list();1159return@list&&scalar(grep{$_->{'path'}eq$project}@list);1160}11611162## ----------------------------------------------------------------------1163## HTML aware string manipulation11641165# Try to chop given string on a word boundary between position1166# $len and $len+$add_len. If there is no word boundary there,1167# chop at $len+$add_len. Do not chop if chopped part plus ellipsis1168# (marking chopped part) would be longer than given string.1169sub chop_str {1170my$str=shift;1171my$len=shift;1172my$add_len=shift||10;1173my$where=shift||'right';# 'left' | 'center' | 'right'11741175# Make sure perl knows it is utf8 encoded so we don't1176# cut in the middle of a utf8 multibyte char.1177$str= to_utf8($str);11781179# allow only $len chars, but don't cut a word if it would fit in $add_len1180# if it doesn't fit, cut it if it's still longer than the dots we would add1181# remove chopped character entities entirely11821183# when chopping in the middle, distribute $len into left and right part1184# return early if chopping wouldn't make string shorter1185if($whereeq'center') {1186return$strif($len+5>=length($str));# filler is length 51187$len=int($len/2);1188}else{1189return$strif($len+4>=length($str));# filler is length 41190}11911192# regexps: ending and beginning with word part up to $add_len1193my$endre=qr/.{$len}\w{0,$add_len}/;1194my$begre=qr/\w{0,$add_len}.{$len}/;11951196if($whereeq'left') {1197$str=~m/^(.*?)($begre)$/;1198my($lead,$body) = ($1,$2);1199if(length($lead) >4) {1200$body=~s/^[^;]*;//if($lead=~m/&[^;]*$/);1201$lead=" ...";1202}1203return"$lead$body";12041205}elsif($whereeq'center') {1206$str=~m/^($endre)(.*)$/;1207my($left,$str) = ($1,$2);1208$str=~m/^(.*?)($begre)$/;1209my($mid,$right) = ($1,$2);1210if(length($mid) >5) {1211$left=~s/&[^;]*$//;1212$right=~s/^[^;]*;//if($mid=~m/&[^;]*$/);1213$mid=" ... ";1214}1215return"$left$mid$right";12161217}else{1218$str=~m/^($endre)(.*)$/;1219my$body=$1;1220my$tail=$2;1221if(length($tail) >4) {1222$body=~s/&[^;]*$//;1223$tail="... ";1224}1225return"$body$tail";1226}1227}12281229# takes the same arguments as chop_str, but also wraps a <span> around the1230# result with a title attribute if it does get chopped. Additionally, the1231# string is HTML-escaped.1232sub chop_and_escape_str {1233my($str) =@_;12341235my$chopped= chop_str(@_);1236if($choppedeq$str) {1237return esc_html($chopped);1238}else{1239$str=~s/([[:cntrl:]])/?/g;1240return$cgi->span({-title=>$str}, esc_html($chopped));1241}1242}12431244## ----------------------------------------------------------------------1245## functions returning short strings12461247# CSS class for given age value (in seconds)1248sub age_class {1249my$age=shift;12501251if(!defined$age) {1252return"noage";1253}elsif($age<60*60*2) {1254return"age0";1255}elsif($age<60*60*24*2) {1256return"age1";1257}else{1258return"age2";1259}1260}12611262# convert age in seconds to "nn units ago" string1263sub age_string {1264my$age=shift;1265my$age_str;12661267if($age>60*60*24*365*2) {1268$age_str= (int$age/60/60/24/365);1269$age_str.=" years ago";1270}elsif($age>60*60*24*(365/12)*2) {1271$age_str=int$age/60/60/24/(365/12);1272$age_str.=" months ago";1273}elsif($age>60*60*24*7*2) {1274$age_str=int$age/60/60/24/7;1275$age_str.=" weeks ago";1276}elsif($age>60*60*24*2) {1277$age_str=int$age/60/60/24;1278$age_str.=" days ago";1279}elsif($age>60*60*2) {1280$age_str=int$age/60/60;1281$age_str.=" hours ago";1282}elsif($age>60*2) {1283$age_str=int$age/60;1284$age_str.=" min ago";1285}elsif($age>2) {1286$age_str=int$age;1287$age_str.=" sec ago";1288}else{1289$age_str.=" right now";1290}1291return$age_str;1292}12931294useconstant{1295 S_IFINVALID =>0030000,1296 S_IFGITLINK =>0160000,1297};12981299# submodule/subproject, a commit object reference1300sub S_ISGITLINK {1301my$mode=shift;13021303return(($mode& S_IFMT) == S_IFGITLINK)1304}13051306# convert file mode in octal to symbolic file mode string1307sub mode_str {1308my$mode=oct shift;13091310if(S_ISGITLINK($mode)) {1311return'm---------';1312}elsif(S_ISDIR($mode& S_IFMT)) {1313return'drwxr-xr-x';1314}elsif(S_ISLNK($mode)) {1315return'lrwxrwxrwx';1316}elsif(S_ISREG($mode)) {1317# git cares only about the executable bit1318if($mode& S_IXUSR) {1319return'-rwxr-xr-x';1320}else{1321return'-rw-r--r--';1322};1323}else{1324return'----------';1325}1326}13271328# convert file mode in octal to file type string1329sub file_type {1330my$mode=shift;13311332if($mode!~m/^[0-7]+$/) {1333return$mode;1334}else{1335$mode=oct$mode;1336}13371338if(S_ISGITLINK($mode)) {1339return"submodule";1340}elsif(S_ISDIR($mode& S_IFMT)) {1341return"directory";1342}elsif(S_ISLNK($mode)) {1343return"symlink";1344}elsif(S_ISREG($mode)) {1345return"file";1346}else{1347return"unknown";1348}1349}13501351# convert file mode in octal to file type description string1352sub file_type_long {1353my$mode=shift;13541355if($mode!~m/^[0-7]+$/) {1356return$mode;1357}else{1358$mode=oct$mode;1359}13601361if(S_ISGITLINK($mode)) {1362return"submodule";1363}elsif(S_ISDIR($mode& S_IFMT)) {1364return"directory";1365}elsif(S_ISLNK($mode)) {1366return"symlink";1367}elsif(S_ISREG($mode)) {1368if($mode& S_IXUSR) {1369return"executable";1370}else{1371return"file";1372};1373}else{1374return"unknown";1375}1376}137713781379## ----------------------------------------------------------------------1380## functions returning short HTML fragments, or transforming HTML fragments1381## which don't belong to other sections13821383# format line of commit message.1384sub format_log_line_html {1385my$line=shift;13861387$line= esc_html($line, -nbsp=>1);1388$line=~ s{\b([0-9a-fA-F]{8,40})\b}{1389$cgi->a({-href => href(action=>"object", hash=>$1),1390-class=>"text"},$1);1391}eg;13921393return$line;1394}13951396# format marker of refs pointing to given object13971398# the destination action is chosen based on object type and current context:1399# - for annotated tags, we choose the tag view unless it's the current view1400# already, in which case we go to shortlog view1401# - for other refs, we keep the current view if we're in history, shortlog or1402# log view, and select shortlog otherwise1403sub format_ref_marker {1404my($refs,$id) =@_;1405my$markers='';14061407if(defined$refs->{$id}) {1408foreachmy$ref(@{$refs->{$id}}) {1409# this code exploits the fact that non-lightweight tags are the1410# only indirect objects, and that they are the only objects for which1411# we want to use tag instead of shortlog as action1412my($type,$name) =qw();1413my$indirect= ($ref=~s/\^\{\}$//);1414# e.g. tags/v2.6.11 or heads/next1415if($ref=~m!^(.*?)s?/(.*)$!) {1416$type=$1;1417$name=$2;1418}else{1419$type="ref";1420$name=$ref;1421}14221423my$class=$type;1424$class.=" indirect"if$indirect;14251426my$dest_action="shortlog";14271428if($indirect) {1429$dest_action="tag"unless$actioneq"tag";1430}elsif($action=~/^(history|(short)?log)$/) {1431$dest_action=$action;1432}14331434my$dest="";1435$dest.="refs/"unless$ref=~ m!^refs/!;1436$dest.=$ref;14371438my$link=$cgi->a({1439-href => href(1440 action=>$dest_action,1441 hash=>$dest1442)},$name);14431444$markers.=" <span class=\"$class\"title=\"$ref\">".1445$link."</span>";1446}1447}14481449if($markers) {1450return' <span class="refs">'.$markers.'</span>';1451}else{1452return"";1453}1454}14551456# format, perhaps shortened and with markers, title line1457sub format_subject_html {1458my($long,$short,$href,$extra) =@_;1459$extra=''unlessdefined($extra);14601461if(length($short) <length($long)) {1462return$cgi->a({-href =>$href, -class=>"list subject",1463-title => to_utf8($long)},1464 esc_html($short) .$extra);1465}else{1466return$cgi->a({-href =>$href, -class=>"list subject"},1467 esc_html($long) .$extra);1468}1469}14701471# format git diff header line, i.e. "diff --(git|combined|cc) ..."1472sub format_git_diff_header_line {1473my$line=shift;1474my$diffinfo=shift;1475my($from,$to) =@_;14761477if($diffinfo->{'nparents'}) {1478# combined diff1479$line=~s!^(diff (.*?) )"?.*$!$1!;1480if($to->{'href'}) {1481$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1482 esc_path($to->{'file'}));1483}else{# file was deleted (no href)1484$line.= esc_path($to->{'file'});1485}1486}else{1487# "ordinary" diff1488$line=~s!^(diff (.*?) )"?a/.*$!$1!;1489if($from->{'href'}) {1490$line.=$cgi->a({-href =>$from->{'href'}, -class=>"path"},1491'a/'. esc_path($from->{'file'}));1492}else{# file was added (no href)1493$line.='a/'. esc_path($from->{'file'});1494}1495$line.=' ';1496if($to->{'href'}) {1497$line.=$cgi->a({-href =>$to->{'href'}, -class=>"path"},1498'b/'. esc_path($to->{'file'}));1499}else{# file was deleted1500$line.='b/'. esc_path($to->{'file'});1501}1502}15031504return"<div class=\"diff header\">$line</div>\n";1505}15061507# format extended diff header line, before patch itself1508sub format_extended_diff_header_line {1509my$line=shift;1510my$diffinfo=shift;1511my($from,$to) =@_;15121513# match <path>1514if($line=~s!^((copy|rename) from ).*$!$1!&&$from->{'href'}) {1515$line.=$cgi->a({-href=>$from->{'href'}, -class=>"path"},1516 esc_path($from->{'file'}));1517}1518if($line=~s!^((copy|rename) to ).*$!$1!&&$to->{'href'}) {1519$line.=$cgi->a({-href=>$to->{'href'}, -class=>"path"},1520 esc_path($to->{'file'}));1521}1522# match single <mode>1523if($line=~m/\s(\d{6})$/) {1524$line.='<span class="info"> ('.1525 file_type_long($1) .1526')</span>';1527}1528# match <hash>1529if($line=~m/^index [0-9a-fA-F]{40},[0-9a-fA-F]{40}/) {1530# can match only for combined diff1531$line='index ';1532for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1533if($from->{'href'}[$i]) {1534$line.=$cgi->a({-href=>$from->{'href'}[$i],1535-class=>"hash"},1536substr($diffinfo->{'from_id'}[$i],0,7));1537}else{1538$line.='0' x 7;1539}1540# separator1541$line.=','if($i<$diffinfo->{'nparents'} -1);1542}1543$line.='..';1544if($to->{'href'}) {1545$line.=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1546substr($diffinfo->{'to_id'},0,7));1547}else{1548$line.='0' x 7;1549}15501551}elsif($line=~m/^index [0-9a-fA-F]{40}..[0-9a-fA-F]{40}/) {1552# can match only for ordinary diff1553my($from_link,$to_link);1554if($from->{'href'}) {1555$from_link=$cgi->a({-href=>$from->{'href'}, -class=>"hash"},1556substr($diffinfo->{'from_id'},0,7));1557}else{1558$from_link='0' x 7;1559}1560if($to->{'href'}) {1561$to_link=$cgi->a({-href=>$to->{'href'}, -class=>"hash"},1562substr($diffinfo->{'to_id'},0,7));1563}else{1564$to_link='0' x 7;1565}1566my($from_id,$to_id) = ($diffinfo->{'from_id'},$diffinfo->{'to_id'});1567$line=~s!$from_id\.\.$to_id!$from_link..$to_link!;1568}15691570return$line."<br/>\n";1571}15721573# format from-file/to-file diff header1574sub format_diff_from_to_header {1575my($from_line,$to_line,$diffinfo,$from,$to,@parents) =@_;1576my$line;1577my$result='';15781579$line=$from_line;1580#assert($line =~ m/^---/) if DEBUG;1581# no extra formatting for "^--- /dev/null"1582if(!$diffinfo->{'nparents'}) {1583# ordinary (single parent) diff1584if($line=~m!^--- "?a/!) {1585if($from->{'href'}) {1586$line='--- a/'.1587$cgi->a({-href=>$from->{'href'}, -class=>"path"},1588 esc_path($from->{'file'}));1589}else{1590$line='--- a/'.1591 esc_path($from->{'file'});1592}1593}1594$result.= qq!<div class="diff from_file">$line</div>\n!;15951596}else{1597# combined diff (merge commit)1598for(my$i=0;$i<$diffinfo->{'nparents'};$i++) {1599if($from->{'href'}[$i]) {1600$line='--- '.1601$cgi->a({-href=>href(action=>"blobdiff",1602 hash_parent=>$diffinfo->{'from_id'}[$i],1603 hash_parent_base=>$parents[$i],1604 file_parent=>$from->{'file'}[$i],1605 hash=>$diffinfo->{'to_id'},1606 hash_base=>$hash,1607 file_name=>$to->{'file'}),1608-class=>"path",1609-title=>"diff". ($i+1)},1610$i+1) .1611'/'.1612$cgi->a({-href=>$from->{'href'}[$i], -class=>"path"},1613 esc_path($from->{'file'}[$i]));1614}else{1615$line='--- /dev/null';1616}1617$result.= qq!<div class="diff from_file">$line</div>\n!;1618}1619}16201621$line=$to_line;1622#assert($line =~ m/^\+\+\+/) if DEBUG;1623# no extra formatting for "^+++ /dev/null"1624if($line=~m!^\+\+\+ "?b/!) {1625if($to->{'href'}) {1626$line='+++ b/'.1627$cgi->a({-href=>$to->{'href'}, -class=>"path"},1628 esc_path($to->{'file'}));1629}else{1630$line='+++ b/'.1631 esc_path($to->{'file'});1632}1633}1634$result.= qq!<div class="diff to_file">$line</div>\n!;16351636return$result;1637}16381639# create note for patch simplified by combined diff1640sub format_diff_cc_simplified {1641my($diffinfo,@parents) =@_;1642my$result='';16431644$result.="<div class=\"diff header\">".1645"diff --cc ";1646if(!is_deleted($diffinfo)) {1647$result.=$cgi->a({-href => href(action=>"blob",1648 hash_base=>$hash,1649 hash=>$diffinfo->{'to_id'},1650 file_name=>$diffinfo->{'to_file'}),1651-class=>"path"},1652 esc_path($diffinfo->{'to_file'}));1653}else{1654$result.= esc_path($diffinfo->{'to_file'});1655}1656$result.="</div>\n".# class="diff header"1657"<div class=\"diff nodifferences\">".1658"Simple merge".1659"</div>\n";# class="diff nodifferences"16601661return$result;1662}16631664# format patch (diff) line (not to be used for diff headers)1665sub format_diff_line {1666my$line=shift;1667my($from,$to) =@_;1668my$diff_class="";16691670chomp$line;16711672if($from&&$to&&ref($from->{'href'})eq"ARRAY") {1673# combined diff1674my$prefix=substr($line,0,scalar@{$from->{'href'}});1675if($line=~m/^\@{3}/) {1676$diff_class=" chunk_header";1677}elsif($line=~m/^\\/) {1678$diff_class=" incomplete";1679}elsif($prefix=~tr/+/+/) {1680$diff_class=" add";1681}elsif($prefix=~tr/-/-/) {1682$diff_class=" rem";1683}1684}else{1685# assume ordinary diff1686my$char=substr($line,0,1);1687if($chareq'+') {1688$diff_class=" add";1689}elsif($chareq'-') {1690$diff_class=" rem";1691}elsif($chareq'@') {1692$diff_class=" chunk_header";1693}elsif($chareq"\\") {1694$diff_class=" incomplete";1695}1696}1697$line= untabify($line);1698if($from&&$to&&$line=~m/^\@{2} /) {1699my($from_text,$from_start,$from_lines,$to_text,$to_start,$to_lines,$section) =1700$line=~m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;17011702$from_lines=0unlessdefined$from_lines;1703$to_lines=0unlessdefined$to_lines;17041705if($from->{'href'}) {1706$from_text=$cgi->a({-href=>"$from->{'href'}#l$from_start",1707-class=>"list"},$from_text);1708}1709if($to->{'href'}) {1710$to_text=$cgi->a({-href=>"$to->{'href'}#l$to_start",1711-class=>"list"},$to_text);1712}1713$line="<span class=\"chunk_info\">@@$from_text$to_text@@</span>".1714"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1715return"<div class=\"diff$diff_class\">$line</div>\n";1716}elsif($from&&$to&&$line=~m/^\@{3}/) {1717my($prefix,$ranges,$section) =$line=~m/^(\@+) (.*?) \@+(.*)$/;1718my(@from_text,@from_start,@from_nlines,$to_text,$to_start,$to_nlines);17191720@from_text=split(' ',$ranges);1721for(my$i=0;$i<@from_text; ++$i) {1722($from_start[$i],$from_nlines[$i]) =1723(split(',',substr($from_text[$i],1)),0);1724}17251726$to_text=pop@from_text;1727$to_start=pop@from_start;1728$to_nlines=pop@from_nlines;17291730$line="<span class=\"chunk_info\">$prefix";1731for(my$i=0;$i<@from_text; ++$i) {1732if($from->{'href'}[$i]) {1733$line.=$cgi->a({-href=>"$from->{'href'}[$i]#l$from_start[$i]",1734-class=>"list"},$from_text[$i]);1735}else{1736$line.=$from_text[$i];1737}1738$line.=" ";1739}1740if($to->{'href'}) {1741$line.=$cgi->a({-href=>"$to->{'href'}#l$to_start",1742-class=>"list"},$to_text);1743}else{1744$line.=$to_text;1745}1746$line.="$prefix</span>".1747"<span class=\"section\">". esc_html($section, -nbsp=>1) ."</span>";1748return"<div class=\"diff$diff_class\">$line</div>\n";1749}1750return"<div class=\"diff$diff_class\">". esc_html($line, -nbsp=>1) ."</div>\n";1751}17521753# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",1754# linked. Pass the hash of the tree/commit to snapshot.1755sub format_snapshot_links {1756my($hash) =@_;1757my$num_fmts=@snapshot_fmts;1758if($num_fmts>1) {1759# A parenthesized list of links bearing format names.1760# e.g. "snapshot (_tar.gz_ _zip_)"1761return"snapshot (".join(' ',map1762$cgi->a({1763-href => href(1764 action=>"snapshot",1765 hash=>$hash,1766 snapshot_format=>$_1767)1768},$known_snapshot_formats{$_}{'display'})1769,@snapshot_fmts) .")";1770}elsif($num_fmts==1) {1771# A single "snapshot" link whose tooltip bears the format name.1772# i.e. "_snapshot_"1773my($fmt) =@snapshot_fmts;1774return1775$cgi->a({1776-href => href(1777 action=>"snapshot",1778 hash=>$hash,1779 snapshot_format=>$fmt1780),1781-title =>"in format:$known_snapshot_formats{$fmt}{'display'}"1782},"snapshot");1783}else{# $num_fmts == 01784returnundef;1785}1786}17871788## ......................................................................1789## functions returning values to be passed, perhaps after some1790## transformation, to other functions; e.g. returning arguments to href()17911792# returns hash to be passed to href to generate gitweb URL1793# in -title key it returns description of link1794sub get_feed_info {1795my$format=shift||'Atom';1796my%res= (action =>lc($format));17971798# feed links are possible only for project views1799return unless(defined$project);1800# some views should link to OPML, or to generic project feed,1801# or don't have specific feed yet (so they should use generic)1802return if($action=~/^(?:tags|heads|forks|tag|search)$/x);18031804my$branch;1805# branches refs uses 'refs/heads/' prefix (fullname) to differentiate1806# from tag links; this also makes possible to detect branch links1807if((defined$hash_base&&$hash_base=~m!^refs/heads/(.*)$!) ||1808(defined$hash&&$hash=~m!^refs/heads/(.*)$!)) {1809$branch=$1;1810}1811# find log type for feed description (title)1812my$type='log';1813if(defined$file_name) {1814$type="history of$file_name";1815$type.="/"if($actioneq'tree');1816$type.=" on '$branch'"if(defined$branch);1817}else{1818$type="log of$branch"if(defined$branch);1819}18201821$res{-title} =$type;1822$res{'hash'} = (defined$branch?"refs/heads/$branch":undef);1823$res{'file_name'} =$file_name;18241825return%res;1826}18271828## ----------------------------------------------------------------------1829## git utility subroutines, invoking git commands18301831# returns path to the core git executable and the --git-dir parameter as list1832sub git_cmd {1833return$GIT,'--git-dir='.$git_dir;1834}18351836# quote the given arguments for passing them to the shell1837# quote_command("command", "arg 1", "arg with ' and ! characters")1838# => "'command' 'arg 1' 'arg with '\'' and '\!' characters'"1839# Try to avoid using this function wherever possible.1840sub quote_command {1841returnjoin(' ',1842map{my$a=$_;$a=~s/(['!])/'\\$1'/g;"'$a'"}@_);1843}18441845# get HEAD ref of given project as hash1846sub git_get_head_hash {1847my$project=shift;1848my$o_git_dir=$git_dir;1849my$retval=undef;1850$git_dir="$projectroot/$project";1851if(open my$fd,"-|", git_cmd(),"rev-parse","--verify","HEAD") {1852my$head= <$fd>;1853close$fd;1854if(defined$head&&$head=~/^([0-9a-fA-F]{40})$/) {1855$retval=$1;1856}1857}1858if(defined$o_git_dir) {1859$git_dir=$o_git_dir;1860}1861return$retval;1862}18631864# get type of given object1865sub git_get_type {1866my$hash=shift;18671868open my$fd,"-|", git_cmd(),"cat-file",'-t',$hashorreturn;1869my$type= <$fd>;1870close$fdorreturn;1871chomp$type;1872return$type;1873}18741875# repository configuration1876our$config_file='';1877our%config;18781879# store multiple values for single key as anonymous array reference1880# single values stored directly in the hash, not as [ <value> ]1881sub hash_set_multi {1882my($hash,$key,$value) =@_;18831884if(!exists$hash->{$key}) {1885$hash->{$key} =$value;1886}elsif(!ref$hash->{$key}) {1887$hash->{$key} = [$hash->{$key},$value];1888}else{1889push@{$hash->{$key}},$value;1890}1891}18921893# return hash of git project configuration1894# optionally limited to some section, e.g. 'gitweb'1895sub git_parse_project_config {1896my$section_regexp=shift;1897my%config;18981899local$/="\0";19001901open my$fh,"-|", git_cmd(),"config",'-z','-l',1902orreturn;19031904while(my$keyval= <$fh>) {1905chomp$keyval;1906my($key,$value) =split(/\n/,$keyval,2);19071908 hash_set_multi(\%config,$key,$value)1909if(!defined$section_regexp||$key=~/^(?:$section_regexp)\./o);1910}1911close$fh;19121913return%config;1914}19151916# convert config value to boolean: 'true' or 'false'1917# no value, number > 0, 'true' and 'yes' values are true1918# rest of values are treated as false (never as error)1919sub config_to_bool {1920my$val=shift;19211922return1if!defined$val;# section.key19231924# strip leading and trailing whitespace1925$val=~s/^\s+//;1926$val=~s/\s+$//;19271928return(($val=~/^\d+$/&&$val) ||# section.key = 11929($val=~/^(?:true|yes)$/i));# section.key = true1930}19311932# convert config value to simple decimal number1933# an optional value suffix of 'k', 'm', or 'g' will cause the value1934# to be multiplied by 1024, 1048576, or 10737418241935sub config_to_int {1936my$val=shift;19371938# strip leading and trailing whitespace1939$val=~s/^\s+//;1940$val=~s/\s+$//;19411942if(my($num,$unit) = ($val=~/^([0-9]*)([kmg])$/i)) {1943$unit=lc($unit);1944# unknown unit is treated as 11945return$num* ($uniteq'g'?1073741824:1946$uniteq'm'?1048576:1947$uniteq'k'?1024:1);1948}1949return$val;1950}19511952# convert config value to array reference, if needed1953sub config_to_multi {1954my$val=shift;19551956returnref($val) ?$val: (defined($val) ? [$val] : []);1957}19581959sub git_get_project_config {1960my($key,$type) =@_;19611962# key sanity check1963return unless($key);1964$key=~s/^gitweb\.//;1965return if($key=~m/\W/);19661967# type sanity check1968if(defined$type) {1969$type=~s/^--//;1970$type=undef1971unless($typeeq'bool'||$typeeq'int');1972}19731974# get config1975if(!defined$config_file||1976$config_filene"$git_dir/config") {1977%config= git_parse_project_config('gitweb');1978$config_file="$git_dir/config";1979}19801981# check if config variable (key) exists1982return unlessexists$config{"gitweb.$key"};19831984# ensure given type1985if(!defined$type) {1986return$config{"gitweb.$key"};1987}elsif($typeeq'bool') {1988# backward compatibility: 'git config --bool' returns true/false1989return config_to_bool($config{"gitweb.$key"}) ?'true':'false';1990}elsif($typeeq'int') {1991return config_to_int($config{"gitweb.$key"});1992}1993return$config{"gitweb.$key"};1994}19951996# get hash of given path at given ref1997sub git_get_hash_by_path {1998my$base=shift;1999my$path=shift||returnundef;2000my$type=shift;20012002$path=~ s,/+$,,;20032004open my$fd,"-|", git_cmd(),"ls-tree",$base,"--",$path2005or die_error(500,"Open git-ls-tree failed");2006my$line= <$fd>;2007close$fdorreturnundef;20082009if(!defined$line) {2010# there is no tree or hash given by $path at $base2011returnundef;2012}20132014#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'2015$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/;2016if(defined$type&&$typene$2) {2017# type doesn't match2018returnundef;2019}2020return$3;2021}20222023# get path of entry with given hash at given tree-ish (ref)2024# used to get 'from' filename for combined diff (merge commit) for renames2025sub git_get_path_by_hash {2026my$base=shift||return;2027my$hash=shift||return;20282029local$/="\0";20302031open my$fd,"-|", git_cmd(),"ls-tree",'-r','-t','-z',$base2032orreturnundef;2033while(my$line= <$fd>) {2034chomp$line;20352036#'040000 tree 595596a6a9117ddba9fe379b6b012b558bac8423 gitweb'2037#'100644 blob e02e90f0429be0d2a69b76571101f20b8f75530f gitweb/README'2038if($line=~m/(?:[0-9]+) (?:.+) $hash\t(.+)$/) {2039close$fd;2040return$1;2041}2042}2043close$fd;2044returnundef;2045}20462047## ......................................................................2048## git utility functions, directly accessing git repository20492050sub git_get_project_description {2051my$path=shift;20522053$git_dir="$projectroot/$path";2054open my$fd,'<',"$git_dir/description"2055orreturn git_get_project_config('description');2056my$descr= <$fd>;2057close$fd;2058if(defined$descr) {2059chomp$descr;2060}2061return$descr;2062}20632064sub git_get_project_ctags {2065my$path=shift;2066my$ctags= {};20672068$git_dir="$projectroot/$path";2069opendir my$dh,"$git_dir/ctags"2070orreturn$ctags;2071foreach(grep{ -f $_}map{"$git_dir/ctags/$_"}readdir($dh)) {2072open my$ct,'<',$_ornext;2073my$val= <$ct>;2074chomp$val;2075close$ct;2076my$ctag=$_;$ctag=~ s#.*/##;2077$ctags->{$ctag} =$val;2078}2079closedir$dh;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(my$mh,'<',$mimemap)orreturnundef;2808while(<$mh>) {2809next ifm/^#/;# skip comments2810my($mimetype,$exts) =split(/\t+/);2811if(defined$exts) {2812my@exts=split(/\s+/,$exts);2813foreachmy$ext(@exts) {2814$mimemap{$ext} =$mimetype;2815}2816}2817}2818close($mh);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}32153216sub git_print_authorship {3217my$co=shift;32183219my%ad= parse_date($co->{'author_epoch'},$co->{'author_tz'});3220print"<div class=\"author_date\">".3221 esc_html($co->{'author_name'}) .3222" [$ad{'rfc2822'}";3223if($ad{'hour_local'} <6) {3224printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",3225$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3226}else{3227printf(" (%02d:%02d%s)",3228$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});3229}3230print"]</div>\n";3231}32323233sub git_print_page_path {3234my$name=shift;3235my$type=shift;3236my$hb=shift;323732383239print"<div class=\"page_path\">";3240print$cgi->a({-href => href(action=>"tree", hash_base=>$hb),3241-title =>'tree root'}, to_utf8("[$project]"));3242print" / ";3243if(defined$name) {3244my@dirname=split'/',$name;3245my$basename=pop@dirname;3246my$fullname='';32473248foreachmy$dir(@dirname) {3249$fullname.= ($fullname?'/':'') .$dir;3250print$cgi->a({-href => href(action=>"tree", file_name=>$fullname,3251 hash_base=>$hb),3252-title =>$fullname}, esc_path($dir));3253print" / ";3254}3255if(defined$type&&$typeeq'blob') {3256print$cgi->a({-href => href(action=>"blob_plain", file_name=>$file_name,3257 hash_base=>$hb),3258-title =>$name}, esc_path($basename));3259}elsif(defined$type&&$typeeq'tree') {3260print$cgi->a({-href => href(action=>"tree", file_name=>$file_name,3261 hash_base=>$hb),3262-title =>$name}, esc_path($basename));3263print" / ";3264}else{3265print esc_path($basename);3266}3267}3268print"<br/></div>\n";3269}32703271sub git_print_log {3272my$log=shift;3273my%opts=@_;32743275if($opts{'-remove_title'}) {3276# remove title, i.e. first line of log3277shift@$log;3278}3279# remove leading empty lines3280while(defined$log->[0] &&$log->[0]eq"") {3281shift@$log;3282}32833284# print log3285my$signoff=0;3286my$empty=0;3287foreachmy$line(@$log) {3288if($line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i) {3289$signoff=1;3290$empty=0;3291if(!$opts{'-remove_signoff'}) {3292print"<span class=\"signoff\">". esc_html($line) ."</span><br/>\n";3293next;3294}else{3295# remove signoff lines3296next;3297}3298}else{3299$signoff=0;3300}33013302# print only one empty line3303# do not print empty line after signoff3304if($lineeq"") {3305next if($empty||$signoff);3306$empty=1;3307}else{3308$empty=0;3309}33103311print format_log_line_html($line) ."<br/>\n";3312}33133314if($opts{'-final_empty_line'}) {3315# end with single empty line3316print"<br/>\n"unless$empty;3317}3318}33193320# return link target (what link points to)3321sub git_get_link_target {3322my$hash=shift;3323my$link_target;33243325# read link3326open my$fd,"-|", git_cmd(),"cat-file","blob",$hash3327orreturn;3328{3329local$/=undef;3330$link_target= <$fd>;3331}3332close$fd3333orreturn;33343335return$link_target;3336}33373338# given link target, and the directory (basedir) the link is in,3339# return target of link relative to top directory (top tree);3340# return undef if it is not possible (including absolute links).3341sub normalize_link_target {3342my($link_target,$basedir,$hash_base) =@_;33433344# we can normalize symlink target only if $hash_base is provided3345return unless$hash_base;33463347# absolute symlinks (beginning with '/') cannot be normalized3348return if(substr($link_target,0,1)eq'/');33493350# normalize link target to path from top (root) tree (dir)3351my$path;3352if($basedir) {3353$path=$basedir.'/'.$link_target;3354}else{3355# we are in top (root) tree (dir)3356$path=$link_target;3357}33583359# remove //, /./, and /../3360my@path_parts;3361foreachmy$part(split('/',$path)) {3362# discard '.' and ''3363next if(!$part||$parteq'.');3364# handle '..'3365if($parteq'..') {3366if(@path_parts) {3367pop@path_parts;3368}else{3369# link leads outside repository (outside top dir)3370return;3371}3372}else{3373push@path_parts,$part;3374}3375}3376$path=join('/',@path_parts);33773378return$path;3379}33803381# print tree entry (row of git_tree), but without encompassing <tr> element3382sub git_print_tree_entry {3383my($t,$basedir,$hash_base,$have_blame) =@_;33843385my%base_key= ();3386$base_key{'hash_base'} =$hash_baseifdefined$hash_base;33873388# The format of a table row is: mode list link. Where mode is3389# the mode of the entry, list is the name of the entry, an href,3390# and link is the action links of the entry.33913392print"<td class=\"mode\">". mode_str($t->{'mode'}) ."</td>\n";3393if($t->{'type'}eq"blob") {3394print"<td class=\"list\">".3395$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3396 file_name=>"$basedir$t->{'name'}",%base_key),3397-class=>"list"}, esc_path($t->{'name'}));3398if(S_ISLNK(oct$t->{'mode'})) {3399my$link_target= git_get_link_target($t->{'hash'});3400if($link_target) {3401my$norm_target= normalize_link_target($link_target,$basedir,$hash_base);3402if(defined$norm_target) {3403print" -> ".3404$cgi->a({-href => href(action=>"object", hash_base=>$hash_base,3405 file_name=>$norm_target),3406-title =>$norm_target}, esc_path($link_target));3407}else{3408print" -> ". esc_path($link_target);3409}3410}3411}3412print"</td>\n";3413print"<td class=\"link\">";3414print$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},3415 file_name=>"$basedir$t->{'name'}",%base_key)},3416"blob");3417if($have_blame) {3418print" | ".3419$cgi->a({-href => href(action=>"blame", hash=>$t->{'hash'},3420 file_name=>"$basedir$t->{'name'}",%base_key)},3421"blame");3422}3423if(defined$hash_base) {3424print" | ".3425$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3426 hash=>$t->{'hash'}, file_name=>"$basedir$t->{'name'}")},3427"history");3428}3429print" | ".3430$cgi->a({-href => href(action=>"blob_plain", hash_base=>$hash_base,3431 file_name=>"$basedir$t->{'name'}")},3432"raw");3433print"</td>\n";34343435}elsif($t->{'type'}eq"tree") {3436print"<td class=\"list\">";3437print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3438 file_name=>"$basedir$t->{'name'}",%base_key)},3439 esc_path($t->{'name'}));3440print"</td>\n";3441print"<td class=\"link\">";3442print$cgi->a({-href => href(action=>"tree", hash=>$t->{'hash'},3443 file_name=>"$basedir$t->{'name'}",%base_key)},3444"tree");3445if(defined$hash_base) {3446print" | ".3447$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,3448 file_name=>"$basedir$t->{'name'}")},3449"history");3450}3451print"</td>\n";3452}else{3453# unknown object: we can only present history for it3454# (this includes 'commit' object, i.e. submodule support)3455print"<td class=\"list\">".3456 esc_path($t->{'name'}) .3457"</td>\n";3458print"<td class=\"link\">";3459if(defined$hash_base) {3460print$cgi->a({-href => href(action=>"history",3461 hash_base=>$hash_base,3462 file_name=>"$basedir$t->{'name'}")},3463"history");3464}3465print"</td>\n";3466}3467}34683469## ......................................................................3470## functions printing large fragments of HTML34713472# get pre-image filenames for merge (combined) diff3473sub fill_from_file_info {3474my($diff,@parents) =@_;34753476$diff->{'from_file'} = [ ];3477$diff->{'from_file'}[$diff->{'nparents'} -1] =undef;3478for(my$i=0;$i<$diff->{'nparents'};$i++) {3479if($diff->{'status'}[$i]eq'R'||3480$diff->{'status'}[$i]eq'C') {3481$diff->{'from_file'}[$i] =3482 git_get_path_by_hash($parents[$i],$diff->{'from_id'}[$i]);3483}3484}34853486return$diff;3487}34883489# is current raw difftree line of file deletion3490sub is_deleted {3491my$diffinfo=shift;34923493return$diffinfo->{'to_id'}eq('0' x 40);3494}34953496# does patch correspond to [previous] difftree raw line3497# $diffinfo - hashref of parsed raw diff format3498# $patchinfo - hashref of parsed patch diff format3499# (the same keys as in $diffinfo)3500sub is_patch_split {3501my($diffinfo,$patchinfo) =@_;35023503returndefined$diffinfo&&defined$patchinfo3504&&$diffinfo->{'to_file'}eq$patchinfo->{'to_file'};3505}350635073508sub git_difftree_body {3509my($difftree,$hash,@parents) =@_;3510my($parent) =$parents[0];3511my$have_blame= gitweb_check_feature('blame');3512print"<div class=\"list_head\">\n";3513if($#{$difftree} >10) {3514print(($#{$difftree} +1) ." files changed:\n");3515}3516print"</div>\n";35173518print"<table class=\"".3519(@parents>1?"combined ":"") .3520"diff_tree\">\n";35213522# header only for combined diff in 'commitdiff' view3523my$has_header=@$difftree&&@parents>1&&$actioneq'commitdiff';3524if($has_header) {3525# table header3526print"<thead><tr>\n".3527"<th></th><th></th>\n";# filename, patchN link3528for(my$i=0;$i<@parents;$i++) {3529my$par=$parents[$i];3530print"<th>".3531$cgi->a({-href => href(action=>"commitdiff",3532 hash=>$hash, hash_parent=>$par),3533-title =>'commitdiff to parent number '.3534($i+1) .': '.substr($par,0,7)},3535$i+1) .3536" </th>\n";3537}3538print"</tr></thead>\n<tbody>\n";3539}35403541my$alternate=1;3542my$patchno=0;3543foreachmy$line(@{$difftree}) {3544my$diff= parsed_difftree_line($line);35453546if($alternate) {3547print"<tr class=\"dark\">\n";3548}else{3549print"<tr class=\"light\">\n";3550}3551$alternate^=1;35523553if(exists$diff->{'nparents'}) {# combined diff35543555 fill_from_file_info($diff,@parents)3556unlessexists$diff->{'from_file'};35573558if(!is_deleted($diff)) {3559# file exists in the result (child) commit3560print"<td>".3561$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3562 file_name=>$diff->{'to_file'},3563 hash_base=>$hash),3564-class=>"list"}, esc_path($diff->{'to_file'})) .3565"</td>\n";3566}else{3567print"<td>".3568 esc_path($diff->{'to_file'}) .3569"</td>\n";3570}35713572if($actioneq'commitdiff') {3573# link to patch3574$patchno++;3575print"<td class=\"link\">".3576$cgi->a({-href =>"#patch$patchno"},"patch") .3577" | ".3578"</td>\n";3579}35803581my$has_history=0;3582my$not_deleted=0;3583for(my$i=0;$i<$diff->{'nparents'};$i++) {3584my$hash_parent=$parents[$i];3585my$from_hash=$diff->{'from_id'}[$i];3586my$from_path=$diff->{'from_file'}[$i];3587my$status=$diff->{'status'}[$i];35883589$has_history||= ($statusne'A');3590$not_deleted||= ($statusne'D');35913592if($statuseq'A') {3593print"<td class=\"link\"align=\"right\"> | </td>\n";3594}elsif($statuseq'D') {3595print"<td class=\"link\">".3596$cgi->a({-href => href(action=>"blob",3597 hash_base=>$hash,3598 hash=>$from_hash,3599 file_name=>$from_path)},3600"blob". ($i+1)) .3601" | </td>\n";3602}else{3603if($diff->{'to_id'}eq$from_hash) {3604print"<td class=\"link nochange\">";3605}else{3606print"<td class=\"link\">";3607}3608print$cgi->a({-href => href(action=>"blobdiff",3609 hash=>$diff->{'to_id'},3610 hash_parent=>$from_hash,3611 hash_base=>$hash,3612 hash_parent_base=>$hash_parent,3613 file_name=>$diff->{'to_file'},3614 file_parent=>$from_path)},3615"diff". ($i+1)) .3616" | </td>\n";3617}3618}36193620print"<td class=\"link\">";3621if($not_deleted) {3622print$cgi->a({-href => href(action=>"blob",3623 hash=>$diff->{'to_id'},3624 file_name=>$diff->{'to_file'},3625 hash_base=>$hash)},3626"blob");3627print" | "if($has_history);3628}3629if($has_history) {3630print$cgi->a({-href => href(action=>"history",3631 file_name=>$diff->{'to_file'},3632 hash_base=>$hash)},3633"history");3634}3635print"</td>\n";36363637print"</tr>\n";3638next;# instead of 'else' clause, to avoid extra indent3639}3640# else ordinary diff36413642my($to_mode_oct,$to_mode_str,$to_file_type);3643my($from_mode_oct,$from_mode_str,$from_file_type);3644if($diff->{'to_mode'}ne('0' x 6)) {3645$to_mode_oct=oct$diff->{'to_mode'};3646if(S_ISREG($to_mode_oct)) {# only for regular file3647$to_mode_str=sprintf("%04o",$to_mode_oct&0777);# permission bits3648}3649$to_file_type= file_type($diff->{'to_mode'});3650}3651if($diff->{'from_mode'}ne('0' x 6)) {3652$from_mode_oct=oct$diff->{'from_mode'};3653if(S_ISREG($to_mode_oct)) {# only for regular file3654$from_mode_str=sprintf("%04o",$from_mode_oct&0777);# permission bits3655}3656$from_file_type= file_type($diff->{'from_mode'});3657}36583659if($diff->{'status'}eq"A") {# created3660my$mode_chng="<span class=\"file_status new\">[new$to_file_type";3661$mode_chng.=" with mode:$to_mode_str"if$to_mode_str;3662$mode_chng.="]</span>";3663print"<td>";3664print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3665 hash_base=>$hash, file_name=>$diff->{'file'}),3666-class=>"list"}, esc_path($diff->{'file'}));3667print"</td>\n";3668print"<td>$mode_chng</td>\n";3669print"<td class=\"link\">";3670if($actioneq'commitdiff') {3671# link to patch3672$patchno++;3673print$cgi->a({-href =>"#patch$patchno"},"patch");3674print" | ";3675}3676print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3677 hash_base=>$hash, file_name=>$diff->{'file'})},3678"blob");3679print"</td>\n";36803681}elsif($diff->{'status'}eq"D") {# deleted3682my$mode_chng="<span class=\"file_status deleted\">[deleted$from_file_type]</span>";3683print"<td>";3684print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3685 hash_base=>$parent, file_name=>$diff->{'file'}),3686-class=>"list"}, esc_path($diff->{'file'}));3687print"</td>\n";3688print"<td>$mode_chng</td>\n";3689print"<td class=\"link\">";3690if($actioneq'commitdiff') {3691# link to patch3692$patchno++;3693print$cgi->a({-href =>"#patch$patchno"},"patch");3694print" | ";3695}3696print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'from_id'},3697 hash_base=>$parent, file_name=>$diff->{'file'})},3698"blob") ." | ";3699if($have_blame) {3700print$cgi->a({-href => href(action=>"blame", hash_base=>$parent,3701 file_name=>$diff->{'file'})},3702"blame") ." | ";3703}3704print$cgi->a({-href => href(action=>"history", hash_base=>$parent,3705 file_name=>$diff->{'file'})},3706"history");3707print"</td>\n";37083709}elsif($diff->{'status'}eq"M"||$diff->{'status'}eq"T") {# modified, or type changed3710my$mode_chnge="";3711if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3712$mode_chnge="<span class=\"file_status mode_chnge\">[changed";3713if($from_file_typene$to_file_type) {3714$mode_chnge.=" from$from_file_typeto$to_file_type";3715}3716if(($from_mode_oct&0777) != ($to_mode_oct&0777)) {3717if($from_mode_str&&$to_mode_str) {3718$mode_chnge.=" mode:$from_mode_str->$to_mode_str";3719}elsif($to_mode_str) {3720$mode_chnge.=" mode:$to_mode_str";3721}3722}3723$mode_chnge.="]</span>\n";3724}3725print"<td>";3726print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3727 hash_base=>$hash, file_name=>$diff->{'file'}),3728-class=>"list"}, esc_path($diff->{'file'}));3729print"</td>\n";3730print"<td>$mode_chnge</td>\n";3731print"<td class=\"link\">";3732if($actioneq'commitdiff') {3733# link to patch3734$patchno++;3735print$cgi->a({-href =>"#patch$patchno"},"patch") .3736" | ";3737}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3738# "commit" view and modified file (not onlu mode changed)3739print$cgi->a({-href => href(action=>"blobdiff",3740 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3741 hash_base=>$hash, hash_parent_base=>$parent,3742 file_name=>$diff->{'file'})},3743"diff") .3744" | ";3745}3746print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3747 hash_base=>$hash, file_name=>$diff->{'file'})},3748"blob") ." | ";3749if($have_blame) {3750print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3751 file_name=>$diff->{'file'})},3752"blame") ." | ";3753}3754print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3755 file_name=>$diff->{'file'})},3756"history");3757print"</td>\n";37583759}elsif($diff->{'status'}eq"R"||$diff->{'status'}eq"C") {# renamed or copied3760my%status_name= ('R'=>'moved','C'=>'copied');3761my$nstatus=$status_name{$diff->{'status'}};3762my$mode_chng="";3763if($diff->{'from_mode'} !=$diff->{'to_mode'}) {3764# mode also for directories, so we cannot use $to_mode_str3765$mode_chng=sprintf(", mode:%04o",$to_mode_oct&0777);3766}3767print"<td>".3768$cgi->a({-href => href(action=>"blob", hash_base=>$hash,3769 hash=>$diff->{'to_id'}, file_name=>$diff->{'to_file'}),3770-class=>"list"}, esc_path($diff->{'to_file'})) ."</td>\n".3771"<td><span class=\"file_status$nstatus\">[$nstatusfrom ".3772$cgi->a({-href => href(action=>"blob", hash_base=>$parent,3773 hash=>$diff->{'from_id'}, file_name=>$diff->{'from_file'}),3774-class=>"list"}, esc_path($diff->{'from_file'})) .3775" with ". (int$diff->{'similarity'}) ."% similarity$mode_chng]</span></td>\n".3776"<td class=\"link\">";3777if($actioneq'commitdiff') {3778# link to patch3779$patchno++;3780print$cgi->a({-href =>"#patch$patchno"},"patch") .3781" | ";3782}elsif($diff->{'to_id'}ne$diff->{'from_id'}) {3783# "commit" view and modified file (not only pure rename or copy)3784print$cgi->a({-href => href(action=>"blobdiff",3785 hash=>$diff->{'to_id'}, hash_parent=>$diff->{'from_id'},3786 hash_base=>$hash, hash_parent_base=>$parent,3787 file_name=>$diff->{'to_file'}, file_parent=>$diff->{'from_file'})},3788"diff") .3789" | ";3790}3791print$cgi->a({-href => href(action=>"blob", hash=>$diff->{'to_id'},3792 hash_base=>$parent, file_name=>$diff->{'to_file'})},3793"blob") ." | ";3794if($have_blame) {3795print$cgi->a({-href => href(action=>"blame", hash_base=>$hash,3796 file_name=>$diff->{'to_file'})},3797"blame") ." | ";3798}3799print$cgi->a({-href => href(action=>"history", hash_base=>$hash,3800 file_name=>$diff->{'to_file'})},3801"history");3802print"</td>\n";38033804}# we should not encounter Unmerged (U) or Unknown (X) status3805print"</tr>\n";3806}3807print"</tbody>"if$has_header;3808print"</table>\n";3809}38103811sub git_patchset_body {3812my($fd,$difftree,$hash,@hash_parents) =@_;3813my($hash_parent) =$hash_parents[0];38143815my$is_combined= (@hash_parents>1);3816my$patch_idx=0;3817my$patch_number=0;3818my$patch_line;3819my$diffinfo;3820my$to_name;3821my(%from,%to);38223823print"<div class=\"patchset\">\n";38243825# skip to first patch3826while($patch_line= <$fd>) {3827chomp$patch_line;38283829last if($patch_line=~m/^diff /);3830}38313832 PATCH:3833while($patch_line) {38343835# parse "git diff" header line3836if($patch_line=~m/^diff --git (\"(?:[^\\\"]*(?:\\.[^\\\"]*)*)\"|[^ "]*) (.*)$/) {3837# $1 is from_name, which we do not use3838$to_name= unquote($2);3839$to_name=~s!^b/!!;3840}elsif($patch_line=~m/^diff --(cc|combined) ("?.*"?)$/) {3841# $1 is 'cc' or 'combined', which we do not use3842$to_name= unquote($2);3843}else{3844$to_name=undef;3845}38463847# check if current patch belong to current raw line3848# and parse raw git-diff line if needed3849if(is_patch_split($diffinfo, {'to_file'=>$to_name})) {3850# this is continuation of a split patch3851print"<div class=\"patch cont\">\n";3852}else{3853# advance raw git-diff output if needed3854$patch_idx++ifdefined$diffinfo;38553856# read and prepare patch information3857$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);38583859# compact combined diff output can have some patches skipped3860# find which patch (using pathname of result) we are at now;3861if($is_combined) {3862while($to_namene$diffinfo->{'to_file'}) {3863print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3864 format_diff_cc_simplified($diffinfo,@hash_parents) .3865"</div>\n";# class="patch"38663867$patch_idx++;3868$patch_number++;38693870last if$patch_idx>$#$difftree;3871$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);3872}3873}38743875# modifies %from, %to hashes3876 parse_from_to_diffinfo($diffinfo, \%from, \%to,@hash_parents);38773878# this is first patch for raw difftree line with $patch_idx index3879# we index @$difftree array from 0, but number patches from 13880print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n";3881}38823883# git diff header3884#assert($patch_line =~ m/^diff /) if DEBUG;3885#assert($patch_line !~ m!$/$!) if DEBUG; # is chomp-ed3886$patch_number++;3887# print "git diff" header3888print format_git_diff_header_line($patch_line,$diffinfo,3889 \%from, \%to);38903891# print extended diff header3892print"<div class=\"diff extended_header\">\n";3893 EXTENDED_HEADER:3894while($patch_line= <$fd>) {3895chomp$patch_line;38963897last EXTENDED_HEADER if($patch_line=~m/^--- |^diff /);38983899print format_extended_diff_header_line($patch_line,$diffinfo,3900 \%from, \%to);3901}3902print"</div>\n";# class="diff extended_header"39033904# from-file/to-file diff header3905if(!$patch_line) {3906print"</div>\n";# class="patch"3907last PATCH;3908}3909next PATCH if($patch_line=~m/^diff /);3910#assert($patch_line =~ m/^---/) if DEBUG;39113912my$last_patch_line=$patch_line;3913$patch_line= <$fd>;3914chomp$patch_line;3915#assert($patch_line =~ m/^\+\+\+/) if DEBUG;39163917print format_diff_from_to_header($last_patch_line,$patch_line,3918$diffinfo, \%from, \%to,3919@hash_parents);39203921# the patch itself3922 LINE:3923while($patch_line= <$fd>) {3924chomp$patch_line;39253926next PATCH if($patch_line=~m/^diff /);39273928print format_diff_line($patch_line, \%from, \%to);3929}39303931}continue{3932print"</div>\n";# class="patch"3933}39343935# for compact combined (--cc) format, with chunk and patch simpliciaction3936# patchset might be empty, but there might be unprocessed raw lines3937for(++$patch_idxif$patch_number>0;3938$patch_idx<@$difftree;3939++$patch_idx) {3940# read and prepare patch information3941$diffinfo= parsed_difftree_line($difftree->[$patch_idx]);39423943# generate anchor for "patch" links in difftree / whatchanged part3944print"<div class=\"patch\"id=\"patch". ($patch_idx+1) ."\">\n".3945 format_diff_cc_simplified($diffinfo,@hash_parents) .3946"</div>\n";# class="patch"39473948$patch_number++;3949}39503951if($patch_number==0) {3952if(@hash_parents>1) {3953print"<div class=\"diff nodifferences\">Trivial merge</div>\n";3954}else{3955print"<div class=\"diff nodifferences\">No differences found</div>\n";3956}3957}39583959print"</div>\n";# class="patchset"3960}39613962# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39633964# fills project list info (age, description, owner, forks) for each3965# project in the list, removing invalid projects from returned list3966# NOTE: modifies $projlist, but does not remove entries from it3967sub fill_project_list_info {3968my($projlist,$check_forks) =@_;3969my@projects;39703971my$show_ctags= gitweb_check_feature('ctags');3972 PROJECT:3973foreachmy$pr(@$projlist) {3974my(@activity) = git_get_last_activity($pr->{'path'});3975unless(@activity) {3976next PROJECT;3977}3978($pr->{'age'},$pr->{'age_string'}) =@activity;3979if(!defined$pr->{'descr'}) {3980my$descr= git_get_project_description($pr->{'path'}) ||"";3981$descr= to_utf8($descr);3982$pr->{'descr_long'} =$descr;3983$pr->{'descr'} = chop_str($descr,$projects_list_description_width,5);3984}3985if(!defined$pr->{'owner'}) {3986$pr->{'owner'} = git_get_project_owner("$pr->{'path'}") ||"";3987}3988if($check_forks) {3989my$pname=$pr->{'path'};3990if(($pname=~s/\.git$//) &&3991($pname!~/\/$/) &&3992(-d "$projectroot/$pname")) {3993$pr->{'forks'} ="-d$projectroot/$pname";3994}else{3995$pr->{'forks'} =0;3996}3997}3998$show_ctagsand$pr->{'ctags'} = git_get_project_ctags($pr->{'path'});3999push@projects,$pr;4000}40014002return@projects;4003}40044005# print 'sort by' <th> element, generating 'sort by $name' replay link4006# if that order is not selected4007sub print_sort_th {4008my($name,$order,$header) =@_;4009$header||=ucfirst($name);40104011if($ordereq$name) {4012print"<th>$header</th>\n";4013}else{4014print"<th>".4015$cgi->a({-href => href(-replay=>1, order=>$name),4016-class=>"header"},$header) .4017"</th>\n";4018}4019}40204021sub git_project_list_body {4022# actually uses global variable $project4023my($projlist,$order,$from,$to,$extra,$no_header) =@_;40244025my$check_forks= gitweb_check_feature('forks');4026my@projects= fill_project_list_info($projlist,$check_forks);40274028$order||=$default_projects_order;4029$from=0unlessdefined$from;4030$to=$#projectsif(!defined$to||$#projects<$to);40314032my%order_info= (4033 project => { key =>'path', type =>'str'},4034 descr => { key =>'descr_long', type =>'str'},4035 owner => { key =>'owner', type =>'str'},4036 age => { key =>'age', type =>'num'}4037);4038my$oi=$order_info{$order};4039if($oi->{'type'}eq'str') {4040@projects=sort{$a->{$oi->{'key'}}cmp$b->{$oi->{'key'}}}@projects;4041}else{4042@projects=sort{$a->{$oi->{'key'}} <=>$b->{$oi->{'key'}}}@projects;4043}40444045my$show_ctags= gitweb_check_feature('ctags');4046if($show_ctags) {4047my%ctags;4048foreachmy$p(@projects) {4049foreachmy$ct(keys%{$p->{'ctags'}}) {4050$ctags{$ct} +=$p->{'ctags'}->{$ct};4051}4052}4053my$cloud= git_populate_project_tagcloud(\%ctags);4054print git_show_project_tagcloud($cloud,64);4055}40564057print"<table class=\"project_list\">\n";4058unless($no_header) {4059print"<tr>\n";4060if($check_forks) {4061print"<th></th>\n";4062}4063 print_sort_th('project',$order,'Project');4064 print_sort_th('descr',$order,'Description');4065 print_sort_th('owner',$order,'Owner');4066 print_sort_th('age',$order,'Last Change');4067print"<th></th>\n".# for links4068"</tr>\n";4069}4070my$alternate=1;4071my$tagfilter=$cgi->param('by_tag');4072for(my$i=$from;$i<=$to;$i++) {4073my$pr=$projects[$i];40744075next if$tagfilterand$show_ctagsand not grep{lc$_eq lc$tagfilter}keys%{$pr->{'ctags'}};4076next if$searchtextand not$pr->{'path'} =~/$searchtext/4077and not$pr->{'descr_long'} =~/$searchtext/;4078# Weed out forks or non-matching entries of search4079if($check_forks) {4080my$forkbase=$project;$forkbase||='';$forkbase=~ s#\.git$#/#;4081$forkbase="^$forkbase"if$forkbase;4082next ifnot$searchtextand not$tagfilterand$show_ctags4083and$pr->{'path'} =~ m#$forkbase.*/.*#; # regexp-safe4084}40854086if($alternate) {4087print"<tr class=\"dark\">\n";4088}else{4089print"<tr class=\"light\">\n";4090}4091$alternate^=1;4092if($check_forks) {4093print"<td>";4094if($pr->{'forks'}) {4095print"<!--$pr->{'forks'} -->\n";4096print$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"+");4097}4098print"</td>\n";4099}4100print"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4101-class=>"list"}, esc_html($pr->{'path'})) ."</td>\n".4102"<td>".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary"),4103-class=>"list", -title =>$pr->{'descr_long'}},4104 esc_html($pr->{'descr'})) ."</td>\n".4105"<td><i>". chop_and_escape_str($pr->{'owner'},15) ."</i></td>\n";4106print"<td class=\"". age_class($pr->{'age'}) ."\">".4107(defined$pr->{'age_string'} ?$pr->{'age_string'} :"No commits") ."</td>\n".4108"<td class=\"link\">".4109$cgi->a({-href => href(project=>$pr->{'path'}, action=>"summary")},"summary") ." | ".4110$cgi->a({-href => href(project=>$pr->{'path'}, action=>"shortlog")},"shortlog") ." | ".4111$cgi->a({-href => href(project=>$pr->{'path'}, action=>"log")},"log") ." | ".4112$cgi->a({-href => href(project=>$pr->{'path'}, action=>"tree")},"tree") .4113($pr->{'forks'} ?" | ".$cgi->a({-href => href(project=>$pr->{'path'}, action=>"forks")},"forks") :'') .4114"</td>\n".4115"</tr>\n";4116}4117if(defined$extra) {4118print"<tr>\n";4119if($check_forks) {4120print"<td></td>\n";4121}4122print"<td colspan=\"5\">$extra</td>\n".4123"</tr>\n";4124}4125print"</table>\n";4126}41274128sub git_shortlog_body {4129# uses global variable $project4130my($commitlist,$from,$to,$refs,$extra) =@_;41314132$from=0unlessdefined$from;4133$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);41344135print"<table class=\"shortlog\">\n";4136my$alternate=1;4137for(my$i=$from;$i<=$to;$i++) {4138my%co= %{$commitlist->[$i]};4139my$commit=$co{'id'};4140my$ref= format_ref_marker($refs,$commit);4141if($alternate) {4142print"<tr class=\"dark\">\n";4143}else{4144print"<tr class=\"light\">\n";4145}4146$alternate^=1;4147my$author= chop_and_escape_str($co{'author_name'},10);4148# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .4149print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4150"<td><i>".$author."</i></td>\n".4151"<td>";4152print format_subject_html($co{'title'},$co{'title_short'},4153 href(action=>"commit", hash=>$commit),$ref);4154print"</td>\n".4155"<td class=\"link\">".4156$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") ." | ".4157$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") ." | ".4158$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree");4159my$snapshot_links= format_snapshot_links($commit);4160if(defined$snapshot_links) {4161print" | ".$snapshot_links;4162}4163print"</td>\n".4164"</tr>\n";4165}4166if(defined$extra) {4167print"<tr>\n".4168"<td colspan=\"4\">$extra</td>\n".4169"</tr>\n";4170}4171print"</table>\n";4172}41734174sub git_history_body {4175# Warning: assumes constant type (blob or tree) during history4176my($commitlist,$from,$to,$refs,$hash_base,$ftype,$extra) =@_;41774178$from=0unlessdefined$from;4179$to=$#{$commitlist}unless(defined$to&&$to<=$#{$commitlist});41804181print"<table class=\"history\">\n";4182my$alternate=1;4183for(my$i=$from;$i<=$to;$i++) {4184my%co= %{$commitlist->[$i]};4185if(!%co) {4186next;4187}4188my$commit=$co{'id'};41894190my$ref= format_ref_marker($refs,$commit);41914192if($alternate) {4193print"<tr class=\"dark\">\n";4194}else{4195print"<tr class=\"light\">\n";4196}4197$alternate^=1;4198# shortlog uses chop_str($co{'author_name'}, 10)4199my$author= chop_and_escape_str($co{'author_name'},15,3);4200print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4201"<td><i>".$author."</i></td>\n".4202"<td>";4203# originally git_history used chop_str($co{'title'}, 50)4204print format_subject_html($co{'title'},$co{'title_short'},4205 href(action=>"commit", hash=>$commit),$ref);4206print"</td>\n".4207"<td class=\"link\">".4208$cgi->a({-href => href(action=>$ftype, hash_base=>$commit, file_name=>$file_name)},$ftype) ." | ".4209$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff");42104211if($ftypeeq'blob') {4212my$blob_current= git_get_hash_by_path($hash_base,$file_name);4213my$blob_parent= git_get_hash_by_path($commit,$file_name);4214if(defined$blob_current&&defined$blob_parent&&4215$blob_currentne$blob_parent) {4216print" | ".4217$cgi->a({-href => href(action=>"blobdiff",4218 hash=>$blob_current, hash_parent=>$blob_parent,4219 hash_base=>$hash_base, hash_parent_base=>$commit,4220 file_name=>$file_name)},4221"diff to current");4222}4223}4224print"</td>\n".4225"</tr>\n";4226}4227if(defined$extra) {4228print"<tr>\n".4229"<td colspan=\"4\">$extra</td>\n".4230"</tr>\n";4231}4232print"</table>\n";4233}42344235sub git_tags_body {4236# uses global variable $project4237my($taglist,$from,$to,$extra) =@_;4238$from=0unlessdefined$from;4239$to=$#{$taglist}if(!defined$to||$#{$taglist} <$to);42404241print"<table class=\"tags\">\n";4242my$alternate=1;4243for(my$i=$from;$i<=$to;$i++) {4244my$entry=$taglist->[$i];4245my%tag=%$entry;4246my$comment=$tag{'subject'};4247my$comment_short;4248if(defined$comment) {4249$comment_short= chop_str($comment,30,5);4250}4251if($alternate) {4252print"<tr class=\"dark\">\n";4253}else{4254print"<tr class=\"light\">\n";4255}4256$alternate^=1;4257if(defined$tag{'age'}) {4258print"<td><i>$tag{'age'}</i></td>\n";4259}else{4260print"<td></td>\n";4261}4262print"<td>".4263$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'}),4264-class=>"list name"}, esc_html($tag{'name'})) .4265"</td>\n".4266"<td>";4267if(defined$comment) {4268print format_subject_html($comment,$comment_short,4269 href(action=>"tag", hash=>$tag{'id'}));4270}4271print"</td>\n".4272"<td class=\"selflink\">";4273if($tag{'type'}eq"tag") {4274print$cgi->a({-href => href(action=>"tag", hash=>$tag{'id'})},"tag");4275}else{4276print" ";4277}4278print"</td>\n".4279"<td class=\"link\">"." | ".4280$cgi->a({-href => href(action=>$tag{'reftype'}, hash=>$tag{'refid'})},$tag{'reftype'});4281if($tag{'reftype'}eq"commit") {4282print" | ".$cgi->a({-href => href(action=>"shortlog", hash=>$tag{'fullname'})},"shortlog") .4283" | ".$cgi->a({-href => href(action=>"log", hash=>$tag{'fullname'})},"log");4284}elsif($tag{'reftype'}eq"blob") {4285print" | ".$cgi->a({-href => href(action=>"blob_plain", hash=>$tag{'refid'})},"raw");4286}4287print"</td>\n".4288"</tr>";4289}4290if(defined$extra) {4291print"<tr>\n".4292"<td colspan=\"5\">$extra</td>\n".4293"</tr>\n";4294}4295print"</table>\n";4296}42974298sub git_heads_body {4299# uses global variable $project4300my($headlist,$head,$from,$to,$extra) =@_;4301$from=0unlessdefined$from;4302$to=$#{$headlist}if(!defined$to||$#{$headlist} <$to);43034304print"<table class=\"heads\">\n";4305my$alternate=1;4306for(my$i=$from;$i<=$to;$i++) {4307my$entry=$headlist->[$i];4308my%ref=%$entry;4309my$curr=$ref{'id'}eq$head;4310if($alternate) {4311print"<tr class=\"dark\">\n";4312}else{4313print"<tr class=\"light\">\n";4314}4315$alternate^=1;4316print"<td><i>$ref{'age'}</i></td>\n".4317($curr?"<td class=\"current_head\">":"<td>") .4318$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'}),4319-class=>"list name"},esc_html($ref{'name'})) .4320"</td>\n".4321"<td class=\"link\">".4322$cgi->a({-href => href(action=>"shortlog", hash=>$ref{'fullname'})},"shortlog") ." | ".4323$cgi->a({-href => href(action=>"log", hash=>$ref{'fullname'})},"log") ." | ".4324$cgi->a({-href => href(action=>"tree", hash=>$ref{'fullname'}, hash_base=>$ref{'name'})},"tree") .4325"</td>\n".4326"</tr>";4327}4328if(defined$extra) {4329print"<tr>\n".4330"<td colspan=\"3\">$extra</td>\n".4331"</tr>\n";4332}4333print"</table>\n";4334}43354336sub git_search_grep_body {4337my($commitlist,$from,$to,$extra) =@_;4338$from=0unlessdefined$from;4339$to=$#{$commitlist}if(!defined$to||$#{$commitlist} <$to);43404341print"<table class=\"commit_search\">\n";4342my$alternate=1;4343for(my$i=$from;$i<=$to;$i++) {4344my%co= %{$commitlist->[$i]};4345if(!%co) {4346next;4347}4348my$commit=$co{'id'};4349if($alternate) {4350print"<tr class=\"dark\">\n";4351}else{4352print"<tr class=\"light\">\n";4353}4354$alternate^=1;4355my$author= chop_and_escape_str($co{'author_name'},15,5);4356print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".4357"<td><i>".$author."</i></td>\n".4358"<td>".4359$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),4360-class=>"list subject"},4361 chop_and_escape_str($co{'title'},50) ."<br/>");4362my$comment=$co{'comment'};4363foreachmy$line(@$comment) {4364if($line=~m/^(.*?)($search_regexp)(.*)$/i) {4365my($lead,$match,$trail) = ($1,$2,$3);4366$match= chop_str($match,70,5,'center');4367my$contextlen=int((80-length($match))/2);4368$contextlen=30if($contextlen>30);4369$lead= chop_str($lead,$contextlen,10,'left');4370$trail= chop_str($trail,$contextlen,10,'right');43714372$lead= esc_html($lead);4373$match= esc_html($match);4374$trail= esc_html($trail);43754376print"$lead<span class=\"match\">$match</span>$trail<br />";4377}4378}4379print"</td>\n".4380"<td class=\"link\">".4381$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .4382" | ".4383$cgi->a({-href => href(action=>"commitdiff", hash=>$co{'id'})},"commitdiff") .4384" | ".4385$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");4386print"</td>\n".4387"</tr>\n";4388}4389if(defined$extra) {4390print"<tr>\n".4391"<td colspan=\"3\">$extra</td>\n".4392"</tr>\n";4393}4394print"</table>\n";4395}43964397## ======================================================================4398## ======================================================================4399## actions44004401sub git_project_list {4402my$order=$input_params{'order'};4403if(defined$order&&$order!~m/none|project|descr|owner|age/) {4404 die_error(400,"Unknown order parameter");4405}44064407my@list= git_get_projects_list();4408if(!@list) {4409 die_error(404,"No projects found");4410}44114412 git_header_html();4413if(-f $home_text) {4414print"<div class=\"index_include\">\n";4415 insert_file($home_text);4416print"</div>\n";4417}4418print$cgi->startform(-method=>"get") .4419"<p class=\"projsearch\">Search:\n".4420$cgi->textfield(-name =>"s", -value =>$searchtext) ."\n".4421"</p>".4422$cgi->end_form() ."\n";4423 git_project_list_body(\@list,$order);4424 git_footer_html();4425}44264427sub git_forks {4428my$order=$input_params{'order'};4429if(defined$order&&$order!~m/none|project|descr|owner|age/) {4430 die_error(400,"Unknown order parameter");4431}44324433my@list= git_get_projects_list($project);4434if(!@list) {4435 die_error(404,"No forks found");4436}44374438 git_header_html();4439 git_print_page_nav('','');4440 git_print_header_div('summary',"$projectforks");4441 git_project_list_body(\@list,$order);4442 git_footer_html();4443}44444445sub git_project_index {4446my@projects= git_get_projects_list($project);44474448print$cgi->header(4449-type =>'text/plain',4450-charset =>'utf-8',4451-content_disposition =>'inline; filename="index.aux"');44524453foreachmy$pr(@projects) {4454if(!exists$pr->{'owner'}) {4455$pr->{'owner'} = git_get_project_owner("$pr->{'path'}");4456}44574458my($path,$owner) = ($pr->{'path'},$pr->{'owner'});4459# quote as in CGI::Util::encode, but keep the slash, and use '+' for ' '4460$path=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4461$owner=~s/([^a-zA-Z0-9_.\-\/ ])/sprintf("%%%02X",ord($1))/eg;4462$path=~s/ /\+/g;4463$owner=~s/ /\+/g;44644465print"$path$owner\n";4466}4467}44684469sub git_summary {4470my$descr= git_get_project_description($project) ||"none";4471my%co= parse_commit("HEAD");4472my%cd=%co? parse_date($co{'committer_epoch'},$co{'committer_tz'}) : ();4473my$head=$co{'id'};44744475my$owner= git_get_project_owner($project);44764477my$refs= git_get_references();4478# These get_*_list functions return one more to allow us to see if4479# there are more ...4480my@taglist= git_get_tags_list(16);4481my@headlist= git_get_heads_list(16);4482my@forklist;4483my$check_forks= gitweb_check_feature('forks');44844485if($check_forks) {4486@forklist= git_get_projects_list($project);4487}44884489 git_header_html();4490 git_print_page_nav('summary','',$head);44914492print"<div class=\"title\"> </div>\n";4493print"<table class=\"projects_list\">\n".4494"<tr id=\"metadata_desc\"><td>description</td><td>". esc_html($descr) ."</td></tr>\n".4495"<tr id=\"metadata_owner\"><td>owner</td><td>". esc_html($owner) ."</td></tr>\n";4496if(defined$cd{'rfc2822'}) {4497print"<tr id=\"metadata_lchange\"><td>last change</td><td>$cd{'rfc2822'}</td></tr>\n";4498}44994500# use per project git URL list in $projectroot/$project/cloneurl4501# or make project git URL from git base URL and project name4502my$url_tag="URL";4503my@url_list= git_get_project_url_list($project);4504@url_list=map{"$_/$project"}@git_base_url_listunless@url_list;4505foreachmy$git_url(@url_list) {4506next unless$git_url;4507print"<tr class=\"metadata_url\"><td>$url_tag</td><td>$git_url</td></tr>\n";4508$url_tag="";4509}45104511# Tag cloud4512my$show_ctags= gitweb_check_feature('ctags');4513if($show_ctags) {4514my$ctags= git_get_project_ctags($project);4515my$cloud= git_populate_project_tagcloud($ctags);4516print"<tr id=\"metadata_ctags\"><td>Content tags:<br />";4517print"</td>\n<td>"unless%$ctags;4518print"<form action=\"$show_ctags\"method=\"post\"><input type=\"hidden\"name=\"p\"value=\"$project\"/>Add: <input type=\"text\"name=\"t\"size=\"8\"/></form>";4519print"</td>\n<td>"if%$ctags;4520print git_show_project_tagcloud($cloud,48);4521print"</td></tr>";4522}45234524print"</table>\n";45254526# If XSS prevention is on, we don't include README.html.4527# TODO: Allow a readme in some safe format.4528if(!$prevent_xss&& -s "$projectroot/$project/README.html") {4529print"<div class=\"title\">readme</div>\n".4530"<div class=\"readme\">\n";4531 insert_file("$projectroot/$project/README.html");4532print"\n</div>\n";# class="readme"4533}45344535# we need to request one more than 16 (0..15) to check if4536# those 16 are all4537my@commitlist=$head? parse_commits($head,17) : ();4538if(@commitlist) {4539 git_print_header_div('shortlog');4540 git_shortlog_body(\@commitlist,0,15,$refs,4541$#commitlist<=15?undef:4542$cgi->a({-href => href(action=>"shortlog")},"..."));4543}45444545if(@taglist) {4546 git_print_header_div('tags');4547 git_tags_body(\@taglist,0,15,4548$#taglist<=15?undef:4549$cgi->a({-href => href(action=>"tags")},"..."));4550}45514552if(@headlist) {4553 git_print_header_div('heads');4554 git_heads_body(\@headlist,$head,0,15,4555$#headlist<=15?undef:4556$cgi->a({-href => href(action=>"heads")},"..."));4557}45584559if(@forklist) {4560 git_print_header_div('forks');4561 git_project_list_body(\@forklist,'age',0,15,4562$#forklist<=15?undef:4563$cgi->a({-href => href(action=>"forks")},"..."),4564'no_header');4565}45664567 git_footer_html();4568}45694570sub git_tag {4571my$head= git_get_head_hash($project);4572 git_header_html();4573 git_print_page_nav('','',$head,undef,$head);4574my%tag= parse_tag($hash);45754576if(!%tag) {4577 die_error(404,"Unknown tag object");4578}45794580 git_print_header_div('commit', esc_html($tag{'name'}),$hash);4581print"<div class=\"title_text\">\n".4582"<table class=\"object_header\">\n".4583"<tr>\n".4584"<td>object</td>\n".4585"<td>".$cgi->a({-class=>"list", -href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4586$tag{'object'}) ."</td>\n".4587"<td class=\"link\">".$cgi->a({-href => href(action=>$tag{'type'}, hash=>$tag{'object'})},4588$tag{'type'}) ."</td>\n".4589"</tr>\n";4590if(defined($tag{'author'})) {4591my%ad= parse_date($tag{'epoch'},$tag{'tz'});4592print"<tr><td>author</td><td>". esc_html($tag{'author'}) ."</td></tr>\n";4593print"<tr><td></td><td>".$ad{'rfc2822'} .4594sprintf(" (%02d:%02d%s)",$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'}) .4595"</td></tr>\n";4596}4597print"</table>\n\n".4598"</div>\n";4599print"<div class=\"page_body\">";4600my$comment=$tag{'comment'};4601foreachmy$line(@$comment) {4602chomp$line;4603print esc_html($line, -nbsp=>1) ."<br/>\n";4604}4605print"</div>\n";4606 git_footer_html();4607}46084609sub git_blame {4610# permissions4611 gitweb_check_feature('blame')4612or die_error(403,"Blame view not allowed");46134614# error checking4615 die_error(400,"No file name given")unless$file_name;4616$hash_base||= git_get_head_hash($project);4617 die_error(404,"Couldn't find base commit")unless$hash_base;4618my%co= parse_commit($hash_base)4619or die_error(404,"Commit not found");4620my$ftype="blob";4621if(!defined$hash) {4622$hash= git_get_hash_by_path($hash_base,$file_name,"blob")4623or die_error(404,"Error looking up file");4624}else{4625$ftype= git_get_type($hash);4626if($ftype!~"blob") {4627 die_error(400,"Object is not a blob");4628}4629}46304631# run git-blame --porcelain4632open my$fd,"-|", git_cmd(),"blame",'-p',4633$hash_base,'--',$file_name4634or die_error(500,"Open git-blame failed");46354636# page header4637 git_header_html();4638my$formats_nav=4639$cgi->a({-href => href(action=>"blob", -replay=>1)},4640"blob") .4641" | ".4642$cgi->a({-href => href(action=>"history", -replay=>1)},4643"history") .4644" | ".4645$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},4646"HEAD");4647 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4648 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4649 git_print_page_path($file_name,$ftype,$hash_base);46504651# page body4652my@rev_color=qw(light2 dark2);4653my$num_colors=scalar(@rev_color);4654my$current_color=0;4655my%metainfo= ();46564657print<<HTML;4658<div class="page_body">4659<table class="blame">4660<tr><th>Commit</th><th>Line</th><th>Data</th></tr>4661HTML4662 LINE:4663while(my$line= <$fd>) {4664chomp$line;4665# the header: <SHA-1> <src lineno> <dst lineno> [<lines in group>]4666# no <lines in group> for subsequent lines in group of lines4667my($full_rev,$orig_lineno,$lineno,$group_size) =4668($line=~/^([0-9a-f]{40}) (\d+) (\d+)(?: (\d+))?$/);4669if(!exists$metainfo{$full_rev}) {4670$metainfo{$full_rev} = {};4671}4672my$meta=$metainfo{$full_rev};4673my$data;4674while($data= <$fd>) {4675chomp$data;4676last if($data=~s/^\t//);# contents of line4677if($data=~/^(\S+) (.*)$/) {4678$meta->{$1} =$2;4679}4680}4681my$short_rev=substr($full_rev,0,8);4682my$author=$meta->{'author'};4683my%date=4684 parse_date($meta->{'author-time'},$meta->{'author-tz'});4685my$date=$date{'iso-tz'};4686if($group_size) {4687$current_color= ($current_color+1) %$num_colors;4688}4689print"<tr id=\"l$lineno\"class=\"$rev_color[$current_color]\">\n";4690if($group_size) {4691print"<td class=\"sha1\"";4692print" title=\"". esc_html($author) .",$date\"";4693print" rowspan=\"$group_size\""if($group_size>1);4694print">";4695print$cgi->a({-href => href(action=>"commit",4696 hash=>$full_rev,4697 file_name=>$file_name)},4698 esc_html($short_rev));4699print"</td>\n";4700}4701my$parent_commit;4702if(!exists$meta->{'parent'}) {4703open(my$dd,"-|", git_cmd(),"rev-parse","$full_rev^")4704or die_error(500,"Open git-rev-parse failed");4705$parent_commit= <$dd>;4706close$dd;4707chomp($parent_commit);4708$meta->{'parent'} =$parent_commit;4709}else{4710$parent_commit=$meta->{'parent'};4711}4712my$blamed= href(action =>'blame',4713 file_name =>$meta->{'filename'},4714 hash_base =>$parent_commit);4715print"<td class=\"linenr\">";4716print$cgi->a({ -href =>"$blamed#l$orig_lineno",4717-class=>"linenr"},4718 esc_html($lineno));4719print"</td>";4720print"<td class=\"pre\">". esc_html($data) ."</td>\n";4721print"</tr>\n";4722}4723print"</table>\n";4724print"</div>";4725close$fd4726or print"Reading blob failed\n";47274728# page footer4729 git_footer_html();4730}47314732sub git_tags {4733my$head= git_get_head_hash($project);4734 git_header_html();4735 git_print_page_nav('','',$head,undef,$head);4736 git_print_header_div('summary',$project);47374738my@tagslist= git_get_tags_list();4739if(@tagslist) {4740 git_tags_body(\@tagslist);4741}4742 git_footer_html();4743}47444745sub git_heads {4746my$head= git_get_head_hash($project);4747 git_header_html();4748 git_print_page_nav('','',$head,undef,$head);4749 git_print_header_div('summary',$project);47504751my@headslist= git_get_heads_list();4752if(@headslist) {4753 git_heads_body(\@headslist,$head);4754}4755 git_footer_html();4756}47574758sub git_blob_plain {4759my$type=shift;4760my$expires;47614762if(!defined$hash) {4763if(defined$file_name) {4764my$base=$hash_base|| git_get_head_hash($project);4765$hash= git_get_hash_by_path($base,$file_name,"blob")4766or die_error(404,"Cannot find file");4767}else{4768 die_error(400,"No file name defined");4769}4770}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4771# blobs defined by non-textual hash id's can be cached4772$expires="+1d";4773}47744775open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4776or die_error(500,"Open git-cat-file blob '$hash' failed");47774778# content-type (can include charset)4779$type= blob_contenttype($fd,$file_name,$type);47804781# "save as" filename, even when no $file_name is given4782my$save_as="$hash";4783if(defined$file_name) {4784$save_as=$file_name;4785}elsif($type=~m/^text\//) {4786$save_as.='.txt';4787}47884789# With XSS prevention on, blobs of all types except a few known safe4790# ones are served with "Content-Disposition: attachment" to make sure4791# they don't run in our security domain. For certain image types,4792# blob view writes an <img> tag referring to blob_plain view, and we4793# want to be sure not to break that by serving the image as an4794# attachment (though Firefox 3 doesn't seem to care).4795my$sandbox=$prevent_xss&&4796$type!~m!^(?:text/plain|image/(?:gif|png|jpeg))$!;47974798print$cgi->header(4799-type =>$type,4800-expires =>$expires,4801-content_disposition =>4802($sandbox?'attachment':'inline')4803.'; filename="'.$save_as.'"');4804local$/=undef;4805binmode STDOUT,':raw';4806print<$fd>;4807binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi4808close$fd;4809}48104811sub git_blob {4812my$expires;48134814if(!defined$hash) {4815if(defined$file_name) {4816my$base=$hash_base|| git_get_head_hash($project);4817$hash= git_get_hash_by_path($base,$file_name,"blob")4818or die_error(404,"Cannot find file");4819}else{4820 die_error(400,"No file name defined");4821}4822}elsif($hash=~m/^[0-9a-fA-F]{40}$/) {4823# blobs defined by non-textual hash id's can be cached4824$expires="+1d";4825}48264827my$have_blame= gitweb_check_feature('blame');4828open my$fd,"-|", git_cmd(),"cat-file","blob",$hash4829or die_error(500,"Couldn't cat$file_name,$hash");4830my$mimetype= blob_mimetype($fd,$file_name);4831if($mimetype!~m!^(?:text/|image/(?:gif|png|jpeg)$)!&& -B $fd) {4832close$fd;4833return git_blob_plain($mimetype);4834}4835# we can have blame only for text/* mimetype4836$have_blame&&= ($mimetype=~m!^text/!);48374838 git_header_html(undef,$expires);4839my$formats_nav='';4840if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4841if(defined$file_name) {4842if($have_blame) {4843$formats_nav.=4844$cgi->a({-href => href(action=>"blame", -replay=>1)},4845"blame") .4846" | ";4847}4848$formats_nav.=4849$cgi->a({-href => href(action=>"history", -replay=>1)},4850"history") .4851" | ".4852$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4853"raw") .4854" | ".4855$cgi->a({-href => href(action=>"blob",4856 hash_base=>"HEAD", file_name=>$file_name)},4857"HEAD");4858}else{4859$formats_nav.=4860$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},4861"raw");4862}4863 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);4864 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);4865}else{4866print"<div class=\"page_nav\">\n".4867"<br/><br/></div>\n".4868"<div class=\"title\">$hash</div>\n";4869}4870 git_print_page_path($file_name,"blob",$hash_base);4871print"<div class=\"page_body\">\n";4872if($mimetype=~m!^image/!) {4873print qq!<img type="$mimetype"!;4874if($file_name) {4875print qq! alt="$file_name" title="$file_name"!;4876}4877print qq! src="! .4878 href(action=>"blob_plain", hash=>$hash,4879 hash_base=>$hash_base, file_name=>$file_name) .4880 qq!"/>\n!;4881}else{4882my$nr;4883while(my$line= <$fd>) {4884chomp$line;4885$nr++;4886$line= untabify($line);4887printf"<div class=\"pre\"><a id=\"l%i\"href=\"#l%i\"class=\"linenr\">%4i</a>%s</div>\n",4888$nr,$nr,$nr, esc_html($line, -nbsp=>1);4889}4890}4891close$fd4892or print"Reading blob failed.\n";4893print"</div>";4894 git_footer_html();4895}48964897sub git_tree {4898if(!defined$hash_base) {4899$hash_base="HEAD";4900}4901if(!defined$hash) {4902if(defined$file_name) {4903$hash= git_get_hash_by_path($hash_base,$file_name,"tree");4904}else{4905$hash=$hash_base;4906}4907}4908 die_error(404,"No such tree")unlessdefined($hash);49094910my@entries= ();4911{4912local$/="\0";4913open my$fd,"-|", git_cmd(),"ls-tree",'-z',$hash4914or die_error(500,"Open git-ls-tree failed");4915@entries=map{chomp;$_} <$fd>;4916close$fd4917or die_error(404,"Reading tree failed");4918}49194920my$refs= git_get_references();4921my$ref= format_ref_marker($refs,$hash_base);4922 git_header_html();4923my$basedir='';4924my$have_blame= gitweb_check_feature('blame');4925if(defined$hash_base&& (my%co= parse_commit($hash_base))) {4926my@views_nav= ();4927if(defined$file_name) {4928push@views_nav,4929$cgi->a({-href => href(action=>"history", -replay=>1)},4930"history"),4931$cgi->a({-href => href(action=>"tree",4932 hash_base=>"HEAD", file_name=>$file_name)},4933"HEAD"),4934}4935my$snapshot_links= format_snapshot_links($hash);4936if(defined$snapshot_links) {4937# FIXME: Should be available when we have no hash base as well.4938push@views_nav,$snapshot_links;4939}4940 git_print_page_nav('tree','',$hash_base,undef,undef,join(' | ',@views_nav));4941 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash_base);4942}else{4943undef$hash_base;4944print"<div class=\"page_nav\">\n";4945print"<br/><br/></div>\n";4946print"<div class=\"title\">$hash</div>\n";4947}4948if(defined$file_name) {4949$basedir=$file_name;4950if($basedirne''&&substr($basedir, -1)ne'/') {4951$basedir.='/';4952}4953 git_print_page_path($file_name,'tree',$hash_base);4954}4955print"<div class=\"page_body\">\n";4956print"<table class=\"tree\">\n";4957my$alternate=1;4958# '..' (top directory) link if possible4959if(defined$hash_base&&4960defined$file_name&&$file_name=~m![^/]+$!) {4961if($alternate) {4962print"<tr class=\"dark\">\n";4963}else{4964print"<tr class=\"light\">\n";4965}4966$alternate^=1;49674968my$up=$file_name;4969$up=~s!/?[^/]+$!!;4970undef$upunless$up;4971# based on git_print_tree_entry4972print'<td class="mode">'. mode_str('040000') ."</td>\n";4973print'<td class="list">';4974print$cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,4975 file_name=>$up)},4976"..");4977print"</td>\n";4978print"<td class=\"link\"></td>\n";49794980print"</tr>\n";4981}4982foreachmy$line(@entries) {4983my%t= parse_ls_tree_line($line, -z =>1);49844985if($alternate) {4986print"<tr class=\"dark\">\n";4987}else{4988print"<tr class=\"light\">\n";4989}4990$alternate^=1;49914992 git_print_tree_entry(\%t,$basedir,$hash_base,$have_blame);49934994print"</tr>\n";4995}4996print"</table>\n".4997"</div>";4998 git_footer_html();4999}50005001sub git_snapshot {5002my$format=$input_params{'snapshot_format'};5003if(!@snapshot_fmts) {5004 die_error(403,"Snapshots not allowed");5005}5006# default to first supported snapshot format5007$format||=$snapshot_fmts[0];5008if($format!~m/^[a-z0-9]+$/) {5009 die_error(400,"Invalid snapshot format parameter");5010}elsif(!exists($known_snapshot_formats{$format})) {5011 die_error(400,"Unknown snapshot format");5012}elsif(!grep($_eq$format,@snapshot_fmts)) {5013 die_error(403,"Unsupported snapshot format");5014}50155016if(!defined$hash) {5017$hash= git_get_head_hash($project);5018}50195020my$name=$project;5021$name=~ s,([^/])/*\.git$,$1,;5022$name= basename($name);5023my$filename= to_utf8($name);5024$name=~s/\047/\047\\\047\047/g;5025my$cmd;5026$filename.="-$hash$known_snapshot_formats{$format}{'suffix'}";5027$cmd= quote_command(5028 git_cmd(),'archive',5029"--format=$known_snapshot_formats{$format}{'format'}",5030"--prefix=$name/",$hash);5031if(exists$known_snapshot_formats{$format}{'compressor'}) {5032$cmd.=' | '. quote_command(@{$known_snapshot_formats{$format}{'compressor'}});5033}50345035print$cgi->header(5036-type =>$known_snapshot_formats{$format}{'type'},5037-content_disposition =>'inline; filename="'."$filename".'"',5038-status =>'200 OK');50395040open my$fd,"-|",$cmd5041or die_error(500,"Execute git-archive failed");5042binmode STDOUT,':raw';5043print<$fd>;5044binmode STDOUT,':utf8';# as set at the beginning of gitweb.cgi5045close$fd;5046}50475048sub git_log {5049my$head= git_get_head_hash($project);5050if(!defined$hash) {5051$hash=$head;5052}5053if(!defined$page) {5054$page=0;5055}5056my$refs= git_get_references();50575058my@commitlist= parse_commits($hash,101, (100*$page));50595060my$paging_nav= format_paging_nav('log',$hash,$head,$page,$#commitlist>=100);50615062my($patch_max) = gitweb_get_feature('patches');5063if($patch_max) {5064if($patch_max<0||@commitlist<=$patch_max) {5065$paging_nav.=" ⋅ ".5066$cgi->a({-href => href(action=>"patches", -replay=>1)},5067"patches");5068}5069}50705071 git_header_html();5072 git_print_page_nav('log','',$hash,undef,undef,$paging_nav);50735074if(!@commitlist) {5075my%co= parse_commit($hash);50765077 git_print_header_div('summary',$project);5078print"<div class=\"page_body\"> Last change$co{'age_string'}.<br/><br/></div>\n";5079}5080my$to= ($#commitlist>=99) ? (99) : ($#commitlist);5081for(my$i=0;$i<=$to;$i++) {5082my%co= %{$commitlist[$i]};5083next if!%co;5084my$commit=$co{'id'};5085my$ref= format_ref_marker($refs,$commit);5086my%ad= parse_date($co{'author_epoch'});5087 git_print_header_div('commit',5088"<span class=\"age\">$co{'age_string'}</span>".5089 esc_html($co{'title'}) .$ref,5090$commit);5091print"<div class=\"title_text\">\n".5092"<div class=\"log_link\">\n".5093$cgi->a({-href => href(action=>"commit", hash=>$commit)},"commit") .5094" | ".5095$cgi->a({-href => href(action=>"commitdiff", hash=>$commit)},"commitdiff") .5096" | ".5097$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)},"tree") .5098"<br/>\n".5099"</div>\n".5100"<i>". esc_html($co{'author_name'}) ." [$ad{'rfc2822'}]</i><br/>\n".5101"</div>\n";51025103print"<div class=\"log_body\">\n";5104 git_print_log($co{'comment'}, -final_empty_line=>1);5105print"</div>\n";5106}5107if($#commitlist>=100) {5108print"<div class=\"page_nav\">\n";5109print$cgi->a({-href => href(-replay=>1, page=>$page+1),5110-accesskey =>"n", -title =>"Alt-n"},"next");5111print"</div>\n";5112}5113 git_footer_html();5114}51155116sub git_commit {5117$hash||=$hash_base||"HEAD";5118my%co= parse_commit($hash)5119or die_error(404,"Unknown commit object");5120my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5121my%cd= parse_date($co{'committer_epoch'},$co{'committer_tz'});51225123my$parent=$co{'parent'};5124my$parents=$co{'parents'};# listref51255126# we need to prepare $formats_nav before any parameter munging5127my$formats_nav;5128if(!defined$parent) {5129# --root commitdiff5130$formats_nav.='(initial)';5131}elsif(@$parents==1) {5132# single parent commit5133$formats_nav.=5134'(parent: '.5135$cgi->a({-href => href(action=>"commit",5136 hash=>$parent)},5137 esc_html(substr($parent,0,7))) .5138')';5139}else{5140# merge commit5141$formats_nav.=5142'(merge: '.5143join(' ',map{5144$cgi->a({-href => href(action=>"commit",5145 hash=>$_)},5146 esc_html(substr($_,0,7)));5147}@$parents) .5148')';5149}5150if(gitweb_check_feature('patches')) {5151$formats_nav.=" | ".5152$cgi->a({-href => href(action=>"patch", -replay=>1)},5153"patch");5154}51555156if(!defined$parent) {5157$parent="--root";5158}5159my@difftree;5160open my$fd,"-|", git_cmd(),"diff-tree",'-r',"--no-commit-id",5161@diff_opts,5162(@$parents<=1?$parent:'-c'),5163$hash,"--"5164or die_error(500,"Open git-diff-tree failed");5165@difftree=map{chomp;$_} <$fd>;5166close$fdor die_error(404,"Reading git-diff-tree failed");51675168# non-textual hash id's can be cached5169my$expires;5170if($hash=~m/^[0-9a-fA-F]{40}$/) {5171$expires="+1d";5172}5173my$refs= git_get_references();5174my$ref= format_ref_marker($refs,$co{'id'});51755176 git_header_html(undef,$expires);5177 git_print_page_nav('commit','',5178$hash,$co{'tree'},$hash,5179$formats_nav);51805181if(defined$co{'parent'}) {5182 git_print_header_div('commitdiff', esc_html($co{'title'}) .$ref,$hash);5183}else{5184 git_print_header_div('tree', esc_html($co{'title'}) .$ref,$co{'tree'},$hash);5185}5186print"<div class=\"title_text\">\n".5187"<table class=\"object_header\">\n";5188print"<tr><td>author</td><td>". esc_html($co{'author'}) ."</td></tr>\n".5189"<tr>".5190"<td></td><td>$ad{'rfc2822'}";5191if($ad{'hour_local'} <6) {5192printf(" (<span class=\"atnight\">%02d:%02d</span>%s)",5193$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5194}else{5195printf(" (%02d:%02d%s)",5196$ad{'hour_local'},$ad{'minute_local'},$ad{'tz_local'});5197}5198print"</td>".5199"</tr>\n";5200print"<tr><td>committer</td><td>". esc_html($co{'committer'}) ."</td></tr>\n";5201print"<tr><td></td><td>$cd{'rfc2822'}".5202sprintf(" (%02d:%02d%s)",$cd{'hour_local'},$cd{'minute_local'},$cd{'tz_local'}) .5203"</td></tr>\n";5204print"<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";5205print"<tr>".5206"<td>tree</td>".5207"<td class=\"sha1\">".5208$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash),5209class=>"list"},$co{'tree'}) .5210"</td>".5211"<td class=\"link\">".5212$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},5213"tree");5214my$snapshot_links= format_snapshot_links($hash);5215if(defined$snapshot_links) {5216print" | ".$snapshot_links;5217}5218print"</td>".5219"</tr>\n";52205221foreachmy$par(@$parents) {5222print"<tr>".5223"<td>parent</td>".5224"<td class=\"sha1\">".5225$cgi->a({-href => href(action=>"commit", hash=>$par),5226class=>"list"},$par) .5227"</td>".5228"<td class=\"link\">".5229$cgi->a({-href => href(action=>"commit", hash=>$par)},"commit") .5230" | ".5231$cgi->a({-href => href(action=>"commitdiff", hash=>$hash, hash_parent=>$par)},"diff") .5232"</td>".5233"</tr>\n";5234}5235print"</table>".5236"</div>\n";52375238print"<div class=\"page_body\">\n";5239 git_print_log($co{'comment'});5240print"</div>\n";52415242 git_difftree_body(\@difftree,$hash,@$parents);52435244 git_footer_html();5245}52465247sub git_object {5248# object is defined by:5249# - hash or hash_base alone5250# - hash_base and file_name5251my$type;52525253# - hash or hash_base alone5254if($hash|| ($hash_base&& !defined$file_name)) {5255my$object_id=$hash||$hash_base;52565257open my$fd,"-|", quote_command(5258 git_cmd(),'cat-file','-t',$object_id) .' 2> /dev/null'5259or die_error(404,"Object does not exist");5260$type= <$fd>;5261chomp$type;5262close$fd5263or die_error(404,"Object does not exist");52645265# - hash_base and file_name5266}elsif($hash_base&&defined$file_name) {5267$file_name=~ s,/+$,,;52685269system(git_cmd(),"cat-file",'-e',$hash_base) ==05270or die_error(404,"Base object does not exist");52715272# here errors should not hapen5273open my$fd,"-|", git_cmd(),"ls-tree",$hash_base,"--",$file_name5274or die_error(500,"Open git-ls-tree failed");5275my$line= <$fd>;5276close$fd;52775278#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c'5279unless($line&&$line=~m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t/) {5280 die_error(404,"File or directory for given base does not exist");5281}5282$type=$2;5283$hash=$3;5284}else{5285 die_error(400,"Not enough information to find object");5286}52875288print$cgi->redirect(-uri => href(action=>$type, -full=>1,5289 hash=>$hash, hash_base=>$hash_base,5290 file_name=>$file_name),5291-status =>'302 Found');5292}52935294sub git_blobdiff {5295my$format=shift||'html';52965297my$fd;5298my@difftree;5299my%diffinfo;5300my$expires;53015302# preparing $fd and %diffinfo for git_patchset_body5303# new style URI5304if(defined$hash_base&&defined$hash_parent_base) {5305if(defined$file_name) {5306# read raw output5307open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5308$hash_parent_base,$hash_base,5309"--", (defined$file_parent?$file_parent: ()),$file_name5310or die_error(500,"Open git-diff-tree failed");5311@difftree=map{chomp;$_} <$fd>;5312close$fd5313or die_error(404,"Reading git-diff-tree failed");5314@difftree5315or die_error(404,"Blob diff not found");53165317}elsif(defined$hash&&5318$hash=~/[0-9a-fA-F]{40}/) {5319# try to find filename from $hash53205321# read filtered raw output5322open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5323$hash_parent_base,$hash_base,"--"5324or die_error(500,"Open git-diff-tree failed");5325@difftree=5326# ':100644 100644 03b21826... 3b93d5e7... M ls-files.c'5327# $hash == to_id5328grep{/^:[0-7]{6} [0-7]{6} [0-9a-fA-F]{40} $hash/}5329map{chomp;$_} <$fd>;5330close$fd5331or die_error(404,"Reading git-diff-tree failed");5332@difftree5333or die_error(404,"Blob diff not found");53345335}else{5336 die_error(400,"Missing one of the blob diff parameters");5337}53385339if(@difftree>1) {5340 die_error(400,"Ambiguous blob diff specification");5341}53425343%diffinfo= parse_difftree_raw_line($difftree[0]);5344$file_parent||=$diffinfo{'from_file'} ||$file_name;5345$file_name||=$diffinfo{'to_file'};53465347$hash_parent||=$diffinfo{'from_id'};5348$hash||=$diffinfo{'to_id'};53495350# non-textual hash id's can be cached5351if($hash_base=~m/^[0-9a-fA-F]{40}$/&&5352$hash_parent_base=~m/^[0-9a-fA-F]{40}$/) {5353$expires='+1d';5354}53555356# open patch output5357open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5358'-p', ($formateq'html'?"--full-index": ()),5359$hash_parent_base,$hash_base,5360"--", (defined$file_parent?$file_parent: ()),$file_name5361or die_error(500,"Open git-diff-tree failed");5362}53635364# old/legacy style URI -- not generated anymore since 1.4.3.5365if(!%diffinfo) {5366 die_error('404 Not Found',"Missing one of the blob diff parameters")5367}53685369# header5370if($formateq'html') {5371my$formats_nav=5372$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},5373"raw");5374 git_header_html(undef,$expires);5375if(defined$hash_base&& (my%co= parse_commit($hash_base))) {5376 git_print_page_nav('','',$hash_base,$co{'tree'},$hash_base,$formats_nav);5377 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5378}else{5379print"<div class=\"page_nav\"><br/>$formats_nav<br/></div>\n";5380print"<div class=\"title\">$hashvs$hash_parent</div>\n";5381}5382if(defined$file_name) {5383 git_print_page_path($file_name,"blob",$hash_base);5384}else{5385print"<div class=\"page_path\"></div>\n";5386}53875388}elsif($formateq'plain') {5389print$cgi->header(5390-type =>'text/plain',5391-charset =>'utf-8',5392-expires =>$expires,5393-content_disposition =>'inline; filename="'."$file_name".'.patch"');53945395print"X-Git-Url: ".$cgi->self_url() ."\n\n";53965397}else{5398 die_error(400,"Unknown blobdiff format");5399}54005401# patch5402if($formateq'html') {5403print"<div class=\"page_body\">\n";54045405 git_patchset_body($fd, [ \%diffinfo],$hash_base,$hash_parent_base);5406close$fd;54075408print"</div>\n";# class="page_body"5409 git_footer_html();54105411}else{5412while(my$line= <$fd>) {5413$line=~s!a/($hash|$hash_parent)!'a/'.esc_path($diffinfo{'from_file'})!eg;5414$line=~s!b/($hash|$hash_parent)!'b/'.esc_path($diffinfo{'to_file'})!eg;54155416print$line;54175418last if$line=~m!^\+\+\+!;5419}5420local$/=undef;5421print<$fd>;5422close$fd;5423}5424}54255426sub git_blobdiff_plain {5427 git_blobdiff('plain');5428}54295430sub git_commitdiff {5431my%params=@_;5432my$format=$params{-format} ||'html';54335434my($patch_max) = gitweb_get_feature('patches');5435if($formateq'patch') {5436 die_error(403,"Patch view not allowed")unless$patch_max;5437}54385439$hash||=$hash_base||"HEAD";5440my%co= parse_commit($hash)5441or die_error(404,"Unknown commit object");54425443# choose format for commitdiff for merge5444if(!defined$hash_parent&& @{$co{'parents'}} >1) {5445$hash_parent='--cc';5446}5447# we need to prepare $formats_nav before almost any parameter munging5448my$formats_nav;5449if($formateq'html') {5450$formats_nav=5451$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},5452"raw");5453if($patch_max) {5454$formats_nav.=" | ".5455$cgi->a({-href => href(action=>"patch", -replay=>1)},5456"patch");5457}54585459if(defined$hash_parent&&5460$hash_parentne'-c'&&$hash_parentne'--cc') {5461# commitdiff with two commits given5462my$hash_parent_short=$hash_parent;5463if($hash_parent=~m/^[0-9a-fA-F]{40}$/) {5464$hash_parent_short=substr($hash_parent,0,7);5465}5466$formats_nav.=5467' (from';5468for(my$i=0;$i< @{$co{'parents'}};$i++) {5469if($co{'parents'}[$i]eq$hash_parent) {5470$formats_nav.=' parent '. ($i+1);5471last;5472}5473}5474$formats_nav.=': '.5475$cgi->a({-href => href(action=>"commitdiff",5476 hash=>$hash_parent)},5477 esc_html($hash_parent_short)) .5478')';5479}elsif(!$co{'parent'}) {5480# --root commitdiff5481$formats_nav.=' (initial)';5482}elsif(scalar@{$co{'parents'}} ==1) {5483# single parent commit5484$formats_nav.=5485' (parent: '.5486$cgi->a({-href => href(action=>"commitdiff",5487 hash=>$co{'parent'})},5488 esc_html(substr($co{'parent'},0,7))) .5489')';5490}else{5491# merge commit5492if($hash_parenteq'--cc') {5493$formats_nav.=' | '.5494$cgi->a({-href => href(action=>"commitdiff",5495 hash=>$hash, hash_parent=>'-c')},5496'combined');5497}else{# $hash_parent eq '-c'5498$formats_nav.=' | '.5499$cgi->a({-href => href(action=>"commitdiff",5500 hash=>$hash, hash_parent=>'--cc')},5501'compact');5502}5503$formats_nav.=5504' (merge: '.5505join(' ',map{5506$cgi->a({-href => href(action=>"commitdiff",5507 hash=>$_)},5508 esc_html(substr($_,0,7)));5509} @{$co{'parents'}} ) .5510')';5511}5512}55135514my$hash_parent_param=$hash_parent;5515if(!defined$hash_parent_param) {5516# --cc for multiple parents, --root for parentless5517$hash_parent_param=5518@{$co{'parents'}} >1?'--cc':$co{'parent'} ||'--root';5519}55205521# read commitdiff5522my$fd;5523my@difftree;5524if($formateq'html') {5525open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5526"--no-commit-id","--patch-with-raw","--full-index",5527$hash_parent_param,$hash,"--"5528or die_error(500,"Open git-diff-tree failed");55295530while(my$line= <$fd>) {5531chomp$line;5532# empty line ends raw part of diff-tree output5533last unless$line;5534push@difftree,scalar parse_difftree_raw_line($line);5535}55365537}elsif($formateq'plain') {5538open$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,5539'-p',$hash_parent_param,$hash,"--"5540or die_error(500,"Open git-diff-tree failed");5541}elsif($formateq'patch') {5542# For commit ranges, we limit the output to the number of5543# patches specified in the 'patches' feature.5544# For single commits, we limit the output to a single patch,5545# diverging from the git-format-patch default.5546my@commit_spec= ();5547if($hash_parent) {5548if($patch_max>0) {5549push@commit_spec,"-$patch_max";5550}5551push@commit_spec,'-n',"$hash_parent..$hash";5552}else{5553if($params{-single}) {5554push@commit_spec,'-1';5555}else{5556if($patch_max>0) {5557push@commit_spec,"-$patch_max";5558}5559push@commit_spec,"-n";5560}5561push@commit_spec,'--root',$hash;5562}5563open$fd,"-|", git_cmd(),"format-patch",'--encoding=utf8',5564'--stdout',@commit_spec5565or die_error(500,"Open git-format-patch failed");5566}else{5567 die_error(400,"Unknown commitdiff format");5568}55695570# non-textual hash id's can be cached5571my$expires;5572if($hash=~m/^[0-9a-fA-F]{40}$/) {5573$expires="+1d";5574}55755576# write commit message5577if($formateq'html') {5578my$refs= git_get_references();5579my$ref= format_ref_marker($refs,$co{'id'});55805581 git_header_html(undef,$expires);5582 git_print_page_nav('commitdiff','',$hash,$co{'tree'},$hash,$formats_nav);5583 git_print_header_div('commit', esc_html($co{'title'}) .$ref,$hash);5584 git_print_authorship(\%co);5585print"<div class=\"page_body\">\n";5586if(@{$co{'comment'}} >1) {5587print"<div class=\"log\">\n";5588 git_print_log($co{'comment'}, -final_empty_line=>1, -remove_title =>1);5589print"</div>\n";# class="log"5590}55915592}elsif($formateq'plain') {5593my$refs= git_get_references("tags");5594my$tagname= git_get_rev_name_tags($hash);5595my$filename= basename($project) ."-$hash.patch";55965597print$cgi->header(5598-type =>'text/plain',5599-charset =>'utf-8',5600-expires =>$expires,5601-content_disposition =>'inline; filename="'."$filename".'"');5602my%ad= parse_date($co{'author_epoch'},$co{'author_tz'});5603print"From: ". to_utf8($co{'author'}) ."\n";5604print"Date:$ad{'rfc2822'} ($ad{'tz_local'})\n";5605print"Subject: ". to_utf8($co{'title'}) ."\n";56065607print"X-Git-Tag:$tagname\n"if$tagname;5608print"X-Git-Url: ".$cgi->self_url() ."\n\n";56095610foreachmy$line(@{$co{'comment'}}) {5611print to_utf8($line) ."\n";5612}5613print"---\n\n";5614}elsif($formateq'patch') {5615my$filename= basename($project) ."-$hash.patch";56165617print$cgi->header(5618-type =>'text/plain',5619-charset =>'utf-8',5620-expires =>$expires,5621-content_disposition =>'inline; filename="'."$filename".'"');5622}56235624# write patch5625if($formateq'html') {5626my$use_parents= !defined$hash_parent||5627$hash_parenteq'-c'||$hash_parenteq'--cc';5628 git_difftree_body(\@difftree,$hash,5629$use_parents? @{$co{'parents'}} :$hash_parent);5630print"<br/>\n";56315632 git_patchset_body($fd, \@difftree,$hash,5633$use_parents? @{$co{'parents'}} :$hash_parent);5634close$fd;5635print"</div>\n";# class="page_body"5636 git_footer_html();56375638}elsif($formateq'plain') {5639local$/=undef;5640print<$fd>;5641close$fd5642or print"Reading git-diff-tree failed\n";5643}elsif($formateq'patch') {5644local$/=undef;5645print<$fd>;5646close$fd5647or print"Reading git-format-patch failed\n";5648}5649}56505651sub git_commitdiff_plain {5652 git_commitdiff(-format =>'plain');5653}56545655# format-patch-style patches5656sub git_patch {5657 git_commitdiff(-format =>'patch', -single=>1);5658}56595660sub git_patches {5661 git_commitdiff(-format =>'patch');5662}56635664sub git_history {5665if(!defined$hash_base) {5666$hash_base= git_get_head_hash($project);5667}5668if(!defined$page) {5669$page=0;5670}5671my$ftype;5672my%co= parse_commit($hash_base)5673or die_error(404,"Unknown commit object");56745675my$refs= git_get_references();5676my$limit=sprintf("--max-count=%i", (100* ($page+1)));56775678my@commitlist= parse_commits($hash_base,101, (100*$page),5679$file_name,"--full-history")5680or die_error(404,"No such file or directory on given branch");56815682if(!defined$hash&&defined$file_name) {5683# some commits could have deleted file in question,5684# and not have it in tree, but one of them has to have it5685for(my$i=0;$i<=@commitlist;$i++) {5686$hash= git_get_hash_by_path($commitlist[$i]{'id'},$file_name);5687last ifdefined$hash;5688}5689}5690if(defined$hash) {5691$ftype= git_get_type($hash);5692}5693if(!defined$ftype) {5694 die_error(500,"Unknown type of object");5695}56965697my$paging_nav='';5698if($page>0) {5699$paging_nav.=5700$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,5701 file_name=>$file_name)},5702"first");5703$paging_nav.=" ⋅ ".5704$cgi->a({-href => href(-replay=>1, page=>$page-1),5705-accesskey =>"p", -title =>"Alt-p"},"prev");5706}else{5707$paging_nav.="first";5708$paging_nav.=" ⋅ prev";5709}5710my$next_link='';5711if($#commitlist>=100) {5712$next_link=5713$cgi->a({-href => href(-replay=>1, page=>$page+1),5714-accesskey =>"n", -title =>"Alt-n"},"next");5715$paging_nav.=" ⋅$next_link";5716}else{5717$paging_nav.=" ⋅ next";5718}57195720 git_header_html();5721 git_print_page_nav('history','',$hash_base,$co{'tree'},$hash_base,$paging_nav);5722 git_print_header_div('commit', esc_html($co{'title'}),$hash_base);5723 git_print_page_path($file_name,$ftype,$hash_base);57245725 git_history_body(\@commitlist,0,99,5726$refs,$hash_base,$ftype,$next_link);57275728 git_footer_html();5729}57305731sub git_search {5732 gitweb_check_feature('search')or die_error(403,"Search is disabled");5733if(!defined$searchtext) {5734 die_error(400,"Text field is empty");5735}5736if(!defined$hash) {5737$hash= git_get_head_hash($project);5738}5739my%co= parse_commit($hash);5740if(!%co) {5741 die_error(404,"Unknown commit object");5742}5743if(!defined$page) {5744$page=0;5745}57465747$searchtype||='commit';5748if($searchtypeeq'pickaxe') {5749# pickaxe may take all resources of your box and run for several minutes5750# with every query - so decide by yourself how public you make this feature5751 gitweb_check_feature('pickaxe')5752or die_error(403,"Pickaxe is disabled");5753}5754if($searchtypeeq'grep') {5755 gitweb_check_feature('grep')5756or die_error(403,"Grep is disabled");5757}57585759 git_header_html();57605761if($searchtypeeq'commit'or$searchtypeeq'author'or$searchtypeeq'committer') {5762my$greptype;5763if($searchtypeeq'commit') {5764$greptype="--grep=";5765}elsif($searchtypeeq'author') {5766$greptype="--author=";5767}elsif($searchtypeeq'committer') {5768$greptype="--committer=";5769}5770$greptype.=$searchtext;5771my@commitlist= parse_commits($hash,101, (100*$page),undef,5772$greptype,'--regexp-ignore-case',5773$search_use_regexp?'--extended-regexp':'--fixed-strings');57745775my$paging_nav='';5776if($page>0) {5777$paging_nav.=5778$cgi->a({-href => href(action=>"search", hash=>$hash,5779 searchtext=>$searchtext,5780 searchtype=>$searchtype)},5781"first");5782$paging_nav.=" ⋅ ".5783$cgi->a({-href => href(-replay=>1, page=>$page-1),5784-accesskey =>"p", -title =>"Alt-p"},"prev");5785}else{5786$paging_nav.="first";5787$paging_nav.=" ⋅ prev";5788}5789my$next_link='';5790if($#commitlist>=100) {5791$next_link=5792$cgi->a({-href => href(-replay=>1, page=>$page+1),5793-accesskey =>"n", -title =>"Alt-n"},"next");5794$paging_nav.=" ⋅$next_link";5795}else{5796$paging_nav.=" ⋅ next";5797}57985799if($#commitlist>=100) {5800}58015802 git_print_page_nav('','',$hash,$co{'tree'},$hash,$paging_nav);5803 git_print_header_div('commit', esc_html($co{'title'}),$hash);5804 git_search_grep_body(\@commitlist,0,99,$next_link);5805}58065807if($searchtypeeq'pickaxe') {5808 git_print_page_nav('','',$hash,$co{'tree'},$hash);5809 git_print_header_div('commit', esc_html($co{'title'}),$hash);58105811print"<table class=\"pickaxe search\">\n";5812my$alternate=1;5813local$/="\n";5814open my$fd,'-|', git_cmd(),'--no-pager','log',@diff_opts,5815'--pretty=format:%H','--no-abbrev','--raw',"-S$searchtext",5816($search_use_regexp?'--pickaxe-regex': ());5817undef%co;5818my@files;5819while(my$line= <$fd>) {5820chomp$line;5821next unless$line;58225823my%set= parse_difftree_raw_line($line);5824if(defined$set{'commit'}) {5825# finish previous commit5826if(%co) {5827print"</td>\n".5828"<td class=\"link\">".5829$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5830" | ".5831$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5832print"</td>\n".5833"</tr>\n";5834}58355836if($alternate) {5837print"<tr class=\"dark\">\n";5838}else{5839print"<tr class=\"light\">\n";5840}5841$alternate^=1;5842%co= parse_commit($set{'commit'});5843my$author= chop_and_escape_str($co{'author_name'},15,5);5844print"<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n".5845"<td><i>$author</i></td>\n".5846"<td>".5847$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),5848-class=>"list subject"},5849 chop_and_escape_str($co{'title'},50) ."<br/>");5850}elsif(defined$set{'to_id'}) {5851next if($set{'to_id'} =~m/^0{40}$/);58525853print$cgi->a({-href => href(action=>"blob", hash_base=>$co{'id'},5854 hash=>$set{'to_id'}, file_name=>$set{'to_file'}),5855-class=>"list"},5856"<span class=\"match\">". esc_path($set{'file'}) ."</span>") .5857"<br/>\n";5858}5859}5860close$fd;58615862# finish last commit (warning: repetition!)5863if(%co) {5864print"</td>\n".5865"<td class=\"link\">".5866$cgi->a({-href => href(action=>"commit", hash=>$co{'id'})},"commit") .5867" | ".5868$cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$co{'id'})},"tree");5869print"</td>\n".5870"</tr>\n";5871}58725873print"</table>\n";5874}58755876if($searchtypeeq'grep') {5877 git_print_page_nav('','',$hash,$co{'tree'},$hash);5878 git_print_header_div('commit', esc_html($co{'title'}),$hash);58795880print"<table class=\"grep_search\">\n";5881my$alternate=1;5882my$matches=0;5883local$/="\n";5884open my$fd,"-|", git_cmd(),'grep','-n',5885$search_use_regexp? ('-E','-i') :'-F',5886$searchtext,$co{'tree'};5887my$lastfile='';5888while(my$line= <$fd>) {5889chomp$line;5890my($file,$lno,$ltext,$binary);5891last if($matches++>1000);5892if($line=~/^Binary file (.+) matches$/) {5893$file=$1;5894$binary=1;5895}else{5896(undef,$file,$lno,$ltext) =split(/:/,$line,4);5897}5898if($filene$lastfile) {5899$lastfileand print"</td></tr>\n";5900if($alternate++) {5901print"<tr class=\"dark\">\n";5902}else{5903print"<tr class=\"light\">\n";5904}5905print"<td class=\"list\">".5906$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5907 file_name=>"$file"),5908-class=>"list"}, esc_path($file));5909print"</td><td>\n";5910$lastfile=$file;5911}5912if($binary) {5913print"<div class=\"binary\">Binary file</div>\n";5914}else{5915$ltext= untabify($ltext);5916if($ltext=~m/^(.*)($search_regexp)(.*)$/i) {5917$ltext= esc_html($1, -nbsp=>1);5918$ltext.='<span class="match">';5919$ltext.= esc_html($2, -nbsp=>1);5920$ltext.='</span>';5921$ltext.= esc_html($3, -nbsp=>1);5922}else{5923$ltext= esc_html($ltext, -nbsp=>1);5924}5925print"<div class=\"pre\">".5926$cgi->a({-href => href(action=>"blob", hash=>$co{'hash'},5927 file_name=>"$file").'#l'.$lno,5928-class=>"linenr"},sprintf('%4i',$lno))5929.' '.$ltext."</div>\n";5930}5931}5932if($lastfile) {5933print"</td></tr>\n";5934if($matches>1000) {5935print"<div class=\"diff nodifferences\">Too many matches, listing trimmed</div>\n";5936}5937}else{5938print"<div class=\"diff nodifferences\">No matches found</div>\n";5939}5940close$fd;59415942print"</table>\n";5943}5944 git_footer_html();5945}59465947sub git_search_help {5948 git_header_html();5949 git_print_page_nav('','',$hash,$hash,$hash);5950print<<EOT;5951<p><strong>Pattern</strong> is by default a normal string that is matched precisely (but without5952regard to case, except in the case of pickaxe). However, when you check the <em>re</em> checkbox,5953the pattern entered is recognized as the POSIX extended5954<a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> (also case5955insensitive).</p>5956<dl>5957<dt><b>commit</b></dt>5958<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>5959EOT5960my$have_grep= gitweb_check_feature('grep');5961if($have_grep) {5962print<<EOT;5963<dt><b>grep</b></dt>5964<dd>All files in the currently selected tree (HEAD unless you are explicitly browsing5965 a different one) are searched for the given pattern. On large trees, this search can take5966a while and put some strain on the server, so please use it with some consideration. Note that5967due to git-grep peculiarity, currently if regexp mode is turned off, the matches are5968case-sensitive.</dd>5969EOT5970}5971print<<EOT;5972<dt><b>author</b></dt>5973<dd>Name and e-mail of the change author and date of birth of the patch will be scanned for the given pattern.</dd>5974<dt><b>committer</b></dt>5975<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>5976EOT5977my$have_pickaxe= gitweb_check_feature('pickaxe');5978if($have_pickaxe) {5979print<<EOT;5980<dt><b>pickaxe</b></dt>5981<dd>All commits that caused the string to appear or disappear from any file (changes that5982added, removed or "modified" the string) will be listed. This search can take a while and5983takes a lot of strain on the server, so please use it wisely. Note that since you may be5984interested even in changes just changing the case as well, this search is case sensitive.</dd>5985EOT5986}5987print"</dl>\n";5988 git_footer_html();5989}59905991sub git_shortlog {5992my$head= git_get_head_hash($project);5993if(!defined$hash) {5994$hash=$head;5995}5996if(!defined$page) {5997$page=0;5998}5999my$refs= git_get_references();60006001my$commit_hash=$hash;6002if(defined$hash_parent) {6003$commit_hash="$hash_parent..$hash";6004}6005my@commitlist= parse_commits($commit_hash,101, (100*$page));60066007my$paging_nav= format_paging_nav('shortlog',$hash,$head,$page,$#commitlist>=100);6008my$next_link='';6009if($#commitlist>=100) {6010$next_link=6011$cgi->a({-href => href(-replay=>1, page=>$page+1),6012-accesskey =>"n", -title =>"Alt-n"},"next");6013}6014my$patch_max= gitweb_check_feature('patches');6015if($patch_max) {6016if($patch_max<0||@commitlist<=$patch_max) {6017$paging_nav.=" ⋅ ".6018$cgi->a({-href => href(action=>"patches", -replay=>1)},6019"patches");6020}6021}60226023 git_header_html();6024 git_print_page_nav('shortlog','',$hash,$hash,$hash,$paging_nav);6025 git_print_header_div('summary',$project);60266027 git_shortlog_body(\@commitlist,0,99,$refs,$next_link);60286029 git_footer_html();6030}60316032## ......................................................................6033## feeds (RSS, Atom; OPML)60346035sub git_feed {6036my$format=shift||'atom';6037my$have_blame= gitweb_check_feature('blame');60386039# Atom: http://www.atomenabled.org/developers/syndication/6040# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ6041if($formatne'rss'&&$formatne'atom') {6042 die_error(400,"Unknown web feed format");6043}60446045# log/feed of current (HEAD) branch, log of given branch, history of file/directory6046my$head=$hash||'HEAD';6047my@commitlist= parse_commits($head,150,0,$file_name);60486049my%latest_commit;6050my%latest_date;6051my$content_type="application/$format+xml";6052if(defined$cgi->http('HTTP_ACCEPT') &&6053$cgi->Accept('text/xml') >$cgi->Accept($content_type)) {6054# browser (feed reader) prefers text/xml6055$content_type='text/xml';6056}6057if(defined($commitlist[0])) {6058%latest_commit= %{$commitlist[0]};6059my$latest_epoch=$latest_commit{'committer_epoch'};6060%latest_date= parse_date($latest_epoch);6061my$if_modified=$cgi->http('IF_MODIFIED_SINCE');6062if(defined$if_modified) {6063my$since;6064if(eval{require HTTP::Date;1; }) {6065$since= HTTP::Date::str2time($if_modified);6066}elsif(eval{require Time::ParseDate;1; }) {6067$since= Time::ParseDate::parsedate($if_modified, GMT =>1);6068}6069if(defined$since&&$latest_epoch<=$since) {6070print$cgi->header(6071-type =>$content_type,6072-charset =>'utf-8',6073-last_modified =>$latest_date{'rfc2822'},6074-status =>'304 Not Modified');6075return;6076}6077}6078print$cgi->header(6079-type =>$content_type,6080-charset =>'utf-8',6081-last_modified =>$latest_date{'rfc2822'});6082}else{6083print$cgi->header(6084-type =>$content_type,6085-charset =>'utf-8');6086}60876088# Optimization: skip generating the body if client asks only6089# for Last-Modified date.6090return if($cgi->request_method()eq'HEAD');60916092# header variables6093my$title="$site_name-$project/$action";6094my$feed_type='log';6095if(defined$hash) {6096$title.=" - '$hash'";6097$feed_type='branch log';6098if(defined$file_name) {6099$title.=" ::$file_name";6100$feed_type='history';6101}6102}elsif(defined$file_name) {6103$title.=" -$file_name";6104$feed_type='history';6105}6106$title.="$feed_type";6107my$descr= git_get_project_description($project);6108if(defined$descr) {6109$descr= esc_html($descr);6110}else{6111$descr="$project".6112($formateq'rss'?'RSS':'Atom') .6113" feed";6114}6115my$owner= git_get_project_owner($project);6116$owner= esc_html($owner);61176118#header6119my$alt_url;6120if(defined$file_name) {6121$alt_url= href(-full=>1, action=>"history", hash=>$hash, file_name=>$file_name);6122}elsif(defined$hash) {6123$alt_url= href(-full=>1, action=>"log", hash=>$hash);6124}else{6125$alt_url= href(-full=>1, action=>"summary");6126}6127print qq!<?xml version="1.0" encoding="utf-8"?>\n!;6128if($formateq'rss') {6129print<<XML;6130<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">6131<channel>6132XML6133print"<title>$title</title>\n".6134"<link>$alt_url</link>\n".6135"<description>$descr</description>\n".6136"<language>en</language>\n".6137# project owner is responsible for 'editorial' content6138"<managingEditor>$owner</managingEditor>\n";6139if(defined$logo||defined$favicon) {6140# prefer the logo to the favicon, since RSS6141# doesn't allow both6142my$img= esc_url($logo||$favicon);6143print"<image>\n".6144"<url>$img</url>\n".6145"<title>$title</title>\n".6146"<link>$alt_url</link>\n".6147"</image>\n";6148}6149if(%latest_date) {6150print"<pubDate>$latest_date{'rfc2822'}</pubDate>\n";6151print"<lastBuildDate>$latest_date{'rfc2822'}</lastBuildDate>\n";6152}6153print"<generator>gitweb v.$version/$git_version</generator>\n";6154}elsif($formateq'atom') {6155print<<XML;6156<feed xmlns="http://www.w3.org/2005/Atom">6157XML6158print"<title>$title</title>\n".6159"<subtitle>$descr</subtitle>\n".6160'<link rel="alternate" type="text/html" href="'.6161$alt_url.'" />'."\n".6162'<link rel="self" type="'.$content_type.'" href="'.6163$cgi->self_url() .'" />'."\n".6164"<id>". href(-full=>1) ."</id>\n".6165# use project owner for feed author6166"<author><name>$owner</name></author>\n";6167if(defined$favicon) {6168print"<icon>". esc_url($favicon) ."</icon>\n";6169}6170if(defined$logo_url) {6171# not twice as wide as tall: 72 x 27 pixels6172print"<logo>". esc_url($logo) ."</logo>\n";6173}6174if(!%latest_date) {6175# dummy date to keep the feed valid until commits trickle in:6176print"<updated>1970-01-01T00:00:00Z</updated>\n";6177}else{6178print"<updated>$latest_date{'iso-8601'}</updated>\n";6179}6180print"<generator version='$version/$git_version'>gitweb</generator>\n";6181}61826183# contents6184for(my$i=0;$i<=$#commitlist;$i++) {6185my%co= %{$commitlist[$i]};6186my$commit=$co{'id'};6187# we read 150, we always show 30 and the ones more recent than 48 hours6188if(($i>=20) && ((time-$co{'author_epoch'}) >48*60*60)) {6189last;6190}6191my%cd= parse_date($co{'author_epoch'});61926193# get list of changed files6194open my$fd,"-|", git_cmd(),"diff-tree",'-r',@diff_opts,6195$co{'parent'} ||"--root",6196$co{'id'},"--", (defined$file_name?$file_name: ())6197ornext;6198my@difftree=map{chomp;$_} <$fd>;6199close$fd6200ornext;62016202# print element (entry, item)6203my$co_url= href(-full=>1, action=>"commitdiff", hash=>$commit);6204if($formateq'rss') {6205print"<item>\n".6206"<title>". esc_html($co{'title'}) ."</title>\n".6207"<author>". esc_html($co{'author'}) ."</author>\n".6208"<pubDate>$cd{'rfc2822'}</pubDate>\n".6209"<guid isPermaLink=\"true\">$co_url</guid>\n".6210"<link>$co_url</link>\n".6211"<description>". esc_html($co{'title'}) ."</description>\n".6212"<content:encoded>".6213"<![CDATA[\n";6214}elsif($formateq'atom') {6215print"<entry>\n".6216"<title type=\"html\">". esc_html($co{'title'}) ."</title>\n".6217"<updated>$cd{'iso-8601'}</updated>\n".6218"<author>\n".6219" <name>". esc_html($co{'author_name'}) ."</name>\n";6220if($co{'author_email'}) {6221print" <email>". esc_html($co{'author_email'}) ."</email>\n";6222}6223print"</author>\n".6224# use committer for contributor6225"<contributor>\n".6226" <name>". esc_html($co{'committer_name'}) ."</name>\n";6227if($co{'committer_email'}) {6228print" <email>". esc_html($co{'committer_email'}) ."</email>\n";6229}6230print"</contributor>\n".6231"<published>$cd{'iso-8601'}</published>\n".6232"<link rel=\"alternate\"type=\"text/html\"href=\"$co_url\"/>\n".6233"<id>$co_url</id>\n".6234"<content type=\"xhtml\"xml:base=\"". esc_url($my_url) ."\">\n".6235"<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";6236}6237my$comment=$co{'comment'};6238print"<pre>\n";6239foreachmy$line(@$comment) {6240$line= esc_html($line);6241print"$line\n";6242}6243print"</pre><ul>\n";6244foreachmy$difftree_line(@difftree) {6245my%difftree= parse_difftree_raw_line($difftree_line);6246next if!$difftree{'from_id'};62476248my$file=$difftree{'file'} ||$difftree{'to_file'};62496250print"<li>".6251"[".6252$cgi->a({-href => href(-full=>1, action=>"blobdiff",6253 hash=>$difftree{'to_id'}, hash_parent=>$difftree{'from_id'},6254 hash_base=>$co{'id'}, hash_parent_base=>$co{'parent'},6255 file_name=>$file, file_parent=>$difftree{'from_file'}),6256-title =>"diff"},'D');6257if($have_blame) {6258print$cgi->a({-href => href(-full=>1, action=>"blame",6259 file_name=>$file, hash_base=>$commit),6260-title =>"blame"},'B');6261}6262# if this is not a feed of a file history6263if(!defined$file_name||$file_namene$file) {6264print$cgi->a({-href => href(-full=>1, action=>"history",6265 file_name=>$file, hash=>$commit),6266-title =>"history"},'H');6267}6268$file= esc_path($file);6269print"] ".6270"$file</li>\n";6271}6272if($formateq'rss') {6273print"</ul>]]>\n".6274"</content:encoded>\n".6275"</item>\n";6276}elsif($formateq'atom') {6277print"</ul>\n</div>\n".6278"</content>\n".6279"</entry>\n";6280}6281}62826283# end of feed6284if($formateq'rss') {6285print"</channel>\n</rss>\n";6286}elsif($formateq'atom') {6287print"</feed>\n";6288}6289}62906291sub git_rss {6292 git_feed('rss');6293}62946295sub git_atom {6296 git_feed('atom');6297}62986299sub git_opml {6300my@list= git_get_projects_list();63016302print$cgi->header(6303-type =>'text/xml',6304-charset =>'utf-8',6305-content_disposition =>'inline; filename="opml.xml"');63066307print<<XML;6308<?xml version="1.0" encoding="utf-8"?>6309<opml version="1.0">6310<head>6311 <title>$site_nameOPML Export</title>6312</head>6313<body>6314<outline text="git RSS feeds">6315XML63166317foreachmy$pr(@list) {6318my%proj=%$pr;6319my$head= git_get_head_hash($proj{'path'});6320if(!defined$head) {6321next;6322}6323$git_dir="$projectroot/$proj{'path'}";6324my%co= parse_commit($head);6325if(!%co) {6326next;6327}63286329my$path= esc_html(chop_str($proj{'path'},25,5));6330my$rss= href('project'=>$proj{'path'},'action'=>'rss', -full =>1);6331my$html= href('project'=>$proj{'path'},'action'=>'summary', -full =>1);6332print"<outline type=\"rss\"text=\"$path\"title=\"$path\"xmlUrl=\"$rss\"htmlUrl=\"$html\"/>\n";6333}6334print<<XML;6335</outline>6336</body>6337</opml>6338XML6339}